From 14bf62cde79423a02a590e02664ed29a36facec1 Mon Sep 17 00:00:00 2001 From: Stefan Achatz Date: Thu, 18 Mar 2010 16:19:43 +0100 Subject: HID: add driver for Roccat Kone gaming mouse This Patch adds support for Kone gaming mouse from Roccat. It provides access to profiles, settings, firmware, weight, actual settings etc. through sysfs attributes. Event handling of this mouse differs from standard hid behaviour in that tilt button press is reported in each move event which results in strange behaviour if not handled by the driver. This is a heavily reworked version of the previously introduced driver. The changes include most of the previously raised concerns, memory leak and other fixes, code cleanups, adoption of additional achieved knowlege about the hardware and is (IMHO) a much better version than before even when I exchanged reduced USB-IO with a bigger memory consumption. I refused to implement one mentioned point: Removing the 'just-because-we-can' attributes. Motivation: Reading the clipped in weight: I'm no gamer and can't determine the usefulness of this feature but if the manufacturer implements such a feature it might make sense to someone and I would unwillingly limit the functionality besides its such a small feature. Reading the actual profile and dpi settings: Here I can testify that one can get lost of the actual settings when switching back and forth. The manufacturers windows driver has the ability for on-screen-display of the values and there is a mouse in the market that has an lcd on the underside of it to show these values. So I think this feature makes sense not only for me and shouldn't be removed. Signed-off-by: Stefan Achatz Signed-off-by: Jiri Kosina --- drivers/hid/Makefile | 1 + 1 file changed, 1 insertion(+) (limited to 'drivers/hid/Makefile') diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile index 0b2618f092ca..08b83ccd9d1b 100644 --- a/drivers/hid/Makefile +++ b/drivers/hid/Makefile @@ -44,6 +44,7 @@ obj-$(CONFIG_HID_ORTEK) += hid-ortek.o obj-$(CONFIG_HID_QUANTA) += hid-quanta.o obj-$(CONFIG_HID_PANTHERLORD) += hid-pl.o obj-$(CONFIG_HID_PETALYNX) += hid-petalynx.o +obj-$(CONFIG_HID_ROCCAT_KONE) += hid-roccat-kone.o obj-$(CONFIG_HID_SAMSUNG) += hid-samsung.o obj-$(CONFIG_HID_SMARTJOYPLUS) += hid-sjoy.o obj-$(CONFIG_HID_SONY) += hid-sony.o -- cgit v1.2.3 From 236db47c2b3b69464d50c695ab2ddd516cf64520 Mon Sep 17 00:00:00 2001 From: Bruno Prémont Date: Tue, 30 Mar 2010 22:33:50 +0200 Subject: HID: new driver for PicoLCD device MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add basic driver for PicoLCD graphics device. Initially support keypad with input device and provide support for debugging communication via events file from debugfs. Signed-off-by: Bruno Prémont Signed-off-by: Jiri Kosina --- Documentation/ABI/testing/sysfs-driver-hid-picolcd | 17 + drivers/hid/Kconfig | 18 + drivers/hid/Makefile | 1 + drivers/hid/hid-core.c | 2 + drivers/hid/hid-ids.h | 2 + drivers/hid/hid-picolcd.c | 1183 ++++++++++++++++++++ 6 files changed, 1223 insertions(+) create mode 100644 Documentation/ABI/testing/sysfs-driver-hid-picolcd create mode 100644 drivers/hid/hid-picolcd.c (limited to 'drivers/hid/Makefile') diff --git a/Documentation/ABI/testing/sysfs-driver-hid-picolcd b/Documentation/ABI/testing/sysfs-driver-hid-picolcd new file mode 100644 index 000000000000..6fb4f21469f7 --- /dev/null +++ b/Documentation/ABI/testing/sysfs-driver-hid-picolcd @@ -0,0 +1,17 @@ +What: /sys/bus/usb/devices/-:./::./operation_mode +Date: March 2010 +Contact: Bruno Prémont +Description: Make it possible to switch the PicoLCD device between LCD + (firmware) and bootloader (flasher) operation modes. + + Reading: returns list of available modes, the active mode being + enclosed in brackets ('[' and ']') + + Writing: causes operation mode switch. Permitted values are + the non-active mode names listed when read, optionally followed + by a delay value expressed in ms. + + Note: when switching mode the current PicoLCD HID device gets + disconnected and reconnects after above delay (default value + is 5 seconds though this default should not be relied on). + diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig index 71d4c0703629..138ba6a277ee 100644 --- a/drivers/hid/Kconfig +++ b/drivers/hid/Kconfig @@ -262,6 +262,24 @@ config HID_PETALYNX ---help--- Support for Petalynx Maxter remote control. +config HID_PICOLCD + tristate "PicoLCD (graphic version)" + depends on USB_HID + ---help--- + This provides support for Minibox PicoLCD devices, currently + only the graphical ones are supported. + + This includes support for the following device features: + - Keypad + - Switching between Firmware and Flash mode + Features that are not (yet) supported: + - Framebuffer for monochrome 256x64 display + - Backlight control + - Contrast control + - IR + - General purpose outputs + - EEProm / Flash access + config HID_QUANTA tristate "Quanta Optical Touch" depends on USB_HID diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile index 0b2618f092ca..7fd1614e7e2c 100644 --- a/drivers/hid/Makefile +++ b/drivers/hid/Makefile @@ -44,6 +44,7 @@ obj-$(CONFIG_HID_ORTEK) += hid-ortek.o obj-$(CONFIG_HID_QUANTA) += hid-quanta.o obj-$(CONFIG_HID_PANTHERLORD) += hid-pl.o obj-$(CONFIG_HID_PETALYNX) += hid-petalynx.o +obj-$(CONFIG_HID_PICOLCD) += hid-picolcd.o obj-$(CONFIG_HID_SAMSUNG) += hid-samsung.o obj-$(CONFIG_HID_SMARTJOYPLUS) += hid-sjoy.o obj-$(CONFIG_HID_SONY) += hid-sony.o diff --git a/drivers/hid/hid-core.c b/drivers/hid/hid-core.c index 2e2aa759d230..bb11fb460d55 100644 --- a/drivers/hid/hid-core.c +++ b/drivers/hid/hid-core.c @@ -1335,6 +1335,8 @@ static const struct hid_device_id hid_blacklist[] = { { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_RUMBLEPAD2) }, { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_SPACETRAVELLER) }, { HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_SPACENAVIGATOR) }, + { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICOLCD) }, + { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICOLCD_BOOTLOADER) }, { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_SIDEWINDER_GV) }, { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_NE4K) }, { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_LK6K) }, diff --git a/drivers/hid/hid-ids.h b/drivers/hid/hid-ids.h index 797e06470356..783b41d8592e 100644 --- a/drivers/hid/hid-ids.h +++ b/drivers/hid/hid-ids.h @@ -341,6 +341,8 @@ #define USB_VENDOR_ID_MICROCHIP 0x04d8 #define USB_DEVICE_ID_PICKIT1 0x0032 #define USB_DEVICE_ID_PICKIT2 0x0033 +#define USB_DEVICE_ID_PICOLCD 0xc002 +#define USB_DEVICE_ID_PICOLCD_BOOTLOADER 0xf002 #define USB_VENDOR_ID_MICROSOFT 0x045e #define USB_DEVICE_ID_SIDEWINDER_GV 0x003b diff --git a/drivers/hid/hid-picolcd.c b/drivers/hid/hid-picolcd.c new file mode 100644 index 000000000000..c7855f388898 --- /dev/null +++ b/drivers/hid/hid-picolcd.c @@ -0,0 +1,1183 @@ +/*************************************************************************** + * Copyright (C) 2010 by Bruno Prémont * + * * + * Based on Logitech G13 driver (v0.4) * + * Copyright (C) 2009 by Rick L. Vinyard, Jr. * + * * + * This program is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, version 2 of the License. * + * * + * This driver is distributed in the hope that it will be useful, but * + * WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this software. If not see . * + ***************************************************************************/ + +#include +#include +#include +#include "hid-ids.h" +#include "usbhid/usbhid.h" +#include + +#include +#include + +#include + +#define PICOLCD_NAME "PicoLCD (graphic)" + +/* Report numbers */ +#define REPORT_ERROR_CODE 0x10 /* LCD: IN[16] */ +#define ERR_SUCCESS 0x00 +#define ERR_PARAMETER_MISSING 0x01 +#define ERR_DATA_MISSING 0x02 +#define ERR_BLOCK_READ_ONLY 0x03 +#define ERR_BLOCK_NOT_ERASABLE 0x04 +#define ERR_BLOCK_TOO_BIG 0x05 +#define ERR_SECTION_OVERFLOW 0x06 +#define ERR_INVALID_CMD_LEN 0x07 +#define ERR_INVALID_DATA_LEN 0x08 +#define REPORT_KEY_STATE 0x11 /* LCD: IN[2] */ +#define REPORT_IR_DATA 0x21 /* LCD: IN[63] */ +#define REPORT_EE_DATA 0x32 /* LCD: IN[63] */ +#define REPORT_MEMORY 0x41 /* LCD: IN[63] */ +#define REPORT_LED_STATE 0x81 /* LCD: OUT[1] */ +#define REPORT_BRIGHTNESS 0x91 /* LCD: OUT[1] */ +#define REPORT_CONTRAST 0x92 /* LCD: OUT[1] */ +#define REPORT_RESET 0x93 /* LCD: OUT[2] */ +#define REPORT_LCD_CMD 0x94 /* LCD: OUT[63] */ +#define REPORT_LCD_DATA 0x95 /* LCD: OUT[63] */ +#define REPORT_LCD_CMD_DATA 0x96 /* LCD: OUT[63] */ +#define REPORT_EE_READ 0xa3 /* LCD: OUT[63] */ +#define REPORT_EE_WRITE 0xa4 /* LCD: OUT[63] */ +#define REPORT_ERASE_MEMORY 0xb2 /* LCD: OUT[2] */ +#define REPORT_READ_MEMORY 0xb3 /* LCD: OUT[3] */ +#define REPORT_WRITE_MEMORY 0xb4 /* LCD: OUT[63] */ +#define REPORT_SPLASH_RESTART 0xc1 /* LCD: OUT[1] */ +#define REPORT_EXIT_KEYBOARD 0xef /* LCD: OUT[2] */ +#define REPORT_VERSION 0xf1 /* LCD: IN[2],OUT[1] Bootloader: IN[2],OUT[1] */ +#define REPORT_BL_ERASE_MEMORY 0xf2 /* Bootloader: IN[36],OUT[4] */ +#define REPORT_BL_READ_MEMORY 0xf3 /* Bootloader: IN[36],OUT[4] */ +#define REPORT_BL_WRITE_MEMORY 0xf4 /* Bootloader: IN[36],OUT[36] */ +#define REPORT_DEVID 0xf5 /* LCD: IN[5], OUT[1] Bootloader: IN[5],OUT[1] */ +#define REPORT_SPLASH_SIZE 0xf6 /* LCD: IN[4], OUT[1] */ +#define REPORT_HOOK_VERSION 0xf7 /* LCD: IN[2], OUT[1] */ +#define REPORT_EXIT_FLASHER 0xff /* Bootloader: OUT[2] */ + +/* Input device + * + * The PicoLCD has an IR receiver header, a built-in keypad with 5 keys + * and header for 4x4 key matrix. The built-in keys are part of the matrix. + */ +static const unsigned short def_keymap[] = { + KEY_RESERVED, /* none */ + KEY_BACK, /* col 4 + row 1 */ + KEY_HOMEPAGE, /* col 3 + row 1 */ + KEY_RESERVED, /* col 2 + row 1 */ + KEY_RESERVED, /* col 1 + row 1 */ + KEY_SCROLLUP, /* col 4 + row 2 */ + KEY_OK, /* col 3 + row 2 */ + KEY_SCROLLDOWN, /* col 2 + row 2 */ + KEY_RESERVED, /* col 1 + row 2 */ + KEY_RESERVED, /* col 4 + row 3 */ + KEY_RESERVED, /* col 3 + row 3 */ + KEY_RESERVED, /* col 2 + row 3 */ + KEY_RESERVED, /* col 1 + row 3 */ + KEY_RESERVED, /* col 4 + row 4 */ + KEY_RESERVED, /* col 3 + row 4 */ + KEY_RESERVED, /* col 2 + row 4 */ + KEY_RESERVED, /* col 1 + row 4 */ +}; +#define PICOLCD_KEYS ARRAY_SIZE(def_keymap) + +/* Description of in-progress IO operation, used for operations + * that trigger response from device */ +struct picolcd_pending { + struct hid_report *out_report; + struct hid_report *in_report; + struct completion ready; + int raw_size; + u8 raw_data[64]; +}; + +/* Per device data structure */ +struct picolcd_data { + struct hid_device *hdev; +#ifdef CONFIG_DEBUG_FS + int addr_sz; +#endif + u8 version[2]; + /* input stuff */ + u8 pressed_keys[2]; + struct input_dev *input_keys; + struct input_dev *input_cir; + unsigned short keycode[PICOLCD_KEYS]; + + /* Housekeeping stuff */ + spinlock_t lock; + struct mutex mutex; + struct picolcd_pending *pending; + int status; +#define PICOLCD_BOOTLOADER 1 +#define PICOLCD_FAILED 2 +}; + + +/* Find a given report */ +#define picolcd_in_report(id, dev) picolcd_report(id, dev, HID_INPUT_REPORT) +#define picolcd_out_report(id, dev) picolcd_report(id, dev, HID_OUTPUT_REPORT) + +static struct hid_report *picolcd_report(int id, struct hid_device *hdev, int dir) +{ + struct list_head *feature_report_list = &hdev->report_enum[dir].report_list; + struct hid_report *report = NULL; + + list_for_each_entry(report, feature_report_list, list) { + if (report->id == id) + return report; + } + dev_warn(&hdev->dev, "No report with id 0x%x found\n", id); + return NULL; +} + +#ifdef CONFIG_DEBUG_FS +static void picolcd_debug_out_report(struct picolcd_data *data, + struct hid_device *hdev, struct hid_report *report); +#define usbhid_submit_report(a, b, c) \ + do { \ + picolcd_debug_out_report(hid_get_drvdata(a), a, b); \ + usbhid_submit_report(a, b, c); \ + } while (0) +#endif + +/* Submit a report and wait for a reply from device - if device fades away + * or does not respond in time, return NULL */ +static struct picolcd_pending *picolcd_send_and_wait(struct hid_device *hdev, + int report_id, const u8 *raw_data, int size) +{ + struct picolcd_data *data = hid_get_drvdata(hdev); + struct picolcd_pending *work; + struct hid_report *report = picolcd_out_report(report_id, hdev); + unsigned long flags; + int i, j, k; + + if (!report || !data) + return NULL; + if (data->status & PICOLCD_FAILED) + return NULL; + work = kzalloc(sizeof(*work), GFP_KERNEL); + if (!work) + return NULL; + + init_completion(&work->ready); + work->out_report = report; + work->in_report = NULL; + work->raw_size = 0; + + mutex_lock(&data->mutex); + spin_lock_irqsave(&data->lock, flags); + for (i = k = 0; i < report->maxfield; i++) + for (j = 0; j < report->field[i]->report_count; j++) { + hid_set_field(report->field[i], j, k < size ? raw_data[k] : 0); + k++; + } + data->pending = work; + usbhid_submit_report(data->hdev, report, USB_DIR_OUT); + spin_unlock_irqrestore(&data->lock, flags); + wait_for_completion_interruptible_timeout(&work->ready, HZ*2); + spin_lock_irqsave(&data->lock, flags); + data->pending = NULL; + spin_unlock_irqrestore(&data->lock, flags); + mutex_unlock(&data->mutex); + return work; +} + +/* + * input class device + */ +static int picolcd_raw_keypad(struct picolcd_data *data, + struct hid_report *report, u8 *raw_data, int size) +{ + /* + * Keypad event + * First and second data bytes list currently pressed keys, + * 0x00 means no key and at most 2 keys may be pressed at same time + */ + int i, j; + + /* determine newly pressed keys */ + for (i = 0; i < size; i++) { + unsigned int key_code; + if (raw_data[i] == 0) + continue; + for (j = 0; j < sizeof(data->pressed_keys); j++) + if (data->pressed_keys[j] == raw_data[i]) + goto key_already_down; + for (j = 0; j < sizeof(data->pressed_keys); j++) + if (data->pressed_keys[j] == 0) { + data->pressed_keys[j] = raw_data[i]; + break; + } + input_event(data->input_keys, EV_MSC, MSC_SCAN, raw_data[i]); + if (raw_data[i] < PICOLCD_KEYS) + key_code = data->keycode[raw_data[i]]; + else + key_code = KEY_UNKNOWN; + if (key_code != KEY_UNKNOWN) { + dbg_hid(PICOLCD_NAME " got key press for %u:%d", + raw_data[i], key_code); + input_report_key(data->input_keys, key_code, 1); + } + input_sync(data->input_keys); +key_already_down: + continue; + } + + /* determine newly released keys */ + for (j = 0; j < sizeof(data->pressed_keys); j++) { + unsigned int key_code; + if (data->pressed_keys[j] == 0) + continue; + for (i = 0; i < size; i++) + if (data->pressed_keys[j] == raw_data[i]) + goto key_still_down; + input_event(data->input_keys, EV_MSC, MSC_SCAN, data->pressed_keys[j]); + if (data->pressed_keys[j] < PICOLCD_KEYS) + key_code = data->keycode[data->pressed_keys[j]]; + else + key_code = KEY_UNKNOWN; + if (key_code != KEY_UNKNOWN) { + dbg_hid(PICOLCD_NAME " got key release for %u:%d", + data->pressed_keys[j], key_code); + input_report_key(data->input_keys, key_code, 0); + } + input_sync(data->input_keys); + data->pressed_keys[j] = 0; +key_still_down: + continue; + } + return 1; +} + +static int picolcd_raw_cir(struct picolcd_data *data, + struct hid_report *report, u8 *raw_data, int size) +{ + /* Need understanding of CIR data format to implement ... */ + return 1; +} + +static int picolcd_check_version(struct hid_device *hdev) +{ + struct picolcd_data *data = hid_get_drvdata(hdev); + struct picolcd_pending *verinfo; + int ret = 0; + + if (!data) + return -ENODEV; + + verinfo = picolcd_send_and_wait(hdev, REPORT_VERSION, NULL, 0); + if (!verinfo) { + dev_err(&hdev->dev, "no version response from PicoLCD"); + return -ENODEV; + } + + if (verinfo->raw_size == 2) { + if (data->status & PICOLCD_BOOTLOADER) { + dev_info(&hdev->dev, "PicoLCD, bootloader version %d.%d\n", + verinfo->raw_data[0], verinfo->raw_data[1]); + data->version[0] = verinfo->raw_data[0]; + data->version[1] = verinfo->raw_data[1]; + } else { + dev_info(&hdev->dev, "PicoLCD, firmware version %d.%d\n", + verinfo->raw_data[1], verinfo->raw_data[0]); + data->version[0] = verinfo->raw_data[1]; + data->version[1] = verinfo->raw_data[0]; + } + } else { + dev_err(&hdev->dev, "confused, got unexpected version response from PicoLCD\n"); + ret = -EINVAL; + } + kfree(verinfo); + return ret; +} + +/* + * Reset our device and wait for answer to VERSION request + */ +static int picolcd_reset(struct hid_device *hdev) +{ + struct picolcd_data *data = hid_get_drvdata(hdev); + struct hid_report *report = picolcd_out_report(REPORT_RESET, hdev); + unsigned long flags; + + if (!data || !report || report->maxfield != 1) + return -ENODEV; + + spin_lock_irqsave(&data->lock, flags); + if (hdev->product == USB_DEVICE_ID_PICOLCD_BOOTLOADER) + data->status |= PICOLCD_BOOTLOADER; + + /* perform the reset */ + hid_set_field(report->field[0], 0, 1); + usbhid_submit_report(hdev, report, USB_DIR_OUT); + spin_unlock_irqrestore(&data->lock, flags); + + return picolcd_check_version(hdev); +} + +/* + * The "operation_mode" sysfs attribute + */ +static ssize_t picolcd_operation_mode_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct picolcd_data *data = dev_get_drvdata(dev); + + if (data->status & PICOLCD_BOOTLOADER) + return snprintf(buf, PAGE_SIZE, "[bootloader] lcd\n"); + else + return snprintf(buf, PAGE_SIZE, "bootloader [lcd]\n"); +} + +static ssize_t picolcd_operation_mode_store(struct device *dev, + struct device_attribute *attr, const char *buf, size_t count) +{ + struct picolcd_data *data = dev_get_drvdata(dev); + struct hid_report *report = NULL; + size_t cnt = count; + int timeout = 5000; + unsigned u; + unsigned long flags; + + if (cnt >= 3 && strncmp("lcd", buf, 3) == 0) { + if (data->status & PICOLCD_BOOTLOADER) + report = picolcd_out_report(REPORT_EXIT_FLASHER, data->hdev); + buf += 3; + cnt -= 3; + } else if (cnt >= 10 && strncmp("bootloader", buf, 10) == 0) { + if (!(data->status & PICOLCD_BOOTLOADER)) + report = picolcd_out_report(REPORT_EXIT_KEYBOARD, data->hdev); + buf += 10; + cnt -= 10; + } + if (!report) + return -EINVAL; + + while (cnt > 0 && (*buf == ' ' || *buf == '\t')) { + buf++; + cnt--; + } + while (cnt > 0 && (buf[cnt-1] == '\n' || buf[cnt-1] == '\r')) + cnt--; + if (cnt > 0) { + if (sscanf(buf, "%u", &u) != 1) + return -EINVAL; + if (u > 30000) + return -EINVAL; + else + timeout = u; + } + + spin_lock_irqsave(&data->lock, flags); + hid_set_field(report->field[0], 0, timeout & 0xff); + hid_set_field(report->field[0], 1, (timeout >> 8) & 0xff); + usbhid_submit_report(data->hdev, report, USB_DIR_OUT); + spin_unlock_irqrestore(&data->lock, flags); + return count; +} + +static DEVICE_ATTR(operation_mode, 0644, picolcd_operation_mode_show, + picolcd_operation_mode_store); + + +#ifdef CONFIG_DEBUG_FS +/* + * Helper code for HID report level dumping/debugging + */ +static const char *error_codes[] = { + "success", "parameter missing", "data_missing", "block readonly", + "block not erasable", "block too big", "section overflow", + "invalid command length", "invalid data length", +}; + +static void dump_buff_as_hex(char *dst, size_t dst_sz, const u8 *data, + const size_t data_len) +{ + int i, j; + for (i = j = 0; i < data_len && j + 3 < dst_sz; i++) { + dst[j++] = hex_asc[(data[i] >> 4) & 0x0f]; + dst[j++] = hex_asc[data[i] & 0x0f]; + dst[j++] = ' '; + } + if (j < dst_sz) { + dst[j--] = '\0'; + dst[j] = '\n'; + } else + dst[j] = '\0'; +} + +static void picolcd_debug_out_report(struct picolcd_data *data, + struct hid_device *hdev, struct hid_report *report) +{ + u8 raw_data[70]; + int raw_size = (report->size >> 3) + 1; + char *buff; +#define BUFF_SZ 256 + + /* Avoid unnecessary overhead if debugfs is disabled */ + if (!hdev->debug_events) + return; + + buff = kmalloc(BUFF_SZ, GFP_ATOMIC); + if (!buff) + return; + + snprintf(buff, BUFF_SZ, "\nout report %d (size %d) = ", + report->id, raw_size); + hid_debug_event(hdev, buff); + if (raw_size + 5 > sizeof(raw_data)) { + hid_debug_event(hdev, " TOO BIG\n"); + return; + } else { + raw_data[0] = report->id; + hid_output_report(report, raw_data); + dump_buff_as_hex(buff, BUFF_SZ, raw_data, raw_size); + hid_debug_event(hdev, buff); + } + + switch (report->id) { + case REPORT_LED_STATE: + /* 1 data byte with GPO state */ + snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n", + "REPORT_LED_STATE", report->id, raw_size-1); + hid_debug_event(hdev, buff); + snprintf(buff, BUFF_SZ, "\tGPO state: 0x%02x\n", raw_data[1]); + hid_debug_event(hdev, buff); + break; + case REPORT_BRIGHTNESS: + /* 1 data byte with brightness */ + snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n", + "REPORT_BRIGHTNESS", report->id, raw_size-1); + hid_debug_event(hdev, buff); + snprintf(buff, BUFF_SZ, "\tBrightness: 0x%02x\n", raw_data[1]); + hid_debug_event(hdev, buff); + break; + case REPORT_CONTRAST: + /* 1 data byte with contrast */ + snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n", + "REPORT_CONTRAST", report->id, raw_size-1); + hid_debug_event(hdev, buff); + snprintf(buff, BUFF_SZ, "\tContrast: 0x%02x\n", raw_data[1]); + hid_debug_event(hdev, buff); + break; + case REPORT_RESET: + /* 2 data bytes with reset duration in ms */ + snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n", + "REPORT_RESET", report->id, raw_size-1); + hid_debug_event(hdev, buff); + snprintf(buff, BUFF_SZ, "\tDuration: 0x%02x%02x (%dms)\n", + raw_data[2], raw_data[1], raw_data[2] << 8 | raw_data[1]); + hid_debug_event(hdev, buff); + break; + case REPORT_LCD_CMD: + /* 63 data bytes with LCD commands */ + snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n", + "REPORT_LCD_CMD", report->id, raw_size-1); + hid_debug_event(hdev, buff); + /* TODO: format decoding */ + break; + case REPORT_LCD_DATA: + /* 63 data bytes with LCD data */ + snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n", + "REPORT_LCD_CMD", report->id, raw_size-1); + /* TODO: format decoding */ + hid_debug_event(hdev, buff); + break; + case REPORT_LCD_CMD_DATA: + /* 63 data bytes with LCD commands and data */ + snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n", + "REPORT_LCD_CMD", report->id, raw_size-1); + /* TODO: format decoding */ + hid_debug_event(hdev, buff); + break; + case REPORT_EE_READ: + /* 3 data bytes with read area description */ + snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n", + "REPORT_EE_READ", report->id, raw_size-1); + hid_debug_event(hdev, buff); + snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n", + raw_data[2], raw_data[1]); + hid_debug_event(hdev, buff); + snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]); + hid_debug_event(hdev, buff); + break; + case REPORT_EE_WRITE: + /* 3+1..20 data bytes with write area description */ + snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n", + "REPORT_EE_WRITE", report->id, raw_size-1); + hid_debug_event(hdev, buff); + snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n", + raw_data[2], raw_data[1]); + hid_debug_event(hdev, buff); + snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]); + hid_debug_event(hdev, buff); + if (raw_data[3] == 0) { + snprintf(buff, BUFF_SZ, "\tNo data\n"); + } else if (raw_data[3] + 4 <= raw_size) { + snprintf(buff, BUFF_SZ, "\tData: "); + hid_debug_event(hdev, buff); + dump_buff_as_hex(buff, BUFF_SZ, raw_data+4, raw_data[3]); + } else { + snprintf(buff, BUFF_SZ, "\tData overflowed\n"); + } + hid_debug_event(hdev, buff); + break; + case REPORT_ERASE_MEMORY: + case REPORT_BL_ERASE_MEMORY: + /* 3 data bytes with pointer inside erase block */ + snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n", + "REPORT_ERASE_MEMORY", report->id, raw_size-1); + hid_debug_event(hdev, buff); + switch (data->addr_sz) { + case 2: + snprintf(buff, BUFF_SZ, "\tAddress inside 64 byte block: 0x%02x%02x\n", + raw_data[2], raw_data[1]); + break; + case 3: + snprintf(buff, BUFF_SZ, "\tAddress inside 64 byte block: 0x%02x%02x%02x\n", + raw_data[3], raw_data[2], raw_data[1]); + break; + default: + snprintf(buff, BUFF_SZ, "\tNot supported\n"); + } + hid_debug_event(hdev, buff); + break; + case REPORT_READ_MEMORY: + case REPORT_BL_READ_MEMORY: + /* 4 data bytes with read area description */ + snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n", + "REPORT_READ_MEMORY", report->id, raw_size-1); + hid_debug_event(hdev, buff); + switch (data->addr_sz) { + case 2: + snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n", + raw_data[2], raw_data[1]); + hid_debug_event(hdev, buff); + snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]); + break; + case 3: + snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x%02x\n", + raw_data[3], raw_data[2], raw_data[1]); + hid_debug_event(hdev, buff); + snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[4]); + break; + default: + snprintf(buff, BUFF_SZ, "\tNot supported\n"); + } + hid_debug_event(hdev, buff); + break; + case REPORT_WRITE_MEMORY: + case REPORT_BL_WRITE_MEMORY: + /* 4+1..32 data bytes with write adrea description */ + snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n", + "REPORT_WRITE_MEMORY", report->id, raw_size-1); + hid_debug_event(hdev, buff); + switch (data->addr_sz) { + case 2: + snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n", + raw_data[2], raw_data[1]); + hid_debug_event(hdev, buff); + snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]); + hid_debug_event(hdev, buff); + if (raw_data[3] == 0) { + snprintf(buff, BUFF_SZ, "\tNo data\n"); + } else if (raw_data[3] + 4 <= raw_size) { + snprintf(buff, BUFF_SZ, "\tData: "); + hid_debug_event(hdev, buff); + dump_buff_as_hex(buff, BUFF_SZ, raw_data+4, raw_data[3]); + } else { + snprintf(buff, BUFF_SZ, "\tData overflowed\n"); + } + break; + case 3: + snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x%02x\n", + raw_data[3], raw_data[2], raw_data[1]); + hid_debug_event(hdev, buff); + snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[4]); + hid_debug_event(hdev, buff); + if (raw_data[4] == 0) { + snprintf(buff, BUFF_SZ, "\tNo data\n"); + } else if (raw_data[4] + 5 <= raw_size) { + snprintf(buff, BUFF_SZ, "\tData: "); + hid_debug_event(hdev, buff); + dump_buff_as_hex(buff, BUFF_SZ, raw_data+5, raw_data[4]); + } else { + snprintf(buff, BUFF_SZ, "\tData overflowed\n"); + } + break; + default: + snprintf(buff, BUFF_SZ, "\tNot supported\n"); + } + hid_debug_event(hdev, buff); + break; + case REPORT_SPLASH_RESTART: + /* TODO */ + break; + case REPORT_EXIT_KEYBOARD: + snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n", + "REPORT_EXIT_KEYBOARD", report->id, raw_size-1); + hid_debug_event(hdev, buff); + snprintf(buff, BUFF_SZ, "\tRestart delay: %dms (0x%02x%02x)\n", + raw_data[1] | (raw_data[2] << 8), + raw_data[2], raw_data[1]); + hid_debug_event(hdev, buff); + break; + case REPORT_VERSION: + snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n", + "REPORT_VERSION", report->id, raw_size-1); + hid_debug_event(hdev, buff); + break; + case REPORT_DEVID: + snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n", + "REPORT_DEVID", report->id, raw_size-1); + hid_debug_event(hdev, buff); + break; + case REPORT_SPLASH_SIZE: + snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n", + "REPORT_SPLASH_SIZE", report->id, raw_size-1); + hid_debug_event(hdev, buff); + break; + case REPORT_HOOK_VERSION: + snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n", + "REPORT_HOOK_VERSION", report->id, raw_size-1); + hid_debug_event(hdev, buff); + break; + case REPORT_EXIT_FLASHER: + snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n", + "REPORT_VERSION", report->id, raw_size-1); + hid_debug_event(hdev, buff); + snprintf(buff, BUFF_SZ, "\tRestart delay: %dms (0x%02x%02x)\n", + raw_data[1] | (raw_data[2] << 8), + raw_data[2], raw_data[1]); + hid_debug_event(hdev, buff); + break; + default: + snprintf(buff, BUFF_SZ, "out report %s (%d, size=%d)\n", + "", report->id, raw_size-1); + hid_debug_event(hdev, buff); + break; + } + wake_up_interruptible(&hdev->debug_wait); + kfree(buff); +} + +static void picolcd_debug_raw_event(struct picolcd_data *data, + struct hid_device *hdev, struct hid_report *report, + u8 *raw_data, int size) +{ + char *buff; + +#define BUFF_SZ 256 + /* Avoid unnecessary overhead if debugfs is disabled */ + if (!hdev->debug_events) + return; + + buff = kmalloc(BUFF_SZ, GFP_ATOMIC); + if (!buff) + return; + + switch (report->id) { + case REPORT_ERROR_CODE: + /* 2 data bytes with affected report and error code */ + snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n", + "REPORT_ERROR_CODE", report->id, size-1); + hid_debug_event(hdev, buff); + if (raw_data[2] < ARRAY_SIZE(error_codes)) + snprintf(buff, BUFF_SZ, "\tError code 0x%02x (%s) in reply to report 0x%02x\n", + raw_data[2], error_codes[raw_data[2]], raw_data[1]); + else + snprintf(buff, BUFF_SZ, "\tError code 0x%02x in reply to report 0x%02x\n", + raw_data[2], raw_data[1]); + hid_debug_event(hdev, buff); + break; + case REPORT_KEY_STATE: + /* 2 data bytes with key state */ + snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n", + "REPORT_KEY_STATE", report->id, size-1); + hid_debug_event(hdev, buff); + if (raw_data[1] == 0) + snprintf(buff, BUFF_SZ, "\tNo key pressed\n"); + else if (raw_data[2] == 0) + snprintf(buff, BUFF_SZ, "\tOne key pressed: 0x%02x (%d)\n", + raw_data[1], raw_data[1]); + else + snprintf(buff, BUFF_SZ, "\tTwo keys pressed: 0x%02x (%d), 0x%02x (%d)\n", + raw_data[1], raw_data[1], raw_data[2], raw_data[2]); + hid_debug_event(hdev, buff); + break; + case REPORT_IR_DATA: + /* Up to 20 byes of IR scancode data */ + snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n", + "REPORT_IR_DATA", report->id, size-1); + hid_debug_event(hdev, buff); + if (raw_data[1] == 0) { + snprintf(buff, BUFF_SZ, "\tUnexpectedly 0 data length\n"); + hid_debug_event(hdev, buff); + } else if (raw_data[1] + 1 <= size) { + snprintf(buff, BUFF_SZ, "\tData length: %d\n\tIR Data: ", + raw_data[1]-1); + hid_debug_event(hdev, buff); + dump_buff_as_hex(buff, BUFF_SZ, raw_data+2, raw_data[1]-1); + hid_debug_event(hdev, buff); + } else { + snprintf(buff, BUFF_SZ, "\tOverflowing data length: %d\n", + raw_data[1]-1); + hid_debug_event(hdev, buff); + } + break; + case REPORT_EE_DATA: + /* Data buffer in response to REPORT_EE_READ or REPORT_EE_WRITE */ + snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n", + "REPORT_EE_DATA", report->id, size-1); + hid_debug_event(hdev, buff); + snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n", + raw_data[2], raw_data[1]); + hid_debug_event(hdev, buff); + snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]); + hid_debug_event(hdev, buff); + if (raw_data[3] == 0) { + snprintf(buff, BUFF_SZ, "\tNo data\n"); + hid_debug_event(hdev, buff); + } else if (raw_data[3] + 4 <= size) { + snprintf(buff, BUFF_SZ, "\tData: "); + hid_debug_event(hdev, buff); + dump_buff_as_hex(buff, BUFF_SZ, raw_data+4, raw_data[3]); + hid_debug_event(hdev, buff); + } else { + snprintf(buff, BUFF_SZ, "\tData overflowed\n"); + hid_debug_event(hdev, buff); + } + break; + case REPORT_MEMORY: + /* Data buffer in response to REPORT_READ_MEMORY or REPORT_WRTIE_MEMORY */ + snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n", + "REPORT_MEMORY", report->id, size-1); + hid_debug_event(hdev, buff); + switch (data->addr_sz) { + case 2: + snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x\n", + raw_data[2], raw_data[1]); + hid_debug_event(hdev, buff); + snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[3]); + hid_debug_event(hdev, buff); + if (raw_data[3] == 0) { + snprintf(buff, BUFF_SZ, "\tNo data\n"); + } else if (raw_data[3] + 4 <= size) { + snprintf(buff, BUFF_SZ, "\tData: "); + hid_debug_event(hdev, buff); + dump_buff_as_hex(buff, BUFF_SZ, raw_data+4, raw_data[3]); + } else { + snprintf(buff, BUFF_SZ, "\tData overflowed\n"); + } + break; + case 3: + snprintf(buff, BUFF_SZ, "\tData address: 0x%02x%02x%02x\n", + raw_data[3], raw_data[2], raw_data[1]); + hid_debug_event(hdev, buff); + snprintf(buff, BUFF_SZ, "\tData length: %d\n", raw_data[4]); + hid_debug_event(hdev, buff); + if (raw_data[4] == 0) { + snprintf(buff, BUFF_SZ, "\tNo data\n"); + } else if (raw_data[4] + 5 <= size) { + snprintf(buff, BUFF_SZ, "\tData: "); + hid_debug_event(hdev, buff); + dump_buff_as_hex(buff, BUFF_SZ, raw_data+5, raw_data[4]); + } else { + snprintf(buff, BUFF_SZ, "\tData overflowed\n"); + } + break; + default: + snprintf(buff, BUFF_SZ, "\tNot supported\n"); + } + hid_debug_event(hdev, buff); + break; + case REPORT_VERSION: + snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n", + "REPORT_VERSION", report->id, size-1); + hid_debug_event(hdev, buff); + snprintf(buff, BUFF_SZ, "\tFirmware version: %d.%d\n", + raw_data[2], raw_data[1]); + hid_debug_event(hdev, buff); + break; + case REPORT_BL_ERASE_MEMORY: + snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n", + "REPORT_BL_ERASE_MEMORY", report->id, size-1); + hid_debug_event(hdev, buff); + /* TODO */ + break; + case REPORT_BL_READ_MEMORY: + snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n", + "REPORT_BL_READ_MEMORY", report->id, size-1); + hid_debug_event(hdev, buff); + /* TODO */ + break; + case REPORT_BL_WRITE_MEMORY: + snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n", + "REPORT_BL_WRITE_MEMORY", report->id, size-1); + hid_debug_event(hdev, buff); + /* TODO */ + break; + case REPORT_DEVID: + snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n", + "REPORT_DEVID", report->id, size-1); + hid_debug_event(hdev, buff); + snprintf(buff, BUFF_SZ, "\tSerial: 0x%02x%02x%02x%02x\n", + raw_data[1], raw_data[2], raw_data[3], raw_data[4]); + hid_debug_event(hdev, buff); + snprintf(buff, BUFF_SZ, "\tType: 0x%02x\n", + raw_data[5]); + hid_debug_event(hdev, buff); + break; + case REPORT_SPLASH_SIZE: + snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n", + "REPORT_SPLASH_SIZE", report->id, size-1); + hid_debug_event(hdev, buff); + snprintf(buff, BUFF_SZ, "\tTotal splash space: %d\n", + (raw_data[2] << 8) | raw_data[1]); + hid_debug_event(hdev, buff); + snprintf(buff, BUFF_SZ, "\tUsed splash space: %d\n", + (raw_data[4] << 8) | raw_data[3]); + hid_debug_event(hdev, buff); + break; + case REPORT_HOOK_VERSION: + snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n", + "REPORT_HOOK_VERSION", report->id, size-1); + hid_debug_event(hdev, buff); + snprintf(buff, BUFF_SZ, "\tFirmware version: %d.%d\n", + raw_data[1], raw_data[2]); + hid_debug_event(hdev, buff); + break; + default: + snprintf(buff, BUFF_SZ, "report %s (%d, size=%d)\n", + "", report->id, size-1); + hid_debug_event(hdev, buff); + break; + } + wake_up_interruptible(&hdev->debug_wait); + kfree(buff); +} +#else +#define picolcd_debug_raw_event(data, hdev, report, raw_data, size) +#endif + +/* + * Handle raw report as sent by device + */ +static int picolcd_raw_event(struct hid_device *hdev, + struct hid_report *report, u8 *raw_data, int size) +{ + struct picolcd_data *data = hid_get_drvdata(hdev); + unsigned long flags; + int ret = 0; + + if (!data) + return 1; + + if (report->id == REPORT_KEY_STATE) { + if (data->input_keys) + ret = picolcd_raw_keypad(data, report, raw_data+1, size-1); + } else if (report->id == REPORT_IR_DATA) { + if (data->input_cir) + ret = picolcd_raw_cir(data, report, raw_data+1, size-1); + } else { + spin_lock_irqsave(&data->lock, flags); + /* + * We let the caller of picolcd_send_and_wait() check if the + * report we got is one of the expected ones or not. + */ + if (data->pending) { + memcpy(data->pending->raw_data, raw_data+1, size-1); + data->pending->raw_size = size-1; + data->pending->in_report = report; + complete(&data->pending->ready); + } + spin_unlock_irqrestore(&data->lock, flags); + } + + picolcd_debug_raw_event(data, hdev, report, raw_data, size); + return 1; +} + +/* initialize keypad input device */ +static int picolcd_init_keys(struct picolcd_data *data, + struct hid_report *report) +{ + struct hid_device *hdev = data->hdev; + struct input_dev *idev; + int error, i; + + if (!report) + return -ENODEV; + if (report->maxfield != 1 || report->field[0]->report_count != 2 || + report->field[0]->report_size != 8) { + dev_err(&hdev->dev, "unsupported KEY_STATE report"); + return -EINVAL; + } + + idev = input_allocate_device(); + if (idev == NULL) { + dev_err(&hdev->dev, "failed to allocate input device"); + return -ENOMEM; + } + input_set_drvdata(idev, hdev); + memcpy(data->keycode, def_keymap, sizeof(def_keymap)); + idev->name = hdev->name; + idev->phys = hdev->phys; + idev->uniq = hdev->uniq; + idev->id.bustype = hdev->bus; + idev->id.vendor = hdev->vendor; + idev->id.product = hdev->product; + idev->id.version = hdev->version; + idev->dev.parent = hdev->dev.parent; + idev->keycode = &data->keycode; + idev->keycodemax = PICOLCD_KEYS; + idev->keycodesize = sizeof(data->keycode[0]); + input_set_capability(idev, EV_MSC, MSC_SCAN); + set_bit(EV_REP, idev->evbit); + for (i = 0; i < PICOLCD_KEYS; i++) + input_set_capability(idev, EV_KEY, data->keycode[i]); + error = input_register_device(idev); + if (error) { + dev_err(&hdev->dev, "error registering the input device"); + input_free_device(idev); + return error; + } + data->input_keys = idev; + return 0; +} + +static void picolcd_exit_keys(struct picolcd_data *data) +{ + struct input_dev *idev = data->input_keys; + + data->input_keys = NULL; + if (idev) + input_unregister_device(idev); +} + +/* initialize CIR input device */ +static inline int picolcd_init_cir(struct picolcd_data *data, struct hid_report *report) +{ + /* support not implemented yet */ + return 0; +} + +static inline void picolcd_exit_cir(struct picolcd_data *data) +{ +} + +static int picolcd_probe_lcd(struct hid_device *hdev, struct picolcd_data *data) +{ + struct hid_report *report; + int error; + + error = picolcd_check_version(hdev); + if (error) + return error; + + if (data->version[0] != 0 && data->version[1] != 3) + dev_info(&hdev->dev, "Device with untested firmware revision, " + "please submit /sys/kernel/debug/hid/%s/rdesc for this device.\n", + dev_name(&hdev->dev)); + + /* Setup keypad input device */ + error = picolcd_init_keys(data, picolcd_in_report(REPORT_KEY_STATE, hdev)); + if (error) + goto err; + + /* Setup CIR input device */ + error = picolcd_init_cir(data, picolcd_in_report(REPORT_IR_DATA, hdev)); + if (error) + goto err; + +#ifdef CONFIG_DEBUG_FS + report = picolcd_out_report(REPORT_READ_MEMORY, hdev); + if (report && report->maxfield == 1 && report->field[0]->report_size == 8) + data->addr_sz = report->field[0]->report_count - 1; + else + data->addr_sz = -1; +#endif + return 0; +err: + picolcd_exit_cir(data); + picolcd_exit_keys(data); + return error; +} + +static int picolcd_probe_bootloader(struct hid_device *hdev, struct picolcd_data *data) +{ + struct hid_report *report; + int error; + + error = picolcd_check_version(hdev); + if (error) + return error; + + if (data->version[0] != 1 && data->version[1] != 0) + dev_info(&hdev->dev, "Device with untested bootloader revision, " + "please submit /sys/kernel/debug/hid/%s/rdesc for this device.\n", + dev_name(&hdev->dev)); + +#ifdef CONFIG_DEBUG_FS + report = picolcd_out_report(REPORT_BL_READ_MEMORY, hdev); + if (report && report->maxfield == 1 && report->field[0]->report_size == 8) + data->addr_sz = report->field[0]->report_count - 1; + else + data->addr_sz = -1; +#endif + return 0; +} + +static int picolcd_probe(struct hid_device *hdev, + const struct hid_device_id *id) +{ + struct picolcd_data *data; + int error = -ENOMEM; + + dbg_hid(PICOLCD_NAME " hardware probe...\n"); + + /* + * Let's allocate the picolcd data structure, set some reasonable + * defaults, and associate it with the device + */ + data = kzalloc(sizeof(struct picolcd_data), GFP_KERNEL); + if (data == NULL) { + dev_err(&hdev->dev, "can't allocate space for Minibox PicoLCD device data\n"); + error = -ENOMEM; + goto err_no_cleanup; + } + + spin_lock_init(&data->lock); + mutex_init(&data->mutex); + data->hdev = hdev; + if (hdev->product == USB_DEVICE_ID_PICOLCD_BOOTLOADER) + data->status |= PICOLCD_BOOTLOADER; + hid_set_drvdata(hdev, data); + + /* Parse the device reports and start it up */ + error = hid_parse(hdev); + if (error) { + dev_err(&hdev->dev, "device report parse failed\n"); + goto err_cleanup_data; + } + + /* We don't use hidinput but hid_hw_start() fails if nothing is + * claimed. So spoof claimed input. */ + hdev->claimed = HID_CLAIMED_INPUT; + error = hid_hw_start(hdev, 0); + hdev->claimed = 0; + if (error) { + dev_err(&hdev->dev, "hardware start failed\n"); + goto err_cleanup_data; + } + + error = hdev->ll_driver->open(hdev); + if (error) { + dev_err(&hdev->dev, "failed to open input interrupt pipe for key and IR events\n"); + goto err_cleanup_hid_hw; + } + + error = device_create_file(&hdev->dev, &dev_attr_operation_mode); + if (error) { + dev_err(&hdev->dev, "failed to create sysfs attributes\n"); + goto err_cleanup_hid_ll; + } + + if (data->status & PICOLCD_BOOTLOADER) + error = picolcd_probe_bootloader(hdev, data); + else + error = picolcd_probe_lcd(hdev, data); + if (error) + goto err_cleanup_sysfs; + + dbg_hid(PICOLCD_NAME " activated and initialized\n"); + return 0; + +err_cleanup_sysfs: + device_remove_file(&hdev->dev, &dev_attr_operation_mode); +err_cleanup_hid_ll: + hdev->ll_driver->close(hdev); +err_cleanup_hid_hw: + hid_hw_stop(hdev); +err_cleanup_data: + kfree(data); +err_no_cleanup: + hid_set_drvdata(hdev, NULL); + + return error; +} + +static void picolcd_remove(struct hid_device *hdev) +{ + struct picolcd_data *data = hid_get_drvdata(hdev); + unsigned long flags; + + dbg_hid(PICOLCD_NAME " hardware remove...\n"); + spin_lock_irqsave(&data->lock, flags); + data->status |= PICOLCD_FAILED; + spin_unlock_irqrestore(&data->lock, flags); + + device_remove_file(&hdev->dev, &dev_attr_operation_mode); + hdev->ll_driver->close(hdev); + hid_hw_stop(hdev); + hid_set_drvdata(hdev, NULL); + + /* Shortcut potential pending reply that will never arrive */ + spin_lock_irqsave(&data->lock, flags); + if (data->pending) + complete(&data->pending->ready); + spin_unlock_irqrestore(&data->lock, flags); + + /* Cleanup input */ + picolcd_exit_cir(data); + picolcd_exit_keys(data); + + mutex_destroy(&data->mutex); + /* Finally, clean up the picolcd data itself */ + kfree(data); +} + +static const struct hid_device_id picolcd_devices[] = { + { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICOLCD) }, + { HID_USB_DEVICE(USB_VENDOR_ID_MICROCHIP, USB_DEVICE_ID_PICOLCD_BOOTLOADER) }, + { } +}; +MODULE_DEVICE_TABLE(hid, picolcd_devices); + +static struct hid_driver picolcd_driver = { + .name = "hid-picolcd", + .id_table = picolcd_devices, + .probe = picolcd_probe, + .remove = picolcd_remove, + .raw_event = picolcd_raw_event, +}; + +static int __init picolcd_init(void) +{ + return hid_register_driver(&picolcd_driver); +} + +static void __exit picolcd_exit(void) +{ + hid_unregister_driver(&picolcd_driver); +} + +module_init(picolcd_init); +module_exit(picolcd_exit); +MODULE_DESCRIPTION("Minibox graphics PicoLCD Driver"); +MODULE_LICENSE("GPL v2"); -- cgit v1.2.3 From 3a370ca1dcf8c80aff7a0a21d6b0f50ca2a151e9 Mon Sep 17 00:00:00 2001 From: Don Prince Date: Wed, 12 May 2010 15:18:59 +0200 Subject: HID: Prodikeys PC-MIDI HID Driver A specialised HID driver for the Creative Prodikeys PC-MIDI USB Keyboard. The Prodikeys PC-MIDI is a multifunction keyboard comprising a qwerty keyboard, multimedia keys and a touch sensitive musical keyboard. The specialised HID driver adds full support for the musical keyboard and extra multimedia keys which are not currently handled by the default HID driver. The specialised HID driver interfaces with ALSA, and presents the midi keyboard as a rawmidi device. Sustain duration, octave shifting and the midi output channel can be read/written form userspace via sysfs. Signed-off-by: Don Prince ALSA parts: Acked-by: Clemens Ladisch Signed-off-by: Jiri Kosina --- .../ABI/testing/sysfs-driver-hid-prodikeys | 29 + drivers/hid/Kconfig | 16 + drivers/hid/Makefile | 1 + drivers/hid/hid-core.c | 1 + drivers/hid/hid-ids.h | 3 + drivers/hid/hid-prodikeys.c | 910 +++++++++++++++++++++ 6 files changed, 960 insertions(+) create mode 100644 Documentation/ABI/testing/sysfs-driver-hid-prodikeys create mode 100644 drivers/hid/hid-prodikeys.c (limited to 'drivers/hid/Makefile') diff --git a/Documentation/ABI/testing/sysfs-driver-hid-prodikeys b/Documentation/ABI/testing/sysfs-driver-hid-prodikeys new file mode 100644 index 000000000000..05d988c29a83 --- /dev/null +++ b/Documentation/ABI/testing/sysfs-driver-hid-prodikeys @@ -0,0 +1,29 @@ +What: /sys/bus/hid/drivers/prodikeys/.../channel +Date: April 2010 +KernelVersion: 2.6.34 +Contact: Don Prince +Description: + Allows control (via software) the midi channel to which + that the pc-midi keyboard will output.midi data. + Range: 0..15 + Type: Read/write +What: /sys/bus/hid/drivers/prodikeys/.../sustain +Date: April 2010 +KernelVersion: 2.6.34 +Contact: Don Prince +Description: + Allows control (via software) the sustain duration of a + note held by the pc-midi driver. + 0 means sustain mode is disabled. + Range: 0..5000 (milliseconds) + Type: Read/write +What: /sys/bus/hid/drivers/prodikeys/.../octave +Date: April 2010 +KernelVersion: 2.6.34 +Contact: Don Prince +Description: + Controls the octave shift modifier in the pc-midi driver. + The octave can be shifted via software up/down 2 octaves. + 0 means the no ocatve shift. + Range: -2..2 (minus 2 to plus 2) + Type: Read/Write diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig index 71d4c0703629..0c3eee94f5a7 100644 --- a/drivers/hid/Kconfig +++ b/drivers/hid/Kconfig @@ -100,6 +100,22 @@ config HID_CHICONY ---help--- Support for Chicony Tactical pad. +config HID_PRODIKEYS + tristate "Prodikeys PC-MIDI Keyboard support" if EMBEDDED + depends on USB_HID && SND + select SND_RAWMIDI + default !EMBEDDED + ---help--- + Support for Prodikeys PC-MIDI Keyboard device support. + Say Y here to enable support for this device. + - Prodikeys PC-MIDI keyboard. + The Prodikeys PC-MIDI acts as a USB Audio device, with one MIDI + input and one MIDI output. These MIDI jacks appear as + a sound "card" in the ALSA sound system. + Note: if you say N here, this device will still function as a basic + multimedia keyboard, but will lack support for the musical keyboard + and some additional multimedia keys. + config HID_CYPRESS tristate "Cypress" if EMBEDDED depends on USB_HID diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile index 0b2618f092ca..f637b388030f 100644 --- a/drivers/hid/Makefile +++ b/drivers/hid/Makefile @@ -41,6 +41,7 @@ obj-$(CONFIG_HID_MONTEREY) += hid-monterey.o obj-$(CONFIG_HID_MOSART) += hid-mosart.o obj-$(CONFIG_HID_NTRIG) += hid-ntrig.o obj-$(CONFIG_HID_ORTEK) += hid-ortek.o +obj-$(CONFIG_HID_PRODIKEYS) += hid-prodikeys.o obj-$(CONFIG_HID_QUANTA) += hid-quanta.o obj-$(CONFIG_HID_PANTHERLORD) += hid-pl.o obj-$(CONFIG_HID_PETALYNX) += hid-petalynx.o diff --git a/drivers/hid/hid-core.c b/drivers/hid/hid-core.c index 143e788b729b..1c57a9391c8f 100644 --- a/drivers/hid/hid-core.c +++ b/drivers/hid/hid-core.c @@ -1293,6 +1293,7 @@ static const struct hid_device_id hid_blacklist[] = { { HID_USB_DEVICE(USB_VENDOR_ID_CHERRY, USB_DEVICE_ID_CHERRY_CYMOTION) }, { HID_USB_DEVICE(USB_VENDOR_ID_CHERRY, USB_DEVICE_ID_CHERRY_CYMOTION_SOLAR) }, { HID_USB_DEVICE(USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_CHICONY_TACTICAL_PAD) }, + { HID_USB_DEVICE(USB_VENDOR_ID_CREATIVELABS, USB_DEVICE_ID_PRODIKEYS_PCMIDI) }, { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_1) }, { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_2) }, { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_3) }, diff --git a/drivers/hid/hid-ids.h b/drivers/hid/hid-ids.h index 09d27649a0f7..50134431aeee 100644 --- a/drivers/hid/hid-ids.h +++ b/drivers/hid/hid-ids.h @@ -148,6 +148,9 @@ #define USB_DEVICE_ID_CODEMERCS_IOW_FIRST 0x1500 #define USB_DEVICE_ID_CODEMERCS_IOW_LAST 0x15ff +#define USB_VENDOR_ID_CREATIVELABS 0x041e +#define USB_DEVICE_ID_PRODIKEYS_PCMIDI 0x2801 + #define USB_VENDOR_ID_CYGNAL 0x10c4 #define USB_DEVICE_ID_CYGNAL_RADIO_SI470X 0x818a diff --git a/drivers/hid/hid-prodikeys.c b/drivers/hid/hid-prodikeys.c new file mode 100644 index 000000000000..845f428b8090 --- /dev/null +++ b/drivers/hid/hid-prodikeys.c @@ -0,0 +1,910 @@ +/* + * HID driver for the Prodikeys PC-MIDI Keyboard + * providing midi & extra multimedia keys functionality + * + * Copyright (c) 2009 Don Prince + * + * Controls for Octave Shift Up/Down, Channel, and + * Sustain Duration available via sysfs. + * + */ + +/* + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or (at your option) + * any later version. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include "usbhid/usbhid.h" +#include "hid-ids.h" + + +#define pk_debug(format, arg...) \ + pr_debug("hid-prodikeys: " format "\n" , ## arg) +#define pk_error(format, arg...) \ + pr_err("hid-prodikeys: " format "\n" , ## arg) + +struct pcmidi_snd; + +struct pk_device { + unsigned long quirks; + + struct hid_device *hdev; + struct pcmidi_snd *pm; /* pcmidi device context */ +}; + +struct pcmidi_snd; + +struct pcmidi_sustain { + unsigned long in_use; + struct pcmidi_snd *pm; + struct timer_list timer; + unsigned char status; + unsigned char note; + unsigned char velocity; +}; + +#define PCMIDI_SUSTAINED_MAX 32 +struct pcmidi_snd { + struct pk_device *pk; + unsigned short ifnum; + struct hid_report *pcmidi_report6; + struct input_dev *input_ep82; + unsigned short midi_mode; + unsigned short midi_sustain_mode; + unsigned short midi_sustain; + unsigned short midi_channel; + short midi_octave; + struct pcmidi_sustain sustained_notes[PCMIDI_SUSTAINED_MAX]; + unsigned short fn_state; + unsigned short last_key[24]; + spinlock_t rawmidi_in_lock; + struct snd_card *card; + struct snd_rawmidi *rwmidi; + struct snd_rawmidi_substream *in_substream; + struct snd_rawmidi_substream *out_substream; + unsigned long in_triggered; + unsigned long out_active; +}; + +#define PK_QUIRK_NOGET 0x00010000 +#define PCMIDI_MIDDLE_C 60 +#define PCMIDI_CHANNEL_MIN 0 +#define PCMIDI_CHANNEL_MAX 15 +#define PCMIDI_OCTAVE_MIN (-2) +#define PCMIDI_OCTAVE_MAX 2 +#define PCMIDI_SUSTAIN_MIN 0 +#define PCMIDI_SUSTAIN_MAX 5000 + +static const char shortname[] = "PC-MIDI"; +static const char longname[] = "Prodikeys PC-MIDI Keyboard"; + +static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; +static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; +static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; + +module_param_array(index, int, NULL, 0444); +module_param_array(id, charp, NULL, 0444); +module_param_array(enable, bool, NULL, 0444); +MODULE_PARM_DESC(index, "Index value for the PC-MIDI virtual audio driver"); +MODULE_PARM_DESC(id, "ID string for the PC-MIDI virtual audio driver"); +MODULE_PARM_DESC(enable, "Enable for the PC-MIDI virtual audio driver"); + + +/* Output routine for the sysfs channel file */ +static ssize_t show_channel(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct hid_device *hdev = container_of(dev, struct hid_device, dev); + struct pk_device *pk = (struct pk_device *)hid_get_drvdata(hdev); + + dbg_hid("pcmidi sysfs read channel=%u\n", pk->pm->midi_channel); + + return sprintf(buf, "%u (min:%u, max:%u)\n", pk->pm->midi_channel, + PCMIDI_CHANNEL_MIN, PCMIDI_CHANNEL_MAX); +} + +/* Input routine for the sysfs channel file */ +static ssize_t store_channel(struct device *dev, + struct device_attribute *attr, const char *buf, size_t count) +{ + struct hid_device *hdev = container_of(dev, struct hid_device, dev); + struct pk_device *pk = (struct pk_device *)hid_get_drvdata(hdev); + + unsigned channel = 0; + + if (sscanf(buf, "%u", &channel) > 0 && channel <= PCMIDI_CHANNEL_MAX) { + dbg_hid("pcmidi sysfs write channel=%u\n", channel); + pk->pm->midi_channel = channel; + return strlen(buf); + } + return -EINVAL; +} + +static DEVICE_ATTR(channel, S_IRUGO | S_IWUGO, show_channel, + store_channel); + +static struct device_attribute *sysfs_device_attr_channel = { + &dev_attr_channel, + }; + +/* Output routine for the sysfs sustain file */ +static ssize_t show_sustain(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct hid_device *hdev = container_of(dev, struct hid_device, dev); + struct pk_device *pk = (struct pk_device *)hid_get_drvdata(hdev); + + dbg_hid("pcmidi sysfs read sustain=%u\n", pk->pm->midi_sustain); + + return sprintf(buf, "%u (off:%u, max:%u (ms))\n", pk->pm->midi_sustain, + PCMIDI_SUSTAIN_MIN, PCMIDI_SUSTAIN_MAX); +} + +/* Input routine for the sysfs sustain file */ +static ssize_t store_sustain(struct device *dev, + struct device_attribute *attr, const char *buf, size_t count) +{ + struct hid_device *hdev = container_of(dev, struct hid_device, dev); + struct pk_device *pk = (struct pk_device *)hid_get_drvdata(hdev); + + unsigned sustain = 0; + + if (sscanf(buf, "%u", &sustain) > 0 && sustain <= PCMIDI_SUSTAIN_MAX) { + dbg_hid("pcmidi sysfs write sustain=%u\n", sustain); + pk->pm->midi_sustain = sustain; + pk->pm->midi_sustain_mode = + (0 == sustain || !pk->pm->midi_mode) ? 0 : 1; + return strlen(buf); + } + return -EINVAL; +} + +static DEVICE_ATTR(sustain, S_IRUGO | S_IWUGO, show_sustain, + store_sustain); + +static struct device_attribute *sysfs_device_attr_sustain = { + &dev_attr_sustain, + }; + +/* Output routine for the sysfs octave file */ +static ssize_t show_octave(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct hid_device *hdev = container_of(dev, struct hid_device, dev); + struct pk_device *pk = (struct pk_device *)hid_get_drvdata(hdev); + + dbg_hid("pcmidi sysfs read octave=%d\n", pk->pm->midi_octave); + + return sprintf(buf, "%d (min:%d, max:%d)\n", pk->pm->midi_octave, + PCMIDI_OCTAVE_MIN, PCMIDI_OCTAVE_MAX); +} + +/* Input routine for the sysfs octave file */ +static ssize_t store_octave(struct device *dev, + struct device_attribute *attr, const char *buf, size_t count) +{ + struct hid_device *hdev = container_of(dev, struct hid_device, dev); + struct pk_device *pk = (struct pk_device *)hid_get_drvdata(hdev); + + int octave = 0; + + if (sscanf(buf, "%d", &octave) > 0 && + octave >= PCMIDI_OCTAVE_MIN && octave <= PCMIDI_OCTAVE_MAX) { + dbg_hid("pcmidi sysfs write octave=%d\n", octave); + pk->pm->midi_octave = octave; + return strlen(buf); + } + return -EINVAL; +} + +static DEVICE_ATTR(octave, S_IRUGO | S_IWUGO, show_octave, + store_octave); + +static struct device_attribute *sysfs_device_attr_octave = { + &dev_attr_octave, + }; + + +static void pcmidi_send_note(struct pcmidi_snd *pm, + unsigned char status, unsigned char note, unsigned char velocity) +{ + unsigned long flags; + unsigned char buffer[3]; + + buffer[0] = status; + buffer[1] = note; + buffer[2] = velocity; + + spin_lock_irqsave(&pm->rawmidi_in_lock, flags); + + if (!pm->in_substream) + goto drop_note; + if (!test_bit(pm->in_substream->number, &pm->in_triggered)) + goto drop_note; + + snd_rawmidi_receive(pm->in_substream, buffer, 3); + +drop_note: + spin_unlock_irqrestore(&pm->rawmidi_in_lock, flags); + + return; +} + +void pcmidi_sustained_note_release(unsigned long data) +{ + struct pcmidi_sustain *pms = (struct pcmidi_sustain *)data; + + pcmidi_send_note(pms->pm, pms->status, pms->note, pms->velocity); + pms->in_use = 0; +} + +void init_sustain_timers(struct pcmidi_snd *pm) +{ + struct pcmidi_sustain *pms; + unsigned i; + + for (i = 0; i < PCMIDI_SUSTAINED_MAX; i++) { + pms = &pm->sustained_notes[i]; + pms->in_use = 0; + pms->pm = pm; + setup_timer(&pms->timer, pcmidi_sustained_note_release, + (unsigned long)pms); + } +} + +void stop_sustain_timers(struct pcmidi_snd *pm) +{ + struct pcmidi_sustain *pms; + unsigned i; + + for (i = 0; i < PCMIDI_SUSTAINED_MAX; i++) { + pms = &pm->sustained_notes[i]; + pms->in_use = 1; + del_timer_sync(&pms->timer); + } +} + +static int pcmidi_get_output_report(struct pcmidi_snd *pm) +{ + struct hid_device *hdev = pm->pk->hdev; + struct hid_report *report; + + list_for_each_entry(report, + &hdev->report_enum[HID_OUTPUT_REPORT].report_list, list) { + if (!(6 == report->id)) + continue; + + if (report->maxfield < 1) { + dev_err(&hdev->dev, "output report is empty\n"); + break; + } + if (report->field[0]->report_count != 2) { + dev_err(&hdev->dev, "field count too low\n"); + break; + } + pm->pcmidi_report6 = report; + return 0; + } + /* should never get here */ + return -ENODEV; +} + +static void pcmidi_submit_output_report(struct pcmidi_snd *pm, int state) +{ + struct hid_device *hdev = pm->pk->hdev; + struct hid_report *report = pm->pcmidi_report6; + report->field[0]->value[0] = 0x01; + report->field[0]->value[1] = state; + + usbhid_submit_report(hdev, report, USB_DIR_OUT); +} + +static int pcmidi_handle_report1(struct pcmidi_snd *pm, u8 *data) +{ + u32 bit_mask; + + bit_mask = data[1]; + bit_mask = (bit_mask << 8) | data[2]; + bit_mask = (bit_mask << 8) | data[3]; + + dbg_hid("pcmidi mode: %d\n", pm->midi_mode); + + /*KEY_MAIL or octave down*/ + if (pm->midi_mode && bit_mask == 0x004000) { + /* octave down */ + pm->midi_octave--; + if (pm->midi_octave < -2) + pm->midi_octave = -2; + dbg_hid("pcmidi mode: %d octave: %d\n", + pm->midi_mode, pm->midi_octave); + return 1; + } + /*KEY_WWW or sustain*/ + else if (pm->midi_mode && bit_mask == 0x000004) { + /* sustain on/off*/ + pm->midi_sustain_mode ^= 0x1; + return 1; + } + + return 0; /* continue key processing */ +} + +static int pcmidi_handle_report3(struct pcmidi_snd *pm, u8 *data, int size) +{ + struct pcmidi_sustain *pms; + unsigned i, j; + unsigned char status, note, velocity; + + unsigned num_notes = (size-1)/2; + for (j = 0; j < num_notes; j++) { + note = data[j*2+1]; + velocity = data[j*2+2]; + + if (note < 0x81) { /* note on */ + status = 128 + 16 + pm->midi_channel; /* 1001nnnn */ + note = note - 0x54 + PCMIDI_MIDDLE_C + + (pm->midi_octave * 12); + if (0 == velocity) + velocity = 1; /* force note on */ + } else { /* note off */ + status = 128 + pm->midi_channel; /* 1000nnnn */ + note = note - 0x94 + PCMIDI_MIDDLE_C + + (pm->midi_octave*12); + + if (pm->midi_sustain_mode) { + for (i = 0; i < PCMIDI_SUSTAINED_MAX; i++) { + pms = &pm->sustained_notes[i]; + if (!pms->in_use) { + pms->status = status; + pms->note = note; + pms->velocity = velocity; + pms->in_use = 1; + + mod_timer(&pms->timer, + jiffies + + msecs_to_jiffies(pm->midi_sustain)); + return 1; + } + } + } + } + pcmidi_send_note(pm, status, note, velocity); + } + + return 1; +} + +static int pcmidi_handle_report4(struct pcmidi_snd *pm, u8 *data) +{ + unsigned key; + u32 bit_mask; + u32 bit_index; + + bit_mask = data[1]; + bit_mask = (bit_mask << 8) | data[2]; + bit_mask = (bit_mask << 8) | data[3]; + + /* break keys */ + for (bit_index = 0; bit_index < 24; bit_index++) { + key = pm->last_key[bit_index]; + if (!((0x01 << bit_index) & bit_mask)) { + input_event(pm->input_ep82, EV_KEY, + pm->last_key[bit_index], 0); + pm->last_key[bit_index] = 0; + } + } + + /* make keys */ + for (bit_index = 0; bit_index < 24; bit_index++) { + key = 0; + switch ((0x01 << bit_index) & bit_mask) { + case 0x000010: /* Fn lock*/ + pm->fn_state ^= 0x000010; + if (pm->fn_state) + pcmidi_submit_output_report(pm, 0xc5); + else + pcmidi_submit_output_report(pm, 0xc6); + continue; + case 0x020000: /* midi launcher..send a key (qwerty) or not? */ + pcmidi_submit_output_report(pm, 0xc1); + pm->midi_mode ^= 0x01; + + dbg_hid("pcmidi mode: %d\n", pm->midi_mode); + continue; + case 0x100000: /* KEY_MESSENGER or octave up */ + dbg_hid("pcmidi mode: %d\n", pm->midi_mode); + if (pm->midi_mode) { + pm->midi_octave++; + if (pm->midi_octave > 2) + pm->midi_octave = 2; + dbg_hid("pcmidi mode: %d octave: %d\n", + pm->midi_mode, pm->midi_octave); + continue; + } else + key = KEY_MESSENGER; + break; + case 0x400000: + key = KEY_CALENDAR; + break; + case 0x080000: + key = KEY_ADDRESSBOOK; + break; + case 0x040000: + key = KEY_DOCUMENTS; + break; + case 0x800000: + key = KEY_WORDPROCESSOR; + break; + case 0x200000: + key = KEY_SPREADSHEET; + break; + case 0x010000: + key = KEY_COFFEE; + break; + case 0x000100: + key = KEY_HELP; + break; + case 0x000200: + key = KEY_SEND; + break; + case 0x000400: + key = KEY_REPLY; + break; + case 0x000800: + key = KEY_FORWARDMAIL; + break; + case 0x001000: + key = KEY_NEW; + break; + case 0x002000: + key = KEY_OPEN; + break; + case 0x004000: + key = KEY_CLOSE; + break; + case 0x008000: + key = KEY_SAVE; + break; + case 0x000001: + key = KEY_UNDO; + break; + case 0x000002: + key = KEY_REDO; + break; + case 0x000004: + key = KEY_SPELLCHECK; + break; + case 0x000008: + key = KEY_PRINT; + break; + } + if (key) { + input_event(pm->input_ep82, EV_KEY, key, 1); + pm->last_key[bit_index] = key; + } + } + + return 1; +} + +int pcmidi_handle_report( + struct pcmidi_snd *pm, unsigned report_id, u8 *data, int size) +{ + int ret = 0; + + switch (report_id) { + case 0x01: /* midi keys (qwerty)*/ + ret = pcmidi_handle_report1(pm, data); + break; + case 0x03: /* midi keyboard (musical)*/ + ret = pcmidi_handle_report3(pm, data, size); + break; + case 0x04: /* multimedia/midi keys (qwerty)*/ + ret = pcmidi_handle_report4(pm, data); + break; + } + return ret; +} + +void pcmidi_setup_extra_keys(struct pcmidi_snd *pm, struct input_dev *input) +{ + /* reassigned functionality for N/A keys + MY PICTURES => KEY_WORDPROCESSOR + MY MUSIC=> KEY_SPREADSHEET + */ + unsigned int keys[] = { + KEY_FN, + KEY_MESSENGER, KEY_CALENDAR, + KEY_ADDRESSBOOK, KEY_DOCUMENTS, + KEY_WORDPROCESSOR, + KEY_SPREADSHEET, + KEY_COFFEE, + KEY_HELP, KEY_SEND, + KEY_REPLY, KEY_FORWARDMAIL, + KEY_NEW, KEY_OPEN, + KEY_CLOSE, KEY_SAVE, + KEY_UNDO, KEY_REDO, + KEY_SPELLCHECK, KEY_PRINT, + 0 + }; + + unsigned int *pkeys = &keys[0]; + unsigned short i; + + if (pm->ifnum != 1) /* only set up ONCE for interace 1 */ + return; + + pm->input_ep82 = input; + + for (i = 0; i < 24; i++) + pm->last_key[i] = 0; + + while (*pkeys != 0) { + set_bit(*pkeys, pm->input_ep82->keybit); + ++pkeys; + } +} + +static int pcmidi_set_operational(struct pcmidi_snd *pm) +{ + if (pm->ifnum != 1) + return 0; /* only set up ONCE for interace 1 */ + + pcmidi_get_output_report(pm); + pcmidi_submit_output_report(pm, 0xc1); + return 0; +} + +static int pcmidi_snd_free(struct snd_device *dev) +{ + return 0; +} + +static int pcmidi_in_open(struct snd_rawmidi_substream *substream) +{ + struct pcmidi_snd *pm = substream->rmidi->private_data; + + dbg_hid("pcmidi in open\n"); + pm->in_substream = substream; + return 0; +} + +static int pcmidi_in_close(struct snd_rawmidi_substream *substream) +{ + dbg_hid("pcmidi in close\n"); + return 0; +} + +static void pcmidi_in_trigger(struct snd_rawmidi_substream *substream, int up) +{ + struct pcmidi_snd *pm = substream->rmidi->private_data; + + dbg_hid("pcmidi in trigger %d\n", up); + + pm->in_triggered = up; +} + +static struct snd_rawmidi_ops pcmidi_in_ops = { + .open = pcmidi_in_open, + .close = pcmidi_in_close, + .trigger = pcmidi_in_trigger +}; + +int pcmidi_snd_initialise(struct pcmidi_snd *pm) +{ + static int dev; + struct snd_card *card; + struct snd_rawmidi *rwmidi; + int err; + + static struct snd_device_ops ops = { + .dev_free = pcmidi_snd_free, + }; + + if (pm->ifnum != 1) + return 0; /* only set up midi device ONCE for interace 1 */ + + if (dev >= SNDRV_CARDS) + return -ENODEV; + + if (!enable[dev]) { + dev++; + return -ENOENT; + } + + /* Setup sound card */ + + err = snd_card_create(index[dev], id[dev], THIS_MODULE, 0, &card); + if (err < 0) { + pk_error("failed to create pc-midi sound card\n"); + err = -ENOMEM; + goto fail; + } + pm->card = card; + + /* Setup sound device */ + err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, pm, &ops); + if (err < 0) { + pk_error("failed to create pc-midi sound device: error %d\n", + err); + goto fail; + } + + strncpy(card->driver, shortname, sizeof(card->driver)); + strncpy(card->shortname, shortname, sizeof(card->shortname)); + strncpy(card->longname, longname, sizeof(card->longname)); + + /* Set up rawmidi */ + err = snd_rawmidi_new(card, card->shortname, 0, + 0, 1, &rwmidi); + if (err < 0) { + pk_error("failed to create pc-midi rawmidi device: error %d\n", + err); + goto fail; + } + pm->rwmidi = rwmidi; + strncpy(rwmidi->name, card->shortname, sizeof(rwmidi->name)); + rwmidi->info_flags = SNDRV_RAWMIDI_INFO_INPUT; + rwmidi->private_data = pm; + + snd_rawmidi_set_ops(rwmidi, SNDRV_RAWMIDI_STREAM_INPUT, + &pcmidi_in_ops); + + snd_card_set_dev(card, &pm->pk->hdev->dev); + + /* create sysfs variables */ + err = device_create_file(&pm->pk->hdev->dev, + sysfs_device_attr_channel); + if (err < 0) { + pk_error("failed to create sysfs attribute channel: error %d\n", + err); + goto fail; + } + + err = device_create_file(&pm->pk->hdev->dev, + sysfs_device_attr_sustain); + if (err < 0) { + pk_error("failed to create sysfs attribute sustain: error %d\n", + err); + goto fail_attr_sustain; + } + + err = device_create_file(&pm->pk->hdev->dev, + sysfs_device_attr_octave); + if (err < 0) { + pk_error("failed to create sysfs attribute octave: error %d\n", + err); + goto fail_attr_octave; + } + + spin_lock_init(&pm->rawmidi_in_lock); + + init_sustain_timers(pm); + pcmidi_set_operational(pm); + + /* register it */ + err = snd_card_register(card); + if (err < 0) { + pk_error("failed to register pc-midi sound card: error %d\n", + err); + goto fail_register; + } + + dbg_hid("pcmidi_snd_initialise finished ok\n"); + return 0; + +fail_register: + stop_sustain_timers(pm); + device_remove_file(&pm->pk->hdev->dev, sysfs_device_attr_octave); +fail_attr_octave: + device_remove_file(&pm->pk->hdev->dev, sysfs_device_attr_sustain); +fail_attr_sustain: + device_remove_file(&pm->pk->hdev->dev, sysfs_device_attr_channel); +fail: + if (pm->card) { + snd_card_free(pm->card); + pm->card = NULL; + } + return err; +} + +int pcmidi_snd_terminate(struct pcmidi_snd *pm) +{ + if (pm->card) { + stop_sustain_timers(pm); + + device_remove_file(&pm->pk->hdev->dev, + sysfs_device_attr_channel); + device_remove_file(&pm->pk->hdev->dev, + sysfs_device_attr_sustain); + device_remove_file(&pm->pk->hdev->dev, + sysfs_device_attr_octave); + + snd_card_disconnect(pm->card); + snd_card_free_when_closed(pm->card); + } + + return 0; +} + +/* + * PC-MIDI report descriptor for report id is wrong. + */ +static void pk_report_fixup(struct hid_device *hdev, __u8 *rdesc, + unsigned int rsize) +{ + if (rsize == 178 && + rdesc[111] == 0x06 && rdesc[112] == 0x00 && + rdesc[113] == 0xff) { + dev_info(&hdev->dev, "fixing up pc-midi keyboard report " + "descriptor\n"); + + rdesc[144] = 0x18; /* report 4: was 0x10 report count */ + } +} + +static int pk_input_mapping(struct hid_device *hdev, struct hid_input *hi, + struct hid_field *field, struct hid_usage *usage, + unsigned long **bit, int *max) +{ + struct pk_device *pk = (struct pk_device *)hid_get_drvdata(hdev); + struct pcmidi_snd *pm; + + pm = pk->pm; + + if (HID_UP_MSVENDOR == (usage->hid & HID_USAGE_PAGE) && + 1 == pm->ifnum) { + pcmidi_setup_extra_keys(pm, hi->input); + return 0; + } + + return 0; +} + + +static int pk_raw_event(struct hid_device *hdev, struct hid_report *report, + u8 *data, int size) +{ + struct pk_device *pk = (struct pk_device *)hid_get_drvdata(hdev); + int ret = 0; + + if (1 == pk->pm->ifnum) { + if (report->id == data[0]) + switch (report->id) { + case 0x01: /* midi keys (qwerty)*/ + case 0x03: /* midi keyboard (musical)*/ + case 0x04: /* extra/midi keys (qwerty)*/ + ret = pcmidi_handle_report(pk->pm, + report->id, data, size); + break; + } + } + + return ret; +} + +static int pk_probe(struct hid_device *hdev, const struct hid_device_id *id) +{ + int ret; + struct usb_interface *intf = to_usb_interface(hdev->dev.parent); + unsigned short ifnum = intf->cur_altsetting->desc.bInterfaceNumber; + unsigned long quirks = id->driver_data; + struct pk_device *pk; + struct pcmidi_snd *pm = NULL; + + pk = kzalloc(sizeof(*pk), GFP_KERNEL); + if (pk == NULL) { + dev_err(&hdev->dev, "prodikeys: can't alloc descriptor\n"); + return -ENOMEM; + } + + pk->hdev = hdev; + + pm = kzalloc(sizeof(*pm), GFP_KERNEL); + if (pm == NULL) { + dev_err(&hdev->dev, + "prodikeys: can't alloc descriptor\n"); + ret = -ENOMEM; + goto err_free; + } + + pm->pk = pk; + pk->pm = pm; + pm->ifnum = ifnum; + + hid_set_drvdata(hdev, pk); + + ret = hid_parse(hdev); + if (ret) { + dev_err(&hdev->dev, "prodikeys: hid parse failed\n"); + goto err_free; + } + + if (quirks & PK_QUIRK_NOGET) { /* hid_parse cleared all the quirks */ + hdev->quirks |= HID_QUIRK_NOGET; + } + + ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT); + if (ret) { + dev_err(&hdev->dev, "prodikeys: hw start failed\n"); + goto err_free; + } + + ret = pcmidi_snd_initialise(pm); + if (ret < 0) + goto err_stop; + + return 0; +err_stop: + hid_hw_stop(hdev); +err_free: + if (pm != NULL) + kfree(pm); + + kfree(pk); + return ret; +} + +static void pk_remove(struct hid_device *hdev) +{ + struct pk_device *pk = (struct pk_device *)hid_get_drvdata(hdev); + struct pcmidi_snd *pm; + + pm = pk->pm; + if (pm) { + pcmidi_snd_terminate(pm); + kfree(pm); + } + + hid_hw_stop(hdev); + + kfree(pk); +} + +static const struct hid_device_id pk_devices[] = { + {HID_USB_DEVICE(USB_VENDOR_ID_CREATIVELABS, + USB_DEVICE_ID_PRODIKEYS_PCMIDI), + .driver_data = PK_QUIRK_NOGET}, + { } +}; +MODULE_DEVICE_TABLE(hid, pk_devices); + +static struct hid_driver pk_driver = { + .name = "prodikeys", + .id_table = pk_devices, + .report_fixup = pk_report_fixup, + .input_mapping = pk_input_mapping, + .raw_event = pk_raw_event, + .probe = pk_probe, + .remove = pk_remove, +}; + +static int pk_init(void) +{ + int ret; + + ret = hid_register_driver(&pk_driver); + if (ret) + printk(KERN_ERR "can't register prodikeys driver\n"); + + return ret; +} + +static void pk_exit(void) +{ + hid_unregister_driver(&pk_driver); +} + +module_init(pk_init); +module_exit(pk_exit); +MODULE_LICENSE("GPL"); -- cgit v1.2.3