seq_clientmgr.c 66 KB

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