lmedm04.c 24 KB

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