blob: 04033efe7ad5394d4fd9493ce6b790de1404666b [file] [log] [blame]
Johannes Stezenbach776338e2005-06-23 22:02:35 -07001/* dvb-usb-firmware.c is part of the DVB USB library.
2 *
Patrick Boettcher99e44da2016-01-24 12:56:58 -02003 * Copyright (C) 2004-6 Patrick Boettcher (patrick.boettcher@posteo.de)
Johannes Stezenbach776338e2005-06-23 22:02:35 -07004 * 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
27/*
28 * load a firmware packet to the device
29 */
30static 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),
Mauro Carvalho Chehab4302c152006-01-09 15:25:22 -020033 0xa0, USB_TYPE_VENDOR, addr, 0x00, data, len, 5000);
Johannes Stezenbach776338e2005-06-23 22:02:35 -070034}
35
Patrick Boettcherf5373782006-01-09 18:21:38 -020036int usb_cypress_load_firmware(struct usb_device *udev, const struct firmware *fw, int type)
Johannes Stezenbach776338e2005-06-23 22:02:35 -070037{
Mauro Carvalho Chehab43fab972017-01-24 08:13:11 -020038 struct hexline *hx;
Stefan Brüns67b05032017-02-12 13:02:13 -020039 u8 *buf;
40 int ret, pos = 0;
41 u16 cpu_cs_register = cypress[type].cpu_cs_register;
Johannes Stezenbach776338e2005-06-23 22:02:35 -070042
Stefan Brüns67b05032017-02-12 13:02:13 -020043 buf = kmalloc(sizeof(*hx), GFP_KERNEL);
44 if (!buf)
Mauro Carvalho Chehab43fab972017-01-24 08:13:11 -020045 return -ENOMEM;
Stefan Brüns67b05032017-02-12 13:02:13 -020046 hx = (struct hexline *)buf;
Mauro Carvalho Chehab43fab972017-01-24 08:13:11 -020047
Patrick Boettcherd3707ad2006-01-09 15:25:04 -020048 /* stop the CPU */
Stefan Brüns67b05032017-02-12 13:02:13 -020049 buf[0] = 1;
50 if (usb_cypress_writemem(udev, cpu_cs_register, buf, 1) != 1)
Patrick Boettcherd3707ad2006-01-09 15:25:04 -020051 err("could not stop the USB controller CPU.");
52
Mauro Carvalho Chehab43fab972017-01-24 08:13:11 -020053 while ((ret = dvb_usb_get_hexline(fw, hx, &pos)) > 0) {
54 deb_fw("writing to address 0x%04x (buffer: 0x%02x %02x)\n", hx->addr, hx->len, hx->chk);
55 ret = usb_cypress_writemem(udev, hx->addr, hx->data, hx->len);
Patrick Boettcherd3707ad2006-01-09 15:25:04 -020056
Mauro Carvalho Chehab43fab972017-01-24 08:13:11 -020057 if (ret != hx->len) {
Mauro Carvalho Chehabf319ed92016-10-18 17:44:15 -020058 err("error while transferring firmware (transferred size: %d, block size: %d)",
Mauro Carvalho Chehab43fab972017-01-24 08:13:11 -020059 ret, hx->len);
Patrick Boettcherd3707ad2006-01-09 15:25:04 -020060 ret = -EINVAL;
61 break;
62 }
63 }
64 if (ret < 0) {
65 err("firmware download failed at %d with %d",pos,ret);
Stefan Brüns67b05032017-02-12 13:02:13 -020066 kfree(buf);
Johannes Stezenbach776338e2005-06-23 22:02:35 -070067 return ret;
68 }
69
Patrick Boettcherd3707ad2006-01-09 15:25:04 -020070 if (ret == 0) {
Johannes Stezenbach776338e2005-06-23 22:02:35 -070071 /* restart the CPU */
Stefan Brüns67b05032017-02-12 13:02:13 -020072 buf[0] = 0;
73 if (usb_cypress_writemem(udev, cpu_cs_register, buf, 1) != 1) {
Johannes Stezenbach776338e2005-06-23 22:02:35 -070074 err("could not restart the USB controller CPU.");
75 ret = -EINVAL;
76 }
Patrick Boettcherd3707ad2006-01-09 15:25:04 -020077 } else
78 ret = -EIO;
Johannes Stezenbach776338e2005-06-23 22:02:35 -070079
Stefan Brüns67b05032017-02-12 13:02:13 -020080 kfree(buf);
Mauro Carvalho Chehab43fab972017-01-24 08:13:11 -020081
Johannes Stezenbach776338e2005-06-23 22:02:35 -070082 return ret;
83}
Patrick Boettcherf5373782006-01-09 18:21:38 -020084EXPORT_SYMBOL(usb_cypress_load_firmware);
Chris Pascoe0029ee12006-01-09 18:21:28 -020085
Patrick Boettcher4d43e132006-09-30 06:53:48 -030086int dvb_usb_download_firmware(struct usb_device *udev, struct dvb_usb_device_properties *props)
Patrick Boettcherd3707ad2006-01-09 15:25:04 -020087{
88 int ret;
89 const struct firmware *fw = NULL;
90
91 if ((ret = request_firmware(&fw, props->firmware, &udev->dev)) != 0) {
Mauro Carvalho Chehabf319ed92016-10-18 17:44:15 -020092 err("did not find the firmware file. (%s) Please see linux/Documentation/dvb/ for more details on firmware-problems. (%d)",
Patrick Boettcherd3707ad2006-01-09 15:25:04 -020093 props->firmware,ret);
94 return ret;
95 }
96
97 info("downloading firmware from file '%s'",props->firmware);
98
99 switch (props->usb_ctrl) {
100 case CYPRESS_AN2135:
101 case CYPRESS_AN2235:
102 case CYPRESS_FX2:
103 ret = usb_cypress_load_firmware(udev, fw, props->usb_ctrl);
104 break;
105 case DEVICE_SPECIFIC:
106 if (props->download_firmware)
107 ret = props->download_firmware(udev,fw);
108 else {
109 err("BUG: driver didn't specified a download_firmware-callback, although it claims to have a DEVICE_SPECIFIC one.");
110 ret = -EINVAL;
111 }
112 break;
113 default:
114 ret = -EINVAL;
115 break;
116 }
117
118 release_firmware(fw);
119 return ret;
120}
121
Patrick Boettcher136cafb2006-09-19 12:51:33 -0300122int dvb_usb_get_hexline(const struct firmware *fw, struct hexline *hx,
Adrian Bunka22a68652006-01-23 09:58:17 -0200123 int *pos)
Patrick Boettcherd3707ad2006-01-09 15:25:04 -0200124{
125 u8 *b = (u8 *) &fw->data[*pos];
126 int data_offs = 4;
127 if (*pos >= fw->size)
128 return 0;
129
130 memset(hx,0,sizeof(struct hexline));
131
132 hx->len = b[0];
133
134 if ((*pos + hx->len + 4) >= fw->size)
135 return -EINVAL;
136
Al Viro637007f2008-05-21 00:33:01 -0300137 hx->addr = b[1] | (b[2] << 8);
Patrick Boettcherd3707ad2006-01-09 15:25:04 -0200138 hx->type = b[3];
139
140 if (hx->type == 0x04) {
141 /* b[4] and b[5] are the Extended linear address record data field */
142 hx->addr |= (b[4] << 24) | (b[5] << 16);
143/* hx->len -= 2;
144 data_offs += 2; */
145 }
146 memcpy(hx->data,&b[data_offs],hx->len);
147 hx->chk = b[hx->len + data_offs];
148
149 *pos += hx->len + 5;
150
151 return *pos;
152}
Patrick Boettcher136cafb2006-09-19 12:51:33 -0300153EXPORT_SYMBOL(dvb_usb_get_hexline);