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