Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * Copyright (C) 2003-2008 Takahiro Hirofuchi
0004  * Copyright (C) 2015-2016 Samsung Electronics
0005  *               Krzysztof Opasiak <k.opasiak@samsung.com>
0006  */
0007 
0008 #ifndef __USBIP_COMMON_H
0009 #define __USBIP_COMMON_H
0010 
0011 #include <linux/compiler.h>
0012 #include <linux/device.h>
0013 #include <linux/interrupt.h>
0014 #include <linux/net.h>
0015 #include <linux/printk.h>
0016 #include <linux/spinlock.h>
0017 #include <linux/types.h>
0018 #include <linux/usb.h>
0019 #include <linux/wait.h>
0020 #include <linux/sched/task.h>
0021 #include <linux/kcov.h>
0022 #include <uapi/linux/usbip.h>
0023 
0024 #undef pr_fmt
0025 
0026 #ifdef DEBUG
0027 #define pr_fmt(fmt)     KBUILD_MODNAME ": %s:%d: " fmt, __func__, __LINE__
0028 #else
0029 #define pr_fmt(fmt)     KBUILD_MODNAME ": " fmt
0030 #endif
0031 
0032 enum {
0033     usbip_debug_xmit    = (1 << 0),
0034     usbip_debug_sysfs   = (1 << 1),
0035     usbip_debug_urb     = (1 << 2),
0036     usbip_debug_eh      = (1 << 3),
0037 
0038     usbip_debug_stub_cmp    = (1 << 8),
0039     usbip_debug_stub_dev    = (1 << 9),
0040     usbip_debug_stub_rx = (1 << 10),
0041     usbip_debug_stub_tx = (1 << 11),
0042 
0043     usbip_debug_vhci_rh = (1 << 8),
0044     usbip_debug_vhci_hc = (1 << 9),
0045     usbip_debug_vhci_rx = (1 << 10),
0046     usbip_debug_vhci_tx = (1 << 11),
0047     usbip_debug_vhci_sysfs  = (1 << 12)
0048 };
0049 
0050 #define usbip_dbg_flag_xmit (usbip_debug_flag & usbip_debug_xmit)
0051 #define usbip_dbg_flag_vhci_rh  (usbip_debug_flag & usbip_debug_vhci_rh)
0052 #define usbip_dbg_flag_vhci_hc  (usbip_debug_flag & usbip_debug_vhci_hc)
0053 #define usbip_dbg_flag_vhci_rx  (usbip_debug_flag & usbip_debug_vhci_rx)
0054 #define usbip_dbg_flag_vhci_tx  (usbip_debug_flag & usbip_debug_vhci_tx)
0055 #define usbip_dbg_flag_stub_rx  (usbip_debug_flag & usbip_debug_stub_rx)
0056 #define usbip_dbg_flag_stub_tx  (usbip_debug_flag & usbip_debug_stub_tx)
0057 #define usbip_dbg_flag_vhci_sysfs  (usbip_debug_flag & usbip_debug_vhci_sysfs)
0058 
0059 extern unsigned long usbip_debug_flag;
0060 extern struct device_attribute dev_attr_usbip_debug;
0061 
0062 #define usbip_dbg_with_flag(flag, fmt, args...)     \
0063     do {                        \
0064         if (flag & usbip_debug_flag)        \
0065             pr_debug(fmt, ##args);      \
0066     } while (0)
0067 
0068 #define usbip_dbg_sysfs(fmt, args...) \
0069     usbip_dbg_with_flag(usbip_debug_sysfs, fmt , ##args)
0070 #define usbip_dbg_xmit(fmt, args...) \
0071     usbip_dbg_with_flag(usbip_debug_xmit, fmt , ##args)
0072 #define usbip_dbg_urb(fmt, args...) \
0073     usbip_dbg_with_flag(usbip_debug_urb, fmt , ##args)
0074 #define usbip_dbg_eh(fmt, args...) \
0075     usbip_dbg_with_flag(usbip_debug_eh, fmt , ##args)
0076 
0077 #define usbip_dbg_vhci_rh(fmt, args...) \
0078     usbip_dbg_with_flag(usbip_debug_vhci_rh, fmt , ##args)
0079 #define usbip_dbg_vhci_hc(fmt, args...) \
0080     usbip_dbg_with_flag(usbip_debug_vhci_hc, fmt , ##args)
0081 #define usbip_dbg_vhci_rx(fmt, args...) \
0082     usbip_dbg_with_flag(usbip_debug_vhci_rx, fmt , ##args)
0083 #define usbip_dbg_vhci_tx(fmt, args...) \
0084     usbip_dbg_with_flag(usbip_debug_vhci_tx, fmt , ##args)
0085 #define usbip_dbg_vhci_sysfs(fmt, args...) \
0086     usbip_dbg_with_flag(usbip_debug_vhci_sysfs, fmt , ##args)
0087 
0088 #define usbip_dbg_stub_cmp(fmt, args...) \
0089     usbip_dbg_with_flag(usbip_debug_stub_cmp, fmt , ##args)
0090 #define usbip_dbg_stub_rx(fmt, args...) \
0091     usbip_dbg_with_flag(usbip_debug_stub_rx, fmt , ##args)
0092 #define usbip_dbg_stub_tx(fmt, args...) \
0093     usbip_dbg_with_flag(usbip_debug_stub_tx, fmt , ##args)
0094 
0095 /*
0096  * USB/IP request headers
0097  *
0098  * Each request is transferred across the network to its counterpart, which
0099  * facilitates the normal USB communication. The values contained in the headers
0100  * are basically the same as in a URB. Currently, four request types are
0101  * defined:
0102  *
0103  *  - USBIP_CMD_SUBMIT: a USB request block, corresponds to usb_submit_urb()
0104  *    (client to server)
0105  *
0106  *  - USBIP_RET_SUBMIT: the result of USBIP_CMD_SUBMIT
0107  *    (server to client)
0108  *
0109  *  - USBIP_CMD_UNLINK: an unlink request of a pending USBIP_CMD_SUBMIT,
0110  *    corresponds to usb_unlink_urb()
0111  *    (client to server)
0112  *
0113  *  - USBIP_RET_UNLINK: the result of USBIP_CMD_UNLINK
0114  *    (server to client)
0115  *
0116  */
0117 #define USBIP_CMD_SUBMIT    0x0001
0118 #define USBIP_CMD_UNLINK    0x0002
0119 #define USBIP_RET_SUBMIT    0x0003
0120 #define USBIP_RET_UNLINK    0x0004
0121 
0122 #define USBIP_DIR_OUT   0x00
0123 #define USBIP_DIR_IN    0x01
0124 
0125 /*
0126  * Arbitrary limit for the maximum number of isochronous packets in an URB,
0127  * compare for example the uhci_submit_isochronous function in
0128  * drivers/usb/host/uhci-q.c
0129  */
0130 #define USBIP_MAX_ISO_PACKETS 1024
0131 
0132 /**
0133  * struct usbip_header_basic - data pertinent to every request
0134  * @command: the usbip request type
0135  * @seqnum: sequential number that identifies requests; incremented per
0136  *      connection
0137  * @devid: specifies a remote USB device uniquely instead of busnum and devnum;
0138  *     in the stub driver, this value is ((busnum << 16) | devnum)
0139  * @direction: direction of the transfer
0140  * @ep: endpoint number
0141  */
0142 struct usbip_header_basic {
0143     __u32 command;
0144     __u32 seqnum;
0145     __u32 devid;
0146     __u32 direction;
0147     __u32 ep;
0148 } __packed;
0149 
0150 /**
0151  * struct usbip_header_cmd_submit - USBIP_CMD_SUBMIT packet header
0152  * @transfer_flags: URB flags
0153  * @transfer_buffer_length: the data size for (in) or (out) transfer
0154  * @start_frame: initial frame for isochronous or interrupt transfers
0155  * @number_of_packets: number of isochronous packets
0156  * @interval: maximum time for the request on the server-side host controller
0157  * @setup: setup data for a control request
0158  */
0159 struct usbip_header_cmd_submit {
0160     __u32 transfer_flags;
0161     __s32 transfer_buffer_length;
0162 
0163     /* it is difficult for usbip to sync frames (reserved only?) */
0164     __s32 start_frame;
0165     __s32 number_of_packets;
0166     __s32 interval;
0167 
0168     unsigned char setup[8];
0169 } __packed;
0170 
0171 /**
0172  * struct usbip_header_ret_submit - USBIP_RET_SUBMIT packet header
0173  * @status: return status of a non-iso request
0174  * @actual_length: number of bytes transferred
0175  * @start_frame: initial frame for isochronous or interrupt transfers
0176  * @number_of_packets: number of isochronous packets
0177  * @error_count: number of errors for isochronous transfers
0178  */
0179 struct usbip_header_ret_submit {
0180     __s32 status;
0181     __s32 actual_length;
0182     __s32 start_frame;
0183     __s32 number_of_packets;
0184     __s32 error_count;
0185 } __packed;
0186 
0187 /**
0188  * struct usbip_header_cmd_unlink - USBIP_CMD_UNLINK packet header
0189  * @seqnum: the URB seqnum to unlink
0190  */
0191 struct usbip_header_cmd_unlink {
0192     __u32 seqnum;
0193 } __packed;
0194 
0195 /**
0196  * struct usbip_header_ret_unlink - USBIP_RET_UNLINK packet header
0197  * @status: return status of the request
0198  */
0199 struct usbip_header_ret_unlink {
0200     __s32 status;
0201 } __packed;
0202 
0203 /**
0204  * struct usbip_header - common header for all usbip packets
0205  * @base: the basic header
0206  * @u: packet type dependent header
0207  */
0208 struct usbip_header {
0209     struct usbip_header_basic base;
0210 
0211     union {
0212         struct usbip_header_cmd_submit  cmd_submit;
0213         struct usbip_header_ret_submit  ret_submit;
0214         struct usbip_header_cmd_unlink  cmd_unlink;
0215         struct usbip_header_ret_unlink  ret_unlink;
0216     } u;
0217 } __packed;
0218 
0219 /*
0220  * This is the same as usb_iso_packet_descriptor but packed for pdu.
0221  */
0222 struct usbip_iso_packet_descriptor {
0223     __u32 offset;
0224     __u32 length;           /* expected length */
0225     __u32 actual_length;
0226     __u32 status;
0227 } __packed;
0228 
0229 enum usbip_side {
0230     USBIP_VHCI,
0231     USBIP_STUB,
0232     USBIP_VUDC,
0233 };
0234 
0235 /* event handler */
0236 #define USBIP_EH_SHUTDOWN   (1 << 0)
0237 #define USBIP_EH_BYE        (1 << 1)
0238 #define USBIP_EH_RESET      (1 << 2)
0239 #define USBIP_EH_UNUSABLE   (1 << 3)
0240 
0241 #define SDEV_EVENT_REMOVED  (USBIP_EH_SHUTDOWN | USBIP_EH_BYE)
0242 #define SDEV_EVENT_DOWN     (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
0243 #define SDEV_EVENT_ERROR_TCP    (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
0244 #define SDEV_EVENT_ERROR_SUBMIT (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
0245 #define SDEV_EVENT_ERROR_MALLOC (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
0246 
0247 #define VUDC_EVENT_REMOVED   (USBIP_EH_SHUTDOWN | USBIP_EH_RESET | USBIP_EH_BYE)
0248 #define VUDC_EVENT_DOWN     (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
0249 #define VUDC_EVENT_ERROR_TCP    (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
0250 /* catastrophic emulated usb error */
0251 #define VUDC_EVENT_ERROR_USB    (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
0252 #define VUDC_EVENT_ERROR_MALLOC (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
0253 
0254 #define VDEV_EVENT_REMOVED (USBIP_EH_SHUTDOWN | USBIP_EH_RESET | USBIP_EH_BYE)
0255 #define VDEV_EVENT_DOWN     (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
0256 #define VDEV_EVENT_ERROR_TCP    (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
0257 #define VDEV_EVENT_ERROR_MALLOC (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
0258 
0259 /* a common structure for stub_device and vhci_device */
0260 struct usbip_device {
0261     enum usbip_side side;
0262     enum usbip_device_status status;
0263 
0264     /* lock for status */
0265     spinlock_t lock;
0266 
0267     /* mutex for synchronizing sysfs store paths */
0268     struct mutex sysfs_lock;
0269 
0270     int sockfd;
0271     struct socket *tcp_socket;
0272 
0273     struct task_struct *tcp_rx;
0274     struct task_struct *tcp_tx;
0275 
0276     unsigned long event;
0277     wait_queue_head_t eh_waitq;
0278 
0279     struct eh_ops {
0280         void (*shutdown)(struct usbip_device *);
0281         void (*reset)(struct usbip_device *);
0282         void (*unusable)(struct usbip_device *);
0283     } eh_ops;
0284 
0285 #ifdef CONFIG_KCOV
0286     u64 kcov_handle;
0287 #endif
0288 };
0289 
0290 #define kthread_get_run(threadfn, data, namefmt, ...)              \
0291 ({                                     \
0292     struct task_struct *__k                        \
0293         = kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \
0294     if (!IS_ERR(__k)) {                        \
0295         get_task_struct(__k);                      \
0296         wake_up_process(__k);                      \
0297     }                                  \
0298     __k;                                   \
0299 })
0300 
0301 #define kthread_stop_put(k)     \
0302     do {                \
0303         kthread_stop(k);    \
0304         put_task_struct(k); \
0305     } while (0)
0306 
0307 /* usbip_common.c */
0308 void usbip_dump_urb(struct urb *purb);
0309 void usbip_dump_header(struct usbip_header *pdu);
0310 
0311 int usbip_recv(struct socket *sock, void *buf, int size);
0312 
0313 void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd,
0314             int pack);
0315 void usbip_header_correct_endian(struct usbip_header *pdu, int send);
0316 
0317 struct usbip_iso_packet_descriptor*
0318 usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen);
0319 
0320 /* some members of urb must be substituted before. */
0321 int usbip_recv_iso(struct usbip_device *ud, struct urb *urb);
0322 void usbip_pad_iso(struct usbip_device *ud, struct urb *urb);
0323 int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb);
0324 
0325 /* usbip_event.c */
0326 int usbip_init_eh(void);
0327 void usbip_finish_eh(void);
0328 int usbip_start_eh(struct usbip_device *ud);
0329 void usbip_stop_eh(struct usbip_device *ud);
0330 void usbip_event_add(struct usbip_device *ud, unsigned long event);
0331 int usbip_event_happened(struct usbip_device *ud);
0332 int usbip_in_eh(struct task_struct *task);
0333 
0334 static inline int interface_to_busnum(struct usb_interface *interface)
0335 {
0336     struct usb_device *udev = interface_to_usbdev(interface);
0337 
0338     return udev->bus->busnum;
0339 }
0340 
0341 static inline int interface_to_devnum(struct usb_interface *interface)
0342 {
0343     struct usb_device *udev = interface_to_usbdev(interface);
0344 
0345     return udev->devnum;
0346 }
0347 
0348 #ifdef CONFIG_KCOV
0349 
0350 static inline void usbip_kcov_handle_init(struct usbip_device *ud)
0351 {
0352     ud->kcov_handle = kcov_common_handle();
0353 }
0354 
0355 static inline void usbip_kcov_remote_start(struct usbip_device *ud)
0356 {
0357     kcov_remote_start_common(ud->kcov_handle);
0358 }
0359 
0360 static inline void usbip_kcov_remote_stop(void)
0361 {
0362     kcov_remote_stop();
0363 }
0364 
0365 #else /* CONFIG_KCOV */
0366 
0367 static inline void usbip_kcov_handle_init(struct usbip_device *ud) { }
0368 static inline void usbip_kcov_remote_start(struct usbip_device *ud) { }
0369 static inline void usbip_kcov_remote_stop(void) { }
0370 
0371 #endif /* CONFIG_KCOV */
0372 
0373 #endif /* __USBIP_COMMON_H */