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