-
Notifications
You must be signed in to change notification settings - Fork 0
/
novimp.c
197 lines (153 loc) · 4.72 KB
/
novimp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/usb.h>
#include <linux/usb/midi.h>
#include <sound/core.h>
#include <sound/initval.h>
#include <sound/rawmidi.h>
#include <linux/version.h>
#include "usbaudio.h"
#define MODULE_NAME "snd-novimp"
#define VERSION "0.0.2"
#define PREFIX MODULE_NAME ": "
MODULE_AUTHOR("John Luebs");
MODULE_DESCRIPTION(
"Support for auxiliary interfaces of Novation Impulse MIDI Controllers");
MODULE_LICENSE("GPL");
MODULE_VERSION(VERSION);
static const struct usb_device_id id_table[] = {
{ USB_DEVICE_INTERFACE_CLASS(0x1235, 0x0019, 0xff) },
{ USB_DEVICE_INTERFACE_CLASS(0x1235, 0x001a, 0xff) },
{ USB_DEVICE_INTERFACE_CLASS(0x1235, 0x001b, 0xff) },
{},
};
struct midi_interface {
struct usb_interface *intf;
};
struct novimp {
struct usb_device *dev;
struct snd_card *card;
struct list_head midi_list;
int card_index;
};
static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
module_param_array(index, int, NULL, 0444);
MODULE_PARM_DESC(index, "card index");
module_param_array(id, charp, NULL, 0444);
MODULE_PARM_DESC(id, "ID string");
static DEFINE_MUTEX(devices_mutex);
static struct novimp *novimp_devices[SNDRV_CARDS];
static struct usb_driver snd_novimp_usb_driver;
static int novimp_init_midi(struct novimp *ndev,
struct usb_interface *interface)
{
static const struct snd_usb_midi_endpoint_info quirk_data = {
.out_cables = 0x0001, .in_cables = 0x0001
};
static const struct snd_usb_audio_quirk quirk = {
.ifnum = QUIRK_ANY_INTERFACE,
.type = QUIRK_MIDI_FIXED_ENDPOINT,
.data = &quirk_data
};
return snd_usbmidi_create(ndev->card, interface, &ndev->midi_list,
&quirk);
}
static int novimp_create(struct usb_interface *intf, struct usb_device *dev,
int idx, struct novimp **rndev)
{
struct snd_card *card;
struct novimp *ndev;
int err;
char vname[32], pname[32], usbpath[32];
*rndev = NULL;
err = snd_card_new(&intf->dev, index[idx], id[idx], THIS_MODULE,
sizeof(*ndev), &card);
if (err < 0) {
dev_err(&dev->dev, "cannot create card instance %d\n", idx);
return err;
}
ndev = card->private_data;
ndev->dev = dev;
ndev->card = card;
INIT_LIST_HEAD(&ndev->midi_list);
ndev->card_index = idx;
strscpy(card->driver, MODULE_NAME, sizeof(card->driver));
usb_string(dev, dev->descriptor.iProduct, pname, sizeof(pname));
usb_string(dev, dev->descriptor.iManufacturer, vname, sizeof(vname));
snprintf(card->shortname, sizeof(card->shortname), "%sA%d", pname,
intf->cur_altsetting->desc.bInterfaceNumber);
usb_make_path(dev, usbpath, sizeof(usbpath));
snprintf(card->longname, sizeof card->longname, "%s %s (%s)", vname,
pname, usbpath);
*rndev = ndev;
return 0;
}
static int novimp_probe(struct usb_interface *interface,
const struct usb_device_id *usb_id)
{
/* extern int usb_driver_claim_interface(struct usb_driver *driver, */
/* struct usb_interface *iface, void *data); */
int err, i;
struct novimp *ndev = NULL;
struct usb_device *usb_dev = interface_to_usbdev(interface);
mutex_lock(&devices_mutex);
for (i = 0; i < SNDRV_CARDS; ++i) {
if (!novimp_devices[i]) {
err = novimp_create(interface, usb_dev, i, &ndev);
if (err < 0) {
dev_info(&usb_dev->dev,
PREFIX "create failed %d", err);
goto probe_error;
}
break;
}
}
if (!ndev) {
dev_err(&usb_dev->dev, "no available audio device");
err = -ENODEV;
goto probe_error;
}
err = novimp_init_midi(ndev, interface);
if (err < 0) {
dev_err(&usb_dev->dev, PREFIX "init_midi failed");
goto probe_error;
}
err = snd_card_register(ndev->card);
if (err < 0) {
snd_usbmidi_disconnect(ndev->midi_list.next);
dev_err(&usb_dev->dev, PREFIX "snd_card_register failed");
goto probe_error;
}
usb_set_intfdata(interface, ndev);
novimp_devices[ndev->card_index] = ndev;
mutex_unlock(&devices_mutex);
return 0;
probe_error:
if (ndev)
snd_card_free(ndev->card);
mutex_unlock(&devices_mutex);
return err;
}
static void novimp_disconnect(struct usb_interface *interface)
{
struct novimp *ndev = usb_get_intfdata(interface);
struct usb_device *usb_dev = interface_to_usbdev(interface);
if (!ndev)
return;
dev_info(&ndev->dev->dev, PREFIX "disconnect %s: %s",
usb_dev->dev.kobj.name, ndev->card->shortname);
mutex_lock(&devices_mutex);
if (!list_empty(&ndev->midi_list))
snd_usbmidi_disconnect(ndev->midi_list.next);
snd_card_disconnect(ndev->card);
novimp_devices[ndev->card_index] = NULL;
snd_card_free_when_closed(ndev->card);
mutex_unlock(&devices_mutex);
}
static struct usb_driver snd_novimp_usb_driver = { .name = "snd-usb-novimp",
.probe = novimp_probe,
.disconnect =
novimp_disconnect,
.id_table = id_table };
module_usb_driver(snd_novimp_usb_driver);