summaryrefslogtreecommitdiff
path: root/sys/dev/usb/auvitek.c
blob: a9ecf0812f754166e0cd85f16485164e41bae38b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/* $NetBSD: auvitek.c,v 1.13 2022/03/13 12:49:36 riastradh Exp $ */

/*-
 * Copyright (c) 2010 Jared D. McNeill <jmcneill@invisible.ca>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * Auvitek AU0828 USB controller
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: auvitek.c,v 1.13 2022/03/13 12:49:36 riastradh Exp $");

#include <sys/types.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/conf.h>
#include <sys/bus.h>
#include <sys/module.h>

#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdi_util.h>
#include <dev/usb/usbdevs.h>

#include <dev/usb/auvitekreg.h>
#include <dev/usb/auvitekvar.h>

static int	auvitek_match(device_t, cfdata_t, void *);
static void	auvitek_attach(device_t, device_t, void *);
static int	auvitek_detach(device_t, int);
static int	auvitek_rescan(device_t, const char *, const int *);
static void	auvitek_childdet(device_t, device_t);
static int	auvitek_activate(device_t, enum devact);

CFATTACH_DECL2_NEW(auvitek, sizeof(struct auvitek_softc),
    auvitek_match, auvitek_attach, auvitek_detach, auvitek_activate,
    auvitek_rescan, auvitek_childdet);

static const struct {
	uint16_t		vendor;
	uint16_t		product;
	const char *		name;
	enum auvitek_board	board;
} auvitek_devices[] = {
	{ 0x2040, 0x7200,
	  "WinTV HVR-950Q", AUVITEK_BOARD_HVR_950Q },
	{ 0x2040, 0x7240,
	  "WinTV HVR-850", AUVITEK_BOARD_HVR_850 },
};

static int
auvitek_match(device_t parent, cfdata_t match, void *opaque)
{
	struct usb_attach_arg *uaa = opaque;
	unsigned int i;

	for (i = 0; i < __arraycount(auvitek_devices); i++) {
		if (auvitek_devices[i].vendor == uaa->uaa_vendor &&
		    auvitek_devices[i].product == uaa->uaa_product)
			return UMATCH_VENDOR_PRODUCT;
	}

	return UMATCH_NONE;
}

static void
auvitek_attach(device_t parent, device_t self, void *opaque)
{
	struct auvitek_softc *sc = device_private(self);
	struct usb_attach_arg *uaa = opaque;
	struct usbd_device *dev = uaa->uaa_device;
	usb_endpoint_descriptor_t *ed;
	usbd_status err;
	unsigned int i;
	uint8_t nep;

	aprint_naive("\n");
	aprint_normal(": AU0828\n");

	sc->sc_dev = self;
	sc->sc_udev = dev;
	sc->sc_uport = uaa->uaa_port;

	for (i = 0; i < __arraycount(auvitek_devices); i++) {
		if (auvitek_devices[i].vendor == uaa->uaa_vendor &&
		    auvitek_devices[i].product == uaa->uaa_product)
			break;
	}
	KASSERT(i != __arraycount(auvitek_devices));
	sc->sc_descr = auvitek_devices[i].name;
	sc->sc_board = auvitek_devices[i].board;

	sc->sc_dying = sc->sc_running = 0;

	mutex_init(&sc->sc_subdev_lock, MUTEX_DEFAULT, IPL_NONE);

	err = usbd_set_config_index(dev, 0, 1);
	if (err) {
		aprint_error_dev(self, "couldn't set config index: %s\n",
		    usbd_errstr(err));
		return;
	}
	err = usbd_device2interface_handle(dev, 0, &sc->sc_isoc_iface);
	if (err) {
		aprint_error_dev(self, "couldn't get interface handle: %s\n",
		    usbd_errstr(err));
		return;
	}
	err = usbd_device2interface_handle(dev, 3, &sc->sc_bulk_iface);
	if (err) {
		aprint_error_dev(self, "couldn't get interface handle: %s\n",
		    usbd_errstr(err));
		return;
	}

	sc->sc_ax.ax_sc = sc->sc_ab.ab_sc = sc;
	sc->sc_ax.ax_endpt = sc->sc_ab.ab_endpt = -1;

	err = usbd_set_interface(sc->sc_isoc_iface, AUVITEK_XFER_ALTNO);
	if (err) {
		aprint_error_dev(self, "couldn't set interface: %s\n",
		    usbd_errstr(err));
		return;
	}

	nep = 0;
	usbd_endpoint_count(sc->sc_isoc_iface, &nep);
	for (i = 0; i < nep; i++) {
		int dir, type;

		ed = usbd_interface2endpoint_descriptor(sc->sc_isoc_iface, i);
		if (ed == NULL) {
			aprint_error_dev(self,
			    "couldn't read endpoint descriptor %d\n", i);
			continue;
		}

		dir = UE_GET_DIR(ed->bEndpointAddress);
		type = UE_GET_XFERTYPE(ed->bmAttributes);

		if (dir == UE_DIR_IN && type == UE_ISOCHRONOUS &&
		    sc->sc_ax.ax_endpt == -1) {
			sc->sc_ax.ax_endpt = ed->bEndpointAddress;
			sc->sc_ax.ax_maxpktlen =
			    UE_GET_SIZE(UGETW(ed->wMaxPacketSize)) *
			    (UE_GET_TRANS(UGETW(ed->wMaxPacketSize)) + 1);
		}
	}

	err = usbd_set_interface(sc->sc_isoc_iface, 0);
	if (err) {
		aprint_error_dev(self, "couldn't set interface: %s\n",
		    usbd_errstr(err));
		return;
	}

	if (sc->sc_ax.ax_endpt == -1) {
		aprint_error_dev(self, "couldn't find isoc endpoint\n");
		sc->sc_dying = 1;
		return;
	}
	if (sc->sc_ax.ax_maxpktlen == 0) {
		aprint_error_dev(self, "couldn't determine packet length\n");
		sc->sc_dying = 1;
		return;
	}

	aprint_debug_dev(self, "isoc endpoint 0x%02x size %d\n",
	    sc->sc_ax.ax_endpt, sc->sc_ax.ax_maxpktlen);

	nep = 0;
	usbd_endpoint_count(sc->sc_bulk_iface, &nep);
	for (i = 0; i < nep; i++) {
		int dir, type;

		ed = usbd_interface2endpoint_descriptor(sc->sc_bulk_iface, i);
		if (ed == NULL) {
			aprint_error_dev(self,
			    "couldn't read endpoint descriptor %d\n", i);
			continue;
		}

		dir = UE_GET_DIR(ed->bEndpointAddress);
		type = UE_GET_XFERTYPE(ed->bmAttributes);

		if (dir == UE_DIR_IN && type == UE_BULK &&
		    sc->sc_ab.ab_endpt == -1) {
			sc->sc_ab.ab_endpt = ed->bEndpointAddress;
		}
	}

	if (sc->sc_ab.ab_endpt == -1) {
		aprint_error_dev(self, "couldn't find bulk endpoint\n");
		sc->sc_dying = 1;
		return;
	}

	aprint_debug_dev(self, "bulk endpoint 0x%02x size %d\n",
	    sc->sc_ab.ab_endpt, AUVITEK_BULK_BUFLEN);

	auvitek_board_init(sc);

	auvitek_i2c_attach(sc);

	sc->sc_au8522 = au8522_open(self, &sc->sc_i2c, 0x8e >> 1,
	    auvitek_board_get_if_frequency(sc));
	if (sc->sc_au8522 == NULL) {
		aprint_error_dev(sc->sc_dev, "couldn't initialize decoder\n");
		sc->sc_dying = 1;
		return;
	}

	config_mountroot(self, auvitek_attach_tuner);

	auvitek_video_attach(sc);
	auvitek_audio_attach(sc);
	auvitek_dtv_attach(sc);
}

void
auvitek_attach_tuner(device_t self)
{
	struct auvitek_softc *sc = device_private(self);

	mutex_enter(&sc->sc_subdev_lock);
	if (sc->sc_xc5k == NULL) {
		sc->sc_xc5k = xc5k_open(sc->sc_dev, &sc->sc_i2c, 0xc2 >> 1,
		    auvitek_board_tuner_reset, sc,
		    auvitek_board_get_if_frequency(sc),
		    FE_ATSC);
	}
	mutex_exit(&sc->sc_subdev_lock);
}

static int
auvitek_detach(device_t self, int flags)
{
	struct auvitek_softc *sc = device_private(self);
	int error;

	sc->sc_dying = 1;

	error = config_detach_children(self, flags);
	if (error) {
		/*
		 * XXX Should ask autoconf to block open with
		 * .d_cfdriver until we're done, instead of setting
		 * this and then rolling it back.
		 */
		sc->sc_dying = 0;
		return error;
	}

	pmf_device_deregister(self);

	auvitek_dtv_detach(sc, flags);
	auvitek_audio_detach(sc, flags);
	auvitek_video_detach(sc, flags);

	if (sc->sc_xc5k)
		xc5k_close(sc->sc_xc5k);
	if (sc->sc_au8522)
		au8522_close(sc->sc_au8522);

	auvitek_i2c_detach(sc, flags);

	mutex_destroy(&sc->sc_subdev_lock);

	return 0;
}

