1 /* dvb-usb-firmware.c is part of the DVB USB library.
2  *
3  * Copyright (C) 2004-6 Patrick Boettcher (patrick.boettcher@posteo.de)
4  * see dvb-usb-init.c for copyright information.
5  *
6  * This file contains functions for downloading the firmware to Cypress FX 1 and 2 based devices.
7  *
8  * FIXME: This part does actually not belong to dvb-usb, but to the usb-subsystem.
9  */
10 #include "dvb-usb-common.h"
11 
12 #include <linux/usb.h>
13 
14 struct usb_cypress_controller {
15 	int id;
16 	const char *name;       /* name of the usb controller */
17 	u16 cpu_cs_register;    /* needs to be restarted, when the firmware has been downloaded. */
18 };
19 
20 static struct usb_cypress_controller cypress[] = {
21 	{ .id = DEVICE_SPECIFIC, .name = "Device specific", .cpu_cs_register = 0 },
22 	{ .id = CYPRESS_AN2135,  .name = "Cypress AN2135",  .cpu_cs_register = 0x7f92 },
23 	{ .id = CYPRESS_AN2235,  .name = "Cypress AN2235",  .cpu_cs_register = 0x7f92 },
24 	{ .id = CYPRESS_FX2,     .name = "Cypress FX2",     .cpu_cs_register = 0xe600 },
25 };
26 
27 /*
28  * load a firmware packet to the device
29  */
30 static int usb_cypress_writemem(struct usb_device *udev,u16 addr,u8 *data, u8 len)
31 {
32 	return usb_control_msg(udev, usb_sndctrlpipe(udev,0),
33 			0xa0, USB_TYPE_VENDOR, addr, 0x00, data, len, 5000);
34 }
35 
36 int usb_cypress_load_firmware(struct usb_device *udev, const struct firmware *fw, int type)
37 {
38 	struct hexline hx;
39 	u8 reset;
40 	int ret,pos=0;
41 
42 	/* stop the CPU */
43 	reset = 1;
44 	if ((ret = usb_cypress_writemem(udev,cypress[type].cpu_cs_register,&reset,1)) != 1)
45 		err("could not stop the USB controller CPU.");
46 
47 	while ((ret = dvb_usb_get_hexline(fw,&hx,&pos)) > 0) {
48 		deb_fw("writing to address 0x%04x (buffer: 0x%02x %02x)\n",hx.addr,hx.len,hx.chk);
49 		ret = usb_cypress_writemem(udev,hx.addr,hx.data,hx.len);
50 
51 		if (ret != hx.len) {
52 			err("error while transferring firmware (transferred size: %d, block size: %d)",
53 				ret,hx.len);
54 			ret = -EINVAL;
55 			break;
56 		}
57 	}
58 	if (ret < 0) {
59 		err("firmware download failed at %d with %d",pos,ret);
60 		return ret;
61 	}
62 
63 	if (ret == 0) {
64 		/* restart the CPU */
65 		reset = 0;
66 		if (ret || usb_cypress_writemem(udev,cypress[type].cpu_cs_register,&reset,1) != 1) {
67 			err("could not restart the USB controller CPU.");
68 			ret = -EINVAL;
69 		}
70 	} else
71 		ret = -EIO;
72 
73 	return ret;
74 }
75 EXPORT_SYMBOL(usb_cypress_load_firmware);
76 
77 int dvb_usb_download_firmware(struct usb_device *udev, struct dvb_usb_device_properties *props)
78 {
79 	int ret;
80 	const struct firmware *fw = NULL;
81 
82 	if ((ret = request_firmware(&fw, props->firmware, &udev->dev)) != 0) {
83 		err("did not find the firmware file. (%s) Please see linux/Documentation/dvb/ for more details on firmware-problems. (%d)",
84 			props->firmware,ret);
85 		return ret;
86 	}
87 
88 	info("downloading firmware from file '%s'",props->firmware);
89 
90 	switch (props->usb_ctrl) {
91 		case CYPRESS_AN2135:
92 		case CYPRESS_AN2235:
93 		case CYPRESS_FX2:
94 			ret = usb_cypress_load_firmware(udev, fw, props->usb_ctrl);
95 			break;
96 		case DEVICE_SPECIFIC:
97 			if (props->download_firmware)
98 				ret = props->download_firmware(udev,fw);
99 			else {
100 				err("BUG: driver didn't specified a download_firmware-callback, although it claims to have a DEVICE_SPECIFIC one.");
101 				ret = -EINVAL;
102 			}
103 			break;
104 		default:
105 			ret = -EINVAL;
106 			break;
107 	}
108 
109 	release_firmware(fw);
110 	return ret;
111 }
112 
113 int dvb_usb_get_hexline(const struct firmware *fw, struct hexline *hx,
114 			       int *pos)
115 {
116 	u8 *b = (u8 *) &fw->data[*pos];
117 	int data_offs = 4;
118 	if (*pos >= fw->size)
119 		return 0;
120 
121 	memset(hx,0,sizeof(struct hexline));
122 
123 	hx->len  = b[0];
124 
125 	if ((*pos + hx->len + 4) >= fw->size)
126 		return -EINVAL;
127 
128 	hx->addr = b[1] | (b[2] << 8);
129 	hx->type = b[3];
130 
131 	if (hx->type == 0x04) {
132 		/* b[4] and b[5] are the Extended linear address record data field */
133 		hx->addr |= (b[4] << 24) | (b[5] << 16);
134 /*		hx->len -= 2;
135 		data_offs += 2; */
136 	}
137 	memcpy(hx->data,&b[data_offs],hx->len);
138 	hx->chk = b[hx->len + data_offs];
139 
140 	*pos += hx->len + 5;
141 
142 	return *pos;
143 }
144 EXPORT_SYMBOL(dvb_usb_get_hexline);
145