emdtv_dtv.c revision 1.10.14.4 1 /* $NetBSD: emdtv_dtv.c,v 1.10.14.4 2015/10/06 21:32:15 skrll 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.10.14.4 2015/10/06 21:32:15 skrll 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 void
129 emdtv_dtv_detach(struct emdtv_softc *sc, int flags)
130 {
131 sc->sc_streaming = false;
132
133 if (sc->sc_dtvdev != NULL) {
134 config_detach(sc->sc_dtvdev, flags);
135 sc->sc_dtvdev = NULL;
136 }
137
138 if (sc->sc_xc3028)
139 xc3028_close(sc->sc_xc3028);
140 if (sc->sc_lg3303)
141 lg3303_close(sc->sc_lg3303);
142
143 if (sc->sc_isoc_pipe) {
144 usbd_abort_pipe(sc->sc_isoc_pipe);
145 usbd_close_pipe(sc->sc_isoc_pipe);
146 sc->sc_isoc_pipe = NULL;
147 }
148 }
149
150 void
151 emdtv_dtv_rescan(struct emdtv_softc *sc, const char *ifattr, const int *locs)
152 {
153 struct dtv_attach_args daa;
154
155 daa.hw = &emdtv_dtv_if;
156 daa.priv = sc;
157
158 if (ifattr_match(ifattr, "dtvbus") && sc->sc_dtvdev == NULL)
159 sc->sc_dtvdev = config_found_ia(sc->sc_dev, "dtvbus",
160 &daa, dtv_print);
161 }
162
163 static void
164 emdtv_dtv_get_devinfo(void *priv, struct dvb_frontend_info *info)
165 {
166 struct emdtv_softc *sc = priv;
167
168 memset(info, 0, sizeof(*info));
169 strlcpy(info->name, sc->sc_board->eb_name, sizeof(info->name));
170 info->type = FE_ATSC;
171 info->frequency_min = 54000000;
172 info->frequency_max = 858000000;
173 info->frequency_stepsize = 62500;
174 info->caps = FE_CAN_8VSB;
175 }
176
177 static int
178 emdtv_dtv_open(void *priv, int flags)
179 {
180 struct emdtv_softc *sc = priv;
181
182 if (sc->sc_dying)
183 return ENXIO;
184
185 aprint_debug_dev(sc->sc_dev, "allocating isoc xfers (pktsz %d)\n",
186 sc->sc_isoc_maxpacketsize);
187
188 for (size_t i = 0; i < EMDTV_NXFERS; i++) {
189 int error = usbd_create_xfer(sc->sc_isoc_pipe,
190 sc->sc_isoc_buflen, USBD_SHORT_XFER_OK, EMDTV_NFRAMES,
191 &sc->sc_ix[i].ix_xfer);
192 if (error)
193 return error;
194 sc->sc_ix[i].ix_buf = usbd_get_buffer(sc->sc_ix[i].ix_xfer);
195 aprint_debug_dev(sc->sc_dev, " ix[%zu] xfer %p buf %p\n",
196 i, sc->sc_ix[i].ix_xfer, sc->sc_ix[i].ix_buf);
197 }
198
199 switch (sc->sc_board->eb_tuner) {
200 case EMDTV_TUNER_XC3028:
201 if (sc->sc_xc3028 == NULL) {
202 sc->sc_xc3028 = xc3028_open(sc->sc_dev,
203 &sc->sc_i2c, 0x61 << 1, emdtv_dtv_tuner_reset, sc,
204 XC3028);
205 }
206 if (sc->sc_xc3028 == NULL) {
207 aprint_error_dev(sc->sc_dev, "couldn't open xc3028\n");
208 return ENXIO;
209 }
210 break;
211 case EMDTV_TUNER_XC3028L:
212 if (sc->sc_xc3028 == NULL) {
213 sc->sc_xc3028 = xc3028_open(sc->sc_dev,
214 &sc->sc_i2c, 0x61 << 1, emdtv_dtv_tuner_reset, sc,
215 XC3028L);
216 }
217 if (sc->sc_xc3028 == NULL) {
218 aprint_error_dev(sc->sc_dev, "couldn't open xc3028l\n");
219 return ENXIO;
220 }
221 break;
222 default:
223 aprint_error_dev(sc->sc_dev, "unsupported tuner (%d)\n",
224 sc->sc_board->eb_tuner);
225 return EIO;
226 }
227
228 switch (sc->sc_board->eb_demod) {
229 case EMDTV_DEMOD_LG3303:
230 if (sc->sc_lg3303 == NULL) {
231 sc->sc_lg3303 = lg3303_open(sc->sc_dev,
232 &sc->sc_i2c, 0x1c, 0);
233 }
234 if (sc->sc_lg3303 == NULL) {
235 aprint_error_dev(sc->sc_dev, "couldn't open lg3303\n");
236 return ENXIO;
237 }
238 break;
239 default:
240 aprint_error_dev(sc->sc_dev, "unsupported demod (%d)\n",
241 sc->sc_board->eb_demod);
242 return EIO;
243 }
244
245 return 0;
246 }
247
248 static void
249 emdtv_dtv_close(void *priv)
250 {
251 struct emdtv_softc *sc = priv;
252
253 for (size_t i = 0; i < EMDTV_NXFERS; i++)
254 if (sc->sc_ix[i].ix_xfer) {
255 usbd_destroy_xfer(sc->sc_ix[i].ix_xfer);
256 sc->sc_ix[i].ix_xfer = NULL;
257 sc->sc_ix[i].ix_buf = NULL;
258 }
259
260 return;
261 }
262
263 static int
264 emdtv_dtv_set_tuner(void *priv, const struct dvb_frontend_parameters *params)
265 {
266 struct emdtv_softc *sc = priv;
267 int error;
268
269 /* Setup demod */
270 error = ENXIO;
271 if (sc->sc_lg3303)
272 error = lg3303_set_modulation(sc->sc_lg3303,
273 params->u.vsb.modulation);
274 if (error)
275 return error;
276
277 /* Setup tuner */
278 error = ENXIO;
279 if (sc->sc_xc3028)
280 error = xc3028_tune_dtv(sc->sc_xc3028, params);
281
282 return error;
283 }
284
285 static fe_status_t
286 emdtv_dtv_get_status(void *priv)
287 {
288 struct emdtv_softc *sc = priv;
289
290 if (sc->sc_lg3303)
291 return lg3303_get_dtv_status(sc->sc_lg3303);
292
293 return 0;
294 }
295
296 uint16_t
297 emdtv_dtv_get_signal_strength(void *priv)
298 {
299 struct emdtv_softc *sc = priv;
300
301 if (sc->sc_lg3303)
302 return lg3303_get_signal_strength(sc->sc_lg3303);
303
304 return 0;
305 }
306
307 uint16_t
308 emdtv_dtv_get_snr(void *priv)
309 {
310 struct emdtv_softc *sc = priv;
311
312 if (sc->sc_lg3303)
313 return lg3303_get_snr(sc->sc_lg3303);
314
315 return 0;
316 }
317
318 static int
319 emdtv_dtv_start_transfer(void *priv,
320 void (*cb)(void *, const struct dtv_payload *), void *arg)
321 {
322 struct emdtv_softc *sc = priv;
323 int s;
324
325 s = splusb();
326
327 sc->sc_streaming = true;
328 sc->sc_dtvsubmitcb = cb;
329 sc->sc_dtvsubmitarg = arg;
330
331 aprint_debug_dev(sc->sc_dev, "starting isoc transactions\n");
332
333 emdtv_dtv_isoc_startall(sc);
334 splx(s);
335
336 return 0;
337 }
338
339 static int
340 emdtv_dtv_stop_transfer(void *priv)
341 {
342 struct emdtv_softc *sc = priv;
343
344 aprint_debug_dev(sc->sc_dev, "stopping stream\n");
345
346 sc->sc_streaming = false;
347
348 KERNEL_LOCK(1, curlwp);
349 if (sc->sc_isoc_pipe != NULL)
350 usbd_abort_pipe(sc->sc_isoc_pipe);
351 KERNEL_UNLOCK_ONE(curlwp);
352
353 sc->sc_dtvsubmitcb = NULL;
354 sc->sc_dtvsubmitarg = NULL;
355
356 return 0;
357 }
358
359 static void
360 emdtv_dtv_isoc_startall(struct emdtv_softc *sc)
361 {
362 int i;
363
364 if (sc->sc_streaming == false || sc->sc_dying == true)
365 return;
366
367 for (i = 0; i < EMDTV_NXFERS; i += 2)
368 emdtv_dtv_isoc_start(sc, &sc->sc_ix[i]);
369 }
370
371 static int
372 emdtv_dtv_isoc_start(struct emdtv_softc *sc, struct emdtv_isoc_xfer *ix)
373 {
374 int i;
375
376 if (sc->sc_isoc_pipe == NULL)
377 return EIO;
378
379 for (i = 0; i < EMDTV_NFRAMES; i++)
380 ix->ix_frlengths[i] = sc->sc_isoc_maxpacketsize;
381
382 usbd_setup_isoc_xfer(ix->ix_xfer,
383 ix,
384 ix->ix_frlengths,
385 EMDTV_NFRAMES,
386 USBD_SHORT_XFER_OK,
387 emdtv_dtv_isoc);
388
389 KERNEL_LOCK(1, curlwp);
390 usbd_transfer(ix->ix_xfer);
391 KERNEL_UNLOCK_ONE(curlwp);
392
393 return 0;
394 }
395
396 static void
397 emdtv_dtv_isoc(struct usbd_xfer *xfer, void * priv,
398 usbd_status err)
399 {
400 struct emdtv_isoc_xfer *ix = priv;
401 struct emdtv_softc *sc = ix->ix_sc;
402 struct dtv_payload payload;
403 struct usbd_pipe * isoc = sc->sc_isoc_pipe;
404 uint32_t len;
405 uint8_t *buf;
406 int i;
407
408 KASSERT(xfer == ix->ix_xfer);
409
410 if (sc->sc_dying || sc->sc_dtvsubmitcb == NULL)
411 return;
412
413 if (err) {
414 if (err == USBD_STALLED) {
415 usbd_clear_endpoint_stall_async(isoc);
416 goto resched;
417 }
418 return;
419 }
420
421 usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
422
423 if (len == 0)
424 goto resched;
425
426 buf = usbd_get_buffer(xfer);
427 if (buf == NULL)
428 goto resched;
429
430 for (i = 0; i < EMDTV_NFRAMES; i++, buf += sc->sc_isoc_maxpacketsize) {
431 if (ix->ix_frlengths[i] == 0)
432 continue;
433 payload.data = buf;
434 payload.size = ix->ix_frlengths[i];
435 sc->sc_dtvsubmitcb(sc->sc_dtvsubmitarg, &payload);
436 }
437
438 resched:
439 emdtv_dtv_isoc_start(sc, ix->ix_altix);
440 }
441
442 static int
443 emdtv_dtv_tuner_reset(void *opaque)
444 {
445 struct emdtv_softc *sc = opaque;
446 emdtv_gpio_ctl(sc, EMDTV_GPIO_TUNER1_RESET, true);
447 return 0;
448 }
449