seq_oss_midi.c 16 KB

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