spcp8x5.c 13 KB

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