1/* $NetBSD: auvitek.c,v 1.13 2022/03/13 12:49:36 riastradh Exp $ */ 2 3/*- 4 * Copyright (c) 2010 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/* 30 * Auvitek AU0828 USB controller 31 */ 32 33#include <sys/cdefs.h> 34__KERNEL_RCSID(0, "$NetBSD: auvitek.c,v 1.13 2022/03/13 12:49:36 riastradh Exp $"); 35 36#include <sys/types.h> 37#include <sys/param.h> 38#include <sys/systm.h> 39#include <sys/device.h> 40#include <sys/conf.h> 41#include <sys/bus.h> 42#include <sys/module.h> 43 44#include <dev/usb/usb.h> 45#include <dev/usb/usbdi.h> 46#include <dev/usb/usbdi_util.h> 47#include <dev/usb/usbdevs.h> 48 49#include <dev/usb/auvitekreg.h> 50#include <dev/usb/auvitekvar.h> 51 52static int auvitek_match(device_t, cfdata_t, void *); 53static void auvitek_attach(device_t, device_t, void *); 54static int auvitek_detach(device_t, int); 55static int auvitek_rescan(device_t, const char *, const int *); 56static void auvitek_childdet(device_t, device_t); 57static int auvitek_activate(device_t, enum devact); 58 59CFATTACH_DECL2_NEW(auvitek, sizeof(struct auvitek_softc), 60 auvitek_match, auvitek_attach, auvitek_detach, auvitek_activate, 61 auvitek_rescan, auvitek_childdet); 62 63static const struct { 64 uint16_t vendor; 65 uint16_t product; 66 const char * name; 67 enum auvitek_board board; 68} auvitek_devices[] = { 69 { 0x2040, 0x7200, 70 "WinTV HVR-950Q", AUVITEK_BOARD_HVR_950Q }, 71 { 0x2040, 0x7240, 72 "WinTV HVR-850", AUVITEK_BOARD_HVR_850 }, 73}; 74 75static int 76auvitek_match(device_t parent, cfdata_t match, void *opaque) 77{ 78 struct usb_attach_arg *uaa = opaque; 79 unsigned int i; 80 81 for (i = 0; i < __arraycount(auvitek_devices); i++) { 82 if (auvitek_devices[i].vendor == uaa->uaa_vendor && 83 auvitek_devices[i].product == uaa->uaa_product) 84 return UMATCH_VENDOR_PRODUCT; 85 } 86 87 return UMATCH_NONE; 88} 89 90static void 91auvitek_attach(device_t parent, device_t self, void *opaque) 92{ 93 struct auvitek_softc *sc = device_private(self); 94 struct usb_attach_arg *uaa = opaque; 95 struct usbd_device *dev = uaa->uaa_device; 96 usb_endpoint_descriptor_t *ed; 97 usbd_status err; 98 unsigned int i; 99 uint8_t nep; 100 101 aprint_naive("\n"); 102 aprint_normal(": AU0828\n"); 103 104 sc->sc_dev = self; 105 sc->sc_udev = dev; 106 sc->sc_uport = uaa->uaa_port; 107 108 for (i = 0; i < __arraycount(auvitek_devices); i++) { 109 if (auvitek_devices[i].vendor == uaa->uaa_vendor && 110 auvitek_devices[i].product == uaa->uaa_product) 111 break; 112 } 113 KASSERT(i != __arraycount(auvitek_devices)); 114 sc->sc_descr = auvitek_devices[i].name; 115 sc->sc_board = auvitek_devices[i].board; 116 117 sc->sc_dying = sc->sc_running = 0; 118 119 mutex_init(&sc->sc_subdev_lock, MUTEX_DEFAULT, IPL_NONE); 120 121 err = usbd_set_config_index(dev, 0, 1); 122 if (err) { 123 aprint_error_dev(self, "couldn't set config index: %s\n", 124 usbd_errstr(err)); 125 return; 126 } 127 err = usbd_device2interface_handle(dev, 0, &sc->sc_isoc_iface); 128 if (err) { 129 aprint_error_dev(self, "couldn't get interface handle: %s\n", 130 usbd_errstr(err)); 131 return; 132 } 133 err = usbd_device2interface_handle(dev, 3, &sc->sc_bulk_iface); 134 if (err) { 135 aprint_error_dev(self, "couldn't get interface handle: %s\n", 136 usbd_errstr(err)); 137 return; 138 } 139 140 sc->sc_ax.ax_sc = sc->sc_ab.ab_sc = sc; 141 sc->sc_ax.ax_endpt = sc->sc_ab.ab_endpt = -1; 142 143 err = usbd_set_interface(sc->sc_isoc_iface, AUVITEK_XFER_ALTNO); 144 if (err) { 145 aprint_error_dev(self, "couldn't set interface: %s\n", 146 usbd_errstr(err)); 147 return; 148 } 149 150 nep = 0; 151 usbd_endpoint_count(sc->sc_isoc_iface, &nep); 152 for (i = 0; i < nep; i++) { 153 int dir, type; 154 155 ed = usbd_interface2endpoint_descriptor(sc->sc_isoc_iface, i); 156 if (ed == NULL) { 157 aprint_error_dev(self, 158 "couldn't read endpoint descriptor %d\n", i); 159 continue; 160 } 161 162 dir = UE_GET_DIR(ed->bEndpointAddress); 163 type = UE_GET_XFERTYPE(ed->bmAttributes); 164 165 if (dir == UE_DIR_IN && type == UE_ISOCHRONOUS && 166 sc->sc_ax.ax_endpt == -1) { 167 sc->sc_ax.ax_endpt = ed->bEndpointAddress; 168 sc->sc_ax.ax_maxpktlen = 169 UE_GET_SIZE(UGETW(ed->wMaxPacketSize)) * 170 (UE_GET_TRANS(UGETW(ed->wMaxPacketSize)) + 1); 171 } 172 } 173 174 err = usbd_set_interface(sc->sc_isoc_iface, 0); 175 if (err) { 176 aprint_error_dev(self, "couldn't set interface: %s\n", 177 usbd_errstr(err)); 178 return; 179 } 180 181 if (sc->sc_ax.ax_endpt == -1) { 182 aprint_error_dev(self, "couldn't find isoc endpoint\n"); 183 sc->sc_dying = 1; 184 return; 185 } 186 if (sc->sc_ax.ax_maxpktlen == 0) { 187 aprint_error_dev(self, "couldn't determine packet length\n"); 188 sc->sc_dying = 1; 189 return; 190 } 191 192 aprint_debug_dev(self, "isoc endpoint 0x%02x size %d\n", 193 sc->sc_ax.ax_endpt, sc->sc_ax.ax_maxpktlen); 194 195 nep = 0; 196 usbd_endpoint_count(sc->sc_bulk_iface, &nep); 197 for (i = 0; i < nep; i++) { 198 int dir, type; 199 200 ed = usbd_interface2endpoint_descriptor(sc->sc_bulk_iface, i); 201 if (ed == NULL) { 202 aprint_error_dev(self, 203 "couldn't read endpoint descriptor %d\n", i); 204 continue; 205 } 206 207 dir = UE_GET_DIR(ed->bEndpointAddress); 208 type = UE_GET_XFERTYPE(ed->bmAttributes); 209 210 if (dir == UE_DIR_IN && type == UE_BULK && 211 sc->sc_ab.ab_endpt == -1) { 212 sc->sc_ab.ab_endpt = ed->bEndpointAddress; 213 } 214 } 215 216 if (sc->sc_ab.ab_endpt == -1) { 217 aprint_error_dev(self, "couldn't find bulk endpoint\n"); 218 sc->sc_dying = 1; 219 return; 220 } 221 222 aprint_debug_dev(self, "bulk endpoint 0x%02x size %d\n", 223 sc->sc_ab.ab_endpt, AUVITEK_BULK_BUFLEN); 224 225 auvitek_board_init(sc); 226 227 auvitek_i2c_attach(sc); 228 229 sc->sc_au8522 = au8522_open(self, &sc->sc_i2c, 0x8e >> 1, 230 auvitek_board_get_if_frequency(sc)); 231 if (sc->sc_au8522 == NULL) { 232 aprint_error_dev(sc->sc_dev, "couldn't initialize decoder\n"); 233 sc->sc_dying = 1; 234 return; 235 } 236 237 config_mountroot(self, auvitek_attach_tuner); 238 239 auvitek_video_attach(sc); 240 auvitek_audio_attach(sc); 241 auvitek_dtv_attach(sc); 242} 243 244void 245auvitek_attach_tuner(device_t self) 246{ 247 struct auvitek_softc *sc = device_private(self); 248 249 mutex_enter(&sc->sc_subdev_lock); 250 if (sc->sc_xc5k == NULL) { 251 sc->sc_xc5k = xc5k_open(sc->sc_dev, &sc->sc_i2c, 0xc2 >> 1, 252 auvitek_board_tuner_reset, sc, 253 auvitek_board_get_if_frequency(sc), 254 FE_ATSC); 255 } 256 mutex_exit(&sc->sc_subdev_lock); 257} 258 259static int 260auvitek_detach(device_t self, int flags) 261{ 262 struct auvitek_softc *sc = device_private(self); 263 int error; 264 265 sc->sc_dying = 1; 266 267 error = config_detach_children(self, flags); 268 if (error) { 269 /* 270 * XXX Should ask autoconf to block open with 271 * .d_cfdriver until we're done, instead of setting 272 * this and then rolling it back. 273 */ 274 sc->sc_dying = 0; 275 return error; 276 } 277 278 pmf_device_deregister(self); 279 280 auvitek_dtv_detach(sc, flags); 281 auvitek_audio_detach(sc, flags); 282 auvitek_video_detach(sc, flags); 283 284 if (sc->sc_xc5k) 285 xc5k_close(sc->sc_xc5k); 286 if (sc->sc_au8522) 287 au8522_close(sc->sc_au8522); 288 289 auvitek_i2c_detach(sc, flags); 290 291 mutex_destroy(&sc->sc_subdev_lock); 292 293 return 0; 294} 295 296int 297auvitek_activate(device_t self, enum devact act) 298{ 299 struct auvitek_softc *sc = device_private(self); 300 301 switch (act) { 302 case DVACT_DEACTIVATE: 303 sc->sc_dying = 1; 304 return 0; 305 default: 306 return 0; 307 } 308} 309 310static int 311auvitek_rescan(device_t self, const char *ifattr, const int *locs) 312{ 313 struct auvitek_softc *sc = device_private(self); 314 315 auvitek_video_rescan(sc, ifattr, locs); 316 auvitek_dtv_rescan(sc, ifattr, locs); 317 auvitek_i2c_rescan(sc, ifattr, locs); 318 319 return 0; 320} 321 322static void 323auvitek_childdet(device_t self, device_t child) 324{ 325 struct auvitek_softc *sc = device_private(self); 326 327 auvitek_video_childdet(sc, child); 328 auvitek_audio_childdet(sc, child); 329 auvitek_dtv_childdet(sc, child); 330} 331 332uint8_t 333auvitek_read_1(struct auvitek_softc *sc, uint16_t reg) 334{ 335 usb_device_request_t req; 336 usbd_status err; 337 int actlen; 338 uint8_t data; 339 340 req.bmRequestType = UT_READ_VENDOR_DEVICE; 341 req.bRequest = AU0828_CMD_REQUEST_IN; 342 USETW(req.wValue, 0); 343 USETW(req.wIndex, reg); 344 USETW(req.wLength, sizeof(data)); 345 346 KERNEL_LOCK(1, curlwp); 347 err = usbd_do_request_flags(sc->sc_udev, &req, &data, 0, 348 &actlen, USBD_DEFAULT_TIMEOUT); 349 KERNEL_UNLOCK_ONE(curlwp); 350 351 if (err) 352 printf("%s: read failed: %s\n", device_xname(sc->sc_dev), 353 usbd_errstr(err)); 354 355 return data; 356} 357 358void 359auvitek_write_1(struct auvitek_softc *sc, uint16_t reg, uint8_t data) 360{ 361 usb_device_request_t req; 362 usbd_status err; 363 int actlen; 364 365 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 366 req.bRequest = AU0828_CMD_REQUEST_OUT; 367 USETW(req.wValue, data); 368 USETW(req.wIndex, reg); 369 USETW(req.wLength, 0); 370 371 KERNEL_LOCK(1, curlwp); 372 err = usbd_do_request_flags(sc->sc_udev, &req, NULL, 0, 373 &actlen, USBD_DEFAULT_TIMEOUT); 374 KERNEL_UNLOCK_ONE(curlwp); 375 376 if (err) 377 printf("%s: write failed: %s\n", device_xname(sc->sc_dev), 378 usbd_errstr(err)); 379} 380 381MODULE(MODULE_CLASS_DRIVER, auvitek, "au8522,xc5k"); 382 383#ifdef _MODULE 384#include "ioconf.c" 385#endif 386 387static int 388auvitek_modcmd(modcmd_t cmd, void *opaque) 389{ 390 switch (cmd) { 391 case MODULE_CMD_INIT: 392#ifdef _MODULE 393 return config_init_component(cfdriver_ioconf_auvitek, 394 cfattach_ioconf_auvitek, cfdata_ioconf_auvitek); 395#else 396 return 0; 397#endif 398 case MODULE_CMD_FINI: 399#ifdef _MODULE 400 return config_fini_component(cfdriver_ioconf_auvitek, 401 cfattach_ioconf_auvitek, cfdata_ioconf_auvitek); 402#else 403 return 0; 404#endif 405 default: 406 return ENOTTY; 407 } 408} 409