xref: /linux/sound/usb/6fire/chip.c (revision 05a54fa773284d1a7923cdfdd8f0c8dabb98bd26)
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 static struct usb_device *devices[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
35 
36 module_param_array(index, int, NULL, 0444);
37 MODULE_PARM_DESC(index, "Index value for the 6fire sound device");
38 module_param_array(id, charp, NULL, 0444);
39 MODULE_PARM_DESC(id, "ID string for the 6fire sound device.");
40 module_param_array(enable, bool, NULL, 0444);
41 MODULE_PARM_DESC(enable, "Enable the 6fire sound device.");
42 
43 static DEFINE_MUTEX(register_mutex);
44 
45 static void usb6fire_chip_abort(struct sfire_chip *chip)
46 {
47 	if (chip) {
48 		if (chip->pcm)
49 			usb6fire_pcm_abort(chip);
50 		if (chip->midi)
51 			usb6fire_midi_abort(chip);
52 		if (chip->comm)
53 			usb6fire_comm_abort(chip);
54 		if (chip->control)
55 			usb6fire_control_abort(chip);
56 		if (chip->card) {
57 			snd_card_disconnect(chip->card);
58 			snd_card_free_when_closed(chip->card);
59 			chip->card = NULL;
60 		}
61 	}
62 }
63 
64 static void usb6fire_card_free(struct snd_card *card)
65 {
66 	struct sfire_chip *chip = card->private_data;
67 
68 	if (chip) {
69 		if (chip->pcm)
70 			usb6fire_pcm_destroy(chip);
71 		if (chip->midi)
72 			usb6fire_midi_destroy(chip);
73 		if (chip->comm)
74 			usb6fire_comm_destroy(chip);
75 		if (chip->control)
76 			usb6fire_control_destroy(chip);
77 	}
78 }
79 
80 static int usb6fire_chip_probe(struct usb_interface *intf,
81 			       const struct usb_device_id *usb_id)
82 {
83 	int ret;
84 	int i;
85 	struct sfire_chip *chip = NULL;
86 	struct usb_device *device = interface_to_usbdev(intf);
87 	int regidx = -1; /* index in module parameter array */
88 	struct snd_card *card = NULL;
89 
90 	/* look if we already serve this card and return if so */
91 	scoped_guard(mutex, &register_mutex) {
92 		for (i = 0; i < SNDRV_CARDS; i++) {
93 			if (devices[i] == device) {
94 				if (chips[i])
95 					chips[i]->intf_count++;
96 				usb_set_intfdata(intf, chips[i]);
97 				return 0;
98 			} else if (!devices[i] && regidx < 0)
99 				regidx = i;
100 		}
101 		if (regidx < 0) {
102 			dev_err(&intf->dev, "too many cards registered.\n");
103 			return -ENODEV;
104 		}
105 		devices[regidx] = device;
106 	}
107 
108 	/* check, if firmware is present on device, upload it if not */
109 	ret = usb6fire_fw_init(intf);
110 	if (ret < 0)
111 		return ret;
112 	else if (ret == FW_NOT_READY) /* firmware update performed */
113 		return 0;
114 
115 	/* if we are here, card can be registered in alsa. */
116 	if (usb_set_interface(device, 0, 0) != 0) {
117 		dev_err(&intf->dev, "can't set first interface.\n");
118 		return -EIO;
119 	}
120 	ret = snd_card_new(&intf->dev, index[regidx], id[regidx],
121 			   THIS_MODULE, sizeof(struct sfire_chip), &card);
122 	if (ret < 0) {
123 		dev_err(&intf->dev, "cannot create alsa card.\n");
124 		return ret;
125 	}
126 	strscpy(card->driver, "6FireUSB");
127 	strscpy(card->shortname, "TerraTec DMX6FireUSB");
128 	sprintf(card->longname, "%s at %d:%d", card->shortname,
129 			device->bus->busnum, device->devnum);
130 
131 	chip = card->private_data;
132 	chips[regidx] = chip;
133 	chip->dev = device;
134 	chip->regidx = regidx;
135 	chip->intf_count = 1;
136 	chip->card = card;
137 	card->private_free = usb6fire_card_free;
138 
139 	ret = usb6fire_comm_init(chip);
140 	if (ret < 0)
141 		goto destroy_chip;
142 
143 	ret = usb6fire_midi_init(chip);
144 	if (ret < 0)
145 		goto destroy_chip;
146 
147 	ret = usb6fire_pcm_init(chip);
148 	if (ret < 0)
149 		goto destroy_chip;
150 
151 	ret = usb6fire_control_init(chip);
152 	if (ret < 0)
153 		goto destroy_chip;
154 
155 	ret = snd_card_register(card);
156 	if (ret < 0) {
157 		dev_err(&intf->dev, "cannot register card.");
158 		goto destroy_chip;
159 	}
160 	usb_set_intfdata(intf, chip);
161 	return 0;
162 
163 destroy_chip:
164 	snd_card_free(card);
165 	return ret;
166 }
167 
168 static void usb6fire_chip_disconnect(struct usb_interface *intf)
169 {
170 	struct sfire_chip *chip;
171 
172 	chip = usb_get_intfdata(intf);
173 	if (chip) { /* if !chip, fw upload has been performed */
174 		chip->intf_count--;
175 		if (!chip->intf_count) {
176 			scoped_guard(mutex, &register_mutex) {
177 				devices[chip->regidx] = NULL;
178 				chips[chip->regidx] = NULL;
179 			}
180 
181 			chip->shutdown = true;
182 			usb6fire_chip_abort(chip);
183 		}
184 	}
185 }
186 
187 static const struct usb_device_id device_table[] = {
188 	{
189 		.match_flags = USB_DEVICE_ID_MATCH_DEVICE,
190 		.idVendor = 0x0ccd,
191 		.idProduct = 0x0080
192 	},
193 	{}
194 };
195 
196 MODULE_DEVICE_TABLE(usb, device_table);
197 
198 static struct usb_driver usb_driver = {
199 	.name = "snd-usb-6fire",
200 	.probe = usb6fire_chip_probe,
201 	.disconnect = usb6fire_chip_disconnect,
202 	.id_table = device_table,
203 };
204 
205 module_usb_driver(usb_driver);
206