oti6858.c 24 KB

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