aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/serial/cp210x.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/usb/serial/cp210x.c')
-rw-r--r--drivers/usb/serial/cp210x.c105
1 files changed, 75 insertions, 30 deletions
diff --git a/drivers/usb/serial/cp210x.c b/drivers/usb/serial/cp210x.c
index fba1147ed916..8dbf51a43c45 100644
--- a/drivers/usb/serial/cp210x.c
+++ b/drivers/usb/serial/cp210x.c
@@ -39,6 +39,8 @@ static void cp210x_get_termios(struct tty_struct *,
39 struct usb_serial_port *port); 39 struct usb_serial_port *port);
40static void cp210x_get_termios_port(struct usb_serial_port *port, 40static void cp210x_get_termios_port(struct usb_serial_port *port,
41 unsigned int *cflagp, unsigned int *baudp); 41 unsigned int *cflagp, unsigned int *baudp);
42static void cp210x_change_speed(struct tty_struct *, struct usb_serial_port *,
43 struct ktermios *);
42static void cp210x_set_termios(struct tty_struct *, struct usb_serial_port *, 44static void cp210x_set_termios(struct tty_struct *, struct usb_serial_port *,
43 struct ktermios*); 45 struct ktermios*);
44static int cp210x_tiocmget(struct tty_struct *); 46static int cp210x_tiocmget(struct tty_struct *);
@@ -138,6 +140,7 @@ static const struct usb_device_id id_table[] = {
138 { USB_DEVICE(0x1843, 0x0200) }, /* Vaisala USB Instrument Cable */ 140 { USB_DEVICE(0x1843, 0x0200) }, /* Vaisala USB Instrument Cable */
139 { USB_DEVICE(0x18EF, 0xE00F) }, /* ELV USB-I2C-Interface */ 141 { USB_DEVICE(0x18EF, 0xE00F) }, /* ELV USB-I2C-Interface */
140 { USB_DEVICE(0x1BE3, 0x07A6) }, /* WAGO 750-923 USB Service Cable */ 142 { USB_DEVICE(0x1BE3, 0x07A6) }, /* WAGO 750-923 USB Service Cable */
143 { USB_DEVICE(0x3195, 0xF190) }, /* Link Instruments MSO-19 */
141 { USB_DEVICE(0x413C, 0x9500) }, /* DW700 GPS USB interface */ 144 { USB_DEVICE(0x413C, 0x9500) }, /* DW700 GPS USB interface */
142 { } /* Terminating Entry */ 145 { } /* Terminating Entry */
143}; 146};
@@ -201,6 +204,8 @@ static struct usb_serial_driver cp210x_device = {
201#define CP210X_EMBED_EVENTS 0x15 204#define CP210X_EMBED_EVENTS 0x15
202#define CP210X_GET_EVENTSTATE 0x16 205#define CP210X_GET_EVENTSTATE 0x16
203#define CP210X_SET_CHARS 0x19 206#define CP210X_SET_CHARS 0x19
207#define CP210X_GET_BAUDRATE 0x1D
208#define CP210X_SET_BAUDRATE 0x1E
204 209
205/* CP210X_IFC_ENABLE */ 210/* CP210X_IFC_ENABLE */
206#define UART_ENABLE 0x0001 211#define UART_ENABLE 0x0001
@@ -360,8 +365,8 @@ static inline int cp210x_set_config_single(struct usb_serial_port *port,
360 * Quantises the baud rate as per AN205 Table 1 365 * Quantises the baud rate as per AN205 Table 1
361 */ 366 */
362static unsigned int cp210x_quantise_baudrate(unsigned int baud) { 367static unsigned int cp210x_quantise_baudrate(unsigned int baud) {
363 if (baud <= 56) baud = 0; 368 if (baud <= 300)
364 else if (baud <= 300) baud = 300; 369 baud = 300;
365 else if (baud <= 600) baud = 600; 370 else if (baud <= 600) baud = 600;
366 else if (baud <= 1200) baud = 1200; 371 else if (baud <= 1200) baud = 1200;
367 else if (baud <= 1800) baud = 1800; 372 else if (baud <= 1800) baud = 1800;
@@ -389,10 +394,10 @@ static unsigned int cp210x_quantise_baudrate(unsigned int baud) {
389 else if (baud <= 491520) baud = 460800; 394 else if (baud <= 491520) baud = 460800;
390 else if (baud <= 567138) baud = 500000; 395 else if (baud <= 567138) baud = 500000;
391 else if (baud <= 670254) baud = 576000; 396 else if (baud <= 670254) baud = 576000;
392 else if (baud <= 1053257) baud = 921600; 397 else if (baud < 1000000)
393 else if (baud <= 1474560) baud = 1228800; 398 baud = 921600;
394 else if (baud <= 2457600) baud = 1843200; 399 else if (baud > 2000000)
395 else baud = 3686400; 400 baud = 2000000;
396 return baud; 401 return baud;
397} 402}
398 403
@@ -409,13 +414,14 @@ static int cp210x_open(struct tty_struct *tty, struct usb_serial_port *port)
409 return result; 414 return result;
410 } 415 }
411 416
412 result = usb_serial_generic_open(tty, port);
413 if (result)
414 return result;
415
416 /* Configure the termios structure */ 417 /* Configure the termios structure */
417 cp210x_get_termios(tty, port); 418 cp210x_get_termios(tty, port);
418 return 0; 419
420 /* The baud rate must be initialised on cp2104 */
421 if (tty)
422 cp210x_change_speed(tty, port, NULL);
423
424 return usb_serial_generic_open(tty, port);
419} 425}
420 426
421static void cp210x_close(struct usb_serial_port *port) 427static void cp210x_close(struct usb_serial_port *port)
@@ -467,10 +473,7 @@ static void cp210x_get_termios_port(struct usb_serial_port *port,
467 473
468 dbg("%s - port %d", __func__, port->number); 474 dbg("%s - port %d", __func__, port->number);
469 475
470 cp210x_get_config(port, CP210X_GET_BAUDDIV, &baud, 2); 476 cp210x_get_config(port, CP210X_GET_BAUDRATE, &baud, 4);
471 /* Convert to baudrate */
472 if (baud)
473 baud = cp210x_quantise_baudrate((BAUD_RATE_GEN_FREQ + baud/2)/ baud);
474 477
475 dbg("%s - baud rate = %d", __func__, baud); 478 dbg("%s - baud rate = %d", __func__, baud);
476 *baudp = baud; 479 *baudp = baud;
@@ -579,11 +582,64 @@ static void cp210x_get_termios_port(struct usb_serial_port *port,
579 *cflagp = cflag; 582 *cflagp = cflag;
580} 583}
581 584
585/*
586 * CP2101 supports the following baud rates:
587 *
588 * 300, 600, 1200, 1800, 2400, 4800, 7200, 9600, 14400, 19200, 28800,
589 * 38400, 56000, 57600, 115200, 128000, 230400, 460800, 921600
590 *
591 * CP2102 and CP2103 support the following additional rates:
592 *
593 * 4000, 16000, 51200, 64000, 76800, 153600, 250000, 256000, 500000,
594 * 576000
595 *
596 * The device will map a requested rate to a supported one, but the result
597 * of requests for rates greater than 1053257 is undefined (see AN205).
598 *
599 * CP2104, CP2105 and CP2110 support most rates up to 2M, 921k and 1M baud,
600 * respectively, with an error less than 1%. The actual rates are determined
601 * by
602 *
603 * div = round(freq / (2 x prescale x request))
604 * actual = freq / (2 x prescale x div)
605 *
606 * For CP2104 and CP2105 freq is 48Mhz and prescale is 4 for request <= 365bps
607 * or 1 otherwise.
608 * For CP2110 freq is 24Mhz and prescale is 4 for request <= 300bps or 1
609 * otherwise.
610 */
611static void cp210x_change_speed(struct tty_struct *tty,
612 struct usb_serial_port *port, struct ktermios *old_termios)
613{
614 u32 baud;
615
616 baud = tty->termios->c_ospeed;
617
618 /* This maps the requested rate to a rate valid on cp2102 or cp2103,
619 * or to an arbitrary rate in [1M,2M].
620 *
621 * NOTE: B0 is not implemented.
622 */
623 baud = cp210x_quantise_baudrate(baud);
624
625 dbg("%s - setting baud rate to %u", __func__, baud);
626 if (cp210x_set_config(port, CP210X_SET_BAUDRATE, &baud,
627 sizeof(baud))) {
628 dev_warn(&port->dev, "failed to set baud rate to %u\n", baud);
629 if (old_termios)
630 baud = old_termios->c_ospeed;
631 else
632 baud = 9600;
633 }
634
635 tty_encode_baud_rate(tty, baud, baud);
636}
637
582static void cp210x_set_termios(struct tty_struct *tty, 638static void cp210x_set_termios(struct tty_struct *tty,
583 struct usb_serial_port *port, struct ktermios *old_termios) 639 struct usb_serial_port *port, struct ktermios *old_termios)
584{ 640{
585 unsigned int cflag, old_cflag; 641 unsigned int cflag, old_cflag;
586 unsigned int baud = 0, bits; 642 unsigned int bits;
587 unsigned int modem_ctl[4]; 643 unsigned int modem_ctl[4];
588 644
589 dbg("%s - port %d", __func__, port->number); 645 dbg("%s - port %d", __func__, port->number);
@@ -593,20 +649,9 @@ static void cp210x_set_termios(struct tty_struct *tty,
593 649
594 cflag = tty->termios->c_cflag; 650 cflag = tty->termios->c_cflag;
595 old_cflag = old_termios->c_cflag; 651 old_cflag = old_termios->c_cflag;
596 baud = cp210x_quantise_baudrate(tty_get_baud_rate(tty)); 652
597 653 if (tty->termios->c_ospeed != old_termios->c_ospeed)
598 /* If the baud rate is to be updated*/ 654 cp210x_change_speed(tty, port, old_termios);
599 if (baud != tty_termios_baud_rate(old_termios) && baud != 0) {
600 dbg("%s - Setting baud rate to %d baud", __func__,
601 baud);
602 if (cp210x_set_config_single(port, CP210X_SET_BAUDDIV,
603 ((BAUD_RATE_GEN_FREQ + baud/2) / baud))) {
604 dbg("Baud rate requested not supported by device");
605 baud = tty_termios_baud_rate(old_termios);
606 }
607 }
608 /* Report back the resulting baud rate */
609 tty_encode_baud_rate(tty, baud, baud);
610 655
611 /* If the number of data bits is to be updated */ 656 /* If the number of data bits is to be updated */
612 if ((cflag & CSIZE) != (old_cflag & CSIZE)) { 657 if ((cflag & CSIZE) != (old_cflag & CSIZE)) {