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