blob: 4a5ea74c91d45c8dc066ca8b8edb99dc30be033a [file] [log] [blame]
Patrick Boettcherb7f54912006-09-19 12:51:37 -03001/* Linux driver for devices based on the DiBcom DiB0700 USB bridge
2 *
3 * This program is free software; you can redistribute it and/or modify it
4 * under the terms of the GNU General Public License as published by the Free
5 * Software Foundation, version 2.
6 *
7 * Copyright (C) 2005-6 DiBcom, SA
8 */
9#include "dib0700.h"
10
11/* debug */
12int dvb_usb_dib0700_debug;
13module_param_named(debug,dvb_usb_dib0700_debug, int, 0644);
14MODULE_PARM_DESC(debug, "set debugging level (1=info,2=fw,4=fwdata,8=data (or-able))." DVB_USB_DEBUG_STATUS);
15
Olivier Grenieacc5c9e2009-12-07 08:22:53 -030016static int nb_packet_buffer_size = 21;
17module_param(nb_packet_buffer_size, int, 0644);
18MODULE_PARM_DESC(nb_packet_buffer_size,
19 "Set the dib0700 driver data buffer size. This parameter "
20 "corresponds to the number of TS packets. The actual size of "
21 "the data buffer corresponds to this parameter "
22 "multiplied by 188 (default: 21)");
23
Janne Grunau78e92002008-04-09 19:13:13 -030024DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
25
Devin Heitmueller99afb982008-11-15 07:13:07 -030026
27int dib0700_get_version(struct dvb_usb_device *d, u32 *hwversion,
28 u32 *romversion, u32 *ramversion, u32 *fwtype)
29{
Olivier Grenieffa58992011-03-24 09:32:26 -030030 struct dib0700_state *st = d->priv;
31 int ret;
32
Olivier Greniebff469f2011-08-01 12:45:58 -030033 if (mutex_lock_interruptible(&d->usb_mutex) < 0) {
Olivier Grenie680417b2011-08-04 13:10:03 -030034 err("could not acquire lock");
Santosh Nayak26a11eb2012-03-19 07:27:37 -030035 return -EINTR;
Olivier Greniebff469f2011-08-01 12:45:58 -030036 }
37
Olivier Grenieffa58992011-03-24 09:32:26 -030038 ret = usb_control_msg(d->udev, usb_rcvctrlpipe(d->udev, 0),
Devin Heitmueller99afb982008-11-15 07:13:07 -030039 REQUEST_GET_VERSION,
40 USB_TYPE_VENDOR | USB_DIR_IN, 0, 0,
Olivier Grenieffa58992011-03-24 09:32:26 -030041 st->buf, 16, USB_CTRL_GET_TIMEOUT);
Olivier Grenieacc5c9e2009-12-07 08:22:53 -030042 if (hwversion != NULL)
Olivier Grenieffa58992011-03-24 09:32:26 -030043 *hwversion = (st->buf[0] << 24) | (st->buf[1] << 16) |
44 (st->buf[2] << 8) | st->buf[3];
Olivier Grenieacc5c9e2009-12-07 08:22:53 -030045 if (romversion != NULL)
Olivier Grenieffa58992011-03-24 09:32:26 -030046 *romversion = (st->buf[4] << 24) | (st->buf[5] << 16) |
47 (st->buf[6] << 8) | st->buf[7];
Olivier Grenieacc5c9e2009-12-07 08:22:53 -030048 if (ramversion != NULL)
Olivier Grenieffa58992011-03-24 09:32:26 -030049 *ramversion = (st->buf[8] << 24) | (st->buf[9] << 16) |
50 (st->buf[10] << 8) | st->buf[11];
Olivier Grenieacc5c9e2009-12-07 08:22:53 -030051 if (fwtype != NULL)
Olivier Grenieffa58992011-03-24 09:32:26 -030052 *fwtype = (st->buf[12] << 24) | (st->buf[13] << 16) |
53 (st->buf[14] << 8) | st->buf[15];
Olivier Greniebff469f2011-08-01 12:45:58 -030054 mutex_unlock(&d->usb_mutex);
Devin Heitmueller99afb982008-11-15 07:13:07 -030055 return ret;
56}
57
Patrick Boettcherb7f54912006-09-19 12:51:37 -030058/* expecting rx buffer: request data[0] data[1] ... data[2] */
59static int dib0700_ctrl_wr(struct dvb_usb_device *d, u8 *tx, u8 txlen)
60{
61 int status;
62
63 deb_data(">>> ");
Daniel Mack230b27c2010-07-01 01:19:31 -030064 debug_dump(tx, txlen, deb_data);
Patrick Boettcherb7f54912006-09-19 12:51:37 -030065
66 status = usb_control_msg(d->udev, usb_sndctrlpipe(d->udev,0),
67 tx[0], USB_TYPE_VENDOR | USB_DIR_OUT, 0, 0, tx, txlen,
68 USB_CTRL_GET_TIMEOUT);
69
70 if (status != txlen)
Patrick Boettcher6958eff2006-09-19 12:51:40 -030071 deb_data("ep 0 write error (status = %d, len: %d)\n",status,txlen);
Patrick Boettcherb7f54912006-09-19 12:51:37 -030072
73 return status < 0 ? status : 0;
74}
75
76/* expecting tx buffer: request data[0] ... data[n] (n <= 4) */
Olivier DANET54d75eb2007-07-25 14:42:54 -030077int dib0700_ctrl_rd(struct dvb_usb_device *d, u8 *tx, u8 txlen, u8 *rx, u8 rxlen)
Patrick Boettcherb7f54912006-09-19 12:51:37 -030078{
79 u16 index, value;
80 int status;
81
82 if (txlen < 2) {
83 err("tx buffer length is smaller than 2. Makes no sense.");
84 return -EINVAL;
85 }
86 if (txlen > 4) {
87 err("tx buffer length is larger than 4. Not supported.");
88 return -EINVAL;
89 }
90
91 deb_data(">>> ");
92 debug_dump(tx,txlen,deb_data);
93
94 value = ((txlen - 2) << 8) | tx[1];
95 index = 0;
96 if (txlen > 2)
97 index |= (tx[2] << 8);
98 if (txlen > 3)
99 index |= tx[3];
100
Patrick Boettcherb7f54912006-09-19 12:51:37 -0300101 status = usb_control_msg(d->udev, usb_rcvctrlpipe(d->udev,0), tx[0],
102 USB_TYPE_VENDOR | USB_DIR_IN, value, index, rx, rxlen,
103 USB_CTRL_GET_TIMEOUT);
104
105 if (status < 0)
Patrick Boettcher6958eff2006-09-19 12:51:40 -0300106 deb_info("ep 0 read error (status = %d)\n",status);
Patrick Boettcherb7f54912006-09-19 12:51:37 -0300107
108 deb_data("<<< ");
Daniel Mack230b27c2010-07-01 01:19:31 -0300109 debug_dump(rx, rxlen, deb_data);
Patrick Boettcherb7f54912006-09-19 12:51:37 -0300110
111 return status; /* length in case of success */
112}
113
114int dib0700_set_gpio(struct dvb_usb_device *d, enum dib07x0_gpios gpio, u8 gpio_dir, u8 gpio_val)
115{
Olivier Grenieffa58992011-03-24 09:32:26 -0300116 struct dib0700_state *st = d->priv;
Olivier Greniebff469f2011-08-01 12:45:58 -0300117 int ret;
118
119 if (mutex_lock_interruptible(&d->usb_mutex) < 0) {
Olivier Grenie680417b2011-08-04 13:10:03 -0300120 err("could not acquire lock");
Santosh Nayak26a11eb2012-03-19 07:27:37 -0300121 return -EINTR;
Olivier Greniebff469f2011-08-01 12:45:58 -0300122 }
Olivier Grenieffa58992011-03-24 09:32:26 -0300123
124 st->buf[0] = REQUEST_SET_GPIO;
125 st->buf[1] = gpio;
126 st->buf[2] = ((gpio_dir & 0x01) << 7) | ((gpio_val & 0x01) << 6);
127
128 ret = dib0700_ctrl_wr(d, st->buf, 3);
129
Olivier Greniebff469f2011-08-01 12:45:58 -0300130 mutex_unlock(&d->usb_mutex);
Olivier Grenieffa58992011-03-24 09:32:26 -0300131 return ret;
Patrick Boettcherb7f54912006-09-19 12:51:37 -0300132}
133
Olivier Grenieacc5c9e2009-12-07 08:22:53 -0300134static int dib0700_set_usb_xfer_len(struct dvb_usb_device *d, u16 nb_ts_packets)
135{
Daniel Mack230b27c2010-07-01 01:19:31 -0300136 struct dib0700_state *st = d->priv;
Daniel Mack230b27c2010-07-01 01:19:31 -0300137 int ret;
Olivier Grenieacc5c9e2009-12-07 08:22:53 -0300138
Daniel Mack230b27c2010-07-01 01:19:31 -0300139 if (st->fw_version >= 0x10201) {
Olivier Greniebff469f2011-08-01 12:45:58 -0300140 if (mutex_lock_interruptible(&d->usb_mutex) < 0) {
Olivier Grenie680417b2011-08-04 13:10:03 -0300141 err("could not acquire lock");
Santosh Nayak26a11eb2012-03-19 07:27:37 -0300142 return -EINTR;
Olivier Greniebff469f2011-08-01 12:45:58 -0300143 }
144
Olivier Grenieffa58992011-03-24 09:32:26 -0300145 st->buf[0] = REQUEST_SET_USB_XFER_LEN;
146 st->buf[1] = (nb_ts_packets >> 8) & 0xff;
147 st->buf[2] = nb_ts_packets & 0xff;
Olivier Grenieacc5c9e2009-12-07 08:22:53 -0300148
Daniel Mack230b27c2010-07-01 01:19:31 -0300149 deb_info("set the USB xfer len to %i Ts packet\n", nb_ts_packets);
Olivier Grenieacc5c9e2009-12-07 08:22:53 -0300150
Olivier Grenieffa58992011-03-24 09:32:26 -0300151 ret = dib0700_ctrl_wr(d, st->buf, 3);
Olivier Greniebff469f2011-08-01 12:45:58 -0300152 mutex_unlock(&d->usb_mutex);
Daniel Mack230b27c2010-07-01 01:19:31 -0300153 } else {
154 deb_info("this firmware does not allow to change the USB xfer len\n");
155 ret = -EIO;
156 }
157
158 return ret;
Olivier Grenieacc5c9e2009-12-07 08:22:53 -0300159}
160
Patrick Boettcherb7f54912006-09-19 12:51:37 -0300161/*
Devin Heitmuellerbdc203e2008-09-06 13:45:27 -0300162 * I2C master xfer function (supported in 1.20 firmware)
Patrick Boettcherb7f54912006-09-19 12:51:37 -0300163 */
Devin Heitmuellerbdc203e2008-09-06 13:45:27 -0300164static int dib0700_i2c_xfer_new(struct i2c_adapter *adap, struct i2c_msg *msg,
165 int num)
166{
167 /* The new i2c firmware messages are more reliable and in particular
168 properly support i2c read calls not preceded by a write */
169
170 struct dvb_usb_device *d = i2c_get_adapdata(adap);
Olivier Grenieffa58992011-03-24 09:32:26 -0300171 struct dib0700_state *st = d->priv;
Devin Heitmuellerbdc203e2008-09-06 13:45:27 -0300172 uint8_t bus_mode = 1; /* 0=eeprom bus, 1=frontend bus */
173 uint8_t gen_mode = 0; /* 0=master i2c, 1=gpio i2c */
174 uint8_t en_start = 0;
175 uint8_t en_stop = 0;
Devin Heitmuellerbdc203e2008-09-06 13:45:27 -0300176 int result, i;
177
178 /* Ensure nobody else hits the i2c bus while we're sending our
179 sequence of messages, (such as the remote control thread) */
180 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
Santosh Nayak26a11eb2012-03-19 07:27:37 -0300181 return -EINTR;
Devin Heitmuellerbdc203e2008-09-06 13:45:27 -0300182
183 for (i = 0; i < num; i++) {
184 if (i == 0) {
185 /* First message in the transaction */
186 en_start = 1;
187 } else if (!(msg[i].flags & I2C_M_NOSTART)) {
188 /* Device supports repeated-start */
189 en_start = 1;
190 } else {
191 /* Not the first packet and device doesn't support
192 repeated start */
193 en_start = 0;
194 }
195 if (i == (num - 1)) {
196 /* Last message in the transaction */
197 en_stop = 1;
198 }
199
200 if (msg[i].flags & I2C_M_RD) {
201 /* Read request */
202 u16 index, value;
203 uint8_t i2c_dest;
204
205 i2c_dest = (msg[i].addr << 1);
206 value = ((en_start << 7) | (en_stop << 6) |
207 (msg[i].len & 0x3F)) << 8 | i2c_dest;
208 /* I2C ctrl + FE bus; */
Daniel Mack230b27c2010-07-01 01:19:31 -0300209 index = ((gen_mode << 6) & 0xC0) |
210 ((bus_mode << 4) & 0x30);
Devin Heitmuellerbdc203e2008-09-06 13:45:27 -0300211
212 result = usb_control_msg(d->udev,
213 usb_rcvctrlpipe(d->udev, 0),
214 REQUEST_NEW_I2C_READ,
215 USB_TYPE_VENDOR | USB_DIR_IN,
Mauro Carvalho Chehabfa1ecd82016-10-07 06:53:51 -0300216 value, index, st->buf,
Devin Heitmuellerbdc203e2008-09-06 13:45:27 -0300217 msg[i].len,
218 USB_CTRL_GET_TIMEOUT);
219 if (result < 0) {
Olivier Greniebe9bae12011-01-04 05:42:19 -0300220 deb_info("i2c read error (status = %d)\n", result);
Devin Heitmuellerbdc203e2008-09-06 13:45:27 -0300221 break;
222 }
Michael Krufkyd2514992009-01-30 00:24:01 -0300223
Mauro Carvalho Chehabfa1ecd82016-10-07 06:53:51 -0300224 if (msg[i].len > sizeof(st->buf)) {
225 deb_info("buffer too small to fit %d bytes\n",
226 msg[i].len);
227 return -EIO;
228 }
229
230 memcpy(msg[i].buf, st->buf, msg[i].len);
231
Michael Krufkyd2514992009-01-30 00:24:01 -0300232 deb_data("<<< ");
233 debug_dump(msg[i].buf, msg[i].len, deb_data);
234
Devin Heitmuellerbdc203e2008-09-06 13:45:27 -0300235 } else {
236 /* Write request */
Olivier Greniebff469f2011-08-01 12:45:58 -0300237 if (mutex_lock_interruptible(&d->usb_mutex) < 0) {
Olivier Grenie680417b2011-08-04 13:10:03 -0300238 err("could not acquire lock");
Santosh Nayak26a11eb2012-03-19 07:27:37 -0300239 mutex_unlock(&d->i2c_mutex);
240 return -EINTR;
Olivier Greniebff469f2011-08-01 12:45:58 -0300241 }
Olivier Grenieffa58992011-03-24 09:32:26 -0300242 st->buf[0] = REQUEST_NEW_I2C_WRITE;
243 st->buf[1] = msg[i].addr << 1;
244 st->buf[2] = (en_start << 7) | (en_stop << 6) |
Devin Heitmuellerbdc203e2008-09-06 13:45:27 -0300245 (msg[i].len & 0x3F);
246 /* I2C ctrl + FE bus; */
Olivier Grenieffa58992011-03-24 09:32:26 -0300247 st->buf[3] = ((gen_mode << 6) & 0xC0) |
Daniel Mack230b27c2010-07-01 01:19:31 -0300248 ((bus_mode << 4) & 0x30);
Mauro Carvalho Chehabfa1ecd82016-10-07 06:53:51 -0300249
250 if (msg[i].len > sizeof(st->buf) - 4) {
251 deb_info("i2c message to big: %d\n",
252 msg[i].len);
253 return -EIO;
254 }
255
Devin Heitmuellerbdc203e2008-09-06 13:45:27 -0300256 /* The Actual i2c payload */
Olivier Grenieffa58992011-03-24 09:32:26 -0300257 memcpy(&st->buf[4], msg[i].buf, msg[i].len);
Devin Heitmuellerbdc203e2008-09-06 13:45:27 -0300258
Michael Krufkyd2514992009-01-30 00:24:01 -0300259 deb_data(">>> ");
Olivier Grenieffa58992011-03-24 09:32:26 -0300260 debug_dump(st->buf, msg[i].len + 4, deb_data);
Michael Krufkyd2514992009-01-30 00:24:01 -0300261
Devin Heitmuellerbdc203e2008-09-06 13:45:27 -0300262 result = usb_control_msg(d->udev,
263 usb_sndctrlpipe(d->udev, 0),
264 REQUEST_NEW_I2C_WRITE,
265 USB_TYPE_VENDOR | USB_DIR_OUT,
Olivier Grenieffa58992011-03-24 09:32:26 -0300266 0, 0, st->buf, msg[i].len + 4,
Devin Heitmuellerbdc203e2008-09-06 13:45:27 -0300267 USB_CTRL_GET_TIMEOUT);
Olivier Greniebff469f2011-08-01 12:45:58 -0300268 mutex_unlock(&d->usb_mutex);
Devin Heitmuellerbdc203e2008-09-06 13:45:27 -0300269 if (result < 0) {
Olivier Greniebe9bae12011-01-04 05:42:19 -0300270 deb_info("i2c write error (status = %d)\n", result);
Devin Heitmuellerbdc203e2008-09-06 13:45:27 -0300271 break;
272 }
273 }
274 }
275 mutex_unlock(&d->i2c_mutex);
276 return i;
277}
278
279/*
280 * I2C master xfer function (pre-1.20 firmware)
281 */
282static int dib0700_i2c_xfer_legacy(struct i2c_adapter *adap,
283 struct i2c_msg *msg, int num)
Patrick Boettcherb7f54912006-09-19 12:51:37 -0300284{
285 struct dvb_usb_device *d = i2c_get_adapdata(adap);
Olivier Grenieffa58992011-03-24 09:32:26 -0300286 struct dib0700_state *st = d->priv;
Patrick Boettcherb7f54912006-09-19 12:51:37 -0300287 int i,len;
Patrick Boettcherb7f54912006-09-19 12:51:37 -0300288
289 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
Santosh Nayak26a11eb2012-03-19 07:27:37 -0300290 return -EINTR;
Olivier Greniebff469f2011-08-01 12:45:58 -0300291 if (mutex_lock_interruptible(&d->usb_mutex) < 0) {
Olivier Grenie680417b2011-08-04 13:10:03 -0300292 err("could not acquire lock");
Santosh Nayak26a11eb2012-03-19 07:27:37 -0300293 mutex_unlock(&d->i2c_mutex);
294 return -EINTR;
Olivier Greniebff469f2011-08-01 12:45:58 -0300295 }
Patrick Boettcherb7f54912006-09-19 12:51:37 -0300296
297 for (i = 0; i < num; i++) {
298 /* fill in the address */
Olivier Grenieffa58992011-03-24 09:32:26 -0300299 st->buf[1] = msg[i].addr << 1;
Patrick Boettcherb7f54912006-09-19 12:51:37 -0300300 /* fill the buffer */
Mauro Carvalho Chehabfa1ecd82016-10-07 06:53:51 -0300301 if (msg[i].len > sizeof(st->buf) - 2) {
302 deb_info("i2c xfer to big: %d\n",
303 msg[i].len);
304 return -EIO;
305 }
Olivier Grenieffa58992011-03-24 09:32:26 -0300306 memcpy(&st->buf[2], msg[i].buf, msg[i].len);
Patrick Boettcherb7f54912006-09-19 12:51:37 -0300307
308 /* write/read request */
309 if (i+1 < num && (msg[i+1].flags & I2C_M_RD)) {
Olivier Grenieffa58992011-03-24 09:32:26 -0300310 st->buf[0] = REQUEST_I2C_READ;
311 st->buf[1] |= 1;
Patrick Boettcherb7f54912006-09-19 12:51:37 -0300312
313 /* special thing in the current firmware: when length is zero the read-failed */
Olivier Grenieffa58992011-03-24 09:32:26 -0300314 len = dib0700_ctrl_rd(d, st->buf, msg[i].len + 2,
Mauro Carvalho Chehabbd1f9762016-10-07 06:40:24 -0300315 st->buf, msg[i + 1].len);
Olivier Grenieffa58992011-03-24 09:32:26 -0300316 if (len <= 0) {
Devin Heitmueller8db12cd2009-01-21 01:40:04 -0300317 deb_info("I2C read failed on address 0x%02x\n",
Olivier Grenieffa58992011-03-24 09:32:26 -0300318 msg[i].addr);
Patrick Boettcherb7f54912006-09-19 12:51:37 -0300319 break;
Patrick Boettcher303cbea2006-09-19 12:51:56 -0300320 }
Patrick Boettcherb7f54912006-09-19 12:51:37 -0300321
Mauro Carvalho Chehabfa1ecd82016-10-07 06:53:51 -0300322 if (msg[i + 1].len > sizeof(st->buf)) {
323 deb_info("i2c xfer buffer to small for %d\n",
324 msg[i].len);
325 return -EIO;
326 }
Mauro Carvalho Chehabbd1f9762016-10-07 06:40:24 -0300327 memcpy(msg[i + 1].buf, st->buf, msg[i + 1].len);
328
Patrick Boettcherb7f54912006-09-19 12:51:37 -0300329 msg[i+1].len = len;
330
331 i++;
332 } else {
Olivier Grenieffa58992011-03-24 09:32:26 -0300333 st->buf[0] = REQUEST_I2C_WRITE;
334 if (dib0700_ctrl_wr(d, st->buf, msg[i].len + 2) < 0)
Patrick Boettcherb7f54912006-09-19 12:51:37 -0300335 break;
336 }
337 }
Olivier Greniebff469f2011-08-01 12:45:58 -0300338 mutex_unlock(&d->usb_mutex);
Patrick Boettcherb7f54912006-09-19 12:51:37 -0300339 mutex_unlock(&d->i2c_mutex);
Olivier Grenieffa58992011-03-24 09:32:26 -0300340
Patrick Boettcherb7f54912006-09-19 12:51:37 -0300341 return i;
342}
343
Devin Heitmuellerbdc203e2008-09-06 13:45:27 -0300344static int dib0700_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msg,
345 int num)
346{
347 struct dvb_usb_device *d = i2c_get_adapdata(adap);
348 struct dib0700_state *st = d->priv;
349
350 if (st->fw_use_new_i2c_api == 1) {
351 /* User running at least fw 1.20 */
352 return dib0700_i2c_xfer_new(adap, msg, num);
353 } else {
354 /* Use legacy calls */
355 return dib0700_i2c_xfer_legacy(adap, msg, num);
356 }
357}
358
Patrick Boettcherb7f54912006-09-19 12:51:37 -0300359static u32 dib0700_i2c_func(struct i2c_adapter *adapter)
360{
361 return I2C_FUNC_I2C;
362}
363
364struct i2c_algorithm dib0700_i2c_algo = {
365 .master_xfer = dib0700_i2c_xfer,
366 .functionality = dib0700_i2c_func,
367};
368
Patrick Boettcher6958eff2006-09-19 12:51:40 -0300369int dib0700_identify_state(struct usb_device *udev, struct dvb_usb_device_properties *props,
370 struct dvb_usb_device_description **desc, int *cold)
371{
Olivier Grenieffa58992011-03-24 09:32:26 -0300372 s16 ret;
373 u8 *b;
374
375 b = kmalloc(16, GFP_KERNEL);
376 if (!b)
377 return -ENOMEM;
378
379
380 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
Patrick Boettchera75763f2006-10-18 08:34:16 -0300381 REQUEST_GET_VERSION, USB_TYPE_VENDOR | USB_DIR_IN, 0, 0, b, 16, USB_CTRL_GET_TIMEOUT);
382
383 deb_info("FW GET_VERSION length: %d\n",ret);
384
385 *cold = ret <= 0;
Patrick Boettcher6958eff2006-09-19 12:51:40 -0300386 deb_info("cold: %d\n", *cold);
Olivier Grenieffa58992011-03-24 09:32:26 -0300387
388 kfree(b);
Patrick Boettcher6958eff2006-09-19 12:51:40 -0300389 return 0;
390}
391
Patrick Boettchera75763f2006-10-18 08:34:16 -0300392static int dib0700_set_clock(struct dvb_usb_device *d, u8 en_pll,
393 u8 pll_src, u8 pll_range, u8 clock_gpio3, u16 pll_prediv,
394 u16 pll_loopdiv, u16 free_div, u16 dsuScaler)
395{
Olivier Grenieffa58992011-03-24 09:32:26 -0300396 struct dib0700_state *st = d->priv;
Olivier Greniebff469f2011-08-01 12:45:58 -0300397 int ret;
398
399 if (mutex_lock_interruptible(&d->usb_mutex) < 0) {
Olivier Grenie680417b2011-08-04 13:10:03 -0300400 err("could not acquire lock");
Santosh Nayak26a11eb2012-03-19 07:27:37 -0300401 return -EINTR;
Olivier Greniebff469f2011-08-01 12:45:58 -0300402 }
Patrick Boettchera75763f2006-10-18 08:34:16 -0300403
Olivier Grenieffa58992011-03-24 09:32:26 -0300404 st->buf[0] = REQUEST_SET_CLOCK;
405 st->buf[1] = (en_pll << 7) | (pll_src << 6) |
406 (pll_range << 5) | (clock_gpio3 << 4);
407 st->buf[2] = (pll_prediv >> 8) & 0xff; /* MSB */
408 st->buf[3] = pll_prediv & 0xff; /* LSB */
409 st->buf[4] = (pll_loopdiv >> 8) & 0xff; /* MSB */
410 st->buf[5] = pll_loopdiv & 0xff; /* LSB */
411 st->buf[6] = (free_div >> 8) & 0xff; /* MSB */
412 st->buf[7] = free_div & 0xff; /* LSB */
413 st->buf[8] = (dsuScaler >> 8) & 0xff; /* MSB */
414 st->buf[9] = dsuScaler & 0xff; /* LSB */
415
416 ret = dib0700_ctrl_wr(d, st->buf, 10);
Olivier Greniebff469f2011-08-01 12:45:58 -0300417 mutex_unlock(&d->usb_mutex);
Olivier Grenieffa58992011-03-24 09:32:26 -0300418
419 return ret;
Patrick Boettchera75763f2006-10-18 08:34:16 -0300420}
421
Olivier Grenie7757ddd2011-01-03 15:30:14 -0300422int dib0700_set_i2c_speed(struct dvb_usb_device *d, u16 scl_kHz)
423{
Olivier Grenieffa58992011-03-24 09:32:26 -0300424 struct dib0700_state *st = d->priv;
Olivier Grenie7757ddd2011-01-03 15:30:14 -0300425 u16 divider;
Olivier Greniebff469f2011-08-01 12:45:58 -0300426 int ret;
Olivier Grenie7757ddd2011-01-03 15:30:14 -0300427
428 if (scl_kHz == 0)
429 return -EINVAL;
430
Olivier Greniebff469f2011-08-01 12:45:58 -0300431 if (mutex_lock_interruptible(&d->usb_mutex) < 0) {
Olivier Grenie680417b2011-08-04 13:10:03 -0300432 err("could not acquire lock");
Santosh Nayak26a11eb2012-03-19 07:27:37 -0300433 return -EINTR;
Olivier Greniebff469f2011-08-01 12:45:58 -0300434 }
435
Olivier Grenieffa58992011-03-24 09:32:26 -0300436 st->buf[0] = REQUEST_SET_I2C_PARAM;
Olivier Grenie7757ddd2011-01-03 15:30:14 -0300437 divider = (u16) (30000 / scl_kHz);
Olivier Grenieffa58992011-03-24 09:32:26 -0300438 st->buf[1] = 0;
439 st->buf[2] = (u8) (divider >> 8);
440 st->buf[3] = (u8) (divider & 0xff);
Olivier Grenie7757ddd2011-01-03 15:30:14 -0300441 divider = (u16) (72000 / scl_kHz);
Olivier Grenieffa58992011-03-24 09:32:26 -0300442 st->buf[4] = (u8) (divider >> 8);
443 st->buf[5] = (u8) (divider & 0xff);
Olivier Grenie7757ddd2011-01-03 15:30:14 -0300444 divider = (u16) (72000 / scl_kHz); /* clock: 72MHz */
Olivier Grenieffa58992011-03-24 09:32:26 -0300445 st->buf[6] = (u8) (divider >> 8);
446 st->buf[7] = (u8) (divider & 0xff);
Olivier Grenie7757ddd2011-01-03 15:30:14 -0300447
Olivier Grenieb4d6046e2011-01-04 13:08:14 -0300448 deb_info("setting I2C speed: %04x %04x %04x (%d kHz).",
Olivier Grenieffa58992011-03-24 09:32:26 -0300449 (st->buf[2] << 8) | (st->buf[3]), (st->buf[4] << 8) |
450 st->buf[5], (st->buf[6] << 8) | st->buf[7], scl_kHz);
Olivier Greniebff469f2011-08-01 12:45:58 -0300451
452 ret = dib0700_ctrl_wr(d, st->buf, 8);
453 mutex_unlock(&d->usb_mutex);
454
455 return ret;
Olivier Grenie7757ddd2011-01-03 15:30:14 -0300456}
457
458
Patrick Boettchera75763f2006-10-18 08:34:16 -0300459int dib0700_ctrl_clock(struct dvb_usb_device *d, u32 clk_MHz, u8 clock_out_gp3)
460{
461 switch (clk_MHz) {
462 case 72: dib0700_set_clock(d, 1, 0, 1, clock_out_gp3, 2, 24, 0, 0x4c); break;
463 default: return -EINVAL;
464 }
465 return 0;
466}
467
Patrick Boettcherb7f54912006-09-19 12:51:37 -0300468static int dib0700_jumpram(struct usb_device *udev, u32 address)
469{
Olivier Grenieffa58992011-03-24 09:32:26 -0300470 int ret = 0, actlen;
471 u8 *buf;
472
473 buf = kmalloc(8, GFP_KERNEL);
474 if (!buf)
475 return -ENOMEM;
476 buf[0] = REQUEST_JUMPRAM;
477 buf[1] = 0;
478 buf[2] = 0;
479 buf[3] = 0;
480 buf[4] = (address >> 24) & 0xff;
481 buf[5] = (address >> 16) & 0xff;
482 buf[6] = (address >> 8) & 0xff;
483 buf[7] = address & 0xff;
Patrick Boettcherb7f54912006-09-19 12:51:37 -0300484
485 if ((ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, 0x01),buf,8,&actlen,1000)) < 0) {
486 deb_fw("jumpram to 0x%x failed\n",address);
Olivier Grenieffa58992011-03-24 09:32:26 -0300487 goto out;
Patrick Boettcherb7f54912006-09-19 12:51:37 -0300488 }
489 if (actlen != 8) {
490 deb_fw("jumpram to 0x%x failed\n",address);
Olivier Grenieffa58992011-03-24 09:32:26 -0300491 ret = -EIO;
492 goto out;
Patrick Boettcherb7f54912006-09-19 12:51:37 -0300493 }
Olivier Grenieffa58992011-03-24 09:32:26 -0300494out:
495 kfree(buf);
496 return ret;
Patrick Boettcherb7f54912006-09-19 12:51:37 -0300497}
498
499int dib0700_download_firmware(struct usb_device *udev, const struct firmware *fw)
500{
501 struct hexline hx;
Olivier Grenieacc5c9e2009-12-07 08:22:53 -0300502 int pos = 0, ret, act_len, i, adap_num;
Olivier Grenieffa58992011-03-24 09:32:26 -0300503 u8 *buf;
Olivier Grenieacc5c9e2009-12-07 08:22:53 -0300504 u32 fw_version;
Patrick Boettcherb7f54912006-09-19 12:51:37 -0300505
Olivier Grenieffa58992011-03-24 09:32:26 -0300506 buf = kmalloc(260, GFP_KERNEL);
507 if (!buf)
508 return -ENOMEM;
Patrick Boettcherb7f54912006-09-19 12:51:37 -0300509
510 while ((ret = dvb_usb_get_hexline(fw, &hx, &pos)) > 0) {
Daniel Mack230b27c2010-07-01 01:19:31 -0300511 deb_fwdata("writing to address 0x%08x (buffer: 0x%02x %02x)\n",
512 hx.addr, hx.len, hx.chk);
Patrick Boettcherb7f54912006-09-19 12:51:37 -0300513
514 buf[0] = hx.len;
Patrick Boettcher5bc63602006-09-19 12:51:46 -0300515 buf[1] = (hx.addr >> 8) & 0xff;
516 buf[2] = hx.addr & 0xff;
Patrick Boettcherb7f54912006-09-19 12:51:37 -0300517 buf[3] = hx.type;
518 memcpy(&buf[4],hx.data,hx.len);
519 buf[4+hx.len] = hx.chk;
520
521 ret = usb_bulk_msg(udev,
522 usb_sndbulkpipe(udev, 0x01),
523 buf,
524 hx.len + 5,
525 &act_len,
526 1000);
527
528 if (ret < 0) {
529 err("firmware download failed at %d with %d",pos,ret);
Olivier Grenieffa58992011-03-24 09:32:26 -0300530 goto out;
Patrick Boettcherb7f54912006-09-19 12:51:37 -0300531 }
532 }
533
534 if (ret == 0) {
535 /* start the firmware */
Patrick Boettcher6958eff2006-09-19 12:51:40 -0300536 if ((ret = dib0700_jumpram(udev, 0x70000000)) == 0) {
Patrick Boettcherb7f54912006-09-19 12:51:37 -0300537 info("firmware started successfully.");
Patrick Boettchera75763f2006-10-18 08:34:16 -0300538 msleep(500);
Patrick Boettcher6958eff2006-09-19 12:51:40 -0300539 }
Patrick Boettcherb7f54912006-09-19 12:51:37 -0300540 } else
541 ret = -EIO;
542
Olivier Grenieacc5c9e2009-12-07 08:22:53 -0300543 /* the number of ts packet has to be at least 1 */
544 if (nb_packet_buffer_size < 1)
545 nb_packet_buffer_size = 1;
546
Kees Cookbad7de72016-05-19 17:09:14 -0700547 /* get the firmware version */
Olivier Grenieacc5c9e2009-12-07 08:22:53 -0300548 usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
549 REQUEST_GET_VERSION,
550 USB_TYPE_VENDOR | USB_DIR_IN, 0, 0,
Olivier Grenieffa58992011-03-24 09:32:26 -0300551 buf, 16, USB_CTRL_GET_TIMEOUT);
552 fw_version = (buf[8] << 24) | (buf[9] << 16) | (buf[10] << 8) | buf[11];
Olivier Grenieacc5c9e2009-12-07 08:22:53 -0300553
554 /* set the buffer size - DVB-USB is allocating URB buffers
555 * only after the firwmare download was successful */
556 for (i = 0; i < dib0700_device_count; i++) {
557 for (adap_num = 0; adap_num < dib0700_devices[i].num_adapters;
558 adap_num++) {
Daniel Mack230b27c2010-07-01 01:19:31 -0300559 if (fw_version >= 0x10201) {
Michael Krufky77eed212011-09-06 09:31:57 -0300560 dib0700_devices[i].adapter[adap_num].fe[0].stream.u.bulk.buffersize = 188*nb_packet_buffer_size;
Daniel Mack230b27c2010-07-01 01:19:31 -0300561 } else {
Olivier Grenieacc5c9e2009-12-07 08:22:53 -0300562 /* for fw version older than 1.20.1,
563 * the buffersize has to be n times 512 */
Michael Krufky77eed212011-09-06 09:31:57 -0300564 dib0700_devices[i].adapter[adap_num].fe[0].stream.u.bulk.buffersize = ((188*nb_packet_buffer_size+188/2)/512)*512;
565 if (dib0700_devices[i].adapter[adap_num].fe[0].stream.u.bulk.buffersize < 512)
566 dib0700_devices[i].adapter[adap_num].fe[0].stream.u.bulk.buffersize = 512;
Olivier Grenieacc5c9e2009-12-07 08:22:53 -0300567 }
568 }
569 }
Olivier Grenieffa58992011-03-24 09:32:26 -0300570out:
571 kfree(buf);
Patrick Boettcherb7f54912006-09-19 12:51:37 -0300572 return ret;
573}
574
575int dib0700_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
576{
577 struct dib0700_state *st = adap->dev->priv;
Olivier Grenieacc5c9e2009-12-07 08:22:53 -0300578 int ret;
579
580 if ((onoff != 0) && (st->fw_version >= 0x10201)) {
581 /* for firmware later than 1.20.1,
582 * the USB xfer length can be set */
583 ret = dib0700_set_usb_xfer_len(adap->dev,
584 st->nb_packet_buffer_size);
585 if (ret < 0) {
586 deb_info("can not set the USB xfer len\n");
587 return ret;
588 }
589 }
Patrick Boettcherb7f54912006-09-19 12:51:37 -0300590
Jiri Slabya96fbe02013-01-03 12:53:53 -0300591 mutex_lock(&adap->dev->usb_mutex);
Olivier Greniebff469f2011-08-01 12:45:58 -0300592
Olivier Grenieffa58992011-03-24 09:32:26 -0300593 st->buf[0] = REQUEST_ENABLE_VIDEO;
594 /* this bit gives a kind of command,
595 * rather than enabling something or not */
596 st->buf[1] = (onoff << 4) | 0x00;
Devin Heitmuellercb22cb52008-09-08 05:42:42 -0300597
598 if (st->disable_streaming_master_mode == 1)
Olivier Grenieffa58992011-03-24 09:32:26 -0300599 st->buf[2] = 0x00;
Devin Heitmuellercb22cb52008-09-08 05:42:42 -0300600 else
Olivier Grenieffa58992011-03-24 09:32:26 -0300601 st->buf[2] = 0x01 << 4; /* Master mode */
Devin Heitmuellercb22cb52008-09-08 05:42:42 -0300602
Olivier Grenieffa58992011-03-24 09:32:26 -0300603 st->buf[3] = 0x00;
Patrick Boettcherb7f54912006-09-19 12:51:37 -0300604
605 deb_info("modifying (%d) streaming state for %d\n", onoff, adap->id);
606
Olivier Grenie7757ddd2011-01-03 15:30:14 -0300607 st->channel_state &= ~0x3;
Michael Krufky77eed212011-09-06 09:31:57 -0300608 if ((adap->fe_adap[0].stream.props.endpoint != 2)
609 && (adap->fe_adap[0].stream.props.endpoint != 3)) {
610 deb_info("the endpoint number (%i) is not correct, use the adapter id instead", adap->fe_adap[0].stream.props.endpoint);
Olivier Grenie7757ddd2011-01-03 15:30:14 -0300611 if (onoff)
612 st->channel_state |= 1 << (adap->id);
613 else
Mauro Carvalho Chehabf85ed0c2013-02-06 08:29:39 -0200614 st->channel_state |= 1 << ~(adap->id);
Olivier Grenieb4d6046e2011-01-04 13:08:14 -0300615 } else {
Olivier Grenie7757ddd2011-01-03 15:30:14 -0300616 if (onoff)
Michael Krufky77eed212011-09-06 09:31:57 -0300617 st->channel_state |= 1 << (adap->fe_adap[0].stream.props.endpoint-2);
Olivier Grenie7757ddd2011-01-03 15:30:14 -0300618 else
Michael Krufky77eed212011-09-06 09:31:57 -0300619 st->channel_state |= 1 << (3-adap->fe_adap[0].stream.props.endpoint);
Olivier Grenie7757ddd2011-01-03 15:30:14 -0300620 }
Patrick Boettcherb7f54912006-09-19 12:51:37 -0300621
Olivier Grenieffa58992011-03-24 09:32:26 -0300622 st->buf[2] |= st->channel_state;
Patrick Boettcherb7f54912006-09-19 12:51:37 -0300623
Olivier Grenieffa58992011-03-24 09:32:26 -0300624 deb_info("data for streaming: %x %x\n", st->buf[1], st->buf[2]);
Patrick Boettcherb7f54912006-09-19 12:51:37 -0300625
Olivier Greniebff469f2011-08-01 12:45:58 -0300626 ret = dib0700_ctrl_wr(adap->dev, st->buf, 4);
627 mutex_unlock(&adap->dev->usb_mutex);
628
629 return ret;
Patrick Boettcherb7f54912006-09-19 12:51:37 -0300630}
631
David Härdemanc003ab12012-10-11 19:11:54 -0300632int dib0700_change_protocol(struct rc_dev *rc, u64 *rc_type)
Mauro Carvalho Chehab0ffd1ab2010-08-01 09:37:23 -0300633{
David Härdemand8b4b582010-10-29 16:08:23 -0300634 struct dvb_usb_device *d = rc->priv;
Mauro Carvalho Chehab0ffd1ab2010-08-01 09:37:23 -0300635 struct dib0700_state *st = d->priv;
Mauro Carvalho Chehab0ffd1ab2010-08-01 09:37:23 -0300636 int new_proto, ret;
637
Olivier Greniebff469f2011-08-01 12:45:58 -0300638 if (mutex_lock_interruptible(&d->usb_mutex) < 0) {
Olivier Grenie680417b2011-08-04 13:10:03 -0300639 err("could not acquire lock");
Santosh Nayak26a11eb2012-03-19 07:27:37 -0300640 return -EINTR;
Olivier Greniebff469f2011-08-01 12:45:58 -0300641 }
642
Olivier Grenieffa58992011-03-24 09:32:26 -0300643 st->buf[0] = REQUEST_SET_RC;
644 st->buf[1] = 0;
645 st->buf[2] = 0;
646
Mauro Carvalho Chehab0ffd1ab2010-08-01 09:37:23 -0300647 /* Set the IR mode */
David Härdemanc003ab12012-10-11 19:11:54 -0300648 if (*rc_type & RC_BIT_RC5) {
Mauro Carvalho Chehab0ffd1ab2010-08-01 09:37:23 -0300649 new_proto = 1;
David Härdemanc003ab12012-10-11 19:11:54 -0300650 *rc_type = RC_BIT_RC5;
651 } else if (*rc_type & RC_BIT_NEC) {
Mauro Carvalho Chehab0ffd1ab2010-08-01 09:37:23 -0300652 new_proto = 0;
David Härdemanc003ab12012-10-11 19:11:54 -0300653 *rc_type = RC_BIT_NEC;
654 } else if (*rc_type & RC_BIT_RC6_MCE) {
Olivier Greniebff469f2011-08-01 12:45:58 -0300655 if (st->fw_version < 0x10200) {
656 ret = -EINVAL;
657 goto out;
658 }
Mauro Carvalho Chehab0ffd1ab2010-08-01 09:37:23 -0300659 new_proto = 2;
David Härdemanc003ab12012-10-11 19:11:54 -0300660 *rc_type = RC_BIT_RC6_MCE;
Olivier Greniebff469f2011-08-01 12:45:58 -0300661 } else {
662 ret = -EINVAL;
663 goto out;
664 }
Mauro Carvalho Chehab0ffd1ab2010-08-01 09:37:23 -0300665
Olivier Grenieffa58992011-03-24 09:32:26 -0300666 st->buf[1] = new_proto;
Mauro Carvalho Chehab0ffd1ab2010-08-01 09:37:23 -0300667
Olivier Grenieffa58992011-03-24 09:32:26 -0300668 ret = dib0700_ctrl_wr(d, st->buf, 3);
Mauro Carvalho Chehab0ffd1ab2010-08-01 09:37:23 -0300669 if (ret < 0) {
670 err("ir protocol setup failed");
Olivier Greniebff469f2011-08-01 12:45:58 -0300671 goto out;
Mauro Carvalho Chehab0ffd1ab2010-08-01 09:37:23 -0300672 }
673
David Härdemanc003ab12012-10-11 19:11:54 -0300674 d->props.rc.core.protocol = *rc_type;
Mauro Carvalho Chehab0ffd1ab2010-08-01 09:37:23 -0300675
Olivier Greniebff469f2011-08-01 12:45:58 -0300676out:
677 mutex_unlock(&d->usb_mutex);
Mauro Carvalho Chehab0ffd1ab2010-08-01 09:37:23 -0300678 return ret;
679}
680
Devin Heitmueller6a207102010-01-04 02:43:19 -0300681/* This is the structure of the RC response packet starting in firmware 1.20 */
682struct dib0700_rc_response {
683 u8 report_id;
684 u8 data_state;
David Härdeman4d298b82015-03-30 17:51:01 -0300685 union {
686 struct {
687 u8 system;
688 u8 not_system;
689 u8 data;
690 u8 not_data;
691 } nec;
692 struct {
693 u8 not_used;
694 u8 system;
695 u8 data;
696 u8 not_data;
697 } rc5;
698 };
Devin Heitmueller6a207102010-01-04 02:43:19 -0300699};
700#define RC_MSG_SIZE_V1_20 6
701
702static void dib0700_rc_urb_completion(struct urb *purb)
703{
704 struct dvb_usb_device *d = purb->context;
Mauro Carvalho Chehabd3c501d2010-08-01 10:35:49 -0300705 struct dib0700_rc_response *poll_reply;
David Härdeman120703f2014-04-03 20:31:30 -0300706 enum rc_type protocol;
Sean Youngba13e982016-11-10 17:44:49 +0100707 u32 keycode;
Mauro Carvalho Chehab72b39312010-07-31 22:56:00 -0300708 u8 toggle;
Devin Heitmueller6a207102010-01-04 02:43:19 -0300709
710 deb_info("%s()\n", __func__);
David Härdemand8b4b582010-10-29 16:08:23 -0300711 if (d->rc_dev == NULL) {
Devin Heitmueller6a207102010-01-04 02:43:19 -0300712 /* This will occur if disable_rc_polling=1 */
Jean Delvared3db22e2012-03-13 13:52:50 -0300713 kfree(purb->transfer_buffer);
Devin Heitmueller6a207102010-01-04 02:43:19 -0300714 usb_free_urb(purb);
715 return;
716 }
717
Mauro Carvalho Chehabd3c501d2010-08-01 10:35:49 -0300718 poll_reply = purb->transfer_buffer;
Devin Heitmueller6a207102010-01-04 02:43:19 -0300719
720 if (purb->status < 0) {
721 deb_info("discontinuing polling\n");
Jean Delvared3db22e2012-03-13 13:52:50 -0300722 kfree(purb->transfer_buffer);
Devin Heitmueller6a207102010-01-04 02:43:19 -0300723 usb_free_urb(purb);
724 return;
725 }
726
727 if (purb->actual_length != RC_MSG_SIZE_V1_20) {
728 deb_info("malformed rc msg size=%d\n", purb->actual_length);
729 goto resubmit;
730 }
731
Mauro Carvalho Chehabd3c501d2010-08-01 10:35:49 -0300732 deb_data("IR ID = %02X state = %02X System = %02X %02X Cmd = %02X %02X (len %d)\n",
733 poll_reply->report_id, poll_reply->data_state,
David Härdeman4d298b82015-03-30 17:51:01 -0300734 poll_reply->nec.system, poll_reply->nec.not_system,
735 poll_reply->nec.data, poll_reply->nec.not_data,
Mauro Carvalho Chehabd3c501d2010-08-01 10:35:49 -0300736 purb->actual_length);
Devin Heitmueller6a207102010-01-04 02:43:19 -0300737
Mauro Carvalho Chehab0ffd1ab2010-08-01 09:37:23 -0300738 switch (d->props.rc.core.protocol) {
David Härdemanc003ab12012-10-11 19:11:54 -0300739 case RC_BIT_NEC:
Mauro Carvalho Chehab72b39312010-07-31 22:56:00 -0300740 toggle = 0;
Devin Heitmueller6a207102010-01-04 02:43:19 -0300741
742 /* NEC protocol sends repeat code as 0 0 0 FF */
David Härdeman4d298b82015-03-30 17:51:01 -0300743 if (poll_reply->nec.system == 0x00 &&
744 poll_reply->nec.not_system == 0x00 &&
745 poll_reply->nec.data == 0x00 &&
746 poll_reply->nec.not_data == 0xff) {
Mauro Carvalho Chehabd3c501d2010-08-01 10:35:49 -0300747 poll_reply->data_state = 2;
Sean Youngba13e982016-11-10 17:44:49 +0100748 rc_repeat(d->rc_dev);
749 goto resubmit;
Devin Heitmueller6a207102010-01-04 02:43:19 -0300750 }
Mauro Carvalho Chehab72b39312010-07-31 22:56:00 -0300751
David Härdeman4d298b82015-03-30 17:51:01 -0300752 if ((poll_reply->nec.data ^ poll_reply->nec.not_data) != 0xff) {
David Härdemanaf3a4a92014-04-03 20:31:51 -0300753 deb_data("NEC32 protocol\n");
David Härdeman4d298b82015-03-30 17:51:01 -0300754 keycode = RC_SCANCODE_NEC32(poll_reply->nec.system << 24 |
755 poll_reply->nec.not_system << 16 |
756 poll_reply->nec.data << 8 |
757 poll_reply->nec.not_data);
Sean Young2ceeca02016-09-21 06:54:19 -0300758 protocol = RC_TYPE_NEC32;
David Härdeman4d298b82015-03-30 17:51:01 -0300759 } else if ((poll_reply->nec.system ^ poll_reply->nec.not_system) != 0xff) {
Mauro Carvalho Chehabd3c501d2010-08-01 10:35:49 -0300760 deb_data("NEC extended protocol\n");
David Härdeman4d298b82015-03-30 17:51:01 -0300761 keycode = RC_SCANCODE_NECX(poll_reply->nec.system << 8 |
762 poll_reply->nec.not_system,
763 poll_reply->nec.data);
David Härdeman120703f2014-04-03 20:31:30 -0300764
Sean Young2ceeca02016-09-21 06:54:19 -0300765 protocol = RC_TYPE_NECX;
Mauro Carvalho Chehabd3c501d2010-08-01 10:35:49 -0300766 } else {
767 deb_data("NEC normal protocol\n");
David Härdeman4d298b82015-03-30 17:51:01 -0300768 keycode = RC_SCANCODE_NEC(poll_reply->nec.system,
769 poll_reply->nec.data);
Sean Young2ceeca02016-09-21 06:54:19 -0300770 protocol = RC_TYPE_NEC;
Mauro Carvalho Chehabd3c501d2010-08-01 10:35:49 -0300771 }
772
Devin Heitmueller6a207102010-01-04 02:43:19 -0300773 break;
774 default:
Mauro Carvalho Chehabd3c501d2010-08-01 10:35:49 -0300775 deb_data("RC5 protocol\n");
David Härdeman120703f2014-04-03 20:31:30 -0300776 protocol = RC_TYPE_RC5;
Mauro Carvalho Chehabd3c501d2010-08-01 10:35:49 -0300777 toggle = poll_reply->report_id;
David Härdeman4d298b82015-03-30 17:51:01 -0300778 keycode = RC_SCANCODE_RC5(poll_reply->rc5.system, poll_reply->rc5.data);
779
780 if ((poll_reply->rc5.data ^ poll_reply->rc5.not_data) != 0xff) {
781 /* Key failed integrity check */
782 err("key failed integrity check: %02x %02x %02x %02x",
783 poll_reply->rc5.not_used, poll_reply->rc5.system,
784 poll_reply->rc5.data, poll_reply->rc5.not_data);
785 goto resubmit;
786 }
Mauro Carvalho Chehab72b39312010-07-31 22:56:00 -0300787
Devin Heitmueller6a207102010-01-04 02:43:19 -0300788 break;
789 }
790
David Härdeman120703f2014-04-03 20:31:30 -0300791 rc_keydown(d->rc_dev, protocol, keycode, toggle);
Devin Heitmueller6a207102010-01-04 02:43:19 -0300792
793resubmit:
794 /* Clean the buffer before we requeue */
795 memset(purb->transfer_buffer, 0, RC_MSG_SIZE_V1_20);
796
797 /* Requeue URB */
798 usb_submit_urb(purb, GFP_ATOMIC);
799}
800
Mauro Carvalho Chehabc4018fa2014-05-21 17:40:25 -0300801int dib0700_rc_setup(struct dvb_usb_device *d, struct usb_interface *intf)
Janne Grunau89f42672007-07-31 19:45:13 -0300802{
Devin Heitmueller6a207102010-01-04 02:43:19 -0300803 struct dib0700_state *st = d->priv;
Devin Heitmueller6a207102010-01-04 02:43:19 -0300804 struct urb *purb;
Mauro Carvalho Chehabc4018fa2014-05-21 17:40:25 -0300805 const struct usb_endpoint_descriptor *e;
806 int ret, rc_ep = 1;
807 unsigned int pipe = 0;
Devin Heitmueller6a207102010-01-04 02:43:19 -0300808
Mauro Carvalho Chehab0ffd1ab2010-08-01 09:37:23 -0300809 /* Poll-based. Don't initialize bulk mode */
Mauro Carvalho Chehabc4018fa2014-05-21 17:40:25 -0300810 if (st->fw_version < 0x10200 || !intf)
Devin Heitmueller6a207102010-01-04 02:43:19 -0300811 return 0;
812
813 /* Starting in firmware 1.20, the RC info is provided on a bulk pipe */
Mauro Carvalho Chehabc4018fa2014-05-21 17:40:25 -0300814
Johan Hovolda1ba8812020-01-03 17:35:12 +0100815 if (intf->cur_altsetting->desc.bNumEndpoints < rc_ep + 1)
Johan Hovold466b45a2017-03-13 09:53:54 -0300816 return -ENODEV;
817
Devin Heitmueller6a207102010-01-04 02:43:19 -0300818 purb = usb_alloc_urb(0, GFP_KERNEL);
Wolfram Sangabbde782016-08-11 18:03:48 -0300819 if (purb == NULL)
Daniel Mack8871c852010-07-01 01:08:58 -0300820 return -ENOMEM;
Devin Heitmueller6a207102010-01-04 02:43:19 -0300821
822 purb->transfer_buffer = kzalloc(RC_MSG_SIZE_V1_20, GFP_KERNEL);
823 if (purb->transfer_buffer == NULL) {
Jiri Slaby24ed6932012-07-30 14:03:16 -0300824 err("rc kzalloc failed");
Devin Heitmueller6a207102010-01-04 02:43:19 -0300825 usb_free_urb(purb);
Daniel Mack8871c852010-07-01 01:08:58 -0300826 return -ENOMEM;
Devin Heitmueller6a207102010-01-04 02:43:19 -0300827 }
828
829 purb->status = -EINPROGRESS;
Mauro Carvalho Chehabc4018fa2014-05-21 17:40:25 -0300830
831 /*
832 * Some devices like the Hauppauge NovaTD model 52009 use an interrupt
833 * endpoint, while others use a bulk one.
834 */
Johan Hovolda1ba8812020-01-03 17:35:12 +0100835 e = &intf->cur_altsetting->endpoint[rc_ep].desc;
Mauro Carvalho Chehabc4018fa2014-05-21 17:40:25 -0300836 if (usb_endpoint_dir_in(e)) {
837 if (usb_endpoint_xfer_bulk(e)) {
838 pipe = usb_rcvbulkpipe(d->udev, rc_ep);
839 usb_fill_bulk_urb(purb, d->udev, pipe,
840 purb->transfer_buffer,
841 RC_MSG_SIZE_V1_20,
842 dib0700_rc_urb_completion, d);
843
844 } else if (usb_endpoint_xfer_int(e)) {
845 pipe = usb_rcvintpipe(d->udev, rc_ep);
846 usb_fill_int_urb(purb, d->udev, pipe,
847 purb->transfer_buffer,
848 RC_MSG_SIZE_V1_20,
849 dib0700_rc_urb_completion, d, 1);
850 }
851 }
852
853 if (!pipe) {
854 err("There's no endpoint for remote controller");
855 kfree(purb->transfer_buffer);
856 usb_free_urb(purb);
857 return 0;
858 }
Devin Heitmueller6a207102010-01-04 02:43:19 -0300859
860 ret = usb_submit_urb(purb, GFP_ATOMIC);
Jean Delvared3db22e2012-03-13 13:52:50 -0300861 if (ret) {
Jiri Slaby24ed6932012-07-30 14:03:16 -0300862 err("rc submit urb failed");
Jean Delvared3db22e2012-03-13 13:52:50 -0300863 kfree(purb->transfer_buffer);
864 usb_free_urb(purb);
865 }
Devin Heitmueller6a207102010-01-04 02:43:19 -0300866
Daniel Mack8871c852010-07-01 01:08:58 -0300867 return ret;
Janne Grunau89f42672007-07-31 19:45:13 -0300868}
869
Patrick Boettcherb7f54912006-09-19 12:51:37 -0300870static int dib0700_probe(struct usb_interface *intf,
871 const struct usb_device_id *id)
872{
873 int i;
Janne Grunau89f42672007-07-31 19:45:13 -0300874 struct dvb_usb_device *dev;
Patrick Boettcherb7f54912006-09-19 12:51:37 -0300875
876 for (i = 0; i < dib0700_device_count; i++)
Janne Grunau78e92002008-04-09 19:13:13 -0300877 if (dvb_usb_device_init(intf, &dib0700_devices[i], THIS_MODULE,
Olivier Grenieacc5c9e2009-12-07 08:22:53 -0300878 &dev, adapter_nr) == 0) {
879 struct dib0700_state *st = dev->priv;
880 u32 hwversion, romversion, fw_version, fwtype;
881
882 dib0700_get_version(dev, &hwversion, &romversion,
883 &fw_version, &fwtype);
884
885 deb_info("Firmware version: %x, %d, 0x%x, %d\n",
886 hwversion, romversion, fw_version, fwtype);
887
888 st->fw_version = fw_version;
889 st->nb_packet_buffer_size = (u32)nb_packet_buffer_size;
890
Mauro Carvalho Chehab72b39312010-07-31 22:56:00 -0300891 /* Disable polling mode on newer firmwares */
892 if (st->fw_version >= 0x10200)
893 dev->props.rc.core.bulk_mode = true;
894 else
895 dev->props.rc.core.bulk_mode = false;
896
Mauro Carvalho Chehabc4018fa2014-05-21 17:40:25 -0300897 dib0700_rc_setup(dev, intf);
Olivier Grenieacc5c9e2009-12-07 08:22:53 -0300898
Patrick Boettcherb7f54912006-09-19 12:51:37 -0300899 return 0;
Janne Grunau89f42672007-07-31 19:45:13 -0300900 }
Patrick Boettcherb7f54912006-09-19 12:51:37 -0300901
902 return -ENODEV;
903}
904
905static struct usb_driver dib0700_driver = {
906 .name = "dvb_usb_dib0700",
907 .probe = dib0700_probe,
908 .disconnect = dvb_usb_device_exit,
909 .id_table = dib0700_usb_id_table,
910};
911
Greg Kroah-Hartmanecb3b2b2011-11-18 09:46:12 -0800912module_usb_driver(dib0700_driver);
Patrick Boettcherb7f54912006-09-19 12:51:37 -0300913
Patrick Boettcher68dc8bc2012-01-17 08:41:20 -0300914MODULE_FIRMWARE("dvb-usb-dib0700-1.20.fw");
Patrick Boettcher99e44da2016-01-24 12:56:58 -0200915MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@posteo.de>");
Patrick Boettcherb7f54912006-09-19 12:51:37 -0300916MODULE_DESCRIPTION("Driver for devices based on DiBcom DiB0700 - USB bridge");
917MODULE_VERSION("1.0");
918MODULE_LICENSE("GPL");