seq_oss_midi.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. /*
  2. * OSS compatible sequencer driver
  3. *
  4. * MIDI device handlers
  5. *
  6. * Copyright (C) 1998,99 Takashi Iwai <tiwai@suse.de>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <sound/asoundef.h>
  23. #include "seq_oss_midi.h"
  24. #include "seq_oss_readq.h"
  25. #include "seq_oss_timer.h"
  26. #include "seq_oss_event.h"
  27. #include <sound/seq_midi_event.h>
  28. #include "../seq_lock.h"
  29. #include <linux/init.h>
  30. #include <linux/slab.h>
  31. /*
  32. * constants
  33. */
  34. #define SNDRV_SEQ_OSS_MAX_MIDI_NAME 30
  35. /*
  36. * definition of midi device record
  37. */
  38. struct seq_oss_midi {
  39. int seq_device; /* device number */
  40. int client; /* sequencer client number */
  41. int port; /* sequencer port number */
  42. unsigned int flags; /* port capability */
  43. int opened; /* flag for opening */
  44. unsigned char name[SNDRV_SEQ_OSS_MAX_MIDI_NAME];
  45. struct snd_midi_event *coder; /* MIDI event coder */
  46. struct seq_oss_devinfo *devinfo; /* assigned OSSseq device */
  47. snd_use_lock_t use_lock;
  48. };
  49. /*
  50. * midi device table
  51. */
  52. static int max_midi_devs;
  53. static struct seq_oss_midi *midi_devs[SNDRV_SEQ_OSS_MAX_MIDI_DEVS];
  54. static DEFINE_SPINLOCK(register_lock);
  55. /*
  56. * prototypes
  57. */
  58. static struct seq_oss_midi *get_mdev(int dev);
  59. static struct seq_oss_midi *get_mididev(struct seq_oss_devinfo *dp, int dev);
  60. static int send_synth_event(struct seq_oss_devinfo *dp, struct snd_seq_event *ev, int dev);
  61. static int send_midi_event(struct seq_oss_devinfo *dp, struct snd_seq_event *ev, struct seq_oss_midi *mdev);
  62. /*
  63. * look up the existing ports
  64. * this looks a very exhausting job.
  65. */
  66. int __init
  67. snd_seq_oss_midi_lookup_ports(int client)
  68. {
  69. struct snd_seq_client_info *clinfo;
  70. struct snd_seq_port_info *pinfo;
  71. clinfo = kzalloc(sizeof(*clinfo), GFP_KERNEL);
  72. pinfo = kzalloc(sizeof(*pinfo), GFP_KERNEL);
  73. if (! clinfo || ! pinfo) {
  74. kfree(clinfo);
  75. kfree(pinfo);
  76. return -ENOMEM;
  77. }
  78. clinfo->client = -1;
  79. while (snd_seq_kernel_client_ctl(client, SNDRV_SEQ_IOCTL_QUERY_NEXT_CLIENT, clinfo) == 0) {
  80. if (clinfo->client == client)
  81. continue; /* ignore myself */
  82. pinfo->addr.client = clinfo->client;
  83. pinfo->addr.port = -1;
  84. while (snd_seq_kernel_client_ctl(client, SNDRV_SEQ_IOCTL_QUERY_NEXT_PORT, pinfo) == 0)
  85. snd_seq_oss_midi_check_new_port(pinfo);
  86. }
  87. kfree(clinfo);
  88. kfree(pinfo);
  89. return 0;
  90. }
  91. /*
  92. */
  93. static struct seq_oss_midi *
  94. get_mdev(int dev)
  95. {
  96. struct seq_oss_midi *mdev;
  97. unsigned long flags;
  98. spin_lock_irqsave(&register_lock, flags);
  99. mdev = midi_devs[dev];
  100. if (mdev)
  101. snd_use_lock_use(&mdev->use_lock);
  102. spin_unlock_irqrestore(&register_lock, flags);
  103. return mdev;
  104. }
  105. /*
  106. * look for the identical slot
  107. */
  108. static struct seq_oss_midi *
  109. find_slot(int client, int port)
  110. {
  111. int i;
  112. struct seq_oss_midi *mdev;
  113. unsigned long flags;
  114. spin_lock_irqsave(&register_lock, flags);
  115. for (i = 0; i < max_midi_devs; i++) {
  116. mdev = midi_devs[i];
  117. if (mdev && mdev->client == client && mdev->port == port) {
  118. /* found! */
  119. snd_use_lock_use(&mdev->use_lock);
  120. spin_unlock_irqrestore(&register_lock, flags);
  121. return mdev;
  122. }
  123. }
  124. spin_unlock_irqrestore(&register_lock, flags);
  125. return NULL;
  126. }
  127. #define PERM_WRITE (SNDRV_SEQ_PORT_CAP_WRITE|SNDRV_SEQ_PORT_CAP_SUBS_WRITE)
  128. #define PERM_READ (SNDRV_SEQ_PORT_CAP_READ|SNDRV_SEQ_PORT_CAP_SUBS_READ)
  129. /*
  130. * register a new port if it doesn't exist yet
  131. */
  132. int
  133. snd_seq_oss_midi_check_new_port(struct snd_seq_port_info *pinfo)
  134. {
  135. int i;
  136. struct seq_oss_midi *mdev;
  137. unsigned long flags;
  138. debug_printk(("check for MIDI client %d port %d\n", pinfo->addr.client, pinfo->addr.port));
  139. /* the port must include generic midi */
  140. if (! (pinfo->type & SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC))
  141. return 0;
  142. /* either read or write subscribable */
  143. if ((pinfo->capability & PERM_WRITE) != PERM_WRITE &&
  144. (pinfo->capability & PERM_READ) != PERM_READ)
  145. return 0;
  146. /*
  147. * look for the identical slot
  148. */
  149. if ((mdev = find_slot(pinfo->addr.client, pinfo->addr.port)) != NULL) {
  150. /* already exists */
  151. snd_use_lock_free(&mdev->use_lock);
  152. return 0;
  153. }
  154. /*
  155. * allocate midi info record
  156. */
  157. if ((mdev = kzalloc(sizeof(*mdev), GFP_KERNEL)) == NULL) {
  158. snd_printk(KERN_ERR "can't malloc midi info\n");
  159. return -ENOMEM;
  160. }
  161. /* copy the port information */
  162. mdev->client = pinfo->addr.client;
  163. mdev->port = pinfo->addr.port;
  164. mdev->flags = pinfo->capability;
  165. mdev->opened = 0;
  166. snd_use_lock_init(&mdev->use_lock);
  167. /* copy and truncate the name of synth device */
  168. strlcpy(mdev->name, pinfo->name, sizeof(mdev->name));
  169. /* create MIDI coder */
  170. if (snd_midi_event_new(MAX_MIDI_EVENT_BUF, &mdev->coder) < 0) {
  171. snd_printk(KERN_ERR "can't malloc midi coder\n");
  172. kfree(mdev);
  173. return -ENOMEM;
  174. }
  175. /* OSS sequencer adds running status to all sequences */
  176. snd_midi_event_no_status(mdev->coder, 1);
  177. /*
  178. * look for en empty slot
  179. */
  180. spin_lock_irqsave(&register_lock, flags);
  181. for (i = 0; i < max_midi_devs; i++) {
  182. if (midi_devs[i] == NULL)
  183. break;
  184. }
  185. if (i >= max_midi_devs) {
  186. if (max_midi_devs >= SNDRV_SEQ_OSS_MAX_MIDI_DEVS) {
  187. spin_unlock_irqrestore(&register_lock, flags);
  188. snd_midi_event_free(mdev->coder);
  189. kfree(mdev);
  190. return -ENOMEM;
  191. }
  192. max_midi_devs++;
  193. }
  194. mdev->seq_device = i;
  195. midi_devs[mdev->seq_device] = mdev;
  196. spin_unlock_irqrestore(&register_lock, flags);
  197. return 0;
  198. }
  199. /*
  200. * release the midi device if it was registered
  201. */
  202. int
  203. snd_seq_oss_midi_check_exit_port(int client, int port)
  204. {
  205. struct seq_oss_midi *mdev;
  206. unsigned long flags;
  207. int index;
  208. if ((mdev = find_slot(client, port)) != NULL) {
  209. spin_lock_irqsave(&register_lock, flags);
  210. midi_devs[mdev->seq_device] = NULL;
  211. spin_unlock_irqrestore(&register_lock, flags);
  212. snd_use_lock_free(&mdev->use_lock);
  213. snd_use_lock_sync(&mdev->use_lock);
  214. if (mdev->coder)
  215. snd_midi_event_free(mdev->coder);
  216. kfree(mdev);
  217. }
  218. spin_lock_irqsave(&register_lock, flags);
  219. for (index = max_midi_devs - 1; index >= 0; index--) {
  220. if (midi_devs[index])
  221. break;
  222. }
  223. max_midi_devs = index + 1;
  224. spin_unlock_irqrestore(&register_lock, flags);
  225. return 0;
  226. }
  227. /*
  228. * release the midi device if it was registered
  229. */
  230. void
  231. snd_seq_oss_midi_clear_all(void)
  232. {
  233. int i;
  234. struct seq_oss_midi *mdev;
  235. unsigned long flags;
  236. spin_lock_irqsave(&register_lock, flags);
  237. for (i = 0; i < max_midi_devs; i++) {
  238. if ((mdev = midi_devs[i]) != NULL) {
  239. if (mdev->coder)
  240. snd_midi_event_free(mdev->coder);
  241. kfree(mdev);
  242. midi_devs[i] = NULL;
  243. }
  244. }
  245. max_midi_devs = 0;
  246. spin_unlock_irqrestore(&register_lock, flags);
  247. }
  248. /*
  249. * set up midi tables
  250. */
  251. void
  252. snd_seq_oss_midi_setup(struct seq_oss_devinfo *dp)
  253. {
  254. dp->max_mididev = max_midi_devs;
  255. }
  256. /*
  257. * clean up midi tables
  258. */
  259. void
  260. snd_seq_oss_midi_cleanup(struct seq_oss_devinfo *dp)
  261. {
  262. int i;
  263. for (i = 0; i < dp->max_mididev; i++)
  264. snd_seq_oss_midi_close(dp, i);
  265. dp->max_mididev = 0;
  266. }
  267. /*
  268. * open all midi devices. ignore errors.
  269. */
  270. void
  271. snd_seq_oss_midi_open_all(struct seq_oss_devinfo *dp, int file_mode)
  272. {
  273. int i;
  274. for (i = 0; i < dp->max_mididev; i++)
  275. snd_seq_oss_midi_open(dp, i, file_mode);
  276. }
  277. /*
  278. * get the midi device information
  279. */
  280. static struct seq_oss_midi *
  281. get_mididev(struct seq_oss_devinfo *dp, int dev)
  282. {
  283. if (dev < 0 || dev >= dp->max_mididev)
  284. return NULL;
  285. return get_mdev(dev);
  286. }
  287. /*
  288. * open the midi device if not opened yet
  289. */
  290. int
  291. snd_seq_oss_midi_open(struct seq_oss_devinfo *dp, int dev, int fmode)
  292. {
  293. int perm;
  294. struct seq_oss_midi *mdev;
  295. struct snd_seq_port_subscribe subs;
  296. if ((mdev = get_mididev(dp, dev)) == NULL)
  297. return -ENODEV;
  298. /* already used? */
  299. if (mdev->opened && mdev->devinfo != dp) {
  300. snd_use_lock_free(&mdev->use_lock);
  301. return -EBUSY;
  302. }
  303. perm = 0;
  304. if (is_write_mode(fmode))
  305. perm |= PERM_WRITE;
  306. if (is_read_mode(fmode))
  307. perm |= PERM_READ;
  308. perm &= mdev->flags;
  309. if (perm == 0) {
  310. snd_use_lock_free(&mdev->use_lock);
  311. return -ENXIO;
  312. }
  313. /* already opened? */
  314. if ((mdev->opened & perm) == perm) {
  315. snd_use_lock_free(&mdev->use_lock);
  316. return 0;
  317. }
  318. perm &= ~mdev->opened;
  319. memset(&subs, 0, sizeof(subs));
  320. if (perm & PERM_WRITE) {
  321. subs.sender = dp->addr;
  322. subs.dest.client = mdev->client;
  323. subs.dest.port = mdev->port;
  324. if (snd_seq_kernel_client_ctl(dp->cseq, SNDRV_SEQ_IOCTL_SUBSCRIBE_PORT, &subs) >= 0)
  325. mdev->opened |= PERM_WRITE;
  326. }
  327. if (perm & PERM_READ) {
  328. subs.sender.client = mdev->client;
  329. subs.sender.port = mdev->port;
  330. subs.dest = dp->addr;
  331. subs.flags = SNDRV_SEQ_PORT_SUBS_TIMESTAMP;
  332. subs.queue = dp->queue; /* queue for timestamps */
  333. if (snd_seq_kernel_client_ctl(dp->cseq, SNDRV_SEQ_IOCTL_SUBSCRIBE_PORT, &subs) >= 0)
  334. mdev->opened |= PERM_READ;
  335. }
  336. if (! mdev->opened) {
  337. snd_use_lock_free(&mdev->use_lock);
  338. return -ENXIO;
  339. }
  340. mdev->devinfo = dp;
  341. snd_use_lock_free(&mdev->use_lock);
  342. return 0;
  343. }
  344. /*
  345. * close the midi device if already opened
  346. */
  347. int
  348. snd_seq_oss_midi_close(struct seq_oss_devinfo *dp, int dev)
  349. {
  350. struct seq_oss_midi *mdev;
  351. struct snd_seq_port_subscribe subs;
  352. if ((mdev = get_mididev(dp, dev)) == NULL)
  353. return -ENODEV;
  354. if (! mdev->opened || mdev->devinfo != dp) {
  355. snd_use_lock_free(&mdev->use_lock);
  356. return 0;
  357. }
  358. debug_printk(("closing client %d port %d mode %d\n", mdev->client, mdev->port, mdev->opened));
  359. memset(&subs, 0, sizeof(subs));
  360. if (mdev->opened & PERM_WRITE) {
  361. subs.sender = dp->addr;
  362. subs.dest.client = mdev->client;
  363. subs.dest.port = mdev->port;
  364. snd_seq_kernel_client_ctl(dp->cseq, SNDRV_SEQ_IOCTL_UNSUBSCRIBE_PORT, &subs);
  365. }
  366. if (mdev->opened & PERM_READ) {
  367. subs.sender.client = mdev->client;
  368. subs.sender.port = mdev->port;
  369. subs.dest = dp->addr;
  370. snd_seq_kernel_client_ctl(dp->cseq, SNDRV_SEQ_IOCTL_UNSUBSCRIBE_PORT, &subs);
  371. }
  372. mdev->opened = 0;
  373. mdev->devinfo = NULL;
  374. snd_use_lock_free(&mdev->use_lock);
  375. return 0;
  376. }
  377. /*
  378. * change seq capability flags to file mode flags
  379. */
  380. int
  381. snd_seq_oss_midi_filemode(struct seq_oss_devinfo *dp, int dev)
  382. {
  383. struct seq_oss_midi *mdev;
  384. int mode;
  385. if ((mdev = get_mididev(dp, dev)) == NULL)
  386. return 0;
  387. mode = 0;
  388. if (mdev->opened & PERM_WRITE)
  389. mode |= SNDRV_SEQ_OSS_FILE_WRITE;
  390. if (mdev->opened & PERM_READ)
  391. mode |= SNDRV_SEQ_OSS_FILE_READ;
  392. snd_use_lock_free(&mdev->use_lock);
  393. return mode;
  394. }
  395. /*
  396. * reset the midi device and close it:
  397. * so far, only close the device.
  398. */
  399. void
  400. snd_seq_oss_midi_reset(struct seq_oss_devinfo *dp, int dev)
  401. {
  402. struct seq_oss_midi *mdev;
  403. if ((mdev = get_mididev(dp, dev)) == NULL)
  404. return;
  405. if (! mdev->opened) {
  406. snd_use_lock_free(&mdev->use_lock);
  407. return;
  408. }
  409. if (mdev->opened & PERM_WRITE) {
  410. struct snd_seq_event ev;
  411. int c;
  412. debug_printk(("resetting client %d port %d\n", mdev->client, mdev->port));
  413. memset(&ev, 0, sizeof(ev));
  414. ev.dest.client = mdev->client;
  415. ev.dest.port = mdev->port;
  416. ev.queue = dp->queue;
  417. ev.source.port = dp->port;
  418. if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_SYNTH) {
  419. ev.type = SNDRV_SEQ_EVENT_SENSING;
  420. snd_seq_oss_dispatch(dp, &ev, 0, 0);
  421. }
  422. for (c = 0; c < 16; c++) {
  423. ev.type = SNDRV_SEQ_EVENT_CONTROLLER;
  424. ev.data.control.channel = c;
  425. ev.data.control.param = MIDI_CTL_ALL_NOTES_OFF;
  426. snd_seq_oss_dispatch(dp, &ev, 0, 0);
  427. if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_MUSIC) {
  428. ev.data.control.param =
  429. MIDI_CTL_RESET_CONTROLLERS;
  430. snd_seq_oss_dispatch(dp, &ev, 0, 0);
  431. ev.type = SNDRV_SEQ_EVENT_PITCHBEND;
  432. ev.data.control.value = 0;
  433. snd_seq_oss_dispatch(dp, &ev, 0, 0);
  434. }
  435. }
  436. }
  437. // snd_seq_oss_midi_close(dp, dev);
  438. snd_use_lock_free(&mdev->use_lock);
  439. }
  440. /*
  441. * get client/port of the specified MIDI device
  442. */
  443. void
  444. snd_seq_oss_midi_get_addr(struct seq_oss_devinfo *dp, int dev, struct snd_seq_addr *addr)
  445. {
  446. struct seq_oss_midi *mdev;
  447. if ((mdev = get_mididev(dp, dev)) == NULL)
  448. return;
  449. addr->client = mdev->client;
  450. addr->port = mdev->port;
  451. snd_use_lock_free(&mdev->use_lock);
  452. }
  453. /*
  454. * input callback - this can be atomic
  455. */
  456. int
  457. snd_seq_oss_midi_input(struct snd_seq_event *ev, int direct, void *private_data)
  458. {
  459. struct seq_oss_devinfo *dp = (struct seq_oss_devinfo *)private_data;
  460. struct seq_oss_midi *mdev;
  461. int rc;
  462. if (dp->readq == NULL)
  463. return 0;
  464. if ((mdev = find_slot(ev->source.client, ev->source.port)) == NULL)
  465. return 0;
  466. if (! (mdev->opened & PERM_READ)) {
  467. snd_use_lock_free(&mdev->use_lock);
  468. return 0;
  469. }
  470. if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_MUSIC)
  471. rc = send_synth_event(dp, ev, mdev->seq_device);
  472. else
  473. rc = send_midi_event(dp, ev, mdev);
  474. snd_use_lock_free(&mdev->use_lock);
  475. return rc;
  476. }
  477. /*
  478. * convert ALSA sequencer event to OSS synth event
  479. */
  480. static int
  481. send_synth_event(struct seq_oss_devinfo *dp, struct snd_seq_event *ev, int dev)
  482. {
  483. union evrec ossev;
  484. memset(&ossev, 0, sizeof(ossev));
  485. switch (ev->type) {
  486. case SNDRV_SEQ_EVENT_NOTEON:
  487. ossev.v.cmd = MIDI_NOTEON; break;
  488. case SNDRV_SEQ_EVENT_NOTEOFF:
  489. ossev.v.cmd = MIDI_NOTEOFF; break;
  490. case SNDRV_SEQ_EVENT_KEYPRESS:
  491. ossev.v.cmd = MIDI_KEY_PRESSURE; break;
  492. case SNDRV_SEQ_EVENT_CONTROLLER:
  493. ossev.l.cmd = MIDI_CTL_CHANGE; break;
  494. case SNDRV_SEQ_EVENT_PGMCHANGE:
  495. ossev.l.cmd = MIDI_PGM_CHANGE; break;
  496. case SNDRV_SEQ_EVENT_CHANPRESS:
  497. ossev.l.cmd = MIDI_CHN_PRESSURE; break;
  498. case SNDRV_SEQ_EVENT_PITCHBEND:
  499. ossev.l.cmd = MIDI_PITCH_BEND; break;
  500. default:
  501. return 0; /* not supported */
  502. }
  503. ossev.v.dev = dev;
  504. switch (ev->type) {
  505. case SNDRV_SEQ_EVENT_NOTEON:
  506. case SNDRV_SEQ_EVENT_NOTEOFF:
  507. case SNDRV_SEQ_EVENT_KEYPRESS:
  508. ossev.v.code = EV_CHN_VOICE;
  509. ossev.v.note = ev->data.note.note;
  510. ossev.v.parm = ev->data.note.velocity;
  511. ossev.v.chn = ev->data.note.channel;
  512. break;
  513. case SNDRV_SEQ_EVENT_CONTROLLER:
  514. case SNDRV_SEQ_EVENT_PGMCHANGE:
  515. case SNDRV_SEQ_EVENT_CHANPRESS:
  516. ossev.l.code = EV_CHN_COMMON;
  517. ossev.l.p1 = ev->data.control.param;
  518. ossev.l.val = ev->data.control.value;
  519. ossev.l.chn = ev->data.control.channel;
  520. break;
  521. case SNDRV_SEQ_EVENT_PITCHBEND:
  522. ossev.l.code = EV_CHN_COMMON;
  523. ossev.l.val = ev->data.control.value + 8192;
  524. ossev.l.chn = ev->data.control.channel;
  525. break;
  526. }
  527. snd_seq_oss_readq_put_timestamp(dp->readq, ev->time.tick, dp->seq_mode);
  528. snd_seq_oss_readq_put_event(dp->readq, &ossev);
  529. return 0;
  530. }
  531. /*
  532. * decode event and send MIDI bytes to read queue
  533. */
  534. static int
  535. send_midi_event(struct seq_oss_devinfo *dp, struct snd_seq_event *ev, struct seq_oss_midi *mdev)
  536. {
  537. char msg[32];
  538. int len;
  539. snd_seq_oss_readq_put_timestamp(dp->readq, ev->time.tick, dp->seq_mode);
  540. if (!dp->timer->running)
  541. len = snd_seq_oss_timer_start(dp->timer);
  542. if (ev->type == SNDRV_SEQ_EVENT_SYSEX) {
  543. if ((ev->flags & SNDRV_SEQ_EVENT_LENGTH_MASK) == SNDRV_SEQ_EVENT_LENGTH_VARIABLE)
  544. snd_seq_oss_readq_puts(dp->readq, mdev->seq_device,
  545. ev->data.ext.ptr, ev->data.ext.len);
  546. } else {
  547. len = snd_midi_event_decode(mdev->coder, msg, sizeof(msg), ev);
  548. if (len > 0)
  549. snd_seq_oss_readq_puts(dp->readq, mdev->seq_device, msg, len);
  550. }
  551. return 0;
  552. }
  553. /*
  554. * dump midi data
  555. * return 0 : enqueued
  556. * non-zero : invalid - ignored
  557. */
  558. int
  559. snd_seq_oss_midi_putc(struct seq_oss_devinfo *dp, int dev, unsigned char c, struct snd_seq_event *ev)
  560. {
  561. struct seq_oss_midi *mdev;
  562. if ((mdev = get_mididev(dp, dev)) == NULL)
  563. return -ENODEV;
  564. if (snd_midi_event_encode_byte(mdev->coder, c, ev) > 0) {
  565. snd_seq_oss_fill_addr(dp, ev, mdev->client, mdev->port);
  566. snd_use_lock_free(&mdev->use_lock);
  567. return 0;
  568. }
  569. snd_use_lock_free(&mdev->use_lock);
  570. return -EINVAL;
  571. }
  572. /*
  573. * create OSS compatible midi_info record
  574. */
  575. int
  576. snd_seq_oss_midi_make_info(struct seq_oss_devinfo *dp, int dev, struct midi_info *inf)
  577. {
  578. struct seq_oss_midi *mdev;
  579. if ((mdev = get_mididev(dp, dev)) == NULL)
  580. return -ENXIO;
  581. inf->device = dev;
  582. inf->dev_type = 0; /* FIXME: ?? */
  583. inf->capabilities = 0; /* FIXME: ?? */
  584. strlcpy(inf->name, mdev->name, sizeof(inf->name));
  585. snd_use_lock_free(&mdev->use_lock);
  586. return 0;
  587. }
  588. #ifdef CONFIG_PROC_FS
  589. /*
  590. * proc interface
  591. */
  592. static char *
  593. capmode_str(int val)
  594. {
  595. val &= PERM_READ|PERM_WRITE;
  596. if (val == (PERM_READ|PERM_WRITE))
  597. return "read/write";
  598. else if (val == PERM_READ)
  599. return "read";
  600. else if (val == PERM_WRITE)
  601. return "write";
  602. else
  603. return "none";
  604. }
  605. void
  606. snd_seq_oss_midi_info_read(struct snd_info_buffer *buf)
  607. {
  608. int i;
  609. struct seq_oss_midi *mdev;
  610. snd_iprintf(buf, "\nNumber of MIDI devices: %d\n", max_midi_devs);
  611. for (i = 0; i < max_midi_devs; i++) {
  612. snd_iprintf(buf, "\nmidi %d: ", i);
  613. mdev = get_mdev(i);
  614. if (mdev == NULL) {
  615. snd_iprintf(buf, "*empty*\n");
  616. continue;
  617. }
  618. snd_iprintf(buf, "[%s] ALSA port %d:%d\n", mdev->name,
  619. mdev->client, mdev->port);
  620. snd_iprintf(buf, " capability %s / opened %s\n",
  621. capmode_str(mdev->flags),
  622. capmode_str(mdev->opened));
  623. snd_use_lock_free(&mdev->use_lock);
  624. }
  625. }
  626. #endif /* CONFIG_PROC_FS */