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