1 /* DVB USB compliant Linux driver for the 2 * - GENPIX 8pks/qpsk/DCII USB2.0 DVB-S module 3 * 4 * Copyright (C) 2006,2007 Alan Nisota (alannisota@gmail.com) 5 * Copyright (C) 2006,2007 Genpix Electronics (genpix@genpix-electronics.com) 6 * 7 * Thanks to GENPIX for the sample code used to implement this module. 8 * 9 * This module is based off the vp7045 and vp702x modules 10 * 11 * This program is free software; you can redistribute it and/or modify it 12 * under the terms of the GNU General Public License as published by the Free 13 * Software Foundation, version 2. 14 * 15 * see Documentation/dvb/README.dvb-usb for more information 16 */ 17 #include "gp8psk.h" 18 19 /* debug */ 20 static char bcm4500_firmware[] = "dvb-usb-gp8psk-02.fw"; 21 int dvb_usb_gp8psk_debug; 22 module_param_named(debug,dvb_usb_gp8psk_debug, int, 0644); 23 MODULE_PARM_DESC(debug, "set debugging level (1=info,xfer=2,rc=4 (or-able))." DVB_USB_DEBUG_STATUS); 24 25 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr); 26 27 struct gp8psk_state { 28 unsigned char data[80]; 29 }; 30 31 static int gp8psk_get_fw_version(struct dvb_usb_device *d, u8 *fw_vers) 32 { 33 return (gp8psk_usb_in_op(d, GET_FW_VERS, 0, 0, fw_vers, 6)); 34 } 35 36 static int gp8psk_get_fpga_version(struct dvb_usb_device *d, u8 *fpga_vers) 37 { 38 return (gp8psk_usb_in_op(d, GET_FPGA_VERS, 0, 0, fpga_vers, 1)); 39 } 40 41 static void gp8psk_info(struct dvb_usb_device *d) 42 { 43 u8 fpga_vers, fw_vers[6]; 44 45 if (!gp8psk_get_fw_version(d, fw_vers)) 46 info("FW Version = %i.%02i.%i (0x%x) Build %4i/%02i/%02i", 47 fw_vers[2], fw_vers[1], fw_vers[0], GP8PSK_FW_VERS(fw_vers), 48 2000 + fw_vers[5], fw_vers[4], fw_vers[3]); 49 else 50 info("failed to get FW version"); 51 52 if (!gp8psk_get_fpga_version(d, &fpga_vers)) 53 info("FPGA Version = %i", fpga_vers); 54 else 55 info("failed to get FPGA version"); 56 } 57 58 int gp8psk_usb_in_op(struct dvb_usb_device *d, u8 req, u16 value, u16 index, u8 *b, int blen) 59 { 60 struct gp8psk_state *st = d->priv; 61 int ret = 0,try = 0; 62 63 if (blen > sizeof(st->data)) 64 return -EIO; 65 66 if ((ret = mutex_lock_interruptible(&d->usb_mutex))) 67 return ret; 68 69 while (ret >= 0 && ret != blen && try < 3) { 70 memcpy(st->data, b, blen); 71 ret = usb_control_msg(d->udev, 72 usb_rcvctrlpipe(d->udev,0), 73 req, 74 USB_TYPE_VENDOR | USB_DIR_IN, 75 value, index, st->data, blen, 76 2000); 77 deb_info("reading number %d (ret: %d)\n",try,ret); 78 try++; 79 } 80 81 if (ret < 0 || ret != blen) { 82 warn("usb in %d operation failed.", req); 83 ret = -EIO; 84 } else 85 ret = 0; 86 87 deb_xfer("in: req. %x, val: %x, ind: %x, buffer: ",req,value,index); 88 debug_dump(b,blen,deb_xfer); 89 90 mutex_unlock(&d->usb_mutex); 91 92 return ret; 93 } 94 95 int gp8psk_usb_out_op(struct dvb_usb_device *d, u8 req, u16 value, 96 u16 index, u8 *b, int blen) 97 { 98 struct gp8psk_state *st = d->priv; 99 int ret; 100 101 deb_xfer("out: req. %x, val: %x, ind: %x, buffer: ",req,value,index); 102 debug_dump(b,blen,deb_xfer); 103 104 if (blen > sizeof(st->data)) 105 return -EIO; 106 107 if ((ret = mutex_lock_interruptible(&d->usb_mutex))) 108 return ret; 109 110 memcpy(st->data, b, blen); 111 if (usb_control_msg(d->udev, 112 usb_sndctrlpipe(d->udev,0), 113 req, 114 USB_TYPE_VENDOR | USB_DIR_OUT, 115 value, index, st->data, blen, 116 2000) != blen) { 117 warn("usb out operation failed."); 118 ret = -EIO; 119 } else 120 ret = 0; 121 mutex_unlock(&d->usb_mutex); 122 123 return ret; 124 } 125 126 static int gp8psk_load_bcm4500fw(struct dvb_usb_device *d) 127 { 128 int ret; 129 const struct firmware *fw = NULL; 130 const u8 *ptr; 131 u8 *buf; 132 if ((ret = request_firmware(&fw, bcm4500_firmware, 133 &d->udev->dev)) != 0) { 134 err("did not find the bcm4500 firmware file. (%s) " 135 "Please see linux/Documentation/dvb/ for more details on firmware-problems. (%d)", 136 bcm4500_firmware,ret); 137 return ret; 138 } 139 140 ret = -EINVAL; 141 142 if (gp8psk_usb_out_op(d, LOAD_BCM4500,1,0,NULL, 0)) 143 goto out_rel_fw; 144 145 info("downloading bcm4500 firmware from file '%s'",bcm4500_firmware); 146 147 ptr = fw->data; 148 buf = kmalloc(64, GFP_KERNEL | GFP_DMA); 149 if (!buf) { 150 ret = -ENOMEM; 151 goto out_rel_fw; 152 } 153 154 while (ptr[0] != 0xff) { 155 u16 buflen = ptr[0] + 4; 156 if (ptr + buflen >= fw->data + fw->size) { 157 err("failed to load bcm4500 firmware."); 158 goto out_free; 159 } 160 if (buflen > 64) { 161 err("firmare chunk size bigger than 64 bytes."); 162 goto out_free; 163 } 164 165 memcpy(buf, ptr, buflen); 166 if (dvb_usb_generic_write(d, buf, buflen)) { 167 err("failed to load bcm4500 firmware."); 168 goto out_free; 169 } 170 ptr += buflen; 171 } 172 173 ret = 0; 174 175 out_free: 176 kfree(buf); 177 out_rel_fw: 178 release_firmware(fw); 179 180 return ret; 181 } 182 183 static int gp8psk_power_ctrl(struct dvb_usb_device *d, int onoff) 184 { 185 u8 status, buf; 186 int gp_product_id = le16_to_cpu(d->udev->descriptor.idProduct); 187 188 if (onoff) { 189 gp8psk_usb_in_op(d, GET_8PSK_CONFIG,0,0,&status,1); 190 if (! (status & bm8pskStarted)) { /* started */ 191 if(gp_product_id == USB_PID_GENPIX_SKYWALKER_CW3K) 192 gp8psk_usb_out_op(d, CW3K_INIT, 1, 0, NULL, 0); 193 if (gp8psk_usb_in_op(d, BOOT_8PSK, 1, 0, &buf, 1)) 194 return -EINVAL; 195 gp8psk_info(d); 196 } 197 198 if (gp_product_id == USB_PID_GENPIX_8PSK_REV_1_WARM) 199 if (! (status & bm8pskFW_Loaded)) /* BCM4500 firmware loaded */ 200 if(gp8psk_load_bcm4500fw(d)) 201 return -EINVAL; 202 203 if (! (status & bmIntersilOn)) /* LNB Power */ 204 if (gp8psk_usb_in_op(d, START_INTERSIL, 1, 0, 205 &buf, 1)) 206 return -EINVAL; 207 208 /* Set DVB mode to 1 */ 209 if (gp_product_id == USB_PID_GENPIX_8PSK_REV_1_WARM) 210 if (gp8psk_usb_out_op(d, SET_DVB_MODE, 1, 0, NULL, 0)) 211 return -EINVAL; 212 /* Abort possible TS (if previous tune crashed) */ 213 if (gp8psk_usb_out_op(d, ARM_TRANSFER, 0, 0, NULL, 0)) 214 return -EINVAL; 215 } else { 216 /* Turn off LNB power */ 217 if (gp8psk_usb_in_op(d, START_INTERSIL, 0, 0, &buf, 1)) 218 return -EINVAL; 219 /* Turn off 8psk power */ 220 if (gp8psk_usb_in_op(d, BOOT_8PSK, 0, 0, &buf, 1)) 221 return -EINVAL; 222 if(gp_product_id == USB_PID_GENPIX_SKYWALKER_CW3K) 223 gp8psk_usb_out_op(d, CW3K_INIT, 0, 0, NULL, 0); 224 } 225 return 0; 226 } 227 228 int gp8psk_bcm4500_reload(struct dvb_usb_device *d) 229 { 230 u8 buf; 231 int gp_product_id = le16_to_cpu(d->udev->descriptor.idProduct); 232 /* Turn off 8psk power */ 233 if (gp8psk_usb_in_op(d, BOOT_8PSK, 0, 0, &buf, 1)) 234 return -EINVAL; 235 /* Turn On 8psk power */ 236 if (gp8psk_usb_in_op(d, BOOT_8PSK, 1, 0, &buf, 1)) 237 return -EINVAL; 238 /* load BCM4500 firmware */ 239 if (gp_product_id == USB_PID_GENPIX_8PSK_REV_1_WARM) 240 if (gp8psk_load_bcm4500fw(d)) 241 return -EINVAL; 242 return 0; 243 } 244 245 static int gp8psk_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff) 246 { 247 return gp8psk_usb_out_op(adap->dev, ARM_TRANSFER, onoff, 0 , NULL, 0); 248 } 249 250 static int gp8psk_frontend_attach(struct dvb_usb_adapter *adap) 251 { 252 adap->fe_adap[0].fe = gp8psk_fe_attach(adap->dev); 253 return 0; 254 } 255 256 static struct dvb_usb_device_properties gp8psk_properties; 257 258 static int gp8psk_usb_probe(struct usb_interface *intf, 259 const struct usb_device_id *id) 260 { 261 int ret; 262 struct usb_device *udev = interface_to_usbdev(intf); 263 ret = dvb_usb_device_init(intf, &gp8psk_properties, 264 THIS_MODULE, NULL, adapter_nr); 265 if (ret == 0) { 266 info("found Genpix USB device pID = %x (hex)", 267 le16_to_cpu(udev->descriptor.idProduct)); 268 } 269 return ret; 270 } 271 272 static struct usb_device_id gp8psk_usb_table [] = { 273 { USB_DEVICE(USB_VID_GENPIX, USB_PID_GENPIX_8PSK_REV_1_COLD) }, 274 { USB_DEVICE(USB_VID_GENPIX, USB_PID_GENPIX_8PSK_REV_1_WARM) }, 275 { USB_DEVICE(USB_VID_GENPIX, USB_PID_GENPIX_8PSK_REV_2) }, 276 { USB_DEVICE(USB_VID_GENPIX, USB_PID_GENPIX_SKYWALKER_1) }, 277 { USB_DEVICE(USB_VID_GENPIX, USB_PID_GENPIX_SKYWALKER_2) }, 278 /* { USB_DEVICE(USB_VID_GENPIX, USB_PID_GENPIX_SKYWALKER_CW3K) }, */ 279 { 0 }, 280 }; 281 MODULE_DEVICE_TABLE(usb, gp8psk_usb_table); 282 283 static struct dvb_usb_device_properties gp8psk_properties = { 284 .usb_ctrl = CYPRESS_FX2, 285 .firmware = "dvb-usb-gp8psk-01.fw", 286 287 .size_of_priv = sizeof(struct gp8psk_state), 288 289 .num_adapters = 1, 290 .adapter = { 291 { 292 .num_frontends = 1, 293 .fe = {{ 294 .streaming_ctrl = gp8psk_streaming_ctrl, 295 .frontend_attach = gp8psk_frontend_attach, 296 /* parameter for the MPEG2-data transfer */ 297 .stream = { 298 .type = USB_BULK, 299 .count = 7, 300 .endpoint = 0x82, 301 .u = { 302 .bulk = { 303 .buffersize = 8192, 304 } 305 } 306 }, 307 }}, 308 } 309 }, 310 .power_ctrl = gp8psk_power_ctrl, 311 312 .generic_bulk_ctrl_endpoint = 0x01, 313 314 .num_device_descs = 4, 315 .devices = { 316 { .name = "Genpix 8PSK-to-USB2 Rev.1 DVB-S receiver", 317 .cold_ids = { &gp8psk_usb_table[0], NULL }, 318 .warm_ids = { &gp8psk_usb_table[1], NULL }, 319 }, 320 { .name = "Genpix 8PSK-to-USB2 Rev.2 DVB-S receiver", 321 .cold_ids = { NULL }, 322 .warm_ids = { &gp8psk_usb_table[2], NULL }, 323 }, 324 { .name = "Genpix SkyWalker-1 DVB-S receiver", 325 .cold_ids = { NULL }, 326 .warm_ids = { &gp8psk_usb_table[3], NULL }, 327 }, 328 { .name = "Genpix SkyWalker-2 DVB-S receiver", 329 .cold_ids = { NULL }, 330 .warm_ids = { &gp8psk_usb_table[4], NULL }, 331 }, 332 { NULL }, 333 } 334 }; 335 336 /* usb specific object needed to register this driver with the usb subsystem */ 337 static struct usb_driver gp8psk_usb_driver = { 338 .name = "dvb_usb_gp8psk", 339 .probe = gp8psk_usb_probe, 340 .disconnect = dvb_usb_device_exit, 341 .id_table = gp8psk_usb_table, 342 }; 343 344 module_usb_driver(gp8psk_usb_driver); 345 346 MODULE_AUTHOR("Alan Nisota <alannisota@gamil.com>"); 347 MODULE_DESCRIPTION("Driver for Genpix DVB-S"); 348 MODULE_VERSION("1.1"); 349 MODULE_LICENSE("GPL"); 350