af9015.c 38 KB

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