V4L/DVB (3310): DViCO Dual Digital DVB-T / USB bluebird updates
[linux-2.6.git] / drivers / media / dvb / dvb-usb / dvb-usb-firmware.c
1 /* 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
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 static 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 "
53                                 "(transferred size: %d, block size: %d)",
54                                 ret,hx.len);
55                         ret = -EINVAL;
56                         break;
57                 }
58         }
59         if (ret < 0) {
60                 err("firmware download failed at %d with %d",pos,ret);
61                 return ret;
62         }
63
64         if (ret == 0) {
65                 /* restart the CPU */
66                 reset = 0;
67                 if (ret || usb_cypress_writemem(udev,cypress[type].cpu_cs_register,&reset,1) != 1) {
68                         err("could not restart the USB controller CPU.");
69                         ret = -EINVAL;
70                 }
71         } else
72                 ret = -EIO;
73
74         return ret;
75 }
76
77 /*
78  * DViCO bluebird firmware needs the "warm" product ID to be patched into the
79  * firmware file before download.
80  */
81 #define BLUEBIRD_01_ID_OFFSET 6638
82 static int dvb_usb_patch_dvico_firmware(struct usb_device *udev, const struct firmware *fw)
83 {
84         if (fw->size < BLUEBIRD_01_ID_OFFSET + 4)
85                 return -EINVAL;
86
87         if (fw->data[BLUEBIRD_01_ID_OFFSET] == (USB_VID_DVICO & 0xff) &&
88             fw->data[BLUEBIRD_01_ID_OFFSET + 1] == USB_VID_DVICO >> 8) {
89                 fw->data[BLUEBIRD_01_ID_OFFSET + 2] = udev->descriptor.idProduct + 1;
90                 fw->data[BLUEBIRD_01_ID_OFFSET + 3] = udev->descriptor.idProduct >> 8;
91
92                 return 0;
93         }
94
95         return -EINVAL;
96 }
97
98 int dvb_usb_download_firmware(struct usb_device *udev, struct dvb_usb_properties *props)
99 {
100         int ret;
101         const struct firmware *fw = NULL;
102
103         if ((ret = request_firmware(&fw, props->firmware, &udev->dev)) != 0) {
104                 err("did not find the firmware file. (%s) "
105                         "Please see linux/Documentation/dvb/ for more details on firmware-problems. (%d)",
106                         props->firmware,ret);
107                 return ret;
108         }
109
110         info("downloading firmware from file '%s'",props->firmware);
111
112         if (le16_to_cpu(udev->descriptor.idVendor) == USB_VID_DVICO) {
113                 ret = dvb_usb_patch_dvico_firmware(udev, fw);
114                 if (ret != 0)
115                         warn("this firmware file not recognised");
116         }
117
118         switch (props->usb_ctrl) {
119                 case CYPRESS_AN2135:
120                 case CYPRESS_AN2235:
121                 case CYPRESS_FX2:
122                         ret = usb_cypress_load_firmware(udev, fw, props->usb_ctrl);
123                         break;
124                 case DEVICE_SPECIFIC:
125                         if (props->download_firmware)
126                                 ret = props->download_firmware(udev,fw);
127                         else {
128                                 err("BUG: driver didn't specified a download_firmware-callback, although it claims to have a DEVICE_SPECIFIC one.");
129                                 ret = -EINVAL;
130                         }
131                         break;
132                 default:
133                         ret = -EINVAL;
134                         break;
135         }
136
137         release_firmware(fw);
138         return ret;
139 }
140
141 int dvb_usb_get_hexline(const struct firmware *fw, struct hexline *hx, int *pos)
142 {
143         u8 *b = (u8 *) &fw->data[*pos];
144         int data_offs = 4;
145         if (*pos >= fw->size)
146                 return 0;
147
148         memset(hx,0,sizeof(struct hexline));
149
150         hx->len  = b[0];
151
152         if ((*pos + hx->len + 4) >= fw->size)
153                 return -EINVAL;
154
155         hx->addr = le16_to_cpu( *((u16 *) &b[1]) );
156         hx->type = b[3];
157
158         if (hx->type == 0x04) {
159                 /* b[4] and b[5] are the Extended linear address record data field */
160                 hx->addr |= (b[4] << 24) | (b[5] << 16);
161 /*              hx->len -= 2;
162                 data_offs += 2; */
163         }
164         memcpy(hx->data,&b[data_offs],hx->len);
165         hx->chk = b[hx->len + data_offs];
166
167         *pos += hx->len + 5;
168
169         return *pos;
170 }
171 EXPORT_SYMBOL(dvb_usb_get_hexline);
172