dibusb-common.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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_device *d, int onoff)
  19. {
  20. if (d->priv != NULL) {
  21. struct dibusb_state *st = d->priv;
  22. if (st->ops.fifo_ctrl != NULL)
  23. if (st->ops.fifo_ctrl(d->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_device *d, int index, u16 pid, int onoff)
  32. {
  33. if (d->priv != NULL) {
  34. struct dibusb_state *st = d->priv;
  35. if (st->ops.pid_ctrl != NULL)
  36. st->ops.pid_ctrl(d->fe,index,pid,onoff);
  37. }
  38. return 0;
  39. }
  40. EXPORT_SYMBOL(dibusb_pid_filter);
  41. int dibusb_pid_filter_ctrl(struct dvb_usb_device *d, int onoff)
  42. {
  43. if (d->priv != NULL) {
  44. struct dibusb_state *st = d->priv;
  45. if (st->ops.pid_parse != NULL)
  46. if (st->ops.pid_parse(d->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_device *d, int onoff)
  65. {
  66. u8 b[3] = { 0 };
  67. int ret;
  68. if ((ret = dibusb_streaming_ctrl(d,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(d,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(d,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+1].flags & I2C_M_RD)) {
  118. if (dibusb_i2c_msg(d, msg[i].addr, msg[i].buf,msg[i].len,
  119. msg[i+1].buf,msg[i+1].len) < 0)
  120. break;
  121. i++;
  122. } else
  123. if (dibusb_i2c_msg(d, msg[i].addr, msg[i].buf,msg[i].len,NULL,0) < 0)
  124. break;
  125. }
  126. mutex_unlock(&d->i2c_mutex);
  127. return i;
  128. }
  129. static u32 dibusb_i2c_func(struct i2c_adapter *adapter)
  130. {
  131. return I2C_FUNC_I2C;
  132. }
  133. struct i2c_algorithm dibusb_i2c_algo = {
  134. .master_xfer = dibusb_i2c_xfer,
  135. .functionality = dibusb_i2c_func,
  136. };
  137. EXPORT_SYMBOL(dibusb_i2c_algo);
  138. int dibusb_read_eeprom_byte(struct dvb_usb_device *d, u8 offs, u8 *val)
  139. {
  140. u8 wbuf[1] = { offs };
  141. return dibusb_i2c_msg(d, 0x50, wbuf, 1, val, 1);
  142. }
  143. EXPORT_SYMBOL(dibusb_read_eeprom_byte);
  144. /* 3000MC/P stuff */
  145. // Config Adjacent channels Perf -cal22
  146. static struct dibx000_agc_config dib3000p_mt2060_agc_config = {
  147. .band_caps = BAND_VHF | BAND_UHF,
  148. .setup = (0 << 15) | (0 << 14) | (1 << 13) | (1 << 12) | (29 << 0),
  149. .agc1_max = 48497,
  150. .agc1_min = 23593,
  151. .agc2_max = 46531,
  152. .agc2_min = 24904,
  153. .agc1_pt1 = 0x65,
  154. .agc1_pt2 = 0x69,
  155. .agc1_slope1 = 0x51,
  156. .agc1_slope2 = 0x27,
  157. .agc2_pt1 = 0,
  158. .agc2_pt2 = 0x33,
  159. .agc2_slope1 = 0x35,
  160. .agc2_slope2 = 0x37,
  161. };
  162. static struct dib3000mc_config stk3000p_dib3000p_config = {
  163. &dib3000p_mt2060_agc_config,
  164. .max_time = 0x196,
  165. .ln_adc_level = 0x1cc7,
  166. .output_mpeg2_in_188_bytes = 1,
  167. };
  168. static struct dibx000_agc_config dib3000p_panasonic_agc_config = {
  169. .setup = (0 << 15) | (0 << 14) | (1 << 13) | (1 << 12) | (29 << 0),
  170. .agc1_max = 56361,
  171. .agc1_min = 22282,
  172. .agc2_max = 47841,
  173. .agc2_min = 36045,
  174. .agc1_pt1 = 0x3b,
  175. .agc1_pt2 = 0x6b,
  176. .agc1_slope1 = 0x55,
  177. .agc1_slope2 = 0x1d,
  178. .agc2_pt1 = 0,
  179. .agc2_pt2 = 0x0a,
  180. .agc2_slope1 = 0x95,
  181. .agc2_slope2 = 0x1e,
  182. };
  183. static struct dib3000mc_config mod3000p_dib3000p_config = {
  184. &dib3000p_panasonic_agc_config,
  185. .max_time = 0x51,
  186. .ln_adc_level = 0x1cc7,
  187. .output_mpeg2_in_188_bytes = 1,
  188. };
  189. int dibusb_dib3000mc_frontend_attach(struct dvb_usb_device *d)
  190. {
  191. if (dib3000mc_attach(&d->i2c_adap, 1, DEFAULT_DIB3000P_I2C_ADDRESS, 0, &mod3000p_dib3000p_config, &d->fe) == 0 ||
  192. dib3000mc_attach(&d->i2c_adap, 1, DEFAULT_DIB3000MC_I2C_ADDRESS, 0, &mod3000p_dib3000p_config, &d->fe) == 0) {
  193. if (d->priv != NULL) {
  194. struct dibusb_state *st = d->priv;
  195. st->ops.pid_parse = dib3000mc_pid_parse;
  196. st->ops.pid_ctrl = dib3000mc_pid_control;
  197. }
  198. return 0;
  199. }
  200. return -ENODEV;
  201. }
  202. EXPORT_SYMBOL(dibusb_dib3000mc_frontend_attach);
  203. static struct mt2060_config stk3000p_mt2060_config = {
  204. 0x60
  205. };
  206. int dibusb_dib3000mc_tuner_attach (struct dvb_usb_device *d)
  207. {
  208. struct dibusb_state *st = d->priv;
  209. int ret;
  210. u8 a,b;
  211. u16 if1 = 1220;
  212. struct i2c_adapter *tun_i2c;
  213. // First IF calibration for Liteon Sticks
  214. if (d->udev->descriptor.idVendor == USB_VID_LITEON &&
  215. d->udev->descriptor.idProduct == USB_PID_LITEON_DVB_T_WARM) {
  216. dibusb_read_eeprom_byte(d,0x7E,&a);
  217. dibusb_read_eeprom_byte(d,0x7F,&b);
  218. if (a == 0x00)
  219. if1 += b;
  220. else if (a == 0x80)
  221. if1 -= b;
  222. else
  223. warn("LITE-ON DVB-T: Strange IF1 calibration :%2X %2X\n", a, b);
  224. } else if (d->udev->descriptor.idVendor == USB_VID_DIBCOM &&
  225. d->udev->descriptor.idProduct == USB_PID_DIBCOM_MOD3001_WARM) {
  226. u8 desc;
  227. dibusb_read_eeprom_byte(d, 7, &desc);
  228. if (desc == 2) {
  229. a = 127;
  230. do {
  231. dibusb_read_eeprom_byte(d, a, &desc);
  232. a--;
  233. } while (a > 7 && (desc == 0xff || desc == 0x00));
  234. if (desc & 0x80)
  235. if1 -= (0xff - desc);
  236. else
  237. if1 += desc;
  238. }
  239. }
  240. tun_i2c = dib3000mc_get_tuner_i2c_master(d->fe, 1);
  241. if ((ret = mt2060_attach(d->fe, tun_i2c, &stk3000p_mt2060_config, if1)) != 0) {
  242. /* not found - use panasonic pll parameters */
  243. if (dvb_pll_attach(d->fe, 0x60, tun_i2c, &dvb_pll_env57h1xd5) == NULL)
  244. return -ENOMEM;
  245. } else {
  246. st->mt2060_present = 1;
  247. /* set the correct parameters for the dib3000p */
  248. dib3000mc_set_config(d->fe, &stk3000p_dib3000p_config);
  249. }
  250. return 0;
  251. }
  252. EXPORT_SYMBOL(dibusb_dib3000mc_tuner_attach);
  253. /*
  254. * common remote control stuff
  255. */
  256. struct dvb_usb_rc_key dibusb_rc_keys[] = {
  257. /* Key codes for the little Artec T1/Twinhan/HAMA/ remote. */
  258. { 0x00, 0x16, KEY_POWER },
  259. { 0x00, 0x10, KEY_MUTE },
  260. { 0x00, 0x03, KEY_1 },
  261. { 0x00, 0x01, KEY_2 },
  262. { 0x00, 0x06, KEY_3 },
  263. { 0x00, 0x09, KEY_4 },
  264. { 0x00, 0x1d, KEY_5 },
  265. { 0x00, 0x1f, KEY_6 },
  266. { 0x00, 0x0d, KEY_7 },
  267. { 0x00, 0x19, KEY_8 },
  268. { 0x00, 0x1b, KEY_9 },
  269. { 0x00, 0x15, KEY_0 },
  270. { 0x00, 0x05, KEY_CHANNELUP },
  271. { 0x00, 0x02, KEY_CHANNELDOWN },
  272. { 0x00, 0x1e, KEY_VOLUMEUP },
  273. { 0x00, 0x0a, KEY_VOLUMEDOWN },
  274. { 0x00, 0x11, KEY_RECORD },
  275. { 0x00, 0x17, KEY_FAVORITES }, /* Heart symbol - Channel list. */
  276. { 0x00, 0x14, KEY_PLAY },
  277. { 0x00, 0x1a, KEY_STOP },
  278. { 0x00, 0x40, KEY_REWIND },
  279. { 0x00, 0x12, KEY_FASTFORWARD },
  280. { 0x00, 0x0e, KEY_PREVIOUS }, /* Recall - Previous channel. */
  281. { 0x00, 0x4c, KEY_PAUSE },
  282. { 0x00, 0x4d, KEY_SCREEN }, /* Full screen mode. */
  283. { 0x00, 0x54, KEY_AUDIO }, /* MTS - Switch to secondary audio. */
  284. /* additional keys TwinHan VisionPlus, the Artec seemingly not have */
  285. { 0x00, 0x0c, KEY_CANCEL }, /* Cancel */
  286. { 0x00, 0x1c, KEY_EPG }, /* EPG */
  287. { 0x00, 0x00, KEY_TAB }, /* Tab */
  288. { 0x00, 0x48, KEY_INFO }, /* Preview */
  289. { 0x00, 0x04, KEY_LIST }, /* RecordList */
  290. { 0x00, 0x0f, KEY_TEXT }, /* Teletext */
  291. /* Key codes for the KWorld/ADSTech/JetWay remote. */
  292. { 0x86, 0x12, KEY_POWER },
  293. { 0x86, 0x0f, KEY_SELECT }, /* source */
  294. { 0x86, 0x0c, KEY_UNKNOWN }, /* scan */
  295. { 0x86, 0x0b, KEY_EPG },
  296. { 0x86, 0x10, KEY_MUTE },
  297. { 0x86, 0x01, KEY_1 },
  298. { 0x86, 0x02, KEY_2 },
  299. { 0x86, 0x03, KEY_3 },
  300. { 0x86, 0x04, KEY_4 },
  301. { 0x86, 0x05, KEY_5 },
  302. { 0x86, 0x06, KEY_6 },
  303. { 0x86, 0x07, KEY_7 },
  304. { 0x86, 0x08, KEY_8 },
  305. { 0x86, 0x09, KEY_9 },
  306. { 0x86, 0x0a, KEY_0 },
  307. { 0x86, 0x18, KEY_ZOOM },
  308. { 0x86, 0x1c, KEY_UNKNOWN }, /* preview */
  309. { 0x86, 0x13, KEY_UNKNOWN }, /* snap */
  310. { 0x86, 0x00, KEY_UNDO },
  311. { 0x86, 0x1d, KEY_RECORD },
  312. { 0x86, 0x0d, KEY_STOP },
  313. { 0x86, 0x0e, KEY_PAUSE },
  314. { 0x86, 0x16, KEY_PLAY },
  315. { 0x86, 0x11, KEY_BACK },
  316. { 0x86, 0x19, KEY_FORWARD },
  317. { 0x86, 0x14, KEY_UNKNOWN }, /* pip */
  318. { 0x86, 0x15, KEY_ESC },
  319. { 0x86, 0x1a, KEY_UP },
  320. { 0x86, 0x1e, KEY_DOWN },
  321. { 0x86, 0x1f, KEY_LEFT },
  322. { 0x86, 0x1b, KEY_RIGHT },
  323. /* Key codes for the DiBcom MOD3000 remote. */
  324. { 0x80, 0x00, KEY_MUTE },
  325. { 0x80, 0x01, KEY_TEXT },
  326. { 0x80, 0x02, KEY_HOME },
  327. { 0x80, 0x03, KEY_POWER },
  328. { 0x80, 0x04, KEY_RED },
  329. { 0x80, 0x05, KEY_GREEN },
  330. { 0x80, 0x06, KEY_YELLOW },
  331. { 0x80, 0x07, KEY_BLUE },
  332. { 0x80, 0x08, KEY_DVD },
  333. { 0x80, 0x09, KEY_AUDIO },
  334. { 0x80, 0x0a, KEY_MEDIA }, /* Pictures */
  335. { 0x80, 0x0b, KEY_VIDEO },
  336. { 0x80, 0x0c, KEY_BACK },
  337. { 0x80, 0x0d, KEY_UP },
  338. { 0x80, 0x0e, KEY_RADIO },
  339. { 0x80, 0x0f, KEY_EPG },
  340. { 0x80, 0x10, KEY_LEFT },
  341. { 0x80, 0x11, KEY_OK },
  342. { 0x80, 0x12, KEY_RIGHT },
  343. { 0x80, 0x13, KEY_UNKNOWN }, /* SAP */
  344. { 0x80, 0x14, KEY_TV },
  345. { 0x80, 0x15, KEY_DOWN },
  346. { 0x80, 0x16, KEY_MENU }, /* DVD Menu */
  347. { 0x80, 0x17, KEY_LAST },
  348. { 0x80, 0x18, KEY_RECORD },
  349. { 0x80, 0x19, KEY_STOP },
  350. { 0x80, 0x1a, KEY_PAUSE },
  351. { 0x80, 0x1b, KEY_PLAY },
  352. { 0x80, 0x1c, KEY_PREVIOUS },
  353. { 0x80, 0x1d, KEY_REWIND },
  354. { 0x80, 0x1e, KEY_FASTFORWARD },
  355. { 0x80, 0x1f, KEY_NEXT},
  356. { 0x80, 0x40, KEY_1 },
  357. { 0x80, 0x41, KEY_2 },
  358. { 0x80, 0x42, KEY_3 },
  359. { 0x80, 0x43, KEY_CHANNELUP },
  360. { 0x80, 0x44, KEY_4 },
  361. { 0x80, 0x45, KEY_5 },
  362. { 0x80, 0x46, KEY_6 },
  363. { 0x80, 0x47, KEY_CHANNELDOWN },
  364. { 0x80, 0x48, KEY_7 },
  365. { 0x80, 0x49, KEY_8 },
  366. { 0x80, 0x4a, KEY_9 },
  367. { 0x80, 0x4b, KEY_VOLUMEUP },
  368. { 0x80, 0x4c, KEY_CLEAR },
  369. { 0x80, 0x4d, KEY_0 },
  370. { 0x80, 0x4e, KEY_ENTER },
  371. { 0x80, 0x4f, KEY_VOLUMEDOWN },
  372. };
  373. EXPORT_SYMBOL(dibusb_rc_keys);
  374. int dibusb_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
  375. {
  376. u8 key[5],cmd = DIBUSB_REQ_POLL_REMOTE;
  377. dvb_usb_generic_rw(d,&cmd,1,key,5,0);
  378. dvb_usb_nec_rc_key_to_event(d,key,event,state);
  379. if (key[0] != 0)
  380. deb_info("key: %x %x %x %x %x\n",key[0],key[1],key[2],key[3],key[4]);
  381. return 0;
  382. }
  383. EXPORT_SYMBOL(dibusb_rc_query);