[PATCH] dvb: Add generalized dvb-usb driver
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / media / dvb / dvb-usb / dvb-usb-firmware.c
1 /* dvb-usb-firmware.c is part of the DVB USB library.
2  *
3  * Copyright (C) 2004-5 Patrick Boettcher (patrick.boettcher@desy.de)
4  * 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
12 #include <linux/firmware.h>
13 #include <linux/usb.h>
14
15 struct 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
21 static struct usb_cypress_controller cypress[] = {
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 },
25 };
26
27 /*
28  * load a firmware packet to the device
29  */
30 static 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),
33                         0xa0, USB_TYPE_VENDOR, addr, 0x00, data, len, 5*HZ);
34 }
35
36 int usb_cypress_load_firmware(struct usb_device *udev, const char *filename, int type)
37 {
38         const struct firmware *fw = NULL;
39         u16 addr;
40         u8 *b,*p;
41         int ret = 0,i;
42
43         if ((ret = request_firmware(&fw, filename, &udev->dev)) != 0) {
44                 err("did not find the firmware file. (%s) "
45                         "Please see linux/Documentation/dvb/ for more details on firmware-problems.",
46                         filename);
47                 return ret;
48         }
49
50         info("downloading firmware from file '%s' to the '%s'",filename,cypress[type].name);
51
52         p = kmalloc(fw->size,GFP_KERNEL);
53         if (p != NULL) {
54                 u8 reset;
55                 /*
56                  * you cannot use the fw->data as buffer for
57                  * usb_control_msg, a new buffer has to be
58                  * created
59                  */
60                 memcpy(p,fw->data,fw->size);
61
62                 /* stop the CPU */
63                 reset = 1;
64                 if ((ret = usb_cypress_writemem(udev,cypress[type].cpu_cs_register,&reset,1)) != 1)
65                         err("could not stop the USB controller CPU.");
66                 for(i = 0; p[i+3] == 0 && i < fw->size; ) {
67                         b = (u8 *) &p[i];
68                         addr = cpu_to_le16( *((u16 *) &b[1]) );
69
70                         deb_fw("writing to address 0x%04x (buffer: 0x%02x%02x)\n",addr,b[1],b[2]);
71
72                         ret = usb_cypress_writemem(udev,addr,&b[4],b[0]);
73
74                         if (ret != b[0]) {
75                                 err("error while transferring firmware "
76                                         "(transferred size: %d, block size: %d)",
77                                         ret,b[0]);
78                                 ret = -EINVAL;
79                                 break;
80                         }
81                         i += 5 + b[0];
82                 }
83                 /* length in ret */
84                 if (ret > 0)
85                         ret = 0;
86                 /* restart the CPU */
87                 reset = 0;
88                 if (ret || usb_cypress_writemem(udev,cypress[type].cpu_cs_register,&reset,1) != 1) {
89                         err("could not restart the USB controller CPU.");
90                         ret = -EINVAL;
91                 }
92
93                 kfree(p);
94         } else {
95                 ret = -ENOMEM;
96         }
97         release_firmware(fw);
98
99         return ret;
100 }