spcp8x5.c 13 KB

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