seq_oss_synth.c 15 KB

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