Rename printf -> kprintf in sys/ and add some defines where necessary
[dragonfly.git] / sys / dev / usbmisc / umodem / umodem.c
CommitLineData
1550dfd9
MD
1/*
2 * $NetBSD: umodem.c,v 1.5 1999/01/08 11:58:25 augustss Exp $
3 * $NetBSD: umodem.c,v 1.45 2002/09/23 05:51:23 simonb Exp $
4 * $FreeBSD: src/sys/dev/usb/umodem.c,v 1.48 2003/08/24 17:55:55 obrien Exp $
e3869ec7 5 * $DragonFly: src/sys/dev/usbmisc/umodem/umodem.c,v 1.12 2006/12/22 23:26:26 swildner Exp $
1550dfd9
MD
6 */
7
8/*-
9 * Copyright (c) 2003, M. Warner Losh <imp@freebsd.org>.
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
984263bc
MD
33
34/*
35 * Copyright (c) 1998 The NetBSD Foundation, Inc.
36 * All rights reserved.
37 *
38 * This code is derived from software contributed to The NetBSD Foundation
39 * by Lennart Augustsson (lennart@augustsson.net) at
40 * Carlstedt Research & Technology.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgement:
52 * This product includes software developed by the NetBSD
53 * Foundation, Inc. and its contributors.
54 * 4. Neither the name of The NetBSD Foundation nor the names of its
55 * contributors may be used to endorse or promote products derived
56 * from this software without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
59 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
60 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
61 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
62 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
63 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
64 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
65 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
66 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
67 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
68 * POSSIBILITY OF SUCH DAMAGE.
69 */
70
71/*
9f3a3c55
JR
72 * Comm Class spec: http://www.usb.org/developers/devclass_docs/usbccs10.pdf
73 * http://www.usb.org/developers/devclass_docs/usbcdc11.pdf
984263bc
MD
74 */
75
76/*
77 * TODO:
78 * - Add error recovery in various places; the big problem is what
79 * to do in a callback if there is an error.
80 * - Implement a Call Device for modems without multiplexed commands.
81 *
82 */
83
84#include <sys/param.h>
85#include <sys/systm.h>
86#include <sys/kernel.h>
984263bc 87#include <sys/ioccom.h>
984263bc
MD
88#include <sys/conf.h>
89#include <sys/tty.h>
984263bc
MD
90#include <sys/file.h>
91#include <sys/select.h>
1550dfd9 92#include <sys/sysctl.h>
984263bc
MD
93#include <sys/proc.h>
94#include <sys/vnode.h>
1550dfd9 95#include <sys/bus.h>
984263bc 96#include <sys/poll.h>
984263bc 97
1f2de5d4
MD
98#include <bus/usb/usb.h>
99#include <bus/usb/usbcdc.h>
984263bc 100
1f2de5d4
MD
101#include <bus/usb/usbdi.h>
102#include <bus/usb/usbdi_util.h>
103#include <bus/usb/usbdevs.h>
1550dfd9 104#include <dev/usbmisc/ucom/ucomvar.h>
1f2de5d4 105#include <bus/usb/usb_quirks.h>
984263bc 106
d2438d69 107#include <bus/usb/usbdevs.h>
984263bc
MD
108
109#ifdef USB_DEBUG
984263bc
MD
110int umodemdebug = 0;
111SYSCTL_NODE(_hw_usb, OID_AUTO, umodem, CTLFLAG_RW, 0, "USB umodem");
112SYSCTL_INT(_hw_usb_umodem, OID_AUTO, debug, CTLFLAG_RW,
113 &umodemdebug, 0, "umodem debug level");
1550dfd9 114#define DPRINTFN(n, x) if (umodemdebug > (n)) logprintf x
984263bc 115#else
984263bc
MD
116#define DPRINTFN(n, x)
117#endif
1550dfd9 118#define DPRINTF(x) DPRINTFN(0, x)
984263bc 119
1550dfd9
MD
120/*
121 * These are the maximum number of bytes transferred per frame.
122 * If some really high speed devices should use this driver they
123 * may need to be increased, but this is good enough for normal modems.
124 */
984263bc 125#define UMODEMIBUFSIZE 64
1550dfd9
MD
126#define UMODEMOBUFSIZE 256
127
128#define UMODEM_MODVER 1 /* module version */
984263bc
MD
129
130struct umodem_softc {
1550dfd9
MD
131 struct ucom_softc sc_ucom;
132
984263bc
MD
133 USBBASEDEVICE sc_dev; /* base device */
134
135 usbd_device_handle sc_udev; /* USB device */
136
137 int sc_ctl_iface_no;
138 usbd_interface_handle sc_ctl_iface; /* control interface */
139 int sc_data_iface_no;
140 usbd_interface_handle sc_data_iface; /* data interface */
141
984263bc
MD
142 int sc_cm_cap; /* CM capabilities */
143 int sc_acm_cap; /* ACM capabilities */
144
145 int sc_cm_over_data;
146
984263bc
MD
147 usb_cdc_line_state_t sc_line_state; /* current line state */
148 u_char sc_dtr; /* current DTR state */
1550dfd9 149 u_char sc_rts; /* current RTS state */
984263bc
MD
150
151 u_char sc_opening; /* lock during open */
984263bc 152
1550dfd9
MD
153 int sc_ctl_notify; /* Notification endpoint */
154 usbd_pipe_handle sc_notify_pipe; /* Notification pipe */
155 usb_cdc_notification_t sc_notify_buf; /* Notification structure */
156 u_char sc_lsr; /* Local status register */
157 u_char sc_msr; /* Modem status register */
984263bc
MD
158};
159
1550dfd9
MD
160Static void *umodem_get_desc(usbd_device_handle dev, int type, int subtype);
161Static usbd_status umodem_set_comm_feature(struct umodem_softc *sc,
162 int feature, int state);
163Static usbd_status umodem_set_line_coding(struct umodem_softc *sc,
164 usb_cdc_line_state_t *state);
165
166Static void umodem_get_caps(usbd_device_handle, int *, int *);
167
168Static void umodem_get_status(void *, int portno, u_char *lsr, u_char *msr);
169Static void umodem_set(void *, int, int, int);
170Static void umodem_dtr(struct umodem_softc *, int);
171Static void umodem_rts(struct umodem_softc *, int);
172Static void umodem_break(struct umodem_softc *, int);
173Static void umodem_set_line_state(struct umodem_softc *);
174Static int umodem_param(void *, int, struct termios *);
175Static int umodem_ioctl(void *, int, u_long, caddr_t, int, usb_proc_ptr );
176Static int umodem_open(void *, int portno);
177Static void umodem_close(void *, int portno);
178Static void umodem_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
179
180Static struct ucom_callback umodem_callback = {
181 umodem_get_status,
182 umodem_set,
183 umodem_param,
184 umodem_ioctl,
185 umodem_open,
186 umodem_close,
187 NULL,
188 NULL,
189};
190
191Static device_probe_t umodem_match;
192Static device_attach_t umodem_attach;
193Static device_detach_t umodem_detach;
194
195Static device_method_t umodem_methods[] = {
196 /* Device interface */
197 DEVMETHOD(device_probe, umodem_match),
198 DEVMETHOD(device_attach, umodem_attach),
199 DEVMETHOD(device_detach, umodem_detach),
200 { 0, 0 }
201};
202
203Static driver_t umodem_driver = {
204 "ucom",
205 umodem_methods,
206 sizeof (struct umodem_softc)
984263bc 207};
984263bc 208
1550dfd9
MD
209DRIVER_MODULE(umodem, uhub, umodem_driver, ucom_devclass, usbd_driver_load, 0);
210MODULE_DEPEND(umodem, usb, 1, 1, 1);
211MODULE_DEPEND(umodem, ucom, UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);
212MODULE_VERSION(umodem, UMODEM_MODVER);
984263bc
MD
213
214USB_MATCH(umodem)
215{
216 USB_MATCH_START(umodem, uaa);
217 usb_interface_descriptor_t *id;
218 int cm, acm;
1550dfd9
MD
219
220 if (uaa->iface == NULL)
984263bc
MD
221 return (UMATCH_NONE);
222
223 id = usbd_get_interface_descriptor(uaa->iface);
1550dfd9 224 if (id == NULL ||
984263bc
MD
225 id->bInterfaceClass != UICLASS_CDC ||
226 id->bInterfaceSubClass != UISUBCLASS_ABSTRACT_CONTROL_MODEL ||
227 id->bInterfaceProtocol != UIPROTO_CDC_AT)
228 return (UMATCH_NONE);
1550dfd9 229
984263bc
MD
230 umodem_get_caps(uaa->device, &cm, &acm);
231 if (!(cm & USB_CDC_CM_DOES_CM) ||
232 !(cm & USB_CDC_CM_OVER_DATA) ||
233 !(acm & USB_CDC_ACM_HAS_LINE))
234 return (UMATCH_NONE);
235
236 return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
237}
238
239USB_ATTACH(umodem)
240{
241 USB_ATTACH_START(umodem, sc, uaa);
242 usbd_device_handle dev = uaa->device;
243 usb_interface_descriptor_t *id;
244 usb_endpoint_descriptor_t *ed;
245 usb_cdc_cm_descriptor_t *cmd;
1550dfd9
MD
246 char *devinfo = NULL;
247 const char *devname;
984263bc 248 usbd_status err;
1550dfd9 249 int data_ifcno;
984263bc 250 int i;
1550dfd9
MD
251 struct ucom_softc *ucom;
252
efda3bd0 253 devinfo = kmalloc(1024, M_USBDEV, M_INTWAIT);
1550dfd9
MD
254 usbd_devinfo(dev, 0, devinfo);
255 ucom = &sc->sc_ucom;
256 ucom->sc_dev = self;
257 sc->sc_dev = self;
258 device_set_desc_copy(self, devinfo);
259 ucom->sc_udev = dev;
260 ucom->sc_iface = uaa->iface;
261 /*USB_ATTACH_SETUP; */
984263bc
MD
262
263 sc->sc_udev = dev;
984263bc 264 sc->sc_ctl_iface = uaa->iface;
1550dfd9
MD
265
266 devname = USBDEVNAME(sc->sc_dev);
267 /* XXX ? use something else ? XXX */
984263bc 268 id = usbd_get_interface_descriptor(sc->sc_ctl_iface);
e3869ec7 269 kprintf("%s: %s, iclass %d/%d\n", devname, devinfo,
1550dfd9 270 id->bInterfaceClass, id->bInterfaceSubClass);
984263bc
MD
271 sc->sc_ctl_iface_no = id->bInterfaceNumber;
272
273 umodem_get_caps(dev, &sc->sc_cm_cap, &sc->sc_acm_cap);
274
275 /* Get the data interface no. */
276 cmd = umodem_get_desc(dev, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM);
1550dfd9 277 if (cmd == NULL) {
e3869ec7 278 kprintf("%s: no CM descriptor\n", devname);
984263bc
MD
279 goto bad;
280 }
1550dfd9 281 sc->sc_data_iface_no = data_ifcno = cmd->bDataInterface;
984263bc 282
e3869ec7 283 kprintf("%s: data interface %d, has %sCM over data, has %sbreak\n",
1550dfd9 284 devname, data_ifcno,
984263bc
MD
285 sc->sc_cm_cap & USB_CDC_CM_OVER_DATA ? "" : "no ",
286 sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK ? "" : "no ");
287
984263bc
MD
288 /* Get the data interface too. */
289 for (i = 0; i < uaa->nifaces; i++) {
1550dfd9 290 if (uaa->ifaces[i] != NULL) {
984263bc 291 id = usbd_get_interface_descriptor(uaa->ifaces[i]);
1550dfd9 292 if (id != NULL && id->bInterfaceNumber == data_ifcno) {
984263bc 293 sc->sc_data_iface = uaa->ifaces[i];
1550dfd9 294 uaa->ifaces[i] = NULL;
984263bc
MD
295 }
296 }
297 }
1550dfd9 298 if (sc->sc_data_iface == NULL) {
e3869ec7 299 kprintf("%s: no data interface\n", devname);
984263bc
MD
300 goto bad;
301 }
1550dfd9 302 ucom->sc_iface = sc->sc_data_iface;
984263bc 303
1550dfd9
MD
304 /*
305 * Find the bulk endpoints.
984263bc
MD
306 * Iterate over all endpoints in the data interface and take note.
307 */
1550dfd9 308 ucom->sc_bulkin_no = ucom->sc_bulkout_no = -1;
984263bc
MD
309
310 id = usbd_get_interface_descriptor(sc->sc_data_iface);
311 for (i = 0; i < id->bNumEndpoints; i++) {
312 ed = usbd_interface2endpoint_descriptor(sc->sc_data_iface, i);
1550dfd9 313 if (ed == NULL) {
e3869ec7 314 kprintf("%s: no endpoint descriptor for %d\n", devname,
1550dfd9 315 i);
984263bc
MD
316 goto bad;
317 }
318 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
1550dfd9
MD
319 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
320 ucom->sc_bulkin_no = ed->bEndpointAddress;
984263bc 321 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
1550dfd9
MD
322 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
323 ucom->sc_bulkout_no = ed->bEndpointAddress;
984263bc
MD
324 }
325 }
326
1550dfd9 327 if (ucom->sc_bulkin_no == -1) {
e3869ec7 328 kprintf("%s: Could not find data bulk in\n", devname);
984263bc
MD
329 goto bad;
330 }
1550dfd9 331 if (ucom->sc_bulkout_no == -1) {
e3869ec7 332 kprintf("%s: Could not find data bulk out\n", devname);
984263bc
MD
333 goto bad;
334 }
335
336 if (usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_ASSUME_CM_OVER_DATA) {
1550dfd9 337 DPRINTF(("Quirk says to assume CM over data\n"));
984263bc
MD
338 sc->sc_cm_over_data = 1;
339 } else {
1550dfd9
MD
340 if (sc->sc_cm_cap & USB_CDC_CM_OVER_DATA) {
341 if (sc->sc_acm_cap & USB_CDC_ACM_HAS_FEATURE)
342 err = umodem_set_comm_feature(sc,
343 UCDC_ABSTRACT_STATE, UCDC_DATA_MULTIPLEXED);
344 else
345 err = 0;
346 if (err) {
e3869ec7 347 kprintf("%s: could not set data multiplex mode\n",
1550dfd9
MD
348 devname);
349 goto bad;
350 }
351 sc->sc_cm_over_data = 1;
352 }
984263bc
MD
353 }
354
1550dfd9
MD
355 /*
356 * The standard allows for notification messages (to indicate things
357 * like a modem hangup) to come in via an interrupt endpoint
358 * off of the control interface. Iterate over the endpoints on
359 * the control interface and see if there are any interrupt
360 * endpoints; if there are, then register it.
361 */
984263bc 362
1550dfd9
MD
363 sc->sc_ctl_notify = -1;
364 sc->sc_notify_pipe = NULL;
365
366 for (i = 0; i < id->bNumEndpoints; i++) {
367 ed = usbd_interface2endpoint_descriptor(sc->sc_ctl_iface, i);
368 if (ed == NULL)
369 continue;
370
371 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
372 (ed->bmAttributes & UE_XFERTYPE) == UE_INTERRUPT) {
e3869ec7 373 kprintf("%s: status change notification available\n",
1550dfd9
MD
374 devname);
375 sc->sc_ctl_notify = ed->bEndpointAddress;
376 }
377 }
984263bc
MD
378
379 sc->sc_dtr = -1;
380
1550dfd9
MD
381 ucom->sc_parent = sc;
382 ucom->sc_portno = UCOM_UNK_PORTNO;
383 /* bulkin, bulkout set above */
384 ucom->sc_ibufsize = UMODEMIBUFSIZE;
385 ucom->sc_obufsize = UMODEMOBUFSIZE;
386 ucom->sc_ibufsizepad = UMODEMIBUFSIZE;
387 ucom->sc_opkthdrlen = 0;
388 ucom->sc_callback = &umodem_callback;
389
390 ucom_attach(&sc->sc_ucom);
391
efda3bd0 392 kfree(devinfo, M_USBDEV);
984263bc
MD
393 USB_ATTACH_SUCCESS_RETURN;
394
395 bad:
1550dfd9 396 ucom->sc_dying = 1;
efda3bd0 397 kfree(devinfo, M_USBDEV);
984263bc
MD
398 USB_ATTACH_ERROR_RETURN;
399}
400
1550dfd9
MD
401Static int
402umodem_open(void *addr, int portno)
984263bc 403{
1550dfd9
MD
404 struct umodem_softc *sc = addr;
405 int err;
984263bc 406
1550dfd9 407 DPRINTF(("umodem_open: sc=%p\n", sc));
984263bc 408
1550dfd9
MD
409 if (sc->sc_ctl_notify != -1 && sc->sc_notify_pipe == NULL) {
410 err = usbd_open_pipe_intr(sc->sc_ctl_iface, sc->sc_ctl_notify,
411 USBD_SHORT_XFER_OK, &sc->sc_notify_pipe, sc,
412 &sc->sc_notify_buf, sizeof(sc->sc_notify_buf),
413 umodem_intr, USBD_DEFAULT_INTERVAL);
984263bc 414
1550dfd9
MD
415 if (err) {
416 DPRINTF(("Failed to establish notify pipe: %s\n",
417 usbd_errstr(err)));
418 return EIO;
419 }
984263bc 420 }
984263bc 421
1550dfd9
MD
422 return 0;
423}
984263bc 424
1550dfd9
MD
425Static void
426umodem_close(void *addr, int portno)
427{
428 struct umodem_softc *sc = addr;
429 int err;
984263bc 430
1550dfd9 431 DPRINTF(("umodem_close: sc=%p\n", sc));
984263bc 432
1550dfd9
MD
433 if (sc->sc_notify_pipe != NULL) {
434 err = usbd_abort_pipe(sc->sc_notify_pipe);
435 if (err)
e3869ec7 436 kprintf("%s: abort notify pipe failed: %s\n",
1550dfd9
MD
437 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
438 err = usbd_close_pipe(sc->sc_notify_pipe);
439 if (err)
e3869ec7 440 kprintf("%s: close notify pipe failed: %s\n",
1550dfd9
MD
441 USBDEVNAME(sc->sc_dev), usbd_errstr(err));
442 sc->sc_notify_pipe = NULL;
984263bc 443 }
1550dfd9 444}
984263bc 445
1550dfd9
MD
446Static void
447umodem_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
448{
449 struct umodem_softc *sc = priv;
450 u_char mstatus;
984263bc 451
1550dfd9
MD
452 if (sc->sc_ucom.sc_dying)
453 return;
984263bc 454
1550dfd9
MD
455 if (status != USBD_NORMAL_COMPLETION) {
456 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
457 return;
e3869ec7 458 kprintf("%s: abnormal status: %s\n", USBDEVNAME(sc->sc_dev),
1550dfd9 459 usbd_errstr(status));
984263bc
MD
460 return;
461 }
462
1550dfd9
MD
463 if (sc->sc_notify_buf.bmRequestType != UCDC_NOTIFICATION) {
464 DPRINTF(("%s: unknown message type (%02x) on notify pipe\n",
465 USBDEVNAME(sc->sc_dev),
466 sc->sc_notify_buf.bmRequestType));
467 return;
468 }
984263bc 469
1550dfd9
MD
470 switch (sc->sc_notify_buf.bNotification) {
471 case UCDC_N_SERIAL_STATE:
472 /*
473 * Set the serial state in ucom driver based on
474 * the bits from the notify message
475 */
476 if (UGETW(sc->sc_notify_buf.wLength) != 2) {
e3869ec7 477 kprintf("%s: Invalid notification length! (%d)\n",
1550dfd9
MD
478 USBDEVNAME(sc->sc_dev),
479 UGETW(sc->sc_notify_buf.wLength));
480 break;
481 }
482 DPRINTF(("%s: notify bytes = %02x%02x\n",
483 USBDEVNAME(sc->sc_dev),
484 sc->sc_notify_buf.data[0],
485 sc->sc_notify_buf.data[1]));
486 /* Currently, lsr is always zero. */
487 sc->sc_lsr = sc->sc_msr = 0;
488 mstatus = sc->sc_notify_buf.data[0];
489
490 if (ISSET(mstatus, UCDC_N_SERIAL_RI))
491 sc->sc_msr |= UMSR_RI;
492 if (ISSET(mstatus, UCDC_N_SERIAL_DSR))
493 sc->sc_msr |= UMSR_DSR;
494 if (ISSET(mstatus, UCDC_N_SERIAL_DCD))
495 sc->sc_msr |= UMSR_DCD;
496 ucom_status_change(&sc->sc_ucom);
497 break;
498 default:
499 DPRINTF(("%s: unknown notify message: %02x\n",
500 USBDEVNAME(sc->sc_dev),
501 sc->sc_notify_buf.bNotification));
502 break;
503 }
984263bc
MD
504}
505
506void
1550dfd9 507umodem_get_caps(usbd_device_handle dev, int *cm, int *acm)
984263bc 508{
1550dfd9
MD
509 usb_cdc_cm_descriptor_t *cmd;
510 usb_cdc_acm_descriptor_t *cad;
984263bc 511
1550dfd9 512 *cm = *acm = 0;
984263bc 513
1550dfd9
MD
514 cmd = umodem_get_desc(dev, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM);
515 if (cmd == NULL) {
516 DPRINTF(("umodem_get_desc: no CM desc\n"));
984263bc 517 return;
1550dfd9
MD
518 }
519 *cm = cmd->bmCapabilities;
984263bc 520
1550dfd9
MD
521 cad = umodem_get_desc(dev, UDESC_CS_INTERFACE, UDESCSUB_CDC_ACM);
522 if (cad == NULL) {
523 DPRINTF(("umodem_get_desc: no ACM desc\n"));
984263bc
MD
524 return;
525 }
1550dfd9
MD
526 *acm = cad->bmCapabilities;
527}
528
529void
530umodem_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
531{
532 struct umodem_softc *sc = addr;
984263bc 533
1550dfd9 534 DPRINTF(("umodem_get_status:\n"));
984263bc 535
1550dfd9
MD
536 if (lsr != NULL)
537 *lsr = sc->sc_lsr;
538 if (msr != NULL)
539 *msr = sc->sc_msr;
984263bc
MD
540}
541
542int
1550dfd9 543umodem_param(void *addr, int portno, struct termios *t)
984263bc 544{
1550dfd9
MD
545 struct umodem_softc *sc = addr;
546 usbd_status err;
984263bc
MD
547 usb_cdc_line_state_t ls;
548
1550dfd9 549 DPRINTF(("umodem_param: sc=%p\n", sc));
984263bc
MD
550
551 USETDW(ls.dwDTERate, t->c_ospeed);
552 if (ISSET(t->c_cflag, CSTOPB))
553 ls.bCharFormat = UCDC_STOP_BIT_2;
554 else
555 ls.bCharFormat = UCDC_STOP_BIT_1;
556 if (ISSET(t->c_cflag, PARENB)) {
557 if (ISSET(t->c_cflag, PARODD))
558 ls.bParityType = UCDC_PARITY_ODD;
559 else
560 ls.bParityType = UCDC_PARITY_EVEN;
561 } else
562 ls.bParityType = UCDC_PARITY_NONE;
563 switch (ISSET(t->c_cflag, CSIZE)) {
564 case CS5:
565 ls.bDataBits = 5;
566 break;
567 case CS6:
568 ls.bDataBits = 6;
569 break;
570 case CS7:
571 ls.bDataBits = 7;
572 break;
573 case CS8:
574 ls.bDataBits = 8;
575 break;
576 }
984263bc 577
1550dfd9 578 err = umodem_set_line_coding(sc, &ls);
984263bc 579 if (err) {
1550dfd9
MD
580 DPRINTF(("umodem_param: err=%s\n", usbd_errstr(err)));
581 return (ENOTTY);
984263bc 582 }
984263bc
MD
583 return (0);
584}
984263bc
MD
585
586int
1550dfd9
MD
587umodem_ioctl(void *addr, int portno, u_long cmd, caddr_t data, int flag,
588 usb_proc_ptr p)
984263bc 589{
1550dfd9
MD
590 struct umodem_softc *sc = addr;
591 int error = 0;
984263bc 592
1550dfd9 593 if (sc->sc_ucom.sc_dying)
984263bc 594 return (EIO);
984263bc 595
984263bc
MD
596 DPRINTF(("umodemioctl: cmd=0x%08lx\n", cmd));
597
984263bc 598 switch (cmd) {
984263bc
MD
599 case USB_GET_CM_OVER_DATA:
600 *(int *)data = sc->sc_cm_over_data;
601 break;
602
603 case USB_SET_CM_OVER_DATA:
604 if (*(int *)data != sc->sc_cm_over_data) {
605 /* XXX change it */
606 }
607 break;
608
609 default:
610 DPRINTF(("umodemioctl: unknown\n"));
611 error = ENOTTY;
1550dfd9 612 break;
984263bc
MD
613 }
614
1550dfd9 615 return (error);
984263bc
MD
616}
617
618void
1550dfd9 619umodem_dtr(struct umodem_softc *sc, int onoff)
984263bc 620{
1550dfd9 621 DPRINTF(("umodem_modem: onoff=%d\n", onoff));
984263bc 622
1550dfd9
MD
623 if (sc->sc_dtr == onoff)
624 return;
625 sc->sc_dtr = onoff;
626
627 umodem_set_line_state(sc);
984263bc
MD
628}
629
630void
1550dfd9 631umodem_rts(struct umodem_softc *sc, int onoff)
984263bc 632{
1550dfd9 633 DPRINTF(("umodem_modem: onoff=%d\n", onoff));
984263bc 634
1550dfd9 635 if (sc->sc_rts == onoff)
984263bc 636 return;
1550dfd9 637 sc->sc_rts = onoff;
984263bc 638
1550dfd9
MD
639 umodem_set_line_state(sc);
640}
641
642void
643umodem_set_line_state(struct umodem_softc *sc)
644{
645 usb_device_request_t req;
646 int ls;
647
648 ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
649 (sc->sc_rts ? UCDC_LINE_RTS : 0);
984263bc
MD
650 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
651 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
1550dfd9 652 USETW(req.wValue, ls);
984263bc
MD
653 USETW(req.wIndex, sc->sc_ctl_iface_no);
654 USETW(req.wLength, 0);
655
656 (void)usbd_do_request(sc->sc_udev, &req, 0);
657
984263bc
MD
658}
659
660void
661umodem_break(struct umodem_softc *sc, int onoff)
662{
663 usb_device_request_t req;
664
1550dfd9 665 DPRINTF(("umodem_break: onoff=%d\n", onoff));
984263bc
MD
666
667 if (!(sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK))
668 return;
669
670 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
671 req.bRequest = UCDC_SEND_BREAK;
672 USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
673 USETW(req.wIndex, sc->sc_ctl_iface_no);
674 USETW(req.wLength, 0);
675
676 (void)usbd_do_request(sc->sc_udev, &req, 0);
677}
678
1550dfd9
MD
679void
680umodem_set(void *addr, int portno, int reg, int onoff)
681{
682 struct umodem_softc *sc = addr;
683
684 switch (reg) {
685 case UCOM_SET_DTR:
686 umodem_dtr(sc, onoff);
687 break;
688 case UCOM_SET_RTS:
689 umodem_rts(sc, onoff);
690 break;
691 case UCOM_SET_BREAK:
692 umodem_break(sc, onoff);
693 break;
694 default:
695 break;
696 }
697}
698
699usbd_status
700umodem_set_line_coding(struct umodem_softc *sc, usb_cdc_line_state_t *state)
701{
702 usb_device_request_t req;
703 usbd_status err;
704
705 DPRINTF(("umodem_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
706 UGETDW(state->dwDTERate), state->bCharFormat,
707 state->bParityType, state->bDataBits));
708
709 if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
710 DPRINTF(("umodem_set_line_coding: already set\n"));
711 return (USBD_NORMAL_COMPLETION);
712 }
713
714 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
715 req.bRequest = UCDC_SET_LINE_CODING;
716 USETW(req.wValue, 0);
717 USETW(req.wIndex, sc->sc_ctl_iface_no);
718 USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
719
720 err = usbd_do_request(sc->sc_udev, &req, state);
721 if (err) {
722 DPRINTF(("umodem_set_line_coding: failed, err=%s\n",
723 usbd_errstr(err)));
724 return (err);
725 }
726
727 sc->sc_line_state = *state;
728
729 return (USBD_NORMAL_COMPLETION);
730}
731
984263bc
MD
732void *
733umodem_get_desc(usbd_device_handle dev, int type, int subtype)
734{
735 usb_descriptor_t *desc;
736 usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
737 uByte *p = (uByte *)cd;
738 uByte *end = p + UGETW(cd->wTotalLength);
739
740 while (p < end) {
741 desc = (usb_descriptor_t *)p;
742 if (desc->bDescriptorType == type &&
743 desc->bDescriptorSubtype == subtype)
744 return (desc);
745 p += desc->bLength;
746 }
747
748 return (0);
749}
750
751usbd_status
752umodem_set_comm_feature(struct umodem_softc *sc, int feature, int state)
753{
754 usb_device_request_t req;
755 usbd_status err;
756 usb_cdc_abstract_state_t ast;
757
1550dfd9
MD
758 DPRINTF(("umodem_set_comm_feature: feature=%d state=%d\n", feature,
759 state));
760
984263bc
MD
761 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
762 req.bRequest = UCDC_SET_COMM_FEATURE;
763 USETW(req.wValue, feature);
764 USETW(req.wIndex, sc->sc_ctl_iface_no);
765 USETW(req.wLength, UCDC_ABSTRACT_STATE_LENGTH);
766 USETW(ast.wState, state);
767
768 err = usbd_do_request(sc->sc_udev, &req, &ast);
769 if (err) {
1550dfd9
MD
770 DPRINTF(("umodem_set_comm_feature: feature=%d, err=%s\n",
771 feature, usbd_errstr(err)));
984263bc
MD
772 return (err);
773 }
774
775 return (USBD_NORMAL_COMPLETION);
776}
777
984263bc
MD
778USB_DETACH(umodem)
779{
780 USB_DETACH_START(umodem, sc);
1550dfd9 781 int rv = 0;
984263bc 782
1550dfd9 783 DPRINTF(("umodem_detach: sc=%p\n", sc));
984263bc 784
1550dfd9
MD
785 if (sc->sc_notify_pipe != NULL) {
786 usbd_abort_pipe(sc->sc_notify_pipe);
787 usbd_close_pipe(sc->sc_notify_pipe);
788 sc->sc_notify_pipe = NULL;
984263bc 789 }
984263bc 790
1550dfd9
MD
791 sc->sc_ucom.sc_dying = 1;
792 rv = ucom_detach(&sc->sc_ucom);
984263bc 793
1550dfd9 794 return (rv);
984263bc 795}