dibusb-common.c 12 KB

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