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