Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* dvb-usb-firmware.c is part of the DVB USB library.
0003  *
0004  * Copyright (C) 2004-6 Patrick Boettcher (patrick.boettcher@posteo.de)
0005  * see dvb-usb-init.c for copyright information.
0006  *
0007  * This file contains functions for downloading the firmware to Cypress FX 1 and 2 based devices.
0008  *
0009  * FIXME: This part does actually not belong to dvb-usb, but to the usb-subsystem.
0010  */
0011 #include "dvb-usb-common.h"
0012 
0013 #include <linux/usb.h>
0014 
0015 struct usb_cypress_controller {
0016     int id;
0017     const char *name;       /* name of the usb controller */
0018     u16 cpu_cs_register;    /* needs to be restarted, when the firmware has been downloaded. */
0019 };
0020 
0021 static struct usb_cypress_controller cypress[] = {
0022     { .id = DEVICE_SPECIFIC, .name = "Device specific", .cpu_cs_register = 0 },
0023     { .id = CYPRESS_AN2135,  .name = "Cypress AN2135",  .cpu_cs_register = 0x7f92 },
0024     { .id = CYPRESS_AN2235,  .name = "Cypress AN2235",  .cpu_cs_register = 0x7f92 },
0025     { .id = CYPRESS_FX2,     .name = "Cypress FX2",     .cpu_cs_register = 0xe600 },
0026 };
0027 
0028 /*
0029  * load a firmware packet to the device
0030  */
0031 static int usb_cypress_writemem(struct usb_device *udev,u16 addr,u8 *data, u8 len)
0032 {
0033     return usb_control_msg(udev, usb_sndctrlpipe(udev,0),
0034             0xa0, USB_TYPE_VENDOR, addr, 0x00, data, len, 5000);
0035 }
0036 
0037 int usb_cypress_load_firmware(struct usb_device *udev, const struct firmware *fw, int type)
0038 {
0039     struct hexline *hx;
0040     u8 *buf;
0041     int ret, pos = 0;
0042     u16 cpu_cs_register = cypress[type].cpu_cs_register;
0043 
0044     buf = kmalloc(sizeof(*hx), GFP_KERNEL);
0045     if (!buf)
0046         return -ENOMEM;
0047     hx = (struct hexline *)buf;
0048 
0049     /* stop the CPU */
0050     buf[0] = 1;
0051     if (usb_cypress_writemem(udev, cpu_cs_register, buf, 1) != 1)
0052         err("could not stop the USB controller CPU.");
0053 
0054     while ((ret = dvb_usb_get_hexline(fw, hx, &pos)) > 0) {
0055         deb_fw("writing to address 0x%04x (buffer: 0x%02x %02x)\n", hx->addr, hx->len, hx->chk);
0056         ret = usb_cypress_writemem(udev, hx->addr, hx->data, hx->len);
0057 
0058         if (ret != hx->len) {
0059             err("error while transferring firmware (transferred size: %d, block size: %d)",
0060                 ret, hx->len);
0061             ret = -EINVAL;
0062             break;
0063         }
0064     }
0065     if (ret < 0) {
0066         err("firmware download failed at %d with %d",pos,ret);
0067         kfree(buf);
0068         return ret;
0069     }
0070 
0071     if (ret == 0) {
0072         /* restart the CPU */
0073         buf[0] = 0;
0074         if (usb_cypress_writemem(udev, cpu_cs_register, buf, 1) != 1) {
0075             err("could not restart the USB controller CPU.");
0076             ret = -EINVAL;
0077         }
0078     } else
0079         ret = -EIO;
0080 
0081     kfree(buf);
0082 
0083     return ret;
0084 }
0085 EXPORT_SYMBOL(usb_cypress_load_firmware);
0086 
0087 int dvb_usb_download_firmware(struct usb_device *udev,
0088                   const struct dvb_usb_device_properties *props)
0089 {
0090     int ret;
0091     const struct firmware *fw = NULL;
0092 
0093     if ((ret = request_firmware(&fw, props->firmware, &udev->dev)) != 0) {
0094         err("did not find the firmware file '%s' (status %d). You can use <kernel_dir>/scripts/get_dvb_firmware to get the firmware",
0095             props->firmware,ret);
0096         return ret;
0097     }
0098 
0099     info("downloading firmware from file '%s'",props->firmware);
0100 
0101     switch (props->usb_ctrl) {
0102         case CYPRESS_AN2135:
0103         case CYPRESS_AN2235:
0104         case CYPRESS_FX2:
0105             ret = usb_cypress_load_firmware(udev, fw, props->usb_ctrl);
0106             break;
0107         case DEVICE_SPECIFIC:
0108             if (props->download_firmware)
0109                 ret = props->download_firmware(udev,fw);
0110             else {
0111                 err("BUG: driver didn't specified a download_firmware-callback, although it claims to have a DEVICE_SPECIFIC one.");
0112                 ret = -EINVAL;
0113             }
0114             break;
0115         default:
0116             ret = -EINVAL;
0117             break;
0118     }
0119 
0120     release_firmware(fw);
0121     return ret;
0122 }
0123 
0124 int dvb_usb_get_hexline(const struct firmware *fw, struct hexline *hx,
0125                    int *pos)
0126 {
0127     u8 *b = (u8 *) &fw->data[*pos];
0128     int data_offs = 4;
0129     if (*pos >= fw->size)
0130         return 0;
0131 
0132     memset(hx,0,sizeof(struct hexline));
0133 
0134     hx->len  = b[0];
0135 
0136     if ((*pos + hx->len + 4) >= fw->size)
0137         return -EINVAL;
0138 
0139     hx->addr = b[1] | (b[2] << 8);
0140     hx->type = b[3];
0141 
0142     if (hx->type == 0x04) {
0143         /* b[4] and b[5] are the Extended linear address record data field */
0144         hx->addr |= (b[4] << 24) | (b[5] << 16);
0145 /*      hx->len -= 2;
0146         data_offs += 2; */
0147     }
0148     memcpy(hx->data,&b[data_offs],hx->len);
0149     hx->chk = b[hx->len + data_offs];
0150 
0151     *pos += hx->len + 5;
0152 
0153     return *pos;
0154 }
0155 EXPORT_SYMBOL(dvb_usb_get_hexline);