seq_clientmgr.c 65 KB

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