cp2101.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. /*
  2. * Silicon Laboratories CP2101/CP2102 USB to RS232 serial adaptor driver
  3. *
  4. * Copyright (C) 2005 Craig Shelley (craig@microtron.org.uk)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License version
  8. * 2 as published by the Free Software Foundation.
  9. *
  10. */
  11. #include <linux/config.h>
  12. #include <linux/kernel.h>
  13. #include <linux/errno.h>
  14. #include <linux/slab.h>
  15. #include <linux/tty.h>
  16. #include <linux/tty_flip.h>
  17. #include <linux/module.h>
  18. #include <linux/moduleparam.h>
  19. #include <linux/usb.h>
  20. #include <asm/uaccess.h>
  21. #include "usb-serial.h"
  22. /*
  23. * Version Information
  24. */
  25. #define DRIVER_VERSION "v0.03"
  26. #define DRIVER_DESC "Silicon Labs CP2101/CP2102 RS232 serial adaptor driver"
  27. /*
  28. * Function Prototypes
  29. */
  30. static int cp2101_open(struct usb_serial_port*, struct file*);
  31. static void cp2101_cleanup(struct usb_serial_port*);
  32. static void cp2101_close(struct usb_serial_port*, struct file*);
  33. static void cp2101_get_termios(struct usb_serial_port*);
  34. static void cp2101_set_termios(struct usb_serial_port*, struct termios*);
  35. static void cp2101_break_ctl(struct usb_serial_port*, int);
  36. static int cp2101_startup (struct usb_serial *);
  37. static void cp2101_shutdown(struct usb_serial*);
  38. static int debug;
  39. static struct usb_device_id id_table [] = {
  40. {USB_DEVICE(0x10c4, 0xea60) }, /*Silicon labs factory default*/
  41. {USB_DEVICE(0x10ab, 0x10c5) }, /*Siemens MC60 Cable*/
  42. { } /* Terminating Entry*/
  43. };
  44. MODULE_DEVICE_TABLE (usb, id_table);
  45. static struct usb_driver cp2101_driver = {
  46. .owner = THIS_MODULE,
  47. .name = "CP2101",
  48. .probe = usb_serial_probe,
  49. .disconnect = usb_serial_disconnect,
  50. .id_table = id_table,
  51. };
  52. static struct usb_serial_device_type cp2101_device = {
  53. .owner = THIS_MODULE,
  54. .name = "CP2101",
  55. .id_table = id_table,
  56. .num_interrupt_in = 0,
  57. .num_bulk_in = 0,
  58. .num_bulk_out = 0,
  59. .num_ports = 1,
  60. .open = cp2101_open,
  61. .close = cp2101_close,
  62. .break_ctl = cp2101_break_ctl,
  63. .set_termios = cp2101_set_termios,
  64. .attach = cp2101_startup,
  65. .shutdown = cp2101_shutdown,
  66. };
  67. /*Config request types*/
  68. #define REQTYPE_HOST_TO_DEVICE 0x41
  69. #define REQTYPE_DEVICE_TO_HOST 0xc1
  70. /*Config SET requests. To GET, add 1 to the request number*/
  71. #define CP2101_UART 0x00 /*Enable / Disable*/
  72. #define CP2101_BAUDRATE 0x01 /*(BAUD_RATE_GEN_FREQ / baudrate)*/
  73. #define CP2101_BITS 0x03 /*0x(0)(data bits)(parity)(stop bits)*/
  74. #define CP2101_BREAK 0x05 /*On / Off*/
  75. #define CP2101_DTRRTS 0x07 /*101 / 202 ???*/
  76. #define CP2101_CONFIG_16 0x13 /*16 bytes of config data ???*/
  77. #define CP2101_CONFIG_6 0x19 /*6 bytes of config data ???*/
  78. /*CP2101_UART*/
  79. #define UART_ENABLE 0x0001
  80. #define UART_DISABLE 0x0000
  81. /*CP2101_BAUDRATE*/
  82. #define BAUD_RATE_GEN_FREQ 0x384000
  83. /*CP2101_BITS*/
  84. #define BITS_DATA_MASK 0X0f00
  85. #define BITS_DATA_6 0X0600
  86. #define BITS_DATA_7 0X0700
  87. #define BITS_DATA_8 0X0800
  88. #define BITS_DATA_9 0X0900
  89. #define BITS_PARITY_MASK 0x00f0
  90. #define BITS_PARITY_NONE 0x0000
  91. #define BITS_PARITY_ODD 0x0010
  92. #define BITS_PARITY_EVEN 0x0020
  93. #define BITS_PARITY_MARK 0x0030
  94. #define BITS_PARITY_SPACE 0x0040
  95. #define BITS_STOP_MASK 0x000f
  96. #define BITS_STOP_1 0x0000
  97. #define BITS_STOP_1_5 0x0001
  98. #define BITS_STOP_2 0x0002
  99. #define BREAK_ON 0x0000
  100. #define BREAK_OFF 0x0001
  101. static int cp2101_get_config(struct usb_serial_port* port, u8 request)
  102. {
  103. struct usb_serial *serial = port->serial;
  104. unsigned char buf[4];
  105. unsigned int value;
  106. int result, i;
  107. /*For get requests, the request number must be incremented*/
  108. request++;
  109. /*Issue the request, attempting to read 4 bytes*/
  110. result = usb_control_msg (serial->dev,usb_rcvctrlpipe (serial->dev, 0),
  111. request, REQTYPE_DEVICE_TO_HOST, 0x0000,
  112. 0, buf, 4, 300);
  113. if (result < 0) {
  114. dev_err(&port->dev, "%s - Unable to send config request, "
  115. "request=0x%x result=%d\n",
  116. __FUNCTION__, request, result);
  117. return result;
  118. }
  119. /*Assemble each byte read into an integer value*/
  120. value = 0;
  121. for (i=0; i<4 && i<result; i++)
  122. value |= (buf[i] << (i * 8));
  123. dbg( " %s - request=0x%x result=%d value=0x%x",
  124. __FUNCTION__, request, result, value);
  125. return value;
  126. }
  127. static int cp2101_set_config(struct usb_serial_port* port, u8 request, u16 value)
  128. {
  129. struct usb_serial *serial = port->serial;
  130. int result;
  131. result = usb_control_msg (serial->dev, usb_sndctrlpipe(serial->dev, 0),
  132. request, REQTYPE_HOST_TO_DEVICE, value,
  133. 0, NULL, 0, 300);
  134. if (result <0) {
  135. dev_err(&port->dev, "%s - Unable to send config request, "
  136. "request=0x%x value=0x%x result=%d\n",
  137. __FUNCTION__, request, value, result);
  138. return result;
  139. }
  140. dbg(" %s - request=0x%x value=0x%x result=%d",
  141. __FUNCTION__, request, value, result);
  142. return 0;
  143. }
  144. static int cp2101_open (struct usb_serial_port *port, struct file *filp)
  145. {
  146. struct usb_serial *serial = port->serial;
  147. int result;
  148. dbg("%s - port %d", __FUNCTION__, port->number);
  149. if (cp2101_set_config(port, CP2101_UART, UART_ENABLE)) {
  150. dev_err(&port->dev, "%s - Unable to enable UART\n",
  151. __FUNCTION__);
  152. return -EPROTO;
  153. }
  154. /* Start reading from the device */
  155. usb_fill_bulk_urb (port->read_urb, serial->dev,
  156. usb_rcvbulkpipe(serial->dev,
  157. port->bulk_in_endpointAddress),
  158. port->read_urb->transfer_buffer,
  159. port->read_urb->transfer_buffer_length,
  160. serial->type->read_bulk_callback,
  161. port);
  162. result = usb_submit_urb(port->read_urb, GFP_KERNEL);
  163. if (result) {
  164. dev_err(&port->dev, "%s - failed resubmitting read urb, "
  165. "error %d\n", __FUNCTION__, result);
  166. return result;
  167. }
  168. /*Configure the termios structure*/
  169. cp2101_get_termios(port);
  170. return 0;
  171. }
  172. static void cp2101_cleanup (struct usb_serial_port *port)
  173. {
  174. struct usb_serial *serial = port->serial;
  175. dbg("%s - port %d", __FUNCTION__, port->number);
  176. if (serial->dev) {
  177. /* shutdown any bulk reads that might be going on */
  178. if (serial->num_bulk_out)
  179. usb_kill_urb(port->write_urb);
  180. if (serial->num_bulk_in)
  181. usb_kill_urb(port->read_urb);
  182. }
  183. }
  184. static void cp2101_close (struct usb_serial_port *port, struct file * filp)
  185. {
  186. dbg("%s - port %d", __FUNCTION__, port->number);
  187. /* shutdown our urbs */
  188. dbg("%s - shutting down urbs", __FUNCTION__);
  189. usb_kill_urb(port->write_urb);
  190. usb_kill_urb(port->read_urb);
  191. cp2101_set_config(port, CP2101_UART, UART_DISABLE);
  192. }
  193. /* cp2101_get_termios*/
  194. /* Reads the baud rate, data bits, parity and stop bits from the device*/
  195. /* Corrects any unsupported values*/
  196. /* Configures the termios structure to reflect the state of the device*/
  197. static void cp2101_get_termios (struct usb_serial_port *port)
  198. {
  199. unsigned int cflag;
  200. int baud;
  201. int bits;
  202. dbg("%s - port %d", __FUNCTION__, port->number);
  203. if ((!port->tty) || (!port->tty->termios)) {
  204. dbg("%s - no tty structures", __FUNCTION__);
  205. return;
  206. }
  207. cflag = port->tty->termios->c_cflag;
  208. baud = cp2101_get_config(port, CP2101_BAUDRATE);
  209. /*Convert to baudrate*/
  210. if (baud)
  211. baud = BAUD_RATE_GEN_FREQ / baud;
  212. dbg("%s - baud rate = %d", __FUNCTION__, baud);
  213. cflag &= ~CBAUD;
  214. switch (baud) {
  215. /* The baud rates which are commented out below
  216. * appear to be supported by the device
  217. * but are non-standard
  218. */
  219. case 600: cflag |= B600; break;
  220. case 1200: cflag |= B1200; break;
  221. case 1800: cflag |= B1800; break;
  222. case 2400: cflag |= B2400; break;
  223. case 4800: cflag |= B4800; break;
  224. /*case 7200: cflag |= B7200; break;*/
  225. case 9600: cflag |= B9600; break;
  226. /*case 14400: cflag |= B14400; break;*/
  227. case 19200: cflag |= B19200; break;
  228. /*case 28800: cflag |= B28800; break;*/
  229. case 38400: cflag |= B38400; break;
  230. /*case 55854: cflag |= B55054; break;*/
  231. case 57600: cflag |= B57600; break;
  232. case 115200: cflag |= B115200; break;
  233. /*case 127117: cflag |= B127117; break;*/
  234. case 230400: cflag |= B230400; break;
  235. case 460800: cflag |= B460800; break;
  236. case 921600: cflag |= B921600; break;
  237. /*case 3686400: cflag |= B3686400; break;*/
  238. default:
  239. dbg("%s - Baud rate is not supported, "
  240. "using 9600 baud", __FUNCTION__);
  241. cflag |= B9600;
  242. cp2101_set_config(port, CP2101_BAUDRATE,
  243. (BAUD_RATE_GEN_FREQ/9600));
  244. break;
  245. }
  246. bits = cp2101_get_config(port, CP2101_BITS);
  247. cflag &= ~CSIZE;
  248. switch(bits & BITS_DATA_MASK) {
  249. case BITS_DATA_6:
  250. dbg("%s - data bits = 6", __FUNCTION__);
  251. cflag |= CS6;
  252. break;
  253. case BITS_DATA_7:
  254. dbg("%s - data bits = 7", __FUNCTION__);
  255. cflag |= CS7;
  256. break;
  257. case BITS_DATA_8:
  258. dbg("%s - data bits = 8", __FUNCTION__);
  259. cflag |= CS8;
  260. break;
  261. case BITS_DATA_9:
  262. dbg("%s - data bits = 9 (not supported, "
  263. "using 8 data bits)", __FUNCTION__);
  264. cflag |= CS8;
  265. bits &= ~BITS_DATA_MASK;
  266. bits |= BITS_DATA_8;
  267. cp2101_set_config(port, CP2101_BITS, bits);
  268. break;
  269. default:
  270. dbg("%s - Unknown number of data bits, "
  271. "using 8", __FUNCTION__);
  272. cflag |= CS8;
  273. bits &= ~BITS_DATA_MASK;
  274. bits |= BITS_DATA_8;
  275. cp2101_set_config(port, CP2101_BITS, bits);
  276. break;
  277. }
  278. switch(bits & BITS_PARITY_MASK) {
  279. case BITS_PARITY_NONE:
  280. dbg("%s - parity = NONE", __FUNCTION__);
  281. cflag &= ~PARENB;
  282. break;
  283. case BITS_PARITY_ODD:
  284. dbg("%s - parity = ODD", __FUNCTION__);
  285. cflag |= (PARENB|PARODD);
  286. break;
  287. case BITS_PARITY_EVEN:
  288. dbg("%s - parity = EVEN", __FUNCTION__);
  289. cflag &= ~PARODD;
  290. cflag |= PARENB;
  291. break;
  292. case BITS_PARITY_MARK:
  293. dbg("%s - parity = MARK (not supported, "
  294. "disabling parity)", __FUNCTION__);
  295. cflag &= ~PARENB;
  296. bits &= ~BITS_PARITY_MASK;
  297. cp2101_set_config(port, CP2101_BITS, bits);
  298. break;
  299. case BITS_PARITY_SPACE:
  300. dbg("%s - parity = SPACE (not supported, "
  301. "disabling parity)", __FUNCTION__);
  302. cflag &= ~PARENB;
  303. bits &= ~BITS_PARITY_MASK;
  304. cp2101_set_config(port, CP2101_BITS, bits);
  305. break;
  306. default:
  307. dbg("%s - Unknown parity mode, "
  308. "disabling parity", __FUNCTION__);
  309. cflag &= ~PARENB;
  310. bits &= ~BITS_PARITY_MASK;
  311. cp2101_set_config(port, CP2101_BITS, bits);
  312. break;
  313. }
  314. cflag &= ~CSTOPB;
  315. switch(bits & BITS_STOP_MASK) {
  316. case BITS_STOP_1:
  317. dbg("%s - stop bits = 1", __FUNCTION__);
  318. break;
  319. case BITS_STOP_1_5:
  320. dbg("%s - stop bits = 1.5 (not supported, "
  321. "using 1 stop bit", __FUNCTION__);
  322. bits &= ~BITS_STOP_MASK;
  323. cp2101_set_config(port, CP2101_BITS, bits);
  324. break;
  325. case BITS_STOP_2:
  326. dbg("%s - stop bits = 2", __FUNCTION__);
  327. cflag |= CSTOPB;
  328. break;
  329. default:
  330. dbg("%s - Unknown number of stop bits, "
  331. "using 1 stop bit", __FUNCTION__);
  332. bits &= ~BITS_STOP_MASK;
  333. cp2101_set_config(port, CP2101_BITS, bits);
  334. break;
  335. }
  336. port->tty->termios->c_cflag = cflag;
  337. }
  338. static void cp2101_set_termios (struct usb_serial_port *port,
  339. struct termios *old_termios)
  340. {
  341. unsigned int cflag, old_cflag=0;
  342. int baud=0;
  343. int bits;
  344. dbg("%s - port %d", __FUNCTION__, port->number);
  345. if ((!port->tty) || (!port->tty->termios)) {
  346. dbg("%s - no tty structures", __FUNCTION__);
  347. return;
  348. }
  349. cflag = port->tty->termios->c_cflag;
  350. /* check that they really want us to change something */
  351. if (old_termios) {
  352. if ((cflag == old_termios->c_cflag) &&
  353. (RELEVANT_IFLAG(port->tty->termios->c_iflag)
  354. == RELEVANT_IFLAG(old_termios->c_iflag))) {
  355. dbg("%s - nothing to change...", __FUNCTION__);
  356. return;
  357. }
  358. old_cflag = old_termios->c_cflag;
  359. }
  360. /* If the baud rate is to be updated*/
  361. if ((cflag & CBAUD) != (old_cflag & CBAUD)) {
  362. switch (cflag & CBAUD) {
  363. /* The baud rates which are commented out below
  364. * appear to be supported by the device
  365. * but are non-standard
  366. */
  367. case B0: baud = 0; break;
  368. case B600: baud = 600; break;
  369. case B1200: baud = 1200; break;
  370. case B1800: baud = 1800; break;
  371. case B2400: baud = 2400; break;
  372. case B4800: baud = 4800; break;
  373. /*case B7200: baud = 7200; break;*/
  374. case B9600: baud = 9600; break;
  375. /*ase B14400: baud = 14400; break;*/
  376. case B19200: baud = 19200; break;
  377. /*case B28800: baud = 28800; break;*/
  378. case B38400: baud = 38400; break;
  379. /*case B55854: baud = 55054; break;*/
  380. case B57600: baud = 57600; break;
  381. case B115200: baud = 115200; break;
  382. /*case B127117: baud = 127117; break;*/
  383. case B230400: baud = 230400; break;
  384. case B460800: baud = 460800; break;
  385. case B921600: baud = 921600; break;
  386. /*case B3686400: baud = 3686400; break;*/
  387. default:
  388. dev_err(&port->dev, "cp2101 driver does not "
  389. "support the baudrate requested\n");
  390. break;
  391. }
  392. if (baud) {
  393. dbg("%s - Setting baud rate to %d baud", __FUNCTION__,
  394. baud);
  395. if (cp2101_set_config(port, CP2101_BAUDRATE,
  396. (BAUD_RATE_GEN_FREQ / baud)))
  397. dev_err(&port->dev, "Baud rate requested not "
  398. "supported by device\n");
  399. }
  400. }
  401. /*If the number of data bits is to be updated*/
  402. if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
  403. bits = cp2101_get_config(port, CP2101_BITS);
  404. bits &= ~BITS_DATA_MASK;
  405. switch (cflag & CSIZE) {
  406. case CS6:
  407. bits |= BITS_DATA_6;
  408. dbg("%s - data bits = 6", __FUNCTION__);
  409. break;
  410. case CS7:
  411. bits |= BITS_DATA_7;
  412. dbg("%s - data bits = 7", __FUNCTION__);
  413. break;
  414. case CS8:
  415. bits |= BITS_DATA_8;
  416. dbg("%s - data bits = 8", __FUNCTION__);
  417. break;
  418. /*case CS9:
  419. bits |= BITS_DATA_9;
  420. dbg("%s - data bits = 9", __FUNCTION__);
  421. break;*/
  422. default:
  423. dev_err(&port->dev, "cp2101 driver does not "
  424. "support the number of bits requested,"
  425. " using 8 bit mode\n");
  426. bits |= BITS_DATA_8;
  427. break;
  428. }
  429. if (cp2101_set_config(port, CP2101_BITS, bits))
  430. dev_err(&port->dev, "Number of data bits requested "
  431. "not supported by device\n");
  432. }
  433. if ((cflag & (PARENB|PARODD)) != (old_cflag & (PARENB|PARODD))) {
  434. bits = cp2101_get_config(port, CP2101_BITS);
  435. bits &= ~BITS_PARITY_MASK;
  436. if (cflag & PARENB) {
  437. if (cflag & PARODD) {
  438. bits |= BITS_PARITY_ODD;
  439. dbg("%s - parity = ODD", __FUNCTION__);
  440. } else {
  441. bits |= BITS_PARITY_EVEN;
  442. dbg("%s - parity = EVEN", __FUNCTION__);
  443. }
  444. }
  445. if (cp2101_set_config(port, CP2101_BITS, bits))
  446. dev_err(&port->dev, "Parity mode not supported "
  447. "by device\n");
  448. }
  449. if ((cflag & CSTOPB) != (old_cflag & CSTOPB)) {
  450. bits = cp2101_get_config(port, CP2101_BITS);
  451. bits &= ~BITS_STOP_MASK;
  452. if (cflag & CSTOPB) {
  453. bits |= BITS_STOP_2;
  454. dbg("%s - stop bits = 2", __FUNCTION__);
  455. } else {
  456. bits |= BITS_STOP_1;
  457. dbg("%s - stop bits = 1", __FUNCTION__);
  458. }
  459. if (cp2101_set_config(port, CP2101_BITS, bits))
  460. dev_err(&port->dev, "Number of stop bits requested "
  461. "not supported by device\n");
  462. }
  463. }
  464. static void cp2101_break_ctl (struct usb_serial_port *port, int break_state)
  465. {
  466. u16 state;
  467. dbg("%s - port %d", __FUNCTION__, port->number);
  468. if (break_state == 0)
  469. state = BREAK_OFF;
  470. else
  471. state = BREAK_ON;
  472. dbg("%s - turning break %s", __FUNCTION__,
  473. state==BREAK_OFF ? "off" : "on");
  474. cp2101_set_config(port, CP2101_BREAK, state);
  475. }
  476. static int cp2101_startup (struct usb_serial *serial)
  477. {
  478. /*CP2101 buffers behave strangely unless device is reset*/
  479. usb_reset_device(serial->dev);
  480. return 0;
  481. }
  482. static void cp2101_shutdown (struct usb_serial *serial)
  483. {
  484. int i;
  485. dbg("%s", __FUNCTION__);
  486. /* stop reads and writes on all ports */
  487. for (i=0; i < serial->num_ports; ++i) {
  488. cp2101_cleanup(serial->port[i]);
  489. }
  490. }
  491. static int __init cp2101_init (void)
  492. {
  493. int retval;
  494. retval = usb_serial_register(&cp2101_device);
  495. if (retval)
  496. return retval; /*Failed to register*/
  497. retval = usb_register(&cp2101_driver);
  498. if (retval) {
  499. /*Failed to register*/
  500. usb_serial_deregister(&cp2101_device);
  501. return retval;
  502. }
  503. /*Success*/
  504. info(DRIVER_DESC " " DRIVER_VERSION);
  505. return 0;
  506. }
  507. static void __exit cp2101_exit (void)
  508. {
  509. usb_deregister (&cp2101_driver);
  510. usb_serial_deregister (&cp2101_device);
  511. }
  512. module_init(cp2101_init);
  513. module_exit(cp2101_exit);
  514. MODULE_DESCRIPTION(DRIVER_DESC);
  515. MODULE_VERSION(DRIVER_VERSION);
  516. MODULE_LICENSE("GPL");
  517. module_param(debug, bool, S_IRUGO | S_IWUSR);
  518. MODULE_PARM_DESC(debug, "Enable verbose debugging messages");