Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

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

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

parent b9208c72
Loading
Loading
Loading
Loading
+114 −101
Original line number Diff line number Diff line
@@ -269,30 +269,18 @@ static int pl2303_set_control_lines(struct usb_serial_port *port, u8 value)
	return retval;
}

static void pl2303_encode_baudrate(struct tty_struct *tty,
					struct usb_serial_port *port,
static int pl2303_baudrate_encode_direct(int baud, enum pl2303_type type,
								      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 !
	 *       => if unsupported values are set, the PL2303 seems to
	 *	    use 9600 baud (at least my PL2303X always does)
	 */
		const int baud_sup[] = { 75, 150, 300, 600, 1200, 1800, 2400,
					 3600, 4800, 7200, 9600, 14400, 19200,
					 28800, 38400, 57600, 115200, 230400,
					 460800, 614400, 921600, 1228800,
					 2457600, 3000000, 6000000 };
	const int baud_sup[] = { 75, 150, 300, 600, 1200, 1800, 2400, 3600,
				 4800, 7200, 9600, 14400, 19200, 28800, 38400,
				 57600, 115200, 230400, 460800, 614400, 921600,
				 1228800, 2457600, 3000000, 6000000 };
	int i;

	/* Set baudrate to nearest supported value */
@@ -300,27 +288,28 @@ static void pl2303_encode_baudrate(struct tty_struct *tty,
		if (baud_sup[i] > baud)
			break;
	}

	if (i == ARRAY_SIZE(baud_sup))
		baud = baud_sup[i - 1];
		else if (i > 0
			     && (baud_sup[i] - baud) > (baud - baud_sup[i - 1]))
	else if (i > 0 && (baud_sup[i] - baud) > (baud - baud_sup[i - 1]))
		baud = baud_sup[i - 1];
	else
		baud = baud_sup[i];

	/* type_0, type_1 only support up to 1228800 baud */
		if (spriv->type != HX)
	if (type != HX)
		baud = min_t(int, baud, 1228800);

	/* Direct (standard) baud rate encoding method */
	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
	 *
		 * NOTE: it's not clear if the type_0/1 chips
		 * support this method
	 * NOTE: it's not clear if the type_0/1 chips support this method
	 *
	 * divisor = 12MHz * 32 / baudrate = 2^A * B
	 *
@@ -337,7 +326,7 @@ static void pl2303_encode_baudrate(struct tty_struct *tty,

	/* Respect the specified baud rate limits */
	baud = max_t(int, baud, 75);
		if (spriv->type == HX)
	if (type == HX)
		baud = min_t(int, baud, 6000000);
	else
		baud = min_t(int, baud, 1228800);
@@ -376,8 +365,32 @@ static void pl2303_encode_baudrate(struct tty_struct *tty,
	if (B <= 8)
		B = 512;
	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 */
	tty_encode_baud_rate(tty, baud, baud);
	dev_dbg(&port->dev, "baud set = %d\n", baud);
@@ -434,8 +447,8 @@ static void pl2303_set_termios(struct tty_struct *tty,
		dev_dbg(&port->dev, "data bits = %d\n", buf[6]);
	}

	/* For reference buf[0]:buf[3] baud rate value */
	pl2303_encode_baudrate(tty, port, &buf[0]);
	/* For reference:   buf[0]:buf[3] baud rate value */
	pl2303_encode_baudrate(tty, port, spriv->type, buf);

	/* For reference buf[4]=0 is 1 stop bits */
	/* For reference buf[4]=1 is 1.5 stop bits */