kobil_sct.c 19 KB

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