rawmidi.c 50 KB

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