opl3_lib.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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. EXPORT_SYMBOL(snd_opl3_interrupt);
  272. /*
  273. */
  274. static int snd_opl3_free(struct snd_opl3 *opl3)
  275. {
  276. snd_assert(opl3 != NULL, return -ENXIO);
  277. if (opl3->private_free)
  278. opl3->private_free(opl3);
  279. release_and_free_resource(opl3->res_l_port);
  280. release_and_free_resource(opl3->res_r_port);
  281. kfree(opl3);
  282. return 0;
  283. }
  284. static int snd_opl3_dev_free(struct snd_device *device)
  285. {
  286. struct snd_opl3 *opl3 = device->device_data;
  287. return snd_opl3_free(opl3);
  288. }
  289. int snd_opl3_new(struct snd_card *card,
  290. unsigned short hardware,
  291. struct snd_opl3 **ropl3)
  292. {
  293. static struct snd_device_ops ops = {
  294. .dev_free = snd_opl3_dev_free,
  295. };
  296. struct snd_opl3 *opl3;
  297. int err;
  298. *ropl3 = NULL;
  299. opl3 = kzalloc(sizeof(*opl3), GFP_KERNEL);
  300. if (opl3 == NULL) {
  301. snd_printk(KERN_ERR "opl3: cannot allocate\n");
  302. return -ENOMEM;
  303. }
  304. opl3->card = card;
  305. opl3->hardware = hardware;
  306. spin_lock_init(&opl3->reg_lock);
  307. spin_lock_init(&opl3->timer_lock);
  308. mutex_init(&opl3->access_mutex);
  309. if ((err = snd_device_new(card, SNDRV_DEV_CODEC, opl3, &ops)) < 0) {
  310. snd_opl3_free(opl3);
  311. return err;
  312. }
  313. *ropl3 = opl3;
  314. return 0;
  315. }
  316. EXPORT_SYMBOL(snd_opl3_new);
  317. int snd_opl3_init(struct snd_opl3 *opl3)
  318. {
  319. if (! opl3->command) {
  320. printk(KERN_ERR "snd_opl3_init: command not defined!\n");
  321. return -EINVAL;
  322. }
  323. opl3->command(opl3, OPL3_LEFT | OPL3_REG_TEST, OPL3_ENABLE_WAVE_SELECT);
  324. /* Melodic mode */
  325. opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION, 0x00);
  326. switch (opl3->hardware & OPL3_HW_MASK) {
  327. case OPL3_HW_OPL2:
  328. opl3->max_voices = MAX_OPL2_VOICES;
  329. break;
  330. case OPL3_HW_OPL3:
  331. case OPL3_HW_OPL4:
  332. opl3->max_voices = MAX_OPL3_VOICES;
  333. /* Enter OPL3 mode */
  334. opl3->command(opl3, OPL3_RIGHT | OPL3_REG_MODE, OPL3_OPL3_ENABLE);
  335. }
  336. return 0;
  337. }
  338. EXPORT_SYMBOL(snd_opl3_init);
  339. int snd_opl3_create(struct snd_card *card,
  340. unsigned long l_port,
  341. unsigned long r_port,
  342. unsigned short hardware,
  343. int integrated,
  344. struct snd_opl3 ** ropl3)
  345. {
  346. struct snd_opl3 *opl3;
  347. int err;
  348. *ropl3 = NULL;
  349. if ((err = snd_opl3_new(card, hardware, &opl3)) < 0)
  350. return err;
  351. if (! integrated) {
  352. if ((opl3->res_l_port = request_region(l_port, 2, "OPL2/3 (left)")) == NULL) {
  353. snd_printk(KERN_ERR "opl3: can't grab left port 0x%lx\n", l_port);
  354. snd_device_free(card, opl3);
  355. return -EBUSY;
  356. }
  357. if (r_port != 0 &&
  358. (opl3->res_r_port = request_region(r_port, 2, "OPL2/3 (right)")) == NULL) {
  359. snd_printk(KERN_ERR "opl3: can't grab right port 0x%lx\n", r_port);
  360. snd_device_free(card, opl3);
  361. return -EBUSY;
  362. }
  363. }
  364. opl3->l_port = l_port;
  365. opl3->r_port = r_port;
  366. switch (opl3->hardware) {
  367. /* some hardware doesn't support timers */
  368. case OPL3_HW_OPL3_SV:
  369. case OPL3_HW_OPL3_CS:
  370. case OPL3_HW_OPL3_FM801:
  371. opl3->command = &snd_opl3_command;
  372. break;
  373. default:
  374. opl3->command = &snd_opl2_command;
  375. if ((err = snd_opl3_detect(opl3)) < 0) {
  376. snd_printd("OPL2/3 chip not detected at 0x%lx/0x%lx\n",
  377. opl3->l_port, opl3->r_port);
  378. snd_device_free(card, opl3);
  379. return err;
  380. }
  381. /* detect routine returns correct hardware type */
  382. switch (opl3->hardware & OPL3_HW_MASK) {
  383. case OPL3_HW_OPL3:
  384. case OPL3_HW_OPL4:
  385. opl3->command = &snd_opl3_command;
  386. }
  387. }
  388. snd_opl3_init(opl3);
  389. *ropl3 = opl3;
  390. return 0;
  391. }
  392. EXPORT_SYMBOL(snd_opl3_create);
  393. int snd_opl3_timer_new(struct snd_opl3 * opl3, int timer1_dev, int timer2_dev)
  394. {
  395. int err;
  396. if (timer1_dev >= 0)
  397. if ((err = snd_opl3_timer1_init(opl3, timer1_dev)) < 0)
  398. return err;
  399. if (timer2_dev >= 0) {
  400. if ((err = snd_opl3_timer2_init(opl3, timer2_dev)) < 0) {
  401. snd_device_free(opl3->card, opl3->timer1);
  402. opl3->timer1 = NULL;
  403. return err;
  404. }
  405. }
  406. return 0;
  407. }
  408. EXPORT_SYMBOL(snd_opl3_timer_new);
  409. int snd_opl3_hwdep_new(struct snd_opl3 * opl3,
  410. int device, int seq_device,
  411. struct snd_hwdep ** rhwdep)
  412. {
  413. struct snd_hwdep *hw;
  414. struct snd_card *card = opl3->card;
  415. int err;
  416. if (rhwdep)
  417. *rhwdep = NULL;
  418. /* create hardware dependent device (direct FM) */
  419. if ((err = snd_hwdep_new(card, "OPL2/OPL3", device, &hw)) < 0) {
  420. snd_device_free(card, opl3);
  421. return err;
  422. }
  423. hw->private_data = opl3;
  424. #ifdef CONFIG_SND_OSSEMUL
  425. if (device == 0) {
  426. hw->oss_type = SNDRV_OSS_DEVICE_TYPE_DMFM;
  427. sprintf(hw->oss_dev, "dmfm%i", card->number);
  428. }
  429. #endif
  430. strcpy(hw->name, hw->id);
  431. switch (opl3->hardware & OPL3_HW_MASK) {
  432. case OPL3_HW_OPL2:
  433. strcpy(hw->name, "OPL2 FM");
  434. hw->iface = SNDRV_HWDEP_IFACE_OPL2;
  435. break;
  436. case OPL3_HW_OPL3:
  437. strcpy(hw->name, "OPL3 FM");
  438. hw->iface = SNDRV_HWDEP_IFACE_OPL3;
  439. break;
  440. case OPL3_HW_OPL4:
  441. strcpy(hw->name, "OPL4 FM");
  442. hw->iface = SNDRV_HWDEP_IFACE_OPL4;
  443. break;
  444. }
  445. /* operators - only ioctl */
  446. hw->ops.open = snd_opl3_open;
  447. hw->ops.ioctl = snd_opl3_ioctl;
  448. hw->ops.release = snd_opl3_release;
  449. opl3->seq_dev_num = seq_device;
  450. #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
  451. if (snd_seq_device_new(card, seq_device, SNDRV_SEQ_DEV_ID_OPL3,
  452. sizeof(struct snd_opl3 *), &opl3->seq_dev) >= 0) {
  453. strcpy(opl3->seq_dev->name, hw->name);
  454. *(struct snd_opl3 **)SNDRV_SEQ_DEVICE_ARGPTR(opl3->seq_dev) = opl3;
  455. }
  456. #endif
  457. if (rhwdep)
  458. *rhwdep = hw;
  459. return 0;
  460. }
  461. EXPORT_SYMBOL(snd_opl3_hwdep_new);
  462. /*
  463. * INIT part
  464. */
  465. static int __init alsa_opl3_init(void)
  466. {
  467. return 0;
  468. }
  469. static void __exit alsa_opl3_exit(void)
  470. {
  471. }
  472. module_init(alsa_opl3_init)
  473. module_exit(alsa_opl3_exit)