oti6858.c 26 KB

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