rawmidi.c 49 KB

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