78 #include <linux/kernel.h>
79 #include <linux/errno.h>
81 #include <linux/slab.h>
82 #include <linux/module.h>
85 #include <asm/uaccess.h>
87 #include <linux/poll.h>
90 #ifdef CONFIG_USB_DEBUG
98 #define dbg(lvl, format, arg...) \
101 printk(KERN_DEBUG "%s: " format "\n", __FILE__, ##arg); \
105 #define DRIVER_VERSION "v0.96"
107 #define DRIVER_DESC "LEGO USB Tower Driver"
120 static int read_buffer_size = 480;
130 static int write_buffer_size = 480;
144 static int packet_timeout = 50;
152 static int read_timeout = 200;
165 static int interrupt_in_interval = 2;
169 static int interrupt_out_interval = 8;
174 #define LEGO_USB_TOWER_VENDOR_ID 0x0694
175 #define LEGO_USB_TOWER_PRODUCT_ID 0x0001
178 #define LEGO_USB_TOWER_REQUEST_RESET 0x04
179 #define LEGO_USB_TOWER_REQUEST_GET_VERSION 0xFD
206 #define LEGO_USB_TOWER_MINOR_BASE 160
250 static loff_t tower_llseek (
struct file *
file, loff_t off,
int whence);
254 static void tower_interrupt_in_callback (
struct urb *
urb);
255 static void tower_interrupt_out_callback (
struct urb *
urb);
265 .write = tower_write,
267 .release = tower_release,
269 .llseek = tower_llseek,
281 static struct usb_class_driver tower_class = {
282 .name =
"legousbtower%d",
283 .devnode = legousbtower_devnode,
290 static struct usb_driver tower_driver = {
291 .name =
"legousbtower",
292 .probe = tower_probe,
293 .disconnect = tower_disconnect,
294 .id_table = tower_table,
301 static inline void lego_usb_tower_debug_data (
int level,
const char *
function,
int size,
const unsigned char *
data)
308 printk (
KERN_DEBUG "%s: %s - length = %d, data = ", __FILE__,
function, size);
309 for (i = 0; i <
size; ++
i) {
310 printk (
"%.2x ", data[i]);
321 dbg(2,
"%s: enter", __func__);
323 tower_abort_transfers (dev);
333 dbg(2,
"%s: leave", __func__);
349 dbg(2,
"%s: enter", __func__);
352 subminor = iminor(inode);
364 dev = usb_get_intfdata(interface);
391 usb_rcvctrlpipe(dev->
udev, 0),
401 "LEGO USB Tower reset control request failed\n");
414 tower_interrupt_in_callback,
425 "Couldn't submit interrupt_in_urb %d\n", retval);
438 dbg(2,
"%s: leave, return value %d ", __func__, retval);
446 static int tower_release (
struct inode *inode,
struct file *file)
451 dbg(2,
"%s: enter", __func__);
456 dbg(1,
"%s: object is NULL", __func__);
468 dbg(1,
"%s: device not opened exactly once", __func__);
485 tower_abort_transfers (dev);
494 dbg(2,
"%s: leave, return value %d", __func__, retval);
505 dbg(2,
"%s: enter", __func__);
508 dbg(1,
"%s: dev is null", __func__);
523 dbg(2,
"%s: leave", __func__);
535 static void tower_check_for_read_packet (
struct lego_usb_tower *dev)
551 static unsigned int tower_poll (
struct file *file,
poll_table *
wait)
554 unsigned int mask = 0;
556 dbg(2,
"%s: enter", __func__);
566 tower_check_for_read_packet(dev);
574 dbg(2,
"%s: leave, mask = %d", __func__, mask);
583 static loff_t tower_llseek (
struct file *file, loff_t off,
int whence)
592 static ssize_t tower_read (
struct file *file,
char __user *
buffer,
size_t count, loff_t *ppos)
595 size_t bytes_to_read;
600 dbg(2,
"%s: enter, count = %Zd", __func__, count);
613 printk(
KERN_ERR "legousbtower: No device or device unplugged %d\n", retval);
619 dbg(1,
"%s: read request of 0 bytes", __func__);
624 timeout =
jiffies + read_timeout *
HZ / 1000;
628 tower_check_for_read_packet (dev);
642 timeout =
jiffies + read_timeout *
HZ / 1000;
645 if (read_timeout &&
time_after (jiffies, timeout)) {
649 tower_check_for_read_packet (dev);
668 retval = bytes_to_read;
675 dbg(2,
"%s: leave, return value %d", __func__, retval);
683 static ssize_t tower_write (
struct file *file,
const char __user *buffer,
size_t count, loff_t *ppos)
686 size_t bytes_to_write;
689 dbg(2,
"%s: enter, count = %Zd", __func__, count);
702 printk(
KERN_ERR "legousbtower: No device or device unplugged %d\n", retval);
708 dbg(1,
"%s: write request of 0 bytes", __func__);
725 bytes_to_write =
min_t(
int, count, write_buffer_size);
726 dbg(4,
"%s: count = %Zd, bytes_to_write = %Zd", __func__, count, bytes_to_write);
739 tower_interrupt_out_callback,
750 "Couldn't submit interrupt_out_urb %d\n", retval);
753 retval = bytes_to_write;
760 dbg(2,
"%s: leave, return value %d", __func__, retval);
769 static void tower_interrupt_in_callback (
struct urb *
urb)
775 dbg(4,
"%s: enter, status %d", __func__, status);
777 lego_usb_tower_debug_data(5, __func__, urb->actual_length, urb->transfer_buffer);
785 dbg(1,
"%s: nonzero status received: %d", __func__, status);
790 if (urb->actual_length > 0) {
798 dbg(3,
"%s: received %d bytes", __func__, urb->actual_length);
800 printk(
KERN_WARNING "%s: read_buffer overflow, %d bytes dropped", __func__, urb->actual_length);
811 "%s: usb_submit_urb failed (%d)\n",
819 lego_usb_tower_debug_data(5, __func__, urb->actual_length, urb->transfer_buffer);
820 dbg(4,
"%s: leave, status %d", __func__, status);
827 static void tower_interrupt_out_callback (
struct urb *urb)
830 int status = urb->status;
832 dbg(4,
"%s: enter, status %d", __func__, status);
833 lego_usb_tower_debug_data(5, __func__, urb->actual_length, urb->transfer_buffer);
836 if (status && !(status == -
ENOENT ||
839 dbg(1,
"%s - nonzero write bulk status received: %d",
846 lego_usb_tower_debug_data(5, __func__, urb->actual_length, urb->transfer_buffer);
847 dbg(4,
"%s: leave, status %d", __func__, status);
860 struct usb_device *
udev = interface_to_usbdev(interface);
862 struct usb_host_interface *iface_desc;
869 dbg(2,
"%s: enter", __func__);
876 dev_err(idev,
"Out of memory\n");
906 iface_desc = interface->cur_altsetting;
909 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++
i) {
910 endpoint = &iface_desc->endpoint[
i].desc;
912 if (usb_endpoint_xfer_int(endpoint)) {
913 if (usb_endpoint_dir_in(endpoint))
920 dev_err(idev,
"interrupt in endpoint not found\n");
924 dev_err(idev,
"interrupt out endpoint not found\n");
930 dev_err(idev,
"Couldn't allocate read_buffer\n");
935 dev_err(idev,
"Couldn't allocate interrupt_in_buffer\n");
940 dev_err(idev,
"Couldn't allocate interrupt_in_urb\n");
945 dev_err(idev,
"Couldn't allocate interrupt_out_buffer\n");
950 dev_err(idev,
"Couldn't allocate interrupt_out_urb\n");
957 usb_set_intfdata (interface, dev);
963 dev_err(idev,
"Not able to get a minor for this device.\n");
964 usb_set_intfdata (interface,
NULL);
967 dev->
minor = interface->minor;
970 dev_info(&interface->dev,
"LEGO USB Tower #%d now attached to major "
976 usb_rcvctrlpipe(udev, 0),
982 sizeof(get_version_reply),
985 dev_err(idev,
"LEGO USB Tower get version control request failed\n");
989 dev_info(&interface->dev,
"LEGO USB Tower firmware version is %d.%d "
990 "build %d\n", get_version_reply.major,
991 get_version_reply.minor,
996 dbg(2,
"%s: leave, return value 0x%.8lx (dev)", __func__, (
long) dev);
1011 static void tower_disconnect (
struct usb_interface *interface)
1016 dbg(2,
"%s: enter", __func__);
1018 dev = usb_get_intfdata (interface);
1020 usb_set_intfdata (interface,
NULL);
1042 dev_info(&interface->dev,
"LEGO USB Tower #%d now disconnected\n",
1045 dbg(2,
"%s: leave", __func__);
1052 #ifdef MODULE_LICENSE