Merge master.kernel.org:/pub/scm/linux/kernel/git/davem/sparc-2.6
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / usb / serial / kobil_sct.c
1 /*
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
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>
47 #include <asm/uaccess.h>
48 #include <linux/usb.h>
49 #include <linux/usb/serial.h>
50 #include <linux/ioctl.h>
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 */
71 static int  kobil_startup (struct usb_serial *serial);
72 static void kobil_shutdown (struct usb_serial *serial);
73 static int  kobil_open (struct usb_serial_port *port, struct file *filp);
74 static void kobil_close (struct usb_serial_port *port, struct file *filp);
75 static int  kobil_write (struct usb_serial_port *port, 
76                          const unsigned char *buf, int count);
77 static int  kobil_write_room(struct usb_serial_port *port);
78 static int  kobil_ioctl(struct usb_serial_port *port, struct file *file,
79                         unsigned int cmd, unsigned long arg);
80 static int  kobil_tiocmget(struct usb_serial_port *port, struct file *file);
81 static int  kobil_tiocmset(struct usb_serial_port *port, struct file *file,
82                            unsigned int set, unsigned int clear);
83 static void kobil_read_int_callback( struct urb *urb );
84 static void kobil_write_callback( struct urb *purb );
85
86
87 static struct usb_device_id id_table [] = {
88         { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_ADAPTER_B_PRODUCT_ID) },
89         { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_ADAPTER_K_PRODUCT_ID) },
90         { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_USBTWIN_PRODUCT_ID) },
91         { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_KAAN_SIM_PRODUCT_ID) },
92         { }                     /* Terminating entry */
93 };
94
95
96 MODULE_DEVICE_TABLE (usb, id_table);
97
98 static struct usb_driver kobil_driver = {
99         .name =         "kobil",
100         .probe =        usb_serial_probe,
101         .disconnect =   usb_serial_disconnect,
102         .id_table =     id_table,
103         .no_dynamic_id =        1,
104 };
105
106
107 static struct usb_serial_driver kobil_device = {
108         .driver = {
109                 .owner =        THIS_MODULE,
110                 .name =         "kobil",
111         },
112         .description =          "KOBIL USB smart card terminal",
113         .usb_driver =           &kobil_driver,
114         .id_table =             id_table,
115         .num_interrupt_in =     NUM_DONT_CARE,
116         .num_bulk_in =          0,
117         .num_bulk_out =         0,
118         .num_ports =            1,
119         .attach =               kobil_startup,
120         .shutdown =             kobil_shutdown,
121         .ioctl =                kobil_ioctl,
122         .tiocmget =             kobil_tiocmget,
123         .tiocmset =             kobil_tiocmset,
124         .open =                 kobil_open,
125         .close =                kobil_close,
126         .write =                kobil_write,
127         .write_room =           kobil_write_room,
128         .read_int_callback =    kobil_read_int_callback,
129 };
130
131
132 struct kobil_private {
133         int write_int_endpoint_address;
134         int read_int_endpoint_address;
135         unsigned char buf[KOBIL_BUF_LENGTH]; // buffer for the APDU to send
136         int filled;  // index of the last char in buf
137         int cur_pos; // index of the next char to send in buf
138         __u16 device_type;
139         int line_state;
140         struct ktermios internal_termios;
141 };
142
143
144 static int kobil_startup (struct usb_serial *serial)
145 {
146         int i;
147         struct kobil_private *priv;
148         struct usb_device *pdev;
149         struct usb_host_config *actconfig;
150         struct usb_interface *interface;
151         struct usb_host_interface *altsetting;
152         struct usb_host_endpoint *endpoint;
153
154         priv = kmalloc(sizeof(struct kobil_private), GFP_KERNEL);
155         if (!priv){
156                 return -ENOMEM;
157         }
158
159         priv->filled = 0;
160         priv->cur_pos = 0;
161         priv->device_type = le16_to_cpu(serial->dev->descriptor.idProduct);
162         priv->line_state = 0;
163
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];
189                 if (usb_endpoint_is_int_out(&endpoint->desc)) {
190                         dbg("%s Found interrupt out endpoint. Address: %d", __FUNCTION__, endpoint->desc.bEndpointAddress);
191                         priv->write_int_endpoint_address = endpoint->desc.bEndpointAddress;
192                 }
193                 if (usb_endpoint_is_int_in(&endpoint->desc)) {
194                         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
202 static 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
217 static int kobil_open (struct usb_serial_port *port, struct file *filp)
218 {
219         int i, result = 0;
220         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);
227         priv->line_state = 0;
228
229         // someone sets the dev to 0 if the close method has been called
230         port->interrupt_in_urb->dev = port->serial->dev;
231
232
233         /* force low_latency on so that our tty_push actually forces
234          * the data through, otherwise it is scheduled, and with high
235          * data rates (like with OHCI) data can get lost.
236          */
237         port->tty->low_latency = 1;
238
239         // without this, every push_tty_char is echoed :-(  
240         port->tty->termios->c_lflag = 0;
241         port->tty->termios->c_lflag &= ~(ISIG | ICANON | ECHO | IEXTEN | XCASE);
242         port->tty->termios->c_iflag = IGNBRK | IGNPAR | IXOFF;
243         port->tty->termios->c_oflag &= ~ONLCR; // do NOT translate CR to CR-NL (0x0A -> 0x0A 0x0D)
244         
245         // set up internal termios structure 
246         priv->internal_termios.c_iflag = port->tty->termios->c_iflag;
247         priv->internal_termios.c_oflag = port->tty->termios->c_oflag;
248         priv->internal_termios.c_cflag = port->tty->termios->c_cflag;
249         priv->internal_termios.c_lflag = port->tty->termios->c_lflag;
250
251         for (i=0; i<NCCS; i++) {
252                 priv->internal_termios.c_cc[i] = port->tty->termios->c_cc[i];
253         }
254         
255         // allocate memory for transfer buffer
256         transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
257         if (! transfer_buffer) {
258                 return -ENOMEM;
259         }
260         
261         // allocate write_urb
262         if (!port->write_urb) { 
263                 dbg("%s - port %d  Allocating port->write_urb", __FUNCTION__, port->number);
264                 port->write_urb = usb_alloc_urb(0, GFP_KERNEL);  
265                 if (!port->write_urb) {
266                         dbg("%s - port %d usb_alloc_urb failed", __FUNCTION__, port->number);
267                         kfree(transfer_buffer);
268                         return -ENOMEM;
269                 }
270         }
271
272         // allocate memory for write_urb transfer buffer
273         port->write_urb->transfer_buffer = kmalloc(write_urb_transfer_buffer_length, GFP_KERNEL);
274         if (! port->write_urb->transfer_buffer) {
275                 kfree(transfer_buffer);
276                 usb_free_urb(port->write_urb);
277                 port->write_urb = NULL;
278                 return -ENOMEM;
279         } 
280
281         // get hardware version
282         result = usb_control_msg( port->serial->dev, 
283                                   usb_rcvctrlpipe(port->serial->dev, 0 ), 
284                                   SUSBCRequest_GetMisc,
285                                   USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
286                                   SUSBCR_MSC_GetHWVersion,
287                                   0,
288                                   transfer_buffer,
289                                   transfer_buffer_length,
290                                   KOBIL_TIMEOUT
291                 );
292         dbg("%s - port %d Send get_HW_version URB returns: %i", __FUNCTION__, port->number, result);
293         dbg("Harware version: %i.%i.%i", transfer_buffer[0], transfer_buffer[1], transfer_buffer[2] );
294         
295         // get firmware version
296         result = usb_control_msg( port->serial->dev, 
297                                   usb_rcvctrlpipe(port->serial->dev, 0 ), 
298                                   SUSBCRequest_GetMisc,
299                                   USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
300                                   SUSBCR_MSC_GetFWVersion,
301                                   0,
302                                   transfer_buffer,
303                                   transfer_buffer_length,
304                                   KOBIL_TIMEOUT
305                 );
306         dbg("%s - port %d Send get_FW_version URB returns: %i", __FUNCTION__, port->number, result);
307         dbg("Firmware version: %i.%i.%i", transfer_buffer[0], transfer_buffer[1], transfer_buffer[2] );
308
309         if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID || priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) {
310                 // Setting Baudrate, Parity and Stopbits
311                 result = usb_control_msg( port->serial->dev, 
312                                           usb_rcvctrlpipe(port->serial->dev, 0 ), 
313                                           SUSBCRequest_SetBaudRateParityAndStopBits,
314                                           USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
315                                           SUSBCR_SBR_9600 | SUSBCR_SPASB_EvenParity | SUSBCR_SPASB_1StopBit,
316                                           0,
317                                           transfer_buffer,
318                                           0,
319                                           KOBIL_TIMEOUT
320                         );
321                 dbg("%s - port %d Send set_baudrate URB returns: %i", __FUNCTION__, port->number, result);
322                 
323                 // reset all queues
324                 result = usb_control_msg( port->serial->dev, 
325                                           usb_rcvctrlpipe(port->serial->dev, 0 ), 
326                                           SUSBCRequest_Misc,
327                                           USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
328                                           SUSBCR_MSC_ResetAllQueues,
329                                           0,
330                                           transfer_buffer,
331                                           0,
332                                           KOBIL_TIMEOUT
333                         );
334                 dbg("%s - port %d Send reset_all_queues URB returns: %i", __FUNCTION__, port->number, result);
335         }
336         if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID || priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID ||
337             priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID) {
338                 // start reading (Adapter B 'cause PNP string)
339                 result = usb_submit_urb( port->interrupt_in_urb, GFP_ATOMIC  ); 
340                 dbg("%s - port %d Send read URB returns: %i", __FUNCTION__, port->number, result);
341         }
342
343         kfree(transfer_buffer);
344         return 0;
345 }
346
347
348 static void kobil_close (struct usb_serial_port *port, struct file *filp)
349 {
350         dbg("%s - port %d", __FUNCTION__, port->number);
351
352         if (port->write_urb) {
353                 usb_kill_urb(port->write_urb);
354                 usb_free_urb( port->write_urb );
355                 port->write_urb = NULL;
356         }
357         usb_kill_urb(port->interrupt_in_urb);
358 }
359
360
361 static void kobil_read_int_callback( struct urb *purb)
362 {
363         int result;
364         struct usb_serial_port *port = (struct usb_serial_port *) purb->context;
365         struct tty_struct *tty;
366         unsigned char *data = purb->transfer_buffer;
367 //      char *dbg_data;
368
369         dbg("%s - port %d", __FUNCTION__, port->number);
370
371         if (purb->status) {
372                 dbg("%s - port %d Read int status not zero: %d", __FUNCTION__, port->number, purb->status);
373                 return;
374         }
375         
376         tty = port->tty; 
377         if (purb->actual_length) {
378                 
379                 // BEGIN DEBUG
380                 /*
381                   dbg_data = kzalloc((3 *  purb->actual_length + 10) * sizeof(char), GFP_KERNEL);
382                   if (! dbg_data) {
383                   return;
384                   }
385                   for (i = 0; i < purb->actual_length; i++) { 
386                   sprintf(dbg_data +3*i, "%02X ", data[i]); 
387                   }
388                   dbg(" <-- %s", dbg_data );
389                   kfree(dbg_data);
390                 */
391                 // END DEBUG
392
393                 tty_buffer_request_room(tty, purb->actual_length);
394                 tty_insert_flip_string(tty, data, purb->actual_length);
395                 tty_flip_buffer_push(tty);
396         }
397
398         // someone sets the dev to 0 if the close method has been called
399         port->interrupt_in_urb->dev = port->serial->dev;
400
401         result = usb_submit_urb( port->interrupt_in_urb, GFP_ATOMIC ); 
402         dbg("%s - port %d Send read URB returns: %i", __FUNCTION__, port->number, result);
403 }
404
405
406 static void kobil_write_callback( struct urb *purb )
407 {
408 }
409
410
411 static int kobil_write (struct usb_serial_port *port, 
412                         const unsigned char *buf, int count)
413 {
414         int length = 0;
415         int result = 0;
416         int todo = 0;
417         struct kobil_private * priv;
418
419         if (count == 0) {
420                 dbg("%s - port %d write request of 0 bytes", __FUNCTION__, port->number);
421                 return 0;
422         }
423
424         priv = usb_get_serial_port_data(port);
425
426         if (count > (KOBIL_BUF_LENGTH - priv->filled)) {
427                 dbg("%s - port %d Error: write request bigger than buffer size", __FUNCTION__, port->number);
428                 return -ENOMEM;
429         }
430
431         // Copy data to buffer
432         memcpy (priv->buf + priv->filled, buf, count);
433
434         usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, priv->buf + priv->filled);
435
436         priv->filled = priv->filled + count;
437
438
439         // only send complete block. TWIN, KAAN SIM and adapter K use the same protocol.
440         if ( ((priv->device_type != KOBIL_ADAPTER_B_PRODUCT_ID) && (priv->filled > 2) && (priv->filled >= (priv->buf[1] + 3))) || 
441              ((priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) && (priv->filled > 3) && (priv->filled >= (priv->buf[2] + 4))) ) {
442                 
443                 // stop reading (except TWIN and KAAN SIM)
444                 if ( (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) || (priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) )
445                         usb_kill_urb(port->interrupt_in_urb);
446
447                 todo = priv->filled - priv->cur_pos;
448
449                 while(todo > 0) {
450                         // max 8 byte in one urb (endpoint size)
451                         length = (todo < 8) ? todo : 8;
452                         // copy data to transfer buffer
453                         memcpy(port->write_urb->transfer_buffer, priv->buf + priv->cur_pos, length );
454                         usb_fill_int_urb( port->write_urb,
455                                           port->serial->dev,
456                                           usb_sndintpipe(port->serial->dev, priv->write_int_endpoint_address),
457                                           port->write_urb->transfer_buffer,
458                                           length,
459                                           kobil_write_callback,
460                                           port,
461                                           8
462                                 );
463
464                         priv->cur_pos = priv->cur_pos + length;
465                         result = usb_submit_urb( port->write_urb, GFP_NOIO );
466                         dbg("%s - port %d Send write URB returns: %i", __FUNCTION__, port->number, result);
467                         todo = priv->filled - priv->cur_pos;
468
469                         if (todo > 0) {
470                                 msleep(24);
471                         }
472
473                 } // end while
474                 
475                 priv->filled = 0;
476                 priv->cur_pos = 0;
477
478                 // someone sets the dev to 0 if the close method has been called
479                 port->interrupt_in_urb->dev = port->serial->dev;
480                 
481                 // start reading (except TWIN and KAAN SIM)
482                 if ( (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) || (priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) ) {
483                         // someone sets the dev to 0 if the close method has been called
484                         port->interrupt_in_urb->dev = port->serial->dev;
485                         
486                         result = usb_submit_urb( port->interrupt_in_urb, GFP_NOIO ); 
487                         dbg("%s - port %d Send read URB returns: %i", __FUNCTION__, port->number, result);
488                 }
489         }
490         return count;
491 }
492
493
494 static int kobil_write_room (struct usb_serial_port *port)
495 {
496         //dbg("%s - port %d", __FUNCTION__, port->number);
497         return 8;
498 }
499
500
501 static int kobil_tiocmget(struct usb_serial_port *port, struct file *file)
502 {
503         struct kobil_private * priv;
504         int result;
505         unsigned char *transfer_buffer;
506         int transfer_buffer_length = 8;
507
508         priv = usb_get_serial_port_data(port);
509         if ((priv->device_type == KOBIL_USBTWIN_PRODUCT_ID) || (priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID)) {
510                 // This device doesn't support ioctl calls
511                 return -EINVAL;
512         }
513
514         // allocate memory for transfer buffer
515         transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
516         if (!transfer_buffer) {
517                 return -ENOMEM;
518         }
519
520         result = usb_control_msg( port->serial->dev, 
521                                   usb_rcvctrlpipe(port->serial->dev, 0 ), 
522                                   SUSBCRequest_GetStatusLineState,
523                                   USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
524                                   0,
525                                   0,
526                                   transfer_buffer,
527                                   transfer_buffer_length,
528                                   KOBIL_TIMEOUT);
529
530         dbg("%s - port %d Send get_status_line_state URB returns: %i. Statusline: %02x", 
531             __FUNCTION__, port->number, result, transfer_buffer[0]);
532
533         if ((transfer_buffer[0] & SUSBCR_GSL_DSR) != 0) {
534                 priv->line_state |= TIOCM_DSR;
535         } else {
536                 priv->line_state &= ~TIOCM_DSR; 
537         }
538
539         kfree(transfer_buffer);
540         return priv->line_state;
541 }
542
543 static int  kobil_tiocmset(struct usb_serial_port *port, struct file *file,
544                            unsigned int set, unsigned int clear)
545 {
546         struct kobil_private * priv;
547         int result;
548         int dtr = 0;
549         int rts = 0;
550         unsigned char *transfer_buffer;
551         int transfer_buffer_length = 8;
552
553         priv = usb_get_serial_port_data(port);
554         if ((priv->device_type == KOBIL_USBTWIN_PRODUCT_ID) || (priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID)) {
555                 // This device doesn't support ioctl calls
556                 return -EINVAL;
557         }
558
559         // allocate memory for transfer buffer
560         transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
561         if (! transfer_buffer) {
562                 return -ENOMEM;
563         }
564
565         if (set & TIOCM_RTS)
566                 rts = 1;
567         if (set & TIOCM_DTR)
568                 dtr = 1;
569         if (clear & TIOCM_RTS)
570                 rts = 0;
571         if (clear & TIOCM_DTR)
572                 dtr = 0;
573
574         if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) {
575                 if (dtr != 0)
576                         dbg("%s - port %d Setting DTR", __FUNCTION__, port->number);
577                 else
578                         dbg("%s - port %d Clearing DTR", __FUNCTION__, port->number);
579                 result = usb_control_msg( port->serial->dev, 
580                                           usb_rcvctrlpipe(port->serial->dev, 0 ), 
581                                           SUSBCRequest_SetStatusLinesOrQueues,
582                                           USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
583                                           ((dtr != 0) ? SUSBCR_SSL_SETDTR : SUSBCR_SSL_CLRDTR),
584                                           0,
585                                           transfer_buffer,
586                                           0,
587                                           KOBIL_TIMEOUT);
588         } else {
589                 if (rts != 0)
590                         dbg("%s - port %d Setting RTS", __FUNCTION__, port->number);
591                 else
592                         dbg("%s - port %d Clearing RTS", __FUNCTION__, 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                                           ((rts != 0) ? SUSBCR_SSL_SETRTS : SUSBCR_SSL_CLRRTS),
598                                           0,
599                                           transfer_buffer,
600                                           0,
601                                           KOBIL_TIMEOUT);
602         }
603         dbg("%s - port %d Send set_status_line URB returns: %i", __FUNCTION__, port->number, result);
604         kfree(transfer_buffer);
605         return (result < 0) ? result : 0;
606 }
607
608
609 static int  kobil_ioctl(struct usb_serial_port *port, struct file *file,
610                         unsigned int cmd, unsigned long arg)
611 {
612         struct kobil_private * priv;
613         int result;
614         unsigned short urb_val = 0;
615         unsigned char *transfer_buffer;
616         int transfer_buffer_length = 8;
617         char *settings;
618         void __user *user_arg = (void __user *)arg;
619
620         priv = usb_get_serial_port_data(port);
621         if ((priv->device_type == KOBIL_USBTWIN_PRODUCT_ID) || (priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID)) {
622                 // This device doesn't support ioctl calls
623                 return 0;
624         }
625
626         switch (cmd) {
627         case TCGETS:   // 0x5401
628                 if (!access_ok(VERIFY_WRITE, user_arg, sizeof(struct ktermios))) {
629                         dbg("%s - port %d Error in access_ok", __FUNCTION__, port->number);
630                         return -EFAULT;
631                 }
632                 if (kernel_termios_to_user_termios((struct ktermios __user *)arg,
633                                                    &priv->internal_termios))
634                         return -EFAULT;
635                 return 0;
636
637         case TCSETS:   // 0x5402
638                 if (!(port->tty->termios)) {
639                         dbg("%s - port %d Error: port->tty->termios is NULL", __FUNCTION__, port->number);
640                         return -ENOTTY;
641                 }
642                 if (!access_ok(VERIFY_READ, user_arg, sizeof(struct ktermios))) {
643                         dbg("%s - port %d Error in access_ok", __FUNCTION__, port->number);
644                         return -EFAULT;
645                 }
646                 if (user_termios_to_kernel_termios(&priv->internal_termios,
647                                                    (struct ktermios __user *)arg))
648                         return -EFAULT;
649                 
650                 settings = kzalloc(50, GFP_KERNEL);
651                 if (! settings) {
652                         return -ENOBUFS;
653                 }
654
655                 switch (priv->internal_termios.c_cflag & CBAUD) {
656                 case B1200:
657                         urb_val = SUSBCR_SBR_1200;
658                         strcat(settings, "1200 ");
659                         break;
660                 case B9600:
661                 default:
662                         urb_val = SUSBCR_SBR_9600;
663                         strcat(settings, "9600 ");
664                         break;
665                 }
666
667                 urb_val |= (priv->internal_termios.c_cflag & CSTOPB) ? SUSBCR_SPASB_2StopBits : SUSBCR_SPASB_1StopBit;
668                 strcat(settings, (priv->internal_termios.c_cflag & CSTOPB) ? "2 StopBits " : "1 StopBit ");
669
670                 if (priv->internal_termios.c_cflag & PARENB) {
671                         if  (priv->internal_termios.c_cflag & PARODD) {
672                                 urb_val |= SUSBCR_SPASB_OddParity;
673                                 strcat(settings, "Odd Parity");
674                         } else {
675                                 urb_val |= SUSBCR_SPASB_EvenParity;
676                                 strcat(settings, "Even Parity");
677                         }
678                 } else {
679                         urb_val |= SUSBCR_SPASB_NoParity;
680                         strcat(settings, "No Parity");
681                 }
682                 dbg("%s - port %d setting port to: %s", __FUNCTION__, port->number, settings );
683
684                 result = usb_control_msg( port->serial->dev, 
685                                           usb_rcvctrlpipe(port->serial->dev, 0 ), 
686                                           SUSBCRequest_SetBaudRateParityAndStopBits,
687                                           USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
688                                           urb_val,
689                                           0,
690                                           settings,
691                                           0,
692                                           KOBIL_TIMEOUT
693                         );
694
695                 dbg("%s - port %d Send set_baudrate URB returns: %i", __FUNCTION__, port->number, result);
696                 kfree(settings);
697                 return 0;
698
699         case TCFLSH:   // 0x540B
700                 transfer_buffer = kmalloc(transfer_buffer_length, GFP_KERNEL);
701                 if (! transfer_buffer) {
702                         return -ENOBUFS;
703                 }
704
705                 result = usb_control_msg( port->serial->dev, 
706                                           usb_rcvctrlpipe(port->serial->dev, 0 ), 
707                                           SUSBCRequest_Misc,
708                                           USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
709                                           SUSBCR_MSC_ResetAllQueues,
710                                           0,
711                                           NULL,//transfer_buffer,
712                                           0,
713                                           KOBIL_TIMEOUT
714                         );
715                 
716                 dbg("%s - port %d Send reset_all_queues (FLUSH) URB returns: %i", __FUNCTION__, port->number, result);
717
718                 kfree(transfer_buffer);
719                 return ((result < 0) ? -EFAULT : 0);
720
721         }
722         return -ENOIOCTLCMD;
723 }
724
725
726 static int __init kobil_init (void)
727 {
728         int retval;
729         retval = usb_serial_register(&kobil_device);
730         if (retval)
731                 goto failed_usb_serial_register;
732         retval = usb_register(&kobil_driver);
733         if (retval) 
734                 goto failed_usb_register;
735
736         info(DRIVER_VERSION " " DRIVER_AUTHOR);
737         info(DRIVER_DESC);
738
739         return 0;
740 failed_usb_register:
741         usb_serial_deregister(&kobil_device);
742 failed_usb_serial_register:
743         return retval;
744 }
745
746
747 static void __exit kobil_exit (void)
748 {
749         usb_deregister (&kobil_driver);
750         usb_serial_deregister (&kobil_device);
751 }
752
753 module_init(kobil_init);
754 module_exit(kobil_exit);
755
756 MODULE_AUTHOR( DRIVER_AUTHOR );
757 MODULE_DESCRIPTION( DRIVER_DESC );
758 MODULE_LICENSE( "GPL" );
759
760 module_param(debug, bool, S_IRUGO | S_IWUSR);
761 MODULE_PARM_DESC(debug, "Debug enabled or not");