^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * mxuport.c - MOXA UPort series driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2006 Moxa Technologies Co., Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (c) 2013 Andrew Lunn <andrew@lunn.ch>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Supports the following Moxa USB to serial converters:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * 2 ports : UPort 1250, UPort 1250I
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * 4 ports : UPort 1410, UPort 1450, UPort 1450I
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * 8 ports : UPort 1610-8, UPort 1650-8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * 16 ports : UPort 1610-16, UPort 1650-16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/firmware.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/serial.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/serial_reg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/tty.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/tty_driver.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/tty_flip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/usb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/usb/serial.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <asm/unaligned.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) /* Definitions for the vendor ID and device ID */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #define MX_USBSERIAL_VID 0x110A
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #define MX_UPORT1250_PID 0x1250
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #define MX_UPORT1251_PID 0x1251
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #define MX_UPORT1410_PID 0x1410
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #define MX_UPORT1450_PID 0x1450
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #define MX_UPORT1451_PID 0x1451
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #define MX_UPORT1618_PID 0x1618
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #define MX_UPORT1658_PID 0x1658
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #define MX_UPORT1613_PID 0x1613
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #define MX_UPORT1653_PID 0x1653
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) /* Definitions for USB info */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #define HEADER_SIZE 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #define EVENT_LENGTH 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #define DOWN_BLOCK_SIZE 64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) /* Definitions for firmware info */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #define VER_ADDR_1 0x20
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #define VER_ADDR_2 0x24
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #define VER_ADDR_3 0x28
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) /* Definitions for USB vendor request */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #define RQ_VENDOR_NONE 0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) #define RQ_VENDOR_SET_BAUD 0x01 /* Set baud rate */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #define RQ_VENDOR_SET_LINE 0x02 /* Set line status */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #define RQ_VENDOR_SET_CHARS 0x03 /* Set Xon/Xoff chars */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) #define RQ_VENDOR_SET_RTS 0x04 /* Set RTS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) #define RQ_VENDOR_SET_DTR 0x05 /* Set DTR */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) #define RQ_VENDOR_SET_XONXOFF 0x06 /* Set auto Xon/Xoff */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) #define RQ_VENDOR_SET_RX_HOST_EN 0x07 /* Set RX host enable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) #define RQ_VENDOR_SET_OPEN 0x08 /* Set open/close port */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) #define RQ_VENDOR_PURGE 0x09 /* Purge Rx/Tx buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) #define RQ_VENDOR_SET_MCR 0x0A /* Set MCR register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) #define RQ_VENDOR_SET_BREAK 0x0B /* Set Break signal */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) #define RQ_VENDOR_START_FW_DOWN 0x0C /* Start firmware download */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) #define RQ_VENDOR_STOP_FW_DOWN 0x0D /* Stop firmware download */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) #define RQ_VENDOR_QUERY_FW_READY 0x0E /* Query if new firmware ready */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) #define RQ_VENDOR_SET_FIFO_DISABLE 0x0F /* Set fifo disable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) #define RQ_VENDOR_SET_INTERFACE 0x10 /* Set interface */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) #define RQ_VENDOR_SET_HIGH_PERFOR 0x11 /* Set hi-performance */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) #define RQ_VENDOR_ERASE_BLOCK 0x12 /* Erase flash block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) #define RQ_VENDOR_WRITE_PAGE 0x13 /* Write flash page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) #define RQ_VENDOR_PREPARE_WRITE 0x14 /* Prepare write flash */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) #define RQ_VENDOR_CONFIRM_WRITE 0x15 /* Confirm write flash */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) #define RQ_VENDOR_LOCATE 0x16 /* Locate the device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) #define RQ_VENDOR_START_ROM_DOWN 0x17 /* Start firmware download */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) #define RQ_VENDOR_ROM_DATA 0x18 /* Rom file data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) #define RQ_VENDOR_STOP_ROM_DOWN 0x19 /* Stop firmware download */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) #define RQ_VENDOR_FW_DATA 0x20 /* Firmware data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) #define RQ_VENDOR_RESET_DEVICE 0x23 /* Try to reset the device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) #define RQ_VENDOR_QUERY_FW_CONFIG 0x24
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) #define RQ_VENDOR_GET_VERSION 0x81 /* Get firmware version */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) #define RQ_VENDOR_GET_PAGE 0x82 /* Read flash page */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) #define RQ_VENDOR_GET_ROM_PROC 0x83 /* Get ROM process state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) #define RQ_VENDOR_GET_INQUEUE 0x84 /* Data in input buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) #define RQ_VENDOR_GET_OUTQUEUE 0x85 /* Data in output buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) #define RQ_VENDOR_GET_MSR 0x86 /* Get modem status register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) /* Definitions for UPort event type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) #define UPORT_EVENT_NONE 0 /* None */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) #define UPORT_EVENT_TXBUF_THRESHOLD 1 /* Tx buffer threshold */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) #define UPORT_EVENT_SEND_NEXT 2 /* Send next */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) #define UPORT_EVENT_MSR 3 /* Modem status */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) #define UPORT_EVENT_LSR 4 /* Line status */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) #define UPORT_EVENT_MCR 5 /* Modem control */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) /* Definitions for serial event type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) #define SERIAL_EV_CTS 0x0008 /* CTS changed state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) #define SERIAL_EV_DSR 0x0010 /* DSR changed state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) #define SERIAL_EV_RLSD 0x0020 /* RLSD changed state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) /* Definitions for modem control event type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) #define SERIAL_EV_XOFF 0x40 /* XOFF received */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) /* Definitions for line control of communication */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) #define MX_WORDLENGTH_5 5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) #define MX_WORDLENGTH_6 6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) #define MX_WORDLENGTH_7 7
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) #define MX_WORDLENGTH_8 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) #define MX_PARITY_NONE 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) #define MX_PARITY_ODD 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) #define MX_PARITY_EVEN 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) #define MX_PARITY_MARK 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) #define MX_PARITY_SPACE 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) #define MX_STOP_BITS_1 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) #define MX_STOP_BITS_1_5 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) #define MX_STOP_BITS_2 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) #define MX_RTS_DISABLE 0x0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) #define MX_RTS_ENABLE 0x1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) #define MX_RTS_HW 0x2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) #define MX_RTS_NO_CHANGE 0x3 /* Flag, not valid register value*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) #define MX_INT_RS232 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) #define MX_INT_2W_RS485 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) #define MX_INT_RS422 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) #define MX_INT_4W_RS485 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) /* Definitions for holding reason */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) #define MX_WAIT_FOR_CTS 0x0001
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) #define MX_WAIT_FOR_DSR 0x0002
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) #define MX_WAIT_FOR_DCD 0x0004
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) #define MX_WAIT_FOR_XON 0x0008
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) #define MX_WAIT_FOR_START_TX 0x0010
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) #define MX_WAIT_FOR_UNTHROTTLE 0x0020
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) #define MX_WAIT_FOR_LOW_WATER 0x0040
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) #define MX_WAIT_FOR_SEND_NEXT 0x0080
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) #define MX_UPORT_2_PORT BIT(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) #define MX_UPORT_4_PORT BIT(1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) #define MX_UPORT_8_PORT BIT(2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) #define MX_UPORT_16_PORT BIT(3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) /* This structure holds all of the local port information */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) struct mxuport_port {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) u8 mcr_state; /* Last MCR state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) u8 msr_state; /* Last MSR state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) struct mutex mutex; /* Protects mcr_state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) spinlock_t spinlock; /* Protects msr_state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) /* Table of devices that work with this driver */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) static const struct usb_device_id mxuport_idtable[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) { USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1250_PID),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) .driver_info = MX_UPORT_2_PORT },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) { USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1251_PID),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) .driver_info = MX_UPORT_2_PORT },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) { USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1410_PID),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) .driver_info = MX_UPORT_4_PORT },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) { USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1450_PID),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) .driver_info = MX_UPORT_4_PORT },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) { USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1451_PID),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) .driver_info = MX_UPORT_4_PORT },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) { USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1618_PID),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) .driver_info = MX_UPORT_8_PORT },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) { USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1658_PID),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) .driver_info = MX_UPORT_8_PORT },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) { USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1613_PID),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) .driver_info = MX_UPORT_16_PORT },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) { USB_DEVICE(MX_USBSERIAL_VID, MX_UPORT1653_PID),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) .driver_info = MX_UPORT_16_PORT },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) {} /* Terminating entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) MODULE_DEVICE_TABLE(usb, mxuport_idtable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) * Add a four byte header containing the port number and the number of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) * bytes of data in the message. Return the number of bytes in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) * buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) static int mxuport_prepare_write_buffer(struct usb_serial_port *port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) void *dest, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) u8 *buf = dest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) int count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) count = kfifo_out_locked(&port->write_fifo, buf + HEADER_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) size - HEADER_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) &port->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) put_unaligned_be16(port->port_number, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) put_unaligned_be16(count, buf + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) dev_dbg(&port->dev, "%s - size %zd count %d\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) size, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) return count + HEADER_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) /* Read the given buffer in from the control pipe. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) static int mxuport_recv_ctrl_urb(struct usb_serial *serial,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) u8 request, u16 value, u16 index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) u8 *data, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) status = usb_control_msg(serial->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) usb_rcvctrlpipe(serial->dev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) request,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) (USB_DIR_IN | USB_TYPE_VENDOR |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) USB_RECIP_DEVICE), value, index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) data, size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) USB_CTRL_GET_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) dev_err(&serial->interface->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) "%s - usb_control_msg failed (%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) __func__, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) if (status != size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) dev_err(&serial->interface->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) "%s - short read (%d / %zd)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) __func__, status, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) /* Write the given buffer out to the control pipe. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) static int mxuport_send_ctrl_data_urb(struct usb_serial *serial,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) u8 request,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) u16 value, u16 index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) u8 *data, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) status = usb_control_msg(serial->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) usb_sndctrlpipe(serial->dev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) request,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) (USB_DIR_OUT | USB_TYPE_VENDOR |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) USB_RECIP_DEVICE), value, index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) data, size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) USB_CTRL_SET_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) dev_err(&serial->interface->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) "%s - usb_control_msg failed (%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) __func__, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) if (status != size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) dev_err(&serial->interface->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) "%s - short write (%d / %zd)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) __func__, status, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) /* Send a vendor request without any data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) static int mxuport_send_ctrl_urb(struct usb_serial *serial,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) u8 request, u16 value, u16 index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) return mxuport_send_ctrl_data_urb(serial, request, value, index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) * mxuport_throttle - throttle function of driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) * This function is called by the tty driver when it wants to stop the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) * data being read from the port. Since all the data comes over one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) * bulk in endpoint, we cannot stop submitting urbs by setting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) * port->throttle. Instead tell the device to stop sending us data for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) * the port.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) static void mxuport_throttle(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) struct usb_serial_port *port = tty->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) struct usb_serial *serial = port->serial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) dev_dbg(&port->dev, "%s\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RX_HOST_EN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 0, port->port_number);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) * mxuport_unthrottle - unthrottle function of driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) * This function is called by the tty driver when it wants to resume
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) * the data being read from the port. Tell the device it can resume
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) * sending us received data from the port.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) static void mxuport_unthrottle(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) struct usb_serial_port *port = tty->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) struct usb_serial *serial = port->serial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) dev_dbg(&port->dev, "%s\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RX_HOST_EN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 1, port->port_number);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) * Processes one chunk of data received for a port. Mostly a copy of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) * usb_serial_generic_process_read_urb().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) static void mxuport_process_read_urb_data(struct usb_serial_port *port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) char *data, int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) if (port->sysrq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) for (i = 0; i < size; i++, data++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) if (!usb_serial_handle_sysrq_char(port, *data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) tty_insert_flip_char(&port->port, *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) TTY_NORMAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) tty_insert_flip_string(&port->port, data, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) tty_flip_buffer_push(&port->port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) static void mxuport_msr_event(struct usb_serial_port *port, u8 buf[4])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) struct mxuport_port *mxport = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) u8 rcv_msr_hold = buf[2] & 0xF0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) u16 rcv_msr_event = get_unaligned_be16(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) if (rcv_msr_event == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) /* Update MSR status */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) spin_lock_irqsave(&mxport->spinlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) dev_dbg(&port->dev, "%s - current MSR status = 0x%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) __func__, mxport->msr_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) if (rcv_msr_hold & UART_MSR_CTS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) mxport->msr_state |= UART_MSR_CTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) dev_dbg(&port->dev, "%s - CTS high\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) mxport->msr_state &= ~UART_MSR_CTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) dev_dbg(&port->dev, "%s - CTS low\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) if (rcv_msr_hold & UART_MSR_DSR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) mxport->msr_state |= UART_MSR_DSR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) dev_dbg(&port->dev, "%s - DSR high\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) mxport->msr_state &= ~UART_MSR_DSR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) dev_dbg(&port->dev, "%s - DSR low\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) if (rcv_msr_hold & UART_MSR_DCD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) mxport->msr_state |= UART_MSR_DCD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) dev_dbg(&port->dev, "%s - DCD high\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) mxport->msr_state &= ~UART_MSR_DCD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) dev_dbg(&port->dev, "%s - DCD low\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) spin_unlock_irqrestore(&mxport->spinlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) if (rcv_msr_event &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) (SERIAL_EV_CTS | SERIAL_EV_DSR | SERIAL_EV_RLSD)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) if (rcv_msr_event & SERIAL_EV_CTS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) port->icount.cts++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) dev_dbg(&port->dev, "%s - CTS change\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) if (rcv_msr_event & SERIAL_EV_DSR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) port->icount.dsr++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) dev_dbg(&port->dev, "%s - DSR change\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) if (rcv_msr_event & SERIAL_EV_RLSD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) port->icount.dcd++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) dev_dbg(&port->dev, "%s - DCD change\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) wake_up_interruptible(&port->port.delta_msr_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) static void mxuport_lsr_event(struct usb_serial_port *port, u8 buf[4])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) u8 lsr_event = buf[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) if (lsr_event & UART_LSR_BI) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) port->icount.brk++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) dev_dbg(&port->dev, "%s - break error\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) if (lsr_event & UART_LSR_FE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) port->icount.frame++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) dev_dbg(&port->dev, "%s - frame error\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) if (lsr_event & UART_LSR_PE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) port->icount.parity++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) dev_dbg(&port->dev, "%s - parity error\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) if (lsr_event & UART_LSR_OE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) port->icount.overrun++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) dev_dbg(&port->dev, "%s - overrun error\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) * When something interesting happens, modem control lines XON/XOFF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) * etc, the device sends an event. Process these events.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) static void mxuport_process_read_urb_event(struct usb_serial_port *port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) u8 buf[4], u32 event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) dev_dbg(&port->dev, "%s - receive event : %04x\n", __func__, event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) switch (event) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) case UPORT_EVENT_SEND_NEXT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) * Sent as part of the flow control on device buffers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) * Not currently used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) case UPORT_EVENT_MSR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) mxuport_msr_event(port, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) case UPORT_EVENT_LSR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) mxuport_lsr_event(port, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) case UPORT_EVENT_MCR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) * Event to indicate a change in XON/XOFF from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) * peer. Currently not used. We just continue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) * sending the device data and it will buffer it if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) * needed. This event could be used for flow control
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) * between the host and the device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) dev_dbg(&port->dev, "Unexpected event\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) * One URB can contain data for multiple ports. Demultiplex the data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) * checking the port exists, is opened and the message is valid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) static void mxuport_process_read_urb_demux_data(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) struct usb_serial_port *port = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) struct usb_serial *serial = port->serial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) u8 *data = urb->transfer_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) u8 *end = data + urb->actual_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) struct usb_serial_port *demux_port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) u8 *ch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) u16 rcv_port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) u16 rcv_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) while (data < end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) if (data + HEADER_SIZE > end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) dev_warn(&port->dev, "%s - message with short header\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) rcv_port = get_unaligned_be16(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) if (rcv_port >= serial->num_ports) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) dev_warn(&port->dev, "%s - message for invalid port\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) demux_port = serial->port[rcv_port];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) rcv_len = get_unaligned_be16(data + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) if (!rcv_len || data + HEADER_SIZE + rcv_len > end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) dev_warn(&port->dev, "%s - short data\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) if (tty_port_initialized(&demux_port->port)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) ch = data + HEADER_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) mxuport_process_read_urb_data(demux_port, ch, rcv_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) dev_dbg(&demux_port->dev, "%s - data for closed port\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) data += HEADER_SIZE + rcv_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) * One URB can contain events for multiple ports. Demultiplex the event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) * checking the port exists, and is opened.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) static void mxuport_process_read_urb_demux_event(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) struct usb_serial_port *port = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) struct usb_serial *serial = port->serial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) u8 *data = urb->transfer_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) u8 *end = data + urb->actual_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) struct usb_serial_port *demux_port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) u8 *ch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) u16 rcv_port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) u16 rcv_event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) while (data < end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) if (data + EVENT_LENGTH > end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) dev_warn(&port->dev, "%s - message with short event\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) rcv_port = get_unaligned_be16(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) if (rcv_port >= serial->num_ports) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) dev_warn(&port->dev, "%s - message for invalid port\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) demux_port = serial->port[rcv_port];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) if (tty_port_initialized(&demux_port->port)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) ch = data + HEADER_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) rcv_event = get_unaligned_be16(data + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) mxuport_process_read_urb_event(demux_port, ch,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) rcv_event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) dev_dbg(&demux_port->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) "%s - event for closed port\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) data += EVENT_LENGTH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) * This is called when we have received data on the bulk in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) * endpoint. Depending on which port it was received on, it can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) * contain serial data or events.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) static void mxuport_process_read_urb(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) struct usb_serial_port *port = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) struct usb_serial *serial = port->serial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) if (port == serial->port[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) mxuport_process_read_urb_demux_data(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) if (port == serial->port[1])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) mxuport_process_read_urb_demux_event(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) * Ask the device how many bytes it has queued to be sent out. If
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) * there are none, return true.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) static bool mxuport_tx_empty(struct usb_serial_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) struct usb_serial *serial = port->serial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) bool is_empty = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) u32 txlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) u8 *len_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) len_buf = kzalloc(4, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) if (!len_buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) err = mxuport_recv_ctrl_urb(serial, RQ_VENDOR_GET_OUTQUEUE, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) port->port_number, len_buf, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) txlen = get_unaligned_be32(len_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) dev_dbg(&port->dev, "%s - tx len = %u\n", __func__, txlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) if (txlen != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) is_empty = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) kfree(len_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) return is_empty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) static int mxuport_set_mcr(struct usb_serial_port *port, u8 mcr_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) struct usb_serial *serial = port->serial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) dev_dbg(&port->dev, "%s - %02x\n", __func__, mcr_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_MCR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) mcr_state, port->port_number);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) dev_err(&port->dev, "%s - failed to change MCR\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) static int mxuport_set_dtr(struct usb_serial_port *port, int on)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) struct mxuport_port *mxport = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) struct usb_serial *serial = port->serial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) mutex_lock(&mxport->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_DTR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) !!on, port->port_number);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) if (on)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) mxport->mcr_state |= UART_MCR_DTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) mxport->mcr_state &= ~UART_MCR_DTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) mutex_unlock(&mxport->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) static int mxuport_set_rts(struct usb_serial_port *port, u8 state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) struct mxuport_port *mxport = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) struct usb_serial *serial = port->serial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) u8 mcr_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) mutex_lock(&mxport->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) mcr_state = mxport->mcr_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) switch (state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) case MX_RTS_DISABLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) mcr_state &= ~UART_MCR_RTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) case MX_RTS_ENABLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) mcr_state |= UART_MCR_RTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) case MX_RTS_HW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) * Do not update mxport->mcr_state when doing hardware
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) * flow control.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) * Should not happen, but somebody might try passing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) * MX_RTS_NO_CHANGE, which is not valid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RTS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) state, port->port_number);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) mxport->mcr_state = mcr_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) mutex_unlock(&mxport->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) static void mxuport_dtr_rts(struct usb_serial_port *port, int on)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) struct mxuport_port *mxport = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) u8 mcr_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) mutex_lock(&mxport->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) mcr_state = mxport->mcr_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) if (on)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) mcr_state |= (UART_MCR_RTS | UART_MCR_DTR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) mcr_state &= ~(UART_MCR_RTS | UART_MCR_DTR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) err = mxuport_set_mcr(port, mcr_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) mxport->mcr_state = mcr_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) mutex_unlock(&mxport->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) static int mxuport_tiocmset(struct tty_struct *tty, unsigned int set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) unsigned int clear)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) struct usb_serial_port *port = tty->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) struct mxuport_port *mxport = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) u8 mcr_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) mutex_lock(&mxport->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) mcr_state = mxport->mcr_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) if (set & TIOCM_RTS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) mcr_state |= UART_MCR_RTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) if (set & TIOCM_DTR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) mcr_state |= UART_MCR_DTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) if (clear & TIOCM_RTS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) mcr_state &= ~UART_MCR_RTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) if (clear & TIOCM_DTR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) mcr_state &= ~UART_MCR_DTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) err = mxuport_set_mcr(port, mcr_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) mxport->mcr_state = mcr_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) mutex_unlock(&mxport->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) static int mxuport_tiocmget(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) struct mxuport_port *mxport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) struct usb_serial_port *port = tty->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) unsigned int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) unsigned int msr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) unsigned int mcr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) mxport = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) mutex_lock(&mxport->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) spin_lock_irqsave(&mxport->spinlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) msr = mxport->msr_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) mcr = mxport->mcr_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) spin_unlock_irqrestore(&mxport->spinlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) mutex_unlock(&mxport->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) result = (((mcr & UART_MCR_DTR) ? TIOCM_DTR : 0) | /* 0x002 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) ((mcr & UART_MCR_RTS) ? TIOCM_RTS : 0) | /* 0x004 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) ((msr & UART_MSR_CTS) ? TIOCM_CTS : 0) | /* 0x020 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) ((msr & UART_MSR_DCD) ? TIOCM_CAR : 0) | /* 0x040 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) ((msr & UART_MSR_RI) ? TIOCM_RI : 0) | /* 0x080 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) ((msr & UART_MSR_DSR) ? TIOCM_DSR : 0)); /* 0x100 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) dev_dbg(&port->dev, "%s - 0x%04x\n", __func__, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) static int mxuport_set_termios_flow(struct tty_struct *tty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) struct ktermios *old_termios,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) struct usb_serial_port *port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) struct usb_serial *serial)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) u8 xon = START_CHAR(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) u8 xoff = STOP_CHAR(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) int enable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) u8 *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) u8 rts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) buf = kmalloc(2, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) /* S/W flow control settings */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) if (I_IXOFF(tty) || I_IXON(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) enable = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) buf[0] = xon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) buf[1] = xoff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) err = mxuport_send_ctrl_data_urb(serial, RQ_VENDOR_SET_CHARS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) 0, port->port_number,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) buf, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) dev_dbg(&port->dev, "%s - XON = 0x%02x, XOFF = 0x%02x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) __func__, xon, xoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) enable = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_XONXOFF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) enable, port->port_number);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) rts = MX_RTS_NO_CHANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) /* H/W flow control settings */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) if (!old_termios ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) C_CRTSCTS(tty) != (old_termios->c_cflag & CRTSCTS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) if (C_CRTSCTS(tty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) rts = MX_RTS_HW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) rts = MX_RTS_ENABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) if (C_BAUD(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) if (old_termios && (old_termios->c_cflag & CBAUD) == B0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) /* Raise DTR and RTS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) if (C_CRTSCTS(tty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) rts = MX_RTS_HW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) rts = MX_RTS_ENABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) mxuport_set_dtr(port, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) /* Drop DTR and RTS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) rts = MX_RTS_DISABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) mxuport_set_dtr(port, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) if (rts != MX_RTS_NO_CHANGE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) err = mxuport_set_rts(port, rts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) static void mxuport_set_termios(struct tty_struct *tty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) struct usb_serial_port *port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) struct ktermios *old_termios)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) struct usb_serial *serial = port->serial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) u8 *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) u8 data_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) u8 stop_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) u8 parity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) int baud;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) if (old_termios &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) !tty_termios_hw_change(&tty->termios, old_termios) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) tty->termios.c_iflag == old_termios->c_iflag) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) dev_dbg(&port->dev, "%s - nothing to change\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) buf = kmalloc(4, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) /* Set data bit of termios */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) switch (C_CSIZE(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) case CS5:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) data_bits = MX_WORDLENGTH_5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) case CS6:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) data_bits = MX_WORDLENGTH_6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) case CS7:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) data_bits = MX_WORDLENGTH_7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) case CS8:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) data_bits = MX_WORDLENGTH_8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) /* Set parity of termios */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) if (C_PARENB(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) if (C_CMSPAR(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) if (C_PARODD(tty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) parity = MX_PARITY_MARK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) parity = MX_PARITY_SPACE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) if (C_PARODD(tty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) parity = MX_PARITY_ODD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) parity = MX_PARITY_EVEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) parity = MX_PARITY_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) /* Set stop bit of termios */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) if (C_CSTOPB(tty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) stop_bits = MX_STOP_BITS_2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) stop_bits = MX_STOP_BITS_1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) buf[0] = data_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) buf[1] = parity;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) buf[2] = stop_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) buf[3] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) err = mxuport_send_ctrl_data_urb(serial, RQ_VENDOR_SET_LINE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) 0, port->port_number, buf, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) err = mxuport_set_termios_flow(tty, old_termios, port, serial);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) baud = tty_get_baud_rate(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) if (!baud)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) baud = 9600;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) /* Note: Little Endian */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) put_unaligned_le32(baud, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) err = mxuport_send_ctrl_data_urb(serial, RQ_VENDOR_SET_BAUD,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) 0, port->port_number,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) buf, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) dev_dbg(&port->dev, "baud_rate : %d\n", baud);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) dev_dbg(&port->dev, "data_bits : %d\n", data_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) dev_dbg(&port->dev, "parity : %d\n", parity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) dev_dbg(&port->dev, "stop_bits : %d\n", stop_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) * Determine how many ports this device has dynamically. It will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) * called after the probe() callback is called, but before attach().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) static int mxuport_calc_num_ports(struct usb_serial *serial,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) struct usb_serial_endpoints *epds)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) unsigned long features = (unsigned long)usb_get_serial_data(serial);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) int num_ports;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) if (features & MX_UPORT_2_PORT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) num_ports = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) } else if (features & MX_UPORT_4_PORT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) num_ports = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) } else if (features & MX_UPORT_8_PORT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) num_ports = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) } else if (features & MX_UPORT_16_PORT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) num_ports = 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) dev_warn(&serial->interface->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) "unknown device, assuming two ports\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) num_ports = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) * Setup bulk-out endpoint multiplexing. All ports share the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) * bulk-out endpoint.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) BUILD_BUG_ON(ARRAY_SIZE(epds->bulk_out) < 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) for (i = 1; i < num_ports; ++i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) epds->bulk_out[i] = epds->bulk_out[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) epds->num_bulk_out = num_ports;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) return num_ports;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) /* Get the version of the firmware currently running. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) static int mxuport_get_fw_version(struct usb_serial *serial, u32 *version)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) u8 *ver_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) ver_buf = kzalloc(4, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) if (!ver_buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) /* Get firmware version from SDRAM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) err = mxuport_recv_ctrl_urb(serial, RQ_VENDOR_GET_VERSION, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) ver_buf, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) if (err != 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) *version = (ver_buf[0] << 16) | (ver_buf[1] << 8) | ver_buf[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) kfree(ver_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) /* Given a firmware blob, download it to the device. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) static int mxuport_download_fw(struct usb_serial *serial,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) const struct firmware *fw_p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) u8 *fw_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) size_t txlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) size_t fwidx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) fw_buf = kmalloc(DOWN_BLOCK_SIZE, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) if (!fw_buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) dev_dbg(&serial->interface->dev, "Starting firmware download...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_START_FW_DOWN, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) fwidx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) txlen = min_t(size_t, (fw_p->size - fwidx), DOWN_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) memcpy(fw_buf, &fw_p->data[fwidx], txlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) err = mxuport_send_ctrl_data_urb(serial, RQ_VENDOR_FW_DATA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 0, 0, fw_buf, txlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) mxuport_send_ctrl_urb(serial, RQ_VENDOR_STOP_FW_DOWN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) fwidx += txlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) usleep_range(1000, 2000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) } while (fwidx < fw_p->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) msleep(1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_STOP_FW_DOWN, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) msleep(1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_QUERY_FW_READY, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) kfree(fw_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) static int mxuport_probe(struct usb_serial *serial,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) const struct usb_device_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) u16 productid = le16_to_cpu(serial->dev->descriptor.idProduct);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) const struct firmware *fw_p = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) u32 version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) int local_ver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) char buf[32];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) /* Load our firmware */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_QUERY_FW_CONFIG, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) mxuport_send_ctrl_urb(serial, RQ_VENDOR_RESET_DEVICE, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) err = mxuport_get_fw_version(serial, &version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) dev_dbg(&serial->interface->dev, "Device firmware version v%x.%x.%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) (version & 0xff0000) >> 16,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) (version & 0xff00) >> 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) (version & 0xff));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) snprintf(buf, sizeof(buf) - 1, "moxa/moxa-%04x.fw", productid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) err = request_firmware(&fw_p, buf, &serial->interface->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) dev_warn(&serial->interface->dev, "Firmware %s not found\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) /* Use the firmware already in the device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) local_ver = ((fw_p->data[VER_ADDR_1] << 16) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) (fw_p->data[VER_ADDR_2] << 8) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) fw_p->data[VER_ADDR_3]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) dev_dbg(&serial->interface->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) "Available firmware version v%x.%x.%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) fw_p->data[VER_ADDR_1], fw_p->data[VER_ADDR_2],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) fw_p->data[VER_ADDR_3]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) if (local_ver > version) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) err = mxuport_download_fw(serial, fw_p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) err = mxuport_get_fw_version(serial, &version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) dev_info(&serial->interface->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) "Using device firmware version v%x.%x.%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) (version & 0xff0000) >> 16,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) (version & 0xff00) >> 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) (version & 0xff));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) * Contains the features of this hardware. Store away for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) * later use, eg, number of ports.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) usb_set_serial_data(serial, (void *)id->driver_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) if (fw_p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) release_firmware(fw_p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) static int mxuport_port_probe(struct usb_serial_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) struct usb_serial *serial = port->serial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) struct mxuport_port *mxport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) mxport = devm_kzalloc(&port->dev, sizeof(struct mxuport_port),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) if (!mxport)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) mutex_init(&mxport->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) spin_lock_init(&mxport->spinlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) /* Set the port private data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) usb_set_serial_port_data(port, mxport);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) /* Set FIFO (Enable) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_FIFO_DISABLE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 0, port->port_number);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) /* Set transmission mode (Hi-Performance) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_HIGH_PERFOR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 0, port->port_number);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) /* Set interface (RS-232) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) return mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_INTERFACE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) MX_INT_RS232,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) port->port_number);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) static int mxuport_attach(struct usb_serial *serial)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) struct usb_serial_port *port0 = serial->port[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) struct usb_serial_port *port1 = serial->port[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) * All data from the ports is received on the first bulk in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) * endpoint, with a multiplex header. The second bulk in is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) * used for events.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) * Start to read from the device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) err = usb_serial_generic_submit_read_urbs(port0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) err = usb_serial_generic_submit_read_urbs(port1, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) usb_serial_generic_close(port0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) static void mxuport_release(struct usb_serial *serial)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) struct usb_serial_port *port0 = serial->port[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) struct usb_serial_port *port1 = serial->port[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) usb_serial_generic_close(port1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) usb_serial_generic_close(port0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) static int mxuport_open(struct tty_struct *tty, struct usb_serial_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) struct mxuport_port *mxport = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) struct usb_serial *serial = port->serial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) /* Set receive host (enable) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RX_HOST_EN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 1, port->port_number);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) err = mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_OPEN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 1, port->port_number);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RX_HOST_EN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 0, port->port_number);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) /* Initial port termios */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) if (tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) mxuport_set_termios(tty, port, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) * TODO: use RQ_VENDOR_GET_MSR, once we know what it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) * returns.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) mxport->msr_state = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) static void mxuport_close(struct usb_serial_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) struct usb_serial *serial = port->serial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_OPEN, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) port->port_number);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_RX_HOST_EN, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) port->port_number);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) /* Send a break to the port. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) static void mxuport_break_ctl(struct tty_struct *tty, int break_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) struct usb_serial_port *port = tty->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) struct usb_serial *serial = port->serial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) int enable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) if (break_state == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) enable = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) dev_dbg(&port->dev, "%s - sending break\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) enable = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) dev_dbg(&port->dev, "%s - clearing break\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) mxuport_send_ctrl_urb(serial, RQ_VENDOR_SET_BREAK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) enable, port->port_number);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) static int mxuport_resume(struct usb_serial *serial)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) struct usb_serial_port *port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) int c = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) for (i = 0; i < 2; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) port = serial->port[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) r = usb_serial_generic_submit_read_urbs(port, GFP_NOIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) c++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) for (i = 0; i < serial->num_ports; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) port = serial->port[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) if (!tty_port_initialized(&port->port))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) r = usb_serial_generic_write_start(port, GFP_NOIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) c++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) return c ? -EIO : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) static struct usb_serial_driver mxuport_device = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) .name = "mxuport",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) .description = "MOXA UPort",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) .id_table = mxuport_idtable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) .num_bulk_in = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) .num_bulk_out = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) .probe = mxuport_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) .port_probe = mxuport_port_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) .attach = mxuport_attach,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) .release = mxuport_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) .calc_num_ports = mxuport_calc_num_ports,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) .open = mxuport_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) .close = mxuport_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) .set_termios = mxuport_set_termios,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) .break_ctl = mxuport_break_ctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) .tx_empty = mxuport_tx_empty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) .tiocmiwait = usb_serial_generic_tiocmiwait,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) .get_icount = usb_serial_generic_get_icount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) .throttle = mxuport_throttle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) .unthrottle = mxuport_unthrottle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) .tiocmget = mxuport_tiocmget,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) .tiocmset = mxuport_tiocmset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) .dtr_rts = mxuport_dtr_rts,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) .process_read_urb = mxuport_process_read_urb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) .prepare_write_buffer = mxuport_prepare_write_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) .resume = mxuport_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) static struct usb_serial_driver *const serial_drivers[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) &mxuport_device, NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) module_usb_serial_driver(serial_drivers, mxuport_idtable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) MODULE_AUTHOR("Andrew Lunn <andrew@lunn.ch>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) MODULE_AUTHOR("<support@moxa.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) MODULE_LICENSE("GPL");