seq_clientmgr.c 66 KB

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