blob: a92a3efb507bdaa45f7134919b30c4bf8b495917 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * KOBIL USB Smart Card Terminal Driver
3 *
Alan Coxdee0a7c2008-07-22 11:14:10 +01004 * Copyright (C) 2002 KOBIL Systems GmbH
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 */
24
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/kernel.h>
27#include <linux/errno.h>
28#include <linux/init.h>
29#include <linux/slab.h>
30#include <linux/tty.h>
31#include <linux/tty_driver.h>
32#include <linux/tty_flip.h>
33#include <linux/module.h>
34#include <linux/spinlock.h>
Alan Coxdee0a7c2008-07-22 11:14:10 +010035#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070037#include <linux/usb/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "kobil_sct.h"
40
Felipe Balbi3297f862012-01-24 10:18:10 +020041static bool debug;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43/* Version Information */
44#define DRIVER_VERSION "21/05/2004"
45#define DRIVER_AUTHOR "KOBIL Systems GmbH - http://www.kobil.com"
46#define DRIVER_DESC "KOBIL USB Smart Card Terminal Driver (experimental)"
47
48#define KOBIL_VENDOR_ID 0x0D46
49#define KOBIL_ADAPTER_B_PRODUCT_ID 0x2011
50#define KOBIL_ADAPTER_K_PRODUCT_ID 0x2012
51#define KOBIL_USBTWIN_PRODUCT_ID 0x0078
52#define KOBIL_KAAN_SIM_PRODUCT_ID 0x0081
53
54#define KOBIL_TIMEOUT 500
55#define KOBIL_BUF_LENGTH 300
56
57
58/* Function prototypes */
Alan Coxdee0a7c2008-07-22 11:14:10 +010059static int kobil_startup(struct usb_serial *serial);
Alan Sternf9c99bb2009-06-02 11:53:55 -040060static void kobil_release(struct usb_serial *serial);
Alan Coxa509a7e2009-09-19 13:13:26 -070061static int kobil_open(struct tty_struct *tty, struct usb_serial_port *port);
Alan Cox335f8512009-06-11 12:26:29 +010062static void kobil_close(struct usb_serial_port *port);
Alan Coxdee0a7c2008-07-22 11:14:10 +010063static int kobil_write(struct tty_struct *tty, struct usb_serial_port *port,
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 const unsigned char *buf, int count);
Alan Cox95da3102008-07-22 11:09:07 +010065static int kobil_write_room(struct tty_struct *tty);
Alan Cox00a0d0d2011-02-14 16:27:06 +000066static int kobil_ioctl(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 unsigned int cmd, unsigned long arg);
Alan Cox60b33c12011-02-14 16:26:14 +000068static int kobil_tiocmget(struct tty_struct *tty);
Alan Cox20b9d172011-02-14 16:26:50 +000069static int kobil_tiocmset(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 unsigned int set, unsigned int clear);
Alan Coxdee0a7c2008-07-22 11:14:10 +010071static void kobil_read_int_callback(struct urb *urb);
72static void kobil_write_callback(struct urb *purb);
73static void kobil_set_termios(struct tty_struct *tty,
Alan Cox95da3102008-07-22 11:09:07 +010074 struct usb_serial_port *port, struct ktermios *old);
Alan Coxfe1ae7f2009-09-19 13:13:33 -070075static void kobil_init_termios(struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Németh Márton7d40d7e2010-01-10 15:34:24 +010077static const struct usb_device_id id_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_ADAPTER_B_PRODUCT_ID) },
79 { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_ADAPTER_K_PRODUCT_ID) },
80 { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_USBTWIN_PRODUCT_ID) },
81 { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_KAAN_SIM_PRODUCT_ID) },
82 { } /* Terminating entry */
83};
84
85
Alan Coxdee0a7c2008-07-22 11:14:10 +010086MODULE_DEVICE_TABLE(usb, id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88static struct usb_driver kobil_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 .name = "kobil",
90 .probe = usb_serial_probe,
91 .disconnect = usb_serial_disconnect,
92 .id_table = id_table,
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -080093 .no_dynamic_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -070094};
95
96
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -070097static struct usb_serial_driver kobil_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070098 .driver = {
99 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700100 .name = "kobil",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700101 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700102 .description = "KOBIL USB smart card terminal",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100103 .usb_driver = &kobil_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 .id_table = id_table,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 .num_ports = 1,
106 .attach = kobil_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -0400107 .release = kobil_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 .ioctl = kobil_ioctl,
Alan Cox94d0f7e2007-08-22 23:09:16 +0100109 .set_termios = kobil_set_termios,
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700110 .init_termios = kobil_init_termios,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 .tiocmget = kobil_tiocmget,
112 .tiocmset = kobil_tiocmset,
113 .open = kobil_open,
114 .close = kobil_close,
115 .write = kobil_write,
116 .write_room = kobil_write_room,
117 .read_int_callback = kobil_read_int_callback,
118};
119
120
121struct kobil_private {
122 int write_int_endpoint_address;
123 int read_int_endpoint_address;
Alan Coxdee0a7c2008-07-22 11:14:10 +0100124 unsigned char buf[KOBIL_BUF_LENGTH]; /* buffer for the APDU to send */
125 int filled; /* index of the last char in buf */
126 int cur_pos; /* index of the next char to send in buf */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 __u16 device_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128};
129
130
Alan Coxdee0a7c2008-07-22 11:14:10 +0100131static int kobil_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
133 int i;
134 struct kobil_private *priv;
135 struct usb_device *pdev;
136 struct usb_host_config *actconfig;
137 struct usb_interface *interface;
138 struct usb_host_interface *altsetting;
139 struct usb_host_endpoint *endpoint;
140
141 priv = kmalloc(sizeof(struct kobil_private), GFP_KERNEL);
Alan Coxdee0a7c2008-07-22 11:14:10 +0100142 if (!priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 priv->filled = 0;
146 priv->cur_pos = 0;
147 priv->device_type = le16_to_cpu(serial->dev->descriptor.idProduct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Alan Coxdee0a7c2008-07-22 11:14:10 +0100149 switch (priv->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 case KOBIL_ADAPTER_B_PRODUCT_ID:
151 printk(KERN_DEBUG "KOBIL B1 PRO / KAAN PRO detected\n");
152 break;
153 case KOBIL_ADAPTER_K_PRODUCT_ID:
Alan Coxdee0a7c2008-07-22 11:14:10 +0100154 printk(KERN_DEBUG
155 "KOBIL KAAN Standard Plus / SecOVID Reader Plus detected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 break;
157 case KOBIL_USBTWIN_PRODUCT_ID:
158 printk(KERN_DEBUG "KOBIL USBTWIN detected\n");
159 break;
160 case KOBIL_KAAN_SIM_PRODUCT_ID:
161 printk(KERN_DEBUG "KOBIL KAAN SIM detected\n");
162 break;
163 }
164 usb_set_serial_port_data(serial->port[0], priv);
165
Alan Coxdee0a7c2008-07-22 11:14:10 +0100166 /* search for the necessary endpoints */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 pdev = serial->dev;
Alan Coxdee0a7c2008-07-22 11:14:10 +0100168 actconfig = pdev->actconfig;
169 interface = actconfig->interface[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 altsetting = interface->cur_altsetting;
Alan Coxdee0a7c2008-07-22 11:14:10 +0100171 endpoint = altsetting->endpoint;
172
173 for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 endpoint = &altsetting->endpoint[i];
Luiz Fernando N. Capitulino4f1f1dd2006-10-26 13:02:53 -0300175 if (usb_endpoint_is_int_out(&endpoint->desc)) {
Alan Coxdee0a7c2008-07-22 11:14:10 +0100176 dbg("%s Found interrupt out endpoint. Address: %d",
177 __func__, endpoint->desc.bEndpointAddress);
178 priv->write_int_endpoint_address =
179 endpoint->desc.bEndpointAddress;
180 }
Luiz Fernando N. Capitulino4f1f1dd2006-10-26 13:02:53 -0300181 if (usb_endpoint_is_int_in(&endpoint->desc)) {
Alan Coxdee0a7c2008-07-22 11:14:10 +0100182 dbg("%s Found interrupt in endpoint. Address: %d",
183 __func__, endpoint->desc.bEndpointAddress);
184 priv->read_int_endpoint_address =
185 endpoint->desc.bEndpointAddress;
186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
188 return 0;
189}
190
191
Alan Sternf9c99bb2009-06-02 11:53:55 -0400192static void kobil_release(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
194 int i;
Harvey Harrison441b62c2008-03-03 16:08:34 -0800195 dbg("%s - port %d", __func__, serial->port[0]->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Alan Sternf9c99bb2009-06-02 11:53:55 -0400197 for (i = 0; i < serial->num_ports; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 kfree(usb_get_serial_port_data(serial->port[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
Alan Coxfe1ae7f2009-09-19 13:13:33 -0700201static void kobil_init_termios(struct tty_struct *tty)
202{
203 /* Default to echo off and other sane device settings */
204 tty->termios->c_lflag = 0;
205 tty->termios->c_lflag &= ~(ISIG | ICANON | ECHO | IEXTEN | XCASE);
206 tty->termios->c_iflag = IGNBRK | IGNPAR | IXOFF;
207 /* do NOT translate CR to CR-NL (0x0A -> 0x0A 0x0D) */
208 tty->termios->c_oflag &= ~ONLCR;
209}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Alan Coxa509a7e2009-09-19 13:13:26 -0700211static int kobil_open(struct tty_struct *tty, struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
Alan Cox94d0f7e2007-08-22 23:09:16 +0100213 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 struct kobil_private *priv;
215 unsigned char *transfer_buffer;
216 int transfer_buffer_length = 8;
217 int write_urb_transfer_buffer_length = 8;
218
Harvey Harrison441b62c2008-03-03 16:08:34 -0800219 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 priv = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Alan Coxdee0a7c2008-07-22 11:14:10 +0100222 /* allocate memory for transfer buffer */
223 transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
224 if (!transfer_buffer)
225 return -ENOMEM;
226
227 /* allocate write_urb */
228 if (!port->write_urb) {
229 dbg("%s - port %d Allocating port->write_urb",
230 __func__, port->number);
231 port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 if (!port->write_urb) {
Alan Coxdee0a7c2008-07-22 11:14:10 +0100233 dbg("%s - port %d usb_alloc_urb failed",
234 __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 kfree(transfer_buffer);
236 return -ENOMEM;
237 }
238 }
239
Alan Coxdee0a7c2008-07-22 11:14:10 +0100240 /* allocate memory for write_urb transfer buffer */
241 port->write_urb->transfer_buffer =
242 kmalloc(write_urb_transfer_buffer_length, GFP_KERNEL);
243 if (!port->write_urb->transfer_buffer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 kfree(transfer_buffer);
245 usb_free_urb(port->write_urb);
246 port->write_urb = NULL;
247 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
Alan Coxdee0a7c2008-07-22 11:14:10 +0100249
250 /* get hardware version */
251 result = usb_control_msg(port->serial->dev,
252 usb_rcvctrlpipe(port->serial->dev, 0),
253 SUSBCRequest_GetMisc,
254 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
255 SUSBCR_MSC_GetHWVersion,
256 0,
257 transfer_buffer,
258 transfer_buffer_length,
259 KOBIL_TIMEOUT
260 );
261 dbg("%s - port %d Send get_HW_version URB returns: %i",
262 __func__, port->number, result);
263 dbg("Harware version: %i.%i.%i",
264 transfer_buffer[0], transfer_buffer[1], transfer_buffer[2]);
265
266 /* get firmware version */
267 result = usb_control_msg(port->serial->dev,
268 usb_rcvctrlpipe(port->serial->dev, 0),
269 SUSBCRequest_GetMisc,
270 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
271 SUSBCR_MSC_GetFWVersion,
272 0,
273 transfer_buffer,
274 transfer_buffer_length,
275 KOBIL_TIMEOUT
276 );
277 dbg("%s - port %d Send get_FW_version URB returns: %i",
278 __func__, port->number, result);
279 dbg("Firmware version: %i.%i.%i",
280 transfer_buffer[0], transfer_buffer[1], transfer_buffer[2]);
281
282 if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID ||
283 priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) {
284 /* Setting Baudrate, Parity and Stopbits */
285 result = usb_control_msg(port->serial->dev,
286 usb_rcvctrlpipe(port->serial->dev, 0),
287 SUSBCRequest_SetBaudRateParityAndStopBits,
288 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
289 SUSBCR_SBR_9600 | SUSBCR_SPASB_EvenParity |
290 SUSBCR_SPASB_1StopBit,
291 0,
292 transfer_buffer,
293 0,
294 KOBIL_TIMEOUT
295 );
296 dbg("%s - port %d Send set_baudrate URB returns: %i",
297 __func__, port->number, result);
298
299 /* reset all queues */
300 result = usb_control_msg(port->serial->dev,
301 usb_rcvctrlpipe(port->serial->dev, 0),
302 SUSBCRequest_Misc,
303 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
304 SUSBCR_MSC_ResetAllQueues,
305 0,
306 transfer_buffer,
307 0,
308 KOBIL_TIMEOUT
309 );
310 dbg("%s - port %d Send reset_all_queues URB returns: %i",
311 __func__, port->number, result);
312 }
313 if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID ||
314 priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID) {
Alan Coxdee0a7c2008-07-22 11:14:10 +0100316 /* start reading (Adapter B 'cause PNP string) */
317 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
318 dbg("%s - port %d Send read URB returns: %i",
319 __func__, port->number, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 }
321
322 kfree(transfer_buffer);
323 return 0;
324}
325
326
Alan Cox335f8512009-06-11 12:26:29 +0100327static void kobil_close(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
Harvey Harrison441b62c2008-03-03 16:08:34 -0800329 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Alan Cox335f8512009-06-11 12:26:29 +0100331 /* FIXME: Add rts/dtr methods */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 if (port->write_urb) {
Johan Hovoldc0f631d2010-05-15 17:53:43 +0200333 usb_poison_urb(port->write_urb);
334 kfree(port->write_urb->transfer_buffer);
Alan Coxdee0a7c2008-07-22 11:14:10 +0100335 usb_free_urb(port->write_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 port->write_urb = NULL;
337 }
Mariusz Kozlowski5505c222006-11-08 15:36:38 +0100338 usb_kill_urb(port->interrupt_in_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
341
Greg Kroah-Hartman6fcdcf02007-06-15 15:44:13 -0700342static void kobil_read_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 int result;
Greg Kroah-Hartman6fcdcf02007-06-15 15:44:13 -0700345 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 struct tty_struct *tty;
Greg Kroah-Hartman6fcdcf02007-06-15 15:44:13 -0700347 unsigned char *data = urb->transfer_buffer;
348 int status = urb->status;
Alan Coxdee0a7c2008-07-22 11:14:10 +0100349/* char *dbg_data; */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Harvey Harrison441b62c2008-03-03 16:08:34 -0800351 dbg("%s - port %d", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Greg Kroah-Hartman6fcdcf02007-06-15 15:44:13 -0700353 if (status) {
354 dbg("%s - port %d Read int status not zero: %d",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800355 __func__, port->number, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return;
357 }
Greg Kroah-Hartman6fcdcf02007-06-15 15:44:13 -0700358
Alan Cox4a90f092008-10-13 10:39:46 +0100359 tty = tty_port_tty_get(&port->port);
Jiri Slaby6960f402011-02-28 10:34:06 +0100360 if (tty && urb->actual_length) {
Greg Kroah-Hartman6fcdcf02007-06-15 15:44:13 -0700361
Alan Coxdee0a7c2008-07-22 11:14:10 +0100362 /* BEGIN DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 /*
Alan Coxdee0a7c2008-07-22 11:14:10 +0100364 dbg_data = kzalloc((3 * purb->actual_length + 10)
365 * sizeof(char), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if (! dbg_data) {
Alan Coxdee0a7c2008-07-22 11:14:10 +0100367 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
Alan Coxdee0a7c2008-07-22 11:14:10 +0100369 for (i = 0; i < purb->actual_length; i++) {
370 sprintf(dbg_data +3*i, "%02X ", data[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
Alan Coxdee0a7c2008-07-22 11:14:10 +0100372 dbg(" <-- %s", dbg_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 kfree(dbg_data);
374 */
Alan Coxdee0a7c2008-07-22 11:14:10 +0100375 /* END DEBUG */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Greg Kroah-Hartman6fcdcf02007-06-15 15:44:13 -0700377 tty_insert_flip_string(tty, data, urb->actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 tty_flip_buffer_push(tty);
379 }
Alan Cox4a90f092008-10-13 10:39:46 +0100380 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Greg Kroah-Hartman6fcdcf02007-06-15 15:44:13 -0700382 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
Alan Coxdee0a7c2008-07-22 11:14:10 +0100383 dbg("%s - port %d Send read URB returns: %i",
384 __func__, port->number, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385}
386
387
Alan Coxdee0a7c2008-07-22 11:14:10 +0100388static void kobil_write_callback(struct urb *purb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
390}
391
392
Alan Coxdee0a7c2008-07-22 11:14:10 +0100393static int kobil_write(struct tty_struct *tty, struct usb_serial_port *port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 const unsigned char *buf, int count)
395{
396 int length = 0;
397 int result = 0;
398 int todo = 0;
Alan Coxdee0a7c2008-07-22 11:14:10 +0100399 struct kobil_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 if (count == 0) {
Alan Coxdee0a7c2008-07-22 11:14:10 +0100402 dbg("%s - port %d write request of 0 bytes",
403 __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 return 0;
405 }
406
407 priv = usb_get_serial_port_data(port);
408
409 if (count > (KOBIL_BUF_LENGTH - priv->filled)) {
Harvey Harrison441b62c2008-03-03 16:08:34 -0800410 dbg("%s - port %d Error: write request bigger than buffer size", __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 return -ENOMEM;
412 }
413
Alan Coxdee0a7c2008-07-22 11:14:10 +0100414 /* Copy data to buffer */
415 memcpy(priv->buf + priv->filled, buf, count);
416 usb_serial_debug_data(debug, &port->dev, __func__, count,
417 priv->buf + priv->filled);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 priv->filled = priv->filled + count;
419
Alan Coxdee0a7c2008-07-22 11:14:10 +0100420 /* only send complete block. TWIN, KAAN SIM and adapter K
421 use the same protocol. */
422 if (((priv->device_type != KOBIL_ADAPTER_B_PRODUCT_ID) && (priv->filled > 2) && (priv->filled >= (priv->buf[1] + 3))) ||
423 ((priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) && (priv->filled > 3) && (priv->filled >= (priv->buf[2] + 4)))) {
424 /* stop reading (except TWIN and KAAN SIM) */
425 if ((priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID)
426 || (priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 usb_kill_urb(port->interrupt_in_urb);
428
429 todo = priv->filled - priv->cur_pos;
430
Alan Coxdee0a7c2008-07-22 11:14:10 +0100431 while (todo > 0) {
432 /* max 8 byte in one urb (endpoint size) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 length = (todo < 8) ? todo : 8;
Alan Coxdee0a7c2008-07-22 11:14:10 +0100434 /* copy data to transfer buffer */
435 memcpy(port->write_urb->transfer_buffer,
436 priv->buf + priv->cur_pos, length);
437 usb_fill_int_urb(port->write_urb,
438 port->serial->dev,
439 usb_sndintpipe(port->serial->dev,
440 priv->write_int_endpoint_address),
441 port->write_urb->transfer_buffer,
442 length,
443 kobil_write_callback,
444 port,
445 8
446 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 priv->cur_pos = priv->cur_pos + length;
Alan Coxdee0a7c2008-07-22 11:14:10 +0100449 result = usb_submit_urb(port->write_urb, GFP_NOIO);
450 dbg("%s - port %d Send write URB returns: %i",
451 __func__, port->number, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 todo = priv->filled - priv->cur_pos;
453
Alan Coxdee0a7c2008-07-22 11:14:10 +0100454 if (todo > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 msleep(24);
Alan Coxdee0a7c2008-07-22 11:14:10 +0100456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 priv->filled = 0;
459 priv->cur_pos = 0;
460
Alan Coxdee0a7c2008-07-22 11:14:10 +0100461 /* start reading (except TWIN and KAAN SIM) */
462 if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID ||
463 priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) {
Alan Coxdee0a7c2008-07-22 11:14:10 +0100464 result = usb_submit_urb(port->interrupt_in_urb,
465 GFP_NOIO);
466 dbg("%s - port %d Send read URB returns: %i",
467 __func__, port->number, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
469 }
470 return count;
471}
472
473
Alan Coxdee0a7c2008-07-22 11:14:10 +0100474static int kobil_write_room(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Alan Coxdee0a7c2008-07-22 11:14:10 +0100476 /* dbg("%s - port %d", __func__, port->number); */
Alan Cox95da3102008-07-22 11:09:07 +0100477 /* FIXME */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return 8;
479}
480
481
Alan Cox60b33c12011-02-14 16:26:14 +0000482static int kobil_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
Alan Cox95da3102008-07-22 11:09:07 +0100484 struct usb_serial_port *port = tty->driver_data;
Alan Coxdee0a7c2008-07-22 11:14:10 +0100485 struct kobil_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 int result;
487 unsigned char *transfer_buffer;
488 int transfer_buffer_length = 8;
489
490 priv = usb_get_serial_port_data(port);
Alan Coxdee0a7c2008-07-22 11:14:10 +0100491 if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID
492 || priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID) {
493 /* This device doesn't support ioctl calls */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 return -EINVAL;
495 }
496
Alan Coxdee0a7c2008-07-22 11:14:10 +0100497 /* allocate memory for transfer buffer */
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100498 transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
Alan Coxdee0a7c2008-07-22 11:14:10 +0100499 if (!transfer_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Alan Coxdee0a7c2008-07-22 11:14:10 +0100502 result = usb_control_msg(port->serial->dev,
503 usb_rcvctrlpipe(port->serial->dev, 0),
504 SUSBCRequest_GetStatusLineState,
505 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
506 0,
507 0,
508 transfer_buffer,
509 transfer_buffer_length,
510 KOBIL_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Alan Coxdee0a7c2008-07-22 11:14:10 +0100512 dbg("%s - port %d Send get_status_line_state URB returns: %i. Statusline: %02x",
Harvey Harrison441b62c2008-03-03 16:08:34 -0800513 __func__, port->number, result, transfer_buffer[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Alan Coxa40d8542008-02-20 21:40:34 +0000515 result = 0;
516 if ((transfer_buffer[0] & SUSBCR_GSL_DSR) != 0)
517 result = TIOCM_DSR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 kfree(transfer_buffer);
Alan Coxa40d8542008-02-20 21:40:34 +0000519 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
521
Alan Cox20b9d172011-02-14 16:26:50 +0000522static int kobil_tiocmset(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 unsigned int set, unsigned int clear)
524{
Alan Cox95da3102008-07-22 11:09:07 +0100525 struct usb_serial_port *port = tty->driver_data;
Alan Coxdee0a7c2008-07-22 11:14:10 +0100526 struct kobil_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 int result;
528 int dtr = 0;
529 int rts = 0;
530 unsigned char *transfer_buffer;
531 int transfer_buffer_length = 8;
532
Alan Coxa40d8542008-02-20 21:40:34 +0000533 /* FIXME: locking ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 priv = usb_get_serial_port_data(port);
Alan Coxdee0a7c2008-07-22 11:14:10 +0100535 if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID
536 || priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID) {
537 /* This device doesn't support ioctl calls */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return -EINVAL;
539 }
540
Alan Coxdee0a7c2008-07-22 11:14:10 +0100541 /* allocate memory for transfer buffer */
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100542 transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
Alan Coxdee0a7c2008-07-22 11:14:10 +0100543 if (!transfer_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 if (set & TIOCM_RTS)
547 rts = 1;
548 if (set & TIOCM_DTR)
549 dtr = 1;
550 if (clear & TIOCM_RTS)
551 rts = 0;
552 if (clear & TIOCM_DTR)
553 dtr = 0;
554
555 if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) {
556 if (dtr != 0)
Alan Coxdee0a7c2008-07-22 11:14:10 +0100557 dbg("%s - port %d Setting DTR",
558 __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 else
Alan Coxdee0a7c2008-07-22 11:14:10 +0100560 dbg("%s - port %d Clearing DTR",
561 __func__, port->number);
562 result = usb_control_msg(port->serial->dev,
563 usb_rcvctrlpipe(port->serial->dev, 0),
564 SUSBCRequest_SetStatusLinesOrQueues,
565 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
566 ((dtr != 0) ? SUSBCR_SSL_SETDTR : SUSBCR_SSL_CLRDTR),
567 0,
568 transfer_buffer,
569 0,
570 KOBIL_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 } else {
572 if (rts != 0)
Alan Coxdee0a7c2008-07-22 11:14:10 +0100573 dbg("%s - port %d Setting RTS",
574 __func__, port->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 else
Alan Coxdee0a7c2008-07-22 11:14:10 +0100576 dbg("%s - port %d Clearing RTS",
577 __func__, port->number);
578 result = usb_control_msg(port->serial->dev,
579 usb_rcvctrlpipe(port->serial->dev, 0),
580 SUSBCRequest_SetStatusLinesOrQueues,
581 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
582 ((rts != 0) ? SUSBCR_SSL_SETRTS : SUSBCR_SSL_CLRRTS),
583 0,
584 transfer_buffer,
585 0,
586 KOBIL_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 }
Alan Coxdee0a7c2008-07-22 11:14:10 +0100588 dbg("%s - port %d Send set_status_line URB returns: %i",
589 __func__, port->number, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 kfree(transfer_buffer);
591 return (result < 0) ? result : 0;
592}
593
Alan Cox95da3102008-07-22 11:09:07 +0100594static void kobil_set_termios(struct tty_struct *tty,
595 struct usb_serial_port *port, struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
Alan Coxdee0a7c2008-07-22 11:14:10 +0100597 struct kobil_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 int result;
599 unsigned short urb_val = 0;
Alan Cox95da3102008-07-22 11:09:07 +0100600 int c_cflag = tty->termios->c_cflag;
Alan Cox94d0f7e2007-08-22 23:09:16 +0100601 speed_t speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 priv = usb_get_serial_port_data(port);
Alan Coxdee0a7c2008-07-22 11:14:10 +0100604 if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID ||
Alan Coxb31f6582008-07-22 11:14:22 +0100605 priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID) {
Alan Coxdee0a7c2008-07-22 11:14:10 +0100606 /* This device doesn't support ioctl calls */
Alan Coxb31f6582008-07-22 11:14:22 +0100607 *tty->termios = *old;
Alan Cox94d0f7e2007-08-22 23:09:16 +0100608 return;
Alan Coxb31f6582008-07-22 11:14:22 +0100609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Alan Coxdee0a7c2008-07-22 11:14:10 +0100611 speed = tty_get_baud_rate(tty);
612 switch (speed) {
613 case 1200:
614 urb_val = SUSBCR_SBR_1200;
615 break;
616 default:
617 speed = 9600;
618 case 9600:
619 urb_val = SUSBCR_SBR_9600;
620 break;
Alan Cox94d0f7e2007-08-22 23:09:16 +0100621 }
Alan Coxdee0a7c2008-07-22 11:14:10 +0100622 urb_val |= (c_cflag & CSTOPB) ? SUSBCR_SPASB_2StopBits :
623 SUSBCR_SPASB_1StopBit;
Alan Cox94d0f7e2007-08-22 23:09:16 +0100624 if (c_cflag & PARENB) {
Johan Hovold96679f62009-12-28 23:01:58 +0100625 if (c_cflag & PARODD)
Alan Cox94d0f7e2007-08-22 23:09:16 +0100626 urb_val |= SUSBCR_SPASB_OddParity;
Johan Hovold96679f62009-12-28 23:01:58 +0100627 else
Alan Cox94d0f7e2007-08-22 23:09:16 +0100628 urb_val |= SUSBCR_SPASB_EvenParity;
Johan Hovold96679f62009-12-28 23:01:58 +0100629 } else
Alan Cox94d0f7e2007-08-22 23:09:16 +0100630 urb_val |= SUSBCR_SPASB_NoParity;
Alan Cox95da3102008-07-22 11:09:07 +0100631 tty->termios->c_cflag &= ~CMSPAR;
632 tty_encode_baud_rate(tty, speed, speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Alan Coxdee0a7c2008-07-22 11:14:10 +0100634 result = usb_control_msg(port->serial->dev,
635 usb_rcvctrlpipe(port->serial->dev, 0),
636 SUSBCRequest_SetBaudRateParityAndStopBits,
637 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
638 urb_val,
639 0,
Johan Hovold96679f62009-12-28 23:01:58 +0100640 NULL,
Alan Coxdee0a7c2008-07-22 11:14:10 +0100641 0,
642 KOBIL_TIMEOUT
Alan Cox94d0f7e2007-08-22 23:09:16 +0100643 );
Alan Cox94d0f7e2007-08-22 23:09:16 +0100644}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Alan Cox00a0d0d2011-02-14 16:27:06 +0000646static int kobil_ioctl(struct tty_struct *tty,
Alan Coxdee0a7c2008-07-22 11:14:10 +0100647 unsigned int cmd, unsigned long arg)
Alan Cox94d0f7e2007-08-22 23:09:16 +0100648{
Alan Cox95da3102008-07-22 11:09:07 +0100649 struct usb_serial_port *port = tty->driver_data;
Alan Coxdee0a7c2008-07-22 11:14:10 +0100650 struct kobil_private *priv = usb_get_serial_port_data(port);
Alan Cox94d0f7e2007-08-22 23:09:16 +0100651 unsigned char *transfer_buffer;
652 int transfer_buffer_length = 8;
653 int result;
654
Alan Coxdee0a7c2008-07-22 11:14:10 +0100655 if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID ||
656 priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID)
657 /* This device doesn't support ioctl calls */
Alan Coxb31f6582008-07-22 11:14:22 +0100658 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Alan Cox94d0f7e2007-08-22 23:09:16 +0100660 switch (cmd) {
Alan Cox95da3102008-07-22 11:09:07 +0100661 case TCFLSH:
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800662 transfer_buffer = kmalloc(transfer_buffer_length, GFP_KERNEL);
Alan Coxdee0a7c2008-07-22 11:14:10 +0100663 if (!transfer_buffer)
664 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Alan Coxdee0a7c2008-07-22 11:14:10 +0100666 result = usb_control_msg(port->serial->dev,
667 usb_rcvctrlpipe(port->serial->dev, 0),
668 SUSBCRequest_Misc,
669 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
670 SUSBCR_MSC_ResetAllQueues,
671 0,
672 NULL, /* transfer_buffer, */
673 0,
674 KOBIL_TIMEOUT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 );
Alan Coxdee0a7c2008-07-22 11:14:10 +0100676
Harvey Harrison441b62c2008-03-03 16:08:34 -0800677 dbg("%s - port %d Send reset_all_queues (FLUSH) URB returns: %i", __func__, port->number, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 kfree(transfer_buffer);
Alan Cox95da3102008-07-22 11:09:07 +0100679 return (result < 0) ? -EIO: 0;
Alan Cox94d0f7e2007-08-22 23:09:16 +0100680 default:
681 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
Alan Coxdee0a7c2008-07-22 11:14:10 +0100685static int __init kobil_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
687 int retval;
688 retval = usb_serial_register(&kobil_device);
689 if (retval)
690 goto failed_usb_serial_register;
691 retval = usb_register(&kobil_driver);
Alan Coxdee0a7c2008-07-22 11:14:10 +0100692 if (retval)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 goto failed_usb_register;
694
Greg Kroah-Hartmanc197a8d2008-08-18 13:21:04 -0700695 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
696 DRIVER_DESC "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 return 0;
699failed_usb_register:
700 usb_serial_deregister(&kobil_device);
701failed_usb_serial_register:
702 return retval;
703}
704
705
Alan Coxdee0a7c2008-07-22 11:14:10 +0100706static void __exit kobil_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
Alan Coxdee0a7c2008-07-22 11:14:10 +0100708 usb_deregister(&kobil_driver);
709 usb_serial_deregister(&kobil_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
711
712module_init(kobil_init);
713module_exit(kobil_exit);
714
Alan Coxdee0a7c2008-07-22 11:14:10 +0100715MODULE_AUTHOR(DRIVER_AUTHOR);
716MODULE_DESCRIPTION(DRIVER_DESC);
717MODULE_LICENSE("GPL");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719module_param(debug, bool, S_IRUGO | S_IWUSR);
720MODULE_PARM_DESC(debug, "Debug enabled or not");