Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  USB ATI Remote support
0004  *
0005  *                Copyright (c) 2011, 2012 Anssi Hannula <anssi.hannula@iki.fi>
0006  *  Version 2.2.0 Copyright (c) 2004 Torrey Hoffman <thoffman@arnor.net>
0007  *  Version 2.1.1 Copyright (c) 2002 Vladimir Dergachev
0008  *
0009  *  This 2.2.0 version is a rewrite / cleanup of the 2.1.1 driver, including
0010  *  porting to the 2.6 kernel interfaces, along with other modification
0011  *  to better match the style of the existing usb/input drivers.  However, the
0012  *  protocol and hardware handling is essentially unchanged from 2.1.1.
0013  *
0014  *  The 2.1.1 driver was derived from the usbati_remote and usbkbd drivers by
0015  *  Vojtech Pavlik.
0016  *
0017  *  Changes:
0018  *
0019  *  Feb 2004: Torrey Hoffman <thoffman@arnor.net>
0020  *            Version 2.2.0
0021  *  Jun 2004: Torrey Hoffman <thoffman@arnor.net>
0022  *            Version 2.2.1
0023  *            Added key repeat support contributed by:
0024  *                Vincent Vanackere <vanackere@lif.univ-mrs.fr>
0025  *            Added support for the "Lola" remote contributed by:
0026  *                Seth Cohn <sethcohn@yahoo.com>
0027  *
0028  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
0029  *
0030  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
0031  *
0032  * Hardware & software notes
0033  *
0034  * These remote controls are distributed by ATI as part of their
0035  * "All-In-Wonder" video card packages.  The receiver self-identifies as a
0036  * "USB Receiver" with manufacturer "X10 Wireless Technology Inc".
0037  *
0038  * The "Lola" remote is available from X10.  See:
0039  *    http://www.x10.com/products/lola_sg1.htm
0040  * The Lola is similar to the ATI remote but has no mouse support, and slightly
0041  * different keys.
0042  *
0043  * It is possible to use multiple receivers and remotes on multiple computers
0044  * simultaneously by configuring them to use specific channels.
0045  *
0046  * The RF protocol used by the remote supports 16 distinct channels, 1 to 16.
0047  * Actually, it may even support more, at least in some revisions of the
0048  * hardware.
0049  *
0050  * Each remote can be configured to transmit on one channel as follows:
0051  *   - Press and hold the "hand icon" button.
0052  *   - When the red LED starts to blink, let go of the "hand icon" button.
0053  *   - When it stops blinking, input the channel code as two digits, from 01
0054  *     to 16, and press the hand icon again.
0055  *
0056  * The timing can be a little tricky.  Try loading the module with debug=1
0057  * to have the kernel print out messages about the remote control number
0058  * and mask.  Note: debugging prints remote numbers as zero-based hexadecimal.
0059  *
0060  * The driver has a "channel_mask" parameter. This bitmask specifies which
0061  * channels will be ignored by the module.  To mask out channels, just add
0062  * all the 2^channel_number values together.
0063  *
0064  * For instance, set channel_mask = 2^4 = 16 (binary 10000) to make ati_remote
0065  * ignore signals coming from remote controls transmitting on channel 4, but
0066  * accept all other channels.
0067  *
0068  * Or, set channel_mask = 65533, (0xFFFD), and all channels except 1 will be
0069  * ignored.
0070  *
0071  * The default is 0 (respond to all channels). Bit 0 and bits 17-32 of this
0072  * parameter are unused.
0073  */
0074 
0075 #include <linux/kernel.h>
0076 #include <linux/errno.h>
0077 #include <linux/init.h>
0078 #include <linux/slab.h>
0079 #include <linux/module.h>
0080 #include <linux/mutex.h>
0081 #include <linux/usb/input.h>
0082 #include <linux/wait.h>
0083 #include <linux/jiffies.h>
0084 #include <media/rc-core.h>
0085 
0086 /*
0087  * Module and Version Information, Module Parameters
0088  */
0089 
0090 #define ATI_REMOTE_VENDOR_ID        0x0bc7
0091 #define LOLA_REMOTE_PRODUCT_ID      0x0002
0092 #define LOLA2_REMOTE_PRODUCT_ID     0x0003
0093 #define ATI_REMOTE_PRODUCT_ID       0x0004
0094 #define NVIDIA_REMOTE_PRODUCT_ID    0x0005
0095 #define MEDION_REMOTE_PRODUCT_ID    0x0006
0096 #define FIREFLY_REMOTE_PRODUCT_ID   0x0008
0097 
0098 #define DRIVER_VERSION      "2.2.1"
0099 #define DRIVER_AUTHOR           "Torrey Hoffman <thoffman@arnor.net>"
0100 #define DRIVER_DESC             "ATI/X10 RF USB Remote Control"
0101 
0102 #define NAME_BUFSIZE      80    /* size of product name, path buffers */
0103 #define DATA_BUFSIZE      63    /* size of URB data buffers */
0104 
0105 /*
0106  * Duplicate event filtering time.
0107  * Sequential, identical KIND_FILTERED inputs with less than
0108  * FILTER_TIME milliseconds between them are considered as repeat
0109  * events. The hardware generates 5 events for the first keypress
0110  * and we have to take this into account for an accurate repeat
0111  * behaviour.
0112  */
0113 #define FILTER_TIME 60 /* msec */
0114 #define REPEAT_DELAY    500 /* msec */
0115 
0116 static unsigned long channel_mask;
0117 module_param(channel_mask, ulong, 0644);
0118 MODULE_PARM_DESC(channel_mask, "Bitmask of remote control channels to ignore");
0119 
0120 static int debug;
0121 module_param(debug, int, 0644);
0122 MODULE_PARM_DESC(debug, "Enable extra debug messages and information");
0123 
0124 static int repeat_filter = FILTER_TIME;
0125 module_param(repeat_filter, int, 0644);
0126 MODULE_PARM_DESC(repeat_filter, "Repeat filter time, default = 60 msec");
0127 
0128 static int repeat_delay = REPEAT_DELAY;
0129 module_param(repeat_delay, int, 0644);
0130 MODULE_PARM_DESC(repeat_delay, "Delay before sending repeats, default = 500 msec");
0131 
0132 static bool mouse = true;
0133 module_param(mouse, bool, 0444);
0134 MODULE_PARM_DESC(mouse, "Enable mouse device, default = yes");
0135 
0136 #define dbginfo(dev, format, arg...) \
0137     do { if (debug) dev_info(dev , format , ## arg); } while (0)
0138 
0139 struct ati_receiver_type {
0140     /* either default_keymap or get_default_keymap should be set */
0141     const char *default_keymap;
0142     const char *(*get_default_keymap)(struct usb_interface *interface);
0143 };
0144 
0145 static const char *get_medion_keymap(struct usb_interface *interface)
0146 {
0147     struct usb_device *udev = interface_to_usbdev(interface);
0148 
0149     /*
0150      * There are many different Medion remotes shipped with a receiver
0151      * with the same usb id, but the receivers have subtle differences
0152      * in the USB descriptors allowing us to detect them.
0153      */
0154 
0155     if (udev->manufacturer && udev->product) {
0156         if (udev->actconfig->desc.bmAttributes & USB_CONFIG_ATT_WAKEUP) {
0157 
0158             if (!strcmp(udev->manufacturer, "X10 Wireless Technology Inc")
0159                 && !strcmp(udev->product, "USB Receiver"))
0160                 return RC_MAP_MEDION_X10_DIGITAINER;
0161 
0162             if (!strcmp(udev->manufacturer, "X10 WTI")
0163                 && !strcmp(udev->product, "RF receiver"))
0164                 return RC_MAP_MEDION_X10_OR2X;
0165         } else {
0166 
0167              if (!strcmp(udev->manufacturer, "X10 Wireless Technology Inc")
0168                 && !strcmp(udev->product, "USB Receiver"))
0169                 return RC_MAP_MEDION_X10;
0170         }
0171     }
0172 
0173     dev_info(&interface->dev,
0174          "Unknown Medion X10 receiver, using default ati_remote Medion keymap\n");
0175 
0176     return RC_MAP_MEDION_X10;
0177 }
0178 
0179 static const struct ati_receiver_type type_ati      = {
0180     .default_keymap = RC_MAP_ATI_X10
0181 };
0182 static const struct ati_receiver_type type_medion   = {
0183     .get_default_keymap = get_medion_keymap
0184 };
0185 static const struct ati_receiver_type type_firefly  = {
0186     .default_keymap = RC_MAP_SNAPSTREAM_FIREFLY
0187 };
0188 
0189 static const struct usb_device_id ati_remote_table[] = {
0190     {
0191         USB_DEVICE(ATI_REMOTE_VENDOR_ID, LOLA_REMOTE_PRODUCT_ID),
0192         .driver_info = (unsigned long)&type_ati
0193     },
0194     {
0195         USB_DEVICE(ATI_REMOTE_VENDOR_ID, LOLA2_REMOTE_PRODUCT_ID),
0196         .driver_info = (unsigned long)&type_ati
0197     },
0198     {
0199         USB_DEVICE(ATI_REMOTE_VENDOR_ID, ATI_REMOTE_PRODUCT_ID),
0200         .driver_info = (unsigned long)&type_ati
0201     },
0202     {
0203         USB_DEVICE(ATI_REMOTE_VENDOR_ID, NVIDIA_REMOTE_PRODUCT_ID),
0204         .driver_info = (unsigned long)&type_ati
0205     },
0206     {
0207         USB_DEVICE(ATI_REMOTE_VENDOR_ID, MEDION_REMOTE_PRODUCT_ID),
0208         .driver_info = (unsigned long)&type_medion
0209     },
0210     {
0211         USB_DEVICE(ATI_REMOTE_VENDOR_ID, FIREFLY_REMOTE_PRODUCT_ID),
0212         .driver_info = (unsigned long)&type_firefly
0213     },
0214     {}  /* Terminating entry */
0215 };
0216 
0217 MODULE_DEVICE_TABLE(usb, ati_remote_table);
0218 
0219 /* Get hi and low bytes of a 16-bits int */
0220 #define HI(a)   ((unsigned char)((a) >> 8))
0221 #define LO(a)   ((unsigned char)((a) & 0xff))
0222 
0223 #define SEND_FLAG_IN_PROGRESS   1
0224 #define SEND_FLAG_COMPLETE  2
0225 
0226 /* Device initialization strings */
0227 static char init1[] = { 0x01, 0x00, 0x20, 0x14 };
0228 static char init2[] = { 0x01, 0x00, 0x20, 0x14, 0x20, 0x20, 0x20 };
0229 
0230 struct ati_remote {
0231     struct input_dev *idev;
0232     struct rc_dev *rdev;
0233     struct usb_device *udev;
0234     struct usb_interface *interface;
0235 
0236     struct urb *irq_urb;
0237     struct urb *out_urb;
0238     struct usb_endpoint_descriptor *endpoint_in;
0239     struct usb_endpoint_descriptor *endpoint_out;
0240     unsigned char *inbuf;
0241     unsigned char *outbuf;
0242     dma_addr_t inbuf_dma;
0243     dma_addr_t outbuf_dma;
0244 
0245     unsigned char old_data;     /* Detect duplicate events */
0246     unsigned long old_jiffies;
0247     unsigned long acc_jiffies;  /* handle acceleration */
0248     unsigned long first_jiffies;
0249 
0250     unsigned int repeat_count;
0251 
0252     char rc_name[NAME_BUFSIZE];
0253     char rc_phys[NAME_BUFSIZE];
0254     char mouse_name[NAME_BUFSIZE];
0255     char mouse_phys[NAME_BUFSIZE];
0256 
0257     wait_queue_head_t wait;
0258     int send_flags;
0259 
0260     int users; /* 0-2, users are rc and input */
0261     struct mutex open_mutex;
0262 };
0263 
0264 /* "Kinds" of messages sent from the hardware to the driver. */
0265 #define KIND_END        0
0266 #define KIND_LITERAL    1   /* Simply pass to input system as EV_KEY */
0267 #define KIND_FILTERED   2   /* Add artificial key-up events, drop keyrepeats */
0268 #define KIND_ACCEL      3   /* Translate to EV_REL mouse-move events */
0269 
0270 /* Translation table from hardware messages to input events. */
0271 static const struct {
0272     unsigned char kind;
0273     unsigned char data; /* Raw key code from remote */
0274     unsigned short code;    /* Input layer translation */
0275 }  ati_remote_tbl[] = {
0276     /* Directional control pad axes.  Code is xxyy */
0277     {KIND_ACCEL,    0x70, 0xff00},  /* left */
0278     {KIND_ACCEL,    0x71, 0x0100},  /* right */
0279     {KIND_ACCEL,    0x72, 0x00ff},  /* up */
0280     {KIND_ACCEL,    0x73, 0x0001},  /* down */
0281 
0282     /* Directional control pad diagonals */
0283     {KIND_ACCEL,    0x74, 0xffff},  /* left up */
0284     {KIND_ACCEL,    0x75, 0x01ff},  /* right up */
0285     {KIND_ACCEL,    0x77, 0xff01},  /* left down */
0286     {KIND_ACCEL,    0x76, 0x0101},  /* right down */
0287 
0288     /* "Mouse button" buttons.  The code below uses the fact that the
0289      * lsbit of the raw code is a down/up indicator. */
0290     {KIND_LITERAL,  0x78, BTN_LEFT}, /* left btn down */
0291     {KIND_LITERAL,  0x79, BTN_LEFT}, /* left btn up */
0292     {KIND_LITERAL,  0x7c, BTN_RIGHT},/* right btn down */
0293     {KIND_LITERAL,  0x7d, BTN_RIGHT},/* right btn up */
0294 
0295     /* Artificial "double-click" events are generated by the hardware.
0296      * They are mapped to the "side" and "extra" mouse buttons here. */
0297     {KIND_FILTERED, 0x7a, BTN_SIDE}, /* left dblclick */
0298     {KIND_FILTERED, 0x7e, BTN_EXTRA},/* right dblclick */
0299 
0300     /* Non-mouse events are handled by rc-core */
0301     {KIND_END, 0x00, 0}
0302 };
0303 
0304 /*
0305  * ati_remote_dump_input
0306  */
0307 static void ati_remote_dump(struct device *dev, unsigned char *data,
0308                 unsigned int len)
0309 {
0310     if (len == 1) {
0311         if (data[0] != (unsigned char)0xff && data[0] != 0x00)
0312             dev_warn(dev, "Weird byte 0x%02x\n", data[0]);
0313     } else if (len == 4)
0314         dev_warn(dev, "Weird key %*ph\n", 4, data);
0315     else
0316         dev_warn(dev, "Weird data, len=%d %*ph ...\n", len, 6, data);
0317 }
0318 
0319 /*
0320  * ati_remote_open
0321  */
0322 static int ati_remote_open(struct ati_remote *ati_remote)
0323 {
0324     int err = 0;
0325 
0326     mutex_lock(&ati_remote->open_mutex);
0327 
0328     if (ati_remote->users++ != 0)
0329         goto out; /* one was already active */
0330 
0331     /* On first open, submit the read urb which was set up previously. */
0332     ati_remote->irq_urb->dev = ati_remote->udev;
0333     if (usb_submit_urb(ati_remote->irq_urb, GFP_KERNEL)) {
0334         dev_err(&ati_remote->interface->dev,
0335             "%s: usb_submit_urb failed!\n", __func__);
0336         err = -EIO;
0337     }
0338 
0339 out:    mutex_unlock(&ati_remote->open_mutex);
0340     return err;
0341 }
0342 
0343 /*
0344  * ati_remote_close
0345  */
0346 static void ati_remote_close(struct ati_remote *ati_remote)
0347 {
0348     mutex_lock(&ati_remote->open_mutex);
0349     if (--ati_remote->users == 0)
0350         usb_kill_urb(ati_remote->irq_urb);
0351     mutex_unlock(&ati_remote->open_mutex);
0352 }
0353 
0354 static int ati_remote_input_open(struct input_dev *inputdev)
0355 {
0356     struct ati_remote *ati_remote = input_get_drvdata(inputdev);
0357     return ati_remote_open(ati_remote);
0358 }
0359 
0360 static void ati_remote_input_close(struct input_dev *inputdev)
0361 {
0362     struct ati_remote *ati_remote = input_get_drvdata(inputdev);
0363     ati_remote_close(ati_remote);
0364 }
0365 
0366 static int ati_remote_rc_open(struct rc_dev *rdev)
0367 {
0368     struct ati_remote *ati_remote = rdev->priv;
0369     return ati_remote_open(ati_remote);
0370 }
0371 
0372 static void ati_remote_rc_close(struct rc_dev *rdev)
0373 {
0374     struct ati_remote *ati_remote = rdev->priv;
0375     ati_remote_close(ati_remote);
0376 }
0377 
0378 /*
0379  * ati_remote_irq_out
0380  */
0381 static void ati_remote_irq_out(struct urb *urb)
0382 {
0383     struct ati_remote *ati_remote = urb->context;
0384 
0385     if (urb->status) {
0386         dev_dbg(&ati_remote->interface->dev, "%s: status %d\n",
0387             __func__, urb->status);
0388         return;
0389     }
0390 
0391     ati_remote->send_flags |= SEND_FLAG_COMPLETE;
0392     wmb();
0393     wake_up(&ati_remote->wait);
0394 }
0395 
0396 /*
0397  * ati_remote_sendpacket
0398  *
0399  * Used to send device initialization strings
0400  */
0401 static int ati_remote_sendpacket(struct ati_remote *ati_remote, u16 cmd,
0402     unsigned char *data)
0403 {
0404     int retval = 0;
0405 
0406     /* Set up out_urb */
0407     memcpy(ati_remote->out_urb->transfer_buffer + 1, data, LO(cmd));
0408     ((char *) ati_remote->out_urb->transfer_buffer)[0] = HI(cmd);
0409 
0410     ati_remote->out_urb->transfer_buffer_length = LO(cmd) + 1;
0411     ati_remote->out_urb->dev = ati_remote->udev;
0412     ati_remote->send_flags = SEND_FLAG_IN_PROGRESS;
0413 
0414     retval = usb_submit_urb(ati_remote->out_urb, GFP_ATOMIC);
0415     if (retval) {
0416         dev_dbg(&ati_remote->interface->dev,
0417              "sendpacket: usb_submit_urb failed: %d\n", retval);
0418         return retval;
0419     }
0420 
0421     wait_event_timeout(ati_remote->wait,
0422         ((ati_remote->out_urb->status != -EINPROGRESS) ||
0423             (ati_remote->send_flags & SEND_FLAG_COMPLETE)),
0424         HZ);
0425     usb_kill_urb(ati_remote->out_urb);
0426 
0427     return retval;
0428 }
0429 
0430 struct accel_times {
0431     const char  value;
0432     unsigned int    msecs;
0433 };
0434 
0435 static const struct accel_times accel[] = {
0436     {  1,  125 },
0437     {  2,  250 },
0438     {  4,  500 },
0439     {  6, 1000 },
0440     {  9, 1500 },
0441     { 13, 2000 },
0442     { 20,    0 },
0443 };
0444 
0445 /*
0446  * ati_remote_compute_accel
0447  *
0448  * Implements acceleration curve for directional control pad
0449  * If elapsed time since last event is > 1/4 second, user "stopped",
0450  * so reset acceleration. Otherwise, user is probably holding the control
0451  * pad down, so we increase acceleration, ramping up over two seconds to
0452  * a maximum speed.
0453  */
0454 static int ati_remote_compute_accel(struct ati_remote *ati_remote)
0455 {
0456     unsigned long now = jiffies, reset_time;
0457     int i;
0458 
0459     reset_time = msecs_to_jiffies(250);
0460 
0461     if (time_after(now, ati_remote->old_jiffies + reset_time)) {
0462         ati_remote->acc_jiffies = now;
0463         return 1;
0464     }
0465     for (i = 0; i < ARRAY_SIZE(accel) - 1; i++) {
0466         unsigned long timeout = msecs_to_jiffies(accel[i].msecs);
0467 
0468         if (time_before(now, ati_remote->acc_jiffies + timeout))
0469             return accel[i].value;
0470     }
0471     return accel[i].value;
0472 }
0473 
0474 /*
0475  * ati_remote_report_input
0476  */
0477 static void ati_remote_input_report(struct urb *urb)
0478 {
0479     struct ati_remote *ati_remote = urb->context;
0480     unsigned char *data= ati_remote->inbuf;
0481     struct input_dev *dev = ati_remote->idev;
0482     int index = -1;
0483     int remote_num;
0484     unsigned char scancode;
0485     u32 wheel_keycode = KEY_RESERVED;
0486     int i;
0487 
0488     /*
0489      * data[0] = 0x14
0490      * data[1] = data[2] + data[3] + 0xd5 (a checksum byte)
0491      * data[2] = the key code (with toggle bit in MSB with some models)
0492      * data[3] = channel << 4 (the low 4 bits must be zero)
0493      */
0494 
0495     /* Deal with strange looking inputs */
0496     if ( urb->actual_length != 4 || data[0] != 0x14 ||
0497          data[1] != (unsigned char)(data[2] + data[3] + 0xD5) ||
0498          (data[3] & 0x0f) != 0x00) {
0499         ati_remote_dump(&urb->dev->dev, data, urb->actual_length);
0500         return;
0501     }
0502 
0503     if (data[1] != ((data[2] + data[3] + 0xd5) & 0xff)) {
0504         dbginfo(&ati_remote->interface->dev,
0505             "wrong checksum in input: %*ph\n", 4, data);
0506         return;
0507     }
0508 
0509     /* Mask unwanted remote channels.  */
0510     /* note: remote_num is 0-based, channel 1 on remote == 0 here */
0511     remote_num = (data[3] >> 4) & 0x0f;
0512     if (channel_mask & (1 << (remote_num + 1))) {
0513         dbginfo(&ati_remote->interface->dev,
0514             "Masked input from channel 0x%02x: data %02x, mask= 0x%02lx\n",
0515             remote_num, data[2], channel_mask);
0516         return;
0517     }
0518 
0519     /*
0520      * MSB is a toggle code, though only used by some devices
0521      * (e.g. SnapStream Firefly)
0522      */
0523     scancode = data[2] & 0x7f;
0524 
0525     dbginfo(&ati_remote->interface->dev,
0526         "channel 0x%02x; key data %02x, scancode %02x\n",
0527         remote_num, data[2], scancode);
0528 
0529     if (scancode >= 0x70) {
0530         /*
0531          * This is either a mouse or scrollwheel event, depending on
0532          * the remote/keymap.
0533          * Get the keycode assigned to scancode 0x78/0x70. If it is
0534          * set, assume this is a scrollwheel up/down event.
0535          */
0536         wheel_keycode = rc_g_keycode_from_table(ati_remote->rdev,
0537                             scancode & 0x78);
0538 
0539         if (wheel_keycode == KEY_RESERVED) {
0540             /* scrollwheel was not mapped, assume mouse */
0541 
0542             /* Look up event code index in the mouse translation
0543              * table.
0544              */
0545             for (i = 0; ati_remote_tbl[i].kind != KIND_END; i++) {
0546                 if (scancode == ati_remote_tbl[i].data) {
0547                     index = i;
0548                     break;
0549                 }
0550             }
0551         }
0552     }
0553 
0554     if (index >= 0 && ati_remote_tbl[index].kind == KIND_LITERAL) {
0555         /*
0556          * The lsbit of the raw key code is a down/up flag.
0557          * Invert it to match the input layer's conventions.
0558          */
0559         input_event(dev, EV_KEY, ati_remote_tbl[index].code,
0560             !(data[2] & 1));
0561 
0562         ati_remote->old_jiffies = jiffies;
0563 
0564     } else if (index < 0 || ati_remote_tbl[index].kind == KIND_FILTERED) {
0565         unsigned long now = jiffies;
0566 
0567         /* Filter duplicate events which happen "too close" together. */
0568         if (ati_remote->old_data == data[2] &&
0569             time_before(now, ati_remote->old_jiffies +
0570                      msecs_to_jiffies(repeat_filter))) {
0571             ati_remote->repeat_count++;
0572         } else {
0573             ati_remote->repeat_count = 0;
0574             ati_remote->first_jiffies = now;
0575         }
0576 
0577         ati_remote->old_jiffies = now;
0578 
0579         /* Ensure we skip at least the 4 first duplicate events
0580          * (generated by a single keypress), and continue skipping
0581          * until repeat_delay msecs have passed.
0582          */
0583         if (ati_remote->repeat_count > 0 &&
0584             (ati_remote->repeat_count < 5 ||
0585              time_before(now, ati_remote->first_jiffies +
0586                       msecs_to_jiffies(repeat_delay))))
0587             return;
0588 
0589         if (index >= 0) {
0590             input_event(dev, EV_KEY, ati_remote_tbl[index].code, 1);
0591             input_event(dev, EV_KEY, ati_remote_tbl[index].code, 0);
0592         } else {
0593             /* Not a mouse event, hand it to rc-core. */
0594             int count = 1;
0595 
0596             if (wheel_keycode != KEY_RESERVED) {
0597                 /*
0598                  * This is a scrollwheel event, send the
0599                  * scroll up (0x78) / down (0x70) scancode
0600                  * repeatedly as many times as indicated by
0601                  * rest of the scancode.
0602                  */
0603                 count = (scancode & 0x07) + 1;
0604                 scancode &= 0x78;
0605             }
0606 
0607             while (count--) {
0608                 /*
0609                 * We don't use the rc-core repeat handling yet as
0610                 * it would cause ghost repeats which would be a
0611                 * regression for this driver.
0612                 */
0613                 rc_keydown_notimeout(ati_remote->rdev,
0614                              RC_PROTO_OTHER,
0615                              scancode, data[2]);
0616                 rc_keyup(ati_remote->rdev);
0617             }
0618             goto nosync;
0619         }
0620 
0621     } else if (ati_remote_tbl[index].kind == KIND_ACCEL) {
0622         signed char dx = ati_remote_tbl[index].code >> 8;
0623         signed char dy = ati_remote_tbl[index].code & 255;
0624 
0625         /*
0626          * Other event kinds are from the directional control pad, and
0627          * have an acceleration factor applied to them.  Without this
0628          * acceleration, the control pad is mostly unusable.
0629          */
0630         int acc = ati_remote_compute_accel(ati_remote);
0631         if (dx)
0632             input_report_rel(dev, REL_X, dx * acc);
0633         if (dy)
0634             input_report_rel(dev, REL_Y, dy * acc);
0635         ati_remote->old_jiffies = jiffies;
0636 
0637     } else {
0638         dev_dbg(&ati_remote->interface->dev, "ati_remote kind=%d\n",
0639             ati_remote_tbl[index].kind);
0640         return;
0641     }
0642     input_sync(dev);
0643 nosync:
0644     ati_remote->old_data = data[2];
0645 }
0646 
0647 /*
0648  * ati_remote_irq_in
0649  */
0650 static void ati_remote_irq_in(struct urb *urb)
0651 {
0652     struct ati_remote *ati_remote = urb->context;
0653     int retval;
0654 
0655     switch (urb->status) {
0656     case 0:         /* success */
0657         ati_remote_input_report(urb);
0658         break;
0659     case -ECONNRESET:   /* unlink */
0660     case -ENOENT:
0661     case -ESHUTDOWN:
0662         dev_dbg(&ati_remote->interface->dev,
0663             "%s: urb error status, unlink?\n",
0664             __func__);
0665         return;
0666     default:        /* error */
0667         dev_dbg(&ati_remote->interface->dev,
0668             "%s: Nonzero urb status %d\n",
0669             __func__, urb->status);
0670     }
0671 
0672     retval = usb_submit_urb(urb, GFP_ATOMIC);
0673     if (retval)
0674         dev_err(&ati_remote->interface->dev,
0675             "%s: usb_submit_urb()=%d\n",
0676             __func__, retval);
0677 }
0678 
0679 /*
0680  * ati_remote_alloc_buffers
0681  */
0682 static int ati_remote_alloc_buffers(struct usb_device *udev,
0683                     struct ati_remote *ati_remote)
0684 {
0685     ati_remote->inbuf = usb_alloc_coherent(udev, DATA_BUFSIZE, GFP_ATOMIC,
0686                            &ati_remote->inbuf_dma);
0687     if (!ati_remote->inbuf)
0688         return -1;
0689 
0690     ati_remote->outbuf = usb_alloc_coherent(udev, DATA_BUFSIZE, GFP_ATOMIC,
0691                         &ati_remote->outbuf_dma);
0692     if (!ati_remote->outbuf)
0693         return -1;
0694 
0695     ati_remote->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
0696     if (!ati_remote->irq_urb)
0697         return -1;
0698 
0699     ati_remote->out_urb = usb_alloc_urb(0, GFP_KERNEL);
0700     if (!ati_remote->out_urb)
0701         return -1;
0702 
0703     return 0;
0704 }
0705 
0706 /*
0707  * ati_remote_free_buffers
0708  */
0709 static void ati_remote_free_buffers(struct ati_remote *ati_remote)
0710 {
0711     usb_free_urb(ati_remote->irq_urb);
0712     usb_free_urb(ati_remote->out_urb);
0713 
0714     usb_free_coherent(ati_remote->udev, DATA_BUFSIZE,
0715         ati_remote->inbuf, ati_remote->inbuf_dma);
0716 
0717     usb_free_coherent(ati_remote->udev, DATA_BUFSIZE,
0718         ati_remote->outbuf, ati_remote->outbuf_dma);
0719 }
0720 
0721 static void ati_remote_input_init(struct ati_remote *ati_remote)
0722 {
0723     struct input_dev *idev = ati_remote->idev;
0724     int i;
0725 
0726     idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
0727     idev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
0728         BIT_MASK(BTN_RIGHT) | BIT_MASK(BTN_SIDE) | BIT_MASK(BTN_EXTRA);
0729     idev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
0730     for (i = 0; ati_remote_tbl[i].kind != KIND_END; i++)
0731         if (ati_remote_tbl[i].kind == KIND_LITERAL ||
0732             ati_remote_tbl[i].kind == KIND_FILTERED)
0733             __set_bit(ati_remote_tbl[i].code, idev->keybit);
0734 
0735     input_set_drvdata(idev, ati_remote);
0736 
0737     idev->open = ati_remote_input_open;
0738     idev->close = ati_remote_input_close;
0739 
0740     idev->name = ati_remote->mouse_name;
0741     idev->phys = ati_remote->mouse_phys;
0742 
0743     usb_to_input_id(ati_remote->udev, &idev->id);
0744     idev->dev.parent = &ati_remote->interface->dev;
0745 }
0746 
0747 static void ati_remote_rc_init(struct ati_remote *ati_remote)
0748 {
0749     struct rc_dev *rdev = ati_remote->rdev;
0750 
0751     rdev->priv = ati_remote;
0752     rdev->allowed_protocols = RC_PROTO_BIT_OTHER;
0753     rdev->driver_name = "ati_remote";
0754 
0755     rdev->open = ati_remote_rc_open;
0756     rdev->close = ati_remote_rc_close;
0757 
0758     rdev->device_name = ati_remote->rc_name;
0759     rdev->input_phys = ati_remote->rc_phys;
0760 
0761     usb_to_input_id(ati_remote->udev, &rdev->input_id);
0762     rdev->dev.parent = &ati_remote->interface->dev;
0763 }
0764 
0765 static int ati_remote_initialize(struct ati_remote *ati_remote)
0766 {
0767     struct usb_device *udev = ati_remote->udev;
0768     int pipe, maxp;
0769 
0770     init_waitqueue_head(&ati_remote->wait);
0771 
0772     /* Set up irq_urb */
0773     pipe = usb_rcvintpipe(udev, ati_remote->endpoint_in->bEndpointAddress);
0774     maxp = usb_maxpacket(udev, pipe);
0775     maxp = (maxp > DATA_BUFSIZE) ? DATA_BUFSIZE : maxp;
0776 
0777     usb_fill_int_urb(ati_remote->irq_urb, udev, pipe, ati_remote->inbuf,
0778              maxp, ati_remote_irq_in, ati_remote,
0779              ati_remote->endpoint_in->bInterval);
0780     ati_remote->irq_urb->transfer_dma = ati_remote->inbuf_dma;
0781     ati_remote->irq_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
0782 
0783     /* Set up out_urb */
0784     pipe = usb_sndintpipe(udev, ati_remote->endpoint_out->bEndpointAddress);
0785     maxp = usb_maxpacket(udev, pipe);
0786     maxp = (maxp > DATA_BUFSIZE) ? DATA_BUFSIZE : maxp;
0787 
0788     usb_fill_int_urb(ati_remote->out_urb, udev, pipe, ati_remote->outbuf,
0789              maxp, ati_remote_irq_out, ati_remote,
0790              ati_remote->endpoint_out->bInterval);
0791     ati_remote->out_urb->transfer_dma = ati_remote->outbuf_dma;
0792     ati_remote->out_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
0793 
0794     /* send initialization strings */
0795     if ((ati_remote_sendpacket(ati_remote, 0x8004, init1)) ||
0796         (ati_remote_sendpacket(ati_remote, 0x8007, init2))) {
0797         dev_err(&ati_remote->interface->dev,
0798              "Initializing ati_remote hardware failed.\n");
0799         return -EIO;
0800     }
0801 
0802     return 0;
0803 }
0804 
0805 /*
0806  * ati_remote_probe
0807  */
0808 static int ati_remote_probe(struct usb_interface *interface,
0809     const struct usb_device_id *id)
0810 {
0811     struct usb_device *udev = interface_to_usbdev(interface);
0812     struct usb_host_interface *iface_host = interface->cur_altsetting;
0813     struct usb_endpoint_descriptor *endpoint_in, *endpoint_out;
0814     struct ati_receiver_type *type = (struct ati_receiver_type *)id->driver_info;
0815     struct ati_remote *ati_remote;
0816     struct input_dev *input_dev;
0817     struct device *device = &interface->dev;
0818     struct rc_dev *rc_dev;
0819     int err = -ENOMEM;
0820 
0821     if (iface_host->desc.bNumEndpoints != 2) {
0822         dev_err(device, "%s: Unexpected desc.bNumEndpoints\n", __func__);
0823         return -ENODEV;
0824     }
0825 
0826     endpoint_in = &iface_host->endpoint[0].desc;
0827     endpoint_out = &iface_host->endpoint[1].desc;
0828 
0829     if (!usb_endpoint_is_int_in(endpoint_in)) {
0830         dev_err(device, "%s: Unexpected endpoint_in\n", __func__);
0831         return -ENODEV;
0832     }
0833     if (le16_to_cpu(endpoint_in->wMaxPacketSize) == 0) {
0834         dev_err(device, "%s: endpoint_in message size==0?\n", __func__);
0835         return -ENODEV;
0836     }
0837     if (!usb_endpoint_is_int_out(endpoint_out)) {
0838         dev_err(device, "%s: Unexpected endpoint_out\n", __func__);
0839         return -ENODEV;
0840     }
0841 
0842     ati_remote = kzalloc(sizeof (struct ati_remote), GFP_KERNEL);
0843     rc_dev = rc_allocate_device(RC_DRIVER_SCANCODE);
0844     if (!ati_remote || !rc_dev)
0845         goto exit_free_dev_rdev;
0846 
0847     /* Allocate URB buffers, URBs */
0848     if (ati_remote_alloc_buffers(udev, ati_remote))
0849         goto exit_free_buffers;
0850 
0851     ati_remote->endpoint_in = endpoint_in;
0852     ati_remote->endpoint_out = endpoint_out;
0853     ati_remote->udev = udev;
0854     ati_remote->rdev = rc_dev;
0855     ati_remote->interface = interface;
0856 
0857     usb_make_path(udev, ati_remote->rc_phys, sizeof(ati_remote->rc_phys));
0858     strscpy(ati_remote->mouse_phys, ati_remote->rc_phys,
0859         sizeof(ati_remote->mouse_phys));
0860 
0861     strlcat(ati_remote->rc_phys, "/input0", sizeof(ati_remote->rc_phys));
0862     strlcat(ati_remote->mouse_phys, "/input1", sizeof(ati_remote->mouse_phys));
0863 
0864     snprintf(ati_remote->rc_name, sizeof(ati_remote->rc_name), "%s%s%s",
0865         udev->manufacturer ?: "",
0866         udev->manufacturer && udev->product ? " " : "",
0867         udev->product ?: "");
0868 
0869     if (!strlen(ati_remote->rc_name))
0870         snprintf(ati_remote->rc_name, sizeof(ati_remote->rc_name),
0871             DRIVER_DESC "(%04x,%04x)",
0872             le16_to_cpu(ati_remote->udev->descriptor.idVendor),
0873             le16_to_cpu(ati_remote->udev->descriptor.idProduct));
0874 
0875     snprintf(ati_remote->mouse_name, sizeof(ati_remote->mouse_name),
0876          "%s mouse", ati_remote->rc_name);
0877 
0878     rc_dev->map_name = RC_MAP_ATI_X10; /* default map */
0879 
0880     /* set default keymap according to receiver model */
0881     if (type) {
0882         if (type->default_keymap)
0883             rc_dev->map_name = type->default_keymap;
0884         else if (type->get_default_keymap)
0885             rc_dev->map_name = type->get_default_keymap(interface);
0886     }
0887 
0888     ati_remote_rc_init(ati_remote);
0889     mutex_init(&ati_remote->open_mutex);
0890 
0891     /* Device Hardware Initialization - fills in ati_remote->idev from udev. */
0892     err = ati_remote_initialize(ati_remote);
0893     if (err)
0894         goto exit_kill_urbs;
0895 
0896     /* Set up and register rc device */
0897     err = rc_register_device(ati_remote->rdev);
0898     if (err)
0899         goto exit_kill_urbs;
0900 
0901     /* Set up and register mouse input device */
0902     if (mouse) {
0903         input_dev = input_allocate_device();
0904         if (!input_dev) {
0905             err = -ENOMEM;
0906             goto exit_unregister_device;
0907         }
0908 
0909         ati_remote->idev = input_dev;
0910         ati_remote_input_init(ati_remote);
0911         err = input_register_device(input_dev);
0912 
0913         if (err)
0914             goto exit_free_input_device;
0915     }
0916 
0917     usb_set_intfdata(interface, ati_remote);
0918     return 0;
0919 
0920  exit_free_input_device:
0921     input_free_device(input_dev);
0922  exit_unregister_device:
0923     rc_unregister_device(rc_dev);
0924     rc_dev = NULL;
0925  exit_kill_urbs:
0926     usb_kill_urb(ati_remote->irq_urb);
0927     usb_kill_urb(ati_remote->out_urb);
0928  exit_free_buffers:
0929     ati_remote_free_buffers(ati_remote);
0930  exit_free_dev_rdev:
0931      rc_free_device(rc_dev);
0932     kfree(ati_remote);
0933     return err;
0934 }
0935 
0936 /*
0937  * ati_remote_disconnect
0938  */
0939 static void ati_remote_disconnect(struct usb_interface *interface)
0940 {
0941     struct ati_remote *ati_remote;
0942 
0943     ati_remote = usb_get_intfdata(interface);
0944     usb_set_intfdata(interface, NULL);
0945     if (!ati_remote) {
0946         dev_warn(&interface->dev, "%s - null device?\n", __func__);
0947         return;
0948     }
0949 
0950     usb_kill_urb(ati_remote->irq_urb);
0951     usb_kill_urb(ati_remote->out_urb);
0952     if (ati_remote->idev)
0953         input_unregister_device(ati_remote->idev);
0954     rc_unregister_device(ati_remote->rdev);
0955     ati_remote_free_buffers(ati_remote);
0956     kfree(ati_remote);
0957 }
0958 
0959 /* usb specific object to register with the usb subsystem */
0960 static struct usb_driver ati_remote_driver = {
0961     .name         = "ati_remote",
0962     .probe        = ati_remote_probe,
0963     .disconnect   = ati_remote_disconnect,
0964     .id_table     = ati_remote_table,
0965 };
0966 
0967 module_usb_driver(ati_remote_driver);
0968 
0969 MODULE_AUTHOR(DRIVER_AUTHOR);
0970 MODULE_DESCRIPTION(DRIVER_DESC);
0971 MODULE_LICENSE("GPL");