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