seq_clientmgr.c 65 KB

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