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 <asm/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 usb_serial_port *port, struct file *filp);
74 static void kobil_close (struct usb_serial_port *port, struct file *filp);
75 static int kobil_write (struct usb_serial_port *port,
76 const unsigned char *buf, int count);
77 static int kobil_write_room(struct usb_serial_port *port);
78 static int kobil_ioctl(struct usb_serial_port *port, struct file *file,
79 unsigned int cmd, unsigned long arg);
80 static int kobil_tiocmget(struct usb_serial_port *port, struct file *file);
81 static int kobil_tiocmset(struct usb_serial_port *port, struct file *file,
82 unsigned int set, unsigned int clear);
83 static void kobil_read_int_callback( struct urb *urb );
84 static void kobil_write_callback( struct urb *purb );
85 static void kobil_set_termios(struct usb_serial_port *port, struct ktermios *old);
88 static struct usb_device_id id_table [] = {
89 { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_ADAPTER_B_PRODUCT_ID) },
90 { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_ADAPTER_K_PRODUCT_ID) },
91 { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_USBTWIN_PRODUCT_ID) },
92 { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_KAAN_SIM_PRODUCT_ID) },
93 { } /* Terminating entry */
97 MODULE_DEVICE_TABLE (usb, id_table);
99 static struct usb_driver kobil_driver = {
101 .probe = usb_serial_probe,
102 .disconnect = usb_serial_disconnect,
103 .id_table = id_table,
108 static struct usb_serial_driver kobil_device = {
110 .owner = THIS_MODULE,
113 .description = "KOBIL USB smart card terminal",
114 .usb_driver = &kobil_driver,
115 .id_table = id_table,
117 .attach = kobil_startup,
118 .shutdown = kobil_shutdown,
119 .ioctl = kobil_ioctl,
120 .set_termios = kobil_set_termios,
121 .tiocmget = kobil_tiocmget,
122 .tiocmset = kobil_tiocmset,
124 .close = kobil_close,
125 .write = kobil_write,
126 .write_room = kobil_write_room,
127 .read_int_callback = kobil_read_int_callback,
131 struct kobil_private {
132 int write_int_endpoint_address;
133 int read_int_endpoint_address;
134 unsigned char buf[KOBIL_BUF_LENGTH]; // buffer for the APDU to send
135 int filled; // index of the last char in buf
136 int cur_pos; // index of the next char to send in buf
141 static int kobil_startup (struct usb_serial *serial)
144 struct kobil_private *priv;
145 struct usb_device *pdev;
146 struct usb_host_config *actconfig;
147 struct usb_interface *interface;
148 struct usb_host_interface *altsetting;
149 struct usb_host_endpoint *endpoint;
151 priv = kmalloc(sizeof(struct kobil_private), GFP_KERNEL);
158 priv->device_type = le16_to_cpu(serial->dev->descriptor.idProduct);
160 switch (priv->device_type){
161 case KOBIL_ADAPTER_B_PRODUCT_ID:
162 printk(KERN_DEBUG "KOBIL B1 PRO / KAAN PRO detected\n");
164 case KOBIL_ADAPTER_K_PRODUCT_ID:
165 printk(KERN_DEBUG "KOBIL KAAN Standard Plus / SecOVID Reader Plus detected\n");
167 case KOBIL_USBTWIN_PRODUCT_ID:
168 printk(KERN_DEBUG "KOBIL USBTWIN detected\n");
170 case KOBIL_KAAN_SIM_PRODUCT_ID:
171 printk(KERN_DEBUG "KOBIL KAAN SIM detected\n");
174 usb_set_serial_port_data(serial->port[0], priv);
176 // search for the necessary endpoints
178 actconfig = pdev->actconfig;
179 interface = actconfig->interface[0];
180 altsetting = interface->cur_altsetting;
181 endpoint = altsetting->endpoint;
183 for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
184 endpoint = &altsetting->endpoint[i];
185 if (usb_endpoint_is_int_out(&endpoint->desc)) {
186 dbg("%s Found interrupt out endpoint. Address: %d", __func__, endpoint->desc.bEndpointAddress);
187 priv->write_int_endpoint_address = endpoint->desc.bEndpointAddress;
189 if (usb_endpoint_is_int_in(&endpoint->desc)) {
190 dbg("%s Found interrupt in endpoint. Address: %d", __func__, endpoint->desc.bEndpointAddress);
191 priv->read_int_endpoint_address = endpoint->desc.bEndpointAddress;
198 static void kobil_shutdown (struct usb_serial *serial)
201 dbg("%s - port %d", __func__, serial->port[0]->number);
203 for (i=0; i < serial->num_ports; ++i) {
204 while (serial->port[i]->open_count > 0) {
205 kobil_close (serial->port[i], NULL);
207 kfree(usb_get_serial_port_data(serial->port[i]));
208 usb_set_serial_port_data(serial->port[i], NULL);
213 static int kobil_open (struct usb_serial_port *port, struct file *filp)
216 struct kobil_private *priv;
217 unsigned char *transfer_buffer;
218 int transfer_buffer_length = 8;
219 int write_urb_transfer_buffer_length = 8;
221 dbg("%s - port %d", __func__, port->number);
222 priv = usb_get_serial_port_data(port);
224 // someone sets the dev to 0 if the close method has been called
225 port->interrupt_in_urb->dev = port->serial->dev;
228 /* force low_latency on so that our tty_push actually forces
229 * the data through, otherwise it is scheduled, and with high
230 * data rates (like with OHCI) data can get lost.
232 port->tty->low_latency = 1;
234 // without this, every push_tty_char is echoed :-(
235 port->tty->termios->c_lflag = 0;
236 port->tty->termios->c_lflag &= ~(ISIG | ICANON | ECHO | IEXTEN | XCASE);
237 port->tty->termios->c_iflag = IGNBRK | IGNPAR | IXOFF;
238 port->tty->termios->c_oflag &= ~ONLCR; // do NOT translate CR to CR-NL (0x0A -> 0x0A 0x0D)
240 // allocate memory for transfer buffer
241 transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
242 if (! transfer_buffer) {
246 // allocate write_urb
247 if (!port->write_urb) {
248 dbg("%s - port %d Allocating port->write_urb", __func__, port->number);
249 port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
250 if (!port->write_urb) {
251 dbg("%s - port %d usb_alloc_urb failed", __func__, port->number);
252 kfree(transfer_buffer);
257 // allocate memory for write_urb transfer buffer
258 port->write_urb->transfer_buffer = kmalloc(write_urb_transfer_buffer_length, GFP_KERNEL);
259 if (! port->write_urb->transfer_buffer) {
260 kfree(transfer_buffer);
261 usb_free_urb(port->write_urb);
262 port->write_urb = NULL;
266 // get hardware version
267 result = usb_control_msg( port->serial->dev,
268 usb_rcvctrlpipe(port->serial->dev, 0 ),
269 SUSBCRequest_GetMisc,
270 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
271 SUSBCR_MSC_GetHWVersion,
274 transfer_buffer_length,
277 dbg("%s - port %d Send get_HW_version URB returns: %i", __func__, port->number, result);
278 dbg("Harware version: %i.%i.%i", transfer_buffer[0], transfer_buffer[1], transfer_buffer[2] );
280 // get firmware version
281 result = usb_control_msg( port->serial->dev,
282 usb_rcvctrlpipe(port->serial->dev, 0 ),
283 SUSBCRequest_GetMisc,
284 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
285 SUSBCR_MSC_GetFWVersion,
288 transfer_buffer_length,
291 dbg("%s - port %d Send get_FW_version URB returns: %i", __func__, port->number, result);
292 dbg("Firmware version: %i.%i.%i", transfer_buffer[0], transfer_buffer[1], transfer_buffer[2] );
294 if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID || priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) {
295 // Setting Baudrate, Parity and Stopbits
296 result = usb_control_msg( port->serial->dev,
297 usb_rcvctrlpipe(port->serial->dev, 0 ),
298 SUSBCRequest_SetBaudRateParityAndStopBits,
299 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
300 SUSBCR_SBR_9600 | SUSBCR_SPASB_EvenParity | SUSBCR_SPASB_1StopBit,
306 dbg("%s - port %d Send set_baudrate URB returns: %i", __func__, port->number, result);
309 result = usb_control_msg( port->serial->dev,
310 usb_rcvctrlpipe(port->serial->dev, 0 ),
312 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
313 SUSBCR_MSC_ResetAllQueues,
319 dbg("%s - port %d Send reset_all_queues URB returns: %i", __func__, port->number, result);
321 if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID || priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID ||
322 priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID) {
323 // start reading (Adapter B 'cause PNP string)
324 result = usb_submit_urb( port->interrupt_in_urb, GFP_ATOMIC );
325 dbg("%s - port %d Send read URB returns: %i", __func__, port->number, result);
328 kfree(transfer_buffer);
333 static void kobil_close (struct usb_serial_port *port, struct file *filp)
335 dbg("%s - port %d", __func__, port->number);
337 if (port->write_urb) {
338 usb_kill_urb(port->write_urb);
339 usb_free_urb( port->write_urb );
340 port->write_urb = NULL;
342 usb_kill_urb(port->interrupt_in_urb);
346 static void kobil_read_int_callback(struct urb *urb)
349 struct usb_serial_port *port = urb->context;
350 struct tty_struct *tty;
351 unsigned char *data = urb->transfer_buffer;
352 int status = urb->status;
355 dbg("%s - port %d", __func__, port->number);
358 dbg("%s - port %d Read int status not zero: %d",
359 __func__, port->number, status);
364 if (urb->actual_length) {
368 dbg_data = kzalloc((3 * purb->actual_length + 10) * sizeof(char), GFP_KERNEL);
372 for (i = 0; i < purb->actual_length; i++) {
373 sprintf(dbg_data +3*i, "%02X ", data[i]);
375 dbg(" <-- %s", dbg_data );
380 tty_buffer_request_room(tty, urb->actual_length);
381 tty_insert_flip_string(tty, data, urb->actual_length);
382 tty_flip_buffer_push(tty);
385 // someone sets the dev to 0 if the close method has been called
386 port->interrupt_in_urb->dev = port->serial->dev;
388 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
389 dbg("%s - port %d Send read URB returns: %i", __func__, port->number, result);
393 static void kobil_write_callback( struct urb *purb )
398 static int kobil_write (struct usb_serial_port *port,
399 const unsigned char *buf, int count)
404 struct kobil_private * priv;
407 dbg("%s - port %d write request of 0 bytes", __func__, port->number);
411 priv = usb_get_serial_port_data(port);
413 if (count > (KOBIL_BUF_LENGTH - priv->filled)) {
414 dbg("%s - port %d Error: write request bigger than buffer size", __func__, port->number);
418 // Copy data to buffer
419 memcpy (priv->buf + priv->filled, buf, count);
421 usb_serial_debug_data(debug, &port->dev, __func__, count, priv->buf + priv->filled);
423 priv->filled = priv->filled + count;
426 // only send complete block. TWIN, KAAN SIM and adapter K use the same protocol.
427 if ( ((priv->device_type != KOBIL_ADAPTER_B_PRODUCT_ID) && (priv->filled > 2) && (priv->filled >= (priv->buf[1] + 3))) ||
428 ((priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) && (priv->filled > 3) && (priv->filled >= (priv->buf[2] + 4))) ) {
430 // stop reading (except TWIN and KAAN SIM)
431 if ( (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) || (priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) )
432 usb_kill_urb(port->interrupt_in_urb);
434 todo = priv->filled - priv->cur_pos;
437 // max 8 byte in one urb (endpoint size)
438 length = (todo < 8) ? todo : 8;
439 // copy data to transfer buffer
440 memcpy(port->write_urb->transfer_buffer, priv->buf + priv->cur_pos, length );
441 usb_fill_int_urb( port->write_urb,
443 usb_sndintpipe(port->serial->dev, priv->write_int_endpoint_address),
444 port->write_urb->transfer_buffer,
446 kobil_write_callback,
451 priv->cur_pos = priv->cur_pos + length;
452 result = usb_submit_urb( port->write_urb, GFP_NOIO );
453 dbg("%s - port %d Send write URB returns: %i", __func__, port->number, result);
454 todo = priv->filled - priv->cur_pos;
465 // someone sets the dev to 0 if the close method has been called
466 port->interrupt_in_urb->dev = port->serial->dev;
468 // start reading (except TWIN and KAAN SIM)
469 if ( (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) || (priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) ) {
470 // someone sets the dev to 0 if the close method has been called
471 port->interrupt_in_urb->dev = port->serial->dev;
473 result = usb_submit_urb( port->interrupt_in_urb, GFP_NOIO );
474 dbg("%s - port %d Send read URB returns: %i", __func__, port->number, result);
481 static int kobil_write_room (struct usb_serial_port *port)
483 //dbg("%s - port %d", __func__, port->number);
488 static int kobil_tiocmget(struct usb_serial_port *port, struct file *file)
490 struct kobil_private * priv;
492 unsigned char *transfer_buffer;
493 int transfer_buffer_length = 8;
495 priv = usb_get_serial_port_data(port);
496 if ((priv->device_type == KOBIL_USBTWIN_PRODUCT_ID) || (priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID)) {
497 // This device doesn't support ioctl calls
501 // allocate memory for transfer buffer
502 transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
503 if (!transfer_buffer) {
507 result = usb_control_msg( port->serial->dev,
508 usb_rcvctrlpipe(port->serial->dev, 0 ),
509 SUSBCRequest_GetStatusLineState,
510 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
514 transfer_buffer_length,
517 dbg("%s - port %d Send get_status_line_state URB returns: %i. Statusline: %02x",
518 __func__, port->number, result, transfer_buffer[0]);
521 if ((transfer_buffer[0] & SUSBCR_GSL_DSR) != 0)
523 kfree(transfer_buffer);
527 static int kobil_tiocmset(struct usb_serial_port *port, struct file *file,
528 unsigned int set, unsigned int clear)
530 struct kobil_private * priv;
534 unsigned char *transfer_buffer;
535 int transfer_buffer_length = 8;
537 /* FIXME: locking ? */
538 priv = usb_get_serial_port_data(port);
539 if ((priv->device_type == KOBIL_USBTWIN_PRODUCT_ID) || (priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID)) {
540 // This device doesn't support ioctl calls
544 // allocate memory for transfer buffer
545 transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
546 if (! transfer_buffer) {
554 if (clear & TIOCM_RTS)
556 if (clear & TIOCM_DTR)
559 if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) {
561 dbg("%s - port %d Setting DTR", __func__, port->number);
563 dbg("%s - port %d Clearing DTR", __func__, port->number);
564 result = usb_control_msg( port->serial->dev,
565 usb_rcvctrlpipe(port->serial->dev, 0 ),
566 SUSBCRequest_SetStatusLinesOrQueues,
567 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
568 ((dtr != 0) ? SUSBCR_SSL_SETDTR : SUSBCR_SSL_CLRDTR),
575 dbg("%s - port %d Setting RTS", __func__, port->number);
577 dbg("%s - port %d Clearing RTS", __func__, port->number);
578 result = usb_control_msg( port->serial->dev,
579 usb_rcvctrlpipe(port->serial->dev, 0 ),
580 SUSBCRequest_SetStatusLinesOrQueues,
581 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
582 ((rts != 0) ? SUSBCR_SSL_SETRTS : SUSBCR_SSL_CLRRTS),
588 dbg("%s - port %d Send set_status_line URB returns: %i", __func__, port->number, result);
589 kfree(transfer_buffer);
590 return (result < 0) ? result : 0;
593 static void kobil_set_termios(struct usb_serial_port *port, struct ktermios *old)
595 struct kobil_private * priv;
597 unsigned short urb_val = 0;
598 int c_cflag = port->tty->termios->c_cflag;
602 priv = usb_get_serial_port_data(port);
603 if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID || priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID)
604 // This device doesn't support ioctl calls
607 switch (speed = tty_get_baud_rate(port->tty)) {
609 urb_val = SUSBCR_SBR_1200;
614 urb_val = SUSBCR_SBR_9600;
617 urb_val |= (c_cflag & CSTOPB) ? SUSBCR_SPASB_2StopBits : SUSBCR_SPASB_1StopBit;
619 settings = kzalloc(50, GFP_KERNEL);
623 sprintf(settings, "%d ", speed);
625 if (c_cflag & PARENB) {
626 if (c_cflag & PARODD) {
627 urb_val |= SUSBCR_SPASB_OddParity;
628 strcat(settings, "Odd Parity");
630 urb_val |= SUSBCR_SPASB_EvenParity;
631 strcat(settings, "Even Parity");
634 urb_val |= SUSBCR_SPASB_NoParity;
635 strcat(settings, "No Parity");
637 port->tty->termios->c_cflag &= ~CMSPAR;
638 tty_encode_baud_rate(port->tty, speed, speed);
640 result = usb_control_msg( port->serial->dev,
641 usb_rcvctrlpipe(port->serial->dev, 0 ),
642 SUSBCRequest_SetBaudRateParityAndStopBits,
643 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
653 static int kobil_ioctl(struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
655 struct kobil_private * priv = usb_get_serial_port_data(port);
656 unsigned char *transfer_buffer;
657 int transfer_buffer_length = 8;
660 if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID || priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID)
661 // This device doesn't support ioctl calls
665 case TCFLSH: // 0x540B
666 transfer_buffer = kmalloc(transfer_buffer_length, GFP_KERNEL);
667 if (! transfer_buffer)
670 result = usb_control_msg( port->serial->dev,
671 usb_rcvctrlpipe(port->serial->dev, 0 ),
673 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
674 SUSBCR_MSC_ResetAllQueues,
676 NULL,//transfer_buffer,
681 dbg("%s - port %d Send reset_all_queues (FLUSH) URB returns: %i", __func__, port->number, result);
682 kfree(transfer_buffer);
683 return (result < 0) ? -EFAULT : 0;
689 static int __init kobil_init (void)
692 retval = usb_serial_register(&kobil_device);
694 goto failed_usb_serial_register;
695 retval = usb_register(&kobil_driver);
697 goto failed_usb_register;
699 info(DRIVER_VERSION " " DRIVER_AUTHOR);
704 usb_serial_deregister(&kobil_device);
705 failed_usb_serial_register:
710 static void __exit kobil_exit (void)
712 usb_deregister (&kobil_driver);
713 usb_serial_deregister (&kobil_device);
716 module_init(kobil_init);
717 module_exit(kobil_exit);
719 MODULE_AUTHOR( DRIVER_AUTHOR );
720 MODULE_DESCRIPTION( DRIVER_DESC );
721 MODULE_LICENSE( "GPL" );
723 module_param(debug, bool, S_IRUGO | S_IWUSR);
724 MODULE_PARM_DESC(debug, "Debug enabled or not");