Commit e917ba01 authored by Frank Schäfer's avatar Frank Schäfer Committed by Greg Kroah-Hartman

usb: pl2303: move the two baud rate encoding methods to separate functions

Signed-off-by: default avatarFrank Schäfer <fschaefer.oss@googlemail.com>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent b9208c72
...@@ -269,30 +269,18 @@ static int pl2303_set_control_lines(struct usb_serial_port *port, u8 value) ...@@ -269,30 +269,18 @@ static int pl2303_set_control_lines(struct usb_serial_port *port, u8 value)
return retval; return retval;
} }
static void pl2303_encode_baudrate(struct tty_struct *tty, static int pl2303_baudrate_encode_direct(int baud, enum pl2303_type type,
struct usb_serial_port *port,
u8 buf[4]) u8 buf[4])
{ {
struct usb_serial *serial = port->serial;
struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
int baud;
baud = tty_get_baud_rate(tty);
dev_dbg(&port->dev, "baud requested = %d\n", baud);
if (!baud)
return;
if (spriv->type != HX || baud <= 115200) {
/* /*
* NOTE: Only the values defined in baud_sup are supported ! * NOTE: Only the values defined in baud_sup are supported !
* => if unsupported values are set, the PL2303 seems to * => if unsupported values are set, the PL2303 seems to
* use 9600 baud (at least my PL2303X always does) * use 9600 baud (at least my PL2303X always does)
*/ */
const int baud_sup[] = { 75, 150, 300, 600, 1200, 1800, 2400, const int baud_sup[] = { 75, 150, 300, 600, 1200, 1800, 2400, 3600,
3600, 4800, 7200, 9600, 14400, 19200, 4800, 7200, 9600, 14400, 19200, 28800, 38400,
28800, 38400, 57600, 115200, 230400, 57600, 115200, 230400, 460800, 614400, 921600,
460800, 614400, 921600, 1228800, 1228800, 2457600, 3000000, 6000000 };
2457600, 3000000, 6000000 };
int i; int i;
/* Set baudrate to nearest supported value */ /* Set baudrate to nearest supported value */
...@@ -300,27 +288,28 @@ static void pl2303_encode_baudrate(struct tty_struct *tty, ...@@ -300,27 +288,28 @@ static void pl2303_encode_baudrate(struct tty_struct *tty,
if (baud_sup[i] > baud) if (baud_sup[i] > baud)
break; break;
} }
if (i == ARRAY_SIZE(baud_sup)) if (i == ARRAY_SIZE(baud_sup))
baud = baud_sup[i - 1]; baud = baud_sup[i - 1];
else if (i > 0 else if (i > 0 && (baud_sup[i] - baud) > (baud - baud_sup[i - 1]))
&& (baud_sup[i] - baud) > (baud - baud_sup[i - 1]))
baud = baud_sup[i - 1]; baud = baud_sup[i - 1];
else else
baud = baud_sup[i]; baud = baud_sup[i];
/* type_0, type_1 only support up to 1228800 baud */ /* type_0, type_1 only support up to 1228800 baud */
if (spriv->type != HX) if (type != HX)
baud = min_t(int, baud, 1228800); baud = min_t(int, baud, 1228800);
/* Direct (standard) baud rate encoding method */ /* Direct (standard) baud rate encoding method */
put_unaligned_le32(baud, buf); put_unaligned_le32(baud, buf);
} else {
return baud;
}
static int pl2303_baudrate_encode_divisor(int baud, enum pl2303_type type,
u8 buf[4])
{
/* /*
* Divisor based baud rate encoding method * Divisor based baud rate encoding method
* *
* NOTE: it's not clear if the type_0/1 chips * NOTE: it's not clear if the type_0/1 chips support this method
* support this method
* *
* divisor = 12MHz * 32 / baudrate = 2^A * B * divisor = 12MHz * 32 / baudrate = 2^A * B
* *
...@@ -337,7 +326,7 @@ static void pl2303_encode_baudrate(struct tty_struct *tty, ...@@ -337,7 +326,7 @@ static void pl2303_encode_baudrate(struct tty_struct *tty,
/* Respect the specified baud rate limits */ /* Respect the specified baud rate limits */
baud = max_t(int, baud, 75); baud = max_t(int, baud, 75);
if (spriv->type == HX) if (type == HX)
baud = min_t(int, baud, 6000000); baud = min_t(int, baud, 6000000);
else else
baud = min_t(int, baud, 1228800); baud = min_t(int, baud, 1228800);
...@@ -376,8 +365,32 @@ static void pl2303_encode_baudrate(struct tty_struct *tty, ...@@ -376,8 +365,32 @@ static void pl2303_encode_baudrate(struct tty_struct *tty,
if (B <= 8) if (B <= 8)
B = 512; B = 512;
baud = 12000000 * 32 / ((1 << A) * B); baud = 12000000 * 32 / ((1 << A) * B);
}
return baud;
}
static void pl2303_encode_baudrate(struct tty_struct *tty,
struct usb_serial_port *port,
enum pl2303_type type,
u8 buf[4])
{
int baud;
baud = tty_get_baud_rate(tty);
dev_dbg(&port->dev, "baud requested = %d\n", baud);
if (!baud)
return;
/*
* There are two methods for setting/encoding the baud rate
* 1) Direct method: encodes the baud rate value directly
* => supported by all chip types
* 2) Divisor based method: encodes a divisor to a base value (12MHz*32)
* => supported by HX chips (and likely not by type_0/1 chips)
*/
if (type != HX || baud <= 115200)
baud = pl2303_baudrate_encode_direct(baud, type, buf);
else
baud = pl2303_baudrate_encode_divisor(baud, type, buf);
/* Save resulting baud rate */ /* Save resulting baud rate */
tty_encode_baud_rate(tty, baud, baud); tty_encode_baud_rate(tty, baud, baud);
dev_dbg(&port->dev, "baud set = %d\n", baud); dev_dbg(&port->dev, "baud set = %d\n", baud);
...@@ -434,8 +447,8 @@ static void pl2303_set_termios(struct tty_struct *tty, ...@@ -434,8 +447,8 @@ static void pl2303_set_termios(struct tty_struct *tty,
dev_dbg(&port->dev, "data bits = %d\n", buf[6]); dev_dbg(&port->dev, "data bits = %d\n", buf[6]);
} }
/* For reference buf[0]:buf[3] baud rate value */ /* For reference: buf[0]:buf[3] baud rate value */
pl2303_encode_baudrate(tty, port, &buf[0]); pl2303_encode_baudrate(tty, port, spriv->type, buf);
/* For reference buf[4]=0 is 1 stop bits */ /* For reference buf[4]=0 is 1 stop bits */
/* For reference buf[4]=1 is 1.5 stop bits */ /* For reference buf[4]=1 is 1.5 stop bits */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment