seq_clientmgr.c 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * ALSA sequencer Client Manager
  4. * Copyright (c) 1998-2001 by Frank van de Pol <fvdpol@coil.demon.nl>
  5. * Jaroslav Kysela <perex@perex.cz>
  6. * Takashi Iwai <tiwai@suse.de>
  7. */
  8. #include <linux/init.h>
  9. #include <linux/export.h>
  10. #include <linux/slab.h>
  11. #include <sound/core.h>
  12. #include <sound/minors.h>
  13. #include <linux/kmod.h>
  14. #include <sound/seq_kernel.h>
  15. #include <sound/ump.h>
  16. #include "seq_clientmgr.h"
  17. #include "seq_memory.h"
  18. #include "seq_queue.h"
  19. #include "seq_timer.h"
  20. #include "seq_info.h"
  21. #include "seq_system.h"
  22. #include "seq_ump_convert.h"
  23. #include <sound/seq_device.h>
  24. #ifdef CONFIG_COMPAT
  25. #include <linux/compat.h>
  26. #endif
  27. /* Client Manager
  28. * this module handles the connections of userland and kernel clients
  29. *
  30. */
  31. /*
  32. * There are four ranges of client numbers (last two shared):
  33. * 0..15: global clients
  34. * 16..127: statically allocated client numbers for cards 0..27
  35. * 128..191: dynamically allocated client numbers for cards 28..31
  36. * 128..191: dynamically allocated client numbers for applications
  37. */
  38. /* number of kernel non-card clients */
  39. #define SNDRV_SEQ_GLOBAL_CLIENTS 16
  40. /* clients per cards, for static clients */
  41. #define SNDRV_SEQ_CLIENTS_PER_CARD 4
  42. /* dynamically allocated client numbers (both kernel drivers and user space) */
  43. #define SNDRV_SEQ_DYNAMIC_CLIENTS_BEGIN 128
  44. #define SNDRV_SEQ_LFLG_INPUT 0x0001
  45. #define SNDRV_SEQ_LFLG_OUTPUT 0x0002
  46. #define SNDRV_SEQ_LFLG_OPEN (SNDRV_SEQ_LFLG_INPUT|SNDRV_SEQ_LFLG_OUTPUT)
  47. static DEFINE_SPINLOCK(clients_lock);
  48. static DEFINE_MUTEX(register_mutex);
  49. /*
  50. * client table
  51. */
  52. static char clienttablock[SNDRV_SEQ_MAX_CLIENTS];
  53. static struct snd_seq_client *clienttab[SNDRV_SEQ_MAX_CLIENTS];
  54. static struct snd_seq_usage client_usage;
  55. /*
  56. * prototypes
  57. */
  58. static int bounce_error_event(struct snd_seq_client *client,
  59. struct snd_seq_event *event,
  60. int err, int atomic, int hop);
  61. static int snd_seq_deliver_single_event(struct snd_seq_client *client,
  62. struct snd_seq_event *event,
  63. int atomic, int hop);
  64. #if IS_ENABLED(CONFIG_SND_SEQ_UMP)
  65. static void free_ump_info(struct snd_seq_client *client);
  66. #endif
  67. /*
  68. */
  69. static inline unsigned short snd_seq_file_flags(struct file *file)
  70. {
  71. switch (file->f_mode & (FMODE_READ | FMODE_WRITE)) {
  72. case FMODE_WRITE:
  73. return SNDRV_SEQ_LFLG_OUTPUT;
  74. case FMODE_READ:
  75. return SNDRV_SEQ_LFLG_INPUT;
  76. default:
  77. return SNDRV_SEQ_LFLG_OPEN;
  78. }
  79. }
  80. static inline int snd_seq_write_pool_allocated(struct snd_seq_client *client)
  81. {
  82. return snd_seq_total_cells(client->pool) > 0;
  83. }
  84. /* return pointer to client structure for specified id */
  85. static struct snd_seq_client *clientptr(int clientid)
  86. {
  87. if (clientid < 0 || clientid >= SNDRV_SEQ_MAX_CLIENTS) {
  88. pr_debug("ALSA: seq: oops. Trying to get pointer to client %d\n",
  89. clientid);
  90. return NULL;
  91. }
  92. return clienttab[clientid];
  93. }
  94. static struct snd_seq_client *client_use_ptr(int clientid, bool load_module)
  95. {
  96. unsigned long flags;
  97. struct snd_seq_client *client;
  98. if (clientid < 0 || clientid >= SNDRV_SEQ_MAX_CLIENTS) {
  99. pr_debug("ALSA: seq: oops. Trying to get pointer to client %d\n",
  100. clientid);
  101. return NULL;
  102. }
  103. spin_lock_irqsave(&clients_lock, flags);
  104. client = clientptr(clientid);
  105. if (client)
  106. goto __lock;
  107. if (clienttablock[clientid]) {
  108. spin_unlock_irqrestore(&clients_lock, flags);
  109. return NULL;
  110. }
  111. spin_unlock_irqrestore(&clients_lock, flags);
  112. #ifdef CONFIG_MODULES
  113. if (load_module) {
  114. static DECLARE_BITMAP(client_requested, SNDRV_SEQ_GLOBAL_CLIENTS);
  115. static DECLARE_BITMAP(card_requested, SNDRV_CARDS);
  116. if (clientid < SNDRV_SEQ_GLOBAL_CLIENTS) {
  117. int idx;
  118. if (!test_and_set_bit(clientid, client_requested)) {
  119. for (idx = 0; idx < 15; idx++) {
  120. if (seq_client_load[idx] < 0)
  121. break;
  122. if (seq_client_load[idx] == clientid) {
  123. request_module("snd-seq-client-%i",
  124. clientid);
  125. break;
  126. }
  127. }
  128. }
  129. } else if (clientid < SNDRV_SEQ_DYNAMIC_CLIENTS_BEGIN) {
  130. int card = (clientid - SNDRV_SEQ_GLOBAL_CLIENTS) /
  131. SNDRV_SEQ_CLIENTS_PER_CARD;
  132. if (card < snd_ecards_limit) {
  133. if (!test_and_set_bit(card, card_requested))
  134. snd_request_card(card);
  135. snd_seq_device_load_drivers();
  136. }
  137. }
  138. spin_lock_irqsave(&clients_lock, flags);
  139. client = clientptr(clientid);
  140. if (client)
  141. goto __lock;
  142. spin_unlock_irqrestore(&clients_lock, flags);
  143. }
  144. #endif
  145. return NULL;
  146. __lock:
  147. snd_use_lock_use(&client->use_lock);
  148. spin_unlock_irqrestore(&clients_lock, flags);
  149. return client;
  150. }
  151. /* get snd_seq_client object for the given id quickly */
  152. struct snd_seq_client *snd_seq_client_use_ptr(int clientid)
  153. {
  154. return client_use_ptr(clientid, false);
  155. }
  156. /* get snd_seq_client object for the given id;
  157. * if not found, retry after loading the modules
  158. */
  159. static struct snd_seq_client *client_load_and_use_ptr(int clientid)
  160. {
  161. return client_use_ptr(clientid, IS_ENABLED(CONFIG_MODULES));
  162. }
  163. /* Take refcount and perform ioctl_mutex lock on the given client;
  164. * used only for OSS sequencer
  165. * Unlock via snd_seq_client_ioctl_unlock() below
  166. */
  167. bool snd_seq_client_ioctl_lock(int clientid)
  168. {
  169. struct snd_seq_client *client;
  170. client = client_load_and_use_ptr(clientid);
  171. if (!client)
  172. return false;
  173. mutex_lock(&client->ioctl_mutex);
  174. /* The client isn't unrefed here; see snd_seq_client_ioctl_unlock() */
  175. return true;
  176. }
  177. EXPORT_SYMBOL_GPL(snd_seq_client_ioctl_lock);
  178. /* Unlock and unref the given client; for OSS sequencer use only */
  179. void snd_seq_client_ioctl_unlock(int clientid)
  180. {
  181. struct snd_seq_client *client;
  182. client = snd_seq_client_use_ptr(clientid);
  183. if (WARN_ON(!client))
  184. return;
  185. mutex_unlock(&client->ioctl_mutex);
  186. /* The doubly unrefs below are intentional; the first one releases the
  187. * leftover from snd_seq_client_ioctl_lock() above, and the second one
  188. * is for releasing snd_seq_client_use_ptr() in this function
  189. */
  190. snd_seq_client_unlock(client);
  191. snd_seq_client_unlock(client);
  192. }
  193. EXPORT_SYMBOL_GPL(snd_seq_client_ioctl_unlock);
  194. static void usage_alloc(struct snd_seq_usage *res, int num)
  195. {
  196. res->cur += num;
  197. if (res->cur > res->peak)
  198. res->peak = res->cur;
  199. }
  200. static void usage_free(struct snd_seq_usage *res, int num)
  201. {
  202. res->cur -= num;
  203. }
  204. /* initialise data structures */
  205. int __init client_init_data(void)
  206. {
  207. /* zap out the client table */
  208. memset(&clienttablock, 0, sizeof(clienttablock));
  209. memset(&clienttab, 0, sizeof(clienttab));
  210. return 0;
  211. }
  212. static struct snd_seq_client *seq_create_client1(int client_index, int poolsize)
  213. {
  214. int c;
  215. struct snd_seq_client *client;
  216. /* init client data */
  217. client = kzalloc(sizeof(*client), GFP_KERNEL);
  218. if (client == NULL)
  219. return NULL;
  220. client->pool = snd_seq_pool_new(poolsize);
  221. if (client->pool == NULL) {
  222. kfree(client);
  223. return NULL;
  224. }
  225. client->type = NO_CLIENT;
  226. snd_use_lock_init(&client->use_lock);
  227. rwlock_init(&client->ports_lock);
  228. mutex_init(&client->ports_mutex);
  229. INIT_LIST_HEAD(&client->ports_list_head);
  230. mutex_init(&client->ioctl_mutex);
  231. client->ump_endpoint_port = -1;
  232. /* find free slot in the client table */
  233. spin_lock_irq(&clients_lock);
  234. if (client_index < 0) {
  235. for (c = SNDRV_SEQ_DYNAMIC_CLIENTS_BEGIN;
  236. c < SNDRV_SEQ_MAX_CLIENTS;
  237. c++) {
  238. if (clienttab[c] || clienttablock[c])
  239. continue;
  240. clienttab[client->number = c] = client;
  241. spin_unlock_irq(&clients_lock);
  242. return client;
  243. }
  244. } else {
  245. if (clienttab[client_index] == NULL && !clienttablock[client_index]) {
  246. clienttab[client->number = client_index] = client;
  247. spin_unlock_irq(&clients_lock);
  248. return client;
  249. }
  250. }
  251. spin_unlock_irq(&clients_lock);
  252. snd_seq_pool_delete(&client->pool);
  253. kfree(client);
  254. return NULL; /* no free slot found or busy, return failure code */
  255. }
  256. static int seq_free_client1(struct snd_seq_client *client)
  257. {
  258. if (!client)
  259. return 0;
  260. spin_lock_irq(&clients_lock);
  261. clienttablock[client->number] = 1;
  262. clienttab[client->number] = NULL;
  263. spin_unlock_irq(&clients_lock);
  264. snd_seq_delete_all_ports(client);
  265. snd_seq_queue_client_leave(client->number);
  266. snd_use_lock_sync(&client->use_lock);
  267. if (client->pool)
  268. snd_seq_pool_delete(&client->pool);
  269. spin_lock_irq(&clients_lock);
  270. clienttablock[client->number] = 0;
  271. spin_unlock_irq(&clients_lock);
  272. return 0;
  273. }
  274. static void seq_free_client(struct snd_seq_client * client)
  275. {
  276. mutex_lock(&register_mutex);
  277. switch (client->type) {
  278. case NO_CLIENT:
  279. pr_warn("ALSA: seq: Trying to free unused client %d\n",
  280. client->number);
  281. break;
  282. case USER_CLIENT:
  283. case KERNEL_CLIENT:
  284. seq_free_client1(client);
  285. usage_free(&client_usage, 1);
  286. break;
  287. default:
  288. pr_err("ALSA: seq: Trying to free client %d with undefined type = %d\n",
  289. client->number, client->type);
  290. }
  291. mutex_unlock(&register_mutex);
  292. snd_seq_system_client_ev_client_exit(client->number);
  293. }
  294. /* -------------------------------------------------------- */
  295. /* create a user client */
  296. static int snd_seq_open(struct inode *inode, struct file *file)
  297. {
  298. int c, mode; /* client id */
  299. struct snd_seq_client *client;
  300. struct snd_seq_user_client *user;
  301. int err;
  302. err = stream_open(inode, file);
  303. if (err < 0)
  304. return err;
  305. mutex_lock(&register_mutex);
  306. client = seq_create_client1(-1, SNDRV_SEQ_DEFAULT_EVENTS);
  307. if (!client) {
  308. mutex_unlock(&register_mutex);
  309. return -ENOMEM; /* failure code */
  310. }
  311. mode = snd_seq_file_flags(file);
  312. if (mode & SNDRV_SEQ_LFLG_INPUT)
  313. client->accept_input = 1;
  314. if (mode & SNDRV_SEQ_LFLG_OUTPUT)
  315. client->accept_output = 1;
  316. user = &client->data.user;
  317. user->fifo = NULL;
  318. user->fifo_pool_size = 0;
  319. if (mode & SNDRV_SEQ_LFLG_INPUT) {
  320. user->fifo_pool_size = SNDRV_SEQ_DEFAULT_CLIENT_EVENTS;
  321. user->fifo = snd_seq_fifo_new(user->fifo_pool_size);
  322. if (user->fifo == NULL) {
  323. seq_free_client1(client);
  324. kfree(client);
  325. mutex_unlock(&register_mutex);
  326. return -ENOMEM;
  327. }
  328. }
  329. usage_alloc(&client_usage, 1);
  330. client->type = USER_CLIENT;
  331. mutex_unlock(&register_mutex);
  332. c = client->number;
  333. file->private_data = client;
  334. /* fill client data */
  335. user->file = file;
  336. sprintf(client->name, "Client-%d", c);
  337. client->data.user.owner = get_pid(task_pid(current));
  338. /* make others aware this new client */
  339. snd_seq_system_client_ev_client_start(c);
  340. return 0;
  341. }
  342. /* delete a user client */
  343. static int snd_seq_release(struct inode *inode, struct file *file)
  344. {
  345. struct snd_seq_client *client = file->private_data;
  346. if (client) {
  347. seq_free_client(client);
  348. if (client->data.user.fifo)
  349. snd_seq_fifo_delete(&client->data.user.fifo);
  350. #if IS_ENABLED(CONFIG_SND_SEQ_UMP)
  351. free_ump_info(client);
  352. #endif
  353. put_pid(client->data.user.owner);
  354. kfree(client);
  355. }
  356. return 0;
  357. }
  358. static bool event_is_compatible(const struct snd_seq_client *client,
  359. const struct snd_seq_event *ev)
  360. {
  361. if (snd_seq_ev_is_ump(ev) && !client->midi_version)
  362. return false;
  363. if (snd_seq_ev_is_ump(ev) && snd_seq_ev_is_variable(ev))
  364. return false;
  365. return true;
  366. }
  367. /* handle client read() */
  368. /* possible error values:
  369. * -ENXIO invalid client or file open mode
  370. * -ENOSPC FIFO overflow (the flag is cleared after this error report)
  371. * -EINVAL no enough user-space buffer to write the whole event
  372. * -EFAULT seg. fault during copy to user space
  373. */
  374. static ssize_t snd_seq_read(struct file *file, char __user *buf, size_t count,
  375. loff_t *offset)
  376. {
  377. struct snd_seq_client *client = file->private_data;
  378. struct snd_seq_fifo *fifo;
  379. size_t aligned_size;
  380. int err;
  381. long result = 0;
  382. struct snd_seq_event_cell *cell;
  383. if (!(snd_seq_file_flags(file) & SNDRV_SEQ_LFLG_INPUT))
  384. return -ENXIO;
  385. if (!access_ok(buf, count))
  386. return -EFAULT;
  387. /* check client structures are in place */
  388. if (snd_BUG_ON(!client))
  389. return -ENXIO;
  390. if (!client->accept_input)
  391. return -ENXIO;
  392. fifo = client->data.user.fifo;
  393. if (!fifo)
  394. return -ENXIO;
  395. if (atomic_read(&fifo->overflow) > 0) {
  396. /* buffer overflow is detected */
  397. snd_seq_fifo_clear(fifo);
  398. /* return error code */
  399. return -ENOSPC;
  400. }
  401. cell = NULL;
  402. err = 0;
  403. snd_seq_fifo_lock(fifo);
  404. if (IS_ENABLED(CONFIG_SND_SEQ_UMP) && client->midi_version > 0)
  405. aligned_size = sizeof(struct snd_seq_ump_event);
  406. else
  407. aligned_size = sizeof(struct snd_seq_event);
  408. /* while data available in queue */
  409. while (count >= aligned_size) {
  410. int nonblock;
  411. nonblock = (file->f_flags & O_NONBLOCK) || result > 0;
  412. err = snd_seq_fifo_cell_out(fifo, &cell, nonblock);
  413. if (err < 0)
  414. break;
  415. if (!event_is_compatible(client, &cell->event)) {
  416. snd_seq_cell_free(cell);
  417. cell = NULL;
  418. continue;
  419. }
  420. if (snd_seq_ev_is_variable(&cell->event)) {
  421. struct snd_seq_ump_event tmpev;
  422. memcpy(&tmpev, &cell->event, aligned_size);
  423. tmpev.data.ext.len &= ~SNDRV_SEQ_EXT_MASK;
  424. if (copy_to_user(buf, &tmpev, aligned_size)) {
  425. err = -EFAULT;
  426. break;
  427. }
  428. count -= aligned_size;
  429. buf += aligned_size;
  430. err = snd_seq_expand_var_event(&cell->event, count,
  431. (char __force *)buf, 0,
  432. aligned_size);
  433. if (err < 0)
  434. break;
  435. result += err;
  436. count -= err;
  437. buf += err;
  438. } else {
  439. if (copy_to_user(buf, &cell->event, aligned_size)) {
  440. err = -EFAULT;
  441. break;
  442. }
  443. count -= aligned_size;
  444. buf += aligned_size;
  445. }
  446. snd_seq_cell_free(cell);
  447. cell = NULL; /* to be sure */
  448. result += aligned_size;
  449. }
  450. if (err < 0) {
  451. if (cell)
  452. snd_seq_fifo_cell_putback(fifo, cell);
  453. if (err == -EAGAIN && result > 0)
  454. err = 0;
  455. }
  456. snd_seq_fifo_unlock(fifo);
  457. return (err < 0) ? err : result;
  458. }
  459. /*
  460. * check access permission to the port
  461. */
  462. static int check_port_perm(struct snd_seq_client_port *port, unsigned int flags)
  463. {
  464. if ((port->capability & flags) != flags)
  465. return 0;
  466. return flags;
  467. }
  468. /*
  469. * check if the destination client is available, and return the pointer
  470. */
  471. static struct snd_seq_client *get_event_dest_client(struct snd_seq_event *event)
  472. {
  473. struct snd_seq_client *dest;
  474. dest = snd_seq_client_use_ptr(event->dest.client);
  475. if (dest == NULL)
  476. return NULL;
  477. if (! dest->accept_input)
  478. goto __not_avail;
  479. if (snd_seq_ev_is_ump(event))
  480. return dest; /* ok - no filter checks */
  481. if ((dest->filter & SNDRV_SEQ_FILTER_USE_EVENT) &&
  482. ! test_bit(event->type, dest->event_filter))
  483. goto __not_avail;
  484. return dest; /* ok - accessible */
  485. __not_avail:
  486. snd_seq_client_unlock(dest);
  487. return NULL;
  488. }
  489. /*
  490. * Return the error event.
  491. *
  492. * If the receiver client is a user client, the original event is
  493. * encapsulated in SNDRV_SEQ_EVENT_BOUNCE as variable length event. If
  494. * the original event is also variable length, the external data is
  495. * copied after the event record.
  496. * If the receiver client is a kernel client, the original event is
  497. * quoted in SNDRV_SEQ_EVENT_KERNEL_ERROR, since this requires no extra
  498. * kmalloc.
  499. */
  500. static int bounce_error_event(struct snd_seq_client *client,
  501. struct snd_seq_event *event,
  502. int err, int atomic, int hop)
  503. {
  504. struct snd_seq_event bounce_ev;
  505. int result;
  506. if (client == NULL ||
  507. ! (client->filter & SNDRV_SEQ_FILTER_BOUNCE) ||
  508. ! client->accept_input)
  509. return 0; /* ignored */
  510. /* set up quoted error */
  511. memset(&bounce_ev, 0, sizeof(bounce_ev));
  512. bounce_ev.type = SNDRV_SEQ_EVENT_KERNEL_ERROR;
  513. bounce_ev.flags = SNDRV_SEQ_EVENT_LENGTH_FIXED;
  514. bounce_ev.queue = SNDRV_SEQ_QUEUE_DIRECT;
  515. bounce_ev.source.client = SNDRV_SEQ_CLIENT_SYSTEM;
  516. bounce_ev.source.port = SNDRV_SEQ_PORT_SYSTEM_ANNOUNCE;
  517. bounce_ev.dest.client = client->number;
  518. bounce_ev.dest.port = event->source.port;
  519. bounce_ev.data.quote.origin = event->dest;
  520. bounce_ev.data.quote.event = event;
  521. bounce_ev.data.quote.value = -err; /* use positive value */
  522. result = snd_seq_deliver_single_event(NULL, &bounce_ev, atomic, hop + 1);
  523. if (result < 0) {
  524. client->event_lost++;
  525. return result;
  526. }
  527. return result;
  528. }
  529. /*
  530. * rewrite the time-stamp of the event record with the curren time
  531. * of the given queue.
  532. * return non-zero if updated.
  533. */
  534. static int update_timestamp_of_queue(struct snd_seq_event *event,
  535. int queue, int real_time)
  536. {
  537. struct snd_seq_queue *q;
  538. q = queueptr(queue);
  539. if (! q)
  540. return 0;
  541. event->queue = queue;
  542. event->flags &= ~SNDRV_SEQ_TIME_STAMP_MASK;
  543. if (real_time) {
  544. event->time.time = snd_seq_timer_get_cur_time(q->timer, true);
  545. event->flags |= SNDRV_SEQ_TIME_STAMP_REAL;
  546. } else {
  547. event->time.tick = snd_seq_timer_get_cur_tick(q->timer);
  548. event->flags |= SNDRV_SEQ_TIME_STAMP_TICK;
  549. }
  550. queuefree(q);
  551. return 1;
  552. }
  553. /* deliver a single event; called from below and UMP converter */
  554. int __snd_seq_deliver_single_event(struct snd_seq_client *dest,
  555. struct snd_seq_client_port *dest_port,
  556. struct snd_seq_event *event,
  557. int atomic, int hop)
  558. {
  559. switch (dest->type) {
  560. case USER_CLIENT:
  561. if (!dest->data.user.fifo)
  562. return 0;
  563. return snd_seq_fifo_event_in(dest->data.user.fifo, event);
  564. case KERNEL_CLIENT:
  565. if (!dest_port->event_input)
  566. return 0;
  567. return dest_port->event_input(event,
  568. snd_seq_ev_is_direct(event),
  569. dest_port->private_data,
  570. atomic, hop);
  571. }
  572. return 0;
  573. }
  574. /*
  575. * deliver an event to the specified destination.
  576. * if filter is non-zero, client filter bitmap is tested.
  577. *
  578. * RETURN VALUE: 0 : if succeeded
  579. * <0 : error
  580. */
  581. static int snd_seq_deliver_single_event(struct snd_seq_client *client,
  582. struct snd_seq_event *event,
  583. int atomic, int hop)
  584. {
  585. struct snd_seq_client *dest = NULL;
  586. struct snd_seq_client_port *dest_port = NULL;
  587. int result = -ENOENT;
  588. int direct;
  589. direct = snd_seq_ev_is_direct(event);
  590. dest = get_event_dest_client(event);
  591. if (dest == NULL)
  592. goto __skip;
  593. dest_port = snd_seq_port_use_ptr(dest, event->dest.port);
  594. if (dest_port == NULL)
  595. goto __skip;
  596. /* check permission */
  597. if (! check_port_perm(dest_port, SNDRV_SEQ_PORT_CAP_WRITE)) {
  598. result = -EPERM;
  599. goto __skip;
  600. }
  601. if (dest_port->timestamping)
  602. update_timestamp_of_queue(event, dest_port->time_queue,
  603. dest_port->time_real);
  604. #if IS_ENABLED(CONFIG_SND_SEQ_UMP)
  605. if (snd_seq_ev_is_ump(event)) {
  606. if (!(dest->filter & SNDRV_SEQ_FILTER_NO_CONVERT)) {
  607. result = snd_seq_deliver_from_ump(client, dest, dest_port,
  608. event, atomic, hop);
  609. goto __skip;
  610. } else if (dest->type == USER_CLIENT &&
  611. !snd_seq_client_is_ump(dest)) {
  612. result = 0; // drop the event
  613. goto __skip;
  614. }
  615. } else if (snd_seq_client_is_ump(dest)) {
  616. if (!(dest->filter & SNDRV_SEQ_FILTER_NO_CONVERT)) {
  617. result = snd_seq_deliver_to_ump(client, dest, dest_port,
  618. event, atomic, hop);
  619. goto __skip;
  620. }
  621. }
  622. #endif /* CONFIG_SND_SEQ_UMP */
  623. result = __snd_seq_deliver_single_event(dest, dest_port, event,
  624. atomic, hop);
  625. __skip:
  626. if (dest_port)
  627. snd_seq_port_unlock(dest_port);
  628. if (dest)
  629. snd_seq_client_unlock(dest);
  630. if (result < 0 && !direct) {
  631. result = bounce_error_event(client, event, result, atomic, hop);
  632. }
  633. return result;
  634. }
  635. /*
  636. * send the event to all subscribers:
  637. */
  638. static int __deliver_to_subscribers(struct snd_seq_client *client,
  639. struct snd_seq_event *event,
  640. int port, int atomic, int hop)
  641. {
  642. struct snd_seq_client_port *src_port;
  643. struct snd_seq_subscribers *subs;
  644. int err, result = 0, num_ev = 0;
  645. union __snd_seq_event event_saved;
  646. size_t saved_size;
  647. struct snd_seq_port_subs_info *grp;
  648. if (port < 0)
  649. return 0;
  650. src_port = snd_seq_port_use_ptr(client, port);
  651. if (!src_port)
  652. return 0;
  653. /* save original event record */
  654. saved_size = snd_seq_event_packet_size(event);
  655. memcpy(&event_saved, event, saved_size);
  656. grp = &src_port->c_src;
  657. /* lock list */
  658. if (atomic)
  659. read_lock(&grp->list_lock);
  660. else
  661. down_read_nested(&grp->list_mutex, hop);
  662. list_for_each_entry(subs, &grp->list_head, src_list) {
  663. /* both ports ready? */
  664. if (atomic_read(&subs->ref_count) != 2)
  665. continue;
  666. event->dest = subs->info.dest;
  667. if (subs->info.flags & SNDRV_SEQ_PORT_SUBS_TIMESTAMP)
  668. /* convert time according to flag with subscription */
  669. update_timestamp_of_queue(event, subs->info.queue,
  670. subs->info.flags & SNDRV_SEQ_PORT_SUBS_TIME_REAL);
  671. err = snd_seq_deliver_single_event(client, event, atomic, hop);
  672. if (err < 0) {
  673. /* save first error that occurs and continue */
  674. if (!result)
  675. result = err;
  676. continue;
  677. }
  678. num_ev++;
  679. /* restore original event record */
  680. memcpy(event, &event_saved, saved_size);
  681. }
  682. if (atomic)
  683. read_unlock(&grp->list_lock);
  684. else
  685. up_read(&grp->list_mutex);
  686. snd_seq_port_unlock(src_port);
  687. memcpy(event, &event_saved, saved_size);
  688. return (result < 0) ? result : num_ev;
  689. }
  690. static int deliver_to_subscribers(struct snd_seq_client *client,
  691. struct snd_seq_event *event,
  692. int atomic, int hop)
  693. {
  694. int ret;
  695. #if IS_ENABLED(CONFIG_SND_SEQ_UMP)
  696. int ret2;
  697. #endif
  698. ret = __deliver_to_subscribers(client, event,
  699. event->source.port, atomic, hop);
  700. #if IS_ENABLED(CONFIG_SND_SEQ_UMP)
  701. if (!snd_seq_client_is_ump(client) || client->ump_endpoint_port < 0)
  702. return ret;
  703. /* If it's an event from EP port (and with a UMP group),
  704. * deliver to subscribers of the corresponding UMP group port, too.
  705. * Or, if it's from non-EP port, deliver to subscribers of EP port, too.
  706. */
  707. if (event->source.port == client->ump_endpoint_port)
  708. ret2 = __deliver_to_subscribers(client, event,
  709. snd_seq_ump_group_port(event),
  710. atomic, hop);
  711. else
  712. ret2 = __deliver_to_subscribers(client, event,
  713. client->ump_endpoint_port,
  714. atomic, hop);
  715. if (ret2 < 0)
  716. return ret2;
  717. #endif
  718. return ret;
  719. }
  720. /* deliver an event to the destination port(s).
  721. * if the event is to subscribers or broadcast, the event is dispatched
  722. * to multiple targets.
  723. *
  724. * RETURN VALUE: n > 0 : the number of delivered events.
  725. * n == 0 : the event was not passed to any client.
  726. * n < 0 : error - event was not processed.
  727. */
  728. static int snd_seq_deliver_event(struct snd_seq_client *client, struct snd_seq_event *event,
  729. int atomic, int hop)
  730. {
  731. int result;
  732. hop++;
  733. if (hop >= SNDRV_SEQ_MAX_HOPS) {
  734. pr_debug("ALSA: seq: too long delivery path (%d:%d->%d:%d)\n",
  735. event->source.client, event->source.port,
  736. event->dest.client, event->dest.port);
  737. return -EMLINK;
  738. }
  739. if (snd_seq_ev_is_variable(event) &&
  740. snd_BUG_ON(atomic && (event->data.ext.len & SNDRV_SEQ_EXT_USRPTR)))
  741. return -EINVAL;
  742. if (event->queue == SNDRV_SEQ_ADDRESS_SUBSCRIBERS ||
  743. event->dest.client == SNDRV_SEQ_ADDRESS_SUBSCRIBERS)
  744. result = deliver_to_subscribers(client, event, atomic, hop);
  745. else
  746. result = snd_seq_deliver_single_event(client, event, atomic, hop);
  747. return result;
  748. }
  749. /*
  750. * dispatch an event cell:
  751. * This function is called only from queue check routines in timer
  752. * interrupts or after enqueued.
  753. * The event cell shall be released or re-queued in this function.
  754. *
  755. * RETURN VALUE: n > 0 : the number of delivered events.
  756. * n == 0 : the event was not passed to any client.
  757. * n < 0 : error - event was not processed.
  758. */
  759. int snd_seq_dispatch_event(struct snd_seq_event_cell *cell, int atomic, int hop)
  760. {
  761. struct snd_seq_client *client;
  762. int result;
  763. if (snd_BUG_ON(!cell))
  764. return -EINVAL;
  765. client = snd_seq_client_use_ptr(cell->event.source.client);
  766. if (client == NULL) {
  767. snd_seq_cell_free(cell); /* release this cell */
  768. return -EINVAL;
  769. }
  770. if (!snd_seq_ev_is_ump(&cell->event) &&
  771. cell->event.type == SNDRV_SEQ_EVENT_NOTE) {
  772. /* NOTE event:
  773. * the event cell is re-used as a NOTE-OFF event and
  774. * enqueued again.
  775. */
  776. struct snd_seq_event tmpev, *ev;
  777. /* reserve this event to enqueue note-off later */
  778. tmpev = cell->event;
  779. tmpev.type = SNDRV_SEQ_EVENT_NOTEON;
  780. result = snd_seq_deliver_event(client, &tmpev, atomic, hop);
  781. /*
  782. * This was originally a note event. We now re-use the
  783. * cell for the note-off event.
  784. */
  785. ev = &cell->event;
  786. ev->type = SNDRV_SEQ_EVENT_NOTEOFF;
  787. ev->flags |= SNDRV_SEQ_PRIORITY_HIGH;
  788. /* add the duration time */
  789. switch (ev->flags & SNDRV_SEQ_TIME_STAMP_MASK) {
  790. case SNDRV_SEQ_TIME_STAMP_TICK:
  791. cell->event.time.tick += ev->data.note.duration;
  792. break;
  793. case SNDRV_SEQ_TIME_STAMP_REAL:
  794. /* unit for duration is ms */
  795. ev->time.time.tv_nsec += 1000000 * (ev->data.note.duration % 1000);
  796. ev->time.time.tv_sec += ev->data.note.duration / 1000 +
  797. ev->time.time.tv_nsec / 1000000000;
  798. ev->time.time.tv_nsec %= 1000000000;
  799. break;
  800. }
  801. ev->data.note.velocity = ev->data.note.off_velocity;
  802. /* Now queue this cell as the note off event */
  803. if (snd_seq_enqueue_event(cell, atomic, hop) < 0)
  804. snd_seq_cell_free(cell); /* release this cell */
  805. } else {
  806. /* Normal events:
  807. * event cell is freed after processing the event
  808. */
  809. result = snd_seq_deliver_event(client, &cell->event, atomic, hop);
  810. snd_seq_cell_free(cell);
  811. }
  812. snd_seq_client_unlock(client);
  813. return result;
  814. }
  815. /* Allocate a cell from client pool and enqueue it to queue:
  816. * if pool is empty and blocking is TRUE, sleep until a new cell is
  817. * available.
  818. */
  819. static int snd_seq_client_enqueue_event(struct snd_seq_client *client,
  820. struct snd_seq_event *event,
  821. struct file *file, int blocking,
  822. int atomic, int hop,
  823. struct mutex *mutexp)
  824. {
  825. struct snd_seq_event_cell *cell;
  826. int err;
  827. /* special queue values - force direct passing */
  828. if (event->queue == SNDRV_SEQ_ADDRESS_SUBSCRIBERS) {
  829. event->dest.client = SNDRV_SEQ_ADDRESS_SUBSCRIBERS;
  830. event->queue = SNDRV_SEQ_QUEUE_DIRECT;
  831. } else if (event->dest.client == SNDRV_SEQ_ADDRESS_SUBSCRIBERS) {
  832. /* check presence of source port */
  833. struct snd_seq_client_port *src_port = snd_seq_port_use_ptr(client, event->source.port);
  834. if (src_port == NULL)
  835. return -EINVAL;
  836. snd_seq_port_unlock(src_port);
  837. }
  838. /* direct event processing without enqueued */
  839. if (snd_seq_ev_is_direct(event)) {
  840. if (!snd_seq_ev_is_ump(event) &&
  841. event->type == SNDRV_SEQ_EVENT_NOTE)
  842. return -EINVAL; /* this event must be enqueued! */
  843. return snd_seq_deliver_event(client, event, atomic, hop);
  844. }
  845. /* Not direct, normal queuing */
  846. if (snd_seq_queue_is_used(event->queue, client->number) <= 0)
  847. return -EINVAL; /* invalid queue */
  848. if (! snd_seq_write_pool_allocated(client))
  849. return -ENXIO; /* queue is not allocated */
  850. /* allocate an event cell */
  851. err = snd_seq_event_dup(client->pool, event, &cell, !blocking || atomic,
  852. file, mutexp);
  853. if (err < 0)
  854. return err;
  855. /* we got a cell. enqueue it. */
  856. err = snd_seq_enqueue_event(cell, atomic, hop);
  857. if (err < 0) {
  858. snd_seq_cell_free(cell);
  859. return err;
  860. }
  861. return 0;
  862. }
  863. /*
  864. * check validity of event type and data length.
  865. * return non-zero if invalid.
  866. */
  867. static int check_event_type_and_length(struct snd_seq_event *ev)
  868. {
  869. switch (snd_seq_ev_length_type(ev)) {
  870. case SNDRV_SEQ_EVENT_LENGTH_FIXED:
  871. if (snd_seq_ev_is_variable_type(ev))
  872. return -EINVAL;
  873. break;
  874. case SNDRV_SEQ_EVENT_LENGTH_VARIABLE:
  875. if (! snd_seq_ev_is_variable_type(ev) ||
  876. (ev->data.ext.len & ~SNDRV_SEQ_EXT_MASK) >= SNDRV_SEQ_MAX_EVENT_LEN)
  877. return -EINVAL;
  878. break;
  879. case SNDRV_SEQ_EVENT_LENGTH_VARUSR:
  880. if (! snd_seq_ev_is_direct(ev))
  881. return -EINVAL;
  882. break;
  883. }
  884. return 0;
  885. }
  886. /* handle write() */
  887. /* possible error values:
  888. * -ENXIO invalid client or file open mode
  889. * -ENOMEM malloc failed
  890. * -EFAULT seg. fault during copy from user space
  891. * -EINVAL invalid event
  892. * -EAGAIN no space in output pool
  893. * -EINTR interrupts while sleep
  894. * -EMLINK too many hops
  895. * others depends on return value from driver callback
  896. */
  897. static ssize_t snd_seq_write(struct file *file, const char __user *buf,
  898. size_t count, loff_t *offset)
  899. {
  900. struct snd_seq_client *client = file->private_data;
  901. int written = 0, len;
  902. int err, handled;
  903. union __snd_seq_event __event;
  904. struct snd_seq_event *ev = &__event.legacy;
  905. if (!(snd_seq_file_flags(file) & SNDRV_SEQ_LFLG_OUTPUT))
  906. return -ENXIO;
  907. /* check client structures are in place */
  908. if (snd_BUG_ON(!client))
  909. return -ENXIO;
  910. if (!client->accept_output || client->pool == NULL)
  911. return -ENXIO;
  912. repeat:
  913. handled = 0;
  914. /* allocate the pool now if the pool is not allocated yet */
  915. mutex_lock(&client->ioctl_mutex);
  916. if (client->pool->size > 0 && !snd_seq_write_pool_allocated(client)) {
  917. err = snd_seq_pool_init(client->pool);
  918. if (err < 0)
  919. goto out;
  920. }
  921. /* only process whole events */
  922. err = -EINVAL;
  923. while (count >= sizeof(struct snd_seq_event)) {
  924. /* Read in the event header from the user */
  925. len = sizeof(struct snd_seq_event);
  926. if (copy_from_user(ev, buf, len)) {
  927. err = -EFAULT;
  928. break;
  929. }
  930. /* read in the rest bytes for UMP events */
  931. if (snd_seq_ev_is_ump(ev)) {
  932. if (count < sizeof(struct snd_seq_ump_event))
  933. break;
  934. if (copy_from_user((char *)ev + len, buf + len,
  935. sizeof(struct snd_seq_ump_event) - len)) {
  936. err = -EFAULT;
  937. break;
  938. }
  939. len = sizeof(struct snd_seq_ump_event);
  940. }
  941. ev->source.client = client->number; /* fill in client number */
  942. /* Check for extension data length */
  943. if (check_event_type_and_length(ev)) {
  944. err = -EINVAL;
  945. break;
  946. }
  947. if (!event_is_compatible(client, ev)) {
  948. err = -EINVAL;
  949. break;
  950. }
  951. /* check for special events */
  952. if (!snd_seq_ev_is_ump(ev)) {
  953. if (ev->type == SNDRV_SEQ_EVENT_NONE)
  954. goto __skip_event;
  955. else if (snd_seq_ev_is_reserved(ev)) {
  956. err = -EINVAL;
  957. break;
  958. }
  959. }
  960. if (snd_seq_ev_is_variable(ev)) {
  961. int extlen = ev->data.ext.len & ~SNDRV_SEQ_EXT_MASK;
  962. if ((size_t)(extlen + len) > count) {
  963. /* back out, will get an error this time or next */
  964. err = -EINVAL;
  965. break;
  966. }
  967. /* set user space pointer */
  968. ev->data.ext.len = extlen | SNDRV_SEQ_EXT_USRPTR;
  969. ev->data.ext.ptr = (char __force *)buf + len;
  970. len += extlen; /* increment data length */
  971. } else {
  972. #ifdef CONFIG_COMPAT
  973. if (client->convert32 && snd_seq_ev_is_varusr(ev))
  974. ev->data.ext.ptr =
  975. (void __force *)compat_ptr(ev->data.raw32.d[1]);
  976. #endif
  977. }
  978. /* ok, enqueue it */
  979. err = snd_seq_client_enqueue_event(client, ev, file,
  980. !(file->f_flags & O_NONBLOCK),
  981. 0, 0, &client->ioctl_mutex);
  982. if (err < 0)
  983. break;
  984. handled++;
  985. __skip_event:
  986. /* Update pointers and counts */
  987. count -= len;
  988. buf += len;
  989. written += len;
  990. /* let's have a coffee break if too many events are queued */
  991. if (++handled >= 200) {
  992. mutex_unlock(&client->ioctl_mutex);
  993. goto repeat;
  994. }
  995. }
  996. out:
  997. mutex_unlock(&client->ioctl_mutex);
  998. return written ? written : err;
  999. }
  1000. /*
  1001. * handle polling
  1002. */
  1003. static __poll_t snd_seq_poll(struct file *file, poll_table * wait)
  1004. {
  1005. struct snd_seq_client *client = file->private_data;
  1006. __poll_t mask = 0;
  1007. /* check client structures are in place */
  1008. if (snd_BUG_ON(!client))
  1009. return EPOLLERR;
  1010. if ((snd_seq_file_flags(file) & SNDRV_SEQ_LFLG_INPUT) &&
  1011. client->data.user.fifo) {
  1012. /* check if data is available in the outqueue */
  1013. if (snd_seq_fifo_poll_wait(client->data.user.fifo, file, wait))
  1014. mask |= EPOLLIN | EPOLLRDNORM;
  1015. }
  1016. if (snd_seq_file_flags(file) & SNDRV_SEQ_LFLG_OUTPUT) {
  1017. /* check if data is available in the pool */
  1018. if (snd_seq_pool_poll_wait(client->pool, file, wait))
  1019. mask |= EPOLLOUT | EPOLLWRNORM;
  1020. }
  1021. return mask;
  1022. }
  1023. /*-----------------------------------------------------*/
  1024. static int snd_seq_ioctl_pversion(struct snd_seq_client *client, void *arg)
  1025. {
  1026. int *pversion = arg;
  1027. *pversion = SNDRV_SEQ_VERSION;
  1028. return 0;
  1029. }
  1030. static int snd_seq_ioctl_user_pversion(struct snd_seq_client *client, void *arg)
  1031. {
  1032. client->user_pversion = *(unsigned int *)arg;
  1033. return 0;
  1034. }
  1035. static int snd_seq_ioctl_client_id(struct snd_seq_client *client, void *arg)
  1036. {
  1037. int *client_id = arg;
  1038. *client_id = client->number;
  1039. return 0;
  1040. }
  1041. /* SYSTEM_INFO ioctl() */
  1042. static int snd_seq_ioctl_system_info(struct snd_seq_client *client, void *arg)
  1043. {
  1044. struct snd_seq_system_info *info = arg;
  1045. memset(info, 0, sizeof(*info));
  1046. /* fill the info fields */
  1047. info->queues = SNDRV_SEQ_MAX_QUEUES;
  1048. info->clients = SNDRV_SEQ_MAX_CLIENTS;
  1049. info->ports = SNDRV_SEQ_MAX_PORTS;
  1050. info->channels = 256; /* fixed limit */
  1051. info->cur_clients = client_usage.cur;
  1052. info->cur_queues = snd_seq_queue_get_cur_queues();
  1053. return 0;
  1054. }
  1055. /* RUNNING_MODE ioctl() */
  1056. static int snd_seq_ioctl_running_mode(struct snd_seq_client *client, void *arg)
  1057. {
  1058. struct snd_seq_running_info *info = arg;
  1059. struct snd_seq_client *cptr;
  1060. int err = 0;
  1061. /* requested client number */
  1062. cptr = client_load_and_use_ptr(info->client);
  1063. if (cptr == NULL)
  1064. return -ENOENT; /* don't change !!! */
  1065. #ifdef SNDRV_BIG_ENDIAN
  1066. if (!info->big_endian) {
  1067. err = -EINVAL;
  1068. goto __err;
  1069. }
  1070. #else
  1071. if (info->big_endian) {
  1072. err = -EINVAL;
  1073. goto __err;
  1074. }
  1075. #endif
  1076. if (info->cpu_mode > sizeof(long)) {
  1077. err = -EINVAL;
  1078. goto __err;
  1079. }
  1080. cptr->convert32 = (info->cpu_mode < sizeof(long));
  1081. __err:
  1082. snd_seq_client_unlock(cptr);
  1083. return err;
  1084. }
  1085. /* CLIENT_INFO ioctl() */
  1086. static void get_client_info(struct snd_seq_client *cptr,
  1087. struct snd_seq_client_info *info)
  1088. {
  1089. info->client = cptr->number;
  1090. /* fill the info fields */
  1091. info->type = cptr->type;
  1092. strcpy(info->name, cptr->name);
  1093. info->filter = cptr->filter;
  1094. info->event_lost = cptr->event_lost;
  1095. memcpy(info->event_filter, cptr->event_filter, 32);
  1096. info->group_filter = cptr->group_filter;
  1097. info->num_ports = cptr->num_ports;
  1098. if (cptr->type == USER_CLIENT)
  1099. info->pid = pid_vnr(cptr->data.user.owner);
  1100. else
  1101. info->pid = -1;
  1102. if (cptr->type == KERNEL_CLIENT)
  1103. info->card = cptr->data.kernel.card ? cptr->data.kernel.card->number : -1;
  1104. else
  1105. info->card = -1;
  1106. info->midi_version = cptr->midi_version;
  1107. memset(info->reserved, 0, sizeof(info->reserved));
  1108. }
  1109. static int snd_seq_ioctl_get_client_info(struct snd_seq_client *client,
  1110. void *arg)
  1111. {
  1112. struct snd_seq_client_info *client_info = arg;
  1113. struct snd_seq_client *cptr;
  1114. /* requested client number */
  1115. cptr = client_load_and_use_ptr(client_info->client);
  1116. if (cptr == NULL)
  1117. return -ENOENT; /* don't change !!! */
  1118. get_client_info(cptr, client_info);
  1119. snd_seq_client_unlock(cptr);
  1120. return 0;
  1121. }
  1122. /* CLIENT_INFO ioctl() */
  1123. static int snd_seq_ioctl_set_client_info(struct snd_seq_client *client,
  1124. void *arg)
  1125. {
  1126. struct snd_seq_client_info *client_info = arg;
  1127. /* it is not allowed to set the info fields for an another client */
  1128. if (client->number != client_info->client)
  1129. return -EPERM;
  1130. /* also client type must be set now */
  1131. if (client->type != client_info->type)
  1132. return -EINVAL;
  1133. if (client->user_pversion >= SNDRV_PROTOCOL_VERSION(1, 0, 3)) {
  1134. /* check validity of midi_version field */
  1135. if (client_info->midi_version > SNDRV_SEQ_CLIENT_UMP_MIDI_2_0)
  1136. return -EINVAL;
  1137. /* check if UMP is supported in kernel */
  1138. if (!IS_ENABLED(CONFIG_SND_SEQ_UMP) &&
  1139. client_info->midi_version > 0)
  1140. return -EINVAL;
  1141. }
  1142. /* fill the info fields */
  1143. if (client_info->name[0])
  1144. strscpy(client->name, client_info->name, sizeof(client->name));
  1145. client->filter = client_info->filter;
  1146. client->event_lost = client_info->event_lost;
  1147. if (client->user_pversion >= SNDRV_PROTOCOL_VERSION(1, 0, 3))
  1148. client->midi_version = client_info->midi_version;
  1149. memcpy(client->event_filter, client_info->event_filter, 32);
  1150. client->group_filter = client_info->group_filter;
  1151. return 0;
  1152. }
  1153. /*
  1154. * CREATE PORT ioctl()
  1155. */
  1156. static int snd_seq_ioctl_create_port(struct snd_seq_client *client, void *arg)
  1157. {
  1158. struct snd_seq_port_info *info = arg;
  1159. struct snd_seq_client_port *port;
  1160. struct snd_seq_port_callback *callback;
  1161. int port_idx, err;
  1162. /* it is not allowed to create the port for an another client */
  1163. if (info->addr.client != client->number)
  1164. return -EPERM;
  1165. if (client->type == USER_CLIENT && info->kernel)
  1166. return -EINVAL;
  1167. if ((info->capability & SNDRV_SEQ_PORT_CAP_UMP_ENDPOINT) &&
  1168. client->ump_endpoint_port >= 0)
  1169. return -EBUSY;
  1170. if (info->flags & SNDRV_SEQ_PORT_FLG_GIVEN_PORT)
  1171. port_idx = info->addr.port;
  1172. else
  1173. port_idx = -1;
  1174. if (port_idx >= SNDRV_SEQ_ADDRESS_UNKNOWN)
  1175. return -EINVAL;
  1176. err = snd_seq_create_port(client, port_idx, &port);
  1177. if (err < 0)
  1178. return err;
  1179. if (client->type == KERNEL_CLIENT) {
  1180. callback = info->kernel;
  1181. if (callback) {
  1182. if (callback->owner)
  1183. port->owner = callback->owner;
  1184. port->private_data = callback->private_data;
  1185. port->private_free = callback->private_free;
  1186. port->event_input = callback->event_input;
  1187. port->c_src.open = callback->subscribe;
  1188. port->c_src.close = callback->unsubscribe;
  1189. port->c_dest.open = callback->use;
  1190. port->c_dest.close = callback->unuse;
  1191. }
  1192. }
  1193. info->addr = port->addr;
  1194. snd_seq_set_port_info(port, info);
  1195. if (info->capability & SNDRV_SEQ_PORT_CAP_UMP_ENDPOINT)
  1196. client->ump_endpoint_port = port->addr.port;
  1197. snd_seq_system_client_ev_port_start(port->addr.client, port->addr.port);
  1198. snd_seq_port_unlock(port);
  1199. return 0;
  1200. }
  1201. /*
  1202. * DELETE PORT ioctl()
  1203. */
  1204. static int snd_seq_ioctl_delete_port(struct snd_seq_client *client, void *arg)
  1205. {
  1206. struct snd_seq_port_info *info = arg;
  1207. int err;
  1208. /* it is not allowed to remove the port for an another client */
  1209. if (info->addr.client != client->number)
  1210. return -EPERM;
  1211. err = snd_seq_delete_port(client, info->addr.port);
  1212. if (err >= 0) {
  1213. if (client->ump_endpoint_port == info->addr.port)
  1214. client->ump_endpoint_port = -1;
  1215. snd_seq_system_client_ev_port_exit(client->number, info->addr.port);
  1216. }
  1217. return err;
  1218. }
  1219. /*
  1220. * GET_PORT_INFO ioctl() (on any client)
  1221. */
  1222. static int snd_seq_ioctl_get_port_info(struct snd_seq_client *client, void *arg)
  1223. {
  1224. struct snd_seq_port_info *info = arg;
  1225. struct snd_seq_client *cptr;
  1226. struct snd_seq_client_port *port;
  1227. cptr = client_load_and_use_ptr(info->addr.client);
  1228. if (cptr == NULL)
  1229. return -ENXIO;
  1230. port = snd_seq_port_use_ptr(cptr, info->addr.port);
  1231. if (port == NULL) {
  1232. snd_seq_client_unlock(cptr);
  1233. return -ENOENT; /* don't change */
  1234. }
  1235. /* get port info */
  1236. snd_seq_get_port_info(port, info);
  1237. snd_seq_port_unlock(port);
  1238. snd_seq_client_unlock(cptr);
  1239. return 0;
  1240. }
  1241. /*
  1242. * SET_PORT_INFO ioctl() (only ports on this/own client)
  1243. */
  1244. static int snd_seq_ioctl_set_port_info(struct snd_seq_client *client, void *arg)
  1245. {
  1246. struct snd_seq_port_info *info = arg;
  1247. struct snd_seq_client_port *port;
  1248. if (info->addr.client != client->number) /* only set our own ports ! */
  1249. return -EPERM;
  1250. port = snd_seq_port_use_ptr(client, info->addr.port);
  1251. if (port) {
  1252. snd_seq_set_port_info(port, info);
  1253. snd_seq_port_unlock(port);
  1254. }
  1255. return 0;
  1256. }
  1257. /*
  1258. * port subscription (connection)
  1259. */
  1260. #define PERM_RD (SNDRV_SEQ_PORT_CAP_READ|SNDRV_SEQ_PORT_CAP_SUBS_READ)
  1261. #define PERM_WR (SNDRV_SEQ_PORT_CAP_WRITE|SNDRV_SEQ_PORT_CAP_SUBS_WRITE)
  1262. static int check_subscription_permission(struct snd_seq_client *client,
  1263. struct snd_seq_client_port *sport,
  1264. struct snd_seq_client_port *dport,
  1265. struct snd_seq_port_subscribe *subs)
  1266. {
  1267. if (client->number != subs->sender.client &&
  1268. client->number != subs->dest.client) {
  1269. /* connection by third client - check export permission */
  1270. if (check_port_perm(sport, SNDRV_SEQ_PORT_CAP_NO_EXPORT))
  1271. return -EPERM;
  1272. if (check_port_perm(dport, SNDRV_SEQ_PORT_CAP_NO_EXPORT))
  1273. return -EPERM;
  1274. }
  1275. /* check read permission */
  1276. /* if sender or receiver is the subscribing client itself,
  1277. * no permission check is necessary
  1278. */
  1279. if (client->number != subs->sender.client) {
  1280. if (! check_port_perm(sport, PERM_RD))
  1281. return -EPERM;
  1282. }
  1283. /* check write permission */
  1284. if (client->number != subs->dest.client) {
  1285. if (! check_port_perm(dport, PERM_WR))
  1286. return -EPERM;
  1287. }
  1288. return 0;
  1289. }
  1290. /*
  1291. * send an subscription notify event to user client:
  1292. * client must be user client.
  1293. */
  1294. int snd_seq_client_notify_subscription(int client, int port,
  1295. struct snd_seq_port_subscribe *info,
  1296. int evtype)
  1297. {
  1298. struct snd_seq_event event;
  1299. memset(&event, 0, sizeof(event));
  1300. event.type = evtype;
  1301. event.data.connect.dest = info->dest;
  1302. event.data.connect.sender = info->sender;
  1303. return snd_seq_system_notify(client, port, &event); /* non-atomic */
  1304. }
  1305. /*
  1306. * add to port's subscription list IOCTL interface
  1307. */
  1308. static int snd_seq_ioctl_subscribe_port(struct snd_seq_client *client,
  1309. void *arg)
  1310. {
  1311. struct snd_seq_port_subscribe *subs = arg;
  1312. int result = -EINVAL;
  1313. struct snd_seq_client *receiver = NULL, *sender = NULL;
  1314. struct snd_seq_client_port *sport = NULL, *dport = NULL;
  1315. receiver = client_load_and_use_ptr(subs->dest.client);
  1316. if (!receiver)
  1317. goto __end;
  1318. sender = client_load_and_use_ptr(subs->sender.client);
  1319. if (!sender)
  1320. goto __end;
  1321. sport = snd_seq_port_use_ptr(sender, subs->sender.port);
  1322. if (!sport)
  1323. goto __end;
  1324. dport = snd_seq_port_use_ptr(receiver, subs->dest.port);
  1325. if (!dport)
  1326. goto __end;
  1327. result = check_subscription_permission(client, sport, dport, subs);
  1328. if (result < 0)
  1329. goto __end;
  1330. /* connect them */
  1331. result = snd_seq_port_connect(client, sender, sport, receiver, dport, subs);
  1332. if (! result) /* broadcast announce */
  1333. snd_seq_client_notify_subscription(SNDRV_SEQ_ADDRESS_SUBSCRIBERS, 0,
  1334. subs, SNDRV_SEQ_EVENT_PORT_SUBSCRIBED);
  1335. __end:
  1336. if (sport)
  1337. snd_seq_port_unlock(sport);
  1338. if (dport)
  1339. snd_seq_port_unlock(dport);
  1340. if (sender)
  1341. snd_seq_client_unlock(sender);
  1342. if (receiver)
  1343. snd_seq_client_unlock(receiver);
  1344. return result;
  1345. }
  1346. /*
  1347. * remove from port's subscription list
  1348. */
  1349. static int snd_seq_ioctl_unsubscribe_port(struct snd_seq_client *client,
  1350. void *arg)
  1351. {
  1352. struct snd_seq_port_subscribe *subs = arg;
  1353. int result = -ENXIO;
  1354. struct snd_seq_client *receiver = NULL, *sender = NULL;
  1355. struct snd_seq_client_port *sport = NULL, *dport = NULL;
  1356. receiver = snd_seq_client_use_ptr(subs->dest.client);
  1357. if (!receiver)
  1358. goto __end;
  1359. sender = snd_seq_client_use_ptr(subs->sender.client);
  1360. if (!sender)
  1361. goto __end;
  1362. sport = snd_seq_port_use_ptr(sender, subs->sender.port);
  1363. if (!sport)
  1364. goto __end;
  1365. dport = snd_seq_port_use_ptr(receiver, subs->dest.port);
  1366. if (!dport)
  1367. goto __end;
  1368. result = check_subscription_permission(client, sport, dport, subs);
  1369. if (result < 0)
  1370. goto __end;
  1371. result = snd_seq_port_disconnect(client, sender, sport, receiver, dport, subs);
  1372. if (! result) /* broadcast announce */
  1373. snd_seq_client_notify_subscription(SNDRV_SEQ_ADDRESS_SUBSCRIBERS, 0,
  1374. subs, SNDRV_SEQ_EVENT_PORT_UNSUBSCRIBED);
  1375. __end:
  1376. if (sport)
  1377. snd_seq_port_unlock(sport);
  1378. if (dport)
  1379. snd_seq_port_unlock(dport);
  1380. if (sender)
  1381. snd_seq_client_unlock(sender);
  1382. if (receiver)
  1383. snd_seq_client_unlock(receiver);
  1384. return result;
  1385. }
  1386. /* CREATE_QUEUE ioctl() */
  1387. static int snd_seq_ioctl_create_queue(struct snd_seq_client *client, void *arg)
  1388. {
  1389. struct snd_seq_queue_info *info = arg;
  1390. struct snd_seq_queue *q;
  1391. q = snd_seq_queue_alloc(client->number, info->locked, info->flags);
  1392. if (IS_ERR(q))
  1393. return PTR_ERR(q);
  1394. info->queue = q->queue;
  1395. info->locked = q->locked;
  1396. info->owner = q->owner;
  1397. /* set queue name */
  1398. if (!info->name[0])
  1399. snprintf(info->name, sizeof(info->name), "Queue-%d", q->queue);
  1400. strscpy(q->name, info->name, sizeof(q->name));
  1401. snd_use_lock_free(&q->use_lock);
  1402. return 0;
  1403. }
  1404. /* DELETE_QUEUE ioctl() */
  1405. static int snd_seq_ioctl_delete_queue(struct snd_seq_client *client, void *arg)
  1406. {
  1407. struct snd_seq_queue_info *info = arg;
  1408. return snd_seq_queue_delete(client->number, info->queue);
  1409. }
  1410. /* GET_QUEUE_INFO ioctl() */
  1411. static int snd_seq_ioctl_get_queue_info(struct snd_seq_client *client,
  1412. void *arg)
  1413. {
  1414. struct snd_seq_queue_info *info = arg;
  1415. struct snd_seq_queue *q;
  1416. q = queueptr(info->queue);
  1417. if (q == NULL)
  1418. return -EINVAL;
  1419. memset(info, 0, sizeof(*info));
  1420. info->queue = q->queue;
  1421. info->owner = q->owner;
  1422. info->locked = q->locked;
  1423. strscpy(info->name, q->name, sizeof(info->name));
  1424. queuefree(q);
  1425. return 0;
  1426. }
  1427. /* SET_QUEUE_INFO ioctl() */
  1428. static int snd_seq_ioctl_set_queue_info(struct snd_seq_client *client,
  1429. void *arg)
  1430. {
  1431. struct snd_seq_queue_info *info = arg;
  1432. struct snd_seq_queue *q;
  1433. if (info->owner != client->number)
  1434. return -EINVAL;
  1435. /* change owner/locked permission */
  1436. if (snd_seq_queue_check_access(info->queue, client->number)) {
  1437. if (snd_seq_queue_set_owner(info->queue, client->number, info->locked) < 0)
  1438. return -EPERM;
  1439. if (info->locked)
  1440. snd_seq_queue_use(info->queue, client->number, 1);
  1441. } else {
  1442. return -EPERM;
  1443. }
  1444. q = queueptr(info->queue);
  1445. if (! q)
  1446. return -EINVAL;
  1447. if (q->owner != client->number) {
  1448. queuefree(q);
  1449. return -EPERM;
  1450. }
  1451. strscpy(q->name, info->name, sizeof(q->name));
  1452. queuefree(q);
  1453. return 0;
  1454. }
  1455. /* GET_NAMED_QUEUE ioctl() */
  1456. static int snd_seq_ioctl_get_named_queue(struct snd_seq_client *client,
  1457. void *arg)
  1458. {
  1459. struct snd_seq_queue_info *info = arg;
  1460. struct snd_seq_queue *q;
  1461. q = snd_seq_queue_find_name(info->name);
  1462. if (q == NULL)
  1463. return -EINVAL;
  1464. info->queue = q->queue;
  1465. info->owner = q->owner;
  1466. info->locked = q->locked;
  1467. queuefree(q);
  1468. return 0;
  1469. }
  1470. /* GET_QUEUE_STATUS ioctl() */
  1471. static int snd_seq_ioctl_get_queue_status(struct snd_seq_client *client,
  1472. void *arg)
  1473. {
  1474. struct snd_seq_queue_status *status = arg;
  1475. struct snd_seq_queue *queue;
  1476. struct snd_seq_timer *tmr;
  1477. queue = queueptr(status->queue);
  1478. if (queue == NULL)
  1479. return -EINVAL;
  1480. memset(status, 0, sizeof(*status));
  1481. status->queue = queue->queue;
  1482. tmr = queue->timer;
  1483. status->events = queue->tickq->cells + queue->timeq->cells;
  1484. status->time = snd_seq_timer_get_cur_time(tmr, true);
  1485. status->tick = snd_seq_timer_get_cur_tick(tmr);
  1486. status->running = tmr->running;
  1487. status->flags = queue->flags;
  1488. queuefree(queue);
  1489. return 0;
  1490. }
  1491. /* GET_QUEUE_TEMPO ioctl() */
  1492. static int snd_seq_ioctl_get_queue_tempo(struct snd_seq_client *client,
  1493. void *arg)
  1494. {
  1495. struct snd_seq_queue_tempo *tempo = arg;
  1496. struct snd_seq_queue *queue;
  1497. struct snd_seq_timer *tmr;
  1498. queue = queueptr(tempo->queue);
  1499. if (queue == NULL)
  1500. return -EINVAL;
  1501. memset(tempo, 0, sizeof(*tempo));
  1502. tempo->queue = queue->queue;
  1503. tmr = queue->timer;
  1504. tempo->tempo = tmr->tempo;
  1505. tempo->ppq = tmr->ppq;
  1506. tempo->skew_value = tmr->skew;
  1507. tempo->skew_base = tmr->skew_base;
  1508. if (client->user_pversion >= SNDRV_PROTOCOL_VERSION(1, 0, 4))
  1509. tempo->tempo_base = tmr->tempo_base;
  1510. queuefree(queue);
  1511. return 0;
  1512. }
  1513. /* SET_QUEUE_TEMPO ioctl() */
  1514. int snd_seq_set_queue_tempo(int client, struct snd_seq_queue_tempo *tempo)
  1515. {
  1516. if (!snd_seq_queue_check_access(tempo->queue, client))
  1517. return -EPERM;
  1518. return snd_seq_queue_timer_set_tempo(tempo->queue, client, tempo);
  1519. }
  1520. EXPORT_SYMBOL(snd_seq_set_queue_tempo);
  1521. static int snd_seq_ioctl_set_queue_tempo(struct snd_seq_client *client,
  1522. void *arg)
  1523. {
  1524. struct snd_seq_queue_tempo *tempo = arg;
  1525. int result;
  1526. if (client->user_pversion < SNDRV_PROTOCOL_VERSION(1, 0, 4))
  1527. tempo->tempo_base = 0;
  1528. result = snd_seq_set_queue_tempo(client->number, tempo);
  1529. return result < 0 ? result : 0;
  1530. }
  1531. /* GET_QUEUE_TIMER ioctl() */
  1532. static int snd_seq_ioctl_get_queue_timer(struct snd_seq_client *client,
  1533. void *arg)
  1534. {
  1535. struct snd_seq_queue_timer *timer = arg;
  1536. struct snd_seq_queue *queue;
  1537. struct snd_seq_timer *tmr;
  1538. queue = queueptr(timer->queue);
  1539. if (queue == NULL)
  1540. return -EINVAL;
  1541. mutex_lock(&queue->timer_mutex);
  1542. tmr = queue->timer;
  1543. memset(timer, 0, sizeof(*timer));
  1544. timer->queue = queue->queue;
  1545. timer->type = tmr->type;
  1546. if (tmr->type == SNDRV_SEQ_TIMER_ALSA) {
  1547. timer->u.alsa.id = tmr->alsa_id;
  1548. timer->u.alsa.resolution = tmr->preferred_resolution;
  1549. }
  1550. mutex_unlock(&queue->timer_mutex);
  1551. queuefree(queue);
  1552. return 0;
  1553. }
  1554. /* SET_QUEUE_TIMER ioctl() */
  1555. static int snd_seq_ioctl_set_queue_timer(struct snd_seq_client *client,
  1556. void *arg)
  1557. {
  1558. struct snd_seq_queue_timer *timer = arg;
  1559. int result = 0;
  1560. if (timer->type != SNDRV_SEQ_TIMER_ALSA)
  1561. return -EINVAL;
  1562. if (snd_seq_queue_check_access(timer->queue, client->number)) {
  1563. struct snd_seq_queue *q;
  1564. struct snd_seq_timer *tmr;
  1565. q = queueptr(timer->queue);
  1566. if (q == NULL)
  1567. return -ENXIO;
  1568. mutex_lock(&q->timer_mutex);
  1569. tmr = q->timer;
  1570. snd_seq_queue_timer_close(timer->queue);
  1571. tmr->type = timer->type;
  1572. if (tmr->type == SNDRV_SEQ_TIMER_ALSA) {
  1573. tmr->alsa_id = timer->u.alsa.id;
  1574. tmr->preferred_resolution = timer->u.alsa.resolution;
  1575. }
  1576. result = snd_seq_queue_timer_open(timer->queue);
  1577. mutex_unlock(&q->timer_mutex);
  1578. queuefree(q);
  1579. } else {
  1580. return -EPERM;
  1581. }
  1582. return result;
  1583. }
  1584. /* GET_QUEUE_CLIENT ioctl() */
  1585. static int snd_seq_ioctl_get_queue_client(struct snd_seq_client *client,
  1586. void *arg)
  1587. {
  1588. struct snd_seq_queue_client *info = arg;
  1589. int used;
  1590. used = snd_seq_queue_is_used(info->queue, client->number);
  1591. if (used < 0)
  1592. return -EINVAL;
  1593. info->used = used;
  1594. info->client = client->number;
  1595. return 0;
  1596. }
  1597. /* SET_QUEUE_CLIENT ioctl() */
  1598. static int snd_seq_ioctl_set_queue_client(struct snd_seq_client *client,
  1599. void *arg)
  1600. {
  1601. struct snd_seq_queue_client *info = arg;
  1602. int err;
  1603. if (info->used >= 0) {
  1604. err = snd_seq_queue_use(info->queue, client->number, info->used);
  1605. if (err < 0)
  1606. return err;
  1607. }
  1608. return snd_seq_ioctl_get_queue_client(client, arg);
  1609. }
  1610. /* GET_CLIENT_POOL ioctl() */
  1611. static int snd_seq_ioctl_get_client_pool(struct snd_seq_client *client,
  1612. void *arg)
  1613. {
  1614. struct snd_seq_client_pool *info = arg;
  1615. struct snd_seq_client *cptr;
  1616. cptr = client_load_and_use_ptr(info->client);
  1617. if (cptr == NULL)
  1618. return -ENOENT;
  1619. memset(info, 0, sizeof(*info));
  1620. info->client = cptr->number;
  1621. info->output_pool = cptr->pool->size;
  1622. info->output_room = cptr->pool->room;
  1623. info->output_free = info->output_pool;
  1624. info->output_free = snd_seq_unused_cells(cptr->pool);
  1625. if (cptr->type == USER_CLIENT) {
  1626. info->input_pool = cptr->data.user.fifo_pool_size;
  1627. info->input_free = info->input_pool;
  1628. info->input_free = snd_seq_fifo_unused_cells(cptr->data.user.fifo);
  1629. } else {
  1630. info->input_pool = 0;
  1631. info->input_free = 0;
  1632. }
  1633. snd_seq_client_unlock(cptr);
  1634. return 0;
  1635. }
  1636. /* SET_CLIENT_POOL ioctl() */
  1637. static int snd_seq_ioctl_set_client_pool(struct snd_seq_client *client,
  1638. void *arg)
  1639. {
  1640. struct snd_seq_client_pool *info = arg;
  1641. int rc;
  1642. if (client->number != info->client)
  1643. return -EINVAL; /* can't change other clients */
  1644. if (info->output_pool >= 1 && info->output_pool <= SNDRV_SEQ_MAX_EVENTS &&
  1645. (! snd_seq_write_pool_allocated(client) ||
  1646. info->output_pool != client->pool->size)) {
  1647. if (snd_seq_write_pool_allocated(client)) {
  1648. /* is the pool in use? */
  1649. if (atomic_read(&client->pool->counter))
  1650. return -EBUSY;
  1651. /* remove all existing cells */
  1652. snd_seq_pool_mark_closing(client->pool);
  1653. snd_seq_pool_done(client->pool);
  1654. }
  1655. client->pool->size = info->output_pool;
  1656. rc = snd_seq_pool_init(client->pool);
  1657. if (rc < 0)
  1658. return rc;
  1659. }
  1660. if (client->type == USER_CLIENT && client->data.user.fifo != NULL &&
  1661. info->input_pool >= 1 &&
  1662. info->input_pool <= SNDRV_SEQ_MAX_CLIENT_EVENTS &&
  1663. info->input_pool != client->data.user.fifo_pool_size) {
  1664. /* change pool size */
  1665. rc = snd_seq_fifo_resize(client->data.user.fifo, info->input_pool);
  1666. if (rc < 0)
  1667. return rc;
  1668. client->data.user.fifo_pool_size = info->input_pool;
  1669. }
  1670. if (info->output_room >= 1 &&
  1671. info->output_room <= client->pool->size) {
  1672. client->pool->room = info->output_room;
  1673. }
  1674. return snd_seq_ioctl_get_client_pool(client, arg);
  1675. }
  1676. /* REMOVE_EVENTS ioctl() */
  1677. static int snd_seq_ioctl_remove_events(struct snd_seq_client *client,
  1678. void *arg)
  1679. {
  1680. struct snd_seq_remove_events *info = arg;
  1681. /*
  1682. * Input mostly not implemented XXX.
  1683. */
  1684. if (info->remove_mode & SNDRV_SEQ_REMOVE_INPUT) {
  1685. /*
  1686. * No restrictions so for a user client we can clear
  1687. * the whole fifo
  1688. */
  1689. if (client->type == USER_CLIENT && client->data.user.fifo)
  1690. snd_seq_fifo_clear(client->data.user.fifo);
  1691. }
  1692. if (info->remove_mode & SNDRV_SEQ_REMOVE_OUTPUT)
  1693. snd_seq_queue_remove_cells(client->number, info);
  1694. return 0;
  1695. }
  1696. /*
  1697. * get subscription info
  1698. */
  1699. static int snd_seq_ioctl_get_subscription(struct snd_seq_client *client,
  1700. void *arg)
  1701. {
  1702. struct snd_seq_port_subscribe *subs = arg;
  1703. int result;
  1704. struct snd_seq_client *sender = NULL;
  1705. struct snd_seq_client_port *sport = NULL;
  1706. result = -EINVAL;
  1707. sender = client_load_and_use_ptr(subs->sender.client);
  1708. if (!sender)
  1709. goto __end;
  1710. sport = snd_seq_port_use_ptr(sender, subs->sender.port);
  1711. if (!sport)
  1712. goto __end;
  1713. result = snd_seq_port_get_subscription(&sport->c_src, &subs->dest,
  1714. subs);
  1715. __end:
  1716. if (sport)
  1717. snd_seq_port_unlock(sport);
  1718. if (sender)
  1719. snd_seq_client_unlock(sender);
  1720. return result;
  1721. }
  1722. /*
  1723. * get subscription info - check only its presence
  1724. */
  1725. static int snd_seq_ioctl_query_subs(struct snd_seq_client *client, void *arg)
  1726. {
  1727. struct snd_seq_query_subs *subs = arg;
  1728. int result = -ENXIO;
  1729. struct snd_seq_client *cptr = NULL;
  1730. struct snd_seq_client_port *port = NULL;
  1731. struct snd_seq_port_subs_info *group;
  1732. struct list_head *p;
  1733. int i;
  1734. cptr = client_load_and_use_ptr(subs->root.client);
  1735. if (!cptr)
  1736. goto __end;
  1737. port = snd_seq_port_use_ptr(cptr, subs->root.port);
  1738. if (!port)
  1739. goto __end;
  1740. switch (subs->type) {
  1741. case SNDRV_SEQ_QUERY_SUBS_READ:
  1742. group = &port->c_src;
  1743. break;
  1744. case SNDRV_SEQ_QUERY_SUBS_WRITE:
  1745. group = &port->c_dest;
  1746. break;
  1747. default:
  1748. goto __end;
  1749. }
  1750. down_read(&group->list_mutex);
  1751. /* search for the subscriber */
  1752. subs->num_subs = group->count;
  1753. i = 0;
  1754. result = -ENOENT;
  1755. list_for_each(p, &group->list_head) {
  1756. if (i++ == subs->index) {
  1757. /* found! */
  1758. struct snd_seq_subscribers *s;
  1759. if (subs->type == SNDRV_SEQ_QUERY_SUBS_READ) {
  1760. s = list_entry(p, struct snd_seq_subscribers, src_list);
  1761. subs->addr = s->info.dest;
  1762. } else {
  1763. s = list_entry(p, struct snd_seq_subscribers, dest_list);
  1764. subs->addr = s->info.sender;
  1765. }
  1766. subs->flags = s->info.flags;
  1767. subs->queue = s->info.queue;
  1768. result = 0;
  1769. break;
  1770. }
  1771. }
  1772. up_read(&group->list_mutex);
  1773. __end:
  1774. if (port)
  1775. snd_seq_port_unlock(port);
  1776. if (cptr)
  1777. snd_seq_client_unlock(cptr);
  1778. return result;
  1779. }
  1780. /*
  1781. * query next client
  1782. */
  1783. static int snd_seq_ioctl_query_next_client(struct snd_seq_client *client,
  1784. void *arg)
  1785. {
  1786. struct snd_seq_client_info *info = arg;
  1787. struct snd_seq_client *cptr = NULL;
  1788. /* search for next client */
  1789. if (info->client < INT_MAX)
  1790. info->client++;
  1791. if (info->client < 0)
  1792. info->client = 0;
  1793. for (; info->client < SNDRV_SEQ_MAX_CLIENTS; info->client++) {
  1794. cptr = client_load_and_use_ptr(info->client);
  1795. if (cptr)
  1796. break; /* found */
  1797. }
  1798. if (cptr == NULL)
  1799. return -ENOENT;
  1800. get_client_info(cptr, info);
  1801. snd_seq_client_unlock(cptr);
  1802. return 0;
  1803. }
  1804. /*
  1805. * query next port
  1806. */
  1807. static int snd_seq_ioctl_query_next_port(struct snd_seq_client *client,
  1808. void *arg)
  1809. {
  1810. struct snd_seq_port_info *info = arg;
  1811. struct snd_seq_client *cptr;
  1812. struct snd_seq_client_port *port = NULL;
  1813. cptr = client_load_and_use_ptr(info->addr.client);
  1814. if (cptr == NULL)
  1815. return -ENXIO;
  1816. /* search for next port */
  1817. info->addr.port++;
  1818. port = snd_seq_port_query_nearest(cptr, info);
  1819. if (port == NULL) {
  1820. snd_seq_client_unlock(cptr);
  1821. return -ENOENT;
  1822. }
  1823. /* get port info */
  1824. info->addr = port->addr;
  1825. snd_seq_get_port_info(port, info);
  1826. snd_seq_port_unlock(port);
  1827. snd_seq_client_unlock(cptr);
  1828. return 0;
  1829. }
  1830. #if IS_ENABLED(CONFIG_SND_SEQ_UMP)
  1831. #define NUM_UMP_INFOS (SNDRV_UMP_MAX_BLOCKS + 1)
  1832. static void free_ump_info(struct snd_seq_client *client)
  1833. {
  1834. int i;
  1835. if (!client->ump_info)
  1836. return;
  1837. for (i = 0; i < NUM_UMP_INFOS; i++)
  1838. kfree(client->ump_info[i]);
  1839. kfree(client->ump_info);
  1840. client->ump_info = NULL;
  1841. }
  1842. static void terminate_ump_info_strings(void *p, int type)
  1843. {
  1844. if (type == SNDRV_SEQ_CLIENT_UMP_INFO_ENDPOINT) {
  1845. struct snd_ump_endpoint_info *ep = p;
  1846. ep->name[sizeof(ep->name) - 1] = 0;
  1847. } else {
  1848. struct snd_ump_block_info *bp = p;
  1849. bp->name[sizeof(bp->name) - 1] = 0;
  1850. }
  1851. }
  1852. #ifdef CONFIG_SND_PROC_FS
  1853. static void dump_ump_info(struct snd_info_buffer *buffer,
  1854. struct snd_seq_client *client)
  1855. {
  1856. struct snd_ump_endpoint_info *ep;
  1857. struct snd_ump_block_info *bp;
  1858. int i;
  1859. if (!client->ump_info)
  1860. return;
  1861. ep = client->ump_info[SNDRV_SEQ_CLIENT_UMP_INFO_ENDPOINT];
  1862. if (ep && *ep->name)
  1863. snd_iprintf(buffer, " UMP Endpoint: \"%s\"\n", ep->name);
  1864. for (i = 0; i < SNDRV_UMP_MAX_BLOCKS; i++) {
  1865. bp = client->ump_info[i + 1];
  1866. if (bp && *bp->name) {
  1867. snd_iprintf(buffer, " UMP Block %d: \"%s\" [%s]\n",
  1868. i, bp->name,
  1869. bp->active ? "Active" : "Inactive");
  1870. snd_iprintf(buffer, " Groups: %d-%d\n",
  1871. bp->first_group + 1,
  1872. bp->first_group + bp->num_groups);
  1873. }
  1874. }
  1875. }
  1876. #endif
  1877. /* UMP-specific ioctls -- called directly without data copy */
  1878. static int snd_seq_ioctl_client_ump_info(struct snd_seq_client *caller,
  1879. unsigned int cmd,
  1880. unsigned long arg)
  1881. {
  1882. struct snd_seq_client_ump_info __user *argp =
  1883. (struct snd_seq_client_ump_info __user *)arg;
  1884. struct snd_seq_client *cptr;
  1885. int client, type, err = 0;
  1886. size_t size;
  1887. void *p;
  1888. if (get_user(client, &argp->client) || get_user(type, &argp->type))
  1889. return -EFAULT;
  1890. if (cmd == SNDRV_SEQ_IOCTL_SET_CLIENT_UMP_INFO &&
  1891. caller->number != client)
  1892. return -EPERM;
  1893. if (type < 0 || type >= NUM_UMP_INFOS)
  1894. return -EINVAL;
  1895. if (type == SNDRV_SEQ_CLIENT_UMP_INFO_ENDPOINT)
  1896. size = sizeof(struct snd_ump_endpoint_info);
  1897. else
  1898. size = sizeof(struct snd_ump_block_info);
  1899. cptr = client_load_and_use_ptr(client);
  1900. if (!cptr)
  1901. return -ENOENT;
  1902. mutex_lock(&cptr->ioctl_mutex);
  1903. if (!cptr->midi_version) {
  1904. err = -EBADFD;
  1905. goto error;
  1906. }
  1907. if (cmd == SNDRV_SEQ_IOCTL_GET_CLIENT_UMP_INFO) {
  1908. if (!cptr->ump_info)
  1909. p = NULL;
  1910. else
  1911. p = cptr->ump_info[type];
  1912. if (!p) {
  1913. err = -ENODEV;
  1914. goto error;
  1915. }
  1916. if (copy_to_user(argp->info, p, size)) {
  1917. err = -EFAULT;
  1918. goto error;
  1919. }
  1920. } else {
  1921. if (cptr->type != USER_CLIENT) {
  1922. err = -EBADFD;
  1923. goto error;
  1924. }
  1925. if (!cptr->ump_info) {
  1926. cptr->ump_info = kcalloc(NUM_UMP_INFOS,
  1927. sizeof(void *), GFP_KERNEL);
  1928. if (!cptr->ump_info) {
  1929. err = -ENOMEM;
  1930. goto error;
  1931. }
  1932. }
  1933. p = memdup_user(argp->info, size);
  1934. if (IS_ERR(p)) {
  1935. err = PTR_ERR(p);
  1936. goto error;
  1937. }
  1938. kfree(cptr->ump_info[type]);
  1939. terminate_ump_info_strings(p, type);
  1940. cptr->ump_info[type] = p;
  1941. }
  1942. error:
  1943. mutex_unlock(&cptr->ioctl_mutex);
  1944. snd_seq_client_unlock(cptr);
  1945. return err;
  1946. }
  1947. #endif
  1948. /* -------------------------------------------------------- */
  1949. static const struct ioctl_handler {
  1950. unsigned int cmd;
  1951. int (*func)(struct snd_seq_client *client, void *arg);
  1952. } ioctl_handlers[] = {
  1953. { SNDRV_SEQ_IOCTL_PVERSION, snd_seq_ioctl_pversion },
  1954. { SNDRV_SEQ_IOCTL_USER_PVERSION, snd_seq_ioctl_user_pversion },
  1955. { SNDRV_SEQ_IOCTL_CLIENT_ID, snd_seq_ioctl_client_id },
  1956. { SNDRV_SEQ_IOCTL_SYSTEM_INFO, snd_seq_ioctl_system_info },
  1957. { SNDRV_SEQ_IOCTL_RUNNING_MODE, snd_seq_ioctl_running_mode },
  1958. { SNDRV_SEQ_IOCTL_GET_CLIENT_INFO, snd_seq_ioctl_get_client_info },
  1959. { SNDRV_SEQ_IOCTL_SET_CLIENT_INFO, snd_seq_ioctl_set_client_info },
  1960. { SNDRV_SEQ_IOCTL_CREATE_PORT, snd_seq_ioctl_create_port },
  1961. { SNDRV_SEQ_IOCTL_DELETE_PORT, snd_seq_ioctl_delete_port },
  1962. { SNDRV_SEQ_IOCTL_GET_PORT_INFO, snd_seq_ioctl_get_port_info },
  1963. { SNDRV_SEQ_IOCTL_SET_PORT_INFO, snd_seq_ioctl_set_port_info },
  1964. { SNDRV_SEQ_IOCTL_SUBSCRIBE_PORT, snd_seq_ioctl_subscribe_port },
  1965. { SNDRV_SEQ_IOCTL_UNSUBSCRIBE_PORT, snd_seq_ioctl_unsubscribe_port },
  1966. { SNDRV_SEQ_IOCTL_CREATE_QUEUE, snd_seq_ioctl_create_queue },
  1967. { SNDRV_SEQ_IOCTL_DELETE_QUEUE, snd_seq_ioctl_delete_queue },
  1968. { SNDRV_SEQ_IOCTL_GET_QUEUE_INFO, snd_seq_ioctl_get_queue_info },
  1969. { SNDRV_SEQ_IOCTL_SET_QUEUE_INFO, snd_seq_ioctl_set_queue_info },
  1970. { SNDRV_SEQ_IOCTL_GET_NAMED_QUEUE, snd_seq_ioctl_get_named_queue },
  1971. { SNDRV_SEQ_IOCTL_GET_QUEUE_STATUS, snd_seq_ioctl_get_queue_status },
  1972. { SNDRV_SEQ_IOCTL_GET_QUEUE_TEMPO, snd_seq_ioctl_get_queue_tempo },
  1973. { SNDRV_SEQ_IOCTL_SET_QUEUE_TEMPO, snd_seq_ioctl_set_queue_tempo },
  1974. { SNDRV_SEQ_IOCTL_GET_QUEUE_TIMER, snd_seq_ioctl_get_queue_timer },
  1975. { SNDRV_SEQ_IOCTL_SET_QUEUE_TIMER, snd_seq_ioctl_set_queue_timer },
  1976. { SNDRV_SEQ_IOCTL_GET_QUEUE_CLIENT, snd_seq_ioctl_get_queue_client },
  1977. { SNDRV_SEQ_IOCTL_SET_QUEUE_CLIENT, snd_seq_ioctl_set_queue_client },
  1978. { SNDRV_SEQ_IOCTL_GET_CLIENT_POOL, snd_seq_ioctl_get_client_pool },
  1979. { SNDRV_SEQ_IOCTL_SET_CLIENT_POOL, snd_seq_ioctl_set_client_pool },
  1980. { SNDRV_SEQ_IOCTL_GET_SUBSCRIPTION, snd_seq_ioctl_get_subscription },
  1981. { SNDRV_SEQ_IOCTL_QUERY_NEXT_CLIENT, snd_seq_ioctl_query_next_client },
  1982. { SNDRV_SEQ_IOCTL_QUERY_NEXT_PORT, snd_seq_ioctl_query_next_port },
  1983. { SNDRV_SEQ_IOCTL_REMOVE_EVENTS, snd_seq_ioctl_remove_events },
  1984. { SNDRV_SEQ_IOCTL_QUERY_SUBS, snd_seq_ioctl_query_subs },
  1985. { 0, NULL },
  1986. };
  1987. static long snd_seq_ioctl(struct file *file, unsigned int cmd,
  1988. unsigned long arg)
  1989. {
  1990. struct snd_seq_client *client = file->private_data;
  1991. /* To use kernel stack for ioctl data. */
  1992. union {
  1993. int pversion;
  1994. int client_id;
  1995. struct snd_seq_system_info system_info;
  1996. struct snd_seq_running_info running_info;
  1997. struct snd_seq_client_info client_info;
  1998. struct snd_seq_port_info port_info;
  1999. struct snd_seq_port_subscribe port_subscribe;
  2000. struct snd_seq_queue_info queue_info;
  2001. struct snd_seq_queue_status queue_status;
  2002. struct snd_seq_queue_tempo tempo;
  2003. struct snd_seq_queue_timer queue_timer;
  2004. struct snd_seq_queue_client queue_client;
  2005. struct snd_seq_client_pool client_pool;
  2006. struct snd_seq_remove_events remove_events;
  2007. struct snd_seq_query_subs query_subs;
  2008. } buf;
  2009. const struct ioctl_handler *handler;
  2010. unsigned long size;
  2011. int err;
  2012. if (snd_BUG_ON(!client))
  2013. return -ENXIO;
  2014. #if IS_ENABLED(CONFIG_SND_SEQ_UMP)
  2015. /* exception - handling large data */
  2016. switch (cmd) {
  2017. case SNDRV_SEQ_IOCTL_GET_CLIENT_UMP_INFO:
  2018. case SNDRV_SEQ_IOCTL_SET_CLIENT_UMP_INFO:
  2019. return snd_seq_ioctl_client_ump_info(client, cmd, arg);
  2020. }
  2021. #endif
  2022. for (handler = ioctl_handlers; handler->cmd > 0; ++handler) {
  2023. if (handler->cmd == cmd)
  2024. break;
  2025. }
  2026. if (handler->cmd == 0)
  2027. return -ENOTTY;
  2028. memset(&buf, 0, sizeof(buf));
  2029. /*
  2030. * All of ioctl commands for ALSA sequencer get an argument of size
  2031. * within 13 bits. We can safely pick up the size from the command.
  2032. */
  2033. size = _IOC_SIZE(handler->cmd);
  2034. if (handler->cmd & IOC_IN) {
  2035. if (copy_from_user(&buf, (const void __user *)arg, size))
  2036. return -EFAULT;
  2037. }
  2038. mutex_lock(&client->ioctl_mutex);
  2039. err = handler->func(client, &buf);
  2040. mutex_unlock(&client->ioctl_mutex);
  2041. if (err >= 0) {
  2042. /* Some commands includes a bug in 'dir' field. */
  2043. if (handler->cmd == SNDRV_SEQ_IOCTL_SET_QUEUE_CLIENT ||
  2044. handler->cmd == SNDRV_SEQ_IOCTL_SET_CLIENT_POOL ||
  2045. (handler->cmd & IOC_OUT))
  2046. if (copy_to_user((void __user *)arg, &buf, size))
  2047. return -EFAULT;
  2048. }
  2049. return err;
  2050. }
  2051. #ifdef CONFIG_COMPAT
  2052. #include "seq_compat.c"
  2053. #else
  2054. #define snd_seq_ioctl_compat NULL
  2055. #endif
  2056. /* -------------------------------------------------------- */
  2057. /* exported to kernel modules */
  2058. int snd_seq_create_kernel_client(struct snd_card *card, int client_index,
  2059. const char *name_fmt, ...)
  2060. {
  2061. struct snd_seq_client *client;
  2062. va_list args;
  2063. if (snd_BUG_ON(in_interrupt()))
  2064. return -EBUSY;
  2065. if (card && client_index >= SNDRV_SEQ_CLIENTS_PER_CARD)
  2066. return -EINVAL;
  2067. if (card == NULL && client_index >= SNDRV_SEQ_GLOBAL_CLIENTS)
  2068. return -EINVAL;
  2069. mutex_lock(&register_mutex);
  2070. if (card) {
  2071. client_index += SNDRV_SEQ_GLOBAL_CLIENTS
  2072. + card->number * SNDRV_SEQ_CLIENTS_PER_CARD;
  2073. if (client_index >= SNDRV_SEQ_DYNAMIC_CLIENTS_BEGIN)
  2074. client_index = -1;
  2075. }
  2076. /* empty write queue as default */
  2077. client = seq_create_client1(client_index, 0);
  2078. if (client == NULL) {
  2079. mutex_unlock(&register_mutex);
  2080. return -EBUSY; /* failure code */
  2081. }
  2082. usage_alloc(&client_usage, 1);
  2083. client->accept_input = 1;
  2084. client->accept_output = 1;
  2085. client->data.kernel.card = card;
  2086. client->user_pversion = SNDRV_SEQ_VERSION;
  2087. va_start(args, name_fmt);
  2088. vsnprintf(client->name, sizeof(client->name), name_fmt, args);
  2089. va_end(args);
  2090. client->type = KERNEL_CLIENT;
  2091. mutex_unlock(&register_mutex);
  2092. /* make others aware this new client */
  2093. snd_seq_system_client_ev_client_start(client->number);
  2094. /* return client number to caller */
  2095. return client->number;
  2096. }
  2097. EXPORT_SYMBOL(snd_seq_create_kernel_client);
  2098. /* exported to kernel modules */
  2099. int snd_seq_delete_kernel_client(int client)
  2100. {
  2101. struct snd_seq_client *ptr;
  2102. if (snd_BUG_ON(in_interrupt()))
  2103. return -EBUSY;
  2104. ptr = clientptr(client);
  2105. if (ptr == NULL)
  2106. return -EINVAL;
  2107. seq_free_client(ptr);
  2108. kfree(ptr);
  2109. return 0;
  2110. }
  2111. EXPORT_SYMBOL(snd_seq_delete_kernel_client);
  2112. /*
  2113. * exported, called by kernel clients to enqueue events (w/o blocking)
  2114. *
  2115. * RETURN VALUE: zero if succeed, negative if error
  2116. */
  2117. int snd_seq_kernel_client_enqueue(int client, struct snd_seq_event *ev,
  2118. struct file *file, bool blocking)
  2119. {
  2120. struct snd_seq_client *cptr;
  2121. int result;
  2122. if (snd_BUG_ON(!ev))
  2123. return -EINVAL;
  2124. if (!snd_seq_ev_is_ump(ev)) {
  2125. if (ev->type == SNDRV_SEQ_EVENT_NONE)
  2126. return 0; /* ignore this */
  2127. if (ev->type == SNDRV_SEQ_EVENT_KERNEL_ERROR)
  2128. return -EINVAL; /* quoted events can't be enqueued */
  2129. }
  2130. /* fill in client number */
  2131. ev->source.client = client;
  2132. if (check_event_type_and_length(ev))
  2133. return -EINVAL;
  2134. cptr = client_load_and_use_ptr(client);
  2135. if (cptr == NULL)
  2136. return -EINVAL;
  2137. if (!cptr->accept_output) {
  2138. result = -EPERM;
  2139. } else { /* send it */
  2140. mutex_lock(&cptr->ioctl_mutex);
  2141. result = snd_seq_client_enqueue_event(cptr, ev, file, blocking,
  2142. false, 0,
  2143. &cptr->ioctl_mutex);
  2144. mutex_unlock(&cptr->ioctl_mutex);
  2145. }
  2146. snd_seq_client_unlock(cptr);
  2147. return result;
  2148. }
  2149. EXPORT_SYMBOL(snd_seq_kernel_client_enqueue);
  2150. /*
  2151. * exported, called by kernel clients to dispatch events directly to other
  2152. * clients, bypassing the queues. Event time-stamp will be updated.
  2153. *
  2154. * RETURN VALUE: negative = delivery failed,
  2155. * zero, or positive: the number of delivered events
  2156. */
  2157. int snd_seq_kernel_client_dispatch(int client, struct snd_seq_event * ev,
  2158. int atomic, int hop)
  2159. {
  2160. struct snd_seq_client *cptr;
  2161. int result;
  2162. if (snd_BUG_ON(!ev))
  2163. return -EINVAL;
  2164. /* fill in client number */
  2165. ev->queue = SNDRV_SEQ_QUEUE_DIRECT;
  2166. ev->source.client = client;
  2167. if (check_event_type_and_length(ev))
  2168. return -EINVAL;
  2169. cptr = snd_seq_client_use_ptr(client);
  2170. if (cptr == NULL)
  2171. return -EINVAL;
  2172. if (!cptr->accept_output)
  2173. result = -EPERM;
  2174. else
  2175. result = snd_seq_deliver_event(cptr, ev, atomic, hop);
  2176. snd_seq_client_unlock(cptr);
  2177. return result;
  2178. }
  2179. EXPORT_SYMBOL(snd_seq_kernel_client_dispatch);
  2180. /**
  2181. * snd_seq_kernel_client_ctl - operate a command for a client with data in
  2182. * kernel space.
  2183. * @clientid: A numerical ID for a client.
  2184. * @cmd: An ioctl(2) command for ALSA sequencer operation.
  2185. * @arg: A pointer to data in kernel space.
  2186. *
  2187. * Against its name, both kernel/application client can be handled by this
  2188. * kernel API. A pointer of 'arg' argument should be in kernel space.
  2189. *
  2190. * Return: 0 at success. Negative error code at failure.
  2191. */
  2192. int snd_seq_kernel_client_ctl(int clientid, unsigned int cmd, void *arg)
  2193. {
  2194. const struct ioctl_handler *handler;
  2195. struct snd_seq_client *client;
  2196. client = clientptr(clientid);
  2197. if (client == NULL)
  2198. return -ENXIO;
  2199. for (handler = ioctl_handlers; handler->cmd > 0; ++handler) {
  2200. if (handler->cmd == cmd)
  2201. return handler->func(client, arg);
  2202. }
  2203. pr_debug("ALSA: seq unknown ioctl() 0x%x (type='%c', number=0x%02x)\n",
  2204. cmd, _IOC_TYPE(cmd), _IOC_NR(cmd));
  2205. return -ENOTTY;
  2206. }
  2207. EXPORT_SYMBOL(snd_seq_kernel_client_ctl);
  2208. /* exported (for OSS emulator) */
  2209. int snd_seq_kernel_client_write_poll(int clientid, struct file *file, poll_table *wait)
  2210. {
  2211. struct snd_seq_client *client;
  2212. client = clientptr(clientid);
  2213. if (client == NULL)
  2214. return -ENXIO;
  2215. if (snd_seq_pool_poll_wait(client->pool, file, wait))
  2216. return 1;
  2217. return 0;
  2218. }
  2219. EXPORT_SYMBOL(snd_seq_kernel_client_write_poll);
  2220. /* get a sequencer client object; for internal use from a kernel client */
  2221. struct snd_seq_client *snd_seq_kernel_client_get(int id)
  2222. {
  2223. return snd_seq_client_use_ptr(id);
  2224. }
  2225. EXPORT_SYMBOL_GPL(snd_seq_kernel_client_get);
  2226. /* put a sequencer client object; for internal use from a kernel client */
  2227. void snd_seq_kernel_client_put(struct snd_seq_client *cptr)
  2228. {
  2229. if (cptr)
  2230. snd_seq_client_unlock(cptr);
  2231. }
  2232. EXPORT_SYMBOL_GPL(snd_seq_kernel_client_put);
  2233. /*---------------------------------------------------------------------------*/
  2234. #ifdef CONFIG_SND_PROC_FS
  2235. /*
  2236. * /proc interface
  2237. */
  2238. static void snd_seq_info_dump_subscribers(struct snd_info_buffer *buffer,
  2239. struct snd_seq_port_subs_info *group,
  2240. int is_src, char *msg)
  2241. {
  2242. struct list_head *p;
  2243. struct snd_seq_subscribers *s;
  2244. int count = 0;
  2245. down_read(&group->list_mutex);
  2246. if (list_empty(&group->list_head)) {
  2247. up_read(&group->list_mutex);
  2248. return;
  2249. }
  2250. snd_iprintf(buffer, msg);
  2251. list_for_each(p, &group->list_head) {
  2252. if (is_src)
  2253. s = list_entry(p, struct snd_seq_subscribers, src_list);
  2254. else
  2255. s = list_entry(p, struct snd_seq_subscribers, dest_list);
  2256. if (count++)
  2257. snd_iprintf(buffer, ", ");
  2258. snd_iprintf(buffer, "%d:%d",
  2259. is_src ? s->info.dest.client : s->info.sender.client,
  2260. is_src ? s->info.dest.port : s->info.sender.port);
  2261. if (s->info.flags & SNDRV_SEQ_PORT_SUBS_TIMESTAMP)
  2262. snd_iprintf(buffer, "[%c:%d]", ((s->info.flags & SNDRV_SEQ_PORT_SUBS_TIME_REAL) ? 'r' : 't'), s->info.queue);
  2263. if (group->exclusive)
  2264. snd_iprintf(buffer, "[ex]");
  2265. }
  2266. up_read(&group->list_mutex);
  2267. snd_iprintf(buffer, "\n");
  2268. }
  2269. #define FLAG_PERM_RD(perm) ((perm) & SNDRV_SEQ_PORT_CAP_READ ? ((perm) & SNDRV_SEQ_PORT_CAP_SUBS_READ ? 'R' : 'r') : '-')
  2270. #define FLAG_PERM_WR(perm) ((perm) & SNDRV_SEQ_PORT_CAP_WRITE ? ((perm) & SNDRV_SEQ_PORT_CAP_SUBS_WRITE ? 'W' : 'w') : '-')
  2271. #define FLAG_PERM_EX(perm) ((perm) & SNDRV_SEQ_PORT_CAP_NO_EXPORT ? '-' : 'e')
  2272. #define FLAG_PERM_DUPLEX(perm) ((perm) & SNDRV_SEQ_PORT_CAP_DUPLEX ? 'X' : '-')
  2273. static const char *port_direction_name(unsigned char dir)
  2274. {
  2275. static const char *names[4] = {
  2276. "-", "In", "Out", "In/Out"
  2277. };
  2278. if (dir > SNDRV_SEQ_PORT_DIR_BIDIRECTION)
  2279. return "Invalid";
  2280. return names[dir];
  2281. }
  2282. static void snd_seq_info_dump_ports(struct snd_info_buffer *buffer,
  2283. struct snd_seq_client *client)
  2284. {
  2285. struct snd_seq_client_port *p;
  2286. mutex_lock(&client->ports_mutex);
  2287. list_for_each_entry(p, &client->ports_list_head, list) {
  2288. if (p->capability & SNDRV_SEQ_PORT_CAP_INACTIVE)
  2289. continue;
  2290. snd_iprintf(buffer, " Port %3d : \"%s\" (%c%c%c%c) [%s]",
  2291. p->addr.port, p->name,
  2292. FLAG_PERM_RD(p->capability),
  2293. FLAG_PERM_WR(p->capability),
  2294. FLAG_PERM_EX(p->capability),
  2295. FLAG_PERM_DUPLEX(p->capability),
  2296. port_direction_name(p->direction));
  2297. #if IS_ENABLED(CONFIG_SND_SEQ_UMP)
  2298. if (snd_seq_client_is_midi2(client) && p->is_midi1)
  2299. snd_iprintf(buffer, " [MIDI1]");
  2300. #endif
  2301. snd_iprintf(buffer, "\n");
  2302. snd_seq_info_dump_subscribers(buffer, &p->c_src, 1, " Connecting To: ");
  2303. snd_seq_info_dump_subscribers(buffer, &p->c_dest, 0, " Connected From: ");
  2304. }
  2305. mutex_unlock(&client->ports_mutex);
  2306. }
  2307. static const char *midi_version_string(unsigned int version)
  2308. {
  2309. switch (version) {
  2310. case SNDRV_SEQ_CLIENT_LEGACY_MIDI:
  2311. return "Legacy";
  2312. case SNDRV_SEQ_CLIENT_UMP_MIDI_1_0:
  2313. return "UMP MIDI1";
  2314. case SNDRV_SEQ_CLIENT_UMP_MIDI_2_0:
  2315. return "UMP MIDI2";
  2316. default:
  2317. return "Unknown";
  2318. }
  2319. }
  2320. /* exported to seq_info.c */
  2321. void snd_seq_info_clients_read(struct snd_info_entry *entry,
  2322. struct snd_info_buffer *buffer)
  2323. {
  2324. int c;
  2325. struct snd_seq_client *client;
  2326. snd_iprintf(buffer, "Client info\n");
  2327. snd_iprintf(buffer, " cur clients : %d\n", client_usage.cur);
  2328. snd_iprintf(buffer, " peak clients : %d\n", client_usage.peak);
  2329. snd_iprintf(buffer, " max clients : %d\n", SNDRV_SEQ_MAX_CLIENTS);
  2330. snd_iprintf(buffer, "\n");
  2331. /* list the client table */
  2332. for (c = 0; c < SNDRV_SEQ_MAX_CLIENTS; c++) {
  2333. client = client_load_and_use_ptr(c);
  2334. if (client == NULL)
  2335. continue;
  2336. if (client->type == NO_CLIENT) {
  2337. snd_seq_client_unlock(client);
  2338. continue;
  2339. }
  2340. snd_iprintf(buffer, "Client %3d : \"%s\" [%s %s]\n",
  2341. c, client->name,
  2342. client->type == USER_CLIENT ? "User" : "Kernel",
  2343. midi_version_string(client->midi_version));
  2344. #if IS_ENABLED(CONFIG_SND_SEQ_UMP)
  2345. dump_ump_info(buffer, client);
  2346. #endif
  2347. snd_seq_info_dump_ports(buffer, client);
  2348. if (snd_seq_write_pool_allocated(client)) {
  2349. snd_iprintf(buffer, " Output pool :\n");
  2350. snd_seq_info_pool(buffer, client->pool, " ");
  2351. }
  2352. if (client->type == USER_CLIENT && client->data.user.fifo &&
  2353. client->data.user.fifo->pool) {
  2354. snd_iprintf(buffer, " Input pool :\n");
  2355. snd_seq_info_pool(buffer, client->data.user.fifo->pool, " ");
  2356. }
  2357. snd_seq_client_unlock(client);
  2358. }
  2359. }
  2360. #endif /* CONFIG_SND_PROC_FS */
  2361. /*---------------------------------------------------------------------------*/
  2362. /*
  2363. * REGISTRATION PART
  2364. */
  2365. static const struct file_operations snd_seq_f_ops =
  2366. {
  2367. .owner = THIS_MODULE,
  2368. .read = snd_seq_read,
  2369. .write = snd_seq_write,
  2370. .open = snd_seq_open,
  2371. .release = snd_seq_release,
  2372. .poll = snd_seq_poll,
  2373. .unlocked_ioctl = snd_seq_ioctl,
  2374. .compat_ioctl = snd_seq_ioctl_compat,
  2375. };
  2376. static struct device *seq_dev;
  2377. /*
  2378. * register sequencer device
  2379. */
  2380. int __init snd_sequencer_device_init(void)
  2381. {
  2382. int err;
  2383. err = snd_device_alloc(&seq_dev, NULL);
  2384. if (err < 0)
  2385. return err;
  2386. dev_set_name(seq_dev, "seq");
  2387. mutex_lock(&register_mutex);
  2388. err = snd_register_device(SNDRV_DEVICE_TYPE_SEQUENCER, NULL, 0,
  2389. &snd_seq_f_ops, NULL, seq_dev);
  2390. mutex_unlock(&register_mutex);
  2391. if (err < 0) {
  2392. put_device(seq_dev);
  2393. return err;
  2394. }
  2395. return 0;
  2396. }
  2397. /*
  2398. * unregister sequencer device
  2399. */
  2400. void snd_sequencer_device_done(void)
  2401. {
  2402. snd_unregister_device(seq_dev);
  2403. put_device(seq_dev);
  2404. }