^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (c) 1999-2001 Vojtech Pavlik
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * USB HIDBP Keyboard support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^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) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * Should you need to contact me, the author, you can do so either by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/usb/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/hid.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * Version Information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #define DRIVER_VERSION ""
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #define DRIVER_AUTHOR "Vojtech Pavlik <vojtech@ucw.cz>"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #define DRIVER_DESC "USB HID Boot Protocol keyboard driver"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) MODULE_AUTHOR(DRIVER_AUTHOR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) MODULE_DESCRIPTION(DRIVER_DESC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) static const unsigned char usb_kbd_keycode[256] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) 0, 0, 0, 0, 30, 48, 46, 32, 18, 33, 34, 35, 23, 36, 37, 38,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) 50, 49, 24, 25, 16, 19, 31, 20, 22, 47, 17, 45, 21, 44, 2, 3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) 4, 5, 6, 7, 8, 9, 10, 11, 28, 1, 14, 15, 57, 12, 13, 26,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) 27, 43, 43, 39, 40, 41, 51, 52, 53, 58, 59, 60, 61, 62, 63, 64,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) 65, 66, 67, 68, 87, 88, 99, 70,119,110,102,104,111,107,109,106,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) 105,108,103, 69, 98, 55, 74, 78, 96, 79, 80, 81, 75, 76, 77, 71,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) 72, 73, 82, 83, 86,127,116,117,183,184,185,186,187,188,189,190,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) 191,192,193,194,134,138,130,132,128,129,131,137,133,135,136,113,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) 115,114, 0, 0, 0,121, 0, 89, 93,124, 92, 94, 95, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) 122,123, 90, 91, 85, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) 29, 42, 56,125, 97, 54,100,126,164,166,165,163,161,115,114,113,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) 150,158,159,128,136,177,178,176,142,152,173,140
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * struct usb_kbd - state of each attached keyboard
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * @dev: input device associated with this keyboard
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * @usbdev: usb device associated with this keyboard
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * @old: data received in the past from the @irq URB representing which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * keys were pressed. By comparing with the current list of keys
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * that are pressed, we are able to see key releases.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * @irq: URB for receiving a list of keys that are pressed when a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * new key is pressed or a key that was pressed is released.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * @led: URB for sending LEDs (e.g. numlock, ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * @newleds: data that will be sent with the @led URB representing which LEDs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) should be on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * @name: Name of the keyboard. @dev's name field points to this buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * @phys: Physical path of the keyboard. @dev's phys field points to this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * @new: Buffer for the @irq URB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * @cr: Control request for @led URB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * @leds: Buffer for the @led URB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * @new_dma: DMA address for @irq URB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * @leds_dma: DMA address for @led URB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * @leds_lock: spinlock that protects @leds, @newleds, and @led_urb_submitted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) * @led_urb_submitted: indicates whether @led is in progress, i.e. it has been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * submitted and its completion handler has not returned yet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * without resubmitting @led
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) struct usb_kbd {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) struct input_dev *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) struct usb_device *usbdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) unsigned char old[8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) struct urb *irq, *led;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) unsigned char newleds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) char name[128];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) char phys[64];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) unsigned char *new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) struct usb_ctrlrequest *cr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) unsigned char *leds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) dma_addr_t new_dma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) dma_addr_t leds_dma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) spinlock_t leds_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) bool led_urb_submitted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static void usb_kbd_irq(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) struct usb_kbd *kbd = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) switch (urb->status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) case 0: /* success */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) case -ECONNRESET: /* unlink */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) case -ENOENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) case -ESHUTDOWN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) /* -EPIPE: should clear the halt */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) default: /* error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) goto resubmit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) for (i = 0; i < 8; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) input_report_key(kbd->dev, usb_kbd_keycode[i + 224], (kbd->new[0] >> i) & 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) for (i = 2; i < 8; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (kbd->old[i] > 3 && memscan(kbd->new + 2, kbd->old[i], 6) == kbd->new + 8) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) if (usb_kbd_keycode[kbd->old[i]])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) input_report_key(kbd->dev, usb_kbd_keycode[kbd->old[i]], 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) hid_info(urb->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) "Unknown key (scancode %#x) released.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) kbd->old[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) if (kbd->new[i] > 3 && memscan(kbd->old + 2, kbd->new[i], 6) == kbd->old + 8) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (usb_kbd_keycode[kbd->new[i]])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) input_report_key(kbd->dev, usb_kbd_keycode[kbd->new[i]], 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) hid_info(urb->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) "Unknown key (scancode %#x) pressed.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) kbd->new[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) input_sync(kbd->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) memcpy(kbd->old, kbd->new, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) resubmit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) i = usb_submit_urb (urb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) if (i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) hid_err(urb->dev, "can't resubmit intr, %s-%s/input0, status %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) kbd->usbdev->bus->bus_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) kbd->usbdev->devpath, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) static int usb_kbd_event(struct input_dev *dev, unsigned int type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) unsigned int code, int value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) struct usb_kbd *kbd = input_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) if (type != EV_LED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) spin_lock_irqsave(&kbd->leds_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) kbd->newleds = (!!test_bit(LED_KANA, dev->led) << 3) | (!!test_bit(LED_COMPOSE, dev->led) << 3) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) (!!test_bit(LED_SCROLLL, dev->led) << 2) | (!!test_bit(LED_CAPSL, dev->led) << 1) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) (!!test_bit(LED_NUML, dev->led));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) if (kbd->led_urb_submitted){
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) spin_unlock_irqrestore(&kbd->leds_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) if (*(kbd->leds) == kbd->newleds){
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) spin_unlock_irqrestore(&kbd->leds_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) *(kbd->leds) = kbd->newleds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) kbd->led->dev = kbd->usbdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if (usb_submit_urb(kbd->led, GFP_ATOMIC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) pr_err("usb_submit_urb(leds) failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) kbd->led_urb_submitted = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) spin_unlock_irqrestore(&kbd->leds_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) static void usb_kbd_led(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) struct usb_kbd *kbd = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) if (urb->status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) hid_warn(urb->dev, "led urb status %d received\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) urb->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) spin_lock_irqsave(&kbd->leds_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) if (*(kbd->leds) == kbd->newleds){
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) kbd->led_urb_submitted = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) spin_unlock_irqrestore(&kbd->leds_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) *(kbd->leds) = kbd->newleds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) kbd->led->dev = kbd->usbdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) if (usb_submit_urb(kbd->led, GFP_ATOMIC)){
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) hid_err(urb->dev, "usb_submit_urb(leds) failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) kbd->led_urb_submitted = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) spin_unlock_irqrestore(&kbd->leds_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) static int usb_kbd_open(struct input_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) struct usb_kbd *kbd = input_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) kbd->irq->dev = kbd->usbdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) if (usb_submit_urb(kbd->irq, GFP_KERNEL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) static void usb_kbd_close(struct input_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) struct usb_kbd *kbd = input_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) usb_kill_urb(kbd->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) static int usb_kbd_alloc_mem(struct usb_device *dev, struct usb_kbd *kbd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) if (!(kbd->irq = usb_alloc_urb(0, GFP_KERNEL)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) if (!(kbd->led = usb_alloc_urb(0, GFP_KERNEL)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) if (!(kbd->new = usb_alloc_coherent(dev, 8, GFP_ATOMIC, &kbd->new_dma)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) if (!(kbd->cr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) if (!(kbd->leds = usb_alloc_coherent(dev, 1, GFP_ATOMIC, &kbd->leds_dma)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) static void usb_kbd_free_mem(struct usb_device *dev, struct usb_kbd *kbd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) usb_free_urb(kbd->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) usb_free_urb(kbd->led);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) usb_free_coherent(dev, 8, kbd->new, kbd->new_dma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) kfree(kbd->cr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) usb_free_coherent(dev, 1, kbd->leds, kbd->leds_dma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) static int usb_kbd_probe(struct usb_interface *iface,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) const struct usb_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) struct usb_device *dev = interface_to_usbdev(iface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) struct usb_host_interface *interface;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) struct usb_endpoint_descriptor *endpoint;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) struct usb_kbd *kbd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) struct input_dev *input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) int i, pipe, maxp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) int error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) interface = iface->cur_altsetting;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) if (interface->desc.bNumEndpoints != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) endpoint = &interface->endpoint[0].desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) if (!usb_endpoint_is_int_in(endpoint))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) kbd = kzalloc(sizeof(struct usb_kbd), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) input_dev = input_allocate_device();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) if (!kbd || !input_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) goto fail1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) if (usb_kbd_alloc_mem(dev, kbd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) goto fail2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) kbd->usbdev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) kbd->dev = input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) spin_lock_init(&kbd->leds_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) if (dev->manufacturer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) strlcpy(kbd->name, dev->manufacturer, sizeof(kbd->name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) if (dev->product) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) if (dev->manufacturer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) strlcat(kbd->name, " ", sizeof(kbd->name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) strlcat(kbd->name, dev->product, sizeof(kbd->name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) if (!strlen(kbd->name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) snprintf(kbd->name, sizeof(kbd->name),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) "USB HIDBP Keyboard %04x:%04x",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) le16_to_cpu(dev->descriptor.idVendor),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) le16_to_cpu(dev->descriptor.idProduct));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) usb_make_path(dev, kbd->phys, sizeof(kbd->phys));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) strlcat(kbd->phys, "/input0", sizeof(kbd->phys));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) input_dev->name = kbd->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) input_dev->phys = kbd->phys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) usb_to_input_id(dev, &input_dev->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) input_dev->dev.parent = &iface->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) input_set_drvdata(input_dev, kbd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_LED) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) BIT_MASK(EV_REP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) input_dev->ledbit[0] = BIT_MASK(LED_NUML) | BIT_MASK(LED_CAPSL) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) BIT_MASK(LED_SCROLLL) | BIT_MASK(LED_COMPOSE) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) BIT_MASK(LED_KANA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) for (i = 0; i < 255; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) set_bit(usb_kbd_keycode[i], input_dev->keybit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) clear_bit(0, input_dev->keybit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) input_dev->event = usb_kbd_event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) input_dev->open = usb_kbd_open;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) input_dev->close = usb_kbd_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) usb_fill_int_urb(kbd->irq, dev, pipe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) kbd->new, (maxp > 8 ? 8 : maxp),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) usb_kbd_irq, kbd, endpoint->bInterval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) kbd->irq->transfer_dma = kbd->new_dma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) kbd->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) kbd->cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) kbd->cr->bRequest = 0x09;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) kbd->cr->wValue = cpu_to_le16(0x200);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) kbd->cr->wIndex = cpu_to_le16(interface->desc.bInterfaceNumber);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) kbd->cr->wLength = cpu_to_le16(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) usb_fill_control_urb(kbd->led, dev, usb_sndctrlpipe(dev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) (void *) kbd->cr, kbd->leds, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) usb_kbd_led, kbd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) kbd->led->transfer_dma = kbd->leds_dma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) kbd->led->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) error = input_register_device(kbd->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) goto fail2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) usb_set_intfdata(iface, kbd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) device_set_wakeup_enable(&dev->dev, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) fail2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) usb_kbd_free_mem(dev, kbd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) fail1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) input_free_device(input_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) kfree(kbd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) static void usb_kbd_disconnect(struct usb_interface *intf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) struct usb_kbd *kbd = usb_get_intfdata (intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) usb_set_intfdata(intf, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) if (kbd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) usb_kill_urb(kbd->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) input_unregister_device(kbd->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) usb_kill_urb(kbd->led);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) usb_kbd_free_mem(interface_to_usbdev(intf), kbd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) kfree(kbd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) static const struct usb_device_id usb_kbd_id_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) { USB_INTERFACE_INFO(USB_INTERFACE_CLASS_HID, USB_INTERFACE_SUBCLASS_BOOT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) USB_INTERFACE_PROTOCOL_KEYBOARD) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) { } /* Terminating entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) MODULE_DEVICE_TABLE (usb, usb_kbd_id_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) static struct usb_driver usb_kbd_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) .name = "usbkbd",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) .probe = usb_kbd_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) .disconnect = usb_kbd_disconnect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) .id_table = usb_kbd_id_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) module_usb_driver(usb_kbd_driver);