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