xref: /linux/drivers/usb/serial/cyberjack.c (revision 507ca9bc0476662f3463888d583864834eab1e11)
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/config.h>
321da177e4SLinus Torvalds #include <linux/kernel.h>
331da177e4SLinus Torvalds #include <linux/errno.h>
341da177e4SLinus Torvalds #include <linux/init.h>
351da177e4SLinus Torvalds #include <linux/slab.h>
361da177e4SLinus Torvalds #include <linux/tty.h>
371da177e4SLinus Torvalds #include <linux/tty_driver.h>
381da177e4SLinus Torvalds #include <linux/tty_flip.h>
391da177e4SLinus Torvalds #include <linux/module.h>
401da177e4SLinus Torvalds #include <linux/spinlock.h>
411da177e4SLinus Torvalds #include <asm/uaccess.h>
421da177e4SLinus Torvalds #include <linux/usb.h>
431da177e4SLinus Torvalds #include "usb-serial.h"
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds #define CYBERJACK_LOCAL_BUF_SIZE 32
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds static int debug;
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds /*
501da177e4SLinus Torvalds  * Version Information
511da177e4SLinus Torvalds  */
521da177e4SLinus Torvalds #define DRIVER_VERSION "v1.01"
531da177e4SLinus Torvalds #define DRIVER_AUTHOR "Matthias Bruestle"
541da177e4SLinus Torvalds #define DRIVER_DESC "REINER SCT cyberJack pinpad/e-com USB Chipcard Reader Driver"
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds #define CYBERJACK_VENDOR_ID	0x0C4B
581da177e4SLinus Torvalds #define CYBERJACK_PRODUCT_ID	0x0100
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds /* Function prototypes */
611da177e4SLinus Torvalds static int cyberjack_startup (struct usb_serial *serial);
621da177e4SLinus Torvalds static void cyberjack_shutdown (struct usb_serial *serial);
631da177e4SLinus Torvalds static int  cyberjack_open (struct usb_serial_port *port, struct file *filp);
641da177e4SLinus Torvalds static void cyberjack_close (struct usb_serial_port *port, struct file *filp);
651da177e4SLinus Torvalds static int cyberjack_write (struct usb_serial_port *port, const unsigned char *buf, int count);
661da177e4SLinus Torvalds static int cyberjack_write_room( struct usb_serial_port *port );
671da177e4SLinus Torvalds static void cyberjack_read_int_callback (struct urb *urb, struct pt_regs *regs);
681da177e4SLinus Torvalds static void cyberjack_read_bulk_callback (struct urb *urb, struct pt_regs *regs);
691da177e4SLinus Torvalds static void cyberjack_write_bulk_callback (struct urb *urb, struct pt_regs *regs);
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds static struct usb_device_id id_table [] = {
721da177e4SLinus Torvalds 	{ USB_DEVICE(CYBERJACK_VENDOR_ID, CYBERJACK_PRODUCT_ID) },
731da177e4SLinus Torvalds 	{ }			/* Terminating entry */
741da177e4SLinus Torvalds };
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds MODULE_DEVICE_TABLE (usb, id_table);
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds static struct usb_driver cyberjack_driver = {
791da177e4SLinus Torvalds 	.owner =	THIS_MODULE,
801da177e4SLinus Torvalds 	.name =		"cyberjack",
811da177e4SLinus Torvalds 	.probe =	usb_serial_probe,
821da177e4SLinus Torvalds 	.disconnect =	usb_serial_disconnect,
831da177e4SLinus Torvalds 	.id_table =	id_table,
841da177e4SLinus Torvalds };
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds static struct usb_serial_device_type cyberjack_device = {
871da177e4SLinus Torvalds 	.owner =		THIS_MODULE,
881da177e4SLinus Torvalds 	.name =			"Reiner SCT Cyberjack USB card reader",
891da177e4SLinus Torvalds 	.short_name =		"cyberjack",
901da177e4SLinus Torvalds 	.id_table =		id_table,
911da177e4SLinus Torvalds 	.num_interrupt_in =	1,
921da177e4SLinus Torvalds 	.num_bulk_in =		1,
931da177e4SLinus Torvalds 	.num_bulk_out =		1,
941da177e4SLinus Torvalds 	.num_ports =		1,
951da177e4SLinus Torvalds 	.attach =		cyberjack_startup,
961da177e4SLinus Torvalds 	.shutdown =		cyberjack_shutdown,
971da177e4SLinus Torvalds 	.open =			cyberjack_open,
981da177e4SLinus Torvalds 	.close =		cyberjack_close,
991da177e4SLinus Torvalds 	.write =		cyberjack_write,
1001da177e4SLinus Torvalds 	.write_room =	cyberjack_write_room,
1011da177e4SLinus Torvalds 	.read_int_callback =	cyberjack_read_int_callback,
1021da177e4SLinus Torvalds 	.read_bulk_callback =	cyberjack_read_bulk_callback,
1031da177e4SLinus Torvalds 	.write_bulk_callback =	cyberjack_write_bulk_callback,
1041da177e4SLinus Torvalds };
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds struct cyberjack_private {
1071da177e4SLinus Torvalds 	spinlock_t	lock;		/* Lock for SMP */
1081da177e4SLinus Torvalds 	short		rdtodo;		/* Bytes still to read */
1091da177e4SLinus Torvalds 	unsigned char	wrbuf[5*64];	/* Buffer for collecting data to write */
1101da177e4SLinus Torvalds 	short		wrfilled;	/* Overall data size we already got */
1111da177e4SLinus Torvalds 	short		wrsent;		/* Data already sent */
1121da177e4SLinus Torvalds };
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds /* do some startup allocations not currently performed by usb_serial_probe() */
1151da177e4SLinus Torvalds static int cyberjack_startup (struct usb_serial *serial)
1161da177e4SLinus Torvalds {
1171da177e4SLinus Torvalds 	struct cyberjack_private *priv;
1181da177e4SLinus Torvalds 	int i;
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds 	dbg("%s", __FUNCTION__);
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds 	/* allocate the private data structure */
1231da177e4SLinus Torvalds 	priv = kmalloc(sizeof(struct cyberjack_private), GFP_KERNEL);
1241da177e4SLinus Torvalds 	if (!priv)
1251da177e4SLinus Torvalds 		return -ENOMEM;
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds 	/* set initial values */
1281da177e4SLinus Torvalds 	spin_lock_init(&priv->lock);
1291da177e4SLinus Torvalds 	priv->rdtodo = 0;
1301da177e4SLinus Torvalds 	priv->wrfilled = 0;
1311da177e4SLinus Torvalds 	priv->wrsent = 0;
1321da177e4SLinus Torvalds 	usb_set_serial_port_data(serial->port[0], priv);
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds 	init_waitqueue_head(&serial->port[0]->write_wait);
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds 	for (i = 0; i < serial->num_ports; ++i) {
1371da177e4SLinus Torvalds 		int result;
1381da177e4SLinus Torvalds 		serial->port[i]->interrupt_in_urb->dev = serial->dev;
1391da177e4SLinus Torvalds 		result = usb_submit_urb(serial->port[i]->interrupt_in_urb,
1401da177e4SLinus Torvalds 					GFP_KERNEL);
1411da177e4SLinus Torvalds 		if (result)
1421da177e4SLinus Torvalds 			err(" usb_submit_urb(read int) failed");
1431da177e4SLinus Torvalds 		dbg("%s - usb_submit_urb(int urb)", __FUNCTION__);
1441da177e4SLinus Torvalds 	}
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds 	return( 0 );
1471da177e4SLinus Torvalds }
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds static void cyberjack_shutdown (struct usb_serial *serial)
1501da177e4SLinus Torvalds {
1511da177e4SLinus Torvalds 	int i;
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds 	dbg("%s", __FUNCTION__);
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds 	for (i=0; i < serial->num_ports; ++i) {
1561da177e4SLinus Torvalds 		usb_kill_urb(serial->port[i]->interrupt_in_urb);
1571da177e4SLinus Torvalds 		/* My special items, the standard routines free my urbs */
1581da177e4SLinus Torvalds 		kfree(usb_get_serial_port_data(serial->port[i]));
1591da177e4SLinus Torvalds 		usb_set_serial_port_data(serial->port[i], NULL);
1601da177e4SLinus Torvalds 	}
1611da177e4SLinus Torvalds }
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds static int  cyberjack_open (struct usb_serial_port *port, struct file *filp)
1641da177e4SLinus Torvalds {
1651da177e4SLinus Torvalds 	struct cyberjack_private *priv;
1661da177e4SLinus Torvalds 	unsigned long flags;
1671da177e4SLinus Torvalds 	int result = 0;
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds 	dbg("%s - port %d", __FUNCTION__, port->number);
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds 	dbg("%s - usb_clear_halt", __FUNCTION__ );
1721da177e4SLinus Torvalds 	usb_clear_halt(port->serial->dev, port->write_urb->pipe);
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds 	/* force low_latency on so that our tty_push actually forces
1751da177e4SLinus Torvalds 	 * the data through, otherwise it is scheduled, and with high
1761da177e4SLinus Torvalds 	 * data rates (like with OHCI) data can get lost.
1771da177e4SLinus Torvalds 	 */
1781da177e4SLinus Torvalds 	port->tty->low_latency = 1;
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds 	priv = usb_get_serial_port_data(port);
1811da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->lock, flags);
1821da177e4SLinus Torvalds 	priv->rdtodo = 0;
1831da177e4SLinus Torvalds 	priv->wrfilled = 0;
1841da177e4SLinus Torvalds 	priv->wrsent = 0;
1851da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->lock, flags);
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds 	return result;
1881da177e4SLinus Torvalds }
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds static void cyberjack_close (struct usb_serial_port *port, struct file *filp)
1911da177e4SLinus Torvalds {
1921da177e4SLinus Torvalds 	dbg("%s - port %d", __FUNCTION__, port->number);
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds 	if (port->serial->dev) {
1951da177e4SLinus Torvalds 		/* shutdown any bulk reads that might be going on */
1961da177e4SLinus Torvalds 		usb_kill_urb(port->write_urb);
1971da177e4SLinus Torvalds 		usb_kill_urb(port->read_urb);
1981da177e4SLinus Torvalds 	}
1991da177e4SLinus Torvalds }
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds static int cyberjack_write (struct usb_serial_port *port, const unsigned char *buf, int count)
2021da177e4SLinus Torvalds {
2031da177e4SLinus Torvalds 	struct usb_serial *serial = port->serial;
2041da177e4SLinus Torvalds 	struct cyberjack_private *priv = usb_get_serial_port_data(port);
2051da177e4SLinus Torvalds 	unsigned long flags;
2061da177e4SLinus Torvalds 	int result;
2071da177e4SLinus Torvalds 	int wrexpected;
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds 	dbg("%s - port %d", __FUNCTION__, port->number);
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds 	if (count == 0) {
2121da177e4SLinus Torvalds 		dbg("%s - write request of 0 bytes", __FUNCTION__);
2131da177e4SLinus Torvalds 		return (0);
2141da177e4SLinus Torvalds 	}
2151da177e4SLinus Torvalds 
216*507ca9bcSGreg Kroah-Hartman 	spin_lock(&port->lock);
217*507ca9bcSGreg Kroah-Hartman 	if (port->write_urb_busy) {
218*507ca9bcSGreg Kroah-Hartman 		spin_unlock(&port->lock);
2191da177e4SLinus Torvalds 		dbg("%s - already writing", __FUNCTION__);
220*507ca9bcSGreg Kroah-Hartman 		return 0;
2211da177e4SLinus Torvalds 	}
222*507ca9bcSGreg Kroah-Hartman 	port->write_urb_busy = 1;
223*507ca9bcSGreg Kroah-Hartman 	spin_unlock(&port->lock);
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds 	spin_lock_irqsave(&priv->lock, flags);
2261da177e4SLinus Torvalds 
2271da177e4SLinus Torvalds 	if( (count+priv->wrfilled)>sizeof(priv->wrbuf) ) {
2281da177e4SLinus Torvalds 		/* To much data for buffer. Reset buffer. */
2291da177e4SLinus Torvalds 		priv->wrfilled=0;
2301da177e4SLinus Torvalds 		spin_unlock_irqrestore(&priv->lock, flags);
231*507ca9bcSGreg Kroah-Hartman 		port->write_urb_busy = 0;
2321da177e4SLinus Torvalds 		return (0);
2331da177e4SLinus Torvalds 	}
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds 	/* Copy data */
2361da177e4SLinus Torvalds 	memcpy (priv->wrbuf+priv->wrfilled, buf, count);
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds 	usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count,
2391da177e4SLinus Torvalds 		priv->wrbuf+priv->wrfilled);
2401da177e4SLinus Torvalds 	priv->wrfilled += count;
2411da177e4SLinus Torvalds 
2421da177e4SLinus Torvalds 	if( priv->wrfilled >= 3 ) {
2431da177e4SLinus Torvalds 		wrexpected = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
2441da177e4SLinus Torvalds 		dbg("%s - expected data: %d", __FUNCTION__, wrexpected);
2451da177e4SLinus Torvalds 	} else {
2461da177e4SLinus Torvalds 		wrexpected = sizeof(priv->wrbuf);
2471da177e4SLinus Torvalds 	}
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds 	if( priv->wrfilled >= wrexpected ) {
2501da177e4SLinus Torvalds 		/* We have enough data to begin transmission */
2511da177e4SLinus Torvalds 		int length;
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds 		dbg("%s - transmitting data (frame 1)", __FUNCTION__);
2541da177e4SLinus Torvalds 		length = (wrexpected > port->bulk_out_size) ? port->bulk_out_size : wrexpected;
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds 		memcpy (port->write_urb->transfer_buffer, priv->wrbuf, length );
2571da177e4SLinus Torvalds 		priv->wrsent=length;
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds 		/* set up our urb */
2601da177e4SLinus Torvalds 		usb_fill_bulk_urb(port->write_urb, serial->dev,
2611da177e4SLinus Torvalds 			      usb_sndbulkpipe(serial->dev, port->bulk_out_endpointAddress),
2621da177e4SLinus Torvalds 			      port->write_urb->transfer_buffer, length,
2631da177e4SLinus Torvalds 			      ((serial->type->write_bulk_callback) ?
2641da177e4SLinus Torvalds 			       serial->type->write_bulk_callback :
2651da177e4SLinus Torvalds 			       cyberjack_write_bulk_callback),
2661da177e4SLinus Torvalds 			      port);
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds 		/* send the data out the bulk port */
2691da177e4SLinus Torvalds 		result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
2701da177e4SLinus Torvalds 		if (result) {
2711da177e4SLinus Torvalds 			err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
2721da177e4SLinus Torvalds 			/* Throw away data. No better idea what to do with it. */
2731da177e4SLinus Torvalds 			priv->wrfilled=0;
2741da177e4SLinus Torvalds 			priv->wrsent=0;
2751da177e4SLinus Torvalds 			spin_unlock_irqrestore(&priv->lock, flags);
276*507ca9bcSGreg Kroah-Hartman 			port->write_urb_busy = 0;
2771da177e4SLinus Torvalds 			return 0;
2781da177e4SLinus Torvalds 		}
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds 		dbg("%s - priv->wrsent=%d", __FUNCTION__,priv->wrsent);
2811da177e4SLinus Torvalds 		dbg("%s - priv->wrfilled=%d", __FUNCTION__,priv->wrfilled);
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds 		if( priv->wrsent>=priv->wrfilled ) {
2841da177e4SLinus Torvalds 			dbg("%s - buffer cleaned", __FUNCTION__);
2851da177e4SLinus Torvalds 			memset( priv->wrbuf, 0, sizeof(priv->wrbuf) );
2861da177e4SLinus Torvalds 			priv->wrfilled=0;
2871da177e4SLinus Torvalds 			priv->wrsent=0;
2881da177e4SLinus Torvalds 		}
2891da177e4SLinus Torvalds 	}
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 	spin_unlock_irqrestore(&priv->lock, flags);
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds 	return (count);
2941da177e4SLinus Torvalds }
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds static int cyberjack_write_room( struct usb_serial_port *port )
2971da177e4SLinus Torvalds {
2981da177e4SLinus Torvalds 	return CYBERJACK_LOCAL_BUF_SIZE;
2991da177e4SLinus Torvalds }
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds static void cyberjack_read_int_callback( struct urb *urb, struct pt_regs *regs )
3021da177e4SLinus Torvalds {
3031da177e4SLinus Torvalds 	struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
3041da177e4SLinus Torvalds 	struct cyberjack_private *priv = usb_get_serial_port_data(port);
3051da177e4SLinus Torvalds 	unsigned char *data = urb->transfer_buffer;
3061da177e4SLinus Torvalds 	int result;
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds 	dbg("%s - port %d", __FUNCTION__, port->number);
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds 	/* the urb might have been killed. */
3111da177e4SLinus Torvalds 	if (urb->status)
3121da177e4SLinus Torvalds 		return;
3131da177e4SLinus Torvalds 
3141da177e4SLinus Torvalds 	usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds 	/* React only to interrupts signaling a bulk_in transfer */
3171da177e4SLinus Torvalds 	if( (urb->actual_length==4) && (data[0]==0x01) ) {
3181da177e4SLinus Torvalds 		short old_rdtodo;
3191da177e4SLinus Torvalds 		int result;
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds 		/* This is a announcement of coming bulk_ins. */
3221da177e4SLinus Torvalds 		unsigned short size = ((unsigned short)data[3]<<8)+data[2]+3;
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds 		spin_lock(&priv->lock);
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds 		old_rdtodo = priv->rdtodo;
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds 		if( (old_rdtodo+size)<(old_rdtodo) ) {
3291da177e4SLinus Torvalds 			dbg( "To many bulk_in urbs to do." );
3301da177e4SLinus Torvalds 			spin_unlock(&priv->lock);
3311da177e4SLinus Torvalds 			goto resubmit;
3321da177e4SLinus Torvalds 		}
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds 		/* "+=" is probably more fault tollerant than "=" */
3351da177e4SLinus Torvalds 		priv->rdtodo += size;
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds 		dbg("%s - rdtodo: %d", __FUNCTION__, priv->rdtodo);
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds 		spin_unlock(&priv->lock);
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds 		if( !old_rdtodo ) {
3421da177e4SLinus Torvalds 			port->read_urb->dev = port->serial->dev;
3431da177e4SLinus Torvalds 			result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
3441da177e4SLinus Torvalds 			if( result )
3451da177e4SLinus Torvalds 				err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
3461da177e4SLinus Torvalds 			dbg("%s - usb_submit_urb(read urb)", __FUNCTION__);
3471da177e4SLinus Torvalds 		}
3481da177e4SLinus Torvalds 	}
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds resubmit:
3511da177e4SLinus Torvalds 	port->interrupt_in_urb->dev = port->serial->dev;
3521da177e4SLinus Torvalds 	result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
3531da177e4SLinus Torvalds 	if (result)
3541da177e4SLinus Torvalds 		err(" usb_submit_urb(read int) failed");
3551da177e4SLinus Torvalds 	dbg("%s - usb_submit_urb(int urb)", __FUNCTION__);
3561da177e4SLinus Torvalds }
3571da177e4SLinus Torvalds 
3581da177e4SLinus Torvalds static void cyberjack_read_bulk_callback (struct urb *urb, struct pt_regs *regs)
3591da177e4SLinus Torvalds {
3601da177e4SLinus Torvalds 	struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
3611da177e4SLinus Torvalds 	struct cyberjack_private *priv = usb_get_serial_port_data(port);
3621da177e4SLinus Torvalds 	struct tty_struct *tty;
3631da177e4SLinus Torvalds 	unsigned char *data = urb->transfer_buffer;
3641da177e4SLinus Torvalds 	short todo;
3651da177e4SLinus Torvalds 	int i;
3661da177e4SLinus Torvalds 	int result;
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds 	dbg("%s - port %d", __FUNCTION__, port->number);
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds 	usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
3711da177e4SLinus Torvalds 	if (urb->status) {
3721da177e4SLinus Torvalds 		dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
3731da177e4SLinus Torvalds 		return;
3741da177e4SLinus Torvalds 	}
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds 	tty = port->tty;
3771da177e4SLinus Torvalds 	if (!tty) {
3781da177e4SLinus Torvalds 		dbg("%s - ignoring since device not open\n", __FUNCTION__);
3791da177e4SLinus Torvalds 		return;
3801da177e4SLinus Torvalds 	}
3811da177e4SLinus Torvalds 	if (urb->actual_length) {
3821da177e4SLinus Torvalds 		for (i = 0; i < urb->actual_length ; ++i) {
3831da177e4SLinus Torvalds 			/* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
3841da177e4SLinus Torvalds 			if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
3851da177e4SLinus Torvalds 				tty_flip_buffer_push(tty);
3861da177e4SLinus Torvalds 			}
3871da177e4SLinus Torvalds 			/* this doesn't actually push the data through unless tty->low_latency is set */
3881da177e4SLinus Torvalds 			tty_insert_flip_char(tty, data[i], 0);
3891da177e4SLinus Torvalds 		}
3901da177e4SLinus Torvalds 	  	tty_flip_buffer_push(tty);
3911da177e4SLinus Torvalds 	}
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds 	spin_lock(&priv->lock);
3941da177e4SLinus Torvalds 
3951da177e4SLinus Torvalds 	/* Reduce urbs to do by one. */
3961da177e4SLinus Torvalds 	priv->rdtodo-=urb->actual_length;
3971da177e4SLinus Torvalds 	/* Just to be sure */
3981da177e4SLinus Torvalds 	if ( priv->rdtodo<0 ) priv->rdtodo = 0;
3991da177e4SLinus Torvalds 	todo = priv->rdtodo;
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds 	spin_unlock(&priv->lock);
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds 	dbg("%s - rdtodo: %d", __FUNCTION__, todo);
4041da177e4SLinus Torvalds 
4051da177e4SLinus Torvalds 	/* Continue to read if we have still urbs to do. */
4061da177e4SLinus Torvalds 	if( todo /* || (urb->actual_length==port->bulk_in_endpointAddress)*/ ) {
4071da177e4SLinus Torvalds 		port->read_urb->dev = port->serial->dev;
4081da177e4SLinus Torvalds 		result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
4091da177e4SLinus Torvalds 		if (result)
4101da177e4SLinus Torvalds 			err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
4111da177e4SLinus Torvalds 		dbg("%s - usb_submit_urb(read urb)", __FUNCTION__);
4121da177e4SLinus Torvalds 	}
4131da177e4SLinus Torvalds }
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds static void cyberjack_write_bulk_callback (struct urb *urb, struct pt_regs *regs)
4161da177e4SLinus Torvalds {
4171da177e4SLinus Torvalds 	struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
4181da177e4SLinus Torvalds 	struct cyberjack_private *priv = usb_get_serial_port_data(port);
4191da177e4SLinus Torvalds 
4201da177e4SLinus Torvalds 	dbg("%s - port %d", __FUNCTION__, port->number);
4211da177e4SLinus Torvalds 
422*507ca9bcSGreg Kroah-Hartman 	port->write_urb_busy = 0;
4231da177e4SLinus Torvalds 	if (urb->status) {
4241da177e4SLinus Torvalds 		dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
4251da177e4SLinus Torvalds 		return;
4261da177e4SLinus Torvalds 	}
4271da177e4SLinus Torvalds 
4281da177e4SLinus Torvalds 	spin_lock(&priv->lock);
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds 	/* only do something if we have more data to send */
4311da177e4SLinus Torvalds 	if( priv->wrfilled ) {
4321da177e4SLinus Torvalds 		int length, blksize, result;
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds 		dbg("%s - transmitting data (frame n)", __FUNCTION__);
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds 		length = ((priv->wrfilled - priv->wrsent) > port->bulk_out_size) ?
4371da177e4SLinus Torvalds 			port->bulk_out_size : (priv->wrfilled - priv->wrsent);
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds 		memcpy (port->write_urb->transfer_buffer, priv->wrbuf + priv->wrsent,
4401da177e4SLinus Torvalds 			length );
4411da177e4SLinus Torvalds 		priv->wrsent+=length;
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds 		/* set up our urb */
4441da177e4SLinus Torvalds 		usb_fill_bulk_urb(port->write_urb, port->serial->dev,
4451da177e4SLinus Torvalds 			      usb_sndbulkpipe(port->serial->dev, port->bulk_out_endpointAddress),
4461da177e4SLinus Torvalds 			      port->write_urb->transfer_buffer, length,
4471da177e4SLinus Torvalds 			      ((port->serial->type->write_bulk_callback) ?
4481da177e4SLinus Torvalds 			       port->serial->type->write_bulk_callback :
4491da177e4SLinus Torvalds 			       cyberjack_write_bulk_callback),
4501da177e4SLinus Torvalds 			      port);
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds 		/* send the data out the bulk port */
4531da177e4SLinus Torvalds 		result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
4541da177e4SLinus Torvalds 		if (result) {
4551da177e4SLinus Torvalds 			err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
4561da177e4SLinus Torvalds 			/* Throw away data. No better idea what to do with it. */
4571da177e4SLinus Torvalds 			priv->wrfilled=0;
4581da177e4SLinus Torvalds 			priv->wrsent=0;
4591da177e4SLinus Torvalds 			goto exit;
4601da177e4SLinus Torvalds 		}
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds 		dbg("%s - priv->wrsent=%d", __FUNCTION__,priv->wrsent);
4631da177e4SLinus Torvalds 		dbg("%s - priv->wrfilled=%d", __FUNCTION__,priv->wrfilled);
4641da177e4SLinus Torvalds 
4651da177e4SLinus Torvalds 		blksize = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds 		if( (priv->wrsent>=priv->wrfilled) || (priv->wrsent>=blksize) ) {
4681da177e4SLinus Torvalds 			dbg("%s - buffer cleaned", __FUNCTION__);
4691da177e4SLinus Torvalds 			memset( priv->wrbuf, 0, sizeof(priv->wrbuf) );
4701da177e4SLinus Torvalds 			priv->wrfilled=0;
4711da177e4SLinus Torvalds 			priv->wrsent=0;
4721da177e4SLinus Torvalds 		}
4731da177e4SLinus Torvalds 	}
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds exit:
4761da177e4SLinus Torvalds 	spin_unlock(&priv->lock);
4771da177e4SLinus Torvalds 	schedule_work(&port->work);
4781da177e4SLinus Torvalds }
4791da177e4SLinus Torvalds 
4801da177e4SLinus Torvalds static int __init cyberjack_init (void)
4811da177e4SLinus Torvalds {
4821da177e4SLinus Torvalds 	int retval;
4831da177e4SLinus Torvalds 	retval  = usb_serial_register(&cyberjack_device);
4841da177e4SLinus Torvalds 	if (retval)
4851da177e4SLinus Torvalds 		goto failed_usb_serial_register;
4861da177e4SLinus Torvalds 	retval = usb_register(&cyberjack_driver);
4871da177e4SLinus Torvalds 	if (retval)
4881da177e4SLinus Torvalds 		goto failed_usb_register;
4891da177e4SLinus Torvalds 
4901da177e4SLinus Torvalds 	info(DRIVER_VERSION " " DRIVER_AUTHOR);
4911da177e4SLinus Torvalds 	info(DRIVER_DESC);
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds 	return 0;
4941da177e4SLinus Torvalds failed_usb_register:
4951da177e4SLinus Torvalds 	usb_serial_deregister(&cyberjack_device);
4961da177e4SLinus Torvalds failed_usb_serial_register:
4971da177e4SLinus Torvalds 	return retval;
4981da177e4SLinus Torvalds }
4991da177e4SLinus Torvalds 
5001da177e4SLinus Torvalds static void __exit cyberjack_exit (void)
5011da177e4SLinus Torvalds {
5021da177e4SLinus Torvalds 	usb_deregister (&cyberjack_driver);
5031da177e4SLinus Torvalds 	usb_serial_deregister (&cyberjack_device);
5041da177e4SLinus Torvalds }
5051da177e4SLinus Torvalds 
5061da177e4SLinus Torvalds module_init(cyberjack_init);
5071da177e4SLinus Torvalds module_exit(cyberjack_exit);
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds MODULE_AUTHOR( DRIVER_AUTHOR );
5101da177e4SLinus Torvalds MODULE_DESCRIPTION( DRIVER_DESC );
5111da177e4SLinus Torvalds MODULE_VERSION( DRIVER_VERSION );
5121da177e4SLinus Torvalds MODULE_LICENSE("GPL");
5131da177e4SLinus Torvalds 
5141da177e4SLinus Torvalds module_param(debug, bool, S_IRUGO | S_IWUSR);
5151da177e4SLinus Torvalds MODULE_PARM_DESC(debug, "Debug enabled or not");
516