source: GPL/trunk/alsa-kernel/pci/ymfpci/ymfpci.c

Last change on this file was 772, checked in by David Azarewicz, 7 months ago

Merge in changes from 6.6-LTS branch.
Fixed additional 25+ problems.

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