blob: 7563229828a883e509bd1e07342487085e860f4f [file] [log] [blame]
Don Prince3a370ca2010-05-12 15:18:59 +02001/*
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
Joe Perches4291ee32010-12-09 19:29:03 -080019#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
Don Prince3a370ca2010-05-12 15:18:59 +020021#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
38struct pcmidi_snd;
39
40struct pk_device {
41 unsigned long quirks;
42
43 struct hid_device *hdev;
44 struct pcmidi_snd *pm; /* pcmidi device context */
45};
46
47struct pcmidi_snd;
48
49struct 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
59struct 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
90static const char shortname[] = "PC-MIDI";
91static const char longname[] = "Prodikeys PC-MIDI Keyboard";
92
93static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
94static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
95static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
96
97module_param_array(index, int, NULL, 0444);
98module_param_array(id, charp, NULL, 0444);
99module_param_array(enable, bool, NULL, 0444);
100MODULE_PARM_DESC(index, "Index value for the PC-MIDI virtual audio driver");
101MODULE_PARM_DESC(id, "ID string for the PC-MIDI virtual audio driver");
102MODULE_PARM_DESC(enable, "Enable for the PC-MIDI virtual audio driver");
103
104
105/* Output routine for the sysfs channel file */
106static 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 */
119static 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
Jiri Kosinac3115982010-11-18 16:27:07 +0100135static DEVICE_ATTR(channel, S_IRUGO | S_IWUSR | S_IWGRP , show_channel,
Don Prince3a370ca2010-05-12 15:18:59 +0200136 store_channel);
137
138static struct device_attribute *sysfs_device_attr_channel = {
139 &dev_attr_channel,
140 };
141
142/* Output routine for the sysfs sustain file */
143static 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 */
156static 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
Jiri Kosinac3115982010-11-18 16:27:07 +0100174static DEVICE_ATTR(sustain, S_IRUGO | S_IWUSR | S_IWGRP, show_sustain,
Don Prince3a370ca2010-05-12 15:18:59 +0200175 store_sustain);
176
177static struct device_attribute *sysfs_device_attr_sustain = {
178 &dev_attr_sustain,
179 };
180
181/* Output routine for the sysfs octave file */
182static 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 */
195static 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
Jiri Kosinac3115982010-11-18 16:27:07 +0100212static DEVICE_ATTR(octave, S_IRUGO | S_IWUSR | S_IWGRP, show_octave,
Don Prince3a370ca2010-05-12 15:18:59 +0200213 store_octave);
214
215static struct device_attribute *sysfs_device_attr_octave = {
216 &dev_attr_octave,
217 };
218
219
220static 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
239drop_note:
240 spin_unlock_irqrestore(&pm->rawmidi_in_lock, flags);
241
242 return;
243}
244
Axel Linf4c79812011-07-21 15:21:33 +0800245static void pcmidi_sustained_note_release(unsigned long data)
Don Prince3a370ca2010-05-12 15:18:59 +0200246{
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
Axel Linf4c79812011-07-21 15:21:33 +0800253static void init_sustain_timers(struct pcmidi_snd *pm)
Don Prince3a370ca2010-05-12 15:18:59 +0200254{
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
Axel Linf4c79812011-07-21 15:21:33 +0800267static void stop_sustain_timers(struct pcmidi_snd *pm)
Don Prince3a370ca2010-05-12 15:18:59 +0200268{
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
279static 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) {
Joe Perches4291ee32010-12-09 19:29:03 -0800290 hid_err(hdev, "output report is empty\n");
Don Prince3a370ca2010-05-12 15:18:59 +0200291 break;
292 }
293 if (report->field[0]->report_count != 2) {
Joe Perches4291ee32010-12-09 19:29:03 -0800294 hid_err(hdev, "field count too low\n");
Don Prince3a370ca2010-05-12 15:18:59 +0200295 break;
296 }
297 pm->pcmidi_report6 = report;
298 return 0;
299 }
300 /* should never get here */
301 return -ENODEV;
302}
303
304static 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
314static 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
344static 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
389static 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
Axel Linf4c79812011-07-21 15:21:33 +0800502static int pcmidi_handle_report(
Don Prince3a370ca2010-05-12 15:18:59 +0200503 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
Axel Linf4c79812011-07-21 15:21:33 +0800521static void pcmidi_setup_extra_keys(
522 struct pcmidi_snd *pm, struct input_dev *input)
Don Prince3a370ca2010-05-12 15:18:59 +0200523{
524 /* reassigned functionality for N/A keys
525 MY PICTURES => KEY_WORDPROCESSOR
526 MY MUSIC=> KEY_SPREADSHEET
527 */
528 unsigned int keys[] = {
529 KEY_FN,
530 KEY_MESSENGER, KEY_CALENDAR,
531 KEY_ADDRESSBOOK, KEY_DOCUMENTS,
532 KEY_WORDPROCESSOR,
533 KEY_SPREADSHEET,
534 KEY_COFFEE,
535 KEY_HELP, KEY_SEND,
536 KEY_REPLY, KEY_FORWARDMAIL,
537 KEY_NEW, KEY_OPEN,
538 KEY_CLOSE, KEY_SAVE,
539 KEY_UNDO, KEY_REDO,
540 KEY_SPELLCHECK, KEY_PRINT,
541 0
542 };
543
544 unsigned int *pkeys = &keys[0];
545 unsigned short i;
546
547 if (pm->ifnum != 1) /* only set up ONCE for interace 1 */
548 return;
549
550 pm->input_ep82 = input;
551
552 for (i = 0; i < 24; i++)
553 pm->last_key[i] = 0;
554
555 while (*pkeys != 0) {
556 set_bit(*pkeys, pm->input_ep82->keybit);
557 ++pkeys;
558 }
559}
560
561static int pcmidi_set_operational(struct pcmidi_snd *pm)
562{
563 if (pm->ifnum != 1)
564 return 0; /* only set up ONCE for interace 1 */
565
566 pcmidi_get_output_report(pm);
567 pcmidi_submit_output_report(pm, 0xc1);
568 return 0;
569}
570
571static int pcmidi_snd_free(struct snd_device *dev)
572{
573 return 0;
574}
575
576static int pcmidi_in_open(struct snd_rawmidi_substream *substream)
577{
578 struct pcmidi_snd *pm = substream->rmidi->private_data;
579
580 dbg_hid("pcmidi in open\n");
581 pm->in_substream = substream;
582 return 0;
583}
584
585static int pcmidi_in_close(struct snd_rawmidi_substream *substream)
586{
587 dbg_hid("pcmidi in close\n");
588 return 0;
589}
590
591static void pcmidi_in_trigger(struct snd_rawmidi_substream *substream, int up)
592{
593 struct pcmidi_snd *pm = substream->rmidi->private_data;
594
595 dbg_hid("pcmidi in trigger %d\n", up);
596
597 pm->in_triggered = up;
598}
599
600static struct snd_rawmidi_ops pcmidi_in_ops = {
601 .open = pcmidi_in_open,
602 .close = pcmidi_in_close,
603 .trigger = pcmidi_in_trigger
604};
605
Axel Linf4c79812011-07-21 15:21:33 +0800606static int pcmidi_snd_initialise(struct pcmidi_snd *pm)
Don Prince3a370ca2010-05-12 15:18:59 +0200607{
608 static int dev;
609 struct snd_card *card;
610 struct snd_rawmidi *rwmidi;
611 int err;
612
613 static struct snd_device_ops ops = {
614 .dev_free = pcmidi_snd_free,
615 };
616
617 if (pm->ifnum != 1)
618 return 0; /* only set up midi device ONCE for interace 1 */
619
620 if (dev >= SNDRV_CARDS)
621 return -ENODEV;
622
623 if (!enable[dev]) {
624 dev++;
625 return -ENOENT;
626 }
627
628 /* Setup sound card */
629
630 err = snd_card_create(index[dev], id[dev], 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 snd_card_set_dev(card, &pm->pk->hdev->dev);
667
668 /* create sysfs variables */
669 err = device_create_file(&pm->pk->hdev->dev,
670 sysfs_device_attr_channel);
671 if (err < 0) {
672 pk_error("failed to create sysfs attribute channel: error %d\n",
673 err);
674 goto fail;
675 }
676
677 err = device_create_file(&pm->pk->hdev->dev,
678 sysfs_device_attr_sustain);
679 if (err < 0) {
680 pk_error("failed to create sysfs attribute sustain: error %d\n",
681 err);
682 goto fail_attr_sustain;
683 }
684
685 err = device_create_file(&pm->pk->hdev->dev,
686 sysfs_device_attr_octave);
687 if (err < 0) {
688 pk_error("failed to create sysfs attribute octave: error %d\n",
689 err);
690 goto fail_attr_octave;
691 }
692
693 spin_lock_init(&pm->rawmidi_in_lock);
694
695 init_sustain_timers(pm);
696 pcmidi_set_operational(pm);
697
698 /* register it */
699 err = snd_card_register(card);
700 if (err < 0) {
701 pk_error("failed to register pc-midi sound card: error %d\n",
702 err);
703 goto fail_register;
704 }
705
706 dbg_hid("pcmidi_snd_initialise finished ok\n");
707 return 0;
708
709fail_register:
710 stop_sustain_timers(pm);
711 device_remove_file(&pm->pk->hdev->dev, sysfs_device_attr_octave);
712fail_attr_octave:
713 device_remove_file(&pm->pk->hdev->dev, sysfs_device_attr_sustain);
714fail_attr_sustain:
715 device_remove_file(&pm->pk->hdev->dev, sysfs_device_attr_channel);
716fail:
717 if (pm->card) {
718 snd_card_free(pm->card);
719 pm->card = NULL;
720 }
721 return err;
722}
723
Axel Linf4c79812011-07-21 15:21:33 +0800724static int pcmidi_snd_terminate(struct pcmidi_snd *pm)
Don Prince3a370ca2010-05-12 15:18:59 +0200725{
726 if (pm->card) {
727 stop_sustain_timers(pm);
728
729 device_remove_file(&pm->pk->hdev->dev,
730 sysfs_device_attr_channel);
731 device_remove_file(&pm->pk->hdev->dev,
732 sysfs_device_attr_sustain);
733 device_remove_file(&pm->pk->hdev->dev,
734 sysfs_device_attr_octave);
735
736 snd_card_disconnect(pm->card);
737 snd_card_free_when_closed(pm->card);
738 }
739
740 return 0;
741}
742
743/*
744 * PC-MIDI report descriptor for report id is wrong.
745 */
Nikolai Kondrashov73e40082010-08-06 23:03:06 +0400746static __u8 *pk_report_fixup(struct hid_device *hdev, __u8 *rdesc,
747 unsigned int *rsize)
Don Prince3a370ca2010-05-12 15:18:59 +0200748{
Nikolai Kondrashov73e40082010-08-06 23:03:06 +0400749 if (*rsize == 178 &&
Don Prince3a370ca2010-05-12 15:18:59 +0200750 rdesc[111] == 0x06 && rdesc[112] == 0x00 &&
751 rdesc[113] == 0xff) {
Joe Perches4291ee32010-12-09 19:29:03 -0800752 hid_info(hdev,
753 "fixing up pc-midi keyboard report descriptor\n");
Don Prince3a370ca2010-05-12 15:18:59 +0200754
755 rdesc[144] = 0x18; /* report 4: was 0x10 report count */
756 }
Nikolai Kondrashov73e40082010-08-06 23:03:06 +0400757 return rdesc;
Don Prince3a370ca2010-05-12 15:18:59 +0200758}
759
760static int pk_input_mapping(struct hid_device *hdev, struct hid_input *hi,
761 struct hid_field *field, struct hid_usage *usage,
762 unsigned long **bit, int *max)
763{
764 struct pk_device *pk = (struct pk_device *)hid_get_drvdata(hdev);
765 struct pcmidi_snd *pm;
766
767 pm = pk->pm;
768
769 if (HID_UP_MSVENDOR == (usage->hid & HID_USAGE_PAGE) &&
770 1 == pm->ifnum) {
771 pcmidi_setup_extra_keys(pm, hi->input);
772 return 0;
773 }
774
775 return 0;
776}
777
778
779static int pk_raw_event(struct hid_device *hdev, struct hid_report *report,
780 u8 *data, int size)
781{
782 struct pk_device *pk = (struct pk_device *)hid_get_drvdata(hdev);
783 int ret = 0;
784
785 if (1 == pk->pm->ifnum) {
786 if (report->id == data[0])
787 switch (report->id) {
788 case 0x01: /* midi keys (qwerty)*/
789 case 0x03: /* midi keyboard (musical)*/
790 case 0x04: /* extra/midi keys (qwerty)*/
791 ret = pcmidi_handle_report(pk->pm,
792 report->id, data, size);
793 break;
794 }
795 }
796
797 return ret;
798}
799
800static int pk_probe(struct hid_device *hdev, const struct hid_device_id *id)
801{
802 int ret;
803 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
804 unsigned short ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
805 unsigned long quirks = id->driver_data;
806 struct pk_device *pk;
807 struct pcmidi_snd *pm = NULL;
808
809 pk = kzalloc(sizeof(*pk), GFP_KERNEL);
810 if (pk == NULL) {
Joe Perches4291ee32010-12-09 19:29:03 -0800811 hid_err(hdev, "can't alloc descriptor\n");
Don Prince3a370ca2010-05-12 15:18:59 +0200812 return -ENOMEM;
813 }
814
815 pk->hdev = hdev;
816
817 pm = kzalloc(sizeof(*pm), GFP_KERNEL);
818 if (pm == NULL) {
Joe Perches4291ee32010-12-09 19:29:03 -0800819 hid_err(hdev, "can't alloc descriptor\n");
Don Prince3a370ca2010-05-12 15:18:59 +0200820 ret = -ENOMEM;
821 goto err_free;
822 }
823
824 pm->pk = pk;
825 pk->pm = pm;
826 pm->ifnum = ifnum;
827
828 hid_set_drvdata(hdev, pk);
829
830 ret = hid_parse(hdev);
831 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800832 hid_err(hdev, "hid parse failed\n");
Don Prince3a370ca2010-05-12 15:18:59 +0200833 goto err_free;
834 }
835
836 if (quirks & PK_QUIRK_NOGET) { /* hid_parse cleared all the quirks */
837 hdev->quirks |= HID_QUIRK_NOGET;
838 }
839
840 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
841 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800842 hid_err(hdev, "hw start failed\n");
Don Prince3a370ca2010-05-12 15:18:59 +0200843 goto err_free;
844 }
845
846 ret = pcmidi_snd_initialise(pm);
847 if (ret < 0)
848 goto err_stop;
849
850 return 0;
851err_stop:
852 hid_hw_stop(hdev);
853err_free:
854 if (pm != NULL)
855 kfree(pm);
856
857 kfree(pk);
858 return ret;
859}
860
861static void pk_remove(struct hid_device *hdev)
862{
863 struct pk_device *pk = (struct pk_device *)hid_get_drvdata(hdev);
864 struct pcmidi_snd *pm;
865
866 pm = pk->pm;
867 if (pm) {
868 pcmidi_snd_terminate(pm);
869 kfree(pm);
870 }
871
872 hid_hw_stop(hdev);
873
874 kfree(pk);
875}
876
877static const struct hid_device_id pk_devices[] = {
878 {HID_USB_DEVICE(USB_VENDOR_ID_CREATIVELABS,
879 USB_DEVICE_ID_PRODIKEYS_PCMIDI),
880 .driver_data = PK_QUIRK_NOGET},
881 { }
882};
883MODULE_DEVICE_TABLE(hid, pk_devices);
884
885static struct hid_driver pk_driver = {
886 .name = "prodikeys",
887 .id_table = pk_devices,
888 .report_fixup = pk_report_fixup,
889 .input_mapping = pk_input_mapping,
890 .raw_event = pk_raw_event,
891 .probe = pk_probe,
892 .remove = pk_remove,
893};
894
895static int pk_init(void)
896{
897 int ret;
898
899 ret = hid_register_driver(&pk_driver);
900 if (ret)
Joe Perches4291ee32010-12-09 19:29:03 -0800901 pr_err("can't register prodikeys driver\n");
Don Prince3a370ca2010-05-12 15:18:59 +0200902
903 return ret;
904}
905
906static void pk_exit(void)
907{
908 hid_unregister_driver(&pk_driver);
909}
910
911module_init(pk_init);
912module_exit(pk_exit);
913MODULE_LICENSE("GPL");