xref: /linux/sound/usb/6fire/chip.c (revision 87e801e1678342fc23b1eb92c0eecedf5dca79cb)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Linux driver for TerraTec DMX 6Fire USB
4  *
5  * Main routines and module definitions.
6  *
7  * Author:	Torsten Schenk <torsten.schenk@zoho.com>
8  * Created:	Jan 01, 2011
9  * Copyright:	(C) Torsten Schenk
10  */
11 
12 #include "chip.h"
13 #include "firmware.h"
14 #include "pcm.h"
15 #include "control.h"
16 #include "comm.h"
17 #include "midi.h"
18 
19 #include <linux/moduleparam.h>
20 #include <linux/interrupt.h>
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/gfp.h>
24 #include <sound/initval.h>
25 
26 MODULE_AUTHOR("Torsten Schenk <torsten.schenk@zoho.com>");
27 MODULE_DESCRIPTION("TerraTec DMX 6Fire USB audio driver");
28 MODULE_LICENSE("GPL v2");
29 
30 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-max */
31 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* Id for card */
32 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable card */
33 static struct sfire_chip *chips[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
34 
35 module_param_array(index, int, NULL, 0444);
36 MODULE_PARM_DESC(index, "Index value for the 6fire sound device");
37 module_param_array(id, charp, NULL, 0444);
38 MODULE_PARM_DESC(id, "ID string for the 6fire sound device.");
39 module_param_array(enable, bool, NULL, 0444);
40 MODULE_PARM_DESC(enable, "Enable the 6fire sound device.");
41 
42 static DEFINE_MUTEX(register_mutex);
43 
44 static void usb6fire_chip_abort(struct sfire_chip *chip)
45 {
46 	if (chip->pcm)
47 		usb6fire_pcm_abort(chip);
48 	if (chip->midi)
49 		usb6fire_midi_abort(chip);
50 	if (chip->comm)
51 		usb6fire_comm_abort(chip);
52 	if (chip->control)
53 		usb6fire_control_abort(chip);
54 }
55 
56 static void usb6fire_card_free(struct snd_card *card)
57 {
58 	struct sfire_chip *chip = card->private_data;
59 
60 	if (chip->pcm)
61 		usb6fire_pcm_destroy(chip);
62 	if (chip->midi)
63 		usb6fire_midi_destroy(chip);
64 	if (chip->comm)
65 		usb6fire_comm_destroy(chip);
66 	if (chip->control)
67 		usb6fire_control_destroy(chip);
68 }
69 
70 static int usb6fire_chip_probe(struct usb_interface *intf,
71 			       const struct usb_device_id *usb_id)
72 {
73 	int ret;
74 	int i;
75 	struct sfire_chip *chip = NULL;
76 	struct usb_device *device = interface_to_usbdev(intf);
77 	int regidx = -1; /* index in module parameter array */
78 	struct snd_card *card = NULL;
79 
80 	/* look if we already serve this card and return if so */
81 	guard(mutex)(&register_mutex);
82 	for (i = 0; i < SNDRV_CARDS; i++) {
83 		if (chips[i] && chips[i]->dev == device) {
84 			chips[i]->intf_count++;
85 			usb_set_intfdata(intf, chips[i]);
86 			return 0;
87 		} else if (!chips[i] && regidx < 0)
88 			regidx = i;
89 	}
90 	if (regidx < 0) {
91 		dev_err(&intf->dev, "too many cards registered.\n");
92 		return -ENODEV;
93 	}
94 
95 	/* check, if firmware is present on device, upload it if not */
96 	ret = usb6fire_fw_init(intf);
97 	if (ret < 0)
98 		return ret;
99 	else if (ret == FW_NOT_READY) /* firmware update performed */
100 		return 0;
101 
102 	/* if we are here, card can be registered in alsa. */
103 	if (usb_set_interface(device, 0, 0) != 0) {
104 		dev_err(&intf->dev, "can't set first interface.\n");
105 		return -EIO;
106 	}
107 	ret = snd_card_new(&intf->dev, index[regidx], id[regidx],
108 			   THIS_MODULE, sizeof(struct sfire_chip), &card);
109 	if (ret < 0) {
110 		dev_err(&intf->dev, "cannot create alsa card.\n");
111 		return ret;
112 	}
113 	strscpy(card->driver, "6FireUSB");
114 	strscpy(card->shortname, "TerraTec DMX6FireUSB");
115 	sprintf(card->longname, "%s at %d:%d", card->shortname,
116 			device->bus->busnum, device->devnum);
117 
118 	chip = card->private_data;
119 	chip->dev = device;
120 	chip->regidx = regidx;
121 	chip->intf_count = 1;
122 	chip->card = card;
123 	card->private_free = usb6fire_card_free;
124 
125 	ret = usb6fire_comm_init(chip);
126 	if (ret < 0)
127 		goto destroy_chip;
128 
129 	ret = usb6fire_midi_init(chip);
130 	if (ret < 0)
131 		goto destroy_chip;
132 
133 	ret = usb6fire_pcm_init(chip);
134 	if (ret < 0)
135 		goto destroy_chip;
136 
137 	ret = usb6fire_control_init(chip);
138 	if (ret < 0)
139 		goto destroy_chip;
140 
141 	ret = snd_card_register(card);
142 	if (ret < 0) {
143 		dev_err(&intf->dev, "cannot register card.");
144 		goto destroy_chip;
145 	}
146 
147 	usb_set_intfdata(intf, chip);
148 	chips[regidx] = chip;
149 
150 	return 0;
151 
152 destroy_chip:
153 	snd_card_free(card);
154 	return ret;
155 }
156 
157 static void usb6fire_chip_disconnect(struct usb_interface *intf)
158 {
159 	struct sfire_chip *chip;
160 	struct snd_card *card;
161 
162 	guard(mutex)(&register_mutex);
163 	chip = usb_get_intfdata(intf);
164 	/* if !chip, fw upload has been performed */
165 	if (!chip)
166 		return;
167 
168 	chip->intf_count--;
169 	if (chip->intf_count)
170 		return;
171 
172 	chips[chip->regidx] = NULL;
173 
174 	/*
175 	 * Save card pointer before teardown.
176 	 * snd_card_free_when_closed() may free card (and
177 	 * the embedded chip) immediately, so it must be
178 	 * called last and chip must not be accessed after.
179 	 */
180 	card = chip->card;
181 	chip->shutdown = true;
182 	if (card)
183 		snd_card_disconnect(card);
184 	usb6fire_chip_abort(chip);
185 	if (card)
186 		snd_card_free_when_closed(card);
187 }
188 
189 static const struct usb_device_id device_table[] = {
190 	{
191 		.match_flags = USB_DEVICE_ID_MATCH_DEVICE,
192 		.idVendor = 0x0ccd,
193 		.idProduct = 0x0080
194 	},
195 	{}
196 };
197 
198 MODULE_DEVICE_TABLE(usb, device_table);
199 
200 static struct usb_driver usb_driver = {
201 	.name = "snd-usb-6fire",
202 	.probe = usb6fire_chip_probe,
203 	.disconnect = usb6fire_chip_disconnect,
204 	.id_table = device_table,
205 };
206 
207 module_usb_driver(usb_driver);
208