rawmidi.c 48 KB

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