lmedm04.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  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/ir-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. ir_keydown(d->rc_input_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 input_dev *input_dev;
  480. char *ir_codes = RC_MAP_LME2510;
  481. int ret = 0;
  482. info("STA Configuring Remote");
  483. usb_make_path(d->udev, d->rc_phys, sizeof(d->rc_phys));
  484. strlcat(d->rc_phys, "/ir0", sizeof(d->rc_phys));
  485. input_dev = input_allocate_device();
  486. if (!input_dev)
  487. return -ENOMEM;
  488. input_dev->name = "LME2510 Remote Control";
  489. input_dev->phys = d->rc_phys;
  490. usb_to_input_id(d->udev, &input_dev->id);
  491. ret |= ir_input_register(input_dev, ir_codes, NULL, "LME 2510");
  492. if (ret) {
  493. input_free_device(input_dev);
  494. return ret;
  495. }
  496. d->rc_input_dev = input_dev;
  497. /* Start the Interupt */
  498. ret = lme2510_int_read(adap);
  499. if (ret < 0) {
  500. ir_input_unregister(input_dev);
  501. input_free_device(input_dev);
  502. }
  503. return (ret < 0) ? -ENODEV : 0;
  504. }
  505. static u8 check_sum(u8 *p, u8 len)
  506. {
  507. u8 sum = 0;
  508. while (len--)
  509. sum += *p++;
  510. return sum;
  511. }
  512. static int lme2510_download_firmware(struct usb_device *dev,
  513. const struct firmware *fw)
  514. {
  515. int ret = 0;
  516. u8 data[512] = {0};
  517. u16 j, wlen, len_in, start, end;
  518. u8 packet_size, dlen, i;
  519. u8 *fw_data;
  520. packet_size = 0x31;
  521. len_in = 1;
  522. info("FRM Starting Firmware Download");
  523. for (i = 1; i < 3; i++) {
  524. start = (i == 1) ? 0 : 512;
  525. end = (i == 1) ? 512 : fw->size;
  526. for (j = start; j < end; j += (packet_size+1)) {
  527. fw_data = (u8 *)(fw->data + j);
  528. if ((end - j) > packet_size) {
  529. data[0] = i;
  530. dlen = packet_size;
  531. } else {
  532. data[0] = i | 0x80;
  533. dlen = (u8)(end - j)-1;
  534. }
  535. data[1] = dlen;
  536. memcpy(&data[2], fw_data, dlen+1);
  537. wlen = (u8) dlen + 4;
  538. data[wlen-1] = check_sum(fw_data, dlen+1);
  539. deb_info(1, "Data S=%02x:E=%02x CS= %02x", data[3],
  540. data[dlen+2], data[dlen+3]);
  541. ret |= lme2510_bulk_write(dev, data, wlen, 1);
  542. ret |= lme2510_bulk_read(dev, data, len_in , 1);
  543. ret |= (data[0] == 0x88) ? 0 : -1;
  544. }
  545. }
  546. usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  547. 0x06, 0x80, 0x0200, 0x00, data, 0x0109, 1000);
  548. data[0] = 0x8a;
  549. len_in = 1;
  550. msleep(2000);
  551. ret |= lme2510_bulk_write(dev, data , len_in, 1); /*Resetting*/
  552. ret |= lme2510_bulk_read(dev, data, len_in, 1);
  553. msleep(400);
  554. if (ret < 0)
  555. info("FRM Firmware Download Failed (%04x)" , ret);
  556. else
  557. info("FRM Firmware Download Completed - Resetting Device");
  558. return (ret < 0) ? -ENODEV : 0;
  559. }
  560. /* Default firmware for LME2510C */
  561. const char lme_firmware[50] = "dvb-usb-lme2510c-s7395.fw";
  562. static void lme_coldreset(struct usb_device *dev)
  563. {
  564. int ret = 0, len_in;
  565. u8 data[512] = {0};
  566. data[0] = 0x0a;
  567. len_in = 1;
  568. info("FRM Firmware Cold Reset");
  569. ret |= lme2510_bulk_write(dev, data , len_in, 1); /*Cold Resetting*/
  570. ret |= lme2510_bulk_read(dev, data, len_in, 1);
  571. return;
  572. }
  573. static void lme_firmware_switch(struct usb_device *udev, int cold)
  574. {
  575. const struct firmware *fw = NULL;
  576. char lme2510c_s7395[] = "dvb-usb-lme2510c-s7395.fw";
  577. char lme2510c_lg[] = "dvb-usb-lme2510c-lg.fw";
  578. char *firm_msg[] = {"Loading", "Switching to"};
  579. int ret;
  580. cold = (cold > 0) ? (cold & 1) : 0;
  581. if (udev->descriptor.idProduct == 0x1122)
  582. return;
  583. switch (dvb_usb_lme2510_firmware) {
  584. case 0:
  585. default:
  586. memcpy(&lme_firmware, lme2510c_s7395, sizeof(lme2510c_s7395));
  587. ret = request_firmware(&fw, lme_firmware, &udev->dev);
  588. if (ret == 0) {
  589. info("FRM %s S7395 Firmware", firm_msg[cold]);
  590. break;
  591. }
  592. if (cold == 0)
  593. dvb_usb_lme2510_firmware = 1;
  594. else
  595. cold = 0;
  596. case 1:
  597. memcpy(&lme_firmware, lme2510c_lg, sizeof(lme2510c_lg));
  598. ret = request_firmware(&fw, lme_firmware, &udev->dev);
  599. if (ret == 0) {
  600. info("FRM %s LG Firmware", firm_msg[cold]);
  601. break;
  602. }
  603. info("FRM No Firmware Found - please install");
  604. dvb_usb_lme2510_firmware = 0;
  605. cold = 0;
  606. break;
  607. }
  608. release_firmware(fw);
  609. if (cold)
  610. lme_coldreset(udev);
  611. return;
  612. }
  613. static int lme2510_kill_urb(struct usb_data_stream *stream)
  614. {
  615. int i;
  616. for (i = 0; i < stream->urbs_submitted; i++) {
  617. deb_info(3, "killing URB no. %d.", i);
  618. /* stop the URB */
  619. usb_kill_urb(stream->urb_list[i]);
  620. }
  621. stream->urbs_submitted = 0;
  622. return 0;
  623. }
  624. static struct tda10086_config tda10086_config = {
  625. .demod_address = 0x1c,
  626. .invert = 0,
  627. .diseqc_tone = 1,
  628. .xtal_freq = TDA10086_XTAL_16M,
  629. };
  630. static struct stv0288_config lme_config = {
  631. .demod_address = 0xd0,
  632. .min_delay_ms = 15,
  633. .inittab = s7395_inittab,
  634. };
  635. static struct ix2505v_config lme_tuner = {
  636. .tuner_address = 0xc0,
  637. .min_delay_ms = 100,
  638. .tuner_gain = 0x0,
  639. .tuner_chargepump = 0x3,
  640. };
  641. static int dm04_lme2510_set_voltage(struct dvb_frontend *fe,
  642. fe_sec_voltage_t voltage)
  643. {
  644. struct dvb_usb_adapter *adap = fe->dvb->priv;
  645. static u8 voltage_low[] = LME_VOLTAGE_L;
  646. static u8 voltage_high[] = LME_VOLTAGE_H;
  647. static u8 rbuf[1];
  648. int ret = 0, len = 3, rlen = 1;
  649. if (mutex_lock_interruptible(&adap->dev->i2c_mutex) < 0)
  650. return -EAGAIN;
  651. switch (voltage) {
  652. case SEC_VOLTAGE_18:
  653. ret |= lme2510_usb_talk(adap->dev,
  654. voltage_high, len, rbuf, rlen);
  655. break;
  656. case SEC_VOLTAGE_OFF:
  657. case SEC_VOLTAGE_13:
  658. default:
  659. ret |= lme2510_usb_talk(adap->dev,
  660. voltage_low, len, rbuf, rlen);
  661. break;
  662. }
  663. mutex_unlock(&adap->dev->i2c_mutex);
  664. return (ret < 0) ? -ENODEV : 0;
  665. }
  666. static int dm04_lme2510_frontend_attach(struct dvb_usb_adapter *adap)
  667. {
  668. int ret = 0;
  669. struct lme2510_state *st = adap->dev->priv;
  670. /* Interupt Start */
  671. ret = lme2510_int_service(adap);
  672. if (ret < 0) {
  673. info("INT Unable to start Interupt Service");
  674. return -ENODEV;
  675. }
  676. st->i2c_talk_onoff = 1;
  677. st->i2c_gate = 4;
  678. adap->fe = dvb_attach(tda10086_attach, &tda10086_config,
  679. &adap->dev->i2c_adap);
  680. if (adap->fe) {
  681. info("TUN Found Frontend TDA10086");
  682. memcpy(&adap->fe->ops.info.name,
  683. &"DM04_LG_TDQY-P001F DVB-S", 24);
  684. adap->fe->ops.set_voltage = dm04_lme2510_set_voltage;
  685. st->i2c_tuner_gate_w = 4;
  686. st->i2c_tuner_gate_r = 4;
  687. st->i2c_tuner_addr = 0xc0;
  688. if (dvb_attach(tda826x_attach, adap->fe, 0xc0,
  689. &adap->dev->i2c_adap, 1)) {
  690. info("TUN TDA8263 Found");
  691. st->tuner_config = TUNER_LG;
  692. if (dvb_usb_lme2510_firmware != 1) {
  693. dvb_usb_lme2510_firmware = 1;
  694. lme_firmware_switch(adap->dev->udev, 1);
  695. } else /*stops LG/Sharp multi tuner problems*/
  696. dvb_usb_lme2510_firmware = 0;
  697. return 0;
  698. }
  699. kfree(adap->fe);
  700. adap->fe = NULL;
  701. }
  702. st->i2c_gate = 5;
  703. adap->fe = dvb_attach(stv0288_attach, &lme_config,
  704. &adap->dev->i2c_adap);
  705. if (adap->fe) {
  706. info("FE Found Stv0288");
  707. memcpy(&adap->fe->ops.info.name,
  708. &"DM04_SHARP:BS2F7HZ7395", 22);
  709. adap->fe->ops.set_voltage = dm04_lme2510_set_voltage;
  710. st->i2c_tuner_gate_w = 4;
  711. st->i2c_tuner_gate_r = 5;
  712. st->i2c_tuner_addr = 0xc0;
  713. if (dvb_attach(ix2505v_attach , adap->fe, &lme_tuner,
  714. &adap->dev->i2c_adap)) {
  715. st->tuner_config = TUNER_S7395;
  716. info("TUN Sharp IX2505V silicon tuner");
  717. if (dvb_usb_lme2510_firmware != 0) {
  718. dvb_usb_lme2510_firmware = 0;
  719. lme_firmware_switch(adap->dev->udev, 1);
  720. }
  721. return 0;
  722. }
  723. kfree(adap->fe);
  724. adap->fe = NULL;
  725. }
  726. info("DM04 Not Supported");
  727. return -ENODEV;
  728. }
  729. static int lme2510_powerup(struct dvb_usb_device *d, int onoff)
  730. {
  731. struct lme2510_state *st = d->priv;
  732. static u8 lnb_on[] = LNB_ON;
  733. static u8 lnb_off[] = LNB_OFF;
  734. static u8 rbuf[1];
  735. int ret, len = 3, rlen = 1;
  736. ret = mutex_lock_interruptible(&d->i2c_mutex);
  737. if (onoff)
  738. ret |= lme2510_usb_talk(d, lnb_on, len, rbuf, rlen);
  739. else
  740. ret |= lme2510_usb_talk(d, lnb_off, len, rbuf, rlen);
  741. st->i2c_talk_onoff = 1;
  742. mutex_unlock(&d->i2c_mutex);
  743. return ret;
  744. }
  745. /* DVB USB Driver stuff */
  746. static struct dvb_usb_device_properties lme2510_properties;
  747. static struct dvb_usb_device_properties lme2510c_properties;
  748. static int lme2510_probe(struct usb_interface *intf,
  749. const struct usb_device_id *id)
  750. {
  751. struct usb_device *udev = interface_to_usbdev(intf);
  752. int ret = 0;
  753. usb_reset_configuration(udev);
  754. usb_set_interface(udev, intf->cur_altsetting->desc.bInterfaceNumber, 1);
  755. if (udev->speed != USB_SPEED_HIGH) {
  756. ret = usb_reset_device(udev);
  757. info("DEV Failed to connect in HIGH SPEED mode");
  758. return -ENODEV;
  759. }
  760. lme_firmware_switch(udev, 0);
  761. if (0 == dvb_usb_device_init(intf, &lme2510_properties,
  762. THIS_MODULE, NULL, adapter_nr)) {
  763. info("DEV registering device driver");
  764. return 0;
  765. }
  766. if (0 == dvb_usb_device_init(intf, &lme2510c_properties,
  767. THIS_MODULE, NULL, adapter_nr)) {
  768. info("DEV registering device driver");
  769. return 0;
  770. }
  771. info("DEV lme2510 Error");
  772. return -ENODEV;
  773. }
  774. static struct usb_device_id lme2510_table[] = {
  775. { USB_DEVICE(0x3344, 0x1122) }, /* LME2510 */
  776. { USB_DEVICE(0x3344, 0x1120) }, /* LME2510C */
  777. {} /* Terminating entry */
  778. };
  779. MODULE_DEVICE_TABLE(usb, lme2510_table);
  780. static struct dvb_usb_device_properties lme2510_properties = {
  781. .caps = DVB_USB_IS_AN_I2C_ADAPTER,
  782. .usb_ctrl = DEVICE_SPECIFIC,
  783. .download_firmware = lme2510_download_firmware,
  784. .firmware = "dvb-usb-lme2510-lg.fw",
  785. .size_of_priv = sizeof(struct lme2510_state),
  786. .num_adapters = 1,
  787. .adapter = {
  788. {
  789. .streaming_ctrl = lme2510_streaming_ctrl,
  790. .frontend_attach = dm04_lme2510_frontend_attach,
  791. /* parameter for the MPEG2-data transfer */
  792. .stream = {
  793. .type = USB_BULK,
  794. .count = 10,
  795. .endpoint = 0x06,
  796. .u = {
  797. .bulk = {
  798. .buffersize = 4096,
  799. }
  800. }
  801. }
  802. }
  803. },
  804. .power_ctrl = lme2510_powerup,
  805. .identify_state = lme2510_identify_state,
  806. .i2c_algo = &lme2510_i2c_algo,
  807. .generic_bulk_ctrl_endpoint = 0,
  808. .num_device_descs = 1,
  809. .devices = {
  810. { "DM04 LME2510 DVB-S USB 2.0",
  811. { &lme2510_table[0], NULL },
  812. },
  813. }
  814. };
  815. static struct dvb_usb_device_properties lme2510c_properties = {
  816. .caps = DVB_USB_IS_AN_I2C_ADAPTER,
  817. .usb_ctrl = DEVICE_SPECIFIC,
  818. .download_firmware = lme2510_download_firmware,
  819. .firmware = lme_firmware,
  820. .size_of_priv = sizeof(struct lme2510_state),
  821. .num_adapters = 1,
  822. .adapter = {
  823. {
  824. .streaming_ctrl = lme2510_streaming_ctrl,
  825. .frontend_attach = dm04_lme2510_frontend_attach,
  826. /* parameter for the MPEG2-data transfer */
  827. .stream = {
  828. .type = USB_BULK,
  829. .count = 10,
  830. .endpoint = 0x8,
  831. .u = {
  832. .bulk = {
  833. .buffersize = 4096,
  834. }
  835. }
  836. }
  837. }
  838. },
  839. .power_ctrl = lme2510_powerup,
  840. .identify_state = lme2510_identify_state,
  841. .i2c_algo = &lme2510_i2c_algo,
  842. .generic_bulk_ctrl_endpoint = 0,
  843. .num_device_descs = 1,
  844. .devices = {
  845. { "DM04 LME2510C USB2.0",
  846. { &lme2510_table[1], NULL },
  847. },
  848. }
  849. };
  850. void *lme2510_exit_int(struct dvb_usb_device *d)
  851. {
  852. struct lme2510_state *st = d->priv;
  853. struct dvb_usb_adapter *adap = &d->adapter[0];
  854. void *buffer = NULL;
  855. if (adap != NULL) {
  856. lme2510_kill_urb(&adap->stream);
  857. adap->feedcount = 0;
  858. }
  859. if (st->lme_urb != NULL) {
  860. st->i2c_talk_onoff = 1;
  861. st->signal_lock = 0;
  862. st->signal_level = 0;
  863. st->signal_sn = 0;
  864. buffer = st->usb_buffer;
  865. usb_kill_urb(st->lme_urb);
  866. usb_free_coherent(d->udev, 5000, st->buffer,
  867. st->lme_urb->transfer_dma);
  868. info("Interupt Service Stopped");
  869. ir_input_unregister(d->rc_input_dev);
  870. info("Remote Stopped");
  871. }
  872. return buffer;
  873. }
  874. void lme2510_exit(struct usb_interface *intf)
  875. {
  876. struct dvb_usb_device *d = usb_get_intfdata(intf);
  877. void *usb_buffer;
  878. if (d != NULL) {
  879. usb_buffer = lme2510_exit_int(d);
  880. dvb_usb_device_exit(intf);
  881. kfree(usb_buffer);
  882. }
  883. }
  884. static struct usb_driver lme2510_driver = {
  885. .name = "LME2510C_DVBS",
  886. .probe = lme2510_probe,
  887. .disconnect = lme2510_exit,
  888. .id_table = lme2510_table,
  889. };
  890. /* module stuff */
  891. static int __init lme2510_module_init(void)
  892. {
  893. int result = usb_register(&lme2510_driver);
  894. if (result) {
  895. err("usb_register failed. Error number %d", result);
  896. return result;
  897. }
  898. return 0;
  899. }
  900. static void __exit lme2510_module_exit(void)
  901. {
  902. /* deregister this driver from the USB subsystem */
  903. usb_deregister(&lme2510_driver);
  904. }
  905. module_init(lme2510_module_init);
  906. module_exit(lme2510_module_exit);
  907. MODULE_AUTHOR("Malcolm Priestley <tvboxspy@gmail.com>");
  908. MODULE_DESCRIPTION("LM2510(C) DVB-S USB2.0");
  909. MODULE_VERSION("1.71");
  910. MODULE_LICENSE("GPL");