seq_clientmgr.c 66 KB

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