auvitek_dtv.c revision 1.8 1 /* $NetBSD: auvitek_dtv.c,v 1.8 2021/04/24 23:36:59 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2011 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 (Digital TV function)
31 */
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: auvitek_dtv.c,v 1.8 2021/04/24 23:36:59 thorpej Exp $");
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/device.h>
39 #include <sys/conf.h>
40 #include <sys/kmem.h>
41 #include <sys/bus.h>
42
43 #include <dev/usb/usb.h>
44 #include <dev/usb/usbdi.h>
45 #include <dev/usb/usbdivar.h>
46 #include <dev/usb/usbdi_util.h>
47 #include <dev/usb/usbdevs.h>
48
49 #include <dev/dtv/dtvif.h>
50
51 #include <dev/usb/auvitekreg.h>
52 #include <dev/usb/auvitekvar.h>
53
54 static void auvitek_dtv_get_devinfo(void *,
55 struct dvb_frontend_info *);
56 static int auvitek_dtv_open(void *, int);
57 static void auvitek_dtv_close(void *);
58 static int auvitek_dtv_set_tuner(void *,
59 const struct dvb_frontend_parameters *);
60 static fe_status_t auvitek_dtv_get_status(void *);
61 static uint16_t auvitek_dtv_get_signal_strength(void *);
62 static uint16_t auvitek_dtv_get_snr(void *);
63 static int auvitek_dtv_start_transfer(void *,
64 void (*)(void *, const struct dtv_payload *),
65 void *);
66 static int auvitek_dtv_stop_transfer(void *);
67
68 static int auvitek_dtv_init_pipes(struct auvitek_softc *);
69 static int auvitek_dtv_abort_pipes(struct auvitek_softc *);
70 static int auvitek_dtv_close_pipes(struct auvitek_softc *);
71
72 static int auvitek_dtv_bulk_start(struct auvitek_softc *);
73 static int auvitek_dtv_bulk_start1(struct auvitek_bulk_xfer *);
74 static void auvitek_dtv_bulk_cb(struct usbd_xfer *, void *,
75 usbd_status);
76
77 static const struct dtv_hw_if auvitek_dtv_if = {
78 .get_devinfo = auvitek_dtv_get_devinfo,
79 .open = auvitek_dtv_open,
80 .close = auvitek_dtv_close,
81 .set_tuner = auvitek_dtv_set_tuner,
82 .get_status = auvitek_dtv_get_status,
83 .get_signal_strength = auvitek_dtv_get_signal_strength,
84 .get_snr = auvitek_dtv_get_snr,
85 .start_transfer = auvitek_dtv_start_transfer,
86 .stop_transfer = auvitek_dtv_stop_transfer,
87 };
88
89 int
90 auvitek_dtv_attach(struct auvitek_softc *sc)
91 {
92
93 auvitek_dtv_rescan(sc, NULL, NULL);
94
95 return sc->sc_dtvdev != NULL;
96 }
97
98 int
99 auvitek_dtv_detach(struct auvitek_softc *sc, int flags)
100 {
101 if (sc->sc_dtvdev != NULL) {
102 config_detach(sc->sc_dtvdev, flags);
103 sc->sc_dtvdev = NULL;
104 }
105
106 return 0;
107 }
108
109 void
110 auvitek_dtv_rescan(struct auvitek_softc *sc, const char *ifattr,
111 const int *locs)
112 {
113 struct dtv_attach_args daa;
114
115 daa.hw = &auvitek_dtv_if;
116 daa.priv = sc;
117
118 if (ifattr_match(ifattr, "dtvbus") && sc->sc_dtvdev == NULL)
119 sc->sc_dtvdev = config_found(sc->sc_dev, &daa, dtv_print,
120 CFARG_IATTR, "dtvbus",
121 CFARG_EOL);
122 }
123
124 void
125 auvitek_dtv_childdet(struct auvitek_softc *sc, device_t child)
126 {
127 if (sc->sc_dtvdev == child)
128 sc->sc_dtvdev = NULL;
129 }
130
131 static void
132 auvitek_dtv_get_devinfo(void *priv, struct dvb_frontend_info *info)
133 {
134 struct auvitek_softc *sc = priv;
135
136 memset(info, 0, sizeof(*info));
137 strlcpy(info->name, sc->sc_descr, sizeof(info->name));
138 info->type = FE_ATSC;
139 info->frequency_min = 54000000;
140 info->frequency_max = 858000000;
141 info->frequency_stepsize = 62500;
142 info->caps = FE_CAN_QAM_64 | FE_CAN_QAM_256 | FE_CAN_8VSB;
143 }
144
145 static int
146 auvitek_dtv_open(void *priv, int flags)
147 {
148 struct auvitek_softc *sc = priv;
149
150 if (sc->sc_dying)
151 return EIO;
152
153 auvitek_attach_tuner(sc->sc_dev);
154 if (sc->sc_xc5k == NULL)
155 return ENXIO;
156
157 int err = auvitek_dtv_init_pipes(sc);
158 if (err)
159 return err;
160
161 for (size_t i = 0; i < AUVITEK_NBULK_XFERS; i++) {
162 sc->sc_ab.ab_bx[i].bx_sc = sc;
163 err = usbd_create_xfer(sc->sc_ab.ab_pipe,
164 AUVITEK_BULK_BUFLEN, 0, 0, &sc->sc_ab.ab_bx[i].bx_xfer);
165 if (err) {
166 aprint_error_dev(sc->sc_dev,
167 "couldn't allocate xfer\n");
168 sc->sc_dying = 1;
169 return err;
170 }
171 sc->sc_ab.ab_bx[i].bx_buffer = usbd_get_buffer(
172 sc->sc_ab.ab_bx[i].bx_xfer);
173 }
174
175
176 return 0;
177 }
178
179 static void
180 auvitek_dtv_close(void *priv)
181 {
182 struct auvitek_softc *sc = priv;
183
184 auvitek_dtv_stop_transfer(sc);
185 auvitek_dtv_abort_pipes(sc);
186
187 for (size_t i = 0; i < AUVITEK_NBULK_XFERS; i++) {
188 if (sc->sc_ab.ab_bx[i].bx_xfer)
189 usbd_destroy_xfer(sc->sc_ab.ab_bx[i].bx_xfer);
190 }
191
192 auvitek_dtv_close_pipes(sc);
193
194 sc->sc_dtvsubmitcb = NULL;
195 sc->sc_dtvsubmitarg = NULL;
196 }
197
198 static int
199 auvitek_dtv_set_tuner(void *priv, const struct dvb_frontend_parameters *params)
200 {
201 struct auvitek_softc *sc = priv;
202 int error;
203
204 error = au8522_set_modulation(sc->sc_au8522, params->u.vsb.modulation);
205 if (error)
206 return error;
207
208 delay(100000);
209
210 au8522_set_gate(sc->sc_au8522, true);
211 error = xc5k_tune_dtv(sc->sc_xc5k, params);
212 au8522_set_gate(sc->sc_au8522, false);
213
214 return error;
215 }
216
217 fe_status_t
218 auvitek_dtv_get_status(void *priv)
219 {
220 struct auvitek_softc *sc = priv;
221
222 return au8522_get_dtv_status(sc->sc_au8522);
223 }
224
225 uint16_t
226 auvitek_dtv_get_signal_strength(void *priv)
227 {
228 return auvitek_dtv_get_snr(priv);
229 }
230
231 uint16_t
232 auvitek_dtv_get_snr(void *priv)
233 {
234 struct auvitek_softc *sc = priv;
235
236 return au8522_get_snr(sc->sc_au8522);
237 }
238
239 static int
240 auvitek_dtv_start_transfer(void *priv,
241 void (*cb)(void *, const struct dtv_payload *), void *arg)
242 {
243 struct auvitek_softc *sc = priv;
244 int s;
245
246 if (sc->sc_ab.ab_running) {
247 return 0;
248 }
249
250 sc->sc_dtvsubmitcb = cb;
251 sc->sc_dtvsubmitarg = arg;
252
253 auvitek_write_1(sc, 0x608, 0x90);
254 auvitek_write_1(sc, 0x609, 0x72);
255 auvitek_write_1(sc, 0x60a, 0x71);
256 auvitek_write_1(sc, 0x60b, 0x01);
257
258 sc->sc_ab.ab_running = true;
259
260 s = splusb();
261 auvitek_dtv_bulk_start(sc);
262 splx(s);
263
264 return 0;
265 }
266
267 static int
268 auvitek_dtv_stop_transfer(void *priv)
269 {
270 struct auvitek_softc *sc = priv;
271
272 sc->sc_ab.ab_running = false;
273
274 auvitek_write_1(sc, 0x608, 0x00);
275 auvitek_write_1(sc, 0x609, 0x00);
276 auvitek_write_1(sc, 0x60a, 0x00);
277 auvitek_write_1(sc, 0x60b, 0x00);
278
279 return 0;
280 }
281
282 static int
283 auvitek_dtv_init_pipes(struct auvitek_softc *sc)
284 {
285 usbd_status err;
286
287 KERNEL_LOCK(1, curlwp);
288 err = usbd_open_pipe(sc->sc_bulk_iface, sc->sc_ab.ab_endpt,
289 USBD_EXCLUSIVE_USE|USBD_MPSAFE, &sc->sc_ab.ab_pipe);
290 KERNEL_UNLOCK_ONE(curlwp);
291
292 if (err) {
293 aprint_error_dev(sc->sc_dev, "couldn't open bulk-in pipe: %s\n",
294 usbd_errstr(err));
295 return ENOMEM;
296 }
297
298 return 0;
299 }
300
301 static int
302 auvitek_dtv_abort_pipes(struct auvitek_softc *sc)
303 {
304 if (sc->sc_ab.ab_pipe != NULL) {
305 KERNEL_LOCK(1, curlwp);
306 usbd_abort_pipe(sc->sc_ab.ab_pipe);
307 KERNEL_UNLOCK_ONE(curlwp);
308 }
309
310 return 0;
311 }
312
313 static int
314 auvitek_dtv_close_pipes(struct auvitek_softc *sc)
315 {
316 if (sc->sc_ab.ab_pipe != NULL) {
317 KERNEL_LOCK(1, curlwp);
318 usbd_close_pipe(sc->sc_ab.ab_pipe);
319 KERNEL_UNLOCK_ONE(curlwp);
320 sc->sc_ab.ab_pipe = NULL;
321 }
322
323 return 0;
324 }
325
326 static void
327 auvitek_dtv_bulk_cb(struct usbd_xfer *xfer, void *priv,
328 usbd_status status)
329 {
330 struct auvitek_bulk_xfer *bx = priv;
331 struct auvitek_softc *sc = bx->bx_sc;
332 struct auvitek_bulk *ab = &sc->sc_ab;
333 struct dtv_payload payload;
334 uint32_t xferlen;
335
336 if (ab->ab_running == false || sc->sc_dtvsubmitcb == NULL)
337 return;
338
339 usbd_get_xfer_status(xfer, NULL, NULL, &xferlen, NULL);
340
341 //printf("%s: status=%d xferlen=%u\n", __func__, status, xferlen);
342
343 if (status != USBD_NORMAL_COMPLETION) {
344 printf("%s: USB error (%s)\n", __func__, usbd_errstr(status));
345 if (status == USBD_STALLED) {
346 usbd_clear_endpoint_stall_async(ab->ab_pipe);
347 goto next;
348 }
349 if (status == USBD_SHORT_XFER) {
350 goto next;
351 }
352 return;
353 }
354
355 if (xferlen == 0) {
356 printf("%s: 0-length xfer\n", __func__);
357 goto next;
358 }
359
360 payload.data = bx->bx_buffer;
361 payload.size = xferlen;
362 sc->sc_dtvsubmitcb(sc->sc_dtvsubmitarg, &payload);
363
364 next:
365 auvitek_dtv_bulk_start1(bx);
366 }
367
368 static int
369 auvitek_dtv_bulk_start(struct auvitek_softc *sc)
370 {
371 int i, error;
372
373 for (i = 0; i < AUVITEK_NBULK_XFERS; i++) {
374 error = auvitek_dtv_bulk_start1(&sc->sc_ab.ab_bx[i]);
375 if (error)
376 return error;
377 }
378
379 return 0;
380 }
381
382 static int
383 auvitek_dtv_bulk_start1(struct auvitek_bulk_xfer *bx)
384 {
385 struct auvitek_softc *sc = bx->bx_sc;
386 usbd_status err;
387
388 usbd_setup_xfer(bx->bx_xfer, bx, bx->bx_buffer, AUVITEK_BULK_BUFLEN,
389 0 /* USBD_SHORT_XFER_OK */, 100 /* USBD_NO_TIMEOUT */,
390 auvitek_dtv_bulk_cb);
391
392 KERNEL_LOCK(1, curlwp);
393 err = usbd_transfer(bx->bx_xfer);
394 KERNEL_UNLOCK_ONE(curlwp);
395
396 if (err != USBD_IN_PROGRESS) {
397 aprint_error_dev(sc->sc_dev, "USB error: %s\n",
398 usbd_errstr(err));
399 return ENODEV;
400 }
401
402 return 0;
403 }
404