spcp8x5.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * spcp8x5 USB to serial adaptor driver
  4. *
  5. * Copyright (C) 2010-2013 Johan Hovold (jhovold@gmail.com)
  6. * Copyright (C) 2006 Linxb (xubin.lin@worldplus.com.cn)
  7. * Copyright (C) 2006 S1 Corp.
  8. *
  9. * Original driver for 2.6.10 pl2303 driver by
  10. * Greg Kroah-Hartman (greg@kroah.com)
  11. * Changes for 2.6.20 by Harald Klein <hari@vt100.at>
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/errno.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/module.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/usb.h>
  22. #include <linux/usb/serial.h>
  23. #define DRIVER_DESC "SPCP8x5 USB to serial adaptor driver"
  24. #define SPCP825_QUIRK_NO_UART_STATUS 0x01
  25. #define SPCP825_QUIRK_NO_WORK_MODE 0x02
  26. #define SPCP8x5_007_VID 0x04FC
  27. #define SPCP8x5_007_PID 0x0201
  28. #define SPCP8x5_008_VID 0x04fc
  29. #define SPCP8x5_008_PID 0x0235
  30. #define SPCP8x5_PHILIPS_VID 0x0471
  31. #define SPCP8x5_PHILIPS_PID 0x081e
  32. #define SPCP8x5_INTERMATIC_VID 0x04FC
  33. #define SPCP8x5_INTERMATIC_PID 0x0204
  34. #define SPCP8x5_835_VID 0x04fc
  35. #define SPCP8x5_835_PID 0x0231
  36. static const struct usb_device_id id_table[] = {
  37. { USB_DEVICE(SPCP8x5_PHILIPS_VID , SPCP8x5_PHILIPS_PID)},
  38. { USB_DEVICE(SPCP8x5_INTERMATIC_VID, SPCP8x5_INTERMATIC_PID)},
  39. { USB_DEVICE(SPCP8x5_835_VID, SPCP8x5_835_PID)},
  40. { USB_DEVICE(SPCP8x5_008_VID, SPCP8x5_008_PID)},
  41. { USB_DEVICE(SPCP8x5_007_VID, SPCP8x5_007_PID),
  42. .driver_info = SPCP825_QUIRK_NO_UART_STATUS |
  43. SPCP825_QUIRK_NO_WORK_MODE },
  44. { } /* Terminating entry */
  45. };
  46. MODULE_DEVICE_TABLE(usb, id_table);
  47. /* spcp8x5 spec register define */
  48. #define MCR_CONTROL_LINE_RTS 0x02
  49. #define MCR_CONTROL_LINE_DTR 0x01
  50. #define MCR_DTR 0x01
  51. #define MCR_RTS 0x02
  52. #define MSR_STATUS_LINE_DCD 0x80
  53. #define MSR_STATUS_LINE_RI 0x40
  54. #define MSR_STATUS_LINE_DSR 0x20
  55. #define MSR_STATUS_LINE_CTS 0x10
  56. /* verdor command here , we should define myself */
  57. #define SET_DEFAULT 0x40
  58. #define SET_DEFAULT_TYPE 0x20
  59. #define SET_UART_FORMAT 0x40
  60. #define SET_UART_FORMAT_TYPE 0x21
  61. #define SET_UART_FORMAT_SIZE_5 0x00
  62. #define SET_UART_FORMAT_SIZE_6 0x01
  63. #define SET_UART_FORMAT_SIZE_7 0x02
  64. #define SET_UART_FORMAT_SIZE_8 0x03
  65. #define SET_UART_FORMAT_STOP_1 0x00
  66. #define SET_UART_FORMAT_STOP_2 0x04
  67. #define SET_UART_FORMAT_PAR_NONE 0x00
  68. #define SET_UART_FORMAT_PAR_ODD 0x10
  69. #define SET_UART_FORMAT_PAR_EVEN 0x30
  70. #define SET_UART_FORMAT_PAR_MASK 0xD0
  71. #define SET_UART_FORMAT_PAR_SPACE 0x90
  72. #define GET_UART_STATUS_TYPE 0xc0
  73. #define GET_UART_STATUS 0x22
  74. #define GET_UART_STATUS_MSR 0x06
  75. #define SET_UART_STATUS 0x40
  76. #define SET_UART_STATUS_TYPE 0x23
  77. #define SET_UART_STATUS_MCR 0x0004
  78. #define SET_UART_STATUS_MCR_DTR 0x01
  79. #define SET_UART_STATUS_MCR_RTS 0x02
  80. #define SET_UART_STATUS_MCR_LOOP 0x10
  81. #define SET_WORKING_MODE 0x40
  82. #define SET_WORKING_MODE_TYPE 0x24
  83. #define SET_WORKING_MODE_U2C 0x00
  84. #define SET_WORKING_MODE_RS485 0x01
  85. #define SET_WORKING_MODE_PDMA 0x02
  86. #define SET_WORKING_MODE_SPP 0x03
  87. #define SET_FLOWCTL_CHAR 0x40
  88. #define SET_FLOWCTL_CHAR_TYPE 0x25
  89. #define GET_VERSION 0xc0
  90. #define GET_VERSION_TYPE 0x26
  91. #define SET_REGISTER 0x40
  92. #define SET_REGISTER_TYPE 0x27
  93. #define GET_REGISTER 0xc0
  94. #define GET_REGISTER_TYPE 0x28
  95. #define SET_RAM 0x40
  96. #define SET_RAM_TYPE 0x31
  97. #define GET_RAM 0xc0
  98. #define GET_RAM_TYPE 0x32
  99. /* how come ??? */
  100. #define UART_STATE 0x08
  101. #define UART_STATE_TRANSIENT_MASK 0x75
  102. #define UART_DCD 0x01
  103. #define UART_DSR 0x02
  104. #define UART_BREAK_ERROR 0x04
  105. #define UART_RING 0x08
  106. #define UART_FRAME_ERROR 0x10
  107. #define UART_PARITY_ERROR 0x20
  108. #define UART_OVERRUN_ERROR 0x40
  109. #define UART_CTS 0x80
  110. struct spcp8x5_private {
  111. unsigned quirks;
  112. spinlock_t lock;
  113. u8 line_control;
  114. };
  115. static int spcp8x5_probe(struct usb_serial *serial,
  116. const struct usb_device_id *id)
  117. {
  118. usb_set_serial_data(serial, (void *)id);
  119. return 0;
  120. }
  121. static int spcp8x5_port_probe(struct usb_serial_port *port)
  122. {
  123. const struct usb_device_id *id = usb_get_serial_data(port->serial);
  124. struct spcp8x5_private *priv;
  125. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  126. if (!priv)
  127. return -ENOMEM;
  128. spin_lock_init(&priv->lock);
  129. priv->quirks = id->driver_info;
  130. usb_set_serial_port_data(port, priv);
  131. port->port.drain_delay = 256;
  132. return 0;
  133. }
  134. static void spcp8x5_port_remove(struct usb_serial_port *port)
  135. {
  136. struct spcp8x5_private *priv;
  137. priv = usb_get_serial_port_data(port);
  138. kfree(priv);
  139. }
  140. static int spcp8x5_set_ctrl_line(struct usb_serial_port *port, u8 mcr)
  141. {
  142. struct spcp8x5_private *priv = usb_get_serial_port_data(port);
  143. struct usb_device *dev = port->serial->dev;
  144. int retval;
  145. if (priv->quirks & SPCP825_QUIRK_NO_UART_STATUS)
  146. return -EPERM;
  147. retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  148. SET_UART_STATUS_TYPE, SET_UART_STATUS,
  149. mcr, 0x04, NULL, 0, 100);
  150. if (retval != 0) {
  151. dev_err(&port->dev, "failed to set control lines: %d\n",
  152. retval);
  153. }
  154. return retval;
  155. }
  156. static int spcp8x5_get_msr(struct usb_serial_port *port, u8 *status)
  157. {
  158. struct spcp8x5_private *priv = usb_get_serial_port_data(port);
  159. struct usb_device *dev = port->serial->dev;
  160. u8 *buf;
  161. int ret;
  162. if (priv->quirks & SPCP825_QUIRK_NO_UART_STATUS)
  163. return -EPERM;
  164. buf = kzalloc(1, GFP_KERNEL);
  165. if (!buf)
  166. return -ENOMEM;
  167. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  168. GET_UART_STATUS, GET_UART_STATUS_TYPE,
  169. 0, GET_UART_STATUS_MSR, buf, 1, 100);
  170. if (ret < 1) {
  171. dev_err(&port->dev, "failed to get modem status: %d\n", ret);
  172. if (ret >= 0)
  173. ret = -EIO;
  174. goto out;
  175. }
  176. dev_dbg(&port->dev, "0xc0:0x22:0:6 %d - 0x02%x\n", ret, *buf);
  177. *status = *buf;
  178. ret = 0;
  179. out:
  180. kfree(buf);
  181. return ret;
  182. }
  183. static void spcp8x5_set_work_mode(struct usb_serial_port *port, u16 value,
  184. u16 index)
  185. {
  186. struct spcp8x5_private *priv = usb_get_serial_port_data(port);
  187. struct usb_device *dev = port->serial->dev;
  188. int ret;
  189. if (priv->quirks & SPCP825_QUIRK_NO_WORK_MODE)
  190. return;
  191. ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  192. SET_WORKING_MODE_TYPE, SET_WORKING_MODE,
  193. value, index, NULL, 0, 100);
  194. dev_dbg(&port->dev, "value = %#x , index = %#x\n", value, index);
  195. if (ret < 0)
  196. dev_err(&port->dev, "failed to set work mode: %d\n", ret);
  197. }
  198. static int spcp8x5_carrier_raised(struct usb_serial_port *port)
  199. {
  200. u8 msr;
  201. int ret;
  202. ret = spcp8x5_get_msr(port, &msr);
  203. if (ret || msr & MSR_STATUS_LINE_DCD)
  204. return 1;
  205. return 0;
  206. }
  207. static void spcp8x5_dtr_rts(struct usb_serial_port *port, int on)
  208. {
  209. struct spcp8x5_private *priv = usb_get_serial_port_data(port);
  210. unsigned long flags;
  211. u8 control;
  212. spin_lock_irqsave(&priv->lock, flags);
  213. if (on)
  214. priv->line_control = MCR_CONTROL_LINE_DTR
  215. | MCR_CONTROL_LINE_RTS;
  216. else
  217. priv->line_control &= ~ (MCR_CONTROL_LINE_DTR
  218. | MCR_CONTROL_LINE_RTS);
  219. control = priv->line_control;
  220. spin_unlock_irqrestore(&priv->lock, flags);
  221. spcp8x5_set_ctrl_line(port, control);
  222. }
  223. static void spcp8x5_init_termios(struct tty_struct *tty)
  224. {
  225. tty_encode_baud_rate(tty, 115200, 115200);
  226. }
  227. static void spcp8x5_set_termios(struct tty_struct *tty,
  228. struct usb_serial_port *port,
  229. const struct ktermios *old_termios)
  230. {
  231. struct usb_serial *serial = port->serial;
  232. struct spcp8x5_private *priv = usb_get_serial_port_data(port);
  233. unsigned long flags;
  234. unsigned int cflag = tty->termios.c_cflag;
  235. unsigned short uartdata;
  236. unsigned char buf[2] = {0, 0};
  237. int baud;
  238. int i;
  239. u8 control;
  240. /* check that they really want us to change something */
  241. if (old_termios && !tty_termios_hw_change(&tty->termios, old_termios))
  242. return;
  243. /* set DTR/RTS active */
  244. spin_lock_irqsave(&priv->lock, flags);
  245. control = priv->line_control;
  246. if (old_termios && (old_termios->c_cflag & CBAUD) == B0) {
  247. priv->line_control |= MCR_DTR;
  248. if (!(old_termios->c_cflag & CRTSCTS))
  249. priv->line_control |= MCR_RTS;
  250. }
  251. if (control != priv->line_control) {
  252. control = priv->line_control;
  253. spin_unlock_irqrestore(&priv->lock, flags);
  254. spcp8x5_set_ctrl_line(port, control);
  255. } else {
  256. spin_unlock_irqrestore(&priv->lock, flags);
  257. }
  258. /* Set Baud Rate */
  259. baud = tty_get_baud_rate(tty);
  260. switch (baud) {
  261. case 300: buf[0] = 0x00; break;
  262. case 600: buf[0] = 0x01; break;
  263. case 1200: buf[0] = 0x02; break;
  264. case 2400: buf[0] = 0x03; break;
  265. case 4800: buf[0] = 0x04; break;
  266. case 9600: buf[0] = 0x05; break;
  267. case 19200: buf[0] = 0x07; break;
  268. case 38400: buf[0] = 0x09; break;
  269. case 57600: buf[0] = 0x0a; break;
  270. case 115200: buf[0] = 0x0b; break;
  271. case 230400: buf[0] = 0x0c; break;
  272. case 460800: buf[0] = 0x0d; break;
  273. case 921600: buf[0] = 0x0e; break;
  274. /* case 1200000: buf[0] = 0x0f; break; */
  275. /* case 2400000: buf[0] = 0x10; break; */
  276. case 3000000: buf[0] = 0x11; break;
  277. /* case 6000000: buf[0] = 0x12; break; */
  278. case 0:
  279. case 1000000:
  280. buf[0] = 0x0b; break;
  281. default:
  282. dev_err(&port->dev, "unsupported baudrate, using 9600\n");
  283. }
  284. /* Set Data Length : 00:5bit, 01:6bit, 10:7bit, 11:8bit */
  285. switch (cflag & CSIZE) {
  286. case CS5:
  287. buf[1] |= SET_UART_FORMAT_SIZE_5;
  288. break;
  289. case CS6:
  290. buf[1] |= SET_UART_FORMAT_SIZE_6;
  291. break;
  292. case CS7:
  293. buf[1] |= SET_UART_FORMAT_SIZE_7;
  294. break;
  295. default:
  296. case CS8:
  297. buf[1] |= SET_UART_FORMAT_SIZE_8;
  298. break;
  299. }
  300. /* Set Stop bit2 : 0:1bit 1:2bit */
  301. buf[1] |= (cflag & CSTOPB) ? SET_UART_FORMAT_STOP_2 :
  302. SET_UART_FORMAT_STOP_1;
  303. /* Set Parity bit3-4 01:Odd 11:Even */
  304. if (cflag & PARENB) {
  305. buf[1] |= (cflag & PARODD) ?
  306. SET_UART_FORMAT_PAR_ODD : SET_UART_FORMAT_PAR_EVEN ;
  307. } else {
  308. buf[1] |= SET_UART_FORMAT_PAR_NONE;
  309. }
  310. uartdata = buf[0] | buf[1]<<8;
  311. i = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  312. SET_UART_FORMAT_TYPE, SET_UART_FORMAT,
  313. uartdata, 0, NULL, 0, 100);
  314. if (i < 0)
  315. dev_err(&port->dev, "Set UART format %#x failed (error = %d)\n",
  316. uartdata, i);
  317. dev_dbg(&port->dev, "0x21:0x40:0:0 %d\n", i);
  318. if (cflag & CRTSCTS) {
  319. /* enable hardware flow control */
  320. spcp8x5_set_work_mode(port, 0x000a, SET_WORKING_MODE_U2C);
  321. }
  322. }
  323. static int spcp8x5_open(struct tty_struct *tty, struct usb_serial_port *port)
  324. {
  325. struct usb_serial *serial = port->serial;
  326. struct spcp8x5_private *priv = usb_get_serial_port_data(port);
  327. int ret;
  328. usb_clear_halt(serial->dev, port->write_urb->pipe);
  329. usb_clear_halt(serial->dev, port->read_urb->pipe);
  330. ret = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  331. 0x09, 0x00,
  332. 0x01, 0x00, NULL, 0x00, 100);
  333. if (ret)
  334. return ret;
  335. spcp8x5_set_ctrl_line(port, priv->line_control);
  336. if (tty)
  337. spcp8x5_set_termios(tty, port, NULL);
  338. return usb_serial_generic_open(tty, port);
  339. }
  340. static int spcp8x5_tiocmset(struct tty_struct *tty,
  341. unsigned int set, unsigned int clear)
  342. {
  343. struct usb_serial_port *port = tty->driver_data;
  344. struct spcp8x5_private *priv = usb_get_serial_port_data(port);
  345. unsigned long flags;
  346. u8 control;
  347. spin_lock_irqsave(&priv->lock, flags);
  348. if (set & TIOCM_RTS)
  349. priv->line_control |= MCR_RTS;
  350. if (set & TIOCM_DTR)
  351. priv->line_control |= MCR_DTR;
  352. if (clear & TIOCM_RTS)
  353. priv->line_control &= ~MCR_RTS;
  354. if (clear & TIOCM_DTR)
  355. priv->line_control &= ~MCR_DTR;
  356. control = priv->line_control;
  357. spin_unlock_irqrestore(&priv->lock, flags);
  358. return spcp8x5_set_ctrl_line(port, control);
  359. }
  360. static int spcp8x5_tiocmget(struct tty_struct *tty)
  361. {
  362. struct usb_serial_port *port = tty->driver_data;
  363. struct spcp8x5_private *priv = usb_get_serial_port_data(port);
  364. unsigned long flags;
  365. unsigned int mcr;
  366. u8 status;
  367. unsigned int result;
  368. result = spcp8x5_get_msr(port, &status);
  369. if (result)
  370. return result;
  371. spin_lock_irqsave(&priv->lock, flags);
  372. mcr = priv->line_control;
  373. spin_unlock_irqrestore(&priv->lock, flags);
  374. result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
  375. | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
  376. | ((status & MSR_STATUS_LINE_CTS) ? TIOCM_CTS : 0)
  377. | ((status & MSR_STATUS_LINE_DSR) ? TIOCM_DSR : 0)
  378. | ((status & MSR_STATUS_LINE_RI) ? TIOCM_RI : 0)
  379. | ((status & MSR_STATUS_LINE_DCD) ? TIOCM_CD : 0);
  380. return result;
  381. }
  382. static struct usb_serial_driver spcp8x5_device = {
  383. .driver = {
  384. .name = "SPCP8x5",
  385. },
  386. .id_table = id_table,
  387. .num_ports = 1,
  388. .num_bulk_in = 1,
  389. .num_bulk_out = 1,
  390. .open = spcp8x5_open,
  391. .dtr_rts = spcp8x5_dtr_rts,
  392. .carrier_raised = spcp8x5_carrier_raised,
  393. .set_termios = spcp8x5_set_termios,
  394. .init_termios = spcp8x5_init_termios,
  395. .tiocmget = spcp8x5_tiocmget,
  396. .tiocmset = spcp8x5_tiocmset,
  397. .probe = spcp8x5_probe,
  398. .port_probe = spcp8x5_port_probe,
  399. .port_remove = spcp8x5_port_remove,
  400. };
  401. static struct usb_serial_driver * const serial_drivers[] = {
  402. &spcp8x5_device, NULL
  403. };
  404. module_usb_serial_driver(serial_drivers, id_table);
  405. MODULE_DESCRIPTION(DRIVER_DESC);
  406. MODULE_LICENSE("GPL");