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);
50c5d1448fSUwe 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);
56*94cc7aeaSJiri Slaby static unsigned 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 = {
70269bda1cSGreg Kroah-Hartman .name = "cyberjack",
7118fcac35SGreg Kroah-Hartman },
72269bda1cSGreg Kroah-Hartman .description = "Reiner SCT Cyberjack USB card reader",
731da177e4SLinus Torvalds .id_table = id_table,
741da177e4SLinus Torvalds .num_ports = 1,
75d183b9b4SJohan Hovold .num_bulk_out = 1,
76a9556040SJohan Hovold .port_probe = cyberjack_port_probe,
77a9556040SJohan Hovold .port_remove = cyberjack_port_remove,
781da177e4SLinus Torvalds .open = cyberjack_open,
791da177e4SLinus Torvalds .close = cyberjack_close,
801da177e4SLinus Torvalds .write = cyberjack_write,
811da177e4SLinus Torvalds .write_room = cyberjack_write_room,
821da177e4SLinus Torvalds .read_int_callback = cyberjack_read_int_callback,
831da177e4SLinus Torvalds .read_bulk_callback = cyberjack_read_bulk_callback,
841da177e4SLinus Torvalds .write_bulk_callback = cyberjack_write_bulk_callback,
851da177e4SLinus Torvalds };
861da177e4SLinus Torvalds
8708a4f6bcSAlan Stern static struct usb_serial_driver * const serial_drivers[] = {
8808a4f6bcSAlan Stern &cyberjack_device, NULL
8908a4f6bcSAlan Stern };
9008a4f6bcSAlan Stern
911da177e4SLinus Torvalds struct cyberjack_private {
921da177e4SLinus Torvalds spinlock_t lock; /* Lock for SMP */
931da177e4SLinus Torvalds short rdtodo; /* Bytes still to read */
941da177e4SLinus Torvalds unsigned char wrbuf[5*64]; /* Buffer for collecting data to write */
951da177e4SLinus Torvalds short wrfilled; /* Overall data size we already got */
961da177e4SLinus Torvalds short wrsent; /* Data already sent */
971da177e4SLinus Torvalds };
981da177e4SLinus Torvalds
cyberjack_port_probe(struct usb_serial_port * port)99a9556040SJohan Hovold static int cyberjack_port_probe(struct usb_serial_port *port)
1001da177e4SLinus Torvalds {
1011da177e4SLinus Torvalds struct cyberjack_private *priv;
102a9556040SJohan Hovold int result;
1031da177e4SLinus Torvalds
1041da177e4SLinus Torvalds priv = kmalloc(sizeof(struct cyberjack_private), GFP_KERNEL);
1051da177e4SLinus Torvalds if (!priv)
1061da177e4SLinus Torvalds return -ENOMEM;
1071da177e4SLinus Torvalds
1081da177e4SLinus Torvalds spin_lock_init(&priv->lock);
1091da177e4SLinus Torvalds priv->rdtodo = 0;
1101da177e4SLinus Torvalds priv->wrfilled = 0;
1111da177e4SLinus Torvalds priv->wrsent = 0;
1121da177e4SLinus Torvalds
113a9556040SJohan Hovold usb_set_serial_port_data(port, priv);
1141da177e4SLinus Torvalds
115a9556040SJohan Hovold result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
1161da177e4SLinus Torvalds if (result)
117a9556040SJohan Hovold dev_err(&port->dev, "usb_submit_urb(read int) failed\n");
118a9556040SJohan Hovold
119a9556040SJohan Hovold return 0;
1201da177e4SLinus Torvalds }
1211da177e4SLinus Torvalds
cyberjack_port_remove(struct usb_serial_port * port)122c5d1448fSUwe Kleine-König static void cyberjack_port_remove(struct usb_serial_port *port)
123a9556040SJohan Hovold {
124a9556040SJohan Hovold struct cyberjack_private *priv;
125a9556040SJohan Hovold
12630d9d42eSJohan Hovold usb_kill_urb(port->interrupt_in_urb);
12730d9d42eSJohan Hovold
128a9556040SJohan Hovold priv = usb_get_serial_port_data(port);
129a9556040SJohan Hovold kfree(priv);
1301da177e4SLinus Torvalds }
1311da177e4SLinus Torvalds
cyberjack_open(struct tty_struct * tty,struct usb_serial_port * port)13295da310eSAlan Cox static int cyberjack_open(struct tty_struct *tty,
133a509a7e4SAlan Cox struct usb_serial_port *port)
1341da177e4SLinus Torvalds {
1351da177e4SLinus Torvalds struct cyberjack_private *priv;
1361da177e4SLinus Torvalds unsigned long flags;
1371da177e4SLinus Torvalds
13896fc8e8dSGreg Kroah-Hartman dev_dbg(&port->dev, "%s - usb_clear_halt\n", __func__);
1391da177e4SLinus Torvalds usb_clear_halt(port->serial->dev, port->write_urb->pipe);
1401da177e4SLinus Torvalds
1411da177e4SLinus Torvalds priv = usb_get_serial_port_data(port);
1421da177e4SLinus Torvalds spin_lock_irqsave(&priv->lock, flags);
1431da177e4SLinus Torvalds priv->rdtodo = 0;
1441da177e4SLinus Torvalds priv->wrfilled = 0;
1451da177e4SLinus Torvalds priv->wrsent = 0;
1461da177e4SLinus Torvalds spin_unlock_irqrestore(&priv->lock, flags);
1471da177e4SLinus Torvalds
1480f3083afSMathieu OTHACEHE return 0;
1491da177e4SLinus Torvalds }
1501da177e4SLinus Torvalds
cyberjack_close(struct usb_serial_port * port)151335f8514SAlan Cox static void cyberjack_close(struct usb_serial_port *port)
1521da177e4SLinus Torvalds {
1531da177e4SLinus Torvalds usb_kill_urb(port->write_urb);
1541da177e4SLinus Torvalds usb_kill_urb(port->read_urb);
1551da177e4SLinus Torvalds }
1561da177e4SLinus Torvalds
cyberjack_write(struct tty_struct * tty,struct usb_serial_port * port,const unsigned char * buf,int count)15795da310eSAlan Cox static int cyberjack_write(struct tty_struct *tty,
15895da310eSAlan Cox struct usb_serial_port *port, const unsigned char *buf, int count)
1591da177e4SLinus Torvalds {
16096fc8e8dSGreg Kroah-Hartman struct device *dev = &port->dev;
1611da177e4SLinus Torvalds struct cyberjack_private *priv = usb_get_serial_port_data(port);
1621da177e4SLinus Torvalds unsigned long flags;
1631da177e4SLinus Torvalds int result;
1641da177e4SLinus Torvalds int wrexpected;
1651da177e4SLinus Torvalds
1661da177e4SLinus Torvalds if (count == 0) {
16796fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - write request of 0 bytes\n", __func__);
168a5b6f60cSAlan Cox return 0;
1691da177e4SLinus Torvalds }
1701da177e4SLinus Torvalds
171c1cac10cSJohan Hovold if (!test_and_clear_bit(0, &port->write_urbs_free)) {
17296fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - already writing\n", __func__);
173507ca9bcSGreg Kroah-Hartman return 0;
1741da177e4SLinus Torvalds }
1751da177e4SLinus Torvalds
1761da177e4SLinus Torvalds spin_lock_irqsave(&priv->lock, flags);
1771da177e4SLinus Torvalds
178b9c52f15SAlan Cox if (count+priv->wrfilled > sizeof(priv->wrbuf)) {
1791da177e4SLinus Torvalds /* To much data for buffer. Reset buffer. */
1801da177e4SLinus Torvalds priv->wrfilled = 0;
181a5b6f60cSAlan Cox spin_unlock_irqrestore(&priv->lock, flags);
182c1cac10cSJohan Hovold set_bit(0, &port->write_urbs_free);
183a5b6f60cSAlan Cox return 0;
1841da177e4SLinus Torvalds }
1851da177e4SLinus Torvalds
1861da177e4SLinus Torvalds /* Copy data */
1871da177e4SLinus Torvalds memcpy(priv->wrbuf + priv->wrfilled, buf, count);
1881da177e4SLinus Torvalds
18959d33f2fSGreg Kroah-Hartman usb_serial_debug_data(dev, __func__, count, priv->wrbuf + priv->wrfilled);
1901da177e4SLinus Torvalds priv->wrfilled += count;
1911da177e4SLinus Torvalds
1921da177e4SLinus Torvalds if (priv->wrfilled >= 3) {
1931da177e4SLinus Torvalds wrexpected = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
19496fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - expected data: %d\n", __func__, wrexpected);
195b9c52f15SAlan Cox } else
1961da177e4SLinus Torvalds wrexpected = sizeof(priv->wrbuf);
1971da177e4SLinus Torvalds
1981da177e4SLinus Torvalds if (priv->wrfilled >= wrexpected) {
1991da177e4SLinus Torvalds /* We have enough data to begin transmission */
2001da177e4SLinus Torvalds int length;
2011da177e4SLinus Torvalds
20296fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - transmitting data (frame 1)\n", __func__);
203b9c52f15SAlan Cox length = (wrexpected > port->bulk_out_size) ?
204b9c52f15SAlan Cox port->bulk_out_size : wrexpected;
2051da177e4SLinus Torvalds
2061da177e4SLinus Torvalds memcpy(port->write_urb->transfer_buffer, priv->wrbuf, length);
2071da177e4SLinus Torvalds priv->wrsent = length;
2081da177e4SLinus Torvalds
2091da177e4SLinus Torvalds /* set up our urb */
210fd11961aSJohan Hovold port->write_urb->transfer_buffer_length = length;
2111da177e4SLinus Torvalds
2121da177e4SLinus Torvalds /* send the data out the bulk port */
2131da177e4SLinus Torvalds result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
2141da177e4SLinus Torvalds if (result) {
215194343d9SGreg Kroah-Hartman dev_err(&port->dev,
216d9a38a87SJohan Hovold "%s - failed submitting write urb, error %d\n",
217b9c52f15SAlan Cox __func__, result);
2181da177e4SLinus Torvalds /* Throw away data. No better idea what to do with it. */
2191da177e4SLinus Torvalds priv->wrfilled = 0;
2201da177e4SLinus Torvalds priv->wrsent = 0;
2211da177e4SLinus Torvalds spin_unlock_irqrestore(&priv->lock, flags);
222c1cac10cSJohan Hovold set_bit(0, &port->write_urbs_free);
2231da177e4SLinus Torvalds return 0;
2241da177e4SLinus Torvalds }
2251da177e4SLinus Torvalds
22696fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - priv->wrsent=%d\n", __func__, priv->wrsent);
22796fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - priv->wrfilled=%d\n", __func__, priv->wrfilled);
2281da177e4SLinus Torvalds
2291da177e4SLinus Torvalds if (priv->wrsent >= priv->wrfilled) {
23096fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - buffer cleaned\n", __func__);
2311da177e4SLinus Torvalds memset(priv->wrbuf, 0, sizeof(priv->wrbuf));
2321da177e4SLinus Torvalds priv->wrfilled = 0;
2331da177e4SLinus Torvalds priv->wrsent = 0;
2341da177e4SLinus Torvalds }
2351da177e4SLinus Torvalds }
2361da177e4SLinus Torvalds
2371da177e4SLinus Torvalds spin_unlock_irqrestore(&priv->lock, flags);
2381da177e4SLinus Torvalds
239b9c52f15SAlan Cox return count;
2401da177e4SLinus Torvalds }
2411da177e4SLinus Torvalds
cyberjack_write_room(struct tty_struct * tty)242*94cc7aeaSJiri Slaby static unsigned int cyberjack_write_room(struct tty_struct *tty)
2431da177e4SLinus Torvalds {
244a5b6f60cSAlan Cox /* FIXME: .... */
2451da177e4SLinus Torvalds return CYBERJACK_LOCAL_BUF_SIZE;
2461da177e4SLinus Torvalds }
2471da177e4SLinus Torvalds
cyberjack_read_int_callback(struct urb * urb)2487d12e780SDavid Howells static void cyberjack_read_int_callback(struct urb *urb)
2491da177e4SLinus Torvalds {
250cdc97792SMing Lei struct usb_serial_port *port = urb->context;
2511da177e4SLinus Torvalds struct cyberjack_private *priv = usb_get_serial_port_data(port);
25296fc8e8dSGreg Kroah-Hartman struct device *dev = &port->dev;
2531da177e4SLinus Torvalds unsigned char *data = urb->transfer_buffer;
2547dcc85cdSGreg Kroah-Hartman int status = urb->status;
255c75d18ccSJohn Ogness unsigned long flags;
2561da177e4SLinus Torvalds int result;
2571da177e4SLinus Torvalds
2581da177e4SLinus Torvalds /* the urb might have been killed. */
2597dcc85cdSGreg Kroah-Hartman if (status)
2601da177e4SLinus Torvalds return;
2611da177e4SLinus Torvalds
26259d33f2fSGreg Kroah-Hartman usb_serial_debug_data(dev, __func__, urb->actual_length, data);
2631da177e4SLinus Torvalds
2641da177e4SLinus Torvalds /* React only to interrupts signaling a bulk_in transfer */
265b9c52f15SAlan Cox if (urb->actual_length == 4 && data[0] == 0x01) {
2661da177e4SLinus Torvalds short old_rdtodo;
2671da177e4SLinus Torvalds
2681da177e4SLinus Torvalds /* This is a announcement of coming bulk_ins. */
2691da177e4SLinus Torvalds unsigned short size = ((unsigned short)data[3]<<8)+data[2]+3;
2701da177e4SLinus Torvalds
271c75d18ccSJohn Ogness spin_lock_irqsave(&priv->lock, flags);
2721da177e4SLinus Torvalds
2731da177e4SLinus Torvalds old_rdtodo = priv->rdtodo;
2741da177e4SLinus Torvalds
2755287bf72SDan Carpenter if (old_rdtodo > SHRT_MAX - size) {
27619c64e73SColin Ian King dev_dbg(dev, "Too many bulk_in urbs to do.\n");
277c75d18ccSJohn Ogness spin_unlock_irqrestore(&priv->lock, flags);
2781da177e4SLinus Torvalds goto resubmit;
2791da177e4SLinus Torvalds }
2801da177e4SLinus Torvalds
281cd8c5053SRahul Bedarkar /* "+=" is probably more fault tolerant than "=" */
2821da177e4SLinus Torvalds priv->rdtodo += size;
2831da177e4SLinus Torvalds
28496fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - rdtodo: %d\n", __func__, priv->rdtodo);
2851da177e4SLinus Torvalds
286c75d18ccSJohn Ogness spin_unlock_irqrestore(&priv->lock, flags);
2871da177e4SLinus Torvalds
2881da177e4SLinus Torvalds if (!old_rdtodo) {
2891da177e4SLinus Torvalds result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
2901da177e4SLinus Torvalds if (result)
29196fc8e8dSGreg Kroah-Hartman dev_err(dev, "%s - failed resubmitting read urb, error %d\n",
292194343d9SGreg Kroah-Hartman __func__, result);
29396fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - usb_submit_urb(read urb)\n", __func__);
2941da177e4SLinus Torvalds }
2951da177e4SLinus Torvalds }
2961da177e4SLinus Torvalds
2971da177e4SLinus Torvalds resubmit:
2981da177e4SLinus Torvalds result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
2991da177e4SLinus Torvalds if (result)
300194343d9SGreg Kroah-Hartman dev_err(&port->dev, "usb_submit_urb(read int) failed\n");
30196fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - usb_submit_urb(int urb)\n", __func__);
3021da177e4SLinus Torvalds }
3031da177e4SLinus Torvalds
cyberjack_read_bulk_callback(struct urb * urb)3047d12e780SDavid Howells static void cyberjack_read_bulk_callback(struct urb *urb)
3051da177e4SLinus Torvalds {
306cdc97792SMing Lei struct usb_serial_port *port = urb->context;
3071da177e4SLinus Torvalds struct cyberjack_private *priv = usb_get_serial_port_data(port);
30896fc8e8dSGreg Kroah-Hartman struct device *dev = &port->dev;
3091da177e4SLinus Torvalds unsigned char *data = urb->transfer_buffer;
310c75d18ccSJohn Ogness unsigned long flags;
3111da177e4SLinus Torvalds short todo;
3121da177e4SLinus Torvalds int result;
3137dcc85cdSGreg Kroah-Hartman int status = urb->status;
3141da177e4SLinus Torvalds
31559d33f2fSGreg Kroah-Hartman usb_serial_debug_data(dev, __func__, urb->actual_length, data);
3167dcc85cdSGreg Kroah-Hartman if (status) {
31796fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - nonzero read bulk status received: %d\n",
318441b62c1SHarvey Harrison __func__, status);
3191da177e4SLinus Torvalds return;
3201da177e4SLinus Torvalds }
3211da177e4SLinus Torvalds
3221da177e4SLinus Torvalds if (urb->actual_length) {
32305c7cd39SJiri Slaby tty_insert_flip_string(&port->port, data, urb->actual_length);
3242e124b4aSJiri Slaby tty_flip_buffer_push(&port->port);
3251da177e4SLinus Torvalds }
3261da177e4SLinus Torvalds
327c75d18ccSJohn Ogness spin_lock_irqsave(&priv->lock, flags);
3281da177e4SLinus Torvalds
3291da177e4SLinus Torvalds /* Reduce urbs to do by one. */
3301da177e4SLinus Torvalds priv->rdtodo -= urb->actual_length;
3311da177e4SLinus Torvalds /* Just to be sure */
332b9c52f15SAlan Cox if (priv->rdtodo < 0)
333b9c52f15SAlan Cox priv->rdtodo = 0;
3341da177e4SLinus Torvalds todo = priv->rdtodo;
3351da177e4SLinus Torvalds
336c75d18ccSJohn Ogness spin_unlock_irqrestore(&priv->lock, flags);
3371da177e4SLinus Torvalds
33896fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - rdtodo: %d\n", __func__, todo);
3391da177e4SLinus Torvalds
3401da177e4SLinus Torvalds /* Continue to read if we have still urbs to do. */
3411da177e4SLinus Torvalds if (todo /* || (urb->actual_length==port->bulk_in_endpointAddress)*/) {
3421da177e4SLinus Torvalds result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
3431da177e4SLinus Torvalds if (result)
34496fc8e8dSGreg Kroah-Hartman dev_err(dev, "%s - failed resubmitting read urb, error %d\n",
34596fc8e8dSGreg Kroah-Hartman __func__, result);
34696fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - usb_submit_urb(read urb)\n", __func__);
3471da177e4SLinus Torvalds }
3481da177e4SLinus Torvalds }
3491da177e4SLinus Torvalds
cyberjack_write_bulk_callback(struct urb * urb)3507d12e780SDavid Howells static void cyberjack_write_bulk_callback(struct urb *urb)
3511da177e4SLinus Torvalds {
352cdc97792SMing Lei struct usb_serial_port *port = urb->context;
3531da177e4SLinus Torvalds struct cyberjack_private *priv = usb_get_serial_port_data(port);
35496fc8e8dSGreg Kroah-Hartman struct device *dev = &port->dev;
3557dcc85cdSGreg Kroah-Hartman int status = urb->status;
356c75d18ccSJohn Ogness unsigned long flags;
357985616f0SJohan Hovold bool resubmitted = false;
3581da177e4SLinus Torvalds
3597dcc85cdSGreg Kroah-Hartman if (status) {
36096fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - nonzero write bulk status received: %d\n",
361441b62c1SHarvey Harrison __func__, status);
362985616f0SJohan Hovold set_bit(0, &port->write_urbs_free);
3631da177e4SLinus Torvalds return;
3641da177e4SLinus Torvalds }
3651da177e4SLinus Torvalds
366c75d18ccSJohn Ogness spin_lock_irqsave(&priv->lock, flags);
3671da177e4SLinus Torvalds
3681da177e4SLinus Torvalds /* only do something if we have more data to send */
3691da177e4SLinus Torvalds if (priv->wrfilled) {
3701da177e4SLinus Torvalds int length, blksize, result;
3711da177e4SLinus Torvalds
37296fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - transmitting data (frame n)\n", __func__);
3731da177e4SLinus Torvalds
3741da177e4SLinus Torvalds length = ((priv->wrfilled - priv->wrsent) > port->bulk_out_size) ?
3751da177e4SLinus Torvalds port->bulk_out_size : (priv->wrfilled - priv->wrsent);
3761da177e4SLinus Torvalds
377b9c52f15SAlan Cox memcpy(port->write_urb->transfer_buffer,
378b9c52f15SAlan Cox priv->wrbuf + priv->wrsent, length);
3791da177e4SLinus Torvalds priv->wrsent += length;
3801da177e4SLinus Torvalds
3811da177e4SLinus Torvalds /* set up our urb */
382fd11961aSJohan Hovold port->write_urb->transfer_buffer_length = length;
3831da177e4SLinus Torvalds
3841da177e4SLinus Torvalds /* send the data out the bulk port */
3851da177e4SLinus Torvalds result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
3861da177e4SLinus Torvalds if (result) {
38796fc8e8dSGreg Kroah-Hartman dev_err(dev, "%s - failed submitting write urb, error %d\n",
388b9c52f15SAlan Cox __func__, result);
3891da177e4SLinus Torvalds /* Throw away data. No better idea what to do with it. */
3901da177e4SLinus Torvalds priv->wrfilled = 0;
3911da177e4SLinus Torvalds priv->wrsent = 0;
3921da177e4SLinus Torvalds goto exit;
3931da177e4SLinus Torvalds }
3941da177e4SLinus Torvalds
395985616f0SJohan Hovold resubmitted = true;
396985616f0SJohan Hovold
39796fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - priv->wrsent=%d\n", __func__, priv->wrsent);
39896fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - priv->wrfilled=%d\n", __func__, priv->wrfilled);
3991da177e4SLinus Torvalds
4001da177e4SLinus Torvalds blksize = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
4011da177e4SLinus Torvalds
402b9c52f15SAlan Cox if (priv->wrsent >= priv->wrfilled ||
403b9c52f15SAlan Cox priv->wrsent >= blksize) {
40496fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - buffer cleaned\n", __func__);
4051da177e4SLinus Torvalds memset(priv->wrbuf, 0, sizeof(priv->wrbuf));
4061da177e4SLinus Torvalds priv->wrfilled = 0;
4071da177e4SLinus Torvalds priv->wrsent = 0;
4081da177e4SLinus Torvalds }
4091da177e4SLinus Torvalds }
4101da177e4SLinus Torvalds
4111da177e4SLinus Torvalds exit:
412c75d18ccSJohn Ogness spin_unlock_irqrestore(&priv->lock, flags);
413985616f0SJohan Hovold if (!resubmitted)
414985616f0SJohan Hovold set_bit(0, &port->write_urbs_free);
415cf2c7481SPete Zaitcev usb_serial_port_softint(port);
4161da177e4SLinus Torvalds }
4171da177e4SLinus Torvalds
41868e24113SGreg Kroah-Hartman module_usb_serial_driver(serial_drivers, id_table);
4191da177e4SLinus Torvalds
4201da177e4SLinus Torvalds MODULE_AUTHOR(DRIVER_AUTHOR);
4211da177e4SLinus Torvalds MODULE_DESCRIPTION(DRIVER_DESC);
4221da177e4SLinus Torvalds MODULE_LICENSE("GPL");
423