auvitek.c revision 1.5 1 /* $NetBSD: auvitek.c,v 1.5 2011/08/09 01:42:24 jmcneill 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.5 2011/08/09 01:42:24 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 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 auvitek_video_attach(sc);
258 auvitek_audio_attach(sc);
259 auvitek_dtv_attach(sc);
260 }
261
262 static int
263 auvitek_detach(device_t self, int flags)
264 {
265 struct auvitek_softc *sc = device_private(self);
266 unsigned int i;
267
268 sc->sc_dying = 1;
269
270 pmf_device_deregister(self);
271
272 auvitek_dtv_detach(sc, flags);
273 auvitek_audio_detach(sc, flags);
274 auvitek_video_detach(sc, flags);
275
276 if (sc->sc_xc5k)
277 xc5k_close(sc->sc_xc5k);
278 if (sc->sc_au8522)
279 au8522_close(sc->sc_au8522);
280
281 auvitek_i2c_detach(sc, flags);
282
283 mutex_destroy(&sc->sc_subdev_lock);
284
285 for (i = 0; i < AUVITEK_NBULK_XFERS; i++) {
286 if (sc->sc_ab.ab_bx[i].bx_xfer)
287 usbd_free_xfer(sc->sc_ab.ab_bx[i].bx_xfer);
288 }
289 cv_destroy(&sc->sc_ab.ab_cv);
290 mutex_destroy(&sc->sc_ab.ab_lock);
291
292 return 0;
293 }
294
295 int
296 auvitek_activate(device_t self, enum devact act)
297 {
298 struct auvitek_softc *sc = device_private(self);
299
300 switch (act) {
301 case DVACT_DEACTIVATE:
302 sc->sc_dying = 1;
303 return 0;
304 default:
305 return 0;
306 }
307 }
308
309 static int
310 auvitek_rescan(device_t self, const char *ifattr, const int *locs)
311 {
312 struct auvitek_softc *sc = device_private(self);
313
314 auvitek_video_rescan(sc, ifattr, locs);
315 auvitek_dtv_rescan(sc, ifattr, locs);
316
317 return 0;
318 }
319
320 static void
321 auvitek_childdet(device_t self, device_t child)
322 {
323 struct auvitek_softc *sc = device_private(self);
324
325 auvitek_video_childdet(sc, child);
326 auvitek_audio_childdet(sc, child);
327 auvitek_dtv_childdet(sc, child);
328 }
329
330 uint8_t
331 auvitek_read_1(struct auvitek_softc *sc, uint16_t reg)
332 {
333 usb_device_request_t req;
334 usbd_status err;
335 int actlen;
336 uint8_t data;
337
338 req.bmRequestType = UT_READ_VENDOR_DEVICE;
339 req.bRequest = AU0828_CMD_REQUEST_IN;
340 USETW(req.wValue, 0);
341 USETW(req.wIndex, reg);
342 USETW(req.wLength, sizeof(data));
343
344 err = usbd_do_request_flags(sc->sc_udev, &req, &data, 0,
345 &actlen, USBD_DEFAULT_TIMEOUT);
346 if (err)
347 printf("%s: read failed: %s\n", device_xname(sc->sc_dev),
348 usbd_errstr(err));
349
350 return data;
351 }
352
353 void
354 auvitek_write_1(struct auvitek_softc *sc, uint16_t reg, uint8_t data)
355 {
356 usb_device_request_t req;
357 usbd_status err;
358 int actlen;
359
360 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
361 req.bRequest = AU0828_CMD_REQUEST_OUT;
362 USETW(req.wValue, data);
363 USETW(req.wIndex, reg);
364 USETW(req.wLength, 0);
365
366 err = usbd_do_request_flags(sc->sc_udev, &req, NULL, 0,
367 &actlen, USBD_DEFAULT_TIMEOUT);
368 if (err)
369 printf("%s: write failed: %s\n", device_xname(sc->sc_dev),
370 usbd_errstr(err));
371 }
372
373 MODULE(MODULE_CLASS_DRIVER, auvitek, "au8522,xc5k");
374
375 #ifdef _MODULE
376 #include "ioconf.c"
377 #endif
378
379 static int
380 auvitek_modcmd(modcmd_t cmd, void *opaque)
381 {
382 switch (cmd) {
383 case MODULE_CMD_INIT:
384 #ifdef _MODULE
385 return config_init_component(cfdriver_ioconf_auvitek,
386 cfattach_ioconf_auvitek, cfdata_ioconf_auvitek);
387 #else
388 return 0;
389 #endif
390 case MODULE_CMD_FINI:
391 #ifdef _MODULE
392 return config_fini_component(cfdriver_ioconf_auvitek,
393 cfattach_ioconf_auvitek, cfdata_ioconf_auvitek);
394 #else
395 return 0;
396 #endif
397 default:
398 return ENOTTY;
399 }
400 }
401