az6007.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  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 mutex ca_mutex;
  50. struct dvb_ca_en50221 ca;
  51. unsigned warm:1;
  52. int (*gate_ctrl) (struct dvb_frontend *, int);
  53. unsigned char data[4096];
  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. .enable_merr_cfg = true,
  61. .no_i2c_bridge = false,
  62. .chunk_size = 64,
  63. .mpeg_out_clk_strength = 0x02,
  64. .microcode_name = "dvb-usb-terratec-h7-drxk.fw",
  65. };
  66. static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable)
  67. {
  68. struct dvb_usb_adapter *adap = fe->sec_priv;
  69. struct az6007_device_state *st;
  70. int status = 0;
  71. deb_info("%s: %s\n", __func__, enable ? "enable" : "disable");
  72. if (!adap)
  73. return -EINVAL;
  74. st = adap->dev->priv;
  75. if (!st)
  76. return -EINVAL;
  77. if (enable)
  78. status = st->gate_ctrl(fe, 1);
  79. else
  80. status = st->gate_ctrl(fe, 0);
  81. return status;
  82. }
  83. static struct mt2063_config az6007_mt2063_config = {
  84. .tuner_address = 0x60,
  85. .refclock = 36125000,
  86. };
  87. static int __az6007_read(struct usb_device *udev, u8 req, u16 value,
  88. u16 index, u8 *b, int blen)
  89. {
  90. int ret;
  91. ret = usb_control_msg(udev,
  92. usb_rcvctrlpipe(udev, 0),
  93. req,
  94. USB_TYPE_VENDOR | USB_DIR_IN,
  95. value, index, b, blen, 5000);
  96. if (ret < 0) {
  97. warn("usb read operation failed. (%d)", ret);
  98. return -EIO;
  99. }
  100. deb_xfer("in: req. %02x, val: %04x, ind: %04x, buffer: ", req, value,
  101. index);
  102. debug_dump(b, blen, deb_xfer);
  103. return ret;
  104. }
  105. static int az6007_read(struct dvb_usb_device *d, u8 req, u16 value,
  106. u16 index, u8 *b, int blen)
  107. {
  108. struct az6007_device_state *st = d->priv;
  109. int ret;
  110. if (mutex_lock_interruptible(&st->mutex) < 0)
  111. return -EAGAIN;
  112. ret = __az6007_read(d->udev, req, value, index, b, blen);
  113. mutex_unlock(&st->mutex);
  114. return ret;
  115. }
  116. static int __az6007_write(struct usb_device *udev, u8 req, u16 value,
  117. u16 index, u8 *b, int blen)
  118. {
  119. int ret;
  120. deb_xfer("out: req. %02x, val: %04x, ind: %04x, buffer: ", req, value,
  121. index);
  122. debug_dump(b, blen, deb_xfer);
  123. if (blen > 64) {
  124. err("az6007: tried to write %d bytes, but I2C max size is 64 bytes\n",
  125. blen);
  126. return -EOPNOTSUPP;
  127. }
  128. ret = usb_control_msg(udev,
  129. usb_sndctrlpipe(udev, 0),
  130. req,
  131. USB_TYPE_VENDOR | USB_DIR_OUT,
  132. value, index, b, blen, 5000);
  133. if (ret != blen) {
  134. err("usb write operation failed. (%d)", ret);
  135. return -EIO;
  136. }
  137. return 0;
  138. }
  139. static int az6007_write(struct dvb_usb_device *d, u8 req, u16 value,
  140. u16 index, u8 *b, int blen)
  141. {
  142. struct az6007_device_state *st = d->priv;
  143. int ret;
  144. if (mutex_lock_interruptible(&st->mutex) < 0)
  145. return -EAGAIN;
  146. ret = __az6007_write(d->udev, req, value, index, b, blen);
  147. mutex_unlock(&st->mutex);
  148. return ret;
  149. }
  150. static int az6007_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
  151. {
  152. struct dvb_usb_device *d = adap->dev;
  153. deb_info("%s: %s", __func__, onoff ? "enable" : "disable");
  154. return az6007_write(d, 0xbc, onoff, 0, NULL, 0);
  155. }
  156. /* remote control stuff (does not work with my box) */
  157. static int az6007_rc_query(struct dvb_usb_device *d)
  158. {
  159. struct az6007_device_state *st = d->priv;
  160. unsigned code = 0;
  161. az6007_read(d, AZ6007_READ_IR, 0, 0, st->data, 10);
  162. if (st->data[1] == 0x44)
  163. return 0;
  164. if ((st->data[1] ^ st->data[2]) == 0xff)
  165. code = st->data[1];
  166. else
  167. code = st->data[1] << 8 | st->data[2];
  168. if ((st->data[3] ^ st->data[4]) == 0xff)
  169. code = code << 8 | st->data[3];
  170. else
  171. code = code << 16 | st->data[3] << 8 | st->data[4];
  172. rc_keydown(d->rc_dev, code, st->data[5]);
  173. return 0;
  174. }
  175. static int az6007_ci_read_attribute_mem(struct dvb_ca_en50221 *ca,
  176. int slot,
  177. int address)
  178. {
  179. struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
  180. struct az6007_device_state *state = (struct az6007_device_state *)d->priv;
  181. int ret;
  182. u8 req;
  183. u16 value;
  184. u16 index;
  185. int blen;
  186. u8 *b;
  187. if (slot != 0)
  188. return -EINVAL;
  189. b = kmalloc(12, GFP_KERNEL);
  190. if (!b)
  191. return -ENOMEM;
  192. mutex_lock(&state->ca_mutex);
  193. req = 0xC1;
  194. value = address;
  195. index = 0;
  196. blen = 1;
  197. ret = az6007_read(d, req, value, index, b, blen);
  198. if (ret < 0) {
  199. warn("usb in operation failed. (%d)", ret);
  200. ret = -EINVAL;
  201. } else {
  202. ret = b[0];
  203. }
  204. mutex_unlock(&state->ca_mutex);
  205. kfree(b);
  206. return ret;
  207. }
  208. static int az6007_ci_write_attribute_mem(struct dvb_ca_en50221 *ca,
  209. int slot,
  210. int address,
  211. u8 value)
  212. {
  213. struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
  214. struct az6007_device_state *state = (struct az6007_device_state *)d->priv;
  215. int ret;
  216. u8 req;
  217. u16 value1;
  218. u16 index;
  219. int blen;
  220. deb_info("%s %d", __func__, slot);
  221. if (slot != 0)
  222. return -EINVAL;
  223. mutex_lock(&state->ca_mutex);
  224. req = 0xC2;
  225. value1 = address;
  226. index = value;
  227. blen = 0;
  228. ret = az6007_write(d, req, value1, index, NULL, blen);
  229. if (ret != 0)
  230. warn("usb out operation failed. (%d)", ret);
  231. mutex_unlock(&state->ca_mutex);
  232. return ret;
  233. }
  234. static int az6007_ci_read_cam_control(struct dvb_ca_en50221 *ca,
  235. int slot,
  236. u8 address)
  237. {
  238. struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
  239. struct az6007_device_state *state = (struct az6007_device_state *)d->priv;
  240. int ret;
  241. u8 req;
  242. u16 value;
  243. u16 index;
  244. int blen;
  245. u8 *b;
  246. if (slot != 0)
  247. return -EINVAL;
  248. b = kmalloc(12, GFP_KERNEL);
  249. if (!b)
  250. return -ENOMEM;
  251. mutex_lock(&state->ca_mutex);
  252. req = 0xC3;
  253. value = address;
  254. index = 0;
  255. blen = 2;
  256. ret = az6007_read(d, req, value, index, b, blen);
  257. if (ret < 0) {
  258. warn("usb in operation failed. (%d)", ret);
  259. ret = -EINVAL;
  260. } else {
  261. if (b[0] == 0)
  262. warn("Read CI IO error");
  263. ret = b[1];
  264. deb_info("read cam data = %x from 0x%x", b[1], value);
  265. }
  266. mutex_unlock(&state->ca_mutex);
  267. kfree(b);
  268. return ret;
  269. }
  270. static int az6007_ci_write_cam_control(struct dvb_ca_en50221 *ca,
  271. int slot,
  272. u8 address,
  273. u8 value)
  274. {
  275. struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
  276. struct az6007_device_state *state = (struct az6007_device_state *)d->priv;
  277. int ret;
  278. u8 req;
  279. u16 value1;
  280. u16 index;
  281. int blen;
  282. if (slot != 0)
  283. return -EINVAL;
  284. mutex_lock(&state->ca_mutex);
  285. req = 0xC4;
  286. value1 = address;
  287. index = value;
  288. blen = 0;
  289. ret = az6007_write(d, req, value1, index, NULL, blen);
  290. if (ret != 0) {
  291. warn("usb out operation failed. (%d)", ret);
  292. goto failed;
  293. }
  294. failed:
  295. mutex_unlock(&state->ca_mutex);
  296. return ret;
  297. }
  298. static int CI_CamReady(struct dvb_ca_en50221 *ca, int slot)
  299. {
  300. struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
  301. int ret;
  302. u8 req;
  303. u16 value;
  304. u16 index;
  305. int blen;
  306. u8 *b;
  307. b = kmalloc(12, GFP_KERNEL);
  308. if (!b)
  309. return -ENOMEM;
  310. req = 0xC8;
  311. value = 0;
  312. index = 0;
  313. blen = 1;
  314. ret = az6007_read(d, req, value, index, b, blen);
  315. if (ret < 0) {
  316. warn("usb in operation failed. (%d)", ret);
  317. ret = -EIO;
  318. } else{
  319. ret = b[0];
  320. }
  321. kfree(b);
  322. return ret;
  323. }
  324. static int az6007_ci_slot_reset(struct dvb_ca_en50221 *ca, int slot)
  325. {
  326. struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
  327. struct az6007_device_state *state = (struct az6007_device_state *)d->priv;
  328. int ret, i;
  329. u8 req;
  330. u16 value;
  331. u16 index;
  332. int blen;
  333. mutex_lock(&state->ca_mutex);
  334. req = 0xC6;
  335. value = 1;
  336. index = 0;
  337. blen = 0;
  338. ret = az6007_write(d, req, value, index, NULL, blen);
  339. if (ret != 0) {
  340. warn("usb out operation failed. (%d)", ret);
  341. goto failed;
  342. }
  343. msleep(500);
  344. req = 0xC6;
  345. value = 0;
  346. index = 0;
  347. blen = 0;
  348. ret = az6007_write(d, req, value, index, NULL, blen);
  349. if (ret != 0) {
  350. warn("usb out operation failed. (%d)", ret);
  351. goto failed;
  352. }
  353. for (i = 0; i < 15; i++) {
  354. msleep(100);
  355. if (CI_CamReady(ca, slot)) {
  356. deb_info("CAM Ready");
  357. break;
  358. }
  359. }
  360. msleep(5000);
  361. failed:
  362. mutex_unlock(&state->ca_mutex);
  363. return ret;
  364. }
  365. static int az6007_ci_slot_shutdown(struct dvb_ca_en50221 *ca, int slot)
  366. {
  367. return 0;
  368. }
  369. static int az6007_ci_slot_ts_enable(struct dvb_ca_en50221 *ca, int slot)
  370. {
  371. struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
  372. struct az6007_device_state *state = (struct az6007_device_state *)d->priv;
  373. int ret;
  374. u8 req;
  375. u16 value;
  376. u16 index;
  377. int blen;
  378. deb_info("%s", __func__);
  379. mutex_lock(&state->ca_mutex);
  380. req = 0xC7;
  381. value = 1;
  382. index = 0;
  383. blen = 0;
  384. ret = az6007_write(d, req, value, index, NULL, blen);
  385. if (ret != 0) {
  386. warn("usb out operation failed. (%d)", ret);
  387. goto failed;
  388. }
  389. failed:
  390. mutex_unlock(&state->ca_mutex);
  391. return ret;
  392. }
  393. static int az6007_ci_poll_slot_status(struct dvb_ca_en50221 *ca, int slot, int open)
  394. {
  395. struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
  396. struct az6007_device_state *state = (struct az6007_device_state *)d->priv;
  397. int ret;
  398. u8 req;
  399. u16 value;
  400. u16 index;
  401. int blen;
  402. u8 *b;
  403. b = kmalloc(12, GFP_KERNEL);
  404. if (!b)
  405. return -ENOMEM;
  406. mutex_lock(&state->ca_mutex);
  407. req = 0xC5;
  408. value = 0;
  409. index = 0;
  410. blen = 1;
  411. ret = az6007_read(d, req, value, index, b, blen);
  412. if (ret < 0) {
  413. warn("usb in operation failed. (%d)", ret);
  414. ret = -EIO;
  415. } else
  416. ret = 0;
  417. if (!ret && b[0] == 1) {
  418. ret = DVB_CA_EN50221_POLL_CAM_PRESENT |
  419. DVB_CA_EN50221_POLL_CAM_READY;
  420. }
  421. mutex_unlock(&state->ca_mutex);
  422. kfree(b);
  423. return ret;
  424. }
  425. static void az6007_ci_uninit(struct dvb_usb_device *d)
  426. {
  427. struct az6007_device_state *state;
  428. deb_info("%s", __func__);
  429. if (NULL == d)
  430. return;
  431. state = (struct az6007_device_state *)d->priv;
  432. if (NULL == state)
  433. return;
  434. if (NULL == state->ca.data)
  435. return;
  436. dvb_ca_en50221_release(&state->ca);
  437. memset(&state->ca, 0, sizeof(state->ca));
  438. }
  439. static int az6007_ci_init(struct dvb_usb_adapter *a)
  440. {
  441. struct dvb_usb_device *d = a->dev;
  442. struct az6007_device_state *state = (struct az6007_device_state *)d->priv;
  443. int ret;
  444. deb_info("%s", __func__);
  445. mutex_init(&state->ca_mutex);
  446. state->ca.owner = THIS_MODULE;
  447. state->ca.read_attribute_mem = az6007_ci_read_attribute_mem;
  448. state->ca.write_attribute_mem = az6007_ci_write_attribute_mem;
  449. state->ca.read_cam_control = az6007_ci_read_cam_control;
  450. state->ca.write_cam_control = az6007_ci_write_cam_control;
  451. state->ca.slot_reset = az6007_ci_slot_reset;
  452. state->ca.slot_shutdown = az6007_ci_slot_shutdown;
  453. state->ca.slot_ts_enable = az6007_ci_slot_ts_enable;
  454. state->ca.poll_slot_status = az6007_ci_poll_slot_status;
  455. state->ca.data = d;
  456. ret = dvb_ca_en50221_init(&a->dvb_adap,
  457. &state->ca,
  458. 0, /* flags */
  459. 1);/* n_slots */
  460. if (ret != 0) {
  461. err("Cannot initialize CI: Error %d.", ret);
  462. memset(&state->ca, 0, sizeof(state->ca));
  463. return ret;
  464. }
  465. deb_info("CI initialized.");
  466. return 0;
  467. }
  468. static int az6007_read_mac_addr(struct dvb_usb_device *d, u8 mac[6])
  469. {
  470. struct az6007_device_state *st = d->priv;
  471. int ret;
  472. ret = az6007_read(d, AZ6007_READ_DATA, 6, 0, st->data, 6);
  473. memcpy(mac, st->data, sizeof(mac));
  474. if (ret > 0)
  475. deb_info("%s: mac is %02x:%02x:%02x:%02x:%02x:%02x\n",
  476. __func__, mac[0], mac[1], mac[2],
  477. mac[3], mac[4], mac[5]);
  478. return ret;
  479. }
  480. static int az6007_frontend_attach(struct dvb_usb_adapter *adap)
  481. {
  482. struct az6007_device_state *st = adap->dev->priv;
  483. deb_info("attaching demod drxk");
  484. adap->fe_adap[0].fe = dvb_attach(drxk_attach, &terratec_h7_drxk,
  485. &adap->dev->i2c_adap);
  486. if (!adap->fe_adap[0].fe)
  487. return -EINVAL;
  488. adap->fe_adap[0].fe->sec_priv = adap;
  489. st->gate_ctrl = adap->fe_adap[0].fe->ops.i2c_gate_ctrl;
  490. adap->fe_adap[0].fe->ops.i2c_gate_ctrl = drxk_gate_ctrl;
  491. az6007_ci_init(adap);
  492. return 0;
  493. }
  494. static int az6007_tuner_attach(struct dvb_usb_adapter *adap)
  495. {
  496. deb_info("attaching tuner mt2063");
  497. /* Attach mt2063 to DVB-C frontend */
  498. if (adap->fe_adap[0].fe->ops.i2c_gate_ctrl)
  499. adap->fe_adap[0].fe->ops.i2c_gate_ctrl(adap->fe_adap[0].fe, 1);
  500. if (!dvb_attach(mt2063_attach, adap->fe_adap[0].fe,
  501. &az6007_mt2063_config,
  502. &adap->dev->i2c_adap))
  503. return -EINVAL;
  504. if (adap->fe_adap[0].fe->ops.i2c_gate_ctrl)
  505. adap->fe_adap[0].fe->ops.i2c_gate_ctrl(adap->fe_adap[0].fe, 0);
  506. return 0;
  507. }
  508. int az6007_power_ctrl(struct dvb_usb_device *d, int onoff)
  509. {
  510. struct az6007_device_state *st = d->priv;
  511. int ret;
  512. deb_info("%s()\n", __func__);
  513. if (!st->warm) {
  514. mutex_init(&st->mutex);
  515. ret = az6007_write(d, AZ6007_POWER, 0, 2, NULL, 0);
  516. if (ret < 0)
  517. return ret;
  518. msleep(60);
  519. ret = az6007_write(d, AZ6007_POWER, 1, 4, NULL, 0);
  520. if (ret < 0)
  521. return ret;
  522. msleep(100);
  523. ret = az6007_write(d, AZ6007_POWER, 1, 3, NULL, 0);
  524. if (ret < 0)
  525. return ret;
  526. msleep(20);
  527. ret = az6007_write(d, AZ6007_POWER, 1, 4, NULL, 0);
  528. if (ret < 0)
  529. return ret;
  530. msleep(400);
  531. ret = az6007_write(d, FX2_SCON1, 0, 3, NULL, 0);
  532. if (ret < 0)
  533. return ret;
  534. msleep(150);
  535. ret = az6007_write(d, FX2_SCON1, 1, 3, NULL, 0);
  536. if (ret < 0)
  537. return ret;
  538. msleep(430);
  539. ret = az6007_write(d, AZ6007_POWER, 0, 0, NULL, 0);
  540. if (ret < 0)
  541. return ret;
  542. st->warm = true;
  543. return 0;
  544. }
  545. if (!onoff)
  546. return 0;
  547. az6007_write(d, AZ6007_POWER, 0, 0, NULL, 0);
  548. az6007_write(d, AZ6007_TS_THROUGH, 0, 0, NULL, 0);
  549. return 0;
  550. }
  551. /* I2C */
  552. static int az6007_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
  553. int num)
  554. {
  555. struct dvb_usb_device *d = i2c_get_adapdata(adap);
  556. struct az6007_device_state *st = d->priv;
  557. int i, j, len;
  558. int ret = 0;
  559. u16 index;
  560. u16 value;
  561. int length;
  562. u8 req, addr;
  563. if (mutex_lock_interruptible(&st->mutex) < 0)
  564. return -EAGAIN;
  565. for (i = 0; i < num; i++) {
  566. addr = msgs[i].addr << 1;
  567. if (((i + 1) < num)
  568. && (msgs[i].len == 1)
  569. && (!msgs[i].flags & I2C_M_RD)
  570. && (msgs[i + 1].flags & I2C_M_RD)
  571. && (msgs[i].addr == msgs[i + 1].addr)) {
  572. /*
  573. * A write + read xfer for the same address, where
  574. * the first xfer has just 1 byte length.
  575. * Need to join both into one operation
  576. */
  577. if (dvb_usb_az6007_debug & 2)
  578. printk(KERN_DEBUG
  579. "az6007 I2C xfer write+read addr=0x%x len=%d/%d: ",
  580. addr, msgs[i].len, msgs[i + 1].len);
  581. req = AZ6007_I2C_RD;
  582. index = msgs[i].buf[0];
  583. value = addr | (1 << 8);
  584. length = 6 + msgs[i + 1].len;
  585. len = msgs[i + 1].len;
  586. ret = __az6007_read(d->udev, req, value, index,
  587. st->data, length);
  588. if (ret >= len) {
  589. for (j = 0; j < len; j++) {
  590. msgs[i + 1].buf[j] = st->data[j + 5];
  591. if (dvb_usb_az6007_debug & 2)
  592. printk(KERN_CONT
  593. "0x%02x ",
  594. msgs[i + 1].buf[j]);
  595. }
  596. } else
  597. ret = -EIO;
  598. i++;
  599. } else if (!(msgs[i].flags & I2C_M_RD)) {
  600. /* write bytes */
  601. if (dvb_usb_az6007_debug & 2)
  602. printk(KERN_DEBUG
  603. "az6007 I2C xfer write addr=0x%x len=%d: ",
  604. addr, msgs[i].len);
  605. req = AZ6007_I2C_WR;
  606. index = msgs[i].buf[0];
  607. value = addr | (1 << 8);
  608. length = msgs[i].len - 1;
  609. len = msgs[i].len - 1;
  610. if (dvb_usb_az6007_debug & 2)
  611. printk(KERN_CONT "(0x%02x) ", msgs[i].buf[0]);
  612. for (j = 0; j < len; j++) {
  613. st->data[j] = msgs[i].buf[j + 1];
  614. if (dvb_usb_az6007_debug & 2)
  615. printk(KERN_CONT "0x%02x ",
  616. st->data[j]);
  617. }
  618. ret = __az6007_write(d->udev, req, value, index,
  619. st->data, length);
  620. } else {
  621. /* read bytes */
  622. if (dvb_usb_az6007_debug & 2)
  623. printk(KERN_DEBUG
  624. "az6007 I2C xfer read addr=0x%x len=%d: ",
  625. addr, msgs[i].len);
  626. req = AZ6007_I2C_RD;
  627. index = msgs[i].buf[0];
  628. value = addr;
  629. length = msgs[i].len + 6;
  630. len = msgs[i].len;
  631. ret = __az6007_read(d->udev, req, value, index,
  632. st->data, length);
  633. for (j = 0; j < len; j++) {
  634. msgs[i].buf[j] = st->data[j + 5];
  635. if (dvb_usb_az6007_debug & 2)
  636. printk(KERN_CONT
  637. "0x%02x ", st->data[j + 5]);
  638. }
  639. }
  640. if (dvb_usb_az6007_debug & 2)
  641. printk(KERN_CONT "\n");
  642. if (ret < 0)
  643. goto err;
  644. }
  645. err:
  646. mutex_unlock(&st->mutex);
  647. if (ret < 0) {
  648. info("%s ERROR: %i", __func__, ret);
  649. return ret;
  650. }
  651. return num;
  652. }
  653. static u32 az6007_i2c_func(struct i2c_adapter *adapter)
  654. {
  655. return I2C_FUNC_I2C;
  656. }
  657. static struct i2c_algorithm az6007_i2c_algo = {
  658. .master_xfer = az6007_i2c_xfer,
  659. .functionality = az6007_i2c_func,
  660. };
  661. int az6007_identify_state(struct usb_device *udev,
  662. struct dvb_usb_device_properties *props,
  663. struct dvb_usb_device_description **desc, int *cold)
  664. {
  665. int ret;
  666. u8 *mac;
  667. mac = kmalloc(6, GFP_ATOMIC);
  668. if (!mac)
  669. return -ENOMEM;
  670. /* Try to read the mac address */
  671. ret = __az6007_read(udev, AZ6007_READ_DATA, 6, 0, mac, 6);
  672. if (ret == 6)
  673. *cold = 0;
  674. else
  675. *cold = 1;
  676. kfree(mac);
  677. if (*cold) {
  678. __az6007_write(udev, 0x09, 1, 0, NULL, 0);
  679. __az6007_write(udev, 0x00, 0, 0, NULL, 0);
  680. __az6007_write(udev, 0x00, 0, 0, NULL, 0);
  681. }
  682. deb_info("Device is on %s state\n", *cold ? "warm" : "cold");
  683. return 0;
  684. }
  685. static struct dvb_usb_device_properties az6007_properties;
  686. static void az6007_usb_disconnect(struct usb_interface *intf)
  687. {
  688. struct dvb_usb_device *d = usb_get_intfdata(intf);
  689. az6007_ci_uninit(d);
  690. dvb_usb_device_exit(intf);
  691. }
  692. static int az6007_usb_probe(struct usb_interface *intf,
  693. const struct usb_device_id *id)
  694. {
  695. return dvb_usb_device_init(intf, &az6007_properties,
  696. THIS_MODULE, NULL, adapter_nr);
  697. }
  698. static struct usb_device_id az6007_usb_table[] = {
  699. {USB_DEVICE(USB_VID_AZUREWAVE, USB_PID_AZUREWAVE_6007)},
  700. {USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_H7)},
  701. {USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_H7_2)},
  702. {0},
  703. };
  704. MODULE_DEVICE_TABLE(usb, az6007_usb_table);
  705. static struct dvb_usb_device_properties az6007_properties = {
  706. .caps = DVB_USB_IS_AN_I2C_ADAPTER,
  707. .usb_ctrl = CYPRESS_FX2,
  708. .firmware = "dvb-usb-terratec-h7-az6007.fw",
  709. .no_reconnect = 1,
  710. .size_of_priv = sizeof(struct az6007_device_state),
  711. .identify_state = az6007_identify_state,
  712. .num_adapters = 1,
  713. .adapter = {
  714. {
  715. .num_frontends = 1,
  716. .fe = {{
  717. .streaming_ctrl = az6007_streaming_ctrl,
  718. .tuner_attach = az6007_tuner_attach,
  719. .frontend_attach = az6007_frontend_attach,
  720. /* parameter for the MPEG2-data transfer */
  721. .stream = {
  722. .type = USB_BULK,
  723. .count = 10,
  724. .endpoint = 0x02,
  725. .u = {
  726. .bulk = {
  727. .buffersize = 4096,
  728. }
  729. }
  730. },
  731. } }
  732. } },
  733. .power_ctrl = az6007_power_ctrl,
  734. .read_mac_address = az6007_read_mac_addr,
  735. .rc.core = {
  736. .rc_interval = 400,
  737. .rc_codes = RC_MAP_NEC_TERRATEC_CINERGY_XS,
  738. .module_name = "az6007",
  739. .rc_query = az6007_rc_query,
  740. .allowed_protos = RC_TYPE_NEC,
  741. },
  742. .i2c_algo = &az6007_i2c_algo,
  743. .num_device_descs = 2,
  744. .devices = {
  745. { .name = "AzureWave DTV StarBox DVB-T/C USB2.0 (az6007)",
  746. .cold_ids = { &az6007_usb_table[0], NULL },
  747. .warm_ids = { NULL },
  748. },
  749. { .name = "TerraTec DTV StarBox DVB-T/C USB2.0 (az6007)",
  750. .cold_ids = { &az6007_usb_table[1], &az6007_usb_table[2], NULL },
  751. .warm_ids = { NULL },
  752. },
  753. { NULL },
  754. }
  755. };
  756. /* usb specific object needed to register this driver with the usb subsystem */
  757. static struct usb_driver az6007_usb_driver = {
  758. .name = "dvb_usb_az6007",
  759. .probe = az6007_usb_probe,
  760. .disconnect = az6007_usb_disconnect,
  761. .id_table = az6007_usb_table,
  762. };
  763. /* module stuff */
  764. static int __init az6007_usb_module_init(void)
  765. {
  766. int result;
  767. deb_info("az6007 usb module init\n");
  768. result = usb_register(&az6007_usb_driver);
  769. if (result) {
  770. err("usb_register failed. (%d)", result);
  771. return result;
  772. }
  773. return 0;
  774. }
  775. static void __exit az6007_usb_module_exit(void)
  776. {
  777. /* deregister this driver from the USB subsystem */
  778. deb_info("az6007 usb module exit\n");
  779. usb_deregister(&az6007_usb_driver);
  780. }
  781. module_init(az6007_usb_module_init);
  782. module_exit(az6007_usb_module_exit);
  783. MODULE_AUTHOR("Henry Wang <Henry.wang@AzureWave.com>");
  784. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
  785. MODULE_DESCRIPTION("Driver for AzureWave 6007 DVB-C/T USB2.0 and clones");
  786. MODULE_VERSION("1.1");
  787. MODULE_LICENSE("GPL");