spcp8x5.c 13 KB

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