1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun // Driver for Xbox DVD Movie Playback Kit
3*4882a593Smuzhiyun // Copyright (c) 2018 by Benjamin Valentin <benpicco@googlemail.com>
4*4882a593Smuzhiyun
5*4882a593Smuzhiyun /*
6*4882a593Smuzhiyun * Xbox DVD Movie Playback Kit USB IR dongle support
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * The driver was derived from the ati_remote driver 2.2.1
9*4882a593Smuzhiyun * and used information from lirc_xbox.c
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * Copyright (c) 2011, 2012 Anssi Hannula <anssi.hannula@iki.fi>
12*4882a593Smuzhiyun * Copyright (c) 2004 Torrey Hoffman <thoffman@arnor.net>
13*4882a593Smuzhiyun * Copyright (c) 2002 Vladimir Dergachev
14*4882a593Smuzhiyun * Copyright (c) 2003-2004 Paul Miller <pmiller9@users.sourceforge.net>
15*4882a593Smuzhiyun */
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun #include <linux/slab.h>
18*4882a593Smuzhiyun #include <linux/module.h>
19*4882a593Smuzhiyun #include <linux/usb/input.h>
20*4882a593Smuzhiyun #include <media/rc-core.h>
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun /*
23*4882a593Smuzhiyun * Module and Version Information
24*4882a593Smuzhiyun */
25*4882a593Smuzhiyun #define DRIVER_VERSION "1.0.0"
26*4882a593Smuzhiyun #define DRIVER_AUTHOR "Benjamin Valentin <benpicco@googlemail.com>"
27*4882a593Smuzhiyun #define DRIVER_DESC "Xbox DVD USB Remote Control"
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun #define NAME_BUFSIZE 80 /* size of product name, path buffers */
30*4882a593Smuzhiyun #define DATA_BUFSIZE 8 /* size of URB data buffers */
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun /*
33*4882a593Smuzhiyun * USB vendor ids for XBOX DVD Dongles
34*4882a593Smuzhiyun */
35*4882a593Smuzhiyun #define VENDOR_GAMESTER 0x040b
36*4882a593Smuzhiyun #define VENDOR_MICROSOFT 0x045e
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun static const struct usb_device_id xbox_remote_table[] = {
39*4882a593Smuzhiyun /* Gamester Xbox DVD Movie Playback Kit IR */
40*4882a593Smuzhiyun {
41*4882a593Smuzhiyun USB_DEVICE(VENDOR_GAMESTER, 0x6521),
42*4882a593Smuzhiyun },
43*4882a593Smuzhiyun /* Microsoft Xbox DVD Movie Playback Kit IR */
44*4882a593Smuzhiyun {
45*4882a593Smuzhiyun USB_DEVICE(VENDOR_MICROSOFT, 0x0284),
46*4882a593Smuzhiyun },
47*4882a593Smuzhiyun {} /* Terminating entry */
48*4882a593Smuzhiyun };
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun MODULE_DEVICE_TABLE(usb, xbox_remote_table);
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun struct xbox_remote {
53*4882a593Smuzhiyun struct rc_dev *rdev;
54*4882a593Smuzhiyun struct usb_device *udev;
55*4882a593Smuzhiyun struct usb_interface *interface;
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun struct urb *irq_urb;
58*4882a593Smuzhiyun unsigned char inbuf[DATA_BUFSIZE] __aligned(sizeof(u16));
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun char rc_name[NAME_BUFSIZE];
61*4882a593Smuzhiyun char rc_phys[NAME_BUFSIZE];
62*4882a593Smuzhiyun };
63*4882a593Smuzhiyun
xbox_remote_rc_open(struct rc_dev * rdev)64*4882a593Smuzhiyun static int xbox_remote_rc_open(struct rc_dev *rdev)
65*4882a593Smuzhiyun {
66*4882a593Smuzhiyun struct xbox_remote *xbox_remote = rdev->priv;
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun /* On first open, submit the read urb which was set up previously. */
69*4882a593Smuzhiyun xbox_remote->irq_urb->dev = xbox_remote->udev;
70*4882a593Smuzhiyun if (usb_submit_urb(xbox_remote->irq_urb, GFP_KERNEL)) {
71*4882a593Smuzhiyun dev_err(&xbox_remote->interface->dev,
72*4882a593Smuzhiyun "%s: usb_submit_urb failed!\n", __func__);
73*4882a593Smuzhiyun return -EIO;
74*4882a593Smuzhiyun }
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun return 0;
77*4882a593Smuzhiyun }
78*4882a593Smuzhiyun
xbox_remote_rc_close(struct rc_dev * rdev)79*4882a593Smuzhiyun static void xbox_remote_rc_close(struct rc_dev *rdev)
80*4882a593Smuzhiyun {
81*4882a593Smuzhiyun struct xbox_remote *xbox_remote = rdev->priv;
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun usb_kill_urb(xbox_remote->irq_urb);
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun /*
87*4882a593Smuzhiyun * xbox_remote_report_input
88*4882a593Smuzhiyun */
xbox_remote_input_report(struct urb * urb)89*4882a593Smuzhiyun static void xbox_remote_input_report(struct urb *urb)
90*4882a593Smuzhiyun {
91*4882a593Smuzhiyun struct xbox_remote *xbox_remote = urb->context;
92*4882a593Smuzhiyun unsigned char *data = xbox_remote->inbuf;
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun /*
95*4882a593Smuzhiyun * data[0] = 0x00
96*4882a593Smuzhiyun * data[1] = length - always 0x06
97*4882a593Smuzhiyun * data[2] = the key code
98*4882a593Smuzhiyun * data[3] = high part of key code
99*4882a593Smuzhiyun * data[4] = last_press_ms (low)
100*4882a593Smuzhiyun * data[5] = last_press_ms (high)
101*4882a593Smuzhiyun */
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun /* Deal with strange looking inputs */
104*4882a593Smuzhiyun if (urb->actual_length != 6 || urb->actual_length != data[1]) {
105*4882a593Smuzhiyun dev_warn(&urb->dev->dev, "Weird data, len=%d: %*ph\n",
106*4882a593Smuzhiyun urb->actual_length, urb->actual_length, data);
107*4882a593Smuzhiyun return;
108*4882a593Smuzhiyun }
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun rc_keydown(xbox_remote->rdev, RC_PROTO_XBOX_DVD,
111*4882a593Smuzhiyun le16_to_cpup((__le16 *)(data + 2)), 0);
112*4882a593Smuzhiyun }
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun /*
115*4882a593Smuzhiyun * xbox_remote_irq_in
116*4882a593Smuzhiyun */
xbox_remote_irq_in(struct urb * urb)117*4882a593Smuzhiyun static void xbox_remote_irq_in(struct urb *urb)
118*4882a593Smuzhiyun {
119*4882a593Smuzhiyun struct xbox_remote *xbox_remote = urb->context;
120*4882a593Smuzhiyun int retval;
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun switch (urb->status) {
123*4882a593Smuzhiyun case 0: /* success */
124*4882a593Smuzhiyun xbox_remote_input_report(urb);
125*4882a593Smuzhiyun break;
126*4882a593Smuzhiyun case -ECONNRESET: /* unlink */
127*4882a593Smuzhiyun case -ENOENT:
128*4882a593Smuzhiyun case -ESHUTDOWN:
129*4882a593Smuzhiyun dev_dbg(&xbox_remote->interface->dev,
130*4882a593Smuzhiyun "%s: urb error status, unlink?\n",
131*4882a593Smuzhiyun __func__);
132*4882a593Smuzhiyun return;
133*4882a593Smuzhiyun default: /* error */
134*4882a593Smuzhiyun dev_dbg(&xbox_remote->interface->dev,
135*4882a593Smuzhiyun "%s: Nonzero urb status %d\n",
136*4882a593Smuzhiyun __func__, urb->status);
137*4882a593Smuzhiyun }
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun retval = usb_submit_urb(urb, GFP_ATOMIC);
140*4882a593Smuzhiyun if (retval)
141*4882a593Smuzhiyun dev_err(&xbox_remote->interface->dev,
142*4882a593Smuzhiyun "%s: usb_submit_urb()=%d\n",
143*4882a593Smuzhiyun __func__, retval);
144*4882a593Smuzhiyun }
145*4882a593Smuzhiyun
xbox_remote_rc_init(struct xbox_remote * xbox_remote)146*4882a593Smuzhiyun static void xbox_remote_rc_init(struct xbox_remote *xbox_remote)
147*4882a593Smuzhiyun {
148*4882a593Smuzhiyun struct rc_dev *rdev = xbox_remote->rdev;
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun rdev->priv = xbox_remote;
151*4882a593Smuzhiyun rdev->allowed_protocols = RC_PROTO_BIT_XBOX_DVD;
152*4882a593Smuzhiyun rdev->driver_name = "xbox_remote";
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun rdev->open = xbox_remote_rc_open;
155*4882a593Smuzhiyun rdev->close = xbox_remote_rc_close;
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun rdev->device_name = xbox_remote->rc_name;
158*4882a593Smuzhiyun rdev->input_phys = xbox_remote->rc_phys;
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun rdev->timeout = MS_TO_US(10);
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun usb_to_input_id(xbox_remote->udev, &rdev->input_id);
163*4882a593Smuzhiyun rdev->dev.parent = &xbox_remote->interface->dev;
164*4882a593Smuzhiyun }
165*4882a593Smuzhiyun
xbox_remote_initialize(struct xbox_remote * xbox_remote,struct usb_endpoint_descriptor * endpoint_in)166*4882a593Smuzhiyun static int xbox_remote_initialize(struct xbox_remote *xbox_remote,
167*4882a593Smuzhiyun struct usb_endpoint_descriptor *endpoint_in)
168*4882a593Smuzhiyun {
169*4882a593Smuzhiyun struct usb_device *udev = xbox_remote->udev;
170*4882a593Smuzhiyun int pipe, maxp;
171*4882a593Smuzhiyun
172*4882a593Smuzhiyun /* Set up irq_urb */
173*4882a593Smuzhiyun pipe = usb_rcvintpipe(udev, endpoint_in->bEndpointAddress);
174*4882a593Smuzhiyun maxp = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
175*4882a593Smuzhiyun maxp = (maxp > DATA_BUFSIZE) ? DATA_BUFSIZE : maxp;
176*4882a593Smuzhiyun
177*4882a593Smuzhiyun usb_fill_int_urb(xbox_remote->irq_urb, udev, pipe, xbox_remote->inbuf,
178*4882a593Smuzhiyun maxp, xbox_remote_irq_in, xbox_remote,
179*4882a593Smuzhiyun endpoint_in->bInterval);
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun return 0;
182*4882a593Smuzhiyun }
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun /*
185*4882a593Smuzhiyun * xbox_remote_probe
186*4882a593Smuzhiyun */
xbox_remote_probe(struct usb_interface * interface,const struct usb_device_id * id)187*4882a593Smuzhiyun static int xbox_remote_probe(struct usb_interface *interface,
188*4882a593Smuzhiyun const struct usb_device_id *id)
189*4882a593Smuzhiyun {
190*4882a593Smuzhiyun struct usb_device *udev = interface_to_usbdev(interface);
191*4882a593Smuzhiyun struct usb_host_interface *iface_host = interface->cur_altsetting;
192*4882a593Smuzhiyun struct usb_endpoint_descriptor *endpoint_in;
193*4882a593Smuzhiyun struct xbox_remote *xbox_remote;
194*4882a593Smuzhiyun struct rc_dev *rc_dev;
195*4882a593Smuzhiyun int err = -ENOMEM;
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun // why is there also a device with no endpoints?
198*4882a593Smuzhiyun if (iface_host->desc.bNumEndpoints == 0)
199*4882a593Smuzhiyun return -ENODEV;
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun if (iface_host->desc.bNumEndpoints != 1) {
202*4882a593Smuzhiyun pr_err("%s: Unexpected desc.bNumEndpoints: %d\n",
203*4882a593Smuzhiyun __func__, iface_host->desc.bNumEndpoints);
204*4882a593Smuzhiyun return -ENODEV;
205*4882a593Smuzhiyun }
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun endpoint_in = &iface_host->endpoint[0].desc;
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun if (!usb_endpoint_is_int_in(endpoint_in)) {
210*4882a593Smuzhiyun pr_err("%s: Unexpected endpoint_in\n", __func__);
211*4882a593Smuzhiyun return -ENODEV;
212*4882a593Smuzhiyun }
213*4882a593Smuzhiyun if (le16_to_cpu(endpoint_in->wMaxPacketSize) == 0) {
214*4882a593Smuzhiyun pr_err("%s: endpoint_in message size==0?\n", __func__);
215*4882a593Smuzhiyun return -ENODEV;
216*4882a593Smuzhiyun }
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun xbox_remote = kzalloc(sizeof(*xbox_remote), GFP_KERNEL);
219*4882a593Smuzhiyun rc_dev = rc_allocate_device(RC_DRIVER_SCANCODE);
220*4882a593Smuzhiyun if (!xbox_remote || !rc_dev)
221*4882a593Smuzhiyun goto exit_free_dev_rdev;
222*4882a593Smuzhiyun
223*4882a593Smuzhiyun /* Allocate URB buffer */
224*4882a593Smuzhiyun xbox_remote->irq_urb = usb_alloc_urb(0, GFP_KERNEL);
225*4882a593Smuzhiyun if (!xbox_remote->irq_urb)
226*4882a593Smuzhiyun goto exit_free_buffers;
227*4882a593Smuzhiyun
228*4882a593Smuzhiyun xbox_remote->udev = udev;
229*4882a593Smuzhiyun xbox_remote->rdev = rc_dev;
230*4882a593Smuzhiyun xbox_remote->interface = interface;
231*4882a593Smuzhiyun
232*4882a593Smuzhiyun usb_make_path(udev, xbox_remote->rc_phys, sizeof(xbox_remote->rc_phys));
233*4882a593Smuzhiyun
234*4882a593Smuzhiyun strlcat(xbox_remote->rc_phys, "/input0", sizeof(xbox_remote->rc_phys));
235*4882a593Smuzhiyun
236*4882a593Smuzhiyun snprintf(xbox_remote->rc_name, sizeof(xbox_remote->rc_name), "%s%s%s",
237*4882a593Smuzhiyun udev->manufacturer ?: "",
238*4882a593Smuzhiyun udev->manufacturer && udev->product ? " " : "",
239*4882a593Smuzhiyun udev->product ?: "");
240*4882a593Smuzhiyun
241*4882a593Smuzhiyun if (!strlen(xbox_remote->rc_name))
242*4882a593Smuzhiyun snprintf(xbox_remote->rc_name, sizeof(xbox_remote->rc_name),
243*4882a593Smuzhiyun DRIVER_DESC "(%04x,%04x)",
244*4882a593Smuzhiyun le16_to_cpu(xbox_remote->udev->descriptor.idVendor),
245*4882a593Smuzhiyun le16_to_cpu(xbox_remote->udev->descriptor.idProduct));
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun rc_dev->map_name = RC_MAP_XBOX_DVD; /* default map */
248*4882a593Smuzhiyun
249*4882a593Smuzhiyun xbox_remote_rc_init(xbox_remote);
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun /* Device Hardware Initialization */
252*4882a593Smuzhiyun err = xbox_remote_initialize(xbox_remote, endpoint_in);
253*4882a593Smuzhiyun if (err)
254*4882a593Smuzhiyun goto exit_kill_urbs;
255*4882a593Smuzhiyun
256*4882a593Smuzhiyun /* Set up and register rc device */
257*4882a593Smuzhiyun err = rc_register_device(xbox_remote->rdev);
258*4882a593Smuzhiyun if (err)
259*4882a593Smuzhiyun goto exit_kill_urbs;
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun usb_set_intfdata(interface, xbox_remote);
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun return 0;
264*4882a593Smuzhiyun
265*4882a593Smuzhiyun exit_kill_urbs:
266*4882a593Smuzhiyun usb_kill_urb(xbox_remote->irq_urb);
267*4882a593Smuzhiyun exit_free_buffers:
268*4882a593Smuzhiyun usb_free_urb(xbox_remote->irq_urb);
269*4882a593Smuzhiyun exit_free_dev_rdev:
270*4882a593Smuzhiyun rc_free_device(rc_dev);
271*4882a593Smuzhiyun kfree(xbox_remote);
272*4882a593Smuzhiyun
273*4882a593Smuzhiyun return err;
274*4882a593Smuzhiyun }
275*4882a593Smuzhiyun
276*4882a593Smuzhiyun /*
277*4882a593Smuzhiyun * xbox_remote_disconnect
278*4882a593Smuzhiyun */
xbox_remote_disconnect(struct usb_interface * interface)279*4882a593Smuzhiyun static void xbox_remote_disconnect(struct usb_interface *interface)
280*4882a593Smuzhiyun {
281*4882a593Smuzhiyun struct xbox_remote *xbox_remote;
282*4882a593Smuzhiyun
283*4882a593Smuzhiyun xbox_remote = usb_get_intfdata(interface);
284*4882a593Smuzhiyun usb_set_intfdata(interface, NULL);
285*4882a593Smuzhiyun if (!xbox_remote) {
286*4882a593Smuzhiyun dev_warn(&interface->dev, "%s - null device?\n", __func__);
287*4882a593Smuzhiyun return;
288*4882a593Smuzhiyun }
289*4882a593Smuzhiyun
290*4882a593Smuzhiyun usb_kill_urb(xbox_remote->irq_urb);
291*4882a593Smuzhiyun rc_unregister_device(xbox_remote->rdev);
292*4882a593Smuzhiyun usb_free_urb(xbox_remote->irq_urb);
293*4882a593Smuzhiyun kfree(xbox_remote);
294*4882a593Smuzhiyun }
295*4882a593Smuzhiyun
296*4882a593Smuzhiyun /* usb specific object to register with the usb subsystem */
297*4882a593Smuzhiyun static struct usb_driver xbox_remote_driver = {
298*4882a593Smuzhiyun .name = "xbox_remote",
299*4882a593Smuzhiyun .probe = xbox_remote_probe,
300*4882a593Smuzhiyun .disconnect = xbox_remote_disconnect,
301*4882a593Smuzhiyun .id_table = xbox_remote_table,
302*4882a593Smuzhiyun };
303*4882a593Smuzhiyun
304*4882a593Smuzhiyun module_usb_driver(xbox_remote_driver);
305*4882a593Smuzhiyun
306*4882a593Smuzhiyun MODULE_AUTHOR(DRIVER_AUTHOR);
307*4882a593Smuzhiyun MODULE_DESCRIPTION(DRIVER_DESC);
308*4882a593Smuzhiyun MODULE_LICENSE("GPL");
309