blob: de9253d63a480c50015fa1640172963ed04751c6 [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>
33#include <linux/init.h>
34#include <linux/slab.h>
35#include <linux/tty.h>
36#include <linux/tty_driver.h>
37#include <linux/tty_flip.h>
38#include <linux/module.h>
39#include <linux/spinlock.h>
Alan Coxb9c52f12008-07-22 11:10:27 +010040#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070042#include <linux/usb/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44#define CYBERJACK_LOCAL_BUF_SIZE 32
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#define DRIVER_AUTHOR "Matthias Bruestle"
47#define DRIVER_DESC "REINER SCT cyberJack pinpad/e-com USB Chipcard Reader Driver"
48
49
50#define CYBERJACK_VENDOR_ID 0x0C4B
51#define CYBERJACK_PRODUCT_ID 0x0100
52
53/* Function prototypes */
Alan Sternf9c99bb2009-06-02 11:53:55 -040054static void cyberjack_disconnect(struct usb_serial *serial);
Johan Hovolda9556042012-10-15 18:20:54 +020055static int cyberjack_port_probe(struct usb_serial_port *port);
56static int cyberjack_port_remove(struct usb_serial_port *port);
Alan Cox95da3102008-07-22 11:09:07 +010057static int cyberjack_open(struct tty_struct *tty,
Alan Coxa509a7e2009-09-19 13:13:26 -070058 struct usb_serial_port *port);
Alan Cox335f8512009-06-11 12:26:29 +010059static void cyberjack_close(struct usb_serial_port *port);
Alan Cox95da3102008-07-22 11:09:07 +010060static int cyberjack_write(struct tty_struct *tty,
61 struct usb_serial_port *port, const unsigned char *buf, int count);
Alan Coxb9c52f12008-07-22 11:10:27 +010062static int cyberjack_write_room(struct tty_struct *tty);
Alan Cox95da3102008-07-22 11:09:07 +010063static void cyberjack_read_int_callback(struct urb *urb);
64static void cyberjack_read_bulk_callback(struct urb *urb);
65static void cyberjack_write_bulk_callback(struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Németh Márton7d40d7e2010-01-10 15:34:24 +010067static const struct usb_device_id id_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 { USB_DEVICE(CYBERJACK_VENDOR_ID, CYBERJACK_PRODUCT_ID) },
69 { } /* Terminating entry */
70};
71
Alan Coxb9c52f12008-07-22 11:10:27 +010072MODULE_DEVICE_TABLE(usb, id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -070074static struct usb_serial_driver cyberjack_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070075 .driver = {
76 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070077 .name = "cyberjack",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070078 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070079 .description = "Reiner SCT Cyberjack USB card reader",
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 .id_table = id_table,
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 .num_ports = 1,
Alan Sternf9c99bb2009-06-02 11:53:55 -040082 .disconnect = cyberjack_disconnect,
Johan Hovolda9556042012-10-15 18:20:54 +020083 .port_probe = cyberjack_port_probe,
84 .port_remove = cyberjack_port_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 .open = cyberjack_open,
86 .close = cyberjack_close,
87 .write = cyberjack_write,
Johannes Hölzld9b1b782006-12-17 21:50:24 +010088 .write_room = cyberjack_write_room,
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 .read_int_callback = cyberjack_read_int_callback,
90 .read_bulk_callback = cyberjack_read_bulk_callback,
91 .write_bulk_callback = cyberjack_write_bulk_callback,
92};
93
Alan Stern08a4f6b2012-02-23 14:56:17 -050094static struct usb_serial_driver * const serial_drivers[] = {
95 &cyberjack_device, NULL
96};
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098struct cyberjack_private {
99 spinlock_t lock; /* Lock for SMP */
100 short rdtodo; /* Bytes still to read */
101 unsigned char wrbuf[5*64]; /* Buffer for collecting data to write */
102 short wrfilled; /* Overall data size we already got */
103 short wrsent; /* Data already sent */
104};
105
Johan Hovolda9556042012-10-15 18:20:54 +0200106static int cyberjack_port_probe(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
108 struct cyberjack_private *priv;
Johan Hovolda9556042012-10-15 18:20:54 +0200109 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 priv = kmalloc(sizeof(struct cyberjack_private), GFP_KERNEL);
112 if (!priv)
113 return -ENOMEM;
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 spin_lock_init(&priv->lock);
116 priv->rdtodo = 0;
117 priv->wrfilled = 0;
118 priv->wrsent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Johan Hovolda9556042012-10-15 18:20:54 +0200120 usb_set_serial_port_data(port, priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Johan Hovolda9556042012-10-15 18:20:54 +0200122 result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
123 if (result)
124 dev_err(&port->dev, "usb_submit_urb(read int) failed\n");
125
126 return 0;
127}
128
129static int cyberjack_port_remove(struct usb_serial_port *port)
130{
131 struct cyberjack_private *priv;
132
133 priv = usb_get_serial_port_data(port);
134 kfree(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Alan Coxb9c52f12008-07-22 11:10:27 +0100136 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
Alan Sternf9c99bb2009-06-02 11:53:55 -0400139static void cyberjack_disconnect(struct usb_serial *serial)
140{
141 int i;
142
Alan Sternf9c99bb2009-06-02 11:53:55 -0400143 for (i = 0; i < serial->num_ports; ++i)
144 usb_kill_urb(serial->port[i]->interrupt_in_urb);
145}
146
Alan Cox95da3102008-07-22 11:09:07 +0100147static int cyberjack_open(struct tty_struct *tty,
Alan Coxa509a7e2009-09-19 13:13:26 -0700148 struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
150 struct cyberjack_private *priv;
151 unsigned long flags;
152 int result = 0;
153
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700154 dev_dbg(&port->dev, "%s - usb_clear_halt\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 usb_clear_halt(port->serial->dev, port->write_urb->pipe);
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 priv = usb_get_serial_port_data(port);
158 spin_lock_irqsave(&priv->lock, flags);
159 priv->rdtodo = 0;
160 priv->wrfilled = 0;
161 priv->wrsent = 0;
162 spin_unlock_irqrestore(&priv->lock, flags);
163
164 return result;
165}
166
Alan Cox335f8512009-06-11 12:26:29 +0100167static void cyberjack_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Johan Hovold1bc77f42013-03-21 12:36:29 +0100169 usb_kill_urb(port->write_urb);
170 usb_kill_urb(port->read_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
Alan Cox95da3102008-07-22 11:09:07 +0100173static int cyberjack_write(struct tty_struct *tty,
174 struct usb_serial_port *port, const unsigned char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700176 struct device *dev = &port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 struct cyberjack_private *priv = usb_get_serial_port_data(port);
178 unsigned long flags;
179 int result;
180 int wrexpected;
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 if (count == 0) {
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700183 dev_dbg(dev, "%s - write request of 0 bytes\n", __func__);
Alan Coxa5b6f602008-04-08 17:16:06 +0100184 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 }
186
Johan Hovoldc1cac102011-11-06 19:06:23 +0100187 if (!test_and_clear_bit(0, &port->write_urbs_free)) {
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700188 dev_dbg(dev, "%s - already writing\n", __func__);
Greg Kroah-Hartman507ca9b2005-04-23 12:49:16 -0700189 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 }
191
192 spin_lock_irqsave(&priv->lock, flags);
193
Alan Coxb9c52f12008-07-22 11:10:27 +0100194 if (count+priv->wrfilled > sizeof(priv->wrbuf)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 /* To much data for buffer. Reset buffer. */
Alan Coxa5b6f602008-04-08 17:16:06 +0100196 priv->wrfilled = 0;
Alan Coxa5b6f602008-04-08 17:16:06 +0100197 spin_unlock_irqrestore(&priv->lock, flags);
Johan Hovoldc1cac102011-11-06 19:06:23 +0100198 set_bit(0, &port->write_urbs_free);
Alan Coxa5b6f602008-04-08 17:16:06 +0100199 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
201
202 /* Copy data */
Alan Coxb9c52f12008-07-22 11:10:27 +0100203 memcpy(priv->wrbuf + priv->wrfilled, buf, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100205 usb_serial_debug_data(dev, __func__, count, priv->wrbuf + priv->wrfilled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 priv->wrfilled += count;
207
Alan Coxb9c52f12008-07-22 11:10:27 +0100208 if (priv->wrfilled >= 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 wrexpected = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700210 dev_dbg(dev, "%s - expected data: %d\n", __func__, wrexpected);
Alan Coxb9c52f12008-07-22 11:10:27 +0100211 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 wrexpected = sizeof(priv->wrbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Alan Coxb9c52f12008-07-22 11:10:27 +0100214 if (priv->wrfilled >= wrexpected) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 /* We have enough data to begin transmission */
216 int length;
217
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700218 dev_dbg(dev, "%s - transmitting data (frame 1)\n", __func__);
Alan Coxb9c52f12008-07-22 11:10:27 +0100219 length = (wrexpected > port->bulk_out_size) ?
220 port->bulk_out_size : wrexpected;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Alan Coxb9c52f12008-07-22 11:10:27 +0100222 memcpy(port->write_urb->transfer_buffer, priv->wrbuf, length);
223 priv->wrsent = length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 /* set up our urb */
Johan Hovoldfd119612011-11-06 19:06:30 +0100226 port->write_urb->transfer_buffer_length = length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 /* send the data out the bulk port */
229 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
230 if (result) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700231 dev_err(&port->dev,
232 "%s - failed submitting write urb, error %d",
233 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 /* Throw away data. No better idea what to do with it. */
Alan Coxa5b6f602008-04-08 17:16:06 +0100235 priv->wrfilled = 0;
236 priv->wrsent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 spin_unlock_irqrestore(&priv->lock, flags);
Johan Hovoldc1cac102011-11-06 19:06:23 +0100238 set_bit(0, &port->write_urbs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 return 0;
240 }
241
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700242 dev_dbg(dev, "%s - priv->wrsent=%d\n", __func__, priv->wrsent);
243 dev_dbg(dev, "%s - priv->wrfilled=%d\n", __func__, priv->wrfilled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Alan Coxb9c52f12008-07-22 11:10:27 +0100245 if (priv->wrsent >= priv->wrfilled) {
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700246 dev_dbg(dev, "%s - buffer cleaned\n", __func__);
Alan Coxb9c52f12008-07-22 11:10:27 +0100247 memset(priv->wrbuf, 0, sizeof(priv->wrbuf));
Alan Coxa5b6f602008-04-08 17:16:06 +0100248 priv->wrfilled = 0;
249 priv->wrsent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
251 }
252
253 spin_unlock_irqrestore(&priv->lock, flags);
254
Alan Coxb9c52f12008-07-22 11:10:27 +0100255 return count;
256}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Alan Cox95da3102008-07-22 11:09:07 +0100258static int cyberjack_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
Alan Coxa5b6f602008-04-08 17:16:06 +0100260 /* FIXME: .... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 return CYBERJACK_LOCAL_BUF_SIZE;
262}
263
Alan Cox95da3102008-07-22 11:09:07 +0100264static void cyberjack_read_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Ming Leicdc97792008-02-24 18:41:47 +0800266 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 struct cyberjack_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700268 struct device *dev = &port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartman7dcc85c2007-06-15 15:44:13 -0700270 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 int result;
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 /* the urb might have been killed. */
Greg Kroah-Hartman7dcc85c2007-06-15 15:44:13 -0700274 if (status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 return;
276
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100277 usb_serial_debug_data(dev, __func__, urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 /* React only to interrupts signaling a bulk_in transfer */
Alan Coxb9c52f12008-07-22 11:10:27 +0100280 if (urb->actual_length == 4 && data[0] == 0x01) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 short old_rdtodo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 /* This is a announcement of coming bulk_ins. */
284 unsigned short size = ((unsigned short)data[3]<<8)+data[2]+3;
285
286 spin_lock(&priv->lock);
287
288 old_rdtodo = priv->rdtodo;
289
Alan Coxb9c52f12008-07-22 11:10:27 +0100290 if (old_rdtodo + size < old_rdtodo) {
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700291 dev_dbg(dev, "To many bulk_in urbs to do.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 spin_unlock(&priv->lock);
293 goto resubmit;
294 }
295
296 /* "+=" is probably more fault tollerant than "=" */
297 priv->rdtodo += size;
298
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700299 dev_dbg(dev, "%s - rdtodo: %d\n", __func__, priv->rdtodo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 spin_unlock(&priv->lock);
302
Alan Coxb9c52f12008-07-22 11:10:27 +0100303 if (!old_rdtodo) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
Alan Coxb9c52f12008-07-22 11:10:27 +0100305 if (result)
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700306 dev_err(dev, "%s - failed resubmitting read urb, error %d\n",
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700307 __func__, result);
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700308 dev_dbg(dev, "%s - usb_submit_urb(read urb)\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
310 }
311
312resubmit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
314 if (result)
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700315 dev_err(&port->dev, "usb_submit_urb(read int) failed\n");
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700316 dev_dbg(dev, "%s - usb_submit_urb(int urb)\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
Alan Cox95da3102008-07-22 11:09:07 +0100319static void cyberjack_read_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
Ming Leicdc97792008-02-24 18:41:47 +0800321 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 struct cyberjack_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700323 struct device *dev = &port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 unsigned char *data = urb->transfer_buffer;
325 short todo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 int result;
Greg Kroah-Hartman7dcc85c2007-06-15 15:44:13 -0700327 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100329 usb_serial_debug_data(dev, __func__, urb->actual_length, data);
Greg Kroah-Hartman7dcc85c2007-06-15 15:44:13 -0700330 if (status) {
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700331 dev_dbg(dev, "%s - nonzero read bulk status received: %d\n",
332 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 return;
334 }
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 if (urb->actual_length) {
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100337 tty_insert_flip_string(&port->port, data, urb->actual_length);
Jiri Slaby2e124b42013-01-03 15:53:06 +0100338 tty_flip_buffer_push(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
340
341 spin_lock(&priv->lock);
342
343 /* Reduce urbs to do by one. */
Alan Coxb9c52f12008-07-22 11:10:27 +0100344 priv->rdtodo -= urb->actual_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 /* Just to be sure */
Alan Coxb9c52f12008-07-22 11:10:27 +0100346 if (priv->rdtodo < 0)
347 priv->rdtodo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 todo = priv->rdtodo;
349
350 spin_unlock(&priv->lock);
351
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700352 dev_dbg(dev, "%s - rdtodo: %d\n", __func__, todo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 /* Continue to read if we have still urbs to do. */
Alan Coxb9c52f12008-07-22 11:10:27 +0100355 if (todo /* || (urb->actual_length==port->bulk_in_endpointAddress)*/) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
357 if (result)
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700358 dev_err(dev, "%s - failed resubmitting read urb, error %d\n",
359 __func__, result);
360 dev_dbg(dev, "%s - usb_submit_urb(read urb)\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
362}
363
Alan Cox95da3102008-07-22 11:09:07 +0100364static void cyberjack_write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
Ming Leicdc97792008-02-24 18:41:47 +0800366 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 struct cyberjack_private *priv = usb_get_serial_port_data(port);
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700368 struct device *dev = &port->dev;
Greg Kroah-Hartman7dcc85c2007-06-15 15:44:13 -0700369 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Johan Hovoldc1cac102011-11-06 19:06:23 +0100371 set_bit(0, &port->write_urbs_free);
Greg Kroah-Hartman7dcc85c2007-06-15 15:44:13 -0700372 if (status) {
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700373 dev_dbg(dev, "%s - nonzero write bulk status received: %d\n",
374 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return;
376 }
377
378 spin_lock(&priv->lock);
379
380 /* only do something if we have more data to send */
Alan Coxb9c52f12008-07-22 11:10:27 +0100381 if (priv->wrfilled) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 int length, blksize, result;
383
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700384 dev_dbg(dev, "%s - transmitting data (frame n)\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386 length = ((priv->wrfilled - priv->wrsent) > port->bulk_out_size) ?
387 port->bulk_out_size : (priv->wrfilled - priv->wrsent);
388
Alan Coxb9c52f12008-07-22 11:10:27 +0100389 memcpy(port->write_urb->transfer_buffer,
390 priv->wrbuf + priv->wrsent, length);
391 priv->wrsent += length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 /* set up our urb */
Johan Hovoldfd119612011-11-06 19:06:30 +0100394 port->write_urb->transfer_buffer_length = length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 /* send the data out the bulk port */
397 result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
398 if (result) {
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700399 dev_err(dev, "%s - failed submitting write urb, error %d\n",
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700400 __func__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 /* Throw away data. No better idea what to do with it. */
Alan Coxa5b6f602008-04-08 17:16:06 +0100402 priv->wrfilled = 0;
403 priv->wrsent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 goto exit;
405 }
406
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700407 dev_dbg(dev, "%s - priv->wrsent=%d\n", __func__, priv->wrsent);
408 dev_dbg(dev, "%s - priv->wrfilled=%d\n", __func__, priv->wrfilled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 blksize = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
411
Alan Coxb9c52f12008-07-22 11:10:27 +0100412 if (priv->wrsent >= priv->wrfilled ||
413 priv->wrsent >= blksize) {
Greg Kroah-Hartman96fc8e82012-09-13 17:18:14 -0700414 dev_dbg(dev, "%s - buffer cleaned\n", __func__);
Alan Coxb9c52f12008-07-22 11:10:27 +0100415 memset(priv->wrbuf, 0, sizeof(priv->wrbuf));
Alan Coxa5b6f602008-04-08 17:16:06 +0100416 priv->wrfilled = 0;
417 priv->wrsent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
419 }
420
421exit:
422 spin_unlock(&priv->lock);
Pete Zaitcevcf2c7482006-05-22 21:58:49 -0700423 usb_serial_port_softint(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700426module_usb_serial_driver(serial_drivers, id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Alan Coxb9c52f12008-07-22 11:10:27 +0100428MODULE_AUTHOR(DRIVER_AUTHOR);
429MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430MODULE_LICENSE("GPL");