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