emdtv_dtv.c revision 1.1 1 1.1 jmcneill /* $NetBSD: emdtv_dtv.c,v 1.1 2011/07/11 18:02:04 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2008, 2011 Jared D. McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
14 1.1 jmcneill * documentation and/or other materials provided with the distribution.
15 1.1 jmcneill *
16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 jmcneill * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 jmcneill * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 jmcneill * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 jmcneill * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 jmcneill * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 jmcneill * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 jmcneill * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 jmcneill * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 jmcneill * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 jmcneill * POSSIBILITY OF SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.1 jmcneill #include <sys/cdefs.h>
30 1.1 jmcneill __KERNEL_RCSID(0, "$NetBSD: emdtv_dtv.c,v 1.1 2011/07/11 18:02:04 jmcneill Exp $");
31 1.1 jmcneill
32 1.1 jmcneill #include <sys/param.h>
33 1.1 jmcneill #include <sys/systm.h>
34 1.1 jmcneill #include <sys/device.h>
35 1.1 jmcneill #include <sys/conf.h>
36 1.1 jmcneill
37 1.1 jmcneill #include <dev/usb/usb.h>
38 1.1 jmcneill #include <dev/usb/usbdi.h>
39 1.1 jmcneill #include <dev/usb/usbdi_util.h>
40 1.1 jmcneill #include <dev/usb/usbdevs.h>
41 1.1 jmcneill
42 1.1 jmcneill #include <dev/i2c/i2cvar.h>
43 1.1 jmcneill
44 1.1 jmcneill #include <dev/usb/emdtvvar.h>
45 1.1 jmcneill #include <dev/usb/emdtvreg.h>
46 1.1 jmcneill
47 1.1 jmcneill static void emdtv_dtv_get_devinfo(void *,
48 1.1 jmcneill struct dvb_frontend_info *);
49 1.1 jmcneill static int emdtv_dtv_open(void *, int);
50 1.1 jmcneill static void emdtv_dtv_close(void *);
51 1.1 jmcneill static int emdtv_dtv_set_tuner(void *,
52 1.1 jmcneill const struct dvb_frontend_parameters *);
53 1.1 jmcneill static fe_status_t emdtv_dtv_get_status(void *);
54 1.1 jmcneill static uint16_t emdtv_dtv_get_signal_strength(void *);
55 1.1 jmcneill static uint16_t emdtv_dtv_get_snr(void *);
56 1.1 jmcneill static int emdtv_dtv_start_transfer(void *);
57 1.1 jmcneill static int emdtv_dtv_stop_transfer(void *);
58 1.1 jmcneill
59 1.1 jmcneill static int emdtv_dtv_tuner_reset(void *);
60 1.1 jmcneill
61 1.1 jmcneill static void emdtv_dtv_isoc_startall(struct emdtv_softc *);
62 1.1 jmcneill static int emdtv_dtv_isoc_start(struct emdtv_softc *,
63 1.1 jmcneill struct emdtv_isoc_xfer *);
64 1.1 jmcneill static void emdtv_dtv_isoc(usbd_xfer_handle, usbd_private_handle,
65 1.1 jmcneill usbd_status);
66 1.1 jmcneill
67 1.1 jmcneill static const struct dtv_hw_if emdtv_dtv_if = {
68 1.1 jmcneill .get_devinfo = emdtv_dtv_get_devinfo,
69 1.1 jmcneill .open = emdtv_dtv_open,
70 1.1 jmcneill .close = emdtv_dtv_close,
71 1.1 jmcneill .set_tuner = emdtv_dtv_set_tuner,
72 1.1 jmcneill .get_status = emdtv_dtv_get_status,
73 1.1 jmcneill .get_signal_strength = emdtv_dtv_get_signal_strength,
74 1.1 jmcneill .get_snr = emdtv_dtv_get_snr,
75 1.1 jmcneill .start_transfer = emdtv_dtv_start_transfer,
76 1.1 jmcneill .stop_transfer = emdtv_dtv_stop_transfer,
77 1.1 jmcneill };
78 1.1 jmcneill
79 1.1 jmcneill void
80 1.1 jmcneill emdtv_dtv_attach(struct emdtv_softc *sc)
81 1.1 jmcneill {
82 1.1 jmcneill usb_endpoint_descriptor_t *ed;
83 1.1 jmcneill usbd_status status;
84 1.1 jmcneill struct dtv_attach_args daa;
85 1.1 jmcneill int i;
86 1.1 jmcneill
87 1.1 jmcneill for (i = 0; i < EMDTV_NXFERS; i++) {
88 1.1 jmcneill sc->sc_ix[i].ix_altix = (i & 1) ?
89 1.1 jmcneill &sc->sc_ix[i - 1] : &sc->sc_ix[i + 1];
90 1.1 jmcneill sc->sc_ix[i].ix_sc = sc;
91 1.1 jmcneill }
92 1.1 jmcneill
93 1.1 jmcneill ed = usbd_interface2endpoint_descriptor(sc->sc_iface, 3);
94 1.1 jmcneill if (ed == NULL) {
95 1.1 jmcneill aprint_error_dev(sc->sc_dev, "couldn't find endpoint 3\n");
96 1.1 jmcneill return;
97 1.1 jmcneill }
98 1.1 jmcneill sc->sc_isoc_maxpacketsize = UGETW(ed->wMaxPacketSize);
99 1.1 jmcneill sc->sc_isoc_buflen = sc->sc_isoc_maxpacketsize * EMDTV_NFRAMES;
100 1.1 jmcneill
101 1.1 jmcneill aprint_debug_dev(sc->sc_dev, "calling usbd_open_pipe, ep 0x%02x\n",
102 1.1 jmcneill ed->bEndpointAddress);
103 1.1 jmcneill status = usbd_open_pipe(sc->sc_iface,
104 1.1 jmcneill ed->bEndpointAddress, USBD_EXCLUSIVE_USE,
105 1.1 jmcneill &sc->sc_isoc_pipe);
106 1.1 jmcneill if (status != USBD_NORMAL_COMPLETION) {
107 1.1 jmcneill aprint_error_dev(sc->sc_dev, "couldn't open isoc pipe\n");
108 1.1 jmcneill usbd_set_interface(sc->sc_iface, 0);
109 1.1 jmcneill return;
110 1.1 jmcneill }
111 1.1 jmcneill
112 1.1 jmcneill emdtv_write_1(sc, UR_GET_STATUS, 0x48, 0x00);
113 1.1 jmcneill emdtv_write_1(sc, UR_GET_STATUS, 0x12, 0x77);
114 1.1 jmcneill usbd_delay_ms(sc->sc_udev, 6);
115 1.1 jmcneill
116 1.1 jmcneill emdtv_gpio_ctl(sc, EMDTV_GPIO_ANALOG_ON, false);
117 1.1 jmcneill emdtv_gpio_ctl(sc, EMDTV_GPIO_TS1_ON, true);
118 1.1 jmcneill emdtv_gpio_ctl(sc, EMDTV_GPIO_TUNER1_ON, true);
119 1.1 jmcneill emdtv_gpio_ctl(sc, EMDTV_GPIO_DEMOD1_RESET, true);
120 1.1 jmcneill usbd_delay_ms(sc->sc_udev, 100);
121 1.1 jmcneill
122 1.1 jmcneill daa.hw = &emdtv_dtv_if;
123 1.1 jmcneill daa.priv = sc;
124 1.1 jmcneill sc->sc_dtvdev = config_found_ia(sc->sc_dev, "dtvbus", &daa, dtv_print);
125 1.1 jmcneill }
126 1.1 jmcneill
127 1.1 jmcneill void
128 1.1 jmcneill emdtv_dtv_detach(struct emdtv_softc *sc, int flags)
129 1.1 jmcneill {
130 1.1 jmcneill sc->sc_streaming = false;
131 1.1 jmcneill
132 1.1 jmcneill if (sc->sc_dtvdev != NULL) {
133 1.1 jmcneill config_detach(sc->sc_dtvdev, flags);
134 1.1 jmcneill sc->sc_dtvdev = NULL;
135 1.1 jmcneill }
136 1.1 jmcneill
137 1.1 jmcneill if (sc->sc_xc3028)
138 1.1 jmcneill xc3028_close(sc->sc_xc3028);
139 1.1 jmcneill if (sc->sc_lg3303)
140 1.1 jmcneill lg3303_close(sc->sc_lg3303);
141 1.1 jmcneill
142 1.1 jmcneill if (sc->sc_isoc_pipe) {
143 1.1 jmcneill usbd_abort_pipe(sc->sc_isoc_pipe);
144 1.1 jmcneill usbd_close_pipe(sc->sc_isoc_pipe);
145 1.1 jmcneill sc->sc_isoc_pipe = NULL;
146 1.1 jmcneill }
147 1.1 jmcneill }
148 1.1 jmcneill
149 1.1 jmcneill static void
150 1.1 jmcneill emdtv_dtv_get_devinfo(void *priv, struct dvb_frontend_info *info)
151 1.1 jmcneill {
152 1.1 jmcneill struct emdtv_softc *sc = priv;
153 1.1 jmcneill
154 1.1 jmcneill memset(info, 0, sizeof(*info));
155 1.1 jmcneill strlcpy(info->name, sc->sc_board->eb_name, sizeof(info->name));
156 1.1 jmcneill info->type = FE_ATSC;
157 1.1 jmcneill info->frequency_min = 54000000;
158 1.1 jmcneill info->frequency_max = 858000000;
159 1.1 jmcneill info->frequency_stepsize = 62500;
160 1.1 jmcneill info->caps = FE_CAN_8VSB;
161 1.1 jmcneill }
162 1.1 jmcneill
163 1.1 jmcneill static int
164 1.1 jmcneill emdtv_dtv_open(void *priv, int flags)
165 1.1 jmcneill {
166 1.1 jmcneill struct emdtv_softc *sc = priv;
167 1.1 jmcneill
168 1.1 jmcneill if (sc->sc_dying)
169 1.1 jmcneill return ENXIO;
170 1.1 jmcneill
171 1.1 jmcneill switch (sc->sc_board->eb_tuner) {
172 1.1 jmcneill case EMDTV_TUNER_XC3028L:
173 1.1 jmcneill if (sc->sc_xc3028 == NULL) {
174 1.1 jmcneill sc->sc_xc3028 = xc3028_open(sc->sc_dev,
175 1.1 jmcneill &sc->sc_i2c, 0x61 << 1, emdtv_dtv_tuner_reset, sc,
176 1.1 jmcneill XC3028L);
177 1.1 jmcneill }
178 1.1 jmcneill if (sc->sc_xc3028 == NULL) {
179 1.1 jmcneill aprint_error_dev(sc->sc_dev, "couldn't open xc3028l\n");
180 1.1 jmcneill return ENXIO;
181 1.1 jmcneill }
182 1.1 jmcneill break;
183 1.1 jmcneill default:
184 1.1 jmcneill aprint_error_dev(sc->sc_dev, "unsupported tuner (%d)\n",
185 1.1 jmcneill sc->sc_board->eb_tuner);
186 1.1 jmcneill return EIO;
187 1.1 jmcneill }
188 1.1 jmcneill
189 1.1 jmcneill switch (sc->sc_board->eb_demod) {
190 1.1 jmcneill case EMDTV_DEMOD_LG3303:
191 1.1 jmcneill if (sc->sc_lg3303 == NULL) {
192 1.1 jmcneill sc->sc_lg3303 = lg3303_open(sc->sc_dev,
193 1.1 jmcneill &sc->sc_i2c, 0x1c);
194 1.1 jmcneill }
195 1.1 jmcneill if (sc->sc_lg3303 == NULL) {
196 1.1 jmcneill aprint_error_dev(sc->sc_dev, "couldn't open lg3303\n");
197 1.1 jmcneill return ENXIO;
198 1.1 jmcneill }
199 1.1 jmcneill break;
200 1.1 jmcneill default:
201 1.1 jmcneill aprint_error_dev(sc->sc_dev, "unsupported demod (%d)\n",
202 1.1 jmcneill sc->sc_board->eb_demod);
203 1.1 jmcneill return EIO;
204 1.1 jmcneill }
205 1.1 jmcneill
206 1.1 jmcneill return 0;
207 1.1 jmcneill }
208 1.1 jmcneill
209 1.1 jmcneill static void
210 1.1 jmcneill emdtv_dtv_close(void *priv)
211 1.1 jmcneill {
212 1.1 jmcneill return;
213 1.1 jmcneill }
214 1.1 jmcneill
215 1.1 jmcneill static int
216 1.1 jmcneill emdtv_dtv_set_tuner(void *priv, const struct dvb_frontend_parameters *params)
217 1.1 jmcneill {
218 1.1 jmcneill struct emdtv_softc *sc = priv;
219 1.1 jmcneill int error;
220 1.1 jmcneill
221 1.1 jmcneill /* Setup demod */
222 1.1 jmcneill error = ENXIO;
223 1.1 jmcneill if (sc->sc_lg3303)
224 1.1 jmcneill error = lg3303_set_modulation(sc->sc_lg3303,
225 1.1 jmcneill params->u.vsb.modulation);
226 1.1 jmcneill if (error)
227 1.1 jmcneill return error;
228 1.1 jmcneill
229 1.1 jmcneill /* Setup tuner */
230 1.1 jmcneill error = ENXIO;
231 1.1 jmcneill if (sc->sc_xc3028)
232 1.1 jmcneill error = xc3028_tune_dtv(sc->sc_xc3028, params);
233 1.1 jmcneill
234 1.1 jmcneill return error;
235 1.1 jmcneill }
236 1.1 jmcneill
237 1.1 jmcneill static fe_status_t
238 1.1 jmcneill emdtv_dtv_get_status(void *priv)
239 1.1 jmcneill {
240 1.1 jmcneill struct emdtv_softc *sc = priv;
241 1.1 jmcneill
242 1.1 jmcneill if (sc->sc_lg3303)
243 1.1 jmcneill return lg3303_get_dtv_status(sc->sc_lg3303);
244 1.1 jmcneill
245 1.1 jmcneill return 0;
246 1.1 jmcneill }
247 1.1 jmcneill
248 1.1 jmcneill uint16_t
249 1.1 jmcneill emdtv_dtv_get_signal_strength(void *priv)
250 1.1 jmcneill {
251 1.1 jmcneill return 0;
252 1.1 jmcneill }
253 1.1 jmcneill
254 1.1 jmcneill uint16_t
255 1.1 jmcneill emdtv_dtv_get_snr(void *priv)
256 1.1 jmcneill {
257 1.1 jmcneill return 0;
258 1.1 jmcneill }
259 1.1 jmcneill
260 1.1 jmcneill static int
261 1.1 jmcneill emdtv_dtv_start_transfer(void *priv)
262 1.1 jmcneill {
263 1.1 jmcneill struct emdtv_softc *sc = priv;
264 1.1 jmcneill int i, s;
265 1.1 jmcneill
266 1.1 jmcneill s = splusb();
267 1.1 jmcneill
268 1.1 jmcneill sc->sc_streaming = true;
269 1.1 jmcneill
270 1.1 jmcneill aprint_debug_dev(sc->sc_dev, "allocating isoc xfers (pktsz %d)\n",
271 1.1 jmcneill sc->sc_isoc_maxpacketsize);
272 1.1 jmcneill
273 1.1 jmcneill for (i = 0; i < EMDTV_NXFERS; i++) {
274 1.1 jmcneill sc->sc_ix[i].ix_xfer = usbd_alloc_xfer(sc->sc_udev);
275 1.1 jmcneill sc->sc_ix[i].ix_buf = usbd_alloc_buffer(sc->sc_ix[i].ix_xfer,
276 1.1 jmcneill sc->sc_isoc_buflen);
277 1.1 jmcneill aprint_debug_dev(sc->sc_dev, " ix[%d] xfer %p buf %p\n",
278 1.1 jmcneill i, sc->sc_ix[i].ix_xfer, sc->sc_ix[i].ix_buf);
279 1.1 jmcneill }
280 1.1 jmcneill
281 1.1 jmcneill aprint_debug_dev(sc->sc_dev, "starting isoc transactions\n");
282 1.1 jmcneill
283 1.1 jmcneill emdtv_dtv_isoc_startall(sc);
284 1.1 jmcneill splx(s);
285 1.1 jmcneill
286 1.1 jmcneill return 0;
287 1.1 jmcneill }
288 1.1 jmcneill
289 1.1 jmcneill static int
290 1.1 jmcneill emdtv_dtv_stop_transfer(void *priv)
291 1.1 jmcneill {
292 1.1 jmcneill struct emdtv_softc *sc = priv;
293 1.1 jmcneill int i;
294 1.1 jmcneill
295 1.1 jmcneill aprint_debug_dev(sc->sc_dev, "stopping stream\n");
296 1.1 jmcneill
297 1.1 jmcneill sc->sc_streaming = false;
298 1.1 jmcneill
299 1.1 jmcneill if (sc->sc_isoc_pipe != NULL)
300 1.1 jmcneill usbd_abort_pipe(sc->sc_isoc_pipe);
301 1.1 jmcneill
302 1.1 jmcneill for (i = 0; i < EMDTV_NXFERS; i++)
303 1.1 jmcneill if (sc->sc_ix[i].ix_xfer) {
304 1.1 jmcneill usbd_free_xfer(sc->sc_ix[i].ix_xfer);
305 1.1 jmcneill sc->sc_ix[i].ix_xfer = NULL;
306 1.1 jmcneill sc->sc_ix[i].ix_buf = NULL;
307 1.1 jmcneill }
308 1.1 jmcneill
309 1.1 jmcneill return 0;
310 1.1 jmcneill }
311 1.1 jmcneill
312 1.1 jmcneill static void
313 1.1 jmcneill emdtv_dtv_isoc_startall(struct emdtv_softc *sc)
314 1.1 jmcneill {
315 1.1 jmcneill int i;
316 1.1 jmcneill
317 1.1 jmcneill if (sc->sc_streaming == false || sc->sc_dying == true)
318 1.1 jmcneill return;
319 1.1 jmcneill
320 1.1 jmcneill for (i = 0; i < EMDTV_NXFERS; i += 2)
321 1.1 jmcneill emdtv_dtv_isoc_start(sc, &sc->sc_ix[i]);
322 1.1 jmcneill }
323 1.1 jmcneill
324 1.1 jmcneill static int
325 1.1 jmcneill emdtv_dtv_isoc_start(struct emdtv_softc *sc, struct emdtv_isoc_xfer *ix)
326 1.1 jmcneill {
327 1.1 jmcneill int i;
328 1.1 jmcneill
329 1.1 jmcneill if (sc->sc_isoc_pipe == NULL)
330 1.1 jmcneill return EIO;
331 1.1 jmcneill
332 1.1 jmcneill for (i = 0; i < EMDTV_NFRAMES; i++)
333 1.1 jmcneill ix->ix_frlengths[i] = sc->sc_isoc_maxpacketsize;
334 1.1 jmcneill
335 1.1 jmcneill usbd_setup_isoc_xfer(ix->ix_xfer,
336 1.1 jmcneill sc->sc_isoc_pipe,
337 1.1 jmcneill ix,
338 1.1 jmcneill ix->ix_frlengths,
339 1.1 jmcneill EMDTV_NFRAMES,
340 1.1 jmcneill USBD_NO_COPY | USBD_SHORT_XFER_OK,
341 1.1 jmcneill emdtv_dtv_isoc);
342 1.1 jmcneill usbd_transfer(ix->ix_xfer);
343 1.1 jmcneill
344 1.1 jmcneill return 0;
345 1.1 jmcneill }
346 1.1 jmcneill
347 1.1 jmcneill static void
348 1.1 jmcneill emdtv_dtv_isoc(usbd_xfer_handle xfer, usbd_private_handle priv,
349 1.1 jmcneill usbd_status err)
350 1.1 jmcneill {
351 1.1 jmcneill struct emdtv_isoc_xfer *ix = priv;
352 1.1 jmcneill struct emdtv_softc *sc = ix->ix_sc;
353 1.1 jmcneill struct dtv_payload payload;
354 1.1 jmcneill usbd_pipe_handle isoc = sc->sc_isoc_pipe;
355 1.1 jmcneill uint32_t len;
356 1.1 jmcneill uint8_t *buf;
357 1.1 jmcneill int i;
358 1.1 jmcneill
359 1.1 jmcneill KASSERT(xfer == ix->ix_xfer);
360 1.1 jmcneill
361 1.1 jmcneill if (sc->sc_dying)
362 1.1 jmcneill return;
363 1.1 jmcneill
364 1.1 jmcneill if (err) {
365 1.1 jmcneill if (err == USBD_STALLED) {
366 1.1 jmcneill usbd_clear_endpoint_stall_async(isoc);
367 1.1 jmcneill goto resched;
368 1.1 jmcneill }
369 1.1 jmcneill return;
370 1.1 jmcneill }
371 1.1 jmcneill
372 1.1 jmcneill usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
373 1.1 jmcneill
374 1.1 jmcneill if (len == 0)
375 1.1 jmcneill goto resched;
376 1.1 jmcneill
377 1.1 jmcneill buf = usbd_get_buffer(xfer);
378 1.1 jmcneill if (buf == NULL)
379 1.1 jmcneill goto resched;
380 1.1 jmcneill
381 1.1 jmcneill for (i = 0; i < EMDTV_NFRAMES; i++, buf += sc->sc_isoc_maxpacketsize) {
382 1.1 jmcneill if (ix->ix_frlengths[i] == 0)
383 1.1 jmcneill continue;
384 1.1 jmcneill payload.data = buf;
385 1.1 jmcneill payload.size = ix->ix_frlengths[i];
386 1.1 jmcneill dtv_submit_payload(sc->sc_dtvdev, &payload);
387 1.1 jmcneill }
388 1.1 jmcneill
389 1.1 jmcneill resched:
390 1.1 jmcneill emdtv_dtv_isoc_start(sc, ix->ix_altix);
391 1.1 jmcneill }
392 1.1 jmcneill
393 1.1 jmcneill static int
394 1.1 jmcneill emdtv_dtv_tuner_reset(void *opaque)
395 1.1 jmcneill {
396 1.1 jmcneill struct emdtv_softc *sc = opaque;
397 1.1 jmcneill emdtv_gpio_ctl(sc, EMDTV_GPIO_TUNER1_RESET, true);
398 1.1 jmcneill return 0;
399 1.1 jmcneill }
400