Release 4.9 drivers/usb/serial/cyberjack.c
  
  
/*
 *  REINER SCT cyberJack pinpad/e-com USB Chipcard Reader Driver
 *
 *  Copyright (C) 2001  REINER SCT
 *  Author: Matthias Bruestle
 *
 *  Contact: support@reiner-sct.com (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.
 *
 *  Please note that later models of the cyberjack reader family are
 *  supported by a libusb-based userspace device driver.
 *
 *  Homepage: http://www.reiner-sct.de/support/treiber_cyberjack.php#linux
 */
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/slab.h>
#include <linux/tty.h>
#include <linux/tty_driver.h>
#include <linux/tty_flip.h>
#include <linux/module.h>
#include <linux/spinlock.h>
#include <linux/uaccess.h>
#include <linux/usb.h>
#include <linux/usb/serial.h>
#define CYBERJACK_LOCAL_BUF_SIZE 32
#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_port_probe(struct usb_serial_port *port);
static int cyberjack_port_remove(struct usb_serial_port *port);
static int  cyberjack_open(struct tty_struct *tty,
	struct usb_serial_port *port);
static void cyberjack_close(struct usb_serial_port *port);
static int cyberjack_write(struct tty_struct *tty,
	struct usb_serial_port *port, const unsigned char *buf, int count);
