seq_clientmgr.c 66 KB

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