-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasv5211.c
275 lines (243 loc) · 7.28 KB
/
asv5211.c
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
/* DVB USB framework compliant Linux driver for the SKNET MonsterTV HD ISDB-T
* receiver. (Firmware downloader)
*
* Copyright (c) 2009 Gombei Nanashi
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "asv5211.h"
/* debug */
static int dvb_usb_asv5211_debug;
module_param_named(debug, dvb_usb_asv5211_debug, int, 0644);
MODULE_PARM_DESC(debug, "set debugging level (1=info,2=xfer (or-able))." DVB_USB_DEBUG_STATUS);
DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
#define deb_info(args...) dprintk(dvb_usb_asv5211_debug, 0x01, args)
#define deb_xfer(args...) dprintk(dvb_usb_asv5211_debug, 0x02, args)
/* Download firmware sub */
static int asv5211_send_firm(struct usb_device *udev,
u8 req, u16 index, void *data, u16 value, u16 size)
{
int ret;
deb_xfer("USB control msg: 40 %02x %04x %04x %04x\n",
req, value, index, size);
ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), req,
USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
value, index, data, size, 5000);
if (ret != size) {
err("failed to download firmware at %04x (%04x)", value, ret);
return -EIO;
}
return 0;
}
/* dvb-usb firmware_download entry */
#ifdef CONFIG_DVB_USB_ASV5211_WIN_DRIVER
static int asv5211_download_firmware(struct usb_device *udev,
const struct firmware *fw)
{
int ret = 0;
u16 idx;
u8 *buff, *fw_data;
if (fw->size < 0xa90+0x4000) {
err("firmware file is too small");
return -EINVAL;
}
buff = kmalloc(0x1000, GFP_KERNEL);
if (buff == NULL) {
return -ENOMEM;
}
deb_info("download firmware: ");
fw_data = (void *)fw->data;
idx = le16_to_cpup((__le16 *)&fw_data[0xa88]);
if ((idx == 0x4d66) /* CD 1.0 */ || (idx == 0x5121) /* CD 1.1 */) {
deb_info("idx = 0x%x\n", idx);
}
else {
err("unknown firmware file");
ret = -EINVAL;
goto done;
}
memcpy(buff, &fw_data[0xa90+0x0000], 0x0c00);
ret = asv5211_send_firm(udev, 0xab, idx, buff, 0x0000, 0x0c00);
if (ret != 0) {
goto done;
}
memcpy(buff, &fw_data[0xa90+0x2000], 0x0400);
ret = asv5211_send_firm(udev, 0xab, idx, buff, 0x2000, 0x0400);
if (ret != 0) {
goto done;
}
memcpy(buff, &fw_data[0xa90+0x2800], 0x1000);
ret = asv5211_send_firm(udev, 0xab, idx, buff, 0x2800, 0x1000);
if (ret != 0) {
goto done;
}
memcpy(buff, &fw_data[0xa90+0x3800], 0x0800);
ret = asv5211_send_firm(udev, 0xac, idx, buff, 0x3800, 0x0800);
done:
kfree(buff);
return ret;
}
#else /* ! CONFIG_DVB_USB_ASV5211_WIN_DRIVER */
static int asv5211_download_firmware(struct usb_device *udev,
const struct firmware *fw)
{
int i;
int ret = 0;
u16 idx, ofs, len, sum;
u8 *buff, *fw_data, req, sum_h;
struct asv5211_fw_header *fw_header;
struct asv5211_fw_block *fw_block;
buff = kmalloc(0x1000, GFP_KERNEL);
if (buff == NULL) {
return -ENOMEM;
}
deb_info("download firmware: ");
/* Locate header & check header contents */
fw_header = (void *)fw->data;
if (fw->size < sizeof(struct asv5211_fw_header)) {
err("bad firmware file (size: %x < %x)", (unsigned int)fw->size, (unsigned int)sizeof(struct asv5211_fw_header));
ret = -EINVAL;
goto done;
}
if (strncmp("ASV5211", fw_header->name, 8) != 0) {
err("bad firmware file (name)");
ret = -EINVAL;
goto done;
}
if (fw_header->header_size < sizeof(struct asv5211_fw_header) + sizeof(struct asv5211_fw_block) * fw_header->count) {
err("bad firmware file (header size)");
ret = -EINVAL;
goto done;
}
if (fw->size < fw_header->header_size) {
err("bad firmware file (size: %x < %x)", (unsigned int)fw->size, fw_header->header_size);
ret = -EINVAL;
goto done;
}
/* Locate block header */
fw_data = (void *)fw->data;
fw_block = (void *)(fw_data + sizeof(struct asv5211_fw_header));
/* Check header checksum & seek to top of firmware data */
sum_h = 0;
for (i = 0; i < fw_header->header_size; i++) {
sum_h += *fw_data;
fw_data++;
}
if (sum_h != 0) {
err("bad firmware file (header checksum: %02x)", sum_h);
ret = -EINVAL;
goto done;
}
/* Check checksum of firmware data */
len = 0;
for (i = 0; i < fw_header->count; i++) {
len += le16_to_cpu(fw_block[i].length);
}
if (fw->size != fw_header->header_size + len) {
err("bad firmware file (size: %x != %x + %x)", (unsigned int)fw->size, fw_header->header_size, len);
ret = -EINVAL;
goto done;
}
sum = 0;
for (i = 0; i < len; i++) {
sum += fw_data[i];
}
if (sum != le16_to_cpu(fw_header->sum)) {
err("bad firmware file (checksum: %04x -> %04x)", le16_to_cpu(fw_header->sum), sum);
ret = -EINVAL;
goto done;
}
/* Download firmware */
idx = le16_to_cpu(fw_header->version);
deb_info("ASV5211 version? %04x\n", idx);
for (i = 0; i < fw_header->count; i++) {
ofs = le16_to_cpu(fw_block[i].offset);
len = le16_to_cpu(fw_block[i].length);
req = (i == fw_header->count - 1) ? 0xac : 0xab;
if (len > 0x1000) {
err("bad firmware file (FW[%d] length: %04x)", i, len);
ret = -EINVAL;
goto done;
}
memcpy(buff, fw_data, len);
ret = asv5211_send_firm(udev, req, idx, buff, ofs, len);
if (ret != 0) {
goto done;
}
fw_data += len;
}
done:
kfree(buff);
return ret;
}
#endif /* ! CONFIG_DVB_USB_ASV5211_WIN_DRIVER */
/* USB Driver stuff */
static struct dvb_usb_device_properties asv5211_properties;
static int asv5211_probe(struct usb_interface *intf,
const struct usb_device_id *id)
{
return dvb_usb_device_init(intf, &asv5211_properties,
THIS_MODULE, NULL, adapter_nr);
}
/* do not change the order of the ID table */
static struct usb_device_id asv5211_table [] = {
/* 00 */ { USB_DEVICE(USB_VID_ASICEN, USB_PID_ASICEN_ASV5211) },
{ 0 } /* Terminating entry */
};
MODULE_DEVICE_TABLE(usb, asv5211_table);
/* common cold state properties for as11loader */
static struct dvb_usb_device_properties asv5211_properties = {
.caps = 0,
.usb_ctrl = DEVICE_SPECIFIC,
#ifdef CONFIG_DVB_USB_ASV5211_WIN_DRIVER
.firmware = "AS11Loader.sys",
#else
.firmware = "dvb-usb-asv5211.fw",
#endif
.download_firmware = asv5211_download_firmware,
.num_device_descs = 1,
.devices = {
{ .name = "ASICEN ASV5211 (cold state)",
.cold_ids = { &asv5211_table[0], NULL },
.warm_ids = { NULL },
},
}
};
static struct usb_driver asv5211_driver = {
.name = "dvb_usb_asv5211",
.probe = asv5211_probe,
.disconnect = dvb_usb_device_exit,
.id_table = asv5211_table,
};
/* module stuff */
static int __init asv5211_module_init(void)
{
int result;
if ((result = usb_register(&asv5211_driver))) {
err("usb_register failed. Error number %d", result);
return result;
}
return 0;
}
static void __exit asv5211_module_exit(void)
{
/* deregister this driver from the USB subsystem */
usb_deregister(&asv5211_driver);
}
module_init (asv5211_module_init);
module_exit (asv5211_module_exit);
MODULE_AUTHOR("Gombei Nanashi");
MODULE_DESCRIPTION("Firmware downloader for ASICEN ASV5211");
MODULE_VERSION("0.1");
MODULE_LICENSE("GPL");