dibusb-common.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /* Common methods for dibusb-based-receivers.
  2. *
  3. * Copyright (C) 2004-5 Patrick Boettcher (patrick.boettcher@desy.de)
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation, version 2.
  8. *
  9. * see Documentation/dvb/README.dvb-usb for more information
  10. */
  11. #include "dibusb.h"
  12. static int debug;
  13. module_param(debug, int, 0644);
  14. MODULE_PARM_DESC(debug, "set debugging level (1=info (|-able))." DVB_USB_DEBUG_STATUS);
  15. MODULE_LICENSE("GPL");
  16. #define deb_info(args...) dprintk(debug,0x01,args)
  17. /* common stuff used by the different dibusb modules */
  18. int dibusb_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
  19. {
  20. if (adap->priv != NULL) {
  21. struct dibusb_state *st = adap->priv;
  22. if (st->ops.fifo_ctrl != NULL)
  23. if (st->ops.fifo_ctrl(adap->fe_adap[0].fe, onoff)) {
  24. err("error while controlling the fifo of the demod.");
  25. return -ENODEV;
  26. }
  27. }
  28. return 0;
  29. }
  30. EXPORT_SYMBOL(dibusb_streaming_ctrl);
  31. int dibusb_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid, int onoff)
  32. {
  33. if (adap->priv != NULL) {
  34. struct dibusb_state *st = adap->priv;
  35. if (st->ops.pid_ctrl != NULL)
  36. st->ops.pid_ctrl(adap->fe_adap[0].fe,
  37. index, pid, onoff);
  38. }
  39. return 0;
  40. }
  41. EXPORT_SYMBOL(dibusb_pid_filter);
  42. int dibusb_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
  43. {
  44. if (adap->priv != NULL) {
  45. struct dibusb_state *st = adap->priv;
  46. if (st->ops.pid_parse != NULL)
  47. if (st->ops.pid_parse(adap->fe_adap[0].fe, onoff) < 0)
  48. err("could not handle pid_parser");
  49. }
  50. return 0;
  51. }
  52. EXPORT_SYMBOL(dibusb_pid_filter_ctrl);
  53. int dibusb_power_ctrl(struct dvb_usb_device *d, int onoff)
  54. {
  55. u8 b[3];
  56. int ret;
  57. b[0] = DIBUSB_REQ_SET_IOCTL;
  58. b[1] = DIBUSB_IOCTL_CMD_POWER_MODE;
  59. b[2] = onoff ? DIBUSB_IOCTL_POWER_WAKEUP : DIBUSB_IOCTL_POWER_SLEEP;
  60. ret = dvb_usb_generic_write(d,b,3);
  61. msleep(10);
  62. return ret;
  63. }
  64. EXPORT_SYMBOL(dibusb_power_ctrl);
  65. int dibusb2_0_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
  66. {
  67. u8 b[3] = { 0 };
  68. int ret;
  69. if ((ret = dibusb_streaming_ctrl(adap,onoff)) < 0)
  70. return ret;
  71. if (onoff) {
  72. b[0] = DIBUSB_REQ_SET_STREAMING_MODE;
  73. b[1] = 0x00;
  74. if ((ret = dvb_usb_generic_write(adap->dev,b,2)) < 0)
  75. return ret;
  76. }
  77. b[0] = DIBUSB_REQ_SET_IOCTL;
  78. b[1] = onoff ? DIBUSB_IOCTL_CMD_ENABLE_STREAM : DIBUSB_IOCTL_CMD_DISABLE_STREAM;
  79. return dvb_usb_generic_write(adap->dev,b,3);
  80. }
  81. EXPORT_SYMBOL(dibusb2_0_streaming_ctrl);
  82. int dibusb2_0_power_ctrl(struct dvb_usb_device *d, int onoff)
  83. {
  84. if (onoff) {
  85. u8 b[3] = { DIBUSB_REQ_SET_IOCTL, DIBUSB_IOCTL_CMD_POWER_MODE, DIBUSB_IOCTL_POWER_WAKEUP };
  86. return dvb_usb_generic_write(d,b,3);
  87. } else
  88. return 0;
  89. }
  90. EXPORT_SYMBOL(dibusb2_0_power_ctrl);
  91. static int dibusb_i2c_msg(struct dvb_usb_device *d, u8 addr,
  92. u8 *wbuf, u16 wlen, u8 *rbuf, u16 rlen)
  93. {
  94. u8 sndbuf[wlen+4]; /* lead(1) devaddr,direction(1) addr(2) data(wlen) (len(2) (when reading)) */
  95. /* write only ? */
  96. int wo = (rbuf == NULL || rlen == 0),
  97. len = 2 + wlen + (wo ? 0 : 2);
  98. sndbuf[0] = wo ? DIBUSB_REQ_I2C_WRITE : DIBUSB_REQ_I2C_READ;
  99. sndbuf[1] = (addr << 1) | (wo ? 0 : 1);
  100. memcpy(&sndbuf[2],wbuf,wlen);
  101. if (!wo) {
  102. sndbuf[wlen+2] = (rlen >> 8) & 0xff;
  103. sndbuf[wlen+3] = rlen & 0xff;
  104. }
  105. return dvb_usb_generic_rw(d,sndbuf,len,rbuf,rlen,0);
  106. }
  107. /*
  108. * I2C master xfer function
  109. */
  110. static int dibusb_i2c_xfer(struct i2c_adapter *adap,struct i2c_msg msg[],int num)
  111. {
  112. struct dvb_usb_device *d = i2c_get_adapdata(adap);
  113. int i;
  114. if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
  115. return -EAGAIN;
  116. for (i = 0; i < num; i++) {
  117. /* write/read request */
  118. if (i+1 < num && (msg[i].flags & I2C_M_RD) == 0
  119. && (msg[i+1].flags & I2C_M_RD)) {
  120. if (dibusb_i2c_msg(d, msg[i].addr, msg[i].buf,msg[i].len,
  121. msg[i+1].buf,msg[i+1].len) < 0)
  122. break;
  123. i++;
  124. } else if ((msg[i].flags & I2C_M_RD) == 0) {
  125. if (dibusb_i2c_msg(d, msg[i].addr, msg[i].buf,msg[i].len,NULL,0) < 0)
  126. break;
  127. } else if (msg[i].addr != 0x50) {
  128. /* 0x50 is the address of the eeprom - we need to protect it
  129. * from dibusb's bad i2c implementation: reads without
  130. * writing the offset before are forbidden */
  131. if (dibusb_i2c_msg(d, msg[i].addr, NULL, 0, msg[i].buf, msg[i].len) < 0)
  132. break;
  133. }
  134. }
  135. mutex_unlock(&d->i2c_mutex);
  136. return i;
  137. }
  138. static u32 dibusb_i2c_func(struct i2c_adapter *adapter)
  139. {
  140. return I2C_FUNC_I2C;
  141. }
  142. struct i2c_algorithm dibusb_i2c_algo = {
  143. .master_xfer = dibusb_i2c_xfer,
  144. .functionality = dibusb_i2c_func,
  145. };
  146. EXPORT_SYMBOL(dibusb_i2c_algo);
  147. int dibusb_read_eeprom_byte(struct dvb_usb_device *d, u8 offs, u8 *val)
  148. {
  149. u8 wbuf[1] = { offs };
  150. return dibusb_i2c_msg(d, 0x50, wbuf, 1, val, 1);
  151. }
  152. EXPORT_SYMBOL(dibusb_read_eeprom_byte);
  153. /* 3000MC/P stuff */
  154. // Config Adjacent channels Perf -cal22
  155. static struct dibx000_agc_config dib3000p_mt2060_agc_config = {
  156. .band_caps = BAND_VHF | BAND_UHF,
  157. .setup = (1 << 8) | (5 << 5) | (1 << 4) | (1 << 3) | (0 << 2) | (2 << 0),
  158. .agc1_max = 48497,
  159. .agc1_min = 23593,
  160. .agc2_max = 46531,
  161. .agc2_min = 24904,
  162. .agc1_pt1 = 0x65,
  163. .agc1_pt2 = 0x69,
  164. .agc1_slope1 = 0x51,
  165. .agc1_slope2 = 0x27,
  166. .agc2_pt1 = 0,
  167. .agc2_pt2 = 0x33,
  168. .agc2_slope1 = 0x35,
  169. .agc2_slope2 = 0x37,
  170. };
  171. static struct dib3000mc_config stk3000p_dib3000p_config = {
  172. &dib3000p_mt2060_agc_config,
  173. .max_time = 0x196,
  174. .ln_adc_level = 0x1cc7,
  175. .output_mpeg2_in_188_bytes = 1,
  176. .agc_command1 = 1,
  177. .agc_command2 = 1,
  178. };
  179. static struct dibx000_agc_config dib3000p_panasonic_agc_config = {
  180. .band_caps = BAND_VHF | BAND_UHF,
  181. .setup = (1 << 8) | (5 << 5) | (1 << 4) | (1 << 3) | (0 << 2) | (2 << 0),
  182. .agc1_max = 56361,
  183. .agc1_min = 22282,
  184. .agc2_max = 47841,
  185. .agc2_min = 36045,
  186. .agc1_pt1 = 0x3b,
  187. .agc1_pt2 = 0x6b,
  188. .agc1_slope1 = 0x55,
  189. .agc1_slope2 = 0x1d,
  190. .agc2_pt1 = 0,
  191. .agc2_pt2 = 0x0a,
  192. .agc2_slope1 = 0x95,
  193. .agc2_slope2 = 0x1e,
  194. };
  195. #if defined(CONFIG_DVB_DIB3000MC) || \
  196. (defined(CONFIG_DVB_DIB3000MC_MODULE) && defined(MODULE))
  197. static struct dib3000mc_config mod3000p_dib3000p_config = {
  198. &dib3000p_panasonic_agc_config,
  199. .max_time = 0x51,
  200. .ln_adc_level = 0x1cc7,
  201. .output_mpeg2_in_188_bytes = 1,
  202. .agc_command1 = 1,
  203. .agc_command2 = 1,
  204. };
  205. int dibusb_dib3000mc_frontend_attach(struct dvb_usb_adapter *adap)
  206. {
  207. if (adap->dev->udev->descriptor.idVendor == USB_VID_LITEON &&
  208. adap->dev->udev->descriptor.idProduct ==
  209. USB_PID_LITEON_DVB_T_WARM) {
  210. msleep(1000);
  211. }
  212. adap->fe_adap[0].fe = dvb_attach(dib3000mc_attach,
  213. &adap->dev->i2c_adap,
  214. DEFAULT_DIB3000P_I2C_ADDRESS,
  215. &mod3000p_dib3000p_config);
  216. if ((adap->fe_adap[0].fe) == NULL)
  217. adap->fe_adap[0].fe = dvb_attach(dib3000mc_attach,
  218. &adap->dev->i2c_adap,
  219. DEFAULT_DIB3000MC_I2C_ADDRESS,
  220. &mod3000p_dib3000p_config);
  221. if ((adap->fe_adap[0].fe) != NULL) {
  222. if (adap->priv != NULL) {
  223. struct dibusb_state *st = adap->priv;
  224. st->ops.pid_parse = dib3000mc_pid_parse;
  225. st->ops.pid_ctrl = dib3000mc_pid_control;
  226. }
  227. return 0;
  228. }
  229. return -ENODEV;
  230. }
  231. EXPORT_SYMBOL(dibusb_dib3000mc_frontend_attach);
  232. static struct mt2060_config stk3000p_mt2060_config = {
  233. 0x60
  234. };
  235. int dibusb_dib3000mc_tuner_attach(struct dvb_usb_adapter *adap)
  236. {
  237. struct dibusb_state *st = adap->priv;
  238. u8 a,b;
  239. u16 if1 = 1220;
  240. struct i2c_adapter *tun_i2c;
  241. // First IF calibration for Liteon Sticks
  242. if (adap->dev->udev->descriptor.idVendor == USB_VID_LITEON &&
  243. adap->dev->udev->descriptor.idProduct == USB_PID_LITEON_DVB_T_WARM) {
  244. dibusb_read_eeprom_byte(adap->dev,0x7E,&a);
  245. dibusb_read_eeprom_byte(adap->dev,0x7F,&b);
  246. if (a == 0x00)
  247. if1 += b;
  248. else if (a == 0x80)
  249. if1 -= b;
  250. else
  251. warn("LITE-ON DVB-T: Strange IF1 calibration :%2X %2X\n", a, b);
  252. } else if (adap->dev->udev->descriptor.idVendor == USB_VID_DIBCOM &&
  253. adap->dev->udev->descriptor.idProduct == USB_PID_DIBCOM_MOD3001_WARM) {
  254. u8 desc;
  255. dibusb_read_eeprom_byte(adap->dev, 7, &desc);
  256. if (desc == 2) {
  257. a = 127;
  258. do {
  259. dibusb_read_eeprom_byte(adap->dev, a, &desc);
  260. a--;
  261. } while (a > 7 && (desc == 0xff || desc == 0x00));
  262. if (desc & 0x80)
  263. if1 -= (0xff - desc);
  264. else
  265. if1 += desc;
  266. }
  267. }
  268. tun_i2c = dib3000mc_get_tuner_i2c_master(adap->fe_adap[0].fe, 1);
  269. if (dvb_attach(mt2060_attach, adap->fe_adap[0].fe, tun_i2c, &stk3000p_mt2060_config, if1) == NULL) {
  270. /* not found - use panasonic pll parameters */
  271. if (dvb_attach(dvb_pll_attach, adap->fe_adap[0].fe, 0x60, tun_i2c, DVB_PLL_ENV57H1XD5) == NULL)
  272. return -ENOMEM;
  273. } else {
  274. st->mt2060_present = 1;
  275. /* set the correct parameters for the dib3000p */
  276. dib3000mc_set_config(adap->fe_adap[0].fe, &stk3000p_dib3000p_config);
  277. }
  278. return 0;
  279. }
  280. EXPORT_SYMBOL(dibusb_dib3000mc_tuner_attach);
  281. #endif
  282. /*
  283. * common remote control stuff
  284. */
  285. struct rc_map_table rc_map_dibusb_table[] = {
  286. /* Key codes for the little Artec T1/Twinhan/HAMA/ remote. */
  287. { 0x0016, KEY_POWER },
  288. { 0x0010, KEY_MUTE },
  289. { 0x0003, KEY_1 },
  290. { 0x0001, KEY_2 },
  291. { 0x0006, KEY_3 },
  292. { 0x0009, KEY_4 },
  293. { 0x001d, KEY_5 },
  294. { 0x001f, KEY_6 },
  295. { 0x000d, KEY_7 },
  296. { 0x0019, KEY_8 },
  297. { 0x001b, KEY_9 },
  298. { 0x0015, KEY_0 },
  299. { 0x0005, KEY_CHANNELUP },
  300. { 0x0002, KEY_CHANNELDOWN },
  301. { 0x001e, KEY_VOLUMEUP },
  302. { 0x000a, KEY_VOLUMEDOWN },
  303. { 0x0011, KEY_RECORD },
  304. { 0x0017, KEY_FAVORITES }, /* Heart symbol - Channel list. */
  305. { 0x0014, KEY_PLAY },
  306. { 0x001a, KEY_STOP },
  307. { 0x0040, KEY_REWIND },
  308. { 0x0012, KEY_FASTFORWARD },
  309. { 0x000e, KEY_PREVIOUS }, /* Recall - Previous channel. */
  310. { 0x004c, KEY_PAUSE },
  311. { 0x004d, KEY_SCREEN }, /* Full screen mode. */
  312. { 0x0054, KEY_AUDIO }, /* MTS - Switch to secondary audio. */
  313. /* additional keys TwinHan VisionPlus, the Artec seemingly not have */
  314. { 0x000c, KEY_CANCEL }, /* Cancel */
  315. { 0x001c, KEY_EPG }, /* EPG */
  316. { 0x0000, KEY_TAB }, /* Tab */
  317. { 0x0048, KEY_INFO }, /* Preview */
  318. { 0x0004, KEY_LIST }, /* RecordList */
  319. { 0x000f, KEY_TEXT }, /* Teletext */
  320. /* Key codes for the KWorld/ADSTech/JetWay remote. */
  321. { 0x8612, KEY_POWER },
  322. { 0x860f, KEY_SELECT }, /* source */
  323. { 0x860c, KEY_UNKNOWN }, /* scan */
  324. { 0x860b, KEY_EPG },
  325. { 0x8610, KEY_MUTE },
  326. { 0x8601, KEY_1 },
  327. { 0x8602, KEY_2 },
  328. { 0x8603, KEY_3 },
  329. { 0x8604, KEY_4 },
  330. { 0x8605, KEY_5 },
  331. { 0x8606, KEY_6 },
  332. { 0x8607, KEY_7 },
  333. { 0x8608, KEY_8 },
  334. { 0x8609, KEY_9 },
  335. { 0x860a, KEY_0 },
  336. { 0x8618, KEY_ZOOM },
  337. { 0x861c, KEY_UNKNOWN }, /* preview */
  338. { 0x8613, KEY_UNKNOWN }, /* snap */
  339. { 0x8600, KEY_UNDO },
  340. { 0x861d, KEY_RECORD },
  341. { 0x860d, KEY_STOP },
  342. { 0x860e, KEY_PAUSE },
  343. { 0x8616, KEY_PLAY },
  344. { 0x8611, KEY_BACK },
  345. { 0x8619, KEY_FORWARD },
  346. { 0x8614, KEY_UNKNOWN }, /* pip */
  347. { 0x8615, KEY_ESC },
  348. { 0x861a, KEY_UP },
  349. { 0x861e, KEY_DOWN },
  350. { 0x861f, KEY_LEFT },
  351. { 0x861b, KEY_RIGHT },
  352. /* Key codes for the DiBcom MOD3000 remote. */
  353. { 0x8000, KEY_MUTE },
  354. { 0x8001, KEY_TEXT },
  355. { 0x8002, KEY_HOME },
  356. { 0x8003, KEY_POWER },
  357. { 0x8004, KEY_RED },
  358. { 0x8005, KEY_GREEN },
  359. { 0x8006, KEY_YELLOW },
  360. { 0x8007, KEY_BLUE },
  361. { 0x8008, KEY_DVD },
  362. { 0x8009, KEY_AUDIO },
  363. { 0x800a, KEY_IMAGES }, /* Pictures */
  364. { 0x800b, KEY_VIDEO },
  365. { 0x800c, KEY_BACK },
  366. { 0x800d, KEY_UP },
  367. { 0x800e, KEY_RADIO },
  368. { 0x800f, KEY_EPG },
  369. { 0x8010, KEY_LEFT },
  370. { 0x8011, KEY_OK },
  371. { 0x8012, KEY_RIGHT },
  372. { 0x8013, KEY_UNKNOWN }, /* SAP */
  373. { 0x8014, KEY_TV },
  374. { 0x8015, KEY_DOWN },
  375. { 0x8016, KEY_MENU }, /* DVD Menu */
  376. { 0x8017, KEY_LAST },
  377. { 0x8018, KEY_RECORD },
  378. { 0x8019, KEY_STOP },
  379. { 0x801a, KEY_PAUSE },
  380. { 0x801b, KEY_PLAY },
  381. { 0x801c, KEY_PREVIOUS },
  382. { 0x801d, KEY_REWIND },
  383. { 0x801e, KEY_FASTFORWARD },
  384. { 0x801f, KEY_NEXT},
  385. { 0x8040, KEY_1 },
  386. { 0x8041, KEY_2 },
  387. { 0x8042, KEY_3 },
  388. { 0x8043, KEY_CHANNELUP },
  389. { 0x8044, KEY_4 },
  390. { 0x8045, KEY_5 },
  391. { 0x8046, KEY_6 },
  392. { 0x8047, KEY_CHANNELDOWN },
  393. { 0x8048, KEY_7 },
  394. { 0x8049, KEY_8 },
  395. { 0x804a, KEY_9 },
  396. { 0x804b, KEY_VOLUMEUP },
  397. { 0x804c, KEY_CLEAR },
  398. { 0x804d, KEY_0 },
  399. { 0x804e, KEY_ENTER },
  400. { 0x804f, KEY_VOLUMEDOWN },
  401. };
  402. EXPORT_SYMBOL(rc_map_dibusb_table);
  403. int dibusb_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
  404. {
  405. u8 key[5],cmd = DIBUSB_REQ_POLL_REMOTE;
  406. dvb_usb_generic_rw(d,&cmd,1,key,5,0);
  407. dvb_usb_nec_rc_key_to_event(d,key,event,state);
  408. if (key[0] != 0)
  409. deb_info("key: %x %x %x %x %x\n",key[0],key[1],key[2],key[3],key[4]);
  410. return 0;
  411. }
  412. EXPORT_SYMBOL(dibusb_rc_query);