lmedm04.c 25 KB

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