rawmidi.c 47 KB

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