lmedm04.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  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. u8 one_tune;
  105. void *buffer;
  106. struct urb *lme_urb;
  107. void *usb_buffer;
  108. };
  109. static int lme2510_bulk_write(struct usb_device *dev,
  110. u8 *snd, int len, u8 pipe)
  111. {
  112. int ret, actual_l;
  113. ret = usb_bulk_msg(dev, usb_sndbulkpipe(dev, pipe),
  114. snd, len , &actual_l, 500);
  115. return ret;
  116. }
  117. static int lme2510_bulk_read(struct usb_device *dev,
  118. u8 *rev, int len, u8 pipe)
  119. {
  120. int ret, actual_l;
  121. ret = usb_bulk_msg(dev, usb_rcvbulkpipe(dev, pipe),
  122. rev, len , &actual_l, 500);
  123. return ret;
  124. }
  125. static int lme2510_usb_talk(struct dvb_usb_device *d,
  126. u8 *wbuf, int wlen, u8 *rbuf, int rlen)
  127. {
  128. struct lme2510_state *st = d->priv;
  129. u8 *buff;
  130. int ret = 0;
  131. if (st->usb_buffer == NULL) {
  132. st->usb_buffer = kmalloc(512, GFP_KERNEL);
  133. if (st->usb_buffer == NULL) {
  134. info("MEM Error no memory");
  135. return -ENOMEM;
  136. }
  137. }
  138. buff = st->usb_buffer;
  139. /* the read/write capped at 512 */
  140. memcpy(buff, wbuf, (wlen > 512) ? 512 : wlen);
  141. ret = mutex_lock_interruptible(&d->usb_mutex);
  142. if (ret < 0)
  143. return -EAGAIN;
  144. ret |= usb_clear_halt(d->udev, usb_sndbulkpipe(d->udev, 0x01));
  145. ret |= lme2510_bulk_write(d->udev, buff, wlen , 0x01);
  146. msleep(12);
  147. ret |= usb_clear_halt(d->udev, usb_rcvbulkpipe(d->udev, 0x01));
  148. ret |= lme2510_bulk_read(d->udev, buff, (rlen > 512) ?
  149. 512 : rlen , 0x01);
  150. if (rlen > 0)
  151. memcpy(rbuf, buff, rlen);
  152. mutex_unlock(&d->usb_mutex);
  153. return (ret < 0) ? -ENODEV : 0;
  154. }
  155. static int lme2510_usb_talk_restart(struct dvb_usb_device *d,
  156. u8 *wbuf, int wlen, u8 *rbuf, int rlen) {
  157. static u8 stream_on[] = LME_ST_ON_W;
  158. int ret;
  159. u8 rbuff[10];
  160. /*Send Normal Command*/
  161. ret = lme2510_usb_talk(d, wbuf, wlen, rbuf, rlen);
  162. /*Restart Stream Command*/
  163. ret |= lme2510_usb_talk(d, stream_on, sizeof(stream_on),
  164. rbuff, sizeof(rbuff));
  165. return ret;
  166. }
  167. static int lme2510_remote_keypress(struct dvb_usb_adapter *adap, u16 keypress)
  168. {
  169. struct dvb_usb_device *d = adap->dev;
  170. deb_info(1, "INT Key Keypress =%04x", keypress);
  171. if (keypress > 0)
  172. ir_keydown(d->rc_input_dev, keypress, 0);
  173. return 0;
  174. }
  175. static void lme2510_int_response(struct urb *lme_urb)
  176. {
  177. struct dvb_usb_adapter *adap = lme_urb->context;
  178. struct lme2510_state *st = adap->dev->priv;
  179. static u8 *ibuf, *rbuf;
  180. int i = 0, offset;
  181. switch (lme_urb->status) {
  182. case 0:
  183. case -ETIMEDOUT:
  184. break;
  185. case -ECONNRESET:
  186. case -ENOENT:
  187. case -ESHUTDOWN:
  188. return;
  189. default:
  190. info("Error %x", lme_urb->status);
  191. break;
  192. }
  193. rbuf = (u8 *) lme_urb->transfer_buffer;
  194. offset = ((lme_urb->actual_length/8) > 4)
  195. ? 4 : (lme_urb->actual_length/8) ;
  196. for (i = 0; i < offset; ++i) {
  197. ibuf = (u8 *)&rbuf[i*8];
  198. deb_info(5, "INT O/S C =%02x C/O=%02x Type =%02x%02x",
  199. offset, i, ibuf[0], ibuf[1]);
  200. switch (ibuf[0]) {
  201. case 0xaa:
  202. debug_data_snipet(1, "INT Remote data snipet in", ibuf);
  203. lme2510_remote_keypress(adap,
  204. (u16)(ibuf[4]<<8)+ibuf[5]);
  205. break;
  206. case 0xbb:
  207. switch (st->tuner_config) {
  208. case TUNER_LG:
  209. if (ibuf[2] > 0)
  210. st->signal_lock = ibuf[2];
  211. st->signal_level = ibuf[4];
  212. st->signal_sn = ibuf[3];
  213. st->time_key = ibuf[7];
  214. break;
  215. case TUNER_S7395:
  216. /* Tweak for earlier firmware*/
  217. if (ibuf[1] == 0x03) {
  218. st->signal_level = ibuf[3];
  219. st->signal_sn = ibuf[4];
  220. } else {
  221. st->signal_level = ibuf[4];
  222. st->signal_sn = ibuf[5];
  223. }
  224. break;
  225. default:
  226. break;
  227. }
  228. debug_data_snipet(5, "INT Remote data snipet in", ibuf);
  229. break;
  230. case 0xcc:
  231. debug_data_snipet(1, "INT Control data snipet", ibuf);
  232. break;
  233. default:
  234. debug_data_snipet(1, "INT Unknown data snipet", ibuf);
  235. break;
  236. }
  237. }
  238. usb_submit_urb(lme_urb, GFP_ATOMIC);
  239. }
  240. static int lme2510_int_read(struct dvb_usb_adapter *adap)
  241. {
  242. struct lme2510_state *lme_int = adap->dev->priv;
  243. lme_int->lme_urb = usb_alloc_urb(0, GFP_ATOMIC);
  244. if (lme_int->lme_urb == NULL)
  245. return -ENOMEM;
  246. lme_int->buffer = usb_alloc_coherent(adap->dev->udev, 5000, GFP_ATOMIC,
  247. &lme_int->lme_urb->transfer_dma);
  248. if (lme_int->buffer == NULL)
  249. return -ENOMEM;
  250. usb_fill_int_urb(lme_int->lme_urb,
  251. adap->dev->udev,
  252. usb_rcvintpipe(adap->dev->udev, 0xa),
  253. lme_int->buffer,
  254. 4096,
  255. lme2510_int_response,
  256. adap,
  257. 11);
  258. lme_int->lme_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  259. usb_submit_urb(lme_int->lme_urb, GFP_ATOMIC);
  260. info("INT Interupt Service Started");
  261. return 0;
  262. }
  263. static int lme2510_return_status(struct usb_device *dev)
  264. {
  265. int ret = 0;
  266. u8 data[10] = {0};
  267. ret |= usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  268. 0x06, 0x80, 0x0302, 0x00, data, 0x0006, 200);
  269. info("Firmware Status: %x (%x)", ret , data[2]);
  270. return (ret < 0) ? -ENODEV : data[2];
  271. }
  272. static int lme2510_msg(struct dvb_usb_device *d,
  273. u8 *wbuf, int wlen, u8 *rbuf, int rlen)
  274. {
  275. int ret = 0;
  276. struct lme2510_state *st = d->priv;
  277. if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
  278. return -EAGAIN;
  279. if (st->i2c_talk_onoff == 1) {
  280. ret = lme2510_usb_talk(d, wbuf, wlen, rbuf, rlen);
  281. switch (st->tuner_config) {
  282. case TUNER_LG:
  283. if (wbuf[2] == 0x1c) {
  284. if (wbuf[3] == 0x0e) {
  285. st->signal_lock = rbuf[1];
  286. if ((st->stream_on & 1) &&
  287. (st->signal_lock & 0x10)) {
  288. lme2510_usb_talk_restart(d,
  289. wbuf, wlen, rbuf, rlen);
  290. st->i2c_talk_onoff = 0;
  291. }
  292. msleep(80);
  293. }
  294. }
  295. break;
  296. case TUNER_S7395:
  297. if (wbuf[2] == 0xd0) {
  298. if (wbuf[3] == 0x24) {
  299. st->signal_lock = rbuf[1];
  300. if ((st->stream_on & 1) &&
  301. (st->signal_lock & 0x8)) {
  302. lme2510_usb_talk_restart(d,
  303. wbuf, wlen, rbuf, rlen);
  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. /*DiSEqC functions as per TDA10086*/
  331. case 0x36:
  332. case 0x48:
  333. case 0x49:
  334. case 0x4a:
  335. case 0x4b:
  336. case 0x4c:
  337. case 0x4d:
  338. if (wbuf[2] == 0x1c)
  339. lme2510_usb_talk_restart(d,
  340. wbuf, wlen, rbuf, rlen);
  341. default:
  342. break;
  343. }
  344. break;
  345. case TUNER_S7395:
  346. switch (wbuf[3]) {
  347. case 0x10:
  348. rbuf[0] = 0x55;
  349. rbuf[1] = (st->signal_level & 0x80)
  350. ? 0 : (st->signal_level * 2);
  351. break;
  352. case 0x2d:
  353. rbuf[0] = 0x55;
  354. rbuf[1] = st->signal_sn;
  355. break;
  356. case 0x24:
  357. rbuf[0] = 0x55;
  358. rbuf[1] = (st->signal_level & 0x80)
  359. ? 0 : st->signal_lock;
  360. break;
  361. case 0x6:
  362. if (wbuf[2] == 0xd0)
  363. lme2510_usb_talk(d,
  364. wbuf, wlen, rbuf, rlen);
  365. break;
  366. case 0x1:
  367. if (st->one_tune > 0)
  368. break;
  369. st->one_tune++;
  370. st->i2c_talk_onoff = 1;
  371. /*DiSEqC functions as per STV0288*/
  372. case 0x5:
  373. case 0x7:
  374. case 0x8:
  375. case 0x9:
  376. case 0xa:
  377. case 0xb:
  378. if (wbuf[2] == 0xd0)
  379. lme2510_usb_talk_restart(d,
  380. wbuf, wlen, rbuf, rlen);
  381. break;
  382. default:
  383. rbuf[0] = 0x55;
  384. rbuf[1] = 0x00;
  385. break;
  386. }
  387. break;
  388. default:
  389. break;
  390. }
  391. deb_info(4, "I2C From Interupt Message out(%02x) in(%02x)",
  392. wbuf[3], rbuf[1]);
  393. }
  394. mutex_unlock(&d->i2c_mutex);
  395. return ret;
  396. }
  397. static int lme2510_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
  398. int num)
  399. {
  400. struct dvb_usb_device *d = i2c_get_adapdata(adap);
  401. struct lme2510_state *st = d->priv;
  402. static u8 obuf[64], ibuf[512];
  403. int i, read, read_o;
  404. u16 len;
  405. u8 gate = st->i2c_gate;
  406. if (gate == 0)
  407. gate = 5;
  408. if (num > 2)
  409. warn("more than 2 i2c messages"
  410. "at a time is not handled yet. TODO.");
  411. for (i = 0; i < num; i++) {
  412. read_o = 1 & (msg[i].flags & I2C_M_RD);
  413. read = i+1 < num && (msg[i+1].flags & I2C_M_RD);
  414. read |= read_o;
  415. gate = (msg[i].addr == st->i2c_tuner_addr)
  416. ? (read) ? st->i2c_tuner_gate_r
  417. : st->i2c_tuner_gate_w
  418. : st->i2c_gate;
  419. obuf[0] = gate | (read << 7);
  420. if (gate == 5)
  421. obuf[1] = (read) ? 2 : msg[i].len + 1;
  422. else
  423. obuf[1] = msg[i].len + read + 1;
  424. obuf[2] = msg[i].addr;
  425. if (read) {
  426. if (read_o)
  427. len = 3;
  428. else {
  429. memcpy(&obuf[3], msg[i].buf, msg[i].len);
  430. obuf[msg[i].len+3] = msg[i+1].len;
  431. len = msg[i].len+4;
  432. }
  433. } else {
  434. memcpy(&obuf[3], msg[i].buf, msg[i].len);
  435. len = msg[i].len+3;
  436. }
  437. if (lme2510_msg(d, obuf, len, ibuf, 512) < 0) {
  438. deb_info(1, "i2c transfer failed.");
  439. return -EAGAIN;
  440. }
  441. if (read) {
  442. if (read_o)
  443. memcpy(msg[i].buf, &ibuf[1], msg[i].len);
  444. else {
  445. memcpy(msg[i+1].buf, &ibuf[1], msg[i+1].len);
  446. i++;
  447. }
  448. }
  449. }
  450. return i;
  451. }
  452. static u32 lme2510_i2c_func(struct i2c_adapter *adapter)
  453. {
  454. return I2C_FUNC_I2C;
  455. }
  456. static struct i2c_algorithm lme2510_i2c_algo = {
  457. .master_xfer = lme2510_i2c_xfer,
  458. .functionality = lme2510_i2c_func,
  459. };
  460. /* Callbacks for DVB USB */
  461. static int lme2510_identify_state(struct usb_device *udev,
  462. struct dvb_usb_device_properties *props,
  463. struct dvb_usb_device_description **desc,
  464. int *cold)
  465. {
  466. if (lme2510_return_status(udev) == 0x44)
  467. *cold = 1;
  468. else
  469. *cold = 0;
  470. return 0;
  471. }
  472. static int lme2510_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
  473. {
  474. struct lme2510_state *st = adap->dev->priv;
  475. static u8 stream_on[] = LME_ST_ON_W;
  476. static u8 clear_reg_3[] = LME_CLEAR_PID;
  477. static u8 rbuf[1];
  478. static u8 timeout;
  479. int ret = 0, len = 2, rlen = sizeof(rbuf);
  480. deb_info(1, "STM (%02x)", onoff);
  481. if (onoff == 1) {
  482. st->i2c_talk_onoff = 0;
  483. timeout = 0;
  484. /* wait for i2C to be free */
  485. while (mutex_lock_interruptible(&adap->dev->i2c_mutex) < 0) {
  486. timeout++;
  487. if (timeout > 5)
  488. return -ENODEV;
  489. }
  490. msleep(100);
  491. ret |= lme2510_usb_talk(adap->dev,
  492. stream_on, len, rbuf, rlen);
  493. st->stream_on = 1;
  494. st->one_tune = 0;
  495. mutex_unlock(&adap->dev->i2c_mutex);
  496. } else {
  497. deb_info(1, "STM Steam Off");
  498. ret |= lme2510_usb_talk(adap->dev, clear_reg_3,
  499. sizeof(clear_reg_3), rbuf, rlen);
  500. st->stream_on = 0;
  501. st->i2c_talk_onoff = 1;
  502. }
  503. return (ret < 0) ? -ENODEV : 0;
  504. }
  505. static int lme2510_int_service(struct dvb_usb_adapter *adap)
  506. {
  507. struct dvb_usb_device *d = adap->dev;
  508. struct input_dev *input_dev;
  509. char *ir_codes = RC_MAP_LME2510;
  510. int ret = 0;
  511. info("STA Configuring Remote");
  512. usb_make_path(d->udev, d->rc_phys, sizeof(d->rc_phys));
  513. strlcat(d->rc_phys, "/ir0", sizeof(d->rc_phys));
  514. input_dev = input_allocate_device();
  515. if (!input_dev)
  516. return -ENOMEM;
  517. input_dev->name = "LME2510 Remote Control";
  518. input_dev->phys = d->rc_phys;
  519. usb_to_input_id(d->udev, &input_dev->id);
  520. ret |= ir_input_register(input_dev, ir_codes, NULL, "LME 2510");
  521. if (ret) {
  522. input_free_device(input_dev);
  523. return ret;
  524. }
  525. d->rc_input_dev = input_dev;
  526. /* Start the Interupt */
  527. ret = lme2510_int_read(adap);
  528. if (ret < 0) {
  529. ir_input_unregister(input_dev);
  530. input_free_device(input_dev);
  531. }
  532. return (ret < 0) ? -ENODEV : 0;
  533. }
  534. static u8 check_sum(u8 *p, u8 len)
  535. {
  536. u8 sum = 0;
  537. while (len--)
  538. sum += *p++;
  539. return sum;
  540. }
  541. static int lme2510_download_firmware(struct usb_device *dev,
  542. const struct firmware *fw)
  543. {
  544. int ret = 0;
  545. u8 data[512] = {0};
  546. u16 j, wlen, len_in, start, end;
  547. u8 packet_size, dlen, i;
  548. u8 *fw_data;
  549. packet_size = 0x31;
  550. len_in = 1;
  551. info("FRM Starting Firmware Download");
  552. for (i = 1; i < 3; i++) {
  553. start = (i == 1) ? 0 : 512;
  554. end = (i == 1) ? 512 : fw->size;
  555. for (j = start; j < end; j += (packet_size+1)) {
  556. fw_data = (u8 *)(fw->data + j);
  557. if ((end - j) > packet_size) {
  558. data[0] = i;
  559. dlen = packet_size;
  560. } else {
  561. data[0] = i | 0x80;
  562. dlen = (u8)(end - j)-1;
  563. }
  564. data[1] = dlen;
  565. memcpy(&data[2], fw_data, dlen+1);
  566. wlen = (u8) dlen + 4;
  567. data[wlen-1] = check_sum(fw_data, dlen+1);
  568. deb_info(1, "Data S=%02x:E=%02x CS= %02x", data[3],
  569. data[dlen+2], data[dlen+3]);
  570. ret |= lme2510_bulk_write(dev, data, wlen, 1);
  571. ret |= lme2510_bulk_read(dev, data, len_in , 1);
  572. ret |= (data[0] == 0x88) ? 0 : -1;
  573. }
  574. }
  575. usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
  576. 0x06, 0x80, 0x0200, 0x00, data, 0x0109, 1000);
  577. data[0] = 0x8a;
  578. len_in = 1;
  579. msleep(2000);
  580. ret |= lme2510_bulk_write(dev, data , len_in, 1); /*Resetting*/
  581. ret |= lme2510_bulk_read(dev, data, len_in, 1);
  582. msleep(400);
  583. if (ret < 0)
  584. info("FRM Firmware Download Failed (%04x)" , ret);
  585. else
  586. info("FRM Firmware Download Completed - Resetting Device");
  587. return (ret < 0) ? -ENODEV : 0;
  588. }
  589. /* Default firmware for LME2510C */
  590. const char lme_firmware[50] = "dvb-usb-lme2510c-s7395.fw";
  591. static void lme_coldreset(struct usb_device *dev)
  592. {
  593. int ret = 0, len_in;
  594. u8 data[512] = {0};
  595. data[0] = 0x0a;
  596. len_in = 1;
  597. info("FRM Firmware Cold Reset");
  598. ret |= lme2510_bulk_write(dev, data , len_in, 1); /*Cold Resetting*/
  599. ret |= lme2510_bulk_read(dev, data, len_in, 1);
  600. return;
  601. }
  602. static void lme_firmware_switch(struct usb_device *udev, int cold)
  603. {
  604. const struct firmware *fw = NULL;
  605. char lme2510c_s7395[] = "dvb-usb-lme2510c-s7395.fw";
  606. char lme2510c_lg[] = "dvb-usb-lme2510c-lg.fw";
  607. char *firm_msg[] = {"Loading", "Switching to"};
  608. int ret;
  609. if (udev->descriptor.idProduct == 0x1122)
  610. return;
  611. switch (dvb_usb_lme2510_firmware) {
  612. case 0:
  613. default:
  614. memcpy(&lme_firmware, lme2510c_s7395, sizeof(lme2510c_s7395));
  615. ret = request_firmware(&fw, lme_firmware, &udev->dev);
  616. if (ret == 0) {
  617. info("FRM %s S7395 Firmware", firm_msg[cold]);
  618. break;
  619. }
  620. if (cold == 0)
  621. dvb_usb_lme2510_firmware = 1;
  622. else
  623. cold = 0;
  624. case 1:
  625. memcpy(&lme_firmware, lme2510c_lg, sizeof(lme2510c_lg));
  626. ret = request_firmware(&fw, lme_firmware, &udev->dev);
  627. if (ret == 0) {
  628. info("FRM %s LG Firmware", firm_msg[cold]);
  629. break;
  630. }
  631. info("FRM No Firmware Found - please install");
  632. dvb_usb_lme2510_firmware = 0;
  633. cold = 0;
  634. break;
  635. }
  636. release_firmware(fw);
  637. if (cold)
  638. lme_coldreset(udev);
  639. return;
  640. }
  641. static int lme2510_kill_urb(struct usb_data_stream *stream)
  642. {
  643. int i;
  644. for (i = 0; i < stream->urbs_submitted; i++) {
  645. deb_info(3, "killing URB no. %d.", i);
  646. /* stop the URB */
  647. usb_kill_urb(stream->urb_list[i]);
  648. }
  649. stream->urbs_submitted = 0;
  650. return 0;
  651. }
  652. static struct tda10086_config tda10086_config = {
  653. .demod_address = 0x1c,
  654. .invert = 0,
  655. .diseqc_tone = 1,
  656. .xtal_freq = TDA10086_XTAL_16M,
  657. };
  658. static struct stv0288_config lme_config = {
  659. .demod_address = 0xd0,
  660. .min_delay_ms = 15,
  661. .inittab = s7395_inittab,
  662. };
  663. static struct ix2505v_config lme_tuner = {
  664. .tuner_address = 0xc0,
  665. .min_delay_ms = 100,
  666. .tuner_gain = 0x0,
  667. .tuner_chargepump = 0x3,
  668. };
  669. static int dm04_lme2510_set_voltage(struct dvb_frontend *fe,
  670. fe_sec_voltage_t voltage)
  671. {
  672. struct dvb_usb_adapter *adap = fe->dvb->priv;
  673. struct lme2510_state *st = adap->dev->priv;
  674. static u8 voltage_low[] = LME_VOLTAGE_L;
  675. static u8 voltage_high[] = LME_VOLTAGE_H;
  676. static u8 lnb_on[] = LNB_ON;
  677. static u8 lnb_off[] = LNB_OFF;
  678. static u8 rbuf[1];
  679. int ret = 0, len = 3, rlen = 1;
  680. if (st->stream_on == 1)
  681. return 0;
  682. ret |= lme2510_usb_talk(adap->dev, lnb_on, len, rbuf, rlen);
  683. switch (voltage) {
  684. case SEC_VOLTAGE_18:
  685. ret |= lme2510_usb_talk(adap->dev,
  686. voltage_high, len, rbuf, rlen);
  687. break;
  688. case SEC_VOLTAGE_OFF:
  689. ret |= lme2510_usb_talk(adap->dev,
  690. lnb_off, len, rbuf, rlen);
  691. case SEC_VOLTAGE_13:
  692. default:
  693. ret |= lme2510_usb_talk(adap->dev,
  694. voltage_low, len, rbuf, rlen);
  695. break;
  696. };
  697. st->i2c_talk_onoff = 1;
  698. return (ret < 0) ? -ENODEV : 0;
  699. }
  700. static int dm04_lme2510_frontend_attach(struct dvb_usb_adapter *adap)
  701. {
  702. int ret = 0;
  703. struct lme2510_state *st = adap->dev->priv;
  704. /* Interupt Start */
  705. ret = lme2510_int_service(adap);
  706. if (ret < 0) {
  707. info("INT Unable to start Interupt Service");
  708. return -ENODEV;
  709. }
  710. st->i2c_talk_onoff = 1;
  711. st->i2c_gate = 4;
  712. adap->fe = dvb_attach(tda10086_attach, &tda10086_config,
  713. &adap->dev->i2c_adap);
  714. if (adap->fe) {
  715. info("TUN Found Frontend TDA10086");
  716. memcpy(&adap->fe->ops.info.name,
  717. &"DM04_LG_TDQY-P001F DVB-S", 24);
  718. adap->fe->ops.set_voltage = dm04_lme2510_set_voltage;
  719. st->i2c_tuner_gate_w = 4;
  720. st->i2c_tuner_gate_r = 4;
  721. st->i2c_tuner_addr = 0xc0;
  722. if (dvb_attach(tda826x_attach, adap->fe, 0xc0,
  723. &adap->dev->i2c_adap, 1)) {
  724. info("TUN TDA8263 Found");
  725. st->tuner_config = TUNER_LG;
  726. if (dvb_usb_lme2510_firmware != 1) {
  727. dvb_usb_lme2510_firmware = 1;
  728. lme_firmware_switch(adap->dev->udev, 1);
  729. }
  730. return 0;
  731. }
  732. kfree(adap->fe);
  733. adap->fe = NULL;
  734. }
  735. st->i2c_gate = 5;
  736. adap->fe = dvb_attach(stv0288_attach, &lme_config,
  737. &adap->dev->i2c_adap);
  738. if (adap->fe) {
  739. info("FE Found Stv0288");
  740. memcpy(&adap->fe->ops.info.name,
  741. &"DM04_SHARP:BS2F7HZ7395", 22);
  742. adap->fe->ops.set_voltage = dm04_lme2510_set_voltage;
  743. st->i2c_tuner_gate_w = 4;
  744. st->i2c_tuner_gate_r = 5;
  745. st->i2c_tuner_addr = 0xc0;
  746. if (dvb_attach(ix2505v_attach , adap->fe, &lme_tuner,
  747. &adap->dev->i2c_adap)) {
  748. st->tuner_config = TUNER_S7395;
  749. info("TUN Sharp IX2505V silicon tuner");
  750. if (dvb_usb_lme2510_firmware != 0) {
  751. dvb_usb_lme2510_firmware = 0;
  752. lme_firmware_switch(adap->dev->udev, 1);
  753. }
  754. return 0;
  755. }
  756. kfree(adap->fe);
  757. adap->fe = NULL;
  758. }
  759. info("DM04 Not Supported");
  760. return -ENODEV;
  761. }
  762. static int lme2510_powerup(struct dvb_usb_device *d, int onoff)
  763. {
  764. struct lme2510_state *st = d->priv;
  765. st->i2c_talk_onoff = 1;
  766. return 0;
  767. }
  768. /* DVB USB Driver stuff */
  769. static struct dvb_usb_device_properties lme2510_properties;
  770. static struct dvb_usb_device_properties lme2510c_properties;
  771. static int lme2510_probe(struct usb_interface *intf,
  772. const struct usb_device_id *id)
  773. {
  774. struct usb_device *udev = interface_to_usbdev(intf);
  775. int ret = 0;
  776. usb_reset_configuration(udev);
  777. usb_set_interface(udev, intf->cur_altsetting->desc.bInterfaceNumber, 1);
  778. if (udev->speed != USB_SPEED_HIGH) {
  779. ret = usb_reset_device(udev);
  780. info("DEV Failed to connect in HIGH SPEED mode");
  781. return -ENODEV;
  782. }
  783. lme_firmware_switch(udev, 0);
  784. if (0 == dvb_usb_device_init(intf, &lme2510_properties,
  785. THIS_MODULE, NULL, adapter_nr)) {
  786. info("DEV registering device driver");
  787. return 0;
  788. }
  789. if (0 == dvb_usb_device_init(intf, &lme2510c_properties,
  790. THIS_MODULE, NULL, adapter_nr)) {
  791. info("DEV registering device driver");
  792. return 0;
  793. }
  794. info("DEV lme2510 Error");
  795. return -ENODEV;
  796. }
  797. static struct usb_device_id lme2510_table[] = {
  798. { USB_DEVICE(0x3344, 0x1122) }, /* LME2510 */
  799. { USB_DEVICE(0x3344, 0x1120) }, /* LME2510C */
  800. {} /* Terminating entry */
  801. };
  802. MODULE_DEVICE_TABLE(usb, lme2510_table);
  803. static struct dvb_usb_device_properties lme2510_properties = {
  804. .caps = DVB_USB_IS_AN_I2C_ADAPTER,
  805. .usb_ctrl = DEVICE_SPECIFIC,
  806. .download_firmware = lme2510_download_firmware,
  807. .firmware = "dvb-usb-lme2510-lg.fw",
  808. .size_of_priv = sizeof(struct lme2510_state),
  809. .num_adapters = 1,
  810. .adapter = {
  811. {
  812. .streaming_ctrl = lme2510_streaming_ctrl,
  813. .frontend_attach = dm04_lme2510_frontend_attach,
  814. /* parameter for the MPEG2-data transfer */
  815. .stream = {
  816. .type = USB_BULK,
  817. .count = 10,
  818. .endpoint = 0x06,
  819. .u = {
  820. .bulk = {
  821. .buffersize = 4096,
  822. }
  823. }
  824. }
  825. }
  826. },
  827. .power_ctrl = lme2510_powerup,
  828. .identify_state = lme2510_identify_state,
  829. .i2c_algo = &lme2510_i2c_algo,
  830. .generic_bulk_ctrl_endpoint = 0,
  831. .num_device_descs = 1,
  832. .devices = {
  833. { "DM04 LME2510 DVB-S USB 2.0",
  834. { &lme2510_table[0], NULL },
  835. },
  836. }
  837. };
  838. static struct dvb_usb_device_properties lme2510c_properties = {
  839. .caps = DVB_USB_IS_AN_I2C_ADAPTER,
  840. .usb_ctrl = DEVICE_SPECIFIC,
  841. .download_firmware = lme2510_download_firmware,
  842. .firmware = lme_firmware,
  843. .size_of_priv = sizeof(struct lme2510_state),
  844. .num_adapters = 1,
  845. .adapter = {
  846. {
  847. .streaming_ctrl = lme2510_streaming_ctrl,
  848. .frontend_attach = dm04_lme2510_frontend_attach,
  849. /* parameter for the MPEG2-data transfer */
  850. .stream = {
  851. .type = USB_BULK,
  852. .count = 10,
  853. .endpoint = 0x8,
  854. .u = {
  855. .bulk = {
  856. .buffersize = 4096,
  857. }
  858. }
  859. }
  860. }
  861. },
  862. .power_ctrl = lme2510_powerup,
  863. .identify_state = lme2510_identify_state,
  864. .i2c_algo = &lme2510_i2c_algo,
  865. .generic_bulk_ctrl_endpoint = 0,
  866. .num_device_descs = 1,
  867. .devices = {
  868. { "DM04 LME2510C USB2.0",
  869. { &lme2510_table[1], NULL },
  870. },
  871. }
  872. };
  873. void *lme2510_exit_int(struct dvb_usb_device *d)
  874. {
  875. struct lme2510_state *st = d->priv;
  876. struct dvb_usb_adapter *adap = &d->adapter[0];
  877. void *buffer = NULL;
  878. if (adap != NULL) {
  879. lme2510_kill_urb(&adap->stream);
  880. adap->feedcount = 0;
  881. }
  882. if (st->lme_urb != NULL) {
  883. st->i2c_talk_onoff = 1;
  884. st->signal_lock = 0;
  885. st->signal_level = 0;
  886. st->signal_sn = 0;
  887. buffer = st->usb_buffer;
  888. usb_kill_urb(st->lme_urb);
  889. usb_free_coherent(d->udev, 5000, st->buffer,
  890. st->lme_urb->transfer_dma);
  891. info("Interupt Service Stopped");
  892. ir_input_unregister(d->rc_input_dev);
  893. info("Remote Stopped");
  894. }
  895. return buffer;
  896. }
  897. void lme2510_exit(struct usb_interface *intf)
  898. {
  899. struct dvb_usb_device *d = usb_get_intfdata(intf);
  900. void *usb_buffer;
  901. if (d != NULL) {
  902. usb_buffer = lme2510_exit_int(d);
  903. dvb_usb_device_exit(intf);
  904. kfree(usb_buffer);
  905. }
  906. }
  907. static struct usb_driver lme2510_driver = {
  908. .name = "LME2510C_DVBS",
  909. .probe = lme2510_probe,
  910. .disconnect = lme2510_exit,
  911. .id_table = lme2510_table,
  912. };
  913. /* module stuff */
  914. static int __init lme2510_module_init(void)
  915. {
  916. int result = usb_register(&lme2510_driver);
  917. if (result) {
  918. err("usb_register failed. Error number %d", result);
  919. return result;
  920. }
  921. return 0;
  922. }
  923. static void __exit lme2510_module_exit(void)
  924. {
  925. /* deregister this driver from the USB subsystem */
  926. usb_deregister(&lme2510_driver);
  927. }
  928. module_init(lme2510_module_init);
  929. module_exit(lme2510_module_exit);
  930. MODULE_AUTHOR("Malcolm Priestley <tvboxspy@gmail.com>");
  931. MODULE_DESCRIPTION("LM2510(C) DVB-S USB2.0");
  932. MODULE_VERSION("1.60");
  933. MODULE_LICENSE("GPL");