xref: /linux/drivers/usb/serial/cyberjack.c (revision 2e124b4a390ca85325fae75764bef92f0547fa25)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  REINER SCT cyberJack pinpad/e-com USB Chipcard Reader Driver
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 2001  REINER SCT
51da177e4SLinus Torvalds  *  Author: Matthias Bruestle
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  *  Contact: support@reiner-sct.com (see MAINTAINERS)
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  *  This program is largely derived from work by the linux-usb group
101da177e4SLinus Torvalds  *  and associated source files.  Please see the usb/serial files for
111da177e4SLinus Torvalds  *  individual credits and copyrights.
121da177e4SLinus Torvalds  *
131da177e4SLinus Torvalds  *  This program is free software; you can redistribute it and/or modify
141da177e4SLinus Torvalds  *  it under the terms of the GNU General Public License as published by
151da177e4SLinus Torvalds  *  the Free Software Foundation; either version 2 of the License, or
161da177e4SLinus Torvalds  *  (at your option) any later version.
171da177e4SLinus Torvalds  *
181da177e4SLinus Torvalds  *  Thanks to Greg Kroah-Hartman (greg@kroah.com) for his help and
191da177e4SLinus Torvalds  *  patience.
201da177e4SLinus Torvalds  *
211da177e4SLinus Torvalds  *  In case of problems, please write to the contact e-mail address
221da177e4SLinus Torvalds  *  mentioned above.
231da177e4SLinus Torvalds  *
241da177e4SLinus Torvalds  *  Please note that later models of the cyberjack reader family are
251da177e4SLinus Torvalds  *  supported by a libusb-based userspace device driver.
261da177e4SLinus Torvalds  *
271da177e4SLinus Torvalds  *  Homepage: http://www.reiner-sct.de/support/treiber_cyberjack.php#linux
281da177e4SLinus Torvalds  */
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds #include <linux/kernel.h>
321da177e4SLinus Torvalds #include <linux/errno.h>
331da177e4SLinus Torvalds #include <linux/init.h>
341da177e4SLinus Torvalds #include <linux/slab.h>
351da177e4SLinus Torvalds #include <linux/tty.h>
361da177e4SLinus Torvalds #include <linux/tty_driver.h>
371da177e4SLinus Torvalds #include <linux/tty_flip.h>
381da177e4SLinus Torvalds #include <linux/module.h>
391da177e4SLinus Torvalds #include <linux/spinlock.h>
40b9c52f15SAlan Cox #include <linux/uaccess.h>
411da177e4SLinus Torvalds #include <linux/usb.h>
42a969888cSGreg Kroah-Hartman #include <linux/usb/serial.h>
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds #define CYBERJACK_LOCAL_BUF_SIZE 32
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds #define DRIVER_AUTHOR "Matthias Bruestle"
471da177e4SLinus Torvalds #define DRIVER_DESC "REINER SCT cyberJack pinpad/e-com USB Chipcard Reader Driver"
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds #define CYBERJACK_VENDOR_ID	0x0C4B
511da177e4SLinus Torvalds #define CYBERJACK_PRODUCT_ID	0x0100
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds /* Function prototypes */
54f9c99bb8SAlan Stern static void cyberjack_disconnect(struct usb_serial *serial);
55a9556040SJohan Hovold static int cyberjack_port_probe(struct usb_serial_port *port);
56a9556040SJohan Hovold static int cyberjack_port_remove(struct usb_serial_port *port);
5795da310eSAlan Cox static int  cyberjack_open(struct tty_struct *tty,
58a509a7e4SAlan Cox 	struct usb_serial_port *port);
59335f8514SAlan Cox static void cyberjack_close(struct usb_serial_port *port);
6095da310eSAlan Cox static int cyberjack_write(struct tty_struct *tty,
6195da310eSAlan Cox 	struct usb_serial_port *port, const unsigned char *buf, int count);
6295da310eSAlan Cox static int cyberjack_write_room(struct tty_struct *tty);
637d12e780SDavid Howells static void cyberjack_read_int_callback(struct urb *urb);
647d12e780SDavid Howells static void cyberjack_read_bulk_callback(struct urb *urb);
657d12e780SDavid Howells static void cyberjack_write_bulk_callback(struct urb *urb);
661da177e4SLinus Torvalds 
677d40d7e8SNémeth Márton static const struct usb_device_id id_table[] = {
681da177e4SLinus Torvalds 	{ USB_DEVICE(CYBERJACK_VENDOR_ID, CYBERJACK_PRODUCT_ID) },
691da177e4SLinus Torvalds 	{ }			/* Terminating entry */
701da177e4SLinus Torvalds };
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds MODULE_DEVICE_TABLE(usb, id_table);
731da177e4SLinus Torvalds 
74ea65370dSGreg Kroah-Hartman static struct usb_serial_driver cyberjack_device = {
7518fcac35SGreg Kroah-Hartman 	.driver = {
761da177e4SLinus Torvalds 		.owner =	THIS_MODULE,
77269bda1cSGreg Kroah-Hartman 		.name =		"cyberjack",
7818fcac35SGreg Kroah-Hartman 	},
79269bda1cSGreg Kroah-Hartman 	.description =		"Reiner SCT Cyberjack USB card reader",
801da177e4SLinus Torvalds 	.id_table =		id_table,
811da177e4SLinus Torvalds 	.num_ports =		1,
82f9c99bb8SAlan Stern 	.disconnect =		cyberjack_disconnect,
83a9556040SJohan Hovold 	.port_probe =		cyberjack_port_probe,
84a9556040SJohan Hovold 	.port_remove =		cyberjack_port_remove,
851da177e4SLinus Torvalds 	.open =			cyberjack_open,
861da177e4SLinus Torvalds 	.close =		cyberjack_close,
871da177e4SLinus Torvalds 	.write =		cyberjack_write,
881da177e4SLinus Torvalds 	.write_room =		cyberjack_write_room,
891da177e4SLinus Torvalds 	.read_int_callback =	cyberjack_read_int_callback,
901da177e4SLinus Torvalds 	.read_bulk_callback =	cyberjack_read_bulk_callback,
911da177e4SLinus Torvalds 	.write_bulk_callback =	cyberjack_write_bulk_callback,
921da177e4SLinus Torvalds };
931da177e4SLinus Torvalds 
9408a4f6bcSAlan Stern static struct usb_serial_driver * const serial_drivers[] = {
9508a4f6bcSAlan Stern 	&cyberjack_device, NULL
9608a4f6bcSAlan Stern };
9708a4f6bcSAlan Stern 
981da177e4SLinus Torvalds struct cyberjack_private {
991da177e4SLinus Torvalds 	spinlock_t	lock;		/* Lock for SMP */
1001da177e4SLinus Torvalds 	short		rdtodo;		/* Bytes still to read */
1011da177e4SLinus Torvalds 	unsigned char	wrbuf[5*64];	/* Buffer for collecting data to write */
1021da177e4SLinus Torvalds 	short		wrfilled;	/* Overall data size we already got */
1031da177e4SLinus Torvalds 	short		wrsent;		/* Data already sent */
1041da177e4SLinus Torvalds };
1051da177e4SLinus Torvalds 
106a9556040SJohan Hovold static int cyberjack_port_probe(struct usb_serial_port *port)
1071da177e4SLinus Torvalds {
1081da177e4SLinus Torvalds 	struct cyberjack_private *priv;
109a9556040SJohan Hovold 	int result;
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds 	priv = kmalloc(sizeof(struct cyberjack_private), GFP_KERNEL);
1121da177e4SLinus Torvalds 	if (!priv)
1131da177e4SLinus Torvalds 		return -ENOMEM;
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds 	spin_lock_init(&priv->lock);
1161da177e4SLinus Torvalds 	priv->rdtodo = 0;
1171da177e4SLinus Torvalds 	priv->wrfilled = 0;
1181da177e4SLinus Torvalds 	priv->wrsent = 0;
1191da177e4SLinus Torvalds 
120a9556040SJohan Hovold 	usb_set_serial_port_data(port, priv);
1211da177e4SLinus Torvalds 
122a9556040SJohan Hovold 	result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
1231da177e4SLinus Torvalds 	if (result)
124a9556040SJohan Hovold 		dev_err(&port->dev, "usb_submit_urb(read int) failed\n");
125a9556040SJohan Hovold 
126a9556040SJohan Hovold 	return 0;
1271da177e4SLinus Torvalds }
1281da177e4SLinus Torvalds 
129a9556040SJohan Hovold static int cyberjack_port_remove(struct usb_serial_port *port)
130a9556040SJohan Hovold {
131a9556040SJohan Hovold 	struct cyberjack_private *priv;
132a9556040SJohan Hovold 
133a9556040SJohan Hovold 	priv = usb_get_serial_port_data(port);
134a9556040SJohan Hovold 	kfree(priv);
135a9556040SJohan Hovold 
136b9c52f15SAlan Cox 	return 0;
1371da177e4SLinus Torvalds }
1381da177e4SLinus Torvalds 
139f9c99bb8SAlan Stern static void cyberjack_disconnect(struct usb_serial *serial)
140f9c99bb8SAlan Stern {
141f9c99bb8SAlan Stern 	int i;
142f9c99bb8SAlan Stern 
143f9c99bb8SAlan Stern 	for (i = 0; i < serial->num_ports; ++i)
144f9c99bb8SAlan Stern 		usb_kill_urb(serial->port[i]->interrupt_in_urb);
145f9c99bb8SAlan Stern }
146f9c99bb8SAlan Stern 
14795da310eSAlan Cox static int  cyberjack_open(struct tty_struct *tty,
148a509a7e4SAlan Cox 					struct usb_serial_port *port)
1491da177e4SLinus Torvalds {
1501da177e4SLinus Torvalds 	struct cyberjack_private *priv;
1511da177e4SLinus Torvalds 	unsigned long flags;
1521da177e4SLinus Torvalds 	int result = 0;
1531da177e4SLinus Torvalds 
15496fc8e8dSGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - usb_clear_halt\n", __func__);
1551da177e4SLinus Torvalds 	usb_clear_halt(port->serial->dev, port->write_urb->pipe);
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds 	priv = usb_get_serial_port_data(port);
1581da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->lock, flags);
1591da177e4SLinus Torvalds 	priv->rdtodo = 0;
1601da177e4SLinus Torvalds 	priv->wrfilled = 0;
1611da177e4SLinus Torvalds 	priv->wrsent = 0;
1621da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->lock, flags);
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 	return result;
1651da177e4SLinus Torvalds }
1661da177e4SLinus Torvalds 
167335f8514SAlan Cox static void cyberjack_close(struct usb_serial_port *port)
1681da177e4SLinus Torvalds {
1691da177e4SLinus Torvalds 	if (port->serial->dev) {
1701da177e4SLinus Torvalds 		/* shutdown any bulk reads that might be going on */
1711da177e4SLinus Torvalds 		usb_kill_urb(port->write_urb);
1721da177e4SLinus Torvalds 		usb_kill_urb(port->read_urb);
1731da177e4SLinus Torvalds 	}
1741da177e4SLinus Torvalds }
1751da177e4SLinus Torvalds 
17695da310eSAlan Cox static int cyberjack_write(struct tty_struct *tty,
17795da310eSAlan Cox 	struct usb_serial_port *port, const unsigned char *buf, int count)
1781da177e4SLinus Torvalds {
17996fc8e8dSGreg Kroah-Hartman 	struct device *dev = &port->dev;
1801da177e4SLinus Torvalds 	struct cyberjack_private *priv = usb_get_serial_port_data(port);
1811da177e4SLinus Torvalds 	unsigned long flags;
1821da177e4SLinus Torvalds 	int result;
1831da177e4SLinus Torvalds 	int wrexpected;
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds 	if (count == 0) {
18696fc8e8dSGreg Kroah-Hartman 		dev_dbg(dev, "%s - write request of 0 bytes\n", __func__);
187a5b6f60cSAlan Cox 		return 0;
1881da177e4SLinus Torvalds 	}
1891da177e4SLinus Torvalds 
190c1cac10cSJohan Hovold 	if (!test_and_clear_bit(0, &port->write_urbs_free)) {
19196fc8e8dSGreg Kroah-Hartman 		dev_dbg(dev, "%s - already writing\n", __func__);
192507ca9bcSGreg Kroah-Hartman 		return 0;
1931da177e4SLinus Torvalds 	}
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->lock, flags);
1961da177e4SLinus Torvalds 
197b9c52f15SAlan Cox 	if (count+priv->wrfilled > sizeof(priv->wrbuf)) {
1981da177e4SLinus Torvalds 		/* To much data for buffer. Reset buffer. */
1991da177e4SLinus Torvalds 		priv->wrfilled = 0;
200a5b6f60cSAlan Cox 		spin_unlock_irqrestore(&priv->lock, flags);
201c1cac10cSJohan Hovold 		set_bit(0, &port->write_urbs_free);
202a5b6f60cSAlan Cox 		return 0;
2031da177e4SLinus Torvalds 	}
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds 	/* Copy data */
2061da177e4SLinus Torvalds 	memcpy(priv->wrbuf + priv->wrfilled, buf, count);
2071da177e4SLinus Torvalds 
20859d33f2fSGreg Kroah-Hartman 	usb_serial_debug_data(dev, __func__, count, priv->wrbuf + priv->wrfilled);
2091da177e4SLinus Torvalds 	priv->wrfilled += count;
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds 	if (priv->wrfilled >= 3) {
2121da177e4SLinus Torvalds 		wrexpected = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
21396fc8e8dSGreg Kroah-Hartman 		dev_dbg(dev, "%s - expected data: %d\n", __func__, wrexpected);
214b9c52f15SAlan Cox 	} else
2151da177e4SLinus Torvalds 		wrexpected = sizeof(priv->wrbuf);
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 	if (priv->wrfilled >= wrexpected) {
2181da177e4SLinus Torvalds 		/* We have enough data to begin transmission */
2191da177e4SLinus Torvalds 		int length;
2201da177e4SLinus Torvalds 
22196fc8e8dSGreg Kroah-Hartman 		dev_dbg(dev, "%s - transmitting data (frame 1)\n", __func__);
222b9c52f15SAlan Cox 		length = (wrexpected > port->bulk_out_size) ?
223b9c52f15SAlan Cox 					port->bulk_out_size : wrexpected;
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds 		memcpy(port->write_urb->transfer_buffer, priv->wrbuf, length);
2261da177e4SLinus Torvalds 		priv->wrsent = length;
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds 		/* set up our urb */
229fd11961aSJohan Hovold 		port->write_urb->transfer_buffer_length = length;
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds 		/* send the data out the bulk port */
2321da177e4SLinus Torvalds 		result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
2331da177e4SLinus Torvalds 		if (result) {
234194343d9SGreg Kroah-Hartman 			dev_err(&port->dev,
235194343d9SGreg Kroah-Hartman 				"%s - failed submitting write urb, error %d",
236b9c52f15SAlan Cox 				__func__, result);
2371da177e4SLinus Torvalds 			/* Throw away data. No better idea what to do with it. */
2381da177e4SLinus Torvalds 			priv->wrfilled = 0;
2391da177e4SLinus Torvalds 			priv->wrsent = 0;
2401da177e4SLinus Torvalds 			spin_unlock_irqrestore(&priv->lock, flags);
241c1cac10cSJohan Hovold 			set_bit(0, &port->write_urbs_free);
2421da177e4SLinus Torvalds 			return 0;
2431da177e4SLinus Torvalds 		}
2441da177e4SLinus Torvalds 
24596fc8e8dSGreg Kroah-Hartman 		dev_dbg(dev, "%s - priv->wrsent=%d\n", __func__, priv->wrsent);
24696fc8e8dSGreg Kroah-Hartman 		dev_dbg(dev, "%s - priv->wrfilled=%d\n", __func__, priv->wrfilled);
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds 		if (priv->wrsent >= priv->wrfilled) {
24996fc8e8dSGreg Kroah-Hartman 			dev_dbg(dev, "%s - buffer cleaned\n", __func__);
2501da177e4SLinus Torvalds 			memset(priv->wrbuf, 0, sizeof(priv->wrbuf));
2511da177e4SLinus Torvalds 			priv->wrfilled = 0;
2521da177e4SLinus Torvalds 			priv->wrsent = 0;
2531da177e4SLinus Torvalds 		}
2541da177e4SLinus Torvalds 	}
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->lock, flags);
2571da177e4SLinus Torvalds 
258b9c52f15SAlan Cox 	return count;
2591da177e4SLinus Torvalds }
2601da177e4SLinus Torvalds 
26195da310eSAlan Cox static int cyberjack_write_room(struct tty_struct *tty)
2621da177e4SLinus Torvalds {
263a5b6f60cSAlan Cox 	/* FIXME: .... */
2641da177e4SLinus Torvalds 	return CYBERJACK_LOCAL_BUF_SIZE;
2651da177e4SLinus Torvalds }
2661da177e4SLinus Torvalds 
2677d12e780SDavid Howells static void cyberjack_read_int_callback(struct urb *urb)
2681da177e4SLinus Torvalds {
269cdc97792SMing Lei 	struct usb_serial_port *port = urb->context;
2701da177e4SLinus Torvalds 	struct cyberjack_private *priv = usb_get_serial_port_data(port);
27196fc8e8dSGreg Kroah-Hartman 	struct device *dev = &port->dev;
2721da177e4SLinus Torvalds 	unsigned char *data = urb->transfer_buffer;
2737dcc85cdSGreg Kroah-Hartman 	int status = urb->status;
2741da177e4SLinus Torvalds 	int result;
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds 	/* the urb might have been killed. */
2777dcc85cdSGreg Kroah-Hartman 	if (status)
2781da177e4SLinus Torvalds 		return;
2791da177e4SLinus Torvalds 
28059d33f2fSGreg Kroah-Hartman 	usb_serial_debug_data(dev, __func__, urb->actual_length, data);
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds 	/* React only to interrupts signaling a bulk_in transfer */
283b9c52f15SAlan Cox 	if (urb->actual_length == 4 && data[0] == 0x01) {
2841da177e4SLinus Torvalds 		short old_rdtodo;
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds 		/* This is a announcement of coming bulk_ins. */
2871da177e4SLinus Torvalds 		unsigned short size = ((unsigned short)data[3]<<8)+data[2]+3;
2881da177e4SLinus Torvalds 
2891da177e4SLinus Torvalds 		spin_lock(&priv->lock);
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 		old_rdtodo = priv->rdtodo;
2921da177e4SLinus Torvalds 
293b9c52f15SAlan Cox 		if (old_rdtodo + size < old_rdtodo) {
29496fc8e8dSGreg Kroah-Hartman 			dev_dbg(dev, "To many bulk_in urbs to do.\n");
2951da177e4SLinus Torvalds 			spin_unlock(&priv->lock);
2961da177e4SLinus Torvalds 			goto resubmit;
2971da177e4SLinus Torvalds 		}
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds 		/* "+=" is probably more fault tollerant than "=" */
3001da177e4SLinus Torvalds 		priv->rdtodo += size;
3011da177e4SLinus Torvalds 
30296fc8e8dSGreg Kroah-Hartman 		dev_dbg(dev, "%s - rdtodo: %d\n", __func__, priv->rdtodo);
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds 		spin_unlock(&priv->lock);
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds 		if (!old_rdtodo) {
3071da177e4SLinus Torvalds 			result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
3081da177e4SLinus Torvalds 			if (result)
30996fc8e8dSGreg Kroah-Hartman 				dev_err(dev, "%s - failed resubmitting read urb, error %d\n",
310194343d9SGreg Kroah-Hartman 					__func__, result);
31196fc8e8dSGreg Kroah-Hartman 			dev_dbg(dev, "%s - usb_submit_urb(read urb)\n", __func__);
3121da177e4SLinus Torvalds 		}
3131da177e4SLinus Torvalds 	}
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds resubmit:
3161da177e4SLinus Torvalds 	result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
3171da177e4SLinus Torvalds 	if (result)
318194343d9SGreg Kroah-Hartman 		dev_err(&port->dev, "usb_submit_urb(read int) failed\n");
31996fc8e8dSGreg Kroah-Hartman 	dev_dbg(dev, "%s - usb_submit_urb(int urb)\n", __func__);
3201da177e4SLinus Torvalds }
3211da177e4SLinus Torvalds 
3227d12e780SDavid Howells static void cyberjack_read_bulk_callback(struct urb *urb)
3231da177e4SLinus Torvalds {
324cdc97792SMing Lei 	struct usb_serial_port *port = urb->context;
3251da177e4SLinus Torvalds 	struct cyberjack_private *priv = usb_get_serial_port_data(port);
32696fc8e8dSGreg Kroah-Hartman 	struct device *dev = &port->dev;
3271da177e4SLinus Torvalds 	unsigned char *data = urb->transfer_buffer;
3281da177e4SLinus Torvalds 	short todo;
3291da177e4SLinus Torvalds 	int result;
3307dcc85cdSGreg Kroah-Hartman 	int status = urb->status;
3311da177e4SLinus Torvalds 
33259d33f2fSGreg Kroah-Hartman 	usb_serial_debug_data(dev, __func__, urb->actual_length, data);
3337dcc85cdSGreg Kroah-Hartman 	if (status) {
33496fc8e8dSGreg Kroah-Hartman 		dev_dbg(dev, "%s - nonzero read bulk status received: %d\n",
335441b62c1SHarvey Harrison 			__func__, status);
3361da177e4SLinus Torvalds 		return;
3371da177e4SLinus Torvalds 	}
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds 	if (urb->actual_length) {
34005c7cd39SJiri Slaby 		tty_insert_flip_string(&port->port, data, urb->actual_length);
341*2e124b4aSJiri Slaby 		tty_flip_buffer_push(&port->port);
3421da177e4SLinus Torvalds 	}
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds 	spin_lock(&priv->lock);
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds 	/* Reduce urbs to do by one. */
3471da177e4SLinus Torvalds 	priv->rdtodo -= urb->actual_length;
3481da177e4SLinus Torvalds 	/* Just to be sure */
349b9c52f15SAlan Cox 	if (priv->rdtodo < 0)
350b9c52f15SAlan Cox 		priv->rdtodo = 0;
3511da177e4SLinus Torvalds 	todo = priv->rdtodo;
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds 	spin_unlock(&priv->lock);
3541da177e4SLinus Torvalds 
35596fc8e8dSGreg Kroah-Hartman 	dev_dbg(dev, "%s - rdtodo: %d\n", __func__, todo);
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds 	/* Continue to read if we have still urbs to do. */
3581da177e4SLinus Torvalds 	if (todo /* || (urb->actual_length==port->bulk_in_endpointAddress)*/) {
3591da177e4SLinus Torvalds 		result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
3601da177e4SLinus Torvalds 		if (result)
36196fc8e8dSGreg Kroah-Hartman 			dev_err(dev, "%s - failed resubmitting read urb, error %d\n",
36296fc8e8dSGreg Kroah-Hartman 				__func__, result);
36396fc8e8dSGreg Kroah-Hartman 		dev_dbg(dev, "%s - usb_submit_urb(read urb)\n", __func__);
3641da177e4SLinus Torvalds 	}
3651da177e4SLinus Torvalds }
3661da177e4SLinus Torvalds 
3677d12e780SDavid Howells static void cyberjack_write_bulk_callback(struct urb *urb)
3681da177e4SLinus Torvalds {
369cdc97792SMing Lei 	struct usb_serial_port *port = urb->context;
3701da177e4SLinus Torvalds 	struct cyberjack_private *priv = usb_get_serial_port_data(port);
37196fc8e8dSGreg Kroah-Hartman 	struct device *dev = &port->dev;
3727dcc85cdSGreg Kroah-Hartman 	int status = urb->status;
3731da177e4SLinus Torvalds 
374c1cac10cSJohan Hovold 	set_bit(0, &port->write_urbs_free);
3757dcc85cdSGreg Kroah-Hartman 	if (status) {
37696fc8e8dSGreg Kroah-Hartman 		dev_dbg(dev, "%s - nonzero write bulk status received: %d\n",
377441b62c1SHarvey Harrison 			__func__, status);
3781da177e4SLinus Torvalds 		return;
3791da177e4SLinus Torvalds 	}
3801da177e4SLinus Torvalds 
3811da177e4SLinus Torvalds 	spin_lock(&priv->lock);
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds 	/* only do something if we have more data to send */
3841da177e4SLinus Torvalds 	if (priv->wrfilled) {
3851da177e4SLinus Torvalds 		int length, blksize, result;
3861da177e4SLinus Torvalds 
38796fc8e8dSGreg Kroah-Hartman 		dev_dbg(dev, "%s - transmitting data (frame n)\n", __func__);
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds 		length = ((priv->wrfilled - priv->wrsent) > port->bulk_out_size) ?
3901da177e4SLinus Torvalds 			port->bulk_out_size : (priv->wrfilled - priv->wrsent);
3911da177e4SLinus Torvalds 
392b9c52f15SAlan Cox 		memcpy(port->write_urb->transfer_buffer,
393b9c52f15SAlan Cox 					priv->wrbuf + priv->wrsent, length);
3941da177e4SLinus Torvalds 		priv->wrsent += length;
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds 		/* set up our urb */
397fd11961aSJohan Hovold 		port->write_urb->transfer_buffer_length = length;
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds 		/* send the data out the bulk port */
4001da177e4SLinus Torvalds 		result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
4011da177e4SLinus Torvalds 		if (result) {
40296fc8e8dSGreg Kroah-Hartman 			dev_err(dev, "%s - failed submitting write urb, error %d\n",
403b9c52f15SAlan Cox 				__func__, result);
4041da177e4SLinus Torvalds 			/* Throw away data. No better idea what to do with it. */
4051da177e4SLinus Torvalds 			priv->wrfilled = 0;
4061da177e4SLinus Torvalds 			priv->wrsent = 0;
4071da177e4SLinus Torvalds 			goto exit;
4081da177e4SLinus Torvalds 		}
4091da177e4SLinus Torvalds 
41096fc8e8dSGreg Kroah-Hartman 		dev_dbg(dev, "%s - priv->wrsent=%d\n", __func__, priv->wrsent);
41196fc8e8dSGreg Kroah-Hartman 		dev_dbg(dev, "%s - priv->wrfilled=%d\n", __func__, priv->wrfilled);
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds 		blksize = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
4141da177e4SLinus Torvalds 
415b9c52f15SAlan Cox 		if (priv->wrsent >= priv->wrfilled ||
416b9c52f15SAlan Cox 					priv->wrsent >= blksize) {
41796fc8e8dSGreg Kroah-Hartman 			dev_dbg(dev, "%s - buffer cleaned\n", __func__);
4181da177e4SLinus Torvalds 			memset(priv->wrbuf, 0, sizeof(priv->wrbuf));
4191da177e4SLinus Torvalds 			priv->wrfilled = 0;
4201da177e4SLinus Torvalds 			priv->wrsent = 0;
4211da177e4SLinus Torvalds 		}
4221da177e4SLinus Torvalds 	}
4231da177e4SLinus Torvalds 
4241da177e4SLinus Torvalds exit:
4251da177e4SLinus Torvalds 	spin_unlock(&priv->lock);
426cf2c7481SPete Zaitcev 	usb_serial_port_softint(port);
4271da177e4SLinus Torvalds }
4281da177e4SLinus Torvalds 
42968e24113SGreg Kroah-Hartman module_usb_serial_driver(serial_drivers, id_table);
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds MODULE_AUTHOR(DRIVER_AUTHOR);
4321da177e4SLinus Torvalds MODULE_DESCRIPTION(DRIVER_DESC);
4331da177e4SLinus Torvalds MODULE_LICENSE("GPL");
434