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