az6007.c 22 KB

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