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