18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Includes for cdc-acm.c
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Mainly take from usbnet's cdc-ether part
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci/*
118c2ecf20Sopenharmony_ci * CMSPAR, some architectures can't have space and mark parity.
128c2ecf20Sopenharmony_ci */
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#ifndef CMSPAR
158c2ecf20Sopenharmony_ci#define CMSPAR			0
168c2ecf20Sopenharmony_ci#endif
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci/*
198c2ecf20Sopenharmony_ci * Major and minor numbers.
208c2ecf20Sopenharmony_ci */
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#define ACM_TTY_MAJOR		166
238c2ecf20Sopenharmony_ci#define ACM_TTY_MINORS		256
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#define ACM_MINOR_INVALID	ACM_TTY_MINORS
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci/*
288c2ecf20Sopenharmony_ci * Requests.
298c2ecf20Sopenharmony_ci */
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#define USB_RT_ACM		(USB_TYPE_CLASS | USB_RECIP_INTERFACE)
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci/*
348c2ecf20Sopenharmony_ci * Output control lines.
358c2ecf20Sopenharmony_ci */
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#define ACM_CTRL_DTR		0x01
388c2ecf20Sopenharmony_ci#define ACM_CTRL_RTS		0x02
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci/*
418c2ecf20Sopenharmony_ci * Input control lines and line errors.
428c2ecf20Sopenharmony_ci */
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci#define ACM_CTRL_DCD		0x01
458c2ecf20Sopenharmony_ci#define ACM_CTRL_DSR		0x02
468c2ecf20Sopenharmony_ci#define ACM_CTRL_BRK		0x04
478c2ecf20Sopenharmony_ci#define ACM_CTRL_RI		0x08
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci#define ACM_CTRL_FRAMING	0x10
508c2ecf20Sopenharmony_ci#define ACM_CTRL_PARITY		0x20
518c2ecf20Sopenharmony_ci#define ACM_CTRL_OVERRUN	0x40
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci/*
548c2ecf20Sopenharmony_ci * Internal driver structures.
558c2ecf20Sopenharmony_ci */
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci/*
588c2ecf20Sopenharmony_ci * The only reason to have several buffers is to accommodate assumptions
598c2ecf20Sopenharmony_ci * in line disciplines. They ask for empty space amount, receive our URB size,
608c2ecf20Sopenharmony_ci * and proceed to issue several 1-character writes, assuming they will fit.
618c2ecf20Sopenharmony_ci * The very first write takes a complete URB. Fortunately, this only happens
628c2ecf20Sopenharmony_ci * when processing onlcr, so we only need 2 buffers. These values must be
638c2ecf20Sopenharmony_ci * powers of 2.
648c2ecf20Sopenharmony_ci */
658c2ecf20Sopenharmony_ci#define ACM_NW  16
668c2ecf20Sopenharmony_ci#define ACM_NR  16
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_cistruct acm_wb {
698c2ecf20Sopenharmony_ci	u8 *buf;
708c2ecf20Sopenharmony_ci	dma_addr_t dmah;
718c2ecf20Sopenharmony_ci	unsigned int len;
728c2ecf20Sopenharmony_ci	struct urb		*urb;
738c2ecf20Sopenharmony_ci	struct acm		*instance;
748c2ecf20Sopenharmony_ci	bool use;
758c2ecf20Sopenharmony_ci};
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_cistruct acm_rb {
788c2ecf20Sopenharmony_ci	int			size;
798c2ecf20Sopenharmony_ci	unsigned char		*base;
808c2ecf20Sopenharmony_ci	dma_addr_t		dma;
818c2ecf20Sopenharmony_ci	int			index;
828c2ecf20Sopenharmony_ci	struct acm		*instance;
838c2ecf20Sopenharmony_ci};
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_cistruct acm {
868c2ecf20Sopenharmony_ci	struct usb_device *dev;				/* the corresponding usb device */
878c2ecf20Sopenharmony_ci	struct usb_interface *control;			/* control interface */
888c2ecf20Sopenharmony_ci	struct usb_interface *data;			/* data interface */
898c2ecf20Sopenharmony_ci	unsigned in, out;				/* i/o pipes */
908c2ecf20Sopenharmony_ci	struct tty_port port;			 	/* our tty port data */
918c2ecf20Sopenharmony_ci	struct urb *ctrlurb;				/* urbs */
928c2ecf20Sopenharmony_ci	u8 *ctrl_buffer;				/* buffers of urbs */
938c2ecf20Sopenharmony_ci	dma_addr_t ctrl_dma;				/* dma handles of buffers */
948c2ecf20Sopenharmony_ci	u8 *country_codes;				/* country codes from device */
958c2ecf20Sopenharmony_ci	unsigned int country_code_size;			/* size of this buffer */
968c2ecf20Sopenharmony_ci	unsigned int country_rel_date;			/* release date of version */
978c2ecf20Sopenharmony_ci	struct acm_wb wb[ACM_NW];
988c2ecf20Sopenharmony_ci	unsigned long read_urbs_free;
998c2ecf20Sopenharmony_ci	struct urb *read_urbs[ACM_NR];
1008c2ecf20Sopenharmony_ci	struct acm_rb read_buffers[ACM_NR];
1018c2ecf20Sopenharmony_ci	int rx_buflimit;
1028c2ecf20Sopenharmony_ci	spinlock_t read_lock;
1038c2ecf20Sopenharmony_ci	u8 *notification_buffer;			/* to reassemble fragmented notifications */
1048c2ecf20Sopenharmony_ci	unsigned int nb_index;
1058c2ecf20Sopenharmony_ci	unsigned int nb_size;
1068c2ecf20Sopenharmony_ci	int transmitting;
1078c2ecf20Sopenharmony_ci	spinlock_t write_lock;
1088c2ecf20Sopenharmony_ci	struct mutex mutex;
1098c2ecf20Sopenharmony_ci	bool disconnected;
1108c2ecf20Sopenharmony_ci	unsigned long flags;
1118c2ecf20Sopenharmony_ci#		define EVENT_TTY_WAKEUP	0
1128c2ecf20Sopenharmony_ci#		define EVENT_RX_STALL	1
1138c2ecf20Sopenharmony_ci#		define ACM_THROTTLED	2
1148c2ecf20Sopenharmony_ci#		define ACM_ERROR_DELAY	3
1158c2ecf20Sopenharmony_ci	unsigned long urbs_in_error_delay;		/* these need to be restarted after a delay */
1168c2ecf20Sopenharmony_ci	struct usb_cdc_line_coding line;		/* bits, stop, parity */
1178c2ecf20Sopenharmony_ci	struct delayed_work dwork;		        /* work queue entry for various purposes */
1188c2ecf20Sopenharmony_ci	unsigned int ctrlin;				/* input control lines (DCD, DSR, RI, break, overruns) */
1198c2ecf20Sopenharmony_ci	unsigned int ctrlout;				/* output control lines (DTR, RTS) */
1208c2ecf20Sopenharmony_ci	struct async_icount iocount;			/* counters for control line changes */
1218c2ecf20Sopenharmony_ci	struct async_icount oldcount;			/* for comparison of counter */
1228c2ecf20Sopenharmony_ci	wait_queue_head_t wioctl;			/* for ioctl */
1238c2ecf20Sopenharmony_ci	unsigned int writesize;				/* max packet size for the output bulk endpoint */
1248c2ecf20Sopenharmony_ci	unsigned int readsize,ctrlsize;			/* buffer sizes for freeing */
1258c2ecf20Sopenharmony_ci	unsigned int minor;				/* acm minor number */
1268c2ecf20Sopenharmony_ci	unsigned char clocal;				/* termios CLOCAL */
1278c2ecf20Sopenharmony_ci	unsigned int ctrl_caps;				/* control capabilities from the class specific header */
1288c2ecf20Sopenharmony_ci	unsigned int susp_count;			/* number of suspended interfaces */
1298c2ecf20Sopenharmony_ci	unsigned int combined_interfaces:1;		/* control and data collapsed */
1308c2ecf20Sopenharmony_ci	u8 bInterval;
1318c2ecf20Sopenharmony_ci	struct usb_anchor delayed;			/* writes queued for a device about to be woken */
1328c2ecf20Sopenharmony_ci	unsigned long quirks;
1338c2ecf20Sopenharmony_ci};
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci/* constants describing various quirks and errors */
1368c2ecf20Sopenharmony_ci#define NO_UNION_NORMAL			BIT(0)
1378c2ecf20Sopenharmony_ci#define SINGLE_RX_URB			BIT(1)
1388c2ecf20Sopenharmony_ci#define NO_CAP_LINE			BIT(2)
1398c2ecf20Sopenharmony_ci#define IGNORE_DEVICE			BIT(3)
1408c2ecf20Sopenharmony_ci#define QUIRK_CONTROL_LINE_STATE	BIT(4)
1418c2ecf20Sopenharmony_ci#define CLEAR_HALT_CONDITIONS		BIT(5)
1428c2ecf20Sopenharmony_ci#define SEND_ZERO_PACKET		BIT(6)
1438c2ecf20Sopenharmony_ci#define DISABLE_ECHO			BIT(7)
144