seq_oss_synth.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. /*
  2. * OSS compatible sequencer driver
  3. *
  4. * synth 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 "seq_oss_synth.h"
  23. #include "seq_oss_midi.h"
  24. #include "../seq_lock.h"
  25. #include <linux/init.h>
  26. #include <linux/slab.h>
  27. /*
  28. * constants
  29. */
  30. #define SNDRV_SEQ_OSS_MAX_SYNTH_NAME 30
  31. #define MAX_SYSEX_BUFLEN 128
  32. /*
  33. * definition of synth info records
  34. */
  35. /* sysex buffer */
  36. struct seq_oss_synth_sysex {
  37. int len;
  38. int skip;
  39. unsigned char buf[MAX_SYSEX_BUFLEN];
  40. };
  41. /* synth info */
  42. struct seq_oss_synth {
  43. int seq_device;
  44. /* for synth_info */
  45. int synth_type;
  46. int synth_subtype;
  47. int nr_voices;
  48. char name[SNDRV_SEQ_OSS_MAX_SYNTH_NAME];
  49. struct snd_seq_oss_callback oper;
  50. int opened;
  51. void *private_data;
  52. snd_use_lock_t use_lock;
  53. };
  54. /*
  55. * device table
  56. */
  57. static int max_synth_devs;
  58. static struct seq_oss_synth *synth_devs[SNDRV_SEQ_OSS_MAX_SYNTH_DEVS];
  59. static struct seq_oss_synth midi_synth_dev = {
  60. -1, /* seq_device */
  61. SYNTH_TYPE_MIDI, /* synth_type */
  62. 0, /* synth_subtype */
  63. 16, /* nr_voices */
  64. "MIDI", /* name */
  65. };
  66. static DEFINE_SPINLOCK(register_lock);
  67. /*
  68. * prototypes
  69. */
  70. static struct seq_oss_synth *get_synthdev(struct seq_oss_devinfo *dp, int dev);
  71. static void reset_channels(struct seq_oss_synthinfo *info);
  72. /*
  73. * global initialization
  74. */
  75. void __init
  76. snd_seq_oss_synth_init(void)
  77. {
  78. snd_use_lock_init(&midi_synth_dev.use_lock);
  79. }
  80. /*
  81. * registration of the synth device
  82. */
  83. int
  84. snd_seq_oss_synth_register(struct snd_seq_device *dev)
  85. {
  86. int i;
  87. struct seq_oss_synth *rec;
  88. struct snd_seq_oss_reg *reg = SNDRV_SEQ_DEVICE_ARGPTR(dev);
  89. unsigned long flags;
  90. if ((rec = kzalloc(sizeof(*rec), GFP_KERNEL)) == NULL) {
  91. snd_printk(KERN_ERR "can't malloc synth info\n");
  92. return -ENOMEM;
  93. }
  94. rec->seq_device = -1;
  95. rec->synth_type = reg->type;
  96. rec->synth_subtype = reg->subtype;
  97. rec->nr_voices = reg->nvoices;
  98. rec->oper = reg->oper;
  99. rec->private_data = reg->private_data;
  100. rec->opened = 0;
  101. snd_use_lock_init(&rec->use_lock);
  102. /* copy and truncate the name of synth device */
  103. strlcpy(rec->name, dev->name, sizeof(rec->name));
  104. /* registration */
  105. spin_lock_irqsave(&register_lock, flags);
  106. for (i = 0; i < max_synth_devs; i++) {
  107. if (synth_devs[i] == NULL)
  108. break;
  109. }
  110. if (i >= max_synth_devs) {
  111. if (max_synth_devs >= SNDRV_SEQ_OSS_MAX_SYNTH_DEVS) {
  112. spin_unlock_irqrestore(&register_lock, flags);
  113. snd_printk(KERN_ERR "no more synth slot\n");
  114. kfree(rec);
  115. return -ENOMEM;
  116. }
  117. max_synth_devs++;
  118. }
  119. rec->seq_device = i;
  120. synth_devs[i] = rec;
  121. debug_printk(("synth %s registered %d\n", rec->name, i));
  122. spin_unlock_irqrestore(&register_lock, flags);
  123. dev->driver_data = rec;
  124. #ifdef SNDRV_OSS_INFO_DEV_SYNTH
  125. if (i < SNDRV_CARDS)
  126. snd_oss_info_register(SNDRV_OSS_INFO_DEV_SYNTH, i, rec->name);
  127. #endif
  128. return 0;
  129. }
  130. int
  131. snd_seq_oss_synth_unregister(struct snd_seq_device *dev)
  132. {
  133. int index;
  134. struct seq_oss_synth *rec = dev->driver_data;
  135. unsigned long flags;
  136. spin_lock_irqsave(&register_lock, flags);
  137. for (index = 0; index < max_synth_devs; index++) {
  138. if (synth_devs[index] == rec)
  139. break;
  140. }
  141. if (index >= max_synth_devs) {
  142. spin_unlock_irqrestore(&register_lock, flags);
  143. snd_printk(KERN_ERR "can't unregister synth\n");
  144. return -EINVAL;
  145. }
  146. synth_devs[index] = NULL;
  147. if (index == max_synth_devs - 1) {
  148. for (index--; index >= 0; index--) {
  149. if (synth_devs[index])
  150. break;
  151. }
  152. max_synth_devs = index + 1;
  153. }
  154. spin_unlock_irqrestore(&register_lock, flags);
  155. #ifdef SNDRV_OSS_INFO_DEV_SYNTH
  156. if (rec->seq_device < SNDRV_CARDS)
  157. snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_SYNTH, rec->seq_device);
  158. #endif
  159. snd_use_lock_sync(&rec->use_lock);
  160. kfree(rec);
  161. return 0;
  162. }
  163. /*
  164. */
  165. static struct seq_oss_synth *
  166. get_sdev(int dev)
  167. {
  168. struct seq_oss_synth *rec;
  169. unsigned long flags;
  170. spin_lock_irqsave(&register_lock, flags);
  171. rec = synth_devs[dev];
  172. if (rec)
  173. snd_use_lock_use(&rec->use_lock);
  174. spin_unlock_irqrestore(&register_lock, flags);
  175. return rec;
  176. }
  177. /*
  178. * set up synth tables
  179. */
  180. void
  181. snd_seq_oss_synth_setup(struct seq_oss_devinfo *dp)
  182. {
  183. int i;
  184. struct seq_oss_synth *rec;
  185. struct seq_oss_synthinfo *info;
  186. dp->max_synthdev = max_synth_devs;
  187. dp->synth_opened = 0;
  188. memset(dp->synths, 0, sizeof(dp->synths));
  189. for (i = 0; i < dp->max_synthdev; i++) {
  190. rec = get_sdev(i);
  191. if (rec == NULL)
  192. continue;
  193. if (rec->oper.open == NULL || rec->oper.close == NULL) {
  194. snd_use_lock_free(&rec->use_lock);
  195. continue;
  196. }
  197. info = &dp->synths[i];
  198. info->arg.app_index = dp->port;
  199. info->arg.file_mode = dp->file_mode;
  200. info->arg.seq_mode = dp->seq_mode;
  201. if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_SYNTH)
  202. info->arg.event_passing = SNDRV_SEQ_OSS_PROCESS_EVENTS;
  203. else
  204. info->arg.event_passing = SNDRV_SEQ_OSS_PASS_EVENTS;
  205. info->opened = 0;
  206. if (!try_module_get(rec->oper.owner)) {
  207. snd_use_lock_free(&rec->use_lock);
  208. continue;
  209. }
  210. if (rec->oper.open(&info->arg, rec->private_data) < 0) {
  211. module_put(rec->oper.owner);
  212. snd_use_lock_free(&rec->use_lock);
  213. continue;
  214. }
  215. info->nr_voices = rec->nr_voices;
  216. if (info->nr_voices > 0) {
  217. info->ch = kcalloc(info->nr_voices, sizeof(struct seq_oss_chinfo), GFP_KERNEL);
  218. if (!info->ch) {
  219. snd_printk(KERN_ERR "Cannot malloc\n");
  220. rec->oper.close(&info->arg);
  221. module_put(rec->oper.owner);
  222. snd_use_lock_free(&rec->use_lock);
  223. continue;
  224. }
  225. reset_channels(info);
  226. }
  227. debug_printk(("synth %d assigned\n", i));
  228. info->opened++;
  229. rec->opened++;
  230. dp->synth_opened++;
  231. snd_use_lock_free(&rec->use_lock);
  232. }
  233. }
  234. /*
  235. * set up synth tables for MIDI emulation - /dev/music mode only
  236. */
  237. void
  238. snd_seq_oss_synth_setup_midi(struct seq_oss_devinfo *dp)
  239. {
  240. int i;
  241. if (dp->max_synthdev >= SNDRV_SEQ_OSS_MAX_SYNTH_DEVS)
  242. return;
  243. for (i = 0; i < dp->max_mididev; i++) {
  244. struct seq_oss_synthinfo *info;
  245. info = &dp->synths[dp->max_synthdev];
  246. if (snd_seq_oss_midi_open(dp, i, dp->file_mode) < 0)
  247. continue;
  248. info->arg.app_index = dp->port;
  249. info->arg.file_mode = dp->file_mode;
  250. info->arg.seq_mode = dp->seq_mode;
  251. info->arg.private_data = info;
  252. info->is_midi = 1;
  253. info->midi_mapped = i;
  254. info->arg.event_passing = SNDRV_SEQ_OSS_PASS_EVENTS;
  255. snd_seq_oss_midi_get_addr(dp, i, &info->arg.addr);
  256. info->opened = 1;
  257. midi_synth_dev.opened++;
  258. dp->max_synthdev++;
  259. if (dp->max_synthdev >= SNDRV_SEQ_OSS_MAX_SYNTH_DEVS)
  260. break;
  261. }
  262. }
  263. /*
  264. * clean up synth tables
  265. */
  266. void
  267. snd_seq_oss_synth_cleanup(struct seq_oss_devinfo *dp)
  268. {
  269. int i;
  270. struct seq_oss_synth *rec;
  271. struct seq_oss_synthinfo *info;
  272. if (snd_BUG_ON(dp->max_synthdev >= SNDRV_SEQ_OSS_MAX_SYNTH_DEVS))
  273. return;
  274. for (i = 0; i < dp->max_synthdev; i++) {
  275. info = &dp->synths[i];
  276. if (! info->opened)
  277. continue;
  278. if (info->is_midi) {
  279. if (midi_synth_dev.opened > 0) {
  280. snd_seq_oss_midi_close(dp, info->midi_mapped);
  281. midi_synth_dev.opened--;
  282. }
  283. } else {
  284. rec = get_sdev(i);
  285. if (rec == NULL)
  286. continue;
  287. if (rec->opened > 0) {
  288. debug_printk(("synth %d closed\n", i));
  289. rec->oper.close(&info->arg);
  290. module_put(rec->oper.owner);
  291. rec->opened = 0;
  292. }
  293. snd_use_lock_free(&rec->use_lock);
  294. }
  295. kfree(info->sysex);
  296. info->sysex = NULL;
  297. kfree(info->ch);
  298. info->ch = NULL;
  299. }
  300. dp->synth_opened = 0;
  301. dp->max_synthdev = 0;
  302. }
  303. /*
  304. * check if the specified device is MIDI mapped device
  305. */
  306. static int
  307. is_midi_dev(struct seq_oss_devinfo *dp, int dev)
  308. {
  309. if (dev < 0 || dev >= dp->max_synthdev)
  310. return 0;
  311. if (dp->synths[dev].is_midi)
  312. return 1;
  313. return 0;
  314. }
  315. /*
  316. * return synth device information pointer
  317. */
  318. static struct seq_oss_synth *
  319. get_synthdev(struct seq_oss_devinfo *dp, int dev)
  320. {
  321. struct seq_oss_synth *rec;
  322. if (dev < 0 || dev >= dp->max_synthdev)
  323. return NULL;
  324. if (! dp->synths[dev].opened)
  325. return NULL;
  326. if (dp->synths[dev].is_midi)
  327. return &midi_synth_dev;
  328. if ((rec = get_sdev(dev)) == NULL)
  329. return NULL;
  330. if (! rec->opened) {
  331. snd_use_lock_free(&rec->use_lock);
  332. return NULL;
  333. }
  334. return rec;
  335. }
  336. /*
  337. * reset note and velocity on each channel.
  338. */
  339. static void
  340. reset_channels(struct seq_oss_synthinfo *info)
  341. {
  342. int i;
  343. if (info->ch == NULL || ! info->nr_voices)
  344. return;
  345. for (i = 0; i < info->nr_voices; i++) {
  346. info->ch[i].note = -1;
  347. info->ch[i].vel = 0;
  348. }
  349. }
  350. /*
  351. * reset synth device:
  352. * call reset callback. if no callback is defined, send a heartbeat
  353. * event to the corresponding port.
  354. */
  355. void
  356. snd_seq_oss_synth_reset(struct seq_oss_devinfo *dp, int dev)
  357. {
  358. struct seq_oss_synth *rec;
  359. struct seq_oss_synthinfo *info;
  360. if (snd_BUG_ON(dev < 0 || dev >= dp->max_synthdev))
  361. return;
  362. info = &dp->synths[dev];
  363. if (! info->opened)
  364. return;
  365. if (info->sysex)
  366. info->sysex->len = 0; /* reset sysex */
  367. reset_channels(info);
  368. if (info->is_midi) {
  369. if (midi_synth_dev.opened <= 0)
  370. return;
  371. snd_seq_oss_midi_reset(dp, info->midi_mapped);
  372. /* reopen the device */
  373. snd_seq_oss_midi_close(dp, dev);
  374. if (snd_seq_oss_midi_open(dp, info->midi_mapped,
  375. dp->file_mode) < 0) {
  376. midi_synth_dev.opened--;
  377. info->opened = 0;
  378. kfree(info->sysex);
  379. info->sysex = NULL;
  380. kfree(info->ch);
  381. info->ch = NULL;
  382. }
  383. return;
  384. }
  385. rec = get_sdev(dev);
  386. if (rec == NULL)
  387. return;
  388. if (rec->oper.reset) {
  389. rec->oper.reset(&info->arg);
  390. } else {
  391. struct snd_seq_event ev;
  392. memset(&ev, 0, sizeof(ev));
  393. snd_seq_oss_fill_addr(dp, &ev, info->arg.addr.client,
  394. info->arg.addr.port);
  395. ev.type = SNDRV_SEQ_EVENT_RESET;
  396. snd_seq_oss_dispatch(dp, &ev, 0, 0);
  397. }
  398. snd_use_lock_free(&rec->use_lock);
  399. }
  400. /*
  401. * load a patch record:
  402. * call load_patch callback function
  403. */
  404. int
  405. snd_seq_oss_synth_load_patch(struct seq_oss_devinfo *dp, int dev, int fmt,
  406. const char __user *buf, int p, int c)
  407. {
  408. struct seq_oss_synth *rec;
  409. int rc;
  410. if (dev < 0 || dev >= dp->max_synthdev)
  411. return -ENXIO;
  412. if (is_midi_dev(dp, dev))
  413. return 0;
  414. if ((rec = get_synthdev(dp, dev)) == NULL)
  415. return -ENXIO;
  416. if (rec->oper.load_patch == NULL)
  417. rc = -ENXIO;
  418. else
  419. rc = rec->oper.load_patch(&dp->synths[dev].arg, fmt, buf, p, c);
  420. snd_use_lock_free(&rec->use_lock);
  421. return rc;
  422. }
  423. /*
  424. * check if the device is valid synth device
  425. */
  426. int
  427. snd_seq_oss_synth_is_valid(struct seq_oss_devinfo *dp, int dev)
  428. {
  429. struct seq_oss_synth *rec;
  430. rec = get_synthdev(dp, dev);
  431. if (rec) {
  432. snd_use_lock_free(&rec->use_lock);
  433. return 1;
  434. }
  435. return 0;
  436. }
  437. /*
  438. * receive OSS 6 byte sysex packet:
  439. * the full sysex message will be sent if it reaches to the end of data
  440. * (0xff).
  441. */
  442. int
  443. snd_seq_oss_synth_sysex(struct seq_oss_devinfo *dp, int dev, unsigned char *buf, struct snd_seq_event *ev)
  444. {
  445. int i, send;
  446. unsigned char *dest;
  447. struct seq_oss_synth_sysex *sysex;
  448. if (! snd_seq_oss_synth_is_valid(dp, dev))
  449. return -ENXIO;
  450. sysex = dp->synths[dev].sysex;
  451. if (sysex == NULL) {
  452. sysex = kzalloc(sizeof(*sysex), GFP_KERNEL);
  453. if (sysex == NULL)
  454. return -ENOMEM;
  455. dp->synths[dev].sysex = sysex;
  456. }
  457. send = 0;
  458. dest = sysex->buf + sysex->len;
  459. /* copy 6 byte packet to the buffer */
  460. for (i = 0; i < 6; i++) {
  461. if (buf[i] == 0xff) {
  462. send = 1;
  463. break;
  464. }
  465. dest[i] = buf[i];
  466. sysex->len++;
  467. if (sysex->len >= MAX_SYSEX_BUFLEN) {
  468. sysex->len = 0;
  469. sysex->skip = 1;
  470. break;
  471. }
  472. }
  473. if (sysex->len && send) {
  474. if (sysex->skip) {
  475. sysex->skip = 0;
  476. sysex->len = 0;
  477. return -EINVAL; /* skip */
  478. }
  479. /* copy the data to event record and send it */
  480. ev->flags = SNDRV_SEQ_EVENT_LENGTH_VARIABLE;
  481. if (snd_seq_oss_synth_addr(dp, dev, ev))
  482. return -EINVAL;
  483. ev->data.ext.len = sysex->len;
  484. ev->data.ext.ptr = sysex->buf;
  485. sysex->len = 0;
  486. return 0;
  487. }
  488. return -EINVAL; /* skip */
  489. }
  490. /*
  491. * fill the event source/destination addresses
  492. */
  493. int
  494. snd_seq_oss_synth_addr(struct seq_oss_devinfo *dp, int dev, struct snd_seq_event *ev)
  495. {
  496. if (! snd_seq_oss_synth_is_valid(dp, dev))
  497. return -EINVAL;
  498. snd_seq_oss_fill_addr(dp, ev, dp->synths[dev].arg.addr.client,
  499. dp->synths[dev].arg.addr.port);
  500. return 0;
  501. }
  502. /*
  503. * OSS compatible ioctl
  504. */
  505. int
  506. snd_seq_oss_synth_ioctl(struct seq_oss_devinfo *dp, int dev, unsigned int cmd, unsigned long addr)
  507. {
  508. struct seq_oss_synth *rec;
  509. int rc;
  510. if (is_midi_dev(dp, dev))
  511. return -ENXIO;
  512. if ((rec = get_synthdev(dp, dev)) == NULL)
  513. return -ENXIO;
  514. if (rec->oper.ioctl == NULL)
  515. rc = -ENXIO;
  516. else
  517. rc = rec->oper.ioctl(&dp->synths[dev].arg, cmd, addr);
  518. snd_use_lock_free(&rec->use_lock);
  519. return rc;
  520. }
  521. /*
  522. * send OSS raw events - SEQ_PRIVATE and SEQ_VOLUME
  523. */
  524. int
  525. snd_seq_oss_synth_raw_event(struct seq_oss_devinfo *dp, int dev, unsigned char *data, struct snd_seq_event *ev)
  526. {
  527. if (! snd_seq_oss_synth_is_valid(dp, dev) || is_midi_dev(dp, dev))
  528. return -ENXIO;
  529. ev->type = SNDRV_SEQ_EVENT_OSS;
  530. memcpy(ev->data.raw8.d, data, 8);
  531. return snd_seq_oss_synth_addr(dp, dev, ev);
  532. }
  533. /*
  534. * create OSS compatible synth_info record
  535. */
  536. int
  537. snd_seq_oss_synth_make_info(struct seq_oss_devinfo *dp, int dev, struct synth_info *inf)
  538. {
  539. struct seq_oss_synth *rec;
  540. if (dev < 0 || dev >= dp->max_synthdev)
  541. return -ENXIO;
  542. if (dp->synths[dev].is_midi) {
  543. struct midi_info minf;
  544. snd_seq_oss_midi_make_info(dp, dp->synths[dev].midi_mapped, &minf);
  545. inf->synth_type = SYNTH_TYPE_MIDI;
  546. inf->synth_subtype = 0;
  547. inf->nr_voices = 16;
  548. inf->device = dev;
  549. strlcpy(inf->name, minf.name, sizeof(inf->name));
  550. } else {
  551. if ((rec = get_synthdev(dp, dev)) == NULL)
  552. return -ENXIO;
  553. inf->synth_type = rec->synth_type;
  554. inf->synth_subtype = rec->synth_subtype;
  555. inf->nr_voices = rec->nr_voices;
  556. inf->device = dev;
  557. strlcpy(inf->name, rec->name, sizeof(inf->name));
  558. snd_use_lock_free(&rec->use_lock);
  559. }
  560. return 0;
  561. }
  562. #ifdef CONFIG_PROC_FS
  563. /*
  564. * proc interface
  565. */
  566. void
  567. snd_seq_oss_synth_info_read(struct snd_info_buffer *buf)
  568. {
  569. int i;
  570. struct seq_oss_synth *rec;
  571. snd_iprintf(buf, "\nNumber of synth devices: %d\n", max_synth_devs);
  572. for (i = 0; i < max_synth_devs; i++) {
  573. snd_iprintf(buf, "\nsynth %d: ", i);
  574. rec = get_sdev(i);
  575. if (rec == NULL) {
  576. snd_iprintf(buf, "*empty*\n");
  577. continue;
  578. }
  579. snd_iprintf(buf, "[%s]\n", rec->name);
  580. snd_iprintf(buf, " type 0x%x : subtype 0x%x : voices %d\n",
  581. rec->synth_type, rec->synth_subtype,
  582. rec->nr_voices);
  583. snd_iprintf(buf, " capabilities : ioctl %s / load_patch %s\n",
  584. enabled_str((long)rec->oper.ioctl),
  585. enabled_str((long)rec->oper.load_patch));
  586. snd_use_lock_free(&rec->use_lock);
  587. }
  588. }
  589. #endif /* CONFIG_PROC_FS */