-
Notifications
You must be signed in to change notification settings - Fork 1
/
sms1xxx-ir.c
346 lines (297 loc) · 8.51 KB
/
sms1xxx-ir.c
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/* SMS1XXX - Siano DVB-T USB driver for FreeBSD 8.0 and higher:
*
* Copyright (C) 2008-2014, Ganaël Laplanche, http://contribs.martymac.org
*
* This driver contains code taken from the FreeBSD dvbusb driver:
*
* Copyright (C) 2006-2007, Raaf
* Copyright (C) 2004-2006, Patrick Boettcher
*
* This driver contains code taken from the Linux siano driver:
*
* Siano Mobile Silicon, Inc.
* MDTV receiver kernel modules.
* Copyright (C) 2006-2009, Uri Shkolnik
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
*
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* * * *
* IR management
* * * *
*/
/* uid(9) stuff */
#include <sys/types.h>
#include <sys/uio.h>
/* mutex(9) stuff */
#include <sys/param.h>
#include <sys/lock.h>
#include <sys/mutex.h>
/* Read flags (O_NONBLOCK) */
#include <sys/fcntl.h>
#include "sms1xxx.h"
#include "sms1xxx-ir.h"
#include "sms1xxx-usb.h"
/*************************
*** Private functions ***
*************************/
static d_open_t sms1xxx_ir_open;
static d_close_t sms1xxx_ir_close;
static d_read_t sms1xxx_ir_read;
static struct cdevsw sms1xxx_ir_cdevsw = {
.d_version = D_VERSION,
.d_flags = D_NEEDGIANT,
.d_open = sms1xxx_ir_open,
.d_close = sms1xxx_ir_close,
.d_read = sms1xxx_ir_read,
.d_name = "sms1xxx_ir",
};
static int
sms1xxx_ir_open(struct cdev *dev, int flag, int mode, struct thread *p)
{
struct sms1xxx_softc *sc = dev->si_drv1;
#ifdef SMS1XXX_DEBUG
int unit = device_get_unit(sc->sc_dev);
TRACE(TRACE_OPEN,"flag=%d, mode=%d, unit=%d\n", flag, mode, unit);
#endif
if(sc == NULL || sc->sc_dying) {
TRACE(TRACE_MODULE,"dying! sc=%p\n",sc);
return (ENXIO);
}
if(sc->ir.state & IR_OPEN)
return (EBUSY);
/* Reset buffer states */
sc->ir.woff =
sc->ir.roff = 0;
mtx_lock(&sc->ir.lock);
sc->ir.wavail = SMS1XXX_IR_BUFSIZE;
sc->ir.ravail = 0;
mtx_unlock(&sc->ir.lock);
sc->ir.state |= IR_OPEN;
return (0);
}
static int
sms1xxx_ir_close(struct cdev *dev, int flag, int mode, struct thread *p)
{
struct sms1xxx_softc *sc = dev->si_drv1;
#ifdef SMS1XXX_DEBUG
int unit = device_get_unit(sc->sc_dev);
TRACE(TRACE_OPEN,"flag=%d, mode=%d, filter=%d\n",
flag, mode, unit);
#endif
if(sc == NULL || sc->sc_dying) {
TRACE(TRACE_MODULE,"dying! sc=%p\n",sc);
return (ENXIO);
}
sc->ir.state &= ~IR_OPEN;
return (0);
}
static inline int
sms1xxx_ir_get_data(struct sms1xxx_softc *sc, struct uio *uio)
{
TRACE(TRACE_IR,"wants %zu bytes, ravail=%u, roff=%u\n",
uio->uio_resid, sc->ir.ravail, sc->ir.roff);
int err = 0;
u32 len = MIN(uio->uio_resid, sc->ir.ravail);
u32 rlen = len;
u32 saved_roff = sc->ir.roff;
if(sc->ir.roff + rlen > SMS1XXX_IR_BUFSIZE) {
err = uiomove(sc->ir.buf + sc->ir.roff,
SMS1XXX_IR_BUFSIZE - sc->ir.roff, uio);
if(err)
return (-err);
rlen -= SMS1XXX_IR_BUFSIZE - sc->ir.roff;
sc->ir.roff = 0;
}
err = uiomove(sc->ir.buf + sc->ir.roff, rlen, uio);
if(err) {
/* Restore original read offset (to leave
offsets coherent) and return */
sc->ir.roff = saved_roff;
return (-err);
}
sc->ir.roff += rlen;
mtx_lock(&sc->ir.lock);
sc->ir.wavail += len;
sc->ir.ravail -= len;
mtx_unlock(&sc->ir.lock);
return (uio->uio_resid);
}
static int
sms1xxx_ir_read(struct cdev *dev, struct uio *uio, int flag)
{
struct sms1xxx_softc *sc = dev->si_drv1;
int err = 0;
TRACE(TRACE_IR,"\n");
if(sc == NULL || sc->sc_dying) {
TRACE(TRACE_MODULE,"dying! sc=%p\n",sc);
return (ENXIO);
}
if(sc->ir.state & IR_SLEEP) {
ERR("read already in progress\n");
return (EBUSY);
}
while((err = sms1xxx_ir_get_data(sc,uio)) > 0) {
if(flag & O_NONBLOCK)
return (EWOULDBLOCK);
sc->ir.state |= IR_SLEEP;
err = tsleep(&sc->ir, PZERO | PCATCH, "irrd", 0);
sc->ir.state &= ~IR_SLEEP;
if(sc->sc_dying) {
TRACE(TRACE_MODULE,"dying! sc=%p\n",sc);
return (ENXIO);
}
else if(err)
return (err);
}
return (-err);
}
/* Set board-specific IR options */
static void
sms1xxx_ir_config(struct sms1xxx_softc *sc)
{
TRACE(TRACE_IR,"\n");
if(sc->ir.module_started) {
WARN("IR module already configured\n");
return;
}
switch(sc->sc_type) {
case SMS1XXX_BOARD_HAUPPAUGE_WINDHAM:
sc->ir.module_avail = 1;
sc->ir.timeout = IR_DEFAULT_TIMEOUT;
sc->ir.controller = 0;
break;
default:
sc->ir.module_avail = 0;
break;
}
return;
}
/*************************
*** Public functions ***
*************************/
/* Start IR module */
int
sms1xxx_ir_init(struct sms1xxx_softc *sc)
{
TRACE(TRACE_IR,"\n");
if(sc->ir.module_started) {
WARN("IR module already initialized\n");
return (ENXIO);
}
/* Configure IR module */
sms1xxx_ir_config(sc);
if(!sc->ir.module_avail) {
/* No IR module supported */
return (ENXIO);
}
/* Start IR events */
if (sms1xxx_usb_ir_start(sc)) {
ERR("could not start IR module\n");
sc->ir.module_started = 0;
return (ENXIO);
}
/* Initialize data handling */
mtx_init(&sc->ir.lock, "DVB IR lock", NULL, MTX_DEF);
sc->ir.wavail = SMS1XXX_IR_BUFSIZE;
sc->ir.woff =
sc->ir.roff =
sc->ir.ravail = 0;
/* Create device */
sc->ir.dev = make_dev(&sms1xxx_ir_cdevsw,
device_get_unit(sc->sc_dev),
UID_ROOT, GID_WHEEL, 0444,
"dvb/adapter%d/ir0",
device_get_unit(sc->sc_dev));
if (sc->ir.dev != NULL)
sc->ir.dev->si_drv1 = sc;
TRACE(TRACE_MODULE,"created ir0 device, addr=%p\n",sc->ir.dev);
sc->ir.module_started = 1;
return (0);
}
/* Stop IR module */
int
sms1xxx_ir_exit(struct sms1xxx_softc *sc)
{
TRACE(TRACE_IR,"\n");
if(!sc->ir.module_started) {
return (ENXIO);
}
/* Wake up reader */
if(sc->ir.state & IR_SLEEP) {
wakeup(&sc->ir);
}
/* Destroy device */
if(sc->ir.dev != NULL) {
TRACE(TRACE_MODULE,"destroying ir0, addr=%p\n",sc->ir.dev);
destroy_dev(sc->ir.dev);
sc->ir.dev = NULL;
}
/* Un-initialize data handling */
sc->ir.woff =
sc->ir.wavail =
sc->ir.roff =
sc->ir.ravail = 0;
mtx_destroy(&sc->ir.lock);
sc->ir.module_started = 0;
return (0);
}
/* Handle received IR data */
int
sms1xxx_ir_put_packet(struct sms1xxx_softc *sc, const u8 *p, u32 len)
{
TRACE(TRACE_IR,"received %d bytes, wavail=%u, woff=%u\n",
len, sc->ir.wavail, sc->ir.woff);
#ifdef SMS1XXX_DEBUG
/* Dump raw data */
TRACE(TRACE_IR,"dumping %d bytes :\n", len);
if(sms1xxxdbg & TRACE_IR)
sms1xxx_dump_data(p, len, '+');
#endif
if(!sc->ir.module_started) {
if(sc->ir.module_avail)
WARN("received IR data while IR module not started, "
"ignoring...\n");
else
WARN("received IR data while no IR module supported, "
"ignoring...\n");
return (EIO);
}
/* Do not store data if nobody is reading */
if(!(sc->ir.state & IR_OPEN))
return (EIO);
/* XXX Should we ignore all bytes instead of trying to fill
the whole buffer ? */
if(len > sc->ir.wavail) {
TRACE(TRACE_IR, "not enough space in buffer, ignoring %d bytes\n",
len - sc->ir.wavail);
len = sc->ir.wavail;
}
u32 rlen = len;
if(sc->ir.woff + rlen > SMS1XXX_IR_BUFSIZE) {
memcpy(sc->ir.buf + sc->ir.woff, p, SMS1XXX_IR_BUFSIZE - sc->ir.woff);
rlen -= SMS1XXX_IR_BUFSIZE - sc->ir.woff;
sc->ir.woff = 0;
}
memcpy(sc->ir.buf + sc->ir.woff, p, rlen);
sc->ir.woff += rlen;
mtx_lock(&sc->ir.lock);
sc->ir.wavail -= len;
sc->ir.ravail += len;
mtx_unlock(&sc->ir.lock);
/* Wake up reader */
if(sc->ir.state & IR_SLEEP) {
wakeup(&sc->ir);
}
return (0);
}