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