rawmidi.c 48 KB

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