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 (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, struct ktermios *old);
  113. static void oti6858_init_termios(struct tty_struct *tty);
  114. static void oti6858_read_int_callback(struct urb *urb);
  115. static void oti6858_read_bulk_callback(struct urb *urb);
  116. static void oti6858_write_bulk_callback(struct urb *urb);
  117. static int oti6858_write(struct tty_struct *tty, struct usb_serial_port *port,
  118. const unsigned char *buf, int count);
  119. static int oti6858_write_room(struct tty_struct *tty);
  120. static int oti6858_chars_in_buffer(struct tty_struct *tty);
  121. static int oti6858_tiocmget(struct tty_struct *tty);
  122. static int oti6858_tiocmset(struct tty_struct *tty,
  123. unsigned int set, unsigned int clear);
  124. static int oti6858_port_probe(struct usb_serial_port *port);
  125. static int oti6858_port_remove(struct usb_serial_port *port);
  126. /* device info */
  127. static struct usb_serial_driver oti6858_device = {
  128. .driver = {
  129. .owner = THIS_MODULE,
  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 int 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. return 0;
  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 int oti6858_write_room(struct tty_struct *tty)
  321. {
  322. struct usb_serial_port *port = tty->driver_data;
  323. int room = 0;
  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 int oti6858_chars_in_buffer(struct tty_struct *tty)
  331. {
  332. struct usb_serial_port *port = tty->driver_data;
  333. int chars = 0;
  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, 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. divisor = priv->pending_setup.divisor;
  356. frame_fmt = priv->pending_setup.frame_fmt;
  357. control = priv->pending_setup.control;
  358. spin_unlock_irqrestore(&priv->lock, flags);
  359. frame_fmt &= ~FMT_DATA_BITS_MASK;
  360. switch (cflag & CSIZE) {
  361. case CS5:
  362. frame_fmt |= FMT_DATA_BITS_5;
  363. break;
  364. case CS6:
  365. frame_fmt |= FMT_DATA_BITS_6;
  366. break;
  367. case CS7:
  368. frame_fmt |= FMT_DATA_BITS_7;
  369. break;
  370. default:
  371. case CS8:
  372. frame_fmt |= FMT_DATA_BITS_8;
  373. break;
  374. }
  375. /* manufacturer claims that this device can work with baud rates
  376. * up to 3 Mbps; I've tested it only on 115200 bps, so I can't
  377. * guarantee that any other baud rate will work (especially
  378. * the higher ones)
  379. */
  380. br = tty_get_baud_rate(tty);
  381. if (br == 0) {
  382. divisor = 0;
  383. } else {
  384. int real_br;
  385. int new_divisor;
  386. br = min(br, OTI6858_MAX_BAUD_RATE);
  387. new_divisor = (96000000 + 8 * br) / (16 * br);
  388. real_br = 96000000 / (16 * new_divisor);
  389. divisor = cpu_to_le16(new_divisor);
  390. tty_encode_baud_rate(tty, real_br, real_br);
  391. }
  392. frame_fmt &= ~FMT_STOP_BITS_MASK;
  393. if ((cflag & CSTOPB) != 0)
  394. frame_fmt |= FMT_STOP_BITS_2;
  395. else
  396. frame_fmt |= FMT_STOP_BITS_1;
  397. frame_fmt &= ~FMT_PARITY_MASK;
  398. if ((cflag & PARENB) != 0) {
  399. if ((cflag & PARODD) != 0)
  400. frame_fmt |= FMT_PARITY_ODD;
  401. else
  402. frame_fmt |= FMT_PARITY_EVEN;
  403. } else {
  404. frame_fmt |= FMT_PARITY_NONE;
  405. }
  406. control &= ~CONTROL_MASK;
  407. if ((cflag & CRTSCTS) != 0)
  408. control |= (CONTROL_DTR_HIGH | CONTROL_RTS_HIGH);
  409. /* change control lines if we are switching to or from B0 */
  410. /* FIXME:
  411. spin_lock_irqsave(&priv->lock, flags);
  412. control = priv->line_control;
  413. if ((cflag & CBAUD) == B0)
  414. priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
  415. else
  416. priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
  417. if (control != priv->line_control) {
  418. control = priv->line_control;
  419. spin_unlock_irqrestore(&priv->lock, flags);
  420. set_control_lines(serial->dev, control);
  421. } else {
  422. spin_unlock_irqrestore(&priv->lock, flags);
  423. }
  424. */
  425. spin_lock_irqsave(&priv->lock, flags);
  426. if (divisor != priv->pending_setup.divisor
  427. || control != priv->pending_setup.control
  428. || frame_fmt != priv->pending_setup.frame_fmt) {
  429. priv->pending_setup.divisor = divisor;
  430. priv->pending_setup.control = control;
  431. priv->pending_setup.frame_fmt = frame_fmt;
  432. }
  433. spin_unlock_irqrestore(&priv->lock, flags);
  434. }
  435. static int oti6858_open(struct tty_struct *tty, struct usb_serial_port *port)
  436. {
  437. struct oti6858_private *priv = usb_get_serial_port_data(port);
  438. struct usb_serial *serial = port->serial;
  439. struct oti6858_control_pkt *buf;
  440. unsigned long flags;
  441. int result;
  442. usb_clear_halt(serial->dev, port->write_urb->pipe);
  443. usb_clear_halt(serial->dev, port->read_urb->pipe);
  444. buf = kmalloc(OTI6858_CTRL_PKT_SIZE, GFP_KERNEL);
  445. if (!buf)
  446. return -ENOMEM;
  447. result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  448. OTI6858_REQ_T_GET_STATUS,
  449. OTI6858_REQ_GET_STATUS,
  450. 0, 0,
  451. buf, OTI6858_CTRL_PKT_SIZE,
  452. 100);
  453. if (result != OTI6858_CTRL_PKT_SIZE) {
  454. /* assume default (after power-on reset) values */
  455. buf->divisor = cpu_to_le16(0x009c); /* 38400 bps */
  456. buf->frame_fmt = 0x03; /* 8N1 */
  457. buf->something = 0x43;
  458. buf->control = 0x4c; /* DTR, RTS */
  459. buf->tx_status = 0x00;
  460. buf->pin_state = 0x5b; /* RTS, CTS, DSR, DTR, RI, DCD */
  461. buf->rx_bytes_avail = 0x00;
  462. }
  463. spin_lock_irqsave(&priv->lock, flags);
  464. memcpy(&priv->status, buf, OTI6858_CTRL_PKT_SIZE);
  465. priv->pending_setup.divisor = buf->divisor;
  466. priv->pending_setup.frame_fmt = buf->frame_fmt;
  467. priv->pending_setup.control = buf->control;
  468. spin_unlock_irqrestore(&priv->lock, flags);
  469. kfree(buf);
  470. dev_dbg(&port->dev, "%s(): submitting interrupt urb\n", __func__);
  471. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  472. if (result != 0) {
  473. dev_err(&port->dev, "%s(): usb_submit_urb() failed with error %d\n",
  474. __func__, result);
  475. oti6858_close(port);
  476. return result;
  477. }
  478. /* setup termios */
  479. if (tty)
  480. oti6858_set_termios(tty, port, NULL);
  481. return 0;
  482. }
  483. static void oti6858_close(struct usb_serial_port *port)
  484. {
  485. struct oti6858_private *priv = usb_get_serial_port_data(port);
  486. unsigned long flags;
  487. spin_lock_irqsave(&port->lock, flags);
  488. /* clear out any remaining data in the buffer */
  489. kfifo_reset_out(&port->write_fifo);
  490. spin_unlock_irqrestore(&port->lock, flags);
  491. dev_dbg(&port->dev, "%s(): after buf_clear()\n", __func__);
  492. /* cancel scheduled setup */
  493. cancel_delayed_work_sync(&priv->delayed_setup_work);
  494. cancel_delayed_work_sync(&priv->delayed_write_work);
  495. /* shutdown our urbs */
  496. dev_dbg(&port->dev, "%s(): shutting down urbs\n", __func__);
  497. usb_kill_urb(port->write_urb);
  498. usb_kill_urb(port->read_urb);
  499. usb_kill_urb(port->interrupt_in_urb);
  500. }
  501. static int oti6858_tiocmset(struct tty_struct *tty,
  502. unsigned int set, unsigned int clear)
  503. {
  504. struct usb_serial_port *port = tty->driver_data;
  505. struct oti6858_private *priv = usb_get_serial_port_data(port);
  506. unsigned long flags;
  507. u8 control;
  508. dev_dbg(&port->dev, "%s(set = 0x%08x, clear = 0x%08x)\n",
  509. __func__, set, clear);
  510. /* FIXME: check if this is correct (active high/low) */
  511. spin_lock_irqsave(&priv->lock, flags);
  512. control = priv->pending_setup.control;
  513. if ((set & TIOCM_RTS) != 0)
  514. control |= CONTROL_RTS_HIGH;
  515. if ((set & TIOCM_DTR) != 0)
  516. control |= CONTROL_DTR_HIGH;
  517. if ((clear & TIOCM_RTS) != 0)
  518. control &= ~CONTROL_RTS_HIGH;
  519. if ((clear & TIOCM_DTR) != 0)
  520. control &= ~CONTROL_DTR_HIGH;
  521. if (control != priv->pending_setup.control)
  522. priv->pending_setup.control = control;
  523. spin_unlock_irqrestore(&priv->lock, flags);
  524. return 0;
  525. }
  526. static int oti6858_tiocmget(struct tty_struct *tty)
  527. {
  528. struct usb_serial_port *port = tty->driver_data;
  529. struct oti6858_private *priv = usb_get_serial_port_data(port);
  530. unsigned long flags;
  531. unsigned pin_state;
  532. unsigned result = 0;
  533. spin_lock_irqsave(&priv->lock, flags);
  534. pin_state = priv->status.pin_state & PIN_MASK;
  535. spin_unlock_irqrestore(&priv->lock, flags);
  536. /* FIXME: check if this is correct (active high/low) */
  537. if ((pin_state & PIN_RTS) != 0)
  538. result |= TIOCM_RTS;
  539. if ((pin_state & PIN_CTS) != 0)
  540. result |= TIOCM_CTS;
  541. if ((pin_state & PIN_DSR) != 0)
  542. result |= TIOCM_DSR;
  543. if ((pin_state & PIN_DTR) != 0)
  544. result |= TIOCM_DTR;
  545. if ((pin_state & PIN_RI) != 0)
  546. result |= TIOCM_RI;
  547. if ((pin_state & PIN_DCD) != 0)
  548. result |= TIOCM_CD;
  549. dev_dbg(&port->dev, "%s() = 0x%08x\n", __func__, result);
  550. return result;
  551. }
  552. static void oti6858_read_int_callback(struct urb *urb)
  553. {
  554. struct usb_serial_port *port = urb->context;
  555. struct oti6858_private *priv = usb_get_serial_port_data(port);
  556. int transient = 0, can_recv = 0, resubmit = 1;
  557. int status = urb->status;
  558. switch (status) {
  559. case 0:
  560. /* success */
  561. break;
  562. case -ECONNRESET:
  563. case -ENOENT:
  564. case -ESHUTDOWN:
  565. /* this urb is terminated, clean up */
  566. dev_dbg(&urb->dev->dev, "%s(): urb shutting down with status: %d\n",
  567. __func__, status);
  568. return;
  569. default:
  570. dev_dbg(&urb->dev->dev, "%s(): nonzero urb status received: %d\n",
  571. __func__, status);
  572. break;
  573. }
  574. if (status == 0 && urb->actual_length == OTI6858_CTRL_PKT_SIZE) {
  575. struct oti6858_control_pkt *xs = urb->transfer_buffer;
  576. unsigned long flags;
  577. spin_lock_irqsave(&priv->lock, flags);
  578. if (!priv->transient) {
  579. if (!OTI6858_CTRL_EQUALS_PENDING(xs, priv)) {
  580. if (xs->rx_bytes_avail == 0) {
  581. priv->transient = 4;
  582. priv->setup_done = 0;
  583. resubmit = 0;
  584. dev_dbg(&port->dev, "%s(): scheduling setup_line()\n", __func__);
  585. schedule_delayed_work(&priv->delayed_setup_work, 0);
  586. }
  587. }
  588. } else {
  589. if (OTI6858_CTRL_EQUALS_PENDING(xs, priv)) {
  590. priv->transient = 0;
  591. } else if (!priv->setup_done) {
  592. resubmit = 0;
  593. } else if (--priv->transient == 0) {
  594. if (xs->rx_bytes_avail == 0) {
  595. priv->transient = 4;
  596. priv->setup_done = 0;
  597. resubmit = 0;
  598. dev_dbg(&port->dev, "%s(): scheduling setup_line()\n", __func__);
  599. schedule_delayed_work(&priv->delayed_setup_work, 0);
  600. }
  601. }
  602. }
  603. if (!priv->transient) {
  604. u8 delta = xs->pin_state ^ priv->status.pin_state;
  605. if (delta & PIN_MSR_MASK) {
  606. if (delta & PIN_CTS)
  607. port->icount.cts++;
  608. if (delta & PIN_DSR)
  609. port->icount.dsr++;
  610. if (delta & PIN_RI)
  611. port->icount.rng++;
  612. if (delta & PIN_DCD)
  613. port->icount.dcd++;
  614. wake_up_interruptible(&port->port.delta_msr_wait);
  615. }
  616. memcpy(&priv->status, xs, OTI6858_CTRL_PKT_SIZE);
  617. }
  618. if (!priv->transient && xs->rx_bytes_avail != 0) {
  619. can_recv = xs->rx_bytes_avail;
  620. priv->flags.read_urb_in_use = 1;
  621. }
  622. transient = priv->transient;
  623. spin_unlock_irqrestore(&priv->lock, flags);
  624. }
  625. if (can_recv) {
  626. int result;
  627. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  628. if (result != 0) {
  629. priv->flags.read_urb_in_use = 0;
  630. dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
  631. " error %d\n", __func__, result);
  632. } else {
  633. resubmit = 0;
  634. }
  635. } else if (!transient) {
  636. unsigned long flags;
  637. int count;
  638. spin_lock_irqsave(&port->lock, flags);
  639. count = kfifo_len(&port->write_fifo);
  640. spin_unlock_irqrestore(&port->lock, flags);
  641. spin_lock_irqsave(&priv->lock, flags);
  642. if (priv->flags.write_urb_in_use == 0 && count != 0) {
  643. schedule_delayed_work(&priv->delayed_write_work, 0);
  644. resubmit = 0;
  645. }
  646. spin_unlock_irqrestore(&priv->lock, flags);
  647. }
  648. if (resubmit) {
  649. int result;
  650. /* dev_dbg(&urb->dev->dev, "%s(): submitting interrupt urb\n", __func__); */
  651. result = usb_submit_urb(urb, GFP_ATOMIC);
  652. if (result != 0) {
  653. dev_err(&urb->dev->dev,
  654. "%s(): usb_submit_urb() failed with"
  655. " error %d\n", __func__, result);
  656. }
  657. }
  658. }
  659. static void oti6858_read_bulk_callback(struct urb *urb)
  660. {
  661. struct usb_serial_port *port = urb->context;
  662. struct oti6858_private *priv = usb_get_serial_port_data(port);
  663. unsigned char *data = urb->transfer_buffer;
  664. unsigned long flags;
  665. int status = urb->status;
  666. int result;
  667. spin_lock_irqsave(&priv->lock, flags);
  668. priv->flags.read_urb_in_use = 0;
  669. spin_unlock_irqrestore(&priv->lock, flags);
  670. if (status != 0) {
  671. dev_dbg(&urb->dev->dev, "%s(): unable to handle the error, exiting\n", __func__);
  672. return;
  673. }
  674. if (urb->actual_length > 0) {
  675. tty_insert_flip_string(&port->port, data, urb->actual_length);
  676. tty_flip_buffer_push(&port->port);
  677. }
  678. /* schedule the interrupt urb */
  679. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  680. if (result != 0 && result != -EPERM) {
  681. dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
  682. " error %d\n", __func__, result);
  683. }
  684. }
  685. static void oti6858_write_bulk_callback(struct urb *urb)
  686. {
  687. struct usb_serial_port *port = urb->context;
  688. struct oti6858_private *priv = usb_get_serial_port_data(port);
  689. int status = urb->status;
  690. int result;
  691. switch (status) {
  692. case 0:
  693. /* success */
  694. break;
  695. case -ECONNRESET:
  696. case -ENOENT:
  697. case -ESHUTDOWN:
  698. /* this urb is terminated, clean up */
  699. dev_dbg(&urb->dev->dev, "%s(): urb shutting down with status: %d\n", __func__, status);
  700. priv->flags.write_urb_in_use = 0;
  701. return;
  702. default:
  703. /* error in the urb, so we have to resubmit it */
  704. dev_dbg(&urb->dev->dev, "%s(): nonzero write bulk status received: %d\n", __func__, status);
  705. dev_dbg(&urb->dev->dev, "%s(): overflow in write\n", __func__);
  706. port->write_urb->transfer_buffer_length = 1;
  707. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  708. if (result) {
  709. dev_err_console(port, "%s(): usb_submit_urb() failed,"
  710. " error %d\n", __func__, result);
  711. } else {
  712. return;
  713. }
  714. }
  715. priv->flags.write_urb_in_use = 0;
  716. /* schedule the interrupt urb if we are still open */
  717. dev_dbg(&port->dev, "%s(): submitting interrupt urb\n", __func__);
  718. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  719. if (result != 0) {
  720. dev_err(&port->dev, "%s(): failed submitting int urb,"
  721. " error %d\n", __func__, result);
  722. }
  723. }
  724. module_usb_serial_driver(serial_drivers, id_table);
  725. MODULE_DESCRIPTION(OTI6858_DESCRIPTION);
  726. MODULE_AUTHOR(OTI6858_AUTHOR);
  727. MODULE_LICENSE("GPL v2");