seq_clientmgr.c 66 KB

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