1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
/* $NetBSD: emdtv_ir.c,v 1.6 2022/06/26 22:49:09 riastradh Exp $ */
/*-
* Copyright (c) 2008 Jared D. McNeill <jmcneill@invisible.ca>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: emdtv_ir.c,v 1.6 2022/06/26 22:49:09 riastradh Exp $");
#include <sys/select.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/conf.h>
#include <sys/bus.h>
#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdi_util.h>
#include <dev/usb/usbdivar.h>
#include <dev/usb/usbdevs.h>
#include <dev/usb/emdtvvar.h>
#include <dev/usb/emdtvreg.h>
#include <dev/ir/ir.h>
#include <dev/ir/cirio.h>
#include <dev/ir/cirvar.h>
static void emdtv_ir_intr(struct usbd_xfer *, void *,
usbd_status);
static void emdtv_ir_worker(struct work *, void *);
static int emdtv_ir_open(void *, int, int, struct proc *);
static int emdtv_ir_close(void *, int, int, struct proc *);
static int emdtv_ir_read(void *, struct uio *, int);
static int emdtv_ir_write(void *, struct uio *, int);
static int emdtv_ir_setparams(void *, struct cir_params *);
static const struct cir_methods emdtv_ir_methods = {
.im_open = emdtv_ir_open,
.im_close = emdtv_ir_close,
.im_read = emdtv_ir_read,
.im_write = emdtv_ir_write,
.im_setparams = emdtv_ir_setparams,
};
void
emdtv_ir_attach(struct emdtv_softc *sc)
{
struct ir_attach_args ia;
usb_endpoint_descriptor_t *ed;
usbd_status status;
int err;
mutex_init(&sc->sc_ir_mutex, MUTEX_DEFAULT, IPL_VM);
ed = usbd_interface2endpoint_descriptor(sc->sc_iface, 0);
if (ed == NULL)
return;
err = workqueue_create(&sc->sc_ir_wq, "emdtvir",
emdtv_ir_worker, sc, PRI_NONE, IPL_VM, 0);
if (err) {
aprint_error_dev(sc->sc_dev, "couldn't create workqueue: %d\n",
err);
return;
}
status = usbd_open_pipe_intr(sc->sc_iface, ed->bEndpointAddress,
USBD_EXCLUSIVE_USE, &sc->sc_intr_pipe, sc, &sc->sc_intr_buf, 1,
emdtv_ir_intr, USBD_DEFAULT_INTERVAL);
if (status != USBD_NORMAL_COMPLETION) {
aprint_error_dev(sc->sc_dev, "couldn't open intr pipe: %s\n",
usbd_errstr(status));
return;
}
ia.ia_type = IR_TYPE_CIR;
ia.ia_methods = &emdtv_ir_methods;
ia.ia_handle = sc;
sc->sc_cirdev =
config_found(sc->sc_dev, &ia, ir_print,
CFARGS(.iattr = "irbus"));
}
void
emdtv_ir_detach(struct emdtv_softc *sc, int flags)
{
if (sc->sc_intr_pipe != NULL) {
usbd_abort_pipe(sc->sc_intr_pipe);
usbd_close_pipe(sc->sc_intr_pipe);
sc->sc_intr_pipe = NULL;
}
if (sc->sc_ir_wq != NULL)
workqueue_destroy(sc->sc_ir_wq);
mutex_destroy(&sc->sc_ir_mutex);
}
static void
emdtv_ir_intr(struct usbd_xfer *xfer, void * priv,
usbd_status status)
{
struct emdtv_softc *sc = priv;
uint32_t len;
usbd_get_xfer_status(xfer, NULL, NULL, &len, NULL);
if (status == USBD_CANCELLED)
return;
if (sc->sc_ir_wq)
workqueue_enqueue(sc->sc_ir_wq, &sc->sc_ir_work, NULL);
}
static void
emdtv_ir_worker(struct work *wk, void *opaque)
{
struct emdtv_softc *sc = opaque;
struct cir_softc *csc;
uint8_t evt[3];
int pos;
if (sc->sc_cirdev == NULL || sc->sc_dying == true ||
sc->sc_ir_open == false)
return;
emdtv_read_multi_1(sc, UR_GET_STATUS, EM28XX_REG_IR, evt, sizeof(evt));
csc = device_private(sc->sc_cirdev);
mutex_enter(&sc->sc_ir_mutex);
pos = (sc->sc_ir_ptr + sc->sc_ir_cnt) % EMDTV_CIR_BUFLEN;
memcpy(&sc->sc_ir_queue[pos], evt, sizeof(evt));
if (sc->sc_ir_cnt < EMDTV_CIR_BUFLEN - 1) {
++sc->sc_ir_cnt;
++csc->sc_rdframes;
}
selnotify(&csc->sc_rdsel, 0, 1);
mutex_exit(&sc->sc_ir_mutex);
}
/*
* cir(4)
*/
static int
emdtv_ir_open(void *opaque, int flag, int mode, struct proc *p)
{
struct emdtv_softc *sc = opaque;
if (sc->sc_ir_open == true)
return EBUSY;
sc->sc_ir_cnt = 0;
sc->sc_ir_ptr = EMDTV_CIR_BUFLEN - 1;
sc->sc_ir_open = true;
return 0;
}
static int
emdtv_ir_close(void *opaque, int flag, int mode, struct proc *p)
{
struct emdtv_softc *sc = opaque;
sc->sc_ir_open = false;
return 0;
}
static int
emdtv_ir_read(void *opaque, struct uio *uio, int flag)
{
struct emdtv_softc *sc = opaque;
struct cir_softc *csc;
int error = 0;
if (uio->uio_resid != 3) /* 3 byte protocol */
return EINVAL;
if (sc->sc_dying)
return EIO;
csc = device_private(sc->sc_cirdev);
mutex_enter(&sc->sc_ir_mutex);
if (sc->sc_ir_cnt == 0)
goto out;
error = uiomove(&sc->sc_ir_queue[sc->sc_ir_ptr], 3, uio);
sc->sc_ir_ptr++;
if (sc->sc_ir_ptr == EMDTV_CIR_BUFLEN)
sc->sc_ir_ptr = 0;
--sc->sc_ir_cnt;
--csc->sc_rdframes;
KASSERT(sc->sc_ir_cnt >= 0);
out:
mutex_exit(&sc->sc_ir_mutex);
return error;
}
static int
emdtv_ir_write(void *opaque, struct uio *uio, int flag)
{
return EINVAL;
}
static int
emdtv_ir_setparams(void *opaque, struct cir_params *cp)
{
return 0;
}
|