rawmidi.c 50 KB

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