oti6858.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  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 (greg@kroah.com)
  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. .name = "oti6858",
  131. },
  132. .id_table = id_table,
  133. .num_ports = 1,
  134. .num_bulk_in = 1,
  135. .num_bulk_out = 1,
  136. .num_interrupt_in = 1,
  137. .open = oti6858_open,
  138. .close = oti6858_close,
  139. .write = oti6858_write,
  140. .set_termios = oti6858_set_termios,
  141. .init_termios = oti6858_init_termios,
  142. .tiocmget = oti6858_tiocmget,
  143. .tiocmset = oti6858_tiocmset,
  144. .tiocmiwait = usb_serial_generic_tiocmiwait,
  145. .read_bulk_callback = oti6858_read_bulk_callback,
  146. .read_int_callback = oti6858_read_int_callback,
  147. .write_bulk_callback = oti6858_write_bulk_callback,
  148. .write_room = oti6858_write_room,
  149. .chars_in_buffer = oti6858_chars_in_buffer,
  150. .port_probe = oti6858_port_probe,
  151. .port_remove = oti6858_port_remove,
  152. };
  153. static struct usb_serial_driver * const serial_drivers[] = {
  154. &oti6858_device, NULL
  155. };
  156. struct oti6858_private {
  157. spinlock_t lock;
  158. struct oti6858_control_pkt status;
  159. struct {
  160. u8 read_urb_in_use;
  161. u8 write_urb_in_use;
  162. } flags;
  163. struct delayed_work delayed_write_work;
  164. struct {
  165. __le16 divisor;
  166. u8 frame_fmt;
  167. u8 control;
  168. } pending_setup;
  169. u8 transient;
  170. u8 setup_done;
  171. struct delayed_work delayed_setup_work;
  172. struct usb_serial_port *port; /* USB port with which associated */
  173. };
  174. static void setup_line(struct work_struct *work)
  175. {
  176. struct oti6858_private *priv = container_of(work,
  177. struct oti6858_private, delayed_setup_work.work);
  178. struct usb_serial_port *port = priv->port;
  179. struct oti6858_control_pkt *new_setup;
  180. unsigned long flags;
  181. int result;
  182. new_setup = kmalloc(OTI6858_CTRL_PKT_SIZE, GFP_KERNEL);
  183. if (!new_setup) {
  184. /* we will try again */
  185. schedule_delayed_work(&priv->delayed_setup_work,
  186. msecs_to_jiffies(2));
  187. return;
  188. }
  189. result = usb_control_msg(port->serial->dev,
  190. usb_rcvctrlpipe(port->serial->dev, 0),
  191. OTI6858_REQ_T_GET_STATUS,
  192. OTI6858_REQ_GET_STATUS,
  193. 0, 0,
  194. new_setup, OTI6858_CTRL_PKT_SIZE,
  195. 100);
  196. if (result != OTI6858_CTRL_PKT_SIZE) {
  197. dev_err(&port->dev, "%s(): error reading status\n", __func__);
  198. kfree(new_setup);
  199. /* we will try again */
  200. schedule_delayed_work(&priv->delayed_setup_work,
  201. msecs_to_jiffies(2));
  202. return;
  203. }
  204. spin_lock_irqsave(&priv->lock, flags);
  205. if (!OTI6858_CTRL_EQUALS_PENDING(new_setup, priv)) {
  206. new_setup->divisor = priv->pending_setup.divisor;
  207. new_setup->control = priv->pending_setup.control;
  208. new_setup->frame_fmt = priv->pending_setup.frame_fmt;
  209. spin_unlock_irqrestore(&priv->lock, flags);
  210. result = usb_control_msg(port->serial->dev,
  211. usb_sndctrlpipe(port->serial->dev, 0),
  212. OTI6858_REQ_T_SET_LINE,
  213. OTI6858_REQ_SET_LINE,
  214. 0, 0,
  215. new_setup, OTI6858_CTRL_PKT_SIZE,
  216. 100);
  217. } else {
  218. spin_unlock_irqrestore(&priv->lock, flags);
  219. result = 0;
  220. }
  221. kfree(new_setup);
  222. spin_lock_irqsave(&priv->lock, flags);
  223. if (result != OTI6858_CTRL_PKT_SIZE)
  224. priv->transient = 0;
  225. priv->setup_done = 1;
  226. spin_unlock_irqrestore(&priv->lock, flags);
  227. dev_dbg(&port->dev, "%s(): submitting interrupt urb\n", __func__);
  228. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  229. if (result != 0) {
  230. dev_err(&port->dev, "%s(): usb_submit_urb() failed with error %d\n",
  231. __func__, result);
  232. }
  233. }
  234. static void send_data(struct work_struct *work)
  235. {
  236. struct oti6858_private *priv = container_of(work,
  237. struct oti6858_private, delayed_write_work.work);
  238. struct usb_serial_port *port = priv->port;
  239. int count = 0, result;
  240. unsigned long flags;
  241. u8 *allow;
  242. spin_lock_irqsave(&priv->lock, flags);
  243. if (priv->flags.write_urb_in_use) {
  244. spin_unlock_irqrestore(&priv->lock, flags);
  245. schedule_delayed_work(&priv->delayed_write_work,
  246. msecs_to_jiffies(2));
  247. return;
  248. }
  249. priv->flags.write_urb_in_use = 1;
  250. spin_unlock_irqrestore(&priv->lock, flags);
  251. spin_lock_irqsave(&port->lock, flags);
  252. count = kfifo_len(&port->write_fifo);
  253. spin_unlock_irqrestore(&port->lock, flags);
  254. if (count > port->bulk_out_size)
  255. count = port->bulk_out_size;
  256. if (count != 0) {
  257. allow = kmalloc(1, GFP_KERNEL);
  258. if (!allow)
  259. return;
  260. result = usb_control_msg(port->serial->dev,
  261. usb_rcvctrlpipe(port->serial->dev, 0),
  262. OTI6858_REQ_T_CHECK_TXBUFF,
  263. OTI6858_REQ_CHECK_TXBUFF,
  264. count, 0, allow, 1, 100);
  265. if (result != 1 || *allow != 0)
  266. count = 0;
  267. kfree(allow);
  268. }
  269. if (count == 0) {
  270. priv->flags.write_urb_in_use = 0;
  271. dev_dbg(&port->dev, "%s(): submitting interrupt urb\n", __func__);
  272. result = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO);
  273. if (result != 0) {
  274. dev_err(&port->dev, "%s(): usb_submit_urb() failed with error %d\n",
  275. __func__, result);
  276. }
  277. return;
  278. }
  279. count = kfifo_out_locked(&port->write_fifo,
  280. port->write_urb->transfer_buffer,
  281. count, &port->lock);
  282. port->write_urb->transfer_buffer_length = count;
  283. result = usb_submit_urb(port->write_urb, GFP_NOIO);
  284. if (result != 0) {
  285. dev_err_console(port, "%s(): usb_submit_urb() failed with error %d\n",
  286. __func__, result);
  287. priv->flags.write_urb_in_use = 0;
  288. }
  289. usb_serial_port_softint(port);
  290. }
  291. static int oti6858_port_probe(struct usb_serial_port *port)
  292. {
  293. struct oti6858_private *priv;
  294. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  295. if (!priv)
  296. return -ENOMEM;
  297. spin_lock_init(&priv->lock);
  298. priv->port = port;
  299. INIT_DELAYED_WORK(&priv->delayed_setup_work, setup_line);
  300. INIT_DELAYED_WORK(&priv->delayed_write_work, send_data);
  301. usb_set_serial_port_data(port, priv);
  302. port->port.drain_delay = 256; /* FIXME: check the FIFO length */
  303. return 0;
  304. }
  305. static void oti6858_port_remove(struct usb_serial_port *port)
  306. {
  307. struct oti6858_private *priv;
  308. priv = usb_get_serial_port_data(port);
  309. kfree(priv);
  310. }
  311. static int oti6858_write(struct tty_struct *tty, struct usb_serial_port *port,
  312. const unsigned char *buf, int count)
  313. {
  314. if (!count)
  315. return count;
  316. count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock);
  317. return count;
  318. }
  319. static unsigned int oti6858_write_room(struct tty_struct *tty)
  320. {
  321. struct usb_serial_port *port = tty->driver_data;
  322. unsigned int room;
  323. unsigned long flags;
  324. spin_lock_irqsave(&port->lock, flags);
  325. room = kfifo_avail(&port->write_fifo);
  326. spin_unlock_irqrestore(&port->lock, flags);
  327. return room;
  328. }
  329. static unsigned int oti6858_chars_in_buffer(struct tty_struct *tty)
  330. {
  331. struct usb_serial_port *port = tty->driver_data;
  332. unsigned int chars;
  333. unsigned long flags;
  334. spin_lock_irqsave(&port->lock, flags);
  335. chars = kfifo_len(&port->write_fifo);
  336. spin_unlock_irqrestore(&port->lock, flags);
  337. return chars;
  338. }
  339. static void oti6858_init_termios(struct tty_struct *tty)
  340. {
  341. tty_encode_baud_rate(tty, 38400, 38400);
  342. }
  343. static void oti6858_set_termios(struct tty_struct *tty,
  344. struct usb_serial_port *port,
  345. const struct ktermios *old_termios)
  346. {
  347. struct oti6858_private *priv = usb_get_serial_port_data(port);
  348. unsigned long flags;
  349. unsigned int cflag;
  350. u8 frame_fmt, control;
  351. __le16 divisor;
  352. int br;
  353. cflag = tty->termios.c_cflag;
  354. spin_lock_irqsave(&priv->lock, flags);
  355. frame_fmt = priv->pending_setup.frame_fmt;
  356. control = priv->pending_setup.control;
  357. spin_unlock_irqrestore(&priv->lock, flags);
  358. frame_fmt &= ~FMT_DATA_BITS_MASK;
  359. switch (cflag & CSIZE) {
  360. case CS5:
  361. frame_fmt |= FMT_DATA_BITS_5;
  362. break;
  363. case CS6:
  364. frame_fmt |= FMT_DATA_BITS_6;
  365. break;
  366. case CS7:
  367. frame_fmt |= FMT_DATA_BITS_7;
  368. break;
  369. default:
  370. case CS8:
  371. frame_fmt |= FMT_DATA_BITS_8;
  372. break;
  373. }
  374. /* manufacturer claims that this device can work with baud rates
  375. * up to 3 Mbps; I've tested it only on 115200 bps, so I can't
  376. * guarantee that any other baud rate will work (especially
  377. * the higher ones)
  378. */
  379. br = tty_get_baud_rate(tty);
  380. if (br == 0) {
  381. divisor = 0;
  382. } else {
  383. int real_br;
  384. int new_divisor;
  385. br = min(br, OTI6858_MAX_BAUD_RATE);
  386. new_divisor = (96000000 + 8 * br) / (16 * br);
  387. real_br = 96000000 / (16 * new_divisor);
  388. divisor = cpu_to_le16(new_divisor);
  389. tty_encode_baud_rate(tty, real_br, real_br);
  390. }
  391. frame_fmt &= ~FMT_STOP_BITS_MASK;
  392. if ((cflag & CSTOPB) != 0)
  393. frame_fmt |= FMT_STOP_BITS_2;
  394. else
  395. frame_fmt |= FMT_STOP_BITS_1;
  396. frame_fmt &= ~FMT_PARITY_MASK;
  397. if ((cflag & PARENB) != 0) {
  398. if ((cflag & PARODD) != 0)
  399. frame_fmt |= FMT_PARITY_ODD;
  400. else
  401. frame_fmt |= FMT_PARITY_EVEN;
  402. } else {
  403. frame_fmt |= FMT_PARITY_NONE;
  404. }
  405. control &= ~CONTROL_MASK;
  406. if ((cflag & CRTSCTS) != 0)
  407. control |= (CONTROL_DTR_HIGH | CONTROL_RTS_HIGH);
  408. /* change control lines if we are switching to or from B0 */
  409. /* FIXME:
  410. spin_lock_irqsave(&priv->lock, flags);
  411. control = priv->line_control;
  412. if ((cflag & CBAUD) == B0)
  413. priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
  414. else
  415. priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
  416. if (control != priv->line_control) {
  417. control = priv->line_control;
  418. spin_unlock_irqrestore(&priv->lock, flags);
  419. set_control_lines(serial->dev, control);
  420. } else {
  421. spin_unlock_irqrestore(&priv->lock, flags);
  422. }
  423. */
  424. spin_lock_irqsave(&priv->lock, flags);
  425. if (divisor != priv->pending_setup.divisor
  426. || control != priv->pending_setup.control
  427. || frame_fmt != priv->pending_setup.frame_fmt) {
  428. priv->pending_setup.divisor = divisor;
  429. priv->pending_setup.control = control;
  430. priv->pending_setup.frame_fmt = frame_fmt;
  431. }
  432. spin_unlock_irqrestore(&priv->lock, flags);
  433. }
  434. static int oti6858_open(struct tty_struct *tty, struct usb_serial_port *port)
  435. {
  436. struct oti6858_private *priv = usb_get_serial_port_data(port);
  437. struct usb_serial *serial = port->serial;
  438. struct oti6858_control_pkt *buf;
  439. unsigned long flags;
  440. int result;
  441. usb_clear_halt(serial->dev, port->write_urb->pipe);
  442. usb_clear_halt(serial->dev, port->read_urb->pipe);
  443. buf = kmalloc(OTI6858_CTRL_PKT_SIZE, GFP_KERNEL);
  444. if (!buf)
  445. return -ENOMEM;
  446. result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  447. OTI6858_REQ_T_GET_STATUS,
  448. OTI6858_REQ_GET_STATUS,
  449. 0, 0,
  450. buf, OTI6858_CTRL_PKT_SIZE,
  451. 100);
  452. if (result != OTI6858_CTRL_PKT_SIZE) {
  453. /* assume default (after power-on reset) values */
  454. buf->divisor = cpu_to_le16(0x009c); /* 38400 bps */
  455. buf->frame_fmt = 0x03; /* 8N1 */
  456. buf->something = 0x43;
  457. buf->control = 0x4c; /* DTR, RTS */
  458. buf->tx_status = 0x00;
  459. buf->pin_state = 0x5b; /* RTS, CTS, DSR, DTR, RI, DCD */
  460. buf->rx_bytes_avail = 0x00;
  461. }
  462. spin_lock_irqsave(&priv->lock, flags);
  463. memcpy(&priv->status, buf, OTI6858_CTRL_PKT_SIZE);
  464. priv->pending_setup.divisor = buf->divisor;
  465. priv->pending_setup.frame_fmt = buf->frame_fmt;
  466. priv->pending_setup.control = buf->control;
  467. spin_unlock_irqrestore(&priv->lock, flags);
  468. kfree(buf);
  469. dev_dbg(&port->dev, "%s(): submitting interrupt urb\n", __func__);
  470. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  471. if (result != 0) {
  472. dev_err(&port->dev, "%s(): usb_submit_urb() failed with error %d\n",
  473. __func__, result);
  474. oti6858_close(port);
  475. return result;
  476. }
  477. /* setup termios */
  478. if (tty)
  479. oti6858_set_termios(tty, port, NULL);
  480. return 0;
  481. }
  482. static void oti6858_close(struct usb_serial_port *port)
  483. {
  484. struct oti6858_private *priv = usb_get_serial_port_data(port);
  485. unsigned long flags;
  486. spin_lock_irqsave(&port->lock, flags);
  487. /* clear out any remaining data in the buffer */
  488. kfifo_reset_out(&port->write_fifo);
  489. spin_unlock_irqrestore(&port->lock, flags);
  490. dev_dbg(&port->dev, "%s(): after buf_clear()\n", __func__);
  491. /* cancel scheduled setup */
  492. cancel_delayed_work_sync(&priv->delayed_setup_work);
  493. cancel_delayed_work_sync(&priv->delayed_write_work);
  494. /* shutdown our urbs */
  495. dev_dbg(&port->dev, "%s(): shutting down urbs\n", __func__);
  496. usb_kill_urb(port->write_urb);
  497. usb_kill_urb(port->read_urb);
  498. usb_kill_urb(port->interrupt_in_urb);
  499. }
  500. static int oti6858_tiocmset(struct tty_struct *tty,
  501. unsigned int set, unsigned int clear)
  502. {
  503. struct usb_serial_port *port = tty->driver_data;
  504. struct oti6858_private *priv = usb_get_serial_port_data(port);
  505. unsigned long flags;
  506. u8 control;
  507. dev_dbg(&port->dev, "%s(set = 0x%08x, clear = 0x%08x)\n",
  508. __func__, set, clear);
  509. /* FIXME: check if this is correct (active high/low) */
  510. spin_lock_irqsave(&priv->lock, flags);
  511. control = priv->pending_setup.control;
  512. if ((set & TIOCM_RTS) != 0)
  513. control |= CONTROL_RTS_HIGH;
  514. if ((set & TIOCM_DTR) != 0)
  515. control |= CONTROL_DTR_HIGH;
  516. if ((clear & TIOCM_RTS) != 0)
  517. control &= ~CONTROL_RTS_HIGH;
  518. if ((clear & TIOCM_DTR) != 0)
  519. control &= ~CONTROL_DTR_HIGH;
  520. if (control != priv->pending_setup.control)
  521. priv->pending_setup.control = control;
  522. spin_unlock_irqrestore(&priv->lock, flags);
  523. return 0;
  524. }
  525. static int oti6858_tiocmget(struct tty_struct *tty)
  526. {
  527. struct usb_serial_port *port = tty->driver_data;
  528. struct oti6858_private *priv = usb_get_serial_port_data(port);
  529. unsigned long flags;
  530. unsigned pin_state;
  531. unsigned result = 0;
  532. spin_lock_irqsave(&priv->lock, flags);
  533. pin_state = priv->status.pin_state & PIN_MASK;
  534. spin_unlock_irqrestore(&priv->lock, flags);
  535. /* FIXME: check if this is correct (active high/low) */
  536. if ((pin_state & PIN_RTS) != 0)
  537. result |= TIOCM_RTS;
  538. if ((pin_state & PIN_CTS) != 0)
  539. result |= TIOCM_CTS;
  540. if ((pin_state & PIN_DSR) != 0)
  541. result |= TIOCM_DSR;
  542. if ((pin_state & PIN_DTR) != 0)
  543. result |= TIOCM_DTR;
  544. if ((pin_state & PIN_RI) != 0)
  545. result |= TIOCM_RI;
  546. if ((pin_state & PIN_DCD) != 0)
  547. result |= TIOCM_CD;
  548. dev_dbg(&port->dev, "%s() = 0x%08x\n", __func__, result);
  549. return result;
  550. }
  551. static void oti6858_read_int_callback(struct urb *urb)
  552. {
  553. struct usb_serial_port *port = urb->context;
  554. struct oti6858_private *priv = usb_get_serial_port_data(port);
  555. int transient = 0, can_recv = 0, resubmit = 1;
  556. int status = urb->status;
  557. switch (status) {
  558. case 0:
  559. /* success */
  560. break;
  561. case -ECONNRESET:
  562. case -ENOENT:
  563. case -ESHUTDOWN:
  564. /* this urb is terminated, clean up */
  565. dev_dbg(&urb->dev->dev, "%s(): urb shutting down with status: %d\n",
  566. __func__, status);
  567. return;
  568. default:
  569. dev_dbg(&urb->dev->dev, "%s(): nonzero urb status received: %d\n",
  570. __func__, status);
  571. break;
  572. }
  573. if (status == 0 && urb->actual_length == OTI6858_CTRL_PKT_SIZE) {
  574. struct oti6858_control_pkt *xs = urb->transfer_buffer;
  575. unsigned long flags;
  576. spin_lock_irqsave(&priv->lock, flags);
  577. if (!priv->transient) {
  578. if (!OTI6858_CTRL_EQUALS_PENDING(xs, priv)) {
  579. if (xs->rx_bytes_avail == 0) {
  580. priv->transient = 4;
  581. priv->setup_done = 0;
  582. resubmit = 0;
  583. dev_dbg(&port->dev, "%s(): scheduling setup_line()\n", __func__);
  584. schedule_delayed_work(&priv->delayed_setup_work, 0);
  585. }
  586. }
  587. } else {
  588. if (OTI6858_CTRL_EQUALS_PENDING(xs, priv)) {
  589. priv->transient = 0;
  590. } else if (!priv->setup_done) {
  591. resubmit = 0;
  592. } else if (--priv->transient == 0) {
  593. if (xs->rx_bytes_avail == 0) {
  594. priv->transient = 4;
  595. priv->setup_done = 0;
  596. resubmit = 0;
  597. dev_dbg(&port->dev, "%s(): scheduling setup_line()\n", __func__);
  598. schedule_delayed_work(&priv->delayed_setup_work, 0);
  599. }
  600. }
  601. }
  602. if (!priv->transient) {
  603. u8 delta = xs->pin_state ^ priv->status.pin_state;
  604. if (delta & PIN_MSR_MASK) {
  605. if (delta & PIN_CTS)
  606. port->icount.cts++;
  607. if (delta & PIN_DSR)
  608. port->icount.dsr++;
  609. if (delta & PIN_RI)
  610. port->icount.rng++;
  611. if (delta & PIN_DCD)
  612. port->icount.dcd++;
  613. wake_up_interruptible(&port->port.delta_msr_wait);
  614. }
  615. memcpy(&priv->status, xs, OTI6858_CTRL_PKT_SIZE);
  616. }
  617. if (!priv->transient && xs->rx_bytes_avail != 0) {
  618. can_recv = xs->rx_bytes_avail;
  619. priv->flags.read_urb_in_use = 1;
  620. }
  621. transient = priv->transient;
  622. spin_unlock_irqrestore(&priv->lock, flags);
  623. }
  624. if (can_recv) {
  625. int result;
  626. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  627. if (result != 0) {
  628. priv->flags.read_urb_in_use = 0;
  629. dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
  630. " error %d\n", __func__, result);
  631. } else {
  632. resubmit = 0;
  633. }
  634. } else if (!transient) {
  635. unsigned long flags;
  636. int count;
  637. spin_lock_irqsave(&port->lock, flags);
  638. count = kfifo_len(&port->write_fifo);
  639. spin_unlock_irqrestore(&port->lock, flags);
  640. spin_lock_irqsave(&priv->lock, flags);
  641. if (priv->flags.write_urb_in_use == 0 && count != 0) {
  642. schedule_delayed_work(&priv->delayed_write_work, 0);
  643. resubmit = 0;
  644. }
  645. spin_unlock_irqrestore(&priv->lock, flags);
  646. }
  647. if (resubmit) {
  648. int result;
  649. /* dev_dbg(&urb->dev->dev, "%s(): submitting interrupt urb\n", __func__); */
  650. result = usb_submit_urb(urb, GFP_ATOMIC);
  651. if (result != 0) {
  652. dev_err(&urb->dev->dev,
  653. "%s(): usb_submit_urb() failed with"
  654. " error %d\n", __func__, result);
  655. }
  656. }
  657. }
  658. static void oti6858_read_bulk_callback(struct urb *urb)
  659. {
  660. struct usb_serial_port *port = urb->context;
  661. struct oti6858_private *priv = usb_get_serial_port_data(port);
  662. unsigned char *data = urb->transfer_buffer;
  663. unsigned long flags;
  664. int status = urb->status;
  665. int result;
  666. spin_lock_irqsave(&priv->lock, flags);
  667. priv->flags.read_urb_in_use = 0;
  668. spin_unlock_irqrestore(&priv->lock, flags);
  669. if (status != 0) {
  670. dev_dbg(&urb->dev->dev, "%s(): unable to handle the error, exiting\n", __func__);
  671. return;
  672. }
  673. if (urb->actual_length > 0) {
  674. tty_insert_flip_string(&port->port, data, urb->actual_length);
  675. tty_flip_buffer_push(&port->port);
  676. }
  677. /* schedule the interrupt urb */
  678. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  679. if (result != 0 && result != -EPERM) {
  680. dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
  681. " error %d\n", __func__, result);
  682. }
  683. }
  684. static void oti6858_write_bulk_callback(struct urb *urb)
  685. {
  686. struct usb_serial_port *port = urb->context;
  687. struct oti6858_private *priv = usb_get_serial_port_data(port);
  688. int status = urb->status;
  689. int result;
  690. switch (status) {
  691. case 0:
  692. /* success */
  693. break;
  694. case -ECONNRESET:
  695. case -ENOENT:
  696. case -ESHUTDOWN:
  697. /* this urb is terminated, clean up */
  698. dev_dbg(&urb->dev->dev, "%s(): urb shutting down with status: %d\n", __func__, status);
  699. priv->flags.write_urb_in_use = 0;
  700. return;
  701. default:
  702. /* error in the urb, so we have to resubmit it */
  703. dev_dbg(&urb->dev->dev, "%s(): nonzero write bulk status received: %d\n", __func__, status);
  704. dev_dbg(&urb->dev->dev, "%s(): overflow in write\n", __func__);
  705. port->write_urb->transfer_buffer_length = 1;
  706. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  707. if (result) {
  708. dev_err_console(port, "%s(): usb_submit_urb() failed,"
  709. " error %d\n", __func__, result);
  710. } else {
  711. return;
  712. }
  713. }
  714. priv->flags.write_urb_in_use = 0;
  715. /* schedule the interrupt urb if we are still open */
  716. dev_dbg(&port->dev, "%s(): submitting interrupt urb\n", __func__);
  717. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  718. if (result != 0) {
  719. dev_err(&port->dev, "%s(): failed submitting int urb,"
  720. " error %d\n", __func__, result);
  721. }
  722. }
  723. module_usb_serial_driver(serial_drivers, id_table);
  724. MODULE_DESCRIPTION(OTI6858_DESCRIPTION);
  725. MODULE_AUTHOR(OTI6858_AUTHOR);
  726. MODULE_LICENSE("GPL v2");