rawmidi.c 58 KB

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