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/init.h> 341da177e4SLinus Torvalds #include <linux/slab.h> 351da177e4SLinus Torvalds #include <linux/tty.h> 361da177e4SLinus Torvalds #include <linux/tty_driver.h> 371da177e4SLinus Torvalds #include <linux/tty_flip.h> 381da177e4SLinus Torvalds #include <linux/module.h> 391da177e4SLinus Torvalds #include <linux/spinlock.h> 40b9c52f15SAlan Cox #include <linux/uaccess.h> 411da177e4SLinus Torvalds #include <linux/usb.h> 42a969888cSGreg Kroah-Hartman #include <linux/usb/serial.h> 431da177e4SLinus Torvalds 441da177e4SLinus Torvalds #define CYBERJACK_LOCAL_BUF_SIZE 32 451da177e4SLinus Torvalds 461da177e4SLinus Torvalds #define DRIVER_AUTHOR "Matthias Bruestle" 471da177e4SLinus Torvalds #define DRIVER_DESC "REINER SCT cyberJack pinpad/e-com USB Chipcard Reader Driver" 481da177e4SLinus Torvalds 491da177e4SLinus Torvalds 501da177e4SLinus Torvalds #define CYBERJACK_VENDOR_ID 0x0C4B 511da177e4SLinus Torvalds #define CYBERJACK_PRODUCT_ID 0x0100 521da177e4SLinus Torvalds 531da177e4SLinus Torvalds /* Function prototypes */ 54a9556040SJohan Hovold static int cyberjack_port_probe(struct usb_serial_port *port); 55a9556040SJohan Hovold static int cyberjack_port_remove(struct usb_serial_port *port); 5695da310eSAlan Cox static int cyberjack_open(struct tty_struct *tty, 57a509a7e4SAlan Cox struct usb_serial_port *port); 58335f8514SAlan Cox static void cyberjack_close(struct usb_serial_port *port); 5995da310eSAlan Cox static int cyberjack_write(struct tty_struct *tty, 6095da310eSAlan Cox struct usb_serial_port *port, const unsigned char *buf, int count); 6195da310eSAlan Cox static int cyberjack_write_room(struct tty_struct *tty); 627d12e780SDavid Howells static void cyberjack_read_int_callback(struct urb *urb); 637d12e780SDavid Howells static void cyberjack_read_bulk_callback(struct urb *urb); 647d12e780SDavid Howells static void cyberjack_write_bulk_callback(struct urb *urb); 651da177e4SLinus Torvalds 667d40d7e8SNémeth Márton static const struct usb_device_id id_table[] = { 671da177e4SLinus Torvalds { USB_DEVICE(CYBERJACK_VENDOR_ID, CYBERJACK_PRODUCT_ID) }, 681da177e4SLinus Torvalds { } /* Terminating entry */ 691da177e4SLinus Torvalds }; 701da177e4SLinus Torvalds 711da177e4SLinus Torvalds MODULE_DEVICE_TABLE(usb, id_table); 721da177e4SLinus Torvalds 73ea65370dSGreg Kroah-Hartman static struct usb_serial_driver cyberjack_device = { 7418fcac35SGreg Kroah-Hartman .driver = { 751da177e4SLinus Torvalds .owner = THIS_MODULE, 76269bda1cSGreg Kroah-Hartman .name = "cyberjack", 7718fcac35SGreg Kroah-Hartman }, 78269bda1cSGreg Kroah-Hartman .description = "Reiner SCT Cyberjack USB card reader", 791da177e4SLinus Torvalds .id_table = id_table, 801da177e4SLinus Torvalds .num_ports = 1, 81a9556040SJohan Hovold .port_probe = cyberjack_port_probe, 82a9556040SJohan Hovold .port_remove = cyberjack_port_remove, 831da177e4SLinus Torvalds .open = cyberjack_open, 841da177e4SLinus Torvalds .close = cyberjack_close, 851da177e4SLinus Torvalds .write = cyberjack_write, 861da177e4SLinus Torvalds .write_room = cyberjack_write_room, 871da177e4SLinus Torvalds .read_int_callback = cyberjack_read_int_callback, 881da177e4SLinus Torvalds .read_bulk_callback = cyberjack_read_bulk_callback, 891da177e4SLinus Torvalds .write_bulk_callback = cyberjack_write_bulk_callback, 901da177e4SLinus Torvalds }; 911da177e4SLinus Torvalds 9208a4f6bcSAlan Stern static struct usb_serial_driver * const serial_drivers[] = { 9308a4f6bcSAlan Stern &cyberjack_device, NULL 9408a4f6bcSAlan Stern }; 9508a4f6bcSAlan Stern 961da177e4SLinus Torvalds struct cyberjack_private { 971da177e4SLinus Torvalds spinlock_t lock; /* Lock for SMP */ 981da177e4SLinus Torvalds short rdtodo; /* Bytes still to read */ 991da177e4SLinus Torvalds unsigned char wrbuf[5*64]; /* Buffer for collecting data to write */ 1001da177e4SLinus Torvalds short wrfilled; /* Overall data size we already got */ 1011da177e4SLinus Torvalds short wrsent; /* Data already sent */ 1021da177e4SLinus Torvalds }; 1031da177e4SLinus Torvalds 104a9556040SJohan Hovold static int cyberjack_port_probe(struct usb_serial_port *port) 1051da177e4SLinus Torvalds { 1061da177e4SLinus Torvalds struct cyberjack_private *priv; 107a9556040SJohan Hovold int result; 1081da177e4SLinus Torvalds 1091da177e4SLinus Torvalds priv = kmalloc(sizeof(struct cyberjack_private), GFP_KERNEL); 1101da177e4SLinus Torvalds if (!priv) 1111da177e4SLinus Torvalds return -ENOMEM; 1121da177e4SLinus Torvalds 1131da177e4SLinus Torvalds spin_lock_init(&priv->lock); 1141da177e4SLinus Torvalds priv->rdtodo = 0; 1151da177e4SLinus Torvalds priv->wrfilled = 0; 1161da177e4SLinus Torvalds priv->wrsent = 0; 1171da177e4SLinus Torvalds 118a9556040SJohan Hovold usb_set_serial_port_data(port, priv); 1191da177e4SLinus Torvalds 120a9556040SJohan Hovold result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL); 1211da177e4SLinus Torvalds if (result) 122a9556040SJohan Hovold dev_err(&port->dev, "usb_submit_urb(read int) failed\n"); 123a9556040SJohan Hovold 124a9556040SJohan Hovold return 0; 1251da177e4SLinus Torvalds } 1261da177e4SLinus Torvalds 127a9556040SJohan Hovold static int cyberjack_port_remove(struct usb_serial_port *port) 128a9556040SJohan Hovold { 129a9556040SJohan Hovold struct cyberjack_private *priv; 130a9556040SJohan Hovold 13130d9d42eSJohan Hovold usb_kill_urb(port->interrupt_in_urb); 13230d9d42eSJohan Hovold 133a9556040SJohan Hovold priv = usb_get_serial_port_data(port); 134a9556040SJohan Hovold kfree(priv); 135a9556040SJohan Hovold 136b9c52f15SAlan Cox return 0; 1371da177e4SLinus Torvalds } 1381da177e4SLinus Torvalds 13995da310eSAlan Cox static int cyberjack_open(struct tty_struct *tty, 140a509a7e4SAlan Cox struct usb_serial_port *port) 1411da177e4SLinus Torvalds { 1421da177e4SLinus Torvalds struct cyberjack_private *priv; 1431da177e4SLinus Torvalds unsigned long flags; 1441da177e4SLinus Torvalds int result = 0; 1451da177e4SLinus Torvalds 14696fc8e8dSGreg Kroah-Hartman dev_dbg(&port->dev, "%s - usb_clear_halt\n", __func__); 1471da177e4SLinus Torvalds usb_clear_halt(port->serial->dev, port->write_urb->pipe); 1481da177e4SLinus Torvalds 1491da177e4SLinus Torvalds priv = usb_get_serial_port_data(port); 1501da177e4SLinus Torvalds spin_lock_irqsave(&priv->lock, flags); 1511da177e4SLinus Torvalds priv->rdtodo = 0; 1521da177e4SLinus Torvalds priv->wrfilled = 0; 1531da177e4SLinus Torvalds priv->wrsent = 0; 1541da177e4SLinus Torvalds spin_unlock_irqrestore(&priv->lock, flags); 1551da177e4SLinus Torvalds 1561da177e4SLinus Torvalds return result; 1571da177e4SLinus Torvalds } 1581da177e4SLinus Torvalds 159335f8514SAlan Cox static void cyberjack_close(struct usb_serial_port *port) 1601da177e4SLinus Torvalds { 1611da177e4SLinus Torvalds usb_kill_urb(port->write_urb); 1621da177e4SLinus Torvalds usb_kill_urb(port->read_urb); 1631da177e4SLinus Torvalds } 1641da177e4SLinus Torvalds 16595da310eSAlan Cox static int cyberjack_write(struct tty_struct *tty, 16695da310eSAlan Cox struct usb_serial_port *port, const unsigned char *buf, int count) 1671da177e4SLinus Torvalds { 16896fc8e8dSGreg Kroah-Hartman struct device *dev = &port->dev; 1691da177e4SLinus Torvalds struct cyberjack_private *priv = usb_get_serial_port_data(port); 1701da177e4SLinus Torvalds unsigned long flags; 1711da177e4SLinus Torvalds int result; 1721da177e4SLinus Torvalds int wrexpected; 1731da177e4SLinus Torvalds 1741da177e4SLinus Torvalds if (count == 0) { 17596fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - write request of 0 bytes\n", __func__); 176a5b6f60cSAlan Cox return 0; 1771da177e4SLinus Torvalds } 1781da177e4SLinus Torvalds 179c1cac10cSJohan Hovold if (!test_and_clear_bit(0, &port->write_urbs_free)) { 18096fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - already writing\n", __func__); 181507ca9bcSGreg Kroah-Hartman return 0; 1821da177e4SLinus Torvalds } 1831da177e4SLinus Torvalds 1841da177e4SLinus Torvalds spin_lock_irqsave(&priv->lock, flags); 1851da177e4SLinus Torvalds 186b9c52f15SAlan Cox if (count+priv->wrfilled > sizeof(priv->wrbuf)) { 1871da177e4SLinus Torvalds /* To much data for buffer. Reset buffer. */ 1881da177e4SLinus Torvalds priv->wrfilled = 0; 189a5b6f60cSAlan Cox spin_unlock_irqrestore(&priv->lock, flags); 190c1cac10cSJohan Hovold set_bit(0, &port->write_urbs_free); 191a5b6f60cSAlan Cox return 0; 1921da177e4SLinus Torvalds } 1931da177e4SLinus Torvalds 1941da177e4SLinus Torvalds /* Copy data */ 1951da177e4SLinus Torvalds memcpy(priv->wrbuf + priv->wrfilled, buf, count); 1961da177e4SLinus Torvalds 19759d33f2fSGreg Kroah-Hartman usb_serial_debug_data(dev, __func__, count, priv->wrbuf + priv->wrfilled); 1981da177e4SLinus Torvalds priv->wrfilled += count; 1991da177e4SLinus Torvalds 2001da177e4SLinus Torvalds if (priv->wrfilled >= 3) { 2011da177e4SLinus Torvalds wrexpected = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3; 20296fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - expected data: %d\n", __func__, wrexpected); 203b9c52f15SAlan Cox } else 2041da177e4SLinus Torvalds wrexpected = sizeof(priv->wrbuf); 2051da177e4SLinus Torvalds 2061da177e4SLinus Torvalds if (priv->wrfilled >= wrexpected) { 2071da177e4SLinus Torvalds /* We have enough data to begin transmission */ 2081da177e4SLinus Torvalds int length; 2091da177e4SLinus Torvalds 21096fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - transmitting data (frame 1)\n", __func__); 211b9c52f15SAlan Cox length = (wrexpected > port->bulk_out_size) ? 212b9c52f15SAlan Cox port->bulk_out_size : wrexpected; 2131da177e4SLinus Torvalds 2141da177e4SLinus Torvalds memcpy(port->write_urb->transfer_buffer, priv->wrbuf, length); 2151da177e4SLinus Torvalds priv->wrsent = length; 2161da177e4SLinus Torvalds 2171da177e4SLinus Torvalds /* set up our urb */ 218fd11961aSJohan Hovold port->write_urb->transfer_buffer_length = length; 2191da177e4SLinus Torvalds 2201da177e4SLinus Torvalds /* send the data out the bulk port */ 2211da177e4SLinus Torvalds result = usb_submit_urb(port->write_urb, GFP_ATOMIC); 2221da177e4SLinus Torvalds if (result) { 223194343d9SGreg Kroah-Hartman dev_err(&port->dev, 224194343d9SGreg Kroah-Hartman "%s - failed submitting write urb, error %d", 225b9c52f15SAlan Cox __func__, result); 2261da177e4SLinus Torvalds /* Throw away data. No better idea what to do with it. */ 2271da177e4SLinus Torvalds priv->wrfilled = 0; 2281da177e4SLinus Torvalds priv->wrsent = 0; 2291da177e4SLinus Torvalds spin_unlock_irqrestore(&priv->lock, flags); 230c1cac10cSJohan Hovold set_bit(0, &port->write_urbs_free); 2311da177e4SLinus Torvalds return 0; 2321da177e4SLinus Torvalds } 2331da177e4SLinus Torvalds 23496fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - priv->wrsent=%d\n", __func__, priv->wrsent); 23596fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - priv->wrfilled=%d\n", __func__, priv->wrfilled); 2361da177e4SLinus Torvalds 2371da177e4SLinus Torvalds if (priv->wrsent >= priv->wrfilled) { 23896fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - buffer cleaned\n", __func__); 2391da177e4SLinus Torvalds memset(priv->wrbuf, 0, sizeof(priv->wrbuf)); 2401da177e4SLinus Torvalds priv->wrfilled = 0; 2411da177e4SLinus Torvalds priv->wrsent = 0; 2421da177e4SLinus Torvalds } 2431da177e4SLinus Torvalds } 2441da177e4SLinus Torvalds 2451da177e4SLinus Torvalds spin_unlock_irqrestore(&priv->lock, flags); 2461da177e4SLinus Torvalds 247b9c52f15SAlan Cox return count; 2481da177e4SLinus Torvalds } 2491da177e4SLinus Torvalds 25095da310eSAlan Cox static int cyberjack_write_room(struct tty_struct *tty) 2511da177e4SLinus Torvalds { 252a5b6f60cSAlan Cox /* FIXME: .... */ 2531da177e4SLinus Torvalds return CYBERJACK_LOCAL_BUF_SIZE; 2541da177e4SLinus Torvalds } 2551da177e4SLinus Torvalds 2567d12e780SDavid Howells static void cyberjack_read_int_callback(struct urb *urb) 2571da177e4SLinus Torvalds { 258cdc97792SMing Lei struct usb_serial_port *port = urb->context; 2591da177e4SLinus Torvalds struct cyberjack_private *priv = usb_get_serial_port_data(port); 26096fc8e8dSGreg Kroah-Hartman struct device *dev = &port->dev; 2611da177e4SLinus Torvalds unsigned char *data = urb->transfer_buffer; 2627dcc85cdSGreg Kroah-Hartman int status = urb->status; 2631da177e4SLinus Torvalds int result; 2641da177e4SLinus Torvalds 2651da177e4SLinus Torvalds /* the urb might have been killed. */ 2667dcc85cdSGreg Kroah-Hartman if (status) 2671da177e4SLinus Torvalds return; 2681da177e4SLinus Torvalds 26959d33f2fSGreg Kroah-Hartman usb_serial_debug_data(dev, __func__, urb->actual_length, data); 2701da177e4SLinus Torvalds 2711da177e4SLinus Torvalds /* React only to interrupts signaling a bulk_in transfer */ 272b9c52f15SAlan Cox if (urb->actual_length == 4 && data[0] == 0x01) { 2731da177e4SLinus Torvalds short old_rdtodo; 2741da177e4SLinus Torvalds 2751da177e4SLinus Torvalds /* This is a announcement of coming bulk_ins. */ 2761da177e4SLinus Torvalds unsigned short size = ((unsigned short)data[3]<<8)+data[2]+3; 2771da177e4SLinus Torvalds 2781da177e4SLinus Torvalds spin_lock(&priv->lock); 2791da177e4SLinus Torvalds 2801da177e4SLinus Torvalds old_rdtodo = priv->rdtodo; 2811da177e4SLinus Torvalds 282*5287bf72SDan Carpenter if (old_rdtodo > SHRT_MAX - size) { 28396fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "To many bulk_in urbs to do.\n"); 2841da177e4SLinus Torvalds spin_unlock(&priv->lock); 2851da177e4SLinus Torvalds goto resubmit; 2861da177e4SLinus Torvalds } 2871da177e4SLinus Torvalds 2881da177e4SLinus Torvalds /* "+=" is probably more fault tollerant than "=" */ 2891da177e4SLinus Torvalds priv->rdtodo += size; 2901da177e4SLinus Torvalds 29196fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - rdtodo: %d\n", __func__, priv->rdtodo); 2921da177e4SLinus Torvalds 2931da177e4SLinus Torvalds spin_unlock(&priv->lock); 2941da177e4SLinus Torvalds 2951da177e4SLinus Torvalds if (!old_rdtodo) { 2961da177e4SLinus Torvalds result = usb_submit_urb(port->read_urb, GFP_ATOMIC); 2971da177e4SLinus Torvalds if (result) 29896fc8e8dSGreg Kroah-Hartman dev_err(dev, "%s - failed resubmitting read urb, error %d\n", 299194343d9SGreg Kroah-Hartman __func__, result); 30096fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - usb_submit_urb(read urb)\n", __func__); 3011da177e4SLinus Torvalds } 3021da177e4SLinus Torvalds } 3031da177e4SLinus Torvalds 3041da177e4SLinus Torvalds resubmit: 3051da177e4SLinus Torvalds result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC); 3061da177e4SLinus Torvalds if (result) 307194343d9SGreg Kroah-Hartman dev_err(&port->dev, "usb_submit_urb(read int) failed\n"); 30896fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - usb_submit_urb(int urb)\n", __func__); 3091da177e4SLinus Torvalds } 3101da177e4SLinus Torvalds 3117d12e780SDavid Howells static void cyberjack_read_bulk_callback(struct urb *urb) 3121da177e4SLinus Torvalds { 313cdc97792SMing Lei struct usb_serial_port *port = urb->context; 3141da177e4SLinus Torvalds struct cyberjack_private *priv = usb_get_serial_port_data(port); 31596fc8e8dSGreg Kroah-Hartman struct device *dev = &port->dev; 3161da177e4SLinus Torvalds unsigned char *data = urb->transfer_buffer; 3171da177e4SLinus Torvalds short todo; 3181da177e4SLinus Torvalds int result; 3197dcc85cdSGreg Kroah-Hartman int status = urb->status; 3201da177e4SLinus Torvalds 32159d33f2fSGreg Kroah-Hartman usb_serial_debug_data(dev, __func__, urb->actual_length, data); 3227dcc85cdSGreg Kroah-Hartman if (status) { 32396fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - nonzero read bulk status received: %d\n", 324441b62c1SHarvey Harrison __func__, status); 3251da177e4SLinus Torvalds return; 3261da177e4SLinus Torvalds } 3271da177e4SLinus Torvalds 3281da177e4SLinus Torvalds if (urb->actual_length) { 32905c7cd39SJiri Slaby tty_insert_flip_string(&port->port, data, urb->actual_length); 3302e124b4aSJiri Slaby tty_flip_buffer_push(&port->port); 3311da177e4SLinus Torvalds } 3321da177e4SLinus Torvalds 3331da177e4SLinus Torvalds spin_lock(&priv->lock); 3341da177e4SLinus Torvalds 3351da177e4SLinus Torvalds /* Reduce urbs to do by one. */ 3361da177e4SLinus Torvalds priv->rdtodo -= urb->actual_length; 3371da177e4SLinus Torvalds /* Just to be sure */ 338b9c52f15SAlan Cox if (priv->rdtodo < 0) 339b9c52f15SAlan Cox priv->rdtodo = 0; 3401da177e4SLinus Torvalds todo = priv->rdtodo; 3411da177e4SLinus Torvalds 3421da177e4SLinus Torvalds spin_unlock(&priv->lock); 3431da177e4SLinus Torvalds 34496fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - rdtodo: %d\n", __func__, todo); 3451da177e4SLinus Torvalds 3461da177e4SLinus Torvalds /* Continue to read if we have still urbs to do. */ 3471da177e4SLinus Torvalds if (todo /* || (urb->actual_length==port->bulk_in_endpointAddress)*/) { 3481da177e4SLinus Torvalds result = usb_submit_urb(port->read_urb, GFP_ATOMIC); 3491da177e4SLinus Torvalds if (result) 35096fc8e8dSGreg Kroah-Hartman dev_err(dev, "%s - failed resubmitting read urb, error %d\n", 35196fc8e8dSGreg Kroah-Hartman __func__, result); 35296fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - usb_submit_urb(read urb)\n", __func__); 3531da177e4SLinus Torvalds } 3541da177e4SLinus Torvalds } 3551da177e4SLinus Torvalds 3567d12e780SDavid Howells static void cyberjack_write_bulk_callback(struct urb *urb) 3571da177e4SLinus Torvalds { 358cdc97792SMing Lei struct usb_serial_port *port = urb->context; 3591da177e4SLinus Torvalds struct cyberjack_private *priv = usb_get_serial_port_data(port); 36096fc8e8dSGreg Kroah-Hartman struct device *dev = &port->dev; 3617dcc85cdSGreg Kroah-Hartman int status = urb->status; 3621da177e4SLinus Torvalds 363c1cac10cSJohan Hovold set_bit(0, &port->write_urbs_free); 3647dcc85cdSGreg Kroah-Hartman if (status) { 36596fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - nonzero write bulk status received: %d\n", 366441b62c1SHarvey Harrison __func__, status); 3671da177e4SLinus Torvalds return; 3681da177e4SLinus Torvalds } 3691da177e4SLinus Torvalds 3701da177e4SLinus Torvalds spin_lock(&priv->lock); 3711da177e4SLinus Torvalds 3721da177e4SLinus Torvalds /* only do something if we have more data to send */ 3731da177e4SLinus Torvalds if (priv->wrfilled) { 3741da177e4SLinus Torvalds int length, blksize, result; 3751da177e4SLinus Torvalds 37696fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - transmitting data (frame n)\n", __func__); 3771da177e4SLinus Torvalds 3781da177e4SLinus Torvalds length = ((priv->wrfilled - priv->wrsent) > port->bulk_out_size) ? 3791da177e4SLinus Torvalds port->bulk_out_size : (priv->wrfilled - priv->wrsent); 3801da177e4SLinus Torvalds 381b9c52f15SAlan Cox memcpy(port->write_urb->transfer_buffer, 382b9c52f15SAlan Cox priv->wrbuf + priv->wrsent, length); 3831da177e4SLinus Torvalds priv->wrsent += length; 3841da177e4SLinus Torvalds 3851da177e4SLinus Torvalds /* set up our urb */ 386fd11961aSJohan Hovold port->write_urb->transfer_buffer_length = length; 3871da177e4SLinus Torvalds 3881da177e4SLinus Torvalds /* send the data out the bulk port */ 3891da177e4SLinus Torvalds result = usb_submit_urb(port->write_urb, GFP_ATOMIC); 3901da177e4SLinus Torvalds if (result) { 39196fc8e8dSGreg Kroah-Hartman dev_err(dev, "%s - failed submitting write urb, error %d\n", 392b9c52f15SAlan Cox __func__, result); 3931da177e4SLinus Torvalds /* Throw away data. No better idea what to do with it. */ 3941da177e4SLinus Torvalds priv->wrfilled = 0; 3951da177e4SLinus Torvalds priv->wrsent = 0; 3961da177e4SLinus Torvalds goto exit; 3971da177e4SLinus Torvalds } 3981da177e4SLinus Torvalds 39996fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - priv->wrsent=%d\n", __func__, priv->wrsent); 40096fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - priv->wrfilled=%d\n", __func__, priv->wrfilled); 4011da177e4SLinus Torvalds 4021da177e4SLinus Torvalds blksize = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3; 4031da177e4SLinus Torvalds 404b9c52f15SAlan Cox if (priv->wrsent >= priv->wrfilled || 405b9c52f15SAlan Cox priv->wrsent >= blksize) { 40696fc8e8dSGreg Kroah-Hartman dev_dbg(dev, "%s - buffer cleaned\n", __func__); 4071da177e4SLinus Torvalds memset(priv->wrbuf, 0, sizeof(priv->wrbuf)); 4081da177e4SLinus Torvalds priv->wrfilled = 0; 4091da177e4SLinus Torvalds priv->wrsent = 0; 4101da177e4SLinus Torvalds } 4111da177e4SLinus Torvalds } 4121da177e4SLinus Torvalds 4131da177e4SLinus Torvalds exit: 4141da177e4SLinus Torvalds spin_unlock(&priv->lock); 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