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