blob: bcacb0f220282d9c3537e8847b178d7f0ec71727 [file] [log] [blame]
Johannes Stezenbach776338e2005-06-23 22:02:35 -07001/* Common methods for dibusb-based-receivers.
2 *
Patrick Boettcher99e44da2016-01-24 12:56:58 -02003 * Copyright (C) 2004-5 Patrick Boettcher (patrick.boettcher@posteo.de)
Johannes Stezenbach776338e2005-06-23 22:02:35 -07004 *
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 */
Mauro Carvalho Chehab028c70f2013-03-21 16:29:36 -030011
Johannes Stezenbach776338e2005-06-23 22:02:35 -070012#include "dibusb.h"
13
Mauro Carvalho Chehab1d7fa352013-11-02 07:23:49 -030014/* Max transfer size done by I2C transfer functions */
15#define MAX_XFER_SIZE 64
16
Johannes Stezenbach776338e2005-06-23 22:02:35 -070017static int debug;
18module_param(debug, int, 0644);
19MODULE_PARM_DESC(debug, "set debugging level (1=info (|-able))." DVB_USB_DEBUG_STATUS);
20MODULE_LICENSE("GPL");
21
22#define deb_info(args...) dprintk(debug,0x01,args)
23
24/* common stuff used by the different dibusb modules */
Patrick Boettcher4d43e132006-09-30 06:53:48 -030025int dibusb_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
Johannes Stezenbach776338e2005-06-23 22:02:35 -070026{
Patrick Boettcher4d43e132006-09-30 06:53:48 -030027 if (adap->priv != NULL) {
28 struct dibusb_state *st = adap->priv;
Patrick Boettcher363bbf42005-12-01 00:51:51 -080029 if (st->ops.fifo_ctrl != NULL)
Michael Krufkyb6bd5de2011-09-16 09:32:00 -030030 if (st->ops.fifo_ctrl(adap->fe_adap[0].fe, onoff)) {
Johannes Stezenbach776338e2005-06-23 22:02:35 -070031 err("error while controlling the fifo of the demod.");
32 return -ENODEV;
33 }
34 }
35 return 0;
36}
37EXPORT_SYMBOL(dibusb_streaming_ctrl);
38
Patrick Boettcher4d43e132006-09-30 06:53:48 -030039int dibusb_pid_filter(struct dvb_usb_adapter *adap, int index, u16 pid, int onoff)
Johannes Stezenbach776338e2005-06-23 22:02:35 -070040{
Patrick Boettcher4d43e132006-09-30 06:53:48 -030041 if (adap->priv != NULL) {
42 struct dibusb_state *st = adap->priv;
Patrick Boettcher363bbf42005-12-01 00:51:51 -080043 if (st->ops.pid_ctrl != NULL)
Michael Krufkyb6bd5de2011-09-16 09:32:00 -030044 st->ops.pid_ctrl(adap->fe_adap[0].fe,
45 index, pid, onoff);
Johannes Stezenbach776338e2005-06-23 22:02:35 -070046 }
47 return 0;
48}
49EXPORT_SYMBOL(dibusb_pid_filter);
50
Patrick Boettcher4d43e132006-09-30 06:53:48 -030051int dibusb_pid_filter_ctrl(struct dvb_usb_adapter *adap, int onoff)
Johannes Stezenbach776338e2005-06-23 22:02:35 -070052{
Patrick Boettcher4d43e132006-09-30 06:53:48 -030053 if (adap->priv != NULL) {
54 struct dibusb_state *st = adap->priv;
Patrick Boettcher363bbf42005-12-01 00:51:51 -080055 if (st->ops.pid_parse != NULL)
Michael Krufkyb6bd5de2011-09-16 09:32:00 -030056 if (st->ops.pid_parse(adap->fe_adap[0].fe, onoff) < 0)
Johannes Stezenbach776338e2005-06-23 22:02:35 -070057 err("could not handle pid_parser");
58 }
59 return 0;
60}
61EXPORT_SYMBOL(dibusb_pid_filter_ctrl);
62
63int dibusb_power_ctrl(struct dvb_usb_device *d, int onoff)
64{
Mauro Carvalho Chehab426398b2016-10-07 07:34:36 -030065 u8 *b;
Johannes Stezenbach776338e2005-06-23 22:02:35 -070066 int ret;
Mauro Carvalho Chehab426398b2016-10-07 07:34:36 -030067
68 b = kmalloc(3, GFP_KERNEL);
69 if (!b)
70 return -ENOMEM;
71
Johannes Stezenbach776338e2005-06-23 22:02:35 -070072 b[0] = DIBUSB_REQ_SET_IOCTL;
73 b[1] = DIBUSB_IOCTL_CMD_POWER_MODE;
74 b[2] = onoff ? DIBUSB_IOCTL_POWER_WAKEUP : DIBUSB_IOCTL_POWER_SLEEP;
Mauro Carvalho Chehab426398b2016-10-07 07:34:36 -030075
76 ret = dvb_usb_generic_write(d, b, 3);
77
78 kfree(b);
79
Johannes Stezenbach776338e2005-06-23 22:02:35 -070080 msleep(10);
Mauro Carvalho Chehab426398b2016-10-07 07:34:36 -030081
Johannes Stezenbach776338e2005-06-23 22:02:35 -070082 return ret;
83}
84EXPORT_SYMBOL(dibusb_power_ctrl);
85
Patrick Boettcher4d43e132006-09-30 06:53:48 -030086int dibusb2_0_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
Johannes Stezenbach776338e2005-06-23 22:02:35 -070087{
Patrick Boettcher8126fdb2005-08-27 19:30:30 +020088 int ret;
Mauro Carvalho Chehab426398b2016-10-07 07:34:36 -030089 u8 *b;
90
91 b = kmalloc(3, GFP_KERNEL);
92 if (!b)
93 return -ENOMEM;
Patrick Boettcher8126fdb2005-08-27 19:30:30 +020094
Patrick Boettcher4d43e132006-09-30 06:53:48 -030095 if ((ret = dibusb_streaming_ctrl(adap,onoff)) < 0)
Mauro Carvalho Chehab426398b2016-10-07 07:34:36 -030096 goto ret;
Patrick Boettcher8126fdb2005-08-27 19:30:30 +020097
98 if (onoff) {
99 b[0] = DIBUSB_REQ_SET_STREAMING_MODE;
100 b[1] = 0x00;
Mauro Carvalho Chehab426398b2016-10-07 07:34:36 -0300101 ret = dvb_usb_generic_write(adap->dev, b, 2);
102 if (ret < 0)
103 goto ret;
Patrick Boettcher8126fdb2005-08-27 19:30:30 +0200104 }
105
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700106 b[0] = DIBUSB_REQ_SET_IOCTL;
107 b[1] = onoff ? DIBUSB_IOCTL_CMD_ENABLE_STREAM : DIBUSB_IOCTL_CMD_DISABLE_STREAM;
Mauro Carvalho Chehab426398b2016-10-07 07:34:36 -0300108 ret = dvb_usb_generic_write(adap->dev, b, 3);
109
110ret:
111 kfree(b);
112 return ret;
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700113}
114EXPORT_SYMBOL(dibusb2_0_streaming_ctrl);
115
116int dibusb2_0_power_ctrl(struct dvb_usb_device *d, int onoff)
117{
Mauro Carvalho Chehab426398b2016-10-07 07:34:36 -0300118 u8 *b;
119 int ret;
120
121 if (!onoff)
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700122 return 0;
Mauro Carvalho Chehab426398b2016-10-07 07:34:36 -0300123
124 b = kmalloc(3, GFP_KERNEL);
125 if (!b)
126 return -ENOMEM;
127
128 b[0] = DIBUSB_REQ_SET_IOCTL;
129 b[1] = DIBUSB_IOCTL_CMD_POWER_MODE;
130 b[2] = DIBUSB_IOCTL_POWER_WAKEUP;
131
132 ret = dvb_usb_generic_write(d, b, 3);
133
134 kfree(b);
135
136 return ret;
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700137}
138EXPORT_SYMBOL(dibusb2_0_power_ctrl);
139
140static int dibusb_i2c_msg(struct dvb_usb_device *d, u8 addr,
141 u8 *wbuf, u16 wlen, u8 *rbuf, u16 rlen)
142{
Mauro Carvalho Chehab426398b2016-10-07 07:34:36 -0300143 u8 *sndbuf;
144 int ret, wo, len;
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700145
Mauro Carvalho Chehab426398b2016-10-07 07:34:36 -0300146 /* write only ? */
147 wo = (rbuf == NULL || rlen == 0);
148
149 len = 2 + wlen + (wo ? 0 : 2);
150
151 sndbuf = kmalloc(MAX_XFER_SIZE, GFP_KERNEL);
152 if (!sndbuf)
153 return -ENOMEM;
154
155 if (4 + wlen > MAX_XFER_SIZE) {
Mauro Carvalho Chehab1d7fa352013-11-02 07:23:49 -0300156 warn("i2c wr: len=%d is too big!\n", wlen);
Mauro Carvalho Chehab426398b2016-10-07 07:34:36 -0300157 ret = -EOPNOTSUPP;
158 goto ret;
Mauro Carvalho Chehab1d7fa352013-11-02 07:23:49 -0300159 }
160
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700161 sndbuf[0] = wo ? DIBUSB_REQ_I2C_WRITE : DIBUSB_REQ_I2C_READ;
162 sndbuf[1] = (addr << 1) | (wo ? 0 : 1);
163
Mauro Carvalho Chehab426398b2016-10-07 07:34:36 -0300164 memcpy(&sndbuf[2], wbuf, wlen);
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700165
166 if (!wo) {
Mauro Carvalho Chehab426398b2016-10-07 07:34:36 -0300167 sndbuf[wlen + 2] = (rlen >> 8) & 0xff;
168 sndbuf[wlen + 3] = rlen & 0xff;
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700169 }
170
Mauro Carvalho Chehab426398b2016-10-07 07:34:36 -0300171 ret = dvb_usb_generic_rw(d, sndbuf, len, rbuf, rlen, 0);
172
173ret:
174 kfree(sndbuf);
175 return ret;
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700176}
177
178/*
179 * I2C master xfer function
180 */
181static int dibusb_i2c_xfer(struct i2c_adapter *adap,struct i2c_msg msg[],int num)
182{
183 struct dvb_usb_device *d = i2c_get_adapdata(adap);
184 int i;
185
Ingo Molnar3593cab2006-02-07 06:49:14 -0200186 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700187 return -EAGAIN;
188
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700189 for (i = 0; i < num; i++) {
190 /* write/read request */
matthieu castet81dae672009-05-20 05:42:33 -0300191 if (i+1 < num && (msg[i].flags & I2C_M_RD) == 0
192 && (msg[i+1].flags & I2C_M_RD)) {
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700193 if (dibusb_i2c_msg(d, msg[i].addr, msg[i].buf,msg[i].len,
194 msg[i+1].buf,msg[i+1].len) < 0)
195 break;
196 i++;
matthieu castet81dae672009-05-20 05:42:33 -0300197 } else if ((msg[i].flags & I2C_M_RD) == 0) {
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700198 if (dibusb_i2c_msg(d, msg[i].addr, msg[i].buf,msg[i].len,NULL,0) < 0)
199 break;
Patrick Boettcher38ffcd12009-11-30 14:14:36 -0300200 } else if (msg[i].addr != 0x50) {
201 /* 0x50 is the address of the eeprom - we need to protect it
202 * from dibusb's bad i2c implementation: reads without
203 * writing the offset before are forbidden */
204 if (dibusb_i2c_msg(d, msg[i].addr, NULL, 0, msg[i].buf, msg[i].len) < 0)
205 break;
206 }
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700207 }
208
Ingo Molnar3593cab2006-02-07 06:49:14 -0200209 mutex_unlock(&d->i2c_mutex);
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700210 return i;
211}
212
213static u32 dibusb_i2c_func(struct i2c_adapter *adapter)
214{
215 return I2C_FUNC_I2C;
216}
217
218struct i2c_algorithm dibusb_i2c_algo = {
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700219 .master_xfer = dibusb_i2c_xfer,
220 .functionality = dibusb_i2c_func,
221};
222EXPORT_SYMBOL(dibusb_i2c_algo);
223
224int dibusb_read_eeprom_byte(struct dvb_usb_device *d, u8 offs, u8 *val)
225{
Laurent Caumont6d333772017-11-11 12:44:46 -0500226 u8 *buf;
227 int rc;
228
229 buf = kmalloc(2, GFP_KERNEL);
230 if (!buf)
231 return -ENOMEM;
232
233 buf[0] = offs;
234
235 rc = dibusb_i2c_msg(d, 0x50, &buf[0], 1, &buf[1], 1);
236 *val = buf[1];
237 kfree(buf);
238
239 return rc;
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700240}
241EXPORT_SYMBOL(dibusb_read_eeprom_byte);
242
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700243/*
244 * common remote control stuff
245 */
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300246struct rc_map_table rc_map_dibusb_table[] = {
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700247 /* Key codes for the little Artec T1/Twinhan/HAMA/ remote. */
Mauro Carvalho Chehab2e365882009-08-29 15:19:31 -0300248 { 0x0016, KEY_POWER },
249 { 0x0010, KEY_MUTE },
250 { 0x0003, KEY_1 },
251 { 0x0001, KEY_2 },
252 { 0x0006, KEY_3 },
253 { 0x0009, KEY_4 },
254 { 0x001d, KEY_5 },
255 { 0x001f, KEY_6 },
256 { 0x000d, KEY_7 },
257 { 0x0019, KEY_8 },
258 { 0x001b, KEY_9 },
259 { 0x0015, KEY_0 },
260 { 0x0005, KEY_CHANNELUP },
261 { 0x0002, KEY_CHANNELDOWN },
262 { 0x001e, KEY_VOLUMEUP },
263 { 0x000a, KEY_VOLUMEDOWN },
264 { 0x0011, KEY_RECORD },
265 { 0x0017, KEY_FAVORITES }, /* Heart symbol - Channel list. */
266 { 0x0014, KEY_PLAY },
267 { 0x001a, KEY_STOP },
268 { 0x0040, KEY_REWIND },
269 { 0x0012, KEY_FASTFORWARD },
270 { 0x000e, KEY_PREVIOUS }, /* Recall - Previous channel. */
271 { 0x004c, KEY_PAUSE },
272 { 0x004d, KEY_SCREEN }, /* Full screen mode. */
273 { 0x0054, KEY_AUDIO }, /* MTS - Switch to secondary audio. */
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700274 /* additional keys TwinHan VisionPlus, the Artec seemingly not have */
Mauro Carvalho Chehab2e365882009-08-29 15:19:31 -0300275 { 0x000c, KEY_CANCEL }, /* Cancel */
276 { 0x001c, KEY_EPG }, /* EPG */
277 { 0x0000, KEY_TAB }, /* Tab */
278 { 0x0048, KEY_INFO }, /* Preview */
279 { 0x0004, KEY_LIST }, /* RecordList */
280 { 0x000f, KEY_TEXT }, /* Teletext */
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700281 /* Key codes for the KWorld/ADSTech/JetWay remote. */
Mauro Carvalho Chehab2e365882009-08-29 15:19:31 -0300282 { 0x8612, KEY_POWER },
283 { 0x860f, KEY_SELECT }, /* source */
284 { 0x860c, KEY_UNKNOWN }, /* scan */
285 { 0x860b, KEY_EPG },
286 { 0x8610, KEY_MUTE },
287 { 0x8601, KEY_1 },
288 { 0x8602, KEY_2 },
289 { 0x8603, KEY_3 },
290 { 0x8604, KEY_4 },
291 { 0x8605, KEY_5 },
292 { 0x8606, KEY_6 },
293 { 0x8607, KEY_7 },
294 { 0x8608, KEY_8 },
295 { 0x8609, KEY_9 },
296 { 0x860a, KEY_0 },
297 { 0x8618, KEY_ZOOM },
298 { 0x861c, KEY_UNKNOWN }, /* preview */
299 { 0x8613, KEY_UNKNOWN }, /* snap */
300 { 0x8600, KEY_UNDO },
301 { 0x861d, KEY_RECORD },
302 { 0x860d, KEY_STOP },
303 { 0x860e, KEY_PAUSE },
304 { 0x8616, KEY_PLAY },
305 { 0x8611, KEY_BACK },
306 { 0x8619, KEY_FORWARD },
307 { 0x8614, KEY_UNKNOWN }, /* pip */
308 { 0x8615, KEY_ESC },
309 { 0x861a, KEY_UP },
310 { 0x861e, KEY_DOWN },
311 { 0x861f, KEY_LEFT },
312 { 0x861b, KEY_RIGHT },
Sergei Hallera841e1f2006-05-14 10:47:21 -0300313
314 /* Key codes for the DiBcom MOD3000 remote. */
Mauro Carvalho Chehab2e365882009-08-29 15:19:31 -0300315 { 0x8000, KEY_MUTE },
316 { 0x8001, KEY_TEXT },
317 { 0x8002, KEY_HOME },
318 { 0x8003, KEY_POWER },
Sergei Hallera841e1f2006-05-14 10:47:21 -0300319
Mauro Carvalho Chehab2e365882009-08-29 15:19:31 -0300320 { 0x8004, KEY_RED },
321 { 0x8005, KEY_GREEN },
322 { 0x8006, KEY_YELLOW },
323 { 0x8007, KEY_BLUE },
Sergei Hallera841e1f2006-05-14 10:47:21 -0300324
Mauro Carvalho Chehab2e365882009-08-29 15:19:31 -0300325 { 0x8008, KEY_DVD },
326 { 0x8009, KEY_AUDIO },
Jarod Wilson56c08932011-04-05 18:42:30 -0300327 { 0x800a, KEY_IMAGES }, /* Pictures */
Mauro Carvalho Chehab2e365882009-08-29 15:19:31 -0300328 { 0x800b, KEY_VIDEO },
Sergei Hallera841e1f2006-05-14 10:47:21 -0300329
Mauro Carvalho Chehab2e365882009-08-29 15:19:31 -0300330 { 0x800c, KEY_BACK },
331 { 0x800d, KEY_UP },
332 { 0x800e, KEY_RADIO },
333 { 0x800f, KEY_EPG },
Sergei Hallera841e1f2006-05-14 10:47:21 -0300334
Mauro Carvalho Chehab2e365882009-08-29 15:19:31 -0300335 { 0x8010, KEY_LEFT },
336 { 0x8011, KEY_OK },
337 { 0x8012, KEY_RIGHT },
338 { 0x8013, KEY_UNKNOWN }, /* SAP */
Sergei Hallera841e1f2006-05-14 10:47:21 -0300339
Mauro Carvalho Chehab2e365882009-08-29 15:19:31 -0300340 { 0x8014, KEY_TV },
341 { 0x8015, KEY_DOWN },
342 { 0x8016, KEY_MENU }, /* DVD Menu */
343 { 0x8017, KEY_LAST },
Sergei Hallera841e1f2006-05-14 10:47:21 -0300344
Mauro Carvalho Chehab2e365882009-08-29 15:19:31 -0300345 { 0x8018, KEY_RECORD },
346 { 0x8019, KEY_STOP },
347 { 0x801a, KEY_PAUSE },
348 { 0x801b, KEY_PLAY },
Sergei Hallera841e1f2006-05-14 10:47:21 -0300349
Mauro Carvalho Chehab2e365882009-08-29 15:19:31 -0300350 { 0x801c, KEY_PREVIOUS },
351 { 0x801d, KEY_REWIND },
352 { 0x801e, KEY_FASTFORWARD },
353 { 0x801f, KEY_NEXT},
Sergei Hallera841e1f2006-05-14 10:47:21 -0300354
Mauro Carvalho Chehab2e365882009-08-29 15:19:31 -0300355 { 0x8040, KEY_1 },
356 { 0x8041, KEY_2 },
357 { 0x8042, KEY_3 },
358 { 0x8043, KEY_CHANNELUP },
Sergei Hallera841e1f2006-05-14 10:47:21 -0300359
Mauro Carvalho Chehab2e365882009-08-29 15:19:31 -0300360 { 0x8044, KEY_4 },
361 { 0x8045, KEY_5 },
362 { 0x8046, KEY_6 },
363 { 0x8047, KEY_CHANNELDOWN },
Sergei Hallera841e1f2006-05-14 10:47:21 -0300364
Mauro Carvalho Chehab2e365882009-08-29 15:19:31 -0300365 { 0x8048, KEY_7 },
366 { 0x8049, KEY_8 },
367 { 0x804a, KEY_9 },
368 { 0x804b, KEY_VOLUMEUP },
Sergei Hallera841e1f2006-05-14 10:47:21 -0300369
Mauro Carvalho Chehab2e365882009-08-29 15:19:31 -0300370 { 0x804c, KEY_CLEAR },
371 { 0x804d, KEY_0 },
372 { 0x804e, KEY_ENTER },
373 { 0x804f, KEY_VOLUMEDOWN },
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700374};
Mauro Carvalho Chehab2f4f58d2010-11-17 15:46:09 -0300375EXPORT_SYMBOL(rc_map_dibusb_table);
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700376
377int dibusb_rc_query(struct dvb_usb_device *d, u32 *event, int *state)
378{
Mauro Carvalho Chehab426398b2016-10-07 07:34:36 -0300379 u8 *buf;
Mauro Carvalho Chehabff1c1232016-10-07 13:40:48 -0300380 int ret;
Mauro Carvalho Chehab426398b2016-10-07 07:34:36 -0300381
382 buf = kmalloc(5, GFP_KERNEL);
383 if (!buf)
384 return -ENOMEM;
385
386 buf[0] = DIBUSB_REQ_POLL_REMOTE;
387
Mauro Carvalho Chehabff1c1232016-10-07 13:40:48 -0300388 ret = dvb_usb_generic_rw(d, buf, 1, buf, 5, 0);
389 if (ret < 0)
390 goto ret;
Mauro Carvalho Chehab426398b2016-10-07 07:34:36 -0300391
392 dvb_usb_nec_rc_key_to_event(d, buf, event, state);
393
394 if (buf[0] != 0)
395 deb_info("key: %*ph\n", 5, buf);
396
Wei Yongjun1f5ecaf2016-10-29 23:36:24 -0200397ret:
Mauro Carvalho Chehab426398b2016-10-07 07:34:36 -0300398 kfree(buf);
399
Mauro Carvalho Chehabff1c1232016-10-07 13:40:48 -0300400 return ret;
Johannes Stezenbach776338e2005-06-23 22:02:35 -0700401}
402EXPORT_SYMBOL(dibusb_rc_query);