rawmidi.c 49 KB

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