seq_clientmgr.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536
  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_nested(&grp->list_mutex, hop);
  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 (snd_seq_ev_is_variable(event) &&
  706. snd_BUG_ON(atomic && (event->data.ext.len & SNDRV_SEQ_EXT_USRPTR)))
  707. return -EINVAL;
  708. if (event->queue == SNDRV_SEQ_ADDRESS_SUBSCRIBERS ||
  709. event->dest.client == SNDRV_SEQ_ADDRESS_SUBSCRIBERS)
  710. result = deliver_to_subscribers(client, event, atomic, hop);
  711. #ifdef SUPPORT_BROADCAST
  712. else if (event->queue == SNDRV_SEQ_ADDRESS_BROADCAST ||
  713. event->dest.client == SNDRV_SEQ_ADDRESS_BROADCAST)
  714. result = broadcast_event(client, event, atomic, hop);
  715. else if (event->dest.client >= SNDRV_SEQ_MAX_CLIENTS)
  716. result = multicast_event(client, event, atomic, hop);
  717. else if (event->dest.port == SNDRV_SEQ_ADDRESS_BROADCAST)
  718. result = port_broadcast_event(client, event, atomic, hop);
  719. #endif
  720. else
  721. result = snd_seq_deliver_single_event(client, event, 0, atomic, hop);
  722. return result;
  723. }
  724. /*
  725. * dispatch an event cell:
  726. * This function is called only from queue check routines in timer
  727. * interrupts or after enqueued.
  728. * The event cell shall be released or re-queued in this function.
  729. *
  730. * RETURN VALUE: n > 0 : the number of delivered events.
  731. * n == 0 : the event was not passed to any client.
  732. * n < 0 : error - event was not processed.
  733. */
  734. int snd_seq_dispatch_event(struct snd_seq_event_cell *cell, int atomic, int hop)
  735. {
  736. struct snd_seq_client *client;
  737. int result;
  738. if (snd_BUG_ON(!cell))
  739. return -EINVAL;
  740. client = snd_seq_client_use_ptr(cell->event.source.client);
  741. if (client == NULL) {
  742. snd_seq_cell_free(cell); /* release this cell */
  743. return -EINVAL;
  744. }
  745. if (cell->event.type == SNDRV_SEQ_EVENT_NOTE) {
  746. /* NOTE event:
  747. * the event cell is re-used as a NOTE-OFF event and
  748. * enqueued again.
  749. */
  750. struct snd_seq_event tmpev, *ev;
  751. /* reserve this event to enqueue note-off later */
  752. tmpev = cell->event;
  753. tmpev.type = SNDRV_SEQ_EVENT_NOTEON;
  754. result = snd_seq_deliver_event(client, &tmpev, atomic, hop);
  755. /*
  756. * This was originally a note event. We now re-use the
  757. * cell for the note-off event.
  758. */
  759. ev = &cell->event;
  760. ev->type = SNDRV_SEQ_EVENT_NOTEOFF;
  761. ev->flags |= SNDRV_SEQ_PRIORITY_HIGH;
  762. /* add the duration time */
  763. switch (ev->flags & SNDRV_SEQ_TIME_STAMP_MASK) {
  764. case SNDRV_SEQ_TIME_STAMP_TICK:
  765. ev->time.tick += ev->data.note.duration;
  766. break;
  767. case SNDRV_SEQ_TIME_STAMP_REAL:
  768. /* unit for duration is ms */
  769. ev->time.time.tv_nsec += 1000000 * (ev->data.note.duration % 1000);
  770. ev->time.time.tv_sec += ev->data.note.duration / 1000 +
  771. ev->time.time.tv_nsec / 1000000000;
  772. ev->time.time.tv_nsec %= 1000000000;
  773. break;
  774. }
  775. ev->data.note.velocity = ev->data.note.off_velocity;
  776. /* Now queue this cell as the note off event */
  777. if (snd_seq_enqueue_event(cell, atomic, hop) < 0)
  778. snd_seq_cell_free(cell); /* release this cell */
  779. } else {
  780. /* Normal events:
  781. * event cell is freed after processing the event
  782. */
  783. result = snd_seq_deliver_event(client, &cell->event, atomic, hop);
  784. snd_seq_cell_free(cell);
  785. }
  786. snd_seq_client_unlock(client);
  787. return result;
  788. }
  789. /* Allocate a cell from client pool and enqueue it to queue:
  790. * if pool is empty and blocking is TRUE, sleep until a new cell is
  791. * available.
  792. */
  793. static int snd_seq_client_enqueue_event(struct snd_seq_client *client,
  794. struct snd_seq_event *event,
  795. struct file *file, int blocking,
  796. int atomic, int hop)
  797. {
  798. struct snd_seq_event_cell *cell;
  799. int err;
  800. /* special queue values - force direct passing */
  801. if (event->queue == SNDRV_SEQ_ADDRESS_SUBSCRIBERS) {
  802. event->dest.client = SNDRV_SEQ_ADDRESS_SUBSCRIBERS;
  803. event->queue = SNDRV_SEQ_QUEUE_DIRECT;
  804. } else
  805. #ifdef SUPPORT_BROADCAST
  806. if (event->queue == SNDRV_SEQ_ADDRESS_BROADCAST) {
  807. event->dest.client = SNDRV_SEQ_ADDRESS_BROADCAST;
  808. event->queue = SNDRV_SEQ_QUEUE_DIRECT;
  809. }
  810. #endif
  811. if (event->dest.client == SNDRV_SEQ_ADDRESS_SUBSCRIBERS) {
  812. /* check presence of source port */
  813. struct snd_seq_client_port *src_port = snd_seq_port_use_ptr(client, event->source.port);
  814. if (src_port == NULL)
  815. return -EINVAL;
  816. snd_seq_port_unlock(src_port);
  817. }
  818. /* direct event processing without enqueued */
  819. if (snd_seq_ev_is_direct(event)) {
  820. if (event->type == SNDRV_SEQ_EVENT_NOTE)
  821. return -EINVAL; /* this event must be enqueued! */
  822. return snd_seq_deliver_event(client, event, atomic, hop);
  823. }
  824. /* Not direct, normal queuing */
  825. if (snd_seq_queue_is_used(event->queue, client->number) <= 0)
  826. return -EINVAL; /* invalid queue */
  827. if (! snd_seq_write_pool_allocated(client))
  828. return -ENXIO; /* queue is not allocated */
  829. /* allocate an event cell */
  830. err = snd_seq_event_dup(client->pool, event, &cell, !blocking || atomic, file);
  831. if (err < 0)
  832. return err;
  833. /* we got a cell. enqueue it. */
  834. if ((err = snd_seq_enqueue_event(cell, atomic, hop)) < 0) {
  835. snd_seq_cell_free(cell);
  836. return err;
  837. }
  838. return 0;
  839. }
  840. /*
  841. * check validity of event type and data length.
  842. * return non-zero if invalid.
  843. */
  844. static int check_event_type_and_length(struct snd_seq_event *ev)
  845. {
  846. switch (snd_seq_ev_length_type(ev)) {
  847. case SNDRV_SEQ_EVENT_LENGTH_FIXED:
  848. if (snd_seq_ev_is_variable_type(ev))
  849. return -EINVAL;
  850. break;
  851. case SNDRV_SEQ_EVENT_LENGTH_VARIABLE:
  852. if (! snd_seq_ev_is_variable_type(ev) ||
  853. (ev->data.ext.len & ~SNDRV_SEQ_EXT_MASK) >= SNDRV_SEQ_MAX_EVENT_LEN)
  854. return -EINVAL;
  855. break;
  856. case SNDRV_SEQ_EVENT_LENGTH_VARUSR:
  857. if (! snd_seq_ev_is_direct(ev))
  858. return -EINVAL;
  859. break;
  860. }
  861. return 0;
  862. }
  863. /* handle write() */
  864. /* possible error values:
  865. * -ENXIO invalid client or file open mode
  866. * -ENOMEM malloc failed
  867. * -EFAULT seg. fault during copy from user space
  868. * -EINVAL invalid event
  869. * -EAGAIN no space in output pool
  870. * -EINTR interrupts while sleep
  871. * -EMLINK too many hops
  872. * others depends on return value from driver callback
  873. */
  874. static ssize_t snd_seq_write(struct file *file, const char __user *buf,
  875. size_t count, loff_t *offset)
  876. {
  877. struct snd_seq_client *client = file->private_data;
  878. int written = 0, len;
  879. int err = -EINVAL;
  880. struct snd_seq_event event;
  881. if (!(snd_seq_file_flags(file) & SNDRV_SEQ_LFLG_OUTPUT))
  882. return -ENXIO;
  883. /* check client structures are in place */
  884. if (snd_BUG_ON(!client))
  885. return -ENXIO;
  886. if (!client->accept_output || client->pool == NULL)
  887. return -ENXIO;
  888. /* allocate the pool now if the pool is not allocated yet */
  889. if (client->pool->size > 0 && !snd_seq_write_pool_allocated(client)) {
  890. if (snd_seq_pool_init(client->pool) < 0)
  891. return -ENOMEM;
  892. }
  893. /* only process whole events */
  894. while (count >= sizeof(struct snd_seq_event)) {
  895. /* Read in the event header from the user */
  896. len = sizeof(event);
  897. if (copy_from_user(&event, buf, len)) {
  898. err = -EFAULT;
  899. break;
  900. }
  901. event.source.client = client->number; /* fill in client number */
  902. /* Check for extension data length */
  903. if (check_event_type_and_length(&event)) {
  904. err = -EINVAL;
  905. break;
  906. }
  907. /* check for special events */
  908. if (event.type == SNDRV_SEQ_EVENT_NONE)
  909. goto __skip_event;
  910. else if (snd_seq_ev_is_reserved(&event)) {
  911. err = -EINVAL;
  912. break;
  913. }
  914. if (snd_seq_ev_is_variable(&event)) {
  915. int extlen = event.data.ext.len & ~SNDRV_SEQ_EXT_MASK;
  916. if ((size_t)(extlen + len) > count) {
  917. /* back out, will get an error this time or next */
  918. err = -EINVAL;
  919. break;
  920. }
  921. /* set user space pointer */
  922. event.data.ext.len = extlen | SNDRV_SEQ_EXT_USRPTR;
  923. event.data.ext.ptr = (char __force *)buf
  924. + sizeof(struct snd_seq_event);
  925. len += extlen; /* increment data length */
  926. } else {
  927. #ifdef CONFIG_COMPAT
  928. if (client->convert32 && snd_seq_ev_is_varusr(&event)) {
  929. void *ptr = (void __force *)compat_ptr(event.data.raw32.d[1]);
  930. event.data.ext.ptr = ptr;
  931. }
  932. #endif
  933. }
  934. /* ok, enqueue it */
  935. err = snd_seq_client_enqueue_event(client, &event, file,
  936. !(file->f_flags & O_NONBLOCK),
  937. 0, 0);
  938. if (err < 0)
  939. break;
  940. __skip_event:
  941. /* Update pointers and counts */
  942. count -= len;
  943. buf += len;
  944. written += len;
  945. }
  946. return written ? written : err;
  947. }
  948. /*
  949. * handle polling
  950. */
  951. static unsigned int snd_seq_poll(struct file *file, poll_table * wait)
  952. {
  953. struct snd_seq_client *client = file->private_data;
  954. unsigned int mask = 0;
  955. /* check client structures are in place */
  956. if (snd_BUG_ON(!client))
  957. return -ENXIO;
  958. if ((snd_seq_file_flags(file) & SNDRV_SEQ_LFLG_INPUT) &&
  959. client->data.user.fifo) {
  960. /* check if data is available in the outqueue */
  961. if (snd_seq_fifo_poll_wait(client->data.user.fifo, file, wait))
  962. mask |= POLLIN | POLLRDNORM;
  963. }
  964. if (snd_seq_file_flags(file) & SNDRV_SEQ_LFLG_OUTPUT) {
  965. /* check if data is available in the pool */
  966. if (!snd_seq_write_pool_allocated(client) ||
  967. snd_seq_pool_poll_wait(client->pool, file, wait))
  968. mask |= POLLOUT | POLLWRNORM;
  969. }
  970. return mask;
  971. }
  972. /*-----------------------------------------------------*/
  973. static int snd_seq_ioctl_pversion(struct snd_seq_client *client, void *arg)
  974. {
  975. int *pversion = arg;
  976. *pversion = SNDRV_SEQ_VERSION;
  977. return 0;
  978. }
  979. static int snd_seq_ioctl_client_id(struct snd_seq_client *client, void *arg)
  980. {
  981. int *client_id = arg;
  982. *client_id = client->number;
  983. return 0;
  984. }
  985. /* SYSTEM_INFO ioctl() */
  986. static int snd_seq_ioctl_system_info(struct snd_seq_client *client, void *arg)
  987. {
  988. struct snd_seq_system_info *info = arg;
  989. memset(info, 0, sizeof(*info));
  990. /* fill the info fields */
  991. info->queues = SNDRV_SEQ_MAX_QUEUES;
  992. info->clients = SNDRV_SEQ_MAX_CLIENTS;
  993. info->ports = SNDRV_SEQ_MAX_PORTS;
  994. info->channels = 256; /* fixed limit */
  995. info->cur_clients = client_usage.cur;
  996. info->cur_queues = snd_seq_queue_get_cur_queues();
  997. return 0;
  998. }
  999. /* RUNNING_MODE ioctl() */
  1000. static int snd_seq_ioctl_running_mode(struct snd_seq_client *client, void *arg)
  1001. {
  1002. struct snd_seq_running_info *info = arg;
  1003. struct snd_seq_client *cptr;
  1004. int err = 0;
  1005. /* requested client number */
  1006. cptr = snd_seq_client_use_ptr(info->client);
  1007. if (cptr == NULL)
  1008. return -ENOENT; /* don't change !!! */
  1009. #ifdef SNDRV_BIG_ENDIAN
  1010. if (!info->big_endian) {
  1011. err = -EINVAL;
  1012. goto __err;
  1013. }
  1014. #else
  1015. if (info->big_endian) {
  1016. err = -EINVAL;
  1017. goto __err;
  1018. }
  1019. #endif
  1020. if (info->cpu_mode > sizeof(long)) {
  1021. err = -EINVAL;
  1022. goto __err;
  1023. }
  1024. cptr->convert32 = (info->cpu_mode < sizeof(long));
  1025. __err:
  1026. snd_seq_client_unlock(cptr);
  1027. return err;
  1028. }
  1029. /* CLIENT_INFO ioctl() */
  1030. static void get_client_info(struct snd_seq_client *cptr,
  1031. struct snd_seq_client_info *info)
  1032. {
  1033. info->client = cptr->number;
  1034. /* fill the info fields */
  1035. info->type = cptr->type;
  1036. strcpy(info->name, cptr->name);
  1037. info->filter = cptr->filter;
  1038. info->event_lost = cptr->event_lost;
  1039. memcpy(info->event_filter, cptr->event_filter, 32);
  1040. info->num_ports = cptr->num_ports;
  1041. if (cptr->type == USER_CLIENT)
  1042. info->pid = pid_vnr(cptr->data.user.owner);
  1043. else
  1044. info->pid = -1;
  1045. if (cptr->type == KERNEL_CLIENT)
  1046. info->card = cptr->data.kernel.card ? cptr->data.kernel.card->number : -1;
  1047. else
  1048. info->card = -1;
  1049. memset(info->reserved, 0, sizeof(info->reserved));
  1050. }
  1051. static int snd_seq_ioctl_get_client_info(struct snd_seq_client *client,
  1052. void *arg)
  1053. {
  1054. struct snd_seq_client_info *client_info = arg;
  1055. struct snd_seq_client *cptr;
  1056. /* requested client number */
  1057. cptr = snd_seq_client_use_ptr(client_info->client);
  1058. if (cptr == NULL)
  1059. return -ENOENT; /* don't change !!! */
  1060. get_client_info(cptr, client_info);
  1061. snd_seq_client_unlock(cptr);
  1062. return 0;
  1063. }
  1064. /* CLIENT_INFO ioctl() */
  1065. static int snd_seq_ioctl_set_client_info(struct snd_seq_client *client,
  1066. void *arg)
  1067. {
  1068. struct snd_seq_client_info *client_info = arg;
  1069. /* it is not allowed to set the info fields for an another client */
  1070. if (client->number != client_info->client)
  1071. return -EPERM;
  1072. /* also client type must be set now */
  1073. if (client->type != client_info->type)
  1074. return -EINVAL;
  1075. /* fill the info fields */
  1076. if (client_info->name[0])
  1077. strlcpy(client->name, client_info->name, sizeof(client->name));
  1078. client->filter = client_info->filter;
  1079. client->event_lost = client_info->event_lost;
  1080. memcpy(client->event_filter, client_info->event_filter, 32);
  1081. return 0;
  1082. }
  1083. /*
  1084. * CREATE PORT ioctl()
  1085. */
  1086. static int snd_seq_ioctl_create_port(struct snd_seq_client *client, void *arg)
  1087. {
  1088. struct snd_seq_port_info *info = arg;
  1089. struct snd_seq_client_port *port;
  1090. struct snd_seq_port_callback *callback;
  1091. int port_idx;
  1092. /* it is not allowed to create the port for an another client */
  1093. if (info->addr.client != client->number)
  1094. return -EPERM;
  1095. port = snd_seq_create_port(client, (info->flags & SNDRV_SEQ_PORT_FLG_GIVEN_PORT) ? info->addr.port : -1);
  1096. if (port == NULL)
  1097. return -ENOMEM;
  1098. if (client->type == USER_CLIENT && info->kernel) {
  1099. port_idx = port->addr.port;
  1100. snd_seq_port_unlock(port);
  1101. snd_seq_delete_port(client, port_idx);
  1102. return -EINVAL;
  1103. }
  1104. if (client->type == KERNEL_CLIENT) {
  1105. if ((callback = info->kernel) != NULL) {
  1106. if (callback->owner)
  1107. port->owner = callback->owner;
  1108. port->private_data = callback->private_data;
  1109. port->private_free = callback->private_free;
  1110. port->event_input = callback->event_input;
  1111. port->c_src.open = callback->subscribe;
  1112. port->c_src.close = callback->unsubscribe;
  1113. port->c_dest.open = callback->use;
  1114. port->c_dest.close = callback->unuse;
  1115. }
  1116. }
  1117. info->addr = port->addr;
  1118. snd_seq_set_port_info(port, info);
  1119. snd_seq_system_client_ev_port_start(port->addr.client, port->addr.port);
  1120. snd_seq_port_unlock(port);
  1121. return 0;
  1122. }
  1123. /*
  1124. * DELETE PORT ioctl()
  1125. */
  1126. static int snd_seq_ioctl_delete_port(struct snd_seq_client *client, void *arg)
  1127. {
  1128. struct snd_seq_port_info *info = arg;
  1129. int err;
  1130. /* it is not allowed to remove the port for an another client */
  1131. if (info->addr.client != client->number)
  1132. return -EPERM;
  1133. err = snd_seq_delete_port(client, info->addr.port);
  1134. if (err >= 0)
  1135. snd_seq_system_client_ev_port_exit(client->number, info->addr.port);
  1136. return err;
  1137. }
  1138. /*
  1139. * GET_PORT_INFO ioctl() (on any client)
  1140. */
  1141. static int snd_seq_ioctl_get_port_info(struct snd_seq_client *client, void *arg)
  1142. {
  1143. struct snd_seq_port_info *info = arg;
  1144. struct snd_seq_client *cptr;
  1145. struct snd_seq_client_port *port;
  1146. cptr = snd_seq_client_use_ptr(info->addr.client);
  1147. if (cptr == NULL)
  1148. return -ENXIO;
  1149. port = snd_seq_port_use_ptr(cptr, info->addr.port);
  1150. if (port == NULL) {
  1151. snd_seq_client_unlock(cptr);
  1152. return -ENOENT; /* don't change */
  1153. }
  1154. /* get port info */
  1155. snd_seq_get_port_info(port, info);
  1156. snd_seq_port_unlock(port);
  1157. snd_seq_client_unlock(cptr);
  1158. return 0;
  1159. }
  1160. /*
  1161. * SET_PORT_INFO ioctl() (only ports on this/own client)
  1162. */
  1163. static int snd_seq_ioctl_set_port_info(struct snd_seq_client *client, void *arg)
  1164. {
  1165. struct snd_seq_port_info *info = arg;
  1166. struct snd_seq_client_port *port;
  1167. if (info->addr.client != client->number) /* only set our own ports ! */
  1168. return -EPERM;
  1169. port = snd_seq_port_use_ptr(client, info->addr.port);
  1170. if (port) {
  1171. snd_seq_set_port_info(port, info);
  1172. snd_seq_port_unlock(port);
  1173. }
  1174. return 0;
  1175. }
  1176. /*
  1177. * port subscription (connection)
  1178. */
  1179. #define PERM_RD (SNDRV_SEQ_PORT_CAP_READ|SNDRV_SEQ_PORT_CAP_SUBS_READ)
  1180. #define PERM_WR (SNDRV_SEQ_PORT_CAP_WRITE|SNDRV_SEQ_PORT_CAP_SUBS_WRITE)
  1181. static int check_subscription_permission(struct snd_seq_client *client,
  1182. struct snd_seq_client_port *sport,
  1183. struct snd_seq_client_port *dport,
  1184. struct snd_seq_port_subscribe *subs)
  1185. {
  1186. if (client->number != subs->sender.client &&
  1187. client->number != subs->dest.client) {
  1188. /* connection by third client - check export permission */
  1189. if (check_port_perm(sport, SNDRV_SEQ_PORT_CAP_NO_EXPORT))
  1190. return -EPERM;
  1191. if (check_port_perm(dport, SNDRV_SEQ_PORT_CAP_NO_EXPORT))
  1192. return -EPERM;
  1193. }
  1194. /* check read permission */
  1195. /* if sender or receiver is the subscribing client itself,
  1196. * no permission check is necessary
  1197. */
  1198. if (client->number != subs->sender.client) {
  1199. if (! check_port_perm(sport, PERM_RD))
  1200. return -EPERM;
  1201. }
  1202. /* check write permission */
  1203. if (client->number != subs->dest.client) {
  1204. if (! check_port_perm(dport, PERM_WR))
  1205. return -EPERM;
  1206. }
  1207. return 0;
  1208. }
  1209. /*
  1210. * send an subscription notify event to user client:
  1211. * client must be user client.
  1212. */
  1213. int snd_seq_client_notify_subscription(int client, int port,
  1214. struct snd_seq_port_subscribe *info,
  1215. int evtype)
  1216. {
  1217. struct snd_seq_event event;
  1218. memset(&event, 0, sizeof(event));
  1219. event.type = evtype;
  1220. event.data.connect.dest = info->dest;
  1221. event.data.connect.sender = info->sender;
  1222. return snd_seq_system_notify(client, port, &event); /* non-atomic */
  1223. }
  1224. /*
  1225. * add to port's subscription list IOCTL interface
  1226. */
  1227. static int snd_seq_ioctl_subscribe_port(struct snd_seq_client *client,
  1228. void *arg)
  1229. {
  1230. struct snd_seq_port_subscribe *subs = arg;
  1231. int result = -EINVAL;
  1232. struct snd_seq_client *receiver = NULL, *sender = NULL;
  1233. struct snd_seq_client_port *sport = NULL, *dport = NULL;
  1234. if ((receiver = snd_seq_client_use_ptr(subs->dest.client)) == NULL)
  1235. goto __end;
  1236. if ((sender = snd_seq_client_use_ptr(subs->sender.client)) == NULL)
  1237. goto __end;
  1238. if ((sport = snd_seq_port_use_ptr(sender, subs->sender.port)) == NULL)
  1239. goto __end;
  1240. if ((dport = snd_seq_port_use_ptr(receiver, subs->dest.port)) == NULL)
  1241. goto __end;
  1242. result = check_subscription_permission(client, sport, dport, subs);
  1243. if (result < 0)
  1244. goto __end;
  1245. /* connect them */
  1246. result = snd_seq_port_connect(client, sender, sport, receiver, dport, subs);
  1247. if (! result) /* broadcast announce */
  1248. snd_seq_client_notify_subscription(SNDRV_SEQ_ADDRESS_SUBSCRIBERS, 0,
  1249. subs, SNDRV_SEQ_EVENT_PORT_SUBSCRIBED);
  1250. __end:
  1251. if (sport)
  1252. snd_seq_port_unlock(sport);
  1253. if (dport)
  1254. snd_seq_port_unlock(dport);
  1255. if (sender)
  1256. snd_seq_client_unlock(sender);
  1257. if (receiver)
  1258. snd_seq_client_unlock(receiver);
  1259. return result;
  1260. }
  1261. /*
  1262. * remove from port's subscription list
  1263. */
  1264. static int snd_seq_ioctl_unsubscribe_port(struct snd_seq_client *client,
  1265. void *arg)
  1266. {
  1267. struct snd_seq_port_subscribe *subs = arg;
  1268. int result = -ENXIO;
  1269. struct snd_seq_client *receiver = NULL, *sender = NULL;
  1270. struct snd_seq_client_port *sport = NULL, *dport = NULL;
  1271. if ((receiver = snd_seq_client_use_ptr(subs->dest.client)) == NULL)
  1272. goto __end;
  1273. if ((sender = snd_seq_client_use_ptr(subs->sender.client)) == NULL)
  1274. goto __end;
  1275. if ((sport = snd_seq_port_use_ptr(sender, subs->sender.port)) == NULL)
  1276. goto __end;
  1277. if ((dport = snd_seq_port_use_ptr(receiver, subs->dest.port)) == NULL)
  1278. goto __end;
  1279. result = check_subscription_permission(client, sport, dport, subs);
  1280. if (result < 0)
  1281. goto __end;
  1282. result = snd_seq_port_disconnect(client, sender, sport, receiver, dport, subs);
  1283. if (! result) /* broadcast announce */
  1284. snd_seq_client_notify_subscription(SNDRV_SEQ_ADDRESS_SUBSCRIBERS, 0,
  1285. subs, SNDRV_SEQ_EVENT_PORT_UNSUBSCRIBED);
  1286. __end:
  1287. if (sport)
  1288. snd_seq_port_unlock(sport);
  1289. if (dport)
  1290. snd_seq_port_unlock(dport);
  1291. if (sender)
  1292. snd_seq_client_unlock(sender);
  1293. if (receiver)
  1294. snd_seq_client_unlock(receiver);
  1295. return result;
  1296. }
  1297. /* CREATE_QUEUE ioctl() */
  1298. static int snd_seq_ioctl_create_queue(struct snd_seq_client *client, void *arg)
  1299. {
  1300. struct snd_seq_queue_info *info = arg;
  1301. struct snd_seq_queue *q;
  1302. q = snd_seq_queue_alloc(client->number, info->locked, info->flags);
  1303. if (IS_ERR(q))
  1304. return PTR_ERR(q);
  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. snd_use_lock_free(&q->use_lock);
  1313. return 0;
  1314. }
  1315. /* DELETE_QUEUE ioctl() */
  1316. static int snd_seq_ioctl_delete_queue(struct snd_seq_client *client, void *arg)
  1317. {
  1318. struct snd_seq_queue_info *info = arg;
  1319. return snd_seq_queue_delete(client->number, info->queue);
  1320. }
  1321. /* GET_QUEUE_INFO ioctl() */
  1322. static int snd_seq_ioctl_get_queue_info(struct snd_seq_client *client,
  1323. void *arg)
  1324. {
  1325. struct snd_seq_queue_info *info = arg;
  1326. struct snd_seq_queue *q;
  1327. q = queueptr(info->queue);
  1328. if (q == NULL)
  1329. return -EINVAL;
  1330. memset(info, 0, sizeof(*info));
  1331. info->queue = q->queue;
  1332. info->owner = q->owner;
  1333. info->locked = q->locked;
  1334. strlcpy(info->name, q->name, sizeof(info->name));
  1335. queuefree(q);
  1336. return 0;
  1337. }
  1338. /* SET_QUEUE_INFO ioctl() */
  1339. static int snd_seq_ioctl_set_queue_info(struct snd_seq_client *client,
  1340. void *arg)
  1341. {
  1342. struct snd_seq_queue_info *info = arg;
  1343. struct snd_seq_queue *q;
  1344. if (info->owner != client->number)
  1345. return -EINVAL;
  1346. /* change owner/locked permission */
  1347. if (snd_seq_queue_check_access(info->queue, client->number)) {
  1348. if (snd_seq_queue_set_owner(info->queue, client->number, info->locked) < 0)
  1349. return -EPERM;
  1350. if (info->locked)
  1351. snd_seq_queue_use(info->queue, client->number, 1);
  1352. } else {
  1353. return -EPERM;
  1354. }
  1355. q = queueptr(info->queue);
  1356. if (! q)
  1357. return -EINVAL;
  1358. if (q->owner != client->number) {
  1359. queuefree(q);
  1360. return -EPERM;
  1361. }
  1362. strlcpy(q->name, info->name, sizeof(q->name));
  1363. queuefree(q);
  1364. return 0;
  1365. }
  1366. /* GET_NAMED_QUEUE ioctl() */
  1367. static int snd_seq_ioctl_get_named_queue(struct snd_seq_client *client,
  1368. void *arg)
  1369. {
  1370. struct snd_seq_queue_info *info = arg;
  1371. struct snd_seq_queue *q;
  1372. q = snd_seq_queue_find_name(info->name);
  1373. if (q == NULL)
  1374. return -EINVAL;
  1375. info->queue = q->queue;
  1376. info->owner = q->owner;
  1377. info->locked = q->locked;
  1378. queuefree(q);
  1379. return 0;
  1380. }
  1381. /* GET_QUEUE_STATUS ioctl() */
  1382. static int snd_seq_ioctl_get_queue_status(struct snd_seq_client *client,
  1383. void *arg)
  1384. {
  1385. struct snd_seq_queue_status *status = arg;
  1386. struct snd_seq_queue *queue;
  1387. struct snd_seq_timer *tmr;
  1388. queue = queueptr(status->queue);
  1389. if (queue == NULL)
  1390. return -EINVAL;
  1391. memset(status, 0, sizeof(*status));
  1392. status->queue = queue->queue;
  1393. tmr = queue->timer;
  1394. status->events = queue->tickq->cells + queue->timeq->cells;
  1395. status->time = snd_seq_timer_get_cur_time(tmr);
  1396. status->tick = snd_seq_timer_get_cur_tick(tmr);
  1397. status->running = tmr->running;
  1398. status->flags = queue->flags;
  1399. queuefree(queue);
  1400. return 0;
  1401. }
  1402. /* GET_QUEUE_TEMPO ioctl() */
  1403. static int snd_seq_ioctl_get_queue_tempo(struct snd_seq_client *client,
  1404. void *arg)
  1405. {
  1406. struct snd_seq_queue_tempo *tempo = arg;
  1407. struct snd_seq_queue *queue;
  1408. struct snd_seq_timer *tmr;
  1409. queue = queueptr(tempo->queue);
  1410. if (queue == NULL)
  1411. return -EINVAL;
  1412. memset(tempo, 0, sizeof(*tempo));
  1413. tempo->queue = queue->queue;
  1414. tmr = queue->timer;
  1415. tempo->tempo = tmr->tempo;
  1416. tempo->ppq = tmr->ppq;
  1417. tempo->skew_value = tmr->skew;
  1418. tempo->skew_base = tmr->skew_base;
  1419. queuefree(queue);
  1420. return 0;
  1421. }
  1422. /* SET_QUEUE_TEMPO ioctl() */
  1423. int snd_seq_set_queue_tempo(int client, struct snd_seq_queue_tempo *tempo)
  1424. {
  1425. if (!snd_seq_queue_check_access(tempo->queue, client))
  1426. return -EPERM;
  1427. return snd_seq_queue_timer_set_tempo(tempo->queue, client, tempo);
  1428. }
  1429. EXPORT_SYMBOL(snd_seq_set_queue_tempo);
  1430. static int snd_seq_ioctl_set_queue_tempo(struct snd_seq_client *client,
  1431. void *arg)
  1432. {
  1433. struct snd_seq_queue_tempo *tempo = arg;
  1434. int result;
  1435. result = snd_seq_set_queue_tempo(client->number, tempo);
  1436. return result < 0 ? result : 0;
  1437. }
  1438. /* GET_QUEUE_TIMER ioctl() */
  1439. static int snd_seq_ioctl_get_queue_timer(struct snd_seq_client *client,
  1440. void *arg)
  1441. {
  1442. struct snd_seq_queue_timer *timer = arg;
  1443. struct snd_seq_queue *queue;
  1444. struct snd_seq_timer *tmr;
  1445. queue = queueptr(timer->queue);
  1446. if (queue == NULL)
  1447. return -EINVAL;
  1448. if (mutex_lock_interruptible(&queue->timer_mutex)) {
  1449. queuefree(queue);
  1450. return -ERESTARTSYS;
  1451. }
  1452. tmr = queue->timer;
  1453. memset(timer, 0, sizeof(*timer));
  1454. timer->queue = queue->queue;
  1455. timer->type = tmr->type;
  1456. if (tmr->type == SNDRV_SEQ_TIMER_ALSA) {
  1457. timer->u.alsa.id = tmr->alsa_id;
  1458. timer->u.alsa.resolution = tmr->preferred_resolution;
  1459. }
  1460. mutex_unlock(&queue->timer_mutex);
  1461. queuefree(queue);
  1462. return 0;
  1463. }
  1464. /* SET_QUEUE_TIMER ioctl() */
  1465. static int snd_seq_ioctl_set_queue_timer(struct snd_seq_client *client,
  1466. void *arg)
  1467. {
  1468. struct snd_seq_queue_timer *timer = arg;
  1469. int result = 0;
  1470. if (timer->type != SNDRV_SEQ_TIMER_ALSA)
  1471. return -EINVAL;
  1472. if (snd_seq_queue_check_access(timer->queue, client->number)) {
  1473. struct snd_seq_queue *q;
  1474. struct snd_seq_timer *tmr;
  1475. q = queueptr(timer->queue);
  1476. if (q == NULL)
  1477. return -ENXIO;
  1478. if (mutex_lock_interruptible(&q->timer_mutex)) {
  1479. queuefree(q);
  1480. return -ERESTARTSYS;
  1481. }
  1482. tmr = q->timer;
  1483. snd_seq_queue_timer_close(timer->queue);
  1484. tmr->type = timer->type;
  1485. if (tmr->type == SNDRV_SEQ_TIMER_ALSA) {
  1486. tmr->alsa_id = timer->u.alsa.id;
  1487. tmr->preferred_resolution = timer->u.alsa.resolution;
  1488. }
  1489. result = snd_seq_queue_timer_open(timer->queue);
  1490. mutex_unlock(&q->timer_mutex);
  1491. queuefree(q);
  1492. } else {
  1493. return -EPERM;
  1494. }
  1495. return result;
  1496. }
  1497. /* GET_QUEUE_CLIENT ioctl() */
  1498. static int snd_seq_ioctl_get_queue_client(struct snd_seq_client *client,
  1499. void *arg)
  1500. {
  1501. struct snd_seq_queue_client *info = arg;
  1502. int used;
  1503. used = snd_seq_queue_is_used(info->queue, client->number);
  1504. if (used < 0)
  1505. return -EINVAL;
  1506. info->used = used;
  1507. info->client = client->number;
  1508. return 0;
  1509. }
  1510. /* SET_QUEUE_CLIENT ioctl() */
  1511. static int snd_seq_ioctl_set_queue_client(struct snd_seq_client *client,
  1512. void *arg)
  1513. {
  1514. struct snd_seq_queue_client *info = arg;
  1515. int err;
  1516. if (info->used >= 0) {
  1517. err = snd_seq_queue_use(info->queue, client->number, info->used);
  1518. if (err < 0)
  1519. return err;
  1520. }
  1521. return snd_seq_ioctl_get_queue_client(client, arg);
  1522. }
  1523. /* GET_CLIENT_POOL ioctl() */
  1524. static int snd_seq_ioctl_get_client_pool(struct snd_seq_client *client,
  1525. void *arg)
  1526. {
  1527. struct snd_seq_client_pool *info = arg;
  1528. struct snd_seq_client *cptr;
  1529. cptr = snd_seq_client_use_ptr(info->client);
  1530. if (cptr == NULL)
  1531. return -ENOENT;
  1532. memset(info, 0, sizeof(*info));
  1533. info->client = cptr->number;
  1534. info->output_pool = cptr->pool->size;
  1535. info->output_room = cptr->pool->room;
  1536. info->output_free = info->output_pool;
  1537. info->output_free = snd_seq_unused_cells(cptr->pool);
  1538. if (cptr->type == USER_CLIENT) {
  1539. info->input_pool = cptr->data.user.fifo_pool_size;
  1540. info->input_free = info->input_pool;
  1541. if (cptr->data.user.fifo)
  1542. info->input_free = snd_seq_unused_cells(cptr->data.user.fifo->pool);
  1543. } else {
  1544. info->input_pool = 0;
  1545. info->input_free = 0;
  1546. }
  1547. snd_seq_client_unlock(cptr);
  1548. return 0;
  1549. }
  1550. /* SET_CLIENT_POOL ioctl() */
  1551. static int snd_seq_ioctl_set_client_pool(struct snd_seq_client *client,
  1552. void *arg)
  1553. {
  1554. struct snd_seq_client_pool *info = arg;
  1555. int rc;
  1556. if (client->number != info->client)
  1557. return -EINVAL; /* can't change other clients */
  1558. if (info->output_pool >= 1 && info->output_pool <= SNDRV_SEQ_MAX_EVENTS &&
  1559. (! snd_seq_write_pool_allocated(client) ||
  1560. info->output_pool != client->pool->size)) {
  1561. if (snd_seq_write_pool_allocated(client)) {
  1562. /* remove all existing cells */
  1563. snd_seq_pool_mark_closing(client->pool);
  1564. snd_seq_queue_client_leave_cells(client->number);
  1565. snd_seq_pool_done(client->pool);
  1566. }
  1567. client->pool->size = info->output_pool;
  1568. rc = snd_seq_pool_init(client->pool);
  1569. if (rc < 0)
  1570. return rc;
  1571. }
  1572. if (client->type == USER_CLIENT && client->data.user.fifo != NULL &&
  1573. info->input_pool >= 1 &&
  1574. info->input_pool <= SNDRV_SEQ_MAX_CLIENT_EVENTS &&
  1575. info->input_pool != client->data.user.fifo_pool_size) {
  1576. /* change pool size */
  1577. rc = snd_seq_fifo_resize(client->data.user.fifo, info->input_pool);
  1578. if (rc < 0)
  1579. return rc;
  1580. client->data.user.fifo_pool_size = info->input_pool;
  1581. }
  1582. if (info->output_room >= 1 &&
  1583. info->output_room <= client->pool->size) {
  1584. client->pool->room = info->output_room;
  1585. }
  1586. return snd_seq_ioctl_get_client_pool(client, arg);
  1587. }
  1588. /* REMOVE_EVENTS ioctl() */
  1589. static int snd_seq_ioctl_remove_events(struct snd_seq_client *client,
  1590. void *arg)
  1591. {
  1592. struct snd_seq_remove_events *info = arg;
  1593. /*
  1594. * Input mostly not implemented XXX.
  1595. */
  1596. if (info->remove_mode & SNDRV_SEQ_REMOVE_INPUT) {
  1597. /*
  1598. * No restrictions so for a user client we can clear
  1599. * the whole fifo
  1600. */
  1601. if (client->type == USER_CLIENT && client->data.user.fifo)
  1602. snd_seq_fifo_clear(client->data.user.fifo);
  1603. }
  1604. if (info->remove_mode & SNDRV_SEQ_REMOVE_OUTPUT)
  1605. snd_seq_queue_remove_cells(client->number, info);
  1606. return 0;
  1607. }
  1608. /*
  1609. * get subscription info
  1610. */
  1611. static int snd_seq_ioctl_get_subscription(struct snd_seq_client *client,
  1612. void *arg)
  1613. {
  1614. struct snd_seq_port_subscribe *subs = arg;
  1615. int result;
  1616. struct snd_seq_client *sender = NULL;
  1617. struct snd_seq_client_port *sport = NULL;
  1618. struct snd_seq_subscribers *p;
  1619. result = -EINVAL;
  1620. if ((sender = snd_seq_client_use_ptr(subs->sender.client)) == NULL)
  1621. goto __end;
  1622. if ((sport = snd_seq_port_use_ptr(sender, subs->sender.port)) == NULL)
  1623. goto __end;
  1624. p = snd_seq_port_get_subscription(&sport->c_src, &subs->dest);
  1625. if (p) {
  1626. result = 0;
  1627. *subs = p->info;
  1628. } else
  1629. result = -ENOENT;
  1630. __end:
  1631. if (sport)
  1632. snd_seq_port_unlock(sport);
  1633. if (sender)
  1634. snd_seq_client_unlock(sender);
  1635. return result;
  1636. }
  1637. /*
  1638. * get subscription info - check only its presence
  1639. */
  1640. static int snd_seq_ioctl_query_subs(struct snd_seq_client *client, void *arg)
  1641. {
  1642. struct snd_seq_query_subs *subs = arg;
  1643. int result = -ENXIO;
  1644. struct snd_seq_client *cptr = NULL;
  1645. struct snd_seq_client_port *port = NULL;
  1646. struct snd_seq_port_subs_info *group;
  1647. struct list_head *p;
  1648. int i;
  1649. if ((cptr = snd_seq_client_use_ptr(subs->root.client)) == NULL)
  1650. goto __end;
  1651. if ((port = snd_seq_port_use_ptr(cptr, subs->root.port)) == NULL)
  1652. goto __end;
  1653. switch (subs->type) {
  1654. case SNDRV_SEQ_QUERY_SUBS_READ:
  1655. group = &port->c_src;
  1656. break;
  1657. case SNDRV_SEQ_QUERY_SUBS_WRITE:
  1658. group = &port->c_dest;
  1659. break;
  1660. default:
  1661. goto __end;
  1662. }
  1663. down_read(&group->list_mutex);
  1664. /* search for the subscriber */
  1665. subs->num_subs = group->count;
  1666. i = 0;
  1667. result = -ENOENT;
  1668. list_for_each(p, &group->list_head) {
  1669. if (i++ == subs->index) {
  1670. /* found! */
  1671. struct snd_seq_subscribers *s;
  1672. if (subs->type == SNDRV_SEQ_QUERY_SUBS_READ) {
  1673. s = list_entry(p, struct snd_seq_subscribers, src_list);
  1674. subs->addr = s->info.dest;
  1675. } else {
  1676. s = list_entry(p, struct snd_seq_subscribers, dest_list);
  1677. subs->addr = s->info.sender;
  1678. }
  1679. subs->flags = s->info.flags;
  1680. subs->queue = s->info.queue;
  1681. result = 0;
  1682. break;
  1683. }
  1684. }
  1685. up_read(&group->list_mutex);
  1686. __end:
  1687. if (port)
  1688. snd_seq_port_unlock(port);
  1689. if (cptr)
  1690. snd_seq_client_unlock(cptr);
  1691. return result;
  1692. }
  1693. /*
  1694. * query next client
  1695. */
  1696. static int snd_seq_ioctl_query_next_client(struct snd_seq_client *client,
  1697. void *arg)
  1698. {
  1699. struct snd_seq_client_info *info = arg;
  1700. struct snd_seq_client *cptr = NULL;
  1701. /* search for next client */
  1702. info->client++;
  1703. if (info->client < 0)
  1704. info->client = 0;
  1705. for (; info->client < SNDRV_SEQ_MAX_CLIENTS; info->client++) {
  1706. cptr = snd_seq_client_use_ptr(info->client);
  1707. if (cptr)
  1708. break; /* found */
  1709. }
  1710. if (cptr == NULL)
  1711. return -ENOENT;
  1712. get_client_info(cptr, info);
  1713. snd_seq_client_unlock(cptr);
  1714. return 0;
  1715. }
  1716. /*
  1717. * query next port
  1718. */
  1719. static int snd_seq_ioctl_query_next_port(struct snd_seq_client *client,
  1720. void *arg)
  1721. {
  1722. struct snd_seq_port_info *info = arg;
  1723. struct snd_seq_client *cptr;
  1724. struct snd_seq_client_port *port = NULL;
  1725. cptr = snd_seq_client_use_ptr(info->addr.client);
  1726. if (cptr == NULL)
  1727. return -ENXIO;
  1728. /* search for next port */
  1729. info->addr.port++;
  1730. port = snd_seq_port_query_nearest(cptr, info);
  1731. if (port == NULL) {
  1732. snd_seq_client_unlock(cptr);
  1733. return -ENOENT;
  1734. }
  1735. /* get port info */
  1736. info->addr = port->addr;
  1737. snd_seq_get_port_info(port, info);
  1738. snd_seq_port_unlock(port);
  1739. snd_seq_client_unlock(cptr);
  1740. return 0;
  1741. }
  1742. /* -------------------------------------------------------- */
  1743. static const struct ioctl_handler {
  1744. unsigned int cmd;
  1745. int (*func)(struct snd_seq_client *client, void *arg);
  1746. } ioctl_handlers[] = {
  1747. { SNDRV_SEQ_IOCTL_PVERSION, snd_seq_ioctl_pversion },
  1748. { SNDRV_SEQ_IOCTL_CLIENT_ID, snd_seq_ioctl_client_id },
  1749. { SNDRV_SEQ_IOCTL_SYSTEM_INFO, snd_seq_ioctl_system_info },
  1750. { SNDRV_SEQ_IOCTL_RUNNING_MODE, snd_seq_ioctl_running_mode },
  1751. { SNDRV_SEQ_IOCTL_GET_CLIENT_INFO, snd_seq_ioctl_get_client_info },
  1752. { SNDRV_SEQ_IOCTL_SET_CLIENT_INFO, snd_seq_ioctl_set_client_info },
  1753. { SNDRV_SEQ_IOCTL_CREATE_PORT, snd_seq_ioctl_create_port },
  1754. { SNDRV_SEQ_IOCTL_DELETE_PORT, snd_seq_ioctl_delete_port },
  1755. { SNDRV_SEQ_IOCTL_GET_PORT_INFO, snd_seq_ioctl_get_port_info },
  1756. { SNDRV_SEQ_IOCTL_SET_PORT_INFO, snd_seq_ioctl_set_port_info },
  1757. { SNDRV_SEQ_IOCTL_SUBSCRIBE_PORT, snd_seq_ioctl_subscribe_port },
  1758. { SNDRV_SEQ_IOCTL_UNSUBSCRIBE_PORT, snd_seq_ioctl_unsubscribe_port },
  1759. { SNDRV_SEQ_IOCTL_CREATE_QUEUE, snd_seq_ioctl_create_queue },
  1760. { SNDRV_SEQ_IOCTL_DELETE_QUEUE, snd_seq_ioctl_delete_queue },
  1761. { SNDRV_SEQ_IOCTL_GET_QUEUE_INFO, snd_seq_ioctl_get_queue_info },
  1762. { SNDRV_SEQ_IOCTL_SET_QUEUE_INFO, snd_seq_ioctl_set_queue_info },
  1763. { SNDRV_SEQ_IOCTL_GET_NAMED_QUEUE, snd_seq_ioctl_get_named_queue },
  1764. { SNDRV_SEQ_IOCTL_GET_QUEUE_STATUS, snd_seq_ioctl_get_queue_status },
  1765. { SNDRV_SEQ_IOCTL_GET_QUEUE_TEMPO, snd_seq_ioctl_get_queue_tempo },
  1766. { SNDRV_SEQ_IOCTL_SET_QUEUE_TEMPO, snd_seq_ioctl_set_queue_tempo },
  1767. { SNDRV_SEQ_IOCTL_GET_QUEUE_TIMER, snd_seq_ioctl_get_queue_timer },
  1768. { SNDRV_SEQ_IOCTL_SET_QUEUE_TIMER, snd_seq_ioctl_set_queue_timer },
  1769. { SNDRV_SEQ_IOCTL_GET_QUEUE_CLIENT, snd_seq_ioctl_get_queue_client },
  1770. { SNDRV_SEQ_IOCTL_SET_QUEUE_CLIENT, snd_seq_ioctl_set_queue_client },
  1771. { SNDRV_SEQ_IOCTL_GET_CLIENT_POOL, snd_seq_ioctl_get_client_pool },
  1772. { SNDRV_SEQ_IOCTL_SET_CLIENT_POOL, snd_seq_ioctl_set_client_pool },
  1773. { SNDRV_SEQ_IOCTL_GET_SUBSCRIPTION, snd_seq_ioctl_get_subscription },
  1774. { SNDRV_SEQ_IOCTL_QUERY_NEXT_CLIENT, snd_seq_ioctl_query_next_client },
  1775. { SNDRV_SEQ_IOCTL_QUERY_NEXT_PORT, snd_seq_ioctl_query_next_port },
  1776. { SNDRV_SEQ_IOCTL_REMOVE_EVENTS, snd_seq_ioctl_remove_events },
  1777. { SNDRV_SEQ_IOCTL_QUERY_SUBS, snd_seq_ioctl_query_subs },
  1778. { 0, NULL },
  1779. };
  1780. static long snd_seq_ioctl(struct file *file, unsigned int cmd,
  1781. unsigned long arg)
  1782. {
  1783. struct snd_seq_client *client = file->private_data;
  1784. /* To use kernel stack for ioctl data. */
  1785. union {
  1786. int pversion;
  1787. int client_id;
  1788. struct snd_seq_system_info system_info;
  1789. struct snd_seq_running_info running_info;
  1790. struct snd_seq_client_info client_info;
  1791. struct snd_seq_port_info port_info;
  1792. struct snd_seq_port_subscribe port_subscribe;
  1793. struct snd_seq_queue_info queue_info;
  1794. struct snd_seq_queue_status queue_status;
  1795. struct snd_seq_queue_tempo tempo;
  1796. struct snd_seq_queue_timer queue_timer;
  1797. struct snd_seq_queue_client queue_client;
  1798. struct snd_seq_client_pool client_pool;
  1799. struct snd_seq_remove_events remove_events;
  1800. struct snd_seq_query_subs query_subs;
  1801. } buf;
  1802. const struct ioctl_handler *handler;
  1803. unsigned long size;
  1804. int err;
  1805. if (snd_BUG_ON(!client))
  1806. return -ENXIO;
  1807. for (handler = ioctl_handlers; handler->cmd > 0; ++handler) {
  1808. if (handler->cmd == cmd)
  1809. break;
  1810. }
  1811. if (handler->cmd == 0)
  1812. return -ENOTTY;
  1813. memset(&buf, 0, sizeof(buf));
  1814. /*
  1815. * All of ioctl commands for ALSA sequencer get an argument of size
  1816. * within 13 bits. We can safely pick up the size from the command.
  1817. */
  1818. size = _IOC_SIZE(handler->cmd);
  1819. if (handler->cmd & IOC_IN) {
  1820. if (copy_from_user(&buf, (const void __user *)arg, size))
  1821. return -EFAULT;
  1822. }
  1823. err = handler->func(client, &buf);
  1824. if (err >= 0) {
  1825. /* Some commands includes a bug in 'dir' field. */
  1826. if (handler->cmd == SNDRV_SEQ_IOCTL_SET_QUEUE_CLIENT ||
  1827. handler->cmd == SNDRV_SEQ_IOCTL_SET_CLIENT_POOL ||
  1828. (handler->cmd & IOC_OUT))
  1829. if (copy_to_user((void __user *)arg, &buf, size))
  1830. return -EFAULT;
  1831. }
  1832. return err;
  1833. }
  1834. #ifdef CONFIG_COMPAT
  1835. #include "seq_compat.c"
  1836. #else
  1837. #define snd_seq_ioctl_compat NULL
  1838. #endif
  1839. /* -------------------------------------------------------- */
  1840. /* exported to kernel modules */
  1841. int snd_seq_create_kernel_client(struct snd_card *card, int client_index,
  1842. const char *name_fmt, ...)
  1843. {
  1844. struct snd_seq_client *client;
  1845. va_list args;
  1846. if (snd_BUG_ON(in_interrupt()))
  1847. return -EBUSY;
  1848. if (card && client_index >= SNDRV_SEQ_CLIENTS_PER_CARD)
  1849. return -EINVAL;
  1850. if (card == NULL && client_index >= SNDRV_SEQ_GLOBAL_CLIENTS)
  1851. return -EINVAL;
  1852. if (mutex_lock_interruptible(&register_mutex))
  1853. return -ERESTARTSYS;
  1854. if (card) {
  1855. client_index += SNDRV_SEQ_GLOBAL_CLIENTS
  1856. + card->number * SNDRV_SEQ_CLIENTS_PER_CARD;
  1857. if (client_index >= SNDRV_SEQ_DYNAMIC_CLIENTS_BEGIN)
  1858. client_index = -1;
  1859. }
  1860. /* empty write queue as default */
  1861. client = seq_create_client1(client_index, 0);
  1862. if (client == NULL) {
  1863. mutex_unlock(&register_mutex);
  1864. return -EBUSY; /* failure code */
  1865. }
  1866. usage_alloc(&client_usage, 1);
  1867. client->accept_input = 1;
  1868. client->accept_output = 1;
  1869. client->data.kernel.card = card;
  1870. va_start(args, name_fmt);
  1871. vsnprintf(client->name, sizeof(client->name), name_fmt, args);
  1872. va_end(args);
  1873. client->type = KERNEL_CLIENT;
  1874. mutex_unlock(&register_mutex);
  1875. /* make others aware this new client */
  1876. snd_seq_system_client_ev_client_start(client->number);
  1877. /* return client number to caller */
  1878. return client->number;
  1879. }
  1880. EXPORT_SYMBOL(snd_seq_create_kernel_client);
  1881. /* exported to kernel modules */
  1882. int snd_seq_delete_kernel_client(int client)
  1883. {
  1884. struct snd_seq_client *ptr;
  1885. if (snd_BUG_ON(in_interrupt()))
  1886. return -EBUSY;
  1887. ptr = clientptr(client);
  1888. if (ptr == NULL)
  1889. return -EINVAL;
  1890. seq_free_client(ptr);
  1891. kfree(ptr);
  1892. return 0;
  1893. }
  1894. EXPORT_SYMBOL(snd_seq_delete_kernel_client);
  1895. /* skeleton to enqueue event, called from snd_seq_kernel_client_enqueue
  1896. * and snd_seq_kernel_client_enqueue_blocking
  1897. */
  1898. static int kernel_client_enqueue(int client, struct snd_seq_event *ev,
  1899. struct file *file, int blocking,
  1900. int atomic, int hop)
  1901. {
  1902. struct snd_seq_client *cptr;
  1903. int result;
  1904. if (snd_BUG_ON(!ev))
  1905. return -EINVAL;
  1906. if (ev->type == SNDRV_SEQ_EVENT_NONE)
  1907. return 0; /* ignore this */
  1908. if (ev->type == SNDRV_SEQ_EVENT_KERNEL_ERROR)
  1909. return -EINVAL; /* quoted events can't be enqueued */
  1910. /* fill in client number */
  1911. ev->source.client = client;
  1912. if (check_event_type_and_length(ev))
  1913. return -EINVAL;
  1914. cptr = snd_seq_client_use_ptr(client);
  1915. if (cptr == NULL)
  1916. return -EINVAL;
  1917. if (! cptr->accept_output)
  1918. result = -EPERM;
  1919. else /* send it */
  1920. result = snd_seq_client_enqueue_event(cptr, ev, file, blocking, atomic, hop);
  1921. snd_seq_client_unlock(cptr);
  1922. return result;
  1923. }
  1924. /*
  1925. * exported, called by kernel clients to enqueue events (w/o blocking)
  1926. *
  1927. * RETURN VALUE: zero if succeed, negative if error
  1928. */
  1929. int snd_seq_kernel_client_enqueue(int client, struct snd_seq_event * ev,
  1930. int atomic, int hop)
  1931. {
  1932. return kernel_client_enqueue(client, ev, NULL, 0, atomic, hop);
  1933. }
  1934. EXPORT_SYMBOL(snd_seq_kernel_client_enqueue);
  1935. /*
  1936. * exported, called by kernel clients to enqueue events (with blocking)
  1937. *
  1938. * RETURN VALUE: zero if succeed, negative if error
  1939. */
  1940. int snd_seq_kernel_client_enqueue_blocking(int client, struct snd_seq_event * ev,
  1941. struct file *file,
  1942. int atomic, int hop)
  1943. {
  1944. return kernel_client_enqueue(client, ev, file, 1, atomic, hop);
  1945. }
  1946. EXPORT_SYMBOL(snd_seq_kernel_client_enqueue_blocking);
  1947. /*
  1948. * exported, called by kernel clients to dispatch events directly to other
  1949. * clients, bypassing the queues. Event time-stamp will be updated.
  1950. *
  1951. * RETURN VALUE: negative = delivery failed,
  1952. * zero, or positive: the number of delivered events
  1953. */
  1954. int snd_seq_kernel_client_dispatch(int client, struct snd_seq_event * ev,
  1955. int atomic, int hop)
  1956. {
  1957. struct snd_seq_client *cptr;
  1958. int result;
  1959. if (snd_BUG_ON(!ev))
  1960. return -EINVAL;
  1961. /* fill in client number */
  1962. ev->queue = SNDRV_SEQ_QUEUE_DIRECT;
  1963. ev->source.client = client;
  1964. if (check_event_type_and_length(ev))
  1965. return -EINVAL;
  1966. cptr = snd_seq_client_use_ptr(client);
  1967. if (cptr == NULL)
  1968. return -EINVAL;
  1969. if (!cptr->accept_output)
  1970. result = -EPERM;
  1971. else
  1972. result = snd_seq_deliver_event(cptr, ev, atomic, hop);
  1973. snd_seq_client_unlock(cptr);
  1974. return result;
  1975. }
  1976. EXPORT_SYMBOL(snd_seq_kernel_client_dispatch);
  1977. /**
  1978. * snd_seq_kernel_client_ctl - operate a command for a client with data in
  1979. * kernel space.
  1980. * @clientid: A numerical ID for a client.
  1981. * @cmd: An ioctl(2) command for ALSA sequencer operation.
  1982. * @arg: A pointer to data in kernel space.
  1983. *
  1984. * Against its name, both kernel/application client can be handled by this
  1985. * kernel API. A pointer of 'arg' argument should be in kernel space.
  1986. *
  1987. * Return: 0 at success. Negative error code at failure.
  1988. */
  1989. int snd_seq_kernel_client_ctl(int clientid, unsigned int cmd, void *arg)
  1990. {
  1991. const struct ioctl_handler *handler;
  1992. struct snd_seq_client *client;
  1993. client = clientptr(clientid);
  1994. if (client == NULL)
  1995. return -ENXIO;
  1996. for (handler = ioctl_handlers; handler->cmd > 0; ++handler) {
  1997. if (handler->cmd == cmd)
  1998. return handler->func(client, arg);
  1999. }
  2000. pr_debug("ALSA: seq unknown ioctl() 0x%x (type='%c', number=0x%02x)\n",
  2001. cmd, _IOC_TYPE(cmd), _IOC_NR(cmd));
  2002. return -ENOTTY;
  2003. }
  2004. EXPORT_SYMBOL(snd_seq_kernel_client_ctl);
  2005. /* exported (for OSS emulator) */
  2006. int snd_seq_kernel_client_write_poll(int clientid, struct file *file, poll_table *wait)
  2007. {
  2008. struct snd_seq_client *client;
  2009. client = clientptr(clientid);
  2010. if (client == NULL)
  2011. return -ENXIO;
  2012. if (! snd_seq_write_pool_allocated(client))
  2013. return 1;
  2014. if (snd_seq_pool_poll_wait(client->pool, file, wait))
  2015. return 1;
  2016. return 0;
  2017. }
  2018. EXPORT_SYMBOL(snd_seq_kernel_client_write_poll);
  2019. /*---------------------------------------------------------------------------*/
  2020. #ifdef CONFIG_SND_PROC_FS
  2021. /*
  2022. * /proc interface
  2023. */
  2024. static void snd_seq_info_dump_subscribers(struct snd_info_buffer *buffer,
  2025. struct snd_seq_port_subs_info *group,
  2026. int is_src, char *msg)
  2027. {
  2028. struct list_head *p;
  2029. struct snd_seq_subscribers *s;
  2030. int count = 0;
  2031. down_read(&group->list_mutex);
  2032. if (list_empty(&group->list_head)) {
  2033. up_read(&group->list_mutex);
  2034. return;
  2035. }
  2036. snd_iprintf(buffer, msg);
  2037. list_for_each(p, &group->list_head) {
  2038. if (is_src)
  2039. s = list_entry(p, struct snd_seq_subscribers, src_list);
  2040. else
  2041. s = list_entry(p, struct snd_seq_subscribers, dest_list);
  2042. if (count++)
  2043. snd_iprintf(buffer, ", ");
  2044. snd_iprintf(buffer, "%d:%d",
  2045. is_src ? s->info.dest.client : s->info.sender.client,
  2046. is_src ? s->info.dest.port : s->info.sender.port);
  2047. if (s->info.flags & SNDRV_SEQ_PORT_SUBS_TIMESTAMP)
  2048. snd_iprintf(buffer, "[%c:%d]", ((s->info.flags & SNDRV_SEQ_PORT_SUBS_TIME_REAL) ? 'r' : 't'), s->info.queue);
  2049. if (group->exclusive)
  2050. snd_iprintf(buffer, "[ex]");
  2051. }
  2052. up_read(&group->list_mutex);
  2053. snd_iprintf(buffer, "\n");
  2054. }
  2055. #define FLAG_PERM_RD(perm) ((perm) & SNDRV_SEQ_PORT_CAP_READ ? ((perm) & SNDRV_SEQ_PORT_CAP_SUBS_READ ? 'R' : 'r') : '-')
  2056. #define FLAG_PERM_WR(perm) ((perm) & SNDRV_SEQ_PORT_CAP_WRITE ? ((perm) & SNDRV_SEQ_PORT_CAP_SUBS_WRITE ? 'W' : 'w') : '-')
  2057. #define FLAG_PERM_EX(perm) ((perm) & SNDRV_SEQ_PORT_CAP_NO_EXPORT ? '-' : 'e')
  2058. #define FLAG_PERM_DUPLEX(perm) ((perm) & SNDRV_SEQ_PORT_CAP_DUPLEX ? 'X' : '-')
  2059. static void snd_seq_info_dump_ports(struct snd_info_buffer *buffer,
  2060. struct snd_seq_client *client)
  2061. {
  2062. struct snd_seq_client_port *p;
  2063. mutex_lock(&client->ports_mutex);
  2064. list_for_each_entry(p, &client->ports_list_head, list) {
  2065. snd_iprintf(buffer, " Port %3d : \"%s\" (%c%c%c%c)\n",
  2066. p->addr.port, p->name,
  2067. FLAG_PERM_RD(p->capability),
  2068. FLAG_PERM_WR(p->capability),
  2069. FLAG_PERM_EX(p->capability),
  2070. FLAG_PERM_DUPLEX(p->capability));
  2071. snd_seq_info_dump_subscribers(buffer, &p->c_src, 1, " Connecting To: ");
  2072. snd_seq_info_dump_subscribers(buffer, &p->c_dest, 0, " Connected From: ");
  2073. }
  2074. mutex_unlock(&client->ports_mutex);
  2075. }
  2076. /* exported to seq_info.c */
  2077. void snd_seq_info_clients_read(struct snd_info_entry *entry,
  2078. struct snd_info_buffer *buffer)
  2079. {
  2080. int c;
  2081. struct snd_seq_client *client;
  2082. snd_iprintf(buffer, "Client info\n");
  2083. snd_iprintf(buffer, " cur clients : %d\n", client_usage.cur);
  2084. snd_iprintf(buffer, " peak clients : %d\n", client_usage.peak);
  2085. snd_iprintf(buffer, " max clients : %d\n", SNDRV_SEQ_MAX_CLIENTS);
  2086. snd_iprintf(buffer, "\n");
  2087. /* list the client table */
  2088. for (c = 0; c < SNDRV_SEQ_MAX_CLIENTS; c++) {
  2089. client = snd_seq_client_use_ptr(c);
  2090. if (client == NULL)
  2091. continue;
  2092. if (client->type == NO_CLIENT) {
  2093. snd_seq_client_unlock(client);
  2094. continue;
  2095. }
  2096. snd_iprintf(buffer, "Client %3d : \"%s\" [%s]\n",
  2097. c, client->name,
  2098. client->type == USER_CLIENT ? "User" : "Kernel");
  2099. snd_seq_info_dump_ports(buffer, client);
  2100. if (snd_seq_write_pool_allocated(client)) {
  2101. snd_iprintf(buffer, " Output pool :\n");
  2102. snd_seq_info_pool(buffer, client->pool, " ");
  2103. }
  2104. if (client->type == USER_CLIENT && client->data.user.fifo &&
  2105. client->data.user.fifo->pool) {
  2106. snd_iprintf(buffer, " Input pool :\n");
  2107. snd_seq_info_pool(buffer, client->data.user.fifo->pool, " ");
  2108. }
  2109. snd_seq_client_unlock(client);
  2110. }
  2111. }
  2112. #endif /* CONFIG_SND_PROC_FS */
  2113. /*---------------------------------------------------------------------------*/
  2114. /*
  2115. * REGISTRATION PART
  2116. */
  2117. static const struct file_operations snd_seq_f_ops =
  2118. {
  2119. .owner = THIS_MODULE,
  2120. .read = snd_seq_read,
  2121. .write = snd_seq_write,
  2122. .open = snd_seq_open,
  2123. .release = snd_seq_release,
  2124. .llseek = no_llseek,
  2125. .poll = snd_seq_poll,
  2126. .unlocked_ioctl = snd_seq_ioctl,
  2127. .compat_ioctl = snd_seq_ioctl_compat,
  2128. };
  2129. static struct device seq_dev;
  2130. /*
  2131. * register sequencer device
  2132. */
  2133. int __init snd_sequencer_device_init(void)
  2134. {
  2135. int err;
  2136. snd_device_initialize(&seq_dev, NULL);
  2137. dev_set_name(&seq_dev, "seq");
  2138. if (mutex_lock_interruptible(&register_mutex))
  2139. return -ERESTARTSYS;
  2140. err = snd_register_device(SNDRV_DEVICE_TYPE_SEQUENCER, NULL, 0,
  2141. &snd_seq_f_ops, NULL, &seq_dev);
  2142. if (err < 0) {
  2143. mutex_unlock(&register_mutex);
  2144. put_device(&seq_dev);
  2145. return err;
  2146. }
  2147. mutex_unlock(&register_mutex);
  2148. return 0;
  2149. }
  2150. /*
  2151. * unregister sequencer device
  2152. */
  2153. void __exit snd_sequencer_device_done(void)
  2154. {
  2155. snd_unregister_device(&seq_dev);
  2156. put_device(&seq_dev);
  2157. }