blob: eb37c9542052846396c9c38ea7cc3da7079266ab [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * LEGO USB Tower driver
3 *
4 * Copyright (C) 2003 David Glance <davidgsf@sourceforge.net>
5 * 2001-2004 Juergen Stuber <starblue@users.sourceforge.net>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
11 *
12 * derived from USB Skeleton driver - 0.5
13 * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
14 *
15 * History:
16 *
17 * 2001-10-13 - 0.1 js
18 * - first version
19 * 2001-11-03 - 0.2 js
20 * - simplified buffering, one-shot URBs for writing
21 * 2001-11-10 - 0.3 js
22 * - removed IOCTL (setting power/mode is more complicated, postponed)
23 * 2001-11-28 - 0.4 js
24 * - added vendor commands for mode of operation and power level in open
25 * 2001-12-04 - 0.5 js
26 * - set IR mode by default (by oversight 0.4 set VLL mode)
27 * 2002-01-11 - 0.5? pcchan
28 * - make read buffer reusable and work around bytes_to_write issue between
29 * uhci and legusbtower
30 * 2002-09-23 - 0.52 david (david@csse.uwa.edu.au)
31 * - imported into lejos project
32 * - changed wake_up to wake_up_interruptible
33 * - changed to use lego0 rather than tower0
Harvey Harrison441b62c2008-03-03 16:08:34 -080034 * - changed dbg() to use __func__ rather than deprecated __func__
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 * 2003-01-12 - 0.53 david (david@csse.uwa.edu.au)
36 * - changed read and write to write everything or
37 * timeout (from a patch by Chris Riesen and Brett Thaeler driver)
38 * - added ioctl functionality to set timeouts
39 * 2003-07-18 - 0.54 davidgsf (david@csse.uwa.edu.au)
40 * - initial import into LegoUSB project
41 * - merge of existing LegoUSB.c driver
42 * 2003-07-18 - 0.56 davidgsf (david@csse.uwa.edu.au)
43 * - port to 2.6 style driver
44 * 2004-02-29 - 0.6 Juergen Stuber <starblue@users.sourceforge.net>
45 * - fix locking
46 * - unlink read URBs which are no longer needed
47 * - allow increased buffer size, eliminates need for timeout on write
48 * - have read URB running continuously
49 * - added poll
50 * - forbid seeking
51 * - added nonblocking I/O
Harvey Harrison441b62c2008-03-03 16:08:34 -080052 * - changed back __func__ to __func__
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 * - read and log tower firmware version
54 * - reset tower on probe, avoids failure of first write
55 * 2004-03-09 - 0.7 Juergen Stuber <starblue@users.sourceforge.net>
56 * - timeout read now only after inactivity, shorten default accordingly
57 * 2004-03-11 - 0.8 Juergen Stuber <starblue@users.sourceforge.net>
58 * - log major, minor instead of possibly confusing device filename
59 * - whitespace cleanup
60 * 2004-03-12 - 0.9 Juergen Stuber <starblue@users.sourceforge.net>
61 * - normalize whitespace in debug messages
62 * - take care about endianness in control message responses
63 * 2004-03-13 - 0.91 Juergen Stuber <starblue@users.sourceforge.net>
64 * - make default intervals longer to accommodate current EHCI driver
65 * 2004-03-19 - 0.92 Juergen Stuber <starblue@users.sourceforge.net>
66 * - replaced atomic_t by memory barriers
67 * 2004-04-21 - 0.93 Juergen Stuber <starblue@users.sourceforge.net>
68 * - wait for completion of write urb in release (needed for remotecontrol)
69 * - corrected poll for write direction (missing negation)
70 * 2004-04-22 - 0.94 Juergen Stuber <starblue@users.sourceforge.net>
71 * - make device locking interruptible
72 * 2004-04-30 - 0.95 Juergen Stuber <starblue@users.sourceforge.net>
73 * - check for valid udev on resubmitting and unlinking urbs
74 * 2004-08-03 - 0.96 Juergen Stuber <starblue@users.sourceforge.net>
75 * - move reset into open to clean out spurious data
76 */
77
Greg Kroah-Hartman38726bf2013-06-26 16:30:42 -070078#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#include <linux/kernel.h>
81#include <linux/errno.h>
82#include <linux/init.h>
83#include <linux/slab.h>
84#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#include <linux/completion.h>
Arjan van de Ven4186ecf2006-01-11 15:55:29 +010086#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#include <asm/uaccess.h>
88#include <linux/usb.h>
89#include <linux/poll.h>
90
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/* Version Information */
93#define DRIVER_VERSION "v0.96"
94#define DRIVER_AUTHOR "Juergen Stuber <starblue@sourceforge.net>"
95#define DRIVER_DESC "LEGO USB Tower Driver"
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98/* The defaults are chosen to work with the latest versions of leJOS and NQC.
99 */
100
101/* Some legacy software likes to receive packets in one piece.
102 * In this case read_buffer_size should exceed the maximal packet length
103 * (417 for datalog uploads), and packet_timeout should be set.
104 */
105static int read_buffer_size = 480;
106module_param(read_buffer_size, int, 0);
107MODULE_PARM_DESC(read_buffer_size, "Read buffer size");
108
109/* Some legacy software likes to send packets in one piece.
110 * In this case write_buffer_size should exceed the maximal packet length
111 * (417 for firmware and program downloads).
112 * A problem with long writes is that the following read may time out
113 * if the software is not prepared to wait long enough.
114 */
115static int write_buffer_size = 480;
116module_param(write_buffer_size, int, 0);
117MODULE_PARM_DESC(write_buffer_size, "Write buffer size");
118
119/* Some legacy software expects reads to contain whole LASM packets.
120 * To achieve this, characters which arrive before a packet timeout
121 * occurs will be returned in a single read operation.
122 * A problem with long reads is that the software may time out
123 * if it is not prepared to wait long enough.
124 * The packet timeout should be greater than the time between the
125 * reception of subsequent characters, which should arrive about
126 * every 5ms for the standard 2400 baud.
127 * Set it to 0 to disable.
128 */
129static int packet_timeout = 50;
130module_param(packet_timeout, int, 0);
131MODULE_PARM_DESC(packet_timeout, "Packet timeout in ms");
132
133/* Some legacy software expects blocking reads to time out.
134 * Timeout occurs after the specified time of read and write inactivity.
135 * Set it to 0 to disable.
136 */
137static int read_timeout = 200;
138module_param(read_timeout, int, 0);
139MODULE_PARM_DESC(read_timeout, "Read timeout in ms");
140
141/* As of kernel version 2.6.4 ehci-hcd uses an
142 * "only one interrupt transfer per frame" shortcut
143 * to simplify the scheduling of periodic transfers.
144 * This conflicts with our standard 1ms intervals for in and out URBs.
145 * We use default intervals of 2ms for in and 8ms for out transfers,
146 * which is fast enough for 2400 baud and allows a small additional load.
147 * Increase the interval to allow more devices that do interrupt transfers,
148 * or set to 0 to use the standard interval from the endpoint descriptors.
149 */
150static int interrupt_in_interval = 2;
151module_param(interrupt_in_interval, int, 0);
152MODULE_PARM_DESC(interrupt_in_interval, "Interrupt in interval in ms");
153
154static int interrupt_out_interval = 8;
155module_param(interrupt_out_interval, int, 0);
156MODULE_PARM_DESC(interrupt_out_interval, "Interrupt out interval in ms");
157
158/* Define these values to match your device */
159#define LEGO_USB_TOWER_VENDOR_ID 0x0694
160#define LEGO_USB_TOWER_PRODUCT_ID 0x0001
161
162/* Vendor requests */
163#define LEGO_USB_TOWER_REQUEST_RESET 0x04
164#define LEGO_USB_TOWER_REQUEST_GET_VERSION 0xFD
165
166struct tower_reset_reply {
167 __le16 size; /* little-endian */
168 __u8 err_code;
169 __u8 spare;
170} __attribute__ ((packed));
171
172struct tower_get_version_reply {
173 __le16 size; /* little-endian */
174 __u8 err_code;
175 __u8 spare;
176 __u8 major;
177 __u8 minor;
178 __le16 build_no; /* little-endian */
179} __attribute__ ((packed));
180
181
182/* table of devices that work with this driver */
Németh Márton33b9e162010-01-10 15:34:45 +0100183static const struct usb_device_id tower_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 { USB_DEVICE(LEGO_USB_TOWER_VENDOR_ID, LEGO_USB_TOWER_PRODUCT_ID) },
185 { } /* Terminating entry */
186};
187
188MODULE_DEVICE_TABLE (usb, tower_table);
Oliver Neukum78663ec2007-10-25 15:48:39 +0200189static DEFINE_MUTEX(open_disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191#define LEGO_USB_TOWER_MINOR_BASE 160
192
193
194/* Structure to hold all of our device specific stuff */
195struct lego_usb_tower {
Daniel Walker18bcbcf2008-01-11 08:45:44 -0800196 struct mutex lock; /* locks this structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 struct usb_device* udev; /* save off the usb device pointer */
198 unsigned char minor; /* the starting minor number for this device */
199
200 int open_count; /* number of times this port has been opened */
201
202 char* read_buffer;
203 size_t read_buffer_length; /* this much came in */
204 size_t read_packet_length; /* this much will be returned on read */
205 spinlock_t read_buffer_lock;
206 int packet_timeout_jiffies;
207 unsigned long read_last_arrival;
208
209 wait_queue_head_t read_wait;
210 wait_queue_head_t write_wait;
211
212 char* interrupt_in_buffer;
213 struct usb_endpoint_descriptor* interrupt_in_endpoint;
214 struct urb* interrupt_in_urb;
215 int interrupt_in_interval;
216 int interrupt_in_running;
217 int interrupt_in_done;
218
219 char* interrupt_out_buffer;
220 struct usb_endpoint_descriptor* interrupt_out_endpoint;
221 struct urb* interrupt_out_urb;
222 int interrupt_out_interval;
223 int interrupt_out_busy;
224
225};
226
227
228/* local function prototypes */
229static ssize_t tower_read (struct file *file, char __user *buffer, size_t count, loff_t *ppos);
230static ssize_t tower_write (struct file *file, const char __user *buffer, size_t count, loff_t *ppos);
231static inline void tower_delete (struct lego_usb_tower *dev);
232static int tower_open (struct inode *inode, struct file *file);
233static int tower_release (struct inode *inode, struct file *file);
234static unsigned int tower_poll (struct file *file, poll_table *wait);
235static loff_t tower_llseek (struct file *file, loff_t off, int whence);
236
237static void tower_abort_transfers (struct lego_usb_tower *dev);
238static void tower_check_for_read_packet (struct lego_usb_tower *dev);
David Howells7d12e782006-10-05 14:55:46 +0100239static void tower_interrupt_in_callback (struct urb *urb);
240static void tower_interrupt_out_callback (struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242static int tower_probe (struct usb_interface *interface, const struct usb_device_id *id);
243static void tower_disconnect (struct usb_interface *interface);
244
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246/* file operations needed when we register this driver */
Luiz Fernando N. Capitulino066202d2006-08-05 20:37:11 -0300247static const struct file_operations tower_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 .owner = THIS_MODULE,
249 .read = tower_read,
250 .write = tower_write,
251 .open = tower_open,
252 .release = tower_release,
253 .poll = tower_poll,
254 .llseek = tower_llseek,
255};
256
Al Viro2c9ede52011-07-23 20:24:48 -0400257static char *legousbtower_devnode(struct device *dev, umode_t *mode)
Kay Sieversf7a386c2009-04-30 15:23:42 +0200258{
259 return kasprintf(GFP_KERNEL, "usb/%s", dev_name(dev));
260}
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262/*
263 * usb class driver info in order to get a minor number from the usb core,
Greg Kroah-Hartmand6e5bcf2005-06-20 21:15:16 -0700264 * and to have the device registered with the driver core
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 */
266static struct usb_class_driver tower_class = {
Greg Kroah-Hartmand6e5bcf2005-06-20 21:15:16 -0700267 .name = "legousbtower%d",
Kay Sieverse454cea2009-09-18 23:01:12 +0200268 .devnode = legousbtower_devnode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 .fops = &tower_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 .minor_base = LEGO_USB_TOWER_MINOR_BASE,
271};
272
273
274/* usb specific object needed to register this driver with the usb subsystem */
275static struct usb_driver tower_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 .name = "legousbtower",
277 .probe = tower_probe,
278 .disconnect = tower_disconnect,
279 .id_table = tower_table,
280};
281
282
283/**
284 * lego_usb_tower_debug_data
285 */
Greg Kroah-Hartman4dae9962013-06-26 16:30:41 -0700286static inline void lego_usb_tower_debug_data(struct device *dev,
287 const char *function, int size,
288 const unsigned char *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Greg Kroah-Hartman4dae9962013-06-26 16:30:41 -0700290 dev_dbg(dev, "%s - length = %d, data = %*ph\n",
291 function, size, size, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
294
295/**
296 * tower_delete
297 */
298static inline void tower_delete (struct lego_usb_tower *dev)
299{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 tower_abort_transfers (dev);
301
302 /* free data structures */
Mariusz Kozlowskif53510e2006-11-08 15:36:14 +0100303 usb_free_urb(dev->interrupt_in_urb);
304 usb_free_urb(dev->interrupt_out_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 kfree (dev->read_buffer);
306 kfree (dev->interrupt_in_buffer);
307 kfree (dev->interrupt_out_buffer);
308 kfree (dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310
311
312/**
313 * tower_open
314 */
315static int tower_open (struct inode *inode, struct file *file)
316{
317 struct lego_usb_tower *dev = NULL;
318 int subminor;
319 int retval = 0;
320 struct usb_interface *interface;
321 struct tower_reset_reply reset_reply;
322 int result;
323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 nonseekable_open(inode, file);
325 subminor = iminor(inode);
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 interface = usb_find_interface (&tower_driver, subminor);
328
329 if (!interface) {
Greg Kroah-Hartman38726bf2013-06-26 16:30:42 -0700330 pr_err("error, can't find device for minor %d\n", subminor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 retval = -ENODEV;
Alan Sternd4ead162007-05-22 11:46:41 -0400332 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 }
334
Oliver Neukum78663ec2007-10-25 15:48:39 +0200335 mutex_lock(&open_disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 dev = usb_get_intfdata(interface);
337
338 if (!dev) {
Oliver Neukum78663ec2007-10-25 15:48:39 +0200339 mutex_unlock(&open_disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 retval = -ENODEV;
Alan Sternd4ead162007-05-22 11:46:41 -0400341 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
343
344 /* lock this device */
Daniel Walker18bcbcf2008-01-11 08:45:44 -0800345 if (mutex_lock_interruptible(&dev->lock)) {
Oliver Neukum78663ec2007-10-25 15:48:39 +0200346 mutex_unlock(&open_disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 retval = -ERESTARTSYS;
Alan Sternd4ead162007-05-22 11:46:41 -0400348 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
350
Oliver Neukum78663ec2007-10-25 15:48:39 +0200351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 /* allow opening only once */
353 if (dev->open_count) {
Oliver Neukum78663ec2007-10-25 15:48:39 +0200354 mutex_unlock(&open_disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 retval = -EBUSY;
356 goto unlock_exit;
357 }
358 dev->open_count = 1;
Oliver Neukum78663ec2007-10-25 15:48:39 +0200359 mutex_unlock(&open_disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 /* reset the tower */
362 result = usb_control_msg (dev->udev,
363 usb_rcvctrlpipe(dev->udev, 0),
364 LEGO_USB_TOWER_REQUEST_RESET,
365 USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_DEVICE,
366 0,
367 0,
368 &reset_reply,
369 sizeof(reset_reply),
370 1000);
371 if (result < 0) {
Greg Kroah-Hartman9d974b22012-04-20 16:53:48 -0700372 dev_err(&dev->udev->dev,
373 "LEGO USB Tower reset control request failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 retval = result;
375 goto unlock_exit;
376 }
377
378 /* initialize in direction */
379 dev->read_buffer_length = 0;
380 dev->read_packet_length = 0;
381 usb_fill_int_urb (dev->interrupt_in_urb,
382 dev->udev,
383 usb_rcvintpipe(dev->udev, dev->interrupt_in_endpoint->bEndpointAddress),
384 dev->interrupt_in_buffer,
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700385 usb_endpoint_maxp(dev->interrupt_in_endpoint),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 tower_interrupt_in_callback,
387 dev,
388 dev->interrupt_in_interval);
389
390 dev->interrupt_in_running = 1;
391 dev->interrupt_in_done = 0;
392 mb();
393
394 retval = usb_submit_urb (dev->interrupt_in_urb, GFP_KERNEL);
395 if (retval) {
Greg Kroah-Hartman9d974b22012-04-20 16:53:48 -0700396 dev_err(&dev->udev->dev,
397 "Couldn't submit interrupt_in_urb %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 dev->interrupt_in_running = 0;
399 dev->open_count = 0;
400 goto unlock_exit;
401 }
402
403 /* save device in the file's private structure */
404 file->private_data = dev;
405
406unlock_exit:
Daniel Walker18bcbcf2008-01-11 08:45:44 -0800407 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Alan Sternd4ead162007-05-22 11:46:41 -0400409exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return retval;
411}
412
413/**
414 * tower_release
415 */
416static int tower_release (struct inode *inode, struct file *file)
417{
418 struct lego_usb_tower *dev;
419 int retval = 0;
420
Joe Perches5bd6e8b2010-07-12 13:50:12 -0700421 dev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 if (dev == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 retval = -ENODEV;
Oliver Neukum78663ec2007-10-25 15:48:39 +0200425 goto exit_nolock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 }
427
Oliver Neukum78663ec2007-10-25 15:48:39 +0200428 mutex_lock(&open_disc_mutex);
Daniel Walker18bcbcf2008-01-11 08:45:44 -0800429 if (mutex_lock_interruptible(&dev->lock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 retval = -ERESTARTSYS;
431 goto exit;
432 }
433
434 if (dev->open_count != 1) {
Greg Kroah-Hartmanfef526c2013-06-26 16:30:40 -0700435 dev_dbg(&dev->udev->dev, "%s: device not opened exactly once\n",
436 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 retval = -ENODEV;
438 goto unlock_exit;
439 }
440 if (dev->udev == NULL) {
441 /* the device was unplugged before the file was released */
Daniel Walker18bcbcf2008-01-11 08:45:44 -0800442
443 /* unlock here as tower_delete frees dev */
444 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 tower_delete (dev);
446 goto exit;
447 }
448
449 /* wait until write transfer is finished */
450 if (dev->interrupt_out_busy) {
451 wait_event_interruptible_timeout (dev->write_wait, !dev->interrupt_out_busy, 2 * HZ);
452 }
453 tower_abort_transfers (dev);
454 dev->open_count = 0;
455
456unlock_exit:
Daniel Walker18bcbcf2008-01-11 08:45:44 -0800457 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459exit:
Oliver Neukum78663ec2007-10-25 15:48:39 +0200460 mutex_unlock(&open_disc_mutex);
461exit_nolock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 return retval;
463}
464
465
466/**
467 * tower_abort_transfers
468 * aborts transfers and frees associated data structures
469 */
470static void tower_abort_transfers (struct lego_usb_tower *dev)
471{
Greg Kroah-Hartmanfef526c2013-06-26 16:30:40 -0700472 if (dev == NULL)
Greg Kroah-Hartmand26f6e52013-06-26 16:30:39 -0700473 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 /* shutdown transfer */
476 if (dev->interrupt_in_running) {
477 dev->interrupt_in_running = 0;
478 mb();
Mariusz Kozlowskif53510e2006-11-08 15:36:14 +0100479 if (dev->udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 usb_kill_urb (dev->interrupt_in_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 }
Mariusz Kozlowskif53510e2006-11-08 15:36:14 +0100482 if (dev->interrupt_out_busy && dev->udev)
483 usb_kill_urb(dev->interrupt_out_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
485
486
487/**
488 * tower_check_for_read_packet
489 *
490 * To get correct semantics for signals and non-blocking I/O
491 * with packetizing we pretend not to see any data in the read buffer
492 * until it has been there unchanged for at least
493 * dev->packet_timeout_jiffies, or until the buffer is full.
494 */
495static void tower_check_for_read_packet (struct lego_usb_tower *dev)
496{
497 spin_lock_irq (&dev->read_buffer_lock);
498 if (!packet_timeout
499 || time_after(jiffies, dev->read_last_arrival + dev->packet_timeout_jiffies)
500 || dev->read_buffer_length == read_buffer_size) {
501 dev->read_packet_length = dev->read_buffer_length;
502 }
503 dev->interrupt_in_done = 0;
504 spin_unlock_irq (&dev->read_buffer_lock);
505}
506
507
508/**
509 * tower_poll
510 */
511static unsigned int tower_poll (struct file *file, poll_table *wait)
512{
513 struct lego_usb_tower *dev;
514 unsigned int mask = 0;
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 dev = file->private_data;
517
Oliver Neukumd35b4ce2009-07-02 12:07:07 +0200518 if (!dev->udev)
519 return POLLERR | POLLHUP;
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 poll_wait(file, &dev->read_wait, wait);
522 poll_wait(file, &dev->write_wait, wait);
523
524 tower_check_for_read_packet(dev);
525 if (dev->read_packet_length > 0) {
526 mask |= POLLIN | POLLRDNORM;
527 }
528 if (!dev->interrupt_out_busy) {
529 mask |= POLLOUT | POLLWRNORM;
530 }
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 return mask;
533}
534
535
536/**
537 * tower_llseek
538 */
539static loff_t tower_llseek (struct file *file, loff_t off, int whence)
540{
541 return -ESPIPE; /* unseekable */
542}
543
544
545/**
546 * tower_read
547 */
548static ssize_t tower_read (struct file *file, char __user *buffer, size_t count, loff_t *ppos)
549{
550 struct lego_usb_tower *dev;
551 size_t bytes_to_read;
552 int i;
553 int retval = 0;
554 unsigned long timeout = 0;
555
Joe Perches5bd6e8b2010-07-12 13:50:12 -0700556 dev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 /* lock this object */
Daniel Walker18bcbcf2008-01-11 08:45:44 -0800559 if (mutex_lock_interruptible(&dev->lock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 retval = -ERESTARTSYS;
561 goto exit;
562 }
563
564 /* verify that the device wasn't unplugged */
565 if (dev->udev == NULL) {
566 retval = -ENODEV;
Greg Kroah-Hartman38726bf2013-06-26 16:30:42 -0700567 pr_err("No device or device unplugged %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 goto unlock_exit;
569 }
570
571 /* verify that we actually have some data to read */
572 if (count == 0) {
Greg Kroah-Hartmanfef526c2013-06-26 16:30:40 -0700573 dev_dbg(&dev->udev->dev, "read request of 0 bytes\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 goto unlock_exit;
575 }
576
577 if (read_timeout) {
578 timeout = jiffies + read_timeout * HZ / 1000;
579 }
580
581 /* wait for data */
582 tower_check_for_read_packet (dev);
583 while (dev->read_packet_length == 0) {
584 if (file->f_flags & O_NONBLOCK) {
585 retval = -EAGAIN;
586 goto unlock_exit;
587 }
588 retval = wait_event_interruptible_timeout(dev->read_wait, dev->interrupt_in_done, dev->packet_timeout_jiffies);
589 if (retval < 0) {
590 goto unlock_exit;
591 }
592
593 /* reset read timeout during read or write activity */
594 if (read_timeout
595 && (dev->read_buffer_length || dev->interrupt_out_busy)) {
596 timeout = jiffies + read_timeout * HZ / 1000;
597 }
598 /* check for read timeout */
599 if (read_timeout && time_after (jiffies, timeout)) {
600 retval = -ETIMEDOUT;
601 goto unlock_exit;
602 }
603 tower_check_for_read_packet (dev);
604 }
605
606 /* copy the data from read_buffer into userspace */
607 bytes_to_read = min(count, dev->read_packet_length);
608
609 if (copy_to_user (buffer, dev->read_buffer, bytes_to_read)) {
610 retval = -EFAULT;
611 goto unlock_exit;
612 }
613
614 spin_lock_irq (&dev->read_buffer_lock);
615 dev->read_buffer_length -= bytes_to_read;
616 dev->read_packet_length -= bytes_to_read;
617 for (i=0; i<dev->read_buffer_length; i++) {
618 dev->read_buffer[i] = dev->read_buffer[i+bytes_to_read];
619 }
620 spin_unlock_irq (&dev->read_buffer_lock);
621
622 retval = bytes_to_read;
623
624unlock_exit:
625 /* unlock the device */
Daniel Walker18bcbcf2008-01-11 08:45:44 -0800626 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return retval;
630}
631
632
633/**
634 * tower_write
635 */
636static ssize_t tower_write (struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
637{
638 struct lego_usb_tower *dev;
639 size_t bytes_to_write;
640 int retval = 0;
641
Joe Perches5bd6e8b2010-07-12 13:50:12 -0700642 dev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 /* lock this object */
Daniel Walker18bcbcf2008-01-11 08:45:44 -0800645 if (mutex_lock_interruptible(&dev->lock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 retval = -ERESTARTSYS;
647 goto exit;
648 }
649
650 /* verify that the device wasn't unplugged */
651 if (dev->udev == NULL) {
652 retval = -ENODEV;
Greg Kroah-Hartman38726bf2013-06-26 16:30:42 -0700653 pr_err("No device or device unplugged %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 goto unlock_exit;
655 }
656
657 /* verify that we actually have some data to write */
658 if (count == 0) {
Greg Kroah-Hartmanfef526c2013-06-26 16:30:40 -0700659 dev_dbg(&dev->udev->dev, "write request of 0 bytes\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 goto unlock_exit;
661 }
662
663 /* wait until previous transfer is finished */
664 while (dev->interrupt_out_busy) {
665 if (file->f_flags & O_NONBLOCK) {
666 retval = -EAGAIN;
667 goto unlock_exit;
668 }
669 retval = wait_event_interruptible (dev->write_wait, !dev->interrupt_out_busy);
670 if (retval) {
671 goto unlock_exit;
672 }
673 }
674
675 /* write the data into interrupt_out_buffer from userspace */
676 bytes_to_write = min_t(int, count, write_buffer_size);
Greg Kroah-Hartmanfef526c2013-06-26 16:30:40 -0700677 dev_dbg(&dev->udev->dev, "%s: count = %Zd, bytes_to_write = %Zd\n",
678 __func__, count, bytes_to_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 if (copy_from_user (dev->interrupt_out_buffer, buffer, bytes_to_write)) {
681 retval = -EFAULT;
682 goto unlock_exit;
683 }
684
685 /* send off the urb */
686 usb_fill_int_urb(dev->interrupt_out_urb,
687 dev->udev,
688 usb_sndintpipe(dev->udev, dev->interrupt_out_endpoint->bEndpointAddress),
689 dev->interrupt_out_buffer,
690 bytes_to_write,
691 tower_interrupt_out_callback,
692 dev,
693 dev->interrupt_out_interval);
694
695 dev->interrupt_out_busy = 1;
696 wmb();
697
698 retval = usb_submit_urb (dev->interrupt_out_urb, GFP_KERNEL);
699 if (retval) {
700 dev->interrupt_out_busy = 0;
Greg Kroah-Hartman9d974b22012-04-20 16:53:48 -0700701 dev_err(&dev->udev->dev,
702 "Couldn't submit interrupt_out_urb %d\n", retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 goto unlock_exit;
704 }
705 retval = bytes_to_write;
706
707unlock_exit:
708 /* unlock the device */
Daniel Walker18bcbcf2008-01-11 08:45:44 -0800709 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
711exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 return retval;
713}
714
715
716/**
717 * tower_interrupt_in_callback
718 */
David Howells7d12e782006-10-05 14:55:46 +0100719static void tower_interrupt_in_callback (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
Ming Leicdc97792008-02-24 18:41:47 +0800721 struct lego_usb_tower *dev = urb->context;
Greg Kroah-Hartman64771a02007-07-18 10:58:02 -0700722 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 int retval;
724
Greg Kroah-Hartman4dae9962013-06-26 16:30:41 -0700725 lego_usb_tower_debug_data(&dev->udev->dev, __func__,
726 urb->actual_length, urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Greg Kroah-Hartman64771a02007-07-18 10:58:02 -0700728 if (status) {
729 if (status == -ENOENT ||
730 status == -ECONNRESET ||
731 status == -ESHUTDOWN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 goto exit;
733 } else {
Greg Kroah-Hartmanfef526c2013-06-26 16:30:40 -0700734 dev_dbg(&dev->udev->dev,
735 "%s: nonzero status received: %d\n", __func__,
736 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 goto resubmit; /* maybe we can recover */
738 }
739 }
740
741 if (urb->actual_length > 0) {
742 spin_lock (&dev->read_buffer_lock);
743 if (dev->read_buffer_length + urb->actual_length < read_buffer_size) {
744 memcpy (dev->read_buffer + dev->read_buffer_length,
745 dev->interrupt_in_buffer,
746 urb->actual_length);
747 dev->read_buffer_length += urb->actual_length;
748 dev->read_last_arrival = jiffies;
Greg Kroah-Hartmanfef526c2013-06-26 16:30:40 -0700749 dev_dbg(&dev->udev->dev, "%s: received %d bytes\n",
750 __func__, urb->actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 } else {
Greg Kroah-Hartman38726bf2013-06-26 16:30:42 -0700752 pr_warn("read_buffer overflow, %d bytes dropped\n",
753 urb->actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
755 spin_unlock (&dev->read_buffer_lock);
756 }
757
758resubmit:
759 /* resubmit if we're still running */
760 if (dev->interrupt_in_running && dev->udev) {
761 retval = usb_submit_urb (dev->interrupt_in_urb, GFP_ATOMIC);
Greg Kroah-Hartman9d974b22012-04-20 16:53:48 -0700762 if (retval)
763 dev_err(&dev->udev->dev,
764 "%s: usb_submit_urb failed (%d)\n",
765 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 }
767
768exit:
769 dev->interrupt_in_done = 1;
770 wake_up_interruptible (&dev->read_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
772
773
774/**
775 * tower_interrupt_out_callback
776 */
David Howells7d12e782006-10-05 14:55:46 +0100777static void tower_interrupt_out_callback (struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
Ming Leicdc97792008-02-24 18:41:47 +0800779 struct lego_usb_tower *dev = urb->context;
Greg Kroah-Hartman64771a02007-07-18 10:58:02 -0700780 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Greg Kroah-Hartman4dae9962013-06-26 16:30:41 -0700782 lego_usb_tower_debug_data(&dev->udev->dev, __func__,
783 urb->actual_length, urb->transfer_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 /* sync/async unlink faults aren't errors */
Greg Kroah-Hartman64771a02007-07-18 10:58:02 -0700786 if (status && !(status == -ENOENT ||
787 status == -ECONNRESET ||
788 status == -ESHUTDOWN)) {
Greg Kroah-Hartmanfef526c2013-06-26 16:30:40 -0700789 dev_dbg(&dev->udev->dev,
790 "%s: nonzero write bulk status received: %d\n", __func__,
791 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 }
793
794 dev->interrupt_out_busy = 0;
795 wake_up_interruptible(&dev->write_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796}
797
798
799/**
800 * tower_probe
801 *
802 * Called by the usb core when a new device is connected that it thinks
803 * this driver might be interested in.
804 */
805static int tower_probe (struct usb_interface *interface, const struct usb_device_id *id)
806{
Greg Kroah-Hartman9d974b22012-04-20 16:53:48 -0700807 struct device *idev = &interface->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 struct usb_device *udev = interface_to_usbdev(interface);
809 struct lego_usb_tower *dev = NULL;
810 struct usb_host_interface *iface_desc;
811 struct usb_endpoint_descriptor* endpoint;
812 struct tower_get_version_reply get_version_reply;
813 int i;
814 int retval = -ENOMEM;
815 int result;
816
Steven Cole093cf722005-05-03 19:07:24 -0600817 /* allocate memory for our device state and initialize it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 dev = kmalloc (sizeof(struct lego_usb_tower), GFP_KERNEL);
820
821 if (dev == NULL) {
Greg Kroah-Hartman9d974b22012-04-20 16:53:48 -0700822 dev_err(idev, "Out of memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 goto exit;
824 }
825
Daniel Walker18bcbcf2008-01-11 08:45:44 -0800826 mutex_init(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
828 dev->udev = udev;
829 dev->open_count = 0;
830
831 dev->read_buffer = NULL;
832 dev->read_buffer_length = 0;
833 dev->read_packet_length = 0;
834 spin_lock_init (&dev->read_buffer_lock);
835 dev->packet_timeout_jiffies = packet_timeout * HZ / 1000;
836 dev->read_last_arrival = jiffies;
837
838 init_waitqueue_head (&dev->read_wait);
839 init_waitqueue_head (&dev->write_wait);
840
841 dev->interrupt_in_buffer = NULL;
842 dev->interrupt_in_endpoint = NULL;
843 dev->interrupt_in_urb = NULL;
844 dev->interrupt_in_running = 0;
845 dev->interrupt_in_done = 0;
846
847 dev->interrupt_out_buffer = NULL;
848 dev->interrupt_out_endpoint = NULL;
849 dev->interrupt_out_urb = NULL;
850 dev->interrupt_out_busy = 0;
851
852 iface_desc = interface->cur_altsetting;
853
854 /* set up the endpoint information */
855 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
856 endpoint = &iface_desc->endpoint[i].desc;
857
Luiz Fernando N. Capitulino240661c2006-10-26 13:02:54 -0300858 if (usb_endpoint_xfer_int(endpoint)) {
859 if (usb_endpoint_dir_in(endpoint))
860 dev->interrupt_in_endpoint = endpoint;
861 else
862 dev->interrupt_out_endpoint = endpoint;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
864 }
865 if(dev->interrupt_in_endpoint == NULL) {
Greg Kroah-Hartman9d974b22012-04-20 16:53:48 -0700866 dev_err(idev, "interrupt in endpoint not found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 goto error;
868 }
869 if (dev->interrupt_out_endpoint == NULL) {
Greg Kroah-Hartman9d974b22012-04-20 16:53:48 -0700870 dev_err(idev, "interrupt out endpoint not found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 goto error;
872 }
873
874 dev->read_buffer = kmalloc (read_buffer_size, GFP_KERNEL);
875 if (!dev->read_buffer) {
Greg Kroah-Hartman9d974b22012-04-20 16:53:48 -0700876 dev_err(idev, "Couldn't allocate read_buffer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 goto error;
878 }
Kuninori Morimoto29cc8892011-08-23 03:12:03 -0700879 dev->interrupt_in_buffer = kmalloc (usb_endpoint_maxp(dev->interrupt_in_endpoint), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 if (!dev->interrupt_in_buffer) {
Greg Kroah-Hartman9d974b22012-04-20 16:53:48 -0700881 dev_err(idev, "Couldn't allocate interrupt_in_buffer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 goto error;
883 }
884 dev->interrupt_in_urb = usb_alloc_urb(0, GFP_KERNEL);
885 if (!dev->interrupt_in_urb) {
Greg Kroah-Hartman9d974b22012-04-20 16:53:48 -0700886 dev_err(idev, "Couldn't allocate interrupt_in_urb\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 goto error;
888 }
889 dev->interrupt_out_buffer = kmalloc (write_buffer_size, GFP_KERNEL);
890 if (!dev->interrupt_out_buffer) {
Greg Kroah-Hartman9d974b22012-04-20 16:53:48 -0700891 dev_err(idev, "Couldn't allocate interrupt_out_buffer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 goto error;
893 }
894 dev->interrupt_out_urb = usb_alloc_urb(0, GFP_KERNEL);
895 if (!dev->interrupt_out_urb) {
Greg Kroah-Hartman9d974b22012-04-20 16:53:48 -0700896 dev_err(idev, "Couldn't allocate interrupt_out_urb\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 goto error;
898 }
899 dev->interrupt_in_interval = interrupt_in_interval ? interrupt_in_interval : dev->interrupt_in_endpoint->bInterval;
900 dev->interrupt_out_interval = interrupt_out_interval ? interrupt_out_interval : dev->interrupt_out_endpoint->bInterval;
901
902 /* we can register the device now, as it is ready */
903 usb_set_intfdata (interface, dev);
904
905 retval = usb_register_dev (interface, &tower_class);
906
907 if (retval) {
908 /* something prevented us from registering this driver */
Greg Kroah-Hartman9d974b22012-04-20 16:53:48 -0700909 dev_err(idev, "Not able to get a minor for this device.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 usb_set_intfdata (interface, NULL);
911 goto error;
912 }
913 dev->minor = interface->minor;
914
915 /* let the user know what node this device is now attached to */
Greg Kroah-Hartman1b29a372008-08-18 13:21:04 -0700916 dev_info(&interface->dev, "LEGO USB Tower #%d now attached to major "
917 "%d minor %d\n", (dev->minor - LEGO_USB_TOWER_MINOR_BASE),
918 USB_MAJOR, dev->minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 /* get the firmware version and log it */
921 result = usb_control_msg (udev,
922 usb_rcvctrlpipe(udev, 0),
923 LEGO_USB_TOWER_REQUEST_GET_VERSION,
924 USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_DEVICE,
925 0,
926 0,
927 &get_version_reply,
928 sizeof(get_version_reply),
929 1000);
930 if (result < 0) {
Greg Kroah-Hartman9d974b22012-04-20 16:53:48 -0700931 dev_err(idev, "LEGO USB Tower get version control request failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 retval = result;
933 goto error;
934 }
Greg Kroah-Hartman1b29a372008-08-18 13:21:04 -0700935 dev_info(&interface->dev, "LEGO USB Tower firmware version is %d.%d "
936 "build %d\n", get_version_reply.major,
937 get_version_reply.minor,
938 le16_to_cpu(get_version_reply.build_no));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940
941exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 return retval;
943
944error:
945 tower_delete(dev);
946 return retval;
947}
948
949
950/**
951 * tower_disconnect
952 *
953 * Called by the usb core when the device is removed from the system.
954 */
955static void tower_disconnect (struct usb_interface *interface)
956{
957 struct lego_usb_tower *dev;
958 int minor;
959
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 dev = usb_get_intfdata (interface);
Oliver Neukum78663ec2007-10-25 15:48:39 +0200961 mutex_lock(&open_disc_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 usb_set_intfdata (interface, NULL);
963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 minor = dev->minor;
965
966 /* give back our minor */
967 usb_deregister_dev (interface, &tower_class);
968
Daniel Walker18bcbcf2008-01-11 08:45:44 -0800969 mutex_lock(&dev->lock);
Oliver Neukum78663ec2007-10-25 15:48:39 +0200970 mutex_unlock(&open_disc_mutex);
Alan Sternd4ead162007-05-22 11:46:41 -0400971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 /* if the device is not opened, then we clean up right now */
973 if (!dev->open_count) {
Daniel Walker18bcbcf2008-01-11 08:45:44 -0800974 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 tower_delete (dev);
976 } else {
977 dev->udev = NULL;
Oliver Neukumd35b4ce2009-07-02 12:07:07 +0200978 /* wake up pollers */
979 wake_up_interruptible_all(&dev->read_wait);
980 wake_up_interruptible_all(&dev->write_wait);
Daniel Walker18bcbcf2008-01-11 08:45:44 -0800981 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 }
983
Greg Kroah-Hartman1b29a372008-08-18 13:21:04 -0700984 dev_info(&interface->dev, "LEGO USB Tower #%d now disconnected\n",
985 (minor - LEGO_USB_TOWER_MINOR_BASE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986}
987
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -0800988module_usb_driver(tower_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990MODULE_AUTHOR(DRIVER_AUTHOR);
991MODULE_DESCRIPTION(DRIVER_DESC);
992#ifdef MODULE_LICENSE
993MODULE_LICENSE("GPL");
994#endif