2 * HID driver for the Prodikeys PC-MIDI Keyboard
3 * providing midi & extra multimedia keys functionality
5 * Copyright (c) 2009 Don Prince <dhprince.devel@yahoo.co.uk>
7 * Controls for Octave Shift Up/Down, Channel, and
8 * Sustain Duration available via sysfs.
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)
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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"
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)
43 struct hid_device *hdev;
44 struct pcmidi_snd *pm; /* pcmidi device context */
49 struct pcmidi_sustain {
51 struct pcmidi_snd *pm;
52 struct timer_list timer;
55 unsigned char velocity;
58 #define PCMIDI_SUSTAINED_MAX 32
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;
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;
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
90 static const char shortname[] = "PC-MIDI";
91 static const char longname[] = "Prodikeys PC-MIDI Keyboard";
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;
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");
105 /* Output routine for the sysfs channel file */
106 static ssize_t show_channel(struct device *dev,
107 struct device_attribute *attr, char *buf)
109 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
110 struct pk_device *pk = (struct pk_device *)hid_get_drvdata(hdev);
112 dbg_hid("pcmidi sysfs read channel=%u\n", pk->pm->midi_channel);
114 return sprintf(buf, "%u (min:%u, max:%u)\n", pk->pm->midi_channel,
115 PCMIDI_CHANNEL_MIN, PCMIDI_CHANNEL_MAX);
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)
122 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
123 struct pk_device *pk = (struct pk_device *)hid_get_drvdata(hdev);
125 unsigned channel = 0;
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;
135 static DEVICE_ATTR(channel, S_IRUGO | S_IWUSR | S_IWGRP , show_channel,
138 static struct device_attribute *sysfs_device_attr_channel = {
142 /* Output routine for the sysfs sustain file */
143 static ssize_t show_sustain(struct device *dev,
144 struct device_attribute *attr, char *buf)
146 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
147 struct pk_device *pk = (struct pk_device *)hid_get_drvdata(hdev);
149 dbg_hid("pcmidi sysfs read sustain=%u\n", pk->pm->midi_sustain);
151 return sprintf(buf, "%u (off:%u, max:%u (ms))\n", pk->pm->midi_sustain,
152 PCMIDI_SUSTAIN_MIN, PCMIDI_SUSTAIN_MAX);
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)
159 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
160 struct pk_device *pk = (struct pk_device *)hid_get_drvdata(hdev);
162 unsigned sustain = 0;
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;
174 static DEVICE_ATTR(sustain, S_IRUGO | S_IWUSR | S_IWGRP, show_sustain,
177 static struct device_attribute *sysfs_device_attr_sustain = {
181 /* Output routine for the sysfs octave file */
182 static ssize_t show_octave(struct device *dev,
183 struct device_attribute *attr, char *buf)
185 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
186 struct pk_device *pk = (struct pk_device *)hid_get_drvdata(hdev);
188 dbg_hid("pcmidi sysfs read octave=%d\n", pk->pm->midi_octave);
190 return sprintf(buf, "%d (min:%d, max:%d)\n", pk->pm->midi_octave,
191 PCMIDI_OCTAVE_MIN, PCMIDI_OCTAVE_MAX);
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)
198 struct hid_device *hdev = container_of(dev, struct hid_device, dev);
199 struct pk_device *pk = (struct pk_device *)hid_get_drvdata(hdev);
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;
212 static DEVICE_ATTR(octave, S_IRUGO | S_IWUSR | S_IWGRP, show_octave,
215 static struct device_attribute *sysfs_device_attr_octave = {
220 static void pcmidi_send_note(struct pcmidi_snd *pm,
221 unsigned char status, unsigned char note, unsigned char velocity)
224 unsigned char buffer[3];
228 buffer[2] = velocity;
230 spin_lock_irqsave(&pm->rawmidi_in_lock, flags);
232 if (!pm->in_substream)
234 if (!test_bit(pm->in_substream->number, &pm->in_triggered))
237 snd_rawmidi_receive(pm->in_substream, buffer, 3);
240 spin_unlock_irqrestore(&pm->rawmidi_in_lock, flags);
245 void pcmidi_sustained_note_release(unsigned long data)
247 struct pcmidi_sustain *pms = (struct pcmidi_sustain *)data;
249 pcmidi_send_note(pms->pm, pms->status, pms->note, pms->velocity);
253 void init_sustain_timers(struct pcmidi_snd *pm)
255 struct pcmidi_sustain *pms;
258 for (i = 0; i < PCMIDI_SUSTAINED_MAX; i++) {
259 pms = &pm->sustained_notes[i];
262 setup_timer(&pms->timer, pcmidi_sustained_note_release,
267 void stop_sustain_timers(struct pcmidi_snd *pm)
269 struct pcmidi_sustain *pms;
272 for (i = 0; i < PCMIDI_SUSTAINED_MAX; i++) {
273 pms = &pm->sustained_notes[i];
275 del_timer_sync(&pms->timer);
279 static int pcmidi_get_output_report(struct pcmidi_snd *pm)
281 struct hid_device *hdev = pm->pk->hdev;
282 struct hid_report *report;
284 list_for_each_entry(report,
285 &hdev->report_enum[HID_OUTPUT_REPORT].report_list, list) {
286 if (!(6 == report->id))
289 if (report->maxfield < 1) {
290 hid_err(hdev, "output report is empty\n");
293 if (report->field[0]->report_count != 2) {
294 hid_err(hdev, "field count too low\n");
297 pm->pcmidi_report6 = report;
300 /* should never get here */
304 static void pcmidi_submit_output_report(struct pcmidi_snd *pm, int state)
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;
311 usbhid_submit_report(hdev, report, USB_DIR_OUT);
314 static int pcmidi_handle_report1(struct pcmidi_snd *pm, u8 *data)
319 bit_mask = (bit_mask << 8) | data[2];
320 bit_mask = (bit_mask << 8) | data[3];
322 dbg_hid("pcmidi mode: %d\n", pm->midi_mode);
324 /*KEY_MAIL or octave down*/
325 if (pm->midi_mode && bit_mask == 0x004000) {
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);
334 /*KEY_WWW or sustain*/
335 else if (pm->midi_mode && bit_mask == 0x000004) {
337 pm->midi_sustain_mode ^= 0x1;
341 return 0; /* continue key processing */
344 static int pcmidi_handle_report3(struct pcmidi_snd *pm, u8 *data, int size)
346 struct pcmidi_sustain *pms;
348 unsigned char status, note, velocity;
350 unsigned num_notes = (size-1)/2;
351 for (j = 0; j < num_notes; j++) {
353 velocity = data[j*2+2];
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);
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);
366 if (pm->midi_sustain_mode) {
367 for (i = 0; i < PCMIDI_SUSTAINED_MAX; i++) {
368 pms = &pm->sustained_notes[i];
370 pms->status = status;
372 pms->velocity = velocity;
375 mod_timer(&pms->timer,
377 msecs_to_jiffies(pm->midi_sustain));
383 pcmidi_send_note(pm, status, note, velocity);
389 static int pcmidi_handle_report4(struct pcmidi_snd *pm, u8 *data)
396 bit_mask = (bit_mask << 8) | data[2];
397 bit_mask = (bit_mask << 8) | data[3];
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;
410 for (bit_index = 0; bit_index < 24; bit_index++) {
412 switch ((0x01 << bit_index) & bit_mask) {
413 case 0x000010: /* Fn lock*/
414 pm->fn_state ^= 0x000010;
416 pcmidi_submit_output_report(pm, 0xc5);
418 pcmidi_submit_output_report(pm, 0xc6);
420 case 0x020000: /* midi launcher..send a key (qwerty) or not? */
421 pcmidi_submit_output_report(pm, 0xc1);
422 pm->midi_mode ^= 0x01;
424 dbg_hid("pcmidi mode: %d\n", pm->midi_mode);
426 case 0x100000: /* KEY_MESSENGER or octave up */
427 dbg_hid("pcmidi mode: %d\n", pm->midi_mode);
430 if (pm->midi_octave > 2)
432 dbg_hid("pcmidi mode: %d octave: %d\n",
433 pm->midi_mode, pm->midi_octave);
442 key = KEY_ADDRESSBOOK;
448 key = KEY_WORDPROCESSOR;
451 key = KEY_SPREADSHEET;
466 key = KEY_FORWARDMAIL;
487 key = KEY_SPELLCHECK;
494 input_event(pm->input_ep82, EV_KEY, key, 1);
495 pm->last_key[bit_index] = key;
502 int pcmidi_handle_report(
503 struct pcmidi_snd *pm, unsigned report_id, u8 *data, int size)
508 case 0x01: /* midi keys (qwerty)*/
509 ret = pcmidi_handle_report1(pm, data);
511 case 0x03: /* midi keyboard (musical)*/
512 ret = pcmidi_handle_report3(pm, data, size);
514 case 0x04: /* multimedia/midi keys (qwerty)*/
515 ret = pcmidi_handle_report4(pm, data);
521 void pcmidi_setup_extra_keys(struct pcmidi_snd *pm, struct input_dev *input)
523 /* reassigned functionality for N/A keys
524 MY PICTURES => KEY_WORDPROCESSOR
525 MY MUSIC=> KEY_SPREADSHEET
527 unsigned int keys[] = {
529 KEY_MESSENGER, KEY_CALENDAR,
530 KEY_ADDRESSBOOK, KEY_DOCUMENTS,
535 KEY_REPLY, KEY_FORWARDMAIL,
539 KEY_SPELLCHECK, KEY_PRINT,
543 unsigned int *pkeys = &keys[0];
546 if (pm->ifnum != 1) /* only set up ONCE for interace 1 */
549 pm->input_ep82 = input;
551 for (i = 0; i < 24; i++)
554 while (*pkeys != 0) {
555 set_bit(*pkeys, pm->input_ep82->keybit);
560 static int pcmidi_set_operational(struct pcmidi_snd *pm)
563 return 0; /* only set up ONCE for interace 1 */
565 pcmidi_get_output_report(pm);
566 pcmidi_submit_output_report(pm, 0xc1);
570 static int pcmidi_snd_free(struct snd_device *dev)
575 static int pcmidi_in_open(struct snd_rawmidi_substream *substream)
577 struct pcmidi_snd *pm = substream->rmidi->private_data;
579 dbg_hid("pcmidi in open\n");
580 pm->in_substream = substream;
584 static int pcmidi_in_close(struct snd_rawmidi_substream *substream)
586 dbg_hid("pcmidi in close\n");
590 static void pcmidi_in_trigger(struct snd_rawmidi_substream *substream, int up)
592 struct pcmidi_snd *pm = substream->rmidi->private_data;
594 dbg_hid("pcmidi in trigger %d\n", up);
596 pm->in_triggered = up;
599 static struct snd_rawmidi_ops pcmidi_in_ops = {
600 .open = pcmidi_in_open,
601 .close = pcmidi_in_close,
602 .trigger = pcmidi_in_trigger
605 int pcmidi_snd_initialise(struct pcmidi_snd *pm)
608 struct snd_card *card;
609 struct snd_rawmidi *rwmidi;
612 static struct snd_device_ops ops = {
613 .dev_free = pcmidi_snd_free,
617 return 0; /* only set up midi device ONCE for interace 1 */
619 if (dev >= SNDRV_CARDS)
627 /* Setup sound card */
629 err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card);
631 pk_error("failed to create pc-midi sound card\n");
637 /* Setup sound device */
638 err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, pm, &ops);
640 pk_error("failed to create pc-midi sound device: error %d\n",
645 strncpy(card->driver, shortname, sizeof(card->driver));
646 strncpy(card->shortname, shortname, sizeof(card->shortname));
647 strncpy(card->longname, longname, sizeof(card->longname));
650 err = snd_rawmidi_new(card, card->shortname, 0,
653 pk_error("failed to create pc-midi rawmidi device: error %d\n",
658 strncpy(rwmidi->name, card->shortname, sizeof(rwmidi->name));
659 rwmidi->info_flags = SNDRV_RAWMIDI_INFO_INPUT;
660 rwmidi->private_data = pm;
662 snd_rawmidi_set_ops(rwmidi, SNDRV_RAWMIDI_STREAM_INPUT,
665 snd_card_set_dev(card, &pm->pk->hdev->dev);
667 /* create sysfs variables */
668 err = device_create_file(&pm->pk->hdev->dev,
669 sysfs_device_attr_channel);
671 pk_error("failed to create sysfs attribute channel: error %d\n",
676 err = device_create_file(&pm->pk->hdev->dev,
677 sysfs_device_attr_sustain);
679 pk_error("failed to create sysfs attribute sustain: error %d\n",
681 goto fail_attr_sustain;
684 err = device_create_file(&pm->pk->hdev->dev,
685 sysfs_device_attr_octave);
687 pk_error("failed to create sysfs attribute octave: error %d\n",
689 goto fail_attr_octave;
692 spin_lock_init(&pm->rawmidi_in_lock);
694 init_sustain_timers(pm);
695 pcmidi_set_operational(pm);
698 err = snd_card_register(card);
700 pk_error("failed to register pc-midi sound card: error %d\n",
705 dbg_hid("pcmidi_snd_initialise finished ok\n");
709 stop_sustain_timers(pm);
710 device_remove_file(&pm->pk->hdev->dev, sysfs_device_attr_octave);
712 device_remove_file(&pm->pk->hdev->dev, sysfs_device_attr_sustain);
714 device_remove_file(&pm->pk->hdev->dev, sysfs_device_attr_channel);
717 snd_card_free(pm->card);
723 int pcmidi_snd_terminate(struct pcmidi_snd *pm)
726 stop_sustain_timers(pm);
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);
735 snd_card_disconnect(pm->card);
736 snd_card_free_when_closed(pm->card);
743 * PC-MIDI report descriptor for report id is wrong.
745 static __u8 *pk_report_fixup(struct hid_device *hdev, __u8 *rdesc,
749 rdesc[111] == 0x06 && rdesc[112] == 0x00 &&
750 rdesc[113] == 0xff) {
752 "fixing up pc-midi keyboard report descriptor\n");
754 rdesc[144] = 0x18; /* report 4: was 0x10 report count */
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)
763 struct pk_device *pk = (struct pk_device *)hid_get_drvdata(hdev);
764 struct pcmidi_snd *pm;
768 if (HID_UP_MSVENDOR == (usage->hid & HID_USAGE_PAGE) &&
770 pcmidi_setup_extra_keys(pm, hi->input);
778 static int pk_raw_event(struct hid_device *hdev, struct hid_report *report,
781 struct pk_device *pk = (struct pk_device *)hid_get_drvdata(hdev);
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);
799 static int pk_probe(struct hid_device *hdev, const struct hid_device_id *id)
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;
808 pk = kzalloc(sizeof(*pk), GFP_KERNEL);
810 hid_err(hdev, "can't alloc descriptor\n");
816 pm = kzalloc(sizeof(*pm), GFP_KERNEL);
818 hid_err(hdev, "can't alloc descriptor\n");
827 hid_set_drvdata(hdev, pk);
829 ret = hid_parse(hdev);
831 hid_err(hdev, "hid parse failed\n");
835 if (quirks & PK_QUIRK_NOGET) { /* hid_parse cleared all the quirks */
836 hdev->quirks |= HID_QUIRK_NOGET;
839 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
841 hid_err(hdev, "hw start failed\n");
845 ret = pcmidi_snd_initialise(pm);
860 static void pk_remove(struct hid_device *hdev)
862 struct pk_device *pk = (struct pk_device *)hid_get_drvdata(hdev);
863 struct pcmidi_snd *pm;
867 pcmidi_snd_terminate(pm);
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},
882 MODULE_DEVICE_TABLE(hid, pk_devices);
884 static struct hid_driver pk_driver = {
886 .id_table = pk_devices,
887 .report_fixup = pk_report_fixup,
888 .input_mapping = pk_input_mapping,
889 .raw_event = pk_raw_event,
894 static int pk_init(void)
898 ret = hid_register_driver(&pk_driver);
900 pr_err("can't register prodikeys driver\n");
905 static void pk_exit(void)
907 hid_unregister_driver(&pk_driver);
910 module_init(pk_init);
911 module_exit(pk_exit);
912 MODULE_LICENSE("GPL");