xref: /netbsd-src/sys/dev/usb/auvitek.c (revision c2f76ff004a2cb67efe5b12d97bd3ef7fe89e18d)
1 /* $NetBSD: auvitek.c,v 1.3 2010/12/28 04:02:33 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.3 2010/12/28 04:02:33 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_VM);
119 
120 	err = usbd_set_config_index(dev, 0, 1);
121 	if (err) {
122 		aprint_error_dev(self, "couldn't set config index: %s\n",
123 		    usbd_errstr(err));
124 		return;
125 	}
126 	err = usbd_device2interface_handle(dev, 0, &sc->sc_iface);
127 	if (err) {
128 		aprint_error_dev(self, "couldn't get interface handle: %s\n",
129 		    usbd_errstr(err));
130 		return;
131 	}
132 
133 	err = usbd_set_interface(sc->sc_iface, AUVITEK_XFER_ALTNO);
134 	if (err) {
135 		aprint_error_dev(self, "couldn't set interface: %s\n",
136 		    usbd_errstr(err));
137 		return;
138 	}
139 
140 	nep = 0;
141 	usbd_endpoint_count(sc->sc_iface, &nep);
142 	sc->sc_ax.ax_sc = sc;
143 	sc->sc_ax.ax_endpt = -1;
144 	for (i = 0; i < nep; i++) {
145 		int dir, type;
146 
147 		ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
148 		if (ed == NULL) {
149 			aprint_error_dev(self,
150 			    "couldn't read endpoint descriptor %d\n", i);
151 			continue;
152 		}
153 
154 		dir = UE_GET_DIR(ed->bEndpointAddress);
155 		type = UE_GET_XFERTYPE(ed->bmAttributes);
156 
157 		if (dir == UE_DIR_IN && type == UE_ISOCHRONOUS) {
158 			sc->sc_ax.ax_endpt = ed->bEndpointAddress;
159 			sc->sc_ax.ax_maxpktlen =
160 			    UE_GET_SIZE(UGETW(ed->wMaxPacketSize)) *
161 			    (UE_GET_TRANS(UGETW(ed->wMaxPacketSize)) + 1);
162 			break;
163 		}
164 	}
165 	err = usbd_set_interface(sc->sc_iface, 0);
166 	if (err) {
167 		aprint_error_dev(self, "couldn't set interface: %s\n",
168 		    usbd_errstr(err));
169 		sc->sc_dying = 1;
170 		return;
171 	}
172 	if (sc->sc_ax.ax_endpt == -1) {
173 		aprint_error_dev(self, "couldn't find isoc endpoint\n");
174 		sc->sc_dying = 1;
175 		return;
176 	}
177 	if (sc->sc_ax.ax_maxpktlen == 0) {
178 		aprint_error_dev(self, "couldn't determine packet length\n");
179 		sc->sc_dying = 1;
180 		return;
181 	}
182 
183 	aprint_debug_dev(self, "isoc endpoint 0x%02x size %d\n",
184 	    sc->sc_ax.ax_endpt, sc->sc_ax.ax_maxpktlen);
185 
186 	auvitek_board_init(sc);
187 
188 	auvitek_i2c_attach(sc);
189 
190 	sc->sc_au8522 = au8522_open(self, &sc->sc_i2c, 0x8e >> 1);
191 	if (sc->sc_au8522 == NULL) {
192 		aprint_error_dev(sc->sc_dev, "couldn't initialize decoder\n");
193 		sc->sc_dying = 1;
194 		return;
195 	}
196 
197 	auvitek_video_attach(sc);
198 	auvitek_audio_attach(sc);
199 }
200 
201 static int
202 auvitek_detach(device_t self, int flags)
203 {
204 	struct auvitek_softc *sc = device_private(self);
205 
206 	sc->sc_dying = 1;
207 
208 	pmf_device_deregister(self);
209 
210 	auvitek_audio_detach(sc, flags);
211 	auvitek_video_detach(sc, flags);
212 
213 	if (sc->sc_xc5k)
214 		xc5k_close(sc->sc_xc5k);
215 	if (sc->sc_au8522)
216 		au8522_close(sc->sc_au8522);
217 
218 	auvitek_i2c_detach(sc, flags);
219 
220 	mutex_destroy(&sc->sc_subdev_lock);
221 
222 	return 0;
223 }
224 
225 int
226 auvitek_activate(device_t self, enum devact act)
227 {
228 	struct auvitek_softc *sc = device_private(self);
229 
230 	switch (act) {
231 	case DVACT_DEACTIVATE:
232 		sc->sc_dying = 1;
233 		return 0;
234 	default:
235 		return 0;
236 	}
237 }
238 
239 static void
240 auvitek_childdet(device_t self, device_t child)
241 {
242 	struct auvitek_softc *sc = device_private(self);
243 
244 	auvitek_video_childdet(sc, child);
245 }
246 
247 uint8_t
248 auvitek_read_1(struct auvitek_softc *sc, uint16_t reg)
249 {
250 	usb_device_request_t req;
251 	usbd_status err;
252 	int actlen;
253 	uint8_t data;
254 
255 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
256 	req.bRequest = AU0828_CMD_REQUEST_IN;
257 	USETW(req.wValue, 0);
258 	USETW(req.wIndex, reg);
259 	USETW(req.wLength, sizeof(data));
260 
261 	err = usbd_do_request_flags(sc->sc_udev, &req, &data, 0,
262 	    &actlen, USBD_DEFAULT_TIMEOUT);
263 	if (err)
264 		printf("%s: read failed: %s\n", device_xname(sc->sc_dev),
265 		    usbd_errstr(err));
266 
267 	return data;
268 }
269 
270 void
271 auvitek_write_1(struct auvitek_softc *sc, uint16_t reg, uint8_t data)
272 {
273 	usb_device_request_t req;
274 	usbd_status err;
275 	int actlen;
276 
277 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
278 	req.bRequest = AU0828_CMD_REQUEST_OUT;
279 	USETW(req.wValue, data);
280 	USETW(req.wIndex, reg);
281 	USETW(req.wLength, 0);
282 
283 	err = usbd_do_request_flags(sc->sc_udev, &req, NULL, 0,
284 	    &actlen, USBD_DEFAULT_TIMEOUT);
285 	if (err)
286 		printf("%s: write failed: %s\n", device_xname(sc->sc_dev),
287 		    usbd_errstr(err));
288 }
289 
290 MODULE(MODULE_CLASS_DRIVER, auvitek, "au8522,xc5k");
291 
292 #ifdef _MODULE
293 #include "ioconf.c"
294 #endif
295 
296 static int
297 auvitek_modcmd(modcmd_t cmd, void *opaque)
298 {
299 	switch (cmd) {
300 	case MODULE_CMD_INIT:
301 #ifdef _MODULE
302 		return config_init_component(cfdriver_ioconf_auvitek,
303 		    cfattach_ioconf_auvitek, cfdata_ioconf_auvitek);
304 #else
305 		return 0;
306 #endif
307 	case MODULE_CMD_FINI:
308 #ifdef _MODULE
309 		return config_fini_component(cfdriver_ioconf_auvitek,
310 		    cfattach_ioconf_auvitek, cfdata_ioconf_auvitek);
311 #else
312 		return 0;
313 #endif
314 	default:
315 		return ENOTTY;
316 	}
317 }
318