af9015.c 43 KB

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