spcp8x5.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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_device *dev, u8 value,
  171. enum spcp8x5_type type)
  172. {
  173. int retval;
  174. u8 mcr = 0;
  175. if (type == SPCP825_007_TYPE)
  176. return -EPERM;
  177. mcr = (unsigned short)value;
  178. retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  179. SET_UART_STATUS_TYPE, SET_UART_STATUS,
  180. mcr, 0x04, NULL, 0, 100);
  181. if (retval != 0)
  182. dev_dbg(&dev->dev, "usb_control_msg return %#x\n", retval);
  183. return retval;
  184. }
  185. /*
  186. * Get the modem status register of the device.
  187. *
  188. * NOTE: not supported by spcp825-007
  189. */
  190. static int spcp8x5_get_msr(struct usb_device *dev, u8 *status,
  191. enum spcp8x5_type type)
  192. {
  193. u8 *status_buffer;
  194. int ret;
  195. /* I return Permited not support here but seem inval device
  196. * is more fix */
  197. if (type == SPCP825_007_TYPE)
  198. return -EPERM;
  199. if (status == NULL)
  200. return -EINVAL;
  201. status_buffer = kmalloc(1, GFP_KERNEL);
  202. if (!status_buffer)
  203. return -ENOMEM;
  204. status_buffer[0] = status[0];
  205. ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  206. GET_UART_STATUS, GET_UART_STATUS_TYPE,
  207. 0, GET_UART_STATUS_MSR, status_buffer, 1, 100);
  208. if (ret < 0)
  209. dev_dbg(&dev->dev, "Get MSR = 0x%p failed (error = %d)",
  210. status_buffer, ret);
  211. dev_dbg(&dev->dev, "0xc0:0x22:0:6 %d - 0x%p ", ret, status_buffer);
  212. status[0] = status_buffer[0];
  213. kfree(status_buffer);
  214. return ret;
  215. }
  216. /*
  217. * Select the work mode.
  218. *
  219. * NOTE: not supported by spcp825-007
  220. */
  221. static void spcp8x5_set_work_mode(struct usb_device *dev, u16 value,
  222. u16 index, enum spcp8x5_type type)
  223. {
  224. int ret;
  225. /* I return Permited not support here but seem inval device
  226. * is more fix */
  227. if (type == SPCP825_007_TYPE)
  228. return;
  229. ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  230. SET_WORKING_MODE_TYPE, SET_WORKING_MODE,
  231. value, index, NULL, 0, 100);
  232. dev_dbg(&dev->dev, "value = %#x , index = %#x\n", value, index);
  233. if (ret < 0)
  234. dev_dbg(&dev->dev,
  235. "RTSCTS usb_control_msg(enable flowctrl) = %d\n", ret);
  236. }
  237. static int spcp8x5_carrier_raised(struct usb_serial_port *port)
  238. {
  239. struct spcp8x5_private *priv = usb_get_serial_port_data(port);
  240. if (priv->line_status & MSR_STATUS_LINE_DCD)
  241. return 1;
  242. return 0;
  243. }
  244. static void spcp8x5_dtr_rts(struct usb_serial_port *port, int on)
  245. {
  246. struct spcp8x5_private *priv = usb_get_serial_port_data(port);
  247. unsigned long flags;
  248. u8 control;
  249. spin_lock_irqsave(&priv->lock, flags);
  250. if (on)
  251. priv->line_control = MCR_CONTROL_LINE_DTR
  252. | MCR_CONTROL_LINE_RTS;
  253. else
  254. priv->line_control &= ~ (MCR_CONTROL_LINE_DTR
  255. | MCR_CONTROL_LINE_RTS);
  256. control = priv->line_control;
  257. spin_unlock_irqrestore(&priv->lock, flags);
  258. spcp8x5_set_ctrl_line(port->serial->dev, control, priv->type);
  259. }
  260. static void spcp8x5_init_termios(struct tty_struct *tty)
  261. {
  262. tty->termios = tty_std_termios;
  263. tty->termios.c_cflag = B115200 | CS8 | CREAD | HUPCL | CLOCAL;
  264. tty->termios.c_ispeed = 115200;
  265. tty->termios.c_ospeed = 115200;
  266. }
  267. static void spcp8x5_set_termios(struct tty_struct *tty,
  268. struct usb_serial_port *port, struct ktermios *old_termios)
  269. {
  270. struct usb_serial *serial = port->serial;
  271. struct spcp8x5_private *priv = usb_get_serial_port_data(port);
  272. unsigned long flags;
  273. unsigned int cflag = tty->termios.c_cflag;
  274. unsigned int old_cflag = old_termios->c_cflag;
  275. unsigned short uartdata;
  276. unsigned char buf[2] = {0, 0};
  277. int baud;
  278. int i;
  279. u8 control;
  280. /* check that they really want us to change something */
  281. if (!tty_termios_hw_change(&tty->termios, old_termios))
  282. return;
  283. /* set DTR/RTS active */
  284. spin_lock_irqsave(&priv->lock, flags);
  285. control = priv->line_control;
  286. if ((old_cflag & CBAUD) == B0) {
  287. priv->line_control |= MCR_DTR;
  288. if (!(old_cflag & CRTSCTS))
  289. priv->line_control |= MCR_RTS;
  290. }
  291. if (control != priv->line_control) {
  292. control = priv->line_control;
  293. spin_unlock_irqrestore(&priv->lock, flags);
  294. spcp8x5_set_ctrl_line(serial->dev, control , priv->type);
  295. } else {
  296. spin_unlock_irqrestore(&priv->lock, flags);
  297. }
  298. /* Set Baud Rate */
  299. baud = tty_get_baud_rate(tty);
  300. switch (baud) {
  301. case 300: buf[0] = 0x00; break;
  302. case 600: buf[0] = 0x01; break;
  303. case 1200: buf[0] = 0x02; break;
  304. case 2400: buf[0] = 0x03; break;
  305. case 4800: buf[0] = 0x04; break;
  306. case 9600: buf[0] = 0x05; break;
  307. case 19200: buf[0] = 0x07; break;
  308. case 38400: buf[0] = 0x09; break;
  309. case 57600: buf[0] = 0x0a; break;
  310. case 115200: buf[0] = 0x0b; break;
  311. case 230400: buf[0] = 0x0c; break;
  312. case 460800: buf[0] = 0x0d; break;
  313. case 921600: buf[0] = 0x0e; break;
  314. /* case 1200000: buf[0] = 0x0f; break; */
  315. /* case 2400000: buf[0] = 0x10; break; */
  316. case 3000000: buf[0] = 0x11; break;
  317. /* case 6000000: buf[0] = 0x12; break; */
  318. case 0:
  319. case 1000000:
  320. buf[0] = 0x0b; break;
  321. default:
  322. dev_err(&port->dev, "spcp825 driver does not support the "
  323. "baudrate requested, using default of 9600.\n");
  324. }
  325. /* Set Data Length : 00:5bit, 01:6bit, 10:7bit, 11:8bit */
  326. if (cflag & CSIZE) {
  327. switch (cflag & CSIZE) {
  328. case CS5:
  329. buf[1] |= SET_UART_FORMAT_SIZE_5;
  330. break;
  331. case CS6:
  332. buf[1] |= SET_UART_FORMAT_SIZE_6;
  333. break;
  334. case CS7:
  335. buf[1] |= SET_UART_FORMAT_SIZE_7;
  336. break;
  337. default:
  338. case CS8:
  339. buf[1] |= SET_UART_FORMAT_SIZE_8;
  340. break;
  341. }
  342. }
  343. /* Set Stop bit2 : 0:1bit 1:2bit */
  344. buf[1] |= (cflag & CSTOPB) ? SET_UART_FORMAT_STOP_2 :
  345. SET_UART_FORMAT_STOP_1;
  346. /* Set Parity bit3-4 01:Odd 11:Even */
  347. if (cflag & PARENB) {
  348. buf[1] |= (cflag & PARODD) ?
  349. SET_UART_FORMAT_PAR_ODD : SET_UART_FORMAT_PAR_EVEN ;
  350. } else {
  351. buf[1] |= SET_UART_FORMAT_PAR_NONE;
  352. }
  353. uartdata = buf[0] | buf[1]<<8;
  354. i = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  355. SET_UART_FORMAT_TYPE, SET_UART_FORMAT,
  356. uartdata, 0, NULL, 0, 100);
  357. if (i < 0)
  358. dev_err(&port->dev, "Set UART format %#x failed (error = %d)\n",
  359. uartdata, i);
  360. dev_dbg(&port->dev, "0x21:0x40:0:0 %d\n", i);
  361. if (cflag & CRTSCTS) {
  362. /* enable hardware flow control */
  363. spcp8x5_set_work_mode(serial->dev, 0x000a,
  364. SET_WORKING_MODE_U2C, priv->type);
  365. }
  366. }
  367. static int spcp8x5_open(struct tty_struct *tty, struct usb_serial_port *port)
  368. {
  369. struct ktermios tmp_termios;
  370. struct usb_serial *serial = port->serial;
  371. struct spcp8x5_private *priv = usb_get_serial_port_data(port);
  372. int ret;
  373. unsigned long flags;
  374. u8 status = 0x30;
  375. /* status 0x30 means DSR and CTS = 1 other CDC RI and delta = 0 */
  376. usb_clear_halt(serial->dev, port->write_urb->pipe);
  377. usb_clear_halt(serial->dev, port->read_urb->pipe);
  378. ret = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  379. 0x09, 0x00,
  380. 0x01, 0x00, NULL, 0x00, 100);
  381. if (ret)
  382. return ret;
  383. spcp8x5_set_ctrl_line(serial->dev, priv->line_control, priv->type);
  384. /* Setup termios */
  385. if (tty)
  386. spcp8x5_set_termios(tty, port, &tmp_termios);
  387. spcp8x5_get_msr(serial->dev, &status, priv->type);
  388. /* may be we should update uart status here but now we did not do */
  389. spin_lock_irqsave(&priv->lock, flags);
  390. priv->line_status = status & 0xf0 ;
  391. spin_unlock_irqrestore(&priv->lock, flags);
  392. port->port.drain_delay = 256;
  393. return usb_serial_generic_open(tty, port);
  394. }
  395. static int spcp8x5_tiocmset(struct tty_struct *tty,
  396. unsigned int set, unsigned int clear)
  397. {
  398. struct usb_serial_port *port = tty->driver_data;
  399. struct spcp8x5_private *priv = usb_get_serial_port_data(port);
  400. unsigned long flags;
  401. u8 control;
  402. spin_lock_irqsave(&priv->lock, flags);
  403. if (set & TIOCM_RTS)
  404. priv->line_control |= MCR_RTS;
  405. if (set & TIOCM_DTR)
  406. priv->line_control |= MCR_DTR;
  407. if (clear & TIOCM_RTS)
  408. priv->line_control &= ~MCR_RTS;
  409. if (clear & TIOCM_DTR)
  410. priv->line_control &= ~MCR_DTR;
  411. control = priv->line_control;
  412. spin_unlock_irqrestore(&priv->lock, flags);
  413. return spcp8x5_set_ctrl_line(port->serial->dev, control, priv->type);
  414. }
  415. static int spcp8x5_tiocmget(struct tty_struct *tty)
  416. {
  417. struct usb_serial_port *port = tty->driver_data;
  418. struct spcp8x5_private *priv = usb_get_serial_port_data(port);
  419. unsigned long flags;
  420. unsigned int mcr;
  421. unsigned int status;
  422. unsigned int result;
  423. spin_lock_irqsave(&priv->lock, flags);
  424. mcr = priv->line_control;
  425. status = priv->line_status;
  426. spin_unlock_irqrestore(&priv->lock, flags);
  427. result = ((mcr & MCR_DTR) ? TIOCM_DTR : 0)
  428. | ((mcr & MCR_RTS) ? TIOCM_RTS : 0)
  429. | ((status & MSR_STATUS_LINE_CTS) ? TIOCM_CTS : 0)
  430. | ((status & MSR_STATUS_LINE_DSR) ? TIOCM_DSR : 0)
  431. | ((status & MSR_STATUS_LINE_RI) ? TIOCM_RI : 0)
  432. | ((status & MSR_STATUS_LINE_DCD) ? TIOCM_CD : 0);
  433. return result;
  434. }
  435. static struct usb_serial_driver spcp8x5_device = {
  436. .driver = {
  437. .owner = THIS_MODULE,
  438. .name = "SPCP8x5",
  439. },
  440. .id_table = id_table,
  441. .num_ports = 1,
  442. .open = spcp8x5_open,
  443. .dtr_rts = spcp8x5_dtr_rts,
  444. .carrier_raised = spcp8x5_carrier_raised,
  445. .set_termios = spcp8x5_set_termios,
  446. .init_termios = spcp8x5_init_termios,
  447. .tiocmget = spcp8x5_tiocmget,
  448. .tiocmset = spcp8x5_tiocmset,
  449. .port_probe = spcp8x5_port_probe,
  450. .port_remove = spcp8x5_port_remove,
  451. };
  452. static struct usb_serial_driver * const serial_drivers[] = {
  453. &spcp8x5_device, NULL
  454. };
  455. module_usb_serial_driver(serial_drivers, id_table);
  456. MODULE_DESCRIPTION(DRIVER_DESC);
  457. MODULE_LICENSE("GPL");