summaryrefslogtreecommitdiffstats
path: root/drivers/media/dvb/dvb-usb-v2/cypress_firmware.c
blob: 9f7c970c6424f6ce16b2232d2ee449d4f16cc5e2 (plain)
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
/*  cypress_firmware.c is part of the DVB USB library.
 *
 * Copyright (C) 2004-6 Patrick Boettcher (patrick.boettcher@desy.de)
 * see dvb-usb-init.c for copyright information.
 *
 * This file contains functions for downloading the firmware to Cypress FX 1
 * and 2 based devices.
 *
 */

#include "dvb_usb.h"
#include "cypress_firmware.h"

struct usb_cypress_controller {
	u8 id;
	const char *name;	/* name of the usb controller */
	u16 cs_reg;		/* needs to be restarted,
				 * when the firmware has been downloaded */
};

static const struct usb_cypress_controller cypress[] = {
	{ .id = CYPRESS_AN2135, .name = "Cypress AN2135", .cs_reg = 0x7f92 },
	{ .id = CYPRESS_AN2235, .name = "Cypress AN2235", .cs_reg = 0x7f92 },
	{ .id = CYPRESS_FX2,    .name = "Cypress FX2",    .cs_reg = 0xe600 },
};

/*
 * load a firmware packet to the device
 */
static int usb_cypress_writemem(struct usb_device *udev, u16 addr, u8 *data,
		u8 len)
{
	return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
			0xa0, USB_TYPE_VENDOR, addr, 0x00, data, len, 5000);
}

int usbv2_cypress_load_firmware(struct usb_device *udev,
		const struct firmware *fw, int type)
{
	struct hexline hx;
	u8 reset;
	int ret, pos = 0;

	/* stop the CPU */
	reset = 1;
	ret = usb_cypress_writemem(udev, cypress[type].cs_reg, &reset, 1);
	if (ret != 1)
		pr_err("%s: could not stop the USB controller CPU",
				KBUILD_MODNAME);

	while ((ret = dvb_usbv2_get_hexline(fw, &hx, &pos)) > 0) {
		pr_debug("%s: writing to address %04x (buffer: %02x %02x)\n",
				__func__, hx.addr, hx.len, hx.chk);

		ret = usb_cypress_writemem(udev, hx.addr, hx.data, hx.len);
		if (ret != hx.len) {
			pr_err("%s: error while transferring firmware " \
					"(transferred size=%d, block size=%d)",
					KBUILD_MODNAME, ret, hx.len);
			ret = -EINVAL;
			break;
		}
	}
	if (ret < 0) {
		pr_err("%s: firmware download failed at %d with %d",
				KBUILD_MODNAME, pos, ret);
		return ret;
	}

	if (ret == 0) {
		/* restart the CPU */
		reset = 0;
		if (ret || usb_cypress_writemem(
				udev, cypress[type].cs_reg, &reset, 1) != 1) {
			pr_err("%s: could not restart the USB controller CPU",
					KBUILD_MODNAME);
			ret = -EINVAL;
		}
	} else
		ret = -EIO;

	return ret;
}
EXPORT_SYMBOL(usbv2_cypress_load_firmware);

int dvb_usbv2_get_hexline(const struct firmware *fw, struct hexline *hx,
		int *pos)
{
	u8 *b = (u8 *) &fw->data[*pos];
	int data_offs = 4;

	if (*pos >= fw->size)
		return 0;

	memset(hx, 0, sizeof(struct hexline));

	hx->len = b[0];

	if ((*pos + hx->len + 4) >= fw->size)
		return -EINVAL;

	hx->addr = b[1] | (b[2] << 8);
	hx->type = b[3];

	if (hx->type == 0x04) {
		/* b[4] and b[5] are the Extended linear address record data
		 * field */
		hx->addr |= (b[4] << 24) | (b[5] << 16);
		/*
		hx->len -= 2;
		data_offs += 2;
		*/
	}
	memcpy(hx->data, &b[data_offs], hx->len);
	hx->chk = b[hx->len + data_offs];

	*pos += hx->len + 5;

	return *pos;
}
EXPORT_SYMBOL(dvb_usbv2_get_hexline);

MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
MODULE_DESCRIPTION("Cypress firmware download");
MODULE_LICENSE("GPL");