1
2/*
3    card-als100.c - driver for Avance Logic ALS100 based soundcards.
4    Copyright (C) 1999-2000 by Massimo Piccioni <dafastidio@libero.it>
5
6    Thanks to Pierfrancesco 'qM2' Passerini.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21*/
22
23#include <sound/driver.h>
24#include <linux/init.h>
25#include <linux/wait.h>
26#include <linux/time.h>
27#include <linux/pnp.h>
28#include <linux/moduleparam.h>
29#include <sound/core.h>
30#include <sound/initval.h>
31#include <sound/mpu401.h>
32#include <sound/opl3.h>
33#include <sound/sb.h>
34
35#define PFX "als100: "
36
37MODULE_AUTHOR("Massimo Piccioni <dafastidio@libero.it>");
38MODULE_DESCRIPTION("Avance Logic ALS1X0");
39MODULE_LICENSE("GPL");
40MODULE_SUPPORTED_DEVICE("{{Avance Logic,ALS100 - PRO16PNP},"
41	        "{Avance Logic,ALS110},"
42	        "{Avance Logic,ALS120},"
43	        "{Avance Logic,ALS200},"
44	        "{3D Melody,MF1000},"
45	        "{Digimate,3D Sound},"
46	        "{Avance Logic,ALS120},"
47	        "{RTL,RTL3000}}");
48
49static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
50static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
51static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP; /* Enable this card */
52static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;	/* PnP setup */
53static long mpu_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;	/* PnP setup */
54static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;	/* PnP setup */
55static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;	/* PnP setup */
56static int mpu_irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;	/* PnP setup */
57static int dma8[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;	/* PnP setup */
58static int dma16[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;	/* PnP setup */
59
60module_param_array(index, int, NULL, 0444);
61MODULE_PARM_DESC(index, "Index value for als100 based soundcard.");
62module_param_array(id, charp, NULL, 0444);
63MODULE_PARM_DESC(id, "ID string for als100 based soundcard.");
64module_param_array(enable, bool, NULL, 0444);
65MODULE_PARM_DESC(enable, "Enable als100 based soundcard.");
66module_param_array(port, long, NULL, 0444);
67MODULE_PARM_DESC(port, "Port # for als100 driver.");
68module_param_array(mpu_port, long, NULL, 0444);
69MODULE_PARM_DESC(mpu_port, "MPU-401 port # for als100 driver.");
70module_param_array(fm_port, long, NULL, 0444);
71MODULE_PARM_DESC(fm_port, "FM port # for als100 driver.");
72module_param_array(irq, int, NULL, 0444);
73MODULE_PARM_DESC(irq, "IRQ # for als100 driver.");
74module_param_array(mpu_irq, int, NULL, 0444);
75MODULE_PARM_DESC(mpu_irq, "MPU-401 IRQ # for als100 driver.");
76module_param_array(dma8, int, NULL, 0444);
77MODULE_PARM_DESC(dma8, "8-bit DMA # for als100 driver.");
78module_param_array(dma16, int, NULL, 0444);
79MODULE_PARM_DESC(dma16, "16-bit DMA # for als100 driver.");
80
81struct snd_card_als100 {
82	int dev_no;
83	struct pnp_dev *dev;
84	struct pnp_dev *devmpu;
85	struct pnp_dev *devopl;
86	struct snd_sb *chip;
87};
88
89static struct pnp_card_device_id snd_als100_pnpids[] = {
90	/* ALS100 - PRO16PNP */
91	{ .id = "ALS0001", .devs = { { "@@@0001" }, { "@X@0001" }, { "@H@0001" } } },
92	/* ALS110 - MF1000 - Digimate 3D Sound */
93	{ .id = "ALS0110", .devs = { { "@@@1001" }, { "@X@1001" }, { "@H@1001" } } },
94	/* ALS120 */
95	{ .id = "ALS0120", .devs = { { "@@@2001" }, { "@X@2001" }, { "@H@2001" } } },
96	/* ALS200 */
97	{ .id = "ALS0200", .devs = { { "@@@0020" }, { "@X@0020" }, { "@H@0001" } } },
98	/* ALS200 OEM */
99	{ .id = "ALS0200", .devs = { { "@@@0020" }, { "@X@0020" }, { "@H@0020" } } },
100	/* RTL3000 */
101	{ .id = "RTL3000", .devs = { { "@@@2001" }, { "@X@2001" }, { "@H@2001" } } },
102	{ .id = "", } /* end */
103};
104
105MODULE_DEVICE_TABLE(pnp_card, snd_als100_pnpids);
106
107#define DRIVER_NAME	"snd-card-als100"
108
109static int __devinit snd_card_als100_pnp(int dev, struct snd_card_als100 *acard,
110					 struct pnp_card_link *card,
111					 const struct pnp_card_device_id *id)
112{
113	struct pnp_dev *pdev;
114	struct pnp_resource_table *cfg = kmalloc(sizeof(*cfg), GFP_KERNEL);
115	int err;
116
117	if (!cfg)
118		return -ENOMEM;
119	acard->dev = pnp_request_card_device(card, id->devs[0].id, NULL);
120	if (acard->dev == NULL) {
121		kfree(cfg);
122		return -ENODEV;
123	}
124	acard->devmpu = pnp_request_card_device(card, id->devs[1].id, acard->dev);
125	acard->devopl = pnp_request_card_device(card, id->devs[2].id, acard->dev);
126
127	pdev = acard->dev;
128
129	pnp_init_resource_table(cfg);
130
131	/* override resources */
132	if (port[dev] != SNDRV_AUTO_PORT)
133		pnp_resource_change(&cfg->port_resource[0], port[dev], 16);
134	if (dma8[dev] != SNDRV_AUTO_DMA)
135		pnp_resource_change(&cfg->dma_resource[0], dma8[dev], 1);
136	if (dma16[dev] != SNDRV_AUTO_DMA)
137		pnp_resource_change(&cfg->dma_resource[1], dma16[dev], 1);
138	if (irq[dev] != SNDRV_AUTO_IRQ)
139		pnp_resource_change(&cfg->irq_resource[0], irq[dev], 1);
140	if (pnp_manual_config_dev(pdev, cfg, 0) < 0)
141		snd_printk(KERN_ERR PFX "AUDIO the requested resources are invalid, using auto config\n");
142	err = pnp_activate_dev(pdev);
143	if (err < 0) {
144		snd_printk(KERN_ERR PFX "AUDIO pnp configure failure\n");
145		kfree(cfg);
146		return err;
147	}
148	port[dev] = pnp_port_start(pdev, 0);
149	dma8[dev] = pnp_dma(pdev, 1);
150	dma16[dev] = pnp_dma(pdev, 0);
151	irq[dev] = pnp_irq(pdev, 0);
152
153	pdev = acard->devmpu;
154	if (pdev != NULL) {
155		pnp_init_resource_table(cfg);
156		if (mpu_port[dev] != SNDRV_AUTO_PORT)
157			pnp_resource_change(&cfg->port_resource[0], mpu_port[dev], 2);
158		if (mpu_irq[dev] != SNDRV_AUTO_IRQ)
159			pnp_resource_change(&cfg->irq_resource[0], mpu_irq[dev], 1);
160		if ((pnp_manual_config_dev(pdev, cfg, 0)) < 0)
161			snd_printk(KERN_ERR PFX "MPU401 the requested resources are invalid, using auto config\n");
162		err = pnp_activate_dev(pdev);
163		if (err < 0)
164			goto __mpu_error;
165		mpu_port[dev] = pnp_port_start(pdev, 0);
166		mpu_irq[dev] = pnp_irq(pdev, 0);
167	} else {
168	     __mpu_error:
169	     	if (pdev) {
170		     	pnp_release_card_device(pdev);
171	     		snd_printk(KERN_ERR PFX "MPU401 pnp configure failure, skipping\n");
172	     	}
173	     	acard->devmpu = NULL;
174	     	mpu_port[dev] = -1;
175	}
176
177	pdev = acard->devopl;
178	if (pdev != NULL) {
179		pnp_init_resource_table(cfg);
180		if (fm_port[dev] != SNDRV_AUTO_PORT)
181			pnp_resource_change(&cfg->port_resource[0], fm_port[dev], 4);
182		if ((pnp_manual_config_dev(pdev, cfg, 0)) < 0)
183			snd_printk(KERN_ERR PFX "OPL3 the requested resources are invalid, using auto config\n");
184		err = pnp_activate_dev(pdev);
185		if (err < 0)
186			goto __fm_error;
187		fm_port[dev] = pnp_port_start(pdev, 0);
188	} else {
189	      __fm_error:
190	     	if (pdev) {
191		     	pnp_release_card_device(pdev);
192	     		snd_printk(KERN_ERR PFX "OPL3 pnp configure failure, skipping\n");
193	     	}
194	     	acard->devopl = NULL;
195	     	fm_port[dev] = -1;
196	}
197
198	kfree(cfg);
199	return 0;
200}
201
202static int __devinit snd_card_als100_probe(int dev,
203					struct pnp_card_link *pcard,
204					const struct pnp_card_device_id *pid)
205{
206	int error;
207	struct snd_sb *chip;
208	struct snd_card *card;
209	struct snd_card_als100 *acard;
210	struct snd_opl3 *opl3;
211
212	if ((card = snd_card_new(index[dev], id[dev], THIS_MODULE,
213				 sizeof(struct snd_card_als100))) == NULL)
214		return -ENOMEM;
215	acard = card->private_data;
216
217	if ((error = snd_card_als100_pnp(dev, acard, pcard, pid))) {
218		snd_card_free(card);
219		return error;
220	}
221	snd_card_set_dev(card, &pcard->card->dev);
222
223	if ((error = snd_sbdsp_create(card, port[dev],
224				      irq[dev],
225				      snd_sb16dsp_interrupt,
226				      dma8[dev],
227				      dma16[dev],
228				      SB_HW_ALS100, &chip)) < 0) {
229		snd_card_free(card);
230		return error;
231	}
232	acard->chip = chip;
233
234	strcpy(card->driver, "ALS100");
235	strcpy(card->shortname, "Avance Logic ALS100");
236	sprintf(card->longname, "%s, %s at 0x%lx, irq %d, dma %d&%d",
237		card->shortname, chip->name, chip->port,
238		irq[dev], dma8[dev], dma16[dev]);
239
240	if ((error = snd_sb16dsp_pcm(chip, 0, NULL)) < 0) {
241		snd_card_free(card);
242		return error;
243	}
244
245	if ((error = snd_sbmixer_new(chip)) < 0) {
246		snd_card_free(card);
247		return error;
248	}
249
250	if (mpu_port[dev] > 0 && mpu_port[dev] != SNDRV_AUTO_PORT) {
251		if (snd_mpu401_uart_new(card, 0, MPU401_HW_ALS100,
252					mpu_port[dev], 0,
253					mpu_irq[dev], IRQF_DISABLED,
254					NULL) < 0)
255			snd_printk(KERN_ERR PFX "no MPU-401 device at 0x%lx\n", mpu_port[dev]);
256	}
257
258	if (fm_port[dev] > 0 && fm_port[dev] != SNDRV_AUTO_PORT) {
259		if (snd_opl3_create(card,
260				    fm_port[dev], fm_port[dev] + 2,
261				    OPL3_HW_AUTO, 0, &opl3) < 0) {
262			snd_printk(KERN_ERR PFX "no OPL device at 0x%lx-0x%lx\n",
263				   fm_port[dev], fm_port[dev] + 2);
264		} else {
265			if ((error = snd_opl3_timer_new(opl3, 0, 1)) < 0) {
266				snd_card_free(card);
267				return error;
268			}
269			if ((error = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
270				snd_card_free(card);
271				return error;
272			}
273		}
274	}
275
276	if ((error = snd_card_register(card)) < 0) {
277		snd_card_free(card);
278		return error;
279	}
280	pnp_set_card_drvdata(pcard, card);
281	return 0;
282}
283
284static unsigned int __devinitdata als100_devices;
285
286static int __devinit snd_als100_pnp_detect(struct pnp_card_link *card,
287					   const struct pnp_card_device_id *id)
288{
289	static int dev;
290	int res;
291
292	for ( ; dev < SNDRV_CARDS; dev++) {
293		if (!enable[dev])
294			continue;
295		res = snd_card_als100_probe(dev, card, id);
296		if (res < 0)
297			return res;
298		dev++;
299		als100_devices++;
300		return 0;
301	}
302	return -ENODEV;
303}
304
305static void __devexit snd_als100_pnp_remove(struct pnp_card_link * pcard)
306{
307	snd_card_free(pnp_get_card_drvdata(pcard));
308	pnp_set_card_drvdata(pcard, NULL);
309}
310
311#ifdef CONFIG_PM
312static int snd_als100_pnp_suspend(struct pnp_card_link *pcard, pm_message_t state)
313{
314	struct snd_card *card = pnp_get_card_drvdata(pcard);
315	struct snd_card_als100 *acard = card->private_data;
316	struct snd_sb *chip = acard->chip;
317
318	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
319	snd_pcm_suspend_all(chip->pcm);
320	snd_sbmixer_suspend(chip);
321	return 0;
322}
323
324static int snd_als100_pnp_resume(struct pnp_card_link *pcard)
325{
326	struct snd_card *card = pnp_get_card_drvdata(pcard);
327	struct snd_card_als100 *acard = card->private_data;
328	struct snd_sb *chip = acard->chip;
329
330	snd_sbdsp_reset(chip);
331	snd_sbmixer_resume(chip);
332	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
333	return 0;
334}
335#endif
336
337static struct pnp_card_driver als100_pnpc_driver = {
338	.flags          = PNP_DRIVER_RES_DISABLE,
339        .name           = "als100",
340        .id_table       = snd_als100_pnpids,
341        .probe          = snd_als100_pnp_detect,
342        .remove         = __devexit_p(snd_als100_pnp_remove),
343#ifdef CONFIG_PM
344	.suspend	= snd_als100_pnp_suspend,
345	.resume		= snd_als100_pnp_resume,
346#endif
347};
348
349static int __init alsa_card_als100_init(void)
350{
351	int err;
352
353	err = pnp_register_card_driver(&als100_pnpc_driver);
354	if (err)
355		return err;
356
357	if (!als100_devices) {
358		pnp_unregister_card_driver(&als100_pnpc_driver);
359#ifdef MODULE
360		snd_printk(KERN_ERR "no ALS100 based soundcards found\n");
361#endif
362		return -ENODEV;
363	}
364	return 0;
365}
366
367static void __exit alsa_card_als100_exit(void)
368{
369	pnp_unregister_card_driver(&als100_pnpc_driver);
370}
371
372module_init(alsa_card_als100_init)
373module_exit(alsa_card_als100_exit)
374