oti6858.c 26 KB

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