xref: /linux/sound/pcmcia/pdaudiocf/pdaudiocf.c (revision 48dea9a700c8728cc31a1dd44588b97578de86ee)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Driver for Sound Core PDAudioCF soundcard
4  *
5  * Copyright (c) 2003 by Jaroslav Kysela <perex@perex.cz>
6  */
7 
8 #include <sound/core.h>
9 #include <linux/slab.h>
10 #include <linux/module.h>
11 #include <pcmcia/ciscode.h>
12 #include <pcmcia/cisreg.h>
13 #include "pdaudiocf.h"
14 #include <sound/initval.h>
15 #include <linux/init.h>
16 
17 /*
18  */
19 
20 #define CARD_NAME	"PDAudio-CF"
21 
22 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
23 MODULE_DESCRIPTION("Sound Core " CARD_NAME);
24 MODULE_LICENSE("GPL");
25 MODULE_SUPPORTED_DEVICE("{{Sound Core," CARD_NAME "}}");
26 
27 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
28 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
29 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;	/* Enable switches */
30 
31 module_param_array(index, int, NULL, 0444);
32 MODULE_PARM_DESC(index, "Index value for " CARD_NAME " soundcard.");
33 module_param_array(id, charp, NULL, 0444);
34 MODULE_PARM_DESC(id, "ID string for " CARD_NAME " soundcard.");
35 module_param_array(enable, bool, NULL, 0444);
36 MODULE_PARM_DESC(enable, "Enable " CARD_NAME " soundcard.");
37 
38 /*
39  */
40 
41 static struct snd_card *card_list[SNDRV_CARDS];
42 
43 /*
44  * prototypes
45  */
46 static int pdacf_config(struct pcmcia_device *link);
47 static void snd_pdacf_detach(struct pcmcia_device *p_dev);
48 
49 static void pdacf_release(struct pcmcia_device *link)
50 {
51 	free_irq(link->irq, link->priv);
52 	pcmcia_disable_device(link);
53 }
54 
55 /*
56  * destructor
57  */
58 static int snd_pdacf_free(struct snd_pdacf *pdacf)
59 {
60 	struct pcmcia_device *link = pdacf->p_dev;
61 
62 	pdacf_release(link);
63 
64 	card_list[pdacf->index] = NULL;
65 	pdacf->card = NULL;
66 
67 	kfree(pdacf);
68 	return 0;
69 }
70 
71 static int snd_pdacf_dev_free(struct snd_device *device)
72 {
73 	struct snd_pdacf *chip = device->device_data;
74 	return snd_pdacf_free(chip);
75 }
76 
77 /*
78  * snd_pdacf_attach - attach callback for cs
79  */
80 static int snd_pdacf_probe(struct pcmcia_device *link)
81 {
82 	int i, err;
83 	struct snd_pdacf *pdacf;
84 	struct snd_card *card;
85 	static const struct snd_device_ops ops = {
86 		.dev_free =	snd_pdacf_dev_free,
87 	};
88 
89 	snd_printdd(KERN_DEBUG "pdacf_attach called\n");
90 	/* find an empty slot from the card list */
91 	for (i = 0; i < SNDRV_CARDS; i++) {
92 		if (! card_list[i])
93 			break;
94 	}
95 	if (i >= SNDRV_CARDS) {
96 		snd_printk(KERN_ERR "pdacf: too many cards found\n");
97 		return -EINVAL;
98 	}
99 	if (! enable[i])
100 		return -ENODEV; /* disabled explicitly */
101 
102 	/* ok, create a card instance */
103 	err = snd_card_new(&link->dev, index[i], id[i], THIS_MODULE,
104 			   0, &card);
105 	if (err < 0) {
106 		snd_printk(KERN_ERR "pdacf: cannot create a card instance\n");
107 		return err;
108 	}
109 
110 	pdacf = snd_pdacf_create(card);
111 	if (!pdacf) {
112 		snd_card_free(card);
113 		return -ENOMEM;
114 	}
115 
116 	err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, pdacf, &ops);
117 	if (err < 0) {
118 		kfree(pdacf);
119 		snd_card_free(card);
120 		return err;
121 	}
122 
123 	pdacf->index = i;
124 	card_list[i] = card;
125 
126 	pdacf->p_dev = link;
127 	link->priv = pdacf;
128 
129 	link->resource[0]->flags |= IO_DATA_PATH_WIDTH_AUTO;
130 	link->resource[0]->end = 16;
131 
132 	link->config_flags = CONF_ENABLE_IRQ | CONF_ENABLE_PULSE_IRQ;
133 	link->config_index = 1;
134 	link->config_regs = PRESENT_OPTION;
135 
136 	return pdacf_config(link);
137 }
138 
139 
140 /**
141  * snd_pdacf_assign_resources - initialize the hardware and card instance.
142  * @pdacf: context
143  * @port: i/o port for the card
144  * @irq: irq number for the card
145  *
146  * this function assigns the specified port and irq, boot the card,
147  * create pcm and control instances, and initialize the rest hardware.
148  *
149  * returns 0 if successful, or a negative error code.
150  */
151 static int snd_pdacf_assign_resources(struct snd_pdacf *pdacf, int port, int irq)
152 {
153 	int err;
154 	struct snd_card *card = pdacf->card;
155 
156 	snd_printdd(KERN_DEBUG "pdacf assign resources: port = 0x%x, irq = %d\n", port, irq);
157 	pdacf->port = port;
158 	pdacf->irq = irq;
159 	pdacf->chip_status |= PDAUDIOCF_STAT_IS_CONFIGURED;
160 
161 	err = snd_pdacf_ak4117_create(pdacf);
162 	if (err < 0)
163 		return err;
164 
165 	strcpy(card->driver, "PDAudio-CF");
166 	sprintf(card->shortname, "Core Sound %s", card->driver);
167 	sprintf(card->longname, "%s at 0x%x, irq %i",
168 		card->shortname, port, irq);
169 
170 	err = snd_pdacf_pcm_new(pdacf);
171 	if (err < 0)
172 		return err;
173 
174 	if ((err = snd_card_register(card)) < 0)
175 		return err;
176 
177 	return 0;
178 }
179 
180 
181 /*
182  * snd_pdacf_detach - detach callback for cs
183  */
184 static void snd_pdacf_detach(struct pcmcia_device *link)
185 {
186 	struct snd_pdacf *chip = link->priv;
187 
188 	snd_printdd(KERN_DEBUG "pdacf_detach called\n");
189 
190 	if (chip->chip_status & PDAUDIOCF_STAT_IS_CONFIGURED)
191 		snd_pdacf_powerdown(chip);
192 	chip->chip_status |= PDAUDIOCF_STAT_IS_STALE; /* to be sure */
193 	snd_card_disconnect(chip->card);
194 	snd_card_free_when_closed(chip->card);
195 }
196 
197 /*
198  * configuration callback
199  */
200 
201 static int pdacf_config(struct pcmcia_device *link)
202 {
203 	struct snd_pdacf *pdacf = link->priv;
204 	int ret;
205 
206 	snd_printdd(KERN_DEBUG "pdacf_config called\n");
207 	link->config_index = 0x5;
208 	link->config_flags |= CONF_ENABLE_IRQ | CONF_ENABLE_PULSE_IRQ;
209 
210 	ret = pcmcia_request_io(link);
211 	if (ret)
212 		goto failed_preirq;
213 
214 	ret = request_threaded_irq(link->irq, pdacf_interrupt,
215 				   pdacf_threaded_irq,
216 				   IRQF_SHARED, link->devname, link->priv);
217 	if (ret)
218 		goto failed_preirq;
219 
220 	ret = pcmcia_enable_device(link);
221 	if (ret)
222 		goto failed;
223 
224 	if (snd_pdacf_assign_resources(pdacf, link->resource[0]->start,
225 					link->irq) < 0)
226 		goto failed;
227 
228 	pdacf->card->sync_irq = link->irq;
229 	return 0;
230 
231  failed:
232 	free_irq(link->irq, link->priv);
233 failed_preirq:
234 	pcmcia_disable_device(link);
235 	return -ENODEV;
236 }
237 
238 #ifdef CONFIG_PM
239 
240 static int pdacf_suspend(struct pcmcia_device *link)
241 {
242 	struct snd_pdacf *chip = link->priv;
243 
244 	snd_printdd(KERN_DEBUG "SUSPEND\n");
245 	if (chip) {
246 		snd_printdd(KERN_DEBUG "snd_pdacf_suspend calling\n");
247 		snd_pdacf_suspend(chip);
248 	}
249 
250 	return 0;
251 }
252 
253 static int pdacf_resume(struct pcmcia_device *link)
254 {
255 	struct snd_pdacf *chip = link->priv;
256 
257 	snd_printdd(KERN_DEBUG "RESUME\n");
258 	if (pcmcia_dev_present(link)) {
259 		if (chip) {
260 			snd_printdd(KERN_DEBUG "calling snd_pdacf_resume\n");
261 			snd_pdacf_resume(chip);
262 		}
263 	}
264 	snd_printdd(KERN_DEBUG "resume done!\n");
265 
266 	return 0;
267 }
268 
269 #endif
270 
271 /*
272  * Module entry points
273  */
274 static const struct pcmcia_device_id snd_pdacf_ids[] = {
275 	/* this is too general PCMCIA_DEVICE_MANF_CARD(0x015d, 0x4c45), */
276 	PCMCIA_DEVICE_PROD_ID12("Core Sound","PDAudio-CF",0x396d19d2,0x71717b49),
277 	PCMCIA_DEVICE_NULL
278 };
279 MODULE_DEVICE_TABLE(pcmcia, snd_pdacf_ids);
280 
281 static struct pcmcia_driver pdacf_cs_driver = {
282 	.owner		= THIS_MODULE,
283 	.name		= "snd-pdaudiocf",
284 	.probe		= snd_pdacf_probe,
285 	.remove		= snd_pdacf_detach,
286 	.id_table	= snd_pdacf_ids,
287 #ifdef CONFIG_PM
288 	.suspend	= pdacf_suspend,
289 	.resume		= pdacf_resume,
290 #endif
291 };
292 module_pcmcia_driver(pdacf_cs_driver);
293