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