18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* dvb-usb-firmware.c is part of the DVB USB library. 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * Copyright (C) 2004-6 Patrick Boettcher (patrick.boettcher@posteo.de) 58c2ecf20Sopenharmony_ci * see dvb-usb-init.c for copyright information. 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * This file contains functions for downloading the firmware to Cypress FX 1 and 2 based devices. 88c2ecf20Sopenharmony_ci * 98c2ecf20Sopenharmony_ci * FIXME: This part does actually not belong to dvb-usb, but to the usb-subsystem. 108c2ecf20Sopenharmony_ci */ 118c2ecf20Sopenharmony_ci#include "dvb-usb-common.h" 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_ci#include <linux/usb.h> 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_cistruct usb_cypress_controller { 168c2ecf20Sopenharmony_ci int id; 178c2ecf20Sopenharmony_ci const char *name; /* name of the usb controller */ 188c2ecf20Sopenharmony_ci u16 cpu_cs_register; /* needs to be restarted, when the firmware has been downloaded. */ 198c2ecf20Sopenharmony_ci}; 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_cistatic struct usb_cypress_controller cypress[] = { 228c2ecf20Sopenharmony_ci { .id = DEVICE_SPECIFIC, .name = "Device specific", .cpu_cs_register = 0 }, 238c2ecf20Sopenharmony_ci { .id = CYPRESS_AN2135, .name = "Cypress AN2135", .cpu_cs_register = 0x7f92 }, 248c2ecf20Sopenharmony_ci { .id = CYPRESS_AN2235, .name = "Cypress AN2235", .cpu_cs_register = 0x7f92 }, 258c2ecf20Sopenharmony_ci { .id = CYPRESS_FX2, .name = "Cypress FX2", .cpu_cs_register = 0xe600 }, 268c2ecf20Sopenharmony_ci}; 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci/* 298c2ecf20Sopenharmony_ci * load a firmware packet to the device 308c2ecf20Sopenharmony_ci */ 318c2ecf20Sopenharmony_cistatic int usb_cypress_writemem(struct usb_device *udev,u16 addr,u8 *data, u8 len) 328c2ecf20Sopenharmony_ci{ 338c2ecf20Sopenharmony_ci return usb_control_msg(udev, usb_sndctrlpipe(udev,0), 348c2ecf20Sopenharmony_ci 0xa0, USB_TYPE_VENDOR, addr, 0x00, data, len, 5000); 358c2ecf20Sopenharmony_ci} 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ciint usb_cypress_load_firmware(struct usb_device *udev, const struct firmware *fw, int type) 388c2ecf20Sopenharmony_ci{ 398c2ecf20Sopenharmony_ci struct hexline *hx; 408c2ecf20Sopenharmony_ci u8 *buf; 418c2ecf20Sopenharmony_ci int ret, pos = 0; 428c2ecf20Sopenharmony_ci u16 cpu_cs_register = cypress[type].cpu_cs_register; 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci buf = kmalloc(sizeof(*hx), GFP_KERNEL); 458c2ecf20Sopenharmony_ci if (!buf) 468c2ecf20Sopenharmony_ci return -ENOMEM; 478c2ecf20Sopenharmony_ci hx = (struct hexline *)buf; 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ci /* stop the CPU */ 508c2ecf20Sopenharmony_ci buf[0] = 1; 518c2ecf20Sopenharmony_ci if (usb_cypress_writemem(udev, cpu_cs_register, buf, 1) != 1) 528c2ecf20Sopenharmony_ci err("could not stop the USB controller CPU."); 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci while ((ret = dvb_usb_get_hexline(fw, hx, &pos)) > 0) { 558c2ecf20Sopenharmony_ci deb_fw("writing to address 0x%04x (buffer: 0x%02x %02x)\n", hx->addr, hx->len, hx->chk); 568c2ecf20Sopenharmony_ci ret = usb_cypress_writemem(udev, hx->addr, hx->data, hx->len); 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci if (ret != hx->len) { 598c2ecf20Sopenharmony_ci err("error while transferring firmware (transferred size: %d, block size: %d)", 608c2ecf20Sopenharmony_ci ret, hx->len); 618c2ecf20Sopenharmony_ci ret = -EINVAL; 628c2ecf20Sopenharmony_ci break; 638c2ecf20Sopenharmony_ci } 648c2ecf20Sopenharmony_ci } 658c2ecf20Sopenharmony_ci if (ret < 0) { 668c2ecf20Sopenharmony_ci err("firmware download failed at %d with %d",pos,ret); 678c2ecf20Sopenharmony_ci kfree(buf); 688c2ecf20Sopenharmony_ci return ret; 698c2ecf20Sopenharmony_ci } 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci if (ret == 0) { 728c2ecf20Sopenharmony_ci /* restart the CPU */ 738c2ecf20Sopenharmony_ci buf[0] = 0; 748c2ecf20Sopenharmony_ci if (usb_cypress_writemem(udev, cpu_cs_register, buf, 1) != 1) { 758c2ecf20Sopenharmony_ci err("could not restart the USB controller CPU."); 768c2ecf20Sopenharmony_ci ret = -EINVAL; 778c2ecf20Sopenharmony_ci } 788c2ecf20Sopenharmony_ci } else 798c2ecf20Sopenharmony_ci ret = -EIO; 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci kfree(buf); 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci return ret; 848c2ecf20Sopenharmony_ci} 858c2ecf20Sopenharmony_ciEXPORT_SYMBOL(usb_cypress_load_firmware); 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ciint dvb_usb_download_firmware(struct usb_device *udev, 888c2ecf20Sopenharmony_ci const struct dvb_usb_device_properties *props) 898c2ecf20Sopenharmony_ci{ 908c2ecf20Sopenharmony_ci int ret; 918c2ecf20Sopenharmony_ci const struct firmware *fw = NULL; 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ci if ((ret = request_firmware(&fw, props->firmware, &udev->dev)) != 0) { 948c2ecf20Sopenharmony_ci err("did not find the firmware file '%s' (status %d). You can use <kernel_dir>/scripts/get_dvb_firmware to get the firmware", 958c2ecf20Sopenharmony_ci props->firmware,ret); 968c2ecf20Sopenharmony_ci return ret; 978c2ecf20Sopenharmony_ci } 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci info("downloading firmware from file '%s'",props->firmware); 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci switch (props->usb_ctrl) { 1028c2ecf20Sopenharmony_ci case CYPRESS_AN2135: 1038c2ecf20Sopenharmony_ci case CYPRESS_AN2235: 1048c2ecf20Sopenharmony_ci case CYPRESS_FX2: 1058c2ecf20Sopenharmony_ci ret = usb_cypress_load_firmware(udev, fw, props->usb_ctrl); 1068c2ecf20Sopenharmony_ci break; 1078c2ecf20Sopenharmony_ci case DEVICE_SPECIFIC: 1088c2ecf20Sopenharmony_ci if (props->download_firmware) 1098c2ecf20Sopenharmony_ci ret = props->download_firmware(udev,fw); 1108c2ecf20Sopenharmony_ci else { 1118c2ecf20Sopenharmony_ci err("BUG: driver didn't specified a download_firmware-callback, although it claims to have a DEVICE_SPECIFIC one."); 1128c2ecf20Sopenharmony_ci ret = -EINVAL; 1138c2ecf20Sopenharmony_ci } 1148c2ecf20Sopenharmony_ci break; 1158c2ecf20Sopenharmony_ci default: 1168c2ecf20Sopenharmony_ci ret = -EINVAL; 1178c2ecf20Sopenharmony_ci break; 1188c2ecf20Sopenharmony_ci } 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci release_firmware(fw); 1218c2ecf20Sopenharmony_ci return ret; 1228c2ecf20Sopenharmony_ci} 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ciint dvb_usb_get_hexline(const struct firmware *fw, struct hexline *hx, 1258c2ecf20Sopenharmony_ci int *pos) 1268c2ecf20Sopenharmony_ci{ 1278c2ecf20Sopenharmony_ci u8 *b = (u8 *) &fw->data[*pos]; 1288c2ecf20Sopenharmony_ci int data_offs = 4; 1298c2ecf20Sopenharmony_ci if (*pos >= fw->size) 1308c2ecf20Sopenharmony_ci return 0; 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci memset(hx,0,sizeof(struct hexline)); 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_ci hx->len = b[0]; 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_ci if ((*pos + hx->len + 4) >= fw->size) 1378c2ecf20Sopenharmony_ci return -EINVAL; 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci hx->addr = b[1] | (b[2] << 8); 1408c2ecf20Sopenharmony_ci hx->type = b[3]; 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_ci if (hx->type == 0x04) { 1438c2ecf20Sopenharmony_ci /* b[4] and b[5] are the Extended linear address record data field */ 1448c2ecf20Sopenharmony_ci hx->addr |= (b[4] << 24) | (b[5] << 16); 1458c2ecf20Sopenharmony_ci/* hx->len -= 2; 1468c2ecf20Sopenharmony_ci data_offs += 2; */ 1478c2ecf20Sopenharmony_ci } 1488c2ecf20Sopenharmony_ci memcpy(hx->data,&b[data_offs],hx->len); 1498c2ecf20Sopenharmony_ci hx->chk = b[hx->len + data_offs]; 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci *pos += hx->len + 5; 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_ci return *pos; 1548c2ecf20Sopenharmony_ci} 1558c2ecf20Sopenharmony_ciEXPORT_SYMBOL(dvb_usb_get_hexline); 156