seq_clientmgr.c 65 KB

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