rawmidi.c 49 KB

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