1 /* $NetBSD: auvitek.c,v 1.9 2014/08/09 13:33:43 jmcneill 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.9 2014/08/09 13:33:43 jmcneill 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 52 static int auvitek_match(device_t, cfdata_t, void *); 53 static void auvitek_attach(device_t, device_t, void *); 54 static int auvitek_detach(device_t, int); 55 static int auvitek_rescan(device_t, const char *, const int *); 56 static void auvitek_childdet(device_t, device_t); 57 static int auvitek_activate(device_t, enum devact); 58 59 CFATTACH_DECL2_NEW(auvitek, sizeof(struct auvitek_softc), 60 auvitek_match, auvitek_attach, auvitek_detach, auvitek_activate, 61 auvitek_rescan, auvitek_childdet); 62 63 static 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 75 static int 76 auvitek_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->vendor && 83 auvitek_devices[i].product == uaa->product) 84 return UMATCH_VENDOR_PRODUCT; 85 } 86 87 return UMATCH_NONE; 88 } 89 90 static void 91 auvitek_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 usbd_device_handle dev = 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->port; 107 108 for (i = 0; i < __arraycount(auvitek_devices); i++) { 109 if (auvitek_devices[i].vendor == uaa->vendor && 110 auvitek_devices[i].product == 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 for (i = 0; i < AUVITEK_NBULK_XFERS; i++) { 223 sc->sc_ab.ab_bx[i].bx_sc = sc; 224 sc->sc_ab.ab_bx[i].bx_xfer = usbd_alloc_xfer(sc->sc_udev); 225 if (sc->sc_ab.ab_bx[i].bx_xfer == NULL) { 226 aprint_error_dev(self, "couldn't allocate xfer\n"); 227 sc->sc_dying = 1; 228 return; 229 } 230 sc->sc_ab.ab_bx[i].bx_buffer = usbd_alloc_buffer( 231 sc->sc_ab.ab_bx[i].bx_xfer, AUVITEK_BULK_BUFLEN); 232 if (sc->sc_ab.ab_bx[i].bx_buffer == NULL) { 233 aprint_error_dev(self, 234 "couldn't allocate xfer buffer\n"); 235 sc->sc_dying = 1; 236 return; 237 } 238 } 239 240 aprint_debug_dev(self, "bulk endpoint 0x%02x size %d\n", 241 sc->sc_ab.ab_endpt, AUVITEK_BULK_BUFLEN); 242 243 auvitek_board_init(sc); 244 245 auvitek_i2c_attach(sc); 246 247 sc->sc_au8522 = au8522_open(self, &sc->sc_i2c, 0x8e >> 1, 248 auvitek_board_get_if_frequency(sc)); 249 if (sc->sc_au8522 == NULL) { 250 aprint_error_dev(sc->sc_dev, "couldn't initialize decoder\n"); 251 sc->sc_dying = 1; 252 return; 253 } 254 255 config_mountroot(self, auvitek_attach_tuner); 256 257 auvitek_video_attach(sc); 258 auvitek_audio_attach(sc); 259 auvitek_dtv_attach(sc); 260 } 261 262 void 263 auvitek_attach_tuner(device_t self) 264 { 265 struct auvitek_softc *sc = device_private(self); 266 267 mutex_enter(&sc->sc_subdev_lock); 268 if (sc->sc_xc5k == NULL) { 269 sc->sc_xc5k = xc5k_open(sc->sc_dev, &sc->sc_i2c, 0xc2 >> 1, 270 auvitek_board_tuner_reset, sc, 271 auvitek_board_get_if_frequency(sc), 272 FE_ATSC); 273 } 274 mutex_exit(&sc->sc_subdev_lock); 275 } 276 277 static int 278 auvitek_detach(device_t self, int flags) 279 { 280 struct auvitek_softc *sc = device_private(self); 281 unsigned int i; 282 283 sc->sc_dying = 1; 284 285 pmf_device_deregister(self); 286 287 auvitek_dtv_detach(sc, flags); 288 auvitek_audio_detach(sc, flags); 289 auvitek_video_detach(sc, flags); 290 291 if (sc->sc_xc5k) 292 xc5k_close(sc->sc_xc5k); 293 if (sc->sc_au8522) 294 au8522_close(sc->sc_au8522); 295 296 auvitek_i2c_detach(sc, flags); 297 298 mutex_destroy(&sc->sc_subdev_lock); 299 300 for (i = 0; i < AUVITEK_NBULK_XFERS; i++) { 301 if (sc->sc_ab.ab_bx[i].bx_xfer) 302 usbd_free_xfer(sc->sc_ab.ab_bx[i].bx_xfer); 303 } 304 305 return 0; 306 } 307 308 int 309 auvitek_activate(device_t self, enum devact act) 310 { 311 struct auvitek_softc *sc = device_private(self); 312 313 switch (act) { 314 case DVACT_DEACTIVATE: 315 sc->sc_dying = 1; 316 return 0; 317 default: 318 return 0; 319 } 320 } 321 322 static int 323 auvitek_rescan(device_t self, const char *ifattr, const int *locs) 324 { 325 struct auvitek_softc *sc = device_private(self); 326 327 auvitek_video_rescan(sc, ifattr, locs); 328 auvitek_dtv_rescan(sc, ifattr, locs); 329 auvitek_i2c_rescan(sc, ifattr, locs); 330 331 return 0; 332 } 333 334 static void 335 auvitek_childdet(device_t self, device_t child) 336 { 337 struct auvitek_softc *sc = device_private(self); 338 339 auvitek_video_childdet(sc, child); 340 auvitek_audio_childdet(sc, child); 341 auvitek_dtv_childdet(sc, child); 342 } 343 344 uint8_t 345 auvitek_read_1(struct auvitek_softc *sc, uint16_t reg) 346 { 347 usb_device_request_t req; 348 usbd_status err; 349 int actlen; 350 uint8_t data; 351 352 req.bmRequestType = UT_READ_VENDOR_DEVICE; 353 req.bRequest = AU0828_CMD_REQUEST_IN; 354 USETW(req.wValue, 0); 355 USETW(req.wIndex, reg); 356 USETW(req.wLength, sizeof(data)); 357 358 KERNEL_LOCK(1, curlwp); 359 err = usbd_do_request_flags(sc->sc_udev, &req, &data, 0, 360 &actlen, USBD_DEFAULT_TIMEOUT); 361 KERNEL_UNLOCK_ONE(curlwp); 362 363 if (err) 364 printf("%s: read failed: %s\n", device_xname(sc->sc_dev), 365 usbd_errstr(err)); 366 367 return data; 368 } 369 370 void 371 auvitek_write_1(struct auvitek_softc *sc, uint16_t reg, uint8_t data) 372 { 373 usb_device_request_t req; 374 usbd_status err; 375 int actlen; 376 377 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 378 req.bRequest = AU0828_CMD_REQUEST_OUT; 379 USETW(req.wValue, data); 380 USETW(req.wIndex, reg); 381 USETW(req.wLength, 0); 382 383 KERNEL_LOCK(1, curlwp); 384 err = usbd_do_request_flags(sc->sc_udev, &req, NULL, 0, 385 &actlen, USBD_DEFAULT_TIMEOUT); 386 KERNEL_UNLOCK_ONE(curlwp); 387 388 if (err) 389 printf("%s: write failed: %s\n", device_xname(sc->sc_dev), 390 usbd_errstr(err)); 391 } 392 393 MODULE(MODULE_CLASS_DRIVER, auvitek, "au8522,xc5k"); 394 395 #ifdef _MODULE 396 #include "ioconf.c" 397 #endif 398 399 static int 400 auvitek_modcmd(modcmd_t cmd, void *opaque) 401 { 402 switch (cmd) { 403 case MODULE_CMD_INIT: 404 #ifdef _MODULE 405 return config_init_component(cfdriver_ioconf_auvitek, 406 cfattach_ioconf_auvitek, cfdata_ioconf_auvitek); 407 #else 408 return 0; 409 #endif 410 case MODULE_CMD_FINI: 411 #ifdef _MODULE 412 return config_fini_component(cfdriver_ioconf_auvitek, 413 cfattach_ioconf_auvitek, cfdata_ioconf_auvitek); 414 #else 415 return 0; 416 #endif 417 default: 418 return ENOTTY; 419 } 420 } 421