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