| 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * USB ATI Remote support |
| 4 | * |
| 5 | * Copyright (c) 2011, 2012 Anssi Hannula <anssi.hannula@iki.fi> |
| 6 | * Version 2.2.0 Copyright (c) 2004 Torrey Hoffman <thoffman@arnor.net> |
| 7 | * Version 2.1.1 Copyright (c) 2002 Vladimir Dergachev |
| 8 | * |
| 9 | * This 2.2.0 version is a rewrite / cleanup of the 2.1.1 driver, including |
| 10 | * porting to the 2.6 kernel interfaces, along with other modification |
| 11 | * to better match the style of the existing usb/input drivers. However, the |
| 12 | * protocol and hardware handling is essentially unchanged from 2.1.1. |
| 13 | * |
| 14 | * The 2.1.1 driver was derived from the usbati_remote and usbkbd drivers by |
| 15 | * Vojtech Pavlik. |
| 16 | * |
| 17 | * Changes: |
| 18 | * |
| 19 | * Feb 2004: Torrey Hoffman <thoffman@arnor.net> |
| 20 | * Version 2.2.0 |
| 21 | * Jun 2004: Torrey Hoffman <thoffman@arnor.net> |
| 22 | * Version 2.2.1 |
| 23 | * Added key repeat support contributed by: |
| 24 | * Vincent Vanackere <vanackere@lif.univ-mrs.fr> |
| 25 | * Added support for the "Lola" remote contributed by: |
| 26 | * Seth Cohn <sethcohn@yahoo.com> |
| 27 | * |
| 28 | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
| 29 | * |
| 30 | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
| 31 | * |
| 32 | * Hardware & software notes |
| 33 | * |
| 34 | * These remote controls are distributed by ATI as part of their |
| 35 | * "All-In-Wonder" video card packages. The receiver self-identifies as a |
| 36 | * "USB Receiver" with manufacturer "X10 Wireless Technology Inc". |
| 37 | * |
| 38 | * The "Lola" remote is available from X10. See: |
| 39 | * http://www.x10.com/products/lola_sg1.htm |
| 40 | * The Lola is similar to the ATI remote but has no mouse support, and slightly |
| 41 | * different keys. |
| 42 | * |
| 43 | * It is possible to use multiple receivers and remotes on multiple computers |
| 44 | * simultaneously by configuring them to use specific channels. |
| 45 | * |
| 46 | * The RF protocol used by the remote supports 16 distinct channels, 1 to 16. |
| 47 | * Actually, it may even support more, at least in some revisions of the |
| 48 | * hardware. |
| 49 | * |
| 50 | * Each remote can be configured to transmit on one channel as follows: |
| 51 | * - Press and hold the "hand icon" button. |
| 52 | * - When the red LED starts to blink, let go of the "hand icon" button. |
| 53 | * - When it stops blinking, input the channel code as two digits, from 01 |
| 54 | * to 16, and press the hand icon again. |
| 55 | * |
| 56 | * The timing can be a little tricky. Try loading the module with debug=1 |
| 57 | * to have the kernel print out messages about the remote control number |
| 58 | * and mask. Note: debugging prints remote numbers as zero-based hexadecimal. |
| 59 | * |
| 60 | * The driver has a "channel_mask" parameter. This bitmask specifies which |
| 61 | * channels will be ignored by the module. To mask out channels, just add |
| 62 | * all the 2^channel_number values together. |
| 63 | * |
| 64 | * For instance, set channel_mask = 2^4 = 16 (binary 10000) to make ati_remote |
| 65 | * ignore signals coming from remote controls transmitting on channel 4, but |
| 66 | * accept all other channels. |
| 67 | * |
| 68 | * Or, set channel_mask = 65533, (0xFFFD), and all channels except 1 will be |
| 69 | * ignored. |
| 70 | * |
| 71 | * The default is 0 (respond to all channels). Bit 0 and bits 17-32 of this |
| 72 | * parameter are unused. |
| 73 | */ |
| 74 | |
| 75 | #include <linux/kernel.h> |
| 76 | #include <linux/errno.h> |
| 77 | #include <linux/init.h> |
| 78 | #include <linux/slab.h> |
| 79 | #include <linux/module.h> |
| 80 | #include <linux/mutex.h> |
| 81 | #include <linux/usb/input.h> |
| 82 | #include <linux/wait.h> |
| 83 | #include <linux/jiffies.h> |
| 84 | #include <media/rc-core.h> |
| 85 | |
| 86 | /* |
| 87 | * Module and Version Information, Module Parameters |
| 88 | */ |
| 89 | |
| 90 | #define ATI_REMOTE_VENDOR_ID 0x0bc7 |
| 91 | #define LOLA_REMOTE_PRODUCT_ID 0x0002 |
| 92 | #define LOLA2_REMOTE_PRODUCT_ID 0x0003 |
| 93 | #define ATI_REMOTE_PRODUCT_ID 0x0004 |
| 94 | #define NVIDIA_REMOTE_PRODUCT_ID 0x0005 |
| 95 | #define MEDION_REMOTE_PRODUCT_ID 0x0006 |
| 96 | #define FIREFLY_REMOTE_PRODUCT_ID 0x0008 |
| 97 | |
| 98 | #define DRIVER_VERSION "2.2.1" |
| 99 | #define DRIVER_AUTHOR "Torrey Hoffman <thoffman@arnor.net>" |
| 100 | #define DRIVER_DESC "ATI/X10 RF USB Remote Control" |
| 101 | |
| 102 | #define NAME_BUFSIZE 80 /* size of product name, path buffers */ |
| 103 | #define DATA_BUFSIZE 63 /* size of URB data buffers */ |
| 104 | |
| 105 | /* |
| 106 | * Duplicate event filtering time. |
| 107 | * Sequential, identical KIND_FILTERED inputs with less than |
| 108 | * FILTER_TIME milliseconds between them are considered as repeat |
| 109 | * events. The hardware generates 5 events for the first keypress |
| 110 | * and we have to take this into account for an accurate repeat |
| 111 | * behaviour. |
| 112 | */ |
| 113 | #define FILTER_TIME 60 /* msec */ |
| 114 | #define REPEAT_DELAY 500 /* msec */ |
| 115 | |
| 116 | static unsigned long channel_mask; |
| 117 | module_param(channel_mask, ulong, 0644); |
| 118 | MODULE_PARM_DESC(channel_mask, "Bitmask of remote control channels to ignore" ); |
| 119 | |
| 120 | static int debug; |
| 121 | module_param(debug, int, 0644); |
| 122 | MODULE_PARM_DESC(debug, "Enable extra debug messages and information" ); |
| 123 | |
| 124 | static int repeat_filter = FILTER_TIME; |
| 125 | module_param(repeat_filter, int, 0644); |
| 126 | MODULE_PARM_DESC(repeat_filter, "Repeat filter time, default = 60 msec" ); |
| 127 | |
| 128 | static int repeat_delay = REPEAT_DELAY; |
| 129 | module_param(repeat_delay, int, 0644); |
| 130 | MODULE_PARM_DESC(repeat_delay, "Delay before sending repeats, default = 500 msec" ); |
| 131 | |
| 132 | static bool mouse = true; |
| 133 | module_param(mouse, bool, 0444); |
| 134 | MODULE_PARM_DESC(mouse, "Enable mouse device, default = yes" ); |
| 135 | |
| 136 | #define dbginfo(dev, format, arg...) \ |
| 137 | do { if (debug) dev_info(dev , format , ## arg); } while (0) |
| 138 | |
| 139 | struct ati_receiver_type { |
| 140 | /* either default_keymap or get_default_keymap should be set */ |
| 141 | const char *default_keymap; |
| 142 | const char *(*get_default_keymap)(struct usb_interface *interface); |
| 143 | }; |
| 144 | |
| 145 | static const char *get_medion_keymap(struct usb_interface *interface) |
| 146 | { |
| 147 | struct usb_device *udev = interface_to_usbdev(interface); |
| 148 | |
| 149 | /* |
| 150 | * There are many different Medion remotes shipped with a receiver |
| 151 | * with the same usb id, but the receivers have subtle differences |
| 152 | * in the USB descriptors allowing us to detect them. |
| 153 | */ |
| 154 | |
| 155 | if (udev->manufacturer && udev->product) { |
| 156 | if (udev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_WAKEUP) { |
| 157 | |
| 158 | if (!strcmp(udev->manufacturer, "X10 Wireless Technology Inc" ) |
| 159 | && !strcmp(udev->product, "USB Receiver" )) |
| 160 | return RC_MAP_MEDION_X10_DIGITAINER; |
| 161 | |
| 162 | if (!strcmp(udev->manufacturer, "X10 WTI" ) |
| 163 | && !strcmp(udev->product, "RF receiver" )) |
| 164 | return RC_MAP_MEDION_X10_OR2X; |
| 165 | } else { |
| 166 | |
| 167 | if (!strcmp(udev->manufacturer, "X10 Wireless Technology Inc" ) |
| 168 | && !strcmp(udev->product, "USB Receiver" )) |
| 169 | return RC_MAP_MEDION_X10; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | dev_info(&interface->dev, |
| 174 | "Unknown Medion X10 receiver, using default ati_remote Medion keymap\n" ); |
| 175 | |
| 176 | return RC_MAP_MEDION_X10; |
| 177 | } |
| 178 | |
| 179 | static const struct ati_receiver_type type_ati = { |
| 180 | .default_keymap = RC_MAP_ATI_X10 |
| 181 | }; |
| 182 | static const struct ati_receiver_type type_medion = { |
| 183 | .get_default_keymap = get_medion_keymap |
| 184 | }; |
| 185 | static const struct ati_receiver_type type_firefly = { |
| 186 | .default_keymap = RC_MAP_SNAPSTREAM_FIREFLY |
| 187 | }; |
| 188 | |
| 189 | static const struct usb_device_id ati_remote_table[] = { |
| 190 | { |
| 191 | USB_DEVICE(ATI_REMOTE_VENDOR_ID, LOLA_REMOTE_PRODUCT_ID), |
| 192 | .driver_info = (unsigned long)&type_ati |
| 193 | }, |
| 194 | { |
| 195 | USB_DEVICE(ATI_REMOTE_VENDOR_ID, LOLA2_REMOTE_PRODUCT_ID), |
| 196 | .driver_info = (unsigned long)&type_ati |
| 197 | }, |
| 198 | { |
| 199 | USB_DEVICE(ATI_REMOTE_VENDOR_ID, ATI_REMOTE_PRODUCT_ID), |
| 200 | .driver_info = (unsigned long)&type_ati |
| 201 | }, |
| 202 | { |
| 203 | USB_DEVICE(ATI_REMOTE_VENDOR_ID, NVIDIA_REMOTE_PRODUCT_ID), |
| 204 | .driver_info = (unsigned long)&type_ati |
| 205 | }, |
| 206 | { |
| 207 | USB_DEVICE(ATI_REMOTE_VENDOR_ID, MEDION_REMOTE_PRODUCT_ID), |
| 208 | .driver_info = (unsigned long)&type_medion |
| 209 | }, |
| 210 | { |
| 211 | USB_DEVICE(ATI_REMOTE_VENDOR_ID, FIREFLY_REMOTE_PRODUCT_ID), |
| 212 | .driver_info = (unsigned long)&type_firefly |
| 213 | }, |
| 214 | {} /* Terminating entry */ |
| 215 | }; |
| 216 | |
| 217 | MODULE_DEVICE_TABLE(usb, ati_remote_table); |
| 218 | |
| 219 | /* Get hi and low bytes of a 16-bits int */ |
| 220 | #define HI(a) ((unsigned char)((a) >> 8)) |
| 221 | #define LO(a) ((unsigned char)((a) & 0xff)) |
| 222 | |
| 223 | #define SEND_FLAG_IN_PROGRESS 1 |
| 224 | #define SEND_FLAG_COMPLETE 2 |
| 225 | |
| 226 | /* Device initialization strings */ |
| 227 | static char init1[] = { 0x01, 0x00, 0x20, 0x14 }; |
| 228 | static char init2[] = { 0x01, 0x00, 0x20, 0x14, 0x20, 0x20, 0x20 }; |
| 229 | |
| 230 | struct ati_remote { |
| 231 | struct input_dev *idev; |
| 232 | struct rc_dev *rdev; |
| 233 | struct usb_device *udev; |
| 234 | struct usb_interface *interface; |
| 235 | |
| 236 | struct urb *irq_urb; |
| 237 | struct urb *out_urb; |
| 238 | struct usb_endpoint_descriptor *endpoint_in; |
| 239 | struct usb_endpoint_descriptor *endpoint_out; |
| 240 | unsigned char *inbuf; |
| 241 | unsigned char *outbuf; |
| 242 | dma_addr_t inbuf_dma; |
| 243 | dma_addr_t outbuf_dma; |
| 244 | |
| 245 | unsigned char old_data; /* Detect duplicate events */ |
| 246 | unsigned long old_jiffies; |
| 247 | unsigned long acc_jiffies; /* handle acceleration */ |
| 248 | unsigned long first_jiffies; |
| 249 | |
| 250 | unsigned int repeat_count; |
| 251 | |
| 252 | char rc_name[NAME_BUFSIZE]; |
| 253 | char rc_phys[NAME_BUFSIZE]; |
| 254 | char mouse_name[NAME_BUFSIZE + 6]; |
| 255 | char mouse_phys[NAME_BUFSIZE]; |
| 256 | |
| 257 | wait_queue_head_t wait; |
| 258 | int send_flags; |
| 259 | |
| 260 | int users; /* 0-2, users are rc and input */ |
| 261 | struct mutex open_mutex; |
| 262 | }; |
| 263 | |
| 264 | /* "Kinds" of messages sent from the hardware to the driver. */ |
| 265 | #define KIND_END 0 |
| 266 | #define KIND_LITERAL 1 /* Simply pass to input system as EV_KEY */ |
| 267 | #define KIND_FILTERED 2 /* Add artificial key-up events, drop keyrepeats */ |
| 268 | #define KIND_ACCEL 3 /* Translate to EV_REL mouse-move events */ |
| 269 | |
| 270 | /* Translation table from hardware messages to input events. */ |
| 271 | static const struct { |
| 272 | unsigned char kind; |
| 273 | unsigned char data; /* Raw key code from remote */ |
| 274 | unsigned short code; /* Input layer translation */ |
| 275 | } ati_remote_tbl[] = { |
| 276 | /* Directional control pad axes. Code is xxyy */ |
| 277 | {KIND_ACCEL, 0x70, 0xff00}, /* left */ |
| 278 | {KIND_ACCEL, 0x71, 0x0100}, /* right */ |
| 279 | {KIND_ACCEL, 0x72, 0x00ff}, /* up */ |
| 280 | {KIND_ACCEL, 0x73, 0x0001}, /* down */ |
| 281 | |
| 282 | /* Directional control pad diagonals */ |
| 283 | {KIND_ACCEL, 0x74, 0xffff}, /* left up */ |
| 284 | {KIND_ACCEL, 0x75, 0x01ff}, /* right up */ |
| 285 | {KIND_ACCEL, 0x77, 0xff01}, /* left down */ |
| 286 | {KIND_ACCEL, 0x76, 0x0101}, /* right down */ |
| 287 | |
| 288 | /* "Mouse button" buttons. The code below uses the fact that the |
| 289 | * lsbit of the raw code is a down/up indicator. */ |
| 290 | {KIND_LITERAL, 0x78, BTN_LEFT}, /* left btn down */ |
| 291 | {KIND_LITERAL, 0x79, BTN_LEFT}, /* left btn up */ |
| 292 | {KIND_LITERAL, 0x7c, BTN_RIGHT},/* right btn down */ |
| 293 | {KIND_LITERAL, 0x7d, BTN_RIGHT},/* right btn up */ |
| 294 | |
| 295 | /* Artificial "double-click" events are generated by the hardware. |
| 296 | * They are mapped to the "side" and "extra" mouse buttons here. */ |
| 297 | {KIND_FILTERED, 0x7a, BTN_SIDE}, /* left dblclick */ |
| 298 | {KIND_FILTERED, 0x7e, BTN_EXTRA},/* right dblclick */ |
| 299 | |
| 300 | /* Non-mouse events are handled by rc-core */ |
| 301 | {KIND_END, 0x00, 0} |
| 302 | }; |
| 303 | |
| 304 | /* |
| 305 | * ati_remote_dump_input |
| 306 | */ |
| 307 | static void ati_remote_dump(struct device *dev, unsigned char *data, |
| 308 | unsigned int len) |
| 309 | { |
| 310 | if (len == 1) { |
| 311 | if (data[0] != (unsigned char)0xff && data[0] != 0x00) |
| 312 | dev_warn(dev, "Weird byte 0x%02x\n" , data[0]); |
| 313 | } else if (len == 4) |
| 314 | dev_warn(dev, "Weird key %4ph\n" , data); |
| 315 | else |
| 316 | dev_warn(dev, "Weird data, len=%d %6ph ...\n" , len, data); |
| 317 | } |
| 318 | |
| 319 | /* |
| 320 | * ati_remote_open |
| 321 | */ |
| 322 | static int ati_remote_open(struct ati_remote *ati_remote) |
| 323 | { |
| 324 | int err = 0; |
| 325 | |
| 326 | mutex_lock(&ati_remote->open_mutex); |
| 327 | |
| 328 | if (ati_remote->users++ != 0) |
| 329 | goto out; /* one was already active */ |
| 330 | |
| 331 | /* On first open, submit the read urb which was set up previously. */ |
| 332 | ati_remote->irq_urb->dev = ati_remote->udev; |
| 333 | if (usb_submit_urb(urb: ati_remote->irq_urb, GFP_KERNEL)) { |
| 334 | dev_err(&ati_remote->interface->dev, |
| 335 | "%s: usb_submit_urb failed!\n" , __func__); |
| 336 | err = -EIO; |
| 337 | } |
| 338 | |
| 339 | out: mutex_unlock(lock: &ati_remote->open_mutex); |
| 340 | return err; |
| 341 | } |
| 342 | |
| 343 | /* |
| 344 | * ati_remote_close |
| 345 | */ |
| 346 | static void ati_remote_close(struct ati_remote *ati_remote) |
| 347 | { |
| 348 | mutex_lock(&ati_remote->open_mutex); |
| 349 | if (--ati_remote->users == 0) |
| 350 | usb_kill_urb(urb: ati_remote->irq_urb); |
| 351 | mutex_unlock(lock: &ati_remote->open_mutex); |
| 352 | } |
| 353 | |
| 354 | static int ati_remote_input_open(struct input_dev *inputdev) |
| 355 | { |
| 356 | struct ati_remote *ati_remote = input_get_drvdata(dev: inputdev); |
| 357 | return ati_remote_open(ati_remote); |
| 358 | } |
| 359 | |
| 360 | static void ati_remote_input_close(struct input_dev *inputdev) |
| 361 | { |
| 362 | struct ati_remote *ati_remote = input_get_drvdata(dev: inputdev); |
| 363 | ati_remote_close(ati_remote); |
| 364 | } |
| 365 | |
| 366 | static int ati_remote_rc_open(struct rc_dev *rdev) |
| 367 | { |
| 368 | struct ati_remote *ati_remote = rdev->priv; |
| 369 | return ati_remote_open(ati_remote); |
| 370 | } |
| 371 | |
| 372 | static void ati_remote_rc_close(struct rc_dev *rdev) |
| 373 | { |
| 374 | struct ati_remote *ati_remote = rdev->priv; |
| 375 | ati_remote_close(ati_remote); |
| 376 | } |
| 377 | |
| 378 | /* |
| 379 | * ati_remote_irq_out |
| 380 | */ |
| 381 | static void ati_remote_irq_out(struct urb *urb) |
| 382 | { |
| 383 | struct ati_remote *ati_remote = urb->context; |
| 384 | |
| 385 | if (urb->status) { |
| 386 | dev_dbg(&ati_remote->interface->dev, "%s: status %d\n" , |
| 387 | __func__, urb->status); |
| 388 | return; |
| 389 | } |
| 390 | |
| 391 | ati_remote->send_flags |= SEND_FLAG_COMPLETE; |
| 392 | wmb(); |
| 393 | wake_up(&ati_remote->wait); |
| 394 | } |
| 395 | |
| 396 | /* |
| 397 | * ati_remote_sendpacket |
| 398 | * |
| 399 | * Used to send device initialization strings |
| 400 | */ |
| 401 | static int ati_remote_sendpacket(struct ati_remote *ati_remote, u16 cmd, |
| 402 | unsigned char *data) |
| 403 | { |
| 404 | int retval = 0; |
| 405 | |
| 406 | /* Set up out_urb */ |
| 407 | memcpy(ati_remote->out_urb->transfer_buffer + 1, data, LO(cmd)); |
| 408 | ((char *) ati_remote->out_urb->transfer_buffer)[0] = HI(cmd); |
| 409 | |
| 410 | ati_remote->out_urb->transfer_buffer_length = LO(cmd) + 1; |
| 411 | ati_remote->out_urb->dev = ati_remote->udev; |
| 412 | ati_remote->send_flags = SEND_FLAG_IN_PROGRESS; |
| 413 | |
| 414 | retval = usb_submit_urb(urb: ati_remote->out_urb, GFP_ATOMIC); |
| 415 | if (retval) { |
| 416 | dev_dbg(&ati_remote->interface->dev, |
| 417 | "sendpacket: usb_submit_urb failed: %d\n" , retval); |
| 418 | return retval; |
| 419 | } |
| 420 | |
| 421 | wait_event_timeout(ati_remote->wait, |
| 422 | ((ati_remote->out_urb->status != -EINPROGRESS) || |
| 423 | (ati_remote->send_flags & SEND_FLAG_COMPLETE)), |
| 424 | HZ); |
| 425 | usb_kill_urb(urb: ati_remote->out_urb); |
| 426 | |
| 427 | return retval; |
| 428 | } |
| 429 | |
| 430 | struct accel_times { |
| 431 | const char value; |
| 432 | unsigned int msecs; |
| 433 | }; |
| 434 | |
| 435 | static const struct accel_times accel[] = { |
| 436 | { 1, 125 }, |
| 437 | { 2, 250 }, |
| 438 | { 4, 500 }, |
| 439 | { 6, 1000 }, |
| 440 | { 9, 1500 }, |
| 441 | { 13, 2000 }, |
| 442 | { 20, 0 }, |
| 443 | }; |
| 444 | |
| 445 | /* |
| 446 | * ati_remote_compute_accel |
| 447 | * |
| 448 | * Implements acceleration curve for directional control pad |
| 449 | * If elapsed time since last event is > 1/4 second, user "stopped", |
| 450 | * so reset acceleration. Otherwise, user is probably holding the control |
| 451 | * pad down, so we increase acceleration, ramping up over two seconds to |
| 452 | * a maximum speed. |
| 453 | */ |
| 454 | static int ati_remote_compute_accel(struct ati_remote *ati_remote) |
| 455 | { |
| 456 | unsigned long now = jiffies, reset_time; |
| 457 | int i; |
| 458 | |
| 459 | reset_time = msecs_to_jiffies(m: 250); |
| 460 | |
| 461 | if (time_after(now, ati_remote->old_jiffies + reset_time)) { |
| 462 | ati_remote->acc_jiffies = now; |
| 463 | return 1; |
| 464 | } |
| 465 | for (i = 0; i < ARRAY_SIZE(accel) - 1; i++) { |
| 466 | unsigned long timeout = msecs_to_jiffies(m: accel[i].msecs); |
| 467 | |
| 468 | if (time_before(now, ati_remote->acc_jiffies + timeout)) |
| 469 | return accel[i].value; |
| 470 | } |
| 471 | return accel[i].value; |
| 472 | } |
| 473 | |
| 474 | /* |
| 475 | * ati_remote_report_input |
| 476 | */ |
| 477 | static void ati_remote_input_report(struct urb *urb) |
| 478 | { |
| 479 | struct ati_remote *ati_remote = urb->context; |
| 480 | unsigned char *data= ati_remote->inbuf; |
| 481 | struct input_dev *dev = ati_remote->idev; |
| 482 | int index = -1; |
| 483 | int remote_num; |
| 484 | unsigned char scancode; |
| 485 | u32 wheel_keycode = KEY_RESERVED; |
| 486 | int i; |
| 487 | |
| 488 | /* |
| 489 | * data[0] = 0x14 |
| 490 | * data[1] = data[2] + data[3] + 0xd5 (a checksum byte) |
| 491 | * data[2] = the key code (with toggle bit in MSB with some models) |
| 492 | * data[3] = channel << 4 (the low 4 bits must be zero) |
| 493 | */ |
| 494 | |
| 495 | /* Deal with strange looking inputs */ |
| 496 | if ( urb->actual_length != 4 || data[0] != 0x14 || |
| 497 | data[1] != (unsigned char)(data[2] + data[3] + 0xD5) || |
| 498 | (data[3] & 0x0f) != 0x00) { |
| 499 | ati_remote_dump(dev: &urb->dev->dev, data, len: urb->actual_length); |
| 500 | return; |
| 501 | } |
| 502 | |
| 503 | if (data[1] != ((data[2] + data[3] + 0xd5) & 0xff)) { |
| 504 | dbginfo(&ati_remote->interface->dev, |
| 505 | "wrong checksum in input: %4ph\n" , data); |
| 506 | return; |
| 507 | } |
| 508 | |
| 509 | /* Mask unwanted remote channels. */ |
| 510 | /* note: remote_num is 0-based, channel 1 on remote == 0 here */ |
| 511 | remote_num = (data[3] >> 4) & 0x0f; |
| 512 | if (channel_mask & (1 << (remote_num + 1))) { |
| 513 | dbginfo(&ati_remote->interface->dev, |
| 514 | "Masked input from channel 0x%02x: data %02x, mask= 0x%02lx\n" , |
| 515 | remote_num, data[2], channel_mask); |
| 516 | return; |
| 517 | } |
| 518 | |
| 519 | /* |
| 520 | * MSB is a toggle code, though only used by some devices |
| 521 | * (e.g. SnapStream Firefly) |
| 522 | */ |
| 523 | scancode = data[2] & 0x7f; |
| 524 | |
| 525 | dbginfo(&ati_remote->interface->dev, |
| 526 | "channel 0x%02x; key data %02x, scancode %02x\n" , |
| 527 | remote_num, data[2], scancode); |
| 528 | |
| 529 | if (scancode >= 0x70) { |
| 530 | /* |
| 531 | * This is either a mouse or scrollwheel event, depending on |
| 532 | * the remote/keymap. |
| 533 | * Get the keycode assigned to scancode 0x78/0x70. If it is |
| 534 | * set, assume this is a scrollwheel up/down event. |
| 535 | */ |
| 536 | wheel_keycode = rc_g_keycode_from_table(dev: ati_remote->rdev, |
| 537 | scancode: scancode & 0x78); |
| 538 | |
| 539 | if (wheel_keycode == KEY_RESERVED) { |
| 540 | /* scrollwheel was not mapped, assume mouse */ |
| 541 | |
| 542 | /* Look up event code index in the mouse translation |
| 543 | * table. |
| 544 | */ |
| 545 | for (i = 0; ati_remote_tbl[i].kind != KIND_END; i++) { |
| 546 | if (scancode == ati_remote_tbl[i].data) { |
| 547 | index = i; |
| 548 | break; |
| 549 | } |
| 550 | } |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | if (index >= 0 && ati_remote_tbl[index].kind == KIND_LITERAL) { |
| 555 | /* |
| 556 | * The lsbit of the raw key code is a down/up flag. |
| 557 | * Invert it to match the input layer's conventions. |
| 558 | */ |
| 559 | input_event(dev, EV_KEY, code: ati_remote_tbl[index].code, |
| 560 | value: !(data[2] & 1)); |
| 561 | |
| 562 | ati_remote->old_jiffies = jiffies; |
| 563 | |
| 564 | } else if (index < 0 || ati_remote_tbl[index].kind == KIND_FILTERED) { |
| 565 | unsigned long now = jiffies; |
| 566 | |
| 567 | /* Filter duplicate events which happen "too close" together. */ |
| 568 | if (ati_remote->old_data == data[2] && |
| 569 | time_before(now, ati_remote->old_jiffies + |
| 570 | msecs_to_jiffies(repeat_filter))) { |
| 571 | ati_remote->repeat_count++; |
| 572 | } else { |
| 573 | ati_remote->repeat_count = 0; |
| 574 | ati_remote->first_jiffies = now; |
| 575 | } |
| 576 | |
| 577 | ati_remote->old_jiffies = now; |
| 578 | |
| 579 | /* Ensure we skip at least the 4 first duplicate events |
| 580 | * (generated by a single keypress), and continue skipping |
| 581 | * until repeat_delay msecs have passed. |
| 582 | */ |
| 583 | if (ati_remote->repeat_count > 0 && |
| 584 | (ati_remote->repeat_count < 5 || |
| 585 | time_before(now, ati_remote->first_jiffies + |
| 586 | msecs_to_jiffies(repeat_delay)))) |
| 587 | return; |
| 588 | |
| 589 | if (index >= 0) { |
| 590 | input_event(dev, EV_KEY, code: ati_remote_tbl[index].code, value: 1); |
| 591 | input_event(dev, EV_KEY, code: ati_remote_tbl[index].code, value: 0); |
| 592 | } else { |
| 593 | /* Not a mouse event, hand it to rc-core. */ |
| 594 | int count = 1; |
| 595 | |
| 596 | if (wheel_keycode != KEY_RESERVED) { |
| 597 | /* |
| 598 | * This is a scrollwheel event, send the |
| 599 | * scroll up (0x78) / down (0x70) scancode |
| 600 | * repeatedly as many times as indicated by |
| 601 | * rest of the scancode. |
| 602 | */ |
| 603 | count = (scancode & 0x07) + 1; |
| 604 | scancode &= 0x78; |
| 605 | } |
| 606 | |
| 607 | while (count--) { |
| 608 | /* |
| 609 | * We don't use the rc-core repeat handling yet as |
| 610 | * it would cause ghost repeats which would be a |
| 611 | * regression for this driver. |
| 612 | */ |
| 613 | rc_keydown_notimeout(dev: ati_remote->rdev, |
| 614 | protocol: RC_PROTO_OTHER, |
| 615 | scancode, toggle: data[2]); |
| 616 | rc_keyup(dev: ati_remote->rdev); |
| 617 | } |
| 618 | goto nosync; |
| 619 | } |
| 620 | |
| 621 | } else if (ati_remote_tbl[index].kind == KIND_ACCEL) { |
| 622 | signed char dx = ati_remote_tbl[index].code >> 8; |
| 623 | signed char dy = ati_remote_tbl[index].code & 255; |
| 624 | |
| 625 | /* |
| 626 | * Other event kinds are from the directional control pad, and |
| 627 | * have an acceleration factor applied to them. Without this |
| 628 | * acceleration, the control pad is mostly unusable. |
| 629 | */ |
| 630 | int acc = ati_remote_compute_accel(ati_remote); |
| 631 | if (dx) |
| 632 | input_report_rel(dev, REL_X, value: dx * acc); |
| 633 | if (dy) |
| 634 | input_report_rel(dev, REL_Y, value: dy * acc); |
| 635 | ati_remote->old_jiffies = jiffies; |
| 636 | |
| 637 | } else { |
| 638 | dev_dbg(&ati_remote->interface->dev, "ati_remote kind=%d\n" , |
| 639 | ati_remote_tbl[index].kind); |
| 640 | return; |
| 641 | } |
| 642 | input_sync(dev); |
| 643 | nosync: |
| 644 | ati_remote->old_data = data[2]; |
| 645 | } |
| 646 | |
| 647 | /* |
| 648 | * ati_remote_irq_in |
| 649 | */ |
| 650 | static void ati_remote_irq_in(struct urb *urb) |
| 651 | { |
| 652 | struct ati_remote *ati_remote = urb->context; |
| 653 | int retval; |
| 654 | |
| 655 | switch (urb->status) { |
| 656 | case 0: /* success */ |
| 657 | ati_remote_input_report(urb); |
| 658 | break; |
| 659 | case -ECONNRESET: /* unlink */ |
| 660 | case -ENOENT: |
| 661 | case -ESHUTDOWN: |
| 662 | dev_dbg(&ati_remote->interface->dev, |
| 663 | "%s: urb error status, unlink?\n" , |
| 664 | __func__); |
| 665 | return; |
| 666 | default: /* error */ |
| 667 | dev_dbg(&ati_remote->interface->dev, |
| 668 | "%s: Nonzero urb status %d\n" , |
| 669 | __func__, urb->status); |
| 670 | } |
| 671 | |
| 672 | retval = usb_submit_urb(urb, GFP_ATOMIC); |
| 673 | if (retval) |
| 674 | dev_err(&ati_remote->interface->dev, |
| 675 | "%s: usb_submit_urb()=%d\n" , |
| 676 | __func__, retval); |
| 677 | } |
| 678 | |
| 679 | /* |
| 680 | * ati_remote_alloc_buffers |
| 681 | */ |
| 682 | static int ati_remote_alloc_buffers(struct usb_device *udev, |
| 683 | struct ati_remote *ati_remote) |
| 684 | { |
| 685 | ati_remote->inbuf = usb_alloc_coherent(dev: udev, DATA_BUFSIZE, GFP_ATOMIC, |
| 686 | dma: &ati_remote->inbuf_dma); |
| 687 | if (!ati_remote->inbuf) |
| 688 | return -1; |
| 689 | |
| 690 | ati_remote->outbuf = usb_alloc_coherent(dev: udev, DATA_BUFSIZE, GFP_ATOMIC, |
| 691 | dma: &ati_remote->outbuf_dma); |
| 692 | if (!ati_remote->outbuf) |
| 693 | return -1; |
| 694 | |
| 695 | ati_remote->irq_urb = usb_alloc_urb(iso_packets: 0, GFP_KERNEL); |
| 696 | if (!ati_remote->irq_urb) |
| 697 | return -1; |
| 698 | |
| 699 | ati_remote->out_urb = usb_alloc_urb(iso_packets: 0, GFP_KERNEL); |
| 700 | if (!ati_remote->out_urb) |
| 701 | return -1; |
| 702 | |
| 703 | return 0; |
| 704 | } |
| 705 | |
| 706 | /* |
| 707 | * ati_remote_free_buffers |
| 708 | */ |
| 709 | static void ati_remote_free_buffers(struct ati_remote *ati_remote) |
| 710 | { |
| 711 | usb_free_urb(urb: ati_remote->irq_urb); |
| 712 | usb_free_urb(urb: ati_remote->out_urb); |
| 713 | |
| 714 | usb_free_coherent(dev: ati_remote->udev, DATA_BUFSIZE, |
| 715 | addr: ati_remote->inbuf, dma: ati_remote->inbuf_dma); |
| 716 | |
| 717 | usb_free_coherent(dev: ati_remote->udev, DATA_BUFSIZE, |
| 718 | addr: ati_remote->outbuf, dma: ati_remote->outbuf_dma); |
| 719 | } |
| 720 | |
| 721 | static void ati_remote_input_init(struct ati_remote *ati_remote) |
| 722 | { |
| 723 | struct input_dev *idev = ati_remote->idev; |
| 724 | int i; |
| 725 | |
| 726 | idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL); |
| 727 | idev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) | |
| 728 | BIT_MASK(BTN_RIGHT) | BIT_MASK(BTN_SIDE) | BIT_MASK(BTN_EXTRA); |
| 729 | idev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y); |
| 730 | for (i = 0; ati_remote_tbl[i].kind != KIND_END; i++) |
| 731 | if (ati_remote_tbl[i].kind == KIND_LITERAL || |
| 732 | ati_remote_tbl[i].kind == KIND_FILTERED) |
| 733 | __set_bit(ati_remote_tbl[i].code, idev->keybit); |
| 734 | |
| 735 | input_set_drvdata(dev: idev, data: ati_remote); |
| 736 | |
| 737 | idev->open = ati_remote_input_open; |
| 738 | idev->close = ati_remote_input_close; |
| 739 | |
| 740 | idev->name = ati_remote->mouse_name; |
| 741 | idev->phys = ati_remote->mouse_phys; |
| 742 | |
| 743 | usb_to_input_id(dev: ati_remote->udev, id: &idev->id); |
| 744 | idev->dev.parent = &ati_remote->interface->dev; |
| 745 | } |
| 746 | |
| 747 | static void ati_remote_rc_init(struct ati_remote *ati_remote) |
| 748 | { |
| 749 | struct rc_dev *rdev = ati_remote->rdev; |
| 750 | |
| 751 | rdev->priv = ati_remote; |
| 752 | rdev->allowed_protocols = RC_PROTO_BIT_OTHER; |
| 753 | rdev->driver_name = "ati_remote" ; |
| 754 | |
| 755 | rdev->open = ati_remote_rc_open; |
| 756 | rdev->close = ati_remote_rc_close; |
| 757 | |
| 758 | rdev->device_name = ati_remote->rc_name; |
| 759 | rdev->input_phys = ati_remote->rc_phys; |
| 760 | |
| 761 | usb_to_input_id(dev: ati_remote->udev, id: &rdev->input_id); |
| 762 | rdev->dev.parent = &ati_remote->interface->dev; |
| 763 | } |
| 764 | |
| 765 | static int ati_remote_initialize(struct ati_remote *ati_remote) |
| 766 | { |
| 767 | struct usb_device *udev = ati_remote->udev; |
| 768 | int pipe, maxp; |
| 769 | |
| 770 | init_waitqueue_head(&ati_remote->wait); |
| 771 | |
| 772 | /* Set up irq_urb */ |
| 773 | pipe = usb_rcvintpipe(udev, ati_remote->endpoint_in->bEndpointAddress); |
| 774 | maxp = usb_maxpacket(udev, pipe); |
| 775 | maxp = (maxp > DATA_BUFSIZE) ? DATA_BUFSIZE : maxp; |
| 776 | |
| 777 | usb_fill_int_urb(urb: ati_remote->irq_urb, dev: udev, pipe, transfer_buffer: ati_remote->inbuf, |
| 778 | buffer_length: maxp, complete_fn: ati_remote_irq_in, context: ati_remote, |
| 779 | interval: ati_remote->endpoint_in->bInterval); |
| 780 | ati_remote->irq_urb->transfer_dma = ati_remote->inbuf_dma; |
| 781 | ati_remote->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
| 782 | |
| 783 | /* Set up out_urb */ |
| 784 | pipe = usb_sndintpipe(udev, ati_remote->endpoint_out->bEndpointAddress); |
| 785 | maxp = usb_maxpacket(udev, pipe); |
| 786 | maxp = (maxp > DATA_BUFSIZE) ? DATA_BUFSIZE : maxp; |
| 787 | |
| 788 | usb_fill_int_urb(urb: ati_remote->out_urb, dev: udev, pipe, transfer_buffer: ati_remote->outbuf, |
| 789 | buffer_length: maxp, complete_fn: ati_remote_irq_out, context: ati_remote, |
| 790 | interval: ati_remote->endpoint_out->bInterval); |
| 791 | ati_remote->out_urb->transfer_dma = ati_remote->outbuf_dma; |
| 792 | ati_remote->out_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
| 793 | |
| 794 | /* send initialization strings */ |
| 795 | if ((ati_remote_sendpacket(ati_remote, cmd: 0x8004, data: init1)) || |
| 796 | (ati_remote_sendpacket(ati_remote, cmd: 0x8007, data: init2))) { |
| 797 | dev_err(&ati_remote->interface->dev, |
| 798 | "Initializing ati_remote hardware failed.\n" ); |
| 799 | return -EIO; |
| 800 | } |
| 801 | |
| 802 | return 0; |
| 803 | } |
| 804 | |
| 805 | /* |
| 806 | * ati_remote_probe |
| 807 | */ |
| 808 | static int ati_remote_probe(struct usb_interface *interface, |
| 809 | const struct usb_device_id *id) |
| 810 | { |
| 811 | struct usb_device *udev = interface_to_usbdev(interface); |
| 812 | struct usb_host_interface *iface_host = interface->cur_altsetting; |
| 813 | struct usb_endpoint_descriptor *endpoint_in, *endpoint_out; |
| 814 | struct ati_receiver_type *type = (struct ati_receiver_type *)id->driver_info; |
| 815 | struct ati_remote *ati_remote; |
| 816 | struct input_dev *input_dev; |
| 817 | struct device *device = &interface->dev; |
| 818 | struct rc_dev *rc_dev; |
| 819 | int err = -ENOMEM; |
| 820 | |
| 821 | if (iface_host->desc.bNumEndpoints != 2) { |
| 822 | dev_err(device, "%s: Unexpected desc.bNumEndpoints\n" , __func__); |
| 823 | return -ENODEV; |
| 824 | } |
| 825 | |
| 826 | endpoint_in = &iface_host->endpoint[0].desc; |
| 827 | endpoint_out = &iface_host->endpoint[1].desc; |
| 828 | |
| 829 | if (!usb_endpoint_is_int_in(epd: endpoint_in)) { |
| 830 | dev_err(device, "%s: Unexpected endpoint_in\n" , __func__); |
| 831 | return -ENODEV; |
| 832 | } |
| 833 | if (le16_to_cpu(endpoint_in->wMaxPacketSize) == 0) { |
| 834 | dev_err(device, "%s: endpoint_in message size==0?\n" , __func__); |
| 835 | return -ENODEV; |
| 836 | } |
| 837 | if (!usb_endpoint_is_int_out(epd: endpoint_out)) { |
| 838 | dev_err(device, "%s: Unexpected endpoint_out\n" , __func__); |
| 839 | return -ENODEV; |
| 840 | } |
| 841 | |
| 842 | ati_remote = kzalloc(sizeof (struct ati_remote), GFP_KERNEL); |
| 843 | rc_dev = rc_allocate_device(RC_DRIVER_SCANCODE); |
| 844 | if (!ati_remote || !rc_dev) |
| 845 | goto exit_free_dev_rdev; |
| 846 | |
| 847 | /* Allocate URB buffers, URBs */ |
| 848 | if (ati_remote_alloc_buffers(udev, ati_remote)) |
| 849 | goto exit_free_buffers; |
| 850 | |
| 851 | ati_remote->endpoint_in = endpoint_in; |
| 852 | ati_remote->endpoint_out = endpoint_out; |
| 853 | ati_remote->udev = udev; |
| 854 | ati_remote->rdev = rc_dev; |
| 855 | ati_remote->interface = interface; |
| 856 | |
| 857 | usb_make_path(dev: udev, buf: ati_remote->rc_phys, size: sizeof(ati_remote->rc_phys)); |
| 858 | strscpy(ati_remote->mouse_phys, ati_remote->rc_phys, |
| 859 | sizeof(ati_remote->mouse_phys)); |
| 860 | |
| 861 | strlcat(p: ati_remote->rc_phys, q: "/input0" , avail: sizeof(ati_remote->rc_phys)); |
| 862 | strlcat(p: ati_remote->mouse_phys, q: "/input1" , avail: sizeof(ati_remote->mouse_phys)); |
| 863 | |
| 864 | snprintf(buf: ati_remote->rc_name, size: sizeof(ati_remote->rc_name), fmt: "%s%s%s" , |
| 865 | udev->manufacturer ?: "" , |
| 866 | udev->manufacturer && udev->product ? " " : "" , |
| 867 | udev->product ?: "" ); |
| 868 | |
| 869 | if (!strlen(ati_remote->rc_name)) |
| 870 | snprintf(buf: ati_remote->rc_name, size: sizeof(ati_remote->rc_name), |
| 871 | DRIVER_DESC "(%04x,%04x)" , |
| 872 | le16_to_cpu(ati_remote->udev->descriptor.idVendor), |
| 873 | le16_to_cpu(ati_remote->udev->descriptor.idProduct)); |
| 874 | |
| 875 | snprintf(buf: ati_remote->mouse_name, size: sizeof(ati_remote->mouse_name), |
| 876 | fmt: "%s mouse" , ati_remote->rc_name); |
| 877 | |
| 878 | rc_dev->map_name = RC_MAP_ATI_X10; /* default map */ |
| 879 | |
| 880 | /* set default keymap according to receiver model */ |
| 881 | if (type) { |
| 882 | if (type->default_keymap) |
| 883 | rc_dev->map_name = type->default_keymap; |
| 884 | else if (type->get_default_keymap) |
| 885 | rc_dev->map_name = type->get_default_keymap(interface); |
| 886 | } |
| 887 | |
| 888 | ati_remote_rc_init(ati_remote); |
| 889 | mutex_init(&ati_remote->open_mutex); |
| 890 | |
| 891 | /* Device Hardware Initialization - fills in ati_remote->idev from udev. */ |
| 892 | err = ati_remote_initialize(ati_remote); |
| 893 | if (err) |
| 894 | goto exit_kill_urbs; |
| 895 | |
| 896 | /* Set up and register rc device */ |
| 897 | err = rc_register_device(dev: ati_remote->rdev); |
| 898 | if (err) |
| 899 | goto exit_kill_urbs; |
| 900 | |
| 901 | /* Set up and register mouse input device */ |
| 902 | if (mouse) { |
| 903 | input_dev = input_allocate_device(); |
| 904 | if (!input_dev) { |
| 905 | err = -ENOMEM; |
| 906 | goto exit_unregister_device; |
| 907 | } |
| 908 | |
| 909 | ati_remote->idev = input_dev; |
| 910 | ati_remote_input_init(ati_remote); |
| 911 | err = input_register_device(input_dev); |
| 912 | |
| 913 | if (err) |
| 914 | goto exit_free_input_device; |
| 915 | } |
| 916 | |
| 917 | usb_set_intfdata(intf: interface, data: ati_remote); |
| 918 | return 0; |
| 919 | |
| 920 | exit_free_input_device: |
| 921 | input_free_device(dev: input_dev); |
| 922 | exit_unregister_device: |
| 923 | rc_unregister_device(dev: rc_dev); |
| 924 | rc_dev = NULL; |
| 925 | exit_kill_urbs: |
| 926 | usb_kill_urb(urb: ati_remote->irq_urb); |
| 927 | usb_kill_urb(urb: ati_remote->out_urb); |
| 928 | exit_free_buffers: |
| 929 | ati_remote_free_buffers(ati_remote); |
| 930 | exit_free_dev_rdev: |
| 931 | rc_free_device(dev: rc_dev); |
| 932 | kfree(objp: ati_remote); |
| 933 | return err; |
| 934 | } |
| 935 | |
| 936 | /* |
| 937 | * ati_remote_disconnect |
| 938 | */ |
| 939 | static void ati_remote_disconnect(struct usb_interface *interface) |
| 940 | { |
| 941 | struct ati_remote *ati_remote; |
| 942 | |
| 943 | ati_remote = usb_get_intfdata(intf: interface); |
| 944 | usb_set_intfdata(intf: interface, NULL); |
| 945 | if (!ati_remote) { |
| 946 | dev_warn(&interface->dev, "%s - null device?\n" , __func__); |
| 947 | return; |
| 948 | } |
| 949 | |
| 950 | usb_kill_urb(urb: ati_remote->irq_urb); |
| 951 | usb_kill_urb(urb: ati_remote->out_urb); |
| 952 | if (ati_remote->idev) |
| 953 | input_unregister_device(ati_remote->idev); |
| 954 | rc_unregister_device(dev: ati_remote->rdev); |
| 955 | ati_remote_free_buffers(ati_remote); |
| 956 | kfree(objp: ati_remote); |
| 957 | } |
| 958 | |
| 959 | /* usb specific object to register with the usb subsystem */ |
| 960 | static struct usb_driver ati_remote_driver = { |
| 961 | .name = "ati_remote" , |
| 962 | .probe = ati_remote_probe, |
| 963 | .disconnect = ati_remote_disconnect, |
| 964 | .id_table = ati_remote_table, |
| 965 | }; |
| 966 | |
| 967 | module_usb_driver(ati_remote_driver); |
| 968 | |
| 969 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 970 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 971 | MODULE_LICENSE("GPL" ); |
| 972 | |