af9015.c 37 KB

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