seq_clientmgr.c 65 KB

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