blob: 9222b0a81f748646f82eda2ec382e16af1d2db7d [file] [log] [blame]
Johannes Stezenbach776338e2005-06-23 22:02:35 -07001/* dvb-usb-firmware.c is part of the DVB USB library.
2 *
3 * Copyright (C) 2004-5 Patrick Boettcher (patrick.boettcher@desy.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
Johannes Stezenbach776338e2005-06-23 22:02:35 -070012#include <linux/usb.h>
13
14struct 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
20static struct usb_cypress_controller cypress[] = {
Patrick Boettcherd3707ad2006-01-09 15:25:04 -020021 { .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 },
Johannes Stezenbach776338e2005-06-23 22:02:35 -070025};
26
Adrian Bunka22a68652006-01-23 09:58:17 -020027static int dvb_usb_get_hexline(const struct firmware *fw, struct hexline *hx,
28 int *pos);
29
Johannes Stezenbach776338e2005-06-23 22:02:35 -070030/*
31 * load a firmware packet to the device
32 */
33static int usb_cypress_writemem(struct usb_device *udev,u16 addr,u8 *data, u8 len)
34{
35 return usb_control_msg(udev, usb_sndctrlpipe(udev,0),
Mauro Carvalho Chehab4302c152006-01-09 15:25:22 -020036 0xa0, USB_TYPE_VENDOR, addr, 0x00, data, len, 5000);
Johannes Stezenbach776338e2005-06-23 22:02:35 -070037}
38
Patrick Boettcherf5373782006-01-09 18:21:38 -020039int usb_cypress_load_firmware(struct usb_device *udev, const struct firmware *fw, int type)
Johannes Stezenbach776338e2005-06-23 22:02:35 -070040{
Patrick Boettcherd3707ad2006-01-09 15:25:04 -020041 struct hexline hx;
42 u8 reset;
43 int ret,pos=0;
Johannes Stezenbach776338e2005-06-23 22:02:35 -070044
Patrick Boettcherd3707ad2006-01-09 15:25:04 -020045 /* stop the CPU */
46 reset = 1;
47 if ((ret = usb_cypress_writemem(udev,cypress[type].cpu_cs_register,&reset,1)) != 1)
48 err("could not stop the USB controller CPU.");
49
50 while ((ret = dvb_usb_get_hexline(fw,&hx,&pos)) > 0) {
51 deb_fw("writing to address 0x%04x (buffer: 0x%02x %02x)\n",hx.addr,hx.len,hx.chk);
52 ret = usb_cypress_writemem(udev,hx.addr,hx.data,hx.len);
53
54 if (ret != hx.len) {
55 err("error while transferring firmware "
56 "(transferred size: %d, block size: %d)",
57 ret,hx.len);
58 ret = -EINVAL;
59 break;
60 }
61 }
62 if (ret < 0) {
63 err("firmware download failed at %d with %d",pos,ret);
Johannes Stezenbach776338e2005-06-23 22:02:35 -070064 return ret;
65 }
66
Patrick Boettcherd3707ad2006-01-09 15:25:04 -020067 if (ret == 0) {
Johannes Stezenbach776338e2005-06-23 22:02:35 -070068 /* restart the CPU */
69 reset = 0;
70 if (ret || usb_cypress_writemem(udev,cypress[type].cpu_cs_register,&reset,1) != 1) {
71 err("could not restart the USB controller CPU.");
72 ret = -EINVAL;
73 }
Patrick Boettcherd3707ad2006-01-09 15:25:04 -020074 } else
75 ret = -EIO;
Johannes Stezenbach776338e2005-06-23 22:02:35 -070076
77 return ret;
78}
Patrick Boettcherf5373782006-01-09 18:21:38 -020079EXPORT_SYMBOL(usb_cypress_load_firmware);
Chris Pascoe0029ee12006-01-09 18:21:28 -020080
Patrick Boettcherd3707ad2006-01-09 15:25:04 -020081int dvb_usb_download_firmware(struct usb_device *udev, struct dvb_usb_properties *props)
82{
83 int ret;
84 const struct firmware *fw = NULL;
85
86 if ((ret = request_firmware(&fw, props->firmware, &udev->dev)) != 0) {
87 err("did not find the firmware file. (%s) "
88 "Please see linux/Documentation/dvb/ for more details on firmware-problems. (%d)",
89 props->firmware,ret);
90 return ret;
91 }
92
93 info("downloading firmware from file '%s'",props->firmware);
94
95 switch (props->usb_ctrl) {
96 case CYPRESS_AN2135:
97 case CYPRESS_AN2235:
98 case CYPRESS_FX2:
99 ret = usb_cypress_load_firmware(udev, fw, props->usb_ctrl);
100 break;
101 case DEVICE_SPECIFIC:
102 if (props->download_firmware)
103 ret = props->download_firmware(udev,fw);
104 else {
105 err("BUG: driver didn't specified a download_firmware-callback, although it claims to have a DEVICE_SPECIFIC one.");
106 ret = -EINVAL;
107 }
108 break;
109 default:
110 ret = -EINVAL;
111 break;
112 }
113
114 release_firmware(fw);
115 return ret;
116}
117
Adrian Bunka22a68652006-01-23 09:58:17 -0200118static int dvb_usb_get_hexline(const struct firmware *fw, struct hexline *hx,
119 int *pos)
Patrick Boettcherd3707ad2006-01-09 15:25:04 -0200120{
121 u8 *b = (u8 *) &fw->data[*pos];
122 int data_offs = 4;
123 if (*pos >= fw->size)
124 return 0;
125
126 memset(hx,0,sizeof(struct hexline));
127
128 hx->len = b[0];
129
130 if ((*pos + hx->len + 4) >= fw->size)
131 return -EINVAL;
132
133 hx->addr = le16_to_cpu( *((u16 *) &b[1]) );
134 hx->type = b[3];
135
136 if (hx->type == 0x04) {
137 /* b[4] and b[5] are the Extended linear address record data field */
138 hx->addr |= (b[4] << 24) | (b[5] << 16);
139/* hx->len -= 2;
140 data_offs += 2; */
141 }
142 memcpy(hx->data,&b[data_offs],hx->len);
143 hx->chk = b[hx->len + data_offs];
144
145 *pos += hx->len + 5;
146
147 return *pos;
148}