az6007.c 15 KB

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