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)  * MCT (Magic Control Technology Corp.) USB RS232 Converter Driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *   Copyright (C) 2000 Wolfgang Grandegger (wolfgang@ces.ch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * This program is largely derived from the Belkin USB Serial Adapter Driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * (see belkin_sa.[ch]). All of the information about the device was acquired
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * by using SniffUSB on Windows98. For technical details see mct_u232.h.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * William G. Greathouse and Greg Kroah-Hartman provided great help on how to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * do the reverse engineering and how to write a USB serial device driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  * TO BE DONE, TO BE CHECKED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  *   DTR/RTS signal handling may be incomplete or incorrect. I have mainly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  *   implemented what I have seen with SniffUSB or found in belkin_sa.c.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  *   For further TODOs check also belkin_sa.c.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/tty.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <linux/tty_driver.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <linux/tty_flip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #include <asm/unaligned.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #include <linux/usb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #include <linux/usb/serial.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #include <linux/serial.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #include "mct_u232.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define DRIVER_AUTHOR "Wolfgang Grandegger <wolfgang@ces.ch>"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #define DRIVER_DESC "Magic Control Technology USB-RS232 converter driver"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39)  * Function prototypes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) static int  mct_u232_port_probe(struct usb_serial_port *port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) static int  mct_u232_port_remove(struct usb_serial_port *remove);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) static int  mct_u232_open(struct tty_struct *tty, struct usb_serial_port *port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) static void mct_u232_close(struct usb_serial_port *port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) static void mct_u232_dtr_rts(struct usb_serial_port *port, int on);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) static void mct_u232_read_int_callback(struct urb *urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) static void mct_u232_set_termios(struct tty_struct *tty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 			struct usb_serial_port *port, struct ktermios *old);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) static void mct_u232_break_ctl(struct tty_struct *tty, int break_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) static int  mct_u232_tiocmget(struct tty_struct *tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) static int  mct_u232_tiocmset(struct tty_struct *tty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 			unsigned int set, unsigned int clear);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) static void mct_u232_throttle(struct tty_struct *tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) static void mct_u232_unthrottle(struct tty_struct *tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58)  * All of the device info needed for the MCT USB-RS232 converter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) static const struct usb_device_id id_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	{ USB_DEVICE(MCT_U232_VID, MCT_U232_PID) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	{ USB_DEVICE(MCT_U232_VID, MCT_U232_SITECOM_PID) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	{ USB_DEVICE(MCT_U232_VID, MCT_U232_DU_H3SP_PID) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	{ USB_DEVICE(MCT_U232_BELKIN_F5U109_VID, MCT_U232_BELKIN_F5U109_PID) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	{ }		/* Terminating entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) MODULE_DEVICE_TABLE(usb, id_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) static struct usb_serial_driver mct_u232_device = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		.owner =	THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		.name =		"mct_u232",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	.description =	     "MCT U232",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	.id_table =	     id_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	.num_ports =	     1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	.open =		     mct_u232_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	.close =	     mct_u232_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	.dtr_rts =	     mct_u232_dtr_rts,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	.throttle =	     mct_u232_throttle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	.unthrottle =	     mct_u232_unthrottle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	.read_int_callback = mct_u232_read_int_callback,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	.set_termios =	     mct_u232_set_termios,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	.break_ctl =	     mct_u232_break_ctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	.tiocmget =	     mct_u232_tiocmget,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	.tiocmset =	     mct_u232_tiocmset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	.tiocmiwait =        usb_serial_generic_tiocmiwait,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	.port_probe =        mct_u232_port_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	.port_remove =       mct_u232_port_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	.get_icount =        usb_serial_generic_get_icount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) static struct usb_serial_driver * const serial_drivers[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	&mct_u232_device, NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) struct mct_u232_private {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	struct urb *read_urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	spinlock_t lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	unsigned int	     control_state; /* Modem Line Setting (TIOCM) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	unsigned char        last_lcr;      /* Line Control Register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	unsigned char	     last_lsr;      /* Line Status Register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	unsigned char	     last_msr;      /* Modem Status Register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	unsigned int	     rx_flags;      /* Throttling flags */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) #define THROTTLED		0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)  * Handle vendor specific USB requests
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) #define WDR_TIMEOUT 5000 /* default urb timeout */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)  * Later day 2.6.0-test kernels have new baud rates like B230400 which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)  * we do not know how to support. We ignore them for the moment.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) static int mct_u232_calculate_baud_rate(struct usb_serial *serial,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 					speed_t value, speed_t *result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	*result = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	if (le16_to_cpu(serial->dev->descriptor.idProduct) == MCT_U232_SITECOM_PID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		|| le16_to_cpu(serial->dev->descriptor.idProduct) == MCT_U232_BELKIN_F5U109_PID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		switch (value) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		case 300:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 			return 0x01;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		case 600:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 			return 0x02; /* this one not tested */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		case 1200:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 			return 0x03;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		case 2400:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 			return 0x04;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		case 4800:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 			return 0x06;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		case 9600:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 			return 0x08;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		case 19200:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 			return 0x09;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		case 38400:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 			return 0x0a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		case 57600:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 			return 0x0b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		case 115200:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 			return 0x0c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 			*result = 9600;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 			return 0x08;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		/* FIXME: Can we use any divider - should we do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		   divider = 115200/value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		   real baud = 115200/divider */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		switch (value) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		case 300: break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		case 600: break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		case 1200: break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		case 2400: break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		case 4800: break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		case 9600: break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		case 19200: break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		case 38400: break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		case 57600: break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		case 115200: break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 			value = 9600;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 			*result = 9600;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		return 115200/value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) static int mct_u232_set_baud_rate(struct tty_struct *tty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	struct usb_serial *serial, struct usb_serial_port *port, speed_t value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	unsigned int divisor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	unsigned char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	unsigned char cts_enable_byte = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	speed_t speed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	buf = kmalloc(MCT_U232_MAX_SIZE, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	if (buf == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	divisor = mct_u232_calculate_baud_rate(serial, value, &speed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	put_unaligned_le32(divisor, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 				MCT_U232_SET_BAUD_RATE_REQUEST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 				MCT_U232_SET_REQUEST_TYPE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 				0, 0, buf, MCT_U232_SET_BAUD_RATE_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 				WDR_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	if (rc < 0)	/*FIXME: What value speed results */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		dev_err(&port->dev, "Set BAUD RATE %d failed (error = %d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 			value, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		tty_encode_baud_rate(tty, speed, speed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	dev_dbg(&port->dev, "set_baud_rate: value: 0x%x, divisor: 0x%x\n", value, divisor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	/* Mimic the MCT-supplied Windows driver (version 1.21P.0104), which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	   always sends two extra USB 'device request' messages after the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	   'baud rate change' message.  The actual functionality of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	   request codes in these messages is not fully understood but these
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	   particular codes are never seen in any operation besides a baud
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	   rate change.  Both of these messages send a single byte of data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	   In the first message, the value of this byte is always zero.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	   The second message has been determined experimentally to control
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	   whether data will be transmitted to a device which is not asserting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	   the 'CTS' signal.  If the second message's data byte is zero, data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	   will be transmitted even if 'CTS' is not asserted (i.e. no hardware
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	   flow control).  if the second message's data byte is nonzero (a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	   value of 1 is used by this driver), data will not be transmitted to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	   a device which is not asserting 'CTS'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	buf[0] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 				MCT_U232_SET_UNKNOWN1_REQUEST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 				MCT_U232_SET_REQUEST_TYPE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 				0, 0, buf, MCT_U232_SET_UNKNOWN1_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 				WDR_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	if (rc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		dev_err(&port->dev, "Sending USB device request code %d "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 			"failed (error = %d)\n", MCT_U232_SET_UNKNOWN1_REQUEST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 			rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	if (port && C_CRTSCTS(tty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	   cts_enable_byte = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	dev_dbg(&port->dev, "set_baud_rate: send second control message, data = %02X\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		cts_enable_byte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	buf[0] = cts_enable_byte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 			MCT_U232_SET_CTS_REQUEST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 			MCT_U232_SET_REQUEST_TYPE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 			0, 0, buf, MCT_U232_SET_CTS_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 			WDR_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	if (rc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		dev_err(&port->dev, "Sending USB device request code %d "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 			"failed (error = %d)\n", MCT_U232_SET_CTS_REQUEST, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) } /* mct_u232_set_baud_rate */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) static int mct_u232_set_line_ctrl(struct usb_serial_port *port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 				  unsigned char lcr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	unsigned char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	buf = kmalloc(MCT_U232_MAX_SIZE, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	if (buf == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	buf[0] = lcr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	rc = usb_control_msg(port->serial->dev, usb_sndctrlpipe(port->serial->dev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 			MCT_U232_SET_LINE_CTRL_REQUEST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 			MCT_U232_SET_REQUEST_TYPE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 			0, 0, buf, MCT_U232_SET_LINE_CTRL_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 			WDR_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	if (rc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		dev_err(&port->dev, "Set LINE CTRL 0x%x failed (error = %d)\n", lcr, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	dev_dbg(&port->dev, "set_line_ctrl: 0x%x\n", lcr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) } /* mct_u232_set_line_ctrl */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) static int mct_u232_set_modem_ctrl(struct usb_serial_port *port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 				   unsigned int control_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	unsigned char mcr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	unsigned char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	buf = kmalloc(MCT_U232_MAX_SIZE, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	if (buf == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	mcr = MCT_U232_MCR_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	if (control_state & TIOCM_DTR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		mcr |= MCT_U232_MCR_DTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	if (control_state & TIOCM_RTS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		mcr |= MCT_U232_MCR_RTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	buf[0] = mcr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	rc = usb_control_msg(port->serial->dev, usb_sndctrlpipe(port->serial->dev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 			MCT_U232_SET_MODEM_CTRL_REQUEST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 			MCT_U232_SET_REQUEST_TYPE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 			0, 0, buf, MCT_U232_SET_MODEM_CTRL_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 			WDR_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	dev_dbg(&port->dev, "set_modem_ctrl: state=0x%x ==> mcr=0x%x\n", control_state, mcr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	if (rc < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		dev_err(&port->dev, "Set MODEM CTRL 0x%x failed (error = %d)\n", mcr, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) } /* mct_u232_set_modem_ctrl */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) static int mct_u232_get_modem_stat(struct usb_serial_port *port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 				   unsigned char *msr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	unsigned char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	buf = kmalloc(MCT_U232_MAX_SIZE, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	if (buf == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		*msr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	rc = usb_control_msg(port->serial->dev, usb_rcvctrlpipe(port->serial->dev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 			MCT_U232_GET_MODEM_STAT_REQUEST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 			MCT_U232_GET_REQUEST_TYPE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 			0, 0, buf, MCT_U232_GET_MODEM_STAT_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 			WDR_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	if (rc < MCT_U232_GET_MODEM_STAT_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		dev_err(&port->dev, "Get MODEM STATus failed (error = %d)\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		if (rc >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 			rc = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		*msr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		*msr = buf[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	dev_dbg(&port->dev, "get_modem_stat: 0x%x\n", *msr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) } /* mct_u232_get_modem_stat */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) static void mct_u232_msr_to_icount(struct async_icount *icount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 						unsigned char msr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	/* Translate Control Line states */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	if (msr & MCT_U232_MSR_DDSR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 		icount->dsr++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	if (msr & MCT_U232_MSR_DCTS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 		icount->cts++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	if (msr & MCT_U232_MSR_DRI)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 		icount->rng++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	if (msr & MCT_U232_MSR_DCD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 		icount->dcd++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) } /* mct_u232_msr_to_icount */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) static void mct_u232_msr_to_state(struct usb_serial_port *port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 				  unsigned int *control_state, unsigned char msr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	/* Translate Control Line states */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	if (msr & MCT_U232_MSR_DSR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 		*control_state |=  TIOCM_DSR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 		*control_state &= ~TIOCM_DSR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	if (msr & MCT_U232_MSR_CTS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 		*control_state |=  TIOCM_CTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 		*control_state &= ~TIOCM_CTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	if (msr & MCT_U232_MSR_RI)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 		*control_state |=  TIOCM_RI;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 		*control_state &= ~TIOCM_RI;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	if (msr & MCT_U232_MSR_CD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 		*control_state |=  TIOCM_CD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 		*control_state &= ~TIOCM_CD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	dev_dbg(&port->dev, "msr_to_state: msr=0x%x ==> state=0x%x\n", msr, *control_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) } /* mct_u232_msr_to_state */
^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)  * Driver's tty interface functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) static int mct_u232_port_probe(struct usb_serial_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	struct usb_serial *serial = port->serial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	struct mct_u232_private *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	/* check first to simplify error handling */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	if (!serial->port[1] || !serial->port[1]->interrupt_in_urb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 		dev_err(&port->dev, "expected endpoint missing\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	if (!priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	/* Use second interrupt-in endpoint for reading. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	priv->read_urb = serial->port[1]->interrupt_in_urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	priv->read_urb->context = port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	spin_lock_init(&priv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	usb_set_serial_port_data(port, priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	return 0;
^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) static int mct_u232_port_remove(struct usb_serial_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	struct mct_u232_private *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	priv = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	kfree(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	return 0;
^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) static int  mct_u232_open(struct tty_struct *tty, struct usb_serial_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	struct usb_serial *serial = port->serial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	struct mct_u232_private *priv = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	int retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	unsigned int control_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	unsigned char last_lcr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	unsigned char last_msr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	/* Compensate for a hardware bug: although the Sitecom U232-P25
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	 * device reports a maximum output packet size of 32 bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	 * it seems to be able to accept only 16 bytes (and that's what
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	 * SniffUSB says too...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	if (le16_to_cpu(serial->dev->descriptor.idProduct)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 						== MCT_U232_SITECOM_PID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 		port->bulk_out_size = 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	/* Do a defined restart: the normal serial device seems to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	 * always turn on DTR and RTS here, so do the same. I'm not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	 * sure if this is really necessary. But it should not harm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	 * either.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	spin_lock_irqsave(&priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	if (tty && C_BAUD(tty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 		priv->control_state = TIOCM_DTR | TIOCM_RTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 		priv->control_state = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	priv->last_lcr = (MCT_U232_DATA_BITS_8 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 			  MCT_U232_PARITY_NONE |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 			  MCT_U232_STOP_BITS_1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	control_state = priv->control_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	last_lcr = priv->last_lcr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	spin_unlock_irqrestore(&priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	mct_u232_set_modem_ctrl(port, control_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	mct_u232_set_line_ctrl(port, last_lcr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	/* Read modem status and update control state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	mct_u232_get_modem_stat(port, &last_msr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	spin_lock_irqsave(&priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	priv->last_msr = last_msr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	mct_u232_msr_to_state(port, &priv->control_state, priv->last_msr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	spin_unlock_irqrestore(&priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	retval = usb_submit_urb(priv->read_urb, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 		dev_err(&port->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 			"usb_submit_urb(read) failed pipe 0x%x err %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 			port->read_urb->pipe, retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 		goto error;
^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) 	retval = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 		usb_kill_urb(priv->read_urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 		dev_err(&port->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 			"usb_submit_urb(read int) failed pipe 0x%x err %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 			port->interrupt_in_urb->pipe, retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) } /* mct_u232_open */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) static void mct_u232_dtr_rts(struct usb_serial_port *port, int on)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	unsigned int control_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	struct mct_u232_private *priv = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	spin_lock_irq(&priv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	if (on)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 		priv->control_state |= TIOCM_DTR | TIOCM_RTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 		priv->control_state &= ~(TIOCM_DTR | TIOCM_RTS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	control_state = priv->control_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	spin_unlock_irq(&priv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	mct_u232_set_modem_ctrl(port, control_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) static void mct_u232_close(struct usb_serial_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	struct mct_u232_private *priv = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	usb_kill_urb(priv->read_urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	usb_kill_urb(port->interrupt_in_urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	usb_serial_generic_close(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) } /* mct_u232_close */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) static void mct_u232_read_int_callback(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	struct usb_serial_port *port = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	struct mct_u232_private *priv = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	unsigned char *data = urb->transfer_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	int status = urb->status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	switch (status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 		/* success */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	case -ECONNRESET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	case -ENOENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	case -ESHUTDOWN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 		/* this urb is terminated, clean up */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 		dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 			__func__, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 		dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 			__func__, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	 * Work-a-round: handle the 'usual' bulk-in pipe here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	if (urb->transfer_buffer_length > 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 		if (urb->actual_length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 			tty_insert_flip_string(&port->port, data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 					urb->actual_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 			tty_flip_buffer_push(&port->port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 	 * The interrupt-in pipe signals exceptional conditions (modem line
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 	 * signal changes and errors). data[0] holds MSR, data[1] holds LSR.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 	spin_lock_irqsave(&priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 	priv->last_msr = data[MCT_U232_MSR_INDEX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 	/* Record Control Line states */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 	mct_u232_msr_to_state(port, &priv->control_state, priv->last_msr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 	mct_u232_msr_to_icount(&port->icount, priv->last_msr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) #if 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 	/* Not yet handled. See belkin_sa.c for further information */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	/* Now to report any errors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	priv->last_lsr = data[MCT_U232_LSR_INDEX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 	 * fill in the flip buffer here, but I do not know the relation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 	 * to the current/next receive buffer or characters.  I need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 	 * to look in to this before committing any code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 	if (priv->last_lsr & MCT_U232_LSR_ERR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 		tty = tty_port_tty_get(&port->port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 		/* Overrun Error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 		if (priv->last_lsr & MCT_U232_LSR_OE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 		/* Parity Error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 		if (priv->last_lsr & MCT_U232_LSR_PE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 		/* Framing Error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 		if (priv->last_lsr & MCT_U232_LSR_FE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 		/* Break Indicator */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 		if (priv->last_lsr & MCT_U232_LSR_BI) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 		tty_kref_put(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 	wake_up_interruptible(&port->port.delta_msr_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 	spin_unlock_irqrestore(&priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 	retval = usb_submit_urb(urb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 		dev_err(&port->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 			"%s - usb_submit_urb failed with result %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 			__func__, retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) } /* mct_u232_read_int_callback */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) static void mct_u232_set_termios(struct tty_struct *tty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 				 struct usb_serial_port *port,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 				 struct ktermios *old_termios)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 	struct usb_serial *serial = port->serial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	struct mct_u232_private *priv = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 	struct ktermios *termios = &tty->termios;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 	unsigned int cflag = termios->c_cflag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 	unsigned int old_cflag = old_termios->c_cflag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 	unsigned int control_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 	unsigned char last_lcr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 	/* get a local copy of the current port settings */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 	spin_lock_irqsave(&priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 	control_state = priv->control_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 	spin_unlock_irqrestore(&priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 	last_lcr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 	 * Update baud rate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 	 * Do not attempt to cache old rates and skip settings,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 	 * disconnects screw such tricks up completely.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 	 * Premature optimization is the root of all evil.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 	/* reassert DTR and RTS on transition from B0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 	if ((old_cflag & CBAUD) == B0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 		dev_dbg(&port->dev, "%s: baud was B0\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 		control_state |= TIOCM_DTR | TIOCM_RTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 		mct_u232_set_modem_ctrl(port, control_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 	mct_u232_set_baud_rate(tty, serial, port, tty_get_baud_rate(tty));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 	if ((cflag & CBAUD) == B0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 		dev_dbg(&port->dev, "%s: baud is B0\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 		/* Drop RTS and DTR */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 		control_state &= ~(TIOCM_DTR | TIOCM_RTS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 		mct_u232_set_modem_ctrl(port, control_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 	 * Update line control register (LCR)
^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) 	/* set the parity */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 	if (cflag & PARENB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 		last_lcr |= (cflag & PARODD) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 			MCT_U232_PARITY_ODD : MCT_U232_PARITY_EVEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 		last_lcr |= MCT_U232_PARITY_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 	/* set the number of data bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 	switch (cflag & CSIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 	case CS5:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 		last_lcr |= MCT_U232_DATA_BITS_5; break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 	case CS6:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 		last_lcr |= MCT_U232_DATA_BITS_6; break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 	case CS7:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 		last_lcr |= MCT_U232_DATA_BITS_7; break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 	case CS8:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 		last_lcr |= MCT_U232_DATA_BITS_8; break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 		dev_err(&port->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 			"CSIZE was not CS5-CS8, using default of 8\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 		last_lcr |= MCT_U232_DATA_BITS_8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 		break;
^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) 	termios->c_cflag &= ~CMSPAR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 	/* set the number of stop bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 	last_lcr |= (cflag & CSTOPB) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 		MCT_U232_STOP_BITS_2 : MCT_U232_STOP_BITS_1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 	mct_u232_set_line_ctrl(port, last_lcr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 	/* save off the modified port settings */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 	spin_lock_irqsave(&priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 	priv->control_state = control_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 	priv->last_lcr = last_lcr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 	spin_unlock_irqrestore(&priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) } /* mct_u232_set_termios */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) static void mct_u232_break_ctl(struct tty_struct *tty, int break_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 	struct usb_serial_port *port = tty->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 	struct mct_u232_private *priv = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 	unsigned char lcr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 	spin_lock_irqsave(&priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 	lcr = priv->last_lcr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 	if (break_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 		lcr |= MCT_U232_SET_BREAK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 	spin_unlock_irqrestore(&priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 	mct_u232_set_line_ctrl(port, lcr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) } /* mct_u232_break_ctl */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) static int mct_u232_tiocmget(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 	struct usb_serial_port *port = tty->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 	struct mct_u232_private *priv = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 	unsigned int control_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 	spin_lock_irqsave(&priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 	control_state = priv->control_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 	spin_unlock_irqrestore(&priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) 	return control_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) static int mct_u232_tiocmset(struct tty_struct *tty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) 			      unsigned int set, unsigned int clear)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) 	struct usb_serial_port *port = tty->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 	struct mct_u232_private *priv = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 	unsigned int control_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 	spin_lock_irqsave(&priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 	control_state = priv->control_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 	if (set & TIOCM_RTS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 		control_state |= TIOCM_RTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 	if (set & TIOCM_DTR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 		control_state |= TIOCM_DTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) 	if (clear & TIOCM_RTS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 		control_state &= ~TIOCM_RTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) 	if (clear & TIOCM_DTR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) 		control_state &= ~TIOCM_DTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) 	priv->control_state = control_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) 	spin_unlock_irqrestore(&priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) 	return mct_u232_set_modem_ctrl(port, control_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) static void mct_u232_throttle(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) {
^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) 	struct mct_u232_private *priv = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) 	unsigned int control_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) 	spin_lock_irq(&priv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) 	priv->rx_flags |= THROTTLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 	if (C_CRTSCTS(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) 		priv->control_state &= ~TIOCM_RTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) 		control_state = priv->control_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) 		spin_unlock_irq(&priv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) 		mct_u232_set_modem_ctrl(port, control_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) 		spin_unlock_irq(&priv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) static void mct_u232_unthrottle(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) 	struct usb_serial_port *port = tty->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) 	struct mct_u232_private *priv = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) 	unsigned int control_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) 	spin_lock_irq(&priv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) 	if ((priv->rx_flags & THROTTLED) && C_CRTSCTS(tty)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) 		priv->rx_flags &= ~THROTTLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) 		priv->control_state |= TIOCM_RTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) 		control_state = priv->control_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) 		spin_unlock_irq(&priv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) 		mct_u232_set_modem_ctrl(port, control_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) 		spin_unlock_irq(&priv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) 	}
^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) module_usb_serial_driver(serial_drivers, id_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) MODULE_AUTHOR(DRIVER_AUTHOR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) MODULE_DESCRIPTION(DRIVER_DESC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) MODULE_LICENSE("GPL");