seq_clientmgr.c 66 KB

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