af9015.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575
  1. /*
  2. * DVB USB Linux driver for Afatech AF9015 DVB-T USB2.0 receiver
  3. *
  4. * Copyright (C) 2007 Antti Palosaari <crope@iki.fi>
  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. */
  23. #include "af9015.h"
  24. #include "af9013.h"
  25. #include "mt2060.h"
  26. #include "qt1010.h"
  27. #include "tda18271.h"
  28. #include "mxl5005s.h"
  29. #include "mc44s803.h"
  30. static int dvb_usb_af9015_debug;
  31. module_param_named(debug, dvb_usb_af9015_debug, int, 0644);
  32. MODULE_PARM_DESC(debug, "set debugging level" DVB_USB_DEBUG_STATUS);
  33. static int dvb_usb_af9015_remote;
  34. module_param_named(remote, dvb_usb_af9015_remote, int, 0644);
  35. MODULE_PARM_DESC(remote, "select remote");
  36. DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
  37. static DEFINE_MUTEX(af9015_usb_mutex);
  38. static struct af9015_config af9015_config;
  39. static struct dvb_usb_device_properties af9015_properties[2];
  40. static int af9015_properties_count = ARRAY_SIZE(af9015_properties);
  41. static struct af9013_config af9015_af9013_config[] = {
  42. {
  43. .demod_address = AF9015_I2C_DEMOD,
  44. .output_mode = AF9013_OUTPUT_MODE_USB,
  45. .api_version = { 0, 1, 9, 0 },
  46. .gpio[0] = AF9013_GPIO_HI,
  47. .gpio[3] = AF9013_GPIO_TUNER_ON,
  48. }, {
  49. .output_mode = AF9013_OUTPUT_MODE_SERIAL,
  50. .api_version = { 0, 1, 9, 0 },
  51. .gpio[0] = AF9013_GPIO_TUNER_ON,
  52. .gpio[1] = AF9013_GPIO_LO,
  53. }
  54. };
  55. static int af9015_rw_udev(struct usb_device *udev, struct req_t *req)
  56. {
  57. int act_len, ret;
  58. u8 buf[64];
  59. u8 write = 1;
  60. u8 msg_len = 8;
  61. static u8 seq; /* packet sequence number */
  62. if (mutex_lock_interruptible(&af9015_usb_mutex) < 0)
  63. return -EAGAIN;
  64. buf[0] = req->cmd;
  65. buf[1] = seq++;
  66. buf[2] = req->i2c_addr;
  67. buf[3] = req->addr >> 8;
  68. buf[4] = req->addr & 0xff;
  69. buf[5] = req->mbox;
  70. buf[6] = req->addr_len;
  71. buf[7] = req->data_len;
  72. switch (req->cmd) {
  73. case GET_CONFIG:
  74. case BOOT:
  75. case READ_MEMORY:
  76. case RECONNECT_USB:
  77. case GET_IR_CODE:
  78. write = 0;
  79. break;
  80. case READ_I2C:
  81. write = 0;
  82. buf[2] |= 0x01; /* set I2C direction */
  83. case WRITE_I2C:
  84. buf[0] = READ_WRITE_I2C;
  85. break;
  86. case WRITE_MEMORY:
  87. if (((req->addr & 0xff00) == 0xff00) ||
  88. ((req->addr & 0xae00) == 0xae00))
  89. buf[0] = WRITE_VIRTUAL_MEMORY;
  90. case WRITE_VIRTUAL_MEMORY:
  91. case COPY_FIRMWARE:
  92. case DOWNLOAD_FIRMWARE:
  93. break;
  94. default:
  95. err("unknown command:%d", req->cmd);
  96. ret = -1;
  97. goto error_unlock;
  98. }
  99. /* write requested */
  100. if (write) {
  101. memcpy(&buf[8], req->data, req->data_len);
  102. msg_len += req->data_len;
  103. }
  104. deb_xfer(">>> ");
  105. debug_dump(buf, msg_len, deb_xfer);
  106. /* send req */
  107. ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, 0x02), buf, msg_len,
  108. &act_len, AF9015_USB_TIMEOUT);
  109. if (ret)
  110. err("bulk message failed:%d (%d/%d)", ret, msg_len, act_len);
  111. else
  112. if (act_len != msg_len)
  113. ret = -1; /* all data is not send */
  114. if (ret)
  115. goto error_unlock;
  116. /* no ack for those packets */
  117. if (req->cmd == DOWNLOAD_FIRMWARE || req->cmd == RECONNECT_USB)
  118. goto exit_unlock;
  119. /* receive ack and data if read req */
  120. msg_len = 1 + 1 + req->data_len; /* seq + status + data len */
  121. ret = usb_bulk_msg(udev, usb_rcvbulkpipe(udev, 0x81), buf, msg_len,
  122. &act_len, AF9015_USB_TIMEOUT);
  123. if (ret) {
  124. err("recv bulk message failed:%d", ret);
  125. ret = -1;
  126. goto error_unlock;
  127. }
  128. deb_xfer("<<< ");
  129. debug_dump(buf, act_len, deb_xfer);
  130. /* remote controller query status is 1 if remote code is not received */
  131. if (req->cmd == GET_IR_CODE && buf[1] == 1) {
  132. buf[1] = 0; /* clear command "error" status */
  133. memset(&buf[2], 0, req->data_len);
  134. buf[3] = 1; /* no remote code received mark */
  135. }
  136. /* check status */
  137. if (buf[1]) {
  138. err("command failed:%d", buf[1]);
  139. ret = -1;
  140. goto error_unlock;
  141. }
  142. /* read request, copy returned data to return buf */
  143. if (!write)
  144. memcpy(req->data, &buf[2], req->data_len);
  145. error_unlock:
  146. exit_unlock:
  147. mutex_unlock(&af9015_usb_mutex);
  148. return ret;
  149. }
  150. static int af9015_ctrl_msg(struct dvb_usb_device *d, struct req_t *req)
  151. {
  152. return af9015_rw_udev(d->udev, req);
  153. }
  154. static int af9015_write_regs(struct dvb_usb_device *d, u16 addr, u8 *val,
  155. u8 len)
  156. {
  157. struct req_t req = {WRITE_MEMORY, AF9015_I2C_DEMOD, addr, 0, 0, len,
  158. val};
  159. return af9015_ctrl_msg(d, &req);
  160. }
  161. static int af9015_write_reg(struct dvb_usb_device *d, u16 addr, u8 val)
  162. {
  163. return af9015_write_regs(d, addr, &val, 1);
  164. }
  165. static int af9015_read_reg(struct dvb_usb_device *d, u16 addr, u8 *val)
  166. {
  167. struct req_t req = {READ_MEMORY, AF9015_I2C_DEMOD, addr, 0, 0, 1, val};
  168. return af9015_ctrl_msg(d, &req);
  169. }
  170. static int af9015_write_reg_i2c(struct dvb_usb_device *d, u8 addr, u16 reg,
  171. u8 val)
  172. {
  173. struct req_t req = {WRITE_I2C, addr, reg, 1, 1, 1, &val};
  174. if (addr == af9015_af9013_config[0].demod_address ||
  175. addr == af9015_af9013_config[1].demod_address)
  176. req.addr_len = 3;
  177. return af9015_ctrl_msg(d, &req);
  178. }
  179. static int af9015_read_reg_i2c(struct dvb_usb_device *d, u8 addr, u16 reg,
  180. u8 *val)
  181. {
  182. struct req_t req = {READ_I2C, addr, reg, 0, 1, 1, val};
  183. if (addr == af9015_af9013_config[0].demod_address ||
  184. addr == af9015_af9013_config[1].demod_address)
  185. req.addr_len = 3;
  186. return af9015_ctrl_msg(d, &req);
  187. }
  188. static int af9015_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
  189. int num)
  190. {
  191. struct dvb_usb_device *d = i2c_get_adapdata(adap);
  192. int ret = 0, i = 0;
  193. u16 addr;
  194. u8 mbox, addr_len;
  195. struct req_t req;
  196. /* TODO: implement bus lock
  197. The bus lock is needed because there is two tuners both using same I2C-address.
  198. Due to that the only way to select correct tuner is use demodulator I2C-gate.
  199. ................................................
  200. . AF9015 includes integrated AF9013 demodulator.
  201. . ____________ ____________ . ____________
  202. .| uC | | demod | . | tuner |
  203. .|------------| |------------| . |------------|
  204. .| AF9015 | | AF9013/5 | . | MXL5003 |
  205. .| |--+----I2C-------|-----/ -----|-.-----I2C-------| |
  206. .| | | | addr 0x38 | . | addr 0xc6 |
  207. .|____________| | |____________| . |____________|
  208. .................|..............................
  209. | ____________ ____________
  210. | | demod | | tuner |
  211. | |------------| |------------|
  212. | | AF9013 | | MXL5003 |
  213. +----I2C-------|-----/ -----|-------I2C-------| |
  214. | addr 0x3a | | addr 0xc6 |
  215. |____________| |____________|
  216. */
  217. if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
  218. return -EAGAIN;
  219. while (i < num) {
  220. if (msg[i].addr == af9015_af9013_config[0].demod_address ||
  221. msg[i].addr == af9015_af9013_config[1].demod_address) {
  222. addr = msg[i].buf[0] << 8;
  223. addr += msg[i].buf[1];
  224. mbox = msg[i].buf[2];
  225. addr_len = 3;
  226. } else {
  227. addr = msg[i].buf[0];
  228. addr_len = 1;
  229. mbox = 0;
  230. }
  231. if (num > i + 1 && (msg[i+1].flags & I2C_M_RD)) {
  232. if (msg[i].addr ==
  233. af9015_af9013_config[0].demod_address)
  234. req.cmd = READ_MEMORY;
  235. else
  236. req.cmd = READ_I2C;
  237. req.i2c_addr = msg[i].addr;
  238. req.addr = addr;
  239. req.mbox = mbox;
  240. req.addr_len = addr_len;
  241. req.data_len = msg[i+1].len;
  242. req.data = &msg[i+1].buf[0];
  243. ret = af9015_ctrl_msg(d, &req);
  244. i += 2;
  245. } else if (msg[i].flags & I2C_M_RD) {
  246. ret = -EINVAL;
  247. if (msg[i].addr ==
  248. af9015_af9013_config[0].demod_address)
  249. goto error;
  250. else
  251. req.cmd = READ_I2C;
  252. req.i2c_addr = msg[i].addr;
  253. req.addr = addr;
  254. req.mbox = mbox;
  255. req.addr_len = addr_len;
  256. req.data_len = msg[i].len;
  257. req.data = &msg[i].buf[0];
  258. ret = af9015_ctrl_msg(d, &req);
  259. i += 1;
  260. } else {
  261. if (msg[i].addr ==
  262. af9015_af9013_config[0].demod_address)
  263. req.cmd = WRITE_MEMORY;
  264. else
  265. req.cmd = WRITE_I2C;
  266. req.i2c_addr = msg[i].addr;
  267. req.addr = addr;
  268. req.mbox = mbox;
  269. req.addr_len = addr_len;
  270. req.data_len = msg[i].len-addr_len;
  271. req.data = &msg[i].buf[addr_len];
  272. ret = af9015_ctrl_msg(d, &req);
  273. i += 1;
  274. }
  275. if (ret)
  276. goto error;
  277. }
  278. ret = i;
  279. error:
  280. mutex_unlock(&d->i2c_mutex);
  281. return ret;
  282. }
  283. static u32 af9015_i2c_func(struct i2c_adapter *adapter)
  284. {
  285. return I2C_FUNC_I2C;
  286. }
  287. static struct i2c_algorithm af9015_i2c_algo = {
  288. .master_xfer = af9015_i2c_xfer,
  289. .functionality = af9015_i2c_func,
  290. };
  291. static int af9015_do_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit, u8 op)
  292. {
  293. int ret;
  294. u8 val, mask = 0x01;
  295. ret = af9015_read_reg(d, addr, &val);
  296. if (ret)
  297. return ret;
  298. mask <<= bit;
  299. if (op) {
  300. /* set bit */
  301. val |= mask;
  302. } else {
  303. /* clear bit */
  304. mask ^= 0xff;
  305. val &= mask;
  306. }
  307. return af9015_write_reg(d, addr, val);
  308. }
  309. static int af9015_set_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit)
  310. {
  311. return af9015_do_reg_bit(d, addr, bit, 1);
  312. }
  313. static int af9015_clear_reg_bit(struct dvb_usb_device *d, u16 addr, u8 bit)
  314. {
  315. return af9015_do_reg_bit(d, addr, bit, 0);
  316. }
  317. static int af9015_init_endpoint(struct dvb_usb_device *d)
  318. {
  319. int ret;
  320. u16 frame_size;
  321. u8 packet_size;
  322. deb_info("%s: USB speed:%d\n", __func__, d->udev->speed);
  323. #define TS_PACKET_SIZE 188
  324. #define TS_USB20_PACKET_COUNT 348
  325. #define TS_USB20_FRAME_SIZE (TS_PACKET_SIZE*TS_USB20_PACKET_COUNT)
  326. #define TS_USB11_PACKET_COUNT 21
  327. #define TS_USB11_FRAME_SIZE (TS_PACKET_SIZE*TS_USB11_PACKET_COUNT)
  328. #define TS_USB20_MAX_PACKET_SIZE 512
  329. #define TS_USB11_MAX_PACKET_SIZE 64
  330. if (d->udev->speed == USB_SPEED_FULL) {
  331. frame_size = TS_USB11_FRAME_SIZE/4;
  332. packet_size = TS_USB11_MAX_PACKET_SIZE/4;
  333. } else {
  334. frame_size = TS_USB20_FRAME_SIZE/4;
  335. packet_size = TS_USB20_MAX_PACKET_SIZE/4;
  336. }
  337. ret = af9015_set_reg_bit(d, 0xd507, 2); /* assert EP4 reset */
  338. if (ret)
  339. goto error;
  340. ret = af9015_set_reg_bit(d, 0xd50b, 1); /* assert EP5 reset */
  341. if (ret)
  342. goto error;
  343. ret = af9015_clear_reg_bit(d, 0xdd11, 5); /* disable EP4 */
  344. if (ret)
  345. goto error;
  346. ret = af9015_clear_reg_bit(d, 0xdd11, 6); /* disable EP5 */
  347. if (ret)
  348. goto error;
  349. ret = af9015_set_reg_bit(d, 0xdd11, 5); /* enable EP4 */
  350. if (ret)
  351. goto error;
  352. if (af9015_config.dual_mode) {
  353. ret = af9015_set_reg_bit(d, 0xdd11, 6); /* enable EP5 */
  354. if (ret)
  355. goto error;
  356. }
  357. ret = af9015_clear_reg_bit(d, 0xdd13, 5); /* disable EP4 NAK */
  358. if (ret)
  359. goto error;
  360. if (af9015_config.dual_mode) {
  361. ret = af9015_clear_reg_bit(d, 0xdd13, 6); /* disable EP5 NAK */
  362. if (ret)
  363. goto error;
  364. }
  365. /* EP4 xfer length */
  366. ret = af9015_write_reg(d, 0xdd88, frame_size & 0xff);
  367. if (ret)
  368. goto error;
  369. ret = af9015_write_reg(d, 0xdd89, frame_size >> 8);
  370. if (ret)
  371. goto error;
  372. /* EP5 xfer length */
  373. ret = af9015_write_reg(d, 0xdd8a, frame_size & 0xff);
  374. if (ret)
  375. goto error;
  376. ret = af9015_write_reg(d, 0xdd8b, frame_size >> 8);
  377. if (ret)
  378. goto error;
  379. ret = af9015_write_reg(d, 0xdd0c, packet_size); /* EP4 packet size */
  380. if (ret)
  381. goto error;
  382. ret = af9015_write_reg(d, 0xdd0d, packet_size); /* EP5 packet size */
  383. if (ret)
  384. goto error;
  385. ret = af9015_clear_reg_bit(d, 0xd507, 2); /* negate EP4 reset */
  386. if (ret)
  387. goto error;
  388. if (af9015_config.dual_mode) {
  389. ret = af9015_clear_reg_bit(d, 0xd50b, 1); /* negate EP5 reset */
  390. if (ret)
  391. goto error;
  392. }
  393. /* enable / disable mp2if2 */
  394. if (af9015_config.dual_mode)
  395. ret = af9015_set_reg_bit(d, 0xd50b, 0);
  396. else
  397. ret = af9015_clear_reg_bit(d, 0xd50b, 0);
  398. error:
  399. if (ret)
  400. err("endpoint init failed:%d", ret);
  401. return ret;
  402. }
  403. static int af9015_copy_firmware(struct dvb_usb_device *d)
  404. {
  405. int ret;
  406. u8 fw_params[4];
  407. u8 val, i;
  408. struct req_t req = {COPY_FIRMWARE, 0, 0x5100, 0, 0, sizeof(fw_params),
  409. fw_params };
  410. deb_info("%s:\n", __func__);
  411. fw_params[0] = af9015_config.firmware_size >> 8;
  412. fw_params[1] = af9015_config.firmware_size & 0xff;
  413. fw_params[2] = af9015_config.firmware_checksum >> 8;
  414. fw_params[3] = af9015_config.firmware_checksum & 0xff;
  415. /* wait 2nd demodulator ready */
  416. msleep(100);
  417. ret = af9015_read_reg_i2c(d, 0x3a, 0x98be, &val);
  418. if (ret)
  419. goto error;
  420. else
  421. deb_info("%s: firmware status:%02x\n", __func__, val);
  422. if (val == 0x0c) /* fw is running, no need for download */
  423. goto exit;
  424. /* set I2C master clock to fast (to speed up firmware copy) */
  425. ret = af9015_write_reg(d, 0xd416, 0x04); /* 0x04 * 400ns */
  426. if (ret)
  427. goto error;
  428. msleep(50);
  429. /* copy firmware */
  430. ret = af9015_ctrl_msg(d, &req);
  431. if (ret)
  432. err("firmware copy cmd failed:%d", ret);
  433. deb_info("%s: firmware copy done\n", __func__);
  434. /* set I2C master clock back to normal */
  435. ret = af9015_write_reg(d, 0xd416, 0x14); /* 0x14 * 400ns */
  436. if (ret)
  437. goto error;
  438. /* request boot firmware */
  439. ret = af9015_write_reg_i2c(d, af9015_af9013_config[1].demod_address,
  440. 0xe205, 1);
  441. deb_info("%s: firmware boot cmd status:%d\n", __func__, ret);
  442. if (ret)
  443. goto error;
  444. for (i = 0; i < 15; i++) {
  445. msleep(100);
  446. /* check firmware status */
  447. ret = af9015_read_reg_i2c(d,
  448. af9015_af9013_config[1].demod_address, 0x98be, &val);
  449. deb_info("%s: firmware status cmd status:%d fw status:%02x\n",
  450. __func__, ret, val);
  451. if (ret)
  452. goto error;
  453. if (val == 0x0c || val == 0x04) /* success or fail */
  454. break;
  455. }
  456. if (val == 0x04) {
  457. err("firmware did not run");
  458. ret = -1;
  459. } else if (val != 0x0c) {
  460. err("firmware boot timeout");
  461. ret = -1;
  462. }
  463. error:
  464. exit:
  465. return ret;
  466. }
  467. /* dump eeprom */
  468. static int af9015_eeprom_dump(struct dvb_usb_device *d)
  469. {
  470. char buf[52], buf2[4];
  471. u8 reg, val;
  472. for (reg = 0; ; reg++) {
  473. if (reg % 16 == 0) {
  474. if (reg)
  475. deb_info("%s\n", buf);
  476. sprintf(buf, "%02x: ", reg);
  477. }
  478. if (af9015_read_reg_i2c(d, AF9015_I2C_EEPROM, reg, &val) == 0)
  479. sprintf(buf2, "%02x ", val);
  480. else
  481. strcpy(buf2, "-- ");
  482. strcat(buf, buf2);
  483. if (reg == 0xff)
  484. break;
  485. }
  486. deb_info("%s\n", buf);
  487. return 0;
  488. }
  489. static int af9015_download_ir_table(struct dvb_usb_device *d)
  490. {
  491. int i, packets = 0, ret;
  492. u16 addr = 0x9a56; /* ir-table start address */
  493. struct req_t req = {WRITE_MEMORY, 0, 0, 0, 0, 1, NULL};
  494. u8 *data = NULL;
  495. deb_info("%s:\n", __func__);
  496. data = af9015_config.ir_table;
  497. packets = af9015_config.ir_table_size;
  498. /* no remote */
  499. if (!packets)
  500. goto exit;
  501. /* load remote ir-table */
  502. for (i = 0; i < packets; i++) {
  503. req.addr = addr + i;
  504. req.data = &data[i];
  505. ret = af9015_ctrl_msg(d, &req);
  506. if (ret) {
  507. err("ir-table download failed at packet %d with " \
  508. "code %d", i, ret);
  509. return ret;
  510. }
  511. }
  512. exit:
  513. return 0;
  514. }
  515. static int af9015_init(struct dvb_usb_device *d)
  516. {
  517. int ret;
  518. deb_info("%s:\n", __func__);
  519. ret = af9015_init_endpoint(d);
  520. if (ret)
  521. goto error;
  522. ret = af9015_download_ir_table(d);
  523. if (ret)
  524. goto error;
  525. error:
  526. return ret;
  527. }
  528. static int af9015_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
  529. {
  530. int ret;
  531. deb_info("%s: onoff:%d\n", __func__, onoff);
  532. if (onoff)
  533. ret = af9015_set_reg_bit(adap->dev, 0xd503, 0);
  534. else
  535. ret = af9015_clear_reg_bit(adap->dev, 0xd503, 0);
  536. return ret;
  537. }
  538. static int af9015_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid,
  539. int onoff)
  540. {
  541. int ret;
  542. u8 idx;
  543. deb_info("%s: set pid filter, index %d, pid %x, onoff %d\n",
  544. __func__, index, pid, onoff);
  545. ret = af9015_write_reg(adap->dev, 0xd505, (pid & 0xff));
  546. if (ret)
  547. goto error;
  548. ret = af9015_write_reg(adap->dev, 0xd506, (pid >> 8));
  549. if (ret)
  550. goto error;
  551. idx = ((index & 0x1f) | (1 << 5));
  552. ret = af9015_write_reg(adap->dev, 0xd504, idx);
  553. error:
  554. return ret;
  555. }
  556. static int af9015_download_firmware(struct usb_device *udev,
  557. const struct firmware *fw)
  558. {
  559. int i, len, packets, remainder, ret;
  560. struct req_t req = {DOWNLOAD_FIRMWARE, 0, 0, 0, 0, 0, NULL};
  561. u16 addr = 0x5100; /* firmware start address */
  562. u16 checksum = 0;
  563. deb_info("%s:\n", __func__);
  564. /* calc checksum */
  565. for (i = 0; i < fw->size; i++)
  566. checksum += fw->data[i];
  567. af9015_config.firmware_size = fw->size;
  568. af9015_config.firmware_checksum = checksum;
  569. #define FW_PACKET_MAX_DATA 55
  570. packets = fw->size / FW_PACKET_MAX_DATA;
  571. remainder = fw->size % FW_PACKET_MAX_DATA;
  572. len = FW_PACKET_MAX_DATA;
  573. for (i = 0; i <= packets; i++) {
  574. if (i == packets) /* set size of the last packet */
  575. len = remainder;
  576. req.data_len = len;
  577. req.data = (u8 *)(fw->data + i * FW_PACKET_MAX_DATA);
  578. req.addr = addr;
  579. addr += FW_PACKET_MAX_DATA;
  580. ret = af9015_rw_udev(udev, &req);
  581. if (ret) {
  582. err("firmware download failed at packet %d with " \
  583. "code %d", i, ret);
  584. goto error;
  585. }
  586. }
  587. /* firmware loaded, request boot */
  588. req.cmd = BOOT;
  589. ret = af9015_rw_udev(udev, &req);
  590. if (ret) {
  591. err("firmware boot failed:%d", ret);
  592. goto error;
  593. }
  594. error:
  595. return ret;
  596. }
  597. static int af9015_read_config(struct usb_device *udev)
  598. {
  599. int ret;
  600. u8 val, i, offset = 0;
  601. struct req_t req = {READ_I2C, AF9015_I2C_EEPROM, 0, 0, 1, 1, &val};
  602. char manufacturer[10];
  603. /* IR remote controller */
  604. req.addr = AF9015_EEPROM_IR_MODE;
  605. /* first message will timeout often due to possible hw bug */
  606. for (i = 0; i < 4; i++) {
  607. ret = af9015_rw_udev(udev, &req);
  608. if (!ret)
  609. break;
  610. }
  611. if (ret)
  612. goto error;
  613. deb_info("%s: IR mode:%d\n", __func__, val);
  614. for (i = 0; i < af9015_properties_count; i++) {
  615. if (val == AF9015_IR_MODE_DISABLED || val == 0x04) {
  616. af9015_properties[i].rc_key_map = NULL;
  617. af9015_properties[i].rc_key_map_size = 0;
  618. } else if (dvb_usb_af9015_remote) {
  619. /* load remote defined as module param */
  620. switch (dvb_usb_af9015_remote) {
  621. case AF9015_REMOTE_A_LINK_DTU_M:
  622. af9015_properties[i].rc_key_map =
  623. af9015_rc_keys_a_link;
  624. af9015_properties[i].rc_key_map_size =
  625. ARRAY_SIZE(af9015_rc_keys_a_link);
  626. af9015_config.ir_table = af9015_ir_table_a_link;
  627. af9015_config.ir_table_size =
  628. ARRAY_SIZE(af9015_ir_table_a_link);
  629. break;
  630. case AF9015_REMOTE_MSI_DIGIVOX_MINI_II_V3:
  631. af9015_properties[i].rc_key_map =
  632. af9015_rc_keys_msi;
  633. af9015_properties[i].rc_key_map_size =
  634. ARRAY_SIZE(af9015_rc_keys_msi);
  635. af9015_config.ir_table = af9015_ir_table_msi;
  636. af9015_config.ir_table_size =
  637. ARRAY_SIZE(af9015_ir_table_msi);
  638. break;
  639. case AF9015_REMOTE_MYGICTV_U718:
  640. af9015_properties[i].rc_key_map =
  641. af9015_rc_keys_mygictv;
  642. af9015_properties[i].rc_key_map_size =
  643. ARRAY_SIZE(af9015_rc_keys_mygictv);
  644. af9015_config.ir_table =
  645. af9015_ir_table_mygictv;
  646. af9015_config.ir_table_size =
  647. ARRAY_SIZE(af9015_ir_table_mygictv);
  648. break;
  649. case AF9015_REMOTE_DIGITTRADE_DVB_T:
  650. af9015_properties[i].rc_key_map =
  651. af9015_rc_keys_digittrade;
  652. af9015_properties[i].rc_key_map_size =
  653. ARRAY_SIZE(af9015_rc_keys_digittrade);
  654. af9015_config.ir_table =
  655. af9015_ir_table_digittrade;
  656. af9015_config.ir_table_size =
  657. ARRAY_SIZE(af9015_ir_table_digittrade);
  658. break;
  659. case AF9015_REMOTE_AVERMEDIA_KS:
  660. af9015_properties[i].rc_key_map =
  661. af9015_rc_keys_avermedia;
  662. af9015_properties[i].rc_key_map_size =
  663. ARRAY_SIZE(af9015_rc_keys_avermedia);
  664. af9015_config.ir_table =
  665. af9015_ir_table_avermedia_ks;
  666. af9015_config.ir_table_size =
  667. ARRAY_SIZE(af9015_ir_table_avermedia_ks);
  668. break;
  669. }
  670. } else {
  671. switch (le16_to_cpu(udev->descriptor.idVendor)) {
  672. case USB_VID_LEADTEK:
  673. af9015_properties[i].rc_key_map =
  674. af9015_rc_keys_leadtek;
  675. af9015_properties[i].rc_key_map_size =
  676. ARRAY_SIZE(af9015_rc_keys_leadtek);
  677. af9015_config.ir_table =
  678. af9015_ir_table_leadtek;
  679. af9015_config.ir_table_size =
  680. ARRAY_SIZE(af9015_ir_table_leadtek);
  681. break;
  682. case USB_VID_VISIONPLUS:
  683. if (udev->descriptor.idProduct ==
  684. cpu_to_le16(USB_PID_AZUREWAVE_AD_TU700)) {
  685. af9015_properties[i].rc_key_map =
  686. af9015_rc_keys_twinhan;
  687. af9015_properties[i].rc_key_map_size =
  688. ARRAY_SIZE(af9015_rc_keys_twinhan);
  689. af9015_config.ir_table =
  690. af9015_ir_table_twinhan;
  691. af9015_config.ir_table_size =
  692. ARRAY_SIZE(af9015_ir_table_twinhan);
  693. }
  694. break;
  695. case USB_VID_KWORLD_2:
  696. /* TODO: use correct rc keys */
  697. af9015_properties[i].rc_key_map =
  698. af9015_rc_keys_twinhan;
  699. af9015_properties[i].rc_key_map_size =
  700. ARRAY_SIZE(af9015_rc_keys_twinhan);
  701. af9015_config.ir_table = af9015_ir_table_kworld;
  702. af9015_config.ir_table_size =
  703. ARRAY_SIZE(af9015_ir_table_kworld);
  704. break;
  705. /* Check USB manufacturer and product strings and try
  706. to determine correct remote in case of chip vendor
  707. reference IDs are used. */
  708. case USB_VID_AFATECH:
  709. memset(manufacturer, 0, sizeof(manufacturer));
  710. usb_string(udev, udev->descriptor.iManufacturer,
  711. manufacturer, sizeof(manufacturer));
  712. if (!strcmp("Geniatech", manufacturer)) {
  713. /* iManufacturer 1 Geniatech
  714. iProduct 2 AF9015 */
  715. af9015_properties[i].rc_key_map =
  716. af9015_rc_keys_mygictv;
  717. af9015_properties[i].rc_key_map_size =
  718. ARRAY_SIZE(af9015_rc_keys_mygictv);
  719. af9015_config.ir_table =
  720. af9015_ir_table_mygictv;
  721. af9015_config.ir_table_size =
  722. ARRAY_SIZE(af9015_ir_table_mygictv);
  723. } else if (!strcmp("MSI", manufacturer)) {
  724. /* iManufacturer 1 MSI
  725. iProduct 2 MSI K-VOX */
  726. af9015_properties[i].rc_key_map =
  727. af9015_rc_keys_msi;
  728. af9015_properties[i].rc_key_map_size =
  729. ARRAY_SIZE(af9015_rc_keys_msi);
  730. af9015_config.ir_table =
  731. af9015_ir_table_msi;
  732. af9015_config.ir_table_size =
  733. ARRAY_SIZE(af9015_ir_table_msi);
  734. } else if (udev->descriptor.idProduct ==
  735. cpu_to_le16(USB_PID_TREKSTOR_DVBT)) {
  736. af9015_properties[i].rc_key_map =
  737. af9015_rc_keys_trekstor;
  738. af9015_properties[i].rc_key_map_size =
  739. ARRAY_SIZE(af9015_rc_keys_trekstor);
  740. af9015_config.ir_table =
  741. af9015_ir_table_trekstor;
  742. af9015_config.ir_table_size =
  743. ARRAY_SIZE(af9015_ir_table_trekstor);
  744. }
  745. break;
  746. case USB_VID_AVERMEDIA:
  747. af9015_properties[i].rc_key_map =
  748. af9015_rc_keys_avermedia;
  749. af9015_properties[i].rc_key_map_size =
  750. ARRAY_SIZE(af9015_rc_keys_avermedia);
  751. af9015_config.ir_table =
  752. af9015_ir_table_avermedia;
  753. af9015_config.ir_table_size =
  754. ARRAY_SIZE(af9015_ir_table_avermedia);
  755. break;
  756. }
  757. }
  758. }
  759. /* TS mode - one or two receivers */
  760. req.addr = AF9015_EEPROM_TS_MODE;
  761. ret = af9015_rw_udev(udev, &req);
  762. if (ret)
  763. goto error;
  764. af9015_config.dual_mode = val;
  765. deb_info("%s: TS mode:%d\n", __func__, af9015_config.dual_mode);
  766. /* Set adapter0 buffer size according to USB port speed, adapter1 buffer
  767. size can be static because it is enabled only USB2.0 */
  768. for (i = 0; i < af9015_properties_count; i++) {
  769. /* USB1.1 set smaller buffersize and disable 2nd adapter */
  770. if (udev->speed == USB_SPEED_FULL) {
  771. af9015_properties[i].adapter[0].stream.u.bulk.buffersize
  772. = TS_USB11_MAX_PACKET_SIZE;
  773. /* disable 2nd adapter because we don't have
  774. PID-filters */
  775. af9015_config.dual_mode = 0;
  776. } else {
  777. af9015_properties[i].adapter[0].stream.u.bulk.buffersize
  778. = TS_USB20_MAX_PACKET_SIZE;
  779. }
  780. }
  781. if (af9015_config.dual_mode) {
  782. /* read 2nd demodulator I2C address */
  783. req.addr = AF9015_EEPROM_DEMOD2_I2C;
  784. ret = af9015_rw_udev(udev, &req);
  785. if (ret)
  786. goto error;
  787. af9015_af9013_config[1].demod_address = val;
  788. /* enable 2nd adapter */
  789. for (i = 0; i < af9015_properties_count; i++)
  790. af9015_properties[i].num_adapters = 2;
  791. } else {
  792. /* disable 2nd adapter */
  793. for (i = 0; i < af9015_properties_count; i++)
  794. af9015_properties[i].num_adapters = 1;
  795. }
  796. for (i = 0; i < af9015_properties[0].num_adapters; i++) {
  797. if (i == 1)
  798. offset = AF9015_EEPROM_OFFSET;
  799. /* xtal */
  800. req.addr = AF9015_EEPROM_XTAL_TYPE1 + offset;
  801. ret = af9015_rw_udev(udev, &req);
  802. if (ret)
  803. goto error;
  804. switch (val) {
  805. case 0:
  806. af9015_af9013_config[i].adc_clock = 28800;
  807. break;
  808. case 1:
  809. af9015_af9013_config[i].adc_clock = 20480;
  810. break;
  811. case 2:
  812. af9015_af9013_config[i].adc_clock = 28000;
  813. break;
  814. case 3:
  815. af9015_af9013_config[i].adc_clock = 25000;
  816. break;
  817. };
  818. deb_info("%s: [%d] xtal:%d set adc_clock:%d\n", __func__, i,
  819. val, af9015_af9013_config[i].adc_clock);
  820. /* tuner IF */
  821. req.addr = AF9015_EEPROM_IF1H + offset;
  822. ret = af9015_rw_udev(udev, &req);
  823. if (ret)
  824. goto error;
  825. af9015_af9013_config[i].tuner_if = val << 8;
  826. req.addr = AF9015_EEPROM_IF1L + offset;
  827. ret = af9015_rw_udev(udev, &req);
  828. if (ret)
  829. goto error;
  830. af9015_af9013_config[i].tuner_if += val;
  831. deb_info("%s: [%d] IF1:%d\n", __func__, i,
  832. af9015_af9013_config[0].tuner_if);
  833. /* MT2060 IF1 */
  834. req.addr = AF9015_EEPROM_MT2060_IF1H + offset;
  835. ret = af9015_rw_udev(udev, &req);
  836. if (ret)
  837. goto error;
  838. af9015_config.mt2060_if1[i] = val << 8;
  839. req.addr = AF9015_EEPROM_MT2060_IF1L + offset;
  840. ret = af9015_rw_udev(udev, &req);
  841. if (ret)
  842. goto error;
  843. af9015_config.mt2060_if1[i] += val;
  844. deb_info("%s: [%d] MT2060 IF1:%d\n", __func__, i,
  845. af9015_config.mt2060_if1[i]);
  846. /* tuner */
  847. req.addr = AF9015_EEPROM_TUNER_ID1 + offset;
  848. ret = af9015_rw_udev(udev, &req);
  849. if (ret)
  850. goto error;
  851. switch (val) {
  852. case AF9013_TUNER_ENV77H11D5:
  853. case AF9013_TUNER_MT2060:
  854. case AF9013_TUNER_QT1010:
  855. case AF9013_TUNER_UNKNOWN:
  856. case AF9013_TUNER_MT2060_2:
  857. case AF9013_TUNER_TDA18271:
  858. case AF9013_TUNER_QT1010A:
  859. af9015_af9013_config[i].rf_spec_inv = 1;
  860. break;
  861. case AF9013_TUNER_MXL5003D:
  862. case AF9013_TUNER_MXL5005D:
  863. case AF9013_TUNER_MXL5005R:
  864. af9015_af9013_config[i].rf_spec_inv = 0;
  865. break;
  866. case AF9013_TUNER_MC44S803:
  867. af9015_af9013_config[i].gpio[1] = AF9013_GPIO_LO;
  868. af9015_af9013_config[i].rf_spec_inv = 1;
  869. break;
  870. default:
  871. warn("tuner id:%d not supported, please report!", val);
  872. return -ENODEV;
  873. };
  874. af9015_af9013_config[i].tuner = val;
  875. deb_info("%s: [%d] tuner id:%d\n", __func__, i, val);
  876. }
  877. error:
  878. if (ret)
  879. err("eeprom read failed:%d", ret);
  880. /* AverMedia AVerTV Volar Black HD (A850) device have bad EEPROM
  881. content :-( Override some wrong values here. */
  882. if (le16_to_cpu(udev->descriptor.idVendor) == USB_VID_AVERMEDIA &&
  883. le16_to_cpu(udev->descriptor.idProduct) == USB_PID_AVERMEDIA_A850) {
  884. deb_info("%s: AverMedia A850: overriding config\n", __func__);
  885. /* disable dual mode */
  886. af9015_config.dual_mode = 0;
  887. /* disable 2nd adapter */
  888. for (i = 0; i < af9015_properties_count; i++)
  889. af9015_properties[i].num_adapters = 1;
  890. /* set correct IF */
  891. af9015_af9013_config[0].tuner_if = 4570;
  892. }
  893. return ret;
  894. }
  895. static int af9015_identify_state(struct usb_device *udev,
  896. struct dvb_usb_device_properties *props,
  897. struct dvb_usb_device_description **desc,
  898. int *cold)
  899. {
  900. int ret;
  901. u8 reply;
  902. struct req_t req = {GET_CONFIG, 0, 0, 0, 0, 1, &reply};
  903. ret = af9015_rw_udev(udev, &req);
  904. if (ret)
  905. return ret;
  906. deb_info("%s: reply:%02x\n", __func__, reply);
  907. if (reply == 0x02)
  908. *cold = 0;
  909. else
  910. *cold = 1;
  911. return ret;
  912. }
  913. static int af9015_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
  914. {
  915. u8 buf[8];
  916. struct req_t req = {GET_IR_CODE, 0, 0, 0, 0, sizeof(buf), buf};
  917. struct dvb_usb_rc_key *keymap = d->props.rc_key_map;
  918. int i, ret;
  919. memset(buf, 0, sizeof(buf));
  920. ret = af9015_ctrl_msg(d, &req);
  921. if (ret)
  922. return ret;
  923. *event = 0;
  924. *state = REMOTE_NO_KEY_PRESSED;
  925. for (i = 0; i < d->props.rc_key_map_size; i++) {
  926. if (!buf[1] && keymap[i].custom == buf[0] &&
  927. keymap[i].data == buf[2]) {
  928. *event = keymap[i].event;
  929. *state = REMOTE_KEY_PRESSED;
  930. break;
  931. }
  932. }
  933. if (!buf[1])
  934. deb_rc("%s: %02x %02x %02x %02x %02x %02x %02x %02x\n",
  935. __func__, buf[0], buf[1], buf[2], buf[3], buf[4],
  936. buf[5], buf[6], buf[7]);
  937. return 0;
  938. }
  939. /* init 2nd I2C adapter */
  940. static int af9015_i2c_init(struct dvb_usb_device *d)
  941. {
  942. int ret;
  943. struct af9015_state *state = d->priv;
  944. deb_info("%s:\n", __func__);
  945. strncpy(state->i2c_adap.name, d->desc->name,
  946. sizeof(state->i2c_adap.name));
  947. #ifdef I2C_ADAP_CLASS_TV_DIGITAL
  948. state->i2c_adap.class = I2C_ADAP_CLASS_TV_DIGITAL,
  949. #else
  950. state->i2c_adap.class = I2C_CLASS_TV_DIGITAL,
  951. #endif
  952. state->i2c_adap.algo = d->props.i2c_algo;
  953. state->i2c_adap.algo_data = NULL;
  954. state->i2c_adap.dev.parent = &d->udev->dev;
  955. i2c_set_adapdata(&state->i2c_adap, d);
  956. ret = i2c_add_adapter(&state->i2c_adap);
  957. if (ret < 0)
  958. err("could not add i2c adapter");
  959. return ret;
  960. }
  961. static int af9015_af9013_frontend_attach(struct dvb_usb_adapter *adap)
  962. {
  963. int ret;
  964. struct af9015_state *state = adap->dev->priv;
  965. struct i2c_adapter *i2c_adap;
  966. if (adap->id == 0) {
  967. /* select I2C adapter */
  968. i2c_adap = &adap->dev->i2c_adap;
  969. deb_info("%s: init I2C\n", __func__);
  970. ret = af9015_i2c_init(adap->dev);
  971. /* dump eeprom (debug) */
  972. ret = af9015_eeprom_dump(adap->dev);
  973. if (ret)
  974. return ret;
  975. } else {
  976. /* select I2C adapter */
  977. i2c_adap = &state->i2c_adap;
  978. /* copy firmware to 2nd demodulator */
  979. if (af9015_config.dual_mode) {
  980. ret = af9015_copy_firmware(adap->dev);
  981. if (ret) {
  982. err("firmware copy to 2nd frontend " \
  983. "failed, will disable it");
  984. af9015_config.dual_mode = 0;
  985. return -ENODEV;
  986. }
  987. } else {
  988. return -ENODEV;
  989. }
  990. }
  991. /* attach demodulator */
  992. adap->fe = dvb_attach(af9013_attach, &af9015_af9013_config[adap->id],
  993. i2c_adap);
  994. return adap->fe == NULL ? -ENODEV : 0;
  995. }
  996. static struct mt2060_config af9015_mt2060_config = {
  997. .i2c_address = 0xc0,
  998. .clock_out = 0,
  999. };
  1000. static struct qt1010_config af9015_qt1010_config = {
  1001. .i2c_address = 0xc4,
  1002. };
  1003. static struct tda18271_config af9015_tda18271_config = {
  1004. .gate = TDA18271_GATE_DIGITAL,
  1005. .small_i2c = 1,
  1006. };
  1007. static struct mxl5005s_config af9015_mxl5003_config = {
  1008. .i2c_address = 0xc6,
  1009. .if_freq = IF_FREQ_4570000HZ,
  1010. .xtal_freq = CRYSTAL_FREQ_16000000HZ,
  1011. .agc_mode = MXL_SINGLE_AGC,
  1012. .tracking_filter = MXL_TF_DEFAULT,
  1013. .rssi_enable = MXL_RSSI_ENABLE,
  1014. .cap_select = MXL_CAP_SEL_ENABLE,
  1015. .div_out = MXL_DIV_OUT_4,
  1016. .clock_out = MXL_CLOCK_OUT_DISABLE,
  1017. .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
  1018. .top = MXL5005S_TOP_25P2,
  1019. .mod_mode = MXL_DIGITAL_MODE,
  1020. .if_mode = MXL_ZERO_IF,
  1021. .AgcMasterByte = 0x00,
  1022. };
  1023. static struct mxl5005s_config af9015_mxl5005_config = {
  1024. .i2c_address = 0xc6,
  1025. .if_freq = IF_FREQ_4570000HZ,
  1026. .xtal_freq = CRYSTAL_FREQ_16000000HZ,
  1027. .agc_mode = MXL_SINGLE_AGC,
  1028. .tracking_filter = MXL_TF_OFF,
  1029. .rssi_enable = MXL_RSSI_ENABLE,
  1030. .cap_select = MXL_CAP_SEL_ENABLE,
  1031. .div_out = MXL_DIV_OUT_4,
  1032. .clock_out = MXL_CLOCK_OUT_DISABLE,
  1033. .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
  1034. .top = MXL5005S_TOP_25P2,
  1035. .mod_mode = MXL_DIGITAL_MODE,
  1036. .if_mode = MXL_ZERO_IF,
  1037. .AgcMasterByte = 0x00,
  1038. };
  1039. static struct mc44s803_config af9015_mc44s803_config = {
  1040. .i2c_address = 0xc0,
  1041. .dig_out = 1,
  1042. };
  1043. static int af9015_tuner_attach(struct dvb_usb_adapter *adap)
  1044. {
  1045. struct af9015_state *state = adap->dev->priv;
  1046. struct i2c_adapter *i2c_adap;
  1047. int ret;
  1048. deb_info("%s: \n", __func__);
  1049. /* select I2C adapter */
  1050. if (adap->id == 0)
  1051. i2c_adap = &adap->dev->i2c_adap;
  1052. else
  1053. i2c_adap = &state->i2c_adap;
  1054. switch (af9015_af9013_config[adap->id].tuner) {
  1055. case AF9013_TUNER_MT2060:
  1056. case AF9013_TUNER_MT2060_2:
  1057. ret = dvb_attach(mt2060_attach, adap->fe, i2c_adap,
  1058. &af9015_mt2060_config,
  1059. af9015_config.mt2060_if1[adap->id])
  1060. == NULL ? -ENODEV : 0;
  1061. break;
  1062. case AF9013_TUNER_QT1010:
  1063. case AF9013_TUNER_QT1010A:
  1064. ret = dvb_attach(qt1010_attach, adap->fe, i2c_adap,
  1065. &af9015_qt1010_config) == NULL ? -ENODEV : 0;
  1066. break;
  1067. case AF9013_TUNER_TDA18271:
  1068. ret = dvb_attach(tda18271_attach, adap->fe, 0xc0, i2c_adap,
  1069. &af9015_tda18271_config) == NULL ? -ENODEV : 0;
  1070. break;
  1071. case AF9013_TUNER_MXL5003D:
  1072. ret = dvb_attach(mxl5005s_attach, adap->fe, i2c_adap,
  1073. &af9015_mxl5003_config) == NULL ? -ENODEV : 0;
  1074. break;
  1075. case AF9013_TUNER_MXL5005D:
  1076. case AF9013_TUNER_MXL5005R:
  1077. ret = dvb_attach(mxl5005s_attach, adap->fe, i2c_adap,
  1078. &af9015_mxl5005_config) == NULL ? -ENODEV : 0;
  1079. break;
  1080. case AF9013_TUNER_ENV77H11D5:
  1081. ret = dvb_attach(dvb_pll_attach, adap->fe, 0xc0, i2c_adap,
  1082. DVB_PLL_TDA665X) == NULL ? -ENODEV : 0;
  1083. break;
  1084. case AF9013_TUNER_MC44S803:
  1085. ret = dvb_attach(mc44s803_attach, adap->fe, i2c_adap,
  1086. &af9015_mc44s803_config) == NULL ? -ENODEV : 0;
  1087. break;
  1088. case AF9013_TUNER_UNKNOWN:
  1089. default:
  1090. ret = -ENODEV;
  1091. err("Unknown tuner id:%d",
  1092. af9015_af9013_config[adap->id].tuner);
  1093. }
  1094. return ret;
  1095. }
  1096. static struct usb_device_id af9015_usb_table[] = {
  1097. /* 0 */{USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9015_9015)},
  1098. {USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9015_9016)},
  1099. {USB_DEVICE(USB_VID_LEADTEK, USB_PID_WINFAST_DTV_DONGLE_GOLD)},
  1100. {USB_DEVICE(USB_VID_PINNACLE, USB_PID_PINNACLE_PCTV71E)},
  1101. {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_399U)},
  1102. /* 5 */{USB_DEVICE(USB_VID_VISIONPLUS,
  1103. USB_PID_TINYTWIN)},
  1104. {USB_DEVICE(USB_VID_VISIONPLUS,
  1105. USB_PID_AZUREWAVE_AD_TU700)},
  1106. {USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_USB_XE_REV2)},
  1107. {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_PC160_2T)},
  1108. {USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_VOLAR_X)},
  1109. /* 10 */{USB_DEVICE(USB_VID_XTENSIONS, USB_PID_XTENSIONS_XD_380)},
  1110. {USB_DEVICE(USB_VID_MSI_2, USB_PID_MSI_DIGIVOX_DUO)},
  1111. {USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_VOLAR_X_2)},
  1112. {USB_DEVICE(USB_VID_TELESTAR, USB_PID_TELESTAR_STARSTICK_2)},
  1113. {USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A309)},
  1114. /* 15 */{USB_DEVICE(USB_VID_MSI_2, USB_PID_MSI_DIGI_VOX_MINI_III)},
  1115. {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U)},
  1116. {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_2)},
  1117. {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_3)},
  1118. {USB_DEVICE(USB_VID_AFATECH, USB_PID_TREKSTOR_DVBT)},
  1119. {USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A850)},
  1120. {0},
  1121. };
  1122. MODULE_DEVICE_TABLE(usb, af9015_usb_table);
  1123. static struct dvb_usb_device_properties af9015_properties[] = {
  1124. {
  1125. .caps = DVB_USB_IS_AN_I2C_ADAPTER,
  1126. .usb_ctrl = DEVICE_SPECIFIC,
  1127. .download_firmware = af9015_download_firmware,
  1128. .firmware = "dvb-usb-af9015.fw",
  1129. .no_reconnect = 1,
  1130. .size_of_priv = sizeof(struct af9015_state), \
  1131. .num_adapters = 2,
  1132. .adapter = {
  1133. {
  1134. .caps = DVB_USB_ADAP_HAS_PID_FILTER |
  1135. DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
  1136. .pid_filter_count = 32,
  1137. .pid_filter = af9015_pid_filter,
  1138. .pid_filter_ctrl = af9015_pid_filter_ctrl,
  1139. .frontend_attach =
  1140. af9015_af9013_frontend_attach,
  1141. .tuner_attach = af9015_tuner_attach,
  1142. .stream = {
  1143. .type = USB_BULK,
  1144. .count = 6,
  1145. .endpoint = 0x84,
  1146. },
  1147. },
  1148. {
  1149. .frontend_attach =
  1150. af9015_af9013_frontend_attach,
  1151. .tuner_attach = af9015_tuner_attach,
  1152. .stream = {
  1153. .type = USB_BULK,
  1154. .count = 6,
  1155. .endpoint = 0x85,
  1156. .u = {
  1157. .bulk = {
  1158. .buffersize =
  1159. TS_USB20_MAX_PACKET_SIZE,
  1160. }
  1161. }
  1162. },
  1163. }
  1164. },
  1165. .identify_state = af9015_identify_state,
  1166. .rc_query = af9015_rc_query,
  1167. .rc_interval = 150,
  1168. .i2c_algo = &af9015_i2c_algo,
  1169. .num_device_descs = 9,
  1170. .devices = {
  1171. {
  1172. .name = "Afatech AF9015 DVB-T USB2.0 stick",
  1173. .cold_ids = {&af9015_usb_table[0],
  1174. &af9015_usb_table[1], NULL},
  1175. .warm_ids = {NULL},
  1176. },
  1177. {
  1178. .name = "Leadtek WinFast DTV Dongle Gold",
  1179. .cold_ids = {&af9015_usb_table[2], NULL},
  1180. .warm_ids = {NULL},
  1181. },
  1182. {
  1183. .name = "Pinnacle PCTV 71e",
  1184. .cold_ids = {&af9015_usb_table[3], NULL},
  1185. .warm_ids = {NULL},
  1186. },
  1187. {
  1188. .name = "KWorld PlusTV Dual DVB-T Stick " \
  1189. "(DVB-T 399U)",
  1190. .cold_ids = {&af9015_usb_table[4], NULL},
  1191. .warm_ids = {NULL},
  1192. },
  1193. {
  1194. .name = "DigitalNow TinyTwin DVB-T Receiver",
  1195. .cold_ids = {&af9015_usb_table[5], NULL},
  1196. .warm_ids = {NULL},
  1197. },
  1198. {
  1199. .name = "TwinHan AzureWave AD-TU700(704J)",
  1200. .cold_ids = {&af9015_usb_table[6], NULL},
  1201. .warm_ids = {NULL},
  1202. },
  1203. {
  1204. .name = "TerraTec Cinergy T USB XE",
  1205. .cold_ids = {&af9015_usb_table[7], NULL},
  1206. .warm_ids = {NULL},
  1207. },
  1208. {
  1209. .name = "KWorld PlusTV Dual DVB-T PCI " \
  1210. "(DVB-T PC160-2T)",
  1211. .cold_ids = {&af9015_usb_table[8], NULL},
  1212. .warm_ids = {NULL},
  1213. },
  1214. {
  1215. .name = "AVerMedia AVerTV DVB-T Volar X",
  1216. .cold_ids = {&af9015_usb_table[9], NULL},
  1217. .warm_ids = {NULL},
  1218. },
  1219. }
  1220. }, {
  1221. .caps = DVB_USB_IS_AN_I2C_ADAPTER,
  1222. .usb_ctrl = DEVICE_SPECIFIC,
  1223. .download_firmware = af9015_download_firmware,
  1224. .firmware = "dvb-usb-af9015.fw",
  1225. .no_reconnect = 1,
  1226. .size_of_priv = sizeof(struct af9015_state), \
  1227. .num_adapters = 2,
  1228. .adapter = {
  1229. {
  1230. .caps = DVB_USB_ADAP_HAS_PID_FILTER |
  1231. DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
  1232. .pid_filter_count = 32,
  1233. .pid_filter = af9015_pid_filter,
  1234. .pid_filter_ctrl = af9015_pid_filter_ctrl,
  1235. .frontend_attach =
  1236. af9015_af9013_frontend_attach,
  1237. .tuner_attach = af9015_tuner_attach,
  1238. .stream = {
  1239. .type = USB_BULK,
  1240. .count = 6,
  1241. .endpoint = 0x84,
  1242. },
  1243. },
  1244. {
  1245. .frontend_attach =
  1246. af9015_af9013_frontend_attach,
  1247. .tuner_attach = af9015_tuner_attach,
  1248. .stream = {
  1249. .type = USB_BULK,
  1250. .count = 6,
  1251. .endpoint = 0x85,
  1252. .u = {
  1253. .bulk = {
  1254. .buffersize =
  1255. TS_USB20_MAX_PACKET_SIZE,
  1256. }
  1257. }
  1258. },
  1259. }
  1260. },
  1261. .identify_state = af9015_identify_state,
  1262. .rc_query = af9015_rc_query,
  1263. .rc_interval = 150,
  1264. .i2c_algo = &af9015_i2c_algo,
  1265. .num_device_descs = 9,
  1266. .devices = {
  1267. {
  1268. .name = "Xtensions XD-380",
  1269. .cold_ids = {&af9015_usb_table[10], NULL},
  1270. .warm_ids = {NULL},
  1271. },
  1272. {
  1273. .name = "MSI DIGIVOX Duo",
  1274. .cold_ids = {&af9015_usb_table[11], NULL},
  1275. .warm_ids = {NULL},
  1276. },
  1277. {
  1278. .name = "Fujitsu-Siemens Slim Mobile USB DVB-T",
  1279. .cold_ids = {&af9015_usb_table[12], NULL},
  1280. .warm_ids = {NULL},
  1281. },
  1282. {
  1283. .name = "Telestar Starstick 2",
  1284. .cold_ids = {&af9015_usb_table[13], NULL},
  1285. .warm_ids = {NULL},
  1286. },
  1287. {
  1288. .name = "AVerMedia A309",
  1289. .cold_ids = {&af9015_usb_table[14], NULL},
  1290. .warm_ids = {NULL},
  1291. },
  1292. {
  1293. .name = "MSI Digi VOX mini III",
  1294. .cold_ids = {&af9015_usb_table[15], NULL},
  1295. .warm_ids = {NULL},
  1296. },
  1297. {
  1298. .name = "KWorld USB DVB-T TV Stick II " \
  1299. "(VS-DVB-T 395U)",
  1300. .cold_ids = {&af9015_usb_table[16],
  1301. &af9015_usb_table[17],
  1302. &af9015_usb_table[18], NULL},
  1303. .warm_ids = {NULL},
  1304. },
  1305. {
  1306. .name = "TrekStor DVB-T USB Stick",
  1307. .cold_ids = {&af9015_usb_table[19], NULL},
  1308. .warm_ids = {NULL},
  1309. },
  1310. {
  1311. .name = "AverMedia AVerTV Volar Black HD " \
  1312. "(A850)",
  1313. .cold_ids = {&af9015_usb_table[20], NULL},
  1314. .warm_ids = {NULL},
  1315. },
  1316. }
  1317. }
  1318. };
  1319. static int af9015_usb_probe(struct usb_interface *intf,
  1320. const struct usb_device_id *id)
  1321. {
  1322. int ret = 0;
  1323. struct dvb_usb_device *d = NULL;
  1324. struct usb_device *udev = interface_to_usbdev(intf);
  1325. u8 i;
  1326. deb_info("%s: interface:%d\n", __func__,
  1327. intf->cur_altsetting->desc.bInterfaceNumber);
  1328. /* interface 0 is used by DVB-T receiver and
  1329. interface 1 is for remote controller (HID) */
  1330. if (intf->cur_altsetting->desc.bInterfaceNumber == 0) {
  1331. ret = af9015_read_config(udev);
  1332. if (ret)
  1333. return ret;
  1334. for (i = 0; i < af9015_properties_count; i++) {
  1335. ret = dvb_usb_device_init(intf, &af9015_properties[i],
  1336. THIS_MODULE, &d, adapter_nr);
  1337. if (!ret)
  1338. break;
  1339. if (ret != -ENODEV)
  1340. return ret;
  1341. }
  1342. if (ret)
  1343. return ret;
  1344. if (d)
  1345. ret = af9015_init(d);
  1346. }
  1347. return ret;
  1348. }
  1349. static void af9015_i2c_exit(struct dvb_usb_device *d)
  1350. {
  1351. struct af9015_state *state = d->priv;
  1352. deb_info("%s: \n", __func__);
  1353. /* remove 2nd I2C adapter */
  1354. if (d->state & DVB_USB_STATE_I2C)
  1355. i2c_del_adapter(&state->i2c_adap);
  1356. }
  1357. static void af9015_usb_device_exit(struct usb_interface *intf)
  1358. {
  1359. struct dvb_usb_device *d = usb_get_intfdata(intf);
  1360. deb_info("%s: \n", __func__);
  1361. /* remove 2nd I2C adapter */
  1362. if (d != NULL && d->desc != NULL)
  1363. af9015_i2c_exit(d);
  1364. dvb_usb_device_exit(intf);
  1365. }
  1366. /* usb specific object needed to register this driver with the usb subsystem */
  1367. static struct usb_driver af9015_usb_driver = {
  1368. .name = "dvb_usb_af9015",
  1369. .probe = af9015_usb_probe,
  1370. .disconnect = af9015_usb_device_exit,
  1371. .id_table = af9015_usb_table,
  1372. };
  1373. /* module stuff */
  1374. static int __init af9015_usb_module_init(void)
  1375. {
  1376. int ret;
  1377. ret = usb_register(&af9015_usb_driver);
  1378. if (ret)
  1379. err("module init failed:%d", ret);
  1380. return ret;
  1381. }
  1382. static void __exit af9015_usb_module_exit(void)
  1383. {
  1384. /* deregister this driver from the USB subsystem */
  1385. usb_deregister(&af9015_usb_driver);
  1386. }
  1387. module_init(af9015_usb_module_init);
  1388. module_exit(af9015_usb_module_exit);
  1389. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  1390. MODULE_DESCRIPTION("Driver for Afatech AF9015 DVB-T");
  1391. MODULE_LICENSE("GPL");