az6007.c 15 KB

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