oti6858.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216
  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. if (port->port.count != 1)
  511. return 0;
  512. buf = kmalloc(OTI6858_CTRL_PKT_SIZE, GFP_KERNEL);
  513. if (buf == NULL) {
  514. dev_err(&port->dev, "%s(): out of memory!\n", __func__);
  515. return -ENOMEM;
  516. }
  517. result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  518. OTI6858_REQ_T_GET_STATUS,
  519. OTI6858_REQ_GET_STATUS,
  520. 0, 0,
  521. buf, OTI6858_CTRL_PKT_SIZE,
  522. 100);
  523. if (result != OTI6858_CTRL_PKT_SIZE) {
  524. /* assume default (after power-on reset) values */
  525. buf->divisor = cpu_to_le16(0x009c); /* 38400 bps */
  526. buf->frame_fmt = 0x03; /* 8N1 */
  527. buf->something = 0x43;
  528. buf->control = 0x4c; /* DTR, RTS */
  529. buf->tx_status = 0x00;
  530. buf->pin_state = 0x5b; /* RTS, CTS, DSR, DTR, RI, DCD */
  531. buf->rx_bytes_avail = 0x00;
  532. }
  533. spin_lock_irqsave(&priv->lock, flags);
  534. memcpy(&priv->status, buf, OTI6858_CTRL_PKT_SIZE);
  535. priv->pending_setup.divisor = buf->divisor;
  536. priv->pending_setup.frame_fmt = buf->frame_fmt;
  537. priv->pending_setup.control = buf->control;
  538. spin_unlock_irqrestore(&priv->lock, flags);
  539. kfree(buf);
  540. dbg("%s(): submitting interrupt urb", __func__);
  541. port->interrupt_in_urb->dev = serial->dev;
  542. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  543. if (result != 0) {
  544. dev_err(&port->dev, "%s(): usb_submit_urb() failed"
  545. " with error %d\n", __func__, result);
  546. oti6858_close(port);
  547. return -EPROTO;
  548. }
  549. /* setup termios */
  550. if (tty)
  551. oti6858_set_termios(tty, port, &tmp_termios);
  552. port->port.drain_delay = 256; /* FIXME: check the FIFO length */
  553. return 0;
  554. }
  555. static void oti6858_close(struct usb_serial_port *port)
  556. {
  557. struct oti6858_private *priv = usb_get_serial_port_data(port);
  558. unsigned long flags;
  559. dbg("%s(port = %d)", __func__, port->number);
  560. spin_lock_irqsave(&priv->lock, flags);
  561. /* clear out any remaining data in the buffer */
  562. oti6858_buf_clear(priv->buf);
  563. spin_unlock_irqrestore(&priv->lock, flags);
  564. dbg("%s(): after buf_clear()", __func__);
  565. /* cancel scheduled setup */
  566. cancel_delayed_work(&priv->delayed_setup_work);
  567. cancel_delayed_work(&priv->delayed_write_work);
  568. flush_scheduled_work();
  569. /* shutdown our urbs */
  570. dbg("%s(): shutting down urbs", __func__);
  571. usb_kill_urb(port->write_urb);
  572. usb_kill_urb(port->read_urb);
  573. usb_kill_urb(port->interrupt_in_urb);
  574. }
  575. static int oti6858_tiocmset(struct tty_struct *tty, struct file *file,
  576. unsigned int set, unsigned int clear)
  577. {
  578. struct usb_serial_port *port = tty->driver_data;
  579. struct oti6858_private *priv = usb_get_serial_port_data(port);
  580. unsigned long flags;
  581. u8 control;
  582. dbg("%s(port = %d, set = 0x%08x, clear = 0x%08x)",
  583. __func__, port->number, set, clear);
  584. if (!usb_get_intfdata(port->serial->interface))
  585. return -ENODEV;
  586. /* FIXME: check if this is correct (active high/low) */
  587. spin_lock_irqsave(&priv->lock, flags);
  588. control = priv->pending_setup.control;
  589. if ((set & TIOCM_RTS) != 0)
  590. control |= CONTROL_RTS_HIGH;
  591. if ((set & TIOCM_DTR) != 0)
  592. control |= CONTROL_DTR_HIGH;
  593. if ((clear & TIOCM_RTS) != 0)
  594. control &= ~CONTROL_RTS_HIGH;
  595. if ((clear & TIOCM_DTR) != 0)
  596. control &= ~CONTROL_DTR_HIGH;
  597. if (control != priv->pending_setup.control)
  598. priv->pending_setup.control = control;
  599. spin_unlock_irqrestore(&priv->lock, flags);
  600. return 0;
  601. }
  602. static int oti6858_tiocmget(struct tty_struct *tty, struct file *file)
  603. {
  604. struct usb_serial_port *port = tty->driver_data;
  605. struct oti6858_private *priv = usb_get_serial_port_data(port);
  606. unsigned long flags;
  607. unsigned pin_state;
  608. unsigned result = 0;
  609. dbg("%s(port = %d)", __func__, port->number);
  610. if (!usb_get_intfdata(port->serial->interface))
  611. return -ENODEV;
  612. spin_lock_irqsave(&priv->lock, flags);
  613. pin_state = priv->status.pin_state & PIN_MASK;
  614. spin_unlock_irqrestore(&priv->lock, flags);
  615. /* FIXME: check if this is correct (active high/low) */
  616. if ((pin_state & PIN_RTS) != 0)
  617. result |= TIOCM_RTS;
  618. if ((pin_state & PIN_CTS) != 0)
  619. result |= TIOCM_CTS;
  620. if ((pin_state & PIN_DSR) != 0)
  621. result |= TIOCM_DSR;
  622. if ((pin_state & PIN_DTR) != 0)
  623. result |= TIOCM_DTR;
  624. if ((pin_state & PIN_RI) != 0)
  625. result |= TIOCM_RI;
  626. if ((pin_state & PIN_DCD) != 0)
  627. result |= TIOCM_CD;
  628. dbg("%s() = 0x%08x", __func__, result);
  629. return result;
  630. }
  631. static int wait_modem_info(struct usb_serial_port *port, unsigned int arg)
  632. {
  633. struct oti6858_private *priv = usb_get_serial_port_data(port);
  634. unsigned long flags;
  635. unsigned int prev, status;
  636. unsigned int changed;
  637. spin_lock_irqsave(&priv->lock, flags);
  638. prev = priv->status.pin_state;
  639. spin_unlock_irqrestore(&priv->lock, flags);
  640. while (1) {
  641. wait_event_interruptible(priv->intr_wait,
  642. priv->status.pin_state != prev);
  643. if (signal_pending(current))
  644. return -ERESTARTSYS;
  645. spin_lock_irqsave(&priv->lock, flags);
  646. status = priv->status.pin_state & PIN_MASK;
  647. spin_unlock_irqrestore(&priv->lock, flags);
  648. changed = prev ^ status;
  649. /* FIXME: check if this is correct (active high/low) */
  650. if (((arg & TIOCM_RNG) && (changed & PIN_RI)) ||
  651. ((arg & TIOCM_DSR) && (changed & PIN_DSR)) ||
  652. ((arg & TIOCM_CD) && (changed & PIN_DCD)) ||
  653. ((arg & TIOCM_CTS) && (changed & PIN_CTS)))
  654. return 0;
  655. prev = status;
  656. }
  657. /* NOTREACHED */
  658. return 0;
  659. }
  660. static int oti6858_ioctl(struct tty_struct *tty, struct file *file,
  661. unsigned int cmd, unsigned long arg)
  662. {
  663. struct usb_serial_port *port = tty->driver_data;
  664. dbg("%s(port = %d, cmd = 0x%04x, arg = 0x%08lx)",
  665. __func__, port->number, cmd, arg);
  666. switch (cmd) {
  667. case TIOCMIWAIT:
  668. dbg("%s(): TIOCMIWAIT", __func__);
  669. return wait_modem_info(port, arg);
  670. default:
  671. dbg("%s(): 0x%04x not supported", __func__, cmd);
  672. break;
  673. }
  674. return -ENOIOCTLCMD;
  675. }
  676. static void oti6858_release(struct usb_serial *serial)
  677. {
  678. struct oti6858_private *priv;
  679. int i;
  680. dbg("%s()", __func__);
  681. for (i = 0; i < serial->num_ports; ++i) {
  682. priv = usb_get_serial_port_data(serial->port[i]);
  683. if (priv) {
  684. oti6858_buf_free(priv->buf);
  685. kfree(priv);
  686. }
  687. }
  688. }
  689. static void oti6858_read_int_callback(struct urb *urb)
  690. {
  691. struct usb_serial_port *port = urb->context;
  692. struct oti6858_private *priv = usb_get_serial_port_data(port);
  693. int transient = 0, can_recv = 0, resubmit = 1;
  694. int status = urb->status;
  695. dbg("%s(port = %d, status = %d)",
  696. __func__, port->number, status);
  697. switch (status) {
  698. case 0:
  699. /* success */
  700. break;
  701. case -ECONNRESET:
  702. case -ENOENT:
  703. case -ESHUTDOWN:
  704. /* this urb is terminated, clean up */
  705. dbg("%s(): urb shutting down with status: %d",
  706. __func__, status);
  707. return;
  708. default:
  709. dbg("%s(): nonzero urb status received: %d",
  710. __func__, status);
  711. break;
  712. }
  713. if (status == 0 && urb->actual_length == OTI6858_CTRL_PKT_SIZE) {
  714. struct oti6858_control_pkt *xs = urb->transfer_buffer;
  715. unsigned long flags;
  716. spin_lock_irqsave(&priv->lock, flags);
  717. if (!priv->transient) {
  718. if (!OTI6858_CTRL_EQUALS_PENDING(xs, priv)) {
  719. if (xs->rx_bytes_avail == 0) {
  720. priv->transient = 4;
  721. priv->setup_done = 0;
  722. resubmit = 0;
  723. dbg("%s(): scheduling setup_line()",
  724. __func__);
  725. schedule_delayed_work(&priv->delayed_setup_work, 0);
  726. }
  727. }
  728. } else {
  729. if (OTI6858_CTRL_EQUALS_PENDING(xs, priv)) {
  730. priv->transient = 0;
  731. } else if (!priv->setup_done) {
  732. resubmit = 0;
  733. } else if (--priv->transient == 0) {
  734. if (xs->rx_bytes_avail == 0) {
  735. priv->transient = 4;
  736. priv->setup_done = 0;
  737. resubmit = 0;
  738. dbg("%s(): scheduling setup_line()",
  739. __func__);
  740. schedule_delayed_work(&priv->delayed_setup_work, 0);
  741. }
  742. }
  743. }
  744. if (!priv->transient) {
  745. if (xs->pin_state != priv->status.pin_state)
  746. wake_up_interruptible(&priv->intr_wait);
  747. memcpy(&priv->status, xs, OTI6858_CTRL_PKT_SIZE);
  748. }
  749. if (!priv->transient && xs->rx_bytes_avail != 0) {
  750. can_recv = xs->rx_bytes_avail;
  751. priv->flags.read_urb_in_use = 1;
  752. }
  753. transient = priv->transient;
  754. spin_unlock_irqrestore(&priv->lock, flags);
  755. }
  756. if (can_recv) {
  757. int result;
  758. port->read_urb->dev = port->serial->dev;
  759. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  760. if (result != 0) {
  761. priv->flags.read_urb_in_use = 0;
  762. dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
  763. " error %d\n", __func__, result);
  764. } else {
  765. resubmit = 0;
  766. }
  767. } else if (!transient) {
  768. unsigned long flags;
  769. spin_lock_irqsave(&priv->lock, flags);
  770. if (priv->flags.write_urb_in_use == 0
  771. && oti6858_buf_data_avail(priv->buf) != 0) {
  772. schedule_delayed_work(&priv->delayed_write_work, 0);
  773. resubmit = 0;
  774. }
  775. spin_unlock_irqrestore(&priv->lock, flags);
  776. }
  777. if (resubmit) {
  778. int result;
  779. /* dbg("%s(): submitting interrupt urb", __func__); */
  780. urb->dev = port->serial->dev;
  781. result = usb_submit_urb(urb, GFP_ATOMIC);
  782. if (result != 0) {
  783. dev_err(&urb->dev->dev,
  784. "%s(): usb_submit_urb() failed with"
  785. " error %d\n", __func__, result);
  786. }
  787. }
  788. }
  789. static void oti6858_read_bulk_callback(struct urb *urb)
  790. {
  791. struct usb_serial_port *port = urb->context;
  792. struct oti6858_private *priv = usb_get_serial_port_data(port);
  793. struct tty_struct *tty;
  794. unsigned char *data = urb->transfer_buffer;
  795. unsigned long flags;
  796. int status = urb->status;
  797. int result;
  798. dbg("%s(port = %d, status = %d)",
  799. __func__, port->number, status);
  800. spin_lock_irqsave(&priv->lock, flags);
  801. priv->flags.read_urb_in_use = 0;
  802. spin_unlock_irqrestore(&priv->lock, flags);
  803. if (status != 0) {
  804. if (!port->port.count) {
  805. dbg("%s(): port is closed, exiting", __func__);
  806. return;
  807. }
  808. /*
  809. if (status == -EPROTO) {
  810. * PL2303 mysteriously fails with -EPROTO reschedule
  811. the read *
  812. dbg("%s - caught -EPROTO, resubmitting the urb",
  813. __func__);
  814. result = usb_submit_urb(urb, GFP_ATOMIC);
  815. if (result)
  816. dev_err(&urb->dev->dev, "%s - failed resubmitting read urb, error %d\n", __func__, result);
  817. return;
  818. }
  819. */
  820. dbg("%s(): unable to handle the error, exiting", __func__);
  821. return;
  822. }
  823. tty = tty_port_tty_get(&port->port);
  824. if (tty != NULL && urb->actual_length > 0) {
  825. tty_insert_flip_string(tty, data, urb->actual_length);
  826. tty_flip_buffer_push(tty);
  827. }
  828. tty_kref_put(tty);
  829. /* schedule the interrupt urb if we are still open */
  830. if (port->port.count != 0) {
  831. port->interrupt_in_urb->dev = port->serial->dev;
  832. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  833. if (result != 0) {
  834. dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
  835. " error %d\n", __func__, result);
  836. }
  837. }
  838. }
  839. static void oti6858_write_bulk_callback(struct urb *urb)
  840. {
  841. struct usb_serial_port *port = urb->context;
  842. struct oti6858_private *priv = usb_get_serial_port_data(port);
  843. int status = urb->status;
  844. int result;
  845. dbg("%s(port = %d, status = %d)",
  846. __func__, port->number, status);
  847. switch (status) {
  848. case 0:
  849. /* success */
  850. break;
  851. case -ECONNRESET:
  852. case -ENOENT:
  853. case -ESHUTDOWN:
  854. /* this urb is terminated, clean up */
  855. dbg("%s(): urb shutting down with status: %d",
  856. __func__, status);
  857. priv->flags.write_urb_in_use = 0;
  858. return;
  859. default:
  860. /* error in the urb, so we have to resubmit it */
  861. dbg("%s(): nonzero write bulk status received: %d",
  862. __func__, status);
  863. dbg("%s(): overflow in write", __func__);
  864. port->write_urb->transfer_buffer_length = 1;
  865. port->write_urb->dev = port->serial->dev;
  866. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  867. if (result) {
  868. dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
  869. " error %d\n", __func__, result);
  870. } else {
  871. return;
  872. }
  873. }
  874. priv->flags.write_urb_in_use = 0;
  875. /* schedule the interrupt urb if we are still open */
  876. port->interrupt_in_urb->dev = port->serial->dev;
  877. dbg("%s(): submitting interrupt urb", __func__);
  878. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  879. if (result != 0) {
  880. dev_err(&port->dev, "%s(): failed submitting int urb,"
  881. " error %d\n", __func__, result);
  882. }
  883. }
  884. /*
  885. * oti6858_buf_alloc
  886. *
  887. * Allocate a circular buffer and all associated memory.
  888. */
  889. static struct oti6858_buf *oti6858_buf_alloc(unsigned int size)
  890. {
  891. struct oti6858_buf *pb;
  892. if (size == 0)
  893. return NULL;
  894. pb = kmalloc(sizeof(struct oti6858_buf), GFP_KERNEL);
  895. if (pb == NULL)
  896. return NULL;
  897. pb->buf_buf = kmalloc(size, GFP_KERNEL);
  898. if (pb->buf_buf == NULL) {
  899. kfree(pb);
  900. return NULL;
  901. }
  902. pb->buf_size = size;
  903. pb->buf_get = pb->buf_put = pb->buf_buf;
  904. return pb;
  905. }
  906. /*
  907. * oti6858_buf_free
  908. *
  909. * Free the buffer and all associated memory.
  910. */
  911. static void oti6858_buf_free(struct oti6858_buf *pb)
  912. {
  913. if (pb) {
  914. kfree(pb->buf_buf);
  915. kfree(pb);
  916. }
  917. }
  918. /*
  919. * oti6858_buf_clear
  920. *
  921. * Clear out all data in the circular buffer.
  922. */
  923. static void oti6858_buf_clear(struct oti6858_buf *pb)
  924. {
  925. if (pb != NULL) {
  926. /* equivalent to a get of all data available */
  927. pb->buf_get = pb->buf_put;
  928. }
  929. }
  930. /*
  931. * oti6858_buf_data_avail
  932. *
  933. * Return the number of bytes of data available in the circular
  934. * buffer.
  935. */
  936. static unsigned int oti6858_buf_data_avail(struct oti6858_buf *pb)
  937. {
  938. if (pb == NULL)
  939. return 0;
  940. return (pb->buf_size + pb->buf_put - pb->buf_get) % pb->buf_size;
  941. }
  942. /*
  943. * oti6858_buf_space_avail
  944. *
  945. * Return the number of bytes of space available in the circular
  946. * buffer.
  947. */
  948. static unsigned int oti6858_buf_space_avail(struct oti6858_buf *pb)
  949. {
  950. if (pb == NULL)
  951. return 0;
  952. return (pb->buf_size + pb->buf_get - pb->buf_put - 1) % pb->buf_size;
  953. }
  954. /*
  955. * oti6858_buf_put
  956. *
  957. * Copy data data from a user buffer and put it into the circular buffer.
  958. * Restrict to the amount of space available.
  959. *
  960. * Return the number of bytes copied.
  961. */
  962. static unsigned int oti6858_buf_put(struct oti6858_buf *pb, const char *buf,
  963. unsigned int count)
  964. {
  965. unsigned int len;
  966. if (pb == NULL)
  967. return 0;
  968. len = oti6858_buf_space_avail(pb);
  969. if (count > len)
  970. count = len;
  971. if (count == 0)
  972. return 0;
  973. len = pb->buf_buf + pb->buf_size - pb->buf_put;
  974. if (count > len) {
  975. memcpy(pb->buf_put, buf, len);
  976. memcpy(pb->buf_buf, buf+len, count - len);
  977. pb->buf_put = pb->buf_buf + count - len;
  978. } else {
  979. memcpy(pb->buf_put, buf, count);
  980. if (count < len)
  981. pb->buf_put += count;
  982. else /* count == len */
  983. pb->buf_put = pb->buf_buf;
  984. }
  985. return count;
  986. }
  987. /*
  988. * oti6858_buf_get
  989. *
  990. * Get data from the circular buffer and copy to the given buffer.
  991. * Restrict to the amount of data available.
  992. *
  993. * Return the number of bytes copied.
  994. */
  995. static unsigned int oti6858_buf_get(struct oti6858_buf *pb, char *buf,
  996. unsigned int count)
  997. {
  998. unsigned int len;
  999. if (pb == NULL)
  1000. return 0;
  1001. len = oti6858_buf_data_avail(pb);
  1002. if (count > len)
  1003. count = len;
  1004. if (count == 0)
  1005. return 0;
  1006. len = pb->buf_buf + pb->buf_size - pb->buf_get;
  1007. if (count > len) {
  1008. memcpy(buf, pb->buf_get, len);
  1009. memcpy(buf+len, pb->buf_buf, count - len);
  1010. pb->buf_get = pb->buf_buf + count - len;
  1011. } else {
  1012. memcpy(buf, pb->buf_get, count);
  1013. if (count < len)
  1014. pb->buf_get += count;
  1015. else /* count == len */
  1016. pb->buf_get = pb->buf_buf;
  1017. }
  1018. return count;
  1019. }
  1020. /* module description and (de)initialization */
  1021. static int __init oti6858_init(void)
  1022. {
  1023. int retval;
  1024. retval = usb_serial_register(&oti6858_device);
  1025. if (retval == 0) {
  1026. retval = usb_register(&oti6858_driver);
  1027. if (retval)
  1028. usb_serial_deregister(&oti6858_device);
  1029. }
  1030. return retval;
  1031. }
  1032. static void __exit oti6858_exit(void)
  1033. {
  1034. usb_deregister(&oti6858_driver);
  1035. usb_serial_deregister(&oti6858_device);
  1036. }
  1037. module_init(oti6858_init);
  1038. module_exit(oti6858_exit);
  1039. MODULE_DESCRIPTION(OTI6858_DESCRIPTION);
  1040. MODULE_AUTHOR(OTI6858_AUTHOR);
  1041. MODULE_VERSION(OTI6858_VERSION);
  1042. MODULE_LICENSE("GPL");
  1043. module_param(debug, bool, S_IRUGO | S_IWUSR);
  1044. MODULE_PARM_DESC(debug, "enable debug output");