oti6858.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  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/init.h>
  42. #include <linux/slab.h>
  43. #include <linux/tty.h>
  44. #include <linux/tty_driver.h>
  45. #include <linux/tty_flip.h>
  46. #include <linux/serial.h>
  47. #include <linux/module.h>
  48. #include <linux/moduleparam.h>
  49. #include <linux/spinlock.h>
  50. #include <linux/usb.h>
  51. #include <linux/usb/serial.h>
  52. #include <linux/uaccess.h>
  53. #include <linux/kfifo.h>
  54. #include "oti6858.h"
  55. #define OTI6858_DESCRIPTION \
  56. "Ours Technology Inc. OTi-6858 USB to serial adapter driver"
  57. #define OTI6858_AUTHOR "Tomasz Michal Lukaszewski <FIXME@FIXME>"
  58. #define OTI6858_VERSION "0.2"
  59. static const struct usb_device_id id_table[] = {
  60. { USB_DEVICE(OTI6858_VENDOR_ID, OTI6858_PRODUCT_ID) },
  61. { }
  62. };
  63. MODULE_DEVICE_TABLE(usb, id_table);
  64. /* requests */
  65. #define OTI6858_REQ_GET_STATUS (USB_DIR_IN | USB_TYPE_VENDOR | 0x00)
  66. #define OTI6858_REQ_T_GET_STATUS 0x01
  67. #define OTI6858_REQ_SET_LINE (USB_DIR_OUT | USB_TYPE_VENDOR | 0x00)
  68. #define OTI6858_REQ_T_SET_LINE 0x00
  69. #define OTI6858_REQ_CHECK_TXBUFF (USB_DIR_IN | USB_TYPE_VENDOR | 0x01)
  70. #define OTI6858_REQ_T_CHECK_TXBUFF 0x00
  71. /* format of the control packet */
  72. struct oti6858_control_pkt {
  73. __le16 divisor; /* baud rate = 96000000 / (16 * divisor), LE */
  74. #define OTI6858_MAX_BAUD_RATE 3000000
  75. u8 frame_fmt;
  76. #define FMT_STOP_BITS_MASK 0xc0
  77. #define FMT_STOP_BITS_1 0x00
  78. #define FMT_STOP_BITS_2 0x40 /* 1.5 stop bits if FMT_DATA_BITS_5 */
  79. #define FMT_PARITY_MASK 0x38
  80. #define FMT_PARITY_NONE 0x00
  81. #define FMT_PARITY_ODD 0x08
  82. #define FMT_PARITY_EVEN 0x18
  83. #define FMT_PARITY_MARK 0x28
  84. #define FMT_PARITY_SPACE 0x38
  85. #define FMT_DATA_BITS_MASK 0x03
  86. #define FMT_DATA_BITS_5 0x00
  87. #define FMT_DATA_BITS_6 0x01
  88. #define FMT_DATA_BITS_7 0x02
  89. #define FMT_DATA_BITS_8 0x03
  90. u8 something; /* always equals 0x43 */
  91. u8 control; /* settings of flow control lines */
  92. #define CONTROL_MASK 0x0c
  93. #define CONTROL_DTR_HIGH 0x08
  94. #define CONTROL_RTS_HIGH 0x04
  95. u8 tx_status;
  96. #define TX_BUFFER_EMPTIED 0x09
  97. u8 pin_state;
  98. #define PIN_MASK 0x3f
  99. #define PIN_RTS 0x20 /* output pin */
  100. #define PIN_CTS 0x10 /* input pin, active low */
  101. #define PIN_DSR 0x08 /* input pin, active low */
  102. #define PIN_DTR 0x04 /* output pin */
  103. #define PIN_RI 0x02 /* input pin, active low */
  104. #define PIN_DCD 0x01 /* input pin, active low */
  105. u8 rx_bytes_avail; /* number of bytes in rx buffer */;
  106. };
  107. #define OTI6858_CTRL_PKT_SIZE sizeof(struct oti6858_control_pkt)
  108. #define OTI6858_CTRL_EQUALS_PENDING(a, priv) \
  109. (((a)->divisor == (priv)->pending_setup.divisor) \
  110. && ((a)->control == (priv)->pending_setup.control) \
  111. && ((a)->frame_fmt == (priv)->pending_setup.frame_fmt))
  112. /* function prototypes */
  113. static int oti6858_open(struct tty_struct *tty, struct usb_serial_port *port);
  114. static void oti6858_close(struct usb_serial_port *port);
  115. static void oti6858_set_termios(struct tty_struct *tty,
  116. struct usb_serial_port *port, struct ktermios *old);
  117. static void oti6858_init_termios(struct tty_struct *tty);
  118. static int oti6858_ioctl(struct tty_struct *tty,
  119. unsigned int cmd, unsigned long arg);
  120. static void oti6858_read_int_callback(struct urb *urb);
  121. static void oti6858_read_bulk_callback(struct urb *urb);
  122. static void oti6858_write_bulk_callback(struct urb *urb);
  123. static int oti6858_write(struct tty_struct *tty, struct usb_serial_port *port,
  124. const unsigned char *buf, int count);
  125. static int oti6858_write_room(struct tty_struct *tty);
  126. static int oti6858_chars_in_buffer(struct tty_struct *tty);
  127. static int oti6858_tiocmget(struct tty_struct *tty);
  128. static int oti6858_tiocmset(struct tty_struct *tty,
  129. unsigned int set, unsigned int clear);
  130. static int oti6858_port_probe(struct usb_serial_port *port);
  131. static int oti6858_port_remove(struct usb_serial_port *port);
  132. /* device info */
  133. static struct usb_serial_driver oti6858_device = {
  134. .driver = {
  135. .owner = THIS_MODULE,
  136. .name = "oti6858",
  137. },
  138. .id_table = id_table,
  139. .num_ports = 1,
  140. .open = oti6858_open,
  141. .close = oti6858_close,
  142. .write = oti6858_write,
  143. .ioctl = oti6858_ioctl,
  144. .set_termios = oti6858_set_termios,
  145. .init_termios = oti6858_init_termios,
  146. .tiocmget = oti6858_tiocmget,
  147. .tiocmset = oti6858_tiocmset,
  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. wait_queue_head_t intr_wait;
  176. struct usb_serial_port *port; /* USB port with which associated */
  177. };
  178. static void setup_line(struct work_struct *work)
  179. {
  180. struct oti6858_private *priv = container_of(work,
  181. struct oti6858_private, delayed_setup_work.work);
  182. struct usb_serial_port *port = priv->port;
  183. struct oti6858_control_pkt *new_setup;
  184. unsigned long flags;
  185. int result;
  186. new_setup = kmalloc(OTI6858_CTRL_PKT_SIZE, GFP_KERNEL);
  187. if (new_setup == NULL) {
  188. dev_err(&port->dev, "%s(): out of memory!\n", __func__);
  189. /* we will try again */
  190. schedule_delayed_work(&priv->delayed_setup_work,
  191. msecs_to_jiffies(2));
  192. return;
  193. }
  194. result = usb_control_msg(port->serial->dev,
  195. usb_rcvctrlpipe(port->serial->dev, 0),
  196. OTI6858_REQ_T_GET_STATUS,
  197. OTI6858_REQ_GET_STATUS,
  198. 0, 0,
  199. new_setup, OTI6858_CTRL_PKT_SIZE,
  200. 100);
  201. if (result != OTI6858_CTRL_PKT_SIZE) {
  202. dev_err(&port->dev, "%s(): error reading status\n", __func__);
  203. kfree(new_setup);
  204. /* we will try again */
  205. schedule_delayed_work(&priv->delayed_setup_work,
  206. msecs_to_jiffies(2));
  207. return;
  208. }
  209. spin_lock_irqsave(&priv->lock, flags);
  210. if (!OTI6858_CTRL_EQUALS_PENDING(new_setup, priv)) {
  211. new_setup->divisor = priv->pending_setup.divisor;
  212. new_setup->control = priv->pending_setup.control;
  213. new_setup->frame_fmt = priv->pending_setup.frame_fmt;
  214. spin_unlock_irqrestore(&priv->lock, flags);
  215. result = usb_control_msg(port->serial->dev,
  216. usb_sndctrlpipe(port->serial->dev, 0),
  217. OTI6858_REQ_T_SET_LINE,
  218. OTI6858_REQ_SET_LINE,
  219. 0, 0,
  220. new_setup, OTI6858_CTRL_PKT_SIZE,
  221. 100);
  222. } else {
  223. spin_unlock_irqrestore(&priv->lock, flags);
  224. result = 0;
  225. }
  226. kfree(new_setup);
  227. spin_lock_irqsave(&priv->lock, flags);
  228. if (result != OTI6858_CTRL_PKT_SIZE)
  229. priv->transient = 0;
  230. priv->setup_done = 1;
  231. spin_unlock_irqrestore(&priv->lock, flags);
  232. dev_dbg(&port->dev, "%s(): submitting interrupt urb\n", __func__);
  233. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  234. if (result != 0) {
  235. dev_err(&port->dev, "%s(): usb_submit_urb() failed with error %d\n",
  236. __func__, result);
  237. }
  238. }
  239. static void send_data(struct work_struct *work)
  240. {
  241. struct oti6858_private *priv = container_of(work,
  242. struct oti6858_private, delayed_write_work.work);
  243. struct usb_serial_port *port = priv->port;
  244. int count = 0, result;
  245. unsigned long flags;
  246. u8 *allow;
  247. spin_lock_irqsave(&priv->lock, flags);
  248. if (priv->flags.write_urb_in_use) {
  249. spin_unlock_irqrestore(&priv->lock, flags);
  250. schedule_delayed_work(&priv->delayed_write_work,
  251. msecs_to_jiffies(2));
  252. return;
  253. }
  254. priv->flags.write_urb_in_use = 1;
  255. spin_unlock_irqrestore(&priv->lock, flags);
  256. spin_lock_irqsave(&port->lock, flags);
  257. count = kfifo_len(&port->write_fifo);
  258. spin_unlock_irqrestore(&port->lock, flags);
  259. if (count > port->bulk_out_size)
  260. count = port->bulk_out_size;
  261. if (count != 0) {
  262. allow = kmalloc(1, GFP_KERNEL);
  263. if (!allow) {
  264. dev_err_console(port, "%s(): kmalloc failed\n",
  265. __func__);
  266. return;
  267. }
  268. result = usb_control_msg(port->serial->dev,
  269. usb_rcvctrlpipe(port->serial->dev, 0),
  270. OTI6858_REQ_T_CHECK_TXBUFF,
  271. OTI6858_REQ_CHECK_TXBUFF,
  272. count, 0, allow, 1, 100);
  273. if (result != 1 || *allow != 0)
  274. count = 0;
  275. kfree(allow);
  276. }
  277. if (count == 0) {
  278. priv->flags.write_urb_in_use = 0;
  279. dev_dbg(&port->dev, "%s(): submitting interrupt urb\n", __func__);
  280. result = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO);
  281. if (result != 0) {
  282. dev_err(&port->dev, "%s(): usb_submit_urb() failed with error %d\n",
  283. __func__, result);
  284. }
  285. return;
  286. }
  287. count = kfifo_out_locked(&port->write_fifo,
  288. port->write_urb->transfer_buffer,
  289. count, &port->lock);
  290. port->write_urb->transfer_buffer_length = count;
  291. result = usb_submit_urb(port->write_urb, GFP_NOIO);
  292. if (result != 0) {
  293. dev_err_console(port, "%s(): usb_submit_urb() failed with error %d\n",
  294. __func__, result);
  295. priv->flags.write_urb_in_use = 0;
  296. }
  297. usb_serial_port_softint(port);
  298. }
  299. static int oti6858_port_probe(struct usb_serial_port *port)
  300. {
  301. struct oti6858_private *priv;
  302. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  303. if (!priv)
  304. return -ENOMEM;
  305. spin_lock_init(&priv->lock);
  306. init_waitqueue_head(&priv->intr_wait);
  307. priv->port = port;
  308. INIT_DELAYED_WORK(&priv->delayed_setup_work, setup_line);
  309. INIT_DELAYED_WORK(&priv->delayed_write_work, send_data);
  310. usb_set_serial_port_data(port, priv);
  311. return 0;
  312. }
  313. static int oti6858_port_remove(struct usb_serial_port *port)
  314. {
  315. struct oti6858_private *priv;
  316. priv = usb_get_serial_port_data(port);
  317. kfree(priv);
  318. return 0;
  319. }
  320. static int oti6858_write(struct tty_struct *tty, struct usb_serial_port *port,
  321. const unsigned char *buf, int count)
  322. {
  323. if (!count)
  324. return count;
  325. count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock);
  326. return count;
  327. }
  328. static int oti6858_write_room(struct tty_struct *tty)
  329. {
  330. struct usb_serial_port *port = tty->driver_data;
  331. int room = 0;
  332. unsigned long flags;
  333. spin_lock_irqsave(&port->lock, flags);
  334. room = kfifo_avail(&port->write_fifo);
  335. spin_unlock_irqrestore(&port->lock, flags);
  336. return room;
  337. }
  338. static int oti6858_chars_in_buffer(struct tty_struct *tty)
  339. {
  340. struct usb_serial_port *port = tty->driver_data;
  341. int chars = 0;
  342. unsigned long flags;
  343. spin_lock_irqsave(&port->lock, flags);
  344. chars = kfifo_len(&port->write_fifo);
  345. spin_unlock_irqrestore(&port->lock, flags);
  346. return chars;
  347. }
  348. static void oti6858_init_termios(struct tty_struct *tty)
  349. {
  350. tty->termios = tty_std_termios;
  351. tty->termios.c_cflag = B38400 | CS8 | CREAD | HUPCL | CLOCAL;
  352. tty->termios.c_ispeed = 38400;
  353. tty->termios.c_ospeed = 38400;
  354. }
  355. static void oti6858_set_termios(struct tty_struct *tty,
  356. struct usb_serial_port *port, struct ktermios *old_termios)
  357. {
  358. struct oti6858_private *priv = usb_get_serial_port_data(port);
  359. unsigned long flags;
  360. unsigned int cflag;
  361. u8 frame_fmt, control;
  362. __le16 divisor;
  363. int br;
  364. if (!tty)
  365. return;
  366. cflag = tty->termios.c_cflag;
  367. spin_lock_irqsave(&priv->lock, flags);
  368. divisor = priv->pending_setup.divisor;
  369. frame_fmt = priv->pending_setup.frame_fmt;
  370. control = priv->pending_setup.control;
  371. spin_unlock_irqrestore(&priv->lock, flags);
  372. frame_fmt &= ~FMT_DATA_BITS_MASK;
  373. switch (cflag & CSIZE) {
  374. case CS5:
  375. frame_fmt |= FMT_DATA_BITS_5;
  376. break;
  377. case CS6:
  378. frame_fmt |= FMT_DATA_BITS_6;
  379. break;
  380. case CS7:
  381. frame_fmt |= FMT_DATA_BITS_7;
  382. break;
  383. default:
  384. case CS8:
  385. frame_fmt |= FMT_DATA_BITS_8;
  386. break;
  387. }
  388. /* manufacturer claims that this device can work with baud rates
  389. * up to 3 Mbps; I've tested it only on 115200 bps, so I can't
  390. * guarantee that any other baud rate will work (especially
  391. * the higher ones)
  392. */
  393. br = tty_get_baud_rate(tty);
  394. if (br == 0) {
  395. divisor = 0;
  396. } else {
  397. int real_br;
  398. int new_divisor;
  399. br = min(br, OTI6858_MAX_BAUD_RATE);
  400. new_divisor = (96000000 + 8 * br) / (16 * br);
  401. real_br = 96000000 / (16 * new_divisor);
  402. divisor = cpu_to_le16(new_divisor);
  403. tty_encode_baud_rate(tty, real_br, real_br);
  404. }
  405. frame_fmt &= ~FMT_STOP_BITS_MASK;
  406. if ((cflag & CSTOPB) != 0)
  407. frame_fmt |= FMT_STOP_BITS_2;
  408. else
  409. frame_fmt |= FMT_STOP_BITS_1;
  410. frame_fmt &= ~FMT_PARITY_MASK;
  411. if ((cflag & PARENB) != 0) {
  412. if ((cflag & PARODD) != 0)
  413. frame_fmt |= FMT_PARITY_ODD;
  414. else
  415. frame_fmt |= FMT_PARITY_EVEN;
  416. } else {
  417. frame_fmt |= FMT_PARITY_NONE;
  418. }
  419. control &= ~CONTROL_MASK;
  420. if ((cflag & CRTSCTS) != 0)
  421. control |= (CONTROL_DTR_HIGH | CONTROL_RTS_HIGH);
  422. /* change control lines if we are switching to or from B0 */
  423. /* FIXME:
  424. spin_lock_irqsave(&priv->lock, flags);
  425. control = priv->line_control;
  426. if ((cflag & CBAUD) == B0)
  427. priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
  428. else
  429. priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
  430. if (control != priv->line_control) {
  431. control = priv->line_control;
  432. spin_unlock_irqrestore(&priv->lock, flags);
  433. set_control_lines(serial->dev, control);
  434. } else {
  435. spin_unlock_irqrestore(&priv->lock, flags);
  436. }
  437. */
  438. spin_lock_irqsave(&priv->lock, flags);
  439. if (divisor != priv->pending_setup.divisor
  440. || control != priv->pending_setup.control
  441. || frame_fmt != priv->pending_setup.frame_fmt) {
  442. priv->pending_setup.divisor = divisor;
  443. priv->pending_setup.control = control;
  444. priv->pending_setup.frame_fmt = frame_fmt;
  445. }
  446. spin_unlock_irqrestore(&priv->lock, flags);
  447. }
  448. static int oti6858_open(struct tty_struct *tty, struct usb_serial_port *port)
  449. {
  450. struct oti6858_private *priv = usb_get_serial_port_data(port);
  451. struct ktermios tmp_termios;
  452. struct usb_serial *serial = port->serial;
  453. struct oti6858_control_pkt *buf;
  454. unsigned long flags;
  455. int result;
  456. usb_clear_halt(serial->dev, port->write_urb->pipe);
  457. usb_clear_halt(serial->dev, port->read_urb->pipe);
  458. buf = kmalloc(OTI6858_CTRL_PKT_SIZE, GFP_KERNEL);
  459. if (buf == NULL) {
  460. dev_err(&port->dev, "%s(): out of memory!\n", __func__);
  461. return -ENOMEM;
  462. }
  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, &tmp_termios);
  497. port->port.drain_delay = 256; /* FIXME: check the FIFO length */
  498. return 0;
  499. }
  500. static void oti6858_close(struct usb_serial_port *port)
  501. {
  502. struct oti6858_private *priv = usb_get_serial_port_data(port);
  503. unsigned long flags;
  504. spin_lock_irqsave(&port->lock, flags);
  505. /* clear out any remaining data in the buffer */
  506. kfifo_reset_out(&port->write_fifo);
  507. spin_unlock_irqrestore(&port->lock, flags);
  508. dev_dbg(&port->dev, "%s(): after buf_clear()\n", __func__);
  509. /* cancel scheduled setup */
  510. cancel_delayed_work_sync(&priv->delayed_setup_work);
  511. cancel_delayed_work_sync(&priv->delayed_write_work);
  512. /* shutdown our urbs */
  513. dev_dbg(&port->dev, "%s(): shutting down urbs\n", __func__);
  514. usb_kill_urb(port->write_urb);
  515. usb_kill_urb(port->read_urb);
  516. usb_kill_urb(port->interrupt_in_urb);
  517. }
  518. static int oti6858_tiocmset(struct tty_struct *tty,
  519. unsigned int set, unsigned int clear)
  520. {
  521. struct usb_serial_port *port = tty->driver_data;
  522. struct oti6858_private *priv = usb_get_serial_port_data(port);
  523. unsigned long flags;
  524. u8 control;
  525. dev_dbg(&port->dev, "%s(set = 0x%08x, clear = 0x%08x)\n",
  526. __func__, set, clear);
  527. /* FIXME: check if this is correct (active high/low) */
  528. spin_lock_irqsave(&priv->lock, flags);
  529. control = priv->pending_setup.control;
  530. if ((set & TIOCM_RTS) != 0)
  531. control |= CONTROL_RTS_HIGH;
  532. if ((set & TIOCM_DTR) != 0)
  533. control |= CONTROL_DTR_HIGH;
  534. if ((clear & TIOCM_RTS) != 0)
  535. control &= ~CONTROL_RTS_HIGH;
  536. if ((clear & TIOCM_DTR) != 0)
  537. control &= ~CONTROL_DTR_HIGH;
  538. if (control != priv->pending_setup.control)
  539. priv->pending_setup.control = control;
  540. spin_unlock_irqrestore(&priv->lock, flags);
  541. return 0;
  542. }
  543. static int oti6858_tiocmget(struct tty_struct *tty)
  544. {
  545. struct usb_serial_port *port = tty->driver_data;
  546. struct oti6858_private *priv = usb_get_serial_port_data(port);
  547. unsigned long flags;
  548. unsigned pin_state;
  549. unsigned result = 0;
  550. spin_lock_irqsave(&priv->lock, flags);
  551. pin_state = priv->status.pin_state & PIN_MASK;
  552. spin_unlock_irqrestore(&priv->lock, flags);
  553. /* FIXME: check if this is correct (active high/low) */
  554. if ((pin_state & PIN_RTS) != 0)
  555. result |= TIOCM_RTS;
  556. if ((pin_state & PIN_CTS) != 0)
  557. result |= TIOCM_CTS;
  558. if ((pin_state & PIN_DSR) != 0)
  559. result |= TIOCM_DSR;
  560. if ((pin_state & PIN_DTR) != 0)
  561. result |= TIOCM_DTR;
  562. if ((pin_state & PIN_RI) != 0)
  563. result |= TIOCM_RI;
  564. if ((pin_state & PIN_DCD) != 0)
  565. result |= TIOCM_CD;
  566. dev_dbg(&port->dev, "%s() = 0x%08x\n", __func__, result);
  567. return result;
  568. }
  569. static int wait_modem_info(struct usb_serial_port *port, unsigned int arg)
  570. {
  571. struct oti6858_private *priv = usb_get_serial_port_data(port);
  572. unsigned long flags;
  573. unsigned int prev, status;
  574. unsigned int changed;
  575. spin_lock_irqsave(&priv->lock, flags);
  576. prev = priv->status.pin_state;
  577. spin_unlock_irqrestore(&priv->lock, flags);
  578. while (1) {
  579. wait_event_interruptible(priv->intr_wait,
  580. priv->status.pin_state != prev);
  581. if (signal_pending(current))
  582. return -ERESTARTSYS;
  583. spin_lock_irqsave(&priv->lock, flags);
  584. status = priv->status.pin_state & PIN_MASK;
  585. spin_unlock_irqrestore(&priv->lock, flags);
  586. changed = prev ^ status;
  587. /* FIXME: check if this is correct (active high/low) */
  588. if (((arg & TIOCM_RNG) && (changed & PIN_RI)) ||
  589. ((arg & TIOCM_DSR) && (changed & PIN_DSR)) ||
  590. ((arg & TIOCM_CD) && (changed & PIN_DCD)) ||
  591. ((arg & TIOCM_CTS) && (changed & PIN_CTS)))
  592. return 0;
  593. prev = status;
  594. }
  595. /* NOTREACHED */
  596. return 0;
  597. }
  598. static int oti6858_ioctl(struct tty_struct *tty,
  599. unsigned int cmd, unsigned long arg)
  600. {
  601. struct usb_serial_port *port = tty->driver_data;
  602. dev_dbg(&port->dev, "%s(cmd = 0x%04x, arg = 0x%08lx)\n", __func__, cmd, arg);
  603. switch (cmd) {
  604. case TIOCMIWAIT:
  605. dev_dbg(&port->dev, "%s(): TIOCMIWAIT\n", __func__);
  606. return wait_modem_info(port, arg);
  607. default:
  608. dev_dbg(&port->dev, "%s(): 0x%04x not supported\n", __func__, cmd);
  609. break;
  610. }
  611. return -ENOIOCTLCMD;
  612. }
  613. static void oti6858_read_int_callback(struct urb *urb)
  614. {
  615. struct usb_serial_port *port = urb->context;
  616. struct oti6858_private *priv = usb_get_serial_port_data(port);
  617. int transient = 0, can_recv = 0, resubmit = 1;
  618. int status = urb->status;
  619. switch (status) {
  620. case 0:
  621. /* success */
  622. break;
  623. case -ECONNRESET:
  624. case -ENOENT:
  625. case -ESHUTDOWN:
  626. /* this urb is terminated, clean up */
  627. dev_dbg(&urb->dev->dev, "%s(): urb shutting down with status: %d\n",
  628. __func__, status);
  629. return;
  630. default:
  631. dev_dbg(&urb->dev->dev, "%s(): nonzero urb status received: %d\n",
  632. __func__, status);
  633. break;
  634. }
  635. if (status == 0 && urb->actual_length == OTI6858_CTRL_PKT_SIZE) {
  636. struct oti6858_control_pkt *xs = urb->transfer_buffer;
  637. unsigned long flags;
  638. spin_lock_irqsave(&priv->lock, flags);
  639. if (!priv->transient) {
  640. if (!OTI6858_CTRL_EQUALS_PENDING(xs, priv)) {
  641. if (xs->rx_bytes_avail == 0) {
  642. priv->transient = 4;
  643. priv->setup_done = 0;
  644. resubmit = 0;
  645. dev_dbg(&port->dev, "%s(): scheduling setup_line()\n", __func__);
  646. schedule_delayed_work(&priv->delayed_setup_work, 0);
  647. }
  648. }
  649. } else {
  650. if (OTI6858_CTRL_EQUALS_PENDING(xs, priv)) {
  651. priv->transient = 0;
  652. } else if (!priv->setup_done) {
  653. resubmit = 0;
  654. } else if (--priv->transient == 0) {
  655. if (xs->rx_bytes_avail == 0) {
  656. priv->transient = 4;
  657. priv->setup_done = 0;
  658. resubmit = 0;
  659. dev_dbg(&port->dev, "%s(): scheduling setup_line()\n", __func__);
  660. schedule_delayed_work(&priv->delayed_setup_work, 0);
  661. }
  662. }
  663. }
  664. if (!priv->transient) {
  665. if (xs->pin_state != priv->status.pin_state)
  666. wake_up_interruptible(&priv->intr_wait);
  667. memcpy(&priv->status, xs, OTI6858_CTRL_PKT_SIZE);
  668. }
  669. if (!priv->transient && xs->rx_bytes_avail != 0) {
  670. can_recv = xs->rx_bytes_avail;
  671. priv->flags.read_urb_in_use = 1;
  672. }
  673. transient = priv->transient;
  674. spin_unlock_irqrestore(&priv->lock, flags);
  675. }
  676. if (can_recv) {
  677. int result;
  678. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  679. if (result != 0) {
  680. priv->flags.read_urb_in_use = 0;
  681. dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
  682. " error %d\n", __func__, result);
  683. } else {
  684. resubmit = 0;
  685. }
  686. } else if (!transient) {
  687. unsigned long flags;
  688. int count;
  689. spin_lock_irqsave(&port->lock, flags);
  690. count = kfifo_len(&port->write_fifo);
  691. spin_unlock_irqrestore(&port->lock, flags);
  692. spin_lock_irqsave(&priv->lock, flags);
  693. if (priv->flags.write_urb_in_use == 0 && count != 0) {
  694. schedule_delayed_work(&priv->delayed_write_work, 0);
  695. resubmit = 0;
  696. }
  697. spin_unlock_irqrestore(&priv->lock, flags);
  698. }
  699. if (resubmit) {
  700. int result;
  701. /* dev_dbg(&urb->dev->dev, "%s(): submitting interrupt urb\n", __func__); */
  702. result = usb_submit_urb(urb, GFP_ATOMIC);
  703. if (result != 0) {
  704. dev_err(&urb->dev->dev,
  705. "%s(): usb_submit_urb() failed with"
  706. " error %d\n", __func__, result);
  707. }
  708. }
  709. }
  710. static void oti6858_read_bulk_callback(struct urb *urb)
  711. {
  712. struct usb_serial_port *port = urb->context;
  713. struct oti6858_private *priv = usb_get_serial_port_data(port);
  714. struct tty_struct *tty;
  715. unsigned char *data = urb->transfer_buffer;
  716. unsigned long flags;
  717. int status = urb->status;
  718. int result;
  719. spin_lock_irqsave(&priv->lock, flags);
  720. priv->flags.read_urb_in_use = 0;
  721. spin_unlock_irqrestore(&priv->lock, flags);
  722. if (status != 0) {
  723. dev_dbg(&urb->dev->dev, "%s(): unable to handle the error, exiting\n", __func__);
  724. return;
  725. }
  726. tty = tty_port_tty_get(&port->port);
  727. if (tty != NULL && urb->actual_length > 0) {
  728. tty_insert_flip_string(tty, data, urb->actual_length);
  729. tty_flip_buffer_push(tty);
  730. }
  731. tty_kref_put(tty);
  732. /* schedule the interrupt urb */
  733. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  734. if (result != 0 && result != -EPERM) {
  735. dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
  736. " error %d\n", __func__, result);
  737. }
  738. }
  739. static void oti6858_write_bulk_callback(struct urb *urb)
  740. {
  741. struct usb_serial_port *port = urb->context;
  742. struct oti6858_private *priv = usb_get_serial_port_data(port);
  743. int status = urb->status;
  744. int result;
  745. switch (status) {
  746. case 0:
  747. /* success */
  748. break;
  749. case -ECONNRESET:
  750. case -ENOENT:
  751. case -ESHUTDOWN:
  752. /* this urb is terminated, clean up */
  753. dev_dbg(&urb->dev->dev, "%s(): urb shutting down with status: %d\n", __func__, status);
  754. priv->flags.write_urb_in_use = 0;
  755. return;
  756. default:
  757. /* error in the urb, so we have to resubmit it */
  758. dev_dbg(&urb->dev->dev, "%s(): nonzero write bulk status received: %d\n", __func__, status);
  759. dev_dbg(&urb->dev->dev, "%s(): overflow in write\n", __func__);
  760. port->write_urb->transfer_buffer_length = 1;
  761. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  762. if (result) {
  763. dev_err_console(port, "%s(): usb_submit_urb() failed,"
  764. " error %d\n", __func__, result);
  765. } else {
  766. return;
  767. }
  768. }
  769. priv->flags.write_urb_in_use = 0;
  770. /* schedule the interrupt urb if we are still open */
  771. dev_dbg(&port->dev, "%s(): submitting interrupt urb\n", __func__);
  772. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  773. if (result != 0) {
  774. dev_err(&port->dev, "%s(): failed submitting int urb,"
  775. " error %d\n", __func__, result);
  776. }
  777. }
  778. module_usb_serial_driver(serial_drivers, id_table);
  779. MODULE_DESCRIPTION(OTI6858_DESCRIPTION);
  780. MODULE_AUTHOR(OTI6858_AUTHOR);
  781. MODULE_VERSION(OTI6858_VERSION);
  782. MODULE_LICENSE("GPL");