oti6858.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Ours Technology Inc. OTi-6858 USB to serial adapter driver.
  4. *
  5. * Copyleft (C) 2007 Kees Lemmens (adapted for kernel 2.6.20)
  6. * Copyright (C) 2006 Tomasz Michal Lukaszewski (FIXME: add e-mail)
  7. * Copyright (C) 2001-2004 Greg Kroah-Hartman ([email protected])
  8. * Copyright (C) 2003 IBM Corp.
  9. *
  10. * Many thanks to the authors of pl2303 driver: all functions in this file
  11. * are heavily based on pl2303 code, buffering code is a 1-to-1 copy.
  12. *
  13. * Warning! You use this driver on your own risk! The only official
  14. * description of this device I have is datasheet from manufacturer,
  15. * and it doesn't contain almost any information needed to write a driver.
  16. * Almost all knowlegde used while writing this driver was gathered by:
  17. * - analyzing traffic between device and the M$ Windows 2000 driver,
  18. * - trying different bit combinations and checking pin states
  19. * with a voltmeter,
  20. * - receiving malformed frames and producing buffer overflows
  21. * to learn how errors are reported,
  22. * So, THIS CODE CAN DESTROY OTi-6858 AND ANY OTHER DEVICES, THAT ARE
  23. * CONNECTED TO IT!
  24. *
  25. * See Documentation/usb/usb-serial.rst for more information on using this
  26. * driver
  27. *
  28. * TODO:
  29. * - implement correct flushing for ioctls and oti6858_close()
  30. * - check how errors (rx overflow, parity error, framing error) are reported
  31. * - implement oti6858_break_ctl()
  32. * - implement more ioctls
  33. * - test/implement flow control
  34. * - allow setting custom baud rates
  35. */
  36. #include <linux/kernel.h>
  37. #include <linux/errno.h>
  38. #include <linux/slab.h>
  39. #include <linux/tty.h>
  40. #include <linux/tty_driver.h>
  41. #include <linux/tty_flip.h>
  42. #include <linux/serial.h>
  43. #include <linux/module.h>
  44. #include <linux/moduleparam.h>
  45. #include <linux/spinlock.h>
  46. #include <linux/usb.h>
  47. #include <linux/usb/serial.h>
  48. #include <linux/uaccess.h>
  49. #include <linux/kfifo.h>
  50. #include "oti6858.h"
  51. #define OTI6858_DESCRIPTION \
  52. "Ours Technology Inc. OTi-6858 USB to serial adapter driver"
  53. #define OTI6858_AUTHOR "Tomasz Michal Lukaszewski <FIXME@FIXME>"
  54. static const struct usb_device_id id_table[] = {
  55. { USB_DEVICE(OTI6858_VENDOR_ID, OTI6858_PRODUCT_ID) },
  56. { }
  57. };
  58. MODULE_DEVICE_TABLE(usb, id_table);
  59. /* requests */
  60. #define OTI6858_REQ_GET_STATUS (USB_DIR_IN | USB_TYPE_VENDOR | 0x00)
  61. #define OTI6858_REQ_T_GET_STATUS 0x01
  62. #define OTI6858_REQ_SET_LINE (USB_DIR_OUT | USB_TYPE_VENDOR | 0x00)
  63. #define OTI6858_REQ_T_SET_LINE 0x00
  64. #define OTI6858_REQ_CHECK_TXBUFF (USB_DIR_IN | USB_TYPE_VENDOR | 0x01)
  65. #define OTI6858_REQ_T_CHECK_TXBUFF 0x00
  66. /* format of the control packet */
  67. struct oti6858_control_pkt {
  68. __le16 divisor; /* baud rate = 96000000 / (16 * divisor), LE */
  69. #define OTI6858_MAX_BAUD_RATE 3000000
  70. u8 frame_fmt;
  71. #define FMT_STOP_BITS_MASK 0xc0
  72. #define FMT_STOP_BITS_1 0x00
  73. #define FMT_STOP_BITS_2 0x40 /* 1.5 stop bits if FMT_DATA_BITS_5 */
  74. #define FMT_PARITY_MASK 0x38
  75. #define FMT_PARITY_NONE 0x00
  76. #define FMT_PARITY_ODD 0x08
  77. #define FMT_PARITY_EVEN 0x18
  78. #define FMT_PARITY_MARK 0x28
  79. #define FMT_PARITY_SPACE 0x38
  80. #define FMT_DATA_BITS_MASK 0x03
  81. #define FMT_DATA_BITS_5 0x00
  82. #define FMT_DATA_BITS_6 0x01
  83. #define FMT_DATA_BITS_7 0x02
  84. #define FMT_DATA_BITS_8 0x03
  85. u8 something; /* always equals 0x43 */
  86. u8 control; /* settings of flow control lines */
  87. #define CONTROL_MASK 0x0c
  88. #define CONTROL_DTR_HIGH 0x08
  89. #define CONTROL_RTS_HIGH 0x04
  90. u8 tx_status;
  91. #define TX_BUFFER_EMPTIED 0x09
  92. u8 pin_state;
  93. #define PIN_MASK 0x3f
  94. #define PIN_MSR_MASK 0x1b
  95. #define PIN_RTS 0x20 /* output pin */
  96. #define PIN_CTS 0x10 /* input pin, active low */
  97. #define PIN_DSR 0x08 /* input pin, active low */
  98. #define PIN_DTR 0x04 /* output pin */
  99. #define PIN_RI 0x02 /* input pin, active low */
  100. #define PIN_DCD 0x01 /* input pin, active low */
  101. u8 rx_bytes_avail; /* number of bytes in rx buffer */;
  102. };
  103. #define OTI6858_CTRL_PKT_SIZE sizeof(struct oti6858_control_pkt)
  104. #define OTI6858_CTRL_EQUALS_PENDING(a, priv) \
  105. (((a)->divisor == (priv)->pending_setup.divisor) \
  106. && ((a)->control == (priv)->pending_setup.control) \
  107. && ((a)->frame_fmt == (priv)->pending_setup.frame_fmt))
  108. /* function prototypes */
  109. static int oti6858_open(struct tty_struct *tty, struct usb_serial_port *port);
  110. static void oti6858_close(struct usb_serial_port *port);
  111. static void oti6858_set_termios(struct tty_struct *tty,
  112. struct usb_serial_port *port,
  113. const struct ktermios *old_termios);
  114. static void oti6858_init_termios(struct tty_struct *tty);
  115. static void oti6858_read_int_callback(struct urb *urb);
  116. static void oti6858_read_bulk_callback(struct urb *urb);
  117. static void oti6858_write_bulk_callback(struct urb *urb);
  118. static int oti6858_write(struct tty_struct *tty, struct usb_serial_port *port,
  119. const unsigned char *buf, int count);
  120. static unsigned int oti6858_write_room(struct tty_struct *tty);
  121. static unsigned int oti6858_chars_in_buffer(struct tty_struct *tty);
  122. static int oti6858_tiocmget(struct tty_struct *tty);
  123. static int oti6858_tiocmset(struct tty_struct *tty,
  124. unsigned int set, unsigned int clear);
  125. static int oti6858_port_probe(struct usb_serial_port *port);
  126. static void oti6858_port_remove(struct usb_serial_port *port);
  127. /* device info */
  128. static struct usb_serial_driver oti6858_device = {
  129. .driver = {
  130. .owner = THIS_MODULE,
  131. .name = "oti6858",
  132. },
  133. .id_table = id_table,
  134. .num_ports = 1,
  135. .num_bulk_in = 1,
  136. .num_bulk_out = 1,
  137. .num_interrupt_in = 1,
  138. .open = oti6858_open,
  139. .close = oti6858_close,
  140. .write = oti6858_write,
  141. .set_termios = oti6858_set_termios,
  142. .init_termios = oti6858_init_termios,
  143. .tiocmget = oti6858_tiocmget,
  144. .tiocmset = oti6858_tiocmset,
  145. .tiocmiwait = usb_serial_generic_tiocmiwait,
  146. .read_bulk_callback = oti6858_read_bulk_callback,
  147. .read_int_callback = oti6858_read_int_callback,
  148. .write_bulk_callback = oti6858_write_bulk_callback,
  149. .write_room = oti6858_write_room,
  150. .chars_in_buffer = oti6858_chars_in_buffer,
  151. .port_probe = oti6858_port_probe,
  152. .port_remove = oti6858_port_remove,
  153. };
  154. static struct usb_serial_driver * const serial_drivers[] = {
  155. &oti6858_device, NULL
  156. };
  157. struct oti6858_private {
  158. spinlock_t lock;
  159. struct oti6858_control_pkt status;
  160. struct {
  161. u8 read_urb_in_use;
  162. u8 write_urb_in_use;
  163. } flags;
  164. struct delayed_work delayed_write_work;
  165. struct {
  166. __le16 divisor;
  167. u8 frame_fmt;
  168. u8 control;
  169. } pending_setup;
  170. u8 transient;
  171. u8 setup_done;
  172. struct delayed_work delayed_setup_work;
  173. struct usb_serial_port *port; /* USB port with which associated */
  174. };
  175. static void setup_line(struct work_struct *work)
  176. {
  177. struct oti6858_private *priv = container_of(work,
  178. struct oti6858_private, delayed_setup_work.work);
  179. struct usb_serial_port *port = priv->port;
  180. struct oti6858_control_pkt *new_setup;
  181. unsigned long flags;
  182. int result;
  183. new_setup = kmalloc(OTI6858_CTRL_PKT_SIZE, GFP_KERNEL);
  184. if (!new_setup) {
  185. /* we will try again */
  186. schedule_delayed_work(&priv->delayed_setup_work,
  187. msecs_to_jiffies(2));
  188. return;
  189. }
  190. result = usb_control_msg(port->serial->dev,
  191. usb_rcvctrlpipe(port->serial->dev, 0),
  192. OTI6858_REQ_T_GET_STATUS,
  193. OTI6858_REQ_GET_STATUS,
  194. 0, 0,
  195. new_setup, OTI6858_CTRL_PKT_SIZE,
  196. 100);
  197. if (result != OTI6858_CTRL_PKT_SIZE) {
  198. dev_err(&port->dev, "%s(): error reading status\n", __func__);
  199. kfree(new_setup);
  200. /* we will try again */
  201. schedule_delayed_work(&priv->delayed_setup_work,
  202. msecs_to_jiffies(2));
  203. return;
  204. }
  205. spin_lock_irqsave(&priv->lock, flags);
  206. if (!OTI6858_CTRL_EQUALS_PENDING(new_setup, priv)) {
  207. new_setup->divisor = priv->pending_setup.divisor;
  208. new_setup->control = priv->pending_setup.control;
  209. new_setup->frame_fmt = priv->pending_setup.frame_fmt;
  210. spin_unlock_irqrestore(&priv->lock, flags);
  211. result = usb_control_msg(port->serial->dev,
  212. usb_sndctrlpipe(port->serial->dev, 0),
  213. OTI6858_REQ_T_SET_LINE,
  214. OTI6858_REQ_SET_LINE,
  215. 0, 0,
  216. new_setup, OTI6858_CTRL_PKT_SIZE,
  217. 100);
  218. } else {
  219. spin_unlock_irqrestore(&priv->lock, flags);
  220. result = 0;
  221. }
  222. kfree(new_setup);
  223. spin_lock_irqsave(&priv->lock, flags);
  224. if (result != OTI6858_CTRL_PKT_SIZE)
  225. priv->transient = 0;
  226. priv->setup_done = 1;
  227. spin_unlock_irqrestore(&priv->lock, flags);
  228. dev_dbg(&port->dev, "%s(): submitting interrupt urb\n", __func__);
  229. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  230. if (result != 0) {
  231. dev_err(&port->dev, "%s(): usb_submit_urb() failed with error %d\n",
  232. __func__, result);
  233. }
  234. }
  235. static void send_data(struct work_struct *work)
  236. {
  237. struct oti6858_private *priv = container_of(work,
  238. struct oti6858_private, delayed_write_work.work);
  239. struct usb_serial_port *port = priv->port;
  240. int count = 0, result;
  241. unsigned long flags;
  242. u8 *allow;
  243. spin_lock_irqsave(&priv->lock, flags);
  244. if (priv->flags.write_urb_in_use) {
  245. spin_unlock_irqrestore(&priv->lock, flags);
  246. schedule_delayed_work(&priv->delayed_write_work,
  247. msecs_to_jiffies(2));
  248. return;
  249. }
  250. priv->flags.write_urb_in_use = 1;
  251. spin_unlock_irqrestore(&priv->lock, flags);
  252. spin_lock_irqsave(&port->lock, flags);
  253. count = kfifo_len(&port->write_fifo);
  254. spin_unlock_irqrestore(&port->lock, flags);
  255. if (count > port->bulk_out_size)
  256. count = port->bulk_out_size;
  257. if (count != 0) {
  258. allow = kmalloc(1, GFP_KERNEL);
  259. if (!allow)
  260. return;
  261. result = usb_control_msg(port->serial->dev,
  262. usb_rcvctrlpipe(port->serial->dev, 0),
  263. OTI6858_REQ_T_CHECK_TXBUFF,
  264. OTI6858_REQ_CHECK_TXBUFF,
  265. count, 0, allow, 1, 100);
  266. if (result != 1 || *allow != 0)
  267. count = 0;
  268. kfree(allow);
  269. }
  270. if (count == 0) {
  271. priv->flags.write_urb_in_use = 0;
  272. dev_dbg(&port->dev, "%s(): submitting interrupt urb\n", __func__);
  273. result = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO);
  274. if (result != 0) {
  275. dev_err(&port->dev, "%s(): usb_submit_urb() failed with error %d\n",
  276. __func__, result);
  277. }
  278. return;
  279. }
  280. count = kfifo_out_locked(&port->write_fifo,
  281. port->write_urb->transfer_buffer,
  282. count, &port->lock);
  283. port->write_urb->transfer_buffer_length = count;
  284. result = usb_submit_urb(port->write_urb, GFP_NOIO);
  285. if (result != 0) {
  286. dev_err_console(port, "%s(): usb_submit_urb() failed with error %d\n",
  287. __func__, result);
  288. priv->flags.write_urb_in_use = 0;
  289. }
  290. usb_serial_port_softint(port);
  291. }
  292. static int oti6858_port_probe(struct usb_serial_port *port)
  293. {
  294. struct oti6858_private *priv;
  295. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  296. if (!priv)
  297. return -ENOMEM;
  298. spin_lock_init(&priv->lock);
  299. priv->port = port;
  300. INIT_DELAYED_WORK(&priv->delayed_setup_work, setup_line);
  301. INIT_DELAYED_WORK(&priv->delayed_write_work, send_data);
  302. usb_set_serial_port_data(port, priv);
  303. port->port.drain_delay = 256; /* FIXME: check the FIFO length */
  304. return 0;
  305. }
  306. static void oti6858_port_remove(struct usb_serial_port *port)
  307. {
  308. struct oti6858_private *priv;
  309. priv = usb_get_serial_port_data(port);
  310. kfree(priv);
  311. }
  312. static int oti6858_write(struct tty_struct *tty, struct usb_serial_port *port,
  313. const unsigned char *buf, int count)
  314. {
  315. if (!count)
  316. return count;
  317. count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock);
  318. return count;
  319. }
  320. static unsigned int oti6858_write_room(struct tty_struct *tty)
  321. {
  322. struct usb_serial_port *port = tty->driver_data;
  323. unsigned int room;
  324. unsigned long flags;
  325. spin_lock_irqsave(&port->lock, flags);
  326. room = kfifo_avail(&port->write_fifo);
  327. spin_unlock_irqrestore(&port->lock, flags);
  328. return room;
  329. }
  330. static unsigned int oti6858_chars_in_buffer(struct tty_struct *tty)
  331. {
  332. struct usb_serial_port *port = tty->driver_data;
  333. unsigned int chars;
  334. unsigned long flags;
  335. spin_lock_irqsave(&port->lock, flags);
  336. chars = kfifo_len(&port->write_fifo);
  337. spin_unlock_irqrestore(&port->lock, flags);
  338. return chars;
  339. }
  340. static void oti6858_init_termios(struct tty_struct *tty)
  341. {
  342. tty_encode_baud_rate(tty, 38400, 38400);
  343. }
  344. static void oti6858_set_termios(struct tty_struct *tty,
  345. struct usb_serial_port *port,
  346. const struct ktermios *old_termios)
  347. {
  348. struct oti6858_private *priv = usb_get_serial_port_data(port);
  349. unsigned long flags;
  350. unsigned int cflag;
  351. u8 frame_fmt, control;
  352. __le16 divisor;
  353. int br;
  354. cflag = tty->termios.c_cflag;
  355. spin_lock_irqsave(&priv->lock, flags);
  356. divisor = priv->pending_setup.divisor;
  357. frame_fmt = priv->pending_setup.frame_fmt;
  358. control = priv->pending_setup.control;
  359. spin_unlock_irqrestore(&priv->lock, flags);
  360. frame_fmt &= ~FMT_DATA_BITS_MASK;
  361. switch (cflag & CSIZE) {
  362. case CS5:
  363. frame_fmt |= FMT_DATA_BITS_5;
  364. break;
  365. case CS6:
  366. frame_fmt |= FMT_DATA_BITS_6;
  367. break;
  368. case CS7:
  369. frame_fmt |= FMT_DATA_BITS_7;
  370. break;
  371. default:
  372. case CS8:
  373. frame_fmt |= FMT_DATA_BITS_8;
  374. break;
  375. }
  376. /* manufacturer claims that this device can work with baud rates
  377. * up to 3 Mbps; I've tested it only on 115200 bps, so I can't
  378. * guarantee that any other baud rate will work (especially
  379. * the higher ones)
  380. */
  381. br = tty_get_baud_rate(tty);
  382. if (br == 0) {
  383. divisor = 0;
  384. } else {
  385. int real_br;
  386. int new_divisor;
  387. br = min(br, OTI6858_MAX_BAUD_RATE);
  388. new_divisor = (96000000 + 8 * br) / (16 * br);
  389. real_br = 96000000 / (16 * new_divisor);
  390. divisor = cpu_to_le16(new_divisor);
  391. tty_encode_baud_rate(tty, real_br, real_br);
  392. }
  393. frame_fmt &= ~FMT_STOP_BITS_MASK;
  394. if ((cflag & CSTOPB) != 0)
  395. frame_fmt |= FMT_STOP_BITS_2;
  396. else
  397. frame_fmt |= FMT_STOP_BITS_1;
  398. frame_fmt &= ~FMT_PARITY_MASK;
  399. if ((cflag & PARENB) != 0) {
  400. if ((cflag & PARODD) != 0)
  401. frame_fmt |= FMT_PARITY_ODD;
  402. else
  403. frame_fmt |= FMT_PARITY_EVEN;
  404. } else {
  405. frame_fmt |= FMT_PARITY_NONE;
  406. }
  407. control &= ~CONTROL_MASK;
  408. if ((cflag & CRTSCTS) != 0)
  409. control |= (CONTROL_DTR_HIGH | CONTROL_RTS_HIGH);
  410. /* change control lines if we are switching to or from B0 */
  411. /* FIXME:
  412. spin_lock_irqsave(&priv->lock, flags);
  413. control = priv->line_control;
  414. if ((cflag & CBAUD) == B0)
  415. priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
  416. else
  417. priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
  418. if (control != priv->line_control) {
  419. control = priv->line_control;
  420. spin_unlock_irqrestore(&priv->lock, flags);
  421. set_control_lines(serial->dev, control);
  422. } else {
  423. spin_unlock_irqrestore(&priv->lock, flags);
  424. }
  425. */
  426. spin_lock_irqsave(&priv->lock, flags);
  427. if (divisor != priv->pending_setup.divisor
  428. || control != priv->pending_setup.control
  429. || frame_fmt != priv->pending_setup.frame_fmt) {
  430. priv->pending_setup.divisor = divisor;
  431. priv->pending_setup.control = control;
  432. priv->pending_setup.frame_fmt = frame_fmt;
  433. }
  434. spin_unlock_irqrestore(&priv->lock, flags);
  435. }
  436. static int oti6858_open(struct tty_struct *tty, struct usb_serial_port *port)
  437. {
  438. struct oti6858_private *priv = usb_get_serial_port_data(port);
  439. struct usb_serial *serial = port->serial;
  440. struct oti6858_control_pkt *buf;
  441. unsigned long flags;
  442. int result;
  443. usb_clear_halt(serial->dev, port->write_urb->pipe);
  444. usb_clear_halt(serial->dev, port->read_urb->pipe);
  445. buf = kmalloc(OTI6858_CTRL_PKT_SIZE, GFP_KERNEL);
  446. if (!buf)
  447. return -ENOMEM;
  448. result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  449. OTI6858_REQ_T_GET_STATUS,
  450. OTI6858_REQ_GET_STATUS,
  451. 0, 0,
  452. buf, OTI6858_CTRL_PKT_SIZE,
  453. 100);
  454. if (result != OTI6858_CTRL_PKT_SIZE) {
  455. /* assume default (after power-on reset) values */
  456. buf->divisor = cpu_to_le16(0x009c); /* 38400 bps */
  457. buf->frame_fmt = 0x03; /* 8N1 */
  458. buf->something = 0x43;
  459. buf->control = 0x4c; /* DTR, RTS */
  460. buf->tx_status = 0x00;
  461. buf->pin_state = 0x5b; /* RTS, CTS, DSR, DTR, RI, DCD */
  462. buf->rx_bytes_avail = 0x00;
  463. }
  464. spin_lock_irqsave(&priv->lock, flags);
  465. memcpy(&priv->status, buf, OTI6858_CTRL_PKT_SIZE);
  466. priv->pending_setup.divisor = buf->divisor;
  467. priv->pending_setup.frame_fmt = buf->frame_fmt;
  468. priv->pending_setup.control = buf->control;
  469. spin_unlock_irqrestore(&priv->lock, flags);
  470. kfree(buf);
  471. dev_dbg(&port->dev, "%s(): submitting interrupt urb\n", __func__);
  472. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  473. if (result != 0) {
  474. dev_err(&port->dev, "%s(): usb_submit_urb() failed with error %d\n",
  475. __func__, result);
  476. oti6858_close(port);
  477. return result;
  478. }
  479. /* setup termios */
  480. if (tty)
  481. oti6858_set_termios(tty, port, NULL);
  482. return 0;
  483. }
  484. static void oti6858_close(struct usb_serial_port *port)
  485. {
  486. struct oti6858_private *priv = usb_get_serial_port_data(port);
  487. unsigned long flags;
  488. spin_lock_irqsave(&port->lock, flags);
  489. /* clear out any remaining data in the buffer */
  490. kfifo_reset_out(&port->write_fifo);
  491. spin_unlock_irqrestore(&port->lock, flags);
  492. dev_dbg(&port->dev, "%s(): after buf_clear()\n", __func__);
  493. /* cancel scheduled setup */
  494. cancel_delayed_work_sync(&priv->delayed_setup_work);
  495. cancel_delayed_work_sync(&priv->delayed_write_work);
  496. /* shutdown our urbs */
  497. dev_dbg(&port->dev, "%s(): shutting down urbs\n", __func__);
  498. usb_kill_urb(port->write_urb);
  499. usb_kill_urb(port->read_urb);
  500. usb_kill_urb(port->interrupt_in_urb);
  501. }
  502. static int oti6858_tiocmset(struct tty_struct *tty,
  503. unsigned int set, unsigned int clear)
  504. {
  505. struct usb_serial_port *port = tty->driver_data;
  506. struct oti6858_private *priv = usb_get_serial_port_data(port);
  507. unsigned long flags;
  508. u8 control;
  509. dev_dbg(&port->dev, "%s(set = 0x%08x, clear = 0x%08x)\n",
  510. __func__, set, clear);
  511. /* FIXME: check if this is correct (active high/low) */
  512. spin_lock_irqsave(&priv->lock, flags);
  513. control = priv->pending_setup.control;
  514. if ((set & TIOCM_RTS) != 0)
  515. control |= CONTROL_RTS_HIGH;
  516. if ((set & TIOCM_DTR) != 0)
  517. control |= CONTROL_DTR_HIGH;
  518. if ((clear & TIOCM_RTS) != 0)
  519. control &= ~CONTROL_RTS_HIGH;
  520. if ((clear & TIOCM_DTR) != 0)
  521. control &= ~CONTROL_DTR_HIGH;
  522. if (control != priv->pending_setup.control)
  523. priv->pending_setup.control = control;
  524. spin_unlock_irqrestore(&priv->lock, flags);
  525. return 0;
  526. }
  527. static int oti6858_tiocmget(struct tty_struct *tty)
  528. {
  529. struct usb_serial_port *port = tty->driver_data;
  530. struct oti6858_private *priv = usb_get_serial_port_data(port);
  531. unsigned long flags;
  532. unsigned pin_state;
  533. unsigned result = 0;
  534. spin_lock_irqsave(&priv->lock, flags);
  535. pin_state = priv->status.pin_state & PIN_MASK;
  536. spin_unlock_irqrestore(&priv->lock, flags);
  537. /* FIXME: check if this is correct (active high/low) */
  538. if ((pin_state & PIN_RTS) != 0)
  539. result |= TIOCM_RTS;
  540. if ((pin_state & PIN_CTS) != 0)
  541. result |= TIOCM_CTS;
  542. if ((pin_state & PIN_DSR) != 0)
  543. result |= TIOCM_DSR;
  544. if ((pin_state & PIN_DTR) != 0)
  545. result |= TIOCM_DTR;
  546. if ((pin_state & PIN_RI) != 0)
  547. result |= TIOCM_RI;
  548. if ((pin_state & PIN_DCD) != 0)
  549. result |= TIOCM_CD;
  550. dev_dbg(&port->dev, "%s() = 0x%08x\n", __func__, result);
  551. return result;
  552. }
  553. static void oti6858_read_int_callback(struct urb *urb)
  554. {
  555. struct usb_serial_port *port = urb->context;
  556. struct oti6858_private *priv = usb_get_serial_port_data(port);
  557. int transient = 0, can_recv = 0, resubmit = 1;
  558. int status = urb->status;
  559. switch (status) {
  560. case 0:
  561. /* success */
  562. break;
  563. case -ECONNRESET:
  564. case -ENOENT:
  565. case -ESHUTDOWN:
  566. /* this urb is terminated, clean up */
  567. dev_dbg(&urb->dev->dev, "%s(): urb shutting down with status: %d\n",
  568. __func__, status);
  569. return;
  570. default:
  571. dev_dbg(&urb->dev->dev, "%s(): nonzero urb status received: %d\n",
  572. __func__, status);
  573. break;
  574. }
  575. if (status == 0 && urb->actual_length == OTI6858_CTRL_PKT_SIZE) {
  576. struct oti6858_control_pkt *xs = urb->transfer_buffer;
  577. unsigned long flags;
  578. spin_lock_irqsave(&priv->lock, flags);
  579. if (!priv->transient) {
  580. if (!OTI6858_CTRL_EQUALS_PENDING(xs, priv)) {
  581. if (xs->rx_bytes_avail == 0) {
  582. priv->transient = 4;
  583. priv->setup_done = 0;
  584. resubmit = 0;
  585. dev_dbg(&port->dev, "%s(): scheduling setup_line()\n", __func__);
  586. schedule_delayed_work(&priv->delayed_setup_work, 0);
  587. }
  588. }
  589. } else {
  590. if (OTI6858_CTRL_EQUALS_PENDING(xs, priv)) {
  591. priv->transient = 0;
  592. } else if (!priv->setup_done) {
  593. resubmit = 0;
  594. } else if (--priv->transient == 0) {
  595. if (xs->rx_bytes_avail == 0) {
  596. priv->transient = 4;
  597. priv->setup_done = 0;
  598. resubmit = 0;
  599. dev_dbg(&port->dev, "%s(): scheduling setup_line()\n", __func__);
  600. schedule_delayed_work(&priv->delayed_setup_work, 0);
  601. }
  602. }
  603. }
  604. if (!priv->transient) {
  605. u8 delta = xs->pin_state ^ priv->status.pin_state;
  606. if (delta & PIN_MSR_MASK) {
  607. if (delta & PIN_CTS)
  608. port->icount.cts++;
  609. if (delta & PIN_DSR)
  610. port->icount.dsr++;
  611. if (delta & PIN_RI)
  612. port->icount.rng++;
  613. if (delta & PIN_DCD)
  614. port->icount.dcd++;
  615. wake_up_interruptible(&port->port.delta_msr_wait);
  616. }
  617. memcpy(&priv->status, xs, OTI6858_CTRL_PKT_SIZE);
  618. }
  619. if (!priv->transient && xs->rx_bytes_avail != 0) {
  620. can_recv = xs->rx_bytes_avail;
  621. priv->flags.read_urb_in_use = 1;
  622. }
  623. transient = priv->transient;
  624. spin_unlock_irqrestore(&priv->lock, flags);
  625. }
  626. if (can_recv) {
  627. int result;
  628. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  629. if (result != 0) {
  630. priv->flags.read_urb_in_use = 0;
  631. dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
  632. " error %d\n", __func__, result);
  633. } else {
  634. resubmit = 0;
  635. }
  636. } else if (!transient) {
  637. unsigned long flags;
  638. int count;
  639. spin_lock_irqsave(&port->lock, flags);
  640. count = kfifo_len(&port->write_fifo);
  641. spin_unlock_irqrestore(&port->lock, flags);
  642. spin_lock_irqsave(&priv->lock, flags);
  643. if (priv->flags.write_urb_in_use == 0 && count != 0) {
  644. schedule_delayed_work(&priv->delayed_write_work, 0);
  645. resubmit = 0;
  646. }
  647. spin_unlock_irqrestore(&priv->lock, flags);
  648. }
  649. if (resubmit) {
  650. int result;
  651. /* dev_dbg(&urb->dev->dev, "%s(): submitting interrupt urb\n", __func__); */
  652. result = usb_submit_urb(urb, GFP_ATOMIC);
  653. if (result != 0) {
  654. dev_err(&urb->dev->dev,
  655. "%s(): usb_submit_urb() failed with"
  656. " error %d\n", __func__, result);
  657. }
  658. }
  659. }
  660. static void oti6858_read_bulk_callback(struct urb *urb)
  661. {
  662. struct usb_serial_port *port = urb->context;
  663. struct oti6858_private *priv = usb_get_serial_port_data(port);
  664. unsigned char *data = urb->transfer_buffer;
  665. unsigned long flags;
  666. int status = urb->status;
  667. int result;
  668. spin_lock_irqsave(&priv->lock, flags);
  669. priv->flags.read_urb_in_use = 0;
  670. spin_unlock_irqrestore(&priv->lock, flags);
  671. if (status != 0) {
  672. dev_dbg(&urb->dev->dev, "%s(): unable to handle the error, exiting\n", __func__);
  673. return;
  674. }
  675. if (urb->actual_length > 0) {
  676. tty_insert_flip_string(&port->port, data, urb->actual_length);
  677. tty_flip_buffer_push(&port->port);
  678. }
  679. /* schedule the interrupt urb */
  680. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  681. if (result != 0 && result != -EPERM) {
  682. dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
  683. " error %d\n", __func__, result);
  684. }
  685. }
  686. static void oti6858_write_bulk_callback(struct urb *urb)
  687. {
  688. struct usb_serial_port *port = urb->context;
  689. struct oti6858_private *priv = usb_get_serial_port_data(port);
  690. int status = urb->status;
  691. int result;
  692. switch (status) {
  693. case 0:
  694. /* success */
  695. break;
  696. case -ECONNRESET:
  697. case -ENOENT:
  698. case -ESHUTDOWN:
  699. /* this urb is terminated, clean up */
  700. dev_dbg(&urb->dev->dev, "%s(): urb shutting down with status: %d\n", __func__, status);
  701. priv->flags.write_urb_in_use = 0;
  702. return;
  703. default:
  704. /* error in the urb, so we have to resubmit it */
  705. dev_dbg(&urb->dev->dev, "%s(): nonzero write bulk status received: %d\n", __func__, status);
  706. dev_dbg(&urb->dev->dev, "%s(): overflow in write\n", __func__);
  707. port->write_urb->transfer_buffer_length = 1;
  708. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  709. if (result) {
  710. dev_err_console(port, "%s(): usb_submit_urb() failed,"
  711. " error %d\n", __func__, result);
  712. } else {
  713. return;
  714. }
  715. }
  716. priv->flags.write_urb_in_use = 0;
  717. /* schedule the interrupt urb if we are still open */
  718. dev_dbg(&port->dev, "%s(): submitting interrupt urb\n", __func__);
  719. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  720. if (result != 0) {
  721. dev_err(&port->dev, "%s(): failed submitting int urb,"
  722. " error %d\n", __func__, result);
  723. }
  724. }
  725. module_usb_serial_driver(serial_drivers, id_table);
  726. MODULE_DESCRIPTION(OTI6858_DESCRIPTION);
  727. MODULE_AUTHOR(OTI6858_AUTHOR);
  728. MODULE_LICENSE("GPL v2");