lmedm04.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  1. /* DVB USB compliant linux driver for
  2. *
  3. * DM04/QQBOX DVB-S USB BOX LME2510C + SHARP:BS2F7HZ7395
  4. * LME2510C + LG TDQY-P001F
  5. * LME2510C + BS2F7HZ0194
  6. * LME2510 + LG TDQY-P001F
  7. * LME2510 + BS2F7HZ0194
  8. *
  9. * MVB7395 (LME2510C+SHARP:BS2F7HZ7395)
  10. * SHARP:BS2F7HZ7395 = (STV0288+Sharp IX2505V)
  11. *
  12. * MV001F (LME2510+LGTDQY-P001F)
  13. * LG TDQY - P001F =(TDA8263 + TDA10086H)
  14. *
  15. * MVB0001F (LME2510C+LGTDQT-P001F)
  16. *
  17. * MV0194 (LME2510+SHARP:BS2F7HZ0194)
  18. * SHARP:BS2F7HZ0194 = (STV0299+IX2410)
  19. *
  20. * MVB0194 (LME2510C+SHARP0194)
  21. *
  22. * For firmware see Documentation/dvb/lmedm04.txt
  23. *
  24. * I2C addresses:
  25. * 0xd0 - STV0288 - Demodulator
  26. * 0xc0 - Sharp IX2505V - Tuner
  27. * --
  28. * 0x1c - TDA10086 - Demodulator
  29. * 0xc0 - TDA8263 - Tuner
  30. * --
  31. * 0xd0 - STV0299 - Demodulator
  32. * 0xc0 - IX2410 - Tuner
  33. *
  34. *
  35. * VID = 3344 PID LME2510=1122 LME2510C=1120
  36. *
  37. * Copyright (C) 2010 Malcolm Priestley (tvboxspy@gmail.com)
  38. * LME2510(C)(C) Leaguerme (Shenzhen) MicroElectronics Co., Ltd.
  39. *
  40. * This program is free software; you can redistribute it and/or modify
  41. * it under the terms of the GNU General Public License Version 2, as
  42. * published by the Free Software Foundation.
  43. *
  44. * This program is distributed in the hope that it will be useful,
  45. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  46. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  47. * GNU General Public License for more details.
  48. *
  49. * You should have received a copy of the GNU General Public License
  50. * along with this program; if not, write to the Free Software
  51. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  52. *
  53. *
  54. * see Documentation/dvb/README.dvb-usb for more information
  55. *
  56. * Known Issues :
  57. * LME2510: Non Intel USB chipsets fail to maintain High Speed on
  58. * Boot or Hot Plug.
  59. *
  60. * QQbox suffers from noise on LNB voltage.
  61. *
  62. * LME2510: SHARP:BS2F7HZ0194(MV0194) cannot cold reset and share system
  63. * with other tuners. After a cold reset streaming will not start.
  64. *
  65. * PID functions have been removed from this driver version due to
  66. * problems with different firmware and application versions.
  67. */
  68. #define DVB_USB_LOG_PREFIX "LME2510(C)"
  69. #include <linux/usb.h>
  70. #include <linux/usb/input.h>
  71. #include <media/rc-core.h>
  72. #include "dvb-usb.h"
  73. #include "lmedm04.h"
  74. #include "tda826x.h"
  75. #include "tda10086.h"
  76. #include "stv0288.h"
  77. #include "ix2505v.h"
  78. #include "stv0299.h"
  79. #include "dvb-pll.h"
  80. #include "z0194a.h"
  81. /* debug */
  82. static int dvb_usb_lme2510_debug;
  83. #define l_dprintk(var, level, args...) do { \
  84. if ((var >= level)) \
  85. printk(KERN_DEBUG DVB_USB_LOG_PREFIX ": " args); \
  86. } while (0)
  87. #define deb_info(level, args...) l_dprintk(dvb_usb_lme2510_debug, level, args)
  88. #define debug_data_snipet(level, name, p) \
  89. deb_info(level, name" (%02x%02x%02x%02x%02x%02x%02x%02x)", \
  90. *p, *(p+1), *(p+2), *(p+3), *(p+4), \
  91. *(p+5), *(p+6), *(p+7));
  92. module_param_named(debug, dvb_usb_lme2510_debug, int, 0644);
  93. MODULE_PARM_DESC(debug, "set debugging level (1=info (or-able))."
  94. DVB_USB_DEBUG_STATUS);
  95. static int dvb_usb_lme2510_firmware;
  96. module_param_named(firmware, dvb_usb_lme2510_firmware, int, 0644);
  97. MODULE_PARM_DESC(firmware, "set default firmware 0=Sharp7395 1=LG");
  98. DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
  99. #define TUNER_DEFAULT 0x0
  100. #define TUNER_LG 0x1
  101. #define TUNER_S7395 0x2
  102. #define TUNER_S0194 0x3
  103. struct lme2510_state {
  104. u8 id;
  105. u8 tuner_config;
  106. u8 signal_lock;
  107. u8 signal_level;
  108. u8 signal_sn;
  109. u8 time_key;
  110. u8 i2c_talk_onoff;
  111. u8 i2c_gate;
  112. u8 i2c_tuner_gate_w;
  113. u8 i2c_tuner_gate_r;
  114. u8 i2c_tuner_addr;
  115. u8 stream_on;
  116. void *buffer;
  117. struct urb *lme_urb;
  118. void *usb_buffer;
  119. };
  120. static int lme2510_bulk_write(struct usb_device *dev,
  121. u8 *snd, int len, u8 pipe)
  122. {
  123. int ret, actual_l;
  124. ret = usb_bulk_msg(dev, usb_sndbulkpipe(dev, pipe),
  125. snd, len , &actual_l, 100);
  126. return ret;
  127. }
  128. static int lme2510_bulk_read(struct usb_device *dev,
  129. u8 *rev, int len, u8 pipe)
  130. {
  131. int ret, actual_l;
  132. ret = usb_bulk_msg(dev, usb_rcvbulkpipe(dev, pipe),
  133. rev, len , &actual_l, 200);
  134. return ret;
  135. }
  136. static int lme2510_usb_talk(struct dvb_usb_device *d,
  137. u8 *wbuf, int wlen, u8 *rbuf, int rlen)
  138. {
  139. struct lme2510_state *st = d->priv;
  140. u8 *buff;
  141. int ret = 0;
  142. if (st->usb_buffer == NULL) {
  143. st->usb_buffer = kmalloc(512, GFP_KERNEL);
  144. if (st->usb_buffer == NULL) {
  145. info("MEM Error no memory");
  146. return -ENOMEM;
  147. }
  148. }
  149. buff = st->usb_buffer;
  150. /* the read/write capped at 512 */
  151. memcpy(buff, wbuf, (wlen > 512) ? 512 : wlen);
  152. ret = mutex_lock_interruptible(&d->usb_mutex);
  153. if (ret < 0)
  154. return -EAGAIN;
  155. ret |= usb_clear_halt(d->udev, usb_sndbulkpipe(d->udev, 0x01));
  156. ret |= lme2510_bulk_write(d->udev, buff, wlen , 0x01);
  157. msleep(10);
  158. ret |= usb_clear_halt(d->udev, usb_rcvbulkpipe(d->udev, 0x01));
  159. ret |= lme2510_bulk_read(d->udev, buff, (rlen > 512) ?
  160. 512 : rlen , 0x01);
  161. if (rlen > 0)
  162. memcpy(rbuf, buff, rlen);
  163. mutex_unlock(&d->usb_mutex);
  164. return (ret < 0) ? -ENODEV : 0;
  165. }
  166. static int lme2510_stream_restart(struct dvb_usb_device *d)
  167. {
  168. static u8 stream_on[] = LME_ST_ON_W;
  169. int ret;
  170. u8 rbuff[10];
  171. /*Restart Stream Command*/
  172. ret = lme2510_usb_talk(d, stream_on, sizeof(stream_on),
  173. rbuff, sizeof(rbuff));
  174. return ret;
  175. }
  176. static int lme2510_remote_keypress(struct dvb_usb_adapter *adap, u32 keypress)
  177. {
  178. struct dvb_usb_device *d = adap->dev;
  179. deb_info(1, "INT Key Keypress =%04x", keypress);
  180. if (keypress > 0)
  181. rc_keydown(d->rc_dev, keypress, 0);
  182. return 0;
  183. }
  184. static void lme2510_int_response(struct urb *lme_urb)
  185. {
  186. struct dvb_usb_adapter *adap = lme_urb->context;
  187. struct lme2510_state *st = adap->dev->priv;
  188. static u8 *ibuf, *rbuf;
  189. int i = 0, offset;
  190. switch (lme_urb->status) {
  191. case 0:
  192. case -ETIMEDOUT:
  193. break;
  194. case -ECONNRESET:
  195. case -ENOENT:
  196. case -ESHUTDOWN:
  197. return;
  198. default:
  199. info("Error %x", lme_urb->status);
  200. break;
  201. }
  202. rbuf = (u8 *) lme_urb->transfer_buffer;
  203. offset = ((lme_urb->actual_length/8) > 4)
  204. ? 4 : (lme_urb->actual_length/8) ;
  205. for (i = 0; i < offset; ++i) {
  206. ibuf = (u8 *)&rbuf[i*8];
  207. deb_info(5, "INT O/S C =%02x C/O=%02x Type =%02x%02x",
  208. offset, i, ibuf[0], ibuf[1]);
  209. switch (ibuf[0]) {
  210. case 0xaa:
  211. debug_data_snipet(1, "INT Remote data snipet in", ibuf);
  212. lme2510_remote_keypress(adap,
  213. (u32)(ibuf[2] << 24) + (ibuf[3] << 16) +
  214. (ibuf[4] << 8) + ibuf[5]);
  215. break;
  216. case 0xbb:
  217. switch (st->tuner_config) {
  218. case TUNER_LG:
  219. if (ibuf[2] > 0)
  220. st->signal_lock = ibuf[2];
  221. st->signal_level = ibuf[4];
  222. st->signal_sn = ibuf[3];
  223. st->time_key = ibuf[7];
  224. break;
  225. case TUNER_S7395:
  226. case TUNER_S0194:
  227. /* Tweak for earlier firmware*/
  228. if (ibuf[1] == 0x03) {
  229. if (ibuf[2] > 1)
  230. st->signal_lock = ibuf[2];
  231. st->signal_level = ibuf[3];
  232. st->signal_sn = ibuf[4];
  233. } else {
  234. st->signal_level = ibuf[4];
  235. st->signal_sn = ibuf[5];
  236. st->signal_lock =
  237. (st->signal_lock & 0xf7) +
  238. ((ibuf[2] & 0x01) << 0x03);
  239. }
  240. break;
  241. default:
  242. break;
  243. }
  244. debug_data_snipet(5, "INT Remote data snipet in", ibuf);
  245. break;
  246. case 0xcc:
  247. debug_data_snipet(1, "INT Control data snipet", ibuf);
  248. break;
  249. default:
  250. debug_data_snipet(1, "INT Unknown data snipet", ibuf);
  251. break;
  252. }
  253. }
  254. usb_submit_urb(lme_urb, GFP_ATOMIC);
  255. }
  256. static int lme2510_int_read(struct dvb_usb_adapter *adap)
  257. {
  258. struct lme2510_state *lme_int = adap->dev->priv;
  259. lme_int->lme_urb = usb_alloc_urb(0, GFP_ATOMIC);
  260. if (lme_int->lme_urb == NULL)
  261. return -ENOMEM;
  262. lme_int->buffer = usb_alloc_coherent(adap->dev->udev, 5000, GFP_ATOMIC,
  263. &lme_int->lme_urb->transfer_dma);
  264. if (lme_int->buffer == NULL)
  265. return -ENOMEM;
  266. usb_fill_int_urb(lme_int->lme_urb,
  267. adap->dev->udev,
  268. usb_rcvintpipe(adap->dev->udev, 0xa),
  269. lme_int->buffer,
  270. 4096,
  271. lme2510_int_response,
  272. adap,
  273. 11);
  274. lme_int->lme_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  275. usb_submit_urb(lme_int->lme_urb, GFP_ATOMIC);
  276. info("INT Interupt Service Started");
  277. return 0;
  278. }
  279. static int lme2510_return_status(struct usb_device *dev)
  280. {
  281. int ret = 0;
  282. u8 data[10] = {0};
  283. ret |= usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  284. 0x06, 0x80, 0x0302, 0x00, data, 0x0006, 200);
  285. info("Firmware Status: %x (%x)", ret , data[2]);
  286. return (ret < 0) ? -ENODEV : data[2];
  287. }
  288. static int lme2510_msg(struct dvb_usb_device *d,
  289. u8 *wbuf, int wlen, u8 *rbuf, int rlen)
  290. {
  291. int ret = 0;
  292. struct lme2510_state *st = d->priv;
  293. if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
  294. return -EAGAIN;
  295. if (st->i2c_talk_onoff == 1) {
  296. ret = lme2510_usb_talk(d, wbuf, wlen, rbuf, rlen);
  297. switch (st->tuner_config) {
  298. case TUNER_LG:
  299. if (wbuf[2] == 0x1c) {
  300. if (wbuf[3] == 0x0e) {
  301. st->signal_lock = rbuf[1];
  302. if ((st->stream_on & 1) &&
  303. (st->signal_lock & 0x10)) {
  304. lme2510_stream_restart(d);
  305. st->i2c_talk_onoff = 0;
  306. }
  307. msleep(80);
  308. }
  309. }
  310. break;
  311. case TUNER_S7395:
  312. if (wbuf[2] == 0xd0) {
  313. if (wbuf[3] == 0x24) {
  314. st->signal_lock = rbuf[1];
  315. if ((st->stream_on & 1) &&
  316. (st->signal_lock & 0x8)) {
  317. lme2510_stream_restart(d);
  318. st->i2c_talk_onoff = 0;
  319. }
  320. }
  321. if ((wbuf[3] != 0x6) & (wbuf[3] != 0x5))
  322. msleep(5);
  323. }
  324. break;
  325. case TUNER_S0194:
  326. if (wbuf[2] == 0xd0) {
  327. if (wbuf[3] == 0x1b) {
  328. st->signal_lock = rbuf[1];
  329. if ((st->stream_on & 1) &&
  330. (st->signal_lock & 0x8)) {
  331. lme2510_stream_restart(d);
  332. st->i2c_talk_onoff = 0;
  333. }
  334. }
  335. }
  336. break;
  337. default:
  338. break;
  339. }
  340. } else {
  341. switch (st->tuner_config) {
  342. case TUNER_LG:
  343. switch (wbuf[3]) {
  344. case 0x0e:
  345. rbuf[0] = 0x55;
  346. rbuf[1] = st->signal_lock;
  347. break;
  348. case 0x43:
  349. rbuf[0] = 0x55;
  350. rbuf[1] = st->signal_level;
  351. break;
  352. case 0x1c:
  353. rbuf[0] = 0x55;
  354. rbuf[1] = st->signal_sn;
  355. break;
  356. case 0x15:
  357. case 0x16:
  358. case 0x17:
  359. case 0x18:
  360. rbuf[0] = 0x55;
  361. rbuf[1] = 0x00;
  362. break;
  363. default:
  364. lme2510_usb_talk(d, wbuf, wlen, rbuf, rlen);
  365. st->i2c_talk_onoff = 1;
  366. break;
  367. }
  368. break;
  369. case TUNER_S7395:
  370. switch (wbuf[3]) {
  371. case 0x10:
  372. rbuf[0] = 0x55;
  373. rbuf[1] = (st->signal_level & 0x80)
  374. ? 0 : (st->signal_level * 2);
  375. break;
  376. case 0x2d:
  377. rbuf[0] = 0x55;
  378. rbuf[1] = st->signal_sn;
  379. break;
  380. case 0x24:
  381. rbuf[0] = 0x55;
  382. rbuf[1] = st->signal_lock;
  383. break;
  384. case 0x2e:
  385. case 0x26:
  386. case 0x27:
  387. rbuf[0] = 0x55;
  388. rbuf[1] = 0x00;
  389. break;
  390. default:
  391. lme2510_usb_talk(d, wbuf, wlen, rbuf, rlen);
  392. st->i2c_talk_onoff = 1;
  393. break;
  394. }
  395. break;
  396. case TUNER_S0194:
  397. switch (wbuf[3]) {
  398. case 0x18:
  399. rbuf[0] = 0x55;
  400. rbuf[1] = (st->signal_level & 0x80)
  401. ? 0 : (st->signal_level * 2);
  402. break;
  403. case 0x24:
  404. rbuf[0] = 0x55;
  405. rbuf[1] = st->signal_sn;
  406. break;
  407. case 0x1b:
  408. rbuf[0] = 0x55;
  409. rbuf[1] = st->signal_lock;
  410. break;
  411. case 0x19:
  412. case 0x25:
  413. case 0x1e:
  414. case 0x1d:
  415. rbuf[0] = 0x55;
  416. rbuf[1] = 0x00;
  417. break;
  418. default:
  419. lme2510_usb_talk(d, wbuf, wlen, rbuf, rlen);
  420. st->i2c_talk_onoff = 1;
  421. break;
  422. }
  423. break;
  424. default:
  425. break;
  426. }
  427. deb_info(4, "I2C From Interupt Message out(%02x) in(%02x)",
  428. wbuf[3], rbuf[1]);
  429. }
  430. mutex_unlock(&d->i2c_mutex);
  431. return ret;
  432. }
  433. static int lme2510_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
  434. int num)
  435. {
  436. struct dvb_usb_device *d = i2c_get_adapdata(adap);
  437. struct lme2510_state *st = d->priv;
  438. static u8 obuf[64], ibuf[512];
  439. int i, read, read_o;
  440. u16 len;
  441. u8 gate = st->i2c_gate;
  442. if (gate == 0)
  443. gate = 5;
  444. if (num > 2)
  445. warn("more than 2 i2c messages"
  446. "at a time is not handled yet. TODO.");
  447. for (i = 0; i < num; i++) {
  448. read_o = 1 & (msg[i].flags & I2C_M_RD);
  449. read = i+1 < num && (msg[i+1].flags & I2C_M_RD);
  450. read |= read_o;
  451. gate = (msg[i].addr == st->i2c_tuner_addr)
  452. ? (read) ? st->i2c_tuner_gate_r
  453. : st->i2c_tuner_gate_w
  454. : st->i2c_gate;
  455. obuf[0] = gate | (read << 7);
  456. if (gate == 5)
  457. obuf[1] = (read) ? 2 : msg[i].len + 1;
  458. else
  459. obuf[1] = msg[i].len + read + 1;
  460. obuf[2] = msg[i].addr;
  461. if (read) {
  462. if (read_o)
  463. len = 3;
  464. else {
  465. memcpy(&obuf[3], msg[i].buf, msg[i].len);
  466. obuf[msg[i].len+3] = msg[i+1].len;
  467. len = msg[i].len+4;
  468. }
  469. } else {
  470. memcpy(&obuf[3], msg[i].buf, msg[i].len);
  471. len = msg[i].len+3;
  472. }
  473. if (lme2510_msg(d, obuf, len, ibuf, 512) < 0) {
  474. deb_info(1, "i2c transfer failed.");
  475. return -EAGAIN;
  476. }
  477. if (read) {
  478. if (read_o)
  479. memcpy(msg[i].buf, &ibuf[1], msg[i].len);
  480. else {
  481. memcpy(msg[i+1].buf, &ibuf[1], msg[i+1].len);
  482. i++;
  483. }
  484. }
  485. }
  486. return i;
  487. }
  488. static u32 lme2510_i2c_func(struct i2c_adapter *adapter)
  489. {
  490. return I2C_FUNC_I2C;
  491. }
  492. static struct i2c_algorithm lme2510_i2c_algo = {
  493. .master_xfer = lme2510_i2c_xfer,
  494. .functionality = lme2510_i2c_func,
  495. };
  496. /* Callbacks for DVB USB */
  497. static int lme2510_identify_state(struct usb_device *udev,
  498. struct dvb_usb_device_properties *props,
  499. struct dvb_usb_device_description **desc,
  500. int *cold)
  501. {
  502. *cold = 0;
  503. return 0;
  504. }
  505. static int lme2510_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
  506. {
  507. struct lme2510_state *st = adap->dev->priv;
  508. static u8 clear_reg_3[] = LME_CLEAR_PID;
  509. static u8 rbuf[1];
  510. int ret = 0, rlen = sizeof(rbuf);
  511. deb_info(1, "STM (%02x)", onoff);
  512. /* Streaming is started by FE_HAS_LOCK */
  513. if (onoff == 1)
  514. st->stream_on = 1;
  515. else {
  516. deb_info(1, "STM Steam Off");
  517. /* mutex is here only to avoid collision with I2C */
  518. ret = mutex_lock_interruptible(&adap->dev->i2c_mutex);
  519. ret |= lme2510_usb_talk(adap->dev, clear_reg_3,
  520. sizeof(clear_reg_3), rbuf, rlen);
  521. st->stream_on = 0;
  522. st->i2c_talk_onoff = 1;
  523. mutex_unlock(&adap->dev->i2c_mutex);
  524. }
  525. return (ret < 0) ? -ENODEV : 0;
  526. }
  527. static int lme2510_int_service(struct dvb_usb_adapter *adap)
  528. {
  529. struct dvb_usb_device *d = adap->dev;
  530. struct rc_dev *rc;
  531. int ret;
  532. info("STA Configuring Remote");
  533. rc = rc_allocate_device();
  534. if (!rc)
  535. return -ENOMEM;
  536. usb_make_path(d->udev, d->rc_phys, sizeof(d->rc_phys));
  537. strlcat(d->rc_phys, "/ir0", sizeof(d->rc_phys));
  538. rc->input_name = "LME2510 Remote Control";
  539. rc->input_phys = d->rc_phys;
  540. rc->map_name = RC_MAP_LME2510;
  541. rc->driver_name = "LME 2510";
  542. usb_to_input_id(d->udev, &rc->input_id);
  543. ret = rc_register_device(rc);
  544. if (ret) {
  545. rc_free_device(rc);
  546. return ret;
  547. }
  548. d->rc_dev = rc;
  549. /* Start the Interupt */
  550. ret = lme2510_int_read(adap);
  551. if (ret < 0) {
  552. rc_unregister_device(rc);
  553. info("INT Unable to start Interupt Service");
  554. return -ENODEV;
  555. }
  556. return 0;
  557. }
  558. static u8 check_sum(u8 *p, u8 len)
  559. {
  560. u8 sum = 0;
  561. while (len--)
  562. sum += *p++;
  563. return sum;
  564. }
  565. static int lme2510_download_firmware(struct usb_device *dev,
  566. const struct firmware *fw)
  567. {
  568. int ret = 0;
  569. u8 data[512] = {0};
  570. u16 j, wlen, len_in, start, end;
  571. u8 packet_size, dlen, i;
  572. u8 *fw_data;
  573. packet_size = 0x31;
  574. len_in = 1;
  575. info("FRM Starting Firmware Download");
  576. for (i = 1; i < 3; i++) {
  577. start = (i == 1) ? 0 : 512;
  578. end = (i == 1) ? 512 : fw->size;
  579. for (j = start; j < end; j += (packet_size+1)) {
  580. fw_data = (u8 *)(fw->data + j);
  581. if ((end - j) > packet_size) {
  582. data[0] = i;
  583. dlen = packet_size;
  584. } else {
  585. data[0] = i | 0x80;
  586. dlen = (u8)(end - j)-1;
  587. }
  588. data[1] = dlen;
  589. memcpy(&data[2], fw_data, dlen+1);
  590. wlen = (u8) dlen + 4;
  591. data[wlen-1] = check_sum(fw_data, dlen+1);
  592. deb_info(1, "Data S=%02x:E=%02x CS= %02x", data[3],
  593. data[dlen+2], data[dlen+3]);
  594. ret |= lme2510_bulk_write(dev, data, wlen, 1);
  595. ret |= lme2510_bulk_read(dev, data, len_in , 1);
  596. ret |= (data[0] == 0x88) ? 0 : -1;
  597. }
  598. }
  599. usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  600. 0x06, 0x80, 0x0200, 0x00, data, 0x0109, 1000);
  601. data[0] = 0x8a;
  602. len_in = 1;
  603. msleep(2000);
  604. ret |= lme2510_bulk_write(dev, data , len_in, 1); /*Resetting*/
  605. ret |= lme2510_bulk_read(dev, data, len_in, 1);
  606. msleep(400);
  607. if (ret < 0)
  608. info("FRM Firmware Download Failed (%04x)" , ret);
  609. else
  610. info("FRM Firmware Download Completed - Resetting Device");
  611. return (ret < 0) ? -ENODEV : 0;
  612. }
  613. static void lme_coldreset(struct usb_device *dev)
  614. {
  615. int ret = 0, len_in;
  616. u8 data[512] = {0};
  617. data[0] = 0x0a;
  618. len_in = 1;
  619. info("FRM Firmware Cold Reset");
  620. ret |= lme2510_bulk_write(dev, data , len_in, 1); /*Cold Resetting*/
  621. ret |= lme2510_bulk_read(dev, data, len_in, 1);
  622. return;
  623. }
  624. static int lme_firmware_switch(struct usb_device *udev, int cold)
  625. {
  626. const struct firmware *fw = NULL;
  627. const char fw_c_s7395[] = "dvb-usb-lme2510c-s7395.fw";
  628. const char fw_c_lg[] = "dvb-usb-lme2510c-lg.fw";
  629. const char fw_c_s0194[] = "dvb-usb-lme2510c-s0194.fw";
  630. const char fw_lg[] = "dvb-usb-lme2510-lg.fw";
  631. const char fw_s0194[] = "dvb-usb-lme2510-s0194.fw";
  632. const char *fw_lme;
  633. int ret, cold_fw;
  634. cold = (cold > 0) ? (cold & 1) : 0;
  635. cold_fw = !cold;
  636. if (udev->descriptor.idProduct == 0x1122) {
  637. switch (dvb_usb_lme2510_firmware) {
  638. default:
  639. dvb_usb_lme2510_firmware = TUNER_S0194;
  640. case TUNER_S0194:
  641. fw_lme = fw_s0194;
  642. ret = request_firmware(&fw, fw_lme, &udev->dev);
  643. if (ret == 0) {
  644. cold = 0;/*lme2510-s0194 cannot cold reset*/
  645. break;
  646. }
  647. dvb_usb_lme2510_firmware = TUNER_LG;
  648. case TUNER_LG:
  649. fw_lme = fw_lg;
  650. ret = request_firmware(&fw, fw_lme, &udev->dev);
  651. if (ret == 0)
  652. break;
  653. info("FRM No Firmware Found - please install");
  654. dvb_usb_lme2510_firmware = TUNER_DEFAULT;
  655. cold = 0;
  656. cold_fw = 0;
  657. break;
  658. }
  659. } else {
  660. switch (dvb_usb_lme2510_firmware) {
  661. default:
  662. dvb_usb_lme2510_firmware = TUNER_S7395;
  663. case TUNER_S7395:
  664. fw_lme = fw_c_s7395;
  665. ret = request_firmware(&fw, fw_lme, &udev->dev);
  666. if (ret == 0)
  667. break;
  668. dvb_usb_lme2510_firmware = TUNER_LG;
  669. case TUNER_LG:
  670. fw_lme = fw_c_lg;
  671. ret = request_firmware(&fw, fw_lme, &udev->dev);
  672. if (ret == 0)
  673. break;
  674. dvb_usb_lme2510_firmware = TUNER_S0194;
  675. case TUNER_S0194:
  676. fw_lme = fw_c_s0194;
  677. ret = request_firmware(&fw, fw_lme, &udev->dev);
  678. if (ret == 0)
  679. break;
  680. info("FRM No Firmware Found - please install");
  681. dvb_usb_lme2510_firmware = TUNER_DEFAULT;
  682. cold = 0;
  683. cold_fw = 0;
  684. break;
  685. }
  686. }
  687. if (cold_fw) {
  688. info("FRM Loading %s file", fw_lme);
  689. ret = lme2510_download_firmware(udev, fw);
  690. }
  691. if (cold) {
  692. info("FRM Changing to %s firmware", fw_lme);
  693. lme_coldreset(udev);
  694. return -ENODEV;
  695. }
  696. release_firmware(fw);
  697. return ret;
  698. }
  699. static int lme2510_kill_urb(struct usb_data_stream *stream)
  700. {
  701. int i;
  702. for (i = 0; i < stream->urbs_submitted; i++) {
  703. deb_info(3, "killing URB no. %d.", i);
  704. /* stop the URB */
  705. usb_kill_urb(stream->urb_list[i]);
  706. }
  707. stream->urbs_submitted = 0;
  708. return 0;
  709. }
  710. static struct tda10086_config tda10086_config = {
  711. .demod_address = 0x1c,
  712. .invert = 0,
  713. .diseqc_tone = 1,
  714. .xtal_freq = TDA10086_XTAL_16M,
  715. };
  716. static struct stv0288_config lme_config = {
  717. .demod_address = 0xd0,
  718. .min_delay_ms = 15,
  719. .inittab = s7395_inittab,
  720. };
  721. static struct ix2505v_config lme_tuner = {
  722. .tuner_address = 0xc0,
  723. .min_delay_ms = 100,
  724. .tuner_gain = 0x0,
  725. .tuner_chargepump = 0x3,
  726. };
  727. static struct stv0299_config sharp_z0194_config = {
  728. .demod_address = 0xd0,
  729. .inittab = sharp_z0194a_inittab,
  730. .mclk = 88000000UL,
  731. .invert = 0,
  732. .skip_reinit = 0,
  733. .lock_output = STV0299_LOCKOUTPUT_1,
  734. .volt13_op0_op1 = STV0299_VOLT13_OP1,
  735. .min_delay_ms = 100,
  736. .set_symbol_rate = sharp_z0194a_set_symbol_rate,
  737. };
  738. static int dm04_lme2510_set_voltage(struct dvb_frontend *fe,
  739. fe_sec_voltage_t voltage)
  740. {
  741. struct dvb_usb_adapter *adap = fe->dvb->priv;
  742. static u8 voltage_low[] = LME_VOLTAGE_L;
  743. static u8 voltage_high[] = LME_VOLTAGE_H;
  744. static u8 rbuf[1];
  745. int ret = 0, len = 3, rlen = 1;
  746. if (mutex_lock_interruptible(&adap->dev->i2c_mutex) < 0)
  747. return -EAGAIN;
  748. switch (voltage) {
  749. case SEC_VOLTAGE_18:
  750. ret |= lme2510_usb_talk(adap->dev,
  751. voltage_high, len, rbuf, rlen);
  752. break;
  753. case SEC_VOLTAGE_OFF:
  754. case SEC_VOLTAGE_13:
  755. default:
  756. ret |= lme2510_usb_talk(adap->dev,
  757. voltage_low, len, rbuf, rlen);
  758. break;
  759. }
  760. mutex_unlock(&adap->dev->i2c_mutex);
  761. return (ret < 0) ? -ENODEV : 0;
  762. }
  763. static int lme_name(struct dvb_usb_adapter *adap)
  764. {
  765. struct lme2510_state *st = adap->dev->priv;
  766. const char *desc = adap->dev->desc->name;
  767. char *fe_name[] = {"", " LG TDQY-P001F", " SHARP:BS2F7HZ7395",
  768. " SHARP:BS2F7HZ0194"};
  769. char *name = adap->fe->ops.info.name;
  770. strlcpy(name, desc, 128);
  771. strlcat(name, fe_name[st->tuner_config], 128);
  772. return 0;
  773. }
  774. static int dm04_lme2510_frontend_attach(struct dvb_usb_adapter *adap)
  775. {
  776. struct lme2510_state *st = adap->dev->priv;
  777. int ret = 0;
  778. st->i2c_talk_onoff = 1;
  779. st->i2c_gate = 4;
  780. adap->fe = dvb_attach(tda10086_attach, &tda10086_config,
  781. &adap->dev->i2c_adap);
  782. if (adap->fe) {
  783. info("TUN Found Frontend TDA10086");
  784. st->i2c_tuner_gate_w = 4;
  785. st->i2c_tuner_gate_r = 4;
  786. st->i2c_tuner_addr = 0xc0;
  787. st->tuner_config = TUNER_LG;
  788. if (dvb_usb_lme2510_firmware != TUNER_LG) {
  789. dvb_usb_lme2510_firmware = TUNER_LG;
  790. ret = lme_firmware_switch(adap->dev->udev, 1);
  791. }
  792. goto end;
  793. }
  794. st->i2c_gate = 4;
  795. adap->fe = dvb_attach(stv0299_attach, &sharp_z0194_config,
  796. &adap->dev->i2c_adap);
  797. if (adap->fe) {
  798. info("FE Found Stv0299");
  799. st->i2c_tuner_gate_w = 4;
  800. st->i2c_tuner_gate_r = 5;
  801. st->i2c_tuner_addr = 0xc0;
  802. st->tuner_config = TUNER_S0194;
  803. if (dvb_usb_lme2510_firmware != TUNER_S0194) {
  804. dvb_usb_lme2510_firmware = TUNER_S0194;
  805. ret = lme_firmware_switch(adap->dev->udev, 1);
  806. }
  807. goto end;
  808. }
  809. st->i2c_gate = 5;
  810. adap->fe = dvb_attach(stv0288_attach, &lme_config,
  811. &adap->dev->i2c_adap);
  812. if (adap->fe) {
  813. info("FE Found Stv0288");
  814. st->i2c_tuner_gate_w = 4;
  815. st->i2c_tuner_gate_r = 5;
  816. st->i2c_tuner_addr = 0xc0;
  817. st->tuner_config = TUNER_S7395;
  818. if (dvb_usb_lme2510_firmware != TUNER_S7395) {
  819. dvb_usb_lme2510_firmware = TUNER_S7395;
  820. ret = lme_firmware_switch(adap->dev->udev, 1);
  821. }
  822. } else {
  823. info("DM04 Not Supported");
  824. return -ENODEV;
  825. }
  826. end: if (ret) {
  827. kfree(adap->fe);
  828. adap->fe = NULL;
  829. return -ENODEV;
  830. }
  831. adap->fe->ops.set_voltage = dm04_lme2510_set_voltage;
  832. ret = lme_name(adap);
  833. return ret;
  834. }
  835. static int dm04_lme2510_tuner(struct dvb_usb_adapter *adap)
  836. {
  837. struct lme2510_state *st = adap->dev->priv;
  838. char *tun_msg[] = {"", "TDA8263", "IX2505V", "DVB_PLL_OPERA"};
  839. int ret = 0;
  840. switch (st->tuner_config) {
  841. case TUNER_LG:
  842. if (dvb_attach(tda826x_attach, adap->fe, 0xc0,
  843. &adap->dev->i2c_adap, 1))
  844. ret = st->tuner_config;
  845. break;
  846. case TUNER_S7395:
  847. if (dvb_attach(ix2505v_attach , adap->fe, &lme_tuner,
  848. &adap->dev->i2c_adap))
  849. ret = st->tuner_config;
  850. break;
  851. case TUNER_S0194:
  852. if (dvb_attach(dvb_pll_attach , adap->fe, 0xc0,
  853. &adap->dev->i2c_adap, DVB_PLL_OPERA1))
  854. ret = st->tuner_config;
  855. break;
  856. default:
  857. break;
  858. }
  859. if (ret)
  860. info("TUN Found %s tuner", tun_msg[ret]);
  861. else {
  862. info("TUN No tuner found --- reseting device");
  863. lme_coldreset(adap->dev->udev);
  864. return -ENODEV;
  865. }
  866. /* Start the Interupt & Remote*/
  867. ret = lme2510_int_service(adap);
  868. return ret;
  869. }
  870. static int lme2510_powerup(struct dvb_usb_device *d, int onoff)
  871. {
  872. struct lme2510_state *st = d->priv;
  873. static u8 lnb_on[] = LNB_ON;
  874. static u8 lnb_off[] = LNB_OFF;
  875. static u8 rbuf[1];
  876. int ret, len = 3, rlen = 1;
  877. ret = mutex_lock_interruptible(&d->i2c_mutex);
  878. if (onoff)
  879. ret |= lme2510_usb_talk(d, lnb_on, len, rbuf, rlen);
  880. else
  881. ret |= lme2510_usb_talk(d, lnb_off, len, rbuf, rlen);
  882. st->i2c_talk_onoff = 1;
  883. mutex_unlock(&d->i2c_mutex);
  884. return ret;
  885. }
  886. /* DVB USB Driver stuff */
  887. static struct dvb_usb_device_properties lme2510_properties;
  888. static struct dvb_usb_device_properties lme2510c_properties;
  889. static int lme2510_probe(struct usb_interface *intf,
  890. const struct usb_device_id *id)
  891. {
  892. struct usb_device *udev = interface_to_usbdev(intf);
  893. int ret = 0;
  894. usb_reset_configuration(udev);
  895. usb_set_interface(udev, intf->cur_altsetting->desc.bInterfaceNumber, 1);
  896. if (udev->speed != USB_SPEED_HIGH) {
  897. ret = usb_reset_device(udev);
  898. info("DEV Failed to connect in HIGH SPEED mode");
  899. return -ENODEV;
  900. }
  901. if (lme2510_return_status(udev) == 0x44) {
  902. lme_firmware_switch(udev, 0);
  903. return -ENODEV;
  904. }
  905. if (0 == dvb_usb_device_init(intf, &lme2510_properties,
  906. THIS_MODULE, NULL, adapter_nr)) {
  907. info("DEV registering device driver");
  908. return 0;
  909. }
  910. if (0 == dvb_usb_device_init(intf, &lme2510c_properties,
  911. THIS_MODULE, NULL, adapter_nr)) {
  912. info("DEV registering device driver");
  913. return 0;
  914. }
  915. info("DEV lme2510 Error");
  916. return -ENODEV;
  917. }
  918. static struct usb_device_id lme2510_table[] = {
  919. { USB_DEVICE(0x3344, 0x1122) }, /* LME2510 */
  920. { USB_DEVICE(0x3344, 0x1120) }, /* LME2510C */
  921. {} /* Terminating entry */
  922. };
  923. MODULE_DEVICE_TABLE(usb, lme2510_table);
  924. static struct dvb_usb_device_properties lme2510_properties = {
  925. .caps = DVB_USB_IS_AN_I2C_ADAPTER,
  926. .size_of_priv = sizeof(struct lme2510_state),
  927. .num_adapters = 1,
  928. .adapter = {
  929. {
  930. .streaming_ctrl = lme2510_streaming_ctrl,
  931. .frontend_attach = dm04_lme2510_frontend_attach,
  932. .tuner_attach = dm04_lme2510_tuner,
  933. /* parameter for the MPEG2-data transfer */
  934. .stream = {
  935. .type = USB_BULK,
  936. .count = 10,
  937. .endpoint = 0x06,
  938. .u = {
  939. .bulk = {
  940. .buffersize = 4096,
  941. }
  942. }
  943. }
  944. }
  945. },
  946. .power_ctrl = lme2510_powerup,
  947. .identify_state = lme2510_identify_state,
  948. .i2c_algo = &lme2510_i2c_algo,
  949. .generic_bulk_ctrl_endpoint = 0,
  950. .num_device_descs = 1,
  951. .devices = {
  952. { "DM04_LME2510_DVB-S",
  953. { &lme2510_table[0], NULL },
  954. },
  955. }
  956. };
  957. static struct dvb_usb_device_properties lme2510c_properties = {
  958. .caps = DVB_USB_IS_AN_I2C_ADAPTER,
  959. .size_of_priv = sizeof(struct lme2510_state),
  960. .num_adapters = 1,
  961. .adapter = {
  962. {
  963. .streaming_ctrl = lme2510_streaming_ctrl,
  964. .frontend_attach = dm04_lme2510_frontend_attach,
  965. .tuner_attach = dm04_lme2510_tuner,
  966. /* parameter for the MPEG2-data transfer */
  967. .stream = {
  968. .type = USB_BULK,
  969. .count = 10,
  970. .endpoint = 0x8,
  971. .u = {
  972. .bulk = {
  973. .buffersize = 4096,
  974. }
  975. }
  976. }
  977. }
  978. },
  979. .power_ctrl = lme2510_powerup,
  980. .identify_state = lme2510_identify_state,
  981. .i2c_algo = &lme2510_i2c_algo,
  982. .generic_bulk_ctrl_endpoint = 0,
  983. .num_device_descs = 1,
  984. .devices = {
  985. { "DM04_LME2510C_DVB-S",
  986. { &lme2510_table[1], NULL },
  987. },
  988. }
  989. };
  990. void *lme2510_exit_int(struct dvb_usb_device *d)
  991. {
  992. struct lme2510_state *st = d->priv;
  993. struct dvb_usb_adapter *adap = &d->adapter[0];
  994. void *buffer = NULL;
  995. if (adap != NULL) {
  996. lme2510_kill_urb(&adap->stream);
  997. adap->feedcount = 0;
  998. }
  999. if (st->lme_urb != NULL) {
  1000. st->i2c_talk_onoff = 1;
  1001. st->signal_lock = 0;
  1002. st->signal_level = 0;
  1003. st->signal_sn = 0;
  1004. buffer = st->usb_buffer;
  1005. usb_kill_urb(st->lme_urb);
  1006. usb_free_coherent(d->udev, 5000, st->buffer,
  1007. st->lme_urb->transfer_dma);
  1008. info("Interupt Service Stopped");
  1009. rc_unregister_device(d->rc_dev);
  1010. info("Remote Stopped");
  1011. }
  1012. return buffer;
  1013. }
  1014. void lme2510_exit(struct usb_interface *intf)
  1015. {
  1016. struct dvb_usb_device *d = usb_get_intfdata(intf);
  1017. void *usb_buffer;
  1018. if (d != NULL) {
  1019. usb_buffer = lme2510_exit_int(d);
  1020. dvb_usb_device_exit(intf);
  1021. kfree(usb_buffer);
  1022. }
  1023. }
  1024. static struct usb_driver lme2510_driver = {
  1025. .name = "LME2510C_DVB-S",
  1026. .probe = lme2510_probe,
  1027. .disconnect = lme2510_exit,
  1028. .id_table = lme2510_table,
  1029. };
  1030. /* module stuff */
  1031. static int __init lme2510_module_init(void)
  1032. {
  1033. int result = usb_register(&lme2510_driver);
  1034. if (result) {
  1035. err("usb_register failed. Error number %d", result);
  1036. return result;
  1037. }
  1038. return 0;
  1039. }
  1040. static void __exit lme2510_module_exit(void)
  1041. {
  1042. /* deregister this driver from the USB subsystem */
  1043. usb_deregister(&lme2510_driver);
  1044. }
  1045. module_init(lme2510_module_init);
  1046. module_exit(lme2510_module_exit);
  1047. MODULE_AUTHOR("Malcolm Priestley <tvboxspy@gmail.com>");
  1048. MODULE_DESCRIPTION("LME2510(C) DVB-S USB2.0");
  1049. MODULE_VERSION("1.80");
  1050. MODULE_LICENSE("GPL");