18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Linux driver for TerraTec DMX 6Fire USB 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Main routines and module definitions. 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Author: Torsten Schenk <torsten.schenk@zoho.com> 88c2ecf20Sopenharmony_ci * Created: Jan 01, 2011 98c2ecf20Sopenharmony_ci * Copyright: (C) Torsten Schenk 108c2ecf20Sopenharmony_ci */ 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci#include "chip.h" 138c2ecf20Sopenharmony_ci#include "firmware.h" 148c2ecf20Sopenharmony_ci#include "pcm.h" 158c2ecf20Sopenharmony_ci#include "control.h" 168c2ecf20Sopenharmony_ci#include "comm.h" 178c2ecf20Sopenharmony_ci#include "midi.h" 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ci#include <linux/moduleparam.h> 208c2ecf20Sopenharmony_ci#include <linux/interrupt.h> 218c2ecf20Sopenharmony_ci#include <linux/module.h> 228c2ecf20Sopenharmony_ci#include <linux/init.h> 238c2ecf20Sopenharmony_ci#include <linux/gfp.h> 248c2ecf20Sopenharmony_ci#include <sound/initval.h> 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ciMODULE_AUTHOR("Torsten Schenk <torsten.schenk@zoho.com>"); 278c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("TerraTec DMX 6Fire USB audio driver"); 288c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 298c2ecf20Sopenharmony_ciMODULE_SUPPORTED_DEVICE("{{TerraTec,DMX 6Fire USB}}"); 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_cistatic int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-max */ 328c2ecf20Sopenharmony_cistatic char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* Id for card */ 338c2ecf20Sopenharmony_cistatic bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable card */ 348c2ecf20Sopenharmony_cistatic struct sfire_chip *chips[SNDRV_CARDS] = SNDRV_DEFAULT_PTR; 358c2ecf20Sopenharmony_cistatic struct usb_device *devices[SNDRV_CARDS] = SNDRV_DEFAULT_PTR; 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_cimodule_param_array(index, int, NULL, 0444); 388c2ecf20Sopenharmony_ciMODULE_PARM_DESC(index, "Index value for the 6fire sound device"); 398c2ecf20Sopenharmony_cimodule_param_array(id, charp, NULL, 0444); 408c2ecf20Sopenharmony_ciMODULE_PARM_DESC(id, "ID string for the 6fire sound device."); 418c2ecf20Sopenharmony_cimodule_param_array(enable, bool, NULL, 0444); 428c2ecf20Sopenharmony_ciMODULE_PARM_DESC(enable, "Enable the 6fire sound device."); 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_cistatic DEFINE_MUTEX(register_mutex); 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_cistatic void usb6fire_chip_abort(struct sfire_chip *chip) 478c2ecf20Sopenharmony_ci{ 488c2ecf20Sopenharmony_ci if (chip) { 498c2ecf20Sopenharmony_ci if (chip->pcm) 508c2ecf20Sopenharmony_ci usb6fire_pcm_abort(chip); 518c2ecf20Sopenharmony_ci if (chip->midi) 528c2ecf20Sopenharmony_ci usb6fire_midi_abort(chip); 538c2ecf20Sopenharmony_ci if (chip->comm) 548c2ecf20Sopenharmony_ci usb6fire_comm_abort(chip); 558c2ecf20Sopenharmony_ci if (chip->control) 568c2ecf20Sopenharmony_ci usb6fire_control_abort(chip); 578c2ecf20Sopenharmony_ci if (chip->card) { 588c2ecf20Sopenharmony_ci snd_card_disconnect(chip->card); 598c2ecf20Sopenharmony_ci snd_card_free_when_closed(chip->card); 608c2ecf20Sopenharmony_ci chip->card = NULL; 618c2ecf20Sopenharmony_ci } 628c2ecf20Sopenharmony_ci } 638c2ecf20Sopenharmony_ci} 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_cistatic void usb6fire_chip_destroy(struct sfire_chip *chip) 668c2ecf20Sopenharmony_ci{ 678c2ecf20Sopenharmony_ci if (chip) { 688c2ecf20Sopenharmony_ci if (chip->pcm) 698c2ecf20Sopenharmony_ci usb6fire_pcm_destroy(chip); 708c2ecf20Sopenharmony_ci if (chip->midi) 718c2ecf20Sopenharmony_ci usb6fire_midi_destroy(chip); 728c2ecf20Sopenharmony_ci if (chip->comm) 738c2ecf20Sopenharmony_ci usb6fire_comm_destroy(chip); 748c2ecf20Sopenharmony_ci if (chip->control) 758c2ecf20Sopenharmony_ci usb6fire_control_destroy(chip); 768c2ecf20Sopenharmony_ci if (chip->card) 778c2ecf20Sopenharmony_ci snd_card_free(chip->card); 788c2ecf20Sopenharmony_ci } 798c2ecf20Sopenharmony_ci} 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_cistatic int usb6fire_chip_probe(struct usb_interface *intf, 828c2ecf20Sopenharmony_ci const struct usb_device_id *usb_id) 838c2ecf20Sopenharmony_ci{ 848c2ecf20Sopenharmony_ci int ret; 858c2ecf20Sopenharmony_ci int i; 868c2ecf20Sopenharmony_ci struct sfire_chip *chip = NULL; 878c2ecf20Sopenharmony_ci struct usb_device *device = interface_to_usbdev(intf); 888c2ecf20Sopenharmony_ci int regidx = -1; /* index in module parameter array */ 898c2ecf20Sopenharmony_ci struct snd_card *card = NULL; 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci /* look if we already serve this card and return if so */ 928c2ecf20Sopenharmony_ci mutex_lock(®ister_mutex); 938c2ecf20Sopenharmony_ci for (i = 0; i < SNDRV_CARDS; i++) { 948c2ecf20Sopenharmony_ci if (devices[i] == device) { 958c2ecf20Sopenharmony_ci if (chips[i]) 968c2ecf20Sopenharmony_ci chips[i]->intf_count++; 978c2ecf20Sopenharmony_ci usb_set_intfdata(intf, chips[i]); 988c2ecf20Sopenharmony_ci mutex_unlock(®ister_mutex); 998c2ecf20Sopenharmony_ci return 0; 1008c2ecf20Sopenharmony_ci } else if (!devices[i] && regidx < 0) 1018c2ecf20Sopenharmony_ci regidx = i; 1028c2ecf20Sopenharmony_ci } 1038c2ecf20Sopenharmony_ci if (regidx < 0) { 1048c2ecf20Sopenharmony_ci mutex_unlock(®ister_mutex); 1058c2ecf20Sopenharmony_ci dev_err(&intf->dev, "too many cards registered.\n"); 1068c2ecf20Sopenharmony_ci return -ENODEV; 1078c2ecf20Sopenharmony_ci } 1088c2ecf20Sopenharmony_ci devices[regidx] = device; 1098c2ecf20Sopenharmony_ci mutex_unlock(®ister_mutex); 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ci /* check, if firmware is present on device, upload it if not */ 1128c2ecf20Sopenharmony_ci ret = usb6fire_fw_init(intf); 1138c2ecf20Sopenharmony_ci if (ret < 0) 1148c2ecf20Sopenharmony_ci return ret; 1158c2ecf20Sopenharmony_ci else if (ret == FW_NOT_READY) /* firmware update performed */ 1168c2ecf20Sopenharmony_ci return 0; 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_ci /* if we are here, card can be registered in alsa. */ 1198c2ecf20Sopenharmony_ci if (usb_set_interface(device, 0, 0) != 0) { 1208c2ecf20Sopenharmony_ci dev_err(&intf->dev, "can't set first interface.\n"); 1218c2ecf20Sopenharmony_ci return -EIO; 1228c2ecf20Sopenharmony_ci } 1238c2ecf20Sopenharmony_ci ret = snd_card_new(&intf->dev, index[regidx], id[regidx], 1248c2ecf20Sopenharmony_ci THIS_MODULE, sizeof(struct sfire_chip), &card); 1258c2ecf20Sopenharmony_ci if (ret < 0) { 1268c2ecf20Sopenharmony_ci dev_err(&intf->dev, "cannot create alsa card.\n"); 1278c2ecf20Sopenharmony_ci return ret; 1288c2ecf20Sopenharmony_ci } 1298c2ecf20Sopenharmony_ci strcpy(card->driver, "6FireUSB"); 1308c2ecf20Sopenharmony_ci strcpy(card->shortname, "TerraTec DMX6FireUSB"); 1318c2ecf20Sopenharmony_ci sprintf(card->longname, "%s at %d:%d", card->shortname, 1328c2ecf20Sopenharmony_ci device->bus->busnum, device->devnum); 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_ci chip = card->private_data; 1358c2ecf20Sopenharmony_ci chips[regidx] = chip; 1368c2ecf20Sopenharmony_ci chip->dev = device; 1378c2ecf20Sopenharmony_ci chip->regidx = regidx; 1388c2ecf20Sopenharmony_ci chip->intf_count = 1; 1398c2ecf20Sopenharmony_ci chip->card = card; 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ci ret = usb6fire_comm_init(chip); 1428c2ecf20Sopenharmony_ci if (ret < 0) 1438c2ecf20Sopenharmony_ci goto destroy_chip; 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ci ret = usb6fire_midi_init(chip); 1468c2ecf20Sopenharmony_ci if (ret < 0) 1478c2ecf20Sopenharmony_ci goto destroy_chip; 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ci ret = usb6fire_pcm_init(chip); 1508c2ecf20Sopenharmony_ci if (ret < 0) 1518c2ecf20Sopenharmony_ci goto destroy_chip; 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_ci ret = usb6fire_control_init(chip); 1548c2ecf20Sopenharmony_ci if (ret < 0) 1558c2ecf20Sopenharmony_ci goto destroy_chip; 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_ci ret = snd_card_register(card); 1588c2ecf20Sopenharmony_ci if (ret < 0) { 1598c2ecf20Sopenharmony_ci dev_err(&intf->dev, "cannot register card."); 1608c2ecf20Sopenharmony_ci goto destroy_chip; 1618c2ecf20Sopenharmony_ci } 1628c2ecf20Sopenharmony_ci usb_set_intfdata(intf, chip); 1638c2ecf20Sopenharmony_ci return 0; 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_cidestroy_chip: 1668c2ecf20Sopenharmony_ci usb6fire_chip_destroy(chip); 1678c2ecf20Sopenharmony_ci return ret; 1688c2ecf20Sopenharmony_ci} 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_cistatic void usb6fire_chip_disconnect(struct usb_interface *intf) 1718c2ecf20Sopenharmony_ci{ 1728c2ecf20Sopenharmony_ci struct sfire_chip *chip; 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_ci chip = usb_get_intfdata(intf); 1758c2ecf20Sopenharmony_ci if (chip) { /* if !chip, fw upload has been performed */ 1768c2ecf20Sopenharmony_ci chip->intf_count--; 1778c2ecf20Sopenharmony_ci if (!chip->intf_count) { 1788c2ecf20Sopenharmony_ci mutex_lock(®ister_mutex); 1798c2ecf20Sopenharmony_ci devices[chip->regidx] = NULL; 1808c2ecf20Sopenharmony_ci chips[chip->regidx] = NULL; 1818c2ecf20Sopenharmony_ci mutex_unlock(®ister_mutex); 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci chip->shutdown = true; 1848c2ecf20Sopenharmony_ci usb6fire_chip_abort(chip); 1858c2ecf20Sopenharmony_ci usb6fire_chip_destroy(chip); 1868c2ecf20Sopenharmony_ci } 1878c2ecf20Sopenharmony_ci } 1888c2ecf20Sopenharmony_ci} 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_cistatic const struct usb_device_id device_table[] = { 1918c2ecf20Sopenharmony_ci { 1928c2ecf20Sopenharmony_ci .match_flags = USB_DEVICE_ID_MATCH_DEVICE, 1938c2ecf20Sopenharmony_ci .idVendor = 0x0ccd, 1948c2ecf20Sopenharmony_ci .idProduct = 0x0080 1958c2ecf20Sopenharmony_ci }, 1968c2ecf20Sopenharmony_ci {} 1978c2ecf20Sopenharmony_ci}; 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(usb, device_table); 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_cistatic struct usb_driver usb_driver = { 2028c2ecf20Sopenharmony_ci .name = "snd-usb-6fire", 2038c2ecf20Sopenharmony_ci .probe = usb6fire_chip_probe, 2048c2ecf20Sopenharmony_ci .disconnect = usb6fire_chip_disconnect, 2058c2ecf20Sopenharmony_ci .id_table = device_table, 2068c2ecf20Sopenharmony_ci}; 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_cimodule_usb_driver(usb_driver); 209