static int cyberjack_write_room(struct tty_struct *tty);
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 const struct usb_device_id id_table[] = {
	{ USB_DEVICE(CYBERJACK_VENDOR_ID, CYBERJACK_PRODUCT_ID) },
	{ }			/* Terminating entry */
};
MODULE_DEVICE_TABLE(usb, id_table);
static struct usb_serial_driver cyberjack_device = {
	.driver = {
		.owner =	THIS_MODULE,
		.name =		"cyberjack",
        },
	.description =		"Reiner SCT Cyberjack USB card reader",
	.id_table =		id_table,
	.num_ports =		1,
	.port_probe =		cyberjack_port_probe,
	.port_remove =		cyberjack_port_remove,
	.open =			cyberjack_open,
	.close =		cyberjack_close,
	.write =		cyberjack_write,
	.write_room =		cyberjack_write_room,
	.read_int_callback =	cyberjack_read_int_callback,
	.read_bulk_callback =	cyberjack_read_bulk_callback,
	.write_bulk_callback =	cyberjack_write_bulk_callback,
};
static struct usb_serial_driver * const serial_drivers[] = {
	&cyberjack_device, NULL
};
struct cyberjack_private {
	
spinlock_t	lock;		/* Lock for SMP */
	
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 already sent */
};
static int cyberjack_port_probe(struct usb_serial_port *port)
{
	struct cyberjack_private *priv;
	int result;
	priv = kmalloc(sizeof(struct cyberjack_private), GFP_KERNEL);
	if (!priv)
		return -ENOMEM;
	spin_lock_init(&priv->lock);
	priv->rdtodo = 0;
	priv->wrfilled = 0;
	priv->wrsent = 0;
	usb_set_serial_port_data(port, priv);
	result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
	if (result)
		dev_err(&port->dev, "usb_submit_urb(read int) failed\n");
	return 0;
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| linus torvalds | linus torvalds | 53 | 51.96% | 1 | 14.29% | 
| harald welte | harald welte | 18 | 17.65% | 1 | 14.29% | 
| greg kroah-hartman | greg kroah-hartman | 18 | 17.65% | 3 | 42.86% | 
| matthias bruestle | matthias bruestle | 8 | 7.84% | 1 | 14.29% | 
| johan hovold | johan hovold | 5 | 4.90% | 1 | 14.29% | 
 | Total | 102 | 100.00% | 7 | 100.00% | 
static int cyberjack_port_remove(struct usb_serial_port *port)
{
	struct cyberjack_private *priv;
	usb_kill_urb(port->interrupt_in_urb);
	priv = usb_get_serial_port_data(port);
	kfree(priv);
	return 0;
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| johan hovold | johan hovold | 25 | 65.79% | 2 | 40.00% | 
| linus torvalds | linus torvalds | 8 | 21.05% | 1 | 20.00% | 
| harald welte | harald welte | 4 | 10.53% | 1 | 20.00% | 
| alan stern | alan stern | 1 | 2.63% | 1 | 20.00% | 
 | Total | 38 | 100.00% | 5 | 100.00% | 
static int  cyberjack_open(struct tty_struct *tty,
					struct usb_serial_port *port)
{
	struct cyberjack_private *priv;
	unsigned long flags;
	dev_dbg(&port->dev, "%s - usb_clear_halt\n", __func__);
	usb_clear_halt(port->serial->dev, port->write_urb->pipe);
	priv = usb_get_serial_port_data(port);
	spin_lock_irqsave(&priv->lock, flags);
	priv->rdtodo = 0;
	priv->wrfilled = 0;
	priv->wrsent = 0;
	spin_unlock_irqrestore(&priv->lock, flags);
	return 0;
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| matthias bruestle | matthias bruestle | 42 | 42.00% | 2 | 25.00% | 
| linus torvalds | linus torvalds | 41 | 41.00% | 1 | 12.50% | 
| greg kroah-hartman | greg kroah-hartman | 10 | 10.00% | 2 | 25.00% | 
| alan cox | alan cox | 5 | 5.00% | 1 | 12.50% | 
| harvey harrison | harvey harrison | 1 | 1.00% | 1 | 12.50% | 
| mathieu othacehe | mathieu othacehe | 1 | 1.00% | 1 | 12.50% | 
 | Total | 100 | 100.00% | 8 | 100.00% | 
static void cyberjack_close(struct usb_serial_port *port)
{
	usb_kill_urb(port->write_urb);
	usb_kill_urb(port->read_urb);
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| linus torvalds | linus torvalds | 23 | 92.00% | 2 | 66.67% | 
| greg kroah-hartman | greg kroah-hartman | 2 | 8.00% | 1 | 33.33% | 
 | Total | 25 | 100.00% | 3 | 100.00% | 
static int cyberjack_write(struct tty_struct *tty,
	struct usb_serial_port *port, const unsigned char *buf, int count)
{
	struct device *dev = &port->dev;
	struct cyberjack_private *priv = usb_get_serial_port_data(port);
	unsigned long flags;
	int result;
	int wrexpected;
	if (count == 0) {
		dev_dbg(dev, "%s - write request of 0 bytes\n", __func__);
		return 0;
	}
	if (!test_and_clear_bit(0, &port->write_urbs_free)) {
		dev_dbg(dev, "%s - already writing\n", __func__);
		return 0;
	}
	spin_lock_irqsave(&priv->lock, flags);
	if (count+priv->wrfilled > sizeof(priv->wrbuf)) {
		/* To much data for buffer. Reset buffer. */
		priv->wrfilled = 0;
		spin_unlock_irqrestore(&priv->lock, flags);
		set_bit(0, &port->write_urbs_free);
		return 0;
	}
	/* Copy data */
	memcpy(priv->wrbuf + priv->wrfilled, buf, count);
	usb_serial_debug_data(dev, __func__, count, priv->wrbuf + priv->wrfilled);
	priv->wrfilled += count;
	if (priv->wrfilled >= 3) {
		wrexpected = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
		dev_dbg(dev, "%s - expected data: %d\n", __func__, wrexpected);
	} else
		wrexpected = sizeof(priv->wrbuf);
	if (priv->wrfilled >= wrexpected) {
		/* We have enough data to begin transmission */
		int length;
		dev_dbg(dev, "%s - transmitting data (frame 1)\n", __func__);
		length = (wrexpected > port->bulk_out_size) ?
					port->bulk_out_size : wrexpected;
		memcpy(port->write_urb->transfer_buffer, priv->wrbuf, length);
		priv->wrsent = length;
		/* set up our urb */
		port->write_urb->transfer_buffer_length = length;
		/* send the data out the bulk port */
		result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
		if (result) {
			dev_err(&port->dev,
				"%s - failed submitting write urb, error %d\n",
				__func__, result);
			/* Throw away data. No better idea what to do with it. */
			priv->wrfilled = 0;
			priv->wrsent = 0;
			spin_unlock_irqrestore(&priv->lock, flags);
			set_bit(0, &port->write_urbs_free);
			return 0;
		}
		dev_dbg(dev, "%s - priv->wrsent=%d\n", __func__, priv->wrsent);
		dev_dbg(dev, "%s - priv->wrfilled=%d\n", __func__, priv->wrfilled);
		if (priv->wrsent >= priv->wrfilled) {
			dev_dbg(dev, "%s - buffer cleaned\n", __func__);
			memset(priv->wrbuf, 0, sizeof(priv->wrbuf));
			priv->wrfilled = 0;
			priv->wrsent = 0;
		}
	}
	spin_unlock_irqrestore(&priv->lock, flags);
	return count;
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| linus torvalds | linus torvalds | 326 | 68.06% | 1 | 6.25% | 
| greg kroah-hartman | greg kroah-hartman | 65 | 13.57% | 7 | 43.75% | 
| matthias bruestle | matthias bruestle | 44 | 9.19% | 1 | 6.25% | 
| johan hovold | johan hovold | 27 | 5.64% | 3 | 18.75% | 
| harvey harrison | harvey harrison | 9 | 1.88% | 1 | 6.25% | 
| alan cox | alan cox | 5 | 1.04% | 1 | 6.25% | 
| siegfried hildebrand | siegfried hildebrand | 2 | 0.42% | 1 | 6.25% | 
| ganesh varadarajan | ganesh varadarajan | 1 | 0.21% | 1 | 6.25% | 
 | Total | 479 | 100.00% | 16 | 100.00% | 
static int cyberjack_write_room(struct tty_struct *tty)
{
	/* FIXME: .... */
	return CYBERJACK_LOCAL_BUF_SIZE;
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| matthias bruestle | matthias bruestle | 12 | 80.00% | 1 | 33.33% | 
| alan cox | alan cox | 3 | 20.00% | 2 | 66.67% | 
 | Total | 15 | 100.00% | 3 | 100.00% | 
static void cyberjack_read_int_callback(struct urb *urb)
{
	struct usb_serial_port *port = urb->context;
	struct cyberjack_private *priv = usb_get_serial_port_data(port);
	struct device *dev = &port->dev;
	unsigned char *data = urb->transfer_buffer;
	int status = urb->status;
	int result;
	/* the urb might have been killed. */
	if (status)
		return;
	usb_serial_debug_data(dev, __func__, urb->actual_length, data);
	/* React only to interrupts signaling a bulk_in transfer */
	if (urb->actual_length == 4 && data[0] == 0x01) {
		short old_rdtodo;
		/* This is a announcement of coming bulk_ins. */
		unsigned short size = ((unsigned short)data[3]<<8)+data[2]+3;
		spin_lock(&priv->lock);
		old_rdtodo = priv->rdtodo;
		if (old_rdtodo > SHRT_MAX - size) {
			dev_dbg(dev, "To many bulk_in urbs to do.\n");
			spin_unlock(&priv->lock);
			goto resubmit;
		}
		/* "+=" is probably more fault tolerant than "=" */
		priv->rdtodo += size;
		dev_dbg(dev, "%s - rdtodo: %d\n", __func__, priv->rdtodo);
		spin_unlock(&priv->lock);
		if (!old_rdtodo) {
			result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
			if (result)
				dev_err(dev, "%s - failed resubmitting read urb, error %d\n",
					__func__, result);
			dev_dbg(dev, "%s - usb_submit_urb(read urb)\n", __func__);
		}
	}
resubmit:
	result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
	if (result)
		dev_err(&port->dev, "usb_submit_urb(read int) failed\n");
	dev_dbg(dev, "%s - usb_submit_urb(int urb)\n", __func__);
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| linus torvalds | linus torvalds | 152 | 55.88% | 1 | 7.14% | 
| matthias bruestle | matthias bruestle | 53 | 19.49% | 1 | 7.14% | 
| greg kroah-hartman | greg kroah-hartman | 52 | 19.12% | 7 | 50.00% | 
| oliver neukum | oliver neukum | 5 | 1.84% | 1 | 7.14% | 
| harvey harrison | harvey harrison | 5 | 1.84% | 1 | 7.14% | 
| dan carpenter | dan carpenter | 3 | 1.10% | 1 | 7.14% | 
| steven cole | steven cole | 1 | 0.37% | 1 | 7.14% | 
| rahul bedarkar | rahul bedarkar | 1 | 0.37% | 1 | 7.14% | 
 | Total | 272 | 100.00% | 14 | 100.00% | 
static void cyberjack_read_bulk_callback(struct urb *urb)
{
	struct usb_serial_port *port = urb->context;
	struct cyberjack_private *priv = usb_get_serial_port_data(port);
	struct device *dev = &port->dev;
	unsigned char *data = urb->transfer_buffer;
	short todo;
	int result;
	int status = urb->status;
	usb_serial_debug_data(dev, __func__, urb->actual_length, data);
	if (status) {
		dev_dbg(dev, "%s - nonzero read bulk status received: %d\n",
			__func__, status);
		return;
	}
	if (urb->actual_length) {
		tty_insert_flip_string(&port->port, data, urb->actual_length);
		tty_flip_buffer_push(&port->port);
	}
	spin_lock(&priv->lock);
	/* Reduce urbs to do by one. */
	priv->rdtodo -= urb->actual_length;
	/* Just to be sure */
	if (priv->rdtodo < 0)
		priv->rdtodo = 0;
	todo = priv->rdtodo;
	spin_unlock(&priv->lock);
	dev_dbg(dev, "%s - rdtodo: %d\n", __func__, todo);
	/* Continue to read if we have still urbs to do. */
	if (todo /* || (urb->actual_length==port->bulk_in_endpointAddress)*/) {
		result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
		if (result)
			dev_err(dev, "%s - failed resubmitting read urb, error %d\n",
				__func__, result);
		dev_dbg(dev, "%s - usb_submit_urb(read urb)\n", __func__);
	}
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| linus torvalds | linus torvalds | 129 | 57.85% | 1 | 6.67% | 
| greg kroah-hartman | greg kroah-hartman | 47 | 21.08% | 7 | 46.67% | 
| matthias bruestle | matthias bruestle | 25 | 11.21% | 1 | 6.67% | 
| jiri slaby | jiri slaby | 8 | 3.59% | 2 | 13.33% | 
| harvey harrison | harvey harrison | 5 | 2.24% | 1 | 6.67% | 
| alan cox | alan cox | 4 | 1.79% | 1 | 6.67% | 
| oliver neukum | oliver neukum | 3 | 1.35% | 1 | 6.67% | 
| harald welte | harald welte | 2 | 0.90% | 1 | 6.67% | 
 | Total | 223 | 100.00% | 15 | 100.00% | 
static void cyberjack_write_bulk_callback(struct urb *urb)
{
	struct usb_serial_port *port = urb->context;
	struct cyberjack_private *priv = usb_get_serial_port_data(port);
	struct device *dev = &port->dev;
	int status = urb->status;
	set_bit(0, &port->write_urbs_free);
	if (status) {
		dev_dbg(dev, "%s - nonzero write bulk status received: %d\n",
			__func__, status);
		return;
	}
	spin_lock(&priv->lock);
	/* only do something if we have more data to send */
	if (priv->wrfilled) {
		int length, blksize, result;
		dev_dbg(dev, "%s - transmitting data (frame n)\n", __func__);
		length = ((priv->wrfilled - priv->wrsent) > port->bulk_out_size) ?
			port->bulk_out_size : (priv->wrfilled - priv->wrsent);
		memcpy(port->write_urb->transfer_buffer,
					priv->wrbuf + priv->wrsent, length);
		priv->wrsent += length;
		/* set up our urb */
		port->write_urb->transfer_buffer_length = length;
		/* send the data out the bulk port */
		result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
		if (result) {
			dev_err(dev, "%s - failed submitting write urb, error %d\n",
				__func__, result);
			/* Throw away data. No better idea what to do with it. */
			priv->wrfilled = 0;
			priv->wrsent = 0;
			goto exit;
		}
		dev_dbg(dev, "%s - priv->wrsent=%d\n", __func__, priv->wrsent);
		dev_dbg(dev, "%s - priv->wrfilled=%d\n", __func__, priv->wrfilled);
		blksize = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
		if (priv->wrsent >= priv->wrfilled ||
					priv->wrsent >= blksize) {
			dev_dbg(dev, "%s - buffer cleaned\n", __func__);
			memset(priv->wrbuf, 0, sizeof(priv->wrbuf));
			priv->wrfilled = 0;
			priv->wrsent = 0;
		}
	}
exit:
	spin_unlock(&priv->lock);
	usb_serial_port_softint(port);
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| linus torvalds | linus torvalds | 248 | 72.94% | 1 | 6.25% | 
| greg kroah-hartman | greg kroah-hartman | 59 | 17.35% | 8 | 50.00% | 
| matthias bruestle | matthias bruestle | 14 | 4.12% | 1 | 6.25% | 
| johan hovold | johan hovold | 9 | 2.65% | 2 | 12.50% | 
| harvey harrison | harvey harrison | 6 | 1.76% | 1 | 6.25% | 
| oliver neukum | oliver neukum | 2 | 0.59% | 1 | 6.25% | 
| ganesh varadarajan | ganesh varadarajan | 1 | 0.29% | 1 | 6.25% | 
| pete zaitcev | pete zaitcev | 1 | 0.29% | 1 | 6.25% | 
 | Total | 340 | 100.00% | 16 | 100.00% | 
module_usb_serial_driver(serial_drivers, id_table);
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE("GPL");
Overall Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| linus torvalds | linus torvalds | 1202 | 62.09% | 5 | 9.26% | 
| greg kroah-hartman | greg kroah-hartman | 277 | 14.31% | 21 | 38.89% | 
| matthias bruestle | matthias bruestle | 219 | 11.31% | 2 | 3.70% | 
| johan hovold | johan hovold | 78 | 4.03% | 5 | 9.26% | 
| alan cox | alan cox | 30 | 1.55% | 4 | 7.41% | 
| harvey harrison | harvey harrison | 26 | 1.34% | 1 | 1.85% | 
| alan stern | alan stern | 26 | 1.34% | 2 | 3.70% | 
| harald welte | harald welte | 25 | 1.29% | 2 | 3.70% | 
| rusty russell | rusty russell | 22 | 1.14% | 1 | 1.85% | 
| oliver neukum | oliver neukum | 10 | 0.52% | 1 | 1.85% | 
| jiri slaby | jiri slaby | 8 | 0.41% | 2 | 3.70% | 
| dan carpenter | dan carpenter | 3 | 0.15% | 1 | 1.85% | 
| siegfried hildebrand | siegfried hildebrand | 3 | 0.15% | 1 | 1.85% | 
| ganesh varadarajan | ganesh varadarajan | 2 | 0.10% | 1 | 1.85% | 
| marton nemeth | marton nemeth | 1 | 0.05% | 1 | 1.85% | 
| steven cole | steven cole | 1 | 0.05% | 1 | 1.85% | 
| mathieu othacehe | mathieu othacehe | 1 | 0.05% | 1 | 1.85% | 
| pete zaitcev | pete zaitcev | 1 | 0.05% | 1 | 1.85% | 
| rahul bedarkar | rahul bedarkar | 1 | 0.05% | 1 | 1.85% | 
 | Total | 1936 | 100.00% | 54 | 100.00% |