31 #include <linux/kallsyms.h>
32 #include <linux/kernel.h>
33 #include <linux/slab.h>
34 #include <linux/device.h>
36 #include <linux/usb/composite.h>
42 #define USB_BUFSIZ 1024
48 static USB_SPEED UsbSpeedDefault;
49 static USB_SPEED *
pUsbSpeed = &UsbSpeedDefault;
57 static struct usb_composite_driver *composite;
64 static ushort idVendor;
68 static ushort idProduct;
72 static ushort bcdDevice;
76 static char *iManufacturer;
80 static char *iProduct;
84 static char *iSerialNumber;
92 static char hs_hid_report_descriptor[] = {
116 static char fs_hid_report_descriptor[] = {
157 struct usb_function *
function)
161 DBG(config->cdev,
"adding '%s'/%p to config '%s'/%p\n",
162 function->name,
function,
163 config->label, config);
165 if (!function->set_alt || !function->disable)
168 function->config = config;
169 list_add_tail(&function->list, &config->functions);
172 if (function->bind) {
173 value =
function->bind(config,
function);
175 list_del(&function->list);
176 function->config =
NULL;
186 if (!config->fullspeed && function->descriptors)
187 config->fullspeed =
true;
188 if (!config->highspeed && function->hs_descriptors)
189 config->highspeed =
true;
193 DBG(config->cdev,
"adding '%s'/%p --> %d\n",
194 function->name,
function, value);
219 struct usb_composite_dev *cdev =
function->config->cdev;
223 spin_lock_irqsave(&cdev->lock, flags);
225 if (cdev->deactivations == 0)
226 status = usb_gadget_disconnect(cdev->gadget);
228 cdev->deactivations++;
230 spin_unlock_irqrestore(&cdev->lock, flags);
246 struct usb_composite_dev *cdev =
function->config->cdev;
249 spin_lock(&cdev->lock);
251 if (WARN_ON(cdev->deactivations == 0))
254 cdev->deactivations--;
255 if (cdev->deactivations == 0)
256 status = usb_gadget_connect(cdev->gadget);
259 spin_unlock(&cdev->lock);
288 struct usb_function *
function)
290 unsigned id = config->next_interface_id;
292 if (
id < MAX_CONFIG_INTERFACES) {
293 config->interface[id] =
function;
294 config->next_interface_id =
id + 1;
301 static int config_buf(
struct usb_configuration *config,
302 enum usb_device_speed speed,
void *buf,
u8 type)
304 struct usb_config_descriptor *c = buf;
305 void *next = buf + USB_DT_CONFIG_SIZE;
307 struct usb_function *f;
312 c->bLength = USB_DT_CONFIG_SIZE;
313 c->bDescriptorType = type;
315 c->bNumInterfaces = config->next_interface_id;
316 c->bConfigurationValue = config->bConfigurationValue;
317 c->iConfiguration = config->iConfiguration;
318 c->bmAttributes = USB_CONFIG_ATT_ONE | config->bmAttributes;
319 c->bMaxPower = config->bMaxPower ? : (CONFIG_USB_GADGET_VBUS_DRAW / 2);
322 if (config->descriptors) {
323 status = usb_descriptor_fillbuf(next, len,
324 config->descriptors);
333 list_for_each_entry(f, &config->functions, list) {
334 struct usb_descriptor_header **descriptors;
336 if (speed == USB_SPEED_HIGH)
338 descriptors = f->hs_descriptors;
342 descriptors = f->descriptors;
347 status = usb_descriptor_fillbuf(next, len,
348 (
const struct usb_descriptor_header **) descriptors);
356 c->wTotalLength = cpu_to_le16(len);
365 static int config_desc(
struct usb_composite_dev *cdev,
unsigned w_value)
367 struct usb_gadget *gadget = cdev->gadget;
368 struct usb_configuration *c;
369 u8 type = w_value >> 8;
370 enum usb_device_speed speed = USB_SPEED_UNKNOWN;
372 if (gadget_is_dualspeed(gadget)) {
383 if (gadget->speed == USB_SPEED_HIGH)
390 printk(
"Speed = HIGH\n\r");
400 printk(
"Speed = LOW\n\r");
404 if (type == USB_DT_OTHER_SPEED_CONFIG)
407 speed = USB_SPEED_HIGH;
412 list_for_each_entry(c, &cdev->configs, list) {
414 if (speed == USB_SPEED_HIGH) {
422 return config_buf(c, speed, cdev->req->buf, type);
432 static int count_configs(
struct usb_composite_dev *cdev,
unsigned type)
434 struct usb_gadget *gadget = cdev->gadget;
435 struct usb_configuration *c;
439 if (gadget_is_dualspeed(gadget)) {
440 if (gadget->speed == USB_SPEED_HIGH)
442 if (type == USB_DT_DEVICE_QUALIFIER)
445 list_for_each_entry(c, &cdev->configs, list) {
462 static void device_qual(
struct usb_composite_dev *cdev)
464 struct usb_qualifier_descriptor *qual = cdev->req->buf;
466 qual->bLength =
sizeof(*qual);
467 qual->bDescriptorType = USB_DT_DEVICE_QUALIFIER;
469 qual->bcdUSB = cdev->desc.bcdUSB;
470 qual->bDeviceClass = cdev->desc.bDeviceClass;
471 qual->bDeviceSubClass = cdev->desc.bDeviceSubClass;
472 qual->bDeviceProtocol = cdev->desc.bDeviceProtocol;
474 qual->bMaxPacketSize0 = cdev->desc.bMaxPacketSize0;
475 qual->bNumConfigurations = count_configs(cdev, USB_DT_DEVICE_QUALIFIER);
481 static void reset_config(
struct usb_composite_dev *cdev)
483 struct usb_function *f;
485 DBG(cdev,
"reset config\n");
487 list_for_each_entry(f, &cdev->config->functions, list) {
491 bitmap_zero(f->endpoints, 32);
500 static int set_config(
struct usb_composite_dev *cdev,
501 const struct usb_ctrlrequest *ctrl,
unsigned number)
503 struct usb_gadget *gadget = cdev->gadget;
504 struct usb_configuration *c =
NULL;
505 int result = -EINVAL;
506 unsigned power = gadget_is_otg(gadget) ? 8 : 100;
513 list_for_each_entry(c, &cdev->configs, list) {
514 if (c->bConfigurationValue == number) {
540 for (tmp = 0; tmp < MAX_CONFIG_INTERFACES; tmp++) {
541 struct usb_function *f = c->interface[tmp];
542 struct usb_descriptor_header **descriptors;
554 if (gadget->speed == USB_SPEED_HIGH)
555 descriptors = f->hs_descriptors;
557 descriptors = f->descriptors;
559 for (; *descriptors; ++descriptors) {
560 struct usb_endpoint_descriptor *ep;
563 if ((*descriptors)->bDescriptorType != USB_DT_ENDPOINT)
566 ep = (
struct usb_endpoint_descriptor *)*descriptors;
567 addr = ((ep->bEndpointAddress & 0x80) >> 3)
568 | (ep->bEndpointAddress & 0x0f);
569 set_bit(addr, f->endpoints);
572 result = f->set_alt(f, tmp, 0);
577 DBG(cdev,
"interface %d (%s/%p) alt 0 --> %d\n",
578 tmp, f->name, f, result);
587 power = c->bMaxPower ? (2 * c->bMaxPower) : CONFIG_USB_GADGET_VBUS_DRAW;
589 usb_gadget_vbus_draw(gadget, power);
615 struct usb_configuration *config)
617 int status = -EINVAL;
618 struct usb_configuration *c;
620 DBG(cdev,
"adding config #%u '%s'/%p\n",
621 config->bConfigurationValue,
622 config->label, config);
624 if (!config->bConfigurationValue || !config->bind)
628 list_for_each_entry(c, &cdev->configs, list) {
629 if (c->bConfigurationValue == config->bConfigurationValue) {
636 list_add_tail(&config->list, &cdev->configs);
638 INIT_LIST_HEAD(&config->functions);
639 config->next_interface_id = 0;
641 status = config->bind(config);
643 list_del(&config->list);
648 DBG(cdev,
"cfg %d/%p speeds:%s%s\n",
649 config->bConfigurationValue, config,
650 config->highspeed ?
" high" :
"",
652 ? (gadget_is_dualspeed(cdev->gadget)
657 for (i = 0; i < MAX_CONFIG_INTERFACES; i++) {
658 struct usb_function *f = config->interface[i];
662 DBG(cdev,
" interface %d = %s/%p\n",
670 usb_ep_autoconfig_reset(cdev->gadget);
674 DBG(cdev,
"added config '%s'/%u --> %d\n", config->label,
675 config->bConfigurationValue, status);
690 static void collect_langs(
struct usb_gadget_strings **sp, __le16 *buf)
692 const struct usb_gadget_strings *s;
698 language = cpu_to_le16(s->language);
699 for (tmp = buf; *tmp && tmp < &buf[126]; tmp++) {
700 if (*tmp == language)
709 static int lookup_string(
710 struct usb_gadget_strings **sp,
716 struct usb_gadget_strings *s;
721 if (s->language != language)
723 value = usb_gadget_get_string(s,
id, buf);
731 static int get_string(
struct usb_composite_dev *cdev,
732 void *buf,
u16 language,
int id)
734 struct usb_configuration *c;
735 struct usb_function *f;
745 struct usb_string_descriptor *s = buf;
746 struct usb_gadget_strings **sp;
749 s->bDescriptorType = USB_DT_STRING;
751 sp = composite->strings;
753 collect_langs(sp, s->wData);
755 list_for_each_entry(c, &cdev->configs, list) {
758 collect_langs(sp, s->wData);
760 list_for_each_entry(f, &c->functions, list) {
763 collect_langs(sp, s->wData);
767 for (len = 0; len <= 126 && s->wData[len]; len++)
772 s->bLength = 2 * (len + 1);
780 if (composite->strings) {
781 len = lookup_string(composite->strings, buf, language,
id);
785 list_for_each_entry(c, &cdev->configs, list) {
787 len = lookup_string(c->strings, buf, language,
id);
791 list_for_each_entry(f, &c->functions, list) {
794 len = lookup_string(f->strings, buf, language,
id);
821 if (cdev->next_string_id < 254) {
823 cdev->next_string_id++;
824 return cdev->next_string_id;
831 static void composite_setup_complete(
struct usb_ep *ep,
struct usb_request *req)
833 if (req->status || req->actual != req->length)
834 DBG((
struct usb_composite_dev *) ep->driver_data,
835 "setup complete --> %d, %d/%d\n",
836 req->status, req->actual, req->length);
849 composite_setup(
struct usb_gadget *gadget,
const struct usb_ctrlrequest *ctrl)
851 struct usb_composite_dev *cdev = get_gadget_data(gadget);
852 struct usb_request *req = cdev->req;
853 int value = -EOPNOTSUPP;
854 u16 w_index = le16_to_cpu(ctrl->wIndex);
855 u8 intf = w_index & 0xFF;
856 u16 w_value = le16_to_cpu(ctrl->wValue);
857 u16 w_length = le16_to_cpu(ctrl->wLength);
858 struct usb_function *f =
NULL;
866 req->complete = composite_setup_complete;
868 gadget->ep0->driver_data = cdev;
870 switch (ctrl->bRequest) {
872 case USB_REQ_GET_DESCRIPTOR:
874 if ((ctrl->bRequestType != USB_DIR_IN) && (ctrl->bRequestType != 0x81))
877 switch (w_value >> 8) {
881 cdev->desc.bNumConfigurations =
882 count_configs(cdev, USB_DT_DEVICE);
883 value = min(w_length, (
u16)
sizeof cdev->desc);
884 memcpy(req->buf, &cdev->desc, value);
887 case USB_DT_DEVICE_QUALIFIER:
889 if (!gadget_is_dualspeed(gadget))
892 value = min_t(
int, w_length,
893 sizeof(
struct usb_qualifier_descriptor));
896 case USB_DT_OTHER_SPEED_CONFIG:
898 if (!gadget_is_dualspeed(gadget))
903 value = config_desc(cdev, w_value);
905 value = min(w_length, (
u16) value);
909 value = get_string(cdev, req->buf,
910 w_index, w_value & 0xff);
912 value = min(w_length, (
u16) value);
920 printk(
"\nWE are in GET_REPORT_DESC !! SETUP in COMPUTIL.C - case 34");
925 memcpy(req->buf, hs_hid_report_descriptor, 29);
928 printk(
"\nWE are @ HIGH REPORT_DESC !!");
933 memcpy(req->buf, fs_hid_report_descriptor, 29);
936 printk(
"\nWE are @ FULL REPORT_DESC !!");
945 case USB_REQ_SET_CONFIGURATION:
947 if (ctrl->bRequestType != 0)
950 if (gadget_is_otg(gadget)) {
952 if (gadget->a_hnp_support)
953 DBG(cdev,
"HNP available\n");
954 else if (gadget->a_alt_hnp_support)
955 DBG(cdev,
"HNP on another port\n");
957 VDBG(cdev,
"HNP inactive\n");
960 spin_lock(&cdev->lock);
961 value = set_config(cdev, ctrl, w_value);
963 spin_unlock(&cdev->lock);
965 case USB_REQ_GET_CONFIGURATION:
967 if (ctrl->bRequestType != USB_DIR_IN)
971 *(
u8 *)req->buf = cdev->config->bConfigurationValue;
974 value = min(w_length, (
u16) 1);
980 case USB_REQ_SET_INTERFACE:
982 if (ctrl->bRequestType != USB_RECIP_INTERFACE)
985 if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES)
988 f = cdev->config->interface[intf];
992 if (w_value && !f->set_alt)
995 value = f->set_alt(f, w_index, w_value);
997 case USB_REQ_GET_INTERFACE:
999 if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
1002 if (!cdev->config || w_index >= MAX_CONFIG_INTERFACES)
1005 f = cdev->config->interface[intf];
1010 value = f->get_alt ? f->get_alt(f, w_index) : 0;
1014 *((
u8 *)req->buf) = value;
1015 value = min(w_length, (
u16) 1);
1020 "non-core control req%02x.%02x v%04x i%04x l%d\n",
1021 ctrl->bRequestType, ctrl->bRequest,
1022 w_value, w_index, w_length);
1032 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1034 case USB_RECIP_INTERFACE:
1036 f = cdev->config->interface[intf];
1039 case USB_RECIP_ENDPOINT:
1041 endp = ((w_index & 0x80) >> 3) | (w_index & 0x0f);
1042 list_for_each_entry(f, &cdev->config->functions, list) {
1043 if (test_bit(endp, f->endpoints))
1047 if (&f->list == &cdev->config->functions)
1053 value = f->setup(f, ctrl);
1056 struct usb_configuration *c;
1060 value = c->setup(c, ctrl);
1069 req->length = value;
1070 req->zero = value < w_length;
1071 value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
1074 DBG(cdev,
"ep_queue --> %d\n", value);
1076 composite_setup_complete(gadget->ep0, req);
1092 static void composite_disconnect(
struct usb_gadget *gadget)
1094 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1095 unsigned long flags;
1101 spin_lock_irqsave(&cdev->lock, flags);
1104 spin_unlock_irqrestore(&cdev->lock, flags);
1110 composite_unbind(
struct usb_gadget *gadget)
1112 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1119 WARN_ON(cdev->config);
1121 while (!list_empty(&cdev->configs)) {
1122 struct usb_configuration *c;
1124 c = list_first_entry(&cdev->configs,
1125 struct usb_configuration, list);
1126 while (!list_empty(&c->functions)) {
1127 struct usb_function *f;
1129 f = list_first_entry(&c->functions,
1130 struct usb_function, list);
1133 DBG(cdev,
"unbind function '%s'/%p\n",
1141 DBG(cdev,
"unbind config '%s'/%p\n", c->label, c);
1146 if (composite->unbind)
1147 composite->unbind(cdev);
1150 kfree(cdev->req->buf);
1151 usb_ep_free_request(gadget->ep0, cdev->req);
1154 set_gadget_data(gadget,
NULL);
1162 string_override_one(
struct usb_gadget_strings *tab,
u8 id,
const char *s)
1164 struct usb_string *str = tab->strings;
1166 for (str = tab->strings; str->s; str++) {
1167 if (str->id ==
id) {
1175 string_override(
struct usb_gadget_strings **tab,
u8 id,
const char *s)
1178 string_override_one(*tab,
id, s);
1183 static int composite_bind(
struct usb_gadget *gadget)
1185 struct usb_composite_dev *cdev;
1186 int status = -ENOMEM;
1188 cdev = kzalloc(
sizeof *cdev, GFP_KERNEL);
1192 spin_lock_init(&cdev->lock);
1193 cdev->gadget = gadget;
1194 set_gadget_data(gadget, cdev);
1195 INIT_LIST_HEAD(&cdev->configs);
1198 cdev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
1201 cdev->req->buf = kmalloc(
USB_BUFSIZ, GFP_KERNEL);
1202 if (!cdev->req->buf)
1204 cdev->req->complete = composite_setup_complete;
1205 gadget->ep0->driver_data = cdev;
1208 cdev->driver = composite;
1210 usb_gadget_set_selfpowered(gadget);
1216 usb_ep_autoconfig_reset(cdev->gadget);
1222 status = composite->bind(cdev);
1226 cdev->desc = *composite->dev;
1227 cdev->desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
1231 cdev->desc.idVendor = cpu_to_le16(idVendor);
1233 cdev->desc.idProduct = cpu_to_le16(idProduct);
1235 cdev->desc.bcdDevice = cpu_to_le16(bcdDevice);
1240 if (cdev->desc.iManufacturer && iManufacturer)
1241 string_override(composite->strings,
1242 cdev->desc.iManufacturer, iManufacturer);
1243 if (cdev->desc.iProduct && iProduct)
1244 string_override(composite->strings,
1245 cdev->desc.iProduct, iProduct);
1246 if (cdev->desc.iSerialNumber && iSerialNumber)
1247 string_override(composite->strings,
1248 cdev->desc.iSerialNumber, iSerialNumber);
1254 composite_unbind(gadget);
1262 composite_suspend(
struct usb_gadget *gadget)
1264 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1265 struct usb_function *f;
1270 DBG(cdev,
"suspend\n");
1272 list_for_each_entry(f, &cdev->config->functions, list) {
1277 if (composite->suspend)
1278 composite->suspend(cdev);
1286 composite_resume(
struct usb_gadget *gadget)
1288 struct usb_composite_dev *cdev = get_gadget_data(gadget);
1289 struct usb_function *f;
1294 DBG(cdev,
"resume\n");
1295 if (composite->resume)
1296 composite->resume(cdev);
1298 list_for_each_entry(f, &cdev->config->functions, list) {
1310 static struct usb_gadget_driver composite_driver = {
1311 .speed = USB_SPEED_HIGH,
1313 .bind = composite_bind,
1315 .unbind = composite_unbind,
1317 .setup = composite_setup,
1318 .disconnect = composite_disconnect,
1320 .suspend = composite_suspend,
1321 .resume = composite_resume,
1324 .owner = THIS_MODULE,
1345 if (!driver || !driver->dev || !driver->bind || composite)
1349 driver->name =
"composite";
1350 composite_driver.function = (
char *) driver->name;
1351 composite_driver.driver.name = driver->name;
1354 return usb_gadget_register_driver(&composite_driver);
1366 if (composite != driver)
1368 usb_gadget_unregister_driver(&composite_driver);
int usb_function_deactivate(struct usb_function *function)
module_param(idVendor, ushort, 0)
int usb_string_id(struct usb_composite_dev *cdev)
int usb_function_activate(struct usb_function *function)
MODULE_PARM_DESC(idVendor,"USB Vendor ID")
int usb_composite_register(struct usb_composite_driver *driver)
int usb_add_function(struct usb_configuration *config, struct usb_function *function)
void usb_composite_unregister(struct usb_composite_driver *driver)
int usb_interface_id(struct usb_configuration *config, struct usb_function *function)
int usb_add_config(struct usb_composite_dev *cdev, struct usb_configuration *config)