seq_clientmgr.c 70 KB

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