oti6858.c 32 KB

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