rawmidi.c 49 KB

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