2 * Copyright (c) 2009-2010 Atheros Communications Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/ioctl.h>
36 #include <bluetooth/bluetooth.h>
37 #include <bluetooth/hci.h>
38 #include <bluetooth/hci_lib.h>
40 #include "hciattach.h"
45 #define FW_PATH "/lib/firmware/ar3k/"
53 struct ps_entry_type {
60 #define HCI_VENDOR_CMD_OGF 0x3F
61 #define HCI_PS_CMD_OCF 0x0B
63 struct ps_cfg_entry ps_list[MAX_TAGS];
65 static void load_hci_ps_hdr(uint8_t *cmd, uint8_t ps_op, int len, int index)
67 hci_command_hdr *ch = (void *)cmd;
69 ch->opcode = htobs(cmd_opcode_pack(HCI_VENDOR_CMD_OGF,
71 ch->plen = len + PS_HDR_LEN;
72 cmd += HCI_COMMAND_HDR_SIZE;
80 #define PS_EVENT_LEN 100
83 * Send HCI command and wait for command complete event.
84 * The event buffer has to be freed by the caller.
86 static int send_hci_cmd_sync(int dev, uint8_t *cmd, int len, uint8_t **event)
90 uint8_t pkt_type = HCI_COMMAND_PKT;
95 if (write(dev, &pkt_type, 1) != 1)
97 if (write(dev, (unsigned char *)cmd, len) != len)
100 hci_event = (uint8_t *)malloc(PS_EVENT_LEN);
104 err = read_hci_event(dev, (unsigned char *)hci_event, PS_EVENT_LEN);
115 #define HCI_EV_SUCCESS 0x00
117 static int read_ps_event(uint8_t *event, uint16_t ocf)
120 uint16_t opcode = htobs(cmd_opcode_pack(HCI_VENDOR_CMD_OGF, ocf));
125 event += HCI_EVENT_HDR_SIZE;
127 if (eh->evt == EVT_CMD_COMPLETE) {
128 evt_cmd_complete *cc = (void *)event;
130 event += EVT_CMD_COMPLETE_SIZE;
132 if (cc->opcode == opcode && event[0] == HCI_EV_SUCCESS)
141 static int write_cmd(int fd, uint8_t *buffer, int len)
146 err = send_hci_cmd_sync(fd, buffer, len, &event);
150 err = read_ps_event(event, HCI_PS_CMD_OCF);
159 #define WRITE_PATCH 8
160 #define ENABLE_PATCH 11
162 #define HCI_PS_CMD_HDR_LEN 7
164 #define PS_RESET_PARAM_LEN 6
165 #define HCI_MAX_CMD_SIZE 260
166 #define PS_RESET_CMD_LEN (HCI_PS_CMD_HDR_LEN + PS_RESET_PARAM_LEN)
168 #define PS_ID_MASK 0xFF
170 /* Sends PS commands using vendor specficic HCI commands */
171 static int write_ps_cmd(int fd, uint8_t opcode, uint32_t ps_param)
173 uint8_t cmd[HCI_MAX_CMD_SIZE];
178 load_hci_ps_hdr(cmd, opcode, 0, 0x00);
180 if (write_cmd(fd, cmd, HCI_PS_CMD_HDR_LEN) < 0)
185 load_hci_ps_hdr(cmd, opcode, PS_RESET_PARAM_LEN, 0x00);
188 cmd[PS_RESET_CMD_LEN - 2] = ps_param & PS_ID_MASK;
189 cmd[PS_RESET_CMD_LEN - 1] = (ps_param >> 8) & PS_ID_MASK;
191 if (write_cmd(fd, cmd, PS_RESET_CMD_LEN) < 0)
196 for (i = 0; i < ps_param; i++) {
197 load_hci_ps_hdr(cmd, opcode, ps_list[i].len,
200 memcpy(&cmd[HCI_PS_CMD_HDR_LEN], ps_list[i].data,
203 if (write_cmd(fd, cmd, ps_list[i].len +
204 HCI_PS_CMD_HDR_LEN) < 0)
213 #define __is_delim(ch) ((ch) == ':')
214 #define MAX_PREAMBLE_LEN 4
216 /* Parse PS entry preamble of format [X:X] for main type and subtype */
217 static int get_ps_type(char *ptr, int index, char *type, char *sub_type)
222 if (index > MAX_PREAMBLE_LEN)
225 for (i = 1; i < index; i++) {
226 if (__is_delim(ptr[i])) {
231 if (isalpha(ptr[i])) {
233 (*type) = toupper(ptr[i]);
235 (*sub_type) = toupper(ptr[i]);
250 static int get_input_format(char *buf, struct ps_entry_type *format)
254 char sub_type = '\0';
256 format->type = PS_HEX;
257 format->array = TRUE;
259 if (strstr(buf, "[") != buf)
262 ptr = strstr(buf, "]");
266 if (get_ps_type(buf, ptr - buf, &type, &sub_type) < 0)
269 /* Check is data type is of array */
270 if (type == ARRAY || sub_type == ARRAY)
271 format->array = TRUE;
273 if (type == STRING || sub_type == STRING)
274 format->array = FALSE;
276 if (type == DECIMAL || type == BINARY)
277 format->type = PS_DEC;
279 format->type = PS_HEX;
284 #define UNDEFINED 0xFFFF
286 static unsigned int read_data_in_section(char *buf, struct ps_entry_type type)
293 if (buf == strstr(buf, "[")) {
294 ptr = strstr(buf, "]");
301 if (type.type == PS_HEX && type.array != TRUE)
302 return strtol(ptr, NULL, 16);
314 static inline int update_char_count(const char *buf)
318 if (strstr(buf, "[") == buf) {
319 end_ptr = strstr(buf, "]");
323 return (end_ptr - buf) + 1;
329 /* Read PS entries as string, convert and add to Hex array */
330 static void update_tag_data(struct ps_cfg_entry *tag,
331 struct tag_info *info, const char *ptr)
337 strncpy(buf, &ptr[info->char_cnt], 2);
338 tag->data[info->byte_count] = strtol(buf, NULL, 16);
342 strncpy(buf, &ptr[info->char_cnt], 2);
343 tag->data[info->byte_count] = strtol(buf, NULL, 16);
353 #define PS_MAX_LEN 500
354 #define LINE_SIZE_MAX (PS_MAX_LEN * 2)
355 #define ENTRY_PER_LINE 16
357 #define __check_comment(buf) (((buf)[0] == '/') && ((buf)[1] == '/'))
358 #define __skip_space(str) while (*(str) == ' ') ((str)++)
360 static int ath_parse_ps(FILE *stream)
362 char buf[LINE_SIZE_MAX + 1];
365 int16_t byte_count = 0;
366 struct ps_entry_type format;
367 struct tag_info status = { 0, 0, 0, 0 };
371 struct ps_cfg_entry *tag;
373 ptr = fgets(buf, LINE_SIZE_MAX, stream);
378 if (__check_comment(ptr))
381 /* Lines with a '#' will be followed by new PS entry */
382 if (ptr == strstr(ptr, "#")) {
383 if (status.section != PS_UNDEF) {
386 status.section = PS_ID;
391 tag = &ps_list[tag_cnt];
393 switch (status.section) {
395 if (get_input_format(ptr, &format) < 0)
398 tag->id = read_data_in_section(ptr, format);
399 status.section = PS_LEN;
403 if (get_input_format(ptr, &format) < 0)
406 byte_count = read_data_in_section(ptr, format);
407 if (byte_count > PS_MAX_LEN)
410 tag->len = byte_count;
411 tag->data = (uint8_t *)malloc(byte_count);
413 status.section = PS_DATA;
414 status.line_count = 0;
418 if (status.line_count == 0)
419 if (get_input_format(ptr, &format) < 0)
424 status.char_cnt = update_char_count(ptr);
426 read_count = (byte_count > ENTRY_PER_LINE) ?
427 ENTRY_PER_LINE : byte_count;
429 if (format.type == PS_HEX && format.array == TRUE) {
430 while (read_count > 0) {
431 update_tag_data(tag, &status, ptr);
435 if (byte_count > ENTRY_PER_LINE)
436 byte_count -= ENTRY_PER_LINE;
444 memset(&status, 0x00, sizeof(struct tag_info));
446 if (status.section == PS_UNDEF)
449 if (tag_cnt == MAX_TAGS)
458 #define MAX_PATCH_CMD 244
461 uint8_t data[MAX_PATCH_CMD];
464 #define SET_PATCH_RAM_ID 0x0D
465 #define SET_PATCH_RAM_CMD_SIZE 11
466 #define ADDRESS_LEN 4
467 static int set_patch_ram(int dev, char *patch_loc, int len)
474 uint8_t *loc_ptr = &cmd[7];
481 load_hci_ps_hdr(cmd, SET_PATCH_RAM_ID, ADDRESS_LEN, 0);
483 for (i = 0, j = 3; i < 4; i++, j--) {
484 loc_byte[0] = patch_loc[0];
485 loc_byte[1] = patch_loc[1];
486 loc_ptr[j] = strtol(loc_byte, NULL, 16);
490 err = send_hci_cmd_sync(dev, cmd, SET_PATCH_RAM_CMD_SIZE, &event);
494 err = read_ps_event(event, HCI_PS_CMD_OCF);
501 #define PATCH_LOC_KEY "DA:"
502 #define PATCH_LOC_STRING_LEN 8
503 static int ps_patch_download(int fd, FILE *stream)
506 char ptr[MAX_PATCH_CMD + 1];
509 char patch_loc[PATCH_LOC_STRING_LEN + 1];
513 while (fgets(ptr, MAX_PATCH_CMD, stream)) {
514 if (strlen(ptr) <= 1)
516 else if (strstr(ptr, PATCH_LOC_KEY) == ptr) {
517 strncpy(patch_loc, &ptr[sizeof(PATCH_LOC_KEY) - 1],
518 PATCH_LOC_STRING_LEN);
519 if (set_patch_ram(fd, patch_loc, sizeof(patch_loc)) < 0)
521 } else if (isxdigit(ptr[0]))
527 byte_cnt = strtol(ptr, NULL, 16);
529 while (byte_cnt > 0) {
531 uint8_t cmd[HCI_MAX_CMD_SIZE];
532 struct patch_entry patch;
534 if (byte_cnt > MAX_PATCH_CMD)
535 patch.len = MAX_PATCH_CMD;
537 patch.len = byte_cnt;
539 for (i = 0; i < patch.len; i++) {
540 if (!fgets(byte, 3, stream))
543 patch.data[i] = strtoul(byte, NULL, 16);
546 load_hci_ps_hdr(cmd, WRITE_PATCH, patch.len, patch_count);
547 memcpy(&cmd[HCI_PS_CMD_HDR_LEN], patch.data, patch.len);
549 if (write_cmd(fd, cmd, patch.len + HCI_PS_CMD_HDR_LEN) < 0)
553 byte_cnt = byte_cnt - MAX_PATCH_CMD;
556 if (write_ps_cmd(fd, ENABLE_PATCH, 0) < 0)
562 #define PS_RAM_SIZE 2048
564 static int ps_config_download(int fd, int tag_count)
566 if (write_ps_cmd(fd, PS_RESET, PS_RAM_SIZE) < 0)
570 if (write_ps_cmd(fd, PS_WRITE, tag_count) < 0)
575 #define PS_ASIC_FILE "PS_ASIC.pst"
576 #define PS_FPGA_FILE "PS_FPGA.pst"
578 static void get_ps_file_name(uint32_t devtype, uint32_t rom_version,
583 if (devtype == 0xdeadc0de)
584 filename = PS_ASIC_FILE;
586 filename = PS_FPGA_FILE;
588 snprintf(path, MAXPATHLEN, "%s%x/%s", FW_PATH, rom_version, filename);
591 #define PATCH_FILE "RamPatch.txt"
592 #define FPGA_ROM_VERSION 0x99999999
593 #define ROM_DEV_TYPE 0xdeadc0de
595 static void get_patch_file_name(uint32_t dev_type, uint32_t rom_version,
596 uint32_t build_version, char *path)
598 if (rom_version == FPGA_ROM_VERSION && dev_type != ROM_DEV_TYPE &&
599 dev_type != 0 && build_version == 1)
602 snprintf(path, MAXPATHLEN, "%s%x/%s",
603 FW_PATH, rom_version, PATCH_FILE);
608 #define PATCH_REGION 2
610 static int get_ath3k_crc(int dev)
616 load_hci_ps_hdr(cmd, VERIFY_CRC, 0, PS_REGION | PATCH_REGION);
618 err = send_hci_cmd_sync(dev, cmd, sizeof(cmd), &event);
621 /* Send error code if CRC check patched */
622 if (read_ps_event(event, HCI_PS_CMD_OCF) >= 0)
630 #define DEV_REGISTER 0x4FFC
631 #define GET_DEV_TYPE_OCF 0x05
633 static int get_device_type(int dev, uint32_t *code)
640 hci_command_hdr *ch = (void *)cmd;
642 ch->opcode = htobs(cmd_opcode_pack(HCI_VENDOR_CMD_OGF,
645 ptr += HCI_COMMAND_HDR_SIZE;
647 ptr[0] = (uint8_t)DEV_REGISTER;
648 ptr[1] = (uint8_t)DEV_REGISTER >> 8;
649 ptr[2] = (uint8_t)DEV_REGISTER >> 16;
650 ptr[3] = (uint8_t)DEV_REGISTER >> 24;
653 err = send_hci_cmd_sync(dev, cmd, sizeof(cmd), &event);
657 err = read_ps_event(event, GET_DEV_TYPE_OCF);
662 reg = (reg << 8) | event[9];
663 reg = (reg << 8) | event[8];
664 reg = (reg << 8) | event[7];
673 #define GET_VERSION_OCF 0x1E
675 static int read_ath3k_version(int pConfig, uint32_t *rom_version,
676 uint32_t *build_version)
682 hci_command_hdr *ch = (void *)cmd;
684 ch->opcode = htobs(cmd_opcode_pack(HCI_VENDOR_CMD_OGF,
688 err = send_hci_cmd_sync(pConfig, cmd, sizeof(cmd), &event);
692 err = read_ps_event(event, GET_VERSION_OCF);
697 status = (status << 8) | event[9];
698 status = (status << 8) | event[8];
699 status = (status << 8) | event[7];
700 *rom_version = status;
703 status = (status << 8) | event[13];
704 status = (status << 8) | event[12];
705 status = (status << 8) | event[11];
706 *build_version = status;
714 static void convert_bdaddr(char *str_bdaddr, char *bdaddr)
717 char *str_byte = str_bdaddr;
719 int colon_present = 0;
721 if (strstr(str_bdaddr, ":"))
726 /* Reverse the BDADDR to LSB first */
727 for (i = 0, j = 5; i < 6; i++, j--) {
728 bdbyte[0] = str_byte[0];
729 bdbyte[1] = str_byte[1];
730 bdaddr[j] = strtol(bdbyte, NULL, 16);
732 if (colon_present == 1)
739 static int write_bdaddr(int pConfig, char *bdaddr)
745 hci_command_hdr *ch = (void *)cmd;
747 memset(cmd, 0, sizeof(cmd));
749 ch->opcode = htobs(cmd_opcode_pack(HCI_VENDOR_CMD_OGF,
752 ptr += HCI_COMMAND_HDR_SIZE;
759 convert_bdaddr(bdaddr, (char *)&ptr[4]);
761 err = send_hci_cmd_sync(pConfig, cmd, sizeof(cmd), &event);
765 err = read_ps_event(event, HCI_PS_CMD_OCF);
772 #define BDADDR_FILE "ar3kbdaddr.pst"
774 static void write_bdaddr_from_file(int rom_version, int fd)
777 char bdaddr[PATH_MAX];
778 char bdaddr_file[PATH_MAX];
780 snprintf(bdaddr_file, MAXPATHLEN, "%s%x/%s",
781 FW_PATH, rom_version, BDADDR_FILE);
783 stream = fopen(bdaddr_file, "r");
787 if (fgets(bdaddr, PATH_MAX - 1, stream))
788 write_bdaddr(fd, bdaddr);
793 static int ath_ps_download(int fd)
798 uint32_t rom_version = 0;
799 uint32_t build_version = 0;
800 uint32_t dev_type = 0;
801 char patch_file[PATH_MAX];
802 char ps_file[PATH_MAX];
806 * Verfiy firmware version. depending on it select the PS
807 * config file to download.
809 if (get_device_type(fd, &dev_type) < 0) {
811 goto download_cmplete;
814 if (read_ath3k_version(fd, &rom_version, &build_version) < 0) {
816 goto download_cmplete;
819 /* Do not download configuration if CRC passes */
820 if (get_ath3k_crc(fd) < 0) {
822 goto download_cmplete;
825 get_ps_file_name(dev_type, rom_version, ps_file);
826 get_patch_file_name(dev_type, rom_version, build_version, patch_file);
828 stream = fopen(ps_file, "r");
830 perror("firmware file open error\n");
832 goto download_cmplete;
834 tag_count = ath_parse_ps(stream);
840 goto download_cmplete;
844 * It is not necessary that Patch file be available,
845 * continue with PS Operations if patch file is not available.
847 if (patch_file[0] == '\0')
850 stream = fopen(patch_file, "r");
854 patch_count = ps_patch_download(fd, stream);
857 if (patch_count < 0) {
859 goto download_cmplete;
863 err = ps_config_download(fd, tag_count);
867 write_bdaddr_from_file(rom_version, fd);
872 #define HCI_SLEEP_CMD_OCF 0x04
875 * Atheros AR300x specific initialization post callback
877 int ath3k_post(int fd, int pm)
880 struct timespec tm = { 0, 50000 };
884 dev_id = ioctl(fd, HCIUARTGETDEVICE, 0);
886 perror("cannot get device id");
890 dd = hci_open_dev(dev_id);
892 perror("HCI device open failed");
896 if (ioctl(dd, HCIDEVUP, dev_id) < 0 && errno != EALREADY) {
897 perror("hci down:Power management Disabled");
902 /* send vendor specific command with Sleep feature Enabled */
903 if (hci_send_cmd(dd, OGF_VENDOR_CMD, HCI_SLEEP_CMD_OCF, 1, &pm) < 0)
904 perror("PM command failed, power management Disabled");
906 nanosleep(&tm, NULL);
912 #define HCI_VENDOR_CMD_OGF 0x3F
913 #define HCI_PS_CMD_OCF 0x0B
914 #define HCI_CHG_BAUD_CMD_OCF 0x0C
916 #define WRITE_BDADDR_CMD_LEN 14
917 #define WRITE_BAUD_CMD_LEN 6
918 #define MAX_CMD_LEN WRITE_BDADDR_CMD_LEN
920 static int set_cntrlr_baud(int fd, int speed)
923 struct timespec tm = { 0, 500000 };
924 unsigned char cmd[MAX_CMD_LEN], rsp[HCI_MAX_EVENT_SIZE];
925 unsigned char *ptr = cmd + 1;
926 hci_command_hdr *ch = (void *)ptr;
928 cmd[0] = HCI_COMMAND_PKT;
930 /* set controller baud rate to user specified value */
932 ch->opcode = htobs(cmd_opcode_pack(HCI_VENDOR_CMD_OGF,
933 HCI_CHG_BAUD_CMD_OCF));
935 ptr += HCI_COMMAND_HDR_SIZE;
939 ptr[1] = (char)(baud >> 8);
941 if (write(fd, cmd, WRITE_BAUD_CMD_LEN) != WRITE_BAUD_CMD_LEN) {
942 perror("Failed to write change baud rate command");
946 nanosleep(&tm, NULL);
948 if (read_hci_event(fd, rsp, sizeof(rsp)) < 0)
955 * Atheros AR300x specific initialization and configuration file
958 int ath3k_init(int fd, int speed, int init_speed, char *bdaddr,
963 struct timespec tm = { 0, 500000 };
964 unsigned char cmd[MAX_CMD_LEN], rsp[HCI_MAX_EVENT_SIZE];
965 unsigned char *ptr = cmd + 1;
966 hci_command_hdr *ch = (void *)ptr;
968 cmd[0] = HCI_COMMAND_PKT;
970 /* set both controller and host baud rate to maximum possible value */
971 err = set_cntrlr_baud(fd, speed);
975 err = set_speed(fd, ti, speed);
977 perror("Can't set required baud rate");
981 /* Download PS and patch */
982 r = ath_ps_download(fd);
984 perror("Failed to Download configuration");
991 ch->opcode = htobs(cmd_opcode_pack(HCI_VENDOR_CMD_OGF,
994 ptr += HCI_COMMAND_HDR_SIZE;
1000 str2ba(bdaddr, (bdaddr_t *)(ptr + 4));
1002 if (write(fd, cmd, WRITE_BDADDR_CMD_LEN) !=
1003 WRITE_BDADDR_CMD_LEN) {
1004 perror("Failed to write BD_ADDR command\n");
1009 if (read_hci_event(fd, rsp, sizeof(rsp)) < 0) {
1010 perror("Failed to set BD_ADDR\n");
1016 /* Send HCI Reset */
1021 r = write(fd, cmd, 4);
1027 nanosleep(&tm, NULL);
1028 if (read_hci_event(fd, rsp, sizeof(rsp)) < 0) {
1033 err = set_cntrlr_baud(fd, speed);
1039 set_cntrlr_baud(fd, init_speed);
1040 set_speed(fd, ti, init_speed);