af9015.c 38 KB

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