rawmidi.c 52 KB

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