rawmidi.c 49 KB

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