From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (NAM11-DM6-obe.outbound.protection.outlook.com [40.107.223.78]) by mx.groups.io with SMTP id smtpd.web10.8074.1678348313207064673 for ; Wed, 08 Mar 2023 23:51:54 -0800 Authentication-Results: mx.groups.io; dkim=fail, err=mail: missing word in phrase: charset not supported: "iso-2022-jp"; spf=pass (domain: ami.com, ip: 40.107.223.78, mailfrom: richardho@ami.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Vjo9xnv8b1OpzzgJLXBXA3JmdtnuxKNGmDq1FXx2/DsFjT1PLWkolJZ2yPNjpeNapXRHOV7nF+ChKRnKW8lPSi/IfhSc+qCNDP9oJwl7Ipb8ZIyhn7IB7dLEbpAsWQ2PItAHYhziQeuAXQGbUEUfqpIZNJGHSaFHbacdaPwlyGJ9ex8Mr8nv3i2mUA0ASLaYLbSxawRIyc2sDTjgSaWmlO96273Jcih2xl4VZ4l6lXjrcLh5l18IZhUUcYQcNmiovGy65GUyHCsGjS8HNA8nU3rGubOelDCxyep1Sv815xUzIsc5J00PjLK76HfErCcsytH0tHgmZNskCguip6Szug== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=nlArPvHgz63/CJZ1ipcO0yOY0loteK5wMyJqps9SuQg=; b=agqEJecnsj6w6GVEv/FwpYZwG8Tnl3DPqZ3qHNySXRVmEkEAaXQcyXbRa2JBgNduv5CVbbyp2aTREa5ObmQF7BQK00Veg9UZ0NIsP3Ybl6JYL0HVYtGtEyja08NRdM/XzJP+meSoJni6rSRsZPcvcVwiQt0ieOZtuYZY1jvQuIioChNqhw8vsYa2rBtbouhsPME19NRZJ/Zm4q/Fq6Lpo9SDDYlduItZjUIcR7aFdGPW4ONhHKN6fn2b00y3KRcFkkn9canfH8Md3wGK62tew4PS8gfFcX6dGsAGnC3viGGbzyxxUUFNMdhG+Wu7KmYL38IUd4aw7ebrCdZ8a9NaDA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=ami.com; dmarc=pass action=none header.from=ami.com; dkim=pass header.d=ami.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ami.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=nlArPvHgz63/CJZ1ipcO0yOY0loteK5wMyJqps9SuQg=; b=FC7MN+vzpf4qXZhYL0hOBZy/rUh8uQTmU6vNOmYfMNkpESa/HN4xdbGjYpW600kR7WSCgdM+HsneJWQLcVSRFuGmKJW3dZ3rVHMTfJF0tW93/wCCQfTY0ylmyEofIGQHetjQO1RmWJ5WxYzqBTX+aNIwdd50c1kvnuqRLFwwmek= Received: from CY8PR10MB6441.namprd10.prod.outlook.com (2603:10b6:930:63::16) by IA1PR10MB6759.namprd10.prod.outlook.com (2603:10b6:208:42d::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6178.19; Thu, 9 Mar 2023 07:51:50 +0000 Received: from CY8PR10MB6441.namprd10.prod.outlook.com ([fe80::7b59:85c6:a3d0:1921]) by CY8PR10MB6441.namprd10.prod.outlook.com ([fe80::7b59:85c6:a3d0:1921%4]) with mapi id 15.20.6156.018; Thu, 9 Mar 2023 07:51:50 +0000 From: =?UTF-8?B?UmljaGFyZEhvIFvkvZXmmI7lv6Bd?= To: "devel@edk2.groups.io" CC: Andrew Fish , Leif Lindholm , Michael D Kinney , Michael Kubacki , Zhiguang Liu , Liming Gao , Rebecca Cran , Tinh Nguyen , =?iso-2022-jp?B?VG9ueSBMbyAoGyRCTWU2Yj4+GyhCKQ==?= Subject: [PATCH v6 3/3] UsbNetworkPkg/UsbCdcNcm: Add USB Cdc NCM devices support Thread-Topic: [PATCH v6 3/3] UsbNetworkPkg/UsbCdcNcm: Add USB Cdc NCM devices support Thread-Index: AQHZUlwBmN/i/N/2MUeJeUwPB8NetA== Date: Thu, 9 Mar 2023 07:51:50 +0000 Message-ID: <20230309075134.864-3-richardho@ami.com> References: <20230309075134.864-1-richardho@ami.com> In-Reply-To: <20230309075134.864-1-richardho@ami.com> Accept-Language: zh-TW, en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-mailer: git-send-email 2.35.1.windows.2 authentication-results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=ami.com; x-ms-publictraffictype: Email x-ms-traffictypediagnostic: CY8PR10MB6441:EE_|IA1PR10MB6759:EE_ x-ms-office365-filtering-correlation-id: 79d5cb26-f464-4752-a48f-08db20732467 x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: p6mJqLgMtqzji7wjtwuu2dCKnYjbav9ZY2XrmguiYwXCzPLXD0AZ9x0nf/cbMiMPtFufy0Z8+5tWyjMRemMwAuEBSoPRUHdho15XpYtS0JGsuxR2KE4LR3wvYm0MjrHCA1LC9l7Y4ifydIDh55Au3ysP7vGCncspUe5//hhZaJryjqdj/FL44fkAmMEGViTNq536D6fyfWeiAPtGv3RGY/b1tqk4D5vGWI5iLB3P3hpwo8B2v50/I5ycZXyHi1KR58we0CRloxupD8aEmGD+mAeZmzPZWKbCkmi/w8SJjjJunbBkMjOtshMxTQZVxJ36qJ/7l4q2LFYKfZq+/5RF0amApCxPs9j9Zx8cJjUFYYEsrySaWAUYD4E20DlXdV2Nm/hK1xBDlRKOUuLG8rFD6fRkGWW9XbOqBc9R6rA4G+B0jPqIs1c4UjFaqtA2H/dDH3V24c/JWVuBb3K7BeCJsduGNBBa/owqViZxORKgLt0lGtA49jmZFipOhkrRbhlIPf9Sq1jiIr8RURMB37Wa5/QkUwyYB7fMTfah3v6rzlS5Ux3QtXvr9I8dIor4W6EK6v6HaqQAa4Jmz+FOhbSR4voJ/4XS21DM4nWeJtyy/aiZWRQItb5JlqcqOdaikoMqBKOeQxcBKVcyw7GIhje3Fdv5S0t2jslPx3B/a3Q5xAgSurC6YfmYMRonYICQ1JXTfqEMVJPX5OnTT0NrKUhl1sn5YFXzb2LngIJtRnfSV+Mi3J7lJPWbo0gZrm9hGOtLf3zgbak3NVy1N+CM1Fu9eQ== x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CY8PR10MB6441.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230025)(4636009)(396003)(366004)(376002)(136003)(346002)(39850400004)(451199018)(30864003)(8936002)(36756003)(85182001)(26005)(5660300002)(1076003)(8676002)(6506007)(6512007)(38100700002)(122000001)(83380400001)(186003)(2616005)(54906003)(19627235002)(86362001)(91956017)(316002)(41300700001)(6916009)(64756008)(4326008)(66556008)(66446008)(66476007)(76116006)(40140700001)(6486002)(71200400001)(478600001)(38070700005)(66946007)(2906002)(579004)(559001)(44824005);DIR:OUT;SFP:1101; x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?iso-2022-jp?B?WDhsaVlTNUxDNXBGNml0Z1lGTVhzUzlSbmJEK1FUSVRrMWs3cnRhemRy?= =?iso-2022-jp?B?SDRDRnY2SWtQVXFWdmpMZjJ4dENSdGNtYm85V0NyKzVWRGovNjA5RUMr?= =?iso-2022-jp?B?ZEo2bU5tTVRhRXdaVlIzeXd1RUVYSVNHZEUzd2xkK0daZCswckhTRGs5?= =?iso-2022-jp?B?L1UybllxL0lwTEtWTFhQUUduVVltWnZrUlZrNE1FczZGTTdRRDNReXgz?= =?iso-2022-jp?B?UXRWVW42N3NIcUY3dWcvTEcwNUR2MkMyMFVzWXpkRXNLZG1tYWtzZlF3?= =?iso-2022-jp?B?dWdvZVNyVmlleEIySWV5aEx1S0hyZHd6T3NVdTF1bkxQQktJMVFISEpP?= =?iso-2022-jp?B?SHM2U3ppVU1DcGJWeWlJZWxDUmJ5TGxFYUdWc2loL2YvUVczUGc3UFFR?= =?iso-2022-jp?B?bVRzallIamp2NXI4TTZKTlhIQlRia1Brc1JqSkpSdHptK3RMT3lUMTZi?= =?iso-2022-jp?B?MTVHNU1UQ3hsUkgxRTR6bUtITExtOHUvMjBMVllFWTlBVXl6TkJ1MGEy?= =?iso-2022-jp?B?MmhVRndFcEI0bnhvcktDVDRmOUJxV2RmUisyTWR0MlREZGlmQ3pvYWVD?= =?iso-2022-jp?B?NXpmSUVRS3lNSkUrRWh3STQvdjA5MjZGOFJZOVY1bFpmMlJBVFJBc05t?= =?iso-2022-jp?B?UERZTWVrVjJLR1hDbnhwWnRTTGVvS20wTmRGeGh4MXgxS3FseHYrRlEx?= =?iso-2022-jp?B?WFg5VkJobjhKVEJ2cXZHZ1NYSGwzS0RwZnZZaGRvb1hWQ1hIWUFHQnRL?= =?iso-2022-jp?B?S1ZhVjVYZW44OTRnb2trRGZQVVEvaEREOGJBWmN6Vm5VMU1OR014eTI4?= =?iso-2022-jp?B?c1ZHNFRqYjF0d0pZMDVQZzJqclhDdVgzQ1BYMEpCb1V1ODZRaVV1VEhX?= =?iso-2022-jp?B?NHlHRm93V2pZR3JJSXVnVUZlT2I5UGxKNzBzUXNmL1ZYOTJrb1VQV05E?= =?iso-2022-jp?B?bnN1em5UMXVIVUJGWW5rcFgreVh0U1gvUmdUZ2syLzlhRG1CREVjbUFC?= =?iso-2022-jp?B?UWxoWmhJTkpZaS9xNjdZUGVLNE1XTFBmU2x0cExrMURLTzg3dXZIdG03?= =?iso-2022-jp?B?M0daOWVZUnhOaUdWcTh1dVJmZ1hBcEZqTWlLQXQzdDQwanlNZytvRmln?= =?iso-2022-jp?B?QTNBVVJPZ3hybXo3UnkrZktkY3R0TXcwVmRFdElQbTRCOUJHL0h5UFFW?= =?iso-2022-jp?B?TkR0bW9RVFVBNVJBcWttZUJQY09JSWh1K3VtUVV6UG1zMFJhTFBLeE10?= =?iso-2022-jp?B?YUlBa1ZYOGxaRjdhRlpIbTBRNjYrczR3VFNlL2lJVlBGaXFoZUVYUWpj?= =?iso-2022-jp?B?TG53a3I1M2ZXQ3hVM003NGdOU2JiSjFNSGIySXlTYUl1TGorY3VyV1VZ?= =?iso-2022-jp?B?V1NzUE1kYm43Uk1NVzloQ3NGcG5hSW82bExmRWJ2MXpxenU2STk3ZW1R?= =?iso-2022-jp?B?eHNCZE55cDFUSEZHU2grVWtkd2tSNG1aRDBGM1VFRlQ3cE94Z0tYa2FS?= =?iso-2022-jp?B?Ri8wVUdxMDc3clJvalVUNmw5R1IvNGRMS2R1eUc5RjdpRHg0K1RSenFF?= =?iso-2022-jp?B?cTZWQ0JwUmFPQ0ZoVFo4RnhXRDRteTJpUTVscDQxQTFQVjJjbHBEa0th?= =?iso-2022-jp?B?QTdzTVFRdFZSQ1htUktIbC8zK0kvSWdFYUFYdlMvZmJKUU5UcENGRlI2?= =?iso-2022-jp?B?Yll3SDNVQzJmT1MzLzBaRFVMbFZIWWZPbStmRTh1UERRampyR0s3dWFp?= =?iso-2022-jp?B?TDg3ZEZuSlAwak9WT0dyb01HSytnN3Q4bkxFRkdhZXhKOXJwdHIyR3Q1?= =?iso-2022-jp?B?NGdNK0kzWWhoM0s4K1ZMaUVkcERBNU81UlRsNFJXNU1PcndSVHhJdDc0?= =?iso-2022-jp?B?cExIbUVxeUNvQ3VoWVdzdG1DaVRLMjc4Z3pIWjM3TFJLQVNDTVdtckxV?= =?iso-2022-jp?B?dHMvd1FHb1o5a0ZQT1hNUHEzdDhOSFZKQzFWZkN4ZE1xelBMRFMzaGhn?= =?iso-2022-jp?B?bGZRSGZRT1daMFplVU5UdWdhUGxZZXhrRmEvOVlVazNxM2o5WWNxcTli?= =?iso-2022-jp?B?MDZlQXZWRjVZbFpOb2Vkc1Q4ODZ4Mng1cCt2L3UwYUVJUHZGUWplZDBL?= =?iso-2022-jp?B?N2U5aDRNdVZNdXJCQ1NIUjFpaCtTTGdNbWFlVVJvRUlGQ0dGN0QvOWRQ?= =?iso-2022-jp?B?RHlsamNVYW9qckx5VHBQcEM2VE1PZ3RqQmF5QnYvNGg4OGpoNUR6dGtH?= =?iso-2022-jp?B?UFNrYndEMFk5L2pqa0k1Ym1RSFAvbVlXRHRrdzAwMEYwVTdubnNjNUpO?= =?iso-2022-jp?B?UktSKw==?= MIME-Version: 1.0 X-OriginatorOrg: ami.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: CY8PR10MB6441.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 79d5cb26-f464-4752-a48f-08db20732467 X-MS-Exchange-CrossTenant-originalarrivaltime: 09 Mar 2023 07:51:50.6688 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 27e97857-e15f-486c-b58e-86c2b3040f93 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: nxiZOsEJ/bSTXhPF8EH+H27ZJ8MSxFC5h75lhy40fEdJuEsGy6FK1CGT+Abo+QWO/ME7rJMYcvosmJ7tjfYm1g== X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR10MB6759 Content-Language: en-US Content-Type: text/plain; charset="iso-2022-jp" Content-Transfer-Encoding: quoted-printable This driver provides UEFI driver for USB CDC NCM device Signed-off-by: Richard Ho Cc: Andrew Fish Cc: Leif Lindholm Cc: Michael D Kinney Cc: Michael Kubacki Cc: Zhiguang Liu Cc: Liming Gao Cc: Rebecca Cran Tested-by: Tinh Nguyen Reviewed-by: Tony Lo --- UsbNetworkPkg/UsbCdcNcm/UsbCdcNcm.inf | 42 + UsbNetworkPkg/UsbCdcNcm/UsbCdcNcm.h | 245 ++++++ UsbNetworkPkg/UsbCdcNcm/ComponentName.c | 170 ++++ UsbNetworkPkg/UsbCdcNcm/UsbCdcNcm.c | 506 ++++++++++++ UsbNetworkPkg/UsbCdcNcm/UsbNcmFunction.c | 966 +++++++++++++++++++++++ 5 files changed, 1929 insertions(+) create mode 100644 UsbNetworkPkg/UsbCdcNcm/UsbCdcNcm.inf create mode 100644 UsbNetworkPkg/UsbCdcNcm/UsbCdcNcm.h create mode 100644 UsbNetworkPkg/UsbCdcNcm/ComponentName.c create mode 100644 UsbNetworkPkg/UsbCdcNcm/UsbCdcNcm.c create mode 100644 UsbNetworkPkg/UsbCdcNcm/UsbNcmFunction.c diff --git a/UsbNetworkPkg/UsbCdcNcm/UsbCdcNcm.inf b/UsbNetworkPkg/UsbCdcNc= m/UsbCdcNcm.inf new file mode 100644 index 000000000000..bd085bdd0037 --- /dev/null +++ b/UsbNetworkPkg/UsbCdcNcm/UsbCdcNcm.inf @@ -0,0 +1,42 @@ +## @file +# This is Usb Cdc Ncm driver for DXE phase. +# +# Copyright (c) 2023, American Megatrends International LLC. All rights re= served.
+# SPDX-License-Identifier: BSD-2-Clause-Patent +## + +[Defines] + INF_VERSION =3D 0x00010005 + BASE_NAME =3D UsbCdcNcm + FILE_GUID =3D 52230d31-6c11-4442-b262-bec6bfe84efa + MODULE_TYPE =3D DXE_DRIVER + VERSION_STRING =3D 1.0 + ENTRY_POINT =3D UsbNcmEntry + +[Sources] + UsbCdcNcm.c + UsbCdcNcm.h + UsbNcmFunction.c + ComponentName.c + +[Packages] + MdePkg/MdePkg.dec + UsbNetworkPkg/UsbNetworkPkg.dec + +[LibraryClasses] + UefiDriverEntryPoint + UefiBootServicesTableLib + UefiLib + DebugLib + UefiUsbLib + MemoryAllocationLib + BaseMemoryLib + +[Protocols] + gEfiUsbIoProtocolGuid + gEfiDevicePathProtocolGuid + gEfiDriverBindingProtocolGuid + gEdkIIUsbEthProtocolGuid + +[Depex] + TRUE diff --git a/UsbNetworkPkg/UsbCdcNcm/UsbCdcNcm.h b/UsbNetworkPkg/UsbCdcNcm/= UsbCdcNcm.h new file mode 100644 index 000000000000..f8c21302f170 --- /dev/null +++ b/UsbNetworkPkg/UsbCdcNcm/UsbCdcNcm.h @@ -0,0 +1,245 @@ +/** @file + Header file for USB Network Control Model driver + + Copyright (c) 2023, American Megatrends International LLC. All rights re= served.
+ SPDX-License-Identifier: BSD-2-Clause-Patent +**/ + +#ifndef _USB_CDC_NCM_H_ +#define _USB_CDC_NCM_H_ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +typedef struct { + UINTN Signature; + EDKII_USB_ETHERNET_PROTOCOL UsbEth; + EFI_HANDLE UsbCdcDataHandle; + EFI_USB_IO_PROTOCOL *UsbIo; + EFI_USB_CONFIG_DESCRIPTOR *Config; + UINT8 NumOfInterface; + UINT8 BulkInEndpoint; + UINT8 BulkOutEndpoint; + UINT8 InterruptEndpoint; + EFI_MAC_ADDRESS MacAddress; + UINT16 BulkOutSequence; + UINT8 *BulkBuffer; + UINT8 TotalDatagram; + UINT8 NowDatagram; +} USB_ETHERNET_DRIVER; + +#define USB_NCM_DRIVER_VERSION 1 +#define USB_ETHERNET_BULK_TIMEOUT 1 +#define USB_ETHERNET_TRANSFER_TIMEOUT 200 +#define USB_NCM_MAX_NTB_SIZE 0xFFFF +#define USB_ETHERNET_FRAME_SIZE 0x5F2 // MAX ethernet frame size + +// Defined in USB NCM 1.0 spec., section 3.2 and 3.3 +#define USB_NCM_NTH_SIGN_16 0x484D434E +#define USB_NCM_NDP_SIGN_16 0x304D434E +#define USB_NCM_NDP_SIGN_16_CRC 0x314D434E +#define USB_NCM_NTH_LENGTH 0x000C +#define USB_NCM_NDP_LENGTH 0x0010// at least 16 + +// USB NCM Transfer header structure - UINT16 +typedef struct { + UINT32 Signature; + UINT16 HeaderLength; + UINT16 Sequence; + UINT16 BlockLength; + UINT16 NdpIndex; +} USB_NCM_TRANSFER_HEADER_16; + +// USB NCM Datagram pointer structure - UINT16 +typedef struct { + UINT32 Signature; + UINT16 Length; + UINT16 NextNdpIndex; +} USB_NCM_DATAGRAM_POINTER_16; + +// USB NCM Datagram structure +typedef struct { + UINT16 DatagramIndex; + UINT16 DatagramLength; +} USB_NCM_DATA_GRAM; + +#define USB_ETHERNET_SIGNATURE SIGNATURE_32('u', 'e', 't', 'h') +#define USB_ETHERNET_DEV_FROM_THIS(a) CR (a, USB_ETHERNET_DRIVER, UsbEth,= USB_ETHERNET_SIGNATURE) + +typedef struct { + UINT16 Src; + UINT16 Dst; +} BIT_MAP; + +extern EFI_COMPONENT_NAME2_PROTOCOL gUsbNcmComponentName2; + +EFI_STATUS +EFIAPI +UsbNcmDriverSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath + ); + +EFI_STATUS +EFIAPI +UsbNcmDriverStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath + ); + +EFI_STATUS +EFIAPI +UsbNcmDriverStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer + ); + +EFI_STATUS +LoadAllDescriptor ( + IN EFI_USB_IO_PROTOCOL *UsbIo, + OUT EFI_USB_CONFIG_DESCRIPTOR **ConfigDesc + ); + +BOOLEAN +NextDescriptor ( + IN EFI_USB_CONFIG_DESCRIPTOR *Desc, + IN OUT UINTN *Offset + ); + +EFI_STATUS +GetFunctionalDescriptor ( + IN EFI_USB_CONFIG_DESCRIPTOR *Config, + IN UINT8 FunDescriptorType, + OUT VOID *DataBuffer + ); + +VOID +GetEndpoint ( + IN EFI_USB_IO_PROTOCOL *UsbIo, + IN OUT USB_ETHERNET_DRIVER *UsbEthDriver + ); + +EFI_STATUS +EFIAPI +UsbEthNcmReceive ( + IN PXE_CDB *Cdb, + IN EDKII_USB_ETHERNET_PROTOCOL *This, + IN OUT VOID *Packet, + IN OUT UINTN *PacketLength + ); + +EFI_STATUS +EFIAPI +UsbEthNcmTransmit ( + IN PXE_CDB *Cdb, + IN EDKII_USB_ETHERNET_PROTOCOL *This, + IN VOID *Packet, + IN OUT UINTN *PacketLength + ); + +EFI_STATUS +EFIAPI +UsbEthNcmInterrupt ( + IN EDKII_USB_ETHERNET_PROTOCOL *This, + IN BOOLEAN IsNewTransfer, + IN UINTN PollingInterval, + IN EFI_USB_DEVICE_REQUEST *Request + ); + +EFI_STATUS +EFIAPI +InterruptCallback ( + IN VOID *Data, + IN UINTN DataLength, + IN VOID *Context, + IN UINT32 Status + ); + +EFI_STATUS +EFIAPI +GetUsbEthMacAddress ( + IN EDKII_USB_ETHERNET_PROTOCOL *This, + OUT EFI_MAC_ADDRESS *MacAddress + ); + +EFI_STATUS +EFIAPI +UsbEthNcmBulkSize ( + IN EDKII_USB_ETHERNET_PROTOCOL *This, + OUT UINTN *BulkSize + ); + +EFI_STATUS +EFIAPI +GetUsbHeaderFunDescriptor ( + IN EDKII_USB_ETHERNET_PROTOCOL *This, + OUT USB_HEADER_FUN_DESCRIPTOR *UsbHeaderFunDescriptor + ); + +EFI_STATUS +EFIAPI +GetUsbUnionFunDescriptor ( + IN EDKII_USB_ETHERNET_PROTOCOL *This, + OUT USB_UNION_FUN_DESCRIPTOR *UsbUnionFunDescriptor + ); + +EFI_STATUS +EFIAPI +GetUsbEthFunDescriptor ( + IN EDKII_USB_ETHERNET_PROTOCOL *This, + OUT USB_ETHERNET_FUN_DESCRIPTOR *UsbEthFunDescriptor + ); + +EFI_STATUS +EFIAPI +SetUsbEthMcastFilter ( + IN EDKII_USB_ETHERNET_PROTOCOL *This, + IN UINT16 Value, + IN VOID *McastAddr + ); + +EFI_STATUS +EFIAPI +SetUsbEthPowerFilter ( + IN EDKII_USB_ETHERNET_PROTOCOL *This, + IN UINT16 Value, + IN UINT16 Length, + IN VOID *PatternFilter + ); + +EFI_STATUS +EFIAPI +GetUsbEthPowerFilter ( + IN EDKII_USB_ETHERNET_PROTOCOL *This, + IN UINT16 Value, + OUT BOOLEAN *PatternActive + ); + +EFI_STATUS +EFIAPI +SetUsbEthPacketFilter ( + IN EDKII_USB_ETHERNET_PROTOCOL *This, + IN UINT16 Value + ); + +EFI_STATUS +EFIAPI +GetUsbEthStatistic ( + IN EDKII_USB_ETHERNET_PROTOCOL *This, + IN UINT16 FeatureSelector, + OUT VOID *Statistic + ); + +#endif diff --git a/UsbNetworkPkg/UsbCdcNcm/ComponentName.c b/UsbNetworkPkg/UsbCdc= Ncm/ComponentName.c new file mode 100644 index 000000000000..3cf3dc222df9 --- /dev/null +++ b/UsbNetworkPkg/UsbCdcNcm/ComponentName.c @@ -0,0 +1,170 @@ +/** @file + This file contains code for USB Ncm Driver Component Name definitions + + Copyright (c) 2023, American Megatrends International LLC. All rights re= served.
+ SPDX-License-Identifier: BSD-2-Clause-Patent +**/ +#include "UsbCdcNcm.h" + +GLOBAL_REMOVE_IF_UNREFERENCED EFI_UNICODE_STRING_TABLE gUsbNcmDriverNameT= able[] =3D { + { + "eng;en", + L"USB NCM Driver" + }, + { + NULL, + NULL + } +}; + +EFI_STATUS +EFIAPI +UsbNcmComponentNameGetDriverName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN CHAR8 *Language, + OUT CHAR16 **DriverName + ); + +EFI_STATUS +EFIAPI +UsbNcmComponentNameGetControllerName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN EFI_HANDLE Controller, + IN EFI_HANDLE ChildHandle OPTIONAL, + IN CHAR8 *Language, + OUT CHAR16 **ControllerName + ); + +GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME_PROTOCOL gUsbNcmComponen= tName =3D { + UsbNcmComponentNameGetDriverName, + UsbNcmComponentNameGetControllerName, + "eng" +}; + +GLOBAL_REMOVE_IF_UNREFERENCED EFI_COMPONENT_NAME2_PROTOCOL gUsbNcmCompone= ntName2 =3D { + (EFI_COMPONENT_NAME2_GET_DRIVER_NAME)UsbNcmComponentNameGetDriverName, + (EFI_COMPONENT_NAME2_GET_CONTROLLER_NAME)UsbNcmComponentNameGetControlle= rName, + "en" +}; + +/** + Retrieves a Unicode string that is the user readable name of the driver. + + This function retrieves the user readable name of a driver in the form o= f a + Unicode string. If the driver specified by This has a user readable name= in + the language specified by Language, then a pointer to the driver name is + returned in DriverName, and EFI_SUCCESS is returned. If the driver speci= fied + by This does not support the language specified by Language, + then EFI_UNSUPPORTED is returned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTO= COL or + EFI_COMPONENT_NAME_PROTOCOL instance. + @param[in] Language A pointer to a Null-terminated ASCII strin= g + array indicating the language. This is the + language of the driver name that the calle= r is + requesting, and it must match one of the + languages specified in SupportedLanguages.= The + number of languages supported by a driver = is up + to the driver writer. Language is specifie= d + in RFC 4646 or ISO 639-2 language code for= mat. + @param[out] DriverName A pointer to the Unicode string to return. + This Unicode string is the name of the + driver specified by This in the language + specified by Language. + + @retval EFI_SUCCESS The Unicode string for the Driver specifie= d by + This and the language specified by Languag= e was + returned in DriverName. + @retval EFI_INVALID_PARAMETER Language is NULL. + @retval EFI_INVALID_PARAMETER DriverName is NULL. + @retval EFI_UNSUPPORTED The driver specified by This does not supp= ort + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +UsbNcmComponentNameGetDriverName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN CHAR8 *Language, + OUT CHAR16 **DriverName + ) +{ + return LookupUnicodeString2 ( + Language, + This->SupportedLanguages, + gUsbNcmDriverNameTable, + DriverName, + (BOOLEAN)(This =3D=3D &gUsbNcmComponentName) + ); +} + +/** + Retrieves a Unicode string that is the user readable name of the control= ler + that is being managed by a driver. + + This function retrieves the user readable name of the controller specifi= ed by + ControllerHandle and ChildHandle in the form of a Unicode string. If the + driver specified by This has a user readable name in the language specif= ied by + Language, then a pointer to the controller name is returned in Controlle= rName, + and EFI_SUCCESS is returned. If the driver specified by This is not cur= rently + managing the controller specified by ControllerHandle and ChildHandle, + then EFI_UNSUPPORTED is returned. If the driver specified by This does = not + support the language specified by Language, then EFI_UNSUPPORTED is retu= rned. + + @param[in] This A pointer to the EFI_COMPONENT_NAME2_PROTO= COL or + EFI_COMPONENT_NAME_PROTOCOL instance. + @param[in] Controller The handle of a controller that the driver + specified by This is managing. This handl= e + specifies the controller whose name is to = be + returned. + @param[in] ChildHandle The handle of the child controller to retr= ieve + the name of. This is an optional paramete= r that + may be NULL. It will be NULL for device + drivers. It will also be NULL for a bus d= rivers + that wish to retrieve the name of the bus + controller. It will not be NULL for a bus + driver that wishes to retrieve the name of= a + child controller. + @param[in] Language A pointer to a Null-terminated ASCII strin= g + array indicating the language. This is th= e + language of the driver name that the calle= r is + requesting, and it must match one of the + languages specified in SupportedLanguages.= The + number of languages supported by a driver = is up + to the driver writer. Language is specifie= d in + RFC 4646 or ISO 639-2 language code format= . + @param[out] ControllerName A pointer to the Unicode string to return. + This Unicode string is the name of the + controller specified by ControllerHandle a= nd + ChildHandle in the language specified by + Language from the point of view of the dri= ver + specified by This. + + @retval EFI_SUCCESS The Unicode string for the user readable n= ame in + the language specified by Language for the + driver specified by This was returned in + DriverName. + @retval EFI_INVALID_PARAMETER ControllerHandle is not a valid EFI_HANDLE= . + @retval EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a va= lid + EFI_HANDLE. + @retval EFI_INVALID_PARAMETER Language is NULL. + @retval EFI_INVALID_PARAMETER ControllerName is NULL. + @retval EFI_UNSUPPORTED The driver specified by This is not curren= tly + managing the controller specified by + ControllerHandle and ChildHandle. + @retval EFI_UNSUPPORTED The driver specified by This does not supp= ort + the language specified by Language. + +**/ +EFI_STATUS +EFIAPI +UsbNcmComponentNameGetControllerName ( + IN EFI_COMPONENT_NAME_PROTOCOL *This, + IN EFI_HANDLE Controller, + IN EFI_HANDLE ChildHandle OPTIONAL, + IN CHAR8 *Language, + OUT CHAR16 **ControllerName + ) +{ + return EFI_UNSUPPORTED; +} diff --git a/UsbNetworkPkg/UsbCdcNcm/UsbCdcNcm.c b/UsbNetworkPkg/UsbCdcNcm/= UsbCdcNcm.c new file mode 100644 index 000000000000..f67e6aab695f --- /dev/null +++ b/UsbNetworkPkg/UsbCdcNcm/UsbCdcNcm.c @@ -0,0 +1,506 @@ +/** @file + This file contains code for USB Network Control Model + binding driver + + Copyright (c) 2023, American Megatrends International LLC. All rights re= served.
+ SPDX-License-Identifier: BSD-2-Clause-Patent +**/ + +#include "UsbCdcNcm.h" + +EFI_DRIVER_BINDING_PROTOCOL gUsbNcmDriverBinding =3D { + UsbNcmDriverSupported, + UsbNcmDriverStart, + UsbNcmDriverStop, + USB_NCM_DRIVER_VERSION, + NULL, + NULL +}; + +/** + Check if this interface is USB NCM SubType + + @param[in] UsbIo A pointer to the EFI_USB_IO_PROTOCOL instance. + + @retval TRUE USB NCM SubType. + @retval FALSE Not USB NCM SubType. + +**/ +BOOLEAN +IsSupportedDevice ( + IN EFI_USB_IO_PROTOCOL *UsbIo + ) +{ + EFI_STATUS Status; + EFI_USB_INTERFACE_DESCRIPTOR InterfaceDescriptor; + + Status =3D UsbIo->UsbGetInterfaceDescriptor (UsbIo, &InterfaceDescriptor= ); + if (EFI_ERROR (Status)) { + return FALSE; + } + + if ((InterfaceDescriptor.InterfaceClass =3D=3D USB_CDC_CLASS) && + (InterfaceDescriptor.InterfaceSubClass =3D=3D USB_CDC_NCM_SUBCLASS) = && + (InterfaceDescriptor.InterfaceProtocol =3D=3D USB_NO_CLASS_PROTOCOL)= ) + { + return TRUE; + } + + return FALSE; +} + +/** + USB NCM Driver Binding Support. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to test. + @param[in] RemainingDevicePath Optional parameter use to pick a speci= fic child + device to start. + + @retval EFI_SUCCESS This driver supports this device. + @retval EFI_ALREADY_STARTED This driver is already running on this= device. + @retval other This driver does not support this devi= ce. + +**/ +EFI_STATUS +EFIAPI +UsbNcmDriverSupported ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath + ) +{ + EFI_STATUS Status; + EFI_USB_IO_PROTOCOL *UsbIo; + + Status =3D gBS->OpenProtocol ( + ControllerHandle, + &gEfiUsbIoProtocolGuid, + (VOID **)&UsbIo, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (EFI_ERROR (Status)) { + return Status; + } + + Status =3D IsSupportedDevice (UsbIo) ? EFI_SUCCESS : EFI_UNSUPPORTED; + + gBS->CloseProtocol ( + ControllerHandle, + &gEfiUsbIoProtocolGuid, + This->DriverBindingHandle, + ControllerHandle + ); + return Status; +} + +/** + Check if the USB NCM and USB CDC Data interfaces are from the same devic= e. + + @param[in] UsbEthPath A pointer to the EFI_DEVICE_PATH= _PROTOCOL instance. + @param[in] UsbCdcDataPath A pointer to the EFI_DEVICE_PATH= _PROTOCOL instance. + + @retval EFI_SUCCESS Is the same device. + @retval EFI_NOT_FOUND Is not the same device. + +**/ +EFI_STATUS +IsSameDevice ( + IN EFI_DEVICE_PATH_PROTOCOL *UsbEthPath, + IN EFI_DEVICE_PATH_PROTOCOL *UsbCdcDataPath + ) +{ + while (1) { + if ((UsbEthPath->Type =3D=3D ACPI_DEVICE_PATH) && (UsbEthPath->SubType= =3D=3D ACPI_DP)) { + if (CompareMem ((ACPI_HID_DEVICE_PATH *)UsbCdcDataPath, (ACPI_HID_DE= VICE_PATH *)UsbEthPath, sizeof (ACPI_HID_DEVICE_PATH))) { + return EFI_NOT_FOUND; + } + } + + if ((UsbEthPath->Type =3D=3D HARDWARE_DEVICE_PATH) && (UsbEthPath->Sub= Type =3D=3D HW_PCI_DP)) { + if (CompareMem ((PCI_DEVICE_PATH *)UsbCdcDataPath, (PCI_DEVICE_PATH = *)UsbEthPath, sizeof (PCI_DEVICE_PATH))) { + return EFI_NOT_FOUND; + } + } + + if ((UsbEthPath->Type =3D=3D MESSAGING_DEVICE_PATH) && (UsbEthPath->Su= bType =3D=3D MSG_USB_DP)) { + if (IsDevicePathEnd (NextDevicePathNode (UsbEthPath))) { + if (((USB_DEVICE_PATH *)UsbEthPath)->ParentPortNumber =3D=3D + ((USB_DEVICE_PATH *)UsbCdcDataPath)->ParentPortNumber) + { + return EFI_SUCCESS; + } else { + return EFI_NOT_FOUND; + } + } else { + if (CompareMem ((USB_DEVICE_PATH *)UsbCdcDataPath, (USB_DEVICE_PAT= H *)UsbEthPath, sizeof (USB_DEVICE_PATH))) { + return EFI_NOT_FOUND; + } + } + } + + UsbEthPath =3D NextDevicePathNode (UsbEthPath); + UsbCdcDataPath =3D NextDevicePathNode (UsbCdcDataPath); + } +} + +/** + Check if the USB CDC Data(UsbIo) installed and return USB CDC Data Handl= e. + + @param[in] UsbEthPath A pointer to the EFI_DEVICE_PATH_PRO= TOCOL instance. + @param[in, out] UsbCdcDataHandle A pointer to the EFI_HANDLE for USB = CDC Data. + + @retval TRUE USB CDC Data(UsbIo) installed. + @retval FALSE USB CDC Data(UsbIo) did not installed. + +**/ +BOOLEAN +IsUsbCdcData ( + IN EFI_DEVICE_PATH_PROTOCOL *UsbEthPath, + IN OUT EFI_HANDLE *UsbCdcDataHandle + ) +{ + EFI_STATUS Status; + UINTN Index; + UINTN HandleCount; + EFI_HANDLE *HandleBuffer; + EFI_USB_IO_PROTOCOL *UsbIo; + EFI_USB_INTERFACE_DESCRIPTOR Interface; + EFI_DEVICE_PATH_PROTOCOL *UsbCdcDataPath; + + Status =3D gBS->LocateHandleBuffer ( + ByProtocol, + &gEfiUsbIoProtocolGuid, + NULL, + &HandleCount, + &HandleBuffer + ); + if (EFI_ERROR (Status)) { + return FALSE; + } + + for (Index =3D 0; Index < HandleCount; Index++) { + Status =3D gBS->HandleProtocol ( + HandleBuffer[Index], + &gEfiUsbIoProtocolGuid, + (VOID **)&UsbIo + ); + ASSERT_EFI_ERROR (Status); + + Status =3D UsbIo->UsbGetInterfaceDescriptor (UsbIo, &Interface); + ASSERT_EFI_ERROR (Status); + + if ((Interface.InterfaceClass =3D=3D USB_CDC_DATA_CLASS) && + (Interface.InterfaceSubClass =3D=3D USB_CDC_DATA_SUBCLASS) && + (Interface.InterfaceProtocol =3D=3D USB_NCM_NTB_PROTOCOL)) + { + Status =3D gBS->HandleProtocol ( + HandleBuffer[Index], + &gEfiDevicePathProtocolGuid, + (VOID **)&UsbCdcDataPath + ); + if (EFI_ERROR (Status)) { + continue; + } + + Status =3D IsSameDevice (UsbEthPath, UsbCdcDataPath); + if (!EFI_ERROR (Status)) { + CopyMem (UsbCdcDataHandle, &HandleBuffer[Index], sizeof (EFI_HANDL= E)); + FreePool (HandleBuffer); + return TRUE; + } + } + } + + FreePool (HandleBuffer); + return FALSE; +} + +/** + Call Back Function. + + @param[in] Event Event whose notification function is being invok= ed. + @param[in] Context The pointer to the notification function's conte= xt, + which is implementation-dependent. + +**/ +VOID +EFIAPI +CallbackFunction ( + IN EFI_EVENT Event, + IN VOID *Context + ) +{ + EFI_STATUS Status; + UINTN Index; + UINTN HandleCount; + EFI_HANDLE *HandleBuffer; + EFI_USB_IO_PROTOCOL *UsbIo; + EFI_USB_INTERFACE_DESCRIPTOR Interface; + + Status =3D gBS->LocateHandleBuffer ( + ByProtocol, + &gEfiUsbIoProtocolGuid, + NULL, + &HandleCount, + &HandleBuffer + ); + if (EFI_ERROR (Status)) { + return; + } + + for (Index =3D 0; Index < HandleCount; Index++) { + Status =3D gBS->HandleProtocol ( + HandleBuffer[Index], + &gEfiUsbIoProtocolGuid, + (VOID **)&UsbIo + ); + ASSERT_EFI_ERROR (Status); + + Status =3D UsbIo->UsbGetInterfaceDescriptor (UsbIo, &Interface); + ASSERT_EFI_ERROR (Status); + + if ((Interface.InterfaceClass =3D=3D USB_CDC_CLASS) && + (Interface.InterfaceSubClass =3D=3D USB_CDC_NCM_SUBCLASS) && + (Interface.InterfaceProtocol =3D=3D USB_NO_CLASS_PROTOCOL)) + { + gBS->ConnectController (HandleBuffer[Index], NULL, NULL, TRUE); + } + } + + FreePool (HandleBuffer); + gBS->CloseEvent (Event); +} + +/** + USB NCM Driver Binding Start. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to bind driver to. + @param[in] RemainingDevicePath Optional parameter use to pick a spe= cific child + device to start. + + @retval EFI_SUCCESS This driver is added to ControllerHa= ndle + @retval EFI_DEVICE_ERROR This driver could not be started due= to a device error + @retval EFI_OUT_OF_RESOURCES The driver could not install success= fully due to a lack of resources. + @retval other This driver does not support this de= vice + +**/ +EFI_STATUS +EFIAPI +UsbNcmDriverStart ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath + ) +{ + EFI_STATUS Status; + VOID *Reg; + EFI_EVENT Event; + USB_ETHERNET_DRIVER *UsbEthDriver; + EFI_DEVICE_PATH_PROTOCOL *UsbEthPath; + EFI_HANDLE UsbCdcDataHandle; + EFI_USB_IO_PROTOCOL *UsbIo; + EFI_USB_INTERFACE_DESCRIPTOR Interface; + + Status =3D gBS->OpenProtocol ( + ControllerHandle, + &gEfiUsbIoProtocolGuid, + (VOID **)&UsbIo, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_BY_DRIVER + ); + if (EFI_ERROR (Status)) { + return Status; + } + + Status =3D gBS->OpenProtocol ( + ControllerHandle, + &gEfiDevicePathProtocolGuid, + (VOID **)&UsbEthPath, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + gBS->CloseProtocol ( + ControllerHandle, + &gEfiUsbIoProtocolGuid, + This->DriverBindingHandle, + ControllerHandle + ); + return Status; + } + + Status =3D IsUsbCdcData (UsbEthPath, &UsbCdcDataHandle) ? EFI_SUCCESS : = EFI_UNSUPPORTED; + if (EFI_ERROR (Status)) { + gBS->CloseProtocol ( + ControllerHandle, + &gEfiUsbIoProtocolGuid, + This->DriverBindingHandle, + ControllerHandle + ); + + Status =3D gBS->CreateEvent (EVT_NOTIFY_SIGNAL, TPL_CALLBACK, Callback= Function, NULL, &Event); + if (EFI_ERROR (Status)) { + return Status; + } + + Status =3D gBS->RegisterProtocolNotify (&gEfiUsbIoProtocolGuid, Event,= &Reg); + return Status; + } + + UsbEthDriver =3D AllocateZeroPool (sizeof (USB_ETHERNET_DRIVER)); + if (!UsbEthDriver) { + gBS->CloseProtocol ( + ControllerHandle, + &gEfiUsbIoProtocolGuid, + This->DriverBindingHandle, + ControllerHandle + ); + return EFI_OUT_OF_RESOURCES; + } + + Status =3D LoadAllDescriptor (UsbIo, &UsbEthDriver->Config); + ASSERT_EFI_ERROR (Status); + + GetEndpoint (UsbIo, UsbEthDriver); + + Status =3D UsbIo->UsbGetInterfaceDescriptor (UsbIo, &Interface); + ASSERT_EFI_ERROR (Status); + + UsbEthDriver->Signature =3D USB_ETHERNET_SIGNAT= URE; + UsbEthDriver->NumOfInterface =3D Interface.Interface= Number; + UsbEthDriver->UsbCdcDataHandle =3D UsbCdcDataHandle; + UsbEthDriver->UsbIo =3D UsbIo; + UsbEthDriver->UsbEth.UsbEthReceive =3D UsbEthNcmReceive; + UsbEthDriver->UsbEth.UsbEthTransmit =3D UsbEthNcmTransmit; + UsbEthDriver->UsbEth.UsbEthInterrupt =3D UsbEthNcmInterrupt; + UsbEthDriver->UsbEth.UsbEthMacAddress =3D GetUsbEthMacAddress= ; + UsbEthDriver->UsbEth.UsbEthMaxBulkSize =3D UsbEthNcmBulkSize; + UsbEthDriver->UsbEth.UsbHeaderFunDescriptor =3D GetUsbHeaderFunDesc= riptor; + UsbEthDriver->UsbEth.UsbUnionFunDescriptor =3D GetUsbUnionFunDescr= iptor; + UsbEthDriver->UsbEth.UsbEthFunDescriptor =3D GetUsbEthFunDescrip= tor; + UsbEthDriver->UsbEth.SetUsbEthMcastFilter =3D SetUsbEthMcastFilte= r; + UsbEthDriver->UsbEth.SetUsbEthPowerPatternFilter =3D SetUsbEthPowerFilte= r; + UsbEthDriver->UsbEth.GetUsbEthPowerPatternFilter =3D GetUsbEthPowerFilte= r; + UsbEthDriver->UsbEth.SetUsbEthPacketFilter =3D SetUsbEthPacketFilt= er; + UsbEthDriver->UsbEth.GetUsbEthStatistic =3D GetUsbEthStatistic; + + UsbEthDriver->BulkBuffer =3D AllocateZeroPool (USB_NCM_MAX_NTB_SIZE); + + Status =3D gBS->InstallProtocolInterface ( + &ControllerHandle, + &gEdkIIUsbEthProtocolGuid, + EFI_NATIVE_INTERFACE, + &(UsbEthDriver->UsbEth) + ); + if (EFI_ERROR (Status)) { + gBS->CloseProtocol ( + ControllerHandle, + &gEfiUsbIoProtocolGuid, + This->DriverBindingHandle, + ControllerHandle + ); + FreePool (UsbEthDriver); + return Status; + } + + return Status; +} + +/** + USB NCM Driver Binding Stop. + + @param[in] This Protocol instance pointer. + @param[in] ControllerHandle Handle of device to stop driver on + @param[in] NumberOfChildren Number of Handles in ChildHandleBuffer= . If number of + children is zero stop the entire bus d= river. + @param[in] ChildHandleBuffer List of Child Handles to Stop. + + @retval EFI_SUCCESS This driver is removed ControllerHandl= e + @retval other This driver was not removed from this = device + +**/ +EFI_STATUS +EFIAPI +UsbNcmDriverStop ( + IN EFI_DRIVER_BINDING_PROTOCOL *This, + IN EFI_HANDLE ControllerHandle, + IN UINTN NumberOfChildren, + IN EFI_HANDLE *ChildHandleBuffer + ) +{ + EFI_STATUS Status; + EDKII_USB_ETHERNET_PROTOCOL *UsbEthProtocol; + USB_ETHERNET_DRIVER *UsbEthDriver; + + Status =3D gBS->OpenProtocol ( + ControllerHandle, + &gEdkIIUsbEthProtocolGuid, + (VOID **)&UsbEthProtocol, + This->DriverBindingHandle, + ControllerHandle, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + if (EFI_ERROR (Status)) { + return Status; + } + + UsbEthDriver =3D USB_ETHERNET_DEV_FROM_THIS (UsbEthProtocol); + + Status =3D gBS->UninstallProtocolInterface ( + ControllerHandle, + &gEdkIIUsbEthProtocolGuid, + UsbEthProtocol + ); + if (EFI_ERROR (Status)) { + return Status; + } + + Status =3D gBS->CloseProtocol ( + ControllerHandle, + &gEfiUsbIoProtocolGuid, + This->DriverBindingHandle, + ControllerHandle + ); + FreePool (UsbEthDriver->Config); + FreePool (UsbEthDriver->BulkBuffer); + FreePool (UsbEthDriver); + return Status; +} + +/** + Entrypoint of NCM Driver. + + This function is the entrypoint of NCM Driver. It installs Driver Bindin= g + Protocols together with Component Name Protocols. + + @param[in] ImageHandle The firmware allocated handle for the EFI = image. + @param[in] SystemTable A pointer to the EFI System Table. + + @retval EFI_SUCCESS The entry point is executed successfully. + +**/ +EFI_STATUS +EFIAPI +UsbNcmEntry ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + gUsbNcmDriverBinding.DriverBindingHandle =3D ImageHandle; + gUsbNcmDriverBinding.ImageHandle =3D ImageHandle; + + return gBS->InstallMultipleProtocolInterfaces ( + &gUsbNcmDriverBinding.DriverBindingHandle, + &gEfiDriverBindingProtocolGuid, + &gUsbNcmDriverBinding, + &gEfiComponentName2ProtocolGuid, + &gUsbNcmComponentName2, + NULL + ); +} diff --git a/UsbNetworkPkg/UsbCdcNcm/UsbNcmFunction.c b/UsbNetworkPkg/UsbCd= cNcm/UsbNcmFunction.c new file mode 100644 index 000000000000..24ced413d60b --- /dev/null +++ b/UsbNetworkPkg/UsbCdcNcm/UsbNcmFunction.c @@ -0,0 +1,966 @@ +/** @file + This file contains code for USB Ethernet descriptor + and specific requests implement. + + Copyright (c) 2023, American Megatrends International LLC. All rights re= served.
+ SPDX-License-Identifier: BSD-2-Clause-Patent +**/ + +#include "UsbCdcNcm.h" + +/** + Load All of device descriptor. + + @param[in] UsbIo A pointer to the EFI_USB_IO_PROTOCOL i= nstance. + @param[out] ConfigDesc A pointer to the configuration descrip= tor. + + @retval EFI_SUCCESS The request executed successfully. + @retval EFI_OUT_OF_RESOURCES The request could not be completed because= the + buffer specified by DescriptorLength and D= escriptor + is not large enough to hold the result of = the request. + @retval EFI_TIMEOUT A timeout occurred executing the request. + @retval EFI_DEVICE_ERROR The request failed due to a device error. = The transfer + status is returned in Status. + +**/ +EFI_STATUS +LoadAllDescriptor ( + IN EFI_USB_IO_PROTOCOL *UsbIo, + OUT EFI_USB_CONFIG_DESCRIPTOR **ConfigDesc + ) +{ + EFI_STATUS Status; + UINT32 TransStatus; + EFI_USB_CONFIG_DESCRIPTOR Tmp; + + Status =3D UsbIo->UsbGetConfigDescriptor (UsbIo, &Tmp); + ASSERT_EFI_ERROR (Status); + + Status =3D gBS->AllocatePool (EfiBootServicesData, Tmp.TotalLength, (VOI= D **)ConfigDesc); + ASSERT_EFI_ERROR (Status); + + Status =3D UsbGetDescriptor ( + UsbIo, + USB_DESC_TYPE_CONFIG << 8 | (Tmp.ConfigurationValue - 1), = // zero based + 0, + Tmp.TotalLength, + *ConfigDesc, + &TransStatus + ); + return Status; +} + +/** + Returns pointer to the next descriptor for the pack of USB descriptors + located in continues memory segment + + @param[in] Desc A pointer to the CONFIG_DESCRIPTOR instance. + @param[in, out] Offset A pointer to the sum of descriptor length. + + @retval TRUE The request executed successfully. + @retval FALSE No next descriptor. + +**/ +BOOLEAN +NextDescriptor ( + IN EFI_USB_CONFIG_DESCRIPTOR *Desc, + IN OUT UINTN *Offset + ) +{ + if ((Desc =3D=3D NULL) || (*Offset >=3D Desc->TotalLength)) { + return FALSE; + } + + if (((EFI_USB_CONFIG_DESCRIPTOR *)((char *)Desc+*Offset))->Length =3D=3D= 0) { + return FALSE; + } + + *Offset +=3D ((EFI_USB_CONFIG_DESCRIPTOR *)((char *)Desc+*Offset))->Leng= th; + if ( *Offset >=3D Desc->TotalLength ) { + return FALSE; + } + + return TRUE; +} + +/** + Read Function descriptor + + @param[in] Config A pointer to all of configuration. + @param[in] FunDescriptorType USB CDC class descriptor SubType. + @param[out] DataBuffer A pointer to the Data of corresponding to= device capability. + + @retval EFI_SUCCESS The device capability descriptor was retrieve= d + successfully. + @retval EFI_UNSUPPORTED No supported. + @retval EFI_NOT_FOUND The device capability descriptor was not foun= d. + +**/ +EFI_STATUS +GetFunctionalDescriptor ( + IN EFI_USB_CONFIG_DESCRIPTOR *Config, + IN UINT8 FunDescriptorType, + OUT VOID *DataBuffer + ) +{ + EFI_STATUS Status; + UINTN Offset; + EFI_USB_INTERFACE_DESCRIPTOR *Interface; + + Status =3D EFI_NOT_FOUND; + + for (Offset =3D 0; NextDescriptor (Config, &Offset);) { + Interface =3D (EFI_USB_INTERFACE_DESCRIPTOR *)((UINT8 *)Config + Offse= t); + if (Interface->DescriptorType =3D=3D CS_INTERFACE) { + if (((USB_HEADER_FUN_DESCRIPTOR *)Interface)->DescriptorSubtype =3D= =3D FunDescriptorType) { + switch (FunDescriptorType) { + case HEADER_FUN_DESCRIPTOR: + CopyMem ( + DataBuffer, + (USB_HEADER_FUN_DESCRIPTOR *)Interface, + sizeof (USB_HEADER_FUN_DESCRIPTOR) + ); + return EFI_SUCCESS; + case UNION_FUN_DESCRIPTOR: + CopyMem ( + DataBuffer, + (USB_UNION_FUN_DESCRIPTOR *)Interface, + ((USB_UNION_FUN_DESCRIPTOR *)Interface)->FunctionLength + ); + return EFI_SUCCESS; + case ETHERNET_FUN_DESCRIPTOR: + CopyMem ( + DataBuffer, + (USB_ETHERNET_FUN_DESCRIPTOR *)Interface, + sizeof (USB_ETHERNET_FUN_DESCRIPTOR) + ); + return EFI_SUCCESS; + default: + Status =3D EFI_UNSUPPORTED; + break; + } + } + } + } + + return Status; +} + +/** + Get USB Ethernet IO endpoint and USB CDC data IO endpoint. + + @param[in] UsbIo A pointer to the EFI_USB_IO_PROTOCOL insta= nce. + @param[in, out] UsbEthDriver A pointer to the USB_ETHERNET_DRIVER insta= nce. + +**/ +VOID +GetEndpoint ( + IN EFI_USB_IO_PROTOCOL *UsbIo, + IN OUT USB_ETHERNET_DRIVER *UsbEthDriver + ) +{ + EFI_STATUS Status; + UINT8 Index; + UINT32 Result; + EFI_USB_INTERFACE_DESCRIPTOR Interface; + EFI_USB_ENDPOINT_DESCRIPTOR Endpoint; + + Status =3D UsbIo->UsbGetInterfaceDescriptor (UsbIo, &Interface); + ASSERT_EFI_ERROR (Status); + + if (Interface.NumEndpoints =3D=3D 0) { + Status =3D UsbSetInterface (UsbIo, Interface.InterfaceNumber, 1, &Resu= lt); + ASSERT_EFI_ERROR (Status); + + Status =3D UsbIo->UsbGetInterfaceDescriptor (UsbIo, &Interface); + ASSERT_EFI_ERROR (Status); + } + + for (Index =3D 0; Index < Interface.NumEndpoints; Index++) { + Status =3D UsbIo->UsbGetEndpointDescriptor (UsbIo, Index, &Endpoint); + ASSERT_EFI_ERROR (Status); + + switch ((Endpoint.Attributes & (BIT0 | BIT1))) { + case USB_ENDPOINT_BULK: + if (Endpoint.EndpointAddress & BIT7) { + UsbEthDriver->BulkInEndpoint =3D Endpoint.EndpointAddress; + } else { + UsbEthDriver->BulkOutEndpoint =3D Endpoint.EndpointAddress; + } + + break; + case USB_ENDPOINT_INTERRUPT: + UsbEthDriver->InterruptEndpoint =3D Endpoint.EndpointAddress; + break; + } + } +} + +/** + This function is used to manage a USB device with the bulk transfer pipe= . The endpoint is Bulk in. + + @param[in] Cdb A pointer to the command descriptor block. + @param[in] This A pointer to the EDKII_USB_ETHERNET_PROTOC= OL instance. + @param[in, out] Packet A pointer to the buffer of data that will = be transmitted to USB + device or received from USB device. + @param[in, out] PacketLength A pointer to the PacketLength. + + @retval EFI_SUCCESS The bulk transfer has been successfully ex= ecuted. + @retval EFI_DEVICE_ERROR The transfer failed. The transfer status i= s returned in status. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + @retval EFI_OUT_OF_RESOURCES The request could not be submitted due to = a lack of resources. + @retval EFI_TIMEOUT The control transfer fails due to timeout. + +**/ +EFI_STATUS +EFIAPI +UsbEthNcmReceive ( + IN PXE_CDB *Cdb, + IN EDKII_USB_ETHERNET_PROTOCOL *This, + IN OUT VOID *Packet, + IN OUT UINTN *PacketLength + ) +{ + EFI_STATUS Status; + USB_ETHERNET_DRIVER *UsbEthDriver; + EFI_USB_IO_PROTOCOL *UsbIo; + UINT32 TransStatus; + UINT8 Index; + UINTN BulkDataLength; + UINTN TotalLength; + USB_NCM_TRANSFER_HEADER_16 *Nth; + USB_NCM_DATAGRAM_POINTER_16 *Ndp; + USB_NCM_DATA_GRAM *Datagram; + + UsbEthDriver =3D USB_ETHERNET_DEV_FROM_THIS (This); + TotalLength =3D 0; + + if (UsbEthDriver->TotalDatagram =3D=3D UsbEthDriver->NowDatagram) { + Status =3D gBS->HandleProtocol ( + UsbEthDriver->UsbCdcDataHandle, + &gEfiUsbIoProtocolGuid, + (VOID **)&UsbIo + ); + if (EFI_ERROR (Status)) { + return Status; + } + + if (UsbEthDriver->BulkInEndpoint =3D=3D 0) { + GetEndpoint (UsbIo, UsbEthDriver); + } + + BulkDataLength =3D USB_NCM_MAX_NTB_SIZE; + SetMem (UsbEthDriver->BulkBuffer, BulkDataLength, 0); + UsbEthDriver->NowDatagram =3D 0; + UsbEthDriver->TotalDatagram =3D 0; + + Status =3D UsbIo->UsbBulkTransfer ( + UsbIo, + UsbEthDriver->BulkInEndpoint, + UsbEthDriver->BulkBuffer, + &BulkDataLength, + USB_ETHERNET_BULK_TIMEOUT, + &TransStatus + ); + if (EFI_ERROR (Status)) { + return Status; + } + + Nth =3D (USB_NCM_TRANSFER_HEADER_16 *)UsbEthDr= iver->BulkBuffer; + Ndp =3D (USB_NCM_DATAGRAM_POINTER_16 *)((UINT8= *)UsbEthDriver->BulkBuffer + Nth->NdpIndex); + Datagram =3D (USB_NCM_DATA_GRAM *)((UINT8 *)Ndp + s= izeof (USB_NCM_DATAGRAM_POINTER_16)); + UsbEthDriver->TotalDatagram =3D (UINT8)((Ndp->Length - 8) / 4 - 1); + + for (Index =3D 0; Index < UsbEthDriver->TotalDatagram; Index++) { + TotalLength +=3D Datagram->DatagramLength; + Datagram =3D (USB_NCM_DATA_GRAM *)((UINT8 *)Datagram + sizeof (U= SB_NCM_DATA_GRAM)); + } + + if (TotalLength < USB_ETHERNET_FRAME_SIZE) { + Datagram =3D (USB_NCM_DATA_GRAM *)((UINT8 *)Ndp + sizeof (USB_NCM_DA= TAGRAM_POINTER_16)); + + TotalLength =3D 0; + for (Index =3D 0; Index < UsbEthDriver->TotalDatagram; Index++) { + CopyMem ((UINT8 *)Packet + TotalLength, (UINT8 *)UsbEthDriver->Bul= kBuffer + Datagram->DatagramIndex, Datagram->DatagramLength); + TotalLength +=3D Datagram->DatagramLength; + Datagram =3D (USB_NCM_DATA_GRAM *)((UINT8 *)Datagram + sizeof = (USB_NCM_DATA_GRAM)); + } + + *PacketLength =3D TotalLength; + UsbEthDriver->NowDatagram =3D UsbEthDriver->TotalDatagram; + } else { + UsbEthDriver->NowDatagram++; + + Datagram =3D (USB_NCM_DATA_GRAM *)((UINT8 *)Ndp + sizeof (USB_NCM_DA= TAGRAM_POINTER_16)); + CopyMem (Packet, (UINT8 *)UsbEthDriver->BulkBuffer + Datagram->Datag= ramIndex, Datagram->DatagramLength); + *PacketLength =3D Datagram->DatagramLength; + } + + return Status; + } else { + UsbEthDriver->NowDatagram++; + + Nth =3D (USB_NCM_TRANSFER_HEADER_16 *)UsbEthDriver->BulkBuffer; + Ndp =3D (USB_NCM_DATAGRAM_POINTER_16 *)((UINT8 *)UsbEthDriver->Bu= lkBuffer + Nth->NdpIndex); + Datagram =3D (USB_NCM_DATA_GRAM *)((UINT8 *)Ndp + sizeof (USB_NCM_DATA= GRAM_POINTER_16)); + Datagram =3D (USB_NCM_DATA_GRAM *)((UINT8 *)Datagram + sizeof (USB_NCM= _DATA_GRAM) * (UsbEthDriver->NowDatagram - 1)); + + CopyMem (Packet, (UINT8 *)UsbEthDriver->BulkBuffer + Datagram->Datagra= mIndex, Datagram->DatagramLength); + *PacketLength =3D Datagram->DatagramLength; + + return EFI_SUCCESS; + } +} + +/** + This function is used to manage a USB device with the bulk transfer pipe= . The endpoint is Bulk out. + + @param[in] Cdb A pointer to the command descriptor block. + @param[in] This A pointer to the EDKII_USB_ETHERNET_PROTOC= OL instance. + @param[in] Packet A pointer to the buffer of data that will = be transmitted to USB + device or received from USB device. + @param[in, out] PacketLength A pointer to the PacketLength. + + @retval EFI_SUCCESS The bulk transfer has been successfully ex= ecuted. + @retval EFI_DEVICE_ERROR The transfer failed. The transfer status i= s returned in status. + @retval EFI_INVALID_PARAMETER One or more parameters are invalid. + @retval EFI_OUT_OF_RESOURCES The request could not be submitted due to = a lack of resources. + @retval EFI_TIMEOUT The control transfer fails due to timeout. + +**/ +EFI_STATUS +EFIAPI +UsbEthNcmTransmit ( + IN PXE_CDB *Cdb, + IN EDKII_USB_ETHERNET_PROTOCOL *This, + IN VOID *Packet, + IN OUT UINTN *PacketLength + ) +{ + EFI_STATUS Status; + USB_ETHERNET_DRIVER *UsbEthDriver; + EFI_USB_IO_PROTOCOL *UsbIo; + UINT32 TransStatus; + USB_NCM_TRANSFER_HEADER_16 *Nth; + USB_NCM_DATAGRAM_POINTER_16 *Ndp; + USB_NCM_DATA_GRAM *Datagram; + UINT8 *TotalPacket; + UINTN TotalLength; + + UsbEthDriver =3D USB_ETHERNET_DEV_FROM_THIS (This); + + Status =3D gBS->HandleProtocol ( + UsbEthDriver->UsbCdcDataHandle, + &gEfiUsbIoProtocolGuid, + (VOID **)&UsbIo + ); + if (EFI_ERROR (Status)) { + return Status; + } + + if (UsbEthDriver->BulkOutEndpoint =3D=3D 0) { + GetEndpoint (UsbIo, UsbEthDriver); + } + + TotalLength =3D (UINTN)(USB_NCM_NTH_LENGTH + USB_NCM_NDP_LENGTH + (*Pack= etLength)); + + Status =3D gBS->AllocatePool (EfiBootServicesData, TotalLength, (VOID **= )&TotalPacket); + if (EFI_ERROR (Status)) { + return Status; + } + + SetMem (TotalPacket, TotalLength, 0); + + Nth =3D (USB_NCM_TRANSFER_HEADER_16 *)TotalPacket; + Nth->Signature =3D USB_NCM_NTH_SIGN_16; + Nth->HeaderLength =3D USB_NCM_NTH_LENGTH; + Nth->Sequence =3D UsbEthDriver->BulkOutSequence++; + Nth->BlockLength =3D (UINT16)TotalLength; + Nth->NdpIndex =3D Nth->HeaderLength; + + Ndp =3D (USB_NCM_DATAGRAM_POINTER_16 *)((UINT8 *)TotalPack= et + Nth->NdpIndex); + Ndp->Signature =3D USB_NCM_NDP_SIGN_16; + Ndp->Length =3D USB_NCM_NDP_LENGTH; + Ndp->NextNdpIndex =3D 0x00; + + Datagram =3D (USB_NCM_DATA_GRAM *)((UINT8 *)Ndp + sizeof= (USB_NCM_DATAGRAM_POINTER_16)); + Datagram->DatagramIndex =3D Nth->HeaderLength + Ndp->Length; + Datagram->DatagramLength =3D (UINT16)*PacketLength; + + CopyMem (TotalPacket + Datagram->DatagramIndex, Packet, *PacketLength); + + *PacketLength =3D TotalLength; + + Status =3D UsbIo->UsbBulkTransfer ( + UsbIo, + UsbEthDriver->BulkOutEndpoint, + TotalPacket, + PacketLength, + USB_ETHERNET_BULK_TIMEOUT, + &TransStatus + ); + FreePool (TotalPacket); + return Status; +} + +/** + Async USB transfer callback routine. + + @param[in] Data Data received or sent via the USB Asynchrono= us Transfer, if the + transfer completed successfully. + @param[in] DataLength The length of Data received or sent via the = Asynchronous + Transfer, if transfer successfully completes= . + @param[in] Context Data passed from UsbAsyncInterruptTransfer()= request. + @param[in] Status Indicates the result of the asynchronous tra= nsfer. + + @retval EFI_SUCCESS The asynchronous USB transfer request has = been successfully executed. + @retval EFI_DEVICE_ERROR The asynchronous USB transfer request fail= ed. + +**/ +EFI_STATUS +EFIAPI +InterruptCallback ( + IN VOID *Data, + IN UINTN DataLength, + IN VOID *Context, + IN UINT32 Status + ) +{ + if ((Data =3D=3D NULL) || (Context =3D=3D NULL)) { + return EFI_INVALID_PARAMETER; + } + + if (((EFI_USB_DEVICE_REQUEST *)Data)->Request =3D=3D USB_CDC_NETWORK_CON= NECTION) { + CopyMem ( + (EFI_USB_DEVICE_REQUEST *)Context, + (EFI_USB_DEVICE_REQUEST *)Data, + sizeof (EFI_USB_DEVICE_REQUEST) + ); + } + + return EFI_SUCCESS; +} + +/** + This function is used to manage a USB device with an interrupt transfer = pipe. + + @param[in] This A pointer to the EDKII_USB_ETHERNET_PROTOC= OL instance. + @param[in] IsNewTransfer If TRUE, a new transfer will be submitted = to USB controller. If + FALSE, the interrupt transfer is deleted f= rom the device's interrupt + transfer queue. + @param[in] PollingInterval Indicates the periodic rate, in millisecon= ds, that the transfer is to be + executed.This parameter is required when I= sNewTransfer is TRUE. The + value must be between 1 to 255, otherwise = EFI_INVALID_PARAMETER is returned. + The units are in milliseconds. + @param[in] Request A pointer to the EFI_USB_DEVICE_REQUEST da= ta. + + @retval EFI_SUCCESS The asynchronous USB transfer request tran= sfer has been successfully executed. + @retval EFI_DEVICE_ERROR The asynchronous USB transfer request fail= ed. + +**/ +EFI_STATUS +EFIAPI +UsbEthNcmInterrupt ( + IN EDKII_USB_ETHERNET_PROTOCOL *This, + IN BOOLEAN IsNewTransfer, + IN UINTN PollingInterval, + IN EFI_USB_DEVICE_REQUEST *Request + ) +{ + EFI_STATUS Status; + USB_ETHERNET_DRIVER *UsbEthDriver; + UINTN DataLength; + + UsbEthDriver =3D USB_ETHERNET_DEV_FROM_THIS (This); + DataLength =3D 0; + + if (IsNewTransfer =3D=3D TRUE) { + DataLength =3D sizeof (EFI_USB_DEVICE_REQUEST) + sizeof (USB_CONNECT_S= PEED_CHANGE); + Status =3D UsbEthDriver->UsbIo->UsbAsyncInterruptTransfer ( + UsbEthDriver->UsbIo, + UsbEthDriver->InterruptEndpoint, + IsNewTransfer, + PollingInterval, + DataLength, + (EFI_ASYNC_USB_TRANSFER_CALLBACK)I= nterruptCallback, + Request + ); + } else { + Status =3D UsbEthDriver->UsbIo->UsbAsyncInterruptTransfer ( + UsbEthDriver->UsbIo, + UsbEthDriver->InterruptEndpoint, + IsNewTransfer, + 0, + 0, + NULL, + NULL + ); + } + + return Status; +} + +/** + Retrieves the USB Ethernet Mac Address. + + @param[in] This A pointer to the EDKII_USB_ETHERNET_PROTOCOL i= nstance. + @param[out] MacAddress A pointer to the caller allocated USB Ethernet= Mac Address. + + @retval EFI_SUCCESS The USB Header Functional descriptor was r= etrieved successfully. + @retval EFI_INVALID_PARAMETER UsbHeaderFunDescriptor is NULL. + @retval EFI_NOT_FOUND The USB Header Functional descriptor was n= ot found. + +**/ +EFI_STATUS +EFIAPI +GetUsbEthMacAddress ( + IN EDKII_USB_ETHERNET_PROTOCOL *This, + OUT EFI_MAC_ADDRESS *MacAddress + ) +{ + EFI_STATUS Status; + USB_ETHERNET_DRIVER *UsbEthDriver; + USB_ETHERNET_FUN_DESCRIPTOR UsbEthDescriptor; + CHAR16 *Data; + CHAR16 *DataPtr; + CHAR16 TmpStr[1]; + UINT8 Index; + UINT8 Hi; + UINT8 Low; + + UsbEthDriver =3D USB_ETHERNET_DEV_FROM_THIS (This); + + Status =3D This->UsbEthFunDescriptor (This, &UsbEthDescriptor); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a:UsbEthFunDescriptor status =3D %r\n", __FUNCT= ION__, Status)); + return Status; + } + + Status =3D UsbEthDriver->UsbIo->UsbGetStringDescriptor ( + UsbEthDriver->UsbIo, + 0x409, // English= -US Language ID + UsbEthDescriptor.MacAddress, + &Data + ); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "%a:UsbGetStringDescriptor status =3D %r\n", __FU= NCTION__, Status)); + return Status; + } + + DataPtr =3D Data; + for (Index =3D 0; Index < PXE_HWADDR_LEN_ETHER; Index++) { + CopyMem (TmpStr, DataPtr, sizeof (CHAR16)); + DataPtr++; + Hi =3D (UINT8)StrHexToUintn (TmpStr); + CopyMem (TmpStr, DataPtr, sizeof (CHAR16)); + DataPtr++; + Low =3D (UINT8)StrHexToUintn (TmpStr); + MacAddress->Addr[Index] =3D (Hi << 4) | Low; + } + + return Status; +} + +/** + Get the USB NCM max NTB size. + + @param[in] This A pointer to the EDKII_USB_ETHERNET_PROTOCOL i= nstance. + @param[out] BulkSize A pointer to the Bulk transfer data size. + + @retval EFI_SUCCESS Get the USB NCM max NTB size successfully. + +**/ +EFI_STATUS +EFIAPI +UsbEthNcmBulkSize ( + IN EDKII_USB_ETHERNET_PROTOCOL *This, + OUT UINTN *BulkSize + ) +{ + *BulkSize =3D USB_NCM_MAX_NTB_SIZE; + return EFI_SUCCESS; +} + +/** + Retrieves the USB Header functional Descriptor. + + @param[in] This A pointer to the EDKII_USB_ETHERNET_P= ROTOCOL instance. + @param[out] UsbHeaderFunDescriptor A pointer to the caller allocated USB= Header Functional Descriptor. + + @retval EFI_SUCCESS The USB Header Functional descriptor was r= etrieved successfully. + @retval EFI_INVALID_PARAMETER UsbHeaderFunDescriptor is NULL. + @retval EFI_NOT_FOUND The USB Header Functional descriptor was n= ot found. + +**/ +EFI_STATUS +EFIAPI +GetUsbHeaderFunDescriptor ( + IN EDKII_USB_ETHERNET_PROTOCOL *This, + OUT USB_HEADER_FUN_DESCRIPTOR *UsbHeaderFunDescriptor + ) +{ + EFI_STATUS Status; + USB_ETHERNET_DRIVER *UsbEthDriver; + + UsbEthDriver =3D USB_ETHERNET_DEV_FROM_THIS (This); + + if (UsbHeaderFunDescriptor =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } + + Status =3D GetFunctionalDescriptor (UsbEthDriver->Config, HEADER_FUN_DES= CRIPTOR, UsbHeaderFunDescriptor); + return Status; +} + +/** + Retrieves the USB Union functional Descriptor. + + @param[in] This A pointer to the EDKII_USB_ETHERNET_P= ROTOCOL instance. + @param[out] UsbUnionFunDescriptor A pointer to the caller allocated USB= Union Functional Descriptor. + + @retval EFI_SUCCESS The USB Union Functional descriptor was re= trieved successfully. + @retval EFI_INVALID_PARAMETER UsbUnionFunDescriptor is NULL. + @retval EFI_NOT_FOUND The USB Union Functional descriptor was no= t found. + +**/ +EFI_STATUS +EFIAPI +GetUsbUnionFunDescriptor ( + IN EDKII_USB_ETHERNET_PROTOCOL *This, + OUT USB_UNION_FUN_DESCRIPTOR *UsbUnionFunDescriptor + ) +{ + EFI_STATUS Status; + USB_ETHERNET_DRIVER *UsbEthDriver; + + UsbEthDriver =3D USB_ETHERNET_DEV_FROM_THIS (This); + + if (UsbUnionFunDescriptor =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } + + Status =3D GetFunctionalDescriptor (UsbEthDriver->Config, UNION_FUN_DESC= RIPTOR, UsbUnionFunDescriptor); + return Status; +} + +/** + Retrieves the USB Ethernet functional Descriptor. + + This function get the Mac Address, Ethernet statistics, maximum segment = size, + number of multicast filters, and number of pattern filters from Ethernet + functional Descriptor. + + @param[in] This A pointer to the EDKII_USB_ETHERNET_P= ROTOCOL instance. + @param[out] UsbEthFunDescriptor A pointer to the caller allocated USB= Ethernet Functional Descriptor. + + @retval EFI_SUCCESS The USB Ethernet Functional descriptor was= retrieved successfully. + @retval EFI_INVALID_PARAMETER UsbEthFunDescriptor is NULL. + @retval EFI_NOT_FOUND The USB Ethernet Functional descriptor was= not found. + +**/ +EFI_STATUS +EFIAPI +GetUsbEthFunDescriptor ( + IN EDKII_USB_ETHERNET_PROTOCOL *This, + OUT USB_ETHERNET_FUN_DESCRIPTOR *UsbEthFunDescriptor + ) +{ + EFI_STATUS Status; + USB_ETHERNET_DRIVER *UsbEthDriver; + + UsbEthDriver =3D USB_ETHERNET_DEV_FROM_THIS (This); + + if (UsbEthFunDescriptor =3D=3D NULL) { + return EFI_INVALID_PARAMETER; + } + + Status =3D GetFunctionalDescriptor (UsbEthDriver->Config, ETHERNET_FUN_D= ESCRIPTOR, UsbEthFunDescriptor); + return Status; +} + +/** + This request sets the Ethernet device multicast filters as specified in = the + sequential list of 48 bit Ethernet multicast addresses. + + @param[in] This A pointer to the EDKII_USB_ETHERNET_P= ROTOCOL instance. + @param[in] Value Number of filters. + @param[in] McastAddr A pointer to the value of the multica= st addresses. + + @retval EFI_SUCCESS The request executed successfully. + @retval EFI_TIMEOUT A timeout occurred executing the request. + @retval EFI_DEVICE_ERROR The request failed due to a device error. + @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value= . + @retval EFI_UNSUPPORTED Not supported. + +**/ +EFI_STATUS +EFIAPI +SetUsbEthMcastFilter ( + IN EDKII_USB_ETHERNET_PROTOCOL *This, + IN UINT16 Value, + IN VOID *McastAddr + ) +{ + EFI_STATUS Status; + EFI_USB_DEVICE_REQUEST Request; + UINT32 TransStatus; + USB_ETHERNET_FUN_DESCRIPTOR UsbEthFunDescriptor; + USB_ETHERNET_DRIVER *UsbEthDriver; + + UsbEthDriver =3D USB_ETHERNET_DEV_FROM_THIS (This); + + Status =3D This->UsbEthFunDescriptor (This, &UsbEthFunDescriptor); + if (EFI_ERROR (Status)) { + return Status; + } + + if ((UsbEthFunDescriptor.NumberMcFilters << 1) =3D=3D 0) { + return EFI_UNSUPPORTED; + } + + Request.RequestType =3D USB_ETHERNET_SET_REQ_TYPE; + Request.Request =3D SET_ETH_MULTICAST_FILTERS_REQ; + Request.Value =3D Value; + Request.Index =3D UsbEthDriver->NumOfInterface; + Request.Length =3D Value * 6; + + return UsbEthDriver->UsbIo->UsbControlTransfer ( + UsbEthDriver->UsbIo, + &Request, + EfiUsbDataOut, + USB_ETHERNET_TRANSFER_TIMEOUT, + McastAddr, + Request.Length, + &TransStatus + ); +} + +/** + This request sets up the specified Ethernet power management pattern fil= ter as + described in the data structure. + + @param[in] This A pointer to the EDKII_USB_ETHERNET_PR= OTOCOL instance. + @param[in] Value Number of filters. + @param[in] Length Size of the power management pattern f= ilter data. + @param[in] PatternFilter A pointer to the power management patt= ern filter structure. + + @retval EFI_SUCCESS The request executed successfully. + @retval EFI_TIMEOUT A timeout occurred executing the request. + @retval EFI_DEVICE_ERROR The request failed due to a device error. + @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value= . + @retval EFI_UNSUPPORTED Not supported. + +**/ +EFI_STATUS +EFIAPI +SetUsbEthPowerFilter ( + IN EDKII_USB_ETHERNET_PROTOCOL *This, + IN UINT16 Value, + IN UINT16 Length, + IN VOID *PatternFilter + ) +{ + EFI_USB_DEVICE_REQUEST Request; + UINT32 TransStatus; + USB_ETHERNET_DRIVER *UsbEthDriver; + + UsbEthDriver =3D USB_ETHERNET_DEV_FROM_THIS (This); + + Request.RequestType =3D USB_ETHERNET_SET_REQ_TYPE; + Request.Request =3D SET_ETH_POWER_MANAGEMENT_PATTERN_FILTER_REQ; + Request.Value =3D Value; + Request.Index =3D UsbEthDriver->NumOfInterface; + Request.Length =3D Length; + + return UsbEthDriver->UsbIo->UsbControlTransfer ( + UsbEthDriver->UsbIo, + &Request, + EfiUsbDataOut, + USB_ETHERNET_TRANSFER_TIMEOUT, + PatternFilter, + Length, + &TransStatus + ); +} + +/** + This request retrieves the status of the specified Ethernet power manage= ment + pattern filter from the device. + + @param[in] This A pointer to the EDKII_USB_ETHERNET_P= ROTOCOL instance. + @param[in] Value The filter number. + @param[out] PatternActive A pointer to the pattern active boole= an. + + @retval EFI_SUCCESS The request executed successfully. + @retval EFI_TIMEOUT A timeout occurred executing the request. + @retval EFI_DEVICE_ERROR The request failed due to a device error. + @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value= . + @retval EFI_UNSUPPORTED Not supported. + +**/ +EFI_STATUS +EFIAPI +GetUsbEthPowerFilter ( + IN EDKII_USB_ETHERNET_PROTOCOL *This, + IN UINT16 Value, + OUT BOOLEAN *PatternActive + ) +{ + EFI_USB_DEVICE_REQUEST Request; + UINT32 TransStatus; + USB_ETHERNET_DRIVER *UsbEthDriver; + + UsbEthDriver =3D USB_ETHERNET_DEV_FROM_THIS (This); + + Request.RequestType =3D USB_ETHERNET_GET_REQ_TYPE; + Request.Request =3D GET_ETH_POWER_MANAGEMENT_PATTERN_FILTER_REQ; + Request.Value =3D Value; + Request.Index =3D UsbEthDriver->NumOfInterface; + Request.Length =3D USB_ETH_POWER_FILTER_LENGTH; + + return UsbEthDriver->UsbIo->UsbControlTransfer ( + UsbEthDriver->UsbIo, + &Request, + EfiUsbDataIn, + USB_ETHERNET_TRANSFER_TIMEOUT, + PatternActive, + USB_ETH_POWER_FILTER_LENGTH, + &TransStatus + ); +} + +BIT_MAP gTable[] =3D { + { PXE_OPFLAGS_RECEIVE_FILTER_UNICAST, USB_ETH_PACKET_TYPE_DIR= ECTED }, + { PXE_OPFLAGS_RECEIVE_FILTER_BROADCAST, USB_ETH_PACKET_TYPE_BRO= ADCAST }, + { PXE_OPFLAGS_RECEIVE_FILTER_FILTERED_MULTICAST, USB_ETH_PACKET_TYPE_MUL= TICAST }, + { PXE_OPFLAGS_RECEIVE_FILTER_PROMISCUOUS, USB_ETH_PACKET_TYPE_PRO= MISCUOUS }, + { PXE_OPFLAGS_RECEIVE_FILTER_ALL_MULTICAST, USB_ETH_PACKET_TYPE_ALL= _MULTICAST }, +}; + +/** + Convert value between PXE receive filter and USB ETH packet filter. + + @param[in] Value PXE filter data. + @param[out] CdcFilter A pointer to the Ethernet Packet Filter Bitmap va= lue converted by PXE_OPFLAGS. + +**/ +VOID +ConvertFilter ( + IN UINT16 Value, + OUT UINT16 *CdcFilter + ) +{ + UINT32 Index; + UINT32 Count; + + Count =3D sizeof (gTable)/sizeof (gTable[0]); + + for (Index =3D 0; (gTable[Index].Src !=3D 0) && (Index < Count); Index++= ) { + if (gTable[Index].Src & Value) { + *CdcFilter |=3D gTable[Index].Dst; + } + } +} + +/** + This request is used to configure device Ethernet packet filter settings= . + + @param[in] This A pointer to the EDKII_USB_ETHERNET_PROTOC= OL instance. + @param[in] Value Packet Filter Bitmap. + + @retval EFI_SUCCESS The request executed successfully. + @retval EFI_TIMEOUT A timeout occurred executing the request. + @retval EFI_DEVICE_ERROR The request failed due to a device error. + @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value= . + @retval EFI_UNSUPPORTED Not supported. + +**/ +EFI_STATUS +EFIAPI +SetUsbEthPacketFilter ( + IN EDKII_USB_ETHERNET_PROTOCOL *This, + IN UINT16 Value + ) +{ + EFI_USB_DEVICE_REQUEST Request; + UINT32 TransStatus; + USB_ETHERNET_DRIVER *UsbEthDriver; + UINT16 CommandFilter; + + UsbEthDriver =3D USB_ETHERNET_DEV_FROM_THIS (This); + CommandFilter =3D 0; + + ConvertFilter (Value, &CommandFilter); + + Request.RequestType =3D USB_ETHERNET_SET_REQ_TYPE; + Request.Request =3D SET_ETH_PACKET_FILTER_REQ; + Request.Value =3D CommandFilter; + Request.Index =3D UsbEthDriver->NumOfInterface; + Request.Length =3D USB_ETH_PACKET_FILTER_LENGTH; + + return UsbEthDriver->UsbIo->UsbControlTransfer ( + UsbEthDriver->UsbIo, + &Request, + EfiUsbNoData, + USB_ETHERNET_TRANSFER_TIMEOUT, + NULL, + USB_ETH_PACKET_FILTER_LENGTH, + &TransStatus + ); +} + +/** + This request is used to retrieve a statistic based on the feature select= or. + + @param[in] This A pointer to the EDKII_USB_ETHERNET_PR= OTOCOL instance. + @param[in] FeatureSelector Value of the feature selector. + @param[out] Statistic A pointer to the 32 bit unsigned integ= er. + + @retval EFI_SUCCESS The request executed successfully. + @retval EFI_TIMEOUT A timeout occurred executing the request. + @retval EFI_DEVICE_ERROR The request failed due to a device error. + @retval EFI_INVALID_PARAMETER One of the parameters has an invalid value= . + @retval EFI_UNSUPPORTED Not supported. + +**/ +EFI_STATUS +EFIAPI +GetUsbEthStatistic ( + IN EDKII_USB_ETHERNET_PROTOCOL *This, + IN UINT16 FeatureSelector, + OUT VOID *Statistic + ) +{ + EFI_STATUS Status; + EFI_USB_DEVICE_REQUEST Request; + UINT32 TransStatus; + USB_ETHERNET_FUN_DESCRIPTOR UsbEthFunDescriptor; + USB_ETHERNET_DRIVER *UsbEthDriver; + + UsbEthDriver =3D USB_ETHERNET_DEV_FROM_THIS (This); + + Status =3D This->UsbEthFunDescriptor (This, &UsbEthFunDescriptor); + if (EFI_ERROR (Status)) { + return Status; + } + + if (UsbEthFunDescriptor.EthernetStatistics =3D=3D 0) { + return EFI_UNSUPPORTED; + } + + Request.RequestType =3D USB_ETHERNET_GET_REQ_TYPE; + Request.Request =3D GET_ETH_STATISTIC_REQ; + Request.Value =3D FeatureSelector; + Request.Index =3D UsbEthDriver->NumOfInterface; + Request.Length =3D USB_ETH_STATISTIC; + + return UsbEthDriver->UsbIo->UsbControlTransfer ( + UsbEthDriver->UsbIo, + &Request, + EfiUsbDataIn, + USB_ETHERNET_TRANSFER_TIMEOUT, + Statistic, + USB_ETH_STATISTIC, + &TransStatus + ); +} -- 2.35.1.windows.2 -The information contained in this message may be confidential and propriet= ary to American Megatrends (AMI). This communication is intended to be read= only by the individual or entity to whom it is addressed or by their desig= nee. If the reader of this message is not the intended recipient, you are o= n notice that any distribution of this message, in any form, is strictly pr= ohibited. Please promptly notify the sender by reply e-mail or by telephone= at 770-246-8600, and then delete or destroy all copies of the transmission= .