ymfpci.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. * The driver for the Yamaha's DS1/DS1E cards
  3. * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
  4. *
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. #include <sound/driver.h>
  22. #include <linux/init.h>
  23. #include <linux/pci.h>
  24. #include <linux/time.h>
  25. #include <linux/moduleparam.h>
  26. #include <sound/core.h>
  27. #include <sound/ymfpci.h>
  28. #include <sound/mpu401.h>
  29. #include <sound/opl3.h>
  30. #include <sound/initval.h>
  31. MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
  32. MODULE_DESCRIPTION("Yamaha DS-XG PCI");
  33. MODULE_LICENSE("GPL");
  34. MODULE_SUPPORTED_DEVICE("{{Yamaha,YMF724},"
  35. "{Yamaha,YMF724F},"
  36. "{Yamaha,YMF740},"
  37. "{Yamaha,YMF740C},"
  38. "{Yamaha,YMF744},"
  39. "{Yamaha,YMF754}}");
  40. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
  41. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
  42. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
  43. static long fm_port[SNDRV_CARDS];
  44. static long mpu_port[SNDRV_CARDS];
  45. #ifdef SUPPORT_JOYSTICK
  46. static long joystick_port[SNDRV_CARDS];
  47. #endif
  48. static int rear_switch[SNDRV_CARDS];
  49. module_param_array(index, int, NULL, 0444);
  50. MODULE_PARM_DESC(index, "Index value for the Yamaha DS-XG PCI soundcard.");
  51. module_param_array(id, charp, NULL, 0444);
  52. MODULE_PARM_DESC(id, "ID string for the Yamaha DS-XG PCI soundcard.");
  53. module_param_array(enable, bool, NULL, 0444);
  54. MODULE_PARM_DESC(enable, "Enable Yamaha DS-XG soundcard.");
  55. module_param_array(mpu_port, long, NULL, 0444);
  56. MODULE_PARM_DESC(mpu_port, "MPU-401 Port.");
  57. module_param_array(fm_port, long, NULL, 0444);
  58. MODULE_PARM_DESC(fm_port, "FM OPL-3 Port.");
  59. #ifdef SUPPORT_JOYSTICK
  60. module_param_array(joystick_port, long, NULL, 0444);
  61. MODULE_PARM_DESC(joystick_port, "Joystick port address");
  62. #endif
  63. module_param_array(rear_switch, bool, NULL, 0444);
  64. MODULE_PARM_DESC(rear_switch, "Enable shared rear/line-in switch");
  65. static struct pci_device_id snd_ymfpci_ids[] = {
  66. { 0x1073, 0x0004, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, /* YMF724 */
  67. { 0x1073, 0x000d, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, /* YMF724F */
  68. { 0x1073, 0x000a, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, /* YMF740 */
  69. { 0x1073, 0x000c, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, /* YMF740C */
  70. { 0x1073, 0x0010, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, /* YMF744 */
  71. { 0x1073, 0x0012, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, /* YMF754 */
  72. { 0, }
  73. };
  74. MODULE_DEVICE_TABLE(pci, snd_ymfpci_ids);
  75. #ifdef SUPPORT_JOYSTICK
  76. static int __devinit snd_ymfpci_create_gameport(ymfpci_t *chip, int dev,
  77. int legacy_ctrl, int legacy_ctrl2)
  78. {
  79. struct gameport *gp;
  80. struct resource *r = NULL;
  81. int io_port = joystick_port[dev];
  82. if (!io_port)
  83. return -ENODEV;
  84. if (chip->pci->device >= 0x0010) { /* YMF 744/754 */
  85. if (io_port == 1) {
  86. /* auto-detect */
  87. if (!(io_port = pci_resource_start(chip->pci, 2)))
  88. return -ENODEV;
  89. }
  90. } else {
  91. if (io_port == 1) {
  92. /* auto-detect */
  93. for (io_port = 0x201; io_port <= 0x205; io_port++) {
  94. if (io_port == 0x203)
  95. continue;
  96. if ((r = request_region(io_port, 1, "YMFPCI gameport")) != NULL)
  97. break;
  98. }
  99. if (!r) {
  100. printk(KERN_ERR "ymfpci: no gameport ports available\n");
  101. return -EBUSY;
  102. }
  103. }
  104. switch (io_port) {
  105. case 0x201: legacy_ctrl2 |= 0 << 6; break;
  106. case 0x202: legacy_ctrl2 |= 1 << 6; break;
  107. case 0x204: legacy_ctrl2 |= 2 << 6; break;
  108. case 0x205: legacy_ctrl2 |= 3 << 6; break;
  109. default:
  110. printk(KERN_ERR "ymfpci: invalid joystick port %#x", io_port);
  111. return -EINVAL;
  112. }
  113. }
  114. if (!r && !(r = request_region(io_port, 1, "YMFPCI gameport"))) {
  115. printk(KERN_ERR "ymfpci: joystick port %#x is in use.\n", io_port);
  116. return -EBUSY;
  117. }
  118. chip->gameport = gp = gameport_allocate_port();
  119. if (!gp) {
  120. printk(KERN_ERR "ymfpci: cannot allocate memory for gameport\n");
  121. release_resource(r);
  122. kfree_nocheck(r);
  123. return -ENOMEM;
  124. }
  125. gameport_set_name(gp, "Yamaha YMF Gameport");
  126. gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
  127. gameport_set_dev_parent(gp, &chip->pci->dev);
  128. gp->io = io_port;
  129. gameport_set_port_data(gp, r);
  130. if (chip->pci->device >= 0x0010) /* YMF 744/754 */
  131. pci_write_config_word(chip->pci, PCIR_DSXG_JOYBASE, io_port);
  132. pci_write_config_word(chip->pci, PCIR_DSXG_LEGACY, legacy_ctrl | YMFPCI_LEGACY_JPEN);
  133. pci_write_config_word(chip->pci, PCIR_DSXG_ELEGACY, legacy_ctrl2);
  134. gameport_register_port(chip->gameport);
  135. return 0;
  136. }
  137. void snd_ymfpci_free_gameport(ymfpci_t *chip)
  138. {
  139. if (chip->gameport) {
  140. struct resource *r = gameport_get_port_data(chip->gameport);
  141. gameport_unregister_port(chip->gameport);
  142. chip->gameport = NULL;
  143. release_resource(r);
  144. kfree_nocheck(r);
  145. }
  146. }
  147. #else
  148. static inline int snd_ymfpci_create_gameport(ymfpci_t *chip, int dev, int l, int l2) { return -ENOSYS; }
  149. void snd_ymfpci_free_gameport(ymfpci_t *chip) { }
  150. #endif /* SUPPORT_JOYSTICK */
  151. static int __devinit snd_card_ymfpci_probe(struct pci_dev *pci,
  152. const struct pci_device_id *pci_id)
  153. {
  154. static int dev;
  155. snd_card_t *card;
  156. struct resource *fm_res = NULL;
  157. struct resource *mpu_res = NULL;
  158. ymfpci_t *chip;
  159. opl3_t *opl3;
  160. char *str;
  161. int err;
  162. u16 legacy_ctrl, legacy_ctrl2, old_legacy_ctrl;
  163. if (dev >= SNDRV_CARDS)
  164. return -ENODEV;
  165. if (!enable[dev]) {
  166. dev++;
  167. return -ENOENT;
  168. }
  169. card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
  170. if (card == NULL)
  171. return -ENOMEM;
  172. switch (pci_id->device) {
  173. case 0x0004: str = "YMF724"; break;
  174. case 0x000d: str = "YMF724F"; break;
  175. case 0x000a: str = "YMF740"; break;
  176. case 0x000c: str = "YMF740C"; break;
  177. case 0x0010: str = "YMF744"; break;
  178. case 0x0012: str = "YMF754"; break;
  179. default: str = "???"; break;
  180. }
  181. legacy_ctrl = 0;
  182. legacy_ctrl2 = 0x0800; /* SBEN = 0, SMOD = 01, LAD = 0 */
  183. if (pci_id->device >= 0x0010) { /* YMF 744/754 */
  184. if (fm_port[dev] == 1) {
  185. /* auto-detect */
  186. fm_port[dev] = pci_resource_start(pci, 1);
  187. }
  188. if (fm_port[dev] > 0 &&
  189. (fm_res = request_region(fm_port[dev], 4, "YMFPCI OPL3")) != NULL) {
  190. legacy_ctrl |= YMFPCI_LEGACY_FMEN;
  191. pci_write_config_word(pci, PCIR_DSXG_FMBASE, fm_port[dev]);
  192. }
  193. if (mpu_port[dev] == 1) {
  194. /* auto-detect */
  195. mpu_port[dev] = pci_resource_start(pci, 1) + 0x20;
  196. }
  197. if (mpu_port[dev] > 0 &&
  198. (mpu_res = request_region(mpu_port[dev], 2, "YMFPCI MPU401")) != NULL) {
  199. legacy_ctrl |= YMFPCI_LEGACY_MEN;
  200. pci_write_config_word(pci, PCIR_DSXG_MPU401BASE, mpu_port[dev]);
  201. }
  202. } else {
  203. switch (fm_port[dev]) {
  204. case 0x388: legacy_ctrl2 |= 0; break;
  205. case 0x398: legacy_ctrl2 |= 1; break;
  206. case 0x3a0: legacy_ctrl2 |= 2; break;
  207. case 0x3a8: legacy_ctrl2 |= 3; break;
  208. default: fm_port[dev] = 0; break;
  209. }
  210. if (fm_port[dev] > 0 &&
  211. (fm_res = request_region(fm_port[dev], 4, "YMFPCI OPL3")) != NULL) {
  212. legacy_ctrl |= YMFPCI_LEGACY_FMEN;
  213. } else {
  214. legacy_ctrl2 &= ~YMFPCI_LEGACY2_FMIO;
  215. fm_port[dev] = 0;
  216. }
  217. switch (mpu_port[dev]) {
  218. case 0x330: legacy_ctrl2 |= 0 << 4; break;
  219. case 0x300: legacy_ctrl2 |= 1 << 4; break;
  220. case 0x332: legacy_ctrl2 |= 2 << 4; break;
  221. case 0x334: legacy_ctrl2 |= 3 << 4; break;
  222. default: mpu_port[dev] = 0; break;
  223. }
  224. if (mpu_port[dev] > 0 &&
  225. (mpu_res = request_region(mpu_port[dev], 2, "YMFPCI MPU401")) != NULL) {
  226. legacy_ctrl |= YMFPCI_LEGACY_MEN;
  227. } else {
  228. legacy_ctrl2 &= ~YMFPCI_LEGACY2_MPUIO;
  229. mpu_port[dev] = 0;
  230. }
  231. }
  232. if (mpu_res) {
  233. legacy_ctrl |= YMFPCI_LEGACY_MIEN;
  234. legacy_ctrl2 |= YMFPCI_LEGACY2_IMOD;
  235. }
  236. pci_read_config_word(pci, PCIR_DSXG_LEGACY, &old_legacy_ctrl);
  237. pci_write_config_word(pci, PCIR_DSXG_LEGACY, legacy_ctrl);
  238. pci_write_config_word(pci, PCIR_DSXG_ELEGACY, legacy_ctrl2);
  239. if ((err = snd_ymfpci_create(card, pci,
  240. old_legacy_ctrl,
  241. &chip)) < 0) {
  242. snd_card_free(card);
  243. if (mpu_res) {
  244. release_resource(mpu_res);
  245. kfree_nocheck(mpu_res);
  246. }
  247. if (fm_res) {
  248. release_resource(fm_res);
  249. kfree_nocheck(fm_res);
  250. }
  251. return err;
  252. }
  253. chip->fm_res = fm_res;
  254. chip->mpu_res = mpu_res;
  255. strcpy(card->driver, str);
  256. sprintf(card->shortname, "Yamaha DS-XG (%s)", str);
  257. sprintf(card->longname, "%s at 0x%lx, irq %i",
  258. card->shortname,
  259. chip->reg_area_phys,
  260. chip->irq);
  261. if ((err = snd_ymfpci_pcm(chip, 0, NULL)) < 0) {
  262. snd_card_free(card);
  263. return err;
  264. }
  265. if ((err = snd_ymfpci_pcm_spdif(chip, 1, NULL)) < 0) {
  266. snd_card_free(card);
  267. return err;
  268. }
  269. if ((err = snd_ymfpci_pcm_4ch(chip, 2, NULL)) < 0) {
  270. snd_card_free(card);
  271. return err;
  272. }
  273. if ((err = snd_ymfpci_pcm2(chip, 3, NULL)) < 0) {
  274. snd_card_free(card);
  275. return err;
  276. }
  277. if ((err = snd_ymfpci_mixer(chip, rear_switch[dev])) < 0) {
  278. snd_card_free(card);
  279. return err;
  280. }
  281. if ((err = snd_ymfpci_timer(chip, 0)) < 0) {
  282. snd_card_free(card);
  283. return err;
  284. }
  285. if (chip->mpu_res) {
  286. if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_YMFPCI,
  287. mpu_port[dev], 1,
  288. pci->irq, 0, &chip->rawmidi)) < 0) {
  289. printk(KERN_WARNING "ymfpci: cannot initialize MPU401 at 0x%lx, skipping...\n", mpu_port[dev]);
  290. legacy_ctrl &= ~YMFPCI_LEGACY_MIEN; /* disable MPU401 irq */
  291. pci_write_config_word(pci, PCIR_DSXG_LEGACY, legacy_ctrl);
  292. }
  293. }
  294. if (chip->fm_res) {
  295. if ((err = snd_opl3_create(card,
  296. fm_port[dev],
  297. fm_port[dev] + 2,
  298. OPL3_HW_OPL3, 1, &opl3)) < 0) {
  299. printk(KERN_WARNING "ymfpci: cannot initialize FM OPL3 at 0x%lx, skipping...\n", fm_port[dev]);
  300. legacy_ctrl &= ~YMFPCI_LEGACY_FMEN;
  301. pci_write_config_word(pci, PCIR_DSXG_LEGACY, legacy_ctrl);
  302. } else if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
  303. snd_card_free(card);
  304. snd_printk("cannot create opl3 hwdep\n");
  305. return err;
  306. }
  307. }
  308. snd_ymfpci_create_gameport(chip, dev, legacy_ctrl, legacy_ctrl2);
  309. if ((err = snd_card_register(card)) < 0) {
  310. snd_card_free(card);
  311. return err;
  312. }
  313. pci_set_drvdata(pci, card);
  314. dev++;
  315. return 0;
  316. }
  317. static void __devexit snd_card_ymfpci_remove(struct pci_dev *pci)
  318. {
  319. snd_card_free(pci_get_drvdata(pci));
  320. pci_set_drvdata(pci, NULL);
  321. }
  322. static struct pci_driver driver = {
  323. .name = "Yamaha DS-XG PCI",
  324. .id_table = snd_ymfpci_ids,
  325. .probe = snd_card_ymfpci_probe,
  326. .remove = __devexit_p(snd_card_ymfpci_remove),
  327. SND_PCI_PM_CALLBACKS
  328. };
  329. static int __init alsa_card_ymfpci_init(void)
  330. {
  331. return pci_register_driver(&driver);
  332. }
  333. static void __exit alsa_card_ymfpci_exit(void)
  334. {
  335. pci_unregister_driver(&driver);
  336. }
  337. module_init(alsa_card_ymfpci_init)
  338. module_exit(alsa_card_ymfpci_exit)