xref: /netbsd-src/sys/dev/usb/umodem_common.c (revision b5677b36047b601b9addaaa494a58ceae82c2a6c)
1 /*	$NetBSD: umodem_common.c,v 1.15 2008/05/24 16:40:58 cube 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@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.15 2008/05/24 16:40:58 cube Exp $");
48 
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/kernel.h>
52 #include <sys/ioctl.h>
53 #include <sys/conf.h>
54 #include <sys/tty.h>
55 #include <sys/file.h>
56 #include <sys/select.h>
57 #include <sys/proc.h>
58 #include <sys/vnode.h>
59 #include <sys/device.h>
60 #include <sys/poll.h>
61 
62 #include <dev/usb/usb.h>
63 #include <dev/usb/usbcdc.h>
64 
65 #include <dev/usb/usbdi.h>
66 #include <dev/usb/usbdi_util.h>
67 #include <dev/usb/usbdevs.h>
68 #include <dev/usb/usb_quirks.h>
69 
70 #include <dev/usb/ucomvar.h>
71 #include <dev/usb/umodemvar.h>
72 
73 #ifdef UMODEM_DEBUG
74 #define DPRINTFN(n, x)	if (umodemdebug > (n)) logprintf x
75 int	umodemdebug = 0;
76 #else
77 #define DPRINTFN(n, x)
78 #endif
79 #define DPRINTF(x) DPRINTFN(0, x)
80 
81 /*
82  * These are the maximum number of bytes transferred per frame.
83  * If some really high speed devices should use this driver they
84  * may need to be increased, but this is good enough for normal modems.
85  *
86  * Note: increased from 64/256, to better support EVDO wireless PPP.
87  * The sizes should not be increased further, or there
88  * will be problems with contiguous storage allocation.
89  */
90 #define UMODEMIBUFSIZE 4096
91 #define UMODEMOBUFSIZE 4096
92 
93 Static usbd_status umodem_set_comm_feature(struct umodem_softc *sc,
94 					   int feature, int state);
95 Static usbd_status umodem_set_line_coding(struct umodem_softc *sc,
96 					  usb_cdc_line_state_t *state);
97 
98 Static void	umodem_dtr(struct umodem_softc *, int);
99 Static void	umodem_rts(struct umodem_softc *, int);
100 Static void	umodem_break(struct umodem_softc *, int);
101 Static void	umodem_set_line_state(struct umodem_softc *);
102 Static void	umodem_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
103 
104 int
105 umodem_common_attach(device_ptr_t self, struct umodem_softc *sc,
106 		     struct usbif_attach_arg *uaa, struct ucom_attach_args *uca)
107 {
108 	usbd_device_handle dev = uaa->device;
109 	usb_interface_descriptor_t *id;
110 	usb_endpoint_descriptor_t *ed;
111 	char *devinfop;
112 	usbd_status err;
113 	int data_ifcno;
114 	int i;
115 
116 	devinfop = usbd_devinfo_alloc(uaa->device, 0);
117 	USB_ATTACH_SETUP;
118 
119 	sc->sc_dev = self;
120 	sc->sc_udev = dev;
121 	sc->sc_ctl_iface = uaa->iface;
122 
123 	id = usbd_get_interface_descriptor(sc->sc_ctl_iface);
124 	aprint_normal_dev(self, "%s, iclass %d/%d\n",
125 	       devinfop, id->bInterfaceClass, id->bInterfaceSubClass);
126 	usbd_devinfo_free(devinfop);
127 	sc->sc_ctl_iface_no = id->bInterfaceNumber;
128 
129 	/* Get the data interface no. */
130 	sc->sc_data_iface_no = data_ifcno =
131 	    umodem_get_caps(dev, &sc->sc_cm_cap, &sc->sc_acm_cap, id);
132 
133 	if (data_ifcno == -1) {
134 		aprint_error_dev(self, "no pointer to data interface\n");
135 		goto bad;
136 	}
137 
138 	aprint_normal_dev(self,
139 	    "data interface %d, has %sCM over data, has %sbreak\n",
140 	    data_ifcno, sc->sc_cm_cap & USB_CDC_CM_OVER_DATA ? "" : "no ",
141 	    sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK ? "" : "no ");
142 
143 	/* Get the data interface too. */
144 	for (i = 0; i < uaa->nifaces; i++) {
145 		if (uaa->ifaces[i] != NULL) {
146 			id = usbd_get_interface_descriptor(uaa->ifaces[i]);
147 			if (id != NULL && id->bInterfaceNumber == data_ifcno) {
148 				sc->sc_data_iface = uaa->ifaces[i];
149 				uaa->ifaces[i] = NULL;
150 			}
151 		}
152 	}
153 	if (sc->sc_data_iface == NULL) {
154 		aprint_error_dev(self, "no data interface\n");
155 		goto bad;
156 	}
157 
158 	/*
159 	 * Find the bulk endpoints.
160 	 * Iterate over all endpoints in the data interface and take note.
161 	 */
162 	uca->bulkin = uca->bulkout = -1;
163 
164 	id = usbd_get_interface_descriptor(sc->sc_data_iface);
165 	for (i = 0; i < id->bNumEndpoints; i++) {
166 		ed = usbd_interface2endpoint_descriptor(sc->sc_data_iface, i);
167 		if (ed == NULL) {
168 			aprint_error_dev(self,
169 			    "no endpoint descriptor for %d\n)", i);
170 			goto bad;
171 		}
172 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
173 		    (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
174 			uca->bulkin = ed->bEndpointAddress;
175 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
176 			   (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
177 			uca->bulkout = ed->bEndpointAddress;
178 		}
179 	}
180 
181 	if (uca->bulkin == -1) {
182 		aprint_error_dev(self, "Could not find data bulk in\n");
183 		goto bad;
184 	}
185 	if (uca->bulkout == -1) {
186 		aprint_error_dev(self, "Could not find data bulk out\n");
187 		goto bad;
188 	}
189 
190 	if (usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_ASSUME_CM_OVER_DATA) {
191 		sc->sc_cm_over_data = 1;
192 	} else {
193 		if (sc->sc_cm_cap & USB_CDC_CM_OVER_DATA) {
194 			if (sc->sc_acm_cap & USB_CDC_ACM_HAS_FEATURE)
195 				err = umodem_set_comm_feature(sc,
196 				    UCDC_ABSTRACT_STATE, UCDC_DATA_MULTIPLEXED);
197 			else
198 				err = 0;
199 			if (err) {
200 				aprint_error_dev(self,
201 				    "could not set data multiplex mode\n");
202 				goto bad;
203 			}
204 			sc->sc_cm_over_data = 1;
205 		}
206 	}
207 
208 	/*
209 	 * The standard allows for notification messages (to indicate things
210 	 * like a modem hangup) to come in via an interrupt endpoint
211 	 * off of the control interface.  Iterate over the endpoints on
212 	 * the control interface and see if there are any interrupt
213 	 * endpoints; if there are, then register it.
214 	 */
215 
216 	sc->sc_ctl_notify = -1;
217 	sc->sc_notify_pipe = NULL;
218 
219 	for (i = 0; i < id->bNumEndpoints; i++) {
220 		ed = usbd_interface2endpoint_descriptor(sc->sc_ctl_iface, i);
221 		if (ed == NULL)
222 			continue;
223 
224 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
225 		    (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
226 			aprint_error_dev(self,
227 			    "status change notification available\n");
228 			sc->sc_ctl_notify = ed->bEndpointAddress;
229 		}
230 	}
231 
232 	sc->sc_dtr = -1;
233 
234 	/* bulkin, bulkout set above */
235 	uca->ibufsize = UMODEMIBUFSIZE;
236 	uca->obufsize = UMODEMOBUFSIZE;
237 	uca->ibufsizepad = UMODEMIBUFSIZE;
238 	uca->opkthdrlen = 0;
239 	uca->device = sc->sc_udev;
240 	uca->iface = sc->sc_data_iface;
241 	uca->arg = sc;
242 
243 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
244 			   USBDEV(sc->sc_dev));
245 
246 	DPRINTF(("umodem_common_attach: sc=%p\n", sc));
247 	sc->sc_subdev = config_found_sm_loc(self, "ucombus", NULL, uca,
248 					    ucomprint, ucomsubmatch);
249 
250 	return 0;
251 
252  bad:
253 	sc->sc_dying = 1;
254 	return 1;
255 }
256 
257 int
258 umodem_open(void *addr, int portno)
259 {
260 	struct umodem_softc *sc = addr;
261 	int err;
262 
263 	DPRINTF(("umodem_open: sc=%p\n", sc));
264 
265 	if (sc->sc_ctl_notify != -1 && sc->sc_notify_pipe == NULL) {
266 		err = usbd_open_pipe_intr(sc->sc_ctl_iface, sc->sc_ctl_notify,
267 		    USBD_SHORT_XFER_OK, &sc->sc_notify_pipe, sc,
268 		    &sc->sc_notify_buf, sizeof(sc->sc_notify_buf),
269 		    umodem_intr, USBD_DEFAULT_INTERVAL);
270 
271 		if (err) {
272 			DPRINTF(("Failed to establish notify pipe: %s\n",
273 				usbd_errstr(err)));
274 			return EIO;
275 		}
276 	}
277 
278 	return 0;
279 }
280 
281 void
282 umodem_close(void *addr, int portno)
283 {
284 	struct umodem_softc *sc = addr;
285 	int err;
286 
287 	DPRINTF(("umodem_close: sc=%p\n", sc));
288 
289 	if (sc->sc_notify_pipe != NULL) {
290 		err = usbd_abort_pipe(sc->sc_notify_pipe);
291 		if (err)
292 			printf("%s: abort notify pipe failed: %s\n",
293 			    USBDEVNAME(sc->sc_dev), usbd_errstr(err));
294 		err = usbd_close_pipe(sc->sc_notify_pipe);
295 		if (err)
296 			printf("%s: close notify pipe failed: %s\n",
297 			    USBDEVNAME(sc->sc_dev), usbd_errstr(err));
298 		sc->sc_notify_pipe = NULL;
299 	}
300 }
301 
302 Static void
303 umodem_intr(usbd_xfer_handle xfer, usbd_private_handle priv,
304     usbd_status status)
305 {
306 	struct umodem_softc *sc = priv;
307 	u_char mstatus;
308 
309 	if (sc->sc_dying)
310 		return;
311 
312 	if (status != USBD_NORMAL_COMPLETION) {
313 		if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
314 			return;
315 		printf("%s: abnormal status: %s\n", USBDEVNAME(sc->sc_dev),
316 		       usbd_errstr(status));
317 		return;
318 	}
319 
320 	if (sc->sc_notify_buf.bmRequestType != UCDC_NOTIFICATION) {
321 		DPRINTF(("%s: unknown message type (%02x) on notify pipe\n",
322 			 USBDEVNAME(sc->sc_dev),
323 			 sc->sc_notify_buf.bmRequestType));
324 		return;
325 	}
326 
327 	switch (sc->sc_notify_buf.bNotification) {
328 	case UCDC_N_SERIAL_STATE:
329 		/*
330 		 * Set the serial state in ucom driver based on
331 		 * the bits from the notify message
332 		 */
333 		if (UGETW(sc->sc_notify_buf.wLength) != 2) {
334 			printf("%s: Invalid notification length! (%d)\n",
335 			       USBDEVNAME(sc->sc_dev),
336 			       UGETW(sc->sc_notify_buf.wLength));
337 			break;
338 		}
339 		DPRINTF(("%s: notify bytes = %02x%02x\n",
340 			 USBDEVNAME(sc->sc_dev),
341 			 sc->sc_notify_buf.data[0],
342 			 sc->sc_notify_buf.data[1]));
343 		/* Currently, lsr is always zero. */
344 		sc->sc_lsr = sc->sc_msr = 0;
345 		mstatus = sc->sc_notify_buf.data[0];
346 
347 		if (ISSET(mstatus, UCDC_N_SERIAL_RI))
348 			sc->sc_msr |= UMSR_RI;
349 		if (ISSET(mstatus, UCDC_N_SERIAL_DSR))
350 			sc->sc_msr |= UMSR_DSR;
351 		if (ISSET(mstatus, UCDC_N_SERIAL_DCD))
352 			sc->sc_msr |= UMSR_DCD;
353 		ucom_status_change(device_private(sc->sc_subdev));
354 		break;
355 	default:
356 		DPRINTF(("%s: unknown notify message: %02x\n",
357 			 USBDEVNAME(sc->sc_dev),
358 			 sc->sc_notify_buf.bNotification));
359 		break;
360 	}
361 }
362 
363 int
364 umodem_get_caps(usbd_device_handle dev, int *cm, int *acm,
365 		usb_interface_descriptor_t *id)
366 {
367 	const usb_cdc_cm_descriptor_t *cmd;
368 	const usb_cdc_acm_descriptor_t *cad;
369 	const usb_cdc_union_descriptor_t *cud;
370 
371 	*cm = *acm = 0;
372 
373 	cmd = (const usb_cdc_cm_descriptor_t *)usb_find_desc_if(dev,
374 							  UDESC_CS_INTERFACE,
375 							  UDESCSUB_CDC_CM, id);
376 	if (cmd == NULL) {
377 		DPRINTF(("umodem_get_caps: no CM desc\n"));
378 	} else {
379 		*cm = cmd->bmCapabilities;
380 	}
381 
382 	cad = (const usb_cdc_acm_descriptor_t *)usb_find_desc_if(dev,
383 							   UDESC_CS_INTERFACE,
384 							   UDESCSUB_CDC_ACM,
385 							   id);
386 	if (cad == NULL) {
387 		DPRINTF(("umodem_get_caps: no ACM desc\n"));
388 	} else {
389 		*acm = cad->bmCapabilities;
390 	}
391 
392 	cud = (const usb_cdc_union_descriptor_t *)usb_find_desc_if(dev,
393 							     UDESC_CS_INTERFACE,
394 							     UDESCSUB_CDC_UNION,
395 							     id);
396 	if (cud == NULL) {
397 		DPRINTF(("umodem_get_caps: no UNION desc\n"));
398 	}
399 
400 	return cmd ? cmd->bDataInterface : cud ? cud->bSlaveInterface[0] : -1;
401 }
402 
403 void
404 umodem_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
405 {
406 	struct umodem_softc *sc = addr;
407 
408 	DPRINTF(("umodem_get_status:\n"));
409 
410 	if (lsr != NULL)
411 		*lsr = sc->sc_lsr;
412 	if (msr != NULL)
413 		*msr = sc->sc_msr;
414 }
415 
416 int
417 umodem_param(void *addr, int portno, struct termios *t)
418 {
419 	struct umodem_softc *sc = addr;
420 	usbd_status err;
421 	usb_cdc_line_state_t ls;
422 
423 	DPRINTF(("umodem_param: sc=%p\n", sc));
424 
425 	USETDW(ls.dwDTERate, t->c_ospeed);
426 	if (ISSET(t->c_cflag, CSTOPB))
427 		ls.bCharFormat = UCDC_STOP_BIT_2;
428 	else
429 		ls.bCharFormat = UCDC_STOP_BIT_1;
430 	if (ISSET(t->c_cflag, PARENB)) {
431 		if (ISSET(t->c_cflag, PARODD))
432 			ls.bParityType = UCDC_PARITY_ODD;
433 		else
434 			ls.bParityType = UCDC_PARITY_EVEN;
435 	} else
436 		ls.bParityType = UCDC_PARITY_NONE;
437 	switch (ISSET(t->c_cflag, CSIZE)) {
438 	case CS5:
439 		ls.bDataBits = 5;
440 		break;
441 	case CS6:
442 		ls.bDataBits = 6;
443 		break;
444 	case CS7:
445 		ls.bDataBits = 7;
446 		break;
447 	case CS8:
448 		ls.bDataBits = 8;
449 		break;
450 	}
451 
452 	err = umodem_set_line_coding(sc, &ls);
453 	if (err) {
454 		DPRINTF(("umodem_param: err=%s\n", usbd_errstr(err)));
455 		return (EPASSTHROUGH);
456 	}
457 	return (0);
458 }
459 
460 int
461 umodem_ioctl(void *addr, int portno, u_long cmd, void *data,
462     int flag, usb_proc_ptr p)
463 {
464 	struct umodem_softc *sc = addr;
465 	int error = 0;
466 
467 	if (sc->sc_dying)
468 		return (EIO);
469 
470 	DPRINTF(("umodem_ioctl: cmd=0x%08lx\n", cmd));
471 
472 	switch (cmd) {
473 	case USB_GET_CM_OVER_DATA:
474 		*(int *)data = sc->sc_cm_over_data;
475 		break;
476 
477 	case USB_SET_CM_OVER_DATA:
478 		if (*(int *)data != sc->sc_cm_over_data) {
479 			/* XXX change it */
480 		}
481 		break;
482 
483 	default:
484 		DPRINTF(("umodem_ioctl: unknown\n"));
485 		error = EPASSTHROUGH;
486 		break;
487 	}
488 
489 	return (error);
490 }
491 
492 void
493 umodem_dtr(struct umodem_softc *sc, int onoff)
494 {
495 	DPRINTF(("umodem_dtr: onoff=%d\n", onoff));
496 
497 	if (sc->sc_dtr == onoff)
498 		return;
499 	sc->sc_dtr = onoff;
500 
501 	umodem_set_line_state(sc);
502 }
503 
504 void
505 umodem_rts(struct umodem_softc *sc, int onoff)
506 {
507 	DPRINTF(("umodem_rts: onoff=%d\n", onoff));
508 
509 	if (sc->sc_rts == onoff)
510 		return;
511 	sc->sc_rts = onoff;
512 
513 	umodem_set_line_state(sc);
514 }
515 
516 void
517 umodem_set_line_state(struct umodem_softc *sc)
518 {
519 	usb_device_request_t req;
520 	int ls;
521 
522 	ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
523 	     (sc->sc_rts ? UCDC_LINE_RTS : 0);
524 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
525 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
526 	USETW(req.wValue, ls);
527 	USETW(req.wIndex, sc->sc_ctl_iface_no);
528 	USETW(req.wLength, 0);
529 
530 	(void)usbd_do_request(sc->sc_udev, &req, 0);
531 
532 }
533 
534 void
535 umodem_break(struct umodem_softc *sc, int onoff)
536 {
537 	usb_device_request_t req;
538 
539 	DPRINTF(("umodem_break: onoff=%d\n", onoff));
540 
541 	if (!(sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK))
542 		return;
543 
544 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
545 	req.bRequest = UCDC_SEND_BREAK;
546 	USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
547 	USETW(req.wIndex, sc->sc_ctl_iface_no);
548 	USETW(req.wLength, 0);
549 
550 	(void)usbd_do_request(sc->sc_udev, &req, 0);
551 }
552 
553 void
554 umodem_set(void *addr, int portno, int reg, int onoff)
555 {
556 	struct umodem_softc *sc = addr;
557 
558 	switch (reg) {
559 	case UCOM_SET_DTR:
560 		umodem_dtr(sc, onoff);
561 		break;
562 	case UCOM_SET_RTS:
563 		umodem_rts(sc, onoff);
564 		break;
565 	case UCOM_SET_BREAK:
566 		umodem_break(sc, onoff);
567 		break;
568 	default:
569 		break;
570 	}
571 }
572 
573 usbd_status
574 umodem_set_line_coding(struct umodem_softc *sc, usb_cdc_line_state_t *state)
575 {
576 	usb_device_request_t req;
577 	usbd_status err;
578 
579 	DPRINTF(("umodem_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
580 		 UGETDW(state->dwDTERate), state->bCharFormat,
581 		 state->bParityType, state->bDataBits));
582 
583 	if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
584 		DPRINTF(("umodem_set_line_coding: already set\n"));
585 		return (USBD_NORMAL_COMPLETION);
586 	}
587 
588 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
589 	req.bRequest = UCDC_SET_LINE_CODING;
590 	USETW(req.wValue, 0);
591 	USETW(req.wIndex, sc->sc_ctl_iface_no);
592 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
593 
594 	err = usbd_do_request(sc->sc_udev, &req, state);
595 	if (err) {
596 		DPRINTF(("umodem_set_line_coding: failed, err=%s\n",
597 			 usbd_errstr(err)));
598 		return (err);
599 	}
600 
601 	sc->sc_line_state = *state;
602 
603 	return (USBD_NORMAL_COMPLETION);
604 }
605 
606 usbd_status
607 umodem_set_comm_feature(struct umodem_softc *sc, int feature, int state)
608 {
609 	usb_device_request_t req;
610 	usbd_status err;
611 	usb_cdc_abstract_state_t ast;
612 
613 	DPRINTF(("umodem_set_comm_feature: feature=%d state=%d\n", feature,
614 		 state));
615 
616 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
617 	req.bRequest = UCDC_SET_COMM_FEATURE;
618 	USETW(req.wValue, feature);
619 	USETW(req.wIndex, sc->sc_ctl_iface_no);
620 	USETW(req.wLength, UCDC_ABSTRACT_STATE_LENGTH);
621 	USETW(ast.wState, state);
622 
623 	err = usbd_do_request(sc->sc_udev, &req, &ast);
624 	if (err) {
625 		DPRINTF(("umodem_set_comm_feature: feature=%d, err=%s\n",
626 			 feature, usbd_errstr(err)));
627 		return (err);
628 	}
629 
630 	return (USBD_NORMAL_COMPLETION);
631 }
632 
633 int
634 umodem_common_activate(struct umodem_softc *sc, enum devact act)
635 {
636 	int rv = 0;
637 
638 	switch (act) {
639 	case DVACT_ACTIVATE:
640 		return (EOPNOTSUPP);
641 
642 	case DVACT_DEACTIVATE:
643 		sc->sc_dying = 1;
644 		if (sc->sc_subdev)
645 			rv = config_deactivate(sc->sc_subdev);
646 		break;
647 	}
648 	return (rv);
649 }
650 
651 void
652 umodem_common_childdet(struct umodem_softc *sc, device_t child)
653 {
654 	KASSERT(sc->sc_subdev == child);
655 	sc->sc_subdev = NULL;
656 }
657 
658 int
659 umodem_common_detach(struct umodem_softc *sc, int flags)
660 {
661 	int rv = 0;
662 
663 	DPRINTF(("umodem_common_detach: sc=%p flags=%d\n", sc, flags));
664 
665 	sc->sc_dying = 1;
666 
667 	if (sc->sc_subdev != NULL)
668 		rv = config_detach(sc->sc_subdev, flags);
669 
670 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
671 			   USBDEV(sc->sc_dev));
672 
673 	return (rv);
674 }
675