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/slab.h> 341da177e4SLinus Torvalds #include <linux/tty.h> 351da177e4SLinus Torvalds #include <linux/tty_driver.h> 361da177e4SLinus Torvalds #include <linux/tty_flip.h> 371da177e4SLinus Torvalds #include <linux/module.h> 381da177e4SLinus Torvalds #include <linux/spinlock.h> 39b9c52f15SAlan Cox #include <linux/uaccess.h> 401da177e4SLinus Torvalds #include <linux/usb.h> 41a969888cSGreg Kroah-Hartman #include <linux/usb/serial.h> 421da177e4SLinus Torvalds 431da177e4SLinus Torvalds #define CYBERJACK_LOCAL_BUF_SIZE 32 441da177e4SLinus Torvalds 451da177e4SLinus Torvalds #define DRIVER_AUTHOR "Matthias Bruestle" 461da177e4SLinus Torvalds #define DRIVER_DESC "REINER SCT cyberJack pinpad/e-com USB Chipcard Reader Driver" 471da177e4SLinus Torvalds 481da177e4SLinus Torvalds 491da177e4SLinus Torvalds #define CYBERJACK_VENDOR_ID 0x0C4B 501da177e4SLinus Torvalds #define CYBERJACK_PRODUCT_ID 0x0100 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds /* Function prototypes */ 53a9556040SJohan Hovold static int cyberjack_port_probe(struct usb_serial_port *port); 54a9556040SJohan Hovold static int cyberjack_port_remove(struct usb_serial_port *port); 5595da310eSAlan Cox static int cyberjack_open(struct tty_struct *tty, 56a509a7e4SAlan Cox struct usb_serial_port *port); 57335f8514SAlan Cox static void cyberjack_close(struct usb_serial_port *port); 5895da310eSAlan Cox static int cyberjack_write(struct tty_struct *tty, 5995da310eSAlan Cox struct usb_serial_port *port, const unsigned char *buf, int count); 6095da310eSAlan Cox static int cyberjack_write_room(struct tty_struct *tty); 617d12e780SDavid Howells static void cyberjack_read_int_callback(struct urb *urb); 627d12e780SDavid Howells static void cyberjack_read_bulk_callback(struct urb *urb); 637d12e780SDavid Howells static void cyberjack_write_bulk_callback(struct urb *urb); 641da177e4SLinus Torvalds 657d40d7e8SNémeth Márton static const struct usb_device_id id_table[] = { 661da177e4SLinus Torvalds { USB_DEVICE(CYBERJACK_VENDOR_ID, CYBERJACK_PRODUCT_ID) }, 671da177e4SLinus Torvalds { } /* Terminating entry */ 681da177e4SLinus Torvalds }; 691da177e4SLinus Torvalds 701da177e4SLinus Torvalds MODULE_DEVICE_TABLE(usb, id_table); 711da177e4SLinus Torvalds 72ea65370dSGreg Kroah-Hartman static struct usb_serial_driver cyberjack_device = { 7318fcac35SGreg Kroah-Hartman .driver = { 741da177e4SLinus Torvalds .owner = THIS_MODULE, 75269bda1cSGreg Kroah-Hartman .name = "cyberjack", 7618fcac35SGreg Kroah-Hartman }, 77269bda1cSGreg Kroah-Hartman .description = "Reiner SCT Cyberjack USB card reader", 781da177e4SLinus Torvalds .id_table = id_table, 791da177e4SLinus Torvalds .num_ports = 1, 80a9556040SJohan Hovold .port_probe = cyberjack_port_probe, 81a9556040SJohan Hovold .port_remove = cyberjack_port_remove, 821da177e4SLinus Torvalds .open = cyberjack_open, 831da177e4SLinus Torvalds .close = cyberjack_close, 841da177e4SLinus Torvalds .write = cyberjack_write, 851da177e4SLinus Torvalds .write_room = cyberjack_write_room, 861da177e4SLinus Torvalds .read_int_callback = cyberjack_read_int_callback, 871da177e4SLinus Torvalds .read_bulk_callback = cyberjack_read_bulk_callback, 881da177e4SLinus Torvalds .write_bulk_callback = cyberjack_write_bulk_callback, 891da177e4SLinus Torvalds }; 901da177e4SLinus Torvalds 9108a4f6bcSAlan Stern static struct usb_serial_driver * const serial_drivers[] = { 9208a4f6bcSAlan Stern &cyberjack_device, NULL 9308a4f6bcSAlan Stern }; 9408a4f6bcSAlan Stern 951da177e4SLinus Torvalds struct cyberjack_private { 961da177e4SLinus Torvalds spinlock_t lock; /* Lock for SMP */ 971da177e4SLinus Torvalds short rdtodo; /* Bytes still to read */ 981da177e4SLinus Torvalds unsigned char wrbuf[5*64]; /* Buffer for collecting data to write */ 991da177e4SLinus Torvalds short wrfilled; /* Overall data size we already got */ 1001da177e4SLinus Torvalds short wrsent; /* Data already sent */ 1011da177e4SLinus Torvalds }; 1021da177e4SLinus Torvalds 103a9556040SJohan Hovold static int cyberjack_port_probe(struct usb_serial_port *port) 1041da177e4SLinus Torvalds { 1051da177e4SLinus Torvalds struct cyberjack_private *priv; 106a9556040SJohan Hovold int result; 1071da177e4SLinus Torvalds 1081da177e4SLinus Torvalds priv = kmalloc(sizeof(struct cyberjack_private), GFP_KERNEL); 1091da177e4SLinus Torvalds if (!priv) 1101da177e4SLinus Torvalds return -ENOMEM; 1111da177e4SLinus Torvalds 1121da177e4SLinus Torvalds spin_lock_init(&priv->lock); 1131da177e4SLinus Torvalds priv->rdtodo = 0; 1141da177e4SLinus Torvalds priv->wrfilled = 0; 1151da177e4SLinus Torvalds priv->wrsent = 0; 1161da177e4SLinus Torvalds 117a9556040SJohan Hovold usb_set_serial_port_data(port, priv); 1181da177e4SLinus Torvalds 119a9556040SJohan Hovold result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL); 1201da177e4SLinus Torvalds if (result) 121a9556040SJohan Hovold dev_err(&port->dev, "usb_submit_urb(read int) failed\n"); 122a9556040SJohan Hovold 123a9556040SJohan Hovold return 0; 1241da177e4SLinus Torvalds } 1251da177e4SLinus Torvalds 126a9556040SJohan Hovold static int cyberjack_port_remove(struct usb_serial_port *port) 127a9556040SJohan Hovold { 128a9556040SJohan Hovold struct cyberjack_private *priv; 129a9556040SJohan Hovold 13030d9d42eSJohan Hovold usb_kill_urb(port->interrupt_in_urb); 13130d9d42eSJohan Hovold 132a9556040SJohan Hovold priv = usb_get_serial_port_data(port); 133a9556040SJohan Hovold kfree(priv); 134a9556040SJohan Hovold 135b9c52f15SAlan Cox return 0; 1361da177e4SLinus Torvalds } 1371da177e4SLinus Torvalds 13895da310eSAlan Cox static int cyberjack_open(struct tty_struct *tty, 139a509a7e4SAlan Cox struct usb_serial_port *port) 1401da177e4SLinus Torvalds { 1411da177e4SLinus Torvalds struct cyberjack_private *priv; 1421da177e4SLinus Torvalds unsigned long flags; 1431da177e4SLinus Torvalds int result = 0; 1441da177e4SLinus Torvalds 14596fc8e8dSGreg Kroah-Hartman dev_dbg(&port->dev, "%s - usb_clear_halt\n", __func__); 1461da177e4SLinus Torvalds usb_clear_halt(port->serial->dev, port->write_urb->pipe); 1471da177e4SLinus Torvalds 1481da177e4SLinus Torvalds priv = usb_get_serial_port_data(port); 1491da177e4SLinus Torvalds spin_lock_irqsave(&priv->lock, flags); 1501da177e4SLinus Torvalds priv->rdtodo = 0; 1511da177e4SLinus Torvalds priv->wrfilled = 0; 1521da177e4SLinus Torvalds priv->wrsent = 0; 1531da177e4SLinus Torvalds spin_unlock_irqrestore(&priv->lock, flags); 1541da177e4SLinus Torvalds 1551da177e4SLinus Torvalds return result; 1561da177e4SLinus Torvalds } 1571da177e4SLinus Torvalds 158335f8514SAlan Cox static void cyberjack_close(struct usb_serial_port *port) 1591da177e4SLinus Torvalds { 1601da177e4SLinus Torvalds usb_kill_urb(port->write_urb); 1611da177e4SLinus Torvalds usb_kill_urb(port->read_urb); 1621da177e4SLinus Torvalds } 1631da177e4SLinus Torvalds 16495da310eSAlan Cox static int cyberjack_write(struct tty_struct *tty, 16595da310eSAlan Cox struct usb_serial_port *port, const unsigned char *buf, int count) 1661da177e4SLinus Torvalds { 16796fc8e8dSGreg Kroah-Hartman struct device *dev = &port->dev; 1681da177e4SLinus Torvalds struct cyberjack_private *priv = usb_get_serial_port_data(port); 1691da177e4SLinus Torvalds unsigned long flags; 1701da177e4SLinus Torvalds int result; 1711da177e4SLinus Torvalds int wrexpected; 1721da177e4SLinus Torvalds 1731da177e4SLinus Torvalds if (count == 0) { 17496fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - write request of 0 bytes\n", __func__); 175a5b6f60cSAlan Cox return 0; 1761da177e4SLinus Torvalds } 1771da177e4SLinus Torvalds 178c1cac10cSJohan Hovold if (!test_and_clear_bit(0, &port->write_urbs_free)) { 17996fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - already writing\n", __func__); 180507ca9bcSGreg Kroah-Hartman return 0; 1811da177e4SLinus Torvalds } 1821da177e4SLinus Torvalds 1831da177e4SLinus Torvalds spin_lock_irqsave(&priv->lock, flags); 1841da177e4SLinus Torvalds 185b9c52f15SAlan Cox if (count+priv->wrfilled > sizeof(priv->wrbuf)) { 1861da177e4SLinus Torvalds /* To much data for buffer. Reset buffer. */ 1871da177e4SLinus Torvalds priv->wrfilled = 0; 188a5b6f60cSAlan Cox spin_unlock_irqrestore(&priv->lock, flags); 189c1cac10cSJohan Hovold set_bit(0, &port->write_urbs_free); 190a5b6f60cSAlan Cox return 0; 1911da177e4SLinus Torvalds } 1921da177e4SLinus Torvalds 1931da177e4SLinus Torvalds /* Copy data */ 1941da177e4SLinus Torvalds memcpy(priv->wrbuf + priv->wrfilled, buf, count); 1951da177e4SLinus Torvalds 19659d33f2fSGreg Kroah-Hartman usb_serial_debug_data(dev, __func__, count, priv->wrbuf + priv->wrfilled); 1971da177e4SLinus Torvalds priv->wrfilled += count; 1981da177e4SLinus Torvalds 1991da177e4SLinus Torvalds if (priv->wrfilled >= 3) { 2001da177e4SLinus Torvalds wrexpected = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3; 20196fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - expected data: %d\n", __func__, wrexpected); 202b9c52f15SAlan Cox } else 2031da177e4SLinus Torvalds wrexpected = sizeof(priv->wrbuf); 2041da177e4SLinus Torvalds 2051da177e4SLinus Torvalds if (priv->wrfilled >= wrexpected) { 2061da177e4SLinus Torvalds /* We have enough data to begin transmission */ 2071da177e4SLinus Torvalds int length; 2081da177e4SLinus Torvalds 20996fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - transmitting data (frame 1)\n", __func__); 210b9c52f15SAlan Cox length = (wrexpected > port->bulk_out_size) ? 211b9c52f15SAlan Cox port->bulk_out_size : wrexpected; 2121da177e4SLinus Torvalds 2131da177e4SLinus Torvalds memcpy(port->write_urb->transfer_buffer, priv->wrbuf, length); 2141da177e4SLinus Torvalds priv->wrsent = length; 2151da177e4SLinus Torvalds 2161da177e4SLinus Torvalds /* set up our urb */ 217fd11961aSJohan Hovold port->write_urb->transfer_buffer_length = length; 2181da177e4SLinus Torvalds 2191da177e4SLinus Torvalds /* send the data out the bulk port */ 2201da177e4SLinus Torvalds result = usb_submit_urb(port->write_urb, GFP_ATOMIC); 2211da177e4SLinus Torvalds if (result) { 222194343d9SGreg Kroah-Hartman dev_err(&port->dev, 223*d9a38a87SJohan Hovold "%s - failed submitting write urb, error %d\n", 224b9c52f15SAlan Cox __func__, result); 2251da177e4SLinus Torvalds /* Throw away data. No better idea what to do with it. */ 2261da177e4SLinus Torvalds priv->wrfilled = 0; 2271da177e4SLinus Torvalds priv->wrsent = 0; 2281da177e4SLinus Torvalds spin_unlock_irqrestore(&priv->lock, flags); 229c1cac10cSJohan Hovold set_bit(0, &port->write_urbs_free); 2301da177e4SLinus Torvalds return 0; 2311da177e4SLinus Torvalds } 2321da177e4SLinus Torvalds 23396fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - priv->wrsent=%d\n", __func__, priv->wrsent); 23496fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - priv->wrfilled=%d\n", __func__, priv->wrfilled); 2351da177e4SLinus Torvalds 2361da177e4SLinus Torvalds if (priv->wrsent >= priv->wrfilled) { 23796fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - buffer cleaned\n", __func__); 2381da177e4SLinus Torvalds memset(priv->wrbuf, 0, sizeof(priv->wrbuf)); 2391da177e4SLinus Torvalds priv->wrfilled = 0; 2401da177e4SLinus Torvalds priv->wrsent = 0; 2411da177e4SLinus Torvalds } 2421da177e4SLinus Torvalds } 2431da177e4SLinus Torvalds 2441da177e4SLinus Torvalds spin_unlock_irqrestore(&priv->lock, flags); 2451da177e4SLinus Torvalds 246b9c52f15SAlan Cox return count; 2471da177e4SLinus Torvalds } 2481da177e4SLinus Torvalds 24995da310eSAlan Cox static int cyberjack_write_room(struct tty_struct *tty) 2501da177e4SLinus Torvalds { 251a5b6f60cSAlan Cox /* FIXME: .... */ 2521da177e4SLinus Torvalds return CYBERJACK_LOCAL_BUF_SIZE; 2531da177e4SLinus Torvalds } 2541da177e4SLinus Torvalds 2557d12e780SDavid Howells static void cyberjack_read_int_callback(struct urb *urb) 2561da177e4SLinus Torvalds { 257cdc97792SMing Lei struct usb_serial_port *port = urb->context; 2581da177e4SLinus Torvalds struct cyberjack_private *priv = usb_get_serial_port_data(port); 25996fc8e8dSGreg Kroah-Hartman struct device *dev = &port->dev; 2601da177e4SLinus Torvalds unsigned char *data = urb->transfer_buffer; 2617dcc85cdSGreg Kroah-Hartman int status = urb->status; 2621da177e4SLinus Torvalds int result; 2631da177e4SLinus Torvalds 2641da177e4SLinus Torvalds /* the urb might have been killed. */ 2657dcc85cdSGreg Kroah-Hartman if (status) 2661da177e4SLinus Torvalds return; 2671da177e4SLinus Torvalds 26859d33f2fSGreg Kroah-Hartman usb_serial_debug_data(dev, __func__, urb->actual_length, data); 2691da177e4SLinus Torvalds 2701da177e4SLinus Torvalds /* React only to interrupts signaling a bulk_in transfer */ 271b9c52f15SAlan Cox if (urb->actual_length == 4 && data[0] == 0x01) { 2721da177e4SLinus Torvalds short old_rdtodo; 2731da177e4SLinus Torvalds 2741da177e4SLinus Torvalds /* This is a announcement of coming bulk_ins. */ 2751da177e4SLinus Torvalds unsigned short size = ((unsigned short)data[3]<<8)+data[2]+3; 2761da177e4SLinus Torvalds 2771da177e4SLinus Torvalds spin_lock(&priv->lock); 2781da177e4SLinus Torvalds 2791da177e4SLinus Torvalds old_rdtodo = priv->rdtodo; 2801da177e4SLinus Torvalds 2815287bf72SDan Carpenter if (old_rdtodo > SHRT_MAX - size) { 28296fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "To many bulk_in urbs to do.\n"); 2831da177e4SLinus Torvalds spin_unlock(&priv->lock); 2841da177e4SLinus Torvalds goto resubmit; 2851da177e4SLinus Torvalds } 2861da177e4SLinus Torvalds 287cd8c5053SRahul Bedarkar /* "+=" is probably more fault tolerant than "=" */ 2881da177e4SLinus Torvalds priv->rdtodo += size; 2891da177e4SLinus Torvalds 29096fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - rdtodo: %d\n", __func__, priv->rdtodo); 2911da177e4SLinus Torvalds 2921da177e4SLinus Torvalds spin_unlock(&priv->lock); 2931da177e4SLinus Torvalds 2941da177e4SLinus Torvalds if (!old_rdtodo) { 2951da177e4SLinus Torvalds result = usb_submit_urb(port->read_urb, GFP_ATOMIC); 2961da177e4SLinus Torvalds if (result) 29796fc8e8dSGreg Kroah-Hartman dev_err(dev, "%s - failed resubmitting read urb, error %d\n", 298194343d9SGreg Kroah-Hartman __func__, result); 29996fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - usb_submit_urb(read urb)\n", __func__); 3001da177e4SLinus Torvalds } 3011da177e4SLinus Torvalds } 3021da177e4SLinus Torvalds 3031da177e4SLinus Torvalds resubmit: 3041da177e4SLinus Torvalds result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC); 3051da177e4SLinus Torvalds if (result) 306194343d9SGreg Kroah-Hartman dev_err(&port->dev, "usb_submit_urb(read int) failed\n"); 30796fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - usb_submit_urb(int urb)\n", __func__); 3081da177e4SLinus Torvalds } 3091da177e4SLinus Torvalds 3107d12e780SDavid Howells static void cyberjack_read_bulk_callback(struct urb *urb) 3111da177e4SLinus Torvalds { 312cdc97792SMing Lei struct usb_serial_port *port = urb->context; 3131da177e4SLinus Torvalds struct cyberjack_private *priv = usb_get_serial_port_data(port); 31496fc8e8dSGreg Kroah-Hartman struct device *dev = &port->dev; 3151da177e4SLinus Torvalds unsigned char *data = urb->transfer_buffer; 3161da177e4SLinus Torvalds short todo; 3171da177e4SLinus Torvalds int result; 3187dcc85cdSGreg Kroah-Hartman int status = urb->status; 3191da177e4SLinus Torvalds 32059d33f2fSGreg Kroah-Hartman usb_serial_debug_data(dev, __func__, urb->actual_length, data); 3217dcc85cdSGreg Kroah-Hartman if (status) { 32296fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - nonzero read bulk status received: %d\n", 323441b62c1SHarvey Harrison __func__, status); 3241da177e4SLinus Torvalds return; 3251da177e4SLinus Torvalds } 3261da177e4SLinus Torvalds 3271da177e4SLinus Torvalds if (urb->actual_length) { 32805c7cd39SJiri Slaby tty_insert_flip_string(&port->port, data, urb->actual_length); 3292e124b4aSJiri Slaby tty_flip_buffer_push(&port->port); 3301da177e4SLinus Torvalds } 3311da177e4SLinus Torvalds 3321da177e4SLinus Torvalds spin_lock(&priv->lock); 3331da177e4SLinus Torvalds 3341da177e4SLinus Torvalds /* Reduce urbs to do by one. */ 3351da177e4SLinus Torvalds priv->rdtodo -= urb->actual_length; 3361da177e4SLinus Torvalds /* Just to be sure */ 337b9c52f15SAlan Cox if (priv->rdtodo < 0) 338b9c52f15SAlan Cox priv->rdtodo = 0; 3391da177e4SLinus Torvalds todo = priv->rdtodo; 3401da177e4SLinus Torvalds 3411da177e4SLinus Torvalds spin_unlock(&priv->lock); 3421da177e4SLinus Torvalds 34396fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - rdtodo: %d\n", __func__, todo); 3441da177e4SLinus Torvalds 3451da177e4SLinus Torvalds /* Continue to read if we have still urbs to do. */ 3461da177e4SLinus Torvalds if (todo /* || (urb->actual_length==port->bulk_in_endpointAddress)*/) { 3471da177e4SLinus Torvalds result = usb_submit_urb(port->read_urb, GFP_ATOMIC); 3481da177e4SLinus Torvalds if (result) 34996fc8e8dSGreg Kroah-Hartman dev_err(dev, "%s - failed resubmitting read urb, error %d\n", 35096fc8e8dSGreg Kroah-Hartman __func__, result); 35196fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - usb_submit_urb(read urb)\n", __func__); 3521da177e4SLinus Torvalds } 3531da177e4SLinus Torvalds } 3541da177e4SLinus Torvalds 3557d12e780SDavid Howells static void cyberjack_write_bulk_callback(struct urb *urb) 3561da177e4SLinus Torvalds { 357cdc97792SMing Lei struct usb_serial_port *port = urb->context; 3581da177e4SLinus Torvalds struct cyberjack_private *priv = usb_get_serial_port_data(port); 35996fc8e8dSGreg Kroah-Hartman struct device *dev = &port->dev; 3607dcc85cdSGreg Kroah-Hartman int status = urb->status; 3611da177e4SLinus Torvalds 362c1cac10cSJohan Hovold set_bit(0, &port->write_urbs_free); 3637dcc85cdSGreg Kroah-Hartman if (status) { 36496fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - nonzero write bulk status received: %d\n", 365441b62c1SHarvey Harrison __func__, status); 3661da177e4SLinus Torvalds return; 3671da177e4SLinus Torvalds } 3681da177e4SLinus Torvalds 3691da177e4SLinus Torvalds spin_lock(&priv->lock); 3701da177e4SLinus Torvalds 3711da177e4SLinus Torvalds /* only do something if we have more data to send */ 3721da177e4SLinus Torvalds if (priv->wrfilled) { 3731da177e4SLinus Torvalds int length, blksize, result; 3741da177e4SLinus Torvalds 37596fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - transmitting data (frame n)\n", __func__); 3761da177e4SLinus Torvalds 3771da177e4SLinus Torvalds length = ((priv->wrfilled - priv->wrsent) > port->bulk_out_size) ? 3781da177e4SLinus Torvalds port->bulk_out_size : (priv->wrfilled - priv->wrsent); 3791da177e4SLinus Torvalds 380b9c52f15SAlan Cox memcpy(port->write_urb->transfer_buffer, 381b9c52f15SAlan Cox priv->wrbuf + priv->wrsent, length); 3821da177e4SLinus Torvalds priv->wrsent += length; 3831da177e4SLinus Torvalds 3841da177e4SLinus Torvalds /* set up our urb */ 385fd11961aSJohan Hovold port->write_urb->transfer_buffer_length = length; 3861da177e4SLinus Torvalds 3871da177e4SLinus Torvalds /* send the data out the bulk port */ 3881da177e4SLinus Torvalds result = usb_submit_urb(port->write_urb, GFP_ATOMIC); 3891da177e4SLinus Torvalds if (result) { 39096fc8e8dSGreg Kroah-Hartman dev_err(dev, "%s - failed submitting write urb, error %d\n", 391b9c52f15SAlan Cox __func__, result); 3921da177e4SLinus Torvalds /* Throw away data. No better idea what to do with it. */ 3931da177e4SLinus Torvalds priv->wrfilled = 0; 3941da177e4SLinus Torvalds priv->wrsent = 0; 3951da177e4SLinus Torvalds goto exit; 3961da177e4SLinus Torvalds } 3971da177e4SLinus Torvalds 39896fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - priv->wrsent=%d\n", __func__, priv->wrsent); 39996fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - priv->wrfilled=%d\n", __func__, priv->wrfilled); 4001da177e4SLinus Torvalds 4011da177e4SLinus Torvalds blksize = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3; 4021da177e4SLinus Torvalds 403b9c52f15SAlan Cox if (priv->wrsent >= priv->wrfilled || 404b9c52f15SAlan Cox priv->wrsent >= blksize) { 40596fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - buffer cleaned\n", __func__); 4061da177e4SLinus Torvalds memset(priv->wrbuf, 0, sizeof(priv->wrbuf)); 4071da177e4SLinus Torvalds priv->wrfilled = 0; 4081da177e4SLinus Torvalds priv->wrsent = 0; 4091da177e4SLinus Torvalds } 4101da177e4SLinus Torvalds } 4111da177e4SLinus Torvalds 4121da177e4SLinus Torvalds exit: 4131da177e4SLinus Torvalds spin_unlock(&priv->lock); 414cf2c7481SPete Zaitcev usb_serial_port_softint(port); 4151da177e4SLinus Torvalds } 4161da177e4SLinus Torvalds 41768e24113SGreg Kroah-Hartman module_usb_serial_driver(serial_drivers, id_table); 4181da177e4SLinus Torvalds 4191da177e4SLinus Torvalds MODULE_AUTHOR(DRIVER_AUTHOR); 4201da177e4SLinus Torvalds MODULE_DESCRIPTION(DRIVER_DESC); 4211da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 422