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