f81232.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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 const struct usb_device_id id_table[] = {
  27. { USB_DEVICE(0x1934, 0x0706) },
  28. { } /* Terminating entry */
  29. };
  30. MODULE_DEVICE_TABLE(usb, id_table);
  31. #define CONTROL_DTR 0x01
  32. #define CONTROL_RTS 0x02
  33. #define UART_STATE 0x08
  34. #define UART_STATE_TRANSIENT_MASK 0x74
  35. #define UART_DCD 0x01
  36. #define UART_DSR 0x02
  37. #define UART_BREAK_ERROR 0x04
  38. #define UART_RING 0x08
  39. #define UART_FRAME_ERROR 0x10
  40. #define UART_PARITY_ERROR 0x20
  41. #define UART_OVERRUN_ERROR 0x40
  42. #define UART_CTS 0x80
  43. struct f81232_private {
  44. spinlock_t lock;
  45. wait_queue_head_t delta_msr_wait;
  46. u8 line_control;
  47. u8 line_status;
  48. };
  49. static void f81232_update_line_status(struct usb_serial_port *port,
  50. unsigned char *data,
  51. unsigned int actual_length)
  52. {
  53. }
  54. static void f81232_read_int_callback(struct urb *urb)
  55. {
  56. struct usb_serial_port *port = urb->context;
  57. unsigned char *data = urb->transfer_buffer;
  58. unsigned int actual_length = urb->actual_length;
  59. int status = urb->status;
  60. int retval;
  61. switch (status) {
  62. case 0:
  63. /* success */
  64. break;
  65. case -ECONNRESET:
  66. case -ENOENT:
  67. case -ESHUTDOWN:
  68. /* this urb is terminated, clean up */
  69. dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
  70. __func__, status);
  71. return;
  72. default:
  73. dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
  74. __func__, status);
  75. goto exit;
  76. }
  77. usb_serial_debug_data(&port->dev, __func__,
  78. urb->actual_length, urb->transfer_buffer);
  79. f81232_update_line_status(port, data, actual_length);
  80. exit:
  81. retval = usb_submit_urb(urb, GFP_ATOMIC);
  82. if (retval)
  83. dev_err(&urb->dev->dev,
  84. "%s - usb_submit_urb failed with result %d\n",
  85. __func__, retval);
  86. }
  87. static void f81232_process_read_urb(struct urb *urb)
  88. {
  89. struct usb_serial_port *port = urb->context;
  90. struct f81232_private *priv = usb_get_serial_port_data(port);
  91. struct tty_struct *tty;
  92. unsigned char *data = urb->transfer_buffer;
  93. char tty_flag = TTY_NORMAL;
  94. unsigned long flags;
  95. u8 line_status;
  96. int i;
  97. /* update line status */
  98. spin_lock_irqsave(&priv->lock, flags);
  99. line_status = priv->line_status;
  100. priv->line_status &= ~UART_STATE_TRANSIENT_MASK;
  101. spin_unlock_irqrestore(&priv->lock, flags);
  102. wake_up_interruptible(&priv->delta_msr_wait);
  103. if (!urb->actual_length)
  104. return;
  105. tty = tty_port_tty_get(&port->port);
  106. if (!tty)
  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(tty, 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(tty, data[i], tty_flag);
  124. } else {
  125. tty_insert_flip_string_fixed_flag(tty, data, tty_flag,
  126. urb->actual_length);
  127. }
  128. tty_flip_buffer_push(tty);
  129. tty_kref_put(tty);
  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 (!tty_termios_hw_change(tty->termios, old_termios))
  151. return;
  152. /* Do the real work here... */
  153. }
  154. static int f81232_tiocmget(struct tty_struct *tty)
  155. {
  156. /* FIXME - Stubbed out for now */
  157. return 0;
  158. }
  159. static int f81232_tiocmset(struct tty_struct *tty,
  160. unsigned int set, unsigned int clear)
  161. {
  162. /* FIXME - Stubbed out for now */
  163. return 0;
  164. }
  165. static int f81232_open(struct tty_struct *tty, struct usb_serial_port *port)
  166. {
  167. struct ktermios tmp_termios;
  168. int result;
  169. /* Setup termios */
  170. if (tty)
  171. f81232_set_termios(tty, port, &tmp_termios);
  172. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  173. if (result) {
  174. dev_err(&port->dev, "%s - failed submitting interrupt urb,"
  175. " error %d\n", __func__, result);
  176. return result;
  177. }
  178. result = usb_serial_generic_open(tty, port);
  179. if (result) {
  180. usb_kill_urb(port->interrupt_in_urb);
  181. return result;
  182. }
  183. port->port.drain_delay = 256;
  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 wait_modem_info(struct usb_serial_port *port, unsigned int arg)
  214. {
  215. struct f81232_private *priv = usb_get_serial_port_data(port);
  216. unsigned long flags;
  217. unsigned int prevstatus;
  218. unsigned int status;
  219. unsigned int changed;
  220. spin_lock_irqsave(&priv->lock, flags);
  221. prevstatus = priv->line_status;
  222. spin_unlock_irqrestore(&priv->lock, flags);
  223. while (1) {
  224. interruptible_sleep_on(&priv->delta_msr_wait);
  225. /* see if a signal did it */
  226. if (signal_pending(current))
  227. return -ERESTARTSYS;
  228. spin_lock_irqsave(&priv->lock, flags);
  229. status = priv->line_status;
  230. spin_unlock_irqrestore(&priv->lock, flags);
  231. changed = prevstatus ^ status;
  232. if (((arg & TIOCM_RNG) && (changed & UART_RING)) ||
  233. ((arg & TIOCM_DSR) && (changed & UART_DSR)) ||
  234. ((arg & TIOCM_CD) && (changed & UART_DCD)) ||
  235. ((arg & TIOCM_CTS) && (changed & UART_CTS))) {
  236. return 0;
  237. }
  238. prevstatus = status;
  239. }
  240. /* NOTREACHED */
  241. return 0;
  242. }
  243. static int f81232_ioctl(struct tty_struct *tty,
  244. unsigned int cmd, unsigned long arg)
  245. {
  246. struct serial_struct ser;
  247. struct usb_serial_port *port = tty->driver_data;
  248. dev_dbg(&port->dev, "%s (%d) cmd = 0x%04x\n", __func__,
  249. port->number, cmd);
  250. switch (cmd) {
  251. case TIOCGSERIAL:
  252. memset(&ser, 0, sizeof ser);
  253. ser.type = PORT_16654;
  254. ser.line = port->serial->minor;
  255. ser.port = port->number;
  256. ser.baud_base = 460800;
  257. if (copy_to_user((void __user *)arg, &ser, sizeof ser))
  258. return -EFAULT;
  259. return 0;
  260. case TIOCMIWAIT:
  261. dev_dbg(&port->dev, "%s (%d) TIOCMIWAIT\n", __func__,
  262. port->number);
  263. return wait_modem_info(port, arg);
  264. default:
  265. dev_dbg(&port->dev, "%s not supported = 0x%04x\n",
  266. __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_serial_driver f81232_device = {
  302. .driver = {
  303. .owner = THIS_MODULE,
  304. .name = "f81232",
  305. },
  306. .id_table = id_table,
  307. .num_ports = 1,
  308. .bulk_in_size = 256,
  309. .bulk_out_size = 256,
  310. .open = f81232_open,
  311. .close = f81232_close,
  312. .dtr_rts = f81232_dtr_rts,
  313. .carrier_raised = f81232_carrier_raised,
  314. .ioctl = f81232_ioctl,
  315. .break_ctl = f81232_break_ctl,
  316. .set_termios = f81232_set_termios,
  317. .tiocmget = f81232_tiocmget,
  318. .tiocmset = f81232_tiocmset,
  319. .process_read_urb = f81232_process_read_urb,
  320. .read_int_callback = f81232_read_int_callback,
  321. .attach = f81232_startup,
  322. .release = f81232_release,
  323. };
  324. static struct usb_serial_driver * const serial_drivers[] = {
  325. &f81232_device,
  326. NULL,
  327. };
  328. module_usb_serial_driver(serial_drivers, id_table);
  329. MODULE_DESCRIPTION("Fintek F81232 USB to serial adaptor driver");
  330. MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org");
  331. MODULE_LICENSE("GPL v2");