spcp8x5.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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->termios = tty_std_termios;
  235. tty->termios.c_cflag = B115200 | CS8 | CREAD | HUPCL | CLOCAL;
  236. tty->termios.c_ispeed = 115200;
  237. tty->termios.c_ospeed = 115200;
  238. }
  239. static void spcp8x5_set_termios(struct tty_struct *tty,
  240. struct usb_serial_port *port, struct ktermios *old_termios)
  241. {
  242. struct usb_serial *serial = port->serial;
  243. struct spcp8x5_private *priv = usb_get_serial_port_data(port);
  244. unsigned long flags;
  245. unsigned int cflag = tty->termios.c_cflag;
  246. unsigned short uartdata;
  247. unsigned char buf[2] = {0, 0};
  248. int baud;
  249. int i;
  250. u8 control;
  251. /* check that they really want us to change something */
  252. if (old_termios && !tty_termios_hw_change(&tty->termios, old_termios))
  253. return;
  254. /* set DTR/RTS active */
  255. spin_lock_irqsave(&priv->lock, flags);
  256. control = priv->line_control;
  257. if (old_termios && (old_termios->c_cflag & CBAUD) == B0) {
  258. priv->line_control |= MCR_DTR;
  259. if (!(old_termios->c_cflag & CRTSCTS))
  260. priv->line_control |= MCR_RTS;
  261. }
  262. if (control != priv->line_control) {
  263. control = priv->line_control;
  264. spin_unlock_irqrestore(&priv->lock, flags);
  265. spcp8x5_set_ctrl_line(port, control);
  266. } else {
  267. spin_unlock_irqrestore(&priv->lock, flags);
  268. }
  269. /* Set Baud Rate */
  270. baud = tty_get_baud_rate(tty);
  271. switch (baud) {
  272. case 300: buf[0] = 0x00; break;
  273. case 600: buf[0] = 0x01; break;
  274. case 1200: buf[0] = 0x02; break;
  275. case 2400: buf[0] = 0x03; break;
  276. case 4800: buf[0] = 0x04; break;
  277. case 9600: buf[0] = 0x05; break;
  278. case 19200: buf[0] = 0x07; break;
  279. case 38400: buf[0] = 0x09; break;
  280. case 57600: buf[0] = 0x0a; break;
  281. case 115200: buf[0] = 0x0b; break;
  282. case 230400: buf[0] = 0x0c; break;
  283. case 460800: buf[0] = 0x0d; break;
  284. case 921600: buf[0] = 0x0e; break;
  285. /* case 1200000: buf[0] = 0x0f; break; */
  286. /* case 2400000: buf[0] = 0x10; break; */
  287. case 3000000: buf[0] = 0x11; break;
  288. /* case 6000000: buf[0] = 0x12; break; */
  289. case 0:
  290. case 1000000:
  291. buf[0] = 0x0b; break;
  292. default:
  293. dev_err(&port->dev, "unsupported baudrate, using 9600\n");
  294. }
  295. /* Set Data Length : 00:5bit, 01:6bit, 10:7bit, 11:8bit */
  296. switch (cflag & CSIZE) {
  297. case CS5:
  298. buf[1] |= SET_UART_FORMAT_SIZE_5;
  299. break;
  300. case CS6:
  301. buf[1] |= SET_UART_FORMAT_SIZE_6;
  302. break;
  303. case CS7:
  304. buf[1] |= SET_UART_FORMAT_SIZE_7;
  305. break;
  306. default:
  307. case CS8:
  308. buf[1] |= SET_UART_FORMAT_SIZE_8;
  309. break;
  310. }
  311. /* Set Stop bit2 : 0:1bit 1:2bit */
  312. buf[1] |= (cflag & CSTOPB) ? SET_UART_FORMAT_STOP_2 :
  313. SET_UART_FORMAT_STOP_1;
  314. /* Set Parity bit3-4 01:Odd 11:Even */
  315. if (cflag & PARENB) {
  316. buf[1] |= (cflag & PARODD) ?
  317. SET_UART_FORMAT_PAR_ODD : SET_UART_FORMAT_PAR_EVEN ;
  318. } else {
  319. buf[1] |= SET_UART_FORMAT_PAR_NONE;
  320. }
  321. uartdata = buf[0] | buf[1]<<8;
  322. i = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  323. SET_UART_FORMAT_TYPE, SET_UART_FORMAT,
  324. uartdata, 0, NULL, 0, 100);
  325. if (i < 0)
  326. dev_err(&port->dev, "Set UART format %#x failed (error = %d)\n",
  327. uartdata, i);
  328. dev_dbg(&port->dev, "0x21:0x40:0:0 %d\n", i);
  329. if (cflag & CRTSCTS) {
  330. /* enable hardware flow control */
  331. spcp8x5_set_work_mode(port, 0x000a, SET_WORKING_MODE_U2C);
  332. }
  333. }
  334. static int spcp8x5_open(struct tty_struct *tty, struct usb_serial_port *port)
  335. {
  336. struct usb_serial *serial = port->serial;
  337. struct spcp8x5_private *priv = usb_get_serial_port_data(port);
  338. int ret;
  339. usb_clear_halt(serial->dev, port->write_urb->pipe);
  340. usb_clear_halt(serial->dev, port->read_urb->pipe);
  341. ret = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  342. 0x09, 0x00,
  343. 0x01, 0x00, NULL, 0x00, 100);
  344. if (ret)
  345. return ret;
  346. spcp8x5_set_ctrl_line(port, priv->line_control);
  347. if (tty)
  348. spcp8x5_set_termios(tty, port, NULL);
  349. return usb_serial_generic_open(tty, port);
  350. }
  351. static int spcp8x5_tiocmset(struct tty_struct *tty,
  352. unsigned int set, unsigned int clear)
  353. {
  354. struct usb_serial_port *port = tty->driver_data;
  355. struct spcp8x5_private *priv = usb_get_serial_port_data(port);
  356. unsigned long flags;
  357. u8 control;
  358. spin_lock_irqsave(&priv->lock, flags);
  359. if (set & TIOCM_RTS)
  360. priv->line_control |= MCR_RTS;
  361. if (set & TIOCM_DTR)
  362. priv->line_control |= MCR_DTR;
  363. if (clear & TIOCM_RTS)
  364. priv->line_control &= ~MCR_RTS;
  365. if (clear & TIOCM_DTR)
  366. priv->line_control &= ~MCR_DTR;
  367. control = priv->line_control;
  368. spin_unlock_irqrestore(&priv->lock, flags);
  369. return spcp8x5_set_ctrl_line(port, control);
  370. }
  371. static int spcp8x5_tiocmget(struct tty_struct *tty)
  372. {
  373. struct usb_serial_port *port = tty->driver_data;
  374. struct spcp8x5_private *priv = usb_get_serial_port_data(port);
  375. unsigned long flags;
  376. unsigned int mcr;
  377. u8 status;
  378. unsigned int result;
  379. result = spcp8x5_get_msr(port, &status);
  380. if (result)
  381. return result;
  382. spin_lock_irqsave(&priv->lock, flags);
  383. mcr = priv->line_control;
  384. spin_unlock_irqrestore(&priv->lock, flags);
  385. result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
  386. | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
  387. | ((status & MSR_STATUS_LINE_CTS) ? TIOCM_CTS : 0)
  388. | ((status & MSR_STATUS_LINE_DSR) ? TIOCM_DSR : 0)
  389. | ((status & MSR_STATUS_LINE_RI) ? TIOCM_RI : 0)
  390. | ((status & MSR_STATUS_LINE_DCD) ? TIOCM_CD : 0);
  391. return result;
  392. }
  393. static struct usb_serial_driver spcp8x5_device = {
  394. .driver = {
  395. .owner = THIS_MODULE,
  396. .name = "SPCP8x5",
  397. },
  398. .id_table = id_table,
  399. .num_ports = 1,
  400. .num_bulk_in = 1,
  401. .num_bulk_out = 1,
  402. .open = spcp8x5_open,
  403. .dtr_rts = spcp8x5_dtr_rts,
  404. .carrier_raised = spcp8x5_carrier_raised,
  405. .set_termios = spcp8x5_set_termios,
  406. .init_termios = spcp8x5_init_termios,
  407. .tiocmget = spcp8x5_tiocmget,
  408. .tiocmset = spcp8x5_tiocmset,
  409. .probe = spcp8x5_probe,
  410. .port_probe = spcp8x5_port_probe,
  411. .port_remove = spcp8x5_port_remove,
  412. };
  413. static struct usb_serial_driver * const serial_drivers[] = {
  414. &spcp8x5_device, NULL
  415. };
  416. module_usb_serial_driver(serial_drivers, id_table);
  417. MODULE_DESCRIPTION(DRIVER_DESC);
  418. MODULE_LICENSE("GPL");