umodem_common.c revision 1.25.8.1 1 /* $NetBSD: umodem_common.c,v 1.25.8.1 2019/04/22 08:17:50 martin Exp $ */
2
3 /*
4 * Copyright (c) 1998 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 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * Comm Class spec: http://www.usb.org/developers/devclass_docs/usbccs10.pdf
35 * http://www.usb.org/developers/devclass_docs/usbcdc11.pdf
36 */
37
38 /*
39 * TODO:
40 * - Add error recovery in various places; the big problem is what
41 * to do in a callback if there is an error.
42 * - Implement a Call Device for modems without multiplexed commands.
43 *
44 */
45
46 #include <sys/cdefs.h>
47 __KERNEL_RCSID(0, "$NetBSD: umodem_common.c,v 1.25.8.1 2019/04/22 08:17:50 martin Exp $");
48
49 #ifdef _KERNEL_OPT
50 #include "opt_usb.h"
51 #endif
52
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/kernel.h>
56 #include <sys/ioctl.h>
57 #include <sys/conf.h>
58 #include <sys/tty.h>
59 #include <sys/file.h>
60 #include <sys/select.h>
61 #include <sys/proc.h>
62 #include <sys/vnode.h>
63 #include <sys/device.h>
64 #include <sys/poll.h>
65
66 #include <dev/usb/usb.h>
67 #include <dev/usb/usbcdc.h>
68
69 #include <dev/usb/usbdi.h>
70 #include <dev/usb/usbdi_util.h>
71 #include <dev/usb/usbdevs.h>
72 #include <dev/usb/usb_quirks.h>
73
74 #include <dev/usb/ucomvar.h>
75 #include <dev/usb/umodemvar.h>
76
77 #ifdef UMODEM_DEBUG
78 #define DPRINTFN(n, x) if (umodemdebug > (n)) printf x
79 int umodemdebug = 0;
80 #else
81 #define DPRINTFN(n, x)
82 #endif
83 #define DPRINTF(x) DPRINTFN(0, x)
84
85 /*
86 * These are the maximum number of bytes transferred per frame.
87 * If some really high speed devices should use this driver they
88 * may need to be increased, but this is good enough for normal modems.
89 *
90 * Note: increased from 64/256, to better support EVDO wireless PPP.
91 * The sizes should not be increased further, or there
92 * will be problems with contiguous storage allocation.
93 */
94 #define UMODEMIBUFSIZE 4096
95 #define UMODEMOBUFSIZE 4096
96
97 static usbd_status umodem_set_comm_feature(struct umodem_softc *,
98 int, int);
99 static usbd_status umodem_set_line_coding(struct umodem_softc *,
100 usb_cdc_line_state_t *);
101
102 static void umodem_dtr(struct umodem_softc *, int);
103 static void umodem_rts(struct umodem_softc *, int);
104 static void umodem_break(struct umodem_softc *, int);
105 static void umodem_set_line_state(struct umodem_softc *);
106 static void umodem_intr(struct usbd_xfer *, void *, usbd_status);
107
108 int
109 umodem_common_attach(device_t self, struct umodem_softc *sc,
110 struct usbif_attach_arg *uiaa, struct ucom_attach_args *ucaa)
111 {
112 struct usbd_device *dev = uiaa->uiaa_device;
113 usb_interface_descriptor_t *id;
114 usb_endpoint_descriptor_t *ed;
115 char *devinfop;
116 usbd_status err;
117 int data_ifcno;
118 int i;
119
120 sc->sc_dev = self;
121 sc->sc_udev = dev;
122 sc->sc_ctl_iface = uiaa->uiaa_iface;
123 sc->sc_refcnt = 0;
124 sc->sc_dying = false;
125
126 aprint_naive("\n");
127 aprint_normal("\n");
128
129 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SOFTUSB);
130 cv_init(&sc->sc_detach_cv, "umodemdet");
131
132 id = usbd_get_interface_descriptor(sc->sc_ctl_iface);
133 devinfop = usbd_devinfo_alloc(uiaa->uiaa_device, 0);
134 aprint_normal_dev(self, "%s, iclass %d/%d\n",
135 devinfop, id->bInterfaceClass, id->bInterfaceSubClass);
136 usbd_devinfo_free(devinfop);
137
138 sc->sc_ctl_iface_no = id->bInterfaceNumber;
139
140 /* Get the data interface no. */
141 sc->sc_data_iface_no = data_ifcno =
142 umodem_get_caps(dev, &sc->sc_cm_cap, &sc->sc_acm_cap, id);
143
144 if (data_ifcno == -1) {
145 aprint_error_dev(self, "no pointer to data interface\n");
146 goto bad;
147 }
148
149 aprint_normal_dev(self,
150 "data interface %d, has %sCM over data, has %sbreak\n",
151 data_ifcno, sc->sc_cm_cap & USB_CDC_CM_OVER_DATA ? "" : "no ",
152 sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK ? "" : "no ");
153
154 /* Get the data interface too. */
155 for (i = 0; i < uiaa->uiaa_nifaces; i++) {
156 if (uiaa->uiaa_ifaces[i] != NULL) {
157 id = usbd_get_interface_descriptor(uiaa->uiaa_ifaces[i]);
158 if (id != NULL && id->bInterfaceNumber == data_ifcno) {
159 sc->sc_data_iface = uiaa->uiaa_ifaces[i];
160 uiaa->uiaa_ifaces[i] = NULL;
161 }
162 }
163 }
164 if (sc->sc_data_iface == NULL) {
165 aprint_error_dev(self, "no data interface\n");
166 goto bad;
167 }
168
169 /*
170 * Find the bulk endpoints.
171 * Iterate over all endpoints in the data interface and take note.
172 */
173 ucaa->ucaa_bulkin = ucaa->ucaa_bulkout = -1;
174
175 id = usbd_get_interface_descriptor(sc->sc_data_iface);
176 for (i = 0; i < id->bNumEndpoints; i++) {
177 ed = usbd_interface2endpoint_descriptor(sc->sc_data_iface, i);
178 if (ed == NULL) {
179 aprint_error_dev(self,
180 "no endpoint descriptor for %d\n)", i);
181 goto bad;
182 }
183 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
184 (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
185 ucaa->ucaa_bulkin = ed->bEndpointAddress;
186 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
187 (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
188 ucaa->ucaa_bulkout = ed->bEndpointAddress;
189 }
190 }
191
192 if (ucaa->ucaa_bulkin == -1) {
193 aprint_error_dev(self, "Could not find data bulk in\n");
194 goto bad;
195 }
196 if (ucaa->ucaa_bulkout == -1) {
197 aprint_error_dev(self, "Could not find data bulk out\n");
198 goto bad;
199 }
200
201 if (usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_ASSUME_CM_OVER_DATA) {
202 sc->sc_cm_over_data = 1;
203 } else {
204 if (sc->sc_cm_cap & USB_CDC_CM_OVER_DATA) {
205 if (sc->sc_acm_cap & USB_CDC_ACM_HAS_FEATURE)
206 err = umodem_set_comm_feature(sc,
207 UCDC_ABSTRACT_STATE, UCDC_DATA_MULTIPLEXED);
208 else
209 err = 0;
210 if (err) {
211 aprint_error_dev(self,
212 "could not set data multiplex mode\n");
213 goto bad;
214 }
215 sc->sc_cm_over_data = 1;
216 }
217 }
218
219 /*
220 * The standard allows for notification messages (to indicate things
221 * like a modem hangup) to come in via an interrupt endpoint
222 * off of the control interface. Iterate over the endpoints on
223 * the control interface and see if there are any interrupt
224 * endpoints; if there are, then register it.
225 */
226
227 sc->sc_ctl_notify = -1;
228 sc->sc_notify_pipe = NULL;
229
230 id = usbd_get_interface_descriptor(sc->sc_ctl_iface);
231 for (i = 0; i < id->bNumEndpoints; i++) {
232 ed = usbd_interface2endpoint_descriptor(sc->sc_ctl_iface, i);
233 if (ed == NULL)
234 continue;
235
236 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
237 (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
238 aprint_error_dev(self,
239 "status change notification available\n");
240 sc->sc_ctl_notify = ed->bEndpointAddress;
241 }
242 }
243
244 sc->sc_dtr = -1;
245
246 /* ucaa_bulkin, ucaa_bulkout set above */
247 ucaa->ucaa_ibufsize = UMODEMIBUFSIZE;
248 ucaa->ucaa_obufsize = UMODEMOBUFSIZE;
249 ucaa->ucaa_ibufsizepad = UMODEMIBUFSIZE;
250 ucaa->ucaa_opkthdrlen = 0;
251 ucaa->ucaa_device = sc->sc_udev;
252 ucaa->ucaa_iface = sc->sc_data_iface;
253 ucaa->ucaa_arg = sc;
254
255 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
256
257 DPRINTF(("umodem_common_attach: sc=%p\n", sc));
258 sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, ucaa,
259 ucomprint, ucomsubmatch);
260
261 return 0;
262
263 bad:
264 sc->sc_dying = true;
265 return 1;
266 }
267
268 int
269 umodem_open(void *addr, int portno)
270 {
271 struct umodem_softc *sc = addr;
272 int err;
273
274 mutex_enter(&sc->sc_lock);
275 if (sc->sc_dying) {
276 mutex_exit(&sc->sc_lock);
277 return EIO;
278 }
279
280 sc->sc_refcnt++;
281 mutex_exit(&sc->sc_lock);
282
283 DPRINTF(("umodem_open: sc=%p\n", sc));
284
285 if (sc->sc_ctl_notify != -1 && sc->sc_notify_pipe == NULL) {
286 err = usbd_open_pipe_intr(sc->sc_ctl_iface, sc->sc_ctl_notify,
287 USBD_SHORT_XFER_OK, &sc->sc_notify_pipe, sc,
288 &sc->sc_notify_buf, sizeof(sc->sc_notify_buf),
289 umodem_intr, USBD_DEFAULT_INTERVAL);
290
291 if (err) {
292 DPRINTF(("Failed to establish notify pipe: %s\n",
293 usbd_errstr(err)));
294 mutex_enter(&sc->sc_lock);
295 if (--sc->sc_refcnt < 0)
296 cv_broadcast(&sc->sc_detach_cv);
297 mutex_exit(&sc->sc_lock);
298 return EIO;
299 }
300 }
301
302 return 0;
303 }
304
305 void
306 umodem_close(void *addr, int portno)
307 {
308 struct umodem_softc *sc = addr;
309 int err;
310
311 mutex_enter(&sc->sc_lock);
312 if (sc->sc_dying)
313 goto out;
314 mutex_exit(&sc->sc_lock);
315
316 DPRINTF(("umodem_close: sc=%p\n", sc));
317
318 if (sc->sc_notify_pipe != NULL) {
319 err = usbd_abort_pipe(sc->sc_notify_pipe);
320 if (err)
321 printf("%s: abort notify pipe failed: %s\n",
322 device_xname(sc->sc_dev), usbd_errstr(err));
323 err = usbd_close_pipe(sc->sc_notify_pipe);
324 if (err)
325 printf("%s: close notify pipe failed: %s\n",
326 device_xname(sc->sc_dev), usbd_errstr(err));
327 sc->sc_notify_pipe = NULL;
328 }
329
330 mutex_enter(&sc->sc_lock);
331 out:
332 if (--sc->sc_refcnt < 0)
333 cv_broadcast(&sc->sc_detach_cv);
334 mutex_exit(&sc->sc_lock);
335 }
336
337 static void
338 umodem_intr(struct usbd_xfer *xfer, void *priv,
339 usbd_status status)
340 {
341 struct umodem_softc *sc = priv;
342 u_char mstatus;
343
344 if (sc->sc_dying)
345 return;
346
347 if (status != USBD_NORMAL_COMPLETION) {
348 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
349 return;
350 printf("%s: abnormal status: %s\n", device_xname(sc->sc_dev),
351 usbd_errstr(status));
352 if (status == USBD_STALLED)
353 usbd_clear_endpoint_stall_async(sc->sc_notify_pipe);
354 return;
355 }
356
357 if (sc->sc_notify_buf.bmRequestType != UCDC_NOTIFICATION) {
358 DPRINTF(("%s: unknown message type (%02x) on notify pipe\n",
359 device_xname(sc->sc_dev),
360 sc->sc_notify_buf.bmRequestType));
361 return;
362 }
363
364 switch (sc->sc_notify_buf.bNotification) {
365 case UCDC_N_SERIAL_STATE:
366 /*
367 * Set the serial state in ucom driver based on
368 * the bits from the notify message
369 */
370 if (UGETW(sc->sc_notify_buf.wLength) != 2) {
371 printf("%s: Invalid notification length! (%d)\n",
372 device_xname(sc->sc_dev),
373 UGETW(sc->sc_notify_buf.wLength));
374 break;
375 }
376 DPRINTF(("%s: notify bytes = %02x%02x\n",
377 device_xname(sc->sc_dev),
378 sc->sc_notify_buf.data[0],
379 sc->sc_notify_buf.data[1]));
380 /* Currently, lsr is always zero. */
381 sc->sc_lsr = sc->sc_msr = 0;
382 mstatus = sc->sc_notify_buf.data[0];
383
384 if (ISSET(mstatus, UCDC_N_SERIAL_RI))
385 sc->sc_msr |= UMSR_RI;
386 if (ISSET(mstatus, UCDC_N_SERIAL_DSR))
387 sc->sc_msr |= UMSR_DSR;
388 if (ISSET(mstatus, UCDC_N_SERIAL_DCD))
389 sc->sc_msr |= UMSR_DCD;
390 ucom_status_change(device_private(sc->sc_subdev));
391 break;
392 default:
393 DPRINTF(("%s: unknown notify message: %02x\n",
394 device_xname(sc->sc_dev),
395 sc->sc_notify_buf.bNotification));
396 break;
397 }
398 }
399
400 int
401 umodem_get_caps(struct usbd_device *dev, int *cm, int *acm,
402 usb_interface_descriptor_t *id)
403 {
404 const usb_cdc_cm_descriptor_t *cmd;
405 const usb_cdc_acm_descriptor_t *cad;
406 const usb_cdc_union_descriptor_t *cud;
407 uint32_t uq_flags;
408
409 *cm = *acm = 0;
410 uq_flags = usbd_get_quirks(dev)->uq_flags;
411
412 if (uq_flags & UQ_NO_UNION_NRM) {
413 DPRINTF(("umodem_get_caps: NO_UNION_NRM quirk - returning 0\n"));
414 return 0;
415 }
416
417 if (uq_flags & UQ_LOST_CS_DESC)
418 id = NULL;
419
420 cmd = (const usb_cdc_cm_descriptor_t *)usb_find_desc_if(dev,
421 UDESC_CS_INTERFACE,
422 UDESCSUB_CDC_CM, id);
423 if (cmd == NULL) {
424 DPRINTF(("umodem_get_caps: no CM desc\n"));
425 } else {
426 *cm = cmd->bmCapabilities;
427 }
428
429 cad = (const usb_cdc_acm_descriptor_t *)usb_find_desc_if(dev,
430 UDESC_CS_INTERFACE,
431 UDESCSUB_CDC_ACM,
432 id);
433 if (cad == NULL) {
434 DPRINTF(("umodem_get_caps: no ACM desc\n"));
435 } else {
436 *acm = cad->bmCapabilities;
437 }
438
439 cud = (const usb_cdc_union_descriptor_t *)usb_find_desc_if(dev,
440 UDESC_CS_INTERFACE,
441 UDESCSUB_CDC_UNION,
442 id);
443 if (cud == NULL) {
444 DPRINTF(("umodem_get_caps: no UNION desc\n"));
445 }
446
447 return cmd ? cmd->bDataInterface : cud ? cud->bSlaveInterface[0] : -1;
448 }
449
450 void
451 umodem_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
452 {
453 struct umodem_softc *sc = addr;
454
455 DPRINTF(("umodem_get_status:\n"));
456
457 if (lsr != NULL)
458 *lsr = sc->sc_lsr;
459 if (msr != NULL)
460 *msr = sc->sc_msr;
461 }
462
463 int
464 umodem_param(void *addr, int portno, struct termios *t)
465 {
466 struct umodem_softc *sc = addr;
467 usbd_status err;
468 usb_cdc_line_state_t ls;
469
470 mutex_enter(&sc->sc_lock);
471 if (sc->sc_dying)
472 return EIO;
473 mutex_exit(&sc->sc_lock);
474
475 DPRINTF(("umodem_param: sc=%p\n", sc));
476
477 USETDW(ls.dwDTERate, t->c_ospeed);
478 if (ISSET(t->c_cflag, CSTOPB))
479 ls.bCharFormat = UCDC_STOP_BIT_2;
480 else
481 ls.bCharFormat = UCDC_STOP_BIT_1;
482 if (ISSET(t->c_cflag, PARENB)) {
483 if (ISSET(t->c_cflag, PARODD))
484 ls.bParityType = UCDC_PARITY_ODD;
485 else
486 ls.bParityType = UCDC_PARITY_EVEN;
487 } else
488 ls.bParityType = UCDC_PARITY_NONE;
489 switch (ISSET(t->c_cflag, CSIZE)) {
490 case CS5:
491 ls.bDataBits = 5;
492 break;
493 case CS6:
494 ls.bDataBits = 6;
495 break;
496 case CS7:
497 ls.bDataBits = 7;
498 break;
499 case CS8:
500 ls.bDataBits = 8;
501 break;
502 }
503
504 err = umodem_set_line_coding(sc, &ls);
505 if (err) {
506 DPRINTF(("umodem_param: err=%s\n", usbd_errstr(err)));
507 return EPASSTHROUGH;
508 }
509 return 0;
510 }
511
512 int
513 umodem_ioctl(void *addr, int portno, u_long cmd, void *data,
514 int flag, proc_t *p)
515 {
516 struct umodem_softc *sc = addr;
517 int error = 0;
518
519 mutex_enter(&sc->sc_lock);
520 if (sc->sc_dying)
521 return EIO;
522 mutex_exit(&sc->sc_lock);
523
524 DPRINTF(("umodem_ioctl: cmd=0x%08lx\n", cmd));
525
526 switch (cmd) {
527 case USB_GET_CM_OVER_DATA:
528 *(int *)data = sc->sc_cm_over_data;
529 break;
530
531 case USB_SET_CM_OVER_DATA:
532 if (*(int *)data != sc->sc_cm_over_data) {
533 /* XXX change it */
534 }
535 break;
536
537 default:
538 DPRINTF(("umodem_ioctl: unknown\n"));
539 error = EPASSTHROUGH;
540 break;
541 }
542
543 return error;
544 }
545
546 static void
547 umodem_dtr(struct umodem_softc *sc, int onoff)
548 {
549 DPRINTF(("umodem_dtr: onoff=%d\n", onoff));
550
551 if (sc->sc_dtr == onoff)
552 return;
553 sc->sc_dtr = onoff;
554
555 umodem_set_line_state(sc);
556 }
557
558 static void
559 umodem_rts(struct umodem_softc *sc, int onoff)
560 {
561 DPRINTF(("umodem_rts: onoff=%d\n", onoff));
562
563 if (sc->sc_rts == onoff)
564 return;
565 sc->sc_rts = onoff;
566
567 umodem_set_line_state(sc);
568 }
569
570 static void
571 umodem_set_line_state(struct umodem_softc *sc)
572 {
573 usb_device_request_t req;
574 int ls;
575
576 ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
577 (sc->sc_rts ? UCDC_LINE_RTS : 0);
578 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
579 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
580 USETW(req.wValue, ls);
581 USETW(req.wIndex, sc->sc_ctl_iface_no);
582 USETW(req.wLength, 0);
583
584 (void)usbd_do_request(sc->sc_udev, &req, 0);
585
586 }
587
588 static void
589 umodem_break(struct umodem_softc *sc, int onoff)
590 {
591 usb_device_request_t req;
592
593 DPRINTF(("umodem_break: onoff=%d\n", onoff));
594
595 if (!(sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK))
596 return;
597
598 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
599 req.bRequest = UCDC_SEND_BREAK;
600 USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
601 USETW(req.wIndex, sc->sc_ctl_iface_no);
602 USETW(req.wLength, 0);
603
604 (void)usbd_do_request(sc->sc_udev, &req, 0);
605 }
606
607 void
608 umodem_set(void *addr, int portno, int reg, int onoff)
609 {
610 struct umodem_softc *sc = addr;
611
612 mutex_enter(&sc->sc_lock);
613 if (sc->sc_dying)
614 return;
615 mutex_exit(&sc->sc_lock);
616
617 switch (reg) {
618 case UCOM_SET_DTR:
619 umodem_dtr(sc, onoff);
620 break;
621 case UCOM_SET_RTS:
622 umodem_rts(sc, onoff);
623 break;
624 case UCOM_SET_BREAK:
625 umodem_break(sc, onoff);
626 break;
627 default:
628 break;
629 }
630 }
631
632 static usbd_status
633 umodem_set_line_coding(struct umodem_softc *sc, usb_cdc_line_state_t *state)
634 {
635 usb_device_request_t req;
636 usbd_status err;
637
638 DPRINTF(("umodem_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
639 UGETDW(state->dwDTERate), state->bCharFormat,
640 state->bParityType, state->bDataBits));
641
642 if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
643 DPRINTF(("umodem_set_line_coding: already set\n"));
644 return USBD_NORMAL_COMPLETION;
645 }
646
647 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
648 req.bRequest = UCDC_SET_LINE_CODING;
649 USETW(req.wValue, 0);
650 USETW(req.wIndex, sc->sc_ctl_iface_no);
651 USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
652
653 err = usbd_do_request(sc->sc_udev, &req, state);
654 if (err) {
655 DPRINTF(("umodem_set_line_coding: failed, err=%s\n",
656 usbd_errstr(err)));
657 return err;
658 }
659
660 sc->sc_line_state = *state;
661
662 return USBD_NORMAL_COMPLETION;
663 }
664
665 static usbd_status
666 umodem_set_comm_feature(struct umodem_softc *sc, int feature, int state)
667 {
668 usb_device_request_t req;
669 usbd_status err;
670 usb_cdc_abstract_state_t ast;
671
672 DPRINTF(("umodem_set_comm_feature: feature=%d state=%d\n", feature,
673 state));
674
675 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
676 req.bRequest = UCDC_SET_COMM_FEATURE;
677 USETW(req.wValue, feature);
678 USETW(req.wIndex, sc->sc_ctl_iface_no);
679 USETW(req.wLength, UCDC_ABSTRACT_STATE_LENGTH);
680 USETW(ast.wState, state);
681
682 err = usbd_do_request(sc->sc_udev, &req, &ast);
683 if (err) {
684 DPRINTF(("umodem_set_comm_feature: feature=%d, err=%s\n",
685 feature, usbd_errstr(err)));
686 return err;
687 }
688
689 return USBD_NORMAL_COMPLETION;
690 }
691
692 int
693 umodem_common_activate(struct umodem_softc *sc, enum devact act)
694 {
695 switch (act) {
696 case DVACT_DEACTIVATE:
697 mutex_enter(&sc->sc_lock);
698 sc->sc_dying = true;
699 mutex_exit(&sc->sc_lock);
700 return 0;
701 default:
702 return EOPNOTSUPP;
703 }
704 }
705
706 void
707 umodem_common_childdet(struct umodem_softc *sc, device_t child)
708 {
709 KASSERT(sc->sc_subdev == child);
710 sc->sc_subdev = NULL;
711 }
712
713 int
714 umodem_common_detach(struct umodem_softc *sc, int flags)
715 {
716 int rv = 0;
717
718 DPRINTF(("umodem_common_detach: sc=%p flags=%d\n", sc, flags));
719
720 mutex_enter(&sc->sc_lock);
721 sc->sc_dying = true;
722
723 sc->sc_refcnt--;
724 while (sc->sc_refcnt > 0) {
725 if (cv_timedwait(&sc->sc_detach_cv, &sc->sc_lock, hz * 60))
726 aprint_error_dev(sc->sc_dev, ": didn't detach\n");
727 }
728 mutex_exit(&sc->sc_lock);
729
730 if (sc->sc_subdev != NULL)
731 rv = config_detach(sc->sc_subdev, flags);
732
733 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, sc->sc_dev);
734
735 mutex_destroy(&sc->sc_lock);
736 cv_destroy(&sc->sc_detach_cv);
737
738 return rv;
739 }
740