az6007.c 15 KB

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