af9005.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  1. /* DVB USB compliant Linux driver for the Afatech 9005
  2. * USB1.1 DVB-T receiver.
  3. *
  4. * Copyright (C) 2007 Luca Olivetti (luca@ventoso.org)
  5. *
  6. * Thanks to Afatech who kindly provided information.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. *
  22. * see Documentation/dvb/README.dvb-usb for more information
  23. */
  24. #include "af9005.h"
  25. /* debug */
  26. int dvb_usb_af9005_debug;
  27. module_param_named(debug, dvb_usb_af9005_debug, int, 0644);
  28. MODULE_PARM_DESC(debug,
  29. "set debugging level (1=info,xfer=2,rc=4,reg=8,i2c=16,fw=32 (or-able))."
  30. DVB_USB_DEBUG_STATUS);
  31. /* enable obnoxious led */
  32. bool dvb_usb_af9005_led = true;
  33. module_param_named(led, dvb_usb_af9005_led, bool, 0644);
  34. MODULE_PARM_DESC(led, "enable led (default: 1).");
  35. /* eeprom dump */
  36. static int dvb_usb_af9005_dump_eeprom;
  37. module_param_named(dump_eeprom, dvb_usb_af9005_dump_eeprom, int, 0);
  38. MODULE_PARM_DESC(dump_eeprom, "dump contents of the eeprom.");
  39. DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
  40. /* remote control decoder */
  41. static int (*rc_decode) (struct dvb_usb_device *d, u8 *data, int len,
  42. u32 *event, int *state);
  43. static void *rc_keys;
  44. static int *rc_keys_size;
  45. u8 regmask[8] = { 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff };
  46. struct af9005_device_state {
  47. u8 sequence;
  48. int led_state;
  49. };
  50. static int af9005_generic_read_write(struct dvb_usb_device *d, u16 reg,
  51. int readwrite, int type, u8 * values, int len)
  52. {
  53. struct af9005_device_state *st = d->priv;
  54. u8 obuf[16] = { 0 };
  55. u8 ibuf[17] = { 0 };
  56. u8 command;
  57. int i;
  58. int ret;
  59. if (len < 1) {
  60. err("generic read/write, less than 1 byte. Makes no sense.");
  61. return -EINVAL;
  62. }
  63. if (len > 8) {
  64. err("generic read/write, more than 8 bytes. Not supported.");
  65. return -EINVAL;
  66. }
  67. obuf[0] = 14; /* rest of buffer length low */
  68. obuf[1] = 0; /* rest of buffer length high */
  69. obuf[2] = AF9005_REGISTER_RW; /* register operation */
  70. obuf[3] = 12; /* rest of buffer length */
  71. obuf[4] = st->sequence++; /* sequence number */
  72. obuf[5] = (u8) (reg >> 8); /* register address */
  73. obuf[6] = (u8) (reg & 0xff);
  74. if (type == AF9005_OFDM_REG) {
  75. command = AF9005_CMD_OFDM_REG;
  76. } else {
  77. command = AF9005_CMD_TUNER;
  78. }
  79. if (len > 1)
  80. command |=
  81. AF9005_CMD_BURST | AF9005_CMD_AUTOINC | (len - 1) << 3;
  82. command |= readwrite;
  83. if (readwrite == AF9005_CMD_WRITE)
  84. for (i = 0; i < len; i++)
  85. obuf[8 + i] = values[i];
  86. else if (type == AF9005_TUNER_REG)
  87. /* read command for tuner, the first byte contains the i2c address */
  88. obuf[8] = values[0];
  89. obuf[7] = command;
  90. ret = dvb_usb_generic_rw(d, obuf, 16, ibuf, 17, 0);
  91. if (ret)
  92. return ret;
  93. /* sanity check */
  94. if (ibuf[2] != AF9005_REGISTER_RW_ACK) {
  95. err("generic read/write, wrong reply code.");
  96. return -EIO;
  97. }
  98. if (ibuf[3] != 0x0d) {
  99. err("generic read/write, wrong length in reply.");
  100. return -EIO;
  101. }
  102. if (ibuf[4] != obuf[4]) {
  103. err("generic read/write, wrong sequence in reply.");
  104. return -EIO;
  105. }
  106. /*
  107. Windows driver doesn't check these fields, in fact sometimes
  108. the register in the reply is different that what has been sent
  109. if (ibuf[5] != obuf[5] || ibuf[6] != obuf[6]) {
  110. err("generic read/write, wrong register in reply.");
  111. return -EIO;
  112. }
  113. if (ibuf[7] != command) {
  114. err("generic read/write wrong command in reply.");
  115. return -EIO;
  116. }
  117. */
  118. if (ibuf[16] != 0x01) {
  119. err("generic read/write wrong status code in reply.");
  120. return -EIO;
  121. }
  122. if (readwrite == AF9005_CMD_READ)
  123. for (i = 0; i < len; i++)
  124. values[i] = ibuf[8 + i];
  125. return 0;
  126. }
  127. int af9005_read_ofdm_register(struct dvb_usb_device *d, u16 reg, u8 * value)
  128. {
  129. int ret;
  130. deb_reg("read register %x ", reg);
  131. ret = af9005_generic_read_write(d, reg,
  132. AF9005_CMD_READ, AF9005_OFDM_REG,
  133. value, 1);
  134. if (ret)
  135. deb_reg("failed\n");
  136. else
  137. deb_reg("value %x\n", *value);
  138. return ret;
  139. }
  140. int af9005_read_ofdm_registers(struct dvb_usb_device *d, u16 reg,
  141. u8 * values, int len)
  142. {
  143. int ret;
  144. deb_reg("read %d registers %x ", len, reg);
  145. ret = af9005_generic_read_write(d, reg,
  146. AF9005_CMD_READ, AF9005_OFDM_REG,
  147. values, len);
  148. if (ret)
  149. deb_reg("failed\n");
  150. else
  151. debug_dump(values, len, deb_reg);
  152. return ret;
  153. }
  154. int af9005_write_ofdm_register(struct dvb_usb_device *d, u16 reg, u8 value)
  155. {
  156. int ret;
  157. u8 temp = value;
  158. deb_reg("write register %x value %x ", reg, value);
  159. ret = af9005_generic_read_write(d, reg,
  160. AF9005_CMD_WRITE, AF9005_OFDM_REG,
  161. &temp, 1);
  162. if (ret)
  163. deb_reg("failed\n");
  164. else
  165. deb_reg("ok\n");
  166. return ret;
  167. }
  168. int af9005_write_ofdm_registers(struct dvb_usb_device *d, u16 reg,
  169. u8 * values, int len)
  170. {
  171. int ret;
  172. deb_reg("write %d registers %x values ", len, reg);
  173. debug_dump(values, len, deb_reg);
  174. ret = af9005_generic_read_write(d, reg,
  175. AF9005_CMD_WRITE, AF9005_OFDM_REG,
  176. values, len);
  177. if (ret)
  178. deb_reg("failed\n");
  179. else
  180. deb_reg("ok\n");
  181. return ret;
  182. }
  183. int af9005_read_register_bits(struct dvb_usb_device *d, u16 reg, u8 pos,
  184. u8 len, u8 * value)
  185. {
  186. u8 temp;
  187. int ret;
  188. deb_reg("read bits %x %x %x", reg, pos, len);
  189. ret = af9005_read_ofdm_register(d, reg, &temp);
  190. if (ret) {
  191. deb_reg(" failed\n");
  192. return ret;
  193. }
  194. *value = (temp >> pos) & regmask[len - 1];
  195. deb_reg(" value %x\n", *value);
  196. return 0;
  197. }
  198. int af9005_write_register_bits(struct dvb_usb_device *d, u16 reg, u8 pos,
  199. u8 len, u8 value)
  200. {
  201. u8 temp, mask;
  202. int ret;
  203. deb_reg("write bits %x %x %x value %x\n", reg, pos, len, value);
  204. if (pos == 0 && len == 8)
  205. return af9005_write_ofdm_register(d, reg, value);
  206. ret = af9005_read_ofdm_register(d, reg, &temp);
  207. if (ret)
  208. return ret;
  209. mask = regmask[len - 1] << pos;
  210. temp = (temp & ~mask) | ((value << pos) & mask);
  211. return af9005_write_ofdm_register(d, reg, temp);
  212. }
  213. static int af9005_usb_read_tuner_registers(struct dvb_usb_device *d,
  214. u16 reg, u8 * values, int len)
  215. {
  216. return af9005_generic_read_write(d, reg,
  217. AF9005_CMD_READ, AF9005_TUNER_REG,
  218. values, len);
  219. }
  220. static int af9005_usb_write_tuner_registers(struct dvb_usb_device *d,
  221. u16 reg, u8 * values, int len)
  222. {
  223. return af9005_generic_read_write(d, reg,
  224. AF9005_CMD_WRITE,
  225. AF9005_TUNER_REG, values, len);
  226. }
  227. int af9005_write_tuner_registers(struct dvb_usb_device *d, u16 reg,
  228. u8 * values, int len)
  229. {
  230. /* don't let the name of this function mislead you: it's just used
  231. as an interface from the firmware to the i2c bus. The actual
  232. i2c addresses are contained in the data */
  233. int ret, i, done = 0, fail = 0;
  234. u8 temp;
  235. ret = af9005_usb_write_tuner_registers(d, reg, values, len);
  236. if (ret)
  237. return ret;
  238. if (reg != 0xffff) {
  239. /* check if write done (0xa40d bit 1) or fail (0xa40d bit 2) */
  240. for (i = 0; i < 200; i++) {
  241. ret =
  242. af9005_read_ofdm_register(d,
  243. xd_I2C_i2c_m_status_wdat_done,
  244. &temp);
  245. if (ret)
  246. return ret;
  247. done = temp & (regmask[i2c_m_status_wdat_done_len - 1]
  248. << i2c_m_status_wdat_done_pos);
  249. if (done)
  250. break;
  251. fail = temp & (regmask[i2c_m_status_wdat_fail_len - 1]
  252. << i2c_m_status_wdat_fail_pos);
  253. if (fail)
  254. break;
  255. msleep(50);
  256. }
  257. if (i == 200)
  258. return -ETIMEDOUT;
  259. if (fail) {
  260. /* clear write fail bit */
  261. af9005_write_register_bits(d,
  262. xd_I2C_i2c_m_status_wdat_fail,
  263. i2c_m_status_wdat_fail_pos,
  264. i2c_m_status_wdat_fail_len,
  265. 1);
  266. return -EIO;
  267. }
  268. /* clear write done bit */
  269. ret =
  270. af9005_write_register_bits(d,
  271. xd_I2C_i2c_m_status_wdat_fail,
  272. i2c_m_status_wdat_done_pos,
  273. i2c_m_status_wdat_done_len, 1);
  274. if (ret)
  275. return ret;
  276. }
  277. return 0;
  278. }
  279. int af9005_read_tuner_registers(struct dvb_usb_device *d, u16 reg, u8 addr,
  280. u8 * values, int len)
  281. {
  282. /* don't let the name of this function mislead you: it's just used
  283. as an interface from the firmware to the i2c bus. The actual
  284. i2c addresses are contained in the data */
  285. int ret, i;
  286. u8 temp, buf[2];
  287. buf[0] = addr; /* tuner i2c address */
  288. buf[1] = values[0]; /* tuner register */
  289. values[0] = addr + 0x01; /* i2c read address */
  290. if (reg == APO_REG_I2C_RW_SILICON_TUNER) {
  291. /* write tuner i2c address to tuner, 0c00c0 undocumented, found by sniffing */
  292. ret = af9005_write_tuner_registers(d, 0x00c0, buf, 2);
  293. if (ret)
  294. return ret;
  295. }
  296. /* send read command to ofsm */
  297. ret = af9005_usb_read_tuner_registers(d, reg, values, 1);
  298. if (ret)
  299. return ret;
  300. /* check if read done */
  301. for (i = 0; i < 200; i++) {
  302. ret = af9005_read_ofdm_register(d, 0xa408, &temp);
  303. if (ret)
  304. return ret;
  305. if (temp & 0x01)
  306. break;
  307. msleep(50);
  308. }
  309. if (i == 200)
  310. return -ETIMEDOUT;
  311. /* clear read done bit (by writing 1) */
  312. ret = af9005_write_ofdm_register(d, xd_I2C_i2c_m_data8, 1);
  313. if (ret)
  314. return ret;
  315. /* get read data (available from 0xa400) */
  316. for (i = 0; i < len; i++) {
  317. ret = af9005_read_ofdm_register(d, 0xa400 + i, &temp);
  318. if (ret)
  319. return ret;
  320. values[i] = temp;
  321. }
  322. return 0;
  323. }
  324. static int af9005_i2c_write(struct dvb_usb_device *d, u8 i2caddr, u8 reg,
  325. u8 * data, int len)
  326. {
  327. int ret, i;
  328. u8 buf[3];
  329. deb_i2c("i2c_write i2caddr %x, reg %x, len %d data ", i2caddr,
  330. reg, len);
  331. debug_dump(data, len, deb_i2c);
  332. for (i = 0; i < len; i++) {
  333. buf[0] = i2caddr;
  334. buf[1] = reg + (u8) i;
  335. buf[2] = data[i];
  336. ret =
  337. af9005_write_tuner_registers(d,
  338. APO_REG_I2C_RW_SILICON_TUNER,
  339. buf, 3);
  340. if (ret) {
  341. deb_i2c("i2c_write failed\n");
  342. return ret;
  343. }
  344. }
  345. deb_i2c("i2c_write ok\n");
  346. return 0;
  347. }
  348. static int af9005_i2c_read(struct dvb_usb_device *d, u8 i2caddr, u8 reg,
  349. u8 * data, int len)
  350. {
  351. int ret, i;
  352. u8 temp;
  353. deb_i2c("i2c_read i2caddr %x, reg %x, len %d\n ", i2caddr, reg, len);
  354. for (i = 0; i < len; i++) {
  355. temp = reg + i;
  356. ret =
  357. af9005_read_tuner_registers(d,
  358. APO_REG_I2C_RW_SILICON_TUNER,
  359. i2caddr, &temp, 1);
  360. if (ret) {
  361. deb_i2c("i2c_read failed\n");
  362. return ret;
  363. }
  364. data[i] = temp;
  365. }
  366. deb_i2c("i2c data read: ");
  367. debug_dump(data, len, deb_i2c);
  368. return 0;
  369. }
  370. static int af9005_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
  371. int num)
  372. {
  373. /* only implements what the mt2060 module does, don't know how
  374. to make it really generic */
  375. struct dvb_usb_device *d = i2c_get_adapdata(adap);
  376. int ret;
  377. u8 reg, addr;
  378. u8 *value;
  379. if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
  380. return -EAGAIN;
  381. if (num > 2)
  382. warn("more than 2 i2c messages at a time is not handled yet. TODO.");
  383. if (num == 2) {
  384. /* reads a single register */
  385. reg = *msg[0].buf;
  386. addr = msg[0].addr;
  387. value = msg[1].buf;
  388. ret = af9005_i2c_read(d, addr, reg, value, 1);
  389. if (ret == 0)
  390. ret = 2;
  391. } else {
  392. /* write one or more registers */
  393. reg = msg[0].buf[0];
  394. addr = msg[0].addr;
  395. value = &msg[0].buf[1];
  396. ret = af9005_i2c_write(d, addr, reg, value, msg[0].len - 1);
  397. if (ret == 0)
  398. ret = 1;
  399. }
  400. mutex_unlock(&d->i2c_mutex);
  401. return ret;
  402. }
  403. static u32 af9005_i2c_func(struct i2c_adapter *adapter)
  404. {
  405. return I2C_FUNC_I2C;
  406. }
  407. static struct i2c_algorithm af9005_i2c_algo = {
  408. .master_xfer = af9005_i2c_xfer,
  409. .functionality = af9005_i2c_func,
  410. };
  411. int af9005_send_command(struct dvb_usb_device *d, u8 command, u8 * wbuf,
  412. int wlen, u8 * rbuf, int rlen)
  413. {
  414. struct af9005_device_state *st = d->priv;
  415. int ret, i, packet_len;
  416. u8 buf[64];
  417. u8 ibuf[64];
  418. if (wlen < 0) {
  419. err("send command, wlen less than 0 bytes. Makes no sense.");
  420. return -EINVAL;
  421. }
  422. if (wlen > 54) {
  423. err("send command, wlen more than 54 bytes. Not supported.");
  424. return -EINVAL;
  425. }
  426. if (rlen > 54) {
  427. err("send command, rlen more than 54 bytes. Not supported.");
  428. return -EINVAL;
  429. }
  430. packet_len = wlen + 5;
  431. buf[0] = (u8) (packet_len & 0xff);
  432. buf[1] = (u8) ((packet_len & 0xff00) >> 8);
  433. buf[2] = 0x26; /* packet type */
  434. buf[3] = wlen + 3;
  435. buf[4] = st->sequence++;
  436. buf[5] = command;
  437. buf[6] = wlen;
  438. for (i = 0; i < wlen; i++)
  439. buf[7 + i] = wbuf[i];
  440. ret = dvb_usb_generic_rw(d, buf, wlen + 7, ibuf, rlen + 7, 0);
  441. if (ret)
  442. return ret;
  443. if (ibuf[2] != 0x27) {
  444. err("send command, wrong reply code.");
  445. return -EIO;
  446. }
  447. if (ibuf[4] != buf[4]) {
  448. err("send command, wrong sequence in reply.");
  449. return -EIO;
  450. }
  451. if (ibuf[5] != 0x01) {
  452. err("send command, wrong status code in reply.");
  453. return -EIO;
  454. }
  455. if (ibuf[6] != rlen) {
  456. err("send command, invalid data length in reply.");
  457. return -EIO;
  458. }
  459. for (i = 0; i < rlen; i++)
  460. rbuf[i] = ibuf[i + 7];
  461. return 0;
  462. }
  463. int af9005_read_eeprom(struct dvb_usb_device *d, u8 address, u8 * values,
  464. int len)
  465. {
  466. struct af9005_device_state *st = d->priv;
  467. u8 obuf[16], ibuf[14];
  468. int ret, i;
  469. memset(obuf, 0, sizeof(obuf));
  470. memset(ibuf, 0, sizeof(ibuf));
  471. obuf[0] = 14; /* length of rest of packet low */
  472. obuf[1] = 0; /* length of rest of packer high */
  473. obuf[2] = 0x2a; /* read/write eeprom */
  474. obuf[3] = 12; /* size */
  475. obuf[4] = st->sequence++;
  476. obuf[5] = 0; /* read */
  477. obuf[6] = len;
  478. obuf[7] = address;
  479. ret = dvb_usb_generic_rw(d, obuf, 16, ibuf, 14, 0);
  480. if (ret)
  481. return ret;
  482. if (ibuf[2] != 0x2b) {
  483. err("Read eeprom, invalid reply code");
  484. return -EIO;
  485. }
  486. if (ibuf[3] != 10) {
  487. err("Read eeprom, invalid reply length");
  488. return -EIO;
  489. }
  490. if (ibuf[4] != obuf[4]) {
  491. err("Read eeprom, wrong sequence in reply ");
  492. return -EIO;
  493. }
  494. if (ibuf[5] != 1) {
  495. err("Read eeprom, wrong status in reply ");
  496. return -EIO;
  497. }
  498. for (i = 0; i < len; i++) {
  499. values[i] = ibuf[6 + i];
  500. }
  501. return 0;
  502. }
  503. static int af9005_boot_packet(struct usb_device *udev, int type, u8 * reply)
  504. {
  505. u8 buf[FW_BULKOUT_SIZE + 2];
  506. u16 checksum;
  507. int act_len, i, ret;
  508. memset(buf, 0, sizeof(buf));
  509. buf[0] = (u8) (FW_BULKOUT_SIZE & 0xff);
  510. buf[1] = (u8) ((FW_BULKOUT_SIZE >> 8) & 0xff);
  511. switch (type) {
  512. case FW_CONFIG:
  513. buf[2] = 0x11;
  514. buf[3] = 0x04;
  515. buf[4] = 0x00; /* sequence number, original driver doesn't increment it here */
  516. buf[5] = 0x03;
  517. checksum = buf[4] + buf[5];
  518. buf[6] = (u8) ((checksum >> 8) & 0xff);
  519. buf[7] = (u8) (checksum & 0xff);
  520. break;
  521. case FW_CONFIRM:
  522. buf[2] = 0x11;
  523. buf[3] = 0x04;
  524. buf[4] = 0x00; /* sequence number, original driver doesn't increment it here */
  525. buf[5] = 0x01;
  526. checksum = buf[4] + buf[5];
  527. buf[6] = (u8) ((checksum >> 8) & 0xff);
  528. buf[7] = (u8) (checksum & 0xff);
  529. break;
  530. case FW_BOOT:
  531. buf[2] = 0x10;
  532. buf[3] = 0x08;
  533. buf[4] = 0x00; /* sequence number, original driver doesn't increment it here */
  534. buf[5] = 0x97;
  535. buf[6] = 0xaa;
  536. buf[7] = 0x55;
  537. buf[8] = 0xa5;
  538. buf[9] = 0x5a;
  539. checksum = 0;
  540. for (i = 4; i <= 9; i++)
  541. checksum += buf[i];
  542. buf[10] = (u8) ((checksum >> 8) & 0xff);
  543. buf[11] = (u8) (checksum & 0xff);
  544. break;
  545. default:
  546. err("boot packet invalid boot packet type");
  547. return -EINVAL;
  548. }
  549. deb_fw(">>> ");
  550. debug_dump(buf, FW_BULKOUT_SIZE + 2, deb_fw);
  551. ret = usb_bulk_msg(udev,
  552. usb_sndbulkpipe(udev, 0x02),
  553. buf, FW_BULKOUT_SIZE + 2, &act_len, 2000);
  554. if (ret)
  555. err("boot packet bulk message failed: %d (%d/%d)", ret,
  556. FW_BULKOUT_SIZE + 2, act_len);
  557. else
  558. ret = act_len != FW_BULKOUT_SIZE + 2 ? -1 : 0;
  559. if (ret)
  560. return ret;
  561. memset(buf, 0, 9);
  562. ret = usb_bulk_msg(udev,
  563. usb_rcvbulkpipe(udev, 0x01), buf, 9, &act_len, 2000);
  564. if (ret) {
  565. err("boot packet recv bulk message failed: %d", ret);
  566. return ret;
  567. }
  568. deb_fw("<<< ");
  569. debug_dump(buf, act_len, deb_fw);
  570. checksum = 0;
  571. switch (type) {
  572. case FW_CONFIG:
  573. if (buf[2] != 0x11) {
  574. err("boot bad config header.");
  575. return -EIO;
  576. }
  577. if (buf[3] != 0x05) {
  578. err("boot bad config size.");
  579. return -EIO;
  580. }
  581. if (buf[4] != 0x00) {
  582. err("boot bad config sequence.");
  583. return -EIO;
  584. }
  585. if (buf[5] != 0x04) {
  586. err("boot bad config subtype.");
  587. return -EIO;
  588. }
  589. for (i = 4; i <= 6; i++)
  590. checksum += buf[i];
  591. if (buf[7] * 256 + buf[8] != checksum) {
  592. err("boot bad config checksum.");
  593. return -EIO;
  594. }
  595. *reply = buf[6];
  596. break;
  597. case FW_CONFIRM:
  598. if (buf[2] != 0x11) {
  599. err("boot bad confirm header.");
  600. return -EIO;
  601. }
  602. if (buf[3] != 0x05) {
  603. err("boot bad confirm size.");
  604. return -EIO;
  605. }
  606. if (buf[4] != 0x00) {
  607. err("boot bad confirm sequence.");
  608. return -EIO;
  609. }
  610. if (buf[5] != 0x02) {
  611. err("boot bad confirm subtype.");
  612. return -EIO;
  613. }
  614. for (i = 4; i <= 6; i++)
  615. checksum += buf[i];
  616. if (buf[7] * 256 + buf[8] != checksum) {
  617. err("boot bad confirm checksum.");
  618. return -EIO;
  619. }
  620. *reply = buf[6];
  621. break;
  622. case FW_BOOT:
  623. if (buf[2] != 0x10) {
  624. err("boot bad boot header.");
  625. return -EIO;
  626. }
  627. if (buf[3] != 0x05) {
  628. err("boot bad boot size.");
  629. return -EIO;
  630. }
  631. if (buf[4] != 0x00) {
  632. err("boot bad boot sequence.");
  633. return -EIO;
  634. }
  635. if (buf[5] != 0x01) {
  636. err("boot bad boot pattern 01.");
  637. return -EIO;
  638. }
  639. if (buf[6] != 0x10) {
  640. err("boot bad boot pattern 10.");
  641. return -EIO;
  642. }
  643. for (i = 4; i <= 6; i++)
  644. checksum += buf[i];
  645. if (buf[7] * 256 + buf[8] != checksum) {
  646. err("boot bad boot checksum.");
  647. return -EIO;
  648. }
  649. break;
  650. }
  651. return 0;
  652. }
  653. static int af9005_download_firmware(struct usb_device *udev, const struct firmware *fw)
  654. {
  655. int i, packets, ret, act_len;
  656. u8 buf[FW_BULKOUT_SIZE + 2];
  657. u8 reply;
  658. ret = af9005_boot_packet(udev, FW_CONFIG, &reply);
  659. if (ret)
  660. return ret;
  661. if (reply != 0x01) {
  662. err("before downloading firmware, FW_CONFIG expected 0x01, received 0x%x", reply);
  663. return -EIO;
  664. }
  665. packets = fw->size / FW_BULKOUT_SIZE;
  666. buf[0] = (u8) (FW_BULKOUT_SIZE & 0xff);
  667. buf[1] = (u8) ((FW_BULKOUT_SIZE >> 8) & 0xff);
  668. for (i = 0; i < packets; i++) {
  669. memcpy(&buf[2], fw->data + i * FW_BULKOUT_SIZE,
  670. FW_BULKOUT_SIZE);
  671. deb_fw(">>> ");
  672. debug_dump(buf, FW_BULKOUT_SIZE + 2, deb_fw);
  673. ret = usb_bulk_msg(udev,
  674. usb_sndbulkpipe(udev, 0x02),
  675. buf, FW_BULKOUT_SIZE + 2, &act_len, 1000);
  676. if (ret) {
  677. err("firmware download failed at packet %d with code %d", i, ret);
  678. return ret;
  679. }
  680. }
  681. ret = af9005_boot_packet(udev, FW_CONFIRM, &reply);
  682. if (ret)
  683. return ret;
  684. if (reply != (u8) (packets & 0xff)) {
  685. err("after downloading firmware, FW_CONFIRM expected 0x%x, received 0x%x", packets & 0xff, reply);
  686. return -EIO;
  687. }
  688. ret = af9005_boot_packet(udev, FW_BOOT, &reply);
  689. if (ret)
  690. return ret;
  691. ret = af9005_boot_packet(udev, FW_CONFIG, &reply);
  692. if (ret)
  693. return ret;
  694. if (reply != 0x02) {
  695. err("after downloading firmware, FW_CONFIG expected 0x02, received 0x%x", reply);
  696. return -EIO;
  697. }
  698. return 0;
  699. }
  700. int af9005_led_control(struct dvb_usb_device *d, int onoff)
  701. {
  702. struct af9005_device_state *st = d->priv;
  703. int temp, ret;
  704. if (onoff && dvb_usb_af9005_led)
  705. temp = 1;
  706. else
  707. temp = 0;
  708. if (st->led_state != temp) {
  709. ret =
  710. af9005_write_register_bits(d, xd_p_reg_top_locken1,
  711. reg_top_locken1_pos,
  712. reg_top_locken1_len, temp);
  713. if (ret)
  714. return ret;
  715. ret =
  716. af9005_write_register_bits(d, xd_p_reg_top_lock1,
  717. reg_top_lock1_pos,
  718. reg_top_lock1_len, temp);
  719. if (ret)
  720. return ret;
  721. st->led_state = temp;
  722. }
  723. return 0;
  724. }
  725. static int af9005_frontend_attach(struct dvb_usb_adapter *adap)
  726. {
  727. u8 buf[8];
  728. int i;
  729. /* without these calls the first commands after downloading
  730. the firmware fail. I put these calls here to simulate
  731. what it is done in dvb-usb-init.c.
  732. */
  733. struct usb_device *udev = adap->dev->udev;
  734. usb_clear_halt(udev, usb_sndbulkpipe(udev, 2));
  735. usb_clear_halt(udev, usb_rcvbulkpipe(udev, 1));
  736. if (dvb_usb_af9005_dump_eeprom) {
  737. printk("EEPROM DUMP\n");
  738. for (i = 0; i < 255; i += 8) {
  739. af9005_read_eeprom(adap->dev, i, buf, 8);
  740. printk("ADDR %x ", i);
  741. debug_dump(buf, 8, printk);
  742. }
  743. }
  744. adap->fe_adap[0].fe = af9005_fe_attach(adap->dev);
  745. return 0;
  746. }
  747. static int af9005_rc_query(struct dvb_usb_device *d, u32 * event, int *state)
  748. {
  749. struct af9005_device_state *st = d->priv;
  750. int ret, len;
  751. u8 obuf[5];
  752. u8 ibuf[256];
  753. *state = REMOTE_NO_KEY_PRESSED;
  754. if (rc_decode == NULL) {
  755. /* it shouldn't never come here */
  756. return 0;
  757. }
  758. /* deb_info("rc_query\n"); */
  759. obuf[0] = 3; /* rest of packet length low */
  760. obuf[1] = 0; /* rest of packet lentgh high */
  761. obuf[2] = 0x40; /* read remote */
  762. obuf[3] = 1; /* rest of packet length */
  763. obuf[4] = st->sequence++; /* sequence number */
  764. ret = dvb_usb_generic_rw(d, obuf, 5, ibuf, 256, 0);
  765. if (ret) {
  766. err("rc query failed");
  767. return ret;
  768. }
  769. if (ibuf[2] != 0x41) {
  770. err("rc query bad header.");
  771. return -EIO;
  772. }
  773. if (ibuf[4] != obuf[4]) {
  774. err("rc query bad sequence.");
  775. return -EIO;
  776. }
  777. len = ibuf[5];
  778. if (len > 246) {
  779. err("rc query invalid length");
  780. return -EIO;
  781. }
  782. if (len > 0) {
  783. deb_rc("rc data (%d) ", len);
  784. debug_dump((ibuf + 6), len, deb_rc);
  785. ret = rc_decode(d, &ibuf[6], len, event, state);
  786. if (ret) {
  787. err("rc_decode failed");
  788. return ret;
  789. } else {
  790. deb_rc("rc_decode state %x event %x\n", *state, *event);
  791. if (*state == REMOTE_KEY_REPEAT)
  792. *event = d->last_event;
  793. }
  794. }
  795. return 0;
  796. }
  797. static int af9005_power_ctrl(struct dvb_usb_device *d, int onoff)
  798. {
  799. return 0;
  800. }
  801. static int af9005_pid_filter_control(struct dvb_usb_adapter *adap, int onoff)
  802. {
  803. int ret;
  804. deb_info("pid filter control onoff %d\n", onoff);
  805. if (onoff) {
  806. ret =
  807. af9005_write_ofdm_register(adap->dev, XD_MP2IF_DMX_CTRL, 1);
  808. if (ret)
  809. return ret;
  810. ret =
  811. af9005_write_register_bits(adap->dev,
  812. XD_MP2IF_DMX_CTRL, 1, 1, 1);
  813. if (ret)
  814. return ret;
  815. ret =
  816. af9005_write_ofdm_register(adap->dev, XD_MP2IF_DMX_CTRL, 1);
  817. } else
  818. ret =
  819. af9005_write_ofdm_register(adap->dev, XD_MP2IF_DMX_CTRL, 0);
  820. if (ret)
  821. return ret;
  822. deb_info("pid filter control ok\n");
  823. return 0;
  824. }
  825. static int af9005_pid_filter(struct dvb_usb_adapter *adap, int index,
  826. u16 pid, int onoff)
  827. {
  828. u8 cmd = index & 0x1f;
  829. int ret;
  830. deb_info("set pid filter, index %d, pid %x, onoff %d\n", index,
  831. pid, onoff);
  832. if (onoff) {
  833. /* cannot use it as pid_filter_ctrl since it has to be done
  834. before setting the first pid */
  835. if (adap->feedcount == 1) {
  836. deb_info("first pid set, enable pid table\n");
  837. ret = af9005_pid_filter_control(adap, onoff);
  838. if (ret)
  839. return ret;
  840. }
  841. ret =
  842. af9005_write_ofdm_register(adap->dev,
  843. XD_MP2IF_PID_DATA_L,
  844. (u8) (pid & 0xff));
  845. if (ret)
  846. return ret;
  847. ret =
  848. af9005_write_ofdm_register(adap->dev,
  849. XD_MP2IF_PID_DATA_H,
  850. (u8) (pid >> 8));
  851. if (ret)
  852. return ret;
  853. cmd |= 0x20 | 0x40;
  854. } else {
  855. if (adap->feedcount == 0) {
  856. deb_info("last pid unset, disable pid table\n");
  857. ret = af9005_pid_filter_control(adap, onoff);
  858. if (ret)
  859. return ret;
  860. }
  861. }
  862. ret = af9005_write_ofdm_register(adap->dev, XD_MP2IF_PID_IDX, cmd);
  863. if (ret)
  864. return ret;
  865. deb_info("set pid ok\n");
  866. return 0;
  867. }
  868. static int af9005_identify_state(struct usb_device *udev,
  869. struct dvb_usb_device_properties *props,
  870. struct dvb_usb_device_description **desc,
  871. int *cold)
  872. {
  873. int ret;
  874. u8 reply;
  875. ret = af9005_boot_packet(udev, FW_CONFIG, &reply);
  876. if (ret)
  877. return ret;
  878. deb_info("result of FW_CONFIG in identify state %d\n", reply);
  879. if (reply == 0x01)
  880. *cold = 1;
  881. else if (reply == 0x02)
  882. *cold = 0;
  883. else
  884. return -EIO;
  885. deb_info("Identify state cold = %d\n", *cold);
  886. return 0;
  887. }
  888. static struct dvb_usb_device_properties af9005_properties;
  889. static int af9005_usb_probe(struct usb_interface *intf,
  890. const struct usb_device_id *id)
  891. {
  892. return dvb_usb_device_init(intf, &af9005_properties,
  893. THIS_MODULE, NULL, adapter_nr);
  894. }
  895. enum af9005_usb_table_entry {
  896. AFATECH_AF9005,
  897. TERRATEC_AF9005,
  898. ANSONIC_AF9005,
  899. };
  900. static struct usb_device_id af9005_usb_table[] = {
  901. [AFATECH_AF9005] = {USB_DEVICE(USB_VID_AFATECH,
  902. USB_PID_AFATECH_AF9005)},
  903. [TERRATEC_AF9005] = {USB_DEVICE(USB_VID_TERRATEC,
  904. USB_PID_TERRATEC_CINERGY_T_USB_XE)},
  905. [ANSONIC_AF9005] = {USB_DEVICE(USB_VID_ANSONIC,
  906. USB_PID_ANSONIC_DVBT_USB)},
  907. { }
  908. };
  909. MODULE_DEVICE_TABLE(usb, af9005_usb_table);
  910. static struct dvb_usb_device_properties af9005_properties = {
  911. .caps = DVB_USB_IS_AN_I2C_ADAPTER,
  912. .usb_ctrl = DEVICE_SPECIFIC,
  913. .firmware = "af9005.fw",
  914. .download_firmware = af9005_download_firmware,
  915. .no_reconnect = 1,
  916. .size_of_priv = sizeof(struct af9005_device_state),
  917. .num_adapters = 1,
  918. .adapter = {
  919. {
  920. .num_frontends = 1,
  921. .fe = {{
  922. .caps =
  923. DVB_USB_ADAP_HAS_PID_FILTER |
  924. DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
  925. .pid_filter_count = 32,
  926. .pid_filter = af9005_pid_filter,
  927. /* .pid_filter_ctrl = af9005_pid_filter_control, */
  928. .frontend_attach = af9005_frontend_attach,
  929. /* .tuner_attach = af9005_tuner_attach, */
  930. /* parameter for the MPEG2-data transfer */
  931. .stream = {
  932. .type = USB_BULK,
  933. .count = 10,
  934. .endpoint = 0x04,
  935. .u = {
  936. .bulk = {
  937. .buffersize = 4096, /* actual size seen is 3948 */
  938. }
  939. }
  940. },
  941. }},
  942. }
  943. },
  944. .power_ctrl = af9005_power_ctrl,
  945. .identify_state = af9005_identify_state,
  946. .i2c_algo = &af9005_i2c_algo,
  947. .rc.legacy = {
  948. .rc_interval = 200,
  949. .rc_map_table = NULL,
  950. .rc_map_size = 0,
  951. .rc_query = af9005_rc_query,
  952. },
  953. .generic_bulk_ctrl_endpoint = 2,
  954. .generic_bulk_ctrl_endpoint_response = 1,
  955. .num_device_descs = 3,
  956. .devices = {
  957. {.name = "Afatech DVB-T USB1.1 stick",
  958. .cold_ids = {&af9005_usb_table[AFATECH_AF9005], NULL},
  959. .warm_ids = {NULL},
  960. },
  961. {.name = "TerraTec Cinergy T USB XE",
  962. .cold_ids = {&af9005_usb_table[TERRATEC_AF9005], NULL},
  963. .warm_ids = {NULL},
  964. },
  965. {.name = "Ansonic DVB-T USB1.1 stick",
  966. .cold_ids = {&af9005_usb_table[ANSONIC_AF9005], NULL},
  967. .warm_ids = {NULL},
  968. },
  969. {NULL},
  970. }
  971. };
  972. /* usb specific object needed to register this driver with the usb subsystem */
  973. static struct usb_driver af9005_usb_driver = {
  974. .name = "dvb_usb_af9005",
  975. .probe = af9005_usb_probe,
  976. .disconnect = dvb_usb_device_exit,
  977. .id_table = af9005_usb_table,
  978. };
  979. /* module stuff */
  980. static int __init af9005_usb_module_init(void)
  981. {
  982. int result;
  983. if ((result = usb_register(&af9005_usb_driver))) {
  984. err("usb_register failed. (%d)", result);
  985. return result;
  986. }
  987. #if IS_MODULE(CONFIG_DVB_USB_AF9005) || defined(CONFIG_DVB_USB_AF9005_REMOTE)
  988. /* FIXME: convert to todays kernel IR infrastructure */
  989. rc_decode = symbol_request(af9005_rc_decode);
  990. rc_keys = symbol_request(rc_map_af9005_table);
  991. rc_keys_size = symbol_request(rc_map_af9005_table_size);
  992. #endif
  993. if (rc_decode == NULL || rc_keys == NULL || rc_keys_size == NULL) {
  994. err("af9005_rc_decode function not found, disabling remote");
  995. af9005_properties.rc.legacy.rc_query = NULL;
  996. } else {
  997. af9005_properties.rc.legacy.rc_map_table = rc_keys;
  998. af9005_properties.rc.legacy.rc_map_size = *rc_keys_size;
  999. }
  1000. return 0;
  1001. }
  1002. static void __exit af9005_usb_module_exit(void)
  1003. {
  1004. /* release rc decode symbols */
  1005. if (rc_decode != NULL)
  1006. symbol_put(af9005_rc_decode);
  1007. if (rc_keys != NULL)
  1008. symbol_put(rc_map_af9005_table);
  1009. if (rc_keys_size != NULL)
  1010. symbol_put(rc_map_af9005_table_size);
  1011. /* deregister this driver from the USB subsystem */
  1012. usb_deregister(&af9005_usb_driver);
  1013. }
  1014. module_init(af9005_usb_module_init);
  1015. module_exit(af9005_usb_module_exit);
  1016. MODULE_AUTHOR("Luca Olivetti <luca@ventoso.org>");
  1017. MODULE_DESCRIPTION("Driver for Afatech 9005 DVB-T USB1.1 stick");
  1018. MODULE_VERSION("1.0");
  1019. MODULE_LICENSE("GPL");