az6007.c 15 KB

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