rawmidi.c 48 KB

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