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