rawmidi.c 47 KB

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