oti6858.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  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 int 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. port->interrupt_in_urb->dev = port->serial->dev;
  241. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  242. if (result != 0) {
  243. dev_err(&port->dev, "%s(): usb_submit_urb() failed"
  244. " with error %d\n", __func__, result);
  245. }
  246. }
  247. static void send_data(struct work_struct *work)
  248. {
  249. struct oti6858_private *priv = container_of(work,
  250. struct oti6858_private, delayed_write_work.work);
  251. struct usb_serial_port *port = priv->port;
  252. int count = 0, result;
  253. unsigned long flags;
  254. u8 *allow;
  255. dbg("%s(port = %d)", __func__, port->number);
  256. spin_lock_irqsave(&priv->lock, flags);
  257. if (priv->flags.write_urb_in_use) {
  258. spin_unlock_irqrestore(&priv->lock, flags);
  259. schedule_delayed_work(&priv->delayed_write_work,
  260. msecs_to_jiffies(2));
  261. return;
  262. }
  263. priv->flags.write_urb_in_use = 1;
  264. spin_unlock_irqrestore(&priv->lock, flags);
  265. spin_lock_irqsave(&port->lock, flags);
  266. count = kfifo_len(&port->write_fifo);
  267. spin_unlock_irqrestore(&port->lock, flags);
  268. if (count > port->bulk_out_size)
  269. count = port->bulk_out_size;
  270. if (count != 0) {
  271. allow = kmalloc(1, GFP_KERNEL);
  272. if (!allow) {
  273. dev_err(&port->dev, "%s(): kmalloc failed\n",
  274. __func__);
  275. return;
  276. }
  277. result = usb_control_msg(port->serial->dev,
  278. usb_rcvctrlpipe(port->serial->dev, 0),
  279. OTI6858_REQ_T_CHECK_TXBUFF,
  280. OTI6858_REQ_CHECK_TXBUFF,
  281. count, 0, allow, 1, 100);
  282. if (result != 1 || *allow != 0)
  283. count = 0;
  284. kfree(allow);
  285. }
  286. if (count == 0) {
  287. priv->flags.write_urb_in_use = 0;
  288. dbg("%s(): submitting interrupt urb", __func__);
  289. port->interrupt_in_urb->dev = port->serial->dev;
  290. result = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO);
  291. if (result != 0) {
  292. dev_err(&port->dev, "%s(): usb_submit_urb() failed"
  293. " with error %d\n", __func__, result);
  294. }
  295. return;
  296. }
  297. count = kfifo_out_locked(&port->write_fifo,
  298. port->write_urb->transfer_buffer,
  299. count, &port->lock);
  300. port->write_urb->transfer_buffer_length = count;
  301. port->write_urb->dev = port->serial->dev;
  302. result = usb_submit_urb(port->write_urb, GFP_NOIO);
  303. if (result != 0) {
  304. dev_err(&port->dev, "%s(): usb_submit_urb() failed"
  305. " with error %d\n", __func__, result);
  306. priv->flags.write_urb_in_use = 0;
  307. }
  308. usb_serial_port_softint(port);
  309. }
  310. static int oti6858_startup(struct usb_serial *serial)
  311. {
  312. struct usb_serial_port *port = serial->port[0];
  313. struct oti6858_private *priv;
  314. int i;
  315. for (i = 0; i < serial->num_ports; ++i) {
  316. priv = kzalloc(sizeof(struct oti6858_private), GFP_KERNEL);
  317. if (!priv)
  318. break;
  319. spin_lock_init(&priv->lock);
  320. init_waitqueue_head(&priv->intr_wait);
  321. /* INIT_WORK(&priv->setup_work, setup_line, serial->port[i]); */
  322. /* INIT_WORK(&priv->write_work, send_data, serial->port[i]); */
  323. priv->port = port;
  324. INIT_DELAYED_WORK(&priv->delayed_setup_work, setup_line);
  325. INIT_DELAYED_WORK(&priv->delayed_write_work, send_data);
  326. usb_set_serial_port_data(serial->port[i], priv);
  327. }
  328. if (i == serial->num_ports)
  329. return 0;
  330. for (--i; i >= 0; --i) {
  331. priv = usb_get_serial_port_data(serial->port[i]);
  332. kfree(priv);
  333. usb_set_serial_port_data(serial->port[i], NULL);
  334. }
  335. return -ENOMEM;
  336. }
  337. static int oti6858_write(struct tty_struct *tty, struct usb_serial_port *port,
  338. const unsigned char *buf, int count)
  339. {
  340. dbg("%s(port = %d, count = %d)", __func__, port->number, count);
  341. if (!count)
  342. return count;
  343. count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock);
  344. return count;
  345. }
  346. static int oti6858_write_room(struct tty_struct *tty)
  347. {
  348. struct usb_serial_port *port = tty->driver_data;
  349. int room = 0;
  350. unsigned long flags;
  351. dbg("%s(port = %d)", __func__, port->number);
  352. spin_lock_irqsave(&port->lock, flags);
  353. room = kfifo_avail(&port->write_fifo);
  354. spin_unlock_irqrestore(&port->lock, flags);
  355. return room;
  356. }
  357. static int oti6858_chars_in_buffer(struct tty_struct *tty)
  358. {
  359. struct usb_serial_port *port = tty->driver_data;
  360. int chars = 0;
  361. unsigned long flags;
  362. dbg("%s(port = %d)", __func__, port->number);
  363. spin_lock_irqsave(&port->lock, flags);
  364. chars = kfifo_len(&port->write_fifo);
  365. spin_unlock_irqrestore(&port->lock, flags);
  366. return chars;
  367. }
  368. static void oti6858_init_termios(struct tty_struct *tty)
  369. {
  370. *(tty->termios) = tty_std_termios;
  371. tty->termios->c_cflag = B38400 | CS8 | CREAD | HUPCL | CLOCAL;
  372. tty->termios->c_ispeed = 38400;
  373. tty->termios->c_ospeed = 38400;
  374. }
  375. static void oti6858_set_termios(struct tty_struct *tty,
  376. struct usb_serial_port *port, struct ktermios *old_termios)
  377. {
  378. struct oti6858_private *priv = usb_get_serial_port_data(port);
  379. unsigned long flags;
  380. unsigned int cflag;
  381. u8 frame_fmt, control;
  382. __le16 divisor;
  383. int br;
  384. dbg("%s(port = %d)", __func__, port->number);
  385. if (!tty) {
  386. dbg("%s(): no tty structures", __func__);
  387. return;
  388. }
  389. cflag = tty->termios->c_cflag;
  390. spin_lock_irqsave(&priv->lock, flags);
  391. divisor = priv->pending_setup.divisor;
  392. frame_fmt = priv->pending_setup.frame_fmt;
  393. control = priv->pending_setup.control;
  394. spin_unlock_irqrestore(&priv->lock, flags);
  395. frame_fmt &= ~FMT_DATA_BITS_MASK;
  396. switch (cflag & CSIZE) {
  397. case CS5:
  398. frame_fmt |= FMT_DATA_BITS_5;
  399. break;
  400. case CS6:
  401. frame_fmt |= FMT_DATA_BITS_6;
  402. break;
  403. case CS7:
  404. frame_fmt |= FMT_DATA_BITS_7;
  405. break;
  406. default:
  407. case CS8:
  408. frame_fmt |= FMT_DATA_BITS_8;
  409. break;
  410. }
  411. /* manufacturer claims that this device can work with baud rates
  412. * up to 3 Mbps; I've tested it only on 115200 bps, so I can't
  413. * guarantee that any other baud rate will work (especially
  414. * the higher ones)
  415. */
  416. br = tty_get_baud_rate(tty);
  417. if (br == 0) {
  418. divisor = 0;
  419. } else {
  420. int real_br;
  421. int new_divisor;
  422. br = min(br, OTI6858_MAX_BAUD_RATE);
  423. new_divisor = (96000000 + 8 * br) / (16 * br);
  424. real_br = 96000000 / (16 * new_divisor);
  425. divisor = cpu_to_le16(new_divisor);
  426. tty_encode_baud_rate(tty, real_br, real_br);
  427. }
  428. frame_fmt &= ~FMT_STOP_BITS_MASK;
  429. if ((cflag & CSTOPB) != 0)
  430. frame_fmt |= FMT_STOP_BITS_2;
  431. else
  432. frame_fmt |= FMT_STOP_BITS_1;
  433. frame_fmt &= ~FMT_PARITY_MASK;
  434. if ((cflag & PARENB) != 0) {
  435. if ((cflag & PARODD) != 0)
  436. frame_fmt |= FMT_PARITY_ODD;
  437. else
  438. frame_fmt |= FMT_PARITY_EVEN;
  439. } else {
  440. frame_fmt |= FMT_PARITY_NONE;
  441. }
  442. control &= ~CONTROL_MASK;
  443. if ((cflag & CRTSCTS) != 0)
  444. control |= (CONTROL_DTR_HIGH | CONTROL_RTS_HIGH);
  445. /* change control lines if we are switching to or from B0 */
  446. /* FIXME:
  447. spin_lock_irqsave(&priv->lock, flags);
  448. control = priv->line_control;
  449. if ((cflag & CBAUD) == B0)
  450. priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
  451. else
  452. priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
  453. if (control != priv->line_control) {
  454. control = priv->line_control;
  455. spin_unlock_irqrestore(&priv->lock, flags);
  456. set_control_lines(serial->dev, control);
  457. } else {
  458. spin_unlock_irqrestore(&priv->lock, flags);
  459. }
  460. */
  461. spin_lock_irqsave(&priv->lock, flags);
  462. if (divisor != priv->pending_setup.divisor
  463. || control != priv->pending_setup.control
  464. || frame_fmt != priv->pending_setup.frame_fmt) {
  465. priv->pending_setup.divisor = divisor;
  466. priv->pending_setup.control = control;
  467. priv->pending_setup.frame_fmt = frame_fmt;
  468. }
  469. spin_unlock_irqrestore(&priv->lock, flags);
  470. }
  471. static int oti6858_open(struct tty_struct *tty, struct usb_serial_port *port)
  472. {
  473. struct oti6858_private *priv = usb_get_serial_port_data(port);
  474. struct ktermios tmp_termios;
  475. struct usb_serial *serial = port->serial;
  476. struct oti6858_control_pkt *buf;
  477. unsigned long flags;
  478. int result;
  479. dbg("%s(port = %d)", __func__, port->number);
  480. usb_clear_halt(serial->dev, port->write_urb->pipe);
  481. usb_clear_halt(serial->dev, port->read_urb->pipe);
  482. buf = kmalloc(OTI6858_CTRL_PKT_SIZE, GFP_KERNEL);
  483. if (buf == NULL) {
  484. dev_err(&port->dev, "%s(): out of memory!\n", __func__);
  485. return -ENOMEM;
  486. }
  487. result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  488. OTI6858_REQ_T_GET_STATUS,
  489. OTI6858_REQ_GET_STATUS,
  490. 0, 0,
  491. buf, OTI6858_CTRL_PKT_SIZE,
  492. 100);
  493. if (result != OTI6858_CTRL_PKT_SIZE) {
  494. /* assume default (after power-on reset) values */
  495. buf->divisor = cpu_to_le16(0x009c); /* 38400 bps */
  496. buf->frame_fmt = 0x03; /* 8N1 */
  497. buf->something = 0x43;
  498. buf->control = 0x4c; /* DTR, RTS */
  499. buf->tx_status = 0x00;
  500. buf->pin_state = 0x5b; /* RTS, CTS, DSR, DTR, RI, DCD */
  501. buf->rx_bytes_avail = 0x00;
  502. }
  503. spin_lock_irqsave(&priv->lock, flags);
  504. memcpy(&priv->status, buf, OTI6858_CTRL_PKT_SIZE);
  505. priv->pending_setup.divisor = buf->divisor;
  506. priv->pending_setup.frame_fmt = buf->frame_fmt;
  507. priv->pending_setup.control = buf->control;
  508. spin_unlock_irqrestore(&priv->lock, flags);
  509. kfree(buf);
  510. dbg("%s(): submitting interrupt urb", __func__);
  511. port->interrupt_in_urb->dev = serial->dev;
  512. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  513. if (result != 0) {
  514. dev_err(&port->dev, "%s(): usb_submit_urb() failed"
  515. " with error %d\n", __func__, result);
  516. oti6858_close(port);
  517. return -EPROTO;
  518. }
  519. /* setup termios */
  520. if (tty)
  521. oti6858_set_termios(tty, port, &tmp_termios);
  522. port->port.drain_delay = 256; /* FIXME: check the FIFO length */
  523. return 0;
  524. }
  525. static void oti6858_close(struct usb_serial_port *port)
  526. {
  527. struct oti6858_private *priv = usb_get_serial_port_data(port);
  528. unsigned long flags;
  529. dbg("%s(port = %d)", __func__, port->number);
  530. spin_lock_irqsave(&port->lock, flags);
  531. /* clear out any remaining data in the buffer */
  532. kfifo_reset_out(&port->write_fifo);
  533. spin_unlock_irqrestore(&port->lock, flags);
  534. dbg("%s(): after buf_clear()", __func__);
  535. /* cancel scheduled setup */
  536. cancel_delayed_work_sync(&priv->delayed_setup_work);
  537. cancel_delayed_work_sync(&priv->delayed_write_work);
  538. /* shutdown our urbs */
  539. dbg("%s(): shutting down urbs", __func__);
  540. usb_kill_urb(port->write_urb);
  541. usb_kill_urb(port->read_urb);
  542. usb_kill_urb(port->interrupt_in_urb);
  543. }
  544. static int oti6858_tiocmset(struct tty_struct *tty,
  545. unsigned int set, unsigned int clear)
  546. {
  547. struct usb_serial_port *port = tty->driver_data;
  548. struct oti6858_private *priv = usb_get_serial_port_data(port);
  549. unsigned long flags;
  550. u8 control;
  551. dbg("%s(port = %d, set = 0x%08x, clear = 0x%08x)",
  552. __func__, port->number, set, clear);
  553. if (!usb_get_intfdata(port->serial->interface))
  554. return -ENODEV;
  555. /* FIXME: check if this is correct (active high/low) */
  556. spin_lock_irqsave(&priv->lock, flags);
  557. control = priv->pending_setup.control;
  558. if ((set & TIOCM_RTS) != 0)
  559. control |= CONTROL_RTS_HIGH;
  560. if ((set & TIOCM_DTR) != 0)
  561. control |= CONTROL_DTR_HIGH;
  562. if ((clear & TIOCM_RTS) != 0)
  563. control &= ~CONTROL_RTS_HIGH;
  564. if ((clear & TIOCM_DTR) != 0)
  565. control &= ~CONTROL_DTR_HIGH;
  566. if (control != priv->pending_setup.control)
  567. priv->pending_setup.control = control;
  568. spin_unlock_irqrestore(&priv->lock, flags);
  569. return 0;
  570. }
  571. static int oti6858_tiocmget(struct tty_struct *tty)
  572. {
  573. struct usb_serial_port *port = tty->driver_data;
  574. struct oti6858_private *priv = usb_get_serial_port_data(port);
  575. unsigned long flags;
  576. unsigned pin_state;
  577. unsigned result = 0;
  578. dbg("%s(port = %d)", __func__, port->number);
  579. if (!usb_get_intfdata(port->serial->interface))
  580. return -ENODEV;
  581. spin_lock_irqsave(&priv->lock, flags);
  582. pin_state = priv->status.pin_state & PIN_MASK;
  583. spin_unlock_irqrestore(&priv->lock, flags);
  584. /* FIXME: check if this is correct (active high/low) */
  585. if ((pin_state & PIN_RTS) != 0)
  586. result |= TIOCM_RTS;
  587. if ((pin_state & PIN_CTS) != 0)
  588. result |= TIOCM_CTS;
  589. if ((pin_state & PIN_DSR) != 0)
  590. result |= TIOCM_DSR;
  591. if ((pin_state & PIN_DTR) != 0)
  592. result |= TIOCM_DTR;
  593. if ((pin_state & PIN_RI) != 0)
  594. result |= TIOCM_RI;
  595. if ((pin_state & PIN_DCD) != 0)
  596. result |= TIOCM_CD;
  597. dbg("%s() = 0x%08x", __func__, result);
  598. return result;
  599. }
  600. static int wait_modem_info(struct usb_serial_port *port, unsigned int arg)
  601. {
  602. struct oti6858_private *priv = usb_get_serial_port_data(port);
  603. unsigned long flags;
  604. unsigned int prev, status;
  605. unsigned int changed;
  606. spin_lock_irqsave(&priv->lock, flags);
  607. prev = priv->status.pin_state;
  608. spin_unlock_irqrestore(&priv->lock, flags);
  609. while (1) {
  610. wait_event_interruptible(priv->intr_wait,
  611. priv->status.pin_state != prev);
  612. if (signal_pending(current))
  613. return -ERESTARTSYS;
  614. spin_lock_irqsave(&priv->lock, flags);
  615. status = priv->status.pin_state & PIN_MASK;
  616. spin_unlock_irqrestore(&priv->lock, flags);
  617. changed = prev ^ status;
  618. /* FIXME: check if this is correct (active high/low) */
  619. if (((arg & TIOCM_RNG) && (changed & PIN_RI)) ||
  620. ((arg & TIOCM_DSR) && (changed & PIN_DSR)) ||
  621. ((arg & TIOCM_CD) && (changed & PIN_DCD)) ||
  622. ((arg & TIOCM_CTS) && (changed & PIN_CTS)))
  623. return 0;
  624. prev = status;
  625. }
  626. /* NOTREACHED */
  627. return 0;
  628. }
  629. static int oti6858_ioctl(struct tty_struct *tty,
  630. unsigned int cmd, unsigned long arg)
  631. {
  632. struct usb_serial_port *port = tty->driver_data;
  633. dbg("%s(port = %d, cmd = 0x%04x, arg = 0x%08lx)",
  634. __func__, port->number, cmd, arg);
  635. switch (cmd) {
  636. case TIOCMIWAIT:
  637. dbg("%s(): TIOCMIWAIT", __func__);
  638. return wait_modem_info(port, arg);
  639. default:
  640. dbg("%s(): 0x%04x not supported", __func__, cmd);
  641. break;
  642. }
  643. return -ENOIOCTLCMD;
  644. }
  645. static void oti6858_release(struct usb_serial *serial)
  646. {
  647. int i;
  648. dbg("%s()", __func__);
  649. for (i = 0; i < serial->num_ports; ++i)
  650. kfree(usb_get_serial_port_data(serial->port[i]));
  651. }
  652. static void oti6858_read_int_callback(struct urb *urb)
  653. {
  654. struct usb_serial_port *port = urb->context;
  655. struct oti6858_private *priv = usb_get_serial_port_data(port);
  656. int transient = 0, can_recv = 0, resubmit = 1;
  657. int status = urb->status;
  658. dbg("%s(port = %d, status = %d)",
  659. __func__, port->number, status);
  660. switch (status) {
  661. case 0:
  662. /* success */
  663. break;
  664. case -ECONNRESET:
  665. case -ENOENT:
  666. case -ESHUTDOWN:
  667. /* this urb is terminated, clean up */
  668. dbg("%s(): urb shutting down with status: %d",
  669. __func__, status);
  670. return;
  671. default:
  672. dbg("%s(): nonzero urb status received: %d",
  673. __func__, status);
  674. break;
  675. }
  676. if (status == 0 && urb->actual_length == OTI6858_CTRL_PKT_SIZE) {
  677. struct oti6858_control_pkt *xs = urb->transfer_buffer;
  678. unsigned long flags;
  679. spin_lock_irqsave(&priv->lock, flags);
  680. if (!priv->transient) {
  681. if (!OTI6858_CTRL_EQUALS_PENDING(xs, priv)) {
  682. if (xs->rx_bytes_avail == 0) {
  683. priv->transient = 4;
  684. priv->setup_done = 0;
  685. resubmit = 0;
  686. dbg("%s(): scheduling setup_line()",
  687. __func__);
  688. schedule_delayed_work(&priv->delayed_setup_work, 0);
  689. }
  690. }
  691. } else {
  692. if (OTI6858_CTRL_EQUALS_PENDING(xs, priv)) {
  693. priv->transient = 0;
  694. } else if (!priv->setup_done) {
  695. resubmit = 0;
  696. } else if (--priv->transient == 0) {
  697. if (xs->rx_bytes_avail == 0) {
  698. priv->transient = 4;
  699. priv->setup_done = 0;
  700. resubmit = 0;
  701. dbg("%s(): scheduling setup_line()",
  702. __func__);
  703. schedule_delayed_work(&priv->delayed_setup_work, 0);
  704. }
  705. }
  706. }
  707. if (!priv->transient) {
  708. if (xs->pin_state != priv->status.pin_state)
  709. wake_up_interruptible(&priv->intr_wait);
  710. memcpy(&priv->status, xs, OTI6858_CTRL_PKT_SIZE);
  711. }
  712. if (!priv->transient && xs->rx_bytes_avail != 0) {
  713. can_recv = xs->rx_bytes_avail;
  714. priv->flags.read_urb_in_use = 1;
  715. }
  716. transient = priv->transient;
  717. spin_unlock_irqrestore(&priv->lock, flags);
  718. }
  719. if (can_recv) {
  720. int result;
  721. port->read_urb->dev = port->serial->dev;
  722. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  723. if (result != 0) {
  724. priv->flags.read_urb_in_use = 0;
  725. dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
  726. " error %d\n", __func__, result);
  727. } else {
  728. resubmit = 0;
  729. }
  730. } else if (!transient) {
  731. unsigned long flags;
  732. int count;
  733. spin_lock_irqsave(&port->lock, flags);
  734. count = kfifo_len(&port->write_fifo);
  735. spin_unlock_irqrestore(&port->lock, flags);
  736. spin_lock_irqsave(&priv->lock, flags);
  737. if (priv->flags.write_urb_in_use == 0 && count != 0) {
  738. schedule_delayed_work(&priv->delayed_write_work, 0);
  739. resubmit = 0;
  740. }
  741. spin_unlock_irqrestore(&priv->lock, flags);
  742. }
  743. if (resubmit) {
  744. int result;
  745. /* dbg("%s(): submitting interrupt urb", __func__); */
  746. urb->dev = port->serial->dev;
  747. result = usb_submit_urb(urb, GFP_ATOMIC);
  748. if (result != 0) {
  749. dev_err(&urb->dev->dev,
  750. "%s(): usb_submit_urb() failed with"
  751. " error %d\n", __func__, result);
  752. }
  753. }
  754. }
  755. static void oti6858_read_bulk_callback(struct urb *urb)
  756. {
  757. struct usb_serial_port *port = urb->context;
  758. struct oti6858_private *priv = usb_get_serial_port_data(port);
  759. struct tty_struct *tty;
  760. unsigned char *data = urb->transfer_buffer;
  761. unsigned long flags;
  762. int status = urb->status;
  763. int result;
  764. dbg("%s(port = %d, status = %d)",
  765. __func__, port->number, status);
  766. spin_lock_irqsave(&priv->lock, flags);
  767. priv->flags.read_urb_in_use = 0;
  768. spin_unlock_irqrestore(&priv->lock, flags);
  769. if (status != 0) {
  770. /*
  771. if (status == -EPROTO) {
  772. * PL2303 mysteriously fails with -EPROTO reschedule
  773. the read *
  774. dbg("%s - caught -EPROTO, resubmitting the urb",
  775. __func__);
  776. result = usb_submit_urb(urb, GFP_ATOMIC);
  777. if (result)
  778. dev_err(&urb->dev->dev, "%s - failed resubmitting read urb, error %d\n", __func__, result);
  779. return;
  780. }
  781. */
  782. dbg("%s(): unable to handle the error, exiting", __func__);
  783. return;
  784. }
  785. tty = tty_port_tty_get(&port->port);
  786. if (tty != NULL && urb->actual_length > 0) {
  787. tty_insert_flip_string(tty, data, urb->actual_length);
  788. tty_flip_buffer_push(tty);
  789. }
  790. tty_kref_put(tty);
  791. /* schedule the interrupt urb */
  792. port->interrupt_in_urb->dev = port->serial->dev;
  793. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  794. if (result != 0 && result != -EPERM) {
  795. dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
  796. " error %d\n", __func__, result);
  797. }
  798. }
  799. static void oti6858_write_bulk_callback(struct urb *urb)
  800. {
  801. struct usb_serial_port *port = urb->context;
  802. struct oti6858_private *priv = usb_get_serial_port_data(port);
  803. int status = urb->status;
  804. int result;
  805. dbg("%s(port = %d, status = %d)",
  806. __func__, port->number, status);
  807. switch (status) {
  808. case 0:
  809. /* success */
  810. break;
  811. case -ECONNRESET:
  812. case -ENOENT:
  813. case -ESHUTDOWN:
  814. /* this urb is terminated, clean up */
  815. dbg("%s(): urb shutting down with status: %d",
  816. __func__, status);
  817. priv->flags.write_urb_in_use = 0;
  818. return;
  819. default:
  820. /* error in the urb, so we have to resubmit it */
  821. dbg("%s(): nonzero write bulk status received: %d",
  822. __func__, status);
  823. dbg("%s(): overflow in write", __func__);
  824. port->write_urb->transfer_buffer_length = 1;
  825. port->write_urb->dev = port->serial->dev;
  826. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  827. if (result) {
  828. dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
  829. " error %d\n", __func__, result);
  830. } else {
  831. return;
  832. }
  833. }
  834. priv->flags.write_urb_in_use = 0;
  835. /* schedule the interrupt urb if we are still open */
  836. port->interrupt_in_urb->dev = port->serial->dev;
  837. dbg("%s(): submitting interrupt urb", __func__);
  838. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  839. if (result != 0) {
  840. dev_err(&port->dev, "%s(): failed submitting int urb,"
  841. " error %d\n", __func__, result);
  842. }
  843. }
  844. /* module description and (de)initialization */
  845. static int __init oti6858_init(void)
  846. {
  847. int retval;
  848. retval = usb_serial_register(&oti6858_device);
  849. if (retval == 0) {
  850. retval = usb_register(&oti6858_driver);
  851. if (retval)
  852. usb_serial_deregister(&oti6858_device);
  853. }
  854. return retval;
  855. }
  856. static void __exit oti6858_exit(void)
  857. {
  858. usb_deregister(&oti6858_driver);
  859. usb_serial_deregister(&oti6858_device);
  860. }
  861. module_init(oti6858_init);
  862. module_exit(oti6858_exit);
  863. MODULE_DESCRIPTION(OTI6858_DESCRIPTION);
  864. MODULE_AUTHOR(OTI6858_AUTHOR);
  865. MODULE_VERSION(OTI6858_VERSION);
  866. MODULE_LICENSE("GPL");
  867. module_param(debug, bool, S_IRUGO | S_IWUSR);
  868. MODULE_PARM_DESC(debug, "enable debug output");