ymfpci.c 11 KB

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