Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

usbip: add USBIP_URB_* URB transfer flags

USBIP driver packs URB transfer flags in network packets that are
exchanged between Server (usbip_host) and Client (vhci_hcd).

URB_* flags are internal to kernel and could change. Where as USBIP
URB flags exchanged in network packets are USBIP user API must not
change.

Add USBIP_URB* flags to make this an explicit API and change the
client and server to map them. Details as follows:

Client tx path (USBIP_CMD_SUBMIT):
- Maps URB_* to USBIP_URB_* when it sends USBIP_CMD_SUBMIT packet.

Server rx path (USBIP_CMD_SUBMIT):
- Maps USBIP_URB_* to URB_* when it receives USBIP_CMD_SUBMIT packet.

Flags aren't included in USBIP_CMD_UNLINK and USBIP_RET_SUBMIT packets
and no special handling is needed for them in the following cases:

- Server rx path (USBIP_CMD_UNLINK)
- Client rx path & Server tx path (USBIP_RET_SUBMIT)

Update protocol documentation to reflect the change.

Suggested-by: Hongren Zenithal Zheng <i@zenithal.me>
Suggested-by: Alan Stern <stern@rowland.harvard.edu>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
Link: https://lore.kernel.org/r/20220824002456.94605-1-skhan@linuxfoundation.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Shuah Khan and committed by
Greg Kroah-Hartman
8f36b3b4 dff98184

