blob: 42c207aacbb12ce90e0ea9d4cfce46996dcd93f8 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Johannes Stezenbach776338e2005-06-23 22:02:35 -07002/* dvb-usb-firmware.c is part of the DVB USB library.
3 *
Patrick Boettcher99e44da2016-01-24 12:56:58 -02004 * Copyright (C) 2004-6 Patrick Boettcher (patrick.boettcher@posteo.de)
Johannes Stezenbach776338e2005-06-23 22:02:35 -07005 * 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
Johannes Stezenbach776338e2005-06-23 22:02:35 -070013#include <linux/usb.h>
14
15struct 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
21static struct usb_cypress_controller cypress[] = {
Patrick Boettcherd3707ad2006-01-09 15:25:04 -020022 { .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 },
Johannes Stezenbach776338e2005-06-23 22:02:35 -070026};
27
28/*
29 * load a firmware packet to the device
30 */
31static 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),
Mauro Carvalho Chehab4302c152006-01-09 15:25:22 -020034 0xa0, USB_TYPE_VENDOR, addr, 0x00, data, len, 5000);
Johannes Stezenbach776338e2005-06-23 22:02:35 -070035}
36
Patrick Boettcherf5373782006-01-09 18:21:38 -020037int usb_cypress_load_firmware(struct usb_device *udev, const struct firmware *fw, int type)
Johannes Stezenbach776338e2005-06-23 22:02:35 -070038{
Mauro Carvalho Chehab43fab972017-01-24 08:13:11 -020039 struct hexline *hx;
Stefan Brüns67b05032017-02-12 13:02:13 -020040 u8 *buf;
41 int ret, pos = 0;
42 u16 cpu_cs_register = cypress[type].cpu_cs_register;
Johannes Stezenbach776338e2005-06-23 22:02:35 -070043
Stefan Brüns67b05032017-02-12 13:02:13 -020044 buf = kmalloc(sizeof(*hx), GFP_KERNEL);
45 if (!buf)
Mauro Carvalho Chehab43fab972017-01-24 08:13:11 -020046 return -ENOMEM;
Stefan Brüns67b05032017-02-12 13:02:13 -020047 hx = (struct hexline *)buf;
Mauro Carvalho Chehab43fab972017-01-24 08:13:11 -020048
Patrick Boettcherd3707ad2006-01-09 15:25:04 -020049 /* stop the CPU */
Stefan Brüns67b05032017-02-12 13:02:13 -020050 buf[0] = 1;
51 if (usb_cypress_writemem(udev, cpu_cs_register, buf, 1) != 1)
Patrick Boettcherd3707ad2006-01-09 15:25:04 -020052 err("could not stop the USB controller CPU.");
53
Mauro Carvalho Chehab43fab972017-01-24 08:13:11 -020054 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);
Patrick Boettcherd3707ad2006-01-09 15:25:04 -020057
Mauro Carvalho Chehab43fab972017-01-24 08:13:11 -020058 if (ret != hx->len) {
Mauro Carvalho Chehabf319ed92016-10-18 17:44:15 -020059 err("error while transferring firmware (transferred size: %d, block size: %d)",
Mauro Carvalho Chehab43fab972017-01-24 08:13:11 -020060 ret, hx->len);
Patrick Boettcherd3707ad2006-01-09 15:25:04 -020061 ret = -EINVAL;
62 break;
63 }
64 }
65 if (ret < 0) {
66 err("firmware download failed at %d with %d",pos,ret);
Stefan Brüns67b05032017-02-12 13:02:13 -020067 kfree(buf);
Johannes Stezenbach776338e2005-06-23 22:02:35 -070068 return ret;
69 }
70
Patrick Boettcherd3707ad2006-01-09 15:25:04 -020071 if (ret == 0) {
Johannes Stezenbach776338e2005-06-23 22:02:35 -070072 /* restart the CPU */
Stefan Brüns67b05032017-02-12 13:02:13 -020073 buf[0] = 0;
74 if (usb_cypress_writemem(udev, cpu_cs_register, buf, 1) != 1) {
Johannes Stezenbach776338e2005-06-23 22:02:35 -070075 err("could not restart the USB controller CPU.");
76 ret = -EINVAL;
77 }
Patrick Boettcherd3707ad2006-01-09 15:25:04 -020078 } else
79 ret = -EIO;
Johannes Stezenbach776338e2005-06-23 22:02:35 -070080
Stefan Brüns67b05032017-02-12 13:02:13 -020081 kfree(buf);
Mauro Carvalho Chehab43fab972017-01-24 08:13:11 -020082
Johannes Stezenbach776338e2005-06-23 22:02:35 -070083 return ret;
84}
Patrick Boettcherf5373782006-01-09 18:21:38 -020085EXPORT_SYMBOL(usb_cypress_load_firmware);
Chris Pascoe0029ee12006-01-09 18:21:28 -020086
Patrick Boettcher4d43e132006-09-30 06:53:48 -030087int dvb_usb_download_firmware(struct usb_device *udev, struct dvb_usb_device_properties *props)
Patrick Boettcherd3707ad2006-01-09 15:25:04 -020088{
89 int ret;
90 const struct firmware *fw = NULL;
91
92 if ((ret = request_firmware(&fw, props->firmware, &udev->dev)) != 0) {
Mauro Carvalho Chehabfe63a1a2018-05-08 18:10:05 -030093 err("did not find the firmware file '%s' (status %d). You can use <kernel_dir>/scripts/get_dvb_firmware to get the firmware",
Patrick Boettcherd3707ad2006-01-09 15:25:04 -020094 props->firmware,ret);
95 return ret;
96 }
97
98 info("downloading firmware from file '%s'",props->firmware);
99
100 switch (props->usb_ctrl) {
101 case CYPRESS_AN2135:
102 case CYPRESS_AN2235:
103 case CYPRESS_FX2:
104 ret = usb_cypress_load_firmware(udev, fw, props->usb_ctrl);
105 break;
106 case DEVICE_SPECIFIC:
107 if (props->download_firmware)
108 ret = props->download_firmware(udev,fw);
109 else {
110 err("BUG: driver didn't specified a download_firmware-callback, although it claims to have a DEVICE_SPECIFIC one.");
111 ret = -EINVAL;
112 }
113 break;
114 default:
115 ret = -EINVAL;
116 break;
117 }
118
119 release_firmware(fw);
120 return ret;
121}
122
Patrick Boettcher136cafb2006-09-19 12:51:33 -0300123int dvb_usb_get_hexline(const struct firmware *fw, struct hexline *hx,
Adrian Bunka22a68652006-01-23 09:58:17 -0200124 int *pos)
Patrick Boettcherd3707ad2006-01-09 15:25:04 -0200125{
126 u8 *b = (u8 *) &fw->data[*pos];
127 int data_offs = 4;
128 if (*pos >= fw->size)
129 return 0;
130
131 memset(hx,0,sizeof(struct hexline));
132
133 hx->len = b[0];
134
135 if ((*pos + hx->len + 4) >= fw->size)
136 return -EINVAL;
137
Al Viro637007f2008-05-21 00:33:01 -0300138 hx->addr = b[1] | (b[2] << 8);
Patrick Boettcherd3707ad2006-01-09 15:25:04 -0200139 hx->type = b[3];
140
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);
144/* hx->len -= 2;
145 data_offs += 2; */
146 }
147 memcpy(hx->data,&b[data_offs],hx->len);
148 hx->chk = b[hx->len + data_offs];
149
150 *pos += hx->len + 5;
151
152 return *pos;
153}
Patrick Boettcher136cafb2006-09-19 12:51:33 -0300154EXPORT_SYMBOL(dvb_usb_get_hexline);