rawmidi.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806
  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/signal.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_entry_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. static int __snd_rawmidi_info_select(struct snd_card *card,
  524. struct snd_rawmidi_info *info)
  525. {
  526. struct snd_rawmidi *rmidi;
  527. struct snd_rawmidi_str *pstr;
  528. struct snd_rawmidi_substream *substream;
  529. rmidi = snd_rawmidi_search(card, info->device);
  530. if (!rmidi)
  531. return -ENXIO;
  532. if (info->stream < 0 || info->stream > 1)
  533. return -EINVAL;
  534. pstr = &rmidi->streams[info->stream];
  535. if (pstr->substream_count == 0)
  536. return -ENOENT;
  537. if (info->subdevice >= pstr->substream_count)
  538. return -ENXIO;
  539. list_for_each_entry(substream, &pstr->substreams, list) {
  540. if ((unsigned int)substream->number == info->subdevice)
  541. return snd_rawmidi_info(substream, info);
  542. }
  543. return -ENXIO;
  544. }
  545. int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info)
  546. {
  547. int ret;
  548. mutex_lock(&register_mutex);
  549. ret = __snd_rawmidi_info_select(card, info);
  550. mutex_unlock(&register_mutex);
  551. return ret;
  552. }
  553. EXPORT_SYMBOL(snd_rawmidi_info_select);
  554. static int snd_rawmidi_info_select_user(struct snd_card *card,
  555. struct snd_rawmidi_info __user *_info)
  556. {
  557. int err;
  558. struct snd_rawmidi_info info;
  559. if (get_user(info.device, &_info->device))
  560. return -EFAULT;
  561. if (get_user(info.stream, &_info->stream))
  562. return -EFAULT;
  563. if (get_user(info.subdevice, &_info->subdevice))
  564. return -EFAULT;
  565. if ((err = snd_rawmidi_info_select(card, &info)) < 0)
  566. return err;
  567. if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
  568. return -EFAULT;
  569. return 0;
  570. }
  571. int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
  572. struct snd_rawmidi_params * params)
  573. {
  574. char *newbuf;
  575. struct snd_rawmidi_runtime *runtime = substream->runtime;
  576. if (substream->append && substream->use_count > 1)
  577. return -EBUSY;
  578. snd_rawmidi_drain_output(substream);
  579. if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
  580. return -EINVAL;
  581. }
  582. if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
  583. return -EINVAL;
  584. }
  585. if (params->buffer_size != runtime->buffer_size) {
  586. newbuf = krealloc(runtime->buffer, params->buffer_size,
  587. GFP_KERNEL);
  588. if (!newbuf)
  589. return -ENOMEM;
  590. runtime->buffer = newbuf;
  591. runtime->buffer_size = params->buffer_size;
  592. runtime->avail = runtime->buffer_size;
  593. }
  594. runtime->avail_min = params->avail_min;
  595. substream->active_sensing = !params->no_active_sensing;
  596. return 0;
  597. }
  598. EXPORT_SYMBOL(snd_rawmidi_output_params);
  599. int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
  600. struct snd_rawmidi_params * params)
  601. {
  602. char *newbuf;
  603. struct snd_rawmidi_runtime *runtime = substream->runtime;
  604. snd_rawmidi_drain_input(substream);
  605. if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
  606. return -EINVAL;
  607. }
  608. if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
  609. return -EINVAL;
  610. }
  611. if (params->buffer_size != runtime->buffer_size) {
  612. newbuf = krealloc(runtime->buffer, params->buffer_size,
  613. GFP_KERNEL);
  614. if (!newbuf)
  615. return -ENOMEM;
  616. runtime->buffer = newbuf;
  617. runtime->buffer_size = params->buffer_size;
  618. }
  619. runtime->avail_min = params->avail_min;
  620. return 0;
  621. }
  622. EXPORT_SYMBOL(snd_rawmidi_input_params);
  623. static int snd_rawmidi_output_status(struct snd_rawmidi_substream *substream,
  624. struct snd_rawmidi_status * status)
  625. {
  626. struct snd_rawmidi_runtime *runtime = substream->runtime;
  627. memset(status, 0, sizeof(*status));
  628. status->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
  629. spin_lock_irq(&runtime->lock);
  630. status->avail = runtime->avail;
  631. spin_unlock_irq(&runtime->lock);
  632. return 0;
  633. }
  634. static int snd_rawmidi_input_status(struct snd_rawmidi_substream *substream,
  635. struct snd_rawmidi_status * status)
  636. {
  637. struct snd_rawmidi_runtime *runtime = substream->runtime;
  638. memset(status, 0, sizeof(*status));
  639. status->stream = SNDRV_RAWMIDI_STREAM_INPUT;
  640. spin_lock_irq(&runtime->lock);
  641. status->avail = runtime->avail;
  642. status->xruns = runtime->xruns;
  643. runtime->xruns = 0;
  644. spin_unlock_irq(&runtime->lock);
  645. return 0;
  646. }
  647. static long snd_rawmidi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  648. {
  649. struct snd_rawmidi_file *rfile;
  650. void __user *argp = (void __user *)arg;
  651. rfile = file->private_data;
  652. if (((cmd >> 8) & 0xff) != 'W')
  653. return -ENOTTY;
  654. switch (cmd) {
  655. case SNDRV_RAWMIDI_IOCTL_PVERSION:
  656. return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0;
  657. case SNDRV_RAWMIDI_IOCTL_INFO:
  658. {
  659. int stream;
  660. struct snd_rawmidi_info __user *info = argp;
  661. if (get_user(stream, &info->stream))
  662. return -EFAULT;
  663. switch (stream) {
  664. case SNDRV_RAWMIDI_STREAM_INPUT:
  665. return snd_rawmidi_info_user(rfile->input, info);
  666. case SNDRV_RAWMIDI_STREAM_OUTPUT:
  667. return snd_rawmidi_info_user(rfile->output, info);
  668. default:
  669. return -EINVAL;
  670. }
  671. }
  672. case SNDRV_RAWMIDI_IOCTL_PARAMS:
  673. {
  674. struct snd_rawmidi_params params;
  675. if (copy_from_user(&params, argp, sizeof(struct snd_rawmidi_params)))
  676. return -EFAULT;
  677. switch (params.stream) {
  678. case SNDRV_RAWMIDI_STREAM_OUTPUT:
  679. if (rfile->output == NULL)
  680. return -EINVAL;
  681. return snd_rawmidi_output_params(rfile->output, &params);
  682. case SNDRV_RAWMIDI_STREAM_INPUT:
  683. if (rfile->input == NULL)
  684. return -EINVAL;
  685. return snd_rawmidi_input_params(rfile->input, &params);
  686. default:
  687. return -EINVAL;
  688. }
  689. }
  690. case SNDRV_RAWMIDI_IOCTL_STATUS:
  691. {
  692. int err = 0;
  693. struct snd_rawmidi_status status;
  694. if (copy_from_user(&status, argp, sizeof(struct snd_rawmidi_status)))
  695. return -EFAULT;
  696. switch (status.stream) {
  697. case SNDRV_RAWMIDI_STREAM_OUTPUT:
  698. if (rfile->output == NULL)
  699. return -EINVAL;
  700. err = snd_rawmidi_output_status(rfile->output, &status);
  701. break;
  702. case SNDRV_RAWMIDI_STREAM_INPUT:
  703. if (rfile->input == NULL)
  704. return -EINVAL;
  705. err = snd_rawmidi_input_status(rfile->input, &status);
  706. break;
  707. default:
  708. return -EINVAL;
  709. }
  710. if (err < 0)
  711. return err;
  712. if (copy_to_user(argp, &status, sizeof(struct snd_rawmidi_status)))
  713. return -EFAULT;
  714. return 0;
  715. }
  716. case SNDRV_RAWMIDI_IOCTL_DROP:
  717. {
  718. int val;
  719. if (get_user(val, (int __user *) argp))
  720. return -EFAULT;
  721. switch (val) {
  722. case SNDRV_RAWMIDI_STREAM_OUTPUT:
  723. if (rfile->output == NULL)
  724. return -EINVAL;
  725. return snd_rawmidi_drop_output(rfile->output);
  726. default:
  727. return -EINVAL;
  728. }
  729. }
  730. case SNDRV_RAWMIDI_IOCTL_DRAIN:
  731. {
  732. int val;
  733. if (get_user(val, (int __user *) argp))
  734. return -EFAULT;
  735. switch (val) {
  736. case SNDRV_RAWMIDI_STREAM_OUTPUT:
  737. if (rfile->output == NULL)
  738. return -EINVAL;
  739. return snd_rawmidi_drain_output(rfile->output);
  740. case SNDRV_RAWMIDI_STREAM_INPUT:
  741. if (rfile->input == NULL)
  742. return -EINVAL;
  743. return snd_rawmidi_drain_input(rfile->input);
  744. default:
  745. return -EINVAL;
  746. }
  747. }
  748. default:
  749. rmidi_dbg(rfile->rmidi,
  750. "rawmidi: unknown command = 0x%x\n", cmd);
  751. }
  752. return -ENOTTY;
  753. }
  754. static int snd_rawmidi_control_ioctl(struct snd_card *card,
  755. struct snd_ctl_file *control,
  756. unsigned int cmd,
  757. unsigned long arg)
  758. {
  759. void __user *argp = (void __user *)arg;
  760. switch (cmd) {
  761. case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
  762. {
  763. int device;
  764. if (get_user(device, (int __user *)argp))
  765. return -EFAULT;
  766. if (device >= SNDRV_RAWMIDI_DEVICES) /* next device is -1 */
  767. device = SNDRV_RAWMIDI_DEVICES - 1;
  768. mutex_lock(&register_mutex);
  769. device = device < 0 ? 0 : device + 1;
  770. while (device < SNDRV_RAWMIDI_DEVICES) {
  771. if (snd_rawmidi_search(card, device))
  772. break;
  773. device++;
  774. }
  775. if (device == SNDRV_RAWMIDI_DEVICES)
  776. device = -1;
  777. mutex_unlock(&register_mutex);
  778. if (put_user(device, (int __user *)argp))
  779. return -EFAULT;
  780. return 0;
  781. }
  782. case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
  783. {
  784. int val;
  785. if (get_user(val, (int __user *)argp))
  786. return -EFAULT;
  787. control->preferred_subdevice[SND_CTL_SUBDEV_RAWMIDI] = val;
  788. return 0;
  789. }
  790. case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
  791. return snd_rawmidi_info_select_user(card, argp);
  792. }
  793. return -ENOIOCTLCMD;
  794. }
  795. /**
  796. * snd_rawmidi_receive - receive the input data from the device
  797. * @substream: the rawmidi substream
  798. * @buffer: the buffer pointer
  799. * @count: the data size to read
  800. *
  801. * Reads the data from the internal buffer.
  802. *
  803. * Return: The size of read data, or a negative error code on failure.
  804. */
  805. int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
  806. const unsigned char *buffer, int count)
  807. {
  808. unsigned long flags;
  809. int result = 0, count1;
  810. struct snd_rawmidi_runtime *runtime = substream->runtime;
  811. if (!substream->opened)
  812. return -EBADFD;
  813. if (runtime->buffer == NULL) {
  814. rmidi_dbg(substream->rmidi,
  815. "snd_rawmidi_receive: input is not active!!!\n");
  816. return -EINVAL;
  817. }
  818. spin_lock_irqsave(&runtime->lock, flags);
  819. if (count == 1) { /* special case, faster code */
  820. substream->bytes++;
  821. if (runtime->avail < runtime->buffer_size) {
  822. runtime->buffer[runtime->hw_ptr++] = buffer[0];
  823. runtime->hw_ptr %= runtime->buffer_size;
  824. runtime->avail++;
  825. result++;
  826. } else {
  827. runtime->xruns++;
  828. }
  829. } else {
  830. substream->bytes += count;
  831. count1 = runtime->buffer_size - runtime->hw_ptr;
  832. if (count1 > count)
  833. count1 = count;
  834. if (count1 > (int)(runtime->buffer_size - runtime->avail))
  835. count1 = runtime->buffer_size - runtime->avail;
  836. memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
  837. runtime->hw_ptr += count1;
  838. runtime->hw_ptr %= runtime->buffer_size;
  839. runtime->avail += count1;
  840. count -= count1;
  841. result += count1;
  842. if (count > 0) {
  843. buffer += count1;
  844. count1 = count;
  845. if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
  846. count1 = runtime->buffer_size - runtime->avail;
  847. runtime->xruns += count - count1;
  848. }
  849. if (count1 > 0) {
  850. memcpy(runtime->buffer, buffer, count1);
  851. runtime->hw_ptr = count1;
  852. runtime->avail += count1;
  853. result += count1;
  854. }
  855. }
  856. }
  857. if (result > 0) {
  858. if (runtime->event)
  859. schedule_work(&runtime->event_work);
  860. else if (snd_rawmidi_ready(substream))
  861. wake_up(&runtime->sleep);
  862. }
  863. spin_unlock_irqrestore(&runtime->lock, flags);
  864. return result;
  865. }
  866. EXPORT_SYMBOL(snd_rawmidi_receive);
  867. static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream,
  868. unsigned char __user *userbuf,
  869. unsigned char *kernelbuf, long count)
  870. {
  871. unsigned long flags;
  872. long result = 0, count1;
  873. struct snd_rawmidi_runtime *runtime = substream->runtime;
  874. unsigned long appl_ptr;
  875. spin_lock_irqsave(&runtime->lock, flags);
  876. while (count > 0 && runtime->avail) {
  877. count1 = runtime->buffer_size - runtime->appl_ptr;
  878. if (count1 > count)
  879. count1 = count;
  880. if (count1 > (int)runtime->avail)
  881. count1 = runtime->avail;
  882. /* update runtime->appl_ptr before unlocking for userbuf */
  883. appl_ptr = runtime->appl_ptr;
  884. runtime->appl_ptr += count1;
  885. runtime->appl_ptr %= runtime->buffer_size;
  886. runtime->avail -= count1;
  887. if (kernelbuf)
  888. memcpy(kernelbuf + result, runtime->buffer + appl_ptr, count1);
  889. if (userbuf) {
  890. spin_unlock_irqrestore(&runtime->lock, flags);
  891. if (copy_to_user(userbuf + result,
  892. runtime->buffer + appl_ptr, count1)) {
  893. return result > 0 ? result : -EFAULT;
  894. }
  895. spin_lock_irqsave(&runtime->lock, flags);
  896. }
  897. result += count1;
  898. count -= count1;
  899. }
  900. spin_unlock_irqrestore(&runtime->lock, flags);
  901. return result;
  902. }
  903. long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
  904. unsigned char *buf, long count)
  905. {
  906. snd_rawmidi_input_trigger(substream, 1);
  907. return snd_rawmidi_kernel_read1(substream, NULL/*userbuf*/, buf, count);
  908. }
  909. EXPORT_SYMBOL(snd_rawmidi_kernel_read);
  910. static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count,
  911. loff_t *offset)
  912. {
  913. long result;
  914. int count1;
  915. struct snd_rawmidi_file *rfile;
  916. struct snd_rawmidi_substream *substream;
  917. struct snd_rawmidi_runtime *runtime;
  918. rfile = file->private_data;
  919. substream = rfile->input;
  920. if (substream == NULL)
  921. return -EIO;
  922. runtime = substream->runtime;
  923. snd_rawmidi_input_trigger(substream, 1);
  924. result = 0;
  925. while (count > 0) {
  926. spin_lock_irq(&runtime->lock);
  927. while (!snd_rawmidi_ready(substream)) {
  928. wait_queue_entry_t wait;
  929. if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
  930. spin_unlock_irq(&runtime->lock);
  931. return result > 0 ? result : -EAGAIN;
  932. }
  933. init_waitqueue_entry(&wait, current);
  934. add_wait_queue(&runtime->sleep, &wait);
  935. set_current_state(TASK_INTERRUPTIBLE);
  936. spin_unlock_irq(&runtime->lock);
  937. schedule();
  938. remove_wait_queue(&runtime->sleep, &wait);
  939. if (rfile->rmidi->card->shutdown)
  940. return -ENODEV;
  941. if (signal_pending(current))
  942. return result > 0 ? result : -ERESTARTSYS;
  943. if (!runtime->avail)
  944. return result > 0 ? result : -EIO;
  945. spin_lock_irq(&runtime->lock);
  946. }
  947. spin_unlock_irq(&runtime->lock);
  948. count1 = snd_rawmidi_kernel_read1(substream,
  949. (unsigned char __user *)buf,
  950. NULL/*kernelbuf*/,
  951. count);
  952. if (count1 < 0)
  953. return result > 0 ? result : count1;
  954. result += count1;
  955. buf += count1;
  956. count -= count1;
  957. }
  958. return result;
  959. }
  960. /**
  961. * snd_rawmidi_transmit_empty - check whether the output buffer is empty
  962. * @substream: the rawmidi substream
  963. *
  964. * Return: 1 if the internal output buffer is empty, 0 if not.
  965. */
  966. int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream)
  967. {
  968. struct snd_rawmidi_runtime *runtime = substream->runtime;
  969. int result;
  970. unsigned long flags;
  971. if (runtime->buffer == NULL) {
  972. rmidi_dbg(substream->rmidi,
  973. "snd_rawmidi_transmit_empty: output is not active!!!\n");
  974. return 1;
  975. }
  976. spin_lock_irqsave(&runtime->lock, flags);
  977. result = runtime->avail >= runtime->buffer_size;
  978. spin_unlock_irqrestore(&runtime->lock, flags);
  979. return result;
  980. }
  981. EXPORT_SYMBOL(snd_rawmidi_transmit_empty);
  982. /**
  983. * __snd_rawmidi_transmit_peek - copy data from the internal buffer
  984. * @substream: the rawmidi substream
  985. * @buffer: the buffer pointer
  986. * @count: data size to transfer
  987. *
  988. * This is a variant of snd_rawmidi_transmit_peek() without spinlock.
  989. */
  990. int __snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
  991. unsigned char *buffer, int count)
  992. {
  993. int result, count1;
  994. struct snd_rawmidi_runtime *runtime = substream->runtime;
  995. if (runtime->buffer == NULL) {
  996. rmidi_dbg(substream->rmidi,
  997. "snd_rawmidi_transmit_peek: output is not active!!!\n");
  998. return -EINVAL;
  999. }
  1000. result = 0;
  1001. if (runtime->avail >= runtime->buffer_size) {
  1002. /* warning: lowlevel layer MUST trigger down the hardware */
  1003. goto __skip;
  1004. }
  1005. if (count == 1) { /* special case, faster code */
  1006. *buffer = runtime->buffer[runtime->hw_ptr];
  1007. result++;
  1008. } else {
  1009. count1 = runtime->buffer_size - runtime->hw_ptr;
  1010. if (count1 > count)
  1011. count1 = count;
  1012. if (count1 > (int)(runtime->buffer_size - runtime->avail))
  1013. count1 = runtime->buffer_size - runtime->avail;
  1014. memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
  1015. count -= count1;
  1016. result += count1;
  1017. if (count > 0) {
  1018. if (count > (int)(runtime->buffer_size - runtime->avail - count1))
  1019. count = runtime->buffer_size - runtime->avail - count1;
  1020. memcpy(buffer + count1, runtime->buffer, count);
  1021. result += count;
  1022. }
  1023. }
  1024. __skip:
  1025. return result;
  1026. }
  1027. EXPORT_SYMBOL(__snd_rawmidi_transmit_peek);
  1028. /**
  1029. * snd_rawmidi_transmit_peek - copy data from the internal buffer
  1030. * @substream: the rawmidi substream
  1031. * @buffer: the buffer pointer
  1032. * @count: data size to transfer
  1033. *
  1034. * Copies data from the internal output buffer to the given buffer.
  1035. *
  1036. * Call this in the interrupt handler when the midi output is ready,
  1037. * and call snd_rawmidi_transmit_ack() after the transmission is
  1038. * finished.
  1039. *
  1040. * Return: The size of copied data, or a negative error code on failure.
  1041. */
  1042. int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
  1043. unsigned char *buffer, int count)
  1044. {
  1045. struct snd_rawmidi_runtime *runtime = substream->runtime;
  1046. int result;
  1047. unsigned long flags;
  1048. spin_lock_irqsave(&runtime->lock, flags);
  1049. result = __snd_rawmidi_transmit_peek(substream, buffer, count);
  1050. spin_unlock_irqrestore(&runtime->lock, flags);
  1051. return result;
  1052. }
  1053. EXPORT_SYMBOL(snd_rawmidi_transmit_peek);
  1054. /**
  1055. * __snd_rawmidi_transmit_ack - acknowledge the transmission
  1056. * @substream: the rawmidi substream
  1057. * @count: the transferred count
  1058. *
  1059. * This is a variant of __snd_rawmidi_transmit_ack() without spinlock.
  1060. */
  1061. int __snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
  1062. {
  1063. struct snd_rawmidi_runtime *runtime = substream->runtime;
  1064. if (runtime->buffer == NULL) {
  1065. rmidi_dbg(substream->rmidi,
  1066. "snd_rawmidi_transmit_ack: output is not active!!!\n");
  1067. return -EINVAL;
  1068. }
  1069. snd_BUG_ON(runtime->avail + count > runtime->buffer_size);
  1070. runtime->hw_ptr += count;
  1071. runtime->hw_ptr %= runtime->buffer_size;
  1072. runtime->avail += count;
  1073. substream->bytes += count;
  1074. if (count > 0) {
  1075. if (runtime->drain || snd_rawmidi_ready(substream))
  1076. wake_up(&runtime->sleep);
  1077. }
  1078. return count;
  1079. }
  1080. EXPORT_SYMBOL(__snd_rawmidi_transmit_ack);
  1081. /**
  1082. * snd_rawmidi_transmit_ack - acknowledge the transmission
  1083. * @substream: the rawmidi substream
  1084. * @count: the transferred count
  1085. *
  1086. * Advances the hardware pointer for the internal output buffer with
  1087. * the given size and updates the condition.
  1088. * Call after the transmission is finished.
  1089. *
  1090. * Return: The advanced size if successful, or a negative error code on failure.
  1091. */
  1092. int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
  1093. {
  1094. struct snd_rawmidi_runtime *runtime = substream->runtime;
  1095. int result;
  1096. unsigned long flags;
  1097. spin_lock_irqsave(&runtime->lock, flags);
  1098. result = __snd_rawmidi_transmit_ack(substream, count);
  1099. spin_unlock_irqrestore(&runtime->lock, flags);
  1100. return result;
  1101. }
  1102. EXPORT_SYMBOL(snd_rawmidi_transmit_ack);
  1103. /**
  1104. * snd_rawmidi_transmit - copy from the buffer to the device
  1105. * @substream: the rawmidi substream
  1106. * @buffer: the buffer pointer
  1107. * @count: the data size to transfer
  1108. *
  1109. * Copies data from the buffer to the device and advances the pointer.
  1110. *
  1111. * Return: The copied size if successful, or a negative error code on failure.
  1112. */
  1113. int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
  1114. unsigned char *buffer, int count)
  1115. {
  1116. struct snd_rawmidi_runtime *runtime = substream->runtime;
  1117. int result;
  1118. unsigned long flags;
  1119. spin_lock_irqsave(&runtime->lock, flags);
  1120. if (!substream->opened)
  1121. result = -EBADFD;
  1122. else {
  1123. count = __snd_rawmidi_transmit_peek(substream, buffer, count);
  1124. if (count <= 0)
  1125. result = count;
  1126. else
  1127. result = __snd_rawmidi_transmit_ack(substream, count);
  1128. }
  1129. spin_unlock_irqrestore(&runtime->lock, flags);
  1130. return result;
  1131. }
  1132. EXPORT_SYMBOL(snd_rawmidi_transmit);
  1133. static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream,
  1134. const unsigned char __user *userbuf,
  1135. const unsigned char *kernelbuf,
  1136. long count)
  1137. {
  1138. unsigned long flags;
  1139. long count1, result;
  1140. struct snd_rawmidi_runtime *runtime = substream->runtime;
  1141. unsigned long appl_ptr;
  1142. if (!kernelbuf && !userbuf)
  1143. return -EINVAL;
  1144. if (snd_BUG_ON(!runtime->buffer))
  1145. return -EINVAL;
  1146. result = 0;
  1147. spin_lock_irqsave(&runtime->lock, flags);
  1148. if (substream->append) {
  1149. if ((long)runtime->avail < count) {
  1150. spin_unlock_irqrestore(&runtime->lock, flags);
  1151. return -EAGAIN;
  1152. }
  1153. }
  1154. while (count > 0 && runtime->avail > 0) {
  1155. count1 = runtime->buffer_size - runtime->appl_ptr;
  1156. if (count1 > count)
  1157. count1 = count;
  1158. if (count1 > (long)runtime->avail)
  1159. count1 = runtime->avail;
  1160. /* update runtime->appl_ptr before unlocking for userbuf */
  1161. appl_ptr = runtime->appl_ptr;
  1162. runtime->appl_ptr += count1;
  1163. runtime->appl_ptr %= runtime->buffer_size;
  1164. runtime->avail -= count1;
  1165. if (kernelbuf)
  1166. memcpy(runtime->buffer + appl_ptr,
  1167. kernelbuf + result, count1);
  1168. else if (userbuf) {
  1169. spin_unlock_irqrestore(&runtime->lock, flags);
  1170. if (copy_from_user(runtime->buffer + appl_ptr,
  1171. userbuf + result, count1)) {
  1172. spin_lock_irqsave(&runtime->lock, flags);
  1173. result = result > 0 ? result : -EFAULT;
  1174. goto __end;
  1175. }
  1176. spin_lock_irqsave(&runtime->lock, flags);
  1177. }
  1178. result += count1;
  1179. count -= count1;
  1180. }
  1181. __end:
  1182. count1 = runtime->avail < runtime->buffer_size;
  1183. spin_unlock_irqrestore(&runtime->lock, flags);
  1184. if (count1)
  1185. snd_rawmidi_output_trigger(substream, 1);
  1186. return result;
  1187. }
  1188. long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream,
  1189. const unsigned char *buf, long count)
  1190. {
  1191. return snd_rawmidi_kernel_write1(substream, NULL, buf, count);
  1192. }
  1193. EXPORT_SYMBOL(snd_rawmidi_kernel_write);
  1194. static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf,
  1195. size_t count, loff_t *offset)
  1196. {
  1197. long result, timeout;
  1198. int count1;
  1199. struct snd_rawmidi_file *rfile;
  1200. struct snd_rawmidi_runtime *runtime;
  1201. struct snd_rawmidi_substream *substream;
  1202. rfile = file->private_data;
  1203. substream = rfile->output;
  1204. runtime = substream->runtime;
  1205. /* we cannot put an atomic message to our buffer */
  1206. if (substream->append && count > runtime->buffer_size)
  1207. return -EIO;
  1208. result = 0;
  1209. while (count > 0) {
  1210. spin_lock_irq(&runtime->lock);
  1211. while (!snd_rawmidi_ready_append(substream, count)) {
  1212. wait_queue_entry_t wait;
  1213. if (file->f_flags & O_NONBLOCK) {
  1214. spin_unlock_irq(&runtime->lock);
  1215. return result > 0 ? result : -EAGAIN;
  1216. }
  1217. init_waitqueue_entry(&wait, current);
  1218. add_wait_queue(&runtime->sleep, &wait);
  1219. set_current_state(TASK_INTERRUPTIBLE);
  1220. spin_unlock_irq(&runtime->lock);
  1221. timeout = schedule_timeout(30 * HZ);
  1222. remove_wait_queue(&runtime->sleep, &wait);
  1223. if (rfile->rmidi->card->shutdown)
  1224. return -ENODEV;
  1225. if (signal_pending(current))
  1226. return result > 0 ? result : -ERESTARTSYS;
  1227. if (!runtime->avail && !timeout)
  1228. return result > 0 ? result : -EIO;
  1229. spin_lock_irq(&runtime->lock);
  1230. }
  1231. spin_unlock_irq(&runtime->lock);
  1232. count1 = snd_rawmidi_kernel_write1(substream, buf, NULL, count);
  1233. if (count1 < 0)
  1234. return result > 0 ? result : count1;
  1235. result += count1;
  1236. buf += count1;
  1237. if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
  1238. break;
  1239. count -= count1;
  1240. }
  1241. if (file->f_flags & O_DSYNC) {
  1242. spin_lock_irq(&runtime->lock);
  1243. while (runtime->avail != runtime->buffer_size) {
  1244. wait_queue_entry_t wait;
  1245. unsigned int last_avail = runtime->avail;
  1246. init_waitqueue_entry(&wait, current);
  1247. add_wait_queue(&runtime->sleep, &wait);
  1248. set_current_state(TASK_INTERRUPTIBLE);
  1249. spin_unlock_irq(&runtime->lock);
  1250. timeout = schedule_timeout(30 * HZ);
  1251. remove_wait_queue(&runtime->sleep, &wait);
  1252. if (signal_pending(current))
  1253. return result > 0 ? result : -ERESTARTSYS;
  1254. if (runtime->avail == last_avail && !timeout)
  1255. return result > 0 ? result : -EIO;
  1256. spin_lock_irq(&runtime->lock);
  1257. }
  1258. spin_unlock_irq(&runtime->lock);
  1259. }
  1260. return result;
  1261. }
  1262. static __poll_t snd_rawmidi_poll(struct file *file, poll_table * wait)
  1263. {
  1264. struct snd_rawmidi_file *rfile;
  1265. struct snd_rawmidi_runtime *runtime;
  1266. __poll_t mask;
  1267. rfile = file->private_data;
  1268. if (rfile->input != NULL) {
  1269. runtime = rfile->input->runtime;
  1270. snd_rawmidi_input_trigger(rfile->input, 1);
  1271. poll_wait(file, &runtime->sleep, wait);
  1272. }
  1273. if (rfile->output != NULL) {
  1274. runtime = rfile->output->runtime;
  1275. poll_wait(file, &runtime->sleep, wait);
  1276. }
  1277. mask = 0;
  1278. if (rfile->input != NULL) {
  1279. if (snd_rawmidi_ready(rfile->input))
  1280. mask |= EPOLLIN | EPOLLRDNORM;
  1281. }
  1282. if (rfile->output != NULL) {
  1283. if (snd_rawmidi_ready(rfile->output))
  1284. mask |= EPOLLOUT | EPOLLWRNORM;
  1285. }
  1286. return mask;
  1287. }
  1288. /*
  1289. */
  1290. #ifdef CONFIG_COMPAT
  1291. #include "rawmidi_compat.c"
  1292. #else
  1293. #define snd_rawmidi_ioctl_compat NULL
  1294. #endif
  1295. /*
  1296. */
  1297. static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry,
  1298. struct snd_info_buffer *buffer)
  1299. {
  1300. struct snd_rawmidi *rmidi;
  1301. struct snd_rawmidi_substream *substream;
  1302. struct snd_rawmidi_runtime *runtime;
  1303. rmidi = entry->private_data;
  1304. snd_iprintf(buffer, "%s\n\n", rmidi->name);
  1305. mutex_lock(&rmidi->open_mutex);
  1306. if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
  1307. list_for_each_entry(substream,
  1308. &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
  1309. list) {
  1310. snd_iprintf(buffer,
  1311. "Output %d\n"
  1312. " Tx bytes : %lu\n",
  1313. substream->number,
  1314. (unsigned long) substream->bytes);
  1315. if (substream->opened) {
  1316. snd_iprintf(buffer,
  1317. " Owner PID : %d\n",
  1318. pid_vnr(substream->pid));
  1319. runtime = substream->runtime;
  1320. snd_iprintf(buffer,
  1321. " Mode : %s\n"
  1322. " Buffer size : %lu\n"
  1323. " Avail : %lu\n",
  1324. runtime->oss ? "OSS compatible" : "native",
  1325. (unsigned long) runtime->buffer_size,
  1326. (unsigned long) runtime->avail);
  1327. }
  1328. }
  1329. }
  1330. if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
  1331. list_for_each_entry(substream,
  1332. &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams,
  1333. list) {
  1334. snd_iprintf(buffer,
  1335. "Input %d\n"
  1336. " Rx bytes : %lu\n",
  1337. substream->number,
  1338. (unsigned long) substream->bytes);
  1339. if (substream->opened) {
  1340. snd_iprintf(buffer,
  1341. " Owner PID : %d\n",
  1342. pid_vnr(substream->pid));
  1343. runtime = substream->runtime;
  1344. snd_iprintf(buffer,
  1345. " Buffer size : %lu\n"
  1346. " Avail : %lu\n"
  1347. " Overruns : %lu\n",
  1348. (unsigned long) runtime->buffer_size,
  1349. (unsigned long) runtime->avail,
  1350. (unsigned long) runtime->xruns);
  1351. }
  1352. }
  1353. }
  1354. mutex_unlock(&rmidi->open_mutex);
  1355. }
  1356. /*
  1357. * Register functions
  1358. */
  1359. static const struct file_operations snd_rawmidi_f_ops =
  1360. {
  1361. .owner = THIS_MODULE,
  1362. .read = snd_rawmidi_read,
  1363. .write = snd_rawmidi_write,
  1364. .open = snd_rawmidi_open,
  1365. .release = snd_rawmidi_release,
  1366. .llseek = no_llseek,
  1367. .poll = snd_rawmidi_poll,
  1368. .unlocked_ioctl = snd_rawmidi_ioctl,
  1369. .compat_ioctl = snd_rawmidi_ioctl_compat,
  1370. };
  1371. static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi,
  1372. struct snd_rawmidi_str *stream,
  1373. int direction,
  1374. int count)
  1375. {
  1376. struct snd_rawmidi_substream *substream;
  1377. int idx;
  1378. for (idx = 0; idx < count; idx++) {
  1379. substream = kzalloc(sizeof(*substream), GFP_KERNEL);
  1380. if (!substream)
  1381. return -ENOMEM;
  1382. substream->stream = direction;
  1383. substream->number = idx;
  1384. substream->rmidi = rmidi;
  1385. substream->pstr = stream;
  1386. list_add_tail(&substream->list, &stream->substreams);
  1387. stream->substream_count++;
  1388. }
  1389. return 0;
  1390. }
  1391. static void release_rawmidi_device(struct device *dev)
  1392. {
  1393. kfree(container_of(dev, struct snd_rawmidi, dev));
  1394. }
  1395. /**
  1396. * snd_rawmidi_new - create a rawmidi instance
  1397. * @card: the card instance
  1398. * @id: the id string
  1399. * @device: the device index
  1400. * @output_count: the number of output streams
  1401. * @input_count: the number of input streams
  1402. * @rrawmidi: the pointer to store the new rawmidi instance
  1403. *
  1404. * Creates a new rawmidi instance.
  1405. * Use snd_rawmidi_set_ops() to set the operators to the new instance.
  1406. *
  1407. * Return: Zero if successful, or a negative error code on failure.
  1408. */
  1409. int snd_rawmidi_new(struct snd_card *card, char *id, int device,
  1410. int output_count, int input_count,
  1411. struct snd_rawmidi ** rrawmidi)
  1412. {
  1413. struct snd_rawmidi *rmidi;
  1414. int err;
  1415. static struct snd_device_ops ops = {
  1416. .dev_free = snd_rawmidi_dev_free,
  1417. .dev_register = snd_rawmidi_dev_register,
  1418. .dev_disconnect = snd_rawmidi_dev_disconnect,
  1419. };
  1420. if (snd_BUG_ON(!card))
  1421. return -ENXIO;
  1422. if (rrawmidi)
  1423. *rrawmidi = NULL;
  1424. rmidi = kzalloc(sizeof(*rmidi), GFP_KERNEL);
  1425. if (!rmidi)
  1426. return -ENOMEM;
  1427. rmidi->card = card;
  1428. rmidi->device = device;
  1429. mutex_init(&rmidi->open_mutex);
  1430. init_waitqueue_head(&rmidi->open_wait);
  1431. INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams);
  1432. INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams);
  1433. if (id != NULL)
  1434. strlcpy(rmidi->id, id, sizeof(rmidi->id));
  1435. snd_device_initialize(&rmidi->dev, card);
  1436. rmidi->dev.release = release_rawmidi_device;
  1437. dev_set_name(&rmidi->dev, "midiC%iD%i", card->number, device);
  1438. if ((err = snd_rawmidi_alloc_substreams(rmidi,
  1439. &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT],
  1440. SNDRV_RAWMIDI_STREAM_INPUT,
  1441. input_count)) < 0) {
  1442. snd_rawmidi_free(rmidi);
  1443. return err;
  1444. }
  1445. if ((err = snd_rawmidi_alloc_substreams(rmidi,
  1446. &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT],
  1447. SNDRV_RAWMIDI_STREAM_OUTPUT,
  1448. output_count)) < 0) {
  1449. snd_rawmidi_free(rmidi);
  1450. return err;
  1451. }
  1452. if ((err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops)) < 0) {
  1453. snd_rawmidi_free(rmidi);
  1454. return err;
  1455. }
  1456. if (rrawmidi)
  1457. *rrawmidi = rmidi;
  1458. return 0;
  1459. }
  1460. EXPORT_SYMBOL(snd_rawmidi_new);
  1461. static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *stream)
  1462. {
  1463. struct snd_rawmidi_substream *substream;
  1464. while (!list_empty(&stream->substreams)) {
  1465. substream = list_entry(stream->substreams.next, struct snd_rawmidi_substream, list);
  1466. list_del(&substream->list);
  1467. kfree(substream);
  1468. }
  1469. }
  1470. static int snd_rawmidi_free(struct snd_rawmidi *rmidi)
  1471. {
  1472. if (!rmidi)
  1473. return 0;
  1474. snd_info_free_entry(rmidi->proc_entry);
  1475. rmidi->proc_entry = NULL;
  1476. mutex_lock(&register_mutex);
  1477. if (rmidi->ops && rmidi->ops->dev_unregister)
  1478. rmidi->ops->dev_unregister(rmidi);
  1479. mutex_unlock(&register_mutex);
  1480. snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
  1481. snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
  1482. if (rmidi->private_free)
  1483. rmidi->private_free(rmidi);
  1484. put_device(&rmidi->dev);
  1485. return 0;
  1486. }
  1487. static int snd_rawmidi_dev_free(struct snd_device *device)
  1488. {
  1489. struct snd_rawmidi *rmidi = device->device_data;
  1490. return snd_rawmidi_free(rmidi);
  1491. }
  1492. #if IS_ENABLED(CONFIG_SND_SEQUENCER)
  1493. static void snd_rawmidi_dev_seq_free(struct snd_seq_device *device)
  1494. {
  1495. struct snd_rawmidi *rmidi = device->private_data;
  1496. rmidi->seq_dev = NULL;
  1497. }
  1498. #endif
  1499. static int snd_rawmidi_dev_register(struct snd_device *device)
  1500. {
  1501. int err;
  1502. struct snd_info_entry *entry;
  1503. char name[16];
  1504. struct snd_rawmidi *rmidi = device->device_data;
  1505. if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
  1506. return -ENOMEM;
  1507. mutex_lock(&register_mutex);
  1508. if (snd_rawmidi_search(rmidi->card, rmidi->device)) {
  1509. mutex_unlock(&register_mutex);
  1510. return -EBUSY;
  1511. }
  1512. list_add_tail(&rmidi->list, &snd_rawmidi_devices);
  1513. mutex_unlock(&register_mutex);
  1514. err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI,
  1515. rmidi->card, rmidi->device,
  1516. &snd_rawmidi_f_ops, rmidi, &rmidi->dev);
  1517. if (err < 0) {
  1518. rmidi_err(rmidi, "unable to register\n");
  1519. mutex_lock(&register_mutex);
  1520. list_del(&rmidi->list);
  1521. mutex_unlock(&register_mutex);
  1522. return err;
  1523. }
  1524. if (rmidi->ops && rmidi->ops->dev_register &&
  1525. (err = rmidi->ops->dev_register(rmidi)) < 0) {
  1526. snd_unregister_device(&rmidi->dev);
  1527. mutex_lock(&register_mutex);
  1528. list_del(&rmidi->list);
  1529. mutex_unlock(&register_mutex);
  1530. return err;
  1531. }
  1532. #ifdef CONFIG_SND_OSSEMUL
  1533. rmidi->ossreg = 0;
  1534. if ((int)rmidi->device == midi_map[rmidi->card->number]) {
  1535. if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
  1536. rmidi->card, 0, &snd_rawmidi_f_ops,
  1537. rmidi) < 0) {
  1538. rmidi_err(rmidi,
  1539. "unable to register OSS rawmidi device %i:%i\n",
  1540. rmidi->card->number, 0);
  1541. } else {
  1542. rmidi->ossreg++;
  1543. #ifdef SNDRV_OSS_INFO_DEV_MIDI
  1544. snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number, rmidi->name);
  1545. #endif
  1546. }
  1547. }
  1548. if ((int)rmidi->device == amidi_map[rmidi->card->number]) {
  1549. if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
  1550. rmidi->card, 1, &snd_rawmidi_f_ops,
  1551. rmidi) < 0) {
  1552. rmidi_err(rmidi,
  1553. "unable to register OSS rawmidi device %i:%i\n",
  1554. rmidi->card->number, 1);
  1555. } else {
  1556. rmidi->ossreg++;
  1557. }
  1558. }
  1559. #endif /* CONFIG_SND_OSSEMUL */
  1560. sprintf(name, "midi%d", rmidi->device);
  1561. entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root);
  1562. if (entry) {
  1563. entry->private_data = rmidi;
  1564. entry->c.text.read = snd_rawmidi_proc_info_read;
  1565. if (snd_info_register(entry) < 0) {
  1566. snd_info_free_entry(entry);
  1567. entry = NULL;
  1568. }
  1569. }
  1570. rmidi->proc_entry = entry;
  1571. #if IS_ENABLED(CONFIG_SND_SEQUENCER)
  1572. if (!rmidi->ops || !rmidi->ops->dev_register) { /* own registration mechanism */
  1573. if (snd_seq_device_new(rmidi->card, rmidi->device, SNDRV_SEQ_DEV_ID_MIDISYNTH, 0, &rmidi->seq_dev) >= 0) {
  1574. rmidi->seq_dev->private_data = rmidi;
  1575. rmidi->seq_dev->private_free = snd_rawmidi_dev_seq_free;
  1576. sprintf(rmidi->seq_dev->name, "MIDI %d-%d", rmidi->card->number, rmidi->device);
  1577. snd_device_register(rmidi->card, rmidi->seq_dev);
  1578. }
  1579. }
  1580. #endif
  1581. return 0;
  1582. }
  1583. static int snd_rawmidi_dev_disconnect(struct snd_device *device)
  1584. {
  1585. struct snd_rawmidi *rmidi = device->device_data;
  1586. int dir;
  1587. mutex_lock(&register_mutex);
  1588. mutex_lock(&rmidi->open_mutex);
  1589. wake_up(&rmidi->open_wait);
  1590. list_del_init(&rmidi->list);
  1591. for (dir = 0; dir < 2; dir++) {
  1592. struct snd_rawmidi_substream *s;
  1593. list_for_each_entry(s, &rmidi->streams[dir].substreams, list) {
  1594. if (s->runtime)
  1595. wake_up(&s->runtime->sleep);
  1596. }
  1597. }
  1598. #ifdef CONFIG_SND_OSSEMUL
  1599. if (rmidi->ossreg) {
  1600. if ((int)rmidi->device == midi_map[rmidi->card->number]) {
  1601. snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0);
  1602. #ifdef SNDRV_OSS_INFO_DEV_MIDI
  1603. snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number);
  1604. #endif
  1605. }
  1606. if ((int)rmidi->device == amidi_map[rmidi->card->number])
  1607. snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1);
  1608. rmidi->ossreg = 0;
  1609. }
  1610. #endif /* CONFIG_SND_OSSEMUL */
  1611. snd_unregister_device(&rmidi->dev);
  1612. mutex_unlock(&rmidi->open_mutex);
  1613. mutex_unlock(&register_mutex);
  1614. return 0;
  1615. }
  1616. /**
  1617. * snd_rawmidi_set_ops - set the rawmidi operators
  1618. * @rmidi: the rawmidi instance
  1619. * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
  1620. * @ops: the operator table
  1621. *
  1622. * Sets the rawmidi operators for the given stream direction.
  1623. */
  1624. void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream,
  1625. const struct snd_rawmidi_ops *ops)
  1626. {
  1627. struct snd_rawmidi_substream *substream;
  1628. list_for_each_entry(substream, &rmidi->streams[stream].substreams, list)
  1629. substream->ops = ops;
  1630. }
  1631. EXPORT_SYMBOL(snd_rawmidi_set_ops);
  1632. /*
  1633. * ENTRY functions
  1634. */
  1635. static int __init alsa_rawmidi_init(void)
  1636. {
  1637. snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
  1638. snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl);
  1639. #ifdef CONFIG_SND_OSSEMUL
  1640. { int i;
  1641. /* check device map table */
  1642. for (i = 0; i < SNDRV_CARDS; i++) {
  1643. if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
  1644. pr_err("ALSA: rawmidi: invalid midi_map[%d] = %d\n",
  1645. i, midi_map[i]);
  1646. midi_map[i] = 0;
  1647. }
  1648. if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
  1649. pr_err("ALSA: rawmidi: invalid amidi_map[%d] = %d\n",
  1650. i, amidi_map[i]);
  1651. amidi_map[i] = 1;
  1652. }
  1653. }
  1654. }
  1655. #endif /* CONFIG_SND_OSSEMUL */
  1656. return 0;
  1657. }
  1658. static void __exit alsa_rawmidi_exit(void)
  1659. {
  1660. snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
  1661. snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl);
  1662. }
  1663. module_init(alsa_rawmidi_init)
  1664. module_exit(alsa_rawmidi_exit)