Line 0
Link Here
|
|
|
1 |
/* lirc_xbox - Xbox remote support for LIRC |
2 |
* Version 0.3 [beta status] |
3 |
* |
4 |
* Copyright (C) 2003-2004 Chris Bainbridge <c.j.bainbridge@ed.ac.uk> |
5 |
* |
6 |
* This driver was derived from: |
7 |
* Paul Miller <pmiller9@users.sourceforge.net>'s 2004 |
8 |
* "lirc_atiusb" LIRC module |
9 |
* |
10 |
* Note from Chris: |
11 |
* This driver essentially *is* the atiusb driver. The only things |
12 |
* altered for the xbox are: |
13 |
* USB device IDs replaced, |
14 |
* Code to send IR codes has been removed |
15 |
* Process packet modified to send 1 byte codes to LIRC |
16 |
*/ |
17 |
|
18 |
/* |
19 |
* This program is free software; you can redistribute it and/or modify |
20 |
* it under the terms of the GNU General Public License as published by |
21 |
* the Free Software Foundation; either version 2 of the License, or |
22 |
* (at your option) any later version. |
23 |
* |
24 |
* This program is distributed in the hope that it will be useful, |
25 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
26 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
27 |
* GNU General Public License for more details. |
28 |
* |
29 |
* You should have received a copy of the GNU General Public License |
30 |
* along with this program; if not, write to the Free Software |
31 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
32 |
* |
33 |
*/ |
34 |
|
35 |
#include <linux/version.h> |
36 |
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 2, 4) |
37 |
#error "*******************************************************" |
38 |
#error "Sorry, this driver needs kernel version 2.2.4 or higher" |
39 |
#error "*******************************************************" |
40 |
#endif |
41 |
|
42 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) |
43 |
#define KERNEL26 1 |
44 |
#else |
45 |
#define KERNEL26 0 |
46 |
#endif |
47 |
|
48 |
#include <linux/config.h> |
49 |
|
50 |
#include <linux/kernel.h> |
51 |
#include <linux/errno.h> |
52 |
#include <linux/init.h> |
53 |
#include <linux/slab.h> |
54 |
#include <linux/module.h> |
55 |
#include <linux/kmod.h> |
56 |
#include <linux/smp_lock.h> |
57 |
#include <linux/completion.h> |
58 |
#include <asm/uaccess.h> |
59 |
#include <linux/usb.h> |
60 |
#include <linux/poll.h> |
61 |
#include <linux/wait.h> |
62 |
|
63 |
#if KERNEL26 |
64 |
#include <linux/lirc.h> |
65 |
#include "lirc_dev.h" |
66 |
#else |
67 |
#include "drivers/lirc.h" |
68 |
#include "drivers/lirc_dev/lirc_dev.h" |
69 |
#endif |
70 |
|
71 |
#define DRIVER_VERSION "0.3" |
72 |
#define DRIVER_AUTHOR "Chris Bainbridge <c.j.bainbridge@ed.ac.uk>" |
73 |
#define DRIVER_DESC "XBOX USB remote driver for LIRC" |
74 |
#define DRIVER_NAME "lirc_xbox" |
75 |
|
76 |
#define CODE_LENGTH 1 // length of code sent to lirc |
77 |
#define CODE_MIN_LENGTH 1 |
78 |
#define USB_BUFLEN 6 // code received from xbox ir |
79 |
|
80 |
#ifdef CONFIG_USB_DEBUG |
81 |
static int debug = 1; |
82 |
#else |
83 |
static int debug = 0; |
84 |
#endif |
85 |
#define dprintk if (debug) printk |
86 |
|
87 |
/* get hi and low bytes of a 16-bits int */ |
88 |
#define HI(a) ((unsigned char)((a) >> 8)) |
89 |
#define LO(a) ((unsigned char)((a) & 0xff)) |
90 |
|
91 |
/* lock irctl structure */ |
92 |
#define IRLOCK down_interruptible(&ir->lock) |
93 |
#define IRUNLOCK up(&ir->lock) |
94 |
|
95 |
/* general constants */ |
96 |
#define SUCCESS 0 |
97 |
#define SEND_FLAG_IN_PROGRESS 1 |
98 |
#define SEND_FLAG_COMPLETE 2 |
99 |
|
100 |
|
101 |
/* data structure for each usb remote */ |
102 |
struct irctl { |
103 |
|
104 |
/* usb */ |
105 |
struct usb_device *usbdev; |
106 |
struct urb *urb_in; |
107 |
int devnum; |
108 |
|
109 |
/* buffers and dma */ |
110 |
unsigned char *buf_in; |
111 |
unsigned char *buf_out; |
112 |
unsigned int len_in; |
113 |
#if KERNEL26 |
114 |
dma_addr_t dma_in; |
115 |
dma_addr_t dma_out; |
116 |
#endif |
117 |
|
118 |
/* lirc */ |
119 |
struct lirc_plugin *p; |
120 |
int connected; |
121 |
|
122 |
/* handle sending (init strings) */ |
123 |
int send_flags; |
124 |
wait_queue_head_t wait_out; |
125 |
|
126 |
struct semaphore lock; |
127 |
}; |
128 |
|
129 |
|
130 |
static int unregister_from_lirc(struct irctl *ir) |
131 |
{ |
132 |
struct lirc_plugin *p = ir->p; |
133 |
int devnum; |
134 |
int rtn; |
135 |
|
136 |
devnum = ir->devnum; |
137 |
dprintk(DRIVER_NAME "[%d]: unregister from lirc called\n", devnum); |
138 |
|
139 |
if ((rtn = lirc_unregister_plugin(p->minor)) > 0) { |
140 |
printk(DRIVER_NAME "[%d]: error in lirc_unregister minor: %d\n" |
141 |
"Trying again...\n", devnum, p->minor); |
142 |
if (rtn == -EBUSY) { |
143 |
printk(DRIVER_NAME |
144 |
"[%d]: device is opened, will unregister" |
145 |
" on close\n", devnum); |
146 |
return -EAGAIN; |
147 |
} |
148 |
set_current_state(TASK_INTERRUPTIBLE); |
149 |
schedule_timeout(HZ); |
150 |
|
151 |
if ((rtn = lirc_unregister_plugin(p->minor)) > 0) { |
152 |
printk(DRIVER_NAME "[%d]: lirc_unregister failed\n", |
153 |
devnum); |
154 |
} |
155 |
} |
156 |
|
157 |
if (rtn != SUCCESS) { |
158 |
printk(DRIVER_NAME "[%d]: didn't free resources\n", devnum); |
159 |
return -EAGAIN; |
160 |
} |
161 |
|
162 |
printk(DRIVER_NAME "[%d]: usb remote disconnected\n", devnum); |
163 |
|
164 |
lirc_buffer_free(p->rbuf); |
165 |
kfree(p->rbuf); |
166 |
kfree(p); |
167 |
kfree(ir); |
168 |
return SUCCESS; |
169 |
} |
170 |
|
171 |
static int set_use_inc(void *data) |
172 |
{ |
173 |
struct irctl *ir = data; |
174 |
|
175 |
if (!ir) { |
176 |
printk(DRIVER_NAME "[?]: set_use_inc called with no context\n"); |
177 |
return -EIO; |
178 |
} |
179 |
dprintk(DRIVER_NAME "[%d]: set use inc\n", ir->devnum); |
180 |
|
181 |
if (!ir->connected) { |
182 |
if (!ir->usbdev) |
183 |
return -ENOENT; |
184 |
ir->urb_in->dev = ir->usbdev; |
185 |
#if KERNEL26 |
186 |
if (usb_submit_urb(ir->urb_in, SLAB_ATOMIC)) { |
187 |
#else |
188 |
if (usb_submit_urb(ir->urb_in)) { |
189 |
#endif |
190 |
printk(DRIVER_NAME "[%d]: open result = -EIO error " |
191 |
"submitting urb\n", ir->devnum); |
192 |
return -EIO; |
193 |
} |
194 |
ir->connected = 1; |
195 |
} |
196 |
|
197 |
return SUCCESS; |
198 |
} |
199 |
|
200 |
static void set_use_dec(void *data) |
201 |
{ |
202 |
struct irctl *ir = data; |
203 |
|
204 |
if (!ir) { |
205 |
printk(DRIVER_NAME "[?]: set_use_dec called with no context\n"); |
206 |
return; |
207 |
} |
208 |
dprintk(DRIVER_NAME "[%d]: set use dec\n", ir->devnum); |
209 |
|
210 |
if (ir->connected) { |
211 |
IRLOCK; |
212 |
usb_unlink_urb(ir->urb_in); |
213 |
ir->connected = 0; |
214 |
IRUNLOCK; |
215 |
} |
216 |
} |
217 |
|
218 |
|
219 |
#if KERNEL26 |
220 |
static void usb_remote_recv(struct urb *urb, struct pt_regs *regs) |
221 |
#else |
222 |
static void usb_remote_recv(struct urb *urb) |
223 |
#endif |
224 |
{ |
225 |
struct irctl *ir; |
226 |
char buf[CODE_LENGTH]; |
227 |
int i, len; |
228 |
//dprintk("in usb_remote_recv\n"); |
229 |
if (!urb) |
230 |
return; |
231 |
|
232 |
if (!(ir = urb->context)) { |
233 |
usb_unlink_urb(urb); |
234 |
return; |
235 |
} |
236 |
|
237 |
dprintk(DRIVER_NAME "[%d]: data received (length %d)\n", |
238 |
ir->devnum, urb->actual_length); |
239 |
|
240 |
//dprintk("length %d\n",urb->actual_length); |
241 |
/*dprintk("ir->buf_in = %x\n",ir->buf_in); |
242 |
dprintk("urb->transfer_buffer = %x\n",urb->transfer_buffer);*/ |
243 |
//dprintk("urb->context->idata = %x\n",((struct usb_xpad*)(urb->context))->idata); |
244 |
unsigned char *idata=ir->buf_in; |
245 |
dprintk(DRIVER_NAME ": received bytes %02x %02x %02x %02x %02x %02x\n", |
246 |
idata[0], |
247 |
idata[1], |
248 |
idata[2], |
249 |
idata[3], |
250 |
idata[4], |
251 |
idata[5]); |
252 |
|
253 |
switch (urb->status) { |
254 |
|
255 |
/* success */ |
256 |
case SUCCESS: |
257 |
// check length of received packet |
258 |
len = urb->actual_length; |
259 |
if (len != USB_BUFLEN) |
260 |
{ |
261 |
dprintk(DRIVER_NAME ": We didn't get 6 bytes from xbox ir.. ?\n"); |
262 |
return; |
263 |
} |
264 |
// the third byte of xbox ir packet seems to contain key info |
265 |
// the last two bytes are.. some kind of clock? |
266 |
buf[0]=((char*)urb->transfer_buffer)[2]; |
267 |
|
268 |
dprintk(DRIVER_NAME ": write to lirc buffer and wakeup\n"); |
269 |
lirc_buffer_write_1(ir->p->rbuf, buf); |
270 |
wake_up(&ir->p->rbuf->wait_poll); |
271 |
break; |
272 |
|
273 |
/* unlink */ |
274 |
case -ECONNRESET: |
275 |
case -ENOENT: |
276 |
case -ESHUTDOWN: |
277 |
usb_unlink_urb(urb); |
278 |
return; |
279 |
} |
280 |
|
281 |
/* resubmit urb */ |
282 |
dprintk("resubmit urb\n"); |
283 |
#if KERNEL26 |
284 |
usb_submit_urb(urb, SLAB_ATOMIC); |
285 |
#else |
286 |
usb_submit_urb(urb); |
287 |
#endif |
288 |
} |
289 |
|
290 |
|
291 |
#if KERNEL26 |
292 |
static int usb_remote_probe(struct usb_interface *intf, |
293 |
const struct usb_device_id *id) |
294 |
{ |
295 |
struct usb_device *dev = NULL; |
296 |
struct usb_host_interface *idesc = NULL; |
297 |
#else |
298 |
static void *usb_remote_probe(struct usb_device *dev, unsigned int ifnum, |
299 |
const struct usb_device_id *id) |
300 |
{ |
301 |
struct usb_interface *intf; |
302 |
struct usb_interface_descriptor *idesc; |
303 |
#endif |
304 |
struct usb_endpoint_descriptor *ep_in; |
305 |
struct irctl *ir = NULL; |
306 |
struct lirc_plugin *plugin = NULL; |
307 |
struct lirc_buffer *rbuf = NULL; |
308 |
int devnum, pipe, maxp, len, buf_len, bytes_in_key; |
309 |
int minor = 0; |
310 |
char buf[63], name[128]=""; |
311 |
int mem_failure = 0; |
312 |
|
313 |
dprintk(DRIVER_NAME ": usb probe called\n"); |
314 |
|
315 |
#if KERNEL26 |
316 |
dev = interface_to_usbdev(intf); |
317 |
idesc = intf->cur_altsetting; |
318 |
ep_in = &idesc->endpoint[0].desc; |
319 |
if (((ep_in->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_IN) |
320 |
|| (ep_in->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) |
321 |
!= USB_ENDPOINT_XFER_INT) |
322 |
return -ENODEV; |
323 |
#else |
324 |
intf = &dev->actconfig->interface[ifnum]; |
325 |
idesc = &intf->altsetting[intf->act_altsetting]; |
326 |
if (idesc->bNumEndpoints != 2) |
327 |
return NULL; |
328 |
ep_in = idesc->endpoint + 0; |
329 |
ep_out = idesc->endpoint + 1; |
330 |
if (((ep_in->bEndpointAddress & USB_ENDPOINT_DIR_MASK) != USB_DIR_IN) |
331 |
|| (ep_in->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) |
332 |
!= USB_ENDPOINT_XFER_INT) |
333 |
return NULL; |
334 |
#endif |
335 |
devnum = dev->devnum; |
336 |
pipe = usb_rcvintpipe(dev, ep_in->bEndpointAddress); |
337 |
maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe)); |
338 |
|
339 |
bytes_in_key = CODE_LENGTH; |
340 |
len = (maxp > USB_BUFLEN) ? USB_BUFLEN : maxp; |
341 |
buf_len = len - (len % bytes_in_key); |
342 |
|
343 |
dprintk(DRIVER_NAME "[%d]: bytes_in_key=%d len=%d maxp=%d buf_len=%d\n", |
344 |
devnum, bytes_in_key, len, maxp, buf_len); |
345 |
|
346 |
|
347 |
/* allocate kernel memory */ |
348 |
mem_failure = 0; |
349 |
if (!(ir = kmalloc(sizeof(struct irctl), GFP_KERNEL))) { |
350 |
mem_failure = 1; |
351 |
} else { |
352 |
memset(ir, 0, sizeof(struct irctl)); |
353 |
|
354 |
if (!(plugin = kmalloc(sizeof(struct lirc_plugin), GFP_KERNEL))) { |
355 |
mem_failure = 2; |
356 |
} else if (!(rbuf = kmalloc(sizeof(struct lirc_buffer), GFP_KERNEL))) { |
357 |
mem_failure = 3; |
358 |
} else if (lirc_buffer_init(rbuf, bytes_in_key, USB_BUFLEN/bytes_in_key)) { |
359 |
mem_failure = 4; |
360 |
#if KERNEL26 |
361 |
} else if (!(ir->buf_in = usb_buffer_alloc(dev, buf_len, SLAB_ATOMIC, &ir->dma_in))) { |
362 |
mem_failure = 5; |
363 |
} else if (!(ir->urb_in = usb_alloc_urb(0, GFP_KERNEL))) { |
364 |
mem_failure = 7; |
365 |
} |
366 |
#else |
367 |
} else if (!(ir->buf_in = kmalloc(buf_len, GFP_KERNEL))) { |
368 |
mem_failure = 5; |
369 |
} else if (!(ir->urb_in = usb_alloc_urb(0))) { |
370 |
mem_failure = 7; |
371 |
} |
372 |
#endif |
373 |
else { |
374 |
memset(plugin, 0, sizeof(struct lirc_plugin)); |
375 |
|
376 |
strcpy(plugin->name, DRIVER_NAME " "); |
377 |
plugin->minor = -1; |
378 |
plugin->code_length = bytes_in_key*8; |
379 |
plugin->features = LIRC_CAN_REC_LIRCCODE; |
380 |
plugin->data = ir; |
381 |
plugin->rbuf = rbuf; |
382 |
plugin->set_use_inc = &set_use_inc; |
383 |
plugin->set_use_dec = &set_use_dec; |
384 |
|
385 |
init_MUTEX(&ir->lock); |
386 |
init_waitqueue_head(&ir->wait_out); |
387 |
|
388 |
if ((minor = lirc_register_plugin(plugin)) < 0) { |
389 |
mem_failure = 9; |
390 |
} |
391 |
} |
392 |
} |
393 |
|
394 |
/* free allocated memory incase of failure */ |
395 |
switch (mem_failure) { |
396 |
case 9: |
397 |
lirc_buffer_free(rbuf); |
398 |
case 7: |
399 |
usb_free_urb(ir->urb_in); |
400 |
#if KERNEL26 |
401 |
case 5: |
402 |
usb_buffer_free(dev, buf_len, ir->buf_in, ir->dma_in); |
403 |
#else |
404 |
case 5: |
405 |
kfree(ir->buf_in); |
406 |
#endif |
407 |
case 4: |
408 |
kfree(rbuf); |
409 |
case 3: |
410 |
kfree(plugin); |
411 |
case 2: |
412 |
kfree(ir); |
413 |
case 1: |
414 |
printk(DRIVER_NAME "[%d]: out of memory (code=%d)\n", |
415 |
devnum, mem_failure); |
416 |
#if KERNEL26 |
417 |
return -ENOMEM; |
418 |
#else |
419 |
return NULL; |
420 |
#endif |
421 |
} |
422 |
|
423 |
plugin->minor = minor; |
424 |
ir->p = plugin; |
425 |
ir->devnum = devnum; |
426 |
ir->usbdev = dev; |
427 |
ir->len_in = buf_len; |
428 |
ir->connected = 0; |
429 |
|
430 |
usb_fill_int_urb(ir->urb_in, dev, pipe, ir->buf_in, |
431 |
buf_len, usb_remote_recv, ir, ep_in->bInterval); |
432 |
|
433 |
if (dev->descriptor.iManufacturer |
434 |
&& usb_string(dev, dev->descriptor.iManufacturer, buf, 63) > 0) |
435 |
strncpy(name, buf, 128); |
436 |
if (dev->descriptor.iProduct |
437 |
&& usb_string(dev, dev->descriptor.iProduct, buf, 63) > 0) |
438 |
snprintf(name, 128, "%s %s", name, buf); |
439 |
strcpy(name,"Xbox IR"); |
440 |
printk(DRIVER_NAME "[%d]: %s on usb%d:%d\n", devnum, name, |
441 |
dev->bus->busnum, devnum); |
442 |
|
443 |
#if KERNEL26 |
444 |
usb_set_intfdata(intf, ir); |
445 |
return SUCCESS; |
446 |
#else |
447 |
return ir; |
448 |
#endif |
449 |
} |
450 |
|
451 |
|
452 |
#if KERNEL26 |
453 |
static void usb_remote_disconnect(struct usb_interface *intf) |
454 |
{ |
455 |
struct usb_device *dev = interface_to_usbdev(intf); |
456 |
struct irctl *ir = usb_get_intfdata(intf); |
457 |
usb_set_intfdata(intf, NULL); |
458 |
#else |
459 |
static void usb_remote_disconnect(struct usb_device *dev, void *ptr) |
460 |
{ |
461 |
struct irctl *ir = ptr; |
462 |
#endif |
463 |
|
464 |
if (!ir || !ir->p) |
465 |
return; |
466 |
|
467 |
ir->usbdev = NULL; |
468 |
wake_up_all(&ir->wait_out); |
469 |
|
470 |
IRLOCK; |
471 |
usb_unlink_urb(ir->urb_in); |
472 |
usb_free_urb(ir->urb_in); |
473 |
#if KERNEL26 |
474 |
usb_buffer_free(dev, ir->len_in, ir->buf_in, ir->dma_in); |
475 |
#else |
476 |
kfree(ir->buf_in); |
477 |
#endif |
478 |
IRUNLOCK; |
479 |
|
480 |
unregister_from_lirc(ir); |
481 |
} |
482 |
|
483 |
static struct usb_device_id usb_remote_id_table [] = { |
484 |
{ USB_DEVICE(0x040b, 0x6521) }, /* Gamester Xbox DVD Movie Playback Kit IR */ |
485 |
{ USB_DEVICE(0x045e, 0x0284) }, /* Microsoft Xbox DVD Movie Playback Kit IR */ |
486 |
|
487 |
{ } /* Terminating entry */ |
488 |
}; |
489 |
|
490 |
static struct usb_driver usb_remote_driver = { |
491 |
.owner = THIS_MODULE, |
492 |
.name = DRIVER_NAME, |
493 |
.probe = usb_remote_probe, |
494 |
.disconnect = usb_remote_disconnect, |
495 |
.id_table = usb_remote_id_table |
496 |
}; |
497 |
|
498 |
static int __init usb_remote_init(void) |
499 |
{ |
500 |
int i; |
501 |
|
502 |
printk("\n" DRIVER_NAME ": " DRIVER_DESC " v" DRIVER_VERSION "\n"); |
503 |
printk(DRIVER_NAME ": " DRIVER_AUTHOR "\n"); |
504 |
dprintk(DRIVER_NAME ": debug mode enabled\n"); |
505 |
|
506 |
request_module("lirc_dev"); |
507 |
|
508 |
if ((i = usb_register(&usb_remote_driver)) < 0) { |
509 |
printk(DRIVER_NAME ": usb register failed, result = %d\n", i); |
510 |
return -ENODEV; |
511 |
} |
512 |
|
513 |
return SUCCESS; |
514 |
} |
515 |
|
516 |
static void __exit usb_remote_exit(void) |
517 |
{ |
518 |
usb_deregister(&usb_remote_driver); |
519 |
} |
520 |
|
521 |
module_init(usb_remote_init); |
522 |
module_exit(usb_remote_exit); |
523 |
|
524 |
MODULE_AUTHOR (DRIVER_AUTHOR); |
525 |
MODULE_DESCRIPTION (DRIVER_DESC); |
526 |
MODULE_LICENSE ("GPL"); |
527 |
MODULE_DEVICE_TABLE (usb, usb_remote_id_table); |
528 |
|
529 |
MODULE_PARM(debug, "i"); |
530 |
MODULE_PARM_DESC(debug, "enable driver debug mode"); |
531 |
|
532 |
#if !KERNEL26 |
533 |
EXPORT_NO_SYMBOLS; |
534 |
#endif |
535 |
|