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