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)  * Copyright (C) 2003-2008 Takahiro Hirofuchi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (C) 2015-2016 Samsung Electronics
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *               Krzysztof Opasiak <k.opasiak@samsung.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #ifndef __USBIP_COMMON_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #define __USBIP_COMMON_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/compiler.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/net.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/printk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/usb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/wait.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/sched/task.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <uapi/linux/usbip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #undef pr_fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #ifdef DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #define pr_fmt(fmt)     KBUILD_MODNAME ": %s:%d: " fmt, __func__, __LINE__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #define pr_fmt(fmt)     KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	usbip_debug_xmit	= (1 << 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	usbip_debug_sysfs	= (1 << 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	usbip_debug_urb		= (1 << 2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	usbip_debug_eh		= (1 << 3),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	usbip_debug_stub_cmp	= (1 << 8),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	usbip_debug_stub_dev	= (1 << 9),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	usbip_debug_stub_rx	= (1 << 10),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	usbip_debug_stub_tx	= (1 << 11),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	usbip_debug_vhci_rh	= (1 << 8),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	usbip_debug_vhci_hc	= (1 << 9),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	usbip_debug_vhci_rx	= (1 << 10),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	usbip_debug_vhci_tx	= (1 << 11),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	usbip_debug_vhci_sysfs  = (1 << 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) #define usbip_dbg_flag_xmit	(usbip_debug_flag & usbip_debug_xmit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) #define usbip_dbg_flag_vhci_rh	(usbip_debug_flag & usbip_debug_vhci_rh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) #define usbip_dbg_flag_vhci_hc	(usbip_debug_flag & usbip_debug_vhci_hc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) #define usbip_dbg_flag_vhci_rx	(usbip_debug_flag & usbip_debug_vhci_rx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) #define usbip_dbg_flag_vhci_tx	(usbip_debug_flag & usbip_debug_vhci_tx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) #define usbip_dbg_flag_stub_rx	(usbip_debug_flag & usbip_debug_stub_rx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) #define usbip_dbg_flag_stub_tx	(usbip_debug_flag & usbip_debug_stub_tx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) #define usbip_dbg_flag_vhci_sysfs  (usbip_debug_flag & usbip_debug_vhci_sysfs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) extern unsigned long usbip_debug_flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) extern struct device_attribute dev_attr_usbip_debug;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) #define usbip_dbg_with_flag(flag, fmt, args...)		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	do {						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		if (flag & usbip_debug_flag)		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 			pr_debug(fmt, ##args);		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	} while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) #define usbip_dbg_sysfs(fmt, args...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	usbip_dbg_with_flag(usbip_debug_sysfs, fmt , ##args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) #define usbip_dbg_xmit(fmt, args...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	usbip_dbg_with_flag(usbip_debug_xmit, fmt , ##args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) #define usbip_dbg_urb(fmt, args...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	usbip_dbg_with_flag(usbip_debug_urb, fmt , ##args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) #define usbip_dbg_eh(fmt, args...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	usbip_dbg_with_flag(usbip_debug_eh, fmt , ##args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) #define usbip_dbg_vhci_rh(fmt, args...)	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	usbip_dbg_with_flag(usbip_debug_vhci_rh, fmt , ##args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) #define usbip_dbg_vhci_hc(fmt, args...)	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	usbip_dbg_with_flag(usbip_debug_vhci_hc, fmt , ##args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) #define usbip_dbg_vhci_rx(fmt, args...)	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	usbip_dbg_with_flag(usbip_debug_vhci_rx, fmt , ##args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) #define usbip_dbg_vhci_tx(fmt, args...)	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	usbip_dbg_with_flag(usbip_debug_vhci_tx, fmt , ##args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) #define usbip_dbg_vhci_sysfs(fmt, args...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	usbip_dbg_with_flag(usbip_debug_vhci_sysfs, fmt , ##args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) #define usbip_dbg_stub_cmp(fmt, args...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	usbip_dbg_with_flag(usbip_debug_stub_cmp, fmt , ##args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) #define usbip_dbg_stub_rx(fmt, args...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	usbip_dbg_with_flag(usbip_debug_stub_rx, fmt , ##args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) #define usbip_dbg_stub_tx(fmt, args...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	usbip_dbg_with_flag(usbip_debug_stub_tx, fmt , ##args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95)  * USB/IP request headers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97)  * Each request is transferred across the network to its counterpart, which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98)  * facilitates the normal USB communication. The values contained in the headers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99)  * are basically the same as in a URB. Currently, four request types are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)  * defined:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)  *  - USBIP_CMD_SUBMIT: a USB request block, corresponds to usb_submit_urb()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)  *    (client to server)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)  *  - USBIP_RET_SUBMIT: the result of USBIP_CMD_SUBMIT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)  *    (server to client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)  *  - USBIP_CMD_UNLINK: an unlink request of a pending USBIP_CMD_SUBMIT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)  *    corresponds to usb_unlink_urb()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)  *    (client to server)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)  *  - USBIP_RET_UNLINK: the result of USBIP_CMD_UNLINK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)  *    (server to client)
^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) #define USBIP_CMD_SUBMIT	0x0001
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) #define USBIP_CMD_UNLINK	0x0002
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) #define USBIP_RET_SUBMIT	0x0003
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) #define USBIP_RET_UNLINK	0x0004
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) #define USBIP_DIR_OUT	0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) #define USBIP_DIR_IN	0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)  * Arbitrary limit for the maximum number of isochronous packets in an URB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)  * compare for example the uhci_submit_isochronous function in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)  * drivers/usb/host/uhci-q.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) #define USBIP_MAX_ISO_PACKETS 1024
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)  * struct usbip_header_basic - data pertinent to every request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)  * @command: the usbip request type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)  * @seqnum: sequential number that identifies requests; incremented per
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)  *	    connection
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)  * @devid: specifies a remote USB device uniquely instead of busnum and devnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)  *	   in the stub driver, this value is ((busnum << 16) | devnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)  * @direction: direction of the transfer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)  * @ep: endpoint number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) struct usbip_header_basic {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	__u32 command;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	__u32 seqnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	__u32 devid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	__u32 direction;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	__u32 ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)  * struct usbip_header_cmd_submit - USBIP_CMD_SUBMIT packet header
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)  * @transfer_flags: URB flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)  * @transfer_buffer_length: the data size for (in) or (out) transfer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)  * @start_frame: initial frame for isochronous or interrupt transfers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)  * @number_of_packets: number of isochronous packets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)  * @interval: maximum time for the request on the server-side host controller
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)  * @setup: setup data for a control request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) struct usbip_header_cmd_submit {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	__u32 transfer_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	__s32 transfer_buffer_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	/* it is difficult for usbip to sync frames (reserved only?) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	__s32 start_frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	__s32 number_of_packets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	__s32 interval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	unsigned char setup[8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)  * struct usbip_header_ret_submit - USBIP_RET_SUBMIT packet header
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)  * @status: return status of a non-iso request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)  * @actual_length: number of bytes transferred
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)  * @start_frame: initial frame for isochronous or interrupt transfers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)  * @number_of_packets: number of isochronous packets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)  * @error_count: number of errors for isochronous transfers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) struct usbip_header_ret_submit {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	__s32 status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	__s32 actual_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	__s32 start_frame;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	__s32 number_of_packets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	__s32 error_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)  * struct usbip_header_cmd_unlink - USBIP_CMD_UNLINK packet header
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)  * @seqnum: the URB seqnum to unlink
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) struct usbip_header_cmd_unlink {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	__u32 seqnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)  * struct usbip_header_ret_unlink - USBIP_RET_UNLINK packet header
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)  * @status: return status of the request
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) struct usbip_header_ret_unlink {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	__s32 status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)  * struct usbip_header - common header for all usbip packets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)  * @base: the basic header
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)  * @u: packet type dependent header
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) struct usbip_header {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	struct usbip_header_basic base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		struct usbip_header_cmd_submit	cmd_submit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		struct usbip_header_ret_submit	ret_submit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		struct usbip_header_cmd_unlink	cmd_unlink;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		struct usbip_header_ret_unlink	ret_unlink;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	} u;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)  * This is the same as usb_iso_packet_descriptor but packed for pdu.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) struct usbip_iso_packet_descriptor {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	__u32 offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	__u32 length;			/* expected length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	__u32 actual_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	__u32 status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) enum usbip_side {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	USBIP_VHCI,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	USBIP_STUB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	USBIP_VUDC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) /* event handler */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) #define USBIP_EH_SHUTDOWN	(1 << 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) #define USBIP_EH_BYE		(1 << 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) #define USBIP_EH_RESET		(1 << 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) #define USBIP_EH_UNUSABLE	(1 << 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) #define	SDEV_EVENT_REMOVED	(USBIP_EH_SHUTDOWN | USBIP_EH_BYE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) #define	SDEV_EVENT_DOWN		(USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) #define	SDEV_EVENT_ERROR_TCP	(USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) #define	SDEV_EVENT_ERROR_SUBMIT	(USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) #define	SDEV_EVENT_ERROR_MALLOC	(USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) #define VUDC_EVENT_REMOVED   (USBIP_EH_SHUTDOWN | USBIP_EH_RESET | USBIP_EH_BYE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) #define	VUDC_EVENT_DOWN		(USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) #define	VUDC_EVENT_ERROR_TCP	(USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) /* catastrophic emulated usb error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) #define	VUDC_EVENT_ERROR_USB	(USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) #define	VUDC_EVENT_ERROR_MALLOC	(USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) #define	VDEV_EVENT_REMOVED (USBIP_EH_SHUTDOWN | USBIP_EH_RESET | USBIP_EH_BYE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) #define	VDEV_EVENT_DOWN		(USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) #define	VDEV_EVENT_ERROR_TCP	(USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) #define	VDEV_EVENT_ERROR_MALLOC	(USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) /* a common structure for stub_device and vhci_device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) struct usbip_device {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	enum usbip_side side;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	enum usbip_device_status status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	/* lock for status */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	spinlock_t lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	/* mutex for synchronizing sysfs store paths */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	struct mutex sysfs_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	int sockfd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	struct socket *tcp_socket;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	struct task_struct *tcp_rx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	struct task_struct *tcp_tx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	unsigned long event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	wait_queue_head_t eh_waitq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	struct eh_ops {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		void (*shutdown)(struct usbip_device *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		void (*reset)(struct usbip_device *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		void (*unusable)(struct usbip_device *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	} eh_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) #define kthread_get_run(threadfn, data, namefmt, ...)			   \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) ({									   \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	struct task_struct *__k						   \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		= kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	if (!IS_ERR(__k)) {						   \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		get_task_struct(__k);					   \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		wake_up_process(__k);					   \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	}								   \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	__k;								   \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) })
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) #define kthread_stop_put(k)		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	do {				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		kthread_stop(k);	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		put_task_struct(k);	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	} while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) /* usbip_common.c */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) void usbip_dump_urb(struct urb *purb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) void usbip_dump_header(struct usbip_header *pdu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) int usbip_recv(struct socket *sock, void *buf, int size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		    int pack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) void usbip_header_correct_endian(struct usbip_header *pdu, int send);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) struct usbip_iso_packet_descriptor*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) /* some members of urb must be substituted before. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) int usbip_recv_iso(struct usbip_device *ud, struct urb *urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) void usbip_pad_iso(struct usbip_device *ud, struct urb *urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) /* usbip_event.c */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) int usbip_init_eh(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) void usbip_finish_eh(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) int usbip_start_eh(struct usbip_device *ud);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) void usbip_stop_eh(struct usbip_device *ud);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) void usbip_event_add(struct usbip_device *ud, unsigned long event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) int usbip_event_happened(struct usbip_device *ud);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) int usbip_in_eh(struct task_struct *task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) static inline int interface_to_busnum(struct usb_interface *interface)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	struct usb_device *udev = interface_to_usbdev(interface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	return udev->bus->busnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) static inline int interface_to_devnum(struct usb_interface *interface)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	struct usb_device *udev = interface_to_usbdev(interface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	return udev->devnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) #endif /* __USBIP_COMMON_H */