mirror of
https://github.com/S3NEO/android_kernel_samsung_msm8226.git
synced 2024-11-07 03:47:13 +00:00
0943bd6d54
usb: gadget: f_fs: HACK: Round reads up to 512 bytes to work with dwc3 Signed-off-by: Arve Hjønnevåg <arve@android.com> USB: f_fs: Fix epfile crash during composition switch epfile's ep pointer may be NULL during adb transfer and composition switch happening in parallel. As part of composition switch, first it is set to NONE. Setting sys.usb.config to NONE stops adb and disables the composition. stop adb is not blocking call and adb still might be doing epfile read/write for some time when function unbind is ongoing making the data structures NULL. To fix this crash, call usb_ep_dequeue only if ep->ep is valid. Similarly in success case, return ep->status only if ep->ep is valid otherwise return -ENODEV. CRs-Fixed: 643663 Change-Id: Ic152fc1db31cad6f97b8d16d91350dad857a4bf9 Signed-off-by: Sujeet Kumar <ksujeet@codeaurora.org> USB: gadget: f_fs: Release endpoint upon disable Endpoints are claimed using usb_ep_autoconfig function, It will choose an unclaimed usb_ep and prevent the endpoint from being returned by a later autoconfig calls. We can mark the driver_data pointer once ep_enable is done in bind. If we cannot mark to null upon function disable the corresponding endpoint is not allocated by a later autoconfig call. The current code does not make the ep->driver_data to null upon function disable. This is leading to unclaimed endpoints for later autoconfig calls. Claim the endpoints by assigning ep->driver_data to NULL. CRs-Fixed: 633673 Change-Id: I221b98ef36cc2a60d27507a2442061a30ed410f4 Signed-off-by: ChandanaKishori Chiluveru <cchilu@codeaurora.org> USB: gagget: f_fs: Return error if TX req is queued during device offline when USB cable is disconnected during TX data transfers, endpoints will be disabled during function disable. If userspace client tries to queue requests on disabled endpoints, driver will wait till endpoints are enabled and then queues previous session requests. This results in kernel driver and userspace driver out of sync and due to this, stall will be seen. Hence fix this issue by returning error value if client tries to queue requests on TX endpoint during device offline. CRs-Fixed: 633497 Change-Id: I3e43b8a704367aff7fe8dd88159315aef811c51c Signed-off-by: Vijayavardhan Vennapusa <vvreddy@codeaurora.org> USB: f_fs: Fail stale read IOs after disconnect After a USB disconnect, endpoints for adb are disabled. After this no IO is allowed on the endpoints. Since, adbd is not aware of this disconnect, it may still perform read/writes IO. For adb writes, IOs are failed, but for adb reads kernel waits untill endpoints are enabled. When a USB disconnect and adb read still queued a buffer to kernel, ffs_epfile_io simply waits for endpoint to be enabled. A next connect happens and endpoints are enabled after set_alt, the adb read stale buffer from previous session continues and queues to endpoint. All this time, adb did not close the epfile because it did not get return status on the IOs which it queued. This is an issue, because a new session is not established and both userspace and kernel goes out of sync. To fix this issue, when endpoints are disbled set epfile error. This epfile error is only cleared in epfile open. This will ensure that after a USB disconnect and connect, new session is established. Also, return ENODEV if endpoints not enabled rather than EINTR as EINTR case, and simply retries the request. Incase usb_ep_queue failed, return -EIO inspite of depend on return status from usb_ep_queue. CRs-Fixed: 633497 Change-Id: I6e677e98ec28e5462b372ed290acdde251286f48 Signed-off-by: Sujeet Kumar <ksujeet@codeaurora.org> USB: f_fs: Cutoff epfile IO before epfile could get freed epfile may get freed and accessing epfile's error flag to cut off IOs may lead to use after free. Move the epfile error flag setting above in the order so that it guaranteed to be valid. CRs-Fixed: 668046 Change-Id: I0017513393ddb4fd288cd4e1c2adf9d5ee3bc660 Signed-off-by: Sujeet Kumar <ksujeet@codeaurora.org> USB: f_fs: Check error status before doing epfile I/O Set error status before disabling endpoint during function disable and also check error status before handling I/O. If error status is set, return error status to read/write calls made by userspace. Also set file's private data to NULL during epfile release. CRs-Fixed: 671880 Change-Id: I14b5ee541dfc18a7802ef4a8033878a7729d9adb Signed-off-by: Vijayavardhan Vennapusa <vvreddy@codeaurora.org> USB: f_fs: Fix disconnect check during ongoing IO F_FS function driver allocated ffs_eps and updates ffs_ep->ep to corresponding usb_ep during func->bind and never clears it. On bind it also saves ffs_ep context in epfile->ep. During func->disable, it clears only ffs_ep context in epfile->ep and on func->unbind it frees ffs_eps memory. ffs_epfile_io routine currently relies on ffs_ep->ep (which is never cleared and ffs_ep could be freed on unbind) to detect any disconnect during active IO. This can result in various issues e.g. use after free use of ffs_ep if unbind finished before epfile_io could resume or "stop adbd" trying to dequeue a freed USB request when epfile_io could execute only after F_FS got disabled as 'if (ep->ep)' check would be TRUE. Fix this by checking stored ffs_ep context against latest epfile->ep to figure out if endpoint got disabled or changed before acquiring spin_lock. Change-Id: I6bdcdf0dff0813ed7b2af8c24f544a22796b0369 Signed-off-by: Manu Gautam <mgautam@codeaurora.org> USB: f_fs: Move ep completion out of stack Allocating completion on the stack may lead to invalid access when udc irq tries to complete the request but interrupted completion returns immediately. This happens because request is not held to be dequeued anymore making the completion invalid. Move the completions in ffs data like it is for ep0. CRs-Fixed: 653761 Change-Id: I15102538d1b5bee14dfa3c7b3fa1f8e3f767cf71 Signed-off-by: Sujeet Kumar <ksujeet@codeaurora.org> usb: dwc3: gadget: Release gadget lock when handling suspend/resume gadget_driver suspend/resume operations might require some dwc3-gadget operations, such as enabling and disabling endpoints. If the lock is not released, this can cause a deadlock scenario. Change-Id: I1e12de65e40492b115ab35de78c2352730649db5 Signed-off-by: Bar Weiner <bweiner@codeaurora.org> usb: dwc3: gadget: Iterate only over valid endpoints Make dwc3_gadget_resize_tx_fifos() iterate only over IN endpoints that are actually present, based on the num_in_eps parameter. This terminates the loop so as to prevent dereferencing a potential NULL dwc->eps[i] where i >= (num_in_eps + num_out_eps). Change-Id: I07f711bfd380dce212e86b59cf417f84ca7eb006 Signed-off-by: Jack Pham <jackp@codeaurora.org> usb: dwc3: gadget: Protect against ep disabling during completion In dwc3_cleanup_done_reqs(), a potential race condition could arise when dwc3_gadget_giveback() temporarily releases the main spinlock. If during this window the very endpoint being handled becomes disabled, it would lead to a NULL pointer dereference in the code that follows. Guard against this by making sure the endpoint is still enabled after returning from the giveback call. CRs-fixed: 628972 Change-Id: Ifdb823fff12747f699217d871a5959c85b5340f7 Signed-off-by: Jack Pham <jackp@codeaurora.org> usb: dwc3: calculate the number of endpoints hwparams2 holds the number of endpoints which were selected during RTL generation, we can use that on our driver. Signed-off-by: Felipe Balbi <balbi@ti.com> usb: dwc3: gadget: use num_(in|out)_eps from HW params that way we will only tell gadget framework about the endpoints we actually have. Change-Id: Iabc6a5712b640a9f5b0310984650a4ac44e5f579 Signed-off-by: Felipe Balbi <balbi@ti.com> usb: gadget: always update HS/SS descriptors and create a copy of them HS and SS descriptors are staticaly created. They are updated during the bind process with the endpoint address, string id or interface numbers. After that, the descriptor chain is linked to struct usb_function which is used by composite in order to serve the GET_DESCRIPTOR requests, number of available configs and so on. There is no need to assign the HS descriptor only if the UDC supports HS speed because composite won't report those to the host if HS support has not been reached. The same reasoning is valid for SS. This patch makes sure each function updates HS/SS descriptors unconditionally and uses the newly introduced helper function to create a copy the descriptors for the speed which is supported by the UDC. While at that, also rename f->descriptors to f->fs_descriptors in order to make it more explicit what that means. Change-Id: Id670fcc25b0a1cb3020722cfc6eda2e1b08441f1 Cc: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de> Signed-off-by: Felipe Balbi <balbi@ti.com> USB: Add super speed descriptors for android functions Update android function drivers like diag, adb, modem, rmnet, mtp and accessory to operate in super speed. The burst capability is not enabled for now. Change-Id: Ie95cbfc9444c56c8268b70e2916713190699c71a Signed-off-by: Pavankumar Kondeti <pkondeti@codeaurora.org> usb: gadget: Finish conversion to fs_descriptor change Change-Id: Iaf72d66bb5cd6b84f14c5aaeb01ffb286568c97b usb: gadget: f_fs: Add support for SuperSpeed Mode Allow userspace to pass SuperSpeed descriptors and handle them in the driver accordingly. This change doesn't modify existing desc_header and thereby keeps the ABI changes backward compatible i.e. existing userspace drivers compiled with old header (functionfs.h) would continue to work with the updated kernel. Change-Id: Ic27035fdef2a83828024348d75be1518e9f8c5c6 Signed-off-by: Manu Gautam <mgautam@codeaurora.org> USB: f_fs: Set ffs->func to NULL after disabling endpoint in set_alt() When adb root is performed, userspace will close and open ffs_epsfile. Closing this file will call ffs_functionfs_callback() which does call remove_config(). This will call ffs_function_eps_disable to disable endpoints and then calls ffs_func_unbind(). Unbind() will also call endpoint disable which might lead to disabling endpoint which is already disabled. Hence set ffs->func to NULL after disabling endpoints in set_alt(). CRs-Fixed: 557532 Change-Id: I3052bdee74a1793d4e003de4b991d353e5d699b0 Signed-off-by: Vijayavardhan Vennapusa <vvreddy@codeaurora.org> usb: gadget: throttle IRQ rate for SuperSpeed There was a merge error from commit 6e0c86d12 "USB: gadget: u_ether: Fix data stall issue in RNDIS tethering mode" that resulted in the accidental removal of checking if the gadget is connected at SuperSpeed. Re-introduce this check so that IRQs on the downlink path are throttled, decreasing the load on the CPU. Change-Id: Ic2aa1d433e0fded95c6e825a760e89f726360522 Signed-off-by: Jack Pham <jackp@codeaurora.org> USB: mbim: Add super speed descriptors for MBIM function This change adds super speed descriptors which is required to get MBIM function to work with SSUSB mode. The burst capability is not enabled for now. CRs-Fixed: 626744 Change-Id: I2a492182c94265ab58014cac470448f61782625c Signed-off-by: Mayank Rana <mrana@codeaurora.org> usb: gadget: ECM: Add super speed descriptors for qc_ecm function This change adds super speed descriptors which is required to get ECM function to work with SSUSB mode. CRs-Fixed: 627063 Change-Id: I275a32f6cb957b59bfdf1c5b5377ba6e189efb6d Signed-off-by: Mayank Rana <mrana@codeaurora.org> usb: gadget: Add file for USB HID function This file the same as f_hid.c. Change-Id: I951b3067f477c3cb502c8320693ab11df90150d2 Signed-off-by: muluhe <muluhe@codeaurora.org> Signed-off-by: Aravind Asam <aasam@codeaurora.org> Signed-off-by: Ameya Thakur <ameyat@codeaurora.org> usb: gadget: Enable HID function for charging mode Provide HID function for only charging mode, in this mode device enumerated as one input device. Change-Id: I769adf76807b8a28adcc298de0536fa779176016 Signed-off-by: Mulu He <muluhe@codeaurora.org> usb: gadget: composite: Fix USB version number for L1 When usb version number is greater than 2.01 USB-CV expects to find a Super Speed USB Device Capability descriptor. When we want to enable BOS descriptor capabilities for a high-speed device the USB version number should be 2.01. CRs-Fixed: 521752 Change-Id: Ic75b5e570b3c2df8e67370389dfddc8de6fb72d4 Signed-off-by: Shimrit Malichi <smalichi@codeaurora.org> usb: gadget: Fix compilation of f_mbim driver after SS updates Change-Id: I72e7dfa5c8f3905bbe57e227ebb7e7035d8b671c [haggertk: port to samsung_msm8974, don't pick this for your own use] Signed-off-by: Kevin F. Haggerty <haggertk@lineageos.org>
897 lines
25 KiB
C
897 lines
25 KiB
C
/*
|
|
* f_ecm.c -- USB CDC Ethernet (ECM) link function driver
|
|
*
|
|
* Copyright (C) 2003-2005,2008 David Brownell
|
|
* Copyright (C) 2008 Nokia Corporation
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
* (at your option) any later version.
|
|
*/
|
|
|
|
/* #define VERBOSE_DEBUG */
|
|
|
|
#include <linux/slab.h>
|
|
#include <linux/kernel.h>
|
|
#include <linux/device.h>
|
|
#include <linux/etherdevice.h>
|
|
|
|
#include "u_ether.h"
|
|
|
|
|
|
/*
|
|
* This function is a "CDC Ethernet Networking Control Model" (CDC ECM)
|
|
* Ethernet link. The data transfer model is simple (packets sent and
|
|
* received over bulk endpoints using normal short packet termination),
|
|
* and the control model exposes various data and optional notifications.
|
|
*
|
|
* ECM is well standardized and (except for Microsoft) supported by most
|
|
* operating systems with USB host support. It's the preferred interop
|
|
* solution for Ethernet over USB, at least for firmware based solutions.
|
|
* (Hardware solutions tend to be more minimalist.) A newer and simpler
|
|
* "Ethernet Emulation Model" (CDC EEM) hasn't yet caught on.
|
|
*
|
|
* Note that ECM requires the use of "alternate settings" for its data
|
|
* interface. This means that the set_alt() method has real work to do,
|
|
* and also means that a get_alt() method is required.
|
|
*/
|
|
|
|
|
|
enum ecm_notify_state {
|
|
ECM_NOTIFY_NONE, /* don't notify */
|
|
ECM_NOTIFY_CONNECT, /* issue CONNECT next */
|
|
ECM_NOTIFY_SPEED, /* issue SPEED_CHANGE next */
|
|
};
|
|
|
|
struct f_ecm {
|
|
struct gether port;
|
|
u8 ctrl_id, data_id;
|
|
|
|
char ethaddr[14];
|
|
|
|
struct usb_ep *notify;
|
|
struct usb_request *notify_req;
|
|
u8 notify_state;
|
|
bool is_open;
|
|
|
|
/* FIXME is_open needs some irq-ish locking
|
|
* ... possibly the same as port.ioport
|
|
*/
|
|
};
|
|
|
|
static inline struct f_ecm *func_to_ecm(struct usb_function *f)
|
|
{
|
|
return container_of(f, struct f_ecm, port.func);
|
|
}
|
|
|
|
/* peak (theoretical) bulk transfer rate in bits-per-second */
|
|
static inline unsigned ecm_bitrate(struct usb_gadget *g)
|
|
{
|
|
if (gadget_is_superspeed(g) && g->speed == USB_SPEED_SUPER)
|
|
return 13 * 1024 * 8 * 1000 * 8;
|
|
else if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
|
|
return 13 * 512 * 8 * 1000 * 8;
|
|
else
|
|
return 19 * 64 * 1 * 1000 * 8;
|
|
}
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
/*
|
|
* Include the status endpoint if we can, even though it's optional.
|
|
*
|
|
* Use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
|
|
* packet, to simplify cancellation; and a big transfer interval, to
|
|
* waste less bandwidth.
|
|
*
|
|
* Some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
|
|
* if they ignore the connect/disconnect notifications that real aether
|
|
* can provide. More advanced cdc configurations might want to support
|
|
* encapsulated commands (vendor-specific, using control-OUT).
|
|
*/
|
|
|
|
#define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
|
|
#define ECM_STATUS_BYTECOUNT 16 /* 8 byte header + data */
|
|
|
|
|
|
/* interface descriptor: */
|
|
|
|
static struct usb_interface_assoc_descriptor
|
|
ecm_iad_descriptor = {
|
|
.bLength = sizeof ecm_iad_descriptor,
|
|
.bDescriptorType = USB_DT_INTERFACE_ASSOCIATION,
|
|
|
|
/* .bFirstInterface = DYNAMIC, */
|
|
.bInterfaceCount = 2, /* control + data */
|
|
.bFunctionClass = USB_CLASS_COMM,
|
|
.bFunctionSubClass = USB_CDC_SUBCLASS_ETHERNET,
|
|
.bFunctionProtocol = USB_CDC_PROTO_NONE,
|
|
/* .iFunction = DYNAMIC */
|
|
};
|
|
|
|
|
|
static struct usb_interface_descriptor ecm_control_intf = {
|
|
.bLength = sizeof ecm_control_intf,
|
|
.bDescriptorType = USB_DT_INTERFACE,
|
|
|
|
/* .bInterfaceNumber = DYNAMIC */
|
|
/* status endpoint is optional; this could be patched later */
|
|
.bNumEndpoints = 1,
|
|
.bInterfaceClass = USB_CLASS_COMM,
|
|
.bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
|
|
.bInterfaceProtocol = USB_CDC_PROTO_NONE,
|
|
/* .iInterface = DYNAMIC */
|
|
};
|
|
|
|
static struct usb_cdc_header_desc ecm_header_desc = {
|
|
.bLength = sizeof ecm_header_desc,
|
|
.bDescriptorType = USB_DT_CS_INTERFACE,
|
|
.bDescriptorSubType = USB_CDC_HEADER_TYPE,
|
|
|
|
.bcdCDC = cpu_to_le16(0x0110),
|
|
};
|
|
|
|
static struct usb_cdc_union_desc ecm_union_desc = {
|
|
.bLength = sizeof(ecm_union_desc),
|
|
.bDescriptorType = USB_DT_CS_INTERFACE,
|
|
.bDescriptorSubType = USB_CDC_UNION_TYPE,
|
|
/* .bMasterInterface0 = DYNAMIC */
|
|
/* .bSlaveInterface0 = DYNAMIC */
|
|
};
|
|
|
|
static struct usb_cdc_ether_desc ecm_desc = {
|
|
.bLength = sizeof ecm_desc,
|
|
.bDescriptorType = USB_DT_CS_INTERFACE,
|
|
.bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
|
|
|
|
/* this descriptor actually adds value, surprise! */
|
|
/* .iMACAddress = DYNAMIC */
|
|
.bmEthernetStatistics = cpu_to_le32(0), /* no statistics */
|
|
.wMaxSegmentSize = cpu_to_le16(ETH_FRAME_LEN),
|
|
.wNumberMCFilters = cpu_to_le16(0),
|
|
.bNumberPowerFilters = 0,
|
|
};
|
|
|
|
/* the default data interface has no endpoints ... */
|
|
|
|
static struct usb_interface_descriptor ecm_data_nop_intf = {
|
|
.bLength = sizeof ecm_data_nop_intf,
|
|
.bDescriptorType = USB_DT_INTERFACE,
|
|
|
|
.bInterfaceNumber = 1,
|
|
.bAlternateSetting = 0,
|
|
.bNumEndpoints = 0,
|
|
.bInterfaceClass = USB_CLASS_CDC_DATA,
|
|
.bInterfaceSubClass = 0,
|
|
.bInterfaceProtocol = 0,
|
|
/* .iInterface = DYNAMIC */
|
|
};
|
|
|
|
/* ... but the "real" data interface has two bulk endpoints */
|
|
|
|
static struct usb_interface_descriptor ecm_data_intf = {
|
|
.bLength = sizeof ecm_data_intf,
|
|
.bDescriptorType = USB_DT_INTERFACE,
|
|
|
|
.bInterfaceNumber = 1,
|
|
.bAlternateSetting = 1,
|
|
.bNumEndpoints = 2,
|
|
.bInterfaceClass = USB_CLASS_CDC_DATA,
|
|
.bInterfaceSubClass = 0,
|
|
.bInterfaceProtocol = 0,
|
|
/* .iInterface = DYNAMIC */
|
|
};
|
|
|
|
/* full speed support: */
|
|
|
|
static struct usb_endpoint_descriptor fs_ecm_notify_desc = {
|
|
.bLength = USB_DT_ENDPOINT_SIZE,
|
|
.bDescriptorType = USB_DT_ENDPOINT,
|
|
|
|
.bEndpointAddress = USB_DIR_IN,
|
|
.bmAttributes = USB_ENDPOINT_XFER_INT,
|
|
.wMaxPacketSize = cpu_to_le16(ECM_STATUS_BYTECOUNT),
|
|
.bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
|
|
};
|
|
|
|
static struct usb_endpoint_descriptor fs_ecm_in_desc = {
|
|
.bLength = USB_DT_ENDPOINT_SIZE,
|
|
.bDescriptorType = USB_DT_ENDPOINT,
|
|
|
|
.bEndpointAddress = USB_DIR_IN,
|
|
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
|
};
|
|
|
|
static struct usb_endpoint_descriptor fs_ecm_out_desc = {
|
|
.bLength = USB_DT_ENDPOINT_SIZE,
|
|
.bDescriptorType = USB_DT_ENDPOINT,
|
|
|
|
.bEndpointAddress = USB_DIR_OUT,
|
|
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
|
};
|
|
|
|
static struct usb_descriptor_header *ecm_fs_function[] = {
|
|
/* CDC ECM control descriptors */
|
|
(struct usb_descriptor_header *) &ecm_iad_descriptor,
|
|
(struct usb_descriptor_header *) &ecm_control_intf,
|
|
(struct usb_descriptor_header *) &ecm_header_desc,
|
|
(struct usb_descriptor_header *) &ecm_union_desc,
|
|
(struct usb_descriptor_header *) &ecm_desc,
|
|
|
|
/* NOTE: status endpoint might need to be removed */
|
|
(struct usb_descriptor_header *) &fs_ecm_notify_desc,
|
|
|
|
/* data interface, altsettings 0 and 1 */
|
|
(struct usb_descriptor_header *) &ecm_data_nop_intf,
|
|
(struct usb_descriptor_header *) &ecm_data_intf,
|
|
(struct usb_descriptor_header *) &fs_ecm_in_desc,
|
|
(struct usb_descriptor_header *) &fs_ecm_out_desc,
|
|
NULL,
|
|
};
|
|
|
|
/* high speed support: */
|
|
|
|
static struct usb_endpoint_descriptor hs_ecm_notify_desc = {
|
|
.bLength = USB_DT_ENDPOINT_SIZE,
|
|
.bDescriptorType = USB_DT_ENDPOINT,
|
|
|
|
.bEndpointAddress = USB_DIR_IN,
|
|
.bmAttributes = USB_ENDPOINT_XFER_INT,
|
|
.wMaxPacketSize = cpu_to_le16(ECM_STATUS_BYTECOUNT),
|
|
.bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
|
|
};
|
|
|
|
static struct usb_endpoint_descriptor hs_ecm_in_desc = {
|
|
.bLength = USB_DT_ENDPOINT_SIZE,
|
|
.bDescriptorType = USB_DT_ENDPOINT,
|
|
|
|
.bEndpointAddress = USB_DIR_IN,
|
|
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
|
.wMaxPacketSize = cpu_to_le16(512),
|
|
};
|
|
|
|
static struct usb_endpoint_descriptor hs_ecm_out_desc = {
|
|
.bLength = USB_DT_ENDPOINT_SIZE,
|
|
.bDescriptorType = USB_DT_ENDPOINT,
|
|
|
|
.bEndpointAddress = USB_DIR_OUT,
|
|
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
|
.wMaxPacketSize = cpu_to_le16(512),
|
|
};
|
|
|
|
static struct usb_descriptor_header *ecm_hs_function[] = {
|
|
/* CDC ECM control descriptors */
|
|
(struct usb_descriptor_header *) &ecm_iad_descriptor,
|
|
(struct usb_descriptor_header *) &ecm_control_intf,
|
|
(struct usb_descriptor_header *) &ecm_header_desc,
|
|
(struct usb_descriptor_header *) &ecm_union_desc,
|
|
(struct usb_descriptor_header *) &ecm_desc,
|
|
|
|
/* NOTE: status endpoint might need to be removed */
|
|
(struct usb_descriptor_header *) &hs_ecm_notify_desc,
|
|
|
|
/* data interface, altsettings 0 and 1 */
|
|
(struct usb_descriptor_header *) &ecm_data_nop_intf,
|
|
(struct usb_descriptor_header *) &ecm_data_intf,
|
|
(struct usb_descriptor_header *) &hs_ecm_in_desc,
|
|
(struct usb_descriptor_header *) &hs_ecm_out_desc,
|
|
NULL,
|
|
};
|
|
|
|
/* super speed support: */
|
|
|
|
static struct usb_endpoint_descriptor ss_ecm_notify_desc = {
|
|
.bLength = USB_DT_ENDPOINT_SIZE,
|
|
.bDescriptorType = USB_DT_ENDPOINT,
|
|
|
|
.bEndpointAddress = USB_DIR_IN,
|
|
.bmAttributes = USB_ENDPOINT_XFER_INT,
|
|
.wMaxPacketSize = cpu_to_le16(ECM_STATUS_BYTECOUNT),
|
|
.bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
|
|
};
|
|
|
|
static struct usb_ss_ep_comp_descriptor ss_ecm_intr_comp_desc = {
|
|
.bLength = sizeof ss_ecm_intr_comp_desc,
|
|
.bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
|
|
|
|
/* the following 3 values can be tweaked if necessary */
|
|
/* .bMaxBurst = 0, */
|
|
/* .bmAttributes = 0, */
|
|
.wBytesPerInterval = cpu_to_le16(ECM_STATUS_BYTECOUNT),
|
|
};
|
|
|
|
static struct usb_endpoint_descriptor ss_ecm_in_desc = {
|
|
.bLength = USB_DT_ENDPOINT_SIZE,
|
|
.bDescriptorType = USB_DT_ENDPOINT,
|
|
|
|
.bEndpointAddress = USB_DIR_IN,
|
|
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
|
.wMaxPacketSize = cpu_to_le16(1024),
|
|
};
|
|
|
|
static struct usb_endpoint_descriptor ss_ecm_out_desc = {
|
|
.bLength = USB_DT_ENDPOINT_SIZE,
|
|
.bDescriptorType = USB_DT_ENDPOINT,
|
|
|
|
.bEndpointAddress = USB_DIR_OUT,
|
|
.bmAttributes = USB_ENDPOINT_XFER_BULK,
|
|
.wMaxPacketSize = cpu_to_le16(1024),
|
|
};
|
|
|
|
static struct usb_ss_ep_comp_descriptor ss_ecm_bulk_comp_desc = {
|
|
.bLength = sizeof ss_ecm_bulk_comp_desc,
|
|
.bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
|
|
|
|
/* the following 2 values can be tweaked if necessary */
|
|
/* .bMaxBurst = 0, */
|
|
/* .bmAttributes = 0, */
|
|
};
|
|
|
|
static struct usb_descriptor_header *ecm_ss_function[] = {
|
|
/* CDC ECM control descriptors */
|
|
(struct usb_descriptor_header *) &ecm_control_intf,
|
|
(struct usb_descriptor_header *) &ecm_header_desc,
|
|
(struct usb_descriptor_header *) &ecm_union_desc,
|
|
(struct usb_descriptor_header *) &ecm_desc,
|
|
|
|
/* NOTE: status endpoint might need to be removed */
|
|
(struct usb_descriptor_header *) &ss_ecm_notify_desc,
|
|
(struct usb_descriptor_header *) &ss_ecm_intr_comp_desc,
|
|
|
|
/* data interface, altsettings 0 and 1 */
|
|
(struct usb_descriptor_header *) &ecm_data_nop_intf,
|
|
(struct usb_descriptor_header *) &ecm_data_intf,
|
|
(struct usb_descriptor_header *) &ss_ecm_in_desc,
|
|
(struct usb_descriptor_header *) &ss_ecm_bulk_comp_desc,
|
|
(struct usb_descriptor_header *) &ss_ecm_out_desc,
|
|
(struct usb_descriptor_header *) &ss_ecm_bulk_comp_desc,
|
|
NULL,
|
|
};
|
|
|
|
/* string descriptors: */
|
|
|
|
static struct usb_string ecm_string_defs[] = {
|
|
[0].s = "CDC Ethernet Control Model (ECM)",
|
|
[1].s = NULL /* DYNAMIC */,
|
|
[2].s = "CDC Ethernet Data",
|
|
[3].s = "CDC ECM",
|
|
{ } /* end of list */
|
|
};
|
|
|
|
static struct usb_gadget_strings ecm_string_table = {
|
|
.language = 0x0409, /* en-us */
|
|
.strings = ecm_string_defs,
|
|
};
|
|
|
|
static struct usb_gadget_strings *ecm_strings[] = {
|
|
&ecm_string_table,
|
|
NULL,
|
|
};
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
static void ecm_do_notify(struct f_ecm *ecm)
|
|
{
|
|
struct usb_request *req = ecm->notify_req;
|
|
struct usb_cdc_notification *event;
|
|
struct usb_composite_dev *cdev = ecm->port.func.config->cdev;
|
|
__le32 *data;
|
|
int status;
|
|
|
|
/* notification already in flight? */
|
|
if (!req)
|
|
return;
|
|
|
|
event = req->buf;
|
|
switch (ecm->notify_state) {
|
|
case ECM_NOTIFY_NONE:
|
|
return;
|
|
|
|
case ECM_NOTIFY_CONNECT:
|
|
event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
|
|
if (ecm->is_open)
|
|
event->wValue = cpu_to_le16(1);
|
|
else
|
|
event->wValue = cpu_to_le16(0);
|
|
event->wLength = 0;
|
|
req->length = sizeof *event;
|
|
|
|
DBG(cdev, "notify connect %s\n",
|
|
ecm->is_open ? "true" : "false");
|
|
ecm->notify_state = ECM_NOTIFY_SPEED;
|
|
break;
|
|
|
|
case ECM_NOTIFY_SPEED:
|
|
event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
|
|
event->wValue = cpu_to_le16(0);
|
|
event->wLength = cpu_to_le16(8);
|
|
req->length = ECM_STATUS_BYTECOUNT;
|
|
|
|
/* SPEED_CHANGE data is up/down speeds in bits/sec */
|
|
data = req->buf + sizeof *event;
|
|
data[0] = cpu_to_le32(ecm_bitrate(cdev->gadget));
|
|
data[1] = data[0];
|
|
|
|
DBG(cdev, "notify speed %d\n", ecm_bitrate(cdev->gadget));
|
|
ecm->notify_state = ECM_NOTIFY_NONE;
|
|
break;
|
|
}
|
|
event->bmRequestType = 0xA1;
|
|
event->wIndex = cpu_to_le16(ecm->ctrl_id);
|
|
|
|
ecm->notify_req = NULL;
|
|
status = usb_ep_queue(ecm->notify, req, GFP_ATOMIC);
|
|
if (status < 0) {
|
|
ecm->notify_req = req;
|
|
DBG(cdev, "notify --> %d\n", status);
|
|
}
|
|
}
|
|
|
|
static void ecm_notify(struct f_ecm *ecm)
|
|
{
|
|
/* NOTE on most versions of Linux, host side cdc-ethernet
|
|
* won't listen for notifications until its netdevice opens.
|
|
* The first notification then sits in the FIFO for a long
|
|
* time, and the second one is queued.
|
|
*/
|
|
ecm->notify_state = ECM_NOTIFY_CONNECT;
|
|
ecm_do_notify(ecm);
|
|
}
|
|
|
|
static void ecm_notify_complete(struct usb_ep *ep, struct usb_request *req)
|
|
{
|
|
struct f_ecm *ecm = req->context;
|
|
struct usb_composite_dev *cdev = ecm->port.func.config->cdev;
|
|
struct usb_cdc_notification *event = req->buf;
|
|
|
|
switch (req->status) {
|
|
case 0:
|
|
/* no fault */
|
|
break;
|
|
case -ECONNRESET:
|
|
case -ESHUTDOWN:
|
|
ecm->notify_state = ECM_NOTIFY_NONE;
|
|
break;
|
|
default:
|
|
DBG(cdev, "event %02x --> %d\n",
|
|
event->bNotificationType, req->status);
|
|
break;
|
|
}
|
|
ecm->notify_req = req;
|
|
ecm_do_notify(ecm);
|
|
}
|
|
|
|
static int ecm_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl)
|
|
{
|
|
struct f_ecm *ecm = func_to_ecm(f);
|
|
struct usb_composite_dev *cdev = f->config->cdev;
|
|
struct usb_request *req = cdev->req;
|
|
int value = -EOPNOTSUPP;
|
|
u16 w_index = le16_to_cpu(ctrl->wIndex);
|
|
u16 w_value = le16_to_cpu(ctrl->wValue);
|
|
u16 w_length = le16_to_cpu(ctrl->wLength);
|
|
|
|
/* composite driver infrastructure handles everything except
|
|
* CDC class messages; interface activation uses set_alt().
|
|
*/
|
|
switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
|
|
case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8)
|
|
| USB_CDC_SET_ETHERNET_PACKET_FILTER:
|
|
/* see 6.2.30: no data, wIndex = interface,
|
|
* wValue = packet filter bitmap
|
|
*/
|
|
if (w_length != 0 || w_index != ecm->ctrl_id)
|
|
goto invalid;
|
|
DBG(cdev, "packet filter %02x\n", w_value);
|
|
/* REVISIT locking of cdc_filter. This assumes the UDC
|
|
* driver won't have a concurrent packet TX irq running on
|
|
* another CPU; or that if it does, this write is atomic...
|
|
*/
|
|
ecm->port.cdc_filter = w_value;
|
|
value = 0;
|
|
break;
|
|
|
|
/* and optionally:
|
|
* case USB_CDC_SEND_ENCAPSULATED_COMMAND:
|
|
* case USB_CDC_GET_ENCAPSULATED_RESPONSE:
|
|
* case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
|
|
* case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
|
|
* case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
|
|
* case USB_CDC_GET_ETHERNET_STATISTIC:
|
|
*/
|
|
|
|
default:
|
|
invalid:
|
|
DBG(cdev, "invalid control req%02x.%02x v%04x i%04x l%d\n",
|
|
ctrl->bRequestType, ctrl->bRequest,
|
|
w_value, w_index, w_length);
|
|
}
|
|
|
|
/* respond with data transfer or status phase? */
|
|
if (value >= 0) {
|
|
DBG(cdev, "ecm req%02x.%02x v%04x i%04x l%d\n",
|
|
ctrl->bRequestType, ctrl->bRequest,
|
|
w_value, w_index, w_length);
|
|
req->zero = 0;
|
|
req->length = value;
|
|
value = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
|
|
if (value < 0)
|
|
ERROR(cdev, "ecm req %02x.%02x response err %d\n",
|
|
ctrl->bRequestType, ctrl->bRequest,
|
|
value);
|
|
}
|
|
|
|
/* device either stalls (value < 0) or reports success */
|
|
return value;
|
|
}
|
|
|
|
|
|
static int ecm_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
|
|
{
|
|
struct f_ecm *ecm = func_to_ecm(f);
|
|
struct usb_composite_dev *cdev = f->config->cdev;
|
|
|
|
/* Control interface has only altsetting 0 */
|
|
if (intf == ecm->ctrl_id) {
|
|
if (alt != 0)
|
|
goto fail;
|
|
|
|
if (ecm->notify->driver_data) {
|
|
VDBG(cdev, "reset ecm control %d\n", intf);
|
|
usb_ep_disable(ecm->notify);
|
|
}
|
|
if (!(ecm->notify->desc)) {
|
|
VDBG(cdev, "init ecm ctrl %d\n", intf);
|
|
if (config_ep_by_speed(cdev->gadget, f, ecm->notify))
|
|
goto fail;
|
|
}
|
|
usb_ep_enable(ecm->notify);
|
|
ecm->notify->driver_data = ecm;
|
|
|
|
/* Data interface has two altsettings, 0 and 1 */
|
|
} else if (intf == ecm->data_id) {
|
|
if (alt > 1)
|
|
goto fail;
|
|
|
|
if (ecm->port.in_ep->driver_data) {
|
|
DBG(cdev, "reset ecm\n");
|
|
gether_disconnect(&ecm->port);
|
|
}
|
|
|
|
if (!ecm->port.in_ep->desc ||
|
|
!ecm->port.out_ep->desc) {
|
|
DBG(cdev, "init ecm\n");
|
|
if (config_ep_by_speed(cdev->gadget, f,
|
|
ecm->port.in_ep) ||
|
|
config_ep_by_speed(cdev->gadget, f,
|
|
ecm->port.out_ep)) {
|
|
ecm->port.in_ep->desc = NULL;
|
|
ecm->port.out_ep->desc = NULL;
|
|
goto fail;
|
|
}
|
|
}
|
|
|
|
/* CDC Ethernet only sends data in non-default altsettings.
|
|
* Changing altsettings resets filters, statistics, etc.
|
|
*/
|
|
if (alt == 1) {
|
|
struct net_device *net;
|
|
|
|
/* Enable zlps by default for ECM conformance;
|
|
* override for musb_hdrc (avoids txdma ovhead).
|
|
*/
|
|
ecm->port.is_zlp_ok = !(gadget_is_musbhdrc(cdev->gadget)
|
|
);
|
|
ecm->port.cdc_filter = DEFAULT_FILTER;
|
|
DBG(cdev, "activate ecm\n");
|
|
net = gether_connect(&ecm->port);
|
|
if (IS_ERR(net))
|
|
return PTR_ERR(net);
|
|
}
|
|
|
|
/* NOTE this can be a minor disagreement with the ECM spec,
|
|
* which says speed notifications will "always" follow
|
|
* connection notifications. But we allow one connect to
|
|
* follow another (if the first is in flight), and instead
|
|
* just guarantee that a speed notification is always sent.
|
|
*/
|
|
ecm_notify(ecm);
|
|
} else
|
|
goto fail;
|
|
|
|
return 0;
|
|
fail:
|
|
return -EINVAL;
|
|
}
|
|
|
|
/* Because the data interface supports multiple altsettings,
|
|
* this ECM function *MUST* implement a get_alt() method.
|
|
*/
|
|
static int ecm_get_alt(struct usb_function *f, unsigned intf)
|
|
{
|
|
struct f_ecm *ecm = func_to_ecm(f);
|
|
|
|
if (intf == ecm->ctrl_id)
|
|
return 0;
|
|
return ecm->port.in_ep->driver_data ? 1 : 0;
|
|
}
|
|
|
|
static void ecm_disable(struct usb_function *f)
|
|
{
|
|
struct f_ecm *ecm = func_to_ecm(f);
|
|
struct usb_composite_dev *cdev = f->config->cdev;
|
|
|
|
DBG(cdev, "ecm deactivated\n");
|
|
|
|
if (ecm->port.in_ep->driver_data)
|
|
gether_disconnect(&ecm->port);
|
|
|
|
if (ecm->notify->driver_data) {
|
|
usb_ep_disable(ecm->notify);
|
|
ecm->notify->driver_data = NULL;
|
|
ecm->notify->desc = NULL;
|
|
}
|
|
}
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
/*
|
|
* Callbacks let us notify the host about connect/disconnect when the
|
|
* net device is opened or closed.
|
|
*
|
|
* For testing, note that link states on this side include both opened
|
|
* and closed variants of:
|
|
*
|
|
* - disconnected/unconfigured
|
|
* - configured but inactive (data alt 0)
|
|
* - configured and active (data alt 1)
|
|
*
|
|
* Each needs to be tested with unplug, rmmod, SET_CONFIGURATION, and
|
|
* SET_INTERFACE (altsetting). Remember also that "configured" doesn't
|
|
* imply the host is actually polling the notification endpoint, and
|
|
* likewise that "active" doesn't imply it's actually using the data
|
|
* endpoints for traffic.
|
|
*/
|
|
|
|
static void ecm_open(struct gether *geth)
|
|
{
|
|
struct f_ecm *ecm = func_to_ecm(&geth->func);
|
|
|
|
DBG(ecm->port.func.config->cdev, "%s\n", __func__);
|
|
|
|
ecm->is_open = true;
|
|
ecm_notify(ecm);
|
|
}
|
|
|
|
static void ecm_close(struct gether *geth)
|
|
{
|
|
struct f_ecm *ecm = func_to_ecm(&geth->func);
|
|
|
|
DBG(ecm->port.func.config->cdev, "%s\n", __func__);
|
|
|
|
ecm->is_open = false;
|
|
ecm_notify(ecm);
|
|
}
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
/* ethernet function driver setup/binding */
|
|
|
|
static int
|
|
ecm_bind(struct usb_configuration *c, struct usb_function *f)
|
|
{
|
|
struct usb_composite_dev *cdev = c->cdev;
|
|
struct f_ecm *ecm = func_to_ecm(f);
|
|
int status;
|
|
struct usb_ep *ep;
|
|
|
|
/* allocate instance-specific interface IDs */
|
|
status = usb_interface_id(c, f);
|
|
if (status < 0)
|
|
goto fail;
|
|
ecm->ctrl_id = status;
|
|
ecm_iad_descriptor.bFirstInterface = status;
|
|
|
|
ecm_control_intf.bInterfaceNumber = status;
|
|
ecm_union_desc.bMasterInterface0 = status;
|
|
|
|
status = usb_interface_id(c, f);
|
|
if (status < 0)
|
|
goto fail;
|
|
ecm->data_id = status;
|
|
|
|
ecm_data_nop_intf.bInterfaceNumber = status;
|
|
ecm_data_intf.bInterfaceNumber = status;
|
|
ecm_union_desc.bSlaveInterface0 = status;
|
|
|
|
status = -ENODEV;
|
|
|
|
/* allocate instance-specific endpoints */
|
|
ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_in_desc);
|
|
if (!ep)
|
|
goto fail;
|
|
ecm->port.in_ep = ep;
|
|
ep->driver_data = cdev; /* claim */
|
|
|
|
ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_out_desc);
|
|
if (!ep)
|
|
goto fail;
|
|
ecm->port.out_ep = ep;
|
|
ep->driver_data = cdev; /* claim */
|
|
|
|
/* NOTE: a status/notification endpoint is *OPTIONAL* but we
|
|
* don't treat it that way. It's simpler, and some newer CDC
|
|
* profiles (wireless handsets) no longer treat it as optional.
|
|
*/
|
|
ep = usb_ep_autoconfig(cdev->gadget, &fs_ecm_notify_desc);
|
|
if (!ep)
|
|
goto fail;
|
|
ecm->notify = ep;
|
|
ep->driver_data = cdev; /* claim */
|
|
|
|
status = -ENOMEM;
|
|
|
|
/* allocate notification request and buffer */
|
|
ecm->notify_req = usb_ep_alloc_request(ep, GFP_KERNEL);
|
|
if (!ecm->notify_req)
|
|
goto fail;
|
|
ecm->notify_req->buf = kmalloc(ECM_STATUS_BYTECOUNT, GFP_KERNEL);
|
|
if (!ecm->notify_req->buf)
|
|
goto fail;
|
|
ecm->notify_req->context = ecm;
|
|
ecm->notify_req->complete = ecm_notify_complete;
|
|
|
|
/* support all relevant hardware speeds... we expect that when
|
|
* hardware is dual speed, all bulk-capable endpoints work at
|
|
* both speeds
|
|
*/
|
|
hs_ecm_in_desc.bEndpointAddress = fs_ecm_in_desc.bEndpointAddress;
|
|
hs_ecm_out_desc.bEndpointAddress = fs_ecm_out_desc.bEndpointAddress;
|
|
hs_ecm_notify_desc.bEndpointAddress =
|
|
fs_ecm_notify_desc.bEndpointAddress;
|
|
|
|
ss_ecm_in_desc.bEndpointAddress = fs_ecm_in_desc.bEndpointAddress;
|
|
ss_ecm_out_desc.bEndpointAddress = fs_ecm_out_desc.bEndpointAddress;
|
|
ss_ecm_notify_desc.bEndpointAddress =
|
|
fs_ecm_notify_desc.bEndpointAddress;
|
|
|
|
status = usb_assign_descriptors(f, ecm_fs_function, ecm_hs_function,
|
|
ecm_ss_function);
|
|
if (status)
|
|
goto fail;
|
|
|
|
/* NOTE: all that is done without knowing or caring about
|
|
* the network link ... which is unavailable to this code
|
|
* until we're activated via set_alt().
|
|
*/
|
|
|
|
ecm->port.open = ecm_open;
|
|
ecm->port.close = ecm_close;
|
|
|
|
DBG(cdev, "CDC Ethernet: %s speed IN/%s OUT/%s NOTIFY/%s\n",
|
|
gadget_is_superspeed(c->cdev->gadget) ? "super" :
|
|
gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
|
|
ecm->port.in_ep->name, ecm->port.out_ep->name,
|
|
ecm->notify->name);
|
|
return 0;
|
|
|
|
fail:
|
|
if (ecm->notify_req) {
|
|
kfree(ecm->notify_req->buf);
|
|
usb_ep_free_request(ecm->notify, ecm->notify_req);
|
|
}
|
|
|
|
/* we might as well release our claims on endpoints */
|
|
if (ecm->notify)
|
|
ecm->notify->driver_data = NULL;
|
|
if (ecm->port.out_ep)
|
|
ecm->port.out_ep->driver_data = NULL;
|
|
if (ecm->port.in_ep)
|
|
ecm->port.in_ep->driver_data = NULL;
|
|
|
|
ERROR(cdev, "%s: can't bind, err %d\n", f->name, status);
|
|
|
|
return status;
|
|
}
|
|
|
|
static void
|
|
ecm_unbind(struct usb_configuration *c, struct usb_function *f)
|
|
{
|
|
struct f_ecm *ecm = func_to_ecm(f);
|
|
|
|
DBG(c->cdev, "ecm unbind\n");
|
|
|
|
usb_free_all_descriptors(f);
|
|
|
|
kfree(ecm->notify_req->buf);
|
|
usb_ep_free_request(ecm->notify, ecm->notify_req);
|
|
|
|
ecm_string_defs[1].s = NULL;
|
|
kfree(ecm);
|
|
}
|
|
|
|
/**
|
|
* ecm_bind_config - add CDC Ethernet network link to a configuration
|
|
* @c: the configuration to support the network link
|
|
* @ethaddr: a buffer in which the ethernet address of the host side
|
|
* side of the link was recorded
|
|
* Context: single threaded during gadget setup
|
|
*
|
|
* Returns zero on success, else negative errno.
|
|
*
|
|
* Caller must have called @gether_setup(). Caller is also responsible
|
|
* for calling @gether_cleanup() before module unload.
|
|
*/
|
|
int
|
|
ecm_bind_config(struct usb_configuration *c, u8 ethaddr[ETH_ALEN])
|
|
{
|
|
struct f_ecm *ecm;
|
|
int status;
|
|
|
|
if (!can_support_ecm(c->cdev->gadget) || !ethaddr)
|
|
return -EINVAL;
|
|
|
|
/* maybe allocate device-global string IDs */
|
|
if (ecm_string_defs[0].id == 0) {
|
|
|
|
/* control interface label */
|
|
status = usb_string_id(c->cdev);
|
|
if (status < 0)
|
|
return status;
|
|
ecm_string_defs[0].id = status;
|
|
ecm_control_intf.iInterface = status;
|
|
|
|
/* data interface label */
|
|
status = usb_string_id(c->cdev);
|
|
if (status < 0)
|
|
return status;
|
|
ecm_string_defs[2].id = status;
|
|
ecm_data_intf.iInterface = status;
|
|
|
|
/* MAC address */
|
|
status = usb_string_id(c->cdev);
|
|
if (status < 0)
|
|
return status;
|
|
ecm_string_defs[1].id = status;
|
|
ecm_desc.iMACAddress = status;
|
|
|
|
/* IAD label */
|
|
status = usb_string_id(c->cdev);
|
|
if (status < 0)
|
|
return status;
|
|
ecm_string_defs[3].id = status;
|
|
ecm_iad_descriptor.iFunction = status;
|
|
}
|
|
|
|
/* allocate and initialize one new instance */
|
|
ecm = kzalloc(sizeof *ecm, GFP_KERNEL);
|
|
if (!ecm)
|
|
return -ENOMEM;
|
|
|
|
/* export host's Ethernet address in CDC format */
|
|
snprintf(ecm->ethaddr, sizeof ecm->ethaddr,
|
|
"%02X%02X%02X%02X%02X%02X",
|
|
ethaddr[0], ethaddr[1], ethaddr[2],
|
|
ethaddr[3], ethaddr[4], ethaddr[5]);
|
|
ecm_string_defs[1].s = ecm->ethaddr;
|
|
|
|
ecm->port.cdc_filter = DEFAULT_FILTER;
|
|
|
|
ecm->port.func.name = "cdc_ethernet";
|
|
ecm->port.func.strings = ecm_strings;
|
|
/* descriptors are per-instance copies */
|
|
ecm->port.func.bind = ecm_bind;
|
|
ecm->port.func.unbind = ecm_unbind;
|
|
ecm->port.func.set_alt = ecm_set_alt;
|
|
ecm->port.func.get_alt = ecm_get_alt;
|
|
ecm->port.func.setup = ecm_setup;
|
|
ecm->port.func.disable = ecm_disable;
|
|
|
|
status = usb_add_function(c, &ecm->port.func);
|
|
if (status) {
|
|
ecm_string_defs[1].s = NULL;
|
|
kfree(ecm);
|
|
}
|
|
return status;
|
|
}
|