dibusb-common.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Common methods for dibusb-based-receivers.
  3. *
  4. * Copyright (C) 2004-5 Patrick Boettcher (patrick.boettcher@posteo.de)
  5. *
  6. * see Documentation/driver-api/media/drivers/dvb-usb.rst for more information
  7. */
  8. #include "dibusb.h"
  9. /* Max transfer size done by I2C transfer functions */
  10. #define MAX_XFER_SIZE 64
  11. static int debug;
  12. module_param(debug, int, 0644);
  13. MODULE_PARM_DESC(debug, "set debugging level (1=info (|-able))." DVB_USB_DEBUG_STATUS);
  14. MODULE_DESCRIPTION("Common methods for dibusb-based receivers");
  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_adap[0].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_adap[0].fe,
  37. index, pid, onoff);
  38. }
  39. return 0;
  40. }
  41. EXPORT_SYMBOL(dibusb_pid_filter);
  42. int dibusb_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
  43. {
  44. if (adap->priv != NULL) {
  45. struct dibusb_state *st = adap->priv;
  46. if (st->ops.pid_parse != NULL)
  47. if (st->ops.pid_parse(adap->fe_adap[0].fe, onoff) < 0)
  48. err("could not handle pid_parser");
  49. }
  50. return 0;
  51. }
  52. EXPORT_SYMBOL(dibusb_pid_filter_ctrl);
  53. int dibusb_power_ctrl(struct dvb_usb_device *d, int onoff)
  54. {
  55. u8 *b;
  56. int ret;
  57. b = kmalloc(3, GFP_KERNEL);
  58. if (!b)
  59. return -ENOMEM;
  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. kfree(b);
  65. msleep(10);
  66. return ret;
  67. }
  68. EXPORT_SYMBOL(dibusb_power_ctrl);
  69. int dibusb2_0_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
  70. {
  71. int ret;
  72. u8 *b;
  73. b = kmalloc(3, GFP_KERNEL);
  74. if (!b)
  75. return -ENOMEM;
  76. if ((ret = dibusb_streaming_ctrl(adap,onoff)) < 0)
  77. goto ret;
  78. if (onoff) {
  79. b[0] = DIBUSB_REQ_SET_STREAMING_MODE;
  80. b[1] = 0x00;
  81. ret = dvb_usb_generic_write(adap->dev, b, 2);
  82. if (ret < 0)
  83. goto ret;
  84. }
  85. b[0] = DIBUSB_REQ_SET_IOCTL;
  86. b[1] = onoff ? DIBUSB_IOCTL_CMD_ENABLE_STREAM : DIBUSB_IOCTL_CMD_DISABLE_STREAM;
  87. ret = dvb_usb_generic_write(adap->dev, b, 3);
  88. ret:
  89. kfree(b);
  90. return ret;
  91. }
  92. EXPORT_SYMBOL(dibusb2_0_streaming_ctrl);
  93. int dibusb2_0_power_ctrl(struct dvb_usb_device *d, int onoff)
  94. {
  95. u8 *b;
  96. int ret;
  97. if (!onoff)
  98. return 0;
  99. b = kmalloc(3, GFP_KERNEL);
  100. if (!b)
  101. return -ENOMEM;
  102. b[0] = DIBUSB_REQ_SET_IOCTL;
  103. b[1] = DIBUSB_IOCTL_CMD_POWER_MODE;
  104. b[2] = DIBUSB_IOCTL_POWER_WAKEUP;
  105. ret = dvb_usb_generic_write(d, b, 3);
  106. kfree(b);
  107. return ret;
  108. }
  109. EXPORT_SYMBOL(dibusb2_0_power_ctrl);
  110. static int dibusb_i2c_msg(struct dvb_usb_device *d, u8 addr,
  111. u8 *wbuf, u16 wlen, u8 *rbuf, u16 rlen)
  112. {
  113. u8 *sndbuf;
  114. int ret, wo, len;
  115. /* write only ? */
  116. wo = (rbuf == NULL || rlen == 0);
  117. len = 2 + wlen + (wo ? 0 : 2);
  118. sndbuf = kmalloc(MAX_XFER_SIZE, GFP_KERNEL);
  119. if (!sndbuf)
  120. return -ENOMEM;
  121. if (4 + wlen > MAX_XFER_SIZE) {
  122. warn("i2c wr: len=%d is too big!\n", wlen);
  123. ret = -EOPNOTSUPP;
  124. goto ret;
  125. }
  126. sndbuf[0] = wo ? DIBUSB_REQ_I2C_WRITE : DIBUSB_REQ_I2C_READ;
  127. sndbuf[1] = (addr << 1) | (wo ? 0 : 1);
  128. memcpy(&sndbuf[2], wbuf, wlen);
  129. if (!wo) {
  130. sndbuf[wlen + 2] = (rlen >> 8) & 0xff;
  131. sndbuf[wlen + 3] = rlen & 0xff;
  132. }
  133. ret = dvb_usb_generic_rw(d, sndbuf, len, rbuf, rlen, 0);
  134. ret:
  135. kfree(sndbuf);
  136. return ret;
  137. }
  138. /*
  139. * I2C master xfer function
  140. */
  141. static int dibusb_i2c_xfer(struct i2c_adapter *adap,struct i2c_msg msg[],int num)
  142. {
  143. struct dvb_usb_device *d = i2c_get_adapdata(adap);
  144. int i;
  145. if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
  146. return -EAGAIN;
  147. for (i = 0; i < num; i++) {
  148. /* write/read request */
  149. if (i+1 < num && (msg[i].flags & I2C_M_RD) == 0
  150. && (msg[i+1].flags & I2C_M_RD)) {
  151. if (dibusb_i2c_msg(d, msg[i].addr, msg[i].buf,msg[i].len,
  152. msg[i+1].buf,msg[i+1].len) < 0)
  153. break;
  154. i++;
  155. } else if ((msg[i].flags & I2C_M_RD) == 0) {
  156. if (dibusb_i2c_msg(d, msg[i].addr, msg[i].buf,msg[i].len,NULL,0) < 0)
  157. break;
  158. } else if (msg[i].addr != 0x50) {
  159. /* 0x50 is the address of the eeprom - we need to protect it
  160. * from dibusb's bad i2c implementation: reads without
  161. * writing the offset before are forbidden */
  162. if (dibusb_i2c_msg(d, msg[i].addr, NULL, 0, msg[i].buf, msg[i].len) < 0)
  163. break;
  164. }
  165. }
  166. mutex_unlock(&d->i2c_mutex);
  167. return i;
  168. }
  169. static u32 dibusb_i2c_func(struct i2c_adapter *adapter)
  170. {
  171. return I2C_FUNC_I2C;
  172. }
  173. struct i2c_algorithm dibusb_i2c_algo = {
  174. .master_xfer = dibusb_i2c_xfer,
  175. .functionality = dibusb_i2c_func,
  176. };
  177. EXPORT_SYMBOL(dibusb_i2c_algo);
  178. int dibusb_read_eeprom_byte(struct dvb_usb_device *d, u8 offs, u8 *val)
  179. {
  180. u8 *buf;
  181. int rc;
  182. buf = kzalloc(2, GFP_KERNEL);
  183. if (!buf)
  184. return -ENOMEM;
  185. buf[0] = offs;
  186. rc = dibusb_i2c_msg(d, 0x50, &buf[0], 1, &buf[1], 1);
  187. *val = buf[1];
  188. kfree(buf);
  189. return rc;
  190. }
  191. EXPORT_SYMBOL(dibusb_read_eeprom_byte);
  192. /*
  193. * common remote control stuff
  194. */
  195. struct rc_map_table rc_map_dibusb_table[] = {
  196. /* Key codes for the little Artec T1/Twinhan/HAMA/ remote. */
  197. { 0x0016, KEY_POWER },
  198. { 0x0010, KEY_MUTE },
  199. { 0x0003, KEY_1 },
  200. { 0x0001, KEY_2 },
  201. { 0x0006, KEY_3 },
  202. { 0x0009, KEY_4 },
  203. { 0x001d, KEY_5 },
  204. { 0x001f, KEY_6 },
  205. { 0x000d, KEY_7 },
  206. { 0x0019, KEY_8 },
  207. { 0x001b, KEY_9 },
  208. { 0x0015, KEY_0 },
  209. { 0x0005, KEY_CHANNELUP },
  210. { 0x0002, KEY_CHANNELDOWN },
  211. { 0x001e, KEY_VOLUMEUP },
  212. { 0x000a, KEY_VOLUMEDOWN },
  213. { 0x0011, KEY_RECORD },
  214. { 0x0017, KEY_FAVORITES }, /* Heart symbol - Channel list. */
  215. { 0x0014, KEY_PLAY },
  216. { 0x001a, KEY_STOP },
  217. { 0x0040, KEY_REWIND },
  218. { 0x0012, KEY_FASTFORWARD },
  219. { 0x000e, KEY_PREVIOUS }, /* Recall - Previous channel. */
  220. { 0x004c, KEY_PAUSE },
  221. { 0x004d, KEY_SCREEN }, /* Full screen mode. */
  222. { 0x0054, KEY_AUDIO }, /* MTS - Switch to secondary audio. */
  223. /* additional keys TwinHan VisionPlus, the Artec seemingly not have */
  224. { 0x000c, KEY_CANCEL }, /* Cancel */
  225. { 0x001c, KEY_EPG }, /* EPG */
  226. { 0x0000, KEY_TAB }, /* Tab */
  227. { 0x0048, KEY_INFO }, /* Preview */
  228. { 0x0004, KEY_LIST }, /* RecordList */
  229. { 0x000f, KEY_TEXT }, /* Teletext */
  230. /* Key codes for the KWorld/ADSTech/JetWay remote. */
  231. { 0x8612, KEY_POWER },
  232. { 0x860f, KEY_SELECT }, /* source */
  233. { 0x860c, KEY_UNKNOWN }, /* scan */
  234. { 0x860b, KEY_EPG },
  235. { 0x8610, KEY_MUTE },
  236. { 0x8601, KEY_1 },
  237. { 0x8602, KEY_2 },
  238. { 0x8603, KEY_3 },
  239. { 0x8604, KEY_4 },
  240. { 0x8605, KEY_5 },
  241. { 0x8606, KEY_6 },
  242. { 0x8607, KEY_7 },
  243. { 0x8608, KEY_8 },
  244. { 0x8609, KEY_9 },
  245. { 0x860a, KEY_0 },
  246. { 0x8618, KEY_ZOOM },
  247. { 0x861c, KEY_UNKNOWN }, /* preview */
  248. { 0x8613, KEY_UNKNOWN }, /* snap */
  249. { 0x8600, KEY_UNDO },
  250. { 0x861d, KEY_RECORD },
  251. { 0x860d, KEY_STOP },
  252. { 0x860e, KEY_PAUSE },
  253. { 0x8616, KEY_PLAY },
  254. { 0x8611, KEY_BACK },
  255. { 0x8619, KEY_FORWARD },
  256. { 0x8614, KEY_UNKNOWN }, /* pip */
  257. { 0x8615, KEY_ESC },
  258. { 0x861a, KEY_UP },
  259. { 0x861e, KEY_DOWN },
  260. { 0x861f, KEY_LEFT },
  261. { 0x861b, KEY_RIGHT },
  262. /* Key codes for the DiBcom MOD3000 remote. */
  263. { 0x8000, KEY_MUTE },
  264. { 0x8001, KEY_TEXT },
  265. { 0x8002, KEY_HOME },
  266. { 0x8003, KEY_POWER },
  267. { 0x8004, KEY_RED },
  268. { 0x8005, KEY_GREEN },
  269. { 0x8006, KEY_YELLOW },
  270. { 0x8007, KEY_BLUE },
  271. { 0x8008, KEY_DVD },
  272. { 0x8009, KEY_AUDIO },
  273. { 0x800a, KEY_IMAGES }, /* Pictures */
  274. { 0x800b, KEY_VIDEO },
  275. { 0x800c, KEY_BACK },
  276. { 0x800d, KEY_UP },
  277. { 0x800e, KEY_RADIO },
  278. { 0x800f, KEY_EPG },
  279. { 0x8010, KEY_LEFT },
  280. { 0x8011, KEY_OK },
  281. { 0x8012, KEY_RIGHT },
  282. { 0x8013, KEY_UNKNOWN }, /* SAP */
  283. { 0x8014, KEY_TV },
  284. { 0x8015, KEY_DOWN },
  285. { 0x8016, KEY_MENU }, /* DVD Menu */
  286. { 0x8017, KEY_LAST },
  287. { 0x8018, KEY_RECORD },
  288. { 0x8019, KEY_STOP },
  289. { 0x801a, KEY_PAUSE },
  290. { 0x801b, KEY_PLAY },
  291. { 0x801c, KEY_PREVIOUS },
  292. { 0x801d, KEY_REWIND },
  293. { 0x801e, KEY_FASTFORWARD },
  294. { 0x801f, KEY_NEXT},
  295. { 0x8040, KEY_1 },
  296. { 0x8041, KEY_2 },
  297. { 0x8042, KEY_3 },
  298. { 0x8043, KEY_CHANNELUP },
  299. { 0x8044, KEY_4 },
  300. { 0x8045, KEY_5 },
  301. { 0x8046, KEY_6 },
  302. { 0x8047, KEY_CHANNELDOWN },
  303. { 0x8048, KEY_7 },
  304. { 0x8049, KEY_8 },
  305. { 0x804a, KEY_9 },
  306. { 0x804b, KEY_VOLUMEUP },
  307. { 0x804c, KEY_CLEAR },
  308. { 0x804d, KEY_0 },
  309. { 0x804e, KEY_ENTER },
  310. { 0x804f, KEY_VOLUMEDOWN },
  311. };
  312. EXPORT_SYMBOL(rc_map_dibusb_table);
  313. int dibusb_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
  314. {
  315. u8 *buf;
  316. int ret;
  317. buf = kmalloc(5, GFP_KERNEL);
  318. if (!buf)
  319. return -ENOMEM;
  320. buf[0] = DIBUSB_REQ_POLL_REMOTE;
  321. ret = dvb_usb_generic_rw(d, buf, 1, buf, 5, 0);
  322. if (ret < 0)
  323. goto ret;
  324. dvb_usb_nec_rc_key_to_event(d, buf, event, state);
  325. if (buf[0] != 0)
  326. deb_info("key: %*ph\n", 5, buf);
  327. ret:
  328. kfree(buf);
  329. return ret;
  330. }
  331. EXPORT_SYMBOL(dibusb_rc_query);