Mauro Carvalho Chehab | 71d6763 | 2011-07-21 17:46:41 -0300 | [diff] [blame] | 1 | /* DVB USB compliant Linux driver for the AzureWave 6017 USB2.0 DVB-S |
| 2 | * receiver. |
| 3 | * see Documentation/dvb/README.dvb-usb for more information |
| 4 | */ |
| 5 | |
| 6 | #include "az6007.h" |
| 7 | #include "drxk.h" |
| 8 | #include "mt2063.h" |
| 9 | #include "dvb_ca_en50221.h" |
| 10 | |
Mauro Carvalho Chehab | 6da3470 | 2011-07-21 18:31:14 -0300 | [diff] [blame] | 11 | /* HACK: Should be moved to the right place */ |
| 12 | #define USB_PID_AZUREWAVE_6007 0xccd |
| 13 | #define USB_PID_TERRATEC_H7 0x10b4 |
| 14 | |
Mauro Carvalho Chehab | 71d6763 | 2011-07-21 17:46:41 -0300 | [diff] [blame] | 15 | /* debug */ |
| 16 | int dvb_usb_az6007_debug; |
| 17 | module_param_named(debug,dvb_usb_az6007_debug, int, 0644); |
| 18 | MODULE_PARM_DESC(debug, "set debugging level (1=info,xfer=2,rc=4 (or-able))." DVB_USB_DEBUG_STATUS); |
| 19 | |
| 20 | |
| 21 | static int az6007_type =0; |
| 22 | module_param(az6007_type, int, 0644); |
| 23 | MODULE_PARM_DESC(az6007_type, "select delivery mode (0=DVB-T, 1=DVB-T"); |
| 24 | |
| 25 | //module_param_named(type, 6007_type, int, 0644); |
| 26 | //MODULE_PARM_DESC(type, "select delivery mode (0=DVB-T, 1=DVB-C)"); |
| 27 | |
| 28 | DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr); |
| 29 | |
| 30 | |
| 31 | struct az6007_device_state { |
| 32 | struct dvb_ca_en50221 ca; |
| 33 | struct mutex ca_mutex; |
| 34 | u8 power_state; |
Mauro Carvalho Chehab | 6da3470 | 2011-07-21 18:31:14 -0300 | [diff] [blame] | 35 | |
| 36 | /* Due to DRX-K - probably need changes */ |
| 37 | int (*gate_ctrl)(struct dvb_frontend *, int); |
| 38 | struct semaphore pll_mutex; |
| 39 | bool dont_attach_fe1; |
Mauro Carvalho Chehab | 71d6763 | 2011-07-21 17:46:41 -0300 | [diff] [blame] | 40 | }; |
| 41 | |
Mauro Carvalho Chehab | 6da3470 | 2011-07-21 18:31:14 -0300 | [diff] [blame] | 42 | struct drxk_config terratec_h7_drxk = { |
| 43 | .adr = 0x29, |
| 44 | .single_master = 1, |
Mauro Carvalho Chehab | d20a7f7 | 2011-07-23 09:51:12 -0300 | [diff] [blame^] | 45 | .no_i2c_bridge = 0, |
Mauro Carvalho Chehab | 6da3470 | 2011-07-21 18:31:14 -0300 | [diff] [blame] | 46 | .microcode_name = "dvb-usb-terratec-h5-drxk.fw", |
Mauro Carvalho Chehab | 71d6763 | 2011-07-21 17:46:41 -0300 | [diff] [blame] | 47 | }; |
| 48 | |
Mauro Carvalho Chehab | 6da3470 | 2011-07-21 18:31:14 -0300 | [diff] [blame] | 49 | static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable) |
| 50 | { |
| 51 | struct dvb_usb_adapter *adap = fe->sec_priv; |
Mauro Carvalho Chehab | caa1a70 | 2011-07-22 10:31:25 -0300 | [diff] [blame] | 52 | struct az6007_device_state *st; |
Mauro Carvalho Chehab | 6da3470 | 2011-07-21 18:31:14 -0300 | [diff] [blame] | 53 | int status; |
| 54 | |
Mauro Carvalho Chehab | caa1a70 | 2011-07-22 10:31:25 -0300 | [diff] [blame] | 55 | info("%s", __func__); |
| 56 | |
| 57 | if (!adap) |
Mauro Carvalho Chehab | 6da3470 | 2011-07-21 18:31:14 -0300 | [diff] [blame] | 58 | return -EINVAL; |
| 59 | |
Mauro Carvalho Chehab | caa1a70 | 2011-07-22 10:31:25 -0300 | [diff] [blame] | 60 | st = adap->priv; |
| 61 | |
| 62 | if (!st) |
| 63 | return -EINVAL; |
| 64 | |
| 65 | |
Mauro Carvalho Chehab | 6da3470 | 2011-07-21 18:31:14 -0300 | [diff] [blame] | 66 | if (enable) { |
| 67 | down(&st->pll_mutex); |
| 68 | status = st->gate_ctrl(fe, 1); |
| 69 | } else { |
| 70 | status = st->gate_ctrl(fe, 0); |
| 71 | up(&st->pll_mutex); |
| 72 | } |
| 73 | return status; |
| 74 | } |
Mauro Carvalho Chehab | 71d6763 | 2011-07-21 17:46:41 -0300 | [diff] [blame] | 75 | |
| 76 | struct mt2063_config az6007_mt2063_config = { |
Mauro Carvalho Chehab | caa1a70 | 2011-07-22 10:31:25 -0300 | [diff] [blame] | 77 | .tuner_address = 0x60, |
Mauro Carvalho Chehab | 71d6763 | 2011-07-21 17:46:41 -0300 | [diff] [blame] | 78 | .refclock = 36125000, |
| 79 | }; |
| 80 | |
| 81 | /* check for mutex FIXME */ |
| 82 | int az6007_usb_in_op(struct dvb_usb_device *d, u8 req, u16 value, u16 index, u8 *b, int blen) |
| 83 | { |
| 84 | int ret = -1; |
| 85 | |
| 86 | ret = usb_control_msg(d->udev, |
| 87 | usb_rcvctrlpipe(d->udev,0), |
| 88 | req, |
| 89 | USB_TYPE_VENDOR | USB_DIR_IN, |
| 90 | value,index,b,blen, |
| 91 | 5000); |
| 92 | |
| 93 | if (ret < 0) { |
| 94 | warn("usb in operation failed. (%d)", ret); |
Mauro Carvalho Chehab | caa1a70 | 2011-07-22 10:31:25 -0300 | [diff] [blame] | 95 | return -EIO; |
| 96 | } |
Mauro Carvalho Chehab | 71d6763 | 2011-07-21 17:46:41 -0300 | [diff] [blame] | 97 | |
| 98 | deb_xfer("in: req. %02x, val: %04x, ind: %04x, buffer: ",req,value,index); |
| 99 | debug_dump(b,blen,deb_xfer); |
| 100 | |
| 101 | return ret; |
| 102 | } |
| 103 | |
| 104 | static int az6007_usb_out_op(struct dvb_usb_device *d, u8 req, u16 value, |
| 105 | u16 index, u8 *b, int blen) |
| 106 | { |
| 107 | int ret; |
| 108 | |
Mauro Carvalho Chehab | 6da3470 | 2011-07-21 18:31:14 -0300 | [diff] [blame] | 109 | #if 0 |
Mauro Carvalho Chehab | 71d6763 | 2011-07-21 17:46:41 -0300 | [diff] [blame] | 110 | int i=0, cyc=0, rem=0; |
| 111 | cyc = blen/64; |
| 112 | rem = blen%64; |
| 113 | #endif |
| 114 | |
| 115 | deb_xfer("out: req. %02x, val: %04x, ind: %04x, buffer: ",req,value,index); |
| 116 | debug_dump(b,blen,deb_xfer); |
| 117 | |
Mauro Carvalho Chehab | 6da3470 | 2011-07-21 18:31:14 -0300 | [diff] [blame] | 118 | |
Mauro Carvalho Chehab | 71d6763 | 2011-07-21 17:46:41 -0300 | [diff] [blame] | 119 | #if 0 |
| 120 | if (blen>64) |
| 121 | { |
| 122 | for (i=0; i<cyc; i++) |
| 123 | { |
| 124 | if ((ret = usb_control_msg(d->udev, |
| 125 | usb_sndctrlpipe(d->udev,0), |
| 126 | req, |
| 127 | USB_TYPE_VENDOR | USB_DIR_OUT, |
| 128 | value,index+i*64,b+i*64,64, |
| 129 | 5000)) != 64) { |
| 130 | warn("usb out operation failed. (%d)",ret); |
| 131 | return -EIO; |
Mauro Carvalho Chehab | 6da3470 | 2011-07-21 18:31:14 -0300 | [diff] [blame] | 132 | } |
Mauro Carvalho Chehab | 71d6763 | 2011-07-21 17:46:41 -0300 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | if (rem>0) |
| 136 | { |
| 137 | if ((ret = usb_control_msg(d->udev, |
| 138 | usb_sndctrlpipe(d->udev,0), |
| 139 | req, |
| 140 | USB_TYPE_VENDOR | USB_DIR_OUT, |
| 141 | value,index+cyc*64,b+cyc*64,rem, |
| 142 | 5000)) != rem) { |
| 143 | warn("usb out operation failed. (%d)",ret); |
| 144 | return -EIO; |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | else |
Mauro Carvalho Chehab | 6da3470 | 2011-07-21 18:31:14 -0300 | [diff] [blame] | 149 | #endif |
Mauro Carvalho Chehab | 71d6763 | 2011-07-21 17:46:41 -0300 | [diff] [blame] | 150 | { |
| 151 | if ((ret = usb_control_msg(d->udev, |
| 152 | usb_sndctrlpipe(d->udev,0), |
| 153 | req, |
| 154 | USB_TYPE_VENDOR | USB_DIR_OUT, |
| 155 | value,index,b,blen, |
| 156 | 5000)) != blen) { |
| 157 | warn("usb out operation failed. (%d)",ret); |
| 158 | return -EIO; |
| 159 | } |
| 160 | } |
Mauro Carvalho Chehab | 6da3470 | 2011-07-21 18:31:14 -0300 | [diff] [blame] | 161 | |
Mauro Carvalho Chehab | 71d6763 | 2011-07-21 17:46:41 -0300 | [diff] [blame] | 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | static int az6007_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff) |
| 166 | { |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | /* keys for the enclosed remote control */ |
Mauro Carvalho Chehab | 6da3470 | 2011-07-21 18:31:14 -0300 | [diff] [blame] | 171 | struct rc_map_table rc_map_az6007_table[] = { |
| 172 | { 0x0001, KEY_1 }, |
| 173 | { 0x0002, KEY_2 }, |
Mauro Carvalho Chehab | 71d6763 | 2011-07-21 17:46:41 -0300 | [diff] [blame] | 174 | }; |
| 175 | |
| 176 | /* remote control stuff (does not work with my box) */ |
| 177 | static int az6007_rc_query(struct dvb_usb_device *d, u32 *event, int *state) |
| 178 | { |
| 179 | return 0; |
| 180 | #if 0 |
| 181 | u8 key[10]; |
| 182 | int i; |
| 183 | |
| 184 | /* remove the following return to enabled remote querying */ |
Mauro Carvalho Chehab | 6da3470 | 2011-07-21 18:31:14 -0300 | [diff] [blame] | 185 | |
Mauro Carvalho Chehab | 71d6763 | 2011-07-21 17:46:41 -0300 | [diff] [blame] | 186 | |
| 187 | az6007_usb_in_op(d,READ_REMOTE_REQ,0,0,key,10); |
| 188 | |
| 189 | deb_rc("remote query key: %x %d\n",key[1],key[1]); |
| 190 | |
| 191 | if (key[1] == 0x44) { |
| 192 | *state = REMOTE_NO_KEY_PRESSED; |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | for (i = 0; i < ARRAY_SIZE(az6007_rc_keys); i++) |
| 197 | if (az6007_rc_keys[i].custom == key[1]) { |
| 198 | *state = REMOTE_KEY_PRESSED; |
| 199 | *event = az6007_rc_keys[i].event; |
| 200 | break; |
| 201 | } |
| 202 | return 0; |
| 203 | #endif |
| 204 | } |
| 205 | |
| 206 | /* |
| 207 | int az6007_power_ctrl(struct dvb_usb_device *d, int onoff) |
| 208 | { |
| 209 | u8 v = onoff; |
| 210 | return az6007_usb_out_op(d,0xBC,v,3,NULL,1); |
| 211 | } |
| 212 | */ |
| 213 | |
| 214 | static int az6007_read_mac_addr(struct dvb_usb_device *d,u8 mac[6]) |
| 215 | { |
| 216 | az6007_usb_in_op(d, 0xb7, 6, 0, &mac[0], 6); |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | static int az6007_frontend_poweron(struct dvb_usb_adapter *adap) |
| 221 | { |
| 222 | int ret; |
| 223 | u8 req; |
| 224 | u16 value; |
| 225 | u16 index; |
| 226 | int blen; |
| 227 | |
Mauro Carvalho Chehab | caa1a70 | 2011-07-22 10:31:25 -0300 | [diff] [blame] | 228 | info("az6007_frontend_poweron adap=%p adap->dev=%p", adap, adap->dev); |
Mauro Carvalho Chehab | 71d6763 | 2011-07-21 17:46:41 -0300 | [diff] [blame] | 229 | |
| 230 | req = 0xBC; |
| 231 | value = 1;//power on |
| 232 | index = 3; |
| 233 | blen =0; |
| 234 | |
| 235 | if((ret = az6007_usb_out_op(adap->dev,req,value,index,NULL,blen)) != 0) |
| 236 | { |
| 237 | err("az6007_frontend_poweron failed!!!"); |
| 238 | return -EIO; |
| 239 | } |
| 240 | |
| 241 | msleep_interruptible(200); |
| 242 | |
| 243 | req = 0xBC; |
| 244 | value = 0;//power on |
| 245 | index = 3; |
| 246 | blen =0; |
| 247 | |
| 248 | if((ret = az6007_usb_out_op(adap->dev,req,value,index,NULL,blen)) != 0) |
| 249 | { |
| 250 | err("az6007_frontend_poweron failed!!!"); |
| 251 | return -EIO; |
| 252 | } |
| 253 | |
| 254 | msleep_interruptible(200); |
| 255 | |
| 256 | req = 0xBC; |
| 257 | value = 1;//power on |
| 258 | index = 3; |
| 259 | blen =0; |
| 260 | |
| 261 | if((ret = az6007_usb_out_op(adap->dev,req,value,index,NULL,blen)) != 0) |
| 262 | { |
| 263 | err("az6007_frontend_poweron failed!!!"); |
| 264 | return -EIO; |
| 265 | } |
Mauro Carvalho Chehab | caa1a70 | 2011-07-22 10:31:25 -0300 | [diff] [blame] | 266 | info("az6007_frontend_poweron: OK"); |
| 267 | |
Mauro Carvalho Chehab | 71d6763 | 2011-07-21 17:46:41 -0300 | [diff] [blame] | 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | static int az6007_frontend_reset(struct dvb_usb_adapter *adap) |
| 272 | { |
| 273 | int ret; |
| 274 | u8 req; |
| 275 | u16 value; |
| 276 | u16 index; |
| 277 | int blen; |
| 278 | |
Mauro Carvalho Chehab | caa1a70 | 2011-07-22 10:31:25 -0300 | [diff] [blame] | 279 | info("az6007_frontend_reset adap=%p adap->dev=%p", adap, adap->dev); |
Mauro Carvalho Chehab | 6da3470 | 2011-07-21 18:31:14 -0300 | [diff] [blame] | 280 | |
Mauro Carvalho Chehab | 71d6763 | 2011-07-21 17:46:41 -0300 | [diff] [blame] | 281 | //reset demodulator |
| 282 | req = 0xC0; |
| 283 | value = 1;//high |
| 284 | index = 3; |
| 285 | blen =0; |
| 286 | if((ret = az6007_usb_out_op(adap->dev,req,value,index,NULL,blen)) != 0) |
| 287 | { |
| 288 | err("az6007_frontend_reset failed 1 !!!"); |
| 289 | return -EIO; |
| 290 | } |
Mauro Carvalho Chehab | 6da3470 | 2011-07-21 18:31:14 -0300 | [diff] [blame] | 291 | |
Mauro Carvalho Chehab | 71d6763 | 2011-07-21 17:46:41 -0300 | [diff] [blame] | 292 | req = 0xC0; |
| 293 | value = 0;//low |
| 294 | index = 3; |
| 295 | blen =0; |
| 296 | msleep_interruptible(200); |
| 297 | if((ret = az6007_usb_out_op(adap->dev,req,value,index,NULL,blen)) != 0) |
| 298 | { |
| 299 | err("az6007_frontend_reset failed 2 !!!"); |
| 300 | return -EIO; |
| 301 | } |
| 302 | msleep_interruptible(200); |
| 303 | req = 0xC0; |
| 304 | value = 1;//high |
| 305 | index = 3; |
| 306 | blen =0; |
| 307 | |
| 308 | if((ret = az6007_usb_out_op(adap->dev,req,value,index,NULL,blen)) != 0) |
| 309 | { |
| 310 | err("az6007_frontend_reset failed 3 !!!"); |
| 311 | return -EIO; |
| 312 | } |
Mauro Carvalho Chehab | 6da3470 | 2011-07-21 18:31:14 -0300 | [diff] [blame] | 313 | |
Mauro Carvalho Chehab | 71d6763 | 2011-07-21 17:46:41 -0300 | [diff] [blame] | 314 | msleep_interruptible(200); |
Mauro Carvalho Chehab | 6da3470 | 2011-07-21 18:31:14 -0300 | [diff] [blame] | 315 | |
Mauro Carvalho Chehab | caa1a70 | 2011-07-22 10:31:25 -0300 | [diff] [blame] | 316 | info("reset az6007 frontend"); |
Mauro Carvalho Chehab | 6da3470 | 2011-07-21 18:31:14 -0300 | [diff] [blame] | 317 | |
Mauro Carvalho Chehab | 71d6763 | 2011-07-21 17:46:41 -0300 | [diff] [blame] | 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | static int az6007_led_on_off(struct usb_interface *intf, int onoff) |
| 322 | { |
| 323 | int ret = -1; |
| 324 | u8 req; |
| 325 | u16 value; |
| 326 | u16 index; |
| 327 | int blen; |
| 328 | //TS through |
| 329 | req = 0xBC; |
| 330 | value = onoff; |
| 331 | index = 0; |
| 332 | blen =0; |
| 333 | |
| 334 | ret = usb_control_msg(interface_to_usbdev(intf), |
| 335 | usb_rcvctrlpipe(interface_to_usbdev(intf),0), |
| 336 | req, |
| 337 | USB_TYPE_VENDOR | USB_DIR_OUT, |
| 338 | value,index,NULL,blen, |
| 339 | 2000); |
| 340 | |
| 341 | if (ret < 0) { |
| 342 | warn("usb in operation failed. (%d)", ret); |
| 343 | ret = -EIO; |
| 344 | } else |
| 345 | ret = 0; |
| 346 | |
| 347 | |
| 348 | deb_xfer("in: req. %02x, val: %04x, ind: %04x, buffer: ",req,value,index); |
| 349 | |
| 350 | return ret; |
| 351 | } |
| 352 | |
| 353 | static int az6007_frontend_tsbypass(struct dvb_usb_adapter *adap,int onoff) |
| 354 | { |
| 355 | int ret; |
| 356 | u8 req; |
| 357 | u16 value; |
| 358 | u16 index; |
| 359 | int blen; |
| 360 | //TS through |
| 361 | req = 0xC7; |
| 362 | value = onoff; |
| 363 | index = 0; |
| 364 | blen =0; |
| 365 | |
| 366 | if((ret = az6007_usb_out_op(adap->dev,req,value,index,NULL,blen)) != 0) |
| 367 | return -EIO; |
| 368 | return 0; |
| 369 | } |
| 370 | |
| 371 | static int az6007_frontend_attach(struct dvb_usb_adapter *adap) |
| 372 | { |
Mauro Carvalho Chehab | 6da3470 | 2011-07-21 18:31:14 -0300 | [diff] [blame] | 373 | struct az6007_device_state *st = adap->priv; |
| 374 | |
| 375 | int result; |
| 376 | |
Mauro Carvalho Chehab | caa1a70 | 2011-07-22 10:31:25 -0300 | [diff] [blame] | 377 | BUG_ON(!st); |
| 378 | |
Mauro Carvalho Chehab | 71d6763 | 2011-07-21 17:46:41 -0300 | [diff] [blame] | 379 | az6007_frontend_poweron(adap); |
| 380 | az6007_frontend_reset(adap); |
| 381 | |
Mauro Carvalho Chehab | caa1a70 | 2011-07-22 10:31:25 -0300 | [diff] [blame] | 382 | info("az6007_frontend_attach: drxk"); |
Mauro Carvalho Chehab | 71d6763 | 2011-07-21 17:46:41 -0300 | [diff] [blame] | 383 | |
Mauro Carvalho Chehab | 6da3470 | 2011-07-21 18:31:14 -0300 | [diff] [blame] | 384 | adap->fe = dvb_attach(drxk_attach, &terratec_h7_drxk, |
| 385 | &adap->dev->i2c_adap, &adap->fe2); |
| 386 | if (!adap->fe) { |
| 387 | result = -EINVAL; |
| 388 | goto out_free; |
Mauro Carvalho Chehab | 71d6763 | 2011-07-21 17:46:41 -0300 | [diff] [blame] | 389 | } |
Mauro Carvalho Chehab | 6da3470 | 2011-07-21 18:31:14 -0300 | [diff] [blame] | 390 | |
Mauro Carvalho Chehab | caa1a70 | 2011-07-22 10:31:25 -0300 | [diff] [blame] | 391 | info("Setting hacks"); |
| 392 | |
Mauro Carvalho Chehab | 6da3470 | 2011-07-21 18:31:14 -0300 | [diff] [blame] | 393 | /* FIXME: do we need a pll semaphore? */ |
| 394 | adap->fe->sec_priv = adap; |
| 395 | sema_init(&st->pll_mutex, 1); |
| 396 | st->gate_ctrl = adap->fe->ops.i2c_gate_ctrl; |
| 397 | adap->fe->ops.i2c_gate_ctrl = drxk_gate_ctrl; |
| 398 | adap->fe2->id = 1; |
| 399 | |
Mauro Carvalho Chehab | caa1a70 | 2011-07-22 10:31:25 -0300 | [diff] [blame] | 400 | info("az6007_frontend_attach: mt2063"); |
Mauro Carvalho Chehab | 6da3470 | 2011-07-21 18:31:14 -0300 | [diff] [blame] | 401 | /* Attach mt2063 to DVB-C frontend */ |
| 402 | if (adap->fe->ops.i2c_gate_ctrl) |
| 403 | adap->fe->ops.i2c_gate_ctrl(adap->fe, 1); |
| 404 | if (!dvb_attach(mt2063_attach, adap->fe, &az6007_mt2063_config, |
| 405 | &adap->dev->i2c_adap)) { |
| 406 | result = -EINVAL; |
| 407 | |
| 408 | goto out_free; |
Mauro Carvalho Chehab | 71d6763 | 2011-07-21 17:46:41 -0300 | [diff] [blame] | 409 | } |
Mauro Carvalho Chehab | 6da3470 | 2011-07-21 18:31:14 -0300 | [diff] [blame] | 410 | if (adap->fe->ops.i2c_gate_ctrl) |
| 411 | adap->fe->ops.i2c_gate_ctrl(adap->fe, 0); |
| 412 | |
| 413 | /* Hack - needed due to drxk */ |
| 414 | adap->fe2->tuner_priv = adap->fe->tuner_priv; |
| 415 | memcpy(&adap->fe2->ops.tuner_ops, |
| 416 | &adap->fe->ops.tuner_ops, |
| 417 | sizeof(adap->fe->ops.tuner_ops)); |
Mauro Carvalho Chehab | 71d6763 | 2011-07-21 17:46:41 -0300 | [diff] [blame] | 418 | return 0; |
Mauro Carvalho Chehab | 6da3470 | 2011-07-21 18:31:14 -0300 | [diff] [blame] | 419 | |
| 420 | out_free: |
| 421 | if (adap->fe) |
| 422 | dvb_frontend_detach(adap->fe); |
| 423 | adap->fe = NULL; |
| 424 | adap->fe2 = NULL; |
| 425 | |
| 426 | return result; |
Mauro Carvalho Chehab | 71d6763 | 2011-07-21 17:46:41 -0300 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | static struct dvb_usb_device_properties az6007_properties; |
| 430 | |
| 431 | static void |
| 432 | az6007_usb_disconnect(struct usb_interface *intf) |
| 433 | { |
| 434 | dvb_usb_device_exit (intf); |
| 435 | } |
| 436 | |
| 437 | /* I2C */ |
Mauro Carvalho Chehab | caa1a70 | 2011-07-22 10:31:25 -0300 | [diff] [blame] | 438 | static int az6007_i2c_xfer(struct i2c_adapter *adap,struct i2c_msg msgs[],int num) |
Mauro Carvalho Chehab | 71d6763 | 2011-07-21 17:46:41 -0300 | [diff] [blame] | 439 | { |
| 440 | struct dvb_usb_device *d = i2c_get_adapdata(adap); |
Mauro Carvalho Chehab | caa1a70 | 2011-07-22 10:31:25 -0300 | [diff] [blame] | 441 | int i, j, len; |
| 442 | int ret = 0; |
Mauro Carvalho Chehab | 71d6763 | 2011-07-21 17:46:41 -0300 | [diff] [blame] | 443 | u16 index; |
| 444 | u16 value; |
| 445 | int length; |
Mauro Carvalho Chehab | caa1a70 | 2011-07-22 10:31:25 -0300 | [diff] [blame] | 446 | u8 req, addr; |
Mauro Carvalho Chehab | 71d6763 | 2011-07-21 17:46:41 -0300 | [diff] [blame] | 447 | u8 data[512]; |
Mauro Carvalho Chehab | 6da3470 | 2011-07-21 18:31:14 -0300 | [diff] [blame] | 448 | |
Mauro Carvalho Chehab | 71d6763 | 2011-07-21 17:46:41 -0300 | [diff] [blame] | 449 | if (mutex_lock_interruptible(&d->i2c_mutex) < 0) |
| 450 | return -EAGAIN; |
Mauro Carvalho Chehab | 71d6763 | 2011-07-21 17:46:41 -0300 | [diff] [blame] | 451 | |
Mauro Carvalho Chehab | caa1a70 | 2011-07-22 10:31:25 -0300 | [diff] [blame] | 452 | for (i = 0; i < num; i++) { |
| 453 | addr = msgs[i].addr << 1; |
Mauro Carvalho Chehab | caa1a70 | 2011-07-22 10:31:25 -0300 | [diff] [blame] | 454 | if (((i + 1) < num) |
| 455 | && (msgs[i].len == 1) |
| 456 | && (!msgs[i].flags & I2C_M_RD) |
| 457 | && (msgs[i + 1].flags & I2C_M_RD) |
| 458 | && (msgs[i].addr == msgs[i + 1].addr)) { |
| 459 | /* |
| 460 | * A write + read xfer for the same address, where |
| 461 | * the first xfer has just 1 byte length. |
| 462 | * Need to join both into one operation |
| 463 | */ |
Mauro Carvalho Chehab | d20a7f7 | 2011-07-23 09:51:12 -0300 | [diff] [blame^] | 464 | if (dvb_usb_az6007_debug & 2) |
| 465 | printk(KERN_DEBUG |
| 466 | "az6007 I2C xfer write+read addr=0x%x len=%d/%d: ", |
| 467 | addr, msgs[i].len, msgs[i + 1].len); |
Mauro Carvalho Chehab | caa1a70 | 2011-07-22 10:31:25 -0300 | [diff] [blame] | 468 | req = 0xb9; |
Mauro Carvalho Chehab | d20a7f7 | 2011-07-23 09:51:12 -0300 | [diff] [blame^] | 469 | index = msgs[i].buf[0]; |
| 470 | value = addr | (1 << 8); |
Mauro Carvalho Chehab | caa1a70 | 2011-07-22 10:31:25 -0300 | [diff] [blame] | 471 | length = 6 + msgs[i + 1].len; |
| 472 | len = msgs[i + 1].len; |
Mauro Carvalho Chehab | 71d6763 | 2011-07-21 17:46:41 -0300 | [diff] [blame] | 473 | ret = az6007_usb_in_op(d,req,value,index,data,length); |
Mauro Carvalho Chehab | caa1a70 | 2011-07-22 10:31:25 -0300 | [diff] [blame] | 474 | if (ret >= len) { |
| 475 | for (j = 0; j < len; j++) { |
| 476 | msgs[i + 1].buf[j] = data[j + 5]; |
Mauro Carvalho Chehab | d20a7f7 | 2011-07-23 09:51:12 -0300 | [diff] [blame^] | 477 | if (dvb_usb_az6007_debug & 2) |
| 478 | printk(KERN_CONT |
| 479 | "0x%02x ", |
| 480 | msgs[i + 1].buf[j]); |
Mauro Carvalho Chehab | caa1a70 | 2011-07-22 10:31:25 -0300 | [diff] [blame] | 481 | } |
| 482 | } else |
| 483 | ret = -EIO; |
| 484 | i++; |
| 485 | } else if (!(msgs[i].flags & I2C_M_RD)) { |
| 486 | /* write bytes */ |
Mauro Carvalho Chehab | d20a7f7 | 2011-07-23 09:51:12 -0300 | [diff] [blame^] | 487 | if (dvb_usb_az6007_debug & 2) |
| 488 | printk(KERN_DEBUG |
| 489 | "az6007 I2C xfer write addr=0x%x len=%d: ", |
| 490 | addr, msgs[i].len); |
Mauro Carvalho Chehab | caa1a70 | 2011-07-22 10:31:25 -0300 | [diff] [blame] | 491 | req = 0xbd; |
| 492 | index = msgs[i].buf[0]; |
| 493 | value = addr | (1 << 8); |
| 494 | length = msgs[i].len - 1; |
| 495 | len = msgs[i].len - 1; |
Mauro Carvalho Chehab | d20a7f7 | 2011-07-23 09:51:12 -0300 | [diff] [blame^] | 496 | if (dvb_usb_az6007_debug & 2) |
| 497 | printk(KERN_CONT |
| 498 | "(0x%02x) ", msgs[i].buf[0]); |
Mauro Carvalho Chehab | caa1a70 | 2011-07-22 10:31:25 -0300 | [diff] [blame] | 499 | for (j = 0; j < len; j++) |
Mauro Carvalho Chehab | 71d6763 | 2011-07-21 17:46:41 -0300 | [diff] [blame] | 500 | { |
Mauro Carvalho Chehab | caa1a70 | 2011-07-22 10:31:25 -0300 | [diff] [blame] | 501 | data[j] = msgs[i].buf[j + 1]; |
Mauro Carvalho Chehab | d20a7f7 | 2011-07-23 09:51:12 -0300 | [diff] [blame^] | 502 | if (dvb_usb_az6007_debug & 2) |
| 503 | printk(KERN_CONT |
| 504 | "0x%02x ", data[j]); |
Mauro Carvalho Chehab | 71d6763 | 2011-07-21 17:46:41 -0300 | [diff] [blame] | 505 | } |
Mauro Carvalho Chehab | caa1a70 | 2011-07-22 10:31:25 -0300 | [diff] [blame] | 506 | ret = az6007_usb_out_op(d,req,value,index,data,length); |
| 507 | } else { |
| 508 | /* read bytes */ |
Mauro Carvalho Chehab | d20a7f7 | 2011-07-23 09:51:12 -0300 | [diff] [blame^] | 509 | if (dvb_usb_az6007_debug & 2) |
| 510 | printk(KERN_DEBUG |
| 511 | "az6007 I2C xfer read addr=0x%x len=%d: ", |
| 512 | addr, msgs[i].len); |
Mauro Carvalho Chehab | caa1a70 | 2011-07-22 10:31:25 -0300 | [diff] [blame] | 513 | req = 0xb9; |
| 514 | index = msgs[i].buf[0]; |
| 515 | value = addr; |
| 516 | length = msgs[i].len + 6; |
| 517 | len = msgs[i].len; |
| 518 | ret = az6007_usb_in_op(d,req,value,index,data,length); |
| 519 | for (j = 0; j < len; j++) |
| 520 | { |
| 521 | msgs[i].buf[j] = data[j + 5]; |
Mauro Carvalho Chehab | d20a7f7 | 2011-07-23 09:51:12 -0300 | [diff] [blame^] | 522 | if (dvb_usb_az6007_debug & 2) |
| 523 | printk(KERN_CONT |
| 524 | "0x%02x ", data[j + 5]); |
Mauro Carvalho Chehab | caa1a70 | 2011-07-22 10:31:25 -0300 | [diff] [blame] | 525 | } |
Mauro Carvalho Chehab | 71d6763 | 2011-07-21 17:46:41 -0300 | [diff] [blame] | 526 | } |
Mauro Carvalho Chehab | d20a7f7 | 2011-07-23 09:51:12 -0300 | [diff] [blame^] | 527 | if (dvb_usb_az6007_debug & 2) |
| 528 | printk(KERN_CONT "\n"); |
Mauro Carvalho Chehab | caa1a70 | 2011-07-22 10:31:25 -0300 | [diff] [blame] | 529 | if (ret < 0) |
| 530 | goto err; |
Mauro Carvalho Chehab | 71d6763 | 2011-07-21 17:46:41 -0300 | [diff] [blame] | 531 | } |
Mauro Carvalho Chehab | caa1a70 | 2011-07-22 10:31:25 -0300 | [diff] [blame] | 532 | err: |
Mauro Carvalho Chehab | 71d6763 | 2011-07-21 17:46:41 -0300 | [diff] [blame] | 533 | mutex_unlock(&d->i2c_mutex); |
Mauro Carvalho Chehab | caa1a70 | 2011-07-22 10:31:25 -0300 | [diff] [blame] | 534 | |
| 535 | if (ret < 0) { |
| 536 | info("%s ERROR: %i\n", __func__, ret); |
| 537 | return ret; |
| 538 | } |
| 539 | return num; |
Mauro Carvalho Chehab | 71d6763 | 2011-07-21 17:46:41 -0300 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | |
| 543 | static u32 az6007_i2c_func(struct i2c_adapter *adapter) |
| 544 | { |
| 545 | return I2C_FUNC_I2C; |
| 546 | } |
| 547 | |
| 548 | static struct i2c_algorithm az6007_i2c_algo = { |
| 549 | .master_xfer = az6007_i2c_xfer, |
| 550 | .functionality = az6007_i2c_func, |
| 551 | #ifdef NEED_ALGO_CONTROL |
| 552 | .algo_control = dummy_algo_control, |
| 553 | #endif |
| 554 | }; |
| 555 | |
| 556 | int az6007_identify_state(struct usb_device *udev, struct dvb_usb_device_properties *props, |
| 557 | struct dvb_usb_device_description **desc, int *cold) |
| 558 | { |
| 559 | u8 b[16]; |
| 560 | s16 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev,0), |
| 561 | 0xb7, USB_TYPE_VENDOR | USB_DIR_IN, 6, 0, b, 6, USB_CTRL_GET_TIMEOUT); |
| 562 | |
Mauro Carvalho Chehab | caa1a70 | 2011-07-22 10:31:25 -0300 | [diff] [blame] | 563 | info("FW GET_VERSION length: %d",ret); |
Mauro Carvalho Chehab | 71d6763 | 2011-07-21 17:46:41 -0300 | [diff] [blame] | 564 | |
| 565 | *cold = ret <= 0; |
| 566 | |
Mauro Carvalho Chehab | caa1a70 | 2011-07-22 10:31:25 -0300 | [diff] [blame] | 567 | info("cold: %d", *cold); |
Mauro Carvalho Chehab | 71d6763 | 2011-07-21 17:46:41 -0300 | [diff] [blame] | 568 | return 0; |
| 569 | } |
| 570 | |
| 571 | static int az6007_usb_probe(struct usb_interface *intf, |
| 572 | const struct usb_device_id *id) |
| 573 | { |
Mauro Carvalho Chehab | 6da3470 | 2011-07-21 18:31:14 -0300 | [diff] [blame] | 574 | az6007_led_on_off(intf, 0); |
| 575 | |
Mauro Carvalho Chehab | 71d6763 | 2011-07-21 17:46:41 -0300 | [diff] [blame] | 576 | return dvb_usb_device_init(intf, &az6007_properties, |
Mauro Carvalho Chehab | 6da3470 | 2011-07-21 18:31:14 -0300 | [diff] [blame] | 577 | THIS_MODULE, NULL, adapter_nr); |
Mauro Carvalho Chehab | 71d6763 | 2011-07-21 17:46:41 -0300 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | static struct usb_device_id az6007_usb_table [] = { |
| 581 | { USB_DEVICE(USB_VID_AZUREWAVE, USB_PID_AZUREWAVE_6007) }, |
| 582 | { USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_H7) }, |
| 583 | { 0 }, |
| 584 | }; |
| 585 | |
| 586 | MODULE_DEVICE_TABLE(usb, az6007_usb_table); |
| 587 | |
| 588 | static struct dvb_usb_device_properties az6007_properties = { |
| 589 | .caps = DVB_USB_IS_AN_I2C_ADAPTER, |
| 590 | .usb_ctrl = CYPRESS_FX2, |
| 591 | //.download_firmware = az6007_download_firmware, |
| 592 | .firmware = "dvb-usb-az6007-03.fw", |
| 593 | .no_reconnect = 1, |
| 594 | |
Mauro Carvalho Chehab | 71d6763 | 2011-07-21 17:46:41 -0300 | [diff] [blame] | 595 | .identify_state = az6007_identify_state, |
| 596 | .num_adapters = 1, |
| 597 | .adapter = { |
| 598 | { |
| 599 | //.caps = DVB_USB_ADAP_RECEIVES_204_BYTE_TS, |
| 600 | |
| 601 | .streaming_ctrl = az6007_streaming_ctrl, |
| 602 | .frontend_attach = az6007_frontend_attach, |
| 603 | |
| 604 | /* parameter for the MPEG2-data transfer */ |
| 605 | .stream = { |
| 606 | .type = USB_BULK, |
| 607 | .count = 10, |
| 608 | .endpoint = 0x02, |
| 609 | .u = { |
| 610 | .bulk = { |
| 611 | .buffersize = 4096, |
| 612 | } |
| 613 | } |
| 614 | }, |
Mauro Carvalho Chehab | caa1a70 | 2011-07-22 10:31:25 -0300 | [diff] [blame] | 615 | .size_of_priv = sizeof(struct az6007_device_state), |
Mauro Carvalho Chehab | 71d6763 | 2011-07-21 17:46:41 -0300 | [diff] [blame] | 616 | } |
| 617 | }, |
| 618 | //.power_ctrl = az6007_power_ctrl, |
| 619 | .read_mac_address = az6007_read_mac_addr, |
| 620 | |
Mauro Carvalho Chehab | 6da3470 | 2011-07-21 18:31:14 -0300 | [diff] [blame] | 621 | .rc.legacy = { |
| 622 | .rc_map_table = rc_map_az6007_table, |
| 623 | .rc_map_size = ARRAY_SIZE(rc_map_az6007_table), |
| 624 | .rc_interval = 400, |
| 625 | .rc_query = az6007_rc_query, |
| 626 | }, |
Mauro Carvalho Chehab | 71d6763 | 2011-07-21 17:46:41 -0300 | [diff] [blame] | 627 | .i2c_algo = &az6007_i2c_algo, |
| 628 | |
| 629 | .num_device_descs = 2, |
| 630 | .devices = { |
| 631 | { .name = "AzureWave DTV StarBox DVB-T/C USB2.0 (az6007)", |
| 632 | .cold_ids = { &az6007_usb_table[0], NULL }, |
| 633 | .warm_ids = { NULL }, |
| 634 | }, |
| 635 | { .name = "TerraTec DTV StarBox DVB-T/C USB2.0 (az6007)", |
| 636 | .cold_ids = { &az6007_usb_table[1], NULL }, |
| 637 | .warm_ids = { NULL }, |
| 638 | }, |
Mauro Carvalho Chehab | 6da3470 | 2011-07-21 18:31:14 -0300 | [diff] [blame] | 639 | { NULL }, |
Mauro Carvalho Chehab | 71d6763 | 2011-07-21 17:46:41 -0300 | [diff] [blame] | 640 | } |
| 641 | }; |
Mauro Carvalho Chehab | 6da3470 | 2011-07-21 18:31:14 -0300 | [diff] [blame] | 642 | |
Mauro Carvalho Chehab | 71d6763 | 2011-07-21 17:46:41 -0300 | [diff] [blame] | 643 | /* usb specific object needed to register this driver with the usb subsystem */ |
| 644 | static struct usb_driver az6007_usb_driver = { |
| 645 | .name = "dvb_usb_az6007", |
| 646 | .probe = az6007_usb_probe, |
| 647 | .disconnect = dvb_usb_device_exit, |
| 648 | //.disconnect = az6007_usb_disconnect, |
| 649 | .id_table = az6007_usb_table, |
| 650 | }; |
| 651 | |
| 652 | /* module stuff */ |
| 653 | static int __init az6007_usb_module_init(void) |
| 654 | { |
| 655 | int result; |
Mauro Carvalho Chehab | caa1a70 | 2011-07-22 10:31:25 -0300 | [diff] [blame] | 656 | info("az6007 usb module init"); |
Mauro Carvalho Chehab | 71d6763 | 2011-07-21 17:46:41 -0300 | [diff] [blame] | 657 | if ((result = usb_register(&az6007_usb_driver))) { |
| 658 | err("usb_register failed. (%d)",result); |
| 659 | return result; |
| 660 | } |
| 661 | |
| 662 | return 0; |
| 663 | } |
| 664 | |
| 665 | static void __exit az6007_usb_module_exit(void) |
| 666 | { |
| 667 | /* deregister this driver from the USB subsystem */ |
Mauro Carvalho Chehab | caa1a70 | 2011-07-22 10:31:25 -0300 | [diff] [blame] | 668 | info("az6007 usb module exit"); |
Mauro Carvalho Chehab | 71d6763 | 2011-07-21 17:46:41 -0300 | [diff] [blame] | 669 | usb_deregister(&az6007_usb_driver); |
| 670 | } |
| 671 | |
| 672 | module_init(az6007_usb_module_init); |
| 673 | module_exit(az6007_usb_module_exit); |
| 674 | |
| 675 | MODULE_AUTHOR("Henry Wang <Henry.wang@AzureWave.com>"); |
| 676 | MODULE_DESCRIPTION("Driver for AzureWave 6007 DVB-C/T USB2.0 and clones"); |
| 677 | MODULE_VERSION("1.0"); |
| 678 | MODULE_LICENSE("GPL"); |