uirda.c revision 1.9.2.4 1 1.9.2.4 nathanw /* $NetBSD: uirda.c,v 1.9.2.4 2002/08/27 23:47:13 nathanw Exp $ */
2 1.9.2.2 nathanw
3 1.9.2.2 nathanw /*
4 1.9.2.2 nathanw * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 1.9.2.2 nathanw * All rights reserved.
6 1.9.2.2 nathanw *
7 1.9.2.2 nathanw * This code is derived from software contributed to The NetBSD Foundation
8 1.9.2.2 nathanw * by Lennart Augustsson (lennart (at) augustsson.net).
9 1.9.2.2 nathanw *
10 1.9.2.2 nathanw * Redistribution and use in source and binary forms, with or without
11 1.9.2.2 nathanw * modification, are permitted provided that the following conditions
12 1.9.2.2 nathanw * are met:
13 1.9.2.2 nathanw * 1. Redistributions of source code must retain the above copyright
14 1.9.2.2 nathanw * notice, this list of conditions and the following disclaimer.
15 1.9.2.2 nathanw * 2. Redistributions in binary form must reproduce the above copyright
16 1.9.2.2 nathanw * notice, this list of conditions and the following disclaimer in the
17 1.9.2.2 nathanw * documentation and/or other materials provided with the distribution.
18 1.9.2.2 nathanw * 3. All advertising materials mentioning features or use of this software
19 1.9.2.2 nathanw * must display the following acknowledgement:
20 1.9.2.2 nathanw * This product includes software developed by the NetBSD
21 1.9.2.2 nathanw * Foundation, Inc. and its contributors.
22 1.9.2.2 nathanw * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.9.2.2 nathanw * contributors may be used to endorse or promote products derived
24 1.9.2.2 nathanw * from this software without specific prior written permission.
25 1.9.2.2 nathanw *
26 1.9.2.2 nathanw * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.9.2.2 nathanw * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.9.2.2 nathanw * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.9.2.2 nathanw * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.9.2.2 nathanw * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.9.2.2 nathanw * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.9.2.2 nathanw * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.9.2.2 nathanw * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.9.2.2 nathanw * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.9.2.2 nathanw * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.9.2.2 nathanw * POSSIBILITY OF SUCH DAMAGE.
37 1.9.2.2 nathanw */
38 1.9.2.2 nathanw
39 1.9.2.2 nathanw #include <sys/cdefs.h>
40 1.9.2.4 nathanw __KERNEL_RCSID(0, "$NetBSD: uirda.c,v 1.9.2.4 2002/08/27 23:47:13 nathanw Exp $");
41 1.9.2.2 nathanw
42 1.9.2.2 nathanw #include <sys/param.h>
43 1.9.2.2 nathanw #include <sys/systm.h>
44 1.9.2.2 nathanw #include <sys/kernel.h>
45 1.9.2.2 nathanw #include <sys/device.h>
46 1.9.2.2 nathanw #include <sys/lock.h>
47 1.9.2.2 nathanw #include <sys/ioctl.h>
48 1.9.2.2 nathanw #include <sys/conf.h>
49 1.9.2.2 nathanw #include <sys/file.h>
50 1.9.2.2 nathanw #include <sys/poll.h>
51 1.9.2.2 nathanw #include <sys/select.h>
52 1.9.2.2 nathanw #include <sys/proc.h>
53 1.9.2.2 nathanw
54 1.9.2.2 nathanw #include <dev/usb/usb.h>
55 1.9.2.2 nathanw #include <dev/usb/usbdi.h>
56 1.9.2.2 nathanw #include <dev/usb/usbdi_util.h>
57 1.9.2.2 nathanw #include <dev/usb/usbdevs.h>
58 1.9.2.2 nathanw
59 1.9.2.2 nathanw #include <dev/ir/ir.h>
60 1.9.2.2 nathanw #include <dev/ir/irdaio.h>
61 1.9.2.2 nathanw #include <dev/ir/irframevar.h>
62 1.9.2.2 nathanw
63 1.9.2.4 nathanw #ifdef UIRDA_DEBUG
64 1.9.2.2 nathanw #define DPRINTF(x) if (uirdadebug) logprintf x
65 1.9.2.2 nathanw #define DPRINTFN(n,x) if (uirdadebug>(n)) logprintf x
66 1.9.2.2 nathanw int uirdadebug = 0;
67 1.9.2.2 nathanw #else
68 1.9.2.2 nathanw #define DPRINTF(x)
69 1.9.2.2 nathanw #define DPRINTFN(n,x)
70 1.9.2.2 nathanw #endif
71 1.9.2.2 nathanw
72 1.9.2.2 nathanw /*
73 1.9.2.2 nathanw * Protocol related definitions
74 1.9.2.2 nathanw */
75 1.9.2.2 nathanw
76 1.9.2.2 nathanw #define UIRDA_INPUT_HEADER_SIZE 1
77 1.9.2.2 nathanw /* Inbound header byte */
78 1.9.2.2 nathanw #define UIRDA_MEDIA_BUSY 0x80
79 1.9.2.2 nathanw #define UIRDA_SPEED_MASK 0x0f
80 1.9.2.2 nathanw #define UIRDA_NO_SPEED 0x00
81 1.9.2.2 nathanw #define UIRDA_2400 0x01
82 1.9.2.2 nathanw #define UIRDA_9600 0x02
83 1.9.2.2 nathanw #define UIRDA_19200 0x03
84 1.9.2.2 nathanw #define UIRDA_38400 0x04
85 1.9.2.2 nathanw #define UIRDA_57600 0x05
86 1.9.2.2 nathanw #define UIRDA_115200 0x06
87 1.9.2.2 nathanw #define UIRDA_576000 0x07
88 1.9.2.2 nathanw #define UIRDA_1152000 0x08
89 1.9.2.2 nathanw #define UIRDA_4000000 0x09
90 1.9.2.2 nathanw
91 1.9.2.2 nathanw #define UIRDA_OUTPUT_HEADER_SIZE 1
92 1.9.2.2 nathanw /* Outbound header byte */
93 1.9.2.2 nathanw #define UIRDA_EB_NO_CHANGE 0x00
94 1.9.2.2 nathanw #define UIRDA_EB_48 0x10
95 1.9.2.2 nathanw #define UIRDA_EB_24 0x20
96 1.9.2.2 nathanw #define UIRDA_EB_12 0x30
97 1.9.2.2 nathanw #define UIRDA_EB_6 0x40
98 1.9.2.2 nathanw #define UIRDA_EB_3 0x50
99 1.9.2.2 nathanw #define UIRDA_EB_2 0x60
100 1.9.2.2 nathanw #define UIRDA_EB_1 0x70
101 1.9.2.2 nathanw #define UIRDA_EB_0 0x80
102 1.9.2.2 nathanw /* Speeds as above */
103 1.9.2.2 nathanw
104 1.9.2.2 nathanw /* Class specific requests */
105 1.9.2.2 nathanw #define UR_IRDA_RECEIVING 0x01 /* Receive in progress? */
106 1.9.2.2 nathanw #define UR_IRDA_CHECK_MEDIA_BUSY 0x03
107 1.9.2.2 nathanw #define UR_IRDA_SET_RATE_SNIFF 0x04 /* opt */
108 1.9.2.2 nathanw #define UR_IRDA_SET_UNICAST_LIST 0x05 /* opt */
109 1.9.2.2 nathanw #define UR_IRDA_GET_DESC 0x06
110 1.9.2.2 nathanw
111 1.9.2.2 nathanw typedef struct {
112 1.9.2.2 nathanw uByte bLength;
113 1.9.2.2 nathanw uByte bDescriptorType;
114 1.9.2.2 nathanw #define UDESC_IRDA 0x21
115 1.9.2.2 nathanw uWord bcdSpecRevision;
116 1.9.2.2 nathanw uByte bmDataSize;
117 1.9.2.2 nathanw #define UI_DS_2048 0x20
118 1.9.2.2 nathanw #define UI_DS_1024 0x10
119 1.9.2.2 nathanw #define UI_DS_512 0x08
120 1.9.2.2 nathanw #define UI_DS_256 0x04
121 1.9.2.2 nathanw #define UI_DS_128 0x02
122 1.9.2.2 nathanw #define UI_DS_64 0x01
123 1.9.2.2 nathanw uByte bmWindowSize;
124 1.9.2.2 nathanw #define UI_WS_7 0x40
125 1.9.2.2 nathanw #define UI_WS_6 0x20
126 1.9.2.2 nathanw #define UI_WS_5 0x10
127 1.9.2.2 nathanw #define UI_WS_4 0x08
128 1.9.2.2 nathanw #define UI_WS_3 0x04
129 1.9.2.2 nathanw #define UI_WS_2 0x02
130 1.9.2.2 nathanw #define UI_WS_1 0x01
131 1.9.2.2 nathanw uByte bmMinTurnaroundTime;
132 1.9.2.2 nathanw #define UI_TA_0 0x80
133 1.9.2.2 nathanw #define UI_TA_10 0x40
134 1.9.2.2 nathanw #define UI_TA_50 0x20
135 1.9.2.2 nathanw #define UI_TA_100 0x10
136 1.9.2.2 nathanw #define UI_TA_500 0x08
137 1.9.2.2 nathanw #define UI_TA_1000 0x04
138 1.9.2.2 nathanw #define UI_TA_5000 0x02
139 1.9.2.2 nathanw #define UI_TA_10000 0x01
140 1.9.2.2 nathanw uWord wBaudRate;
141 1.9.2.2 nathanw #define UI_BR_4000000 0x0100
142 1.9.2.2 nathanw #define UI_BR_1152000 0x0080
143 1.9.2.2 nathanw #define UI_BR_576000 0x0040
144 1.9.2.2 nathanw #define UI_BR_115200 0x0020
145 1.9.2.2 nathanw #define UI_BR_57600 0x0010
146 1.9.2.2 nathanw #define UI_BR_38400 0x0008
147 1.9.2.2 nathanw #define UI_BR_19200 0x0004
148 1.9.2.2 nathanw #define UI_BR_9600 0x0002
149 1.9.2.2 nathanw #define UI_BR_2400 0x0001
150 1.9.2.2 nathanw uByte bmAdditionalBOFs;
151 1.9.2.2 nathanw #define UI_EB_0 0x80
152 1.9.2.2 nathanw #define UI_EB_1 0x40
153 1.9.2.2 nathanw #define UI_EB_2 0x20
154 1.9.2.2 nathanw #define UI_EB_3 0x10
155 1.9.2.2 nathanw #define UI_EB_6 0x08
156 1.9.2.2 nathanw #define UI_EB_12 0x04
157 1.9.2.2 nathanw #define UI_EB_24 0x02
158 1.9.2.2 nathanw #define UI_EB_48 0x01
159 1.9.2.2 nathanw uByte bIrdaSniff;
160 1.9.2.2 nathanw uByte bMaxUnicastList;
161 1.9.2.2 nathanw } UPACKED usb_irda_descriptor_t;
162 1.9.2.2 nathanw #define USB_IRDA_DESCRIPTOR_SIZE 12
163 1.9.2.2 nathanw
164 1.9.2.2 nathanw
165 1.9.2.2 nathanw #define UIRDA_NEBOFS 8
166 1.9.2.2 nathanw static struct {
167 1.9.2.2 nathanw int count;
168 1.9.2.2 nathanw int mask;
169 1.9.2.2 nathanw int header;
170 1.9.2.2 nathanw } uirda_ebofs[UIRDA_NEBOFS] = {
171 1.9.2.2 nathanw { 0, UI_EB_0, UIRDA_EB_0 },
172 1.9.2.2 nathanw { 1, UI_EB_1, UIRDA_EB_1 },
173 1.9.2.2 nathanw { 2, UI_EB_2, UIRDA_EB_2 },
174 1.9.2.2 nathanw { 3, UI_EB_3, UIRDA_EB_3 },
175 1.9.2.2 nathanw { 6, UI_EB_6, UIRDA_EB_6 },
176 1.9.2.2 nathanw { 12, UI_EB_12, UIRDA_EB_12 },
177 1.9.2.2 nathanw { 24, UI_EB_24, UIRDA_EB_24 },
178 1.9.2.2 nathanw { 48, UI_EB_48, UIRDA_EB_48 }
179 1.9.2.2 nathanw };
180 1.9.2.2 nathanw
181 1.9.2.2 nathanw #define UIRDA_NSPEEDS 9
182 1.9.2.2 nathanw static struct {
183 1.9.2.2 nathanw int speed;
184 1.9.2.2 nathanw int mask;
185 1.9.2.2 nathanw int header;
186 1.9.2.2 nathanw } uirda_speeds[UIRDA_NSPEEDS] = {
187 1.9.2.2 nathanw { 4000000, UI_BR_4000000, UIRDA_4000000 },
188 1.9.2.2 nathanw { 1152000, UI_BR_1152000, UIRDA_1152000 },
189 1.9.2.2 nathanw { 576000, UI_BR_576000, UIRDA_576000 },
190 1.9.2.2 nathanw { 115200, UI_BR_115200, UIRDA_115200 },
191 1.9.2.2 nathanw { 57600, UI_BR_57600, UIRDA_57600 },
192 1.9.2.2 nathanw { 38400, UI_BR_38400, UIRDA_38400 },
193 1.9.2.2 nathanw { 19200, UI_BR_19200, UIRDA_19200 },
194 1.9.2.2 nathanw { 9600, UI_BR_9600, UIRDA_9600 },
195 1.9.2.2 nathanw { 2400, UI_BR_2400, UIRDA_2400 },
196 1.9.2.2 nathanw };
197 1.9.2.2 nathanw
198 1.9.2.2 nathanw struct uirda_softc {
199 1.9.2.2 nathanw USBBASEDEVICE sc_dev;
200 1.9.2.2 nathanw usbd_device_handle sc_udev;
201 1.9.2.2 nathanw usbd_interface_handle sc_iface;
202 1.9.2.2 nathanw
203 1.9.2.2 nathanw struct lock sc_rd_buf_lk;
204 1.9.2.2 nathanw u_int8_t *sc_rd_buf;
205 1.9.2.2 nathanw int sc_rd_addr;
206 1.9.2.2 nathanw usbd_pipe_handle sc_rd_pipe;
207 1.9.2.2 nathanw usbd_xfer_handle sc_rd_xfer;
208 1.9.2.2 nathanw struct selinfo sc_rd_sel;
209 1.9.2.2 nathanw u_int sc_rd_count;
210 1.9.2.2 nathanw u_char sc_rd_err;
211 1.9.2.2 nathanw
212 1.9.2.2 nathanw struct lock sc_wr_buf_lk;
213 1.9.2.2 nathanw u_int8_t *sc_wr_buf;
214 1.9.2.2 nathanw int sc_wr_addr;
215 1.9.2.2 nathanw usbd_xfer_handle sc_wr_xfer;
216 1.9.2.2 nathanw usbd_pipe_handle sc_wr_pipe;
217 1.9.2.2 nathanw int sc_wr_hdr;
218 1.9.2.2 nathanw
219 1.9.2.2 nathanw struct device *sc_child;
220 1.9.2.2 nathanw struct irda_params sc_params;
221 1.9.2.2 nathanw usb_irda_descriptor_t sc_irdadesc;
222 1.9.2.2 nathanw
223 1.9.2.2 nathanw int sc_refcnt;
224 1.9.2.2 nathanw char sc_dying;
225 1.9.2.2 nathanw };
226 1.9.2.2 nathanw
227 1.9.2.2 nathanw #define UIRDA_WR_TIMEOUT 200
228 1.9.2.2 nathanw
229 1.9.2.2 nathanw int uirda_open(void *h, int flag, int mode, usb_proc_ptr p);
230 1.9.2.2 nathanw int uirda_close(void *h, int flag, int mode, usb_proc_ptr p);
231 1.9.2.2 nathanw int uirda_read(void *h, struct uio *uio, int flag);
232 1.9.2.2 nathanw int uirda_write(void *h, struct uio *uio, int flag);
233 1.9.2.2 nathanw int uirda_set_params(void *h, struct irda_params *params);
234 1.9.2.2 nathanw int uirda_get_speeds(void *h, int *speeds);
235 1.9.2.2 nathanw int uirda_get_turnarounds(void *h, int *times);
236 1.9.2.2 nathanw int uirda_poll(void *h, int events, usb_proc_ptr p);
237 1.9.2.2 nathanw
238 1.9.2.2 nathanw struct irframe_methods uirda_methods = {
239 1.9.2.3 nathanw uirda_open, uirda_close, uirda_read, uirda_write, uirda_poll,
240 1.9.2.2 nathanw uirda_set_params, uirda_get_speeds, uirda_get_turnarounds
241 1.9.2.2 nathanw };
242 1.9.2.2 nathanw
243 1.9.2.2 nathanw void uirda_rd_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
244 1.9.2.2 nathanw usbd_status status);
245 1.9.2.2 nathanw usbd_status uirda_start_read(struct uirda_softc *sc);
246 1.9.2.2 nathanw
247 1.9.2.2 nathanw usb_descriptor_t *usb_find_desc(usbd_device_handle dev, int type);
248 1.9.2.2 nathanw
249 1.9.2.3 nathanw /*
250 1.9.2.2 nathanw * These devices don't quite follow the spec. Speed changing is broken
251 1.9.2.2 nathanw * and they don't handle windows.
252 1.9.2.2 nathanw * But we change speed in a safe way, and don't use windows now.
253 1.9.2.2 nathanw * Some devices also seem to have an interrupt pipe that can be ignored.
254 1.9.2.2 nathanw *
255 1.9.2.2 nathanw * Table information taken from Linux driver.
256 1.9.2.2 nathanw */
257 1.9.2.2 nathanw Static const struct usb_devno uirda_devs[] = {
258 1.9.2.2 nathanw { USB_VENDOR_ACTISYS, USB_PRODUCT_ACTISYS_IR2000U },
259 1.9.2.2 nathanw { USB_VENDOR_EXTENDED, USB_PRODUCT_EXTENDED_XTNDACCESS },
260 1.9.2.2 nathanw { USB_VENDOR_KAWATSU, USB_PRODUCT_KAWATSU_KC180 },
261 1.9.2.2 nathanw };
262 1.9.2.2 nathanw #define uirda_lookup(v, p) (usb_lookup(uirda_devs, v, p))
263 1.9.2.2 nathanw
264 1.9.2.2 nathanw USB_DECLARE_DRIVER(uirda);
265 1.9.2.2 nathanw
266 1.9.2.2 nathanw USB_MATCH(uirda)
267 1.9.2.2 nathanw {
268 1.9.2.2 nathanw USB_MATCH_START(uirda, uaa);
269 1.9.2.2 nathanw usb_interface_descriptor_t *id;
270 1.9.2.2 nathanw
271 1.9.2.2 nathanw DPRINTFN(50,("uirda_match\n"));
272 1.9.2.2 nathanw
273 1.9.2.2 nathanw if (uaa->iface == NULL)
274 1.9.2.2 nathanw return (UMATCH_NONE);
275 1.9.2.2 nathanw
276 1.9.2.2 nathanw if (uirda_lookup(uaa->vendor, uaa->product) != NULL)
277 1.9.2.2 nathanw return (UMATCH_VENDOR_PRODUCT);
278 1.9.2.2 nathanw
279 1.9.2.2 nathanw id = usbd_get_interface_descriptor(uaa->iface);
280 1.9.2.2 nathanw if (id != NULL &&
281 1.9.2.2 nathanw id->bInterfaceClass == UICLASS_APPL_SPEC &&
282 1.9.2.2 nathanw id->bInterfaceSubClass == UISUBCLASS_IRDA &&
283 1.9.2.2 nathanw id->bInterfaceProtocol == UIPROTO_IRDA)
284 1.9.2.2 nathanw return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
285 1.9.2.2 nathanw return (UMATCH_NONE);
286 1.9.2.2 nathanw }
287 1.9.2.2 nathanw
288 1.9.2.2 nathanw USB_ATTACH(uirda)
289 1.9.2.2 nathanw {
290 1.9.2.2 nathanw USB_ATTACH_START(uirda, sc, uaa);
291 1.9.2.2 nathanw usbd_device_handle dev = uaa->device;
292 1.9.2.2 nathanw usbd_interface_handle iface = uaa->iface;
293 1.9.2.2 nathanw char devinfo[1024];
294 1.9.2.2 nathanw usb_endpoint_descriptor_t *ed;
295 1.9.2.2 nathanw usbd_status err;
296 1.9.2.2 nathanw u_int8_t epcount;
297 1.9.2.2 nathanw u_int specrev;
298 1.9.2.2 nathanw int i;
299 1.9.2.2 nathanw struct ir_attach_args ia;
300 1.9.2.2 nathanw
301 1.9.2.2 nathanw DPRINTFN(10,("uirda_attach: sc=%p\n", sc));
302 1.9.2.2 nathanw
303 1.9.2.2 nathanw usbd_devinfo(dev, 0, devinfo);
304 1.9.2.2 nathanw USB_ATTACH_SETUP;
305 1.9.2.2 nathanw printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
306 1.9.2.2 nathanw
307 1.9.2.2 nathanw sc->sc_udev = dev;
308 1.9.2.2 nathanw sc->sc_iface = iface;
309 1.9.2.2 nathanw
310 1.9.2.2 nathanw epcount = 0;
311 1.9.2.2 nathanw (void)usbd_endpoint_count(iface, &epcount);
312 1.9.2.2 nathanw
313 1.9.2.2 nathanw sc->sc_rd_addr = -1;
314 1.9.2.2 nathanw sc->sc_wr_addr = -1;
315 1.9.2.2 nathanw for (i = 0; i < epcount; i++) {
316 1.9.2.2 nathanw ed = usbd_interface2endpoint_descriptor(iface, i);
317 1.9.2.2 nathanw if (ed == NULL) {
318 1.9.2.2 nathanw printf("%s: couldn't get ep %d\n",
319 1.9.2.2 nathanw USBDEVNAME(sc->sc_dev), i);
320 1.9.2.2 nathanw USB_ATTACH_ERROR_RETURN;
321 1.9.2.2 nathanw }
322 1.9.2.2 nathanw if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
323 1.9.2.2 nathanw UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
324 1.9.2.2 nathanw sc->sc_rd_addr = ed->bEndpointAddress;
325 1.9.2.2 nathanw } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
326 1.9.2.2 nathanw UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
327 1.9.2.2 nathanw sc->sc_wr_addr = ed->bEndpointAddress;
328 1.9.2.2 nathanw }
329 1.9.2.2 nathanw }
330 1.9.2.2 nathanw if (sc->sc_rd_addr == -1 || sc->sc_wr_addr == -1) {
331 1.9.2.2 nathanw printf("%s: missing endpoint\n", USBDEVNAME(sc->sc_dev));
332 1.9.2.2 nathanw USB_ATTACH_ERROR_RETURN;
333 1.9.2.2 nathanw }
334 1.9.2.2 nathanw
335 1.9.2.2 nathanw /* Get the IrDA descriptor */
336 1.9.2.2 nathanw err = usbd_get_desc(sc->sc_udev, UDESC_IRDA, 0,
337 1.9.2.2 nathanw USB_IRDA_DESCRIPTOR_SIZE, &sc->sc_irdadesc);
338 1.9.2.2 nathanw if (err) {
339 1.9.2.2 nathanw /* maybe it's embedded in the config desc? */
340 1.9.2.2 nathanw void *d = usb_find_desc(sc->sc_udev, UDESC_IRDA);
341 1.9.2.2 nathanw if (d == NULL) {
342 1.9.2.2 nathanw printf("%s: Cannot get IrDA descriptor\n",
343 1.9.2.2 nathanw USBDEVNAME(sc->sc_dev));
344 1.9.2.2 nathanw USB_ATTACH_ERROR_RETURN;
345 1.9.2.2 nathanw }
346 1.9.2.2 nathanw memcpy(&sc->sc_irdadesc, d, USB_IRDA_DESCRIPTOR_SIZE);
347 1.9.2.2 nathanw }
348 1.9.2.2 nathanw DPRINTF(("uirda_attach: bmDataSize=0x%02x bmWindowSize=0x%02x "
349 1.9.2.2 nathanw "bmMinTurnaroundTime=0x%02x wBaudRate=0x%04x "
350 1.9.2.2 nathanw "bmAdditionalBOFs=0x%02x bIrdaSniff=%d bMaxUnicastList=%d\n",
351 1.9.2.2 nathanw sc->sc_irdadesc.bmDataSize,
352 1.9.2.2 nathanw sc->sc_irdadesc.bmWindowSize,
353 1.9.2.2 nathanw sc->sc_irdadesc.bmMinTurnaroundTime,
354 1.9.2.2 nathanw UGETW(sc->sc_irdadesc.wBaudRate),
355 1.9.2.2 nathanw sc->sc_irdadesc.bmAdditionalBOFs,
356 1.9.2.2 nathanw sc->sc_irdadesc.bIrdaSniff,
357 1.9.2.2 nathanw sc->sc_irdadesc.bMaxUnicastList));
358 1.9.2.2 nathanw
359 1.9.2.2 nathanw specrev = UGETW(sc->sc_irdadesc.bcdSpecRevision);
360 1.9.2.2 nathanw printf("%s: USB-IrDA protocol version %x.%02x\n",
361 1.9.2.2 nathanw USBDEVNAME(sc->sc_dev), specrev >> 8, specrev & 0xff);
362 1.9.2.2 nathanw
363 1.9.2.2 nathanw DPRINTFN(10, ("uirda_attach: %p\n", sc->sc_udev));
364 1.9.2.2 nathanw
365 1.9.2.2 nathanw usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
366 1.9.2.2 nathanw USBDEV(sc->sc_dev));
367 1.9.2.2 nathanw
368 1.9.2.2 nathanw lockinit(&sc->sc_wr_buf_lk, PZERO, "iirwrl", 0, 0);
369 1.9.2.2 nathanw lockinit(&sc->sc_rd_buf_lk, PZERO, "uirrdl", 0, 0);
370 1.9.2.2 nathanw
371 1.9.2.2 nathanw ia.ia_type = IR_TYPE_IRFRAME;
372 1.9.2.2 nathanw ia.ia_methods = &uirda_methods;
373 1.9.2.2 nathanw ia.ia_handle = sc;
374 1.9.2.2 nathanw
375 1.9.2.2 nathanw sc->sc_child = config_found(self, &ia, ir_print);
376 1.9.2.2 nathanw
377 1.9.2.2 nathanw USB_ATTACH_SUCCESS_RETURN;
378 1.9.2.2 nathanw }
379 1.9.2.2 nathanw
380 1.9.2.2 nathanw USB_DETACH(uirda)
381 1.9.2.2 nathanw {
382 1.9.2.2 nathanw USB_DETACH_START(uirda, sc);
383 1.9.2.2 nathanw int s;
384 1.9.2.2 nathanw int rv = 0;
385 1.9.2.2 nathanw
386 1.9.2.2 nathanw DPRINTF(("uirda_detach: sc=%p flags=%d\n", sc, flags));
387 1.9.2.2 nathanw
388 1.9.2.2 nathanw sc->sc_dying = 1;
389 1.9.2.2 nathanw /* Abort all pipes. Causes processes waiting for transfer to wake. */
390 1.9.2.2 nathanw if (sc->sc_rd_pipe != NULL) {
391 1.9.2.2 nathanw usbd_abort_pipe(sc->sc_rd_pipe);
392 1.9.2.2 nathanw usbd_close_pipe(sc->sc_rd_pipe);
393 1.9.2.2 nathanw sc->sc_rd_pipe = NULL;
394 1.9.2.2 nathanw }
395 1.9.2.2 nathanw if (sc->sc_wr_pipe != NULL) {
396 1.9.2.2 nathanw usbd_abort_pipe(sc->sc_wr_pipe);
397 1.9.2.2 nathanw usbd_close_pipe(sc->sc_wr_pipe);
398 1.9.2.2 nathanw sc->sc_wr_pipe = NULL;
399 1.9.2.2 nathanw }
400 1.9.2.2 nathanw wakeup(&sc->sc_rd_count);
401 1.9.2.2 nathanw
402 1.9.2.2 nathanw s = splusb();
403 1.9.2.2 nathanw if (--sc->sc_refcnt >= 0) {
404 1.9.2.2 nathanw /* Wait for processes to go away. */
405 1.9.2.2 nathanw usb_detach_wait(USBDEV(sc->sc_dev));
406 1.9.2.2 nathanw }
407 1.9.2.2 nathanw splx(s);
408 1.9.2.2 nathanw
409 1.9.2.2 nathanw if (sc->sc_child != NULL) {
410 1.9.2.2 nathanw rv = config_detach(sc->sc_child, flags);
411 1.9.2.2 nathanw sc->sc_child = NULL;
412 1.9.2.2 nathanw }
413 1.9.2.2 nathanw
414 1.9.2.2 nathanw usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
415 1.9.2.2 nathanw USBDEV(sc->sc_dev));
416 1.9.2.2 nathanw
417 1.9.2.2 nathanw return (rv);
418 1.9.2.2 nathanw }
419 1.9.2.2 nathanw
420 1.9.2.2 nathanw int
421 1.9.2.2 nathanw uirda_activate(device_ptr_t self, enum devact act)
422 1.9.2.2 nathanw {
423 1.9.2.2 nathanw struct uirda_softc *sc = (struct uirda_softc *)self;
424 1.9.2.2 nathanw int error = 0;
425 1.9.2.2 nathanw
426 1.9.2.2 nathanw switch (act) {
427 1.9.2.2 nathanw case DVACT_ACTIVATE:
428 1.9.2.2 nathanw return (EOPNOTSUPP);
429 1.9.2.2 nathanw break;
430 1.9.2.2 nathanw
431 1.9.2.2 nathanw case DVACT_DEACTIVATE:
432 1.9.2.2 nathanw sc->sc_dying = 1;
433 1.9.2.2 nathanw if (sc->sc_child != NULL)
434 1.9.2.2 nathanw error = config_deactivate(sc->sc_child);
435 1.9.2.2 nathanw break;
436 1.9.2.2 nathanw }
437 1.9.2.2 nathanw return (error);
438 1.9.2.2 nathanw }
439 1.9.2.2 nathanw
440 1.9.2.2 nathanw int
441 1.9.2.2 nathanw uirda_open(void *h, int flag, int mode, usb_proc_ptr p)
442 1.9.2.2 nathanw {
443 1.9.2.2 nathanw struct uirda_softc *sc = h;
444 1.9.2.2 nathanw int error;
445 1.9.2.2 nathanw usbd_status err;
446 1.9.2.2 nathanw
447 1.9.2.3 nathanw DPRINTF(("%s: sc=%p\n", __func__, sc));
448 1.9.2.2 nathanw
449 1.9.2.2 nathanw err = usbd_open_pipe(sc->sc_iface, sc->sc_rd_addr, 0, &sc->sc_rd_pipe);
450 1.9.2.2 nathanw if (err) {
451 1.9.2.2 nathanw error = EIO;
452 1.9.2.2 nathanw goto bad1;
453 1.9.2.2 nathanw }
454 1.9.2.2 nathanw err = usbd_open_pipe(sc->sc_iface, sc->sc_wr_addr, 0, &sc->sc_wr_pipe);
455 1.9.2.2 nathanw if (err) {
456 1.9.2.2 nathanw error = EIO;
457 1.9.2.2 nathanw goto bad2;
458 1.9.2.2 nathanw }
459 1.9.2.2 nathanw sc->sc_rd_xfer = usbd_alloc_xfer(sc->sc_udev);
460 1.9.2.2 nathanw if (sc->sc_rd_xfer == NULL) {
461 1.9.2.2 nathanw error = ENOMEM;
462 1.9.2.2 nathanw goto bad3;
463 1.9.2.2 nathanw }
464 1.9.2.2 nathanw sc->sc_wr_xfer = usbd_alloc_xfer(sc->sc_udev);
465 1.9.2.2 nathanw if (sc->sc_wr_xfer == NULL) {
466 1.9.2.2 nathanw error = ENOMEM;
467 1.9.2.2 nathanw goto bad4;
468 1.9.2.2 nathanw }
469 1.9.2.2 nathanw sc->sc_rd_buf = usbd_alloc_buffer(sc->sc_rd_xfer,
470 1.9.2.2 nathanw IRDA_MAX_FRAME_SIZE + UIRDA_INPUT_HEADER_SIZE);
471 1.9.2.2 nathanw if (sc->sc_rd_buf == NULL) {
472 1.9.2.2 nathanw error = ENOMEM;
473 1.9.2.2 nathanw goto bad5;
474 1.9.2.2 nathanw }
475 1.9.2.2 nathanw sc->sc_wr_buf = usbd_alloc_buffer(sc->sc_wr_xfer,
476 1.9.2.2 nathanw IRDA_MAX_FRAME_SIZE + UIRDA_OUTPUT_HEADER_SIZE);
477 1.9.2.2 nathanw if (sc->sc_wr_buf == NULL) {
478 1.9.2.2 nathanw error = ENOMEM;
479 1.9.2.2 nathanw goto bad5;
480 1.9.2.2 nathanw }
481 1.9.2.2 nathanw sc->sc_rd_count = 0;
482 1.9.2.2 nathanw sc->sc_rd_err = 0;
483 1.9.2.2 nathanw sc->sc_params.speed = 0;
484 1.9.2.2 nathanw sc->sc_params.ebofs = 0;
485 1.9.2.2 nathanw sc->sc_params.maxsize = IRDA_MAX_FRAME_SIZE;
486 1.9.2.2 nathanw sc->sc_wr_hdr = -1;
487 1.9.2.2 nathanw
488 1.9.2.2 nathanw err = uirda_start_read(sc);
489 1.9.2.2 nathanw /* XXX check err */
490 1.9.2.2 nathanw
491 1.9.2.2 nathanw return (0);
492 1.9.2.2 nathanw
493 1.9.2.2 nathanw bad5:
494 1.9.2.2 nathanw usbd_free_xfer(sc->sc_wr_xfer);
495 1.9.2.2 nathanw sc->sc_wr_xfer = NULL;
496 1.9.2.2 nathanw bad4:
497 1.9.2.2 nathanw usbd_free_xfer(sc->sc_rd_xfer);
498 1.9.2.2 nathanw sc->sc_rd_xfer = NULL;
499 1.9.2.2 nathanw bad3:
500 1.9.2.2 nathanw usbd_close_pipe(sc->sc_wr_pipe);
501 1.9.2.2 nathanw sc->sc_wr_pipe = NULL;
502 1.9.2.2 nathanw bad2:
503 1.9.2.2 nathanw usbd_close_pipe(sc->sc_rd_pipe);
504 1.9.2.2 nathanw sc->sc_rd_pipe = NULL;
505 1.9.2.2 nathanw bad1:
506 1.9.2.2 nathanw return (error);
507 1.9.2.2 nathanw }
508 1.9.2.2 nathanw
509 1.9.2.2 nathanw int
510 1.9.2.2 nathanw uirda_close(void *h, int flag, int mode, usb_proc_ptr p)
511 1.9.2.2 nathanw {
512 1.9.2.2 nathanw struct uirda_softc *sc = h;
513 1.9.2.2 nathanw
514 1.9.2.3 nathanw DPRINTF(("%s: sc=%p\n", __func__, sc));
515 1.9.2.2 nathanw
516 1.9.2.2 nathanw if (sc->sc_rd_pipe != NULL) {
517 1.9.2.2 nathanw usbd_abort_pipe(sc->sc_rd_pipe);
518 1.9.2.2 nathanw usbd_close_pipe(sc->sc_rd_pipe);
519 1.9.2.2 nathanw sc->sc_rd_pipe = NULL;
520 1.9.2.2 nathanw }
521 1.9.2.2 nathanw if (sc->sc_wr_pipe != NULL) {
522 1.9.2.2 nathanw usbd_abort_pipe(sc->sc_wr_pipe);
523 1.9.2.2 nathanw usbd_close_pipe(sc->sc_wr_pipe);
524 1.9.2.2 nathanw sc->sc_wr_pipe = NULL;
525 1.9.2.2 nathanw }
526 1.9.2.2 nathanw if (sc->sc_rd_xfer != NULL) {
527 1.9.2.2 nathanw usbd_free_xfer(sc->sc_rd_xfer);
528 1.9.2.2 nathanw sc->sc_rd_xfer = NULL;
529 1.9.2.2 nathanw sc->sc_rd_buf = NULL;
530 1.9.2.2 nathanw }
531 1.9.2.2 nathanw if (sc->sc_wr_xfer != NULL) {
532 1.9.2.2 nathanw usbd_free_xfer(sc->sc_wr_xfer);
533 1.9.2.2 nathanw sc->sc_wr_xfer = NULL;
534 1.9.2.2 nathanw sc->sc_wr_buf = NULL;
535 1.9.2.2 nathanw }
536 1.9.2.2 nathanw
537 1.9.2.2 nathanw return (0);
538 1.9.2.2 nathanw }
539 1.9.2.2 nathanw
540 1.9.2.2 nathanw int
541 1.9.2.2 nathanw uirda_read(void *h, struct uio *uio, int flag)
542 1.9.2.2 nathanw {
543 1.9.2.2 nathanw struct uirda_softc *sc = h;
544 1.9.2.2 nathanw usbd_status err;
545 1.9.2.2 nathanw int s;
546 1.9.2.2 nathanw int error;
547 1.9.2.2 nathanw u_int n;
548 1.9.2.2 nathanw
549 1.9.2.3 nathanw DPRINTFN(1,("%s: sc=%p\n", __func__, sc));
550 1.9.2.2 nathanw
551 1.9.2.2 nathanw if (sc->sc_dying)
552 1.9.2.2 nathanw return (EIO);
553 1.9.2.2 nathanw
554 1.9.2.2 nathanw #ifdef DIAGNOSTIC
555 1.9.2.2 nathanw if (sc->sc_rd_buf == NULL)
556 1.9.2.2 nathanw return (EINVAL);
557 1.9.2.2 nathanw #endif
558 1.9.2.2 nathanw
559 1.9.2.2 nathanw sc->sc_refcnt++;
560 1.9.2.2 nathanw
561 1.9.2.2 nathanw do {
562 1.9.2.2 nathanw s = splusb();
563 1.9.2.2 nathanw while (sc->sc_rd_count == 0) {
564 1.9.2.2 nathanw DPRINTFN(5,("uirda_read: calling tsleep()\n"));
565 1.9.2.2 nathanw error = tsleep(&sc->sc_rd_count, PZERO | PCATCH,
566 1.9.2.2 nathanw "uirdrd", 0);
567 1.9.2.2 nathanw if (sc->sc_dying)
568 1.9.2.2 nathanw error = EIO;
569 1.9.2.2 nathanw if (error) {
570 1.9.2.2 nathanw splx(s);
571 1.9.2.2 nathanw DPRINTF(("uirda_read: tsleep() = %d\n", error));
572 1.9.2.2 nathanw goto ret;
573 1.9.2.2 nathanw }
574 1.9.2.2 nathanw }
575 1.9.2.2 nathanw splx(s);
576 1.9.2.3 nathanw
577 1.9.2.2 nathanw lockmgr(&sc->sc_rd_buf_lk, LK_EXCLUSIVE, NULL);
578 1.9.2.2 nathanw n = sc->sc_rd_count - UIRDA_INPUT_HEADER_SIZE;
579 1.9.2.3 nathanw DPRINTFN(1,("%s: sc=%p n=%u, hdr=0x%02x\n", __func__,
580 1.9.2.2 nathanw sc, n, sc->sc_rd_buf[0]));
581 1.9.2.2 nathanw if (n > uio->uio_resid)
582 1.9.2.2 nathanw error = EINVAL;
583 1.9.2.2 nathanw else
584 1.9.2.2 nathanw error = uiomove(sc->sc_rd_buf+UIRDA_INPUT_HEADER_SIZE,
585 1.9.2.2 nathanw n, uio);
586 1.9.2.2 nathanw sc->sc_rd_count = 0;
587 1.9.2.2 nathanw lockmgr(&sc->sc_rd_buf_lk, LK_RELEASE, NULL);
588 1.9.2.3 nathanw
589 1.9.2.2 nathanw err = uirda_start_read(sc);
590 1.9.2.2 nathanw /* XXX check err */
591 1.9.2.2 nathanw
592 1.9.2.2 nathanw } while (n == 0);
593 1.9.2.2 nathanw
594 1.9.2.2 nathanw DPRINTFN(1,("uirda_read: return %d\n", error));
595 1.9.2.2 nathanw
596 1.9.2.2 nathanw ret:
597 1.9.2.2 nathanw if (--sc->sc_refcnt < 0)
598 1.9.2.2 nathanw usb_detach_wakeup(USBDEV(sc->sc_dev));
599 1.9.2.2 nathanw return (error);
600 1.9.2.2 nathanw }
601 1.9.2.2 nathanw
602 1.9.2.2 nathanw int
603 1.9.2.2 nathanw uirda_write(void *h, struct uio *uio, int flag)
604 1.9.2.2 nathanw {
605 1.9.2.2 nathanw struct uirda_softc *sc = h;
606 1.9.2.2 nathanw usbd_status err;
607 1.9.2.2 nathanw u_int32_t n;
608 1.9.2.2 nathanw int error = 0;
609 1.9.2.2 nathanw
610 1.9.2.3 nathanw DPRINTFN(1,("%s: sc=%p\n", __func__, sc));
611 1.9.2.2 nathanw
612 1.9.2.2 nathanw if (sc->sc_dying)
613 1.9.2.2 nathanw return (EIO);
614 1.9.2.2 nathanw
615 1.9.2.2 nathanw #ifdef DIAGNOSTIC
616 1.9.2.2 nathanw if (sc->sc_wr_buf == NULL)
617 1.9.2.2 nathanw return (EINVAL);
618 1.9.2.2 nathanw #endif
619 1.9.2.2 nathanw
620 1.9.2.2 nathanw n = uio->uio_resid;
621 1.9.2.2 nathanw if (n > sc->sc_params.maxsize)
622 1.9.2.2 nathanw return (EINVAL);
623 1.9.2.2 nathanw
624 1.9.2.2 nathanw sc->sc_refcnt++;
625 1.9.2.2 nathanw lockmgr(&sc->sc_wr_buf_lk, LK_EXCLUSIVE, NULL);
626 1.9.2.2 nathanw
627 1.9.2.2 nathanw sc->sc_wr_buf[0] = UIRDA_EB_NO_CHANGE | UIRDA_NO_SPEED;
628 1.9.2.2 nathanw error = uiomove(sc->sc_wr_buf + UIRDA_OUTPUT_HEADER_SIZE, n, uio);
629 1.9.2.2 nathanw if (!error) {
630 1.9.2.2 nathanw DPRINTFN(1, ("uirdawrite: transfer %d bytes\n", n));
631 1.9.2.2 nathanw
632 1.9.2.2 nathanw n++;
633 1.9.2.2 nathanw err = usbd_bulk_transfer(sc->sc_wr_xfer, sc->sc_wr_pipe,
634 1.9.2.2 nathanw USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
635 1.9.2.2 nathanw UIRDA_WR_TIMEOUT,
636 1.9.2.2 nathanw sc->sc_wr_buf, &n, "uirdawr");
637 1.9.2.2 nathanw DPRINTFN(2, ("uirdawrite: err=%d\n", err));
638 1.9.2.2 nathanw if (err) {
639 1.9.2.2 nathanw if (err == USBD_INTERRUPTED)
640 1.9.2.2 nathanw error = EINTR;
641 1.9.2.2 nathanw else if (err == USBD_TIMEOUT)
642 1.9.2.2 nathanw error = ETIMEDOUT;
643 1.9.2.2 nathanw else
644 1.9.2.2 nathanw error = EIO;
645 1.9.2.2 nathanw }
646 1.9.2.2 nathanw }
647 1.9.2.2 nathanw
648 1.9.2.2 nathanw lockmgr(&sc->sc_wr_buf_lk, LK_RELEASE, NULL);
649 1.9.2.2 nathanw if (--sc->sc_refcnt < 0)
650 1.9.2.2 nathanw usb_detach_wakeup(USBDEV(sc->sc_dev));
651 1.9.2.2 nathanw
652 1.9.2.3 nathanw DPRINTFN(1,("%s: sc=%p done\n", __func__, sc));
653 1.9.2.2 nathanw return (error);
654 1.9.2.2 nathanw }
655 1.9.2.2 nathanw
656 1.9.2.2 nathanw int
657 1.9.2.2 nathanw uirda_poll(void *h, int events, usb_proc_ptr p)
658 1.9.2.2 nathanw {
659 1.9.2.2 nathanw struct uirda_softc *sc = h;
660 1.9.2.2 nathanw int revents = 0;
661 1.9.2.2 nathanw int s;
662 1.9.2.2 nathanw
663 1.9.2.3 nathanw DPRINTFN(1,("%s: sc=%p\n", __func__, sc));
664 1.9.2.2 nathanw
665 1.9.2.2 nathanw s = splusb();
666 1.9.2.2 nathanw if (events & (POLLOUT | POLLWRNORM))
667 1.9.2.2 nathanw revents |= events & (POLLOUT | POLLWRNORM);
668 1.9.2.2 nathanw if (events & (POLLIN | POLLRDNORM)) {
669 1.9.2.2 nathanw if (sc->sc_rd_count != 0) {
670 1.9.2.3 nathanw DPRINTFN(2,("%s: have data\n", __func__));
671 1.9.2.2 nathanw revents |= events & (POLLIN | POLLRDNORM);
672 1.9.2.2 nathanw } else {
673 1.9.2.3 nathanw DPRINTFN(2,("%s: recording select\n", __func__));
674 1.9.2.2 nathanw selrecord(p, &sc->sc_rd_sel);
675 1.9.2.2 nathanw }
676 1.9.2.2 nathanw }
677 1.9.2.2 nathanw splx(s);
678 1.9.2.2 nathanw
679 1.9.2.2 nathanw return (revents);
680 1.9.2.2 nathanw }
681 1.9.2.2 nathanw
682 1.9.2.2 nathanw int
683 1.9.2.2 nathanw uirda_set_params(void *h, struct irda_params *p)
684 1.9.2.2 nathanw {
685 1.9.2.2 nathanw struct uirda_softc *sc = h;
686 1.9.2.2 nathanw usbd_status err;
687 1.9.2.2 nathanw int i;
688 1.9.2.2 nathanw u_int8_t hdr;
689 1.9.2.2 nathanw u_int32_t n;
690 1.9.2.2 nathanw u_int mask;
691 1.9.2.2 nathanw
692 1.9.2.3 nathanw DPRINTF(("%s: sc=%p, speed=%d ebofs=%d maxsize=%d\n", __func__,
693 1.9.2.2 nathanw sc, p->speed, p->ebofs, p->maxsize));
694 1.9.2.2 nathanw
695 1.9.2.2 nathanw if (sc->sc_dying)
696 1.9.2.2 nathanw return (EIO);
697 1.9.2.2 nathanw
698 1.9.2.2 nathanw hdr = 0;
699 1.9.2.2 nathanw if (p->ebofs != sc->sc_params.ebofs) {
700 1.9.2.2 nathanw /* round up ebofs */
701 1.9.2.2 nathanw mask = sc->sc_irdadesc.bmAdditionalBOFs;
702 1.9.2.2 nathanw for (i = 0; i < UIRDA_NEBOFS; i++) {
703 1.9.2.2 nathanw if ((mask & uirda_ebofs[i].mask) &&
704 1.9.2.2 nathanw uirda_ebofs[i].count >= p->ebofs) {
705 1.9.2.2 nathanw hdr = uirda_ebofs[i].header;
706 1.9.2.2 nathanw goto found1;
707 1.9.2.2 nathanw }
708 1.9.2.2 nathanw }
709 1.9.2.2 nathanw /* no good value found */
710 1.9.2.2 nathanw return (EINVAL);
711 1.9.2.2 nathanw found1:
712 1.9.2.2 nathanw DPRINTF(("uirda_set_params: ebofs hdr=0x%02x\n", hdr));
713 1.9.2.2 nathanw ;
714 1.9.2.3 nathanw
715 1.9.2.2 nathanw }
716 1.9.2.2 nathanw if (hdr != 0 || p->speed != sc->sc_params.speed) {
717 1.9.2.2 nathanw /* find speed */
718 1.9.2.2 nathanw mask = UGETW(sc->sc_irdadesc.wBaudRate);
719 1.9.2.2 nathanw for (i = 0; i < UIRDA_NSPEEDS; i++) {
720 1.9.2.2 nathanw if ((mask & uirda_speeds[i].mask) &&
721 1.9.2.2 nathanw uirda_speeds[i].speed == p->speed) {
722 1.9.2.2 nathanw hdr |= uirda_speeds[i].header;
723 1.9.2.2 nathanw goto found2;
724 1.9.2.2 nathanw }
725 1.9.2.2 nathanw }
726 1.9.2.2 nathanw /* no good value found */
727 1.9.2.2 nathanw return (EINVAL);
728 1.9.2.2 nathanw found2:
729 1.9.2.2 nathanw DPRINTF(("uirda_set_params: speed hdr=0x%02x\n", hdr));
730 1.9.2.2 nathanw ;
731 1.9.2.2 nathanw }
732 1.9.2.2 nathanw if (p->maxsize != sc->sc_params.maxsize) {
733 1.9.2.2 nathanw if (p->maxsize > IRDA_MAX_FRAME_SIZE)
734 1.9.2.2 nathanw return (EINVAL);
735 1.9.2.2 nathanw sc->sc_params.maxsize = p->maxsize;
736 1.9.2.2 nathanw #if 0
737 1.9.2.3 nathanw DPRINTF(("%s: new buffers, old size=%d\n", __func__,
738 1.9.2.2 nathanw sc->sc_params.maxsize));
739 1.9.2.2 nathanw if (p->maxsize > 10000 || p < 0) /* XXX */
740 1.9.2.2 nathanw return (EINVAL);
741 1.9.2.2 nathanw
742 1.9.2.2 nathanw /* Change the write buffer */
743 1.9.2.2 nathanw lockmgr(&sc->sc_wr_buf_lk, LK_EXCLUSIVE, NULL);
744 1.9.2.2 nathanw if (sc->sc_wr_buf != NULL)
745 1.9.2.2 nathanw usbd_free_buffer(sc->sc_wr_xfer);
746 1.9.2.2 nathanw sc->sc_wr_buf = usbd_alloc_buffer(sc->sc_wr_xfer, p->maxsize+1);
747 1.9.2.2 nathanw lockmgr(&sc->sc_wr_buf_lk, LK_RELEASE, NULL);
748 1.9.2.2 nathanw if (sc->sc_wr_buf == NULL)
749 1.9.2.2 nathanw return (ENOMEM);
750 1.9.2.2 nathanw
751 1.9.2.2 nathanw /* Change the read buffer */
752 1.9.2.2 nathanw lockmgr(&sc->sc_rd_buf_lk, LK_EXCLUSIVE, NULL);
753 1.9.2.2 nathanw usbd_abort_pipe(sc->sc_rd_pipe);
754 1.9.2.2 nathanw if (sc->sc_rd_buf != NULL)
755 1.9.2.2 nathanw usbd_free_buffer(sc->sc_rd_xfer);
756 1.9.2.2 nathanw sc->sc_rd_buf = usbd_alloc_buffer(sc->sc_rd_xfer, p->maxsize+1);
757 1.9.2.2 nathanw sc->sc_rd_count = 0;
758 1.9.2.2 nathanw if (sc->sc_rd_buf == NULL) {
759 1.9.2.2 nathanw lockmgr(&sc->sc_rd_buf_lk, LK_RELEASE, NULL);
760 1.9.2.2 nathanw return (ENOMEM);
761 1.9.2.2 nathanw }
762 1.9.2.2 nathanw sc->sc_params.maxsize = p->maxsize;
763 1.9.2.2 nathanw err = uirda_start_read(sc); /* XXX check */
764 1.9.2.2 nathanw lockmgr(&sc->sc_rd_buf_lk, LK_RELEASE, NULL);
765 1.9.2.2 nathanw #endif
766 1.9.2.2 nathanw }
767 1.9.2.2 nathanw if (hdr != 0 && hdr != sc->sc_wr_hdr) {
768 1.9.2.3 nathanw /*
769 1.9.2.2 nathanw * A change has occurred, transmit a 0 length frame with
770 1.9.2.2 nathanw * the new settings. The 0 length frame is not sent to the
771 1.9.2.2 nathanw * device.
772 1.9.2.2 nathanw */
773 1.9.2.2 nathanw DPRINTF(("%s: sc=%p setting header 0x%02x\n",
774 1.9.2.3 nathanw __func__, sc, hdr));
775 1.9.2.2 nathanw sc->sc_wr_hdr = hdr;
776 1.9.2.2 nathanw lockmgr(&sc->sc_wr_buf_lk, LK_EXCLUSIVE, NULL);
777 1.9.2.2 nathanw sc->sc_wr_buf[0] = hdr;
778 1.9.2.2 nathanw n = UIRDA_OUTPUT_HEADER_SIZE;
779 1.9.2.2 nathanw err = usbd_bulk_transfer(sc->sc_wr_xfer, sc->sc_wr_pipe,
780 1.9.2.2 nathanw USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
781 1.9.2.2 nathanw UIRDA_WR_TIMEOUT, sc->sc_wr_buf, &n, "uirdast");
782 1.9.2.2 nathanw if (err) {
783 1.9.2.2 nathanw printf("%s: set failed, err=%d\n",
784 1.9.2.2 nathanw USBDEVNAME(sc->sc_dev), err);
785 1.9.2.2 nathanw usbd_clear_endpoint_stall(sc->sc_wr_pipe);
786 1.9.2.2 nathanw }
787 1.9.2.2 nathanw lockmgr(&sc->sc_wr_buf_lk, LK_RELEASE, NULL);
788 1.9.2.2 nathanw }
789 1.9.2.2 nathanw
790 1.9.2.2 nathanw sc->sc_params = *p;
791 1.9.2.2 nathanw
792 1.9.2.2 nathanw return (0);
793 1.9.2.2 nathanw }
794 1.9.2.2 nathanw
795 1.9.2.2 nathanw int
796 1.9.2.2 nathanw uirda_get_speeds(void *h, int *speeds)
797 1.9.2.2 nathanw {
798 1.9.2.2 nathanw struct uirda_softc *sc = h;
799 1.9.2.2 nathanw u_int isp;
800 1.9.2.2 nathanw u_int usp;
801 1.9.2.2 nathanw
802 1.9.2.3 nathanw DPRINTF(("%s: sc=%p\n", __func__, sc));
803 1.9.2.2 nathanw
804 1.9.2.2 nathanw if (sc->sc_dying)
805 1.9.2.2 nathanw return (EIO);
806 1.9.2.2 nathanw
807 1.9.2.2 nathanw usp = UGETW(sc->sc_irdadesc.wBaudRate);
808 1.9.2.2 nathanw isp = 0;
809 1.9.2.2 nathanw if (usp & UI_BR_4000000) isp |= IRDA_SPEED_4000000;
810 1.9.2.2 nathanw if (usp & UI_BR_1152000) isp |= IRDA_SPEED_1152000;
811 1.9.2.2 nathanw if (usp & UI_BR_576000) isp |= IRDA_SPEED_576000;
812 1.9.2.2 nathanw if (usp & UI_BR_115200) isp |= IRDA_SPEED_115200;
813 1.9.2.2 nathanw if (usp & UI_BR_57600) isp |= IRDA_SPEED_57600;
814 1.9.2.2 nathanw if (usp & UI_BR_38400) isp |= IRDA_SPEED_38400;
815 1.9.2.2 nathanw if (usp & UI_BR_19200) isp |= IRDA_SPEED_19200;
816 1.9.2.2 nathanw if (usp & UI_BR_9600) isp |= IRDA_SPEED_9600;
817 1.9.2.2 nathanw if (usp & UI_BR_2400) isp |= IRDA_SPEED_2400;
818 1.9.2.2 nathanw *speeds = isp;
819 1.9.2.2 nathanw return (0);
820 1.9.2.2 nathanw }
821 1.9.2.2 nathanw
822 1.9.2.2 nathanw int
823 1.9.2.2 nathanw uirda_get_turnarounds(void *h, int *turnarounds)
824 1.9.2.2 nathanw {
825 1.9.2.2 nathanw struct uirda_softc *sc = h;
826 1.9.2.2 nathanw u_int ita;
827 1.9.2.2 nathanw u_int uta;
828 1.9.2.2 nathanw
829 1.9.2.3 nathanw DPRINTF(("%s: sc=%p\n", __func__, sc));
830 1.9.2.2 nathanw
831 1.9.2.2 nathanw if (sc->sc_dying)
832 1.9.2.2 nathanw return (EIO);
833 1.9.2.2 nathanw
834 1.9.2.2 nathanw uta = sc->sc_irdadesc.bmMinTurnaroundTime;
835 1.9.2.2 nathanw ita = 0;
836 1.9.2.2 nathanw if (uta & UI_TA_0) ita |= IRDA_TURNT_0;
837 1.9.2.2 nathanw if (uta & UI_TA_10) ita |= IRDA_TURNT_10;
838 1.9.2.2 nathanw if (uta & UI_TA_50) ita |= IRDA_TURNT_50;
839 1.9.2.2 nathanw if (uta & UI_TA_100) ita |= IRDA_TURNT_100;
840 1.9.2.2 nathanw if (uta & UI_TA_500) ita |= IRDA_TURNT_500;
841 1.9.2.2 nathanw if (uta & UI_TA_1000) ita |= IRDA_TURNT_1000;
842 1.9.2.2 nathanw if (uta & UI_TA_5000) ita |= IRDA_TURNT_5000;
843 1.9.2.2 nathanw if (uta & UI_TA_10000) ita |= IRDA_TURNT_10000;
844 1.9.2.2 nathanw *turnarounds = ita;
845 1.9.2.2 nathanw return (0);
846 1.9.2.2 nathanw }
847 1.9.2.2 nathanw
848 1.9.2.2 nathanw void
849 1.9.2.2 nathanw uirda_rd_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
850 1.9.2.2 nathanw usbd_status status)
851 1.9.2.2 nathanw {
852 1.9.2.2 nathanw struct uirda_softc *sc = priv;
853 1.9.2.2 nathanw u_int32_t size;
854 1.9.2.2 nathanw
855 1.9.2.3 nathanw DPRINTFN(1,("%s: sc=%p\n", __func__, sc));
856 1.9.2.2 nathanw
857 1.9.2.2 nathanw if (status == USBD_CANCELLED) /* this is normal */
858 1.9.2.2 nathanw return;
859 1.9.2.2 nathanw if (status) {
860 1.9.2.2 nathanw size = UIRDA_INPUT_HEADER_SIZE;
861 1.9.2.2 nathanw sc->sc_rd_err = 1;
862 1.9.2.2 nathanw } else {
863 1.9.2.2 nathanw usbd_get_xfer_status(xfer, NULL, NULL, &size, NULL);
864 1.9.2.2 nathanw }
865 1.9.2.3 nathanw DPRINTFN(1,("%s: sc=%p size=%u, err=%d\n", __func__, sc, size,
866 1.9.2.2 nathanw sc->sc_rd_err));
867 1.9.2.2 nathanw sc->sc_rd_count = size;
868 1.9.2.2 nathanw wakeup(&sc->sc_rd_count); /* XXX should use flag */
869 1.9.2.2 nathanw selwakeup(&sc->sc_rd_sel);
870 1.9.2.2 nathanw }
871 1.9.2.2 nathanw
872 1.9.2.2 nathanw usbd_status
873 1.9.2.2 nathanw uirda_start_read(struct uirda_softc *sc)
874 1.9.2.2 nathanw {
875 1.9.2.2 nathanw usbd_status err;
876 1.9.2.2 nathanw
877 1.9.2.3 nathanw DPRINTFN(1,("%s: sc=%p, size=%d\n", __func__, sc,
878 1.9.2.2 nathanw sc->sc_params.maxsize + UIRDA_INPUT_HEADER_SIZE));
879 1.9.2.2 nathanw
880 1.9.2.2 nathanw if (sc->sc_dying)
881 1.9.2.2 nathanw return (USBD_IOERROR);
882 1.9.2.2 nathanw
883 1.9.2.2 nathanw if (sc->sc_rd_err) {
884 1.9.2.2 nathanw sc->sc_rd_err = 0;
885 1.9.2.2 nathanw DPRINTF(("uirda_start_read: clear stall\n"));
886 1.9.2.2 nathanw usbd_clear_endpoint_stall(sc->sc_rd_pipe);
887 1.9.2.2 nathanw }
888 1.9.2.2 nathanw
889 1.9.2.2 nathanw usbd_setup_xfer(sc->sc_rd_xfer, sc->sc_rd_pipe, sc, sc->sc_rd_buf,
890 1.9.2.2 nathanw sc->sc_params.maxsize + UIRDA_INPUT_HEADER_SIZE,
891 1.9.2.2 nathanw USBD_SHORT_XFER_OK | USBD_NO_COPY,
892 1.9.2.2 nathanw USBD_NO_TIMEOUT, uirda_rd_cb);
893 1.9.2.2 nathanw err = usbd_transfer(sc->sc_rd_xfer);
894 1.9.2.2 nathanw if (err != USBD_IN_PROGRESS) {
895 1.9.2.2 nathanw DPRINTF(("uirda_start_read: err=%d\n", err));
896 1.9.2.2 nathanw return (err);
897 1.9.2.2 nathanw }
898 1.9.2.2 nathanw return (USBD_NORMAL_COMPLETION);
899 1.9.2.2 nathanw }
900