1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * HID driver for the Prodikeys PC-MIDI Keyboard
4 * providing midi & extra multimedia keys functionality
6 * Copyright (c) 2009 Don Prince <dhprince.devel@yahoo.co.uk>
8 * Controls for Octave Shift Up/Down, Channel, and
9 * Sustain Duration available via sysfs.
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 #include <linux/device.h>
18 #include <linux/module.h>
19 #include <linux/usb.h>
20 #include <linux/mutex.h>
21 #include <linux/hid.h>
22 #include <sound/core.h>
23 #include <sound/initval.h>
24 #include <sound/rawmidi.h>
28 #define pk_debug(format, arg...) \
29 pr_debug("hid-prodikeys: " format "\n" , ## arg)
30 #define pk_error(format, arg...) \
31 pr_err("hid-prodikeys: " format "\n" , ## arg)
38 struct hid_device
*hdev
;
39 struct pcmidi_snd
*pm
; /* pcmidi device context */
42 struct pcmidi_sustain
{
44 struct pcmidi_snd
*pm
;
45 struct timer_list timer
;
48 unsigned char velocity
;
51 #define PCMIDI_SUSTAINED_MAX 32
55 struct hid_report
*pcmidi_report6
;
56 struct input_dev
*input_ep82
;
57 unsigned short midi_mode
;
58 unsigned short midi_sustain_mode
;
59 unsigned short midi_sustain
;
60 unsigned short midi_channel
;
62 struct pcmidi_sustain sustained_notes
[PCMIDI_SUSTAINED_MAX
];
63 unsigned short fn_state
;
64 unsigned short last_key
[24];
65 spinlock_t rawmidi_in_lock
;
66 struct snd_card
*card
;
67 struct snd_rawmidi
*rwmidi
;
68 struct snd_rawmidi_substream
*in_substream
;
69 struct snd_rawmidi_substream
*out_substream
;
70 unsigned long in_triggered
;
71 unsigned long out_active
;
74 #define PK_QUIRK_NOGET 0x00010000
75 #define PCMIDI_MIDDLE_C 60
76 #define PCMIDI_CHANNEL_MIN 0
77 #define PCMIDI_CHANNEL_MAX 15
78 #define PCMIDI_OCTAVE_MIN (-2)
79 #define PCMIDI_OCTAVE_MAX 2
80 #define PCMIDI_SUSTAIN_MIN 0
81 #define PCMIDI_SUSTAIN_MAX 5000
83 static const char shortname
[] = "PC-MIDI";
84 static const char longname
[] = "Prodikeys PC-MIDI Keyboard";
86 static int index
[SNDRV_CARDS
] = SNDRV_DEFAULT_IDX
;
87 static char *id
[SNDRV_CARDS
] = SNDRV_DEFAULT_STR
;
88 static bool enable
[SNDRV_CARDS
] = SNDRV_DEFAULT_ENABLE_PNP
;
90 module_param_array(index
, int, NULL
, 0444);
91 module_param_array(id
, charp
, NULL
, 0444);
92 module_param_array(enable
, bool, NULL
, 0444);
93 MODULE_PARM_DESC(index
, "Index value for the PC-MIDI virtual audio driver");
94 MODULE_PARM_DESC(id
, "ID string for the PC-MIDI virtual audio driver");
95 MODULE_PARM_DESC(enable
, "Enable for the PC-MIDI virtual audio driver");
98 /* Output routine for the sysfs channel file */
99 static ssize_t
show_channel(struct device
*dev
,
100 struct device_attribute
*attr
, char *buf
)
102 struct hid_device
*hdev
= to_hid_device(dev
);
103 struct pk_device
*pk
= hid_get_drvdata(hdev
);
105 dbg_hid("pcmidi sysfs read channel=%u\n", pk
->pm
->midi_channel
);
107 return sprintf(buf
, "%u (min:%u, max:%u)\n", pk
->pm
->midi_channel
,
108 PCMIDI_CHANNEL_MIN
, PCMIDI_CHANNEL_MAX
);
111 /* Input routine for the sysfs channel file */
112 static ssize_t
store_channel(struct device
*dev
,
113 struct device_attribute
*attr
, const char *buf
, size_t count
)
115 struct hid_device
*hdev
= to_hid_device(dev
);
116 struct pk_device
*pk
= hid_get_drvdata(hdev
);
118 unsigned channel
= 0;
120 if (sscanf(buf
, "%u", &channel
) > 0 && channel
<= PCMIDI_CHANNEL_MAX
) {
121 dbg_hid("pcmidi sysfs write channel=%u\n", channel
);
122 pk
->pm
->midi_channel
= channel
;
128 static DEVICE_ATTR(channel
, S_IRUGO
| S_IWUSR
| S_IWGRP
, show_channel
,
131 static struct device_attribute
*sysfs_device_attr_channel
= {
135 /* Output routine for the sysfs sustain file */
136 static ssize_t
show_sustain(struct device
*dev
,
137 struct device_attribute
*attr
, char *buf
)
139 struct hid_device
*hdev
= to_hid_device(dev
);
140 struct pk_device
*pk
= hid_get_drvdata(hdev
);
142 dbg_hid("pcmidi sysfs read sustain=%u\n", pk
->pm
->midi_sustain
);
144 return sprintf(buf
, "%u (off:%u, max:%u (ms))\n", pk
->pm
->midi_sustain
,
145 PCMIDI_SUSTAIN_MIN
, PCMIDI_SUSTAIN_MAX
);
148 /* Input routine for the sysfs sustain file */
149 static ssize_t
store_sustain(struct device
*dev
,
150 struct device_attribute
*attr
, const char *buf
, size_t count
)
152 struct hid_device
*hdev
= to_hid_device(dev
);
153 struct pk_device
*pk
= hid_get_drvdata(hdev
);
155 unsigned sustain
= 0;
157 if (sscanf(buf
, "%u", &sustain
) > 0 && sustain
<= PCMIDI_SUSTAIN_MAX
) {
158 dbg_hid("pcmidi sysfs write sustain=%u\n", sustain
);
159 pk
->pm
->midi_sustain
= sustain
;
160 pk
->pm
->midi_sustain_mode
=
161 (0 == sustain
|| !pk
->pm
->midi_mode
) ? 0 : 1;
167 static DEVICE_ATTR(sustain
, S_IRUGO
| S_IWUSR
| S_IWGRP
, show_sustain
,
170 static struct device_attribute
*sysfs_device_attr_sustain
= {
174 /* Output routine for the sysfs octave file */
175 static ssize_t
show_octave(struct device
*dev
,
176 struct device_attribute
*attr
, char *buf
)
178 struct hid_device
*hdev
= to_hid_device(dev
);
179 struct pk_device
*pk
= hid_get_drvdata(hdev
);
181 dbg_hid("pcmidi sysfs read octave=%d\n", pk
->pm
->midi_octave
);
183 return sprintf(buf
, "%d (min:%d, max:%d)\n", pk
->pm
->midi_octave
,
184 PCMIDI_OCTAVE_MIN
, PCMIDI_OCTAVE_MAX
);
187 /* Input routine for the sysfs octave file */
188 static ssize_t
store_octave(struct device
*dev
,
189 struct device_attribute
*attr
, const char *buf
, size_t count
)
191 struct hid_device
*hdev
= to_hid_device(dev
);
192 struct pk_device
*pk
= hid_get_drvdata(hdev
);
196 if (sscanf(buf
, "%d", &octave
) > 0 &&
197 octave
>= PCMIDI_OCTAVE_MIN
&& octave
<= PCMIDI_OCTAVE_MAX
) {
198 dbg_hid("pcmidi sysfs write octave=%d\n", octave
);
199 pk
->pm
->midi_octave
= octave
;
205 static DEVICE_ATTR(octave
, S_IRUGO
| S_IWUSR
| S_IWGRP
, show_octave
,
208 static struct device_attribute
*sysfs_device_attr_octave
= {
213 static void pcmidi_send_note(struct pcmidi_snd
*pm
,
214 unsigned char status
, unsigned char note
, unsigned char velocity
)
217 unsigned char buffer
[3];
221 buffer
[2] = velocity
;
223 spin_lock_irqsave(&pm
->rawmidi_in_lock
, flags
);
225 if (!pm
->in_substream
)
227 if (!test_bit(pm
->in_substream
->number
, &pm
->in_triggered
))
230 snd_rawmidi_receive(pm
->in_substream
, buffer
, 3);
233 spin_unlock_irqrestore(&pm
->rawmidi_in_lock
, flags
);
238 static void pcmidi_sustained_note_release(struct timer_list
*t
)
240 struct pcmidi_sustain
*pms
= from_timer(pms
, t
, timer
);
242 pcmidi_send_note(pms
->pm
, pms
->status
, pms
->note
, pms
->velocity
);
246 static void init_sustain_timers(struct pcmidi_snd
*pm
)
248 struct pcmidi_sustain
*pms
;
251 for (i
= 0; i
< PCMIDI_SUSTAINED_MAX
; i
++) {
252 pms
= &pm
->sustained_notes
[i
];
255 timer_setup(&pms
->timer
, pcmidi_sustained_note_release
, 0);
259 static void stop_sustain_timers(struct pcmidi_snd
*pm
)
261 struct pcmidi_sustain
*pms
;
264 for (i
= 0; i
< PCMIDI_SUSTAINED_MAX
; i
++) {
265 pms
= &pm
->sustained_notes
[i
];
267 del_timer_sync(&pms
->timer
);
271 static int pcmidi_get_output_report(struct pcmidi_snd
*pm
)
273 struct hid_device
*hdev
= pm
->pk
->hdev
;
274 struct hid_report
*report
;
276 list_for_each_entry(report
,
277 &hdev
->report_enum
[HID_OUTPUT_REPORT
].report_list
, list
) {
278 if (!(6 == report
->id
))
281 if (report
->maxfield
< 1) {
282 hid_err(hdev
, "output report is empty\n");
285 if (report
->field
[0]->report_count
!= 2) {
286 hid_err(hdev
, "field count too low\n");
289 pm
->pcmidi_report6
= report
;
292 /* should never get here */
296 static void pcmidi_submit_output_report(struct pcmidi_snd
*pm
, int state
)
298 struct hid_device
*hdev
= pm
->pk
->hdev
;
299 struct hid_report
*report
= pm
->pcmidi_report6
;
300 report
->field
[0]->value
[0] = 0x01;
301 report
->field
[0]->value
[1] = state
;
303 hid_hw_request(hdev
, report
, HID_REQ_SET_REPORT
);
306 static int pcmidi_handle_report1(struct pcmidi_snd
*pm
, u8
*data
)
311 bit_mask
= (bit_mask
<< 8) | data
[2];
312 bit_mask
= (bit_mask
<< 8) | data
[3];
314 dbg_hid("pcmidi mode: %d\n", pm
->midi_mode
);
316 /*KEY_MAIL or octave down*/
317 if (pm
->midi_mode
&& bit_mask
== 0x004000) {
320 if (pm
->midi_octave
< -2)
321 pm
->midi_octave
= -2;
322 dbg_hid("pcmidi mode: %d octave: %d\n",
323 pm
->midi_mode
, pm
->midi_octave
);
326 /*KEY_WWW or sustain*/
327 else if (pm
->midi_mode
&& bit_mask
== 0x000004) {
329 pm
->midi_sustain_mode
^= 0x1;
333 return 0; /* continue key processing */
336 static int pcmidi_handle_report3(struct pcmidi_snd
*pm
, u8
*data
, int size
)
338 struct pcmidi_sustain
*pms
;
340 unsigned char status
, note
, velocity
;
342 unsigned num_notes
= (size
-1)/2;
343 for (j
= 0; j
< num_notes
; j
++) {
345 velocity
= data
[j
*2+2];
347 if (note
< 0x81) { /* note on */
348 status
= 128 + 16 + pm
->midi_channel
; /* 1001nnnn */
349 note
= note
- 0x54 + PCMIDI_MIDDLE_C
+
350 (pm
->midi_octave
* 12);
352 velocity
= 1; /* force note on */
353 } else { /* note off */
354 status
= 128 + pm
->midi_channel
; /* 1000nnnn */
355 note
= note
- 0x94 + PCMIDI_MIDDLE_C
+
356 (pm
->midi_octave
*12);
358 if (pm
->midi_sustain_mode
) {
359 for (i
= 0; i
< PCMIDI_SUSTAINED_MAX
; i
++) {
360 pms
= &pm
->sustained_notes
[i
];
362 pms
->status
= status
;
364 pms
->velocity
= velocity
;
367 mod_timer(&pms
->timer
,
369 msecs_to_jiffies(pm
->midi_sustain
));
375 pcmidi_send_note(pm
, status
, note
, velocity
);
381 static int pcmidi_handle_report4(struct pcmidi_snd
*pm
, u8
*data
)
388 bit_mask
= (bit_mask
<< 8) | data
[2];
389 bit_mask
= (bit_mask
<< 8) | data
[3];
392 for (bit_index
= 0; bit_index
< 24; bit_index
++) {
393 if (!((0x01 << bit_index
) & bit_mask
)) {
394 input_event(pm
->input_ep82
, EV_KEY
,
395 pm
->last_key
[bit_index
], 0);
396 pm
->last_key
[bit_index
] = 0;
401 for (bit_index
= 0; bit_index
< 24; bit_index
++) {
403 switch ((0x01 << bit_index
) & bit_mask
) {
404 case 0x000010: /* Fn lock*/
405 pm
->fn_state
^= 0x000010;
407 pcmidi_submit_output_report(pm
, 0xc5);
409 pcmidi_submit_output_report(pm
, 0xc6);
411 case 0x020000: /* midi launcher..send a key (qwerty) or not? */
412 pcmidi_submit_output_report(pm
, 0xc1);
413 pm
->midi_mode
^= 0x01;
415 dbg_hid("pcmidi mode: %d\n", pm
->midi_mode
);
417 case 0x100000: /* KEY_MESSENGER or octave up */
418 dbg_hid("pcmidi mode: %d\n", pm
->midi_mode
);
421 if (pm
->midi_octave
> 2)
423 dbg_hid("pcmidi mode: %d octave: %d\n",
424 pm
->midi_mode
, pm
->midi_octave
);
433 key
= KEY_ADDRESSBOOK
;
439 key
= KEY_WORDPROCESSOR
;
442 key
= KEY_SPREADSHEET
;
457 key
= KEY_FORWARDMAIL
;
478 key
= KEY_SPELLCHECK
;
485 input_event(pm
->input_ep82
, EV_KEY
, key
, 1);
486 pm
->last_key
[bit_index
] = key
;
493 static int pcmidi_handle_report(
494 struct pcmidi_snd
*pm
, unsigned report_id
, u8
*data
, int size
)
499 case 0x01: /* midi keys (qwerty)*/
500 ret
= pcmidi_handle_report1(pm
, data
);
502 case 0x03: /* midi keyboard (musical)*/
503 ret
= pcmidi_handle_report3(pm
, data
, size
);
505 case 0x04: /* multimedia/midi keys (qwerty)*/
506 ret
= pcmidi_handle_report4(pm
, data
);
512 static void pcmidi_setup_extra_keys(
513 struct pcmidi_snd
*pm
, struct input_dev
*input
)
515 /* reassigned functionality for N/A keys
516 MY PICTURES => KEY_WORDPROCESSOR
517 MY MUSIC=> KEY_SPREADSHEET
519 unsigned int keys
[] = {
521 KEY_MESSENGER
, KEY_CALENDAR
,
522 KEY_ADDRESSBOOK
, KEY_DOCUMENTS
,
527 KEY_REPLY
, KEY_FORWARDMAIL
,
531 KEY_SPELLCHECK
, KEY_PRINT
,
535 unsigned int *pkeys
= &keys
[0];
538 if (pm
->ifnum
!= 1) /* only set up ONCE for interace 1 */
541 pm
->input_ep82
= input
;
543 for (i
= 0; i
< 24; i
++)
546 while (*pkeys
!= 0) {
547 set_bit(*pkeys
, pm
->input_ep82
->keybit
);
552 static int pcmidi_set_operational(struct pcmidi_snd
*pm
)
555 return 0; /* only set up ONCE for interace 1 */
557 pcmidi_get_output_report(pm
);
558 pcmidi_submit_output_report(pm
, 0xc1);
562 static int pcmidi_snd_free(struct snd_device
*dev
)
567 static int pcmidi_in_open(struct snd_rawmidi_substream
*substream
)
569 struct pcmidi_snd
*pm
= substream
->rmidi
->private_data
;
571 dbg_hid("pcmidi in open\n");
572 pm
->in_substream
= substream
;
576 static int pcmidi_in_close(struct snd_rawmidi_substream
*substream
)
578 dbg_hid("pcmidi in close\n");
582 static void pcmidi_in_trigger(struct snd_rawmidi_substream
*substream
, int up
)
584 struct pcmidi_snd
*pm
= substream
->rmidi
->private_data
;
586 dbg_hid("pcmidi in trigger %d\n", up
);
588 pm
->in_triggered
= up
;
591 static const struct snd_rawmidi_ops pcmidi_in_ops
= {
592 .open
= pcmidi_in_open
,
593 .close
= pcmidi_in_close
,
594 .trigger
= pcmidi_in_trigger
597 static int pcmidi_snd_initialise(struct pcmidi_snd
*pm
)
600 struct snd_card
*card
;
601 struct snd_rawmidi
*rwmidi
;
604 static struct snd_device_ops ops
= {
605 .dev_free
= pcmidi_snd_free
,
609 return 0; /* only set up midi device ONCE for interace 1 */
611 if (dev
>= SNDRV_CARDS
)
619 /* Setup sound card */
621 err
= snd_card_new(&pm
->pk
->hdev
->dev
, index
[dev
], id
[dev
],
622 THIS_MODULE
, 0, &card
);
624 pk_error("failed to create pc-midi sound card\n");
630 /* Setup sound device */
631 err
= snd_device_new(card
, SNDRV_DEV_LOWLEVEL
, pm
, &ops
);
633 pk_error("failed to create pc-midi sound device: error %d\n",
638 strncpy(card
->driver
, shortname
, sizeof(card
->driver
));
639 strncpy(card
->shortname
, shortname
, sizeof(card
->shortname
));
640 strncpy(card
->longname
, longname
, sizeof(card
->longname
));
643 err
= snd_rawmidi_new(card
, card
->shortname
, 0,
646 pk_error("failed to create pc-midi rawmidi device: error %d\n",
651 strncpy(rwmidi
->name
, card
->shortname
, sizeof(rwmidi
->name
));
652 rwmidi
->info_flags
= SNDRV_RAWMIDI_INFO_INPUT
;
653 rwmidi
->private_data
= pm
;
655 snd_rawmidi_set_ops(rwmidi
, SNDRV_RAWMIDI_STREAM_INPUT
,
658 /* create sysfs variables */
659 err
= device_create_file(&pm
->pk
->hdev
->dev
,
660 sysfs_device_attr_channel
);
662 pk_error("failed to create sysfs attribute channel: error %d\n",
667 err
= device_create_file(&pm
->pk
->hdev
->dev
,
668 sysfs_device_attr_sustain
);
670 pk_error("failed to create sysfs attribute sustain: error %d\n",
672 goto fail_attr_sustain
;
675 err
= device_create_file(&pm
->pk
->hdev
->dev
,
676 sysfs_device_attr_octave
);
678 pk_error("failed to create sysfs attribute octave: error %d\n",
680 goto fail_attr_octave
;
683 spin_lock_init(&pm
->rawmidi_in_lock
);
685 init_sustain_timers(pm
);
686 pcmidi_set_operational(pm
);
689 err
= snd_card_register(card
);
691 pk_error("failed to register pc-midi sound card: error %d\n",
696 dbg_hid("pcmidi_snd_initialise finished ok\n");
700 stop_sustain_timers(pm
);
701 device_remove_file(&pm
->pk
->hdev
->dev
, sysfs_device_attr_octave
);
703 device_remove_file(&pm
->pk
->hdev
->dev
, sysfs_device_attr_sustain
);
705 device_remove_file(&pm
->pk
->hdev
->dev
, sysfs_device_attr_channel
);
708 snd_card_free(pm
->card
);
714 static int pcmidi_snd_terminate(struct pcmidi_snd
*pm
)
717 stop_sustain_timers(pm
);
719 device_remove_file(&pm
->pk
->hdev
->dev
,
720 sysfs_device_attr_channel
);
721 device_remove_file(&pm
->pk
->hdev
->dev
,
722 sysfs_device_attr_sustain
);
723 device_remove_file(&pm
->pk
->hdev
->dev
,
724 sysfs_device_attr_octave
);
726 snd_card_disconnect(pm
->card
);
727 snd_card_free_when_closed(pm
->card
);
734 * PC-MIDI report descriptor for report id is wrong.
736 static __u8
*pk_report_fixup(struct hid_device
*hdev
, __u8
*rdesc
,
740 rdesc
[111] == 0x06 && rdesc
[112] == 0x00 &&
741 rdesc
[113] == 0xff) {
743 "fixing up pc-midi keyboard report descriptor\n");
745 rdesc
[144] = 0x18; /* report 4: was 0x10 report count */
750 static int pk_input_mapping(struct hid_device
*hdev
, struct hid_input
*hi
,
751 struct hid_field
*field
, struct hid_usage
*usage
,
752 unsigned long **bit
, int *max
)
754 struct pk_device
*pk
= hid_get_drvdata(hdev
);
755 struct pcmidi_snd
*pm
;
759 if (HID_UP_MSVENDOR
== (usage
->hid
& HID_USAGE_PAGE
) &&
761 pcmidi_setup_extra_keys(pm
, hi
->input
);
769 static int pk_raw_event(struct hid_device
*hdev
, struct hid_report
*report
,
772 struct pk_device
*pk
= hid_get_drvdata(hdev
);
775 if (1 == pk
->pm
->ifnum
) {
776 if (report
->id
== data
[0])
777 switch (report
->id
) {
778 case 0x01: /* midi keys (qwerty)*/
779 case 0x03: /* midi keyboard (musical)*/
780 case 0x04: /* extra/midi keys (qwerty)*/
781 ret
= pcmidi_handle_report(pk
->pm
,
782 report
->id
, data
, size
);
790 static int pk_probe(struct hid_device
*hdev
, const struct hid_device_id
*id
)
793 struct usb_interface
*intf
= to_usb_interface(hdev
->dev
.parent
);
794 unsigned short ifnum
= intf
->cur_altsetting
->desc
.bInterfaceNumber
;
795 unsigned long quirks
= id
->driver_data
;
796 struct pk_device
*pk
;
797 struct pcmidi_snd
*pm
= NULL
;
799 pk
= kzalloc(sizeof(*pk
), GFP_KERNEL
);
801 hid_err(hdev
, "can't alloc descriptor\n");
807 pm
= kzalloc(sizeof(*pm
), GFP_KERNEL
);
809 hid_err(hdev
, "can't alloc descriptor\n");
818 hid_set_drvdata(hdev
, pk
);
820 ret
= hid_parse(hdev
);
822 hid_err(hdev
, "hid parse failed\n");
826 if (quirks
& PK_QUIRK_NOGET
) { /* hid_parse cleared all the quirks */
827 hdev
->quirks
|= HID_QUIRK_NOGET
;
830 ret
= hid_hw_start(hdev
, HID_CONNECT_DEFAULT
);
832 hid_err(hdev
, "hw start failed\n");
836 ret
= pcmidi_snd_initialise(pm
);
851 static void pk_remove(struct hid_device
*hdev
)
853 struct pk_device
*pk
= hid_get_drvdata(hdev
);
854 struct pcmidi_snd
*pm
;
858 pcmidi_snd_terminate(pm
);
867 static const struct hid_device_id pk_devices
[] = {
868 {HID_USB_DEVICE(USB_VENDOR_ID_CREATIVELABS
,
869 USB_DEVICE_ID_PRODIKEYS_PCMIDI
),
870 .driver_data
= PK_QUIRK_NOGET
},
873 MODULE_DEVICE_TABLE(hid
, pk_devices
);
875 static struct hid_driver pk_driver
= {
877 .id_table
= pk_devices
,
878 .report_fixup
= pk_report_fixup
,
879 .input_mapping
= pk_input_mapping
,
880 .raw_event
= pk_raw_event
,
884 module_hid_driver(pk_driver
);
886 MODULE_LICENSE("GPL");