Linux Audio

Check our new training course

Loading...
v3.1
  1/*
  2 * Opticon USB barcode to serial driver
  3 *
  4 * Copyright (C) 2011 Martin Jansen <martin.jansen@opticon.com>
  5 * Copyright (C) 2008 - 2009 Greg Kroah-Hartman <gregkh@suse.de>
  6 * Copyright (C) 2008 - 2009 Novell Inc.
  7 *
  8 *	This program is free software; you can redistribute it and/or
  9 *	modify it under the terms of the GNU General Public License version
 10 *	2 as published by the Free Software Foundation.
 11 */
 12
 13#include <linux/kernel.h>
 14#include <linux/init.h>
 15#include <linux/tty.h>
 16#include <linux/tty_driver.h>
 17#include <linux/slab.h>
 18#include <linux/tty_flip.h>
 19#include <linux/serial.h>
 20#include <linux/module.h>
 21#include <linux/usb.h>
 22#include <linux/usb/serial.h>
 23#include <linux/uaccess.h>
 24
 25#define CONTROL_RTS			0x02
 26#define RESEND_CTS_STATE	0x03
 27
 28/* max number of write urbs in flight */
 29#define URB_UPPER_LIMIT	8
 30
 31/* This driver works for the Opticon 1D barcode reader
 32 * an examples of 1D barcode types are EAN, UPC, Code39, IATA etc.. */
 33#define DRIVER_DESC	"Opticon USB barcode to serial driver (1D)"
 34
 35static int debug;
 36
 37static const struct usb_device_id id_table[] = {
 38	{ USB_DEVICE(0x065a, 0x0009) },
 39	{ },
 40};
 41MODULE_DEVICE_TABLE(usb, id_table);
 42
 43/* This structure holds all of the individual device information */
 44struct opticon_private {
 45	struct usb_device *udev;
 46	struct usb_serial *serial;
 47	struct usb_serial_port *port;
 48	unsigned char *bulk_in_buffer;
 49	struct urb *bulk_read_urb;
 50	int buffer_size;
 51	u8 bulk_address;
 52	spinlock_t lock;	/* protects the following flags */
 53	bool throttled;
 54	bool actually_throttled;
 55	bool rts;
 56	bool cts;
 57	int outstanding_urbs;
 58};
 59
 60
 61
 62static void opticon_read_bulk_callback(struct urb *urb)
 63{
 64	struct opticon_private *priv = urb->context;
 65	unsigned char *data = urb->transfer_buffer;
 66	struct usb_serial_port *port = priv->port;
 67	int status = urb->status;
 68	struct tty_struct *tty;
 69	int result;
 70	int data_length;
 71	unsigned long flags;
 72
 73	dbg("%s - port %d", __func__, port->number);
 74
 75	switch (status) {
 76	case 0:
 77		/* success */
 78		break;
 79	case -ECONNRESET:
 80	case -ENOENT:
 81	case -ESHUTDOWN:
 82		/* this urb is terminated, clean up */
 83		dbg("%s - urb shutting down with status: %d",
 84		    __func__, status);
 85		return;
 86	default:
 87		dbg("%s - nonzero urb status received: %d",
 88		    __func__, status);
 89		goto exit;
 90	}
 91
 92	usb_serial_debug_data(debug, &port->dev, __func__, urb->actual_length,
 93			      data);
 94
 95	if (urb->actual_length > 2) {
 96		data_length = urb->actual_length - 2;
 97
 98		/*
 99		 * Data from the device comes with a 2 byte header:
100		 *
101		 * <0x00><0x00>data...
102		 *	This is real data to be sent to the tty layer
103		 * <0x00><0x01)level
104		 *	This is a CTS level change, the third byte is the CTS
105		 *	value (0 for low, 1 for high).
106		 */
107		if ((data[0] == 0x00) && (data[1] == 0x00)) {
108			/* real data, send it to the tty layer */
109			tty = tty_port_tty_get(&port->port);
110			if (tty) {
111				tty_insert_flip_string(tty, data + 2,
112						       data_length);
113				tty_flip_buffer_push(tty);
114				tty_kref_put(tty);
115			}
116		} else {
117			if ((data[0] == 0x00) && (data[1] == 0x01)) {
118				spin_lock_irqsave(&priv->lock, flags);
119				/* CTS status information package */
120				if (data[2] == 0x00)
121					priv->cts = false;
122				else
123					priv->cts = true;
124				spin_unlock_irqrestore(&priv->lock, flags);
125			} else {
126				dev_dbg(&priv->udev->dev,
127					"Unknown data packet received from the device:"
128					" %2x %2x\n",
129					data[0], data[1]);
130			}
131		}
132	} else {
133		dev_dbg(&priv->udev->dev,
134			"Improper amount of data received from the device, "
135			"%d bytes", urb->actual_length);
136	}
137
138exit:
139	spin_lock(&priv->lock);
140
141	/* Continue trying to always read if we should */
142	if (!priv->throttled) {
143		usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev,
144				  usb_rcvbulkpipe(priv->udev,
145						  priv->bulk_address),
146				  priv->bulk_in_buffer, priv->buffer_size,
147				  opticon_read_bulk_callback, priv);
148		result = usb_submit_urb(priv->bulk_read_urb, GFP_ATOMIC);
149		if (result)
150			dev_err(&port->dev,
151			    "%s - failed resubmitting read urb, error %d\n",
152							__func__, result);
153	} else
154		priv->actually_throttled = true;
155	spin_unlock(&priv->lock);
156}
157
158static int send_control_msg(struct usb_serial_port *port, u8 requesttype,
159				u8 val)
160{
161	struct usb_serial *serial = port->serial;
162	int retval;
163	u8 buffer[2];
164
165	buffer[0] = val;
166	/* Send the message to the vendor control endpoint
167	 * of the connected device */
168	retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
169				requesttype,
170				USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
171				0, 0, buffer, 1, 0);
172
173	return retval;
174}
175
176static int opticon_open(struct tty_struct *tty, struct usb_serial_port *port)
177{
178	struct opticon_private *priv = usb_get_serial_data(port->serial);
179	unsigned long flags;
180	int result = 0;
181
182	dbg("%s - port %d", __func__, port->number);
183
184	spin_lock_irqsave(&priv->lock, flags);
185	priv->throttled = false;
186	priv->actually_throttled = false;
187	priv->port = port;
188	priv->rts = false;
189	spin_unlock_irqrestore(&priv->lock, flags);
190
191	/* Clear RTS line */
192	send_control_msg(port, CONTROL_RTS, 0);
193
194	/* Setup the read URB and start reading from the device */
195	usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev,
196			  usb_rcvbulkpipe(priv->udev,
197					  priv->bulk_address),
198			  priv->bulk_in_buffer, priv->buffer_size,
199			  opticon_read_bulk_callback, priv);
200
201	/* clear the halt status of the enpoint */
202	usb_clear_halt(priv->udev, priv->bulk_read_urb->pipe);
203
204	result = usb_submit_urb(priv->bulk_read_urb, GFP_KERNEL);
205	if (result)
206		dev_err(&port->dev,
207			"%s - failed resubmitting read urb, error %d\n",
208			__func__, result);
209	/* Request CTS line state, sometimes during opening the current
210	 * CTS state can be missed. */
211	send_control_msg(port, RESEND_CTS_STATE, 1);
212	return result;
213}
214
215static void opticon_close(struct usb_serial_port *port)
216{
217	struct opticon_private *priv = usb_get_serial_data(port->serial);
218
219	dbg("%s - port %d", __func__, port->number);
220
221	/* shutdown our urbs */
222	usb_kill_urb(priv->bulk_read_urb);
223}
224
225static void opticon_write_control_callback(struct urb *urb)
226{
227	struct opticon_private *priv = urb->context;
228	int status = urb->status;
229	unsigned long flags;
230
231	/* free up the transfer buffer, as usb_free_urb() does not do this */
232	kfree(urb->transfer_buffer);
233
234	/* setup packet may be set if we're using it for writing */
235	kfree(urb->setup_packet);
236
237	if (status)
238		dbg("%s - nonzero write bulk status received: %d",
239		    __func__, status);
240
241	spin_lock_irqsave(&priv->lock, flags);
242	--priv->outstanding_urbs;
243	spin_unlock_irqrestore(&priv->lock, flags);
244
245	usb_serial_port_softint(priv->port);
246}
247
248static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port,
249			 const unsigned char *buf, int count)
250{
251	struct opticon_private *priv = usb_get_serial_data(port->serial);
252	struct usb_serial *serial = port->serial;
253	struct urb *urb;
254	unsigned char *buffer;
255	unsigned long flags;
256	int status;
257	struct usb_ctrlrequest *dr;
258
259	dbg("%s - port %d", __func__, port->number);
260
261	spin_lock_irqsave(&priv->lock, flags);
262	if (priv->outstanding_urbs > URB_UPPER_LIMIT) {
263		spin_unlock_irqrestore(&priv->lock, flags);
264		dbg("%s - write limit hit", __func__);
265		return 0;
266	}
267	priv->outstanding_urbs++;
268	spin_unlock_irqrestore(&priv->lock, flags);
269
270	buffer = kmalloc(count, GFP_ATOMIC);
271	if (!buffer) {
272		dev_err(&port->dev, "out of memory\n");
273		count = -ENOMEM;
274
275		goto error_no_buffer;
276	}
277
278	urb = usb_alloc_urb(0, GFP_ATOMIC);
279	if (!urb) {
280		dev_err(&port->dev, "no more free urbs\n");
281		count = -ENOMEM;
282		goto error_no_urb;
283	}
284
285	memcpy(buffer, buf, count);
286
287	usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
288
289	/* The conncected devices do not have a bulk write endpoint,
290	 * to transmit data to de barcode device the control endpoint is used */
291	dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_NOIO);
292	if (!dr) {
293		dev_err(&port->dev, "out of memory\n");
294		count = -ENOMEM;
295		goto error;
296	}
297
298	dr->bRequestType = USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT;
299	dr->bRequest = 0x01;
300	dr->wValue = 0;
301	dr->wIndex = 0;
302	dr->wLength = cpu_to_le16(count);
303
304	usb_fill_control_urb(urb, serial->dev,
305		usb_sndctrlpipe(serial->dev, 0),
306		(unsigned char *)dr, buffer, count,
307		opticon_write_control_callback, priv);
308
309	/* send it down the pipe */
310	status = usb_submit_urb(urb, GFP_ATOMIC);
311	if (status) {
312		dev_err(&port->dev,
313		"%s - usb_submit_urb(write endpoint) failed status = %d\n",
314							__func__, status);
315		count = status;
316		goto error;
317	}
318
319	/* we are done with this urb, so let the host driver
320	 * really free it when it is finished with it */
321	usb_free_urb(urb);
322
323	return count;
324error:
325	usb_free_urb(urb);
326error_no_urb:
327	kfree(buffer);
328error_no_buffer:
329	spin_lock_irqsave(&priv->lock, flags);
330	--priv->outstanding_urbs;
331	spin_unlock_irqrestore(&priv->lock, flags);
332	return count;
333}
334
335static int opticon_write_room(struct tty_struct *tty)
336{
337	struct usb_serial_port *port = tty->driver_data;
338	struct opticon_private *priv = usb_get_serial_data(port->serial);
339	unsigned long flags;
340
341	dbg("%s - port %d", __func__, port->number);
342
343	/*
344	 * We really can take almost anything the user throws at us
345	 * but let's pick a nice big number to tell the tty
346	 * layer that we have lots of free space, unless we don't.
347	 */
348	spin_lock_irqsave(&priv->lock, flags);
349	if (priv->outstanding_urbs > URB_UPPER_LIMIT * 2 / 3) {
350		spin_unlock_irqrestore(&priv->lock, flags);
351		dbg("%s - write limit hit", __func__);
352		return 0;
353	}
354	spin_unlock_irqrestore(&priv->lock, flags);
355
356	return 2048;
357}
358
359static void opticon_throttle(struct tty_struct *tty)
360{
361	struct usb_serial_port *port = tty->driver_data;
362	struct opticon_private *priv = usb_get_serial_data(port->serial);
363	unsigned long flags;
364
365	dbg("%s - port %d", __func__, port->number);
366	spin_lock_irqsave(&priv->lock, flags);
367	priv->throttled = true;
368	spin_unlock_irqrestore(&priv->lock, flags);
369}
370
371
372static void opticon_unthrottle(struct tty_struct *tty)
373{
374	struct usb_serial_port *port = tty->driver_data;
375	struct opticon_private *priv = usb_get_serial_data(port->serial);
376	unsigned long flags;
377	int result, was_throttled;
378
379	dbg("%s - port %d", __func__, port->number);
380
381	spin_lock_irqsave(&priv->lock, flags);
382	priv->throttled = false;
383	was_throttled = priv->actually_throttled;
384	priv->actually_throttled = false;
385	spin_unlock_irqrestore(&priv->lock, flags);
386
387	priv->bulk_read_urb->dev = port->serial->dev;
388	if (was_throttled) {
389		result = usb_submit_urb(priv->bulk_read_urb, GFP_ATOMIC);
390		if (result)
391			dev_err(&port->dev,
392				"%s - failed submitting read urb, error %d\n",
393							__func__, result);
394	}
395}
396
397static int opticon_tiocmget(struct tty_struct *tty)
398{
399	struct usb_serial_port *port = tty->driver_data;
400	struct opticon_private *priv = usb_get_serial_data(port->serial);
401	unsigned long flags;
402	int result = 0;
403
404	dbg("%s - port %d", __func__, port->number);
405	if (!usb_get_intfdata(port->serial->interface))
406		return -ENODEV;
407
408	spin_lock_irqsave(&priv->lock, flags);
409	if (priv->rts)
410		result |= TIOCM_RTS;
411	if (priv->cts)
412		result |= TIOCM_CTS;
413	spin_unlock_irqrestore(&priv->lock, flags);
414
415	dbg("%s - %x", __func__, result);
416	return result;
417}
418
419static int opticon_tiocmset(struct tty_struct *tty,
420			   unsigned int set, unsigned int clear)
421{
422	struct usb_serial_port *port = tty->driver_data;
 
423	struct opticon_private *priv = usb_get_serial_data(port->serial);
424	unsigned long flags;
425	bool rts;
426	bool changed = false;
 
427
428	if (!usb_get_intfdata(port->serial->interface))
429		return -ENODEV;
430	/* We only support RTS so we only handle that */
431	spin_lock_irqsave(&priv->lock, flags);
432
433	rts = priv->rts;
434	if (set & TIOCM_RTS)
435		priv->rts = true;
436	if (clear & TIOCM_RTS)
437		priv->rts = false;
438	changed = rts ^ priv->rts;
439	spin_unlock_irqrestore(&priv->lock, flags);
440
441	if (!changed)
442		return 0;
443
444	/* Send the new RTS state to the connected device */
445	return send_control_msg(port, CONTROL_RTS, !rts);
 
 
 
 
 
 
 
446}
447
448static int get_serial_info(struct opticon_private *priv,
449			   struct serial_struct __user *serial)
450{
451	struct serial_struct tmp;
452
453	if (!serial)
454		return -EFAULT;
455
456	memset(&tmp, 0x00, sizeof(tmp));
457
458	/* fake emulate a 16550 uart to make userspace code happy */
459	tmp.type		= PORT_16550A;
460	tmp.line		= priv->serial->minor;
461	tmp.port		= 0;
462	tmp.irq			= 0;
463	tmp.flags		= ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
464	tmp.xmit_fifo_size	= 1024;
465	tmp.baud_base		= 9600;
466	tmp.close_delay		= 5*HZ;
467	tmp.closing_wait	= 30*HZ;
468
469	if (copy_to_user(serial, &tmp, sizeof(*serial)))
470		return -EFAULT;
471	return 0;
472}
473
474static int opticon_ioctl(struct tty_struct *tty,
475			 unsigned int cmd, unsigned long arg)
476{
477	struct usb_serial_port *port = tty->driver_data;
478	struct opticon_private *priv = usb_get_serial_data(port->serial);
479
480	dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
481
482	switch (cmd) {
483	case TIOCGSERIAL:
484		return get_serial_info(priv,
485				       (struct serial_struct __user *)arg);
486	}
487
488	return -ENOIOCTLCMD;
489}
490
491static int opticon_startup(struct usb_serial *serial)
492{
493	struct opticon_private *priv;
494	struct usb_host_interface *intf;
495	int i;
496	int retval = -ENOMEM;
497	bool bulk_in_found = false;
498
499	/* create our private serial structure */
500	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
501	if (priv == NULL) {
502		dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
503		return -ENOMEM;
504	}
505	spin_lock_init(&priv->lock);
506	priv->serial = serial;
507	priv->port = serial->port[0];
508	priv->udev = serial->dev;
509	priv->outstanding_urbs = 0;	/* Init the outstanding urbs */
510
511	/* find our bulk endpoint */
512	intf = serial->interface->altsetting;
513	for (i = 0; i < intf->desc.bNumEndpoints; ++i) {
514		struct usb_endpoint_descriptor *endpoint;
515
516		endpoint = &intf->endpoint[i].desc;
517		if (!usb_endpoint_is_bulk_in(endpoint))
518			continue;
519
520		priv->bulk_read_urb = usb_alloc_urb(0, GFP_KERNEL);
521		if (!priv->bulk_read_urb) {
522			dev_err(&priv->udev->dev, "out of memory\n");
523			goto error;
524		}
525
526		priv->buffer_size = le16_to_cpu(endpoint->wMaxPacketSize) * 2;
527		priv->bulk_in_buffer = kmalloc(priv->buffer_size, GFP_KERNEL);
528		if (!priv->bulk_in_buffer) {
529			dev_err(&priv->udev->dev, "out of memory\n");
530			goto error;
531		}
532
533		priv->bulk_address = endpoint->bEndpointAddress;
534
535		bulk_in_found = true;
536		break;
537		}
538
539	if (!bulk_in_found) {
540		dev_err(&priv->udev->dev,
541			"Error - the proper endpoints were not found!\n");
542		goto error;
543	}
544
545	usb_set_serial_data(serial, priv);
546	return 0;
547
548error:
549	usb_free_urb(priv->bulk_read_urb);
550	kfree(priv->bulk_in_buffer);
551	kfree(priv);
552	return retval;
553}
554
555static void opticon_disconnect(struct usb_serial *serial)
556{
557	struct opticon_private *priv = usb_get_serial_data(serial);
558
559	dbg("%s", __func__);
560
561	usb_kill_urb(priv->bulk_read_urb);
562	usb_free_urb(priv->bulk_read_urb);
563}
564
565static void opticon_release(struct usb_serial *serial)
566{
567	struct opticon_private *priv = usb_get_serial_data(serial);
568
569	dbg("%s", __func__);
570
571	kfree(priv->bulk_in_buffer);
572	kfree(priv);
573}
574
575static int opticon_suspend(struct usb_interface *intf, pm_message_t message)
576{
577	struct usb_serial *serial = usb_get_intfdata(intf);
578	struct opticon_private *priv = usb_get_serial_data(serial);
579
580	usb_kill_urb(priv->bulk_read_urb);
581	return 0;
582}
583
584static int opticon_resume(struct usb_interface *intf)
585{
586	struct usb_serial *serial = usb_get_intfdata(intf);
587	struct opticon_private *priv = usb_get_serial_data(serial);
588	struct usb_serial_port *port = serial->port[0];
589	int result;
590
591	mutex_lock(&port->port.mutex);
592	/* This is protected by the port mutex against close/open */
593	if (test_bit(ASYNCB_INITIALIZED, &port->port.flags))
594		result = usb_submit_urb(priv->bulk_read_urb, GFP_NOIO);
595	else
596		result = 0;
597	mutex_unlock(&port->port.mutex);
598	return result;
599}
600
601static struct usb_driver opticon_driver = {
602	.name =		"opticon",
603	.probe =	usb_serial_probe,
604	.disconnect =	usb_serial_disconnect,
605	.suspend =	opticon_suspend,
606	.resume =	opticon_resume,
607	.id_table =	id_table,
608	.no_dynamic_id = 	1,
609};
610
611static struct usb_serial_driver opticon_device = {
612	.driver = {
613		.owner =	THIS_MODULE,
614		.name =		"opticon",
615	},
616	.id_table =		id_table,
617	.usb_driver = 		&opticon_driver,
618	.num_ports =		1,
619	.attach =		opticon_startup,
620	.open =			opticon_open,
621	.close =		opticon_close,
622	.write =		opticon_write,
623	.write_room = 		opticon_write_room,
624	.disconnect =		opticon_disconnect,
625	.release =		opticon_release,
626	.throttle = 		opticon_throttle,
627	.unthrottle =		opticon_unthrottle,
628	.ioctl =		opticon_ioctl,
629	.tiocmget =		opticon_tiocmget,
630	.tiocmset =		opticon_tiocmset,
 
 
631};
632
633static int __init opticon_init(void)
634{
635	int retval;
636
637	retval = usb_serial_register(&opticon_device);
638	if (retval)
639		return retval;
640	retval = usb_register(&opticon_driver);
641	if (retval)
642		usb_serial_deregister(&opticon_device);
643	return retval;
644}
645
646static void __exit opticon_exit(void)
647{
648	usb_deregister(&opticon_driver);
649	usb_serial_deregister(&opticon_device);
650}
651
652module_init(opticon_init);
653module_exit(opticon_exit);
654MODULE_DESCRIPTION(DRIVER_DESC);
655MODULE_LICENSE("GPL");
656
657module_param(debug, bool, S_IRUGO | S_IWUSR);
658MODULE_PARM_DESC(debug, "Debug enabled or not");
v3.5.6
  1/*
  2 * Opticon USB barcode to serial driver
  3 *
  4 * Copyright (C) 2011 Martin Jansen <martin.jansen@opticon.com>
  5 * Copyright (C) 2008 - 2009 Greg Kroah-Hartman <gregkh@suse.de>
  6 * Copyright (C) 2008 - 2009 Novell Inc.
  7 *
  8 *	This program is free software; you can redistribute it and/or
  9 *	modify it under the terms of the GNU General Public License version
 10 *	2 as published by the Free Software Foundation.
 11 */
 12
 13#include <linux/kernel.h>
 14#include <linux/init.h>
 15#include <linux/tty.h>
 16#include <linux/tty_driver.h>
 17#include <linux/slab.h>
 18#include <linux/tty_flip.h>
 19#include <linux/serial.h>
 20#include <linux/module.h>
 21#include <linux/usb.h>
 22#include <linux/usb/serial.h>
 23#include <linux/uaccess.h>
 24
 25#define CONTROL_RTS			0x02
 26#define RESEND_CTS_STATE	0x03
 27
 28/* max number of write urbs in flight */
 29#define URB_UPPER_LIMIT	8
 30
 31/* This driver works for the Opticon 1D barcode reader
 32 * an examples of 1D barcode types are EAN, UPC, Code39, IATA etc.. */
 33#define DRIVER_DESC	"Opticon USB barcode to serial driver (1D)"
 34
 35static bool debug;
 36
 37static const struct usb_device_id id_table[] = {
 38	{ USB_DEVICE(0x065a, 0x0009) },
 39	{ },
 40};
 41MODULE_DEVICE_TABLE(usb, id_table);
 42
 43/* This structure holds all of the individual device information */
 44struct opticon_private {
 45	struct usb_device *udev;
 46	struct usb_serial *serial;
 47	struct usb_serial_port *port;
 48	unsigned char *bulk_in_buffer;
 49	struct urb *bulk_read_urb;
 50	int buffer_size;
 51	u8 bulk_address;
 52	spinlock_t lock;	/* protects the following flags */
 53	bool throttled;
 54	bool actually_throttled;
 55	bool rts;
 56	bool cts;
 57	int outstanding_urbs;
 58};
 59
 60
 61
 62static void opticon_read_bulk_callback(struct urb *urb)
 63{
 64	struct opticon_private *priv = urb->context;
 65	unsigned char *data = urb->transfer_buffer;
 66	struct usb_serial_port *port = priv->port;
 67	int status = urb->status;
 68	struct tty_struct *tty;
 69	int result;
 70	int data_length;
 71	unsigned long flags;
 72
 
 
 73	switch (status) {
 74	case 0:
 75		/* success */
 76		break;
 77	case -ECONNRESET:
 78	case -ENOENT:
 79	case -ESHUTDOWN:
 80		/* this urb is terminated, clean up */
 81		dbg("%s - urb shutting down with status: %d",
 82		    __func__, status);
 83		return;
 84	default:
 85		dbg("%s - nonzero urb status received: %d",
 86		    __func__, status);
 87		goto exit;
 88	}
 89
 90	usb_serial_debug_data(debug, &port->dev, __func__, urb->actual_length,
 91			      data);
 92
 93	if (urb->actual_length > 2) {
 94		data_length = urb->actual_length - 2;
 95
 96		/*
 97		 * Data from the device comes with a 2 byte header:
 98		 *
 99		 * <0x00><0x00>data...
100		 *	This is real data to be sent to the tty layer
101		 * <0x00><0x01)level
102		 *	This is a CTS level change, the third byte is the CTS
103		 *	value (0 for low, 1 for high).
104		 */
105		if ((data[0] == 0x00) && (data[1] == 0x00)) {
106			/* real data, send it to the tty layer */
107			tty = tty_port_tty_get(&port->port);
108			if (tty) {
109				tty_insert_flip_string(tty, data + 2,
110						       data_length);
111				tty_flip_buffer_push(tty);
112				tty_kref_put(tty);
113			}
114		} else {
115			if ((data[0] == 0x00) && (data[1] == 0x01)) {
116				spin_lock_irqsave(&priv->lock, flags);
117				/* CTS status information package */
118				if (data[2] == 0x00)
119					priv->cts = false;
120				else
121					priv->cts = true;
122				spin_unlock_irqrestore(&priv->lock, flags);
123			} else {
124				dev_dbg(&priv->udev->dev,
125					"Unknown data packet received from the device:"
126					" %2x %2x\n",
127					data[0], data[1]);
128			}
129		}
130	} else {
131		dev_dbg(&priv->udev->dev,
132			"Improper amount of data received from the device, "
133			"%d bytes", urb->actual_length);
134	}
135
136exit:
137	spin_lock(&priv->lock);
138
139	/* Continue trying to always read if we should */
140	if (!priv->throttled) {
141		usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev,
142				  usb_rcvbulkpipe(priv->udev,
143						  priv->bulk_address),
144				  priv->bulk_in_buffer, priv->buffer_size,
145				  opticon_read_bulk_callback, priv);
146		result = usb_submit_urb(priv->bulk_read_urb, GFP_ATOMIC);
147		if (result)
148			dev_err(&port->dev,
149			    "%s - failed resubmitting read urb, error %d\n",
150							__func__, result);
151	} else
152		priv->actually_throttled = true;
153	spin_unlock(&priv->lock);
154}
155
156static int send_control_msg(struct usb_serial_port *port, u8 requesttype,
157				u8 val)
158{
159	struct usb_serial *serial = port->serial;
160	int retval;
161	u8 buffer[2];
162
163	buffer[0] = val;
164	/* Send the message to the vendor control endpoint
165	 * of the connected device */
166	retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
167				requesttype,
168				USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE,
169				0, 0, buffer, 1, 0);
170
171	return retval;
172}
173
174static int opticon_open(struct tty_struct *tty, struct usb_serial_port *port)
175{
176	struct opticon_private *priv = usb_get_serial_data(port->serial);
177	unsigned long flags;
178	int result = 0;
179
 
 
180	spin_lock_irqsave(&priv->lock, flags);
181	priv->throttled = false;
182	priv->actually_throttled = false;
183	priv->port = port;
184	priv->rts = false;
185	spin_unlock_irqrestore(&priv->lock, flags);
186
187	/* Clear RTS line */
188	send_control_msg(port, CONTROL_RTS, 0);
189
190	/* Setup the read URB and start reading from the device */
191	usb_fill_bulk_urb(priv->bulk_read_urb, priv->udev,
192			  usb_rcvbulkpipe(priv->udev,
193					  priv->bulk_address),
194			  priv->bulk_in_buffer, priv->buffer_size,
195			  opticon_read_bulk_callback, priv);
196
197	/* clear the halt status of the enpoint */
198	usb_clear_halt(priv->udev, priv->bulk_read_urb->pipe);
199
200	result = usb_submit_urb(priv->bulk_read_urb, GFP_KERNEL);
201	if (result)
202		dev_err(&port->dev,
203			"%s - failed resubmitting read urb, error %d\n",
204			__func__, result);
205	/* Request CTS line state, sometimes during opening the current
206	 * CTS state can be missed. */
207	send_control_msg(port, RESEND_CTS_STATE, 1);
208	return result;
209}
210
211static void opticon_close(struct usb_serial_port *port)
212{
213	struct opticon_private *priv = usb_get_serial_data(port->serial);
214
 
 
215	/* shutdown our urbs */
216	usb_kill_urb(priv->bulk_read_urb);
217}
218
219static void opticon_write_control_callback(struct urb *urb)
220{
221	struct opticon_private *priv = urb->context;
222	int status = urb->status;
223	unsigned long flags;
224
225	/* free up the transfer buffer, as usb_free_urb() does not do this */
226	kfree(urb->transfer_buffer);
227
228	/* setup packet may be set if we're using it for writing */
229	kfree(urb->setup_packet);
230
231	if (status)
232		dbg("%s - nonzero write bulk status received: %d",
233		    __func__, status);
234
235	spin_lock_irqsave(&priv->lock, flags);
236	--priv->outstanding_urbs;
237	spin_unlock_irqrestore(&priv->lock, flags);
238
239	usb_serial_port_softint(priv->port);
240}
241
242static int opticon_write(struct tty_struct *tty, struct usb_serial_port *port,
243			 const unsigned char *buf, int count)
244{
245	struct opticon_private *priv = usb_get_serial_data(port->serial);
246	struct usb_serial *serial = port->serial;
247	struct urb *urb;
248	unsigned char *buffer;
249	unsigned long flags;
250	int status;
251	struct usb_ctrlrequest *dr;
252
 
 
253	spin_lock_irqsave(&priv->lock, flags);
254	if (priv->outstanding_urbs > URB_UPPER_LIMIT) {
255		spin_unlock_irqrestore(&priv->lock, flags);
256		dbg("%s - write limit hit", __func__);
257		return 0;
258	}
259	priv->outstanding_urbs++;
260	spin_unlock_irqrestore(&priv->lock, flags);
261
262	buffer = kmalloc(count, GFP_ATOMIC);
263	if (!buffer) {
264		dev_err(&port->dev, "out of memory\n");
265		count = -ENOMEM;
266
267		goto error_no_buffer;
268	}
269
270	urb = usb_alloc_urb(0, GFP_ATOMIC);
271	if (!urb) {
272		dev_err(&port->dev, "no more free urbs\n");
273		count = -ENOMEM;
274		goto error_no_urb;
275	}
276
277	memcpy(buffer, buf, count);
278
279	usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
280
281	/* The conncected devices do not have a bulk write endpoint,
282	 * to transmit data to de barcode device the control endpoint is used */
283	dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_NOIO);
284	if (!dr) {
285		dev_err(&port->dev, "out of memory\n");
286		count = -ENOMEM;
287		goto error;
288	}
289
290	dr->bRequestType = USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT;
291	dr->bRequest = 0x01;
292	dr->wValue = 0;
293	dr->wIndex = 0;
294	dr->wLength = cpu_to_le16(count);
295
296	usb_fill_control_urb(urb, serial->dev,
297		usb_sndctrlpipe(serial->dev, 0),
298		(unsigned char *)dr, buffer, count,
299		opticon_write_control_callback, priv);
300
301	/* send it down the pipe */
302	status = usb_submit_urb(urb, GFP_ATOMIC);
303	if (status) {
304		dev_err(&port->dev,
305		"%s - usb_submit_urb(write endpoint) failed status = %d\n",
306							__func__, status);
307		count = status;
308		goto error;
309	}
310
311	/* we are done with this urb, so let the host driver
312	 * really free it when it is finished with it */
313	usb_free_urb(urb);
314
315	return count;
316error:
317	usb_free_urb(urb);
318error_no_urb:
319	kfree(buffer);
320error_no_buffer:
321	spin_lock_irqsave(&priv->lock, flags);
322	--priv->outstanding_urbs;
323	spin_unlock_irqrestore(&priv->lock, flags);
324	return count;
325}
326
327static int opticon_write_room(struct tty_struct *tty)
328{
329	struct usb_serial_port *port = tty->driver_data;
330	struct opticon_private *priv = usb_get_serial_data(port->serial);
331	unsigned long flags;
332
 
 
333	/*
334	 * We really can take almost anything the user throws at us
335	 * but let's pick a nice big number to tell the tty
336	 * layer that we have lots of free space, unless we don't.
337	 */
338	spin_lock_irqsave(&priv->lock, flags);
339	if (priv->outstanding_urbs > URB_UPPER_LIMIT * 2 / 3) {
340		spin_unlock_irqrestore(&priv->lock, flags);
341		dbg("%s - write limit hit", __func__);
342		return 0;
343	}
344	spin_unlock_irqrestore(&priv->lock, flags);
345
346	return 2048;
347}
348
349static void opticon_throttle(struct tty_struct *tty)
350{
351	struct usb_serial_port *port = tty->driver_data;
352	struct opticon_private *priv = usb_get_serial_data(port->serial);
353	unsigned long flags;
354
 
355	spin_lock_irqsave(&priv->lock, flags);
356	priv->throttled = true;
357	spin_unlock_irqrestore(&priv->lock, flags);
358}
359
360
361static void opticon_unthrottle(struct tty_struct *tty)
362{
363	struct usb_serial_port *port = tty->driver_data;
364	struct opticon_private *priv = usb_get_serial_data(port->serial);
365	unsigned long flags;
366	int result, was_throttled;
367
 
 
368	spin_lock_irqsave(&priv->lock, flags);
369	priv->throttled = false;
370	was_throttled = priv->actually_throttled;
371	priv->actually_throttled = false;
372	spin_unlock_irqrestore(&priv->lock, flags);
373
 
374	if (was_throttled) {
375		result = usb_submit_urb(priv->bulk_read_urb, GFP_ATOMIC);
376		if (result)
377			dev_err(&port->dev,
378				"%s - failed submitting read urb, error %d\n",
379							__func__, result);
380	}
381}
382
383static int opticon_tiocmget(struct tty_struct *tty)
384{
385	struct usb_serial_port *port = tty->driver_data;
386	struct opticon_private *priv = usb_get_serial_data(port->serial);
387	unsigned long flags;
388	int result = 0;
389
 
 
 
 
390	spin_lock_irqsave(&priv->lock, flags);
391	if (priv->rts)
392		result |= TIOCM_RTS;
393	if (priv->cts)
394		result |= TIOCM_CTS;
395	spin_unlock_irqrestore(&priv->lock, flags);
396
397	dbg("%s - %x", __func__, result);
398	return result;
399}
400
401static int opticon_tiocmset(struct tty_struct *tty,
402			   unsigned int set, unsigned int clear)
403{
404	struct usb_serial_port *port = tty->driver_data;
405	struct usb_serial *serial = port->serial;
406	struct opticon_private *priv = usb_get_serial_data(port->serial);
407	unsigned long flags;
408	bool rts;
409	bool changed = false;
410	int ret;
411
 
 
412	/* We only support RTS so we only handle that */
413	spin_lock_irqsave(&priv->lock, flags);
414
415	rts = priv->rts;
416	if (set & TIOCM_RTS)
417		priv->rts = true;
418	if (clear & TIOCM_RTS)
419		priv->rts = false;
420	changed = rts ^ priv->rts;
421	spin_unlock_irqrestore(&priv->lock, flags);
422
423	if (!changed)
424		return 0;
425
426	/* Send the new RTS state to the connected device */
427	mutex_lock(&serial->disc_mutex);
428	if (!serial->disconnected)
429		ret = send_control_msg(port, CONTROL_RTS, !rts);
430	else
431		ret = -ENODEV;
432	mutex_unlock(&serial->disc_mutex);
433
434	return ret;
435}
436
437static int get_serial_info(struct opticon_private *priv,
438			   struct serial_struct __user *serial)
439{
440	struct serial_struct tmp;
441
442	if (!serial)
443		return -EFAULT;
444
445	memset(&tmp, 0x00, sizeof(tmp));
446
447	/* fake emulate a 16550 uart to make userspace code happy */
448	tmp.type		= PORT_16550A;
449	tmp.line		= priv->serial->minor;
450	tmp.port		= 0;
451	tmp.irq			= 0;
452	tmp.flags		= ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
453	tmp.xmit_fifo_size	= 1024;
454	tmp.baud_base		= 9600;
455	tmp.close_delay		= 5*HZ;
456	tmp.closing_wait	= 30*HZ;
457
458	if (copy_to_user(serial, &tmp, sizeof(*serial)))
459		return -EFAULT;
460	return 0;
461}
462
463static int opticon_ioctl(struct tty_struct *tty,
464			 unsigned int cmd, unsigned long arg)
465{
466	struct usb_serial_port *port = tty->driver_data;
467	struct opticon_private *priv = usb_get_serial_data(port->serial);
468
469	dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
470
471	switch (cmd) {
472	case TIOCGSERIAL:
473		return get_serial_info(priv,
474				       (struct serial_struct __user *)arg);
475	}
476
477	return -ENOIOCTLCMD;
478}
479
480static int opticon_startup(struct usb_serial *serial)
481{
482	struct opticon_private *priv;
483	struct usb_host_interface *intf;
484	int i;
485	int retval = -ENOMEM;
486	bool bulk_in_found = false;
487
488	/* create our private serial structure */
489	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
490	if (priv == NULL) {
491		dev_err(&serial->dev->dev, "%s - Out of memory\n", __func__);
492		return -ENOMEM;
493	}
494	spin_lock_init(&priv->lock);
495	priv->serial = serial;
496	priv->port = serial->port[0];
497	priv->udev = serial->dev;
498	priv->outstanding_urbs = 0;	/* Init the outstanding urbs */
499
500	/* find our bulk endpoint */
501	intf = serial->interface->altsetting;
502	for (i = 0; i < intf->desc.bNumEndpoints; ++i) {
503		struct usb_endpoint_descriptor *endpoint;
504
505		endpoint = &intf->endpoint[i].desc;
506		if (!usb_endpoint_is_bulk_in(endpoint))
507			continue;
508
509		priv->bulk_read_urb = usb_alloc_urb(0, GFP_KERNEL);
510		if (!priv->bulk_read_urb) {
511			dev_err(&priv->udev->dev, "out of memory\n");
512			goto error;
513		}
514
515		priv->buffer_size = usb_endpoint_maxp(endpoint) * 2;
516		priv->bulk_in_buffer = kmalloc(priv->buffer_size, GFP_KERNEL);
517		if (!priv->bulk_in_buffer) {
518			dev_err(&priv->udev->dev, "out of memory\n");
519			goto error;
520		}
521
522		priv->bulk_address = endpoint->bEndpointAddress;
523
524		bulk_in_found = true;
525		break;
526		}
527
528	if (!bulk_in_found) {
529		dev_err(&priv->udev->dev,
530			"Error - the proper endpoints were not found!\n");
531		goto error;
532	}
533
534	usb_set_serial_data(serial, priv);
535	return 0;
536
537error:
538	usb_free_urb(priv->bulk_read_urb);
539	kfree(priv->bulk_in_buffer);
540	kfree(priv);
541	return retval;
542}
543
544static void opticon_disconnect(struct usb_serial *serial)
545{
546	struct opticon_private *priv = usb_get_serial_data(serial);
547
 
 
548	usb_kill_urb(priv->bulk_read_urb);
549	usb_free_urb(priv->bulk_read_urb);
550}
551
552static void opticon_release(struct usb_serial *serial)
553{
554	struct opticon_private *priv = usb_get_serial_data(serial);
555
 
 
556	kfree(priv->bulk_in_buffer);
557	kfree(priv);
558}
559
560static int opticon_suspend(struct usb_serial *serial, pm_message_t message)
561{
 
562	struct opticon_private *priv = usb_get_serial_data(serial);
563
564	usb_kill_urb(priv->bulk_read_urb);
565	return 0;
566}
567
568static int opticon_resume(struct usb_serial *serial)
569{
 
570	struct opticon_private *priv = usb_get_serial_data(serial);
571	struct usb_serial_port *port = serial->port[0];
572	int result;
573
574	mutex_lock(&port->port.mutex);
575	/* This is protected by the port mutex against close/open */
576	if (test_bit(ASYNCB_INITIALIZED, &port->port.flags))
577		result = usb_submit_urb(priv->bulk_read_urb, GFP_NOIO);
578	else
579		result = 0;
580	mutex_unlock(&port->port.mutex);
581	return result;
582}
583
 
 
 
 
 
 
 
 
 
 
584static struct usb_serial_driver opticon_device = {
585	.driver = {
586		.owner =	THIS_MODULE,
587		.name =		"opticon",
588	},
589	.id_table =		id_table,
 
590	.num_ports =		1,
591	.attach =		opticon_startup,
592	.open =			opticon_open,
593	.close =		opticon_close,
594	.write =		opticon_write,
595	.write_room = 		opticon_write_room,
596	.disconnect =		opticon_disconnect,
597	.release =		opticon_release,
598	.throttle = 		opticon_throttle,
599	.unthrottle =		opticon_unthrottle,
600	.ioctl =		opticon_ioctl,
601	.tiocmget =		opticon_tiocmget,
602	.tiocmset =		opticon_tiocmset,
603	.suspend = 		opticon_suspend,
604	.resume =		opticon_resume,
605};
606
607static struct usb_serial_driver * const serial_drivers[] = {
608	&opticon_device, NULL
609};
 
 
 
 
 
 
 
 
 
610
611module_usb_serial_driver(serial_drivers, id_table);
 
 
 
 
612
 
 
613MODULE_DESCRIPTION(DRIVER_DESC);
614MODULE_LICENSE("GPL");
615
616module_param(debug, bool, S_IRUGO | S_IWUSR);
617MODULE_PARM_DESC(debug, "Debug enabled or not");