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