1 /* $NetBSD: emdtv_dtv.c,v 1.14 2020/03/14 02:35:33 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 2008, 2011 Jared D. McNeill <jmcneill@invisible.ca> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __KERNEL_RCSID(0, "$NetBSD: emdtv_dtv.c,v 1.14 2020/03/14 02:35:33 christos Exp $"); 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/device.h> 35 #include <sys/lwp.h> 36 #include <sys/conf.h> 37 38 #include <dev/usb/usb.h> 39 #include <dev/usb/usbdi.h> 40 #include <dev/usb/usbdi_util.h> 41 #include <dev/usb/usbdivar.h> 42 #include <dev/usb/usbdevs.h> 43 44 #include <dev/i2c/i2cvar.h> 45 46 #include <dev/usb/emdtvvar.h> 47 #include <dev/usb/emdtvreg.h> 48 49 static void emdtv_dtv_get_devinfo(void *, 50 struct dvb_frontend_info *); 51 static int emdtv_dtv_open(void *, int); 52 static void emdtv_dtv_close(void *); 53 static int emdtv_dtv_set_tuner(void *, 54 const struct dvb_frontend_parameters *); 55 static fe_status_t emdtv_dtv_get_status(void *); 56 static uint16_t emdtv_dtv_get_signal_strength(void *); 57 static uint16_t emdtv_dtv_get_snr(void *); 58 static int emdtv_dtv_start_transfer(void *, 59 void (*)(void *, const struct dtv_payload *), 60 void *); 61 static int emdtv_dtv_stop_transfer(void *); 62 63 static int emdtv_dtv_tuner_reset(void *); 64 65 static void emdtv_dtv_isoc_startall(struct emdtv_softc *); 66 static int emdtv_dtv_isoc_start(struct emdtv_softc *, 67 struct emdtv_isoc_xfer *); 68 static void emdtv_dtv_isoc(struct usbd_xfer *, void *, 69 usbd_status); 70 71 static const struct dtv_hw_if emdtv_dtv_if = { 72 .get_devinfo = emdtv_dtv_get_devinfo, 73 .open = emdtv_dtv_open, 74 .close = emdtv_dtv_close, 75 .set_tuner = emdtv_dtv_set_tuner, 76 .get_status = emdtv_dtv_get_status, 77 .get_signal_strength = emdtv_dtv_get_signal_strength, 78 .get_snr = emdtv_dtv_get_snr, 79 .start_transfer = emdtv_dtv_start_transfer, 80 .stop_transfer = emdtv_dtv_stop_transfer, 81 }; 82 83 void 84 emdtv_dtv_attach(struct emdtv_softc *sc) 85 { 86 usb_endpoint_descriptor_t *ed; 87 usbd_status status; 88 int i; 89 90 for (i = 0; i < EMDTV_NXFERS; i++) { 91 sc->sc_ix[i].ix_altix = (i & 1) ? 92 &sc->sc_ix[i - 1] : &sc->sc_ix[i + 1]; 93 sc->sc_ix[i].ix_sc = sc; 94 } 95 96 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, 3); 97 if (ed == NULL) { 98 aprint_error_dev(sc->sc_dev, "couldn't find endpoint 3\n"); 99 return; 100 } 101 sc->sc_isoc_maxpacketsize = UGETW(ed->wMaxPacketSize); 102 sc->sc_isoc_buflen = sc->sc_isoc_maxpacketsize * EMDTV_NFRAMES; 103 104 aprint_debug_dev(sc->sc_dev, "calling usbd_open_pipe, ep 0x%02x\n", 105 ed->bEndpointAddress); 106 status = usbd_open_pipe(sc->sc_iface, 107 ed->bEndpointAddress, USBD_EXCLUSIVE_USE|USBD_MPSAFE, 108 &sc->sc_isoc_pipe); 109 if (status != USBD_NORMAL_COMPLETION) { 110 aprint_error_dev(sc->sc_dev, "couldn't open isoc pipe\n"); 111 usbd_set_interface(sc->sc_iface, 0); 112 return; 113 } 114 115 emdtv_write_1(sc, UR_GET_STATUS, 0x48, 0x00); 116 emdtv_write_1(sc, UR_GET_STATUS, 0x12, 0x77); 117 usbd_delay_ms(sc->sc_udev, 6); 118 119 emdtv_gpio_ctl(sc, EMDTV_GPIO_ANALOG_ON, false); 120 emdtv_gpio_ctl(sc, EMDTV_GPIO_TS1_ON, true); 121 emdtv_gpio_ctl(sc, EMDTV_GPIO_TUNER1_ON, true); 122 emdtv_gpio_ctl(sc, EMDTV_GPIO_DEMOD1_RESET, true); 123 usbd_delay_ms(sc->sc_udev, 100); 124 125 emdtv_dtv_rescan(sc, NULL, NULL); 126 } 127 128 static void 129 emdtv_dtv_free_xfers(struct emdtv_softc *sc) 130 { 131 132 for (size_t i = 0; i < EMDTV_NXFERS; i++) 133 if (sc->sc_ix[i].ix_xfer) { 134 usbd_destroy_xfer(sc->sc_ix[i].ix_xfer); 135 sc->sc_ix[i].ix_xfer = NULL; 136 sc->sc_ix[i].ix_buf = NULL; 137 } 138 139 return; 140 } 141 142 void 143 emdtv_dtv_detach(struct emdtv_softc *sc, int flags) 144 { 145 sc->sc_streaming = false; 146 147 if (sc->sc_dtvdev != NULL) { 148 config_detach(sc->sc_dtvdev, flags); 149 sc->sc_dtvdev = NULL; 150 } 151 152 if (sc->sc_xc3028) 153 xc3028_close(sc->sc_xc3028); 154 if (sc->sc_lg3303) 155 lg3303_close(sc->sc_lg3303); 156 157 if (sc->sc_isoc_pipe) { 158 usbd_abort_pipe(sc->sc_isoc_pipe); 159 emdtv_dtv_free_xfers(sc); 160 usbd_close_pipe(sc->sc_isoc_pipe); 161 sc->sc_isoc_pipe = NULL; 162 } 163 } 164 165 void 166 emdtv_dtv_rescan(struct emdtv_softc *sc, const char *ifattr, const int *locs) 167 { 168 struct dtv_attach_args daa; 169 170 daa.hw = &emdtv_dtv_if; 171 daa.priv = sc; 172 173 if (ifattr_match(ifattr, "dtvbus") && sc->sc_dtvdev == NULL) 174 sc->sc_dtvdev = config_found_ia(sc->sc_dev, "dtvbus", 175 &daa, dtv_print); 176 } 177 178 static void 179 emdtv_dtv_get_devinfo(void *priv, struct dvb_frontend_info *info) 180 { 181 struct emdtv_softc *sc = priv; 182 183 memset(info, 0, sizeof(*info)); 184 strlcpy(info->name, sc->sc_board->eb_name, sizeof(info->name)); 185 info->type = FE_ATSC; 186 info->frequency_min = 54000000; 187 info->frequency_max = 858000000; 188 info->frequency_stepsize = 62500; 189 info->caps = FE_CAN_8VSB; 190 } 191 192 static int 193 emdtv_dtv_open(void *priv, int flags) 194 { 195 struct emdtv_softc *sc = priv; 196 197 if (sc->sc_dying) 198 return ENXIO; 199 200 aprint_debug_dev(sc->sc_dev, "allocating isoc xfers (pktsz %d)\n", 201 sc->sc_isoc_maxpacketsize); 202 203 for (size_t i = 0; i < EMDTV_NXFERS; i++) { 204 int error = usbd_create_xfer(sc->sc_isoc_pipe, 205 sc->sc_isoc_buflen, USBD_SHORT_XFER_OK, EMDTV_NFRAMES, 206 &sc->sc_ix[i].ix_xfer); 207 if (error) 208 return error; 209 sc->sc_ix[i].ix_buf = usbd_get_buffer(sc->sc_ix[i].ix_xfer); 210 aprint_debug_dev(sc->sc_dev, " ix[%zu] xfer %p buf %p\n", 211 i, sc->sc_ix[i].ix_xfer, sc->sc_ix[i].ix_buf); 212 } 213 214 switch (sc->sc_board->eb_tuner) { 215 case EMDTV_TUNER_XC3028: 216 if (sc->sc_xc3028 == NULL) { 217 sc->sc_xc3028 = xc3028_open(sc->sc_dev, 218 &sc->sc_i2c, 0x61 << 1, emdtv_dtv_tuner_reset, sc, 219 XC3028); 220 } 221 if (sc->sc_xc3028 == NULL) { 222 aprint_error_dev(sc->sc_dev, "couldn't open xc3028\n"); 223 return ENXIO; 224 } 225 break; 226 case EMDTV_TUNER_XC3028L: 227 if (sc->sc_xc3028 == NULL) { 228 sc->sc_xc3028 = xc3028_open(sc->sc_dev, 229 &sc->sc_i2c, 0x61 << 1, emdtv_dtv_tuner_reset, sc, 230 XC3028L); 231 } 232 if (sc->sc_xc3028 == NULL) { 233 aprint_error_dev(sc->sc_dev, "couldn't open xc3028l\n"); 234 return ENXIO; 235 } 236 break; 237 default: 238 aprint_error_dev(sc->sc_dev, "unsupported tuner (%d)\n", 239 sc->sc_board->eb_tuner); 240 return EIO; 241 } 242 243 switch (sc->sc_board->eb_demod) { 244 case EMDTV_DEMOD_LG3303: 245 if (sc->sc_lg3303 == NULL) { 246 sc->sc_lg3303 = lg3303_open(sc->sc_dev, 247 &sc->sc_i2c, 0x1c, 0); 248 } 249 if (sc->sc_lg3303 == NULL) { 250 aprint_error_dev(sc->sc_dev, "couldn't open lg3303\n"); 251 return ENXIO; 252 } 253 break; 254 default: 255 aprint_error_dev(sc->sc_dev, "unsupported demod (%d)\n", 256 sc->sc_board->eb_demod); 257 return EIO; 258 } 259 260 return 0; 261 } 262 263 static void 264 emdtv_dtv_close(void *priv) 265 { 266 struct emdtv_softc *sc = priv; 267 268 emdtv_dtv_free_xfers(sc); 269 } 270 271 static int 272 emdtv_dtv_set_tuner(void *priv, const struct dvb_frontend_parameters *params) 273 { 274 struct emdtv_softc *sc = priv; 275 int error; 276 277 /* Setup demod */ 278 error = ENXIO; 279 if (sc->sc_lg3303) 280 error = lg3303_set_modulation(sc->sc_lg3303, 281 params->u.vsb.modulation); 282 if (error) 283 return error; 284 285 /* Setup tuner */ 286 error = ENXIO; 287 if (sc->sc_xc3028) 288 error = xc3028_tune_dtv(sc->sc_xc3028, params); 289 290 return error; 291 } 292 293 static fe_status_t 294 emdtv_dtv_get_status(void *priv) 295 { 296 struct emdtv_softc *sc = priv; 297 298 if (sc->sc_lg3303) 299 return lg3303_get_dtv_status(sc->sc_lg3303); 300 301 return 0; 302 } 303 304 uint16_t 305 emdtv_dtv_get_signal_strength(void *priv) 306 { 307 struct emdtv_softc *sc = priv; 308 309 if (sc->sc_lg3303) 310 return lg3303_get_signal_strength(sc->sc_lg3303); 311 312 return 0; 313 } 314 315 uint16_t 316 emdtv_dtv_get_snr(void *priv) 317 { 318 struct emdtv_softc *sc = priv; 319 320 if (sc->sc_lg3303) 321 return lg3303_get_snr(sc->sc_lg3303); 322 323 return 0; 324 } 325 326 static int 327 emdtv_dtv_start_transfer(void *priv, 328 void (*cb)(void *, const struct dtv_payload *), void *arg) 329 { 330 struct emdtv_softc *sc = priv; 331 int s; 332 333 s = splusb(); 334 335 sc->sc_streaming = true; 336 sc->sc_dtvsubmitcb = cb; 337 sc->sc_dtvsubmitarg = arg; 338 339 aprint_debug_dev(sc->sc_dev, "starting isoc transactions\n"); 340 341 emdtv_dtv_isoc_startall(sc); 342 splx(s); 343 344 return 0; 345 } 346 347 static int 348 emdtv_dtv_stop_transfer(void *priv) 349 { 350 struct emdtv_softc *sc = priv; 351 352 aprint_debug_dev(sc->sc_dev, "stopping stream\n"); 353 354 sc->sc_streaming = false; 355 356 KERNEL_LOCK(1, curlwp); 357 if (sc->sc_isoc_pipe != NULL) 358 usbd_abort_pipe(sc->sc_isoc_pipe); 359 KERNEL_UNLOCK_ONE(curlwp); 360 361 sc->sc_dtvsubmitcb = NULL; 362 sc->sc_dtvsubmitarg = NULL; 363 364 return 0; 365 } 366 367 static void 368 emdtv_dtv_isoc_startall(struct emdtv_softc *sc) 369 { 370 int i; 371 372 if (sc->sc_streaming == false || sc->sc_dying == true) 373 return; 374 375 for (i = 0; i < EMDTV_NXFERS; i += 2) 376 emdtv_dtv_isoc_start(sc, &sc->sc_ix[i]); 377 } 378 379 static int 380 emdtv_dtv_isoc_start(struct emdtv_softc *sc, struct emdtv_isoc_xfer *ix) 381 { 382 int i; 383 384 if (sc->sc_isoc_pipe == NULL) 385 return EIO; 386 387 for (i = 0; i < EMDTV_NFRAMES; i++) 388 ix->ix_frlengths[i] = sc->sc_isoc_maxpacketsize; 389 390 usbd_setup_isoc_xfer(ix->ix_xfer, 391 ix, 392 ix->ix_frlengths, 393 EMDTV_NFRAMES, 394 USBD_SHORT_XFER_OK, 395 emdtv_dtv_isoc); 396 397 KERNEL_LOCK(1, curlwp); 398 usbd_transfer(ix->ix_xfer); 399 KERNEL_UNLOCK_ONE(curlwp); 400 401 return 0; 402 } 403 404 static void 405 emdtv_dtv_isoc(struct usbd_xfer *xfer, void * priv, 406 usbd_status err) 407 { 408 struct emdtv_isoc_xfer *ix = priv; 409 struct emdtv_softc *sc = ix->ix_sc; 410 struct dtv_payload payload; 411 struct usbd_pipe *isoc = sc->sc_isoc_pipe; 412 uint32_t len; 413 uint8_t *buf; 414 int i; 415 416 KASSERT(xfer == ix->ix_xfer); 417 418 if (sc->sc_dying || sc->sc_dtvsubmitcb == NULL) 419 return; 420 421 if (err) { 422 if (err == USBD_STALLED) { 423 usbd_clear_endpoint_stall_async(isoc); 424 goto resched; 425 } 426 return; 427 } 428 429 usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL); 430 431 if (len == 0) 432 goto resched; 433 434 buf = usbd_get_buffer(xfer); 435 if (buf == NULL) 436 goto resched; 437 438 for (i = 0; i < EMDTV_NFRAMES; i++, buf += sc->sc_isoc_maxpacketsize) { 439 if (ix->ix_frlengths[i] == 0) 440 continue; 441 payload.data = buf; 442 payload.size = ix->ix_frlengths[i]; 443 sc->sc_dtvsubmitcb(sc->sc_dtvsubmitarg, &payload); 444 } 445 446 resched: 447 emdtv_dtv_isoc_start(sc, ix->ix_altix); 448 } 449 450 static int 451 emdtv_dtv_tuner_reset(void *opaque) 452 { 453 struct emdtv_softc *sc = opaque; 454 emdtv_gpio_ctl(sc, EMDTV_GPIO_TUNER1_RESET, true); 455 return 0; 456 } 457