]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/media/common/cypress_firmware.c
Merge tag 'scsi-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi
[mirror_ubuntu-bionic-kernel.git] / drivers / media / common / cypress_firmware.c
CommitLineData
355b4b2b 1/* cypress_firmware.c is part of the DVB USB library.
b00a9018 2 *
99e44da7 3 * Copyright (C) 2004-6 Patrick Boettcher (patrick.boettcher@posteo.de)
b00a9018
AP
4 * see dvb-usb-init.c for copyright information.
5 *
6 * This file contains functions for downloading the firmware to Cypress FX 1
7 * and 2 based devices.
8 *
9 */
10
79a63c60
HV
11#include <linux/module.h>
12#include <linux/slab.h>
13#include <linux/usb.h>
14#include <linux/firmware.h>
355b4b2b 15#include "cypress_firmware.h"
b00a9018
AP
16
17struct usb_cypress_controller {
18 u8 id;
19 const char *name; /* name of the usb controller */
20 u16 cs_reg; /* needs to be restarted,
21 * when the firmware has been downloaded */
22};
23
24static const struct usb_cypress_controller cypress[] = {
25 { .id = CYPRESS_AN2135, .name = "Cypress AN2135", .cs_reg = 0x7f92 },
26 { .id = CYPRESS_AN2235, .name = "Cypress AN2235", .cs_reg = 0x7f92 },
27 { .id = CYPRESS_FX2, .name = "Cypress FX2", .cs_reg = 0xe600 },
28};
29
30/*
31 * load a firmware packet to the device
32 */
33static int usb_cypress_writemem(struct usb_device *udev, u16 addr, u8 *data,
34 u8 len)
35{
36 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
37 0xa0, USB_TYPE_VENDOR, addr, 0x00, data, len, 5000);
38}
39
79a63c60
HV
40static int cypress_get_hexline(const struct firmware *fw,
41 struct hexline *hx, int *pos)
42{
43 u8 *b = (u8 *) &fw->data[*pos];
44 int data_offs = 4;
45
46 if (*pos >= fw->size)
47 return 0;
48
49 memset(hx, 0, sizeof(struct hexline));
50 hx->len = b[0];
51
52 if ((*pos + hx->len + 4) >= fw->size)
53 return -EINVAL;
54
55 hx->addr = b[1] | (b[2] << 8);
56 hx->type = b[3];
57
58 if (hx->type == 0x04) {
59 /* b[4] and b[5] are the Extended linear address record data
60 * field */
61 hx->addr |= (b[4] << 24) | (b[5] << 16);
62 }
63
64 memcpy(hx->data, &b[data_offs], hx->len);
65 hx->chk = b[hx->len + data_offs];
66 *pos += hx->len + 5;
67
68 return *pos;
69}
70
71int cypress_load_firmware(struct usb_device *udev,
b00a9018
AP
72 const struct firmware *fw, int type)
73{
2347e683 74 struct hexline *hx;
b00a9018
AP
75 int ret, pos = 0;
76
2d3da59f 77 hx = kmalloc(sizeof(*hx), GFP_KERNEL);
c38e8657 78 if (!hx)
2347e683 79 return -ENOMEM;
2347e683 80
b00a9018 81 /* stop the CPU */
2347e683
AP
82 hx->data[0] = 1;
83 ret = usb_cypress_writemem(udev, cypress[type].cs_reg, hx->data, 1);
84 if (ret != 1) {
85 dev_err(&udev->dev, "%s: CPU stop failed=%d\n",
86 KBUILD_MODNAME, ret);
87 ret = -EIO;
88 goto err_kfree;
89 }
90
91 /* write firmware to memory */
92 for (;;) {
79a63c60 93 ret = cypress_get_hexline(fw, hx, &pos);
2347e683
AP
94 if (ret < 0)
95 goto err_kfree;
96 else if (ret == 0)
97 break;
98
99 ret = usb_cypress_writemem(udev, hx->addr, hx->data, hx->len);
100 if (ret < 0) {
101 goto err_kfree;
102 } else if (ret != hx->len) {
61356eea
AP
103 dev_err(&udev->dev,
104 "%s: error while transferring firmware (transferred size=%d, block size=%d)\n",
2347e683
AP
105 KBUILD_MODNAME, ret, hx->len);
106 ret = -EIO;
107 goto err_kfree;
b00a9018
AP
108 }
109 }
b00a9018 110
2347e683
AP
111 /* start the CPU */
112 hx->data[0] = 0;
113 ret = usb_cypress_writemem(udev, cypress[type].cs_reg, hx->data, 1);
114 if (ret != 1) {
115 dev_err(&udev->dev, "%s: CPU start failed=%d\n",
116 KBUILD_MODNAME, ret);
b00a9018 117 ret = -EIO;
2347e683
AP
118 goto err_kfree;
119 }
b00a9018 120
2347e683
AP
121 ret = 0;
122err_kfree:
123 kfree(hx);
b00a9018
AP
124 return ret;
125}
79a63c60 126EXPORT_SYMBOL(cypress_load_firmware);
b00a9018
AP
127
128MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
129MODULE_DESCRIPTION("Cypress firmware download");
130MODULE_LICENSE("GPL");