int
auvitek_activate(device_t self, enum devact act)
{
	struct auvitek_softc *sc = device_private(self);

	switch (act) {
	case DVACT_DEACTIVATE:
		sc->sc_dying = 1;
		return 0;
	default:
		return 0;
	}
}

static int
auvitek_rescan(device_t self, const char *ifattr, const int *locs)
{
	struct auvitek_softc *sc = device_private(self);

	auvitek_video_rescan(sc, ifattr, locs);
	auvitek_dtv_rescan(sc, ifattr, locs);
	auvitek_i2c_rescan(sc, ifattr, locs);

	return 0;
}

static void
auvitek_childdet(device_t self, device_t child)
{
	struct auvitek_softc *sc = device_private(self);

	auvitek_video_childdet(sc, child);
	auvitek_audio_childdet(sc, child);
	auvitek_dtv_childdet(sc, child);
}

uint8_t
auvitek_read_1(struct auvitek_softc *sc, uint16_t reg)
{
	usb_device_request_t req;
	usbd_status err;
	int actlen;
	uint8_t data;

	req.bmRequestType = UT_READ_VENDOR_DEVICE;
	req.bRequest = AU0828_CMD_REQUEST_IN;
	USETW(req.wValue, 0);
	USETW(req.wIndex, reg);
	USETW(req.wLength, sizeof(data));

	KERNEL_LOCK(1, curlwp);
	err = usbd_do_request_flags(sc->sc_udev, &req, &data, 0,
	    &actlen, USBD_DEFAULT_TIMEOUT);
	KERNEL_UNLOCK_ONE(curlwp);

	if (err)
		printf("%s: read failed: %s\n", device_xname(sc->sc_dev),
		    usbd_errstr(err));

	return data;
}

