af9015.c 39 KB

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