1 /* $NetBSD: dtv_device.c,v 1.15 2025/11/15 19:12:13 jmcneill Exp $ */ 2 3 /*- 4 * Copyright (c) 2011 Jared D. McNeill <jmcneill (at) invisible.ca> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of The NetBSD Foundation nor the names of its 16 * contributors may be used to endorse or promote products derived 17 * from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: dtv_device.c,v 1.15 2025/11/15 19:12:13 jmcneill Exp $"); 34 35 #include <sys/param.h> 36 #include <sys/types.h> 37 #include <sys/atomic.h> 38 #include <sys/conf.h> 39 #include <sys/device.h> 40 #include <sys/module.h> 41 #include <sys/poll.h> 42 #include <sys/select.h> 43 44 #include <dev/dtv/dtvvar.h> 45 46 #include "ioconf.h" 47 48 MODULE(MODULE_CLASS_DRIVER, dtv, NULL); 49 50 static dev_type_open(dtvopen); 51 static dev_type_close(dtvclose); 52 static dev_type_read(dtvread); 53 static dev_type_ioctl(dtvioctl); 54 static dev_type_poll(dtvpoll); 55 56 const struct cdevsw dtv_cdevsw = { 57 .d_open = dtvopen, 58 .d_close = dtvclose, 59 .d_read = dtvread, 60 .d_write = nowrite, 61 .d_ioctl = dtvioctl, 62 .d_stop = nostop, 63 .d_tty = notty, 64 .d_poll = dtvpoll, 65 .d_mmap = nommap, 66 .d_kqfilter = nokqfilter, 67 .d_discard = nodiscard, 68 .d_flag = D_OTHER | D_MPSAFE, 69 }; 70 71 static int dtv_match(device_t, cfdata_t, void *); 72 static void dtv_attach(device_t, device_t, void *); 73 static int dtv_detach(device_t, int); 74 75 CFATTACH_DECL_NEW(dtv, 76 sizeof(struct dtv_softc), 77 dtv_match, 78 dtv_attach, 79 dtv_detach, 80 NULL 81 ); 82 83 static int 84 dtv_match(device_t parent, cfdata_t cfdata, void *aa) 85 { 86 return 1; 87 } 88 89 static void 90 dtv_attach(device_t parent, device_t self, void *aa) 91 { 92 struct dtv_attach_args *daa = aa; 93 struct dtv_softc *sc = device_private(self); 94 struct dtv_stream *ds = &sc->sc_stream; 95 struct dvb_frontend_info info; 96 97 sc->sc_dev = self; 98 sc->sc_hw = daa->hw; 99 sc->sc_priv = daa->priv; 100 sc->sc_open = 0; 101 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE); 102 103 ds->ds_nbufs = 0; 104 ds->ds_buf = NULL; 105 SIMPLEQ_INIT(&ds->ds_ingress); 106 SIMPLEQ_INIT(&ds->ds_egress); 107 mutex_init(&ds->ds_egress_lock, MUTEX_DEFAULT, IPL_SCHED); 108 mutex_init(&ds->ds_ingress_lock, MUTEX_DEFAULT, IPL_SCHED); 109 cv_init(&ds->ds_sample_cv, "dtv"); 110 selinit(&ds->ds_sel); 111 dtv_scatter_buf_init(&ds->ds_data); 112 if (dtv_buffer_realloc(sc, DTV_DEFAULT_BUFSIZE) != 0) { 113 aprint_error(": no memory\n"); 114 sc->sc_dying = true; 115 return; 116 } 117 118 mutex_init(&sc->sc_demux_lock, MUTEX_DEFAULT, IPL_SCHED); 119 TAILQ_INIT(&sc->sc_demux_list); 120 sc->sc_demux_runcnt = 0; 121 122 dtv_device_get_devinfo(sc, &info); 123 124 aprint_naive("\n"); 125 aprint_normal(": %s", info.name); 126 switch (info.type) { 127 case FE_QPSK: 128 aprint_normal(" [QPSK]"); 129 break; 130 case FE_QAM: 131 aprint_normal(" [QAM]"); 132 break; 133 case FE_OFDM: 134 aprint_normal(" [OFDM]"); 135 break; 136 case FE_ATSC: 137 aprint_normal(" [ATSC]"); 138 break; 139 } 140 aprint_normal("\n"); 141 } 142 143 static int 144 dtv_detach(device_t self, int flags) 145 { 146 struct dtv_softc *sc = device_private(self); 147 struct dtv_stream *ds = &sc->sc_stream; 148 149 cv_destroy(&ds->ds_sample_cv); 150 mutex_destroy(&ds->ds_ingress_lock); 151 mutex_destroy(&ds->ds_egress_lock); 152 seldestroy(&ds->ds_sel); 153 dtv_buffer_realloc(sc, 0); 154 dtv_scatter_buf_destroy(&ds->ds_data); 155 156 mutex_destroy(&sc->sc_demux_lock); 157 mutex_destroy(&sc->sc_lock); 158 159 return 0; 160 } 161 162 #ifdef _MODULE 163 #include "ioconf.c" 164 #endif 165 166 static int 167 dtv_modcmd(modcmd_t cmd, void *arg) 168 { 169 #ifdef _MODULE 170 int error, bmaj = -1, cmaj = -1; 171 #endif 172 173 switch (cmd) { 174 case MODULE_CMD_INIT: 175 #ifdef _MODULE 176 error = devsw_attach("dtv", NULL, &bmaj, &dtv_cdevsw, &cmaj); 177 if (error) 178 return error; 179 180 error = config_init_component(cfdriver_ioconf_dtv, 181 cfattach_ioconf_dtv, cfdata_ioconf_dtv); 182 if (error) 183 devsw_detach(NULL, &dtv_cdevsw); 184 return error; 185 #else 186 return 0; 187 #endif 188 case MODULE_CMD_FINI: 189 #ifdef _MODULE 190 error = config_fini_component(cfdriver_ioconf_dtv, 191 cfattach_ioconf_dtv, cfdata_ioconf_dtv); 192 if (error == 0) 193 devsw_detach(NULL, &dtv_cdevsw); 194 195 return error; 196 #else 197 return 0; 198 #endif 199 default: 200 return ENOTTY; 201 } 202 } 203 204 static int 205 dtvopen(dev_t dev, int flags, int mode, lwp_t *l) 206 { 207 struct dtv_softc *sc; 208 struct dtv_ts *ts; 209 int error; 210 211 if ((sc = device_lookup_private(&dtv_cd, DTVUNIT(dev))) == NULL) 212 return ENXIO; 213 if (sc->sc_dying == true) 214 return ENODEV; 215 ts = &sc->sc_ts; 216 217 mutex_enter(&sc->sc_lock); 218 if (sc->sc_open == 0) { 219 error = dtv_device_open(sc, flags); 220 if (error) 221 return error; 222 sc->sc_bufsize = DTV_DEFAULT_BUFSIZE; 223 sc->sc_bufsize_chg = true; 224 memset(ts->ts_pidfilter, 0, sizeof(ts->ts_pidfilter)); 225 } 226 ++sc->sc_open; 227 mutex_exit(&sc->sc_lock); 228 229 if (ISDTVDEMUX(dev)) 230 return dtv_demux_open(sc, flags, mode, l); 231 232 return 0; 233 } 234 235 static int 236 dtvclose(dev_t dev, int flags, int mode, lwp_t *l) 237 { 238 struct dtv_softc *sc; 239 240 if ((sc = device_lookup_private(&dtv_cd, DTVUNIT(dev))) == NULL) 241 return ENXIO; 242 243 dtv_common_close(sc); 244 245 return 0; 246 } 247 248 static int 249 dtvread(dev_t dev, struct uio *uio, int flags) 250 { 251 struct dtv_softc *sc; 252 253 if ((sc = device_lookup_private(&dtv_cd, DTVUNIT(dev))) == NULL) 254 return ENXIO; 255 256 if (ISDTVDVR(dev)) 257 return dtv_buffer_read(sc, uio, flags); 258 259 return ENXIO; 260 } 261 262 static int 263 dtvioctl(dev_t dev, u_long cmd, void *ptr, int flags, lwp_t *l) 264 { 265 struct dtv_softc *sc; 266 267 if ((sc = device_lookup_private(&dtv_cd, DTVUNIT(dev))) == NULL) 268 return ENXIO; 269 270 if (ISDTVFRONTEND(dev)) { 271 return dtv_frontend_ioctl(sc, cmd, ptr, flags); 272 } 273 274 return EINVAL; 275 } 276 277 static int 278 dtvpoll(dev_t dev, int events, lwp_t *l) 279 { 280 struct dtv_softc *sc; 281 282 if ((sc = device_lookup_private(&dtv_cd, DTVUNIT(dev))) == NULL) 283 return POLLERR; 284 285 if (ISDTVFRONTEND(dev)) { 286 return POLLPRI|POLLIN; /* XXX event */ 287 } else if (ISDTVDVR(dev)) { 288 return dtv_buffer_poll(sc, events, l); 289 } 290 291 return POLLERR; 292 } 293 294 void 295 dtv_common_close(struct dtv_softc *sc) 296 { 297 mutex_enter(&sc->sc_lock); 298 KASSERT(sc->sc_open > 0); 299 --sc->sc_open; 300 if (sc->sc_open == 0) { 301 dtv_device_close(sc); 302 dtv_buffer_destroy(sc); 303 } 304 mutex_exit(&sc->sc_lock); 305 } 306