cb4a65af754d4ecd645b1f523e26456d29e94079
[cascardo/linux.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
39 static int          msglevel                =MSG_LEVEL_INFO;
40 //static int          msglevel                =MSG_LEVEL_DEBUG;
41
42 #define FIRMWARE_VERSION        0x133           /* version 1.51 */
43 #define FIRMWARE_NAME           "vntwusb.fw"
44
45 #define FIRMWARE_CHUNK_SIZE     0x400
46
47
48
49
50
51
52
53 int FIRMWAREbDownload(struct vnt_private *pDevice)
54 {
55         struct device *dev = &pDevice->usb->dev;
56         const struct firmware *fw;
57         int NdisStatus;
58         void *pBuffer = NULL;
59         bool result = false;
60         u16 wLength;
61         int ii, rc;
62
63
64         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---->Download firmware\n");
65         spin_unlock_irq(&pDevice->lock);
66
67         rc = request_firmware(&fw, FIRMWARE_NAME, dev);
68         if (rc) {
69                 dev_err(dev, "firmware file %s request failed (%d)\n",
70                         FIRMWARE_NAME, rc);
71                         goto out;
72         }
73
74         pBuffer = kmalloc(FIRMWARE_CHUNK_SIZE, GFP_KERNEL);
75         if (!pBuffer)
76                 goto out;
77
78         for (ii = 0; ii < fw->size; ii += FIRMWARE_CHUNK_SIZE) {
79                 wLength = min_t(int, fw->size - ii, FIRMWARE_CHUNK_SIZE);
80                 memcpy(pBuffer, fw->data + ii, wLength);
81
82                 NdisStatus = CONTROLnsRequestOutAsyn(pDevice,
83                                             0,
84                                             0x1200+ii,
85                                             0x0000,
86                                             wLength,
87                                             pBuffer
88                                             );
89
90                 DBG_PRT(MSG_LEVEL_DEBUG,
91                         KERN_INFO"Download firmware...%d %zu\n", ii, fw->size);
92                 if (NdisStatus != STATUS_SUCCESS)
93                         goto free_fw;
94         }
95
96         result = true;
97 free_fw:
98         release_firmware(fw);
99
100 out:
101         kfree(pBuffer);
102
103         spin_lock_irq(&pDevice->lock);
104         return result;
105 }
106 MODULE_FIRMWARE(FIRMWARE_NAME);
107
108 int FIRMWAREbBrach2Sram(struct vnt_private *pDevice)
109 {
110         int NdisStatus;
111
112     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"---->Branch to Sram\n");
113
114     NdisStatus = CONTROLnsRequestOut(pDevice,
115                                     1,
116                                     0x1200,
117                                     0x0000,
118                                     0,
119                                     NULL
120                                     );
121
122     if (NdisStatus != STATUS_SUCCESS) {
123         return (false);
124     } else {
125         return (true);
126     }
127 }
128
129
130 int FIRMWAREbCheckVersion(struct vnt_private *pDevice)
131 {
132         int ntStatus;
133
134     ntStatus = CONTROLnsRequestIn(pDevice,
135                                     MESSAGE_TYPE_READ,
136                                     0,
137                                     MESSAGE_REQUEST_VERSION,
138                                     2,
139                                     (u8 *) &(pDevice->wFirmwareVersion));
140
141     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Firmware Version [%04x]\n", pDevice->wFirmwareVersion);
142     if (ntStatus != STATUS_SUCCESS) {
143         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Firmware Invalid.\n");
144         return false;
145     }
146     if (pDevice->wFirmwareVersion == 0xFFFF) {
147         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"In Loader.\n");
148         return false;
149     }
150     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Firmware Version [%04x]\n", pDevice->wFirmwareVersion);
151     if (pDevice->wFirmwareVersion < FIRMWARE_VERSION) {
152         // branch to loader for download new firmware
153         FIRMWAREbBrach2Sram(pDevice);
154         return false;
155     }
156     return true;
157 }