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 */
47 struct pcmidi_sustain
{
49 struct pcmidi_snd
*pm
;
50 struct timer_list timer
;
53 unsigned char velocity
;
56 #define PCMIDI_SUSTAINED_MAX 32
60 struct hid_report
*pcmidi_report6
;
61 struct input_dev
*input_ep82
;
62 unsigned short midi_mode
;
63 unsigned short midi_sustain_mode
;
64 unsigned short midi_sustain
;
65 unsigned short midi_channel
;
67 struct pcmidi_sustain sustained_notes
[PCMIDI_SUSTAINED_MAX
];
68 unsigned short fn_state
;
69 unsigned short last_key
[24];
70 spinlock_t rawmidi_in_lock
;
71 struct snd_card
*card
;
72 struct snd_rawmidi
*rwmidi
;
73 struct snd_rawmidi_substream
*in_substream
;
74 struct snd_rawmidi_substream
*out_substream
;
75 unsigned long in_triggered
;
76 unsigned long out_active
;
79 #define PK_QUIRK_NOGET 0x00010000
80 #define PCMIDI_MIDDLE_C 60
81 #define PCMIDI_CHANNEL_MIN 0
82 #define PCMIDI_CHANNEL_MAX 15
83 #define PCMIDI_OCTAVE_MIN (-2)
84 #define PCMIDI_OCTAVE_MAX 2
85 #define PCMIDI_SUSTAIN_MIN 0
86 #define PCMIDI_SUSTAIN_MAX 5000
88 static const char shortname
[] = "PC-MIDI";
89 static const char longname
[] = "Prodikeys PC-MIDI Keyboard";
91 static int index
[SNDRV_CARDS
] = SNDRV_DEFAULT_IDX
;
92 static char *id
[SNDRV_CARDS
] = SNDRV_DEFAULT_STR
;
93 static bool enable
[SNDRV_CARDS
] = SNDRV_DEFAULT_ENABLE_PNP
;
95 module_param_array(index
, int, NULL
, 0444);
96 module_param_array(id
, charp
, NULL
, 0444);
97 module_param_array(enable
, bool, NULL
, 0444);
98 MODULE_PARM_DESC(index
, "Index value for the PC-MIDI virtual audio driver");
99 MODULE_PARM_DESC(id
, "ID string for the PC-MIDI virtual audio driver");
100 MODULE_PARM_DESC(enable
, "Enable for the PC-MIDI virtual audio driver");
103 /* Output routine for the sysfs channel file */
104 static ssize_t
show_channel(struct device
*dev
,
105 struct device_attribute
*attr
, char *buf
)
107 struct hid_device
*hdev
= container_of(dev
, struct hid_device
, dev
);
108 struct pk_device
*pk
= hid_get_drvdata(hdev
);
110 dbg_hid("pcmidi sysfs read channel=%u\n", pk
->pm
->midi_channel
);
112 return sprintf(buf
, "%u (min:%u, max:%u)\n", pk
->pm
->midi_channel
,
113 PCMIDI_CHANNEL_MIN
, PCMIDI_CHANNEL_MAX
);
116 /* Input routine for the sysfs channel file */
117 static ssize_t
store_channel(struct device
*dev
,
118 struct device_attribute
*attr
, const char *buf
, size_t count
)
120 struct hid_device
*hdev
= container_of(dev
, struct hid_device
, dev
);
121 struct pk_device
*pk
= hid_get_drvdata(hdev
);
123 unsigned channel
= 0;
125 if (sscanf(buf
, "%u", &channel
) > 0 && channel
<= PCMIDI_CHANNEL_MAX
) {
126 dbg_hid("pcmidi sysfs write channel=%u\n", channel
);
127 pk
->pm
->midi_channel
= channel
;
133 static DEVICE_ATTR(channel
, S_IRUGO
| S_IWUSR
| S_IWGRP
, show_channel
,
136 static struct device_attribute
*sysfs_device_attr_channel
= {
140 /* Output routine for the sysfs sustain file */
141 static ssize_t
show_sustain(struct device
*dev
,
142 struct device_attribute
*attr
, char *buf
)
144 struct hid_device
*hdev
= container_of(dev
, struct hid_device
, dev
);
145 struct pk_device
*pk
= hid_get_drvdata(hdev
);
147 dbg_hid("pcmidi sysfs read sustain=%u\n", pk
->pm
->midi_sustain
);
149 return sprintf(buf
, "%u (off:%u, max:%u (ms))\n", pk
->pm
->midi_sustain
,
150 PCMIDI_SUSTAIN_MIN
, PCMIDI_SUSTAIN_MAX
);
153 /* Input routine for the sysfs sustain file */
154 static ssize_t
store_sustain(struct device
*dev
,
155 struct device_attribute
*attr
, const char *buf
, size_t count
)
157 struct hid_device
*hdev
= container_of(dev
, struct hid_device
, dev
);
158 struct pk_device
*pk
= hid_get_drvdata(hdev
);
160 unsigned sustain
= 0;
162 if (sscanf(buf
, "%u", &sustain
) > 0 && sustain
<= PCMIDI_SUSTAIN_MAX
) {
163 dbg_hid("pcmidi sysfs write sustain=%u\n", sustain
);
164 pk
->pm
->midi_sustain
= sustain
;
165 pk
->pm
->midi_sustain_mode
=
166 (0 == sustain
|| !pk
->pm
->midi_mode
) ? 0 : 1;
172 static DEVICE_ATTR(sustain
, S_IRUGO
| S_IWUSR
| S_IWGRP
, show_sustain
,
175 static struct device_attribute
*sysfs_device_attr_sustain
= {
179 /* Output routine for the sysfs octave file */
180 static ssize_t
show_octave(struct device
*dev
,
181 struct device_attribute
*attr
, char *buf
)
183 struct hid_device
*hdev
= container_of(dev
, struct hid_device
, dev
);
184 struct pk_device
*pk
= hid_get_drvdata(hdev
);
186 dbg_hid("pcmidi sysfs read octave=%d\n", pk
->pm
->midi_octave
);
188 return sprintf(buf
, "%d (min:%d, max:%d)\n", pk
->pm
->midi_octave
,
189 PCMIDI_OCTAVE_MIN
, PCMIDI_OCTAVE_MAX
);
192 /* Input routine for the sysfs octave file */
193 static ssize_t
store_octave(struct device
*dev
,
194 struct device_attribute
*attr
, const char *buf
, size_t count
)
196 struct hid_device
*hdev
= container_of(dev
, struct hid_device
, dev
);
197 struct pk_device
*pk
= hid_get_drvdata(hdev
);
201 if (sscanf(buf
, "%d", &octave
) > 0 &&
202 octave
>= PCMIDI_OCTAVE_MIN
&& octave
<= PCMIDI_OCTAVE_MAX
) {
203 dbg_hid("pcmidi sysfs write octave=%d\n", octave
);
204 pk
->pm
->midi_octave
= octave
;
210 static DEVICE_ATTR(octave
, S_IRUGO
| S_IWUSR
| S_IWGRP
, show_octave
,
213 static struct device_attribute
*sysfs_device_attr_octave
= {
218 static void pcmidi_send_note(struct pcmidi_snd
*pm
,
219 unsigned char status
, unsigned char note
, unsigned char velocity
)
222 unsigned char buffer
[3];
226 buffer
[2] = velocity
;
228 spin_lock_irqsave(&pm
->rawmidi_in_lock
, flags
);
230 if (!pm
->in_substream
)
232 if (!test_bit(pm
->in_substream
->number
, &pm
->in_triggered
))
235 snd_rawmidi_receive(pm
->in_substream
, buffer
, 3);
238 spin_unlock_irqrestore(&pm
->rawmidi_in_lock
, flags
);
243 static void pcmidi_sustained_note_release(unsigned long data
)
245 struct pcmidi_sustain
*pms
= (struct pcmidi_sustain
*)data
;
247 pcmidi_send_note(pms
->pm
, pms
->status
, pms
->note
, pms
->velocity
);
251 static void init_sustain_timers(struct pcmidi_snd
*pm
)
253 struct pcmidi_sustain
*pms
;
256 for (i
= 0; i
< PCMIDI_SUSTAINED_MAX
; i
++) {
257 pms
= &pm
->sustained_notes
[i
];
260 setup_timer(&pms
->timer
, pcmidi_sustained_note_release
,
265 static void stop_sustain_timers(struct pcmidi_snd
*pm
)
267 struct pcmidi_sustain
*pms
;
270 for (i
= 0; i
< PCMIDI_SUSTAINED_MAX
; i
++) {
271 pms
= &pm
->sustained_notes
[i
];
273 del_timer_sync(&pms
->timer
);
277 static int pcmidi_get_output_report(struct pcmidi_snd
*pm
)
279 struct hid_device
*hdev
= pm
->pk
->hdev
;
280 struct hid_report
*report
;
282 list_for_each_entry(report
,
283 &hdev
->report_enum
[HID_OUTPUT_REPORT
].report_list
, list
) {
284 if (!(6 == report
->id
))
287 if (report
->maxfield
< 1) {
288 hid_err(hdev
, "output report is empty\n");
291 if (report
->field
[0]->report_count
!= 2) {
292 hid_err(hdev
, "field count too low\n");
295 pm
->pcmidi_report6
= report
;
298 /* should never get here */
302 static void pcmidi_submit_output_report(struct pcmidi_snd
*pm
, int state
)
304 struct hid_device
*hdev
= pm
->pk
->hdev
;
305 struct hid_report
*report
= pm
->pcmidi_report6
;
306 report
->field
[0]->value
[0] = 0x01;
307 report
->field
[0]->value
[1] = state
;
309 usbhid_submit_report(hdev
, report
, USB_DIR_OUT
);
312 static int pcmidi_handle_report1(struct pcmidi_snd
*pm
, u8
*data
)
317 bit_mask
= (bit_mask
<< 8) | data
[2];
318 bit_mask
= (bit_mask
<< 8) | data
[3];
320 dbg_hid("pcmidi mode: %d\n", pm
->midi_mode
);
322 /*KEY_MAIL or octave down*/
323 if (pm
->midi_mode
&& bit_mask
== 0x004000) {
326 if (pm
->midi_octave
< -2)
327 pm
->midi_octave
= -2;
328 dbg_hid("pcmidi mode: %d octave: %d\n",
329 pm
->midi_mode
, pm
->midi_octave
);
332 /*KEY_WWW or sustain*/
333 else if (pm
->midi_mode
&& bit_mask
== 0x000004) {
335 pm
->midi_sustain_mode
^= 0x1;
339 return 0; /* continue key processing */
342 static int pcmidi_handle_report3(struct pcmidi_snd
*pm
, u8
*data
, int size
)
344 struct pcmidi_sustain
*pms
;
346 unsigned char status
, note
, velocity
;
348 unsigned num_notes
= (size
-1)/2;
349 for (j
= 0; j
< num_notes
; j
++) {
351 velocity
= data
[j
*2+2];
353 if (note
< 0x81) { /* note on */
354 status
= 128 + 16 + pm
->midi_channel
; /* 1001nnnn */
355 note
= note
- 0x54 + PCMIDI_MIDDLE_C
+
356 (pm
->midi_octave
* 12);
358 velocity
= 1; /* force note on */
359 } else { /* note off */
360 status
= 128 + pm
->midi_channel
; /* 1000nnnn */
361 note
= note
- 0x94 + PCMIDI_MIDDLE_C
+
362 (pm
->midi_octave
*12);
364 if (pm
->midi_sustain_mode
) {
365 for (i
= 0; i
< PCMIDI_SUSTAINED_MAX
; i
++) {
366 pms
= &pm
->sustained_notes
[i
];
368 pms
->status
= status
;
370 pms
->velocity
= velocity
;
373 mod_timer(&pms
->timer
,
375 msecs_to_jiffies(pm
->midi_sustain
));
381 pcmidi_send_note(pm
, status
, note
, velocity
);
387 static int pcmidi_handle_report4(struct pcmidi_snd
*pm
, u8
*data
)
394 bit_mask
= (bit_mask
<< 8) | data
[2];
395 bit_mask
= (bit_mask
<< 8) | data
[3];
398 for (bit_index
= 0; bit_index
< 24; bit_index
++) {
399 key
= pm
->last_key
[bit_index
];
400 if (!((0x01 << bit_index
) & bit_mask
)) {
401 input_event(pm
->input_ep82
, EV_KEY
,
402 pm
->last_key
[bit_index
], 0);
403 pm
->last_key
[bit_index
] = 0;
408 for (bit_index
= 0; bit_index
< 24; bit_index
++) {
410 switch ((0x01 << bit_index
) & bit_mask
) {
411 case 0x000010: /* Fn lock*/
412 pm
->fn_state
^= 0x000010;
414 pcmidi_submit_output_report(pm
, 0xc5);
416 pcmidi_submit_output_report(pm
, 0xc6);
418 case 0x020000: /* midi launcher..send a key (qwerty) or not? */
419 pcmidi_submit_output_report(pm
, 0xc1);
420 pm
->midi_mode
^= 0x01;
422 dbg_hid("pcmidi mode: %d\n", pm
->midi_mode
);
424 case 0x100000: /* KEY_MESSENGER or octave up */
425 dbg_hid("pcmidi mode: %d\n", pm
->midi_mode
);
428 if (pm
->midi_octave
> 2)
430 dbg_hid("pcmidi mode: %d octave: %d\n",
431 pm
->midi_mode
, pm
->midi_octave
);
440 key
= KEY_ADDRESSBOOK
;
446 key
= KEY_WORDPROCESSOR
;
449 key
= KEY_SPREADSHEET
;
464 key
= KEY_FORWARDMAIL
;
485 key
= KEY_SPELLCHECK
;
492 input_event(pm
->input_ep82
, EV_KEY
, key
, 1);
493 pm
->last_key
[bit_index
] = key
;
500 static int pcmidi_handle_report(
501 struct pcmidi_snd
*pm
, unsigned report_id
, u8
*data
, int size
)
506 case 0x01: /* midi keys (qwerty)*/
507 ret
= pcmidi_handle_report1(pm
, data
);
509 case 0x03: /* midi keyboard (musical)*/
510 ret
= pcmidi_handle_report3(pm
, data
, size
);
512 case 0x04: /* multimedia/midi keys (qwerty)*/
513 ret
= pcmidi_handle_report4(pm
, data
);
519 static void pcmidi_setup_extra_keys(
520 struct pcmidi_snd
*pm
, struct input_dev
*input
)
522 /* reassigned functionality for N/A keys
523 MY PICTURES => KEY_WORDPROCESSOR
524 MY MUSIC=> KEY_SPREADSHEET
526 unsigned int keys
[] = {
528 KEY_MESSENGER
, KEY_CALENDAR
,
529 KEY_ADDRESSBOOK
, KEY_DOCUMENTS
,
534 KEY_REPLY
, KEY_FORWARDMAIL
,
538 KEY_SPELLCHECK
, KEY_PRINT
,
542 unsigned int *pkeys
= &keys
[0];
545 if (pm
->ifnum
!= 1) /* only set up ONCE for interace 1 */
548 pm
->input_ep82
= input
;
550 for (i
= 0; i
< 24; i
++)
553 while (*pkeys
!= 0) {
554 set_bit(*pkeys
, pm
->input_ep82
->keybit
);
559 static int pcmidi_set_operational(struct pcmidi_snd
*pm
)
562 return 0; /* only set up ONCE for interace 1 */
564 pcmidi_get_output_report(pm
);
565 pcmidi_submit_output_report(pm
, 0xc1);
569 static int pcmidi_snd_free(struct snd_device
*dev
)
574 static int pcmidi_in_open(struct snd_rawmidi_substream
*substream
)
576 struct pcmidi_snd
*pm
= substream
->rmidi
->private_data
;
578 dbg_hid("pcmidi in open\n");
579 pm
->in_substream
= substream
;
583 static int pcmidi_in_close(struct snd_rawmidi_substream
*substream
)
585 dbg_hid("pcmidi in close\n");
589 static void pcmidi_in_trigger(struct snd_rawmidi_substream
*substream
, int up
)
591 struct pcmidi_snd
*pm
= substream
->rmidi
->private_data
;
593 dbg_hid("pcmidi in trigger %d\n", up
);
595 pm
->in_triggered
= up
;
598 static struct snd_rawmidi_ops pcmidi_in_ops
= {
599 .open
= pcmidi_in_open
,
600 .close
= pcmidi_in_close
,
601 .trigger
= pcmidi_in_trigger
604 static int pcmidi_snd_initialise(struct pcmidi_snd
*pm
)
607 struct snd_card
*card
;
608 struct snd_rawmidi
*rwmidi
;
611 static struct snd_device_ops ops
= {
612 .dev_free
= pcmidi_snd_free
,
616 return 0; /* only set up midi device ONCE for interace 1 */
618 if (dev
>= SNDRV_CARDS
)
626 /* Setup sound card */
628 err
= snd_card_create(index
[dev
], id
[dev
], THIS_MODULE
, 0, &card
);
630 pk_error("failed to create pc-midi sound card\n");
636 /* Setup sound device */
637 err
= snd_device_new(card
, SNDRV_DEV_LOWLEVEL
, pm
, &ops
);
639 pk_error("failed to create pc-midi sound device: error %d\n",
644 strncpy(card
->driver
, shortname
, sizeof(card
->driver
));
645 strncpy(card
->shortname
, shortname
, sizeof(card
->shortname
));
646 strncpy(card
->longname
, longname
, sizeof(card
->longname
));
649 err
= snd_rawmidi_new(card
, card
->shortname
, 0,
652 pk_error("failed to create pc-midi rawmidi device: error %d\n",
657 strncpy(rwmidi
->name
, card
->shortname
, sizeof(rwmidi
->name
));
658 rwmidi
->info_flags
= SNDRV_RAWMIDI_INFO_INPUT
;
659 rwmidi
->private_data
= pm
;
661 snd_rawmidi_set_ops(rwmidi
, SNDRV_RAWMIDI_STREAM_INPUT
,
664 snd_card_set_dev(card
, &pm
->pk
->hdev
->dev
);
666 /* create sysfs variables */
667 err
= device_create_file(&pm
->pk
->hdev
->dev
,
668 sysfs_device_attr_channel
);
670 pk_error("failed to create sysfs attribute channel: error %d\n",
675 err
= device_create_file(&pm
->pk
->hdev
->dev
,
676 sysfs_device_attr_sustain
);
678 pk_error("failed to create sysfs attribute sustain: error %d\n",
680 goto fail_attr_sustain
;
683 err
= device_create_file(&pm
->pk
->hdev
->dev
,
684 sysfs_device_attr_octave
);
686 pk_error("failed to create sysfs attribute octave: error %d\n",
688 goto fail_attr_octave
;
691 spin_lock_init(&pm
->rawmidi_in_lock
);
693 init_sustain_timers(pm
);
694 pcmidi_set_operational(pm
);
697 err
= snd_card_register(card
);
699 pk_error("failed to register pc-midi sound card: error %d\n",
704 dbg_hid("pcmidi_snd_initialise finished ok\n");
708 stop_sustain_timers(pm
);
709 device_remove_file(&pm
->pk
->hdev
->dev
, sysfs_device_attr_octave
);
711 device_remove_file(&pm
->pk
->hdev
->dev
, sysfs_device_attr_sustain
);
713 device_remove_file(&pm
->pk
->hdev
->dev
, sysfs_device_attr_channel
);
716 snd_card_free(pm
->card
);
722 static int pcmidi_snd_terminate(struct pcmidi_snd
*pm
)
725 stop_sustain_timers(pm
);
727 device_remove_file(&pm
->pk
->hdev
->dev
,
728 sysfs_device_attr_channel
);
729 device_remove_file(&pm
->pk
->hdev
->dev
,
730 sysfs_device_attr_sustain
);
731 device_remove_file(&pm
->pk
->hdev
->dev
,
732 sysfs_device_attr_octave
);
734 snd_card_disconnect(pm
->card
);
735 snd_card_free_when_closed(pm
->card
);
742 * PC-MIDI report descriptor for report id is wrong.
744 static __u8
*pk_report_fixup(struct hid_device
*hdev
, __u8
*rdesc
,
748 rdesc
[111] == 0x06 && rdesc
[112] == 0x00 &&
749 rdesc
[113] == 0xff) {
751 "fixing up pc-midi keyboard report descriptor\n");
753 rdesc
[144] = 0x18; /* report 4: was 0x10 report count */
758 static int pk_input_mapping(struct hid_device
*hdev
, struct hid_input
*hi
,
759 struct hid_field
*field
, struct hid_usage
*usage
,
760 unsigned long **bit
, int *max
)
762 struct pk_device
*pk
= hid_get_drvdata(hdev
);
763 struct pcmidi_snd
*pm
;
767 if (HID_UP_MSVENDOR
== (usage
->hid
& HID_USAGE_PAGE
) &&
769 pcmidi_setup_extra_keys(pm
, hi
->input
);
777 static int pk_raw_event(struct hid_device
*hdev
, struct hid_report
*report
,
780 struct pk_device
*pk
= hid_get_drvdata(hdev
);
783 if (1 == pk
->pm
->ifnum
) {
784 if (report
->id
== data
[0])
785 switch (report
->id
) {
786 case 0x01: /* midi keys (qwerty)*/
787 case 0x03: /* midi keyboard (musical)*/
788 case 0x04: /* extra/midi keys (qwerty)*/
789 ret
= pcmidi_handle_report(pk
->pm
,
790 report
->id
, data
, size
);
798 static int pk_probe(struct hid_device
*hdev
, const struct hid_device_id
*id
)
801 struct usb_interface
*intf
= to_usb_interface(hdev
->dev
.parent
);
802 unsigned short ifnum
= intf
->cur_altsetting
->desc
.bInterfaceNumber
;
803 unsigned long quirks
= id
->driver_data
;
804 struct pk_device
*pk
;
805 struct pcmidi_snd
*pm
= NULL
;
807 pk
= kzalloc(sizeof(*pk
), GFP_KERNEL
);
809 hid_err(hdev
, "can't alloc descriptor\n");
815 pm
= kzalloc(sizeof(*pm
), GFP_KERNEL
);
817 hid_err(hdev
, "can't alloc descriptor\n");
826 hid_set_drvdata(hdev
, pk
);
828 ret
= hid_parse(hdev
);
830 hid_err(hdev
, "hid parse failed\n");
834 if (quirks
& PK_QUIRK_NOGET
) { /* hid_parse cleared all the quirks */
835 hdev
->quirks
|= HID_QUIRK_NOGET
;
838 ret
= hid_hw_start(hdev
, HID_CONNECT_DEFAULT
);
840 hid_err(hdev
, "hw start failed\n");
844 ret
= pcmidi_snd_initialise(pm
);
859 static void pk_remove(struct hid_device
*hdev
)
861 struct pk_device
*pk
= hid_get_drvdata(hdev
);
862 struct pcmidi_snd
*pm
;
866 pcmidi_snd_terminate(pm
);
875 static const struct hid_device_id pk_devices
[] = {
876 {HID_USB_DEVICE(USB_VENDOR_ID_CREATIVELABS
,
877 USB_DEVICE_ID_PRODIKEYS_PCMIDI
),
878 .driver_data
= PK_QUIRK_NOGET
},
881 MODULE_DEVICE_TABLE(hid
, pk_devices
);
883 static struct hid_driver pk_driver
= {
885 .id_table
= pk_devices
,
886 .report_fixup
= pk_report_fixup
,
887 .input_mapping
= pk_input_mapping
,
888 .raw_event
= pk_raw_event
,
892 module_hid_driver(pk_driver
);
894 MODULE_LICENSE("GPL");