Linux-2.6.12-rc2
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / media / dvb / dibusb / dvb-dibusb-firmware.c
1 /*
2 * dvb-dibusb-firmware.c is part of the driver for mobile USB Budget DVB-T devices
3 * based on reference design made by DiBcom (http://www.dibcom.fr/)
4 *
5 * Copyright (C) 2004-5 Patrick Boettcher (patrick.boettcher@desy.de)
6 *
7 * see dvb-dibusb-core.c for more copyright details.
8 *
9 * This file contains functions for downloading the firmware to the device.
10 */
11 #include "dvb-dibusb.h"
12
13 #include <linux/firmware.h>
14 #include <linux/usb.h>
15
16 /*
17 * load a firmware packet to the device
18 */
19 static int dibusb_writemem(struct usb_device *udev,u16 addr,u8 *data, u8 len)
20 {
21 return usb_control_msg(udev, usb_sndctrlpipe(udev,0),
22 0xa0, USB_TYPE_VENDOR, addr, 0x00, data, len, 5000);
23 }
24
25 int dibusb_loadfirmware(struct usb_device *udev, struct dibusb_usb_device *dibdev)
26 {
27 const struct firmware *fw = NULL;
28 u16 addr;
29 u8 *b,*p;
30 int ret = 0,i;
31
32 if ((ret = request_firmware(&fw, dibdev->dev_cl->firmware, &udev->dev)) != 0) {
33 err("did not find the firmware file. (%s) "
34 "Please see linux/Documentation/dvb/ for more details on firmware-problems.",
35 dibdev->dev_cl->firmware);
36 return ret;
37 }
38
39 info("downloading firmware from file '%s'.",dibdev->dev_cl->firmware);
40
41 p = kmalloc(fw->size,GFP_KERNEL);
42 if (p != NULL) {
43 u8 reset;
44 /*
45 * you cannot use the fw->data as buffer for
46 * usb_control_msg, a new buffer has to be
47 * created
48 */
49 memcpy(p,fw->data,fw->size);
50
51 /* stop the CPU */
52 reset = 1;
53 if ((ret = dibusb_writemem(udev,dibdev->dev_cl->usb_ctrl->cpu_cs_register,&reset,1)) != 1)
54 err("could not stop the USB controller CPU.");
55 for(i = 0; p[i+3] == 0 && i < fw->size; ) {
56 b = (u8 *) &p[i];
57 addr = *((u16 *) &b[1]);
58
59 ret = dibusb_writemem(udev,addr,&b[4],b[0]);
60
61 if (ret != b[0]) {
62 err("error while transferring firmware "
63 "(transferred size: %d, block size: %d)",
64 ret,b[0]);
65 ret = -EINVAL;
66 break;
67 }
68 i += 5 + b[0];
69 }
70 /* length in ret */
71 if (ret > 0)
72 ret = 0;
73 /* restart the CPU */
74 reset = 0;
75 if (ret || dibusb_writemem(udev,dibdev->dev_cl->usb_ctrl->cpu_cs_register,&reset,1) != 1) {
76 err("could not restart the USB controller CPU.");
77 ret = -EINVAL;
78 }
79
80 kfree(p);
81 } else {
82 ret = -ENOMEM;
83 }
84 release_firmware(fw);
85
86 return ret;
87 }