ymfpci.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. static int rear_swap[SNDRV_CARDS] = { [0 ... (SNDRV_CARDS-1)] = 1 };
  50. module_param_array(index, int, NULL, 0444);
  51. MODULE_PARM_DESC(index, "Index value for the Yamaha DS-1 PCI soundcard.");
  52. module_param_array(id, charp, NULL, 0444);
  53. MODULE_PARM_DESC(id, "ID string for the Yamaha DS-1 PCI soundcard.");
  54. module_param_array(enable, bool, NULL, 0444);
  55. MODULE_PARM_DESC(enable, "Enable Yamaha DS-1 soundcard.");
  56. module_param_array(mpu_port, long, NULL, 0444);
  57. MODULE_PARM_DESC(mpu_port, "MPU-401 Port.");
  58. module_param_array(fm_port, long, NULL, 0444);
  59. MODULE_PARM_DESC(fm_port, "FM OPL-3 Port.");
  60. #ifdef SUPPORT_JOYSTICK
  61. module_param_array(joystick_port, long, NULL, 0444);
  62. MODULE_PARM_DESC(joystick_port, "Joystick port address");
  63. #endif
  64. module_param_array(rear_switch, bool, NULL, 0444);
  65. MODULE_PARM_DESC(rear_switch, "Enable shared rear/line-in switch");
  66. module_param_array(rear_swap, bool, NULL, 0444);
  67. MODULE_PARM_DESC(rear_swap, "Swap rear channels (must be enabled for correct IEC958 (S/PDIF)) output");
  68. static struct pci_device_id snd_ymfpci_ids[] = {
  69. { 0x1073, 0x0004, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, /* YMF724 */
  70. { 0x1073, 0x000d, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, /* YMF724F */
  71. { 0x1073, 0x000a, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, /* YMF740 */
  72. { 0x1073, 0x000c, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, /* YMF740C */
  73. { 0x1073, 0x0010, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, /* YMF744 */
  74. { 0x1073, 0x0012, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, /* YMF754 */
  75. { 0, }
  76. };
  77. MODULE_DEVICE_TABLE(pci, snd_ymfpci_ids);
  78. #ifdef SUPPORT_JOYSTICK
  79. static int __devinit snd_ymfpci_create_gameport(struct snd_ymfpci *chip, int dev,
  80. int legacy_ctrl, int legacy_ctrl2)
  81. {
  82. struct gameport *gp;
  83. struct resource *r = NULL;
  84. int io_port = joystick_port[dev];
  85. if (!io_port)
  86. return -ENODEV;
  87. if (chip->pci->device >= 0x0010) { /* YMF 744/754 */
  88. if (io_port == 1) {
  89. /* auto-detect */
  90. if (!(io_port = pci_resource_start(chip->pci, 2)))
  91. return -ENODEV;
  92. }
  93. } else {
  94. if (io_port == 1) {
  95. /* auto-detect */
  96. for (io_port = 0x201; io_port <= 0x205; io_port++) {
  97. if (io_port == 0x203)
  98. continue;
  99. if ((r = request_region(io_port, 1, "YMFPCI gameport")) != NULL)
  100. break;
  101. }
  102. if (!r) {
  103. printk(KERN_ERR "ymfpci: no gameport ports available\n");
  104. return -EBUSY;
  105. }
  106. }
  107. switch (io_port) {
  108. case 0x201: legacy_ctrl2 |= 0 << 6; break;
  109. case 0x202: legacy_ctrl2 |= 1 << 6; break;
  110. case 0x204: legacy_ctrl2 |= 2 << 6; break;
  111. case 0x205: legacy_ctrl2 |= 3 << 6; break;
  112. default:
  113. printk(KERN_ERR "ymfpci: invalid joystick port %#x", io_port);
  114. return -EINVAL;
  115. }
  116. }
  117. if (!r && !(r = request_region(io_port, 1, "YMFPCI gameport"))) {
  118. printk(KERN_ERR "ymfpci: joystick port %#x is in use.\n", io_port);
  119. return -EBUSY;
  120. }
  121. chip->gameport = gp = gameport_allocate_port();
  122. if (!gp) {
  123. printk(KERN_ERR "ymfpci: cannot allocate memory for gameport\n");
  124. release_and_free_resource(r);
  125. return -ENOMEM;
  126. }
  127. gameport_set_name(gp, "Yamaha YMF Gameport");
  128. gameport_set_phys(gp, "pci%s/gameport0", pci_name(chip->pci));
  129. gameport_set_dev_parent(gp, &chip->pci->dev);
  130. gp->io = io_port;
  131. gameport_set_port_data(gp, r);
  132. if (chip->pci->device >= 0x0010) /* YMF 744/754 */
  133. pci_write_config_word(chip->pci, PCIR_DSXG_JOYBASE, io_port);
  134. pci_write_config_word(chip->pci, PCIR_DSXG_LEGACY, legacy_ctrl | YMFPCI_LEGACY_JPEN);
  135. pci_write_config_word(chip->pci, PCIR_DSXG_ELEGACY, legacy_ctrl2);
  136. gameport_register_port(chip->gameport);
  137. return 0;
  138. }
  139. void snd_ymfpci_free_gameport(struct snd_ymfpci *chip)
  140. {
  141. if (chip->gameport) {
  142. struct resource *r = gameport_get_port_data(chip->gameport);
  143. gameport_unregister_port(chip->gameport);
  144. chip->gameport = NULL;
  145. release_and_free_resource(r);
  146. }
  147. }
  148. #else
  149. static inline int snd_ymfpci_create_gameport(struct snd_ymfpci *chip, int dev, int l, int l2) { return -ENOSYS; }
  150. void snd_ymfpci_free_gameport(struct snd_ymfpci *chip) { }
  151. #endif /* SUPPORT_JOYSTICK */
  152. static int __devinit snd_card_ymfpci_probe(struct pci_dev *pci,
  153. const struct pci_device_id *pci_id)
  154. {
  155. static int dev;
  156. struct snd_card *card;
  157. struct resource *fm_res = NULL;
  158. struct resource *mpu_res = NULL;
  159. struct snd_ymfpci *chip;
  160. struct snd_opl3 *opl3;
  161. const char *str, *model;
  162. int err;
  163. u16 legacy_ctrl, legacy_ctrl2, old_legacy_ctrl;
  164. if (dev >= SNDRV_CARDS)
  165. return -ENODEV;
  166. if (!enable[dev]) {
  167. dev++;
  168. return -ENOENT;
  169. }
  170. card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
  171. if (card == NULL)
  172. return -ENOMEM;
  173. switch (pci_id->device) {
  174. case 0x0004: str = "YMF724"; model = "DS-1"; break;
  175. case 0x000d: str = "YMF724F"; model = "DS-1"; break;
  176. case 0x000a: str = "YMF740"; model = "DS-1L"; break;
  177. case 0x000c: str = "YMF740C"; model = "DS-1L"; break;
  178. case 0x0010: str = "YMF744"; model = "DS-1S"; break;
  179. case 0x0012: str = "YMF754"; model = "DS-1E"; break;
  180. default: model = str = "???"; break;
  181. }
  182. legacy_ctrl = 0;
  183. legacy_ctrl2 = 0x0800; /* SBEN = 0, SMOD = 01, LAD = 0 */
  184. if (pci_id->device >= 0x0010) { /* YMF 744/754 */
  185. if (fm_port[dev] == 1) {
  186. /* auto-detect */
  187. fm_port[dev] = pci_resource_start(pci, 1);
  188. }
  189. if (fm_port[dev] > 0 &&
  190. (fm_res = request_region(fm_port[dev], 4, "YMFPCI OPL3")) != NULL) {
  191. legacy_ctrl |= YMFPCI_LEGACY_FMEN;
  192. pci_write_config_word(pci, PCIR_DSXG_FMBASE, fm_port[dev]);
  193. }
  194. if (mpu_port[dev] == 1) {
  195. /* auto-detect */
  196. mpu_port[dev] = pci_resource_start(pci, 1) + 0x20;
  197. }
  198. if (mpu_port[dev] > 0 &&
  199. (mpu_res = request_region(mpu_port[dev], 2, "YMFPCI MPU401")) != NULL) {
  200. legacy_ctrl |= YMFPCI_LEGACY_MEN;
  201. pci_write_config_word(pci, PCIR_DSXG_MPU401BASE, mpu_port[dev]);
  202. }
  203. } else {
  204. switch (fm_port[dev]) {
  205. case 0x388: legacy_ctrl2 |= 0; break;
  206. case 0x398: legacy_ctrl2 |= 1; break;
  207. case 0x3a0: legacy_ctrl2 |= 2; break;
  208. case 0x3a8: legacy_ctrl2 |= 3; break;
  209. default: fm_port[dev] = 0; break;
  210. }
  211. if (fm_port[dev] > 0 &&
  212. (fm_res = request_region(fm_port[dev], 4, "YMFPCI OPL3")) != NULL) {
  213. legacy_ctrl |= YMFPCI_LEGACY_FMEN;
  214. } else {
  215. legacy_ctrl2 &= ~YMFPCI_LEGACY2_FMIO;
  216. fm_port[dev] = 0;
  217. }
  218. switch (mpu_port[dev]) {
  219. case 0x330: legacy_ctrl2 |= 0 << 4; break;
  220. case 0x300: legacy_ctrl2 |= 1 << 4; break;
  221. case 0x332: legacy_ctrl2 |= 2 << 4; break;
  222. case 0x334: legacy_ctrl2 |= 3 << 4; break;
  223. default: mpu_port[dev] = 0; break;
  224. }
  225. if (mpu_port[dev] > 0 &&
  226. (mpu_res = request_region(mpu_port[dev], 2, "YMFPCI MPU401")) != NULL) {
  227. legacy_ctrl |= YMFPCI_LEGACY_MEN;
  228. } else {
  229. legacy_ctrl2 &= ~YMFPCI_LEGACY2_MPUIO;
  230. mpu_port[dev] = 0;
  231. }
  232. }
  233. if (mpu_res) {
  234. legacy_ctrl |= YMFPCI_LEGACY_MIEN;
  235. legacy_ctrl2 |= YMFPCI_LEGACY2_IMOD;
  236. }
  237. pci_read_config_word(pci, PCIR_DSXG_LEGACY, &old_legacy_ctrl);
  238. pci_write_config_word(pci, PCIR_DSXG_LEGACY, legacy_ctrl);
  239. pci_write_config_word(pci, PCIR_DSXG_ELEGACY, legacy_ctrl2);
  240. if ((err = snd_ymfpci_create(card, pci,
  241. old_legacy_ctrl,
  242. &chip)) < 0) {
  243. snd_card_free(card);
  244. release_and_free_resource(mpu_res);
  245. release_and_free_resource(fm_res);
  246. return err;
  247. }
  248. chip->fm_res = fm_res;
  249. chip->mpu_res = mpu_res;
  250. card->private_data = chip;
  251. strcpy(card->driver, str);
  252. sprintf(card->shortname, "Yamaha %s (%s)", model, str);
  253. sprintf(card->longname, "%s at 0x%lx, irq %i",
  254. card->shortname,
  255. chip->reg_area_phys,
  256. chip->irq);
  257. if ((err = snd_ymfpci_pcm(chip, 0, NULL)) < 0) {
  258. snd_card_free(card);
  259. return err;
  260. }
  261. if ((err = snd_ymfpci_pcm_spdif(chip, 1, NULL)) < 0) {
  262. snd_card_free(card);
  263. return err;
  264. }
  265. if ((err = snd_ymfpci_pcm_4ch(chip, 2, NULL)) < 0) {
  266. snd_card_free(card);
  267. return err;
  268. }
  269. if ((err = snd_ymfpci_pcm2(chip, 3, NULL)) < 0) {
  270. snd_card_free(card);
  271. return err;
  272. }
  273. if ((err = snd_ymfpci_mixer(chip, rear_switch[dev], rear_swap[dev])) < 0) {
  274. snd_card_free(card);
  275. return err;
  276. }
  277. if ((err = snd_ymfpci_timer(chip, 0)) < 0) {
  278. snd_card_free(card);
  279. return err;
  280. }
  281. if (chip->mpu_res) {
  282. if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_YMFPCI,
  283. mpu_port[dev],
  284. MPU401_INFO_INTEGRATED,
  285. pci->irq, 0, &chip->rawmidi)) < 0) {
  286. printk(KERN_WARNING "ymfpci: cannot initialize MPU401 at 0x%lx, skipping...\n", mpu_port[dev]);
  287. legacy_ctrl &= ~YMFPCI_LEGACY_MIEN; /* disable MPU401 irq */
  288. pci_write_config_word(pci, PCIR_DSXG_LEGACY, legacy_ctrl);
  289. }
  290. }
  291. if (chip->fm_res) {
  292. if ((err = snd_opl3_create(card,
  293. fm_port[dev],
  294. fm_port[dev] + 2,
  295. OPL3_HW_OPL3, 1, &opl3)) < 0) {
  296. printk(KERN_WARNING "ymfpci: cannot initialize FM OPL3 at 0x%lx, skipping...\n", fm_port[dev]);
  297. legacy_ctrl &= ~YMFPCI_LEGACY_FMEN;
  298. pci_write_config_word(pci, PCIR_DSXG_LEGACY, legacy_ctrl);
  299. } else if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
  300. snd_card_free(card);
  301. snd_printk(KERN_ERR "cannot create opl3 hwdep\n");
  302. return err;
  303. }
  304. }
  305. snd_ymfpci_create_gameport(chip, dev, legacy_ctrl, legacy_ctrl2);
  306. if ((err = snd_card_register(card)) < 0) {
  307. snd_card_free(card);
  308. return err;
  309. }
  310. pci_set_drvdata(pci, card);
  311. dev++;
  312. return 0;
  313. }
  314. static void __devexit snd_card_ymfpci_remove(struct pci_dev *pci)
  315. {
  316. snd_card_free(pci_get_drvdata(pci));
  317. pci_set_drvdata(pci, NULL);
  318. }
  319. static struct pci_driver driver = {
  320. .name = "Yamaha DS-1 PCI",
  321. .id_table = snd_ymfpci_ids,
  322. .probe = snd_card_ymfpci_probe,
  323. .remove = __devexit_p(snd_card_ymfpci_remove),
  324. #ifdef CONFIG_PM
  325. .suspend = snd_ymfpci_suspend,
  326. .resume = snd_ymfpci_resume,
  327. #endif
  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)