ath6kl: remove-typedef ST_PS_DATA_FORMAT
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / ath6kl / miscdrv / ar3kps / ar3kpsparser.c
blob7c9d4a80ee0d3f779c5445ccd764bf8bfee7d9ba
1 /*
2 * Copyright (c) 2004-2010 Atheros Communications Inc.
3 * All rights reserved.
5 * This file implements the Atheros PS and patch parser.
6 * It implements APIs to parse data buffer with patch and PS information and convert it to HCI commands.
10 * ar3kpsparser.c
14 * The software source and binaries included in this development package are
15 * licensed, not sold. You, or your company, received the package under one
16 * or more license agreements. The rights granted to you are specifically
17 * listed in these license agreement(s). All other rights remain with Atheros
18 * Communications, Inc., its subsidiaries, or the respective owner including
19 * those listed on the included copyright notices.. Distribution of any
20 * portion of this package must be in strict compliance with the license
21 * agreement(s) terms.
28 #include "ar3kpsparser.h"
30 #include <linux/ctype.h>
31 #include <linux/kernel.h>
33 #define BD_ADDR_SIZE 6
34 #define WRITE_PATCH 8
35 #define ENABLE_PATCH 11
36 #define PS_RESET 2
37 #define PS_WRITE 1
38 #define PS_VERIFY_CRC 9
39 #define CHANGE_BDADDR 15
41 #define HCI_COMMAND_HEADER 7
43 #define HCI_EVENT_SIZE 7
45 #define WRITE_PATCH_COMMAND_STATUS_OFFSET 5
47 #define PS_RAM_SIZE 2048
49 #define RAM_PS_REGION (1<<0)
50 #define RAM_PATCH_REGION (1<<1)
51 #define RAMPS_MAX_PS_DATA_PER_TAG 20000
52 #define MAX_RADIO_CFG_TABLE_SIZE 244
53 #define RAMPS_MAX_PS_TAGS_PER_FILE 50
55 #define PS_MAX_LEN 500
56 #define LINE_SIZE_MAX (PS_MAX_LEN *2)
58 /* Constant values used by parser */
59 #define BYTES_OF_PS_DATA_PER_LINE 16
60 #define RAMPS_MAX_PS_DATA_PER_TAG 20000
63 /* Number pf PS/Patch entries in an HCI packet */
64 #define MAX_BYTE_LENGTH 244
66 #define SKIP_BLANKS(str) while (*str == ' ') str++
68 enum MinBootFileFormatE
70 MB_FILEFORMAT_RADIOTBL,
71 MB_FILEFORMAT_PATCH,
72 MB_FILEFORMAT_COEXCONFIG
75 enum RamPsSection
77 RAM_PS_SECTION,
78 RAM_PATCH_SECTION,
79 RAM_DYN_MEM_SECTION
82 enum eType {
83 eHex,
84 edecimal
88 typedef struct tPsTagEntry
90 u32 TagId;
91 u32 TagLen;
92 u8 *TagData;
93 } tPsTagEntry, *tpPsTagEntry;
95 typedef struct tRamPatch
97 u16 Len;
98 u8 *Data;
99 } tRamPatch, *ptRamPatch;
103 struct st_ps_data_format {
104 enum eType eDataType;
105 bool bIsArray;
108 typedef struct ST_READ_STATUS {
109 unsigned uTagID;
110 unsigned uSection;
111 unsigned uLineCount;
112 unsigned uCharCount;
113 unsigned uByteCount;
114 }ST_READ_STATUS;
117 /* Stores the number of PS Tags */
118 static u32 Tag_Count = 0;
120 /* Stores the number of patch commands */
121 static u32 Patch_Count = 0;
122 static u32 Total_tag_lenght = 0;
123 bool BDADDR = false;
124 u32 StartTagId;
126 tPsTagEntry PsTagEntry[RAMPS_MAX_PS_TAGS_PER_FILE];
127 tRamPatch RamPatch[MAX_NUM_PATCH_ENTRY];
130 int AthParseFilesUnified(u8 *srcbuffer,u32 srclen, int FileFormat);
131 char AthReadChar(u8 *buffer, u32 len,u32 *pos);
132 char *AthGetLine(char *buffer, int maxlen, u8 *srcbuffer,u32 len,u32 *pos);
133 static int AthPSCreateHCICommand(u8 Opcode, u32 Param1,struct ps_cmd_packet *PSPatchPacket,u32 *index);
135 /* Function to reads the next character from the input buffer */
136 char AthReadChar(u8 *buffer, u32 len,u32 *pos)
138 char Ch;
139 if(buffer == NULL || *pos >=len )
141 return '\0';
142 } else {
143 Ch = buffer[*pos];
144 (*pos)++;
145 return Ch;
148 /* PS parser helper function */
149 unsigned int uGetInputDataFormat(char *pCharLine, struct st_ps_data_format *pstFormat)
151 if(pCharLine[0] != '[') {
152 pstFormat->eDataType = eHex;
153 pstFormat->bIsArray = true;
154 return 0;
156 switch(pCharLine[1]) {
157 case 'H':
158 case 'h':
159 if(pCharLine[2]==':') {
160 if((pCharLine[3]== 'a') || (pCharLine[3]== 'A')) {
161 if(pCharLine[4] == ']') {
162 pstFormat->eDataType = eHex;
163 pstFormat->bIsArray = true;
164 pCharLine += 5;
165 return 0;
167 else {
168 AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("Illegal Data format\n")); //[H:A
169 return 1;
172 if((pCharLine[3]== 'S') || (pCharLine[3]== 's')) {
173 if(pCharLine[4] == ']') {
174 pstFormat->eDataType = eHex;
175 pstFormat->bIsArray = false;
176 pCharLine += 5;
177 return 0;
179 else {
180 AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("Illegal Data format\n")); //[H:A
181 return 1;
184 else if(pCharLine[3] == ']') { //[H:]
185 pstFormat->eDataType = eHex;
186 pstFormat->bIsArray = true;
187 pCharLine += 4;
188 return 0;
190 else { //[H:
191 AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("Illegal Data format\n"));
192 return 1;
195 else if(pCharLine[2]==']') { //[H]
196 pstFormat->eDataType = eHex;
197 pstFormat->bIsArray = true;
198 pCharLine += 3;
199 return 0;
201 else { //[H
202 AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("Illegal Data format\n"));
203 return 1;
205 break;
207 case 'A':
208 case 'a':
209 if(pCharLine[2]==':') {
210 if((pCharLine[3]== 'h') || (pCharLine[3]== 'H')) {
211 if(pCharLine[4] == ']') {
212 pstFormat->eDataType = eHex;
213 pstFormat->bIsArray = true;
214 pCharLine += 5;
215 return 0;
217 else {
218 AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("Illegal Data format 1\n")); //[A:H
219 return 1;
222 else if(pCharLine[3]== ']') { //[A:]
223 pstFormat->eDataType = eHex;
224 pstFormat->bIsArray = true;
225 pCharLine += 4;
226 return 0;
228 else { //[A:
229 AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("Illegal Data format 2\n"));
230 return 1;
233 else if(pCharLine[2]==']') { //[H]
234 pstFormat->eDataType = eHex;
235 pstFormat->bIsArray = true;
236 pCharLine += 3;
237 return 0;
239 else { //[H
240 AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("Illegal Data format 3\n"));
241 return 1;
243 break;
245 case 'S':
246 case 's':
247 if(pCharLine[2]==':') {
248 if((pCharLine[3]== 'h') || (pCharLine[3]== 'H')) {
249 if(pCharLine[4] == ']') {
250 pstFormat->eDataType = eHex;
251 pstFormat->bIsArray = true;
252 pCharLine += 5;
253 return 0;
255 else {
256 AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("Illegal Data format 5\n")); //[A:H
257 return 1;
260 else if(pCharLine[3]== ']') { //[A:]
261 pstFormat->eDataType = eHex;
262 pstFormat->bIsArray = true;
263 pCharLine += 4;
264 return 0;
266 else { //[A:
267 AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("Illegal Data format 6\n"));
268 return 1;
271 else if(pCharLine[2]==']') { //[H]
272 pstFormat->eDataType = eHex;
273 pstFormat->bIsArray = true;
274 pCharLine += 3;
275 return 0;
277 else { //[H
278 AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("Illegal Data format 7\n"));
279 return 1;
281 break;
283 default:
284 AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("Illegal Data format 8\n"));
285 return 1;
289 unsigned int uReadDataInSection(char *pCharLine, struct st_ps_data_format stPS_DataFormat)
291 char *pTokenPtr = pCharLine;
293 if(pTokenPtr[0] == '[') {
294 while(pTokenPtr[0] != ']' && pTokenPtr[0] != '\0') {
295 pTokenPtr++;
297 if(pTokenPtr[0] == '\0') {
298 return (0x0FFF);
300 pTokenPtr++;
304 if(stPS_DataFormat.eDataType == eHex) {
305 if(stPS_DataFormat.bIsArray == true) {
306 //Not implemented
307 return (0x0FFF);
309 else {
310 return (A_STRTOL(pTokenPtr, NULL, 16));
313 else {
314 //Not implemented
315 return (0x0FFF);
318 int AthParseFilesUnified(u8 *srcbuffer,u32 srclen, int FileFormat)
320 char *Buffer;
321 char *pCharLine;
322 u8 TagCount;
323 u16 ByteCount;
324 u8 ParseSection=RAM_PS_SECTION;
325 u32 pos;
329 int uReadCount;
330 struct st_ps_data_format stPS_DataFormat;
331 ST_READ_STATUS stReadStatus = {0, 0, 0,0};
332 pos = 0;
333 Buffer = NULL;
335 if (srcbuffer == NULL || srclen == 0)
337 AR_DEBUG_PRINTF(ATH_DEBUG_ERR, ("Could not open .\n"));
338 return A_ERROR;
340 TagCount = 0;
341 ByteCount = 0;
342 Buffer = A_MALLOC(LINE_SIZE_MAX + 1);
343 if(NULL == Buffer) {
344 return A_ERROR;
346 if (FileFormat == MB_FILEFORMAT_PATCH)
348 int LineRead = 0;
349 while((pCharLine = AthGetLine(Buffer, LINE_SIZE_MAX, srcbuffer,srclen,&pos)) != NULL)
352 SKIP_BLANKS(pCharLine);
354 // Comment line or empty line
355 if ((pCharLine[0] == '/') && (pCharLine[1] == '/'))
357 continue;
360 if ((pCharLine[0] == '#')) {
361 if (stReadStatus.uSection != 0)
363 AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("error\n"));
364 if(Buffer != NULL) {
365 A_FREE(Buffer);
367 return A_ERROR;
369 else {
370 stReadStatus.uSection = 1;
371 continue;
374 if ((pCharLine[0] == '/') && (pCharLine[1] == '*'))
376 pCharLine+=2;
377 SKIP_BLANKS(pCharLine);
379 if(!strncmp(pCharLine,"PA",2)||!strncmp(pCharLine,"Pa",2)||!strncmp(pCharLine,"pa",2))
380 ParseSection=RAM_PATCH_SECTION;
382 if(!strncmp(pCharLine,"DY",2)||!strncmp(pCharLine,"Dy",2)||!strncmp(pCharLine,"dy",2))
383 ParseSection=RAM_DYN_MEM_SECTION;
385 if(!strncmp(pCharLine,"PS",2)||!strncmp(pCharLine,"Ps",2)||!strncmp(pCharLine,"ps",2))
386 ParseSection=RAM_PS_SECTION;
388 LineRead = 0;
389 stReadStatus.uSection = 0;
391 continue;
394 switch(ParseSection)
396 case RAM_PS_SECTION:
398 if (stReadStatus.uSection == 1) //TagID
400 SKIP_BLANKS(pCharLine);
401 if(uGetInputDataFormat(pCharLine, &stPS_DataFormat)) {
402 AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("uGetInputDataFormat fail\n"));
403 if(Buffer != NULL) {
404 A_FREE(Buffer);
406 return A_ERROR;
408 //pCharLine +=5;
409 PsTagEntry[TagCount].TagId = uReadDataInSection(pCharLine, stPS_DataFormat);
410 AR_DEBUG_PRINTF(ATH_DEBUG_ERR,(" TAG ID %d \n",PsTagEntry[TagCount].TagId));
412 //AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("tag # %x\n", PsTagEntry[TagCount].TagId);
413 if (TagCount == 0)
415 StartTagId = PsTagEntry[TagCount].TagId;
417 stReadStatus.uSection = 2;
419 else if (stReadStatus.uSection == 2) //TagLength
422 if(uGetInputDataFormat(pCharLine, &stPS_DataFormat)) {
423 AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("uGetInputDataFormat fail \n"));
424 if(Buffer != NULL) {
425 A_FREE(Buffer);
427 return A_ERROR;
429 //pCharLine +=5;
430 ByteCount = uReadDataInSection(pCharLine, stPS_DataFormat);
432 //AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("tag length %x\n", ByteCount));
433 if (ByteCount > LINE_SIZE_MAX/2)
435 if(Buffer != NULL) {
436 A_FREE(Buffer);
438 return A_ERROR;
440 PsTagEntry[TagCount].TagLen = ByteCount;
441 PsTagEntry[TagCount].TagData = (u8 *)A_MALLOC(ByteCount);
442 AR_DEBUG_PRINTF(ATH_DEBUG_ERR,(" TAG Length %d Tag Index %d \n",PsTagEntry[TagCount].TagLen,TagCount));
443 stReadStatus.uSection = 3;
444 stReadStatus.uLineCount = 0;
446 else if( stReadStatus.uSection == 3) { //Data
448 if(stReadStatus.uLineCount == 0) {
449 if(uGetInputDataFormat(pCharLine,&stPS_DataFormat)) {
450 AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("uGetInputDataFormat Fail\n"));
451 if(Buffer != NULL) {
452 A_FREE(Buffer);
454 return A_ERROR;
456 //pCharLine +=5;
458 SKIP_BLANKS(pCharLine);
459 stReadStatus.uCharCount = 0;
460 if(pCharLine[stReadStatus.uCharCount] == '[') {
461 while(pCharLine[stReadStatus.uCharCount] != ']' && pCharLine[stReadStatus.uCharCount] != '\0' ) {
462 stReadStatus.uCharCount++;
464 if(pCharLine[stReadStatus.uCharCount] == ']' ) {
465 stReadStatus.uCharCount++;
466 } else {
467 stReadStatus.uCharCount = 0;
470 uReadCount = (ByteCount > BYTES_OF_PS_DATA_PER_LINE)? BYTES_OF_PS_DATA_PER_LINE: ByteCount;
471 //AR_DEBUG_PRINTF(ATH_DEBUG_ERR,(" "));
472 if((stPS_DataFormat.eDataType == eHex) && stPS_DataFormat.bIsArray == true) {
473 while(uReadCount > 0) {
474 PsTagEntry[TagCount].TagData[stReadStatus.uByteCount] =
475 (u8)(hex_to_bin(pCharLine[stReadStatus.uCharCount]) << 4)
476 | (u8)(hex_to_bin(pCharLine[stReadStatus.uCharCount + 1]));
478 PsTagEntry[TagCount].TagData[stReadStatus.uByteCount+1] =
479 (u8)(hex_to_bin(pCharLine[stReadStatus.uCharCount + 3]) << 4)
480 | (u8)(hex_to_bin(pCharLine[stReadStatus.uCharCount + 4]));
482 stReadStatus.uCharCount += 6; // read two bytes, plus a space;
483 stReadStatus.uByteCount += 2;
484 uReadCount -= 2;
486 if(ByteCount > BYTES_OF_PS_DATA_PER_LINE) {
487 ByteCount -= BYTES_OF_PS_DATA_PER_LINE;
489 else {
490 ByteCount = 0;
493 else {
494 //to be implemented
497 stReadStatus.uLineCount++;
499 if(ByteCount == 0) {
500 stReadStatus.uSection = 0;
501 stReadStatus.uCharCount = 0;
502 stReadStatus.uLineCount = 0;
503 stReadStatus.uByteCount = 0;
505 else {
506 stReadStatus.uCharCount = 0;
509 if((stReadStatus.uSection == 0)&&(++TagCount == RAMPS_MAX_PS_TAGS_PER_FILE))
511 AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("\n Buffer over flow PS File too big!!!"));
512 if(Buffer != NULL) {
513 A_FREE(Buffer);
515 return A_ERROR;
516 //Sleep (3000);
517 //exit(1);
523 break;
524 default:
526 if(Buffer != NULL) {
527 A_FREE(Buffer);
529 return A_ERROR;
531 break;
533 LineRead++;
535 Tag_Count = TagCount;
536 AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("Number of Tags %d\n", Tag_Count));
540 if (TagCount > RAMPS_MAX_PS_TAGS_PER_FILE)
543 if(Buffer != NULL) {
544 A_FREE(Buffer);
546 return A_ERROR;
549 if(Buffer != NULL) {
550 A_FREE(Buffer);
552 return 0;
558 /********************/
561 int GetNextTwoChar(u8 *srcbuffer,u32 len, u32 *pos, char *buffer)
563 unsigned char ch;
565 ch = AthReadChar(srcbuffer,len,pos);
566 if(ch != '\0' && isxdigit(ch)) {
567 buffer[0] = ch;
568 } else
570 return A_ERROR;
572 ch = AthReadChar(srcbuffer,len,pos);
573 if(ch != '\0' && isxdigit(ch)) {
574 buffer[1] = ch;
575 } else
577 return A_ERROR;
579 return 0;
582 int AthDoParsePatch(u8 *patchbuffer, u32 patchlen)
585 char Byte[3];
586 char Line[MAX_BYTE_LENGTH + 1];
587 int ByteCount,ByteCount_Org;
588 int count;
589 int i,j,k;
590 int data;
591 u32 filepos;
592 Byte[2] = '\0';
593 j = 0;
594 filepos = 0;
595 Patch_Count = 0;
597 while(NULL != AthGetLine(Line,MAX_BYTE_LENGTH,patchbuffer,patchlen,&filepos)) {
598 if(strlen(Line) <= 1 || !isxdigit(Line[0])) {
599 continue;
600 } else {
601 break;
604 ByteCount = A_STRTOL(Line, NULL, 16);
605 ByteCount_Org = ByteCount;
607 while(ByteCount > MAX_BYTE_LENGTH){
609 /* Handle case when the number of patch buffer is more than the 20K */
610 if(MAX_NUM_PATCH_ENTRY == Patch_Count) {
611 for(i = 0; i < Patch_Count; i++) {
612 A_FREE(RamPatch[i].Data);
614 return A_ERROR;
616 RamPatch[Patch_Count].Len= MAX_BYTE_LENGTH;
617 RamPatch[Patch_Count].Data = (u8 *)A_MALLOC(MAX_BYTE_LENGTH);
618 Patch_Count ++;
621 ByteCount= ByteCount - MAX_BYTE_LENGTH;
624 RamPatch[Patch_Count].Len= (ByteCount & 0xFF);
625 if(ByteCount != 0) {
626 RamPatch[Patch_Count].Data = (u8 *)A_MALLOC(ByteCount);
627 Patch_Count ++;
629 count = 0;
630 while(ByteCount_Org > MAX_BYTE_LENGTH){
631 AR_DEBUG_PRINTF(ATH_DEBUG_ERR,(" Index [%d]\n",j));
632 for (i = 0,k=0; i < MAX_BYTE_LENGTH*2; i += 2,k++,count +=2) {
633 if(GetNextTwoChar(patchbuffer,patchlen,&filepos,Byte) == A_ERROR) {
634 return A_ERROR;
636 data = A_STRTOUL(&Byte[0], NULL, 16);
637 RamPatch[j].Data[k] = (data & 0xFF);
641 j++;
642 ByteCount_Org = ByteCount_Org - MAX_BYTE_LENGTH;
644 if(j == 0){
645 j++;
647 AR_DEBUG_PRINTF(ATH_DEBUG_ERR,(" Index [%d]\n",j));
648 for (k=0; k < ByteCount_Org; i += 2,k++,count+=2) {
649 if(GetNextTwoChar(patchbuffer,patchlen,&filepos,Byte) == A_ERROR) {
650 return A_ERROR;
652 data = A_STRTOUL(Byte, NULL, 16);
653 RamPatch[j].Data[k] = (data & 0xFF);
657 return 0;
661 /********************/
662 int AthDoParsePS(u8 *srcbuffer, u32 srclen)
664 int status;
665 int i;
666 bool BDADDR_Present = false;
668 Tag_Count = 0;
670 Total_tag_lenght = 0;
671 BDADDR = false;
674 status = A_ERROR;
676 if(NULL != srcbuffer && srclen != 0)
678 AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("File Open Operation Successful\n"));
680 status = AthParseFilesUnified(srcbuffer,srclen,MB_FILEFORMAT_PATCH);
685 if(Tag_Count == 0){
686 Total_tag_lenght = 10;
689 else{
690 for(i=0; i<Tag_Count; i++){
691 if(PsTagEntry[i].TagId == 1){
692 BDADDR_Present = true;
693 AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("BD ADDR is present in Patch File \r\n"));
696 if(PsTagEntry[i].TagLen % 2 == 1){
697 Total_tag_lenght = Total_tag_lenght + PsTagEntry[i].TagLen + 1;
699 else{
700 Total_tag_lenght = Total_tag_lenght + PsTagEntry[i].TagLen;
706 if(Tag_Count > 0 && !BDADDR_Present){
707 AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("BD ADDR is not present adding 10 extra bytes \r\n"));
708 Total_tag_lenght=Total_tag_lenght + 10;
710 Total_tag_lenght = Total_tag_lenght+ 10 + (Tag_Count*4);
711 AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("** Total Length %d\n",Total_tag_lenght));
714 return status;
716 char *AthGetLine(char *buffer, int maxlen, u8 *srcbuffer,u32 len,u32 *pos)
719 int count;
720 static short flag;
721 char CharRead;
722 count = 0;
723 flag = A_ERROR;
727 CharRead = AthReadChar(srcbuffer,len,pos);
728 if( CharRead == '\0' ) {
729 buffer[count+1] = '\0';
730 if(count == 0) {
731 return NULL;
733 else {
734 return buffer;
738 if(CharRead == 13) {
739 } else if(CharRead == 10) {
740 buffer[count] ='\0';
741 flag = A_ERROR;
742 return buffer;
743 }else {
744 buffer[count++] = CharRead;
748 while(count < maxlen-1 && CharRead != '\0');
749 buffer[count] = '\0';
751 return buffer;
754 static void LoadHeader(u8 *HCI_PS_Command,u8 opcode,int length,int index){
756 HCI_PS_Command[0]= 0x0B;
757 HCI_PS_Command[1]= 0xFC;
758 HCI_PS_Command[2]= length + 4;
759 HCI_PS_Command[3]= opcode;
760 HCI_PS_Command[4]= (index & 0xFF);
761 HCI_PS_Command[5]= ((index>>8) & 0xFF);
762 HCI_PS_Command[6]= length;
765 /////////////////////////
767 int AthCreateCommandList(struct ps_cmd_packet **HciPacketList, u32 *numPackets)
770 u8 count;
771 u32 NumcmdEntry = 0;
773 u32 Crc = 0;
774 *numPackets = 0;
777 if(Patch_Count > 0)
778 Crc |= RAM_PATCH_REGION;
779 if(Tag_Count > 0)
780 Crc |= RAM_PS_REGION;
781 AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("PS Thread Started CRC %x Patch Count %d Tag Count %d \n",Crc,Patch_Count,Tag_Count));
783 if(Patch_Count || Tag_Count ){
784 NumcmdEntry+=(2 + Patch_Count + Tag_Count); /* CRC Packet + PS Reset Packet + Patch List + PS List*/
785 if(Patch_Count > 0) {
786 NumcmdEntry++; /* Patch Enable Command */
788 AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("Num Cmd Entries %d Size %d \r\n",NumcmdEntry,(u32)sizeof(struct ps_cmd_packet) * NumcmdEntry));
789 (*HciPacketList) = A_MALLOC(sizeof(struct ps_cmd_packet) * NumcmdEntry);
790 if(NULL == *HciPacketList) {
791 AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("memory allocation failed \r\n"));
793 AthPSCreateHCICommand(PS_VERIFY_CRC,Crc,*HciPacketList,numPackets);
794 if(Patch_Count > 0){
795 AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("*** Write Patch**** \r\n"));
796 AthPSCreateHCICommand(WRITE_PATCH,Patch_Count,*HciPacketList,numPackets);
797 AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("*** Enable Patch**** \r\n"));
798 AthPSCreateHCICommand(ENABLE_PATCH,0,*HciPacketList,numPackets);
801 AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("*** PS Reset**** %d[0x%x] \r\n",PS_RAM_SIZE,PS_RAM_SIZE));
802 AthPSCreateHCICommand(PS_RESET,PS_RAM_SIZE,*HciPacketList,numPackets);
803 if(Tag_Count > 0){
804 AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("*** PS Write**** \r\n"));
805 AthPSCreateHCICommand(PS_WRITE,Tag_Count,*HciPacketList,numPackets);
808 if(!BDADDR){
809 AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("BD ADDR not present \r\n"));
812 for(count = 0; count < Patch_Count; count++) {
814 AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("Freeing Patch Buffer %d \r\n",count));
815 A_FREE(RamPatch[Patch_Count].Data);
818 for(count = 0; count < Tag_Count; count++) {
820 AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("Freeing PS Buffer %d \r\n",count));
821 A_FREE(PsTagEntry[count].TagData);
825 * SDIO Transport uses synchronous mode of data transfer
826 * So, AthPSOperations() call returns only after receiving the
827 * command complete event.
829 return *numPackets;
833 ////////////////////////
835 /////////////
836 static int AthPSCreateHCICommand(u8 Opcode, u32 Param1,struct ps_cmd_packet *PSPatchPacket,u32 *index)
838 u8 *HCI_PS_Command;
839 u32 Length;
840 int i,j;
842 switch(Opcode)
844 case WRITE_PATCH:
847 for(i=0;i< Param1;i++){
849 HCI_PS_Command = (u8 *) A_MALLOC(RamPatch[i].Len+HCI_COMMAND_HEADER);
850 AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("Allocated Buffer Size %d\n",RamPatch[i].Len+HCI_COMMAND_HEADER));
851 if(HCI_PS_Command == NULL){
852 AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("MALLOC Failed\r\n"));
853 return A_ERROR;
855 memset (HCI_PS_Command, 0, RamPatch[i].Len+HCI_COMMAND_HEADER);
856 LoadHeader(HCI_PS_Command,Opcode,RamPatch[i].Len,i);
857 for(j=0;j<RamPatch[i].Len;j++){
858 HCI_PS_Command[HCI_COMMAND_HEADER+j]=RamPatch[i].Data[j];
860 PSPatchPacket[*index].Hcipacket = HCI_PS_Command;
861 PSPatchPacket[*index].packetLen = RamPatch[i].Len+HCI_COMMAND_HEADER;
862 (*index)++;
867 break;
869 case ENABLE_PATCH:
872 Length = 0;
873 i= 0;
874 HCI_PS_Command = (u8 *) A_MALLOC(Length+HCI_COMMAND_HEADER);
875 if(HCI_PS_Command == NULL){
876 AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("MALLOC Failed\r\n"));
877 return A_ERROR;
880 memset (HCI_PS_Command, 0, Length+HCI_COMMAND_HEADER);
881 LoadHeader(HCI_PS_Command,Opcode,Length,i);
882 PSPatchPacket[*index].Hcipacket = HCI_PS_Command;
883 PSPatchPacket[*index].packetLen = Length+HCI_COMMAND_HEADER;
884 (*index)++;
886 break;
888 case PS_RESET:
889 Length = 0x06;
890 i=0;
891 HCI_PS_Command = (u8 *) A_MALLOC(Length+HCI_COMMAND_HEADER);
892 if(HCI_PS_Command == NULL){
893 AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("MALLOC Failed\r\n"));
894 return A_ERROR;
896 memset (HCI_PS_Command, 0, Length+HCI_COMMAND_HEADER);
897 LoadHeader(HCI_PS_Command,Opcode,Length,i);
898 HCI_PS_Command[7]= 0x00;
899 HCI_PS_Command[Length+HCI_COMMAND_HEADER -2]= (Param1 & 0xFF);
900 HCI_PS_Command[Length+HCI_COMMAND_HEADER -1]= ((Param1 >> 8) & 0xFF);
901 PSPatchPacket[*index].Hcipacket = HCI_PS_Command;
902 PSPatchPacket[*index].packetLen = Length+HCI_COMMAND_HEADER;
903 (*index)++;
905 break;
907 case PS_WRITE:
908 for(i=0;i< Param1;i++){
909 if(PsTagEntry[i].TagId ==1)
910 BDADDR = true;
912 HCI_PS_Command = (u8 *) A_MALLOC(PsTagEntry[i].TagLen+HCI_COMMAND_HEADER);
913 if(HCI_PS_Command == NULL){
914 AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("MALLOC Failed\r\n"));
915 return A_ERROR;
918 memset (HCI_PS_Command, 0, PsTagEntry[i].TagLen+HCI_COMMAND_HEADER);
919 LoadHeader(HCI_PS_Command,Opcode,PsTagEntry[i].TagLen,PsTagEntry[i].TagId);
921 for(j=0;j<PsTagEntry[i].TagLen;j++){
922 HCI_PS_Command[HCI_COMMAND_HEADER+j]=PsTagEntry[i].TagData[j];
925 PSPatchPacket[*index].Hcipacket = HCI_PS_Command;
926 PSPatchPacket[*index].packetLen = PsTagEntry[i].TagLen+HCI_COMMAND_HEADER;
927 (*index)++;
931 break;
934 case PS_VERIFY_CRC:
935 Length = 0x0;
937 AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("VALUE of CRC:%d At index %d\r\n",Param1,*index));
939 HCI_PS_Command = (u8 *) A_MALLOC(Length+HCI_COMMAND_HEADER);
940 if(HCI_PS_Command == NULL){
941 AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("MALLOC Failed\r\n"));
942 return A_ERROR;
944 memset (HCI_PS_Command, 0, Length+HCI_COMMAND_HEADER);
945 LoadHeader(HCI_PS_Command,Opcode,Length,Param1);
947 PSPatchPacket[*index].Hcipacket = HCI_PS_Command;
948 PSPatchPacket[*index].packetLen = Length+HCI_COMMAND_HEADER;
949 (*index)++;
951 break;
953 case CHANGE_BDADDR:
954 break;
956 return 0;
958 int AthFreeCommandList(struct ps_cmd_packet **HciPacketList, u32 numPackets)
960 int i;
961 if(*HciPacketList == NULL) {
962 return A_ERROR;
964 for(i = 0; i < numPackets;i++) {
965 A_FREE((*HciPacketList)[i].Hcipacket);
967 A_FREE(*HciPacketList);
968 return 0;