usb: serial: add support for CH348
Subsystems (2)
v2 cover usb: serial: add support for CH348
Hello
The CH348 is an octo serial to USB adapter.
The following patch adds a driver for supporting it.
Since there is no public datasheet, unfortunatly it remains some magic values.
It was tested with a large range of baud from 1200 to 1500000 and used with success in one of our kernel CI testlab.
Regards
Changes since v1:
- use a data structure for encoding/decoding messages.
- check if needed endpoints exists
- fix URB leak in ch348_allocate_status_read error case
- test for maximum baud rate as stated by datasheet
Corentin Labbe (2):
usb: serial: add support for CH348
usb: serial: add myself as maintainer of CH348
MAINTAINERS | 5 + drivers/usb/serial/Kconfig | 9 + drivers/usb/serial/Makefile | 1 + drivers/usb/serial/ch348.c | 738 ++++++++++++++++++++++++++++++++++++ 4 files changed, 753 insertions(+) create mode 100644 drivers/usb/serial/ch348.c
Signature · 1 line
2.37.4
PATCH v2 1/2 usb: serial: add support for CH348
The CH348 is an USB octo port serial adapter.
This patch adds support for it.
--- drivers/usb/serial/Kconfig | 9 + drivers/usb/serial/Makefile | 1 + drivers/usb/serial/ch348.c | 738 ++++++++++++++++++++++++++++++++++++ 3 files changed, 748 insertions(+) create mode 100644 drivers/usb/serial/ch348.c
drivers/usb/serial/Kconfig +9 -0
@@ -112,6 +112,15 @@ config USB_SERIAL_CH341 To compile this driver as a module, choose M here: the module will be called ch341. +config USB_SERIAL_CH348 + tristate "USB Winchiphead CH348 Octo Port Serial Driver" + help + Say Y here if you want to use a Winchiphead CH348 octo port + USB to serial adapter. + + To compile this driver as a module, choose M here: the + module will be called ch348. + config USB_SERIAL_WHITEHEAT tristate "USB ConnectTech WhiteHEAT Serial Driver" select USB_EZUSB_FX2
drivers/usb/serial/Makefile +1 -0
@@ -15,6 +15,7 @@ obj-$(CONFIG_USB_SERIAL_AIRCABLE) += aircable.o obj-$(CONFIG_USB_SERIAL_ARK3116) += ark3116.o obj-$(CONFIG_USB_SERIAL_BELKIN) += belkin_sa.o obj-$(CONFIG_USB_SERIAL_CH341) += ch341.o +obj-$(CONFIG_USB_SERIAL_CH348) += ch348.o obj-$(CONFIG_USB_SERIAL_CP210X) += cp210x.o obj-$(CONFIG_USB_SERIAL_CYBERJACK) += cyberjack.o obj-$(CONFIG_USB_SERIAL_CYPRESS_M8) += cypress_m8.o
drivers/usb/serial/ch348.c new file +738 -0
738 lines not on this page · raw message
Signature · 1 line
2.37.4
Quoted 3 lines · Corentin Labbe
The CH348 is an USB octo port serial adapter.
This patch adds support for it.
Hi,
comments inline.
Quoted 5 lines
drivers/usb/serial/ch348.c new file +5
-0+struct ch348_rxbuf { +u8 port; +u8 length; +u8 data[]; +} __packed;
drivers/usb/serial/ch348.c line 76
Quoted 10 lines
drivers/usb/serial/ch348.c new file +10
-0+ +#define CH348_RX_PORT_CHUNK_LENGTH 32 +#define CH348_RX_PORT_MAX_LENGTH 30 + +struct ch348_txbuf { +u8 port; +__le16 length; +u8 data[]; +} __packed; +
You know how long data will be. Why leave it unspecified?
drivers/usb/serial/ch348.c line 203
Quoted 9 lines
drivers/usb/serial/ch348.c new file +9
-0+static void ch348_process_read_urb (struct urb *urb) +{ +struct usb_serial_port *port = urb->context; +struct ch348 *ch348 =usb_get_serial_data (port->serial); +u8 *buffer = urb->transfer_buffer, *end; +unsigned int portnum, usblen; +struct ch348_rxbuf *rxb; + +if (!urb->actual_length) {
That check needs to be for < 2 or you can process garbage.
Quoted 7 lines
drivers/usb/serial/ch348.c new file +7
-0+dev_warn (&port->dev,"%s:%d empty rx buffer\n" , __func__, __LINE__); +return ; + } + + end = buffer + urb->actual_length; + +for (; buffer < end; buffer +=CH348_RX_PORT_CHUNK_LENGTH ) {
drivers/usb/serial/ch348.c line 220
Quoted 10 lines
drivers/usb/serial/ch348.c new file +10
-0+ rxb = (struct ch348_rxbuf *)buffer; + portnum = rxb->port; +if (portnum >=CH348_MAXPORT ) { +dev_warn (&port->dev,"%s:%d invalid port %d\n" , + __func__, __LINE__, portnum); +break ; + } + + usblen = rxb->length; +if (usblen >30 ) {
You have defined a nummerical constant for that. Use it.
Quoted 7 lines
drivers/usb/serial/ch348.c new file +7
-0+dev_warn (&port->dev,"%s:%d invalid length %d for port %d\n" , + __func__, __LINE__, usblen, portnum); +break ; + } + + port = ch348->ttyport[portnum].port; +tty_insert_flip_string (&port->port, rxb->data, usblen);
drivers/usb/serial/ch348.c line 237
Quoted 10 lines
drivers/usb/serial/ch348.c new file +10
-0+tty_flip_buffer_push (&port->port); + port->icount.rx += usblen; +usb_serial_debug_data (&port->dev, __func__, usblen, rxb->data); + } +} + +static int ch348_prepare_write_buffer (struct usb_serial_port *port,void *dest,size_t size) +{ +struct ch348_txbuf *rxt = dest; +const size_t txhdrsize = offsetof(struct ch348_txbuf , data);
What is that? This is a constant.
Quoted 9 lines
drivers/usb/serial/ch348.c new file +9
-0+int count; + + count =kfifo_out_locked (&port->write_fifo, rxt->data, + size - txhdrsize, &port->lock); + + rxt->port = port->port_number; + rxt->length =cpu_to_le16 (count); + +return count + txhdrsize;
drivers/usb/serial/ch348.c line 276
Quoted 10 lines
drivers/usb/serial/ch348.c new file +9
-0+} +static void ch348_set_termios (struct tty_struct *tty,struct usb_serial_port *port, +const struct ktermios *termios_old) +{ +struct ch348 *ch348 =usb_get_serial_data (port->serial); +int portnum = port->port_number; +struct ktermios *termios = &tty->termios; +int ret, sent; +__le32 dwDTERate;
OK it is LE.
Quoted 4 lines
drivers/usb/serial/ch348.c new file +4
-0+u8 bCharFormat; +struct ch348_initbuf *buffer; + +if (termios_old && !tty_termios_hw_change (&tty->termios, termios_old))
drivers/usb/serial/ch348.c line 290
Quoted 10 lines
drivers/usb/serial/ch348.c new file +10
-0+return ; + + buffer =kzalloc (sizeof (*buffer),GFP_KERNEL ); +if (!buffer) { +if (termios_old) + tty->termios = *termios_old; +return ; + } + + dwDTERate =tty_get_baud_rate (tty);
This is speed_t, in other words unsigned int without specified endianness.
drivers/usb/serial/ch348.c line 296
Quoted 6 lines
drivers/usb/serial/ch348.c new file +6
-0+/* test show no success on low baud and datasheet said it is not supported */ +if (dwDTERate <1200 ) + dwDTERate =DEFAULT_BAUD_RATE ; +/* datasheet said it is not supported */ +if (dwDTERate >6000000 ) + dwDTERate =6000000 ;
You are comparing to constants in native endianness.
Quoted 16 lines
drivers/usb/serial/ch348.c new file +16
-0+ + bCharFormat = termios->c_cflag &CSTOPB ?2 :1 ; + + buffer->bParityType = termios->c_cflag &PARENB ? + (termios->c_cflag &PARODD ?1 :2 ) + + (termios->c_cflag &CMSPAR ?2 :0 ) :0 ; + +switch (termios->c_cflag &CSIZE ) { +case CS5 : + buffer->bDataBits =5 ; +break ; +case CS6 : + buffer->bDataBits =6 ; +break ; +case CS7 : + buffer->bDataBits =7 ;
drivers/usb/serial/ch348.c line 322
Quoted 10 lines
drivers/usb/serial/ch348.c new file +10
-0+break ; +case CS8 : +default : + buffer->bDataBits =8 ; +break ; + } + buffer->cmd =CMD_WB_E | (portnum &0x0F ); + buffer->reg =R_INIT ; + buffer->port = portnum; + buffer->dwDTERate =cpu_to_be32 (le32_to_cpu (dwDTERate));
So it is native, not LE?
[..]
drivers/usb/serial/ch348.c line 371
Quoted 7 lines
drivers/usb/serial/ch348.c new file +7
-0+ +static int ch348_fixup_port_bulk_in (struct ch348 *ch348,struct usb_serial_port *port) +{ +int i; + +/* Already Initialized */ +if (port->bulk_in_size) {
BTW, shouldn't these be unsigned int?
[..]
Regards
Oliver
Quoted 149 lines · Oliver Neukum
Quoted 3 lines · Corentin Labbe
The CH348 is an USB octo port serial adapter.
This patch adds support for it.Hi,
comments inline.
Quoted 5 lines
drivers/usb/serial/ch348.c new file +5
-0+struct ch348_rxbuf { +u8 port; +u8 length; +u8 data[]; +} __packed;drivers/usb/serial/ch348.c line 76
Quoted 10 lines
drivers/usb/serial/ch348.c new file +10
-0+ +#define CH348_RX_PORT_CHUNK_LENGTH 32 +#define CH348_RX_PORT_MAX_LENGTH 30 + +struct ch348_txbuf { +u8 port; +__le16 length; +u8 data[]; +} __packed; +You know how long data will be. Why leave it unspecified?
drivers/usb/serial/ch348.c line 203
Quoted 9 lines
drivers/usb/serial/ch348.c new file +9
-0+static void ch348_process_read_urb (struct urb *urb) +{ +struct usb_serial_port *port = urb->context; +struct ch348 *ch348 =usb_get_serial_data (port->serial); +u8 *buffer = urb->transfer_buffer, *end; +unsigned int portnum, usblen; +struct ch348_rxbuf *rxb; + +if (!urb->actual_length) {That check needs to be for < 2 or you can process garbage.
Quoted 7 lines
drivers/usb/serial/ch348.c new file +7
-0+dev_warn (&port->dev,"%s:%d empty rx buffer\n" , __func__, __LINE__); +return ; + } + + end = buffer + urb->actual_length; + +for (; buffer < end; buffer +=CH348_RX_PORT_CHUNK_LENGTH ) {drivers/usb/serial/ch348.c line 220
Quoted 10 lines
drivers/usb/serial/ch348.c new file +10
-0+ rxb = (struct ch348_rxbuf *)buffer; + portnum = rxb->port; +if (portnum >=CH348_MAXPORT ) { +dev_warn (&port->dev,"%s:%d invalid port %d\n" , + __func__, __LINE__, portnum); +break ; + } + + usblen = rxb->length; +if (usblen >30 ) {You have defined a nummerical constant for that. Use it.
Quoted 7 lines
drivers/usb/serial/ch348.c new file +7
-0+dev_warn (&port->dev,"%s:%d invalid length %d for port %d\n" , + __func__, __LINE__, usblen, portnum); +break ; + } + + port = ch348->ttyport[portnum].port; +tty_insert_flip_string (&port->port, rxb->data, usblen);drivers/usb/serial/ch348.c line 237
Quoted 10 lines
drivers/usb/serial/ch348.c new file +10
-0+tty_flip_buffer_push (&port->port); + port->icount.rx += usblen; +usb_serial_debug_data (&port->dev, __func__, usblen, rxb->data); + } +} + +static int ch348_prepare_write_buffer (struct usb_serial_port *port,void *dest,size_t size) +{ +struct ch348_txbuf *rxt = dest; +const size_t txhdrsize = offsetof(struct ch348_txbuf , data);What is that? This is a constant.
Quoted 9 lines
drivers/usb/serial/ch348.c new file +9
-0+int count; + + count =kfifo_out_locked (&port->write_fifo, rxt->data, + size - txhdrsize, &port->lock); + + rxt->port = port->port_number; + rxt->length =cpu_to_le16 (count); + +return count + txhdrsize;drivers/usb/serial/ch348.c line 276
Quoted 10 lines
drivers/usb/serial/ch348.c new file +9
-0+} +static void ch348_set_termios (struct tty_struct *tty,struct usb_serial_port *port, +const struct ktermios *termios_old) +{ +struct ch348 *ch348 =usb_get_serial_data (port->serial); +int portnum = port->port_number; +struct ktermios *termios = &tty->termios; +int ret, sent; +__le32 dwDTERate;OK it is LE.
Quoted 4 lines
drivers/usb/serial/ch348.c new file +4
-0+u8 bCharFormat; +struct ch348_initbuf *buffer; + +if (termios_old && !tty_termios_hw_change (&tty->termios, termios_old))drivers/usb/serial/ch348.c line 290
Quoted 10 lines
drivers/usb/serial/ch348.c new file +10
-0+return ; + + buffer =kzalloc (sizeof (*buffer),GFP_KERNEL ); +if (!buffer) { +if (termios_old) + tty->termios = *termios_old; +return ; + } + + dwDTERate =tty_get_baud_rate (tty);This is
speed_t, in other words unsigned int without specified endianness.drivers/usb/serial/ch348.c line 296
Quoted 6 lines
drivers/usb/serial/ch348.c new file +6
-0+/* test show no success on low baud and datasheet said it is not supported */ +if (dwDTERate <1200 ) + dwDTERate =DEFAULT_BAUD_RATE ; +/* datasheet said it is not supported */ +if (dwDTERate >6000000 ) + dwDTERate =6000000 ;You are comparing to constants in native endianness.
Quoted 20 lines
drivers/usb/serial/ch348.c new file +20
-0+ + bCharFormat = termios->c_cflag &CSTOPB ?2 :1 ; + + buffer->bParityType = termios->c_cflag &PARENB ? + (termios->c_cflag &PARODD ?1 :2 ) + + (termios->c_cflag &CMSPAR ?2 :0 ) :0 ; + +switch (termios->c_cflag &CSIZE ) { +case CS5 : + buffer->bDataBits =5 ; +break ; +case CS6 : + buffer->bDataBits =6 ; +break ; +case CS7 : + buffer->bDataBits =7 ; +break ; +case CS8 : +default : + buffer->bDataBits =8 ;
Quoted 9 lines
drivers/usb/serial/ch348.c line 322
Quoted 6 lines
drivers/usb/serial/ch348.c new file +6
-0+break ; + } + buffer->cmd =CMD_WB_E | (portnum &0x0F ); + buffer->reg =R_INIT ; + buffer->port = portnum; + buffer->dwDTERate =cpu_to_be32 (le32_to_cpu (dwDTERate));So it is native, not LE?
Hello
Thanks for your review, I will fix them all.
Quoted 11 lines
[..]
drivers/usb/serial/ch348.c line 371
Quoted 7 lines
drivers/usb/serial/ch348.c new file +7
-0+ +static int ch348_fixup_port_bulk_in (struct ch348 *ch348,struct usb_serial_port *port) +{ +int i; + +/* Already Initialized */ +if (port->bulk_in_size) {BTW, shouldn't these be unsigned int?
I dont understand what you mean here.
Regards
On 26.10.22 09:44, Corentin LABBE wrote:
Hi,
Quoted 2 lines
Thanks for your review, I will fix them all.
I am happy to point out issues.
Quoted 12 lines
Quoted 11 lines
[..]
drivers/usb/serial/ch348.c line 371
Quoted 7 lines
drivers/usb/serial/ch348.c new file +7
-0+ +static int ch348_fixup_port_bulk_in (struct ch348 *ch348,struct usb_serial_port *port) +{ +int i; + +/* Already Initialized */ +if (port->bulk_in_size) {BTW, shouldn't these be unsigned int?
Quoted 1 line
I dont understand what you mean here.
bulk_in_size as a member of the structure is defined as int. As a size cannot meaningfully be negative and this takes no error reports, I was wondering whether it should be declared an unsigned integer for the sake of cleanliness.
Regards
Oliver
PATCH v2 2/2 usb: serial: add myself as maintainer of CH348
In reply to Corentin LabbeSince I did the driver and have hardware to test, set myself as maintainer of it.
--- MAINTAINERS | 5 +++++ 1 file changed, 5 insertions(+)
MAINTAINERS +5 -0
@@ -4847,6 +4847,11 @@ S: Maintained F: drivers/auxdisplay/cfag12864bfb.c F: include/linux/cfag12864b.h +CH348 USB SERIAL ADAPTER +M: Corentin Labbe <[email protected]> +S: Maintained +F: drivers/usb/serial/ch348.c + CHAR and MISC DRIVERS M: Arnd Bergmann <[email protected]> M: Greg Kroah-Hartman <[email protected]>
Signature · 1 line
2.37.4