rawmidi.c 49 KB

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