rawmidi.c 48 KB

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