oti6858.c 27 KB

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