rawmidi.c 48 KB

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