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