Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
776338e1 JS |
2 | /* dvb-usb-firmware.c is part of the DVB USB library. |
3 | * | |
99e44da7 | 4 | * Copyright (C) 2004-6 Patrick Boettcher (patrick.boettcher@posteo.de) |
776338e1 JS |
5 | * see dvb-usb-init.c for copyright information. |
6 | * | |
7 | * This file contains functions for downloading the firmware to Cypress FX 1 and 2 based devices. | |
8 | * | |
9 | * FIXME: This part does actually not belong to dvb-usb, but to the usb-subsystem. | |
10 | */ | |
11 | #include "dvb-usb-common.h" | |
12 | ||
776338e1 JS |
13 | #include <linux/usb.h> |
14 | ||
15 | struct usb_cypress_controller { | |
16 | int id; | |
17 | const char *name; /* name of the usb controller */ | |
18 | u16 cpu_cs_register; /* needs to be restarted, when the firmware has been downloaded. */ | |
19 | }; | |
20 | ||
21 | static struct usb_cypress_controller cypress[] = { | |
d3707add PB |
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 }, | |
776338e1 JS |
26 | }; |
27 | ||
28 | /* | |
29 | * load a firmware packet to the device | |
30 | */ | |
31 | static int usb_cypress_writemem(struct usb_device *udev,u16 addr,u8 *data, u8 len) | |
32 | { | |
33 | return usb_control_msg(udev, usb_sndctrlpipe(udev,0), | |
4302c15e | 34 | 0xa0, USB_TYPE_VENDOR, addr, 0x00, data, len, 5000); |
776338e1 JS |
35 | } |
36 | ||
f5373788 | 37 | int usb_cypress_load_firmware(struct usb_device *udev, const struct firmware *fw, int type) |
776338e1 | 38 | { |
43fab979 | 39 | struct hexline *hx; |
67b0503d SB |
40 | u8 *buf; |
41 | int ret, pos = 0; | |
42 | u16 cpu_cs_register = cypress[type].cpu_cs_register; | |
776338e1 | 43 | |
67b0503d SB |
44 | buf = kmalloc(sizeof(*hx), GFP_KERNEL); |
45 | if (!buf) | |
43fab979 | 46 | return -ENOMEM; |
67b0503d | 47 | hx = (struct hexline *)buf; |
43fab979 | 48 | |
d3707add | 49 | /* stop the CPU */ |
67b0503d SB |
50 | buf[0] = 1; |
51 | if (usb_cypress_writemem(udev, cpu_cs_register, buf, 1) != 1) | |
d3707add PB |
52 | err("could not stop the USB controller CPU."); |
53 | ||
43fab979 MCC |
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); | |
d3707add | 57 | |
43fab979 | 58 | if (ret != hx->len) { |
f319ed91 | 59 | err("error while transferring firmware (transferred size: %d, block size: %d)", |
43fab979 | 60 | ret, hx->len); |
d3707add PB |
61 | ret = -EINVAL; |
62 | break; | |
63 | } | |
64 | } | |
65 | if (ret < 0) { | |
66 | err("firmware download failed at %d with %d",pos,ret); | |
67b0503d | 67 | kfree(buf); |
776338e1 JS |
68 | return ret; |
69 | } | |
70 | ||
d3707add | 71 | if (ret == 0) { |
776338e1 | 72 | /* restart the CPU */ |
67b0503d SB |
73 | buf[0] = 0; |
74 | if (usb_cypress_writemem(udev, cpu_cs_register, buf, 1) != 1) { | |
776338e1 JS |
75 | err("could not restart the USB controller CPU."); |
76 | ret = -EINVAL; | |
77 | } | |
d3707add PB |
78 | } else |
79 | ret = -EIO; | |
776338e1 | 80 | |
67b0503d | 81 | kfree(buf); |
43fab979 | 82 | |
d3707add PB |
83 | return ret; |
84 | } | |
f5373788 | 85 | EXPORT_SYMBOL(usb_cypress_load_firmware); |
0029ee14 | 86 | |
d27958df SY |
87 | int dvb_usb_download_firmware(struct usb_device *udev, |
88 | const struct dvb_usb_device_properties *props) | |
d3707add PB |
89 | { |
90 | int ret; | |
91 | const struct firmware *fw = NULL; | |
92 | ||
93 | if ((ret = request_firmware(&fw, props->firmware, &udev->dev)) != 0) { | |
fe63a1a6 | 94 | err("did not find the firmware file '%s' (status %d). You can use <kernel_dir>/scripts/get_dvb_firmware to get the firmware", |
d3707add PB |
95 | props->firmware,ret); |
96 | return ret; | |
776338e1 | 97 | } |
776338e1 | 98 | |
d3707add PB |
99 | info("downloading firmware from file '%s'",props->firmware); |
100 | ||
101 | switch (props->usb_ctrl) { | |
102 | case CYPRESS_AN2135: | |
103 | case CYPRESS_AN2235: | |
104 | case CYPRESS_FX2: | |
105 | ret = usb_cypress_load_firmware(udev, fw, props->usb_ctrl); | |
106 | break; | |
107 | case DEVICE_SPECIFIC: | |
108 | if (props->download_firmware) | |
109 | ret = props->download_firmware(udev,fw); | |
110 | else { | |
111 | err("BUG: driver didn't specified a download_firmware-callback, although it claims to have a DEVICE_SPECIFIC one."); | |
112 | ret = -EINVAL; | |
113 | } | |
114 | break; | |
115 | default: | |
116 | ret = -EINVAL; | |
117 | break; | |
118 | } | |
119 | ||
120 | release_firmware(fw); | |
776338e1 JS |
121 | return ret; |
122 | } | |
d3707add | 123 | |
136cafbf | 124 | int dvb_usb_get_hexline(const struct firmware *fw, struct hexline *hx, |
a22a6865 | 125 | int *pos) |
d3707add PB |
126 | { |
127 | u8 *b = (u8 *) &fw->data[*pos]; | |
128 | int data_offs = 4; | |
129 | if (*pos >= fw->size) | |
130 | return 0; | |
131 | ||
132 | memset(hx,0,sizeof(struct hexline)); | |
133 | ||
134 | hx->len = b[0]; | |
135 | ||
136 | if ((*pos + hx->len + 4) >= fw->size) | |
137 | return -EINVAL; | |
138 | ||
637007fe | 139 | hx->addr = b[1] | (b[2] << 8); |
d3707add PB |
140 | hx->type = b[3]; |
141 | ||
142 | if (hx->type == 0x04) { | |
143 | /* b[4] and b[5] are the Extended linear address record data field */ | |
144 | hx->addr |= (b[4] << 24) | (b[5] << 16); | |
145 | /* hx->len -= 2; | |
146 | data_offs += 2; */ | |
147 | } | |
148 | memcpy(hx->data,&b[data_offs],hx->len); | |
149 | hx->chk = b[hx->len + data_offs]; | |
150 | ||
151 | *pos += hx->len + 5; | |
152 | ||
153 | return *pos; | |
154 | } | |
136cafbf | 155 | EXPORT_SYMBOL(dvb_usb_get_hexline); |