oti6858.c 32 KB

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