xref: /linux/drivers/usb/serial/cyberjack.c (revision c5d1448fa353242635fa3e1fed6ab4558e0e7d9a)
15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  REINER SCT cyberJack pinpad/e-com USB Chipcard Reader Driver
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  Copyright (C) 2001  REINER SCT
61da177e4SLinus Torvalds  *  Author: Matthias Bruestle
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *  Contact: support@reiner-sct.com (see MAINTAINERS)
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *  This program is largely derived from work by the linux-usb group
111da177e4SLinus Torvalds  *  and associated source files.  Please see the usb/serial files for
121da177e4SLinus Torvalds  *  individual credits and copyrights.
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  *  Thanks to Greg Kroah-Hartman (greg@kroah.com) for his help and
151da177e4SLinus Torvalds  *  patience.
161da177e4SLinus Torvalds  *
171da177e4SLinus Torvalds  *  In case of problems, please write to the contact e-mail address
181da177e4SLinus Torvalds  *  mentioned above.
191da177e4SLinus Torvalds  *
201da177e4SLinus Torvalds  *  Please note that later models of the cyberjack reader family are
211da177e4SLinus Torvalds  *  supported by a libusb-based userspace device driver.
221da177e4SLinus Torvalds  *
231da177e4SLinus Torvalds  *  Homepage: http://www.reiner-sct.de/support/treiber_cyberjack.php#linux
241da177e4SLinus Torvalds  */
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds 
271da177e4SLinus Torvalds #include <linux/kernel.h>
281da177e4SLinus Torvalds #include <linux/errno.h>
291da177e4SLinus Torvalds #include <linux/slab.h>
301da177e4SLinus Torvalds #include <linux/tty.h>
311da177e4SLinus Torvalds #include <linux/tty_driver.h>
321da177e4SLinus Torvalds #include <linux/tty_flip.h>
331da177e4SLinus Torvalds #include <linux/module.h>
341da177e4SLinus Torvalds #include <linux/spinlock.h>
35b9c52f15SAlan Cox #include <linux/uaccess.h>
361da177e4SLinus Torvalds #include <linux/usb.h>
37a969888cSGreg Kroah-Hartman #include <linux/usb/serial.h>
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds #define CYBERJACK_LOCAL_BUF_SIZE 32
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds #define DRIVER_AUTHOR "Matthias Bruestle"
421da177e4SLinus Torvalds #define DRIVER_DESC "REINER SCT cyberJack pinpad/e-com USB Chipcard Reader Driver"
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds #define CYBERJACK_VENDOR_ID	0x0C4B
461da177e4SLinus Torvalds #define CYBERJACK_PRODUCT_ID	0x0100
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds /* Function prototypes */
49a9556040SJohan Hovold static int cyberjack_port_probe(struct usb_serial_port *port);
50*c5d1448fSUwe Kleine-König static void cyberjack_port_remove(struct usb_serial_port *port);
5195da310eSAlan Cox static int  cyberjack_open(struct tty_struct *tty,
52a509a7e4SAlan Cox 	struct usb_serial_port *port);
53335f8514SAlan Cox static void cyberjack_close(struct usb_serial_port *port);
5495da310eSAlan Cox static int cyberjack_write(struct tty_struct *tty,
5595da310eSAlan Cox 	struct usb_serial_port *port, const unsigned char *buf, int count);
5695da310eSAlan Cox static int cyberjack_write_room(struct tty_struct *tty);
577d12e780SDavid Howells static void cyberjack_read_int_callback(struct urb *urb);
587d12e780SDavid Howells static void cyberjack_read_bulk_callback(struct urb *urb);
597d12e780SDavid Howells static void cyberjack_write_bulk_callback(struct urb *urb);
601da177e4SLinus Torvalds 
617d40d7e8SNémeth Márton static const struct usb_device_id id_table[] = {
621da177e4SLinus Torvalds 	{ USB_DEVICE(CYBERJACK_VENDOR_ID, CYBERJACK_PRODUCT_ID) },
631da177e4SLinus Torvalds 	{ }			/* Terminating entry */
641da177e4SLinus Torvalds };
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds MODULE_DEVICE_TABLE(usb, id_table);
671da177e4SLinus Torvalds 
68ea65370dSGreg Kroah-Hartman static struct usb_serial_driver cyberjack_device = {
6918fcac35SGreg Kroah-Hartman 	.driver = {
701da177e4SLinus Torvalds 		.owner =	THIS_MODULE,
71269bda1cSGreg Kroah-Hartman 		.name =		"cyberjack",
7218fcac35SGreg Kroah-Hartman 	},
73269bda1cSGreg Kroah-Hartman 	.description =		"Reiner SCT Cyberjack USB card reader",
741da177e4SLinus Torvalds 	.id_table =		id_table,
751da177e4SLinus Torvalds 	.num_ports =		1,
76d183b9b4SJohan Hovold 	.num_bulk_out =		1,
77a9556040SJohan Hovold 	.port_probe =		cyberjack_port_probe,
78a9556040SJohan Hovold 	.port_remove =		cyberjack_port_remove,
791da177e4SLinus Torvalds 	.open =			cyberjack_open,
801da177e4SLinus Torvalds 	.close =		cyberjack_close,
811da177e4SLinus Torvalds 	.write =		cyberjack_write,
821da177e4SLinus Torvalds 	.write_room =		cyberjack_write_room,
831da177e4SLinus Torvalds 	.read_int_callback =	cyberjack_read_int_callback,
841da177e4SLinus Torvalds 	.read_bulk_callback =	cyberjack_read_bulk_callback,
851da177e4SLinus Torvalds 	.write_bulk_callback =	cyberjack_write_bulk_callback,
861da177e4SLinus Torvalds };
871da177e4SLinus Torvalds 
8808a4f6bcSAlan Stern static struct usb_serial_driver * const serial_drivers[] = {
8908a4f6bcSAlan Stern 	&cyberjack_device, NULL
9008a4f6bcSAlan Stern };
9108a4f6bcSAlan Stern 
921da177e4SLinus Torvalds struct cyberjack_private {
931da177e4SLinus Torvalds 	spinlock_t	lock;		/* Lock for SMP */
941da177e4SLinus Torvalds 	short		rdtodo;		/* Bytes still to read */
951da177e4SLinus Torvalds 	unsigned char	wrbuf[5*64];	/* Buffer for collecting data to write */
961da177e4SLinus Torvalds 	short		wrfilled;	/* Overall data size we already got */
971da177e4SLinus Torvalds 	short		wrsent;		/* Data already sent */
981da177e4SLinus Torvalds };
991da177e4SLinus Torvalds 
100a9556040SJohan Hovold static int cyberjack_port_probe(struct usb_serial_port *port)
1011da177e4SLinus Torvalds {
1021da177e4SLinus Torvalds 	struct cyberjack_private *priv;
103a9556040SJohan Hovold 	int result;
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds 	priv = kmalloc(sizeof(struct cyberjack_private), GFP_KERNEL);
1061da177e4SLinus Torvalds 	if (!priv)
1071da177e4SLinus Torvalds 		return -ENOMEM;
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds 	spin_lock_init(&priv->lock);
1101da177e4SLinus Torvalds 	priv->rdtodo = 0;
1111da177e4SLinus Torvalds 	priv->wrfilled = 0;
1121da177e4SLinus Torvalds 	priv->wrsent = 0;
1131da177e4SLinus Torvalds 
114a9556040SJohan Hovold 	usb_set_serial_port_data(port, priv);
1151da177e4SLinus Torvalds 
116a9556040SJohan Hovold 	result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
1171da177e4SLinus Torvalds 	if (result)
118a9556040SJohan Hovold 		dev_err(&port->dev, "usb_submit_urb(read int) failed\n");
119a9556040SJohan Hovold 
120a9556040SJohan Hovold 	return 0;
1211da177e4SLinus Torvalds }
1221da177e4SLinus Torvalds 
123*c5d1448fSUwe Kleine-König static void cyberjack_port_remove(struct usb_serial_port *port)
124a9556040SJohan Hovold {
125a9556040SJohan Hovold 	struct cyberjack_private *priv;
126a9556040SJohan Hovold 
12730d9d42eSJohan Hovold 	usb_kill_urb(port->interrupt_in_urb);
12830d9d42eSJohan Hovold 
129a9556040SJohan Hovold 	priv = usb_get_serial_port_data(port);
130a9556040SJohan Hovold 	kfree(priv);
1311da177e4SLinus Torvalds }
1321da177e4SLinus Torvalds 
13395da310eSAlan Cox static int  cyberjack_open(struct tty_struct *tty,
134a509a7e4SAlan Cox 					struct usb_serial_port *port)
1351da177e4SLinus Torvalds {
1361da177e4SLinus Torvalds 	struct cyberjack_private *priv;
1371da177e4SLinus Torvalds 	unsigned long flags;
1381da177e4SLinus Torvalds 
13996fc8e8dSGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - usb_clear_halt\n", __func__);
1401da177e4SLinus Torvalds 	usb_clear_halt(port->serial->dev, port->write_urb->pipe);
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds 	priv = usb_get_serial_port_data(port);
1431da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->lock, flags);
1441da177e4SLinus Torvalds 	priv->rdtodo = 0;
1451da177e4SLinus Torvalds 	priv->wrfilled = 0;
1461da177e4SLinus Torvalds 	priv->wrsent = 0;
1471da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->lock, flags);
1481da177e4SLinus Torvalds 
1490f3083afSMathieu OTHACEHE 	return 0;
1501da177e4SLinus Torvalds }
1511da177e4SLinus Torvalds 
152335f8514SAlan Cox static void cyberjack_close(struct usb_serial_port *port)
1531da177e4SLinus Torvalds {
1541da177e4SLinus Torvalds 	usb_kill_urb(port->write_urb);
1551da177e4SLinus Torvalds 	usb_kill_urb(port->read_urb);
1561da177e4SLinus Torvalds }
1571da177e4SLinus Torvalds 
15895da310eSAlan Cox static int cyberjack_write(struct tty_struct *tty,
15995da310eSAlan Cox 	struct usb_serial_port *port, const unsigned char *buf, int count)
1601da177e4SLinus Torvalds {
16196fc8e8dSGreg Kroah-Hartman 	struct device *dev = &port->dev;
1621da177e4SLinus Torvalds 	struct cyberjack_private *priv = usb_get_serial_port_data(port);
1631da177e4SLinus Torvalds 	unsigned long flags;
1641da177e4SLinus Torvalds 	int result;
1651da177e4SLinus Torvalds 	int wrexpected;
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds 	if (count == 0) {
16896fc8e8dSGreg Kroah-Hartman 		dev_dbg(dev, "%s - write request of 0 bytes\n", __func__);
169a5b6f60cSAlan Cox 		return 0;
1701da177e4SLinus Torvalds 	}
1711da177e4SLinus Torvalds 
172c1cac10cSJohan Hovold 	if (!test_and_clear_bit(0, &port->write_urbs_free)) {
17396fc8e8dSGreg Kroah-Hartman 		dev_dbg(dev, "%s - already writing\n", __func__);
174507ca9bcSGreg Kroah-Hartman 		return 0;
1751da177e4SLinus Torvalds 	}
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->lock, flags);
1781da177e4SLinus Torvalds 
179b9c52f15SAlan Cox 	if (count+priv->wrfilled > sizeof(priv->wrbuf)) {
1801da177e4SLinus Torvalds 		/* To much data for buffer. Reset buffer. */
1811da177e4SLinus Torvalds 		priv->wrfilled = 0;
182a5b6f60cSAlan Cox 		spin_unlock_irqrestore(&priv->lock, flags);
183c1cac10cSJohan Hovold 		set_bit(0, &port->write_urbs_free);
184a5b6f60cSAlan Cox 		return 0;
1851da177e4SLinus Torvalds 	}
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds 	/* Copy data */
1881da177e4SLinus Torvalds 	memcpy(priv->wrbuf + priv->wrfilled, buf, count);
1891da177e4SLinus Torvalds 
19059d33f2fSGreg Kroah-Hartman 	usb_serial_debug_data(dev, __func__, count, priv->wrbuf + priv->wrfilled);
1911da177e4SLinus Torvalds 	priv->wrfilled += count;
1921da177e4SLinus Torvalds 
1931da177e4SLinus Torvalds 	if (priv->wrfilled >= 3) {
1941da177e4SLinus Torvalds 		wrexpected = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
19596fc8e8dSGreg Kroah-Hartman 		dev_dbg(dev, "%s - expected data: %d\n", __func__, wrexpected);
196b9c52f15SAlan Cox 	} else
1971da177e4SLinus Torvalds 		wrexpected = sizeof(priv->wrbuf);
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds 	if (priv->wrfilled >= wrexpected) {
2001da177e4SLinus Torvalds 		/* We have enough data to begin transmission */
2011da177e4SLinus Torvalds 		int length;
2021da177e4SLinus Torvalds 
20396fc8e8dSGreg Kroah-Hartman 		dev_dbg(dev, "%s - transmitting data (frame 1)\n", __func__);
204b9c52f15SAlan Cox 		length = (wrexpected > port->bulk_out_size) ?
205b9c52f15SAlan Cox 					port->bulk_out_size : wrexpected;
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds 		memcpy(port->write_urb->transfer_buffer, priv->wrbuf, length);
2081da177e4SLinus Torvalds 		priv->wrsent = length;
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds 		/* set up our urb */
211fd11961aSJohan Hovold 		port->write_urb->transfer_buffer_length = length;
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds 		/* send the data out the bulk port */
2141da177e4SLinus Torvalds 		result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
2151da177e4SLinus Torvalds 		if (result) {
216194343d9SGreg Kroah-Hartman 			dev_err(&port->dev,
217d9a38a87SJohan Hovold 				"%s - failed submitting write urb, error %d\n",
218b9c52f15SAlan Cox 				__func__, result);
2191da177e4SLinus Torvalds 			/* Throw away data. No better idea what to do with it. */
2201da177e4SLinus Torvalds 			priv->wrfilled = 0;
2211da177e4SLinus Torvalds 			priv->wrsent = 0;
2221da177e4SLinus Torvalds 			spin_unlock_irqrestore(&priv->lock, flags);
223c1cac10cSJohan Hovold 			set_bit(0, &port->write_urbs_free);
2241da177e4SLinus Torvalds 			return 0;
2251da177e4SLinus Torvalds 		}
2261da177e4SLinus Torvalds 
22796fc8e8dSGreg Kroah-Hartman 		dev_dbg(dev, "%s - priv->wrsent=%d\n", __func__, priv->wrsent);
22896fc8e8dSGreg Kroah-Hartman 		dev_dbg(dev, "%s - priv->wrfilled=%d\n", __func__, priv->wrfilled);
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds 		if (priv->wrsent >= priv->wrfilled) {
23196fc8e8dSGreg Kroah-Hartman 			dev_dbg(dev, "%s - buffer cleaned\n", __func__);
2321da177e4SLinus Torvalds 			memset(priv->wrbuf, 0, sizeof(priv->wrbuf));
2331da177e4SLinus Torvalds 			priv->wrfilled = 0;
2341da177e4SLinus Torvalds 			priv->wrsent = 0;
2351da177e4SLinus Torvalds 		}
2361da177e4SLinus Torvalds 	}
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->lock, flags);
2391da177e4SLinus Torvalds 
240b9c52f15SAlan Cox 	return count;
2411da177e4SLinus Torvalds }
2421da177e4SLinus Torvalds 
24395da310eSAlan Cox static int cyberjack_write_room(struct tty_struct *tty)
2441da177e4SLinus Torvalds {
245a5b6f60cSAlan Cox 	/* FIXME: .... */
2461da177e4SLinus Torvalds 	return CYBERJACK_LOCAL_BUF_SIZE;
2471da177e4SLinus Torvalds }
2481da177e4SLinus Torvalds 
2497d12e780SDavid Howells static void cyberjack_read_int_callback(struct urb *urb)
2501da177e4SLinus Torvalds {
251cdc97792SMing Lei 	struct usb_serial_port *port = urb->context;
2521da177e4SLinus Torvalds 	struct cyberjack_private *priv = usb_get_serial_port_data(port);
25396fc8e8dSGreg Kroah-Hartman 	struct device *dev = &port->dev;
2541da177e4SLinus Torvalds 	unsigned char *data = urb->transfer_buffer;
2557dcc85cdSGreg Kroah-Hartman 	int status = urb->status;
256c75d18ccSJohn Ogness 	unsigned long flags;
2571da177e4SLinus Torvalds 	int result;
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds 	/* the urb might have been killed. */
2607dcc85cdSGreg Kroah-Hartman 	if (status)
2611da177e4SLinus Torvalds 		return;
2621da177e4SLinus Torvalds 
26359d33f2fSGreg Kroah-Hartman 	usb_serial_debug_data(dev, __func__, urb->actual_length, data);
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds 	/* React only to interrupts signaling a bulk_in transfer */
266b9c52f15SAlan Cox 	if (urb->actual_length == 4 && data[0] == 0x01) {
2671da177e4SLinus Torvalds 		short old_rdtodo;
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds 		/* This is a announcement of coming bulk_ins. */
2701da177e4SLinus Torvalds 		unsigned short size = ((unsigned short)data[3]<<8)+data[2]+3;
2711da177e4SLinus Torvalds 
272c75d18ccSJohn Ogness 		spin_lock_irqsave(&priv->lock, flags);
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds 		old_rdtodo = priv->rdtodo;
2751da177e4SLinus Torvalds 
2765287bf72SDan Carpenter 		if (old_rdtodo > SHRT_MAX - size) {
27719c64e73SColin Ian King 			dev_dbg(dev, "Too many bulk_in urbs to do.\n");
278c75d18ccSJohn Ogness 			spin_unlock_irqrestore(&priv->lock, flags);
2791da177e4SLinus Torvalds 			goto resubmit;
2801da177e4SLinus Torvalds 		}
2811da177e4SLinus Torvalds 
282cd8c5053SRahul Bedarkar 		/* "+=" is probably more fault tolerant than "=" */
2831da177e4SLinus Torvalds 		priv->rdtodo += size;
2841da177e4SLinus Torvalds 
28596fc8e8dSGreg Kroah-Hartman 		dev_dbg(dev, "%s - rdtodo: %d\n", __func__, priv->rdtodo);
2861da177e4SLinus Torvalds 
287c75d18ccSJohn Ogness 		spin_unlock_irqrestore(&priv->lock, flags);
2881da177e4SLinus Torvalds 
2891da177e4SLinus Torvalds 		if (!old_rdtodo) {
2901da177e4SLinus Torvalds 			result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
2911da177e4SLinus Torvalds 			if (result)
29296fc8e8dSGreg Kroah-Hartman 				dev_err(dev, "%s - failed resubmitting read urb, error %d\n",
293194343d9SGreg Kroah-Hartman 					__func__, result);
29496fc8e8dSGreg Kroah-Hartman 			dev_dbg(dev, "%s - usb_submit_urb(read urb)\n", __func__);
2951da177e4SLinus Torvalds 		}
2961da177e4SLinus Torvalds 	}
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds resubmit:
2991da177e4SLinus Torvalds 	result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
3001da177e4SLinus Torvalds 	if (result)
301194343d9SGreg Kroah-Hartman 		dev_err(&port->dev, "usb_submit_urb(read int) failed\n");
30296fc8e8dSGreg Kroah-Hartman 	dev_dbg(dev, "%s - usb_submit_urb(int urb)\n", __func__);
3031da177e4SLinus Torvalds }
3041da177e4SLinus Torvalds 
3057d12e780SDavid Howells static void cyberjack_read_bulk_callback(struct urb *urb)
3061da177e4SLinus Torvalds {
307cdc97792SMing Lei 	struct usb_serial_port *port = urb->context;
3081da177e4SLinus Torvalds 	struct cyberjack_private *priv = usb_get_serial_port_data(port);
30996fc8e8dSGreg Kroah-Hartman 	struct device *dev = &port->dev;
3101da177e4SLinus Torvalds 	unsigned char *data = urb->transfer_buffer;
311c75d18ccSJohn Ogness 	unsigned long flags;
3121da177e4SLinus Torvalds 	short todo;
3131da177e4SLinus Torvalds 	int result;
3147dcc85cdSGreg Kroah-Hartman 	int status = urb->status;
3151da177e4SLinus Torvalds 
31659d33f2fSGreg Kroah-Hartman 	usb_serial_debug_data(dev, __func__, urb->actual_length, data);
3177dcc85cdSGreg Kroah-Hartman 	if (status) {
31896fc8e8dSGreg Kroah-Hartman 		dev_dbg(dev, "%s - nonzero read bulk status received: %d\n",
319441b62c1SHarvey Harrison 			__func__, status);
3201da177e4SLinus Torvalds 		return;
3211da177e4SLinus Torvalds 	}
3221da177e4SLinus Torvalds 
3231da177e4SLinus Torvalds 	if (urb->actual_length) {
32405c7cd39SJiri Slaby 		tty_insert_flip_string(&port->port, data, urb->actual_length);
3252e124b4aSJiri Slaby 		tty_flip_buffer_push(&port->port);
3261da177e4SLinus Torvalds 	}
3271da177e4SLinus Torvalds 
328c75d18ccSJohn Ogness 	spin_lock_irqsave(&priv->lock, flags);
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds 	/* Reduce urbs to do by one. */
3311da177e4SLinus Torvalds 	priv->rdtodo -= urb->actual_length;
3321da177e4SLinus Torvalds 	/* Just to be sure */
333b9c52f15SAlan Cox 	if (priv->rdtodo < 0)
334b9c52f15SAlan Cox 		priv->rdtodo = 0;
3351da177e4SLinus Torvalds 	todo = priv->rdtodo;
3361da177e4SLinus Torvalds 
337c75d18ccSJohn Ogness 	spin_unlock_irqrestore(&priv->lock, flags);
3381da177e4SLinus Torvalds 
33996fc8e8dSGreg Kroah-Hartman 	dev_dbg(dev, "%s - rdtodo: %d\n", __func__, todo);
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds 	/* Continue to read if we have still urbs to do. */
3421da177e4SLinus Torvalds 	if (todo /* || (urb->actual_length==port->bulk_in_endpointAddress)*/) {
3431da177e4SLinus Torvalds 		result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
3441da177e4SLinus Torvalds 		if (result)
34596fc8e8dSGreg Kroah-Hartman 			dev_err(dev, "%s - failed resubmitting read urb, error %d\n",
34696fc8e8dSGreg Kroah-Hartman 				__func__, result);
34796fc8e8dSGreg Kroah-Hartman 		dev_dbg(dev, "%s - usb_submit_urb(read urb)\n", __func__);
3481da177e4SLinus Torvalds 	}
3491da177e4SLinus Torvalds }
3501da177e4SLinus Torvalds 
3517d12e780SDavid Howells static void cyberjack_write_bulk_callback(struct urb *urb)
3521da177e4SLinus Torvalds {
353cdc97792SMing Lei 	struct usb_serial_port *port = urb->context;
3541da177e4SLinus Torvalds 	struct cyberjack_private *priv = usb_get_serial_port_data(port);
35596fc8e8dSGreg Kroah-Hartman 	struct device *dev = &port->dev;
3567dcc85cdSGreg Kroah-Hartman 	int status = urb->status;
357c75d18ccSJohn Ogness 	unsigned long flags;
358985616f0SJohan Hovold 	bool resubmitted = false;
3591da177e4SLinus Torvalds 
3607dcc85cdSGreg Kroah-Hartman 	if (status) {
36196fc8e8dSGreg Kroah-Hartman 		dev_dbg(dev, "%s - nonzero write bulk status received: %d\n",
362441b62c1SHarvey Harrison 			__func__, status);
363985616f0SJohan Hovold 		set_bit(0, &port->write_urbs_free);
3641da177e4SLinus Torvalds 		return;
3651da177e4SLinus Torvalds 	}
3661da177e4SLinus Torvalds 
367c75d18ccSJohn Ogness 	spin_lock_irqsave(&priv->lock, flags);
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds 	/* only do something if we have more data to send */
3701da177e4SLinus Torvalds 	if (priv->wrfilled) {
3711da177e4SLinus Torvalds 		int length, blksize, result;
3721da177e4SLinus Torvalds 
37396fc8e8dSGreg Kroah-Hartman 		dev_dbg(dev, "%s - transmitting data (frame n)\n", __func__);
3741da177e4SLinus Torvalds 
3751da177e4SLinus Torvalds 		length = ((priv->wrfilled - priv->wrsent) > port->bulk_out_size) ?
3761da177e4SLinus Torvalds 			port->bulk_out_size : (priv->wrfilled - priv->wrsent);
3771da177e4SLinus Torvalds 
378b9c52f15SAlan Cox 		memcpy(port->write_urb->transfer_buffer,
379b9c52f15SAlan Cox 					priv->wrbuf + priv->wrsent, length);
3801da177e4SLinus Torvalds 		priv->wrsent += length;
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds 		/* set up our urb */
383fd11961aSJohan Hovold 		port->write_urb->transfer_buffer_length = length;
3841da177e4SLinus Torvalds 
3851da177e4SLinus Torvalds 		/* send the data out the bulk port */
3861da177e4SLinus Torvalds 		result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
3871da177e4SLinus Torvalds 		if (result) {
38896fc8e8dSGreg Kroah-Hartman 			dev_err(dev, "%s - failed submitting write urb, error %d\n",
389b9c52f15SAlan Cox 				__func__, result);
3901da177e4SLinus Torvalds 			/* Throw away data. No better idea what to do with it. */
3911da177e4SLinus Torvalds 			priv->wrfilled = 0;
3921da177e4SLinus Torvalds 			priv->wrsent = 0;
3931da177e4SLinus Torvalds 			goto exit;
3941da177e4SLinus Torvalds 		}
3951da177e4SLinus Torvalds 
396985616f0SJohan Hovold 		resubmitted = true;
397985616f0SJohan Hovold 
39896fc8e8dSGreg Kroah-Hartman 		dev_dbg(dev, "%s - priv->wrsent=%d\n", __func__, priv->wrsent);
39996fc8e8dSGreg Kroah-Hartman 		dev_dbg(dev, "%s - priv->wrfilled=%d\n", __func__, priv->wrfilled);
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds 		blksize = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
4021da177e4SLinus Torvalds 
403b9c52f15SAlan Cox 		if (priv->wrsent >= priv->wrfilled ||
404b9c52f15SAlan Cox 					priv->wrsent >= blksize) {
40596fc8e8dSGreg Kroah-Hartman 			dev_dbg(dev, "%s - buffer cleaned\n", __func__);
4061da177e4SLinus Torvalds 			memset(priv->wrbuf, 0, sizeof(priv->wrbuf));
4071da177e4SLinus Torvalds 			priv->wrfilled = 0;
4081da177e4SLinus Torvalds 			priv->wrsent = 0;
4091da177e4SLinus Torvalds 		}
4101da177e4SLinus Torvalds 	}
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds exit:
413c75d18ccSJohn Ogness 	spin_unlock_irqrestore(&priv->lock, flags);
414985616f0SJohan Hovold 	if (!resubmitted)
415985616f0SJohan Hovold 		set_bit(0, &port->write_urbs_free);
416cf2c7481SPete Zaitcev 	usb_serial_port_softint(port);
4171da177e4SLinus Torvalds }
4181da177e4SLinus Torvalds 
41968e24113SGreg Kroah-Hartman module_usb_serial_driver(serial_drivers, id_table);
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds MODULE_AUTHOR(DRIVER_AUTHOR);
4221da177e4SLinus Torvalds MODULE_DESCRIPTION(DRIVER_DESC);
4231da177e4SLinus Torvalds MODULE_LICENSE("GPL");
424