az6007.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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 0xb4
  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;
  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 read 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 write 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. struct rc_map_table *keymap = d->props.rc.legacy.rc_map_table;
  146. u8 key[10];
  147. int i;
  148. /*
  149. * FIXME: remove the following return to enabled remote querying
  150. * The driver likely needs proper locking to avoid troubles between
  151. * this call and other concurrent calls.
  152. */
  153. return 0;
  154. az6007_read(d->udev, AZ6007_READ_IR, 0, 0, key, 10);
  155. if (key[1] == 0x44) {
  156. *state = REMOTE_NO_KEY_PRESSED;
  157. return 0;
  158. }
  159. /*
  160. * FIXME: need to make something useful with the keycodes and to
  161. * convert it to the non-legacy mode. Yet, it is producing some
  162. * debug info already, like:
  163. * 88 04 eb 02 fd ff 00 82 63 82 (terratec IR)
  164. * 88 04 eb 03 fc 00 00 82 63 82 (terratec IR)
  165. * 88 80 7e 0d f2 ff 00 82 63 82 (another NEC-extended based IR)
  166. * I suspect that the IR data is at bytes 1 to 4, and byte 5 is parity
  167. */
  168. deb_rc("remote query key: %x %d\n", key[1], key[1]);
  169. print_hex_dump_bytes("Remote: ", DUMP_PREFIX_NONE, key, 10);
  170. for (i = 0; i < d->props.rc.legacy.rc_map_size; i++) {
  171. if (rc5_custom(&keymap[i]) == key[1]) {
  172. *event = keymap[i].keycode;
  173. *state = REMOTE_KEY_PRESSED;
  174. return 0;
  175. }
  176. }
  177. return 0;
  178. }
  179. #if 0
  180. int az6007_power_ctrl(struct dvb_usb_device *d, int onoff)
  181. {
  182. u8 v = onoff;
  183. return az6007_write(d->udev, AZ6007_POWER, v , 3, NULL, 1);
  184. }
  185. #endif
  186. static int az6007_read_mac_addr(struct dvb_usb_device *d, u8 mac[6])
  187. {
  188. int ret;
  189. ret = az6007_read(d->udev, AZ6007_READ_DATA, 6, 0, mac, 6);
  190. if (ret > 0)
  191. deb_info("%s: mac is %02x:%02x:%02x:%02x:%02x:%02x\n",
  192. __func__, mac[0], mac[1], mac[2],
  193. mac[3], mac[4], mac[5]);
  194. return ret;
  195. }
  196. static int az6007_frontend_poweron(struct dvb_usb_adapter *adap)
  197. {
  198. int ret;
  199. struct usb_device *udev = adap->dev->udev;
  200. deb_info("%s: adap=%p adap->dev=%p\n", __func__, adap, adap->dev);
  201. ret = az6007_write(udev, AZ6007_POWER, 0, 2, NULL, 0);
  202. if (ret < 0)
  203. goto error;
  204. msleep(150);
  205. ret = az6007_write(udev, AZ6007_POWER, 1, 4, NULL, 0);
  206. if (ret < 0)
  207. goto error;
  208. msleep(100);
  209. ret = az6007_write(udev, AZ6007_POWER, 1, 3, NULL, 0);
  210. if (ret < 0)
  211. goto error;
  212. msleep(100);
  213. ret = az6007_write(udev, AZ6007_POWER, 1, 4, NULL, 0);
  214. if (ret < 0)
  215. goto error;
  216. msleep(100);
  217. ret = az6007_write(udev, FX2_SCON1, 0, 3, NULL, 0);
  218. if (ret < 0)
  219. goto error;
  220. msleep (10);
  221. ret = az6007_write(udev, FX2_SCON1, 1, 3, NULL, 0);
  222. if (ret < 0)
  223. goto error;
  224. msleep (10);
  225. ret = az6007_write(udev, AZ6007_POWER, 0, 0, NULL, 0);
  226. error:
  227. if (ret < 0)
  228. err("%s failed with error %d", __func__, ret);
  229. return ret;
  230. }
  231. static int az6007_led_on_off(struct usb_interface *intf, int onoff)
  232. {
  233. struct usb_device *udev = interface_to_usbdev(intf);
  234. int ret;
  235. /* TS through */
  236. ret = az6007_write(udev, AZ6007_POWER, onoff, 0, NULL, 0);
  237. if (ret < 0)
  238. err("%s failed with error %d", __func__, ret);
  239. return ret;
  240. }
  241. static int az6007_frontend_attach(struct dvb_usb_adapter *adap)
  242. {
  243. struct az6007_device_state *st = adap->priv;
  244. int result;
  245. BUG_ON(!st);
  246. az6007_frontend_poweron(adap);
  247. info("az6007: attaching demod drxk");
  248. adap->fe = dvb_attach(drxk_attach, &terratec_h7_drxk,
  249. &adap->dev->i2c_adap, &adap->fe2);
  250. if (!adap->fe) {
  251. result = -EINVAL;
  252. goto out_free;
  253. }
  254. deb_info("Setting hacks\n");
  255. /* FIXME: do we need a pll semaphore? */
  256. adap->fe->sec_priv = adap;
  257. sema_init(&st->pll_mutex, 1);
  258. st->gate_ctrl = adap->fe->ops.i2c_gate_ctrl;
  259. adap->fe->ops.i2c_gate_ctrl = drxk_gate_ctrl;
  260. adap->fe2->id = 1;
  261. info("az6007: attaching tuner mt2063");
  262. /* Attach mt2063 to DVB-C frontend */
  263. if (adap->fe->ops.i2c_gate_ctrl)
  264. adap->fe->ops.i2c_gate_ctrl(adap->fe, 1);
  265. if (!dvb_attach(mt2063_attach, adap->fe, &az6007_mt2063_config,
  266. &adap->dev->i2c_adap)) {
  267. result = -EINVAL;
  268. goto out_free;
  269. }
  270. if (adap->fe->ops.i2c_gate_ctrl)
  271. adap->fe->ops.i2c_gate_ctrl(adap->fe, 0);
  272. /* Hack - needed due to drxk */
  273. adap->fe2->tuner_priv = adap->fe->tuner_priv;
  274. memcpy(&adap->fe2->ops.tuner_ops,
  275. &adap->fe->ops.tuner_ops, sizeof(adap->fe->ops.tuner_ops));
  276. return 0;
  277. out_free:
  278. if (adap->fe)
  279. dvb_frontend_detach(adap->fe);
  280. adap->fe = NULL;
  281. adap->fe2 = NULL;
  282. return result;
  283. }
  284. int az6007_power_ctrl(struct dvb_usb_device *d, int onoff)
  285. {
  286. if (!onoff)
  287. return 0;
  288. info("Sending poweron sequence");
  289. az6007_write(d->udev, AZ6007_TS_THROUGH, 0, 0, NULL, 0);
  290. #if 0
  291. // Seems to be a poweroff sequence
  292. az6007_write(d->udev, 0xbc, 1, 3, NULL, 0);
  293. az6007_write(d->udev, 0xbc, 1, 4, NULL, 0);
  294. az6007_write(d->udev, 0xc0, 0, 3, NULL, 0);
  295. az6007_write(d->udev, 0xc0, 1, 3, NULL, 0);
  296. az6007_write(d->udev, 0xbc, 0, 1, NULL, 0);
  297. #endif
  298. return 0;
  299. }
  300. static struct dvb_usb_device_properties az6007_properties;
  301. static void az6007_usb_disconnect(struct usb_interface *intf)
  302. {
  303. dvb_usb_device_exit(intf);
  304. }
  305. /* I2C */
  306. static int az6007_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
  307. int num)
  308. {
  309. struct dvb_usb_device *d = i2c_get_adapdata(adap);
  310. int i, j, len;
  311. int ret = 0;
  312. u16 index;
  313. u16 value;
  314. int length;
  315. u8 req, addr;
  316. u8 data[512];
  317. if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
  318. return -EAGAIN;
  319. for (i = 0; i < num; i++) {
  320. addr = msgs[i].addr << 1;
  321. if (((i + 1) < num)
  322. && (msgs[i].len == 1)
  323. && (!msgs[i].flags & I2C_M_RD)
  324. && (msgs[i + 1].flags & I2C_M_RD)
  325. && (msgs[i].addr == msgs[i + 1].addr)) {
  326. /*
  327. * A write + read xfer for the same address, where
  328. * the first xfer has just 1 byte length.
  329. * Need to join both into one operation
  330. */
  331. if (dvb_usb_az6007_debug & 2)
  332. printk(KERN_DEBUG
  333. "az6007 I2C xfer write+read addr=0x%x len=%d/%d: ",
  334. addr, msgs[i].len, msgs[i + 1].len);
  335. req = AZ6007_I2C_RD;
  336. index = msgs[i].buf[0];
  337. value = addr | (1 << 8);
  338. length = 6 + msgs[i + 1].len;
  339. len = msgs[i + 1].len;
  340. ret = az6007_read(d->udev, req, value, index, data,
  341. length);
  342. if (ret >= len) {
  343. for (j = 0; j < len; j++) {
  344. msgs[i + 1].buf[j] = data[j + 5];
  345. if (dvb_usb_az6007_debug & 2)
  346. printk(KERN_CONT
  347. "0x%02x ",
  348. msgs[i + 1].buf[j]);
  349. }
  350. } else
  351. ret = -EIO;
  352. i++;
  353. } else if (!(msgs[i].flags & I2C_M_RD)) {
  354. /* write bytes */
  355. if (dvb_usb_az6007_debug & 2)
  356. printk(KERN_DEBUG
  357. "az6007 I2C xfer write addr=0x%x len=%d: ",
  358. addr, msgs[i].len);
  359. req = AZ6007_I2C_WR;
  360. index = msgs[i].buf[0];
  361. value = addr | (1 << 8);
  362. length = msgs[i].len - 1;
  363. len = msgs[i].len - 1;
  364. if (dvb_usb_az6007_debug & 2)
  365. printk(KERN_CONT "(0x%02x) ", msgs[i].buf[0]);
  366. for (j = 0; j < len; j++) {
  367. data[j] = msgs[i].buf[j + 1];
  368. if (dvb_usb_az6007_debug & 2)
  369. printk(KERN_CONT "0x%02x ", data[j]);
  370. }
  371. ret = az6007_write(d->udev, req, value, index, data,
  372. length);
  373. } else {
  374. /* read bytes */
  375. if (dvb_usb_az6007_debug & 2)
  376. printk(KERN_DEBUG
  377. "az6007 I2C xfer read addr=0x%x len=%d: ",
  378. addr, msgs[i].len);
  379. req = AZ6007_I2C_RD;
  380. index = msgs[i].buf[0];
  381. value = addr;
  382. length = msgs[i].len + 6;
  383. len = msgs[i].len;
  384. ret = az6007_read(d->udev, req, value, index, data,
  385. length);
  386. for (j = 0; j < len; j++) {
  387. msgs[i].buf[j] = data[j + 5];
  388. if (dvb_usb_az6007_debug & 2)
  389. printk(KERN_CONT
  390. "0x%02x ", data[j + 5]);
  391. }
  392. }
  393. if (dvb_usb_az6007_debug & 2)
  394. printk(KERN_CONT "\n");
  395. if (ret < 0)
  396. goto err;
  397. }
  398. err:
  399. mutex_unlock(&d->i2c_mutex);
  400. if (ret < 0) {
  401. info("%s ERROR: %i", __func__, ret);
  402. return ret;
  403. }
  404. return num;
  405. }
  406. static u32 az6007_i2c_func(struct i2c_adapter *adapter)
  407. {
  408. return I2C_FUNC_I2C;
  409. }
  410. static struct i2c_algorithm az6007_i2c_algo = {
  411. .master_xfer = az6007_i2c_xfer,
  412. .functionality = az6007_i2c_func,
  413. };
  414. int az6007_identify_state(struct usb_device *udev,
  415. struct dvb_usb_device_properties *props,
  416. struct dvb_usb_device_description **desc, int *cold)
  417. {
  418. int ret;
  419. u8 mac[6];
  420. /* Try to read the mac address */
  421. ret = az6007_read(udev, AZ6007_READ_DATA, 6, 0, mac, 6);
  422. if (ret == 6)
  423. *cold = 0;
  424. else
  425. *cold = 1;
  426. deb_info("Device is on %s state\n", *cold? "warm" : "cold");
  427. return 0;
  428. }
  429. static int az6007_usb_probe(struct usb_interface *intf,
  430. const struct usb_device_id *id)
  431. {
  432. az6007_led_on_off(intf, 0);
  433. return dvb_usb_device_init(intf, &az6007_properties,
  434. THIS_MODULE, NULL, adapter_nr);
  435. }
  436. static struct usb_device_id az6007_usb_table[] = {
  437. {USB_DEVICE(USB_VID_AZUREWAVE, USB_PID_AZUREWAVE_6007)},
  438. {USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_H7)},
  439. {0},
  440. };
  441. MODULE_DEVICE_TABLE(usb, az6007_usb_table);
  442. static struct dvb_usb_device_properties az6007_properties = {
  443. .caps = DVB_USB_IS_AN_I2C_ADAPTER,
  444. .usb_ctrl = CYPRESS_FX2,
  445. .firmware = "dvb-usb-terratec-h7-az6007.fw",
  446. .no_reconnect = 1,
  447. .identify_state = az6007_identify_state,
  448. .num_adapters = 1,
  449. .adapter = {
  450. {
  451. .streaming_ctrl = az6007_streaming_ctrl,
  452. .frontend_attach = az6007_frontend_attach,
  453. /* parameter for the MPEG2-data transfer */
  454. .stream = {
  455. .type = USB_BULK,
  456. .count = 10,
  457. .endpoint = 0x02,
  458. .u = {
  459. .bulk = {
  460. .buffersize = 4096,
  461. }
  462. }
  463. },
  464. .size_of_priv = sizeof(struct az6007_device_state),
  465. }
  466. },
  467. .power_ctrl = az6007_power_ctrl,
  468. .read_mac_address = az6007_read_mac_addr,
  469. .rc.legacy = {
  470. .rc_map_table = rc_map_az6007_table,
  471. .rc_map_size = ARRAY_SIZE(rc_map_az6007_table),
  472. .rc_interval = 400,
  473. .rc_query = az6007_rc_query,
  474. },
  475. .i2c_algo = &az6007_i2c_algo,
  476. .num_device_descs = 2,
  477. .devices = {
  478. { .name = "AzureWave DTV StarBox DVB-T/C USB2.0 (az6007)",
  479. .cold_ids = { &az6007_usb_table[0], NULL },
  480. .warm_ids = { NULL },
  481. },
  482. { .name = "TerraTec DTV StarBox DVB-T/C USB2.0 (az6007)",
  483. .cold_ids = { &az6007_usb_table[1], NULL },
  484. .warm_ids = { NULL },
  485. },
  486. { NULL },
  487. }
  488. };
  489. /* usb specific object needed to register this driver with the usb subsystem */
  490. static struct usb_driver az6007_usb_driver = {
  491. .name = "dvb_usb_az6007",
  492. .probe = az6007_usb_probe,
  493. .disconnect = dvb_usb_device_exit,
  494. /* .disconnect = az6007_usb_disconnect, */
  495. .id_table = az6007_usb_table,
  496. };
  497. /* module stuff */
  498. static int __init az6007_usb_module_init(void)
  499. {
  500. int result;
  501. deb_info("az6007 usb module init\n");
  502. result = usb_register(&az6007_usb_driver);
  503. if (result) {
  504. err("usb_register failed. (%d)", result);
  505. return result;
  506. }
  507. return 0;
  508. }
  509. static void __exit az6007_usb_module_exit(void)
  510. {
  511. /* deregister this driver from the USB subsystem */
  512. deb_info("az6007 usb module exit\n");
  513. usb_deregister(&az6007_usb_driver);
  514. }
  515. module_init(az6007_usb_module_init);
  516. module_exit(az6007_usb_module_exit);
  517. MODULE_AUTHOR("Henry Wang <Henry.wang@AzureWave.com>");
  518. MODULE_DESCRIPTION("Driver for AzureWave 6007 DVB-C/T USB2.0 and clones");
  519. MODULE_VERSION("1.1");
  520. MODULE_LICENSE("GPL");