1 2#include "dvb-usb-common.h" 3 4#include <linux/usb.h> 5 6struct usb_cypress_controller { 7 int id; 8 const char *name; /* name of the usb controller */ 9 u16 cpu_cs_register; /* needs to be restarted, when the firmware has been downloaded. */ 10}; 11 12static struct usb_cypress_controller cypress[] = { 13 { .id = DEVICE_SPECIFIC, .name = "Device specific", .cpu_cs_register = 0 }, 14 { .id = CYPRESS_AN2135, .name = "Cypress AN2135", .cpu_cs_register = 0x7f92 }, 15 { .id = CYPRESS_AN2235, .name = "Cypress AN2235", .cpu_cs_register = 0x7f92 }, 16 { .id = CYPRESS_FX2, .name = "Cypress FX2", .cpu_cs_register = 0xe600 }, 17}; 18 19/* 20 * load a firmware packet to the device 21 */ 22static int usb_cypress_writemem(struct usb_device *udev,u16 addr,u8 *data, u8 len) 23{ 24 return usb_control_msg(udev, usb_sndctrlpipe(udev,0), 25 0xa0, USB_TYPE_VENDOR, addr, 0x00, data, len, 5000); 26} 27 28int usb_cypress_load_firmware(struct usb_device *udev, const struct firmware *fw, int type) 29{ 30 struct hexline hx; 31 u8 reset; 32 int ret,pos=0; 33 34 /* stop the CPU */ 35 reset = 1; 36 if ((ret = usb_cypress_writemem(udev,cypress[type].cpu_cs_register,&reset,1)) != 1) 37 err("could not stop the USB controller CPU."); 38 39 while ((ret = dvb_usb_get_hexline(fw,&hx,&pos)) > 0) { 40 deb_fw("writing to address 0x%04x (buffer: 0x%02x %02x)\n",hx.addr,hx.len,hx.chk); 41 ret = usb_cypress_writemem(udev,hx.addr,hx.data,hx.len); 42 43 if (ret != hx.len) { 44 err("error while transferring firmware " 45 "(transferred size: %d, block size: %d)", 46 ret,hx.len); 47 ret = -EINVAL; 48 break; 49 } 50 } 51 if (ret < 0) { 52 err("firmware download failed at %d with %d",pos,ret); 53 return ret; 54 } 55 56 if (ret == 0) { 57 /* restart the CPU */ 58 reset = 0; 59 if (ret || usb_cypress_writemem(udev,cypress[type].cpu_cs_register,&reset,1) != 1) { 60 err("could not restart the USB controller CPU."); 61 ret = -EINVAL; 62 } 63 } else 64 ret = -EIO; 65 66 return ret; 67} 68EXPORT_SYMBOL(usb_cypress_load_firmware); 69 70int dvb_usb_download_firmware(struct usb_device *udev, struct dvb_usb_device_properties *props) 71{ 72 int ret; 73 const struct firmware *fw = NULL; 74 75 if ((ret = request_firmware(&fw, props->firmware, &udev->dev)) != 0) { 76 err("did not find the firmware file. (%s) " 77 "Please see linux/Documentation/dvb/ for more details on firmware-problems. (%d)", 78 props->firmware,ret); 79 return ret; 80 } 81 82 info("downloading firmware from file '%s'",props->firmware); 83 84 switch (props->usb_ctrl) { 85 case CYPRESS_AN2135: 86 case CYPRESS_AN2235: 87 case CYPRESS_FX2: 88 ret = usb_cypress_load_firmware(udev, fw, props->usb_ctrl); 89 break; 90 case DEVICE_SPECIFIC: 91 if (props->download_firmware) 92 ret = props->download_firmware(udev,fw); 93 else { 94 err("BUG: driver didn't specified a download_firmware-callback, although it claims to have a DEVICE_SPECIFIC one."); 95 ret = -EINVAL; 96 } 97 break; 98 default: 99 ret = -EINVAL; 100 break; 101 } 102 103 release_firmware(fw); 104 return ret; 105} 106 107int dvb_usb_get_hexline(const struct firmware *fw, struct hexline *hx, 108 int *pos) 109{ 110 u8 *b = (u8 *) &fw->data[*pos]; 111 int data_offs = 4; 112 if (*pos >= fw->size) 113 return 0; 114 115 memset(hx,0,sizeof(struct hexline)); 116 117 hx->len = b[0]; 118 119 if ((*pos + hx->len + 4) >= fw->size) 120 return -EINVAL; 121 122 hx->addr = b[1] | (b[2] << 8); 123 hx->type = b[3]; 124 125 if (hx->type == 0x04) { 126 /* b[4] and b[5] are the Extended linear address record data field */ 127 hx->addr |= (b[4] << 24) | (b[5] << 16); 128/* hx->len -= 2; 129 data_offs += 2; */ 130 } 131 memcpy(hx->data,&b[data_offs],hx->len); 132 hx->chk = b[hx->len + data_offs]; 133 134 *pos += hx->len + 5; 135 136 return *pos; 137} 138EXPORT_SYMBOL(dvb_usb_get_hexline); 139