f81232.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * Fintek F81232 USB to serial adaptor driver
  3. *
  4. * Copyright (C) 2012 Greg Kroah-Hartman (gregkh@linuxfoundation.org)
  5. * Copyright (C) 2012 Linux Foundation
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published by
  9. * the Free Software Foundation.
  10. *
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/errno.h>
  14. #include <linux/init.h>
  15. #include <linux/slab.h>
  16. #include <linux/tty.h>
  17. #include <linux/tty_driver.h>
  18. #include <linux/tty_flip.h>
  19. #include <linux/serial.h>
  20. #include <linux/module.h>
  21. #include <linux/moduleparam.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/usb.h>
  25. #include <linux/usb/serial.h>
  26. static bool debug;
  27. static const struct usb_device_id id_table[] = {
  28. { USB_DEVICE(0x1934, 0x0706) },
  29. { } /* Terminating entry */
  30. };
  31. MODULE_DEVICE_TABLE(usb, id_table);
  32. #define CONTROL_DTR 0x01
  33. #define CONTROL_RTS 0x02
  34. #define UART_STATE 0x08
  35. #define UART_STATE_TRANSIENT_MASK 0x74
  36. #define UART_DCD 0x01
  37. #define UART_DSR 0x02
  38. #define UART_BREAK_ERROR 0x04
  39. #define UART_RING 0x08
  40. #define UART_FRAME_ERROR 0x10
  41. #define UART_PARITY_ERROR 0x20
  42. #define UART_OVERRUN_ERROR 0x40
  43. #define UART_CTS 0x80
  44. struct f81232_private {
  45. spinlock_t lock;
  46. wait_queue_head_t delta_msr_wait;
  47. u8 line_control;
  48. u8 line_status;
  49. };
  50. static void f81232_update_line_status(struct usb_serial_port *port,
  51. unsigned char *data,
  52. unsigned int actual_length)
  53. {
  54. }
  55. static void f81232_read_int_callback(struct urb *urb)
  56. {
  57. struct usb_serial_port *port = urb->context;
  58. unsigned char *data = urb->transfer_buffer;
  59. unsigned int actual_length = urb->actual_length;
  60. int status = urb->status;
  61. int retval;
  62. dbg("%s (%d)", __func__, port->number);
  63. switch (status) {
  64. case 0:
  65. /* success */
  66. break;
  67. case -ECONNRESET:
  68. case -ENOENT:
  69. case -ESHUTDOWN:
  70. /* this urb is terminated, clean up */
  71. dbg("%s - urb shutting down with status: %d", __func__,
  72. status);
  73. return;
  74. default:
  75. dbg("%s - nonzero urb status received: %d", __func__,
  76. status);
  77. goto exit;
  78. }
  79. usb_serial_debug_data(debug, &port->dev, __func__,
  80. urb->actual_length, urb->transfer_buffer);
  81. f81232_update_line_status(port, data, actual_length);
  82. exit:
  83. retval = usb_submit_urb(urb, GFP_ATOMIC);
  84. if (retval)
  85. dev_err(&urb->dev->dev,
  86. "%s - usb_submit_urb failed with result %d\n",
  87. __func__, retval);
  88. }
  89. static void f81232_process_read_urb(struct urb *urb)
  90. {
  91. struct usb_serial_port *port = urb->context;
  92. struct f81232_private *priv = usb_get_serial_port_data(port);
  93. struct tty_struct *tty;
  94. unsigned char *data = urb->transfer_buffer;
  95. char tty_flag = TTY_NORMAL;
  96. unsigned long flags;
  97. u8 line_status;
  98. int i;
  99. /* update line status */
  100. spin_lock_irqsave(&priv->lock, flags);
  101. line_status = priv->line_status;
  102. priv->line_status &= ~UART_STATE_TRANSIENT_MASK;
  103. spin_unlock_irqrestore(&priv->lock, flags);
  104. wake_up_interruptible(&priv->delta_msr_wait);
  105. if (!urb->actual_length)
  106. return;
  107. tty = tty_port_tty_get(&port->port);
  108. if (!tty)
  109. return;
  110. /* break takes precedence over parity, */
  111. /* which takes precedence over framing errors */
  112. if (line_status & UART_BREAK_ERROR)
  113. tty_flag = TTY_BREAK;
  114. else if (line_status & UART_PARITY_ERROR)
  115. tty_flag = TTY_PARITY;
  116. else if (line_status & UART_FRAME_ERROR)
  117. tty_flag = TTY_FRAME;
  118. dbg("%s - tty_flag = %d", __func__, tty_flag);
  119. /* overrun is special, not associated with a char */
  120. if (line_status & UART_OVERRUN_ERROR)
  121. tty_insert_flip_char(tty, 0, TTY_OVERRUN);
  122. if (port->port.console && port->sysrq) {
  123. for (i = 0; i < urb->actual_length; ++i)
  124. if (!usb_serial_handle_sysrq_char(port, data[i]))
  125. tty_insert_flip_char(tty, data[i], tty_flag);
  126. } else {
  127. tty_insert_flip_string_fixed_flag(tty, data, tty_flag,
  128. urb->actual_length);
  129. }
  130. tty_flip_buffer_push(tty);
  131. tty_kref_put(tty);
  132. }
  133. static int set_control_lines(struct usb_device *dev, u8 value)
  134. {
  135. /* FIXME - Stubbed out for now */
  136. return 0;
  137. }
  138. static void f81232_break_ctl(struct tty_struct *tty, int break_state)
  139. {
  140. /* FIXME - Stubbed out for now */
  141. /*
  142. * break_state = -1 to turn on break, and 0 to turn off break
  143. * see drivers/char/tty_io.c to see it used.
  144. * last_set_data_urb_value NEVER has the break bit set in it.
  145. */
  146. }
  147. static void f81232_set_termios(struct tty_struct *tty,
  148. struct usb_serial_port *port, struct ktermios *old_termios)
  149. {
  150. /* FIXME - Stubbed out for now */
  151. /* Don't change anything if nothing has changed */
  152. if (!tty_termios_hw_change(tty->termios, old_termios))
  153. return;
  154. /* Do the real work here... */
  155. }
  156. static int f81232_tiocmget(struct tty_struct *tty)
  157. {
  158. /* FIXME - Stubbed out for now */
  159. return 0;
  160. }
  161. static int f81232_tiocmset(struct tty_struct *tty,
  162. unsigned int set, unsigned int clear)
  163. {
  164. /* FIXME - Stubbed out for now */
  165. return 0;
  166. }
  167. static int f81232_open(struct tty_struct *tty, struct usb_serial_port *port)
  168. {
  169. struct ktermios tmp_termios;
  170. int result;
  171. /* Setup termios */
  172. if (tty)
  173. f81232_set_termios(tty, port, &tmp_termios);
  174. dbg("%s - submitting interrupt urb", __func__);
  175. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  176. if (result) {
  177. dev_err(&port->dev, "%s - failed submitting interrupt urb,"
  178. " error %d\n", __func__, result);
  179. return result;
  180. }
  181. result = usb_serial_generic_open(tty, port);
  182. if (result) {
  183. usb_kill_urb(port->interrupt_in_urb);
  184. return result;
  185. }
  186. port->port.drain_delay = 256;
  187. return 0;
  188. }
  189. static void f81232_close(struct usb_serial_port *port)
  190. {
  191. usb_serial_generic_close(port);
  192. usb_kill_urb(port->interrupt_in_urb);
  193. }
  194. static void f81232_dtr_rts(struct usb_serial_port *port, int on)
  195. {
  196. struct f81232_private *priv = usb_get_serial_port_data(port);
  197. unsigned long flags;
  198. u8 control;
  199. spin_lock_irqsave(&priv->lock, flags);
  200. /* Change DTR and RTS */
  201. if (on)
  202. priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
  203. else
  204. priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
  205. control = priv->line_control;
  206. spin_unlock_irqrestore(&priv->lock, flags);
  207. set_control_lines(port->serial->dev, control);
  208. }
  209. static int f81232_carrier_raised(struct usb_serial_port *port)
  210. {
  211. struct f81232_private *priv = usb_get_serial_port_data(port);
  212. if (priv->line_status & UART_DCD)
  213. return 1;
  214. return 0;
  215. }
  216. static int wait_modem_info(struct usb_serial_port *port, unsigned int arg)
  217. {
  218. struct f81232_private *priv = usb_get_serial_port_data(port);
  219. unsigned long flags;
  220. unsigned int prevstatus;
  221. unsigned int status;
  222. unsigned int changed;
  223. spin_lock_irqsave(&priv->lock, flags);
  224. prevstatus = priv->line_status;
  225. spin_unlock_irqrestore(&priv->lock, flags);
  226. while (1) {
  227. interruptible_sleep_on(&priv->delta_msr_wait);
  228. /* see if a signal did it */
  229. if (signal_pending(current))
  230. return -ERESTARTSYS;
  231. spin_lock_irqsave(&priv->lock, flags);
  232. status = priv->line_status;
  233. spin_unlock_irqrestore(&priv->lock, flags);
  234. changed = prevstatus ^ status;
  235. if (((arg & TIOCM_RNG) && (changed & UART_RING)) ||
  236. ((arg & TIOCM_DSR) && (changed & UART_DSR)) ||
  237. ((arg & TIOCM_CD) && (changed & UART_DCD)) ||
  238. ((arg & TIOCM_CTS) && (changed & UART_CTS))) {
  239. return 0;
  240. }
  241. prevstatus = status;
  242. }
  243. /* NOTREACHED */
  244. return 0;
  245. }
  246. static int f81232_ioctl(struct tty_struct *tty,
  247. unsigned int cmd, unsigned long arg)
  248. {
  249. struct serial_struct ser;
  250. struct usb_serial_port *port = tty->driver_data;
  251. dbg("%s (%d) cmd = 0x%04x", __func__, port->number, cmd);
  252. switch (cmd) {
  253. case TIOCGSERIAL:
  254. memset(&ser, 0, sizeof ser);
  255. ser.type = PORT_16654;
  256. ser.line = port->serial->minor;
  257. ser.port = port->number;
  258. ser.baud_base = 460800;
  259. if (copy_to_user((void __user *)arg, &ser, sizeof ser))
  260. return -EFAULT;
  261. return 0;
  262. case TIOCMIWAIT:
  263. dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
  264. return wait_modem_info(port, arg);
  265. default:
  266. dbg("%s not supported = 0x%04x", __func__, cmd);
  267. break;
  268. }
  269. return -ENOIOCTLCMD;
  270. }
  271. static int f81232_startup(struct usb_serial *serial)
  272. {
  273. struct f81232_private *priv;
  274. int i;
  275. for (i = 0; i < serial->num_ports; ++i) {
  276. priv = kzalloc(sizeof(struct f81232_private), GFP_KERNEL);
  277. if (!priv)
  278. goto cleanup;
  279. spin_lock_init(&priv->lock);
  280. init_waitqueue_head(&priv->delta_msr_wait);
  281. usb_set_serial_port_data(serial->port[i], priv);
  282. }
  283. return 0;
  284. cleanup:
  285. for (--i; i >= 0; --i) {
  286. priv = usb_get_serial_port_data(serial->port[i]);
  287. kfree(priv);
  288. usb_set_serial_port_data(serial->port[i], NULL);
  289. }
  290. return -ENOMEM;
  291. }
  292. static void f81232_release(struct usb_serial *serial)
  293. {
  294. int i;
  295. struct f81232_private *priv;
  296. for (i = 0; i < serial->num_ports; ++i) {
  297. priv = usb_get_serial_port_data(serial->port[i]);
  298. kfree(priv);
  299. }
  300. }
  301. static struct usb_driver f81232_driver = {
  302. .name = "f81232",
  303. .probe = usb_serial_probe,
  304. .disconnect = usb_serial_disconnect,
  305. .id_table = id_table,
  306. .suspend = usb_serial_suspend,
  307. .resume = usb_serial_resume,
  308. .no_dynamic_id = 1,
  309. .supports_autosuspend = 1,
  310. };
  311. static struct usb_serial_driver f81232_device = {
  312. .driver = {
  313. .owner = THIS_MODULE,
  314. .name = "f81232",
  315. },
  316. .id_table = id_table,
  317. .usb_driver = &f81232_driver,
  318. .num_ports = 1,
  319. .bulk_in_size = 256,
  320. .bulk_out_size = 256,
  321. .open = f81232_open,
  322. .close = f81232_close,
  323. .dtr_rts = f81232_dtr_rts,
  324. .carrier_raised = f81232_carrier_raised,
  325. .ioctl = f81232_ioctl,
  326. .break_ctl = f81232_break_ctl,
  327. .set_termios = f81232_set_termios,
  328. .tiocmget = f81232_tiocmget,
  329. .tiocmset = f81232_tiocmset,
  330. .process_read_urb = f81232_process_read_urb,
  331. .read_int_callback = f81232_read_int_callback,
  332. .attach = f81232_startup,
  333. .release = f81232_release,
  334. };
  335. static struct usb_serial_driver * const serial_drivers[] = {
  336. &f81232_device,
  337. NULL,
  338. };
  339. module_usb_serial_driver(f81232_driver, serial_drivers);
  340. MODULE_DESCRIPTION("Fintek F81232 USB to serial adaptor driver");
  341. MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org");
  342. MODULE_LICENSE("GPL v2");
  343. module_param(debug, bool, S_IRUGO | S_IWUSR);
  344. MODULE_PARM_DESC(debug, "Debug enabled or not");