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