Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
awilliam
GitHub Repository: awilliam/linux-vfio
Path: blob/master/drivers/hid/hid-prodikeys.c
15109 views
1
/*
2
* HID driver for the Prodikeys PC-MIDI Keyboard
3
* providing midi & extra multimedia keys functionality
4
*
5
* Copyright (c) 2009 Don Prince <[email protected]>
6
*
7
* Controls for Octave Shift Up/Down, Channel, and
8
* Sustain Duration available via sysfs.
9
*
10
*/
11
12
/*
13
* This program is free software; you can redistribute it and/or modify it
14
* under the terms of the GNU General Public License as published by the Free
15
* Software Foundation; either version 2 of the License, or (at your option)
16
* any later version.
17
*/
18
19
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
21
#include <linux/device.h>
22
#include <linux/module.h>
23
#include <linux/usb.h>
24
#include <linux/mutex.h>
25
#include <linux/hid.h>
26
#include <sound/core.h>
27
#include <sound/initval.h>
28
#include <sound/rawmidi.h>
29
#include "usbhid/usbhid.h"
30
#include "hid-ids.h"
31
32
33
#define pk_debug(format, arg...) \
34
pr_debug("hid-prodikeys: " format "\n" , ## arg)
35
#define pk_error(format, arg...) \
36
pr_err("hid-prodikeys: " format "\n" , ## arg)
37
38
struct pcmidi_snd;
39
40
struct pk_device {
41
unsigned long quirks;
42
43
struct hid_device *hdev;
44
struct pcmidi_snd *pm; /* pcmidi device context */
45
};
46
47
struct pcmidi_snd;
48
49
struct pcmidi_sustain {
50
unsigned long in_use;
51
struct pcmidi_snd *pm;
52
struct timer_list timer;
53
unsigned char status;
54
unsigned char note;
55
unsigned char velocity;
56
};
57
58
#define PCMIDI_SUSTAINED_MAX 32
59
struct pcmidi_snd {
60
struct pk_device *pk;
61
unsigned short ifnum;
62
struct hid_report *pcmidi_report6;
63
struct input_dev *input_ep82;
64
unsigned short midi_mode;
65
unsigned short midi_sustain_mode;
66
unsigned short midi_sustain;
67
unsigned short midi_channel;
68
short midi_octave;
69
struct pcmidi_sustain sustained_notes[PCMIDI_SUSTAINED_MAX];
70
unsigned short fn_state;
71
unsigned short last_key[24];
72
spinlock_t rawmidi_in_lock;
73
struct snd_card *card;
74
struct snd_rawmidi *rwmidi;
75
struct snd_rawmidi_substream *in_substream;
76
struct snd_rawmidi_substream *out_substream;
77
unsigned long in_triggered;
78
unsigned long out_active;
79
};
80
81
#define PK_QUIRK_NOGET 0x00010000
82
#define PCMIDI_MIDDLE_C 60
83
#define PCMIDI_CHANNEL_MIN 0
84
#define PCMIDI_CHANNEL_MAX 15
85
#define PCMIDI_OCTAVE_MIN (-2)
86
#define PCMIDI_OCTAVE_MAX 2
87
#define PCMIDI_SUSTAIN_MIN 0
88
#define PCMIDI_SUSTAIN_MAX 5000
89
90
static const char shortname[] = "PC-MIDI";
91
static const char longname[] = "Prodikeys PC-MIDI Keyboard";
92
93
static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
94
static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
95
static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
96
97
module_param_array(index, int, NULL, 0444);
98
module_param_array(id, charp, NULL, 0444);
99
module_param_array(enable, bool, NULL, 0444);
100
MODULE_PARM_DESC(index, "Index value for the PC-MIDI virtual audio driver");
101
MODULE_PARM_DESC(id, "ID string for the PC-MIDI virtual audio driver");
102
MODULE_PARM_DESC(enable, "Enable for the PC-MIDI virtual audio driver");
103
104
105
/* Output routine for the sysfs channel file */
106
static ssize_t show_channel(struct device *dev,
107
struct device_attribute *attr, char *buf)
108
{
109
struct hid_device *hdev = container_of(dev, struct hid_device, dev);
110
struct pk_device *pk = (struct pk_device *)hid_get_drvdata(hdev);
111
112
dbg_hid("pcmidi sysfs read channel=%u\n", pk->pm->midi_channel);
113
114
return sprintf(buf, "%u (min:%u, max:%u)\n", pk->pm->midi_channel,
115
PCMIDI_CHANNEL_MIN, PCMIDI_CHANNEL_MAX);
116
}
117
118
/* Input routine for the sysfs channel file */
119
static ssize_t store_channel(struct device *dev,
120
struct device_attribute *attr, const char *buf, size_t count)
121
{
122
struct hid_device *hdev = container_of(dev, struct hid_device, dev);
123
struct pk_device *pk = (struct pk_device *)hid_get_drvdata(hdev);
124
125
unsigned channel = 0;
126
127
if (sscanf(buf, "%u", &channel) > 0 && channel <= PCMIDI_CHANNEL_MAX) {
128
dbg_hid("pcmidi sysfs write channel=%u\n", channel);
129
pk->pm->midi_channel = channel;
130
return strlen(buf);
131
}
132
return -EINVAL;
133
}
134
135
static DEVICE_ATTR(channel, S_IRUGO | S_IWUSR | S_IWGRP , show_channel,
136
store_channel);
137
138
static struct device_attribute *sysfs_device_attr_channel = {
139
&dev_attr_channel,
140
};
141
142
/* Output routine for the sysfs sustain file */
143
static ssize_t show_sustain(struct device *dev,
144
struct device_attribute *attr, char *buf)
145
{
146
struct hid_device *hdev = container_of(dev, struct hid_device, dev);
147
struct pk_device *pk = (struct pk_device *)hid_get_drvdata(hdev);
148
149
dbg_hid("pcmidi sysfs read sustain=%u\n", pk->pm->midi_sustain);
150
151
return sprintf(buf, "%u (off:%u, max:%u (ms))\n", pk->pm->midi_sustain,
152
PCMIDI_SUSTAIN_MIN, PCMIDI_SUSTAIN_MAX);
153
}
154
155
/* Input routine for the sysfs sustain file */
156
static ssize_t store_sustain(struct device *dev,
157
struct device_attribute *attr, const char *buf, size_t count)
158
{
159
struct hid_device *hdev = container_of(dev, struct hid_device, dev);
160
struct pk_device *pk = (struct pk_device *)hid_get_drvdata(hdev);
161
162
unsigned sustain = 0;
163
164
if (sscanf(buf, "%u", &sustain) > 0 && sustain <= PCMIDI_SUSTAIN_MAX) {
165
dbg_hid("pcmidi sysfs write sustain=%u\n", sustain);
166
pk->pm->midi_sustain = sustain;
167
pk->pm->midi_sustain_mode =
168
(0 == sustain || !pk->pm->midi_mode) ? 0 : 1;
169
return strlen(buf);
170
}
171
return -EINVAL;
172
}
173
174
static DEVICE_ATTR(sustain, S_IRUGO | S_IWUSR | S_IWGRP, show_sustain,
175
store_sustain);
176
177
static struct device_attribute *sysfs_device_attr_sustain = {
178
&dev_attr_sustain,
179
};
180
181
/* Output routine for the sysfs octave file */
182
static ssize_t show_octave(struct device *dev,
183
struct device_attribute *attr, char *buf)
184
{
185
struct hid_device *hdev = container_of(dev, struct hid_device, dev);
186
struct pk_device *pk = (struct pk_device *)hid_get_drvdata(hdev);
187
188
dbg_hid("pcmidi sysfs read octave=%d\n", pk->pm->midi_octave);
189
190
return sprintf(buf, "%d (min:%d, max:%d)\n", pk->pm->midi_octave,
191
PCMIDI_OCTAVE_MIN, PCMIDI_OCTAVE_MAX);
192
}
193
194
/* Input routine for the sysfs octave file */
195
static ssize_t store_octave(struct device *dev,
196
struct device_attribute *attr, const char *buf, size_t count)
197
{
198
struct hid_device *hdev = container_of(dev, struct hid_device, dev);
199
struct pk_device *pk = (struct pk_device *)hid_get_drvdata(hdev);
200
201
int octave = 0;
202
203
if (sscanf(buf, "%d", &octave) > 0 &&
204
octave >= PCMIDI_OCTAVE_MIN && octave <= PCMIDI_OCTAVE_MAX) {
205
dbg_hid("pcmidi sysfs write octave=%d\n", octave);
206
pk->pm->midi_octave = octave;
207
return strlen(buf);
208
}
209
return -EINVAL;
210
}
211
212
static DEVICE_ATTR(octave, S_IRUGO | S_IWUSR | S_IWGRP, show_octave,
213
store_octave);
214
215
static struct device_attribute *sysfs_device_attr_octave = {
216
&dev_attr_octave,
217
};
218
219
220
static void pcmidi_send_note(struct pcmidi_snd *pm,
221
unsigned char status, unsigned char note, unsigned char velocity)
222
{
223
unsigned long flags;
224
unsigned char buffer[3];
225
226
buffer[0] = status;
227
buffer[1] = note;
228
buffer[2] = velocity;
229
230
spin_lock_irqsave(&pm->rawmidi_in_lock, flags);
231
232
if (!pm->in_substream)
233
goto drop_note;
234
if (!test_bit(pm->in_substream->number, &pm->in_triggered))
235
goto drop_note;
236
237
snd_rawmidi_receive(pm->in_substream, buffer, 3);
238
239
drop_note:
240
spin_unlock_irqrestore(&pm->rawmidi_in_lock, flags);
241
242
return;
243
}
244
245
void pcmidi_sustained_note_release(unsigned long data)
246
{
247
struct pcmidi_sustain *pms = (struct pcmidi_sustain *)data;
248
249
pcmidi_send_note(pms->pm, pms->status, pms->note, pms->velocity);
250
pms->in_use = 0;
251
}
252
253
void init_sustain_timers(struct pcmidi_snd *pm)
254
{
255
struct pcmidi_sustain *pms;
256
unsigned i;
257
258
for (i = 0; i < PCMIDI_SUSTAINED_MAX; i++) {
259
pms = &pm->sustained_notes[i];
260
pms->in_use = 0;
261
pms->pm = pm;
262
setup_timer(&pms->timer, pcmidi_sustained_note_release,
263
(unsigned long)pms);
264
}
265
}
266
267
void stop_sustain_timers(struct pcmidi_snd *pm)
268
{
269
struct pcmidi_sustain *pms;
270
unsigned i;
271
272
for (i = 0; i < PCMIDI_SUSTAINED_MAX; i++) {
273
pms = &pm->sustained_notes[i];
274
pms->in_use = 1;
275
del_timer_sync(&pms->timer);
276
}
277
}
278
279
static int pcmidi_get_output_report(struct pcmidi_snd *pm)
280
{
281
struct hid_device *hdev = pm->pk->hdev;
282
struct hid_report *report;
283
284
list_for_each_entry(report,
285
&hdev->report_enum[HID_OUTPUT_REPORT].report_list, list) {
286
if (!(6 == report->id))
287
continue;
288
289
if (report->maxfield < 1) {
290
hid_err(hdev, "output report is empty\n");
291
break;
292
}
293
if (report->field[0]->report_count != 2) {
294
hid_err(hdev, "field count too low\n");
295
break;
296
}
297
pm->pcmidi_report6 = report;
298
return 0;
299
}
300
/* should never get here */
301
return -ENODEV;
302
}
303
304
static void pcmidi_submit_output_report(struct pcmidi_snd *pm, int state)
305
{
306
struct hid_device *hdev = pm->pk->hdev;
307
struct hid_report *report = pm->pcmidi_report6;
308
report->field[0]->value[0] = 0x01;
309
report->field[0]->value[1] = state;
310
311
usbhid_submit_report(hdev, report, USB_DIR_OUT);
312
}
313
314
static int pcmidi_handle_report1(struct pcmidi_snd *pm, u8 *data)
315
{
316
u32 bit_mask;
317
318
bit_mask = data[1];
319
bit_mask = (bit_mask << 8) | data[2];
320
bit_mask = (bit_mask << 8) | data[3];
321
322
dbg_hid("pcmidi mode: %d\n", pm->midi_mode);
323
324
/*KEY_MAIL or octave down*/
325
if (pm->midi_mode && bit_mask == 0x004000) {
326
/* octave down */
327
pm->midi_octave--;
328
if (pm->midi_octave < -2)
329
pm->midi_octave = -2;
330
dbg_hid("pcmidi mode: %d octave: %d\n",
331
pm->midi_mode, pm->midi_octave);
332
return 1;
333
}
334
/*KEY_WWW or sustain*/
335
else if (pm->midi_mode && bit_mask == 0x000004) {
336
/* sustain on/off*/
337
pm->midi_sustain_mode ^= 0x1;
338
return 1;
339
}
340
341
return 0; /* continue key processing */
342
}
343
344
static int pcmidi_handle_report3(struct pcmidi_snd *pm, u8 *data, int size)
345
{
346
struct pcmidi_sustain *pms;
347
unsigned i, j;
348
unsigned char status, note, velocity;
349
350
unsigned num_notes = (size-1)/2;
351
for (j = 0; j < num_notes; j++) {
352
note = data[j*2+1];
353
velocity = data[j*2+2];
354
355
if (note < 0x81) { /* note on */
356
status = 128 + 16 + pm->midi_channel; /* 1001nnnn */
357
note = note - 0x54 + PCMIDI_MIDDLE_C +
358
(pm->midi_octave * 12);
359
if (0 == velocity)
360
velocity = 1; /* force note on */
361
} else { /* note off */
362
status = 128 + pm->midi_channel; /* 1000nnnn */
363
note = note - 0x94 + PCMIDI_MIDDLE_C +
364
(pm->midi_octave*12);
365
366
if (pm->midi_sustain_mode) {
367
for (i = 0; i < PCMIDI_SUSTAINED_MAX; i++) {
368
pms = &pm->sustained_notes[i];
369
if (!pms->in_use) {
370
pms->status = status;
371
pms->note = note;
372
pms->velocity = velocity;
373
pms->in_use = 1;
374
375
mod_timer(&pms->timer,
376
jiffies +
377
msecs_to_jiffies(pm->midi_sustain));
378
return 1;
379
}
380
}
381
}
382
}
383
pcmidi_send_note(pm, status, note, velocity);
384
}
385
386
return 1;
387
}
388
389
static int pcmidi_handle_report4(struct pcmidi_snd *pm, u8 *data)
390
{
391
unsigned key;
392
u32 bit_mask;
393
u32 bit_index;
394
395
bit_mask = data[1];
396
bit_mask = (bit_mask << 8) | data[2];
397
bit_mask = (bit_mask << 8) | data[3];
398
399
/* break keys */
400
for (bit_index = 0; bit_index < 24; bit_index++) {
401
key = pm->last_key[bit_index];
402
if (!((0x01 << bit_index) & bit_mask)) {
403
input_event(pm->input_ep82, EV_KEY,
404
pm->last_key[bit_index], 0);
405
pm->last_key[bit_index] = 0;
406
}
407
}
408
409
/* make keys */
410
for (bit_index = 0; bit_index < 24; bit_index++) {
411
key = 0;
412
switch ((0x01 << bit_index) & bit_mask) {
413
case 0x000010: /* Fn lock*/
414
pm->fn_state ^= 0x000010;
415
if (pm->fn_state)
416
pcmidi_submit_output_report(pm, 0xc5);
417
else
418
pcmidi_submit_output_report(pm, 0xc6);
419
continue;
420
case 0x020000: /* midi launcher..send a key (qwerty) or not? */
421
pcmidi_submit_output_report(pm, 0xc1);
422
pm->midi_mode ^= 0x01;
423
424
dbg_hid("pcmidi mode: %d\n", pm->midi_mode);
425
continue;
426
case 0x100000: /* KEY_MESSENGER or octave up */
427
dbg_hid("pcmidi mode: %d\n", pm->midi_mode);
428
if (pm->midi_mode) {
429
pm->midi_octave++;
430
if (pm->midi_octave > 2)
431
pm->midi_octave = 2;
432
dbg_hid("pcmidi mode: %d octave: %d\n",
433
pm->midi_mode, pm->midi_octave);
434
continue;
435
} else
436
key = KEY_MESSENGER;
437
break;
438
case 0x400000:
439
key = KEY_CALENDAR;
440
break;
441
case 0x080000:
442
key = KEY_ADDRESSBOOK;
443
break;
444
case 0x040000:
445
key = KEY_DOCUMENTS;
446
break;
447
case 0x800000:
448
key = KEY_WORDPROCESSOR;
449
break;
450
case 0x200000:
451
key = KEY_SPREADSHEET;
452
break;
453
case 0x010000:
454
key = KEY_COFFEE;
455
break;
456
case 0x000100:
457
key = KEY_HELP;
458
break;
459
case 0x000200:
460
key = KEY_SEND;
461
break;
462
case 0x000400:
463
key = KEY_REPLY;
464
break;
465
case 0x000800:
466
key = KEY_FORWARDMAIL;
467
break;
468
case 0x001000:
469
key = KEY_NEW;
470
break;
471
case 0x002000:
472
key = KEY_OPEN;
473
break;
474
case 0x004000:
475
key = KEY_CLOSE;
476
break;
477
case 0x008000:
478
key = KEY_SAVE;
479
break;
480
case 0x000001:
481
key = KEY_UNDO;
482
break;
483
case 0x000002:
484
key = KEY_REDO;
485
break;
486
case 0x000004:
487
key = KEY_SPELLCHECK;
488
break;
489
case 0x000008:
490
key = KEY_PRINT;
491
break;
492
}
493
if (key) {
494
input_event(pm->input_ep82, EV_KEY, key, 1);
495
pm->last_key[bit_index] = key;
496
}
497
}
498
499
return 1;
500
}
501
502
int pcmidi_handle_report(
503
struct pcmidi_snd *pm, unsigned report_id, u8 *data, int size)
504
{
505
int ret = 0;
506
507
switch (report_id) {
508
case 0x01: /* midi keys (qwerty)*/
509
ret = pcmidi_handle_report1(pm, data);
510
break;
511
case 0x03: /* midi keyboard (musical)*/
512
ret = pcmidi_handle_report3(pm, data, size);
513
break;
514
case 0x04: /* multimedia/midi keys (qwerty)*/
515
ret = pcmidi_handle_report4(pm, data);
516
break;
517
}
518
return ret;
519
}
520
521
void pcmidi_setup_extra_keys(struct pcmidi_snd *pm, struct input_dev *input)
522
{
523
/* reassigned functionality for N/A keys
524
MY PICTURES => KEY_WORDPROCESSOR
525
MY MUSIC=> KEY_SPREADSHEET
526
*/
527
unsigned int keys[] = {
528
KEY_FN,
529
KEY_MESSENGER, KEY_CALENDAR,
530
KEY_ADDRESSBOOK, KEY_DOCUMENTS,
531
KEY_WORDPROCESSOR,
532
KEY_SPREADSHEET,
533
KEY_COFFEE,
534
KEY_HELP, KEY_SEND,
535
KEY_REPLY, KEY_FORWARDMAIL,
536
KEY_NEW, KEY_OPEN,
537
KEY_CLOSE, KEY_SAVE,
538
KEY_UNDO, KEY_REDO,
539
KEY_SPELLCHECK, KEY_PRINT,
540
0
541
};
542
543
unsigned int *pkeys = &keys[0];
544
unsigned short i;
545
546
if (pm->ifnum != 1) /* only set up ONCE for interace 1 */
547
return;
548
549
pm->input_ep82 = input;
550
551
for (i = 0; i < 24; i++)
552
pm->last_key[i] = 0;
553
554
while (*pkeys != 0) {
555
set_bit(*pkeys, pm->input_ep82->keybit);
556
++pkeys;
557
}
558
}
559
560
static int pcmidi_set_operational(struct pcmidi_snd *pm)
561
{
562
if (pm->ifnum != 1)
563
return 0; /* only set up ONCE for interace 1 */
564
565
pcmidi_get_output_report(pm);
566
pcmidi_submit_output_report(pm, 0xc1);
567
return 0;
568
}
569
570
static int pcmidi_snd_free(struct snd_device *dev)
571
{
572
return 0;
573
}
574
575
static int pcmidi_in_open(struct snd_rawmidi_substream *substream)
576
{
577
struct pcmidi_snd *pm = substream->rmidi->private_data;
578
579
dbg_hid("pcmidi in open\n");
580
pm->in_substream = substream;
581
return 0;
582
}
583
584
static int pcmidi_in_close(struct snd_rawmidi_substream *substream)
585
{
586
dbg_hid("pcmidi in close\n");
587
return 0;
588
}
589
590
static void pcmidi_in_trigger(struct snd_rawmidi_substream *substream, int up)
591
{
592
struct pcmidi_snd *pm = substream->rmidi->private_data;
593
594
dbg_hid("pcmidi in trigger %d\n", up);
595
596
pm->in_triggered = up;
597
}
598
599
static struct snd_rawmidi_ops pcmidi_in_ops = {
600
.open = pcmidi_in_open,
601
.close = pcmidi_in_close,
602
.trigger = pcmidi_in_trigger
603
};
604
605
int pcmidi_snd_initialise(struct pcmidi_snd *pm)
606
{
607
static int dev;
608
struct snd_card *card;
609
struct snd_rawmidi *rwmidi;
610
int err;
611
612
static struct snd_device_ops ops = {
613
.dev_free = pcmidi_snd_free,
614
};
615
616
if (pm->ifnum != 1)
617
return 0; /* only set up midi device ONCE for interace 1 */
618
619
if (dev >= SNDRV_CARDS)
620
return -ENODEV;
621
622
if (!enable[dev]) {
623
dev++;
624
return -ENOENT;
625
}
626
627
/* Setup sound card */
628
629
err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
630
if (err < 0) {
631
pk_error("failed to create pc-midi sound card\n");
632
err = -ENOMEM;
633
goto fail;
634
}
635
pm->card = card;
636
637
/* Setup sound device */
638
err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, pm, &ops);
639
if (err < 0) {
640
pk_error("failed to create pc-midi sound device: error %d\n",
641
err);
642
goto fail;
643
}
644
645
strncpy(card->driver, shortname, sizeof(card->driver));
646
strncpy(card->shortname, shortname, sizeof(card->shortname));
647
strncpy(card->longname, longname, sizeof(card->longname));
648
649
/* Set up rawmidi */
650
err = snd_rawmidi_new(card, card->shortname, 0,
651
0, 1, &rwmidi);
652
if (err < 0) {
653
pk_error("failed to create pc-midi rawmidi device: error %d\n",
654
err);
655
goto fail;
656
}
657
pm->rwmidi = rwmidi;
658
strncpy(rwmidi->name, card->shortname, sizeof(rwmidi->name));
659
rwmidi->info_flags = SNDRV_RAWMIDI_INFO_INPUT;
660
rwmidi->private_data = pm;
661
662
snd_rawmidi_set_ops(rwmidi, SNDRV_RAWMIDI_STREAM_INPUT,
663
&pcmidi_in_ops);
664
665
snd_card_set_dev(card, &pm->pk->hdev->dev);
666
667
/* create sysfs variables */
668
err = device_create_file(&pm->pk->hdev->dev,
669
sysfs_device_attr_channel);
670
if (err < 0) {
671
pk_error("failed to create sysfs attribute channel: error %d\n",
672
err);
673
goto fail;
674
}
675
676
err = device_create_file(&pm->pk->hdev->dev,
677
sysfs_device_attr_sustain);
678
if (err < 0) {
679
pk_error("failed to create sysfs attribute sustain: error %d\n",
680
err);
681
goto fail_attr_sustain;
682
}
683
684
err = device_create_file(&pm->pk->hdev->dev,
685
sysfs_device_attr_octave);
686
if (err < 0) {
687
pk_error("failed to create sysfs attribute octave: error %d\n",
688
err);
689
goto fail_attr_octave;
690
}
691
692
spin_lock_init(&pm->rawmidi_in_lock);
693
694
init_sustain_timers(pm);
695
pcmidi_set_operational(pm);
696
697
/* register it */
698
err = snd_card_register(card);
699
if (err < 0) {
700
pk_error("failed to register pc-midi sound card: error %d\n",
701
err);
702
goto fail_register;
703
}
704
705
dbg_hid("pcmidi_snd_initialise finished ok\n");
706
return 0;
707
708
fail_register:
709
stop_sustain_timers(pm);
710
device_remove_file(&pm->pk->hdev->dev, sysfs_device_attr_octave);
711
fail_attr_octave:
712
device_remove_file(&pm->pk->hdev->dev, sysfs_device_attr_sustain);
713
fail_attr_sustain:
714
device_remove_file(&pm->pk->hdev->dev, sysfs_device_attr_channel);
715
fail:
716
if (pm->card) {
717
snd_card_free(pm->card);
718
pm->card = NULL;
719
}
720
return err;
721
}
722
723
int pcmidi_snd_terminate(struct pcmidi_snd *pm)
724
{
725
if (pm->card) {
726
stop_sustain_timers(pm);
727
728
device_remove_file(&pm->pk->hdev->dev,
729
sysfs_device_attr_channel);
730
device_remove_file(&pm->pk->hdev->dev,
731
sysfs_device_attr_sustain);
732
device_remove_file(&pm->pk->hdev->dev,
733
sysfs_device_attr_octave);
734
735
snd_card_disconnect(pm->card);
736
snd_card_free_when_closed(pm->card);
737
}
738
739
return 0;
740
}
741
742
/*
743
* PC-MIDI report descriptor for report id is wrong.
744
*/
745
static __u8 *pk_report_fixup(struct hid_device *hdev, __u8 *rdesc,
746
unsigned int *rsize)
747
{
748
if (*rsize == 178 &&
749
rdesc[111] == 0x06 && rdesc[112] == 0x00 &&
750
rdesc[113] == 0xff) {
751
hid_info(hdev,
752
"fixing up pc-midi keyboard report descriptor\n");
753
754
rdesc[144] = 0x18; /* report 4: was 0x10 report count */
755
}
756
return rdesc;
757
}
758
759
static int pk_input_mapping(struct hid_device *hdev, struct hid_input *hi,
760
struct hid_field *field, struct hid_usage *usage,
761
unsigned long **bit, int *max)
762
{
763
struct pk_device *pk = (struct pk_device *)hid_get_drvdata(hdev);
764
struct pcmidi_snd *pm;
765
766
pm = pk->pm;
767
768
if (HID_UP_MSVENDOR == (usage->hid & HID_USAGE_PAGE) &&
769
1 == pm->ifnum) {
770
pcmidi_setup_extra_keys(pm, hi->input);
771
return 0;
772
}
773
774
return 0;
775
}
776
777
778
static int pk_raw_event(struct hid_device *hdev, struct hid_report *report,
779
u8 *data, int size)
780
{
781
struct pk_device *pk = (struct pk_device *)hid_get_drvdata(hdev);
782
int ret = 0;
783
784
if (1 == pk->pm->ifnum) {
785
if (report->id == data[0])
786
switch (report->id) {
787
case 0x01: /* midi keys (qwerty)*/
788
case 0x03: /* midi keyboard (musical)*/
789
case 0x04: /* extra/midi keys (qwerty)*/
790
ret = pcmidi_handle_report(pk->pm,
791
report->id, data, size);
792
break;
793
}
794
}
795
796
return ret;
797
}
798
799
static int pk_probe(struct hid_device *hdev, const struct hid_device_id *id)
800
{
801
int ret;
802
struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
803
unsigned short ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
804
unsigned long quirks = id->driver_data;
805
struct pk_device *pk;
806
struct pcmidi_snd *pm = NULL;
807
808
pk = kzalloc(sizeof(*pk), GFP_KERNEL);
809
if (pk == NULL) {
810
hid_err(hdev, "can't alloc descriptor\n");
811
return -ENOMEM;
812
}
813
814
pk->hdev = hdev;
815
816
pm = kzalloc(sizeof(*pm), GFP_KERNEL);
817
if (pm == NULL) {
818
hid_err(hdev, "can't alloc descriptor\n");
819
ret = -ENOMEM;
820
goto err_free;
821
}
822
823
pm->pk = pk;
824
pk->pm = pm;
825
pm->ifnum = ifnum;
826
827
hid_set_drvdata(hdev, pk);
828
829
ret = hid_parse(hdev);
830
if (ret) {
831
hid_err(hdev, "hid parse failed\n");
832
goto err_free;
833
}
834
835
if (quirks & PK_QUIRK_NOGET) { /* hid_parse cleared all the quirks */
836
hdev->quirks |= HID_QUIRK_NOGET;
837
}
838
839
ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
840
if (ret) {
841
hid_err(hdev, "hw start failed\n");
842
goto err_free;
843
}
844
845
ret = pcmidi_snd_initialise(pm);
846
if (ret < 0)
847
goto err_stop;
848
849
return 0;
850
err_stop:
851
hid_hw_stop(hdev);
852
err_free:
853
if (pm != NULL)
854
kfree(pm);
855
856
kfree(pk);
857
return ret;
858
}
859
860
static void pk_remove(struct hid_device *hdev)
861
{
862
struct pk_device *pk = (struct pk_device *)hid_get_drvdata(hdev);
863
struct pcmidi_snd *pm;
864
865
pm = pk->pm;
866
if (pm) {
867
pcmidi_snd_terminate(pm);
868
kfree(pm);
869
}
870
871
hid_hw_stop(hdev);
872
873
kfree(pk);
874
}
875
876
static const struct hid_device_id pk_devices[] = {
877
{HID_USB_DEVICE(USB_VENDOR_ID_CREATIVELABS,
878
USB_DEVICE_ID_PRODIKEYS_PCMIDI),
879
.driver_data = PK_QUIRK_NOGET},
880
{ }
881
};
882
MODULE_DEVICE_TABLE(hid, pk_devices);
883
884
static struct hid_driver pk_driver = {
885
.name = "prodikeys",
886
.id_table = pk_devices,
887
.report_fixup = pk_report_fixup,
888
.input_mapping = pk_input_mapping,
889
.raw_event = pk_raw_event,
890
.probe = pk_probe,
891
.remove = pk_remove,
892
};
893
894
static int pk_init(void)
895
{
896
int ret;
897
898
ret = hid_register_driver(&pk_driver);
899
if (ret)
900
pr_err("can't register prodikeys driver\n");
901
902
return ret;
903
}
904
905
static void pk_exit(void)
906
{
907
hid_unregister_driver(&pk_driver);
908
}
909
910
module_init(pk_init);
911
module_exit(pk_exit);
912
MODULE_LICENSE("GPL");
913
914