1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Driver for Digigram VXpocket V2/440 soundcards
4  *
5  * Copyright (c) 2002 by Takashi Iwai <tiwai@suse.de>
6 
7  */
8 
9 
10 #include <linux/init.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <sound/core.h>
14 #include "vxpocket.h"
15 #include <pcmcia/ciscode.h>
16 #include <pcmcia/cisreg.h>
17 #include <sound/initval.h>
18 #include <sound/tlv.h>
19 
20 MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
21 MODULE_DESCRIPTION("Digigram VXPocket");
22 MODULE_LICENSE("GPL");
23 
24 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
25 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
26 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;	/* Enable switches */
27 static int ibl[SNDRV_CARDS];
28 
29 module_param_array(index, int, NULL, 0444);
30 MODULE_PARM_DESC(index, "Index value for VXPocket soundcard.");
31 module_param_array(id, charp, NULL, 0444);
32 MODULE_PARM_DESC(id, "ID string for VXPocket soundcard.");
33 module_param_array(enable, bool, NULL, 0444);
34 MODULE_PARM_DESC(enable, "Enable VXPocket soundcard.");
35 module_param_array(ibl, int, NULL, 0444);
36 MODULE_PARM_DESC(ibl, "Capture IBL size for VXPocket soundcard.");
37 
38 
39 /*
40  */
41 
42 static unsigned int card_alloc;
43 
44 
45 /*
46  */
vxpocket_release(struct pcmcia_device * link)47 static void vxpocket_release(struct pcmcia_device *link)
48 {
49 	free_irq(link->irq, link->priv);
50 	pcmcia_disable_device(link);
51 }
52 
53 /*
54  * destructor, called from snd_card_free_when_closed()
55  */
snd_vxpocket_dev_free(struct snd_device * device)56 static int snd_vxpocket_dev_free(struct snd_device *device)
57 {
58 	struct vx_core *chip = device->device_data;
59 
60 	snd_vx_free_firmware(chip);
61 	kfree(chip);
62 	return 0;
63 }
64 
65 
66 /*
67  * Hardware information
68  */
69 
70 /* VX-pocket V2
71  *
72  * 1 DSP, 1 sync UER
73  * 1 programmable clock (NIY)
74  * 1 stereo analog input (line/micro)
75  * 1 stereo analog output
76  * Only output levels can be modified
77  */
78 
79 static const DECLARE_TLV_DB_SCALE(db_scale_old_vol, -11350, 50, 0);
80 
81 static const struct snd_vx_hardware vxpocket_hw = {
82 	.name = "VXPocket",
83 	.type = VX_TYPE_VXPOCKET,
84 
85 	/* hardware specs */
86 	.num_codecs = 1,
87 	.num_ins = 1,
88 	.num_outs = 1,
89 	.output_level_max = VX_ANALOG_OUT_LEVEL_MAX,
90 	.output_level_db_scale = db_scale_old_vol,
91 };
92 
93 /* VX-pocket 440
94  *
95  * 1 DSP, 1 sync UER, 1 sync World Clock (NIY)
96  * SMPTE (NIY)
97  * 2 stereo analog input (line/micro)
98  * 2 stereo analog output
99  * Only output levels can be modified
100  * UER, but only for the first two inputs and outputs.
101  */
102 
103 static const struct snd_vx_hardware vxp440_hw = {
104 	.name = "VXPocket440",
105 	.type = VX_TYPE_VXP440,
106 
107 	/* hardware specs */
108 	.num_codecs = 2,
109 	.num_ins = 2,
110 	.num_outs = 2,
111 	.output_level_max = VX_ANALOG_OUT_LEVEL_MAX,
112 	.output_level_db_scale = db_scale_old_vol,
113 };
114 
115 
116 /*
117  * create vxpocket instance
118  */
snd_vxpocket_new(struct snd_card * card,int ibl,struct pcmcia_device * link,struct snd_vxpocket ** chip_ret)119 static int snd_vxpocket_new(struct snd_card *card, int ibl,
120 			    struct pcmcia_device *link,
121 			    struct snd_vxpocket **chip_ret)
122 {
123 	struct vx_core *chip;
124 	struct snd_vxpocket *vxp;
125 	static const struct snd_device_ops ops = {
126 		.dev_free =	snd_vxpocket_dev_free,
127 	};
128 	int err;
129 
130 	chip = snd_vx_create(card, &vxpocket_hw, &snd_vxpocket_ops,
131 			     sizeof(struct snd_vxpocket) - sizeof(struct vx_core));
132 	if (!chip)
133 		return -ENOMEM;
134 
135 	err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
136 	if (err < 0) {
137 		kfree(chip);
138 		return err;
139 	}
140 	chip->ibl.size = ibl;
141 
142 	vxp = to_vxpocket(chip);
143 
144 	vxp->p_dev = link;
145 	link->priv = chip;
146 
147 	link->resource[0]->flags |= IO_DATA_PATH_WIDTH_AUTO;
148 	link->resource[0]->end = 16;
149 
150 	link->config_flags |= CONF_ENABLE_IRQ;
151 	link->config_index = 1;
152 	link->config_regs = PRESENT_OPTION;
153 
154 	*chip_ret = vxp;
155 	return 0;
156 }
157 
158 
159 /**
160  * snd_vxpocket_assign_resources - initialize the hardware and card instance.
161  * @chip: VX core instance
162  * @port: i/o port for the card
163  * @irq: irq number for the card
164  *
165  * this function assigns the specified port and irq, boot the card,
166  * create pcm and control instances, and initialize the rest hardware.
167  *
168  * returns 0 if successful, or a negative error code.
169  */
snd_vxpocket_assign_resources(struct vx_core * chip,int port,int irq)170 static int snd_vxpocket_assign_resources(struct vx_core *chip, int port, int irq)
171 {
172 	int err;
173 	struct snd_card *card = chip->card;
174 	struct snd_vxpocket *vxp = to_vxpocket(chip);
175 
176 	snd_printdd(KERN_DEBUG "vxpocket assign resources: port = 0x%x, irq = %d\n", port, irq);
177 	vxp->port = port;
178 
179 	sprintf(card->shortname, "Digigram %s", card->driver);
180 	sprintf(card->longname, "%s at 0x%x, irq %i",
181 		card->shortname, port, irq);
182 
183 	chip->irq = irq;
184 	card->sync_irq = chip->irq;
185 
186 	if ((err = snd_vx_setup_firmware(chip)) < 0)
187 		return err;
188 
189 	return 0;
190 }
191 
192 
193 /*
194  * configuration callback
195  */
196 
vxpocket_config(struct pcmcia_device * link)197 static int vxpocket_config(struct pcmcia_device *link)
198 {
199 	struct vx_core *chip = link->priv;
200 	int ret;
201 
202 	snd_printdd(KERN_DEBUG "vxpocket_config called\n");
203 
204 	/* redefine hardware record according to the VERSION1 string */
205 	if (!strcmp(link->prod_id[1], "VX-POCKET")) {
206 		snd_printdd("VX-pocket is detected\n");
207 	} else {
208 		snd_printdd("VX-pocket 440 is detected\n");
209 		/* overwrite the hardware information */
210 		chip->hw = &vxp440_hw;
211 		chip->type = vxp440_hw.type;
212 		strcpy(chip->card->driver, vxp440_hw.name);
213 	}
214 
215 	ret = pcmcia_request_io(link);
216 	if (ret)
217 		goto failed_preirq;
218 
219 	ret = request_threaded_irq(link->irq, snd_vx_irq_handler,
220 				   snd_vx_threaded_irq_handler,
221 				   IRQF_SHARED, link->devname, link->priv);
222 	if (ret)
223 		goto failed_preirq;
224 
225 	ret = pcmcia_enable_device(link);
226 	if (ret)
227 		goto failed;
228 
229 	chip->dev = &link->dev;
230 
231 	if (snd_vxpocket_assign_resources(chip, link->resource[0]->start,
232 						link->irq) < 0)
233 		goto failed;
234 
235 	return 0;
236 
237  failed:
238 	free_irq(link->irq, link->priv);
239 failed_preirq:
240 	pcmcia_disable_device(link);
241 	return -ENODEV;
242 }
243 
244 #ifdef CONFIG_PM
245 
vxp_suspend(struct pcmcia_device * link)246 static int vxp_suspend(struct pcmcia_device *link)
247 {
248 	struct vx_core *chip = link->priv;
249 
250 	snd_printdd(KERN_DEBUG "SUSPEND\n");
251 	if (chip) {
252 		snd_printdd(KERN_DEBUG "snd_vx_suspend calling\n");
253 		snd_vx_suspend(chip);
254 	}
255 
256 	return 0;
257 }
258 
vxp_resume(struct pcmcia_device * link)259 static int vxp_resume(struct pcmcia_device *link)
260 {
261 	struct vx_core *chip = link->priv;
262 
263 	snd_printdd(KERN_DEBUG "RESUME\n");
264 	if (pcmcia_dev_present(link)) {
265 		//struct snd_vxpocket *vxp = (struct snd_vxpocket *)chip;
266 		if (chip) {
267 			snd_printdd(KERN_DEBUG "calling snd_vx_resume\n");
268 			snd_vx_resume(chip);
269 		}
270 	}
271 	snd_printdd(KERN_DEBUG "resume done!\n");
272 
273 	return 0;
274 }
275 
276 #endif
277 
278 
279 /*
280  */
vxpocket_probe(struct pcmcia_device * p_dev)281 static int vxpocket_probe(struct pcmcia_device *p_dev)
282 {
283 	struct snd_card *card;
284 	struct snd_vxpocket *vxp;
285 	int i, err;
286 
287 	/* find an empty slot from the card list */
288 	for (i = 0; i < SNDRV_CARDS; i++) {
289 		if (!(card_alloc & (1 << i)))
290 			break;
291 	}
292 	if (i >= SNDRV_CARDS) {
293 		snd_printk(KERN_ERR "vxpocket: too many cards found\n");
294 		return -EINVAL;
295 	}
296 	if (! enable[i])
297 		return -ENODEV; /* disabled explicitly */
298 
299 	/* ok, create a card instance */
300 	err = snd_card_new(&p_dev->dev, index[i], id[i], THIS_MODULE,
301 			   0, &card);
302 	if (err < 0) {
303 		snd_printk(KERN_ERR "vxpocket: cannot create a card instance\n");
304 		return err;
305 	}
306 
307 	err = snd_vxpocket_new(card, ibl[i], p_dev, &vxp);
308 	if (err < 0) {
309 		snd_card_free(card);
310 		return err;
311 	}
312 	card->private_data = vxp;
313 
314 	vxp->index = i;
315 	card_alloc |= 1 << i;
316 
317 	vxp->p_dev = p_dev;
318 
319 	return vxpocket_config(p_dev);
320 }
321 
vxpocket_detach(struct pcmcia_device * link)322 static void vxpocket_detach(struct pcmcia_device *link)
323 {
324 	struct snd_vxpocket *vxp;
325 	struct vx_core *chip;
326 
327 	if (! link)
328 		return;
329 
330 	vxp = link->priv;
331 	chip = (struct vx_core *)vxp;
332 	card_alloc &= ~(1 << vxp->index);
333 
334 	chip->chip_status |= VX_STAT_IS_STALE; /* to be sure */
335 	snd_card_disconnect(chip->card);
336 	vxpocket_release(link);
337 	snd_card_free_when_closed(chip->card);
338 }
339 
340 /*
341  * Module entry points
342  */
343 
344 static const struct pcmcia_device_id vxp_ids[] = {
345 	PCMCIA_DEVICE_MANF_CARD(0x01f1, 0x0100),
346 	PCMCIA_DEVICE_NULL
347 };
348 MODULE_DEVICE_TABLE(pcmcia, vxp_ids);
349 
350 static struct pcmcia_driver vxp_cs_driver = {
351 	.owner		= THIS_MODULE,
352 	.name		= "snd-vxpocket",
353 	.probe		= vxpocket_probe,
354 	.remove		= vxpocket_detach,
355 	.id_table	= vxp_ids,
356 #ifdef CONFIG_PM
357 	.suspend	= vxp_suspend,
358 	.resume		= vxp_resume,
359 #endif
360 };
361 module_pcmcia_driver(vxp_cs_driver);
362