kobil_sct.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  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. #include <linux/kernel.h>
  25. #include <linux/errno.h>
  26. #include <linux/init.h>
  27. #include <linux/slab.h>
  28. #include <linux/tty.h>
  29. #include <linux/tty_driver.h>
  30. #include <linux/tty_flip.h>
  31. #include <linux/module.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/uaccess.h>
  34. #include <linux/usb.h>
  35. #include <linux/usb/serial.h>
  36. #include <linux/ioctl.h>
  37. #include "kobil_sct.h"
  38. #define DRIVER_AUTHOR "KOBIL Systems GmbH - http://www.kobil.com"
  39. #define DRIVER_DESC "KOBIL USB Smart Card Terminal Driver (experimental)"
  40. #define KOBIL_VENDOR_ID 0x0D46
  41. #define KOBIL_ADAPTER_B_PRODUCT_ID 0x2011
  42. #define KOBIL_ADAPTER_K_PRODUCT_ID 0x2012
  43. #define KOBIL_USBTWIN_PRODUCT_ID 0x0078
  44. #define KOBIL_KAAN_SIM_PRODUCT_ID 0x0081
  45. #define KOBIL_TIMEOUT 500
  46. #define KOBIL_BUF_LENGTH 300
  47. /* Function prototypes */
  48. static int kobil_port_probe(struct usb_serial_port *probe);
  49. static int kobil_port_remove(struct usb_serial_port *probe);
  50. static int kobil_open(struct tty_struct *tty, struct usb_serial_port *port);
  51. static void kobil_close(struct usb_serial_port *port);
  52. static int kobil_write(struct tty_struct *tty, struct usb_serial_port *port,
  53. const unsigned char *buf, int count);
  54. static int kobil_write_room(struct tty_struct *tty);
  55. static int kobil_ioctl(struct tty_struct *tty,
  56. unsigned int cmd, unsigned long arg);
  57. static int kobil_tiocmget(struct tty_struct *tty);
  58. static int kobil_tiocmset(struct tty_struct *tty,
  59. unsigned int set, unsigned int clear);
  60. static void kobil_read_int_callback(struct urb *urb);
  61. static void kobil_write_callback(struct urb *purb);
  62. static void kobil_set_termios(struct tty_struct *tty,
  63. struct usb_serial_port *port, struct ktermios *old);
  64. static void kobil_init_termios(struct tty_struct *tty);
  65. static const struct usb_device_id id_table[] = {
  66. { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_ADAPTER_B_PRODUCT_ID) },
  67. { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_ADAPTER_K_PRODUCT_ID) },
  68. { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_USBTWIN_PRODUCT_ID) },
  69. { USB_DEVICE(KOBIL_VENDOR_ID, KOBIL_KAAN_SIM_PRODUCT_ID) },
  70. { } /* Terminating entry */
  71. };
  72. MODULE_DEVICE_TABLE(usb, id_table);
  73. static struct usb_serial_driver kobil_device = {
  74. .driver = {
  75. .owner = THIS_MODULE,
  76. .name = "kobil",
  77. },
  78. .description = "KOBIL USB smart card terminal",
  79. .id_table = id_table,
  80. .num_ports = 1,
  81. .port_probe = kobil_port_probe,
  82. .port_remove = kobil_port_remove,
  83. .ioctl = kobil_ioctl,
  84. .set_termios = kobil_set_termios,
  85. .init_termios = kobil_init_termios,
  86. .tiocmget = kobil_tiocmget,
  87. .tiocmset = kobil_tiocmset,
  88. .open = kobil_open,
  89. .close = kobil_close,
  90. .write = kobil_write,
  91. .write_room = kobil_write_room,
  92. .read_int_callback = kobil_read_int_callback,
  93. };
  94. static struct usb_serial_driver * const serial_drivers[] = {
  95. &kobil_device, NULL
  96. };
  97. struct kobil_private {
  98. int write_int_endpoint_address;
  99. int read_int_endpoint_address;
  100. unsigned char buf[KOBIL_BUF_LENGTH]; /* buffer for the APDU to send */
  101. int filled; /* index of the last char in buf */
  102. int cur_pos; /* index of the next char to send in buf */
  103. __u16 device_type;
  104. };
  105. static int kobil_port_probe(struct usb_serial_port *port)
  106. {
  107. int i;
  108. struct usb_serial *serial = port->serial;
  109. struct kobil_private *priv;
  110. struct usb_device *pdev;
  111. struct usb_host_config *actconfig;
  112. struct usb_interface *interface;
  113. struct usb_host_interface *altsetting;
  114. struct usb_host_endpoint *endpoint;
  115. priv = kmalloc(sizeof(struct kobil_private), GFP_KERNEL);
  116. if (!priv)
  117. return -ENOMEM;
  118. priv->filled = 0;
  119. priv->cur_pos = 0;
  120. priv->device_type = le16_to_cpu(serial->dev->descriptor.idProduct);
  121. switch (priv->device_type) {
  122. case KOBIL_ADAPTER_B_PRODUCT_ID:
  123. dev_dbg(&serial->dev->dev, "KOBIL B1 PRO / KAAN PRO detected\n");
  124. break;
  125. case KOBIL_ADAPTER_K_PRODUCT_ID:
  126. dev_dbg(&serial->dev->dev, "KOBIL KAAN Standard Plus / SecOVID Reader Plus detected\n");
  127. break;
  128. case KOBIL_USBTWIN_PRODUCT_ID:
  129. dev_dbg(&serial->dev->dev, "KOBIL USBTWIN detected\n");
  130. break;
  131. case KOBIL_KAAN_SIM_PRODUCT_ID:
  132. dev_dbg(&serial->dev->dev, "KOBIL KAAN SIM detected\n");
  133. break;
  134. }
  135. usb_set_serial_port_data(port, priv);
  136. /* search for the necessary endpoints */
  137. pdev = serial->dev;
  138. actconfig = pdev->actconfig;
  139. interface = actconfig->interface[0];
  140. altsetting = interface->cur_altsetting;
  141. endpoint = altsetting->endpoint;
  142. for (i = 0; i < altsetting->desc.bNumEndpoints; i++) {
  143. endpoint = &altsetting->endpoint[i];
  144. if (usb_endpoint_is_int_out(&endpoint->desc)) {
  145. dev_dbg(&serial->dev->dev,
  146. "%s Found interrupt out endpoint. Address: %d\n",
  147. __func__, endpoint->desc.bEndpointAddress);
  148. priv->write_int_endpoint_address =
  149. endpoint->desc.bEndpointAddress;
  150. }
  151. if (usb_endpoint_is_int_in(&endpoint->desc)) {
  152. dev_dbg(&serial->dev->dev,
  153. "%s Found interrupt in endpoint. Address: %d\n",
  154. __func__, endpoint->desc.bEndpointAddress);
  155. priv->read_int_endpoint_address =
  156. endpoint->desc.bEndpointAddress;
  157. }
  158. }
  159. return 0;
  160. }
  161. static int kobil_port_remove(struct usb_serial_port *port)
  162. {
  163. struct kobil_private *priv;
  164. priv = usb_get_serial_port_data(port);
  165. kfree(priv);
  166. return 0;
  167. }
  168. static void kobil_init_termios(struct tty_struct *tty)
  169. {
  170. /* Default to echo off and other sane device settings */
  171. tty->termios.c_lflag = 0;
  172. tty->termios.c_iflag &= ~(ISIG | ICANON | ECHO | IEXTEN | XCASE);
  173. tty->termios.c_iflag |= IGNBRK | IGNPAR | IXOFF;
  174. /* do NOT translate CR to CR-NL (0x0A -> 0x0A 0x0D) */
  175. tty->termios.c_oflag &= ~ONLCR;
  176. }
  177. static int kobil_open(struct tty_struct *tty, struct usb_serial_port *port)
  178. {
  179. struct device *dev = &port->dev;
  180. int result = 0;
  181. struct kobil_private *priv;
  182. unsigned char *transfer_buffer;
  183. int transfer_buffer_length = 8;
  184. int write_urb_transfer_buffer_length = 8;
  185. priv = usb_get_serial_port_data(port);
  186. /* allocate memory for transfer buffer */
  187. transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
  188. if (!transfer_buffer)
  189. return -ENOMEM;
  190. /* allocate write_urb */
  191. if (!port->write_urb) {
  192. dev_dbg(dev, "%s - Allocating port->write_urb\n", __func__);
  193. port->write_urb = usb_alloc_urb(0, GFP_KERNEL);
  194. if (!port->write_urb) {
  195. dev_dbg(dev, "%s - usb_alloc_urb failed\n", __func__);
  196. kfree(transfer_buffer);
  197. return -ENOMEM;
  198. }
  199. }
  200. /* allocate memory for write_urb transfer buffer */
  201. port->write_urb->transfer_buffer =
  202. kmalloc(write_urb_transfer_buffer_length, GFP_KERNEL);
  203. if (!port->write_urb->transfer_buffer) {
  204. kfree(transfer_buffer);
  205. usb_free_urb(port->write_urb);
  206. port->write_urb = NULL;
  207. return -ENOMEM;
  208. }
  209. /* get hardware version */
  210. result = usb_control_msg(port->serial->dev,
  211. usb_rcvctrlpipe(port->serial->dev, 0),
  212. SUSBCRequest_GetMisc,
  213. USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
  214. SUSBCR_MSC_GetHWVersion,
  215. 0,
  216. transfer_buffer,
  217. transfer_buffer_length,
  218. KOBIL_TIMEOUT
  219. );
  220. dev_dbg(dev, "%s - Send get_HW_version URB returns: %i\n", __func__, result);
  221. dev_dbg(dev, "Harware version: %i.%i.%i\n", transfer_buffer[0],
  222. transfer_buffer[1], transfer_buffer[2]);
  223. /* get firmware version */
  224. result = usb_control_msg(port->serial->dev,
  225. usb_rcvctrlpipe(port->serial->dev, 0),
  226. SUSBCRequest_GetMisc,
  227. USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
  228. SUSBCR_MSC_GetFWVersion,
  229. 0,
  230. transfer_buffer,
  231. transfer_buffer_length,
  232. KOBIL_TIMEOUT
  233. );
  234. dev_dbg(dev, "%s - Send get_FW_version URB returns: %i\n", __func__, result);
  235. dev_dbg(dev, "Firmware version: %i.%i.%i\n", transfer_buffer[0],
  236. transfer_buffer[1], transfer_buffer[2]);
  237. if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID ||
  238. priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) {
  239. /* Setting Baudrate, Parity and Stopbits */
  240. result = usb_control_msg(port->serial->dev,
  241. usb_rcvctrlpipe(port->serial->dev, 0),
  242. SUSBCRequest_SetBaudRateParityAndStopBits,
  243. USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
  244. SUSBCR_SBR_9600 | SUSBCR_SPASB_EvenParity |
  245. SUSBCR_SPASB_1StopBit,
  246. 0,
  247. transfer_buffer,
  248. 0,
  249. KOBIL_TIMEOUT
  250. );
  251. dev_dbg(dev, "%s - Send set_baudrate URB returns: %i\n", __func__, result);
  252. /* reset all queues */
  253. result = usb_control_msg(port->serial->dev,
  254. usb_rcvctrlpipe(port->serial->dev, 0),
  255. SUSBCRequest_Misc,
  256. USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
  257. SUSBCR_MSC_ResetAllQueues,
  258. 0,
  259. transfer_buffer,
  260. 0,
  261. KOBIL_TIMEOUT
  262. );
  263. dev_dbg(dev, "%s - Send reset_all_queues URB returns: %i\n", __func__, result);
  264. }
  265. if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID ||
  266. priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID ||
  267. priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID) {
  268. /* start reading (Adapter B 'cause PNP string) */
  269. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  270. dev_dbg(dev, "%s - Send read URB returns: %i\n", __func__, result);
  271. }
  272. kfree(transfer_buffer);
  273. return 0;
  274. }
  275. static void kobil_close(struct usb_serial_port *port)
  276. {
  277. /* FIXME: Add rts/dtr methods */
  278. if (port->write_urb) {
  279. usb_poison_urb(port->write_urb);
  280. kfree(port->write_urb->transfer_buffer);
  281. usb_free_urb(port->write_urb);
  282. port->write_urb = NULL;
  283. }
  284. usb_kill_urb(port->interrupt_in_urb);
  285. }
  286. static void kobil_read_int_callback(struct urb *urb)
  287. {
  288. int result;
  289. struct usb_serial_port *port = urb->context;
  290. unsigned char *data = urb->transfer_buffer;
  291. int status = urb->status;
  292. if (status) {
  293. dev_dbg(&port->dev, "%s - Read int status not zero: %d\n", __func__, status);
  294. return;
  295. }
  296. if (urb->actual_length) {
  297. /* BEGIN DEBUG */
  298. /*
  299. char *dbg_data;
  300. dbg_data = kzalloc((3 * purb->actual_length + 10)
  301. * sizeof(char), GFP_KERNEL);
  302. if (! dbg_data) {
  303. return;
  304. }
  305. for (i = 0; i < purb->actual_length; i++) {
  306. sprintf(dbg_data +3*i, "%02X ", data[i]);
  307. }
  308. dev_dbg(&port->dev, " <-- %s\n", dbg_data);
  309. kfree(dbg_data);
  310. */
  311. /* END DEBUG */
  312. tty_insert_flip_string(&port->port, data, urb->actual_length);
  313. tty_flip_buffer_push(&port->port);
  314. }
  315. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  316. dev_dbg(&port->dev, "%s - Send read URB returns: %i\n", __func__, result);
  317. }
  318. static void kobil_write_callback(struct urb *purb)
  319. {
  320. }
  321. static int kobil_write(struct tty_struct *tty, struct usb_serial_port *port,
  322. const unsigned char *buf, int count)
  323. {
  324. int length = 0;
  325. int result = 0;
  326. int todo = 0;
  327. struct kobil_private *priv;
  328. if (count == 0) {
  329. dev_dbg(&port->dev, "%s - write request of 0 bytes\n", __func__);
  330. return 0;
  331. }
  332. priv = usb_get_serial_port_data(port);
  333. if (count > (KOBIL_BUF_LENGTH - priv->filled)) {
  334. dev_dbg(&port->dev, "%s - Error: write request bigger than buffer size\n", __func__);
  335. return -ENOMEM;
  336. }
  337. /* Copy data to buffer */
  338. memcpy(priv->buf + priv->filled, buf, count);
  339. usb_serial_debug_data(&port->dev, __func__, count, priv->buf + priv->filled);
  340. priv->filled = priv->filled + count;
  341. /* only send complete block. TWIN, KAAN SIM and adapter K
  342. use the same protocol. */
  343. if (((priv->device_type != KOBIL_ADAPTER_B_PRODUCT_ID) && (priv->filled > 2) && (priv->filled >= (priv->buf[1] + 3))) ||
  344. ((priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) && (priv->filled > 3) && (priv->filled >= (priv->buf[2] + 4)))) {
  345. /* stop reading (except TWIN and KAAN SIM) */
  346. if ((priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID)
  347. || (priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID))
  348. usb_kill_urb(port->interrupt_in_urb);
  349. todo = priv->filled - priv->cur_pos;
  350. while (todo > 0) {
  351. /* max 8 byte in one urb (endpoint size) */
  352. length = (todo < 8) ? todo : 8;
  353. /* copy data to transfer buffer */
  354. memcpy(port->write_urb->transfer_buffer,
  355. priv->buf + priv->cur_pos, length);
  356. usb_fill_int_urb(port->write_urb,
  357. port->serial->dev,
  358. usb_sndintpipe(port->serial->dev,
  359. priv->write_int_endpoint_address),
  360. port->write_urb->transfer_buffer,
  361. length,
  362. kobil_write_callback,
  363. port,
  364. 8
  365. );
  366. priv->cur_pos = priv->cur_pos + length;
  367. result = usb_submit_urb(port->write_urb, GFP_NOIO);
  368. dev_dbg(&port->dev, "%s - Send write URB returns: %i\n", __func__, result);
  369. todo = priv->filled - priv->cur_pos;
  370. if (todo > 0)
  371. msleep(24);
  372. }
  373. priv->filled = 0;
  374. priv->cur_pos = 0;
  375. /* start reading (except TWIN and KAAN SIM) */
  376. if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID ||
  377. priv->device_type == KOBIL_ADAPTER_K_PRODUCT_ID) {
  378. result = usb_submit_urb(port->interrupt_in_urb,
  379. GFP_NOIO);
  380. dev_dbg(&port->dev, "%s - Send read URB returns: %i\n", __func__, result);
  381. }
  382. }
  383. return count;
  384. }
  385. static int kobil_write_room(struct tty_struct *tty)
  386. {
  387. /* FIXME */
  388. return 8;
  389. }
  390. static int kobil_tiocmget(struct tty_struct *tty)
  391. {
  392. struct usb_serial_port *port = tty->driver_data;
  393. struct kobil_private *priv;
  394. int result;
  395. unsigned char *transfer_buffer;
  396. int transfer_buffer_length = 8;
  397. priv = usb_get_serial_port_data(port);
  398. if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID
  399. || priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID) {
  400. /* This device doesn't support ioctl calls */
  401. return -EINVAL;
  402. }
  403. /* allocate memory for transfer buffer */
  404. transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
  405. if (!transfer_buffer)
  406. return -ENOMEM;
  407. result = usb_control_msg(port->serial->dev,
  408. usb_rcvctrlpipe(port->serial->dev, 0),
  409. SUSBCRequest_GetStatusLineState,
  410. USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_IN,
  411. 0,
  412. 0,
  413. transfer_buffer,
  414. transfer_buffer_length,
  415. KOBIL_TIMEOUT);
  416. dev_dbg(&port->dev, "%s - Send get_status_line_state URB returns: %i. Statusline: %02x\n",
  417. __func__, result, transfer_buffer[0]);
  418. result = 0;
  419. if ((transfer_buffer[0] & SUSBCR_GSL_DSR) != 0)
  420. result = TIOCM_DSR;
  421. kfree(transfer_buffer);
  422. return result;
  423. }
  424. static int kobil_tiocmset(struct tty_struct *tty,
  425. unsigned int set, unsigned int clear)
  426. {
  427. struct usb_serial_port *port = tty->driver_data;
  428. struct device *dev = &port->dev;
  429. struct kobil_private *priv;
  430. int result;
  431. int dtr = 0;
  432. int rts = 0;
  433. unsigned char *transfer_buffer;
  434. int transfer_buffer_length = 8;
  435. /* FIXME: locking ? */
  436. priv = usb_get_serial_port_data(port);
  437. if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID
  438. || priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID) {
  439. /* This device doesn't support ioctl calls */
  440. return -EINVAL;
  441. }
  442. /* allocate memory for transfer buffer */
  443. transfer_buffer = kzalloc(transfer_buffer_length, GFP_KERNEL);
  444. if (!transfer_buffer)
  445. return -ENOMEM;
  446. if (set & TIOCM_RTS)
  447. rts = 1;
  448. if (set & TIOCM_DTR)
  449. dtr = 1;
  450. if (clear & TIOCM_RTS)
  451. rts = 0;
  452. if (clear & TIOCM_DTR)
  453. dtr = 0;
  454. if (priv->device_type == KOBIL_ADAPTER_B_PRODUCT_ID) {
  455. if (dtr != 0)
  456. dev_dbg(dev, "%s - Setting DTR\n", __func__);
  457. else
  458. dev_dbg(dev, "%s - Clearing DTR\n", __func__);
  459. result = usb_control_msg(port->serial->dev,
  460. usb_rcvctrlpipe(port->serial->dev, 0),
  461. SUSBCRequest_SetStatusLinesOrQueues,
  462. USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
  463. ((dtr != 0) ? SUSBCR_SSL_SETDTR : SUSBCR_SSL_CLRDTR),
  464. 0,
  465. transfer_buffer,
  466. 0,
  467. KOBIL_TIMEOUT);
  468. } else {
  469. if (rts != 0)
  470. dev_dbg(dev, "%s - Setting RTS\n", __func__);
  471. else
  472. dev_dbg(dev, "%s - Clearing RTS\n", __func__);
  473. result = usb_control_msg(port->serial->dev,
  474. usb_rcvctrlpipe(port->serial->dev, 0),
  475. SUSBCRequest_SetStatusLinesOrQueues,
  476. USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
  477. ((rts != 0) ? SUSBCR_SSL_SETRTS : SUSBCR_SSL_CLRRTS),
  478. 0,
  479. transfer_buffer,
  480. 0,
  481. KOBIL_TIMEOUT);
  482. }
  483. dev_dbg(dev, "%s - Send set_status_line URB returns: %i\n", __func__, result);
  484. kfree(transfer_buffer);
  485. return (result < 0) ? result : 0;
  486. }
  487. static void kobil_set_termios(struct tty_struct *tty,
  488. struct usb_serial_port *port, struct ktermios *old)
  489. {
  490. struct kobil_private *priv;
  491. int result;
  492. unsigned short urb_val = 0;
  493. int c_cflag = tty->termios.c_cflag;
  494. speed_t speed;
  495. priv = usb_get_serial_port_data(port);
  496. if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID ||
  497. priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID) {
  498. /* This device doesn't support ioctl calls */
  499. tty_termios_copy_hw(&tty->termios, old);
  500. return;
  501. }
  502. speed = tty_get_baud_rate(tty);
  503. switch (speed) {
  504. case 1200:
  505. urb_val = SUSBCR_SBR_1200;
  506. break;
  507. default:
  508. speed = 9600;
  509. case 9600:
  510. urb_val = SUSBCR_SBR_9600;
  511. break;
  512. }
  513. urb_val |= (c_cflag & CSTOPB) ? SUSBCR_SPASB_2StopBits :
  514. SUSBCR_SPASB_1StopBit;
  515. if (c_cflag & PARENB) {
  516. if (c_cflag & PARODD)
  517. urb_val |= SUSBCR_SPASB_OddParity;
  518. else
  519. urb_val |= SUSBCR_SPASB_EvenParity;
  520. } else
  521. urb_val |= SUSBCR_SPASB_NoParity;
  522. tty->termios.c_cflag &= ~CMSPAR;
  523. tty_encode_baud_rate(tty, speed, speed);
  524. result = usb_control_msg(port->serial->dev,
  525. usb_rcvctrlpipe(port->serial->dev, 0),
  526. SUSBCRequest_SetBaudRateParityAndStopBits,
  527. USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
  528. urb_val,
  529. 0,
  530. NULL,
  531. 0,
  532. KOBIL_TIMEOUT
  533. );
  534. }
  535. static int kobil_ioctl(struct tty_struct *tty,
  536. unsigned int cmd, unsigned long arg)
  537. {
  538. struct usb_serial_port *port = tty->driver_data;
  539. struct kobil_private *priv = usb_get_serial_port_data(port);
  540. unsigned char *transfer_buffer;
  541. int transfer_buffer_length = 8;
  542. int result;
  543. if (priv->device_type == KOBIL_USBTWIN_PRODUCT_ID ||
  544. priv->device_type == KOBIL_KAAN_SIM_PRODUCT_ID)
  545. /* This device doesn't support ioctl calls */
  546. return -ENOIOCTLCMD;
  547. switch (cmd) {
  548. case TCFLSH:
  549. transfer_buffer = kmalloc(transfer_buffer_length, GFP_KERNEL);
  550. if (!transfer_buffer)
  551. return -ENOBUFS;
  552. result = usb_control_msg(port->serial->dev,
  553. usb_rcvctrlpipe(port->serial->dev, 0),
  554. SUSBCRequest_Misc,
  555. USB_TYPE_VENDOR | USB_RECIP_ENDPOINT | USB_DIR_OUT,
  556. SUSBCR_MSC_ResetAllQueues,
  557. 0,
  558. NULL, /* transfer_buffer, */
  559. 0,
  560. KOBIL_TIMEOUT
  561. );
  562. dev_dbg(&port->dev,
  563. "%s - Send reset_all_queues (FLUSH) URB returns: %i", __func__, result);
  564. kfree(transfer_buffer);
  565. return (result < 0) ? -EIO: 0;
  566. default:
  567. return -ENOIOCTLCMD;
  568. }
  569. }
  570. module_usb_serial_driver(serial_drivers, id_table);
  571. MODULE_AUTHOR(DRIVER_AUTHOR);
  572. MODULE_DESCRIPTION(DRIVER_DESC);
  573. MODULE_LICENSE("GPL");