kobil_sct.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  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. #include <linux/kernel.h>
  37. #include <linux/errno.h>
  38. #include <linux/init.h>
  39. #include <linux/slab.h>
  40. #include <linux/tty.h>
  41. #include <linux/tty_driver.h>
  42. #include <linux/tty_flip.h>
  43. #include <linux/module.h>
  44. #include <linux/spinlock.h>
  45. #include <asm/uaccess.h>
  46. #include <linux/usb.h>
  47. #include <linux/usb/serial.h>
  48. #include <linux/ioctl.h>
  49. #include "kobil_sct.h"
  50. static int debug;
  51. /* Version Information */
  52. #define DRIVER_VERSION "21/05/2004"
  53. #define DRIVER_AUTHOR "KOBIL Systems GmbH - http://www.kobil.com"
  54. #define DRIVER_DESC "KOBIL USB Smart Card Terminal Driver (experimental)"
  55. #define KOBIL_VENDOR_ID 0x0D46
  56. #define KOBIL_ADAPTER_B_PRODUCT_ID 0x2011
  57. #define KOBIL_ADAPTER_K_PRODUCT_ID 0x2012
  58. #define KOBIL_USBTWIN_PRODUCT_ID 0x0078
  59. #define KOBIL_KAAN_SIM_PRODUCT_ID 0x0081
  60. #define KOBIL_TIMEOUT 500
  61. #define KOBIL_BUF_LENGTH 300
  62. /* Function prototypes */
  63. static int kobil_startup (struct usb_serial *serial);
  64. static void kobil_shutdown (struct usb_serial *serial);
  65. static int kobil_open (struct usb_serial_port *port, struct file *filp);
  66. static void kobil_close (struct usb_serial_port *port, struct file *filp);
  67. static int kobil_write (struct usb_serial_port *port,
  68. const unsigned char *buf, int count);
  69. static int kobil_write_room(struct usb_serial_port *port);
  70. static int kobil_ioctl(struct usb_serial_port *port, struct file *file,
  71. unsigned int cmd, unsigned long arg);
  72. static int kobil_tiocmget(struct usb_serial_port *port, struct file *file);
  73. static int kobil_tiocmset(struct usb_serial_port *port, struct file *file,
  74. unsigned int set, unsigned int clear);
  75. static void kobil_read_int_callback( struct urb *urb );
  76. static void kobil_write_callback( struct urb *purb );
  77. static struct usb_device_id id_table [] = {
  78. { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_ADAPTER_B_PRODUCT_ID) },
  79. { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_ADAPTER_K_PRODUCT_ID) },
  80. { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_USBTWIN_PRODUCT_ID) },
  81. { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_KAAN_SIM_PRODUCT_ID) },
  82. { } /* Terminating entry */
  83. };
  84. MODULE_DEVICE_TABLE (usb, id_table);
  85. static struct usb_driver kobil_driver = {
  86. .name = "kobil",
  87. .probe = usb_serial_probe,
  88. .disconnect = usb_serial_disconnect,
  89. .id_table = id_table,
  90. .no_dynamic_id = 1,
  91. };
  92. static struct usb_serial_driver kobil_device = {
  93. .driver = {
  94. .owner = THIS_MODULE,
  95. .name = "kobil",
  96. },
  97. .description = "KOBIL USB smart card terminal",
  98. .id_table = id_table,
  99. .num_interrupt_in = NUM_DONT_CARE,
  100. .num_bulk_in = 0,
  101. .num_bulk_out = 0,
  102. .num_ports = 1,
  103. .attach = kobil_startup,
  104. .shutdown = kobil_shutdown,
  105. .ioctl = kobil_ioctl,
  106. .tiocmget = kobil_tiocmget,
  107. .tiocmset = kobil_tiocmset,
  108. .open = kobil_open,
  109. .close = kobil_close,
  110. .write = kobil_write,
  111. .write_room = kobil_write_room,
  112. .read_int_callback = kobil_read_int_callback,
  113. };
  114. struct kobil_private {
  115. int write_int_endpoint_address;
  116. int read_int_endpoint_address;
  117. unsigned char buf[KOBIL_BUF_LENGTH]; // buffer for the APDU to send
  118. int filled; // index of the last char in buf
  119. int cur_pos; // index of the next char to send in buf
  120. __u16 device_type;
  121. int line_state;
  122. struct termios internal_termios;
  123. };
  124. static int kobil_startup (struct usb_serial *serial)
  125. {
  126. int i;
  127. struct kobil_private *priv;
  128. struct usb_device *pdev;
  129. struct usb_host_config *actconfig;
  130. struct usb_interface *interface;
  131. struct usb_host_interface *altsetting;
  132. struct usb_host_endpoint *endpoint;
  133. priv = kmalloc(sizeof(struct kobil_private), GFP_KERNEL);
  134. if (!priv){
  135. return -ENOMEM;
  136. }
  137. priv->filled = 0;
  138. priv->cur_pos = 0;
  139. priv->device_type = le16_to_cpu(serial->dev->descriptor.idProduct);
  140. priv->line_state = 0;
  141. switch (priv->device_type){
  142. case KOBIL_ADAPTER_B_PRODUCT_ID:
  143. printk(KERN_DEBUG "KOBIL B1 PRO / KAAN PRO detected\n");
  144. break;
  145. case KOBIL_ADAPTER_K_PRODUCT_ID:
  146. printk(KERN_DEBUG "KOBIL KAAN Standard Plus / SecOVID Reader Plus detected\n");
  147. break;
  148. case KOBIL_USBTWIN_PRODUCT_ID:
  149. printk(KERN_DEBUG "KOBIL USBTWIN detected\n");
  150. break;
  151. case KOBIL_KAAN_SIM_PRODUCT_ID:
  152. printk(KERN_DEBUG "KOBIL KAAN SIM detected\n");
  153. break;
  154. }
  155. usb_set_serial_port_data(serial->port[0], priv);
  156. // search for the necessary endpoints
  157. pdev = serial->dev;
  158. actconfig = pdev->actconfig;
  159. interface = actconfig->interface[0];
  160. altsetting = interface->cur_altsetting;
  161. endpoint = altsetting->endpoint;
  162. for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
  163. endpoint = &altsetting->endpoint[i];
  164. if (usb_endpoint_is_int_out(&endpoint->desc)) {
  165. dbg("%s Found interrupt out endpoint. Address: %d", __FUNCTION__, endpoint->desc.bEndpointAddress);
  166. priv->write_int_endpoint_address = endpoint->desc.bEndpointAddress;
  167. }
  168. if (usb_endpoint_is_int_in(&endpoint->desc)) {
  169. dbg("%s Found interrupt in endpoint. Address: %d", __FUNCTION__, endpoint->desc.bEndpointAddress);
  170. priv->read_int_endpoint_address = endpoint->desc.bEndpointAddress;
  171. }
  172. }
  173. return 0;
  174. }
  175. static void kobil_shutdown (struct usb_serial *serial)
  176. {
  177. int i;
  178. dbg("%s - port %d", __FUNCTION__, serial->port[0]->number);
  179. for (i=0; i < serial->num_ports; ++i) {
  180. while (serial->port[i]->open_count > 0) {
  181. kobil_close (serial->port[i], NULL);
  182. }
  183. kfree(usb_get_serial_port_data(serial->port[i]));
  184. usb_set_serial_port_data(serial->port[i], NULL);
  185. }
  186. }
  187. static int kobil_open (struct usb_serial_port *port, struct file *filp)
  188. {
  189. int i, result = 0;
  190. struct kobil_private *priv;
  191. unsigned char *transfer_buffer;
  192. int transfer_buffer_length = 8;
  193. int write_urb_transfer_buffer_length = 8;
  194. dbg("%s - port %d", __FUNCTION__, port->number);
  195. priv = usb_get_serial_port_data(port);
  196. priv->line_state = 0;
  197. // someone sets the dev to 0 if the close method has been called
  198. port->interrupt_in_urb->dev = port->serial->dev;
  199. /* force low_latency on so that our tty_push actually forces
  200. * the data through, otherwise it is scheduled, and with high
  201. * data rates (like with OHCI) data can get lost.
  202. */
  203. port->tty->low_latency = 1;
  204. // without this, every push_tty_char is echoed :-(
  205. port->tty->termios->c_lflag = 0;
  206. port->tty->termios->c_lflag &= ~(ISIG | ICANON | ECHO | IEXTEN | XCASE);
  207. port->tty->termios->c_iflag = IGNBRK | IGNPAR | IXOFF;
  208. port->tty->termios->c_oflag &= ~ONLCR; // do NOT translate CR to CR-NL (0x0A -> 0x0A 0x0D)
  209. // set up internal termios structure
  210. priv->internal_termios.c_iflag = port->tty->termios->c_iflag;
  211. priv->internal_termios.c_oflag = port->tty->termios->c_oflag;
  212. priv->internal_termios.c_cflag = port->tty->termios->c_cflag;
  213. priv->internal_termios.c_lflag = port->tty->termios->c_lflag;
  214. for (i=0; i<NCCS; i++) {
  215. priv->internal_termios.c_cc[i] = port->tty->termios->c_cc[i];
  216. }
  217. // allocate memory for transfer buffer
  218. transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
  219. if (! transfer_buffer) {
  220. return -ENOMEM;
  221. }
  222. // allocate write_urb
  223. if (!port->write_urb) {
  224. dbg("%s - port %d Allocating port->write_urb", __FUNCTION__, port->number);
  225. port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
  226. if (!port->write_urb) {
  227. dbg("%s - port %d usb_alloc_urb failed", __FUNCTION__, port->number);
  228. kfree(transfer_buffer);
  229. return -ENOMEM;
  230. }
  231. }
  232. // allocate memory for write_urb transfer buffer
  233. port->write_urb->transfer_buffer = (unsigned char *) kmalloc(write_urb_transfer_buffer_length, GFP_KERNEL);
  234. if (! port->write_urb->transfer_buffer) {
  235. kfree(transfer_buffer);
  236. usb_free_urb(port->write_urb);
  237. port->write_urb = NULL;
  238. return -ENOMEM;
  239. }
  240. // get hardware version
  241. result = usb_control_msg( port->serial->dev,
  242. usb_rcvctrlpipe(port->serial->dev, 0 ),
  243. SUSBCRequest_GetMisc,
  244. USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
  245. SUSBCR_MSC_GetHWVersion,
  246. 0,
  247. transfer_buffer,
  248. transfer_buffer_length,
  249. KOBIL_TIMEOUT
  250. );
  251. dbg("%s - port %d Send get_HW_version URB returns: %i", __FUNCTION__, port->number, result);
  252. dbg("Harware version: %i.%i.%i", transfer_buffer[0], transfer_buffer[1], transfer_buffer[2] );
  253. // get firmware version
  254. result = usb_control_msg( port->serial->dev,
  255. usb_rcvctrlpipe(port->serial->dev, 0 ),
  256. SUSBCRequest_GetMisc,
  257. USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
  258. SUSBCR_MSC_GetFWVersion,
  259. 0,
  260. transfer_buffer,
  261. transfer_buffer_length,
  262. KOBIL_TIMEOUT
  263. );
  264. dbg("%s - port %d Send get_FW_version URB returns: %i", __FUNCTION__, port->number, result);
  265. dbg("Firmware version: %i.%i.%i", transfer_buffer[0], transfer_buffer[1], transfer_buffer[2] );
  266. if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID || priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) {
  267. // Setting Baudrate, Parity and Stopbits
  268. result = usb_control_msg( port->serial->dev,
  269. usb_rcvctrlpipe(port->serial->dev, 0 ),
  270. SUSBCRequest_SetBaudRateParityAndStopBits,
  271. USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
  272. SUSBCR_SBR_9600 | SUSBCR_SPASB_EvenParity | SUSBCR_SPASB_1StopBit,
  273. 0,
  274. transfer_buffer,
  275. 0,
  276. KOBIL_TIMEOUT
  277. );
  278. dbg("%s - port %d Send set_baudrate URB returns: %i", __FUNCTION__, port->number, result);
  279. // reset all queues
  280. result = usb_control_msg( port->serial->dev,
  281. usb_rcvctrlpipe(port->serial->dev, 0 ),
  282. SUSBCRequest_Misc,
  283. USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
  284. SUSBCR_MSC_ResetAllQueues,
  285. 0,
  286. transfer_buffer,
  287. 0,
  288. KOBIL_TIMEOUT
  289. );
  290. dbg("%s - port %d Send reset_all_queues URB returns: %i", __FUNCTION__, port->number, result);
  291. }
  292. if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID || priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID ||
  293. priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID) {
  294. // start reading (Adapter B 'cause PNP string)
  295. result = usb_submit_urb( port->interrupt_in_urb, GFP_ATOMIC );
  296. dbg("%s - port %d Send read URB returns: %i", __FUNCTION__, port->number, result);
  297. }
  298. kfree(transfer_buffer);
  299. return 0;
  300. }
  301. static void kobil_close (struct usb_serial_port *port, struct file *filp)
  302. {
  303. dbg("%s - port %d", __FUNCTION__, port->number);
  304. if (port->write_urb) {
  305. usb_kill_urb(port->write_urb);
  306. usb_free_urb( port->write_urb );
  307. port->write_urb = NULL;
  308. }
  309. usb_kill_urb(port->interrupt_in_urb);
  310. }
  311. static void kobil_read_int_callback( struct urb *purb)
  312. {
  313. int result;
  314. struct usb_serial_port *port = (struct usb_serial_port *) purb->context;
  315. struct tty_struct *tty;
  316. unsigned char *data = purb->transfer_buffer;
  317. // char *dbg_data;
  318. dbg("%s - port %d", __FUNCTION__, port->number);
  319. if (purb->status) {
  320. dbg("%s - port %d Read int status not zero: %d", __FUNCTION__, port->number, purb->status);
  321. return;
  322. }
  323. tty = port->tty;
  324. if (purb->actual_length) {
  325. // BEGIN DEBUG
  326. /*
  327. dbg_data = kzalloc((3 * purb->actual_length + 10) * sizeof(char), GFP_KERNEL);
  328. if (! dbg_data) {
  329. return;
  330. }
  331. for (i = 0; i < purb->actual_length; i++) {
  332. sprintf(dbg_data +3*i, "%02X ", data[i]);
  333. }
  334. dbg(" <-- %s", dbg_data );
  335. kfree(dbg_data);
  336. */
  337. // END DEBUG
  338. tty_buffer_request_room(tty, purb->actual_length);
  339. tty_insert_flip_string(tty, data, purb->actual_length);
  340. tty_flip_buffer_push(tty);
  341. }
  342. // someone sets the dev to 0 if the close method has been called
  343. port->interrupt_in_urb->dev = port->serial->dev;
  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. static void kobil_write_callback( struct urb *purb )
  348. {
  349. }
  350. static int kobil_write (struct usb_serial_port *port,
  351. const unsigned char *buf, int count)
  352. {
  353. int length = 0;
  354. int result = 0;
  355. int todo = 0;
  356. struct kobil_private * priv;
  357. if (count == 0) {
  358. dbg("%s - port %d write request of 0 bytes", __FUNCTION__, port->number);
  359. return 0;
  360. }
  361. priv = usb_get_serial_port_data(port);
  362. if (count > (KOBIL_BUF_LENGTH - priv->filled)) {
  363. dbg("%s - port %d Error: write request bigger than buffer size", __FUNCTION__, port->number);
  364. return -ENOMEM;
  365. }
  366. // Copy data to buffer
  367. memcpy (priv->buf + priv->filled, buf, count);
  368. usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count, priv->buf + priv->filled);
  369. priv->filled = priv->filled + count;
  370. // only send complete block. TWIN, KAAN SIM and adapter K use the same protocol.
  371. if ( ((priv->device_type != KOBIL_ADAPTER_B_PRODUCT_ID) && (priv->filled > 2) && (priv->filled >= (priv->buf[1] + 3))) ||
  372. ((priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) && (priv->filled > 3) && (priv->filled >= (priv->buf[2] + 4))) ) {
  373. // stop reading (except TWIN and KAAN SIM)
  374. if ( (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) || (priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) )
  375. usb_kill_urb(port->interrupt_in_urb);
  376. todo = priv->filled - priv->cur_pos;
  377. while(todo > 0) {
  378. // max 8 byte in one urb (endpoint size)
  379. length = (todo < 8) ? todo : 8;
  380. // copy data to transfer buffer
  381. memcpy(port->write_urb->transfer_buffer, priv->buf + priv->cur_pos, length );
  382. usb_fill_int_urb( port->write_urb,
  383. port->serial->dev,
  384. usb_sndintpipe(port->serial->dev, priv->write_int_endpoint_address),
  385. port->write_urb->transfer_buffer,
  386. length,
  387. kobil_write_callback,
  388. port,
  389. 8
  390. );
  391. priv->cur_pos = priv->cur_pos + length;
  392. result = usb_submit_urb( port->write_urb, GFP_NOIO );
  393. dbg("%s - port %d Send write URB returns: %i", __FUNCTION__, port->number, result);
  394. todo = priv->filled - priv->cur_pos;
  395. if (todo > 0) {
  396. msleep(24);
  397. }
  398. } // end while
  399. priv->filled = 0;
  400. priv->cur_pos = 0;
  401. // someone sets the dev to 0 if the close method has been called
  402. port->interrupt_in_urb->dev = port->serial->dev;
  403. // start reading (except TWIN and KAAN SIM)
  404. if ( (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) || (priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) ) {
  405. // someone sets the dev to 0 if the close method has been called
  406. port->interrupt_in_urb->dev = port->serial->dev;
  407. result = usb_submit_urb( port->interrupt_in_urb, GFP_NOIO );
  408. dbg("%s - port %d Send read URB returns: %i", __FUNCTION__, port->number, result);
  409. }
  410. }
  411. return count;
  412. }
  413. static int kobil_write_room (struct usb_serial_port *port)
  414. {
  415. //dbg("%s - port %d", __FUNCTION__, port->number);
  416. return 8;
  417. }
  418. static int kobil_tiocmget(struct usb_serial_port *port, struct file *file)
  419. {
  420. struct kobil_private * priv;
  421. int result;
  422. unsigned char *transfer_buffer;
  423. int transfer_buffer_length = 8;
  424. priv = usb_get_serial_port_data(port);
  425. if ((priv->device_type == KOBIL_USBTWIN_PRODUCT_ID) || (priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID)) {
  426. // This device doesn't support ioctl calls
  427. return -EINVAL;
  428. }
  429. // allocate memory for transfer buffer
  430. transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
  431. if (!transfer_buffer) {
  432. return -ENOMEM;
  433. }
  434. result = usb_control_msg( port->serial->dev,
  435. usb_rcvctrlpipe(port->serial->dev, 0 ),
  436. SUSBCRequest_GetStatusLineState,
  437. USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
  438. 0,
  439. 0,
  440. transfer_buffer,
  441. transfer_buffer_length,
  442. KOBIL_TIMEOUT);
  443. dbg("%s - port %d Send get_status_line_state URB returns: %i. Statusline: %02x",
  444. __FUNCTION__, port->number, result, transfer_buffer[0]);
  445. if ((transfer_buffer[0] & SUSBCR_GSL_DSR) != 0) {
  446. priv->line_state |= TIOCM_DSR;
  447. } else {
  448. priv->line_state &= ~TIOCM_DSR;
  449. }
  450. kfree(transfer_buffer);
  451. return priv->line_state;
  452. }
  453. static int kobil_tiocmset(struct usb_serial_port *port, struct file *file,
  454. unsigned int set, unsigned int clear)
  455. {
  456. struct kobil_private * priv;
  457. int result;
  458. int dtr = 0;
  459. int rts = 0;
  460. unsigned char *transfer_buffer;
  461. int transfer_buffer_length = 8;
  462. priv = usb_get_serial_port_data(port);
  463. if ((priv->device_type == KOBIL_USBTWIN_PRODUCT_ID) || (priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID)) {
  464. // This device doesn't support ioctl calls
  465. return -EINVAL;
  466. }
  467. // allocate memory for transfer buffer
  468. transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
  469. if (! transfer_buffer) {
  470. return -ENOMEM;
  471. }
  472. if (set & TIOCM_RTS)
  473. rts = 1;
  474. if (set & TIOCM_DTR)
  475. dtr = 1;
  476. if (clear & TIOCM_RTS)
  477. rts = 0;
  478. if (clear & TIOCM_DTR)
  479. dtr = 0;
  480. if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) {
  481. if (dtr != 0)
  482. dbg("%s - port %d Setting DTR", __FUNCTION__, port->number);
  483. else
  484. dbg("%s - port %d Clearing DTR", __FUNCTION__, port->number);
  485. result = usb_control_msg( port->serial->dev,
  486. usb_rcvctrlpipe(port->serial->dev, 0 ),
  487. SUSBCRequest_SetStatusLinesOrQueues,
  488. USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
  489. ((dtr != 0) ? SUSBCR_SSL_SETDTR : SUSBCR_SSL_CLRDTR),
  490. 0,
  491. transfer_buffer,
  492. 0,
  493. KOBIL_TIMEOUT);
  494. } else {
  495. if (rts != 0)
  496. dbg("%s - port %d Setting RTS", __FUNCTION__, port->number);
  497. else
  498. dbg("%s - port %d Clearing RTS", __FUNCTION__, port->number);
  499. result = usb_control_msg( port->serial->dev,
  500. usb_rcvctrlpipe(port->serial->dev, 0 ),
  501. SUSBCRequest_SetStatusLinesOrQueues,
  502. USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
  503. ((rts != 0) ? SUSBCR_SSL_SETRTS : SUSBCR_SSL_CLRRTS),
  504. 0,
  505. transfer_buffer,
  506. 0,
  507. KOBIL_TIMEOUT);
  508. }
  509. dbg("%s - port %d Send set_status_line URB returns: %i", __FUNCTION__, port->number, result);
  510. kfree(transfer_buffer);
  511. return (result < 0) ? result : 0;
  512. }
  513. static int kobil_ioctl(struct usb_serial_port *port, struct file *file,
  514. unsigned int cmd, unsigned long arg)
  515. {
  516. struct kobil_private * priv;
  517. int result;
  518. unsigned short urb_val = 0;
  519. unsigned char *transfer_buffer;
  520. int transfer_buffer_length = 8;
  521. char *settings;
  522. void __user *user_arg = (void __user *)arg;
  523. priv = usb_get_serial_port_data(port);
  524. if ((priv->device_type == KOBIL_USBTWIN_PRODUCT_ID) || (priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID)) {
  525. // This device doesn't support ioctl calls
  526. return 0;
  527. }
  528. switch (cmd) {
  529. case TCGETS: // 0x5401
  530. if (!access_ok(VERIFY_WRITE, user_arg, sizeof(struct termios))) {
  531. dbg("%s - port %d Error in access_ok", __FUNCTION__, port->number);
  532. return -EFAULT;
  533. }
  534. if (kernel_termios_to_user_termios((struct termios __user *)arg,
  535. &priv->internal_termios))
  536. return -EFAULT;
  537. return 0;
  538. case TCSETS: // 0x5402
  539. if (!(port->tty->termios)) {
  540. dbg("%s - port %d Error: port->tty->termios is NULL", __FUNCTION__, port->number);
  541. return -ENOTTY;
  542. }
  543. if (!access_ok(VERIFY_READ, user_arg, sizeof(struct termios))) {
  544. dbg("%s - port %d Error in access_ok", __FUNCTION__, port->number);
  545. return -EFAULT;
  546. }
  547. if (user_termios_to_kernel_termios(&priv->internal_termios,
  548. (struct termios __user *)arg))
  549. return -EFAULT;
  550. settings = kzalloc(50, GFP_KERNEL);
  551. if (! settings) {
  552. return -ENOBUFS;
  553. }
  554. switch (priv->internal_termios.c_cflag & CBAUD) {
  555. case B1200:
  556. urb_val = SUSBCR_SBR_1200;
  557. strcat(settings, "1200 ");
  558. break;
  559. case B9600:
  560. default:
  561. urb_val = SUSBCR_SBR_9600;
  562. strcat(settings, "9600 ");
  563. break;
  564. }
  565. urb_val |= (priv->internal_termios.c_cflag & CSTOPB) ? SUSBCR_SPASB_2StopBits : SUSBCR_SPASB_1StopBit;
  566. strcat(settings, (priv->internal_termios.c_cflag & CSTOPB) ? "2 StopBits " : "1 StopBit ");
  567. if (priv->internal_termios.c_cflag & PARENB) {
  568. if (priv->internal_termios.c_cflag & PARODD) {
  569. urb_val |= SUSBCR_SPASB_OddParity;
  570. strcat(settings, "Odd Parity");
  571. } else {
  572. urb_val |= SUSBCR_SPASB_EvenParity;
  573. strcat(settings, "Even Parity");
  574. }
  575. } else {
  576. urb_val |= SUSBCR_SPASB_NoParity;
  577. strcat(settings, "No Parity");
  578. }
  579. dbg("%s - port %d setting port to: %s", __FUNCTION__, port->number, settings );
  580. result = usb_control_msg( port->serial->dev,
  581. usb_rcvctrlpipe(port->serial->dev, 0 ),
  582. SUSBCRequest_SetBaudRateParityAndStopBits,
  583. USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
  584. urb_val,
  585. 0,
  586. settings,
  587. 0,
  588. KOBIL_TIMEOUT
  589. );
  590. dbg("%s - port %d Send set_baudrate URB returns: %i", __FUNCTION__, port->number, result);
  591. kfree(settings);
  592. return 0;
  593. case TCFLSH: // 0x540B
  594. transfer_buffer = (unsigned char *) kmalloc(transfer_buffer_length, GFP_KERNEL);
  595. if (! transfer_buffer) {
  596. return -ENOBUFS;
  597. }
  598. result = usb_control_msg( port->serial->dev,
  599. usb_rcvctrlpipe(port->serial->dev, 0 ),
  600. SUSBCRequest_Misc,
  601. USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
  602. SUSBCR_MSC_ResetAllQueues,
  603. 0,
  604. NULL,//transfer_buffer,
  605. 0,
  606. KOBIL_TIMEOUT
  607. );
  608. dbg("%s - port %d Send reset_all_queues (FLUSH) URB returns: %i", __FUNCTION__, port->number, result);
  609. kfree(transfer_buffer);
  610. return ((result < 0) ? -EFAULT : 0);
  611. }
  612. return -ENOIOCTLCMD;
  613. }
  614. static int __init kobil_init (void)
  615. {
  616. int retval;
  617. retval = usb_serial_register(&kobil_device);
  618. if (retval)
  619. goto failed_usb_serial_register;
  620. retval = usb_register(&kobil_driver);
  621. if (retval)
  622. goto failed_usb_register;
  623. info(DRIVER_VERSION " " DRIVER_AUTHOR);
  624. info(DRIVER_DESC);
  625. return 0;
  626. failed_usb_register:
  627. usb_serial_deregister(&kobil_device);
  628. failed_usb_serial_register:
  629. return retval;
  630. }
  631. static void __exit kobil_exit (void)
  632. {
  633. usb_deregister (&kobil_driver);
  634. usb_serial_deregister (&kobil_device);
  635. }
  636. module_init(kobil_init);
  637. module_exit(kobil_exit);
  638. MODULE_AUTHOR( DRIVER_AUTHOR );
  639. MODULE_DESCRIPTION( DRIVER_DESC );
  640. MODULE_LICENSE( "GPL" );
  641. module_param(debug, bool, S_IRUGO | S_IWUSR);
  642. MODULE_PARM_DESC(debug, "Debug enabled or not");