umct.c revision 1.2.4.2 1 1.2.4.2 bouyer /* $NetBSD: umct.c,v 1.2.4.2 2001/04/21 17:50:04 bouyer Exp $ */
2 1.2.4.2 bouyer /*
3 1.2.4.2 bouyer * Copyright (c) 2001 The NetBSD Foundation, Inc.
4 1.2.4.2 bouyer * All rights reserved.
5 1.2.4.2 bouyer *
6 1.2.4.2 bouyer * This code is derived from software contributed to The NetBSD Foundation
7 1.2.4.2 bouyer * by Ichiro FUKUHARA (ichiro (at) ichiro.org).
8 1.2.4.2 bouyer *
9 1.2.4.2 bouyer * Redistribution and use in source and binary forms, with or without
10 1.2.4.2 bouyer * modification, are permitted provided that the following conditions
11 1.2.4.2 bouyer * are met:
12 1.2.4.2 bouyer * 1. Redistributions of source code must retain the above copyright
13 1.2.4.2 bouyer * notice, this list of conditions and the following disclaimer.
14 1.2.4.2 bouyer * 2. Redistributions in binary form must reproduce the above copyright
15 1.2.4.2 bouyer * notice, this list of conditions and the following disclaimer in the
16 1.2.4.2 bouyer * documentation and/or other materials provided with the distribution.
17 1.2.4.2 bouyer * 3. All advertising materials mentioning features or use of this software
18 1.2.4.2 bouyer * must display the following acknowledgement:
19 1.2.4.2 bouyer * This product includes software developed by the NetBSD
20 1.2.4.2 bouyer * Foundation, Inc. and its contributors.
21 1.2.4.2 bouyer * 4. Neither the name of The NetBSD Foundation nor the names of its
22 1.2.4.2 bouyer * contributors may be used to endorse or promote products derived
23 1.2.4.2 bouyer * from this software without specific prior written permission.
24 1.2.4.2 bouyer *
25 1.2.4.2 bouyer * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26 1.2.4.2 bouyer * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.2.4.2 bouyer * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.2.4.2 bouyer * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29 1.2.4.2 bouyer * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.2.4.2 bouyer * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.2.4.2 bouyer * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.2.4.2 bouyer * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.2.4.2 bouyer * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.2.4.2 bouyer * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.2.4.2 bouyer * POSSIBILITY OF SUCH DAMAGE.
36 1.2.4.2 bouyer */
37 1.2.4.2 bouyer
38 1.2.4.2 bouyer /*
39 1.2.4.2 bouyer * This driver is for the device MCT USB-RS232 Interfact Controller.
40 1.2.4.2 bouyer * http://www.mct.com.tw/p_u232.html
41 1.2.4.2 bouyer * http://www.dlink.com/products/usb/dsbs25
42 1.2.4.2 bouyer *
43 1.2.4.2 bouyer */
44 1.2.4.2 bouyer
45 1.2.4.2 bouyer #include <sys/param.h>
46 1.2.4.2 bouyer #include <sys/systm.h>
47 1.2.4.2 bouyer #include <sys/kernel.h>
48 1.2.4.2 bouyer #include <sys/malloc.h>
49 1.2.4.2 bouyer #include <sys/ioctl.h>
50 1.2.4.2 bouyer #include <sys/conf.h>
51 1.2.4.2 bouyer #include <sys/tty.h>
52 1.2.4.2 bouyer #include <sys/file.h>
53 1.2.4.2 bouyer #include <sys/select.h>
54 1.2.4.2 bouyer #include <sys/proc.h>
55 1.2.4.2 bouyer #include <sys/vnode.h>
56 1.2.4.2 bouyer #include <sys/device.h>
57 1.2.4.2 bouyer #include <sys/poll.h>
58 1.2.4.2 bouyer
59 1.2.4.2 bouyer #include <dev/usb/usb.h>
60 1.2.4.2 bouyer #include <dev/usb/usbcdc.h>
61 1.2.4.2 bouyer
62 1.2.4.2 bouyer #include <dev/usb/usbdi.h>
63 1.2.4.2 bouyer #include <dev/usb/usbdi_util.h>
64 1.2.4.2 bouyer #include <dev/usb/usbdevs.h>
65 1.2.4.2 bouyer #include <dev/usb/usb_quirks.h>
66 1.2.4.2 bouyer
67 1.2.4.2 bouyer #include <dev/usb/usbdevs.h>
68 1.2.4.2 bouyer #include <dev/usb/ucomvar.h>
69 1.2.4.2 bouyer
70 1.2.4.2 bouyer #include <dev/usb/umct.h>
71 1.2.4.2 bouyer
72 1.2.4.2 bouyer #ifdef UMCT_DEBUG
73 1.2.4.2 bouyer #define DPRINTFN(n, x) if (umctdebug > (n)) logprintf x
74 1.2.4.2 bouyer int umctdebug = 0;
75 1.2.4.2 bouyer #else
76 1.2.4.2 bouyer #define DPRINTFN(n, x)
77 1.2.4.2 bouyer #endif
78 1.2.4.2 bouyer #define DPRINTF(x) DPRINTFN(0, x)
79 1.2.4.2 bouyer
80 1.2.4.2 bouyer #define UMCT_CONFIG_INDEX 0
81 1.2.4.2 bouyer #define UMCT_IFACE_INDEX 0
82 1.2.4.2 bouyer
83 1.2.4.2 bouyer struct umct_softc {
84 1.2.4.2 bouyer USBBASEDEVICE sc_dev; /* base device */
85 1.2.4.2 bouyer usbd_device_handle sc_udev; /* USB device */
86 1.2.4.2 bouyer usbd_interface_handle sc_iface; /* interface */
87 1.2.4.2 bouyer int sc_iface_number; /* interface number */
88 1.2.4.2 bouyer
89 1.2.4.2 bouyer int sc_intr_number; /* interrupt number */
90 1.2.4.2 bouyer usbd_pipe_handle sc_intr_pipe; /* interrupt pipe */
91 1.2.4.2 bouyer u_char *sc_intr_buf; /* interrupt buffer */
92 1.2.4.2 bouyer int sc_isize;
93 1.2.4.2 bouyer
94 1.2.4.2 bouyer usb_cdc_line_state_t sc_line_state; /* current line state */
95 1.2.4.2 bouyer u_char sc_dtr; /* current DTR state */
96 1.2.4.2 bouyer u_char sc_rts; /* current RTS state */
97 1.2.4.2 bouyer u_char sc_break; /* set break */
98 1.2.4.2 bouyer
99 1.2.4.2 bouyer u_char sc_status;
100 1.2.4.2 bouyer
101 1.2.4.2 bouyer device_ptr_t sc_subdev; /* ucom device */
102 1.2.4.2 bouyer
103 1.2.4.2 bouyer u_char sc_dying; /* disconnecting */
104 1.2.4.2 bouyer
105 1.2.4.2 bouyer u_char sc_lsr; /* Local status register */
106 1.2.4.2 bouyer u_char sc_msr; /* umct status register */
107 1.2.4.2 bouyer };
108 1.2.4.2 bouyer
109 1.2.4.2 bouyer /*
110 1.2.4.2 bouyer * These are the maximum number of bytes transferred per frame.
111 1.2.4.2 bouyer * The output buffer size cannot be increased due to the size encoding.
112 1.2.4.2 bouyer */
113 1.2.4.2 bouyer #define UMCTIBUFSIZE 256
114 1.2.4.2 bouyer #define UMCTOBUFSIZE 256
115 1.2.4.2 bouyer
116 1.2.4.2 bouyer Static void umct_init(struct umct_softc *);
117 1.2.4.2 bouyer Static void umct_set_baudrate(struct umct_softc *, void *);
118 1.2.4.2 bouyer Static void umct_set_lcr(struct umct_softc *, void *);
119 1.2.4.2 bouyer Static void umct_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
120 1.2.4.2 bouyer
121 1.2.4.2 bouyer Static void umct_set(void *, int, int, int);
122 1.2.4.2 bouyer Static void umct_dtr(struct umct_softc *, int);
123 1.2.4.2 bouyer Static void umct_rts(struct umct_softc *, int);
124 1.2.4.2 bouyer Static void umct_break(struct umct_softc *, int);
125 1.2.4.2 bouyer Static void umct_set_line_state(struct umct_softc *);
126 1.2.4.2 bouyer Static void umct_get_status(void *, int portno, u_char *lsr, u_char *msr);
127 1.2.4.2 bouyer Static int umct_param(void *, int, struct termios *);
128 1.2.4.2 bouyer Static int umct_open(void *, int);
129 1.2.4.2 bouyer Static void umct_close(void *, int);
130 1.2.4.2 bouyer
131 1.2.4.2 bouyer struct ucom_methods umct_methods = {
132 1.2.4.2 bouyer umct_get_status,
133 1.2.4.2 bouyer umct_set,
134 1.2.4.2 bouyer umct_param,
135 1.2.4.2 bouyer NULL,
136 1.2.4.2 bouyer umct_open,
137 1.2.4.2 bouyer umct_close,
138 1.2.4.2 bouyer NULL,
139 1.2.4.2 bouyer NULL,
140 1.2.4.2 bouyer };
141 1.2.4.2 bouyer
142 1.2.4.2 bouyer static const struct umct_product {
143 1.2.4.2 bouyer uint16_t vendor;
144 1.2.4.2 bouyer uint16_t product;
145 1.2.4.2 bouyer } umct_products [] = {
146 1.2.4.2 bouyer /* MCT USB-232 Interface Products */
147 1.2.4.2 bouyer { USB_VENDOR_MCT, USB_PRODUCT_MCT_USB232 },
148 1.2.4.2 bouyer /* Sitecom USB-232 Products */
149 1.2.4.2 bouyer { USB_VENDOR_MCT, USB_PRODUCT_MCT_SITECOM_USB232 },
150 1.2.4.2 bouyer /* D-Link DU-H3SP USB BAY Hub Products */
151 1.2.4.2 bouyer { USB_VENDOR_MCT, USB_PRODUCT_MCT_DU_H3SP_USB232 },
152 1.2.4.2 bouyer
153 1.2.4.2 bouyer { 0, 0 }
154 1.2.4.2 bouyer };
155 1.2.4.2 bouyer
156 1.2.4.2 bouyer USB_DECLARE_DRIVER(umct);
157 1.2.4.2 bouyer
158 1.2.4.2 bouyer USB_MATCH(umct)
159 1.2.4.2 bouyer {
160 1.2.4.2 bouyer USB_MATCH_START(umct, uaa);
161 1.2.4.2 bouyer int i;
162 1.2.4.2 bouyer
163 1.2.4.2 bouyer if (uaa->iface != NULL)
164 1.2.4.2 bouyer return (UMATCH_NONE);
165 1.2.4.2 bouyer
166 1.2.4.2 bouyer for (i = 0; umct_products[i].vendor != 0; i++) {
167 1.2.4.2 bouyer if (umct_products[i].vendor == uaa->vendor &&
168 1.2.4.2 bouyer umct_products[i].product == uaa->product) {
169 1.2.4.2 bouyer return (UMATCH_VENDOR_PRODUCT);
170 1.2.4.2 bouyer }
171 1.2.4.2 bouyer }
172 1.2.4.2 bouyer return (UMATCH_NONE);
173 1.2.4.2 bouyer }
174 1.2.4.2 bouyer
175 1.2.4.2 bouyer USB_ATTACH(umct)
176 1.2.4.2 bouyer {
177 1.2.4.2 bouyer USB_ATTACH_START(umct, sc, uaa);
178 1.2.4.2 bouyer usbd_device_handle dev = uaa->device;
179 1.2.4.2 bouyer usb_config_descriptor_t *cdesc;
180 1.2.4.2 bouyer usb_interface_descriptor_t *id;
181 1.2.4.2 bouyer usb_endpoint_descriptor_t *ed;
182 1.2.4.2 bouyer
183 1.2.4.2 bouyer char devinfo[1024];
184 1.2.4.2 bouyer char *devname = USBDEVNAME(sc->sc_dev);
185 1.2.4.2 bouyer usbd_status err;
186 1.2.4.2 bouyer int i,found;
187 1.2.4.2 bouyer struct ucom_attach_args uca;
188 1.2.4.2 bouyer
189 1.2.4.2 bouyer usbd_devinfo(dev, 0, devinfo);
190 1.2.4.2 bouyer USB_ATTACH_SETUP;
191 1.2.4.2 bouyer printf("%s: %s\n", devname, devinfo);
192 1.2.4.2 bouyer
193 1.2.4.2 bouyer sc->sc_udev = dev;
194 1.2.4.2 bouyer
195 1.2.4.2 bouyer DPRINTF(("\n\numct attach: sc=%p\n", sc));
196 1.2.4.2 bouyer
197 1.2.4.2 bouyer /* initialize endpoints */
198 1.2.4.2 bouyer uca.bulkin = uca.bulkout = -1;
199 1.2.4.2 bouyer sc->sc_intr_number = -1;
200 1.2.4.2 bouyer sc->sc_intr_pipe = NULL;
201 1.2.4.2 bouyer
202 1.2.4.2 bouyer /* Move the device into the configured state. */
203 1.2.4.2 bouyer err = usbd_set_config_index(dev, UMCT_CONFIG_INDEX, 1);
204 1.2.4.2 bouyer if (err) {
205 1.2.4.2 bouyer printf("\n%s: failed to set configuration, err=%s\n",
206 1.2.4.2 bouyer devname, usbd_errstr(err));
207 1.2.4.2 bouyer sc->sc_dying = 1;
208 1.2.4.2 bouyer USB_ATTACH_ERROR_RETURN;
209 1.2.4.2 bouyer }
210 1.2.4.2 bouyer
211 1.2.4.2 bouyer /* get the config descriptor */
212 1.2.4.2 bouyer cdesc = usbd_get_config_descriptor(sc->sc_udev);
213 1.2.4.2 bouyer
214 1.2.4.2 bouyer if (cdesc == NULL) {
215 1.2.4.2 bouyer printf("%s: failed to get configuration descriptor\n",
216 1.2.4.2 bouyer USBDEVNAME(sc->sc_dev));
217 1.2.4.2 bouyer sc->sc_dying = 1;
218 1.2.4.2 bouyer USB_ATTACH_ERROR_RETURN;
219 1.2.4.2 bouyer }
220 1.2.4.2 bouyer
221 1.2.4.2 bouyer /* get the interface */
222 1.2.4.2 bouyer err = usbd_device2interface_handle(dev, UMCT_IFACE_INDEX,
223 1.2.4.2 bouyer &sc->sc_iface);
224 1.2.4.2 bouyer if (err) {
225 1.2.4.2 bouyer printf("\n%s: failed to get interface, err=%s\n",
226 1.2.4.2 bouyer devname, usbd_errstr(err));
227 1.2.4.2 bouyer sc->sc_dying = 1;
228 1.2.4.2 bouyer USB_ATTACH_ERROR_RETURN;
229 1.2.4.2 bouyer }
230 1.2.4.2 bouyer
231 1.2.4.2 bouyer /* Find the bulk{in,out} and interrupt endpoints */
232 1.2.4.2 bouyer
233 1.2.4.2 bouyer id = usbd_get_interface_descriptor(sc->sc_iface);
234 1.2.4.2 bouyer sc->sc_iface_number = id->bInterfaceNumber;
235 1.2.4.2 bouyer found = 0;
236 1.2.4.2 bouyer
237 1.2.4.2 bouyer for (i = 0; i < id->bNumEndpoints; i++) {
238 1.2.4.2 bouyer ed = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
239 1.2.4.2 bouyer if (ed == NULL) {
240 1.2.4.2 bouyer printf("%s: no endpoint descriptor for %d\n",
241 1.2.4.2 bouyer USBDEVNAME(sc->sc_dev), i);
242 1.2.4.2 bouyer sc->sc_dying = 1;
243 1.2.4.2 bouyer USB_ATTACH_ERROR_RETURN;
244 1.2.4.2 bouyer }
245 1.2.4.2 bouyer
246 1.2.4.2 bouyer if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
247 1.2.4.2 bouyer UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT &&
248 1.2.4.2 bouyer found == 0) {
249 1.2.4.2 bouyer uca.bulkin = ed->bEndpointAddress;
250 1.2.4.2 bouyer found = 1;
251 1.2.4.2 bouyer } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
252 1.2.4.2 bouyer UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
253 1.2.4.2 bouyer uca.bulkout = ed->bEndpointAddress;
254 1.2.4.2 bouyer } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
255 1.2.4.2 bouyer UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
256 1.2.4.2 bouyer sc->sc_intr_number = ed->bEndpointAddress;
257 1.2.4.2 bouyer sc->sc_isize = UGETW(ed->wMaxPacketSize);
258 1.2.4.2 bouyer }
259 1.2.4.2 bouyer }
260 1.2.4.2 bouyer
261 1.2.4.2 bouyer if (uca.bulkin == -1) {
262 1.2.4.2 bouyer printf("%s: Could not find data bulk in\n",
263 1.2.4.2 bouyer USBDEVNAME(sc->sc_dev));
264 1.2.4.2 bouyer sc->sc_dying = 1;
265 1.2.4.2 bouyer USB_ATTACH_ERROR_RETURN;
266 1.2.4.2 bouyer }
267 1.2.4.2 bouyer
268 1.2.4.2 bouyer if (uca.bulkout == -1) {
269 1.2.4.2 bouyer printf("%s: Could not find data bulk out\n",
270 1.2.4.2 bouyer USBDEVNAME(sc->sc_dev));
271 1.2.4.2 bouyer sc->sc_dying = 1;
272 1.2.4.2 bouyer USB_ATTACH_ERROR_RETURN;
273 1.2.4.2 bouyer }
274 1.2.4.2 bouyer
275 1.2.4.2 bouyer if (sc->sc_intr_number== -1) {
276 1.2.4.2 bouyer printf("%s: Could not find interrupt in\n",
277 1.2.4.2 bouyer USBDEVNAME(sc->sc_dev));
278 1.2.4.2 bouyer sc->sc_dying = 1;
279 1.2.4.2 bouyer USB_ATTACH_ERROR_RETURN;
280 1.2.4.2 bouyer }
281 1.2.4.2 bouyer
282 1.2.4.2 bouyer sc->sc_dtr = sc->sc_rts = 0;
283 1.2.4.2 bouyer uca.portno = UCOM_UNK_PORTNO;
284 1.2.4.2 bouyer /* bulkin, bulkout set above */
285 1.2.4.2 bouyer uca.ibufsize = UMCTIBUFSIZE;
286 1.2.4.2 bouyer uca.obufsize = UMCTOBUFSIZE;
287 1.2.4.2 bouyer uca.ibufsizepad = UMCTIBUFSIZE;
288 1.2.4.2 bouyer uca.opkthdrlen = 0;
289 1.2.4.2 bouyer uca.device = dev;
290 1.2.4.2 bouyer uca.iface = sc->sc_iface;
291 1.2.4.2 bouyer uca.methods = &umct_methods;
292 1.2.4.2 bouyer uca.arg = sc;
293 1.2.4.2 bouyer uca.info = NULL;
294 1.2.4.2 bouyer
295 1.2.4.2 bouyer umct_init(sc);
296 1.2.4.2 bouyer
297 1.2.4.2 bouyer usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
298 1.2.4.2 bouyer USBDEV(sc->sc_dev));
299 1.2.4.2 bouyer
300 1.2.4.2 bouyer DPRINTF(("umct: in=0x%x out=0x%x intr=0x%x\n",
301 1.2.4.2 bouyer uca.bulkin, uca.bulkout, sc->sc_intr_number ));
302 1.2.4.2 bouyer sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch);
303 1.2.4.2 bouyer
304 1.2.4.2 bouyer USB_ATTACH_SUCCESS_RETURN;
305 1.2.4.2 bouyer }
306 1.2.4.2 bouyer
307 1.2.4.2 bouyer USB_DETACH(umct)
308 1.2.4.2 bouyer {
309 1.2.4.2 bouyer USB_DETACH_START(umct, sc);
310 1.2.4.2 bouyer int rv = 0;
311 1.2.4.2 bouyer
312 1.2.4.2 bouyer DPRINTF(("umct_detach: sc=%p flags=%d\n", sc, flags));
313 1.2.4.2 bouyer
314 1.2.4.2 bouyer if (sc->sc_intr_pipe != NULL) {
315 1.2.4.2 bouyer usbd_abort_pipe(sc->sc_intr_pipe);
316 1.2.4.2 bouyer usbd_close_pipe(sc->sc_intr_pipe);
317 1.2.4.2 bouyer free(sc->sc_intr_buf, M_USBDEV);
318 1.2.4.2 bouyer sc->sc_intr_pipe = NULL;
319 1.2.4.2 bouyer }
320 1.2.4.2 bouyer
321 1.2.4.2 bouyer sc->sc_dying = 1;
322 1.2.4.2 bouyer if (sc->sc_subdev != NULL) {
323 1.2.4.2 bouyer rv = config_detach(sc->sc_subdev, flags);
324 1.2.4.2 bouyer sc->sc_subdev = NULL;
325 1.2.4.2 bouyer }
326 1.2.4.2 bouyer
327 1.2.4.2 bouyer usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
328 1.2.4.2 bouyer USBDEV(sc->sc_dev));
329 1.2.4.2 bouyer
330 1.2.4.2 bouyer return (rv);
331 1.2.4.2 bouyer }
332 1.2.4.2 bouyer
333 1.2.4.2 bouyer int
334 1.2.4.2 bouyer umct_activate(device_ptr_t self, enum devact act)
335 1.2.4.2 bouyer {
336 1.2.4.2 bouyer struct umct_softc *sc = (struct umct_softc *)self;
337 1.2.4.2 bouyer int rv = 0;
338 1.2.4.2 bouyer
339 1.2.4.2 bouyer switch (act) {
340 1.2.4.2 bouyer case DVACT_ACTIVATE:
341 1.2.4.2 bouyer return (EOPNOTSUPP);
342 1.2.4.2 bouyer break;
343 1.2.4.2 bouyer
344 1.2.4.2 bouyer case DVACT_DEACTIVATE:
345 1.2.4.2 bouyer if (sc->sc_subdev != NULL)
346 1.2.4.2 bouyer rv = config_deactivate(sc->sc_subdev);
347 1.2.4.2 bouyer sc->sc_dying = 1;
348 1.2.4.2 bouyer break;
349 1.2.4.2 bouyer }
350 1.2.4.2 bouyer return (rv);
351 1.2.4.2 bouyer }
352 1.2.4.2 bouyer
353 1.2.4.2 bouyer void
354 1.2.4.2 bouyer umct_set_line_state(struct umct_softc *sc)
355 1.2.4.2 bouyer {
356 1.2.4.2 bouyer usb_device_request_t req;
357 1.2.4.2 bouyer int ls = MCR_NONE;
358 1.2.4.2 bouyer
359 1.2.4.2 bouyer ls = (sc->sc_dtr ? MCR_DTR : 0) |
360 1.2.4.2 bouyer (sc->sc_rts ? MCR_RTS : 0);
361 1.2.4.2 bouyer
362 1.2.4.2 bouyer DPRINTF(("umct_set_line_state: DTR=%d,RTS=%d,ls=%02x\n",
363 1.2.4.2 bouyer sc->sc_dtr, sc->sc_rts, ls));
364 1.2.4.2 bouyer
365 1.2.4.2 bouyer req.bmRequestType = UMCT_SET_REQUEST;
366 1.2.4.2 bouyer req.bRequest = REQ_SET_MCR;
367 1.2.4.2 bouyer USETW(req.wValue, 0);
368 1.2.4.2 bouyer USETW(req.wIndex, sc->sc_iface_number);
369 1.2.4.2 bouyer USETW(req.wLength, LENGTH_SET_MCR);
370 1.2.4.2 bouyer
371 1.2.4.2 bouyer (void)usbd_do_request(sc->sc_udev, &req, &ls);
372 1.2.4.2 bouyer }
373 1.2.4.2 bouyer
374 1.2.4.2 bouyer void
375 1.2.4.2 bouyer umct_set(void *addr, int portno, int reg, int onoff)
376 1.2.4.2 bouyer {
377 1.2.4.2 bouyer struct umct_softc *sc = addr;
378 1.2.4.2 bouyer
379 1.2.4.2 bouyer switch (reg) {
380 1.2.4.2 bouyer case UCOM_SET_DTR:
381 1.2.4.2 bouyer umct_dtr(sc, onoff);
382 1.2.4.2 bouyer break;
383 1.2.4.2 bouyer case UCOM_SET_RTS:
384 1.2.4.2 bouyer umct_rts(sc, onoff);
385 1.2.4.2 bouyer break;
386 1.2.4.2 bouyer case UCOM_SET_BREAK:
387 1.2.4.2 bouyer umct_break(sc, onoff);
388 1.2.4.2 bouyer break;
389 1.2.4.2 bouyer default:
390 1.2.4.2 bouyer break;
391 1.2.4.2 bouyer }
392 1.2.4.2 bouyer }
393 1.2.4.2 bouyer
394 1.2.4.2 bouyer void
395 1.2.4.2 bouyer umct_dtr(struct umct_softc *sc, int onoff)
396 1.2.4.2 bouyer {
397 1.2.4.2 bouyer
398 1.2.4.2 bouyer DPRINTF(("umct_dtr: onoff=%d\n", onoff));
399 1.2.4.2 bouyer
400 1.2.4.2 bouyer if (sc->sc_dtr == onoff)
401 1.2.4.2 bouyer return;
402 1.2.4.2 bouyer sc->sc_dtr = onoff;
403 1.2.4.2 bouyer
404 1.2.4.2 bouyer umct_set_line_state(sc);
405 1.2.4.2 bouyer }
406 1.2.4.2 bouyer
407 1.2.4.2 bouyer void
408 1.2.4.2 bouyer umct_rts(struct umct_softc *sc, int onoff)
409 1.2.4.2 bouyer {
410 1.2.4.2 bouyer DPRINTF(("umct_rts: onoff=%d\n", onoff));
411 1.2.4.2 bouyer
412 1.2.4.2 bouyer if (sc->sc_rts == onoff)
413 1.2.4.2 bouyer return;
414 1.2.4.2 bouyer sc->sc_rts = onoff;
415 1.2.4.2 bouyer
416 1.2.4.2 bouyer umct_set_line_state(sc);
417 1.2.4.2 bouyer }
418 1.2.4.2 bouyer
419 1.2.4.2 bouyer void
420 1.2.4.2 bouyer umct_break(struct umct_softc *sc, int onoff)
421 1.2.4.2 bouyer {
422 1.2.4.2 bouyer u_char data;
423 1.2.4.2 bouyer
424 1.2.4.2 bouyer DPRINTF(("umct_break: onoff=%d\n", onoff));
425 1.2.4.2 bouyer
426 1.2.4.2 bouyer data = onoff ? LCR_SET_BREAK : 0;
427 1.2.4.2 bouyer
428 1.2.4.2 bouyer umct_set_lcr(sc, &data);
429 1.2.4.2 bouyer }
430 1.2.4.2 bouyer
431 1.2.4.2 bouyer void
432 1.2.4.2 bouyer umct_set_lcr(struct umct_softc *sc, void *data)
433 1.2.4.2 bouyer {
434 1.2.4.2 bouyer usb_device_request_t req;
435 1.2.4.2 bouyer
436 1.2.4.2 bouyer req.bmRequestType = UMCT_SET_REQUEST;
437 1.2.4.2 bouyer req.bRequest = REQ_SET_LCR;
438 1.2.4.2 bouyer USETW(req.wValue, 0);
439 1.2.4.2 bouyer USETW(req.wIndex, sc->sc_iface_number);
440 1.2.4.2 bouyer USETW(req.wLength, LENGTH_SET_LCR);
441 1.2.4.2 bouyer
442 1.2.4.2 bouyer (void)usbd_do_request(sc->sc_udev, &req, data);
443 1.2.4.2 bouyer }
444 1.2.4.2 bouyer
445 1.2.4.2 bouyer void
446 1.2.4.2 bouyer umct_set_baudrate(struct umct_softc *sc, void *rate)
447 1.2.4.2 bouyer {
448 1.2.4.2 bouyer usb_device_request_t req;
449 1.2.4.2 bouyer
450 1.2.4.2 bouyer req.bmRequestType = UMCT_SET_REQUEST;
451 1.2.4.2 bouyer req.bRequest = REQ_SET_BAUD_RATE;
452 1.2.4.2 bouyer USETW(req.wValue, 0);
453 1.2.4.2 bouyer USETW(req.wIndex, sc->sc_iface_number);
454 1.2.4.2 bouyer USETW(req.wLength, LENGTH_BAUD_RATE);
455 1.2.4.2 bouyer
456 1.2.4.2 bouyer (void)usbd_do_request(sc->sc_udev, &req, rate);
457 1.2.4.2 bouyer }
458 1.2.4.2 bouyer
459 1.2.4.2 bouyer void
460 1.2.4.2 bouyer umct_init(struct umct_softc *sc)
461 1.2.4.2 bouyer {
462 1.2.4.2 bouyer int brate, data;
463 1.2.4.2 bouyer
464 1.2.4.2 bouyer brate = UMCT_BAUD_RATE(9600);
465 1.2.4.2 bouyer data |= LCR_DATA_BITS_8 | LCR_PARITY_NONE |
466 1.2.4.2 bouyer LCR_STOP_BITS_1;
467 1.2.4.2 bouyer
468 1.2.4.2 bouyer umct_set_baudrate(sc, &brate);
469 1.2.4.2 bouyer umct_set_lcr(sc, &data);
470 1.2.4.2 bouyer }
471 1.2.4.2 bouyer
472 1.2.4.2 bouyer int
473 1.2.4.2 bouyer umct_param(void *addr, int portno, struct termios *t)
474 1.2.4.2 bouyer {
475 1.2.4.2 bouyer struct umct_softc *sc = addr;
476 1.2.4.2 bouyer int divisor = 0;
477 1.2.4.2 bouyer u_char data = NULL;
478 1.2.4.2 bouyer
479 1.2.4.2 bouyer DPRINTF(("umct_param: sc=%p\n", sc));
480 1.2.4.2 bouyer
481 1.2.4.2 bouyer divisor = UMCT_BAUD_RATE(t->c_ospeed);
482 1.2.4.2 bouyer DPRINTF(("umct_param: BAUDRATE=%d\n", t->c_ospeed));
483 1.2.4.2 bouyer
484 1.2.4.2 bouyer if (ISSET(t->c_cflag, CSTOPB))
485 1.2.4.2 bouyer data |= LCR_STOP_BITS_2;
486 1.2.4.2 bouyer else
487 1.2.4.2 bouyer data |= LCR_STOP_BITS_1;
488 1.2.4.2 bouyer if (ISSET(t->c_cflag, PARENB)) {
489 1.2.4.2 bouyer if (ISSET(t->c_cflag, PARODD))
490 1.2.4.2 bouyer data |= LCR_PARITY_ODD;
491 1.2.4.2 bouyer else
492 1.2.4.2 bouyer data |= LCR_PARITY_EVEN;
493 1.2.4.2 bouyer } else
494 1.2.4.2 bouyer data |= LCR_PARITY_NONE;
495 1.2.4.2 bouyer switch (ISSET(t->c_cflag, CSIZE)) {
496 1.2.4.2 bouyer case CS5:
497 1.2.4.2 bouyer data |= LCR_DATA_BITS_5;
498 1.2.4.2 bouyer break;
499 1.2.4.2 bouyer case CS6:
500 1.2.4.2 bouyer data |= LCR_DATA_BITS_6;
501 1.2.4.2 bouyer break;
502 1.2.4.2 bouyer case CS7:
503 1.2.4.2 bouyer data |= LCR_DATA_BITS_7;
504 1.2.4.2 bouyer break;
505 1.2.4.2 bouyer case CS8:
506 1.2.4.2 bouyer data |= LCR_DATA_BITS_8;
507 1.2.4.2 bouyer break;
508 1.2.4.2 bouyer }
509 1.2.4.2 bouyer
510 1.2.4.2 bouyer umct_set_baudrate(sc, &divisor);
511 1.2.4.2 bouyer
512 1.2.4.2 bouyer umct_set_lcr(sc, &data);
513 1.2.4.2 bouyer
514 1.2.4.2 bouyer return (0);
515 1.2.4.2 bouyer }
516 1.2.4.2 bouyer
517 1.2.4.2 bouyer int
518 1.2.4.2 bouyer umct_open(void *addr, int portno)
519 1.2.4.2 bouyer {
520 1.2.4.2 bouyer struct umct_softc *sc = addr;
521 1.2.4.2 bouyer int err, lcr_data;
522 1.2.4.2 bouyer
523 1.2.4.2 bouyer if (sc->sc_dying)
524 1.2.4.2 bouyer return (EIO);
525 1.2.4.2 bouyer
526 1.2.4.2 bouyer DPRINTF(("umct_open: sc=%p\n", sc));
527 1.2.4.2 bouyer
528 1.2.4.2 bouyer /* initialize LCR */
529 1.2.4.2 bouyer lcr_data |= LCR_DATA_BITS_8 | LCR_PARITY_NONE |
530 1.2.4.2 bouyer LCR_STOP_BITS_1;
531 1.2.4.2 bouyer umct_set_lcr(sc, &lcr_data);
532 1.2.4.2 bouyer
533 1.2.4.2 bouyer if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
534 1.2.4.2 bouyer sc->sc_status = 0; /* clear status bit */
535 1.2.4.2 bouyer sc->sc_intr_buf = malloc(sc->sc_isize, M_USBDEV, M_WAITOK);
536 1.2.4.2 bouyer err = usbd_open_pipe_intr(sc->sc_iface, sc->sc_intr_number,
537 1.2.4.2 bouyer USBD_SHORT_XFER_OK, &sc->sc_intr_pipe, sc,
538 1.2.4.2 bouyer sc->sc_intr_buf, sc->sc_isize,
539 1.2.4.2 bouyer umct_intr, USBD_DEFAULT_INTERVAL);
540 1.2.4.2 bouyer if (err) {
541 1.2.4.2 bouyer DPRINTF(("%s: cannot open interrupt pipe (addr %d)\n",
542 1.2.4.2 bouyer USBDEVNAME(sc->sc_dev), sc->sc_intr_number));
543 1.2.4.2 bouyer return (EIO);
544 1.2.4.2 bouyer }
545 1.2.4.2 bouyer }
546 1.2.4.2 bouyer
547 1.2.4.2 bouyer return (0);
548 1.2.4.2 bouyer }
549 1.2.4.2 bouyer
550 1.2.4.2 bouyer void
551 1.2.4.2 bouyer umct_close(void *addr, int portno)
552 1.2.4.2 bouyer {
553 1.2.4.2 bouyer struct umct_softc *sc = addr;
554 1.2.4.2 bouyer int err;
555 1.2.4.2 bouyer
556 1.2.4.2 bouyer if (sc->sc_dying)
557 1.2.4.2 bouyer return;
558 1.2.4.2 bouyer
559 1.2.4.2 bouyer DPRINTF(("umct_close: close\n"));
560 1.2.4.2 bouyer
561 1.2.4.2 bouyer if (sc->sc_intr_pipe != NULL) {
562 1.2.4.2 bouyer err = usbd_abort_pipe(sc->sc_intr_pipe);
563 1.2.4.2 bouyer if (err)
564 1.2.4.2 bouyer printf("%s: abort interrupt pipe failed: %s\n",
565 1.2.4.2 bouyer USBDEVNAME(sc->sc_dev), usbd_errstr(err));
566 1.2.4.2 bouyer err = usbd_close_pipe(sc->sc_intr_pipe);
567 1.2.4.2 bouyer if (err)
568 1.2.4.2 bouyer printf("%s: close interrupt pipe failed: %s\n",
569 1.2.4.2 bouyer USBDEVNAME(sc->sc_dev), usbd_errstr(err));
570 1.2.4.2 bouyer free(sc->sc_intr_buf, M_USBDEV);
571 1.2.4.2 bouyer sc->sc_intr_pipe = NULL;
572 1.2.4.2 bouyer }
573 1.2.4.2 bouyer }
574 1.2.4.2 bouyer
575 1.2.4.2 bouyer void
576 1.2.4.2 bouyer umct_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
577 1.2.4.2 bouyer {
578 1.2.4.2 bouyer struct umct_softc *sc = priv;
579 1.2.4.2 bouyer u_char *buf = sc->sc_intr_buf;
580 1.2.4.2 bouyer u_char mstatus, lstatus;
581 1.2.4.2 bouyer
582 1.2.4.2 bouyer if (sc->sc_dying)
583 1.2.4.2 bouyer return;
584 1.2.4.2 bouyer
585 1.2.4.2 bouyer if (status != USBD_NORMAL_COMPLETION) {
586 1.2.4.2 bouyer if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
587 1.2.4.2 bouyer return;
588 1.2.4.2 bouyer
589 1.2.4.2 bouyer DPRINTF(("%s: abnormal status: %s\n", USBDEVNAME(sc->sc_dev),
590 1.2.4.2 bouyer usbd_errstr(status)));
591 1.2.4.2 bouyer usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
592 1.2.4.2 bouyer return;
593 1.2.4.2 bouyer }
594 1.2.4.2 bouyer
595 1.2.4.2 bouyer DPRINTF(("%s: umct status = MSR:%02x, LSR:%02x\n",
596 1.2.4.2 bouyer USBDEVNAME(sc->sc_dev), buf[0],buf[1]));
597 1.2.4.2 bouyer
598 1.2.4.2 bouyer sc->sc_lsr = sc->sc_msr = 0;
599 1.2.4.2 bouyer mstatus = buf[0];
600 1.2.4.2 bouyer lstatus = buf[1];
601 1.2.4.2 bouyer if (ISSET(mstatus, MSR_DSR))
602 1.2.4.2 bouyer sc->sc_msr |= UMSR_DSR;
603 1.2.4.2 bouyer if (ISSET(mstatus, MSR_DCD))
604 1.2.4.2 bouyer sc->sc_msr |= UMSR_DCD;
605 1.2.4.2 bouyer ucom_status_change((struct ucom_softc *)sc->sc_subdev);
606 1.2.4.2 bouyer }
607 1.2.4.2 bouyer
608 1.2.4.2 bouyer void
609 1.2.4.2 bouyer umct_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
610 1.2.4.2 bouyer {
611 1.2.4.2 bouyer struct umct_softc *sc = addr;
612 1.2.4.2 bouyer
613 1.2.4.2 bouyer DPRINTF(("umct_get_status:\n"));
614 1.2.4.2 bouyer
615 1.2.4.2 bouyer if (lsr != NULL)
616 1.2.4.2 bouyer *lsr = sc->sc_lsr;
617 1.2.4.2 bouyer if (msr != NULL)
618 1.2.4.2 bouyer *msr = sc->sc_msr;
619 1.2.4.2 bouyer }
620