rawmidi.c 49 KB

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