📦 barneyman / somagic

📄 smi2021_bootloader.c · 266 lines
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/************************************************************************
 * 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,
			"Firmware version to be uploaded to device\n"
			"if there are more than one firmware present");

struct smi2021_firmware {
	int		id;
	const char	*name;
	int		found;
};

struct smi2021_firmware available_fw[] = {
	{
		.id = 0x3c,
		.name = SMI2021_3C_FIRMWARE,
	},
	{
		.id = 0x3e,
		.name = SMI2021_3E_FIRMWARE,
	},
	{
		.id = 0x3f,
		.name = SMI2021_3F_FIRMWARE,
	}
};

static const struct firmware *firmware[ARRAY_SIZE(available_fw)];
static int firmwares = -1;

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 == NULL) 
	{
		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 == NULL) 
	{
		dev_err(&udev->dev, "could not allocate space for usb data\n");
		rc = -ENOMEM;
		goto free_out;
	}

	if (firmware == NULL) 
	{
		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 (%d)\n", firmware->size);
		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;
}

static int smi2021_choose_firmware(struct usb_device *udev)
{
	int i, found, id;
	for (i = 0; i < ARRAY_SIZE(available_fw); i++) {
		found = available_fw[i].found;
		id = available_fw[i].id;
		if (firmware_version == id && found >= 0) {
			dev_info(&udev->dev, "uploading firmware for 0x%x\n",
					id);
			return smi2021_load_firmware(udev, firmware[found]);
		}
	}

	dev_info(&udev->dev,
	"could not decide what firmware to upload, user action required\n");
	return 0;
}

int smi2021_bootloader_probe(struct usb_interface *intf,const struct usb_device_id *devid)
{
	struct usb_device *udev = interface_to_usbdev(intf);
	int rc, i;

	/* Check what firmwares are available in the system */
	for (i = 0; i < ARRAY_SIZE(available_fw); i++) 
	{
		dev_info(&udev->dev, "Looking for: %s\n",available_fw[i].name);
		
		rc = request_firmware(&firmware[firmwares + 1],available_fw[i].name, &udev->dev);

		if (rc == 0) 
		{
			firmwares++;
			available_fw[i].found = firmwares;
			dev_info(&udev->dev, "Found firmware for 0x00%x\n",available_fw[i].id);
		} 
		else if (rc == -ENOENT) 
		{
			available_fw[i].found = -1;
		} 
		else 
		{
			dev_err(&udev->dev,"request_firmware failed with: %d\n", rc);
			goto err_out;
		}
	}

	if (firmwares < 0) 
	{
		dev_err(&udev->dev,"could not find any firmware for this device\n");
		goto no_dev;
	} 
	else if (firmwares == 0) 
	{
		dev_err(&udev->dev,"only one firmware found, defaulting to that\n");
		rc = smi2021_load_firmware(udev, firmware[0]);
		if (rc < 0)
			goto err_out;
	} 
	else 
	{
		smi2021_choose_firmware(udev);
	}

	return 0;

no_dev:
	rc = -ENODEV;
err_out:
	return rc;
}

void smi2021_bootloader_disconnect(struct usb_interface *intf)
{
	struct usb_device *udev = interface_to_usbdev(intf);
	int i;

	for (i = 0; i < ARRAY_SIZE(available_fw); i++) {
		if (available_fw[i].found >= 0) {
			dev_info(&udev->dev, "Releasing firmware for 0x00%x\n",
							available_fw[i].id);
			release_firmware(firmware[available_fw[i].found]);
			firmware[available_fw[i].found] = NULL;
			available_fw[i].found = -1;
		}
	}
	firmwares = -1;

}

MODULE_FIRMWARE(SMI2021_3C_FIRMWARE);
MODULE_FIRMWARE(SMI2021_3E_FIRMWARE);
MODULE_FIRMWARE(SMI2021_3F_FIRMWARE);