spcp8x5.c 14 KB

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