rawmidi.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726
  1. /*
  2. * Abstract layer for MIDI v1.0 stream
  3. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  4. *
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. #include <sound/core.h>
  22. #include <linux/major.h>
  23. #include <linux/init.h>
  24. #include <linux/sched.h>
  25. #include <linux/slab.h>
  26. #include <linux/time.h>
  27. #include <linux/wait.h>
  28. #include <linux/mutex.h>
  29. #include <linux/module.h>
  30. #include <linux/delay.h>
  31. #include <sound/rawmidi.h>
  32. #include <sound/info.h>
  33. #include <sound/control.h>
  34. #include <sound/minors.h>
  35. #include <sound/initval.h>
  36. MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
  37. MODULE_DESCRIPTION("Midlevel RawMidi code for ALSA.");
  38. MODULE_LICENSE("GPL");
  39. #ifdef CONFIG_SND_OSSEMUL
  40. static int midi_map[SNDRV_CARDS];
  41. static int amidi_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
  42. module_param_array(midi_map, int, NULL, 0444);
  43. MODULE_PARM_DESC(midi_map, "Raw MIDI device number assigned to 1st OSS device.");
  44. module_param_array(amidi_map, int, NULL, 0444);
  45. MODULE_PARM_DESC(amidi_map, "Raw MIDI device number assigned to 2nd OSS device.");
  46. #endif /* CONFIG_SND_OSSEMUL */
  47. static int snd_rawmidi_free(struct snd_rawmidi *rawmidi);
  48. static int snd_rawmidi_dev_free(struct snd_device *device);
  49. static int snd_rawmidi_dev_register(struct snd_device *device);
  50. static int snd_rawmidi_dev_disconnect(struct snd_device *device);
  51. static LIST_HEAD(snd_rawmidi_devices);
  52. static DEFINE_MUTEX(register_mutex);
  53. static struct snd_rawmidi *snd_rawmidi_search(struct snd_card *card, int device)
  54. {
  55. struct snd_rawmidi *rawmidi;
  56. list_for_each_entry(rawmidi, &snd_rawmidi_devices, list)
  57. if (rawmidi->card == card && rawmidi->device == device)
  58. return rawmidi;
  59. return NULL;
  60. }
  61. static inline unsigned short snd_rawmidi_file_flags(struct file *file)
  62. {
  63. switch (file->f_mode & (FMODE_READ | FMODE_WRITE)) {
  64. case FMODE_WRITE:
  65. return SNDRV_RAWMIDI_LFLG_OUTPUT;
  66. case FMODE_READ:
  67. return SNDRV_RAWMIDI_LFLG_INPUT;
  68. default:
  69. return SNDRV_RAWMIDI_LFLG_OPEN;
  70. }
  71. }
  72. static inline int snd_rawmidi_ready(struct snd_rawmidi_substream *substream)
  73. {
  74. struct snd_rawmidi_runtime *runtime = substream->runtime;
  75. return runtime->avail >= runtime->avail_min;
  76. }
  77. static inline int snd_rawmidi_ready_append(struct snd_rawmidi_substream *substream,
  78. size_t count)
  79. {
  80. struct snd_rawmidi_runtime *runtime = substream->runtime;
  81. return runtime->avail >= runtime->avail_min &&
  82. (!substream->append || runtime->avail >= count);
  83. }
  84. static void snd_rawmidi_input_event_work(struct work_struct *work)
  85. {
  86. struct snd_rawmidi_runtime *runtime =
  87. container_of(work, struct snd_rawmidi_runtime, event_work);
  88. if (runtime->event)
  89. runtime->event(runtime->substream);
  90. }
  91. static int snd_rawmidi_runtime_create(struct snd_rawmidi_substream *substream)
  92. {
  93. struct snd_rawmidi_runtime *runtime;
  94. if ((runtime = kzalloc(sizeof(*runtime), GFP_KERNEL)) == NULL)
  95. return -ENOMEM;
  96. runtime->substream = substream;
  97. spin_lock_init(&runtime->lock);
  98. init_waitqueue_head(&runtime->sleep);
  99. INIT_WORK(&runtime->event_work, snd_rawmidi_input_event_work);
  100. runtime->event = NULL;
  101. runtime->buffer_size = PAGE_SIZE;
  102. runtime->avail_min = 1;
  103. if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
  104. runtime->avail = 0;
  105. else
  106. runtime->avail = runtime->buffer_size;
  107. if ((runtime->buffer = kmalloc(runtime->buffer_size, GFP_KERNEL)) == NULL) {
  108. kfree(runtime);
  109. return -ENOMEM;
  110. }
  111. runtime->appl_ptr = runtime->hw_ptr = 0;
  112. substream->runtime = runtime;
  113. return 0;
  114. }
  115. static int snd_rawmidi_runtime_free(struct snd_rawmidi_substream *substream)
  116. {
  117. struct snd_rawmidi_runtime *runtime = substream->runtime;
  118. kfree(runtime->buffer);
  119. kfree(runtime);
  120. substream->runtime = NULL;
  121. return 0;
  122. }
  123. static inline void snd_rawmidi_output_trigger(struct snd_rawmidi_substream *substream,int up)
  124. {
  125. if (!substream->opened)
  126. return;
  127. substream->ops->trigger(substream, up);
  128. }
  129. static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
  130. {
  131. if (!substream->opened)
  132. return;
  133. substream->ops->trigger(substream, up);
  134. if (!up)
  135. cancel_work_sync(&substream->runtime->event_work);
  136. }
  137. int snd_rawmidi_drop_output(struct snd_rawmidi_substream *substream)
  138. {
  139. unsigned long flags;
  140. struct snd_rawmidi_runtime *runtime = substream->runtime;
  141. snd_rawmidi_output_trigger(substream, 0);
  142. runtime->drain = 0;
  143. spin_lock_irqsave(&runtime->lock, flags);
  144. runtime->appl_ptr = runtime->hw_ptr = 0;
  145. runtime->avail = runtime->buffer_size;
  146. spin_unlock_irqrestore(&runtime->lock, flags);
  147. return 0;
  148. }
  149. int snd_rawmidi_drain_output(struct snd_rawmidi_substream *substream)
  150. {
  151. int err;
  152. long timeout;
  153. struct snd_rawmidi_runtime *runtime = substream->runtime;
  154. err = 0;
  155. runtime->drain = 1;
  156. timeout = wait_event_interruptible_timeout(runtime->sleep,
  157. (runtime->avail >= runtime->buffer_size),
  158. 10*HZ);
  159. if (signal_pending(current))
  160. err = -ERESTARTSYS;
  161. if (runtime->avail < runtime->buffer_size && !timeout) {
  162. snd_printk(KERN_WARNING "rawmidi drain error (avail = %li, buffer_size = %li)\n", (long)runtime->avail, (long)runtime->buffer_size);
  163. err = -EIO;
  164. }
  165. runtime->drain = 0;
  166. if (err != -ERESTARTSYS) {
  167. /* we need wait a while to make sure that Tx FIFOs are empty */
  168. if (substream->ops->drain)
  169. substream->ops->drain(substream);
  170. else
  171. msleep(50);
  172. snd_rawmidi_drop_output(substream);
  173. }
  174. return err;
  175. }
  176. int snd_rawmidi_drain_input(struct snd_rawmidi_substream *substream)
  177. {
  178. unsigned long flags;
  179. struct snd_rawmidi_runtime *runtime = substream->runtime;
  180. snd_rawmidi_input_trigger(substream, 0);
  181. runtime->drain = 0;
  182. spin_lock_irqsave(&runtime->lock, flags);
  183. runtime->appl_ptr = runtime->hw_ptr = 0;
  184. runtime->avail = 0;
  185. spin_unlock_irqrestore(&runtime->lock, flags);
  186. return 0;
  187. }
  188. /* look for an available substream for the given stream direction;
  189. * if a specific subdevice is given, try to assign it
  190. */
  191. static int assign_substream(struct snd_rawmidi *rmidi, int subdevice,
  192. int stream, int mode,
  193. struct snd_rawmidi_substream **sub_ret)
  194. {
  195. struct snd_rawmidi_substream *substream;
  196. struct snd_rawmidi_str *s = &rmidi->streams[stream];
  197. static unsigned int info_flags[2] = {
  198. [SNDRV_RAWMIDI_STREAM_OUTPUT] = SNDRV_RAWMIDI_INFO_OUTPUT,
  199. [SNDRV_RAWMIDI_STREAM_INPUT] = SNDRV_RAWMIDI_INFO_INPUT,
  200. };
  201. if (!(rmidi->info_flags & info_flags[stream]))
  202. return -ENXIO;
  203. if (subdevice >= 0 && subdevice >= s->substream_count)
  204. return -ENODEV;
  205. list_for_each_entry(substream, &s->substreams, list) {
  206. if (substream->opened) {
  207. if (stream == SNDRV_RAWMIDI_STREAM_INPUT ||
  208. !(mode & SNDRV_RAWMIDI_LFLG_APPEND) ||
  209. !substream->append)
  210. continue;
  211. }
  212. if (subdevice < 0 || subdevice == substream->number) {
  213. *sub_ret = substream;
  214. return 0;
  215. }
  216. }
  217. return -EAGAIN;
  218. }
  219. /* open and do ref-counting for the given substream */
  220. static int open_substream(struct snd_rawmidi *rmidi,
  221. struct snd_rawmidi_substream *substream,
  222. int mode)
  223. {
  224. int err;
  225. if (substream->use_count == 0) {
  226. err = snd_rawmidi_runtime_create(substream);
  227. if (err < 0)
  228. return err;
  229. err = substream->ops->open(substream);
  230. if (err < 0) {
  231. snd_rawmidi_runtime_free(substream);
  232. return err;
  233. }
  234. substream->opened = 1;
  235. substream->active_sensing = 0;
  236. if (mode & SNDRV_RAWMIDI_LFLG_APPEND)
  237. substream->append = 1;
  238. substream->pid = get_pid(task_pid(current));
  239. rmidi->streams[substream->stream].substream_opened++;
  240. }
  241. substream->use_count++;
  242. return 0;
  243. }
  244. static void close_substream(struct snd_rawmidi *rmidi,
  245. struct snd_rawmidi_substream *substream,
  246. int cleanup);
  247. static int rawmidi_open_priv(struct snd_rawmidi *rmidi, int subdevice, int mode,
  248. struct snd_rawmidi_file *rfile)
  249. {
  250. struct snd_rawmidi_substream *sinput = NULL, *soutput = NULL;
  251. int err;
  252. rfile->input = rfile->output = NULL;
  253. if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
  254. err = assign_substream(rmidi, subdevice,
  255. SNDRV_RAWMIDI_STREAM_INPUT,
  256. mode, &sinput);
  257. if (err < 0)
  258. return err;
  259. }
  260. if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
  261. err = assign_substream(rmidi, subdevice,
  262. SNDRV_RAWMIDI_STREAM_OUTPUT,
  263. mode, &soutput);
  264. if (err < 0)
  265. return err;
  266. }
  267. if (sinput) {
  268. err = open_substream(rmidi, sinput, mode);
  269. if (err < 0)
  270. return err;
  271. }
  272. if (soutput) {
  273. err = open_substream(rmidi, soutput, mode);
  274. if (err < 0) {
  275. if (sinput)
  276. close_substream(rmidi, sinput, 0);
  277. return err;
  278. }
  279. }
  280. rfile->rmidi = rmidi;
  281. rfile->input = sinput;
  282. rfile->output = soutput;
  283. return 0;
  284. }
  285. /* called from sound/core/seq/seq_midi.c */
  286. int snd_rawmidi_kernel_open(struct snd_card *card, int device, int subdevice,
  287. int mode, struct snd_rawmidi_file * rfile)
  288. {
  289. struct snd_rawmidi *rmidi;
  290. int err;
  291. if (snd_BUG_ON(!rfile))
  292. return -EINVAL;
  293. mutex_lock(&register_mutex);
  294. rmidi = snd_rawmidi_search(card, device);
  295. if (rmidi == NULL) {
  296. mutex_unlock(&register_mutex);
  297. return -ENODEV;
  298. }
  299. if (!try_module_get(rmidi->card->module)) {
  300. mutex_unlock(&register_mutex);
  301. return -ENXIO;
  302. }
  303. mutex_unlock(&register_mutex);
  304. mutex_lock(&rmidi->open_mutex);
  305. err = rawmidi_open_priv(rmidi, subdevice, mode, rfile);
  306. mutex_unlock(&rmidi->open_mutex);
  307. if (err < 0)
  308. module_put(rmidi->card->module);
  309. return err;
  310. }
  311. static int snd_rawmidi_open(struct inode *inode, struct file *file)
  312. {
  313. int maj = imajor(inode);
  314. struct snd_card *card;
  315. int subdevice;
  316. unsigned short fflags;
  317. int err;
  318. struct snd_rawmidi *rmidi;
  319. struct snd_rawmidi_file *rawmidi_file = NULL;
  320. wait_queue_t wait;
  321. struct snd_ctl_file *kctl;
  322. if ((file->f_flags & O_APPEND) && !(file->f_flags & O_NONBLOCK))
  323. return -EINVAL; /* invalid combination */
  324. err = nonseekable_open(inode, file);
  325. if (err < 0)
  326. return err;
  327. if (maj == snd_major) {
  328. rmidi = snd_lookup_minor_data(iminor(inode),
  329. SNDRV_DEVICE_TYPE_RAWMIDI);
  330. #ifdef CONFIG_SND_OSSEMUL
  331. } else if (maj == SOUND_MAJOR) {
  332. rmidi = snd_lookup_oss_minor_data(iminor(inode),
  333. SNDRV_OSS_DEVICE_TYPE_MIDI);
  334. #endif
  335. } else
  336. return -ENXIO;
  337. if (rmidi == NULL)
  338. return -ENODEV;
  339. if (!try_module_get(rmidi->card->module)) {
  340. snd_card_unref(rmidi->card);
  341. return -ENXIO;
  342. }
  343. mutex_lock(&rmidi->open_mutex);
  344. card = rmidi->card;
  345. err = snd_card_file_add(card, file);
  346. if (err < 0)
  347. goto __error_card;
  348. fflags = snd_rawmidi_file_flags(file);
  349. if ((file->f_flags & O_APPEND) || maj == SOUND_MAJOR) /* OSS emul? */
  350. fflags |= SNDRV_RAWMIDI_LFLG_APPEND;
  351. rawmidi_file = kmalloc(sizeof(*rawmidi_file), GFP_KERNEL);
  352. if (rawmidi_file == NULL) {
  353. err = -ENOMEM;
  354. goto __error;
  355. }
  356. init_waitqueue_entry(&wait, current);
  357. add_wait_queue(&rmidi->open_wait, &wait);
  358. while (1) {
  359. subdevice = -1;
  360. read_lock(&card->ctl_files_rwlock);
  361. list_for_each_entry(kctl, &card->ctl_files, list) {
  362. if (kctl->pid == task_pid(current)) {
  363. subdevice = kctl->prefer_rawmidi_subdevice;
  364. if (subdevice != -1)
  365. break;
  366. }
  367. }
  368. read_unlock(&card->ctl_files_rwlock);
  369. err = rawmidi_open_priv(rmidi, subdevice, fflags, rawmidi_file);
  370. if (err >= 0)
  371. break;
  372. if (err == -EAGAIN) {
  373. if (file->f_flags & O_NONBLOCK) {
  374. err = -EBUSY;
  375. break;
  376. }
  377. } else
  378. break;
  379. set_current_state(TASK_INTERRUPTIBLE);
  380. mutex_unlock(&rmidi->open_mutex);
  381. schedule();
  382. mutex_lock(&rmidi->open_mutex);
  383. if (rmidi->card->shutdown) {
  384. err = -ENODEV;
  385. break;
  386. }
  387. if (signal_pending(current)) {
  388. err = -ERESTARTSYS;
  389. break;
  390. }
  391. }
  392. remove_wait_queue(&rmidi->open_wait, &wait);
  393. if (err < 0) {
  394. kfree(rawmidi_file);
  395. goto __error;
  396. }
  397. #ifdef CONFIG_SND_OSSEMUL
  398. if (rawmidi_file->input && rawmidi_file->input->runtime)
  399. rawmidi_file->input->runtime->oss = (maj == SOUND_MAJOR);
  400. if (rawmidi_file->output && rawmidi_file->output->runtime)
  401. rawmidi_file->output->runtime->oss = (maj == SOUND_MAJOR);
  402. #endif
  403. file->private_data = rawmidi_file;
  404. mutex_unlock(&rmidi->open_mutex);
  405. snd_card_unref(rmidi->card);
  406. return 0;
  407. __error:
  408. snd_card_file_remove(card, file);
  409. __error_card:
  410. mutex_unlock(&rmidi->open_mutex);
  411. module_put(rmidi->card->module);
  412. snd_card_unref(rmidi->card);
  413. return err;
  414. }
  415. static void close_substream(struct snd_rawmidi *rmidi,
  416. struct snd_rawmidi_substream *substream,
  417. int cleanup)
  418. {
  419. if (--substream->use_count)
  420. return;
  421. if (cleanup) {
  422. if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
  423. snd_rawmidi_input_trigger(substream, 0);
  424. else {
  425. if (substream->active_sensing) {
  426. unsigned char buf = 0xfe;
  427. /* sending single active sensing message
  428. * to shut the device up
  429. */
  430. snd_rawmidi_kernel_write(substream, &buf, 1);
  431. }
  432. if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS)
  433. snd_rawmidi_output_trigger(substream, 0);
  434. }
  435. }
  436. substream->ops->close(substream);
  437. if (substream->runtime->private_free)
  438. substream->runtime->private_free(substream);
  439. snd_rawmidi_runtime_free(substream);
  440. substream->opened = 0;
  441. substream->append = 0;
  442. put_pid(substream->pid);
  443. substream->pid = NULL;
  444. rmidi->streams[substream->stream].substream_opened--;
  445. }
  446. static void rawmidi_release_priv(struct snd_rawmidi_file *rfile)
  447. {
  448. struct snd_rawmidi *rmidi;
  449. rmidi = rfile->rmidi;
  450. mutex_lock(&rmidi->open_mutex);
  451. if (rfile->input) {
  452. close_substream(rmidi, rfile->input, 1);
  453. rfile->input = NULL;
  454. }
  455. if (rfile->output) {
  456. close_substream(rmidi, rfile->output, 1);
  457. rfile->output = NULL;
  458. }
  459. rfile->rmidi = NULL;
  460. mutex_unlock(&rmidi->open_mutex);
  461. wake_up(&rmidi->open_wait);
  462. }
  463. /* called from sound/core/seq/seq_midi.c */
  464. int snd_rawmidi_kernel_release(struct snd_rawmidi_file *rfile)
  465. {
  466. struct snd_rawmidi *rmidi;
  467. if (snd_BUG_ON(!rfile))
  468. return -ENXIO;
  469. rmidi = rfile->rmidi;
  470. rawmidi_release_priv(rfile);
  471. module_put(rmidi->card->module);
  472. return 0;
  473. }
  474. static int snd_rawmidi_release(struct inode *inode, struct file *file)
  475. {
  476. struct snd_rawmidi_file *rfile;
  477. struct snd_rawmidi *rmidi;
  478. struct module *module;
  479. rfile = file->private_data;
  480. rmidi = rfile->rmidi;
  481. rawmidi_release_priv(rfile);
  482. kfree(rfile);
  483. module = rmidi->card->module;
  484. snd_card_file_remove(rmidi->card, file);
  485. module_put(module);
  486. return 0;
  487. }
  488. static int snd_rawmidi_info(struct snd_rawmidi_substream *substream,
  489. struct snd_rawmidi_info *info)
  490. {
  491. struct snd_rawmidi *rmidi;
  492. if (substream == NULL)
  493. return -ENODEV;
  494. rmidi = substream->rmidi;
  495. memset(info, 0, sizeof(*info));
  496. info->card = rmidi->card->number;
  497. info->device = rmidi->device;
  498. info->subdevice = substream->number;
  499. info->stream = substream->stream;
  500. info->flags = rmidi->info_flags;
  501. strcpy(info->id, rmidi->id);
  502. strcpy(info->name, rmidi->name);
  503. strcpy(info->subname, substream->name);
  504. info->subdevices_count = substream->pstr->substream_count;
  505. info->subdevices_avail = (substream->pstr->substream_count -
  506. substream->pstr->substream_opened);
  507. return 0;
  508. }
  509. static int snd_rawmidi_info_user(struct snd_rawmidi_substream *substream,
  510. struct snd_rawmidi_info __user * _info)
  511. {
  512. struct snd_rawmidi_info info;
  513. int err;
  514. if ((err = snd_rawmidi_info(substream, &info)) < 0)
  515. return err;
  516. if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
  517. return -EFAULT;
  518. return 0;
  519. }
  520. int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info)
  521. {
  522. struct snd_rawmidi *rmidi;
  523. struct snd_rawmidi_str *pstr;
  524. struct snd_rawmidi_substream *substream;
  525. mutex_lock(&register_mutex);
  526. rmidi = snd_rawmidi_search(card, info->device);
  527. mutex_unlock(&register_mutex);
  528. if (!rmidi)
  529. return -ENXIO;
  530. if (info->stream < 0 || info->stream > 1)
  531. return -EINVAL;
  532. pstr = &rmidi->streams[info->stream];
  533. if (pstr->substream_count == 0)
  534. return -ENOENT;
  535. if (info->subdevice >= pstr->substream_count)
  536. return -ENXIO;
  537. list_for_each_entry(substream, &pstr->substreams, list) {
  538. if ((unsigned int)substream->number == info->subdevice)
  539. return snd_rawmidi_info(substream, info);
  540. }
  541. return -ENXIO;
  542. }
  543. static int snd_rawmidi_info_select_user(struct snd_card *card,
  544. struct snd_rawmidi_info __user *_info)
  545. {
  546. int err;
  547. struct snd_rawmidi_info info;
  548. if (get_user(info.device, &_info->device))
  549. return -EFAULT;
  550. if (get_user(info.stream, &_info->stream))
  551. return -EFAULT;
  552. if (get_user(info.subdevice, &_info->subdevice))
  553. return -EFAULT;
  554. if ((err = snd_rawmidi_info_select(card, &info)) < 0)
  555. return err;
  556. if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
  557. return -EFAULT;
  558. return 0;
  559. }
  560. int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
  561. struct snd_rawmidi_params * params)
  562. {
  563. char *newbuf;
  564. struct snd_rawmidi_runtime *runtime = substream->runtime;
  565. if (substream->append && substream->use_count > 1)
  566. return -EBUSY;
  567. snd_rawmidi_drain_output(substream);
  568. if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
  569. return -EINVAL;
  570. }
  571. if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
  572. return -EINVAL;
  573. }
  574. if (params->buffer_size != runtime->buffer_size) {
  575. newbuf = krealloc(runtime->buffer, params->buffer_size,
  576. GFP_KERNEL);
  577. if (!newbuf)
  578. return -ENOMEM;
  579. runtime->buffer = newbuf;
  580. runtime->buffer_size = params->buffer_size;
  581. runtime->avail = runtime->buffer_size;
  582. }
  583. runtime->avail_min = params->avail_min;
  584. substream->active_sensing = !params->no_active_sensing;
  585. return 0;
  586. }
  587. int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
  588. struct snd_rawmidi_params * params)
  589. {
  590. char *newbuf;
  591. struct snd_rawmidi_runtime *runtime = substream->runtime;
  592. snd_rawmidi_drain_input(substream);
  593. if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
  594. return -EINVAL;
  595. }
  596. if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
  597. return -EINVAL;
  598. }
  599. if (params->buffer_size != runtime->buffer_size) {
  600. newbuf = krealloc(runtime->buffer, params->buffer_size,
  601. GFP_KERNEL);
  602. if (!newbuf)
  603. return -ENOMEM;
  604. runtime->buffer = newbuf;
  605. runtime->buffer_size = params->buffer_size;
  606. }
  607. runtime->avail_min = params->avail_min;
  608. return 0;
  609. }
  610. static int snd_rawmidi_output_status(struct snd_rawmidi_substream *substream,
  611. struct snd_rawmidi_status * status)
  612. {
  613. struct snd_rawmidi_runtime *runtime = substream->runtime;
  614. memset(status, 0, sizeof(*status));
  615. status->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
  616. spin_lock_irq(&runtime->lock);
  617. status->avail = runtime->avail;
  618. spin_unlock_irq(&runtime->lock);
  619. return 0;
  620. }
  621. static int snd_rawmidi_input_status(struct snd_rawmidi_substream *substream,
  622. struct snd_rawmidi_status * status)
  623. {
  624. struct snd_rawmidi_runtime *runtime = substream->runtime;
  625. memset(status, 0, sizeof(*status));
  626. status->stream = SNDRV_RAWMIDI_STREAM_INPUT;
  627. spin_lock_irq(&runtime->lock);
  628. status->avail = runtime->avail;
  629. status->xruns = runtime->xruns;
  630. runtime->xruns = 0;
  631. spin_unlock_irq(&runtime->lock);
  632. return 0;
  633. }
  634. static long snd_rawmidi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  635. {
  636. struct snd_rawmidi_file *rfile;
  637. void __user *argp = (void __user *)arg;
  638. rfile = file->private_data;
  639. if (((cmd >> 8) & 0xff) != 'W')
  640. return -ENOTTY;
  641. switch (cmd) {
  642. case SNDRV_RAWMIDI_IOCTL_PVERSION:
  643. return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0;
  644. case SNDRV_RAWMIDI_IOCTL_INFO:
  645. {
  646. int stream;
  647. struct snd_rawmidi_info __user *info = argp;
  648. if (get_user(stream, &info->stream))
  649. return -EFAULT;
  650. switch (stream) {
  651. case SNDRV_RAWMIDI_STREAM_INPUT:
  652. return snd_rawmidi_info_user(rfile->input, info);
  653. case SNDRV_RAWMIDI_STREAM_OUTPUT:
  654. return snd_rawmidi_info_user(rfile->output, info);
  655. default:
  656. return -EINVAL;
  657. }
  658. }
  659. case SNDRV_RAWMIDI_IOCTL_PARAMS:
  660. {
  661. struct snd_rawmidi_params params;
  662. if (copy_from_user(&params, argp, sizeof(struct snd_rawmidi_params)))
  663. return -EFAULT;
  664. switch (params.stream) {
  665. case SNDRV_RAWMIDI_STREAM_OUTPUT:
  666. if (rfile->output == NULL)
  667. return -EINVAL;
  668. return snd_rawmidi_output_params(rfile->output, &params);
  669. case SNDRV_RAWMIDI_STREAM_INPUT:
  670. if (rfile->input == NULL)
  671. return -EINVAL;
  672. return snd_rawmidi_input_params(rfile->input, &params);
  673. default:
  674. return -EINVAL;
  675. }
  676. }
  677. case SNDRV_RAWMIDI_IOCTL_STATUS:
  678. {
  679. int err = 0;
  680. struct snd_rawmidi_status status;
  681. if (copy_from_user(&status, argp, sizeof(struct snd_rawmidi_status)))
  682. return -EFAULT;
  683. switch (status.stream) {
  684. case SNDRV_RAWMIDI_STREAM_OUTPUT:
  685. if (rfile->output == NULL)
  686. return -EINVAL;
  687. err = snd_rawmidi_output_status(rfile->output, &status);
  688. break;
  689. case SNDRV_RAWMIDI_STREAM_INPUT:
  690. if (rfile->input == NULL)
  691. return -EINVAL;
  692. err = snd_rawmidi_input_status(rfile->input, &status);
  693. break;
  694. default:
  695. return -EINVAL;
  696. }
  697. if (err < 0)
  698. return err;
  699. if (copy_to_user(argp, &status, sizeof(struct snd_rawmidi_status)))
  700. return -EFAULT;
  701. return 0;
  702. }
  703. case SNDRV_RAWMIDI_IOCTL_DROP:
  704. {
  705. int val;
  706. if (get_user(val, (int __user *) argp))
  707. return -EFAULT;
  708. switch (val) {
  709. case SNDRV_RAWMIDI_STREAM_OUTPUT:
  710. if (rfile->output == NULL)
  711. return -EINVAL;
  712. return snd_rawmidi_drop_output(rfile->output);
  713. default:
  714. return -EINVAL;
  715. }
  716. }
  717. case SNDRV_RAWMIDI_IOCTL_DRAIN:
  718. {
  719. int val;
  720. if (get_user(val, (int __user *) argp))
  721. return -EFAULT;
  722. switch (val) {
  723. case SNDRV_RAWMIDI_STREAM_OUTPUT:
  724. if (rfile->output == NULL)
  725. return -EINVAL;
  726. return snd_rawmidi_drain_output(rfile->output);
  727. case SNDRV_RAWMIDI_STREAM_INPUT:
  728. if (rfile->input == NULL)
  729. return -EINVAL;
  730. return snd_rawmidi_drain_input(rfile->input);
  731. default:
  732. return -EINVAL;
  733. }
  734. }
  735. #ifdef CONFIG_SND_DEBUG
  736. default:
  737. snd_printk(KERN_WARNING "rawmidi: unknown command = 0x%x\n", cmd);
  738. #endif
  739. }
  740. return -ENOTTY;
  741. }
  742. static int snd_rawmidi_control_ioctl(struct snd_card *card,
  743. struct snd_ctl_file *control,
  744. unsigned int cmd,
  745. unsigned long arg)
  746. {
  747. void __user *argp = (void __user *)arg;
  748. switch (cmd) {
  749. case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
  750. {
  751. int device;
  752. if (get_user(device, (int __user *)argp))
  753. return -EFAULT;
  754. if (device >= SNDRV_RAWMIDI_DEVICES) /* next device is -1 */
  755. device = SNDRV_RAWMIDI_DEVICES - 1;
  756. mutex_lock(&register_mutex);
  757. device = device < 0 ? 0 : device + 1;
  758. while (device < SNDRV_RAWMIDI_DEVICES) {
  759. if (snd_rawmidi_search(card, device))
  760. break;
  761. device++;
  762. }
  763. if (device == SNDRV_RAWMIDI_DEVICES)
  764. device = -1;
  765. mutex_unlock(&register_mutex);
  766. if (put_user(device, (int __user *)argp))
  767. return -EFAULT;
  768. return 0;
  769. }
  770. case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
  771. {
  772. int val;
  773. if (get_user(val, (int __user *)argp))
  774. return -EFAULT;
  775. control->prefer_rawmidi_subdevice = val;
  776. return 0;
  777. }
  778. case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
  779. return snd_rawmidi_info_select_user(card, argp);
  780. }
  781. return -ENOIOCTLCMD;
  782. }
  783. /**
  784. * snd_rawmidi_receive - receive the input data from the device
  785. * @substream: the rawmidi substream
  786. * @buffer: the buffer pointer
  787. * @count: the data size to read
  788. *
  789. * Reads the data from the internal buffer.
  790. *
  791. * Returns the size of read data, or a negative error code on failure.
  792. */
  793. int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
  794. const unsigned char *buffer, int count)
  795. {
  796. unsigned long flags;
  797. int result = 0, count1;
  798. struct snd_rawmidi_runtime *runtime = substream->runtime;
  799. if (!substream->opened)
  800. return -EBADFD;
  801. if (runtime->buffer == NULL) {
  802. snd_printd("snd_rawmidi_receive: input is not active!!!\n");
  803. return -EINVAL;
  804. }
  805. spin_lock_irqsave(&runtime->lock, flags);
  806. if (count == 1) { /* special case, faster code */
  807. substream->bytes++;
  808. if (runtime->avail < runtime->buffer_size) {
  809. runtime->buffer[runtime->hw_ptr++] = buffer[0];
  810. runtime->hw_ptr %= runtime->buffer_size;
  811. runtime->avail++;
  812. result++;
  813. } else {
  814. runtime->xruns++;
  815. }
  816. } else {
  817. substream->bytes += count;
  818. count1 = runtime->buffer_size - runtime->hw_ptr;
  819. if (count1 > count)
  820. count1 = count;
  821. if (count1 > (int)(runtime->buffer_size - runtime->avail))
  822. count1 = runtime->buffer_size - runtime->avail;
  823. memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
  824. runtime->hw_ptr += count1;
  825. runtime->hw_ptr %= runtime->buffer_size;
  826. runtime->avail += count1;
  827. count -= count1;
  828. result += count1;
  829. if (count > 0) {
  830. buffer += count1;
  831. count1 = count;
  832. if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
  833. count1 = runtime->buffer_size - runtime->avail;
  834. runtime->xruns += count - count1;
  835. }
  836. if (count1 > 0) {
  837. memcpy(runtime->buffer, buffer, count1);
  838. runtime->hw_ptr = count1;
  839. runtime->avail += count1;
  840. result += count1;
  841. }
  842. }
  843. }
  844. if (result > 0) {
  845. if (runtime->event)
  846. schedule_work(&runtime->event_work);
  847. else if (snd_rawmidi_ready(substream))
  848. wake_up(&runtime->sleep);
  849. }
  850. spin_unlock_irqrestore(&runtime->lock, flags);
  851. return result;
  852. }
  853. static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream,
  854. unsigned char __user *userbuf,
  855. unsigned char *kernelbuf, long count)
  856. {
  857. unsigned long flags;
  858. long result = 0, count1;
  859. struct snd_rawmidi_runtime *runtime = substream->runtime;
  860. while (count > 0 && runtime->avail) {
  861. count1 = runtime->buffer_size - runtime->appl_ptr;
  862. if (count1 > count)
  863. count1 = count;
  864. spin_lock_irqsave(&runtime->lock, flags);
  865. if (count1 > (int)runtime->avail)
  866. count1 = runtime->avail;
  867. if (kernelbuf)
  868. memcpy(kernelbuf + result, runtime->buffer + runtime->appl_ptr, count1);
  869. if (userbuf) {
  870. spin_unlock_irqrestore(&runtime->lock, flags);
  871. if (copy_to_user(userbuf + result,
  872. runtime->buffer + runtime->appl_ptr, count1)) {
  873. return result > 0 ? result : -EFAULT;
  874. }
  875. spin_lock_irqsave(&runtime->lock, flags);
  876. }
  877. runtime->appl_ptr += count1;
  878. runtime->appl_ptr %= runtime->buffer_size;
  879. runtime->avail -= count1;
  880. spin_unlock_irqrestore(&runtime->lock, flags);
  881. result += count1;
  882. count -= count1;
  883. }
  884. return result;
  885. }
  886. long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
  887. unsigned char *buf, long count)
  888. {
  889. snd_rawmidi_input_trigger(substream, 1);
  890. return snd_rawmidi_kernel_read1(substream, NULL/*userbuf*/, buf, count);
  891. }
  892. static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count,
  893. loff_t *offset)
  894. {
  895. long result;
  896. int count1;
  897. struct snd_rawmidi_file *rfile;
  898. struct snd_rawmidi_substream *substream;
  899. struct snd_rawmidi_runtime *runtime;
  900. rfile = file->private_data;
  901. substream = rfile->input;
  902. if (substream == NULL)
  903. return -EIO;
  904. runtime = substream->runtime;
  905. snd_rawmidi_input_trigger(substream, 1);
  906. result = 0;
  907. while (count > 0) {
  908. spin_lock_irq(&runtime->lock);
  909. while (!snd_rawmidi_ready(substream)) {
  910. wait_queue_t wait;
  911. if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
  912. spin_unlock_irq(&runtime->lock);
  913. return result > 0 ? result : -EAGAIN;
  914. }
  915. init_waitqueue_entry(&wait, current);
  916. add_wait_queue(&runtime->sleep, &wait);
  917. set_current_state(TASK_INTERRUPTIBLE);
  918. spin_unlock_irq(&runtime->lock);
  919. schedule();
  920. remove_wait_queue(&runtime->sleep, &wait);
  921. if (rfile->rmidi->card->shutdown)
  922. return -ENODEV;
  923. if (signal_pending(current))
  924. return result > 0 ? result : -ERESTARTSYS;
  925. if (!runtime->avail)
  926. return result > 0 ? result : -EIO;
  927. spin_lock_irq(&runtime->lock);
  928. }
  929. spin_unlock_irq(&runtime->lock);
  930. count1 = snd_rawmidi_kernel_read1(substream,
  931. (unsigned char __user *)buf,
  932. NULL/*kernelbuf*/,
  933. count);
  934. if (count1 < 0)
  935. return result > 0 ? result : count1;
  936. result += count1;
  937. buf += count1;
  938. count -= count1;
  939. }
  940. return result;
  941. }
  942. /**
  943. * snd_rawmidi_transmit_empty - check whether the output buffer is empty
  944. * @substream: the rawmidi substream
  945. *
  946. * Returns 1 if the internal output buffer is empty, 0 if not.
  947. */
  948. int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream)
  949. {
  950. struct snd_rawmidi_runtime *runtime = substream->runtime;
  951. int result;
  952. unsigned long flags;
  953. if (runtime->buffer == NULL) {
  954. snd_printd("snd_rawmidi_transmit_empty: output is not active!!!\n");
  955. return 1;
  956. }
  957. spin_lock_irqsave(&runtime->lock, flags);
  958. result = runtime->avail >= runtime->buffer_size;
  959. spin_unlock_irqrestore(&runtime->lock, flags);
  960. return result;
  961. }
  962. /**
  963. * snd_rawmidi_transmit_peek - copy data from the internal buffer
  964. * @substream: the rawmidi substream
  965. * @buffer: the buffer pointer
  966. * @count: data size to transfer
  967. *
  968. * Copies data from the internal output buffer to the given buffer.
  969. *
  970. * Call this in the interrupt handler when the midi output is ready,
  971. * and call snd_rawmidi_transmit_ack() after the transmission is
  972. * finished.
  973. *
  974. * Returns the size of copied data, or a negative error code on failure.
  975. */
  976. int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
  977. unsigned char *buffer, int count)
  978. {
  979. unsigned long flags;
  980. int result, count1;
  981. struct snd_rawmidi_runtime *runtime = substream->runtime;
  982. if (runtime->buffer == NULL) {
  983. snd_printd("snd_rawmidi_transmit_peek: output is not active!!!\n");
  984. return -EINVAL;
  985. }
  986. result = 0;
  987. spin_lock_irqsave(&runtime->lock, flags);
  988. if (runtime->avail >= runtime->buffer_size) {
  989. /* warning: lowlevel layer MUST trigger down the hardware */
  990. goto __skip;
  991. }
  992. if (count == 1) { /* special case, faster code */
  993. *buffer = runtime->buffer[runtime->hw_ptr];
  994. result++;
  995. } else {
  996. count1 = runtime->buffer_size - runtime->hw_ptr;
  997. if (count1 > count)
  998. count1 = count;
  999. if (count1 > (int)(runtime->buffer_size - runtime->avail))
  1000. count1 = runtime->buffer_size - runtime->avail;
  1001. memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
  1002. count -= count1;
  1003. result += count1;
  1004. if (count > 0) {
  1005. if (count > (int)(runtime->buffer_size - runtime->avail - count1))
  1006. count = runtime->buffer_size - runtime->avail - count1;
  1007. memcpy(buffer + count1, runtime->buffer, count);
  1008. result += count;
  1009. }
  1010. }
  1011. __skip:
  1012. spin_unlock_irqrestore(&runtime->lock, flags);
  1013. return result;
  1014. }
  1015. /**
  1016. * snd_rawmidi_transmit_ack - acknowledge the transmission
  1017. * @substream: the rawmidi substream
  1018. * @count: the tranferred count
  1019. *
  1020. * Advances the hardware pointer for the internal output buffer with
  1021. * the given size and updates the condition.
  1022. * Call after the transmission is finished.
  1023. *
  1024. * Returns the advanced size if successful, or a negative error code on failure.
  1025. */
  1026. int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
  1027. {
  1028. unsigned long flags;
  1029. struct snd_rawmidi_runtime *runtime = substream->runtime;
  1030. if (runtime->buffer == NULL) {
  1031. snd_printd("snd_rawmidi_transmit_ack: output is not active!!!\n");
  1032. return -EINVAL;
  1033. }
  1034. spin_lock_irqsave(&runtime->lock, flags);
  1035. snd_BUG_ON(runtime->avail + count > runtime->buffer_size);
  1036. runtime->hw_ptr += count;
  1037. runtime->hw_ptr %= runtime->buffer_size;
  1038. runtime->avail += count;
  1039. substream->bytes += count;
  1040. if (count > 0) {
  1041. if (runtime->drain || snd_rawmidi_ready(substream))
  1042. wake_up(&runtime->sleep);
  1043. }
  1044. spin_unlock_irqrestore(&runtime->lock, flags);
  1045. return count;
  1046. }
  1047. /**
  1048. * snd_rawmidi_transmit - copy from the buffer to the device
  1049. * @substream: the rawmidi substream
  1050. * @buffer: the buffer pointer
  1051. * @count: the data size to transfer
  1052. *
  1053. * Copies data from the buffer to the device and advances the pointer.
  1054. *
  1055. * Returns the copied size if successful, or a negative error code on failure.
  1056. */
  1057. int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
  1058. unsigned char *buffer, int count)
  1059. {
  1060. if (!substream->opened)
  1061. return -EBADFD;
  1062. count = snd_rawmidi_transmit_peek(substream, buffer, count);
  1063. if (count < 0)
  1064. return count;
  1065. return snd_rawmidi_transmit_ack(substream, count);
  1066. }
  1067. static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream,
  1068. const unsigned char __user *userbuf,
  1069. const unsigned char *kernelbuf,
  1070. long count)
  1071. {
  1072. unsigned long flags;
  1073. long count1, result;
  1074. struct snd_rawmidi_runtime *runtime = substream->runtime;
  1075. if (snd_BUG_ON(!kernelbuf && !userbuf))
  1076. return -EINVAL;
  1077. if (snd_BUG_ON(!runtime->buffer))
  1078. return -EINVAL;
  1079. result = 0;
  1080. spin_lock_irqsave(&runtime->lock, flags);
  1081. if (substream->append) {
  1082. if ((long)runtime->avail < count) {
  1083. spin_unlock_irqrestore(&runtime->lock, flags);
  1084. return -EAGAIN;
  1085. }
  1086. }
  1087. while (count > 0 && runtime->avail > 0) {
  1088. count1 = runtime->buffer_size - runtime->appl_ptr;
  1089. if (count1 > count)
  1090. count1 = count;
  1091. if (count1 > (long)runtime->avail)
  1092. count1 = runtime->avail;
  1093. if (kernelbuf)
  1094. memcpy(runtime->buffer + runtime->appl_ptr,
  1095. kernelbuf + result, count1);
  1096. else if (userbuf) {
  1097. spin_unlock_irqrestore(&runtime->lock, flags);
  1098. if (copy_from_user(runtime->buffer + runtime->appl_ptr,
  1099. userbuf + result, count1)) {
  1100. spin_lock_irqsave(&runtime->lock, flags);
  1101. result = result > 0 ? result : -EFAULT;
  1102. goto __end;
  1103. }
  1104. spin_lock_irqsave(&runtime->lock, flags);
  1105. }
  1106. runtime->appl_ptr += count1;
  1107. runtime->appl_ptr %= runtime->buffer_size;
  1108. runtime->avail -= count1;
  1109. result += count1;
  1110. count -= count1;
  1111. }
  1112. __end:
  1113. count1 = runtime->avail < runtime->buffer_size;
  1114. spin_unlock_irqrestore(&runtime->lock, flags);
  1115. if (count1)
  1116. snd_rawmidi_output_trigger(substream, 1);
  1117. return result;
  1118. }
  1119. long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream,
  1120. const unsigned char *buf, long count)
  1121. {
  1122. return snd_rawmidi_kernel_write1(substream, NULL, buf, count);
  1123. }
  1124. static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf,
  1125. size_t count, loff_t *offset)
  1126. {
  1127. long result, timeout;
  1128. int count1;
  1129. struct snd_rawmidi_file *rfile;
  1130. struct snd_rawmidi_runtime *runtime;
  1131. struct snd_rawmidi_substream *substream;
  1132. rfile = file->private_data;
  1133. substream = rfile->output;
  1134. runtime = substream->runtime;
  1135. /* we cannot put an atomic message to our buffer */
  1136. if (substream->append && count > runtime->buffer_size)
  1137. return -EIO;
  1138. result = 0;
  1139. while (count > 0) {
  1140. spin_lock_irq(&runtime->lock);
  1141. while (!snd_rawmidi_ready_append(substream, count)) {
  1142. wait_queue_t wait;
  1143. if (file->f_flags & O_NONBLOCK) {
  1144. spin_unlock_irq(&runtime->lock);
  1145. return result > 0 ? result : -EAGAIN;
  1146. }
  1147. init_waitqueue_entry(&wait, current);
  1148. add_wait_queue(&runtime->sleep, &wait);
  1149. set_current_state(TASK_INTERRUPTIBLE);
  1150. spin_unlock_irq(&runtime->lock);
  1151. timeout = schedule_timeout(30 * HZ);
  1152. remove_wait_queue(&runtime->sleep, &wait);
  1153. if (rfile->rmidi->card->shutdown)
  1154. return -ENODEV;
  1155. if (signal_pending(current))
  1156. return result > 0 ? result : -ERESTARTSYS;
  1157. if (!runtime->avail && !timeout)
  1158. return result > 0 ? result : -EIO;
  1159. spin_lock_irq(&runtime->lock);
  1160. }
  1161. spin_unlock_irq(&runtime->lock);
  1162. count1 = snd_rawmidi_kernel_write1(substream, buf, NULL, count);
  1163. if (count1 < 0)
  1164. return result > 0 ? result : count1;
  1165. result += count1;
  1166. buf += count1;
  1167. if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
  1168. break;
  1169. count -= count1;
  1170. }
  1171. if (file->f_flags & O_DSYNC) {
  1172. spin_lock_irq(&runtime->lock);
  1173. while (runtime->avail != runtime->buffer_size) {
  1174. wait_queue_t wait;
  1175. unsigned int last_avail = runtime->avail;
  1176. init_waitqueue_entry(&wait, current);
  1177. add_wait_queue(&runtime->sleep, &wait);
  1178. set_current_state(TASK_INTERRUPTIBLE);
  1179. spin_unlock_irq(&runtime->lock);
  1180. timeout = schedule_timeout(30 * HZ);
  1181. remove_wait_queue(&runtime->sleep, &wait);
  1182. if (signal_pending(current))
  1183. return result > 0 ? result : -ERESTARTSYS;
  1184. if (runtime->avail == last_avail && !timeout)
  1185. return result > 0 ? result : -EIO;
  1186. spin_lock_irq(&runtime->lock);
  1187. }
  1188. spin_unlock_irq(&runtime->lock);
  1189. }
  1190. return result;
  1191. }
  1192. static unsigned int snd_rawmidi_poll(struct file *file, poll_table * wait)
  1193. {
  1194. struct snd_rawmidi_file *rfile;
  1195. struct snd_rawmidi_runtime *runtime;
  1196. unsigned int mask;
  1197. rfile = file->private_data;
  1198. if (rfile->input != NULL) {
  1199. runtime = rfile->input->runtime;
  1200. snd_rawmidi_input_trigger(rfile->input, 1);
  1201. poll_wait(file, &runtime->sleep, wait);
  1202. }
  1203. if (rfile->output != NULL) {
  1204. runtime = rfile->output->runtime;
  1205. poll_wait(file, &runtime->sleep, wait);
  1206. }
  1207. mask = 0;
  1208. if (rfile->input != NULL) {
  1209. if (snd_rawmidi_ready(rfile->input))
  1210. mask |= POLLIN | POLLRDNORM;
  1211. }
  1212. if (rfile->output != NULL) {
  1213. if (snd_rawmidi_ready(rfile->output))
  1214. mask |= POLLOUT | POLLWRNORM;
  1215. }
  1216. return mask;
  1217. }
  1218. /*
  1219. */
  1220. #ifdef CONFIG_COMPAT
  1221. #include "rawmidi_compat.c"
  1222. #else
  1223. #define snd_rawmidi_ioctl_compat NULL
  1224. #endif
  1225. /*
  1226. */
  1227. static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry,
  1228. struct snd_info_buffer *buffer)
  1229. {
  1230. struct snd_rawmidi *rmidi;
  1231. struct snd_rawmidi_substream *substream;
  1232. struct snd_rawmidi_runtime *runtime;
  1233. rmidi = entry->private_data;
  1234. snd_iprintf(buffer, "%s\n\n", rmidi->name);
  1235. mutex_lock(&rmidi->open_mutex);
  1236. if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
  1237. list_for_each_entry(substream,
  1238. &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
  1239. list) {
  1240. snd_iprintf(buffer,
  1241. "Output %d\n"
  1242. " Tx bytes : %lu\n",
  1243. substream->number,
  1244. (unsigned long) substream->bytes);
  1245. if (substream->opened) {
  1246. snd_iprintf(buffer,
  1247. " Owner PID : %d\n",
  1248. pid_vnr(substream->pid));
  1249. runtime = substream->runtime;
  1250. snd_iprintf(buffer,
  1251. " Mode : %s\n"
  1252. " Buffer size : %lu\n"
  1253. " Avail : %lu\n",
  1254. runtime->oss ? "OSS compatible" : "native",
  1255. (unsigned long) runtime->buffer_size,
  1256. (unsigned long) runtime->avail);
  1257. }
  1258. }
  1259. }
  1260. if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
  1261. list_for_each_entry(substream,
  1262. &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams,
  1263. list) {
  1264. snd_iprintf(buffer,
  1265. "Input %d\n"
  1266. " Rx bytes : %lu\n",
  1267. substream->number,
  1268. (unsigned long) substream->bytes);
  1269. if (substream->opened) {
  1270. snd_iprintf(buffer,
  1271. " Owner PID : %d\n",
  1272. pid_vnr(substream->pid));
  1273. runtime = substream->runtime;
  1274. snd_iprintf(buffer,
  1275. " Buffer size : %lu\n"
  1276. " Avail : %lu\n"
  1277. " Overruns : %lu\n",
  1278. (unsigned long) runtime->buffer_size,
  1279. (unsigned long) runtime->avail,
  1280. (unsigned long) runtime->xruns);
  1281. }
  1282. }
  1283. }
  1284. mutex_unlock(&rmidi->open_mutex);
  1285. }
  1286. /*
  1287. * Register functions
  1288. */
  1289. static const struct file_operations snd_rawmidi_f_ops =
  1290. {
  1291. .owner = THIS_MODULE,
  1292. .read = snd_rawmidi_read,
  1293. .write = snd_rawmidi_write,
  1294. .open = snd_rawmidi_open,
  1295. .release = snd_rawmidi_release,
  1296. .llseek = no_llseek,
  1297. .poll = snd_rawmidi_poll,
  1298. .unlocked_ioctl = snd_rawmidi_ioctl,
  1299. .compat_ioctl = snd_rawmidi_ioctl_compat,
  1300. };
  1301. static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi,
  1302. struct snd_rawmidi_str *stream,
  1303. int direction,
  1304. int count)
  1305. {
  1306. struct snd_rawmidi_substream *substream;
  1307. int idx;
  1308. for (idx = 0; idx < count; idx++) {
  1309. substream = kzalloc(sizeof(*substream), GFP_KERNEL);
  1310. if (substream == NULL) {
  1311. snd_printk(KERN_ERR "rawmidi: cannot allocate substream\n");
  1312. return -ENOMEM;
  1313. }
  1314. substream->stream = direction;
  1315. substream->number = idx;
  1316. substream->rmidi = rmidi;
  1317. substream->pstr = stream;
  1318. list_add_tail(&substream->list, &stream->substreams);
  1319. stream->substream_count++;
  1320. }
  1321. return 0;
  1322. }
  1323. /**
  1324. * snd_rawmidi_new - create a rawmidi instance
  1325. * @card: the card instance
  1326. * @id: the id string
  1327. * @device: the device index
  1328. * @output_count: the number of output streams
  1329. * @input_count: the number of input streams
  1330. * @rrawmidi: the pointer to store the new rawmidi instance
  1331. *
  1332. * Creates a new rawmidi instance.
  1333. * Use snd_rawmidi_set_ops() to set the operators to the new instance.
  1334. *
  1335. * Returns zero if successful, or a negative error code on failure.
  1336. */
  1337. int snd_rawmidi_new(struct snd_card *card, char *id, int device,
  1338. int output_count, int input_count,
  1339. struct snd_rawmidi ** rrawmidi)
  1340. {
  1341. struct snd_rawmidi *rmidi;
  1342. int err;
  1343. static struct snd_device_ops ops = {
  1344. .dev_free = snd_rawmidi_dev_free,
  1345. .dev_register = snd_rawmidi_dev_register,
  1346. .dev_disconnect = snd_rawmidi_dev_disconnect,
  1347. };
  1348. if (snd_BUG_ON(!card))
  1349. return -ENXIO;
  1350. if (rrawmidi)
  1351. *rrawmidi = NULL;
  1352. rmidi = kzalloc(sizeof(*rmidi), GFP_KERNEL);
  1353. if (rmidi == NULL) {
  1354. snd_printk(KERN_ERR "rawmidi: cannot allocate\n");
  1355. return -ENOMEM;
  1356. }
  1357. rmidi->card = card;
  1358. rmidi->device = device;
  1359. mutex_init(&rmidi->open_mutex);
  1360. init_waitqueue_head(&rmidi->open_wait);
  1361. INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams);
  1362. INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams);
  1363. if (id != NULL)
  1364. strlcpy(rmidi->id, id, sizeof(rmidi->id));
  1365. if ((err = snd_rawmidi_alloc_substreams(rmidi,
  1366. &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT],
  1367. SNDRV_RAWMIDI_STREAM_INPUT,
  1368. input_count)) < 0) {
  1369. snd_rawmidi_free(rmidi);
  1370. return err;
  1371. }
  1372. if ((err = snd_rawmidi_alloc_substreams(rmidi,
  1373. &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT],
  1374. SNDRV_RAWMIDI_STREAM_OUTPUT,
  1375. output_count)) < 0) {
  1376. snd_rawmidi_free(rmidi);
  1377. return err;
  1378. }
  1379. if ((err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops)) < 0) {
  1380. snd_rawmidi_free(rmidi);
  1381. return err;
  1382. }
  1383. if (rrawmidi)
  1384. *rrawmidi = rmidi;
  1385. return 0;
  1386. }
  1387. static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *stream)
  1388. {
  1389. struct snd_rawmidi_substream *substream;
  1390. while (!list_empty(&stream->substreams)) {
  1391. substream = list_entry(stream->substreams.next, struct snd_rawmidi_substream, list);
  1392. list_del(&substream->list);
  1393. kfree(substream);
  1394. }
  1395. }
  1396. static int snd_rawmidi_free(struct snd_rawmidi *rmidi)
  1397. {
  1398. if (!rmidi)
  1399. return 0;
  1400. snd_info_free_entry(rmidi->proc_entry);
  1401. rmidi->proc_entry = NULL;
  1402. mutex_lock(&register_mutex);
  1403. if (rmidi->ops && rmidi->ops->dev_unregister)
  1404. rmidi->ops->dev_unregister(rmidi);
  1405. mutex_unlock(&register_mutex);
  1406. snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
  1407. snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
  1408. if (rmidi->private_free)
  1409. rmidi->private_free(rmidi);
  1410. kfree(rmidi);
  1411. return 0;
  1412. }
  1413. static int snd_rawmidi_dev_free(struct snd_device *device)
  1414. {
  1415. struct snd_rawmidi *rmidi = device->device_data;
  1416. return snd_rawmidi_free(rmidi);
  1417. }
  1418. #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
  1419. static void snd_rawmidi_dev_seq_free(struct snd_seq_device *device)
  1420. {
  1421. struct snd_rawmidi *rmidi = device->private_data;
  1422. rmidi->seq_dev = NULL;
  1423. }
  1424. #endif
  1425. static int snd_rawmidi_dev_register(struct snd_device *device)
  1426. {
  1427. int err;
  1428. struct snd_info_entry *entry;
  1429. char name[16];
  1430. struct snd_rawmidi *rmidi = device->device_data;
  1431. if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
  1432. return -ENOMEM;
  1433. mutex_lock(&register_mutex);
  1434. if (snd_rawmidi_search(rmidi->card, rmidi->device)) {
  1435. mutex_unlock(&register_mutex);
  1436. return -EBUSY;
  1437. }
  1438. list_add_tail(&rmidi->list, &snd_rawmidi_devices);
  1439. sprintf(name, "midiC%iD%i", rmidi->card->number, rmidi->device);
  1440. if ((err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI,
  1441. rmidi->card, rmidi->device,
  1442. &snd_rawmidi_f_ops, rmidi, name)) < 0) {
  1443. snd_printk(KERN_ERR "unable to register rawmidi device %i:%i\n", rmidi->card->number, rmidi->device);
  1444. list_del(&rmidi->list);
  1445. mutex_unlock(&register_mutex);
  1446. return err;
  1447. }
  1448. if (rmidi->ops && rmidi->ops->dev_register &&
  1449. (err = rmidi->ops->dev_register(rmidi)) < 0) {
  1450. snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
  1451. list_del(&rmidi->list);
  1452. mutex_unlock(&register_mutex);
  1453. return err;
  1454. }
  1455. #ifdef CONFIG_SND_OSSEMUL
  1456. rmidi->ossreg = 0;
  1457. if ((int)rmidi->device == midi_map[rmidi->card->number]) {
  1458. if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
  1459. rmidi->card, 0, &snd_rawmidi_f_ops,
  1460. rmidi, name) < 0) {
  1461. snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 0);
  1462. } else {
  1463. rmidi->ossreg++;
  1464. #ifdef SNDRV_OSS_INFO_DEV_MIDI
  1465. snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number, rmidi->name);
  1466. #endif
  1467. }
  1468. }
  1469. if ((int)rmidi->device == amidi_map[rmidi->card->number]) {
  1470. if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
  1471. rmidi->card, 1, &snd_rawmidi_f_ops,
  1472. rmidi, name) < 0) {
  1473. snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i\n", rmidi->card->number, 1);
  1474. } else {
  1475. rmidi->ossreg++;
  1476. }
  1477. }
  1478. #endif /* CONFIG_SND_OSSEMUL */
  1479. mutex_unlock(&register_mutex);
  1480. sprintf(name, "midi%d", rmidi->device);
  1481. entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root);
  1482. if (entry) {
  1483. entry->private_data = rmidi;
  1484. entry->c.text.read = snd_rawmidi_proc_info_read;
  1485. if (snd_info_register(entry) < 0) {
  1486. snd_info_free_entry(entry);
  1487. entry = NULL;
  1488. }
  1489. }
  1490. rmidi->proc_entry = entry;
  1491. #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
  1492. if (!rmidi->ops || !rmidi->ops->dev_register) { /* own registration mechanism */
  1493. if (snd_seq_device_new(rmidi->card, rmidi->device, SNDRV_SEQ_DEV_ID_MIDISYNTH, 0, &rmidi->seq_dev) >= 0) {
  1494. rmidi->seq_dev->private_data = rmidi;
  1495. rmidi->seq_dev->private_free = snd_rawmidi_dev_seq_free;
  1496. sprintf(rmidi->seq_dev->name, "MIDI %d-%d", rmidi->card->number, rmidi->device);
  1497. snd_device_register(rmidi->card, rmidi->seq_dev);
  1498. }
  1499. }
  1500. #endif
  1501. return 0;
  1502. }
  1503. static int snd_rawmidi_dev_disconnect(struct snd_device *device)
  1504. {
  1505. struct snd_rawmidi *rmidi = device->device_data;
  1506. int dir;
  1507. mutex_lock(&register_mutex);
  1508. mutex_lock(&rmidi->open_mutex);
  1509. wake_up(&rmidi->open_wait);
  1510. list_del_init(&rmidi->list);
  1511. for (dir = 0; dir < 2; dir++) {
  1512. struct snd_rawmidi_substream *s;
  1513. list_for_each_entry(s, &rmidi->streams[dir].substreams, list) {
  1514. if (s->runtime)
  1515. wake_up(&s->runtime->sleep);
  1516. }
  1517. }
  1518. #ifdef CONFIG_SND_OSSEMUL
  1519. if (rmidi->ossreg) {
  1520. if ((int)rmidi->device == midi_map[rmidi->card->number]) {
  1521. snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0);
  1522. #ifdef SNDRV_OSS_INFO_DEV_MIDI
  1523. snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number);
  1524. #endif
  1525. }
  1526. if ((int)rmidi->device == amidi_map[rmidi->card->number])
  1527. snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1);
  1528. rmidi->ossreg = 0;
  1529. }
  1530. #endif /* CONFIG_SND_OSSEMUL */
  1531. snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
  1532. mutex_unlock(&rmidi->open_mutex);
  1533. mutex_unlock(&register_mutex);
  1534. return 0;
  1535. }
  1536. /**
  1537. * snd_rawmidi_set_ops - set the rawmidi operators
  1538. * @rmidi: the rawmidi instance
  1539. * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
  1540. * @ops: the operator table
  1541. *
  1542. * Sets the rawmidi operators for the given stream direction.
  1543. */
  1544. void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream,
  1545. struct snd_rawmidi_ops *ops)
  1546. {
  1547. struct snd_rawmidi_substream *substream;
  1548. list_for_each_entry(substream, &rmidi->streams[stream].substreams, list)
  1549. substream->ops = ops;
  1550. }
  1551. /*
  1552. * ENTRY functions
  1553. */
  1554. static int __init alsa_rawmidi_init(void)
  1555. {
  1556. snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
  1557. snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl);
  1558. #ifdef CONFIG_SND_OSSEMUL
  1559. { int i;
  1560. /* check device map table */
  1561. for (i = 0; i < SNDRV_CARDS; i++) {
  1562. if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
  1563. snd_printk(KERN_ERR "invalid midi_map[%d] = %d\n", i, midi_map[i]);
  1564. midi_map[i] = 0;
  1565. }
  1566. if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
  1567. snd_printk(KERN_ERR "invalid amidi_map[%d] = %d\n", i, amidi_map[i]);
  1568. amidi_map[i] = 1;
  1569. }
  1570. }
  1571. }
  1572. #endif /* CONFIG_SND_OSSEMUL */
  1573. return 0;
  1574. }
  1575. static void __exit alsa_rawmidi_exit(void)
  1576. {
  1577. snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
  1578. snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl);
  1579. }
  1580. module_init(alsa_rawmidi_init)
  1581. module_exit(alsa_rawmidi_exit)
  1582. EXPORT_SYMBOL(snd_rawmidi_output_params);
  1583. EXPORT_SYMBOL(snd_rawmidi_input_params);
  1584. EXPORT_SYMBOL(snd_rawmidi_drop_output);
  1585. EXPORT_SYMBOL(snd_rawmidi_drain_output);
  1586. EXPORT_SYMBOL(snd_rawmidi_drain_input);
  1587. EXPORT_SYMBOL(snd_rawmidi_receive);
  1588. EXPORT_SYMBOL(snd_rawmidi_transmit_empty);
  1589. EXPORT_SYMBOL(snd_rawmidi_transmit_peek);
  1590. EXPORT_SYMBOL(snd_rawmidi_transmit_ack);
  1591. EXPORT_SYMBOL(snd_rawmidi_transmit);
  1592. EXPORT_SYMBOL(snd_rawmidi_new);
  1593. EXPORT_SYMBOL(snd_rawmidi_set_ops);
  1594. EXPORT_SYMBOL(snd_rawmidi_info_select);
  1595. EXPORT_SYMBOL(snd_rawmidi_kernel_open);
  1596. EXPORT_SYMBOL(snd_rawmidi_kernel_release);
  1597. EXPORT_SYMBOL(snd_rawmidi_kernel_read);
  1598. EXPORT_SYMBOL(snd_rawmidi_kernel_write);