^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) * composite.c - infrastructure for Composite USB Gadgets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2006-2008 David Brownell
^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) /* #define VERBOSE_DEBUG */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/kallsyms.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/utsname.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/bitfield.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/usb/composite.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/usb/otg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <asm/unaligned.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include "u_os_desc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * struct usb_os_string - represents OS String to be reported by a gadget
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * @bLength: total length of the entire descritor, always 0x12
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * @bDescriptorType: USB_DT_STRING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * @qwSignature: the OS String proper
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * @bMS_VendorCode: code used by the host for subsequent requests
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * @bPad: not used, must be zero
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct usb_os_string {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) __u8 bLength;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) __u8 bDescriptorType;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) __u8 qwSignature[OS_STRING_QW_SIGN_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) __u8 bMS_VendorCode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) __u8 bPad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * The code in this file is utility code, used to build a gadget driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * from one or more "function" drivers, one or more "configuration"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * objects, and a "usb_composite_driver" by gluing them together along
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * with the relevant device-wide data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) static struct usb_gadget_strings **get_containers_gs(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct usb_gadget_string_container *uc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) return (struct usb_gadget_strings **)uc->stash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * function_descriptors() - get function descriptors for speed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * @f: the function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * @speed: the speed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * Returns the descriptors or NULL if not set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) static struct usb_descriptor_header **
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) function_descriptors(struct usb_function *f,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) enum usb_device_speed speed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct usb_descriptor_header **descriptors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * NOTE: we try to help gadget drivers which might not be setting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * max_speed appropriately.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) switch (speed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) case USB_SPEED_SUPER_PLUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) descriptors = f->ssp_descriptors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) if (descriptors)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) case USB_SPEED_SUPER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) descriptors = f->ss_descriptors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if (descriptors)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) case USB_SPEED_HIGH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) descriptors = f->hs_descriptors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) if (descriptors)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) descriptors = f->fs_descriptors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) * if we can't find any descriptors at all, then this gadget deserves to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) * Oops with a NULL pointer dereference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) return descriptors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * next_desc() - advance to the next desc_type descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * @t: currect pointer within descriptor array
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * @desc_type: descriptor type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * Return: next desc_type descriptor or NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) * Iterate over @t until either desc_type descriptor found or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) * NULL (that indicates end of list) encountered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) static struct usb_descriptor_header**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) next_desc(struct usb_descriptor_header **t, u8 desc_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) for (; *t; t++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) if ((*t)->bDescriptorType == desc_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) return t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * for_each_desc() - iterate over desc_type descriptors in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) * descriptors list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) * @start: pointer within descriptor array.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) * @iter_desc: desc_type descriptor to use as the loop cursor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) * @desc_type: wanted descriptr type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) #define for_each_desc(start, iter_desc, desc_type) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) for (iter_desc = next_desc(start, desc_type); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) iter_desc; iter_desc = next_desc(iter_desc + 1, desc_type))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) * config_ep_by_speed_and_alt() - configures the given endpoint
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * according to gadget speed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * @g: pointer to the gadget
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * @f: usb function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) * @_ep: the endpoint to configure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) * @alt: alternate setting number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) * Return: error code, 0 on success
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * This function chooses the right descriptors for a given
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) * endpoint according to gadget speed and saves it in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) * endpoint desc field. If the endpoint already has a descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) * assigned to it - overwrites it with currently corresponding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) * descriptor. The endpoint maxpacket field is updated according
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) * to the chosen descriptor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) * Note: the supplied function should hold all the descriptors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * for supported speeds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) int config_ep_by_speed_and_alt(struct usb_gadget *g,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) struct usb_function *f,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) struct usb_ep *_ep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) u8 alt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) struct usb_endpoint_descriptor *chosen_desc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) struct usb_interface_descriptor *int_desc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) struct usb_descriptor_header **speed_desc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) struct usb_ss_ep_comp_descriptor *comp_desc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) int want_comp_desc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) struct usb_descriptor_header **d_spd; /* cursor for speed desc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) struct usb_composite_dev *cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) bool incomplete_desc = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) if (!g || !f || !_ep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) /* select desired speed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) switch (g->speed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) case USB_SPEED_SUPER_PLUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (gadget_is_superspeed_plus(g)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) if (f->ssp_descriptors) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) speed_desc = f->ssp_descriptors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) want_comp_desc = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) incomplete_desc = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) case USB_SPEED_SUPER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) if (gadget_is_superspeed(g)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) if (f->ss_descriptors) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) speed_desc = f->ss_descriptors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) want_comp_desc = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) incomplete_desc = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) case USB_SPEED_HIGH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) if (gadget_is_dualspeed(g)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) if (f->hs_descriptors) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) speed_desc = f->hs_descriptors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) incomplete_desc = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) speed_desc = f->fs_descriptors;
^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) cdev = get_gadget_data(g);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) if (incomplete_desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) WARNING(cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) "%s doesn't hold the descriptors for current speed\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) f->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) /* find correct alternate setting descriptor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) for_each_desc(speed_desc, d_spd, USB_DT_INTERFACE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) int_desc = (struct usb_interface_descriptor *)*d_spd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) if (int_desc->bAlternateSetting == alt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) speed_desc = d_spd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) goto intf_found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) intf_found:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) /* find descriptors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) for_each_desc(speed_desc, d_spd, USB_DT_ENDPOINT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) chosen_desc = (struct usb_endpoint_descriptor *)*d_spd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) if (chosen_desc->bEndpointAddress == _ep->address)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) goto ep_found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) ep_found:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) /* commit results */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) _ep->maxpacket = usb_endpoint_maxp(chosen_desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) _ep->desc = chosen_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) _ep->comp_desc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) _ep->maxburst = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) _ep->mult = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) if (g->speed == USB_SPEED_HIGH && (usb_endpoint_xfer_isoc(_ep->desc) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) usb_endpoint_xfer_int(_ep->desc)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) _ep->mult = usb_endpoint_maxp_mult(_ep->desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) if (!want_comp_desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) * Companion descriptor should follow EP descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) * USB 3.0 spec, #9.6.7
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) comp_desc = (struct usb_ss_ep_comp_descriptor *)*(++d_spd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) if (!comp_desc ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) (comp_desc->bDescriptorType != USB_DT_SS_ENDPOINT_COMP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) _ep->comp_desc = comp_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) if (g->speed >= USB_SPEED_SUPER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) switch (usb_endpoint_type(_ep->desc)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) case USB_ENDPOINT_XFER_ISOC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) /* mult: bits 1:0 of bmAttributes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) _ep->mult = (comp_desc->bmAttributes & 0x3) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) case USB_ENDPOINT_XFER_BULK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) case USB_ENDPOINT_XFER_INT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) _ep->maxburst = comp_desc->bMaxBurst + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) if (comp_desc->bMaxBurst != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) ERROR(cdev, "ep0 bMaxBurst must be 0\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) _ep->maxburst = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) EXPORT_SYMBOL_GPL(config_ep_by_speed_and_alt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) * config_ep_by_speed() - configures the given endpoint
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) * according to gadget speed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) * @g: pointer to the gadget
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) * @f: usb function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) * @_ep: the endpoint to configure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) * Return: error code, 0 on success
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) * This function chooses the right descriptors for a given
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) * endpoint according to gadget speed and saves it in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) * endpoint desc field. If the endpoint already has a descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) * assigned to it - overwrites it with currently corresponding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) * descriptor. The endpoint maxpacket field is updated according
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) * to the chosen descriptor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) * Note: the supplied function should hold all the descriptors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) * for supported speeds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) int config_ep_by_speed(struct usb_gadget *g,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) struct usb_function *f,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) struct usb_ep *_ep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) return config_ep_by_speed_and_alt(g, f, _ep, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) EXPORT_SYMBOL_GPL(config_ep_by_speed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) * usb_add_function() - add a function to a configuration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) * @config: the configuration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) * @function: the function being added
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) * Context: single threaded during gadget setup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) * After initialization, each configuration must have one or more
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) * functions added to it. Adding a function involves calling its @bind()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) * method to allocate resources such as interface and string identifiers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) * and endpoints.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) * This function returns the value of the function's bind(), which is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) * zero for success else a negative errno value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) int usb_add_function(struct usb_configuration *config,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) struct usb_function *function)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) int value = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) DBG(config->cdev, "adding '%s'/%p to config '%s'/%p\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) function->name, function,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) config->label, config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) if (!function->set_alt || !function->disable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) function->config = config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) list_add_tail(&function->list, &config->functions);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) if (function->bind_deactivated) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) value = usb_function_deactivate(function);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) if (value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) /* REVISIT *require* function->bind? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) if (function->bind) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) value = function->bind(config, function);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) if (value < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) list_del(&function->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) function->config = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) value = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) /* We allow configurations that don't work at both speeds.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) * If we run into a lowspeed Linux system, treat it the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) * as full speed ... it's the function drivers that will need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) * to avoid bulk and ISO transfers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) if (!config->fullspeed && function->fs_descriptors)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) config->fullspeed = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) if (!config->highspeed && function->hs_descriptors)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) config->highspeed = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) if (!config->superspeed && function->ss_descriptors)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) config->superspeed = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) if (!config->superspeed_plus && function->ssp_descriptors)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) config->superspeed_plus = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) if (value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) DBG(config->cdev, "adding '%s'/%p --> %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) function->name, function, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) return value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) EXPORT_SYMBOL_GPL(usb_add_function);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) void usb_remove_function(struct usb_configuration *c, struct usb_function *f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) if (f->disable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) f->disable(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) bitmap_zero(f->endpoints, 32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) list_del(&f->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) if (f->unbind)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) f->unbind(c, f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) if (f->bind_deactivated)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) usb_function_activate(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) EXPORT_SYMBOL_GPL(usb_remove_function);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) * usb_function_deactivate - prevent function and gadget enumeration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) * @function: the function that isn't yet ready to respond
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) * Blocks response of the gadget driver to host enumeration by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) * preventing the data line pullup from being activated. This is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) * normally called during @bind() processing to change from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) * initial "ready to respond" state, or when a required resource
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) * becomes available.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) * For example, drivers that serve as a passthrough to a userspace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) * daemon can block enumeration unless that daemon (such as an OBEX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) * MTP, or print server) is ready to handle host requests.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) * Not all systems support software control of their USB peripheral
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) * data pullups.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) * Returns zero on success, else negative errno.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) int usb_function_deactivate(struct usb_function *function)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) struct usb_composite_dev *cdev = function->config->cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) int status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) spin_lock_irqsave(&cdev->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) if (cdev->deactivations == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) spin_unlock_irqrestore(&cdev->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) status = usb_gadget_deactivate(cdev->gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) spin_lock_irqsave(&cdev->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) if (status == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) cdev->deactivations++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) spin_unlock_irqrestore(&cdev->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) EXPORT_SYMBOL_GPL(usb_function_deactivate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) * usb_function_activate - allow function and gadget enumeration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) * @function: function on which usb_function_activate() was called
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) * Reverses effect of usb_function_deactivate(). If no more functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) * are delaying their activation, the gadget driver will respond to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) * host enumeration procedures.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) * Returns zero on success, else negative errno.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) int usb_function_activate(struct usb_function *function)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) struct usb_composite_dev *cdev = function->config->cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) int status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) spin_lock_irqsave(&cdev->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) if (WARN_ON(cdev->deactivations == 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) status = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) cdev->deactivations--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) if (cdev->deactivations == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) spin_unlock_irqrestore(&cdev->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) status = usb_gadget_activate(cdev->gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) spin_lock_irqsave(&cdev->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) spin_unlock_irqrestore(&cdev->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) EXPORT_SYMBOL_GPL(usb_function_activate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) * usb_interface_id() - allocate an unused interface ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) * @config: configuration associated with the interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) * @function: function handling the interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) * Context: single threaded during gadget setup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) * usb_interface_id() is called from usb_function.bind() callbacks to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) * allocate new interface IDs. The function driver will then store that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) * ID in interface, association, CDC union, and other descriptors. It
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) * will also handle any control requests targeted at that interface,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) * particularly changing its altsetting via set_alt(). There may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) * also be class-specific or vendor-specific requests to handle.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) * All interface identifier should be allocated using this routine, to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) * ensure that for example different functions don't wrongly assign
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) * different meanings to the same identifier. Note that since interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) * identifiers are configuration-specific, functions used in more than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) * one configuration (or more than once in a given configuration) need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) * multiple versions of the relevant descriptors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) * Returns the interface ID which was allocated; or -ENODEV if no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) * more interface IDs can be allocated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) int usb_interface_id(struct usb_configuration *config,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) struct usb_function *function)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) unsigned id = config->next_interface_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) if (id < MAX_CONFIG_INTERFACES) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) config->interface[id] = function;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) config->next_interface_id = id + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) return id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) EXPORT_SYMBOL_GPL(usb_interface_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) static u8 encode_bMaxPower(enum usb_device_speed speed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) struct usb_configuration *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) unsigned val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) if (c->MaxPower || (c->bmAttributes & USB_CONFIG_ATT_SELFPOWER))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) val = c->MaxPower;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) val = CONFIG_USB_GADGET_VBUS_DRAW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) if (!val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) if (speed < USB_SPEED_SUPER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) return min(val, 500U) / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) * USB 3.x supports up to 900mA, but since 900 isn't divisible
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) * by 8 the integral division will effectively cap to 896mA.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) return min(val, 900U) / 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) static int config_buf(struct usb_configuration *config,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) enum usb_device_speed speed, void *buf, u8 type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) struct usb_config_descriptor *c = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) void *next = buf + USB_DT_CONFIG_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) struct usb_function *f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) len = USB_COMP_EP0_BUFSIZ - USB_DT_CONFIG_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) /* write the config descriptor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) c = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) c->bLength = USB_DT_CONFIG_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) c->bDescriptorType = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) /* wTotalLength is written later */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) c->bNumInterfaces = config->next_interface_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) c->bConfigurationValue = config->bConfigurationValue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) c->iConfiguration = config->iConfiguration;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) c->bMaxPower = encode_bMaxPower(speed, config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) /* There may be e.g. OTG descriptors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) if (config->descriptors) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) status = usb_descriptor_fillbuf(next, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) config->descriptors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) len -= status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) next += status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) /* add each function's descriptors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) list_for_each_entry(f, &config->functions, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) struct usb_descriptor_header **descriptors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) descriptors = function_descriptors(f, speed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) if (!descriptors)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) status = usb_descriptor_fillbuf(next, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) (const struct usb_descriptor_header **) descriptors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) len -= status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) next += status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) len = next - buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) c->wTotalLength = cpu_to_le16(len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) static int config_desc(struct usb_composite_dev *cdev, unsigned w_value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) struct usb_gadget *gadget = cdev->gadget;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) struct usb_configuration *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) struct list_head *pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) u8 type = w_value >> 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) enum usb_device_speed speed = USB_SPEED_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) if (gadget->speed >= USB_SPEED_SUPER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) speed = gadget->speed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) else if (gadget_is_dualspeed(gadget)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) int hs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) if (gadget->speed == USB_SPEED_HIGH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) hs = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) if (type == USB_DT_OTHER_SPEED_CONFIG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) hs = !hs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) if (hs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) speed = USB_SPEED_HIGH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) /* This is a lookup by config *INDEX* */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) w_value &= 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) pos = &cdev->configs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) c = cdev->os_desc_config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) if (c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) goto check_config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) while ((pos = pos->next) != &cdev->configs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) c = list_entry(pos, typeof(*c), list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) /* skip OS Descriptors config which is handled separately */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) if (c == cdev->os_desc_config)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) check_config:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) /* ignore configs that won't work at this speed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) switch (speed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) case USB_SPEED_SUPER_PLUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) if (!c->superspeed_plus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) case USB_SPEED_SUPER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) if (!c->superspeed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) case USB_SPEED_HIGH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) if (!c->highspeed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) if (!c->fullspeed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) if (w_value == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) return config_buf(c, speed, cdev->req->buf, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) w_value--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) static int count_configs(struct usb_composite_dev *cdev, unsigned type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) struct usb_gadget *gadget = cdev->gadget;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) struct usb_configuration *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) unsigned count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) int hs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) int ss = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) int ssp = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) if (gadget_is_dualspeed(gadget)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) if (gadget->speed == USB_SPEED_HIGH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) hs = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) if (gadget->speed == USB_SPEED_SUPER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) ss = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) if (gadget->speed == USB_SPEED_SUPER_PLUS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) ssp = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) if (type == USB_DT_DEVICE_QUALIFIER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) hs = !hs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) list_for_each_entry(c, &cdev->configs, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) /* ignore configs that won't work at this speed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) if (ssp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) if (!c->superspeed_plus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) } else if (ss) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) if (!c->superspeed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) } else if (hs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) if (!c->highspeed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) if (!c->fullspeed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) * bos_desc() - prepares the BOS descriptor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) * @cdev: pointer to usb_composite device to generate the bos
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) * descriptor for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) * This function generates the BOS (Binary Device Object)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) * descriptor and its device capabilities descriptors. The BOS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) * descriptor should be supported by a SuperSpeed device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) static int bos_desc(struct usb_composite_dev *cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) struct usb_ext_cap_descriptor *usb_ext;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) struct usb_dcd_config_params dcd_config_params;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) struct usb_bos_descriptor *bos = cdev->req->buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) unsigned int besl = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) bos->bLength = USB_DT_BOS_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) bos->bDescriptorType = USB_DT_BOS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) bos->wTotalLength = cpu_to_le16(USB_DT_BOS_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) bos->bNumDeviceCaps = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) /* Get Controller configuration */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) if (cdev->gadget->ops->get_config_params) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) cdev->gadget->ops->get_config_params(cdev->gadget,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) &dcd_config_params);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) dcd_config_params.besl_baseline =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) USB_DEFAULT_BESL_UNSPECIFIED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) dcd_config_params.besl_deep =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) USB_DEFAULT_BESL_UNSPECIFIED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) dcd_config_params.bU1devExitLat =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) USB_DEFAULT_U1_DEV_EXIT_LAT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) dcd_config_params.bU2DevExitLat =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) cpu_to_le16(USB_DEFAULT_U2_DEV_EXIT_LAT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) if (dcd_config_params.besl_baseline != USB_DEFAULT_BESL_UNSPECIFIED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) besl = USB_BESL_BASELINE_VALID |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) USB_SET_BESL_BASELINE(dcd_config_params.besl_baseline);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) if (dcd_config_params.besl_deep != USB_DEFAULT_BESL_UNSPECIFIED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) besl |= USB_BESL_DEEP_VALID |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) USB_SET_BESL_DEEP(dcd_config_params.besl_deep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) * A SuperSpeed device shall include the USB2.0 extension descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) * and shall support LPM when operating in USB2.0 HS mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) usb_ext = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) bos->bNumDeviceCaps++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) le16_add_cpu(&bos->wTotalLength, USB_DT_USB_EXT_CAP_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) usb_ext->bLength = USB_DT_USB_EXT_CAP_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) usb_ext->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) usb_ext->bDevCapabilityType = USB_CAP_TYPE_EXT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) usb_ext->bmAttributes = cpu_to_le32(USB_LPM_SUPPORT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) USB_BESL_SUPPORT | besl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) * The Superspeed USB Capability descriptor shall be implemented by all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) * SuperSpeed devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) if (gadget_is_superspeed(cdev->gadget)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) struct usb_ss_cap_descriptor *ss_cap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) ss_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) bos->bNumDeviceCaps++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SS_CAP_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) ss_cap->bLength = USB_DT_USB_SS_CAP_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) ss_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) ss_cap->bDevCapabilityType = USB_SS_CAP_TYPE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) ss_cap->bmAttributes = 0; /* LTM is not supported yet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) ss_cap->wSpeedSupported = cpu_to_le16(USB_LOW_SPEED_OPERATION |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) USB_FULL_SPEED_OPERATION |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) USB_HIGH_SPEED_OPERATION |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) USB_5GBPS_OPERATION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) ss_cap->bFunctionalitySupport = USB_LOW_SPEED_OPERATION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) ss_cap->bU1devExitLat = dcd_config_params.bU1devExitLat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) ss_cap->bU2DevExitLat = dcd_config_params.bU2DevExitLat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) /* The SuperSpeedPlus USB Device Capability descriptor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) if (gadget_is_superspeed_plus(cdev->gadget)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) struct usb_ssp_cap_descriptor *ssp_cap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) u8 ssac = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) u8 ssic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) if (cdev->gadget->max_ssp_rate == USB_SSP_GEN_2x2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) ssac = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) * Paired RX and TX sublink speed attributes share
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) * the same SSID.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) ssic = (ssac + 1) / 2 - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) ssp_cap = cdev->req->buf + le16_to_cpu(bos->wTotalLength);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) bos->bNumDeviceCaps++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) le16_add_cpu(&bos->wTotalLength, USB_DT_USB_SSP_CAP_SIZE(ssac));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) ssp_cap->bLength = USB_DT_USB_SSP_CAP_SIZE(ssac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) ssp_cap->bDescriptorType = USB_DT_DEVICE_CAPABILITY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) ssp_cap->bDevCapabilityType = USB_SSP_CAP_TYPE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) ssp_cap->bReserved = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) ssp_cap->wReserved = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) ssp_cap->bmAttributes =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) cpu_to_le32(FIELD_PREP(USB_SSP_SUBLINK_SPEED_ATTRIBS, ssac) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) FIELD_PREP(USB_SSP_SUBLINK_SPEED_IDS, ssic));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) ssp_cap->wFunctionalitySupport =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) cpu_to_le16(FIELD_PREP(USB_SSP_MIN_SUBLINK_SPEED_ATTRIBUTE_ID, 0) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) FIELD_PREP(USB_SSP_MIN_RX_LANE_COUNT, 1) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) FIELD_PREP(USB_SSP_MIN_TX_LANE_COUNT, 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) * Use 1 SSID if the gadget supports up to gen2x1 or not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) * specified:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) * - SSID 0 for symmetric RX/TX sublink speed of 10 Gbps.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) * Use 1 SSID if the gadget supports up to gen1x2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) * - SSID 0 for symmetric RX/TX sublink speed of 5 Gbps.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) * Use 2 SSIDs if the gadget supports up to gen2x2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) * - SSID 0 for symmetric RX/TX sublink speed of 5 Gbps.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) * - SSID 1 for symmetric RX/TX sublink speed of 10 Gbps.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) for (i = 0; i < ssac + 1; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) u8 ssid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) u8 mantissa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) u8 type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) ssid = i >> 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) if (cdev->gadget->max_ssp_rate == USB_SSP_GEN_2x1 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) cdev->gadget->max_ssp_rate == USB_SSP_GEN_UNKNOWN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) mantissa = 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) mantissa = 5 << ssid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) if (i % 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) type = USB_SSP_SUBLINK_SPEED_ST_SYM_TX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) type = USB_SSP_SUBLINK_SPEED_ST_SYM_RX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) ssp_cap->bmSublinkSpeedAttr[i] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) cpu_to_le32(FIELD_PREP(USB_SSP_SUBLINK_SPEED_SSID, ssid) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) FIELD_PREP(USB_SSP_SUBLINK_SPEED_LSE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) USB_SSP_SUBLINK_SPEED_LSE_GBPS) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) FIELD_PREP(USB_SSP_SUBLINK_SPEED_ST, type) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) FIELD_PREP(USB_SSP_SUBLINK_SPEED_LP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) USB_SSP_SUBLINK_SPEED_LP_SSP) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) FIELD_PREP(USB_SSP_SUBLINK_SPEED_LSM, mantissa));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) return le16_to_cpu(bos->wTotalLength);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) static void device_qual(struct usb_composite_dev *cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) struct usb_qualifier_descriptor *qual = cdev->req->buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) qual->bLength = sizeof(*qual);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) /* POLICY: same bcdUSB and device type info at both speeds */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) qual->bcdUSB = cdev->desc.bcdUSB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) qual->bDeviceClass = cdev->desc.bDeviceClass;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) /* ASSUME same EP0 fifo size at both speeds */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) qual->bMaxPacketSize0 = cdev->gadget->ep0->maxpacket;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) qual->bRESERVED = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) /*-------------------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) static void reset_config(struct usb_composite_dev *cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) struct usb_function *f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) DBG(cdev, "reset config\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) list_for_each_entry(f, &cdev->config->functions, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) if (f->disable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) f->disable(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) bitmap_zero(f->endpoints, 32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) cdev->config = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) cdev->delayed_status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) static int set_config(struct usb_composite_dev *cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) const struct usb_ctrlrequest *ctrl, unsigned number)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) struct usb_gadget *gadget = cdev->gadget;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) struct usb_configuration *c = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) int result = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) unsigned power = gadget_is_otg(gadget) ? 8 : 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) int tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) if (number) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) list_for_each_entry(c, &cdev->configs, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) if (c->bConfigurationValue == number) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) * We disable the FDs of the previous
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) * configuration only if the new configuration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) * is a valid one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) if (cdev->config)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) reset_config(cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) result = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) if (result < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) } else { /* Zero configuration value - need to reset the config */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) if (cdev->config)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) reset_config(cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) result = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) DBG(cdev, "%s config #%d: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) usb_speed_string(gadget->speed),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) number, c ? c->label : "unconfigured");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) if (!c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) usb_gadget_set_state(gadget, USB_STATE_CONFIGURED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) cdev->config = c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) /* Initialize all interfaces by setting them to altsetting zero. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) struct usb_function *f = c->interface[tmp];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) struct usb_descriptor_header **descriptors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) if (!f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) * Record which endpoints are used by the function. This is used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) * to dispatch control requests targeted at that endpoint to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) * function's setup callback instead of the current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) * configuration's setup callback.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) descriptors = function_descriptors(f, gadget->speed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) for (; *descriptors; ++descriptors) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) struct usb_endpoint_descriptor *ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) int addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) ep = (struct usb_endpoint_descriptor *)*descriptors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) addr = ((ep->bEndpointAddress & 0x80) >> 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) | (ep->bEndpointAddress & 0x0f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) set_bit(addr, f->endpoints);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) result = f->set_alt(f, tmp, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) if (result < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) DBG(cdev, "interface %d (%s/%p) alt 0 --> %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) tmp, f->name, f, result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) reset_config(cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) if (result == USB_GADGET_DELAYED_STATUS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) DBG(cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) "%s: interface %d (%s) requested delayed status\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) __func__, tmp, f->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) cdev->delayed_status++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) DBG(cdev, "delayed_status count %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) cdev->delayed_status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) /* when we return, be sure our power usage is valid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) if (c->MaxPower || (c->bmAttributes & USB_CONFIG_ATT_SELFPOWER))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) power = c->MaxPower;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) power = CONFIG_USB_GADGET_VBUS_DRAW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) if (gadget->speed < USB_SPEED_SUPER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) power = min(power, 500U);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) power = min(power, 900U);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) if (power <= USB_SELF_POWER_VBUS_MAX_DRAW)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) usb_gadget_set_selfpowered(gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) usb_gadget_clear_selfpowered(gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) usb_gadget_vbus_draw(gadget, power);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) if (result >= 0 && cdev->delayed_status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) result = USB_GADGET_DELAYED_STATUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) int usb_add_config_only(struct usb_composite_dev *cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) struct usb_configuration *config)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) struct usb_configuration *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) if (!config->bConfigurationValue)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) /* Prevent duplicate configuration identifiers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) list_for_each_entry(c, &cdev->configs, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) if (c->bConfigurationValue == config->bConfigurationValue)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) config->cdev = cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) list_add_tail(&config->list, &cdev->configs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) INIT_LIST_HEAD(&config->functions);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) config->next_interface_id = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) memset(config->interface, 0, sizeof(config->interface));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) EXPORT_SYMBOL_GPL(usb_add_config_only);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) * usb_add_config() - add a configuration to a device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) * @cdev: wraps the USB gadget
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) * @config: the configuration, with bConfigurationValue assigned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) * @bind: the configuration's bind function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) * Context: single threaded during gadget setup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) * One of the main tasks of a composite @bind() routine is to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) * add each of the configurations it supports, using this routine.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) * This function returns the value of the configuration's @bind(), which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) * is zero for success else a negative errno value. Binding configurations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) * assigns global resources including string IDs, and per-configuration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) * resources such as interface IDs and endpoints.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) int usb_add_config(struct usb_composite_dev *cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) struct usb_configuration *config,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) int (*bind)(struct usb_configuration *))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) int status = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) if (!bind)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) DBG(cdev, "adding config #%u '%s'/%p\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) config->bConfigurationValue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) config->label, config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) status = usb_add_config_only(cdev, config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) status = bind(config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) while (!list_empty(&config->functions)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) struct usb_function *f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) f = list_first_entry(&config->functions,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) struct usb_function, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) list_del(&f->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) if (f->unbind) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) DBG(cdev, "unbind function '%s'/%p\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) f->name, f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) f->unbind(config, f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) /* may free memory for "f" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) list_del(&config->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) config->cdev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) unsigned i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) DBG(cdev, "cfg %d/%p speeds:%s%s%s%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) config->bConfigurationValue, config,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) config->superspeed_plus ? " superplus" : "",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) config->superspeed ? " super" : "",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) config->highspeed ? " high" : "",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) config->fullspeed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) ? (gadget_is_dualspeed(cdev->gadget)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) ? " full"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) : " full/low")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) : "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) struct usb_function *f = config->interface[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) if (!f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) DBG(cdev, " interface %d = %s/%p\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) i, f->name, f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) /* set_alt(), or next bind(), sets up ep->claimed as needed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) usb_ep_autoconfig_reset(cdev->gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) DBG(cdev, "added config '%s'/%u --> %d\n", config->label,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) config->bConfigurationValue, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) EXPORT_SYMBOL_GPL(usb_add_config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) static void remove_config(struct usb_composite_dev *cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) struct usb_configuration *config)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) while (!list_empty(&config->functions)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) struct usb_function *f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) f = list_first_entry(&config->functions,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) struct usb_function, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) usb_remove_function(config, f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) list_del(&config->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) if (config->unbind) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) DBG(cdev, "unbind config '%s'/%p\n", config->label, config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) config->unbind(config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) /* may free memory for "c" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) * usb_remove_config() - remove a configuration from a device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) * @cdev: wraps the USB gadget
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) * @config: the configuration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) * Drivers must call usb_gadget_disconnect before calling this function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) * to disconnect the device from the host and make sure the host will not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) * try to enumerate the device while we are changing the config list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) void usb_remove_config(struct usb_composite_dev *cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) struct usb_configuration *config)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) spin_lock_irqsave(&cdev->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) if (cdev->config == config)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) reset_config(cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) spin_unlock_irqrestore(&cdev->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) remove_config(cdev, config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) /*-------------------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) /* We support strings in multiple languages ... string descriptor zero
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) * says which languages are supported. The typical case will be that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) * only one language (probably English) is used, with i18n handled on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) * the host side.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) static void collect_langs(struct usb_gadget_strings **sp, __le16 *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) const struct usb_gadget_strings *s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) __le16 language;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) __le16 *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) while (*sp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) s = *sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) language = cpu_to_le16(s->language);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) for (tmp = buf; *tmp && tmp < &buf[USB_MAX_STRING_LEN]; tmp++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) if (*tmp == language)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) goto repeat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) *tmp++ = language;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) repeat:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) sp++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) static int lookup_string(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) struct usb_gadget_strings **sp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) void *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) u16 language,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) int id
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) struct usb_gadget_strings *s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) int value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) while (*sp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) s = *sp++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) if (s->language != language)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) value = usb_gadget_get_string(s, id, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) if (value > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) return value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) static int get_string(struct usb_composite_dev *cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) void *buf, u16 language, int id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) struct usb_composite_driver *composite = cdev->driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) struct usb_gadget_string_container *uc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) struct usb_configuration *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) struct usb_function *f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) /* Yes, not only is USB's i18n support probably more than most
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) * folk will ever care about ... also, it's all supported here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) * (Except for UTF8 support for Unicode's "Astral Planes".)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) /* 0 == report all available language codes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) if (id == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) struct usb_string_descriptor *s = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) struct usb_gadget_strings **sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) memset(s, 0, 256);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) s->bDescriptorType = USB_DT_STRING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) sp = composite->strings;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) if (sp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) collect_langs(sp, s->wData);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) list_for_each_entry(c, &cdev->configs, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) sp = c->strings;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) if (sp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) collect_langs(sp, s->wData);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) list_for_each_entry(f, &c->functions, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) sp = f->strings;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) if (sp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) collect_langs(sp, s->wData);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) list_for_each_entry(uc, &cdev->gstrings, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) struct usb_gadget_strings **sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) sp = get_containers_gs(uc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) collect_langs(sp, s->wData);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) for (len = 0; len <= USB_MAX_STRING_LEN && s->wData[len]; len++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) if (!len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) s->bLength = 2 * (len + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) return s->bLength;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) if (cdev->use_os_string && language == 0 && id == OS_STRING_IDX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) struct usb_os_string *b = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) b->bLength = sizeof(*b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) b->bDescriptorType = USB_DT_STRING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) compiletime_assert(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) sizeof(b->qwSignature) == sizeof(cdev->qw_sign),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) "qwSignature size must be equal to qw_sign");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) memcpy(&b->qwSignature, cdev->qw_sign, sizeof(b->qwSignature));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) b->bMS_VendorCode = cdev->b_vendor_code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) b->bPad = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) return sizeof(*b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) list_for_each_entry(uc, &cdev->gstrings, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) struct usb_gadget_strings **sp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) sp = get_containers_gs(uc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) len = lookup_string(sp, buf, language, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) if (len > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) /* String IDs are device-scoped, so we look up each string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) * table we're told about. These lookups are infrequent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) * simpler-is-better here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) if (composite->strings) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) len = lookup_string(composite->strings, buf, language, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) if (len > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) list_for_each_entry(c, &cdev->configs, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) if (c->strings) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) len = lookup_string(c->strings, buf, language, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) if (len > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) list_for_each_entry(f, &c->functions, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) if (!f->strings)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) len = lookup_string(f->strings, buf, language, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) if (len > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) * usb_string_id() - allocate an unused string ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) * @cdev: the device whose string descriptor IDs are being allocated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) * Context: single threaded during gadget setup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) * @usb_string_id() is called from bind() callbacks to allocate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) * string IDs. Drivers for functions, configurations, or gadgets will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) * then store that ID in the appropriate descriptors and string table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) * All string identifier should be allocated using this,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) * @usb_string_ids_tab() or @usb_string_ids_n() routine, to ensure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) * that for example different functions don't wrongly assign different
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) * meanings to the same identifier.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) int usb_string_id(struct usb_composite_dev *cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) if (cdev->next_string_id < 254) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) /* string id 0 is reserved by USB spec for list of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) * supported languages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) /* 255 reserved as well? -- mina86 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) cdev->next_string_id++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) return cdev->next_string_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) EXPORT_SYMBOL_GPL(usb_string_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) * usb_string_ids_tab() - allocate unused string IDs in batch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) * @cdev: the device whose string descriptor IDs are being allocated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) * @str: an array of usb_string objects to assign numbers to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) * Context: single threaded during gadget setup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) * @usb_string_ids() is called from bind() callbacks to allocate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) * string IDs. Drivers for functions, configurations, or gadgets will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) * then copy IDs from the string table to the appropriate descriptors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) * and string table for other languages.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) * All string identifier should be allocated using this,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) * example different functions don't wrongly assign different meanings
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) * to the same identifier.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) int usb_string_ids_tab(struct usb_composite_dev *cdev, struct usb_string *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) int next = cdev->next_string_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) for (; str->s; ++str) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) if (unlikely(next >= 254))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) str->id = ++next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) cdev->next_string_id = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) EXPORT_SYMBOL_GPL(usb_string_ids_tab);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) static struct usb_gadget_string_container *copy_gadget_strings(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) struct usb_gadget_strings **sp, unsigned n_gstrings,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) unsigned n_strings)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) struct usb_gadget_string_container *uc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) struct usb_gadget_strings **gs_array;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) struct usb_gadget_strings *gs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) struct usb_string *s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) unsigned mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) unsigned n_gs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) unsigned n_s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) void *stash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) mem = sizeof(*uc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) mem += sizeof(void *) * (n_gstrings + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) mem += sizeof(struct usb_gadget_strings) * n_gstrings;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) mem += sizeof(struct usb_string) * (n_strings + 1) * (n_gstrings);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) uc = kmalloc(mem, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) if (!uc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) gs_array = get_containers_gs(uc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) stash = uc->stash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) stash += sizeof(void *) * (n_gstrings + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) for (n_gs = 0; n_gs < n_gstrings; n_gs++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) struct usb_string *org_s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) gs_array[n_gs] = stash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) gs = gs_array[n_gs];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) stash += sizeof(struct usb_gadget_strings);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) gs->language = sp[n_gs]->language;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) gs->strings = stash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) org_s = sp[n_gs]->strings;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) for (n_s = 0; n_s < n_strings; n_s++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) s = stash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) stash += sizeof(struct usb_string);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) if (org_s->s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) s->s = org_s->s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) s->s = "";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) org_s++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) s = stash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) s->s = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) stash += sizeof(struct usb_string);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) gs_array[n_gs] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) return uc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) * usb_gstrings_attach() - attach gadget strings to a cdev and assign ids
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) * @cdev: the device whose string descriptor IDs are being allocated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) * and attached.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) * @sp: an array of usb_gadget_strings to attach.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) * @n_strings: number of entries in each usb_strings array (sp[]->strings)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) * This function will create a deep copy of usb_gadget_strings and usb_string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) * and attach it to the cdev. The actual string (usb_string.s) will not be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) * copied but only a referenced will be made. The struct usb_gadget_strings
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) * array may contain multiple languages and should be NULL terminated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) * The ->language pointer of each struct usb_gadget_strings has to contain the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) * same amount of entries.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) * For instance: sp[0] is en-US, sp[1] is es-ES. It is expected that the first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) * usb_string entry of es-ES contains the translation of the first usb_string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) * entry of en-US. Therefore both entries become the same id assign.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) struct usb_string *usb_gstrings_attach(struct usb_composite_dev *cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) struct usb_gadget_strings **sp, unsigned n_strings)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) struct usb_gadget_string_container *uc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) struct usb_gadget_strings **n_gs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) unsigned n_gstrings = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) unsigned i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) for (i = 0; sp[i]; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) n_gstrings++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) if (!n_gstrings)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) uc = copy_gadget_strings(sp, n_gstrings, n_strings);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) if (IS_ERR(uc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) return ERR_CAST(uc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) n_gs = get_containers_gs(uc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) ret = usb_string_ids_tab(cdev, n_gs[0]->strings);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) for (i = 1; i < n_gstrings; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) struct usb_string *m_s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) struct usb_string *s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) unsigned n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) m_s = n_gs[0]->strings;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) s = n_gs[i]->strings;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) for (n = 0; n < n_strings; n++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) s->id = m_s->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) s++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) m_s++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) list_add_tail(&uc->list, &cdev->gstrings);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) return n_gs[0]->strings;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) kfree(uc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) EXPORT_SYMBOL_GPL(usb_gstrings_attach);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) * usb_string_ids_n() - allocate unused string IDs in batch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) * @c: the device whose string descriptor IDs are being allocated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) * @n: number of string IDs to allocate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) * Context: single threaded during gadget setup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) * Returns the first requested ID. This ID and next @n-1 IDs are now
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) * valid IDs. At least provided that @n is non-zero because if it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) * is, returns last requested ID which is now very useful information.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) * @usb_string_ids_n() is called from bind() callbacks to allocate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) * string IDs. Drivers for functions, configurations, or gadgets will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) * then store that ID in the appropriate descriptors and string table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) * All string identifier should be allocated using this,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) * @usb_string_id() or @usb_string_ids_n() routine, to ensure that for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) * example different functions don't wrongly assign different meanings
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) * to the same identifier.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) int usb_string_ids_n(struct usb_composite_dev *c, unsigned n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) unsigned next = c->next_string_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) if (unlikely(n > 254 || (unsigned)next + n > 254))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) c->next_string_id += n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) return next + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) EXPORT_SYMBOL_GPL(usb_string_ids_n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) /*-------------------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) static void composite_setup_complete(struct usb_ep *ep, struct usb_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) struct usb_composite_dev *cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) if (req->status || req->actual != req->length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) DBG((struct usb_composite_dev *) ep->driver_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) "setup complete --> %d, %d/%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) req->status, req->actual, req->length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) * REVIST The same ep0 requests are shared with function drivers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) * so they don't have to maintain the same ->complete() stubs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) * Because of that, we need to check for the validity of ->context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) * here, even though we know we've set it to something useful.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) if (!req->context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) cdev = req->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) if (cdev->req == req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) cdev->setup_pending = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) else if (cdev->os_desc_req == req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) cdev->os_desc_pending = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) WARN(1, "unknown request %p\n", req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) static int composite_ep0_queue(struct usb_composite_dev *cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) struct usb_request *req, gfp_t gfp_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) ret = usb_ep_queue(cdev->gadget->ep0, req, gfp_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) if (ret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) if (cdev->req == req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) cdev->setup_pending = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) else if (cdev->os_desc_req == req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) cdev->os_desc_pending = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) WARN(1, "unknown request %p\n", req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) static int count_ext_compat(struct usb_configuration *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) int i, res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) res = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) for (i = 0; i < c->next_interface_id; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) struct usb_function *f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) int j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) f = c->interface[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) for (j = 0; j < f->os_desc_n; ++j) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) struct usb_os_desc *d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) if (i != f->os_desc_table[j].if_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) d = f->os_desc_table[j].os_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) if (d && d->ext_compat_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) ++res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) BUG_ON(res > 255);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) static int fill_ext_compat(struct usb_configuration *c, u8 *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) int i, count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) count = 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) buf += 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) for (i = 0; i < c->next_interface_id; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) struct usb_function *f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) int j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) f = c->interface[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) for (j = 0; j < f->os_desc_n; ++j) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) struct usb_os_desc *d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) if (i != f->os_desc_table[j].if_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) d = f->os_desc_table[j].os_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) if (d && d->ext_compat_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) *buf++ = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) *buf++ = 0x01;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) memcpy(buf, d->ext_compat_id, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) buf += 22;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) ++buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) *buf = 0x01;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) buf += 23;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) count += 24;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) if (count + 24 >= USB_COMP_EP0_OS_DESC_BUFSIZ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) static int count_ext_prop(struct usb_configuration *c, int interface)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) struct usb_function *f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) int j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) f = c->interface[interface];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) for (j = 0; j < f->os_desc_n; ++j) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) struct usb_os_desc *d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) if (interface != f->os_desc_table[j].if_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) d = f->os_desc_table[j].os_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) if (d && d->ext_compat_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) return d->ext_prop_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) static int len_ext_prop(struct usb_configuration *c, int interface)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) struct usb_function *f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) struct usb_os_desc *d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) int j, res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) res = 10; /* header length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) f = c->interface[interface];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) for (j = 0; j < f->os_desc_n; ++j) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) if (interface != f->os_desc_table[j].if_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) d = f->os_desc_table[j].os_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) if (d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) return min(res + d->ext_prop_len, 4096);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) static int fill_ext_prop(struct usb_configuration *c, int interface, u8 *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) struct usb_function *f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) struct usb_os_desc *d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) struct usb_os_desc_ext_prop *ext_prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) int j, count, n, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) f = c->interface[interface];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) count = 10; /* header length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) buf += 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) for (j = 0; j < f->os_desc_n; ++j) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) if (interface != f->os_desc_table[j].if_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) d = f->os_desc_table[j].os_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) if (d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) list_for_each_entry(ext_prop, &d->ext_prop, entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) n = ext_prop->data_len +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) ext_prop->name_len + 14;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) if (count + n >= USB_COMP_EP0_OS_DESC_BUFSIZ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) usb_ext_prop_put_size(buf, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) usb_ext_prop_put_type(buf, ext_prop->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) ret = usb_ext_prop_put_name(buf, ext_prop->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) ext_prop->name_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) switch (ext_prop->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) case USB_EXT_PROP_UNICODE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) case USB_EXT_PROP_UNICODE_ENV:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) case USB_EXT_PROP_UNICODE_LINK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) usb_ext_prop_put_unicode(buf, ret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) ext_prop->data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) ext_prop->data_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) case USB_EXT_PROP_BINARY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) usb_ext_prop_put_binary(buf, ret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) ext_prop->data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) ext_prop->data_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) case USB_EXT_PROP_LE32:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) /* not implemented */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) case USB_EXT_PROP_BE32:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) /* not implemented */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) buf += n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) count += n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) * The setup() callback implements all the ep0 functionality that's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) * not handled lower down, in hardware or the hardware driver(like
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) * device and endpoint feature flags, and their status). It's all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) * housekeeping for the gadget function we're implementing. Most of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) * the work is in config and function specific setup.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) struct usb_composite_dev *cdev = get_gadget_data(gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) struct usb_request *req = cdev->req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) int value = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) int status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) u16 w_index = le16_to_cpu(ctrl->wIndex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) u8 intf = w_index & 0xFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) u16 w_value = le16_to_cpu(ctrl->wValue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) u16 w_length = le16_to_cpu(ctrl->wLength);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) struct usb_function *f = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) u8 endp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) if (w_length > USB_COMP_EP0_BUFSIZ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) if (ctrl->bRequestType & USB_DIR_IN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) /* Cast away the const, we are going to overwrite on purpose. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) __le16 *temp = (__le16 *)&ctrl->wLength;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) *temp = cpu_to_le16(USB_COMP_EP0_BUFSIZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) w_length = USB_COMP_EP0_BUFSIZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) /* partial re-init of the response message; the function or the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) * gadget might need to intercept e.g. a control-OUT completion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) * when we delegate to it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) req->zero = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) req->context = cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) req->complete = composite_setup_complete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) req->length = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) gadget->ep0->driver_data = cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) * Don't let non-standard requests match any of the cases below
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) * by accident.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) if ((ctrl->bRequestType & USB_TYPE_MASK) != USB_TYPE_STANDARD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) goto unknown;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) switch (ctrl->bRequest) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) /* we handle all standard USB descriptors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) case USB_REQ_GET_DESCRIPTOR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) if (ctrl->bRequestType != USB_DIR_IN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) goto unknown;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) switch (w_value >> 8) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) case USB_DT_DEVICE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) cdev->desc.bNumConfigurations =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) count_configs(cdev, USB_DT_DEVICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) cdev->desc.bMaxPacketSize0 =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) cdev->gadget->ep0->maxpacket;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) if (gadget_is_superspeed(gadget)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) if (gadget->speed >= USB_SPEED_SUPER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) cdev->desc.bcdUSB = cpu_to_le16(0x0320);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) cdev->desc.bMaxPacketSize0 = 9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) cdev->desc.bcdUSB = cpu_to_le16(0x0210);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) if (gadget->lpm_capable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) cdev->desc.bcdUSB = cpu_to_le16(0x0201);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) cdev->desc.bcdUSB = cpu_to_le16(0x0200);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) value = min(w_length, (u16) sizeof cdev->desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) memcpy(req->buf, &cdev->desc, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) case USB_DT_DEVICE_QUALIFIER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) if (!gadget_is_dualspeed(gadget) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) gadget->speed >= USB_SPEED_SUPER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) device_qual(cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) value = min_t(int, w_length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) sizeof(struct usb_qualifier_descriptor));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) case USB_DT_OTHER_SPEED_CONFIG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) if (!gadget_is_dualspeed(gadget) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) gadget->speed >= USB_SPEED_SUPER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) case USB_DT_CONFIG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) value = config_desc(cdev, w_value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) if (value >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) value = min(w_length, (u16) value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) case USB_DT_STRING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) value = get_string(cdev, req->buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) w_index, w_value & 0xff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) if (value >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) value = min(w_length, (u16) value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) case USB_DT_BOS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) if (gadget_is_superspeed(gadget) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) gadget->lpm_capable) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) value = bos_desc(cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) value = min(w_length, (u16) value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) case USB_DT_OTG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) if (gadget_is_otg(gadget)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) struct usb_configuration *config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) int otg_desc_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) if (cdev->config)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) config = cdev->config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) config = list_first_entry(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) &cdev->configs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) struct usb_configuration, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) if (!config)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) if (gadget->otg_caps &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) (gadget->otg_caps->otg_rev >= 0x0200))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) otg_desc_len += sizeof(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) struct usb_otg20_descriptor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) otg_desc_len += sizeof(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) struct usb_otg_descriptor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) value = min_t(int, w_length, otg_desc_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) memcpy(req->buf, config->descriptors[0], value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) /* any number of configs can work */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) case USB_REQ_SET_CONFIGURATION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) if (ctrl->bRequestType != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) goto unknown;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) if (gadget_is_otg(gadget)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) if (gadget->a_hnp_support)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) DBG(cdev, "HNP available\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) else if (gadget->a_alt_hnp_support)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) DBG(cdev, "HNP on another port\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) VDBG(cdev, "HNP inactive\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) spin_lock(&cdev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) value = set_config(cdev, ctrl, w_value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) spin_unlock(&cdev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) case USB_REQ_GET_CONFIGURATION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) if (ctrl->bRequestType != USB_DIR_IN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) goto unknown;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) if (cdev->config)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) *(u8 *)req->buf = cdev->config->bConfigurationValue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) *(u8 *)req->buf = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) value = min(w_length, (u16) 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) /* function drivers must handle get/set altsetting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) case USB_REQ_SET_INTERFACE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) if (ctrl->bRequestType != USB_RECIP_INTERFACE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) goto unknown;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) f = cdev->config->interface[intf];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) if (!f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) * If there's no get_alt() method, we know only altsetting zero
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) * works. There is no need to check if set_alt() is not NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) * as we check this in usb_add_function().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) if (w_value && !f->get_alt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) spin_lock(&cdev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) value = f->set_alt(f, w_index, w_value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) if (value == USB_GADGET_DELAYED_STATUS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) DBG(cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) "%s: interface %d (%s) requested delayed status\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) __func__, intf, f->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) cdev->delayed_status++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) DBG(cdev, "delayed_status count %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) cdev->delayed_status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) spin_unlock(&cdev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) case USB_REQ_GET_INTERFACE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) goto unknown;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) f = cdev->config->interface[intf];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) if (!f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) /* lots of interfaces only need altsetting zero... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) value = f->get_alt ? f->get_alt(f, w_index) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) if (value < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) *((u8 *)req->buf) = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) value = min(w_length, (u16) 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) case USB_REQ_GET_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) if (gadget_is_otg(gadget) && gadget->hnp_polling_support &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) (w_index == OTG_STS_SELECTOR)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) if (ctrl->bRequestType != (USB_DIR_IN |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) USB_RECIP_DEVICE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) goto unknown;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) *((u8 *)req->buf) = gadget->host_request_flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) value = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) * USB 3.0 additions:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) * Function driver should handle get_status request. If such cb
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) * wasn't supplied we respond with default value = 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) * Note: function driver should supply such cb only for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) * first interface of the function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) if (!gadget_is_superspeed(gadget))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) goto unknown;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) if (ctrl->bRequestType != (USB_DIR_IN | USB_RECIP_INTERFACE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) goto unknown;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) value = 2; /* This is the length of the get_status reply */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) put_unaligned_le16(0, req->buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) f = cdev->config->interface[intf];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) if (!f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) status = f->get_status ? f->get_status(f) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) put_unaligned_le16(status & 0x0000ffff, req->buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) * Function drivers should handle SetFeature/ClearFeature
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) * (FUNCTION_SUSPEND) request. function_suspend cb should be supplied
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) * only for the first interface of the function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) case USB_REQ_CLEAR_FEATURE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) case USB_REQ_SET_FEATURE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) if (!gadget_is_superspeed(gadget))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) goto unknown;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) if (ctrl->bRequestType != (USB_DIR_OUT | USB_RECIP_INTERFACE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) goto unknown;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) switch (w_value) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) case USB_INTRF_FUNC_SUSPEND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) f = cdev->config->interface[intf];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) if (!f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) value = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) if (f->func_suspend)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) value = f->func_suspend(f, w_index >> 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) if (value < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) ERROR(cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) "func_suspend() returned error %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) value = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) unknown:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) * OS descriptors handling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) if (cdev->use_os_string && cdev->os_desc_config &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) (ctrl->bRequestType & USB_TYPE_VENDOR) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) ctrl->bRequest == cdev->b_vendor_code) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) struct usb_configuration *os_desc_cfg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) u8 *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) int interface;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) int count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) req = cdev->os_desc_req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) req->context = cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) req->complete = composite_setup_complete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) buf = req->buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) os_desc_cfg = cdev->os_desc_config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) w_length = min_t(u16, w_length, USB_COMP_EP0_OS_DESC_BUFSIZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) memset(buf, 0, w_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) buf[5] = 0x01;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) switch (ctrl->bRequestType & USB_RECIP_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) case USB_RECIP_DEVICE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) if (w_index != 0x4 || (w_value >> 8))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) buf[6] = w_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) /* Number of ext compat interfaces */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) count = count_ext_compat(os_desc_cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) buf[8] = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) count *= 24; /* 24 B/ext compat desc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) count += 16; /* header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) put_unaligned_le32(count, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) value = w_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) if (w_length > 0x10) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) value = fill_ext_compat(os_desc_cfg, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) value = min_t(u16, w_length, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) case USB_RECIP_INTERFACE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) if (w_index != 0x5 || (w_value >> 8))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) interface = w_value & 0xFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) if (interface >= MAX_CONFIG_INTERFACES ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) !os_desc_cfg->interface[interface])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) buf[6] = w_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) count = count_ext_prop(os_desc_cfg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) interface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) put_unaligned_le16(count, buf + 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) count = len_ext_prop(os_desc_cfg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) interface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) put_unaligned_le32(count, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) value = w_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) if (w_length > 0x0A) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) value = fill_ext_prop(os_desc_cfg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) interface, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) if (value >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) value = min_t(u16, w_length, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) goto check_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) VDBG(cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) "non-core control req%02x.%02x v%04x i%04x l%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) ctrl->bRequestType, ctrl->bRequest,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) w_value, w_index, w_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) /* functions always handle their interfaces and endpoints...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) * punt other recipients (other, WUSB, ...) to the current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) * configuration code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) if (cdev->config) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) list_for_each_entry(f, &cdev->config->functions, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) if (f->req_match &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) f->req_match(f, ctrl, false))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) goto try_fun_setup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) struct usb_configuration *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) list_for_each_entry(c, &cdev->configs, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) list_for_each_entry(f, &c->functions, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) if (f->req_match &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) f->req_match(f, ctrl, true))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) goto try_fun_setup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) f = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) switch (ctrl->bRequestType & USB_RECIP_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) case USB_RECIP_INTERFACE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) if (!cdev->config || intf >= MAX_CONFIG_INTERFACES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) f = cdev->config->interface[intf];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) case USB_RECIP_ENDPOINT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) if (!cdev->config)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) list_for_each_entry(f, &cdev->config->functions, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) if (test_bit(endp, f->endpoints))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) if (&f->list == &cdev->config->functions)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) f = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) try_fun_setup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) if (f && f->setup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) value = f->setup(f, ctrl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) struct usb_configuration *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) c = cdev->config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) if (!c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) /* try current config's setup */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) if (c->setup) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) value = c->setup(c, ctrl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) /* try the only function in the current config */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) if (!list_is_singular(&c->functions))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) f = list_first_entry(&c->functions, struct usb_function,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) if (f->setup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) value = f->setup(f, ctrl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) check_value:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) /* respond with data transfer before status phase? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) if (value >= 0 && value != USB_GADGET_DELAYED_STATUS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) req->length = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) req->context = cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) req->zero = value < w_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) value = composite_ep0_queue(cdev, req, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) if (value < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) DBG(cdev, "ep_queue --> %d\n", value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) req->status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) composite_setup_complete(gadget->ep0, req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) } else if (value == USB_GADGET_DELAYED_STATUS && w_length != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) WARN(cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) "%s: Delayed status not supported for w_length != 0",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) /* device either stalls (value < 0) or reports success */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) return value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) static void __composite_disconnect(struct usb_gadget *gadget)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) struct usb_composite_dev *cdev = get_gadget_data(gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) /* REVISIT: should we have config and device level
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) * disconnect callbacks?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) spin_lock_irqsave(&cdev->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) cdev->suspended = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) if (cdev->config)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) reset_config(cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) if (cdev->driver->disconnect)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) cdev->driver->disconnect(cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) spin_unlock_irqrestore(&cdev->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) void composite_disconnect(struct usb_gadget *gadget)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) usb_gadget_vbus_draw(gadget, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) __composite_disconnect(gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) void composite_reset(struct usb_gadget *gadget)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) * Section 1.4.13 Standard Downstream Port of the USB battery charging
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) * specification v1.2 states that a device connected on a SDP shall only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) * draw at max 100mA while in a connected, but unconfigured state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) usb_gadget_vbus_draw(gadget, 100);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) __composite_disconnect(gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) /*-------------------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) static ssize_t suspended_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) struct usb_gadget *gadget = dev_to_usb_gadget(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) struct usb_composite_dev *cdev = get_gadget_data(gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150) return sprintf(buf, "%d\n", cdev->suspended);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) static DEVICE_ATTR_RO(suspended);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) static void __composite_unbind(struct usb_gadget *gadget, bool unbind_driver)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) struct usb_composite_dev *cdev = get_gadget_data(gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) struct usb_gadget_strings *gstr = cdev->driver->strings[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) struct usb_string *dev_str = gstr->strings;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) /* composite_disconnect() must already have been called
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) * by the underlying peripheral controller driver!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) * so there's no i/o concurrency that could affect the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) * state protected by cdev->lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) WARN_ON(cdev->config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) while (!list_empty(&cdev->configs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) struct usb_configuration *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) c = list_first_entry(&cdev->configs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) struct usb_configuration, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) remove_config(cdev, c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) if (cdev->driver->unbind && unbind_driver)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) cdev->driver->unbind(cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) composite_dev_cleanup(cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) if (dev_str[USB_GADGET_MANUFACTURER_IDX].s == cdev->def_manufacturer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) dev_str[USB_GADGET_MANUFACTURER_IDX].s = "";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) kfree(cdev->def_manufacturer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) kfree(cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) set_gadget_data(gadget, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) static void composite_unbind(struct usb_gadget *gadget)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) __composite_unbind(gadget, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) static void update_unchanged_dev_desc(struct usb_device_descriptor *new,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) const struct usb_device_descriptor *old)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) __le16 idVendor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) __le16 idProduct;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) __le16 bcdDevice;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197) u8 iSerialNumber;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198) u8 iManufacturer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) u8 iProduct;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202) * these variables may have been set in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) * usb_composite_overwrite_options()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205) idVendor = new->idVendor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) idProduct = new->idProduct;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207) bcdDevice = new->bcdDevice;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) iSerialNumber = new->iSerialNumber;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209) iManufacturer = new->iManufacturer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) iProduct = new->iProduct;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212) *new = *old;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213) if (idVendor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214) new->idVendor = idVendor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215) if (idProduct)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) new->idProduct = idProduct;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217) if (bcdDevice)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) new->bcdDevice = bcdDevice;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) new->bcdDevice = cpu_to_le16(get_default_bcdDevice());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221) if (iSerialNumber)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222) new->iSerialNumber = iSerialNumber;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223) if (iManufacturer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224) new->iManufacturer = iManufacturer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225) if (iProduct)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226) new->iProduct = iProduct;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229) int composite_dev_prepare(struct usb_composite_driver *composite,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230) struct usb_composite_dev *cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232) struct usb_gadget *gadget = cdev->gadget;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233) int ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235) /* preallocate control response and buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236) cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237) if (!cdev->req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240) cdev->req->buf = kzalloc(USB_COMP_EP0_BUFSIZ, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241) if (!cdev->req->buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) ret = device_create_file(&gadget->dev, &dev_attr_suspended);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246) goto fail_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) cdev->req->complete = composite_setup_complete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) cdev->req->context = cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250) gadget->ep0->driver_data = cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) cdev->driver = composite;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) * As per USB compliance update, a device that is actively drawing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) * more than 100mA from USB must report itself as bus-powered in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) * the GetStatus(DEVICE) call.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259) if (CONFIG_USB_GADGET_VBUS_DRAW <= USB_SELF_POWER_VBUS_MAX_DRAW)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260) usb_gadget_set_selfpowered(gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262) /* interface and string IDs start at zero via kzalloc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263) * we force endpoints to start unassigned; few controller
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) * drivers will zero ep->driver_data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266) usb_ep_autoconfig_reset(gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268) fail_dev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269) kfree(cdev->req->buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271) usb_ep_free_request(gadget->ep0, cdev->req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) cdev->req = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276) int composite_os_desc_req_prepare(struct usb_composite_dev *cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) struct usb_ep *ep0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281) cdev->os_desc_req = usb_ep_alloc_request(ep0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282) if (!cdev->os_desc_req) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284) goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287) cdev->os_desc_req->buf = kmalloc(USB_COMP_EP0_OS_DESC_BUFSIZ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289) if (!cdev->os_desc_req->buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291) usb_ep_free_request(ep0, cdev->os_desc_req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292) goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294) cdev->os_desc_req->context = cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295) cdev->os_desc_req->complete = composite_setup_complete;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296) end:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300) void composite_dev_cleanup(struct usb_composite_dev *cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302) struct usb_gadget_string_container *uc, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303) struct usb_ep *ep, *tmp_ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305) list_for_each_entry_safe(uc, tmp, &cdev->gstrings, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306) list_del(&uc->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307) kfree(uc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309) if (cdev->os_desc_req) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310) if (cdev->os_desc_pending)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311) usb_ep_dequeue(cdev->gadget->ep0, cdev->os_desc_req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313) kfree(cdev->os_desc_req->buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314) cdev->os_desc_req->buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315) usb_ep_free_request(cdev->gadget->ep0, cdev->os_desc_req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316) cdev->os_desc_req = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318) if (cdev->req) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319) if (cdev->setup_pending)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320) usb_ep_dequeue(cdev->gadget->ep0, cdev->req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322) kfree(cdev->req->buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323) cdev->req->buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324) usb_ep_free_request(cdev->gadget->ep0, cdev->req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325) cdev->req = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327) cdev->next_string_id = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) device_remove_file(&cdev->gadget->dev, &dev_attr_suspended);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331) * Some UDC backends have a dynamic EP allocation scheme.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333) * In that case, the dispose() callback is used to notify the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334) * backend that the EPs are no longer in use.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336) * Note: The UDC backend can remove the EP from the ep_list as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337) * a result, so we need to use the _safe list iterator.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339) list_for_each_entry_safe(ep, tmp_ep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340) &cdev->gadget->ep_list, ep_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341) if (ep->ops->dispose)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342) ep->ops->dispose(ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346) static int composite_bind(struct usb_gadget *gadget,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347) struct usb_gadget_driver *gdriver)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349) struct usb_composite_dev *cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350) struct usb_composite_driver *composite = to_cdriver(gdriver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351) int status = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353) cdev = kzalloc(sizeof *cdev, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354) if (!cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357) spin_lock_init(&cdev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358) cdev->gadget = gadget;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359) set_gadget_data(gadget, cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360) INIT_LIST_HEAD(&cdev->configs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361) INIT_LIST_HEAD(&cdev->gstrings);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363) status = composite_dev_prepare(composite, cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367) /* composite gadget needs to assign strings for whole device (like
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368) * serial number), register function drivers, potentially update
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369) * power state and consumption, etc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371) status = composite->bind(cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372) if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375) if (cdev->use_os_string) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376) status = composite_os_desc_req_prepare(cdev, gadget->ep0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2377) if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2378) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2379) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2381) update_unchanged_dev_desc(&cdev->desc, composite->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2383) /* has userspace failed to provide a serial number? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2384) if (composite->needs_serial && !cdev->desc.iSerialNumber)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2385) WARNING(cdev, "userspace failed to provide iSerialNumber\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2387) INFO(cdev, "%s ready\n", composite->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2388) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2390) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2391) __composite_unbind(gadget, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2392) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2393) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2395) /*-------------------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2397) void composite_suspend(struct usb_gadget *gadget)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2398) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2399) struct usb_composite_dev *cdev = get_gadget_data(gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2400) struct usb_function *f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2402) /* REVISIT: should we have config level
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2403) * suspend/resume callbacks?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2404) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2405) DBG(cdev, "suspend\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2406) if (cdev->config) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2407) list_for_each_entry(f, &cdev->config->functions, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2408) if (f->suspend)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2409) f->suspend(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2410) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2411) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2412) if (cdev->driver->suspend)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2413) cdev->driver->suspend(cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2415) cdev->suspended = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2416)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2417) usb_gadget_set_selfpowered(gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2418) usb_gadget_vbus_draw(gadget, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2419) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2421) void composite_resume(struct usb_gadget *gadget)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2422) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2423) struct usb_composite_dev *cdev = get_gadget_data(gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2424) struct usb_function *f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2425) unsigned maxpower;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2427) /* REVISIT: should we have config level
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2428) * suspend/resume callbacks?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2429) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2430) DBG(cdev, "resume\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2431) if (cdev->driver->resume)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2432) cdev->driver->resume(cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2433) if (cdev->config) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2434) list_for_each_entry(f, &cdev->config->functions, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2435) if (f->resume)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2436) f->resume(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2437) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2439) maxpower = cdev->config->MaxPower ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2440) cdev->config->MaxPower : CONFIG_USB_GADGET_VBUS_DRAW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2441) if (gadget->speed < USB_SPEED_SUPER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2442) maxpower = min(maxpower, 500U);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2443) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2444) maxpower = min(maxpower, 900U);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2446) if (maxpower > USB_SELF_POWER_VBUS_MAX_DRAW)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2447) usb_gadget_clear_selfpowered(gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2449) usb_gadget_vbus_draw(gadget, maxpower);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2450) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2452) cdev->suspended = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2453) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2455) /*-------------------------------------------------------------------------*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2457) static const struct usb_gadget_driver composite_driver_template = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2458) .bind = composite_bind,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2459) .unbind = composite_unbind,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2461) .setup = composite_setup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2462) .reset = composite_reset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2463) .disconnect = composite_disconnect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2464)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2465) .suspend = composite_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2466) .resume = composite_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2468) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2469) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2470) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2471) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2472)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2473) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2474) * usb_composite_probe() - register a composite driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2475) * @driver: the driver to register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2476) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2477) * Context: single threaded during gadget setup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2478) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2479) * This function is used to register drivers using the composite driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2480) * framework. The return value is zero, or a negative errno value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2481) * Those values normally come from the driver's @bind method, which does
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2482) * all the work of setting up the driver to match the hardware.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2483) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2484) * On successful return, the gadget is ready to respond to requests from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2485) * the host, unless one of its components invokes usb_gadget_disconnect()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2486) * while it was binding. That would usually be done in order to wait for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2487) * some userspace participation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2488) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2489) int usb_composite_probe(struct usb_composite_driver *driver)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2490) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2491) struct usb_gadget_driver *gadget_driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2492)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2493) if (!driver || !driver->dev || !driver->bind)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2494) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2495)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2496) if (!driver->name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2497) driver->name = "composite";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2499) driver->gadget_driver = composite_driver_template;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2500) gadget_driver = &driver->gadget_driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2501)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2502) gadget_driver->function = (char *) driver->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2503) gadget_driver->driver.name = driver->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2504) gadget_driver->max_speed = driver->max_speed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2506) return usb_gadget_probe_driver(gadget_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2507) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2508) EXPORT_SYMBOL_GPL(usb_composite_probe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2509)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2510) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2511) * usb_composite_unregister() - unregister a composite driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2512) * @driver: the driver to unregister
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2513) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2514) * This function is used to unregister drivers using the composite
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2515) * driver framework.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2516) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2517) void usb_composite_unregister(struct usb_composite_driver *driver)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2518) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2519) usb_gadget_unregister_driver(&driver->gadget_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2520) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2521) EXPORT_SYMBOL_GPL(usb_composite_unregister);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2523) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2524) * usb_composite_setup_continue() - Continue with the control transfer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2525) * @cdev: the composite device who's control transfer was kept waiting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2526) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2527) * This function must be called by the USB function driver to continue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2528) * with the control transfer's data/status stage in case it had requested to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2529) * delay the data/status stages. A USB function's setup handler (e.g. set_alt())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2530) * can request the composite framework to delay the setup request's data/status
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2531) * stages by returning USB_GADGET_DELAYED_STATUS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2532) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2533) void usb_composite_setup_continue(struct usb_composite_dev *cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2534) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2535) int value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2536) struct usb_request *req = cdev->req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2537) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2538)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2539) DBG(cdev, "%s\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2540) spin_lock_irqsave(&cdev->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2542) if (cdev->delayed_status == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2543) WARN(cdev, "%s: Unexpected call\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2544)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2545) } else if (--cdev->delayed_status == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2546) DBG(cdev, "%s: Completing delayed status\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2547) req->length = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2548) req->context = cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2549) value = composite_ep0_queue(cdev, req, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2550) if (value < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2551) DBG(cdev, "ep_queue --> %d\n", value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2552) req->status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2553) composite_setup_complete(cdev->gadget->ep0, req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2554) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2556)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2557) spin_unlock_irqrestore(&cdev->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2558) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2559) EXPORT_SYMBOL_GPL(usb_composite_setup_continue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2560)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2561) static char *composite_default_mfr(struct usb_gadget *gadget)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2562) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2563) return kasprintf(GFP_KERNEL, "%s %s with %s", init_utsname()->sysname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2564) init_utsname()->release, gadget->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2565) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2566)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2567) void usb_composite_overwrite_options(struct usb_composite_dev *cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2568) struct usb_composite_overwrite *covr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2569) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2570) struct usb_device_descriptor *desc = &cdev->desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2571) struct usb_gadget_strings *gstr = cdev->driver->strings[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2572) struct usb_string *dev_str = gstr->strings;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2573)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2574) if (covr->idVendor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2575) desc->idVendor = cpu_to_le16(covr->idVendor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2576)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2577) if (covr->idProduct)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2578) desc->idProduct = cpu_to_le16(covr->idProduct);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2579)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2580) if (covr->bcdDevice)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2581) desc->bcdDevice = cpu_to_le16(covr->bcdDevice);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2582)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2583) if (covr->serial_number) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2584) desc->iSerialNumber = dev_str[USB_GADGET_SERIAL_IDX].id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2585) dev_str[USB_GADGET_SERIAL_IDX].s = covr->serial_number;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2586) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2587) if (covr->manufacturer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2588) desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2589) dev_str[USB_GADGET_MANUFACTURER_IDX].s = covr->manufacturer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2590)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2591) } else if (!strlen(dev_str[USB_GADGET_MANUFACTURER_IDX].s)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2592) desc->iManufacturer = dev_str[USB_GADGET_MANUFACTURER_IDX].id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2593) cdev->def_manufacturer = composite_default_mfr(cdev->gadget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2594) dev_str[USB_GADGET_MANUFACTURER_IDX].s = cdev->def_manufacturer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2595) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2596)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2597) if (covr->product) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2598) desc->iProduct = dev_str[USB_GADGET_PRODUCT_IDX].id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2599) dev_str[USB_GADGET_PRODUCT_IDX].s = covr->product;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2600) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2601) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2602) EXPORT_SYMBOL_GPL(usb_composite_overwrite_options);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2603)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2604) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2605) MODULE_AUTHOR("David Brownell");