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