rawmidi.c 49 KB

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