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