Johannes Stezenbach | 776338e | 2005-06-23 22:02:35 -0700 | [diff] [blame] | 1 | /* dvb-usb-firmware.c is part of the DVB USB library. |
| 2 | * |
Patrick Boettcher | 99e44da | 2016-01-24 12:56:58 -0200 | [diff] [blame] | 3 | * Copyright (C) 2004-6 Patrick Boettcher (patrick.boettcher@posteo.de) |
Johannes Stezenbach | 776338e | 2005-06-23 22:02:35 -0700 | [diff] [blame] | 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 | |
Johannes Stezenbach | 776338e | 2005-06-23 22:02:35 -0700 | [diff] [blame] | 12 | #include <linux/usb.h> |
| 13 | |
| 14 | struct usb_cypress_controller { |
| 15 | int id; |
| 16 | const char *name; /* name of the usb controller */ |
| 17 | u16 cpu_cs_register; /* needs to be restarted, when the firmware has been downloaded. */ |
| 18 | }; |
| 19 | |
| 20 | static struct usb_cypress_controller cypress[] = { |
Patrick Boettcher | d3707ad | 2006-01-09 15:25:04 -0200 | [diff] [blame] | 21 | { .id = DEVICE_SPECIFIC, .name = "Device specific", .cpu_cs_register = 0 }, |
| 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 }, |
Johannes Stezenbach | 776338e | 2005-06-23 22:02:35 -0700 | [diff] [blame] | 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), |
Mauro Carvalho Chehab | 4302c15 | 2006-01-09 15:25:22 -0200 | [diff] [blame] | 33 | 0xa0, USB_TYPE_VENDOR, addr, 0x00, data, len, 5000); |
Johannes Stezenbach | 776338e | 2005-06-23 22:02:35 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Patrick Boettcher | f537378 | 2006-01-09 18:21:38 -0200 | [diff] [blame] | 36 | int usb_cypress_load_firmware(struct usb_device *udev, const struct firmware *fw, int type) |
Johannes Stezenbach | 776338e | 2005-06-23 22:02:35 -0700 | [diff] [blame] | 37 | { |
Mauro Carvalho Chehab | 43fab97 | 2017-01-24 08:13:11 -0200 | [diff] [blame^] | 38 | struct hexline *hx; |
Patrick Boettcher | d3707ad | 2006-01-09 15:25:04 -0200 | [diff] [blame] | 39 | u8 reset; |
| 40 | int ret,pos=0; |
Johannes Stezenbach | 776338e | 2005-06-23 22:02:35 -0700 | [diff] [blame] | 41 | |
Mauro Carvalho Chehab | 43fab97 | 2017-01-24 08:13:11 -0200 | [diff] [blame^] | 42 | hx = kmalloc(sizeof(*hx), GFP_KERNEL); |
| 43 | if (!hx) |
| 44 | return -ENOMEM; |
| 45 | |
Patrick Boettcher | d3707ad | 2006-01-09 15:25:04 -0200 | [diff] [blame] | 46 | /* stop the CPU */ |
| 47 | reset = 1; |
| 48 | if ((ret = usb_cypress_writemem(udev,cypress[type].cpu_cs_register,&reset,1)) != 1) |
| 49 | err("could not stop the USB controller CPU."); |
| 50 | |
Mauro Carvalho Chehab | 43fab97 | 2017-01-24 08:13:11 -0200 | [diff] [blame^] | 51 | while ((ret = dvb_usb_get_hexline(fw, hx, &pos)) > 0) { |
| 52 | deb_fw("writing to address 0x%04x (buffer: 0x%02x %02x)\n", hx->addr, hx->len, hx->chk); |
| 53 | ret = usb_cypress_writemem(udev, hx->addr, hx->data, hx->len); |
Patrick Boettcher | d3707ad | 2006-01-09 15:25:04 -0200 | [diff] [blame] | 54 | |
Mauro Carvalho Chehab | 43fab97 | 2017-01-24 08:13:11 -0200 | [diff] [blame^] | 55 | if (ret != hx->len) { |
Mauro Carvalho Chehab | f319ed9 | 2016-10-18 17:44:15 -0200 | [diff] [blame] | 56 | err("error while transferring firmware (transferred size: %d, block size: %d)", |
Mauro Carvalho Chehab | 43fab97 | 2017-01-24 08:13:11 -0200 | [diff] [blame^] | 57 | ret, hx->len); |
Patrick Boettcher | d3707ad | 2006-01-09 15:25:04 -0200 | [diff] [blame] | 58 | ret = -EINVAL; |
| 59 | break; |
| 60 | } |
| 61 | } |
| 62 | if (ret < 0) { |
| 63 | err("firmware download failed at %d with %d",pos,ret); |
Mauro Carvalho Chehab | 43fab97 | 2017-01-24 08:13:11 -0200 | [diff] [blame^] | 64 | kfree(hx); |
Johannes Stezenbach | 776338e | 2005-06-23 22:02:35 -0700 | [diff] [blame] | 65 | return ret; |
| 66 | } |
| 67 | |
Patrick Boettcher | d3707ad | 2006-01-09 15:25:04 -0200 | [diff] [blame] | 68 | if (ret == 0) { |
Johannes Stezenbach | 776338e | 2005-06-23 22:02:35 -0700 | [diff] [blame] | 69 | /* restart the CPU */ |
| 70 | reset = 0; |
| 71 | if (ret || usb_cypress_writemem(udev,cypress[type].cpu_cs_register,&reset,1) != 1) { |
| 72 | err("could not restart the USB controller CPU."); |
| 73 | ret = -EINVAL; |
| 74 | } |
Patrick Boettcher | d3707ad | 2006-01-09 15:25:04 -0200 | [diff] [blame] | 75 | } else |
| 76 | ret = -EIO; |
Johannes Stezenbach | 776338e | 2005-06-23 22:02:35 -0700 | [diff] [blame] | 77 | |
Mauro Carvalho Chehab | 43fab97 | 2017-01-24 08:13:11 -0200 | [diff] [blame^] | 78 | kfree(hx); |
| 79 | |
Johannes Stezenbach | 776338e | 2005-06-23 22:02:35 -0700 | [diff] [blame] | 80 | return ret; |
| 81 | } |
Patrick Boettcher | f537378 | 2006-01-09 18:21:38 -0200 | [diff] [blame] | 82 | EXPORT_SYMBOL(usb_cypress_load_firmware); |
Chris Pascoe | 0029ee1 | 2006-01-09 18:21:28 -0200 | [diff] [blame] | 83 | |
Patrick Boettcher | 4d43e13 | 2006-09-30 06:53:48 -0300 | [diff] [blame] | 84 | int dvb_usb_download_firmware(struct usb_device *udev, struct dvb_usb_device_properties *props) |
Patrick Boettcher | d3707ad | 2006-01-09 15:25:04 -0200 | [diff] [blame] | 85 | { |
| 86 | int ret; |
| 87 | const struct firmware *fw = NULL; |
| 88 | |
| 89 | if ((ret = request_firmware(&fw, props->firmware, &udev->dev)) != 0) { |
Mauro Carvalho Chehab | f319ed9 | 2016-10-18 17:44:15 -0200 | [diff] [blame] | 90 | err("did not find the firmware file. (%s) Please see linux/Documentation/dvb/ for more details on firmware-problems. (%d)", |
Patrick Boettcher | d3707ad | 2006-01-09 15:25:04 -0200 | [diff] [blame] | 91 | props->firmware,ret); |
| 92 | return ret; |
| 93 | } |
| 94 | |
| 95 | info("downloading firmware from file '%s'",props->firmware); |
| 96 | |
| 97 | switch (props->usb_ctrl) { |
| 98 | case CYPRESS_AN2135: |
| 99 | case CYPRESS_AN2235: |
| 100 | case CYPRESS_FX2: |
| 101 | ret = usb_cypress_load_firmware(udev, fw, props->usb_ctrl); |
| 102 | break; |
| 103 | case DEVICE_SPECIFIC: |
| 104 | if (props->download_firmware) |
| 105 | ret = props->download_firmware(udev,fw); |
| 106 | else { |
| 107 | err("BUG: driver didn't specified a download_firmware-callback, although it claims to have a DEVICE_SPECIFIC one."); |
| 108 | ret = -EINVAL; |
| 109 | } |
| 110 | break; |
| 111 | default: |
| 112 | ret = -EINVAL; |
| 113 | break; |
| 114 | } |
| 115 | |
| 116 | release_firmware(fw); |
| 117 | return ret; |
| 118 | } |
| 119 | |
Patrick Boettcher | 136cafb | 2006-09-19 12:51:33 -0300 | [diff] [blame] | 120 | int dvb_usb_get_hexline(const struct firmware *fw, struct hexline *hx, |
Adrian Bunk | a22a6865 | 2006-01-23 09:58:17 -0200 | [diff] [blame] | 121 | int *pos) |
Patrick Boettcher | d3707ad | 2006-01-09 15:25:04 -0200 | [diff] [blame] | 122 | { |
| 123 | u8 *b = (u8 *) &fw->data[*pos]; |
| 124 | int data_offs = 4; |
| 125 | if (*pos >= fw->size) |
| 126 | return 0; |
| 127 | |
| 128 | memset(hx,0,sizeof(struct hexline)); |
| 129 | |
| 130 | hx->len = b[0]; |
| 131 | |
| 132 | if ((*pos + hx->len + 4) >= fw->size) |
| 133 | return -EINVAL; |
| 134 | |
Al Viro | 637007f | 2008-05-21 00:33:01 -0300 | [diff] [blame] | 135 | hx->addr = b[1] | (b[2] << 8); |
Patrick Boettcher | d3707ad | 2006-01-09 15:25:04 -0200 | [diff] [blame] | 136 | hx->type = b[3]; |
| 137 | |
| 138 | if (hx->type == 0x04) { |
| 139 | /* b[4] and b[5] are the Extended linear address record data field */ |
| 140 | hx->addr |= (b[4] << 24) | (b[5] << 16); |
| 141 | /* hx->len -= 2; |
| 142 | data_offs += 2; */ |
| 143 | } |
| 144 | memcpy(hx->data,&b[data_offs],hx->len); |
| 145 | hx->chk = b[hx->len + data_offs]; |
| 146 | |
| 147 | *pos += hx->len + 5; |
| 148 | |
| 149 | return *pos; |
| 150 | } |
Patrick Boettcher | 136cafb | 2006-09-19 12:51:33 -0300 | [diff] [blame] | 151 | EXPORT_SYMBOL(dvb_usb_get_hexline); |