aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/serial/opticon.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/usb/serial/opticon.c')
-rw-r--r--drivers/usb/serial/opticon.c215
1 files changed, 212 insertions, 3 deletions
diff --git a/drivers/usb/serial/opticon.c b/drivers/usb/serial/opticon.c
index cea326f1f10..839583dc8b6 100644
--- a/drivers/usb/serial/opticon.c
+++ b/drivers/usb/serial/opticon.c
@@ -1,8 +1,8 @@
1/* 1/*
2 * Opticon USB barcode to serial driver 2 * Opticon USB barcode to serial driver
3 * 3 *
4 * Copyright (C) 2008 Greg Kroah-Hartman <gregkh@suse.de> 4 * Copyright (C) 2008 - 2009 Greg Kroah-Hartman <gregkh@suse.de>
5 * Copyright (C) 2008 Novell Inc. 5 * Copyright (C) 2008 - 2009 Novell Inc.
6 * 6 *
7 * This program is free software; you can redistribute it and/or 7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version 8 * modify it under the terms of the GNU General Public License version
@@ -14,6 +14,7 @@
14#include <linux/tty.h> 14#include <linux/tty.h>
15#include <linux/tty_driver.h> 15#include <linux/tty_driver.h>
16#include <linux/tty_flip.h> 16#include <linux/tty_flip.h>
17#include <linux/serial.h>
17#include <linux/module.h> 18#include <linux/module.h>
18#include <linux/usb.h> 19#include <linux/usb.h>
19#include <linux/usb/serial.h> 20#include <linux/usb/serial.h>
@@ -40,8 +41,12 @@ struct opticon_private {
40 bool throttled; 41 bool throttled;
41 bool actually_throttled; 42 bool actually_throttled;
42 bool rts; 43 bool rts;
44 int outstanding_urbs;
43}; 45};
44 46
47/* max number of write urbs in flight */
48#define URB_UPPER_LIMIT 4
49
45static void opticon_bulk_callback(struct urb *urb) 50static void opticon_bulk_callback(struct urb *urb)
46{ 51{
47 struct opticon_private *priv = urb->context; 52 struct opticon_private *priv = urb->context;
@@ -106,7 +111,6 @@ static void opticon_bulk_callback(struct urb *urb)
106 priv->rts = false; 111 priv->rts = false;
107 else 112 else
108 priv->rts = true; 113 priv->rts = true;
109 /* FIXME change the RTS level */
110 } else { 114 } else {
111 dev_dbg(&priv->udev->dev, 115 dev_dbg(&priv->udev->dev,
112 "Unknown data packet received from the device:" 116 "Unknown data packet received from the device:"
@@ -188,6 +192,120 @@ static void opticon_close(struct tty_struct *tty, struct usb_serial_port *port,
188 usb_kill_urb(priv->bulk_read_urb); 192 usb_kill_urb(priv->bulk_read_urb);
189} 193}
190 194
195static void opticon_write_bulk_callback(struct urb *urb)
196{
197 struct opticon_private *priv = urb->context;
198 int status = urb->status;
199 unsigned long flags;
200
201 /* free up the transfer buffer, as usb_free_urb() does not do this */
202 kfree(urb->transfer_buffer);
203
204 if (status)
205 dbg("%s - nonzero write bulk status received: %d",
206 __func__, status);
207
208 spin_lock_irqsave(&priv->lock, flags);
209 --priv->outstanding_urbs;
210 spin_unlock_irqrestore(&priv->lock, flags);
211
212 usb_serial_port_softint(priv->port);
213}
214
215static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port,
216 const unsigned char *buf, int count)
217{
218 struct opticon_private *priv = usb_get_serial_data(port->serial);
219 struct usb_serial *serial = port->serial;
220 struct urb *urb;
221 unsigned char *buffer;
222 unsigned long flags;
223 int status;
224
225 dbg("%s - port %d", __func__, port->number);
226
227 spin_lock_irqsave(&priv->lock, flags);
228 if (priv->outstanding_urbs > URB_UPPER_LIMIT) {
229 spin_unlock_irqrestore(&priv->lock, flags);
230 dbg("%s - write limit hit\n", __func__);
231 return 0;
232 }
233 priv->outstanding_urbs++;
234 spin_unlock_irqrestore(&priv->lock, flags);
235
236 buffer = kmalloc(count, GFP_ATOMIC);
237 if (!buffer) {
238 dev_err(&port->dev, "out of memory\n");
239 count = -ENOMEM;
240 goto error_no_buffer;
241 }
242
243 urb = usb_alloc_urb(0, GFP_ATOMIC);
244 if (!urb) {
245 dev_err(&port->dev, "no more free urbs\n");
246 count = -ENOMEM;
247 goto error_no_urb;
248 }
249
250 memcpy(buffer, buf, count);
251
252 usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
253
254 usb_fill_bulk_urb(urb, serial->dev,
255 usb_sndbulkpipe(serial->dev,
256 port->bulk_out_endpointAddress),
257 buffer, count, opticon_write_bulk_callback, priv);
258
259 /* send it down the pipe */
260 status = usb_submit_urb(urb, GFP_ATOMIC);
261 if (status) {
262 dev_err(&port->dev,
263 "%s - usb_submit_urb(write bulk) failed with status = %d\n",
264 __func__, status);
265 count = status;
266 goto error;
267 }
268
269 /* we are done with this urb, so let the host driver
270 * really free it when it is finished with it */
271 usb_free_urb(urb);
272
273 return count;
274error:
275 usb_free_urb(urb);
276error_no_urb:
277 kfree(buffer);
278error_no_buffer:
279 spin_lock_irqsave(&priv->lock, flags);
280 --priv->outstanding_urbs;
281 spin_unlock_irqrestore(&priv->lock, flags);
282 return count;
283}
284
285static int opticon_write_room(struct tty_struct *tty)
286{
287 struct usb_serial_port *port = tty->driver_data;
288 struct opticon_private *priv = usb_get_serial_data(port->serial);
289 unsigned long flags;
290
291 dbg("%s - port %d", __func__, port->number);
292
293 /*
294 * We really can take almost anything the user throws at us
295 * but let's pick a nice big number to tell the tty
296 * layer that we have lots of free space, unless we don't.
297 */
298 spin_lock_irqsave(&priv->lock, flags);
299 if (priv->outstanding_urbs > URB_UPPER_LIMIT * 2 / 3) {
300 spin_unlock_irqrestore(&priv->lock, flags);
301 dbg("%s - write limit hit\n", __func__);
302 return 0;
303 }
304 spin_unlock_irqrestore(&priv->lock, flags);
305
306 return 2048;
307}
308
191static void opticon_throttle(struct tty_struct *tty) 309static void opticon_throttle(struct tty_struct *tty)
192{ 310{
193 struct usb_serial_port *port = tty->driver_data; 311 struct usb_serial_port *port = tty->driver_data;
@@ -223,6 +341,67 @@ static void opticon_unthrottle(struct tty_struct *tty)
223 __func__, result); 341 __func__, result);
224} 342}
225 343
344static int opticon_tiocmget(struct tty_struct *tty, struct file *file)
345{
346 struct usb_serial_port *port = tty->driver_data;
347 struct opticon_private *priv = usb_get_serial_data(port->serial);
348 unsigned long flags;
349 int result = 0;
350
351 dbg("%s - port %d", __func__, port->number);
352
353 spin_lock_irqsave(&priv->lock, flags);
354 if (priv->rts)
355 result = TIOCM_RTS;
356 spin_unlock_irqrestore(&priv->lock, flags);
357
358 dbg("%s - %x", __func__, result);
359 return result;
360}
361
362static int get_serial_info(struct opticon_private *priv,
363 struct serial_struct __user *serial)
364{
365 struct serial_struct tmp;
366
367 if (!serial)
368 return -EFAULT;
369
370 memset(&tmp, 0x00, sizeof(tmp));
371
372 /* fake emulate a 16550 uart to make userspace code happy */
373 tmp.type = PORT_16550A;
374 tmp.line = priv->serial->minor;
375 tmp.port = 0;
376 tmp.irq = 0;
377 tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
378 tmp.xmit_fifo_size = 1024;
379 tmp.baud_base = 9600;
380 tmp.close_delay = 5*HZ;
381 tmp.closing_wait = 30*HZ;
382
383 if (copy_to_user(serial, &tmp, sizeof(*serial)))
384 return -EFAULT;
385 return 0;
386}
387
388static int opticon_ioctl(struct tty_struct *tty, struct file *file,
389 unsigned int cmd, unsigned long arg)
390{
391 struct usb_serial_port *port = tty->driver_data;
392 struct opticon_private *priv = usb_get_serial_data(port->serial);
393
394 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
395
396 switch (cmd) {
397 case TIOCGSERIAL:
398 return get_serial_info(priv,
399 (struct serial_struct __user *)arg);
400 }
401
402 return -ENOIOCTLCMD;
403}
404
226static int opticon_startup(struct usb_serial *serial) 405static int opticon_startup(struct usb_serial *serial)
227{ 406{
228 struct opticon_private *priv; 407 struct opticon_private *priv;
@@ -306,11 +485,37 @@ static void opticon_shutdown(struct usb_serial *serial)
306 usb_set_serial_data(serial, NULL); 485 usb_set_serial_data(serial, NULL);
307} 486}
308 487
488static int opticon_suspend(struct usb_interface *intf, pm_message_t message)
489{
490 struct usb_serial *serial = usb_get_intfdata(intf);
491 struct opticon_private *priv = usb_get_serial_data(serial);
492
493 usb_kill_urb(priv->bulk_read_urb);
494 return 0;
495}
496
497static int opticon_resume(struct usb_interface *intf)
498{
499 struct usb_serial *serial = usb_get_intfdata(intf);
500 struct opticon_private *priv = usb_get_serial_data(serial);
501 struct usb_serial_port *port = serial->port[0];
502 int result;
503
504 mutex_lock(&port->mutex);
505 if (port->port.count)
506 result = usb_submit_urb(priv->bulk_read_urb, GFP_NOIO);
507 else
508 result = 0;
509 mutex_unlock(&port->mutex);
510 return result;
511}
309 512
310static struct usb_driver opticon_driver = { 513static struct usb_driver opticon_driver = {
311 .name = "opticon", 514 .name = "opticon",
312 .probe = usb_serial_probe, 515 .probe = usb_serial_probe,
313 .disconnect = usb_serial_disconnect, 516 .disconnect = usb_serial_disconnect,
517 .suspend = opticon_suspend,
518 .resume = opticon_resume,
314 .id_table = id_table, 519 .id_table = id_table,
315 .no_dynamic_id = 1, 520 .no_dynamic_id = 1,
316}; 521};
@@ -326,9 +531,13 @@ static struct usb_serial_driver opticon_device = {
326 .attach = opticon_startup, 531 .attach = opticon_startup,
327 .open = opticon_open, 532 .open = opticon_open,
328 .close = opticon_close, 533 .close = opticon_close,
534 .write = opticon_write,
535 .write_room = opticon_write_room,
329 .shutdown = opticon_shutdown, 536 .shutdown = opticon_shutdown,
330 .throttle = opticon_throttle, 537 .throttle = opticon_throttle,
331 .unthrottle = opticon_unthrottle, 538 .unthrottle = opticon_unthrottle,
539 .ioctl = opticon_ioctl,
540 .tiocmget = opticon_tiocmget,
332}; 541};
333 542
334static int __init opticon_init(void) 543static int __init opticon_init(void)