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 = kzalloc(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. if (maj == snd_major) {
  354. cardnum = SNDRV_MINOR_CARD(iminor(inode));
  355. cardnum %= SNDRV_CARDS;
  356. device = SNDRV_MINOR_DEVICE(iminor(inode)) - SNDRV_MINOR_RAWMIDI;
  357. device %= SNDRV_MINOR_RAWMIDIS;
  358. #ifdef CONFIG_SND_OSSEMUL
  359. } else if (maj == SOUND_MAJOR) {
  360. cardnum = SNDRV_MINOR_OSS_CARD(iminor(inode));
  361. cardnum %= SNDRV_CARDS;
  362. device = SNDRV_MINOR_OSS_DEVICE(iminor(inode)) == SNDRV_MINOR_OSS_MIDI ?
  363. midi_map[cardnum] : amidi_map[cardnum];
  364. #endif
  365. } else
  366. return -ENXIO;
  367. rmidi = snd_rawmidi_devices[(cardnum * SNDRV_RAWMIDI_DEVICES) + device];
  368. if (rmidi == NULL)
  369. return -ENODEV;
  370. #ifdef CONFIG_SND_OSSEMUL
  371. if (maj == SOUND_MAJOR && !rmidi->ossreg)
  372. return -ENXIO;
  373. #endif
  374. if ((file->f_flags & O_APPEND) && !(file->f_flags & O_NONBLOCK))
  375. return -EINVAL; /* invalid combination */
  376. card = rmidi->card;
  377. err = snd_card_file_add(card, file);
  378. if (err < 0)
  379. return -ENODEV;
  380. fflags = snd_rawmidi_file_flags(file);
  381. if ((file->f_flags & O_APPEND) || maj == SOUND_MAJOR) /* OSS emul? */
  382. fflags |= SNDRV_RAWMIDI_LFLG_APPEND;
  383. fflags |= SNDRV_RAWMIDI_LFLG_NOOPENLOCK;
  384. rawmidi_file = kmalloc(sizeof(*rawmidi_file), GFP_KERNEL);
  385. if (rawmidi_file == NULL) {
  386. snd_card_file_remove(card, file);
  387. return -ENOMEM;
  388. }
  389. init_waitqueue_entry(&wait, current);
  390. add_wait_queue(&rmidi->open_wait, &wait);
  391. down(&rmidi->open_mutex);
  392. while (1) {
  393. subdevice = -1;
  394. down_read(&card->controls_rwsem);
  395. list_for_each(list, &card->ctl_files) {
  396. kctl = snd_ctl_file(list);
  397. if (kctl->pid == current->pid) {
  398. subdevice = kctl->prefer_rawmidi_subdevice;
  399. break;
  400. }
  401. }
  402. up_read(&card->controls_rwsem);
  403. err = snd_rawmidi_kernel_open(cardnum, device, subdevice, fflags, rawmidi_file);
  404. if (err >= 0)
  405. break;
  406. if (err == -EAGAIN) {
  407. if (file->f_flags & O_NONBLOCK) {
  408. err = -EBUSY;
  409. break;
  410. }
  411. } else
  412. break;
  413. set_current_state(TASK_INTERRUPTIBLE);
  414. up(&rmidi->open_mutex);
  415. schedule();
  416. down(&rmidi->open_mutex);
  417. if (signal_pending(current)) {
  418. err = -ERESTARTSYS;
  419. break;
  420. }
  421. }
  422. #ifdef CONFIG_SND_OSSEMUL
  423. if (rawmidi_file->input && rawmidi_file->input->runtime)
  424. rawmidi_file->input->runtime->oss = (maj == SOUND_MAJOR);
  425. if (rawmidi_file->output && rawmidi_file->output->runtime)
  426. rawmidi_file->output->runtime->oss = (maj == SOUND_MAJOR);
  427. #endif
  428. remove_wait_queue(&rmidi->open_wait, &wait);
  429. if (err >= 0) {
  430. file->private_data = rawmidi_file;
  431. } else {
  432. snd_card_file_remove(card, file);
  433. kfree(rawmidi_file);
  434. }
  435. up(&rmidi->open_mutex);
  436. return err;
  437. }
  438. int snd_rawmidi_kernel_release(snd_rawmidi_file_t * rfile)
  439. {
  440. snd_rawmidi_t *rmidi;
  441. snd_rawmidi_substream_t *substream;
  442. snd_rawmidi_runtime_t *runtime;
  443. snd_assert(rfile != NULL, return -ENXIO);
  444. snd_assert(rfile->input != NULL || rfile->output != NULL, return -ENXIO);
  445. rmidi = rfile->rmidi;
  446. down(&rmidi->open_mutex);
  447. if (rfile->input != NULL) {
  448. substream = rfile->input;
  449. rfile->input = NULL;
  450. runtime = substream->runtime;
  451. snd_rawmidi_input_trigger(substream, 0);
  452. substream->ops->close(substream);
  453. if (runtime->private_free != NULL)
  454. runtime->private_free(substream);
  455. snd_rawmidi_runtime_free(substream);
  456. substream->opened = 0;
  457. rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened--;
  458. }
  459. if (rfile->output != NULL) {
  460. substream = rfile->output;
  461. rfile->output = NULL;
  462. if (--substream->use_count == 0) {
  463. runtime = substream->runtime;
  464. if (substream->active_sensing) {
  465. unsigned char buf = 0xfe;
  466. /* sending single active sensing message to shut the device up */
  467. snd_rawmidi_kernel_write(substream, &buf, 1);
  468. }
  469. if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS)
  470. snd_rawmidi_output_trigger(substream, 0);
  471. substream->ops->close(substream);
  472. if (runtime->private_free != NULL)
  473. runtime->private_free(substream);
  474. snd_rawmidi_runtime_free(substream);
  475. substream->opened = 0;
  476. substream->append = 0;
  477. }
  478. rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substream_opened--;
  479. }
  480. up(&rmidi->open_mutex);
  481. module_put(rmidi->card->module);
  482. return 0;
  483. }
  484. static int snd_rawmidi_release(struct inode *inode, struct file *file)
  485. {
  486. snd_rawmidi_file_t *rfile;
  487. snd_rawmidi_t *rmidi;
  488. int err;
  489. rfile = file->private_data;
  490. err = snd_rawmidi_kernel_release(rfile);
  491. rmidi = rfile->rmidi;
  492. wake_up(&rmidi->open_wait);
  493. kfree(rfile);
  494. snd_card_file_remove(rmidi->card, file);
  495. return err;
  496. }
  497. int snd_rawmidi_info(snd_rawmidi_substream_t *substream, snd_rawmidi_info_t *info)
  498. {
  499. snd_rawmidi_t *rmidi;
  500. if (substream == NULL)
  501. return -ENODEV;
  502. rmidi = substream->rmidi;
  503. memset(info, 0, sizeof(*info));
  504. info->card = rmidi->card->number;
  505. info->device = rmidi->device;
  506. info->subdevice = substream->number;
  507. info->stream = substream->stream;
  508. info->flags = rmidi->info_flags;
  509. strcpy(info->id, rmidi->id);
  510. strcpy(info->name, rmidi->name);
  511. strcpy(info->subname, substream->name);
  512. info->subdevices_count = substream->pstr->substream_count;
  513. info->subdevices_avail = (substream->pstr->substream_count -
  514. substream->pstr->substream_opened);
  515. return 0;
  516. }
  517. static int snd_rawmidi_info_user(snd_rawmidi_substream_t *substream, snd_rawmidi_info_t __user * _info)
  518. {
  519. snd_rawmidi_info_t info;
  520. int err;
  521. if ((err = snd_rawmidi_info(substream, &info)) < 0)
  522. return err;
  523. if (copy_to_user(_info, &info, sizeof(snd_rawmidi_info_t)))
  524. return -EFAULT;
  525. return 0;
  526. }
  527. int snd_rawmidi_info_select(snd_card_t *card, snd_rawmidi_info_t *info)
  528. {
  529. snd_rawmidi_t *rmidi;
  530. snd_rawmidi_str_t *pstr;
  531. snd_rawmidi_substream_t *substream;
  532. struct list_head *list;
  533. if (info->device >= SNDRV_RAWMIDI_DEVICES)
  534. return -ENXIO;
  535. rmidi = snd_rawmidi_devices[card->number * SNDRV_RAWMIDI_DEVICES + info->device];
  536. if (info->stream < 0 || info->stream > 1)
  537. return -EINVAL;
  538. pstr = &rmidi->streams[info->stream];
  539. if (pstr->substream_count == 0)
  540. return -ENOENT;
  541. if (info->subdevice >= pstr->substream_count)
  542. return -ENXIO;
  543. list_for_each(list, &pstr->substreams) {
  544. substream = list_entry(list, snd_rawmidi_substream_t, list);
  545. if ((unsigned int)substream->number == info->subdevice)
  546. return snd_rawmidi_info(substream, info);
  547. }
  548. return -ENXIO;
  549. }
  550. static int snd_rawmidi_info_select_user(snd_card_t *card,
  551. snd_rawmidi_info_t __user *_info)
  552. {
  553. int err;
  554. snd_rawmidi_info_t info;
  555. if (get_user(info.device, &_info->device))
  556. return -EFAULT;
  557. if (get_user(info.stream, &_info->stream))
  558. return -EFAULT;
  559. if (get_user(info.subdevice, &_info->subdevice))
  560. return -EFAULT;
  561. if ((err = snd_rawmidi_info_select(card, &info)) < 0)
  562. return err;
  563. if (copy_to_user(_info, &info, sizeof(snd_rawmidi_info_t)))
  564. return -EFAULT;
  565. return 0;
  566. }
  567. int snd_rawmidi_output_params(snd_rawmidi_substream_t * substream,
  568. snd_rawmidi_params_t * params)
  569. {
  570. char *newbuf;
  571. snd_rawmidi_runtime_t *runtime = substream->runtime;
  572. if (substream->append && substream->use_count > 1)
  573. return -EBUSY;
  574. snd_rawmidi_drain_output(substream);
  575. if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
  576. return -EINVAL;
  577. }
  578. if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
  579. return -EINVAL;
  580. }
  581. if (params->buffer_size != runtime->buffer_size) {
  582. if ((newbuf = (char *) kmalloc(params->buffer_size, GFP_KERNEL)) == NULL)
  583. return -ENOMEM;
  584. kfree(runtime->buffer);
  585. runtime->buffer = newbuf;
  586. runtime->buffer_size = params->buffer_size;
  587. }
  588. runtime->avail_min = params->avail_min;
  589. substream->active_sensing = !params->no_active_sensing;
  590. return 0;
  591. }
  592. int snd_rawmidi_input_params(snd_rawmidi_substream_t * substream,
  593. snd_rawmidi_params_t * params)
  594. {
  595. char *newbuf;
  596. snd_rawmidi_runtime_t *runtime = substream->runtime;
  597. snd_rawmidi_drain_input(substream);
  598. if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
  599. return -EINVAL;
  600. }
  601. if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
  602. return -EINVAL;
  603. }
  604. if (params->buffer_size != runtime->buffer_size) {
  605. if ((newbuf = (char *) kmalloc(params->buffer_size, GFP_KERNEL)) == NULL)
  606. return -ENOMEM;
  607. kfree(runtime->buffer);
  608. runtime->buffer = newbuf;
  609. runtime->buffer_size = params->buffer_size;
  610. }
  611. runtime->avail_min = params->avail_min;
  612. return 0;
  613. }
  614. static int snd_rawmidi_output_status(snd_rawmidi_substream_t * substream,
  615. snd_rawmidi_status_t * status)
  616. {
  617. snd_rawmidi_runtime_t *runtime = substream->runtime;
  618. memset(status, 0, sizeof(*status));
  619. status->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
  620. spin_lock_irq(&runtime->lock);
  621. status->avail = runtime->avail;
  622. spin_unlock_irq(&runtime->lock);
  623. return 0;
  624. }
  625. static int snd_rawmidi_input_status(snd_rawmidi_substream_t * substream,
  626. snd_rawmidi_status_t * status)
  627. {
  628. snd_rawmidi_runtime_t *runtime = substream->runtime;
  629. memset(status, 0, sizeof(*status));
  630. status->stream = SNDRV_RAWMIDI_STREAM_INPUT;
  631. spin_lock_irq(&runtime->lock);
  632. status->avail = runtime->avail;
  633. status->xruns = runtime->xruns;
  634. runtime->xruns = 0;
  635. spin_unlock_irq(&runtime->lock);
  636. return 0;
  637. }
  638. static long snd_rawmidi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  639. {
  640. snd_rawmidi_file_t *rfile;
  641. void __user *argp = (void __user *)arg;
  642. rfile = file->private_data;
  643. if (((cmd >> 8) & 0xff) != 'W')
  644. return -ENOTTY;
  645. switch (cmd) {
  646. case SNDRV_RAWMIDI_IOCTL_PVERSION:
  647. return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0;
  648. case SNDRV_RAWMIDI_IOCTL_INFO:
  649. {
  650. snd_rawmidi_stream_t stream;
  651. snd_rawmidi_info_t __user *info = argp;
  652. if (get_user(stream, &info->stream))
  653. return -EFAULT;
  654. switch (stream) {
  655. case SNDRV_RAWMIDI_STREAM_INPUT:
  656. return snd_rawmidi_info_user(rfile->input, info);
  657. case SNDRV_RAWMIDI_STREAM_OUTPUT:
  658. return snd_rawmidi_info_user(rfile->output, info);
  659. default:
  660. return -EINVAL;
  661. }
  662. }
  663. case SNDRV_RAWMIDI_IOCTL_PARAMS:
  664. {
  665. snd_rawmidi_params_t params;
  666. if (copy_from_user(&params, argp, sizeof(snd_rawmidi_params_t)))
  667. return -EFAULT;
  668. switch (params.stream) {
  669. case SNDRV_RAWMIDI_STREAM_OUTPUT:
  670. if (rfile->output == NULL)
  671. return -EINVAL;
  672. return snd_rawmidi_output_params(rfile->output, &params);
  673. case SNDRV_RAWMIDI_STREAM_INPUT:
  674. if (rfile->input == NULL)
  675. return -EINVAL;
  676. return snd_rawmidi_input_params(rfile->input, &params);
  677. default:
  678. return -EINVAL;
  679. }
  680. }
  681. case SNDRV_RAWMIDI_IOCTL_STATUS:
  682. {
  683. int err = 0;
  684. snd_rawmidi_status_t status;
  685. if (copy_from_user(&status, argp, sizeof(snd_rawmidi_status_t)))
  686. return -EFAULT;
  687. switch (status.stream) {
  688. case SNDRV_RAWMIDI_STREAM_OUTPUT:
  689. if (rfile->output == NULL)
  690. return -EINVAL;
  691. err = snd_rawmidi_output_status(rfile->output, &status);
  692. break;
  693. case SNDRV_RAWMIDI_STREAM_INPUT:
  694. if (rfile->input == NULL)
  695. return -EINVAL;
  696. err = snd_rawmidi_input_status(rfile->input, &status);
  697. break;
  698. default:
  699. return -EINVAL;
  700. }
  701. if (err < 0)
  702. return err;
  703. if (copy_to_user(argp, &status, sizeof(snd_rawmidi_status_t)))
  704. return -EFAULT;
  705. return 0;
  706. }
  707. case SNDRV_RAWMIDI_IOCTL_DROP:
  708. {
  709. int val;
  710. if (get_user(val, (int __user *) argp))
  711. return -EFAULT;
  712. switch (val) {
  713. case SNDRV_RAWMIDI_STREAM_OUTPUT:
  714. if (rfile->output == NULL)
  715. return -EINVAL;
  716. return snd_rawmidi_drop_output(rfile->output);
  717. default:
  718. return -EINVAL;
  719. }
  720. }
  721. case SNDRV_RAWMIDI_IOCTL_DRAIN:
  722. {
  723. int val;
  724. if (get_user(val, (int __user *) argp))
  725. return -EFAULT;
  726. switch (val) {
  727. case SNDRV_RAWMIDI_STREAM_OUTPUT:
  728. if (rfile->output == NULL)
  729. return -EINVAL;
  730. return snd_rawmidi_drain_output(rfile->output);
  731. case SNDRV_RAWMIDI_STREAM_INPUT:
  732. if (rfile->input == NULL)
  733. return -EINVAL;
  734. return snd_rawmidi_drain_input(rfile->input);
  735. default:
  736. return -EINVAL;
  737. }
  738. }
  739. #ifdef CONFIG_SND_DEBUG
  740. default:
  741. snd_printk(KERN_WARNING "rawmidi: unknown command = 0x%x\n", cmd);
  742. #endif
  743. }
  744. return -ENOTTY;
  745. }
  746. static int snd_rawmidi_control_ioctl(snd_card_t * card,
  747. snd_ctl_file_t * control,
  748. unsigned int cmd,
  749. unsigned long arg)
  750. {
  751. void __user *argp = (void __user *)arg;
  752. unsigned int tmp;
  753. tmp = card->number * SNDRV_RAWMIDI_DEVICES;
  754. switch (cmd) {
  755. case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
  756. {
  757. int device;
  758. if (get_user(device, (int __user *)argp))
  759. return -EFAULT;
  760. device = device < 0 ? 0 : device + 1;
  761. while (device < SNDRV_RAWMIDI_DEVICES) {
  762. if (snd_rawmidi_devices[tmp + device])
  763. break;
  764. device++;
  765. }
  766. if (device == SNDRV_RAWMIDI_DEVICES)
  767. device = -1;
  768. if (put_user(device, (int __user *)argp))
  769. return -EFAULT;
  770. return 0;
  771. }
  772. case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
  773. {
  774. int val;
  775. if (get_user(val, (int __user *)argp))
  776. return -EFAULT;
  777. control->prefer_rawmidi_subdevice = val;
  778. return 0;
  779. }
  780. case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
  781. return snd_rawmidi_info_select_user(card, argp);
  782. }
  783. return -ENOIOCTLCMD;
  784. }
  785. /**
  786. * snd_rawmidi_receive - receive the input data from the device
  787. * @substream: the rawmidi substream
  788. * @buffer: the buffer pointer
  789. * @count: the data size to read
  790. *
  791. * Reads the data from the internal buffer.
  792. *
  793. * Returns the size of read data, or a negative error code on failure.
  794. */
  795. int snd_rawmidi_receive(snd_rawmidi_substream_t * substream, const unsigned char *buffer, int count)
  796. {
  797. unsigned long flags;
  798. int result = 0, count1;
  799. snd_rawmidi_runtime_t *runtime = substream->runtime;
  800. if (runtime->buffer == NULL) {
  801. snd_printd("snd_rawmidi_receive: input is not active!!!\n");
  802. return -EINVAL;
  803. }
  804. spin_lock_irqsave(&runtime->lock, flags);
  805. if (count == 1) { /* special case, faster code */
  806. substream->bytes++;
  807. if (runtime->avail < runtime->buffer_size) {
  808. runtime->buffer[runtime->hw_ptr++] = buffer[0];
  809. runtime->hw_ptr %= runtime->buffer_size;
  810. runtime->avail++;
  811. result++;
  812. } else {
  813. runtime->xruns++;
  814. }
  815. } else {
  816. substream->bytes += count;
  817. count1 = runtime->buffer_size - runtime->hw_ptr;
  818. if (count1 > count)
  819. count1 = count;
  820. if (count1 > (int)(runtime->buffer_size - runtime->avail))
  821. count1 = runtime->buffer_size - runtime->avail;
  822. memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
  823. runtime->hw_ptr += count1;
  824. runtime->hw_ptr %= runtime->buffer_size;
  825. runtime->avail += count1;
  826. count -= count1;
  827. result += count1;
  828. if (count > 0) {
  829. buffer += count1;
  830. count1 = count;
  831. if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
  832. count1 = runtime->buffer_size - runtime->avail;
  833. runtime->xruns += count - count1;
  834. }
  835. if (count1 > 0) {
  836. memcpy(runtime->buffer, buffer, count1);
  837. runtime->hw_ptr = count1;
  838. runtime->avail += count1;
  839. result += count1;
  840. }
  841. }
  842. }
  843. if (result > 0) {
  844. if (runtime->event)
  845. tasklet_hi_schedule(&runtime->tasklet);
  846. else if (snd_rawmidi_ready(substream))
  847. wake_up(&runtime->sleep);
  848. }
  849. spin_unlock_irqrestore(&runtime->lock, flags);
  850. return result;
  851. }
  852. static long snd_rawmidi_kernel_read1(snd_rawmidi_substream_t *substream,
  853. unsigned char *buf, long count, int kernel)
  854. {
  855. unsigned long flags;
  856. long result = 0, count1;
  857. snd_rawmidi_runtime_t *runtime = substream->runtime;
  858. while (count > 0 && runtime->avail) {
  859. count1 = runtime->buffer_size - runtime->appl_ptr;
  860. if (count1 > count)
  861. count1 = count;
  862. spin_lock_irqsave(&runtime->lock, flags);
  863. if (count1 > (int)runtime->avail)
  864. count1 = runtime->avail;
  865. if (kernel) {
  866. memcpy(buf + result, runtime->buffer + runtime->appl_ptr, count1);
  867. } else {
  868. spin_unlock_irqrestore(&runtime->lock, flags);
  869. if (copy_to_user((char __user *)buf + result,
  870. runtime->buffer + runtime->appl_ptr, count1)) {
  871. return result > 0 ? result : -EFAULT;
  872. }
  873. spin_lock_irqsave(&runtime->lock, flags);
  874. }
  875. runtime->appl_ptr += count1;
  876. runtime->appl_ptr %= runtime->buffer_size;
  877. runtime->avail -= count1;
  878. spin_unlock_irqrestore(&runtime->lock, flags);
  879. result += count1;
  880. count -= count1;
  881. }
  882. return result;
  883. }
  884. long snd_rawmidi_kernel_read(snd_rawmidi_substream_t *substream, unsigned char *buf, long count)
  885. {
  886. snd_rawmidi_input_trigger(substream, 1);
  887. return snd_rawmidi_kernel_read1(substream, buf, count, 1);
  888. }
  889. static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
  890. {
  891. long result;
  892. int count1;
  893. snd_rawmidi_file_t *rfile;
  894. snd_rawmidi_substream_t *substream;
  895. snd_rawmidi_runtime_t *runtime;
  896. rfile = file->private_data;
  897. substream = rfile->input;
  898. if (substream == NULL)
  899. return -EIO;
  900. runtime = substream->runtime;
  901. snd_rawmidi_input_trigger(substream, 1);
  902. result = 0;
  903. while (count > 0) {
  904. spin_lock_irq(&runtime->lock);
  905. while (!snd_rawmidi_ready(substream)) {
  906. wait_queue_t wait;
  907. if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
  908. spin_unlock_irq(&runtime->lock);
  909. return result > 0 ? result : -EAGAIN;
  910. }
  911. init_waitqueue_entry(&wait, current);
  912. add_wait_queue(&runtime->sleep, &wait);
  913. set_current_state(TASK_INTERRUPTIBLE);
  914. spin_unlock_irq(&runtime->lock);
  915. schedule();
  916. remove_wait_queue(&runtime->sleep, &wait);
  917. if (signal_pending(current))
  918. return result > 0 ? result : -ERESTARTSYS;
  919. if (!runtime->avail)
  920. return result > 0 ? result : -EIO;
  921. spin_lock_irq(&runtime->lock);
  922. }
  923. spin_unlock_irq(&runtime->lock);
  924. count1 = snd_rawmidi_kernel_read1(substream,
  925. (unsigned char __force *)buf,
  926. count, 0);
  927. if (count1 < 0)
  928. return result > 0 ? result : count1;
  929. result += count1;
  930. buf += count1;
  931. count -= count1;
  932. }
  933. return result;
  934. }
  935. /**
  936. * snd_rawmidi_transmit_empty - check whether the output buffer is empty
  937. * @substream: the rawmidi substream
  938. *
  939. * Returns 1 if the internal output buffer is empty, 0 if not.
  940. */
  941. int snd_rawmidi_transmit_empty(snd_rawmidi_substream_t * substream)
  942. {
  943. snd_rawmidi_runtime_t *runtime = substream->runtime;
  944. int result;
  945. unsigned long flags;
  946. if (runtime->buffer == NULL) {
  947. snd_printd("snd_rawmidi_transmit_empty: output is not active!!!\n");
  948. return 1;
  949. }
  950. spin_lock_irqsave(&runtime->lock, flags);
  951. result = runtime->avail >= runtime->buffer_size;
  952. spin_unlock_irqrestore(&runtime->lock, flags);
  953. return result;
  954. }
  955. /**
  956. * snd_rawmidi_transmit_peek - copy data from the internal buffer
  957. * @substream: the rawmidi substream
  958. * @buffer: the buffer pointer
  959. * @count: data size to transfer
  960. *
  961. * Copies data from the internal output buffer to the given buffer.
  962. *
  963. * Call this in the interrupt handler when the midi output is ready,
  964. * and call snd_rawmidi_transmit_ack() after the transmission is
  965. * finished.
  966. *
  967. * Returns the size of copied data, or a negative error code on failure.
  968. */
  969. int snd_rawmidi_transmit_peek(snd_rawmidi_substream_t * substream, unsigned char *buffer, int count)
  970. {
  971. unsigned long flags;
  972. int result, count1;
  973. snd_rawmidi_runtime_t *runtime = substream->runtime;
  974. if (runtime->buffer == NULL) {
  975. snd_printd("snd_rawmidi_transmit_peek: output is not active!!!\n");
  976. return -EINVAL;
  977. }
  978. result = 0;
  979. spin_lock_irqsave(&runtime->lock, flags);
  980. if (runtime->avail >= runtime->buffer_size) {
  981. /* warning: lowlevel layer MUST trigger down the hardware */
  982. goto __skip;
  983. }
  984. if (count == 1) { /* special case, faster code */
  985. *buffer = runtime->buffer[runtime->hw_ptr];
  986. result++;
  987. } else {
  988. count1 = runtime->buffer_size - runtime->hw_ptr;
  989. if (count1 > count)
  990. count1 = count;
  991. if (count1 > (int)(runtime->buffer_size - runtime->avail))
  992. count1 = runtime->buffer_size - runtime->avail;
  993. memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
  994. count -= count1;
  995. result += count1;
  996. if (count > 0) {
  997. if (count > (int)(runtime->buffer_size - runtime->avail - count1))
  998. count = runtime->buffer_size - runtime->avail - count1;
  999. memcpy(buffer + count1, runtime->buffer, count);
  1000. result += count;
  1001. }
  1002. }
  1003. __skip:
  1004. spin_unlock_irqrestore(&runtime->lock, flags);
  1005. return result;
  1006. }
  1007. /**
  1008. * snd_rawmidi_transmit_ack - acknowledge the transmission
  1009. * @substream: the rawmidi substream
  1010. * @count: the tranferred count
  1011. *
  1012. * Advances the hardware pointer for the internal output buffer with
  1013. * the given size and updates the condition.
  1014. * Call after the transmission is finished.
  1015. *
  1016. * Returns the advanced size if successful, or a negative error code on failure.
  1017. */
  1018. int snd_rawmidi_transmit_ack(snd_rawmidi_substream_t * substream, int count)
  1019. {
  1020. unsigned long flags;
  1021. snd_rawmidi_runtime_t *runtime = substream->runtime;
  1022. if (runtime->buffer == NULL) {
  1023. snd_printd("snd_rawmidi_transmit_ack: output is not active!!!\n");
  1024. return -EINVAL;
  1025. }
  1026. spin_lock_irqsave(&runtime->lock, flags);
  1027. snd_assert(runtime->avail + count <= runtime->buffer_size, );
  1028. runtime->hw_ptr += count;
  1029. runtime->hw_ptr %= runtime->buffer_size;
  1030. runtime->avail += count;
  1031. substream->bytes += count;
  1032. if (count > 0) {
  1033. if (runtime->drain || snd_rawmidi_ready(substream))
  1034. wake_up(&runtime->sleep);
  1035. }
  1036. spin_unlock_irqrestore(&runtime->lock, flags);
  1037. return count;
  1038. }
  1039. /**
  1040. * snd_rawmidi_transmit - copy from the buffer to the device
  1041. * @substream: the rawmidi substream
  1042. * @buffer: the buffer pointer
  1043. * @count: the data size to transfer
  1044. *
  1045. * Copies data from the buffer to the device and advances the pointer.
  1046. *
  1047. * Returns the copied size if successful, or a negative error code on failure.
  1048. */
  1049. int snd_rawmidi_transmit(snd_rawmidi_substream_t * substream, unsigned char *buffer, int count)
  1050. {
  1051. count = snd_rawmidi_transmit_peek(substream, buffer, count);
  1052. if (count < 0)
  1053. return count;
  1054. return snd_rawmidi_transmit_ack(substream, count);
  1055. }
  1056. static long snd_rawmidi_kernel_write1(snd_rawmidi_substream_t * substream, const unsigned char *buf, long count, int kernel)
  1057. {
  1058. unsigned long flags;
  1059. long count1, result;
  1060. snd_rawmidi_runtime_t *runtime = substream->runtime;
  1061. snd_assert(buf != NULL, return -EINVAL);
  1062. snd_assert(runtime->buffer != NULL, return -EINVAL);
  1063. result = 0;
  1064. spin_lock_irqsave(&runtime->lock, flags);
  1065. if (substream->append) {
  1066. if ((long)runtime->avail < count) {
  1067. spin_unlock_irqrestore(&runtime->lock, flags);
  1068. return -EAGAIN;
  1069. }
  1070. }
  1071. while (count > 0 && runtime->avail > 0) {
  1072. count1 = runtime->buffer_size - runtime->appl_ptr;
  1073. if (count1 > count)
  1074. count1 = count;
  1075. if (count1 > (long)runtime->avail)
  1076. count1 = runtime->avail;
  1077. if (kernel) {
  1078. memcpy(runtime->buffer + runtime->appl_ptr, buf, count1);
  1079. } else {
  1080. spin_unlock_irqrestore(&runtime->lock, flags);
  1081. if (copy_from_user(runtime->buffer + runtime->appl_ptr,
  1082. (char __user *)buf, count1)) {
  1083. spin_lock_irqsave(&runtime->lock, flags);
  1084. result = result > 0 ? result : -EFAULT;
  1085. goto __end;
  1086. }
  1087. spin_lock_irqsave(&runtime->lock, flags);
  1088. }
  1089. runtime->appl_ptr += count1;
  1090. runtime->appl_ptr %= runtime->buffer_size;
  1091. runtime->avail -= count1;
  1092. result += count1;
  1093. buf += count1;
  1094. count -= count1;
  1095. }
  1096. __end:
  1097. count1 = runtime->avail < runtime->buffer_size;
  1098. spin_unlock_irqrestore(&runtime->lock, flags);
  1099. if (count1)
  1100. snd_rawmidi_output_trigger(substream, 1);
  1101. return result;
  1102. }
  1103. long snd_rawmidi_kernel_write(snd_rawmidi_substream_t * substream, const unsigned char *buf, long count)
  1104. {
  1105. return snd_rawmidi_kernel_write1(substream, buf, count, 1);
  1106. }
  1107. static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
  1108. {
  1109. long result, timeout;
  1110. int count1;
  1111. snd_rawmidi_file_t *rfile;
  1112. snd_rawmidi_runtime_t *runtime;
  1113. snd_rawmidi_substream_t *substream;
  1114. rfile = file->private_data;
  1115. substream = rfile->output;
  1116. runtime = substream->runtime;
  1117. /* we cannot put an atomic message to our buffer */
  1118. if (substream->append && count > runtime->buffer_size)
  1119. return -EIO;
  1120. result = 0;
  1121. while (count > 0) {
  1122. spin_lock_irq(&runtime->lock);
  1123. while (!snd_rawmidi_ready_append(substream, count)) {
  1124. wait_queue_t wait;
  1125. if (file->f_flags & O_NONBLOCK) {
  1126. spin_unlock_irq(&runtime->lock);
  1127. return result > 0 ? result : -EAGAIN;
  1128. }
  1129. init_waitqueue_entry(&wait, current);
  1130. add_wait_queue(&runtime->sleep, &wait);
  1131. set_current_state(TASK_INTERRUPTIBLE);
  1132. spin_unlock_irq(&runtime->lock);
  1133. timeout = schedule_timeout(30 * HZ);
  1134. remove_wait_queue(&runtime->sleep, &wait);
  1135. if (signal_pending(current))
  1136. return result > 0 ? result : -ERESTARTSYS;
  1137. if (!runtime->avail && !timeout)
  1138. return result > 0 ? result : -EIO;
  1139. spin_lock_irq(&runtime->lock);
  1140. }
  1141. spin_unlock_irq(&runtime->lock);
  1142. count1 = snd_rawmidi_kernel_write1(substream,
  1143. (unsigned char __force *)buf,
  1144. 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 = kzalloc(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 = kzalloc(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);