Merge remote-tracking branch 'lsk/v3.10/topic/gic-workaround' into linux-linaro-lsk
[firefly-linux-kernel-4.4.55.git] / drivers / staging / vt6656 / firmware.c
1 /*
2  * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
3  * All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  *
20  * File: baseband.c
21  *
22  * Purpose: Implement functions to access baseband
23  *
24  * Author: Yiching Chen
25  *
26  * Date: May 20, 2004
27  *
28  * Functions:
29  *
30  * Revision History:
31  *
32  */
33
34 #include "firmware.h"
35 #include "control.h"
36 #include "rndis.h"
37
38 static int          msglevel                =MSG_LEVEL_INFO;
39 //static int          msglevel                =MSG_LEVEL_DEBUG;
40
41 #define FIRMWARE_VERSION        0x133           /* version 1.51 */
42 #define FIRMWARE_NAME           "vntwusb.fw"
43
44 #define FIRMWARE_CHUNK_SIZE     0x400
45
46 int FIRMWAREbDownload(struct vnt_private *pDevice)
47 {
48         struct device *dev = &pDevice->usb->dev;
49         const struct firmware *fw;
50         int NdisStatus;
51         void *pBuffer = NULL;
52         bool result = false;
53         u16 wLength;
54         int ii, rc;
55
56         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---->Download firmware\n");
57         spin_unlock_irq(&pDevice->lock);
58
59         rc = request_firmware(&fw, FIRMWARE_NAME, dev);
60         if (rc) {
61                 dev_err(dev, "firmware file %s request failed (%d)\n",
62                         FIRMWARE_NAME, rc);
63                         goto out;
64         }
65
66         pBuffer = kmalloc(FIRMWARE_CHUNK_SIZE, GFP_KERNEL);
67         if (!pBuffer)
68                 goto out;
69
70         for (ii = 0; ii < fw->size; ii += FIRMWARE_CHUNK_SIZE) {
71                 wLength = min_t(int, fw->size - ii, FIRMWARE_CHUNK_SIZE);
72                 memcpy(pBuffer, fw->data + ii, wLength);
73
74                 NdisStatus = CONTROLnsRequestOutAsyn(pDevice,
75                                             0,
76                                             0x1200+ii,
77                                             0x0000,
78                                             wLength,
79                                             pBuffer
80                                             );
81
82                 DBG_PRT(MSG_LEVEL_DEBUG,
83                         KERN_INFO"Download firmware...%d %zu\n", ii, fw->size);
84                 if (NdisStatus != STATUS_SUCCESS)
85                         goto free_fw;
86         }
87
88         result = true;
89 free_fw:
90         release_firmware(fw);
91
92 out:
93         kfree(pBuffer);
94
95         spin_lock_irq(&pDevice->lock);
96         return result;
97 }
98 MODULE_FIRMWARE(FIRMWARE_NAME);
99
100 int FIRMWAREbBrach2Sram(struct vnt_private *pDevice)
101 {
102         int NdisStatus;
103
104     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---->Branch to Sram\n");
105
106     NdisStatus = CONTROLnsRequestOut(pDevice,
107                                     1,
108                                     0x1200,
109                                     0x0000,
110                                     0,
111                                     NULL
112                                     );
113
114     if (NdisStatus != STATUS_SUCCESS) {
115         return (false);
116     } else {
117         return (true);
118     }
119 }
120
121 int FIRMWAREbCheckVersion(struct vnt_private *pDevice)
122 {
123         int ntStatus;
124
125     ntStatus = CONTROLnsRequestIn(pDevice,
126                                     MESSAGE_TYPE_READ,
127                                     0,
128                                     MESSAGE_REQUEST_VERSION,
129                                     2,
130                                     (u8 *) &(pDevice->wFirmwareVersion));
131
132     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Firmware Version [%04x]\n", pDevice->wFirmwareVersion);
133     if (ntStatus != STATUS_SUCCESS) {
134         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Firmware Invalid.\n");
135         return false;
136     }
137     if (pDevice->wFirmwareVersion == 0xFFFF) {
138         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"In Loader.\n");
139         return false;
140     }
141     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Firmware Version [%04x]\n", pDevice->wFirmwareVersion);
142     if (pDevice->wFirmwareVersion < FIRMWARE_VERSION) {
143         // branch to loader for download new firmware
144         FIRMWAREbBrach2Sram(pDevice);
145         return false;
146     }
147     return true;
148 }