kobil_sct.c 22 KB

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