dibusb-common.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. if (num > 2)
  116. warn("more than 2 i2c messages at a time is not handled yet. TODO.");
  117. for (i = 0; i < num; i++) {
  118. /* write/read request */
  119. if (i+1 < num && (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
  125. if (dibusb_i2c_msg(d, msg[i].addr, msg[i].buf,msg[i].len,NULL,0) < 0)
  126. break;
  127. }
  128. mutex_unlock(&d->i2c_mutex);
  129. return i;
  130. }
  131. static u32 dibusb_i2c_func(struct i2c_adapter *adapter)
  132. {
  133. return I2C_FUNC_I2C;
  134. }
  135. struct i2c_algorithm dibusb_i2c_algo = {
  136. .master_xfer = dibusb_i2c_xfer,
  137. .functionality = dibusb_i2c_func,
  138. };
  139. EXPORT_SYMBOL(dibusb_i2c_algo);
  140. int dibusb_read_eeprom_byte(struct dvb_usb_device *d, u8 offs, u8 *val)
  141. {
  142. u8 wbuf[1] = { offs };
  143. return dibusb_i2c_msg(d, 0x50, wbuf, 1, val, 1);
  144. }
  145. EXPORT_SYMBOL(dibusb_read_eeprom_byte);
  146. static struct mt2060_config stk3000p_mt2060_config = {
  147. .i2c_address = 0x60,
  148. };
  149. static int dibusb_tuner_init(struct dvb_frontend *fe)
  150. {
  151. struct dvb_usb_device *d = fe->dvb->priv;
  152. struct dibusb_state *st = d->priv;
  153. if (d->tuner_pass_ctrl && st->mt2060_present) {
  154. int ret;
  155. d->tuner_pass_ctrl(d->fe, 1, stk3000p_mt2060_config.i2c_address);
  156. ret = mt2060_init(&st->mt2060);
  157. d->tuner_pass_ctrl(d->fe, 0, 0);
  158. return ret;
  159. }
  160. return dvb_usb_pll_init_i2c(fe);
  161. }
  162. static int dibusb_tuner_set(struct dvb_frontend *fe, struct dvb_frontend_parameters *fep)
  163. {
  164. struct dvb_usb_device *d = fe->dvb->priv;
  165. struct dibusb_state *st = d->priv;
  166. if (d->tuner_pass_ctrl && st->mt2060_present) {
  167. int ret;
  168. d->tuner_pass_ctrl(d->fe, 1, stk3000p_mt2060_config.i2c_address);
  169. ret = mt2060_set(&st->mt2060,fep);
  170. d->tuner_pass_ctrl(d->fe,0,0);
  171. return ret;
  172. }
  173. return dvb_usb_pll_set_i2c(fe,fep);
  174. }
  175. static const struct dib3000p_agc_config dib3000p_agc_panasonic_env57h1xd5 = {
  176. { 0x51, 0x301d, 0x0, 0x1cc7, 0xdc29, 0x570a,
  177. 0xbae1, 0x8ccd, 0x3b6d, 0x551d, 0xa, 0x951e }
  178. };
  179. static const struct dib3000p_agc_config dib3000p_agc_microtune_mt2060 = {
  180. { 0x196, 0x301d, 0x0, 0x1cc7, 0xffff, 0x5c29,
  181. 0xa8f6, 0x5eb8, 0x65ff, 0x40ff, 0x8a, 0x1114 }
  182. };
  183. int dibusb_dib3000mc_frontend_attach(struct dvb_usb_device *d)
  184. {
  185. struct dib3000_config demod_cfg;
  186. struct dibusb_state *st = d->priv;
  187. demod_cfg.agc = &dib3000p_agc_panasonic_env57h1xd5;
  188. demod_cfg.pll_set = dibusb_tuner_set;
  189. demod_cfg.pll_init = dibusb_tuner_init;
  190. for (demod_cfg.demod_address = 0x8; demod_cfg.demod_address < 0xd; demod_cfg.demod_address++)
  191. if ((d->fe = dib3000mc_attach(&demod_cfg,&d->i2c_adap,&st->ops)) != NULL) {
  192. d->tuner_pass_ctrl = st->ops.tuner_pass_ctrl;
  193. return 0;
  194. }
  195. return -ENODEV;
  196. }
  197. EXPORT_SYMBOL(dibusb_dib3000mc_frontend_attach);
  198. int dibusb_dib3000mc_tuner_attach (struct dvb_usb_device *d)
  199. {
  200. int ret;
  201. u8 a,b;
  202. u16 if1=1220;
  203. if (d->tuner_pass_ctrl) {
  204. struct dibusb_state *st = d->priv;
  205. d->tuner_pass_ctrl(d->fe, 1, stk3000p_mt2060_config.i2c_address);
  206. // First IF calibration for Liteon Sticks
  207. if (d->udev->descriptor.idVendor == USB_VID_LITEON &&
  208. d->udev->descriptor.idProduct == USB_PID_LITEON_DVB_T_WARM) {
  209. dibusb_read_eeprom_byte(d,0x7E,&a);
  210. dibusb_read_eeprom_byte(d,0x7F,&b);
  211. if (a == 0xFF && b == 0xFF) {
  212. if1 = 1220;
  213. } else
  214. if (a == 0x00) {
  215. if1 = 1220+b;
  216. } else
  217. if (a == 0x80) {
  218. if1 = 1220-b;
  219. } else {
  220. warn("LITE-ON DVB-T Tuner : Strange IF1 calibration :%2X %2X\n",(int)a,(int)b);
  221. if1 = 1220;
  222. }
  223. }
  224. if ((ret = mt2060_attach(&st->mt2060,&stk3000p_mt2060_config, &d->i2c_adap,if1)) != 0) {
  225. /* not found - use panasonic pll parameters */
  226. d->pll_addr = 0x60;
  227. d->pll_desc = &dvb_pll_env57h1xd5;
  228. } else {
  229. st->mt2060_present = 1;
  230. /* set the correct agc parameters for the dib3000p */
  231. dib3000mc_set_agc_config(d->fe, &dib3000p_agc_microtune_mt2060);
  232. }
  233. d->tuner_pass_ctrl(d->fe,0,0);
  234. }
  235. return 0;
  236. }
  237. EXPORT_SYMBOL(dibusb_dib3000mc_tuner_attach);
  238. /*
  239. * common remote control stuff
  240. */
  241. struct dvb_usb_rc_key dibusb_rc_keys[] = {
  242. /* Key codes for the little Artec T1/Twinhan/HAMA/ remote. */
  243. { 0x00, 0x16, KEY_POWER },
  244. { 0x00, 0x10, KEY_MUTE },
  245. { 0x00, 0x03, KEY_1 },
  246. { 0x00, 0x01, KEY_2 },
  247. { 0x00, 0x06, KEY_3 },
  248. { 0x00, 0x09, KEY_4 },
  249. { 0x00, 0x1d, KEY_5 },
  250. { 0x00, 0x1f, KEY_6 },
  251. { 0x00, 0x0d, KEY_7 },
  252. { 0x00, 0x19, KEY_8 },
  253. { 0x00, 0x1b, KEY_9 },
  254. { 0x00, 0x15, KEY_0 },
  255. { 0x00, 0x05, KEY_CHANNELUP },
  256. { 0x00, 0x02, KEY_CHANNELDOWN },
  257. { 0x00, 0x1e, KEY_VOLUMEUP },
  258. { 0x00, 0x0a, KEY_VOLUMEDOWN },
  259. { 0x00, 0x11, KEY_RECORD },
  260. { 0x00, 0x17, KEY_FAVORITES }, /* Heart symbol - Channel list. */
  261. { 0x00, 0x14, KEY_PLAY },
  262. { 0x00, 0x1a, KEY_STOP },
  263. { 0x00, 0x40, KEY_REWIND },
  264. { 0x00, 0x12, KEY_FASTFORWARD },
  265. { 0x00, 0x0e, KEY_PREVIOUS }, /* Recall - Previous channel. */
  266. { 0x00, 0x4c, KEY_PAUSE },
  267. { 0x00, 0x4d, KEY_SCREEN }, /* Full screen mode. */
  268. { 0x00, 0x54, KEY_AUDIO }, /* MTS - Switch to secondary audio. */
  269. /* additional keys TwinHan VisionPlus, the Artec seemingly not have */
  270. { 0x00, 0x0c, KEY_CANCEL }, /* Cancel */
  271. { 0x00, 0x1c, KEY_EPG }, /* EPG */
  272. { 0x00, 0x00, KEY_TAB }, /* Tab */
  273. { 0x00, 0x48, KEY_INFO }, /* Preview */
  274. { 0x00, 0x04, KEY_LIST }, /* RecordList */
  275. { 0x00, 0x0f, KEY_TEXT }, /* Teletext */
  276. /* Key codes for the KWorld/ADSTech/JetWay remote. */
  277. { 0x86, 0x12, KEY_POWER },
  278. { 0x86, 0x0f, KEY_SELECT }, /* source */
  279. { 0x86, 0x0c, KEY_UNKNOWN }, /* scan */
  280. { 0x86, 0x0b, KEY_EPG },
  281. { 0x86, 0x10, KEY_MUTE },
  282. { 0x86, 0x01, KEY_1 },
  283. { 0x86, 0x02, KEY_2 },
  284. { 0x86, 0x03, KEY_3 },
  285. { 0x86, 0x04, KEY_4 },
  286. { 0x86, 0x05, KEY_5 },
  287. { 0x86, 0x06, KEY_6 },
  288. { 0x86, 0x07, KEY_7 },
  289. { 0x86, 0x08, KEY_8 },
  290. { 0x86, 0x09, KEY_9 },
  291. { 0x86, 0x0a, KEY_0 },
  292. { 0x86, 0x18, KEY_ZOOM },
  293. { 0x86, 0x1c, KEY_UNKNOWN }, /* preview */
  294. { 0x86, 0x13, KEY_UNKNOWN }, /* snap */
  295. { 0x86, 0x00, KEY_UNDO },
  296. { 0x86, 0x1d, KEY_RECORD },
  297. { 0x86, 0x0d, KEY_STOP },
  298. { 0x86, 0x0e, KEY_PAUSE },
  299. { 0x86, 0x16, KEY_PLAY },
  300. { 0x86, 0x11, KEY_BACK },
  301. { 0x86, 0x19, KEY_FORWARD },
  302. { 0x86, 0x14, KEY_UNKNOWN }, /* pip */
  303. { 0x86, 0x15, KEY_ESC },
  304. { 0x86, 0x1a, KEY_UP },
  305. { 0x86, 0x1e, KEY_DOWN },
  306. { 0x86, 0x1f, KEY_LEFT },
  307. { 0x86, 0x1b, KEY_RIGHT },
  308. /* Key codes for the DiBcom MOD3000 remote. */
  309. { 0x80, 0x00, KEY_MUTE },
  310. { 0x80, 0x01, KEY_TEXT },
  311. { 0x80, 0x02, KEY_HOME },
  312. { 0x80, 0x03, KEY_POWER },
  313. { 0x80, 0x04, KEY_RED },
  314. { 0x80, 0x05, KEY_GREEN },
  315. { 0x80, 0x06, KEY_YELLOW },
  316. { 0x80, 0x07, KEY_BLUE },
  317. { 0x80, 0x08, KEY_DVD },
  318. { 0x80, 0x09, KEY_AUDIO },
  319. { 0x80, 0x0a, KEY_MEDIA }, /* Pictures */
  320. { 0x80, 0x0b, KEY_VIDEO },
  321. { 0x80, 0x0c, KEY_BACK },
  322. { 0x80, 0x0d, KEY_UP },
  323. { 0x80, 0x0e, KEY_RADIO },
  324. { 0x80, 0x0f, KEY_EPG },
  325. { 0x80, 0x10, KEY_LEFT },
  326. { 0x80, 0x11, KEY_OK },
  327. { 0x80, 0x12, KEY_RIGHT },
  328. { 0x80, 0x13, KEY_UNKNOWN }, /* SAP */
  329. { 0x80, 0x14, KEY_TV },
  330. { 0x80, 0x15, KEY_DOWN },
  331. { 0x80, 0x16, KEY_MENU }, /* DVD Menu */
  332. { 0x80, 0x17, KEY_LAST },
  333. { 0x80, 0x18, KEY_RECORD },
  334. { 0x80, 0x19, KEY_STOP },
  335. { 0x80, 0x1a, KEY_PAUSE },
  336. { 0x80, 0x1b, KEY_PLAY },
  337. { 0x80, 0x1c, KEY_PREVIOUS },
  338. { 0x80, 0x1d, KEY_REWIND },
  339. { 0x80, 0x1e, KEY_FASTFORWARD },
  340. { 0x80, 0x1f, KEY_NEXT},
  341. { 0x80, 0x40, KEY_1 },
  342. { 0x80, 0x41, KEY_2 },
  343. { 0x80, 0x42, KEY_3 },
  344. { 0x80, 0x43, KEY_CHANNELUP },
  345. { 0x80, 0x44, KEY_4 },
  346. { 0x80, 0x45, KEY_5 },
  347. { 0x80, 0x46, KEY_6 },
  348. { 0x80, 0x47, KEY_CHANNELDOWN },
  349. { 0x80, 0x48, KEY_7 },
  350. { 0x80, 0x49, KEY_8 },
  351. { 0x80, 0x4a, KEY_9 },
  352. { 0x80, 0x4b, KEY_VOLUMEUP },
  353. { 0x80, 0x4c, KEY_CLEAR },
  354. { 0x80, 0x4d, KEY_0 },
  355. { 0x80, 0x4e, KEY_ENTER },
  356. { 0x80, 0x4f, KEY_VOLUMEDOWN },
  357. };
  358. EXPORT_SYMBOL(dibusb_rc_keys);
  359. int dibusb_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
  360. {
  361. u8 key[5],cmd = DIBUSB_REQ_POLL_REMOTE;
  362. dvb_usb_generic_rw(d,&cmd,1,key,5,0);
  363. dvb_usb_nec_rc_key_to_event(d,key,event,state);
  364. if (key[0] != 0)
  365. deb_info("key: %x %x %x %x %x\n",key[0],key[1],key[2],key[3],key[4]);
  366. return 0;
  367. }
  368. EXPORT_SYMBOL(dibusb_rc_query);