Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * KOBIL USB Smart Card Terminal Driver |
| 3 | * |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 4 | * Copyright (C) 2002 KOBIL Systems GmbH |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * Author: Thomas Wahrenbruch |
| 6 | * |
| 7 | * Contact: linuxusb@kobil.de |
| 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 | * Supported readers: USB TWIN, KAAN Standard Plus and SecOVID Reader Plus |
| 22 | * (Adapter K), B1 Professional and KAAN Professional (Adapter B) |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 23 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | * (21/05/2004) tw |
| 25 | * Fix bug with P'n'P readers |
| 26 | * |
| 27 | * (28/05/2003) tw |
| 28 | * Add support for KAAN SIM |
| 29 | * |
| 30 | * (12/09/2002) tw |
| 31 | * Adapted to 2.5. |
| 32 | * |
| 33 | * (11/08/2002) tw |
| 34 | * Initial version. |
| 35 | */ |
| 36 | |
| 37 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 47 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #include <linux/usb.h> |
Greg Kroah-Hartman | a969888 | 2006-07-11 21:22:58 -0700 | [diff] [blame] | 49 | #include <linux/usb/serial.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | #include <linux/ioctl.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #include "kobil_sct.h" |
| 52 | |
| 53 | static int debug; |
| 54 | |
| 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)" |
| 59 | |
| 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 |
| 65 | |
| 66 | #define KOBIL_TIMEOUT 500 |
| 67 | #define KOBIL_BUF_LENGTH 300 |
| 68 | |
| 69 | |
| 70 | /* Function prototypes */ |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 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, |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 74 | struct usb_serial_port *port, struct file *filp); |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 75 | static void kobil_close(struct tty_struct *tty, struct usb_serial_port *port, |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 76 | struct file *filp); |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 77 | static int kobil_write(struct tty_struct *tty, struct usb_serial_port *port, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | const unsigned char *buf, int count); |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 79 | static int kobil_write_room(struct tty_struct *tty); |
| 80 | static int kobil_ioctl(struct tty_struct *tty, struct file *file, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | unsigned int cmd, unsigned long arg); |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 82 | static int kobil_tiocmget(struct tty_struct *tty, struct file *file); |
| 83 | static int kobil_tiocmset(struct tty_struct *tty, struct file *file, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | unsigned int set, unsigned int clear); |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 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, |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 88 | struct usb_serial_port *port, struct ktermios *old); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
| 90 | |
| 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 */ |
| 97 | }; |
| 98 | |
| 99 | |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 100 | MODULE_DEVICE_TABLE(usb, id_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
| 102 | static struct usb_driver kobil_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | .name = "kobil", |
| 104 | .probe = usb_serial_probe, |
| 105 | .disconnect = usb_serial_disconnect, |
| 106 | .id_table = id_table, |
Greg Kroah-Hartman | ba9dc65 | 2005-11-16 13:41:28 -0800 | [diff] [blame] | 107 | .no_dynamic_id = 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | |
Greg Kroah-Hartman | ea65370 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 111 | static struct usb_serial_driver kobil_device = { |
Greg Kroah-Hartman | 18fcac3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 112 | .driver = { |
| 113 | .owner = THIS_MODULE, |
Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 114 | .name = "kobil", |
Greg Kroah-Hartman | 18fcac3 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 115 | }, |
Greg Kroah-Hartman | 269bda1 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 116 | .description = "KOBIL USB smart card terminal", |
Johannes Hölzl | d9b1b78 | 2006-12-17 21:50:24 +0100 | [diff] [blame] | 117 | .usb_driver = &kobil_driver, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | .id_table = id_table, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | .num_ports = 1, |
| 120 | .attach = kobil_startup, |
| 121 | .shutdown = kobil_shutdown, |
| 122 | .ioctl = kobil_ioctl, |
Alan Cox | 94d0f7e | 2007-08-22 23:09:16 +0100 | [diff] [blame] | 123 | .set_termios = kobil_set_termios, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | .tiocmget = kobil_tiocmget, |
| 125 | .tiocmset = kobil_tiocmset, |
| 126 | .open = kobil_open, |
| 127 | .close = kobil_close, |
| 128 | .write = kobil_write, |
| 129 | .write_room = kobil_write_room, |
| 130 | .read_int_callback = kobil_read_int_callback, |
| 131 | }; |
| 132 | |
| 133 | |
| 134 | struct kobil_private { |
| 135 | int write_int_endpoint_address; |
| 136 | int read_int_endpoint_address; |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 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 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | __u16 device_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 144 | static int kobil_startup(struct usb_serial *serial) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | { |
| 146 | int i; |
| 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; |
| 153 | |
| 154 | priv = kmalloc(sizeof(struct kobil_private), GFP_KERNEL); |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 155 | if (!priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
| 158 | priv->filled = 0; |
| 159 | priv->cur_pos = 0; |
| 160 | priv->device_type = le16_to_cpu(serial->dev->descriptor.idProduct); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 162 | switch (priv->device_type) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | case KOBIL_ADAPTER_B_PRODUCT_ID: |
| 164 | printk(KERN_DEBUG "KOBIL B1 PRO / KAAN PRO detected\n"); |
| 165 | break; |
| 166 | case KOBIL_ADAPTER_K_PRODUCT_ID: |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 167 | printk(KERN_DEBUG |
| 168 | "KOBIL KAAN Standard Plus / SecOVID Reader Plus detected\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | break; |
| 170 | case KOBIL_USBTWIN_PRODUCT_ID: |
| 171 | printk(KERN_DEBUG "KOBIL USBTWIN detected\n"); |
| 172 | break; |
| 173 | case KOBIL_KAAN_SIM_PRODUCT_ID: |
| 174 | printk(KERN_DEBUG "KOBIL KAAN SIM detected\n"); |
| 175 | break; |
| 176 | } |
| 177 | usb_set_serial_port_data(serial->port[0], priv); |
| 178 | |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 179 | /* search for the necessary endpoints */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | pdev = serial->dev; |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 181 | actconfig = pdev->actconfig; |
| 182 | interface = actconfig->interface[0]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | altsetting = interface->cur_altsetting; |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 184 | endpoint = altsetting->endpoint; |
| 185 | |
| 186 | for (i = 0; i < altsetting->desc.bNumEndpoints; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | endpoint = &altsetting->endpoint[i]; |
Luiz Fernando N. Capitulino | 4f1f1dd | 2006-10-26 13:02:53 -0300 | [diff] [blame] | 188 | if (usb_endpoint_is_int_out(&endpoint->desc)) { |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 189 | dbg("%s Found interrupt out endpoint. Address: %d", |
| 190 | __func__, endpoint->desc.bEndpointAddress); |
| 191 | priv->write_int_endpoint_address = |
| 192 | endpoint->desc.bEndpointAddress; |
| 193 | } |
Luiz Fernando N. Capitulino | 4f1f1dd | 2006-10-26 13:02:53 -0300 | [diff] [blame] | 194 | if (usb_endpoint_is_int_in(&endpoint->desc)) { |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 195 | dbg("%s Found interrupt in endpoint. Address: %d", |
| 196 | __func__, endpoint->desc.bEndpointAddress); |
| 197 | priv->read_int_endpoint_address = |
| 198 | endpoint->desc.bEndpointAddress; |
| 199 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | } |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 205 | static void kobil_shutdown(struct usb_serial *serial) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | { |
| 207 | int i; |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 208 | dbg("%s - port %d", __func__, serial->port[0]->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | kfree(usb_get_serial_port_data(serial->port[i])); |
| 214 | usb_set_serial_port_data(serial->port[i], NULL); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 219 | static int kobil_open(struct tty_struct *tty, |
| 220 | struct usb_serial_port *port, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | { |
Alan Cox | 94d0f7e | 2007-08-22 23:09:16 +0100 | [diff] [blame] | 222 | int result = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | struct kobil_private *priv; |
| 224 | unsigned char *transfer_buffer; |
| 225 | int transfer_buffer_length = 8; |
| 226 | int write_urb_transfer_buffer_length = 8; |
| 227 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 228 | dbg("%s - port %d", __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | priv = usb_get_serial_port_data(port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 231 | /* someone sets the dev to 0 if the close method has been called */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | port->interrupt_in_urb->dev = port->serial->dev; |
| 233 | |
| 234 | |
| 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. |
| 238 | */ |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 239 | if (tty) { |
| 240 | tty->low_latency = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 242 | /* Default to echo off and other sane device settings */ |
| 243 | tty->termios->c_lflag = 0; |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 244 | tty->termios->c_lflag &= ~(ISIG | ICANON | ECHO | IEXTEN | |
| 245 | XCASE); |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 246 | tty->termios->c_iflag = IGNBRK | IGNPAR | IXOFF; |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 247 | /* do NOT translate CR to CR-NL (0x0A -> 0x0A 0x0D) */ |
| 248 | tty->termios->c_oflag &= ~ONLCR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | } |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 250 | /* allocate memory for transfer buffer */ |
| 251 | transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL); |
| 252 | if (!transfer_buffer) |
| 253 | return -ENOMEM; |
| 254 | |
| 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | if (!port->write_urb) { |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 261 | dbg("%s - port %d usb_alloc_urb failed", |
| 262 | __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | kfree(transfer_buffer); |
| 264 | return -ENOMEM; |
| 265 | } |
| 266 | } |
| 267 | |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 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) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | kfree(transfer_buffer); |
| 273 | usb_free_urb(port->write_urb); |
| 274 | port->write_urb = NULL; |
| 275 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | } |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 277 | |
| 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, |
| 284 | 0, |
| 285 | transfer_buffer, |
| 286 | transfer_buffer_length, |
| 287 | KOBIL_TIMEOUT |
| 288 | ); |
| 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]); |
| 293 | |
| 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, |
| 300 | 0, |
| 301 | transfer_buffer, |
| 302 | transfer_buffer_length, |
| 303 | KOBIL_TIMEOUT |
| 304 | ); |
| 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]); |
| 309 | |
| 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, |
| 319 | 0, |
| 320 | transfer_buffer, |
| 321 | 0, |
| 322 | KOBIL_TIMEOUT |
| 323 | ); |
| 324 | dbg("%s - port %d Send set_baudrate URB returns: %i", |
| 325 | __func__, port->number, result); |
| 326 | |
| 327 | /* reset all queues */ |
| 328 | result = usb_control_msg(port->serial->dev, |
| 329 | usb_rcvctrlpipe(port->serial->dev, 0), |
| 330 | SUSBCRequest_Misc, |
| 331 | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT, |
| 332 | SUSBCR_MSC_ResetAllQueues, |
| 333 | 0, |
| 334 | transfer_buffer, |
| 335 | 0, |
| 336 | KOBIL_TIMEOUT |
| 337 | ); |
| 338 | dbg("%s - port %d Send reset_all_queues URB returns: %i", |
| 339 | __func__, port->number, result); |
| 340 | } |
| 341 | if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID || |
| 342 | priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID) { |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | kfree(transfer_buffer); |
| 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 355 | static void kobil_close(struct tty_struct *tty, |
| 356 | struct usb_serial_port *port, struct file *filp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 358 | dbg("%s - port %d", __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | |
| 360 | if (port->write_urb) { |
| 361 | usb_kill_urb(port->write_urb); |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 362 | usb_free_urb(port->write_urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | port->write_urb = NULL; |
| 364 | } |
Mariusz Kozlowski | 5505c22 | 2006-11-08 15:36:38 +0100 | [diff] [blame] | 365 | usb_kill_urb(port->interrupt_in_urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | |
Greg Kroah-Hartman | 6fcdcf0 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 369 | static void kobil_read_int_callback(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | int result; |
Greg Kroah-Hartman | 6fcdcf0 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 372 | struct usb_serial_port *port = urb->context; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | struct tty_struct *tty; |
Greg Kroah-Hartman | 6fcdcf0 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 374 | unsigned char *data = urb->transfer_buffer; |
| 375 | int status = urb->status; |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 376 | /* char *dbg_data; */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 378 | dbg("%s - port %d", __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | |
Greg Kroah-Hartman | 6fcdcf0 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 380 | if (status) { |
| 381 | dbg("%s - port %d Read int status not zero: %d", |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 382 | __func__, port->number, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | return; |
| 384 | } |
Greg Kroah-Hartman | 6fcdcf0 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 385 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 386 | tty = port->port.tty; |
Greg Kroah-Hartman | 6fcdcf0 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 387 | if (urb->actual_length) { |
| 388 | |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 389 | /* BEGIN DEBUG */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | /* |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 391 | dbg_data = kzalloc((3 * purb->actual_length + 10) |
| 392 | * sizeof(char), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | if (! dbg_data) { |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 394 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | } |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 396 | for (i = 0; i < purb->actual_length; i++) { |
| 397 | sprintf(dbg_data +3*i, "%02X ", data[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | } |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 399 | dbg(" <-- %s", dbg_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | kfree(dbg_data); |
| 401 | */ |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 402 | /* END DEBUG */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | |
Greg Kroah-Hartman | 6fcdcf0 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 404 | tty_buffer_request_room(tty, urb->actual_length); |
| 405 | tty_insert_flip_string(tty, data, urb->actual_length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | tty_flip_buffer_push(tty); |
| 407 | } |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 408 | /* someone sets the dev to 0 if the close method has been called */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | port->interrupt_in_urb->dev = port->serial->dev; |
| 410 | |
Greg Kroah-Hartman | 6fcdcf0 | 2007-06-15 15:44:13 -0700 | [diff] [blame] | 411 | result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC); |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 412 | dbg("%s - port %d Send read URB returns: %i", |
| 413 | __func__, port->number, result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 417 | static void kobil_write_callback(struct urb *purb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | { |
| 419 | } |
| 420 | |
| 421 | |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 422 | static int kobil_write(struct tty_struct *tty, struct usb_serial_port *port, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | const unsigned char *buf, int count) |
| 424 | { |
| 425 | int length = 0; |
| 426 | int result = 0; |
| 427 | int todo = 0; |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 428 | struct kobil_private *priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | |
| 430 | if (count == 0) { |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 431 | dbg("%s - port %d write request of 0 bytes", |
| 432 | __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | priv = usb_get_serial_port_data(port); |
| 437 | |
| 438 | if (count > (KOBIL_BUF_LENGTH - priv->filled)) { |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 439 | dbg("%s - port %d Error: write request bigger than buffer size", __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | return -ENOMEM; |
| 441 | } |
| 442 | |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | priv->filled = priv->filled + count; |
| 448 | |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 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)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | usb_kill_urb(port->interrupt_in_urb); |
| 457 | |
| 458 | todo = priv->filled - priv->cur_pos; |
| 459 | |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 460 | while (todo > 0) { |
| 461 | /* max 8 byte in one urb (endpoint size) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | length = (todo < 8) ? todo : 8; |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 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, |
| 467 | port->serial->dev, |
| 468 | usb_sndintpipe(port->serial->dev, |
| 469 | priv->write_int_endpoint_address), |
| 470 | port->write_urb->transfer_buffer, |
| 471 | length, |
| 472 | kobil_write_callback, |
| 473 | port, |
| 474 | 8 |
| 475 | ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | |
| 477 | priv->cur_pos = priv->cur_pos + length; |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | todo = priv->filled - priv->cur_pos; |
| 482 | |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 483 | if (todo > 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | msleep(24); |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 485 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | priv->filled = 0; |
| 488 | priv->cur_pos = 0; |
| 489 | |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 490 | /* someone sets the dev to 0 if the close method |
| 491 | has been called */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | port->interrupt_in_urb->dev = port->serial->dev; |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 493 | |
| 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 |
| 498 | been called */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | port->interrupt_in_urb->dev = port->serial->dev; |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 500 | |
| 501 | result = usb_submit_urb(port->interrupt_in_urb, |
| 502 | GFP_NOIO); |
| 503 | dbg("%s - port %d Send read URB returns: %i", |
| 504 | __func__, port->number, result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | } |
| 506 | } |
| 507 | return count; |
| 508 | } |
| 509 | |
| 510 | |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 511 | static int kobil_write_room(struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | { |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 513 | /* dbg("%s - port %d", __func__, port->number); */ |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 514 | /* FIXME */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | return 8; |
| 516 | } |
| 517 | |
| 518 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 519 | static int kobil_tiocmget(struct tty_struct *tty, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 521 | struct usb_serial_port *port = tty->driver_data; |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 522 | struct kobil_private *priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | int result; |
| 524 | unsigned char *transfer_buffer; |
| 525 | int transfer_buffer_length = 8; |
| 526 | |
| 527 | priv = usb_get_serial_port_data(port); |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 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 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | return -EINVAL; |
| 532 | } |
| 533 | |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 534 | /* allocate memory for transfer buffer */ |
Eric Sesterhenn | 80b6ca4 | 2006-02-27 21:29:43 +0100 | [diff] [blame] | 535 | transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL); |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 536 | if (!transfer_buffer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 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, |
| 543 | 0, |
| 544 | 0, |
| 545 | transfer_buffer, |
| 546 | transfer_buffer_length, |
| 547 | KOBIL_TIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 549 | dbg("%s - port %d Send get_status_line_state URB returns: %i. Statusline: %02x", |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 550 | __func__, port->number, result, transfer_buffer[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | |
Alan Cox | a40d854 | 2008-02-20 21:40:34 +0000 | [diff] [blame] | 552 | result = 0; |
| 553 | if ((transfer_buffer[0] & SUSBCR_GSL_DSR) != 0) |
| 554 | result = TIOCM_DSR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | kfree(transfer_buffer); |
Alan Cox | a40d854 | 2008-02-20 21:40:34 +0000 | [diff] [blame] | 556 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | } |
| 558 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 559 | static int kobil_tiocmset(struct tty_struct *tty, struct file *file, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | unsigned int set, unsigned int clear) |
| 561 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 562 | struct usb_serial_port *port = tty->driver_data; |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 563 | struct kobil_private *priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | int result; |
| 565 | int dtr = 0; |
| 566 | int rts = 0; |
| 567 | unsigned char *transfer_buffer; |
| 568 | int transfer_buffer_length = 8; |
| 569 | |
Alan Cox | a40d854 | 2008-02-20 21:40:34 +0000 | [diff] [blame] | 570 | /* FIXME: locking ? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | priv = usb_get_serial_port_data(port); |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 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 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | return -EINVAL; |
| 576 | } |
| 577 | |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 578 | /* allocate memory for transfer buffer */ |
Eric Sesterhenn | 80b6ca4 | 2006-02-27 21:29:43 +0100 | [diff] [blame] | 579 | transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL); |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 580 | if (!transfer_buffer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | |
| 583 | if (set & TIOCM_RTS) |
| 584 | rts = 1; |
| 585 | if (set & TIOCM_DTR) |
| 586 | dtr = 1; |
| 587 | if (clear & TIOCM_RTS) |
| 588 | rts = 0; |
| 589 | if (clear & TIOCM_DTR) |
| 590 | dtr = 0; |
| 591 | |
| 592 | if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) { |
| 593 | if (dtr != 0) |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 594 | dbg("%s - port %d Setting DTR", |
| 595 | __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | else |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 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), |
| 604 | 0, |
| 605 | transfer_buffer, |
| 606 | 0, |
| 607 | KOBIL_TIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | } else { |
| 609 | if (rts != 0) |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 610 | dbg("%s - port %d Setting RTS", |
| 611 | __func__, port->number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | else |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 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), |
| 620 | 0, |
| 621 | transfer_buffer, |
| 622 | 0, |
| 623 | KOBIL_TIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | } |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 625 | dbg("%s - port %d Send set_status_line URB returns: %i", |
| 626 | __func__, port->number, result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | kfree(transfer_buffer); |
| 628 | return (result < 0) ? result : 0; |
| 629 | } |
| 630 | |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 631 | static void kobil_set_termios(struct tty_struct *tty, |
| 632 | struct usb_serial_port *port, struct ktermios *old) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | { |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 634 | struct kobil_private *priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | int result; |
| 636 | unsigned short urb_val = 0; |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 637 | int c_cflag = tty->termios->c_cflag; |
Alan Cox | 94d0f7e | 2007-08-22 23:09:16 +0100 | [diff] [blame] | 638 | speed_t speed; |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 639 | void *settings; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | |
| 641 | priv = usb_get_serial_port_data(port); |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 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 */ |
Alan Cox | 94d0f7e | 2007-08-22 23:09:16 +0100 | [diff] [blame] | 645 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 647 | speed = tty_get_baud_rate(tty); |
| 648 | switch (speed) { |
| 649 | case 1200: |
| 650 | urb_val = SUSBCR_SBR_1200; |
| 651 | break; |
| 652 | default: |
| 653 | speed = 9600; |
| 654 | case 9600: |
| 655 | urb_val = SUSBCR_SBR_9600; |
| 656 | break; |
Alan Cox | 94d0f7e | 2007-08-22 23:09:16 +0100 | [diff] [blame] | 657 | } |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 658 | urb_val |= (c_cflag & CSTOPB) ? SUSBCR_SPASB_2StopBits : |
| 659 | SUSBCR_SPASB_1StopBit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | |
Alan Cox | 94d0f7e | 2007-08-22 23:09:16 +0100 | [diff] [blame] | 661 | settings = kzalloc(50, GFP_KERNEL); |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 662 | if (!settings) |
Alan Cox | 94d0f7e | 2007-08-22 23:09:16 +0100 | [diff] [blame] | 663 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | |
Alan Cox | 94d0f7e | 2007-08-22 23:09:16 +0100 | [diff] [blame] | 665 | sprintf(settings, "%d ", speed); |
| 666 | |
| 667 | if (c_cflag & PARENB) { |
| 668 | if (c_cflag & PARODD) { |
| 669 | urb_val |= SUSBCR_SPASB_OddParity; |
| 670 | strcat(settings, "Odd Parity"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | } else { |
Alan Cox | 94d0f7e | 2007-08-22 23:09:16 +0100 | [diff] [blame] | 672 | urb_val |= SUSBCR_SPASB_EvenParity; |
| 673 | strcat(settings, "Even Parity"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | } |
Alan Cox | 94d0f7e | 2007-08-22 23:09:16 +0100 | [diff] [blame] | 675 | } else { |
| 676 | urb_val |= SUSBCR_SPASB_NoParity; |
| 677 | strcat(settings, "No Parity"); |
| 678 | } |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 679 | tty->termios->c_cflag &= ~CMSPAR; |
| 680 | tty_encode_baud_rate(tty, speed, speed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 682 | result = usb_control_msg(port->serial->dev, |
| 683 | usb_rcvctrlpipe(port->serial->dev, 0), |
| 684 | SUSBCRequest_SetBaudRateParityAndStopBits, |
| 685 | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT, |
| 686 | urb_val, |
| 687 | 0, |
| 688 | settings, |
| 689 | 0, |
| 690 | KOBIL_TIMEOUT |
Alan Cox | 94d0f7e | 2007-08-22 23:09:16 +0100 | [diff] [blame] | 691 | ); |
| 692 | kfree(settings); |
| 693 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 695 | static int kobil_ioctl(struct tty_struct *tty, struct file *file, |
| 696 | unsigned int cmd, unsigned long arg) |
Alan Cox | 94d0f7e | 2007-08-22 23:09:16 +0100 | [diff] [blame] | 697 | { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 698 | struct usb_serial_port *port = tty->driver_data; |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 699 | struct kobil_private *priv = usb_get_serial_port_data(port); |
Alan Cox | 94d0f7e | 2007-08-22 23:09:16 +0100 | [diff] [blame] | 700 | unsigned char *transfer_buffer; |
| 701 | int transfer_buffer_length = 8; |
| 702 | int result; |
| 703 | |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 704 | if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID || |
| 705 | priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID) |
| 706 | /* This device doesn't support ioctl calls */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | return 0; |
| 708 | |
Alan Cox | 94d0f7e | 2007-08-22 23:09:16 +0100 | [diff] [blame] | 709 | switch (cmd) { |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 710 | case TCFLSH: |
Robert P. J. Day | 5cbded5 | 2006-12-13 00:35:56 -0800 | [diff] [blame] | 711 | transfer_buffer = kmalloc(transfer_buffer_length, GFP_KERNEL); |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 712 | if (!transfer_buffer) |
| 713 | return -ENOBUFS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 715 | result = usb_control_msg(port->serial->dev, |
| 716 | usb_rcvctrlpipe(port->serial->dev, 0), |
| 717 | SUSBCRequest_Misc, |
| 718 | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT, |
| 719 | SUSBCR_MSC_ResetAllQueues, |
| 720 | 0, |
| 721 | NULL, /* transfer_buffer, */ |
| 722 | 0, |
| 723 | KOBIL_TIMEOUT |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | ); |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 725 | |
Harvey Harrison | 441b62c | 2008-03-03 16:08:34 -0800 | [diff] [blame] | 726 | dbg("%s - port %d Send reset_all_queues (FLUSH) URB returns: %i", __func__, port->number, result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | kfree(transfer_buffer); |
Alan Cox | 95da310 | 2008-07-22 11:09:07 +0100 | [diff] [blame] | 728 | return (result < 0) ? -EIO: 0; |
Alan Cox | 94d0f7e | 2007-08-22 23:09:16 +0100 | [diff] [blame] | 729 | default: |
| 730 | return -ENOIOCTLCMD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | } |
| 733 | |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 734 | static int __init kobil_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | { |
| 736 | int retval; |
| 737 | retval = usb_serial_register(&kobil_device); |
| 738 | if (retval) |
| 739 | goto failed_usb_serial_register; |
| 740 | retval = usb_register(&kobil_driver); |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 741 | if (retval) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | goto failed_usb_register; |
| 743 | |
| 744 | info(DRIVER_VERSION " " DRIVER_AUTHOR); |
| 745 | info(DRIVER_DESC); |
| 746 | |
| 747 | return 0; |
| 748 | failed_usb_register: |
| 749 | usb_serial_deregister(&kobil_device); |
| 750 | failed_usb_serial_register: |
| 751 | return retval; |
| 752 | } |
| 753 | |
| 754 | |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 755 | static void __exit kobil_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | { |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 757 | usb_deregister(&kobil_driver); |
| 758 | usb_serial_deregister(&kobil_device); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | module_init(kobil_init); |
| 762 | module_exit(kobil_exit); |
| 763 | |
Alan Cox | dee0a7c | 2008-07-22 11:14:10 +0100 | [diff] [blame^] | 764 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 765 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 766 | MODULE_LICENSE("GPL"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | |
| 768 | module_param(debug, bool, S_IRUGO | S_IWUSR); |
| 769 | MODULE_PARM_DESC(debug, "Debug enabled or not"); |