blob: 5f17a3b9916d79ea28c82b04755e23d5680d4b2d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * REINER SCT cyberJack pinpad/e-com USB Chipcard Reader Driver
3 *
4 * Copyright (C) 2001 REINER SCT
5 * Author: Matthias Bruestle
6 *
7 * Contact: support@reiner-sct.com (see MAINTAINERS)
8 *
9 * This program is largely derived from work by the linux-usb group
10 * and associated source files. Please see the usb/serial files for
11 * individual credits and copyrights.
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * Thanks to Greg Kroah-Hartman (greg@kroah.com) for his help and
19 * patience.
20 *
21 * In case of problems, please write to the contact e-mail address
22 * mentioned above.
23 *
24 * Please note that later models of the cyberjack reader family are
25 * supported by a libusb-based userspace device driver.
26 *
27 * Homepage: http://www.reiner-sct.de/support/treiber_cyberjack.php#linux
28 */
29
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/kernel.h>
32#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/slab.h>
34#include <linux/tty.h>
35#include <linux/tty_driver.h>
36#include <linux/tty_flip.h>
37#include <linux/module.h>
38#include <linux/spinlock.h>
Alan Coxb9c52f12008-07-22 11:10:27 +010039#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070041#include <linux/usb/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#define CYBERJACK_LOCAL_BUF_SIZE 32
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#define DRIVER_AUTHOR "Matthias Bruestle"
46#define DRIVER_DESC "REINER SCT cyberJack pinpad/e-com USB Chipcard Reader Driver"
47
48
49#define CYBERJACK_VENDOR_ID 0x0C4B
50#define CYBERJACK_PRODUCT_ID 0x0100
51
52/* Function prototypes */
Johan Hovolda9556042012-10-15 18:20:54 +020053static int cyberjack_port_probe(struct usb_serial_port *port);
54static int cyberjack_port_remove(struct usb_serial_port *port);
Alan Cox95da3102008-07-22 11:09:07 +010055static int cyberjack_open(struct tty_struct *tty,
Alan Coxa509a7e2009-09-19 13:13:26 -070056 struct usb_serial_port *port);
Alan Cox335f8512009-06-11 12:26:29 +010057static void cyberjack_close(struct usb_serial_port *port);
Alan Cox95da3102008-07-22 11:09:07 +010058static int cyberjack_write(struct tty_struct *tty,
59 struct usb_serial_port *port, const unsigned char *buf, int count);
Alan Coxb9c52f12008-07-22 11:10:27 +010060static int cyberjack_write_room(struct tty_struct *tty);
Alan Cox95da3102008-07-22 11:09:07 +010061static void cyberjack_read_int_callback(struct urb *urb);
62static void cyberjack_read_bulk_callback(struct urb *urb);
63static void cyberjack_write_bulk_callback(struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Németh Márton7d40d7e2010-01-10 15:34:24 +010065static const struct usb_device_id id_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 { USB_DEVICE(CYBERJACK_VENDOR_ID, CYBERJACK_PRODUCT_ID) },
67 { } /* Terminating entry */
68};
69
Alan Coxb9c52f12008-07-22 11:10:27 +010070MODULE_DEVICE_TABLE(usb, id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -070072static struct usb_serial_driver cyberjack_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070073 .driver = {
74 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070075 .name = "cyberjack",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070076 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070077 .description = "Reiner SCT Cyberjack USB card reader",
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 .id_table = id_table,
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 .num_ports = 1,
Johan Hovolda9556042012-10-15 18:20:54 +020080 .port_probe = cyberjack_port_probe,
81 .port_remove = cyberjack_port_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 .open = cyberjack_open,
83 .close = cyberjack_close,
84 .write = cyberjack_write,
Johannes Hölzld9b1b782006-12-17 21:50:24 +010085 .write_room = cyberjack_write_room,
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 .read_int_callback = cyberjack_read_int_callback,
87 .read_bulk_callback = cyberjack_read_bulk_callback,
88 .write_bulk_callback = cyberjack_write_bulk_callback,
89};
90
Alan Stern08a4f6b2012-02-23 14:56:17 -050091static struct usb_serial_driver * const serial_drivers[] = {
92 &cyberjack_device, NULL
93};
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095struct cyberjack_private {
96 spinlock_t lock; /* Lock for SMP */
97 short rdtodo; /* Bytes still to read */
98 unsigned char wrbuf[5*64]; /* Buffer for collecting data to write */
99 short wrfilled; /* Overall data size we already got */
100 short wrsent; /* Data already sent */
101};
102
Johan Hovolda9556042012-10-15 18:20:54 +0200103static int cyberjack_port_probe(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
105 struct cyberjack_private *priv;
Johan Hovolda9556042012-10-15 18:20:54 +0200106 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 priv = kmalloc(sizeof(struct cyberjack_private), GFP_KERNEL);
109 if (!priv)
110 return -ENOMEM;
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 spin_lock_init(&priv->lock);
113 priv->rdtodo = 0;
114 priv->wrfilled = 0;
115 priv->wrsent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Johan Hovolda9556042012-10-15 18:20:54 +0200117 usb_set_serial_port_data(port, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Johan Hovolda9556042012-10-15 18:20:54 +0200119 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
120 if (result)
121 dev_err(&port->dev, "usb_submit_urb(read int) failed\n");
122
123 return 0;
124}
125
126static int cyberjack_port_remove(struct usb_serial_port *port)
127{
128 struct cyberjack_private *priv;
129
Johan Hovold30d9d422013-03-21 12:36:45 +0100130 usb_kill_urb(port->interrupt_in_urb);
131
Johan Hovolda9556042012-10-15 18:20:54 +0200132 priv = usb_get_serial_port_data(port);
133 kfree(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Alan Coxb9c52f12008-07-22 11:10:27 +0100135 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
Alan Cox95da3102008-07-22 11:09:07 +0100138static int cyberjack_open(struct tty_struct *tty,
Alan Coxa509a7e2009-09-19 13:13:26 -0700139 struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
141 struct cyberjack_private *priv;
142 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700144 dev_dbg(&port->dev, "%s - usb_clear_halt\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 usb_clear_halt(port->serial->dev, port->write_urb->pipe);
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 priv = usb_get_serial_port_data(port);
148 spin_lock_irqsave(&priv->lock, flags);
149 priv->rdtodo = 0;
150 priv->wrfilled = 0;
151 priv->wrsent = 0;
152 spin_unlock_irqrestore(&priv->lock, flags);
153
Mathieu OTHACEHE0f3083a2016-02-04 19:01:28 +0100154 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
Alan Cox335f8512009-06-11 12:26:29 +0100157static void cyberjack_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Johan Hovold1bc77f42013-03-21 12:36:29 +0100159 usb_kill_urb(port->write_urb);
160 usb_kill_urb(port->read_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
Alan Cox95da3102008-07-22 11:09:07 +0100163static int cyberjack_write(struct tty_struct *tty,
164 struct usb_serial_port *port, const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700166 struct device *dev = &port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 struct cyberjack_private *priv = usb_get_serial_port_data(port);
168 unsigned long flags;
169 int result;
170 int wrexpected;
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 if (count == 0) {
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700173 dev_dbg(dev, "%s - write request of 0 bytes\n", __func__);
Alan Coxa5b6f602008-04-08 17:16:06 +0100174 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 }
176
Johan Hovoldc1cac102011-11-06 19:06:23 +0100177 if (!test_and_clear_bit(0, &port->write_urbs_free)) {
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700178 dev_dbg(dev, "%s - already writing\n", __func__);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700179 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 }
181
182 spin_lock_irqsave(&priv->lock, flags);
183
Alan Coxb9c52f12008-07-22 11:10:27 +0100184 if (count+priv->wrfilled > sizeof(priv->wrbuf)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 /* To much data for buffer. Reset buffer. */
Alan Coxa5b6f602008-04-08 17:16:06 +0100186 priv->wrfilled = 0;
Alan Coxa5b6f602008-04-08 17:16:06 +0100187 spin_unlock_irqrestore(&priv->lock, flags);
Johan Hovoldc1cac102011-11-06 19:06:23 +0100188 set_bit(0, &port->write_urbs_free);
Alan Coxa5b6f602008-04-08 17:16:06 +0100189 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 }
191
192 /* Copy data */
Alan Coxb9c52f12008-07-22 11:10:27 +0100193 memcpy(priv->wrbuf + priv->wrfilled, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100195 usb_serial_debug_data(dev, __func__, count, priv->wrbuf + priv->wrfilled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 priv->wrfilled += count;
197
Alan Coxb9c52f12008-07-22 11:10:27 +0100198 if (priv->wrfilled >= 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 wrexpected = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700200 dev_dbg(dev, "%s - expected data: %d\n", __func__, wrexpected);
Alan Coxb9c52f12008-07-22 11:10:27 +0100201 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 wrexpected = sizeof(priv->wrbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Alan Coxb9c52f12008-07-22 11:10:27 +0100204 if (priv->wrfilled >= wrexpected) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 /* We have enough data to begin transmission */
206 int length;
207
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700208 dev_dbg(dev, "%s - transmitting data (frame 1)\n", __func__);
Alan Coxb9c52f12008-07-22 11:10:27 +0100209 length = (wrexpected > port->bulk_out_size) ?
210 port->bulk_out_size : wrexpected;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Alan Coxb9c52f12008-07-22 11:10:27 +0100212 memcpy(port->write_urb->transfer_buffer, priv->wrbuf, length);
213 priv->wrsent = length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 /* set up our urb */
Johan Hovoldfd119612011-11-06 19:06:30 +0100216 port->write_urb->transfer_buffer_length = length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218 /* send the data out the bulk port */
219 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
220 if (result) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700221 dev_err(&port->dev,
Johan Hovoldd9a38a82014-03-12 19:09:42 +0100222 "%s - failed submitting write urb, error %d\n",
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700223 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 /* Throw away data. No better idea what to do with it. */
Alan Coxa5b6f602008-04-08 17:16:06 +0100225 priv->wrfilled = 0;
226 priv->wrsent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 spin_unlock_irqrestore(&priv->lock, flags);
Johan Hovoldc1cac102011-11-06 19:06:23 +0100228 set_bit(0, &port->write_urbs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return 0;
230 }
231
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700232 dev_dbg(dev, "%s - priv->wrsent=%d\n", __func__, priv->wrsent);
233 dev_dbg(dev, "%s - priv->wrfilled=%d\n", __func__, priv->wrfilled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Alan Coxb9c52f12008-07-22 11:10:27 +0100235 if (priv->wrsent >= priv->wrfilled) {
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700236 dev_dbg(dev, "%s - buffer cleaned\n", __func__);
Alan Coxb9c52f12008-07-22 11:10:27 +0100237 memset(priv->wrbuf, 0, sizeof(priv->wrbuf));
Alan Coxa5b6f602008-04-08 17:16:06 +0100238 priv->wrfilled = 0;
239 priv->wrsent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
241 }
242
243 spin_unlock_irqrestore(&priv->lock, flags);
244
Alan Coxb9c52f12008-07-22 11:10:27 +0100245 return count;
246}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Alan Cox95da3102008-07-22 11:09:07 +0100248static int cyberjack_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Alan Coxa5b6f602008-04-08 17:16:06 +0100250 /* FIXME: .... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 return CYBERJACK_LOCAL_BUF_SIZE;
252}
253
Alan Cox95da3102008-07-22 11:09:07 +0100254static void cyberjack_read_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
Ming Leicdc97792008-02-24 18:41:47 +0800256 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 struct cyberjack_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700258 struct device *dev = &port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartman7dcc85c2007-06-15 15:44:13 -0700260 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 int result;
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 /* the urb might have been killed. */
Greg Kroah-Hartman7dcc85c2007-06-15 15:44:13 -0700264 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 return;
266
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100267 usb_serial_debug_data(dev, __func__, urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 /* React only to interrupts signaling a bulk_in transfer */
Alan Coxb9c52f12008-07-22 11:10:27 +0100270 if (urb->actual_length == 4 && data[0] == 0x01) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 short old_rdtodo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 /* This is a announcement of coming bulk_ins. */
274 unsigned short size = ((unsigned short)data[3]<<8)+data[2]+3;
275
276 spin_lock(&priv->lock);
277
278 old_rdtodo = priv->rdtodo;
279
Dan Carpenter5287bf72013-10-06 03:32:53 +0300280 if (old_rdtodo > SHRT_MAX - size) {
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700281 dev_dbg(dev, "To many bulk_in urbs to do.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 spin_unlock(&priv->lock);
283 goto resubmit;
284 }
285
Rahul Bedarkarcd8c5052014-01-02 19:29:24 +0530286 /* "+=" is probably more fault tolerant than "=" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 priv->rdtodo += size;
288
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700289 dev_dbg(dev, "%s - rdtodo: %d\n", __func__, priv->rdtodo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 spin_unlock(&priv->lock);
292
Alan Coxb9c52f12008-07-22 11:10:27 +0100293 if (!old_rdtodo) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
Alan Coxb9c52f12008-07-22 11:10:27 +0100295 if (result)
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700296 dev_err(dev, "%s - failed resubmitting read urb, error %d\n",
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700297 __func__, result);
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700298 dev_dbg(dev, "%s - usb_submit_urb(read urb)\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300 }
301
302resubmit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
304 if (result)
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700305 dev_err(&port->dev, "usb_submit_urb(read int) failed\n");
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700306 dev_dbg(dev, "%s - usb_submit_urb(int urb)\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
Alan Cox95da3102008-07-22 11:09:07 +0100309static void cyberjack_read_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Ming Leicdc97792008-02-24 18:41:47 +0800311 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 struct cyberjack_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700313 struct device *dev = &port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 unsigned char *data = urb->transfer_buffer;
315 short todo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 int result;
Greg Kroah-Hartman7dcc85c2007-06-15 15:44:13 -0700317 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100319 usb_serial_debug_data(dev, __func__, urb->actual_length, data);
Greg Kroah-Hartman7dcc85c2007-06-15 15:44:13 -0700320 if (status) {
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700321 dev_dbg(dev, "%s - nonzero read bulk status received: %d\n",
322 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 return;
324 }
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (urb->actual_length) {
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100327 tty_insert_flip_string(&port->port, data, urb->actual_length);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100328 tty_flip_buffer_push(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 }
330
331 spin_lock(&priv->lock);
332
333 /* Reduce urbs to do by one. */
Alan Coxb9c52f12008-07-22 11:10:27 +0100334 priv->rdtodo -= urb->actual_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 /* Just to be sure */
Alan Coxb9c52f12008-07-22 11:10:27 +0100336 if (priv->rdtodo < 0)
337 priv->rdtodo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 todo = priv->rdtodo;
339
340 spin_unlock(&priv->lock);
341
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700342 dev_dbg(dev, "%s - rdtodo: %d\n", __func__, todo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 /* Continue to read if we have still urbs to do. */
Alan Coxb9c52f12008-07-22 11:10:27 +0100345 if (todo /* || (urb->actual_length==port->bulk_in_endpointAddress)*/) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
347 if (result)
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700348 dev_err(dev, "%s - failed resubmitting read urb, error %d\n",
349 __func__, result);
350 dev_dbg(dev, "%s - usb_submit_urb(read urb)\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 }
352}
353
Alan Cox95da3102008-07-22 11:09:07 +0100354static void cyberjack_write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
Ming Leicdc97792008-02-24 18:41:47 +0800356 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 struct cyberjack_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700358 struct device *dev = &port->dev;
Greg Kroah-Hartman7dcc85c2007-06-15 15:44:13 -0700359 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Johan Hovoldc1cac102011-11-06 19:06:23 +0100361 set_bit(0, &port->write_urbs_free);
Greg Kroah-Hartman7dcc85c2007-06-15 15:44:13 -0700362 if (status) {
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700363 dev_dbg(dev, "%s - nonzero write bulk status received: %d\n",
364 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 return;
366 }
367
368 spin_lock(&priv->lock);
369
370 /* only do something if we have more data to send */
Alan Coxb9c52f12008-07-22 11:10:27 +0100371 if (priv->wrfilled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 int length, blksize, result;
373
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700374 dev_dbg(dev, "%s - transmitting data (frame n)\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 length = ((priv->wrfilled - priv->wrsent) > port->bulk_out_size) ?
377 port->bulk_out_size : (priv->wrfilled - priv->wrsent);
378
Alan Coxb9c52f12008-07-22 11:10:27 +0100379 memcpy(port->write_urb->transfer_buffer,
380 priv->wrbuf + priv->wrsent, length);
381 priv->wrsent += length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383 /* set up our urb */
Johan Hovoldfd119612011-11-06 19:06:30 +0100384 port->write_urb->transfer_buffer_length = length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386 /* send the data out the bulk port */
387 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
388 if (result) {
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700389 dev_err(dev, "%s - failed submitting write urb, error %d\n",
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700390 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 /* Throw away data. No better idea what to do with it. */
Alan Coxa5b6f602008-04-08 17:16:06 +0100392 priv->wrfilled = 0;
393 priv->wrsent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 goto exit;
395 }
396
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700397 dev_dbg(dev, "%s - priv->wrsent=%d\n", __func__, priv->wrsent);
398 dev_dbg(dev, "%s - priv->wrfilled=%d\n", __func__, priv->wrfilled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 blksize = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
401
Alan Coxb9c52f12008-07-22 11:10:27 +0100402 if (priv->wrsent >= priv->wrfilled ||
403 priv->wrsent >= blksize) {
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700404 dev_dbg(dev, "%s - buffer cleaned\n", __func__);
Alan Coxb9c52f12008-07-22 11:10:27 +0100405 memset(priv->wrbuf, 0, sizeof(priv->wrbuf));
Alan Coxa5b6f602008-04-08 17:16:06 +0100406 priv->wrfilled = 0;
407 priv->wrsent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
409 }
410
411exit:
412 spin_unlock(&priv->lock);
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700413 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414}
415
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700416module_usb_serial_driver(serial_drivers, id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Alan Coxb9c52f12008-07-22 11:10:27 +0100418MODULE_AUTHOR(DRIVER_AUTHOR);
419MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420MODULE_LICENSE("GPL");