az6007.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. /*
  2. * Driver for AzureWave 6007 DVB-C/T USB2.0 and clones
  3. *
  4. * Copyright (c) Henry Wang <Henry.wang@AzureWave.com>
  5. *
  6. * This driver was made publicly available by Terratec, at:
  7. * http://linux.terratec.de/files/TERRATEC_H7/20110323_TERRATEC_H7_Linux.tar.gz
  8. * The original driver's license is GPL, as declared with MODULE_LICENSE()
  9. *
  10. * Driver modifiyed by Mauro Carvalho Chehab <mchehab@redhat.com> in order
  11. * to work with upstream drxk driver, and to fix some bugs.
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation under version 2 of the License.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. */
  22. #include "drxk.h"
  23. #include "mt2063.h"
  24. #include "dvb_ca_en50221.h"
  25. #define DVB_USB_LOG_PREFIX "az6007"
  26. #include "dvb-usb.h"
  27. /* debug */
  28. int dvb_usb_az6007_debug;
  29. module_param_named(debug, dvb_usb_az6007_debug, int, 0644);
  30. MODULE_PARM_DESC(debug, "set debugging level (1=info,xfer=2,rc=4 (or-able))."
  31. DVB_USB_DEBUG_STATUS);
  32. #define deb_info(args...) dprintk(dvb_usb_az6007_debug, 0x01, args)
  33. #define deb_xfer(args...) dprintk(dvb_usb_az6007_debug, 0x02, args)
  34. #define deb_rc(args...) dprintk(dvb_usb_az6007_debug, 0x04, args)
  35. #define deb_fe(args...) dprintk(dvb_usb_az6007_debug, 0x08, args)
  36. DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
  37. /* Known requests (Cypress FX2 firmware + az6007 "private" ones*/
  38. #define FX2_OED 0xb5
  39. #define AZ6007_READ_DATA 0xb7
  40. #define AZ6007_I2C_RD 0xb9
  41. #define AZ6007_POWER 0xbc
  42. #define AZ6007_I2C_WR 0xbd
  43. #define FX2_SCON1 0xc0
  44. #define AZ6007_TS_THROUGH 0xc7
  45. #define AZ6007_READ_IR 0xc5
  46. struct az6007_device_state {
  47. struct dvb_ca_en50221 ca;
  48. struct mutex ca_mutex;
  49. u8 power_state;
  50. /* Due to DRX-K - probably need changes */
  51. int (*gate_ctrl) (struct dvb_frontend *, int);
  52. struct semaphore pll_mutex;
  53. bool dont_attach_fe1;
  54. };
  55. static struct drxk_config terratec_h7_drxk = {
  56. .adr = 0x29,
  57. .single_master = 1,
  58. .no_i2c_bridge = 0,
  59. .max_size = 64,
  60. .microcode_name = "dvb-usb-terratec-h7-drxk.fw",
  61. .parallel_ts = 1,
  62. };
  63. static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable)
  64. {
  65. struct dvb_usb_adapter *adap = fe->sec_priv;
  66. struct az6007_device_state *st;
  67. int status = 0;
  68. deb_info("%s: %s\n", __func__, enable ? "enable" : "disable");
  69. if (!adap)
  70. return -EINVAL;
  71. st = adap->priv;
  72. if (!st)
  73. return -EINVAL;
  74. if (enable) {
  75. #if 0
  76. down(&st->pll_mutex);
  77. #endif
  78. status = st->gate_ctrl(fe, 1);
  79. } else {
  80. #if 0
  81. status = st->gate_ctrl(fe, 0);
  82. #endif
  83. up(&st->pll_mutex);
  84. }
  85. return status;
  86. }
  87. static struct mt2063_config az6007_mt2063_config = {
  88. .tuner_address = 0x60,
  89. .refclock = 36125000,
  90. };
  91. /* check for mutex FIXME */
  92. static int az6007_read(struct usb_device *udev, u8 req, u16 value,
  93. u16 index, u8 *b, int blen)
  94. {
  95. int ret = -1;
  96. ret = usb_control_msg(udev,
  97. usb_rcvctrlpipe(udev, 0),
  98. req,
  99. USB_TYPE_VENDOR | USB_DIR_IN,
  100. value, index, b, blen, 5000);
  101. if (ret < 0) {
  102. warn("usb in operation failed. (%d)", ret);
  103. return -EIO;
  104. }
  105. deb_xfer("in: req. %02x, val: %04x, ind: %04x, buffer: ", req, value,
  106. index);
  107. debug_dump(b, blen, deb_xfer);
  108. return ret;
  109. }
  110. static int az6007_write(struct usb_device *udev, u8 req, u16 value,
  111. u16 index, u8 *b, int blen)
  112. {
  113. int ret;
  114. deb_xfer("out: req. %02x, val: %04x, ind: %04x, buffer: ", req, value,
  115. index);
  116. debug_dump(b, blen, deb_xfer);
  117. if (blen > 64) {
  118. err("az6007: tried to write %d bytes, but I2C max size is 64 bytes\n",
  119. blen);
  120. return -EOPNOTSUPP;
  121. }
  122. ret = usb_control_msg(udev,
  123. usb_sndctrlpipe(udev, 0),
  124. req,
  125. USB_TYPE_VENDOR | USB_DIR_OUT,
  126. value, index, b, blen, 5000);
  127. if (ret != blen) {
  128. err("usb out operation failed. (%d)", ret);
  129. return -EIO;
  130. }
  131. return 0;
  132. }
  133. static int az6007_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
  134. {
  135. return 0;
  136. }
  137. /* keys for the enclosed remote control */
  138. static struct rc_map_table rc_map_az6007_table[] = {
  139. {0x0001, KEY_1},
  140. {0x0002, KEY_2},
  141. };
  142. /* remote control stuff (does not work with my box) */
  143. static int az6007_rc_query(struct dvb_usb_device *d, u32 * event, int *state)
  144. {
  145. return 0;
  146. #if 0
  147. u8 key[10];
  148. int i;
  149. /* remove the following return to enabled remote querying */
  150. az6007_read(d->udev, AZ6007_READ_IR, 0, 0, key, 10);
  151. deb_rc("remote query key: %x %d\n", key[1], key[1]);
  152. if (key[1] == 0x44) {
  153. *state = REMOTE_NO_KEY_PRESSED;
  154. return 0;
  155. }
  156. for (i = 0; i < ARRAY_SIZE(az6007_rc_keys); i++)
  157. if (az6007_rc_keys[i].custom == key[1]) {
  158. *state = REMOTE_KEY_PRESSED;
  159. *event = az6007_rc_keys[i].event;
  160. break;
  161. }
  162. return 0;
  163. #endif
  164. }
  165. #if 0
  166. int az6007_power_ctrl(struct dvb_usb_device *d, int onoff)
  167. {
  168. u8 v = onoff;
  169. return az6007_write(d->udev, AZ6007_POWER, v , 3, NULL, 1);
  170. }
  171. #endif
  172. static int az6007_read_mac_addr(struct dvb_usb_device *d, u8 mac[6])
  173. {
  174. int ret;
  175. ret = az6007_read(d->udev, AZ6007_READ_DATA, 6, 0, mac, 6);
  176. if (ret > 0)
  177. deb_info("%s: mac is %02x:%02x:%02x:%02x:%02x:%02x\n",
  178. __func__, mac[0], mac[1], mac[2],
  179. mac[3], mac[4], mac[5]);
  180. return ret;
  181. }
  182. static int az6007_frontend_poweron(struct dvb_usb_adapter *adap)
  183. {
  184. int ret;
  185. struct usb_device *udev = adap->dev->udev;
  186. deb_info("%s: adap=%p adap->dev=%p\n", __func__, adap, adap->dev);
  187. ret = az6007_write(udev, AZ6007_POWER, 0, 2, NULL, 0);
  188. if (ret < 0)
  189. goto error;
  190. msleep(150);
  191. ret = az6007_write(udev, AZ6007_POWER, 1, 4, NULL, 0);
  192. if (ret < 0)
  193. goto error;
  194. msleep(100);
  195. ret = az6007_write(udev, AZ6007_POWER, 1, 3, NULL, 0);
  196. if (ret < 0)
  197. goto error;
  198. msleep(100);
  199. ret = az6007_write(udev, AZ6007_POWER, 1, 4, NULL, 0);
  200. if (ret < 0)
  201. goto error;
  202. msleep(100);
  203. ret = az6007_write(udev, FX2_SCON1, 0, 3, NULL, 0);
  204. if (ret < 0)
  205. goto error;
  206. msleep (10);
  207. ret = az6007_write(udev, FX2_SCON1, 1, 3, NULL, 0);
  208. if (ret < 0)
  209. goto error;
  210. msleep (10);
  211. ret = az6007_write(udev, AZ6007_POWER, 0, 0, NULL, 0);
  212. error:
  213. if (ret < 0)
  214. err("%s failed with error %d", __func__, ret);
  215. return ret;
  216. }
  217. static int az6007_led_on_off(struct usb_interface *intf, int onoff)
  218. {
  219. struct usb_device *udev = interface_to_usbdev(intf);
  220. int ret;
  221. /* TS through */
  222. ret = az6007_write(udev, AZ6007_POWER, onoff, 0, NULL, 0);
  223. if (ret < 0)
  224. err("%s failed with error %d", __func__, ret);
  225. return ret;
  226. }
  227. static int az6007_frontend_attach(struct dvb_usb_adapter *adap)
  228. {
  229. struct az6007_device_state *st = adap->priv;
  230. int result;
  231. BUG_ON(!st);
  232. az6007_frontend_poweron(adap);
  233. info("az6007: attaching demod drxk");
  234. adap->fe = dvb_attach(drxk_attach, &terratec_h7_drxk,
  235. &adap->dev->i2c_adap, &adap->fe2);
  236. if (!adap->fe) {
  237. result = -EINVAL;
  238. goto out_free;
  239. }
  240. deb_info("Setting hacks\n");
  241. /* FIXME: do we need a pll semaphore? */
  242. adap->fe->sec_priv = adap;
  243. sema_init(&st->pll_mutex, 1);
  244. st->gate_ctrl = adap->fe->ops.i2c_gate_ctrl;
  245. adap->fe->ops.i2c_gate_ctrl = drxk_gate_ctrl;
  246. adap->fe2->id = 1;
  247. info("az6007: attaching tuner mt2063");
  248. /* Attach mt2063 to DVB-C frontend */
  249. if (adap->fe->ops.i2c_gate_ctrl)
  250. adap->fe->ops.i2c_gate_ctrl(adap->fe, 1);
  251. if (!dvb_attach(mt2063_attach, adap->fe, &az6007_mt2063_config,
  252. &adap->dev->i2c_adap)) {
  253. result = -EINVAL;
  254. goto out_free;
  255. }
  256. if (adap->fe->ops.i2c_gate_ctrl)
  257. adap->fe->ops.i2c_gate_ctrl(adap->fe, 0);
  258. /* Hack - needed due to drxk */
  259. adap->fe2->tuner_priv = adap->fe->tuner_priv;
  260. memcpy(&adap->fe2->ops.tuner_ops,
  261. &adap->fe->ops.tuner_ops, sizeof(adap->fe->ops.tuner_ops));
  262. return 0;
  263. out_free:
  264. if (adap->fe)
  265. dvb_frontend_detach(adap->fe);
  266. adap->fe = NULL;
  267. adap->fe2 = NULL;
  268. return result;
  269. }
  270. int az6007_power_ctrl(struct dvb_usb_device *d, int onoff)
  271. {
  272. if (!onoff)
  273. return 0;
  274. info("Sending poweron sequence");
  275. az6007_write(d->udev, AZ6007_TS_THROUGH, 0, 0, NULL, 0);
  276. #if 0
  277. // Seems to be a poweroff sequence
  278. az6007_write(d->udev, 0xbc, 1, 3, NULL, 0);
  279. az6007_write(d->udev, 0xbc, 1, 4, NULL, 0);
  280. az6007_write(d->udev, 0xc0, 0, 3, NULL, 0);
  281. az6007_write(d->udev, 0xc0, 1, 3, NULL, 0);
  282. az6007_write(d->udev, 0xbc, 0, 1, NULL, 0);
  283. #endif
  284. return 0;
  285. }
  286. static struct dvb_usb_device_properties az6007_properties;
  287. static void az6007_usb_disconnect(struct usb_interface *intf)
  288. {
  289. dvb_usb_device_exit(intf);
  290. }
  291. /* I2C */
  292. static int az6007_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
  293. int num)
  294. {
  295. struct dvb_usb_device *d = i2c_get_adapdata(adap);
  296. int i, j, len;
  297. int ret = 0;
  298. u16 index;
  299. u16 value;
  300. int length;
  301. u8 req, addr;
  302. u8 data[512];
  303. if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
  304. return -EAGAIN;
  305. for (i = 0; i < num; i++) {
  306. addr = msgs[i].addr << 1;
  307. if (((i + 1) < num)
  308. && (msgs[i].len == 1)
  309. && (!msgs[i].flags & I2C_M_RD)
  310. && (msgs[i + 1].flags & I2C_M_RD)
  311. && (msgs[i].addr == msgs[i + 1].addr)) {
  312. /*
  313. * A write + read xfer for the same address, where
  314. * the first xfer has just 1 byte length.
  315. * Need to join both into one operation
  316. */
  317. if (dvb_usb_az6007_debug & 2)
  318. printk(KERN_DEBUG
  319. "az6007 I2C xfer write+read addr=0x%x len=%d/%d: ",
  320. addr, msgs[i].len, msgs[i + 1].len);
  321. req = AZ6007_I2C_RD;
  322. index = msgs[i].buf[0];
  323. value = addr | (1 << 8);
  324. length = 6 + msgs[i + 1].len;
  325. len = msgs[i + 1].len;
  326. ret = az6007_read(d->udev, req, value, index, data,
  327. length);
  328. if (ret >= len) {
  329. for (j = 0; j < len; j++) {
  330. msgs[i + 1].buf[j] = data[j + 5];
  331. if (dvb_usb_az6007_debug & 2)
  332. printk(KERN_CONT
  333. "0x%02x ",
  334. msgs[i + 1].buf[j]);
  335. }
  336. } else
  337. ret = -EIO;
  338. i++;
  339. } else if (!(msgs[i].flags & I2C_M_RD)) {
  340. /* write bytes */
  341. if (dvb_usb_az6007_debug & 2)
  342. printk(KERN_DEBUG
  343. "az6007 I2C xfer write addr=0x%x len=%d: ",
  344. addr, msgs[i].len);
  345. req = AZ6007_I2C_WR;
  346. index = msgs[i].buf[0];
  347. value = addr | (1 << 8);
  348. length = msgs[i].len - 1;
  349. len = msgs[i].len - 1;
  350. if (dvb_usb_az6007_debug & 2)
  351. printk(KERN_CONT "(0x%02x) ", msgs[i].buf[0]);
  352. for (j = 0; j < len; j++) {
  353. data[j] = msgs[i].buf[j + 1];
  354. if (dvb_usb_az6007_debug & 2)
  355. printk(KERN_CONT "0x%02x ", data[j]);
  356. }
  357. ret = az6007_write(d->udev, req, value, index, data,
  358. length);
  359. } else {
  360. /* read bytes */
  361. if (dvb_usb_az6007_debug & 2)
  362. printk(KERN_DEBUG
  363. "az6007 I2C xfer read addr=0x%x len=%d: ",
  364. addr, msgs[i].len);
  365. req = AZ6007_I2C_RD;
  366. index = msgs[i].buf[0];
  367. value = addr;
  368. length = msgs[i].len + 6;
  369. len = msgs[i].len;
  370. ret = az6007_read(d->udev, req, value, index, data,
  371. length);
  372. for (j = 0; j < len; j++) {
  373. msgs[i].buf[j] = data[j + 5];
  374. if (dvb_usb_az6007_debug & 2)
  375. printk(KERN_CONT
  376. "0x%02x ", data[j + 5]);
  377. }
  378. }
  379. if (dvb_usb_az6007_debug & 2)
  380. printk(KERN_CONT "\n");
  381. if (ret < 0)
  382. goto err;
  383. }
  384. err:
  385. mutex_unlock(&d->i2c_mutex);
  386. if (ret < 0) {
  387. info("%s ERROR: %i", __func__, ret);
  388. return ret;
  389. }
  390. return num;
  391. }
  392. static u32 az6007_i2c_func(struct i2c_adapter *adapter)
  393. {
  394. return I2C_FUNC_I2C;
  395. }
  396. static struct i2c_algorithm az6007_i2c_algo = {
  397. .master_xfer = az6007_i2c_xfer,
  398. .functionality = az6007_i2c_func,
  399. };
  400. int az6007_identify_state(struct usb_device *udev,
  401. struct dvb_usb_device_properties *props,
  402. struct dvb_usb_device_description **desc, int *cold)
  403. {
  404. int ret;
  405. u8 mac[6];
  406. /* Try to read the mac address */
  407. ret = az6007_read(udev, AZ6007_READ_DATA, 6, 0, mac, 6);
  408. if (ret == 6)
  409. *cold = 0;
  410. else
  411. *cold = 1;
  412. deb_info("Device is on %s state\n", *cold? "warm" : "cold");
  413. return 0;
  414. }
  415. static int az6007_usb_probe(struct usb_interface *intf,
  416. const struct usb_device_id *id)
  417. {
  418. az6007_led_on_off(intf, 0);
  419. return dvb_usb_device_init(intf, &az6007_properties,
  420. THIS_MODULE, NULL, adapter_nr);
  421. }
  422. static struct usb_device_id az6007_usb_table[] = {
  423. {USB_DEVICE(USB_VID_AZUREWAVE, USB_PID_AZUREWAVE_6007)},
  424. {USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_H7)},
  425. {0},
  426. };
  427. MODULE_DEVICE_TABLE(usb, az6007_usb_table);
  428. static struct dvb_usb_device_properties az6007_properties = {
  429. .caps = DVB_USB_IS_AN_I2C_ADAPTER,
  430. .usb_ctrl = CYPRESS_FX2,
  431. .firmware = "dvb-usb-terratec-h7-az6007.fw",
  432. .no_reconnect = 1,
  433. .identify_state = az6007_identify_state,
  434. .num_adapters = 1,
  435. .adapter = {
  436. {
  437. .streaming_ctrl = az6007_streaming_ctrl,
  438. .frontend_attach = az6007_frontend_attach,
  439. /* parameter for the MPEG2-data transfer */
  440. .stream = {
  441. .type = USB_BULK,
  442. .count = 10,
  443. .endpoint = 0x02,
  444. .u = {
  445. .bulk = {
  446. .buffersize = 4096,
  447. }
  448. }
  449. },
  450. .size_of_priv = sizeof(struct az6007_device_state),
  451. }
  452. },
  453. .power_ctrl = az6007_power_ctrl,
  454. .read_mac_address = az6007_read_mac_addr,
  455. .rc.legacy = {
  456. .rc_map_table = rc_map_az6007_table,
  457. .rc_map_size = ARRAY_SIZE(rc_map_az6007_table),
  458. .rc_interval = 400,
  459. .rc_query = az6007_rc_query,
  460. },
  461. .i2c_algo = &az6007_i2c_algo,
  462. .num_device_descs = 2,
  463. .devices = {
  464. { .name = "AzureWave DTV StarBox DVB-T/C USB2.0 (az6007)",
  465. .cold_ids = { &az6007_usb_table[0], NULL },
  466. .warm_ids = { NULL },
  467. },
  468. { .name = "TerraTec DTV StarBox DVB-T/C USB2.0 (az6007)",
  469. .cold_ids = { &az6007_usb_table[1], NULL },
  470. .warm_ids = { NULL },
  471. },
  472. { NULL },
  473. }
  474. };
  475. /* usb specific object needed to register this driver with the usb subsystem */
  476. static struct usb_driver az6007_usb_driver = {
  477. .name = "dvb_usb_az6007",
  478. .probe = az6007_usb_probe,
  479. .disconnect = dvb_usb_device_exit,
  480. /* .disconnect = az6007_usb_disconnect, */
  481. .id_table = az6007_usb_table,
  482. };
  483. /* module stuff */
  484. static int __init az6007_usb_module_init(void)
  485. {
  486. int result;
  487. deb_info("az6007 usb module init\n");
  488. result = usb_register(&az6007_usb_driver);
  489. if (result) {
  490. err("usb_register failed. (%d)", result);
  491. return result;
  492. }
  493. return 0;
  494. }
  495. static void __exit az6007_usb_module_exit(void)
  496. {
  497. /* deregister this driver from the USB subsystem */
  498. deb_info("az6007 usb module exit\n");
  499. usb_deregister(&az6007_usb_driver);
  500. }
  501. module_init(az6007_usb_module_init);
  502. module_exit(az6007_usb_module_exit);
  503. MODULE_AUTHOR("Henry Wang <Henry.wang@AzureWave.com>");
  504. MODULE_DESCRIPTION("Driver for AzureWave 6007 DVB-C/T USB2.0 and clones");
  505. MODULE_VERSION("1.1");
  506. MODULE_LICENSE("GPL");