f81232.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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/slab.h>
  15. #include <linux/tty.h>
  16. #include <linux/tty_driver.h>
  17. #include <linux/tty_flip.h>
  18. #include <linux/serial.h>
  19. #include <linux/module.h>
  20. #include <linux/moduleparam.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/uaccess.h>
  23. #include <linux/usb.h>
  24. #include <linux/usb/serial.h>
  25. static const struct usb_device_id id_table[] = {
  26. { USB_DEVICE(0x1934, 0x0706) },
  27. { } /* Terminating entry */
  28. };
  29. MODULE_DEVICE_TABLE(usb, id_table);
  30. #define CONTROL_DTR 0x01
  31. #define CONTROL_RTS 0x02
  32. #define UART_STATE 0x08
  33. #define UART_STATE_TRANSIENT_MASK 0x74
  34. #define UART_DCD 0x01
  35. #define UART_DSR 0x02
  36. #define UART_BREAK_ERROR 0x04
  37. #define UART_RING 0x08
  38. #define UART_FRAME_ERROR 0x10
  39. #define UART_PARITY_ERROR 0x20
  40. #define UART_OVERRUN_ERROR 0x40
  41. #define UART_CTS 0x80
  42. struct f81232_private {
  43. spinlock_t lock;
  44. u8 line_control;
  45. u8 line_status;
  46. };
  47. static void f81232_update_line_status(struct usb_serial_port *port,
  48. unsigned char *data,
  49. unsigned int actual_length)
  50. {
  51. /*
  52. * FIXME: Update port->icount, and call
  53. *
  54. * wake_up_interruptible(&port->port.delta_msr_wait);
  55. *
  56. * on MSR changes.
  57. */
  58. }
  59. static void f81232_read_int_callback(struct urb *urb)
  60. {
  61. struct usb_serial_port *port = urb->context;
  62. unsigned char *data = urb->transfer_buffer;
  63. unsigned int actual_length = urb->actual_length;
  64. int status = urb->status;
  65. int retval;
  66. switch (status) {
  67. case 0:
  68. /* success */
  69. break;
  70. case -ECONNRESET:
  71. case -ENOENT:
  72. case -ESHUTDOWN:
  73. /* this urb is terminated, clean up */
  74. dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
  75. __func__, status);
  76. return;
  77. default:
  78. dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
  79. __func__, status);
  80. goto exit;
  81. }
  82. usb_serial_debug_data(&port->dev, __func__,
  83. urb->actual_length, urb->transfer_buffer);
  84. f81232_update_line_status(port, data, actual_length);
  85. exit:
  86. retval = usb_submit_urb(urb, GFP_ATOMIC);
  87. if (retval)
  88. dev_err(&urb->dev->dev,
  89. "%s - usb_submit_urb failed with result %d\n",
  90. __func__, retval);
  91. }
  92. static void f81232_process_read_urb(struct urb *urb)
  93. {
  94. struct usb_serial_port *port = urb->context;
  95. struct f81232_private *priv = usb_get_serial_port_data(port);
  96. unsigned char *data = urb->transfer_buffer;
  97. char tty_flag = TTY_NORMAL;
  98. unsigned long flags;
  99. u8 line_status;
  100. int i;
  101. /* update line status */
  102. spin_lock_irqsave(&priv->lock, flags);
  103. line_status = priv->line_status;
  104. priv->line_status &= ~UART_STATE_TRANSIENT_MASK;
  105. spin_unlock_irqrestore(&priv->lock, flags);
  106. if (!urb->actual_length)
  107. return;
  108. /* break takes precedence over parity, */
  109. /* which takes precedence over framing errors */
  110. if (line_status & UART_BREAK_ERROR)
  111. tty_flag = TTY_BREAK;
  112. else if (line_status & UART_PARITY_ERROR)
  113. tty_flag = TTY_PARITY;
  114. else if (line_status & UART_FRAME_ERROR)
  115. tty_flag = TTY_FRAME;
  116. dev_dbg(&port->dev, "%s - tty_flag = %d\n", __func__, tty_flag);
  117. /* overrun is special, not associated with a char */
  118. if (line_status & UART_OVERRUN_ERROR)
  119. tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
  120. if (port->port.console && port->sysrq) {
  121. for (i = 0; i < urb->actual_length; ++i)
  122. if (!usb_serial_handle_sysrq_char(port, data[i]))
  123. tty_insert_flip_char(&port->port, data[i],
  124. tty_flag);
  125. } else {
  126. tty_insert_flip_string_fixed_flag(&port->port, data, tty_flag,
  127. urb->actual_length);
  128. }
  129. tty_flip_buffer_push(&port->port);
  130. }
  131. static int set_control_lines(struct usb_device *dev, u8 value)
  132. {
  133. /* FIXME - Stubbed out for now */
  134. return 0;
  135. }
  136. static void f81232_break_ctl(struct tty_struct *tty, int break_state)
  137. {
  138. /* FIXME - Stubbed out for now */
  139. /*
  140. * break_state = -1 to turn on break, and 0 to turn off break
  141. * see drivers/char/tty_io.c to see it used.
  142. * last_set_data_urb_value NEVER has the break bit set in it.
  143. */
  144. }
  145. static void f81232_set_termios(struct tty_struct *tty,
  146. struct usb_serial_port *port, struct ktermios *old_termios)
  147. {
  148. /* FIXME - Stubbed out for now */
  149. /* Don't change anything if nothing has changed */
  150. if (old_termios && !tty_termios_hw_change(&tty->termios, old_termios))
  151. return;
  152. /* Do the real work here... */
  153. if (old_termios)
  154. tty_termios_copy_hw(&tty->termios, old_termios);
  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. int result;
  170. /* Setup termios */
  171. if (tty)
  172. f81232_set_termios(tty, port, NULL);
  173. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  174. if (result) {
  175. dev_err(&port->dev, "%s - failed submitting interrupt urb,"
  176. " error %d\n", __func__, result);
  177. return result;
  178. }
  179. result = usb_serial_generic_open(tty, port);
  180. if (result) {
  181. usb_kill_urb(port->interrupt_in_urb);
  182. return result;
  183. }
  184. return 0;
  185. }
  186. static void f81232_close(struct usb_serial_port *port)
  187. {
  188. usb_serial_generic_close(port);
  189. usb_kill_urb(port->interrupt_in_urb);
  190. }
  191. static void f81232_dtr_rts(struct usb_serial_port *port, int on)
  192. {
  193. struct f81232_private *priv = usb_get_serial_port_data(port);
  194. unsigned long flags;
  195. u8 control;
  196. spin_lock_irqsave(&priv->lock, flags);
  197. /* Change DTR and RTS */
  198. if (on)
  199. priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
  200. else
  201. priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
  202. control = priv->line_control;
  203. spin_unlock_irqrestore(&priv->lock, flags);
  204. set_control_lines(port->serial->dev, control);
  205. }
  206. static int f81232_carrier_raised(struct usb_serial_port *port)
  207. {
  208. struct f81232_private *priv = usb_get_serial_port_data(port);
  209. if (priv->line_status & UART_DCD)
  210. return 1;
  211. return 0;
  212. }
  213. static int f81232_ioctl(struct tty_struct *tty,
  214. unsigned int cmd, unsigned long arg)
  215. {
  216. struct serial_struct ser;
  217. struct usb_serial_port *port = tty->driver_data;
  218. switch (cmd) {
  219. case TIOCGSERIAL:
  220. memset(&ser, 0, sizeof ser);
  221. ser.type = PORT_16654;
  222. ser.line = port->minor;
  223. ser.port = port->port_number;
  224. ser.baud_base = 460800;
  225. if (copy_to_user((void __user *)arg, &ser, sizeof ser))
  226. return -EFAULT;
  227. return 0;
  228. default:
  229. break;
  230. }
  231. return -ENOIOCTLCMD;
  232. }
  233. static int f81232_port_probe(struct usb_serial_port *port)
  234. {
  235. struct f81232_private *priv;
  236. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  237. if (!priv)
  238. return -ENOMEM;
  239. spin_lock_init(&priv->lock);
  240. usb_set_serial_port_data(port, priv);
  241. port->port.drain_delay = 256;
  242. return 0;
  243. }
  244. static int f81232_port_remove(struct usb_serial_port *port)
  245. {
  246. struct f81232_private *priv;
  247. priv = usb_get_serial_port_data(port);
  248. kfree(priv);
  249. return 0;
  250. }
  251. static struct usb_serial_driver f81232_device = {
  252. .driver = {
  253. .owner = THIS_MODULE,
  254. .name = "f81232",
  255. },
  256. .id_table = id_table,
  257. .num_ports = 1,
  258. .bulk_in_size = 256,
  259. .bulk_out_size = 256,
  260. .open = f81232_open,
  261. .close = f81232_close,
  262. .dtr_rts = f81232_dtr_rts,
  263. .carrier_raised = f81232_carrier_raised,
  264. .ioctl = f81232_ioctl,
  265. .break_ctl = f81232_break_ctl,
  266. .set_termios = f81232_set_termios,
  267. .tiocmget = f81232_tiocmget,
  268. .tiocmset = f81232_tiocmset,
  269. .tiocmiwait = usb_serial_generic_tiocmiwait,
  270. .process_read_urb = f81232_process_read_urb,
  271. .read_int_callback = f81232_read_int_callback,
  272. .port_probe = f81232_port_probe,
  273. .port_remove = f81232_port_remove,
  274. };
  275. static struct usb_serial_driver * const serial_drivers[] = {
  276. &f81232_device,
  277. NULL,
  278. };
  279. module_usb_serial_driver(serial_drivers, id_table);
  280. MODULE_DESCRIPTION("Fintek F81232 USB to serial adaptor driver");
  281. MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org");
  282. MODULE_LICENSE("GPL v2");