| 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 <[email protected]> |
| 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 <[email protected]>"); |
| 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 |
} |
| 57 |
} |
| 58 |
|
| 59 |
static void usb6fire_card_free(struct snd_card *card) |
| 60 |
{ |
| 61 |
struct sfire_chip *chip = card->private_data; |
| 62 |
|
| 63 |
if (chip) { |
| 64 |
if (chip->pcm) |
| 65 |
usb6fire_pcm_destroy(chip); |
| 66 |
if (chip->midi) |
| 67 |
usb6fire_midi_destroy(chip); |
| 68 |
if (chip->comm) |
| 69 |
usb6fire_comm_destroy(chip); |
| 70 |
if (chip->control) |
| 71 |
usb6fire_control_destroy(chip); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
static int usb6fire_chip_probe(struct usb_interface *intf, |
| 76 |
const struct usb_device_id *usb_id) |
| 77 |
{ |
| 78 |
int ret; |
| 79 |
int i; |
| 80 |
struct sfire_chip *chip = NULL; |
| 81 |
struct usb_device *device = interface_to_usbdev(intf); |
| 82 |
int regidx = -1; /* index in module parameter array */ |
| 83 |
struct snd_card *card = NULL; |
| 84 |
|
| 85 |
/* look if we already serve this card and return if so */ |
| 86 |
scoped_guard(mutex, ®ister_mutex) { |
| 87 |
for (i = 0; i < SNDRV_CARDS; i++) { |
| 88 |
if (devices[i] == device) { |
| 89 |
if (chips[i]) |
| 90 |
chips[i]->intf_count++; |
| 91 |
usb_set_intfdata(intf, chips[i]); |
| 92 |
return 0; |
| 93 |
} else if (!devices[i] && regidx < 0) |
| 94 |
regidx = i; |
| 95 |
} |
| 96 |
if (regidx < 0) { |
| 97 |
dev_err(&intf->dev, "too many cards registered.\n"); |
| 98 |
return -ENODEV; |
| 99 |
} |
| 100 |
devices[regidx] = device; |
| 101 |
} |
| 102 |
|
| 103 |
/* check, if firmware is present on device, upload it if not */ |
| 104 |
ret = usb6fire_fw_init(intf); |
| 105 |
if (ret < 0) |
| 106 |
return ret; |
| 107 |
else if (ret == FW_NOT_READY) /* firmware update performed */ |
| 108 |
return 0; |
| 109 |
|
| 110 |
/* if we are here, card can be registered in alsa. */ |
| 111 |
if (usb_set_interface(device, 0, 0) != 0) { |
| 112 |
dev_err(&intf->dev, "can't set first interface.\n"); |
| 113 |
return -EIO; |
| 114 |
} |
| 115 |
ret = snd_card_new(&intf->dev, index[regidx], id[regidx], |
| 116 |
THIS_MODULE, sizeof(struct sfire_chip), &card); |
| 117 |
if (ret < 0) { |
| 118 |
dev_err(&intf->dev, "cannot create alsa card.\n"); |
| 119 |
return ret; |
| 120 |
} |
| 121 |
strscpy(card->driver, "6FireUSB"); |
| 122 |
strscpy(card->shortname, "TerraTec DMX6FireUSB"); |
| 123 |
sprintf(card->longname, "%s at %d:%d", card->shortname, |
| 124 |
device->bus->busnum, device->devnum); |
| 125 |
|
| 126 |
chip = card->private_data; |
| 127 |
chips[regidx] = chip; |
| 128 |
chip->dev = device; |
| 129 |
chip->regidx = regidx; |
| 130 |
chip->intf_count = 1; |
| 131 |
chip->card = card; |
| 132 |
card->private_free = usb6fire_card_free; |
| 133 |
|
| 134 |
ret = usb6fire_comm_init(chip); |
| 135 |
if (ret < 0) |
| 136 |
goto destroy_chip; |
| 137 |
|
| 138 |
ret = usb6fire_midi_init(chip); |
| 139 |
if (ret < 0) |
| 140 |
goto destroy_chip; |
| 141 |
|
| 142 |
ret = usb6fire_pcm_init(chip); |
| 143 |
if (ret < 0) |
| 144 |
goto destroy_chip; |
| 145 |
|
| 146 |
ret = usb6fire_control_init(chip); |
| 147 |
if (ret < 0) |
| 148 |
goto destroy_chip; |
| 149 |
|
| 150 |
ret = snd_card_register(card); |
| 151 |
if (ret < 0) { |
| 152 |
dev_err(&intf->dev, "cannot register card."); |
| 153 |
goto destroy_chip; |
| 154 |
} |
| 155 |
usb_set_intfdata(intf, chip); |
| 156 |
return 0; |
| 157 |
|
| 158 |
destroy_chip: |
| 159 |
snd_card_free(card); |
| 160 |
return ret; |
| 161 |
} |
| 162 |
|
| 163 |
static void usb6fire_chip_disconnect(struct usb_interface *intf) |
| 164 |
{ |
| 165 |
struct sfire_chip *chip; |
| 166 |
struct snd_card *card; |
| 167 |
|
| 168 |
chip = usb_get_intfdata(intf); |
| 169 |
if (chip) { /* if !chip, fw upload has been performed */ |
| 170 |
chip->intf_count--; |
| 171 |
if (!chip->intf_count) { |
| 172 |
scoped_guard(mutex, ®ister_mutex) { |
| 173 |
devices[chip->regidx] = NULL; |
| 174 |
chips[chip->regidx] = NULL; |
| 175 |
} |
| 176 |
|
| 177 |
/* |
| 178 |
* Save card pointer before teardown. |
| 179 |
* snd_card_free_when_closed() may free card (and |
| 180 |
* the embedded chip) immediately, so it must be |
| 181 |
* called last and chip must not be accessed after. |
| 182 |
*/ |
| 183 |
card = chip->card; |
| 184 |
chip->shutdown = true; |
| 185 |
if (card) |
| 186 |
snd_card_disconnect(card); |
| 187 |
usb6fire_chip_abort(chip); |
| 188 |
if (card) |
| 189 |
snd_card_free_when_closed(card); |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
static 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 |
|
| 203 |
MODULE_DEVICE_TABLE(usb, device_table); |
| 204 |
|
| 205 |
static 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 |
|
| 212 |
module_usb_driver(usb_driver); |