Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^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)  * usb-serial driver for Quatech USB 2 devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2012 Bill Pemberton (wfp5p@virginia.edu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *  These devices all have only 1 bulk in and 1 bulk out that is shared
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *  for all serial ports.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <asm/unaligned.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/tty.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/tty_driver.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/tty_flip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/module.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/usb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/usb/serial.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/serial_reg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) /* default urb timeout for usb operations */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #define QT2_USB_TIMEOUT USB_CTRL_SET_TIMEOUT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #define QT_OPEN_CLOSE_CHANNEL       0xca
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #define QT_SET_GET_DEVICE           0xc2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #define QT_SET_GET_REGISTER         0xc0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #define QT_GET_SET_PREBUF_TRIG_LVL  0xcc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define QT_SET_ATF                  0xcd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #define QT_TRANSFER_IN              0xc0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #define QT_HW_FLOW_CONTROL_MASK     0xc5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define QT_SW_FLOW_CONTROL_MASK     0xc6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #define QT2_BREAK_CONTROL	    0xc8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #define QT2_GET_SET_UART            0xc1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define QT2_FLUSH_DEVICE	    0xc4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #define QT2_GET_SET_QMCR            0xe1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #define QT2_QMCR_RS232              0x40
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #define QT2_QMCR_RS422              0x10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #define  SERIAL_CRTSCTS ((UART_MCR_RTS << 8) | UART_MSR_CTS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) #define  SERIAL_EVEN_PARITY         (UART_LCR_PARITY | UART_LCR_EPAR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) /* status bytes for the device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) #define QT2_CONTROL_BYTE    0x1b
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) #define QT2_LINE_STATUS     0x00  /* following 1 byte is line status */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) #define QT2_MODEM_STATUS    0x01  /* following 1 byte is modem status */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) #define QT2_XMIT_HOLD       0x02  /* following 2 bytes are ?? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) #define QT2_CHANGE_PORT     0x03  /* following 1 byte is port to change to */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) #define QT2_REC_FLUSH       0x04  /* no following info */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) #define QT2_XMIT_FLUSH      0x05  /* no following info */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) #define QT2_CONTROL_ESCAPE  0xff  /* pass through previous 2 control bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) #define  MAX_BAUD_RATE              921600
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) #define  DEFAULT_BAUD_RATE          9600
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) #define QT2_READ_BUFFER_SIZE    512  /* size of read buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) #define QT2_WRITE_BUFFER_SIZE   512  /* size of write buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) #define QT2_WRITE_CONTROL_SIZE  5    /* control bytes used for a write */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) #define DRIVER_DESC "Quatech 2nd gen USB to Serial Driver"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) #define	USB_VENDOR_ID_QUATECH	0x061d
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) #define QUATECH_SSU2_100	0xC120	/* RS232 single port */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) #define QUATECH_DSU2_100	0xC140	/* RS232 dual port */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) #define QUATECH_DSU2_400	0xC150	/* RS232/422/485 dual port */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) #define QUATECH_QSU2_100	0xC160	/* RS232 four port */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) #define QUATECH_QSU2_400	0xC170	/* RS232/422/485 four port */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) #define QUATECH_ESU2_100	0xC1A0	/* RS232 eight port */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) #define QUATECH_ESU2_400	0xC180	/* RS232/422/485 eight port */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) struct qt2_device_detail {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	int product_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	int num_ports;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) #define QT_DETAILS(prod, ports)	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	.product_id = (prod),   \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	.num_ports = (ports)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) static const struct qt2_device_detail qt2_device_details[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	{QT_DETAILS(QUATECH_SSU2_100, 1)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	{QT_DETAILS(QUATECH_DSU2_400, 2)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	{QT_DETAILS(QUATECH_DSU2_100, 2)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	{QT_DETAILS(QUATECH_QSU2_400, 4)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	{QT_DETAILS(QUATECH_QSU2_100, 4)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	{QT_DETAILS(QUATECH_ESU2_400, 8)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	{QT_DETAILS(QUATECH_ESU2_100, 8)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	{QT_DETAILS(0, 0)}	/* Terminating entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) static const struct usb_device_id id_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	{USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_SSU2_100)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	{USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_DSU2_100)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	{USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_DSU2_400)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	{USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_QSU2_100)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	{USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_QSU2_400)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	{USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU2_100)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	{USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_ESU2_400)},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	{}			/* Terminating entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) MODULE_DEVICE_TABLE(usb, id_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) struct qt2_serial_private {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	unsigned char current_port;  /* current port for incoming data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	struct urb	*read_urb;   /* shared among all ports */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	char		*read_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) struct qt2_port_private {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	u8   device_port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	spinlock_t urb_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	bool       urb_in_use;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	struct urb *write_urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	char       *write_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	spinlock_t  lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	u8          shadowLSR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	u8          shadowMSR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	struct usb_serial_port *port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) static void qt2_update_lsr(struct usb_serial_port *port, unsigned char *ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) static void qt2_update_msr(struct usb_serial_port *port, unsigned char *ch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) static void qt2_write_bulk_callback(struct urb *urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) static void qt2_read_bulk_callback(struct urb *urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) static void qt2_release(struct usb_serial *serial)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	struct qt2_serial_private *serial_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	serial_priv = usb_get_serial_data(serial);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	usb_kill_urb(serial_priv->read_urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	usb_free_urb(serial_priv->read_urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	kfree(serial_priv->read_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	kfree(serial_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) static inline int calc_baud_divisor(int baudrate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	int divisor, rem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	divisor = MAX_BAUD_RATE / baudrate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	rem = MAX_BAUD_RATE % baudrate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	/* Round to nearest divisor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	if (((rem * 2) >= baudrate) && (baudrate != 110))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		divisor++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	return divisor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) static inline int qt2_set_port_config(struct usb_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 				      unsigned char port_number,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 				      u16 baudrate, u16 lcr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	int divisor = calc_baud_divisor(baudrate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	u16 index = ((u16) (lcr << 8) | (u16) (port_number));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 			       QT2_GET_SET_UART, 0x40,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 			       divisor, index, NULL, 0, QT2_USB_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) static inline int qt2_control_msg(struct usb_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 				  u8 request, u16 data, u16 index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 			       request, 0x40, data, index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 			       NULL, 0, QT2_USB_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) static inline int qt2_setdevice(struct usb_device *dev, u8 *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	u16 x = ((u16) (data[1] << 8) | (u16) (data[0]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	return qt2_control_msg(dev, QT_SET_GET_DEVICE, x, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) static inline int qt2_getregister(struct usb_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 				  u8 uart,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 				  u8 reg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 				  u8 *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 			      QT_SET_GET_REGISTER, 0xc0, reg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 			      uart, data, sizeof(*data), QT2_USB_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	if (ret < (int)sizeof(*data)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		if (ret >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 			ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) static inline int qt2_setregister(struct usb_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 				  u8 uart, u8 reg, u16 data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	u16 value = (data << 8) | reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 			       QT_SET_GET_REGISTER, 0x40, value, uart,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 			       NULL, 0, QT2_USB_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) static inline int update_mctrl(struct qt2_port_private *port_priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 			       unsigned int set, unsigned int clear)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	struct usb_serial_port *port = port_priv->port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	struct usb_device *dev = port->serial->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	unsigned urb_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	if (((set | clear) & (TIOCM_DTR | TIOCM_RTS)) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		dev_dbg(&port->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 			"update_mctrl - DTR|RTS not being set|cleared\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		return 0;	/* no change */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	clear &= ~set;	/* 'set' takes precedence over 'clear' */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	urb_value = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	if (set & TIOCM_DTR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		urb_value |= UART_MCR_DTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	if (set & TIOCM_RTS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		urb_value |= UART_MCR_RTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	status = qt2_setregister(dev, port_priv->device_port, UART_MCR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 				 urb_value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		dev_err(&port->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 			"update_mctrl - Error from MODEM_CTRL urb: %i\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 			status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) static int qt2_calc_num_ports(struct usb_serial *serial,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 					struct usb_serial_endpoints *epds)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	struct qt2_device_detail d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	for (i = 0; d = qt2_device_details[i], d.product_id != 0; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		if (d.product_id == le16_to_cpu(serial->dev->descriptor.idProduct))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 			return d.num_ports;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	/* we didn't recognize the device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	dev_err(&serial->dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		 "don't know the number of ports, assuming 1\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) static void qt2_set_termios(struct tty_struct *tty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 			    struct usb_serial_port *port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 			    struct ktermios *old_termios)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	struct usb_device *dev = port->serial->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	struct qt2_port_private *port_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	struct ktermios *termios = &tty->termios;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	u16 baud;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	unsigned int cflag = termios->c_cflag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	u16 new_lcr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	port_priv = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	if (cflag & PARENB) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 		if (cflag & PARODD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 			new_lcr |= UART_LCR_PARITY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 			new_lcr |= SERIAL_EVEN_PARITY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	switch (cflag & CSIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	case CS5:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		new_lcr |= UART_LCR_WLEN5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	case CS6:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		new_lcr |= UART_LCR_WLEN6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	case CS7:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		new_lcr |= UART_LCR_WLEN7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	case CS8:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		new_lcr |= UART_LCR_WLEN8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	baud = tty_get_baud_rate(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	if (!baud)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		baud = 9600;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	status = qt2_set_port_config(dev, port_priv->device_port, baud,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 				     new_lcr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		dev_err(&port->dev, "%s - qt2_set_port_config failed: %i\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 			__func__, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	if (cflag & CRTSCTS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		status = qt2_control_msg(dev, QT_HW_FLOW_CONTROL_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 					 SERIAL_CRTSCTS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 					 port_priv->device_port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		status = qt2_control_msg(dev, QT_HW_FLOW_CONTROL_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 					 0, port_priv->device_port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 		dev_err(&port->dev, "%s - set HW flow control failed: %i\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 			__func__, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	if (I_IXOFF(tty) || I_IXON(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		u16 x = ((u16) (START_CHAR(tty) << 8) | (u16) (STOP_CHAR(tty)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		status = qt2_control_msg(dev, QT_SW_FLOW_CONTROL_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 					 x, port_priv->device_port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		status = qt2_control_msg(dev, QT_SW_FLOW_CONTROL_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 					 0, port_priv->device_port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		dev_err(&port->dev, "%s - set SW flow control failed: %i\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 			__func__, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) static int qt2_open(struct tty_struct *tty, struct usb_serial_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	struct usb_serial *serial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	struct qt2_port_private *port_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	u8 *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	u16 device_port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	device_port = port->port_number;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	serial = port->serial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	port_priv = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	/* set the port to RS232 mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	status = qt2_control_msg(serial->dev, QT2_GET_SET_QMCR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 				 QT2_QMCR_RS232, device_port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 		dev_err(&port->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 			"%s failed to set RS232 mode for port %i error %i\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 			__func__, device_port, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 		return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	data = kzalloc(2, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	/* open the port */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	status = usb_control_msg(serial->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 				 usb_rcvctrlpipe(serial->dev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 				 QT_OPEN_CLOSE_CHANNEL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 				 0xc0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 				 device_port, data, 2, QT2_USB_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	if (status < 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 		dev_err(&port->dev, "%s - open port failed %i\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 			status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 		if (status >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 			status = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 		kfree(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 		return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	spin_lock_irqsave(&port_priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	port_priv->shadowLSR = data[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	port_priv->shadowMSR = data[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	spin_unlock_irqrestore(&port_priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	kfree(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	/* set to default speed and 8bit word size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	status = qt2_set_port_config(serial->dev, device_port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 				     DEFAULT_BAUD_RATE, UART_LCR_WLEN8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 		dev_err(&port->dev, "%s - initial setup failed (%i)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 			__func__, device_port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 		return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	port_priv->device_port = (u8) device_port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	if (tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 		qt2_set_termios(tty, port, &tty->termios);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) static void qt2_close(struct usb_serial_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	struct usb_serial *serial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	struct qt2_port_private *port_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	serial = port->serial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	port_priv = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	usb_kill_urb(port_priv->write_urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	/* flush the port transmit buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	i = usb_control_msg(serial->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 			    usb_sndctrlpipe(serial->dev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 			    QT2_FLUSH_DEVICE, 0x40, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 			    port_priv->device_port, NULL, 0, QT2_USB_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	if (i < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 		dev_err(&port->dev, "%s - transmit buffer flush failed: %i\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 			__func__, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	/* flush the port receive buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	i = usb_control_msg(serial->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 			    usb_sndctrlpipe(serial->dev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 			    QT2_FLUSH_DEVICE, 0x40, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 			    port_priv->device_port, NULL, 0, QT2_USB_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	if (i < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 		dev_err(&port->dev, "%s - receive buffer flush failed: %i\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 			__func__, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	/* close the port */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	i = usb_control_msg(serial->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 			    usb_sndctrlpipe(serial->dev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 			    QT_OPEN_CLOSE_CHANNEL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 			    0x40, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 			    port_priv->device_port, NULL, 0, QT2_USB_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	if (i < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 		dev_err(&port->dev, "%s - close port failed %i\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 			__func__, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) static void qt2_disconnect(struct usb_serial *serial)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	struct qt2_serial_private *serial_priv = usb_get_serial_data(serial);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	usb_kill_urb(serial_priv->read_urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) static int get_serial_info(struct tty_struct *tty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 			   struct serial_struct *ss)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	struct usb_serial_port *port = tty->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	ss->line		= port->minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	ss->port		= 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	ss->irq			= 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	ss->xmit_fifo_size	= port->bulk_out_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	ss->baud_base		= 9600;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	ss->close_delay		= 5*HZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	ss->closing_wait	= 30*HZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) static void qt2_process_status(struct usb_serial_port *port, unsigned char *ch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	switch (*ch) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	case QT2_LINE_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 		qt2_update_lsr(port, ch + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	case QT2_MODEM_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 		qt2_update_msr(port, ch + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) static void qt2_process_read_urb(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	struct usb_serial *serial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	struct qt2_serial_private *serial_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	struct usb_serial_port *port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	bool escapeflag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	unsigned char *ch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	unsigned char newport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	int len = urb->actual_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	if (!len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	ch = urb->transfer_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	serial = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	serial_priv = usb_get_serial_data(serial);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	port = serial->port[serial_priv->current_port];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	for (i = 0; i < urb->actual_length; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 		ch = (unsigned char *)urb->transfer_buffer + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 		if ((i <= (len - 3)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 		    (*ch == QT2_CONTROL_BYTE) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 		    (*(ch + 1) == QT2_CONTROL_BYTE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 			escapeflag = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 			switch (*(ch + 2)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 			case QT2_LINE_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 			case QT2_MODEM_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 				if (i > (len - 4)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 					dev_warn(&port->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 						 "%s - status message too short\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 						__func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 				qt2_process_status(port, ch + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 				i += 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 				escapeflag = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 			case QT2_XMIT_HOLD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 				if (i > (len - 5)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 					dev_warn(&port->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 						 "%s - xmit_empty message too short\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 						 __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 				/* bytes_written = (ch[1] << 4) + ch[0]; */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 				i += 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 				escapeflag = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 			case QT2_CHANGE_PORT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 				if (i > (len - 4)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 					dev_warn(&port->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 						 "%s - change_port message too short\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 						 __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 				tty_flip_buffer_push(&port->port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 				newport = *(ch + 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 				if (newport > serial->num_ports) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 					dev_err(&port->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 						"%s - port change to invalid port: %i\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 						__func__, newport);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 				serial_priv->current_port = newport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 				port = serial->port[serial_priv->current_port];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 				i += 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 				escapeflag = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 			case QT2_REC_FLUSH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 			case QT2_XMIT_FLUSH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 				i += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 				escapeflag = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 			case QT2_CONTROL_ESCAPE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 				tty_insert_flip_string(&port->port, ch, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 				i += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 				escapeflag = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 			default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 				dev_warn(&port->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 					 "%s - unsupported command %i\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 					 __func__, *(ch + 2));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 			if (escapeflag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 		tty_insert_flip_char(&port->port, *ch, TTY_NORMAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 	tty_flip_buffer_push(&port->port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) static void qt2_write_bulk_callback(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 	struct usb_serial_port *port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 	struct qt2_port_private *port_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 	port = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 	port_priv = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 	spin_lock_irqsave(&port_priv->urb_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 	port_priv->urb_in_use = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 	usb_serial_port_softint(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 	spin_unlock_irqrestore(&port_priv->urb_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) static void qt2_read_bulk_callback(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	struct usb_serial *serial = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 	int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 	if (urb->status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 		dev_warn(&serial->dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 			 "%s - non-zero urb status: %i\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 			 urb->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 	qt2_process_read_urb(urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 	status = usb_submit_urb(urb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 	if (status != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 		dev_err(&serial->dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 			"%s - resubmit read urb failed: %i\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 			__func__, status);
^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 qt2_setup_urbs(struct usb_serial *serial)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 	struct usb_serial_port *port0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 	struct qt2_serial_private *serial_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 	int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 	port0 = serial->port[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 	serial_priv = usb_get_serial_data(serial);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 	serial_priv->read_urb = usb_alloc_urb(0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 	if (!serial_priv->read_urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 	usb_fill_bulk_urb(serial_priv->read_urb, serial->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 			  usb_rcvbulkpipe(serial->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 					  port0->bulk_in_endpointAddress),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 			  serial_priv->read_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 			  QT2_READ_BUFFER_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 			  qt2_read_bulk_callback, serial);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 	status = usb_submit_urb(serial_priv->read_urb, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 	if (status != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 		dev_err(&serial->dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 			"%s - submit read urb failed %i\n", __func__, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 		usb_free_urb(serial_priv->read_urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 		return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) static int qt2_attach(struct usb_serial *serial)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 	struct qt2_serial_private *serial_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 	int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 	/* power on unit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 	status = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 				 0xc2, 0x40, 0x8000, 0, NULL, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 				 QT2_USB_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 	if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 		dev_err(&serial->dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 			"%s - failed to power on unit: %i\n", __func__, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 		return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 	serial_priv = kzalloc(sizeof(*serial_priv), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 	if (!serial_priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 	serial_priv->read_buffer = kmalloc(QT2_READ_BUFFER_SIZE, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 	if (!serial_priv->read_buffer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 		status = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 		goto err_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 	usb_set_serial_data(serial, serial_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 	status = qt2_setup_urbs(serial);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 	if (status != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 		goto attach_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) attach_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 	kfree(serial_priv->read_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) err_buf:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 	kfree(serial_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 	return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) static int qt2_port_probe(struct usb_serial_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 	struct usb_serial *serial = port->serial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 	struct qt2_port_private *port_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 	u8 bEndpointAddress;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 	port_priv = kzalloc(sizeof(*port_priv), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 	if (!port_priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 	spin_lock_init(&port_priv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 	spin_lock_init(&port_priv->urb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 	port_priv->port = port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 	port_priv->write_buffer = kmalloc(QT2_WRITE_BUFFER_SIZE, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 	if (!port_priv->write_buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 		goto err_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) 	port_priv->write_urb = usb_alloc_urb(0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) 	if (!port_priv->write_urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) 		goto err_urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) 	bEndpointAddress = serial->port[0]->bulk_out_endpointAddress;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) 	usb_fill_bulk_urb(port_priv->write_urb, serial->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 				usb_sndbulkpipe(serial->dev, bEndpointAddress),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) 				port_priv->write_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 				QT2_WRITE_BUFFER_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 				qt2_write_bulk_callback, port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 	usb_set_serial_port_data(port, port_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) err_urb:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 	kfree(port_priv->write_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) err_buf:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 	kfree(port_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 	return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) static int qt2_port_remove(struct usb_serial_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) 	struct qt2_port_private *port_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) 	port_priv = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) 	usb_free_urb(port_priv->write_urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 	kfree(port_priv->write_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 	kfree(port_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) static int qt2_tiocmget(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) 	struct usb_serial_port *port = tty->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) 	struct usb_device *dev = port->serial->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 	struct qt2_port_private *port_priv = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) 	u8 *d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) 	d = kzalloc(2, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) 	if (!d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) 	r = qt2_getregister(dev, port_priv->device_port, UART_MCR, d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) 	if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) 		goto mget_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) 	r = qt2_getregister(dev, port_priv->device_port, UART_MSR, d + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) 	if (r < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) 		goto mget_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) 	r = (d[0] & UART_MCR_DTR ? TIOCM_DTR : 0) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) 	    (d[0] & UART_MCR_RTS ? TIOCM_RTS : 0) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) 	    (d[1] & UART_MSR_CTS ? TIOCM_CTS : 0) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) 	    (d[1] & UART_MSR_DCD ? TIOCM_CAR : 0) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) 	    (d[1] & UART_MSR_RI ? TIOCM_RI : 0) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) 	    (d[1] & UART_MSR_DSR ? TIOCM_DSR : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) mget_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) 	kfree(d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) static int qt2_tiocmset(struct tty_struct *tty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) 			unsigned int set, unsigned int clear)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) 	struct qt2_port_private *port_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) 	port_priv = usb_get_serial_port_data(tty->driver_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) 	return update_mctrl(port_priv, set, clear);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) static void qt2_break_ctl(struct tty_struct *tty, int break_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) 	struct usb_serial_port *port = tty->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) 	struct qt2_port_private *port_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) 	int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) 	u16 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) 	port_priv = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) 	val = (break_state == -1) ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) 	status = qt2_control_msg(port->serial->dev, QT2_BREAK_CONTROL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) 				 val, port_priv->device_port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) 	if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) 		dev_warn(&port->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) 			 "%s - failed to send control message: %i\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) 			 status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) }
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) static void qt2_dtr_rts(struct usb_serial_port *port, int on)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) 	struct usb_device *dev = port->serial->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) 	struct qt2_port_private *port_priv = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) 	/* Disable flow control */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) 	if (!on) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) 		if (qt2_setregister(dev, port_priv->device_port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) 					   UART_MCR, 0) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) 			dev_warn(&port->dev, "error from flowcontrol urb\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) 	/* drop RTS and DTR */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) 	if (on)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) 		update_mctrl(port_priv, TIOCM_DTR | TIOCM_RTS, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) 		update_mctrl(port_priv, 0, TIOCM_DTR | TIOCM_RTS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) static void qt2_update_msr(struct usb_serial_port *port, unsigned char *ch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) 	struct qt2_port_private *port_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) 	u8 newMSR = (u8) *ch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) 	/* May be called from qt2_process_read_urb() for an unbound port. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) 	port_priv = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) 	if (!port_priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) 	spin_lock_irqsave(&port_priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) 	port_priv->shadowMSR = newMSR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) 	spin_unlock_irqrestore(&port_priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) 	if (newMSR & UART_MSR_ANY_DELTA) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) 		/* update input line counters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) 		if (newMSR & UART_MSR_DCTS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) 			port->icount.cts++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) 		if (newMSR & UART_MSR_DDSR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) 			port->icount.dsr++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) 		if (newMSR & UART_MSR_DDCD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) 			port->icount.dcd++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) 		if (newMSR & UART_MSR_TERI)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) 			port->icount.rng++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) 		wake_up_interruptible(&port->port.delta_msr_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) static void qt2_update_lsr(struct usb_serial_port *port, unsigned char *ch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) 	struct qt2_port_private *port_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) 	struct async_icount *icount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) 	u8 newLSR = (u8) *ch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) 	/* May be called from qt2_process_read_urb() for an unbound port. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) 	port_priv = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) 	if (!port_priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) 	if (newLSR & UART_LSR_BI)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) 		newLSR &= (u8) (UART_LSR_OE | UART_LSR_BI);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) 	spin_lock_irqsave(&port_priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) 	port_priv->shadowLSR = newLSR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) 	spin_unlock_irqrestore(&port_priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) 	icount = &port->icount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) 	if (newLSR & UART_LSR_BRK_ERROR_BITS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) 		if (newLSR & UART_LSR_BI)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) 			icount->brk++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) 		if (newLSR & UART_LSR_OE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) 			icount->overrun++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) 		if (newLSR & UART_LSR_PE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) 			icount->parity++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) 		if (newLSR & UART_LSR_FE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) 			icount->frame++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) static int qt2_write_room(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) 	struct usb_serial_port *port = tty->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) 	struct qt2_port_private *port_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) 	unsigned long flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) 	int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) 	port_priv = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) 	spin_lock_irqsave(&port_priv->urb_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) 	if (port_priv->urb_in_use)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) 		r = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) 		r = QT2_WRITE_BUFFER_SIZE - QT2_WRITE_CONTROL_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) 	spin_unlock_irqrestore(&port_priv->urb_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) 	return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) static int qt2_write(struct tty_struct *tty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) 		     struct usb_serial_port *port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) 		     const unsigned char *buf, int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) 	struct qt2_port_private *port_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) 	struct urb *write_urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) 	unsigned char *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) 	int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) 	int bytes_out = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) 	port_priv = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) 	if (port_priv->write_urb == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) 		dev_err(&port->dev, "%s - no output urb\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) 	write_urb = port_priv->write_urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) 	count = min(count, QT2_WRITE_BUFFER_SIZE - QT2_WRITE_CONTROL_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) 	data = write_urb->transfer_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) 	spin_lock_irqsave(&port_priv->urb_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) 	if (port_priv->urb_in_use) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) 		dev_err(&port->dev, "qt2_write - urb is in use\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) 		goto write_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) 	*data++ = QT2_CONTROL_BYTE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) 	*data++ = QT2_CONTROL_BYTE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) 	*data++ = port_priv->device_port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) 	put_unaligned_le16(count, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) 	data += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) 	memcpy(data, buf, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) 	write_urb->transfer_buffer_length = count + QT2_WRITE_CONTROL_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) 	status = usb_submit_urb(write_urb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) 	if (status == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) 		port_priv->urb_in_use = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) 		bytes_out += count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) write_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) 	spin_unlock_irqrestore(&port_priv->urb_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) 	return bytes_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) static struct usb_serial_driver qt2_device = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) 		.owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) 		.name = "quatech-serial",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) 	.description	     = DRIVER_DESC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) 	.id_table	     = id_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) 	.open		     = qt2_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) 	.close		     = qt2_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) 	.write               = qt2_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) 	.write_room          = qt2_write_room,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) 	.calc_num_ports      = qt2_calc_num_ports,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) 	.attach              = qt2_attach,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) 	.release             = qt2_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) 	.disconnect          = qt2_disconnect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) 	.port_probe          = qt2_port_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) 	.port_remove         = qt2_port_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) 	.dtr_rts             = qt2_dtr_rts,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) 	.break_ctl           = qt2_break_ctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) 	.tiocmget            = qt2_tiocmget,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) 	.tiocmset            = qt2_tiocmset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) 	.tiocmiwait          = usb_serial_generic_tiocmiwait,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) 	.get_icount	     = usb_serial_generic_get_icount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) 	.get_serial          = get_serial_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) 	.set_termios         = qt2_set_termios,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) static struct usb_serial_driver *const serial_drivers[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) 	&qt2_device, NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) module_usb_serial_driver(serial_drivers, id_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) MODULE_DESCRIPTION(DRIVER_DESC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) MODULE_LICENSE("GPL v2");