19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include <linux/device.h>
22 #include <linux/module.h>
25 #include <linux/hid.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)
56 #define PCMIDI_SUSTAINED_MAX 32
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";
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,
121 struct pk_device *pk = hid_get_drvdata(hdev);
126 dbg_hid(
"pcmidi sysfs write channel=%u\n", channel);
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,
158 struct pk_device *pk = hid_get_drvdata(hdev);
160 unsigned sustain = 0;
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;
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,
197 struct pk_device *pk = hid_get_drvdata(hdev);
201 if (
sscanf(buf,
"%d", &octave) > 0 &&
203 dbg_hid(
"pcmidi sysfs write octave=%d\n", octave);
204 pk->
pm->midi_octave = octave;
219 unsigned char status,
unsigned char note,
unsigned char velocity)
226 buffer[2] = velocity;
243 static void pcmidi_sustained_note_release(
unsigned long data)
251 static void init_sustain_timers(
struct pcmidi_snd *pm)
265 static void stop_sustain_timers(
struct pcmidi_snd *pm)
277 static int pcmidi_get_output_report(
struct pcmidi_snd *pm)
284 if (!(6 == report->
id))
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");
302 static void pcmidi_submit_output_report(
struct pcmidi_snd *pm,
int state)
306 report->
field[0]->value[0] = 0x01;
317 bit_mask = (bit_mask << 8) | data[2];
318 bit_mask = (bit_mask << 8) | data[3];
323 if (pm->
midi_mode && bit_mask == 0x004000) {
328 dbg_hid(
"pcmidi mode: %d octave: %d\n",
333 else if (pm->
midi_mode && bit_mask == 0x000004) {
348 unsigned num_notes = (size-1)/2;
349 for (j = 0; j < num_notes; j++) {
351 velocity = data[j*2+2];
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++) {
400 if (!((0x01 << bit_index) & bit_mask)) {
408 for (bit_index = 0; bit_index < 24; bit_index++) {
410 switch ((0x01 << bit_index) & bit_mask) {
414 pcmidi_submit_output_report(pm, 0xc5);
416 pcmidi_submit_output_report(pm, 0xc6);
419 pcmidi_submit_output_report(pm, 0xc1);
430 dbg_hid(
"pcmidi mode: %d octave: %d\n",
500 static int pcmidi_handle_report(
501 struct pcmidi_snd *pm,
unsigned report_id,
u8 *data,
int size)
507 ret = pcmidi_handle_report1(pm, data);
510 ret = pcmidi_handle_report3(pm, data, size);
513 ret = pcmidi_handle_report4(pm, data);
519 static void pcmidi_setup_extra_keys(
526 unsigned int keys[] = {
542 unsigned int *pkeys = &keys[0];
550 for (i = 0; i < 24; i++)
553 while (*pkeys != 0) {
559 static int pcmidi_set_operational(
struct pcmidi_snd *pm)
564 pcmidi_get_output_report(pm);
565 pcmidi_submit_output_report(pm, 0xc1);
569 static int pcmidi_snd_free(
struct snd_device *dev)
593 dbg_hid(
"pcmidi in trigger %d\n", up);
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)
612 .dev_free = pcmidi_snd_free,
630 pk_error(
"failed to create pc-midi sound card\n");
639 pk_error(
"failed to create pc-midi sound device: error %d\n",
652 pk_error(
"failed to create pc-midi rawmidi device: error %d\n",
668 sysfs_device_attr_channel);
670 pk_error(
"failed to create sysfs attribute channel: error %d\n",
676 sysfs_device_attr_sustain);
678 pk_error(
"failed to create sysfs attribute sustain: error %d\n",
680 goto fail_attr_sustain;
684 sysfs_device_attr_octave);
686 pk_error(
"failed to create sysfs attribute octave: error %d\n",
688 goto fail_attr_octave;
693 init_sustain_timers(pm);
694 pcmidi_set_operational(pm);
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);
722 static int pcmidi_snd_terminate(
struct pcmidi_snd *pm)
725 stop_sustain_timers(pm);
728 sysfs_device_attr_channel);
730 sysfs_device_attr_sustain);
732 sysfs_device_attr_octave);
748 rdesc[111] == 0x06 && rdesc[112] == 0x00 &&
749 rdesc[113] == 0xff) {
751 "fixing up pc-midi keyboard report descriptor\n");
760 unsigned long **
bit,
int *
max)
762 struct pk_device *pk = hid_get_drvdata(hdev);
769 pcmidi_setup_extra_keys(pm, hi->
input);
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) {
789 ret = pcmidi_handle_report(pk->
pm,
790 report->
id, data, size);
802 unsigned short ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
803 unsigned long quirks =
id->driver_data;
809 hid_err(hdev,
"can't alloc descriptor\n");
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");
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);
866 pcmidi_snd_terminate(pm);
878 .driver_data = PK_QUIRK_NOGET},
885 .id_table = pk_devices,
886 .report_fixup = pk_report_fixup,
887 .input_mapping = pk_input_mapping,
888 .raw_event = pk_raw_event,
893 static int pk_init(
void)
899 pr_err(
"can't register prodikeys driver\n");
904 static void pk_exit(
void)