Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 1 | /* |
| 2 | * ati_remote2 - ATI/Philips USB RF remote driver |
| 3 | * |
Ville Syrjala | 1971b9d | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 4 | * Copyright (C) 2005-2008 Ville Syrjala <syrjala@sci.fi> |
| 5 | * Copyright (C) 2007-2008 Peter Stokes <linux@dadeos.co.uk> |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 |
| 9 | * as published by the Free Software Foundation. |
| 10 | */ |
| 11 | |
David Brownell | ae0dadc | 2006-06-13 10:04:34 -0700 | [diff] [blame] | 12 | #include <linux/usb/input.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/slab.h> |
Paul Gortmaker | d2d8442 | 2011-07-03 13:53:48 -0400 | [diff] [blame] | 14 | #include <linux/module.h> |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 15 | |
| 16 | #define DRIVER_DESC "ATI/Philips USB RF remote driver" |
Ville Syrjala | 1971b9d | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 17 | #define DRIVER_VERSION "0.3" |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 18 | |
| 19 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 20 | MODULE_VERSION(DRIVER_VERSION); |
| 21 | MODULE_AUTHOR("Ville Syrjala <syrjala@sci.fi>"); |
| 22 | MODULE_LICENSE("GPL"); |
| 23 | |
Peter Stokes | a1421d3 | 2007-04-12 01:33:10 -0400 | [diff] [blame] | 24 | /* |
| 25 | * ATI Remote Wonder II Channel Configuration |
| 26 | * |
| 27 | * The remote control can by assigned one of sixteen "channels" in order to facilitate |
| 28 | * the use of multiple remote controls within range of each other. |
| 29 | * A remote's "channel" may be altered by pressing and holding the "PC" button for |
| 30 | * approximately 3 seconds, after which the button will slowly flash the count of the |
| 31 | * currently configured "channel", using the numeric keypad enter a number between 1 and |
Ville Syrjala | 1971b9d | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 32 | * 16 and then press the "PC" button again, the button will slowly flash the count of the |
Peter Stokes | a1421d3 | 2007-04-12 01:33:10 -0400 | [diff] [blame] | 33 | * newly configured "channel". |
| 34 | */ |
| 35 | |
Ville Syrjala | d329e33 | 2009-01-29 23:42:16 -0800 | [diff] [blame] | 36 | enum { |
| 37 | ATI_REMOTE2_MAX_CHANNEL_MASK = 0xFFFF, |
| 38 | ATI_REMOTE2_MAX_MODE_MASK = 0x1F, |
| 39 | }; |
| 40 | |
Ville Syrjala | 8a49cfa | 2009-01-29 23:42:16 -0800 | [diff] [blame] | 41 | static int ati_remote2_set_mask(const char *val, |
Rusty Russell | 9bbb9e5 | 2010-08-11 23:04:12 -0600 | [diff] [blame] | 42 | const struct kernel_param *kp, |
| 43 | unsigned int max) |
Ville Syrjala | 8a49cfa | 2009-01-29 23:42:16 -0800 | [diff] [blame] | 44 | { |
JJ Ding | 76496e7 | 2011-11-09 10:20:14 -0800 | [diff] [blame] | 45 | unsigned int mask; |
Ville Syrjala | 8a49cfa | 2009-01-29 23:42:16 -0800 | [diff] [blame] | 46 | int ret; |
| 47 | |
| 48 | if (!val) |
| 49 | return -EINVAL; |
| 50 | |
JJ Ding | 76496e7 | 2011-11-09 10:20:14 -0800 | [diff] [blame] | 51 | ret = kstrtouint(val, 0, &mask); |
Ville Syrjala | 8a49cfa | 2009-01-29 23:42:16 -0800 | [diff] [blame] | 52 | if (ret) |
| 53 | return ret; |
| 54 | |
| 55 | if (mask & ~max) |
| 56 | return -EINVAL; |
| 57 | |
| 58 | *(unsigned int *)kp->arg = mask; |
| 59 | |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | static int ati_remote2_set_channel_mask(const char *val, |
Rusty Russell | 9bbb9e5 | 2010-08-11 23:04:12 -0600 | [diff] [blame] | 64 | const struct kernel_param *kp) |
Ville Syrjala | 8a49cfa | 2009-01-29 23:42:16 -0800 | [diff] [blame] | 65 | { |
| 66 | pr_debug("%s()\n", __func__); |
| 67 | |
| 68 | return ati_remote2_set_mask(val, kp, ATI_REMOTE2_MAX_CHANNEL_MASK); |
| 69 | } |
| 70 | |
Rusty Russell | 9bbb9e5 | 2010-08-11 23:04:12 -0600 | [diff] [blame] | 71 | static int ati_remote2_get_channel_mask(char *buffer, |
| 72 | const struct kernel_param *kp) |
Ville Syrjala | 8a49cfa | 2009-01-29 23:42:16 -0800 | [diff] [blame] | 73 | { |
| 74 | pr_debug("%s()\n", __func__); |
| 75 | |
| 76 | return sprintf(buffer, "0x%04x", *(unsigned int *)kp->arg); |
| 77 | } |
| 78 | |
Rusty Russell | 9bbb9e5 | 2010-08-11 23:04:12 -0600 | [diff] [blame] | 79 | static int ati_remote2_set_mode_mask(const char *val, |
| 80 | const struct kernel_param *kp) |
Ville Syrjala | 8a49cfa | 2009-01-29 23:42:16 -0800 | [diff] [blame] | 81 | { |
| 82 | pr_debug("%s()\n", __func__); |
| 83 | |
| 84 | return ati_remote2_set_mask(val, kp, ATI_REMOTE2_MAX_MODE_MASK); |
| 85 | } |
| 86 | |
Rusty Russell | 9bbb9e5 | 2010-08-11 23:04:12 -0600 | [diff] [blame] | 87 | static int ati_remote2_get_mode_mask(char *buffer, |
| 88 | const struct kernel_param *kp) |
Ville Syrjala | 8a49cfa | 2009-01-29 23:42:16 -0800 | [diff] [blame] | 89 | { |
| 90 | pr_debug("%s()\n", __func__); |
| 91 | |
| 92 | return sprintf(buffer, "0x%02x", *(unsigned int *)kp->arg); |
| 93 | } |
| 94 | |
Ville Syrjala | d329e33 | 2009-01-29 23:42:16 -0800 | [diff] [blame] | 95 | static unsigned int channel_mask = ATI_REMOTE2_MAX_CHANNEL_MASK; |
Ville Syrjala | 8a49cfa | 2009-01-29 23:42:16 -0800 | [diff] [blame] | 96 | #define param_check_channel_mask(name, p) __param_check(name, p, unsigned int) |
Rusty Russell | 9bbb9e5 | 2010-08-11 23:04:12 -0600 | [diff] [blame] | 97 | static struct kernel_param_ops param_ops_channel_mask = { |
| 98 | .set = ati_remote2_set_channel_mask, |
| 99 | .get = ati_remote2_get_channel_mask, |
| 100 | }; |
Ville Syrjala | 8a49cfa | 2009-01-29 23:42:16 -0800 | [diff] [blame] | 101 | module_param(channel_mask, channel_mask, 0644); |
Peter Stokes | a1421d3 | 2007-04-12 01:33:10 -0400 | [diff] [blame] | 102 | MODULE_PARM_DESC(channel_mask, "Bitmask of channels to accept <15:Channel16>...<1:Channel2><0:Channel1>"); |
| 103 | |
Ville Syrjala | d329e33 | 2009-01-29 23:42:16 -0800 | [diff] [blame] | 104 | static unsigned int mode_mask = ATI_REMOTE2_MAX_MODE_MASK; |
Ville Syrjala | 8a49cfa | 2009-01-29 23:42:16 -0800 | [diff] [blame] | 105 | #define param_check_mode_mask(name, p) __param_check(name, p, unsigned int) |
Rusty Russell | 9bbb9e5 | 2010-08-11 23:04:12 -0600 | [diff] [blame] | 106 | static struct kernel_param_ops param_ops_mode_mask = { |
| 107 | .set = ati_remote2_set_mode_mask, |
| 108 | .get = ati_remote2_get_mode_mask, |
| 109 | }; |
Ville Syrjala | 8a49cfa | 2009-01-29 23:42:16 -0800 | [diff] [blame] | 110 | module_param(mode_mask, mode_mask, 0644); |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 111 | MODULE_PARM_DESC(mode_mask, "Bitmask of modes to accept <4:PC><3:AUX4><2:AUX3><1:AUX2><0:AUX1>"); |
| 112 | |
| 113 | static struct usb_device_id ati_remote2_id_table[] = { |
| 114 | { USB_DEVICE(0x0471, 0x0602) }, /* ATI Remote Wonder II */ |
| 115 | { } |
| 116 | }; |
| 117 | MODULE_DEVICE_TABLE(usb, ati_remote2_id_table); |
| 118 | |
Ville Syrjala | d6505ab | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 119 | static DEFINE_MUTEX(ati_remote2_mutex); |
| 120 | |
| 121 | enum { |
| 122 | ATI_REMOTE2_OPENED = 0x1, |
| 123 | ATI_REMOTE2_SUSPENDED = 0x2, |
| 124 | }; |
| 125 | |
Ville Syrjala | 1971b9d | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 126 | enum { |
| 127 | ATI_REMOTE2_AUX1, |
| 128 | ATI_REMOTE2_AUX2, |
| 129 | ATI_REMOTE2_AUX3, |
| 130 | ATI_REMOTE2_AUX4, |
| 131 | ATI_REMOTE2_PC, |
| 132 | ATI_REMOTE2_MODES, |
| 133 | }; |
| 134 | |
| 135 | static const struct { |
| 136 | u8 hw_code; |
| 137 | u16 keycode; |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 138 | } ati_remote2_key_table[] = { |
| 139 | { 0x00, KEY_0 }, |
| 140 | { 0x01, KEY_1 }, |
| 141 | { 0x02, KEY_2 }, |
| 142 | { 0x03, KEY_3 }, |
| 143 | { 0x04, KEY_4 }, |
| 144 | { 0x05, KEY_5 }, |
| 145 | { 0x06, KEY_6 }, |
| 146 | { 0x07, KEY_7 }, |
| 147 | { 0x08, KEY_8 }, |
| 148 | { 0x09, KEY_9 }, |
| 149 | { 0x0c, KEY_POWER }, |
| 150 | { 0x0d, KEY_MUTE }, |
| 151 | { 0x10, KEY_VOLUMEUP }, |
| 152 | { 0x11, KEY_VOLUMEDOWN }, |
| 153 | { 0x20, KEY_CHANNELUP }, |
| 154 | { 0x21, KEY_CHANNELDOWN }, |
| 155 | { 0x28, KEY_FORWARD }, |
| 156 | { 0x29, KEY_REWIND }, |
| 157 | { 0x2c, KEY_PLAY }, |
| 158 | { 0x30, KEY_PAUSE }, |
| 159 | { 0x31, KEY_STOP }, |
| 160 | { 0x37, KEY_RECORD }, |
| 161 | { 0x38, KEY_DVD }, |
| 162 | { 0x39, KEY_TV }, |
Ville Syrjala | 1971b9d | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 163 | { 0x3f, KEY_PROG1 }, /* AUX1-AUX4 and PC */ |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 164 | { 0x54, KEY_MENU }, |
| 165 | { 0x58, KEY_UP }, |
| 166 | { 0x59, KEY_DOWN }, |
| 167 | { 0x5a, KEY_LEFT }, |
| 168 | { 0x5b, KEY_RIGHT }, |
| 169 | { 0x5c, KEY_OK }, |
| 170 | { 0x78, KEY_A }, |
| 171 | { 0x79, KEY_B }, |
| 172 | { 0x7a, KEY_C }, |
| 173 | { 0x7b, KEY_D }, |
| 174 | { 0x7c, KEY_E }, |
| 175 | { 0x7d, KEY_F }, |
| 176 | { 0x82, KEY_ENTER }, |
| 177 | { 0x8e, KEY_VENDOR }, |
| 178 | { 0x96, KEY_COFFEE }, |
| 179 | { 0xa9, BTN_LEFT }, |
| 180 | { 0xaa, BTN_RIGHT }, |
| 181 | { 0xbe, KEY_QUESTION }, |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 182 | { 0xd0, KEY_EDIT }, |
Ville Syrjala | 1971b9d | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 183 | { 0xd5, KEY_FRONT }, |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 184 | { 0xf9, KEY_INFO }, |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | struct ati_remote2 { |
| 188 | struct input_dev *idev; |
| 189 | struct usb_device *udev; |
| 190 | |
| 191 | struct usb_interface *intf[2]; |
| 192 | struct usb_endpoint_descriptor *ep[2]; |
| 193 | struct urb *urb[2]; |
| 194 | void *buf[2]; |
| 195 | dma_addr_t buf_dma[2]; |
| 196 | |
| 197 | unsigned long jiffies; |
| 198 | int mode; |
| 199 | |
| 200 | char name[64]; |
| 201 | char phys[64]; |
Ville Syrjala | 1971b9d | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 202 | |
| 203 | /* Each mode (AUX1-AUX4 and PC) can have an independent keymap. */ |
| 204 | u16 keycode[ATI_REMOTE2_MODES][ARRAY_SIZE(ati_remote2_key_table)]; |
Ville Syrjala | d6505ab | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 205 | |
| 206 | unsigned int flags; |
Ville Syrjala | d329e33 | 2009-01-29 23:42:16 -0800 | [diff] [blame] | 207 | |
| 208 | unsigned int channel_mask; |
| 209 | unsigned int mode_mask; |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 210 | }; |
| 211 | |
| 212 | static int ati_remote2_probe(struct usb_interface *interface, const struct usb_device_id *id); |
| 213 | static void ati_remote2_disconnect(struct usb_interface *interface); |
Ville Syrjala | d6505ab | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 214 | static int ati_remote2_suspend(struct usb_interface *interface, pm_message_t message); |
| 215 | static int ati_remote2_resume(struct usb_interface *interface); |
Ville Syrjala | 169bc1e | 2009-01-29 23:42:16 -0800 | [diff] [blame] | 216 | static int ati_remote2_reset_resume(struct usb_interface *interface); |
| 217 | static int ati_remote2_pre_reset(struct usb_interface *interface); |
| 218 | static int ati_remote2_post_reset(struct usb_interface *interface); |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 219 | |
| 220 | static struct usb_driver ati_remote2_driver = { |
| 221 | .name = "ati_remote2", |
| 222 | .probe = ati_remote2_probe, |
| 223 | .disconnect = ati_remote2_disconnect, |
| 224 | .id_table = ati_remote2_id_table, |
Ville Syrjala | d6505ab | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 225 | .suspend = ati_remote2_suspend, |
| 226 | .resume = ati_remote2_resume, |
Ville Syrjala | 169bc1e | 2009-01-29 23:42:16 -0800 | [diff] [blame] | 227 | .reset_resume = ati_remote2_reset_resume, |
| 228 | .pre_reset = ati_remote2_pre_reset, |
| 229 | .post_reset = ati_remote2_post_reset, |
Ville Syrjala | d6505ab | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 230 | .supports_autosuspend = 1, |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 231 | }; |
| 232 | |
Ville Syrjala | d6505ab | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 233 | static int ati_remote2_submit_urbs(struct ati_remote2 *ar2) |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 234 | { |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 235 | int r; |
| 236 | |
| 237 | r = usb_submit_urb(ar2->urb[0], GFP_KERNEL); |
| 238 | if (r) { |
| 239 | dev_err(&ar2->intf[0]->dev, |
Ville Syrjala | d6505ab | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 240 | "%s(): usb_submit_urb() = %d\n", __func__, r); |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 241 | return r; |
| 242 | } |
| 243 | r = usb_submit_urb(ar2->urb[1], GFP_KERNEL); |
| 244 | if (r) { |
| 245 | usb_kill_urb(ar2->urb[0]); |
| 246 | dev_err(&ar2->intf[1]->dev, |
Ville Syrjala | d6505ab | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 247 | "%s(): usb_submit_urb() = %d\n", __func__, r); |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 248 | return r; |
| 249 | } |
| 250 | |
| 251 | return 0; |
| 252 | } |
| 253 | |
Ville Syrjala | d6505ab | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 254 | static void ati_remote2_kill_urbs(struct ati_remote2 *ar2) |
| 255 | { |
| 256 | usb_kill_urb(ar2->urb[1]); |
| 257 | usb_kill_urb(ar2->urb[0]); |
| 258 | } |
| 259 | |
| 260 | static int ati_remote2_open(struct input_dev *idev) |
| 261 | { |
| 262 | struct ati_remote2 *ar2 = input_get_drvdata(idev); |
| 263 | int r; |
| 264 | |
| 265 | dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__); |
| 266 | |
| 267 | r = usb_autopm_get_interface(ar2->intf[0]); |
| 268 | if (r) { |
| 269 | dev_err(&ar2->intf[0]->dev, |
| 270 | "%s(): usb_autopm_get_interface() = %d\n", __func__, r); |
| 271 | goto fail1; |
| 272 | } |
| 273 | |
| 274 | mutex_lock(&ati_remote2_mutex); |
| 275 | |
| 276 | if (!(ar2->flags & ATI_REMOTE2_SUSPENDED)) { |
| 277 | r = ati_remote2_submit_urbs(ar2); |
| 278 | if (r) |
| 279 | goto fail2; |
| 280 | } |
| 281 | |
| 282 | ar2->flags |= ATI_REMOTE2_OPENED; |
| 283 | |
| 284 | mutex_unlock(&ati_remote2_mutex); |
| 285 | |
| 286 | usb_autopm_put_interface(ar2->intf[0]); |
| 287 | |
| 288 | return 0; |
| 289 | |
| 290 | fail2: |
| 291 | mutex_unlock(&ati_remote2_mutex); |
| 292 | usb_autopm_put_interface(ar2->intf[0]); |
| 293 | fail1: |
| 294 | return r; |
| 295 | } |
| 296 | |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 297 | static void ati_remote2_close(struct input_dev *idev) |
| 298 | { |
Dmitry Torokhov | 7791bda | 2007-04-12 01:34:39 -0400 | [diff] [blame] | 299 | struct ati_remote2 *ar2 = input_get_drvdata(idev); |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 300 | |
Ville Syrjala | d6505ab | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 301 | dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__); |
| 302 | |
| 303 | mutex_lock(&ati_remote2_mutex); |
| 304 | |
| 305 | if (!(ar2->flags & ATI_REMOTE2_SUSPENDED)) |
| 306 | ati_remote2_kill_urbs(ar2); |
| 307 | |
| 308 | ar2->flags &= ~ATI_REMOTE2_OPENED; |
| 309 | |
| 310 | mutex_unlock(&ati_remote2_mutex); |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 311 | } |
| 312 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 313 | static void ati_remote2_input_mouse(struct ati_remote2 *ar2) |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 314 | { |
| 315 | struct input_dev *idev = ar2->idev; |
| 316 | u8 *data = ar2->buf[0]; |
Peter Stokes | a1421d3 | 2007-04-12 01:33:10 -0400 | [diff] [blame] | 317 | int channel, mode; |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 318 | |
Peter Stokes | a1421d3 | 2007-04-12 01:33:10 -0400 | [diff] [blame] | 319 | channel = data[0] >> 4; |
| 320 | |
Ville Syrjala | d329e33 | 2009-01-29 23:42:16 -0800 | [diff] [blame] | 321 | if (!((1 << channel) & ar2->channel_mask)) |
Peter Stokes | a1421d3 | 2007-04-12 01:33:10 -0400 | [diff] [blame] | 322 | return; |
| 323 | |
| 324 | mode = data[0] & 0x0F; |
| 325 | |
Ville Syrjala | 1971b9d | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 326 | if (mode > ATI_REMOTE2_PC) { |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 327 | dev_err(&ar2->intf[0]->dev, |
| 328 | "Unknown mode byte (%02x %02x %02x %02x)\n", |
| 329 | data[3], data[2], data[1], data[0]); |
| 330 | return; |
| 331 | } |
| 332 | |
Ville Syrjala | d329e33 | 2009-01-29 23:42:16 -0800 | [diff] [blame] | 333 | if (!((1 << mode) & ar2->mode_mask)) |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 334 | return; |
| 335 | |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 336 | input_event(idev, EV_REL, REL_X, (s8) data[1]); |
| 337 | input_event(idev, EV_REL, REL_Y, (s8) data[2]); |
| 338 | input_sync(idev); |
| 339 | } |
| 340 | |
| 341 | static int ati_remote2_lookup(unsigned int hw_code) |
| 342 | { |
| 343 | int i; |
| 344 | |
Ville Syrjala | 1971b9d | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 345 | for (i = 0; i < ARRAY_SIZE(ati_remote2_key_table); i++) |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 346 | if (ati_remote2_key_table[i].hw_code == hw_code) |
| 347 | return i; |
| 348 | |
| 349 | return -1; |
| 350 | } |
| 351 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 352 | static void ati_remote2_input_key(struct ati_remote2 *ar2) |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 353 | { |
| 354 | struct input_dev *idev = ar2->idev; |
| 355 | u8 *data = ar2->buf[1]; |
Peter Stokes | a1421d3 | 2007-04-12 01:33:10 -0400 | [diff] [blame] | 356 | int channel, mode, hw_code, index; |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 357 | |
Peter Stokes | a1421d3 | 2007-04-12 01:33:10 -0400 | [diff] [blame] | 358 | channel = data[0] >> 4; |
| 359 | |
Ville Syrjala | d329e33 | 2009-01-29 23:42:16 -0800 | [diff] [blame] | 360 | if (!((1 << channel) & ar2->channel_mask)) |
Peter Stokes | a1421d3 | 2007-04-12 01:33:10 -0400 | [diff] [blame] | 361 | return; |
| 362 | |
| 363 | mode = data[0] & 0x0F; |
| 364 | |
Ville Syrjala | 1971b9d | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 365 | if (mode > ATI_REMOTE2_PC) { |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 366 | dev_err(&ar2->intf[1]->dev, |
| 367 | "Unknown mode byte (%02x %02x %02x %02x)\n", |
| 368 | data[3], data[2], data[1], data[0]); |
| 369 | return; |
| 370 | } |
| 371 | |
| 372 | hw_code = data[2]; |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 373 | if (hw_code == 0x3f) { |
| 374 | /* |
| 375 | * For some incomprehensible reason the mouse pad generates |
| 376 | * events which look identical to the events from the last |
| 377 | * pressed mode key. Naturally we don't want to generate key |
| 378 | * events for the mouse pad so we filter out any subsequent |
| 379 | * events from the same mode key. |
| 380 | */ |
Peter Stokes | a1421d3 | 2007-04-12 01:33:10 -0400 | [diff] [blame] | 381 | if (ar2->mode == mode) |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 382 | return; |
| 383 | |
| 384 | if (data[1] == 0) |
Peter Stokes | a1421d3 | 2007-04-12 01:33:10 -0400 | [diff] [blame] | 385 | ar2->mode = mode; |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 386 | } |
| 387 | |
Ville Syrjala | d329e33 | 2009-01-29 23:42:16 -0800 | [diff] [blame] | 388 | if (!((1 << mode) & ar2->mode_mask)) |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 389 | return; |
| 390 | |
| 391 | index = ati_remote2_lookup(hw_code); |
| 392 | if (index < 0) { |
| 393 | dev_err(&ar2->intf[1]->dev, |
| 394 | "Unknown code byte (%02x %02x %02x %02x)\n", |
| 395 | data[3], data[2], data[1], data[0]); |
| 396 | return; |
| 397 | } |
| 398 | |
| 399 | switch (data[1]) { |
| 400 | case 0: /* release */ |
| 401 | break; |
| 402 | case 1: /* press */ |
| 403 | ar2->jiffies = jiffies + msecs_to_jiffies(idev->rep[REP_DELAY]); |
| 404 | break; |
| 405 | case 2: /* repeat */ |
| 406 | |
| 407 | /* No repeat for mouse buttons. */ |
Ville Syrjala | 1971b9d | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 408 | if (ar2->keycode[mode][index] == BTN_LEFT || |
| 409 | ar2->keycode[mode][index] == BTN_RIGHT) |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 410 | return; |
| 411 | |
| 412 | if (!time_after_eq(jiffies, ar2->jiffies)) |
| 413 | return; |
| 414 | |
| 415 | ar2->jiffies = jiffies + msecs_to_jiffies(idev->rep[REP_PERIOD]); |
| 416 | break; |
| 417 | default: |
| 418 | dev_err(&ar2->intf[1]->dev, |
| 419 | "Unknown state byte (%02x %02x %02x %02x)\n", |
| 420 | data[3], data[2], data[1], data[0]); |
| 421 | return; |
| 422 | } |
| 423 | |
Ville Syrjala | 1971b9d | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 424 | input_event(idev, EV_KEY, ar2->keycode[mode][index], data[1]); |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 425 | input_sync(idev); |
| 426 | } |
| 427 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 428 | static void ati_remote2_complete_mouse(struct urb *urb) |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 429 | { |
| 430 | struct ati_remote2 *ar2 = urb->context; |
| 431 | int r; |
| 432 | |
| 433 | switch (urb->status) { |
| 434 | case 0: |
Ville Syrjala | d6505ab | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 435 | usb_mark_last_busy(ar2->udev); |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 436 | ati_remote2_input_mouse(ar2); |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 437 | break; |
| 438 | case -ENOENT: |
| 439 | case -EILSEQ: |
| 440 | case -ECONNRESET: |
| 441 | case -ESHUTDOWN: |
| 442 | dev_dbg(&ar2->intf[0]->dev, |
Harvey Harrison | ea3e6c5 | 2008-05-05 11:36:18 -0400 | [diff] [blame] | 443 | "%s(): urb status = %d\n", __func__, urb->status); |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 444 | return; |
| 445 | default: |
Ville Syrjala | d6505ab | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 446 | usb_mark_last_busy(ar2->udev); |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 447 | dev_err(&ar2->intf[0]->dev, |
Harvey Harrison | ea3e6c5 | 2008-05-05 11:36:18 -0400 | [diff] [blame] | 448 | "%s(): urb status = %d\n", __func__, urb->status); |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | r = usb_submit_urb(urb, GFP_ATOMIC); |
| 452 | if (r) |
| 453 | dev_err(&ar2->intf[0]->dev, |
Harvey Harrison | ea3e6c5 | 2008-05-05 11:36:18 -0400 | [diff] [blame] | 454 | "%s(): usb_submit_urb() = %d\n", __func__, r); |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 455 | } |
| 456 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 457 | static void ati_remote2_complete_key(struct urb *urb) |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 458 | { |
| 459 | struct ati_remote2 *ar2 = urb->context; |
| 460 | int r; |
| 461 | |
| 462 | switch (urb->status) { |
| 463 | case 0: |
Ville Syrjala | d6505ab | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 464 | usb_mark_last_busy(ar2->udev); |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 465 | ati_remote2_input_key(ar2); |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 466 | break; |
| 467 | case -ENOENT: |
| 468 | case -EILSEQ: |
| 469 | case -ECONNRESET: |
| 470 | case -ESHUTDOWN: |
| 471 | dev_dbg(&ar2->intf[1]->dev, |
Harvey Harrison | ea3e6c5 | 2008-05-05 11:36:18 -0400 | [diff] [blame] | 472 | "%s(): urb status = %d\n", __func__, urb->status); |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 473 | return; |
| 474 | default: |
Ville Syrjala | d6505ab | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 475 | usb_mark_last_busy(ar2->udev); |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 476 | dev_err(&ar2->intf[1]->dev, |
Harvey Harrison | ea3e6c5 | 2008-05-05 11:36:18 -0400 | [diff] [blame] | 477 | "%s(): urb status = %d\n", __func__, urb->status); |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | r = usb_submit_urb(urb, GFP_ATOMIC); |
| 481 | if (r) |
| 482 | dev_err(&ar2->intf[1]->dev, |
Harvey Harrison | ea3e6c5 | 2008-05-05 11:36:18 -0400 | [diff] [blame] | 483 | "%s(): usb_submit_urb() = %d\n", __func__, r); |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 484 | } |
| 485 | |
Ville Syrjala | 1971b9d | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 486 | static int ati_remote2_getkeycode(struct input_dev *idev, |
Dmitry Torokhov | 1f7930c | 2010-09-15 19:36:34 -0700 | [diff] [blame] | 487 | struct input_keymap_entry *ke) |
Ville Syrjala | 1971b9d | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 488 | { |
| 489 | struct ati_remote2 *ar2 = input_get_drvdata(idev); |
Dmitry Torokhov | 58b9399 | 2010-03-08 22:37:10 -0800 | [diff] [blame] | 490 | unsigned int mode; |
Dmitry Torokhov | 1f7930c | 2010-09-15 19:36:34 -0700 | [diff] [blame] | 491 | int offset; |
| 492 | unsigned int index; |
| 493 | unsigned int scancode; |
Ville Syrjala | 1971b9d | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 494 | |
Dmitry Torokhov | 1f7930c | 2010-09-15 19:36:34 -0700 | [diff] [blame] | 495 | if (ke->flags & INPUT_KEYMAP_BY_INDEX) { |
| 496 | index = ke->index; |
| 497 | if (index >= ATI_REMOTE2_MODES * |
| 498 | ARRAY_SIZE(ati_remote2_key_table)) |
| 499 | return -EINVAL; |
Ville Syrjala | 1971b9d | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 500 | |
Dmitry Torokhov | 1f7930c | 2010-09-15 19:36:34 -0700 | [diff] [blame] | 501 | mode = ke->index / ARRAY_SIZE(ati_remote2_key_table); |
| 502 | offset = ke->index % ARRAY_SIZE(ati_remote2_key_table); |
| 503 | scancode = (mode << 8) + ati_remote2_key_table[offset].hw_code; |
| 504 | } else { |
| 505 | if (input_scancode_to_scalar(ke, &scancode)) |
| 506 | return -EINVAL; |
Ville Syrjala | 1971b9d | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 507 | |
Dmitry Torokhov | 1f7930c | 2010-09-15 19:36:34 -0700 | [diff] [blame] | 508 | mode = scancode >> 8; |
| 509 | if (mode > ATI_REMOTE2_PC) |
| 510 | return -EINVAL; |
| 511 | |
| 512 | offset = ati_remote2_lookup(scancode & 0xff); |
| 513 | if (offset < 0) |
| 514 | return -EINVAL; |
| 515 | |
| 516 | index = mode * ARRAY_SIZE(ati_remote2_key_table) + offset; |
| 517 | } |
| 518 | |
| 519 | ke->keycode = ar2->keycode[mode][offset]; |
| 520 | ke->len = sizeof(scancode); |
| 521 | memcpy(&ke->scancode, &scancode, sizeof(scancode)); |
| 522 | ke->index = index; |
| 523 | |
Ville Syrjala | 1971b9d | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 524 | return 0; |
| 525 | } |
| 526 | |
Dmitry Torokhov | 58b9399 | 2010-03-08 22:37:10 -0800 | [diff] [blame] | 527 | static int ati_remote2_setkeycode(struct input_dev *idev, |
Dmitry Torokhov | 1f7930c | 2010-09-15 19:36:34 -0700 | [diff] [blame] | 528 | const struct input_keymap_entry *ke, |
| 529 | unsigned int *old_keycode) |
Ville Syrjala | 1971b9d | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 530 | { |
| 531 | struct ati_remote2 *ar2 = input_get_drvdata(idev); |
Dmitry Torokhov | 1f7930c | 2010-09-15 19:36:34 -0700 | [diff] [blame] | 532 | unsigned int mode; |
| 533 | int offset; |
| 534 | unsigned int index; |
| 535 | unsigned int scancode; |
Ville Syrjala | 1971b9d | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 536 | |
Dmitry Torokhov | 1f7930c | 2010-09-15 19:36:34 -0700 | [diff] [blame] | 537 | if (ke->flags & INPUT_KEYMAP_BY_INDEX) { |
| 538 | if (ke->index >= ATI_REMOTE2_MODES * |
| 539 | ARRAY_SIZE(ati_remote2_key_table)) |
| 540 | return -EINVAL; |
Ville Syrjala | 1971b9d | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 541 | |
Dmitry Torokhov | 1f7930c | 2010-09-15 19:36:34 -0700 | [diff] [blame] | 542 | mode = ke->index / ARRAY_SIZE(ati_remote2_key_table); |
| 543 | offset = ke->index % ARRAY_SIZE(ati_remote2_key_table); |
| 544 | } else { |
| 545 | if (input_scancode_to_scalar(ke, &scancode)) |
| 546 | return -EINVAL; |
Ville Syrjala | 1971b9d | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 547 | |
Dmitry Torokhov | 1f7930c | 2010-09-15 19:36:34 -0700 | [diff] [blame] | 548 | mode = scancode >> 8; |
| 549 | if (mode > ATI_REMOTE2_PC) |
| 550 | return -EINVAL; |
| 551 | |
| 552 | offset = ati_remote2_lookup(scancode & 0xff); |
| 553 | if (offset < 0) |
| 554 | return -EINVAL; |
| 555 | } |
| 556 | |
| 557 | *old_keycode = ar2->keycode[mode][offset]; |
| 558 | ar2->keycode[mode][offset] = ke->keycode; |
| 559 | __set_bit(ke->keycode, idev->keybit); |
Ville Syrjala | 1971b9d | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 560 | |
| 561 | for (mode = 0; mode < ATI_REMOTE2_MODES; mode++) { |
| 562 | for (index = 0; index < ARRAY_SIZE(ati_remote2_key_table); index++) { |
Dmitry Torokhov | 1f7930c | 2010-09-15 19:36:34 -0700 | [diff] [blame] | 563 | if (ar2->keycode[mode][index] == *old_keycode) |
Ville Syrjala | 1971b9d | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 564 | return 0; |
| 565 | } |
| 566 | } |
| 567 | |
Dmitry Torokhov | 1f7930c | 2010-09-15 19:36:34 -0700 | [diff] [blame] | 568 | __clear_bit(*old_keycode, idev->keybit); |
Ville Syrjala | 1971b9d | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 569 | |
| 570 | return 0; |
| 571 | } |
| 572 | |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 573 | static int ati_remote2_input_init(struct ati_remote2 *ar2) |
| 574 | { |
| 575 | struct input_dev *idev; |
Ville Syrjala | 1971b9d | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 576 | int index, mode, retval; |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 577 | |
| 578 | idev = input_allocate_device(); |
| 579 | if (!idev) |
| 580 | return -ENOMEM; |
| 581 | |
| 582 | ar2->idev = idev; |
Dmitry Torokhov | 7791bda | 2007-04-12 01:34:39 -0400 | [diff] [blame] | 583 | input_set_drvdata(idev, ar2); |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 584 | |
Jiri Slaby | 7b19ada | 2007-10-18 23:40:32 -0700 | [diff] [blame] | 585 | idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) | BIT_MASK(EV_REL); |
| 586 | idev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) | |
| 587 | BIT_MASK(BTN_RIGHT); |
| 588 | idev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y); |
Ville Syrjala | 1971b9d | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 589 | |
| 590 | for (mode = 0; mode < ATI_REMOTE2_MODES; mode++) { |
| 591 | for (index = 0; index < ARRAY_SIZE(ati_remote2_key_table); index++) { |
| 592 | ar2->keycode[mode][index] = ati_remote2_key_table[index].keycode; |
Ville Syrjala | 225c988 | 2009-05-18 16:01:25 -0700 | [diff] [blame] | 593 | __set_bit(ar2->keycode[mode][index], idev->keybit); |
Ville Syrjala | 1971b9d | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 594 | } |
| 595 | } |
| 596 | |
| 597 | /* AUX1-AUX4 and PC generate the same scancode. */ |
| 598 | index = ati_remote2_lookup(0x3f); |
| 599 | ar2->keycode[ATI_REMOTE2_AUX1][index] = KEY_PROG1; |
| 600 | ar2->keycode[ATI_REMOTE2_AUX2][index] = KEY_PROG2; |
| 601 | ar2->keycode[ATI_REMOTE2_AUX3][index] = KEY_PROG3; |
| 602 | ar2->keycode[ATI_REMOTE2_AUX4][index] = KEY_PROG4; |
| 603 | ar2->keycode[ATI_REMOTE2_PC][index] = KEY_PC; |
Ville Syrjala | 225c988 | 2009-05-18 16:01:25 -0700 | [diff] [blame] | 604 | __set_bit(KEY_PROG1, idev->keybit); |
| 605 | __set_bit(KEY_PROG2, idev->keybit); |
| 606 | __set_bit(KEY_PROG3, idev->keybit); |
| 607 | __set_bit(KEY_PROG4, idev->keybit); |
| 608 | __set_bit(KEY_PC, idev->keybit); |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 609 | |
| 610 | idev->rep[REP_DELAY] = 250; |
| 611 | idev->rep[REP_PERIOD] = 33; |
| 612 | |
| 613 | idev->open = ati_remote2_open; |
| 614 | idev->close = ati_remote2_close; |
| 615 | |
Dmitry Torokhov | aebd636 | 2011-01-31 21:06:39 -0800 | [diff] [blame] | 616 | idev->getkeycode = ati_remote2_getkeycode; |
| 617 | idev->setkeycode = ati_remote2_setkeycode; |
Ville Syrjala | 1971b9d | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 618 | |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 619 | idev->name = ar2->name; |
| 620 | idev->phys = ar2->phys; |
| 621 | |
| 622 | usb_to_input_id(ar2->udev, &idev->id); |
Dmitry Torokhov | c0f82d5 | 2007-04-12 01:35:03 -0400 | [diff] [blame] | 623 | idev->dev.parent = &ar2->udev->dev; |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 624 | |
Dmitry Torokhov | 5014186 | 2007-04-12 01:33:39 -0400 | [diff] [blame] | 625 | retval = input_register_device(idev); |
| 626 | if (retval) |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 627 | input_free_device(idev); |
| 628 | |
Dmitry Torokhov | 5014186 | 2007-04-12 01:33:39 -0400 | [diff] [blame] | 629 | return retval; |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | static int ati_remote2_urb_init(struct ati_remote2 *ar2) |
| 633 | { |
| 634 | struct usb_device *udev = ar2->udev; |
| 635 | int i, pipe, maxp; |
| 636 | |
| 637 | for (i = 0; i < 2; i++) { |
Daniel Mack | 997ea58 | 2010-04-12 13:17:25 +0200 | [diff] [blame] | 638 | ar2->buf[i] = usb_alloc_coherent(udev, 4, GFP_KERNEL, &ar2->buf_dma[i]); |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 639 | if (!ar2->buf[i]) |
| 640 | return -ENOMEM; |
| 641 | |
| 642 | ar2->urb[i] = usb_alloc_urb(0, GFP_KERNEL); |
| 643 | if (!ar2->urb[i]) |
| 644 | return -ENOMEM; |
| 645 | |
| 646 | pipe = usb_rcvintpipe(udev, ar2->ep[i]->bEndpointAddress); |
| 647 | maxp = usb_maxpacket(udev, pipe, usb_pipeout(pipe)); |
| 648 | maxp = maxp > 4 ? 4 : maxp; |
| 649 | |
| 650 | usb_fill_int_urb(ar2->urb[i], udev, pipe, ar2->buf[i], maxp, |
| 651 | i ? ati_remote2_complete_key : ati_remote2_complete_mouse, |
| 652 | ar2, ar2->ep[i]->bInterval); |
| 653 | ar2->urb[i]->transfer_dma = ar2->buf_dma[i]; |
| 654 | ar2->urb[i]->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
| 655 | } |
| 656 | |
| 657 | return 0; |
| 658 | } |
| 659 | |
| 660 | static void ati_remote2_urb_cleanup(struct ati_remote2 *ar2) |
| 661 | { |
| 662 | int i; |
| 663 | |
| 664 | for (i = 0; i < 2; i++) { |
Mariusz Kozlowski | 2381526 | 2006-11-08 15:35:50 +0100 | [diff] [blame] | 665 | usb_free_urb(ar2->urb[i]); |
Daniel Mack | 997ea58 | 2010-04-12 13:17:25 +0200 | [diff] [blame] | 666 | usb_free_coherent(ar2->udev, 4, ar2->buf[i], ar2->buf_dma[i]); |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 667 | } |
| 668 | } |
| 669 | |
Ville Syrjala | d329e33 | 2009-01-29 23:42:16 -0800 | [diff] [blame] | 670 | static int ati_remote2_setup(struct ati_remote2 *ar2, unsigned int ch_mask) |
Peter Stokes | a1421d3 | 2007-04-12 01:33:10 -0400 | [diff] [blame] | 671 | { |
| 672 | int r, i, channel; |
| 673 | |
| 674 | /* |
| 675 | * Configure receiver to only accept input from remote "channel" |
| 676 | * channel == 0 -> Accept input from any remote channel |
| 677 | * channel == 1 -> Only accept input from remote channel 1 |
| 678 | * channel == 2 -> Only accept input from remote channel 2 |
| 679 | * ... |
| 680 | * channel == 16 -> Only accept input from remote channel 16 |
| 681 | */ |
| 682 | |
| 683 | channel = 0; |
| 684 | for (i = 0; i < 16; i++) { |
Ville Syrjala | d329e33 | 2009-01-29 23:42:16 -0800 | [diff] [blame] | 685 | if ((1 << i) & ch_mask) { |
| 686 | if (!(~(1 << i) & ch_mask)) |
Peter Stokes | a1421d3 | 2007-04-12 01:33:10 -0400 | [diff] [blame] | 687 | channel = i + 1; |
| 688 | break; |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | r = usb_control_msg(ar2->udev, usb_sndctrlpipe(ar2->udev, 0), |
| 693 | 0x20, |
| 694 | USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE, |
| 695 | channel, 0x0, NULL, 0, USB_CTRL_SET_TIMEOUT); |
| 696 | if (r) { |
| 697 | dev_err(&ar2->udev->dev, "%s - failed to set channel due to error: %d\n", |
Harvey Harrison | ea3e6c5 | 2008-05-05 11:36:18 -0400 | [diff] [blame] | 698 | __func__, r); |
Peter Stokes | a1421d3 | 2007-04-12 01:33:10 -0400 | [diff] [blame] | 699 | return r; |
| 700 | } |
| 701 | |
| 702 | return 0; |
| 703 | } |
| 704 | |
Ville Syrjala | d329e33 | 2009-01-29 23:42:16 -0800 | [diff] [blame] | 705 | static ssize_t ati_remote2_show_channel_mask(struct device *dev, |
| 706 | struct device_attribute *attr, |
| 707 | char *buf) |
| 708 | { |
| 709 | struct usb_device *udev = to_usb_device(dev); |
| 710 | struct usb_interface *intf = usb_ifnum_to_if(udev, 0); |
| 711 | struct ati_remote2 *ar2 = usb_get_intfdata(intf); |
| 712 | |
| 713 | return sprintf(buf, "0x%04x\n", ar2->channel_mask); |
| 714 | } |
| 715 | |
| 716 | static ssize_t ati_remote2_store_channel_mask(struct device *dev, |
| 717 | struct device_attribute *attr, |
| 718 | const char *buf, size_t count) |
| 719 | { |
| 720 | struct usb_device *udev = to_usb_device(dev); |
| 721 | struct usb_interface *intf = usb_ifnum_to_if(udev, 0); |
| 722 | struct ati_remote2 *ar2 = usb_get_intfdata(intf); |
JJ Ding | 76496e7 | 2011-11-09 10:20:14 -0800 | [diff] [blame] | 723 | unsigned int mask; |
Ville Syrjala | d329e33 | 2009-01-29 23:42:16 -0800 | [diff] [blame] | 724 | int r; |
| 725 | |
JJ Ding | 76496e7 | 2011-11-09 10:20:14 -0800 | [diff] [blame] | 726 | r = kstrtouint(buf, 0, &mask); |
| 727 | if (r) |
| 728 | return r; |
Ville Syrjala | d329e33 | 2009-01-29 23:42:16 -0800 | [diff] [blame] | 729 | |
| 730 | if (mask & ~ATI_REMOTE2_MAX_CHANNEL_MASK) |
| 731 | return -EINVAL; |
| 732 | |
| 733 | r = usb_autopm_get_interface(ar2->intf[0]); |
| 734 | if (r) { |
| 735 | dev_err(&ar2->intf[0]->dev, |
| 736 | "%s(): usb_autopm_get_interface() = %d\n", __func__, r); |
| 737 | return r; |
| 738 | } |
| 739 | |
| 740 | mutex_lock(&ati_remote2_mutex); |
| 741 | |
Ville Syrjala | 7388754 | 2011-05-04 20:54:27 -0700 | [diff] [blame] | 742 | if (mask != ar2->channel_mask) { |
| 743 | r = ati_remote2_setup(ar2, mask); |
| 744 | if (!r) |
| 745 | ar2->channel_mask = mask; |
| 746 | } |
Ville Syrjala | d329e33 | 2009-01-29 23:42:16 -0800 | [diff] [blame] | 747 | |
| 748 | mutex_unlock(&ati_remote2_mutex); |
| 749 | |
| 750 | usb_autopm_put_interface(ar2->intf[0]); |
| 751 | |
Ville Syrjala | 7388754 | 2011-05-04 20:54:27 -0700 | [diff] [blame] | 752 | return r ? r : count; |
Ville Syrjala | d329e33 | 2009-01-29 23:42:16 -0800 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | static ssize_t ati_remote2_show_mode_mask(struct device *dev, |
| 756 | struct device_attribute *attr, |
| 757 | char *buf) |
| 758 | { |
| 759 | struct usb_device *udev = to_usb_device(dev); |
| 760 | struct usb_interface *intf = usb_ifnum_to_if(udev, 0); |
| 761 | struct ati_remote2 *ar2 = usb_get_intfdata(intf); |
| 762 | |
| 763 | return sprintf(buf, "0x%02x\n", ar2->mode_mask); |
| 764 | } |
| 765 | |
| 766 | static ssize_t ati_remote2_store_mode_mask(struct device *dev, |
| 767 | struct device_attribute *attr, |
| 768 | const char *buf, size_t count) |
| 769 | { |
| 770 | struct usb_device *udev = to_usb_device(dev); |
| 771 | struct usb_interface *intf = usb_ifnum_to_if(udev, 0); |
| 772 | struct ati_remote2 *ar2 = usb_get_intfdata(intf); |
JJ Ding | 76496e7 | 2011-11-09 10:20:14 -0800 | [diff] [blame] | 773 | unsigned int mask; |
| 774 | int err; |
Ville Syrjala | d329e33 | 2009-01-29 23:42:16 -0800 | [diff] [blame] | 775 | |
JJ Ding | 76496e7 | 2011-11-09 10:20:14 -0800 | [diff] [blame] | 776 | err = kstrtouint(buf, 0, &mask); |
| 777 | if (err) |
| 778 | return err; |
Ville Syrjala | d329e33 | 2009-01-29 23:42:16 -0800 | [diff] [blame] | 779 | |
| 780 | if (mask & ~ATI_REMOTE2_MAX_MODE_MASK) |
| 781 | return -EINVAL; |
| 782 | |
| 783 | ar2->mode_mask = mask; |
| 784 | |
| 785 | return count; |
| 786 | } |
| 787 | |
| 788 | static DEVICE_ATTR(channel_mask, 0644, ati_remote2_show_channel_mask, |
| 789 | ati_remote2_store_channel_mask); |
| 790 | |
| 791 | static DEVICE_ATTR(mode_mask, 0644, ati_remote2_show_mode_mask, |
| 792 | ati_remote2_store_mode_mask); |
| 793 | |
| 794 | static struct attribute *ati_remote2_attrs[] = { |
| 795 | &dev_attr_channel_mask.attr, |
| 796 | &dev_attr_mode_mask.attr, |
| 797 | NULL, |
| 798 | }; |
| 799 | |
| 800 | static struct attribute_group ati_remote2_attr_group = { |
| 801 | .attrs = ati_remote2_attrs, |
| 802 | }; |
| 803 | |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 804 | static int ati_remote2_probe(struct usb_interface *interface, const struct usb_device_id *id) |
| 805 | { |
| 806 | struct usb_device *udev = interface_to_usbdev(interface); |
| 807 | struct usb_host_interface *alt = interface->cur_altsetting; |
| 808 | struct ati_remote2 *ar2; |
| 809 | int r; |
| 810 | |
| 811 | if (alt->desc.bInterfaceNumber) |
| 812 | return -ENODEV; |
| 813 | |
| 814 | ar2 = kzalloc(sizeof (struct ati_remote2), GFP_KERNEL); |
| 815 | if (!ar2) |
| 816 | return -ENOMEM; |
| 817 | |
| 818 | ar2->udev = udev; |
| 819 | |
| 820 | ar2->intf[0] = interface; |
| 821 | ar2->ep[0] = &alt->endpoint[0].desc; |
| 822 | |
| 823 | ar2->intf[1] = usb_ifnum_to_if(udev, 1); |
| 824 | r = usb_driver_claim_interface(&ati_remote2_driver, ar2->intf[1], ar2); |
| 825 | if (r) |
| 826 | goto fail1; |
| 827 | alt = ar2->intf[1]->cur_altsetting; |
| 828 | ar2->ep[1] = &alt->endpoint[0].desc; |
| 829 | |
| 830 | r = ati_remote2_urb_init(ar2); |
| 831 | if (r) |
| 832 | goto fail2; |
| 833 | |
Ville Syrjala | 8a49cfa | 2009-01-29 23:42:16 -0800 | [diff] [blame] | 834 | ar2->channel_mask = channel_mask; |
| 835 | ar2->mode_mask = mode_mask; |
Ville Syrjala | d329e33 | 2009-01-29 23:42:16 -0800 | [diff] [blame] | 836 | |
| 837 | r = ati_remote2_setup(ar2, ar2->channel_mask); |
Peter Stokes | a1421d3 | 2007-04-12 01:33:10 -0400 | [diff] [blame] | 838 | if (r) |
| 839 | goto fail2; |
| 840 | |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 841 | usb_make_path(udev, ar2->phys, sizeof(ar2->phys)); |
| 842 | strlcat(ar2->phys, "/input0", sizeof(ar2->phys)); |
| 843 | |
| 844 | strlcat(ar2->name, "ATI Remote Wonder II", sizeof(ar2->name)); |
| 845 | |
Ville Syrjala | d329e33 | 2009-01-29 23:42:16 -0800 | [diff] [blame] | 846 | r = sysfs_create_group(&udev->dev.kobj, &ati_remote2_attr_group); |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 847 | if (r) |
| 848 | goto fail2; |
| 849 | |
Ville Syrjala | d329e33 | 2009-01-29 23:42:16 -0800 | [diff] [blame] | 850 | r = ati_remote2_input_init(ar2); |
| 851 | if (r) |
| 852 | goto fail3; |
| 853 | |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 854 | usb_set_intfdata(interface, ar2); |
| 855 | |
Ville Syrjala | d6505ab | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 856 | interface->needs_remote_wakeup = 1; |
| 857 | |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 858 | return 0; |
| 859 | |
Ville Syrjala | d329e33 | 2009-01-29 23:42:16 -0800 | [diff] [blame] | 860 | fail3: |
| 861 | sysfs_remove_group(&udev->dev.kobj, &ati_remote2_attr_group); |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 862 | fail2: |
| 863 | ati_remote2_urb_cleanup(ar2); |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 864 | usb_driver_release_interface(&ati_remote2_driver, ar2->intf[1]); |
| 865 | fail1: |
| 866 | kfree(ar2); |
| 867 | |
| 868 | return r; |
| 869 | } |
| 870 | |
| 871 | static void ati_remote2_disconnect(struct usb_interface *interface) |
| 872 | { |
| 873 | struct ati_remote2 *ar2; |
| 874 | struct usb_host_interface *alt = interface->cur_altsetting; |
| 875 | |
| 876 | if (alt->desc.bInterfaceNumber) |
| 877 | return; |
| 878 | |
| 879 | ar2 = usb_get_intfdata(interface); |
| 880 | usb_set_intfdata(interface, NULL); |
| 881 | |
| 882 | input_unregister_device(ar2->idev); |
| 883 | |
Ville Syrjala | d329e33 | 2009-01-29 23:42:16 -0800 | [diff] [blame] | 884 | sysfs_remove_group(&ar2->udev->dev.kobj, &ati_remote2_attr_group); |
| 885 | |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 886 | ati_remote2_urb_cleanup(ar2); |
| 887 | |
| 888 | usb_driver_release_interface(&ati_remote2_driver, ar2->intf[1]); |
| 889 | |
| 890 | kfree(ar2); |
| 891 | } |
| 892 | |
Ville Syrjala | d6505ab | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 893 | static int ati_remote2_suspend(struct usb_interface *interface, |
| 894 | pm_message_t message) |
| 895 | { |
| 896 | struct ati_remote2 *ar2; |
| 897 | struct usb_host_interface *alt = interface->cur_altsetting; |
| 898 | |
| 899 | if (alt->desc.bInterfaceNumber) |
| 900 | return 0; |
| 901 | |
| 902 | ar2 = usb_get_intfdata(interface); |
| 903 | |
| 904 | dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__); |
| 905 | |
| 906 | mutex_lock(&ati_remote2_mutex); |
| 907 | |
| 908 | if (ar2->flags & ATI_REMOTE2_OPENED) |
| 909 | ati_remote2_kill_urbs(ar2); |
| 910 | |
| 911 | ar2->flags |= ATI_REMOTE2_SUSPENDED; |
| 912 | |
| 913 | mutex_unlock(&ati_remote2_mutex); |
| 914 | |
| 915 | return 0; |
| 916 | } |
| 917 | |
| 918 | static int ati_remote2_resume(struct usb_interface *interface) |
| 919 | { |
| 920 | struct ati_remote2 *ar2; |
| 921 | struct usb_host_interface *alt = interface->cur_altsetting; |
| 922 | int r = 0; |
| 923 | |
| 924 | if (alt->desc.bInterfaceNumber) |
| 925 | return 0; |
| 926 | |
| 927 | ar2 = usb_get_intfdata(interface); |
| 928 | |
| 929 | dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__); |
| 930 | |
| 931 | mutex_lock(&ati_remote2_mutex); |
| 932 | |
| 933 | if (ar2->flags & ATI_REMOTE2_OPENED) |
| 934 | r = ati_remote2_submit_urbs(ar2); |
| 935 | |
| 936 | if (!r) |
| 937 | ar2->flags &= ~ATI_REMOTE2_SUSPENDED; |
| 938 | |
| 939 | mutex_unlock(&ati_remote2_mutex); |
| 940 | |
| 941 | return r; |
| 942 | } |
| 943 | |
Ville Syrjala | 169bc1e | 2009-01-29 23:42:16 -0800 | [diff] [blame] | 944 | static int ati_remote2_reset_resume(struct usb_interface *interface) |
| 945 | { |
| 946 | struct ati_remote2 *ar2; |
| 947 | struct usb_host_interface *alt = interface->cur_altsetting; |
| 948 | int r = 0; |
| 949 | |
| 950 | if (alt->desc.bInterfaceNumber) |
| 951 | return 0; |
| 952 | |
| 953 | ar2 = usb_get_intfdata(interface); |
| 954 | |
| 955 | dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__); |
| 956 | |
| 957 | mutex_lock(&ati_remote2_mutex); |
| 958 | |
Ville Syrjala | d329e33 | 2009-01-29 23:42:16 -0800 | [diff] [blame] | 959 | r = ati_remote2_setup(ar2, ar2->channel_mask); |
Ville Syrjala | 169bc1e | 2009-01-29 23:42:16 -0800 | [diff] [blame] | 960 | if (r) |
| 961 | goto out; |
| 962 | |
| 963 | if (ar2->flags & ATI_REMOTE2_OPENED) |
| 964 | r = ati_remote2_submit_urbs(ar2); |
| 965 | |
| 966 | if (!r) |
| 967 | ar2->flags &= ~ATI_REMOTE2_SUSPENDED; |
| 968 | |
| 969 | out: |
| 970 | mutex_unlock(&ati_remote2_mutex); |
| 971 | |
| 972 | return r; |
| 973 | } |
| 974 | |
| 975 | static int ati_remote2_pre_reset(struct usb_interface *interface) |
| 976 | { |
| 977 | struct ati_remote2 *ar2; |
| 978 | struct usb_host_interface *alt = interface->cur_altsetting; |
| 979 | |
| 980 | if (alt->desc.bInterfaceNumber) |
| 981 | return 0; |
| 982 | |
| 983 | ar2 = usb_get_intfdata(interface); |
| 984 | |
| 985 | dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__); |
| 986 | |
| 987 | mutex_lock(&ati_remote2_mutex); |
| 988 | |
| 989 | if (ar2->flags == ATI_REMOTE2_OPENED) |
| 990 | ati_remote2_kill_urbs(ar2); |
| 991 | |
| 992 | return 0; |
| 993 | } |
| 994 | |
| 995 | static int ati_remote2_post_reset(struct usb_interface *interface) |
| 996 | { |
| 997 | struct ati_remote2 *ar2; |
| 998 | struct usb_host_interface *alt = interface->cur_altsetting; |
| 999 | int r = 0; |
| 1000 | |
| 1001 | if (alt->desc.bInterfaceNumber) |
| 1002 | return 0; |
| 1003 | |
| 1004 | ar2 = usb_get_intfdata(interface); |
| 1005 | |
| 1006 | dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__); |
| 1007 | |
| 1008 | if (ar2->flags == ATI_REMOTE2_OPENED) |
| 1009 | r = ati_remote2_submit_urbs(ar2); |
| 1010 | |
| 1011 | mutex_unlock(&ati_remote2_mutex); |
| 1012 | |
| 1013 | return r; |
| 1014 | } |
| 1015 | |
Greg Kroah-Hartman | 08642e7 | 2011-11-18 09:48:31 -0800 | [diff] [blame] | 1016 | module_usb_driver(ati_remote2_driver); |