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