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