af9015.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654
  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[3];
  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[4+3*16+1], 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. af9015_properties[i].rc_key_map =
  684. af9015_rc_keys_twinhan;
  685. af9015_properties[i].rc_key_map_size =
  686. ARRAY_SIZE(af9015_rc_keys_twinhan);
  687. af9015_config.ir_table =
  688. af9015_ir_table_twinhan;
  689. af9015_config.ir_table_size =
  690. ARRAY_SIZE(af9015_ir_table_twinhan);
  691. break;
  692. case USB_VID_KWORLD_2:
  693. /* TODO: use correct rc keys */
  694. af9015_properties[i].rc_key_map =
  695. af9015_rc_keys_twinhan;
  696. af9015_properties[i].rc_key_map_size =
  697. ARRAY_SIZE(af9015_rc_keys_twinhan);
  698. af9015_config.ir_table = af9015_ir_table_kworld;
  699. af9015_config.ir_table_size =
  700. ARRAY_SIZE(af9015_ir_table_kworld);
  701. break;
  702. /* Check USB manufacturer and product strings and try
  703. to determine correct remote in case of chip vendor
  704. reference IDs are used. */
  705. case USB_VID_AFATECH:
  706. memset(manufacturer, 0, sizeof(manufacturer));
  707. usb_string(udev, udev->descriptor.iManufacturer,
  708. manufacturer, sizeof(manufacturer));
  709. if (!strcmp("Geniatech", manufacturer)) {
  710. /* iManufacturer 1 Geniatech
  711. iProduct 2 AF9015 */
  712. af9015_properties[i].rc_key_map =
  713. af9015_rc_keys_mygictv;
  714. af9015_properties[i].rc_key_map_size =
  715. ARRAY_SIZE(af9015_rc_keys_mygictv);
  716. af9015_config.ir_table =
  717. af9015_ir_table_mygictv;
  718. af9015_config.ir_table_size =
  719. ARRAY_SIZE(af9015_ir_table_mygictv);
  720. } else if (!strcmp("MSI", manufacturer)) {
  721. /* iManufacturer 1 MSI
  722. iProduct 2 MSI K-VOX */
  723. af9015_properties[i].rc_key_map =
  724. af9015_rc_keys_msi;
  725. af9015_properties[i].rc_key_map_size =
  726. ARRAY_SIZE(af9015_rc_keys_msi);
  727. af9015_config.ir_table =
  728. af9015_ir_table_msi;
  729. af9015_config.ir_table_size =
  730. ARRAY_SIZE(af9015_ir_table_msi);
  731. } else if (udev->descriptor.idProduct ==
  732. cpu_to_le16(USB_PID_TREKSTOR_DVBT)) {
  733. af9015_properties[i].rc_key_map =
  734. af9015_rc_keys_trekstor;
  735. af9015_properties[i].rc_key_map_size =
  736. ARRAY_SIZE(af9015_rc_keys_trekstor);
  737. af9015_config.ir_table =
  738. af9015_ir_table_trekstor;
  739. af9015_config.ir_table_size =
  740. ARRAY_SIZE(af9015_ir_table_trekstor);
  741. }
  742. break;
  743. case USB_VID_AVERMEDIA:
  744. af9015_properties[i].rc_key_map =
  745. af9015_rc_keys_avermedia;
  746. af9015_properties[i].rc_key_map_size =
  747. ARRAY_SIZE(af9015_rc_keys_avermedia);
  748. af9015_config.ir_table =
  749. af9015_ir_table_avermedia;
  750. af9015_config.ir_table_size =
  751. ARRAY_SIZE(af9015_ir_table_avermedia);
  752. break;
  753. }
  754. }
  755. }
  756. /* TS mode - one or two receivers */
  757. req.addr = AF9015_EEPROM_TS_MODE;
  758. ret = af9015_rw_udev(udev, &req);
  759. if (ret)
  760. goto error;
  761. af9015_config.dual_mode = val;
  762. deb_info("%s: TS mode:%d\n", __func__, af9015_config.dual_mode);
  763. /* Set adapter0 buffer size according to USB port speed, adapter1 buffer
  764. size can be static because it is enabled only USB2.0 */
  765. for (i = 0; i < af9015_properties_count; i++) {
  766. /* USB1.1 set smaller buffersize and disable 2nd adapter */
  767. if (udev->speed == USB_SPEED_FULL) {
  768. af9015_properties[i].adapter[0].stream.u.bulk.buffersize
  769. = TS_USB11_MAX_PACKET_SIZE;
  770. /* disable 2nd adapter because we don't have
  771. PID-filters */
  772. af9015_config.dual_mode = 0;
  773. } else {
  774. af9015_properties[i].adapter[0].stream.u.bulk.buffersize
  775. = TS_USB20_MAX_PACKET_SIZE;
  776. }
  777. }
  778. if (af9015_config.dual_mode) {
  779. /* read 2nd demodulator I2C address */
  780. req.addr = AF9015_EEPROM_DEMOD2_I2C;
  781. ret = af9015_rw_udev(udev, &req);
  782. if (ret)
  783. goto error;
  784. af9015_af9013_config[1].demod_address = val;
  785. /* enable 2nd adapter */
  786. for (i = 0; i < af9015_properties_count; i++)
  787. af9015_properties[i].num_adapters = 2;
  788. } else {
  789. /* disable 2nd adapter */
  790. for (i = 0; i < af9015_properties_count; i++)
  791. af9015_properties[i].num_adapters = 1;
  792. }
  793. for (i = 0; i < af9015_properties[0].num_adapters; i++) {
  794. if (i == 1)
  795. offset = AF9015_EEPROM_OFFSET;
  796. /* xtal */
  797. req.addr = AF9015_EEPROM_XTAL_TYPE1 + offset;
  798. ret = af9015_rw_udev(udev, &req);
  799. if (ret)
  800. goto error;
  801. switch (val) {
  802. case 0:
  803. af9015_af9013_config[i].adc_clock = 28800;
  804. break;
  805. case 1:
  806. af9015_af9013_config[i].adc_clock = 20480;
  807. break;
  808. case 2:
  809. af9015_af9013_config[i].adc_clock = 28000;
  810. break;
  811. case 3:
  812. af9015_af9013_config[i].adc_clock = 25000;
  813. break;
  814. };
  815. deb_info("%s: [%d] xtal:%d set adc_clock:%d\n", __func__, i,
  816. val, af9015_af9013_config[i].adc_clock);
  817. /* tuner IF */
  818. req.addr = AF9015_EEPROM_IF1H + offset;
  819. ret = af9015_rw_udev(udev, &req);
  820. if (ret)
  821. goto error;
  822. af9015_af9013_config[i].tuner_if = val << 8;
  823. req.addr = AF9015_EEPROM_IF1L + offset;
  824. ret = af9015_rw_udev(udev, &req);
  825. if (ret)
  826. goto error;
  827. af9015_af9013_config[i].tuner_if += val;
  828. deb_info("%s: [%d] IF1:%d\n", __func__, i,
  829. af9015_af9013_config[0].tuner_if);
  830. /* MT2060 IF1 */
  831. req.addr = AF9015_EEPROM_MT2060_IF1H + offset;
  832. ret = af9015_rw_udev(udev, &req);
  833. if (ret)
  834. goto error;
  835. af9015_config.mt2060_if1[i] = val << 8;
  836. req.addr = AF9015_EEPROM_MT2060_IF1L + offset;
  837. ret = af9015_rw_udev(udev, &req);
  838. if (ret)
  839. goto error;
  840. af9015_config.mt2060_if1[i] += val;
  841. deb_info("%s: [%d] MT2060 IF1:%d\n", __func__, i,
  842. af9015_config.mt2060_if1[i]);
  843. /* tuner */
  844. req.addr = AF9015_EEPROM_TUNER_ID1 + offset;
  845. ret = af9015_rw_udev(udev, &req);
  846. if (ret)
  847. goto error;
  848. switch (val) {
  849. case AF9013_TUNER_ENV77H11D5:
  850. case AF9013_TUNER_MT2060:
  851. case AF9013_TUNER_QT1010:
  852. case AF9013_TUNER_UNKNOWN:
  853. case AF9013_TUNER_MT2060_2:
  854. case AF9013_TUNER_TDA18271:
  855. case AF9013_TUNER_QT1010A:
  856. af9015_af9013_config[i].rf_spec_inv = 1;
  857. break;
  858. case AF9013_TUNER_MXL5003D:
  859. case AF9013_TUNER_MXL5005D:
  860. case AF9013_TUNER_MXL5005R:
  861. af9015_af9013_config[i].rf_spec_inv = 0;
  862. break;
  863. case AF9013_TUNER_MC44S803:
  864. af9015_af9013_config[i].gpio[1] = AF9013_GPIO_LO;
  865. af9015_af9013_config[i].rf_spec_inv = 1;
  866. break;
  867. default:
  868. warn("tuner id:%d not supported, please report!", val);
  869. return -ENODEV;
  870. };
  871. af9015_af9013_config[i].tuner = val;
  872. deb_info("%s: [%d] tuner id:%d\n", __func__, i, val);
  873. }
  874. error:
  875. if (ret)
  876. err("eeprom read failed:%d", ret);
  877. /* AverMedia AVerTV Volar Black HD (A850) device have bad EEPROM
  878. content :-( Override some wrong values here. */
  879. if (le16_to_cpu(udev->descriptor.idVendor) == USB_VID_AVERMEDIA &&
  880. le16_to_cpu(udev->descriptor.idProduct) == USB_PID_AVERMEDIA_A850) {
  881. deb_info("%s: AverMedia A850: overriding config\n", __func__);
  882. /* disable dual mode */
  883. af9015_config.dual_mode = 0;
  884. /* disable 2nd adapter */
  885. for (i = 0; i < af9015_properties_count; i++)
  886. af9015_properties[i].num_adapters = 1;
  887. /* set correct IF */
  888. af9015_af9013_config[0].tuner_if = 4570;
  889. }
  890. return ret;
  891. }
  892. static int af9015_identify_state(struct usb_device *udev,
  893. struct dvb_usb_device_properties *props,
  894. struct dvb_usb_device_description **desc,
  895. int *cold)
  896. {
  897. int ret;
  898. u8 reply;
  899. struct req_t req = {GET_CONFIG, 0, 0, 0, 0, 1, &reply};
  900. ret = af9015_rw_udev(udev, &req);
  901. if (ret)
  902. return ret;
  903. deb_info("%s: reply:%02x\n", __func__, reply);
  904. if (reply == 0x02)
  905. *cold = 0;
  906. else
  907. *cold = 1;
  908. return ret;
  909. }
  910. static int af9015_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
  911. {
  912. u8 buf[8];
  913. struct req_t req = {GET_IR_CODE, 0, 0, 0, 0, sizeof(buf), buf};
  914. struct dvb_usb_rc_key *keymap = d->props.rc_key_map;
  915. int i, ret;
  916. memset(buf, 0, sizeof(buf));
  917. ret = af9015_ctrl_msg(d, &req);
  918. if (ret)
  919. return ret;
  920. *event = 0;
  921. *state = REMOTE_NO_KEY_PRESSED;
  922. for (i = 0; i < d->props.rc_key_map_size; i++) {
  923. if (!buf[1] && keymap[i].custom == buf[0] &&
  924. keymap[i].data == buf[2]) {
  925. *event = keymap[i].event;
  926. *state = REMOTE_KEY_PRESSED;
  927. break;
  928. }
  929. }
  930. if (!buf[1])
  931. deb_rc("%s: %02x %02x %02x %02x %02x %02x %02x %02x\n",
  932. __func__, buf[0], buf[1], buf[2], buf[3], buf[4],
  933. buf[5], buf[6], buf[7]);
  934. return 0;
  935. }
  936. /* init 2nd I2C adapter */
  937. static int af9015_i2c_init(struct dvb_usb_device *d)
  938. {
  939. int ret;
  940. struct af9015_state *state = d->priv;
  941. deb_info("%s:\n", __func__);
  942. strncpy(state->i2c_adap.name, d->desc->name,
  943. sizeof(state->i2c_adap.name));
  944. #ifdef I2C_ADAP_CLASS_TV_DIGITAL
  945. state->i2c_adap.class = I2C_ADAP_CLASS_TV_DIGITAL,
  946. #else
  947. state->i2c_adap.class = I2C_CLASS_TV_DIGITAL,
  948. #endif
  949. state->i2c_adap.algo = d->props.i2c_algo;
  950. state->i2c_adap.algo_data = NULL;
  951. state->i2c_adap.dev.parent = &d->udev->dev;
  952. i2c_set_adapdata(&state->i2c_adap, d);
  953. ret = i2c_add_adapter(&state->i2c_adap);
  954. if (ret < 0)
  955. err("could not add i2c adapter");
  956. return ret;
  957. }
  958. static int af9015_af9013_frontend_attach(struct dvb_usb_adapter *adap)
  959. {
  960. int ret;
  961. struct af9015_state *state = adap->dev->priv;
  962. struct i2c_adapter *i2c_adap;
  963. if (adap->id == 0) {
  964. /* select I2C adapter */
  965. i2c_adap = &adap->dev->i2c_adap;
  966. deb_info("%s: init I2C\n", __func__);
  967. ret = af9015_i2c_init(adap->dev);
  968. /* dump eeprom (debug) */
  969. ret = af9015_eeprom_dump(adap->dev);
  970. if (ret)
  971. return ret;
  972. } else {
  973. /* select I2C adapter */
  974. i2c_adap = &state->i2c_adap;
  975. /* copy firmware to 2nd demodulator */
  976. if (af9015_config.dual_mode) {
  977. ret = af9015_copy_firmware(adap->dev);
  978. if (ret) {
  979. err("firmware copy to 2nd frontend " \
  980. "failed, will disable it");
  981. af9015_config.dual_mode = 0;
  982. return -ENODEV;
  983. }
  984. } else {
  985. return -ENODEV;
  986. }
  987. }
  988. /* attach demodulator */
  989. adap->fe = dvb_attach(af9013_attach, &af9015_af9013_config[adap->id],
  990. i2c_adap);
  991. return adap->fe == NULL ? -ENODEV : 0;
  992. }
  993. static struct mt2060_config af9015_mt2060_config = {
  994. .i2c_address = 0xc0,
  995. .clock_out = 0,
  996. };
  997. static struct qt1010_config af9015_qt1010_config = {
  998. .i2c_address = 0xc4,
  999. };
  1000. static struct tda18271_config af9015_tda18271_config = {
  1001. .gate = TDA18271_GATE_DIGITAL,
  1002. .small_i2c = 1,
  1003. };
  1004. static struct mxl5005s_config af9015_mxl5003_config = {
  1005. .i2c_address = 0xc6,
  1006. .if_freq = IF_FREQ_4570000HZ,
  1007. .xtal_freq = CRYSTAL_FREQ_16000000HZ,
  1008. .agc_mode = MXL_SINGLE_AGC,
  1009. .tracking_filter = MXL_TF_DEFAULT,
  1010. .rssi_enable = MXL_RSSI_ENABLE,
  1011. .cap_select = MXL_CAP_SEL_ENABLE,
  1012. .div_out = MXL_DIV_OUT_4,
  1013. .clock_out = MXL_CLOCK_OUT_DISABLE,
  1014. .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
  1015. .top = MXL5005S_TOP_25P2,
  1016. .mod_mode = MXL_DIGITAL_MODE,
  1017. .if_mode = MXL_ZERO_IF,
  1018. .AgcMasterByte = 0x00,
  1019. };
  1020. static struct mxl5005s_config af9015_mxl5005_config = {
  1021. .i2c_address = 0xc6,
  1022. .if_freq = IF_FREQ_4570000HZ,
  1023. .xtal_freq = CRYSTAL_FREQ_16000000HZ,
  1024. .agc_mode = MXL_SINGLE_AGC,
  1025. .tracking_filter = MXL_TF_OFF,
  1026. .rssi_enable = MXL_RSSI_ENABLE,
  1027. .cap_select = MXL_CAP_SEL_ENABLE,
  1028. .div_out = MXL_DIV_OUT_4,
  1029. .clock_out = MXL_CLOCK_OUT_DISABLE,
  1030. .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
  1031. .top = MXL5005S_TOP_25P2,
  1032. .mod_mode = MXL_DIGITAL_MODE,
  1033. .if_mode = MXL_ZERO_IF,
  1034. .AgcMasterByte = 0x00,
  1035. };
  1036. static struct mc44s803_config af9015_mc44s803_config = {
  1037. .i2c_address = 0xc0,
  1038. .dig_out = 1,
  1039. };
  1040. static int af9015_tuner_attach(struct dvb_usb_adapter *adap)
  1041. {
  1042. struct af9015_state *state = adap->dev->priv;
  1043. struct i2c_adapter *i2c_adap;
  1044. int ret;
  1045. deb_info("%s: \n", __func__);
  1046. /* select I2C adapter */
  1047. if (adap->id == 0)
  1048. i2c_adap = &adap->dev->i2c_adap;
  1049. else
  1050. i2c_adap = &state->i2c_adap;
  1051. switch (af9015_af9013_config[adap->id].tuner) {
  1052. case AF9013_TUNER_MT2060:
  1053. case AF9013_TUNER_MT2060_2:
  1054. ret = dvb_attach(mt2060_attach, adap->fe, i2c_adap,
  1055. &af9015_mt2060_config,
  1056. af9015_config.mt2060_if1[adap->id])
  1057. == NULL ? -ENODEV : 0;
  1058. break;
  1059. case AF9013_TUNER_QT1010:
  1060. case AF9013_TUNER_QT1010A:
  1061. ret = dvb_attach(qt1010_attach, adap->fe, i2c_adap,
  1062. &af9015_qt1010_config) == NULL ? -ENODEV : 0;
  1063. break;
  1064. case AF9013_TUNER_TDA18271:
  1065. ret = dvb_attach(tda18271_attach, adap->fe, 0xc0, i2c_adap,
  1066. &af9015_tda18271_config) == NULL ? -ENODEV : 0;
  1067. break;
  1068. case AF9013_TUNER_MXL5003D:
  1069. ret = dvb_attach(mxl5005s_attach, adap->fe, i2c_adap,
  1070. &af9015_mxl5003_config) == NULL ? -ENODEV : 0;
  1071. break;
  1072. case AF9013_TUNER_MXL5005D:
  1073. case AF9013_TUNER_MXL5005R:
  1074. ret = dvb_attach(mxl5005s_attach, adap->fe, i2c_adap,
  1075. &af9015_mxl5005_config) == NULL ? -ENODEV : 0;
  1076. break;
  1077. case AF9013_TUNER_ENV77H11D5:
  1078. ret = dvb_attach(dvb_pll_attach, adap->fe, 0xc0, i2c_adap,
  1079. DVB_PLL_TDA665X) == NULL ? -ENODEV : 0;
  1080. break;
  1081. case AF9013_TUNER_MC44S803:
  1082. ret = dvb_attach(mc44s803_attach, adap->fe, i2c_adap,
  1083. &af9015_mc44s803_config) == NULL ? -ENODEV : 0;
  1084. break;
  1085. case AF9013_TUNER_UNKNOWN:
  1086. default:
  1087. ret = -ENODEV;
  1088. err("Unknown tuner id:%d",
  1089. af9015_af9013_config[adap->id].tuner);
  1090. }
  1091. return ret;
  1092. }
  1093. static struct usb_device_id af9015_usb_table[] = {
  1094. /* 0 */{USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9015_9015)},
  1095. {USB_DEVICE(USB_VID_AFATECH, USB_PID_AFATECH_AF9015_9016)},
  1096. {USB_DEVICE(USB_VID_LEADTEK, USB_PID_WINFAST_DTV_DONGLE_GOLD)},
  1097. {USB_DEVICE(USB_VID_PINNACLE, USB_PID_PINNACLE_PCTV71E)},
  1098. {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_399U)},
  1099. /* 5 */{USB_DEVICE(USB_VID_VISIONPLUS,
  1100. USB_PID_TINYTWIN)},
  1101. {USB_DEVICE(USB_VID_VISIONPLUS,
  1102. USB_PID_AZUREWAVE_AD_TU700)},
  1103. {USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_CINERGY_T_USB_XE_REV2)},
  1104. {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_PC160_2T)},
  1105. {USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_VOLAR_X)},
  1106. /* 10 */{USB_DEVICE(USB_VID_XTENSIONS, USB_PID_XTENSIONS_XD_380)},
  1107. {USB_DEVICE(USB_VID_MSI_2, USB_PID_MSI_DIGIVOX_DUO)},
  1108. {USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_VOLAR_X_2)},
  1109. {USB_DEVICE(USB_VID_TELESTAR, USB_PID_TELESTAR_STARSTICK_2)},
  1110. {USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A309)},
  1111. /* 15 */{USB_DEVICE(USB_VID_MSI_2, USB_PID_MSI_DIGI_VOX_MINI_III)},
  1112. {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U)},
  1113. {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_2)},
  1114. {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_395U_3)},
  1115. {USB_DEVICE(USB_VID_AFATECH, USB_PID_TREKSTOR_DVBT)},
  1116. /* 20 */{USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A850)},
  1117. {USB_DEVICE(USB_VID_AVERMEDIA, USB_PID_AVERMEDIA_A805)},
  1118. {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_CONCEPTRONIC_CTVDIGRCU)},
  1119. {USB_DEVICE(USB_VID_KWORLD_2, USB_PID_KWORLD_MC810)},
  1120. {USB_DEVICE(USB_VID_KYE, USB_PID_GENIUS_TVGO_DVB_T03)},
  1121. {0},
  1122. };
  1123. MODULE_DEVICE_TABLE(usb, af9015_usb_table);
  1124. static struct dvb_usb_device_properties af9015_properties[] = {
  1125. {
  1126. .caps = DVB_USB_IS_AN_I2C_ADAPTER,
  1127. .usb_ctrl = DEVICE_SPECIFIC,
  1128. .download_firmware = af9015_download_firmware,
  1129. .firmware = "dvb-usb-af9015.fw",
  1130. .no_reconnect = 1,
  1131. .size_of_priv = sizeof(struct af9015_state), \
  1132. .num_adapters = 2,
  1133. .adapter = {
  1134. {
  1135. .caps = DVB_USB_ADAP_HAS_PID_FILTER |
  1136. DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
  1137. .pid_filter_count = 32,
  1138. .pid_filter = af9015_pid_filter,
  1139. .pid_filter_ctrl = af9015_pid_filter_ctrl,
  1140. .frontend_attach =
  1141. af9015_af9013_frontend_attach,
  1142. .tuner_attach = af9015_tuner_attach,
  1143. .stream = {
  1144. .type = USB_BULK,
  1145. .count = 6,
  1146. .endpoint = 0x84,
  1147. },
  1148. },
  1149. {
  1150. .frontend_attach =
  1151. af9015_af9013_frontend_attach,
  1152. .tuner_attach = af9015_tuner_attach,
  1153. .stream = {
  1154. .type = USB_BULK,
  1155. .count = 6,
  1156. .endpoint = 0x85,
  1157. .u = {
  1158. .bulk = {
  1159. .buffersize =
  1160. TS_USB20_MAX_PACKET_SIZE,
  1161. }
  1162. }
  1163. },
  1164. }
  1165. },
  1166. .identify_state = af9015_identify_state,
  1167. .rc_query = af9015_rc_query,
  1168. .rc_interval = 150,
  1169. .i2c_algo = &af9015_i2c_algo,
  1170. .num_device_descs = 9, /* max 9 */
  1171. .devices = {
  1172. {
  1173. .name = "Afatech AF9015 DVB-T USB2.0 stick",
  1174. .cold_ids = {&af9015_usb_table[0],
  1175. &af9015_usb_table[1], NULL},
  1176. .warm_ids = {NULL},
  1177. },
  1178. {
  1179. .name = "Leadtek WinFast DTV Dongle Gold",
  1180. .cold_ids = {&af9015_usb_table[2], NULL},
  1181. .warm_ids = {NULL},
  1182. },
  1183. {
  1184. .name = "Pinnacle PCTV 71e",
  1185. .cold_ids = {&af9015_usb_table[3], NULL},
  1186. .warm_ids = {NULL},
  1187. },
  1188. {
  1189. .name = "KWorld PlusTV Dual DVB-T Stick " \
  1190. "(DVB-T 399U)",
  1191. .cold_ids = {&af9015_usb_table[4], NULL},
  1192. .warm_ids = {NULL},
  1193. },
  1194. {
  1195. .name = "DigitalNow TinyTwin DVB-T Receiver",
  1196. .cold_ids = {&af9015_usb_table[5], NULL},
  1197. .warm_ids = {NULL},
  1198. },
  1199. {
  1200. .name = "TwinHan AzureWave AD-TU700(704J)",
  1201. .cold_ids = {&af9015_usb_table[6], NULL},
  1202. .warm_ids = {NULL},
  1203. },
  1204. {
  1205. .name = "TerraTec Cinergy T USB XE",
  1206. .cold_ids = {&af9015_usb_table[7], NULL},
  1207. .warm_ids = {NULL},
  1208. },
  1209. {
  1210. .name = "KWorld PlusTV Dual DVB-T PCI " \
  1211. "(DVB-T PC160-2T)",
  1212. .cold_ids = {&af9015_usb_table[8], NULL},
  1213. .warm_ids = {NULL},
  1214. },
  1215. {
  1216. .name = "AVerMedia AVerTV DVB-T Volar X",
  1217. .cold_ids = {&af9015_usb_table[9], NULL},
  1218. .warm_ids = {NULL},
  1219. },
  1220. }
  1221. }, {
  1222. .caps = DVB_USB_IS_AN_I2C_ADAPTER,
  1223. .usb_ctrl = DEVICE_SPECIFIC,
  1224. .download_firmware = af9015_download_firmware,
  1225. .firmware = "dvb-usb-af9015.fw",
  1226. .no_reconnect = 1,
  1227. .size_of_priv = sizeof(struct af9015_state), \
  1228. .num_adapters = 2,
  1229. .adapter = {
  1230. {
  1231. .caps = DVB_USB_ADAP_HAS_PID_FILTER |
  1232. DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
  1233. .pid_filter_count = 32,
  1234. .pid_filter = af9015_pid_filter,
  1235. .pid_filter_ctrl = af9015_pid_filter_ctrl,
  1236. .frontend_attach =
  1237. af9015_af9013_frontend_attach,
  1238. .tuner_attach = af9015_tuner_attach,
  1239. .stream = {
  1240. .type = USB_BULK,
  1241. .count = 6,
  1242. .endpoint = 0x84,
  1243. },
  1244. },
  1245. {
  1246. .frontend_attach =
  1247. af9015_af9013_frontend_attach,
  1248. .tuner_attach = af9015_tuner_attach,
  1249. .stream = {
  1250. .type = USB_BULK,
  1251. .count = 6,
  1252. .endpoint = 0x85,
  1253. .u = {
  1254. .bulk = {
  1255. .buffersize =
  1256. TS_USB20_MAX_PACKET_SIZE,
  1257. }
  1258. }
  1259. },
  1260. }
  1261. },
  1262. .identify_state = af9015_identify_state,
  1263. .rc_query = af9015_rc_query,
  1264. .rc_interval = 150,
  1265. .i2c_algo = &af9015_i2c_algo,
  1266. .num_device_descs = 9, /* max 9 */
  1267. .devices = {
  1268. {
  1269. .name = "Xtensions XD-380",
  1270. .cold_ids = {&af9015_usb_table[10], NULL},
  1271. .warm_ids = {NULL},
  1272. },
  1273. {
  1274. .name = "MSI DIGIVOX Duo",
  1275. .cold_ids = {&af9015_usb_table[11], NULL},
  1276. .warm_ids = {NULL},
  1277. },
  1278. {
  1279. .name = "Fujitsu-Siemens Slim Mobile USB DVB-T",
  1280. .cold_ids = {&af9015_usb_table[12], NULL},
  1281. .warm_ids = {NULL},
  1282. },
  1283. {
  1284. .name = "Telestar Starstick 2",
  1285. .cold_ids = {&af9015_usb_table[13], NULL},
  1286. .warm_ids = {NULL},
  1287. },
  1288. {
  1289. .name = "AVerMedia A309",
  1290. .cold_ids = {&af9015_usb_table[14], NULL},
  1291. .warm_ids = {NULL},
  1292. },
  1293. {
  1294. .name = "MSI Digi VOX mini III",
  1295. .cold_ids = {&af9015_usb_table[15], NULL},
  1296. .warm_ids = {NULL},
  1297. },
  1298. {
  1299. .name = "KWorld USB DVB-T TV Stick II " \
  1300. "(VS-DVB-T 395U)",
  1301. .cold_ids = {&af9015_usb_table[16],
  1302. &af9015_usb_table[17],
  1303. &af9015_usb_table[18], NULL},
  1304. .warm_ids = {NULL},
  1305. },
  1306. {
  1307. .name = "TrekStor DVB-T USB Stick",
  1308. .cold_ids = {&af9015_usb_table[19], NULL},
  1309. .warm_ids = {NULL},
  1310. },
  1311. {
  1312. .name = "AverMedia AVerTV Volar Black HD " \
  1313. "(A850)",
  1314. .cold_ids = {&af9015_usb_table[20], NULL},
  1315. .warm_ids = {NULL},
  1316. },
  1317. }
  1318. }, {
  1319. .caps = DVB_USB_IS_AN_I2C_ADAPTER,
  1320. .usb_ctrl = DEVICE_SPECIFIC,
  1321. .download_firmware = af9015_download_firmware,
  1322. .firmware = "dvb-usb-af9015.fw",
  1323. .no_reconnect = 1,
  1324. .size_of_priv = sizeof(struct af9015_state), \
  1325. .num_adapters = 2,
  1326. .adapter = {
  1327. {
  1328. .caps = DVB_USB_ADAP_HAS_PID_FILTER |
  1329. DVB_USB_ADAP_PID_FILTER_CAN_BE_TURNED_OFF,
  1330. .pid_filter_count = 32,
  1331. .pid_filter = af9015_pid_filter,
  1332. .pid_filter_ctrl = af9015_pid_filter_ctrl,
  1333. .frontend_attach =
  1334. af9015_af9013_frontend_attach,
  1335. .tuner_attach = af9015_tuner_attach,
  1336. .stream = {
  1337. .type = USB_BULK,
  1338. .count = 6,
  1339. .endpoint = 0x84,
  1340. },
  1341. },
  1342. {
  1343. .frontend_attach =
  1344. af9015_af9013_frontend_attach,
  1345. .tuner_attach = af9015_tuner_attach,
  1346. .stream = {
  1347. .type = USB_BULK,
  1348. .count = 6,
  1349. .endpoint = 0x85,
  1350. .u = {
  1351. .bulk = {
  1352. .buffersize =
  1353. TS_USB20_MAX_PACKET_SIZE,
  1354. }
  1355. }
  1356. },
  1357. }
  1358. },
  1359. .identify_state = af9015_identify_state,
  1360. .rc_query = af9015_rc_query,
  1361. .rc_interval = 150,
  1362. .i2c_algo = &af9015_i2c_algo,
  1363. .num_device_descs = 4, /* max 9 */
  1364. .devices = {
  1365. {
  1366. .name = "AverMedia AVerTV Volar GPS 805 (A805)",
  1367. .cold_ids = {&af9015_usb_table[21], NULL},
  1368. .warm_ids = {NULL},
  1369. },
  1370. {
  1371. .name = "Conceptronic USB2.0 DVB-T CTVDIGRCU " \
  1372. "V3.0",
  1373. .cold_ids = {&af9015_usb_table[22], NULL},
  1374. .warm_ids = {NULL},
  1375. },
  1376. {
  1377. .name = "KWorld Digial MC-810",
  1378. .cold_ids = {&af9015_usb_table[23], NULL},
  1379. .warm_ids = {NULL},
  1380. },
  1381. {
  1382. .name = "Genius TVGo DVB-T03",
  1383. .cold_ids = {&af9015_usb_table[24], NULL},
  1384. .warm_ids = {NULL},
  1385. },
  1386. }
  1387. },
  1388. };
  1389. static int af9015_usb_probe(struct usb_interface *intf,
  1390. const struct usb_device_id *id)
  1391. {
  1392. int ret = 0;
  1393. struct dvb_usb_device *d = NULL;
  1394. struct usb_device *udev = interface_to_usbdev(intf);
  1395. u8 i;
  1396. deb_info("%s: interface:%d\n", __func__,
  1397. intf->cur_altsetting->desc.bInterfaceNumber);
  1398. /* interface 0 is used by DVB-T receiver and
  1399. interface 1 is for remote controller (HID) */
  1400. if (intf->cur_altsetting->desc.bInterfaceNumber == 0) {
  1401. ret = af9015_read_config(udev);
  1402. if (ret)
  1403. return ret;
  1404. for (i = 0; i < af9015_properties_count; i++) {
  1405. ret = dvb_usb_device_init(intf, &af9015_properties[i],
  1406. THIS_MODULE, &d, adapter_nr);
  1407. if (!ret)
  1408. break;
  1409. if (ret != -ENODEV)
  1410. return ret;
  1411. }
  1412. if (ret)
  1413. return ret;
  1414. if (d)
  1415. ret = af9015_init(d);
  1416. }
  1417. return ret;
  1418. }
  1419. static void af9015_i2c_exit(struct dvb_usb_device *d)
  1420. {
  1421. struct af9015_state *state = d->priv;
  1422. deb_info("%s: \n", __func__);
  1423. /* remove 2nd I2C adapter */
  1424. if (d->state & DVB_USB_STATE_I2C)
  1425. i2c_del_adapter(&state->i2c_adap);
  1426. }
  1427. static void af9015_usb_device_exit(struct usb_interface *intf)
  1428. {
  1429. struct dvb_usb_device *d = usb_get_intfdata(intf);
  1430. deb_info("%s: \n", __func__);
  1431. /* remove 2nd I2C adapter */
  1432. if (d != NULL && d->desc != NULL)
  1433. af9015_i2c_exit(d);
  1434. dvb_usb_device_exit(intf);
  1435. }
  1436. /* usb specific object needed to register this driver with the usb subsystem */
  1437. static struct usb_driver af9015_usb_driver = {
  1438. .name = "dvb_usb_af9015",
  1439. .probe = af9015_usb_probe,
  1440. .disconnect = af9015_usb_device_exit,
  1441. .id_table = af9015_usb_table,
  1442. };
  1443. /* module stuff */
  1444. static int __init af9015_usb_module_init(void)
  1445. {
  1446. int ret;
  1447. ret = usb_register(&af9015_usb_driver);
  1448. if (ret)
  1449. err("module init failed:%d", ret);
  1450. return ret;
  1451. }
  1452. static void __exit af9015_usb_module_exit(void)
  1453. {
  1454. /* deregister this driver from the USB subsystem */
  1455. usb_deregister(&af9015_usb_driver);
  1456. }
  1457. module_init(af9015_usb_module_init);
  1458. module_exit(af9015_usb_module_exit);
  1459. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  1460. MODULE_DESCRIPTION("Driver for Afatech AF9015 DVB-T");
  1461. MODULE_LICENSE("GPL");