seq_clientmgr.c 67 KB

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