initial commit with v2.6.9
[linux-2.6.9-moxart.git] / drivers / usb / serial / kobil_sct.c
blobb479916ce269c062591ce3d89c7cbe54c55e0bdd
1 /*
2 * KOBIL USB Smart Card Terminal Driver
4 * Copyright (C) 2002 KOBIL Systems GmbH
5 * Author: Thomas Wahrenbruch
7 * Contact: linuxusb@kobil.de
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.
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.
18 * Thanks to Greg Kroah-Hartman (greg@kroah.com) for his help and
19 * patience.
21 * Supported readers: USB TWIN, KAAN Standard Plus and SecOVID Reader Plus
22 * (Adapter K), B1 Professional and KAAN Professional (Adapter B)
24 * (21/05/2004) tw
25 * Fix bug with P'n'P readers
27 * (28/05/2003) tw
28 * Add support for KAAN SIM
30 * (12/09/2002) tw
31 * Adapted to 2.5.
33 * (11/08/2002) tw
34 * Initial version.
38 #include <linux/config.h>
39 #include <linux/kernel.h>
40 #include <linux/errno.h>
41 #include <linux/init.h>
42 #include <linux/slab.h>
43 #include <linux/tty.h>
44 #include <linux/tty_driver.h>
45 #include <linux/tty_flip.h>
46 #include <linux/module.h>
47 #include <linux/spinlock.h>
48 #include <asm/uaccess.h>
49 #include <linux/usb.h>
50 #include <linux/ioctl.h>
51 #include "usb-serial.h"
52 #include "kobil_sct.h"
54 static int debug;
56 /* Version Information */
57 #define DRIVER_VERSION "21/05/2004"
58 #define DRIVER_AUTHOR "KOBIL Systems GmbH - http://www.kobil.com"
59 #define DRIVER_DESC "KOBIL USB Smart Card Terminal Driver (experimental)"
61 #define KOBIL_VENDOR_ID 0x0D46
62 #define KOBIL_ADAPTER_B_PRODUCT_ID 0x2011
63 #define KOBIL_ADAPTER_K_PRODUCT_ID 0x2012
64 #define KOBIL_USBTWIN_PRODUCT_ID 0x0078
65 #define KOBIL_KAAN_SIM_PRODUCT_ID 0x0081
67 #define KOBIL_TIMEOUT 500
68 #define KOBIL_BUF_LENGTH 300
71 /* Function prototypes */
72 static int kobil_startup (struct usb_serial *serial);
73 static void kobil_shutdown (struct usb_serial *serial);
74 static int kobil_open (struct usb_serial_port *port, struct file *filp);
75 static void kobil_close (struct usb_serial_port *port, struct file *filp);
76 static int kobil_write (struct usb_serial_port *port, int from_user,
77 const unsigned char *buf, int count);
78 static int kobil_write_room(struct usb_serial_port *port);
79 static int kobil_ioctl(struct usb_serial_port *port, struct file *file,
80 unsigned int cmd, unsigned long arg);
81 static int kobil_tiocmget(struct usb_serial_port *port, struct file *file);
82 static int kobil_tiocmset(struct usb_serial_port *port, struct file *file,
83 unsigned int set, unsigned int clear);
84 static void kobil_read_int_callback( struct urb *urb, struct pt_regs *regs );
85 static void kobil_write_callback( struct urb *purb, struct pt_regs *regs );
88 static 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 */
97 MODULE_DEVICE_TABLE (usb, id_table);
99 static struct usb_driver kobil_driver = {
100 .owner = THIS_MODULE,
101 .name = "kobil",
102 .probe = usb_serial_probe,
103 .disconnect = usb_serial_disconnect,
104 .id_table = id_table,
108 static struct usb_serial_device_type kobil_device = {
109 .owner = THIS_MODULE,
110 .name = "KOBIL USB smart card terminal",
111 .id_table = id_table,
112 .num_interrupt_in = NUM_DONT_CARE,
113 .num_bulk_in = 0,
114 .num_bulk_out = 0,
115 .num_ports = 1,
116 .attach = kobil_startup,
117 .shutdown = kobil_shutdown,
118 .ioctl = kobil_ioctl,
119 .tiocmget = kobil_tiocmget,
120 .tiocmset = kobil_tiocmset,
121 .open = kobil_open,
122 .close = kobil_close,
123 .write = kobil_write,
124 .write_room = kobil_write_room,
125 .read_int_callback = kobil_read_int_callback,
129 struct kobil_private {
130 int write_int_endpoint_address;
131 int read_int_endpoint_address;
132 unsigned char buf[KOBIL_BUF_LENGTH]; // buffer for the APDU to send
133 int filled; // index of the last char in buf
134 int cur_pos; // index of the next char to send in buf
135 __u16 device_type;
136 int line_state;
137 struct termios internal_termios;
141 static int kobil_startup (struct usb_serial *serial)
143 int i;
144 struct kobil_private *priv;
145 struct usb_device *pdev;
146 struct usb_host_config *actconfig;
147 struct usb_interface *interface;
148 struct usb_host_interface *altsetting;
149 struct usb_host_endpoint *endpoint;
151 priv = kmalloc(sizeof(struct kobil_private), GFP_KERNEL);
152 if (!priv){
153 return -ENOMEM;
156 priv->filled = 0;
157 priv->cur_pos = 0;
158 priv->device_type = serial->product;
159 priv->line_state = 0;
161 switch (priv->device_type){
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:
166 printk(KERN_DEBUG "KOBIL KAAN Standard Plus / SecOVID Reader Plus detected\n");
167 break;
168 case KOBIL_USBTWIN_PRODUCT_ID:
169 printk(KERN_DEBUG "KOBIL USBTWIN detected\n");
170 break;
171 case KOBIL_KAAN_SIM_PRODUCT_ID:
172 printk(KERN_DEBUG "KOBIL KAAN SIM detected\n");
173 break;
175 usb_set_serial_port_data(serial->port[0], priv);
177 // search for the necessary endpoints
178 pdev = serial->dev;
179 actconfig = pdev->actconfig;
180 interface = actconfig->interface[0];
181 altsetting = interface->cur_altsetting;
182 endpoint = altsetting->endpoint;
184 for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
185 endpoint = &altsetting->endpoint[i];
186 if (((endpoint->desc.bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) &&
187 ((endpoint->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)) {
188 dbg("%s Found interrupt out endpoint. Address: %d", __FUNCTION__, endpoint->desc.bEndpointAddress);
189 priv->write_int_endpoint_address = endpoint->desc.bEndpointAddress;
191 if (((endpoint->desc.bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) &&
192 ((endpoint->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)) {
193 dbg("%s Found interrupt in endpoint. Address: %d", __FUNCTION__, endpoint->desc.bEndpointAddress);
194 priv->read_int_endpoint_address = endpoint->desc.bEndpointAddress;
197 return 0;
201 static void kobil_shutdown (struct usb_serial *serial)
203 int i;
204 dbg("%s - port %d", __FUNCTION__, serial->port[0]->number);
206 for (i=0; i < serial->num_ports; ++i) {
207 while (serial->port[i]->open_count > 0) {
208 kobil_close (serial->port[i], NULL);
210 kfree(usb_get_serial_port_data(serial->port[i]));
211 usb_set_serial_port_data(serial->port[i], NULL);
216 static int kobil_open (struct usb_serial_port *port, struct file *filp)
218 int i, result = 0;
219 struct kobil_private *priv;
220 unsigned char *transfer_buffer;
221 int transfer_buffer_length = 8;
222 int write_urb_transfer_buffer_length = 8;
224 dbg("%s - port %d", __FUNCTION__, port->number);
225 priv = usb_get_serial_port_data(port);
226 priv->line_state = 0;
228 // someone sets the dev to 0 if the close method has been called
229 port->interrupt_in_urb->dev = port->serial->dev;
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.
236 port->tty->low_latency = 1;
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)
244 // set up internal termios structure
245 priv->internal_termios.c_iflag = port->tty->termios->c_iflag;
246 priv->internal_termios.c_oflag = port->tty->termios->c_oflag;
247 priv->internal_termios.c_cflag = port->tty->termios->c_cflag;
248 priv->internal_termios.c_lflag = port->tty->termios->c_lflag;
250 for (i=0; i<NCCS; i++) {
251 priv->internal_termios.c_cc[i] = port->tty->termios->c_cc[i];
254 // allocate memory for transfer buffer
255 transfer_buffer = (unsigned char *) kmalloc(transfer_buffer_length, GFP_KERNEL);
256 if (! transfer_buffer) {
257 return -ENOMEM;
258 } else {
259 memset(transfer_buffer, 0, transfer_buffer_length);
262 // allocate write_urb
263 if (!port->write_urb) {
264 dbg("%s - port %d Allocating port->write_urb", __FUNCTION__, port->number);
265 port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
266 if (!port->write_urb) {
267 dbg("%s - port %d usb_alloc_urb failed", __FUNCTION__, port->number);
268 kfree(transfer_buffer);
269 return -ENOMEM;
273 // allocate memory for write_urb transfer buffer
274 port->write_urb->transfer_buffer = (unsigned char *) kmalloc(write_urb_transfer_buffer_length, GFP_KERNEL);
275 if (! port->write_urb->transfer_buffer) {
276 kfree(transfer_buffer);
277 usb_free_urb(port->write_urb);
278 port->write_urb = NULL;
279 return -ENOMEM;
282 // get hardware version
283 result = usb_control_msg( port->serial->dev,
284 usb_rcvctrlpipe(port->serial->dev, 0 ),
285 SUSBCRequest_GetMisc,
286 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
287 SUSBCR_MSC_GetHWVersion,
289 transfer_buffer,
290 transfer_buffer_length,
291 KOBIL_TIMEOUT
293 dbg("%s - port %d Send get_HW_version URB returns: %i", __FUNCTION__, port->number, result);
294 dbg("Harware version: %i.%i.%i", transfer_buffer[0], transfer_buffer[1], transfer_buffer[2] );
296 // get firmware version
297 result = usb_control_msg( port->serial->dev,
298 usb_rcvctrlpipe(port->serial->dev, 0 ),
299 SUSBCRequest_GetMisc,
300 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
301 SUSBCR_MSC_GetFWVersion,
303 transfer_buffer,
304 transfer_buffer_length,
305 KOBIL_TIMEOUT
307 dbg("%s - port %d Send get_FW_version URB returns: %i", __FUNCTION__, port->number, result);
308 dbg("Firmware version: %i.%i.%i", transfer_buffer[0], transfer_buffer[1], transfer_buffer[2] );
310 if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID || priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) {
311 // Setting Baudrate, Parity and Stopbits
312 result = usb_control_msg( port->serial->dev,
313 usb_rcvctrlpipe(port->serial->dev, 0 ),
314 SUSBCRequest_SetBaudRateParityAndStopBits,
315 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
316 SUSBCR_SBR_9600 | SUSBCR_SPASB_EvenParity | SUSBCR_SPASB_1StopBit,
318 transfer_buffer,
320 KOBIL_TIMEOUT
322 dbg("%s - port %d Send set_baudrate URB returns: %i", __FUNCTION__, port->number, result);
324 // reset all queues
325 result = usb_control_msg( port->serial->dev,
326 usb_rcvctrlpipe(port->serial->dev, 0 ),
327 SUSBCRequest_Misc,
328 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
329 SUSBCR_MSC_ResetAllQueues,
331 transfer_buffer,
333 KOBIL_TIMEOUT
335 dbg("%s - port %d Send reset_all_queues URB returns: %i", __FUNCTION__, port->number, result);
337 if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID || priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID ||
338 priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID) {
339 // start reading (Adapter B 'cause PNP string)
340 result = usb_submit_urb( port->interrupt_in_urb, GFP_ATOMIC );
341 dbg("%s - port %d Send read URB returns: %i", __FUNCTION__, port->number, result);
344 kfree(transfer_buffer);
345 return 0;
349 static void kobil_close (struct usb_serial_port *port, struct file *filp)
351 dbg("%s - port %d", __FUNCTION__, port->number);
353 if (port->write_urb){
354 usb_unlink_urb( port->write_urb );
355 usb_free_urb( port->write_urb );
356 port->write_urb = NULL;
358 if (port->interrupt_in_urb){
359 usb_unlink_urb (port->interrupt_in_urb);
364 static void kobil_read_int_callback( struct urb *purb, struct pt_regs *regs)
366 int i;
367 int result;
368 struct usb_serial_port *port = (struct usb_serial_port *) purb->context;
369 struct tty_struct *tty;
370 unsigned char *data = purb->transfer_buffer;
371 // char *dbg_data;
373 dbg("%s - port %d", __FUNCTION__, port->number);
375 if (purb->status) {
376 dbg("%s - port %d Read int status not zero: %d", __FUNCTION__, port->number, purb->status);
377 return;
380 tty = port->tty;
381 if (purb->actual_length) {
383 // BEGIN DEBUG
385 dbg_data = (unsigned char *) kmalloc((3 * purb->actual_length + 10) * sizeof(char), GFP_KERNEL);
386 if (! dbg_data) {
387 return;
389 memset(dbg_data, 0, (3 * purb->actual_length + 10));
390 for (i = 0; i < purb->actual_length; i++) {
391 sprintf(dbg_data +3*i, "%02X ", data[i]);
393 dbg(" <-- %s", dbg_data );
394 kfree(dbg_data);
396 // END DEBUG
398 for (i = 0; i < purb->actual_length; ++i) {
399 // if we insert more than TTY_FLIPBUF_SIZE characters, we drop them.
400 if(tty->flip.count >= TTY_FLIPBUF_SIZE) {
401 tty_flip_buffer_push(tty);
403 // this doesn't actually push the data through unless tty->low_latency is set
404 tty_insert_flip_char(tty, data[i], 0);
406 tty_flip_buffer_push(tty);
409 // someone sets the dev to 0 if the close method has been called
410 port->interrupt_in_urb->dev = port->serial->dev;
412 result = usb_submit_urb( port->interrupt_in_urb, GFP_ATOMIC );
413 dbg("%s - port %d Send read URB returns: %i", __FUNCTION__, port->number, result);
417 static void kobil_write_callback( struct urb *purb, struct pt_regs *regs )
422 static int kobil_write (struct usb_serial_port *port, int from_user,
423 const unsigned char *buf, int count)
425 int length = 0;
426 int result = 0;
427 int todo = 0;
428 struct kobil_private * priv;
430 if (count == 0) {
431 dbg("%s - port %d write request of 0 bytes", __FUNCTION__, port->number);
432 return 0;
435 priv = usb_get_serial_port_data(port);
437 if (count > (KOBIL_BUF_LENGTH - priv->filled)) {
438 dbg("%s - port %d Error: write request bigger than buffer size", __FUNCTION__, port->number);
439 return -ENOMEM;
442 // Copy data to buffer
443 if (from_user) {
444 if (copy_from_user(priv->buf + priv->filled, buf, count)) {
445 return -EFAULT;
447 } else {
448 memcpy (priv->buf + priv->filled, buf, count);
451 usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, priv->buf + priv->filled);
453 priv->filled = priv->filled + count;
456 // only send complete block. TWIN, KAAN SIM and adapter K use the same protocol.
457 if ( ((priv->device_type != KOBIL_ADAPTER_B_PRODUCT_ID) && (priv->filled > 2) && (priv->filled >= (priv->buf[1] + 3))) ||
458 ((priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) && (priv->filled > 3) && (priv->filled >= (priv->buf[2] + 4))) ) {
460 // stop reading (except TWIN and KAAN SIM)
461 if ( (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) || (priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) ) {
462 usb_unlink_urb( port->interrupt_in_urb );
465 todo = priv->filled - priv->cur_pos;
467 while(todo > 0) {
468 // max 8 byte in one urb (endpoint size)
469 length = (todo < 8) ? todo : 8;
470 // copy data to transfer buffer
471 memcpy(port->write_urb->transfer_buffer, priv->buf + priv->cur_pos, length );
472 usb_fill_int_urb( port->write_urb,
473 port->serial->dev,
474 usb_sndintpipe(port->serial->dev, priv->write_int_endpoint_address),
475 port->write_urb->transfer_buffer,
476 length,
477 kobil_write_callback,
478 port,
482 priv->cur_pos = priv->cur_pos + length;
483 result = usb_submit_urb( port->write_urb, GFP_NOIO );
484 dbg("%s - port %d Send write URB returns: %i", __FUNCTION__, port->number, result);
485 todo = priv->filled - priv->cur_pos;
487 if (todo > 0) {
488 msleep(24);
491 } // end while
493 priv->filled = 0;
494 priv->cur_pos = 0;
496 // someone sets the dev to 0 if the close method has been called
497 port->interrupt_in_urb->dev = port->serial->dev;
499 // start reading (except TWIN and KAAN SIM)
500 if ( (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) || (priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) ) {
501 // someone sets the dev to 0 if the close method has been called
502 port->interrupt_in_urb->dev = port->serial->dev;
504 result = usb_submit_urb( port->interrupt_in_urb, GFP_NOIO );
505 dbg("%s - port %d Send read URB returns: %i", __FUNCTION__, port->number, result);
508 return count;
512 static int kobil_write_room (struct usb_serial_port *port)
514 //dbg("%s - port %d", __FUNCTION__, port->number);
515 return 8;
519 static int kobil_tiocmget(struct usb_serial_port *port, struct file *file)
521 struct kobil_private * priv;
522 int result;
523 unsigned char *transfer_buffer;
524 int transfer_buffer_length = 8;
526 priv = usb_get_serial_port_data(port);
527 if ((priv->device_type == KOBIL_USBTWIN_PRODUCT_ID) || (priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID)) {
528 // This device doesn't support ioctl calls
529 return -EINVAL;
532 // allocate memory for transfer buffer
533 transfer_buffer = (unsigned char *) kmalloc(transfer_buffer_length, GFP_KERNEL);
534 if (!transfer_buffer) {
535 return -ENOMEM;
537 memset(transfer_buffer, 0, transfer_buffer_length);
539 result = usb_control_msg( port->serial->dev,
540 usb_rcvctrlpipe(port->serial->dev, 0 ),
541 SUSBCRequest_GetStatusLineState,
542 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
545 transfer_buffer,
546 transfer_buffer_length,
547 KOBIL_TIMEOUT);
549 dbg("%s - port %d Send get_status_line_state URB returns: %i. Statusline: %02x",
550 __FUNCTION__, port->number, result, transfer_buffer[0]);
552 if ((transfer_buffer[0] & SUSBCR_GSL_DSR) != 0) {
553 priv->line_state |= TIOCM_DSR;
554 } else {
555 priv->line_state &= ~TIOCM_DSR;
558 kfree(transfer_buffer);
559 return priv->line_state;
562 static int kobil_tiocmset(struct usb_serial_port *port, struct file *file,
563 unsigned int set, unsigned int clear)
565 struct kobil_private * priv;
566 int result;
567 int dtr = 0;
568 int rts = 0;
569 unsigned char *transfer_buffer;
570 int transfer_buffer_length = 8;
572 priv = usb_get_serial_port_data(port);
573 if ((priv->device_type == KOBIL_USBTWIN_PRODUCT_ID) || (priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID)) {
574 // This device doesn't support ioctl calls
575 return -EINVAL;
578 // allocate memory for transfer buffer
579 transfer_buffer = (unsigned char *) kmalloc(transfer_buffer_length, GFP_KERNEL);
580 if (! transfer_buffer) {
581 return -ENOMEM;
583 memset(transfer_buffer, 0, transfer_buffer_length);
585 if (set & TIOCM_RTS)
586 rts = 1;
587 if (set & TIOCM_DTR)
588 dtr = 1;
589 if (clear & TIOCM_RTS)
590 rts = 0;
591 if (clear & TIOCM_DTR)
592 dtr = 0;
594 if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) {
595 if (dtr != 0)
596 dbg("%s - port %d Setting DTR", __FUNCTION__, port->number);
597 else
598 dbg("%s - port %d Clearing DTR", __FUNCTION__, port->number);
599 result = usb_control_msg( port->serial->dev,
600 usb_rcvctrlpipe(port->serial->dev, 0 ),
601 SUSBCRequest_SetStatusLinesOrQueues,
602 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
603 ((dtr != 0) ? SUSBCR_SSL_SETDTR : SUSBCR_SSL_CLRDTR),
605 transfer_buffer,
607 KOBIL_TIMEOUT);
608 } else {
609 if (rts != 0)
610 dbg("%s - port %d Setting RTS", __FUNCTION__, port->number);
611 else
612 dbg("%s - port %d Clearing RTS", __FUNCTION__, port->number);
613 result = usb_control_msg( port->serial->dev,
614 usb_rcvctrlpipe(port->serial->dev, 0 ),
615 SUSBCRequest_SetStatusLinesOrQueues,
616 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
617 ((rts != 0) ? SUSBCR_SSL_SETRTS : SUSBCR_SSL_CLRRTS),
619 transfer_buffer,
621 KOBIL_TIMEOUT);
623 dbg("%s - port %d Send set_status_line URB returns: %i", __FUNCTION__, port->number, result);
624 kfree(transfer_buffer);
625 return (result < 0) ? result : 0;
629 static int kobil_ioctl(struct usb_serial_port *port, struct file *file,
630 unsigned int cmd, unsigned long arg)
632 struct kobil_private * priv;
633 int result;
634 unsigned short urb_val = 0;
635 unsigned char *transfer_buffer;
636 int transfer_buffer_length = 8;
637 char *settings;
638 void __user *user_arg = (void __user *)arg;
640 priv = usb_get_serial_port_data(port);
641 if ((priv->device_type == KOBIL_USBTWIN_PRODUCT_ID) || (priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID)) {
642 // This device doesn't support ioctl calls
643 return 0;
646 switch (cmd) {
647 case TCGETS: // 0x5401
648 result = verify_area(VERIFY_WRITE, user_arg, sizeof(struct termios));
649 if (result) {
650 dbg("%s - port %d Error in verify_area", __FUNCTION__, port->number);
651 return(result);
653 if (kernel_termios_to_user_termios((struct termios __user *)arg,
654 &priv->internal_termios))
655 return -EFAULT;
656 return 0;
658 case TCSETS: // 0x5402
659 if (!(port->tty->termios)) {
660 dbg("%s - port %d Error: port->tty->termios is NULL", __FUNCTION__, port->number);
661 return -ENOTTY;
663 result = verify_area(VERIFY_READ, user_arg, sizeof(struct termios));
664 if (result) {
665 dbg("%s - port %d Error in verify_area", __FUNCTION__, port->number);
666 return result;
668 if (user_termios_to_kernel_termios(&priv->internal_termios,
669 (struct termios __user *)arg))
670 return -EFAULT;
672 settings = (unsigned char *) kmalloc(50, GFP_KERNEL);
673 if (! settings) {
674 return -ENOBUFS;
676 memset(settings, 0, 50);
678 switch (priv->internal_termios.c_cflag & CBAUD) {
679 case B1200:
680 urb_val = SUSBCR_SBR_1200;
681 strcat(settings, "1200 ");
682 break;
683 case B9600:
684 default:
685 urb_val = SUSBCR_SBR_9600;
686 strcat(settings, "9600 ");
687 break;
690 urb_val |= (priv->internal_termios.c_cflag & CSTOPB) ? SUSBCR_SPASB_2StopBits : SUSBCR_SPASB_1StopBit;
691 strcat(settings, (priv->internal_termios.c_cflag & CSTOPB) ? "2 StopBits " : "1 StopBit ");
693 if (priv->internal_termios.c_cflag & PARENB) {
694 if (priv->internal_termios.c_cflag & PARODD) {
695 urb_val |= SUSBCR_SPASB_OddParity;
696 strcat(settings, "Odd Parity");
697 } else {
698 urb_val |= SUSBCR_SPASB_EvenParity;
699 strcat(settings, "Even Parity");
701 } else {
702 urb_val |= SUSBCR_SPASB_NoParity;
703 strcat(settings, "No Parity");
705 dbg("%s - port %d setting port to: %s", __FUNCTION__, port->number, settings );
707 result = usb_control_msg( port->serial->dev,
708 usb_rcvctrlpipe(port->serial->dev, 0 ),
709 SUSBCRequest_SetBaudRateParityAndStopBits,
710 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
711 urb_val,
713 settings,
715 KOBIL_TIMEOUT
718 dbg("%s - port %d Send set_baudrate URB returns: %i", __FUNCTION__, port->number, result);
719 kfree(settings);
720 return 0;
722 case TCFLSH: // 0x540B
723 transfer_buffer = (unsigned char *) kmalloc(transfer_buffer_length, GFP_KERNEL);
724 if (! transfer_buffer) {
725 return -ENOBUFS;
728 result = usb_control_msg( port->serial->dev,
729 usb_rcvctrlpipe(port->serial->dev, 0 ),
730 SUSBCRequest_Misc,
731 USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
732 SUSBCR_MSC_ResetAllQueues,
734 NULL,//transfer_buffer,
736 KOBIL_TIMEOUT
739 dbg("%s - port %d Send reset_all_queues (FLUSH) URB returns: %i", __FUNCTION__, port->number, result);
741 kfree(transfer_buffer);
742 return ((result < 0) ? -EFAULT : 0);
745 return -ENOIOCTLCMD;
749 static int __init kobil_init (void)
751 int retval;
752 retval = usb_serial_register(&kobil_device);
753 if (retval)
754 goto failed_usb_serial_register;
755 retval = usb_register(&kobil_driver);
756 if (retval)
757 goto failed_usb_register;
759 info(DRIVER_VERSION " " DRIVER_AUTHOR);
760 info(DRIVER_DESC);
762 return 0;
763 failed_usb_register:
764 usb_serial_deregister(&kobil_device);
765 failed_usb_serial_register:
766 return retval;
770 static void __exit kobil_exit (void)
772 usb_deregister (&kobil_driver);
773 usb_serial_deregister (&kobil_device);
776 module_init(kobil_init);
777 module_exit(kobil_exit);
779 MODULE_AUTHOR( DRIVER_AUTHOR );
780 MODULE_DESCRIPTION( DRIVER_DESC );
781 MODULE_LICENSE( "GPL" );
783 module_param(debug, bool, S_IRUGO | S_IWUSR);
784 MODULE_PARM_DESC(debug, "Debug enabled or not");