Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  *  KOBIL USB Smart Card Terminal Driver
0004  *
0005  *  Copyright (C) 2002  KOBIL Systems GmbH
0006  *  Author: Thomas Wahrenbruch
0007  *
0008  *  Contact: linuxusb@kobil.de
0009  *
0010  *  This program is largely derived from work by the linux-usb group
0011  *  and associated source files.  Please see the usb/serial files for
0012  *  individual credits and copyrights.
0013  *
0014  *  Thanks to Greg Kroah-Hartman (greg@kroah.com) for his help and
0015  *  patience.
0016  *
0017  * Supported readers: USB TWIN, KAAN Standard Plus and SecOVID Reader Plus
0018  * (Adapter K), B1 Professional and KAAN Professional (Adapter B)
0019  */
0020 
0021 
0022 #include <linux/kernel.h>
0023 #include <linux/errno.h>
0024 #include <linux/slab.h>
0025 #include <linux/tty.h>
0026 #include <linux/tty_driver.h>
0027 #include <linux/tty_flip.h>
0028 #include <linux/module.h>
0029 #include <linux/spinlock.h>
0030 #include <linux/uaccess.h>
0031 #include <linux/usb.h>
0032 #include <linux/usb/serial.h>
0033 #include <linux/ioctl.h>
0034 #include "kobil_sct.h"
0035 
0036 #define DRIVER_AUTHOR "KOBIL Systems GmbH - http://www.kobil.com"
0037 #define DRIVER_DESC "KOBIL USB Smart Card Terminal Driver (experimental)"
0038 
0039 #define KOBIL_VENDOR_ID         0x0D46
0040 #define KOBIL_ADAPTER_B_PRODUCT_ID  0x2011
0041 #define KOBIL_ADAPTER_K_PRODUCT_ID  0x2012
0042 #define KOBIL_USBTWIN_PRODUCT_ID    0x0078
0043 #define KOBIL_KAAN_SIM_PRODUCT_ID       0x0081
0044 
0045 #define KOBIL_TIMEOUT       500
0046 #define KOBIL_BUF_LENGTH    300
0047 
0048 
0049 /* Function prototypes */
0050 static int kobil_port_probe(struct usb_serial_port *probe);
0051 static void kobil_port_remove(struct usb_serial_port *probe);
0052 static int  kobil_open(struct tty_struct *tty, struct usb_serial_port *port);
0053 static void kobil_close(struct usb_serial_port *port);
0054 static int  kobil_write(struct tty_struct *tty, struct usb_serial_port *port,
0055              const unsigned char *buf, int count);
0056 static unsigned int kobil_write_room(struct tty_struct *tty);
0057 static int  kobil_ioctl(struct tty_struct *tty,
0058             unsigned int cmd, unsigned long arg);
0059 static int  kobil_tiocmget(struct tty_struct *tty);
0060 static int  kobil_tiocmset(struct tty_struct *tty,
0061                unsigned int set, unsigned int clear);
0062 static void kobil_read_int_callback(struct urb *urb);
0063 static void kobil_write_int_callback(struct urb *urb);
0064 static void kobil_set_termios(struct tty_struct *tty,
0065             struct usb_serial_port *port, struct ktermios *old);
0066 static void kobil_init_termios(struct tty_struct *tty);
0067 
0068 static const struct usb_device_id id_table[] = {
0069     { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_ADAPTER_B_PRODUCT_ID) },
0070     { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_ADAPTER_K_PRODUCT_ID) },
0071     { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_USBTWIN_PRODUCT_ID) },
0072     { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_KAAN_SIM_PRODUCT_ID) },
0073     { }         /* Terminating entry */
0074 };
0075 MODULE_DEVICE_TABLE(usb, id_table);
0076 
0077 static struct usb_serial_driver kobil_device = {
0078     .driver = {
0079         .owner =    THIS_MODULE,
0080         .name =     "kobil",
0081     },
0082     .description =      "KOBIL USB smart card terminal",
0083     .id_table =     id_table,
0084     .num_ports =        1,
0085     .num_interrupt_out =    1,
0086     .port_probe =       kobil_port_probe,
0087     .port_remove =      kobil_port_remove,
0088     .ioctl =        kobil_ioctl,
0089     .set_termios =      kobil_set_termios,
0090     .init_termios =     kobil_init_termios,
0091     .tiocmget =     kobil_tiocmget,
0092     .tiocmset =     kobil_tiocmset,
0093     .open =         kobil_open,
0094     .close =        kobil_close,
0095     .write =        kobil_write,
0096     .write_room =       kobil_write_room,
0097     .read_int_callback =    kobil_read_int_callback,
0098     .write_int_callback =   kobil_write_int_callback,
0099 };
0100 
0101 static struct usb_serial_driver * const serial_drivers[] = {
0102     &kobil_device, NULL
0103 };
0104 
0105 struct kobil_private {
0106     unsigned char buf[KOBIL_BUF_LENGTH]; /* buffer for the APDU to send */
0107     int filled;  /* index of the last char in buf */
0108     int cur_pos; /* index of the next char to send in buf */
0109     __u16 device_type;
0110 };
0111 
0112 
0113 static int kobil_port_probe(struct usb_serial_port *port)
0114 {
0115     struct usb_serial *serial = port->serial;
0116     struct kobil_private *priv;
0117 
0118     priv = kmalloc(sizeof(struct kobil_private), GFP_KERNEL);
0119     if (!priv)
0120         return -ENOMEM;
0121 
0122     priv->filled = 0;
0123     priv->cur_pos = 0;
0124     priv->device_type = le16_to_cpu(serial->dev->descriptor.idProduct);
0125 
0126     switch (priv->device_type) {
0127     case KOBIL_ADAPTER_B_PRODUCT_ID:
0128         dev_dbg(&serial->dev->dev, "KOBIL B1 PRO / KAAN PRO detected\n");
0129         break;
0130     case KOBIL_ADAPTER_K_PRODUCT_ID:
0131         dev_dbg(&serial->dev->dev, "KOBIL KAAN Standard Plus / SecOVID Reader Plus detected\n");
0132         break;
0133     case KOBIL_USBTWIN_PRODUCT_ID:
0134         dev_dbg(&serial->dev->dev, "KOBIL USBTWIN detected\n");
0135         break;
0136     case KOBIL_KAAN_SIM_PRODUCT_ID:
0137         dev_dbg(&serial->dev->dev, "KOBIL KAAN SIM detected\n");
0138         break;
0139     }
0140     usb_set_serial_port_data(port, priv);
0141 
0142     return 0;
0143 }
0144 
0145 
0146 static void kobil_port_remove(struct usb_serial_port *port)
0147 {
0148     struct kobil_private *priv;
0149 
0150     priv = usb_get_serial_port_data(port);
0151     kfree(priv);
0152 }
0153 
0154 static void kobil_init_termios(struct tty_struct *tty)
0155 {
0156     /* Default to echo off and other sane device settings */
0157     tty->termios.c_lflag = 0;
0158     tty->termios.c_iflag &= ~(ISIG | ICANON | ECHO | IEXTEN | XCASE);
0159     tty->termios.c_iflag |= IGNBRK | IGNPAR | IXOFF;
0160     /* do NOT translate CR to CR-NL (0x0A -> 0x0A 0x0D) */
0161     tty->termios.c_oflag &= ~ONLCR;
0162 }
0163 
0164 static int kobil_open(struct tty_struct *tty, struct usb_serial_port *port)
0165 {
0166     struct device *dev = &port->dev;
0167     int result = 0;
0168     struct kobil_private *priv;
0169     unsigned char *transfer_buffer;
0170     int transfer_buffer_length = 8;
0171 
0172     priv = usb_get_serial_port_data(port);
0173 
0174     /* allocate memory for transfer buffer */
0175     transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
0176     if (!transfer_buffer)
0177         return -ENOMEM;
0178 
0179     /* get hardware version */
0180     result = usb_control_msg(port->serial->dev,
0181               usb_rcvctrlpipe(port->serial->dev, 0),
0182               SUSBCRequest_GetMisc,
0183               USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
0184               SUSBCR_MSC_GetHWVersion,
0185               0,
0186               transfer_buffer,
0187               transfer_buffer_length,
0188               KOBIL_TIMEOUT
0189     );
0190     dev_dbg(dev, "%s - Send get_HW_version URB returns: %i\n", __func__, result);
0191     if (result >= 3) {
0192         dev_dbg(dev, "Hardware version: %i.%i.%i\n", transfer_buffer[0],
0193                 transfer_buffer[1], transfer_buffer[2]);
0194     }
0195 
0196     /* get firmware version */
0197     result = usb_control_msg(port->serial->dev,
0198               usb_rcvctrlpipe(port->serial->dev, 0),
0199               SUSBCRequest_GetMisc,
0200               USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
0201               SUSBCR_MSC_GetFWVersion,
0202               0,
0203               transfer_buffer,
0204               transfer_buffer_length,
0205               KOBIL_TIMEOUT
0206     );
0207     dev_dbg(dev, "%s - Send get_FW_version URB returns: %i\n", __func__, result);
0208     if (result >= 3) {
0209         dev_dbg(dev, "Firmware version: %i.%i.%i\n", transfer_buffer[0],
0210                 transfer_buffer[1], transfer_buffer[2]);
0211     }
0212 
0213     if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID ||
0214             priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) {
0215         /* Setting Baudrate, Parity and Stopbits */
0216         result = usb_control_msg(port->serial->dev,
0217               usb_sndctrlpipe(port->serial->dev, 0),
0218               SUSBCRequest_SetBaudRateParityAndStopBits,
0219               USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
0220               SUSBCR_SBR_9600 | SUSBCR_SPASB_EvenParity |
0221                             SUSBCR_SPASB_1StopBit,
0222               0,
0223               NULL,
0224               0,
0225               KOBIL_TIMEOUT
0226         );
0227         dev_dbg(dev, "%s - Send set_baudrate URB returns: %i\n", __func__, result);
0228 
0229         /* reset all queues */
0230         result = usb_control_msg(port->serial->dev,
0231               usb_sndctrlpipe(port->serial->dev, 0),
0232               SUSBCRequest_Misc,
0233               USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
0234               SUSBCR_MSC_ResetAllQueues,
0235               0,
0236               NULL,
0237               0,
0238               KOBIL_TIMEOUT
0239         );
0240         dev_dbg(dev, "%s - Send reset_all_queues URB returns: %i\n", __func__, result);
0241     }
0242     if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID ||
0243         priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID ||
0244         priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID) {
0245         /* start reading (Adapter B 'cause PNP string) */
0246         result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
0247         dev_dbg(dev, "%s - Send read URB returns: %i\n", __func__, result);
0248     }
0249 
0250     kfree(transfer_buffer);
0251     return 0;
0252 }
0253 
0254 
0255 static void kobil_close(struct usb_serial_port *port)
0256 {
0257     /* FIXME: Add rts/dtr methods */
0258     usb_kill_urb(port->interrupt_out_urb);
0259     usb_kill_urb(port->interrupt_in_urb);
0260 }
0261 
0262 
0263 static void kobil_read_int_callback(struct urb *urb)
0264 {
0265     int result;
0266     struct usb_serial_port *port = urb->context;
0267     unsigned char *data = urb->transfer_buffer;
0268     int status = urb->status;
0269 
0270     if (status) {
0271         dev_dbg(&port->dev, "%s - Read int status not zero: %d\n", __func__, status);
0272         return;
0273     }
0274 
0275     if (urb->actual_length) {
0276         usb_serial_debug_data(&port->dev, __func__, urb->actual_length,
0277                                     data);
0278         tty_insert_flip_string(&port->port, data, urb->actual_length);
0279         tty_flip_buffer_push(&port->port);
0280     }
0281 
0282     result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
0283     dev_dbg(&port->dev, "%s - Send read URB returns: %i\n", __func__, result);
0284 }
0285 
0286 
0287 static void kobil_write_int_callback(struct urb *urb)
0288 {
0289 }
0290 
0291 
0292 static int kobil_write(struct tty_struct *tty, struct usb_serial_port *port,
0293             const unsigned char *buf, int count)
0294 {
0295     int length = 0;
0296     int result = 0;
0297     int todo = 0;
0298     struct kobil_private *priv;
0299 
0300     if (count == 0) {
0301         dev_dbg(&port->dev, "%s - write request of 0 bytes\n", __func__);
0302         return 0;
0303     }
0304 
0305     priv = usb_get_serial_port_data(port);
0306 
0307     if (count > (KOBIL_BUF_LENGTH - priv->filled)) {
0308         dev_dbg(&port->dev, "%s - Error: write request bigger than buffer size\n", __func__);
0309         return -ENOMEM;
0310     }
0311 
0312     /* Copy data to buffer */
0313     memcpy(priv->buf + priv->filled, buf, count);
0314     usb_serial_debug_data(&port->dev, __func__, count, priv->buf + priv->filled);
0315     priv->filled = priv->filled + count;
0316 
0317     /* only send complete block. TWIN, KAAN SIM and adapter K
0318        use the same protocol. */
0319     if (((priv->device_type != KOBIL_ADAPTER_B_PRODUCT_ID) && (priv->filled > 2) && (priv->filled >= (priv->buf[1] + 3))) ||
0320          ((priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) && (priv->filled > 3) && (priv->filled >= (priv->buf[2] + 4)))) {
0321         /* stop reading (except TWIN and KAAN SIM) */
0322         if ((priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID)
0323             || (priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID))
0324             usb_kill_urb(port->interrupt_in_urb);
0325 
0326         todo = priv->filled - priv->cur_pos;
0327 
0328         while (todo > 0) {
0329             /* max 8 byte in one urb (endpoint size) */
0330             length = min(todo, port->interrupt_out_size);
0331             /* copy data to transfer buffer */
0332             memcpy(port->interrupt_out_buffer,
0333                     priv->buf + priv->cur_pos, length);
0334             port->interrupt_out_urb->transfer_buffer_length = length;
0335 
0336             priv->cur_pos = priv->cur_pos + length;
0337             result = usb_submit_urb(port->interrupt_out_urb,
0338                     GFP_ATOMIC);
0339             dev_dbg(&port->dev, "%s - Send write URB returns: %i\n", __func__, result);
0340             todo = priv->filled - priv->cur_pos;
0341 
0342             if (todo > 0)
0343                 msleep(24);
0344         }
0345 
0346         priv->filled = 0;
0347         priv->cur_pos = 0;
0348 
0349         /* start reading (except TWIN and KAAN SIM) */
0350         if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID ||
0351             priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) {
0352             result = usb_submit_urb(port->interrupt_in_urb,
0353                     GFP_ATOMIC);
0354             dev_dbg(&port->dev, "%s - Send read URB returns: %i\n", __func__, result);
0355         }
0356     }
0357     return count;
0358 }
0359 
0360 
0361 static unsigned int kobil_write_room(struct tty_struct *tty)
0362 {
0363     /* FIXME */
0364     return 8;
0365 }
0366 
0367 
0368 static int kobil_tiocmget(struct tty_struct *tty)
0369 {
0370     struct usb_serial_port *port = tty->driver_data;
0371     struct kobil_private *priv;
0372     int result;
0373     unsigned char *transfer_buffer;
0374     int transfer_buffer_length = 8;
0375 
0376     priv = usb_get_serial_port_data(port);
0377     if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID
0378             || priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID) {
0379         /* This device doesn't support ioctl calls */
0380         return -EINVAL;
0381     }
0382 
0383     /* allocate memory for transfer buffer */
0384     transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
0385     if (!transfer_buffer)
0386         return -ENOMEM;
0387 
0388     result = usb_control_msg(port->serial->dev,
0389               usb_rcvctrlpipe(port->serial->dev, 0),
0390               SUSBCRequest_GetStatusLineState,
0391               USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
0392               0,
0393               0,
0394               transfer_buffer,
0395               transfer_buffer_length,
0396               KOBIL_TIMEOUT);
0397 
0398     dev_dbg(&port->dev, "Send get_status_line_state URB returns: %i\n",
0399             result);
0400     if (result < 1) {
0401         if (result >= 0)
0402             result = -EIO;
0403         goto out_free;
0404     }
0405 
0406     dev_dbg(&port->dev, "Statusline: %02x\n", transfer_buffer[0]);
0407 
0408     result = 0;
0409     if ((transfer_buffer[0] & SUSBCR_GSL_DSR) != 0)
0410         result = TIOCM_DSR;
0411 out_free:
0412     kfree(transfer_buffer);
0413     return result;
0414 }
0415 
0416 static int kobil_tiocmset(struct tty_struct *tty,
0417                unsigned int set, unsigned int clear)
0418 {
0419     struct usb_serial_port *port = tty->driver_data;
0420     struct device *dev = &port->dev;
0421     struct kobil_private *priv;
0422     int result;
0423     int dtr = 0;
0424     int rts = 0;
0425 
0426     /* FIXME: locking ? */
0427     priv = usb_get_serial_port_data(port);
0428     if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID
0429         || priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID) {
0430         /* This device doesn't support ioctl calls */
0431         return -EINVAL;
0432     }
0433 
0434     if (set & TIOCM_RTS)
0435         rts = 1;
0436     if (set & TIOCM_DTR)
0437         dtr = 1;
0438     if (clear & TIOCM_RTS)
0439         rts = 0;
0440     if (clear & TIOCM_DTR)
0441         dtr = 0;
0442 
0443     if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) {
0444         if (dtr != 0)
0445             dev_dbg(dev, "%s - Setting DTR\n", __func__);
0446         else
0447             dev_dbg(dev, "%s - Clearing DTR\n", __func__);
0448         result = usb_control_msg(port->serial->dev,
0449               usb_sndctrlpipe(port->serial->dev, 0),
0450               SUSBCRequest_SetStatusLinesOrQueues,
0451               USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
0452               ((dtr != 0) ? SUSBCR_SSL_SETDTR : SUSBCR_SSL_CLRDTR),
0453               0,
0454               NULL,
0455               0,
0456               KOBIL_TIMEOUT);
0457     } else {
0458         if (rts != 0)
0459             dev_dbg(dev, "%s - Setting RTS\n", __func__);
0460         else
0461             dev_dbg(dev, "%s - Clearing RTS\n", __func__);
0462         result = usb_control_msg(port->serial->dev,
0463             usb_sndctrlpipe(port->serial->dev, 0),
0464             SUSBCRequest_SetStatusLinesOrQueues,
0465             USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
0466             ((rts != 0) ? SUSBCR_SSL_SETRTS : SUSBCR_SSL_CLRRTS),
0467             0,
0468             NULL,
0469             0,
0470             KOBIL_TIMEOUT);
0471     }
0472     dev_dbg(dev, "%s - Send set_status_line URB returns: %i\n", __func__, result);
0473     return (result < 0) ? result : 0;
0474 }
0475 
0476 static void kobil_set_termios(struct tty_struct *tty,
0477             struct usb_serial_port *port, struct ktermios *old)
0478 {
0479     struct kobil_private *priv;
0480     int result;
0481     unsigned short urb_val = 0;
0482     int c_cflag = tty->termios.c_cflag;
0483     speed_t speed;
0484 
0485     priv = usb_get_serial_port_data(port);
0486     if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID ||
0487             priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID) {
0488         /* This device doesn't support ioctl calls */
0489         tty_termios_copy_hw(&tty->termios, old);
0490         return;
0491     }
0492 
0493     speed = tty_get_baud_rate(tty);
0494     switch (speed) {
0495     case 1200:
0496         urb_val = SUSBCR_SBR_1200;
0497         break;
0498     default:
0499         speed = 9600;
0500         fallthrough;
0501     case 9600:
0502         urb_val = SUSBCR_SBR_9600;
0503         break;
0504     }
0505     urb_val |= (c_cflag & CSTOPB) ? SUSBCR_SPASB_2StopBits :
0506                             SUSBCR_SPASB_1StopBit;
0507     if (c_cflag & PARENB) {
0508         if  (c_cflag & PARODD)
0509             urb_val |= SUSBCR_SPASB_OddParity;
0510         else
0511             urb_val |= SUSBCR_SPASB_EvenParity;
0512     } else
0513         urb_val |= SUSBCR_SPASB_NoParity;
0514     tty->termios.c_cflag &= ~CMSPAR;
0515     tty_encode_baud_rate(tty, speed, speed);
0516 
0517     result = usb_control_msg(port->serial->dev,
0518           usb_sndctrlpipe(port->serial->dev, 0),
0519           SUSBCRequest_SetBaudRateParityAndStopBits,
0520           USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
0521           urb_val,
0522           0,
0523           NULL,
0524           0,
0525           KOBIL_TIMEOUT
0526         );
0527     if (result) {
0528         dev_err(&port->dev, "failed to update line settings: %d\n",
0529                 result);
0530     }
0531 }
0532 
0533 static int kobil_ioctl(struct tty_struct *tty,
0534                     unsigned int cmd, unsigned long arg)
0535 {
0536     struct usb_serial_port *port = tty->driver_data;
0537     struct kobil_private *priv = usb_get_serial_port_data(port);
0538     int result;
0539 
0540     if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID ||
0541             priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID)
0542         /* This device doesn't support ioctl calls */
0543         return -ENOIOCTLCMD;
0544 
0545     switch (cmd) {
0546     case TCFLSH:
0547         result = usb_control_msg(port->serial->dev,
0548               usb_sndctrlpipe(port->serial->dev, 0),
0549               SUSBCRequest_Misc,
0550               USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
0551               SUSBCR_MSC_ResetAllQueues,
0552               0,
0553               NULL,
0554               0,
0555               KOBIL_TIMEOUT
0556             );
0557 
0558         dev_dbg(&port->dev,
0559             "%s - Send reset_all_queues (FLUSH) URB returns: %i\n",
0560             __func__, result);
0561         return (result < 0) ? -EIO: 0;
0562     default:
0563         return -ENOIOCTLCMD;
0564     }
0565 }
0566 
0567 module_usb_serial_driver(serial_drivers, id_table);
0568 
0569 MODULE_AUTHOR(DRIVER_AUTHOR);
0570 MODULE_DESCRIPTION(DRIVER_DESC);
0571 MODULE_LICENSE("GPL");