seq_clientmgr.c 65 KB

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