oti6858.c 24 KB

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