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