Advertisement
Guest User

hid-kye.c

a guest
May 7th, 2012
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 18.96 KB | None | 0 0
  1. /*
  2.  *  HID driver for Kye/Genius devices not fully compliant with HID standard
  3.  *
  4.  *  Copyright (c) 2009 Jiri Kosina
  5.  *  Copyright (c) 2009 Tomas Hanak
  6.  *  Copyright (c) 2012 Nikolai Kondrashov
  7.  */
  8.  
  9. /*
  10.  * This program is free software; you can redistribute it and/or modify it
  11.  * under the terms of the GNU General Public License as published by the Free
  12.  * Software Foundation; either version 2 of the License, or (at your option)
  13.  * any later version.
  14.  */
  15.  
  16. #include <linux/device.h>
  17. #include <linux/hid.h>
  18. #include <linux/module.h>
  19. #include <linux/usb.h>
  20. #include "usbhid/usbhid.h"
  21.  
  22.  
  23. #include "hid-ids.h"
  24.  
  25. /* the fixups that need to be done:
  26.  *   - change led usage page to button for extra buttons
  27.  *   - report size 8 count 1 must be size 1 count 8 for button bitfield
  28.  *   - change the button usage range to 4-7 for the extra buttons
  29.  */
  30.  
  31. /*
  32.  * See EasyPen i405X description, device and HID report descriptors at
  33.  * http://sf.net/apps/mediawiki/digimend/?title=KYE_EasyPen_i405X
  34.   */
  35.  
  36. /* Original EasyPen i405X report descriptor size */
  37. #define EASYPEN_I405X_RDESC_ORIG_SIZE   476
  38.  
  39. /* Fixed EasyPen i405X report descriptor */
  40. static __u8 easypen_i405x_rdesc_fixed[] = {
  41.     0x06, 0x00, 0xFF, /*  Usage Page (FF00h),             */
  42.     0x09, 0x01,       /*  Usage (01h),                    */
  43.     0xA1, 0x01,       /*  Collection (Application),       */
  44.     0x85, 0x05,       /*    Report ID (5),                */
  45.     0x09, 0x01,       /*    Usage (01h),                  */
  46.     0x15, 0x80,       /*    Logical Minimum (-128),       */
  47.     0x25, 0x7F,       /*    Logical Maximum (127),        */
  48.     0x75, 0x08,       /*    Report Size (8),              */
  49.     0x95, 0x07,       /*    Report Count (7),             */
  50.     0xB1, 0x02,       /*    Feature (Variable),           */
  51.     0xC0,             /*  End Collection,                 */
  52.     0x05, 0x0D,       /*  Usage Page (Digitizer),         */
  53.     0x09, 0x02,       /*  Usage (Pen),                    */
  54.     0xA1, 0x01,       /*  Collection (Application),       */
  55.     0x85, 0x10,       /*    Report ID (16),               */
  56.     0x09, 0x20,       /*    Usage (Stylus),               */
  57.     0xA0,             /*    Collection (Physical),        */
  58.     0x14,             /*      Logical Minimum (0),        */
  59.     0x25, 0x01,       /*      Logical Maximum (1),        */
  60.     0x75, 0x01,       /*      Report Size (1),            */
  61.     0x09, 0x42,       /*      Usage (Tip Switch),         */
  62.     0x09, 0x44,       /*      Usage (Barrel Switch),      */
  63.     0x09, 0x46,       /*      Usage (Tablet Pick),        */
  64.     0x95, 0x03,       /*      Report Count (3),           */
  65.     0x81, 0x02,       /*      Input (Variable),           */
  66.     0x95, 0x04,       /*      Report Count (4),           */
  67.     0x81, 0x03,       /*      Input (Constant, Variable), */
  68.     0x09, 0x32,       /*      Usage (In Range),           */
  69.     0x95, 0x01,       /*      Report Count (1),           */
  70.     0x81, 0x02,       /*      Input (Variable),           */
  71.     0x75, 0x10,       /*      Report Size (16),           */
  72.     0x95, 0x01,       /*      Report Count (1),           */
  73.     0xA4,             /*      Push,                       */
  74.     0x05, 0x01,       /*      Usage Page (Desktop),       */
  75.     0x55, 0xFD,       /*      Unit Exponent (-3),         */
  76.     0x65, 0x13,       /*      Unit (Inch),                */
  77.     0x34,             /*      Physical Minimum (0),       */
  78.     0x09, 0x30,       /*      Usage (X),                  */
  79.     0x46, 0x7C, 0x15, /*      Physical Maximum (5500),    */
  80.     0x26, 0x00, 0x37, /*      Logical Maximum (14080),    */
  81.     0x81, 0x02,       /*      Input (Variable),           */
  82.     0x09, 0x31,       /*      Usage (Y),                  */
  83.     0x46, 0xA0, 0x0F, /*      Physical Maximum (4000),    */
  84.     0x26, 0x00, 0x28, /*      Logical Maximum (10240),    */
  85.     0x81, 0x02,       /*      Input (Variable),           */
  86.     0xB4,             /*      Pop,                        */
  87.     0x09, 0x30,       /*      Usage (Tip Pressure),       */
  88.     0x26, 0xFF, 0x03, /*      Logical Maximum (1023),     */
  89.     0x81, 0x02,       /*      Input (Variable),           */
  90.     0xC0,             /*    End Collection,               */
  91.     0xC0              /*  End Collection                  */
  92. };
  93.  
  94. /*
  95.  * See MousePen i608X description, device and HID report descriptors at
  96.  * http://sf.net/apps/mediawiki/digimend/?title=KYE_MousePen_i608X
  97.  */
  98.  
  99. /* Original MousePen i608X report descriptor size */
  100. #define MOUSEPEN_I608X_RDESC_ORIG_SIZE  476
  101.  
  102. /* Fixed MousePen i608X report descriptor */
  103. static __u8 mousepen_i608x_rdesc_fixed[] = {
  104.     0x06, 0x00, 0xFF, /*  Usage Page (FF00h),             */
  105.     0x09, 0x01,       /*  Usage (01h),                    */
  106.     0xA1, 0x01,       /*  Collection (Application),       */
  107.     0x85, 0x05,       /*    Report ID (5),                */
  108.     0x09, 0x01,       /*    Usage (01h),                  */
  109.     0x15, 0x80,       /*    Logical Minimum (-128),       */
  110.     0x25, 0x7F,       /*    Logical Maximum (127),        */
  111.     0x75, 0x08,       /*    Report Size (8),              */
  112.     0x95, 0x07,       /*    Report Count (7),             */
  113.     0xB1, 0x02,       /*    Feature (Variable),           */
  114.     0xC0,             /*  End Collection,                 */
  115.     0x05, 0x0D,       /*  Usage Page (Digitizer),         */
  116.     0x09, 0x02,       /*  Usage (Pen),                    */
  117.     0xA1, 0x01,       /*  Collection (Application),       */
  118.     0x85, 0x10,       /*    Report ID (16),               */
  119.     0x09, 0x20,       /*    Usage (Stylus),               */
  120.     0xA0,             /*    Collection (Physical),        */
  121.     0x14,             /*      Logical Minimum (0),        */
  122.     0x25, 0x01,       /*      Logical Maximum (1),        */
  123.     0x75, 0x01,       /*      Report Size (1),            */
  124.     0x09, 0x42,       /*      Usage (Tip Switch),         */
  125.     0x09, 0x44,       /*      Usage (Barrel Switch),      */
  126.     0x09, 0x46,       /*      Usage (Tablet Pick),        */
  127.     0x95, 0x03,       /*      Report Count (3),           */
  128.     0x81, 0x02,       /*      Input (Variable),           */
  129.     0x95, 0x04,       /*      Report Count (4),           */
  130.     0x81, 0x03,       /*      Input (Constant, Variable), */
  131.     0x09, 0x32,       /*      Usage (In Range),           */
  132.     0x95, 0x01,       /*      Report Count (1),           */
  133.     0x81, 0x02,       /*      Input (Variable),           */
  134.     0x75, 0x10,       /*      Report Size (16),           */
  135.     0x95, 0x01,       /*      Report Count (1),           */
  136.     0xA4,             /*      Push,                       */
  137.     0x05, 0x01,       /*      Usage Page (Desktop),       */
  138.     0x55, 0xFD,       /*      Unit Exponent (-3),         */
  139.     0x65, 0x13,       /*      Unit (Inch),                */
  140.     0x34,             /*      Physical Minimum (0),       */
  141.     0x09, 0x30,       /*      Usage (X),                  */
  142.     0x46, 0x40, 0x1F, /*      Physical Maximum (8000),    */
  143.     0x26, 0x00, 0x50, /*      Logical Maximum (20480),    */
  144.     0x81, 0x02,       /*      Input (Variable),           */
  145.     0x09, 0x31,       /*      Usage (Y),                  */
  146.     0x46, 0x70, 0x17, /*      Physical Maximum (6000),    */
  147.     0x26, 0x00, 0x3C, /*      Logical Maximum (15360),    */
  148.     0x81, 0x02,       /*      Input (Variable),           */
  149.     0xB4,             /*      Pop,                        */
  150.     0x09, 0x30,       /*      Usage (Tip Pressure),       */
  151.     0x26, 0xFF, 0x03, /*      Logical Maximum (1023),     */
  152.     0x81, 0x02,       /*      Input (Variable),           */
  153.     0xC0,             /*    End Collection,               */
  154.     0xC0,             /*  End Collection,                 */
  155.     0x05, 0x01,       /*  Usage Page (Desktop),           */
  156.     0x09, 0x02,       /*  Usage (Mouse),                  */
  157.     0xA1, 0x01,       /*  Collection (Application),       */
  158.     0x85, 0x11,       /*    Report ID (17),               */
  159.     0x09, 0x01,       /*    Usage (Pointer),              */
  160.     0xA0,             /*    Collection (Physical),        */
  161.     0x14,             /*      Logical Minimum (0),        */
  162.     0xA4,             /*      Push,                       */
  163.     0x05, 0x09,       /*      Usage Page (Button),        */
  164.     0x75, 0x01,       /*      Report Size (1),            */
  165.     0x19, 0x01,       /*      Usage Minimum (01h),        */
  166.     0x29, 0x03,       /*      Usage Maximum (03h),        */
  167.     0x25, 0x01,       /*      Logical Maximum (1),        */
  168.     0x95, 0x03,       /*      Report Count (3),           */
  169.     0x81, 0x02,       /*      Input (Variable),           */
  170.     0x95, 0x05,       /*      Report Count (5),           */
  171.     0x81, 0x01,       /*      Input (Constant),           */
  172.     0xB4,             /*      Pop,                        */
  173.     0x95, 0x01,       /*      Report Count (1),           */
  174.     0xA4,             /*      Push,                       */
  175.     0x55, 0xFD,       /*      Unit Exponent (-3),         */
  176.     0x65, 0x13,       /*      Unit (Inch),                */
  177.     0x34,             /*      Physical Minimum (0),       */
  178.     0x75, 0x10,       /*      Report Size (16),           */
  179.     0x09, 0x30,       /*      Usage (X),                  */
  180.     0x46, 0x40, 0x1F, /*      Physical Maximum (8000),    */
  181.     0x26, 0x00, 0x50, /*      Logical Maximum (20480),    */
  182.     0x81, 0x02,       /*      Input (Variable),           */
  183.     0x09, 0x31,       /*      Usage (Y),                  */
  184.     0x46, 0x70, 0x17, /*      Physical Maximum (6000),    */
  185.     0x26, 0x00, 0x3C, /*      Logical Maximum (15360),    */
  186.     0x81, 0x02,       /*      Input (Variable),           */
  187.     0xB4,             /*      Pop,                        */
  188.     0x75, 0x08,       /*      Report Size (8),            */
  189.     0x09, 0x38,       /*      Usage (Wheel),              */
  190.     0x15, 0xFF,       /*      Logical Minimum (-1),       */
  191.     0x25, 0x01,       /*      Logical Maximum (1),        */
  192.     0x81, 0x06,       /*      Input (Variable, Relative), */
  193.     0x81, 0x01,       /*      Input (Constant),           */
  194.     0xC0,             /*    End Collection,               */
  195.     0xC0              /*  End Collection                  */
  196. };
  197.  
  198. /*
  199.  * See EasyPen M610X description, device and HID report descriptors at
  200.  * http://sf.net/apps/mediawiki/digimend/?title=KYE_EasyPen_M610X
  201.  */
  202.  
  203. /* Original EasyPen M610X report descriptor size */
  204. #define EASYPEN_M610X_RDESC_ORIG_SIZE   476
  205.  
  206. /* Fixed EasyPen M610X report descriptor */
  207. static __u8 easypen_m610x_rdesc_fixed[] = {
  208.     0x06, 0x00, 0xFF,             /*  Usage Page (FF00h),             */
  209.     0x09, 0x01,                   /*  Usage (01h),                    */
  210.     0xA1, 0x01,                   /*  Collection (Application),       */
  211.     0x85, 0x05,                   /*    Report ID (5),                */
  212.     0x09, 0x01,                   /*    Usage (01h),                  */
  213.     0x15, 0x80,                   /*    Logical Minimum (-128),       */
  214.     0x25, 0x7F,                   /*    Logical Maximum (127),        */
  215.     0x75, 0x08,                   /*    Report Size (8),              */
  216.     0x95, 0x07,                   /*    Report Count (7),             */
  217.     0xB1, 0x02,                   /*    Feature (Variable),           */
  218.     0xC0,                         /*  End Collection,                 */
  219.     0x05, 0x0D,                   /*  Usage Page (Digitizer),         */
  220.     0x09, 0x02,                   /*  Usage (Pen),                    */
  221.     0xA1, 0x01,                   /*  Collection (Application),       */
  222.     0x85, 0x10,                   /*    Report ID (16),               */
  223.     0x09, 0x20,                   /*    Usage (Stylus),               */
  224.     0xA0,                         /*    Collection (Physical),        */
  225.     0x14,                         /*      Logical Minimum (0),        */
  226.     0x25, 0x01,                   /*      Logical Maximum (1),        */
  227.     0x75, 0x01,                   /*      Report Size (1),            */
  228.     0x09, 0x42,                   /*      Usage (Tip Switch),         */
  229.     0x09, 0x44,                   /*      Usage (Barrel Switch),      */
  230.     0x09, 0x46,                   /*      Usage (Tablet Pick),        */
  231.     0x95, 0x03,                   /*      Report Count (3),           */
  232.     0x81, 0x02,                   /*      Input (Variable),           */
  233.     0x95, 0x04,                   /*      Report Count (4),           */
  234.     0x81, 0x03,                   /*      Input (Constant, Variable), */
  235.     0x09, 0x32,                   /*      Usage (In Range),           */
  236.     0x95, 0x01,                   /*      Report Count (1),           */
  237.     0x81, 0x02,                   /*      Input (Variable),           */
  238.     0x75, 0x10,                   /*      Report Size (16),           */
  239.     0x95, 0x01,                   /*      Report Count (1),           */
  240.     0xA4,                         /*      Push,                       */
  241.     0x05, 0x01,                   /*      Usage Page (Desktop),       */
  242.     0x55, 0xFD,                   /*      Unit Exponent (-3),         */
  243.     0x65, 0x13,                   /*      Unit (Inch),                */
  244.     0x34,                         /*      Physical Minimum (0),       */
  245.     0x09, 0x30,                   /*      Usage (X),                  */
  246.     0x46, 0x10, 0x27,             /*      Physical Maximum (10000),   */
  247.     0x27, 0x00, 0xA0, 0x00, 0x00, /*      Logical Maximum (40960),    */
  248.     0x81, 0x02,                   /*      Input (Variable),           */
  249.     0x09, 0x31,                   /*      Usage (Y),                  */
  250.     0x46, 0x6A, 0x18,             /*      Physical Maximum (6250),    */
  251.     0x26, 0x00, 0x64,             /*      Logical Maximum (25600),    */
  252.     0x81, 0x02,                   /*      Input (Variable),           */
  253.     0xB4,                         /*      Pop,                        */
  254.     0x09, 0x30,                   /*      Usage (Tip Pressure),       */
  255.     0x26, 0xFF, 0x03,             /*      Logical Maximum (1023),     */
  256.     0x81, 0x02,                   /*      Input (Variable),           */
  257.     0xC0,                         /*    End Collection,               */
  258.     0xC0,                         /*  End Collection,                 */
  259.     0x05, 0x0C,                   /*  Usage Page (Consumer),          */
  260.     0x09, 0x01,                   /*  Usage (Consumer Control),       */
  261.     0xA1, 0x01,                   /*  Collection (Application),       */
  262.     0x85, 0x12,                   /*    Report ID (18),               */
  263.     0x14,                         /*    Logical Minimum (0),          */
  264.     0x25, 0x01,                   /*    Logical Maximum (1),          */
  265.     0x75, 0x01,                   /*    Report Size (1),              */
  266.     0x95, 0x04,                   /*    Report Count (4),             */
  267.     0x0A, 0x1A, 0x02,             /*    Usage (AC Undo),              */
  268.     0x0A, 0x79, 0x02,             /*    Usage (AC Redo Or Repeat),    */
  269.     0x0A, 0x2D, 0x02,             /*    Usage (AC Zoom In),           */
  270.     0x0A, 0x2E, 0x02,             /*    Usage (AC Zoom Out),          */
  271.     0x81, 0x02,                   /*    Input (Variable),             */
  272.     0x95, 0x01,                   /*    Report Count (1),             */
  273.     0x75, 0x14,                   /*    Report Size (20),             */
  274.     0x81, 0x03,                   /*    Input (Constant, Variable),   */
  275.     0x75, 0x20,                   /*    Report Size (32),             */
  276.     0x81, 0x03,                   /*    Input (Constant, Variable),   */
  277.     0xC0                          /*  End Collection                  */
  278. };
  279.  
  280.  
  281. static __u8 *kye_report_fixup(struct hid_device *hdev, __u8 *rdesc,
  282.         unsigned int *rsize)
  283. {
  284.  
  285.     switch (hdev->product) {
  286.     case USB_DEVICE_ID_KYE_ERGO_525V:
  287.         /* the fixups that need to be done:
  288.          *   - change led usage page to button for extra buttons
  289.          *   - report size 8 count 1 must be size 1 count 8 for button
  290.          *     bitfield
  291.          *   - change the button usage range to 4-7 for the extra
  292.          *     buttons
  293.          */
  294.         if (*rsize >= 74 &&
  295.             rdesc[61] == 0x05 && rdesc[62] == 0x08 &&
  296.             rdesc[63] == 0x19 && rdesc[64] == 0x08 &&
  297.             rdesc[65] == 0x29 && rdesc[66] == 0x0f &&
  298.             rdesc[71] == 0x75 && rdesc[72] == 0x08 &&
  299.             rdesc[73] == 0x95 && rdesc[74] == 0x01) {
  300.             dev_info(&hdev->dev,
  301.                  "fixing up Kye/Genius Ergo Mouse "
  302.                  "report descriptor\n");
  303.             rdesc[62] = 0x09;
  304.             rdesc[64] = 0x04;
  305.             rdesc[66] = 0x07;
  306.             rdesc[72] = 0x01;
  307.             rdesc[74] = 0x08;
  308.         }
  309.         break;
  310.     case USB_DEVICE_ID_KYE_EASYPEN_I405X:
  311.         if (*rsize == EASYPEN_I405X_RDESC_ORIG_SIZE) {
  312.             rdesc = easypen_i405x_rdesc_fixed;
  313.             *rsize = sizeof(easypen_i405x_rdesc_fixed);
  314.         }
  315.         break;
  316.     case USB_DEVICE_ID_KYE_MOUSEPEN_I608X:
  317.         if (*rsize == MOUSEPEN_I608X_RDESC_ORIG_SIZE) {
  318.             rdesc = mousepen_i608x_rdesc_fixed;
  319.             *rsize = sizeof(mousepen_i608x_rdesc_fixed);
  320.         }
  321.         break;
  322.     case USB_DEVICE_ID_KYE_EASYPEN_M610X:
  323.         if (*rsize == EASYPEN_M610X_RDESC_ORIG_SIZE) {
  324.             rdesc = easypen_m610x_rdesc_fixed;
  325.             *rsize = sizeof(easypen_m610x_rdesc_fixed);
  326.         }
  327.         break;
  328.  
  329.     }
  330.     return rdesc;
  331. }
  332.  
  333.  
  334. /**
  335.  * Enable fully-functional tablet mode by setting a special feature report.
  336.  *
  337.  * @hdev:   HID device
  338.  *
  339.  * The specific report ID and data were discovered by sniffing the
  340.  * Windows driver traffic.
  341.  */
  342. static int kye_tablet_enable(struct hid_device *hdev)
  343. {
  344.     struct list_head *list;
  345.     struct list_head *head;
  346.     struct hid_report *report;
  347.     __s32 *value;
  348.  
  349.     list = &hdev->report_enum[HID_FEATURE_REPORT].report_list;
  350.     list_for_each(head, list) {
  351.         report = list_entry(head, struct hid_report, list);
  352.         if (report->id == 5)
  353.             break;
  354.     }
  355.  
  356.     if (head == list) {
  357.         dev_err(&hdev->dev,
  358.             "tablet-enabling feature report not found\n");
  359.         return -ENODEV;
  360.     }
  361.  
  362.     if (report->maxfield < 1 || report->field[0]->report_count < 7) {
  363.         dev_err(&hdev->dev,
  364.             "invalid tablet-enabling feature report\n");
  365.         return -ENODEV;
  366.     }
  367.  
  368.     value = report->field[0]->value;
  369.  
  370.     value[0] = 0x12;
  371.     value[1] = 0x10;
  372.     value[2] = 0x11;
  373.     value[3] = 0x12;
  374.     value[4] = 0x00;
  375.     value[5] = 0x00;
  376.     value[6] = 0x00;
  377.     usbhid_submit_report(hdev, report, USB_DIR_OUT);
  378.  
  379.     return 0;
  380. }
  381.  
  382. static int kye_probe(struct hid_device *hdev, const struct hid_device_id *id)
  383. {
  384.     int ret;
  385.  
  386.     ret = hid_parse(hdev);
  387.     if (ret) {
  388.         dev_err(&hdev->dev, "parse failed\n");
  389.         goto err;
  390.     }
  391.  
  392.     ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  393.     if (ret) {
  394.         dev_err(&hdev->dev, "hw start failed\n");
  395.         goto err;
  396.     }
  397.  
  398.     switch (id->product) {
  399.     case USB_DEVICE_ID_KYE_EASYPEN_I405X:
  400.     case USB_DEVICE_ID_KYE_MOUSEPEN_I608X:
  401.     case USB_DEVICE_ID_KYE_EASYPEN_M610X:
  402.         ret = kye_tablet_enable(hdev);
  403.         if (ret) {
  404.             dev_err(&hdev->dev, "tablet enabling failed\n");
  405.             goto enabling_err;
  406.         }
  407.         break;
  408.     }
  409.  
  410.     return 0;
  411. enabling_err:
  412.     hid_hw_stop(hdev);
  413. err:
  414.     return ret;
  415. }
  416.  
  417.  static const struct hid_device_id kye_devices[] = {
  418.     { HID_USB_DEVICE(USB_VENDOR_ID_KYE, USB_DEVICE_ID_KYE_ERGO_525V) },
  419.     { HID_USB_DEVICE(USB_VENDOR_ID_KYE,
  420.                 USB_DEVICE_ID_KYE_EASYPEN_I405X) },
  421.     { HID_USB_DEVICE(USB_VENDOR_ID_KYE,
  422.                 USB_DEVICE_ID_KYE_MOUSEPEN_I608X) },
  423.     { HID_USB_DEVICE(USB_VENDOR_ID_KYE,
  424.                 USB_DEVICE_ID_KYE_EASYPEN_M610X) },
  425.     { }
  426.  };
  427.  MODULE_DEVICE_TABLE(hid, kye_devices);
  428.  
  429.  
  430.  
  431. static struct hid_driver kye_driver = {
  432.     .name = "kye",
  433.     .id_table = kye_devices,
  434.     .probe = kye_probe,
  435.     .report_fixup = kye_report_fixup,
  436. };
  437.  
  438. static int __init kye_init(void)
  439. {
  440.     return hid_register_driver(&kye_driver);
  441. }
  442.  
  443. static void __exit kye_exit(void)
  444. {
  445.     hid_unregister_driver(&kye_driver);
  446. }
  447.  
  448. module_init(kye_init);
  449. module_exit(kye_exit);
  450. MODULE_LICENSE("GPL");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement