summaryrefslogtreecommitdiff
path: root/drivers/net/wireless/ath6kl/miscdrv/ar3kconfig.c
blob: 44ae924ef134f96d9e43f9903abb57401a3a3b75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
//------------------------------------------------------------------------------
// <copyright file="ar3kconfig.c" company="Atheros">
//    Copyright (c) 2009 Atheros Corporation.  All rights reserved.
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation;
//
// Software distributed under the License is distributed on an "AS
// IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
// implied. See the License for the specific language governing
// rights and limitations under the License.
//
//
//------------------------------------------------------------------------------
//==============================================================================
// AR3K configuration implementation
//
// Author(s): ="Atheros"
//==============================================================================

#include "a_config.h"
#include "athdefs.h"
#include "a_types.h"
#include "a_osapi.h"
#define ATH_MODULE_NAME misc
#include "a_debug.h"
#include "common_drv.h"
#ifdef EXPORT_HCI_BRIDGE_INTERFACE
#include "export_hci_transport.h"
#else
#include "hci_transport_api.h"
#endif
#include "ar3kconfig.h"

#define BAUD_CHANGE_COMMAND_STATUS_OFFSET   5
#define HCI_EVENT_RESP_TIMEOUTMS            3000
#define HCI_CMD_OPCODE_BYTE_LOW_OFFSET      0
#define HCI_CMD_OPCODE_BYTE_HI_OFFSET       1
#define HCI_EVENT_OPCODE_BYTE_LOW           3
#define HCI_EVENT_OPCODE_BYTE_HI            4
#define HCI_CMD_COMPLETE_EVENT_CODE         0xE
#define HCI_MAX_EVT_RECV_LENGTH             257
#define EXIT_MIN_BOOT_COMMAND_STATUS_OFFSET  5

A_STATUS AthPSInitialize(AR3K_CONFIG_INFO *hdev);

static A_STATUS SendHCICommand(AR3K_CONFIG_INFO *pConfig,
                               A_UINT8          *pBuffer,
                               int              Length)
{
    HTC_PACKET  *pPacket = NULL;
    A_STATUS    status = A_OK;
       
    do {   
        
        pPacket = (HTC_PACKET *)A_MALLOC(sizeof(HTC_PACKET));     
        if (NULL == pPacket) {
            status = A_NO_MEMORY;
            break;    
        }       
        
        A_MEMZERO(pPacket,sizeof(HTC_PACKET));      
        SET_HTC_PACKET_INFO_TX(pPacket,
                               NULL,
                               pBuffer, 
                               Length,
                               HCI_COMMAND_TYPE, 
                               AR6K_CONTROL_PKT_TAG);
        
            /* issue synchronously */                                      
        status = HCI_TransportSendPkt(pConfig->pHCIDev,pPacket,TRUE);   
        
    } while (FALSE);
   
    if (pPacket != NULL) {
        A_FREE(pPacket);
    }
        
    return status;
}

static A_STATUS RecvHCIEvent(AR3K_CONFIG_INFO *pConfig,
                             A_UINT8          *pBuffer,
                             int              *pLength)
{
    A_STATUS    status = A_OK; 
    HTC_PACKET  *pRecvPacket = NULL;
    
    do {
                 
        pRecvPacket = (HTC_PACKET *)A_MALLOC(sizeof(HTC_PACKET));
        if (NULL == pRecvPacket) {
            status = A_NO_MEMORY;
            AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("Failed to alloc HTC struct \n"));
            break;    
        }     
        
        A_MEMZERO(pRecvPacket,sizeof(HTC_PACKET)); 
         
        SET_HTC_PACKET_INFO_RX_REFILL(pRecvPacket,NULL,pBuffer,*pLength,HCI_EVENT_TYPE);
        
        status = HCI_TransportRecvHCIEventSync(pConfig->pHCIDev,
                                               pRecvPacket,
                                               HCI_EVENT_RESP_TIMEOUTMS);
        if (A_FAILED(status)) {
            break;    
        }

        *pLength = pRecvPacket->ActualLength;
        
    } while (FALSE);
       
    if (pRecvPacket != NULL) {
        A_FREE(pRecvPacket);    
    }
    
    return status;
} 
    
A_STATUS SendHCICommandWaitCommandComplete(AR3K_CONFIG_INFO *pConfig,
                                           A_UINT8          *pHCICommand,
                                           int              CmdLength,
                                           A_UINT8          **ppEventBuffer,
                                           A_UINT8          **ppBufferToFree)
{
    A_STATUS    status = A_OK;   
    A_UINT8     *pBuffer = NULL;
    A_UINT8     *pTemp;
    int         length;
    A_BOOL      commandComplete = FALSE;
    A_UINT8     opCodeBytes[2];
                               
    do {
        
        length = max(HCI_MAX_EVT_RECV_LENGTH,CmdLength);
        length += pConfig->pHCIProps->HeadRoom + pConfig->pHCIProps->TailRoom;
        length += pConfig->pHCIProps->IOBlockPad;
                                     
        pBuffer = (A_UINT8 *)A_MALLOC(length);        
        if (NULL == pBuffer) {
            AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("AR3K Config: Failed to allocate bt buffer \n"));
            status = A_NO_MEMORY;
            break;    
        }
        
            /* get the opcodes to check the command complete event */
        opCodeBytes[0] = pHCICommand[HCI_CMD_OPCODE_BYTE_LOW_OFFSET];
        opCodeBytes[1] = pHCICommand[HCI_CMD_OPCODE_BYTE_HI_OFFSET];
        
            /* copy HCI command */
        A_MEMCPY(pBuffer + pConfig->pHCIProps->HeadRoom,pHCICommand,CmdLength);         
            /* send command */
        status = SendHCICommand(pConfig,
                                pBuffer + pConfig->pHCIProps->HeadRoom,
                                CmdLength);
        if (A_FAILED(status)) {
            AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("AR3K Config: Failed to send HCI Command (%d) \n", status));
            AR_DEBUG_PRINTBUF(pHCICommand,CmdLength,"HCI Bridge Failed HCI Command");
            break;    
        }   
        
            /* reuse buffer to capture command complete event */
        A_MEMZERO(pBuffer,length);
        status = RecvHCIEvent(pConfig,pBuffer,&length);        
        if (A_FAILED(status)) {
            AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("AR3K Config: HCI event recv failed \n"));
            AR_DEBUG_PRINTBUF(pHCICommand,CmdLength,"HCI Bridge Failed HCI Command");
            break;    
        }
        
        pTemp = pBuffer + pConfig->pHCIProps->HeadRoom;        
        if (pTemp[0] == HCI_CMD_COMPLETE_EVENT_CODE) {
            if ((pTemp[HCI_EVENT_OPCODE_BYTE_LOW] == opCodeBytes[0]) &&
                (pTemp[HCI_EVENT_OPCODE_BYTE_HI] == opCodeBytes[1])) {
                commandComplete = TRUE;    
            }
        }
        
        if (!commandComplete) {
            AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("AR3K Config: Unexpected HCI event : %d \n",pTemp[0]));
            AR_DEBUG_PRINTBUF(pTemp,pTemp[1],"Unexpected HCI event");
            status = A_ECOMM;
            break;    
        }       
        
        if (ppEventBuffer != NULL) {
                /* caller wants to look at the event */
            *ppEventBuffer = pTemp;
            if (ppBufferToFree == NULL) {
                status = A_EINVAL;
                break;        
            }
                /* caller must free the buffer */
            *ppBufferToFree = pBuffer;
            pBuffer = NULL;            
        }
        
    } while (FALSE);

    if (pBuffer != NULL) {
        A_FREE(pBuffer);    
    }
    
    return status;    
}

static A_STATUS AR3KConfigureHCIBaud(AR3K_CONFIG_INFO *pConfig)
{
    A_STATUS    status = A_OK;
    A_UINT8     hciBaudChangeCommand[] =  {0x0c,0xfc,0x2,0,0};
    A_UINT16    baudVal; 
    A_UINT8     *pEvent = NULL;
    A_UINT8     *pBufferToFree = NULL;
    
    do {
        
        if (pConfig->Flags & AR3K_CONFIG_FLAG_SET_AR3K_BAUD) {
            baudVal = (A_UINT16)(pConfig->AR3KBaudRate / 100);
            hciBaudChangeCommand[3] = (A_UINT8)baudVal;
            hciBaudChangeCommand[4] = (A_UINT8)(baudVal >> 8);
            
            status = SendHCICommandWaitCommandComplete(pConfig,
                                                       hciBaudChangeCommand,
                                                       sizeof(hciBaudChangeCommand),
                                                       &pEvent,
                                                       &pBufferToFree);          
            if (A_FAILED(status)) {
                AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("AR3K Config: Baud rate change failed! \n"));  
                break;    
            }
            
            if (pEvent[BAUD_CHANGE_COMMAND_STATUS_OFFSET] != 0) {
                AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
                    ("AR3K Config: Baud change command event status failed: %d \n", 
                                pEvent[BAUD_CHANGE_COMMAND_STATUS_OFFSET]));
                status = A_ECOMM; 
                break;           
            } 
            
            AR_DEBUG_PRINTF(ATH_DEBUG_ANY,
                    ("AR3K Config: Baud Changed to %d \n",pConfig->AR3KBaudRate));  
        }
        
        if (pConfig->Flags & AR3K_CONFIG_FLAG_AR3K_BAUD_CHANGE_DELAY) {
                /* some versions of AR3K do not switch baud immediately, up to 300MS */
            A_MDELAY(325);
        }
        
        if (pConfig->Flags & AR3K_CONFIG_FLAG_SET_AR6K_SCALE_STEP) {
            /* Tell target to change UART baud rate for AR6K */
            status = HCI_TransportSetBaudRate(pConfig->pHCIDev, pConfig->AR3KBaudRate);

            if (A_FAILED(status)) {
                AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
                    ("AR3K Config: failed to set scale and step values: %d \n", status));
                break;    
            }
    
            AR_DEBUG_PRINTF(ATH_DEBUG_ANY,
                    ("AR3K Config: Baud changed to %d for AR6K\n", pConfig->AR3KBaudRate));            
        }
                
    } while (FALSE);
                        
    if (pBufferToFree != NULL) {
        A_FREE(pBufferToFree);    
    }
        
    return status;
}

static A_STATUS AR3KExitMinBoot(AR3K_CONFIG_INFO *pConfig)
{
    A_STATUS  status;
    A_CHAR    exitMinBootCmd[] = {0x25,0xFC,0x0c,0x03,0x00,0x00,0x00,0x00,0x00,0x00,
                                  0x00,0x00,0x00,0x00,0x00};
    A_UINT8   *pEvent = NULL;
    A_UINT8   *pBufferToFree = NULL;
    
    status = SendHCICommandWaitCommandComplete(pConfig,
                                               exitMinBootCmd,
                                               sizeof(exitMinBootCmd),
                                               &pEvent,
                                               &pBufferToFree);
    
    if (A_SUCCESS(status)) {
        if (pEvent[EXIT_MIN_BOOT_COMMAND_STATUS_OFFSET] != 0) {
            AR_DEBUG_PRINTF(ATH_DEBUG_ERR,
                ("AR3K Config: MinBoot exit command event status failed: %d \n", 
                            pEvent[EXIT_MIN_BOOT_COMMAND_STATUS_OFFSET]));
            status = A_ECOMM;            
        } else {
            AR_DEBUG_PRINTF(ATH_DEBUG_INFO, 
                                ("AR3K Config: MinBoot Exit Command Complete (Success) \n"));
            A_MDELAY(1);
        }
    } else {
        AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("AR3K Config: MinBoot Exit Failed! \n"));    
    }
    
    if (pBufferToFree != NULL) {
        A_FREE(pBufferToFree);    
    }
    
    return status;                                              
}
                                 
static A_STATUS AR3KConfigureSendHCIReset(AR3K_CONFIG_INFO *pConfig)
{
    A_STATUS status = A_OK;
    A_UINT8 hciResetCommand[] = {0x03,0x0c,0x0};
    A_UINT8 *pEvent = NULL;
    A_UINT8 *pBufferToFree = NULL;

    status = SendHCICommandWaitCommandComplete( pConfig,
                                                hciResetCommand,
                                                sizeof(hciResetCommand),
                                                &pEvent,
                                                &pBufferToFree );

    if (A_FAILED(status)) {
        AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("AR3K Config: HCI reset failed! \n"));
    }

    if (pBufferToFree != NULL) {
        A_FREE(pBufferToFree);
    }

    return status;
}

A_STATUS AR3KConfigure(AR3K_CONFIG_INFO *pConfig)
{
    A_STATUS        status = A_OK; 
        
    AR_DEBUG_PRINTF(ATH_DEBUG_INFO,("AR3K Config: Configuring AR3K ...\n"));
                                
    do {
        
        if ((pConfig->pHCIDev == NULL) || (pConfig->pHCIProps == NULL) || (pConfig->pHIFDevice == NULL)) {
            status = A_EINVAL;
            break;    
        }
        
            /* disable asynchronous recv while we issue commands and receive events synchronously */
        status = HCI_TransportEnableDisableAsyncRecv(pConfig->pHCIDev,FALSE);
        if (A_FAILED(status)) {
            break;    
        }
      
        if (pConfig->Flags & AR3K_CONFIG_FLAG_FORCE_MINBOOT_EXIT) {
            status =  AR3KExitMinBoot(pConfig);   
            if (A_FAILED(status)) {
                break;    
            }    
        }
        
        if (pConfig->Flags & 
                (AR3K_CONFIG_FLAG_SET_AR3K_BAUD | AR3K_CONFIG_FLAG_SET_AR6K_SCALE_STEP)) {
            status = AR3KConfigureHCIBaud(pConfig);      
            if (A_FAILED(status)) {
                break;    
            }
        }

        /* Load patching and PST file if available*/
        if (A_OK != AthPSInitialize(pConfig)) {
            AR_DEBUG_PRINTF(ATH_DEBUG_ERR,("Patch Download Failed!\n"));
        }

        /* Send HCI reset to make PS tags take effect*/
        AR3KConfigureSendHCIReset(pConfig);
               
           /* re-enable asynchronous recv */
        status = HCI_TransportEnableDisableAsyncRecv(pConfig->pHCIDev,TRUE);
        if (A_FAILED(status)) {
            break;    
        }     
    
    
    } while (FALSE);   
    
  
    AR_DEBUG_PRINTF(ATH_DEBUG_INFO,("AR3K Config: Configuration Complete (status = %d) \n",status));
    
    return status;
}

A_STATUS AR3KConfigureExit(void *config)
{
    A_STATUS        status = A_OK; 
    AR3K_CONFIG_INFO *pConfig = (AR3K_CONFIG_INFO *)config;
        
    AR_DEBUG_PRINTF(ATH_DEBUG_INFO,("AR3K Config: Cleaning up AR3K ...\n"));
                                
    do {
        
        if ((pConfig->pHCIDev == NULL) || (pConfig->pHCIProps == NULL) || (pConfig->pHIFDevice == NULL)) {
            status = A_EINVAL;
            break;    
        }
        
            /* disable asynchronous recv while we issue commands and receive events synchronously */
        status = HCI_TransportEnableDisableAsyncRecv(pConfig->pHCIDev,FALSE);
        if (A_FAILED(status)) {
            break;    
        }
      
        if (pConfig->Flags & 
                (AR3K_CONFIG_FLAG_SET_AR3K_BAUD | AR3K_CONFIG_FLAG_SET_AR6K_SCALE_STEP)) {
            status = AR3KConfigureHCIBaud(pConfig);      
            if (A_FAILED(status)) {
                break;    
            }
        }

           /* re-enable asynchronous recv */
        status = HCI_TransportEnableDisableAsyncRecv(pConfig->pHCIDev,TRUE);
        if (A_FAILED(status)) {
            break;    
        }     
    
    
    } while (FALSE);   
    
  
    AR_DEBUG_PRINTF(ATH_DEBUG_INFO,("AR3K Config: Cleanup Complete (status = %d) \n",status));
    
    return status;
}