ustir.c revision 1.1.4.7 1 1.1.4.7 thorpej /* $NetBSD: ustir.c,v 1.1.4.7 2002/12/29 20:49:34 thorpej Exp $ */
2 1.1.4.2 nathanw
3 1.1.4.2 nathanw /*
4 1.1.4.2 nathanw * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 1.1.4.2 nathanw * All rights reserved.
6 1.1.4.2 nathanw *
7 1.1.4.2 nathanw * This code is derived from software contributed to The NetBSD Foundation
8 1.1.4.2 nathanw * by David Sainty <David.Sainty (at) dtsp.co.nz>
9 1.1.4.2 nathanw *
10 1.1.4.2 nathanw * Redistribution and use in source and binary forms, with or without
11 1.1.4.2 nathanw * modification, are permitted provided that the following conditions
12 1.1.4.2 nathanw * are met:
13 1.1.4.2 nathanw * 1. Redistributions of source code must retain the above copyright
14 1.1.4.2 nathanw * notice, this list of conditions and the following disclaimer.
15 1.1.4.2 nathanw * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.4.2 nathanw * notice, this list of conditions and the following disclaimer in the
17 1.1.4.2 nathanw * documentation and/or other materials provided with the distribution.
18 1.1.4.2 nathanw * 3. All advertising materials mentioning features or use of this software
19 1.1.4.2 nathanw * must display the following acknowledgement:
20 1.1.4.2 nathanw * This product includes software developed by the NetBSD
21 1.1.4.2 nathanw * Foundation, Inc. and its contributors.
22 1.1.4.2 nathanw * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1.4.2 nathanw * contributors may be used to endorse or promote products derived
24 1.1.4.2 nathanw * from this software without specific prior written permission.
25 1.1.4.2 nathanw *
26 1.1.4.2 nathanw * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1.4.2 nathanw * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1.4.2 nathanw * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1.4.2 nathanw * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1.4.2 nathanw * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1.4.2 nathanw * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1.4.2 nathanw * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1.4.2 nathanw * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1.4.2 nathanw * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1.4.2 nathanw * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1.4.2 nathanw * POSSIBILITY OF SUCH DAMAGE.
37 1.1.4.2 nathanw */
38 1.1.4.2 nathanw
39 1.1.4.2 nathanw #include <sys/cdefs.h>
40 1.1.4.7 thorpej __KERNEL_RCSID(0, "$NetBSD: ustir.c,v 1.1.4.7 2002/12/29 20:49:34 thorpej Exp $");
41 1.1.4.2 nathanw
42 1.1.4.2 nathanw #include <sys/param.h>
43 1.1.4.2 nathanw #include <sys/systm.h>
44 1.1.4.2 nathanw #include <sys/kernel.h>
45 1.1.4.2 nathanw #include <sys/device.h>
46 1.1.4.2 nathanw #include <sys/malloc.h>
47 1.1.4.2 nathanw #include <sys/conf.h>
48 1.1.4.2 nathanw #include <sys/file.h>
49 1.1.4.2 nathanw #include <sys/poll.h>
50 1.1.4.2 nathanw #include <sys/select.h>
51 1.1.4.2 nathanw #include <sys/proc.h>
52 1.1.4.2 nathanw #include <sys/kthread.h>
53 1.1.4.2 nathanw
54 1.1.4.2 nathanw #ifdef USTIR_DEBUG_IOCTLS
55 1.1.4.2 nathanw #include <sys/ioctl.h>
56 1.1.4.2 nathanw #include <dev/usb/ustir.h>
57 1.1.4.2 nathanw #endif
58 1.1.4.2 nathanw
59 1.1.4.2 nathanw #include <dev/usb/usb.h>
60 1.1.4.2 nathanw #include <dev/usb/usbdevs.h>
61 1.1.4.2 nathanw #include <dev/usb/usbdi.h>
62 1.1.4.2 nathanw #include <dev/usb/usbdi_util.h>
63 1.1.4.2 nathanw #include <dev/usb/ustirreg.h>
64 1.1.4.2 nathanw
65 1.1.4.2 nathanw #include <dev/ir/ir.h>
66 1.1.4.2 nathanw #include <dev/ir/irdaio.h>
67 1.1.4.2 nathanw #include <dev/ir/irframevar.h>
68 1.1.4.2 nathanw #include <dev/ir/sir.h>
69 1.1.4.2 nathanw
70 1.1.4.2 nathanw #ifdef USTIR_DEBUG
71 1.1.4.2 nathanw #define DPRINTFN(n,x) if (ustirdebug>(n)) logprintf x
72 1.1.4.2 nathanw int ustirdebug = 0;
73 1.1.4.2 nathanw #else
74 1.1.4.2 nathanw #define DPRINTFN(n,x)
75 1.1.4.2 nathanw #endif
76 1.1.4.2 nathanw
77 1.1.4.2 nathanw /* Max size with framing. */
78 1.1.4.2 nathanw #define MAX_USTIR_OUTPUT_FRAME (2*IRDA_MAX_FRAME_SIZE + IRDA_MAX_EBOFS + STIR_OUTPUT_HEADER_SIZE + 4)
79 1.1.4.2 nathanw
80 1.1.4.2 nathanw #define USTIR_NSPEEDS 9
81 1.1.4.2 nathanw struct ustir_speedrec {
82 1.1.4.2 nathanw unsigned int speed;
83 1.1.4.2 nathanw unsigned int config;
84 1.1.4.2 nathanw };
85 1.1.4.2 nathanw
86 1.1.4.2 nathanw Static struct ustir_speedrec const ustir_speeds[USTIR_NSPEEDS] = {
87 1.1.4.2 nathanw { 4000000, STIR_BRMODE_4000000 },
88 1.1.4.2 nathanw { 1152000, STIR_BRMODE_1152000 },
89 1.1.4.2 nathanw { 576000, STIR_BRMODE_576000 },
90 1.1.4.2 nathanw { 115200, STIR_BRMODE_115200 },
91 1.1.4.2 nathanw { 57600, STIR_BRMODE_57600 },
92 1.1.4.2 nathanw { 38400, STIR_BRMODE_38400 },
93 1.1.4.2 nathanw { 19200, STIR_BRMODE_19200 },
94 1.1.4.2 nathanw { 9600, STIR_BRMODE_9600 },
95 1.1.4.2 nathanw { 2400, STIR_BRMODE_2400 }
96 1.1.4.2 nathanw };
97 1.1.4.2 nathanw
98 1.1.4.2 nathanw struct framedefn {
99 1.1.4.2 nathanw unsigned int bof_count;
100 1.1.4.2 nathanw u_int8_t bof_byte;
101 1.1.4.2 nathanw
102 1.1.4.2 nathanw u_int8_t esc_byte;
103 1.1.4.2 nathanw u_int8_t esc_xor;
104 1.1.4.2 nathanw
105 1.1.4.2 nathanw unsigned int eof_count;
106 1.1.4.2 nathanw u_int8_t eof_byte;
107 1.1.4.2 nathanw
108 1.1.4.2 nathanw unsigned int fcs_count;
109 1.1.4.2 nathanw u_int32_t fcs_init;
110 1.1.4.2 nathanw u_int32_t fcs_correct;
111 1.1.4.2 nathanw
112 1.1.4.2 nathanw u_int32_t (*fcs_calc)(u_int32_t, u_int8_t const*, size_t);
113 1.1.4.2 nathanw };
114 1.1.4.2 nathanw
115 1.1.4.2 nathanw Static u_int32_t crc_ccitt_16(u_int32_t, u_int8_t const*, size_t);
116 1.1.4.2 nathanw
117 1.1.4.2 nathanw struct framedefn const framedef_sir = {
118 1.1.4.2 nathanw 1, 0xc0,
119 1.1.4.2 nathanw 0x7d, 0x20,
120 1.1.4.2 nathanw 1, 0xc1,
121 1.1.4.2 nathanw 2, INITFCS, GOODFCS,
122 1.1.4.2 nathanw crc_ccitt_16
123 1.1.4.2 nathanw };
124 1.1.4.2 nathanw
125 1.1.4.2 nathanw enum framefsmstate {
126 1.1.4.2 nathanw FSTATE_END_OF_FRAME,
127 1.1.4.2 nathanw FSTATE_START_OF_FRAME,
128 1.1.4.2 nathanw FSTATE_IN_DATA,
129 1.1.4.2 nathanw FSTATE_IN_END
130 1.1.4.2 nathanw };
131 1.1.4.2 nathanw
132 1.1.4.2 nathanw enum frameresult {
133 1.1.4.2 nathanw FR_IDLE,
134 1.1.4.2 nathanw FR_INPROGRESS,
135 1.1.4.2 nathanw FR_FRAMEOK,
136 1.1.4.2 nathanw FR_FRAMEBADFCS,
137 1.1.4.2 nathanw FR_FRAMEMALFORMED,
138 1.1.4.2 nathanw FR_BUFFEROVERRUN
139 1.1.4.2 nathanw };
140 1.1.4.2 nathanw
141 1.1.4.2 nathanw struct framestate {
142 1.1.4.2 nathanw struct framedefn const *definition;
143 1.1.4.2 nathanw
144 1.1.4.2 nathanw u_int8_t *buffer;
145 1.1.4.2 nathanw size_t buflen;
146 1.1.4.2 nathanw size_t bufindex;
147 1.1.4.2 nathanw
148 1.1.4.2 nathanw enum framefsmstate fsmstate;
149 1.1.4.2 nathanw u_int escaped;
150 1.1.4.2 nathanw u_int state_index;
151 1.1.4.2 nathanw };
152 1.1.4.2 nathanw
153 1.1.4.2 nathanw #define deframe_isclear(fs) ((fs)->fsmstate == FSTATE_END_OF_FRAME)
154 1.1.4.2 nathanw
155 1.1.4.3 nathanw Static void deframe_clear(struct framestate *);
156 1.1.4.3 nathanw Static void deframe_init(struct framestate *, struct framedefn const *,
157 1.1.4.3 nathanw u_int8_t *, size_t);
158 1.1.4.3 nathanw Static enum frameresult deframe_process(struct framestate *, u_int8_t const **,
159 1.1.4.3 nathanw size_t *);
160 1.1.4.2 nathanw
161 1.1.4.2 nathanw struct ustir_softc {
162 1.1.4.2 nathanw USBBASEDEVICE sc_dev;
163 1.1.4.2 nathanw usbd_device_handle sc_udev;
164 1.1.4.2 nathanw usbd_interface_handle sc_iface;
165 1.1.4.2 nathanw
166 1.1.4.2 nathanw u_int8_t *sc_ur_buf; /* Unencapsulated frame */
167 1.1.4.2 nathanw u_int sc_ur_framelen;
168 1.1.4.2 nathanw
169 1.1.4.2 nathanw u_int8_t *sc_rd_buf; /* Raw incoming data stream */
170 1.1.4.2 nathanw size_t sc_rd_index;
171 1.1.4.2 nathanw int sc_rd_addr;
172 1.1.4.2 nathanw usbd_pipe_handle sc_rd_pipe;
173 1.1.4.2 nathanw usbd_xfer_handle sc_rd_xfer;
174 1.1.4.2 nathanw u_int sc_rd_count;
175 1.1.4.2 nathanw int sc_rd_readinprogress;
176 1.1.4.2 nathanw u_int sc_rd_expectdataticks;
177 1.1.4.2 nathanw u_char sc_rd_err;
178 1.1.4.2 nathanw struct framestate sc_framestate;
179 1.1.4.2 nathanw struct proc *sc_thread;
180 1.1.4.2 nathanw struct selinfo sc_rd_sel;
181 1.1.4.2 nathanw
182 1.1.4.2 nathanw u_int8_t *sc_wr_buf;
183 1.1.4.2 nathanw int sc_wr_addr;
184 1.1.4.7 thorpej int sc_wr_stalewrite;
185 1.1.4.2 nathanw usbd_xfer_handle sc_wr_xfer;
186 1.1.4.2 nathanw usbd_pipe_handle sc_wr_pipe;
187 1.1.4.2 nathanw struct selinfo sc_wr_sel;
188 1.1.4.2 nathanw
189 1.1.4.2 nathanw enum {
190 1.1.4.2 nathanw udir_input, /* Receiving data */
191 1.1.4.2 nathanw udir_output, /* Transmitting data */
192 1.1.4.2 nathanw udir_stalled, /* Error preventing data flow */
193 1.1.4.2 nathanw udir_idle /* Neither receiving nor transmitting */
194 1.1.4.2 nathanw } sc_direction;
195 1.1.4.2 nathanw
196 1.1.4.2 nathanw struct ustir_speedrec const *sc_speedrec;
197 1.1.4.2 nathanw
198 1.1.4.2 nathanw struct device *sc_child;
199 1.1.4.2 nathanw struct irda_params sc_params;
200 1.1.4.2 nathanw
201 1.1.4.2 nathanw int sc_refcnt;
202 1.1.4.2 nathanw char sc_closing;
203 1.1.4.2 nathanw char sc_dying;
204 1.1.4.2 nathanw };
205 1.1.4.2 nathanw
206 1.1.4.2 nathanw /* True if we cannot safely read data from the device */
207 1.1.4.2 nathanw #define USTIR_BLOCK_RX_DATA(sc) ((sc)->sc_ur_framelen != 0)
208 1.1.4.2 nathanw
209 1.1.4.2 nathanw #define USTIR_WR_TIMEOUT 200
210 1.1.4.2 nathanw
211 1.1.4.2 nathanw Static int ustir_activate(device_ptr_t self, enum devact act);
212 1.1.4.2 nathanw Static int ustir_open(void *h, int flag, int mode, usb_proc_ptr p);
213 1.1.4.2 nathanw Static int ustir_close(void *h, int flag, int mode, usb_proc_ptr p);
214 1.1.4.2 nathanw Static int ustir_read(void *h, struct uio *uio, int flag);
215 1.1.4.2 nathanw Static int ustir_write(void *h, struct uio *uio, int flag);
216 1.1.4.2 nathanw Static int ustir_set_params(void *h, struct irda_params *params);
217 1.1.4.2 nathanw Static int ustir_get_speeds(void *h, int *speeds);
218 1.1.4.2 nathanw Static int ustir_get_turnarounds(void *h, int *times);
219 1.1.4.2 nathanw Static int ustir_poll(void *h, int events, usb_proc_ptr p);
220 1.1.4.5 nathanw Static int ustir_kqfilter(void *h, struct knote *kn);
221 1.1.4.2 nathanw
222 1.1.4.2 nathanw #ifdef USTIR_DEBUG_IOCTLS
223 1.1.4.2 nathanw Static int ustir_ioctl(void *h, u_long cmd, caddr_t addr, int flag, usb_proc_ptr p);
224 1.1.4.2 nathanw #endif
225 1.1.4.2 nathanw
226 1.1.4.2 nathanw Static struct irframe_methods const ustir_methods = {
227 1.1.4.2 nathanw ustir_open, ustir_close, ustir_read, ustir_write, ustir_poll,
228 1.1.4.5 nathanw ustir_kqfilter, ustir_set_params, ustir_get_speeds,
229 1.1.4.5 nathanw ustir_get_turnarounds,
230 1.1.4.2 nathanw #ifdef USTIR_DEBUG_IOCTLS
231 1.1.4.2 nathanw ustir_ioctl
232 1.1.4.2 nathanw #endif
233 1.1.4.2 nathanw };
234 1.1.4.2 nathanw
235 1.1.4.2 nathanw Static void ustir_rd_cb(usbd_xfer_handle, usbd_private_handle, usbd_status);
236 1.1.4.3 nathanw Static usbd_status ustir_start_read(struct ustir_softc *);
237 1.1.4.3 nathanw Static void ustir_periodic(struct ustir_softc *);
238 1.1.4.3 nathanw Static void ustir_thread(void *);
239 1.1.4.2 nathanw
240 1.1.4.2 nathanw Static u_int32_t
241 1.1.4.2 nathanw crc_ccitt_16(u_int32_t crcinit, u_int8_t const *buf, size_t blen)
242 1.1.4.2 nathanw {
243 1.1.4.2 nathanw while (blen-- > 0) {
244 1.1.4.2 nathanw u_int8_t chr;
245 1.1.4.2 nathanw chr = *buf++;
246 1.1.4.2 nathanw crcinit = updateFCS(crcinit, chr);
247 1.1.4.2 nathanw }
248 1.1.4.2 nathanw return crcinit;
249 1.1.4.2 nathanw }
250 1.1.4.2 nathanw
251 1.1.4.2 nathanw static usbd_status
252 1.1.4.2 nathanw ustir_read_reg(struct ustir_softc *sc, unsigned int reg, u_int8_t *data)
253 1.1.4.2 nathanw {
254 1.1.4.2 nathanw usb_device_request_t req;
255 1.1.4.2 nathanw
256 1.1.4.2 nathanw req.bmRequestType = UT_READ_VENDOR_DEVICE;
257 1.1.4.2 nathanw req.bRequest = STIR_CMD_READMULTIREG;
258 1.1.4.2 nathanw USETW(req.wValue, 0);
259 1.1.4.2 nathanw USETW(req.wIndex, reg);
260 1.1.4.2 nathanw USETW(req.wLength, 1);
261 1.1.4.2 nathanw
262 1.1.4.2 nathanw return usbd_do_request(sc->sc_udev, &req, data);
263 1.1.4.2 nathanw }
264 1.1.4.2 nathanw
265 1.1.4.2 nathanw static usbd_status
266 1.1.4.2 nathanw ustir_write_reg(struct ustir_softc *sc, unsigned int reg, u_int8_t data)
267 1.1.4.2 nathanw {
268 1.1.4.2 nathanw usb_device_request_t req;
269 1.1.4.2 nathanw
270 1.1.4.2 nathanw req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
271 1.1.4.2 nathanw req.bRequest = STIR_CMD_WRITESINGLEREG;
272 1.1.4.2 nathanw USETW(req.wValue, data);
273 1.1.4.2 nathanw USETW(req.wIndex, reg);
274 1.1.4.2 nathanw USETW(req.wLength, 0);
275 1.1.4.2 nathanw
276 1.1.4.2 nathanw return usbd_do_request(sc->sc_udev, &req, NULL);
277 1.1.4.2 nathanw }
278 1.1.4.2 nathanw
279 1.1.4.2 nathanw #ifdef USTIR_DEBUG
280 1.1.4.2 nathanw static void
281 1.1.4.7 thorpej ustir_dumpdata(u_int8_t const *data, size_t dlen, char const *desc)
282 1.1.4.2 nathanw {
283 1.1.4.2 nathanw size_t bdindex;
284 1.1.4.2 nathanw printf("%s: (%lx)", desc, (unsigned long)dlen);
285 1.1.4.2 nathanw for (bdindex = 0; bdindex < dlen; bdindex++)
286 1.1.4.7 thorpej printf(" %02x", (unsigned int)data[bdindex]);
287 1.1.4.2 nathanw printf("\n");
288 1.1.4.2 nathanw }
289 1.1.4.2 nathanw #endif
290 1.1.4.2 nathanw
291 1.1.4.2 nathanw USB_DECLARE_DRIVER(ustir);
292 1.1.4.2 nathanw
293 1.1.4.2 nathanw USB_MATCH(ustir)
294 1.1.4.2 nathanw {
295 1.1.4.2 nathanw USB_MATCH_START(ustir, uaa);
296 1.1.4.2 nathanw
297 1.1.4.2 nathanw DPRINTFN(50,("ustir_match\n"));
298 1.1.4.2 nathanw
299 1.1.4.2 nathanw if (uaa->iface == NULL)
300 1.1.4.2 nathanw return UMATCH_NONE;
301 1.1.4.2 nathanw
302 1.1.4.2 nathanw if (uaa->vendor == USB_VENDOR_SIGMATEL &&
303 1.1.4.2 nathanw uaa->product == USB_PRODUCT_SIGMATEL_IRDA)
304 1.1.4.2 nathanw return UMATCH_VENDOR_PRODUCT;
305 1.1.4.2 nathanw
306 1.1.4.2 nathanw return UMATCH_NONE;
307 1.1.4.2 nathanw }
308 1.1.4.2 nathanw
309 1.1.4.2 nathanw USB_ATTACH(ustir)
310 1.1.4.2 nathanw {
311 1.1.4.2 nathanw USB_ATTACH_START(ustir, sc, uaa);
312 1.1.4.2 nathanw usbd_device_handle dev = uaa->device;
313 1.1.4.2 nathanw usbd_interface_handle iface = uaa->iface;
314 1.1.4.2 nathanw char devinfo[1024];
315 1.1.4.2 nathanw usb_endpoint_descriptor_t *ed;
316 1.1.4.2 nathanw u_int8_t epcount;
317 1.1.4.2 nathanw int i;
318 1.1.4.2 nathanw struct ir_attach_args ia;
319 1.1.4.2 nathanw
320 1.1.4.2 nathanw DPRINTFN(10,("ustir_attach: sc=%p\n", sc));
321 1.1.4.2 nathanw
322 1.1.4.2 nathanw usbd_devinfo(dev, 0, devinfo);
323 1.1.4.2 nathanw USB_ATTACH_SETUP;
324 1.1.4.2 nathanw printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
325 1.1.4.2 nathanw
326 1.1.4.2 nathanw sc->sc_udev = dev;
327 1.1.4.2 nathanw sc->sc_iface = iface;
328 1.1.4.2 nathanw
329 1.1.4.2 nathanw epcount = 0;
330 1.1.4.2 nathanw (void)usbd_endpoint_count(iface, &epcount);
331 1.1.4.2 nathanw
332 1.1.4.2 nathanw sc->sc_rd_addr = -1;
333 1.1.4.2 nathanw sc->sc_wr_addr = -1;
334 1.1.4.2 nathanw for (i = 0; i < epcount; i++) {
335 1.1.4.2 nathanw ed = usbd_interface2endpoint_descriptor(iface, i);
336 1.1.4.2 nathanw if (ed == NULL) {
337 1.1.4.2 nathanw printf("%s: couldn't get ep %d\n",
338 1.1.4.2 nathanw USBDEVNAME(sc->sc_dev), i);
339 1.1.4.2 nathanw USB_ATTACH_ERROR_RETURN;
340 1.1.4.2 nathanw }
341 1.1.4.2 nathanw if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
342 1.1.4.2 nathanw UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
343 1.1.4.2 nathanw sc->sc_rd_addr = ed->bEndpointAddress;
344 1.1.4.2 nathanw } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
345 1.1.4.2 nathanw UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
346 1.1.4.2 nathanw sc->sc_wr_addr = ed->bEndpointAddress;
347 1.1.4.2 nathanw }
348 1.1.4.2 nathanw }
349 1.1.4.2 nathanw if (sc->sc_rd_addr == -1 || sc->sc_wr_addr == -1) {
350 1.1.4.2 nathanw printf("%s: missing endpoint\n", USBDEVNAME(sc->sc_dev));
351 1.1.4.2 nathanw USB_ATTACH_ERROR_RETURN;
352 1.1.4.2 nathanw }
353 1.1.4.2 nathanw
354 1.1.4.2 nathanw DPRINTFN(10, ("ustir_attach: %p\n", sc->sc_udev));
355 1.1.4.2 nathanw
356 1.1.4.2 nathanw usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
357 1.1.4.2 nathanw USBDEV(sc->sc_dev));
358 1.1.4.2 nathanw
359 1.1.4.2 nathanw ia.ia_type = IR_TYPE_IRFRAME;
360 1.1.4.2 nathanw ia.ia_methods = &ustir_methods;
361 1.1.4.2 nathanw ia.ia_handle = sc;
362 1.1.4.2 nathanw
363 1.1.4.2 nathanw sc->sc_child = config_found(self, &ia, ir_print);
364 1.1.4.2 nathanw
365 1.1.4.2 nathanw USB_ATTACH_SUCCESS_RETURN;
366 1.1.4.2 nathanw }
367 1.1.4.2 nathanw
368 1.1.4.2 nathanw USB_DETACH(ustir)
369 1.1.4.2 nathanw {
370 1.1.4.2 nathanw USB_DETACH_START(ustir, sc);
371 1.1.4.2 nathanw int s;
372 1.1.4.2 nathanw int rv = 0;
373 1.1.4.2 nathanw
374 1.1.4.2 nathanw DPRINTFN(0, ("ustir_detach: sc=%p flags=%d\n", sc, flags));
375 1.1.4.2 nathanw
376 1.1.4.2 nathanw sc->sc_closing = sc->sc_dying = 1;
377 1.1.4.2 nathanw
378 1.1.4.2 nathanw wakeup(&sc->sc_thread);
379 1.1.4.2 nathanw
380 1.1.4.2 nathanw while (sc->sc_thread != NULL)
381 1.1.4.2 nathanw tsleep(&sc->sc_closing, PWAIT, "usircl", 0);
382 1.1.4.2 nathanw
383 1.1.4.2 nathanw /* Abort all pipes. Causes processes waiting for transfer to wake. */
384 1.1.4.2 nathanw if (sc->sc_rd_pipe != NULL) {
385 1.1.4.2 nathanw usbd_abort_pipe(sc->sc_rd_pipe);
386 1.1.4.2 nathanw usbd_close_pipe(sc->sc_rd_pipe);
387 1.1.4.2 nathanw sc->sc_rd_pipe = NULL;
388 1.1.4.2 nathanw }
389 1.1.4.2 nathanw if (sc->sc_wr_pipe != NULL) {
390 1.1.4.2 nathanw usbd_abort_pipe(sc->sc_wr_pipe);
391 1.1.4.2 nathanw usbd_close_pipe(sc->sc_wr_pipe);
392 1.1.4.2 nathanw sc->sc_wr_pipe = NULL;
393 1.1.4.2 nathanw }
394 1.1.4.2 nathanw wakeup(&sc->sc_ur_framelen);
395 1.1.4.2 nathanw wakeup(&sc->sc_wr_buf);
396 1.1.4.2 nathanw
397 1.1.4.2 nathanw s = splusb();
398 1.1.4.2 nathanw if (--sc->sc_refcnt >= 0) {
399 1.1.4.2 nathanw /* Wait for processes to go away. */
400 1.1.4.2 nathanw usb_detach_wait(USBDEV(sc->sc_dev));
401 1.1.4.2 nathanw }
402 1.1.4.2 nathanw splx(s);
403 1.1.4.2 nathanw
404 1.1.4.2 nathanw if (sc->sc_child != NULL) {
405 1.1.4.2 nathanw rv = config_detach(sc->sc_child, flags);
406 1.1.4.2 nathanw sc->sc_child = NULL;
407 1.1.4.2 nathanw }
408 1.1.4.2 nathanw
409 1.1.4.2 nathanw usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
410 1.1.4.2 nathanw USBDEV(sc->sc_dev));
411 1.1.4.2 nathanw
412 1.1.4.2 nathanw return rv;
413 1.1.4.2 nathanw }
414 1.1.4.2 nathanw
415 1.1.4.2 nathanw Static void
416 1.1.4.2 nathanw deframe_clear(struct framestate *fstate)
417 1.1.4.2 nathanw {
418 1.1.4.2 nathanw fstate->bufindex = 0;
419 1.1.4.2 nathanw fstate->fsmstate = FSTATE_END_OF_FRAME;
420 1.1.4.2 nathanw fstate->escaped = 0;
421 1.1.4.2 nathanw }
422 1.1.4.2 nathanw
423 1.1.4.2 nathanw Static void
424 1.1.4.2 nathanw deframe_init(struct framestate *fstate, struct framedefn const *definition,
425 1.1.4.2 nathanw u_int8_t *buf, size_t buflen)
426 1.1.4.2 nathanw {
427 1.1.4.2 nathanw fstate->definition = definition;
428 1.1.4.2 nathanw fstate->buffer = buf;
429 1.1.4.2 nathanw fstate->buflen = buflen;
430 1.1.4.2 nathanw
431 1.1.4.2 nathanw deframe_clear(fstate);
432 1.1.4.2 nathanw }
433 1.1.4.2 nathanw
434 1.1.4.2 nathanw Static enum frameresult
435 1.1.4.2 nathanw deframe_process(struct framestate *fstate, u_int8_t const **bptr, size_t *blen)
436 1.1.4.2 nathanw {
437 1.1.4.2 nathanw struct framedefn const *definition;
438 1.1.4.2 nathanw u_int8_t const *cptr;
439 1.1.4.2 nathanw u_int8_t escchr;
440 1.1.4.2 nathanw size_t ibuflen, obufindex, obuflen;
441 1.1.4.2 nathanw enum framefsmstate fsmstate;
442 1.1.4.2 nathanw enum frameresult result;
443 1.1.4.2 nathanw
444 1.1.4.2 nathanw cptr = *bptr;
445 1.1.4.2 nathanw fsmstate = fstate->fsmstate;
446 1.1.4.2 nathanw definition = fstate->definition;
447 1.1.4.2 nathanw escchr = definition->esc_byte;
448 1.1.4.2 nathanw obufindex = fstate->bufindex;
449 1.1.4.2 nathanw obuflen = fstate->buflen;
450 1.1.4.2 nathanw ibuflen = *blen;
451 1.1.4.2 nathanw
452 1.1.4.2 nathanw while (ibuflen-- > 0) {
453 1.1.4.2 nathanw u_int8_t chr;
454 1.1.4.2 nathanw
455 1.1.4.2 nathanw chr = *cptr++;
456 1.1.4.2 nathanw
457 1.1.4.2 nathanw if (fstate->escaped) {
458 1.1.4.2 nathanw fstate->escaped = 0;
459 1.1.4.2 nathanw chr ^= definition->esc_xor;
460 1.1.4.2 nathanw } else if (chr == escchr) {
461 1.1.4.2 nathanw fstate->escaped = 1;
462 1.1.4.2 nathanw continue;
463 1.1.4.2 nathanw }
464 1.1.4.2 nathanw
465 1.1.4.2 nathanw switch (fsmstate) {
466 1.1.4.2 nathanw case FSTATE_IN_DATA:
467 1.1.4.2 nathanw if (chr == definition->eof_byte) {
468 1.1.4.2 nathanw fsmstate = FSTATE_IN_END;
469 1.1.4.2 nathanw fstate->state_index = definition->eof_count;
470 1.1.4.2 nathanw goto state_in_end;
471 1.1.4.2 nathanw }
472 1.1.4.2 nathanw if (obufindex >= obuflen) {
473 1.1.4.2 nathanw result = FR_BUFFEROVERRUN;
474 1.1.4.2 nathanw fsmstate = FSTATE_END_OF_FRAME;
475 1.1.4.2 nathanw goto complete;
476 1.1.4.2 nathanw }
477 1.1.4.2 nathanw fstate->buffer[obufindex++] = chr;
478 1.1.4.2 nathanw break;
479 1.1.4.2 nathanw
480 1.1.4.2 nathanw state_in_end:
481 1.1.4.5 nathanw /* FALLTHROUGH */
482 1.1.4.5 nathanw
483 1.1.4.2 nathanw case FSTATE_IN_END:
484 1.1.4.2 nathanw if (--fstate->state_index == 0) {
485 1.1.4.2 nathanw u_int32_t crc;
486 1.1.4.2 nathanw size_t fcslen;
487 1.1.4.2 nathanw
488 1.1.4.2 nathanw fsmstate = FSTATE_END_OF_FRAME;
489 1.1.4.2 nathanw
490 1.1.4.2 nathanw fcslen = definition->fcs_count;
491 1.1.4.2 nathanw
492 1.1.4.2 nathanw if (obufindex < fcslen) {
493 1.1.4.2 nathanw result = FR_FRAMEMALFORMED;
494 1.1.4.2 nathanw goto complete;
495 1.1.4.2 nathanw }
496 1.1.4.2 nathanw
497 1.1.4.2 nathanw crc = definition->
498 1.1.4.2 nathanw fcs_calc(definition->fcs_init,
499 1.1.4.2 nathanw fstate->buffer, obufindex);
500 1.1.4.2 nathanw
501 1.1.4.2 nathanw /* Remove check bytes from buffer length */
502 1.1.4.2 nathanw obufindex -= fcslen;
503 1.1.4.2 nathanw
504 1.1.4.2 nathanw if (crc == definition->fcs_correct)
505 1.1.4.2 nathanw result = FR_FRAMEOK;
506 1.1.4.2 nathanw else
507 1.1.4.2 nathanw result = FR_FRAMEBADFCS;
508 1.1.4.2 nathanw
509 1.1.4.2 nathanw goto complete;
510 1.1.4.2 nathanw }
511 1.1.4.2 nathanw break;
512 1.1.4.2 nathanw
513 1.1.4.2 nathanw case FSTATE_END_OF_FRAME:
514 1.1.4.2 nathanw if (chr != definition->bof_byte)
515 1.1.4.2 nathanw break;
516 1.1.4.2 nathanw
517 1.1.4.2 nathanw fsmstate = FSTATE_START_OF_FRAME;
518 1.1.4.2 nathanw fstate->state_index = definition->bof_count;
519 1.1.4.2 nathanw /* FALLTHROUGH */
520 1.1.4.2 nathanw case FSTATE_START_OF_FRAME:
521 1.1.4.2 nathanw if (--fstate->state_index == 0) {
522 1.1.4.2 nathanw fsmstate = FSTATE_IN_DATA;
523 1.1.4.2 nathanw obufindex = 0;
524 1.1.4.2 nathanw }
525 1.1.4.2 nathanw break;
526 1.1.4.2 nathanw }
527 1.1.4.2 nathanw }
528 1.1.4.2 nathanw
529 1.1.4.2 nathanw result = (fsmstate == FSTATE_END_OF_FRAME) ? FR_IDLE : FR_INPROGRESS;
530 1.1.4.2 nathanw
531 1.1.4.2 nathanw complete:
532 1.1.4.2 nathanw fstate->bufindex = obufindex;
533 1.1.4.2 nathanw fstate->fsmstate = fsmstate;
534 1.1.4.2 nathanw *blen = ibuflen;
535 1.1.4.2 nathanw
536 1.1.4.2 nathanw return result;
537 1.1.4.2 nathanw }
538 1.1.4.2 nathanw
539 1.1.4.2 nathanw /* Returns 0 if more data required, 1 if a complete frame was extracted */
540 1.1.4.2 nathanw static int
541 1.1.4.2 nathanw deframe_rd_ur(struct ustir_softc *sc)
542 1.1.4.2 nathanw {
543 1.1.4.2 nathanw while (sc->sc_rd_index < sc->sc_rd_count) {
544 1.1.4.2 nathanw u_int8_t const *buf;
545 1.1.4.2 nathanw size_t buflen;
546 1.1.4.2 nathanw enum frameresult fresult;
547 1.1.4.2 nathanw
548 1.1.4.2 nathanw buf = &sc->sc_rd_buf[sc->sc_rd_index];
549 1.1.4.2 nathanw buflen = sc->sc_rd_count - sc->sc_rd_index;
550 1.1.4.2 nathanw
551 1.1.4.2 nathanw fresult = deframe_process(&sc->sc_framestate, &buf, &buflen);
552 1.1.4.2 nathanw
553 1.1.4.2 nathanw sc->sc_rd_index = sc->sc_rd_count - buflen;
554 1.1.4.2 nathanw
555 1.1.4.4 nathanw DPRINTFN(1,("%s: result=%d\n", __func__, (int)fresult));
556 1.1.4.2 nathanw
557 1.1.4.2 nathanw switch (fresult) {
558 1.1.4.2 nathanw case FR_IDLE:
559 1.1.4.2 nathanw case FR_INPROGRESS:
560 1.1.4.2 nathanw case FR_FRAMEBADFCS:
561 1.1.4.2 nathanw case FR_FRAMEMALFORMED:
562 1.1.4.2 nathanw case FR_BUFFEROVERRUN:
563 1.1.4.2 nathanw break;
564 1.1.4.2 nathanw case FR_FRAMEOK:
565 1.1.4.2 nathanw sc->sc_ur_framelen = sc->sc_framestate.bufindex;
566 1.1.4.2 nathanw wakeup(&sc->sc_ur_framelen); /* XXX should use flag */
567 1.1.4.5 nathanw selnotify(&sc->sc_rd_sel, 0);
568 1.1.4.2 nathanw return 1;
569 1.1.4.2 nathanw }
570 1.1.4.2 nathanw }
571 1.1.4.2 nathanw
572 1.1.4.2 nathanw /* Reset indices into USB-side buffer */
573 1.1.4.2 nathanw sc->sc_rd_index = sc->sc_rd_count = 0;
574 1.1.4.2 nathanw
575 1.1.4.2 nathanw return 0;
576 1.1.4.2 nathanw }
577 1.1.4.2 nathanw
578 1.1.4.2 nathanw /*
579 1.1.4.2 nathanw * Direction transitions:
580 1.1.4.2 nathanw *
581 1.1.4.2 nathanw * ustir_periodic() can switch the direction from:
582 1.1.4.2 nathanw *
583 1.1.4.2 nathanw * output -> idle
584 1.1.4.2 nathanw * output -> stalled
585 1.1.4.2 nathanw * stalled -> idle
586 1.1.4.2 nathanw * idle -> input
587 1.1.4.2 nathanw *
588 1.1.4.2 nathanw * ustir_rd_cb() can switch the direction from:
589 1.1.4.2 nathanw *
590 1.1.4.2 nathanw * input -> stalled
591 1.1.4.2 nathanw * input -> idle
592 1.1.4.2 nathanw *
593 1.1.4.2 nathanw * ustir_write() can switch the direction from:
594 1.1.4.2 nathanw *
595 1.1.4.2 nathanw * idle -> output
596 1.1.4.2 nathanw */
597 1.1.4.2 nathanw Static void
598 1.1.4.2 nathanw ustir_periodic(struct ustir_softc *sc)
599 1.1.4.2 nathanw {
600 1.1.4.2 nathanw DPRINTFN(60, ("%s: direction = %d\n",
601 1.1.4.4 nathanw __func__, sc->sc_direction));
602 1.1.4.2 nathanw
603 1.1.4.2 nathanw if (sc->sc_direction == udir_output ||
604 1.1.4.2 nathanw sc->sc_direction == udir_stalled) {
605 1.1.4.2 nathanw usbd_status err;
606 1.1.4.2 nathanw u_int8_t regval;
607 1.1.4.2 nathanw
608 1.1.4.2 nathanw DPRINTFN(60, ("%s: reading status register\n",
609 1.1.4.4 nathanw __func__));
610 1.1.4.2 nathanw
611 1.1.4.2 nathanw err = ustir_read_reg(sc, STIR_REG_STATUS,
612 1.1.4.2 nathanw ®val);
613 1.1.4.7 thorpej if (err != USBD_NORMAL_COMPLETION) {
614 1.1.4.2 nathanw printf("%s: status register read failed: %s\n",
615 1.1.4.2 nathanw USBDEVNAME(sc->sc_dev),
616 1.1.4.2 nathanw usbd_errstr(err));
617 1.1.4.2 nathanw } else {
618 1.1.4.2 nathanw DPRINTFN(10, ("%s: status register = 0x%x\n",
619 1.1.4.4 nathanw __func__,
620 1.1.4.2 nathanw (unsigned int)regval));
621 1.1.4.2 nathanw if (sc->sc_direction == udir_output &&
622 1.1.4.2 nathanw !(regval & STIR_RSTATUS_FFDIR))
623 1.1.4.2 nathanw /* Output has completed */
624 1.1.4.2 nathanw sc->sc_direction = udir_idle;
625 1.1.4.2 nathanw if (regval & STIR_RSTATUS_FFOVER) {
626 1.1.4.2 nathanw /*
627 1.1.4.2 nathanw * On an overrun the FIFO hangs, and
628 1.1.4.2 nathanw * any data bulk transfers will stall.
629 1.1.4.2 nathanw * Reset the FIFO.
630 1.1.4.2 nathanw */
631 1.1.4.2 nathanw sc->sc_direction = udir_stalled;
632 1.1.4.2 nathanw
633 1.1.4.2 nathanw DPRINTFN(10, ("%s: clearing FIFO error\n",
634 1.1.4.4 nathanw __func__));
635 1.1.4.2 nathanw
636 1.1.4.2 nathanw err = ustir_write_reg(sc, STIR_REG_STATUS,
637 1.1.4.2 nathanw STIR_RSTATUS_FFCLR);
638 1.1.4.2 nathanw /* XXX if we fail partway through
639 1.1.4.2 nathanw * this, we may not recover? */
640 1.1.4.7 thorpej if (err == USBD_NORMAL_COMPLETION)
641 1.1.4.2 nathanw err = ustir_write_reg(sc,
642 1.1.4.2 nathanw STIR_REG_STATUS,
643 1.1.4.2 nathanw 0);
644 1.1.4.7 thorpej if (err != USBD_NORMAL_COMPLETION) {
645 1.1.4.2 nathanw printf("%s: FIFO reset failed: %s\n",
646 1.1.4.2 nathanw USBDEVNAME(sc->sc_dev),
647 1.1.4.2 nathanw usbd_errstr(err));
648 1.1.4.2 nathanw } else {
649 1.1.4.2 nathanw /* FIFO reset */
650 1.1.4.2 nathanw sc->sc_direction = udir_idle;
651 1.1.4.2 nathanw }
652 1.1.4.2 nathanw }
653 1.1.4.2 nathanw }
654 1.1.4.2 nathanw }
655 1.1.4.2 nathanw
656 1.1.4.7 thorpej if (sc->sc_wr_stalewrite && sc->sc_direction == udir_idle) {
657 1.1.4.7 thorpej /*
658 1.1.4.7 thorpej * In a stale write case, we need to check if the
659 1.1.4.7 thorpej * write has completed. Once that has happened, the
660 1.1.4.7 thorpej * write is no longer stale.
661 1.1.4.7 thorpej *
662 1.1.4.7 thorpej * But note that we may immediately start a read poll...
663 1.1.4.7 thorpej */
664 1.1.4.7 thorpej sc->sc_wr_stalewrite = 0;
665 1.1.4.7 thorpej wakeup(&sc->sc_wr_buf);
666 1.1.4.7 thorpej }
667 1.1.4.7 thorpej
668 1.1.4.2 nathanw if (!sc->sc_rd_readinprogress &&
669 1.1.4.2 nathanw (sc->sc_direction == udir_idle ||
670 1.1.4.2 nathanw sc->sc_direction == udir_input))
671 1.1.4.2 nathanw /* Do a read poll if appropriate... */
672 1.1.4.2 nathanw ustir_start_read(sc);
673 1.1.4.2 nathanw }
674 1.1.4.2 nathanw
675 1.1.4.2 nathanw Static void
676 1.1.4.2 nathanw ustir_thread(void *arg)
677 1.1.4.2 nathanw {
678 1.1.4.2 nathanw struct ustir_softc *sc = arg;
679 1.1.4.2 nathanw
680 1.1.4.4 nathanw DPRINTFN(20, ("%s: starting polling thread\n", __func__));
681 1.1.4.2 nathanw
682 1.1.4.2 nathanw while (!sc->sc_closing) {
683 1.1.4.2 nathanw if (!sc->sc_rd_readinprogress && !USTIR_BLOCK_RX_DATA(sc))
684 1.1.4.2 nathanw ustir_periodic(sc);
685 1.1.4.2 nathanw
686 1.1.4.2 nathanw if (!sc->sc_closing) {
687 1.1.4.2 nathanw int error;
688 1.1.4.2 nathanw error = tsleep(&sc->sc_thread, PWAIT,
689 1.1.4.2 nathanw "ustir", hz / 10);
690 1.1.4.2 nathanw if (error == EWOULDBLOCK &&
691 1.1.4.2 nathanw sc->sc_rd_expectdataticks > 0)
692 1.1.4.2 nathanw /*
693 1.1.4.2 nathanw * After a timeout decrement the tick
694 1.1.4.2 nathanw * counter within which time we expect
695 1.1.4.2 nathanw * data to arrive if we are receiving
696 1.1.4.2 nathanw * data...
697 1.1.4.2 nathanw */
698 1.1.4.2 nathanw sc->sc_rd_expectdataticks--;
699 1.1.4.2 nathanw }
700 1.1.4.2 nathanw }
701 1.1.4.2 nathanw
702 1.1.4.4 nathanw DPRINTFN(20, ("%s: exiting polling thread\n", __func__));
703 1.1.4.2 nathanw
704 1.1.4.2 nathanw sc->sc_thread = NULL;
705 1.1.4.2 nathanw
706 1.1.4.2 nathanw wakeup(&sc->sc_closing);
707 1.1.4.2 nathanw
708 1.1.4.2 nathanw if (--sc->sc_refcnt < 0)
709 1.1.4.2 nathanw usb_detach_wakeup(USBDEV(sc->sc_dev));
710 1.1.4.2 nathanw
711 1.1.4.2 nathanw kthread_exit(0);
712 1.1.4.2 nathanw }
713 1.1.4.2 nathanw
714 1.1.4.2 nathanw Static void
715 1.1.4.2 nathanw ustir_rd_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
716 1.1.4.2 nathanw usbd_status status)
717 1.1.4.2 nathanw {
718 1.1.4.2 nathanw struct ustir_softc *sc = priv;
719 1.1.4.2 nathanw u_int32_t size;
720 1.1.4.2 nathanw
721 1.1.4.4 nathanw DPRINTFN(60, ("%s: sc=%p\n", __func__, sc));
722 1.1.4.2 nathanw
723 1.1.4.2 nathanw /* Read is no longer in progress */
724 1.1.4.2 nathanw sc->sc_rd_readinprogress = 0;
725 1.1.4.2 nathanw
726 1.1.4.2 nathanw if (status == USBD_CANCELLED || sc->sc_closing) /* this is normal */
727 1.1.4.2 nathanw return;
728 1.1.4.2 nathanw if (status) {
729 1.1.4.2 nathanw size = 0;
730 1.1.4.2 nathanw sc->sc_rd_err = 1;
731 1.1.4.2 nathanw
732 1.1.4.2 nathanw if (sc->sc_direction == udir_input ||
733 1.1.4.2 nathanw sc->sc_direction == udir_idle) {
734 1.1.4.2 nathanw /*
735 1.1.4.2 nathanw * Receive error, probably need to clear error
736 1.1.4.2 nathanw * condition.
737 1.1.4.2 nathanw */
738 1.1.4.2 nathanw sc->sc_direction = udir_stalled;
739 1.1.4.2 nathanw }
740 1.1.4.2 nathanw } else {
741 1.1.4.2 nathanw usbd_get_xfer_status(xfer, NULL, NULL, &size, NULL);
742 1.1.4.2 nathanw }
743 1.1.4.2 nathanw
744 1.1.4.2 nathanw sc->sc_rd_index = 0;
745 1.1.4.2 nathanw sc->sc_rd_count = size;
746 1.1.4.2 nathanw
747 1.1.4.2 nathanw DPRINTFN(((size > 0 || sc->sc_rd_err != 0) ? 20 : 60),
748 1.1.4.4 nathanw ("%s: sc=%p size=%u, err=%d\n", __func__,
749 1.1.4.2 nathanw sc, size, sc->sc_rd_err));
750 1.1.4.2 nathanw
751 1.1.4.2 nathanw #ifdef USTIR_DEBUG
752 1.1.4.2 nathanw if (ustirdebug >= 20 && size > 0)
753 1.1.4.4 nathanw ustir_dumpdata(sc->sc_rd_buf, size, __func__);
754 1.1.4.2 nathanw #endif
755 1.1.4.2 nathanw
756 1.1.4.2 nathanw if (!deframe_rd_ur(sc)) {
757 1.1.4.2 nathanw if (!deframe_isclear(&sc->sc_framestate) && size == 0 &&
758 1.1.4.2 nathanw sc->sc_rd_expectdataticks == 0) {
759 1.1.4.2 nathanw /*
760 1.1.4.2 nathanw * Expected data, but didn't get it
761 1.1.4.2 nathanw * within expected time...
762 1.1.4.2 nathanw */
763 1.1.4.2 nathanw DPRINTFN(5,("%s: incoming packet timeout\n",
764 1.1.4.4 nathanw __func__));
765 1.1.4.2 nathanw deframe_clear(&sc->sc_framestate);
766 1.1.4.2 nathanw } else if (size > 0) {
767 1.1.4.2 nathanw /*
768 1.1.4.2 nathanw * If we also received actual data, reset the
769 1.1.4.2 nathanw * data read timeout and wake up the possibly
770 1.1.4.2 nathanw * sleeping thread...
771 1.1.4.2 nathanw */
772 1.1.4.2 nathanw sc->sc_rd_expectdataticks = 2;
773 1.1.4.2 nathanw wakeup(&sc->sc_thread);
774 1.1.4.2 nathanw }
775 1.1.4.2 nathanw }
776 1.1.4.2 nathanw
777 1.1.4.2 nathanw /*
778 1.1.4.2 nathanw * Check if incoming data has stopped, or that we cannot
779 1.1.4.2 nathanw * safely read any more data. In the case of the latter we
780 1.1.4.2 nathanw * must switch to idle so that a write will not block...
781 1.1.4.2 nathanw */
782 1.1.4.2 nathanw if (sc->sc_direction == udir_input &&
783 1.1.4.2 nathanw ((size == 0 && sc->sc_rd_expectdataticks == 0) ||
784 1.1.4.2 nathanw USTIR_BLOCK_RX_DATA(sc))) {
785 1.1.4.7 thorpej DPRINTFN(8,("%s: idling on packet timeout, "
786 1.1.4.7 thorpej "complete frame, or no data\n", __func__));
787 1.1.4.2 nathanw sc->sc_direction = udir_idle;
788 1.1.4.2 nathanw
789 1.1.4.2 nathanw /* Wake up for possible output */
790 1.1.4.2 nathanw wakeup(&sc->sc_wr_buf);
791 1.1.4.5 nathanw selnotify(&sc->sc_wr_sel, 0);
792 1.1.4.2 nathanw }
793 1.1.4.2 nathanw }
794 1.1.4.2 nathanw
795 1.1.4.2 nathanw Static usbd_status
796 1.1.4.2 nathanw ustir_start_read(struct ustir_softc *sc)
797 1.1.4.2 nathanw {
798 1.1.4.2 nathanw usbd_status err;
799 1.1.4.2 nathanw
800 1.1.4.4 nathanw DPRINTFN(60,("%s: sc=%p, size=%d\n", __func__, sc,
801 1.1.4.2 nathanw sc->sc_params.maxsize));
802 1.1.4.2 nathanw
803 1.1.4.2 nathanw if (sc->sc_dying)
804 1.1.4.2 nathanw return USBD_IOERROR;
805 1.1.4.2 nathanw
806 1.1.4.2 nathanw if (USTIR_BLOCK_RX_DATA(sc) || deframe_rd_ur(sc)) {
807 1.1.4.2 nathanw /*
808 1.1.4.2 nathanw * Can't start reading just yet. Since we aren't
809 1.1.4.2 nathanw * going to start a read, have to switch direction to
810 1.1.4.2 nathanw * idle.
811 1.1.4.2 nathanw */
812 1.1.4.2 nathanw sc->sc_direction = udir_idle;
813 1.1.4.2 nathanw return USBD_NORMAL_COMPLETION;
814 1.1.4.2 nathanw }
815 1.1.4.2 nathanw
816 1.1.4.2 nathanw /* Starting a read... */
817 1.1.4.2 nathanw sc->sc_rd_readinprogress = 1;
818 1.1.4.2 nathanw sc->sc_direction = udir_input;
819 1.1.4.2 nathanw
820 1.1.4.2 nathanw if (sc->sc_rd_err) {
821 1.1.4.2 nathanw sc->sc_rd_err = 0;
822 1.1.4.4 nathanw DPRINTFN(0, ("%s: clear stall\n", __func__));
823 1.1.4.2 nathanw usbd_clear_endpoint_stall(sc->sc_rd_pipe);
824 1.1.4.2 nathanw }
825 1.1.4.2 nathanw
826 1.1.4.2 nathanw usbd_setup_xfer(sc->sc_rd_xfer, sc->sc_rd_pipe, sc, sc->sc_rd_buf,
827 1.1.4.2 nathanw sc->sc_params.maxsize,
828 1.1.4.2 nathanw USBD_SHORT_XFER_OK | USBD_NO_COPY,
829 1.1.4.2 nathanw USBD_NO_TIMEOUT, ustir_rd_cb);
830 1.1.4.2 nathanw err = usbd_transfer(sc->sc_rd_xfer);
831 1.1.4.2 nathanw if (err != USBD_IN_PROGRESS) {
832 1.1.4.7 thorpej DPRINTFN(0, ("%s: err=%d\n", __func__, (int)err));
833 1.1.4.2 nathanw return err;
834 1.1.4.2 nathanw }
835 1.1.4.2 nathanw return USBD_NORMAL_COMPLETION;
836 1.1.4.2 nathanw }
837 1.1.4.2 nathanw
838 1.1.4.2 nathanw Static int
839 1.1.4.2 nathanw ustir_activate(device_ptr_t self, enum devact act)
840 1.1.4.2 nathanw {
841 1.1.4.2 nathanw struct ustir_softc *sc = (struct ustir_softc *)self;
842 1.1.4.2 nathanw int error = 0;
843 1.1.4.2 nathanw
844 1.1.4.2 nathanw switch (act) {
845 1.1.4.2 nathanw case DVACT_ACTIVATE:
846 1.1.4.2 nathanw return EOPNOTSUPP;
847 1.1.4.2 nathanw
848 1.1.4.2 nathanw case DVACT_DEACTIVATE:
849 1.1.4.2 nathanw sc->sc_dying = 1;
850 1.1.4.2 nathanw if (sc->sc_child != NULL)
851 1.1.4.2 nathanw error = config_deactivate(sc->sc_child);
852 1.1.4.2 nathanw break;
853 1.1.4.2 nathanw }
854 1.1.4.2 nathanw return error;
855 1.1.4.2 nathanw }
856 1.1.4.2 nathanw
857 1.1.4.5 nathanw /* ARGSUSED */
858 1.1.4.2 nathanw Static int
859 1.1.4.2 nathanw ustir_open(void *h, int flag, int mode, usb_proc_ptr p)
860 1.1.4.2 nathanw {
861 1.1.4.2 nathanw struct ustir_softc *sc = h;
862 1.1.4.2 nathanw int error;
863 1.1.4.2 nathanw usbd_status err;
864 1.1.4.2 nathanw
865 1.1.4.4 nathanw DPRINTFN(0, ("%s: sc=%p\n", __func__, sc));
866 1.1.4.2 nathanw
867 1.1.4.2 nathanw err = usbd_open_pipe(sc->sc_iface, sc->sc_rd_addr, 0, &sc->sc_rd_pipe);
868 1.1.4.7 thorpej if (err != USBD_NORMAL_COMPLETION) {
869 1.1.4.2 nathanw error = EIO;
870 1.1.4.2 nathanw goto bad1;
871 1.1.4.2 nathanw }
872 1.1.4.2 nathanw err = usbd_open_pipe(sc->sc_iface, sc->sc_wr_addr, 0, &sc->sc_wr_pipe);
873 1.1.4.7 thorpej if (err != USBD_NORMAL_COMPLETION) {
874 1.1.4.2 nathanw error = EIO;
875 1.1.4.2 nathanw goto bad2;
876 1.1.4.2 nathanw }
877 1.1.4.2 nathanw sc->sc_rd_xfer = usbd_alloc_xfer(sc->sc_udev);
878 1.1.4.2 nathanw if (sc->sc_rd_xfer == NULL) {
879 1.1.4.2 nathanw error = ENOMEM;
880 1.1.4.2 nathanw goto bad3;
881 1.1.4.2 nathanw }
882 1.1.4.2 nathanw sc->sc_wr_xfer = usbd_alloc_xfer(sc->sc_udev);
883 1.1.4.2 nathanw if (sc->sc_wr_xfer == NULL) {
884 1.1.4.2 nathanw error = ENOMEM;
885 1.1.4.2 nathanw goto bad4;
886 1.1.4.2 nathanw }
887 1.1.4.2 nathanw sc->sc_rd_buf = usbd_alloc_buffer(sc->sc_rd_xfer,
888 1.1.4.2 nathanw IRDA_MAX_FRAME_SIZE);
889 1.1.4.2 nathanw if (sc->sc_rd_buf == NULL) {
890 1.1.4.2 nathanw error = ENOMEM;
891 1.1.4.2 nathanw goto bad5;
892 1.1.4.2 nathanw }
893 1.1.4.2 nathanw sc->sc_wr_buf = usbd_alloc_buffer(sc->sc_wr_xfer,
894 1.1.4.2 nathanw IRDA_MAX_FRAME_SIZE + STIR_OUTPUT_HEADER_SIZE);
895 1.1.4.2 nathanw if (sc->sc_wr_buf == NULL) {
896 1.1.4.2 nathanw error = ENOMEM;
897 1.1.4.2 nathanw goto bad5;
898 1.1.4.2 nathanw }
899 1.1.4.2 nathanw sc->sc_ur_buf = malloc(IRDA_MAX_FRAME_SIZE, M_USBDEV, M_NOWAIT);
900 1.1.4.2 nathanw if (sc->sc_ur_buf == NULL) {
901 1.1.4.2 nathanw error = ENOMEM;
902 1.1.4.2 nathanw goto bad5;
903 1.1.4.2 nathanw }
904 1.1.4.2 nathanw
905 1.1.4.2 nathanw sc->sc_rd_index = sc->sc_rd_count = 0;
906 1.1.4.2 nathanw sc->sc_closing = 0;
907 1.1.4.2 nathanw sc->sc_rd_readinprogress = 0;
908 1.1.4.2 nathanw sc->sc_rd_expectdataticks = 0;
909 1.1.4.2 nathanw sc->sc_ur_framelen = 0;
910 1.1.4.2 nathanw sc->sc_rd_err = 0;
911 1.1.4.7 thorpej sc->sc_wr_stalewrite = 0;
912 1.1.4.2 nathanw sc->sc_speedrec = NULL;
913 1.1.4.2 nathanw sc->sc_direction = udir_idle;
914 1.1.4.2 nathanw sc->sc_params.speed = 0;
915 1.1.4.2 nathanw sc->sc_params.ebofs = 0;
916 1.1.4.2 nathanw sc->sc_params.maxsize = IRDA_MAX_FRAME_SIZE;
917 1.1.4.2 nathanw
918 1.1.4.2 nathanw deframe_init(&sc->sc_framestate, &framedef_sir, sc->sc_ur_buf,
919 1.1.4.2 nathanw IRDA_MAX_FRAME_SIZE);
920 1.1.4.2 nathanw
921 1.1.4.2 nathanw error = kthread_create1(ustir_thread, sc, &sc->sc_thread, "%s",
922 1.1.4.2 nathanw sc->sc_dev.dv_xname);
923 1.1.4.2 nathanw if (error)
924 1.1.4.2 nathanw goto bad5;
925 1.1.4.2 nathanw /* Increment reference for thread */
926 1.1.4.2 nathanw sc->sc_refcnt++;
927 1.1.4.2 nathanw
928 1.1.4.2 nathanw return 0;
929 1.1.4.2 nathanw
930 1.1.4.2 nathanw bad5:
931 1.1.4.2 nathanw usbd_free_xfer(sc->sc_wr_xfer);
932 1.1.4.2 nathanw sc->sc_wr_xfer = NULL;
933 1.1.4.2 nathanw bad4:
934 1.1.4.2 nathanw usbd_free_xfer(sc->sc_rd_xfer);
935 1.1.4.2 nathanw sc->sc_rd_xfer = NULL;
936 1.1.4.2 nathanw bad3:
937 1.1.4.2 nathanw usbd_close_pipe(sc->sc_wr_pipe);
938 1.1.4.2 nathanw sc->sc_wr_pipe = NULL;
939 1.1.4.2 nathanw bad2:
940 1.1.4.2 nathanw usbd_close_pipe(sc->sc_rd_pipe);
941 1.1.4.2 nathanw sc->sc_rd_pipe = NULL;
942 1.1.4.2 nathanw bad1:
943 1.1.4.2 nathanw return error;
944 1.1.4.2 nathanw }
945 1.1.4.2 nathanw
946 1.1.4.5 nathanw /* ARGSUSED */
947 1.1.4.2 nathanw Static int
948 1.1.4.2 nathanw ustir_close(void *h, int flag, int mode, usb_proc_ptr p)
949 1.1.4.2 nathanw {
950 1.1.4.2 nathanw struct ustir_softc *sc = h;
951 1.1.4.2 nathanw
952 1.1.4.4 nathanw DPRINTFN(0, ("%s: sc=%p\n", __func__, sc));
953 1.1.4.2 nathanw
954 1.1.4.2 nathanw sc->sc_refcnt++;
955 1.1.4.2 nathanw
956 1.1.4.2 nathanw sc->sc_rd_readinprogress = 1;
957 1.1.4.2 nathanw sc->sc_closing = 1;
958 1.1.4.2 nathanw
959 1.1.4.2 nathanw wakeup(&sc->sc_thread);
960 1.1.4.2 nathanw
961 1.1.4.2 nathanw while (sc->sc_thread != NULL)
962 1.1.4.2 nathanw tsleep(&sc->sc_closing, PWAIT, "usircl", 0);
963 1.1.4.2 nathanw
964 1.1.4.2 nathanw if (sc->sc_rd_pipe != NULL) {
965 1.1.4.2 nathanw usbd_abort_pipe(sc->sc_rd_pipe);
966 1.1.4.2 nathanw usbd_close_pipe(sc->sc_rd_pipe);
967 1.1.4.2 nathanw sc->sc_rd_pipe = NULL;
968 1.1.4.2 nathanw }
969 1.1.4.2 nathanw if (sc->sc_wr_pipe != NULL) {
970 1.1.4.2 nathanw usbd_abort_pipe(sc->sc_wr_pipe);
971 1.1.4.2 nathanw usbd_close_pipe(sc->sc_wr_pipe);
972 1.1.4.2 nathanw sc->sc_wr_pipe = NULL;
973 1.1.4.2 nathanw }
974 1.1.4.2 nathanw if (sc->sc_rd_xfer != NULL) {
975 1.1.4.2 nathanw usbd_free_xfer(sc->sc_rd_xfer);
976 1.1.4.2 nathanw sc->sc_rd_xfer = NULL;
977 1.1.4.2 nathanw sc->sc_rd_buf = NULL;
978 1.1.4.2 nathanw }
979 1.1.4.2 nathanw if (sc->sc_wr_xfer != NULL) {
980 1.1.4.2 nathanw usbd_free_xfer(sc->sc_wr_xfer);
981 1.1.4.2 nathanw sc->sc_wr_xfer = NULL;
982 1.1.4.2 nathanw sc->sc_wr_buf = NULL;
983 1.1.4.2 nathanw }
984 1.1.4.2 nathanw if (sc->sc_ur_buf != NULL) {
985 1.1.4.2 nathanw free(sc->sc_ur_buf, M_USBDEV);
986 1.1.4.2 nathanw sc->sc_ur_buf = NULL;
987 1.1.4.2 nathanw }
988 1.1.4.2 nathanw
989 1.1.4.2 nathanw if (--sc->sc_refcnt < 0)
990 1.1.4.2 nathanw usb_detach_wakeup(USBDEV(sc->sc_dev));
991 1.1.4.2 nathanw
992 1.1.4.2 nathanw return 0;
993 1.1.4.2 nathanw }
994 1.1.4.2 nathanw
995 1.1.4.7 thorpej /* ARGSUSED */
996 1.1.4.2 nathanw Static int
997 1.1.4.2 nathanw ustir_read(void *h, struct uio *uio, int flag)
998 1.1.4.2 nathanw {
999 1.1.4.2 nathanw struct ustir_softc *sc = h;
1000 1.1.4.2 nathanw int s;
1001 1.1.4.2 nathanw int error;
1002 1.1.4.2 nathanw u_int uframelen;
1003 1.1.4.2 nathanw
1004 1.1.4.4 nathanw DPRINTFN(1,("%s: sc=%p\n", __func__, sc));
1005 1.1.4.2 nathanw
1006 1.1.4.2 nathanw if (sc->sc_dying)
1007 1.1.4.2 nathanw return EIO;
1008 1.1.4.2 nathanw
1009 1.1.4.2 nathanw #ifdef DIAGNOSTIC
1010 1.1.4.2 nathanw if (sc->sc_rd_buf == NULL)
1011 1.1.4.2 nathanw return EINVAL;
1012 1.1.4.2 nathanw #endif
1013 1.1.4.2 nathanw
1014 1.1.4.2 nathanw sc->sc_refcnt++;
1015 1.1.4.2 nathanw
1016 1.1.4.2 nathanw if (!sc->sc_rd_readinprogress && !USTIR_BLOCK_RX_DATA(sc))
1017 1.1.4.2 nathanw /* Possibly wake up polling thread */
1018 1.1.4.2 nathanw wakeup(&sc->sc_thread);
1019 1.1.4.2 nathanw
1020 1.1.4.2 nathanw do {
1021 1.1.4.2 nathanw s = splusb();
1022 1.1.4.2 nathanw while (sc->sc_ur_framelen == 0) {
1023 1.1.4.4 nathanw DPRINTFN(5,("%s: calling tsleep()\n", __func__));
1024 1.1.4.2 nathanw error = tsleep(&sc->sc_ur_framelen, PZERO | PCATCH,
1025 1.1.4.2 nathanw "usirrd", 0);
1026 1.1.4.2 nathanw if (sc->sc_dying)
1027 1.1.4.2 nathanw error = EIO;
1028 1.1.4.2 nathanw if (error) {
1029 1.1.4.2 nathanw splx(s);
1030 1.1.4.2 nathanw DPRINTFN(0, ("%s: tsleep() = %d\n",
1031 1.1.4.4 nathanw __func__, error));
1032 1.1.4.2 nathanw goto ret;
1033 1.1.4.2 nathanw }
1034 1.1.4.2 nathanw }
1035 1.1.4.2 nathanw splx(s);
1036 1.1.4.2 nathanw
1037 1.1.4.2 nathanw uframelen = sc->sc_ur_framelen;
1038 1.1.4.2 nathanw DPRINTFN(1,("%s: sc=%p framelen=%u, hdr=0x%02x\n",
1039 1.1.4.4 nathanw __func__, sc, uframelen, sc->sc_ur_buf[0]));
1040 1.1.4.2 nathanw if (uframelen > uio->uio_resid)
1041 1.1.4.2 nathanw error = EINVAL;
1042 1.1.4.2 nathanw else
1043 1.1.4.2 nathanw error = uiomove(sc->sc_ur_buf, uframelen, uio);
1044 1.1.4.2 nathanw sc->sc_ur_framelen = 0;
1045 1.1.4.2 nathanw
1046 1.1.4.2 nathanw if (!deframe_rd_ur(sc) && uframelen > 0) {
1047 1.1.4.2 nathanw /*
1048 1.1.4.2 nathanw * Need to wait for another read to obtain a
1049 1.1.4.2 nathanw * complete frame... If we also obtained
1050 1.1.4.2 nathanw * actual data, wake up the possibly sleeping
1051 1.1.4.2 nathanw * thread immediately...
1052 1.1.4.2 nathanw */
1053 1.1.4.2 nathanw wakeup(&sc->sc_thread);
1054 1.1.4.2 nathanw }
1055 1.1.4.2 nathanw } while (uframelen == 0);
1056 1.1.4.2 nathanw
1057 1.1.4.4 nathanw DPRINTFN(1,("%s: return %d\n", __func__, error));
1058 1.1.4.2 nathanw
1059 1.1.4.2 nathanw ret:
1060 1.1.4.2 nathanw if (--sc->sc_refcnt < 0)
1061 1.1.4.2 nathanw usb_detach_wakeup(USBDEV(sc->sc_dev));
1062 1.1.4.2 nathanw return error;
1063 1.1.4.2 nathanw }
1064 1.1.4.2 nathanw
1065 1.1.4.7 thorpej /* ARGSUSED */
1066 1.1.4.2 nathanw Static int
1067 1.1.4.2 nathanw ustir_write(void *h, struct uio *uio, int flag)
1068 1.1.4.2 nathanw {
1069 1.1.4.2 nathanw struct ustir_softc *sc = h;
1070 1.1.4.2 nathanw usbd_status err;
1071 1.1.4.2 nathanw u_int32_t wrlen;
1072 1.1.4.2 nathanw int error, sirlength;
1073 1.1.4.2 nathanw u_int8_t *wrbuf;
1074 1.1.4.2 nathanw int s;
1075 1.1.4.2 nathanw
1076 1.1.4.4 nathanw DPRINTFN(1,("%s: sc=%p\n", __func__, sc));
1077 1.1.4.2 nathanw
1078 1.1.4.2 nathanw if (sc->sc_dying)
1079 1.1.4.2 nathanw return EIO;
1080 1.1.4.2 nathanw
1081 1.1.4.2 nathanw #ifdef DIAGNOSTIC
1082 1.1.4.2 nathanw if (sc->sc_wr_buf == NULL)
1083 1.1.4.2 nathanw return EINVAL;
1084 1.1.4.2 nathanw #endif
1085 1.1.4.2 nathanw
1086 1.1.4.2 nathanw wrlen = uio->uio_resid;
1087 1.1.4.2 nathanw if (wrlen > sc->sc_params.maxsize)
1088 1.1.4.2 nathanw return EINVAL;
1089 1.1.4.2 nathanw
1090 1.1.4.2 nathanw sc->sc_refcnt++;
1091 1.1.4.2 nathanw
1092 1.1.4.7 thorpej if (!USTIR_BLOCK_RX_DATA(sc)) {
1093 1.1.4.7 thorpej /*
1094 1.1.4.7 thorpej * If reads are not blocked, determine what action we
1095 1.1.4.7 thorpej * should potentially take...
1096 1.1.4.7 thorpej */
1097 1.1.4.7 thorpej if (sc->sc_direction == udir_output) {
1098 1.1.4.7 thorpej /*
1099 1.1.4.7 thorpej * If the last operation was an output, wait for the
1100 1.1.4.7 thorpej * polling thread to check for incoming data.
1101 1.1.4.7 thorpej */
1102 1.1.4.7 thorpej sc->sc_wr_stalewrite = 1;
1103 1.1.4.7 thorpej wakeup(&sc->sc_thread);
1104 1.1.4.7 thorpej } else if (!sc->sc_rd_readinprogress &&
1105 1.1.4.7 thorpej (sc->sc_direction == udir_idle ||
1106 1.1.4.7 thorpej sc->sc_direction == udir_input)) {
1107 1.1.4.7 thorpej /* If idle, check for input before outputting */
1108 1.1.4.7 thorpej ustir_start_read(sc);
1109 1.1.4.7 thorpej }
1110 1.1.4.7 thorpej }
1111 1.1.4.2 nathanw
1112 1.1.4.2 nathanw s = splusb();
1113 1.1.4.7 thorpej while (sc->sc_wr_stalewrite ||
1114 1.1.4.7 thorpej (sc->sc_direction != udir_output &&
1115 1.1.4.7 thorpej sc->sc_direction != udir_idle)) {
1116 1.1.4.7 thorpej DPRINTFN(5, ("%s: sc=%p stalewrite=%d direction=%d, "
1117 1.1.4.7 thorpej "calling tsleep()\n", __func__,
1118 1.1.4.7 thorpej sc, sc->sc_wr_stalewrite, sc->sc_direction));
1119 1.1.4.2 nathanw error = tsleep(&sc->sc_wr_buf, PZERO | PCATCH,
1120 1.1.4.2 nathanw "usirwr", 0);
1121 1.1.4.2 nathanw if (sc->sc_dying)
1122 1.1.4.2 nathanw error = EIO;
1123 1.1.4.2 nathanw if (error) {
1124 1.1.4.2 nathanw splx(s);
1125 1.1.4.4 nathanw DPRINTFN(0, ("%s: tsleep() = %d\n", __func__,
1126 1.1.4.2 nathanw error));
1127 1.1.4.2 nathanw goto ret;
1128 1.1.4.2 nathanw }
1129 1.1.4.2 nathanw }
1130 1.1.4.2 nathanw splx(s);
1131 1.1.4.2 nathanw
1132 1.1.4.2 nathanw wrbuf = sc->sc_wr_buf;
1133 1.1.4.2 nathanw
1134 1.1.4.2 nathanw /* Build header */
1135 1.1.4.2 nathanw wrbuf[0] = STIR_OUTPUT_HEADER_BYTE0;
1136 1.1.4.2 nathanw wrbuf[1] = STIR_OUTPUT_HEADER_BYTE1;
1137 1.1.4.2 nathanw
1138 1.1.4.2 nathanw sirlength = irda_sir_frame(&wrbuf[STIR_OUTPUT_HEADER_SIZE],
1139 1.1.4.2 nathanw MAX_USTIR_OUTPUT_FRAME -
1140 1.1.4.2 nathanw STIR_OUTPUT_HEADER_SIZE,
1141 1.1.4.2 nathanw uio, sc->sc_params.ebofs);
1142 1.1.4.2 nathanw if (sirlength < 0) {
1143 1.1.4.2 nathanw error = -sirlength;
1144 1.1.4.2 nathanw } else {
1145 1.1.4.2 nathanw u_int32_t btlen;
1146 1.1.4.2 nathanw
1147 1.1.4.4 nathanw DPRINTFN(1, ("%s: transfer %u bytes\n", __func__,
1148 1.1.4.2 nathanw (unsigned int)wrlen));
1149 1.1.4.2 nathanw
1150 1.1.4.2 nathanw wrbuf[2] = sirlength & 0xff;
1151 1.1.4.2 nathanw wrbuf[3] = (sirlength >> 8) & 0xff;
1152 1.1.4.2 nathanw
1153 1.1.4.2 nathanw btlen = STIR_OUTPUT_HEADER_SIZE + sirlength;
1154 1.1.4.2 nathanw
1155 1.1.4.2 nathanw sc->sc_direction = udir_output;
1156 1.1.4.2 nathanw
1157 1.1.4.2 nathanw #ifdef USTIR_DEBUG
1158 1.1.4.2 nathanw if (ustirdebug >= 20)
1159 1.1.4.4 nathanw ustir_dumpdata(wrbuf, btlen, __func__);
1160 1.1.4.2 nathanw #endif
1161 1.1.4.2 nathanw
1162 1.1.4.2 nathanw err = usbd_bulk_transfer(sc->sc_wr_xfer, sc->sc_wr_pipe,
1163 1.1.4.2 nathanw USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
1164 1.1.4.2 nathanw USTIR_WR_TIMEOUT,
1165 1.1.4.2 nathanw wrbuf, &btlen, "ustiwr");
1166 1.1.4.4 nathanw DPRINTFN(2, ("%s: err=%d\n", __func__, err));
1167 1.1.4.7 thorpej if (err != USBD_NORMAL_COMPLETION) {
1168 1.1.4.2 nathanw if (err == USBD_INTERRUPTED)
1169 1.1.4.2 nathanw error = EINTR;
1170 1.1.4.2 nathanw else if (err == USBD_TIMEOUT)
1171 1.1.4.2 nathanw error = ETIMEDOUT;
1172 1.1.4.2 nathanw else
1173 1.1.4.2 nathanw error = EIO;
1174 1.1.4.2 nathanw } else {
1175 1.1.4.2 nathanw error = 0;
1176 1.1.4.2 nathanw }
1177 1.1.4.2 nathanw }
1178 1.1.4.2 nathanw
1179 1.1.4.2 nathanw ret:
1180 1.1.4.2 nathanw if (--sc->sc_refcnt < 0)
1181 1.1.4.2 nathanw usb_detach_wakeup(USBDEV(sc->sc_dev));
1182 1.1.4.2 nathanw
1183 1.1.4.4 nathanw DPRINTFN(1,("%s: sc=%p done\n", __func__, sc));
1184 1.1.4.2 nathanw return error;
1185 1.1.4.2 nathanw }
1186 1.1.4.2 nathanw
1187 1.1.4.2 nathanw Static int
1188 1.1.4.2 nathanw ustir_poll(void *h, int events, usb_proc_ptr p)
1189 1.1.4.2 nathanw {
1190 1.1.4.2 nathanw struct ustir_softc *sc = h;
1191 1.1.4.2 nathanw int revents = 0;
1192 1.1.4.2 nathanw
1193 1.1.4.4 nathanw DPRINTFN(1,("%s: sc=%p\n", __func__, sc));
1194 1.1.4.2 nathanw
1195 1.1.4.2 nathanw if (events & (POLLOUT | POLLWRNORM)) {
1196 1.1.4.2 nathanw if (sc->sc_direction != udir_input) {
1197 1.1.4.2 nathanw revents |= events & (POLLOUT | POLLWRNORM);
1198 1.1.4.2 nathanw } else {
1199 1.1.4.2 nathanw DPRINTFN(2,("%s: recording write select\n",
1200 1.1.4.4 nathanw __func__));
1201 1.1.4.2 nathanw selrecord(p, &sc->sc_wr_sel);
1202 1.1.4.2 nathanw }
1203 1.1.4.2 nathanw }
1204 1.1.4.2 nathanw
1205 1.1.4.2 nathanw if (events & (POLLIN | POLLRDNORM)) {
1206 1.1.4.2 nathanw if (sc->sc_ur_framelen != 0) {
1207 1.1.4.4 nathanw DPRINTFN(2,("%s: have data\n", __func__));
1208 1.1.4.2 nathanw revents |= events & (POLLIN | POLLRDNORM);
1209 1.1.4.2 nathanw } else {
1210 1.1.4.2 nathanw DPRINTFN(2,("%s: recording read select\n",
1211 1.1.4.4 nathanw __func__));
1212 1.1.4.2 nathanw selrecord(p, &sc->sc_rd_sel);
1213 1.1.4.2 nathanw }
1214 1.1.4.2 nathanw }
1215 1.1.4.2 nathanw
1216 1.1.4.2 nathanw return revents;
1217 1.1.4.2 nathanw }
1218 1.1.4.2 nathanw
1219 1.1.4.5 nathanw static void
1220 1.1.4.5 nathanw filt_ustirrdetach(struct knote *kn)
1221 1.1.4.5 nathanw {
1222 1.1.4.5 nathanw struct ustir_softc *sc = kn->kn_hook;
1223 1.1.4.5 nathanw int s;
1224 1.1.4.5 nathanw
1225 1.1.4.5 nathanw s = splusb();
1226 1.1.4.6 thorpej SLIST_REMOVE(&sc->sc_rd_sel.sel_klist, kn, knote, kn_selnext);
1227 1.1.4.5 nathanw splx(s);
1228 1.1.4.5 nathanw }
1229 1.1.4.5 nathanw
1230 1.1.4.7 thorpej /* ARGSUSED */
1231 1.1.4.5 nathanw static int
1232 1.1.4.5 nathanw filt_ustirread(struct knote *kn, long hint)
1233 1.1.4.5 nathanw {
1234 1.1.4.5 nathanw struct ustir_softc *sc = kn->kn_hook;
1235 1.1.4.5 nathanw
1236 1.1.4.5 nathanw kn->kn_data = sc->sc_ur_framelen;
1237 1.1.4.5 nathanw return (kn->kn_data > 0);
1238 1.1.4.5 nathanw }
1239 1.1.4.5 nathanw
1240 1.1.4.5 nathanw static void
1241 1.1.4.5 nathanw filt_ustirwdetach(struct knote *kn)
1242 1.1.4.5 nathanw {
1243 1.1.4.5 nathanw struct ustir_softc *sc = kn->kn_hook;
1244 1.1.4.5 nathanw int s;
1245 1.1.4.5 nathanw
1246 1.1.4.5 nathanw s = splusb();
1247 1.1.4.6 thorpej SLIST_REMOVE(&sc->sc_wr_sel.sel_klist, kn, knote, kn_selnext);
1248 1.1.4.5 nathanw splx(s);
1249 1.1.4.5 nathanw }
1250 1.1.4.5 nathanw
1251 1.1.4.7 thorpej /* ARGSUSED */
1252 1.1.4.5 nathanw static int
1253 1.1.4.5 nathanw filt_ustirwrite(struct knote *kn, long hint)
1254 1.1.4.5 nathanw {
1255 1.1.4.5 nathanw struct ustir_softc *sc = kn->kn_hook;
1256 1.1.4.5 nathanw
1257 1.1.4.5 nathanw kn->kn_data = 0;
1258 1.1.4.5 nathanw return (sc->sc_direction != udir_input);
1259 1.1.4.5 nathanw }
1260 1.1.4.5 nathanw
1261 1.1.4.5 nathanw static const struct filterops ustirread_filtops =
1262 1.1.4.5 nathanw { 1, NULL, filt_ustirrdetach, filt_ustirread };
1263 1.1.4.5 nathanw static const struct filterops ustirwrite_filtops =
1264 1.1.4.5 nathanw { 1, NULL, filt_ustirwdetach, filt_ustirwrite };
1265 1.1.4.5 nathanw
1266 1.1.4.5 nathanw Static int
1267 1.1.4.5 nathanw ustir_kqfilter(void *h, struct knote *kn)
1268 1.1.4.5 nathanw {
1269 1.1.4.5 nathanw struct ustir_softc *sc = h;
1270 1.1.4.5 nathanw struct klist *klist;
1271 1.1.4.5 nathanw int s;
1272 1.1.4.5 nathanw
1273 1.1.4.5 nathanw switch (kn->kn_filter) {
1274 1.1.4.5 nathanw case EVFILT_READ:
1275 1.1.4.6 thorpej klist = &sc->sc_rd_sel.sel_klist;
1276 1.1.4.5 nathanw kn->kn_fop = &ustirread_filtops;
1277 1.1.4.5 nathanw break;
1278 1.1.4.5 nathanw case EVFILT_WRITE:
1279 1.1.4.6 thorpej klist = &sc->sc_wr_sel.sel_klist;
1280 1.1.4.5 nathanw kn->kn_fop = &ustirwrite_filtops;
1281 1.1.4.5 nathanw break;
1282 1.1.4.5 nathanw default:
1283 1.1.4.5 nathanw return (1);
1284 1.1.4.5 nathanw }
1285 1.1.4.5 nathanw
1286 1.1.4.5 nathanw kn->kn_hook = sc;
1287 1.1.4.5 nathanw
1288 1.1.4.5 nathanw s = splusb();
1289 1.1.4.5 nathanw SLIST_INSERT_HEAD(klist, kn, kn_selnext);
1290 1.1.4.5 nathanw splx(s);
1291 1.1.4.5 nathanw
1292 1.1.4.5 nathanw return (0);
1293 1.1.4.5 nathanw }
1294 1.1.4.5 nathanw
1295 1.1.4.2 nathanw #ifdef USTIR_DEBUG_IOCTLS
1296 1.1.4.2 nathanw Static int ustir_ioctl(void *h, u_long cmd, caddr_t addr, int flag, usb_proc_ptr p)
1297 1.1.4.2 nathanw {
1298 1.1.4.2 nathanw struct ustir_softc *sc = h;
1299 1.1.4.2 nathanw int error;
1300 1.1.4.2 nathanw unsigned int regnum;
1301 1.1.4.2 nathanw usbd_status err;
1302 1.1.4.2 nathanw u_int8_t regdata;
1303 1.1.4.2 nathanw
1304 1.1.4.2 nathanw if (sc->sc_dying)
1305 1.1.4.2 nathanw return EIO;
1306 1.1.4.2 nathanw
1307 1.1.4.2 nathanw sc->sc_refcnt++;
1308 1.1.4.2 nathanw
1309 1.1.4.2 nathanw error = 0;
1310 1.1.4.2 nathanw switch (cmd) {
1311 1.1.4.2 nathanw case USTIR_READ_REGISTER:
1312 1.1.4.3 nathanw regnum = *(unsigned int *)addr;
1313 1.1.4.2 nathanw
1314 1.1.4.2 nathanw if (regnum > STIR_MAX_REG) {
1315 1.1.4.2 nathanw error = EINVAL;
1316 1.1.4.2 nathanw break;
1317 1.1.4.2 nathanw }
1318 1.1.4.2 nathanw
1319 1.1.4.2 nathanw err = ustir_read_reg(sc, regnum, ®data);
1320 1.1.4.2 nathanw
1321 1.1.4.4 nathanw DPRINTFN(10, ("%s: regget(%u) = 0x%x\n", __func__,
1322 1.1.4.2 nathanw regnum, (unsigned int)regdata));
1323 1.1.4.2 nathanw
1324 1.1.4.3 nathanw *(unsigned int *)addr = regdata;
1325 1.1.4.7 thorpej if (err != USBD_NORMAL_COMPLETION) {
1326 1.1.4.2 nathanw printf("%s: register read failed: %s\n",
1327 1.1.4.2 nathanw USBDEVNAME(sc->sc_dev),
1328 1.1.4.2 nathanw usbd_errstr(err));
1329 1.1.4.2 nathanw error = EIO;
1330 1.1.4.2 nathanw }
1331 1.1.4.2 nathanw break;
1332 1.1.4.2 nathanw
1333 1.1.4.2 nathanw case USTIR_WRITE_REGISTER:
1334 1.1.4.3 nathanw regnum = *(unsigned int *)addr;
1335 1.1.4.2 nathanw regdata = (regnum >> 8) & 0xff;
1336 1.1.4.2 nathanw regnum = regnum & 0xff;
1337 1.1.4.2 nathanw
1338 1.1.4.2 nathanw if (regnum > STIR_MAX_REG) {
1339 1.1.4.2 nathanw error = EINVAL;
1340 1.1.4.2 nathanw break;
1341 1.1.4.2 nathanw }
1342 1.1.4.2 nathanw
1343 1.1.4.4 nathanw DPRINTFN(10, ("%s: regset(%u, 0x%x)\n", __func__,
1344 1.1.4.2 nathanw regnum, (unsigned int)regdata));
1345 1.1.4.2 nathanw
1346 1.1.4.2 nathanw err = ustir_write_reg(sc, regnum, regdata);
1347 1.1.4.7 thorpej if (err != USBD_NORMAL_COMPLETION) {
1348 1.1.4.2 nathanw printf("%s: register write failed: %s\n",
1349 1.1.4.2 nathanw USBDEVNAME(sc->sc_dev),
1350 1.1.4.2 nathanw usbd_errstr(err));
1351 1.1.4.2 nathanw error = EIO;
1352 1.1.4.2 nathanw }
1353 1.1.4.2 nathanw break;
1354 1.1.4.2 nathanw
1355 1.1.4.2 nathanw case USTIR_DEBUG_LEVEL:
1356 1.1.4.2 nathanw #ifdef USTIR_DEBUG
1357 1.1.4.3 nathanw ustirdebug = *(int *)addr;
1358 1.1.4.2 nathanw #endif
1359 1.1.4.2 nathanw break;
1360 1.1.4.2 nathanw
1361 1.1.4.2 nathanw case USTIR_DEBUG_OPERATION:
1362 1.1.4.2 nathanw break;
1363 1.1.4.2 nathanw
1364 1.1.4.2 nathanw default:
1365 1.1.4.2 nathanw error = EINVAL;
1366 1.1.4.2 nathanw break;
1367 1.1.4.2 nathanw }
1368 1.1.4.2 nathanw
1369 1.1.4.2 nathanw if (--sc->sc_refcnt < 0)
1370 1.1.4.2 nathanw usb_detach_wakeup(USBDEV(sc->sc_dev));
1371 1.1.4.2 nathanw
1372 1.1.4.2 nathanw return error;
1373 1.1.4.2 nathanw }
1374 1.1.4.2 nathanw #endif
1375 1.1.4.2 nathanw
1376 1.1.4.2 nathanw Static int
1377 1.1.4.2 nathanw ustir_set_params(void *h, struct irda_params *p)
1378 1.1.4.2 nathanw {
1379 1.1.4.2 nathanw struct ustir_softc *sc = h;
1380 1.1.4.2 nathanw struct ustir_speedrec const *speedblk;
1381 1.1.4.2 nathanw int i;
1382 1.1.4.2 nathanw
1383 1.1.4.4 nathanw DPRINTFN(0, ("%s: sc=%p, speed=%d ebofs=%d maxsize=%d\n", __func__,
1384 1.1.4.2 nathanw sc, p->speed, p->ebofs, p->maxsize));
1385 1.1.4.2 nathanw
1386 1.1.4.2 nathanw if (sc->sc_dying)
1387 1.1.4.2 nathanw return EIO;
1388 1.1.4.2 nathanw
1389 1.1.4.2 nathanw speedblk = NULL;
1390 1.1.4.2 nathanw
1391 1.1.4.2 nathanw if (sc->sc_speedrec == NULL || p->speed != sc->sc_speedrec->speed) {
1392 1.1.4.2 nathanw /* find speed */
1393 1.1.4.2 nathanw for (i = 0; i < USTIR_NSPEEDS; i++) {
1394 1.1.4.2 nathanw if (ustir_speeds[i].speed == p->speed) {
1395 1.1.4.2 nathanw speedblk = &ustir_speeds[i];
1396 1.1.4.2 nathanw goto found2;
1397 1.1.4.2 nathanw }
1398 1.1.4.2 nathanw }
1399 1.1.4.2 nathanw /* no good value found */
1400 1.1.4.2 nathanw return EINVAL;
1401 1.1.4.2 nathanw found2:
1402 1.1.4.2 nathanw ;
1403 1.1.4.2 nathanw }
1404 1.1.4.2 nathanw if (p->maxsize != sc->sc_params.maxsize) {
1405 1.1.4.2 nathanw if (p->maxsize > IRDA_MAX_FRAME_SIZE)
1406 1.1.4.2 nathanw return EINVAL;
1407 1.1.4.2 nathanw sc->sc_params.maxsize = p->maxsize;
1408 1.1.4.2 nathanw }
1409 1.1.4.2 nathanw
1410 1.1.4.2 nathanw sc->sc_params = *p;
1411 1.1.4.2 nathanw
1412 1.1.4.2 nathanw if (speedblk != NULL) {
1413 1.1.4.2 nathanw usbd_status err;
1414 1.1.4.2 nathanw u_int8_t regmode;
1415 1.1.4.2 nathanw u_int8_t regbrate;
1416 1.1.4.2 nathanw
1417 1.1.4.2 nathanw sc->sc_speedrec = speedblk;
1418 1.1.4.2 nathanw
1419 1.1.4.2 nathanw regmode = STIR_BRMODE_MODEREG(speedblk->config);
1420 1.1.4.2 nathanw regbrate = STIR_BRMODE_BRATEREG(speedblk->config);
1421 1.1.4.2 nathanw
1422 1.1.4.2 nathanw /*
1423 1.1.4.2 nathanw * FFSPRST must be set to enable the FIFO.
1424 1.1.4.2 nathanw */
1425 1.1.4.2 nathanw regmode |= STIR_RMODE_FFSPRST;
1426 1.1.4.2 nathanw
1427 1.1.4.4 nathanw DPRINTFN(10, ("%s: setting BRATE = %x\n", __func__,
1428 1.1.4.2 nathanw (unsigned int)regbrate));
1429 1.1.4.2 nathanw err = ustir_write_reg(sc, STIR_REG_BRATE, regbrate);
1430 1.1.4.7 thorpej if (err == USBD_NORMAL_COMPLETION) {
1431 1.1.4.4 nathanw DPRINTFN(10, ("%s: setting MODE = %x\n", __func__,
1432 1.1.4.2 nathanw (unsigned int)regmode));
1433 1.1.4.2 nathanw err = ustir_write_reg(sc, STIR_REG_MODE, regmode);
1434 1.1.4.2 nathanw }
1435 1.1.4.7 thorpej if (err != USBD_NORMAL_COMPLETION) {
1436 1.1.4.2 nathanw DPRINTFN(10, ("%s: error setting register: %s\n",
1437 1.1.4.4 nathanw __func__, usbd_errstr(err)));
1438 1.1.4.2 nathanw return EIO;
1439 1.1.4.2 nathanw }
1440 1.1.4.2 nathanw }
1441 1.1.4.2 nathanw
1442 1.1.4.2 nathanw return 0;
1443 1.1.4.2 nathanw }
1444 1.1.4.2 nathanw
1445 1.1.4.2 nathanw Static int
1446 1.1.4.2 nathanw ustir_get_speeds(void *h, int *speeds)
1447 1.1.4.2 nathanw {
1448 1.1.4.2 nathanw struct ustir_softc *sc = h;
1449 1.1.4.2 nathanw
1450 1.1.4.4 nathanw DPRINTFN(0, ("%s: sc=%p\n", __func__, sc));
1451 1.1.4.2 nathanw
1452 1.1.4.2 nathanw if (sc->sc_dying)
1453 1.1.4.2 nathanw return EIO;
1454 1.1.4.2 nathanw
1455 1.1.4.2 nathanw /* All these speeds are supported */
1456 1.1.4.2 nathanw *speeds = IRDA_SPEED_4000000 |
1457 1.1.4.2 nathanw IRDA_SPEED_1152000 |
1458 1.1.4.2 nathanw IRDA_SPEED_576000 |
1459 1.1.4.2 nathanw IRDA_SPEED_115200 |
1460 1.1.4.2 nathanw IRDA_SPEED_57600 |
1461 1.1.4.2 nathanw IRDA_SPEED_38400 |
1462 1.1.4.2 nathanw IRDA_SPEED_19200 |
1463 1.1.4.2 nathanw IRDA_SPEED_9600 |
1464 1.1.4.2 nathanw IRDA_SPEED_2400;
1465 1.1.4.2 nathanw
1466 1.1.4.2 nathanw return 0;
1467 1.1.4.2 nathanw }
1468 1.1.4.2 nathanw
1469 1.1.4.2 nathanw Static int
1470 1.1.4.2 nathanw ustir_get_turnarounds(void *h, int *turnarounds)
1471 1.1.4.2 nathanw {
1472 1.1.4.2 nathanw struct ustir_softc *sc = h;
1473 1.1.4.2 nathanw
1474 1.1.4.4 nathanw DPRINTFN(0, ("%s: sc=%p\n", __func__, sc));
1475 1.1.4.2 nathanw
1476 1.1.4.2 nathanw if (sc->sc_dying)
1477 1.1.4.2 nathanw return EIO;
1478 1.1.4.2 nathanw
1479 1.1.4.2 nathanw /*
1480 1.1.4.2 nathanw * Documentation is on the light side with respect to
1481 1.1.4.2 nathanw * turnaround time for this device.
1482 1.1.4.2 nathanw */
1483 1.1.4.2 nathanw *turnarounds = IRDA_TURNT_10000;
1484 1.1.4.2 nathanw
1485 1.1.4.2 nathanw return 0;
1486 1.1.4.2 nathanw }
1487