rawmidi.c 49 KB

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