dibusb-common.c 12 KB

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