dibusb-common.c 8.8 KB

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