Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Torsten Schenk | c6d43ba | 2011-01-24 18:45:30 +0100 | [diff] [blame] | 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 |
Torsten Schenk | c6d43ba | 2011-01-24 18:45:30 +0100 | [diff] [blame] | 9 | * Copyright: (C) Torsten Schenk |
Torsten Schenk | c6d43ba | 2011-01-24 18:45:30 +0100 | [diff] [blame] | 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>"); |
Torsten Schenk | c596758 | 2012-02-22 15:20:45 +0100 | [diff] [blame] | 27 | MODULE_DESCRIPTION("TerraTec DMX 6Fire USB audio driver"); |
Torsten Schenk | c6d43ba | 2011-01-24 18:45:30 +0100 | [diff] [blame] | 28 | MODULE_LICENSE("GPL v2"); |
Torsten Schenk | c6d43ba | 2011-01-24 18:45:30 +0100 | [diff] [blame] | 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 */ |
Rusty Russell | a67ff6a | 2011-12-15 13:49:36 +1030 | [diff] [blame] | 32 | static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable card */ |
Torsten Schenk | c6d43ba | 2011-01-24 18:45:30 +0100 | [diff] [blame] | 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_chip_destroy(struct sfire_chip *chip) |
| 65 | { |
| 66 | if (chip) { |
| 67 | if (chip->pcm) |
| 68 | usb6fire_pcm_destroy(chip); |
| 69 | if (chip->midi) |
| 70 | usb6fire_midi_destroy(chip); |
| 71 | if (chip->comm) |
| 72 | usb6fire_comm_destroy(chip); |
| 73 | if (chip->control) |
| 74 | usb6fire_control_destroy(chip); |
| 75 | if (chip->card) |
| 76 | snd_card_free(chip->card); |
| 77 | } |
| 78 | } |
| 79 | |
Bill Pemberton | 87f9796 | 2012-12-06 12:35:28 -0500 | [diff] [blame] | 80 | static int usb6fire_chip_probe(struct usb_interface *intf, |
| 81 | const struct usb_device_id *usb_id) |
Torsten Schenk | c6d43ba | 2011-01-24 18:45:30 +0100 | [diff] [blame] | 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; |
Takashi Iwai | 9b389a8 | 2013-10-28 11:24:23 +0100 | [diff] [blame] | 99 | } else if (!devices[i] && regidx < 0) |
Torsten Schenk | c6d43ba | 2011-01-24 18:45:30 +0100 | [diff] [blame] | 100 | regidx = i; |
| 101 | } |
| 102 | if (regidx < 0) { |
| 103 | mutex_unlock(®ister_mutex); |
Takashi Iwai | e3b3757 | 2014-02-26 15:51:04 +0100 | [diff] [blame] | 104 | dev_err(&intf->dev, "too many cards registered.\n"); |
Torsten Schenk | c6d43ba | 2011-01-24 18:45:30 +0100 | [diff] [blame] | 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) { |
Takashi Iwai | e3b3757 | 2014-02-26 15:51:04 +0100 | [diff] [blame] | 119 | dev_err(&intf->dev, "can't set first interface.\n"); |
Torsten Schenk | c6d43ba | 2011-01-24 18:45:30 +0100 | [diff] [blame] | 120 | return -EIO; |
| 121 | } |
Takashi Iwai | 874b8d4 | 2014-01-29 14:22:20 +0100 | [diff] [blame] | 122 | ret = snd_card_new(&intf->dev, index[regidx], id[regidx], |
| 123 | THIS_MODULE, sizeof(struct sfire_chip), &card); |
Torsten Schenk | c6d43ba | 2011-01-24 18:45:30 +0100 | [diff] [blame] | 124 | if (ret < 0) { |
Takashi Iwai | e3b3757 | 2014-02-26 15:51:04 +0100 | [diff] [blame] | 125 | dev_err(&intf->dev, "cannot create alsa card.\n"); |
Torsten Schenk | c6d43ba | 2011-01-24 18:45:30 +0100 | [diff] [blame] | 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); |
Torsten Schenk | c6d43ba | 2011-01-24 18:45:30 +0100 | [diff] [blame] | 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 | |
| 140 | ret = usb6fire_comm_init(chip); |
Markus Elfring | f804fff | 2017-09-06 16:26:29 +0200 | [diff] [blame] | 141 | if (ret < 0) |
| 142 | goto destroy_chip; |
Torsten Schenk | c6d43ba | 2011-01-24 18:45:30 +0100 | [diff] [blame] | 143 | |
| 144 | ret = usb6fire_midi_init(chip); |
Markus Elfring | f804fff | 2017-09-06 16:26:29 +0200 | [diff] [blame] | 145 | if (ret < 0) |
| 146 | goto destroy_chip; |
Torsten Schenk | c6d43ba | 2011-01-24 18:45:30 +0100 | [diff] [blame] | 147 | |
| 148 | ret = usb6fire_pcm_init(chip); |
Markus Elfring | f804fff | 2017-09-06 16:26:29 +0200 | [diff] [blame] | 149 | if (ret < 0) |
| 150 | goto destroy_chip; |
Torsten Schenk | c6d43ba | 2011-01-24 18:45:30 +0100 | [diff] [blame] | 151 | |
| 152 | ret = usb6fire_control_init(chip); |
Markus Elfring | f804fff | 2017-09-06 16:26:29 +0200 | [diff] [blame] | 153 | if (ret < 0) |
| 154 | goto destroy_chip; |
Torsten Schenk | c6d43ba | 2011-01-24 18:45:30 +0100 | [diff] [blame] | 155 | |
| 156 | ret = snd_card_register(card); |
| 157 | if (ret < 0) { |
Takashi Iwai | e3b3757 | 2014-02-26 15:51:04 +0100 | [diff] [blame] | 158 | dev_err(&intf->dev, "cannot register card."); |
Markus Elfring | f804fff | 2017-09-06 16:26:29 +0200 | [diff] [blame] | 159 | goto destroy_chip; |
Torsten Schenk | c6d43ba | 2011-01-24 18:45:30 +0100 | [diff] [blame] | 160 | } |
| 161 | usb_set_intfdata(intf, chip); |
| 162 | return 0; |
Markus Elfring | f804fff | 2017-09-06 16:26:29 +0200 | [diff] [blame] | 163 | |
| 164 | destroy_chip: |
| 165 | usb6fire_chip_destroy(chip); |
| 166 | return ret; |
Torsten Schenk | c6d43ba | 2011-01-24 18:45:30 +0100 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | static void usb6fire_chip_disconnect(struct usb_interface *intf) |
| 170 | { |
| 171 | struct sfire_chip *chip; |
Torsten Schenk | c6d43ba | 2011-01-24 18:45:30 +0100 | [diff] [blame] | 172 | |
| 173 | chip = usb_get_intfdata(intf); |
| 174 | if (chip) { /* if !chip, fw upload has been performed */ |
Torsten Schenk | c6d43ba | 2011-01-24 18:45:30 +0100 | [diff] [blame] | 175 | chip->intf_count--; |
| 176 | if (!chip->intf_count) { |
| 177 | mutex_lock(®ister_mutex); |
| 178 | devices[chip->regidx] = NULL; |
| 179 | chips[chip->regidx] = NULL; |
| 180 | mutex_unlock(®ister_mutex); |
| 181 | |
| 182 | chip->shutdown = true; |
| 183 | usb6fire_chip_abort(chip); |
| 184 | usb6fire_chip_destroy(chip); |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
Arvind Yadav | 05d72cd | 2017-08-06 12:18:53 +0530 | [diff] [blame] | 189 | static const struct usb_device_id device_table[] = { |
Torsten Schenk | c6d43ba | 2011-01-24 18:45:30 +0100 | [diff] [blame] | 190 | { |
| 191 | .match_flags = USB_DEVICE_ID_MATCH_DEVICE, |
| 192 | .idVendor = 0x0ccd, |
| 193 | .idProduct = 0x0080 |
| 194 | }, |
| 195 | {} |
| 196 | }; |
| 197 | |
| 198 | MODULE_DEVICE_TABLE(usb, device_table); |
| 199 | |
Greg Kroah-Hartman | 424f075 | 2011-11-18 09:50:44 -0800 | [diff] [blame] | 200 | static struct usb_driver usb_driver = { |
Torsten Schenk | c6d43ba | 2011-01-24 18:45:30 +0100 | [diff] [blame] | 201 | .name = "snd-usb-6fire", |
| 202 | .probe = usb6fire_chip_probe, |
| 203 | .disconnect = usb6fire_chip_disconnect, |
| 204 | .id_table = device_table, |
| 205 | }; |
| 206 | |
Greg Kroah-Hartman | 424f075 | 2011-11-18 09:50:44 -0800 | [diff] [blame] | 207 | module_usb_driver(usb_driver); |