spcp8x5.c 13 KB

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