auvitek.c revision 1.7.6.1 1 /* $NetBSD: auvitek.c,v 1.7.6.1 2012/02/18 07:35:04 mrg Exp $ */
2
3 /*-
4 * Copyright (c) 2010 Jared D. McNeill <jmcneill (at) 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.7.6.1 2012/02/18 07:35:04 mrg 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 mutex_init(&sc->sc_ab.ab_lock, MUTEX_DEFAULT, IPL_USB);
121 cv_init(&sc->sc_ab.ab_cv, "auvitekbulk");
122
123 err = usbd_set_config_index(dev, 0, 1);
124 if (err) {
125 aprint_error_dev(self, "couldn't set config index: %s\n",
126 usbd_errstr(err));
127 return;
128 }
129 err = usbd_device2interface_handle(dev, 0, &sc->sc_isoc_iface);
130 if (err) {
131 aprint_error_dev(self, "couldn't get interface handle: %s\n",
132 usbd_errstr(err));
133 return;
134 }
135 err = usbd_device2interface_handle(dev, 3, &sc->sc_bulk_iface);
136 if (err) {
137 aprint_error_dev(self, "couldn't get interface handle: %s\n",
138 usbd_errstr(err));
139 return;
140 }
141
142 sc->sc_ax.ax_sc = sc->sc_ab.ab_sc = sc;
143 sc->sc_ax.ax_endpt = sc->sc_ab.ab_endpt = -1;
144
145 err = usbd_set_interface(sc->sc_isoc_iface, AUVITEK_XFER_ALTNO);
146 if (err) {
147 aprint_error_dev(self, "couldn't set interface: %s\n",
148 usbd_errstr(err));
149 return;
150 }
151
152 nep = 0;
153 usbd_endpoint_count(sc->sc_isoc_iface, &nep);
154 for (i = 0; i < nep; i++) {
155 int dir, type;
156
157 ed = usbd_interface2endpoint_descriptor(sc->sc_isoc_iface, i);
158 if (ed == NULL) {
159 aprint_error_dev(self,
160 "couldn't read endpoint descriptor %d\n", i);
161 continue;
162 }
163
164 dir = UE_GET_DIR(ed->bEndpointAddress);
165 type = UE_GET_XFERTYPE(ed->bmAttributes);
166
167 if (dir == UE_DIR_IN && type == UE_ISOCHRONOUS &&
168 sc->sc_ax.ax_endpt == -1) {
169 sc->sc_ax.ax_endpt = ed->bEndpointAddress;
170 sc->sc_ax.ax_maxpktlen =
171 UE_GET_SIZE(UGETW(ed->wMaxPacketSize)) *
172 (UE_GET_TRANS(UGETW(ed->wMaxPacketSize)) + 1);
173 }
174 }
175
176 err = usbd_set_interface(sc->sc_isoc_iface, 0);
177 if (err) {
178 aprint_error_dev(self, "couldn't set interface: %s\n",
179 usbd_errstr(err));
180 return;
181 }
182
183 if (sc->sc_ax.ax_endpt == -1) {
184 aprint_error_dev(self, "couldn't find isoc endpoint\n");
185 sc->sc_dying = 1;
186 return;
187 }
188 if (sc->sc_ax.ax_maxpktlen == 0) {
189 aprint_error_dev(self, "couldn't determine packet length\n");
190 sc->sc_dying = 1;
191 return;
192 }
193
194 aprint_debug_dev(self, "isoc endpoint 0x%02x size %d\n",
195 sc->sc_ax.ax_endpt, sc->sc_ax.ax_maxpktlen);
196
197 nep = 0;
198 usbd_endpoint_count(sc->sc_bulk_iface, &nep);
199 for (i = 0; i < nep; i++) {
200 int dir, type;
201
202 ed = usbd_interface2endpoint_descriptor(sc->sc_bulk_iface, i);
203 if (ed == NULL) {
204 aprint_error_dev(self,
205 "couldn't read endpoint descriptor %d\n", i);
206 continue;
207 }
208
209 dir = UE_GET_DIR(ed->bEndpointAddress);
210 type = UE_GET_XFERTYPE(ed->bmAttributes);
211
212 if (dir == UE_DIR_IN && type == UE_BULK &&
213 sc->sc_ab.ab_endpt == -1) {
214 sc->sc_ab.ab_endpt = ed->bEndpointAddress;
215 }
216 }
217
218 if (sc->sc_ab.ab_endpt == -1) {
219 aprint_error_dev(self, "couldn't find bulk endpoint\n");
220 sc->sc_dying = 1;
221 return;
222 }
223
224 for (i = 0; i < AUVITEK_NBULK_XFERS; i++) {
225 sc->sc_ab.ab_bx[i].bx_sc = sc;
226 sc->sc_ab.ab_bx[i].bx_xfer = usbd_alloc_xfer(sc->sc_udev);
227 if (sc->sc_ab.ab_bx[i].bx_xfer == NULL) {
228 aprint_error_dev(self, "couldn't allocate xfer\n");
229 sc->sc_dying = 1;
230 return;
231 }
232 sc->sc_ab.ab_bx[i].bx_buffer = usbd_alloc_buffer(
233 sc->sc_ab.ab_bx[i].bx_xfer, AUVITEK_BULK_BUFLEN);
234 if (sc->sc_ab.ab_bx[i].bx_buffer == NULL) {
235 aprint_error_dev(self,
236 "couldn't allocate xfer buffer\n");
237 sc->sc_dying = 1;
238 return;
239 }
240 }
241
242 aprint_debug_dev(self, "bulk endpoint 0x%02x size %d\n",
243 sc->sc_ab.ab_endpt, AUVITEK_BULK_BUFLEN);
244
245 auvitek_board_init(sc);
246
247 auvitek_i2c_attach(sc);
248
249 sc->sc_au8522 = au8522_open(self, &sc->sc_i2c, 0x8e >> 1,
250 auvitek_board_get_if_frequency(sc));
251 if (sc->sc_au8522 == NULL) {
252 aprint_error_dev(sc->sc_dev, "couldn't initialize decoder\n");
253 sc->sc_dying = 1;
254 return;
255 }
256
257 config_mountroot(self, auvitek_attach_tuner);
258
259 auvitek_video_attach(sc);
260 auvitek_audio_attach(sc);
261 auvitek_dtv_attach(sc);
262 }
263
264 void
265 auvitek_attach_tuner(device_t self)
266 {
267 struct auvitek_softc *sc = device_private(self);
268
269 mutex_enter(&sc->sc_subdev_lock);
270 if (sc->sc_xc5k == NULL) {
271 sc->sc_xc5k = xc5k_open(sc->sc_dev, &sc->sc_i2c, 0xc2 >> 1,
272 auvitek_board_tuner_reset, sc,
273 auvitek_board_get_if_frequency(sc),
274 FE_ATSC);
275 }
276 mutex_exit(&sc->sc_subdev_lock);
277 }
278
279 static int
280 auvitek_detach(device_t self, int flags)
281 {
282 struct auvitek_softc *sc = device_private(self);
283 unsigned int i;
284
285 sc->sc_dying = 1;
286
287 pmf_device_deregister(self);
288
289 auvitek_dtv_detach(sc, flags);
290 auvitek_audio_detach(sc, flags);
291 auvitek_video_detach(sc, flags);
292
293 if (sc->sc_xc5k)
294 xc5k_close(sc->sc_xc5k);
295 if (sc->sc_au8522)
296 au8522_close(sc->sc_au8522);
297
298 auvitek_i2c_detach(sc, flags);
299
300 mutex_destroy(&sc->sc_subdev_lock);
301
302 for (i = 0; i < AUVITEK_NBULK_XFERS; i++) {
303 if (sc->sc_ab.ab_bx[i].bx_xfer)
304 usbd_free_xfer(sc->sc_ab.ab_bx[i].bx_xfer);
305 }
306 cv_destroy(&sc->sc_ab.ab_cv);
307 mutex_destroy(&sc->sc_ab.ab_lock);
308
309 return 0;
310 }
311
312 int
313 auvitek_activate(device_t self, enum devact act)
314 {
315 struct auvitek_softc *sc = device_private(self);
316
317 switch (act) {
318 case DVACT_DEACTIVATE:
319 sc->sc_dying = 1;
320 return 0;
321 default:
322 return 0;
323 }
324 }
325
326 static int
327 auvitek_rescan(device_t self, const char *ifattr, const int *locs)
328 {
329 struct auvitek_softc *sc = device_private(self);
330
331 auvitek_video_rescan(sc, ifattr, locs);
332 auvitek_dtv_rescan(sc, ifattr, locs);
333 auvitek_i2c_rescan(sc, ifattr, locs);
334
335 return 0;
336 }
337
338 static void
339 auvitek_childdet(device_t self, device_t child)
340 {
341 struct auvitek_softc *sc = device_private(self);
342
343 auvitek_video_childdet(sc, child);
344 auvitek_audio_childdet(sc, child);
345 auvitek_dtv_childdet(sc, child);
346 }
347
348 uint8_t
349 auvitek_read_1(struct auvitek_softc *sc, uint16_t reg)
350 {
351 usb_device_request_t req;
352 usbd_status err;
353 int actlen;
354 uint8_t data;
355
356 req.bmRequestType = UT_READ_VENDOR_DEVICE;
357 req.bRequest = AU0828_CMD_REQUEST_IN;
358 USETW(req.wValue, 0);
359 USETW(req.wIndex, reg);
360 USETW(req.wLength, sizeof(data));
361
362 KERNEL_LOCK(1, curlwp);
363 err = usbd_do_request_flags(sc->sc_udev, &req, &data, 0,
364 &actlen, USBD_DEFAULT_TIMEOUT);
365 KERNEL_UNLOCK_ONE(curlwp);
366
367 if (err)
368 printf("%s: read failed: %s\n", device_xname(sc->sc_dev),
369 usbd_errstr(err));
370
371 return data;
372 }
373
374 void
375 auvitek_write_1(struct auvitek_softc *sc, uint16_t reg, uint8_t data)
376 {
377 usb_device_request_t req;
378 usbd_status err;
379 int actlen;
380
381 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
382 req.bRequest = AU0828_CMD_REQUEST_OUT;
383 USETW(req.wValue, data);
384 USETW(req.wIndex, reg);
385 USETW(req.wLength, 0);
386
387 KERNEL_LOCK(1, curlwp);
388 err = usbd_do_request_flags(sc->sc_udev, &req, NULL, 0,
389 &actlen, USBD_DEFAULT_TIMEOUT);
390 KERNEL_UNLOCK_ONE(curlwp);
391
392 if (err)
393 printf("%s: write failed: %s\n", device_xname(sc->sc_dev),
394 usbd_errstr(err));
395 }
396
397 MODULE(MODULE_CLASS_DRIVER, auvitek, "au8522,xc5k");
398
399 #ifdef _MODULE
400 #include "ioconf.c"
401 #endif
402
403 static int
404 auvitek_modcmd(modcmd_t cmd, void *opaque)
405 {
406 switch (cmd) {
407 case MODULE_CMD_INIT:
408 #ifdef _MODULE
409 return config_init_component(cfdriver_ioconf_auvitek,
410 cfattach_ioconf_auvitek, cfdata_ioconf_auvitek);
411 #else
412 return 0;
413 #endif
414 case MODULE_CMD_FINI:
415 #ifdef _MODULE
416 return config_fini_component(cfdriver_ioconf_auvitek,
417 cfattach_ioconf_auvitek, cfdata_ioconf_auvitek);
418 #else
419 return 0;
420 #endif
421 default:
422 return ENOTTY;
423 }
424 }
425