opl3_lib.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /*
  2. * Copyright (c) by Jaroslav Kysela <perex@suse.cz>,
  3. * Hannu Savolainen 1993-1996,
  4. * Rob Hooft
  5. *
  6. * Routines for control of AdLib FM cards (OPL2/OPL3/OPL4 chips)
  7. *
  8. * Most if code is ported from OSS/Lite.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. */
  25. #include <sound/opl3.h>
  26. #include <asm/io.h>
  27. #include <linux/delay.h>
  28. #include <linux/init.h>
  29. #include <linux/slab.h>
  30. #include <linux/ioport.h>
  31. #include <sound/minors.h>
  32. MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>, Hannu Savolainen 1993-1996, Rob Hooft");
  33. MODULE_DESCRIPTION("Routines for control of AdLib FM cards (OPL2/OPL3/OPL4 chips)");
  34. MODULE_LICENSE("GPL");
  35. extern char snd_opl3_regmap[MAX_OPL2_VOICES][4];
  36. static void snd_opl2_command(struct snd_opl3 * opl3, unsigned short cmd, unsigned char val)
  37. {
  38. unsigned long flags;
  39. unsigned long port;
  40. /*
  41. * The original 2-OP synth requires a quite long delay
  42. * after writing to a register.
  43. */
  44. port = (cmd & OPL3_RIGHT) ? opl3->r_port : opl3->l_port;
  45. spin_lock_irqsave(&opl3->reg_lock, flags);
  46. outb((unsigned char) cmd, port);
  47. udelay(10);
  48. outb((unsigned char) val, port + 1);
  49. udelay(30);
  50. spin_unlock_irqrestore(&opl3->reg_lock, flags);
  51. }
  52. static void snd_opl3_command(struct snd_opl3 * opl3, unsigned short cmd, unsigned char val)
  53. {
  54. unsigned long flags;
  55. unsigned long port;
  56. /*
  57. * The OPL-3 survives with just two INBs
  58. * after writing to a register.
  59. */
  60. port = (cmd & OPL3_RIGHT) ? opl3->r_port : opl3->l_port;
  61. spin_lock_irqsave(&opl3->reg_lock, flags);
  62. outb((unsigned char) cmd, port);
  63. inb(opl3->l_port);
  64. inb(opl3->l_port);
  65. outb((unsigned char) val, port + 1);
  66. inb(opl3->l_port);
  67. inb(opl3->l_port);
  68. spin_unlock_irqrestore(&opl3->reg_lock, flags);
  69. }
  70. static int snd_opl3_detect(struct snd_opl3 * opl3)
  71. {
  72. /*
  73. * This function returns 1 if the FM chip is present at the given I/O port
  74. * The detection algorithm plays with the timer built in the FM chip and
  75. * looks for a change in the status register.
  76. *
  77. * Note! The timers of the FM chip are not connected to AdLib (and compatible)
  78. * boards.
  79. *
  80. * Note2! The chip is initialized if detected.
  81. */
  82. unsigned char stat1, stat2, signature;
  83. /* Reset timers 1 and 2 */
  84. opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, OPL3_TIMER1_MASK | OPL3_TIMER2_MASK);
  85. /* Reset the IRQ of the FM chip */
  86. opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, OPL3_IRQ_RESET);
  87. signature = stat1 = inb(opl3->l_port); /* Status register */
  88. if ((stat1 & 0xe0) != 0x00) { /* Should be 0x00 */
  89. snd_printd("OPL3: stat1 = 0x%x\n", stat1);
  90. return -ENODEV;
  91. }
  92. /* Set timer1 to 0xff */
  93. opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER1, 0xff);
  94. /* Unmask and start timer 1 */
  95. opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, OPL3_TIMER2_MASK | OPL3_TIMER1_START);
  96. /* Now we have to delay at least 80us */
  97. udelay(200);
  98. /* Read status after timers have expired */
  99. stat2 = inb(opl3->l_port);
  100. /* Stop the timers */
  101. opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, OPL3_TIMER1_MASK | OPL3_TIMER2_MASK);
  102. /* Reset the IRQ of the FM chip */
  103. opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, OPL3_IRQ_RESET);
  104. if ((stat2 & 0xe0) != 0xc0) { /* There is no YM3812 */
  105. snd_printd("OPL3: stat2 = 0x%x\n", stat2);
  106. return -ENODEV;
  107. }
  108. /* If the toplevel code knows exactly the type of chip, don't try
  109. to detect it. */
  110. if (opl3->hardware != OPL3_HW_AUTO)
  111. return 0;
  112. /* There is a FM chip on this address. Detect the type (OPL2 to OPL4) */
  113. if (signature == 0x06) { /* OPL2 */
  114. opl3->hardware = OPL3_HW_OPL2;
  115. } else {
  116. /*
  117. * If we had an OPL4 chip, opl3->hardware would have been set
  118. * by the OPL4 driver; so we can assume OPL3 here.
  119. */
  120. snd_assert(opl3->r_port != 0, return -ENODEV);
  121. opl3->hardware = OPL3_HW_OPL3;
  122. }
  123. return 0;
  124. }
  125. /*
  126. * AdLib timers
  127. */
  128. /*
  129. * Timer 1 - 80us
  130. */
  131. static int snd_opl3_timer1_start(struct snd_timer * timer)
  132. {
  133. unsigned long flags;
  134. unsigned char tmp;
  135. unsigned int ticks;
  136. struct snd_opl3 *opl3;
  137. opl3 = snd_timer_chip(timer);
  138. spin_lock_irqsave(&opl3->timer_lock, flags);
  139. ticks = timer->sticks;
  140. tmp = (opl3->timer_enable | OPL3_TIMER1_START) & ~OPL3_TIMER1_MASK;
  141. opl3->timer_enable = tmp;
  142. opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER1, 256 - ticks); /* timer 1 count */
  143. opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, tmp); /* enable timer 1 IRQ */
  144. spin_unlock_irqrestore(&opl3->timer_lock, flags);
  145. return 0;
  146. }
  147. static int snd_opl3_timer1_stop(struct snd_timer * timer)
  148. {
  149. unsigned long flags;
  150. unsigned char tmp;
  151. struct snd_opl3 *opl3;
  152. opl3 = snd_timer_chip(timer);
  153. spin_lock_irqsave(&opl3->timer_lock, flags);
  154. tmp = (opl3->timer_enable | OPL3_TIMER1_MASK) & ~OPL3_TIMER1_START;
  155. opl3->timer_enable = tmp;
  156. opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, tmp); /* disable timer #1 */
  157. spin_unlock_irqrestore(&opl3->timer_lock, flags);
  158. return 0;
  159. }
  160. /*
  161. * Timer 2 - 320us
  162. */
  163. static int snd_opl3_timer2_start(struct snd_timer * timer)
  164. {
  165. unsigned long flags;
  166. unsigned char tmp;
  167. unsigned int ticks;
  168. struct snd_opl3 *opl3;
  169. opl3 = snd_timer_chip(timer);
  170. spin_lock_irqsave(&opl3->timer_lock, flags);
  171. ticks = timer->sticks;
  172. tmp = (opl3->timer_enable | OPL3_TIMER2_START) & ~OPL3_TIMER2_MASK;
  173. opl3->timer_enable = tmp;
  174. opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER2, 256 - ticks); /* timer 1 count */
  175. opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, tmp); /* enable timer 1 IRQ */
  176. spin_unlock_irqrestore(&opl3->timer_lock, flags);
  177. return 0;
  178. }
  179. static int snd_opl3_timer2_stop(struct snd_timer * timer)
  180. {
  181. unsigned long flags;
  182. unsigned char tmp;
  183. struct snd_opl3 *opl3;
  184. opl3 = snd_timer_chip(timer);
  185. spin_lock_irqsave(&opl3->timer_lock, flags);
  186. tmp = (opl3->timer_enable | OPL3_TIMER2_MASK) & ~OPL3_TIMER2_START;
  187. opl3->timer_enable = tmp;
  188. opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, tmp); /* disable timer #1 */
  189. spin_unlock_irqrestore(&opl3->timer_lock, flags);
  190. return 0;
  191. }
  192. /*
  193. */
  194. static struct snd_timer_hardware snd_opl3_timer1 =
  195. {
  196. .flags = SNDRV_TIMER_HW_STOP,
  197. .resolution = 80000,
  198. .ticks = 256,
  199. .start = snd_opl3_timer1_start,
  200. .stop = snd_opl3_timer1_stop,
  201. };
  202. static struct snd_timer_hardware snd_opl3_timer2 =
  203. {
  204. .flags = SNDRV_TIMER_HW_STOP,
  205. .resolution = 320000,
  206. .ticks = 256,
  207. .start = snd_opl3_timer2_start,
  208. .stop = snd_opl3_timer2_stop,
  209. };
  210. static int snd_opl3_timer1_init(struct snd_opl3 * opl3, int timer_no)
  211. {
  212. struct snd_timer *timer = NULL;
  213. struct snd_timer_id tid;
  214. int err;
  215. tid.dev_class = SNDRV_TIMER_CLASS_CARD;
  216. tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
  217. tid.card = opl3->card->number;
  218. tid.device = timer_no;
  219. tid.subdevice = 0;
  220. if ((err = snd_timer_new(opl3->card, "AdLib timer #1", &tid, &timer)) >= 0) {
  221. strcpy(timer->name, "AdLib timer #1");
  222. timer->private_data = opl3;
  223. timer->hw = snd_opl3_timer1;
  224. }
  225. opl3->timer1 = timer;
  226. return err;
  227. }
  228. static int snd_opl3_timer2_init(struct snd_opl3 * opl3, int timer_no)
  229. {
  230. struct snd_timer *timer = NULL;
  231. struct snd_timer_id tid;
  232. int err;
  233. tid.dev_class = SNDRV_TIMER_CLASS_CARD;
  234. tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
  235. tid.card = opl3->card->number;
  236. tid.device = timer_no;
  237. tid.subdevice = 0;
  238. if ((err = snd_timer_new(opl3->card, "AdLib timer #2", &tid, &timer)) >= 0) {
  239. strcpy(timer->name, "AdLib timer #2");
  240. timer->private_data = opl3;
  241. timer->hw = snd_opl3_timer2;
  242. }
  243. opl3->timer2 = timer;
  244. return err;
  245. }
  246. /*
  247. */
  248. void snd_opl3_interrupt(struct snd_hwdep * hw)
  249. {
  250. unsigned char status;
  251. struct snd_opl3 *opl3;
  252. struct snd_timer *timer;
  253. if (hw == NULL)
  254. return;
  255. opl3 = hw->private_data;
  256. status = inb(opl3->l_port);
  257. #if 0
  258. snd_printk("AdLib IRQ status = 0x%x\n", status);
  259. #endif
  260. if (!(status & 0x80))
  261. return;
  262. if (status & 0x40) {
  263. timer = opl3->timer1;
  264. snd_timer_interrupt(timer, timer->sticks);
  265. }
  266. if (status & 0x20) {
  267. timer = opl3->timer2;
  268. snd_timer_interrupt(timer, timer->sticks);
  269. }
  270. }
  271. /*
  272. */
  273. static int snd_opl3_free(struct snd_opl3 *opl3)
  274. {
  275. snd_assert(opl3 != NULL, return -ENXIO);
  276. if (opl3->private_free)
  277. opl3->private_free(opl3);
  278. release_and_free_resource(opl3->res_l_port);
  279. release_and_free_resource(opl3->res_r_port);
  280. kfree(opl3);
  281. return 0;
  282. }
  283. static int snd_opl3_dev_free(struct snd_device *device)
  284. {
  285. struct snd_opl3 *opl3 = device->device_data;
  286. return snd_opl3_free(opl3);
  287. }
  288. int snd_opl3_new(struct snd_card *card,
  289. unsigned short hardware,
  290. struct snd_opl3 **ropl3)
  291. {
  292. static struct snd_device_ops ops = {
  293. .dev_free = snd_opl3_dev_free,
  294. };
  295. struct snd_opl3 *opl3;
  296. int err;
  297. *ropl3 = NULL;
  298. opl3 = kzalloc(sizeof(*opl3), GFP_KERNEL);
  299. if (opl3 == NULL) {
  300. snd_printk(KERN_ERR "opl3: cannot allocate\n");
  301. return -ENOMEM;
  302. }
  303. opl3->card = card;
  304. opl3->hardware = hardware;
  305. spin_lock_init(&opl3->reg_lock);
  306. spin_lock_init(&opl3->timer_lock);
  307. init_MUTEX(&opl3->access_mutex);
  308. if ((err = snd_device_new(card, SNDRV_DEV_CODEC, opl3, &ops)) < 0) {
  309. snd_opl3_free(opl3);
  310. return err;
  311. }
  312. *ropl3 = opl3;
  313. return 0;
  314. }
  315. int snd_opl3_init(struct snd_opl3 *opl3)
  316. {
  317. if (! opl3->command) {
  318. printk(KERN_ERR "snd_opl3_init: command not defined!\n");
  319. return -EINVAL;
  320. }
  321. opl3->command(opl3, OPL3_LEFT | OPL3_REG_TEST, OPL3_ENABLE_WAVE_SELECT);
  322. /* Melodic mode */
  323. opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION, 0x00);
  324. switch (opl3->hardware & OPL3_HW_MASK) {
  325. case OPL3_HW_OPL2:
  326. opl3->max_voices = MAX_OPL2_VOICES;
  327. break;
  328. case OPL3_HW_OPL3:
  329. case OPL3_HW_OPL4:
  330. opl3->max_voices = MAX_OPL3_VOICES;
  331. /* Enter OPL3 mode */
  332. opl3->command(opl3, OPL3_RIGHT | OPL3_REG_MODE, OPL3_OPL3_ENABLE);
  333. }
  334. return 0;
  335. }
  336. int snd_opl3_create(struct snd_card *card,
  337. unsigned long l_port,
  338. unsigned long r_port,
  339. unsigned short hardware,
  340. int integrated,
  341. struct snd_opl3 ** ropl3)
  342. {
  343. struct snd_opl3 *opl3;
  344. int err;
  345. *ropl3 = NULL;
  346. if ((err = snd_opl3_new(card, hardware, &opl3)) < 0)
  347. return err;
  348. if (! integrated) {
  349. if ((opl3->res_l_port = request_region(l_port, 2, "OPL2/3 (left)")) == NULL) {
  350. snd_printk(KERN_ERR "opl3: can't grab left port 0x%lx\n", l_port);
  351. snd_device_free(card, opl3);
  352. return -EBUSY;
  353. }
  354. if (r_port != 0 &&
  355. (opl3->res_r_port = request_region(r_port, 2, "OPL2/3 (right)")) == NULL) {
  356. snd_printk(KERN_ERR "opl3: can't grab right port 0x%lx\n", r_port);
  357. snd_device_free(card, opl3);
  358. return -EBUSY;
  359. }
  360. }
  361. opl3->l_port = l_port;
  362. opl3->r_port = r_port;
  363. switch (opl3->hardware) {
  364. /* some hardware doesn't support timers */
  365. case OPL3_HW_OPL3_SV:
  366. case OPL3_HW_OPL3_CS:
  367. case OPL3_HW_OPL3_FM801:
  368. opl3->command = &snd_opl3_command;
  369. break;
  370. default:
  371. opl3->command = &snd_opl2_command;
  372. if ((err = snd_opl3_detect(opl3)) < 0) {
  373. snd_printd("OPL2/3 chip not detected at 0x%lx/0x%lx\n",
  374. opl3->l_port, opl3->r_port);
  375. snd_device_free(card, opl3);
  376. return err;
  377. }
  378. /* detect routine returns correct hardware type */
  379. switch (opl3->hardware & OPL3_HW_MASK) {
  380. case OPL3_HW_OPL3:
  381. case OPL3_HW_OPL4:
  382. opl3->command = &snd_opl3_command;
  383. }
  384. }
  385. snd_opl3_init(opl3);
  386. *ropl3 = opl3;
  387. return 0;
  388. }
  389. int snd_opl3_timer_new(struct snd_opl3 * opl3, int timer1_dev, int timer2_dev)
  390. {
  391. int err;
  392. if (timer1_dev >= 0)
  393. if ((err = snd_opl3_timer1_init(opl3, timer1_dev)) < 0)
  394. return err;
  395. if (timer2_dev >= 0) {
  396. if ((err = snd_opl3_timer2_init(opl3, timer2_dev)) < 0) {
  397. snd_device_free(opl3->card, opl3->timer1);
  398. opl3->timer1 = NULL;
  399. return err;
  400. }
  401. }
  402. return 0;
  403. }
  404. int snd_opl3_hwdep_new(struct snd_opl3 * opl3,
  405. int device, int seq_device,
  406. struct snd_hwdep ** rhwdep)
  407. {
  408. struct snd_hwdep *hw;
  409. struct snd_card *card = opl3->card;
  410. int err;
  411. if (rhwdep)
  412. *rhwdep = NULL;
  413. /* create hardware dependent device (direct FM) */
  414. if ((err = snd_hwdep_new(card, "OPL2/OPL3", device, &hw)) < 0) {
  415. snd_device_free(card, opl3);
  416. return err;
  417. }
  418. hw->private_data = opl3;
  419. #ifdef CONFIG_SND_OSSEMUL
  420. if (device == 0) {
  421. hw->oss_type = SNDRV_OSS_DEVICE_TYPE_DMFM;
  422. sprintf(hw->oss_dev, "dmfm%i", card->number);
  423. }
  424. #endif
  425. strcpy(hw->name, hw->id);
  426. switch (opl3->hardware & OPL3_HW_MASK) {
  427. case OPL3_HW_OPL2:
  428. strcpy(hw->name, "OPL2 FM");
  429. hw->iface = SNDRV_HWDEP_IFACE_OPL2;
  430. break;
  431. case OPL3_HW_OPL3:
  432. strcpy(hw->name, "OPL3 FM");
  433. hw->iface = SNDRV_HWDEP_IFACE_OPL3;
  434. break;
  435. case OPL3_HW_OPL4:
  436. strcpy(hw->name, "OPL4 FM");
  437. hw->iface = SNDRV_HWDEP_IFACE_OPL4;
  438. break;
  439. }
  440. /* operators - only ioctl */
  441. hw->ops.open = snd_opl3_open;
  442. hw->ops.ioctl = snd_opl3_ioctl;
  443. hw->ops.release = snd_opl3_release;
  444. opl3->seq_dev_num = seq_device;
  445. #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
  446. if (snd_seq_device_new(card, seq_device, SNDRV_SEQ_DEV_ID_OPL3,
  447. sizeof(struct snd_opl3 *), &opl3->seq_dev) >= 0) {
  448. strcpy(opl3->seq_dev->name, hw->name);
  449. *(struct snd_opl3 **)SNDRV_SEQ_DEVICE_ARGPTR(opl3->seq_dev) = opl3;
  450. }
  451. #endif
  452. if (rhwdep)
  453. *rhwdep = hw;
  454. return 0;
  455. }
  456. EXPORT_SYMBOL(snd_opl3_interrupt);
  457. EXPORT_SYMBOL(snd_opl3_new);
  458. EXPORT_SYMBOL(snd_opl3_init);
  459. EXPORT_SYMBOL(snd_opl3_create);
  460. EXPORT_SYMBOL(snd_opl3_timer_new);
  461. EXPORT_SYMBOL(snd_opl3_hwdep_new);
  462. /* opl3_synth.c */
  463. EXPORT_SYMBOL(snd_opl3_regmap);
  464. EXPORT_SYMBOL(snd_opl3_reset);
  465. /*
  466. * INIT part
  467. */
  468. static int __init alsa_opl3_init(void)
  469. {
  470. return 0;
  471. }
  472. static void __exit alsa_opl3_exit(void)
  473. {
  474. }
  475. module_init(alsa_opl3_init)
  476. module_exit(alsa_opl3_exit)