udsir.c revision 1.1 1 1.1 kiyohara /* $NetBSD: udsir.c,v 1.1 2013/05/28 12:03:26 kiyohara Exp $ */
2 1.1 kiyohara
3 1.1 kiyohara /*
4 1.1 kiyohara * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 1.1 kiyohara * All rights reserved.
6 1.1 kiyohara *
7 1.1 kiyohara * This code is derived from software contributed to The NetBSD Foundation
8 1.1 kiyohara * by David Sainty <David.Sainty (at) dtsp.co.nz>
9 1.1 kiyohara *
10 1.1 kiyohara * Redistribution and use in source and binary forms, with or without
11 1.1 kiyohara * modification, are permitted provided that the following conditions
12 1.1 kiyohara * are met:
13 1.1 kiyohara * 1. Redistributions of source code must retain the above copyright
14 1.1 kiyohara * notice, this list of conditions and the following disclaimer.
15 1.1 kiyohara * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 kiyohara * notice, this list of conditions and the following disclaimer in the
17 1.1 kiyohara * documentation and/or other materials provided with the distribution.
18 1.1 kiyohara *
19 1.1 kiyohara * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 kiyohara * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 kiyohara * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 kiyohara * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 kiyohara * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 kiyohara * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 kiyohara * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 kiyohara * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 kiyohara * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 kiyohara * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 kiyohara * POSSIBILITY OF SUCH DAMAGE.
30 1.1 kiyohara */
31 1.1 kiyohara
32 1.1 kiyohara #include <sys/cdefs.h>
33 1.1 kiyohara __KERNEL_RCSID(0, "$NetBSD: udsir.c,v 1.1 2013/05/28 12:03:26 kiyohara Exp $");
34 1.1 kiyohara
35 1.1 kiyohara #include <sys/param.h>
36 1.1 kiyohara #include <sys/device.h>
37 1.1 kiyohara #include <sys/errno.h>
38 1.1 kiyohara #include <sys/systm.h>
39 1.1 kiyohara #include <sys/kernel.h>
40 1.1 kiyohara #include <sys/malloc.h>
41 1.1 kiyohara #include <sys/conf.h>
42 1.1 kiyohara #include <sys/file.h>
43 1.1 kiyohara #include <sys/poll.h>
44 1.1 kiyohara #include <sys/select.h>
45 1.1 kiyohara #include <sys/proc.h>
46 1.1 kiyohara #include <sys/kthread.h>
47 1.1 kiyohara
48 1.1 kiyohara #include <dev/usb/usb.h>
49 1.1 kiyohara #include <dev/usb/usbdevs.h>
50 1.1 kiyohara #include <dev/usb/usbdi.h>
51 1.1 kiyohara #include <dev/usb/usbdi_util.h>
52 1.1 kiyohara
53 1.1 kiyohara #include <dev/ir/ir.h>
54 1.1 kiyohara #include <dev/ir/irdaio.h>
55 1.1 kiyohara #include <dev/ir/irframevar.h>
56 1.1 kiyohara #include <dev/ir/sir.h>
57 1.1 kiyohara
58 1.1 kiyohara #ifdef UDSIR_DEBUG
59 1.1 kiyohara #define DPRINTFN(n,x) if (udsirdebug > (n)) printf x
60 1.1 kiyohara int udsirdebug = 0;
61 1.1 kiyohara #else
62 1.1 kiyohara #define DPRINTFN(n,x)
63 1.1 kiyohara #endif
64 1.1 kiyohara
65 1.1 kiyohara /* Max size with framing. */
66 1.1 kiyohara #define MAX_UDSIR_OUTPUT_FRAME (2 * IRDA_MAX_FRAME_SIZE + IRDA_MAX_EBOFS + 4)
67 1.1 kiyohara
68 1.1 kiyohara struct udsir_softc {
69 1.1 kiyohara device_t sc_dev;
70 1.1 kiyohara usbd_device_handle sc_udev;
71 1.1 kiyohara usbd_interface_handle sc_iface;
72 1.1 kiyohara
73 1.1 kiyohara uint8_t *sc_ur_buf; /* Unencapsulated frame */
74 1.1 kiyohara u_int sc_ur_framelen;
75 1.1 kiyohara
76 1.1 kiyohara uint8_t *sc_rd_buf; /* Raw incoming data stream */
77 1.1 kiyohara int sc_rd_maxpsz;
78 1.1 kiyohara size_t sc_rd_index;
79 1.1 kiyohara int sc_rd_addr;
80 1.1 kiyohara usbd_pipe_handle sc_rd_pipe;
81 1.1 kiyohara usbd_xfer_handle sc_rd_xfer;
82 1.1 kiyohara u_int sc_rd_count;
83 1.1 kiyohara int sc_rd_readinprogress;
84 1.1 kiyohara int sc_rd_expectdataticks;
85 1.1 kiyohara u_char sc_rd_err;
86 1.1 kiyohara struct framestate sc_framestate;
87 1.1 kiyohara struct lwp *sc_thread;
88 1.1 kiyohara struct selinfo sc_rd_sel;
89 1.1 kiyohara
90 1.1 kiyohara uint8_t *sc_wr_buf;
91 1.1 kiyohara int sc_wr_maxpsz;
92 1.1 kiyohara int sc_wr_addr;
93 1.1 kiyohara int sc_wr_stalewrite;
94 1.1 kiyohara usbd_xfer_handle sc_wr_xfer;
95 1.1 kiyohara usbd_pipe_handle sc_wr_pipe;
96 1.1 kiyohara struct selinfo sc_wr_sel;
97 1.1 kiyohara
98 1.1 kiyohara enum {
99 1.1 kiyohara udir_input, /* Receiving data */
100 1.1 kiyohara udir_output, /* Transmitting data */
101 1.1 kiyohara udir_stalled, /* Error preventing data flow */
102 1.1 kiyohara udir_idle /* Neither receiving nor transmitting */
103 1.1 kiyohara } sc_direction;
104 1.1 kiyohara
105 1.1 kiyohara device_t sc_child;
106 1.1 kiyohara struct irda_params sc_params;
107 1.1 kiyohara
108 1.1 kiyohara int sc_refcnt;
109 1.1 kiyohara char sc_closing;
110 1.1 kiyohara char sc_dying;
111 1.1 kiyohara };
112 1.1 kiyohara
113 1.1 kiyohara /* True if we cannot safely read data from the device */
114 1.1 kiyohara #define UDSIR_BLOCK_RX_DATA(sc) ((sc)->sc_ur_framelen != 0)
115 1.1 kiyohara
116 1.1 kiyohara #define UDSIR_WR_TIMEOUT 200
117 1.1 kiyohara
118 1.1 kiyohara static int udsir_match(device_t, cfdata_t, void *);
119 1.1 kiyohara static void udsir_attach(device_t, device_t, void *);
120 1.1 kiyohara static int udsir_detach(device_t, int);
121 1.1 kiyohara static void udsir_childdet(device_t, device_t);
122 1.1 kiyohara static int udsir_activate(device_t, enum devact);
123 1.1 kiyohara
124 1.1 kiyohara static int udsir_open(void *, int, int, struct lwp *);
125 1.1 kiyohara static int udsir_close(void *, int, int, struct lwp *);
126 1.1 kiyohara static int udsir_read(void *, struct uio *, int);
127 1.1 kiyohara static int udsir_write(void *, struct uio *, int);
128 1.1 kiyohara static int udsir_poll(void *, int, struct lwp *);
129 1.1 kiyohara static int udsir_kqfilter(void *, struct knote *);
130 1.1 kiyohara static int udsir_set_params(void *, struct irda_params *);
131 1.1 kiyohara static int udsir_get_speeds(void *, int *);
132 1.1 kiyohara static int udsir_get_turnarounds(void *, int *);
133 1.1 kiyohara
134 1.1 kiyohara static void filt_udsirrdetach(struct knote *);
135 1.1 kiyohara static int filt_udsirread(struct knote *, long);
136 1.1 kiyohara static void filt_udsirwdetach(struct knote *);
137 1.1 kiyohara static int filt_udsirwrite(struct knote *, long);
138 1.1 kiyohara
139 1.1 kiyohara static void udsir_thread(void *);
140 1.1 kiyohara
141 1.1 kiyohara #ifdef UDSIR_DEBUG
142 1.1 kiyohara static void udsir_dumpdata(uint8_t const *, size_t, char const *);
143 1.1 kiyohara #endif
144 1.1 kiyohara static int deframe_rd_ur(struct udsir_softc *);
145 1.1 kiyohara static void udsir_periodic(struct udsir_softc *);
146 1.1 kiyohara static void udsir_rd_cb(usbd_xfer_handle, usbd_private_handle, usbd_status);
147 1.1 kiyohara static usbd_status udsir_start_read(struct udsir_softc *);
148 1.1 kiyohara
149 1.1 kiyohara CFATTACH_DECL2_NEW(udsir, sizeof(struct udsir_softc),
150 1.1 kiyohara udsir_match, udsir_attach, udsir_detach,
151 1.1 kiyohara udsir_activate, NULL, udsir_childdet);
152 1.1 kiyohara
153 1.1 kiyohara static struct irframe_methods const udsir_methods = {
154 1.1 kiyohara udsir_open, udsir_close, udsir_read, udsir_write, udsir_poll,
155 1.1 kiyohara udsir_kqfilter, udsir_set_params, udsir_get_speeds, udsir_get_turnarounds,
156 1.1 kiyohara };
157 1.1 kiyohara
158 1.1 kiyohara static int
159 1.1 kiyohara udsir_match(device_t parent, cfdata_t match, void *aux)
160 1.1 kiyohara {
161 1.1 kiyohara struct usbif_attach_arg *uaa = aux;
162 1.1 kiyohara
163 1.1 kiyohara DPRINTFN(50, ("udsir_match\n"));
164 1.1 kiyohara
165 1.1 kiyohara if (uaa->vendor == USB_VENDOR_KINGSUN &&
166 1.1 kiyohara uaa->product == USB_PRODUCT_KINGSUN_IRDA)
167 1.1 kiyohara return UMATCH_VENDOR_PRODUCT;
168 1.1 kiyohara
169 1.1 kiyohara return UMATCH_NONE;
170 1.1 kiyohara }
171 1.1 kiyohara
172 1.1 kiyohara static void
173 1.1 kiyohara udsir_attach(device_t parent, device_t self, void *aux)
174 1.1 kiyohara {
175 1.1 kiyohara struct udsir_softc *sc = device_private(self);
176 1.1 kiyohara struct usbif_attach_arg *uaa = aux;
177 1.1 kiyohara usbd_device_handle dev = uaa->device;
178 1.1 kiyohara usbd_interface_handle iface = uaa->iface;
179 1.1 kiyohara char *devinfop;
180 1.1 kiyohara usb_endpoint_descriptor_t *ed;
181 1.1 kiyohara uint8_t epcount;
182 1.1 kiyohara int i;
183 1.1 kiyohara struct ir_attach_args ia;
184 1.1 kiyohara
185 1.1 kiyohara DPRINTFN(10, ("udsir_attach: sc=%p\n", sc));
186 1.1 kiyohara
187 1.1 kiyohara sc->sc_dev = self;
188 1.1 kiyohara
189 1.1 kiyohara aprint_naive("\n");
190 1.1 kiyohara aprint_normal("\n");
191 1.1 kiyohara
192 1.1 kiyohara devinfop = usbd_devinfo_alloc(dev, 0);
193 1.1 kiyohara aprint_normal_dev(self, "%s\n", devinfop);
194 1.1 kiyohara usbd_devinfo_free(devinfop);
195 1.1 kiyohara
196 1.1 kiyohara sc->sc_udev = dev;
197 1.1 kiyohara sc->sc_iface = iface;
198 1.1 kiyohara
199 1.1 kiyohara epcount = 0;
200 1.1 kiyohara (void)usbd_endpoint_count(iface, &epcount);
201 1.1 kiyohara
202 1.1 kiyohara sc->sc_rd_addr = -1;
203 1.1 kiyohara sc->sc_wr_addr = -1;
204 1.1 kiyohara for (i = 0; i < epcount; i++) {
205 1.1 kiyohara ed = usbd_interface2endpoint_descriptor(iface, i);
206 1.1 kiyohara if (ed == NULL) {
207 1.1 kiyohara aprint_error_dev(self, "couldn't get ep %d\n", i);
208 1.1 kiyohara return;
209 1.1 kiyohara }
210 1.1 kiyohara if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
211 1.1 kiyohara UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
212 1.1 kiyohara sc->sc_rd_addr = ed->bEndpointAddress;
213 1.1 kiyohara sc->sc_rd_maxpsz = UGETW(ed->wMaxPacketSize);
214 1.1 kiyohara } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
215 1.1 kiyohara UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
216 1.1 kiyohara sc->sc_wr_addr = ed->bEndpointAddress;
217 1.1 kiyohara sc->sc_wr_maxpsz = UGETW(ed->wMaxPacketSize);
218 1.1 kiyohara }
219 1.1 kiyohara }
220 1.1 kiyohara if (sc->sc_rd_addr == -1 || sc->sc_wr_addr == -1) {
221 1.1 kiyohara aprint_error_dev(self, "missing endpoint\n");
222 1.1 kiyohara return;
223 1.1 kiyohara }
224 1.1 kiyohara
225 1.1 kiyohara DPRINTFN(10, ("udsir_attach: %p\n", sc->sc_udev));
226 1.1 kiyohara
227 1.1 kiyohara usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
228 1.1 kiyohara sc->sc_dev);
229 1.1 kiyohara
230 1.1 kiyohara ia.ia_type = IR_TYPE_IRFRAME;
231 1.1 kiyohara ia.ia_methods = &udsir_methods;
232 1.1 kiyohara ia.ia_handle = sc;
233 1.1 kiyohara
234 1.1 kiyohara sc->sc_child = config_found(self, &ia, ir_print);
235 1.1 kiyohara selinit(&sc->sc_rd_sel);
236 1.1 kiyohara selinit(&sc->sc_wr_sel);
237 1.1 kiyohara
238 1.1 kiyohara return;
239 1.1 kiyohara }
240 1.1 kiyohara
241 1.1 kiyohara static int
242 1.1 kiyohara udsir_detach(device_t self, int flags)
243 1.1 kiyohara {
244 1.1 kiyohara struct udsir_softc *sc = device_private(self);
245 1.1 kiyohara int s;
246 1.1 kiyohara int rv = 0;
247 1.1 kiyohara
248 1.1 kiyohara DPRINTFN(0, ("udsir_detach: sc=%p flags=%d\n", sc, flags));
249 1.1 kiyohara
250 1.1 kiyohara sc->sc_closing = sc->sc_dying = 1;
251 1.1 kiyohara
252 1.1 kiyohara wakeup(&sc->sc_thread);
253 1.1 kiyohara
254 1.1 kiyohara while (sc->sc_thread != NULL)
255 1.1 kiyohara tsleep(&sc->sc_closing, PWAIT, "usircl", 0);
256 1.1 kiyohara
257 1.1 kiyohara /* Abort all pipes. Causes processes waiting for transfer to wake. */
258 1.1 kiyohara if (sc->sc_rd_pipe != NULL) {
259 1.1 kiyohara usbd_abort_pipe(sc->sc_rd_pipe);
260 1.1 kiyohara usbd_close_pipe(sc->sc_rd_pipe);
261 1.1 kiyohara sc->sc_rd_pipe = NULL;
262 1.1 kiyohara }
263 1.1 kiyohara if (sc->sc_wr_pipe != NULL) {
264 1.1 kiyohara usbd_abort_pipe(sc->sc_wr_pipe);
265 1.1 kiyohara usbd_close_pipe(sc->sc_wr_pipe);
266 1.1 kiyohara sc->sc_wr_pipe = NULL;
267 1.1 kiyohara }
268 1.1 kiyohara wakeup(&sc->sc_ur_framelen);
269 1.1 kiyohara wakeup(&sc->sc_wr_buf);
270 1.1 kiyohara
271 1.1 kiyohara s = splusb();
272 1.1 kiyohara if (--sc->sc_refcnt >= 0) {
273 1.1 kiyohara /* Wait for processes to go away. */
274 1.1 kiyohara usb_detach_waitold(sc->sc_dev);
275 1.1 kiyohara }
276 1.1 kiyohara splx(s);
277 1.1 kiyohara
278 1.1 kiyohara if (sc->sc_child != NULL)
279 1.1 kiyohara rv = config_detach(sc->sc_child, flags);
280 1.1 kiyohara
281 1.1 kiyohara usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
282 1.1 kiyohara
283 1.1 kiyohara seldestroy(&sc->sc_rd_sel);
284 1.1 kiyohara seldestroy(&sc->sc_wr_sel);
285 1.1 kiyohara
286 1.1 kiyohara return rv;
287 1.1 kiyohara }
288 1.1 kiyohara
289 1.1 kiyohara static void
290 1.1 kiyohara udsir_childdet(device_t self, device_t child)
291 1.1 kiyohara {
292 1.1 kiyohara struct udsir_softc *sc = device_private(self);
293 1.1 kiyohara
294 1.1 kiyohara KASSERT(sc->sc_child == child);
295 1.1 kiyohara sc->sc_child = NULL;
296 1.1 kiyohara }
297 1.1 kiyohara
298 1.1 kiyohara static int
299 1.1 kiyohara udsir_activate(device_t self, enum devact act)
300 1.1 kiyohara {
301 1.1 kiyohara struct udsir_softc *sc = device_private(self);
302 1.1 kiyohara
303 1.1 kiyohara switch (act) {
304 1.1 kiyohara case DVACT_DEACTIVATE:
305 1.1 kiyohara sc->sc_dying = 1;
306 1.1 kiyohara return 0;
307 1.1 kiyohara default:
308 1.1 kiyohara return EOPNOTSUPP;
309 1.1 kiyohara }
310 1.1 kiyohara }
311 1.1 kiyohara
312 1.1 kiyohara /* ARGSUSED */
313 1.1 kiyohara static int
314 1.1 kiyohara udsir_open(void *h, int flag, int mode, struct lwp *l)
315 1.1 kiyohara {
316 1.1 kiyohara struct udsir_softc *sc = h;
317 1.1 kiyohara int error;
318 1.1 kiyohara usbd_status err;
319 1.1 kiyohara
320 1.1 kiyohara DPRINTFN(0, ("%s: sc=%p\n", __func__, sc));
321 1.1 kiyohara
322 1.1 kiyohara err = usbd_open_pipe(sc->sc_iface, sc->sc_rd_addr, 0, &sc->sc_rd_pipe);
323 1.1 kiyohara if (err != USBD_NORMAL_COMPLETION) {
324 1.1 kiyohara error = EIO;
325 1.1 kiyohara goto bad1;
326 1.1 kiyohara }
327 1.1 kiyohara err = usbd_open_pipe(sc->sc_iface, sc->sc_wr_addr, 0, &sc->sc_wr_pipe);
328 1.1 kiyohara if (err != USBD_NORMAL_COMPLETION) {
329 1.1 kiyohara error = EIO;
330 1.1 kiyohara goto bad2;
331 1.1 kiyohara }
332 1.1 kiyohara sc->sc_rd_xfer = usbd_alloc_xfer(sc->sc_udev);
333 1.1 kiyohara if (sc->sc_rd_xfer == NULL) {
334 1.1 kiyohara error = ENOMEM;
335 1.1 kiyohara goto bad3;
336 1.1 kiyohara }
337 1.1 kiyohara sc->sc_wr_xfer = usbd_alloc_xfer(sc->sc_udev);
338 1.1 kiyohara if (sc->sc_wr_xfer == NULL) {
339 1.1 kiyohara error = ENOMEM;
340 1.1 kiyohara goto bad4;
341 1.1 kiyohara }
342 1.1 kiyohara sc->sc_rd_buf = usbd_alloc_buffer(sc->sc_rd_xfer, sc->sc_rd_maxpsz);
343 1.1 kiyohara if (sc->sc_rd_buf == NULL) {
344 1.1 kiyohara error = ENOMEM;
345 1.1 kiyohara goto bad5;
346 1.1 kiyohara }
347 1.1 kiyohara sc->sc_wr_buf = usbd_alloc_buffer(sc->sc_wr_xfer, IRDA_MAX_FRAME_SIZE);
348 1.1 kiyohara if (sc->sc_wr_buf == NULL) {
349 1.1 kiyohara error = ENOMEM;
350 1.1 kiyohara goto bad5;
351 1.1 kiyohara }
352 1.1 kiyohara sc->sc_ur_buf = malloc(IRDA_MAX_FRAME_SIZE, M_USBDEV, M_NOWAIT);
353 1.1 kiyohara if (sc->sc_ur_buf == NULL) {
354 1.1 kiyohara error = ENOMEM;
355 1.1 kiyohara goto bad5;
356 1.1 kiyohara }
357 1.1 kiyohara
358 1.1 kiyohara sc->sc_rd_index = sc->sc_rd_count = 0;
359 1.1 kiyohara sc->sc_closing = 0;
360 1.1 kiyohara sc->sc_rd_readinprogress = 0;
361 1.1 kiyohara sc->sc_rd_expectdataticks = 0;
362 1.1 kiyohara sc->sc_ur_framelen = 0;
363 1.1 kiyohara sc->sc_rd_err = 0;
364 1.1 kiyohara sc->sc_wr_stalewrite = 0;
365 1.1 kiyohara sc->sc_direction = udir_idle;
366 1.1 kiyohara sc->sc_params.speed = 0;
367 1.1 kiyohara sc->sc_params.ebofs = 0;
368 1.1 kiyohara sc->sc_params.maxsize = min(sc->sc_rd_maxpsz, sc->sc_wr_maxpsz);
369 1.1 kiyohara
370 1.1 kiyohara deframe_init(&sc->sc_framestate, sc->sc_ur_buf, IRDA_MAX_FRAME_SIZE);
371 1.1 kiyohara
372 1.1 kiyohara /* Increment reference for thread */
373 1.1 kiyohara sc->sc_refcnt++;
374 1.1 kiyohara
375 1.1 kiyohara error = kthread_create(PRI_NONE, 0, NULL, udsir_thread, sc,
376 1.1 kiyohara &sc->sc_thread, "%s", device_xname(sc->sc_dev));
377 1.1 kiyohara if (error) {
378 1.1 kiyohara sc->sc_refcnt--;
379 1.1 kiyohara goto bad5;
380 1.1 kiyohara }
381 1.1 kiyohara
382 1.1 kiyohara return 0;
383 1.1 kiyohara
384 1.1 kiyohara bad5:
385 1.1 kiyohara usbd_free_xfer(sc->sc_wr_xfer);
386 1.1 kiyohara sc->sc_wr_xfer = NULL;
387 1.1 kiyohara bad4:
388 1.1 kiyohara usbd_free_xfer(sc->sc_rd_xfer);
389 1.1 kiyohara sc->sc_rd_xfer = NULL;
390 1.1 kiyohara bad3:
391 1.1 kiyohara usbd_close_pipe(sc->sc_wr_pipe);
392 1.1 kiyohara sc->sc_wr_pipe = NULL;
393 1.1 kiyohara bad2:
394 1.1 kiyohara usbd_close_pipe(sc->sc_rd_pipe);
395 1.1 kiyohara sc->sc_rd_pipe = NULL;
396 1.1 kiyohara bad1:
397 1.1 kiyohara return error;
398 1.1 kiyohara }
399 1.1 kiyohara
400 1.1 kiyohara /* ARGSUSED */
401 1.1 kiyohara static int
402 1.1 kiyohara udsir_close(void *h, int flag, int mode, struct lwp *l)
403 1.1 kiyohara {
404 1.1 kiyohara struct udsir_softc *sc = h;
405 1.1 kiyohara
406 1.1 kiyohara DPRINTFN(0, ("%s: sc=%p\n", __func__, sc));
407 1.1 kiyohara
408 1.1 kiyohara sc->sc_refcnt++;
409 1.1 kiyohara
410 1.1 kiyohara sc->sc_rd_readinprogress = 1;
411 1.1 kiyohara sc->sc_closing = 1;
412 1.1 kiyohara
413 1.1 kiyohara wakeup(&sc->sc_thread);
414 1.1 kiyohara
415 1.1 kiyohara while (sc->sc_thread != NULL)
416 1.1 kiyohara tsleep(&sc->sc_closing, PWAIT, "usircl", 0);
417 1.1 kiyohara
418 1.1 kiyohara if (sc->sc_rd_pipe != NULL) {
419 1.1 kiyohara usbd_abort_pipe(sc->sc_rd_pipe);
420 1.1 kiyohara usbd_close_pipe(sc->sc_rd_pipe);
421 1.1 kiyohara sc->sc_rd_pipe = NULL;
422 1.1 kiyohara }
423 1.1 kiyohara if (sc->sc_wr_pipe != NULL) {
424 1.1 kiyohara usbd_abort_pipe(sc->sc_wr_pipe);
425 1.1 kiyohara usbd_close_pipe(sc->sc_wr_pipe);
426 1.1 kiyohara sc->sc_wr_pipe = NULL;
427 1.1 kiyohara }
428 1.1 kiyohara if (sc->sc_rd_xfer != NULL) {
429 1.1 kiyohara usbd_free_xfer(sc->sc_rd_xfer);
430 1.1 kiyohara sc->sc_rd_xfer = NULL;
431 1.1 kiyohara sc->sc_rd_buf = NULL;
432 1.1 kiyohara }
433 1.1 kiyohara if (sc->sc_wr_xfer != NULL) {
434 1.1 kiyohara usbd_free_xfer(sc->sc_wr_xfer);
435 1.1 kiyohara sc->sc_wr_xfer = NULL;
436 1.1 kiyohara sc->sc_wr_buf = NULL;
437 1.1 kiyohara }
438 1.1 kiyohara if (sc->sc_ur_buf != NULL) {
439 1.1 kiyohara free(sc->sc_ur_buf, M_USBDEV);
440 1.1 kiyohara sc->sc_ur_buf = NULL;
441 1.1 kiyohara }
442 1.1 kiyohara
443 1.1 kiyohara if (--sc->sc_refcnt < 0)
444 1.1 kiyohara usb_detach_wakeupold(sc->sc_dev);
445 1.1 kiyohara
446 1.1 kiyohara return 0;
447 1.1 kiyohara }
448 1.1 kiyohara
449 1.1 kiyohara /* ARGSUSED */
450 1.1 kiyohara static int
451 1.1 kiyohara udsir_read(void *h, struct uio *uio, int flag)
452 1.1 kiyohara {
453 1.1 kiyohara struct udsir_softc *sc = h;
454 1.1 kiyohara int s;
455 1.1 kiyohara int error;
456 1.1 kiyohara u_int uframelen;
457 1.1 kiyohara
458 1.1 kiyohara DPRINTFN(1, ("%s: sc=%p\n", __func__, sc));
459 1.1 kiyohara
460 1.1 kiyohara if (sc->sc_dying)
461 1.1 kiyohara return EIO;
462 1.1 kiyohara
463 1.1 kiyohara #ifdef DIAGNOSTIC
464 1.1 kiyohara if (sc->sc_rd_buf == NULL)
465 1.1 kiyohara return EINVAL;
466 1.1 kiyohara #endif
467 1.1 kiyohara
468 1.1 kiyohara sc->sc_refcnt++;
469 1.1 kiyohara
470 1.1 kiyohara if (!sc->sc_rd_readinprogress && !UDSIR_BLOCK_RX_DATA(sc))
471 1.1 kiyohara /* Possibly wake up polling thread */
472 1.1 kiyohara wakeup(&sc->sc_thread);
473 1.1 kiyohara
474 1.1 kiyohara do {
475 1.1 kiyohara s = splusb();
476 1.1 kiyohara while (sc->sc_ur_framelen == 0) {
477 1.1 kiyohara DPRINTFN(5, ("%s: calling tsleep()\n", __func__));
478 1.1 kiyohara error = tsleep(&sc->sc_ur_framelen, PZERO | PCATCH,
479 1.1 kiyohara "usirrd", 0);
480 1.1 kiyohara if (sc->sc_dying)
481 1.1 kiyohara error = EIO;
482 1.1 kiyohara if (error) {
483 1.1 kiyohara splx(s);
484 1.1 kiyohara DPRINTFN(0, ("%s: tsleep() = %d\n",
485 1.1 kiyohara __func__, error));
486 1.1 kiyohara goto ret;
487 1.1 kiyohara }
488 1.1 kiyohara }
489 1.1 kiyohara splx(s);
490 1.1 kiyohara
491 1.1 kiyohara uframelen = sc->sc_ur_framelen;
492 1.1 kiyohara DPRINTFN(1, ("%s: sc=%p framelen=%u, hdr=0x%02x\n",
493 1.1 kiyohara __func__, sc, uframelen, sc->sc_ur_buf[0]));
494 1.1 kiyohara if (uframelen > uio->uio_resid)
495 1.1 kiyohara error = EINVAL;
496 1.1 kiyohara else
497 1.1 kiyohara error = uiomove(sc->sc_ur_buf, uframelen, uio);
498 1.1 kiyohara sc->sc_ur_framelen = 0;
499 1.1 kiyohara
500 1.1 kiyohara if (deframe_rd_ur(sc) == 0 && uframelen > 0) {
501 1.1 kiyohara /*
502 1.1 kiyohara * Need to wait for another read to obtain a
503 1.1 kiyohara * complete frame... If we also obtained
504 1.1 kiyohara * actual data, wake up the possibly sleeping
505 1.1 kiyohara * thread immediately...
506 1.1 kiyohara */
507 1.1 kiyohara wakeup(&sc->sc_thread);
508 1.1 kiyohara }
509 1.1 kiyohara } while (uframelen == 0);
510 1.1 kiyohara
511 1.1 kiyohara DPRINTFN(1, ("%s: return %d\n", __func__, error));
512 1.1 kiyohara
513 1.1 kiyohara ret:
514 1.1 kiyohara if (--sc->sc_refcnt < 0)
515 1.1 kiyohara usb_detach_wakeupold(sc->sc_dev);
516 1.1 kiyohara return error;
517 1.1 kiyohara }
518 1.1 kiyohara
519 1.1 kiyohara /* ARGSUSED */
520 1.1 kiyohara static int
521 1.1 kiyohara udsir_write(void *h, struct uio *uio, int flag)
522 1.1 kiyohara {
523 1.1 kiyohara struct udsir_softc *sc = h;
524 1.1 kiyohara usbd_status err;
525 1.1 kiyohara uint32_t wrlen;
526 1.1 kiyohara int error, sirlength;
527 1.1 kiyohara uint8_t *wrbuf;
528 1.1 kiyohara int s;
529 1.1 kiyohara
530 1.1 kiyohara DPRINTFN(1, ("%s: sc=%p\n", __func__, sc));
531 1.1 kiyohara
532 1.1 kiyohara if (sc->sc_dying)
533 1.1 kiyohara return EIO;
534 1.1 kiyohara
535 1.1 kiyohara #ifdef DIAGNOSTIC
536 1.1 kiyohara if (sc->sc_wr_buf == NULL)
537 1.1 kiyohara return EINVAL;
538 1.1 kiyohara #endif
539 1.1 kiyohara
540 1.1 kiyohara wrlen = uio->uio_resid;
541 1.1 kiyohara if (wrlen > sc->sc_wr_maxpsz)
542 1.1 kiyohara return EINVAL;
543 1.1 kiyohara
544 1.1 kiyohara sc->sc_refcnt++;
545 1.1 kiyohara
546 1.1 kiyohara if (!UDSIR_BLOCK_RX_DATA(sc)) {
547 1.1 kiyohara /*
548 1.1 kiyohara * If reads are not blocked, determine what action we
549 1.1 kiyohara * should potentially take...
550 1.1 kiyohara */
551 1.1 kiyohara if (sc->sc_direction == udir_output) {
552 1.1 kiyohara /*
553 1.1 kiyohara * If the last operation was an output, wait for the
554 1.1 kiyohara * polling thread to check for incoming data.
555 1.1 kiyohara */
556 1.1 kiyohara sc->sc_wr_stalewrite = 1;
557 1.1 kiyohara wakeup(&sc->sc_thread);
558 1.1 kiyohara } else if (!sc->sc_rd_readinprogress &&
559 1.1 kiyohara (sc->sc_direction == udir_idle ||
560 1.1 kiyohara sc->sc_direction == udir_input)) {
561 1.1 kiyohara /* If idle, check for input before outputting */
562 1.1 kiyohara udsir_start_read(sc);
563 1.1 kiyohara }
564 1.1 kiyohara }
565 1.1 kiyohara
566 1.1 kiyohara s = splusb();
567 1.1 kiyohara while (sc->sc_wr_stalewrite ||
568 1.1 kiyohara (sc->sc_direction != udir_output &&
569 1.1 kiyohara sc->sc_direction != udir_idle)) {
570 1.1 kiyohara DPRINTFN(5, ("%s: sc=%p stalewrite=%d direction=%d, "
571 1.1 kiyohara "calling tsleep()\n",
572 1.1 kiyohara __func__, sc, sc->sc_wr_stalewrite,
573 1.1 kiyohara sc->sc_direction));
574 1.1 kiyohara error = tsleep(&sc->sc_wr_buf, PZERO | PCATCH, "usirwr", 0);
575 1.1 kiyohara if (sc->sc_dying)
576 1.1 kiyohara error = EIO;
577 1.1 kiyohara if (error) {
578 1.1 kiyohara splx(s);
579 1.1 kiyohara DPRINTFN(0, ("%s: tsleep() = %d\n", __func__, error));
580 1.1 kiyohara goto ret;
581 1.1 kiyohara }
582 1.1 kiyohara }
583 1.1 kiyohara splx(s);
584 1.1 kiyohara
585 1.1 kiyohara wrbuf = sc->sc_wr_buf;
586 1.1 kiyohara
587 1.1 kiyohara sirlength = irda_sir_frame(wrbuf, MAX_UDSIR_OUTPUT_FRAME,
588 1.1 kiyohara uio, sc->sc_params.ebofs);
589 1.1 kiyohara if (sirlength < 0)
590 1.1 kiyohara error = -sirlength;
591 1.1 kiyohara else {
592 1.1 kiyohara uint32_t btlen;
593 1.1 kiyohara
594 1.1 kiyohara DPRINTFN(1, ("%s: transfer %u bytes\n",
595 1.1 kiyohara __func__, (unsigned int)wrlen));
596 1.1 kiyohara
597 1.1 kiyohara btlen = sirlength;
598 1.1 kiyohara
599 1.1 kiyohara sc->sc_direction = udir_output;
600 1.1 kiyohara
601 1.1 kiyohara #ifdef UDSIR_DEBUG
602 1.1 kiyohara if (udsirdebug >= 20)
603 1.1 kiyohara udsir_dumpdata(wrbuf, btlen, __func__);
604 1.1 kiyohara #endif
605 1.1 kiyohara
606 1.1 kiyohara err = usbd_intr_transfer(sc->sc_wr_xfer, sc->sc_wr_pipe,
607 1.1 kiyohara USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
608 1.1 kiyohara UDSIR_WR_TIMEOUT, wrbuf, &btlen, "udsiwr");
609 1.1 kiyohara DPRINTFN(2, ("%s: err=%d\n", __func__, err));
610 1.1 kiyohara if (err != USBD_NORMAL_COMPLETION) {
611 1.1 kiyohara if (err == USBD_INTERRUPTED)
612 1.1 kiyohara error = EINTR;
613 1.1 kiyohara else if (err == USBD_TIMEOUT)
614 1.1 kiyohara error = ETIMEDOUT;
615 1.1 kiyohara else
616 1.1 kiyohara error = EIO;
617 1.1 kiyohara } else
618 1.1 kiyohara error = 0;
619 1.1 kiyohara }
620 1.1 kiyohara
621 1.1 kiyohara ret:
622 1.1 kiyohara if (--sc->sc_refcnt < 0)
623 1.1 kiyohara usb_detach_wakeupold(sc->sc_dev);
624 1.1 kiyohara
625 1.1 kiyohara DPRINTFN(1, ("%s: sc=%p done\n", __func__, sc));
626 1.1 kiyohara return error;
627 1.1 kiyohara }
628 1.1 kiyohara
629 1.1 kiyohara static int
630 1.1 kiyohara udsir_poll(void *h, int events, struct lwp *l)
631 1.1 kiyohara {
632 1.1 kiyohara struct udsir_softc *sc = h;
633 1.1 kiyohara int revents = 0;
634 1.1 kiyohara
635 1.1 kiyohara DPRINTFN(1, ("%s: sc=%p\n", __func__, sc));
636 1.1 kiyohara
637 1.1 kiyohara if (events & (POLLOUT | POLLWRNORM)) {
638 1.1 kiyohara if (sc->sc_direction != udir_input)
639 1.1 kiyohara revents |= events & (POLLOUT | POLLWRNORM);
640 1.1 kiyohara else {
641 1.1 kiyohara DPRINTFN(2, ("%s: recording write select\n", __func__));
642 1.1 kiyohara selrecord(l, &sc->sc_wr_sel);
643 1.1 kiyohara }
644 1.1 kiyohara }
645 1.1 kiyohara
646 1.1 kiyohara if (events & (POLLIN | POLLRDNORM)) {
647 1.1 kiyohara if (sc->sc_ur_framelen != 0) {
648 1.1 kiyohara DPRINTFN(2, ("%s: have data\n", __func__));
649 1.1 kiyohara revents |= events & (POLLIN | POLLRDNORM);
650 1.1 kiyohara } else {
651 1.1 kiyohara DPRINTFN(2, ("%s: recording read select\n", __func__));
652 1.1 kiyohara selrecord(l, &sc->sc_rd_sel);
653 1.1 kiyohara }
654 1.1 kiyohara }
655 1.1 kiyohara
656 1.1 kiyohara return revents;
657 1.1 kiyohara }
658 1.1 kiyohara
659 1.1 kiyohara static const struct filterops udsirread_filtops =
660 1.1 kiyohara { 1, NULL, filt_udsirrdetach, filt_udsirread };
661 1.1 kiyohara static const struct filterops udsirwrite_filtops =
662 1.1 kiyohara { 1, NULL, filt_udsirwdetach, filt_udsirwrite };
663 1.1 kiyohara
664 1.1 kiyohara static int
665 1.1 kiyohara udsir_kqfilter(void *h, struct knote *kn)
666 1.1 kiyohara {
667 1.1 kiyohara struct udsir_softc *sc = h;
668 1.1 kiyohara struct klist *klist;
669 1.1 kiyohara int s;
670 1.1 kiyohara
671 1.1 kiyohara switch (kn->kn_filter) {
672 1.1 kiyohara case EVFILT_READ:
673 1.1 kiyohara klist = &sc->sc_rd_sel.sel_klist;
674 1.1 kiyohara kn->kn_fop = &udsirread_filtops;
675 1.1 kiyohara break;
676 1.1 kiyohara case EVFILT_WRITE:
677 1.1 kiyohara klist = &sc->sc_wr_sel.sel_klist;
678 1.1 kiyohara kn->kn_fop = &udsirwrite_filtops;
679 1.1 kiyohara break;
680 1.1 kiyohara default:
681 1.1 kiyohara return (EINVAL);
682 1.1 kiyohara }
683 1.1 kiyohara
684 1.1 kiyohara kn->kn_hook = sc;
685 1.1 kiyohara
686 1.1 kiyohara s = splusb();
687 1.1 kiyohara SLIST_INSERT_HEAD(klist, kn, kn_selnext);
688 1.1 kiyohara splx(s);
689 1.1 kiyohara
690 1.1 kiyohara return (0);
691 1.1 kiyohara }
692 1.1 kiyohara
693 1.1 kiyohara static int
694 1.1 kiyohara udsir_set_params(void *h, struct irda_params *p)
695 1.1 kiyohara {
696 1.1 kiyohara struct udsir_softc *sc = h;
697 1.1 kiyohara
698 1.1 kiyohara DPRINTFN(0, ("%s: sc=%p, speed=%d ebofs=%d maxsize=%d\n",
699 1.1 kiyohara __func__, sc, p->speed, p->ebofs, p->maxsize));
700 1.1 kiyohara
701 1.1 kiyohara if (sc->sc_dying)
702 1.1 kiyohara return EIO;
703 1.1 kiyohara
704 1.1 kiyohara if (p->speed != 9600)
705 1.1 kiyohara return EINVAL;
706 1.1 kiyohara
707 1.1 kiyohara if (p->maxsize != sc->sc_params.maxsize) {
708 1.1 kiyohara if (p->maxsize > min(sc->sc_rd_maxpsz, sc->sc_wr_maxpsz))
709 1.1 kiyohara return EINVAL;
710 1.1 kiyohara sc->sc_params.maxsize = p->maxsize;
711 1.1 kiyohara }
712 1.1 kiyohara
713 1.1 kiyohara sc->sc_params = *p;
714 1.1 kiyohara
715 1.1 kiyohara return 0;
716 1.1 kiyohara }
717 1.1 kiyohara
718 1.1 kiyohara static int
719 1.1 kiyohara udsir_get_speeds(void *h, int *speeds)
720 1.1 kiyohara {
721 1.1 kiyohara struct udsir_softc *sc = h;
722 1.1 kiyohara
723 1.1 kiyohara DPRINTFN(0, ("%s: sc=%p\n", __func__, sc));
724 1.1 kiyohara
725 1.1 kiyohara if (sc->sc_dying)
726 1.1 kiyohara return EIO;
727 1.1 kiyohara
728 1.1 kiyohara /* Support only 9600bps now. */
729 1.1 kiyohara *speeds = IRDA_SPEED_9600;
730 1.1 kiyohara
731 1.1 kiyohara return 0;
732 1.1 kiyohara }
733 1.1 kiyohara
734 1.1 kiyohara static int
735 1.1 kiyohara udsir_get_turnarounds(void *h, int *turnarounds)
736 1.1 kiyohara {
737 1.1 kiyohara struct udsir_softc *sc = h;
738 1.1 kiyohara
739 1.1 kiyohara DPRINTFN(0, ("%s: sc=%p\n", __func__, sc));
740 1.1 kiyohara
741 1.1 kiyohara if (sc->sc_dying)
742 1.1 kiyohara return EIO;
743 1.1 kiyohara
744 1.1 kiyohara /*
745 1.1 kiyohara * Documentation is on the light side with respect to
746 1.1 kiyohara * turnaround time for this device.
747 1.1 kiyohara */
748 1.1 kiyohara *turnarounds = IRDA_TURNT_10000;
749 1.1 kiyohara
750 1.1 kiyohara return 0;
751 1.1 kiyohara }
752 1.1 kiyohara
753 1.1 kiyohara static void
754 1.1 kiyohara filt_udsirrdetach(struct knote *kn)
755 1.1 kiyohara {
756 1.1 kiyohara struct udsir_softc *sc = kn->kn_hook;
757 1.1 kiyohara int s;
758 1.1 kiyohara
759 1.1 kiyohara s = splusb();
760 1.1 kiyohara SLIST_REMOVE(&sc->sc_rd_sel.sel_klist, kn, knote, kn_selnext);
761 1.1 kiyohara splx(s);
762 1.1 kiyohara }
763 1.1 kiyohara
764 1.1 kiyohara /* ARGSUSED */
765 1.1 kiyohara static int
766 1.1 kiyohara filt_udsirread(struct knote *kn, long hint)
767 1.1 kiyohara {
768 1.1 kiyohara struct udsir_softc *sc = kn->kn_hook;
769 1.1 kiyohara
770 1.1 kiyohara kn->kn_data = sc->sc_ur_framelen;
771 1.1 kiyohara return (kn->kn_data > 0);
772 1.1 kiyohara }
773 1.1 kiyohara
774 1.1 kiyohara static void
775 1.1 kiyohara filt_udsirwdetach(struct knote *kn)
776 1.1 kiyohara {
777 1.1 kiyohara struct udsir_softc *sc = kn->kn_hook;
778 1.1 kiyohara int s;
779 1.1 kiyohara
780 1.1 kiyohara s = splusb();
781 1.1 kiyohara SLIST_REMOVE(&sc->sc_wr_sel.sel_klist, kn, knote, kn_selnext);
782 1.1 kiyohara splx(s);
783 1.1 kiyohara }
784 1.1 kiyohara
785 1.1 kiyohara /* ARGSUSED */
786 1.1 kiyohara static int
787 1.1 kiyohara filt_udsirwrite(struct knote *kn, long hint)
788 1.1 kiyohara {
789 1.1 kiyohara struct udsir_softc *sc = kn->kn_hook;
790 1.1 kiyohara
791 1.1 kiyohara kn->kn_data = 0;
792 1.1 kiyohara return (sc->sc_direction != udir_input);
793 1.1 kiyohara }
794 1.1 kiyohara
795 1.1 kiyohara
796 1.1 kiyohara static void
797 1.1 kiyohara udsir_thread(void *arg)
798 1.1 kiyohara {
799 1.1 kiyohara struct udsir_softc *sc = arg;
800 1.1 kiyohara int error;
801 1.1 kiyohara
802 1.1 kiyohara DPRINTFN(20, ("%s: starting polling thread\n", __func__));
803 1.1 kiyohara
804 1.1 kiyohara while (!sc->sc_closing) {
805 1.1 kiyohara if (!sc->sc_rd_readinprogress && !UDSIR_BLOCK_RX_DATA(sc))
806 1.1 kiyohara udsir_periodic(sc);
807 1.1 kiyohara
808 1.1 kiyohara if (!sc->sc_closing) {
809 1.1 kiyohara error = tsleep(&sc->sc_thread, PWAIT, "udsir", hz / 10);
810 1.1 kiyohara if (error == EWOULDBLOCK &&
811 1.1 kiyohara sc->sc_rd_expectdataticks > 0)
812 1.1 kiyohara /*
813 1.1 kiyohara * After a timeout decrement the tick
814 1.1 kiyohara * counter within which time we expect
815 1.1 kiyohara * data to arrive if we are receiving
816 1.1 kiyohara * data...
817 1.1 kiyohara */
818 1.1 kiyohara sc->sc_rd_expectdataticks--;
819 1.1 kiyohara }
820 1.1 kiyohara }
821 1.1 kiyohara
822 1.1 kiyohara DPRINTFN(20, ("%s: exiting polling thread\n", __func__));
823 1.1 kiyohara
824 1.1 kiyohara sc->sc_thread = NULL;
825 1.1 kiyohara
826 1.1 kiyohara wakeup(&sc->sc_closing);
827 1.1 kiyohara
828 1.1 kiyohara if (--sc->sc_refcnt < 0)
829 1.1 kiyohara usb_detach_wakeupold(sc->sc_dev);
830 1.1 kiyohara
831 1.1 kiyohara kthread_exit(0);
832 1.1 kiyohara }
833 1.1 kiyohara
834 1.1 kiyohara #ifdef UDSIR_DEBUG
835 1.1 kiyohara static void
836 1.1 kiyohara udsir_dumpdata(uint8_t const *data, size_t dlen, char const *desc)
837 1.1 kiyohara {
838 1.1 kiyohara size_t bdindex;
839 1.1 kiyohara
840 1.1 kiyohara printf("%s: (%lx)", desc, (unsigned long)dlen);
841 1.1 kiyohara for (bdindex = 0; bdindex < dlen; bdindex++)
842 1.1 kiyohara printf(" %02x", (unsigned int)data[bdindex]);
843 1.1 kiyohara printf("\n");
844 1.1 kiyohara }
845 1.1 kiyohara #endif
846 1.1 kiyohara
847 1.1 kiyohara /* Returns 0 if more data required, 1 if a complete frame was extracted */
848 1.1 kiyohara static int
849 1.1 kiyohara deframe_rd_ur(struct udsir_softc *sc)
850 1.1 kiyohara {
851 1.1 kiyohara
852 1.1 kiyohara if (sc->sc_rd_index == 0) {
853 1.1 kiyohara KASSERT(sc->sc_rd_count == sc->sc_rd_maxpsz);
854 1.1 kiyohara /* valid count */
855 1.1 kiyohara sc->sc_rd_count = sc->sc_rd_buf[sc->sc_rd_index++] + 1;
856 1.1 kiyohara KASSERT(sc->sc_rd_count < sc->sc_rd_maxpsz);
857 1.1 kiyohara }
858 1.1 kiyohara
859 1.1 kiyohara while (sc->sc_rd_index < sc->sc_rd_count) {
860 1.1 kiyohara uint8_t const *buf;
861 1.1 kiyohara size_t buflen;
862 1.1 kiyohara enum frameresult fresult;
863 1.1 kiyohara
864 1.1 kiyohara buf = &sc->sc_rd_buf[sc->sc_rd_index];
865 1.1 kiyohara buflen = sc->sc_rd_count - sc->sc_rd_index;
866 1.1 kiyohara
867 1.1 kiyohara fresult = deframe_process(&sc->sc_framestate, &buf, &buflen);
868 1.1 kiyohara
869 1.1 kiyohara sc->sc_rd_index = sc->sc_rd_count - buflen;
870 1.1 kiyohara
871 1.1 kiyohara DPRINTFN(1,("%s: result=%d\n", __func__, (int)fresult));
872 1.1 kiyohara
873 1.1 kiyohara switch (fresult) {
874 1.1 kiyohara case FR_IDLE:
875 1.1 kiyohara case FR_INPROGRESS:
876 1.1 kiyohara case FR_FRAMEBADFCS:
877 1.1 kiyohara case FR_FRAMEMALFORMED:
878 1.1 kiyohara case FR_BUFFEROVERRUN:
879 1.1 kiyohara break;
880 1.1 kiyohara case FR_FRAMEOK:
881 1.1 kiyohara sc->sc_ur_framelen = sc->sc_framestate.bufindex;
882 1.1 kiyohara wakeup(&sc->sc_ur_framelen); /* XXX should use flag */
883 1.1 kiyohara selnotify(&sc->sc_rd_sel, 0, 0);
884 1.1 kiyohara return 1;
885 1.1 kiyohara }
886 1.1 kiyohara }
887 1.1 kiyohara
888 1.1 kiyohara /* Reset indices into USB-side buffer */
889 1.1 kiyohara sc->sc_rd_index = sc->sc_rd_count = 0;
890 1.1 kiyohara
891 1.1 kiyohara return 0;
892 1.1 kiyohara }
893 1.1 kiyohara
894 1.1 kiyohara /*
895 1.1 kiyohara * Direction transitions:
896 1.1 kiyohara *
897 1.1 kiyohara * udsir_periodic() can switch the direction from:
898 1.1 kiyohara *
899 1.1 kiyohara * output -> idle
900 1.1 kiyohara * output -> stalled
901 1.1 kiyohara * stalled -> idle
902 1.1 kiyohara * idle -> input
903 1.1 kiyohara *
904 1.1 kiyohara * udsir_rd_cb() can switch the direction from:
905 1.1 kiyohara *
906 1.1 kiyohara * input -> stalled
907 1.1 kiyohara * input -> idle
908 1.1 kiyohara *
909 1.1 kiyohara * udsir_write() can switch the direction from:
910 1.1 kiyohara *
911 1.1 kiyohara * idle -> output
912 1.1 kiyohara */
913 1.1 kiyohara static void
914 1.1 kiyohara udsir_periodic(struct udsir_softc *sc)
915 1.1 kiyohara {
916 1.1 kiyohara
917 1.1 kiyohara DPRINTFN(60, ("%s: direction = %d\n", __func__, sc->sc_direction));
918 1.1 kiyohara
919 1.1 kiyohara if (sc->sc_wr_stalewrite && sc->sc_direction == udir_idle) {
920 1.1 kiyohara /*
921 1.1 kiyohara * In a stale write case, we need to check if the
922 1.1 kiyohara * write has completed. Once that has happened, the
923 1.1 kiyohara * write is no longer stale.
924 1.1 kiyohara *
925 1.1 kiyohara * But note that we may immediately start a read poll...
926 1.1 kiyohara */
927 1.1 kiyohara sc->sc_wr_stalewrite = 0;
928 1.1 kiyohara wakeup(&sc->sc_wr_buf);
929 1.1 kiyohara }
930 1.1 kiyohara
931 1.1 kiyohara if (!sc->sc_rd_readinprogress &&
932 1.1 kiyohara (sc->sc_direction == udir_idle ||
933 1.1 kiyohara sc->sc_direction == udir_input))
934 1.1 kiyohara /* Do a read poll if appropriate... */
935 1.1 kiyohara udsir_start_read(sc);
936 1.1 kiyohara }
937 1.1 kiyohara
938 1.1 kiyohara static void
939 1.1 kiyohara udsir_rd_cb(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
940 1.1 kiyohara {
941 1.1 kiyohara struct udsir_softc *sc = priv;
942 1.1 kiyohara uint32_t size;
943 1.1 kiyohara
944 1.1 kiyohara DPRINTFN(60, ("%s: sc=%p\n", __func__, sc));
945 1.1 kiyohara
946 1.1 kiyohara /* Read is no longer in progress */
947 1.1 kiyohara sc->sc_rd_readinprogress = 0;
948 1.1 kiyohara
949 1.1 kiyohara if (status == USBD_CANCELLED || sc->sc_closing) /* this is normal */
950 1.1 kiyohara return;
951 1.1 kiyohara if (status) {
952 1.1 kiyohara size = 0;
953 1.1 kiyohara sc->sc_rd_err = 1;
954 1.1 kiyohara
955 1.1 kiyohara if (sc->sc_direction == udir_input ||
956 1.1 kiyohara sc->sc_direction == udir_idle) {
957 1.1 kiyohara /*
958 1.1 kiyohara * Receive error, probably need to clear error
959 1.1 kiyohara * condition.
960 1.1 kiyohara */
961 1.1 kiyohara sc->sc_direction = udir_stalled;
962 1.1 kiyohara }
963 1.1 kiyohara } else
964 1.1 kiyohara usbd_get_xfer_status(xfer, NULL, NULL, &size, NULL);
965 1.1 kiyohara
966 1.1 kiyohara sc->sc_rd_index = 0;
967 1.1 kiyohara sc->sc_rd_count = size;
968 1.1 kiyohara
969 1.1 kiyohara DPRINTFN(((size > 0 || sc->sc_rd_err != 0) ? 20 : 60),
970 1.1 kiyohara ("%s: sc=%p size=%u, err=%d\n",
971 1.1 kiyohara __func__, sc, size, sc->sc_rd_err));
972 1.1 kiyohara
973 1.1 kiyohara #ifdef UDSIR_DEBUG
974 1.1 kiyohara if (udsirdebug >= 20 && size > 0)
975 1.1 kiyohara udsir_dumpdata(sc->sc_rd_buf, size, __func__);
976 1.1 kiyohara #endif
977 1.1 kiyohara
978 1.1 kiyohara if (deframe_rd_ur(sc) == 0) {
979 1.1 kiyohara if (!deframe_isclear(&sc->sc_framestate) && size == 0 &&
980 1.1 kiyohara sc->sc_rd_expectdataticks == 0) {
981 1.1 kiyohara /*
982 1.1 kiyohara * Expected data, but didn't get it
983 1.1 kiyohara * within expected time...
984 1.1 kiyohara */
985 1.1 kiyohara DPRINTFN(5,("%s: incoming packet timeout\n",
986 1.1 kiyohara __func__));
987 1.1 kiyohara deframe_clear(&sc->sc_framestate);
988 1.1 kiyohara } else if (size > 0) {
989 1.1 kiyohara /*
990 1.1 kiyohara * If we also received actual data, reset the
991 1.1 kiyohara * data read timeout and wake up the possibly
992 1.1 kiyohara * sleeping thread...
993 1.1 kiyohara */
994 1.1 kiyohara sc->sc_rd_expectdataticks = 2;
995 1.1 kiyohara wakeup(&sc->sc_thread);
996 1.1 kiyohara }
997 1.1 kiyohara }
998 1.1 kiyohara
999 1.1 kiyohara /*
1000 1.1 kiyohara * Check if incoming data has stopped, or that we cannot
1001 1.1 kiyohara * safely read any more data. In the case of the latter we
1002 1.1 kiyohara * must switch to idle so that a write will not block...
1003 1.1 kiyohara */
1004 1.1 kiyohara if (sc->sc_direction == udir_input &&
1005 1.1 kiyohara ((size == 0 && sc->sc_rd_expectdataticks == 0) ||
1006 1.1 kiyohara UDSIR_BLOCK_RX_DATA(sc))) {
1007 1.1 kiyohara DPRINTFN(8, ("%s: idling on packet timeout, "
1008 1.1 kiyohara "complete frame, or no data\n", __func__));
1009 1.1 kiyohara sc->sc_direction = udir_idle;
1010 1.1 kiyohara
1011 1.1 kiyohara /* Wake up for possible output */
1012 1.1 kiyohara wakeup(&sc->sc_wr_buf);
1013 1.1 kiyohara selnotify(&sc->sc_wr_sel, 0, 0);
1014 1.1 kiyohara }
1015 1.1 kiyohara }
1016 1.1 kiyohara
1017 1.1 kiyohara static usbd_status
1018 1.1 kiyohara udsir_start_read(struct udsir_softc *sc)
1019 1.1 kiyohara {
1020 1.1 kiyohara usbd_status err;
1021 1.1 kiyohara
1022 1.1 kiyohara DPRINTFN(60, ("%s: sc=%p, size=%d\n", __func__, sc, sc->sc_rd_maxpsz));
1023 1.1 kiyohara
1024 1.1 kiyohara if (sc->sc_dying)
1025 1.1 kiyohara return USBD_IOERROR;
1026 1.1 kiyohara
1027 1.1 kiyohara if (UDSIR_BLOCK_RX_DATA(sc) || deframe_rd_ur(sc)) {
1028 1.1 kiyohara /*
1029 1.1 kiyohara * Can't start reading just yet. Since we aren't
1030 1.1 kiyohara * going to start a read, have to switch direction to
1031 1.1 kiyohara * idle.
1032 1.1 kiyohara */
1033 1.1 kiyohara sc->sc_direction = udir_idle;
1034 1.1 kiyohara return USBD_NORMAL_COMPLETION;
1035 1.1 kiyohara }
1036 1.1 kiyohara
1037 1.1 kiyohara /* Starting a read... */
1038 1.1 kiyohara sc->sc_rd_readinprogress = 1;
1039 1.1 kiyohara sc->sc_direction = udir_input;
1040 1.1 kiyohara
1041 1.1 kiyohara if (sc->sc_rd_err) {
1042 1.1 kiyohara sc->sc_rd_err = 0;
1043 1.1 kiyohara DPRINTFN(0, ("%s: clear stall\n", __func__));
1044 1.1 kiyohara usbd_clear_endpoint_stall(sc->sc_rd_pipe);
1045 1.1 kiyohara }
1046 1.1 kiyohara
1047 1.1 kiyohara usbd_setup_xfer(sc->sc_rd_xfer, sc->sc_rd_pipe, sc, sc->sc_rd_buf,
1048 1.1 kiyohara sc->sc_rd_maxpsz, USBD_SHORT_XFER_OK | USBD_NO_COPY,
1049 1.1 kiyohara USBD_NO_TIMEOUT, udsir_rd_cb);
1050 1.1 kiyohara err = usbd_transfer(sc->sc_rd_xfer);
1051 1.1 kiyohara if (err != USBD_IN_PROGRESS) {
1052 1.1 kiyohara DPRINTFN(0, ("%s: err=%d\n", __func__, (int)err));
1053 1.1 kiyohara return err;
1054 1.1 kiyohara }
1055 1.1 kiyohara return USBD_NORMAL_COMPLETION;
1056 1.1 kiyohara }
1057