DEVFS - remove dev_ops_add(), dev_ops_get(), and get_dev()
[dragonfly.git] / sys / dev / video / bktr / bktr_os.c
CommitLineData
d3f3bfa9 1/*-
984263bc
MD
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.
a35cc233 32 *
d3f3bfa9 33 * $FreeBSD: src/sys/dev/bktr/bktr_os.c,v 1.54 2007/02/23 12:18:34 piso Exp $
0d6742a3 34 * $DragonFly: src/sys/dev/video/bktr/bktr_os.c,v 1.21 2008/05/18 03:02:53 pavalos Exp $
984263bc
MD
35 */
36
a35cc233
JS
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 */
984263bc
MD
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
984263bc
MD
54#include <sys/param.h>
55#include <sys/systm.h>
56#include <sys/conf.h>
fef8985e 57#include <sys/device.h>
984263bc
MD
58#include <sys/uio.h>
59#include <sys/kernel.h>
60#include <sys/signalvar.h>
a35cc233 61#include <sys/malloc.h>
984263bc
MD
62#include <sys/mman.h>
63#include <sys/poll.h>
64#include <sys/select.h>
1f7ab7c9
MD
65#include <sys/bus.h>
66#include <sys/rman.h>
052b6ffa 67#include <sys/thread2.h>
d3f3bfa9 68#include <sys/selinfo.h>
984263bc
MD
69
70#include <vm/vm.h>
71#include <vm/vm_kern.h>
72#include <vm/pmap.h>
73#include <vm/vm_extern.h>
74
1f2de5d4
MD
75#include <bus/pci/pcivar.h>
76#include <bus/pci/pcireg.h>
a35cc233 77#include <bus/pci/pcidevs.h>
984263bc
MD
78
79#include <sys/sysctl.h>
80int bt848_card = -1;
81int bt848_tuner = -1;
82int bt848_reverse_mute = -1;
83int bt848_format = -1;
84int bt848_slow_msp_audio = -1;
a35cc233
JS
85#ifdef BKTR_NEW_MSP34XX_DRIVER
86int bt848_stereo_once = 0; /* no continuous stereo monitoring */
87int bt848_amsound = 0; /* hard-wire AM sound at 6.5 Hz (france),
88 the autoscan seems work well only with FM... */
89int bt848_dolby = 0;
90#endif
984263bc
MD
91
92SYSCTL_NODE(_hw, OID_AUTO, bt848, CTLFLAG_RW, 0, "Bt848 Driver mgmt");
93SYSCTL_INT(_hw_bt848, OID_AUTO, card, CTLFLAG_RW, &bt848_card, -1, "");
94SYSCTL_INT(_hw_bt848, OID_AUTO, tuner, CTLFLAG_RW, &bt848_tuner, -1, "");
95SYSCTL_INT(_hw_bt848, OID_AUTO, reverse_mute, CTLFLAG_RW, &bt848_reverse_mute, -1, "");
96SYSCTL_INT(_hw_bt848, OID_AUTO, format, CTLFLAG_RW, &bt848_format, -1, "");
97SYSCTL_INT(_hw_bt848, OID_AUTO, slow_msp_audio, CTLFLAG_RW, &bt848_slow_msp_audio, -1, "");
a35cc233
JS
98#ifdef BKTR_NEW_MSP34XX_DRIVER
99SYSCTL_INT(_hw_bt848, OID_AUTO, stereo_once, CTLFLAG_RW, &bt848_stereo_once, 0, "");
100SYSCTL_INT(_hw_bt848, OID_AUTO, amsound, CTLFLAG_RW, &bt848_amsound, 0, "");
101SYSCTL_INT(_hw_bt848, OID_AUTO, dolby, CTLFLAG_RW, &bt848_dolby, 0, "");
984263bc
MD
102#endif
103
a35cc233
JS
104#include <dev/video/meteor/ioctl_meteor.h>
105#include <dev/video/bktr/ioctl_bt848.h> /* extensions to ioctl_meteor.h */
106#include <dev/video/bktr/bktr_reg.h>
107#include <dev/video/bktr/bktr_tuner.h>
108#include <dev/video/bktr/bktr_card.h>
109#include <dev/video/bktr/bktr_audio.h>
110#include <dev/video/bktr/bktr_core.h>
111#include <dev/video/bktr/bktr_os.h>
984263bc 112
984263bc 113#if defined(BKTR_USE_FREEBSD_SMBUS)
a35cc233 114#include <dev/video/bktr/bktr_i2c.h>
984263bc 115
a35cc233
JS
116#include "iicbb_if.h"
117#include "smbus_if.h"
118#endif
984263bc
MD
119
120static int bktr_probe( device_t dev );
121static int bktr_attach( device_t dev );
122static int bktr_detach( device_t dev );
123static int bktr_shutdown( device_t dev );
124static void bktr_intr(void *arg) { common_bktr_intr(arg); }
125
126static device_method_t bktr_methods[] = {
127 /* Device interface */
128 DEVMETHOD(device_probe, bktr_probe),
129 DEVMETHOD(device_attach, bktr_attach),
130 DEVMETHOD(device_detach, bktr_detach),
131 DEVMETHOD(device_shutdown, bktr_shutdown),
132
a35cc233
JS
133#if defined(BKTR_USE_FREEBSD_SMBUS)
134 /* iicbb interface */
135 DEVMETHOD(iicbb_callback, bti2c_iic_callback),
136 DEVMETHOD(iicbb_setsda, bti2c_iic_setsda),
137 DEVMETHOD(iicbb_setscl, bti2c_iic_setscl),
138 DEVMETHOD(iicbb_getsda, bti2c_iic_getsda),
139 DEVMETHOD(iicbb_getscl, bti2c_iic_getscl),
140 DEVMETHOD(iicbb_reset, bti2c_iic_reset),
141
142 /* smbus interface */
143 DEVMETHOD(smbus_callback, bti2c_smb_callback),
144 DEVMETHOD(smbus_writeb, bti2c_smb_writeb),
145 DEVMETHOD(smbus_writew, bti2c_smb_writew),
146 DEVMETHOD(smbus_readb, bti2c_smb_readb),
147#endif
148
984263bc
MD
149 { 0, 0 }
150};
151
152static driver_t bktr_driver = {
153 "bktr",
154 bktr_methods,
155 sizeof(struct bktr_softc),
156};
157
158static devclass_t bktr_devclass;
159
160static d_open_t bktr_open;
161static d_close_t bktr_close;
162static d_read_t bktr_read;
163static d_write_t bktr_write;
164static d_ioctl_t bktr_ioctl;
165static d_mmap_t bktr_mmap;
166static d_poll_t bktr_poll;
167
168#define CDEV_MAJOR 92
fef8985e
MD
169static struct dev_ops bktr_ops = {
170 { "bktr", CDEV_MAJOR, 0 },
171 .d_open = bktr_open,
172 .d_close = bktr_close,
173 .d_read = bktr_read,
174 .d_write = bktr_write,
175 .d_ioctl = bktr_ioctl,
176 .d_poll = bktr_poll,
177 .d_mmap = bktr_mmap,
984263bc
MD
178};
179
180DRIVER_MODULE(bktr, pci, bktr_driver, bktr_devclass, 0, 0);
984263bc
MD
181MODULE_DEPEND(bktr, bktr_mem, 1,1,1);
182MODULE_VERSION(bktr, 1);
984263bc
MD
183
184/*
185 * the boot time probe routine.
186 */
187static int
188bktr_probe( device_t dev )
189{
190 unsigned int type = pci_get_devid(dev);
191 unsigned int rev = pci_get_revid(dev);
192
193 if (PCI_VENDOR(type) == PCI_VENDOR_BROOKTREE)
194 {
195 switch (PCI_PRODUCT(type)) {
196 case PCI_PRODUCT_BROOKTREE_BT848:
197 if (rev == 0x12)
198 device_set_desc(dev, "BrookTree 848A");
199 else
200 device_set_desc(dev, "BrookTree 848");
d3f3bfa9 201 return BUS_PROBE_DEFAULT;
984263bc
MD
202 case PCI_PRODUCT_BROOKTREE_BT849:
203 device_set_desc(dev, "BrookTree 849A");
d3f3bfa9 204 return BUS_PROBE_DEFAULT;
984263bc
MD
205 case PCI_PRODUCT_BROOKTREE_BT878:
206 device_set_desc(dev, "BrookTree 878");
d3f3bfa9 207 return BUS_PROBE_DEFAULT;
984263bc
MD
208 case PCI_PRODUCT_BROOKTREE_BT879:
209 device_set_desc(dev, "BrookTree 879");
d3f3bfa9 210 return BUS_PROBE_DEFAULT;
984263bc
MD
211 }
212 };
213
214 return ENXIO;
215}
216
217
218/*
219 * the attach routine.
220 */
221static int
222bktr_attach( device_t dev )
223{
224 u_long latency;
225 u_long fun;
226 u_long val;
227 unsigned int rev;
228 unsigned int unit;
229 int error = 0;
230#ifdef BROOKTREE_IRQ
231 u_long old_irq, new_irq;
232#endif
233
234 struct bktr_softc *bktr = device_get_softc(dev);
235
236 unit = device_get_unit(dev);
237
238 /* build the device name for bktr_name() */
f8c7a42d 239 ksnprintf(bktr->bktr_xname, sizeof(bktr->bktr_xname), "bktr%d",unit);
984263bc
MD
240
241 /*
242 * Enable bus mastering and Memory Mapped device
243 */
244 val = pci_read_config(dev, PCIR_COMMAND, 4);
245 val |= (PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
246 pci_write_config(dev, PCIR_COMMAND, val, 4);
247
248 /*
249 * Map control/status registers.
250 */
a35cc233
JS
251 bktr->mem_rid = PCIR_BAR(0);
252 bktr->res_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
253 &bktr->mem_rid, RF_ACTIVE);
984263bc
MD
254
255 if (!bktr->res_mem) {
256 device_printf(dev, "could not map memory\n");
257 error = ENXIO;
258 goto fail;
259 }
260 bktr->memt = rman_get_bustag(bktr->res_mem);
261 bktr->memh = rman_get_bushandle(bktr->res_mem);
262
263
264 /*
265 * Disable the brooktree device
266 */
267 OUTL(bktr, BKTR_INT_MASK, ALL_INTS_DISABLED);
268 OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_RISC_DISABLED);
269
270
271#ifdef BROOKTREE_IRQ /* from the configuration file */
272 old_irq = pci_conf_read(tag, PCI_INTERRUPT_REG);
273 pci_conf_write(tag, PCI_INTERRUPT_REG, BROOKTREE_IRQ);
274 new_irq = pci_conf_read(tag, PCI_INTERRUPT_REG);
e3869ec7 275 kprintf("bktr%d: attach: irq changed from %d to %d\n",
984263bc
MD
276 unit, (old_irq & 0xff), (new_irq & 0xff));
277#endif
278
279 /*
280 * Allocate our interrupt.
281 */
282 bktr->irq_rid = 0;
a35cc233
JS
283 bktr->res_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
284 &bktr->irq_rid, RF_SHAREABLE | RF_ACTIVE);
984263bc
MD
285 if (bktr->res_irq == NULL) {
286 device_printf(dev, "could not map interrupt\n");
287 error = ENXIO;
288 goto fail;
289 }
290
ee61f228 291 error = bus_setup_intr(dev, bktr->res_irq, 0,
e9cb6d99 292 bktr_intr, bktr, &bktr->res_ih, NULL);
984263bc
MD
293 if (error) {
294 device_printf(dev, "could not setup irq\n");
295 goto fail;
296
297 }
298
299
300 /* Update the Device Control Register */
301 /* on Bt878 and Bt879 cards */
302 fun = pci_read_config( dev, 0x40, 2);
303 fun = fun | 1; /* Enable writes to the sub-system vendor ID */
304
305#if defined( BKTR_430_FX_MODE )
e3869ec7 306 if (bootverbose) kprintf("Using 430 FX chipset compatibilty mode\n");
984263bc
MD
307 fun = fun | 2; /* Enable Intel 430 FX compatibility mode */
308#endif
309
310#if defined( BKTR_SIS_VIA_MODE )
e3869ec7 311 if (bootverbose) kprintf("Using SiS/VIA chipset compatibilty mode\n");
0d6742a3 312 fun = fun | 4; /* Enable SiS/VIA compatibility mode (useful for
984263bc
MD
313 OPTi chipset motherboards too */
314#endif
315 pci_write_config(dev, 0x40, fun, 2);
316
984263bc 317#if defined(BKTR_USE_FREEBSD_SMBUS)
a35cc233 318 if (bt848_i2c_attach(dev))
e3869ec7 319 kprintf("bktr%d: i2c_attach: can't attach\n", unit);
984263bc
MD
320#endif
321
984263bc
MD
322/*
323 * PCI latency timer. 32 is a good value for 4 bus mastering slots, if
324 * you have more than four, then 16 would probably be a better value.
325 */
326#ifndef BROOKTREE_DEF_LATENCY_VALUE
327#define BROOKTREE_DEF_LATENCY_VALUE 10
328#endif
329 latency = pci_read_config(dev, PCI_LATENCY_TIMER, 4);
330 latency = (latency >> 8) & 0xff;
331 if ( bootverbose ) {
332 if (latency)
e3869ec7 333 kprintf("brooktree%d: PCI bus latency is", unit);
984263bc 334 else
e3869ec7 335 kprintf("brooktree%d: PCI bus latency was 0 changing to",
984263bc
MD
336 unit);
337 }
338 if ( !latency ) {
339 latency = BROOKTREE_DEF_LATENCY_VALUE;
340 pci_write_config(dev, PCI_LATENCY_TIMER, latency<<8, 4);
341 }
342 if ( bootverbose ) {
e3869ec7 343 kprintf(" %d.\n", (int) latency);
984263bc
MD
344 }
345
346 /* read the pci device id and revision id */
347 fun = pci_get_devid(dev);
348 rev = pci_get_revid(dev);
349
350 /* call the common attach code */
351 common_bktr_attach( bktr, unit, fun, rev );
352
353 /* make the device entries */
fef8985e
MD
354 make_dev(&bktr_ops, unit, 0, 0, 0444, "bktr%d", unit);
355 make_dev(&bktr_ops, unit+16, 0, 0, 0444, "tuner%d", unit);
356 make_dev(&bktr_ops, unit+32, 0, 0, 0444, "vbi%d" , unit);
984263bc 357
984263bc
MD
358 return 0;
359
360fail:
361 if (bktr->res_irq)
362 bus_release_resource(dev, SYS_RES_IRQ, bktr->irq_rid, bktr->res_irq);
363 if (bktr->res_mem)
364 bus_release_resource(dev, SYS_RES_IRQ, bktr->mem_rid, bktr->res_mem);
365 return error;
366
367}
368
369/*
370 * the detach routine.
371 */
372static int
373bktr_detach( device_t dev )
374{
984263bc
MD
375 struct bktr_softc *bktr = device_get_softc(dev);
376
a35cc233
JS
377#ifdef BKTR_NEW_MSP34XX_DRIVER
378 /* Disable the soundchip and kernel thread */
379 if (bktr->msp3400c_info != NULL)
380 msp_detach(bktr);
381#endif
984263bc
MD
382
383 /* Disable the brooktree device */
384 OUTL(bktr, BKTR_INT_MASK, ALL_INTS_DISABLED);
385 OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_RISC_DISABLED);
386
a35cc233
JS
387#if defined(BKTR_USE_FREEBSD_SMBUS)
388 if (bt848_i2c_detach(dev))
e3869ec7 389 kprintf("bktr%d: i2c_attach: can't attach\n",
a35cc233
JS
390 device_get_unit(dev));
391#endif
392#ifdef USE_VBIMUTEX
393 mtx_destroy(&bktr->vbimutex);
394#endif
395
984263bc
MD
396 /* Note: We do not free memory for RISC programs, grab buffer, vbi buffers */
397 /* The memory is retained by the bktr_mem module so we can unload and */
398 /* then reload the main bktr driver module */
399
fef8985e 400 /* removing the ops automatically destroys all related devices */
cd29885a 401 dev_ops_remove_minor(&bktr_ops, /*0x0f, */device_get_unit(dev));
984263bc 402
984263bc
MD
403 /*
404 * Deallocate resources.
405 */
406 bus_teardown_intr(dev, bktr->res_irq, bktr->res_ih);
407 bus_release_resource(dev, SYS_RES_IRQ, bktr->irq_rid, bktr->res_irq);
408 bus_release_resource(dev, SYS_RES_MEMORY, bktr->mem_rid, bktr->res_mem);
409
410 return 0;
411}
412
413/*
414 * the shutdown routine.
415 */
416static int
417bktr_shutdown( device_t dev )
418{
419 struct bktr_softc *bktr = device_get_softc(dev);
420
421 /* Disable the brooktree device */
422 OUTL(bktr, BKTR_INT_MASK, ALL_INTS_DISABLED);
423 OUTW(bktr, BKTR_GPIO_DMA_CTL, FIFO_RISC_DISABLED);
424
425 return 0;
426}
427
428
429/*
430 * Special Memory Allocation
431 */
432vm_offset_t
433get_bktr_mem( int unit, unsigned size )
434{
435 vm_offset_t addr = 0;
436
a35cc233
JS
437 addr = (vm_offset_t)contigmalloc(size, M_DEVBUF, M_NOWAIT, 0,
438 0xffffffff, 1<<24, 0);
984263bc 439 if (addr == 0)
a35cc233
JS
440 addr = (vm_offset_t)contigmalloc(size, M_DEVBUF, M_NOWAIT, 0,
441 0xffffffff, PAGE_SIZE, 0);
984263bc 442 if (addr == 0) {
e3869ec7 443 kprintf("bktr%d: Unable to allocate %d bytes of memory.\n",
984263bc
MD
444 unit, size);
445 }
446
447 return( addr );
448}
449
450
451/*---------------------------------------------------------
452**
453** BrookTree 848 character device driver routines
454**
455**---------------------------------------------------------
456*/
457
458#define VIDEO_DEV 0x00
459#define TUNER_DEV 0x01
460#define VBI_DEV 0x02
461
462#define UNIT(x) ((x) & 0x0f)
463#define FUNCTION(x) (x >> 4)
464
465/*
466 *
467 */
a35cc233 468static int
fef8985e 469bktr_open(struct dev_open_args *ap)
984263bc 470{
b13267a5 471 cdev_t dev = ap->a_head.a_dev;
984263bc
MD
472 bktr_ptr_t bktr;
473 int unit;
474 int result;
475
476 unit = UNIT( minor(dev) );
477
478 /* Get the device data */
479 bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
480 if (bktr == NULL) {
481 /* the device is no longer valid/functioning */
482 return (ENXIO);
483 }
484
485 if (!(bktr->flags & METEOR_INITALIZED)) /* device not found */
486 return( ENXIO );
487
488 /* Record that the device is now busy */
489 device_busy(devclass_get_device(bktr_devclass, unit));
490
491
492 if (bt848_card != -1) {
493 if ((bt848_card >> 8 == unit ) &&
494 ( (bt848_card & 0xff) < Bt848_MAX_CARD )) {
495 if ( bktr->bt848_card != (bt848_card & 0xff) ) {
496 bktr->bt848_card = (bt848_card & 0xff);
497 probeCard(bktr, FALSE, unit);
498 }
499 }
500 }
501
502 if (bt848_tuner != -1) {
503 if ((bt848_tuner >> 8 == unit ) &&
504 ( (bt848_tuner & 0xff) < Bt848_MAX_TUNER )) {
505 if ( bktr->bt848_tuner != (bt848_tuner & 0xff) ) {
506 bktr->bt848_tuner = (bt848_tuner & 0xff);
507 probeCard(bktr, FALSE, unit);
508 }
509 }
510 }
511
512 if (bt848_reverse_mute != -1) {
513 if ((bt848_reverse_mute >> 8) == unit ) {
514 bktr->reverse_mute = bt848_reverse_mute & 0xff;
515 }
516 }
517
518 if (bt848_slow_msp_audio != -1) {
519 if ((bt848_slow_msp_audio >> 8) == unit ) {
520 bktr->slow_msp_audio = (bt848_slow_msp_audio & 0xff);
521 }
522 }
523
a35cc233
JS
524#ifdef BKTR_NEW_MSP34XX_DRIVER
525 if (bt848_stereo_once != 0) {
526 if ((bt848_stereo_once >> 8) == unit ) {
527 bktr->stereo_once = (bt848_stereo_once & 0xff);
528 }
529 }
530
531 if (bt848_amsound != -1) {
532 if ((bt848_amsound >> 8) == unit ) {
533 bktr->amsound = (bt848_amsound & 0xff);
534 }
535 }
536
537 if (bt848_dolby != -1) {
538 if ((bt848_dolby >> 8) == unit ) {
539 bktr->dolby = (bt848_dolby & 0xff);
540 }
541 }
542#endif
543
984263bc
MD
544 switch ( FUNCTION( minor(dev) ) ) {
545 case VIDEO_DEV:
546 result = video_open( bktr );
547 break;
548 case TUNER_DEV:
549 result = tuner_open( bktr );
550 break;
551 case VBI_DEV:
552 result = vbi_open( bktr );
553 break;
554 default:
555 result = ENXIO;
556 break;
557 }
558
559 /* If there was an error opening the device, undo the busy status */
560 if (result != 0)
561 device_unbusy(devclass_get_device(bktr_devclass, unit));
562 return( result );
563}
564
565
566/*
567 *
568 */
a35cc233 569static int
fef8985e 570bktr_close(struct dev_close_args *ap)
984263bc 571{
b13267a5 572 cdev_t dev = ap->a_head.a_dev;
984263bc
MD
573 bktr_ptr_t bktr;
574 int unit;
575 int result;
576
577 unit = UNIT( minor(dev) );
578
579 /* Get the device data */
580 bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
581 if (bktr == NULL) {
582 /* the device is no longer valid/functioning */
583 return (ENXIO);
584 }
585
586 switch ( FUNCTION( minor(dev) ) ) {
587 case VIDEO_DEV:
588 result = video_close( bktr );
589 break;
590 case TUNER_DEV:
591 result = tuner_close( bktr );
592 break;
593 case VBI_DEV:
594 result = vbi_close( bktr );
595 break;
596 default:
597 return (ENXIO);
598 break;
599 }
600
601 device_unbusy(devclass_get_device(bktr_devclass, unit));
602 return( result );
603}
604
605
606/*
607 *
608 */
a35cc233 609static int
fef8985e 610bktr_read(struct dev_read_args *ap)
984263bc 611{
b13267a5 612 cdev_t dev = ap->a_head.a_dev;
984263bc
MD
613 bktr_ptr_t bktr;
614 int unit;
615
616 unit = UNIT(minor(dev));
617
618 /* Get the device data */
619 bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
620 if (bktr == NULL) {
621 /* the device is no longer valid/functioning */
622 return (ENXIO);
623 }
624
625 switch ( FUNCTION( minor(dev) ) ) {
626 case VIDEO_DEV:
fef8985e 627 return( video_read( bktr, unit, dev, ap->a_uio ) );
984263bc 628 case VBI_DEV:
fef8985e 629 return( vbi_read( bktr, ap->a_uio, ap->a_ioflag ) );
984263bc
MD
630 }
631 return( ENXIO );
632}
633
634
635/*
636 *
637 */
a35cc233 638static int
fef8985e 639bktr_write(struct dev_write_args *ap)
984263bc
MD
640{
641 return( EINVAL ); /* XXX or ENXIO ? */
642}
643
644
645/*
646 *
647 */
a35cc233 648static int
fef8985e 649bktr_ioctl(struct dev_ioctl_args *ap)
984263bc 650{
b13267a5 651 cdev_t dev = ap->a_head.a_dev;
7fccda02 652 u_long cmd = ap->a_cmd;
984263bc
MD
653 bktr_ptr_t bktr;
654 int unit;
655
656 unit = UNIT(minor(dev));
657
658 /* Get the device data */
659 bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
660 if (bktr == NULL) {
661 /* the device is no longer valid/functioning */
662 return (ENXIO);
663 }
664
d3f3bfa9
SW
665#ifdef BKTR_GPIO_ACCESS
666 if (bktr->bigbuf == 0 && cmd != BT848_GPIO_GET_EN &&
667 cmd != BT848_GPIO_SET_EN && cmd != BT848_GPIO_GET_DATA &&
668 cmd != BT848_GPIO_SET_DATA) /* no frame buffer allocated (ioctl failed) */
669 return( ENOMEM );
670#else
984263bc
MD
671 if (bktr->bigbuf == 0) /* no frame buffer allocated (ioctl failed) */
672 return( ENOMEM );
d3f3bfa9 673#endif
984263bc
MD
674
675 switch ( FUNCTION( minor(dev) ) ) {
676 case VIDEO_DEV:
7fccda02 677 return( video_ioctl( bktr, unit, cmd, ap->a_data, curthread ) );
984263bc 678 case TUNER_DEV:
7fccda02 679 return( tuner_ioctl( bktr, unit, cmd, ap->a_data, curthread ) );
984263bc
MD
680 }
681
682 return( ENXIO );
683}
684
685
686/*
687 *
688 */
a35cc233 689static int
fef8985e 690bktr_mmap(struct dev_mmap_args *ap)
984263bc 691{
b13267a5 692 cdev_t dev = ap->a_head.a_dev;
984263bc
MD
693 int unit;
694 bktr_ptr_t bktr;
695
696 unit = UNIT(minor(dev));
697
698 if (FUNCTION(minor(dev)) > 0) /* only allow mmap on /dev/bktr[n] */
fef8985e 699 return(EINVAL);
984263bc
MD
700
701 /* Get the device data */
702 bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
703 if (bktr == NULL) {
704 /* the device is no longer valid/functioning */
705 return (ENXIO);
706 }
707
fef8985e
MD
708 if (ap->a_nprot & PROT_EXEC)
709 return(EINVAL);
984263bc 710
fef8985e
MD
711 if (ap->a_offset < 0)
712 return(EINVAL);
984263bc 713
fef8985e
MD
714 if (ap->a_offset >= bktr->alloc_pages * PAGE_SIZE)
715 return(EINVAL);
984263bc 716
fef8985e
MD
717 ap->a_result = atop(vtophys(bktr->bigbuf) + ap->a_offset);
718 return(0);
984263bc
MD
719}
720
a35cc233 721static int
fef8985e 722bktr_poll(struct dev_poll_args *ap)
984263bc 723{
b13267a5 724 cdev_t dev = ap->a_head.a_dev;
984263bc
MD
725 int unit;
726 bktr_ptr_t bktr;
727 int revents = 0;
984263bc
MD
728
729 unit = UNIT(minor(dev));
730
731 /* Get the device data */
732 bktr = (struct bktr_softc*)devclass_get_softc(bktr_devclass, unit);
733 if (bktr == NULL) {
734 /* the device is no longer valid/functioning */
735 return (ENXIO);
736 }
737
a35cc233 738 LOCK_VBI(bktr);
052b6ffa 739 crit_enter();
984263bc 740
fef8985e 741 if (ap->a_events & (POLLIN | POLLRDNORM)) {
984263bc
MD
742
743 switch ( FUNCTION( minor(dev) ) ) {
744 case VBI_DEV:
745 if(bktr->vbisize == 0)
fef8985e 746 selrecord(curthread, &bktr->vbi_select);
984263bc 747 else
fef8985e 748 revents |= ap->a_events & (POLLIN | POLLRDNORM);
984263bc
MD
749 break;
750 }
751 }
752
052b6ffa 753 crit_exit();
a35cc233 754 UNLOCK_VBI(bktr);
984263bc 755
fef8985e
MD
756 ap->a_events = revents;
757 return (0);
984263bc 758}