dibusb-common.c 12 KB

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