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