Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

[media] dvbsky: new driver to support DVBSky S860/S960 devices

Support for DVBSky dvb-s2 usb: add dvb-usb-v2 driver for DVBSky dvb-s2
box, no ci support yet.

Signed-off-by: Nibble Max <nibble.max@gmail.com>
Reviewed-by: Antti Palosaari <crope@iki.fi>
Signed-off-by: Mauro Carvalho Chehab <mchehab@osg.samsung.com>

authored by

nibble.max and committed by
Mauro Carvalho Chehab
af64fb3f 8db3e5df

+470
+7
drivers/media/usb/dvb-usb-v2/Kconfig
··· 141 141 help 142 142 Say Y here to support the Realtek RTL28xxU DVB USB receiver. 143 143 144 + config DVB_USB_DVBSKY 145 + tristate "DVBSky USB support" 146 + depends on DVB_USB_V2 147 + select DVB_M88DS3103 if MEDIA_SUBDRV_AUTOSELECT 148 + select MEDIA_TUNER_M88TS2022 if MEDIA_SUBDRV_AUTOSELECT 149 + help 150 + Say Y here to support the USB receivers from DVBSky.
+3
drivers/media/usb/dvb-usb-v2/Makefile
··· 37 37 dvb-usb-rtl28xxu-objs := rtl28xxu.o 38 38 obj-$(CONFIG_DVB_USB_RTL28XXU) += dvb-usb-rtl28xxu.o 39 39 40 + dvb-usb-dvbsky-objs := dvbsky.o 41 + obj-$(CONFIG_DVB_USB_DVBSKY) += dvb-usb-dvbsky.o 42 + 40 43 ccflags-y += -I$(srctree)/drivers/media/dvb-core 41 44 ccflags-y += -I$(srctree)/drivers/media/dvb-frontends 42 45 ccflags-y += -I$(srctree)/drivers/media/tuners
+460
drivers/media/usb/dvb-usb-v2/dvbsky.c
··· 1 + /* 2 + * Driver for DVBSky USB2.0 receiver 3 + * 4 + * Copyright (C) 2013 Max nibble <nibble.max@gmail.com> 5 + * 6 + * This program is free software; you can redistribute it and/or modify 7 + * it under the terms of the GNU General Public License as published by 8 + * the Free Software Foundation; either version 2 of the License, or 9 + * (at your option) any later version. 10 + * 11 + * This program is distributed in the hope that it will be useful, 12 + * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 + * GNU General Public License for more details. 15 + * 16 + * You should have received a copy of the GNU General Public License 17 + * along with this program; if not, write to the Free Software 18 + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 19 + */ 20 + 21 + #include "dvb_usb.h" 22 + #include "m88ds3103.h" 23 + #include "m88ts2022.h" 24 + 25 + #define DVBSKY_MSG_DELAY 0/*2000*/ 26 + #define DVBSKY_BUF_LEN 64 27 + 28 + DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr); 29 + 30 + struct dvbsky_state { 31 + struct mutex stream_mutex; 32 + u8 ibuf[DVBSKY_BUF_LEN]; 33 + u8 obuf[DVBSKY_BUF_LEN]; 34 + u8 last_lock; 35 + struct i2c_client *i2c_client_tuner; 36 + 37 + /* fe hook functions*/ 38 + int (*fe_set_voltage)(struct dvb_frontend *fe, 39 + fe_sec_voltage_t voltage); 40 + int (*fe_read_status)(struct dvb_frontend *fe, 41 + fe_status_t *status); 42 + }; 43 + 44 + static int dvbsky_usb_generic_rw(struct dvb_usb_device *d, 45 + u8 *wbuf, u16 wlen, u8 *rbuf, u16 rlen) 46 + { 47 + int ret; 48 + struct dvbsky_state *state = d_to_priv(d); 49 + 50 + mutex_lock(&d->usb_mutex); 51 + if (wlen != 0) 52 + memcpy(state->obuf, wbuf, wlen); 53 + 54 + ret = dvb_usbv2_generic_rw_locked(d, state->obuf, wlen, 55 + state->ibuf, rlen); 56 + 57 + if (!ret && (rlen != 0)) 58 + memcpy(rbuf, state->ibuf, rlen); 59 + 60 + mutex_unlock(&d->usb_mutex); 61 + return ret; 62 + } 63 + 64 + static int dvbsky_stream_ctrl(struct dvb_usb_device *d, u8 onoff) 65 + { 66 + struct dvbsky_state *state = d_to_priv(d); 67 + int ret; 68 + u8 obuf_pre[3] = { 0x37, 0, 0 }; 69 + u8 obuf_post[3] = { 0x36, 3, 0 }; 70 + 71 + mutex_lock(&state->stream_mutex); 72 + ret = dvbsky_usb_generic_rw(d, obuf_pre, 3, NULL, 0); 73 + if (!ret && onoff) { 74 + msleep(20); 75 + ret = dvbsky_usb_generic_rw(d, obuf_post, 3, NULL, 0); 76 + } 77 + mutex_unlock(&state->stream_mutex); 78 + return ret; 79 + } 80 + 81 + static int dvbsky_streaming_ctrl(struct dvb_frontend *fe, int onoff) 82 + { 83 + struct dvb_usb_device *d = fe_to_d(fe); 84 + 85 + return dvbsky_stream_ctrl(d, (onoff == 0) ? 0 : 1); 86 + } 87 + 88 + /* GPIO */ 89 + static int dvbsky_gpio_ctrl(struct dvb_usb_device *d, u8 gport, u8 value) 90 + { 91 + int ret; 92 + u8 obuf[3], ibuf[2]; 93 + 94 + obuf[0] = 0x0e; 95 + obuf[1] = gport; 96 + obuf[2] = value; 97 + ret = dvbsky_usb_generic_rw(d, obuf, 3, ibuf, 1); 98 + if (ret) 99 + dev_err(&d->udev->dev, "%s: %s() failed=%d\n", 100 + KBUILD_MODNAME, __func__, ret); 101 + return ret; 102 + } 103 + 104 + /* I2C */ 105 + static int dvbsky_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[], 106 + int num) 107 + { 108 + struct dvb_usb_device *d = i2c_get_adapdata(adap); 109 + int ret = 0; 110 + u8 ibuf[64], obuf[64]; 111 + 112 + if (mutex_lock_interruptible(&d->i2c_mutex) < 0) 113 + return -EAGAIN; 114 + 115 + if (num > 2) { 116 + dev_err(&d->udev->dev, 117 + "dvbsky_usb: too many i2c messages[%d] than 2.", num); 118 + ret = -EOPNOTSUPP; 119 + goto i2c_error; 120 + } 121 + 122 + if (num == 1) { 123 + if (msg[0].len > 60) { 124 + dev_err(&d->udev->dev, 125 + "dvbsky_usb: too many i2c bytes[%d] than 60.", 126 + msg[0].len); 127 + ret = -EOPNOTSUPP; 128 + goto i2c_error; 129 + } 130 + if (msg[0].flags & I2C_M_RD) { 131 + /* single read */ 132 + obuf[0] = 0x09; 133 + obuf[1] = 0; 134 + obuf[2] = msg[0].len; 135 + obuf[3] = msg[0].addr; 136 + ret = dvbsky_usb_generic_rw(d, obuf, 4, 137 + ibuf, msg[0].len + 1); 138 + if (ret) 139 + dev_err(&d->udev->dev, "%s: %s() failed=%d\n", 140 + KBUILD_MODNAME, __func__, ret); 141 + if (!ret) 142 + memcpy(msg[0].buf, &ibuf[1], msg[0].len); 143 + } else { 144 + /* write */ 145 + obuf[0] = 0x08; 146 + obuf[1] = msg[0].addr; 147 + obuf[2] = msg[0].len; 148 + memcpy(&obuf[3], msg[0].buf, msg[0].len); 149 + ret = dvbsky_usb_generic_rw(d, obuf, 150 + msg[0].len + 3, ibuf, 1); 151 + if (ret) 152 + dev_err(&d->udev->dev, "%s: %s() failed=%d\n", 153 + KBUILD_MODNAME, __func__, ret); 154 + } 155 + } else { 156 + if ((msg[0].len > 60) || (msg[1].len > 60)) { 157 + dev_err(&d->udev->dev, 158 + "dvbsky_usb: too many i2c bytes[w-%d][r-%d] than 60.", 159 + msg[0].len, msg[1].len); 160 + ret = -EOPNOTSUPP; 161 + goto i2c_error; 162 + } 163 + /* write then read */ 164 + obuf[0] = 0x09; 165 + obuf[1] = msg[0].len; 166 + obuf[2] = msg[1].len; 167 + obuf[3] = msg[0].addr; 168 + memcpy(&obuf[4], msg[0].buf, msg[0].len); 169 + ret = dvbsky_usb_generic_rw(d, obuf, 170 + msg[0].len + 4, ibuf, msg[1].len + 1); 171 + if (ret) 172 + dev_err(&d->udev->dev, "%s: %s() failed=%d\n", 173 + KBUILD_MODNAME, __func__, ret); 174 + 175 + if (!ret) 176 + memcpy(msg[1].buf, &ibuf[1], msg[1].len); 177 + } 178 + i2c_error: 179 + mutex_unlock(&d->i2c_mutex); 180 + return (ret) ? ret : num; 181 + } 182 + 183 + static u32 dvbsky_i2c_func(struct i2c_adapter *adapter) 184 + { 185 + return I2C_FUNC_I2C; 186 + } 187 + 188 + static struct i2c_algorithm dvbsky_i2c_algo = { 189 + .master_xfer = dvbsky_i2c_xfer, 190 + .functionality = dvbsky_i2c_func, 191 + }; 192 + 193 + #if IS_ENABLED(CONFIG_RC_CORE) 194 + static int dvbsky_rc_query(struct dvb_usb_device *d) 195 + { 196 + u32 code = 0xffff, scancode; 197 + u8 rc5_command, rc5_system; 198 + u8 obuf[2], ibuf[2], toggle; 199 + int ret; 200 + 201 + obuf[0] = 0x10; 202 + ret = dvbsky_usb_generic_rw(d, obuf, 1, ibuf, 2); 203 + if (ret) 204 + dev_err(&d->udev->dev, "%s: %s() failed=%d\n", 205 + KBUILD_MODNAME, __func__, ret); 206 + if (ret == 0) 207 + code = (ibuf[0] << 8) | ibuf[1]; 208 + if (code != 0xffff) { 209 + dev_dbg(&d->udev->dev, "rc code: %x\n", code); 210 + rc5_command = code & 0x3F; 211 + rc5_system = (code & 0x7C0) >> 6; 212 + toggle = (code & 0x800) ? 1 : 0; 213 + scancode = rc5_system << 8 | rc5_command; 214 + rc_keydown(d->rc_dev, RC_TYPE_RC5, scancode, toggle); 215 + } 216 + return 0; 217 + } 218 + 219 + static int dvbsky_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc) 220 + { 221 + rc->allowed_protos = RC_BIT_RC5; 222 + rc->query = dvbsky_rc_query; 223 + rc->interval = 300; 224 + return 0; 225 + } 226 + #else 227 + #define dvbsky_get_rc_config NULL 228 + #endif 229 + 230 + static int dvbsky_usb_set_voltage(struct dvb_frontend *fe, 231 + fe_sec_voltage_t voltage) 232 + { 233 + struct dvb_usb_device *d = fe_to_d(fe); 234 + struct dvbsky_state *state = d_to_priv(d); 235 + u8 value; 236 + 237 + if (voltage == SEC_VOLTAGE_OFF) 238 + value = 0; 239 + else 240 + value = 1; 241 + dvbsky_gpio_ctrl(d, 0x80, value); 242 + 243 + return state->fe_set_voltage(fe, voltage); 244 + } 245 + 246 + static int dvbsky_read_mac_addr(struct dvb_usb_adapter *adap, u8 mac[6]) 247 + { 248 + struct dvb_usb_device *d = adap_to_d(adap); 249 + u8 obuf[] = { 0x1e, 0x00 }; 250 + u8 ibuf[6] = { 0 }; 251 + struct i2c_msg msg[] = { 252 + { 253 + .addr = 0x51, 254 + .flags = 0, 255 + .buf = obuf, 256 + .len = 2, 257 + }, { 258 + .addr = 0x51, 259 + .flags = I2C_M_RD, 260 + .buf = ibuf, 261 + .len = 6, 262 + } 263 + }; 264 + 265 + if (i2c_transfer(&d->i2c_adap, msg, 2) == 2) 266 + memcpy(mac, ibuf, 6); 267 + 268 + dev_info(&d->udev->dev, "dvbsky_usb MAC address=%pM\n", mac); 269 + 270 + return 0; 271 + } 272 + 273 + static int dvbsky_usb_read_status(struct dvb_frontend *fe, fe_status_t *status) 274 + { 275 + struct dvb_usb_device *d = fe_to_d(fe); 276 + struct dvbsky_state *state = d_to_priv(d); 277 + int ret; 278 + 279 + ret = state->fe_read_status(fe, status); 280 + 281 + /* it need resync slave fifo when signal change from unlock to lock.*/ 282 + if ((*status & FE_HAS_LOCK) && (!state->last_lock)) 283 + dvbsky_stream_ctrl(d, 1); 284 + 285 + state->last_lock = (*status & FE_HAS_LOCK) ? 1 : 0; 286 + return ret; 287 + } 288 + 289 + static const struct m88ds3103_config dvbsky_s960_m88ds3103_config = { 290 + .i2c_addr = 0x68, 291 + .clock = 27000000, 292 + .i2c_wr_max = 33, 293 + .clock_out = 0, 294 + .ts_mode = M88DS3103_TS_CI, 295 + .ts_clk = 16000, 296 + .ts_clk_pol = 0, 297 + .agc = 0x99, 298 + .lnb_hv_pol = 1, 299 + .lnb_en_pol = 1, 300 + }; 301 + 302 + static int dvbsky_s960_attach(struct dvb_usb_adapter *adap) 303 + { 304 + struct dvbsky_state *state = adap_to_priv(adap); 305 + struct dvb_usb_device *d = adap_to_d(adap); 306 + int ret = 0; 307 + /* demod I2C adapter */ 308 + struct i2c_adapter *i2c_adapter; 309 + struct i2c_client *client; 310 + struct i2c_board_info info; 311 + struct m88ts2022_config m88ts2022_config = { 312 + .clock = 27000000, 313 + }; 314 + memset(&info, 0, sizeof(struct i2c_board_info)); 315 + 316 + /* attach demod */ 317 + adap->fe[0] = dvb_attach(m88ds3103_attach, 318 + &dvbsky_s960_m88ds3103_config, 319 + &d->i2c_adap, 320 + &i2c_adapter); 321 + if (!adap->fe[0]) { 322 + dev_err(&d->udev->dev, "dvbsky_s960_attach fail.\n"); 323 + ret = -ENODEV; 324 + goto fail_attach; 325 + } 326 + 327 + /* attach tuner */ 328 + m88ts2022_config.fe = adap->fe[0]; 329 + strlcpy(info.type, "m88ts2022", I2C_NAME_SIZE); 330 + info.addr = 0x60; 331 + info.platform_data = &m88ts2022_config; 332 + request_module("m88ts2022"); 333 + client = i2c_new_device(i2c_adapter, &info); 334 + if (client == NULL || client->dev.driver == NULL) { 335 + dvb_frontend_detach(adap->fe[0]); 336 + ret = -ENODEV; 337 + goto fail_attach; 338 + } 339 + 340 + if (!try_module_get(client->dev.driver->owner)) { 341 + i2c_unregister_device(client); 342 + dvb_frontend_detach(adap->fe[0]); 343 + ret = -ENODEV; 344 + goto fail_attach; 345 + } 346 + 347 + /* delegate signal strength measurement to tuner */ 348 + adap->fe[0]->ops.read_signal_strength = 349 + adap->fe[0]->ops.tuner_ops.get_rf_strength; 350 + 351 + /* hook fe: need to resync the slave fifo when signal locks. */ 352 + state->fe_read_status = adap->fe[0]->ops.read_status; 353 + adap->fe[0]->ops.read_status = dvbsky_usb_read_status; 354 + 355 + /* hook fe: LNB off/on is control by Cypress usb chip. */ 356 + state->fe_set_voltage = adap->fe[0]->ops.set_voltage; 357 + adap->fe[0]->ops.set_voltage = dvbsky_usb_set_voltage; 358 + 359 + state->i2c_client_tuner = client; 360 + 361 + fail_attach: 362 + return ret; 363 + } 364 + 365 + static int dvbsky_identify_state(struct dvb_usb_device *d, const char **name) 366 + { 367 + dvbsky_gpio_ctrl(d, 0x04, 1); 368 + msleep(20); 369 + dvbsky_gpio_ctrl(d, 0x83, 0); 370 + dvbsky_gpio_ctrl(d, 0xc0, 1); 371 + msleep(100); 372 + dvbsky_gpio_ctrl(d, 0x83, 1); 373 + dvbsky_gpio_ctrl(d, 0xc0, 0); 374 + msleep(50); 375 + 376 + return WARM; 377 + } 378 + 379 + static int dvbsky_init(struct dvb_usb_device *d) 380 + { 381 + struct dvbsky_state *state = d_to_priv(d); 382 + 383 + /* use default interface */ 384 + /* 385 + ret = usb_set_interface(d->udev, 0, 0); 386 + if (ret) 387 + return ret; 388 + */ 389 + mutex_init(&state->stream_mutex); 390 + 391 + state->last_lock = 0; 392 + 393 + return 0; 394 + } 395 + 396 + static void dvbsky_exit(struct dvb_usb_device *d) 397 + { 398 + struct dvbsky_state *state = d_to_priv(d); 399 + struct i2c_client *client; 400 + 401 + client = state->i2c_client_tuner; 402 + /* remove I2C tuner */ 403 + if (client) { 404 + module_put(client->dev.driver->owner); 405 + i2c_unregister_device(client); 406 + } 407 + } 408 + 409 + /* DVB USB Driver stuff */ 410 + static struct dvb_usb_device_properties dvbsky_s960_props = { 411 + .driver_name = KBUILD_MODNAME, 412 + .owner = THIS_MODULE, 413 + .adapter_nr = adapter_nr, 414 + .size_of_priv = sizeof(struct dvbsky_state), 415 + 416 + .generic_bulk_ctrl_endpoint = 0x01, 417 + .generic_bulk_ctrl_endpoint_response = 0x81, 418 + .generic_bulk_ctrl_delay = DVBSKY_MSG_DELAY, 419 + 420 + .i2c_algo = &dvbsky_i2c_algo, 421 + .frontend_attach = dvbsky_s960_attach, 422 + .init = dvbsky_init, 423 + .get_rc_config = dvbsky_get_rc_config, 424 + .streaming_ctrl = dvbsky_streaming_ctrl, 425 + .identify_state = dvbsky_identify_state, 426 + .exit = dvbsky_exit, 427 + .read_mac_address = dvbsky_read_mac_addr, 428 + 429 + .num_adapters = 1, 430 + .adapter = { 431 + { 432 + .stream = DVB_USB_STREAM_BULK(0x82, 8, 4096), 433 + } 434 + } 435 + }; 436 + 437 + static const struct usb_device_id dvbsky_id_table[] = { 438 + { DVB_USB_DEVICE(0x0572, 0x6831, 439 + &dvbsky_s960_props, "DVBSky S960/S860", RC_MAP_DVBSKY) }, 440 + { } 441 + }; 442 + MODULE_DEVICE_TABLE(usb, dvbsky_id_table); 443 + 444 + static struct usb_driver dvbsky_usb_driver = { 445 + .name = KBUILD_MODNAME, 446 + .id_table = dvbsky_id_table, 447 + .probe = dvb_usbv2_probe, 448 + .disconnect = dvb_usbv2_disconnect, 449 + .suspend = dvb_usbv2_suspend, 450 + .resume = dvb_usbv2_resume, 451 + .reset_resume = dvb_usbv2_reset_resume, 452 + .no_dynamic_id = 1, 453 + .soft_unbind = 1, 454 + }; 455 + 456 + module_usb_driver(dvbsky_usb_driver); 457 + 458 + MODULE_AUTHOR("Max nibble <nibble.max@gmail.com>"); 459 + MODULE_DESCRIPTION("Driver for DVBSky USB"); 460 + MODULE_LICENSE("GPL");