1 /* $NetBSD: auvitek.c,v 1.4 2011/07/09 15:00:44 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.4 2011/07/09 15:00:44 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 void auvitek_childdet(device_t, device_t); 56 static int auvitek_activate(device_t, enum devact); 57 58 CFATTACH_DECL2_NEW(auvitek, sizeof(struct auvitek_softc), 59 auvitek_match, auvitek_attach, auvitek_detach, auvitek_activate, 60 NULL, auvitek_childdet); 61 62 static const struct { 63 uint16_t vendor; 64 uint16_t product; 65 const char * name; 66 enum auvitek_board board; 67 } auvitek_devices[] = { 68 { 0x2040, 0x7200, 69 "WinTV HVR-950Q", AUVITEK_BOARD_HVR_950Q }, 70 { 0x2040, 0x7240, 71 "WinTV HVR-850", AUVITEK_BOARD_HVR_850 }, 72 }; 73 74 static int 75 auvitek_match(device_t parent, cfdata_t match, void *opaque) 76 { 77 struct usb_attach_arg *uaa = opaque; 78 unsigned int i; 79 80 for (i = 0; i < __arraycount(auvitek_devices); i++) { 81 if (auvitek_devices[i].vendor == uaa->vendor && 82 auvitek_devices[i].product == uaa->product) 83 return UMATCH_VENDOR_PRODUCT; 84 } 85 86 return UMATCH_NONE; 87 } 88 89 static void 90 auvitek_attach(device_t parent, device_t self, void *opaque) 91 { 92 struct auvitek_softc *sc = device_private(self); 93 struct usb_attach_arg *uaa = opaque; 94 usbd_device_handle dev = uaa->device; 95 usb_endpoint_descriptor_t *ed; 96 usbd_status err; 97 unsigned int i; 98 uint8_t nep; 99 100 aprint_naive("\n"); 101 aprint_normal(": AU0828\n"); 102 103 sc->sc_dev = self; 104 sc->sc_udev = dev; 105 sc->sc_uport = uaa->port; 106 107 for (i = 0; i < __arraycount(auvitek_devices); i++) { 108 if (auvitek_devices[i].vendor == uaa->vendor && 109 auvitek_devices[i].product == uaa->product) 110 break; 111 } 112 KASSERT(i != __arraycount(auvitek_devices)); 113 sc->sc_descr = auvitek_devices[i].name; 114 sc->sc_board = auvitek_devices[i].board; 115 116 sc->sc_dying = sc->sc_running = 0; 117 118 mutex_init(&sc->sc_subdev_lock, MUTEX_DEFAULT, IPL_NONE); 119 mutex_init(&sc->sc_ab.ab_lock, MUTEX_DEFAULT, IPL_USB); 120 cv_init(&sc->sc_ab.ab_cv, "auvitekbulk"); 121 122 err = usbd_set_config_index(dev, 0, 1); 123 if (err) { 124 aprint_error_dev(self, "couldn't set config index: %s\n", 125 usbd_errstr(err)); 126 return; 127 } 128 err = usbd_device2interface_handle(dev, 0, &sc->sc_isoc_iface); 129 if (err) { 130 aprint_error_dev(self, "couldn't get interface handle: %s\n", 131 usbd_errstr(err)); 132 return; 133 } 134 err = usbd_device2interface_handle(dev, 3, &sc->sc_bulk_iface); 135 if (err) { 136 aprint_error_dev(self, "couldn't get interface handle: %s\n", 137 usbd_errstr(err)); 138 return; 139 } 140 141 sc->sc_ax.ax_sc = sc->sc_ab.ab_sc = sc; 142 sc->sc_ax.ax_endpt = sc->sc_ab.ab_endpt = -1; 143 144 err = usbd_set_interface(sc->sc_isoc_iface, AUVITEK_XFER_ALTNO); 145 if (err) { 146 aprint_error_dev(self, "couldn't set interface: %s\n", 147 usbd_errstr(err)); 148 return; 149 } 150 151 nep = 0; 152 usbd_endpoint_count(sc->sc_isoc_iface, &nep); 153 for (i = 0; i < nep; i++) { 154 int dir, type; 155 156 ed = usbd_interface2endpoint_descriptor(sc->sc_isoc_iface, i); 157 if (ed == NULL) { 158 aprint_error_dev(self, 159 "couldn't read endpoint descriptor %d\n", i); 160 continue; 161 } 162 163 dir = UE_GET_DIR(ed->bEndpointAddress); 164 type = UE_GET_XFERTYPE(ed->bmAttributes); 165 166 if (dir == UE_DIR_IN && type == UE_ISOCHRONOUS && 167 sc->sc_ax.ax_endpt == -1) { 168 sc->sc_ax.ax_endpt = ed->bEndpointAddress; 169 sc->sc_ax.ax_maxpktlen = 170 UE_GET_SIZE(UGETW(ed->wMaxPacketSize)) * 171 (UE_GET_TRANS(UGETW(ed->wMaxPacketSize)) + 1); 172 } 173 } 174 175 err = usbd_set_interface(sc->sc_isoc_iface, 0); 176 if (err) { 177 aprint_error_dev(self, "couldn't set interface: %s\n", 178 usbd_errstr(err)); 179 return; 180 } 181 182 if (sc->sc_ax.ax_endpt == -1) { 183 aprint_error_dev(self, "couldn't find isoc endpoint\n"); 184 sc->sc_dying = 1; 185 return; 186 } 187 if (sc->sc_ax.ax_maxpktlen == 0) { 188 aprint_error_dev(self, "couldn't determine packet length\n"); 189 sc->sc_dying = 1; 190 return; 191 } 192 193 aprint_debug_dev(self, "isoc endpoint 0x%02x size %d\n", 194 sc->sc_ax.ax_endpt, sc->sc_ax.ax_maxpktlen); 195 196 nep = 0; 197 usbd_endpoint_count(sc->sc_bulk_iface, &nep); 198 for (i = 0; i < nep; i++) { 199 int dir, type; 200 201 ed = usbd_interface2endpoint_descriptor(sc->sc_bulk_iface, i); 202 if (ed == NULL) { 203 aprint_error_dev(self, 204 "couldn't read endpoint descriptor %d\n", i); 205 continue; 206 } 207 208 dir = UE_GET_DIR(ed->bEndpointAddress); 209 type = UE_GET_XFERTYPE(ed->bmAttributes); 210 211 if (dir == UE_DIR_IN && type == UE_BULK && 212 sc->sc_ab.ab_endpt == -1) { 213 sc->sc_ab.ab_endpt = ed->bEndpointAddress; 214 } 215 } 216 217 if (sc->sc_ab.ab_endpt == -1) { 218 aprint_error_dev(self, "couldn't find bulk endpoint\n"); 219 sc->sc_dying = 1; 220 return; 221 } 222 223 for (i = 0; i < AUVITEK_NBULK_XFERS; i++) { 224 sc->sc_ab.ab_bx[i].bx_sc = sc; 225 sc->sc_ab.ab_bx[i].bx_xfer = usbd_alloc_xfer(sc->sc_udev); 226 if (sc->sc_ab.ab_bx[i].bx_xfer == NULL) { 227 aprint_error_dev(self, "couldn't allocate xfer\n"); 228 sc->sc_dying = 1; 229 return; 230 } 231 sc->sc_ab.ab_bx[i].bx_buffer = usbd_alloc_buffer( 232 sc->sc_ab.ab_bx[i].bx_xfer, AUVITEK_BULK_BUFLEN); 233 if (sc->sc_ab.ab_bx[i].bx_buffer == NULL) { 234 aprint_error_dev(self, 235 "couldn't allocate xfer buffer\n"); 236 sc->sc_dying = 1; 237 return; 238 } 239 } 240 241 aprint_debug_dev(self, "bulk endpoint 0x%02x size %d\n", 242 sc->sc_ab.ab_endpt, AUVITEK_BULK_BUFLEN); 243 244 auvitek_board_init(sc); 245 246 auvitek_i2c_attach(sc); 247 248 sc->sc_au8522 = au8522_open(self, &sc->sc_i2c, 0x8e >> 1, 249 auvitek_board_get_if_frequency(sc)); 250 if (sc->sc_au8522 == NULL) { 251 aprint_error_dev(sc->sc_dev, "couldn't initialize decoder\n"); 252 sc->sc_dying = 1; 253 return; 254 } 255 256 auvitek_video_attach(sc); 257 auvitek_audio_attach(sc); 258 auvitek_dtv_attach(sc); 259 } 260 261 static int 262 auvitek_detach(device_t self, int flags) 263 { 264 struct auvitek_softc *sc = device_private(self); 265 unsigned int i; 266 267 sc->sc_dying = 1; 268 269 pmf_device_deregister(self); 270 271 auvitek_dtv_detach(sc, flags); 272 auvitek_audio_detach(sc, flags); 273 auvitek_video_detach(sc, flags); 274 275 if (sc->sc_xc5k) 276 xc5k_close(sc->sc_xc5k); 277 if (sc->sc_au8522) 278 au8522_close(sc->sc_au8522); 279 280 auvitek_i2c_detach(sc, flags); 281 282 mutex_destroy(&sc->sc_subdev_lock); 283 284 for (i = 0; i < AUVITEK_NBULK_XFERS; i++) { 285 if (sc->sc_ab.ab_bx[i].bx_xfer) 286 usbd_free_xfer(sc->sc_ab.ab_bx[i].bx_xfer); 287 } 288 cv_destroy(&sc->sc_ab.ab_cv); 289 mutex_destroy(&sc->sc_ab.ab_lock); 290 291 return 0; 292 } 293 294 int 295 auvitek_activate(device_t self, enum devact act) 296 { 297 struct auvitek_softc *sc = device_private(self); 298 299 switch (act) { 300 case DVACT_DEACTIVATE: 301 sc->sc_dying = 1; 302 return 0; 303 default: 304 return 0; 305 } 306 } 307 308 static void 309 auvitek_childdet(device_t self, device_t child) 310 { 311 struct auvitek_softc *sc = device_private(self); 312 313 auvitek_video_childdet(sc, child); 314 auvitek_audio_childdet(sc, child); 315 auvitek_dtv_childdet(sc, child); 316 } 317 318 uint8_t 319 auvitek_read_1(struct auvitek_softc *sc, uint16_t reg) 320 { 321 usb_device_request_t req; 322 usbd_status err; 323 int actlen; 324 uint8_t data; 325 326 req.bmRequestType = UT_READ_VENDOR_DEVICE; 327 req.bRequest = AU0828_CMD_REQUEST_IN; 328 USETW(req.wValue, 0); 329 USETW(req.wIndex, reg); 330 USETW(req.wLength, sizeof(data)); 331 332 err = usbd_do_request_flags(sc->sc_udev, &req, &data, 0, 333 &actlen, USBD_DEFAULT_TIMEOUT); 334 if (err) 335 printf("%s: read failed: %s\n", device_xname(sc->sc_dev), 336 usbd_errstr(err)); 337 338 return data; 339 } 340 341 void 342 auvitek_write_1(struct auvitek_softc *sc, uint16_t reg, uint8_t data) 343 { 344 usb_device_request_t req; 345 usbd_status err; 346 int actlen; 347 348 req.bmRequestType = UT_WRITE_VENDOR_DEVICE; 349 req.bRequest = AU0828_CMD_REQUEST_OUT; 350 USETW(req.wValue, data); 351 USETW(req.wIndex, reg); 352 USETW(req.wLength, 0); 353 354 err = usbd_do_request_flags(sc->sc_udev, &req, NULL, 0, 355 &actlen, USBD_DEFAULT_TIMEOUT); 356 if (err) 357 printf("%s: write failed: %s\n", device_xname(sc->sc_dev), 358 usbd_errstr(err)); 359 } 360 361 MODULE(MODULE_CLASS_DRIVER, auvitek, "au8522,xc5k,dtv"); 362 363 #ifdef _MODULE 364 #include "ioconf.c" 365 #endif 366 367 static int 368 auvitek_modcmd(modcmd_t cmd, void *opaque) 369 { 370 switch (cmd) { 371 case MODULE_CMD_INIT: 372 #ifdef _MODULE 373 return config_init_component(cfdriver_ioconf_auvitek, 374 cfattach_ioconf_auvitek, cfdata_ioconf_auvitek); 375 #else 376 return 0; 377 #endif 378 case MODULE_CMD_FINI: 379 #ifdef _MODULE 380 return config_fini_component(cfdriver_ioconf_auvitek, 381 cfattach_ioconf_auvitek, cfdata_ioconf_auvitek); 382 #else 383 return 0; 384 #endif 385 default: 386 return ENOTTY; 387 } 388 } 389