2 * Copyright (c) 2003, 2004, 2005
3 * John Wehle <john@feith.com>. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by John Wehle.
16 * 4. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * Conexant MPEG-2 Codec driver. Supports the CX23415 / CX23416
34 * chips that are on the Hauppauge PVR-250 and PVR-350 video
35 * capture cards. Currently only the encoder is supported.
37 * This driver was written using the invaluable information
38 * compiled by The IvyTV Project (ivtv.sourceforge.net).
41 #include <sys/param.h>
42 #include <sys/systm.h>
45 #include <sys/kernel.h>
47 #include <sys/module.h>
48 #include <sys/event.h>
50 #include <sys/signalvar.h>
51 #include <sys/thread2.h>
52 #include <sys/vnode.h>
53 #include <sys/resource.h>
57 #include <machine/clock.h>
59 #include <dev/video/meteor/ioctl_meteor.h>
60 #include <dev/video/bktr/ioctl_bt848.h>
62 #include <bus/pci/pcireg.h>
63 #include <bus/pci/pcivar.h>
65 #include <dev/video/cxm/cxm.h>
67 #include <bus/iicbus/iiconf.h>
70 * Various supported device vendors/types and their names.
72 static struct cxm_dev cxm_devs[] = {
73 { PCI_VENDOR_ICOMPRESSION, PCI_PRODUCT_ICOMPRESSION_ITVC15,
74 "Conexant iTVC15 MPEG Coder" },
75 { PCI_VENDOR_ICOMPRESSION, PCI_PRODUCT_ICOMPRESSION_ITVC16,
76 "Conexant iTVC16 MPEG Coder" },
81 static int cxm_probe(device_t dev);
82 static int cxm_attach(device_t dev);
83 static int cxm_detach(device_t dev);
84 static int cxm_shutdown(device_t dev);
85 static void cxm_intr(void *arg);
87 static void cxm_child_detached(device_t dev, device_t child);
88 static int cxm_read_ivar(device_t bus, device_t dev,
89 int index, uintptr_t* val);
90 static int cxm_write_ivar(device_t bus, device_t dev,
91 int index, uintptr_t val);
94 static device_method_t cxm_methods[] = {
95 /* Device interface */
96 DEVMETHOD(device_probe, cxm_probe),
97 DEVMETHOD(device_attach, cxm_attach),
98 DEVMETHOD(device_detach, cxm_detach),
99 DEVMETHOD(device_shutdown, cxm_shutdown),
102 DEVMETHOD(bus_child_detached, cxm_child_detached),
103 DEVMETHOD(bus_print_child, bus_generic_print_child),
104 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
105 DEVMETHOD(bus_read_ivar, cxm_read_ivar),
106 DEVMETHOD(bus_write_ivar, cxm_write_ivar),
111 static driver_t cxm_driver = {
114 sizeof(struct cxm_softc),
117 static devclass_t cxm_devclass;
119 static d_open_t cxm_open;
120 static d_close_t cxm_close;
121 static d_read_t cxm_read;
122 static d_ioctl_t cxm_ioctl;
123 static d_kqfilter_t cxm_kqfilter;
125 static void cxm_filter_detach(struct knote *);
126 static int cxm_filter(struct knote *, long);
128 #define CDEV_MAJOR 93
130 static struct dev_ops cxm_ops = {
131 { "cxm", CDEV_MAJOR, D_KQFILTER },
133 .d_close = cxm_close,
135 .d_ioctl = cxm_ioctl,
136 .d_kqfilter = cxm_kqfilter
139 MODULE_DEPEND(cxm, cxm_iic, 1, 1, 1);
140 DRIVER_MODULE(cxm, pci, cxm_driver, cxm_devclass, 0, 0);
143 static struct cxm_codec_audio_format codec_audio_formats[] = {
144 { 44100, 0xb8 }, /* 44.1 Khz, MPEG-1 Layer II, 224 kb/s */
145 { 48000, 0xe9 } /* 48 Khz, MPEG-1 Layer II, 384 kb/s */
152 static struct cxm_codec_profile vcd_ntsc_profile = {
153 "MPEG-1 VideoCD NTSC video and MPEG audio",
154 CXM_FW_STREAM_TYPE_VCD,
163 * Spatial filter = Manual, Temporal filter = Manual
164 * Median filter = Horizontal / Vertical
165 * Spatial filter value = 1, Temporal filter value = 4
171 static struct cxm_codec_profile vcd_pal_profile = {
172 "MPEG-1 VideoCD PAL video and MPEG audio",
173 CXM_FW_STREAM_TYPE_VCD,
182 * Spatial filter = Manual, Temporal filter = Manual
183 * Median filter = Horizontal / Vertical
184 * Spatial filter value = 1, Temporal filter value = 4
190 static struct cxm_codec_profile svcd_ntsc_profile = {
191 "MPEG-2 SuperVCD NTSC video and MPEG audio",
192 CXM_FW_STREAM_TYPE_SVCD,
198 /* 2.5 Mb/s peak limit to keep bbdmux followed by mplex -f 4 happy */
199 { 0, 1150000, 2500000 },
202 * Spatial filter = Manual, Temporal filter = Manual
203 * Median filter = Horizontal / Vertical
204 * Spatial filter value = 1, Temporal filter value = 4
210 static struct cxm_codec_profile svcd_pal_profile = {
211 "MPEG-2 SuperVCD PAL video and MPEG audio",
212 CXM_FW_STREAM_TYPE_SVCD,
218 /* 2.5 Mb/s peak limit to keep bbdmux followed by mplex -f 4 happy */
219 { 0, 1150000, 2500000 },
222 * Spatial filter = Manual, Temporal filter = Manual
223 * Median filter = Horizontal / Vertical
224 * Spatial filter value = 1, Temporal filter value = 4
230 static struct cxm_codec_profile dvd_half_d1_ntsc_profile = {
231 "MPEG-2 DVD NTSC video and MPEG audio",
232 CXM_FW_STREAM_TYPE_DVD,
238 { 0, 4000000, 4520000 }, /* 4 hours on 8.54 GB media */
241 * Spatial filter = Manual, Temporal filter = Manual
242 * Median filter = Horizontal / Vertical
243 * Spatial filter value = 1, Temporal filter value = 4
249 static struct cxm_codec_profile dvd_half_d1_pal_profile = {
250 "MPEG-2 DVD PAL video and MPEG audio",
251 CXM_FW_STREAM_TYPE_DVD,
257 { 0, 4000000, 4520000 }, /* 4 hours on 8.54 GB media */
260 * Spatial filter = Manual, Temporal filter = Manual
261 * Median filter = Horizontal / Vertical
262 * Spatial filter value = 1, Temporal filter value = 4
268 static struct cxm_codec_profile dvd_full_d1_ntsc_profile = {
269 "MPEG-2 DVD NTSC video and MPEG audio",
270 CXM_FW_STREAM_TYPE_DVD,
276 /* 9.52 Mb/s peak limit to keep bbdmux followed by mplex -f 8 happy */
277 { 0, 9000000, 9520000 }, /* 1 hour on 4.7 GB media */
280 * Spatial filter = Manual, Temporal filter = Manual
281 * Median filter = Horizontal / Vertical
282 * Spatial filter value = 1, Temporal filter value = 4
288 static struct cxm_codec_profile dvd_full_d1_pal_profile = {
289 "MPEG-2 DVD PAL video and MPEG audio",
290 CXM_FW_STREAM_TYPE_DVD,
296 /* 9.52 Mb/s peak limit to keep bbdmux followed by mplex -f 8 happy */
297 { 0, 9000000, 9520000 }, /* 1 hour on 4.7 GB media */
300 * Spatial filter = Manual, Temporal filter = Manual
301 * Median filter = Horizontal / Vertical
302 * Spatial filter value = 1, Temporal filter value = 4
309 static const struct cxm_codec_profile
310 *codec_profiles[] = {
315 &dvd_half_d1_ntsc_profile,
316 &dvd_half_d1_pal_profile,
317 &dvd_full_d1_ntsc_profile,
318 &dvd_full_d1_pal_profile
323 cxm_queue_firmware_command(struct cxm_softc *sc,
324 enum cxm_mailbox_name mbx_name, uint32_t cmd,
325 uint32_t *parameters, unsigned int nparameters)
328 unsigned int mailbox;
329 uint32_t completed_command;
332 if (nparameters > CXM_MBX_MAX_PARAMETERS) {
333 device_printf(sc->dev, "too many parameters for mailbox\n");
340 case cxm_dec_mailbox:
341 mailbox = sc->dec_mbx
342 + CXM_MBX_FW_CMD_MAILBOX *sizeof(struct cxm_mailbox);
345 case cxm_enc_mailbox:
346 mailbox = sc->enc_mbx
347 + CXM_MBX_FW_CMD_MAILBOX *sizeof(struct cxm_mailbox);
355 for (i = 0; i < CXM_MBX_FW_CMD_MAILBOXES; i++) {
356 flags = CSR_READ_4(sc,
358 + offsetof(struct cxm_mailbox, flags));
359 if (!(flags & CXM_MBX_FLAG_IN_USE))
363 * Mail boxes containing certain completed commands
364 * for which the results are never needed can be reused.
367 if ((flags & (CXM_MBX_FLAG_DRV_DONE | CXM_MBX_FLAG_FW_DONE))
368 == (CXM_MBX_FLAG_DRV_DONE | CXM_MBX_FLAG_FW_DONE)) {
372 + offsetof(struct cxm_mailbox, command));
375 * DMA results are always check by reading the
376 * DMA status register ... never by checking
377 * the mailbox after the command has completed.
380 if (completed_command == CXM_FW_CMD_SCHED_DMA_TO_HOST)
384 mailbox += sizeof(struct cxm_mailbox);
387 if (i >= CXM_MBX_FW_CMD_MAILBOXES) {
392 CSR_WRITE_4(sc, mailbox + offsetof(struct cxm_mailbox, flags),
393 CXM_MBX_FLAG_IN_USE);
396 * PCI writes may be buffered so force the
397 * write to complete by reading the last
401 CSR_READ_4(sc, mailbox + offsetof(struct cxm_mailbox, flags));
405 CSR_WRITE_4(sc, mailbox + offsetof(struct cxm_mailbox, command), cmd);
406 CSR_WRITE_4(sc, mailbox + offsetof(struct cxm_mailbox, timeout),
409 for (i = 0; i < nparameters; i++)
412 + offsetof(struct cxm_mailbox, parameters)
413 + i * sizeof(uint32_t),
416 for (; i < CXM_MBX_MAX_PARAMETERS; i++)
419 + offsetof(struct cxm_mailbox, parameters)
420 + i * sizeof(uint32_t), 0);
422 CSR_WRITE_4(sc, mailbox + offsetof(struct cxm_mailbox, flags),
423 CXM_MBX_FLAG_IN_USE | CXM_MBX_FLAG_DRV_DONE);
430 cxm_firmware_command(struct cxm_softc *sc,
431 enum cxm_mailbox_name mbx_name, uint32_t cmd,
432 uint32_t *parameters, unsigned int nparameters)
437 unsigned int mailbox;
445 case cxm_dec_mailbox:
450 case cxm_enc_mailbox:
459 mailbox = cxm_queue_firmware_command(sc, mbx_name, cmd,
460 parameters, nparameters);
462 device_printf(sc->dev, "no free mailboxes\n");
466 /* Give the firmware a chance to start processing the request */
467 tsleep(bmp, 0, wmesg, hz / 100);
469 for (i = 0; i < 100; i++) {
470 flags = CSR_READ_4(sc,
472 + offsetof(struct cxm_mailbox, flags));
473 if ((flags & CXM_MBX_FLAG_FW_DONE))
477 tsleep(bmp, 0, wmesg, hz / 100);
481 device_printf(sc->dev, "timeout\n");
485 result = CSR_READ_4(sc,
487 + offsetof(struct cxm_mailbox, result));
489 for (i = 0; i < nparameters; i++)
493 + offsetof(struct cxm_mailbox, parameters)
494 + i * sizeof(uint32_t));
496 CSR_WRITE_4(sc, mailbox + offsetof(struct cxm_mailbox, flags), 0);
498 return result == 0 ? 0 : -1;
503 cxm_firmware_command_nosleep(struct cxm_softc *sc,
504 enum cxm_mailbox_name mbx_name, uint32_t cmd,
505 uint32_t *parameters, unsigned int nparameters)
508 unsigned int mailbox;
512 for (i = 0; i < 100; i++) {
513 mailbox = cxm_queue_firmware_command(sc, mbx_name, cmd,
514 parameters, nparameters);
523 device_printf(sc->dev, "no free mailboxes\n");
527 /* Give the firmware a chance to start processing the request */
530 for (i = 0; i < 100; i++) {
531 flags = CSR_READ_4(sc,
533 + offsetof(struct cxm_mailbox, flags));
534 if ((flags & CXM_MBX_FLAG_FW_DONE))
542 device_printf(sc->dev, "timeout\n");
546 result = CSR_READ_4(sc,
548 + offsetof(struct cxm_mailbox, result));
550 for (i = 0; i < nparameters; i++)
554 + offsetof(struct cxm_mailbox, parameters)
555 + i * sizeof(uint32_t));
557 CSR_WRITE_4(sc, mailbox + offsetof(struct cxm_mailbox, flags), 0);
559 return result == 0 ? 0 : -1;
564 cxm_stop_firmware(struct cxm_softc *sc)
567 if (cxm_firmware_command_nosleep(sc, cxm_enc_mailbox,
568 CXM_FW_CMD_ENC_HALT_FW, NULL, 0) < 0)
571 if (sc->type == cxm_iTVC15_type
572 && cxm_firmware_command_nosleep(sc, cxm_dec_mailbox,
573 CXM_FW_CMD_DEC_HALT_FW,
585 cxm_set_irq_mask(struct cxm_softc *sc, uint32_t mask)
589 CSR_WRITE_4(sc, CXM_REG_IRQ_MASK, mask);
592 * PCI writes may be buffered so force the
593 * write to complete by reading the last
597 CSR_READ_4(sc, CXM_REG_IRQ_MASK);
606 cxm_set_irq_status(struct cxm_softc *sc, uint32_t status)
609 CSR_WRITE_4(sc, CXM_REG_IRQ_STATUS, status);
612 * PCI writes may be buffered so force the
613 * write to complete by reading the last
617 CSR_READ_4(sc, CXM_REG_IRQ_STATUS);
622 cxm_stop_hardware(struct cxm_softc *sc)
625 if (cxm_saa7115_mute(sc) < 0)
627 if (cxm_msp_mute(sc) < 0)
631 /* Halt the firmware */
632 if (sc->enc_mbx != -1) {
633 if (cxm_stop_firmware(sc) < 0)
637 /* Mask all interrupts */
638 cxm_set_irq_mask(sc, 0xffffffff);
641 CSR_WRITE_4(sc, CXM_REG_VDM, CXM_CMD_VDM_STOP);
644 CSR_WRITE_4(sc, CXM_REG_AO, CXM_CMD_AO_STOP);
647 CSR_WRITE_4(sc, CXM_REG_APU, CXM_CMD_APU_PING);
650 CSR_WRITE_4(sc, CXM_REG_VPU, sc->type == cxm_iTVC15_type
652 : CXM_CMD_VPU_STOP16);
654 /* Reset Hw Blocks */
655 CSR_WRITE_4(sc, CXM_REG_HW_BLOCKS, CXM_CMD_HW_BLOCKS_RST);
658 CSR_WRITE_4(sc, CXM_REG_SPU, CXM_CMD_SPU_STOP);
668 cxm_download_firmware(struct cxm_softc *sc)
673 /* Check if firmware is compiled in */
674 if (strncmp((const char *)cxm_enc_fw, "NOFW", 4) == 0) {
675 device_printf(sc->dev, "encoder firmware not compiled in\n");
677 } else if (strncmp((const char *)cxm_dec_fw, "NOFW", 4) == 0) {
678 device_printf(sc->dev, "decoder firmware not compiled in\n");
682 /* Download the encoder firmware */
683 fw = (const uint32_t *)cxm_enc_fw;
684 for (i = 0; i < CXM_FW_SIZE; i += sizeof(*fw))
685 CSR_WRITE_4(sc, CXM_MEM_ENC + i, *fw++);
687 /* Download the decoder firmware */
688 if (sc->type == cxm_iTVC15_type) {
689 fw = (const uint32_t *)cxm_dec_fw;
690 for (i = 0; i < CXM_FW_SIZE; i += sizeof(*fw))
691 CSR_WRITE_4(sc, CXM_MEM_DEC + i, *fw++);
699 cxm_init_hardware(struct cxm_softc *sc)
702 unsigned int mailbox;
705 if (cxm_stop_hardware(sc) < 0)
708 /* Initialize encoder SDRAM pre-charge */
709 CSR_WRITE_4(sc, CXM_REG_ENC_SDRAM_PRECHARGE,
710 CXM_CMD_SDRAM_PRECHARGE_INIT);
712 /* Initialize encoder SDRAM refresh to 1us */
713 CSR_WRITE_4(sc, CXM_REG_ENC_SDRAM_REFRESH,
714 CXM_CMD_SDRAM_REFRESH_INIT);
716 /* Initialize decoder SDRAM pre-charge */
717 CSR_WRITE_4(sc, CXM_REG_DEC_SDRAM_PRECHARGE,
718 CXM_CMD_SDRAM_PRECHARGE_INIT);
720 /* Initialize decoder SDRAM refresh to 1us */
721 CSR_WRITE_4(sc, CXM_REG_DEC_SDRAM_REFRESH,
722 CXM_CMD_SDRAM_REFRESH_INIT);
727 if (cxm_download_firmware(sc) < 0)
731 CSR_WRITE_4(sc, CXM_REG_SPU,
732 CSR_READ_4(sc, CXM_REG_SPU) & CXM_MASK_SPU_ENABLE);
734 /* Wait for 1 second */
738 CSR_WRITE_4(sc, CXM_REG_VPU,
739 CSR_READ_4(sc, CXM_REG_VPU)
740 & (sc->type == cxm_iTVC15_type
741 ? CXM_MASK_VPU_ENABLE15
742 : CXM_MASK_VPU_ENABLE16));
744 /* Wait for 1 second */
747 /* Locate encoder mailbox */
748 mailbox = CXM_MEM_ENC;
749 for (i = 0; i < CXM_MEM_ENC_SIZE; i += 0x100)
750 if (CSR_READ_4(sc, mailbox + i) == 0x12345678
751 && CSR_READ_4(sc, mailbox + i + 4) == 0x34567812
752 && CSR_READ_4(sc, mailbox + i + 8) == 0x56781234
753 && CSR_READ_4(sc, mailbox + i + 12) == 0x78123456)
756 if (i >= CXM_MEM_ENC_SIZE)
759 sc->enc_mbx = mailbox + i + 16;
761 /* Locate decoder mailbox */
762 if (sc->type == cxm_iTVC15_type) {
763 mailbox = CXM_MEM_DEC;
764 for (i = 0; i < CXM_MEM_DEC_SIZE; i += 0x100)
765 if (CSR_READ_4(sc, mailbox + i) == 0x12345678
766 && CSR_READ_4(sc, mailbox + i + 4) == 0x34567812
767 && CSR_READ_4(sc, mailbox + i + 8) == 0x56781234
768 && CSR_READ_4(sc, mailbox + i + 12) == 0x78123456)
771 if (i >= CXM_MEM_DEC_SIZE)
774 sc->dec_mbx = mailbox + i + 16;
777 /* Get encoder firmware version */
779 if (cxm_firmware_command_nosleep(sc, cxm_enc_mailbox,
780 CXM_FW_CMD_ENC_GET_FW_VER,
784 device_printf(sc->dev, "encoder firmware version %#x\n",
785 (unsigned int)parameter);
787 /* Get decoder firmware version */
788 if (sc->type == cxm_iTVC15_type) {
790 if (cxm_firmware_command_nosleep(sc, cxm_dec_mailbox,
791 CXM_FW_CMD_DEC_GET_FW_VER,
795 device_printf(sc->dev, "decoder firmware version %#x\n",
796 (unsigned int)parameter);
804 cxm_configure_encoder(struct cxm_softc *sc)
808 uint32_t parameters[12];
809 const struct cxm_codec_profile *cpp;
811 if (sc->source == cxm_fm_source)
812 switch (cxm_tuner_selected_channel_set(sc)) {
814 case CHNLSET_CABLEIRC:
815 case CHNLSET_JPNBCST:
816 case CHNLSET_JPNCABLE:
825 fps = cxm_saa7115_detected_fps(sc);
830 if (sc->profile->fps != fps) {
833 * Pick a profile with the correct fps using the
834 * chosen stream type and width to decide between
835 * the VCD, SVCD, or DVD profiles.
838 for (i = 0; i < NUM_ELEMENTS(codec_profiles); i++)
839 if (codec_profiles[i]->fps == fps
840 && codec_profiles[i]->stream_type
841 == sc->profile->stream_type
842 && codec_profiles[i]->width == sc->profile->width)
845 if (i >= NUM_ELEMENTS(codec_profiles))
848 sc->profile = codec_profiles[i];
853 if (cxm_saa7115_configure(sc,
854 cpp->width, cpp->source_height, fps,
855 cpp->audio_sample_rate) < 0)
858 /* assign dma block len */
859 parameters[0] = 1; /* Transfer block size = 1 */
860 parameters[1] = 1; /* Units = 1 (frames) */
861 if (cxm_firmware_command(sc, cxm_enc_mailbox,
862 CXM_FW_CMD_ASSIGN_DMA_BLOCKLEN,
867 /* assign program index info */
868 parameters[0] = 0; /* Picture mask = 0 (don't generate index) */
869 parameters[1] = 0; /* Num_req = 0 */
870 if (cxm_firmware_command(sc, cxm_enc_mailbox,
871 CXM_FW_CMD_ASSIGN_PGM_INDEX_INFO,
875 /* assign stream type */
876 parameters[0] = cpp->stream_type;
877 if (cxm_firmware_command(sc, cxm_enc_mailbox,
878 CXM_FW_CMD_ASSIGN_STREAM_TYPE,
882 /* assign output port */
883 parameters[0] = 0; /* 0 (Memory) */
884 if (cxm_firmware_command(sc, cxm_enc_mailbox,
885 CXM_FW_CMD_ASSIGN_OUTPUT_PORT,
889 /* assign framerate */
890 parameters[0] = cpp->fps == 30 ? 0 : 1;
891 if (cxm_firmware_command(sc, cxm_enc_mailbox,
892 CXM_FW_CMD_ASSIGN_FRAME_RATE,
896 /* assign frame size */
897 parameters[0] = cpp->height;
898 parameters[1] = cpp->width;
899 if (cxm_firmware_command(sc, cxm_enc_mailbox,
900 CXM_FW_CMD_ASSIGN_FRAME_SIZE,
904 /* assign aspect ratio */
905 parameters[0] = cpp->aspect;
906 if (cxm_firmware_command(sc, cxm_enc_mailbox,
907 CXM_FW_CMD_ASSIGN_ASPECT_RATIO,
911 /* assign bitrates */
912 parameters[0] = cpp->bitrate.mode;
913 parameters[1] = cpp->bitrate.average;
914 parameters[2] = cpp->bitrate.peak / 400;
915 if (cxm_firmware_command(sc, cxm_enc_mailbox,
916 CXM_FW_CMD_ASSIGN_BITRATES,
920 /* assign gop closure */
921 parameters[0] = cpp->gop.closure;
922 if (cxm_firmware_command(sc, cxm_enc_mailbox,
923 CXM_FW_CMD_ASSIGN_GOP_CLOSURE,
927 /* assign gop properties */
928 parameters[0] = cpp->gop.frames;
929 parameters[1] = cpp->gop.bframes;
930 if (cxm_firmware_command(sc, cxm_enc_mailbox,
931 CXM_FW_CMD_ASSIGN_GOP_PROPERTIES,
935 /* assign 3 2 pulldown */
936 parameters[0] = cpp->pulldown;
937 if (cxm_firmware_command(sc, cxm_enc_mailbox,
938 CXM_FW_CMD_ASSIGN_3_2_PULLDOWN,
942 /* assign dnr filter mode */
943 parameters[0] = cpp->dnr.mode;
944 parameters[1] = cpp->dnr.type;
945 if (cxm_firmware_command(sc, cxm_enc_mailbox,
946 CXM_FW_CMD_ASSIGN_DNR_FILTER_MODE,
950 /* assign dnr filter props */
951 parameters[0] = cpp->dnr.spatial;
952 parameters[1] = cpp->dnr.temporal;
953 if (cxm_firmware_command(sc, cxm_enc_mailbox,
954 CXM_FW_CMD_ASSIGN_DNR_FILTER_PROPERTIES,
959 * assign audio properties
962 for (i = 0; i < NUM_ELEMENTS(codec_audio_formats); i++)
963 if (codec_audio_formats[i].sample_rate
964 == cpp->audio_sample_rate)
967 if (i >= NUM_ELEMENTS(codec_audio_formats))
970 parameters[0] = codec_audio_formats[i].format;
971 if (cxm_firmware_command(sc, cxm_enc_mailbox,
972 CXM_FW_CMD_ASSIGN_AUDIO_PROPERTIES,
976 /* assign coring levels */
977 parameters[0] = 0; /* luma_h */
978 parameters[1] = 255; /* luma_l */
979 parameters[2] = 0; /* chroma_h */
980 parameters[3] = 255; /* chroma_l */
981 if (cxm_firmware_command(sc, cxm_enc_mailbox,
982 CXM_FW_CMD_ASSIGN_CORING_LEVELS,
986 /* assign spatial filter type */
987 parameters[0] = 3; /* Luminance filter = 3 (2D H/V Separable) */
988 parameters[1] = 1; /* Chrominance filter = 1 (1D Horizontal) */
989 if (cxm_firmware_command(sc, cxm_enc_mailbox,
990 CXM_FW_CMD_ASSIGN_SPATIAL_FILTER_TYPE,
994 /* assign frame drop rate */
996 if (cxm_firmware_command(sc, cxm_enc_mailbox,
997 CXM_FW_CMD_ASSIGN_FRAME_DROP_RATE,
1001 /* assign placeholder */
1002 parameters[0] = 0; /* type = 0 (Extension / UserData) */
1003 parameters[1] = 0; /* period */
1004 parameters[2] = 0; /* size_t */
1005 parameters[3] = 0; /* arg0 */
1006 parameters[4] = 0; /* arg1 */
1007 parameters[5] = 0; /* arg2 */
1008 parameters[6] = 0; /* arg3 */
1009 parameters[7] = 0; /* arg4 */
1010 parameters[8] = 0; /* arg5 */
1011 parameters[9] = 0; /* arg6 */
1012 parameters[10] = 0; /* arg7 */
1013 parameters[11] = 0; /* arg8 */
1014 if (cxm_firmware_command(sc, cxm_enc_mailbox,
1015 CXM_FW_CMD_ASSIGN_PLACEHOLDER,
1016 parameters, 12) != 0)
1019 /* assign VBI properties */
1020 parameters[0] = 0xbd04; /* mode = 0 (sliced), stream and user data */
1021 parameters[1] = 0; /* frames per interrupt (only valid in raw mode) */
1022 parameters[2] = 0; /* total raw VBI frames (only valid in raw mode) */
1023 parameters[3] = 0x25256262; /* ITU 656 start codes (saa7115 table 24)*/
1024 parameters[4] = 0x38387f7f; /* ITU 656 stop codes (saa7115 table 24) */
1025 parameters[5] = cpp->vbi.nlines; /* lines per frame */
1026 parameters[6] = 1440; /* bytes per line = 720 pixels */
1027 if (cxm_firmware_command(sc, cxm_enc_mailbox,
1028 CXM_FW_CMD_ASSIGN_VBI_PROPERTIES,
1029 parameters, 7) != 0)
1032 /* assign VBI lines */
1033 parameters[0] = 0xffffffff; /* all lines */
1034 parameters[1] = 0; /* disable VBI features */
1038 if (cxm_firmware_command(sc, cxm_enc_mailbox,
1039 CXM_FW_CMD_ASSIGN_VBI_LINE,
1040 parameters, 5) != 0)
1043 /* assign number of lines in fields 1 and 2 */
1044 parameters[0] = cpp->source_height / 2 + cpp->vbi.nlines;
1045 parameters[1] = cpp->source_height / 2 + cpp->vbi.nlines;
1046 if (cxm_firmware_command(sc, cxm_enc_mailbox,
1047 CXM_FW_CMD_ASSIGN_NUM_VSYNC_LINES,
1048 parameters, 2) != 0)
1056 cxm_start_encoder(struct cxm_softc *sc)
1058 uint32_t parameters[4];
1066 if (cxm_configure_encoder(sc) < 0)
1069 /* Mute the video input if necessary. */
1070 parameters[0] = sc->source == cxm_fm_source ? 1 : 0;
1071 if (cxm_firmware_command(sc, cxm_enc_mailbox,
1072 CXM_FW_CMD_MUTE_VIDEO_INPUT,
1073 parameters, 1) != 0)
1076 /* Clear pending encoder interrupts (which are currently masked) */
1077 cxm_set_irq_status(sc, CXM_IRQ_ENC);
1079 /* Enable event notification */
1080 parameters[0] = 0; /* Event = 0 (refresh encoder input) */
1081 parameters[1] = 1; /* Notification = 1 (enable) */
1082 parameters[2] = 0x10000000; /* Interrupt bit */
1083 parameters[3] = -1; /* Mailbox = -1 (no mailbox) */
1084 if (cxm_firmware_command(sc, cxm_enc_mailbox,
1085 CXM_FW_CMD_ENC_EVENT_NOTIFICATION,
1086 parameters, 4) != 0)
1089 if (cxm_saa7115_mute(sc) < 0)
1091 if (cxm_msp_mute(sc) < 0)
1094 if (cxm_firmware_command(sc, cxm_enc_mailbox,
1095 CXM_FW_CMD_INITIALIZE_VIDEO_INPUT,
1099 if (cxm_saa7115_unmute(sc) < 0)
1101 if (cxm_msp_unmute(sc) < 0)
1104 /* Wait for 100ms */
1105 tsleep(&sc->encoding, 0, "cxmce", hz / 10);
1107 type = sc->mpeg ? CXM_FW_CAPTURE_STREAM_TYPE_MPEG
1108 : CXM_FW_CAPTURE_STREAM_TYPE_RAW;
1109 subtype = ((sc->mpeg || sc->source == cxm_fm_source)
1110 ? CXM_FW_CAPTURE_STREAM_PCM_AUDIO : 0)
1111 | ((sc->mpeg || sc->source != cxm_fm_source)
1112 ? CXM_FW_CAPTURE_STREAM_YUV : 0);
1114 /* Start the encoder */
1115 parameters[0] = type;
1116 parameters[1] = subtype;
1117 if (cxm_firmware_command(sc, cxm_enc_mailbox,
1118 CXM_FW_CMD_BEGIN_CAPTURE, parameters, 2) != 0)
1121 sc->enc_pool.offset = 0;
1122 sc->enc_pool.read = 0;
1123 sc->enc_pool.write = 0;
1125 sc->encoding_eos = 0;
1129 /* Enable interrupts */
1130 cxm_set_irq_mask(sc, sc->irq_mask & ~CXM_IRQ_ENC);
1137 cxm_stop_encoder(struct cxm_softc *sc)
1139 uint32_t parameters[4];
1146 type = sc->mpeg ? CXM_FW_CAPTURE_STREAM_TYPE_MPEG
1147 : CXM_FW_CAPTURE_STREAM_TYPE_RAW;
1148 subtype = ((sc->mpeg || sc->source == cxm_fm_source)
1149 ? CXM_FW_CAPTURE_STREAM_PCM_AUDIO : 0)
1150 | ((sc->mpeg || sc->source != cxm_fm_source)
1151 ? CXM_FW_CAPTURE_STREAM_YUV : 0);
1153 /* Stop the encoder */
1154 parameters[0] = sc->mpeg ? 0 : 1; /* When = 0 (end of GOP) */
1155 parameters[1] = type;
1156 parameters[2] = subtype;
1157 if (cxm_firmware_command(sc, cxm_enc_mailbox,
1158 CXM_FW_CMD_END_CAPTURE, parameters, 3) != 0)
1161 /* Wait for up to 1 second */
1163 if (!sc->encoding_eos)
1164 tsleep(&sc->encoding_eos, 0, "cxmeos", hz);
1167 if (sc->mpeg && !sc->encoding_eos)
1168 device_printf(sc->dev, "missing encoder EOS\n");
1170 /* Disable event notification */
1171 parameters[0] = 0; /* Event = 0 (refresh encoder input) */
1172 parameters[1] = 0; /* Notification = 0 (disable) */
1173 parameters[2] = 0x10000000; /* Interrupt bit */
1174 parameters[3] = -1; /* Mailbox = -1 (no mailbox) */
1175 if (cxm_firmware_command(sc, cxm_enc_mailbox,
1176 CXM_FW_CMD_ENC_EVENT_NOTIFICATION,
1177 parameters, 4) != 0)
1180 /* Disable interrupts */
1181 cxm_set_irq_mask(sc, sc->irq_mask | CXM_IRQ_ENC);
1190 cxm_pause_encoder(struct cxm_softc *sc)
1194 /* Pause the encoder */
1196 if (cxm_firmware_command(sc, cxm_enc_mailbox,
1197 CXM_FW_CMD_PAUSE_ENCODER, ¶meter, 1) != 0)
1205 cxm_unpause_encoder(struct cxm_softc *sc)
1209 /* Unpause the encoder */
1211 if (cxm_firmware_command(sc, cxm_enc_mailbox,
1212 CXM_FW_CMD_PAUSE_ENCODER, ¶meter, 1) != 0)
1220 cxm_encoder_fixup_byte_order(struct cxm_softc *sc,
1221 unsigned int current, size_t offset)
1223 unsigned int strips;
1227 unsigned int macroblocks_per_line;
1228 unsigned int scratch;
1229 unsigned int words_per_line;
1234 switch (sc->enc_pool.bufs[current].byte_order) {
1235 case cxm_device_mpeg_byte_order:
1238 * Convert each 32 bit word to the proper byte ordering.
1242 ptr = (uint32_t *)sc->enc_pool.bufs[current].vaddr;
1243 nbytes != sc->enc_pool.bufs[current].size;
1244 nbytes += sizeof(*ptr), ptr++)
1245 *ptr = bswap32(*ptr);
1248 case cxm_device_yuv12_byte_order:
1251 * Convert each macro block to planar using
1252 * a scratch buffer (the buffer prior to the
1253 * current buffer is always free since it marks
1254 * the end of the ring buffer).
1257 scratch = (current + (CXM_SG_BUFFERS - 1)) % CXM_SG_BUFFERS;
1264 src = (uint32_t *)sc->enc_pool.bufs[current].vaddr;
1265 words_per_line = sc->profile->width / sizeof(*ptr);
1266 macroblocks_per_line
1267 = sc->profile->width / CXM_MACROBLOCK_WIDTH;
1268 strips = sc->enc_pool.bufs[current].size
1269 / (macroblocks_per_line * CXM_MACROBLOCK_SIZE);
1271 for (i = 0; i < strips; i++) {
1272 ptr = (uint32_t *)sc->enc_pool.bufs[scratch].vaddr
1273 + i * macroblocks_per_line * CXM_MACROBLOCK_SIZE
1275 for (j = 0; j < macroblocks_per_line; j++) {
1276 for (k = 0; k < CXM_MACROBLOCK_HEIGHT; k++) {
1277 #if CXM_MACROBLOCK_WIDTH != 16
1278 # error CXM_MACROBLOCK_WIDTH != 16
1280 *(ptr + k * words_per_line)
1282 *(ptr + k * words_per_line + 1)
1284 *(ptr + k * words_per_line + 2)
1286 *(ptr + k * words_per_line + 3)
1289 ptr += CXM_MACROBLOCK_WIDTH / sizeof(*ptr);
1293 sc->enc_pool.bufs[scratch].size
1294 = sc->enc_pool.bufs[current].size;
1303 sc->enc_pool.bufs[current].byte_order = cxm_host_byte_order;
1310 cxm_encoder_dma_discard(struct cxm_softc *sc)
1312 uint32_t parameters[3];
1314 /* Discard the DMA request */
1318 if (cxm_queue_firmware_command(sc, cxm_enc_mailbox,
1319 CXM_FW_CMD_SCHED_DMA_TO_HOST,
1320 parameters, 3) == -1) {
1321 device_printf(sc->dev,
1322 "failed to discard encoder dma request\n");
1326 sc->encoding_dma = -1;
1331 cxm_encoder_dma_done(struct cxm_softc *sc)
1333 int buffers_pending;
1336 if (!sc->encoding_dma) {
1337 device_printf(sc->dev,
1338 "encoder dma not already in progress\n");
1342 buffers_pending = sc->encoding_dma;
1343 sc->encoding_dma = 0;
1345 if (buffers_pending < 0)
1348 status = CSR_READ_4(sc, CXM_REG_DMA_STATUS) & 0x0000000f;
1351 & (CXM_DMA_ERROR_LIST | CXM_DMA_ERROR_WRITE | CXM_DMA_SUCCESS))
1352 != CXM_DMA_SUCCESS) {
1353 device_printf(sc->dev, "encoder dma status %#x\n",
1354 (unsigned int)status);
1358 /* Update the books */
1360 sc->enc_pool.write = (sc->enc_pool.write + buffers_pending)
1364 /* signal anyone requesting notification */
1366 ksignal (sc->enc_proc, sc->enc_signal);
1368 /* wakeup anyone waiting for data */
1369 wakeup(&sc->enc_pool.read);
1371 /* wakeup anyone polling for data */
1372 KNOTE(&sc->enc_kq.ki_note, 0);
1377 cxm_encoder_dma_request(struct cxm_softc *sc)
1379 enum cxm_byte_order byte_order;
1381 int buffers_pending;
1382 unsigned int current;
1384 unsigned int mailbox;
1385 unsigned int macroblocks_per_line;
1386 unsigned int nrequests;
1387 unsigned int strips;
1388 uint32_t parameters[CXM_MBX_MAX_PARAMETERS];
1390 size_t max_sg_segment;
1396 if (sc->encoding_dma) {
1397 device_printf(sc->dev, "encoder dma already in progress\n");
1398 cxm_encoder_dma_discard(sc);
1402 mailbox = sc->enc_mbx
1403 + CXM_MBX_FW_DMA_MAILBOX * sizeof(struct cxm_mailbox);
1405 for (i = 0; i < CXM_MBX_MAX_PARAMETERS; i++)
1409 + offsetof(struct cxm_mailbox, parameters)
1410 + i * sizeof(uint32_t)
1413 byte_order = cxm_device_mpeg_byte_order;
1414 max_sg_segment = CXM_SG_SEGMENT;
1416 type = parameters[0];
1420 requests[nrequests].offset = parameters[1];
1421 requests[nrequests++].size = parameters[2];
1425 byte_order = cxm_device_yuv12_byte_order;
1428 * Simplify macroblock unpacking by ensuring
1429 * that strips don't span buffers.
1432 #if CXM_MACROBLOCK_SIZE % 256
1433 # error CXM_MACROBLOCK_SIZE not a multiple of 256
1436 macroblocks_per_line = sc->profile->width
1437 / CXM_MACROBLOCK_WIDTH;
1438 strips = CXM_SG_SEGMENT
1439 / (macroblocks_per_line * CXM_MACROBLOCK_SIZE);
1440 max_sg_segment = strips
1441 * macroblocks_per_line * CXM_MACROBLOCK_SIZE;
1443 requests[nrequests].offset = parameters[1]; /* Y */
1444 requests[nrequests++].size = parameters[2];
1445 requests[nrequests].offset = parameters[3]; /* UV */
1446 requests[nrequests++].size = parameters[4];
1449 case 2: /* PCM (audio) */
1452 device_printf(sc->dev, "encoder dma type %#x unsupported\n",
1453 (unsigned int)type);
1454 cxm_encoder_dma_discard(sc);
1459 * Determine the number of buffers free at this * instant *
1460 * taking into consideration that the ring buffer wraps.
1463 buffers_free = sc->enc_pool.read - sc->enc_pool.write;
1464 if (buffers_free <= 0)
1465 buffers_free += CXM_SG_BUFFERS;
1469 * Build the scatter / gather list taking in
1470 * consideration that the ring buffer wraps,
1471 * at least one free buffer must always be
1472 * present to mark the end of the ring buffer,
1473 * and each transfer must be a multiple of 256.
1476 buffers_pending = 0;
1477 current = sc->enc_pool.write;
1479 for (i = 0; i < nrequests; i++) {
1480 if (!requests[i].size) {
1481 device_printf(sc->dev, "encoder dma size is zero\n");
1482 cxm_encoder_dma_discard(sc);
1486 while (requests[i].size) {
1487 sc->enc_pool.bufs[current].size
1488 = requests[i].size > max_sg_segment
1489 ? max_sg_segment : requests[i].size;
1490 sc->enc_pool.bufs[current].byte_order = byte_order;
1492 sc->enc_sg.vaddr[buffers_pending].src
1493 = requests[i].offset;
1494 sc->enc_sg.vaddr[buffers_pending].dst
1495 = sc->enc_pool.bufs[current].baddr;
1496 sc->enc_sg.vaddr[buffers_pending].size
1497 = (sc->enc_pool.bufs[current].size + 0x000000ff)
1500 requests[i].offset += sc->enc_pool.bufs[current].size;
1501 requests[i].size -= sc->enc_pool.bufs[current].size;
1503 current = (current + 1) % CXM_SG_BUFFERS;
1505 if (buffers_pending >= buffers_free) {
1506 device_printf(sc->dev,
1507 "encoder dma not enough buffer space free\n");
1508 cxm_encoder_dma_discard(sc);
1514 /* Mark the last transfer in the list */
1515 sc->enc_sg.vaddr[buffers_pending - 1].size |= 0x80000000;
1517 /* Schedule the DMA */
1518 parameters[0] = sc->enc_sg.baddr;
1519 parameters[1] = buffers_pending * sizeof(sc->enc_sg.vaddr[0]);
1520 parameters[2] = type;
1521 if (cxm_queue_firmware_command(sc, cxm_enc_mailbox,
1522 CXM_FW_CMD_SCHED_DMA_TO_HOST,
1523 parameters, 3) == -1) {
1524 device_printf(sc->dev,
1525 "failed to schedule encoder dma request\n");
1530 * Record the number of pending buffers for the
1531 * benefit of cxm_encoder_dma_done. Doing this
1532 * after queuing the command doesn't introduce
1533 * a race condition since we're already in the
1534 * interrupt handler.
1537 sc->encoding_dma = buffers_pending;
1542 cxm_encoder_wait_for_lock(struct cxm_softc *sc)
1551 * Wait for the tuner to lock.
1553 if (sc->source == cxm_fm_source || sc->source == cxm_tuner_source) {
1554 result = cxm_tuner_wait_for_lock(sc);
1560 * Wait for the video decoder to lock.
1562 if (sc->source != cxm_fm_source) {
1563 result = cxm_saa7115_wait_for_lock(sc);
1566 else if (result == 0)
1571 * Wait for the audio decoder to lock.
1573 if (sc->source == cxm_tuner_source) {
1574 muted = cxm_msp_is_muted(sc);
1576 result = cxm_msp_autodetect_standard(sc);
1579 else if (result == 0)
1582 if (muted == 0 && cxm_msp_unmute(sc) < 0)
1591 cxm_mapmem(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1593 bus_addr_t *busaddrp;
1596 * Only the first bus space address is needed
1597 * since it's known that the memory is physically
1598 * contiguous due to bus_dmamem_alloc.
1601 busaddrp = (bus_addr_t *)arg;
1602 *busaddrp = segs->ds_addr;
1607 * the boot time probe routine.
1610 cxm_probe(device_t dev)
1616 while(t->name != NULL) {
1617 if ((pci_get_vendor(dev) == t->vid) &&
1618 (pci_get_device(dev) == t->did)) {
1619 device_set_desc(dev, t->name);
1630 * the attach routine.
1633 cxm_attach(device_t dev)
1640 struct cxm_softc *sc;
1642 /* Get the device data */
1643 sc = device_get_softc(dev);
1644 unit = device_get_unit(dev);
1647 sc->type = cxm_iTVC15_type;
1649 switch(pci_get_device(dev)) {
1650 case PCI_PRODUCT_ICOMPRESSION_ITVC16:
1651 sc->type = cxm_iTVC16_type;
1659 * Enable bus mastering and memory mapped I/O.
1661 pci_enable_busmaster(dev);
1662 pci_enable_io(dev, SYS_RES_MEMORY);
1663 command = pci_read_config(dev, PCIR_COMMAND, 4);
1665 if (!(command & PCIM_CMD_MEMEN)) {
1666 device_printf(dev, "failed to enable memory mappings\n");
1672 * Map control/status registers.
1675 sc->mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
1676 0, ~0, 1, RF_ACTIVE);
1679 device_printf(dev, "could not map memory\n");
1684 sc->btag = rman_get_bustag(sc->mem_res);
1685 sc->bhandle = rman_get_bushandle(sc->mem_res);
1688 * Attach the I2C bus.
1690 sc->cxm_iic = device_add_child(dev, "cxm_iic", unit);
1693 device_printf(dev, "could not add cxm_iic\n");
1698 error = device_probe_and_attach(sc->cxm_iic);
1701 device_printf(dev, "could not attach cxm_iic\n");
1706 * Initialize the tuner.
1708 if (cxm_tuner_init(sc) < 0) {
1709 device_printf(dev, "could not initialize tuner\n");
1715 * Initialize the SAA7115.
1717 if (cxm_saa7115_init(sc) < 0) {
1718 device_printf(dev, "could not initialize video decoder\n");
1724 * Initialize the MSP3400.
1726 if (cxm_msp_init(sc) < 0) {
1727 device_printf(dev, "could not initialize audio decoder\n");
1733 * Initialize the IR Remote.
1735 if (cxm_ir_init(sc) < 0) {
1736 device_printf(dev, "could not initialize IR remote\n");
1745 * Disable the Conexant device.
1747 * This is done * after * attaching the I2C bus so
1748 * cxm_stop_hardware can mute the video and audio
1751 cxm_stop_hardware(sc);
1754 * Allocate our interrupt.
1757 sc->irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, &rid,
1758 0, ~0, 1, RF_SHAREABLE | RF_ACTIVE);
1760 if (sc->irq_res == NULL) {
1761 device_printf(dev, "could not map interrupt\n");
1766 error = bus_setup_intr(dev, sc->irq_res, 0,
1767 cxm_intr, sc, &sc->ih_cookie, NULL);
1769 device_printf(dev, "could not setup irq\n");
1775 * Allocate a DMA tag for the parent bus.
1777 error = bus_dma_tag_create(NULL, 1, 0,
1778 BUS_SPACE_MAXADDR_32BIT,
1779 BUS_SPACE_MAXADDR, NULL, NULL,
1780 BUS_SPACE_MAXSIZE_32BIT, 1,
1781 BUS_SPACE_MAXSIZE_32BIT, 0,
1784 device_printf(dev, "could not create parent bus DMA tag\n");
1789 * Allocate a DMA tag for the encoder buffers.
1791 error = bus_dma_tag_create(sc->parent_dmat, 256, 0,
1792 BUS_SPACE_MAXADDR_32BIT,
1793 BUS_SPACE_MAXADDR, NULL, NULL,
1795 BUS_SPACE_MAXSIZE_32BIT, 0,
1796 &sc->enc_pool.dmat);
1799 "could not create encoder buffer DMA tag\n");
1803 for (i = 0; i < CXM_SG_BUFFERS; i++) {
1806 * Allocate the encoder buffer.
1808 error = bus_dmamem_alloc(sc->enc_pool.dmat,
1809 (void **)&sc->enc_pool.bufs[i].vaddr,
1811 &sc->enc_pool.bufs[i].dmamap);
1814 "could not allocate encoder buffer\n");
1819 * Map the encoder buffer.
1821 error = bus_dmamap_load(sc->enc_pool.dmat,
1822 sc->enc_pool.bufs[i].dmamap,
1823 sc->enc_pool.bufs[i].vaddr,
1826 &sc->enc_pool.bufs[i].baddr, 0);
1828 device_printf(dev, "could not map encoder buffer\n");
1834 * Allocate a DMA tag for the scatter / gather list.
1836 error = bus_dma_tag_create(sc->parent_dmat, 1, 0,
1837 BUS_SPACE_MAXADDR_32BIT,
1838 BUS_SPACE_MAXADDR, NULL, NULL,
1840 * sizeof(struct cxm_sg_entry), 1,
1841 BUS_SPACE_MAXSIZE_32BIT, 0,
1845 "could not create scatter / gather DMA tag\n");
1850 * Allocate the scatter / gather list.
1852 error = bus_dmamem_alloc(sc->enc_sg.dmat, (void **)&sc->enc_sg.vaddr,
1853 BUS_DMA_NOWAIT, &sc->enc_sg.dmamap);
1856 "could not allocate scatter / gather list\n");
1861 * Map the scatter / gather list.
1863 error = bus_dmamap_load(sc->enc_sg.dmat, sc->enc_sg.dmamap,
1865 CXM_SG_BUFFERS * sizeof(struct cxm_sg_entry),
1866 cxm_mapmem, &sc->enc_sg.baddr, 0);
1868 device_printf(dev, "could not map scatter / gather list\n");
1873 * Initialize the hardware.
1875 if (cxm_init_hardware(sc) < 0) {
1876 device_printf(dev, "could not initialize hardware\n");
1881 sc->profile = &dvd_full_d1_ntsc_profile;
1883 sc->source = cxm_tuner_source;
1886 /* make the device entries */
1887 sc->cxm_dev_t = make_dev(&cxm_ops, unit,
1888 0, 0, 0444, "cxm%d", unit);
1893 if (sc->enc_sg.baddr)
1894 bus_dmamap_unload(sc->enc_sg.dmat, sc->enc_sg.dmamap);
1895 if (sc->enc_sg.vaddr)
1896 bus_dmamem_free(sc->enc_sg.dmat, sc->enc_sg.vaddr,
1898 if (sc->enc_sg.dmat)
1899 bus_dma_tag_destroy(sc->enc_sg.dmat);
1901 for (i = 0; i < CXM_SG_BUFFERS; i++) {
1902 if (sc->enc_pool.bufs[i].baddr)
1903 bus_dmamap_unload(sc->enc_pool.dmat,
1904 sc->enc_pool.bufs[i].dmamap);
1905 if (sc->enc_pool.bufs[i].vaddr)
1906 bus_dmamem_free(sc->enc_pool.dmat,
1907 sc->enc_pool.bufs[i].vaddr,
1908 sc->enc_pool.bufs[i].dmamap);
1911 if (sc->enc_pool.dmat)
1912 bus_dma_tag_destroy(sc->enc_pool.dmat);
1914 if (sc->parent_dmat)
1915 bus_dma_tag_destroy(sc->parent_dmat);
1918 * Detach the I2C bus.
1920 * This is done * after * deallocating the scatter / gather
1921 * list and buffers so the kernel has a better chance of
1922 * gracefully handling a memory shortage.
1924 * Detach the children before recursively deleting
1925 * in case a child has a pointer to a grandchild
1926 * which is used by the child's detach routine.
1928 bus_generic_detach(dev);
1930 device_delete_child(dev, sc->cxm_iic);
1933 bus_teardown_intr(dev, sc->irq_res, sc->ih_cookie);
1935 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq_res);
1937 bus_release_resource(dev, SYS_RES_MEMORY, CXM_RID, sc->mem_res);
1943 * the detach routine.
1946 cxm_detach(device_t dev)
1949 struct cxm_softc *sc;
1952 /* Get the device data */
1953 sc = device_get_softc(dev);
1955 /* Disable the Conexant device. */
1956 cxm_stop_hardware(sc);
1958 /* Unregister the /dev/cxmN device. */
1959 dev_ops_remove_minor(&cxm_ops, /*0, */device_get_unit(dev));
1962 * Deallocate scatter / gather list and buffers.
1964 bus_dmamap_unload(sc->enc_sg.dmat, sc->enc_sg.dmamap);
1965 bus_dmamem_free(sc->enc_sg.dmat, sc->enc_sg.vaddr, sc->enc_sg.dmamap);
1967 bus_dma_tag_destroy(sc->enc_sg.dmat);
1969 for (i = 0; i < CXM_SG_BUFFERS; i++) {
1970 bus_dmamap_unload(sc->enc_pool.dmat,
1971 sc->enc_pool.bufs[i].dmamap);
1972 bus_dmamem_free(sc->enc_pool.dmat, sc->enc_pool.bufs[i].vaddr,
1973 sc->enc_pool.bufs[i].dmamap);
1976 bus_dma_tag_destroy(sc->enc_pool.dmat);
1978 bus_dma_tag_destroy(sc->parent_dmat);
1981 * Detach the I2C bus.
1983 * This is done * after * deallocating the scatter / gather
1984 * list and buffers so the kernel has a better chance of
1985 * gracefully handling a memory shortage.
1987 * Detach the children before recursively deleting
1988 * in case a child has a pointer to a grandchild
1989 * which is used by the child's detach routine.
1991 * Remember the child before detaching so we can
1992 * delete it (bus_generic_detach indirectly zeroes
1995 child = sc->cxm_iic;
1996 bus_generic_detach(dev);
1998 device_delete_child(dev, child);
2000 /* Deallocate resources. */
2001 bus_teardown_intr(dev, sc->irq_res, sc->ih_cookie);
2002 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq_res);
2003 bus_release_resource(dev, SYS_RES_MEMORY, CXM_RID, sc->mem_res);
2009 * the shutdown routine.
2012 cxm_shutdown(device_t dev)
2014 struct cxm_softc *sc = device_get_softc(dev);
2016 /* Disable the Conexant device. */
2017 cxm_stop_hardware(sc);
2023 * the interrupt routine.
2029 struct cxm_softc *sc;
2031 /* Get the device data */
2032 sc = (struct cxm_softc *)arg;
2034 status = CSR_READ_4(sc, CXM_REG_IRQ_STATUS);
2036 status &= ~sc->irq_mask;
2041 /* Process DMA done before handling a new DMA request or EOS */
2042 if (status & CXM_IRQ_ENC_DMA_DONE)
2043 cxm_encoder_dma_done(sc);
2045 if (status & CXM_IRQ_ENC_DMA_REQUEST)
2046 cxm_encoder_dma_request(sc);
2048 if (status & CXM_IRQ_ENC_EOS) {
2049 sc->encoding_eos = 1;
2050 wakeup(&sc->encoding_eos);
2053 cxm_set_irq_status(sc, status);
2058 * the child detached routine.
2061 cxm_child_detached(device_t dev, device_t child)
2063 struct cxm_softc *sc;
2065 /* Get the device data */
2066 sc = device_get_softc(dev);
2068 if (child == sc->cxm_iic)
2074 cxm_read_ivar(device_t dev, device_t child, int index, uintptr_t* val)
2076 struct cxm_softc *sc;
2078 /* Get the device data */
2079 sc = device_get_softc(dev);
2082 case CXM_IVAR_BHANDLE:
2083 *(bus_space_handle_t **)val = &sc->bhandle;
2087 *(bus_space_tag_t **)val = &sc->btag;
2090 case CXM_IVAR_IICBUS:
2091 *(device_t **)val = &sc->iicbus;
2103 cxm_write_ivar(device_t dev, device_t child, int index, uintptr_t val)
2105 struct cxm_softc *sc;
2107 /* Get the device data */
2108 sc = device_get_softc(dev);
2111 case CXM_IVAR_BHANDLE:
2117 case CXM_IVAR_IICBUS:
2120 sc->iicbus = val ? *(device_t *)val : NULL;
2131 /*---------------------------------------------------------
2133 ** Conexant iTVC15 / iTVC16 character device driver routines
2135 **---------------------------------------------------------
2138 #define UNIT(x) ((x) & 0x0f)
2139 #define FUNCTION(x) (x >> 4)
2145 cxm_open(struct dev_open_args *ap)
2147 cdev_t dev = ap->a_head.a_dev;
2149 struct cxm_softc *sc;
2151 unit = UNIT(minor(dev));
2153 /* Get the device data */
2154 sc = (struct cxm_softc*)devclass_get_softc(cxm_devclass, unit);
2156 /* the device is no longer valid/functioning */
2166 /* Record that the device is now busy */
2167 device_busy(devclass_get_device(cxm_devclass, unit));
2177 cxm_close(struct dev_close_args *ap)
2179 cdev_t dev = ap->a_head.a_dev;
2181 struct cxm_softc *sc;
2183 unit = UNIT(minor(dev));
2185 /* Get the device data */
2186 sc = (struct cxm_softc*)devclass_get_softc(cxm_devclass, unit);
2188 /* the device is no longer valid/functioning */
2192 if (cxm_stop_encoder(sc) < 0)
2195 sc->enc_pool.offset = 0;
2196 sc->enc_pool.read = 0;
2197 sc->enc_pool.write = 0;
2199 sc->enc_proc = NULL;
2202 device_unbusy(devclass_get_device(cxm_devclass, unit));
2214 cxm_read(struct dev_read_args *ap)
2216 cdev_t dev = ap->a_head.a_dev;
2217 int buffers_available;
2221 unsigned int current;
2225 struct cxm_softc *sc;
2227 unit = UNIT(minor(dev));
2229 /* Get the device data */
2230 sc = (struct cxm_softc*)devclass_get_softc(cxm_devclass, unit);
2232 /* the device is no longer valid/functioning */
2236 /* Only trigger the encoder if the ring buffer is empty */
2237 if (!sc->encoding && sc->enc_pool.read == sc->enc_pool.write) {
2238 if (cxm_start_encoder(sc) < 0)
2240 if (ap->a_ioflag & IO_NDELAY)
2244 buffers_available = 0;
2247 while (sc->enc_pool.read == sc->enc_pool.write) {
2248 error = tsleep(&sc->enc_pool.read, PCATCH, "cxmrd", 0);
2256 * Determine the number of buffers available at this * instant *
2257 * taking in consideration that the ring buffer wraps.
2259 buffers_available = sc->enc_pool.write - sc->enc_pool.read;
2260 if (buffers_available < 0)
2261 buffers_available += CXM_SG_BUFFERS;
2264 offset = sc->enc_pool.offset;
2266 for (buffers_read = 0, i = sc->enc_pool.read;
2267 buffers_read != buffers_available && ap->a_uio->uio_resid;
2268 buffers_read++, i = (i + 1) % CXM_SG_BUFFERS) {
2270 current = cxm_encoder_fixup_byte_order (sc, i, offset);
2272 nbytes = sc->enc_pool.bufs[current].size - offset;
2274 /* Don't transfer more than requested */
2275 if (nbytes > ap->a_uio->uio_resid)
2276 nbytes = ap->a_uio->uio_resid;
2278 error = uiomove(sc->enc_pool.bufs[current].vaddr + offset,
2285 /* Handle a partial read of a buffer */
2286 if (!ap->a_uio->uio_resid && offset != sc->enc_pool.bufs[i].size)
2292 sc->enc_pool.offset = offset;
2294 /* Update the books */
2296 sc->enc_pool.read = (sc->enc_pool.read + buffers_read)
2308 cxm_ioctl(struct dev_ioctl_args *ap)
2310 cdev_t dev = ap->a_head.a_dev;
2312 int chroma_saturation;
2322 struct cxm_softc *sc;
2323 enum cxm_source source;
2324 struct bktr_capture_area *cap;
2325 struct bktr_remote *remote;
2327 unit = UNIT(minor(dev));
2329 /* Get the device data */
2330 sc = (struct cxm_softc*)devclass_get_softc(cxm_devclass, unit);
2332 /* the device is no longer valid/functioning */
2336 switch (ap->a_cmd) {
2338 switch (cxm_msp_selected_source(sc)) {
2339 case cxm_tuner_source:
2340 *(int *) ap->a_data = AUDIO_TUNER;
2343 case cxm_line_in_source_composite:
2344 case cxm_line_in_source_svideo:
2345 *(int *) ap->a_data = AUDIO_EXTERN;
2349 *(int *) ap->a_data = AUDIO_INTERN;
2356 if (cxm_msp_is_muted(sc) == 1)
2357 *(int *) ap->a_data |= AUDIO_MUTE;
2361 source = cxm_unknown_source;
2363 switch (*(int *) ap->a_data) {
2365 source = cxm_tuner_source;
2369 source = cxm_line_in_source_composite;
2373 source = cxm_fm_source;
2377 if (cxm_msp_mute(sc) < 0)
2382 if (cxm_msp_unmute(sc) < 0)
2393 * Switching between audio + video and audio only
2394 * subtypes isn't supported while encoding.
2397 if (source != sc->source
2398 && (source == cxm_fm_source
2399 || sc->source == cxm_fm_source))
2403 if (cxm_pause_encoder(sc) < 0)
2406 if (cxm_msp_select_source(sc, source) < 0)
2409 if (source == cxm_fm_source)
2410 sc->source = source;
2412 result = cxm_encoder_wait_for_lock(sc);
2415 else if (result == 0)
2418 if (cxm_unpause_encoder(sc) < 0)
2423 brightness = cxm_saa7115_get_brightness(sc);
2429 * Brooktree brightness:
2430 * 0x80 = -50.0%, 0x00 = +0.0%, 0x7f = +49.6%
2432 *(int *)ap->a_data = (int)(unsigned char)brightness - 128;
2438 * Brooktree brightness:
2439 * 0x80 = -50.0%, 0x00 = +0.0%, 0x7f = +49.6%
2441 brightness = *(int *)ap->a_data + 128;
2443 if (cxm_saa7115_set_brightness(sc, brightness) < 0)
2448 brightness = cxm_saa7115_get_brightness(sc);
2453 *(unsigned char *)ap->a_data = (unsigned char)brightness;
2457 brightness = *(unsigned char *)ap->a_data;
2459 if (cxm_saa7115_set_brightness(sc, brightness) < 0)
2464 chroma_saturation = cxm_saa7115_get_chroma_saturation(sc);
2466 if (chroma_saturation < 0)
2470 * Brooktree chroma saturation:
2471 * 0x000 = 0%, 0x0fe = 100%, 0x1ff = 201.18%
2473 *(int *)ap->a_data = ((signed char)chroma_saturation > 0)
2474 ? (chroma_saturation * 4 - 2) : 0;
2480 * Brooktree chroma saturation:
2481 * 0x000 = 0%, 0x0fe = 100%, 0x1ff = 201.18%
2483 chroma_saturation = (*(int *)ap->a_data & 0x1ff) < 510
2484 ? ((*(int *)ap->a_data & 0x1ff) + 2) / 4 : 127;
2486 if (cxm_saa7115_set_chroma_saturation(sc, chroma_saturation)
2493 chroma_saturation = cxm_saa7115_get_chroma_saturation(sc);
2495 if (chroma_saturation < 0)
2498 *(unsigned char *)ap->a_data = (unsigned char)chroma_saturation;
2502 chroma_saturation = *(unsigned char *)ap->a_data;
2504 if (cxm_saa7115_set_chroma_saturation(sc, chroma_saturation)
2510 contrast = cxm_saa7115_get_contrast(sc);
2515 *(unsigned char *)ap->a_data = (unsigned char)contrast;
2519 contrast = *(unsigned char *)ap->a_data;
2521 if (cxm_saa7115_set_contrast(sc, contrast) < 0)
2526 hue = cxm_saa7115_get_hue(sc);
2531 *(int *)ap->a_data = (signed char)hue;
2535 hue = *(int *)ap->a_data;
2537 if (cxm_saa7115_set_hue(sc, hue) < 0)
2542 hue = cxm_saa7115_get_hue(sc);
2547 *(signed char *)ap->a_data = (signed char)hue;
2551 hue = *(signed char *)ap->a_data;
2553 if (cxm_saa7115_set_hue(sc, hue) < 0)
2558 switch (*(int *) ap->a_data) {
2559 case METEOR_CAP_CONTINOUS:
2560 if (cxm_start_encoder(sc) < 0)
2564 case METEOR_CAP_STOP_CONT:
2565 if (cxm_stop_encoder(sc) < 0)
2574 case BT848_GCAPAREA:
2575 cap = (struct bktr_capture_area *)ap->a_data;
2576 memset (cap, 0, sizeof (*cap));
2579 cap->x_size = sc->profile->width;
2580 cap->y_size = sc->profile->height;
2583 case BT848_SCAPAREA:
2587 cap = (struct bktr_capture_area *)ap->a_data;
2588 if (cap->x_offset || cap->y_offset
2589 || (cap->x_size % CXM_MACROBLOCK_WIDTH)
2590 || (cap->y_size % CXM_MACROBLOCK_HEIGHT))
2594 * Setting the width and height has the side effect of
2595 * chosing between the VCD, SVCD, and DVD profiles.
2598 for (i = 0; i < NUM_ELEMENTS(codec_profiles); i++)
2599 if (codec_profiles[i]->width == cap->x_size
2600 && codec_profiles[i]->height == cap->y_size)
2603 if (i >= NUM_ELEMENTS(codec_profiles))
2606 sc->profile = codec_profiles[i];
2610 switch (cxm_saa7115_detected_format(sc)) {
2611 case cxm_ntsc_60hz_source_format:
2612 *(unsigned long *)ap->a_data = BT848_IFORM_F_NTSCM;
2615 case cxm_pal_50hz_source_format:
2616 *(unsigned long *)ap->a_data = BT848_IFORM_F_PALBDGHI;
2619 case cxm_secam_50hz_source_format:
2620 *(unsigned long *)ap->a_data = BT848_IFORM_F_SECAM;
2623 case cxm_pal_60hz_source_format:
2624 *(unsigned long *)ap->a_data = BT848_IFORM_F_PALM;
2627 case cxm_bw_50hz_source_format:
2628 case cxm_bw_60hz_source_format:
2629 case cxm_ntsc_50hz_source_format:
2630 *(unsigned long *)ap->a_data = BT848_IFORM_F_AUTO;
2639 switch (cxm_saa7115_detected_format(sc)) {
2640 case cxm_ntsc_60hz_source_format:
2641 *(unsigned long *)ap->a_data = METEOR_FMT_NTSC;
2644 case cxm_pal_50hz_source_format:
2645 *(unsigned long *)ap->a_data = METEOR_FMT_PAL;
2648 case cxm_secam_50hz_source_format:
2649 *(unsigned long *)ap->a_data = METEOR_FMT_SECAM;
2652 case cxm_bw_50hz_source_format:
2653 case cxm_bw_60hz_source_format:
2654 case cxm_ntsc_50hz_source_format:
2655 case cxm_pal_60hz_source_format:
2656 *(unsigned long *)ap->a_data = METEOR_FMT_AUTOMODE;
2665 fps = cxm_saa7115_detected_fps(sc);
2670 *(unsigned short *)ap->a_data = fps;
2674 switch (sc->source) {
2675 case cxm_tuner_source:
2676 *(unsigned long *)ap->a_data = METEOR_INPUT_DEV1;
2679 case cxm_line_in_source_composite:
2680 *(unsigned long *)ap->a_data = METEOR_INPUT_DEV2;
2683 case cxm_line_in_source_svideo:
2684 *(unsigned long *)ap->a_data = METEOR_INPUT_DEV_SVIDEO;
2693 source = cxm_unknown_source;
2695 switch (*(unsigned long *)ap->a_data & 0xf000) {
2696 case METEOR_INPUT_DEV1:
2697 source = cxm_tuner_source;
2700 case METEOR_INPUT_DEV2:
2701 source = cxm_line_in_source_composite;
2704 case METEOR_INPUT_DEV_SVIDEO:
2705 source = cxm_line_in_source_svideo;
2715 * Switching between audio + video and audio only
2716 * subtypes isn't supported while encoding.
2719 if (source != sc->source
2720 && (source == cxm_fm_source
2721 || sc->source == cxm_fm_source))
2725 if (cxm_pause_encoder(sc) < 0)
2728 if (cxm_saa7115_select_source(sc, source) < 0)
2730 if (cxm_msp_select_source(sc, source) < 0)
2732 sc->source = source;
2734 result = cxm_encoder_wait_for_lock(sc);
2737 else if (result == 0)
2740 if (cxm_unpause_encoder(sc) < 0)
2745 *(unsigned int *)ap->a_data = sc->enc_signal;
2749 sig = *(unsigned int *)ap->a_data;
2751 if (!_SIG_VALID(sig))
2755 * Historically, applications used METEOR_SIG_MODE_MASK
2756 * to reset signal delivery.
2758 if (sig == METEOR_SIG_MODE_MASK)
2762 sc->enc_proc = sig ? curproc : NULL;
2763 sc->enc_signal = sig;
2768 /* Convert from kHz to MHz * 100 */
2769 freq = sc->tuner_freq / 10;
2771 *(unsigned int *)ap->a_data = freq;
2775 if (sc->source == cxm_fm_source)
2776 if (cxm_pause_encoder(sc) < 0)
2779 /* Convert from MHz * 100 to kHz */
2780 freq = *(unsigned int *)ap->a_data * 10;
2782 if (cxm_tuner_select_frequency(sc, cxm_tuner_fm_freq_type,
2787 * Explicitly wait for the tuner lock so we
2788 * can indicate if there's a station present.
2790 if (cxm_tuner_wait_for_lock(sc) < 0)
2793 result = cxm_encoder_wait_for_lock(sc);
2796 else if (result == 0)
2799 if (sc->source == cxm_fm_source)
2800 if (cxm_unpause_encoder(sc) < 0)
2804 case TVTUNER_GETAFC:
2805 *(int *)ap->a_data = sc->tuner_afc;
2808 case TVTUNER_SETAFC:
2809 sc->tuner_afc = (*(int *)ap->a_data != 0);
2812 case TVTUNER_GETTYPE:
2813 *(unsigned int *)ap->a_data = cxm_tuner_selected_channel_set(sc);
2816 case TVTUNER_SETTYPE:
2817 if (cxm_tuner_select_channel_set(sc, *(unsigned int *)ap->a_data) < 0)
2821 case TVTUNER_SETCHNL:
2822 if (sc->source == cxm_tuner_source)
2823 if (cxm_pause_encoder(sc) < 0)
2826 if (cxm_tuner_select_channel(sc, *(unsigned int *)ap->a_data) < 0)
2830 if (cxm_tuner_apply_afc(sc) < 0)
2834 * Explicitly wait for the tuner lock so we
2835 * can indicate if there's a station present.
2837 if (cxm_tuner_wait_for_lock(sc) < 0)
2840 result = cxm_encoder_wait_for_lock(sc);
2843 else if (result == 0)
2846 if (sc->source == cxm_tuner_source)
2847 if (cxm_unpause_encoder(sc) < 0)
2851 case TVTUNER_GETFREQ:
2852 /* Convert from kHz to MHz * 16 */
2853 freq = (sc->tuner_freq * 16) / 1000;
2855 *(unsigned int *)ap->a_data = freq;
2858 case TVTUNER_SETFREQ:
2859 if (sc->source == cxm_tuner_source)
2860 if (cxm_pause_encoder(sc) < 0)
2863 /* Convert from MHz * 16 to kHz */
2864 freq = (*(unsigned int *)ap->a_data * 1000) / 16;
2866 if (cxm_tuner_select_frequency(sc, cxm_tuner_tv_freq_type,
2871 * Explicitly wait for the tuner lock so we
2872 * can indicate if there's a station present.
2874 if (cxm_tuner_wait_for_lock(sc) < 0)
2877 result = cxm_encoder_wait_for_lock(sc);
2880 else if (result == 0)
2883 if (sc->source == cxm_tuner_source)
2884 if (cxm_unpause_encoder(sc) < 0)
2889 case TVTUNER_GETSTATUS:
2890 status = cxm_tuner_status(sc);
2893 *(unsigned long *)ap->a_data = status & 0xff;
2897 remote = (struct bktr_remote *)ap->a_data;
2898 if (cxm_ir_key(sc, (char *)remote, sizeof(*remote)) < 0)
2909 static struct filterops cxm_filterops =
2910 { 1, NULL, cxm_filter_detach, cxm_filter };
2913 cxm_kqfilter(struct dev_kqfilter_args *ap)
2915 cdev_t dev = ap->a_head.a_dev;
2916 struct knote *kn = ap->a_kn;
2917 struct cxm_softc *sc;
2918 struct klist *klist;
2923 switch (kn->kn_filter) {
2925 unit = UNIT(minor(dev));
2926 /* Get the device data */
2927 sc = (struct cxm_softc *)devclass_get_softc(cxm_devclass, unit);
2928 kn->kn_fop = &cxm_filterops;
2929 kn->kn_hook = (caddr_t)sc;
2932 ap->a_result = EOPNOTSUPP;
2936 klist = &sc->enc_kq.ki_note;
2937 knote_insert(klist, kn);
2943 cxm_filter_detach(struct knote *kn)
2945 struct cxm_softc *sc = (struct cxm_softc *)kn->kn_hook;
2946 struct klist *klist = &sc->enc_kq.ki_note;
2948 knote_remove(klist, kn);
2952 cxm_filter(struct knote *kn, long hint)
2954 struct cxm_softc *sc = (struct cxm_softc *)kn->kn_hook;
2958 /* the device is no longer valid/functioning */
2959 kn->kn_flags |= EV_EOF;
2964 if (sc->enc_pool.read != sc->enc_pool.write)