ymfpci.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 "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_hw_array(mpu_port, long, ioport, NULL, 0444);
  55. MODULE_PARM_DESC(mpu_port, "MPU-401 Port.");
  56. module_param_hw_array(fm_port, long, ioport, NULL, 0444);
  57. MODULE_PARM_DESC(fm_port, "FM OPL-3 Port.");
  58. #ifdef SUPPORT_JOYSTICK
  59. module_param_hw_array(joystick_port, long, ioport, 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 const struct pci_device_id 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 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. dev_err(chip->card->dev,
  100. "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. dev_err(chip->card->dev,
  111. "invalid joystick port %#x", io_port);
  112. return -EINVAL;
  113. }
  114. }
  115. if (!r && !(r = request_region(io_port, 1, "YMFPCI gameport"))) {
  116. dev_err(chip->card->dev,
  117. "joystick port %#x is in use.\n", io_port);
  118. return -EBUSY;
  119. }
  120. chip->gameport = gp = gameport_allocate_port();
  121. if (!gp) {
  122. dev_err(chip->card->dev,
  123. "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 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. err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
  171. 0, &card);
  172. if (err < 0)
  173. return err;
  174. switch (pci_id->device) {
  175. case 0x0004: str = "YMF724"; model = "DS-1"; break;
  176. case 0x000d: str = "YMF724F"; model = "DS-1"; break;
  177. case 0x000a: str = "YMF740"; model = "DS-1L"; break;
  178. case 0x000c: str = "YMF740C"; model = "DS-1L"; break;
  179. case 0x0010: str = "YMF744"; model = "DS-1S"; break;
  180. case 0x0012: str = "YMF754"; model = "DS-1E"; break;
  181. default: model = str = "???"; break;
  182. }
  183. legacy_ctrl = 0;
  184. legacy_ctrl2 = 0x0800; /* SBEN = 0, SMOD = 01, LAD = 0 */
  185. if (pci_id->device >= 0x0010) { /* YMF 744/754 */
  186. if (fm_port[dev] == 1) {
  187. /* auto-detect */
  188. fm_port[dev] = pci_resource_start(pci, 1);
  189. }
  190. if (fm_port[dev] > 0 &&
  191. (fm_res = request_region(fm_port[dev], 4, "YMFPCI OPL3")) != NULL) {
  192. legacy_ctrl |= YMFPCI_LEGACY_FMEN;
  193. pci_write_config_word(pci, PCIR_DSXG_FMBASE, fm_port[dev]);
  194. }
  195. if (mpu_port[dev] == 1) {
  196. /* auto-detect */
  197. mpu_port[dev] = pci_resource_start(pci, 1) + 0x20;
  198. }
  199. if (mpu_port[dev] > 0 &&
  200. (mpu_res = request_region(mpu_port[dev], 2, "YMFPCI MPU401")) != NULL) {
  201. legacy_ctrl |= YMFPCI_LEGACY_MEN;
  202. pci_write_config_word(pci, PCIR_DSXG_MPU401BASE, mpu_port[dev]);
  203. }
  204. } else {
  205. switch (fm_port[dev]) {
  206. case 0x388: legacy_ctrl2 |= 0; break;
  207. case 0x398: legacy_ctrl2 |= 1; break;
  208. case 0x3a0: legacy_ctrl2 |= 2; break;
  209. case 0x3a8: legacy_ctrl2 |= 3; break;
  210. default: fm_port[dev] = 0; break;
  211. }
  212. if (fm_port[dev] > 0 &&
  213. (fm_res = request_region(fm_port[dev], 4, "YMFPCI OPL3")) != NULL) {
  214. legacy_ctrl |= YMFPCI_LEGACY_FMEN;
  215. } else {
  216. legacy_ctrl2 &= ~YMFPCI_LEGACY2_FMIO;
  217. fm_port[dev] = 0;
  218. }
  219. switch (mpu_port[dev]) {
  220. case 0x330: legacy_ctrl2 |= 0 << 4; break;
  221. case 0x300: legacy_ctrl2 |= 1 << 4; break;
  222. case 0x332: legacy_ctrl2 |= 2 << 4; break;
  223. case 0x334: legacy_ctrl2 |= 3 << 4; break;
  224. default: mpu_port[dev] = 0; break;
  225. }
  226. if (mpu_port[dev] > 0 &&
  227. (mpu_res = request_region(mpu_port[dev], 2, "YMFPCI MPU401")) != NULL) {
  228. legacy_ctrl |= YMFPCI_LEGACY_MEN;
  229. } else {
  230. legacy_ctrl2 &= ~YMFPCI_LEGACY2_MPUIO;
  231. mpu_port[dev] = 0;
  232. }
  233. }
  234. if (mpu_res) {
  235. legacy_ctrl |= YMFPCI_LEGACY_MIEN;
  236. legacy_ctrl2 |= YMFPCI_LEGACY2_IMOD;
  237. }
  238. pci_read_config_word(pci, PCIR_DSXG_LEGACY, &old_legacy_ctrl);
  239. pci_write_config_word(pci, PCIR_DSXG_LEGACY, legacy_ctrl);
  240. pci_write_config_word(pci, PCIR_DSXG_ELEGACY, legacy_ctrl2);
  241. if ((err = snd_ymfpci_create(card, pci,
  242. old_legacy_ctrl,
  243. &chip)) < 0) {
  244. release_and_free_resource(mpu_res);
  245. release_and_free_resource(fm_res);
  246. goto free_card;
  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. err = snd_ymfpci_pcm(chip, 0);
  258. if (err < 0)
  259. goto free_card;
  260. err = snd_ymfpci_pcm_spdif(chip, 1);
  261. if (err < 0)
  262. goto free_card;
  263. err = snd_ymfpci_mixer(chip, rear_switch[dev]);
  264. if (err < 0)
  265. goto free_card;
  266. if (chip->ac97->ext_id & AC97_EI_SDAC) {
  267. err = snd_ymfpci_pcm_4ch(chip, 2);
  268. if (err < 0)
  269. goto free_card;
  270. err = snd_ymfpci_pcm2(chip, 3);
  271. if (err < 0)
  272. goto free_card;
  273. }
  274. err = snd_ymfpci_timer(chip, 0);
  275. if (err < 0)
  276. goto free_card;
  277. if (chip->mpu_res) {
  278. if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_YMFPCI,
  279. mpu_port[dev],
  280. MPU401_INFO_INTEGRATED |
  281. MPU401_INFO_IRQ_HOOK,
  282. -1, &chip->rawmidi)) < 0) {
  283. dev_warn(card->dev,
  284. "cannot initialize MPU401 at 0x%lx, skipping...\n",
  285. mpu_port[dev]);
  286. legacy_ctrl &= ~YMFPCI_LEGACY_MIEN; /* disable MPU401 irq */
  287. pci_write_config_word(pci, PCIR_DSXG_LEGACY, legacy_ctrl);
  288. }
  289. }
  290. if (chip->fm_res) {
  291. if ((err = snd_opl3_create(card,
  292. fm_port[dev],
  293. fm_port[dev] + 2,
  294. OPL3_HW_OPL3, 1, &opl3)) < 0) {
  295. dev_warn(card->dev,
  296. "cannot initialize FM OPL3 at 0x%lx, skipping...\n",
  297. fm_port[dev]);
  298. legacy_ctrl &= ~YMFPCI_LEGACY_FMEN;
  299. pci_write_config_word(pci, PCIR_DSXG_LEGACY, legacy_ctrl);
  300. } else if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
  301. dev_err(card->dev, "cannot create opl3 hwdep\n");
  302. goto free_card;
  303. }
  304. }
  305. snd_ymfpci_create_gameport(chip, dev, legacy_ctrl, legacy_ctrl2);
  306. err = snd_card_register(card);
  307. if (err < 0)
  308. goto free_card;
  309. pci_set_drvdata(pci, card);
  310. dev++;
  311. return 0;
  312. free_card:
  313. snd_card_free(card);
  314. return err;
  315. }
  316. static void snd_card_ymfpci_remove(struct pci_dev *pci)
  317. {
  318. snd_card_free(pci_get_drvdata(pci));
  319. }
  320. static struct pci_driver ymfpci_driver = {
  321. .name = KBUILD_MODNAME,
  322. .id_table = snd_ymfpci_ids,
  323. .probe = snd_card_ymfpci_probe,
  324. .remove = snd_card_ymfpci_remove,
  325. #ifdef CONFIG_PM_SLEEP
  326. .driver = {
  327. .pm = &snd_ymfpci_pm,
  328. },
  329. #endif
  330. };
  331. module_pci_driver(ymfpci_driver);