seq_clientmgr.c 66 KB

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