<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">From: Greg KH &lt;greg@kroah.com&gt;
To: Alan Cox &lt;alan@lxorguk.ukuu.org.uk&gt;
Cc: linux-usb-devel@lists.sourceforge.net
Subject: [PATCH 03 of 15] USB Serial Cyberjack driver added

This patch adds the cyberjack driver.



diff -Nru a/drivers/usb/serial/cyberjack.c b/drivers/usb/serial/cyberjack.c
--- /dev/null	Wed Dec 31 16:00:00 1969
+++ b/drivers/usb/serial/cyberjack.c	Thu Jan  3 21:41:54 2002
@@ -0,0 +1,522 @@
+/*
+ *  REINER SCT cyberJack pinpad/e-com USB Chipcard Reader Driver
+ *
+ *  Copyright (C) 2001  REINER SCT
+ *  Author: Matthias Bruestle
+ *
+ *  Contact: linux-usb@sii.li (see MAINTAINERS)
+ *
+ *  This program is largely derived from work by the linux-usb group
+ *  and associated source files.  Please see the usb/serial files for
+ *  individual credits and copyrights.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  Thanks to Greg Kroah-Hartman (greg@kroah.com) for his help and
+ *  patience.
+ *
+ *  In case of problems, please write to the contact e-mail address
+ *  mentioned above.
+ */
+
+
+#include &lt;linux/config.h&gt;
+#include &lt;linux/kernel.h&gt;
+#include &lt;linux/sched.h&gt;
+#include &lt;linux/signal.h&gt;
+#include &lt;linux/errno.h&gt;
+#include &lt;linux/poll.h&gt;
+#include &lt;linux/init.h&gt;
+#include &lt;linux/slab.h&gt;
+#include &lt;linux/fcntl.h&gt;
+#include &lt;linux/tty.h&gt;
+#include &lt;linux/tty_driver.h&gt;
+#include &lt;linux/tty_flip.h&gt;
+#include &lt;linux/module.h&gt;
+#include &lt;linux/spinlock.h&gt;
+#include &lt;linux/usb.h&gt;
+
+#ifdef CONFIG_USB_SERIAL_DEBUG
+	static int debug = 1;
+#else
+	static int debug;
+#endif
+
+#include "usb-serial.h"
+
+/*
+ * Version Information
+ */
+#define DRIVER_VERSION "v1.0"
+#define DRIVER_AUTHOR "Matthias Bruestle"
+#define DRIVER_DESC "REINER SCT cyberJack pinpad/e-com USB Chipcard Reader Driver"
+
+
+#define CYBERJACK_VENDOR_ID	0x0C4B
+#define CYBERJACK_PRODUCT_ID	0x0100
+
+/* Function prototypes */
+static int cyberjack_startup (struct usb_serial *serial);
+static void cyberjack_shutdown (struct usb_serial *serial);
+static int  cyberjack_open (struct usb_serial_port *port, struct file *filp);
+static void cyberjack_close (struct usb_serial_port *port, struct file *filp);
+static int cyberjack_write (struct usb_serial_port *port, int from_user,
+	const unsigned char *buf, int count);
+static void cyberjack_read_int_callback( struct urb *urb );
+static void cyberjack_read_bulk_callback (struct urb *urb);
+static void cyberjack_write_bulk_callback (struct urb *urb);
+
+
+static u16 cyberjack_vendor_id	= CYBERJACK_VENDOR_ID;
+static u16 cyberjack_product_id = CYBERJACK_PRODUCT_ID;
+
+static struct usb_serial_device_type cyberjack_device = {
+	name:			"Reiner SCT Cyberjack USB card reader",
+	idVendor:		&amp;cyberjack_vendor_id,
+	idProduct:		&amp;cyberjack_product_id,
+	needs_interrupt_in:	MUST_HAVE,
+	needs_bulk_in:		MUST_HAVE,
+	needs_bulk_out:		MUST_HAVE,
+	num_interrupt_in:	1,
+	num_bulk_in:		1,
+	num_bulk_out:		1,
+	num_ports:		1,
+	startup:		cyberjack_startup,
+	shutdown:		cyberjack_shutdown,
+	open:			cyberjack_open,
+	close:			cyberjack_close,
+	write:			cyberjack_write,
+	read_int_callback:	cyberjack_read_int_callback,
+	read_bulk_callback:	cyberjack_read_bulk_callback,
+	write_bulk_callback:	cyberjack_write_bulk_callback,
+};
+
+struct cyberjack_private {
+	short	rdtodo;		/* Bytes still to read */
+	unsigned char	wrbuf[5*64];	/* Buffer for collecting data to write */
+	short	wrfilled;	/* Overall data size we already got */
+	short	wrsent;		/* Data akready sent */
+};
+
+/* do some startup allocations not currently performed by usb_serial_probe() */
+static int cyberjack_startup (struct usb_serial *serial)
+{
+	struct cyberjack_private *priv;
+
+	dbg (__FUNCTION__);
+
+	/* allocate the private data structure */
+	serial-&gt;port-&gt;private = kmalloc(sizeof(struct cyberjack_private), GFP_KERNEL);
+	if (!serial-&gt;port-&gt;private)
+		return (-1); /* error */
+
+	/* set initial values */
+	priv = (struct cyberjack_private *)serial-&gt;port-&gt;private;
+	priv-&gt;rdtodo = 0;
+	priv-&gt;wrfilled = 0;
+	priv-&gt;wrsent = 0;
+
+	init_waitqueue_head(&amp;serial-&gt;port-&gt;write_wait);
+
+	return( 0 );
+}
+
+static void cyberjack_shutdown (struct usb_serial *serial)
+{
+	int i;
+	
+	dbg (__FUNCTION__);
+
+	/* stop reads and writes on all ports */
+	for (i=0; i &lt; serial-&gt;num_ports; ++i) {
+		while (serial-&gt;port[i].open_count &gt; 0) {
+			cyberjack_close (&amp;serial-&gt;port[i], NULL);
+		}
+		/* My special items, the standard routines free my urbs */
+		if (serial-&gt;port[i].private)
+			kfree(serial-&gt;port[i].private);
+	}
+}
+	
+static int  cyberjack_open (struct usb_serial_port *port, struct file *filp)
+{
+	struct cyberjack_private *priv;
+	int result = 0;
+
+	if (port_paranoia_check (port, __FUNCTION__))
+		return -ENODEV;
+
+	MOD_INC_USE_COUNT;
+
+	dbg(__FUNCTION__ " - port %d", port-&gt;number);
+
+	down (&amp;port-&gt;sem);
+
+	++port-&gt;open_count;
+
+	if (!port-&gt;active) {
+		port-&gt;active = 1;
+		/* force low_latency on so that our tty_push actually forces
+		 * the data through, otherwise it is scheduled, and with high
+		 * data rates (like with OHCI) data can get lost.
+		 */
+		port-&gt;tty-&gt;low_latency = 1;
+
+		priv = (struct cyberjack_private *)port-&gt;private;
+		priv-&gt;rdtodo = 0;
+		priv-&gt;wrfilled = 0;
+		priv-&gt;wrsent = 0;
+
+		/* shutdown any bulk reads that might be going on */
+		usb_unlink_urb (port-&gt;write_urb);
+		usb_unlink_urb (port-&gt;read_urb);
+		usb_unlink_urb (port-&gt;interrupt_in_urb);
+
+		port-&gt;interrupt_in_urb-&gt;dev = port-&gt;serial-&gt;dev;
+		result = usb_submit_urb(port-&gt;interrupt_in_urb);
+		if (result)
+			err(" usb_submit_urb(read int) failed");
+		dbg(__FUNCTION__ " - usb_submit_urb(int urb)");
+	}
+
+	up (&amp;port-&gt;sem);
+
+	return result;
+}
+
+static void cyberjack_close (struct usb_serial_port *port, struct file *filp)
+{
+	dbg(__FUNCTION__ " - port %d", port-&gt;number);
+
+	down (&amp;port-&gt;sem);
+
+	--port-&gt;open_count;
+
+	if (port-&gt;open_count &lt;= 0) {
+		if (port-&gt;serial-&gt;dev) {
+			/* shutdown any bulk reads that might be going on */
+			usb_unlink_urb (port-&gt;write_urb);
+			usb_unlink_urb (port-&gt;read_urb);
+			usb_unlink_urb (port-&gt;interrupt_in_urb);
+		}
+
+		port-&gt;active = 0;
+		port-&gt;open_count = 0;
+	}
+
+	up (&amp;port-&gt;sem);
+	MOD_DEC_USE_COUNT;
+}
+
+static int cyberjack_write (struct usb_serial_port *port, int from_user, const unsigned char *buf, int count)
+{
+	struct usb_serial *serial = port-&gt;serial;
+	struct cyberjack_private *priv = (struct cyberjack_private *)port-&gt;private;
+	int result;
+	int wrexpected;
+
+	dbg(__FUNCTION__ " - port %d", port-&gt;number);
+	dbg(__FUNCTION__ " - from_user %d", from_user);
+
+	if (count == 0) {
+		dbg(__FUNCTION__ " - write request of 0 bytes");
+		return (0);
+	}
+
+	if (port-&gt;write_urb-&gt;status == -EINPROGRESS) {
+		dbg (__FUNCTION__ " - already writing");
+		return (0);
+	}
+
+	down (&amp;port-&gt;sem);
+
+	if( (count+priv-&gt;wrfilled)&gt;sizeof(priv-&gt;wrbuf) ) {
+		/* To much data  for buffer. Reset buffer. */
+		priv-&gt;wrfilled=0;
+		return (0);
+	}
+
+	/* Copy data */
+	if (from_user) {
+		if (copy_from_user(priv-&gt;wrbuf+priv-&gt;wrfilled, buf, count))
+			return -EFAULT;
+	} else {
+		memcpy (priv-&gt;wrbuf+priv-&gt;wrfilled, buf, count);
+	}  
+	usb_serial_debug_data (__FILE__, __FUNCTION__, count,
+		priv-&gt;wrbuf+priv-&gt;wrfilled);
+	priv-&gt;wrfilled += count;
+
+	if( priv-&gt;wrfilled &gt;= 3 ) {
+		wrexpected = ((int)priv-&gt;wrbuf[2]&lt;&lt;8)+priv-&gt;wrbuf[1]+3;
+		dbg(__FUNCTION__ " - expected data: %d", wrexpected);
+	} else {
+		wrexpected = sizeof(priv-&gt;wrbuf);
+	}
+
+	if( priv-&gt;wrfilled &gt;= wrexpected ) {
+		/* We have enough data to begin transmission */
+		int length;
+
+		dbg(__FUNCTION__ " - transmitting data (frame 1)");
+		length = (wrexpected &gt; port-&gt;bulk_out_size) ? port-&gt;bulk_out_size : wrexpected;
+
+		memcpy (port-&gt;write_urb-&gt;transfer_buffer, priv-&gt;wrbuf, length );
+		priv-&gt;wrsent=length;
+
+		/* set up our urb */
+		FILL_BULK_URB(port-&gt;write_urb, serial-&gt;dev, 
+			      usb_sndbulkpipe(serial-&gt;dev, port-&gt;bulk_out_endpointAddress),
+			      port-&gt;write_urb-&gt;transfer_buffer, length,
+			      ((serial-&gt;type-&gt;write_bulk_callback) ? 
+			       serial-&gt;type-&gt;write_bulk_callback : 
+			       cyberjack_write_bulk_callback), 
+			      port);
+
+		/* send the data out the bulk port */
+		result = usb_submit_urb(port-&gt;write_urb);
+		if (result) {
+			err(__FUNCTION__ " - failed submitting write urb, error %d", result);
+			/* Throw away data. No better idea what to do with it. */
+			priv-&gt;wrfilled=0;
+			priv-&gt;wrsent=0;
+			up (&amp;port-&gt;sem);
+			return 0;
+		}
+
+		dbg(__FUNCTION__ " - priv-&gt;wrsent=%d",priv-&gt;wrsent);
+		dbg(__FUNCTION__ " - priv-&gt;wrfilled=%d",priv-&gt;wrfilled);
+
+		if( priv-&gt;wrsent&gt;=priv-&gt;wrfilled ) {
+			dbg(__FUNCTION__ " - buffer cleaned");
+			memset( priv-&gt;wrbuf, 0, sizeof(priv-&gt;wrbuf) );
+			priv-&gt;wrfilled=0;
+			priv-&gt;wrsent=0;
+		}
+	}
+
+	up (&amp;port-&gt;sem);
+	return (count);
+} 
+
+static void cyberjack_read_int_callback( struct urb *urb )
+{
+	struct usb_serial_port *port = (struct usb_serial_port *)urb-&gt;context;
+	struct cyberjack_private *priv = (struct cyberjack_private *)port-&gt;private;
+	struct usb_serial *serial;
+	unsigned char *data = urb-&gt;transfer_buffer;
+
+	if (port_paranoia_check (port, __FUNCTION__)) return;
+
+	dbg(__FUNCTION__ " - port %d", port-&gt;number);
+
+	/* the urb might have been killed. */
+	if (urb-&gt;status)
+		return;
+
+	serial = port-&gt;serial;
+	if (serial_paranoia_check (serial, __FUNCTION__)) return;
+
+	usb_serial_debug_data (__FILE__, __FUNCTION__, urb-&gt;actual_length, data);
+
+	/* React only to interrupts signaling a bulk_in transfer */
+	if( (urb-&gt;actual_length==4) &amp;&amp; (data[0]==0x01) ) {
+		short old_rdtodo = priv-&gt;rdtodo;
+		int result;
+
+		/* This is a announcement of comming bulk_ins. */
+		unsigned short size = ((unsigned short)data[3]&lt;&lt;8)+data[2]+3;
+
+		if( (size&gt;259) || (size==0) ) {
+			dbg( "Bad announced bulk_in data length: %d", size );
+			/* Dunno what is most reliable to do here. */
+			/* return; */
+		}
+
+		if( (old_rdtodo+size)&lt;(old_rdtodo) ) {
+			dbg( "To many bulk_in urbs to do." );
+			return;
+		}
+
+		/* "+=" is probably more fault tollerant than "=" */
+		priv-&gt;rdtodo += size;
+
+		dbg(__FUNCTION__ " - rdtodo: %d", priv-&gt;rdtodo);
+
+		if( !old_rdtodo ) {
+			port-&gt;read_urb-&gt;dev = port-&gt;serial-&gt;dev;
+			result = usb_submit_urb(port-&gt;read_urb);
+			if( result )
+				err(__FUNCTION__ " - failed resubmitting read urb, error %d", result);
+			dbg(__FUNCTION__ " - usb_submit_urb(read urb)");
+		}
+	}
+}
+
+static void cyberjack_read_bulk_callback (struct urb *urb)
+{
+	struct usb_serial_port *port = (struct usb_serial_port *)urb-&gt;context;
+	struct cyberjack_private *priv = (struct cyberjack_private *)port-&gt;private;
+	struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
+	struct tty_struct *tty;
+	unsigned char *data = urb-&gt;transfer_buffer;
+	int i;
+	int result;
+
+	dbg(__FUNCTION__ " - port %d", port-&gt;number);
+	
+	if (!serial) {
+		dbg(__FUNCTION__ " - bad serial pointer, exiting");
+		return;
+	}
+
+	if (urb-&gt;status) {
+		usb_serial_debug_data (__FILE__, __FUNCTION__, urb-&gt;actual_length, urb-&gt;transfer_buffer);
+		dbg(__FUNCTION__ " - nonzero read bulk status received: %d", urb-&gt;status);
+		return;
+	}
+
+	usb_serial_debug_data (__FILE__, __FUNCTION__, urb-&gt;actual_length, data);
+
+	tty = port-&gt;tty;
+	if (urb-&gt;actual_length) {
+		for (i = 0; i &lt; urb-&gt;actual_length ; ++i) {
+			/* if we insert more than TTY_FLIPBUF_SIZE characters, we drop them. */
+			if(tty-&gt;flip.count &gt;= TTY_FLIPBUF_SIZE) {
+				tty_flip_buffer_push(tty);
+			}
+			/* this doesn't actually push the data through unless tty-&gt;low_latency is set */
+			tty_insert_flip_char(tty, data[i], 0);
+		}
+	  	tty_flip_buffer_push(tty);
+	}
+
+	/* Reduce urbs to do by one. */
+	priv-&gt;rdtodo-=urb-&gt;actual_length;
+	/* Just to be sure */
+	if( priv-&gt;rdtodo&lt;0 ) priv-&gt;rdtodo=0;
+
+	dbg(__FUNCTION__ " - rdtodo: %d", priv-&gt;rdtodo);
+
+	/* Continue to read if we have still urbs to do. */
+	if( priv-&gt;rdtodo /* || (urb-&gt;actual_length==port-&gt;bulk_in_endpointAddress)*/ ) {
+		port-&gt;read_urb-&gt;dev = port-&gt;serial-&gt;dev;
+		result = usb_submit_urb(port-&gt;read_urb);
+		if (result)
+			err(__FUNCTION__ " - failed resubmitting read urb, error %d", result);
+		dbg(__FUNCTION__ " - usb_submit_urb(read urb)");
+	}
+}
+
+static void cyberjack_write_bulk_callback (struct urb *urb)
+{
+	struct usb_serial_port *port = (struct usb_serial_port *)urb-&gt;context;
+	struct cyberjack_private *priv = (struct cyberjack_private *)port-&gt;private;
+	struct usb_serial *serial = get_usb_serial (port, __FUNCTION__);
+
+	dbg(__FUNCTION__ " - port %d", port-&gt;number);
+	
+	if (!serial) {
+		dbg(__FUNCTION__ " - bad serial pointer, exiting");
+		return;
+	}
+
+	if (urb-&gt;status) {
+		dbg(__FUNCTION__ " - nonzero write bulk status received: %d", urb-&gt;status);
+		return;
+	}
+
+	/* only do something if we have more data to send */
+	if( priv-&gt;wrfilled ) {
+		int length, blksize, result;
+
+		if (port-&gt;write_urb-&gt;status == -EINPROGRESS) {
+			dbg (__FUNCTION__ " - already writing");
+			return;
+		}
+
+		down (&amp;port-&gt;sem);
+
+		dbg(__FUNCTION__ " - transmitting data (frame n)");
+
+		length = ((priv-&gt;wrfilled - priv-&gt;wrsent) &gt; port-&gt;bulk_out_size) ?
+			port-&gt;bulk_out_size : (priv-&gt;wrfilled - priv-&gt;wrsent);
+
+		memcpy (port-&gt;write_urb-&gt;transfer_buffer, priv-&gt;wrbuf + priv-&gt;wrsent,
+			length );
+		priv-&gt;wrsent+=length;
+
+		/* set up our urb */
+		FILL_BULK_URB(port-&gt;write_urb, serial-&gt;dev, 
+			      usb_sndbulkpipe(serial-&gt;dev, port-&gt;bulk_out_endpointAddress),
+			      port-&gt;write_urb-&gt;transfer_buffer, length,
+			      ((serial-&gt;type-&gt;write_bulk_callback) ? 
+			       serial-&gt;type-&gt;write_bulk_callback : 
+			       cyberjack_write_bulk_callback), 
+			      port);
+
+		/* send the data out the bulk port */
+		result = usb_submit_urb(port-&gt;write_urb);
+		if (result) {
+			err(__FUNCTION__ " - failed submitting write urb, error %d", result);
+			/* Throw away data. No better idea what to do with it. */
+			priv-&gt;wrfilled=0;
+			priv-&gt;wrsent=0;
+			up (&amp;port-&gt;sem);
+			queue_task(&amp;port-&gt;tqueue, &amp;tq_immediate);
+			mark_bh(IMMEDIATE_BH);
+			return;
+		}
+
+		dbg(__FUNCTION__ " - priv-&gt;wrsent=%d",priv-&gt;wrsent);
+		dbg(__FUNCTION__ " - priv-&gt;wrfilled=%d",priv-&gt;wrfilled);
+
+		blksize = ((int)priv-&gt;wrbuf[2]&lt;&lt;8)+priv-&gt;wrbuf[1]+3;
+
+		if( (priv-&gt;wrsent&gt;=priv-&gt;wrfilled) || (priv-&gt;wrsent&gt;=blksize) ) {
+			dbg(__FUNCTION__ " - buffer cleaned");
+			memset( priv-&gt;wrbuf, 0, sizeof(priv-&gt;wrbuf) );
+			priv-&gt;wrfilled=0;
+			priv-&gt;wrsent=0;
+		}
+
+		up (&amp;port-&gt;sem);
+		queue_task(&amp;port-&gt;tqueue, &amp;tq_immediate);
+		mark_bh(IMMEDIATE_BH);
+		return;
+	}
+
+	queue_task(&amp;port-&gt;tqueue, &amp;tq_immediate);
+	mark_bh(IMMEDIATE_BH);
+	
+	return;
+}
+
+static int __init cyberjack_init (void)
+{
+	usb_serial_register (&amp;cyberjack_device);
+
+	info(DRIVER_VERSION " " DRIVER_AUTHOR);
+	info(DRIVER_DESC);
+
+	return 0;
+}
+
+static void __exit cyberjack_exit (void)
+{
+	usb_serial_deregister (&amp;cyberjack_device);
+}
+
+module_init(cyberjack_init);
+module_exit(cyberjack_exit);
+
+MODULE_AUTHOR( DRIVER_AUTHOR );
+MODULE_DESCRIPTION( DRIVER_DESC );
+MODULE_LICENSE("GPL");
+
+MODULE_PARM(debug, "i");
+MODULE_PARM_DESC(debug, "Debug enabled or not");
+

</pre></body></html>