rawmidi.c 49 KB

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