seq_clientmgr.c 67 KB

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