ymfpci.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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-1 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-1 PCI soundcard.");
  51. module_param_array(id, charp, NULL, 0444);
  52. MODULE_PARM_DESC(id, "ID string for the Yamaha DS-1 PCI soundcard.");
  53. module_param_array(enable, bool, NULL, 0444);
  54. MODULE_PARM_DESC(enable, "Enable Yamaha DS-1 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(struct snd_ymfpci *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_and_free_resource(r);
  122. return -ENOMEM;
  123. }
  124. gameport_set_name(gp, "Yamaha YMF Gameport");
  125. gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
  126. gameport_set_dev_parent(gp, &chip->pci->dev);
  127. gp->io = io_port;
  128. gameport_set_port_data(gp, r);
  129. if (chip->pci->device >= 0x0010) /* YMF 744/754 */
  130. pci_write_config_word(chip->pci, PCIR_DSXG_JOYBASE, io_port);
  131. pci_write_config_word(chip->pci, PCIR_DSXG_LEGACY, legacy_ctrl | YMFPCI_LEGACY_JPEN);
  132. pci_write_config_word(chip->pci, PCIR_DSXG_ELEGACY, legacy_ctrl2);
  133. gameport_register_port(chip->gameport);
  134. return 0;
  135. }
  136. void snd_ymfpci_free_gameport(struct snd_ymfpci *chip)
  137. {
  138. if (chip->gameport) {
  139. struct resource *r = gameport_get_port_data(chip->gameport);
  140. gameport_unregister_port(chip->gameport);
  141. chip->gameport = NULL;
  142. release_and_free_resource(r);
  143. }
  144. }
  145. #else
  146. static inline int snd_ymfpci_create_gameport(struct snd_ymfpci *chip, int dev, int l, int l2) { return -ENOSYS; }
  147. void snd_ymfpci_free_gameport(struct snd_ymfpci *chip) { }
  148. #endif /* SUPPORT_JOYSTICK */
  149. static int __devinit snd_card_ymfpci_probe(struct pci_dev *pci,
  150. const struct pci_device_id *pci_id)
  151. {
  152. static int dev;
  153. struct snd_card *card;
  154. struct resource *fm_res = NULL;
  155. struct resource *mpu_res = NULL;
  156. struct snd_ymfpci *chip;
  157. struct snd_opl3 *opl3;
  158. const char *str, *model;
  159. int err;
  160. u16 legacy_ctrl, legacy_ctrl2, old_legacy_ctrl;
  161. if (dev >= SNDRV_CARDS)
  162. return -ENODEV;
  163. if (!enable[dev]) {
  164. dev++;
  165. return -ENOENT;
  166. }
  167. card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
  168. if (card == NULL)
  169. return -ENOMEM;
  170. switch (pci_id->device) {
  171. case 0x0004: str = "YMF724"; model = "DS-1"; break;
  172. case 0x000d: str = "YMF724F"; model = "DS-1"; break;
  173. case 0x000a: str = "YMF740"; model = "DS-1L"; break;
  174. case 0x000c: str = "YMF740C"; model = "DS-1L"; break;
  175. case 0x0010: str = "YMF744"; model = "DS-1S"; break;
  176. case 0x0012: str = "YMF754"; model = "DS-1E"; break;
  177. default: model = str = "???"; break;
  178. }
  179. legacy_ctrl = 0;
  180. legacy_ctrl2 = 0x0800; /* SBEN = 0, SMOD = 01, LAD = 0 */
  181. if (pci_id->device >= 0x0010) { /* YMF 744/754 */
  182. if (fm_port[dev] == 1) {
  183. /* auto-detect */
  184. fm_port[dev] = pci_resource_start(pci, 1);
  185. }
  186. if (fm_port[dev] > 0 &&
  187. (fm_res = request_region(fm_port[dev], 4, "YMFPCI OPL3")) != NULL) {
  188. legacy_ctrl |= YMFPCI_LEGACY_FMEN;
  189. pci_write_config_word(pci, PCIR_DSXG_FMBASE, fm_port[dev]);
  190. }
  191. if (mpu_port[dev] == 1) {
  192. /* auto-detect */
  193. mpu_port[dev] = pci_resource_start(pci, 1) + 0x20;
  194. }
  195. if (mpu_port[dev] > 0 &&
  196. (mpu_res = request_region(mpu_port[dev], 2, "YMFPCI MPU401")) != NULL) {
  197. legacy_ctrl |= YMFPCI_LEGACY_MEN;
  198. pci_write_config_word(pci, PCIR_DSXG_MPU401BASE, mpu_port[dev]);
  199. }
  200. } else {
  201. switch (fm_port[dev]) {
  202. case 0x388: legacy_ctrl2 |= 0; break;
  203. case 0x398: legacy_ctrl2 |= 1; break;
  204. case 0x3a0: legacy_ctrl2 |= 2; break;
  205. case 0x3a8: legacy_ctrl2 |= 3; break;
  206. default: fm_port[dev] = 0; break;
  207. }
  208. if (fm_port[dev] > 0 &&
  209. (fm_res = request_region(fm_port[dev], 4, "YMFPCI OPL3")) != NULL) {
  210. legacy_ctrl |= YMFPCI_LEGACY_FMEN;
  211. } else {
  212. legacy_ctrl2 &= ~YMFPCI_LEGACY2_FMIO;
  213. fm_port[dev] = 0;
  214. }
  215. switch (mpu_port[dev]) {
  216. case 0x330: legacy_ctrl2 |= 0 << 4; break;
  217. case 0x300: legacy_ctrl2 |= 1 << 4; break;
  218. case 0x332: legacy_ctrl2 |= 2 << 4; break;
  219. case 0x334: legacy_ctrl2 |= 3 << 4; break;
  220. default: mpu_port[dev] = 0; break;
  221. }
  222. if (mpu_port[dev] > 0 &&
  223. (mpu_res = request_region(mpu_port[dev], 2, "YMFPCI MPU401")) != NULL) {
  224. legacy_ctrl |= YMFPCI_LEGACY_MEN;
  225. } else {
  226. legacy_ctrl2 &= ~YMFPCI_LEGACY2_MPUIO;
  227. mpu_port[dev] = 0;
  228. }
  229. }
  230. if (mpu_res) {
  231. legacy_ctrl |= YMFPCI_LEGACY_MIEN;
  232. legacy_ctrl2 |= YMFPCI_LEGACY2_IMOD;
  233. }
  234. pci_read_config_word(pci, PCIR_DSXG_LEGACY, &old_legacy_ctrl);
  235. pci_write_config_word(pci, PCIR_DSXG_LEGACY, legacy_ctrl);
  236. pci_write_config_word(pci, PCIR_DSXG_ELEGACY, legacy_ctrl2);
  237. if ((err = snd_ymfpci_create(card, pci,
  238. old_legacy_ctrl,
  239. &chip)) < 0) {
  240. snd_card_free(card);
  241. release_and_free_resource(mpu_res);
  242. release_and_free_resource(fm_res);
  243. return err;
  244. }
  245. chip->fm_res = fm_res;
  246. chip->mpu_res = mpu_res;
  247. card->private_data = chip;
  248. strcpy(card->driver, str);
  249. sprintf(card->shortname, "Yamaha %s (%s)", model, str);
  250. sprintf(card->longname, "%s at 0x%lx, irq %i",
  251. card->shortname,
  252. chip->reg_area_phys,
  253. chip->irq);
  254. if ((err = snd_ymfpci_pcm(chip, 0, NULL)) < 0) {
  255. snd_card_free(card);
  256. return err;
  257. }
  258. if ((err = snd_ymfpci_pcm_spdif(chip, 1, NULL)) < 0) {
  259. snd_card_free(card);
  260. return err;
  261. }
  262. if ((err = snd_ymfpci_pcm_4ch(chip, 2, NULL)) < 0) {
  263. snd_card_free(card);
  264. return err;
  265. }
  266. if ((err = snd_ymfpci_pcm2(chip, 3, NULL)) < 0) {
  267. snd_card_free(card);
  268. return err;
  269. }
  270. if ((err = snd_ymfpci_mixer(chip, rear_switch[dev])) < 0) {
  271. snd_card_free(card);
  272. return err;
  273. }
  274. if ((err = snd_ymfpci_timer(chip, 0)) < 0) {
  275. snd_card_free(card);
  276. return err;
  277. }
  278. if (chip->mpu_res) {
  279. if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_YMFPCI,
  280. mpu_port[dev], 1,
  281. pci->irq, 0, &chip->rawmidi)) < 0) {
  282. printk(KERN_WARNING "ymfpci: cannot initialize MPU401 at 0x%lx, skipping...\n", mpu_port[dev]);
  283. legacy_ctrl &= ~YMFPCI_LEGACY_MIEN; /* disable MPU401 irq */
  284. pci_write_config_word(pci, PCIR_DSXG_LEGACY, legacy_ctrl);
  285. }
  286. }
  287. if (chip->fm_res) {
  288. if ((err = snd_opl3_create(card,
  289. fm_port[dev],
  290. fm_port[dev] + 2,
  291. OPL3_HW_OPL3, 1, &opl3)) < 0) {
  292. printk(KERN_WARNING "ymfpci: cannot initialize FM OPL3 at 0x%lx, skipping...\n", fm_port[dev]);
  293. legacy_ctrl &= ~YMFPCI_LEGACY_FMEN;
  294. pci_write_config_word(pci, PCIR_DSXG_LEGACY, legacy_ctrl);
  295. } else if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
  296. snd_card_free(card);
  297. snd_printk(KERN_ERR "cannot create opl3 hwdep\n");
  298. return err;
  299. }
  300. }
  301. snd_ymfpci_create_gameport(chip, dev, legacy_ctrl, legacy_ctrl2);
  302. if ((err = snd_card_register(card)) < 0) {
  303. snd_card_free(card);
  304. return err;
  305. }
  306. pci_set_drvdata(pci, card);
  307. dev++;
  308. return 0;
  309. }
  310. static void __devexit snd_card_ymfpci_remove(struct pci_dev *pci)
  311. {
  312. snd_card_free(pci_get_drvdata(pci));
  313. pci_set_drvdata(pci, NULL);
  314. }
  315. static struct pci_driver driver = {
  316. .name = "Yamaha DS-1 PCI",
  317. .id_table = snd_ymfpci_ids,
  318. .probe = snd_card_ymfpci_probe,
  319. .remove = __devexit_p(snd_card_ymfpci_remove),
  320. #ifdef CONFIG_PM
  321. .suspend = snd_ymfpci_suspend,
  322. .resume = snd_ymfpci_resume,
  323. #endif
  324. };
  325. static int __init alsa_card_ymfpci_init(void)
  326. {
  327. return pci_register_driver(&driver);
  328. }
  329. static void __exit alsa_card_ymfpci_exit(void)
  330. {
  331. pci_unregister_driver(&driver);
  332. }
  333. module_init(alsa_card_ymfpci_init)
  334. module_exit(alsa_card_ymfpci_exit)