Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
chip.c
Go to the documentation of this file.
1 /*
2  * Linux driver for TerraTec DMX 6Fire USB
3  *
4  * Main routines and module definitions.
5  *
6  * Author: Torsten Schenk <[email protected]>
7  * Created: Jan 01, 2011
8  * 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 
30 MODULE_AUTHOR("Torsten Schenk <[email protected]>");
31 MODULE_DESCRIPTION("TerraTec DMX 6Fire USB audio driver");
32 MODULE_LICENSE("GPL v2");
33 MODULE_SUPPORTED_DEVICE("{{TerraTec, DMX 6Fire USB}}");
34 
35 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-max */
36 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* Id for card */
37 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable card */
39 static struct usb_device *devices[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
40 
41 module_param_array(index, int, NULL, 0444);
42 MODULE_PARM_DESC(index, "Index value for the 6fire sound device");
43 module_param_array(id, charp, NULL, 0444);
44 MODULE_PARM_DESC(id, "ID string for the 6fire sound device.");
45 module_param_array(enable, bool, NULL, 0444);
46 MODULE_PARM_DESC(enable, "Enable the 6fire sound device.");
47 
48 static DEFINE_MUTEX(register_mutex);
49 
50 static 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)
61  if (chip->card) {
64  chip->card = NULL;
65  }
66  }
67 }
68 
69 static void usb6fire_chip_destroy(struct sfire_chip *chip)
70 {
71  if (chip) {
72  if (chip->pcm)
74  if (chip->midi)
76  if (chip->comm)
78  if (chip->control)
80  if (chip->card)
81  snd_card_free(chip->card);
82  }
83 }
84 
85 static int __devinit usb6fire_chip_probe(struct usb_interface *intf,
86  const struct usb_device_id *usb_id)
87 {
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;
104  } else if (regidx < 0)
105  regidx = i;
106  }
107  if (regidx < 0) {
108  mutex_unlock(&register_mutex);
109  snd_printk(KERN_ERR PREFIX "too many cards registered.\n");
110  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) {
124  snd_printk(KERN_ERR PREFIX "can't set first interface.\n");
125  return -EIO;
126  }
127  ret = snd_card_create(index[regidx], id[regidx], THIS_MODULE,
128  sizeof(struct sfire_chip), &card);
129  if (ret < 0) {
130  snd_printk(KERN_ERR PREFIX "cannot create alsa card.\n");
131  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);
137  snd_card_set_dev(card, &intf->dev);
138 
139  chip = card->private_data;
140  chips[regidx] = chip;
141  chip->dev = device;
142  chip->regidx = regidx;
143  chip->intf_count = 1;
144  chip->card = card;
145 
146  ret = usb6fire_comm_init(chip);
147  if (ret < 0) {
148  usb6fire_chip_destroy(chip);
149  return ret;
150  }
151 
152  ret = usb6fire_midi_init(chip);
153  if (ret < 0) {
154  usb6fire_chip_destroy(chip);
155  return ret;
156  }
157 
158  ret = usb6fire_pcm_init(chip);
159  if (ret < 0) {
160  usb6fire_chip_destroy(chip);
161  return ret;
162  }
163 
164  ret = usb6fire_control_init(chip);
165  if (ret < 0) {
166  usb6fire_chip_destroy(chip);
167  return ret;
168  }
169 
170  ret = snd_card_register(card);
171  if (ret < 0) {
172  snd_printk(KERN_ERR PREFIX "cannot register card.");
173  usb6fire_chip_destroy(chip);
174  return ret;
175  }
176  usb_set_intfdata(intf, chip);
177  return 0;
178 }
179 
180 static void usb6fire_chip_disconnect(struct usb_interface *intf)
181 {
182  struct sfire_chip *chip;
183  struct snd_card *card;
184 
185  chip = usb_get_intfdata(intf);
186  if (chip) { /* if !chip, fw upload has been performed */
187  card = chip->card;
188  chip->intf_count--;
189  if (!chip->intf_count) {
190  mutex_lock(&register_mutex);
191  devices[chip->regidx] = NULL;
192  chips[chip->regidx] = NULL;
193  mutex_unlock(&register_mutex);
194 
195  chip->shutdown = true;
196  usb6fire_chip_abort(chip);
197  usb6fire_chip_destroy(chip);
198  }
199  }
200 }
201 
202 static struct usb_device_id device_table[] = {
203  {
204  .match_flags = USB_DEVICE_ID_MATCH_DEVICE,
205  .idVendor = 0x0ccd,
206  .idProduct = 0x0080
207  },
208  {}
209 };
210 
211 MODULE_DEVICE_TABLE(usb, device_table);
212 
213 static struct usb_driver usb_driver = {
214  .name = "snd-usb-6fire",
215  .probe = usb6fire_chip_probe,
216  .disconnect = usb6fire_chip_disconnect,
217  .id_table = device_table,
218 };
219 
220 module_usb_driver(usb_driver);