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