seq_oss_midi.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  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
  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. /* the port must include generic midi */
  139. if (! (pinfo->type & SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC))
  140. return 0;
  141. /* either read or write subscribable */
  142. if ((pinfo->capability & PERM_WRITE) != PERM_WRITE &&
  143. (pinfo->capability & PERM_READ) != PERM_READ)
  144. return 0;
  145. /*
  146. * look for the identical slot
  147. */
  148. if ((mdev = find_slot(pinfo->addr.client, pinfo->addr.port)) != NULL) {
  149. /* already exists */
  150. snd_use_lock_free(&mdev->use_lock);
  151. return 0;
  152. }
  153. /*
  154. * allocate midi info record
  155. */
  156. if ((mdev = kzalloc(sizeof(*mdev), GFP_KERNEL)) == NULL) {
  157. pr_err("ALSA: seq_oss: can't malloc midi info\n");
  158. return -ENOMEM;
  159. }
  160. /* copy the port information */
  161. mdev->client = pinfo->addr.client;
  162. mdev->port = pinfo->addr.port;
  163. mdev->flags = pinfo->capability;
  164. mdev->opened = 0;
  165. snd_use_lock_init(&mdev->use_lock);
  166. /* copy and truncate the name of synth device */
  167. strlcpy(mdev->name, pinfo->name, sizeof(mdev->name));
  168. /* create MIDI coder */
  169. if (snd_midi_event_new(MAX_MIDI_EVENT_BUF, &mdev->coder) < 0) {
  170. pr_err("ALSA: seq_oss: can't malloc midi coder\n");
  171. kfree(mdev);
  172. return -ENOMEM;
  173. }
  174. /* OSS sequencer adds running status to all sequences */
  175. snd_midi_event_no_status(mdev->coder, 1);
  176. /*
  177. * look for en empty slot
  178. */
  179. spin_lock_irqsave(&register_lock, flags);
  180. for (i = 0; i < max_midi_devs; i++) {
  181. if (midi_devs[i] == NULL)
  182. break;
  183. }
  184. if (i >= max_midi_devs) {
  185. if (max_midi_devs >= SNDRV_SEQ_OSS_MAX_MIDI_DEVS) {
  186. spin_unlock_irqrestore(&register_lock, flags);
  187. snd_midi_event_free(mdev->coder);
  188. kfree(mdev);
  189. return -ENOMEM;
  190. }
  191. max_midi_devs++;
  192. }
  193. mdev->seq_device = i;
  194. midi_devs[mdev->seq_device] = mdev;
  195. spin_unlock_irqrestore(&register_lock, flags);
  196. return 0;
  197. }
  198. /*
  199. * release the midi device if it was registered
  200. */
  201. int
  202. snd_seq_oss_midi_check_exit_port(int client, int port)
  203. {
  204. struct seq_oss_midi *mdev;
  205. unsigned long flags;
  206. int index;
  207. if ((mdev = find_slot(client, port)) != NULL) {
  208. spin_lock_irqsave(&register_lock, flags);
  209. midi_devs[mdev->seq_device] = NULL;
  210. spin_unlock_irqrestore(&register_lock, flags);
  211. snd_use_lock_free(&mdev->use_lock);
  212. snd_use_lock_sync(&mdev->use_lock);
  213. if (mdev->coder)
  214. snd_midi_event_free(mdev->coder);
  215. kfree(mdev);
  216. }
  217. spin_lock_irqsave(&register_lock, flags);
  218. for (index = max_midi_devs - 1; index >= 0; index--) {
  219. if (midi_devs[index])
  220. break;
  221. }
  222. max_midi_devs = index + 1;
  223. spin_unlock_irqrestore(&register_lock, flags);
  224. return 0;
  225. }
  226. /*
  227. * release the midi device if it was registered
  228. */
  229. void
  230. snd_seq_oss_midi_clear_all(void)
  231. {
  232. int i;
  233. struct seq_oss_midi *mdev;
  234. unsigned long flags;
  235. spin_lock_irqsave(&register_lock, flags);
  236. for (i = 0; i < max_midi_devs; i++) {
  237. if ((mdev = midi_devs[i]) != NULL) {
  238. if (mdev->coder)
  239. snd_midi_event_free(mdev->coder);
  240. kfree(mdev);
  241. midi_devs[i] = NULL;
  242. }
  243. }
  244. max_midi_devs = 0;
  245. spin_unlock_irqrestore(&register_lock, flags);
  246. }
  247. /*
  248. * set up midi tables
  249. */
  250. void
  251. snd_seq_oss_midi_setup(struct seq_oss_devinfo *dp)
  252. {
  253. dp->max_mididev = max_midi_devs;
  254. }
  255. /*
  256. * clean up midi tables
  257. */
  258. void
  259. snd_seq_oss_midi_cleanup(struct seq_oss_devinfo *dp)
  260. {
  261. int i;
  262. for (i = 0; i < dp->max_mididev; i++)
  263. snd_seq_oss_midi_close(dp, i);
  264. dp->max_mididev = 0;
  265. }
  266. /*
  267. * open all midi devices. ignore errors.
  268. */
  269. void
  270. snd_seq_oss_midi_open_all(struct seq_oss_devinfo *dp, int file_mode)
  271. {
  272. int i;
  273. for (i = 0; i < dp->max_mididev; i++)
  274. snd_seq_oss_midi_open(dp, i, file_mode);
  275. }
  276. /*
  277. * get the midi device information
  278. */
  279. static struct seq_oss_midi *
  280. get_mididev(struct seq_oss_devinfo *dp, int dev)
  281. {
  282. if (dev < 0 || dev >= dp->max_mididev)
  283. return NULL;
  284. return get_mdev(dev);
  285. }
  286. /*
  287. * open the midi device if not opened yet
  288. */
  289. int
  290. snd_seq_oss_midi_open(struct seq_oss_devinfo *dp, int dev, int fmode)
  291. {
  292. int perm;
  293. struct seq_oss_midi *mdev;
  294. struct snd_seq_port_subscribe subs;
  295. if ((mdev = get_mididev(dp, dev)) == NULL)
  296. return -ENODEV;
  297. /* already used? */
  298. if (mdev->opened && mdev->devinfo != dp) {
  299. snd_use_lock_free(&mdev->use_lock);
  300. return -EBUSY;
  301. }
  302. perm = 0;
  303. if (is_write_mode(fmode))
  304. perm |= PERM_WRITE;
  305. if (is_read_mode(fmode))
  306. perm |= PERM_READ;
  307. perm &= mdev->flags;
  308. if (perm == 0) {
  309. snd_use_lock_free(&mdev->use_lock);
  310. return -ENXIO;
  311. }
  312. /* already opened? */
  313. if ((mdev->opened & perm) == perm) {
  314. snd_use_lock_free(&mdev->use_lock);
  315. return 0;
  316. }
  317. perm &= ~mdev->opened;
  318. memset(&subs, 0, sizeof(subs));
  319. if (perm & PERM_WRITE) {
  320. subs.sender = dp->addr;
  321. subs.dest.client = mdev->client;
  322. subs.dest.port = mdev->port;
  323. if (snd_seq_kernel_client_ctl(dp->cseq, SNDRV_SEQ_IOCTL_SUBSCRIBE_PORT, &subs) >= 0)
  324. mdev->opened |= PERM_WRITE;
  325. }
  326. if (perm & PERM_READ) {
  327. subs.sender.client = mdev->client;
  328. subs.sender.port = mdev->port;
  329. subs.dest = dp->addr;
  330. subs.flags = SNDRV_SEQ_PORT_SUBS_TIMESTAMP;
  331. subs.queue = dp->queue; /* queue for timestamps */
  332. if (snd_seq_kernel_client_ctl(dp->cseq, SNDRV_SEQ_IOCTL_SUBSCRIBE_PORT, &subs) >= 0)
  333. mdev->opened |= PERM_READ;
  334. }
  335. if (! mdev->opened) {
  336. snd_use_lock_free(&mdev->use_lock);
  337. return -ENXIO;
  338. }
  339. mdev->devinfo = dp;
  340. snd_use_lock_free(&mdev->use_lock);
  341. return 0;
  342. }
  343. /*
  344. * close the midi device if already opened
  345. */
  346. int
  347. snd_seq_oss_midi_close(struct seq_oss_devinfo *dp, int dev)
  348. {
  349. struct seq_oss_midi *mdev;
  350. struct snd_seq_port_subscribe subs;
  351. if ((mdev = get_mididev(dp, dev)) == NULL)
  352. return -ENODEV;
  353. if (! mdev->opened || mdev->devinfo != dp) {
  354. snd_use_lock_free(&mdev->use_lock);
  355. return 0;
  356. }
  357. memset(&subs, 0, sizeof(subs));
  358. if (mdev->opened & PERM_WRITE) {
  359. subs.sender = dp->addr;
  360. subs.dest.client = mdev->client;
  361. subs.dest.port = mdev->port;
  362. snd_seq_kernel_client_ctl(dp->cseq, SNDRV_SEQ_IOCTL_UNSUBSCRIBE_PORT, &subs);
  363. }
  364. if (mdev->opened & PERM_READ) {
  365. subs.sender.client = mdev->client;
  366. subs.sender.port = mdev->port;
  367. subs.dest = dp->addr;
  368. snd_seq_kernel_client_ctl(dp->cseq, SNDRV_SEQ_IOCTL_UNSUBSCRIBE_PORT, &subs);
  369. }
  370. mdev->opened = 0;
  371. mdev->devinfo = NULL;
  372. snd_use_lock_free(&mdev->use_lock);
  373. return 0;
  374. }
  375. /*
  376. * change seq capability flags to file mode flags
  377. */
  378. int
  379. snd_seq_oss_midi_filemode(struct seq_oss_devinfo *dp, int dev)
  380. {
  381. struct seq_oss_midi *mdev;
  382. int mode;
  383. if ((mdev = get_mididev(dp, dev)) == NULL)
  384. return 0;
  385. mode = 0;
  386. if (mdev->opened & PERM_WRITE)
  387. mode |= SNDRV_SEQ_OSS_FILE_WRITE;
  388. if (mdev->opened & PERM_READ)
  389. mode |= SNDRV_SEQ_OSS_FILE_READ;
  390. snd_use_lock_free(&mdev->use_lock);
  391. return mode;
  392. }
  393. /*
  394. * reset the midi device and close it:
  395. * so far, only close the device.
  396. */
  397. void
  398. snd_seq_oss_midi_reset(struct seq_oss_devinfo *dp, int dev)
  399. {
  400. struct seq_oss_midi *mdev;
  401. if ((mdev = get_mididev(dp, dev)) == NULL)
  402. return;
  403. if (! mdev->opened) {
  404. snd_use_lock_free(&mdev->use_lock);
  405. return;
  406. }
  407. if (mdev->opened & PERM_WRITE) {
  408. struct snd_seq_event ev;
  409. int c;
  410. memset(&ev, 0, sizeof(ev));
  411. ev.dest.client = mdev->client;
  412. ev.dest.port = mdev->port;
  413. ev.queue = dp->queue;
  414. ev.source.port = dp->port;
  415. if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_SYNTH) {
  416. ev.type = SNDRV_SEQ_EVENT_SENSING;
  417. snd_seq_oss_dispatch(dp, &ev, 0, 0);
  418. }
  419. for (c = 0; c < 16; c++) {
  420. ev.type = SNDRV_SEQ_EVENT_CONTROLLER;
  421. ev.data.control.channel = c;
  422. ev.data.control.param = MIDI_CTL_ALL_NOTES_OFF;
  423. snd_seq_oss_dispatch(dp, &ev, 0, 0);
  424. if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_MUSIC) {
  425. ev.data.control.param =
  426. MIDI_CTL_RESET_CONTROLLERS;
  427. snd_seq_oss_dispatch(dp, &ev, 0, 0);
  428. ev.type = SNDRV_SEQ_EVENT_PITCHBEND;
  429. ev.data.control.value = 0;
  430. snd_seq_oss_dispatch(dp, &ev, 0, 0);
  431. }
  432. }
  433. }
  434. // snd_seq_oss_midi_close(dp, dev);
  435. snd_use_lock_free(&mdev->use_lock);
  436. }
  437. /*
  438. * get client/port of the specified MIDI device
  439. */
  440. void
  441. snd_seq_oss_midi_get_addr(struct seq_oss_devinfo *dp, int dev, struct snd_seq_addr *addr)
  442. {
  443. struct seq_oss_midi *mdev;
  444. if ((mdev = get_mididev(dp, dev)) == NULL)
  445. return;
  446. addr->client = mdev->client;
  447. addr->port = mdev->port;
  448. snd_use_lock_free(&mdev->use_lock);
  449. }
  450. /*
  451. * input callback - this can be atomic
  452. */
  453. int
  454. snd_seq_oss_midi_input(struct snd_seq_event *ev, int direct, void *private_data)
  455. {
  456. struct seq_oss_devinfo *dp = (struct seq_oss_devinfo *)private_data;
  457. struct seq_oss_midi *mdev;
  458. int rc;
  459. if (dp->readq == NULL)
  460. return 0;
  461. if ((mdev = find_slot(ev->source.client, ev->source.port)) == NULL)
  462. return 0;
  463. if (! (mdev->opened & PERM_READ)) {
  464. snd_use_lock_free(&mdev->use_lock);
  465. return 0;
  466. }
  467. if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_MUSIC)
  468. rc = send_synth_event(dp, ev, mdev->seq_device);
  469. else
  470. rc = send_midi_event(dp, ev, mdev);
  471. snd_use_lock_free(&mdev->use_lock);
  472. return rc;
  473. }
  474. /*
  475. * convert ALSA sequencer event to OSS synth event
  476. */
  477. static int
  478. send_synth_event(struct seq_oss_devinfo *dp, struct snd_seq_event *ev, int dev)
  479. {
  480. union evrec ossev;
  481. memset(&ossev, 0, sizeof(ossev));
  482. switch (ev->type) {
  483. case SNDRV_SEQ_EVENT_NOTEON:
  484. ossev.v.cmd = MIDI_NOTEON; break;
  485. case SNDRV_SEQ_EVENT_NOTEOFF:
  486. ossev.v.cmd = MIDI_NOTEOFF; break;
  487. case SNDRV_SEQ_EVENT_KEYPRESS:
  488. ossev.v.cmd = MIDI_KEY_PRESSURE; break;
  489. case SNDRV_SEQ_EVENT_CONTROLLER:
  490. ossev.l.cmd = MIDI_CTL_CHANGE; break;
  491. case SNDRV_SEQ_EVENT_PGMCHANGE:
  492. ossev.l.cmd = MIDI_PGM_CHANGE; break;
  493. case SNDRV_SEQ_EVENT_CHANPRESS:
  494. ossev.l.cmd = MIDI_CHN_PRESSURE; break;
  495. case SNDRV_SEQ_EVENT_PITCHBEND:
  496. ossev.l.cmd = MIDI_PITCH_BEND; break;
  497. default:
  498. return 0; /* not supported */
  499. }
  500. ossev.v.dev = dev;
  501. switch (ev->type) {
  502. case SNDRV_SEQ_EVENT_NOTEON:
  503. case SNDRV_SEQ_EVENT_NOTEOFF:
  504. case SNDRV_SEQ_EVENT_KEYPRESS:
  505. ossev.v.code = EV_CHN_VOICE;
  506. ossev.v.note = ev->data.note.note;
  507. ossev.v.parm = ev->data.note.velocity;
  508. ossev.v.chn = ev->data.note.channel;
  509. break;
  510. case SNDRV_SEQ_EVENT_CONTROLLER:
  511. case SNDRV_SEQ_EVENT_PGMCHANGE:
  512. case SNDRV_SEQ_EVENT_CHANPRESS:
  513. ossev.l.code = EV_CHN_COMMON;
  514. ossev.l.p1 = ev->data.control.param;
  515. ossev.l.val = ev->data.control.value;
  516. ossev.l.chn = ev->data.control.channel;
  517. break;
  518. case SNDRV_SEQ_EVENT_PITCHBEND:
  519. ossev.l.code = EV_CHN_COMMON;
  520. ossev.l.val = ev->data.control.value + 8192;
  521. ossev.l.chn = ev->data.control.channel;
  522. break;
  523. }
  524. snd_seq_oss_readq_put_timestamp(dp->readq, ev->time.tick, dp->seq_mode);
  525. snd_seq_oss_readq_put_event(dp->readq, &ossev);
  526. return 0;
  527. }
  528. /*
  529. * decode event and send MIDI bytes to read queue
  530. */
  531. static int
  532. send_midi_event(struct seq_oss_devinfo *dp, struct snd_seq_event *ev, struct seq_oss_midi *mdev)
  533. {
  534. char msg[32];
  535. int len;
  536. snd_seq_oss_readq_put_timestamp(dp->readq, ev->time.tick, dp->seq_mode);
  537. if (!dp->timer->running)
  538. len = snd_seq_oss_timer_start(dp->timer);
  539. if (ev->type == SNDRV_SEQ_EVENT_SYSEX) {
  540. if ((ev->flags & SNDRV_SEQ_EVENT_LENGTH_MASK) == SNDRV_SEQ_EVENT_LENGTH_VARIABLE)
  541. snd_seq_oss_readq_puts(dp->readq, mdev->seq_device,
  542. ev->data.ext.ptr, ev->data.ext.len);
  543. } else {
  544. len = snd_midi_event_decode(mdev->coder, msg, sizeof(msg), ev);
  545. if (len > 0)
  546. snd_seq_oss_readq_puts(dp->readq, mdev->seq_device, msg, len);
  547. }
  548. return 0;
  549. }
  550. /*
  551. * dump midi data
  552. * return 0 : enqueued
  553. * non-zero : invalid - ignored
  554. */
  555. int
  556. snd_seq_oss_midi_putc(struct seq_oss_devinfo *dp, int dev, unsigned char c, struct snd_seq_event *ev)
  557. {
  558. struct seq_oss_midi *mdev;
  559. if ((mdev = get_mididev(dp, dev)) == NULL)
  560. return -ENODEV;
  561. if (snd_midi_event_encode_byte(mdev->coder, c, ev) > 0) {
  562. snd_seq_oss_fill_addr(dp, ev, mdev->client, mdev->port);
  563. snd_use_lock_free(&mdev->use_lock);
  564. return 0;
  565. }
  566. snd_use_lock_free(&mdev->use_lock);
  567. return -EINVAL;
  568. }
  569. /*
  570. * create OSS compatible midi_info record
  571. */
  572. int
  573. snd_seq_oss_midi_make_info(struct seq_oss_devinfo *dp, int dev, struct midi_info *inf)
  574. {
  575. struct seq_oss_midi *mdev;
  576. if ((mdev = get_mididev(dp, dev)) == NULL)
  577. return -ENXIO;
  578. inf->device = dev;
  579. inf->dev_type = 0; /* FIXME: ?? */
  580. inf->capabilities = 0; /* FIXME: ?? */
  581. strlcpy(inf->name, mdev->name, sizeof(inf->name));
  582. snd_use_lock_free(&mdev->use_lock);
  583. return 0;
  584. }
  585. #ifdef CONFIG_PROC_FS
  586. /*
  587. * proc interface
  588. */
  589. static char *
  590. capmode_str(int val)
  591. {
  592. val &= PERM_READ|PERM_WRITE;
  593. if (val == (PERM_READ|PERM_WRITE))
  594. return "read/write";
  595. else if (val == PERM_READ)
  596. return "read";
  597. else if (val == PERM_WRITE)
  598. return "write";
  599. else
  600. return "none";
  601. }
  602. void
  603. snd_seq_oss_midi_info_read(struct snd_info_buffer *buf)
  604. {
  605. int i;
  606. struct seq_oss_midi *mdev;
  607. snd_iprintf(buf, "\nNumber of MIDI devices: %d\n", max_midi_devs);
  608. for (i = 0; i < max_midi_devs; i++) {
  609. snd_iprintf(buf, "\nmidi %d: ", i);
  610. mdev = get_mdev(i);
  611. if (mdev == NULL) {
  612. snd_iprintf(buf, "*empty*\n");
  613. continue;
  614. }
  615. snd_iprintf(buf, "[%s] ALSA port %d:%d\n", mdev->name,
  616. mdev->client, mdev->port);
  617. snd_iprintf(buf, " capability %s / opened %s\n",
  618. capmode_str(mdev->flags),
  619. capmode_str(mdev->opened));
  620. snd_use_lock_free(&mdev->use_lock);
  621. }
  622. }
  623. #endif /* CONFIG_PROC_FS */