oti6858.c 28 KB

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