seq_clientmgr.c 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581
  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 <sound/driver.h>
  24. #include <linux/init.h>
  25. #include <linux/slab.h>
  26. #include <sound/core.h>
  27. #include <sound/minors.h>
  28. #include <linux/kmod.h>
  29. #include <sound/seq_kernel.h>
  30. #include "seq_clientmgr.h"
  31. #include "seq_memory.h"
  32. #include "seq_queue.h"
  33. #include "seq_timer.h"
  34. #include "seq_info.h"
  35. #include "seq_system.h"
  36. #include <sound/seq_device.h>
  37. #ifdef CONFIG_COMPAT
  38. #include <linux/compat.h>
  39. #endif
  40. /* Client Manager
  41. * this module handles the connections of userland and kernel clients
  42. *
  43. */
  44. /*
  45. * There are four ranges of client numbers (last two shared):
  46. * 0..15: global clients
  47. * 16..127: statically allocated client numbers for cards 0..27
  48. * 128..191: dynamically allocated client numbers for cards 28..31
  49. * 128..191: dynamically allocated client numbers for applications
  50. */
  51. /* number of kernel non-card clients */
  52. #define SNDRV_SEQ_GLOBAL_CLIENTS 16
  53. /* clients per cards, for static clients */
  54. #define SNDRV_SEQ_CLIENTS_PER_CARD 4
  55. /* dynamically allocated client numbers (both kernel drivers and user space) */
  56. #define SNDRV_SEQ_DYNAMIC_CLIENTS_BEGIN 128
  57. #define SNDRV_SEQ_LFLG_INPUT 0x0001
  58. #define SNDRV_SEQ_LFLG_OUTPUT 0x0002
  59. #define SNDRV_SEQ_LFLG_OPEN (SNDRV_SEQ_LFLG_INPUT|SNDRV_SEQ_LFLG_OUTPUT)
  60. static DEFINE_SPINLOCK(clients_lock);
  61. static DEFINE_MUTEX(register_mutex);
  62. /*
  63. * client table
  64. */
  65. static char clienttablock[SNDRV_SEQ_MAX_CLIENTS];
  66. static struct snd_seq_client *clienttab[SNDRV_SEQ_MAX_CLIENTS];
  67. static struct snd_seq_usage client_usage;
  68. /*
  69. * prototypes
  70. */
  71. static int bounce_error_event(struct snd_seq_client *client,
  72. struct snd_seq_event *event,
  73. int err, int atomic, int hop);
  74. static int snd_seq_deliver_single_event(struct snd_seq_client *client,
  75. struct snd_seq_event *event,
  76. int filter, int atomic, int hop);
  77. /*
  78. */
  79. static inline mm_segment_t snd_enter_user(void)
  80. {
  81. mm_segment_t fs = get_fs();
  82. set_fs(get_ds());
  83. return fs;
  84. }
  85. static inline void snd_leave_user(mm_segment_t fs)
  86. {
  87. set_fs(fs);
  88. }
  89. /*
  90. */
  91. static inline unsigned short snd_seq_file_flags(struct file *file)
  92. {
  93. switch (file->f_mode & (FMODE_READ | FMODE_WRITE)) {
  94. case FMODE_WRITE:
  95. return SNDRV_SEQ_LFLG_OUTPUT;
  96. case FMODE_READ:
  97. return SNDRV_SEQ_LFLG_INPUT;
  98. default:
  99. return SNDRV_SEQ_LFLG_OPEN;
  100. }
  101. }
  102. static inline int snd_seq_write_pool_allocated(struct snd_seq_client *client)
  103. {
  104. return snd_seq_total_cells(client->pool) > 0;
  105. }
  106. /* return pointer to client structure for specified id */
  107. static struct snd_seq_client *clientptr(int clientid)
  108. {
  109. if (clientid < 0 || clientid >= SNDRV_SEQ_MAX_CLIENTS) {
  110. snd_printd("Seq: oops. Trying to get pointer to client %d\n",
  111. clientid);
  112. return NULL;
  113. }
  114. return clienttab[clientid];
  115. }
  116. extern int seq_client_load[];
  117. struct snd_seq_client *snd_seq_client_use_ptr(int clientid)
  118. {
  119. unsigned long flags;
  120. struct snd_seq_client *client;
  121. if (clientid < 0 || clientid >= SNDRV_SEQ_MAX_CLIENTS) {
  122. snd_printd("Seq: oops. Trying to get pointer to client %d\n",
  123. clientid);
  124. return NULL;
  125. }
  126. spin_lock_irqsave(&clients_lock, flags);
  127. client = clientptr(clientid);
  128. if (client)
  129. goto __lock;
  130. if (clienttablock[clientid]) {
  131. spin_unlock_irqrestore(&clients_lock, flags);
  132. return NULL;
  133. }
  134. spin_unlock_irqrestore(&clients_lock, flags);
  135. #ifdef CONFIG_KMOD
  136. if (!in_interrupt() && current->fs->root) {
  137. static char client_requested[SNDRV_SEQ_GLOBAL_CLIENTS];
  138. static char card_requested[SNDRV_CARDS];
  139. if (clientid < SNDRV_SEQ_GLOBAL_CLIENTS) {
  140. int idx;
  141. if (! client_requested[clientid] && current->fs->root) {
  142. client_requested[clientid] = 1;
  143. for (idx = 0; idx < 15; idx++) {
  144. if (seq_client_load[idx] < 0)
  145. break;
  146. if (seq_client_load[idx] == clientid) {
  147. request_module("snd-seq-client-%i",
  148. clientid);
  149. break;
  150. }
  151. }
  152. }
  153. } else if (clientid < SNDRV_SEQ_DYNAMIC_CLIENTS_BEGIN) {
  154. int card = (clientid - SNDRV_SEQ_GLOBAL_CLIENTS) /
  155. SNDRV_SEQ_CLIENTS_PER_CARD;
  156. if (card < snd_ecards_limit) {
  157. if (! card_requested[card]) {
  158. card_requested[card] = 1;
  159. snd_request_card(card);
  160. }
  161. snd_seq_device_load_drivers();
  162. }
  163. }
  164. spin_lock_irqsave(&clients_lock, flags);
  165. client = clientptr(clientid);
  166. if (client)
  167. goto __lock;
  168. spin_unlock_irqrestore(&clients_lock, flags);
  169. }
  170. #endif
  171. return NULL;
  172. __lock:
  173. snd_use_lock_use(&client->use_lock);
  174. spin_unlock_irqrestore(&clients_lock, flags);
  175. return client;
  176. }
  177. static void usage_alloc(struct snd_seq_usage *res, int num)
  178. {
  179. res->cur += num;
  180. if (res->cur > res->peak)
  181. res->peak = res->cur;
  182. }
  183. static void usage_free(struct snd_seq_usage *res, int num)
  184. {
  185. res->cur -= num;
  186. }
  187. /* initialise data structures */
  188. int __init client_init_data(void)
  189. {
  190. /* zap out the client table */
  191. memset(&clienttablock, 0, sizeof(clienttablock));
  192. memset(&clienttab, 0, sizeof(clienttab));
  193. return 0;
  194. }
  195. static struct snd_seq_client *seq_create_client1(int client_index, int poolsize)
  196. {
  197. unsigned long flags;
  198. int c;
  199. struct snd_seq_client *client;
  200. /* init client data */
  201. client = kzalloc(sizeof(*client), GFP_KERNEL);
  202. if (client == NULL)
  203. return NULL;
  204. client->pool = snd_seq_pool_new(poolsize);
  205. if (client->pool == NULL) {
  206. kfree(client);
  207. return NULL;
  208. }
  209. client->type = NO_CLIENT;
  210. snd_use_lock_init(&client->use_lock);
  211. rwlock_init(&client->ports_lock);
  212. mutex_init(&client->ports_mutex);
  213. INIT_LIST_HEAD(&client->ports_list_head);
  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. snd_assert(client != NULL, return -EINVAL);
  242. snd_seq_delete_all_ports(client);
  243. snd_seq_queue_client_leave(client->number);
  244. spin_lock_irqsave(&clients_lock, flags);
  245. clienttablock[client->number] = 1;
  246. clienttab[client->number] = NULL;
  247. spin_unlock_irqrestore(&clients_lock, flags);
  248. snd_use_lock_sync(&client->use_lock);
  249. snd_seq_queue_client_termination(client->number);
  250. if (client->pool)
  251. snd_seq_pool_delete(&client->pool);
  252. spin_lock_irqsave(&clients_lock, flags);
  253. clienttablock[client->number] = 0;
  254. spin_unlock_irqrestore(&clients_lock, flags);
  255. return 0;
  256. }
  257. static void seq_free_client(struct snd_seq_client * client)
  258. {
  259. mutex_lock(&register_mutex);
  260. switch (client->type) {
  261. case NO_CLIENT:
  262. snd_printk(KERN_WARNING "Seq: Trying to free unused client %d\n",
  263. client->number);
  264. break;
  265. case USER_CLIENT:
  266. case KERNEL_CLIENT:
  267. seq_free_client1(client);
  268. usage_free(&client_usage, 1);
  269. break;
  270. default:
  271. snd_printk(KERN_ERR "Seq: Trying to free client %d with undefined type = %d\n",
  272. client->number, client->type);
  273. }
  274. mutex_unlock(&register_mutex);
  275. snd_seq_system_client_ev_client_exit(client->number);
  276. }
  277. /* -------------------------------------------------------- */
  278. /* create a user client */
  279. static int snd_seq_open(struct inode *inode, struct file *file)
  280. {
  281. int c, mode; /* client id */
  282. struct snd_seq_client *client;
  283. struct snd_seq_user_client *user;
  284. if (mutex_lock_interruptible(&register_mutex))
  285. return -ERESTARTSYS;
  286. client = seq_create_client1(-1, SNDRV_SEQ_DEFAULT_EVENTS);
  287. if (client == NULL) {
  288. mutex_unlock(&register_mutex);
  289. return -ENOMEM; /* failure code */
  290. }
  291. mode = snd_seq_file_flags(file);
  292. if (mode & SNDRV_SEQ_LFLG_INPUT)
  293. client->accept_input = 1;
  294. if (mode & SNDRV_SEQ_LFLG_OUTPUT)
  295. client->accept_output = 1;
  296. user = &client->data.user;
  297. user->fifo = NULL;
  298. user->fifo_pool_size = 0;
  299. if (mode & SNDRV_SEQ_LFLG_INPUT) {
  300. user->fifo_pool_size = SNDRV_SEQ_DEFAULT_CLIENT_EVENTS;
  301. user->fifo = snd_seq_fifo_new(user->fifo_pool_size);
  302. if (user->fifo == NULL) {
  303. seq_free_client1(client);
  304. kfree(client);
  305. mutex_unlock(&register_mutex);
  306. return -ENOMEM;
  307. }
  308. }
  309. usage_alloc(&client_usage, 1);
  310. client->type = USER_CLIENT;
  311. mutex_unlock(&register_mutex);
  312. c = client->number;
  313. file->private_data = client;
  314. /* fill client data */
  315. user->file = file;
  316. sprintf(client->name, "Client-%d", c);
  317. /* make others aware this new client */
  318. snd_seq_system_client_ev_client_start(c);
  319. return 0;
  320. }
  321. /* delete a user client */
  322. static int snd_seq_release(struct inode *inode, struct file *file)
  323. {
  324. struct snd_seq_client *client = file->private_data;
  325. if (client) {
  326. seq_free_client(client);
  327. if (client->data.user.fifo)
  328. snd_seq_fifo_delete(&client->data.user.fifo);
  329. kfree(client);
  330. }
  331. return 0;
  332. }
  333. /* handle client read() */
  334. /* possible error values:
  335. * -ENXIO invalid client or file open mode
  336. * -ENOSPC FIFO overflow (the flag is cleared after this error report)
  337. * -EINVAL no enough user-space buffer to write the whole event
  338. * -EFAULT seg. fault during copy to user space
  339. */
  340. static ssize_t snd_seq_read(struct file *file, char __user *buf, size_t count,
  341. loff_t *offset)
  342. {
  343. struct snd_seq_client *client = file->private_data;
  344. struct snd_seq_fifo *fifo;
  345. int err;
  346. long result = 0;
  347. struct snd_seq_event_cell *cell;
  348. if (!(snd_seq_file_flags(file) & SNDRV_SEQ_LFLG_INPUT))
  349. return -ENXIO;
  350. if (!access_ok(VERIFY_WRITE, buf, count))
  351. return -EFAULT;
  352. /* check client structures are in place */
  353. snd_assert(client != NULL, return -ENXIO);
  354. if (!client->accept_input || (fifo = client->data.user.fifo) == NULL)
  355. return -ENXIO;
  356. if (atomic_read(&fifo->overflow) > 0) {
  357. /* buffer overflow is detected */
  358. snd_seq_fifo_clear(fifo);
  359. /* return error code */
  360. return -ENOSPC;
  361. }
  362. cell = NULL;
  363. err = 0;
  364. snd_seq_fifo_lock(fifo);
  365. /* while data available in queue */
  366. while (count >= sizeof(struct snd_seq_event)) {
  367. int nonblock;
  368. nonblock = (file->f_flags & O_NONBLOCK) || result > 0;
  369. if ((err = snd_seq_fifo_cell_out(fifo, &cell, nonblock)) < 0) {
  370. break;
  371. }
  372. if (snd_seq_ev_is_variable(&cell->event)) {
  373. struct snd_seq_event tmpev;
  374. tmpev = cell->event;
  375. tmpev.data.ext.len &= ~SNDRV_SEQ_EXT_MASK;
  376. if (copy_to_user(buf, &tmpev, sizeof(struct snd_seq_event))) {
  377. err = -EFAULT;
  378. break;
  379. }
  380. count -= sizeof(struct snd_seq_event);
  381. buf += sizeof(struct snd_seq_event);
  382. err = snd_seq_expand_var_event(&cell->event, count,
  383. (char __force *)buf, 0,
  384. sizeof(struct snd_seq_event));
  385. if (err < 0)
  386. break;
  387. result += err;
  388. count -= err;
  389. buf += err;
  390. } else {
  391. if (copy_to_user(buf, &cell->event, sizeof(struct snd_seq_event))) {
  392. err = -EFAULT;
  393. break;
  394. }
  395. count -= sizeof(struct snd_seq_event);
  396. buf += sizeof(struct snd_seq_event);
  397. }
  398. snd_seq_cell_free(cell);
  399. cell = NULL; /* to be sure */
  400. result += sizeof(struct snd_seq_event);
  401. }
  402. if (err < 0) {
  403. if (cell)
  404. snd_seq_fifo_cell_putback(fifo, cell);
  405. if (err == -EAGAIN && result > 0)
  406. err = 0;
  407. }
  408. snd_seq_fifo_unlock(fifo);
  409. return (err < 0) ? err : result;
  410. }
  411. /*
  412. * check access permission to the port
  413. */
  414. static int check_port_perm(struct snd_seq_client_port *port, unsigned int flags)
  415. {
  416. if ((port->capability & flags) != flags)
  417. return 0;
  418. return flags;
  419. }
  420. /*
  421. * check if the destination client is available, and return the pointer
  422. * if filter is non-zero, client filter bitmap is tested.
  423. */
  424. static struct snd_seq_client *get_event_dest_client(struct snd_seq_event *event,
  425. int filter)
  426. {
  427. struct snd_seq_client *dest;
  428. dest = snd_seq_client_use_ptr(event->dest.client);
  429. if (dest == NULL)
  430. return NULL;
  431. if (! dest->accept_input)
  432. goto __not_avail;
  433. if ((dest->filter & SNDRV_SEQ_FILTER_USE_EVENT) &&
  434. ! test_bit(event->type, dest->event_filter))
  435. goto __not_avail;
  436. if (filter && !(dest->filter & filter))
  437. goto __not_avail;
  438. return dest; /* ok - accessible */
  439. __not_avail:
  440. snd_seq_client_unlock(dest);
  441. return NULL;
  442. }
  443. /*
  444. * Return the error event.
  445. *
  446. * If the receiver client is a user client, the original event is
  447. * encapsulated in SNDRV_SEQ_EVENT_BOUNCE as variable length event. If
  448. * the original event is also variable length, the external data is
  449. * copied after the event record.
  450. * If the receiver client is a kernel client, the original event is
  451. * quoted in SNDRV_SEQ_EVENT_KERNEL_ERROR, since this requires no extra
  452. * kmalloc.
  453. */
  454. static int bounce_error_event(struct snd_seq_client *client,
  455. struct snd_seq_event *event,
  456. int err, int atomic, int hop)
  457. {
  458. struct snd_seq_event bounce_ev;
  459. int result;
  460. if (client == NULL ||
  461. ! (client->filter & SNDRV_SEQ_FILTER_BOUNCE) ||
  462. ! client->accept_input)
  463. return 0; /* ignored */
  464. /* set up quoted error */
  465. memset(&bounce_ev, 0, sizeof(bounce_ev));
  466. bounce_ev.type = SNDRV_SEQ_EVENT_KERNEL_ERROR;
  467. bounce_ev.flags = SNDRV_SEQ_EVENT_LENGTH_FIXED;
  468. bounce_ev.queue = SNDRV_SEQ_QUEUE_DIRECT;
  469. bounce_ev.source.client = SNDRV_SEQ_CLIENT_SYSTEM;
  470. bounce_ev.source.port = SNDRV_SEQ_PORT_SYSTEM_ANNOUNCE;
  471. bounce_ev.dest.client = client->number;
  472. bounce_ev.dest.port = event->source.port;
  473. bounce_ev.data.quote.origin = event->dest;
  474. bounce_ev.data.quote.event = event;
  475. bounce_ev.data.quote.value = -err; /* use positive value */
  476. result = snd_seq_deliver_single_event(NULL, &bounce_ev, 0, atomic, hop + 1);
  477. if (result < 0) {
  478. client->event_lost++;
  479. return result;
  480. }
  481. return result;
  482. }
  483. /*
  484. * rewrite the time-stamp of the event record with the curren time
  485. * of the given queue.
  486. * return non-zero if updated.
  487. */
  488. static int update_timestamp_of_queue(struct snd_seq_event *event,
  489. int queue, int real_time)
  490. {
  491. struct snd_seq_queue *q;
  492. q = queueptr(queue);
  493. if (! q)
  494. return 0;
  495. event->queue = queue;
  496. event->flags &= ~SNDRV_SEQ_TIME_STAMP_MASK;
  497. if (real_time) {
  498. event->time.time = snd_seq_timer_get_cur_time(q->timer);
  499. event->flags |= SNDRV_SEQ_TIME_STAMP_REAL;
  500. } else {
  501. event->time.tick = snd_seq_timer_get_cur_tick(q->timer);
  502. event->flags |= SNDRV_SEQ_TIME_STAMP_TICK;
  503. }
  504. queuefree(q);
  505. return 1;
  506. }
  507. /*
  508. * deliver an event to the specified destination.
  509. * if filter is non-zero, client filter bitmap is tested.
  510. *
  511. * RETURN VALUE: 0 : if succeeded
  512. * <0 : error
  513. */
  514. static int snd_seq_deliver_single_event(struct snd_seq_client *client,
  515. struct snd_seq_event *event,
  516. int filter, int atomic, int hop)
  517. {
  518. struct snd_seq_client *dest = NULL;
  519. struct snd_seq_client_port *dest_port = NULL;
  520. int result = -ENOENT;
  521. int direct;
  522. direct = snd_seq_ev_is_direct(event);
  523. dest = get_event_dest_client(event, filter);
  524. if (dest == NULL)
  525. goto __skip;
  526. dest_port = snd_seq_port_use_ptr(dest, event->dest.port);
  527. if (dest_port == NULL)
  528. goto __skip;
  529. /* check permission */
  530. if (! check_port_perm(dest_port, SNDRV_SEQ_PORT_CAP_WRITE)) {
  531. result = -EPERM;
  532. goto __skip;
  533. }
  534. if (dest_port->timestamping)
  535. update_timestamp_of_queue(event, dest_port->time_queue,
  536. dest_port->time_real);
  537. switch (dest->type) {
  538. case USER_CLIENT:
  539. if (dest->data.user.fifo)
  540. result = snd_seq_fifo_event_in(dest->data.user.fifo, event);
  541. break;
  542. case KERNEL_CLIENT:
  543. if (dest_port->event_input == NULL)
  544. break;
  545. result = dest_port->event_input(event, direct,
  546. dest_port->private_data,
  547. atomic, hop);
  548. break;
  549. default:
  550. break;
  551. }
  552. __skip:
  553. if (dest_port)
  554. snd_seq_port_unlock(dest_port);
  555. if (dest)
  556. snd_seq_client_unlock(dest);
  557. if (result < 0 && !direct) {
  558. result = bounce_error_event(client, event, result, atomic, hop);
  559. }
  560. return result;
  561. }
  562. /*
  563. * send the event to all subscribers:
  564. */
  565. static int deliver_to_subscribers(struct snd_seq_client *client,
  566. struct snd_seq_event *event,
  567. int atomic, int hop)
  568. {
  569. struct snd_seq_subscribers *subs;
  570. int err = 0, num_ev = 0;
  571. struct snd_seq_event event_saved;
  572. struct snd_seq_client_port *src_port;
  573. struct snd_seq_port_subs_info *grp;
  574. src_port = snd_seq_port_use_ptr(client, event->source.port);
  575. if (src_port == NULL)
  576. return -EINVAL; /* invalid source port */
  577. /* save original event record */
  578. event_saved = *event;
  579. grp = &src_port->c_src;
  580. /* lock list */
  581. if (atomic)
  582. read_lock(&grp->list_lock);
  583. else
  584. down_read(&grp->list_mutex);
  585. list_for_each_entry(subs, &grp->list_head, src_list) {
  586. event->dest = subs->info.dest;
  587. if (subs->info.flags & SNDRV_SEQ_PORT_SUBS_TIMESTAMP)
  588. /* convert time according to flag with subscription */
  589. update_timestamp_of_queue(event, subs->info.queue,
  590. subs->info.flags & SNDRV_SEQ_PORT_SUBS_TIME_REAL);
  591. err = snd_seq_deliver_single_event(client, event,
  592. 0, atomic, hop);
  593. if (err < 0)
  594. break;
  595. num_ev++;
  596. /* restore original event record */
  597. *event = event_saved;
  598. }
  599. if (atomic)
  600. read_unlock(&grp->list_lock);
  601. else
  602. up_read(&grp->list_mutex);
  603. *event = event_saved; /* restore */
  604. snd_seq_port_unlock(src_port);
  605. return (err < 0) ? err : num_ev;
  606. }
  607. #ifdef SUPPORT_BROADCAST
  608. /*
  609. * broadcast to all ports:
  610. */
  611. static int port_broadcast_event(struct snd_seq_client *client,
  612. struct snd_seq_event *event,
  613. int atomic, int hop)
  614. {
  615. int num_ev = 0, err = 0;
  616. struct snd_seq_client *dest_client;
  617. struct snd_seq_client_port *port;
  618. dest_client = get_event_dest_client(event, SNDRV_SEQ_FILTER_BROADCAST);
  619. if (dest_client == NULL)
  620. return 0; /* no matching destination */
  621. read_lock(&dest_client->ports_lock);
  622. list_for_each_entry(port, &dest_client->ports_list_head, 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. EXPORT_SYMBOL(snd_seq_set_queue_tempo);
  1452. static int snd_seq_ioctl_set_queue_tempo(struct snd_seq_client *client,
  1453. void __user *arg)
  1454. {
  1455. int result;
  1456. struct snd_seq_queue_tempo tempo;
  1457. if (copy_from_user(&tempo, arg, sizeof(tempo)))
  1458. return -EFAULT;
  1459. result = snd_seq_set_queue_tempo(client->number, &tempo);
  1460. return result < 0 ? result : 0;
  1461. }
  1462. /* GET_QUEUE_TIMER ioctl() */
  1463. static int snd_seq_ioctl_get_queue_timer(struct snd_seq_client *client,
  1464. void __user *arg)
  1465. {
  1466. struct snd_seq_queue_timer timer;
  1467. struct snd_seq_queue *queue;
  1468. struct snd_seq_timer *tmr;
  1469. if (copy_from_user(&timer, arg, sizeof(timer)))
  1470. return -EFAULT;
  1471. queue = queueptr(timer.queue);
  1472. if (queue == NULL)
  1473. return -EINVAL;
  1474. if (mutex_lock_interruptible(&queue->timer_mutex)) {
  1475. queuefree(queue);
  1476. return -ERESTARTSYS;
  1477. }
  1478. tmr = queue->timer;
  1479. memset(&timer, 0, sizeof(timer));
  1480. timer.queue = queue->queue;
  1481. timer.type = tmr->type;
  1482. if (tmr->type == SNDRV_SEQ_TIMER_ALSA) {
  1483. timer.u.alsa.id = tmr->alsa_id;
  1484. timer.u.alsa.resolution = tmr->preferred_resolution;
  1485. }
  1486. mutex_unlock(&queue->timer_mutex);
  1487. queuefree(queue);
  1488. if (copy_to_user(arg, &timer, sizeof(timer)))
  1489. return -EFAULT;
  1490. return 0;
  1491. }
  1492. /* SET_QUEUE_TIMER ioctl() */
  1493. static int snd_seq_ioctl_set_queue_timer(struct snd_seq_client *client,
  1494. void __user *arg)
  1495. {
  1496. int result = 0;
  1497. struct snd_seq_queue_timer timer;
  1498. if (copy_from_user(&timer, arg, sizeof(timer)))
  1499. return -EFAULT;
  1500. if (timer.type != SNDRV_SEQ_TIMER_ALSA)
  1501. return -EINVAL;
  1502. if (snd_seq_queue_check_access(timer.queue, client->number)) {
  1503. struct snd_seq_queue *q;
  1504. struct snd_seq_timer *tmr;
  1505. q = queueptr(timer.queue);
  1506. if (q == NULL)
  1507. return -ENXIO;
  1508. if (mutex_lock_interruptible(&q->timer_mutex)) {
  1509. queuefree(q);
  1510. return -ERESTARTSYS;
  1511. }
  1512. tmr = q->timer;
  1513. snd_seq_queue_timer_close(timer.queue);
  1514. tmr->type = timer.type;
  1515. if (tmr->type == SNDRV_SEQ_TIMER_ALSA) {
  1516. tmr->alsa_id = timer.u.alsa.id;
  1517. tmr->preferred_resolution = timer.u.alsa.resolution;
  1518. }
  1519. result = snd_seq_queue_timer_open(timer.queue);
  1520. mutex_unlock(&q->timer_mutex);
  1521. queuefree(q);
  1522. } else {
  1523. return -EPERM;
  1524. }
  1525. return result;
  1526. }
  1527. /* GET_QUEUE_CLIENT ioctl() */
  1528. static int snd_seq_ioctl_get_queue_client(struct snd_seq_client *client,
  1529. void __user *arg)
  1530. {
  1531. struct snd_seq_queue_client info;
  1532. int used;
  1533. if (copy_from_user(&info, arg, sizeof(info)))
  1534. return -EFAULT;
  1535. used = snd_seq_queue_is_used(info.queue, client->number);
  1536. if (used < 0)
  1537. return -EINVAL;
  1538. info.used = used;
  1539. info.client = client->number;
  1540. if (copy_to_user(arg, &info, sizeof(info)))
  1541. return -EFAULT;
  1542. return 0;
  1543. }
  1544. /* SET_QUEUE_CLIENT ioctl() */
  1545. static int snd_seq_ioctl_set_queue_client(struct snd_seq_client *client,
  1546. void __user *arg)
  1547. {
  1548. int err;
  1549. struct snd_seq_queue_client info;
  1550. if (copy_from_user(&info, arg, sizeof(info)))
  1551. return -EFAULT;
  1552. if (info.used >= 0) {
  1553. err = snd_seq_queue_use(info.queue, client->number, info.used);
  1554. if (err < 0)
  1555. return err;
  1556. }
  1557. return snd_seq_ioctl_get_queue_client(client, arg);
  1558. }
  1559. /* GET_CLIENT_POOL ioctl() */
  1560. static int snd_seq_ioctl_get_client_pool(struct snd_seq_client *client,
  1561. void __user *arg)
  1562. {
  1563. struct snd_seq_client_pool info;
  1564. struct snd_seq_client *cptr;
  1565. if (copy_from_user(&info, arg, sizeof(info)))
  1566. return -EFAULT;
  1567. cptr = snd_seq_client_use_ptr(info.client);
  1568. if (cptr == NULL)
  1569. return -ENOENT;
  1570. memset(&info, 0, sizeof(info));
  1571. info.output_pool = cptr->pool->size;
  1572. info.output_room = cptr->pool->room;
  1573. info.output_free = info.output_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. const char *name_fmt, ...)
  1878. {
  1879. struct snd_seq_client *client;
  1880. va_list args;
  1881. snd_assert(! in_interrupt(), return -EBUSY);
  1882. if (card && client_index >= SNDRV_SEQ_CLIENTS_PER_CARD)
  1883. return -EINVAL;
  1884. if (card == NULL && client_index >= SNDRV_SEQ_GLOBAL_CLIENTS)
  1885. return -EINVAL;
  1886. if (mutex_lock_interruptible(&register_mutex))
  1887. return -ERESTARTSYS;
  1888. if (card) {
  1889. client_index += SNDRV_SEQ_GLOBAL_CLIENTS
  1890. + card->number * SNDRV_SEQ_CLIENTS_PER_CARD;
  1891. if (client_index >= SNDRV_SEQ_DYNAMIC_CLIENTS_BEGIN)
  1892. client_index = -1;
  1893. }
  1894. /* empty write queue as default */
  1895. client = seq_create_client1(client_index, 0);
  1896. if (client == NULL) {
  1897. mutex_unlock(&register_mutex);
  1898. return -EBUSY; /* failure code */
  1899. }
  1900. usage_alloc(&client_usage, 1);
  1901. client->accept_input = 1;
  1902. client->accept_output = 1;
  1903. va_start(args, name_fmt);
  1904. vsnprintf(client->name, sizeof(client->name), name_fmt, args);
  1905. va_end(args);
  1906. client->type = KERNEL_CLIENT;
  1907. mutex_unlock(&register_mutex);
  1908. /* make others aware this new client */
  1909. snd_seq_system_client_ev_client_start(client->number);
  1910. /* return client number to caller */
  1911. return client->number;
  1912. }
  1913. EXPORT_SYMBOL(snd_seq_create_kernel_client);
  1914. /* exported to kernel modules */
  1915. int snd_seq_delete_kernel_client(int client)
  1916. {
  1917. struct snd_seq_client *ptr;
  1918. snd_assert(! in_interrupt(), return -EBUSY);
  1919. ptr = clientptr(client);
  1920. if (ptr == NULL)
  1921. return -EINVAL;
  1922. seq_free_client(ptr);
  1923. kfree(ptr);
  1924. return 0;
  1925. }
  1926. EXPORT_SYMBOL(snd_seq_delete_kernel_client);
  1927. /* skeleton to enqueue event, called from snd_seq_kernel_client_enqueue
  1928. * and snd_seq_kernel_client_enqueue_blocking
  1929. */
  1930. static int kernel_client_enqueue(int client, struct snd_seq_event *ev,
  1931. struct file *file, int blocking,
  1932. int atomic, int hop)
  1933. {
  1934. struct snd_seq_client *cptr;
  1935. int result;
  1936. snd_assert(ev != NULL, return -EINVAL);
  1937. if (ev->type == SNDRV_SEQ_EVENT_NONE)
  1938. return 0; /* ignore this */
  1939. if (ev->type == SNDRV_SEQ_EVENT_KERNEL_ERROR)
  1940. return -EINVAL; /* quoted events can't be enqueued */
  1941. /* fill in client number */
  1942. ev->source.client = client;
  1943. if (check_event_type_and_length(ev))
  1944. return -EINVAL;
  1945. cptr = snd_seq_client_use_ptr(client);
  1946. if (cptr == NULL)
  1947. return -EINVAL;
  1948. if (! cptr->accept_output)
  1949. result = -EPERM;
  1950. else /* send it */
  1951. result = snd_seq_client_enqueue_event(cptr, ev, file, blocking, atomic, hop);
  1952. snd_seq_client_unlock(cptr);
  1953. return result;
  1954. }
  1955. /*
  1956. * exported, called by kernel clients to enqueue events (w/o blocking)
  1957. *
  1958. * RETURN VALUE: zero if succeed, negative if error
  1959. */
  1960. int snd_seq_kernel_client_enqueue(int client, struct snd_seq_event * ev,
  1961. int atomic, int hop)
  1962. {
  1963. return kernel_client_enqueue(client, ev, NULL, 0, atomic, hop);
  1964. }
  1965. EXPORT_SYMBOL(snd_seq_kernel_client_enqueue);
  1966. /*
  1967. * exported, called by kernel clients to enqueue events (with blocking)
  1968. *
  1969. * RETURN VALUE: zero if succeed, negative if error
  1970. */
  1971. int snd_seq_kernel_client_enqueue_blocking(int client, struct snd_seq_event * ev,
  1972. struct file *file,
  1973. int atomic, int hop)
  1974. {
  1975. return kernel_client_enqueue(client, ev, file, 1, atomic, hop);
  1976. }
  1977. EXPORT_SYMBOL(snd_seq_kernel_client_enqueue_blocking);
  1978. /*
  1979. * exported, called by kernel clients to dispatch events directly to other
  1980. * clients, bypassing the queues. Event time-stamp will be updated.
  1981. *
  1982. * RETURN VALUE: negative = delivery failed,
  1983. * zero, or positive: the number of delivered events
  1984. */
  1985. int snd_seq_kernel_client_dispatch(int client, struct snd_seq_event * ev,
  1986. int atomic, int hop)
  1987. {
  1988. struct snd_seq_client *cptr;
  1989. int result;
  1990. snd_assert(ev != NULL, return -EINVAL);
  1991. /* fill in client number */
  1992. ev->queue = SNDRV_SEQ_QUEUE_DIRECT;
  1993. ev->source.client = client;
  1994. if (check_event_type_and_length(ev))
  1995. return -EINVAL;
  1996. cptr = snd_seq_client_use_ptr(client);
  1997. if (cptr == NULL)
  1998. return -EINVAL;
  1999. if (!cptr->accept_output)
  2000. result = -EPERM;
  2001. else
  2002. result = snd_seq_deliver_event(cptr, ev, atomic, hop);
  2003. snd_seq_client_unlock(cptr);
  2004. return result;
  2005. }
  2006. EXPORT_SYMBOL(snd_seq_kernel_client_dispatch);
  2007. /*
  2008. * exported, called by kernel clients to perform same functions as with
  2009. * userland ioctl()
  2010. */
  2011. int snd_seq_kernel_client_ctl(int clientid, unsigned int cmd, void *arg)
  2012. {
  2013. struct snd_seq_client *client;
  2014. mm_segment_t fs;
  2015. int result;
  2016. client = clientptr(clientid);
  2017. if (client == NULL)
  2018. return -ENXIO;
  2019. fs = snd_enter_user();
  2020. result = snd_seq_do_ioctl(client, cmd, (void __user *)arg);
  2021. snd_leave_user(fs);
  2022. return result;
  2023. }
  2024. EXPORT_SYMBOL(snd_seq_kernel_client_ctl);
  2025. /* exported (for OSS emulator) */
  2026. int snd_seq_kernel_client_write_poll(int clientid, struct file *file, poll_table *wait)
  2027. {
  2028. struct snd_seq_client *client;
  2029. client = clientptr(clientid);
  2030. if (client == NULL)
  2031. return -ENXIO;
  2032. if (! snd_seq_write_pool_allocated(client))
  2033. return 1;
  2034. if (snd_seq_pool_poll_wait(client->pool, file, wait))
  2035. return 1;
  2036. return 0;
  2037. }
  2038. EXPORT_SYMBOL(snd_seq_kernel_client_write_poll);
  2039. /*---------------------------------------------------------------------------*/
  2040. #ifdef CONFIG_PROC_FS
  2041. /*
  2042. * /proc interface
  2043. */
  2044. static void snd_seq_info_dump_subscribers(struct snd_info_buffer *buffer,
  2045. struct snd_seq_port_subs_info *group,
  2046. int is_src, char *msg)
  2047. {
  2048. struct list_head *p;
  2049. struct snd_seq_subscribers *s;
  2050. int count = 0;
  2051. down_read(&group->list_mutex);
  2052. if (list_empty(&group->list_head)) {
  2053. up_read(&group->list_mutex);
  2054. return;
  2055. }
  2056. snd_iprintf(buffer, msg);
  2057. list_for_each(p, &group->list_head) {
  2058. if (is_src)
  2059. s = list_entry(p, struct snd_seq_subscribers, src_list);
  2060. else
  2061. s = list_entry(p, struct snd_seq_subscribers, dest_list);
  2062. if (count++)
  2063. snd_iprintf(buffer, ", ");
  2064. snd_iprintf(buffer, "%d:%d",
  2065. is_src ? s->info.dest.client : s->info.sender.client,
  2066. is_src ? s->info.dest.port : s->info.sender.port);
  2067. if (s->info.flags & SNDRV_SEQ_PORT_SUBS_TIMESTAMP)
  2068. snd_iprintf(buffer, "[%c:%d]", ((s->info.flags & SNDRV_SEQ_PORT_SUBS_TIME_REAL) ? 'r' : 't'), s->info.queue);
  2069. if (group->exclusive)
  2070. snd_iprintf(buffer, "[ex]");
  2071. }
  2072. up_read(&group->list_mutex);
  2073. snd_iprintf(buffer, "\n");
  2074. }
  2075. #define FLAG_PERM_RD(perm) ((perm) & SNDRV_SEQ_PORT_CAP_READ ? ((perm) & SNDRV_SEQ_PORT_CAP_SUBS_READ ? 'R' : 'r') : '-')
  2076. #define FLAG_PERM_WR(perm) ((perm) & SNDRV_SEQ_PORT_CAP_WRITE ? ((perm) & SNDRV_SEQ_PORT_CAP_SUBS_WRITE ? 'W' : 'w') : '-')
  2077. #define FLAG_PERM_EX(perm) ((perm) & SNDRV_SEQ_PORT_CAP_NO_EXPORT ? '-' : 'e')
  2078. #define FLAG_PERM_DUPLEX(perm) ((perm) & SNDRV_SEQ_PORT_CAP_DUPLEX ? 'X' : '-')
  2079. static void snd_seq_info_dump_ports(struct snd_info_buffer *buffer,
  2080. struct snd_seq_client *client)
  2081. {
  2082. struct snd_seq_client_port *p;
  2083. mutex_lock(&client->ports_mutex);
  2084. list_for_each_entry(p, &client->ports_list_head, list) {
  2085. snd_iprintf(buffer, " Port %3d : \"%s\" (%c%c%c%c)\n",
  2086. p->addr.port, p->name,
  2087. FLAG_PERM_RD(p->capability),
  2088. FLAG_PERM_WR(p->capability),
  2089. FLAG_PERM_EX(p->capability),
  2090. FLAG_PERM_DUPLEX(p->capability));
  2091. snd_seq_info_dump_subscribers(buffer, &p->c_src, 1, " Connecting To: ");
  2092. snd_seq_info_dump_subscribers(buffer, &p->c_dest, 0, " Connected From: ");
  2093. }
  2094. mutex_unlock(&client->ports_mutex);
  2095. }
  2096. void snd_seq_info_pool(struct snd_info_buffer *buffer,
  2097. struct snd_seq_pool *pool, char *space);
  2098. /* exported to seq_info.c */
  2099. void snd_seq_info_clients_read(struct snd_info_entry *entry,
  2100. struct snd_info_buffer *buffer)
  2101. {
  2102. int c;
  2103. struct snd_seq_client *client;
  2104. snd_iprintf(buffer, "Client info\n");
  2105. snd_iprintf(buffer, " cur clients : %d\n", client_usage.cur);
  2106. snd_iprintf(buffer, " peak clients : %d\n", client_usage.peak);
  2107. snd_iprintf(buffer, " max clients : %d\n", SNDRV_SEQ_MAX_CLIENTS);
  2108. snd_iprintf(buffer, "\n");
  2109. /* list the client table */
  2110. for (c = 0; c < SNDRV_SEQ_MAX_CLIENTS; c++) {
  2111. client = snd_seq_client_use_ptr(c);
  2112. if (client == NULL)
  2113. continue;
  2114. if (client->type == NO_CLIENT) {
  2115. snd_seq_client_unlock(client);
  2116. continue;
  2117. }
  2118. snd_iprintf(buffer, "Client %3d : \"%s\" [%s]\n",
  2119. c, client->name,
  2120. client->type == USER_CLIENT ? "User" : "Kernel");
  2121. snd_seq_info_dump_ports(buffer, client);
  2122. if (snd_seq_write_pool_allocated(client)) {
  2123. snd_iprintf(buffer, " Output pool :\n");
  2124. snd_seq_info_pool(buffer, client->pool, " ");
  2125. }
  2126. if (client->type == USER_CLIENT && client->data.user.fifo &&
  2127. client->data.user.fifo->pool) {
  2128. snd_iprintf(buffer, " Input pool :\n");
  2129. snd_seq_info_pool(buffer, client->data.user.fifo->pool, " ");
  2130. }
  2131. snd_seq_client_unlock(client);
  2132. }
  2133. }
  2134. #endif /* CONFIG_PROC_FS */
  2135. /*---------------------------------------------------------------------------*/
  2136. /*
  2137. * REGISTRATION PART
  2138. */
  2139. static const struct file_operations snd_seq_f_ops =
  2140. {
  2141. .owner = THIS_MODULE,
  2142. .read = snd_seq_read,
  2143. .write = snd_seq_write,
  2144. .open = snd_seq_open,
  2145. .release = snd_seq_release,
  2146. .poll = snd_seq_poll,
  2147. .unlocked_ioctl = snd_seq_ioctl,
  2148. .compat_ioctl = snd_seq_ioctl_compat,
  2149. };
  2150. /*
  2151. * register sequencer device
  2152. */
  2153. int __init snd_sequencer_device_init(void)
  2154. {
  2155. int err;
  2156. if (mutex_lock_interruptible(&register_mutex))
  2157. return -ERESTARTSYS;
  2158. if ((err = snd_register_device(SNDRV_DEVICE_TYPE_SEQUENCER, NULL, 0,
  2159. &snd_seq_f_ops, NULL, "seq")) < 0) {
  2160. mutex_unlock(&register_mutex);
  2161. return err;
  2162. }
  2163. mutex_unlock(&register_mutex);
  2164. return 0;
  2165. }
  2166. /*
  2167. * unregister sequencer device
  2168. */
  2169. void __exit snd_sequencer_device_done(void)
  2170. {
  2171. snd_unregister_device(SNDRV_DEVICE_TYPE_SEQUENCER, NULL, 0);
  2172. }