az6007.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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. /* remote control stuff (does not work with my box) */
  156. static int az6007_rc_query(struct dvb_usb_device *d)
  157. {
  158. struct az6007_device_state *st = d->priv;
  159. unsigned code = 0;
  160. az6007_read(d, AZ6007_READ_IR, 0, 0, st->data, 10);
  161. if (st->data[1] == 0x44)
  162. return 0;
  163. if ((st->data[1] ^ st->data[2]) == 0xff)
  164. code = st->data[1];
  165. else
  166. code = st->data[1] << 8 | st->data[2];
  167. if ((st->data[3] ^ st->data[4]) == 0xff)
  168. code = code << 8 | st->data[3];
  169. else
  170. code = code << 16 | st->data[3] << 8| st->data[4];
  171. printk("remote query key: %04x\n", code);
  172. rc_keydown(d->rc_dev, code, st->data[5]);
  173. return 0;
  174. }
  175. static int az6007_read_mac_addr(struct dvb_usb_device *d, u8 mac[6])
  176. {
  177. struct az6007_device_state *st = d->priv;
  178. int ret;
  179. ret = az6007_read(d, AZ6007_READ_DATA, 6, 0, st->data, 6);
  180. memcpy(mac, st->data, sizeof(mac));
  181. if (ret > 0)
  182. deb_info("%s: mac is %02x:%02x:%02x:%02x:%02x:%02x\n",
  183. __func__, mac[0], mac[1], mac[2],
  184. mac[3], mac[4], mac[5]);
  185. return ret;
  186. }
  187. static int az6007_frontend_attach(struct dvb_usb_adapter *adap)
  188. {
  189. struct az6007_device_state *st = adap->dev->priv;
  190. deb_info("attaching demod drxk");
  191. adap->fe_adap[0].fe = dvb_attach(drxk_attach, &terratec_h7_drxk,
  192. &adap->dev->i2c_adap);
  193. if (!adap->fe_adap[0].fe)
  194. return -EINVAL;
  195. adap->fe_adap[0].fe->sec_priv = adap;
  196. st->gate_ctrl = adap->fe_adap[0].fe->ops.i2c_gate_ctrl;
  197. adap->fe_adap[0].fe->ops.i2c_gate_ctrl = drxk_gate_ctrl;
  198. return 0;
  199. }
  200. static int az6007_tuner_attach(struct dvb_usb_adapter *adap)
  201. {
  202. deb_info("attaching tuner mt2063");
  203. /* Attach mt2063 to DVB-C frontend */
  204. if (adap->fe_adap[0].fe->ops.i2c_gate_ctrl)
  205. adap->fe_adap[0].fe->ops.i2c_gate_ctrl(adap->fe_adap[0].fe, 1);
  206. if (!dvb_attach(mt2063_attach, adap->fe_adap[0].fe,
  207. &az6007_mt2063_config,
  208. &adap->dev->i2c_adap))
  209. return -EINVAL;
  210. if (adap->fe_adap[0].fe->ops.i2c_gate_ctrl)
  211. adap->fe_adap[0].fe->ops.i2c_gate_ctrl(adap->fe_adap[0].fe, 0);
  212. return 0;
  213. }
  214. int az6007_power_ctrl(struct dvb_usb_device *d, int onoff)
  215. {
  216. struct az6007_device_state *st = d->priv;
  217. int ret;
  218. deb_info("%s()\n", __func__);
  219. if (!st->warm) {
  220. mutex_init(&st->mutex);
  221. ret = az6007_write(d, AZ6007_POWER, 0, 2, NULL, 0);
  222. if (ret < 0)
  223. return ret;
  224. msleep(60);
  225. ret = az6007_write(d, AZ6007_POWER, 1, 4, NULL, 0);
  226. if (ret < 0)
  227. return ret;
  228. msleep(100);
  229. ret = az6007_write(d, AZ6007_POWER, 1, 3, NULL, 0);
  230. if (ret < 0)
  231. return ret;
  232. msleep(20);
  233. ret = az6007_write(d, AZ6007_POWER, 1, 4, NULL, 0);
  234. if (ret < 0)
  235. return ret;
  236. msleep(400);
  237. ret = az6007_write(d, FX2_SCON1, 0, 3, NULL, 0);
  238. if (ret < 0)
  239. return ret;
  240. msleep (150);
  241. ret = az6007_write(d, FX2_SCON1, 1, 3, NULL, 0);
  242. if (ret < 0)
  243. return ret;
  244. msleep (430);
  245. ret = az6007_write(d, AZ6007_POWER, 0, 0, NULL, 0);
  246. if (ret < 0)
  247. return ret;
  248. st->warm = true;
  249. return 0;
  250. }
  251. if (!onoff)
  252. return 0;
  253. az6007_write(d, AZ6007_POWER, 0, 0, NULL, 0);
  254. az6007_write(d, AZ6007_TS_THROUGH, 0, 0, NULL, 0);
  255. return 0;
  256. }
  257. /* I2C */
  258. static int az6007_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
  259. int num)
  260. {
  261. struct dvb_usb_device *d = i2c_get_adapdata(adap);
  262. struct az6007_device_state *st = d->priv;
  263. int i, j, len;
  264. int ret = 0;
  265. u16 index;
  266. u16 value;
  267. int length;
  268. u8 req, addr;
  269. if (mutex_lock_interruptible(&st->mutex) < 0)
  270. return -EAGAIN;
  271. for (i = 0; i < num; i++) {
  272. addr = msgs[i].addr << 1;
  273. if (((i + 1) < num)
  274. && (msgs[i].len == 1)
  275. && (!msgs[i].flags & I2C_M_RD)
  276. && (msgs[i + 1].flags & I2C_M_RD)
  277. && (msgs[i].addr == msgs[i + 1].addr)) {
  278. /*
  279. * A write + read xfer for the same address, where
  280. * the first xfer has just 1 byte length.
  281. * Need to join both into one operation
  282. */
  283. if (dvb_usb_az6007_debug & 2)
  284. printk(KERN_DEBUG
  285. "az6007 I2C xfer write+read addr=0x%x len=%d/%d: ",
  286. addr, msgs[i].len, msgs[i + 1].len);
  287. req = AZ6007_I2C_RD;
  288. index = msgs[i].buf[0];
  289. value = addr | (1 << 8);
  290. length = 6 + msgs[i + 1].len;
  291. len = msgs[i + 1].len;
  292. ret = __az6007_read(d->udev, req, value, index, st->data,
  293. length);
  294. if (ret >= len) {
  295. for (j = 0; j < len; j++) {
  296. msgs[i + 1].buf[j] = st->data[j + 5];
  297. if (dvb_usb_az6007_debug & 2)
  298. printk(KERN_CONT
  299. "0x%02x ",
  300. msgs[i + 1].buf[j]);
  301. }
  302. } else
  303. ret = -EIO;
  304. i++;
  305. } else if (!(msgs[i].flags & I2C_M_RD)) {
  306. /* write bytes */
  307. if (dvb_usb_az6007_debug & 2)
  308. printk(KERN_DEBUG
  309. "az6007 I2C xfer write addr=0x%x len=%d: ",
  310. addr, msgs[i].len);
  311. req = AZ6007_I2C_WR;
  312. index = msgs[i].buf[0];
  313. value = addr | (1 << 8);
  314. length = msgs[i].len - 1;
  315. len = msgs[i].len - 1;
  316. if (dvb_usb_az6007_debug & 2)
  317. printk(KERN_CONT "(0x%02x) ", msgs[i].buf[0]);
  318. for (j = 0; j < len; j++) {
  319. st->data[j] = msgs[i].buf[j + 1];
  320. if (dvb_usb_az6007_debug & 2)
  321. printk(KERN_CONT "0x%02x ", st->data[j]);
  322. }
  323. ret = __az6007_write(d->udev, req, value, index, st->data,
  324. length);
  325. } else {
  326. /* read bytes */
  327. if (dvb_usb_az6007_debug & 2)
  328. printk(KERN_DEBUG
  329. "az6007 I2C xfer read addr=0x%x len=%d: ",
  330. addr, msgs[i].len);
  331. req = AZ6007_I2C_RD;
  332. index = msgs[i].buf[0];
  333. value = addr;
  334. length = msgs[i].len + 6;
  335. len = msgs[i].len;
  336. ret = __az6007_read(d->udev, req, value, index, st->data,
  337. length);
  338. for (j = 0; j < len; j++) {
  339. msgs[i].buf[j] = st->data[j + 5];
  340. if (dvb_usb_az6007_debug & 2)
  341. printk(KERN_CONT
  342. "0x%02x ", st->data[j + 5]);
  343. }
  344. }
  345. if (dvb_usb_az6007_debug & 2)
  346. printk(KERN_CONT "\n");
  347. if (ret < 0)
  348. goto err;
  349. }
  350. err:
  351. mutex_unlock(&st->mutex);
  352. if (ret < 0) {
  353. info("%s ERROR: %i", __func__, ret);
  354. return ret;
  355. }
  356. return num;
  357. }
  358. static u32 az6007_i2c_func(struct i2c_adapter *adapter)
  359. {
  360. return I2C_FUNC_I2C;
  361. }
  362. static struct i2c_algorithm az6007_i2c_algo = {
  363. .master_xfer = az6007_i2c_xfer,
  364. .functionality = az6007_i2c_func,
  365. };
  366. int az6007_identify_state(struct usb_device *udev,
  367. struct dvb_usb_device_properties *props,
  368. struct dvb_usb_device_description **desc, int *cold)
  369. {
  370. int ret;
  371. u8 *mac;
  372. mac = kmalloc(6, GFP_ATOMIC);
  373. if (!mac)
  374. return -ENOMEM;
  375. /* Try to read the mac address */
  376. ret = __az6007_read(udev, AZ6007_READ_DATA, 6, 0, mac, 6);
  377. if (ret == 6)
  378. *cold = 0;
  379. else
  380. *cold = 1;
  381. kfree(mac);
  382. if (*cold) {
  383. __az6007_write(udev, 0x09, 1, 0, NULL, 0);
  384. __az6007_write(udev, 0x00, 0, 0, NULL, 0);
  385. __az6007_write(udev, 0x00, 0, 0, NULL, 0);
  386. }
  387. deb_info("Device is on %s state\n", *cold? "warm" : "cold");
  388. return 0;
  389. }
  390. static struct dvb_usb_device_properties az6007_properties;
  391. static int az6007_usb_probe(struct usb_interface *intf,
  392. const struct usb_device_id *id)
  393. {
  394. return dvb_usb_device_init(intf, &az6007_properties,
  395. THIS_MODULE, NULL, adapter_nr);
  396. }
  397. static struct usb_device_id az6007_usb_table[] = {
  398. {USB_DEVICE(USB_VID_AZUREWAVE, USB_PID_AZUREWAVE_6007)},
  399. {USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_H7)},
  400. {0},
  401. };
  402. MODULE_DEVICE_TABLE(usb, az6007_usb_table);
  403. static struct dvb_usb_device_properties az6007_properties = {
  404. .caps = DVB_USB_IS_AN_I2C_ADAPTER,
  405. .usb_ctrl = CYPRESS_FX2,
  406. .firmware = "dvb-usb-terratec-h7-az6007.fw",
  407. .no_reconnect = 1,
  408. .size_of_priv = sizeof(struct az6007_device_state),
  409. .identify_state = az6007_identify_state,
  410. .num_adapters = 1,
  411. .adapter = {
  412. {
  413. .num_frontends = 1,
  414. .fe = {{
  415. .streaming_ctrl = az6007_streaming_ctrl,
  416. .tuner_attach = az6007_tuner_attach,
  417. .frontend_attach = az6007_frontend_attach,
  418. /* parameter for the MPEG2-data transfer */
  419. .stream = {
  420. .type = USB_BULK,
  421. .count = 10,
  422. .endpoint = 0x02,
  423. .u = {
  424. .bulk = {
  425. .buffersize = 4096,
  426. }
  427. }
  428. },
  429. }}
  430. } },
  431. .power_ctrl = az6007_power_ctrl,
  432. .read_mac_address = az6007_read_mac_addr,
  433. .rc.core = {
  434. .rc_interval = 400,
  435. .rc_codes = RC_MAP_DIB0700_NEC_TABLE,
  436. .module_name = "az6007",
  437. .rc_query = az6007_rc_query,
  438. .allowed_protos = RC_TYPE_NEC,
  439. },
  440. .i2c_algo = &az6007_i2c_algo,
  441. .num_device_descs = 2,
  442. .devices = {
  443. { .name = "AzureWave DTV StarBox DVB-T/C USB2.0 (az6007)",
  444. .cold_ids = { &az6007_usb_table[0], NULL },
  445. .warm_ids = { NULL },
  446. },
  447. { .name = "TerraTec DTV StarBox DVB-T/C USB2.0 (az6007)",
  448. .cold_ids = { &az6007_usb_table[1], NULL },
  449. .warm_ids = { NULL },
  450. },
  451. { NULL },
  452. }
  453. };
  454. /* usb specific object needed to register this driver with the usb subsystem */
  455. static struct usb_driver az6007_usb_driver = {
  456. .name = "dvb_usb_az6007",
  457. .probe = az6007_usb_probe,
  458. .disconnect = dvb_usb_device_exit,
  459. .id_table = az6007_usb_table,
  460. };
  461. /* module stuff */
  462. static int __init az6007_usb_module_init(void)
  463. {
  464. int result;
  465. deb_info("az6007 usb module init\n");
  466. result = usb_register(&az6007_usb_driver);
  467. if (result) {
  468. err("usb_register failed. (%d)", result);
  469. return result;
  470. }
  471. return 0;
  472. }
  473. static void __exit az6007_usb_module_exit(void)
  474. {
  475. /* deregister this driver from the USB subsystem */
  476. deb_info("az6007 usb module exit\n");
  477. usb_deregister(&az6007_usb_driver);
  478. }
  479. module_init(az6007_usb_module_init);
  480. module_exit(az6007_usb_module_exit);
  481. MODULE_AUTHOR("Henry Wang <Henry.wang@AzureWave.com>");
  482. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
  483. MODULE_DESCRIPTION("Driver for AzureWave 6007 DVB-C/T USB2.0 and clones");
  484. MODULE_VERSION("1.1");
  485. MODULE_LICENSE("GPL");