seq_clientmgr.c 66 KB

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