oti6858.c 32 KB

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