seq_clientmgr.c 66 KB

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