-
Notifications
You must be signed in to change notification settings - Fork 1
/
smi2021_bootloader.c
221 lines (187 loc) · 5.8 KB
/
smi2021_bootloader.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
/************************************************************************
* smi2021_bootloader.c *
* *
* USB Driver for SMI2021 - EasyCAP *
* **********************************************************************
*
* Copyright 2011-2013 Jon Arne Jørgensen
* <jonjon.arnearne--a.t--gmail.com>
*
* Copyright 2011, 2012 Tony Brown, Michal Demin, Jeffry Johnston
*
* 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/>.
*
* This driver is heavily influensed by the STK1160 driver.
* Copyright (C) 2012 Ezequiel Garcia
* <elezegarcia--a.t--gmail.com>
*
*/
#include "smi2021.h"
#include <linux/module.h>
#include <linux/usb.h>
#include <linux/firmware.h>
#include <linux/slab.h>
#define FIRMWARE_CHUNK_SIZE 62
#define FIRMWARE_HEADER_SIZE 2
#define FIRMWARE_CHUNK_HEAD_0 0x05
#define FIRMWARE_CHUNK_HEAD_1 0xff
#define FIRMWARE_HW_STATE_HEAD 0x01
#define FIRMWARE_HW_READY_STATE 0x07
#define SMI2021_3C_FIRMWARE "smi2021_3c.bin"
#define SMI2021_3E_FIRMWARE "smi2021_3e.bin"
#define SMI2021_3F_FIRMWARE "smi2021_3f.bin"
static unsigned int firmware_version;
module_param(firmware_version, int, 0644);
MODULE_PARM_DESC(firmware_version,
"Select what firmware to upload\n"
"accepted values: 0x3c, 0x3e, 0x3f");
static int smi2021_load_firmware(struct usb_device *udev,
const struct firmware *firmware)
{
int i, size, rc;
struct smi2021_set_hw_state *hw_state;
u8 *chunk;
size = FIRMWARE_CHUNK_SIZE + FIRMWARE_HEADER_SIZE;
chunk = kzalloc(size, GFP_KERNEL);
if (!chunk) {
dev_err(&udev->dev,
"could not allocate space for firmware chunk\n");
rc = -ENOMEM;
goto end_out;
}
hw_state = kzalloc(sizeof(*hw_state), GFP_KERNEL);
if (!hw_state) {
dev_err(&udev->dev, "could not allocate space for usb data\n");
rc = -ENOMEM;
goto free_out;
}
if (!firmware) {
dev_err(&udev->dev, "firmware is NULL\n");
rc = -ENODEV;
goto free_out;
}
if (firmware->size % FIRMWARE_CHUNK_SIZE) {
dev_err(&udev->dev, "firmware has wrong size\n");
rc = -ENODEV;
goto free_out;
}
rc = usb_control_msg(udev, usb_rcvctrlpipe(udev, SMI2021_USB_RCVPIPE),
SMI2021_USB_REQUEST,
USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
FIRMWARE_HW_STATE_HEAD, SMI2021_USB_INDEX,
hw_state, sizeof(*hw_state), 1000);
if (rc < 0 || hw_state->state != FIRMWARE_HW_READY_STATE) {
dev_err(&udev->dev,
"device is not ready for firmware upload: %d\n", rc);
goto free_out;
}
chunk[0] = FIRMWARE_CHUNK_HEAD_0;
chunk[1] = FIRMWARE_CHUNK_HEAD_1;
for (i = 0; i < firmware->size / FIRMWARE_CHUNK_SIZE; i++) {
memcpy(chunk + FIRMWARE_HEADER_SIZE,
firmware->data + (i * FIRMWARE_CHUNK_SIZE),
FIRMWARE_CHUNK_SIZE);
rc = usb_control_msg(udev,
usb_sndctrlpipe(udev, SMI2021_USB_SNDPIPE),
SMI2021_USB_REQUEST,
USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
FIRMWARE_CHUNK_HEAD_0, SMI2021_USB_INDEX,
chunk, size, 1000);
if (rc < 0) {
dev_err(&udev->dev, "firmware upload failed: %d\n",
rc);
goto free_out;
}
}
hw_state->head = FIRMWARE_HW_READY_STATE;
hw_state->state = 0x00;
rc = usb_control_msg(udev, usb_sndctrlpipe(udev, SMI2021_USB_SNDPIPE),
SMI2021_USB_REQUEST,
USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
FIRMWARE_HW_READY_STATE, SMI2021_USB_INDEX,
hw_state, sizeof(*hw_state), 1000);
if (rc < 0) {
dev_err(&udev->dev, "device failed to ack firmware: %d\n", rc);
goto free_out;
}
rc = 0;
free_out:
kfree(chunk);
kfree(hw_state);
end_out:
return rc;
}
/*
* There are atleast three different hardware versions of the smi2021 devices
* that require different firmwares.
* Before the firmware is loaded, they all report the same usb product id,
* so I don't know of any way to tell what device the user just plugged.
* If we only find one smi2021 firmware,
* we can probably asume it's correct for the device.
*
* Users with multiple different firmwares/devices
* will have to specify the version in /sysfs before plugging in each device.
*/
int smi2021_bootloader_probe(struct usb_interface *intf,
const struct usb_device_id *devid)
{
struct usb_device *udev = interface_to_usbdev(intf);
const struct firmware *firmware;
int rc, i;
struct smi2021_versions {
unsigned int id;
const char *name;
} static const hw_versions[3] = {
{
.id = 0x3f,
.name = SMI2021_3F_FIRMWARE,
},
{
.id = 0x3e,
.name = SMI2021_3E_FIRMWARE,
},
{
.id = 0x3c,
.name = SMI2021_3C_FIRMWARE,
}
};
for (i = 0; i < ARRAY_SIZE(hw_versions); i++) {
if (firmware_version && firmware_version != hw_versions[i].id)
continue;
dev_info(&udev->dev, "Looking for: %s\n", hw_versions[i].name);
rc = request_firmware_direct(&firmware, hw_versions[i].name,
&udev->dev);
if (rc == 0) {
dev_info(&udev->dev, "Found firmware for 0x00%x\n",
hw_versions[i].id);
goto load_fw;
}
}
if (firmware_version)
dev_err(&udev->dev,
"the specified firmware for this device could not be loaded\n");
else
dev_err(&udev->dev,
"could not load any firmware for this device\n");
return rc;
load_fw:
rc = smi2021_load_firmware(udev, firmware);
if (rc < 0)
dev_err(&udev->dev, "firmware upload failed\n");
release_firmware(firmware);
return rc;
}
MODULE_FIRMWARE(SMI2021_3C_FIRMWARE);
MODULE_FIRMWARE(SMI2021_3E_FIRMWARE);
MODULE_FIRMWARE(SMI2021_3F_FIRMWARE);