rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | #include <linux/kernel.h> |
| 2 | #include <linux/slab.h> |
| 3 | #include <linux/module.h> |
| 4 | #include <linux/usb/input.h> |
| 5 | #include <asm/unaligned.h> |
| 6 | |
| 7 | /* |
| 8 | * Version Information |
| 9 | * v0.0.1 - Original, extremely basic version, 2.4.xx only |
| 10 | * v0.0.2 - Updated, works with 2.5.62 and 2.4.20; |
| 11 | * - added pressure-threshold modules param code from |
| 12 | * Alex Perry <alex.perry@ieee.org> |
| 13 | */ |
| 14 | |
| 15 | #define DRIVER_VERSION "v0.0.2" |
| 16 | #define DRIVER_AUTHOR "Josh Myer <josh@joshisanerd.com>" |
| 17 | #define DRIVER_DESC "USB KB Gear JamStudio Tablet driver" |
| 18 | #define DRIVER_LICENSE "GPL" |
| 19 | |
| 20 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 21 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 22 | MODULE_LICENSE(DRIVER_LICENSE); |
| 23 | |
| 24 | #define USB_VENDOR_ID_KBGEAR 0x084e |
| 25 | |
| 26 | static int kb_pressure_click = 0x10; |
| 27 | module_param(kb_pressure_click, int, 0); |
| 28 | MODULE_PARM_DESC(kb_pressure_click, "pressure threshold for clicks"); |
| 29 | |
| 30 | struct kbtab { |
| 31 | unsigned char *data; |
| 32 | dma_addr_t data_dma; |
| 33 | struct input_dev *dev; |
| 34 | struct usb_interface *intf; |
| 35 | struct urb *irq; |
| 36 | char phys[32]; |
| 37 | }; |
| 38 | |
| 39 | static void kbtab_irq(struct urb *urb) |
| 40 | { |
| 41 | struct kbtab *kbtab = urb->context; |
| 42 | unsigned char *data = kbtab->data; |
| 43 | struct input_dev *dev = kbtab->dev; |
| 44 | int pressure; |
| 45 | int retval; |
| 46 | |
| 47 | switch (urb->status) { |
| 48 | case 0: |
| 49 | /* success */ |
| 50 | break; |
| 51 | case -ECONNRESET: |
| 52 | case -ENOENT: |
| 53 | case -ESHUTDOWN: |
| 54 | /* this urb is terminated, clean up */ |
| 55 | dev_dbg(&kbtab->intf->dev, |
| 56 | "%s - urb shutting down with status: %d\n", |
| 57 | __func__, urb->status); |
| 58 | return; |
| 59 | default: |
| 60 | dev_dbg(&kbtab->intf->dev, |
| 61 | "%s - nonzero urb status received: %d\n", |
| 62 | __func__, urb->status); |
| 63 | goto exit; |
| 64 | } |
| 65 | |
| 66 | |
| 67 | input_report_key(dev, BTN_TOOL_PEN, 1); |
| 68 | |
| 69 | input_report_abs(dev, ABS_X, get_unaligned_le16(&data[1])); |
| 70 | input_report_abs(dev, ABS_Y, get_unaligned_le16(&data[3])); |
| 71 | |
| 72 | /*input_report_key(dev, BTN_TOUCH , data[0] & 0x01);*/ |
| 73 | input_report_key(dev, BTN_RIGHT, data[0] & 0x02); |
| 74 | |
| 75 | pressure = data[5]; |
| 76 | if (kb_pressure_click == -1) |
| 77 | input_report_abs(dev, ABS_PRESSURE, pressure); |
| 78 | else |
| 79 | input_report_key(dev, BTN_LEFT, pressure > kb_pressure_click ? 1 : 0); |
| 80 | |
| 81 | input_sync(dev); |
| 82 | |
| 83 | exit: |
| 84 | retval = usb_submit_urb(urb, GFP_ATOMIC); |
| 85 | if (retval) |
| 86 | dev_err(&kbtab->intf->dev, |
| 87 | "%s - usb_submit_urb failed with result %d\n", |
| 88 | __func__, retval); |
| 89 | } |
| 90 | |
| 91 | static const struct usb_device_id kbtab_ids[] = { |
| 92 | { USB_DEVICE(USB_VENDOR_ID_KBGEAR, 0x1001), .driver_info = 0 }, |
| 93 | { } |
| 94 | }; |
| 95 | |
| 96 | MODULE_DEVICE_TABLE(usb, kbtab_ids); |
| 97 | |
| 98 | static int kbtab_open(struct input_dev *dev) |
| 99 | { |
| 100 | struct kbtab *kbtab = input_get_drvdata(dev); |
| 101 | struct usb_device *udev = interface_to_usbdev(kbtab->intf); |
| 102 | |
| 103 | kbtab->irq->dev = udev; |
| 104 | if (usb_submit_urb(kbtab->irq, GFP_KERNEL)) |
| 105 | return -EIO; |
| 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | static void kbtab_close(struct input_dev *dev) |
| 111 | { |
| 112 | struct kbtab *kbtab = input_get_drvdata(dev); |
| 113 | |
| 114 | usb_kill_urb(kbtab->irq); |
| 115 | } |
| 116 | |
| 117 | static int kbtab_probe(struct usb_interface *intf, const struct usb_device_id *id) |
| 118 | { |
| 119 | struct usb_device *dev = interface_to_usbdev(intf); |
| 120 | struct usb_endpoint_descriptor *endpoint; |
| 121 | struct kbtab *kbtab; |
| 122 | struct input_dev *input_dev; |
| 123 | int error = -ENOMEM; |
| 124 | |
| 125 | if (intf->cur_altsetting->desc.bNumEndpoints < 1) |
| 126 | return -ENODEV; |
| 127 | |
| 128 | endpoint = &intf->cur_altsetting->endpoint[0].desc; |
| 129 | if (!usb_endpoint_is_int_in(endpoint)) |
| 130 | return -ENODEV; |
| 131 | |
| 132 | kbtab = kzalloc(sizeof(struct kbtab), GFP_KERNEL); |
| 133 | input_dev = input_allocate_device(); |
| 134 | if (!kbtab || !input_dev) |
| 135 | goto fail1; |
| 136 | |
| 137 | kbtab->data = usb_alloc_coherent(dev, 8, GFP_KERNEL, &kbtab->data_dma); |
| 138 | if (!kbtab->data) |
| 139 | goto fail1; |
| 140 | |
| 141 | kbtab->irq = usb_alloc_urb(0, GFP_KERNEL); |
| 142 | if (!kbtab->irq) |
| 143 | goto fail2; |
| 144 | |
| 145 | kbtab->intf = intf; |
| 146 | kbtab->dev = input_dev; |
| 147 | |
| 148 | usb_make_path(dev, kbtab->phys, sizeof(kbtab->phys)); |
| 149 | strlcat(kbtab->phys, "/input0", sizeof(kbtab->phys)); |
| 150 | |
| 151 | input_dev->name = "KB Gear Tablet"; |
| 152 | input_dev->phys = kbtab->phys; |
| 153 | usb_to_input_id(dev, &input_dev->id); |
| 154 | input_dev->dev.parent = &intf->dev; |
| 155 | |
| 156 | input_set_drvdata(input_dev, kbtab); |
| 157 | |
| 158 | input_dev->open = kbtab_open; |
| 159 | input_dev->close = kbtab_close; |
| 160 | |
| 161 | input_dev->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS); |
| 162 | input_dev->keybit[BIT_WORD(BTN_LEFT)] |= |
| 163 | BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_RIGHT); |
| 164 | input_dev->keybit[BIT_WORD(BTN_DIGI)] |= |
| 165 | BIT_MASK(BTN_TOOL_PEN) | BIT_MASK(BTN_TOUCH); |
| 166 | input_set_abs_params(input_dev, ABS_X, 0, 0x2000, 4, 0); |
| 167 | input_set_abs_params(input_dev, ABS_Y, 0, 0x1750, 4, 0); |
| 168 | input_set_abs_params(input_dev, ABS_PRESSURE, 0, 0xff, 0, 0); |
| 169 | |
| 170 | usb_fill_int_urb(kbtab->irq, dev, |
| 171 | usb_rcvintpipe(dev, endpoint->bEndpointAddress), |
| 172 | kbtab->data, 8, |
| 173 | kbtab_irq, kbtab, endpoint->bInterval); |
| 174 | kbtab->irq->transfer_dma = kbtab->data_dma; |
| 175 | kbtab->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
| 176 | |
| 177 | error = input_register_device(kbtab->dev); |
| 178 | if (error) |
| 179 | goto fail3; |
| 180 | |
| 181 | usb_set_intfdata(intf, kbtab); |
| 182 | |
| 183 | return 0; |
| 184 | |
| 185 | fail3: usb_free_urb(kbtab->irq); |
| 186 | fail2: usb_free_coherent(dev, 8, kbtab->data, kbtab->data_dma); |
| 187 | fail1: input_free_device(input_dev); |
| 188 | kfree(kbtab); |
| 189 | return error; |
| 190 | } |
| 191 | |
| 192 | static void kbtab_disconnect(struct usb_interface *intf) |
| 193 | { |
| 194 | struct kbtab *kbtab = usb_get_intfdata(intf); |
| 195 | struct usb_device *udev = interface_to_usbdev(intf); |
| 196 | |
| 197 | usb_set_intfdata(intf, NULL); |
| 198 | |
| 199 | input_unregister_device(kbtab->dev); |
| 200 | usb_free_urb(kbtab->irq); |
| 201 | usb_free_coherent(udev, 8, kbtab->data, kbtab->data_dma); |
| 202 | kfree(kbtab); |
| 203 | } |
| 204 | |
| 205 | static struct usb_driver kbtab_driver = { |
| 206 | .name = "kbtab", |
| 207 | .probe = kbtab_probe, |
| 208 | .disconnect = kbtab_disconnect, |
| 209 | .id_table = kbtab_ids, |
| 210 | }; |
| 211 | |
| 212 | module_usb_driver(kbtab_driver); |