blob: 03cb5dd8cbe3a497531eed9ad83113013ade695b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * KOBIL USB Smart Card Terminal Driver
3 *
4 * Copyright (C) 2002 KOBIL Systems GmbH
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)
23 *
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 Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/kernel.h>
39#include <linux/errno.h>
40#include <linux/init.h>
41#include <linux/slab.h>
42#include <linux/tty.h>
43#include <linux/tty_driver.h>
44#include <linux/tty_flip.h>
45#include <linux/module.h>
46#include <linux/spinlock.h>
47#include <asm/uaccess.h>
48#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070049#include <linux/usb/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include "kobil_sct.h"
52
53static 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 */
71static int kobil_startup (struct usb_serial *serial);
72static void kobil_shutdown (struct usb_serial *serial);
73static int kobil_open (struct usb_serial_port *port, struct file *filp);
74static void kobil_close (struct usb_serial_port *port, struct file *filp);
75static int kobil_write (struct usb_serial_port *port,
76 const unsigned char *buf, int count);
77static int kobil_write_room(struct usb_serial_port *port);
78static int kobil_ioctl(struct usb_serial_port *port, struct file *file,
79 unsigned int cmd, unsigned long arg);
80static int kobil_tiocmget(struct usb_serial_port *port, struct file *file);
81static int kobil_tiocmset(struct usb_serial_port *port, struct file *file,
82 unsigned int set, unsigned int clear);
David Howells7d12e782006-10-05 14:55:46 +010083static void kobil_read_int_callback( struct urb *urb );
84static void kobil_write_callback( struct urb *purb );
Alan Cox94d0f7e2007-08-22 23:09:16 +010085static void kobil_set_termios(struct usb_serial_port *port, struct ktermios *old);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87
88static struct usb_device_id id_table [] = {
89 { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_ADAPTER_B_PRODUCT_ID) },
90 { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_ADAPTER_K_PRODUCT_ID) },
91 { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_USBTWIN_PRODUCT_ID) },
92 { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_KAAN_SIM_PRODUCT_ID) },
93 { } /* Terminating entry */
94};
95
96
97MODULE_DEVICE_TABLE (usb, id_table);
98
99static struct usb_driver kobil_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 .name = "kobil",
101 .probe = usb_serial_probe,
102 .disconnect = usb_serial_disconnect,
103 .id_table = id_table,
Greg Kroah-Hartmanba9dc652005-11-16 13:41:28 -0800104 .no_dynamic_id = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105};
106
107
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -0700108static struct usb_serial_driver kobil_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700109 .driver = {
110 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700111 .name = "kobil",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -0700112 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -0700113 .description = "KOBIL USB smart card terminal",
Johannes Hölzld9b1b782006-12-17 21:50:24 +0100114 .usb_driver = &kobil_driver,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 .id_table = id_table,
116 .num_interrupt_in = NUM_DONT_CARE,
Stefan Bader8dd70702008-01-25 08:09:41 -0500117 .num_interrupt_out = NUM_DONT_CARE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 .num_bulk_in = 0,
119 .num_bulk_out = 0,
120 .num_ports = 1,
121 .attach = kobil_startup,
122 .shutdown = kobil_shutdown,
123 .ioctl = kobil_ioctl,
Alan Cox94d0f7e2007-08-22 23:09:16 +0100124 .set_termios = kobil_set_termios,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 .tiocmget = kobil_tiocmget,
126 .tiocmset = kobil_tiocmset,
127 .open = kobil_open,
128 .close = kobil_close,
129 .write = kobil_write,
130 .write_room = kobil_write_room,
131 .read_int_callback = kobil_read_int_callback,
132};
133
134
135struct kobil_private {
136 int write_int_endpoint_address;
137 int read_int_endpoint_address;
138 unsigned char buf[KOBIL_BUF_LENGTH]; // buffer for the APDU to send
139 int filled; // index of the last char in buf
140 int cur_pos; // index of the next char to send in buf
141 __u16 device_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142};
143
144
145static int kobil_startup (struct usb_serial *serial)
146{
147 int i;
148 struct kobil_private *priv;
149 struct usb_device *pdev;
150 struct usb_host_config *actconfig;
151 struct usb_interface *interface;
152 struct usb_host_interface *altsetting;
153 struct usb_host_endpoint *endpoint;
154
155 priv = kmalloc(sizeof(struct kobil_private), GFP_KERNEL);
156 if (!priv){
157 return -ENOMEM;
158 }
159
160 priv->filled = 0;
161 priv->cur_pos = 0;
162 priv->device_type = le16_to_cpu(serial->dev->descriptor.idProduct);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 switch (priv->device_type){
165 case KOBIL_ADAPTER_B_PRODUCT_ID:
166 printk(KERN_DEBUG "KOBIL B1 PRO / KAAN PRO detected\n");
167 break;
168 case KOBIL_ADAPTER_K_PRODUCT_ID:
169 printk(KERN_DEBUG "KOBIL KAAN Standard Plus / SecOVID Reader Plus detected\n");
170 break;
171 case KOBIL_USBTWIN_PRODUCT_ID:
172 printk(KERN_DEBUG "KOBIL USBTWIN detected\n");
173 break;
174 case KOBIL_KAAN_SIM_PRODUCT_ID:
175 printk(KERN_DEBUG "KOBIL KAAN SIM detected\n");
176 break;
177 }
178 usb_set_serial_port_data(serial->port[0], priv);
179
180 // search for the necessary endpoints
181 pdev = serial->dev;
182 actconfig = pdev->actconfig;
183 interface = actconfig->interface[0];
184 altsetting = interface->cur_altsetting;
185 endpoint = altsetting->endpoint;
186
187 for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
188 endpoint = &altsetting->endpoint[i];
Luiz Fernando N. Capitulino4f1f1dd2006-10-26 13:02:53 -0300189 if (usb_endpoint_is_int_out(&endpoint->desc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 dbg("%s Found interrupt out endpoint. Address: %d", __FUNCTION__, endpoint->desc.bEndpointAddress);
191 priv->write_int_endpoint_address = endpoint->desc.bEndpointAddress;
192 }
Luiz Fernando N. Capitulino4f1f1dd2006-10-26 13:02:53 -0300193 if (usb_endpoint_is_int_in(&endpoint->desc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 dbg("%s Found interrupt in endpoint. Address: %d", __FUNCTION__, endpoint->desc.bEndpointAddress);
195 priv->read_int_endpoint_address = endpoint->desc.bEndpointAddress;
196 }
197 }
198 return 0;
199}
200
201
202static void kobil_shutdown (struct usb_serial *serial)
203{
204 int i;
205 dbg("%s - port %d", __FUNCTION__, serial->port[0]->number);
206
207 for (i=0; i < serial->num_ports; ++i) {
208 while (serial->port[i]->open_count > 0) {
209 kobil_close (serial->port[i], NULL);
210 }
211 kfree(usb_get_serial_port_data(serial->port[i]));
212 usb_set_serial_port_data(serial->port[i], NULL);
213 }
214}
215
216
217static int kobil_open (struct usb_serial_port *port, struct file *filp)
218{
Alan Cox94d0f7e2007-08-22 23:09:16 +0100219 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 struct kobil_private *priv;
221 unsigned char *transfer_buffer;
222 int transfer_buffer_length = 8;
223 int write_urb_transfer_buffer_length = 8;
224
225 dbg("%s - port %d", __FUNCTION__, port->number);
226 priv = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 // someone sets the dev to 0 if the close method has been called
229 port->interrupt_in_urb->dev = port->serial->dev;
230
231
232 /* force low_latency on so that our tty_push actually forces
233 * the data through, otherwise it is scheduled, and with high
234 * data rates (like with OHCI) data can get lost.
235 */
236 port->tty->low_latency = 1;
237
238 // without this, every push_tty_char is echoed :-(
239 port->tty->termios->c_lflag = 0;
240 port->tty->termios->c_lflag &= ~(ISIG | ICANON | ECHO | IEXTEN | XCASE);
241 port->tty->termios->c_iflag = IGNBRK | IGNPAR | IXOFF;
242 port->tty->termios->c_oflag &= ~ONLCR; // do NOT translate CR to CR-NL (0x0A -> 0x0A 0x0D)
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 // allocate memory for transfer buffer
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100245 transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 if (! transfer_buffer) {
247 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
249
250 // allocate write_urb
251 if (!port->write_urb) {
252 dbg("%s - port %d Allocating port->write_urb", __FUNCTION__, port->number);
253 port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
254 if (!port->write_urb) {
255 dbg("%s - port %d usb_alloc_urb failed", __FUNCTION__, port->number);
256 kfree(transfer_buffer);
257 return -ENOMEM;
258 }
259 }
260
261 // allocate memory for write_urb transfer buffer
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800262 port->write_urb->transfer_buffer = kmalloc(write_urb_transfer_buffer_length, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if (! port->write_urb->transfer_buffer) {
264 kfree(transfer_buffer);
265 usb_free_urb(port->write_urb);
266 port->write_urb = NULL;
267 return -ENOMEM;
268 }
269
270 // get hardware version
271 result = usb_control_msg( port->serial->dev,
272 usb_rcvctrlpipe(port->serial->dev, 0 ),
273 SUSBCRequest_GetMisc,
274 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
275 SUSBCR_MSC_GetHWVersion,
276 0,
277 transfer_buffer,
278 transfer_buffer_length,
279 KOBIL_TIMEOUT
280 );
281 dbg("%s - port %d Send get_HW_version URB returns: %i", __FUNCTION__, port->number, result);
282 dbg("Harware version: %i.%i.%i", transfer_buffer[0], transfer_buffer[1], transfer_buffer[2] );
283
284 // get firmware version
285 result = usb_control_msg( port->serial->dev,
286 usb_rcvctrlpipe(port->serial->dev, 0 ),
287 SUSBCRequest_GetMisc,
288 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
289 SUSBCR_MSC_GetFWVersion,
290 0,
291 transfer_buffer,
292 transfer_buffer_length,
293 KOBIL_TIMEOUT
294 );
295 dbg("%s - port %d Send get_FW_version URB returns: %i", __FUNCTION__, port->number, result);
296 dbg("Firmware version: %i.%i.%i", transfer_buffer[0], transfer_buffer[1], transfer_buffer[2] );
297
298 if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID || priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) {
299 // Setting Baudrate, Parity and Stopbits
300 result = usb_control_msg( port->serial->dev,
301 usb_rcvctrlpipe(port->serial->dev, 0 ),
302 SUSBCRequest_SetBaudRateParityAndStopBits,
303 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
304 SUSBCR_SBR_9600 | SUSBCR_SPASB_EvenParity | SUSBCR_SPASB_1StopBit,
305 0,
306 transfer_buffer,
307 0,
308 KOBIL_TIMEOUT
309 );
310 dbg("%s - port %d Send set_baudrate URB returns: %i", __FUNCTION__, port->number, result);
311
312 // reset all queues
313 result = usb_control_msg( port->serial->dev,
314 usb_rcvctrlpipe(port->serial->dev, 0 ),
315 SUSBCRequest_Misc,
316 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
317 SUSBCR_MSC_ResetAllQueues,
318 0,
319 transfer_buffer,
320 0,
321 KOBIL_TIMEOUT
322 );
323 dbg("%s - port %d Send reset_all_queues URB returns: %i", __FUNCTION__, port->number, result);
324 }
325 if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID || priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID ||
326 priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID) {
327 // start reading (Adapter B 'cause PNP string)
328 result = usb_submit_urb( port->interrupt_in_urb, GFP_ATOMIC );
329 dbg("%s - port %d Send read URB returns: %i", __FUNCTION__, port->number, result);
330 }
331
332 kfree(transfer_buffer);
333 return 0;
334}
335
336
337static void kobil_close (struct usb_serial_port *port, struct file *filp)
338{
339 dbg("%s - port %d", __FUNCTION__, port->number);
340
341 if (port->write_urb) {
342 usb_kill_urb(port->write_urb);
343 usb_free_urb( port->write_urb );
344 port->write_urb = NULL;
345 }
Mariusz Kozlowski5505c222006-11-08 15:36:38 +0100346 usb_kill_urb(port->interrupt_in_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
348
349
Greg Kroah-Hartman6fcdcf02007-06-15 15:44:13 -0700350static void kobil_read_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 int result;
Greg Kroah-Hartman6fcdcf02007-06-15 15:44:13 -0700353 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 struct tty_struct *tty;
Greg Kroah-Hartman6fcdcf02007-06-15 15:44:13 -0700355 unsigned char *data = urb->transfer_buffer;
356 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357// char *dbg_data;
358
359 dbg("%s - port %d", __FUNCTION__, port->number);
360
Greg Kroah-Hartman6fcdcf02007-06-15 15:44:13 -0700361 if (status) {
362 dbg("%s - port %d Read int status not zero: %d",
363 __FUNCTION__, port->number, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return;
365 }
Greg Kroah-Hartman6fcdcf02007-06-15 15:44:13 -0700366
367 tty = port->tty;
368 if (urb->actual_length) {
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 // BEGIN DEBUG
371 /*
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100372 dbg_data = kzalloc((3 * purb->actual_length + 10) * sizeof(char), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 if (! dbg_data) {
374 return;
375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 for (i = 0; i < purb->actual_length; i++) {
377 sprintf(dbg_data +3*i, "%02X ", data[i]);
378 }
379 dbg(" <-- %s", dbg_data );
380 kfree(dbg_data);
381 */
382 // END DEBUG
383
Greg Kroah-Hartman6fcdcf02007-06-15 15:44:13 -0700384 tty_buffer_request_room(tty, urb->actual_length);
385 tty_insert_flip_string(tty, data, urb->actual_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 tty_flip_buffer_push(tty);
387 }
388
389 // someone sets the dev to 0 if the close method has been called
390 port->interrupt_in_urb->dev = port->serial->dev;
391
Greg Kroah-Hartman6fcdcf02007-06-15 15:44:13 -0700392 result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 dbg("%s - port %d Send read URB returns: %i", __FUNCTION__, port->number, result);
394}
395
396
David Howells7d12e782006-10-05 14:55:46 +0100397static void kobil_write_callback( struct urb *purb )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
399}
400
401
402static int kobil_write (struct usb_serial_port *port,
403 const unsigned char *buf, int count)
404{
405 int length = 0;
406 int result = 0;
407 int todo = 0;
408 struct kobil_private * priv;
409
410 if (count == 0) {
411 dbg("%s - port %d write request of 0 bytes", __FUNCTION__, port->number);
412 return 0;
413 }
414
415 priv = usb_get_serial_port_data(port);
416
417 if (count > (KOBIL_BUF_LENGTH - priv->filled)) {
418 dbg("%s - port %d Error: write request bigger than buffer size", __FUNCTION__, port->number);
419 return -ENOMEM;
420 }
421
422 // Copy data to buffer
423 memcpy (priv->buf + priv->filled, buf, count);
424
425 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, priv->buf + priv->filled);
426
427 priv->filled = priv->filled + count;
428
429
430 // only send complete block. TWIN, KAAN SIM and adapter K use the same protocol.
431 if ( ((priv->device_type != KOBIL_ADAPTER_B_PRODUCT_ID) && (priv->filled > 2) && (priv->filled >= (priv->buf[1] + 3))) ||
432 ((priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) && (priv->filled > 3) && (priv->filled >= (priv->buf[2] + 4))) ) {
433
434 // stop reading (except TWIN and KAAN SIM)
435 if ( (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) || (priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) )
436 usb_kill_urb(port->interrupt_in_urb);
437
438 todo = priv->filled - priv->cur_pos;
439
440 while(todo > 0) {
441 // max 8 byte in one urb (endpoint size)
442 length = (todo < 8) ? todo : 8;
443 // copy data to transfer buffer
444 memcpy(port->write_urb->transfer_buffer, priv->buf + priv->cur_pos, length );
445 usb_fill_int_urb( port->write_urb,
446 port->serial->dev,
447 usb_sndintpipe(port->serial->dev, priv->write_int_endpoint_address),
448 port->write_urb->transfer_buffer,
449 length,
450 kobil_write_callback,
451 port,
452 8
453 );
454
455 priv->cur_pos = priv->cur_pos + length;
456 result = usb_submit_urb( port->write_urb, GFP_NOIO );
457 dbg("%s - port %d Send write URB returns: %i", __FUNCTION__, port->number, result);
458 todo = priv->filled - priv->cur_pos;
459
460 if (todo > 0) {
461 msleep(24);
462 }
463
464 } // end while
465
466 priv->filled = 0;
467 priv->cur_pos = 0;
468
469 // someone sets the dev to 0 if the close method has been called
470 port->interrupt_in_urb->dev = port->serial->dev;
471
472 // start reading (except TWIN and KAAN SIM)
473 if ( (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) || (priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) ) {
474 // someone sets the dev to 0 if the close method has been called
475 port->interrupt_in_urb->dev = port->serial->dev;
476
477 result = usb_submit_urb( port->interrupt_in_urb, GFP_NOIO );
478 dbg("%s - port %d Send read URB returns: %i", __FUNCTION__, port->number, result);
479 }
480 }
481 return count;
482}
483
484
485static int kobil_write_room (struct usb_serial_port *port)
486{
487 //dbg("%s - port %d", __FUNCTION__, port->number);
488 return 8;
489}
490
491
492static int kobil_tiocmget(struct usb_serial_port *port, struct file *file)
493{
494 struct kobil_private * priv;
495 int result;
496 unsigned char *transfer_buffer;
497 int transfer_buffer_length = 8;
498
499 priv = usb_get_serial_port_data(port);
500 if ((priv->device_type == KOBIL_USBTWIN_PRODUCT_ID) || (priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID)) {
501 // This device doesn't support ioctl calls
502 return -EINVAL;
503 }
504
505 // allocate memory for transfer buffer
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100506 transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (!transfer_buffer) {
508 return -ENOMEM;
509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 result = usb_control_msg( port->serial->dev,
512 usb_rcvctrlpipe(port->serial->dev, 0 ),
513 SUSBCRequest_GetStatusLineState,
514 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
515 0,
516 0,
517 transfer_buffer,
518 transfer_buffer_length,
519 KOBIL_TIMEOUT);
520
521 dbg("%s - port %d Send get_status_line_state URB returns: %i. Statusline: %02x",
522 __FUNCTION__, port->number, result, transfer_buffer[0]);
523
Alan Coxa40d8542008-02-20 21:40:34 +0000524 result = 0;
525 if ((transfer_buffer[0] & SUSBCR_GSL_DSR) != 0)
526 result = TIOCM_DSR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 kfree(transfer_buffer);
Alan Coxa40d8542008-02-20 21:40:34 +0000528 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
530
531static int kobil_tiocmset(struct usb_serial_port *port, struct file *file,
532 unsigned int set, unsigned int clear)
533{
534 struct kobil_private * priv;
535 int result;
536 int dtr = 0;
537 int rts = 0;
538 unsigned char *transfer_buffer;
539 int transfer_buffer_length = 8;
540
Alan Coxa40d8542008-02-20 21:40:34 +0000541 /* FIXME: locking ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 priv = usb_get_serial_port_data(port);
543 if ((priv->device_type == KOBIL_USBTWIN_PRODUCT_ID) || (priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID)) {
544 // This device doesn't support ioctl calls
545 return -EINVAL;
546 }
547
548 // allocate memory for transfer buffer
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100549 transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 if (! transfer_buffer) {
551 return -ENOMEM;
552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 if (set & TIOCM_RTS)
555 rts = 1;
556 if (set & TIOCM_DTR)
557 dtr = 1;
558 if (clear & TIOCM_RTS)
559 rts = 0;
560 if (clear & TIOCM_DTR)
561 dtr = 0;
562
563 if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) {
564 if (dtr != 0)
565 dbg("%s - port %d Setting DTR", __FUNCTION__, port->number);
566 else
567 dbg("%s - port %d Clearing DTR", __FUNCTION__, port->number);
568 result = usb_control_msg( port->serial->dev,
569 usb_rcvctrlpipe(port->serial->dev, 0 ),
570 SUSBCRequest_SetStatusLinesOrQueues,
571 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
572 ((dtr != 0) ? SUSBCR_SSL_SETDTR : SUSBCR_SSL_CLRDTR),
573 0,
574 transfer_buffer,
575 0,
576 KOBIL_TIMEOUT);
577 } else {
578 if (rts != 0)
579 dbg("%s - port %d Setting RTS", __FUNCTION__, port->number);
580 else
581 dbg("%s - port %d Clearing RTS", __FUNCTION__, port->number);
582 result = usb_control_msg( port->serial->dev,
583 usb_rcvctrlpipe(port->serial->dev, 0 ),
584 SUSBCRequest_SetStatusLinesOrQueues,
585 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
586 ((rts != 0) ? SUSBCR_SSL_SETRTS : SUSBCR_SSL_CLRRTS),
587 0,
588 transfer_buffer,
589 0,
590 KOBIL_TIMEOUT);
591 }
592 dbg("%s - port %d Send set_status_line URB returns: %i", __FUNCTION__, port->number, result);
593 kfree(transfer_buffer);
594 return (result < 0) ? result : 0;
595}
596
Alan Cox94d0f7e2007-08-22 23:09:16 +0100597static void kobil_set_termios(struct usb_serial_port *port, struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
599 struct kobil_private * priv;
600 int result;
601 unsigned short urb_val = 0;
Alan Cox94d0f7e2007-08-22 23:09:16 +0100602 int c_cflag = port->tty->termios->c_cflag;
603 speed_t speed;
604 void * settings;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 priv = usb_get_serial_port_data(port);
Alan Cox94d0f7e2007-08-22 23:09:16 +0100607 if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID || priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 // This device doesn't support ioctl calls
Alan Cox94d0f7e2007-08-22 23:09:16 +0100609 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Alan Cox94d0f7e2007-08-22 23:09:16 +0100611 switch (speed = tty_get_baud_rate(port->tty)) {
612 case 1200:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 urb_val = SUSBCR_SBR_1200;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 default:
Alan Coxa6ebf802007-10-18 01:24:21 -0700616 speed = 9600;
617 case 9600:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 urb_val = SUSBCR_SBR_9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 break;
Alan Cox94d0f7e2007-08-22 23:09:16 +0100620 }
621 urb_val |= (c_cflag & CSTOPB) ? SUSBCR_SPASB_2StopBits : SUSBCR_SPASB_1StopBit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Alan Cox94d0f7e2007-08-22 23:09:16 +0100623 settings = kzalloc(50, GFP_KERNEL);
624 if (! settings)
625 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Alan Cox94d0f7e2007-08-22 23:09:16 +0100627 sprintf(settings, "%d ", speed);
628
629 if (c_cflag & PARENB) {
630 if (c_cflag & PARODD) {
631 urb_val |= SUSBCR_SPASB_OddParity;
632 strcat(settings, "Odd Parity");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 } else {
Alan Cox94d0f7e2007-08-22 23:09:16 +0100634 urb_val |= SUSBCR_SPASB_EvenParity;
635 strcat(settings, "Even Parity");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 }
Alan Cox94d0f7e2007-08-22 23:09:16 +0100637 } else {
638 urb_val |= SUSBCR_SPASB_NoParity;
639 strcat(settings, "No Parity");
640 }
Alan Coxa6ebf802007-10-18 01:24:21 -0700641 port->tty->termios->c_cflag &= ~CMSPAR;
642 tty_encode_baud_rate(port->tty, speed, speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Alan Cox94d0f7e2007-08-22 23:09:16 +0100644 result = usb_control_msg( port->serial->dev,
645 usb_rcvctrlpipe(port->serial->dev, 0 ),
646 SUSBCRequest_SetBaudRateParityAndStopBits,
647 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
648 urb_val,
649 0,
650 settings,
651 0,
652 KOBIL_TIMEOUT
653 );
654 kfree(settings);
655}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Alan Cox94d0f7e2007-08-22 23:09:16 +0100657static int kobil_ioctl(struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
658{
659 struct kobil_private * priv = usb_get_serial_port_data(port);
660 unsigned char *transfer_buffer;
661 int transfer_buffer_length = 8;
662 int result;
663
664 if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID || priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID)
665 // This device doesn't support ioctl calls
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return 0;
667
Alan Cox94d0f7e2007-08-22 23:09:16 +0100668 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 case TCFLSH: // 0x540B
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800670 transfer_buffer = kmalloc(transfer_buffer_length, GFP_KERNEL);
Alan Cox94d0f7e2007-08-22 23:09:16 +0100671 if (! transfer_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 result = usb_control_msg( port->serial->dev,
675 usb_rcvctrlpipe(port->serial->dev, 0 ),
676 SUSBCRequest_Misc,
677 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
678 SUSBCR_MSC_ResetAllQueues,
679 0,
680 NULL,//transfer_buffer,
681 0,
682 KOBIL_TIMEOUT
683 );
684
685 dbg("%s - port %d Send reset_all_queues (FLUSH) URB returns: %i", __FUNCTION__, port->number, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 kfree(transfer_buffer);
Alan Cox94d0f7e2007-08-22 23:09:16 +0100687 return (result < 0) ? -EFAULT : 0;
688 default:
689 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691}
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693static int __init kobil_init (void)
694{
695 int retval;
696 retval = usb_serial_register(&kobil_device);
697 if (retval)
698 goto failed_usb_serial_register;
699 retval = usb_register(&kobil_driver);
700 if (retval)
701 goto failed_usb_register;
702
703 info(DRIVER_VERSION " " DRIVER_AUTHOR);
704 info(DRIVER_DESC);
705
706 return 0;
707failed_usb_register:
708 usb_serial_deregister(&kobil_device);
709failed_usb_serial_register:
710 return retval;
711}
712
713
714static void __exit kobil_exit (void)
715{
716 usb_deregister (&kobil_driver);
717 usb_serial_deregister (&kobil_device);
718}
719
720module_init(kobil_init);
721module_exit(kobil_exit);
722
723MODULE_AUTHOR( DRIVER_AUTHOR );
724MODULE_DESCRIPTION( DRIVER_DESC );
725MODULE_LICENSE( "GPL" );
726
727module_param(debug, bool, S_IRUGO | S_IWUSR);
728MODULE_PARM_DESC(debug, "Debug enabled or not");