Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754717AbbKCM7r (ORCPT ); Tue, 3 Nov 2015 07:59:47 -0500 Received: from mailout1.samsung.com ([203.254.224.24]:55348 "EHLO mailout1.samsung.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753983AbbKCMyx (ORCPT ); Tue, 3 Nov 2015 07:54:53 -0500 X-AuditID: cbfee61a-f79a06d000005c6f-b6-5638ae92957a From: Robert Baldyga To: balbi@ti.com Cc: gregkh@linuxfoundation.org, andrzej.p@samsung.com, m.szyprowski@samsung.com, b.zolnierkie@samsung.com, linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, Robert Baldyga Subject: [PATCH 09/23] usb: gadget: composite: handle function bind Date: Tue, 03 Nov 2015 13:53:48 +0100 Message-id: <1446555242-3733-10-git-send-email-r.baldyga@samsung.com> X-Mailer: git-send-email 1.9.1 In-reply-to: <1446555242-3733-1-git-send-email-r.baldyga@samsung.com> References: <1446555242-3733-1-git-send-email-r.baldyga@samsung.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFprCLMWRmVeSWpSXmKPExsVy+t9jQd1J6yzCDB5fNbWY9bKdxWLjjPWs Fgfv11s0L17PZnF51xw2i0XLWpkt1h65y27x4PBOdgcOj/1z17B79G1Zxehx/MZ2Jo/Pm+QC WKK4bFJSczLLUov07RK4Mm513WMu2GVR8WVDC1sDY4NeFyMnh4SAicSEBW9ZIWwxiQv31rOB 2EICsxgl1h1U6mLkArJ/MkqsmXWYGSTBJqAjseX7BEYQW0RAQGL9i0vsIEXMAucYJR7eaQNL CAs4S+x8PhlsKouAqkTXxg4WEJtXwFXi88LlTBDb5CROHgOp4eDgBIpfeqcMYgoJuEgc384z gZF3ASPDKkaJ1ILkguKk9FzDvNRyveLE3OLSvHS95PzcTYzgsHomtYPx4C73Q4wCHIxKPLwL lpiHCbEmlhVX5h5ilOBgVhLh3T3XIkyINyWxsiq1KD++qDQntfgQozQHi5I4r76nUZiQQHpi SWp2ampBahFMlomDU6qBcck5zyQppuPPj/Jc+yRSyZ3jUNvCxsz74vOT8sS4bXIPFwWcbPEv 7PSyVBCL8lcVO/T0RvGs7znmSlWW0SJ7VTedW12e9abAP9zBZF9Em61Ht+TyKv/oK/4Jtx4F nTllb8x5ctfV6u+fyywyVKNvrb2sfmprc9EhAZY1n2ulH5wpUAr/LzJJiaU4I9FQi7moOBEA KvlMKicCAAA= Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 7243 Lines: 233 As now USB function supplies entity descriptors to composite in prep_descs() callback, we can perform bind inside composite framework without involving bind() callback (which now is unused and will be removed after converting all functions in kernel to new API). For now we bind each configuration when it's added, because we have to support functions based on old API, but after completing conversion of functions, we will be able to do bind after adding all configurations. Also more sophisticated autoconfig solver will be provided to improve utilization of available hardware endpoints. Signed-off-by: Robert Baldyga --- drivers/usb/gadget/composite.c | 162 +++++++++++++++++++++++++++++++++++++++++ include/linux/usb/composite.h | 3 + 2 files changed, 165 insertions(+) diff --git a/drivers/usb/gadget/composite.c b/drivers/usb/gadget/composite.c index 3ecfaca..f4189b1 100644 --- a/drivers/usb/gadget/composite.c +++ b/drivers/usb/gadget/composite.c @@ -327,6 +327,21 @@ int usb_function_activate(struct usb_function *function) EXPORT_SYMBOL_GPL(usb_function_activate); /** + * usb_function_is_new_api - checks if USB function uses new API + * @f: USB function + * + * This function is added temporarily to allow both old and new function API + * to coexist. It function will be removed after converting all USB functions + * in kernel to new API. + * + * Returns true if function uses new API. + */ +static inline bool usb_function_is_new_api(struct usb_function *f) +{ + return !!f->prep_descs; +} + +/** * usb_function_set_descs - assing descriptors to USB function * @f: USB function * @descs: USB descriptors to be assigned to function @@ -1109,6 +1124,12 @@ int usb_add_config(struct usb_composite_dev *cdev, goto done; status = bind(config); + if (status < 0) + goto out; + + status = usb_config_do_bind(config); + +out: if (status < 0) { while (!list_empty(&config->functions)) { struct usb_function *f; @@ -2404,6 +2425,147 @@ void composite_dev_cleanup(struct usb_composite_dev *cdev) device_remove_file(&cdev->gadget->dev, &dev_attr_suspended); } +/** + * usb_cmp_ep_descs - compare descriptors of two endpoints + * + * As currently during autoconfig procedure we take into consideration only + * FullSpeed and SuperSpeed Companion descriptors, we need to compare only + * these descriptors. It they are the same, endpoints are identical from + * autoconfig point of view. + */ +static int usb_cmp_ep_descs(struct usb_composite_ep *ep1, + struct usb_composite_ep *ep2) +{ + if (ep1->fs.desc->bLength != ep2->fs.desc->bLength) + return 0; + if (usb_endpoint_dir_in(ep1->fs.desc) ^ + usb_endpoint_dir_in(ep2->fs.desc)) + return 0; + if (ep1->fs.desc->bmAttributes != ep2->fs.desc->bmAttributes) + return 0; + if (ep1->fs.desc->wMaxPacketSize != ep2->fs.desc->wMaxPacketSize) + return 0; + if (ep1->fs.desc->bInterval != ep2->fs.desc->bInterval) + return 0; + + if (ep1->fs.desc->bLength != USB_DT_ENDPOINT_AUDIO_SIZE) + goto ss_comp; + + if (ep1->fs.desc->bRefresh != ep2->fs.desc->bRefresh) + return 0; + if (ep1->fs.desc->bSynchAddress != ep2->fs.desc->bSynchAddress) + return 0; + +ss_comp: + if (!ep1->ss_comp.desc ^ !ep2->ss_comp.desc) + return 0; + if (!ep1->ss_comp.desc) + return 1; + + if (ep1->ss_comp.desc->bMaxBurst != ep2->ss_comp.desc->bMaxBurst) + return 0; + if (ep1->ss_comp.desc->bmAttributes != ep2->ss_comp.desc->bmAttributes) + return 0; + if (ep1->ss_comp.desc->wBytesPerInterval != + ep2->ss_comp.desc->wBytesPerInterval) + return 0; + + return 1; +} + +/** + * ep_update_address() - update endpoint address in descriptors + * @ep: composite endpoint with assigned hardware ep + * + * This function should be called after setting ep->ep to endpoint obtained + * from usb_ep_autoconfig_ss(), to update endpoint address in descriptors for + * all supported speeds. + */ +static inline void ep_update_address(struct usb_composite_ep *ep) +{ + if (ep->fs.desc) + ep->hs.desc->bEndpointAddress = ep->ep->address; + if (ep->hs.desc) + ep->hs.desc->bEndpointAddress = ep->ep->address; + if (ep->ss.desc) + ep->ss.desc->bEndpointAddress = ep->ep->address; +} + +/** + * interface_do_bind() - bind interface to UDC + * @c: USB configuration + * @f: USB function in configuration c + * @intf: USB interface in function f + * + * For now we use only simple interface-level ep aucoconfig solver. + * We share endpoints between altsettings where it's possible. + */ +static int interface_do_bind(struct usb_configuration *c, + struct usb_function *f, struct usb_composite_intf *intf) +{ + struct usb_composite_altset *alt, *altx; + struct usb_composite_ep *ep, *epx; + int a, e, ax, ex; + + intf->id = usb_interface_id(c, f); + intf->cur_altset = -1; + + for (a = 0; a < intf->altsets_num; ++a) { + alt = intf->altsets[a]; + alt->alt.desc->bInterfaceNumber = intf->id; + for (e = 0; e < alt->eps_num; ++e) { + ep = alt->eps[e]; + if (ep->ep) + continue; + ep->ep = usb_ep_autoconfig_ss(c->cdev->gadget, + ep->fs.desc, ep->ss_comp.desc); + if (!ep->ep) + return -ENODEV; + ep_update_address(ep); + /* Try endpoint for other altsets */ + for (ax = a + 1; ax < intf->altsets_num; ++ax) { + altx = intf->altsets[ax]; + for (ex = 0; ex < altx->eps_num; ++ex) { + epx = altx->eps[ex]; + if (usb_cmp_ep_descs(ep, epx)) { + epx->ep = ep->ep; + ep_update_address(epx); + } + } + } + } + } + + return 0; +} + +/** + * config_do_bind() - bind configuration to UDC + * @c: USB configuration + * + * Bind the all functions in configuration to UDC. + */ +int usb_config_do_bind(struct usb_configuration *c) +{ + struct usb_function *f; + struct usb_composite_intf *intf; + int i, ret; + + list_for_each_entry(f, &c->functions, list) { + if (!usb_function_is_new_api(f)) + continue; + for (i = 0; i < f->descs->intfs_num; ++i) { + intf = f->descs->intfs[i]; + ret = interface_do_bind(c, f, intf); + if (ret) + return ret; + } + } + + return 0; +} +EXPORT_SYMBOL_GPL(usb_config_do_bind); + static int composite_bind(struct usb_gadget *gadget, struct usb_gadget_driver *gdriver) { diff --git a/include/linux/usb/composite.h b/include/linux/usb/composite.h index 58d2929..a92da38 100644 --- a/include/linux/usb/composite.h +++ b/include/linux/usb/composite.h @@ -413,6 +413,7 @@ int usb_altset_add_vendor_desc(struct usb_function *f, int i, int a, int usb_ep_add_vendor_desc(struct usb_function *f, int i, int a, int e, struct usb_descriptor_header *desc); + int usb_interface_id(struct usb_configuration *, struct usb_function *); int config_ep_by_speed(struct usb_gadget *g, struct usb_function *f, @@ -503,6 +504,8 @@ int usb_add_config(struct usb_composite_dev *, void usb_remove_config(struct usb_composite_dev *, struct usb_configuration *); +int usb_config_do_bind(struct usb_configuration *c); + /* predefined index for usb_composite_driver */ enum { USB_GADGET_MANUFACTURER_IDX = 0, -- 1.9.1 -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/