fe0ce82f592b7a2da13982d195eb40d2a431b3c6
[dragonfly.git] / sys / dev / video / bktr / bktr_os.c
1 /*-
2  * 1. Redistributions of source code must retain the 
3  * Copyright (c) 1997 Amancio Hasty, 1999 Roger Hardiman
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *      This product includes software developed by Amancio Hasty and
17  *      Roger Hardiman
18  * 4. The name of the author may not be used to endorse or promote products 
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  * $FreeBSD: src/sys/dev/bktr/bktr_os.c,v 1.54 2007/02/23 12:18:34 piso Exp $
34  * $DragonFly: src/sys/dev/video/bktr/bktr_os.c,v 1.21 2008/05/18 03:02:53 pavalos Exp $
35  */
36
37 /*
38  * This is part of the Driver for Video Capture Cards (Frame grabbers)
39  * and TV Tuner cards using the Brooktree Bt848, Bt848A, Bt849A, Bt878, Bt879
40  * chipset.
41  * Copyright Roger Hardiman and Amancio Hasty.
42  *
43  * bktr_os : This has all the Operating System dependant code,
44  *             probe/attach and open/close/ioctl/read/mmap
45  *             memory allocation
46  *             PCI bus interfacing
47  */
48
49 #include "opt_bktr.h"           /* include any kernel config options */
50
51 #define FIFO_RISC_DISABLED      0
52 #define ALL_INTS_DISABLED       0
53
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/conf.h>
57 #include <sys/device.h>
58 #include <sys/uio.h>
59 #include <sys/kernel.h>
60 #include <sys/signalvar.h>
61 #include <sys/malloc.h>
62 #include <sys/mman.h>
63 #include <sys/poll.h>
64 #include <sys/select.h>
65 #include <sys/event.h>
66 #include <sys/bus.h>
67 #include <sys/rman.h>
68 #include <sys/thread2.h>
69 #include <sys/selinfo.h>
70
71 #include <vm/vm.h>
72 #include <vm/vm_kern.h>
73 #include <vm/pmap.h>
74 #include <vm/vm_extern.h>
75
76 #include <bus/pci/pcivar.h>
77 #include <bus/pci/pcireg.h>
78 #include <bus/pci/pcidevs.h>
79
80 #include <sys/sysctl.h>
81 int bt848_card = -1; 
82 int bt848_tuner = -1;
83 int bt848_reverse_mute = -1; 
84 int bt848_format = -1;
85 int bt848_slow_msp_audio = -1;
86 #ifdef BKTR_NEW_MSP34XX_DRIVER
87 int bt848_stereo_once = 0;      /* no continuous stereo monitoring */
88 int bt848_amsound = 0;          /* hard-wire AM sound at 6.5 Hz (france),
89                                    the autoscan seems work well only with FM... */
90 int bt848_dolby = 0;
91 #endif
92
93 SYSCTL_NODE(_hw, OID_AUTO, bt848, CTLFLAG_RW, 0, "Bt848 Driver mgmt");
94 SYSCTL_INT(_hw_bt848, OID_AUTO, card, CTLFLAG_RW, &bt848_card, -1, "");
95 SYSCTL_INT(_hw_bt848, OID_AUTO, tuner, CTLFLAG_RW, &bt848_tuner, -1, "");
96 SYSCTL_INT(_hw_bt848, OID_AUTO, reverse_mute, CTLFLAG_RW, &bt848_reverse_mute, -1, "");
97 SYSCTL_INT(_hw_bt848, OID_AUTO, format, CTLFLAG_RW, &bt848_format, -1, "");
98 SYSCTL_INT(_hw_bt848, OID_AUTO, slow_msp_audio, CTLFLAG_RW, &bt848_slow_msp_audio, -1, "");
99 #ifdef BKTR_NEW_MSP34XX_DRIVER
100 SYSCTL_INT(_hw_bt848, OID_AUTO, stereo_once, CTLFLAG_RW, &bt848_stereo_once, 0, "");
101 SYSCTL_INT(_hw_bt848, OID_AUTO, amsound, CTLFLAG_RW, &bt848_amsound, 0, "");
102 SYSCTL_INT(_hw_bt848, OID_AUTO, dolby, CTLFLAG_RW, &bt848_dolby, 0, "");
103 #endif
104
105 #include <dev/video/meteor/ioctl_meteor.h>
106 #include <dev/video/bktr/ioctl_bt848.h> /* extensions to ioctl_meteor.h */
107 #include <dev/video/bktr/bktr_reg.h>
108 #include <dev/video/bktr/bktr_tuner.h>
109 #include <dev/video/bktr/bktr_card.h>
110 #include <dev/video/bktr/bktr_audio.h>
111 #include <dev/video/bktr/bktr_core.h>
112 #include <dev/video/bktr/bktr_os.h>
113
114 #if defined(BKTR_USE_FREEBSD_SMBUS)
115 #include <dev/video/bktr/bktr_i2c.h>
116
117 #include "iicbb_if.h"
118 #include "smbus_if.h"
119 #endif
120
121 static int      bktr_probe( device_t dev );
122 static int      bktr_attach( device_t dev );
123 static int      bktr_detach( device_t dev );
124 static int      bktr_shutdown( device_t dev );
125 static void     bktr_intr(void *arg) { common_bktr_intr(arg); }
126
127 static device_method_t bktr_methods[] = {
128         /* Device interface */
129         DEVMETHOD(device_probe,         bktr_probe),
130         DEVMETHOD(device_attach,        bktr_attach),
131         DEVMETHOD(device_detach,        bktr_detach),
132         DEVMETHOD(device_shutdown,      bktr_shutdown),
133
134 #if defined(BKTR_USE_FREEBSD_SMBUS)
135         /* iicbb interface */
136         DEVMETHOD(iicbb_callback,       bti2c_iic_callback),
137         DEVMETHOD(iicbb_setsda,         bti2c_iic_setsda),
138         DEVMETHOD(iicbb_setscl,         bti2c_iic_setscl),
139         DEVMETHOD(iicbb_getsda,         bti2c_iic_getsda),
140         DEVMETHOD(iicbb_getscl,         bti2c_iic_getscl),
141         DEVMETHOD(iicbb_reset,          bti2c_iic_reset),
142         
143         /* smbus interface */
144         DEVMETHOD(smbus_callback,       bti2c_smb_callback),
145         DEVMETHOD(smbus_writeb,         bti2c_smb_writeb),
146         DEVMETHOD(smbus_writew,         bti2c_smb_writew),
147         DEVMETHOD(smbus_readb,          bti2c_smb_readb),
148 #endif
149
150         { 0, 0 }
151 };
152
153 static driver_t bktr_driver = {
154         "bktr",
155         bktr_methods,
156         sizeof(struct bktr_softc),
157 };
158
159 static devclass_t bktr_devclass;
160
161 static  d_open_t        bktr_open;
162 static  d_close_t       bktr_close;
163 static  d_read_t        bktr_read;
164 static  d_write_t       bktr_write;
165 static  d_ioctl_t       bktr_ioctl;
166 static  d_mmap_t        bktr_mmap;
167 static  d_poll_t        bktr_poll;
168 static  d_kqfilter_t    bktr_kqfilter;
169
170 static void bktr_filter_detach(struct knote *);
171 static int bktr_filter(struct knote *, long);
172
173 #define CDEV_MAJOR 92 
174 static struct dev_ops bktr_ops = {
175         { "bktr", CDEV_MAJOR, 0 },
176         .d_open =       bktr_open,
177         .d_close =      bktr_close,
178         .d_read =       bktr_read,
179         .d_write =      bktr_write,
180         .d_ioctl =      bktr_ioctl,
181         .d_poll =       bktr_poll,
182         .d_kqfilter =   bktr_kqfilter,
183         .d_mmap =       bktr_mmap,
184 };
185
186 DRIVER_MODULE(bktr, pci, bktr_driver, bktr_devclass, 0, 0);
187 MODULE_DEPEND(bktr, bktr_mem, 1,1,1);
188 MODULE_VERSION(bktr, 1);
189
190 /*
191  * the boot time probe routine.
192  */
193 static int
194 bktr_probe( device_t dev )
195 {
196         unsigned int type = pci_get_devid(dev);
197         unsigned int rev  = pci_get_revid(dev);
198
199         if (PCI_VENDOR(type) == PCI_VENDOR_BROOKTREE)
200         {
201                 switch (PCI_PRODUCT(type)) {
202                 case PCI_PRODUCT_BROOKTREE_BT848:
203                         if (rev == 0x12)
204                                 device_set_desc(dev, "BrookTree 848A");
205                         else
206                                 device_set_desc(dev, "BrookTree 848");
207                         return BUS_PROBE_DEFAULT;
208                 case PCI_PRODUCT_BROOKTREE_BT849:
209                         device_set_desc(dev, "BrookTree 849A");
210                         return BUS_PROBE_DEFAULT;
211                 case PCI_PRODUCT_BROOKTREE_BT878:
212                         device_set_desc(dev, "BrookTree 878");
213                         return BUS_PROBE_DEFAULT;
214                 case PCI_PRODUCT_BROOKTREE_BT879:
215                         device_set_desc(dev, "BrookTree 879");
216                         return BUS_PROBE_DEFAULT;
217                 }
218         };
219
220         return ENXIO;
221 }
222
223
224 /*
225  * the attach routine.
226  */
227 static int
228 bktr_attach( device_t dev )
229 {
230         u_long          latency;
231         u_long          fun;
232         u_long          val;
233         unsigned int    rev;
234         unsigned int    unit;
235         int             error = 0;
236 #ifdef BROOKTREE_IRQ
237         u_long          old_irq, new_irq;
238 #endif 
239
240         struct bktr_softc *bktr = device_get_softc(dev);
241
242         unit = device_get_unit(dev);
243
244         /* build the device name for bktr_name() */
245         ksnprintf(bktr->bktr_xname, sizeof(bktr->bktr_xname), "bktr%d",unit);
246
247         /*
248          * Enable bus mastering and Memory Mapped device
249          */
250         val = pci_read_config(dev, PCIR_COMMAND, 4);
251         val |= (PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
252         pci_write_config(dev, PCIR_COMMAND, val, 4);
253
254         /*
255          * Map control/status registers.
256          */
257         bktr->mem_rid = PCIR_BAR(0);
258         bktr->res_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 
259                                         &bktr->mem_rid, RF_ACTIVE);
260
261         if (!bktr->res_mem) {
262                 device_printf(dev, "could not map memory\n");
263                 error = ENXIO;
264                 goto fail;
265         }
266         bktr->memt = rman_get_bustag(bktr->res_mem);
267         bktr->memh = rman_get_bushandle(bktr->res_mem);
268
269
270         /*
271          * Disable the brooktree device
272          */
273         OUTL(bktr, BKTR_INT_MASK, ALL_INTS_DISABLED);
274         OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_RISC_DISABLED);
275
276
277 #ifdef BROOKTREE_IRQ            /* from the configuration file */
278         old_irq = pci_conf_read(tag, PCI_INTERRUPT_REG);
279         pci_conf_write(tag, PCI_INTERRUPT_REG, BROOKTREE_IRQ);
280         new_irq = pci_conf_read(tag, PCI_INTERRUPT_REG);
281         kprintf("bktr%d: attach: irq changed from %d to %d\n",
282                 unit, (old_irq & 0xff), (new_irq & 0xff));
283 #endif 
284
285         /*
286          * Allocate our interrupt.
287          */
288         bktr->irq_rid = 0;
289         bktr->res_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, 
290                                 &bktr->irq_rid, RF_SHAREABLE | RF_ACTIVE);
291         if (bktr->res_irq == NULL) {
292                 device_printf(dev, "could not map interrupt\n");
293                 error = ENXIO;
294                 goto fail;
295         }
296
297         error = bus_setup_intr(dev, bktr->res_irq, 0,
298                                bktr_intr, bktr, &bktr->res_ih, NULL);
299         if (error) {
300                 device_printf(dev, "could not setup irq\n");
301                 goto fail;
302
303         }
304
305
306         /* Update the Device Control Register */
307         /* on Bt878 and Bt879 cards           */
308         fun = pci_read_config( dev, 0x40, 2);
309         fun = fun | 1;  /* Enable writes to the sub-system vendor ID */
310
311 #if defined( BKTR_430_FX_MODE )
312         if (bootverbose) kprintf("Using 430 FX chipset compatibilty mode\n");
313         fun = fun | 2;  /* Enable Intel 430 FX compatibility mode */
314 #endif
315
316 #if defined( BKTR_SIS_VIA_MODE )
317         if (bootverbose) kprintf("Using SiS/VIA chipset compatibilty mode\n");
318         fun = fun | 4;  /* Enable SiS/VIA compatibility mode (useful for
319                            OPTi chipset motherboards too */
320 #endif
321         pci_write_config(dev, 0x40, fun, 2);
322
323 #if defined(BKTR_USE_FREEBSD_SMBUS)
324         if (bt848_i2c_attach(dev))
325                 kprintf("bktr%d: i2c_attach: can't attach\n", unit);
326 #endif
327
328 /*
329  * PCI latency timer.  32 is a good value for 4 bus mastering slots, if
330  * you have more than four, then 16 would probably be a better value.
331  */
332 #ifndef BROOKTREE_DEF_LATENCY_VALUE
333 #define BROOKTREE_DEF_LATENCY_VALUE     10
334 #endif
335         latency = pci_read_config(dev, PCI_LATENCY_TIMER, 4);
336         latency = (latency >> 8) & 0xff;
337         if ( bootverbose ) {
338                 if (latency)
339                         kprintf("brooktree%d: PCI bus latency is", unit);
340                 else
341                         kprintf("brooktree%d: PCI bus latency was 0 changing to",
342                                 unit);
343         }
344         if ( !latency ) {
345                 latency = BROOKTREE_DEF_LATENCY_VALUE;
346                 pci_write_config(dev, PCI_LATENCY_TIMER, latency<<8, 4);
347         }
348         if ( bootverbose ) {
349                 kprintf(" %d.\n", (int) latency);
350         }
351
352         /* read the pci device id and revision id */
353         fun = pci_get_devid(dev);
354         rev = pci_get_revid(dev);
355
356         /* call the common attach code */
357         common_bktr_attach( bktr, unit, fun, rev );
358
359         /* make the device entries */
360         make_dev(&bktr_ops, unit,    0, 0, 0444, "bktr%d",  unit);
361         make_dev(&bktr_ops, unit+16, 0, 0, 0444, "tuner%d", unit);
362         make_dev(&bktr_ops, unit+32, 0, 0, 0444, "vbi%d"  , unit);
363
364         return 0;
365
366 fail:
367         if (bktr->res_irq)
368                 bus_release_resource(dev, SYS_RES_IRQ, bktr->irq_rid, bktr->res_irq);
369         if (bktr->res_mem)
370                 bus_release_resource(dev, SYS_RES_IRQ, bktr->mem_rid, bktr->res_mem);
371         return error;
372
373 }
374
375 /*
376  * the detach routine.
377  */
378 static int
379 bktr_detach( device_t dev )
380 {
381         struct bktr_softc *bktr = device_get_softc(dev);
382
383 #ifdef BKTR_NEW_MSP34XX_DRIVER
384         /* Disable the soundchip and kernel thread */
385         if (bktr->msp3400c_info != NULL)
386                 msp_detach(bktr);
387 #endif
388
389         /* Disable the brooktree device */
390         OUTL(bktr, BKTR_INT_MASK, ALL_INTS_DISABLED);
391         OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_RISC_DISABLED);
392
393 #if defined(BKTR_USE_FREEBSD_SMBUS)
394         if (bt848_i2c_detach(dev))
395                 kprintf("bktr%d: i2c_attach: can't attach\n",
396                      device_get_unit(dev));
397 #endif
398 #ifdef USE_VBIMUTEX
399         mtx_destroy(&bktr->vbimutex);
400 #endif
401
402         /* Note: We do not free memory for RISC programs, grab buffer, vbi buffers */
403         /* The memory is retained by the bktr_mem module so we can unload and */
404         /* then reload the main bktr driver module */
405
406         /* removing the ops automatically destroys all related devices */
407         dev_ops_remove_minor(&bktr_ops, /*0x0f, */device_get_unit(dev));
408
409         /*
410          * Deallocate resources.
411          */
412         bus_teardown_intr(dev, bktr->res_irq, bktr->res_ih);
413         bus_release_resource(dev, SYS_RES_IRQ, bktr->irq_rid, bktr->res_irq);
414         bus_release_resource(dev, SYS_RES_MEMORY, bktr->mem_rid, bktr->res_mem);
415          
416         return 0;
417 }
418
419 /*
420  * the shutdown routine.
421  */
422 static int
423 bktr_shutdown( device_t dev )
424 {
425         struct bktr_softc *bktr = device_get_softc(dev);
426
427         /* Disable the brooktree device */
428         OUTL(bktr, BKTR_INT_MASK, ALL_INTS_DISABLED);
429         OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_RISC_DISABLED);
430
431         return 0;
432 }
433
434
435 /*
436  * Special Memory Allocation
437  */
438 vm_offset_t
439 get_bktr_mem( int unit, unsigned size )
440 {
441         vm_offset_t     addr = 0;
442
443         addr = (vm_offset_t)contigmalloc(size, M_DEVBUF, M_NOWAIT, 0,
444             0xffffffff, 1<<24, 0);
445         if (addr == 0)
446                 addr = (vm_offset_t)contigmalloc(size, M_DEVBUF, M_NOWAIT, 0,
447                     0xffffffff, PAGE_SIZE, 0);
448         if (addr == 0) {
449                 kprintf("bktr%d: Unable to allocate %d bytes of memory.\n",
450                         unit, size);
451         }
452
453         return( addr );
454 }
455
456
457 /*---------------------------------------------------------
458 **
459 **      BrookTree 848 character device driver routines
460 **
461 **---------------------------------------------------------
462 */
463
464 #define VIDEO_DEV       0x00
465 #define TUNER_DEV       0x01
466 #define VBI_DEV         0x02
467
468 #define UNIT(x)         ((x) & 0x0f)
469 #define FUNCTION(x)     (x >> 4)
470
471 /*
472  * 
473  */
474 static int
475 bktr_open(struct dev_open_args *ap)
476 {
477         cdev_t dev = ap->a_head.a_dev;
478         bktr_ptr_t      bktr;
479         int             unit;
480         int             result;
481
482         unit = UNIT( minor(dev) );
483
484         /* Get the device data */
485         bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
486         if (bktr == NULL) {
487                 /* the device is no longer valid/functioning */
488                 return (ENXIO);
489         }
490
491         if (!(bktr->flags & METEOR_INITALIZED)) /* device not found */
492                 return( ENXIO );        
493
494         /* Record that the device is now busy */
495         device_busy(devclass_get_device(bktr_devclass, unit)); 
496
497
498         if (bt848_card != -1) {
499           if ((bt848_card >> 8   == unit ) &&
500              ( (bt848_card & 0xff) < Bt848_MAX_CARD )) {
501             if ( bktr->bt848_card != (bt848_card & 0xff) ) {
502               bktr->bt848_card = (bt848_card & 0xff);
503               probeCard(bktr, FALSE, unit);
504             }
505           }
506         }
507
508         if (bt848_tuner != -1) {
509           if ((bt848_tuner >> 8   == unit ) &&
510              ( (bt848_tuner & 0xff) < Bt848_MAX_TUNER )) {
511             if ( bktr->bt848_tuner != (bt848_tuner & 0xff) ) {
512               bktr->bt848_tuner = (bt848_tuner & 0xff);
513               probeCard(bktr, FALSE, unit);
514             }
515           }
516         }
517
518         if (bt848_reverse_mute != -1) {
519           if ((bt848_reverse_mute >> 8)   == unit ) {
520             bktr->reverse_mute = bt848_reverse_mute & 0xff;
521           }
522         }
523
524         if (bt848_slow_msp_audio != -1) {
525           if ((bt848_slow_msp_audio >> 8) == unit ) {
526               bktr->slow_msp_audio = (bt848_slow_msp_audio & 0xff);
527           }
528         }
529
530 #ifdef BKTR_NEW_MSP34XX_DRIVER
531         if (bt848_stereo_once != 0) {
532           if ((bt848_stereo_once >> 8) == unit ) {
533               bktr->stereo_once = (bt848_stereo_once & 0xff);
534           }
535         }
536
537         if (bt848_amsound != -1) {
538           if ((bt848_amsound >> 8) == unit ) {
539               bktr->amsound = (bt848_amsound & 0xff);
540           }
541         }
542
543         if (bt848_dolby != -1) {
544           if ((bt848_dolby >> 8) == unit ) {
545               bktr->dolby = (bt848_dolby & 0xff);
546           }
547         }
548 #endif
549
550         switch ( FUNCTION( minor(dev) ) ) {
551         case VIDEO_DEV:
552                 result = video_open( bktr );
553                 break;
554         case TUNER_DEV:
555                 result = tuner_open( bktr );
556                 break;
557         case VBI_DEV:
558                 result = vbi_open( bktr );
559                 break;
560         default:
561                 result = ENXIO;
562                 break;
563         }
564
565         /* If there was an error opening the device, undo the busy status */
566         if (result != 0)
567                 device_unbusy(devclass_get_device(bktr_devclass, unit)); 
568         return( result );
569 }
570
571
572 /*
573  * 
574  */
575 static int
576 bktr_close(struct dev_close_args *ap)
577 {
578         cdev_t dev = ap->a_head.a_dev;
579         bktr_ptr_t      bktr;
580         int             unit;
581         int             result;
582
583         unit = UNIT( minor(dev) );
584
585         /* Get the device data */
586         bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
587         if (bktr == NULL) {
588                 /* the device is no longer valid/functioning */
589                 return (ENXIO);
590         }
591
592         switch ( FUNCTION( minor(dev) ) ) {
593         case VIDEO_DEV:
594                 result = video_close( bktr );
595                 break;
596         case TUNER_DEV:
597                 result = tuner_close( bktr );
598                 break;
599         case VBI_DEV:
600                 result = vbi_close( bktr );
601                 break;
602         default:
603                 return (ENXIO);
604                 break;
605         }
606
607         device_unbusy(devclass_get_device(bktr_devclass, unit)); 
608         return( result );
609 }
610
611
612 /*
613  * 
614  */
615 static int
616 bktr_read(struct dev_read_args *ap)
617 {
618         cdev_t dev = ap->a_head.a_dev;
619         bktr_ptr_t      bktr;
620         int             unit;
621         
622         unit = UNIT(minor(dev));
623
624         /* Get the device data */
625         bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
626         if (bktr == NULL) {
627                 /* the device is no longer valid/functioning */
628                 return (ENXIO);
629         }
630
631         switch ( FUNCTION( minor(dev) ) ) {
632         case VIDEO_DEV:
633                 return( video_read( bktr, unit, dev, ap->a_uio ) );
634         case VBI_DEV:
635                 return( vbi_read( bktr, ap->a_uio, ap->a_ioflag ) );
636         }
637         return( ENXIO );
638 }
639
640
641 /*
642  * 
643  */
644 static int
645 bktr_write(struct dev_write_args *ap)
646 {
647         return( EINVAL ); /* XXX or ENXIO ? */
648 }
649
650
651 /*
652  * 
653  */
654 static int
655 bktr_ioctl(struct dev_ioctl_args *ap)
656 {
657         cdev_t dev = ap->a_head.a_dev;
658         u_long cmd = ap->a_cmd;
659         bktr_ptr_t      bktr;
660         int             unit;
661
662         unit = UNIT(minor(dev));
663
664         /* Get the device data */
665         bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
666         if (bktr == NULL) {
667                 /* the device is no longer valid/functioning */
668                 return (ENXIO);
669         }
670
671 #ifdef BKTR_GPIO_ACCESS
672         if (bktr->bigbuf == 0 && cmd != BT848_GPIO_GET_EN &&
673             cmd != BT848_GPIO_SET_EN && cmd != BT848_GPIO_GET_DATA &&
674             cmd != BT848_GPIO_SET_DATA) /* no frame buffer allocated (ioctl failed) */
675                 return( ENOMEM );
676 #else
677         if (bktr->bigbuf == 0)  /* no frame buffer allocated (ioctl failed) */
678                 return( ENOMEM );
679 #endif
680
681         switch ( FUNCTION( minor(dev) ) ) {
682         case VIDEO_DEV:
683                 return( video_ioctl( bktr, unit, cmd, ap->a_data, curthread ) );
684         case TUNER_DEV:
685                 return( tuner_ioctl( bktr, unit, cmd, ap->a_data, curthread ) );
686         }
687
688         return( ENXIO );
689 }
690
691
692 /*
693  * 
694  */
695 static int
696 bktr_mmap(struct dev_mmap_args *ap)
697 {
698         cdev_t dev = ap->a_head.a_dev;
699         int             unit;
700         bktr_ptr_t      bktr;
701
702         unit = UNIT(minor(dev));
703
704         if (FUNCTION(minor(dev)) > 0)   /* only allow mmap on /dev/bktr[n] */
705                 return(EINVAL);
706
707         /* Get the device data */
708         bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
709         if (bktr == NULL) {
710                 /* the device is no longer valid/functioning */
711                 return (ENXIO);
712         }
713
714         if (ap->a_nprot & PROT_EXEC)
715                 return(EINVAL);
716
717         if (ap->a_offset < 0)
718                 return(EINVAL);
719
720         if (ap->a_offset >= bktr->alloc_pages * PAGE_SIZE)
721                 return(EINVAL);
722
723         ap->a_result = atop(vtophys(bktr->bigbuf) + ap->a_offset);
724         return(0);
725 }
726
727 static int
728 bktr_poll(struct dev_poll_args *ap)
729 {
730         cdev_t dev = ap->a_head.a_dev;
731         int             unit;
732         bktr_ptr_t      bktr;
733         int revents = 0; 
734
735         unit = UNIT(minor(dev));
736
737         /* Get the device data */
738         bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
739         if (bktr == NULL) {
740                 /* the device is no longer valid/functioning */
741                 return (ENXIO);
742         }
743
744         LOCK_VBI(bktr);
745         crit_enter();
746
747         if (ap->a_events & (POLLIN | POLLRDNORM)) {
748
749                 switch ( FUNCTION( minor(dev) ) ) {
750                 case VBI_DEV:
751                         if(bktr->vbisize == 0)
752                                 selrecord(curthread, &bktr->vbi_select);
753                         else
754                                 revents |= ap->a_events & (POLLIN | POLLRDNORM);
755                         break;
756                 }
757         }
758
759         crit_exit();
760         UNLOCK_VBI(bktr);
761
762         ap->a_events = revents;
763         return (0);
764 }
765
766 static struct filterops bktr_filterops =
767         { 1, NULL, bktr_filter_detach, bktr_filter };
768
769 static int
770 bktr_kqfilter(struct dev_kqfilter_args *ap)
771 {
772         cdev_t dev = ap->a_head.a_dev;
773         struct knote *kn = ap->a_kn;
774         struct klist *klist;
775         bktr_ptr_t bktr;
776         int unit;
777
778         ap->a_result = 0;
779
780         switch (kn->kn_filter) {
781         case EVFILT_READ:
782                 if (FUNCTION(minor(dev)) == VBI_DEV) {
783                         unit = UNIT(minor(dev));
784                         /* Get the device data */
785                         bktr = (struct bktr_softc *)
786                             devclass_get_softc(bktr_devclass, unit);
787                         kn->kn_fop = &bktr_filterops;
788                         kn->kn_hook = (caddr_t)bktr;
789                         break;
790                 }
791                 /* fall through */
792         default:
793                 ap->a_result = EOPNOTSUPP;
794                 return (0);
795         }
796
797         crit_enter();
798         klist = &bktr->vbi_select.si_note;
799         SLIST_INSERT_HEAD(klist, kn, kn_selnext);
800         crit_exit();
801
802         return (0);
803 }
804
805 static void
806 bktr_filter_detach(struct knote *kn)
807 {
808         bktr_ptr_t bktr = (bktr_ptr_t)kn->kn_hook;
809         struct klist *klist;
810
811         crit_enter();
812         klist = &bktr->vbi_select.si_note;
813         SLIST_REMOVE(klist, kn, knote, kn_selnext);
814         crit_exit();
815 }
816
817 static int
818 bktr_filter(struct knote *kn, long hint)
819 {
820         bktr_ptr_t bktr = (bktr_ptr_t)kn->kn_hook;
821         int ready = 0;
822
823         if (bktr == NULL) {
824                 /* the device is no longer valid/functioning */
825                 kn->kn_flags |= EV_EOF;
826                 return (1);
827         }
828
829         LOCK_VBI(bktr);
830         crit_enter();
831         if (bktr->vbisize != 0)
832                 ready = 1;
833         crit_exit();
834         UNLOCK_VBI(bktr);
835
836         return (ready);
837 }