-
Notifications
You must be signed in to change notification settings - Fork 3
/
chip.c
655 lines (537 loc) · 15.6 KB
/
chip.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
/*
* Linux driver for D.O.Tec EXBOX.UMA (PloyTec USB interface)
*
* Copyright 2015 (C) Google Inc.
*
* Authors: Adrian Knoth <[email protected]>
* Adrian Knoth <[email protected]>
*
* The driver is based on the hiface driver.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/version.h>
#include <sound/info.h>
#include <sound/initval.h>
#include <sound/control.h>
#include <linux/usb/audio.h>
#include "chip.h"
#include "pcm.h"
#include "midi.h"
MODULE_AUTHOR("Adrian Knoth <[email protected]>");
MODULE_DESCRIPTION("DirectOut EXBOX.UMA USB audio driver");
MODULE_LICENSE("GPL v2");
MODULE_SUPPORTED_DEVICE("{{directOut,EXBOX.UMA},"
"{directOut,EXBOX.XT}}");
static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-max */
static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* Id for card */
static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
static struct exbox_chip *chips[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
static struct usb_device *devices[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
static struct usb_driver exbox_usb_driver;
#define DRIVER_NAME "snd-usb-exbox"
#define CARD_NAME "EXBOX"
module_param_array(index, int, NULL, 0444);
MODULE_PARM_DESC(index, "Index value for " CARD_NAME " soundcard.");
module_param_array(id, charp, NULL, 0444);
MODULE_PARM_DESC(id, "ID string for " CARD_NAME " soundcard.");
module_param_array(enable, bool, NULL, 0444);
MODULE_PARM_DESC(enable, "Enable " CARD_NAME " soundcard.");
static DEFINE_MUTEX(register_mutex);
struct exbox_vendor_quirk {
const char *device_name;
u8 extra_freq;
};
/*
* Wrapper for usb_control_msg().
* Allocates a temp buffer to prevent DMAing from/to the stack.
* Copied from sound/usb/usx2y/us122l.c.
*/
static int exbox_ctl_msg(struct usb_device *dev, unsigned int pipe,
__u8 request, __u8 requesttype,
__u16 value, __u16 index, void *data,
__u16 size, int timeout)
{
int err;
void *buf = NULL;
if (size > 0) {
buf = kmemdup(data, size, GFP_KERNEL);
if (!buf)
return -ENOMEM;
}
err = usb_control_msg(dev, pipe, request, requesttype,
value, index, buf, size, timeout);
if (size > 0) {
memcpy(data, buf, size);
kfree(buf);
}
return err;
}
static int my_control_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
{
uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
uinfo->count = 1;
uinfo->value.integer.min = 0;
uinfo->value.integer.max = 1;
return 0;
}
static int my_control_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
{
struct exbox_chip *chip = snd_kcontrol_chip(kcontrol);
//struct pcm_runtime *pcm = chip->pcm;
u8 state = STREAM_DISABLED;
//if (pcm)
state = chip->device_streaming;
ucontrol->value.integer.value[0] = (state == STREAM_RUNNING) ? 1 : 0;
return 0;
}
static int my_control_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
{
struct exbox_chip *chip = snd_kcontrol_chip(kcontrol);
struct pcm_runtime *pcm_rt = chip->pcm;
struct midi_runtime *midi_rt = chip->midi;
struct usb_device *device = pcm_rt->chip->dev;
int ret;
bool want_stream;
if (!pcm_rt)
return -ENODEV;
want_stream = ucontrol->value.integer.value[0];
if (!want_stream) {
/* Stop the streams. Not sure if it really does what I think
* it does
*/
dev_err(&device->dev, "Turning streams off\n");
#if 1
if (pcm_rt->stream_state == STREAM_RUNNING) {
mutex_lock(&pcm_rt->stream_mutex);
exbox_pcm_stream_stop(pcm_rt);
mutex_unlock(&pcm_rt->stream_mutex);
}
#endif
#if 1
if (midi_rt->stream_state == STREAM_RUNNING) {
mutex_lock(&midi_rt->stream_mutex);
exbox_midi_stream_stop(midi_rt);
mutex_unlock(&midi_rt->stream_mutex);
} else {
dev_err(&device->dev, "NOT stopping MIDI streams, already in state %i\n",
midi_rt->stream_state);
}
#endif
snd_exbox_clear_problems(device, false);
/* We are no longer streaming */
chip->device_streaming = false;
return 0;
}
if (want_stream) {
/* FIXME: Do we need a mutex here? */
if (!chip->device_streaming) {
/* Turn on the stream */
snd_exbox_clear_problems(device, true);
usb_clear_halt(device, usb_sndbulkpipe(device, EP_OUT));
usb_clear_halt(device, usb_rcvbulkpipe(device, EP_IN));
usb_clear_halt(device, usb_rcvbulkpipe(device, EP_MIDI_IN));
dev_err(&device->dev, "Turning streams on\n");
}
#if 1
if (midi_rt->stream_state == STREAM_DISABLED) {
mutex_lock(&midi_rt->stream_mutex);
ret = exbox_midi_stream_start(midi_rt);
mutex_unlock(&midi_rt->stream_mutex);
if (ret) {
dev_err(&device->dev, "Error starting MIDI streams, aborting.\n");
return ret;
}
} else {
dev_err(&device->dev, "NOT starting MIDI streams, already in state %i\n",
midi_rt->stream_state);
}
#endif
#if 1
if (pcm_rt->stream_state == STREAM_DISABLED) {
mutex_lock(&pcm_rt->stream_mutex);
ret = exbox_pcm_stream_start(pcm_rt);
mutex_unlock(&pcm_rt->stream_mutex);
if (ret) {
dev_err(&device->dev, "Error starting PCM streams, aborting.\n");
return ret;
}
}
#endif
/* We are streaming */
chip->device_streaming = true;
return 0;
}
return 0;
}
static struct snd_kcontrol_new my_control = {
.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
.name = "Streaming enabled Switch",
.index = 0,
.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
SNDRV_CTL_ELEM_ACCESS_VOLATILE,
.private_value = 0xffff,
.info = my_control_info,
.get = my_control_get,
.put = my_control_put
};
static int exbox_chip_create(struct usb_interface *intf,
struct usb_device *device, int idx,
const struct exbox_vendor_quirk *quirk,
struct exbox_chip **rchip)
{
struct snd_card *card = NULL;
struct exbox_chip *chip;
int ret;
int len;
*rchip = NULL;
/* if we are here, card can be registered in alsa. */
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,15,0)
ret = snd_card_new(&intf->dev, index[idx], id[idx], THIS_MODULE,
sizeof(*chip), &card);
#else
ret = snd_card_create(index[idx], id[idx], THIS_MODULE,
sizeof(*chip), &card);
#endif
if (ret < 0) {
dev_err(&device->dev, "cannot create alsa card.\n");
return ret;
}
strlcpy(card->driver, DRIVER_NAME, sizeof(card->driver));
if (quirk && quirk->device_name)
strlcpy(card->shortname, quirk->device_name, sizeof(card->shortname));
else
strlcpy(card->shortname, "EXBOX.UMA", sizeof(card->shortname));
strlcat(card->longname, card->shortname, sizeof(card->longname));
len = strlcat(card->longname, " at ", sizeof(card->longname));
if (len < sizeof(card->longname))
usb_make_path(device, card->longname + len,
sizeof(card->longname) - len);
chip = card->private_data;
chip->dev = device;
chip->card = card;
chip->regidx = idx;
chip->intf_count = 1;
/* Init MIDI-to-PCM mux FIFO. Since we receive MIDI from userspace in
* midi.c but send this data in the PCM out handler (pcm.c), we need
* to temporarily store the information in the FIFO.
*
*/
INIT_KFIFO(chip->midi2pcm);
/* Create control interfaces */
ret = snd_ctl_add(card, snd_ctl_new1(&my_control, chip));
if (ret < 0)
return ret;
*rchip = chip;
return 0;
}
/*
Bit0: digital instead of analog input on "only 2 channel" units, doesn't apply here
X Bit1: internal mode (ignore sync from outside)
Bit2: digitally locked (read only)
Bit3: direct monitoring, doesn't apply here
X Bit4: 32bit input, doesn't apply here
X Bit5: 16 instead of 32 inputs, doesn't apply here
Bit6: constant MIDI for port reading, doesn't apply here
Bit7: unused
**/
static unsigned char snd_exbox_read_status(struct exbox_chip *chip)
{
int rc;
unsigned char status;
struct usb_device *dev = chip->dev;
rc = exbox_ctl_msg(dev,
usb_rcvctrlpipe(dev, 0),
0x49,
0xc0,
//USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
0x0, 0,
&status, sizeof(status), 1000);
if (rc < 0) {
dev_err(&dev->dev, "Error reading EXBOX status:%i\n",
rc);
}
return status;
}
int snd_exbox_write_status(struct exbox_chip *chip, unsigned char status)
{
int rc;
struct usb_device *dev = chip->dev;
rc = usb_control_msg(dev,
usb_sndctrlpipe(dev, 0),
0x49,
0x40,
status, 0, NULL, 0, 1000);
if (rc < 0) {
dev_err(&dev->dev, "Error writing status %02x\n",
status);
}
return rc;
}
int snd_exbox_get_samplerate(struct exbox_chip *chip)
{
int err;
int rate;
const size_t datalen = 3;
unsigned char data[3];
struct usb_device *dev = chip->dev;
err = exbox_ctl_msg(dev,
usb_rcvctrlpipe(dev, 0),
0x81,
0xa2,
0x0100,
EP_IN,
data,
datalen,
1000);
if (err < 0) {
dev_err(&dev->dev, "Error reading samplerate %i\n",
err);
rate = -1;
} else {
rate = data[0] + (data[1] << 8);
}
return rate;
}
int snd_exbox_set_samplerate(struct exbox_chip *chip, unsigned int rate)
{
int rc = 0;
int readback_rate;
const size_t datalen = 3;
unsigned char data[3];
struct usb_device *dev = chip->dev;
#if 0
data[0] = (rate & 0xFF);
data[1] = (rate & 0xFF00) >> 8;
data[2] = 0;
#else
/* This is the class compliant version. */
data[0] = rate;
data[1] = rate >> 8;
data[2] = rate >> 16;
#endif
rc = exbox_ctl_msg(dev,
usb_sndctrlpipe(dev, 0),
UAC_SET_CUR,
USB_TYPE_CLASS | USB_RECIP_ENDPOINT | USB_DIR_OUT,
UAC_EP_CS_ATTR_SAMPLE_RATE << 8,
EP_IN,
data,
datalen,
1000);
if (rc < 0) {
dev_err(&dev->dev, "Error setting samplerate %i: %i\n",
rate, rc);
return rc;
}
rc = exbox_ctl_msg(dev,
usb_sndctrlpipe(dev, 0),
0x01,
0x22,
0x0100,
EP_OUT,
data,
datalen,
1000);
if (rc < 0) {
dev_err(&dev->dev, "Error setting samplerate %i: %i\n",
rate, rc);
return rc;
}
readback_rate = snd_exbox_get_samplerate(chip);
chip->samplerate = readback_rate;
if (readback_rate != (unsigned) rate) {
dev_err(&dev->dev, "Error setting samplerate to %iHz (device still at %iHz)\n",
rate, readback_rate);
return -1;
}
return rc; /* 0 */
}
static void
snd_exbox_proc_read_debug(struct snd_info_entry *entry,
struct snd_info_buffer *buffer)
{
struct exbox_chip *chip = entry->private_data;
unsigned char status = snd_exbox_read_status(chip);
snd_iprintf(buffer, "PCM in_count: %lu\n", chip->in_count);
snd_iprintf(buffer, "PCM out_count: %lu\n", chip->out_count);
snd_iprintf(buffer, "MIDI in_count: %lu\n", chip->midi_in_count);
snd_iprintf(buffer, "Device streaming status: %i\n",
chip->device_streaming);
snd_iprintf(buffer, "STATUS (0x%02x):\n", status);
snd_iprintf(buffer, " Internal mode: %s\n", (status&0x02)?"on":"off");
snd_iprintf(buffer, " Digitally locked: %s\n", (status&0x04)?"yes":"no");
snd_iprintf(buffer, " Samplerate: %i\n",
snd_exbox_get_samplerate(chip));
}
static void snd_exbox_proc_init(struct exbox_chip *chip)
{
struct snd_info_entry *entry;
/* debug file to read all hdspm registers */
if (!snd_card_proc_new(chip->card, "exbox", &entry)) {
snd_info_set_text_ops(entry, chip,
snd_exbox_proc_read_debug);
} else {
dev_err(chip->card->dev, "Error setting up proc.\n");
}
return;
}
int snd_exbox_clear_problems(struct usb_device *usbdev, bool enable_streaming)
{
int onoff = 0;
if (enable_streaming) {
onoff = 1;
}
if (usb_set_interface(usbdev, 0, onoff) != 0) {
dev_err(&usbdev->dev, "can't set first interface for " CARD_NAME " device.\n");
return -EIO;
}
if (usb_set_interface(usbdev, 1, onoff) != 0) {
dev_err(&usbdev->dev, "can't set second interface.\n");
return -EIO;
}
return 0;
}
static void exbox_chip_destroy(struct exbox_chip *chip)
{
if (chip) {
if (chip->pcm)
exbox_pcm_destroy(chip);
if (chip->midi)
exbox_midi_destroy(chip);
if (chip->card)
snd_card_free(chip->card);
}
}
static int exbox_chip_probe(struct usb_interface *intf,
const struct usb_device_id *usb_id)
{
const struct exbox_vendor_quirk *quirk = (struct exbox_vendor_quirk *)usb_id->driver_info;
int ret;
int i;
struct exbox_chip *chip;
struct usb_device *device = interface_to_usbdev(intf);
int regidx = -1; /* index in module parameter array */
#if 1
ret = usb_set_interface(device, 0, 1);
if (ret != 0) {
dev_err(&device->dev, "can't set first interface for " CARD_NAME " device.\n");
return -EIO;
}
if (usb_set_interface(device, 1, 1) != 0) {
dev_err(&device->dev, "can't set second interface.\n");
return -EIO;
}
#else
ret = snd_exbox_clear_problems(device, false);
if (ret != 0) {
return ret;
}
#endif
/* check whether the card is already registered */
chip = NULL;
mutex_lock(®ister_mutex);
for (i = 0; i < SNDRV_CARDS; i++) {
if (devices[i] == device) {
if (chips[i])
chips[i]->intf_count++;
usb_set_intfdata(intf, chips[i]);
#if 0
usb_driver_claim_interface(&exbox_usb_driver,
intf, chips[i]);
#endif
mutex_unlock(®ister_mutex);
return 0;
} else if (!devices[i] && regidx < 0)
regidx = i;
}
if (regidx < 0) {
mutex_unlock(®ister_mutex);
dev_err(&intf->dev, "too many cards registered.\n");
return -ENODEV;
}
devices[regidx] = device;
ret = exbox_chip_create(intf, device, regidx, quirk, &chip);
if (ret < 0)
goto err;
chips[regidx] = chip;
#if 0
usb_driver_claim_interface(&exbox_usb_driver,
intf, chips[regidx]);
#endif
ret = exbox_pcm_init(chip, quirk ? quirk->extra_freq : 0);
if (ret < 0)
goto err_chip_destroy;
ret = exbox_midi_init(chip);
if (ret < 0)
goto err_chip_destroy;
dev_info(&device->dev, "proc init...\n");
snd_exbox_proc_init(chip);
ret = snd_card_register(chip->card);
if (ret < 0) {
dev_err(&device->dev, "cannot register " CARD_NAME " card\n");
goto err_chip_destroy;
}
usb_set_intfdata(intf, chip);
/* XXX: Maybe set to 0x32. */
snd_exbox_write_status(chip, 0x36 | (1<<6));
/* This will most likely fail, but that's no problem.*/
snd_exbox_set_samplerate(chip, 48000);
usb_clear_halt(device, usb_rcvbulkpipe(device, EP_IN));
usb_clear_halt(device, usb_rcvbulkpipe(device, EP_MIDI_IN));
usb_clear_halt(device, usb_sndbulkpipe(device, EP_OUT));
chip->device_streaming = false;
mutex_unlock(®ister_mutex);
return 0;
err_chip_destroy:
exbox_chip_destroy(chip);
err:
mutex_unlock(®ister_mutex);
return ret;
}
static void exbox_chip_disconnect(struct usb_interface *intf)
{
struct exbox_chip *chip;
struct snd_card *card;
chip = usb_get_intfdata(intf);
if (!chip)
return;
card = chip->card;
chip->intf_count--;
if (!chip->intf_count) {
mutex_lock(®ister_mutex);
devices[chip->regidx] = NULL;
chips[chip->regidx] = NULL;
mutex_unlock(®ister_mutex);
//chip->shutdown = true;
/* Make sure that the userspace cannot create new request */
snd_card_disconnect(card);
exbox_midi_abort(chip);
exbox_pcm_abort(chip);
exbox_chip_destroy(chip);
}
}
static const struct usb_device_id device_table[] = {
/*XXX: hier USB-IDs eintragen; .driver_info kann NULL sein */
{
USB_DEVICE(0x0a4a, 0xd064),
.driver_info = (unsigned long)&(const struct exbox_vendor_quirk) {
.device_name = "EXBOX.UMA",
.extra_freq = 1,
}
},
{}
};
MODULE_DEVICE_TABLE(usb, device_table);
static struct usb_driver exbox_usb_driver = {
.name = DRIVER_NAME,
.probe = exbox_chip_probe,
.disconnect = exbox_chip_disconnect,
.id_table = device_table,
};
module_usb_driver(exbox_usb_driver);