xref: /linux/drivers/usb/serial/cyberjack.c (revision 7d40d7e85a25e01948bcb4dc3eda1355af318337)
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 static int debug;
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds /*
491da177e4SLinus Torvalds  * Version Information
501da177e4SLinus Torvalds  */
511da177e4SLinus Torvalds #define DRIVER_VERSION "v1.01"
521da177e4SLinus Torvalds #define DRIVER_AUTHOR "Matthias Bruestle"
531da177e4SLinus Torvalds #define DRIVER_DESC "REINER SCT cyberJack pinpad/e-com USB Chipcard Reader Driver"
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds #define CYBERJACK_VENDOR_ID	0x0C4B
571da177e4SLinus Torvalds #define CYBERJACK_PRODUCT_ID	0x0100
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds /* Function prototypes */
601da177e4SLinus Torvalds static int cyberjack_startup(struct usb_serial *serial);
61f9c99bb8SAlan Stern static void cyberjack_disconnect(struct usb_serial *serial);
62f9c99bb8SAlan Stern static void cyberjack_release(struct usb_serial *serial);
6395da310eSAlan Cox static int  cyberjack_open(struct tty_struct *tty,
64a509a7e4SAlan Cox 	struct usb_serial_port *port);
65335f8514SAlan Cox static void cyberjack_close(struct usb_serial_port *port);
6695da310eSAlan Cox static int cyberjack_write(struct tty_struct *tty,
6795da310eSAlan Cox 	struct usb_serial_port *port, const unsigned char *buf, int count);
6895da310eSAlan Cox static int cyberjack_write_room(struct tty_struct *tty);
697d12e780SDavid Howells static void cyberjack_read_int_callback(struct urb *urb);
707d12e780SDavid Howells static void cyberjack_read_bulk_callback(struct urb *urb);
717d12e780SDavid Howells static void cyberjack_write_bulk_callback(struct urb *urb);
721da177e4SLinus Torvalds 
73*7d40d7e8SNémeth Márton static const struct usb_device_id id_table[] = {
741da177e4SLinus Torvalds 	{ USB_DEVICE(CYBERJACK_VENDOR_ID, CYBERJACK_PRODUCT_ID) },
751da177e4SLinus Torvalds 	{ }			/* Terminating entry */
761da177e4SLinus Torvalds };
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds MODULE_DEVICE_TABLE(usb, id_table);
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds static struct usb_driver cyberjack_driver = {
811da177e4SLinus Torvalds 	.name =		"cyberjack",
821da177e4SLinus Torvalds 	.probe =	usb_serial_probe,
831da177e4SLinus Torvalds 	.disconnect =	usb_serial_disconnect,
841da177e4SLinus Torvalds 	.id_table =	id_table,
85ba9dc657SGreg Kroah-Hartman 	.no_dynamic_id = 	1,
861da177e4SLinus Torvalds };
871da177e4SLinus Torvalds 
88ea65370dSGreg Kroah-Hartman static struct usb_serial_driver cyberjack_device = {
8918fcac35SGreg Kroah-Hartman 	.driver = {
901da177e4SLinus Torvalds 		.owner =	THIS_MODULE,
91269bda1cSGreg Kroah-Hartman 		.name =		"cyberjack",
9218fcac35SGreg Kroah-Hartman 	},
93269bda1cSGreg Kroah-Hartman 	.description =		"Reiner SCT Cyberjack USB card reader",
94d9b1b787SJohannes Hölzl 	.usb_driver = 		&cyberjack_driver,
951da177e4SLinus Torvalds 	.id_table =		id_table,
961da177e4SLinus Torvalds 	.num_ports =		1,
971da177e4SLinus Torvalds 	.attach =		cyberjack_startup,
98f9c99bb8SAlan Stern 	.disconnect =		cyberjack_disconnect,
99f9c99bb8SAlan Stern 	.release =		cyberjack_release,
1001da177e4SLinus Torvalds 	.open =			cyberjack_open,
1011da177e4SLinus Torvalds 	.close =		cyberjack_close,
1021da177e4SLinus Torvalds 	.write =		cyberjack_write,
1031da177e4SLinus Torvalds 	.write_room =		cyberjack_write_room,
1041da177e4SLinus Torvalds 	.read_int_callback =	cyberjack_read_int_callback,
1051da177e4SLinus Torvalds 	.read_bulk_callback =	cyberjack_read_bulk_callback,
1061da177e4SLinus Torvalds 	.write_bulk_callback =	cyberjack_write_bulk_callback,
1071da177e4SLinus Torvalds };
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds struct cyberjack_private {
1101da177e4SLinus Torvalds 	spinlock_t	lock;		/* Lock for SMP */
1111da177e4SLinus Torvalds 	short		rdtodo;		/* Bytes still to read */
1121da177e4SLinus Torvalds 	unsigned char	wrbuf[5*64];	/* Buffer for collecting data to write */
1131da177e4SLinus Torvalds 	short		wrfilled;	/* Overall data size we already got */
1141da177e4SLinus Torvalds 	short		wrsent;		/* Data already sent */
1151da177e4SLinus Torvalds };
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds /* do some startup allocations not currently performed by usb_serial_probe() */
1181da177e4SLinus Torvalds static int cyberjack_startup(struct usb_serial *serial)
1191da177e4SLinus Torvalds {
1201da177e4SLinus Torvalds 	struct cyberjack_private *priv;
1211da177e4SLinus Torvalds 	int i;
1221da177e4SLinus Torvalds 
123441b62c1SHarvey Harrison 	dbg("%s", __func__);
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds 	/* allocate the private data structure */
1261da177e4SLinus Torvalds 	priv = kmalloc(sizeof(struct cyberjack_private), GFP_KERNEL);
1271da177e4SLinus Torvalds 	if (!priv)
1281da177e4SLinus Torvalds 		return -ENOMEM;
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds 	/* set initial values */
1311da177e4SLinus Torvalds 	spin_lock_init(&priv->lock);
1321da177e4SLinus Torvalds 	priv->rdtodo = 0;
1331da177e4SLinus Torvalds 	priv->wrfilled = 0;
1341da177e4SLinus Torvalds 	priv->wrsent = 0;
1351da177e4SLinus Torvalds 	usb_set_serial_port_data(serial->port[0], priv);
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds 	init_waitqueue_head(&serial->port[0]->write_wait);
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds 	for (i = 0; i < serial->num_ports; ++i) {
1401da177e4SLinus Torvalds 		int result;
1411da177e4SLinus Torvalds 		serial->port[i]->interrupt_in_urb->dev = serial->dev;
1421da177e4SLinus Torvalds 		result = usb_submit_urb(serial->port[i]->interrupt_in_urb,
1431da177e4SLinus Torvalds 					GFP_KERNEL);
1441da177e4SLinus Torvalds 		if (result)
145194343d9SGreg Kroah-Hartman 			dev_err(&serial->dev->dev,
146194343d9SGreg Kroah-Hartman 				"usb_submit_urb(read int) failed\n");
147441b62c1SHarvey Harrison 		dbg("%s - usb_submit_urb(int urb)", __func__);
1481da177e4SLinus Torvalds 	}
1491da177e4SLinus Torvalds 
150b9c52f15SAlan Cox 	return 0;
1511da177e4SLinus Torvalds }
1521da177e4SLinus Torvalds 
153f9c99bb8SAlan Stern static void cyberjack_disconnect(struct usb_serial *serial)
154f9c99bb8SAlan Stern {
155f9c99bb8SAlan Stern 	int i;
156f9c99bb8SAlan Stern 
157f9c99bb8SAlan Stern 	dbg("%s", __func__);
158f9c99bb8SAlan Stern 
159f9c99bb8SAlan Stern 	for (i = 0; i < serial->num_ports; ++i)
160f9c99bb8SAlan Stern 		usb_kill_urb(serial->port[i]->interrupt_in_urb);
161f9c99bb8SAlan Stern }
162f9c99bb8SAlan Stern 
163f9c99bb8SAlan Stern static void cyberjack_release(struct usb_serial *serial)
1641da177e4SLinus Torvalds {
1651da177e4SLinus Torvalds 	int i;
1661da177e4SLinus Torvalds 
167441b62c1SHarvey Harrison 	dbg("%s", __func__);
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds 	for (i = 0; i < serial->num_ports; ++i) {
1701da177e4SLinus Torvalds 		/* My special items, the standard routines free my urbs */
1711da177e4SLinus Torvalds 		kfree(usb_get_serial_port_data(serial->port[i]));
1721da177e4SLinus Torvalds 	}
1731da177e4SLinus Torvalds }
1741da177e4SLinus Torvalds 
17595da310eSAlan Cox static int  cyberjack_open(struct tty_struct *tty,
176a509a7e4SAlan Cox 					struct usb_serial_port *port)
1771da177e4SLinus Torvalds {
1781da177e4SLinus Torvalds 	struct cyberjack_private *priv;
1791da177e4SLinus Torvalds 	unsigned long flags;
1801da177e4SLinus Torvalds 	int result = 0;
1811da177e4SLinus Torvalds 
182441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, port->number);
1831da177e4SLinus Torvalds 
184441b62c1SHarvey Harrison 	dbg("%s - usb_clear_halt", __func__);
1851da177e4SLinus Torvalds 	usb_clear_halt(port->serial->dev, port->write_urb->pipe);
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds 	priv = usb_get_serial_port_data(port);
1881da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->lock, flags);
1891da177e4SLinus Torvalds 	priv->rdtodo = 0;
1901da177e4SLinus Torvalds 	priv->wrfilled = 0;
1911da177e4SLinus Torvalds 	priv->wrsent = 0;
1921da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->lock, flags);
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds 	return result;
1951da177e4SLinus Torvalds }
1961da177e4SLinus Torvalds 
197335f8514SAlan Cox static void cyberjack_close(struct usb_serial_port *port)
1981da177e4SLinus Torvalds {
199441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, port->number);
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds 	if (port->serial->dev) {
2021da177e4SLinus Torvalds 		/* shutdown any bulk reads that might be going on */
2031da177e4SLinus Torvalds 		usb_kill_urb(port->write_urb);
2041da177e4SLinus Torvalds 		usb_kill_urb(port->read_urb);
2051da177e4SLinus Torvalds 	}
2061da177e4SLinus Torvalds }
2071da177e4SLinus Torvalds 
20895da310eSAlan Cox static int cyberjack_write(struct tty_struct *tty,
20995da310eSAlan Cox 	struct usb_serial_port *port, const unsigned char *buf, int count)
2101da177e4SLinus Torvalds {
2111da177e4SLinus Torvalds 	struct usb_serial *serial = port->serial;
2121da177e4SLinus Torvalds 	struct cyberjack_private *priv = usb_get_serial_port_data(port);
2131da177e4SLinus Torvalds 	unsigned long flags;
2141da177e4SLinus Torvalds 	int result;
2151da177e4SLinus Torvalds 	int wrexpected;
2161da177e4SLinus Torvalds 
217441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, port->number);
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds 	if (count == 0) {
220441b62c1SHarvey Harrison 		dbg("%s - write request of 0 bytes", __func__);
221a5b6f60cSAlan Cox 		return 0;
2221da177e4SLinus Torvalds 	}
2231da177e4SLinus Torvalds 
224e81ee637SPeter Zijlstra 	spin_lock_bh(&port->lock);
225507ca9bcSGreg Kroah-Hartman 	if (port->write_urb_busy) {
226e81ee637SPeter Zijlstra 		spin_unlock_bh(&port->lock);
227441b62c1SHarvey Harrison 		dbg("%s - already writing", __func__);
228507ca9bcSGreg Kroah-Hartman 		return 0;
2291da177e4SLinus Torvalds 	}
230507ca9bcSGreg Kroah-Hartman 	port->write_urb_busy = 1;
231e81ee637SPeter Zijlstra 	spin_unlock_bh(&port->lock);
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->lock, flags);
2341da177e4SLinus Torvalds 
235b9c52f15SAlan Cox 	if (count+priv->wrfilled > sizeof(priv->wrbuf)) {
2361da177e4SLinus Torvalds 		/* To much data for buffer. Reset buffer. */
2371da177e4SLinus Torvalds 		priv->wrfilled = 0;
238507ca9bcSGreg Kroah-Hartman 		port->write_urb_busy = 0;
239a5b6f60cSAlan Cox 		spin_unlock_irqrestore(&priv->lock, flags);
240a5b6f60cSAlan Cox 		return 0;
2411da177e4SLinus Torvalds 	}
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds 	/* Copy data */
2441da177e4SLinus Torvalds 	memcpy(priv->wrbuf + priv->wrfilled, buf, count);
2451da177e4SLinus Torvalds 
246441b62c1SHarvey Harrison 	usb_serial_debug_data(debug, &port->dev, __func__, count,
2471da177e4SLinus Torvalds 		priv->wrbuf + priv->wrfilled);
2481da177e4SLinus Torvalds 	priv->wrfilled += count;
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds 	if (priv->wrfilled >= 3) {
2511da177e4SLinus Torvalds 		wrexpected = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
252441b62c1SHarvey Harrison 		dbg("%s - expected data: %d", __func__, wrexpected);
253b9c52f15SAlan Cox 	} else
2541da177e4SLinus Torvalds 		wrexpected = sizeof(priv->wrbuf);
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds 	if (priv->wrfilled >= wrexpected) {
2571da177e4SLinus Torvalds 		/* We have enough data to begin transmission */
2581da177e4SLinus Torvalds 		int length;
2591da177e4SLinus Torvalds 
260441b62c1SHarvey Harrison 		dbg("%s - transmitting data (frame 1)", __func__);
261b9c52f15SAlan Cox 		length = (wrexpected > port->bulk_out_size) ?
262b9c52f15SAlan Cox 					port->bulk_out_size : wrexpected;
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds 		memcpy(port->write_urb->transfer_buffer, priv->wrbuf, length);
2651da177e4SLinus Torvalds 		priv->wrsent = length;
2661da177e4SLinus Torvalds 
2671da177e4SLinus Torvalds 		/* set up our urb */
2681da177e4SLinus Torvalds 		usb_fill_bulk_urb(port->write_urb, serial->dev,
2691da177e4SLinus Torvalds 			      usb_sndbulkpipe(serial->dev, port->bulk_out_endpointAddress),
2701da177e4SLinus Torvalds 			      port->write_urb->transfer_buffer, length,
2711da177e4SLinus Torvalds 			      ((serial->type->write_bulk_callback) ?
2721da177e4SLinus Torvalds 			       serial->type->write_bulk_callback :
2731da177e4SLinus Torvalds 			       cyberjack_write_bulk_callback),
2741da177e4SLinus Torvalds 			      port);
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds 		/* send the data out the bulk port */
2771da177e4SLinus Torvalds 		result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
2781da177e4SLinus Torvalds 		if (result) {
279194343d9SGreg Kroah-Hartman 			dev_err(&port->dev,
280194343d9SGreg Kroah-Hartman 				"%s - failed submitting write urb, error %d",
281b9c52f15SAlan Cox 				__func__, result);
2821da177e4SLinus Torvalds 			/* Throw away data. No better idea what to do with it. */
2831da177e4SLinus Torvalds 			priv->wrfilled = 0;
2841da177e4SLinus Torvalds 			priv->wrsent = 0;
2851da177e4SLinus Torvalds 			spin_unlock_irqrestore(&priv->lock, flags);
286507ca9bcSGreg Kroah-Hartman 			port->write_urb_busy = 0;
2871da177e4SLinus Torvalds 			return 0;
2881da177e4SLinus Torvalds 		}
2891da177e4SLinus Torvalds 
290441b62c1SHarvey Harrison 		dbg("%s - priv->wrsent=%d", __func__, priv->wrsent);
291441b62c1SHarvey Harrison 		dbg("%s - priv->wrfilled=%d", __func__, priv->wrfilled);
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds 		if (priv->wrsent >= priv->wrfilled) {
294441b62c1SHarvey Harrison 			dbg("%s - buffer cleaned", __func__);
2951da177e4SLinus Torvalds 			memset(priv->wrbuf, 0, sizeof(priv->wrbuf));
2961da177e4SLinus Torvalds 			priv->wrfilled = 0;
2971da177e4SLinus Torvalds 			priv->wrsent = 0;
2981da177e4SLinus Torvalds 		}
2991da177e4SLinus Torvalds 	}
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->lock, flags);
3021da177e4SLinus Torvalds 
303b9c52f15SAlan Cox 	return count;
3041da177e4SLinus Torvalds }
3051da177e4SLinus Torvalds 
30695da310eSAlan Cox static int cyberjack_write_room(struct tty_struct *tty)
3071da177e4SLinus Torvalds {
308a5b6f60cSAlan Cox 	/* FIXME: .... */
3091da177e4SLinus Torvalds 	return CYBERJACK_LOCAL_BUF_SIZE;
3101da177e4SLinus Torvalds }
3111da177e4SLinus Torvalds 
3127d12e780SDavid Howells static void cyberjack_read_int_callback(struct urb *urb)
3131da177e4SLinus Torvalds {
314cdc97792SMing Lei 	struct usb_serial_port *port = urb->context;
3151da177e4SLinus Torvalds 	struct cyberjack_private *priv = usb_get_serial_port_data(port);
3161da177e4SLinus Torvalds 	unsigned char *data = urb->transfer_buffer;
3177dcc85cdSGreg Kroah-Hartman 	int status = urb->status;
3181da177e4SLinus Torvalds 	int result;
3191da177e4SLinus Torvalds 
320441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, port->number);
3211da177e4SLinus Torvalds 
3221da177e4SLinus Torvalds 	/* the urb might have been killed. */
3237dcc85cdSGreg Kroah-Hartman 	if (status)
3241da177e4SLinus Torvalds 		return;
3251da177e4SLinus Torvalds 
326b9c52f15SAlan Cox 	usb_serial_debug_data(debug, &port->dev, __func__,
327b9c52f15SAlan Cox 						urb->actual_length, data);
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds 	/* React only to interrupts signaling a bulk_in transfer */
330b9c52f15SAlan Cox 	if (urb->actual_length == 4 && data[0] == 0x01) {
3311da177e4SLinus Torvalds 		short old_rdtodo;
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds 		/* This is a announcement of coming bulk_ins. */
3341da177e4SLinus Torvalds 		unsigned short size = ((unsigned short)data[3]<<8)+data[2]+3;
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 		spin_lock(&priv->lock);
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds 		old_rdtodo = priv->rdtodo;
3391da177e4SLinus Torvalds 
340b9c52f15SAlan Cox 		if (old_rdtodo + size < old_rdtodo) {
3411da177e4SLinus Torvalds 			dbg("To many bulk_in urbs to do.");
3421da177e4SLinus Torvalds 			spin_unlock(&priv->lock);
3431da177e4SLinus Torvalds 			goto resubmit;
3441da177e4SLinus Torvalds 		}
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds 		/* "+=" is probably more fault tollerant than "=" */
3471da177e4SLinus Torvalds 		priv->rdtodo += size;
3481da177e4SLinus Torvalds 
349441b62c1SHarvey Harrison 		dbg("%s - rdtodo: %d", __func__, priv->rdtodo);
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds 		spin_unlock(&priv->lock);
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds 		if (!old_rdtodo) {
3541da177e4SLinus Torvalds 			port->read_urb->dev = port->serial->dev;
3551da177e4SLinus Torvalds 			result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
3561da177e4SLinus Torvalds 			if (result)
357194343d9SGreg Kroah-Hartman 				dev_err(&port->dev, "%s - failed resubmitting "
358194343d9SGreg Kroah-Hartman 					"read urb, error %d\n",
359194343d9SGreg Kroah-Hartman 					__func__, result);
360441b62c1SHarvey Harrison 			dbg("%s - usb_submit_urb(read urb)", __func__);
3611da177e4SLinus Torvalds 		}
3621da177e4SLinus Torvalds 	}
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds resubmit:
3651da177e4SLinus Torvalds 	port->interrupt_in_urb->dev = port->serial->dev;
3661da177e4SLinus Torvalds 	result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
3671da177e4SLinus Torvalds 	if (result)
368194343d9SGreg Kroah-Hartman 		dev_err(&port->dev, "usb_submit_urb(read int) failed\n");
369441b62c1SHarvey Harrison 	dbg("%s - usb_submit_urb(int urb)", __func__);
3701da177e4SLinus Torvalds }
3711da177e4SLinus Torvalds 
3727d12e780SDavid Howells static void cyberjack_read_bulk_callback(struct urb *urb)
3731da177e4SLinus Torvalds {
374cdc97792SMing Lei 	struct usb_serial_port *port = urb->context;
3751da177e4SLinus Torvalds 	struct cyberjack_private *priv = usb_get_serial_port_data(port);
3761da177e4SLinus Torvalds 	struct tty_struct *tty;
3771da177e4SLinus Torvalds 	unsigned char *data = urb->transfer_buffer;
3781da177e4SLinus Torvalds 	short todo;
3791da177e4SLinus Torvalds 	int result;
3807dcc85cdSGreg Kroah-Hartman 	int status = urb->status;
3811da177e4SLinus Torvalds 
382441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, port->number);
3831da177e4SLinus Torvalds 
384b9c52f15SAlan Cox 	usb_serial_debug_data(debug, &port->dev, __func__,
385b9c52f15SAlan Cox 						urb->actual_length, data);
3867dcc85cdSGreg Kroah-Hartman 	if (status) {
3877dcc85cdSGreg Kroah-Hartman 		dbg("%s - nonzero read bulk status received: %d",
388441b62c1SHarvey Harrison 		    __func__, status);
3891da177e4SLinus Torvalds 		return;
3901da177e4SLinus Torvalds 	}
3911da177e4SLinus Torvalds 
3924a90f09bSAlan Cox 	tty = tty_port_tty_get(&port->port);
3931da177e4SLinus Torvalds 	if (!tty) {
394441b62c1SHarvey Harrison 		dbg("%s - ignoring since device not open\n", __func__);
3951da177e4SLinus Torvalds 		return;
3961da177e4SLinus Torvalds 	}
3971da177e4SLinus Torvalds 	if (urb->actual_length) {
39833f0f88fSAlan Cox 		tty_buffer_request_room(tty, urb->actual_length);
39933f0f88fSAlan Cox 		tty_insert_flip_string(tty, data, urb->actual_length);
4001da177e4SLinus Torvalds 		tty_flip_buffer_push(tty);
4011da177e4SLinus Torvalds 	}
4024a90f09bSAlan Cox 	tty_kref_put(tty);
4031da177e4SLinus Torvalds 
4041da177e4SLinus Torvalds 	spin_lock(&priv->lock);
4051da177e4SLinus Torvalds 
4061da177e4SLinus Torvalds 	/* Reduce urbs to do by one. */
4071da177e4SLinus Torvalds 	priv->rdtodo -= urb->actual_length;
4081da177e4SLinus Torvalds 	/* Just to be sure */
409b9c52f15SAlan Cox 	if (priv->rdtodo < 0)
410b9c52f15SAlan Cox 		priv->rdtodo = 0;
4111da177e4SLinus Torvalds 	todo = priv->rdtodo;
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds 	spin_unlock(&priv->lock);
4141da177e4SLinus Torvalds 
415441b62c1SHarvey Harrison 	dbg("%s - rdtodo: %d", __func__, todo);
4161da177e4SLinus Torvalds 
4171da177e4SLinus Torvalds 	/* Continue to read if we have still urbs to do. */
4181da177e4SLinus Torvalds 	if (todo /* || (urb->actual_length==port->bulk_in_endpointAddress)*/) {
4191da177e4SLinus Torvalds 		port->read_urb->dev = port->serial->dev;
4201da177e4SLinus Torvalds 		result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
4211da177e4SLinus Torvalds 		if (result)
422194343d9SGreg Kroah-Hartman 			dev_err(&port->dev, "%s - failed resubmitting read "
423194343d9SGreg Kroah-Hartman 				"urb, error %d\n", __func__, result);
424441b62c1SHarvey Harrison 		dbg("%s - usb_submit_urb(read urb)", __func__);
4251da177e4SLinus Torvalds 	}
4261da177e4SLinus Torvalds }
4271da177e4SLinus Torvalds 
4287d12e780SDavid Howells static void cyberjack_write_bulk_callback(struct urb *urb)
4291da177e4SLinus Torvalds {
430cdc97792SMing Lei 	struct usb_serial_port *port = urb->context;
4311da177e4SLinus Torvalds 	struct cyberjack_private *priv = usb_get_serial_port_data(port);
4327dcc85cdSGreg Kroah-Hartman 	int status = urb->status;
4331da177e4SLinus Torvalds 
434441b62c1SHarvey Harrison 	dbg("%s - port %d", __func__, port->number);
4351da177e4SLinus Torvalds 
436507ca9bcSGreg Kroah-Hartman 	port->write_urb_busy = 0;
4377dcc85cdSGreg Kroah-Hartman 	if (status) {
4387dcc85cdSGreg Kroah-Hartman 		dbg("%s - nonzero write bulk status received: %d",
439441b62c1SHarvey Harrison 		    __func__, status);
4401da177e4SLinus Torvalds 		return;
4411da177e4SLinus Torvalds 	}
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds 	spin_lock(&priv->lock);
4441da177e4SLinus Torvalds 
4451da177e4SLinus Torvalds 	/* only do something if we have more data to send */
4461da177e4SLinus Torvalds 	if (priv->wrfilled) {
4471da177e4SLinus Torvalds 		int length, blksize, result;
4481da177e4SLinus Torvalds 
449441b62c1SHarvey Harrison 		dbg("%s - transmitting data (frame n)", __func__);
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds 		length = ((priv->wrfilled - priv->wrsent) > port->bulk_out_size) ?
4521da177e4SLinus Torvalds 			port->bulk_out_size : (priv->wrfilled - priv->wrsent);
4531da177e4SLinus Torvalds 
454b9c52f15SAlan Cox 		memcpy(port->write_urb->transfer_buffer,
455b9c52f15SAlan Cox 					priv->wrbuf + priv->wrsent, length);
4561da177e4SLinus Torvalds 		priv->wrsent += length;
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds 		/* set up our urb */
4591da177e4SLinus Torvalds 		usb_fill_bulk_urb(port->write_urb, port->serial->dev,
4601da177e4SLinus Torvalds 			      usb_sndbulkpipe(port->serial->dev, port->bulk_out_endpointAddress),
4611da177e4SLinus Torvalds 			      port->write_urb->transfer_buffer, length,
4621da177e4SLinus Torvalds 			      ((port->serial->type->write_bulk_callback) ?
4631da177e4SLinus Torvalds 			       port->serial->type->write_bulk_callback :
4641da177e4SLinus Torvalds 			       cyberjack_write_bulk_callback),
4651da177e4SLinus Torvalds 			      port);
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds 		/* send the data out the bulk port */
4681da177e4SLinus Torvalds 		result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
4691da177e4SLinus Torvalds 		if (result) {
470194343d9SGreg Kroah-Hartman 			dev_err(&port->dev,
471194343d9SGreg Kroah-Hartman 				"%s - failed submitting write urb, error %d\n",
472b9c52f15SAlan Cox 				__func__, result);
4731da177e4SLinus Torvalds 			/* Throw away data. No better idea what to do with it. */
4741da177e4SLinus Torvalds 			priv->wrfilled = 0;
4751da177e4SLinus Torvalds 			priv->wrsent = 0;
4761da177e4SLinus Torvalds 			goto exit;
4771da177e4SLinus Torvalds 		}
4781da177e4SLinus Torvalds 
479441b62c1SHarvey Harrison 		dbg("%s - priv->wrsent=%d", __func__, priv->wrsent);
480441b62c1SHarvey Harrison 		dbg("%s - priv->wrfilled=%d", __func__, priv->wrfilled);
4811da177e4SLinus Torvalds 
4821da177e4SLinus Torvalds 		blksize = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
4831da177e4SLinus Torvalds 
484b9c52f15SAlan Cox 		if (priv->wrsent >= priv->wrfilled ||
485b9c52f15SAlan Cox 					priv->wrsent >= blksize) {
486441b62c1SHarvey Harrison 			dbg("%s - buffer cleaned", __func__);
4871da177e4SLinus Torvalds 			memset(priv->wrbuf, 0, sizeof(priv->wrbuf));
4881da177e4SLinus Torvalds 			priv->wrfilled = 0;
4891da177e4SLinus Torvalds 			priv->wrsent = 0;
4901da177e4SLinus Torvalds 		}
4911da177e4SLinus Torvalds 	}
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds exit:
4941da177e4SLinus Torvalds 	spin_unlock(&priv->lock);
495cf2c7481SPete Zaitcev 	usb_serial_port_softint(port);
4961da177e4SLinus Torvalds }
4971da177e4SLinus Torvalds 
4981da177e4SLinus Torvalds static int __init cyberjack_init(void)
4991da177e4SLinus Torvalds {
5001da177e4SLinus Torvalds 	int retval;
5011da177e4SLinus Torvalds 	retval  = usb_serial_register(&cyberjack_device);
5021da177e4SLinus Torvalds 	if (retval)
5031da177e4SLinus Torvalds 		goto failed_usb_serial_register;
5041da177e4SLinus Torvalds 	retval = usb_register(&cyberjack_driver);
5051da177e4SLinus Torvalds 	if (retval)
5061da177e4SLinus Torvalds 		goto failed_usb_register;
5071da177e4SLinus Torvalds 
508c197a8dbSGreg Kroah-Hartman 	printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION " "
509c197a8dbSGreg Kroah-Hartman 	       DRIVER_AUTHOR "\n");
510c197a8dbSGreg Kroah-Hartman 	printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
5111da177e4SLinus Torvalds 
5121da177e4SLinus Torvalds 	return 0;
5131da177e4SLinus Torvalds failed_usb_register:
5141da177e4SLinus Torvalds 	usb_serial_deregister(&cyberjack_device);
5151da177e4SLinus Torvalds failed_usb_serial_register:
5161da177e4SLinus Torvalds 	return retval;
5171da177e4SLinus Torvalds }
5181da177e4SLinus Torvalds 
5191da177e4SLinus Torvalds static void __exit cyberjack_exit(void)
5201da177e4SLinus Torvalds {
5211da177e4SLinus Torvalds 	usb_deregister(&cyberjack_driver);
5221da177e4SLinus Torvalds 	usb_serial_deregister(&cyberjack_device);
5231da177e4SLinus Torvalds }
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds module_init(cyberjack_init);
5261da177e4SLinus Torvalds module_exit(cyberjack_exit);
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds MODULE_AUTHOR(DRIVER_AUTHOR);
5291da177e4SLinus Torvalds MODULE_DESCRIPTION(DRIVER_DESC);
5301da177e4SLinus Torvalds MODULE_VERSION(DRIVER_VERSION);
5311da177e4SLinus Torvalds MODULE_LICENSE("GPL");
5321da177e4SLinus Torvalds 
5331da177e4SLinus Torvalds module_param(debug, bool, S_IRUGO | S_IWUSR);
5341da177e4SLinus Torvalds MODULE_PARM_DESC(debug, "Debug enabled or not");
535