emdtv_dtv.c revision 1.15 1 /* $NetBSD: emdtv_dtv.c,v 1.15 2021/04/24 23:36:59 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2008, 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 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: emdtv_dtv.c,v 1.15 2021/04/24 23:36:59 thorpej Exp $");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/device.h>
35 #include <sys/lwp.h>
36 #include <sys/conf.h>
37
38 #include <dev/usb/usb.h>
39 #include <dev/usb/usbdi.h>
40 #include <dev/usb/usbdi_util.h>
41 #include <dev/usb/usbdivar.h>
42 #include <dev/usb/usbdevs.h>
43
44 #include <dev/i2c/i2cvar.h>
45
46 #include <dev/usb/emdtvvar.h>
47 #include <dev/usb/emdtvreg.h>
48
49 static void emdtv_dtv_get_devinfo(void *,
50 struct dvb_frontend_info *);
51 static int emdtv_dtv_open(void *, int);
52 static void emdtv_dtv_close(void *);
53 static int emdtv_dtv_set_tuner(void *,
54 const struct dvb_frontend_parameters *);
55 static fe_status_t emdtv_dtv_get_status(void *);
56 static uint16_t emdtv_dtv_get_signal_strength(void *);
57 static uint16_t emdtv_dtv_get_snr(void *);
58 static int emdtv_dtv_start_transfer(void *,
59 void (*)(void *, const struct dtv_payload *),
60 void *);
61 static int emdtv_dtv_stop_transfer(void *);
62
63 static int emdtv_dtv_tuner_reset(void *);
64
65 static void emdtv_dtv_isoc_startall(struct emdtv_softc *);
66 static int emdtv_dtv_isoc_start(struct emdtv_softc *,
67 struct emdtv_isoc_xfer *);
68 static void emdtv_dtv_isoc(struct usbd_xfer *, void *,
69 usbd_status);
70
71 static const struct dtv_hw_if emdtv_dtv_if = {
72 .get_devinfo = emdtv_dtv_get_devinfo,
73 .open = emdtv_dtv_open,
74 .close = emdtv_dtv_close,
75 .set_tuner = emdtv_dtv_set_tuner,
76 .get_status = emdtv_dtv_get_status,
77 .get_signal_strength = emdtv_dtv_get_signal_strength,
78 .get_snr = emdtv_dtv_get_snr,
79 .start_transfer = emdtv_dtv_start_transfer,
80 .stop_transfer = emdtv_dtv_stop_transfer,
81 };
82
83 void
84 emdtv_dtv_attach(struct emdtv_softc *sc)
85 {
86 usb_endpoint_descriptor_t *ed;
87 usbd_status status;
88 int i;
89
90 for (i = 0; i < EMDTV_NXFERS; i++) {
91 sc->sc_ix[i].ix_altix = (i & 1) ?
92 &sc->sc_ix[i - 1] : &sc->sc_ix[i + 1];
93 sc->sc_ix[i].ix_sc = sc;
94 }
95
96 ed = usbd_interface2endpoint_descriptor(sc->sc_iface, 3);
97 if (ed == NULL) {
98 aprint_error_dev(sc->sc_dev, "couldn't find endpoint 3\n");
99 return;
100 }
101 sc->sc_isoc_maxpacketsize = UGETW(ed->wMaxPacketSize);
102 sc->sc_isoc_buflen = sc->sc_isoc_maxpacketsize * EMDTV_NFRAMES;
103
104 aprint_debug_dev(sc->sc_dev, "calling usbd_open_pipe, ep 0x%02x\n",
105 ed->bEndpointAddress);
106 status = usbd_open_pipe(sc->sc_iface,
107 ed->bEndpointAddress, USBD_EXCLUSIVE_USE|USBD_MPSAFE,
108 &sc->sc_isoc_pipe);
109 if (status != USBD_NORMAL_COMPLETION) {
110 aprint_error_dev(sc->sc_dev, "couldn't open isoc pipe\n");
111 usbd_set_interface(sc->sc_iface, 0);
112 return;
113 }
114
115 emdtv_write_1(sc, UR_GET_STATUS, 0x48, 0x00);
116 emdtv_write_1(sc, UR_GET_STATUS, 0x12, 0x77);
117 usbd_delay_ms(sc->sc_udev, 6);
118
119 emdtv_gpio_ctl(sc, EMDTV_GPIO_ANALOG_ON, false);
120 emdtv_gpio_ctl(sc, EMDTV_GPIO_TS1_ON, true);
121 emdtv_gpio_ctl(sc, EMDTV_GPIO_TUNER1_ON, true);
122 emdtv_gpio_ctl(sc, EMDTV_GPIO_DEMOD1_RESET, true);
123 usbd_delay_ms(sc->sc_udev, 100);
124
125 emdtv_dtv_rescan(sc, NULL, NULL);
126 }
127
128 static void
129 emdtv_dtv_free_xfers(struct emdtv_softc *sc)
130 {
131
132 for (size_t i = 0; i < EMDTV_NXFERS; i++)
133 if (sc->sc_ix[i].ix_xfer) {
134 usbd_destroy_xfer(sc->sc_ix[i].ix_xfer);
135 sc->sc_ix[i].ix_xfer = NULL;
136 sc->sc_ix[i].ix_buf = NULL;
137 }
138
139 return;
140 }
141
142 void
143 emdtv_dtv_detach(struct emdtv_softc *sc, int flags)
144 {
145 sc->sc_streaming = false;
146
147 if (sc->sc_dtvdev != NULL) {
148 config_detach(sc->sc_dtvdev, flags);
149 sc->sc_dtvdev = NULL;
150 }
151
152 if (sc->sc_xc3028)
153 xc3028_close(sc->sc_xc3028);
154 if (sc->sc_lg3303)
155 lg3303_close(sc->sc_lg3303);
156
157 if (sc->sc_isoc_pipe) {
158 usbd_abort_pipe(sc->sc_isoc_pipe);
159 emdtv_dtv_free_xfers(sc);
160 usbd_close_pipe(sc->sc_isoc_pipe);
161 sc->sc_isoc_pipe = NULL;
162 }
163 }
164
165 void
166 emdtv_dtv_rescan(struct emdtv_softc *sc, const char *ifattr, const int *locs)
167 {
168 struct dtv_attach_args daa;
169
170 daa.hw = &emdtv_dtv_if;
171 daa.priv = sc;
172
173 if (ifattr_match(ifattr, "dtvbus") && sc->sc_dtvdev == NULL)
174 sc->sc_dtvdev = config_found(sc->sc_dev, &daa, dtv_print,
175 CFARG_IATTR, "dtvbus",
176 CFARG_EOL);
177 }
178
179 static void
180 emdtv_dtv_get_devinfo(void *priv, struct dvb_frontend_info *info)
181 {
182 struct emdtv_softc *sc = priv;
183
184 memset(info, 0, sizeof(*info));
185 strlcpy(info->name, sc->sc_board->eb_name, sizeof(info->name));
186 info->type = FE_ATSC;
187 info->frequency_min = 54000000;
188 info->frequency_max = 858000000;
189 info->frequency_stepsize = 62500;
190 info->caps = FE_CAN_8VSB;
191 }
192
193 static int
194 emdtv_dtv_open(void *priv, int flags)
195 {
196 struct emdtv_softc *sc = priv;
197
198 if (sc->sc_dying)
199 return ENXIO;
200
201 aprint_debug_dev(sc->sc_dev, "allocating isoc xfers (pktsz %d)\n",
202 sc->sc_isoc_maxpacketsize);
203
204 for (size_t i = 0; i < EMDTV_NXFERS; i++) {
205 int error = usbd_create_xfer(sc->sc_isoc_pipe,
206 sc->sc_isoc_buflen, USBD_SHORT_XFER_OK, EMDTV_NFRAMES,
207 &sc->sc_ix[i].ix_xfer);
208 if (error)
209 return error;
210 sc->sc_ix[i].ix_buf = usbd_get_buffer(sc->sc_ix[i].ix_xfer);
211 aprint_debug_dev(sc->sc_dev, " ix[%zu] xfer %p buf %p\n",
212 i, sc->sc_ix[i].ix_xfer, sc->sc_ix[i].ix_buf);
213 }
214
215 switch (sc->sc_board->eb_tuner) {
216 case EMDTV_TUNER_XC3028:
217 if (sc->sc_xc3028 == NULL) {
218 sc->sc_xc3028 = xc3028_open(sc->sc_dev,
219 &sc->sc_i2c, 0x61 << 1, emdtv_dtv_tuner_reset, sc,
220 XC3028);
221 }
222 if (sc->sc_xc3028 == NULL) {
223 aprint_error_dev(sc->sc_dev, "couldn't open xc3028\n");
224 return ENXIO;
225 }
226 break;
227 case EMDTV_TUNER_XC3028L:
228 if (sc->sc_xc3028 == NULL) {
229 sc->sc_xc3028 = xc3028_open(sc->sc_dev,
230 &sc->sc_i2c, 0x61 << 1, emdtv_dtv_tuner_reset, sc,
231 XC3028L);
232 }
233 if (sc->sc_xc3028 == NULL) {
234 aprint_error_dev(sc->sc_dev, "couldn't open xc3028l\n");
235 return ENXIO;
236 }
237 break;
238 default:
239 aprint_error_dev(sc->sc_dev, "unsupported tuner (%d)\n",
240 sc->sc_board->eb_tuner);
241 return EIO;
242 }
243
244 switch (sc->sc_board->eb_demod) {
245 case EMDTV_DEMOD_LG3303:
246 if (sc->sc_lg3303 == NULL) {
247 sc->sc_lg3303 = lg3303_open(sc->sc_dev,
248 &sc->sc_i2c, 0x1c, 0);
249 }
250 if (sc->sc_lg3303 == NULL) {
251 aprint_error_dev(sc->sc_dev, "couldn't open lg3303\n");
252 return ENXIO;
253 }
254 break;
255 default:
256 aprint_error_dev(sc->sc_dev, "unsupported demod (%d)\n",
257 sc->sc_board->eb_demod);
258 return EIO;
259 }
260
261 return 0;
262 }
263
264 static void
265 emdtv_dtv_close(void *priv)
266 {
267 struct emdtv_softc *sc = priv;
268
269 emdtv_dtv_free_xfers(sc);
270 }
271
272 static int
273 emdtv_dtv_set_tuner(void *priv, const struct dvb_frontend_parameters *params)
274 {
275 struct emdtv_softc *sc = priv;
276 int error;
277
278 /* Setup demod */
279 error = ENXIO;
280 if (sc->sc_lg3303)
281 error = lg3303_set_modulation(sc->sc_lg3303,
282 params->u.vsb.modulation);
283 if (error)
284 return error;
285
286 /* Setup tuner */
287 error = ENXIO;
288 if (sc->sc_xc3028)
289 error = xc3028_tune_dtv(sc->sc_xc3028, params);
290
291 return error;
292 }
293
294 static fe_status_t
295 emdtv_dtv_get_status(void *priv)
296 {
297 struct emdtv_softc *sc = priv;
298
299 if (sc->sc_lg3303)
300 return lg3303_get_dtv_status(sc->sc_lg3303);
301
302 return 0;
303 }
304
305 uint16_t
306 emdtv_dtv_get_signal_strength(void *priv)
307 {
308 struct emdtv_softc *sc = priv;
309
310 if (sc->sc_lg3303)
311 return lg3303_get_signal_strength(sc->sc_lg3303);
312
313 return 0;
314 }
315
316 uint16_t
317 emdtv_dtv_get_snr(void *priv)
318 {
319 struct emdtv_softc *sc = priv;
320
321 if (sc->sc_lg3303)
322 return lg3303_get_snr(sc->sc_lg3303);
323
324 return 0;
325 }
326
327 static int
328 emdtv_dtv_start_transfer(void *priv,
329 void (*cb)(void *, const struct dtv_payload *), void *arg)
330 {
331 struct emdtv_softc *sc = priv;
332 int s;
333
334 s = splusb();
335
336 sc->sc_streaming = true;
337 sc->sc_dtvsubmitcb = cb;
338 sc->sc_dtvsubmitarg = arg;
339
340 aprint_debug_dev(sc->sc_dev, "starting isoc transactions\n");
341
342 emdtv_dtv_isoc_startall(sc);
343 splx(s);
344
345 return 0;
346 }
347
348 static int
349 emdtv_dtv_stop_transfer(void *priv)
350 {
351 struct emdtv_softc *sc = priv;
352
353 aprint_debug_dev(sc->sc_dev, "stopping stream\n");
354
355 sc->sc_streaming = false;
356
357 KERNEL_LOCK(1, curlwp);
358 if (sc->sc_isoc_pipe != NULL)
359 usbd_abort_pipe(sc->sc_isoc_pipe);
360 KERNEL_UNLOCK_ONE(curlwp);
361
362 sc->sc_dtvsubmitcb = NULL;
363 sc->sc_dtvsubmitarg = NULL;
364
365 return 0;
366 }
367
368 static void
369 emdtv_dtv_isoc_startall(struct emdtv_softc *sc)
370 {
371 int i;
372
373 if (sc->sc_streaming == false || sc->sc_dying == true)
374 return;
375
376 for (i = 0; i < EMDTV_NXFERS; i += 2)
377 emdtv_dtv_isoc_start(sc, &sc->sc_ix[i]);
378 }
379
380 static int
381 emdtv_dtv_isoc_start(struct emdtv_softc *sc, struct emdtv_isoc_xfer *ix)
382 {
383 int i;
384
385 if (sc->sc_isoc_pipe == NULL)
386 return EIO;
387
388 for (i = 0; i < EMDTV_NFRAMES; i++)
389 ix->ix_frlengths[i] = sc->sc_isoc_maxpacketsize;
390
391 usbd_setup_isoc_xfer(ix->ix_xfer,
392 ix,
393 ix->ix_frlengths,
394 EMDTV_NFRAMES,
395 USBD_SHORT_XFER_OK,
396 emdtv_dtv_isoc);
397
398 KERNEL_LOCK(1, curlwp);
399 usbd_transfer(ix->ix_xfer);
400 KERNEL_UNLOCK_ONE(curlwp);
401
402 return 0;
403 }
404
405 static void
406 emdtv_dtv_isoc(struct usbd_xfer *xfer, void * priv,
407 usbd_status err)
408 {
409 struct emdtv_isoc_xfer *ix = priv;
410 struct emdtv_softc *sc = ix->ix_sc;
411 struct dtv_payload payload;
412 struct usbd_pipe *isoc = sc->sc_isoc_pipe;
413 uint32_t len;
414 uint8_t *buf;
415 int i;
416
417 KASSERT(xfer == ix->ix_xfer);
418
419 if (sc->sc_dying || sc->sc_dtvsubmitcb == NULL)
420 return;
421
422 if (err) {
423 if (err == USBD_STALLED) {
424 usbd_clear_endpoint_stall_async(isoc);
425 goto resched;
426 }
427 return;
428 }
429
430 usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
431
432 if (len == 0)
433 goto resched;
434
435 buf = usbd_get_buffer(xfer);
436 if (buf == NULL)
437 goto resched;
438
439 for (i = 0; i < EMDTV_NFRAMES; i++, buf += sc->sc_isoc_maxpacketsize) {
440 if (ix->ix_frlengths[i] == 0)
441 continue;
442 payload.data = buf;
443 payload.size = ix->ix_frlengths[i];
444 sc->sc_dtvsubmitcb(sc->sc_dtvsubmitarg, &payload);
445 }
446
447 resched:
448 emdtv_dtv_isoc_start(sc, ix->ix_altix);
449 }
450
451 static int
452 emdtv_dtv_tuner_reset(void *opaque)
453 {
454 struct emdtv_softc *sc = opaque;
455 emdtv_gpio_ctl(sc, EMDTV_GPIO_TUNER1_RESET, true);
456 return 0;
457 }
458