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