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_release(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 usb_serial_port *port);
76 static int kobil_write(struct tty_struct *tty, struct usb_serial_port *port,
77 const unsigned char *buf, int count);
78 static int kobil_write_room(struct tty_struct *tty);
79 static int kobil_ioctl(struct tty_struct *tty, struct file *file,
80 unsigned int cmd, unsigned long arg);
81 static int kobil_tiocmget(struct tty_struct *tty, struct file *file);
82 static int kobil_tiocmset(struct tty_struct *tty, struct file *file,
83 unsigned int set, unsigned int clear);
84 static void kobil_read_int_callback(struct urb *urb);
85 static void kobil_write_callback(struct urb *purb);
86 static void kobil_set_termios(struct tty_struct *tty,
87 struct usb_serial_port *port, struct ktermios *old);
90 static struct usb_device_id id_table [] = {
91 { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_ADAPTER_B_PRODUCT_ID) },
92 { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_ADAPTER_K_PRODUCT_ID) },
93 { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_USBTWIN_PRODUCT_ID) },
94 { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_KAAN_SIM_PRODUCT_ID) },
95 { } /* Terminating entry */
99 MODULE_DEVICE_TABLE(usb, id_table);
101 static struct usb_driver kobil_driver = {
103 .probe = usb_serial_probe,
104 .disconnect = usb_serial_disconnect,
105 .id_table = id_table,
110 static struct usb_serial_driver kobil_device = {
112 .owner = THIS_MODULE,
115 .description = "KOBIL USB smart card terminal",
116 .usb_driver = &kobil_driver,
117 .id_table = id_table,
119 .attach = kobil_startup,
120 .release = kobil_release,
121 .ioctl = kobil_ioctl,
122 .set_termios = kobil_set_termios,
123 .tiocmget = kobil_tiocmget,
124 .tiocmset = kobil_tiocmset,
126 .close = kobil_close,
127 .write = kobil_write,
128 .write_room = kobil_write_room,
129 .read_int_callback = kobil_read_int_callback,
133 struct kobil_private {
134 int write_int_endpoint_address;
135 int read_int_endpoint_address;
136 unsigned char buf[KOBIL_BUF_LENGTH]; /* buffer for the APDU to send */
137 int filled; /* index of the last char in buf */
138 int cur_pos; /* index of the next char to send in buf */
143 static int kobil_startup(struct usb_serial *serial)
146 struct kobil_private *priv;
147 struct usb_device *pdev;
148 struct usb_host_config *actconfig;
149 struct usb_interface *interface;
150 struct usb_host_interface *altsetting;
151 struct usb_host_endpoint *endpoint;
153 priv = kmalloc(sizeof(struct kobil_private), GFP_KERNEL);
159 priv->device_type = le16_to_cpu(serial->dev->descriptor.idProduct);
161 switch (priv->device_type) {
162 case KOBIL_ADAPTER_B_PRODUCT_ID:
163 printk(KERN_DEBUG "KOBIL B1 PRO / KAAN PRO detected\n");
165 case KOBIL_ADAPTER_K_PRODUCT_ID:
167 "KOBIL KAAN Standard Plus / SecOVID Reader Plus detected\n");
169 case KOBIL_USBTWIN_PRODUCT_ID:
170 printk(KERN_DEBUG "KOBIL USBTWIN detected\n");
172 case KOBIL_KAAN_SIM_PRODUCT_ID:
173 printk(KERN_DEBUG "KOBIL KAAN SIM detected\n");
176 usb_set_serial_port_data(serial->port[0], priv);
178 /* search for the necessary endpoints */
180 actconfig = pdev->actconfig;
181 interface = actconfig->interface[0];
182 altsetting = interface->cur_altsetting;
183 endpoint = altsetting->endpoint;
185 for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
186 endpoint = &altsetting->endpoint[i];
187 if (usb_endpoint_is_int_out(&endpoint->desc)) {
188 dbg("%s Found interrupt out endpoint. Address: %d",
189 __func__, endpoint->desc.bEndpointAddress);
190 priv->write_int_endpoint_address =
191 endpoint->desc.bEndpointAddress;
193 if (usb_endpoint_is_int_in(&endpoint->desc)) {
194 dbg("%s Found interrupt in endpoint. Address: %d",
195 __func__, endpoint->desc.bEndpointAddress);
196 priv->read_int_endpoint_address =
197 endpoint->desc.bEndpointAddress;
204 static void kobil_release(struct usb_serial *serial)
207 dbg("%s - port %d", __func__, serial->port[0]->number);
209 for (i = 0; i < serial->num_ports; ++i)
210 kfree(usb_get_serial_port_data(serial->port[i]));
214 static int kobil_open(struct tty_struct *tty,
215 struct usb_serial_port *port, struct file *filp)
218 struct kobil_private *priv;
219 unsigned char *transfer_buffer;
220 int transfer_buffer_length = 8;
221 int write_urb_transfer_buffer_length = 8;
223 dbg("%s - port %d", __func__, port->number);
224 priv = usb_get_serial_port_data(port);
226 /* someone sets the dev to 0 if the close method has been called */
227 port->interrupt_in_urb->dev = port->serial->dev;
231 /* Default to echo off and other sane device settings */
232 tty->termios->c_lflag = 0;
233 tty->termios->c_lflag &= ~(ISIG | ICANON | ECHO | IEXTEN |
235 tty->termios->c_iflag = IGNBRK | IGNPAR | IXOFF;
236 /* do NOT translate CR to CR-NL (0x0A -> 0x0A 0x0D) */
237 tty->termios->c_oflag &= ~ONLCR;
239 /* allocate memory for transfer buffer */
240 transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
241 if (!transfer_buffer)
244 /* allocate write_urb */
245 if (!port->write_urb) {
246 dbg("%s - port %d Allocating port->write_urb",
247 __func__, port->number);
248 port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
249 if (!port->write_urb) {
250 dbg("%s - port %d usb_alloc_urb failed",
251 __func__, port->number);
252 kfree(transfer_buffer);
257 /* allocate memory for write_urb transfer buffer */
258 port->write_urb->transfer_buffer =
259 kmalloc(write_urb_transfer_buffer_length, GFP_KERNEL);
260 if (!port->write_urb->transfer_buffer) {
261 kfree(transfer_buffer);
262 usb_free_urb(port->write_urb);
263 port->write_urb = NULL;
267 /* get hardware version */
268 result = usb_control_msg(port->serial->dev,
269 usb_rcvctrlpipe(port->serial->dev, 0),
270 SUSBCRequest_GetMisc,
271 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
272 SUSBCR_MSC_GetHWVersion,
275 transfer_buffer_length,
278 dbg("%s - port %d Send get_HW_version URB returns: %i",
279 __func__, port->number, result);
280 dbg("Harware version: %i.%i.%i",
281 transfer_buffer[0], transfer_buffer[1], transfer_buffer[2]);
283 /* get firmware version */
284 result = usb_control_msg(port->serial->dev,
285 usb_rcvctrlpipe(port->serial->dev, 0),
286 SUSBCRequest_GetMisc,
287 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
288 SUSBCR_MSC_GetFWVersion,
291 transfer_buffer_length,
294 dbg("%s - port %d Send get_FW_version URB returns: %i",
295 __func__, port->number, result);
296 dbg("Firmware version: %i.%i.%i",
297 transfer_buffer[0], transfer_buffer[1], transfer_buffer[2]);
299 if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID ||
300 priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) {
301 /* Setting Baudrate, Parity and Stopbits */
302 result = usb_control_msg(port->serial->dev,
303 usb_rcvctrlpipe(port->serial->dev, 0),
304 SUSBCRequest_SetBaudRateParityAndStopBits,
305 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
306 SUSBCR_SBR_9600 | SUSBCR_SPASB_EvenParity |
307 SUSBCR_SPASB_1StopBit,
313 dbg("%s - port %d Send set_baudrate URB returns: %i",
314 __func__, port->number, result);
316 /* reset all queues */
317 result = usb_control_msg(port->serial->dev,
318 usb_rcvctrlpipe(port->serial->dev, 0),
320 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
321 SUSBCR_MSC_ResetAllQueues,
327 dbg("%s - port %d Send reset_all_queues URB returns: %i",
328 __func__, port->number, result);
330 if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID ||
331 priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID ||
332 priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID) {
333 /* start reading (Adapter B 'cause PNP string) */
334 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
335 dbg("%s - port %d Send read URB returns: %i",
336 __func__, port->number, result);
339 kfree(transfer_buffer);
344 static void kobil_close(struct usb_serial_port *port)
346 dbg("%s - port %d", __func__, port->number);
348 /* FIXME: Add rts/dtr methods */
349 if (port->write_urb) {
350 usb_kill_urb(port->write_urb);
351 usb_free_urb(port->write_urb);
352 port->write_urb = NULL;
354 usb_kill_urb(port->interrupt_in_urb);
358 static void kobil_read_int_callback(struct urb *urb)
361 struct usb_serial_port *port = urb->context;
362 struct tty_struct *tty;
363 unsigned char *data = urb->transfer_buffer;
364 int status = urb->status;
365 /* char *dbg_data; */
367 dbg("%s - port %d", __func__, port->number);
370 dbg("%s - port %d Read int status not zero: %d",
371 __func__, port->number, status);
375 tty = tty_port_tty_get(&port->port);
376 if (urb->actual_length) {
380 dbg_data = kzalloc((3 * purb->actual_length + 10)
381 * sizeof(char), GFP_KERNEL);
385 for (i = 0; i < purb->actual_length; i++) {
386 sprintf(dbg_data +3*i, "%02X ", data[i]);
388 dbg(" <-- %s", dbg_data);
393 tty_buffer_request_room(tty, urb->actual_length);
394 tty_insert_flip_string(tty, data, urb->actual_length);
395 tty_flip_buffer_push(tty);
398 /* someone sets the dev to 0 if the close method has been called */
399 port->interrupt_in_urb->dev = port->serial->dev;
401 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
402 dbg("%s - port %d Send read URB returns: %i",
403 __func__, port->number, result);
407 static void kobil_write_callback(struct urb *purb)
412 static int kobil_write(struct tty_struct *tty, struct usb_serial_port *port,
413 const unsigned char *buf, int count)
418 struct kobil_private *priv;
421 dbg("%s - port %d write request of 0 bytes",
422 __func__, port->number);
426 priv = usb_get_serial_port_data(port);
428 if (count > (KOBIL_BUF_LENGTH - priv->filled)) {
429 dbg("%s - port %d Error: write request bigger than buffer size", __func__, port->number);
433 /* Copy data to buffer */
434 memcpy(priv->buf + priv->filled, buf, count);
435 usb_serial_debug_data(debug, &port->dev, __func__, count,
436 priv->buf + priv->filled);
437 priv->filled = priv->filled + count;
439 /* only send complete block. TWIN, KAAN SIM and adapter K
440 use the same protocol. */
441 if (((priv->device_type != KOBIL_ADAPTER_B_PRODUCT_ID) && (priv->filled > 2) && (priv->filled >= (priv->buf[1] + 3))) ||
442 ((priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) && (priv->filled > 3) && (priv->filled >= (priv->buf[2] + 4)))) {
443 /* stop reading (except TWIN and KAAN SIM) */
444 if ((priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID)
445 || (priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID))
446 usb_kill_urb(port->interrupt_in_urb);
448 todo = priv->filled - priv->cur_pos;
451 /* max 8 byte in one urb (endpoint size) */
452 length = (todo < 8) ? todo : 8;
453 /* copy data to transfer buffer */
454 memcpy(port->write_urb->transfer_buffer,
455 priv->buf + priv->cur_pos, length);
456 usb_fill_int_urb(port->write_urb,
458 usb_sndintpipe(port->serial->dev,
459 priv->write_int_endpoint_address),
460 port->write_urb->transfer_buffer,
462 kobil_write_callback,
467 priv->cur_pos = priv->cur_pos + length;
468 result = usb_submit_urb(port->write_urb, GFP_NOIO);
469 dbg("%s - port %d Send write URB returns: %i",
470 __func__, port->number, result);
471 todo = priv->filled - priv->cur_pos;
480 /* someone sets the dev to 0 if the close method
482 port->interrupt_in_urb->dev = port->serial->dev;
484 /* start reading (except TWIN and KAAN SIM) */
485 if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID ||
486 priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) {
487 /* someone sets the dev to 0 if the close method has
489 port->interrupt_in_urb->dev = port->serial->dev;
491 result = usb_submit_urb(port->interrupt_in_urb,
493 dbg("%s - port %d Send read URB returns: %i",
494 __func__, port->number, result);
501 static int kobil_write_room(struct tty_struct *tty)
503 /* dbg("%s - port %d", __func__, port->number); */
509 static int kobil_tiocmget(struct tty_struct *tty, struct file *file)
511 struct usb_serial_port *port = tty->driver_data;
512 struct kobil_private *priv;
514 unsigned char *transfer_buffer;
515 int transfer_buffer_length = 8;
517 priv = usb_get_serial_port_data(port);
518 if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID
519 || priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID) {
520 /* This device doesn't support ioctl calls */
524 /* allocate memory for transfer buffer */
525 transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
526 if (!transfer_buffer)
529 result = usb_control_msg(port->serial->dev,
530 usb_rcvctrlpipe(port->serial->dev, 0),
531 SUSBCRequest_GetStatusLineState,
532 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
536 transfer_buffer_length,
539 dbg("%s - port %d Send get_status_line_state URB returns: %i. Statusline: %02x",
540 __func__, port->number, result, transfer_buffer[0]);
543 if ((transfer_buffer[0] & SUSBCR_GSL_DSR) != 0)
545 kfree(transfer_buffer);
549 static int kobil_tiocmset(struct tty_struct *tty, struct file *file,
550 unsigned int set, unsigned int clear)
552 struct usb_serial_port *port = tty->driver_data;
553 struct kobil_private *priv;
557 unsigned char *transfer_buffer;
558 int transfer_buffer_length = 8;
560 /* FIXME: locking ? */
561 priv = usb_get_serial_port_data(port);
562 if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID
563 || priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID) {
564 /* This device doesn't support ioctl calls */
568 /* allocate memory for transfer buffer */
569 transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
570 if (!transfer_buffer)
577 if (clear & TIOCM_RTS)
579 if (clear & TIOCM_DTR)
582 if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) {
584 dbg("%s - port %d Setting DTR",
585 __func__, port->number);
587 dbg("%s - port %d Clearing DTR",
588 __func__, port->number);
589 result = usb_control_msg(port->serial->dev,
590 usb_rcvctrlpipe(port->serial->dev, 0),
591 SUSBCRequest_SetStatusLinesOrQueues,
592 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
593 ((dtr != 0) ? SUSBCR_SSL_SETDTR : SUSBCR_SSL_CLRDTR),
600 dbg("%s - port %d Setting RTS",
601 __func__, port->number);
603 dbg("%s - port %d Clearing RTS",
604 __func__, port->number);
605 result = usb_control_msg(port->serial->dev,
606 usb_rcvctrlpipe(port->serial->dev, 0),
607 SUSBCRequest_SetStatusLinesOrQueues,
608 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
609 ((rts != 0) ? SUSBCR_SSL_SETRTS : SUSBCR_SSL_CLRRTS),
615 dbg("%s - port %d Send set_status_line URB returns: %i",
616 __func__, port->number, result);
617 kfree(transfer_buffer);
618 return (result < 0) ? result : 0;
621 static void kobil_set_termios(struct tty_struct *tty,
622 struct usb_serial_port *port, struct ktermios *old)
624 struct kobil_private *priv;
626 unsigned short urb_val = 0;
627 int c_cflag = tty->termios->c_cflag;
631 priv = usb_get_serial_port_data(port);
632 if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID ||
633 priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID) {
634 /* This device doesn't support ioctl calls */
635 *tty->termios = *old;
639 speed = tty_get_baud_rate(tty);
642 urb_val = SUSBCR_SBR_1200;
647 urb_val = SUSBCR_SBR_9600;
650 urb_val |= (c_cflag & CSTOPB) ? SUSBCR_SPASB_2StopBits :
651 SUSBCR_SPASB_1StopBit;
653 settings = kzalloc(50, GFP_KERNEL);
657 sprintf(settings, "%d ", speed);
659 if (c_cflag & PARENB) {
660 if (c_cflag & PARODD) {
661 urb_val |= SUSBCR_SPASB_OddParity;
662 strcat(settings, "Odd Parity");
664 urb_val |= SUSBCR_SPASB_EvenParity;
665 strcat(settings, "Even Parity");
668 urb_val |= SUSBCR_SPASB_NoParity;
669 strcat(settings, "No Parity");
671 tty->termios->c_cflag &= ~CMSPAR;
672 tty_encode_baud_rate(tty, speed, speed);
674 result = usb_control_msg(port->serial->dev,
675 usb_rcvctrlpipe(port->serial->dev, 0),
676 SUSBCRequest_SetBaudRateParityAndStopBits,
677 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
687 static int kobil_ioctl(struct tty_struct *tty, struct file *file,
688 unsigned int cmd, unsigned long arg)
690 struct usb_serial_port *port = tty->driver_data;
691 struct kobil_private *priv = usb_get_serial_port_data(port);
692 unsigned char *transfer_buffer;
693 int transfer_buffer_length = 8;
696 if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID ||
697 priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID)
698 /* This device doesn't support ioctl calls */
703 transfer_buffer = kmalloc(transfer_buffer_length, GFP_KERNEL);
704 if (!transfer_buffer)
707 result = usb_control_msg(port->serial->dev,
708 usb_rcvctrlpipe(port->serial->dev, 0),
710 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
711 SUSBCR_MSC_ResetAllQueues,
713 NULL, /* transfer_buffer, */
718 dbg("%s - port %d Send reset_all_queues (FLUSH) URB returns: %i", __func__, port->number, result);
719 kfree(transfer_buffer);
720 return (result < 0) ? -EIO: 0;
726 static int __init kobil_init(void)
729 retval = usb_serial_register(&kobil_device);
731 goto failed_usb_serial_register;
732 retval = usb_register(&kobil_driver);
734 goto failed_usb_register;
736 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
741 usb_serial_deregister(&kobil_device);
742 failed_usb_serial_register:
747 static void __exit kobil_exit(void)
749 usb_deregister(&kobil_driver);
750 usb_serial_deregister(&kobil_device);
753 module_init(kobil_init);
754 module_exit(kobil_exit);
756 MODULE_AUTHOR(DRIVER_AUTHOR);
757 MODULE_DESCRIPTION(DRIVER_DESC);
758 MODULE_LICENSE("GPL");
760 module_param(debug, bool, S_IRUGO | S_IWUSR);
761 MODULE_PARM_DESC(debug, "Debug enabled or not");