2 * KOBIL USB Smart Card Terminal Driver
4 * Copyright (C) 2002 KOBIL Systems GmbH
5 * Author: Thomas Wahrenbruch
7 * Contact: linuxusb@kobil.de
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.
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.
18 * Thanks to Greg Kroah-Hartman (greg@kroah.com) for his help and
21 * Supported readers: USB TWIN, KAAN Standard Plus and SecOVID Reader Plus
22 * (Adapter K), B1 Professional and KAAN Professional (Adapter B)
25 * Fix bug with P'n'P readers
28 * Add support for KAAN SIM
38 #include <linux/kernel.h>
39 #include <linux/errno.h>
40 #include <linux/init.h>
41 #include <linux/slab.h>
42 #include <linux/tty.h>
43 #include <linux/tty_driver.h>
44 #include <linux/tty_flip.h>
45 #include <linux/module.h>
46 #include <linux/spinlock.h>
47 #include <linux/uaccess.h>
48 #include <linux/usb.h>
49 #include <linux/usb/serial.h>
50 #include <linux/ioctl.h>
51 #include "kobil_sct.h"
55 /* Version Information */
56 #define DRIVER_VERSION "21/05/2004"
57 #define DRIVER_AUTHOR "KOBIL Systems GmbH - http://www.kobil.com"
58 #define DRIVER_DESC "KOBIL USB Smart Card Terminal Driver (experimental)"
60 #define KOBIL_VENDOR_ID 0x0D46
61 #define KOBIL_ADAPTER_B_PRODUCT_ID 0x2011
62 #define KOBIL_ADAPTER_K_PRODUCT_ID 0x2012
63 #define KOBIL_USBTWIN_PRODUCT_ID 0x0078
64 #define KOBIL_KAAN_SIM_PRODUCT_ID 0x0081
66 #define KOBIL_TIMEOUT 500
67 #define KOBIL_BUF_LENGTH 300
70 /* Function prototypes */
71 static int kobil_startup(struct usb_serial *serial);
72 static void kobil_shutdown(struct usb_serial *serial);
73 static int kobil_open(struct tty_struct *tty,
74 struct usb_serial_port *port, struct file *filp);
75 static void kobil_close(struct tty_struct *tty, struct usb_serial_port *port,
77 static int kobil_write(struct tty_struct *tty, struct usb_serial_port *port,
78 const unsigned char *buf, int count);
79 static int kobil_write_room(struct tty_struct *tty);
80 static int kobil_ioctl(struct tty_struct *tty, struct file *file,
81 unsigned int cmd, unsigned long arg);
82 static int kobil_tiocmget(struct tty_struct *tty, struct file *file);
83 static int kobil_tiocmset(struct tty_struct *tty, struct file *file,
84 unsigned int set, unsigned int clear);
85 static void kobil_read_int_callback(struct urb *urb);
86 static void kobil_write_callback(struct urb *purb);
87 static void kobil_set_termios(struct tty_struct *tty,
88 struct usb_serial_port *port, struct ktermios *old);
91 static struct usb_device_id id_table [] = {
92 { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_ADAPTER_B_PRODUCT_ID) },
93 { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_ADAPTER_K_PRODUCT_ID) },
94 { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_USBTWIN_PRODUCT_ID) },
95 { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_KAAN_SIM_PRODUCT_ID) },
96 { } /* Terminating entry */
100 MODULE_DEVICE_TABLE(usb, id_table);
102 static struct usb_driver kobil_driver = {
104 .probe = usb_serial_probe,
105 .disconnect = usb_serial_disconnect,
106 .id_table = id_table,
111 static struct usb_serial_driver kobil_device = {
113 .owner = THIS_MODULE,
116 .description = "KOBIL USB smart card terminal",
117 .usb_driver = &kobil_driver,
118 .id_table = id_table,
120 .attach = kobil_startup,
121 .shutdown = kobil_shutdown,
122 .ioctl = kobil_ioctl,
123 .set_termios = kobil_set_termios,
124 .tiocmget = kobil_tiocmget,
125 .tiocmset = kobil_tiocmset,
127 .close = kobil_close,
128 .write = kobil_write,
129 .write_room = kobil_write_room,
130 .read_int_callback = kobil_read_int_callback,
134 struct kobil_private {
135 int write_int_endpoint_address;
136 int read_int_endpoint_address;
137 unsigned char buf[KOBIL_BUF_LENGTH]; /* buffer for the APDU to send */
138 int filled; /* index of the last char in buf */
139 int cur_pos; /* index of the next char to send in buf */
144 static int kobil_startup(struct usb_serial *serial)
147 struct kobil_private *priv;
148 struct usb_device *pdev;
149 struct usb_host_config *actconfig;
150 struct usb_interface *interface;
151 struct usb_host_interface *altsetting;
152 struct usb_host_endpoint *endpoint;
154 priv = kmalloc(sizeof(struct kobil_private), GFP_KERNEL);
160 priv->device_type = le16_to_cpu(serial->dev->descriptor.idProduct);
162 switch (priv->device_type) {
163 case KOBIL_ADAPTER_B_PRODUCT_ID:
164 printk(KERN_DEBUG "KOBIL B1 PRO / KAAN PRO detected\n");
166 case KOBIL_ADAPTER_K_PRODUCT_ID:
168 "KOBIL KAAN Standard Plus / SecOVID Reader Plus detected\n");
170 case KOBIL_USBTWIN_PRODUCT_ID:
171 printk(KERN_DEBUG "KOBIL USBTWIN detected\n");
173 case KOBIL_KAAN_SIM_PRODUCT_ID:
174 printk(KERN_DEBUG "KOBIL KAAN SIM detected\n");
177 usb_set_serial_port_data(serial->port[0], priv);
179 /* search for the necessary endpoints */
181 actconfig = pdev->actconfig;
182 interface = actconfig->interface[0];
183 altsetting = interface->cur_altsetting;
184 endpoint = altsetting->endpoint;
186 for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
187 endpoint = &altsetting->endpoint[i];
188 if (usb_endpoint_is_int_out(&endpoint->desc)) {
189 dbg("%s Found interrupt out endpoint. Address: %d",
190 __func__, endpoint->desc.bEndpointAddress);
191 priv->write_int_endpoint_address =
192 endpoint->desc.bEndpointAddress;
194 if (usb_endpoint_is_int_in(&endpoint->desc)) {
195 dbg("%s Found interrupt in endpoint. Address: %d",
196 __func__, endpoint->desc.bEndpointAddress);
197 priv->read_int_endpoint_address =
198 endpoint->desc.bEndpointAddress;
205 static void kobil_shutdown(struct usb_serial *serial)
208 dbg("%s - port %d", __func__, serial->port[0]->number);
210 for (i = 0; i < serial->num_ports; ++i) {
211 while (serial->port[i]->port.count > 0)
212 kobil_close(NULL, serial->port[i], NULL);
213 kfree(usb_get_serial_port_data(serial->port[i]));
214 usb_set_serial_port_data(serial->port[i], NULL);
219 static int kobil_open(struct tty_struct *tty,
220 struct usb_serial_port *port, struct file *filp)
223 struct kobil_private *priv;
224 unsigned char *transfer_buffer;
225 int transfer_buffer_length = 8;
226 int write_urb_transfer_buffer_length = 8;
228 dbg("%s - port %d", __func__, port->number);
229 priv = usb_get_serial_port_data(port);
231 /* someone sets the dev to 0 if the close method has been called */
232 port->interrupt_in_urb->dev = port->serial->dev;
235 /* force low_latency on so that our tty_push actually forces
236 * the data through, otherwise it is scheduled, and with high
237 * data rates (like with OHCI) data can get lost.
240 tty->low_latency = 1;
242 /* Default to echo off and other sane device settings */
243 tty->termios->c_lflag = 0;
244 tty->termios->c_lflag &= ~(ISIG | ICANON | ECHO | IEXTEN |
246 tty->termios->c_iflag = IGNBRK | IGNPAR | IXOFF;
247 /* do NOT translate CR to CR-NL (0x0A -> 0x0A 0x0D) */
248 tty->termios->c_oflag &= ~ONLCR;
250 /* allocate memory for transfer buffer */
251 transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
252 if (!transfer_buffer)
255 /* allocate write_urb */
256 if (!port->write_urb) {
257 dbg("%s - port %d Allocating port->write_urb",
258 __func__, port->number);
259 port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
260 if (!port->write_urb) {
261 dbg("%s - port %d usb_alloc_urb failed",
262 __func__, port->number);
263 kfree(transfer_buffer);
268 /* allocate memory for write_urb transfer buffer */
269 port->write_urb->transfer_buffer =
270 kmalloc(write_urb_transfer_buffer_length, GFP_KERNEL);
271 if (!port->write_urb->transfer_buffer) {
272 kfree(transfer_buffer);
273 usb_free_urb(port->write_urb);
274 port->write_urb = NULL;
278 /* get hardware version */
279 result = usb_control_msg(port->serial->dev,
280 usb_rcvctrlpipe(port->serial->dev, 0),
281 SUSBCRequest_GetMisc,
282 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
283 SUSBCR_MSC_GetHWVersion,
286 transfer_buffer_length,
289 dbg("%s - port %d Send get_HW_version URB returns: %i",
290 __func__, port->number, result);
291 dbg("Harware version: %i.%i.%i",
292 transfer_buffer[0], transfer_buffer[1], transfer_buffer[2]);
294 /* get firmware version */
295 result = usb_control_msg(port->serial->dev,
296 usb_rcvctrlpipe(port->serial->dev, 0),
297 SUSBCRequest_GetMisc,
298 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
299 SUSBCR_MSC_GetFWVersion,
302 transfer_buffer_length,
305 dbg("%s - port %d Send get_FW_version URB returns: %i",
306 __func__, port->number, result);
307 dbg("Firmware version: %i.%i.%i",
308 transfer_buffer[0], transfer_buffer[1], transfer_buffer[2]);
310 if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID ||
311 priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) {
312 /* Setting Baudrate, Parity and Stopbits */
313 result = usb_control_msg(port->serial->dev,
314 usb_rcvctrlpipe(port->serial->dev, 0),
315 SUSBCRequest_SetBaudRateParityAndStopBits,
316 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
317 SUSBCR_SBR_9600 | SUSBCR_SPASB_EvenParity |
318 SUSBCR_SPASB_1StopBit,
324 dbg("%s - port %d Send set_baudrate URB returns: %i",
325 __func__, port->number, result);
327 /* reset all queues */
328 result = usb_control_msg(port->serial->dev,
329 usb_rcvctrlpipe(port->serial->dev, 0),
331 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
332 SUSBCR_MSC_ResetAllQueues,
338 dbg("%s - port %d Send reset_all_queues URB returns: %i",
339 __func__, port->number, result);
341 if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID ||
342 priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID ||
343 priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID) {
344 /* start reading (Adapter B 'cause PNP string) */
345 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
346 dbg("%s - port %d Send read URB returns: %i",
347 __func__, port->number, result);
350 kfree(transfer_buffer);
355 static void kobil_close(struct tty_struct *tty,
356 struct usb_serial_port *port, struct file *filp)
358 dbg("%s - port %d", __func__, port->number);
360 if (port->write_urb) {
361 usb_kill_urb(port->write_urb);
362 usb_free_urb(port->write_urb);
363 port->write_urb = NULL;
365 usb_kill_urb(port->interrupt_in_urb);
369 static void kobil_read_int_callback(struct urb *urb)
372 struct usb_serial_port *port = urb->context;
373 struct tty_struct *tty;
374 unsigned char *data = urb->transfer_buffer;
375 int status = urb->status;
376 /* char *dbg_data; */
378 dbg("%s - port %d", __func__, port->number);
381 dbg("%s - port %d Read int status not zero: %d",
382 __func__, port->number, status);
386 tty = port->port.tty;
387 if (urb->actual_length) {
391 dbg_data = kzalloc((3 * purb->actual_length + 10)
392 * sizeof(char), GFP_KERNEL);
396 for (i = 0; i < purb->actual_length; i++) {
397 sprintf(dbg_data +3*i, "%02X ", data[i]);
399 dbg(" <-- %s", dbg_data);
404 tty_buffer_request_room(tty, urb->actual_length);
405 tty_insert_flip_string(tty, data, urb->actual_length);
406 tty_flip_buffer_push(tty);
408 /* someone sets the dev to 0 if the close method has been called */
409 port->interrupt_in_urb->dev = port->serial->dev;
411 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
412 dbg("%s - port %d Send read URB returns: %i",
413 __func__, port->number, result);
417 static void kobil_write_callback(struct urb *purb)
422 static int kobil_write(struct tty_struct *tty, struct usb_serial_port *port,
423 const unsigned char *buf, int count)
428 struct kobil_private *priv;
431 dbg("%s - port %d write request of 0 bytes",
432 __func__, port->number);
436 priv = usb_get_serial_port_data(port);
438 if (count > (KOBIL_BUF_LENGTH - priv->filled)) {
439 dbg("%s - port %d Error: write request bigger than buffer size", __func__, port->number);
443 /* Copy data to buffer */
444 memcpy(priv->buf + priv->filled, buf, count);
445 usb_serial_debug_data(debug, &port->dev, __func__, count,
446 priv->buf + priv->filled);
447 priv->filled = priv->filled + count;
449 /* only send complete block. TWIN, KAAN SIM and adapter K
450 use the same protocol. */
451 if (((priv->device_type != KOBIL_ADAPTER_B_PRODUCT_ID) && (priv->filled > 2) && (priv->filled >= (priv->buf[1] + 3))) ||
452 ((priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) && (priv->filled > 3) && (priv->filled >= (priv->buf[2] + 4)))) {
453 /* stop reading (except TWIN and KAAN SIM) */
454 if ((priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID)
455 || (priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID))
456 usb_kill_urb(port->interrupt_in_urb);
458 todo = priv->filled - priv->cur_pos;
461 /* max 8 byte in one urb (endpoint size) */
462 length = (todo < 8) ? todo : 8;
463 /* copy data to transfer buffer */
464 memcpy(port->write_urb->transfer_buffer,
465 priv->buf + priv->cur_pos, length);
466 usb_fill_int_urb(port->write_urb,
468 usb_sndintpipe(port->serial->dev,
469 priv->write_int_endpoint_address),
470 port->write_urb->transfer_buffer,
472 kobil_write_callback,
477 priv->cur_pos = priv->cur_pos + length;
478 result = usb_submit_urb(port->write_urb, GFP_NOIO);
479 dbg("%s - port %d Send write URB returns: %i",
480 __func__, port->number, result);
481 todo = priv->filled - priv->cur_pos;
490 /* someone sets the dev to 0 if the close method
492 port->interrupt_in_urb->dev = port->serial->dev;
494 /* start reading (except TWIN and KAAN SIM) */
495 if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID ||
496 priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) {
497 /* someone sets the dev to 0 if the close method has
499 port->interrupt_in_urb->dev = port->serial->dev;
501 result = usb_submit_urb(port->interrupt_in_urb,
503 dbg("%s - port %d Send read URB returns: %i",
504 __func__, port->number, result);
511 static int kobil_write_room(struct tty_struct *tty)
513 /* dbg("%s - port %d", __func__, port->number); */
519 static int kobil_tiocmget(struct tty_struct *tty, struct file *file)
521 struct usb_serial_port *port = tty->driver_data;
522 struct kobil_private *priv;
524 unsigned char *transfer_buffer;
525 int transfer_buffer_length = 8;
527 priv = usb_get_serial_port_data(port);
528 if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID
529 || priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID) {
530 /* This device doesn't support ioctl calls */
534 /* allocate memory for transfer buffer */
535 transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
536 if (!transfer_buffer)
539 result = usb_control_msg(port->serial->dev,
540 usb_rcvctrlpipe(port->serial->dev, 0),
541 SUSBCRequest_GetStatusLineState,
542 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
546 transfer_buffer_length,
549 dbg("%s - port %d Send get_status_line_state URB returns: %i. Statusline: %02x",
550 __func__, port->number, result, transfer_buffer[0]);
553 if ((transfer_buffer[0] & SUSBCR_GSL_DSR) != 0)
555 kfree(transfer_buffer);
559 static int kobil_tiocmset(struct tty_struct *tty, struct file *file,
560 unsigned int set, unsigned int clear)
562 struct usb_serial_port *port = tty->driver_data;
563 struct kobil_private *priv;
567 unsigned char *transfer_buffer;
568 int transfer_buffer_length = 8;
570 /* FIXME: locking ? */
571 priv = usb_get_serial_port_data(port);
572 if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID
573 || priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID) {
574 /* This device doesn't support ioctl calls */
578 /* allocate memory for transfer buffer */
579 transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
580 if (!transfer_buffer)
587 if (clear & TIOCM_RTS)
589 if (clear & TIOCM_DTR)
592 if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) {
594 dbg("%s - port %d Setting DTR",
595 __func__, port->number);
597 dbg("%s - port %d Clearing DTR",
598 __func__, port->number);
599 result = usb_control_msg(port->serial->dev,
600 usb_rcvctrlpipe(port->serial->dev, 0),
601 SUSBCRequest_SetStatusLinesOrQueues,
602 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
603 ((dtr != 0) ? SUSBCR_SSL_SETDTR : SUSBCR_SSL_CLRDTR),
610 dbg("%s - port %d Setting RTS",
611 __func__, port->number);
613 dbg("%s - port %d Clearing RTS",
614 __func__, port->number);
615 result = usb_control_msg(port->serial->dev,
616 usb_rcvctrlpipe(port->serial->dev, 0),
617 SUSBCRequest_SetStatusLinesOrQueues,
618 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
619 ((rts != 0) ? SUSBCR_SSL_SETRTS : SUSBCR_SSL_CLRRTS),
625 dbg("%s - port %d Send set_status_line URB returns: %i",
626 __func__, port->number, result);
627 kfree(transfer_buffer);
628 return (result < 0) ? result : 0;
631 static void kobil_set_termios(struct tty_struct *tty,
632 struct usb_serial_port *port, struct ktermios *old)
634 struct kobil_private *priv;
636 unsigned short urb_val = 0;
637 int c_cflag = tty->termios->c_cflag;
641 priv = usb_get_serial_port_data(port);
642 if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID ||
643 priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID) {
644 /* This device doesn't support ioctl calls */
645 *tty->termios = *old;
649 speed = tty_get_baud_rate(tty);
652 urb_val = SUSBCR_SBR_1200;
657 urb_val = SUSBCR_SBR_9600;
660 urb_val |= (c_cflag & CSTOPB) ? SUSBCR_SPASB_2StopBits :
661 SUSBCR_SPASB_1StopBit;
663 settings = kzalloc(50, GFP_KERNEL);
667 sprintf(settings, "%d ", speed);
669 if (c_cflag & PARENB) {
670 if (c_cflag & PARODD) {
671 urb_val |= SUSBCR_SPASB_OddParity;
672 strcat(settings, "Odd Parity");
674 urb_val |= SUSBCR_SPASB_EvenParity;
675 strcat(settings, "Even Parity");
678 urb_val |= SUSBCR_SPASB_NoParity;
679 strcat(settings, "No Parity");
681 tty->termios->c_cflag &= ~CMSPAR;
682 tty_encode_baud_rate(tty, speed, speed);
684 result = usb_control_msg(port->serial->dev,
685 usb_rcvctrlpipe(port->serial->dev, 0),
686 SUSBCRequest_SetBaudRateParityAndStopBits,
687 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
697 static int kobil_ioctl(struct tty_struct *tty, struct file *file,
698 unsigned int cmd, unsigned long arg)
700 struct usb_serial_port *port = tty->driver_data;
701 struct kobil_private *priv = usb_get_serial_port_data(port);
702 unsigned char *transfer_buffer;
703 int transfer_buffer_length = 8;
706 if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID ||
707 priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID)
708 /* This device doesn't support ioctl calls */
713 transfer_buffer = kmalloc(transfer_buffer_length, GFP_KERNEL);
714 if (!transfer_buffer)
717 result = usb_control_msg(port->serial->dev,
718 usb_rcvctrlpipe(port->serial->dev, 0),
720 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
721 SUSBCR_MSC_ResetAllQueues,
723 NULL, /* transfer_buffer, */
728 dbg("%s - port %d Send reset_all_queues (FLUSH) URB returns: %i", __func__, port->number, result);
729 kfree(transfer_buffer);
730 return (result < 0) ? -EIO: 0;
736 static int __init kobil_init(void)
739 retval = usb_serial_register(&kobil_device);
741 goto failed_usb_serial_register;
742 retval = usb_register(&kobil_driver);
744 goto failed_usb_register;
746 info(DRIVER_VERSION " " DRIVER_AUTHOR);
751 usb_serial_deregister(&kobil_device);
752 failed_usb_serial_register:
757 static void __exit kobil_exit(void)
759 usb_deregister(&kobil_driver);
760 usb_serial_deregister(&kobil_device);
763 module_init(kobil_init);
764 module_exit(kobil_exit);
766 MODULE_AUTHOR(DRIVER_AUTHOR);
767 MODULE_DESCRIPTION(DRIVER_DESC);
768 MODULE_LICENSE("GPL");
770 module_param(debug, bool, S_IRUGO | S_IWUSR);
771 MODULE_PARM_DESC(debug, "Debug enabled or not");