void
auvitek_write_1(struct auvitek_softc *sc, uint16_t reg, uint8_t data)
{
	usb_device_request_t req;
	usbd_status err;
	int actlen;

	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
	req.bRequest = AU0828_CMD_REQUEST_OUT;
	USETW(req.wValue, data);
	USETW(req.wIndex, reg);
	USETW(req.wLength, 0);

	KERNEL_LOCK(1, curlwp);
	err = usbd_do_request_flags(sc->sc_udev, &req, NULL, 0,
	    &actlen, USBD_DEFAULT_TIMEOUT);
	KERNEL_UNLOCK_ONE(curlwp);

	if (err)
		printf("%s: write failed: %s\n", device_xname(sc->sc_dev),
		    usbd_errstr(err));
}

MODULE(MODULE_CLASS_DRIVER, auvitek, "au8522,xc5k");

#ifdef _MODULE
#include "ioconf.c"
#endif

static int
auvitek_modcmd(modcmd_t cmd, void *opaque)
{
	switch (cmd) {
	case MODULE_CMD_INIT:
#ifdef _MODULE
		return config_init_component(cfdriver_ioconf_auvitek,
		    cfattach_ioconf_auvitek, cfdata_ioconf_auvitek);
#else
		return 0;
#endif
	case MODULE_CMD_FINI:
#ifdef _MODULE
		return config_fini_component(cfdriver_ioconf_auvitek,
		    cfattach_ioconf_auvitek, cfdata_ioconf_auvitek);
#else
		return 0;
#endif
	default:
		return ENOTTY;
	}
}