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
usb6fire_chip_abort(struct sfire_chip * chip)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
usb6fire_card_free(struct snd_card * card)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
usb6fire_chip_probe(struct usb_interface * intf,const struct usb_device_id * usb_id)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)(®ister_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 chip->shutdown = true;
154 if (card)
155 snd_card_disconnect(card);
156 usb6fire_chip_abort(chip);
157 snd_card_free(card);
158 return ret;
159 }
160
usb6fire_chip_disconnect(struct usb_interface * intf)161 static void usb6fire_chip_disconnect(struct usb_interface *intf)
162 {
163 struct sfire_chip *chip;
164 struct snd_card *card;
165
166 guard(mutex)(®ister_mutex);
167 chip = usb_get_intfdata(intf);
168 /* if !chip, fw upload has been performed */
169 if (!chip)
170 return;
171
172 chip->intf_count--;
173 if (chip->intf_count)
174 return;
175
176 chips[chip->regidx] = NULL;
177
178 /*
179 * Save card pointer before teardown.
180 * snd_card_free_when_closed() may free card (and
181 * the embedded chip) immediately, so it must be
182 * called last and chip must not be accessed after.
183 */
184 card = chip->card;
185 chip->shutdown = true;
186 if (card)
187 snd_card_disconnect(card);
188 usb6fire_chip_abort(chip);
189 if (card)
190 snd_card_free_when_closed(card);
191 }
192
193 static const struct usb_device_id device_table[] = {
194 {
195 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
196 .idVendor = 0x0ccd,
197 .idProduct = 0x0080
198 },
199 {}
200 };
201
202 MODULE_DEVICE_TABLE(usb, device_table);
203
204 static struct usb_driver usb_driver = {
205 .name = "snd-usb-6fire",
206 .probe = usb6fire_chip_probe,
207 .disconnect = usb6fire_chip_disconnect,
208 .id_table = device_table,
209 };
210
211 module_usb_driver(usb_driver);
212