rawmidi.c 50 KB

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