Path: blob/main/sys/netgraph/bluetooth/drivers/ubt/ng_ubt_intel.c
35065 views
/*1* ng_ubt_intel.c2*/34/*-5* SPDX-License-Identifier: BSD-2-Clause6*7* Copyright (c) 2019, 2021 Vladimir Kondratyev <[email protected]>8* Copyright (c) 2023 Future Crew LLC.9*10* Redistribution and use in source and binary forms, with or without11* modification, are permitted provided that the following conditions12* are met:13* 1. Redistributions of source code must retain the above copyright14* notice, this list of conditions and the following disclaimer.15* 2. Redistributions in binary form must reproduce the above copyright16* notice, this list of conditions and the following disclaimer in the17* documentation and/or other materials provided with the distribution.18*19* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND20* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE21* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE22* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE23* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL24* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS25* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)26* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT27* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY28* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF29* SUCH DAMAGE.30*/3132/*33* Attempt to initialize FreeBSD bluetooth stack while Intel Wireless 8260/826534* device is in bootloader mode locks the adapter hardly so it requires power35* on/off cycle to restore. This driver blocks ng_ubt attachment until36* operational firmware is loaded by iwmbtfw utility thus avoiding the lock up.37*/3839#include <sys/types.h>40#include <sys/bus.h>41#include <sys/kernel.h>42#include <sys/mbuf.h>43#include <sys/module.h>44#include <sys/systm.h>45#include <sys/taskqueue.h>4647#include "usbdevs.h"48#include <dev/usb/usb.h>49#include <dev/usb/usbdi.h>5051#include <netgraph/ng_message.h>52#include <netgraph/netgraph.h>53#include <netgraph/ng_parse.h>54#include <netgraph/bluetooth/include/ng_bluetooth.h>55#include <netgraph/bluetooth/include/ng_hci.h>56#include <netgraph/bluetooth/include/ng_ubt.h>57#include <netgraph/bluetooth/drivers/ubt/ng_ubt_var.h>5859#define UBT_INTEL_HCICMD_TIMEOUT 2000 /* ms */60#define UBT_INTEL_TLV_IMAGE_TYPE 0x1c6162enum {63UBT_INTEL_DEVICE_7260,64UBT_INTEL_DEVICE_8260,65UBT_INTEL_DEVICE_9260,66};6768struct ubt_intel_version_rp {69uint8_t status;70uint8_t hw_platform;71uint8_t hw_variant;72uint8_t hw_revision;73uint8_t fw_variant;74uint8_t fw_revision;75uint8_t fw_build_num;76uint8_t fw_build_ww;77uint8_t fw_build_yy;78uint8_t fw_patch_num;79} __attribute__ ((packed));8081static device_probe_t ubt_intel_probe;8283/*84* List of supported bluetooth devices. If you add a new device PID here ensure85* that it is blacklisted in ng_ubt.c and is supported by iwmbtfw utility.86*/8788static const STRUCT_USB_HOST_ID ubt_intel_devs[] =89{90/* Intel Wireless 7260/7265 and successors */91{ USB_VPI(USB_VENDOR_INTEL2, 0x07dc, UBT_INTEL_DEVICE_7260) },92{ USB_VPI(USB_VENDOR_INTEL2, 0x0a2a, UBT_INTEL_DEVICE_7260) },93{ USB_VPI(USB_VENDOR_INTEL2, 0x0aa7, UBT_INTEL_DEVICE_7260) },94/* Intel Wireless 8260/8265 and successors */95{ USB_VPI(USB_VENDOR_INTEL2, 0x0a2b, UBT_INTEL_DEVICE_8260) },96{ USB_VPI(USB_VENDOR_INTEL2, 0x0aaa, UBT_INTEL_DEVICE_8260) },97{ USB_VPI(USB_VENDOR_INTEL2, 0x0025, UBT_INTEL_DEVICE_8260) },98{ USB_VPI(USB_VENDOR_INTEL2, 0x0026, UBT_INTEL_DEVICE_8260) },99{ USB_VPI(USB_VENDOR_INTEL2, 0x0029, UBT_INTEL_DEVICE_8260) },100/* Intel Wireless 9260/9560 and successors */101{ USB_VPI(USB_VENDOR_INTEL2, 0x0032, UBT_INTEL_DEVICE_9260) },102{ USB_VPI(USB_VENDOR_INTEL2, 0x0033, UBT_INTEL_DEVICE_9260) },103};104105/*106* Execute generic HCI command and return response in provided buffer.107*/108109static usb_error_t110ubt_intel_do_hci_request(struct usb_device *udev, uint16_t opcode,111void *resp, uint8_t resp_len)112{113struct ubt_hci_event_command_compl *evt;114struct ubt_hci_cmd cmd;115usb_error_t error;116117memset(&cmd, 0, sizeof(cmd));118cmd.opcode = htole16(opcode);119evt = malloc(offsetof(struct ubt_hci_event_command_compl, data) +120resp_len, M_TEMP, M_ZERO | M_WAITOK);121evt->header.event = NG_HCI_EVENT_COMMAND_COMPL;122evt->header.length = resp_len + UBT_HCI_EVENT_COMPL_HEAD_SIZE;123124error = ubt_do_hci_request(udev, &cmd, evt, UBT_INTEL_HCICMD_TIMEOUT);125if (error != USB_ERR_NORMAL_COMPLETION)126goto exit;127128if (evt->header.length == resp_len + UBT_HCI_EVENT_COMPL_HEAD_SIZE)129memcpy(resp, evt->data, resp_len);130else131error = USB_ERR_INVAL;132exit:133free(evt, M_TEMP);134return (error);135}136137static uint8_t138ubt_intel_get_img_type(struct usb_device *udev)139{140#define UBT_INTEL_MAX_EVT_SIZE 256141static struct ubt_hci_cmd cmd = {142.opcode = htole16(NG_HCI_OPCODE(NG_HCI_OGF_VENDOR, 0x05)),143.length = 1,144.data = { 0xff },145};146struct ubt_hci_event_command_compl *evt;147usb_error_t error;148uint8_t status, datalen, type, len, img_type = 0;149uint8_t *data;150151evt = malloc(UBT_INTEL_MAX_EVT_SIZE, M_TEMP, M_ZERO | M_WAITOK);152evt->header.event = NG_HCI_EVENT_COMMAND_COMPL;153evt->header.length =154UBT_INTEL_MAX_EVT_SIZE - sizeof(struct ubt_hci_evhdr);155156error = ubt_do_hci_request(udev, &cmd, evt, UBT_INTEL_HCICMD_TIMEOUT);157if (error != USB_ERR_NORMAL_COMPLETION)158goto exit;159160datalen = evt->header.length - UBT_HCI_EVENT_COMPL_HEAD_SIZE;161data = evt->data;162status = *data++;163if (status != 0)164goto exit;165datalen--;166167while (datalen >= 2) {168type = *data++;169len = *data++;170datalen -= 2;171if (datalen < len)172break;173if (type == UBT_INTEL_TLV_IMAGE_TYPE && len == 1) {174img_type = *data;175break;176}177datalen -= len;178data += len;179}180exit:181free(evt, M_TEMP);182return (img_type);183}184185/*186* Probe for a Intel Wireless Bluetooth device.187*/188189static int190ubt_intel_probe(device_t dev)191{192struct usb_attach_arg *uaa = device_get_ivars(dev);193struct ubt_intel_version_rp version;194ng_hci_reset_rp reset;195int error;196uint8_t img_type;197198if (uaa->usb_mode != USB_MODE_HOST)199return (ENXIO);200201if (uaa->info.bIfaceIndex != 0)202return (ENXIO);203204error = usbd_lookup_id_by_uaa(ubt_intel_devs, sizeof(ubt_intel_devs),205uaa);206if (error != 0)207return (error);208209switch (USB_GET_DRIVER_INFO(uaa)) {210case UBT_INTEL_DEVICE_7260:211/*212* Send HCI Reset command to workaround controller bug with the213* first HCI command sent to it returning number of completed214* commands as zero. This will reset the number of completed215* commands and allow further normal command processing.216*/217if (ubt_intel_do_hci_request(uaa->device,218NG_HCI_OPCODE(NG_HCI_OGF_HC_BASEBAND, NG_HCI_OCF_RESET),219&reset, sizeof(reset)) != USB_ERR_NORMAL_COMPLETION)220return (ENXIO);221if (reset.status != 0)222return (ENXIO);223/*224* fw_patch_num indicates the version of patch the device225* currently have. If there is no patch data in the device,226* it is always 0x00 and we need to patch the device again.227*/228if (ubt_intel_do_hci_request(uaa->device,229NG_HCI_OPCODE(NG_HCI_OGF_VENDOR, 0x05),230&version, sizeof(version)) != USB_ERR_NORMAL_COMPLETION)231return (ENXIO);232if (version.fw_patch_num == 0)233return (ENXIO);234break;235236case UBT_INTEL_DEVICE_8260:237/*238* Find if the Intel Wireless 8260/8265 device is in bootloader239* mode or is running operational firmware with checking of240* variant byte of "Intel version" HCI command response.241* The value 0x23 identifies the operational firmware.242*/243if (ubt_intel_do_hci_request(uaa->device,244NG_HCI_OPCODE(NG_HCI_OGF_VENDOR, 0x05),245&version, sizeof(version)) != USB_ERR_NORMAL_COMPLETION)246return (ENXIO);247if (version.fw_variant != 0x23)248return (ENXIO);249break;250251case UBT_INTEL_DEVICE_9260:252/*253* Find if the Intel Wireless 9260/9560 device is in bootloader254* mode or is running operational firmware with checking of255* image type byte of "Intel version" HCI command response.256* The value 0x03 identifies the operational firmware.257*/258img_type = ubt_intel_get_img_type(uaa->device);259if (img_type != 0x03)260return (ENXIO);261break;262263default:264KASSERT(0 == 1, ("Unknown DRIVER_INFO"));265}266267return (BUS_PROBE_DEFAULT);268}269270/*271* Module interface. Attach and detach methods, netgraph node type272* registration and PNP string are inherited from ng_ubt.c driver.273*/274275static device_method_t ubt_intel_methods[] =276{277DEVMETHOD(device_probe, ubt_intel_probe),278DEVMETHOD_END279};280281DEFINE_CLASS_1(ubt, ubt_intel_driver, ubt_intel_methods,282sizeof(struct ubt_softc), ubt_driver);283DRIVER_MODULE(ng_ubt_intel, uhub, ubt_intel_driver, 0, 0);284MODULE_VERSION(ng_ubt_intel, NG_BLUETOOTH_VERSION);285MODULE_DEPEND(ng_ubt_intel, netgraph, NG_ABI_VERSION, NG_ABI_VERSION, NG_ABI_VERSION);286MODULE_DEPEND(ng_ubt_intel, ng_hci, NG_BLUETOOTH_VERSION, NG_BLUETOOTH_VERSION, NG_BLUETOOTH_VERSION);287MODULE_DEPEND(ng_ubt_intel, usb, 1, 1, 1);288289290