1 // SPDX-License-Identifier: GPL-2.0
2 /* dvb-usb-firmware.c is part of the DVB USB library.
4 * Copyright (C) 2004-6 Patrick Boettcher (patrick.boettcher@posteo.de)
5 * see dvb-usb-init.c for copyright information.
7 * This file contains functions for downloading the firmware to Cypress FX 1 and 2 based devices.
9 * FIXME: This part does actually not belong to dvb-usb, but to the usb-subsystem.
11 #include "dvb-usb-common.h"
13 #include <linux/usb.h>
15 struct usb_cypress_controller
{
17 const char *name
; /* name of the usb controller */
18 u16 cpu_cs_register
; /* needs to be restarted, when the firmware has been downloaded. */
21 static struct usb_cypress_controller cypress
[] = {
22 { .id
= DEVICE_SPECIFIC
, .name
= "Device specific", .cpu_cs_register
= 0 },
23 { .id
= CYPRESS_AN2135
, .name
= "Cypress AN2135", .cpu_cs_register
= 0x7f92 },
24 { .id
= CYPRESS_AN2235
, .name
= "Cypress AN2235", .cpu_cs_register
= 0x7f92 },
25 { .id
= CYPRESS_FX2
, .name
= "Cypress FX2", .cpu_cs_register
= 0xe600 },
29 * load a firmware packet to the device
31 static int usb_cypress_writemem(struct usb_device
*udev
,u16 addr
,u8
*data
, u8 len
)
33 return usb_control_msg(udev
, usb_sndctrlpipe(udev
,0),
34 0xa0, USB_TYPE_VENDOR
, addr
, 0x00, data
, len
, 5000);
37 int usb_cypress_load_firmware(struct usb_device
*udev
, const struct firmware
*fw
, int type
)
42 u16 cpu_cs_register
= cypress
[type
].cpu_cs_register
;
44 buf
= kmalloc(sizeof(*hx
), GFP_KERNEL
);
47 hx
= (struct hexline
*)buf
;
51 if (usb_cypress_writemem(udev
, cpu_cs_register
, buf
, 1) != 1)
52 err("could not stop the USB controller CPU.");
54 while ((ret
= dvb_usb_get_hexline(fw
, hx
, &pos
)) > 0) {
55 deb_fw("writing to address 0x%04x (buffer: 0x%02x %02x)\n", hx
->addr
, hx
->len
, hx
->chk
);
56 ret
= usb_cypress_writemem(udev
, hx
->addr
, hx
->data
, hx
->len
);
59 err("error while transferring firmware (transferred size: %d, block size: %d)",
66 err("firmware download failed at %d with %d",pos
,ret
);
74 if (usb_cypress_writemem(udev
, cpu_cs_register
, buf
, 1) != 1) {
75 err("could not restart the USB controller CPU.");
85 EXPORT_SYMBOL(usb_cypress_load_firmware
);
87 int dvb_usb_download_firmware(struct usb_device
*udev
, struct dvb_usb_device_properties
*props
)
90 const struct firmware
*fw
= NULL
;
92 if ((ret
= request_firmware(&fw
, props
->firmware
, &udev
->dev
)) != 0) {
93 err("did not find the firmware file '%s' (status %d). You can use <kernel_dir>/scripts/get_dvb_firmware to get the firmware",
98 info("downloading firmware from file '%s'",props
->firmware
);
100 switch (props
->usb_ctrl
) {
104 ret
= usb_cypress_load_firmware(udev
, fw
, props
->usb_ctrl
);
106 case DEVICE_SPECIFIC
:
107 if (props
->download_firmware
)
108 ret
= props
->download_firmware(udev
,fw
);
110 err("BUG: driver didn't specified a download_firmware-callback, although it claims to have a DEVICE_SPECIFIC one.");
119 release_firmware(fw
);
123 int dvb_usb_get_hexline(const struct firmware
*fw
, struct hexline
*hx
,
126 u8
*b
= (u8
*) &fw
->data
[*pos
];
128 if (*pos
>= fw
->size
)
131 memset(hx
,0,sizeof(struct hexline
));
135 if ((*pos
+ hx
->len
+ 4) >= fw
->size
)
138 hx
->addr
= b
[1] | (b
[2] << 8);
141 if (hx
->type
== 0x04) {
142 /* b[4] and b[5] are the Extended linear address record data field */
143 hx
->addr
|= (b
[4] << 24) | (b
[5] << 16);
147 memcpy(hx
->data
,&b
[data_offs
],hx
->len
);
148 hx
->chk
= b
[hx
->len
+ data_offs
];
154 EXPORT_SYMBOL(dvb_usb_get_hexline
);