+122 -12
+6 -7
Documentation/usb/usbip_protocol.rst
··· 340 340 | 0 | 20 | usbip_header_basic, 'command' shall be 0x00000001 | 341 341 +-----------+--------+---------------------------------------------------+ 342 342 | 0x14 | 4 | transfer_flags: possible values depend on the | 343 - | | | URB transfer_flags (refer to URB doc in | 344 - | | | Documentation/driver-api/usb/URB.rst) | 345 - | | | but with URB_NO_TRANSFER_DMA_MAP masked. Refer to | 346 - | | | function usbip_pack_cmd_submit and function | 347 - | | | tweak_transfer_flags in drivers/usb/usbip/ | 348 - | | | usbip_common.c. The following fields may also ref | 349 - | | | to function usbip_pack_cmd_submit and URB doc | 343 + | | | USBIP_URB transfer_flags. | 344 + | | | Refer to include/uapi/linux/usbip.h and | 345 + | | | Documentation/driver-api/usb/URB.rst. | 346 + | | | Refer to usbip_pack_cmd_submit() and | 347 + | | | tweak_transfer_flags() in drivers/usb/usbip/ | 348 + | | | usbip_common.c. | 350 349 +-----------+--------+---------------------------------------------------+ 351 350 | 0x18 | 4 | transfer_buffer_length: | 352 351 | | | use URB transfer_buffer_length |
+2 -2
drivers/usb/usbip/stub_rx.c
··· 464 464 int nents; 465 465 int num_urbs = 1; 466 466 int pipe = get_pipe(sdev, pdu); 467 - int use_sg = pdu->u.cmd_submit.transfer_flags & URB_DMA_MAP_SG; 467 + int use_sg = pdu->u.cmd_submit.transfer_flags & USBIP_URB_DMA_MAP_SG; 468 468 int support_sg = 1; 469 469 int np = 0; 470 470 int ret, i; ··· 514 514 num_urbs = nents; 515 515 priv->completed_urbs = 0; 516 516 pdu->u.cmd_submit.transfer_flags &= 517 - ~URB_DMA_MAP_SG; 517 + ~USBIP_URB_DMA_MAP_SG; 518 518 } 519 519 } else { 520 520 buffer = kzalloc(buf_len, GFP_KERNEL);
+88 -3
drivers/usb/usbip/usbip_common.c
··· 344 344 return flags; 345 345 } 346 346 347 + /* 348 + * USBIP driver packs URB transfer flags in PDUs that are exchanged 349 + * between Server (usbip_host) and Client (vhci_hcd). URB_* flags 350 + * are internal to kernel and could change. Where as USBIP URB flags 351 + * exchanged in PDUs are USBIP user API must not change. 352 + * 353 + * USBIP_URB* flags are exported as explicit API and client and server 354 + * do mapping from kernel flags to USBIP_URB*. Details as follows: 355 + * 356 + * Client tx path (USBIP_CMD_SUBMIT): 357 + * - Maps URB_* to USBIP_URB_* when it sends USBIP_CMD_SUBMIT packet. 358 + * 359 + * Server rx path (USBIP_CMD_SUBMIT): 360 + * - Maps USBIP_URB_* to URB_* when it receives USBIP_CMD_SUBMIT packet. 361 + * 362 + * Flags aren't included in USBIP_CMD_UNLINK and USBIP_RET_SUBMIT packets 363 + * and no special handling is needed for them in the following cases: 364 + * - Server rx path (USBIP_CMD_UNLINK) 365 + * - Client rx path & Server tx path (USBIP_RET_SUBMIT) 366 + * 367 + * Code paths: 368 + * usbip_pack_pdu() is the common routine that handles packing pdu from 369 + * urb and unpack pdu to an urb. 370 + * 371 + * usbip_pack_cmd_submit() and usbip_pack_ret_submit() handle 372 + * USBIP_CMD_SUBMIT and USBIP_RET_SUBMIT respectively. 373 + * 374 + * usbip_map_urb_to_usbip() and usbip_map_usbip_to_urb() are used 375 + * by usbip_pack_cmd_submit() and usbip_pack_ret_submit() to map 376 + * flags. 377 + */ 378 + 379 + struct urb_to_usbip_flags { 380 + u32 urb_flag; 381 + u32 usbip_flag; 382 + }; 383 + 384 + #define NUM_USBIP_FLAGS 17 385 + 386 + static const struct urb_to_usbip_flags flag_map[NUM_USBIP_FLAGS] = { 387 + {URB_SHORT_NOT_OK, USBIP_URB_SHORT_NOT_OK}, 388 + {URB_ISO_ASAP, USBIP_URB_ISO_ASAP}, 389 + {URB_NO_TRANSFER_DMA_MAP, USBIP_URB_NO_TRANSFER_DMA_MAP}, 390 + {URB_ZERO_PACKET, USBIP_URB_ZERO_PACKET}, 391 + {URB_NO_INTERRUPT, USBIP_URB_NO_INTERRUPT}, 392 + {URB_FREE_BUFFER, USBIP_URB_FREE_BUFFER}, 393 + {URB_DIR_IN, USBIP_URB_DIR_IN}, 394 + {URB_DIR_OUT, USBIP_URB_DIR_OUT}, 395 + {URB_DIR_MASK, USBIP_URB_DIR_MASK}, 396 + {URB_DMA_MAP_SINGLE, USBIP_URB_DMA_MAP_SINGLE}, 397 + {URB_DMA_MAP_PAGE, USBIP_URB_DMA_MAP_PAGE}, 398 + {URB_DMA_MAP_SG, USBIP_URB_DMA_MAP_SG}, 399 + {URB_MAP_LOCAL, USBIP_URB_MAP_LOCAL}, 400 + {URB_SETUP_MAP_SINGLE, USBIP_URB_SETUP_MAP_SINGLE}, 401 + {URB_SETUP_MAP_LOCAL, USBIP_URB_SETUP_MAP_LOCAL}, 402 + {URB_DMA_SG_COMBINED, USBIP_URB_DMA_SG_COMBINED}, 403 + {URB_ALIGNED_TEMP_BUFFER, USBIP_URB_ALIGNED_TEMP_BUFFER}, 404 + }; 405 + 406 + static unsigned int urb_to_usbip(unsigned int flags) 407 + { 408 + unsigned int map_flags = 0; 409 + int loop; 410 + 411 + for (loop = 0; loop < NUM_USBIP_FLAGS; loop++) { 412 + if (flags & flag_map[loop].urb_flag) 413 + map_flags |= flag_map[loop].usbip_flag; 414 + } 415 + 416 + return map_flags; 417 + } 418 + 419 + static unsigned int usbip_to_urb(unsigned int flags) 420 + { 421 + unsigned int map_flags = 0; 422 + int loop; 423 + 424 + for (loop = 0; loop < NUM_USBIP_FLAGS; loop++) { 425 + if (flags & flag_map[loop].usbip_flag) 426 + map_flags |= flag_map[loop].urb_flag; 427 + } 428 + 429 + return map_flags; 430 + } 431 + 347 432 static void usbip_pack_cmd_submit(struct usbip_header *pdu, struct urb *urb, 348 433 int pack) 349 434 { ··· 439 354 * will be discussed when usbip is ported to other operating systems. 440 355 */ 441 356 if (pack) { 442 - spdu->transfer_flags = 443 - tweak_transfer_flags(urb->transfer_flags); 357 + /* map after tweaking the urb flags */ 358 + spdu->transfer_flags = urb_to_usbip(tweak_transfer_flags(urb->transfer_flags)); 444 359 spdu->transfer_buffer_length = urb->transfer_buffer_length; 445 360 spdu->start_frame = urb->start_frame; 446 361 spdu->number_of_packets = urb->number_of_packets; 447 362 spdu->interval = urb->interval; 448 363 } else { 449 - urb->transfer_flags = spdu->transfer_flags; 364 + urb->transfer_flags = usbip_to_urb(spdu->transfer_flags); 450 365 urb->transfer_buffer_length = spdu->transfer_buffer_length; 451 366 urb->start_frame = spdu->start_frame; 452 367 urb->number_of_packets = spdu->number_of_packets;
+26
include/uapi/linux/usbip.h
··· 24 24 VDEV_ST_USED, 25 25 VDEV_ST_ERROR 26 26 }; 27 + 28 + /* USB URB Transfer flags: 29 + * 30 + * USBIP server and client (vchi) pack URBs in TCP packets. The following 31 + * are the transfer type defines used in USBIP protocol. 32 + */ 33 + 34 + #define USBIP_URB_SHORT_NOT_OK 0x0001 35 + #define USBIP_URB_ISO_ASAP 0x0002 36 + #define USBIP_URB_NO_TRANSFER_DMA_MAP 0x0004 37 + #define USBIP_URB_ZERO_PACKET 0x0040 38 + #define USBIP_URB_NO_INTERRUPT 0x0080 39 + #define USBIP_URB_FREE_BUFFER 0x0100 40 + #define USBIP_URB_DIR_IN 0x0200 41 + #define USBIP_URB_DIR_OUT 0 42 + #define USBIP_URB_DIR_MASK USBIP_URB_DIR_IN 43 + 44 + #define USBIP_URB_DMA_MAP_SINGLE 0x00010000 45 + #define USBIP_URB_DMA_MAP_PAGE 0x00020000 46 + #define USBIP_URB_DMA_MAP_SG 0x00040000 47 + #define USBIP_URB_MAP_LOCAL 0x00080000 48 + #define USBIP_URB_SETUP_MAP_SINGLE 0x00100000 49 + #define USBIP_URB_SETUP_MAP_LOCAL 0x00200000 50 + #define USBIP_URB_DMA_SG_COMBINED 0x00400000 51 + #define USBIP_URB_ALIGNED_TEMP_BUFFER 0x00800000 52 + 27 53 #endif /* _UAPI_LINUX_USBIP_H */