Added lirc.
[irreco.git] / lirc-0.8.4a / drivers / lirc_mceusb / lirc_mceusb.c
blob266f4e9cb032b8488755857882af2005598d1c64
1 /*
2 * USB Microsoft IR Transceiver driver - 0.2
4 * Copyright (c) 2003-2004 Dan Conti (dconti@acm.wwu.edu)
6 * This driver is based on the USB skeleton driver packaged with the
7 * kernel, and the notice from that package has been retained below.
9 * The Microsoft IR Transceiver is a neat little IR receiver with two
10 * emitters on it designed for Windows Media Center. This driver might
11 * work for all media center remotes, but I have only tested it with
12 * the philips model. The first revision of this driver only supports
13 * the receive function - the transmit function will be much more
14 * tricky due to the nature of the hardware. Microsoft chose to build
15 * this device inexpensively, therefore making it extra dumb.
16 * There is no interrupt endpoint on this device; all usb traffic
17 * happens over two bulk endpoints. As a result of this, poll() for
18 * this device is an actual hardware poll (instead of a receive queue
19 * check) and is rather expensive.
21 * All trademarks property of their respective owners. This driver was
22 * originally based on the USB skeleton driver, although significant
23 * portions of that code have been removed as the driver has evolved.
25 * 2003_11_11 - Restructured to minimalize code interpretation in the
26 * driver. The normal use case will be with lirc.
28 * 2004_01_01 - Removed all code interpretation. Generate mode2 data
29 * for passing off to lirc. Cleanup
31 * 2004_01_04 - Removed devfs handle. Put in a temporary workaround
32 * for a known issue where repeats generate two
33 * sequential spaces (last_was_repeat_gap)
35 * 2004_02_17 - Changed top level api to no longer use fops, and
36 * instead use new interface for polling via
37 * lirc_thread. Restructure data read/mode2 generation to
38 * a single pass, reducing number of buffers. Rev to .2
40 * 2004_02_27 - Last of fixups to plugin->add_to_buf API. Properly
41 * handle broken fragments from the receiver. Up the
42 * sample rate and remove any pacing from
43 * fetch_more_data. Fixes all known issues.
45 * TODO
46 * - Fix up minor number, registration of major/minor with usb subsystem
50 * USB Skeleton driver - 1.1
52 * Copyright (C) 2001-2003 Greg Kroah-Hartman (greg@kroah.com)
54 * This program is free software; you can redistribute it and/or
55 * modify it under the terms of the GNU General Public License as
56 * published by the Free Software Foundation, version 2.
59 * This driver is to be used as a skeleton driver to be able to create a
60 * USB driver quickly. The design of it is based on the usb-serial and
61 * dc2xx drivers.
63 * Thanks to Oliver Neukum, David Brownell, and Alan Stern for their help
64 * in debugging this driver.
67 * History:
69 * 2003-05-06 - 1.1 - changes due to usb core changes with usb_register_dev()
70 * 2003-02-25 - 1.0 - fix races involving urb->status, unlink_urb(), and
71 * disconnect. Fix transfer amount in read(). Use
72 * macros instead of magic numbers in probe(). Change
73 * size variables to size_t. Show how to eliminate
74 * DMA bounce buffer.
75 * 2002_12_12 - 0.9 - compile fixes and got rid of fixed minor array.
76 * 2002_09_26 - 0.8 - changes due to USB core conversion to struct device
77 * driver.
78 * 2002_02_12 - 0.7 - zero out dev in probe function for devices that do
79 * not have both a bulk in and bulk out endpoint.
80 * Thanks to Holger Waechtler for the fix.
81 * 2001_11_05 - 0.6 - fix minor locking problem in skel_disconnect.
82 * Thanks to Pete Zaitcev for the fix.
83 * 2001_09_04 - 0.5 - fix devfs bug in skel_disconnect. Thanks to wim delvaux
84 * 2001_08_21 - 0.4 - more small bug fixes.
85 * 2001_05_29 - 0.3 - more bug fixes based on review from linux-usb-devel
86 * 2001_05_24 - 0.2 - bug fixes based on review from linux-usb-devel people
87 * 2001_05_01 - 0.1 - first version
91 #include <linux/autoconf.h>
92 #include <linux/kernel.h>
93 #include <linux/errno.h>
94 #include <linux/init.h>
95 #include <linux/slab.h>
96 #include <linux/module.h>
97 #include <linux/smp_lock.h>
98 #include <linux/usb.h>
99 #ifdef KERNEL_2_5
100 #include <linux/completion.h>
101 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19)
102 #include <asm/uaccess.h>
103 #else
104 #include <linux/uaccess.h>
105 #endif
106 #else
107 #include <linux/spinlock.h>
108 #include <linux/list.h>
109 #include <linux/fcntl.h>
110 #include <linux/poll.h>
111 #include <linux/sched.h>
112 #include <linux/signal.h>
113 #endif
115 #ifdef CONFIG_USB_DEBUG
116 static int debug = 1;
117 #else
118 static int debug;
119 #endif
121 #include "drivers/kcompat.h"
122 #include "drivers/lirc.h"
123 #include "drivers/lirc_dev/lirc_dev.h"
125 /* Use our own dbg macro */
126 #define dprintk(fmt, args...) \
127 do { \
128 if (debug) \
129 printk(KERN_DEBUG __FILE__ ": " \
130 fmt "\n", ## args); \
131 } while (0)
133 /* Version Information */
134 #define DRIVER_VERSION "v0.2"
135 #define DRIVER_AUTHOR "Dan Conti, dconti@acm.wwu.edu"
136 #define DRIVER_DESC "USB Microsoft IR Transceiver Driver"
137 #define DRIVER_NAME "lirc_mceusb"
139 /* Define these values to match your device */
140 #define USB_MCEUSB_VENDOR_ID 0x045e
141 #define USB_MCEUSB_PRODUCT_ID 0x006d
143 /* table of devices that work with this driver */
144 static struct usb_device_id mceusb_table[] = {
145 /* USB Microsoft IR Transceiver */
146 { USB_DEVICE(USB_MCEUSB_VENDOR_ID, USB_MCEUSB_PRODUCT_ID) },
147 /* Terminating entry */
151 /* we can have up to this number of device plugged in at once */
152 #define MAX_DEVICES 16
154 /* Structure to hold all of our device specific stuff */
155 struct usb_skel {
156 struct usb_device *udev; /* save off the usb device pointer */
157 struct usb_interface *interface; /* the interface for this device */
158 unsigned char minor; /* the starting minor number for this device */
159 unsigned char num_ports; /* the number of ports this device has */
160 char num_interrupt_in; /* number of interrupt in endpoints */
161 char num_bulk_in; /* number of bulk in endpoints */
162 char num_bulk_out; /* number of bulk out endpoints */
164 unsigned char *bulk_in_buffer; /* the buffer to receive data */
165 int bulk_in_size; /* the size of the receive buffer */
166 __u8 bulk_in_endpointAddr; /* the address of bulk in endpoint */
168 unsigned char *bulk_out_buffer; /* the buffer to send data */
169 int bulk_out_size; /* the size of the send buffer */
170 struct urb *write_urb; /* the urb used to send data */
171 __u8 bulk_out_endpointAddr; /* the address of bulk out endpoint */
173 atomic_t write_busy; /* true iff write urb is busy */
174 struct completion write_finished; /* wait for the write to finish */
176 wait_queue_head_t wait_q; /* for timeouts */
177 int open_count; /* number of times this port has been opened */
178 struct semaphore sem; /* locks this structure */
180 int present; /* if the device is not disconnected */
182 struct lirc_plugin *plugin;
184 lirc_t lircdata[256]; /* place to store data until lirc processes it */
185 int lircidx; /* current index */
186 int lirccnt; /* remaining values */
188 int usb_valid_bytes_in_bulk_buffer; /* leftover data from prior read */
189 int mce_bytes_left_in_packet; /* for packets split across reads */
191 /* Value to hold the last received space; 0 if last value
192 * received was a pulse */
193 int last_space;
195 #ifdef KERNEL_2_5
196 dma_addr_t dma_in;
197 dma_addr_t dma_out;
198 #endif
201 #define MCE_TIME_UNIT 50
203 /* driver api */
204 #ifdef KERNEL_2_5
205 static int mceusb_probe(struct usb_interface *interface,
206 const struct usb_device_id *id);
207 static void mceusb_disconnect(struct usb_interface *interface);
208 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19)
209 static void mceusb_write_bulk_callback(struct urb *urb, struct pt_regs *regs);
210 #else
211 static void mceusb_write_bulk_callback(struct urb *urb);
212 #endif
213 #else
214 static void *mceusb_probe(struct usb_device *dev, unsigned int ifnum,
215 const struct usb_device_id *id);
216 static void mceusb_disconnect(struct usb_device *dev, void *ptr);
217 static void mceusb_write_bulk_callback(struct urb *urb);
218 #endif
220 /* read data from the usb bus; convert to mode2 */
221 static int msir_fetch_more_data(struct usb_skel *dev, int dont_block);
223 /* helper functions */
224 static void msir_cleanup(struct usb_skel *dev);
225 static void set_use_dec(void *data);
226 static int set_use_inc(void *data);
228 /* array of pointers to our devices that are currently connected */
229 static struct usb_skel *minor_table[MAX_DEVICES];
231 /* lock to protect the minor_table structure */
232 static DECLARE_MUTEX(minor_table_mutex);
233 static void mceusb_setup(struct usb_device *udev);
235 /* usb specific object needed to register this driver with the usb subsystem */
236 static struct usb_driver mceusb_driver = {
237 LIRC_THIS_MODULE(.owner = THIS_MODULE)
238 .name = DRIVER_NAME,
239 .probe = mceusb_probe,
240 .disconnect = mceusb_disconnect,
241 .id_table = mceusb_table,
246 * usb_mceusb_debug_data
248 static inline void usb_mceusb_debug_data(const char *function, int size,
249 const unsigned char *data)
251 int i;
252 if (!debug)
253 return;
255 printk(KERN_DEBUG __FILE__": %s - length = %d, data = ",
256 function, size);
257 for (i = 0; i < size; ++i)
258 printk(KERN_DEBUG "%.2x ", data[i]);
259 printk(KERN_DEBUG "\n");
263 *mceusb_delete
265 static inline void mceusb_delete(struct usb_skel *dev)
267 dprintk("%s", __func__);
268 minor_table[dev->minor] = NULL;
269 #ifdef KERNEL_2_5
270 usb_buffer_free(dev->udev, dev->bulk_in_size,
271 dev->bulk_in_buffer, dev->dma_in);
272 usb_buffer_free(dev->udev, dev->bulk_out_size,
273 dev->bulk_out_buffer, dev->dma_out);
274 #else
275 if (dev->bulk_in_buffer != NULL)
276 kfree(dev->bulk_in_buffer);
277 if (dev->bulk_out_buffer != NULL)
278 kfree(dev->bulk_out_buffer);
279 #endif
280 if (dev->write_urb != NULL)
281 usb_free_urb(dev->write_urb);
282 kfree(dev);
285 static void mceusb_setup(struct usb_device *udev)
287 char data[8];
288 int res;
290 memset(data, 0, 8);
292 /* Get Status */
293 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
294 USB_REQ_GET_STATUS, USB_DIR_IN,
295 0, 0, data, 2, HZ * 3);
297 /* res = usb_get_status( udev, 0, 0, data ); */
298 dprintk("%s - res = %d status = 0x%x 0x%x", __func__,
299 res, data[0], data[1]);
301 /* This is a strange one. They issue a set address to the device
302 * on the receive control pipe and expect a certain value pair back
304 memset(data, 0, 8);
306 res = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
307 5, USB_TYPE_VENDOR, 0, 0,
308 data, 2, HZ * 3);
309 dprintk("%s - res = %d, devnum = %d", __func__, res, udev->devnum);
310 dprintk("%s - data[0] = %d, data[1] = %d", __func__,
311 data[0], data[1]);
314 /* set feature */
315 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
316 USB_REQ_SET_FEATURE, USB_TYPE_VENDOR,
317 0xc04e, 0x0000, NULL, 0, HZ * 3);
319 dprintk("%s - res = %d", __func__, res);
321 /* These two are sent by the windows driver, but stall for
322 * me. I dont have an analyzer on the linux side so i can't
323 * see what is actually different and why the device takes
324 * issue with them
326 #if 0
327 /* this is some custom control message they send */
328 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
329 0x04, USB_TYPE_VENDOR,
330 0x0808, 0x0000, NULL, 0, HZ * 3);
332 dprintk("%s - res = %d", __func__, res);
334 /* this is another custom control message they send */
335 res = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
336 0x02, USB_TYPE_VENDOR,
337 0x0000, 0x0100, NULL, 0, HZ * 3);
339 dprintk("%s - res = %d", __func__, res);
340 #endif
343 static void msir_cleanup(struct usb_skel *dev)
345 memset(dev->bulk_in_buffer, 0, dev->bulk_in_size);
347 dev->usb_valid_bytes_in_bulk_buffer = 0;
349 dev->last_space = PULSE_MASK;
351 dev->mce_bytes_left_in_packet = 0;
352 dev->lircidx = 0;
353 dev->lirccnt = 0;
354 memset(dev->lircdata, 0, sizeof(dev->lircdata));
357 static int set_use_inc(void *data)
359 MOD_INC_USE_COUNT;
360 return 0;
363 static void set_use_dec(void *data)
365 MOD_DEC_USE_COUNT;
369 * msir_fetch_more_data
371 * The goal here is to read in more remote codes from the remote. In
372 * the event that the remote isn't sending us anything, the caller
373 * will block until a key is pressed (i.e. this performs phys read,
374 * filtering, and queueing of data) unless dont_block is set to 1; in
375 * this situation, it will perform a few reads and will exit out if it
376 * does not see any appropriate data
378 * dev->sem should be locked when this function is called - fine grain
379 * locking isn't really important here anyways
381 * This routine always returns the number of words available
384 static int msir_fetch_more_data(struct usb_skel *dev, int dont_block)
386 int retries = 0;
387 int words_to_read =
388 (sizeof(dev->lircdata)/sizeof(lirc_t)) - dev->lirccnt;
389 int partial, this_read = 0;
390 int bulkidx = 0;
391 int bytes_left_in_packet = 0;
392 signed char *signedp = (signed char*)dev->bulk_in_buffer;
394 if (words_to_read == 0)
395 return dev->lirccnt;
397 /* this forces all existing data to be read by lirc before we
398 * issue another usb command. this is the only form of
399 * throttling we have
401 if (dev->lirccnt)
402 return dev->lirccnt;
404 /* reserve room for our leading space */
405 if (dev->last_space)
406 words_to_read--;
408 while (words_to_read) {
409 /* handle signals and USB disconnects */
410 if (signal_pending(current))
411 return dev->lirccnt ? dev->lirccnt : -EINTR;
412 if (!dev->udev)
413 return -ENODEV;
415 bulkidx = 0;
418 * perform data read (phys or from previous buffer)
421 /* use leftovers if present, otherwise perform a read */
422 if (dev->usb_valid_bytes_in_bulk_buffer) {
423 this_read = dev->usb_valid_bytes_in_bulk_buffer;
424 partial = this_read;
425 dev->usb_valid_bytes_in_bulk_buffer = 0;
426 } else {
427 int retval;
429 this_read = dev->bulk_in_size;
430 partial = 0;
431 retval = usb_bulk_msg(dev->udev,
432 usb_rcvbulkpipe(dev->udev,
433 dev->bulk_in_endpointAddr),
434 (unsigned char *)dev->bulk_in_buffer,
435 this_read, &partial, HZ*10);
437 /* retry a few times on overruns; map all
438 other errors to -EIO */
439 if (retval) {
440 if (retval == -EOVERFLOW && retries < 5) {
441 retries++;
442 interruptible_sleep_on_timeout(
443 &dev->wait_q, HZ);
444 continue;
445 } else
446 return -EIO;
449 retries = 0;
450 if (partial)
451 this_read = partial;
453 /* skip the header */
454 bulkidx += 2;
456 /* check for empty reads (header only) */
457 if (this_read == 2) {
458 /* assume no data */
459 if (dont_block)
460 break;
462 /* sleep for a bit before performing
463 another read */
464 interruptible_sleep_on_timeout(&dev->wait_q, 1);
465 continue;
470 * process data
473 /* at this point this_read is > 0 */
474 while (bulkidx < this_read &&
475 (words_to_read > (dev->last_space ? 1 : 0))) {
476 /* while( bulkidx < this_read && words_to_read) */
477 int keycode;
478 int pulse = 0;
480 /* read packet length if needed */
481 if (!bytes_left_in_packet) {
482 /* we assume we are on a packet length
483 * value. it is possible, in some
484 * cases, to get a packet that does
485 * not start with a length, apparently
486 * due to some sort of fragmenting,
487 * but occaisonally we do not receive
488 * the second half of a fragment
490 bytes_left_in_packet =
491 128 + signedp[bulkidx++];
493 /* unfortunately rather than keep all
494 * the data in the packetized format,
495 * the transceiver sends a trailing 8
496 * bytes that aren't part of the
497 * transmittion from the remote,
498 * aren't packetized, and dont really
499 * have any value. we can basically
500 * tell we have hit them if 1) we have
501 * a loooong space currently stored
502 * up, and 2) the bytes_left value for
503 * this packet is obviously wrong
505 if (bytes_left_in_packet > 4) {
506 if (dev->mce_bytes_left_in_packet) {
507 bytes_left_in_packet =
508 dev->mce_bytes_left_in_packet;
509 bulkidx--;
511 bytes_left_in_packet = 0;
512 bulkidx = this_read;
515 /* always clear this if we have a
516 valid packet */
517 dev->mce_bytes_left_in_packet = 0;
519 /* continue here to verify we haven't
520 hit the end of the bulk_in */
521 continue;
526 * generate mode2
529 keycode = signedp[bulkidx++];
530 if (keycode < 0) {
531 pulse = 1;
532 keycode += 128;
534 keycode *= MCE_TIME_UNIT;
536 bytes_left_in_packet--;
538 if (pulse) {
539 if (dev->last_space) {
540 dev->lircdata[dev->lirccnt++] =
541 dev->last_space;
542 dev->last_space = 0;
543 words_to_read--;
545 /* clear the lirc_t for the pulse */
546 dev->lircdata[dev->lirccnt] = 0;
548 dev->lircdata[dev->lirccnt] += keycode;
549 dev->lircdata[dev->lirccnt] |= PULSE_BIT;
550 } else {
551 /* on pulse->space transition, add one
552 for the existing pulse */
553 if (dev->lircdata[dev->lirccnt] &&
554 !dev->last_space) {
555 dev->lirccnt++;
556 words_to_read--;
559 dev->last_space += keycode;
564 /* save off some info if we are exiting mid-packet, or with
565 leftovers */
566 if (bytes_left_in_packet)
567 dev->mce_bytes_left_in_packet = bytes_left_in_packet;
568 if (bulkidx < this_read) {
569 dev->usb_valid_bytes_in_bulk_buffer = (this_read - bulkidx);
570 memcpy(dev->bulk_in_buffer, &(dev->bulk_in_buffer[bulkidx]),
571 dev->usb_valid_bytes_in_bulk_buffer);
573 return dev->lirccnt;
576 /* mceusb_add_to_buf: called by lirc_dev to fetch all available keys
577 * this is used as a polling interface for us: since we set
578 * plugin->sample_rate we will periodically get the below call to
579 * check for new data returns 0 on success, or -ENODATA if nothing is
580 * available
582 static int mceusb_add_to_buf(void *data, struct lirc_buffer *buf)
584 struct usb_skel *dev = (struct usb_skel *) data;
586 down(&dev->sem);
588 /* verify device still present */
589 if (dev->udev == NULL) {
590 up(&dev->sem);
591 return -ENODEV;
594 if (!dev->lirccnt) {
595 int res;
596 dev->lircidx = 0;
598 res = msir_fetch_more_data(dev, 1);
600 if (res == 0)
601 res = -ENODATA;
602 if (res < 0) {
603 up(&dev->sem);
604 return res;
608 if (dev->lirccnt) {
609 int keys_to_copy;
611 /* determine available buffer space and available data */
612 keys_to_copy = lirc_buffer_available(buf);
613 if (keys_to_copy > dev->lirccnt)
614 keys_to_copy = dev->lirccnt;
616 lirc_buffer_write_n(buf,
617 (unsigned char *) &(dev->lircdata[dev->lircidx]),
618 keys_to_copy);
619 dev->lircidx += keys_to_copy;
620 dev->lirccnt -= keys_to_copy;
622 up(&dev->sem);
623 return 0;
626 up(&dev->sem);
627 return -ENODATA;
631 * mceusb_write_bulk_callback
633 #if defined(KERNEL_2_5) && LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19)
634 static void mceusb_write_bulk_callback(struct urb *urb, struct pt_regs *regs)
635 #else
636 static void mceusb_write_bulk_callback(struct urb *urb)
637 #endif
639 struct usb_skel *dev = (struct usb_skel *)urb->context;
641 dprintk("%s - minor %d", __func__, dev->minor);
643 if ((urb->status != -ENOENT) &&
644 (urb->status != -ECONNRESET)) {
645 dprintk("%s - nonzero write buld status received: %d",
646 __func__, urb->status);
647 return;
650 return;
654 * mceusb_probe
656 * Called by the usb core when a new device is connected that it
657 * thinks this driver might be interested in.
659 #ifdef KERNEL_2_5
660 static int mceusb_probe(struct usb_interface *interface,
661 const struct usb_device_id *id)
663 struct usb_device *udev = interface_to_usbdev(interface);
664 struct usb_host_interface *iface_desc;
665 #else
666 static void *mceusb_probe(struct usb_device *udev, unsigned int ifnum,
667 const struct usb_device_id *id)
669 struct usb_interface *interface = &udev->actconfig->interface[ifnum];
670 struct usb_interface_descriptor *iface_desc;
671 #endif
672 struct usb_skel *dev = NULL;
673 struct usb_endpoint_descriptor *endpoint;
675 struct lirc_plugin *plugin;
676 struct lirc_buffer *rbuf;
678 int minor;
679 size_t buffer_size;
680 int i;
681 int retval = -ENOMEM;
682 char junk[64];
683 int partial = 0;
685 /* See if the device offered us matches what we can accept */
686 if (cpu_to_le16(udev->descriptor.idVendor) != USB_MCEUSB_VENDOR_ID ||
687 cpu_to_le16(udev->descriptor.idProduct) != USB_MCEUSB_PRODUCT_ID) {
688 dprintk("Wrong Vendor/Product IDs");
689 #ifdef KERNEL_2_5
690 return -ENODEV;
691 #else
692 return NULL;
693 #endif
696 /* select a "subminor" number (part of a minor number) */
697 down(&minor_table_mutex);
698 for (minor = 0; minor < MAX_DEVICES; ++minor) {
699 if (minor_table[minor] == NULL)
700 break;
702 if (minor >= MAX_DEVICES) {
703 info("Too many devices plugged in, "
704 "can not handle this device.");
705 goto error;
708 /* allocate memory for our device state and initialize it */
709 dev = kmalloc(sizeof(struct usb_skel), GFP_KERNEL);
710 if (dev == NULL) {
711 err("Out of memory");
712 #ifdef KERNEL_2_5
713 retval = -ENOMEM;
714 #endif
715 goto error;
717 minor_table[minor] = dev;
719 memset(dev, 0x00, sizeof(*dev));
720 init_MUTEX(&dev->sem);
721 dev->udev = udev;
722 dev->interface = interface;
723 dev->minor = minor;
725 /* set up the endpoint information */
726 /* check out the endpoints */
727 /* use only the first bulk-in and bulk-out endpoints */
728 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 5)
729 iface_desc = &interface->altsetting[0];
730 #else
731 iface_desc = interface->cur_altsetting;
732 #endif
734 #ifdef KERNEL_2_5
735 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
736 endpoint = &iface_desc->endpoint[i].desc;
737 #else
738 for (i = 0; i < iface_desc->bNumEndpoints; ++i) {
739 endpoint = &iface_desc->endpoint[i];
740 #endif
741 if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) &&
742 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
743 USB_ENDPOINT_XFER_BULK)) {
744 dprintk("we found a bulk in endpoint");
745 buffer_size = endpoint->wMaxPacketSize;
746 dev->bulk_in_size = buffer_size;
747 dev->bulk_in_endpointAddr = endpoint->bEndpointAddress;
748 #ifdef KERNEL_2_5
749 dev->bulk_in_buffer =
750 usb_buffer_alloc(udev, buffer_size,
751 GFP_ATOMIC, &dev->dma_in);
752 #else
753 dev->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
754 #endif
755 if (!dev->bulk_in_buffer) {
756 err("Couldn't allocate bulk_in_buffer");
757 goto error;
761 if (((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
762 == 0x00)
763 && ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
764 USB_ENDPOINT_XFER_BULK)) {
765 dprintk("we found a bulk out endpoint");
766 #ifdef KERNEL_2_5
767 dev->write_urb = usb_alloc_urb(0, GFP_KERNEL);
768 #else
769 dev->write_urb = usb_alloc_urb(0);
770 #endif
771 if (!dev->write_urb) {
772 err("No free urbs available");
773 goto error;
775 buffer_size = endpoint->wMaxPacketSize;
776 dev->bulk_out_size = buffer_size;
777 dev->bulk_out_endpointAddr = endpoint->bEndpointAddress;
778 #ifdef KERNEL_2_5
779 dev->bulk_out_buffer =
780 usb_buffer_alloc(udev, buffer_size,
781 GFP_ATOMIC, &dev->dma_out);
782 #else
783 dev->bulk_out_buffer = kmalloc(buffer_size, GFP_KERNEL);
784 #endif
785 if (!dev->bulk_out_buffer) {
786 err("Couldn't allocate bulk_out_buffer");
787 goto error;
789 #ifdef KERNEL_2_5
790 usb_fill_bulk_urb(dev->write_urb, udev,
791 usb_sndbulkpipe
792 (udev, endpoint->bEndpointAddress),
793 dev->bulk_out_buffer, buffer_size,
794 mceusb_write_bulk_callback, dev);
795 #else
796 FILL_BULK_URB(dev->write_urb, udev,
797 usb_sndbulkpipe(udev,
798 endpoint->bEndpointAddress),
799 dev->bulk_out_buffer, buffer_size,
800 mceusb_write_bulk_callback, dev);
801 #endif
805 if (!(dev->bulk_in_endpointAddr && dev->bulk_out_endpointAddr)) {
806 err("Couldn't find both bulk-in and bulk-out endpoints");
807 goto error;
810 /* init the waitq */
811 init_waitqueue_head(&dev->wait_q);
814 /* Set up our lirc plugin */
815 plugin = kmalloc(sizeof(struct lirc_plugin), GFP_KERNEL);
816 if (!plugin) {
817 err("out of memory");
818 goto error;
820 memset(plugin, 0, sizeof(struct lirc_plugin));
822 rbuf = kmalloc(sizeof(struct lirc_buffer), GFP_KERNEL);
823 if (!rbuf) {
824 err("out of memory");
825 kfree(plugin);
826 goto error;
829 /* the lirc_atiusb module doesn't memset rbuf here ... ? */
830 if (lirc_buffer_init(rbuf, sizeof(lirc_t), 128)) {
831 err("out of memory");
832 kfree(plugin);
833 kfree(rbuf);
834 goto error;
837 strcpy(plugin->name, DRIVER_NAME " ");
838 plugin->minor = minor;
839 plugin->code_length = sizeof(lirc_t) * 8;
840 plugin->features = LIRC_CAN_REC_MODE2; /* | LIRC_CAN_SEND_MODE2; */
841 plugin->data = dev;
842 plugin->rbuf = rbuf;
843 plugin->ioctl = NULL;
844 plugin->set_use_inc = &set_use_inc;
845 plugin->set_use_dec = &set_use_dec;
846 plugin->sample_rate = 80; /* sample at 100hz (10ms) */
847 plugin->add_to_buf = &mceusb_add_to_buf;
848 /* plugin->fops = &mceusb_fops; */
849 #ifdef LIRC_HAVE_SYSFS
850 plugin->dev = &udev->dev;
851 #endif
852 plugin->owner = THIS_MODULE;
853 if (lirc_register_plugin(plugin) < 0) {
854 kfree(plugin);
855 lirc_buffer_free(rbuf);
856 kfree(rbuf);
857 goto error;
859 dev->plugin = plugin;
861 /* clear off the first few messages. these look like
862 * calibration or test data, i can't really tell
863 * this also flushes in case we have random ir data queued up
865 for (i = 0; i < 40; i++)
866 (void) usb_bulk_msg(udev,
867 usb_rcvbulkpipe(udev,
868 dev->bulk_in_endpointAddr),
869 junk, 64, &partial, HZ*10);
871 msir_cleanup(dev);
872 mceusb_setup(udev);
874 #ifdef KERNEL_2_5
875 /* we can register the device now, as it is ready */
876 usb_set_intfdata(interface, dev);
877 #endif
878 /* let the user know what node this device is now attached to */
879 /* info("USB Microsoft IR Transceiver device now attached to msir%d",
880 dev->minor); */
881 up(&minor_table_mutex);
882 #ifdef KERNEL_2_5
883 return 0;
884 #else
885 return dev;
886 #endif
887 error:
888 mceusb_delete(dev);
889 dev = NULL;
890 dprintk("%s: retval = %x", __func__, retval);
891 up(&minor_table_mutex);
892 #ifdef KERNEL_2_5
893 return retval;
894 #else
895 return NULL;
896 #endif
900 * mceusb_disconnect
902 * Called by the usb core when the device is removed from the system.
904 * This routine guarantees that the driver will not submit any more urbs
905 * by clearing dev->udev. It is also supposed to terminate any currently
906 * active urbs. Unfortunately, usb_bulk_msg(), used in skel_read(), does
907 * not provide any way to do this. But at least we can cancel an active
908 * write.
910 #ifdef KERNEL_2_5
911 static void mceusb_disconnect(struct usb_interface *interface)
912 #else
913 static void mceusb_disconnect(struct usb_device *udev, void *ptr)
914 #endif
916 struct usb_skel *dev;
917 int minor;
918 #ifdef KERNEL_2_5
919 dev = usb_get_intfdata(interface);
920 usb_set_intfdata(interface, NULL);
921 #else
922 dev = (struct usb_skel *)ptr;
923 #endif
925 down(&minor_table_mutex);
926 down(&dev->sem);
927 minor = dev->minor;
929 /* unhook lirc things */
930 lirc_unregister_plugin(minor);
931 lirc_buffer_free(dev->plugin->rbuf);
932 kfree(dev->plugin->rbuf);
933 kfree(dev->plugin);
934 #ifdef KERNEL_2_5
935 /* terminate an ongoing write */
936 if (atomic_read(&dev->write_busy)) {
937 usb_kill_urb(dev->write_urb);
938 wait_for_completion(&dev->write_finished);
941 /* prevent device read, write and ioctl */
942 dev->present = 0;
943 #endif
945 mceusb_delete(dev);
947 info("Microsoft IR Transceiver #%d now disconnected", minor);
948 up(&dev->sem);
949 up(&minor_table_mutex);
955 * usb_mceusb_init
957 static int __init usb_mceusb_init(void)
959 int result;
961 /* register this driver with the USB subsystem */
962 result = usb_register(&mceusb_driver);
963 #ifdef KERNEL_2_5
964 if (result) {
965 #else
966 if (result < 0) {
967 #endif
968 err("usb_register failed for the " DRIVER_NAME
969 " driver. error number %d", result);
970 #ifdef KERNEL_2_5
971 return result;
972 #else
973 return -1;
974 #endif
977 info(DRIVER_DESC " " DRIVER_VERSION);
978 return 0;
983 * usb_mceusb_exit
985 static void __exit usb_mceusb_exit(void)
987 /* deregister this driver with the USB subsystem */
988 usb_deregister(&mceusb_driver);
991 module_init(usb_mceusb_init);
992 module_exit(usb_mceusb_exit);
994 MODULE_DESCRIPTION(DRIVER_DESC);
995 MODULE_AUTHOR(DRIVER_AUTHOR);
996 MODULE_LICENSE("GPL");
997 MODULE_DEVICE_TABLE(usb, mceusb_table);
999 module_param(debug, int, 0644);
1000 MODULE_PARM_DESC(debug, "Debug enabled or not");
1002 EXPORT_NO_SYMBOLS;