xref: /linux/drivers/usb/serial/cyberjack.c (revision 08ec212c0f92cbf30e3ecc7349f18151714041d6)
1 /*
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 
31 #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>
40 #include <linux/uaccess.h>
41 #include <linux/usb.h>
42 #include <linux/usb/serial.h>
43 
44 #define CYBERJACK_LOCAL_BUF_SIZE 32
45 
46 /*
47  * Version Information
48  */
49 #define DRIVER_VERSION "v1.01"
50 #define DRIVER_AUTHOR "Matthias Bruestle"
51 #define DRIVER_DESC "REINER SCT cyberJack pinpad/e-com USB Chipcard Reader Driver"
52 
53 
54 #define CYBERJACK_VENDOR_ID	0x0C4B
55 #define CYBERJACK_PRODUCT_ID	0x0100
56 
57 /* Function prototypes */
58 static void cyberjack_disconnect(struct usb_serial *serial);
59 static int cyberjack_port_probe(struct usb_serial_port *port);
60 static int cyberjack_port_remove(struct usb_serial_port *port);
61 static int  cyberjack_open(struct tty_struct *tty,
62 	struct usb_serial_port *port);
63 static void cyberjack_close(struct usb_serial_port *port);
64 static int cyberjack_write(struct tty_struct *tty,
65 	struct usb_serial_port *port, const unsigned char *buf, int count);
66 static int cyberjack_write_room(struct tty_struct *tty);
67 static void cyberjack_read_int_callback(struct urb *urb);
68 static void cyberjack_read_bulk_callback(struct urb *urb);
69 static void cyberjack_write_bulk_callback(struct urb *urb);
70 
71 static const struct usb_device_id id_table[] = {
72 	{ USB_DEVICE(CYBERJACK_VENDOR_ID, CYBERJACK_PRODUCT_ID) },
73 	{ }			/* Terminating entry */
74 };
75 
76 MODULE_DEVICE_TABLE(usb, id_table);
77 
78 static struct usb_serial_driver cyberjack_device = {
79 	.driver = {
80 		.owner =	THIS_MODULE,
81 		.name =		"cyberjack",
82 	},
83 	.description =		"Reiner SCT Cyberjack USB card reader",
84 	.id_table =		id_table,
85 	.num_ports =		1,
86 	.disconnect =		cyberjack_disconnect,
87 	.port_probe =		cyberjack_port_probe,
88 	.port_remove =		cyberjack_port_remove,
89 	.open =			cyberjack_open,
90 	.close =		cyberjack_close,
91 	.write =		cyberjack_write,
92 	.write_room =		cyberjack_write_room,
93 	.read_int_callback =	cyberjack_read_int_callback,
94 	.read_bulk_callback =	cyberjack_read_bulk_callback,
95 	.write_bulk_callback =	cyberjack_write_bulk_callback,
96 };
97 
98 static struct usb_serial_driver * const serial_drivers[] = {
99 	&cyberjack_device, NULL
100 };
101 
102 struct cyberjack_private {
103 	spinlock_t	lock;		/* Lock for SMP */
104 	short		rdtodo;		/* Bytes still to read */
105 	unsigned char	wrbuf[5*64];	/* Buffer for collecting data to write */
106 	short		wrfilled;	/* Overall data size we already got */
107 	short		wrsent;		/* Data already sent */
108 };
109 
110 static int cyberjack_port_probe(struct usb_serial_port *port)
111 {
112 	struct cyberjack_private *priv;
113 	int result;
114 
115 	priv = kmalloc(sizeof(struct cyberjack_private), GFP_KERNEL);
116 	if (!priv)
117 		return -ENOMEM;
118 
119 	spin_lock_init(&priv->lock);
120 	priv->rdtodo = 0;
121 	priv->wrfilled = 0;
122 	priv->wrsent = 0;
123 
124 	usb_set_serial_port_data(port, priv);
125 
126 	result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
127 	if (result)
128 		dev_err(&port->dev, "usb_submit_urb(read int) failed\n");
129 
130 	return 0;
131 }
132 
133 static int cyberjack_port_remove(struct usb_serial_port *port)
134 {
135 	struct cyberjack_private *priv;
136 
137 	priv = usb_get_serial_port_data(port);
138 	kfree(priv);
139 
140 	return 0;
141 }
142 
143 static void cyberjack_disconnect(struct usb_serial *serial)
144 {
145 	int i;
146 
147 	for (i = 0; i < serial->num_ports; ++i)
148 		usb_kill_urb(serial->port[i]->interrupt_in_urb);
149 }
150 
151 static int  cyberjack_open(struct tty_struct *tty,
152 					struct usb_serial_port *port)
153 {
154 	struct cyberjack_private *priv;
155 	unsigned long flags;
156 	int result = 0;
157 
158 	dev_dbg(&port->dev, "%s - usb_clear_halt\n", __func__);
159 	usb_clear_halt(port->serial->dev, port->write_urb->pipe);
160 
161 	priv = usb_get_serial_port_data(port);
162 	spin_lock_irqsave(&priv->lock, flags);
163 	priv->rdtodo = 0;
164 	priv->wrfilled = 0;
165 	priv->wrsent = 0;
166 	spin_unlock_irqrestore(&priv->lock, flags);
167 
168 	return result;
169 }
170 
171 static void cyberjack_close(struct usb_serial_port *port)
172 {
173 	if (port->serial->dev) {
174 		/* shutdown any bulk reads that might be going on */
175 		usb_kill_urb(port->write_urb);
176 		usb_kill_urb(port->read_urb);
177 	}
178 }
179 
180 static int cyberjack_write(struct tty_struct *tty,
181 	struct usb_serial_port *port, const unsigned char *buf, int count)
182 {
183 	struct device *dev = &port->dev;
184 	struct cyberjack_private *priv = usb_get_serial_port_data(port);
185 	unsigned long flags;
186 	int result;
187 	int wrexpected;
188 
189 	if (count == 0) {
190 		dev_dbg(dev, "%s - write request of 0 bytes\n", __func__);
191 		return 0;
192 	}
193 
194 	if (!test_and_clear_bit(0, &port->write_urbs_free)) {
195 		dev_dbg(dev, "%s - already writing\n", __func__);
196 		return 0;
197 	}
198 
199 	spin_lock_irqsave(&priv->lock, flags);
200 
201 	if (count+priv->wrfilled > sizeof(priv->wrbuf)) {
202 		/* To much data for buffer. Reset buffer. */
203 		priv->wrfilled = 0;
204 		spin_unlock_irqrestore(&priv->lock, flags);
205 		set_bit(0, &port->write_urbs_free);
206 		return 0;
207 	}
208 
209 	/* Copy data */
210 	memcpy(priv->wrbuf + priv->wrfilled, buf, count);
211 
212 	usb_serial_debug_data(dev, __func__, count, priv->wrbuf + priv->wrfilled);
213 	priv->wrfilled += count;
214 
215 	if (priv->wrfilled >= 3) {
216 		wrexpected = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
217 		dev_dbg(dev, "%s - expected data: %d\n", __func__, wrexpected);
218 	} else
219 		wrexpected = sizeof(priv->wrbuf);
220 
221 	if (priv->wrfilled >= wrexpected) {
222 		/* We have enough data to begin transmission */
223 		int length;
224 
225 		dev_dbg(dev, "%s - transmitting data (frame 1)\n", __func__);
226 		length = (wrexpected > port->bulk_out_size) ?
227 					port->bulk_out_size : wrexpected;
228 
229 		memcpy(port->write_urb->transfer_buffer, priv->wrbuf, length);
230 		priv->wrsent = length;
231 
232 		/* set up our urb */
233 		port->write_urb->transfer_buffer_length = length;
234 
235 		/* send the data out the bulk port */
236 		result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
237 		if (result) {
238 			dev_err(&port->dev,
239 				"%s - failed submitting write urb, error %d",
240 				__func__, result);
241 			/* Throw away data. No better idea what to do with it. */
242 			priv->wrfilled = 0;
243 			priv->wrsent = 0;
244 			spin_unlock_irqrestore(&priv->lock, flags);
245 			set_bit(0, &port->write_urbs_free);
246 			return 0;
247 		}
248 
249 		dev_dbg(dev, "%s - priv->wrsent=%d\n", __func__, priv->wrsent);
250 		dev_dbg(dev, "%s - priv->wrfilled=%d\n", __func__, priv->wrfilled);
251 
252 		if (priv->wrsent >= priv->wrfilled) {
253 			dev_dbg(dev, "%s - buffer cleaned\n", __func__);
254 			memset(priv->wrbuf, 0, sizeof(priv->wrbuf));
255 			priv->wrfilled = 0;
256 			priv->wrsent = 0;
257 		}
258 	}
259 
260 	spin_unlock_irqrestore(&priv->lock, flags);
261 
262 	return count;
263 }
264 
265 static int cyberjack_write_room(struct tty_struct *tty)
266 {
267 	/* FIXME: .... */
268 	return CYBERJACK_LOCAL_BUF_SIZE;
269 }
270 
271 static void cyberjack_read_int_callback(struct urb *urb)
272 {
273 	struct usb_serial_port *port = urb->context;
274 	struct cyberjack_private *priv = usb_get_serial_port_data(port);
275 	struct device *dev = &port->dev;
276 	unsigned char *data = urb->transfer_buffer;
277 	int status = urb->status;
278 	int result;
279 
280 	/* the urb might have been killed. */
281 	if (status)
282 		return;
283 
284 	usb_serial_debug_data(dev, __func__, urb->actual_length, data);
285 
286 	/* React only to interrupts signaling a bulk_in transfer */
287 	if (urb->actual_length == 4 && data[0] == 0x01) {
288 		short old_rdtodo;
289 
290 		/* This is a announcement of coming bulk_ins. */
291 		unsigned short size = ((unsigned short)data[3]<<8)+data[2]+3;
292 
293 		spin_lock(&priv->lock);
294 
295 		old_rdtodo = priv->rdtodo;
296 
297 		if (old_rdtodo + size < old_rdtodo) {
298 			dev_dbg(dev, "To many bulk_in urbs to do.\n");
299 			spin_unlock(&priv->lock);
300 			goto resubmit;
301 		}
302 
303 		/* "+=" is probably more fault tollerant than "=" */
304 		priv->rdtodo += size;
305 
306 		dev_dbg(dev, "%s - rdtodo: %d\n", __func__, priv->rdtodo);
307 
308 		spin_unlock(&priv->lock);
309 
310 		if (!old_rdtodo) {
311 			result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
312 			if (result)
313 				dev_err(dev, "%s - failed resubmitting read urb, error %d\n",
314 					__func__, result);
315 			dev_dbg(dev, "%s - usb_submit_urb(read urb)\n", __func__);
316 		}
317 	}
318 
319 resubmit:
320 	result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
321 	if (result)
322 		dev_err(&port->dev, "usb_submit_urb(read int) failed\n");
323 	dev_dbg(dev, "%s - usb_submit_urb(int urb)\n", __func__);
324 }
325 
326 static void cyberjack_read_bulk_callback(struct urb *urb)
327 {
328 	struct usb_serial_port *port = urb->context;
329 	struct cyberjack_private *priv = usb_get_serial_port_data(port);
330 	struct device *dev = &port->dev;
331 	struct tty_struct *tty;
332 	unsigned char *data = urb->transfer_buffer;
333 	short todo;
334 	int result;
335 	int status = urb->status;
336 
337 	usb_serial_debug_data(dev, __func__, urb->actual_length, data);
338 	if (status) {
339 		dev_dbg(dev, "%s - nonzero read bulk status received: %d\n",
340 			__func__, status);
341 		return;
342 	}
343 
344 	tty = tty_port_tty_get(&port->port);
345 	if (!tty) {
346 		dev_dbg(dev, "%s - ignoring since device not open\n", __func__);
347 		return;
348 	}
349 	if (urb->actual_length) {
350 		tty_insert_flip_string(tty, data, urb->actual_length);
351 		tty_flip_buffer_push(tty);
352 	}
353 	tty_kref_put(tty);
354 
355 	spin_lock(&priv->lock);
356 
357 	/* Reduce urbs to do by one. */
358 	priv->rdtodo -= urb->actual_length;
359 	/* Just to be sure */
360 	if (priv->rdtodo < 0)
361 		priv->rdtodo = 0;
362 	todo = priv->rdtodo;
363 
364 	spin_unlock(&priv->lock);
365 
366 	dev_dbg(dev, "%s - rdtodo: %d\n", __func__, todo);
367 
368 	/* Continue to read if we have still urbs to do. */
369 	if (todo /* || (urb->actual_length==port->bulk_in_endpointAddress)*/) {
370 		result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
371 		if (result)
372 			dev_err(dev, "%s - failed resubmitting read urb, error %d\n",
373 				__func__, result);
374 		dev_dbg(dev, "%s - usb_submit_urb(read urb)\n", __func__);
375 	}
376 }
377 
378 static void cyberjack_write_bulk_callback(struct urb *urb)
379 {
380 	struct usb_serial_port *port = urb->context;
381 	struct cyberjack_private *priv = usb_get_serial_port_data(port);
382 	struct device *dev = &port->dev;
383 	int status = urb->status;
384 
385 	set_bit(0, &port->write_urbs_free);
386 	if (status) {
387 		dev_dbg(dev, "%s - nonzero write bulk status received: %d\n",
388 			__func__, status);
389 		return;
390 	}
391 
392 	spin_lock(&priv->lock);
393 
394 	/* only do something if we have more data to send */
395 	if (priv->wrfilled) {
396 		int length, blksize, result;
397 
398 		dev_dbg(dev, "%s - transmitting data (frame n)\n", __func__);
399 
400 		length = ((priv->wrfilled - priv->wrsent) > port->bulk_out_size) ?
401 			port->bulk_out_size : (priv->wrfilled - priv->wrsent);
402 
403 		memcpy(port->write_urb->transfer_buffer,
404 					priv->wrbuf + priv->wrsent, length);
405 		priv->wrsent += length;
406 
407 		/* set up our urb */
408 		port->write_urb->transfer_buffer_length = length;
409 
410 		/* send the data out the bulk port */
411 		result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
412 		if (result) {
413 			dev_err(dev, "%s - failed submitting write urb, error %d\n",
414 				__func__, result);
415 			/* Throw away data. No better idea what to do with it. */
416 			priv->wrfilled = 0;
417 			priv->wrsent = 0;
418 			goto exit;
419 		}
420 
421 		dev_dbg(dev, "%s - priv->wrsent=%d\n", __func__, priv->wrsent);
422 		dev_dbg(dev, "%s - priv->wrfilled=%d\n", __func__, priv->wrfilled);
423 
424 		blksize = ((int)priv->wrbuf[2]<<8)+priv->wrbuf[1]+3;
425 
426 		if (priv->wrsent >= priv->wrfilled ||
427 					priv->wrsent >= blksize) {
428 			dev_dbg(dev, "%s - buffer cleaned\n", __func__);
429 			memset(priv->wrbuf, 0, sizeof(priv->wrbuf));
430 			priv->wrfilled = 0;
431 			priv->wrsent = 0;
432 		}
433 	}
434 
435 exit:
436 	spin_unlock(&priv->lock);
437 	usb_serial_port_softint(port);
438 }
439 
440 module_usb_serial_driver(serial_drivers, id_table);
441 
442 MODULE_AUTHOR(DRIVER_AUTHOR);
443 MODULE_DESCRIPTION(DRIVER_DESC);
444 MODULE_VERSION(DRIVER_VERSION);
445 MODULE_LICENSE("GPL");
446