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