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