Line data Source code
1 : /* $OpenBSD: bktr_os.c,v 1.32 2015/03/14 03:38:49 jsg Exp $ */
2 : /* $FreeBSD: src/sys/dev/bktr/bktr_os.c,v 1.20 2000/10/20 08:16:53 roger Exp $ */
3 :
4 : /*
5 : * This is part of the Driver for Video Capture Cards (Frame grabbers)
6 : * and TV Tuner cards using the Brooktree Bt848, Bt848A, Bt849A, Bt878, Bt879
7 : * chipset.
8 : * Copyright Roger Hardiman and Amancio Hasty.
9 : *
10 : * bktr_os : This has all the Operating System dependant code,
11 : * probe/attach and open/close/ioctl/read/mmap
12 : * memory allocation
13 : * PCI bus interfacing
14 : *
15 : *
16 : */
17 :
18 : /*
19 : * 1. Redistributions of source code must retain the
20 : * Copyright (c) 1997 Amancio Hasty, 1999 Roger Hardiman
21 : * All rights reserved.
22 : *
23 : * Redistribution and use in source and binary forms, with or without
24 : * modification, are permitted provided that the following conditions
25 : * are met:
26 : * 1. Redistributions of source code must retain the above copyright
27 : * notice, this list of conditions and the following disclaimer.
28 : * 2. Redistributions in binary form must reproduce the above copyright
29 : * notice, this list of conditions and the following disclaimer in the
30 : * documentation and/or other materials provided with the distribution.
31 : * 3. All advertising materials mentioning features or use of this software
32 : * must display the following acknowledgement:
33 : * This product includes software developed by Amancio Hasty and
34 : * Roger Hardiman
35 : * 4. The name of the author may not be used to endorse or promote products
36 : * derived from this software without specific prior written permission.
37 : *
38 : * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
39 : * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
40 : * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41 : * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
42 : * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
43 : * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
44 : * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
45 : * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
46 : * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
47 : * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
48 : * POSSIBILITY OF SUCH DAMAGE.
49 : */
50 :
51 : #define FIFO_RISC_DISABLED 0
52 : #define ALL_INTS_DISABLED 0
53 :
54 : #include "radio.h"
55 :
56 : #include <sys/param.h>
57 : #include <sys/systm.h>
58 : #include <sys/conf.h>
59 : #include <sys/uio.h>
60 : #include <sys/kernel.h>
61 : #include <sys/signalvar.h>
62 : #include <sys/mman.h>
63 : #include <sys/poll.h>
64 : #include <sys/selinfo.h>
65 : #include <sys/vnode.h>
66 : #if NRADIO > 0
67 : #include <sys/radioio.h>
68 : #include <dev/radio_if.h>
69 : #endif
70 :
71 : #include <uvm/uvm_extern.h>
72 :
73 : #include <sys/device.h>
74 : #include <dev/pci/pcivar.h>
75 : #include <dev/pci/pcireg.h>
76 : #include <dev/pci/pcidevs.h>
77 :
78 : #ifdef BKTR_DEBUG
79 : int bktr_debug = 1;
80 : #define DPR(x) (bktr_debug ? printf x : 0)
81 : #else
82 : #define DPR(x)
83 : #endif
84 :
85 : #include <dev/ic/bt8xx.h> /* OpenBSD location for .h files */
86 : #include <dev/pci/bktr/bktr_reg.h>
87 : #include <dev/pci/bktr/bktr_tuner.h>
88 : #include <dev/pci/bktr/bktr_audio.h>
89 : #include <dev/pci/bktr/bktr_core.h>
90 : #include <dev/pci/bktr/bktr_os.h>
91 :
92 : #define IPL_VIDEO IPL_BIO /* XXX */
93 :
94 0 : static int bktr_intr(void *arg) { return common_bktr_intr(arg); }
95 :
96 : #define bktr_open bktropen
97 : #define bktr_close bktrclose
98 : #define bktr_read bktrread
99 : #define bktr_write bktrwrite
100 : #define bktr_ioctl bktrioctl
101 : #define bktr_mmap bktrmmap
102 :
103 : int bktr_open(dev_t, int, int, struct proc *);
104 : int bktr_close(dev_t, int, int, struct proc *);
105 : int bktr_read(dev_t, struct uio *, int);
106 : int bktr_write(dev_t, struct uio *, int);
107 : int bktr_ioctl(dev_t, ioctl_cmd_t, caddr_t, int, struct proc *);
108 : paddr_t bktr_mmap(dev_t, off_t, int);
109 :
110 : static int bktr_probe(struct device *, void *, void *);
111 : static void bktr_attach(struct device *, struct device *, void *);
112 :
113 : struct cfattach bktr_ca = {
114 : sizeof(struct bktr_softc), bktr_probe, bktr_attach
115 : };
116 :
117 : struct cfdriver bktr_cd = {
118 : NULL, "bktr", DV_DULL
119 : };
120 :
121 : #if NRADIO > 0
122 : /* for radio(4) */
123 : int bktr_get_info(void *, struct radio_info *);
124 : int bktr_set_info(void *, struct radio_info *);
125 :
126 : struct radio_hw_if bktr_hw_if = {
127 : NULL, /* open */
128 : NULL, /* close */
129 : bktr_get_info,
130 : bktr_set_info,
131 : NULL /* search */
132 : };
133 : #endif
134 :
135 : int
136 0 : bktr_probe(parent, match, aux)
137 : struct device *parent;
138 : void *match;
139 : void *aux;
140 : {
141 0 : struct pci_attach_args *pa = aux;
142 :
143 0 : if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_BROOKTREE &&
144 0 : (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROOKTREE_BT848 ||
145 0 : PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROOKTREE_BT849 ||
146 0 : PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROOKTREE_BT878 ||
147 0 : PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BROOKTREE_BT879))
148 0 : return 1;
149 :
150 0 : return 0;
151 0 : }
152 :
153 :
154 : /*
155 : * the attach routine.
156 : */
157 : static void
158 0 : bktr_attach(struct device *parent, struct device *self, void *aux)
159 : {
160 : bktr_ptr_t bktr;
161 : u_int latency;
162 : u_int fun;
163 : unsigned int rev;
164 0 : struct pci_attach_args *pa = aux;
165 0 : pci_intr_handle_t ih;
166 : const char *intrstr;
167 : int retval;
168 : int unit;
169 :
170 0 : bktr = (bktr_ptr_t)self;
171 0 : unit = bktr->bktr_dev.dv_unit;
172 0 : bktr->dmat = pa->pa_dmat;
173 :
174 : /* Enable Back-to-Back
175 : XXX: check if all old DMA is stopped first (e.g. after warm
176 : boot) */
177 0 : fun = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
178 : DPR((" fun=%b", fun, PCI_COMMAND_STATUS_BITS));
179 0 : pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
180 0 : fun | PCI_COMMAND_BACKTOBACK_ENABLE);
181 :
182 : /*
183 : * map memory
184 : */
185 0 : retval = pci_mapreg_map(pa, PCI_MAPREG_START, PCI_MAPREG_TYPE_MEM |
186 0 : PCI_MAPREG_MEM_TYPE_32BIT, 0, &bktr->memt, &bktr->memh, NULL,
187 0 : &bktr->obmemsz, 0);
188 : DPR(("pci_mapreg_map: memt %lx, memh %lx, size %x\n",
189 : bktr->memt, bktr->memh, bktr->obmemsz));
190 0 : if (retval) {
191 0 : printf("%s: can't map mem space\n", bktr_name(bktr));
192 0 : return;
193 : }
194 :
195 : /*
196 : * Disable the brooktree device
197 : */
198 0 : OUTL(bktr, BKTR_INT_MASK, ALL_INTS_DISABLED);
199 0 : OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_RISC_DISABLED);
200 :
201 : /*
202 : * map interrupt
203 : */
204 0 : if (pci_intr_map(pa, &ih)) {
205 0 : printf("%s: can't map interrupt\n",
206 0 : bktr_name(bktr));
207 0 : return;
208 : }
209 0 : intrstr = pci_intr_string(pa->pa_pc, ih);
210 0 : bktr->ih = pci_intr_establish(pa->pa_pc, ih, IPL_VIDEO,
211 0 : bktr_intr, bktr, bktr->bktr_dev.dv_xname);
212 0 : if (bktr->ih == NULL) {
213 0 : printf("%s: can't establish interrupt",
214 0 : bktr_name(bktr));
215 0 : if (intrstr != NULL)
216 0 : printf(" at %s", intrstr);
217 0 : printf("\n");
218 0 : return;
219 : }
220 0 : if (intrstr != NULL)
221 0 : printf(": %s\n", intrstr);
222 :
223 : /*
224 : * PCI latency timer. 32 is a good value for 4 bus mastering slots, if
225 : * you have more than four, then 16 would probably be a better value.
226 : */
227 : #ifndef BROOKTREE_DEF_LATENCY_VALUE
228 : #define BROOKTREE_DEF_LATENCY_VALUE 0x10
229 : #endif
230 0 : latency = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_LATENCY_TIMER);
231 0 : latency = (latency >> 8) & 0xff;
232 :
233 0 : if (!latency) {
234 : if (bootverbose) {
235 : printf("%s: PCI bus latency was 0 changing to %d",
236 : bktr_name(bktr), BROOKTREE_DEF_LATENCY_VALUE);
237 : }
238 : latency = BROOKTREE_DEF_LATENCY_VALUE;
239 0 : pci_conf_write(pa->pa_pc, pa->pa_tag,
240 : PCI_LATENCY_TIMER, latency<<8);
241 0 : }
242 :
243 :
244 : /* read the pci id and determine the card type */
245 0 : fun = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_ID_REG);
246 0 : rev = PCI_REVISION(pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_CLASS_REG));
247 :
248 0 : common_bktr_attach(bktr, unit, fun, rev);
249 :
250 : #if NRADIO > 0
251 0 : if (bktr->card.tuner->pllControl[3] != 0x00)
252 0 : radio_attach_mi(&bktr_hw_if, bktr, &bktr->bktr_dev);
253 : #endif
254 0 : }
255 :
256 :
257 : /*
258 : * Special Memory Allocation
259 : */
260 : vaddr_t
261 0 : get_bktr_mem(bktr, dmapp, size)
262 : bktr_ptr_t bktr;
263 : bus_dmamap_t *dmapp;
264 : unsigned int size;
265 : {
266 0 : bus_dma_tag_t dmat = bktr->dmat;
267 0 : bus_dma_segment_t seg;
268 : bus_size_t align;
269 0 : int rseg;
270 0 : caddr_t kva;
271 :
272 : /*
273 : * Allocate a DMA area
274 : */
275 : align = 1 << 24;
276 0 : if (bus_dmamem_alloc(dmat, size, align, 0, &seg, 1,
277 : &rseg, BUS_DMA_NOWAIT)) {
278 : align = PAGE_SIZE;
279 0 : if (bus_dmamem_alloc(dmat, size, align, 0, &seg, 1,
280 : &rseg, BUS_DMA_NOWAIT)) {
281 0 : printf("%s: Unable to dmamem_alloc of %d bytes\n",
282 0 : bktr_name(bktr), size);
283 0 : return 0;
284 : }
285 : }
286 0 : if (bus_dmamem_map(dmat, &seg, rseg, size,
287 : &kva, BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) {
288 0 : printf("%s: Unable to dmamem_map of %d bytes\n",
289 0 : bktr_name(bktr), size);
290 0 : bus_dmamem_free(dmat, &seg, rseg);
291 0 : return 0;
292 : }
293 : /*
294 : * Create and locd the DMA map for the DMA area
295 : */
296 0 : if (bus_dmamap_create(dmat, size, 1, size, 0, BUS_DMA_NOWAIT, dmapp)) {
297 0 : printf("%s: Unable to dmamap_create of %d bytes\n",
298 0 : bktr_name(bktr), size);
299 0 : bus_dmamem_unmap(dmat, kva, size);
300 0 : bus_dmamem_free(dmat, &seg, rseg);
301 0 : return 0;
302 : }
303 0 : if (bus_dmamap_load(dmat, *dmapp, kva, size, NULL, BUS_DMA_NOWAIT)) {
304 0 : printf("%s: Unable to dmamap_load of %d bytes\n",
305 0 : bktr_name(bktr), size);
306 0 : bus_dmamem_unmap(dmat, kva, size);
307 0 : bus_dmamem_free(dmat, &seg, rseg);
308 0 : bus_dmamap_destroy(dmat, *dmapp);
309 0 : return 0;
310 : }
311 0 : return (vaddr_t)kva;
312 0 : }
313 :
314 : void
315 0 : free_bktr_mem(bktr, dmap, kva)
316 : bktr_ptr_t bktr;
317 : bus_dmamap_t dmap;
318 : vaddr_t kva;
319 : {
320 0 : bus_dma_tag_t dmat = bktr->dmat;
321 :
322 0 : bus_dmamem_unmap(dmat, (caddr_t)kva, dmap->dm_mapsize);
323 0 : bus_dmamem_free(dmat, dmap->dm_segs, 1);
324 0 : bus_dmamap_destroy(dmat, dmap);
325 0 : }
326 :
327 :
328 : /*---------------------------------------------------------
329 : **
330 : ** BrookTree 848 character device driver routines
331 : **
332 : **---------------------------------------------------------
333 : */
334 :
335 :
336 : #define VIDEO_DEV 0x00
337 : #define TUNER_DEV 0x01
338 : #define VBI_DEV 0x02
339 :
340 : #define UNIT(x) (minor((x) & 0x0f))
341 : #define FUNCTION(x) (minor((x >> 4) & 0x0f))
342 :
343 : /*
344 : *
345 : */
346 : int
347 0 : bktr_open(dev_t dev, int flags, int fmt, struct proc *p)
348 : {
349 : bktr_ptr_t bktr;
350 : int unit;
351 :
352 0 : unit = UNIT(dev);
353 :
354 : /* unit out of range */
355 0 : if ((unit >= bktr_cd.cd_ndevs) || (bktr_cd.cd_devs[unit] == NULL))
356 0 : return(ENXIO);
357 :
358 0 : bktr = bktr_cd.cd_devs[unit];
359 :
360 0 : if (!(bktr->flags & METEOR_INITIALIZED)) /* device not found */
361 0 : return(ENXIO);
362 :
363 0 : switch (FUNCTION(dev)) {
364 : case VIDEO_DEV:
365 0 : return(video_open(bktr));
366 : case TUNER_DEV:
367 0 : return(tuner_open(bktr));
368 : case VBI_DEV:
369 0 : return(vbi_open(bktr));
370 : }
371 :
372 0 : return(ENXIO);
373 0 : }
374 :
375 :
376 : /*
377 : *
378 : */
379 : int
380 0 : bktr_close(dev_t dev, int flags, int fmt, struct proc *p)
381 : {
382 : bktr_ptr_t bktr;
383 : int unit;
384 :
385 0 : unit = UNIT(dev);
386 :
387 0 : bktr = bktr_cd.cd_devs[unit];
388 :
389 0 : switch (FUNCTION(dev)) {
390 : case VIDEO_DEV:
391 0 : return(video_close(bktr));
392 : case TUNER_DEV:
393 0 : return(tuner_close(bktr));
394 : case VBI_DEV:
395 0 : return(vbi_close(bktr));
396 : }
397 :
398 0 : return(ENXIO);
399 0 : }
400 :
401 : /*
402 : *
403 : */
404 : int
405 0 : bktr_read(dev_t dev, struct uio *uio, int ioflag)
406 : {
407 : bktr_ptr_t bktr;
408 : int unit;
409 :
410 0 : unit = UNIT(dev);
411 :
412 0 : bktr = bktr_cd.cd_devs[unit];
413 :
414 0 : switch (FUNCTION(dev)) {
415 : case VIDEO_DEV:
416 0 : return(video_read(bktr, unit, dev, uio));
417 : case VBI_DEV:
418 0 : return(vbi_read(bktr, uio, ioflag));
419 : }
420 :
421 0 : return(ENXIO);
422 0 : }
423 :
424 :
425 : /*
426 : *
427 : */
428 : int
429 0 : bktr_write(dev_t dev, struct uio *uio, int ioflag)
430 : {
431 : /* operation not supported */
432 0 : return(EOPNOTSUPP);
433 : }
434 :
435 : /*
436 : *
437 : */
438 : int
439 0 : bktr_ioctl(dev_t dev, ioctl_cmd_t cmd, caddr_t arg, int flag, struct proc* pr)
440 : {
441 : bktr_ptr_t bktr;
442 : int unit;
443 :
444 0 : unit = UNIT(dev);
445 :
446 0 : bktr = bktr_cd.cd_devs[unit];
447 :
448 0 : if (bktr->bigbuf == 0) /* no frame buffer allocated (ioctl failed) */
449 0 : return(ENOMEM);
450 :
451 0 : switch (FUNCTION(dev)) {
452 : case VIDEO_DEV:
453 0 : return(video_ioctl(bktr, unit, cmd, arg, pr));
454 : case TUNER_DEV:
455 0 : return(tuner_ioctl(bktr, unit, cmd, arg, pr));
456 : }
457 :
458 0 : return(ENXIO);
459 0 : }
460 :
461 : /*
462 : *
463 : */
464 : paddr_t
465 0 : bktr_mmap(dev_t dev, off_t offset, int nprot)
466 : {
467 : int unit;
468 : bktr_ptr_t bktr;
469 :
470 0 : unit = UNIT(dev);
471 :
472 0 : if (FUNCTION(dev) > 0) /* only allow mmap on /dev/bktr[n] */
473 0 : return(-1);
474 :
475 0 : bktr = bktr_cd.cd_devs[unit];
476 :
477 0 : if (offset < 0)
478 0 : return(-1);
479 :
480 0 : if (offset >= bktr->alloc_pages * PAGE_SIZE)
481 0 : return(-1);
482 :
483 0 : return (bus_dmamem_mmap(bktr->dmat, bktr->dm_mem->dm_segs, 1,
484 : offset, nprot, BUS_DMA_WAITOK));
485 0 : }
486 :
487 : #if NRADIO > 0
488 : int
489 0 : bktr_set_info(void *v, struct radio_info *ri)
490 : {
491 0 : struct bktr_softc *sc = v;
492 0 : struct TVTUNER *tv = &sc->tuner;
493 : u_int32_t freq;
494 : u_int32_t chan;
495 :
496 0 : if (ri->mute) {
497 : /* mute the audio stream by switching the mux */
498 0 : set_audio(sc, AUDIO_MUTE);
499 0 : } else {
500 : /* unmute the audio stream */
501 0 : set_audio(sc, AUDIO_UNMUTE);
502 0 : init_audio_devices(sc);
503 : }
504 :
505 0 : set_audio(sc, AUDIO_INTERN); /* use internal audio */
506 0 : temp_mute(sc, TRUE);
507 :
508 0 : if (ri->tuner_mode == RADIO_TUNER_MODE_TV) {
509 0 : if (ri->chan) {
510 0 : if (ri->chan < MIN_TV_CHAN)
511 0 : ri->chan = MIN_TV_CHAN;
512 0 : if (ri->chan > MAX_TV_CHAN)
513 0 : ri->chan = MAX_TV_CHAN;
514 :
515 0 : chan = ri->chan;
516 0 : ri->chan = tv_channel(sc, chan);
517 0 : tv->tuner_mode = BT848_TUNER_MODE_TV;
518 0 : } else {
519 0 : ri->chan = tv->channel;
520 : }
521 : } else {
522 0 : if (ri->freq) {
523 0 : if (ri->freq < MIN_FM_FREQ)
524 0 : ri->freq = MIN_FM_FREQ;
525 0 : if (ri->freq > MAX_FM_FREQ)
526 0 : ri->freq = MAX_FM_FREQ;
527 :
528 0 : freq = ri->freq / 10;
529 0 : ri->freq = tv_freq(sc, freq, FM_RADIO_FREQUENCY) * 10;
530 0 : tv->tuner_mode = BT848_TUNER_MODE_RADIO;
531 0 : } else {
532 0 : ri->freq = tv->frequency;
533 : }
534 : }
535 :
536 0 : if (ri->chnlset >= CHNLSET_MIN && ri->chnlset <= CHNLSET_MAX)
537 0 : tv->chnlset = ri->chnlset;
538 : else
539 0 : tv->chnlset = DEFAULT_CHNLSET;
540 :
541 0 : temp_mute(sc, FALSE);
542 :
543 0 : return (0);
544 : }
545 :
546 : int
547 0 : bktr_get_info(void *v, struct radio_info *ri)
548 : {
549 0 : struct bktr_softc *sc = v;
550 0 : struct TVTUNER *tv = &sc->tuner;
551 : int status;
552 :
553 0 : status = get_tuner_status(sc);
554 :
555 : #define STATUSBIT_STEREO 0x10
556 0 : ri->mute = (int)sc->audio_mute_state ? 1 : 0;
557 0 : ri->caps = RADIO_CAPS_DETECT_STEREO | RADIO_CAPS_HW_AFC;
558 0 : ri->info = (status & STATUSBIT_STEREO) ? RADIO_INFO_STEREO : 0;
559 :
560 : /* not yet supported */
561 0 : ri->volume = ri->rfreq = ri->lock = 0;
562 :
563 0 : switch (tv->tuner_mode) {
564 : case BT848_TUNER_MODE_TV:
565 0 : ri->tuner_mode = RADIO_TUNER_MODE_TV;
566 0 : ri->freq = tv->frequency * 1000 / 16;
567 0 : break;
568 : case BT848_TUNER_MODE_RADIO:
569 0 : ri->tuner_mode = RADIO_TUNER_MODE_RADIO;
570 0 : ri->freq = tv->frequency * 10;
571 0 : break;
572 : }
573 :
574 : /*
575 : * The field ri->stereo is used to forcible switch to
576 : * mono/stereo, not as an indicator of received signal quality.
577 : * The ri->info is for that purpose.
578 : */
579 0 : ri->stereo = 1; /* Can't switch to mono, always stereo */
580 :
581 0 : ri->chan = tv->channel;
582 0 : ri->chnlset = tv->chnlset;
583 :
584 0 : return (0);
585 : }
586 : #endif /* NRADIO */
|