Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/next/external/cache/sources/hcitools/hciattach_bcm43xx.c
Views: 3959
/*1*2* BlueZ - Bluetooth protocol stack for Linux3*4* Copyright (C) 2014 Intel Corporation. All rights reserved.5*6*7* This program is free software; you can redistribute it and/or modify8* it under the terms of the GNU General Public License as published by9* the Free Software Foundation; either version 2 of the License, or10* (at your option) any later version.11*12* This program is distributed in the hope that it will be useful,13* but WITHOUT ANY WARRANTY; without even the implied warranty of14* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the15* GNU General Public License for more details.16*17* You should have received a copy of the GNU General Public License18* along with this program; if not, write to the Free Software19* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA20*21*/2223#ifdef HAVE_CONFIG_H24#include <config.h>25#endif2627#include <stdio.h>28#include <stdlib.h>29#include <unistd.h>30#include <termios.h>31#include <fcntl.h>32#include <unistd.h>33#include <errno.h>34#include <dirent.h>35#include <time.h>3637#include <bluetooth/bluetooth.h>38#include <bluetooth/hci.h>39#include <bluetooth/hci_lib.h>4041#include "hciattach.h"4243#ifndef FIRMWARE_DIR44#define FIRMWARE_DIR "/etc/firmware"45#endif4647#define FW_EXT ".hcd"4849#define BCM43XX_CLOCK_48 150#define BCM43XX_CLOCK_24 25152#define CMD_SUCCESS 0x005354#define CC_MIN_SIZE 75556#define MIN(X,Y) ((X) < (Y) ? (X) : (Y))5758static int bcm43xx_read_local_name(int fd, char *name, size_t size)59{60unsigned char cmd[] = { HCI_COMMAND_PKT, 0x14, 0x0C, 0x00 };61unsigned char *resp;62unsigned int name_len;6364resp = malloc(size + CC_MIN_SIZE);65if (!resp)66return -1;6768tcflush(fd, TCIOFLUSH);6970if (write(fd, cmd, sizeof(cmd)) != sizeof(cmd)) {71fprintf(stderr, "Failed to write read local name command\n");72goto fail;73}7475if (read_hci_event(fd, resp, size) < CC_MIN_SIZE) {76fprintf(stderr, "Failed to read local name, invalid HCI event\n");77goto fail;78}7980if (resp[4] != cmd[1] || resp[5] != cmd[2] || resp[6] != CMD_SUCCESS) {81fprintf(stderr, "Failed to read local name, command failure\n");82goto fail;83}8485name_len = resp[2] - 1;8687strncpy(name, (char *) &resp[7], MIN(name_len, size));88name[size - 1] = 0;8990free(resp);91return 0;9293fail:94free(resp);95return -1;96}9798static int bcm43xx_reset(int fd)99{100unsigned char cmd[] = { HCI_COMMAND_PKT, 0x03, 0x0C, 0x00 };101unsigned char resp[CC_MIN_SIZE];102103if (write(fd, cmd, sizeof(cmd)) != sizeof(cmd)) {104fprintf(stderr, "Failed to write reset command\n");105return -1;106}107108if (read_hci_event(fd, resp, sizeof(resp)) < CC_MIN_SIZE) {109fprintf(stderr, "Failed to reset chip, invalid HCI event\n");110return -1;111}112113if (resp[4] != cmd[1] || resp[5] != cmd[2] || resp[6] != CMD_SUCCESS) {114fprintf(stderr, "Failed to reset chip, command failure\n");115return -1;116}117118return 0;119}120121static int bcm43xx_set_bdaddr(int fd, const char *bdaddr)122{123unsigned char cmd[] =124{ HCI_COMMAND_PKT, 0x01, 0xfc, 0x06, 0x00, 0x00,1250x00, 0x00, 0x00, 0x00 };126unsigned char resp[CC_MIN_SIZE];127128printf("Set BDADDR UART: %s\n", bdaddr);129130if (str2ba(bdaddr, (bdaddr_t *) (&cmd[4])) < 0) {131fprintf(stderr, "Incorrect bdaddr\n");132return -1;133}134135tcflush(fd, TCIOFLUSH);136137if (write(fd, cmd, sizeof(cmd)) != sizeof(cmd)) {138fprintf(stderr, "Failed to write set bdaddr command\n");139return -1;140}141142if (read_hci_event(fd, resp, sizeof(resp)) < CC_MIN_SIZE) {143fprintf(stderr, "Failed to set bdaddr, invalid HCI event\n");144return -1;145}146147if (resp[4] != cmd[1] || resp[5] != cmd[2] || resp[6] != CMD_SUCCESS) {148fprintf(stderr, "Failed to set bdaddr, command failure\n");149return -1;150}151152return 0;153}154155static int bcm43xx_set_speed(int fd, uint32_t speed)156{157unsigned char cmd[] =158{ HCI_COMMAND_PKT, 0x18, 0xfc, 0x06, 0x00, 0x00,1590x00, 0x00, 0x00, 0x00 };160unsigned char resp[CC_MIN_SIZE];161162printf("Set Controller UART speed to %d bit/s\n", speed);163164cmd[6] = (uint8_t) (speed);165cmd[7] = (uint8_t) (speed >> 8);166cmd[8] = (uint8_t) (speed >> 16);167cmd[9] = (uint8_t) (speed >> 24);168169tcflush(fd, TCIOFLUSH);170171if (write(fd, cmd, sizeof(cmd)) != sizeof(cmd)) {172fprintf(stderr, "Failed to write update baudrate command\n");173return -1;174}175176if (read_hci_event(fd, resp, sizeof(resp)) < CC_MIN_SIZE) {177fprintf(stderr, "Failed to update baudrate, invalid HCI event\n");178return -1;179}180181if (resp[4] != cmd[1] || resp[5] != cmd[2] || resp[6] != CMD_SUCCESS) {182fprintf(stderr, "Failed to update baudrate, command failure\n");183return -1;184}185186return 0;187}188189static int bcm43xx_set_clock(int fd, unsigned char clock)190{191unsigned char cmd[] = { HCI_COMMAND_PKT, 0x45, 0xfc, 0x01, 0x00 };192unsigned char resp[CC_MIN_SIZE];193194printf("Set Controller clock (%d)\n", clock);195196cmd[4] = clock;197198tcflush(fd, TCIOFLUSH);199200if (write(fd, cmd, sizeof(cmd)) != sizeof(cmd)) {201fprintf(stderr, "Failed to write update clock command\n");202return -1;203}204205if (read_hci_event(fd, resp, sizeof(resp)) < CC_MIN_SIZE) {206fprintf(stderr, "Failed to update clock, invalid HCI event\n");207return -1;208}209210if (resp[4] != cmd[1] || resp[5] != cmd[2] || resp[6] != CMD_SUCCESS) {211fprintf(stderr, "Failed to update clock, command failure\n");212return -1;213}214215return 0;216}217218static int bcm43xx_load_firmware(int fd, const char *fw)219{220unsigned char cmd[] = { HCI_COMMAND_PKT, 0x2e, 0xfc, 0x00 };221struct timespec tm_mode = { 0, 50000 };222struct timespec tm_ready = { 0, 2000000 };223unsigned char resp[CC_MIN_SIZE];224unsigned char tx_buf[1024];225int len, fd_fw, n;226227printf("Flash firmware %s\n", fw);228229fd_fw = open(fw, O_RDONLY);230if (fd_fw < 0) {231fprintf(stderr, "Unable to open firmware (%s)\n", fw);232return -1;233}234235tcflush(fd, TCIOFLUSH);236237if (write(fd, cmd, sizeof(cmd)) != sizeof(cmd)) {238fprintf(stderr, "Failed to write download mode command\n");239goto fail;240}241242if (read_hci_event(fd, resp, sizeof(resp)) < CC_MIN_SIZE) {243fprintf(stderr, "Failed to load firmware, invalid HCI event\n");244goto fail;245}246247if (resp[4] != cmd[1] || resp[5] != cmd[2] || resp[6] != CMD_SUCCESS) {248fprintf(stderr, "Failed to load firmware, command failure\n");249goto fail;250}251252/* Wait 50ms to let the firmware placed in download mode */253nanosleep(&tm_mode, NULL);254255tcflush(fd, TCIOFLUSH);256257while ((n = read(fd_fw, &tx_buf[1], 3))) {258if (n < 0) {259fprintf(stderr, "Failed to read firmware\n");260goto fail;261}262263tx_buf[0] = HCI_COMMAND_PKT;264265len = tx_buf[3];266267if (read(fd_fw, &tx_buf[4], len) < 0) {268fprintf(stderr, "Failed to read firmware\n");269goto fail;270}271272if (write(fd, tx_buf, len + 4) != (len + 4)) {273fprintf(stderr, "Failed to write firmware\n");274goto fail;275}276277read_hci_event(fd, resp, sizeof(resp));278tcflush(fd, TCIOFLUSH);279}280281/* Wait for firmware ready */282nanosleep(&tm_ready, NULL);283284close(fd_fw);285return 0;286287fail:288close(fd_fw);289return -1;290}291292static int bcm43xx_locate_patch(const char *dir_name,293const char *chip_name, char *location)294{295DIR *dir;296int ret = -1;297298dir = opendir(dir_name);299if (!dir) {300fprintf(stderr, "Cannot open directory '%s': %s\n",301dir_name, strerror(errno));302return -1;303}304305/* Recursively look for a BCM43XX*.hcd */306while (1) {307struct dirent *entry = readdir(dir);308if (!entry)309break;310311if (entry->d_type & DT_DIR) {312char path[PATH_MAX];313314if (!strcmp(entry->d_name, "..") || !strcmp(entry->d_name, "."))315continue;316317snprintf(path, PATH_MAX, "%s/%s", dir_name, entry->d_name);318319ret = bcm43xx_locate_patch(path, chip_name, location);320if (!ret)321break;322} else if (!strncmp(chip_name, entry->d_name, strlen(chip_name))) {323unsigned int name_len = strlen(entry->d_name);324size_t curs_ext = name_len - sizeof(FW_EXT) + 1;325326if (curs_ext > name_len)327break;328329if (strncmp(FW_EXT, &entry->d_name[curs_ext], sizeof(FW_EXT)))330break;331332/* found */333snprintf(location, PATH_MAX, "%s/%s", dir_name, entry->d_name);334ret = 0;335break;336}337}338339closedir(dir);340341return ret;342}343344int bcm43xx_init(int fd, int speed, struct termios *ti, const char *bdaddr)345{346char chip_name[20];347char fw_path[PATH_MAX];348349printf("bcm43xx_init\n");350351if (bcm43xx_reset(fd))352return -1;353354if (bcm43xx_read_local_name(fd, chip_name, sizeof(chip_name)))355return -1;356357if (bcm43xx_locate_patch(FIRMWARE_DIR, chip_name, fw_path)) {358fprintf(stderr, "Patch not found, continue anyway\n");359} else {360if (bcm43xx_load_firmware(fd, fw_path))361return -1;362363if (bcm43xx_reset(fd))364return -1;365}366367if (bdaddr)368bcm43xx_set_bdaddr(fd, bdaddr);369370if (speed > 3000000 && bcm43xx_set_clock(fd, BCM43XX_CLOCK_48))371return -1;372373if (bcm43xx_set_speed(fd, speed))374return -1;375376return 0;377}378379380