udsir.c revision 1.3 1 1.3 msaitoh /* $NetBSD: udsir.c,v 1.3 2016/07/07 06:55:42 msaitoh 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.3 msaitoh __KERNEL_RCSID(0, "$NetBSD: udsir.c,v 1.3 2016/07/07 06:55:42 msaitoh 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.2 skrll #include <sys/kmem.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.2 skrll struct usbd_device *sc_udev;
71 1.2 skrll struct usbd_interface *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.2 skrll struct usbd_pipe *sc_rd_pipe;
81 1.2 skrll struct usbd_xfer *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.2 skrll struct usbd_xfer *sc_wr_xfer;
95 1.2 skrll struct usbd_pipe *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.2 skrll static void udsir_rd_cb(struct usbd_xfer *, void *, 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.2 skrll struct usbif_attach_arg *uiaa = aux;
162 1.1 kiyohara
163 1.1 kiyohara DPRINTFN(50, ("udsir_match\n"));
164 1.1 kiyohara
165 1.2 skrll if (uiaa->uiaa_vendor == USB_VENDOR_KINGSUN &&
166 1.2 skrll uiaa->uiaa_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.2 skrll struct usbif_attach_arg *uiaa = aux;
177 1.2 skrll struct usbd_device *dev = uiaa->uiaa_device;
178 1.2 skrll struct usbd_interface *iface = uiaa->uiaa_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.3 msaitoh usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
228 1.1 kiyohara
229 1.1 kiyohara ia.ia_type = IR_TYPE_IRFRAME;
230 1.1 kiyohara ia.ia_methods = &udsir_methods;
231 1.1 kiyohara ia.ia_handle = sc;
232 1.1 kiyohara
233 1.1 kiyohara sc->sc_child = config_found(self, &ia, ir_print);
234 1.1 kiyohara selinit(&sc->sc_rd_sel);
235 1.1 kiyohara selinit(&sc->sc_wr_sel);
236 1.1 kiyohara
237 1.1 kiyohara return;
238 1.1 kiyohara }
239 1.1 kiyohara
240 1.1 kiyohara static int
241 1.1 kiyohara udsir_detach(device_t self, int flags)
242 1.1 kiyohara {
243 1.1 kiyohara struct udsir_softc *sc = device_private(self);
244 1.1 kiyohara int s;
245 1.1 kiyohara int rv = 0;
246 1.1 kiyohara
247 1.1 kiyohara DPRINTFN(0, ("udsir_detach: sc=%p flags=%d\n", sc, flags));
248 1.1 kiyohara
249 1.1 kiyohara sc->sc_closing = sc->sc_dying = 1;
250 1.1 kiyohara
251 1.1 kiyohara wakeup(&sc->sc_thread);
252 1.1 kiyohara
253 1.1 kiyohara while (sc->sc_thread != NULL)
254 1.1 kiyohara tsleep(&sc->sc_closing, PWAIT, "usircl", 0);
255 1.1 kiyohara
256 1.1 kiyohara /* Abort all pipes. Causes processes waiting for transfer to wake. */
257 1.1 kiyohara if (sc->sc_rd_pipe != NULL) {
258 1.1 kiyohara usbd_abort_pipe(sc->sc_rd_pipe);
259 1.2 skrll }
260 1.2 skrll if (sc->sc_wr_pipe != NULL) {
261 1.2 skrll usbd_abort_pipe(sc->sc_wr_pipe);
262 1.2 skrll }
263 1.2 skrll if (sc->sc_rd_xfer != NULL) {
264 1.2 skrll usbd_destroy_xfer(sc->sc_rd_xfer);
265 1.2 skrll sc->sc_rd_xfer = NULL;
266 1.2 skrll sc->sc_rd_buf = NULL;
267 1.2 skrll }
268 1.2 skrll if (sc->sc_wr_xfer != NULL) {
269 1.2 skrll usbd_destroy_xfer(sc->sc_wr_xfer);
270 1.2 skrll sc->sc_wr_xfer = NULL;
271 1.2 skrll sc->sc_wr_buf = NULL;
272 1.2 skrll }
273 1.2 skrll /* Close pipes. */
274 1.2 skrll if (sc->sc_rd_pipe != NULL) {
275 1.1 kiyohara usbd_close_pipe(sc->sc_rd_pipe);
276 1.1 kiyohara sc->sc_rd_pipe = NULL;
277 1.1 kiyohara }
278 1.1 kiyohara if (sc->sc_wr_pipe != NULL) {
279 1.1 kiyohara usbd_close_pipe(sc->sc_wr_pipe);
280 1.1 kiyohara sc->sc_wr_pipe = NULL;
281 1.1 kiyohara }
282 1.1 kiyohara wakeup(&sc->sc_ur_framelen);
283 1.1 kiyohara wakeup(&sc->sc_wr_buf);
284 1.1 kiyohara
285 1.1 kiyohara s = splusb();
286 1.1 kiyohara if (--sc->sc_refcnt >= 0) {
287 1.1 kiyohara /* Wait for processes to go away. */
288 1.1 kiyohara usb_detach_waitold(sc->sc_dev);
289 1.1 kiyohara }
290 1.1 kiyohara splx(s);
291 1.1 kiyohara
292 1.1 kiyohara if (sc->sc_child != NULL)
293 1.1 kiyohara rv = config_detach(sc->sc_child, flags);
294 1.1 kiyohara
295 1.1 kiyohara usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
296 1.1 kiyohara
297 1.1 kiyohara seldestroy(&sc->sc_rd_sel);
298 1.1 kiyohara seldestroy(&sc->sc_wr_sel);
299 1.1 kiyohara
300 1.1 kiyohara return rv;
301 1.1 kiyohara }
302 1.1 kiyohara
303 1.1 kiyohara static void
304 1.1 kiyohara udsir_childdet(device_t self, device_t child)
305 1.1 kiyohara {
306 1.1 kiyohara struct udsir_softc *sc = device_private(self);
307 1.1 kiyohara
308 1.1 kiyohara KASSERT(sc->sc_child == child);
309 1.1 kiyohara sc->sc_child = NULL;
310 1.1 kiyohara }
311 1.1 kiyohara
312 1.1 kiyohara static int
313 1.1 kiyohara udsir_activate(device_t self, enum devact act)
314 1.1 kiyohara {
315 1.1 kiyohara struct udsir_softc *sc = device_private(self);
316 1.1 kiyohara
317 1.1 kiyohara switch (act) {
318 1.1 kiyohara case DVACT_DEACTIVATE:
319 1.1 kiyohara sc->sc_dying = 1;
320 1.1 kiyohara return 0;
321 1.1 kiyohara default:
322 1.1 kiyohara return EOPNOTSUPP;
323 1.1 kiyohara }
324 1.1 kiyohara }
325 1.1 kiyohara
326 1.1 kiyohara /* ARGSUSED */
327 1.1 kiyohara static int
328 1.1 kiyohara udsir_open(void *h, int flag, int mode, struct lwp *l)
329 1.1 kiyohara {
330 1.1 kiyohara struct udsir_softc *sc = h;
331 1.1 kiyohara int error;
332 1.1 kiyohara usbd_status err;
333 1.1 kiyohara
334 1.1 kiyohara DPRINTFN(0, ("%s: sc=%p\n", __func__, sc));
335 1.1 kiyohara
336 1.1 kiyohara err = usbd_open_pipe(sc->sc_iface, sc->sc_rd_addr, 0, &sc->sc_rd_pipe);
337 1.1 kiyohara if (err != USBD_NORMAL_COMPLETION) {
338 1.1 kiyohara error = EIO;
339 1.1 kiyohara goto bad1;
340 1.1 kiyohara }
341 1.1 kiyohara err = usbd_open_pipe(sc->sc_iface, sc->sc_wr_addr, 0, &sc->sc_wr_pipe);
342 1.1 kiyohara if (err != USBD_NORMAL_COMPLETION) {
343 1.1 kiyohara error = EIO;
344 1.1 kiyohara goto bad2;
345 1.1 kiyohara }
346 1.2 skrll error = usbd_create_xfer(sc->sc_rd_pipe, sc->sc_rd_maxpsz,
347 1.2 skrll USBD_SHORT_XFER_OK, 0, &sc->sc_rd_xfer);
348 1.2 skrll if (error)
349 1.2 skrll goto bad3;
350 1.2 skrll
351 1.2 skrll error = usbd_create_xfer(sc->sc_wr_pipe, IRDA_MAX_FRAME_SIZE,
352 1.2 skrll USBD_FORCE_SHORT_XFER, 0, &sc->sc_wr_xfer);
353 1.2 skrll if (error)
354 1.1 kiyohara goto bad4;
355 1.2 skrll
356 1.2 skrll sc->sc_rd_buf = usbd_get_buffer(sc->sc_rd_xfer);
357 1.2 skrll sc->sc_wr_buf = usbd_get_buffer(sc->sc_wr_xfer);
358 1.2 skrll
359 1.2 skrll sc->sc_ur_buf = kmem_alloc(IRDA_MAX_FRAME_SIZE, KM_SLEEP);
360 1.1 kiyohara if (sc->sc_ur_buf == NULL) {
361 1.1 kiyohara error = ENOMEM;
362 1.1 kiyohara goto bad5;
363 1.1 kiyohara }
364 1.1 kiyohara
365 1.1 kiyohara sc->sc_rd_index = sc->sc_rd_count = 0;
366 1.1 kiyohara sc->sc_closing = 0;
367 1.1 kiyohara sc->sc_rd_readinprogress = 0;
368 1.1 kiyohara sc->sc_rd_expectdataticks = 0;
369 1.1 kiyohara sc->sc_ur_framelen = 0;
370 1.1 kiyohara sc->sc_rd_err = 0;
371 1.1 kiyohara sc->sc_wr_stalewrite = 0;
372 1.1 kiyohara sc->sc_direction = udir_idle;
373 1.1 kiyohara sc->sc_params.speed = 0;
374 1.1 kiyohara sc->sc_params.ebofs = 0;
375 1.1 kiyohara sc->sc_params.maxsize = min(sc->sc_rd_maxpsz, sc->sc_wr_maxpsz);
376 1.1 kiyohara
377 1.1 kiyohara deframe_init(&sc->sc_framestate, sc->sc_ur_buf, IRDA_MAX_FRAME_SIZE);
378 1.1 kiyohara
379 1.1 kiyohara /* Increment reference for thread */
380 1.1 kiyohara sc->sc_refcnt++;
381 1.1 kiyohara
382 1.1 kiyohara error = kthread_create(PRI_NONE, 0, NULL, udsir_thread, sc,
383 1.1 kiyohara &sc->sc_thread, "%s", device_xname(sc->sc_dev));
384 1.1 kiyohara if (error) {
385 1.1 kiyohara sc->sc_refcnt--;
386 1.1 kiyohara goto bad5;
387 1.1 kiyohara }
388 1.1 kiyohara
389 1.1 kiyohara return 0;
390 1.1 kiyohara
391 1.1 kiyohara bad5:
392 1.2 skrll usbd_destroy_xfer(sc->sc_wr_xfer);
393 1.1 kiyohara sc->sc_wr_xfer = NULL;
394 1.1 kiyohara bad4:
395 1.2 skrll usbd_destroy_xfer(sc->sc_rd_xfer);
396 1.1 kiyohara sc->sc_rd_xfer = NULL;
397 1.1 kiyohara bad3:
398 1.1 kiyohara usbd_close_pipe(sc->sc_wr_pipe);
399 1.1 kiyohara sc->sc_wr_pipe = NULL;
400 1.1 kiyohara bad2:
401 1.1 kiyohara usbd_close_pipe(sc->sc_rd_pipe);
402 1.1 kiyohara sc->sc_rd_pipe = NULL;
403 1.1 kiyohara bad1:
404 1.1 kiyohara return error;
405 1.1 kiyohara }
406 1.1 kiyohara
407 1.1 kiyohara /* ARGSUSED */
408 1.1 kiyohara static int
409 1.1 kiyohara udsir_close(void *h, int flag, int mode, struct lwp *l)
410 1.1 kiyohara {
411 1.1 kiyohara struct udsir_softc *sc = h;
412 1.1 kiyohara
413 1.1 kiyohara DPRINTFN(0, ("%s: sc=%p\n", __func__, sc));
414 1.1 kiyohara
415 1.1 kiyohara sc->sc_refcnt++;
416 1.1 kiyohara
417 1.1 kiyohara sc->sc_rd_readinprogress = 1;
418 1.1 kiyohara sc->sc_closing = 1;
419 1.1 kiyohara
420 1.1 kiyohara wakeup(&sc->sc_thread);
421 1.1 kiyohara
422 1.1 kiyohara while (sc->sc_thread != NULL)
423 1.1 kiyohara tsleep(&sc->sc_closing, PWAIT, "usircl", 0);
424 1.1 kiyohara
425 1.1 kiyohara if (sc->sc_rd_pipe != NULL) {
426 1.1 kiyohara usbd_abort_pipe(sc->sc_rd_pipe);
427 1.1 kiyohara }
428 1.1 kiyohara if (sc->sc_wr_pipe != NULL) {
429 1.1 kiyohara usbd_abort_pipe(sc->sc_wr_pipe);
430 1.1 kiyohara }
431 1.1 kiyohara if (sc->sc_rd_xfer != NULL) {
432 1.2 skrll usbd_destroy_xfer(sc->sc_rd_xfer);
433 1.1 kiyohara sc->sc_rd_xfer = NULL;
434 1.1 kiyohara sc->sc_rd_buf = NULL;
435 1.1 kiyohara }
436 1.1 kiyohara if (sc->sc_wr_xfer != NULL) {
437 1.2 skrll usbd_destroy_xfer(sc->sc_wr_xfer);
438 1.1 kiyohara sc->sc_wr_xfer = NULL;
439 1.1 kiyohara sc->sc_wr_buf = NULL;
440 1.1 kiyohara }
441 1.2 skrll if (sc->sc_rd_pipe != NULL) {
442 1.2 skrll usbd_close_pipe(sc->sc_rd_pipe);
443 1.2 skrll sc->sc_rd_pipe = NULL;
444 1.2 skrll }
445 1.2 skrll if (sc->sc_wr_pipe != NULL) {
446 1.2 skrll usbd_close_pipe(sc->sc_wr_pipe);
447 1.2 skrll sc->sc_wr_pipe = NULL;
448 1.2 skrll }
449 1.1 kiyohara if (sc->sc_ur_buf != NULL) {
450 1.2 skrll kmem_free(sc->sc_ur_buf, IRDA_MAX_FRAME_SIZE);
451 1.1 kiyohara sc->sc_ur_buf = NULL;
452 1.1 kiyohara }
453 1.1 kiyohara
454 1.1 kiyohara if (--sc->sc_refcnt < 0)
455 1.1 kiyohara usb_detach_wakeupold(sc->sc_dev);
456 1.1 kiyohara
457 1.1 kiyohara return 0;
458 1.1 kiyohara }
459 1.1 kiyohara
460 1.1 kiyohara /* ARGSUSED */
461 1.1 kiyohara static int
462 1.1 kiyohara udsir_read(void *h, struct uio *uio, int flag)
463 1.1 kiyohara {
464 1.1 kiyohara struct udsir_softc *sc = h;
465 1.1 kiyohara int s;
466 1.1 kiyohara int error;
467 1.1 kiyohara u_int uframelen;
468 1.1 kiyohara
469 1.1 kiyohara DPRINTFN(1, ("%s: sc=%p\n", __func__, sc));
470 1.1 kiyohara
471 1.1 kiyohara if (sc->sc_dying)
472 1.1 kiyohara return EIO;
473 1.1 kiyohara
474 1.1 kiyohara #ifdef DIAGNOSTIC
475 1.1 kiyohara if (sc->sc_rd_buf == NULL)
476 1.1 kiyohara return EINVAL;
477 1.1 kiyohara #endif
478 1.1 kiyohara
479 1.1 kiyohara sc->sc_refcnt++;
480 1.1 kiyohara
481 1.1 kiyohara if (!sc->sc_rd_readinprogress && !UDSIR_BLOCK_RX_DATA(sc))
482 1.1 kiyohara /* Possibly wake up polling thread */
483 1.1 kiyohara wakeup(&sc->sc_thread);
484 1.1 kiyohara
485 1.1 kiyohara do {
486 1.1 kiyohara s = splusb();
487 1.1 kiyohara while (sc->sc_ur_framelen == 0) {
488 1.1 kiyohara DPRINTFN(5, ("%s: calling tsleep()\n", __func__));
489 1.1 kiyohara error = tsleep(&sc->sc_ur_framelen, PZERO | PCATCH,
490 1.1 kiyohara "usirrd", 0);
491 1.1 kiyohara if (sc->sc_dying)
492 1.1 kiyohara error = EIO;
493 1.1 kiyohara if (error) {
494 1.1 kiyohara splx(s);
495 1.1 kiyohara DPRINTFN(0, ("%s: tsleep() = %d\n",
496 1.1 kiyohara __func__, error));
497 1.1 kiyohara goto ret;
498 1.1 kiyohara }
499 1.1 kiyohara }
500 1.1 kiyohara splx(s);
501 1.1 kiyohara
502 1.1 kiyohara uframelen = sc->sc_ur_framelen;
503 1.1 kiyohara DPRINTFN(1, ("%s: sc=%p framelen=%u, hdr=0x%02x\n",
504 1.1 kiyohara __func__, sc, uframelen, sc->sc_ur_buf[0]));
505 1.1 kiyohara if (uframelen > uio->uio_resid)
506 1.1 kiyohara error = EINVAL;
507 1.1 kiyohara else
508 1.1 kiyohara error = uiomove(sc->sc_ur_buf, uframelen, uio);
509 1.1 kiyohara sc->sc_ur_framelen = 0;
510 1.1 kiyohara
511 1.1 kiyohara if (deframe_rd_ur(sc) == 0 && uframelen > 0) {
512 1.1 kiyohara /*
513 1.1 kiyohara * Need to wait for another read to obtain a
514 1.1 kiyohara * complete frame... If we also obtained
515 1.1 kiyohara * actual data, wake up the possibly sleeping
516 1.1 kiyohara * thread immediately...
517 1.1 kiyohara */
518 1.1 kiyohara wakeup(&sc->sc_thread);
519 1.1 kiyohara }
520 1.1 kiyohara } while (uframelen == 0);
521 1.1 kiyohara
522 1.1 kiyohara DPRINTFN(1, ("%s: return %d\n", __func__, error));
523 1.1 kiyohara
524 1.1 kiyohara ret:
525 1.1 kiyohara if (--sc->sc_refcnt < 0)
526 1.1 kiyohara usb_detach_wakeupold(sc->sc_dev);
527 1.1 kiyohara return error;
528 1.1 kiyohara }
529 1.1 kiyohara
530 1.1 kiyohara /* ARGSUSED */
531 1.1 kiyohara static int
532 1.1 kiyohara udsir_write(void *h, struct uio *uio, int flag)
533 1.1 kiyohara {
534 1.1 kiyohara struct udsir_softc *sc = h;
535 1.1 kiyohara usbd_status err;
536 1.1 kiyohara uint32_t wrlen;
537 1.1 kiyohara int error, sirlength;
538 1.1 kiyohara uint8_t *wrbuf;
539 1.1 kiyohara int s;
540 1.1 kiyohara
541 1.1 kiyohara DPRINTFN(1, ("%s: sc=%p\n", __func__, sc));
542 1.1 kiyohara
543 1.1 kiyohara if (sc->sc_dying)
544 1.1 kiyohara return EIO;
545 1.1 kiyohara
546 1.1 kiyohara #ifdef DIAGNOSTIC
547 1.1 kiyohara if (sc->sc_wr_buf == NULL)
548 1.1 kiyohara return EINVAL;
549 1.1 kiyohara #endif
550 1.1 kiyohara
551 1.1 kiyohara wrlen = uio->uio_resid;
552 1.1 kiyohara if (wrlen > sc->sc_wr_maxpsz)
553 1.1 kiyohara return EINVAL;
554 1.1 kiyohara
555 1.1 kiyohara sc->sc_refcnt++;
556 1.1 kiyohara
557 1.1 kiyohara if (!UDSIR_BLOCK_RX_DATA(sc)) {
558 1.1 kiyohara /*
559 1.1 kiyohara * If reads are not blocked, determine what action we
560 1.1 kiyohara * should potentially take...
561 1.1 kiyohara */
562 1.1 kiyohara if (sc->sc_direction == udir_output) {
563 1.1 kiyohara /*
564 1.1 kiyohara * If the last operation was an output, wait for the
565 1.1 kiyohara * polling thread to check for incoming data.
566 1.1 kiyohara */
567 1.1 kiyohara sc->sc_wr_stalewrite = 1;
568 1.1 kiyohara wakeup(&sc->sc_thread);
569 1.1 kiyohara } else if (!sc->sc_rd_readinprogress &&
570 1.1 kiyohara (sc->sc_direction == udir_idle ||
571 1.1 kiyohara sc->sc_direction == udir_input)) {
572 1.1 kiyohara /* If idle, check for input before outputting */
573 1.1 kiyohara udsir_start_read(sc);
574 1.1 kiyohara }
575 1.1 kiyohara }
576 1.1 kiyohara
577 1.1 kiyohara s = splusb();
578 1.1 kiyohara while (sc->sc_wr_stalewrite ||
579 1.1 kiyohara (sc->sc_direction != udir_output &&
580 1.1 kiyohara sc->sc_direction != udir_idle)) {
581 1.1 kiyohara DPRINTFN(5, ("%s: sc=%p stalewrite=%d direction=%d, "
582 1.1 kiyohara "calling tsleep()\n",
583 1.1 kiyohara __func__, sc, sc->sc_wr_stalewrite,
584 1.1 kiyohara sc->sc_direction));
585 1.1 kiyohara error = tsleep(&sc->sc_wr_buf, PZERO | PCATCH, "usirwr", 0);
586 1.1 kiyohara if (sc->sc_dying)
587 1.1 kiyohara error = EIO;
588 1.1 kiyohara if (error) {
589 1.1 kiyohara splx(s);
590 1.1 kiyohara DPRINTFN(0, ("%s: tsleep() = %d\n", __func__, error));
591 1.1 kiyohara goto ret;
592 1.1 kiyohara }
593 1.1 kiyohara }
594 1.1 kiyohara splx(s);
595 1.1 kiyohara
596 1.1 kiyohara wrbuf = sc->sc_wr_buf;
597 1.1 kiyohara
598 1.1 kiyohara sirlength = irda_sir_frame(wrbuf, MAX_UDSIR_OUTPUT_FRAME,
599 1.1 kiyohara uio, sc->sc_params.ebofs);
600 1.1 kiyohara if (sirlength < 0)
601 1.1 kiyohara error = -sirlength;
602 1.1 kiyohara else {
603 1.1 kiyohara uint32_t btlen;
604 1.1 kiyohara
605 1.1 kiyohara DPRINTFN(1, ("%s: transfer %u bytes\n",
606 1.1 kiyohara __func__, (unsigned int)wrlen));
607 1.1 kiyohara
608 1.1 kiyohara btlen = sirlength;
609 1.1 kiyohara
610 1.1 kiyohara sc->sc_direction = udir_output;
611 1.1 kiyohara
612 1.1 kiyohara #ifdef UDSIR_DEBUG
613 1.1 kiyohara if (udsirdebug >= 20)
614 1.1 kiyohara udsir_dumpdata(wrbuf, btlen, __func__);
615 1.1 kiyohara #endif
616 1.1 kiyohara
617 1.1 kiyohara err = usbd_intr_transfer(sc->sc_wr_xfer, sc->sc_wr_pipe,
618 1.2 skrll USBD_FORCE_SHORT_XFER, UDSIR_WR_TIMEOUT,
619 1.2 skrll wrbuf, &btlen);
620 1.1 kiyohara DPRINTFN(2, ("%s: err=%d\n", __func__, err));
621 1.1 kiyohara if (err != USBD_NORMAL_COMPLETION) {
622 1.1 kiyohara if (err == USBD_INTERRUPTED)
623 1.1 kiyohara error = EINTR;
624 1.1 kiyohara else if (err == USBD_TIMEOUT)
625 1.1 kiyohara error = ETIMEDOUT;
626 1.1 kiyohara else
627 1.1 kiyohara error = EIO;
628 1.1 kiyohara } else
629 1.1 kiyohara error = 0;
630 1.1 kiyohara }
631 1.1 kiyohara
632 1.1 kiyohara ret:
633 1.1 kiyohara if (--sc->sc_refcnt < 0)
634 1.1 kiyohara usb_detach_wakeupold(sc->sc_dev);
635 1.1 kiyohara
636 1.1 kiyohara DPRINTFN(1, ("%s: sc=%p done\n", __func__, sc));
637 1.1 kiyohara return error;
638 1.1 kiyohara }
639 1.1 kiyohara
640 1.1 kiyohara static int
641 1.1 kiyohara udsir_poll(void *h, int events, struct lwp *l)
642 1.1 kiyohara {
643 1.1 kiyohara struct udsir_softc *sc = h;
644 1.1 kiyohara int revents = 0;
645 1.1 kiyohara
646 1.1 kiyohara DPRINTFN(1, ("%s: sc=%p\n", __func__, sc));
647 1.1 kiyohara
648 1.1 kiyohara if (events & (POLLOUT | POLLWRNORM)) {
649 1.1 kiyohara if (sc->sc_direction != udir_input)
650 1.1 kiyohara revents |= events & (POLLOUT | POLLWRNORM);
651 1.1 kiyohara else {
652 1.1 kiyohara DPRINTFN(2, ("%s: recording write select\n", __func__));
653 1.1 kiyohara selrecord(l, &sc->sc_wr_sel);
654 1.1 kiyohara }
655 1.1 kiyohara }
656 1.1 kiyohara
657 1.1 kiyohara if (events & (POLLIN | POLLRDNORM)) {
658 1.1 kiyohara if (sc->sc_ur_framelen != 0) {
659 1.1 kiyohara DPRINTFN(2, ("%s: have data\n", __func__));
660 1.1 kiyohara revents |= events & (POLLIN | POLLRDNORM);
661 1.1 kiyohara } else {
662 1.1 kiyohara DPRINTFN(2, ("%s: recording read select\n", __func__));
663 1.1 kiyohara selrecord(l, &sc->sc_rd_sel);
664 1.1 kiyohara }
665 1.1 kiyohara }
666 1.1 kiyohara
667 1.1 kiyohara return revents;
668 1.1 kiyohara }
669 1.1 kiyohara
670 1.1 kiyohara static const struct filterops udsirread_filtops =
671 1.1 kiyohara { 1, NULL, filt_udsirrdetach, filt_udsirread };
672 1.1 kiyohara static const struct filterops udsirwrite_filtops =
673 1.1 kiyohara { 1, NULL, filt_udsirwdetach, filt_udsirwrite };
674 1.1 kiyohara
675 1.1 kiyohara static int
676 1.1 kiyohara udsir_kqfilter(void *h, struct knote *kn)
677 1.1 kiyohara {
678 1.1 kiyohara struct udsir_softc *sc = h;
679 1.1 kiyohara struct klist *klist;
680 1.1 kiyohara int s;
681 1.1 kiyohara
682 1.1 kiyohara switch (kn->kn_filter) {
683 1.1 kiyohara case EVFILT_READ:
684 1.1 kiyohara klist = &sc->sc_rd_sel.sel_klist;
685 1.1 kiyohara kn->kn_fop = &udsirread_filtops;
686 1.1 kiyohara break;
687 1.1 kiyohara case EVFILT_WRITE:
688 1.1 kiyohara klist = &sc->sc_wr_sel.sel_klist;
689 1.1 kiyohara kn->kn_fop = &udsirwrite_filtops;
690 1.1 kiyohara break;
691 1.1 kiyohara default:
692 1.1 kiyohara return (EINVAL);
693 1.1 kiyohara }
694 1.1 kiyohara
695 1.1 kiyohara kn->kn_hook = sc;
696 1.1 kiyohara
697 1.1 kiyohara s = splusb();
698 1.1 kiyohara SLIST_INSERT_HEAD(klist, kn, kn_selnext);
699 1.1 kiyohara splx(s);
700 1.1 kiyohara
701 1.1 kiyohara return (0);
702 1.1 kiyohara }
703 1.1 kiyohara
704 1.1 kiyohara static int
705 1.1 kiyohara udsir_set_params(void *h, struct irda_params *p)
706 1.1 kiyohara {
707 1.1 kiyohara struct udsir_softc *sc = h;
708 1.1 kiyohara
709 1.1 kiyohara DPRINTFN(0, ("%s: sc=%p, speed=%d ebofs=%d maxsize=%d\n",
710 1.1 kiyohara __func__, sc, p->speed, p->ebofs, p->maxsize));
711 1.1 kiyohara
712 1.1 kiyohara if (sc->sc_dying)
713 1.1 kiyohara return EIO;
714 1.1 kiyohara
715 1.1 kiyohara if (p->speed != 9600)
716 1.1 kiyohara return EINVAL;
717 1.1 kiyohara
718 1.1 kiyohara if (p->maxsize != sc->sc_params.maxsize) {
719 1.1 kiyohara if (p->maxsize > min(sc->sc_rd_maxpsz, sc->sc_wr_maxpsz))
720 1.1 kiyohara return EINVAL;
721 1.1 kiyohara sc->sc_params.maxsize = p->maxsize;
722 1.1 kiyohara }
723 1.1 kiyohara
724 1.1 kiyohara sc->sc_params = *p;
725 1.1 kiyohara
726 1.1 kiyohara return 0;
727 1.1 kiyohara }
728 1.1 kiyohara
729 1.1 kiyohara static int
730 1.1 kiyohara udsir_get_speeds(void *h, int *speeds)
731 1.1 kiyohara {
732 1.1 kiyohara struct udsir_softc *sc = h;
733 1.1 kiyohara
734 1.1 kiyohara DPRINTFN(0, ("%s: sc=%p\n", __func__, sc));
735 1.1 kiyohara
736 1.1 kiyohara if (sc->sc_dying)
737 1.1 kiyohara return EIO;
738 1.1 kiyohara
739 1.1 kiyohara /* Support only 9600bps now. */
740 1.1 kiyohara *speeds = IRDA_SPEED_9600;
741 1.1 kiyohara
742 1.1 kiyohara return 0;
743 1.1 kiyohara }
744 1.1 kiyohara
745 1.1 kiyohara static int
746 1.1 kiyohara udsir_get_turnarounds(void *h, int *turnarounds)
747 1.1 kiyohara {
748 1.1 kiyohara struct udsir_softc *sc = h;
749 1.1 kiyohara
750 1.1 kiyohara DPRINTFN(0, ("%s: sc=%p\n", __func__, sc));
751 1.1 kiyohara
752 1.1 kiyohara if (sc->sc_dying)
753 1.1 kiyohara return EIO;
754 1.1 kiyohara
755 1.1 kiyohara /*
756 1.1 kiyohara * Documentation is on the light side with respect to
757 1.1 kiyohara * turnaround time for this device.
758 1.1 kiyohara */
759 1.1 kiyohara *turnarounds = IRDA_TURNT_10000;
760 1.1 kiyohara
761 1.1 kiyohara return 0;
762 1.1 kiyohara }
763 1.1 kiyohara
764 1.1 kiyohara static void
765 1.1 kiyohara filt_udsirrdetach(struct knote *kn)
766 1.1 kiyohara {
767 1.1 kiyohara struct udsir_softc *sc = kn->kn_hook;
768 1.1 kiyohara int s;
769 1.1 kiyohara
770 1.1 kiyohara s = splusb();
771 1.1 kiyohara SLIST_REMOVE(&sc->sc_rd_sel.sel_klist, kn, knote, kn_selnext);
772 1.1 kiyohara splx(s);
773 1.1 kiyohara }
774 1.1 kiyohara
775 1.1 kiyohara /* ARGSUSED */
776 1.1 kiyohara static int
777 1.1 kiyohara filt_udsirread(struct knote *kn, long hint)
778 1.1 kiyohara {
779 1.1 kiyohara struct udsir_softc *sc = kn->kn_hook;
780 1.1 kiyohara
781 1.1 kiyohara kn->kn_data = sc->sc_ur_framelen;
782 1.1 kiyohara return (kn->kn_data > 0);
783 1.1 kiyohara }
784 1.1 kiyohara
785 1.1 kiyohara static void
786 1.1 kiyohara filt_udsirwdetach(struct knote *kn)
787 1.1 kiyohara {
788 1.1 kiyohara struct udsir_softc *sc = kn->kn_hook;
789 1.1 kiyohara int s;
790 1.1 kiyohara
791 1.1 kiyohara s = splusb();
792 1.1 kiyohara SLIST_REMOVE(&sc->sc_wr_sel.sel_klist, kn, knote, kn_selnext);
793 1.1 kiyohara splx(s);
794 1.1 kiyohara }
795 1.1 kiyohara
796 1.1 kiyohara /* ARGSUSED */
797 1.1 kiyohara static int
798 1.1 kiyohara filt_udsirwrite(struct knote *kn, long hint)
799 1.1 kiyohara {
800 1.1 kiyohara struct udsir_softc *sc = kn->kn_hook;
801 1.1 kiyohara
802 1.1 kiyohara kn->kn_data = 0;
803 1.1 kiyohara return (sc->sc_direction != udir_input);
804 1.1 kiyohara }
805 1.1 kiyohara
806 1.1 kiyohara
807 1.1 kiyohara static void
808 1.1 kiyohara udsir_thread(void *arg)
809 1.1 kiyohara {
810 1.1 kiyohara struct udsir_softc *sc = arg;
811 1.1 kiyohara int error;
812 1.1 kiyohara
813 1.1 kiyohara DPRINTFN(20, ("%s: starting polling thread\n", __func__));
814 1.1 kiyohara
815 1.1 kiyohara while (!sc->sc_closing) {
816 1.1 kiyohara if (!sc->sc_rd_readinprogress && !UDSIR_BLOCK_RX_DATA(sc))
817 1.1 kiyohara udsir_periodic(sc);
818 1.1 kiyohara
819 1.1 kiyohara if (!sc->sc_closing) {
820 1.1 kiyohara error = tsleep(&sc->sc_thread, PWAIT, "udsir", hz / 10);
821 1.1 kiyohara if (error == EWOULDBLOCK &&
822 1.1 kiyohara sc->sc_rd_expectdataticks > 0)
823 1.1 kiyohara /*
824 1.1 kiyohara * After a timeout decrement the tick
825 1.1 kiyohara * counter within which time we expect
826 1.1 kiyohara * data to arrive if we are receiving
827 1.1 kiyohara * data...
828 1.1 kiyohara */
829 1.1 kiyohara sc->sc_rd_expectdataticks--;
830 1.1 kiyohara }
831 1.1 kiyohara }
832 1.1 kiyohara
833 1.1 kiyohara DPRINTFN(20, ("%s: exiting polling thread\n", __func__));
834 1.1 kiyohara
835 1.1 kiyohara sc->sc_thread = NULL;
836 1.1 kiyohara
837 1.1 kiyohara wakeup(&sc->sc_closing);
838 1.1 kiyohara
839 1.1 kiyohara if (--sc->sc_refcnt < 0)
840 1.1 kiyohara usb_detach_wakeupold(sc->sc_dev);
841 1.1 kiyohara
842 1.1 kiyohara kthread_exit(0);
843 1.1 kiyohara }
844 1.1 kiyohara
845 1.1 kiyohara #ifdef UDSIR_DEBUG
846 1.1 kiyohara static void
847 1.1 kiyohara udsir_dumpdata(uint8_t const *data, size_t dlen, char const *desc)
848 1.1 kiyohara {
849 1.1 kiyohara size_t bdindex;
850 1.1 kiyohara
851 1.1 kiyohara printf("%s: (%lx)", desc, (unsigned long)dlen);
852 1.1 kiyohara for (bdindex = 0; bdindex < dlen; bdindex++)
853 1.1 kiyohara printf(" %02x", (unsigned int)data[bdindex]);
854 1.1 kiyohara printf("\n");
855 1.1 kiyohara }
856 1.1 kiyohara #endif
857 1.1 kiyohara
858 1.1 kiyohara /* Returns 0 if more data required, 1 if a complete frame was extracted */
859 1.1 kiyohara static int
860 1.1 kiyohara deframe_rd_ur(struct udsir_softc *sc)
861 1.1 kiyohara {
862 1.1 kiyohara
863 1.1 kiyohara if (sc->sc_rd_index == 0) {
864 1.1 kiyohara KASSERT(sc->sc_rd_count == sc->sc_rd_maxpsz);
865 1.1 kiyohara /* valid count */
866 1.1 kiyohara sc->sc_rd_count = sc->sc_rd_buf[sc->sc_rd_index++] + 1;
867 1.1 kiyohara KASSERT(sc->sc_rd_count < sc->sc_rd_maxpsz);
868 1.1 kiyohara }
869 1.1 kiyohara
870 1.1 kiyohara while (sc->sc_rd_index < sc->sc_rd_count) {
871 1.1 kiyohara uint8_t const *buf;
872 1.1 kiyohara size_t buflen;
873 1.1 kiyohara enum frameresult fresult;
874 1.1 kiyohara
875 1.1 kiyohara buf = &sc->sc_rd_buf[sc->sc_rd_index];
876 1.1 kiyohara buflen = sc->sc_rd_count - sc->sc_rd_index;
877 1.1 kiyohara
878 1.1 kiyohara fresult = deframe_process(&sc->sc_framestate, &buf, &buflen);
879 1.1 kiyohara
880 1.1 kiyohara sc->sc_rd_index = sc->sc_rd_count - buflen;
881 1.1 kiyohara
882 1.1 kiyohara DPRINTFN(1,("%s: result=%d\n", __func__, (int)fresult));
883 1.1 kiyohara
884 1.1 kiyohara switch (fresult) {
885 1.1 kiyohara case FR_IDLE:
886 1.1 kiyohara case FR_INPROGRESS:
887 1.1 kiyohara case FR_FRAMEBADFCS:
888 1.1 kiyohara case FR_FRAMEMALFORMED:
889 1.1 kiyohara case FR_BUFFEROVERRUN:
890 1.1 kiyohara break;
891 1.1 kiyohara case FR_FRAMEOK:
892 1.1 kiyohara sc->sc_ur_framelen = sc->sc_framestate.bufindex;
893 1.1 kiyohara wakeup(&sc->sc_ur_framelen); /* XXX should use flag */
894 1.1 kiyohara selnotify(&sc->sc_rd_sel, 0, 0);
895 1.1 kiyohara return 1;
896 1.1 kiyohara }
897 1.1 kiyohara }
898 1.1 kiyohara
899 1.1 kiyohara /* Reset indices into USB-side buffer */
900 1.1 kiyohara sc->sc_rd_index = sc->sc_rd_count = 0;
901 1.1 kiyohara
902 1.1 kiyohara return 0;
903 1.1 kiyohara }
904 1.1 kiyohara
905 1.1 kiyohara /*
906 1.1 kiyohara * Direction transitions:
907 1.1 kiyohara *
908 1.1 kiyohara * udsir_periodic() can switch the direction from:
909 1.1 kiyohara *
910 1.1 kiyohara * output -> idle
911 1.1 kiyohara * output -> stalled
912 1.1 kiyohara * stalled -> idle
913 1.1 kiyohara * idle -> input
914 1.1 kiyohara *
915 1.1 kiyohara * udsir_rd_cb() can switch the direction from:
916 1.1 kiyohara *
917 1.1 kiyohara * input -> stalled
918 1.1 kiyohara * input -> idle
919 1.1 kiyohara *
920 1.1 kiyohara * udsir_write() can switch the direction from:
921 1.1 kiyohara *
922 1.1 kiyohara * idle -> output
923 1.1 kiyohara */
924 1.1 kiyohara static void
925 1.1 kiyohara udsir_periodic(struct udsir_softc *sc)
926 1.1 kiyohara {
927 1.1 kiyohara
928 1.1 kiyohara DPRINTFN(60, ("%s: direction = %d\n", __func__, sc->sc_direction));
929 1.1 kiyohara
930 1.1 kiyohara if (sc->sc_wr_stalewrite && sc->sc_direction == udir_idle) {
931 1.1 kiyohara /*
932 1.1 kiyohara * In a stale write case, we need to check if the
933 1.1 kiyohara * write has completed. Once that has happened, the
934 1.1 kiyohara * write is no longer stale.
935 1.1 kiyohara *
936 1.1 kiyohara * But note that we may immediately start a read poll...
937 1.1 kiyohara */
938 1.1 kiyohara sc->sc_wr_stalewrite = 0;
939 1.1 kiyohara wakeup(&sc->sc_wr_buf);
940 1.1 kiyohara }
941 1.1 kiyohara
942 1.1 kiyohara if (!sc->sc_rd_readinprogress &&
943 1.1 kiyohara (sc->sc_direction == udir_idle ||
944 1.1 kiyohara sc->sc_direction == udir_input))
945 1.1 kiyohara /* Do a read poll if appropriate... */
946 1.1 kiyohara udsir_start_read(sc);
947 1.1 kiyohara }
948 1.1 kiyohara
949 1.1 kiyohara static void
950 1.2 skrll udsir_rd_cb(struct usbd_xfer *xfer, void * priv, usbd_status status)
951 1.1 kiyohara {
952 1.1 kiyohara struct udsir_softc *sc = priv;
953 1.1 kiyohara uint32_t size;
954 1.1 kiyohara
955 1.1 kiyohara DPRINTFN(60, ("%s: sc=%p\n", __func__, sc));
956 1.1 kiyohara
957 1.1 kiyohara /* Read is no longer in progress */
958 1.1 kiyohara sc->sc_rd_readinprogress = 0;
959 1.1 kiyohara
960 1.1 kiyohara if (status == USBD_CANCELLED || sc->sc_closing) /* this is normal */
961 1.1 kiyohara return;
962 1.1 kiyohara if (status) {
963 1.1 kiyohara size = 0;
964 1.1 kiyohara sc->sc_rd_err = 1;
965 1.1 kiyohara
966 1.1 kiyohara if (sc->sc_direction == udir_input ||
967 1.1 kiyohara sc->sc_direction == udir_idle) {
968 1.1 kiyohara /*
969 1.1 kiyohara * Receive error, probably need to clear error
970 1.1 kiyohara * condition.
971 1.1 kiyohara */
972 1.1 kiyohara sc->sc_direction = udir_stalled;
973 1.1 kiyohara }
974 1.1 kiyohara } else
975 1.1 kiyohara usbd_get_xfer_status(xfer, NULL, NULL, &size, NULL);
976 1.1 kiyohara
977 1.1 kiyohara sc->sc_rd_index = 0;
978 1.1 kiyohara sc->sc_rd_count = size;
979 1.1 kiyohara
980 1.1 kiyohara DPRINTFN(((size > 0 || sc->sc_rd_err != 0) ? 20 : 60),
981 1.1 kiyohara ("%s: sc=%p size=%u, err=%d\n",
982 1.1 kiyohara __func__, sc, size, sc->sc_rd_err));
983 1.1 kiyohara
984 1.1 kiyohara #ifdef UDSIR_DEBUG
985 1.1 kiyohara if (udsirdebug >= 20 && size > 0)
986 1.1 kiyohara udsir_dumpdata(sc->sc_rd_buf, size, __func__);
987 1.1 kiyohara #endif
988 1.1 kiyohara
989 1.1 kiyohara if (deframe_rd_ur(sc) == 0) {
990 1.1 kiyohara if (!deframe_isclear(&sc->sc_framestate) && size == 0 &&
991 1.1 kiyohara sc->sc_rd_expectdataticks == 0) {
992 1.1 kiyohara /*
993 1.1 kiyohara * Expected data, but didn't get it
994 1.1 kiyohara * within expected time...
995 1.1 kiyohara */
996 1.1 kiyohara DPRINTFN(5,("%s: incoming packet timeout\n",
997 1.1 kiyohara __func__));
998 1.1 kiyohara deframe_clear(&sc->sc_framestate);
999 1.1 kiyohara } else if (size > 0) {
1000 1.1 kiyohara /*
1001 1.1 kiyohara * If we also received actual data, reset the
1002 1.1 kiyohara * data read timeout and wake up the possibly
1003 1.1 kiyohara * sleeping thread...
1004 1.1 kiyohara */
1005 1.1 kiyohara sc->sc_rd_expectdataticks = 2;
1006 1.1 kiyohara wakeup(&sc->sc_thread);
1007 1.1 kiyohara }
1008 1.1 kiyohara }
1009 1.1 kiyohara
1010 1.1 kiyohara /*
1011 1.1 kiyohara * Check if incoming data has stopped, or that we cannot
1012 1.1 kiyohara * safely read any more data. In the case of the latter we
1013 1.1 kiyohara * must switch to idle so that a write will not block...
1014 1.1 kiyohara */
1015 1.1 kiyohara if (sc->sc_direction == udir_input &&
1016 1.1 kiyohara ((size == 0 && sc->sc_rd_expectdataticks == 0) ||
1017 1.1 kiyohara UDSIR_BLOCK_RX_DATA(sc))) {
1018 1.1 kiyohara DPRINTFN(8, ("%s: idling on packet timeout, "
1019 1.1 kiyohara "complete frame, or no data\n", __func__));
1020 1.1 kiyohara sc->sc_direction = udir_idle;
1021 1.1 kiyohara
1022 1.1 kiyohara /* Wake up for possible output */
1023 1.1 kiyohara wakeup(&sc->sc_wr_buf);
1024 1.1 kiyohara selnotify(&sc->sc_wr_sel, 0, 0);
1025 1.1 kiyohara }
1026 1.1 kiyohara }
1027 1.1 kiyohara
1028 1.1 kiyohara static usbd_status
1029 1.1 kiyohara udsir_start_read(struct udsir_softc *sc)
1030 1.1 kiyohara {
1031 1.1 kiyohara usbd_status err;
1032 1.1 kiyohara
1033 1.1 kiyohara DPRINTFN(60, ("%s: sc=%p, size=%d\n", __func__, sc, sc->sc_rd_maxpsz));
1034 1.1 kiyohara
1035 1.1 kiyohara if (sc->sc_dying)
1036 1.1 kiyohara return USBD_IOERROR;
1037 1.1 kiyohara
1038 1.1 kiyohara if (UDSIR_BLOCK_RX_DATA(sc) || deframe_rd_ur(sc)) {
1039 1.1 kiyohara /*
1040 1.1 kiyohara * Can't start reading just yet. Since we aren't
1041 1.1 kiyohara * going to start a read, have to switch direction to
1042 1.1 kiyohara * idle.
1043 1.1 kiyohara */
1044 1.1 kiyohara sc->sc_direction = udir_idle;
1045 1.1 kiyohara return USBD_NORMAL_COMPLETION;
1046 1.1 kiyohara }
1047 1.1 kiyohara
1048 1.1 kiyohara /* Starting a read... */
1049 1.1 kiyohara sc->sc_rd_readinprogress = 1;
1050 1.1 kiyohara sc->sc_direction = udir_input;
1051 1.1 kiyohara
1052 1.1 kiyohara if (sc->sc_rd_err) {
1053 1.1 kiyohara sc->sc_rd_err = 0;
1054 1.1 kiyohara DPRINTFN(0, ("%s: clear stall\n", __func__));
1055 1.1 kiyohara usbd_clear_endpoint_stall(sc->sc_rd_pipe);
1056 1.1 kiyohara }
1057 1.1 kiyohara
1058 1.2 skrll usbd_setup_xfer(sc->sc_rd_xfer, sc, sc->sc_rd_buf, sc->sc_rd_maxpsz,
1059 1.2 skrll USBD_SHORT_XFER_OK, USBD_NO_TIMEOUT, udsir_rd_cb);
1060 1.1 kiyohara err = usbd_transfer(sc->sc_rd_xfer);
1061 1.1 kiyohara if (err != USBD_IN_PROGRESS) {
1062 1.1 kiyohara DPRINTFN(0, ("%s: err=%d\n", __func__, (int)err));
1063 1.1 kiyohara return err;
1064 1.1 kiyohara }
1065 1.1 kiyohara return USBD_NORMAL_COMPLETION;
1066 1.1 kiyohara }
1067