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> |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 13 | |
| 14 | #define DRIVER_DESC "ATI/Philips USB RF remote driver" |
Ville Syrjala | 1971b9d | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 15 | #define DRIVER_VERSION "0.3" |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 16 | |
| 17 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 18 | MODULE_VERSION(DRIVER_VERSION); |
| 19 | MODULE_AUTHOR("Ville Syrjala <syrjala@sci.fi>"); |
| 20 | MODULE_LICENSE("GPL"); |
| 21 | |
Peter Stokes | a1421d3 | 2007-04-12 01:33:10 -0400 | [diff] [blame] | 22 | /* |
| 23 | * ATI Remote Wonder II Channel Configuration |
| 24 | * |
| 25 | * The remote control can by assigned one of sixteen "channels" in order to facilitate |
| 26 | * the use of multiple remote controls within range of each other. |
| 27 | * A remote's "channel" may be altered by pressing and holding the "PC" button for |
| 28 | * approximately 3 seconds, after which the button will slowly flash the count of the |
| 29 | * 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] | 30 | * 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] | 31 | * newly configured "channel". |
| 32 | */ |
| 33 | |
| 34 | static unsigned int channel_mask = 0xFFFF; |
| 35 | module_param(channel_mask, uint, 0644); |
| 36 | MODULE_PARM_DESC(channel_mask, "Bitmask of channels to accept <15:Channel16>...<1:Channel2><0:Channel1>"); |
| 37 | |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 38 | static unsigned int mode_mask = 0x1F; |
| 39 | module_param(mode_mask, uint, 0644); |
| 40 | MODULE_PARM_DESC(mode_mask, "Bitmask of modes to accept <4:PC><3:AUX4><2:AUX3><1:AUX2><0:AUX1>"); |
| 41 | |
| 42 | static struct usb_device_id ati_remote2_id_table[] = { |
| 43 | { USB_DEVICE(0x0471, 0x0602) }, /* ATI Remote Wonder II */ |
| 44 | { } |
| 45 | }; |
| 46 | MODULE_DEVICE_TABLE(usb, ati_remote2_id_table); |
| 47 | |
Ville Syrjala | d6505ab | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 48 | static DEFINE_MUTEX(ati_remote2_mutex); |
| 49 | |
| 50 | enum { |
| 51 | ATI_REMOTE2_OPENED = 0x1, |
| 52 | ATI_REMOTE2_SUSPENDED = 0x2, |
| 53 | }; |
| 54 | |
Ville Syrjala | 1971b9d | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 55 | enum { |
| 56 | ATI_REMOTE2_AUX1, |
| 57 | ATI_REMOTE2_AUX2, |
| 58 | ATI_REMOTE2_AUX3, |
| 59 | ATI_REMOTE2_AUX4, |
| 60 | ATI_REMOTE2_PC, |
| 61 | ATI_REMOTE2_MODES, |
| 62 | }; |
| 63 | |
| 64 | static const struct { |
| 65 | u8 hw_code; |
| 66 | u16 keycode; |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 67 | } ati_remote2_key_table[] = { |
| 68 | { 0x00, KEY_0 }, |
| 69 | { 0x01, KEY_1 }, |
| 70 | { 0x02, KEY_2 }, |
| 71 | { 0x03, KEY_3 }, |
| 72 | { 0x04, KEY_4 }, |
| 73 | { 0x05, KEY_5 }, |
| 74 | { 0x06, KEY_6 }, |
| 75 | { 0x07, KEY_7 }, |
| 76 | { 0x08, KEY_8 }, |
| 77 | { 0x09, KEY_9 }, |
| 78 | { 0x0c, KEY_POWER }, |
| 79 | { 0x0d, KEY_MUTE }, |
| 80 | { 0x10, KEY_VOLUMEUP }, |
| 81 | { 0x11, KEY_VOLUMEDOWN }, |
| 82 | { 0x20, KEY_CHANNELUP }, |
| 83 | { 0x21, KEY_CHANNELDOWN }, |
| 84 | { 0x28, KEY_FORWARD }, |
| 85 | { 0x29, KEY_REWIND }, |
| 86 | { 0x2c, KEY_PLAY }, |
| 87 | { 0x30, KEY_PAUSE }, |
| 88 | { 0x31, KEY_STOP }, |
| 89 | { 0x37, KEY_RECORD }, |
| 90 | { 0x38, KEY_DVD }, |
| 91 | { 0x39, KEY_TV }, |
Ville Syrjala | 1971b9d | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 92 | { 0x3f, KEY_PROG1 }, /* AUX1-AUX4 and PC */ |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 93 | { 0x54, KEY_MENU }, |
| 94 | { 0x58, KEY_UP }, |
| 95 | { 0x59, KEY_DOWN }, |
| 96 | { 0x5a, KEY_LEFT }, |
| 97 | { 0x5b, KEY_RIGHT }, |
| 98 | { 0x5c, KEY_OK }, |
| 99 | { 0x78, KEY_A }, |
| 100 | { 0x79, KEY_B }, |
| 101 | { 0x7a, KEY_C }, |
| 102 | { 0x7b, KEY_D }, |
| 103 | { 0x7c, KEY_E }, |
| 104 | { 0x7d, KEY_F }, |
| 105 | { 0x82, KEY_ENTER }, |
| 106 | { 0x8e, KEY_VENDOR }, |
| 107 | { 0x96, KEY_COFFEE }, |
| 108 | { 0xa9, BTN_LEFT }, |
| 109 | { 0xaa, BTN_RIGHT }, |
| 110 | { 0xbe, KEY_QUESTION }, |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 111 | { 0xd0, KEY_EDIT }, |
Ville Syrjala | 1971b9d | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 112 | { 0xd5, KEY_FRONT }, |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 113 | { 0xf9, KEY_INFO }, |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | struct ati_remote2 { |
| 117 | struct input_dev *idev; |
| 118 | struct usb_device *udev; |
| 119 | |
| 120 | struct usb_interface *intf[2]; |
| 121 | struct usb_endpoint_descriptor *ep[2]; |
| 122 | struct urb *urb[2]; |
| 123 | void *buf[2]; |
| 124 | dma_addr_t buf_dma[2]; |
| 125 | |
| 126 | unsigned long jiffies; |
| 127 | int mode; |
| 128 | |
| 129 | char name[64]; |
| 130 | char phys[64]; |
Ville Syrjala | 1971b9d | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 131 | |
| 132 | /* Each mode (AUX1-AUX4 and PC) can have an independent keymap. */ |
| 133 | u16 keycode[ATI_REMOTE2_MODES][ARRAY_SIZE(ati_remote2_key_table)]; |
Ville Syrjala | d6505ab | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 134 | |
| 135 | unsigned int flags; |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 136 | }; |
| 137 | |
| 138 | static int ati_remote2_probe(struct usb_interface *interface, const struct usb_device_id *id); |
| 139 | static void ati_remote2_disconnect(struct usb_interface *interface); |
Ville Syrjala | d6505ab | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 140 | static int ati_remote2_suspend(struct usb_interface *interface, pm_message_t message); |
| 141 | static int ati_remote2_resume(struct usb_interface *interface); |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 142 | |
| 143 | static struct usb_driver ati_remote2_driver = { |
| 144 | .name = "ati_remote2", |
| 145 | .probe = ati_remote2_probe, |
| 146 | .disconnect = ati_remote2_disconnect, |
| 147 | .id_table = ati_remote2_id_table, |
Ville Syrjala | d6505ab | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 148 | .suspend = ati_remote2_suspend, |
| 149 | .resume = ati_remote2_resume, |
| 150 | .supports_autosuspend = 1, |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 151 | }; |
| 152 | |
Ville Syrjala | d6505ab | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 153 | static int ati_remote2_submit_urbs(struct ati_remote2 *ar2) |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 154 | { |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 155 | int r; |
| 156 | |
| 157 | r = usb_submit_urb(ar2->urb[0], GFP_KERNEL); |
| 158 | if (r) { |
| 159 | dev_err(&ar2->intf[0]->dev, |
Ville Syrjala | d6505ab | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 160 | "%s(): usb_submit_urb() = %d\n", __func__, r); |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 161 | return r; |
| 162 | } |
| 163 | r = usb_submit_urb(ar2->urb[1], GFP_KERNEL); |
| 164 | if (r) { |
| 165 | usb_kill_urb(ar2->urb[0]); |
| 166 | dev_err(&ar2->intf[1]->dev, |
Ville Syrjala | d6505ab | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 167 | "%s(): usb_submit_urb() = %d\n", __func__, r); |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 168 | return r; |
| 169 | } |
| 170 | |
| 171 | return 0; |
| 172 | } |
| 173 | |
Ville Syrjala | d6505ab | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 174 | static void ati_remote2_kill_urbs(struct ati_remote2 *ar2) |
| 175 | { |
| 176 | usb_kill_urb(ar2->urb[1]); |
| 177 | usb_kill_urb(ar2->urb[0]); |
| 178 | } |
| 179 | |
| 180 | static int ati_remote2_open(struct input_dev *idev) |
| 181 | { |
| 182 | struct ati_remote2 *ar2 = input_get_drvdata(idev); |
| 183 | int r; |
| 184 | |
| 185 | dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__); |
| 186 | |
| 187 | r = usb_autopm_get_interface(ar2->intf[0]); |
| 188 | if (r) { |
| 189 | dev_err(&ar2->intf[0]->dev, |
| 190 | "%s(): usb_autopm_get_interface() = %d\n", __func__, r); |
| 191 | goto fail1; |
| 192 | } |
| 193 | |
| 194 | mutex_lock(&ati_remote2_mutex); |
| 195 | |
| 196 | if (!(ar2->flags & ATI_REMOTE2_SUSPENDED)) { |
| 197 | r = ati_remote2_submit_urbs(ar2); |
| 198 | if (r) |
| 199 | goto fail2; |
| 200 | } |
| 201 | |
| 202 | ar2->flags |= ATI_REMOTE2_OPENED; |
| 203 | |
| 204 | mutex_unlock(&ati_remote2_mutex); |
| 205 | |
| 206 | usb_autopm_put_interface(ar2->intf[0]); |
| 207 | |
| 208 | return 0; |
| 209 | |
| 210 | fail2: |
| 211 | mutex_unlock(&ati_remote2_mutex); |
| 212 | usb_autopm_put_interface(ar2->intf[0]); |
| 213 | fail1: |
| 214 | return r; |
| 215 | } |
| 216 | |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 217 | static void ati_remote2_close(struct input_dev *idev) |
| 218 | { |
Dmitry Torokhov | 7791bda | 2007-04-12 01:34:39 -0400 | [diff] [blame] | 219 | struct ati_remote2 *ar2 = input_get_drvdata(idev); |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 220 | |
Ville Syrjala | d6505ab | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 221 | dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__); |
| 222 | |
| 223 | mutex_lock(&ati_remote2_mutex); |
| 224 | |
| 225 | if (!(ar2->flags & ATI_REMOTE2_SUSPENDED)) |
| 226 | ati_remote2_kill_urbs(ar2); |
| 227 | |
| 228 | ar2->flags &= ~ATI_REMOTE2_OPENED; |
| 229 | |
| 230 | mutex_unlock(&ati_remote2_mutex); |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 231 | } |
| 232 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 233 | static void ati_remote2_input_mouse(struct ati_remote2 *ar2) |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 234 | { |
| 235 | struct input_dev *idev = ar2->idev; |
| 236 | u8 *data = ar2->buf[0]; |
Peter Stokes | a1421d3 | 2007-04-12 01:33:10 -0400 | [diff] [blame] | 237 | int channel, mode; |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 238 | |
Peter Stokes | a1421d3 | 2007-04-12 01:33:10 -0400 | [diff] [blame] | 239 | channel = data[0] >> 4; |
| 240 | |
| 241 | if (!((1 << channel) & channel_mask)) |
| 242 | return; |
| 243 | |
| 244 | mode = data[0] & 0x0F; |
| 245 | |
Ville Syrjala | 1971b9d | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 246 | if (mode > ATI_REMOTE2_PC) { |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 247 | dev_err(&ar2->intf[0]->dev, |
| 248 | "Unknown mode byte (%02x %02x %02x %02x)\n", |
| 249 | data[3], data[2], data[1], data[0]); |
| 250 | return; |
| 251 | } |
| 252 | |
Peter Stokes | a1421d3 | 2007-04-12 01:33:10 -0400 | [diff] [blame] | 253 | if (!((1 << mode) & mode_mask)) |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 254 | return; |
| 255 | |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 256 | input_event(idev, EV_REL, REL_X, (s8) data[1]); |
| 257 | input_event(idev, EV_REL, REL_Y, (s8) data[2]); |
| 258 | input_sync(idev); |
| 259 | } |
| 260 | |
| 261 | static int ati_remote2_lookup(unsigned int hw_code) |
| 262 | { |
| 263 | int i; |
| 264 | |
Ville Syrjala | 1971b9d | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 265 | for (i = 0; i < ARRAY_SIZE(ati_remote2_key_table); i++) |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 266 | if (ati_remote2_key_table[i].hw_code == hw_code) |
| 267 | return i; |
| 268 | |
| 269 | return -1; |
| 270 | } |
| 271 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 272 | static void ati_remote2_input_key(struct ati_remote2 *ar2) |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 273 | { |
| 274 | struct input_dev *idev = ar2->idev; |
| 275 | u8 *data = ar2->buf[1]; |
Peter Stokes | a1421d3 | 2007-04-12 01:33:10 -0400 | [diff] [blame] | 276 | int channel, mode, hw_code, index; |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 277 | |
Peter Stokes | a1421d3 | 2007-04-12 01:33:10 -0400 | [diff] [blame] | 278 | channel = data[0] >> 4; |
| 279 | |
| 280 | if (!((1 << channel) & channel_mask)) |
| 281 | return; |
| 282 | |
| 283 | mode = data[0] & 0x0F; |
| 284 | |
Ville Syrjala | 1971b9d | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 285 | if (mode > ATI_REMOTE2_PC) { |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 286 | dev_err(&ar2->intf[1]->dev, |
| 287 | "Unknown mode byte (%02x %02x %02x %02x)\n", |
| 288 | data[3], data[2], data[1], data[0]); |
| 289 | return; |
| 290 | } |
| 291 | |
| 292 | hw_code = data[2]; |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 293 | if (hw_code == 0x3f) { |
| 294 | /* |
| 295 | * For some incomprehensible reason the mouse pad generates |
| 296 | * events which look identical to the events from the last |
| 297 | * pressed mode key. Naturally we don't want to generate key |
| 298 | * events for the mouse pad so we filter out any subsequent |
| 299 | * events from the same mode key. |
| 300 | */ |
Peter Stokes | a1421d3 | 2007-04-12 01:33:10 -0400 | [diff] [blame] | 301 | if (ar2->mode == mode) |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 302 | return; |
| 303 | |
| 304 | if (data[1] == 0) |
Peter Stokes | a1421d3 | 2007-04-12 01:33:10 -0400 | [diff] [blame] | 305 | ar2->mode = mode; |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 306 | } |
| 307 | |
Peter Stokes | a1421d3 | 2007-04-12 01:33:10 -0400 | [diff] [blame] | 308 | if (!((1 << mode) & mode_mask)) |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 309 | return; |
| 310 | |
| 311 | index = ati_remote2_lookup(hw_code); |
| 312 | if (index < 0) { |
| 313 | dev_err(&ar2->intf[1]->dev, |
| 314 | "Unknown code byte (%02x %02x %02x %02x)\n", |
| 315 | data[3], data[2], data[1], data[0]); |
| 316 | return; |
| 317 | } |
| 318 | |
| 319 | switch (data[1]) { |
| 320 | case 0: /* release */ |
| 321 | break; |
| 322 | case 1: /* press */ |
| 323 | ar2->jiffies = jiffies + msecs_to_jiffies(idev->rep[REP_DELAY]); |
| 324 | break; |
| 325 | case 2: /* repeat */ |
| 326 | |
| 327 | /* No repeat for mouse buttons. */ |
Ville Syrjala | 1971b9d | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 328 | if (ar2->keycode[mode][index] == BTN_LEFT || |
| 329 | ar2->keycode[mode][index] == BTN_RIGHT) |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 330 | return; |
| 331 | |
| 332 | if (!time_after_eq(jiffies, ar2->jiffies)) |
| 333 | return; |
| 334 | |
| 335 | ar2->jiffies = jiffies + msecs_to_jiffies(idev->rep[REP_PERIOD]); |
| 336 | break; |
| 337 | default: |
| 338 | dev_err(&ar2->intf[1]->dev, |
| 339 | "Unknown state byte (%02x %02x %02x %02x)\n", |
| 340 | data[3], data[2], data[1], data[0]); |
| 341 | return; |
| 342 | } |
| 343 | |
Ville Syrjala | 1971b9d | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 344 | input_event(idev, EV_KEY, ar2->keycode[mode][index], data[1]); |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 345 | input_sync(idev); |
| 346 | } |
| 347 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 348 | static void ati_remote2_complete_mouse(struct urb *urb) |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 349 | { |
| 350 | struct ati_remote2 *ar2 = urb->context; |
| 351 | int r; |
| 352 | |
| 353 | switch (urb->status) { |
| 354 | case 0: |
Ville Syrjala | d6505ab | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 355 | usb_mark_last_busy(ar2->udev); |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 356 | ati_remote2_input_mouse(ar2); |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 357 | break; |
| 358 | case -ENOENT: |
| 359 | case -EILSEQ: |
| 360 | case -ECONNRESET: |
| 361 | case -ESHUTDOWN: |
| 362 | dev_dbg(&ar2->intf[0]->dev, |
Harvey Harrison | ea3e6c5 | 2008-05-05 11:36:18 -0400 | [diff] [blame] | 363 | "%s(): urb status = %d\n", __func__, urb->status); |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 364 | return; |
| 365 | default: |
Ville Syrjala | d6505ab | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 366 | usb_mark_last_busy(ar2->udev); |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 367 | dev_err(&ar2->intf[0]->dev, |
Harvey Harrison | ea3e6c5 | 2008-05-05 11:36:18 -0400 | [diff] [blame] | 368 | "%s(): urb status = %d\n", __func__, urb->status); |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | r = usb_submit_urb(urb, GFP_ATOMIC); |
| 372 | if (r) |
| 373 | dev_err(&ar2->intf[0]->dev, |
Harvey Harrison | ea3e6c5 | 2008-05-05 11:36:18 -0400 | [diff] [blame] | 374 | "%s(): usb_submit_urb() = %d\n", __func__, r); |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 375 | } |
| 376 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 377 | static void ati_remote2_complete_key(struct urb *urb) |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 378 | { |
| 379 | struct ati_remote2 *ar2 = urb->context; |
| 380 | int r; |
| 381 | |
| 382 | switch (urb->status) { |
| 383 | case 0: |
Ville Syrjala | d6505ab | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 384 | usb_mark_last_busy(ar2->udev); |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 385 | ati_remote2_input_key(ar2); |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 386 | break; |
| 387 | case -ENOENT: |
| 388 | case -EILSEQ: |
| 389 | case -ECONNRESET: |
| 390 | case -ESHUTDOWN: |
| 391 | dev_dbg(&ar2->intf[1]->dev, |
Harvey Harrison | ea3e6c5 | 2008-05-05 11:36:18 -0400 | [diff] [blame] | 392 | "%s(): urb status = %d\n", __func__, urb->status); |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 393 | return; |
| 394 | default: |
Ville Syrjala | d6505ab | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 395 | usb_mark_last_busy(ar2->udev); |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 396 | dev_err(&ar2->intf[1]->dev, |
Harvey Harrison | ea3e6c5 | 2008-05-05 11:36:18 -0400 | [diff] [blame] | 397 | "%s(): urb status = %d\n", __func__, urb->status); |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | r = usb_submit_urb(urb, GFP_ATOMIC); |
| 401 | if (r) |
| 402 | dev_err(&ar2->intf[1]->dev, |
Harvey Harrison | ea3e6c5 | 2008-05-05 11:36:18 -0400 | [diff] [blame] | 403 | "%s(): usb_submit_urb() = %d\n", __func__, r); |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 404 | } |
| 405 | |
Ville Syrjala | 1971b9d | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 406 | static int ati_remote2_getkeycode(struct input_dev *idev, |
| 407 | int scancode, int *keycode) |
| 408 | { |
| 409 | struct ati_remote2 *ar2 = input_get_drvdata(idev); |
| 410 | int index, mode; |
| 411 | |
| 412 | mode = scancode >> 8; |
| 413 | if (mode > ATI_REMOTE2_PC || !((1 << mode) & mode_mask)) |
| 414 | return -EINVAL; |
| 415 | |
| 416 | index = ati_remote2_lookup(scancode & 0xFF); |
| 417 | if (index < 0) |
| 418 | return -EINVAL; |
| 419 | |
| 420 | *keycode = ar2->keycode[mode][index]; |
| 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | static int ati_remote2_setkeycode(struct input_dev *idev, int scancode, int keycode) |
| 425 | { |
| 426 | struct ati_remote2 *ar2 = input_get_drvdata(idev); |
| 427 | int index, mode, old_keycode; |
| 428 | |
| 429 | mode = scancode >> 8; |
| 430 | if (mode > ATI_REMOTE2_PC || !((1 << mode) & mode_mask)) |
| 431 | return -EINVAL; |
| 432 | |
| 433 | index = ati_remote2_lookup(scancode & 0xFF); |
| 434 | if (index < 0) |
| 435 | return -EINVAL; |
| 436 | |
| 437 | if (keycode < KEY_RESERVED || keycode > KEY_MAX) |
| 438 | return -EINVAL; |
| 439 | |
| 440 | old_keycode = ar2->keycode[mode][index]; |
| 441 | ar2->keycode[mode][index] = keycode; |
| 442 | set_bit(keycode, idev->keybit); |
| 443 | |
| 444 | for (mode = 0; mode < ATI_REMOTE2_MODES; mode++) { |
| 445 | for (index = 0; index < ARRAY_SIZE(ati_remote2_key_table); index++) { |
| 446 | if (ar2->keycode[mode][index] == old_keycode) |
| 447 | return 0; |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | clear_bit(old_keycode, idev->keybit); |
| 452 | |
| 453 | return 0; |
| 454 | } |
| 455 | |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 456 | static int ati_remote2_input_init(struct ati_remote2 *ar2) |
| 457 | { |
| 458 | struct input_dev *idev; |
Ville Syrjala | 1971b9d | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 459 | int index, mode, retval; |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 460 | |
| 461 | idev = input_allocate_device(); |
| 462 | if (!idev) |
| 463 | return -ENOMEM; |
| 464 | |
| 465 | ar2->idev = idev; |
Dmitry Torokhov | 7791bda | 2007-04-12 01:34:39 -0400 | [diff] [blame] | 466 | input_set_drvdata(idev, ar2); |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 467 | |
Jiri Slaby | 7b19ada | 2007-10-18 23:40:32 -0700 | [diff] [blame] | 468 | idev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) | BIT_MASK(EV_REL); |
| 469 | idev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) | |
| 470 | BIT_MASK(BTN_RIGHT); |
| 471 | idev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y); |
Ville Syrjala | 1971b9d | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 472 | |
| 473 | for (mode = 0; mode < ATI_REMOTE2_MODES; mode++) { |
| 474 | for (index = 0; index < ARRAY_SIZE(ati_remote2_key_table); index++) { |
| 475 | ar2->keycode[mode][index] = ati_remote2_key_table[index].keycode; |
| 476 | set_bit(ar2->keycode[mode][index], idev->keybit); |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | /* AUX1-AUX4 and PC generate the same scancode. */ |
| 481 | index = ati_remote2_lookup(0x3f); |
| 482 | ar2->keycode[ATI_REMOTE2_AUX1][index] = KEY_PROG1; |
| 483 | ar2->keycode[ATI_REMOTE2_AUX2][index] = KEY_PROG2; |
| 484 | ar2->keycode[ATI_REMOTE2_AUX3][index] = KEY_PROG3; |
| 485 | ar2->keycode[ATI_REMOTE2_AUX4][index] = KEY_PROG4; |
| 486 | ar2->keycode[ATI_REMOTE2_PC][index] = KEY_PC; |
| 487 | set_bit(KEY_PROG1, idev->keybit); |
| 488 | set_bit(KEY_PROG2, idev->keybit); |
| 489 | set_bit(KEY_PROG3, idev->keybit); |
| 490 | set_bit(KEY_PROG4, idev->keybit); |
| 491 | set_bit(KEY_PC, idev->keybit); |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 492 | |
| 493 | idev->rep[REP_DELAY] = 250; |
| 494 | idev->rep[REP_PERIOD] = 33; |
| 495 | |
| 496 | idev->open = ati_remote2_open; |
| 497 | idev->close = ati_remote2_close; |
| 498 | |
Ville Syrjala | 1971b9d | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 499 | idev->getkeycode = ati_remote2_getkeycode; |
| 500 | idev->setkeycode = ati_remote2_setkeycode; |
| 501 | |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 502 | idev->name = ar2->name; |
| 503 | idev->phys = ar2->phys; |
| 504 | |
| 505 | usb_to_input_id(ar2->udev, &idev->id); |
Dmitry Torokhov | c0f82d5 | 2007-04-12 01:35:03 -0400 | [diff] [blame] | 506 | idev->dev.parent = &ar2->udev->dev; |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 507 | |
Dmitry Torokhov | 5014186 | 2007-04-12 01:33:39 -0400 | [diff] [blame] | 508 | retval = input_register_device(idev); |
| 509 | if (retval) |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 510 | input_free_device(idev); |
| 511 | |
Dmitry Torokhov | 5014186 | 2007-04-12 01:33:39 -0400 | [diff] [blame] | 512 | return retval; |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | static int ati_remote2_urb_init(struct ati_remote2 *ar2) |
| 516 | { |
| 517 | struct usb_device *udev = ar2->udev; |
| 518 | int i, pipe, maxp; |
| 519 | |
| 520 | for (i = 0; i < 2; i++) { |
| 521 | ar2->buf[i] = usb_buffer_alloc(udev, 4, GFP_KERNEL, &ar2->buf_dma[i]); |
| 522 | if (!ar2->buf[i]) |
| 523 | return -ENOMEM; |
| 524 | |
| 525 | ar2->urb[i] = usb_alloc_urb(0, GFP_KERNEL); |
| 526 | if (!ar2->urb[i]) |
| 527 | return -ENOMEM; |
| 528 | |
| 529 | pipe = usb_rcvintpipe(udev, ar2->ep[i]->bEndpointAddress); |
| 530 | maxp = usb_maxpacket(udev, pipe, usb_pipeout(pipe)); |
| 531 | maxp = maxp > 4 ? 4 : maxp; |
| 532 | |
| 533 | usb_fill_int_urb(ar2->urb[i], udev, pipe, ar2->buf[i], maxp, |
| 534 | i ? ati_remote2_complete_key : ati_remote2_complete_mouse, |
| 535 | ar2, ar2->ep[i]->bInterval); |
| 536 | ar2->urb[i]->transfer_dma = ar2->buf_dma[i]; |
| 537 | ar2->urb[i]->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
| 538 | } |
| 539 | |
| 540 | return 0; |
| 541 | } |
| 542 | |
| 543 | static void ati_remote2_urb_cleanup(struct ati_remote2 *ar2) |
| 544 | { |
| 545 | int i; |
| 546 | |
| 547 | for (i = 0; i < 2; i++) { |
Mariusz Kozlowski | 2381526 | 2006-11-08 15:35:50 +0100 | [diff] [blame] | 548 | usb_free_urb(ar2->urb[i]); |
Dmitry Torokhov | e37a97d | 2007-05-03 00:57:29 -0400 | [diff] [blame] | 549 | usb_buffer_free(ar2->udev, 4, ar2->buf[i], ar2->buf_dma[i]); |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 550 | } |
| 551 | } |
| 552 | |
Peter Stokes | a1421d3 | 2007-04-12 01:33:10 -0400 | [diff] [blame] | 553 | static int ati_remote2_setup(struct ati_remote2 *ar2) |
| 554 | { |
| 555 | int r, i, channel; |
| 556 | |
| 557 | /* |
| 558 | * Configure receiver to only accept input from remote "channel" |
| 559 | * channel == 0 -> Accept input from any remote channel |
| 560 | * channel == 1 -> Only accept input from remote channel 1 |
| 561 | * channel == 2 -> Only accept input from remote channel 2 |
| 562 | * ... |
| 563 | * channel == 16 -> Only accept input from remote channel 16 |
| 564 | */ |
| 565 | |
| 566 | channel = 0; |
| 567 | for (i = 0; i < 16; i++) { |
| 568 | if ((1 << i) & channel_mask) { |
| 569 | if (!(~(1 << i) & 0xFFFF & channel_mask)) |
| 570 | channel = i + 1; |
| 571 | break; |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | r = usb_control_msg(ar2->udev, usb_sndctrlpipe(ar2->udev, 0), |
| 576 | 0x20, |
| 577 | USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE, |
| 578 | channel, 0x0, NULL, 0, USB_CTRL_SET_TIMEOUT); |
| 579 | if (r) { |
| 580 | 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] | 581 | __func__, r); |
Peter Stokes | a1421d3 | 2007-04-12 01:33:10 -0400 | [diff] [blame] | 582 | return r; |
| 583 | } |
| 584 | |
| 585 | return 0; |
| 586 | } |
| 587 | |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 588 | static int ati_remote2_probe(struct usb_interface *interface, const struct usb_device_id *id) |
| 589 | { |
| 590 | struct usb_device *udev = interface_to_usbdev(interface); |
| 591 | struct usb_host_interface *alt = interface->cur_altsetting; |
| 592 | struct ati_remote2 *ar2; |
| 593 | int r; |
| 594 | |
| 595 | if (alt->desc.bInterfaceNumber) |
| 596 | return -ENODEV; |
| 597 | |
| 598 | ar2 = kzalloc(sizeof (struct ati_remote2), GFP_KERNEL); |
| 599 | if (!ar2) |
| 600 | return -ENOMEM; |
| 601 | |
| 602 | ar2->udev = udev; |
| 603 | |
| 604 | ar2->intf[0] = interface; |
| 605 | ar2->ep[0] = &alt->endpoint[0].desc; |
| 606 | |
| 607 | ar2->intf[1] = usb_ifnum_to_if(udev, 1); |
| 608 | r = usb_driver_claim_interface(&ati_remote2_driver, ar2->intf[1], ar2); |
| 609 | if (r) |
| 610 | goto fail1; |
| 611 | alt = ar2->intf[1]->cur_altsetting; |
| 612 | ar2->ep[1] = &alt->endpoint[0].desc; |
| 613 | |
| 614 | r = ati_remote2_urb_init(ar2); |
| 615 | if (r) |
| 616 | goto fail2; |
| 617 | |
Peter Stokes | a1421d3 | 2007-04-12 01:33:10 -0400 | [diff] [blame] | 618 | r = ati_remote2_setup(ar2); |
| 619 | if (r) |
| 620 | goto fail2; |
| 621 | |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 622 | usb_make_path(udev, ar2->phys, sizeof(ar2->phys)); |
| 623 | strlcat(ar2->phys, "/input0", sizeof(ar2->phys)); |
| 624 | |
| 625 | strlcat(ar2->name, "ATI Remote Wonder II", sizeof(ar2->name)); |
| 626 | |
| 627 | r = ati_remote2_input_init(ar2); |
| 628 | if (r) |
| 629 | goto fail2; |
| 630 | |
| 631 | usb_set_intfdata(interface, ar2); |
| 632 | |
Ville Syrjala | d6505ab | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 633 | interface->needs_remote_wakeup = 1; |
| 634 | |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 635 | return 0; |
| 636 | |
| 637 | fail2: |
| 638 | ati_remote2_urb_cleanup(ar2); |
| 639 | |
| 640 | usb_driver_release_interface(&ati_remote2_driver, ar2->intf[1]); |
| 641 | fail1: |
| 642 | kfree(ar2); |
| 643 | |
| 644 | return r; |
| 645 | } |
| 646 | |
| 647 | static void ati_remote2_disconnect(struct usb_interface *interface) |
| 648 | { |
| 649 | struct ati_remote2 *ar2; |
| 650 | struct usb_host_interface *alt = interface->cur_altsetting; |
| 651 | |
| 652 | if (alt->desc.bInterfaceNumber) |
| 653 | return; |
| 654 | |
| 655 | ar2 = usb_get_intfdata(interface); |
| 656 | usb_set_intfdata(interface, NULL); |
| 657 | |
| 658 | input_unregister_device(ar2->idev); |
| 659 | |
| 660 | ati_remote2_urb_cleanup(ar2); |
| 661 | |
| 662 | usb_driver_release_interface(&ati_remote2_driver, ar2->intf[1]); |
| 663 | |
| 664 | kfree(ar2); |
| 665 | } |
| 666 | |
Ville Syrjala | d6505ab | 2008-07-03 10:45:37 -0400 | [diff] [blame] | 667 | static int ati_remote2_suspend(struct usb_interface *interface, |
| 668 | pm_message_t message) |
| 669 | { |
| 670 | struct ati_remote2 *ar2; |
| 671 | struct usb_host_interface *alt = interface->cur_altsetting; |
| 672 | |
| 673 | if (alt->desc.bInterfaceNumber) |
| 674 | return 0; |
| 675 | |
| 676 | ar2 = usb_get_intfdata(interface); |
| 677 | |
| 678 | dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__); |
| 679 | |
| 680 | mutex_lock(&ati_remote2_mutex); |
| 681 | |
| 682 | if (ar2->flags & ATI_REMOTE2_OPENED) |
| 683 | ati_remote2_kill_urbs(ar2); |
| 684 | |
| 685 | ar2->flags |= ATI_REMOTE2_SUSPENDED; |
| 686 | |
| 687 | mutex_unlock(&ati_remote2_mutex); |
| 688 | |
| 689 | return 0; |
| 690 | } |
| 691 | |
| 692 | static int ati_remote2_resume(struct usb_interface *interface) |
| 693 | { |
| 694 | struct ati_remote2 *ar2; |
| 695 | struct usb_host_interface *alt = interface->cur_altsetting; |
| 696 | int r = 0; |
| 697 | |
| 698 | if (alt->desc.bInterfaceNumber) |
| 699 | return 0; |
| 700 | |
| 701 | ar2 = usb_get_intfdata(interface); |
| 702 | |
| 703 | dev_dbg(&ar2->intf[0]->dev, "%s()\n", __func__); |
| 704 | |
| 705 | mutex_lock(&ati_remote2_mutex); |
| 706 | |
| 707 | if (ar2->flags & ATI_REMOTE2_OPENED) |
| 708 | r = ati_remote2_submit_urbs(ar2); |
| 709 | |
| 710 | if (!r) |
| 711 | ar2->flags &= ~ATI_REMOTE2_SUSPENDED; |
| 712 | |
| 713 | mutex_unlock(&ati_remote2_mutex); |
| 714 | |
| 715 | return r; |
| 716 | } |
| 717 | |
Ville Syrjälä | 735b0cb | 2005-12-10 20:30:54 +0200 | [diff] [blame] | 718 | static int __init ati_remote2_init(void) |
| 719 | { |
| 720 | int r; |
| 721 | |
| 722 | r = usb_register(&ati_remote2_driver); |
| 723 | if (r) |
| 724 | printk(KERN_ERR "ati_remote2: usb_register() = %d\n", r); |
| 725 | else |
| 726 | printk(KERN_INFO "ati_remote2: " DRIVER_DESC " " DRIVER_VERSION "\n"); |
| 727 | |
| 728 | return r; |
| 729 | } |
| 730 | |
| 731 | static void __exit ati_remote2_exit(void) |
| 732 | { |
| 733 | usb_deregister(&ati_remote2_driver); |
| 734 | } |
| 735 | |
| 736 | module_init(ati_remote2_init); |
| 737 | module_exit(ati_remote2_exit); |