linux/drivers/media/usb/dvb-usb/dvb-usb-firmware.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/* dvb-usb-firmware.c is part of the DVB USB library.
   3 *
   4 * Copyright (C) 2004-6 Patrick Boettcher (patrick.boettcher@desy.de)
   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
  13#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[] = {
  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 },
  26};
  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),
  34                        0xa0, USB_TYPE_VENDOR, addr, 0x00, data, len, 5000);
  35}
  36
  37int usb_cypress_load_firmware(struct usb_device *udev, const struct firmware *fw, int type)
  38{
  39        struct hexline hx;
  40        u8 reset;
  41        int ret,pos=0;
  42
  43        /* stop the CPU */
  44        reset = 1;
  45        if ((ret = usb_cypress_writemem(udev,cypress[type].cpu_cs_register,&reset,1)) != 1)
  46                err("could not stop the USB controller CPU.");
  47
  48        while ((ret = dvb_usb_get_hexline(fw,&hx,&pos)) > 0) {
  49                deb_fw("writing to address 0x%04x (buffer: 0x%02x %02x)\n",hx.addr,hx.len,hx.chk);
  50                ret = usb_cypress_writemem(udev,hx.addr,hx.data,hx.len);
  51
  52                if (ret != hx.len) {
  53                        err("error while transferring firmware "
  54                                "(transferred size: %d, block size: %d)",
  55                                ret,hx.len);
  56                        ret = -EINVAL;
  57                        break;
  58                }
  59        }
  60        if (ret < 0) {
  61                err("firmware download failed at %d with %d",pos,ret);
  62                return ret;
  63        }
  64
  65        if (ret == 0) {
  66                /* restart the CPU */
  67                reset = 0;
  68                if (ret || usb_cypress_writemem(udev,cypress[type].cpu_cs_register,&reset,1) != 1) {
  69                        err("could not restart the USB controller CPU.");
  70                        ret = -EINVAL;
  71                }
  72        } else
  73                ret = -EIO;
  74
  75        return ret;
  76}
  77EXPORT_SYMBOL(usb_cypress_load_firmware);
  78
  79int dvb_usb_download_firmware(struct usb_device *udev, struct dvb_usb_device_properties *props)
  80{
  81        int ret;
  82        const struct firmware *fw = NULL;
  83
  84        if ((ret = request_firmware(&fw, props->firmware, &udev->dev)) != 0) {
  85                err("did not find the firmware file. (%s) "
  86                        "Please see linux/Documentation/dvb/ for more details on firmware-problems. (%d)",
  87                        props->firmware,ret);
  88                return ret;
  89        }
  90
  91        info("downloading firmware from file '%s'",props->firmware);
  92
  93        switch (props->usb_ctrl) {
  94                case CYPRESS_AN2135:
  95                case CYPRESS_AN2235:
  96                case CYPRESS_FX2:
  97                        ret = usb_cypress_load_firmware(udev, fw, props->usb_ctrl);
  98                        break;
  99                case DEVICE_SPECIFIC:
 100                        if (props->download_firmware)
 101                                ret = props->download_firmware(udev,fw);
 102                        else {
 103                                err("BUG: driver didn't specified a download_firmware-callback, although it claims to have a DEVICE_SPECIFIC one.");
 104                                ret = -EINVAL;
 105                        }
 106                        break;
 107                default:
 108                        ret = -EINVAL;
 109                        break;
 110        }
 111
 112        release_firmware(fw);
 113        return ret;
 114}
 115
 116int dvb_usb_get_hexline(const struct firmware *fw, struct hexline *hx,
 117                               int *pos)
 118{
 119        u8 *b = (u8 *) &fw->data[*pos];
 120        int data_offs = 4;
 121        if (*pos >= fw->size)
 122                return 0;
 123
 124        memset(hx,0,sizeof(struct hexline));
 125
 126        hx->len  = b[0];
 127
 128        if ((*pos + hx->len + 4) >= fw->size)
 129                return -EINVAL;
 130
 131        hx->addr = b[1] | (b[2] << 8);
 132        hx->type = b[3];
 133
 134        if (hx->type == 0x04) {
 135                /* b[4] and b[5] are the Extended linear address record data field */
 136                hx->addr |= (b[4] << 24) | (b[5] << 16);
 137/*              hx->len -= 2;
 138                data_offs += 2; */
 139        }
 140        memcpy(hx->data,&b[data_offs],hx->len);
 141        hx->chk = b[hx->len + data_offs];
 142
 143        *pos += hx->len + 5;
 144
 145        return *pos;
 146}
 147EXPORT_SYMBOL(dvb_usb_get_hexline);
 148