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)   Some of this code is credited to Linux USB open source files that are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)   distributed with Linux.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)   Copyright:	2007 Metrologic Instruments. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)   Copyright:	2011 Azimut Ltd. <http://azimutrzn.ru/>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/tty.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/usb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/slab.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/moduleparam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/uaccess.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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #define DRIVER_DESC "Metrologic Instruments Inc. - USB-POS driver"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) /* Product information. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #define FOCUS_VENDOR_ID			0x0C2E
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #define FOCUS_PRODUCT_ID_BI		0x0720
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #define FOCUS_PRODUCT_ID_UNI		0x0700
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #define METROUSB_SET_REQUEST_TYPE	0x40
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #define METROUSB_SET_MODEM_CTRL_REQUEST	10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define METROUSB_SET_BREAK_REQUEST	0x40
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #define METROUSB_MCR_NONE		0x08	/* Deactivate DTR and RTS. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #define METROUSB_MCR_RTS		0x0a	/* Activate RTS. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define METROUSB_MCR_DTR		0x09	/* Activate DTR. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #define WDR_TIMEOUT			5000	/* default urb timeout. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) /* Private data structure. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) struct metrousb_private {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	spinlock_t lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	int throttled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	unsigned long control_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) /* Device table list. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) static const struct usb_device_id id_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	{ USB_DEVICE(FOCUS_VENDOR_ID, FOCUS_PRODUCT_ID_BI) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	{ USB_DEVICE(FOCUS_VENDOR_ID, FOCUS_PRODUCT_ID_UNI) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	{ USB_DEVICE_INTERFACE_CLASS(0x0c2e, 0x0730, 0xff) },	/* MS7820 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	{ }, /* Terminating entry. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) MODULE_DEVICE_TABLE(usb, id_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) /* UNI-Directional mode commands for device configure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) #define UNI_CMD_OPEN	0x80
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) #define UNI_CMD_CLOSE	0xFF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) static int metrousb_is_unidirectional_mode(struct usb_serial *serial)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	u16 product_id = le16_to_cpu(serial->dev->descriptor.idProduct);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	return product_id == FOCUS_PRODUCT_ID_UNI;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) static int metrousb_calc_num_ports(struct usb_serial *serial,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 				   struct usb_serial_endpoints *epds)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	if (metrousb_is_unidirectional_mode(serial)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		if (epds->num_interrupt_out == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 			dev_err(&serial->interface->dev, "interrupt-out endpoint missing\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 			return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) static int metrousb_send_unidirectional_cmd(u8 cmd, struct usb_serial_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	int actual_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	u8 *buffer_cmd = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	if (!metrousb_is_unidirectional_mode(port->serial))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	buffer_cmd = kzalloc(sizeof(cmd), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	if (!buffer_cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	*buffer_cmd = cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	ret = usb_interrupt_msg(port->serial->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		buffer_cmd, sizeof(cmd),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		&actual_len, USB_CTRL_SET_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	kfree(buffer_cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	else if (actual_len != sizeof(cmd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	return 0;
^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) static void metrousb_read_int_callback(struct urb *urb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	struct usb_serial_port *port = urb->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	unsigned char *data = urb->transfer_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	int throttled = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	int result = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	unsigned long flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	dev_dbg(&port->dev, "%s\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	switch (urb->status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		/* Success status, read from the port. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	case -ECONNRESET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	case -ENOENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	case -ESHUTDOWN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		/* urb has been terminated. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		dev_dbg(&port->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 			"%s - urb shutting down, error code=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 			__func__, urb->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		dev_dbg(&port->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 			"%s - non-zero urb received, error code=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 			__func__, urb->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	/* Set the data read from the usb port into the serial port buffer. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	if (urb->actual_length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		/* Loop through the data copying each byte to the tty layer. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		tty_insert_flip_string(&port->port, data, urb->actual_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		/* Force the data to the tty layer. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		tty_flip_buffer_push(&port->port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	/* Set any port variables. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	spin_lock_irqsave(&metro_priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	throttled = metro_priv->throttled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	spin_unlock_irqrestore(&metro_priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	if (throttled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	/* Try to resubmit the urb. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	result = usb_submit_urb(urb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		dev_err(&port->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 			"%s - failed submitting interrupt in urb, error code=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 			__func__, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) static void metrousb_cleanup(struct usb_serial_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	usb_kill_urb(port->interrupt_in_urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	metrousb_send_unidirectional_cmd(UNI_CMD_CLOSE, port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) static int metrousb_open(struct tty_struct *tty, struct usb_serial_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	struct usb_serial *serial = port->serial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	unsigned long flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	int result = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	/* Set the private data information for the port. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	spin_lock_irqsave(&metro_priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	metro_priv->control_state = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	metro_priv->throttled = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	spin_unlock_irqrestore(&metro_priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	/* Clear the urb pipe. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	usb_clear_halt(serial->dev, port->interrupt_in_urb->pipe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	/* Start reading from the device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 			  usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 			   port->interrupt_in_urb->transfer_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 			   port->interrupt_in_urb->transfer_buffer_length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 			   metrousb_read_int_callback, port, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	if (result) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		dev_err(&port->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 			"%s - failed submitting interrupt in urb, error code=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 			__func__, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	/* Send activate cmd to device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	result = metrousb_send_unidirectional_cmd(UNI_CMD_OPEN, port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	if (result) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		dev_err(&port->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 			"%s - failed to configure device, error code=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 			__func__, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		goto err_kill_urb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) err_kill_urb:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	usb_kill_urb(port->interrupt_in_urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	return result;
^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) static int metrousb_set_modem_ctrl(struct usb_serial *serial, unsigned int control_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	int retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	unsigned char mcr = METROUSB_MCR_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	dev_dbg(&serial->dev->dev, "%s - control state = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		__func__, control_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	/* Set the modem control value. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	if (control_state & TIOCM_DTR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		mcr |= METROUSB_MCR_DTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	if (control_state & TIOCM_RTS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		mcr |= METROUSB_MCR_RTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	/* Send the command to the usb port. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	retval = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 				METROUSB_SET_REQUEST_TYPE, METROUSB_SET_MODEM_CTRL_REQUEST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 				control_state, 0, NULL, 0, WDR_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	if (retval < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		dev_err(&serial->dev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 			"%s - set modem ctrl=0x%x failed, error code=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 			__func__, mcr, retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) static int metrousb_port_probe(struct usb_serial_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	struct metrousb_private *metro_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	metro_priv = kzalloc(sizeof(*metro_priv), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	if (!metro_priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	spin_lock_init(&metro_priv->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	usb_set_serial_port_data(port, metro_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) static int metrousb_port_remove(struct usb_serial_port *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	struct metrousb_private *metro_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	metro_priv = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	kfree(metro_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) static void metrousb_throttle(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	struct usb_serial_port *port = tty->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	unsigned long flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	/* Set the private information for the port to stop reading data. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	spin_lock_irqsave(&metro_priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	metro_priv->throttled = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	spin_unlock_irqrestore(&metro_priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) static int metrousb_tiocmget(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	unsigned long control_state = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	struct usb_serial_port *port = tty->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	unsigned long flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	spin_lock_irqsave(&metro_priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	control_state = metro_priv->control_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	spin_unlock_irqrestore(&metro_priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	return control_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) static int metrousb_tiocmset(struct tty_struct *tty,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 			     unsigned int set, unsigned int clear)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	struct usb_serial_port *port = tty->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	struct usb_serial *serial = port->serial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	unsigned long flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	unsigned long control_state = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	dev_dbg(tty->dev, "%s - set=%d, clear=%d\n", __func__, set, clear);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	spin_lock_irqsave(&metro_priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	control_state = metro_priv->control_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	/* Set the RTS and DTR values. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	if (set & TIOCM_RTS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		control_state |= TIOCM_RTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	if (set & TIOCM_DTR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		control_state |= TIOCM_DTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	if (clear & TIOCM_RTS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		control_state &= ~TIOCM_RTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	if (clear & TIOCM_DTR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 		control_state &= ~TIOCM_DTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	metro_priv->control_state = control_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	spin_unlock_irqrestore(&metro_priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	return metrousb_set_modem_ctrl(serial, control_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) static void metrousb_unthrottle(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	struct usb_serial_port *port = tty->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	struct metrousb_private *metro_priv = usb_get_serial_port_data(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	unsigned long flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	int result = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	/* Set the private information for the port to resume reading data. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	spin_lock_irqsave(&metro_priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	metro_priv->throttled = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	spin_unlock_irqrestore(&metro_priv->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	/* Submit the urb to read from the port. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 		dev_err(tty->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 			"failed submitting interrupt in urb error code=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 			result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) static struct usb_serial_driver metrousb_device = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 		.owner =	THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 		.name =		"metro-usb",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	.description		= "Metrologic USB to Serial",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	.id_table		= id_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	.num_interrupt_in	= 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	.calc_num_ports		= metrousb_calc_num_ports,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	.open			= metrousb_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	.close			= metrousb_cleanup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	.read_int_callback	= metrousb_read_int_callback,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	.port_probe		= metrousb_port_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	.port_remove		= metrousb_port_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	.throttle		= metrousb_throttle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	.unthrottle		= metrousb_unthrottle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	.tiocmget		= metrousb_tiocmget,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	.tiocmset		= metrousb_tiocmset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) static struct usb_serial_driver * const serial_drivers[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	&metrousb_device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) module_usb_serial_driver(serial_drivers, id_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) MODULE_LICENSE("GPL v2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) MODULE_AUTHOR("Philip Nicastro");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) MODULE_AUTHOR("Aleksey Babahin <tamerlan311@gmail.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) MODULE_DESCRIPTION(DRIVER_DESC);