rawmidi.c 50 KB

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