blob: dcddfc354ba683c65a5266a0c448d625371d21b7 [file] [log] [blame]
Torsten Schenkc6d43ba2011-01-24 18:45:30 +01001/*
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
Torsten Schenkc6d43ba2011-01-24 18:45:30 +01008 * 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>");
Torsten Schenkc5967582012-02-22 15:20:45 +010031MODULE_DESCRIPTION("TerraTec DMX 6Fire USB audio driver");
Torsten Schenkc6d43ba2011-01-24 18:45:30 +010032MODULE_LICENSE("GPL v2");
Antonio Ospite0af49ff2013-06-21 13:11:51 +020033MODULE_SUPPORTED_DEVICE("{{TerraTec,DMX 6Fire USB}}");
Torsten Schenkc6d43ba2011-01-24 18:45:30 +010034
35static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-max */
36static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* Id for card */
Rusty Russella67ff6a2011-12-15 13:49:36 +103037static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable card */
Torsten Schenkc6d43ba2011-01-24 18:45:30 +010038static 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
Bill Pemberton87f97962012-12-06 12:35:28 -050085static int usb6fire_chip_probe(struct usb_interface *intf,
86 const struct usb_device_id *usb_id)
Torsten Schenkc6d43ba2011-01-24 18:45:30 +010087{
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(&register_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(&register_mutex);
103 return 0;
Takashi Iwai9b389a82013-10-28 11:24:23 +0100104 } else if (!devices[i] && regidx < 0)
Torsten Schenkc6d43ba2011-01-24 18:45:30 +0100105 regidx = i;
106 }
107 if (regidx < 0) {
108 mutex_unlock(&register_mutex);
Takashi Iwaie3b37572014-02-26 15:51:04 +0100109 dev_err(&intf->dev, "too many cards registered.\n");
Torsten Schenkc6d43ba2011-01-24 18:45:30 +0100110 return -ENODEV;
111 }
112 devices[regidx] = device;
113 mutex_unlock(&register_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) {
Takashi Iwaie3b37572014-02-26 15:51:04 +0100124 dev_err(&intf->dev, "can't set first interface.\n");
Torsten Schenkc6d43ba2011-01-24 18:45:30 +0100125 return -EIO;
126 }
Takashi Iwai874b8d42014-01-29 14:22:20 +0100127 ret = snd_card_new(&intf->dev, index[regidx], id[regidx],
128 THIS_MODULE, sizeof(struct sfire_chip), &card);
Torsten Schenkc6d43ba2011-01-24 18:45:30 +0100129 if (ret < 0) {
Takashi Iwaie3b37572014-02-26 15:51:04 +0100130 dev_err(&intf->dev, "cannot create alsa card.\n");
Torsten Schenkc6d43ba2011-01-24 18:45:30 +0100131 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);
Torsten Schenkc6d43ba2011-01-24 18:45:30 +0100137
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 usb6fire_chip_destroy(chip);
148 return ret;
149 }
150
151 ret = usb6fire_midi_init(chip);
152 if (ret < 0) {
153 usb6fire_chip_destroy(chip);
154 return ret;
155 }
156
157 ret = usb6fire_pcm_init(chip);
158 if (ret < 0) {
159 usb6fire_chip_destroy(chip);
160 return ret;
161 }
162
163 ret = usb6fire_control_init(chip);
164 if (ret < 0) {
165 usb6fire_chip_destroy(chip);
166 return ret;
167 }
168
169 ret = snd_card_register(card);
170 if (ret < 0) {
Takashi Iwaie3b37572014-02-26 15:51:04 +0100171 dev_err(&intf->dev, "cannot register card.");
Torsten Schenkc6d43ba2011-01-24 18:45:30 +0100172 usb6fire_chip_destroy(chip);
173 return ret;
174 }
175 usb_set_intfdata(intf, chip);
176 return 0;
177}
178
179static void usb6fire_chip_disconnect(struct usb_interface *intf)
180{
181 struct sfire_chip *chip;
182 struct snd_card *card;
183
184 chip = usb_get_intfdata(intf);
185 if (chip) { /* if !chip, fw upload has been performed */
186 card = chip->card;
187 chip->intf_count--;
188 if (!chip->intf_count) {
189 mutex_lock(&register_mutex);
190 devices[chip->regidx] = NULL;
191 chips[chip->regidx] = NULL;
192 mutex_unlock(&register_mutex);
193
194 chip->shutdown = true;
195 usb6fire_chip_abort(chip);
196 usb6fire_chip_destroy(chip);
197 }
198 }
199}
200
201static struct usb_device_id device_table[] = {
202 {
203 .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
204 .idVendor = 0x0ccd,
205 .idProduct = 0x0080
206 },
207 {}
208};
209
210MODULE_DEVICE_TABLE(usb, device_table);
211
Greg Kroah-Hartman424f0752011-11-18 09:50:44 -0800212static struct usb_driver usb_driver = {
Torsten Schenkc6d43ba2011-01-24 18:45:30 +0100213 .name = "snd-usb-6fire",
214 .probe = usb6fire_chip_probe,
215 .disconnect = usb6fire_chip_disconnect,
216 .id_table = device_table,
217};
218
Greg Kroah-Hartman424f0752011-11-18 09:50:44 -0800219module_usb_driver(usb_driver);