uftdi.c revision 1.55 1 1.55 reinoud /* $NetBSD: uftdi.c,v 1.55 2013/01/20 13:43:24 reinoud Exp $ */
2 1.1 augustss
3 1.1 augustss /*
4 1.1 augustss * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 1.1 augustss * All rights reserved.
6 1.1 augustss *
7 1.1 augustss * This code is derived from software contributed to The NetBSD Foundation
8 1.1 augustss * by Lennart Augustsson (lennart (at) augustsson.net).
9 1.1 augustss *
10 1.1 augustss * Redistribution and use in source and binary forms, with or without
11 1.1 augustss * modification, are permitted provided that the following conditions
12 1.1 augustss * are met:
13 1.1 augustss * 1. Redistributions of source code must retain the above copyright
14 1.1 augustss * notice, this list of conditions and the following disclaimer.
15 1.1 augustss * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 augustss * notice, this list of conditions and the following disclaimer in the
17 1.1 augustss * documentation and/or other materials provided with the distribution.
18 1.1 augustss *
19 1.1 augustss * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 augustss * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 augustss * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 augustss * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 augustss * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 augustss * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 augustss * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 augustss * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 augustss * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 augustss * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 augustss * POSSIBILITY OF SUCH DAMAGE.
30 1.1 augustss */
31 1.1 augustss
32 1.7 lukem #include <sys/cdefs.h>
33 1.55 reinoud __KERNEL_RCSID(0, "$NetBSD: uftdi.c,v 1.55 2013/01/20 13:43:24 reinoud Exp $");
34 1.1 augustss
35 1.1 augustss #include <sys/param.h>
36 1.1 augustss #include <sys/systm.h>
37 1.1 augustss #include <sys/kernel.h>
38 1.1 augustss #include <sys/device.h>
39 1.1 augustss #include <sys/conf.h>
40 1.1 augustss #include <sys/tty.h>
41 1.1 augustss
42 1.1 augustss #include <dev/usb/usb.h>
43 1.1 augustss
44 1.1 augustss #include <dev/usb/usbdi.h>
45 1.1 augustss #include <dev/usb/usbdi_util.h>
46 1.1 augustss #include <dev/usb/usbdevs.h>
47 1.1 augustss
48 1.1 augustss #include <dev/usb/ucomvar.h>
49 1.1 augustss
50 1.1 augustss #include <dev/usb/uftdireg.h>
51 1.1 augustss
52 1.1 augustss #ifdef UFTDI_DEBUG
53 1.1 augustss #define DPRINTF(x) if (uftdidebug) printf x
54 1.1 augustss #define DPRINTFN(n,x) if (uftdidebug>(n)) printf x
55 1.4 augustss int uftdidebug = 0;
56 1.1 augustss #else
57 1.1 augustss #define DPRINTF(x)
58 1.1 augustss #define DPRINTFN(n,x)
59 1.1 augustss #endif
60 1.1 augustss
61 1.1 augustss #define UFTDI_CONFIG_INDEX 0
62 1.1 augustss #define UFTDI_IFACE_INDEX 0
63 1.41 nisimura #define UFTDI_MAX_PORTS 4
64 1.1 augustss
65 1.1 augustss /*
66 1.53 riastrad * These are the default number of bytes transferred per frame if the
67 1.53 riastrad * endpoint doesn't tell us. The output buffer size is a hard limit
68 1.53 riastrad * for devices that use a 6-bit size encoding.
69 1.1 augustss */
70 1.1 augustss #define UFTDIIBUFSIZE 64
71 1.1 augustss #define UFTDIOBUFSIZE 64
72 1.1 augustss
73 1.53 riastrad /*
74 1.53 riastrad * Magic constants! Where do these come from? They're what Linux uses...
75 1.53 riastrad */
76 1.53 riastrad #define UFTDI_MAX_IBUFSIZE 512
77 1.53 riastrad #define UFTDI_MAX_OBUFSIZE 256
78 1.53 riastrad
79 1.1 augustss struct uftdi_softc {
80 1.47 dyoung device_t sc_dev; /* base device */
81 1.1 augustss usbd_device_handle sc_udev; /* device */
82 1.32 riz usbd_interface_handle sc_iface[UFTDI_MAX_PORTS]; /* interface */
83 1.1 augustss
84 1.10 scw enum uftdi_type sc_type;
85 1.10 scw u_int sc_hdrlen;
86 1.32 riz u_int sc_numports;
87 1.41 nisimura u_int sc_chiptype;
88 1.10 scw
89 1.1 augustss u_char sc_msr;
90 1.1 augustss u_char sc_lsr;
91 1.1 augustss
92 1.47 dyoung device_t sc_subdev[UFTDI_MAX_PORTS];
93 1.1 augustss
94 1.1 augustss u_char sc_dying;
95 1.8 ichiro
96 1.8 ichiro u_int last_lcr;
97 1.41 nisimura
98 1.1 augustss };
99 1.1 augustss
100 1.2 augustss Static void uftdi_get_status(void *, int portno, u_char *lsr, u_char *msr);
101 1.2 augustss Static void uftdi_set(void *, int, int, int);
102 1.2 augustss Static int uftdi_param(void *, int, struct termios *);
103 1.2 augustss Static int uftdi_open(void *sc, int portno);
104 1.2 augustss Static void uftdi_read(void *sc, int portno, u_char **ptr,u_int32_t *count);
105 1.2 augustss Static void uftdi_write(void *sc, int portno, u_char *to, u_char *from,
106 1.2 augustss u_int32_t *count);
107 1.8 ichiro Static void uftdi_break(void *sc, int portno, int onoff);
108 1.1 augustss
109 1.1 augustss struct ucom_methods uftdi_methods = {
110 1.1 augustss uftdi_get_status,
111 1.1 augustss uftdi_set,
112 1.1 augustss uftdi_param,
113 1.1 augustss NULL,
114 1.1 augustss uftdi_open,
115 1.1 augustss NULL,
116 1.1 augustss uftdi_read,
117 1.1 augustss uftdi_write,
118 1.1 augustss };
119 1.1 augustss
120 1.50 jakllsch /*
121 1.26 christos * The devices default to UFTDI_TYPE_8U232AM.
122 1.47 dyoung * Remember to update uftdi_attach() if it should be UFTDI_TYPE_SIO instead
123 1.26 christos */
124 1.26 christos static const struct usb_devno uftdi_devs[] = {
125 1.26 christos { USB_VENDOR_BBELECTRONICS, USB_PRODUCT_BBELECTRONICS_USOTL4 },
126 1.26 christos { USB_VENDOR_FALCOM, USB_PRODUCT_FALCOM_TWIST },
127 1.26 christos { USB_VENDOR_FALCOM, USB_PRODUCT_FALCOM_SAMBA },
128 1.55 reinoud { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_232H },
129 1.30 riz { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_2232C },
130 1.42 nisimura { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_4232H },
131 1.26 christos { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_8U100AX },
132 1.26 christos { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SERIAL_8U232AM },
133 1.26 christos { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_KW },
134 1.26 christos { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_YS },
135 1.26 christos { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_Y6 },
136 1.26 christos { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_Y8 },
137 1.26 christos { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_IC },
138 1.26 christos { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_DB9 },
139 1.26 christos { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_RS232 },
140 1.26 christos { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MHAM_Y9 },
141 1.26 christos { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_COASTAL_TNCX },
142 1.45 martin { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_CTI_485_MINI },
143 1.45 martin { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_CTI_NANO_485 },
144 1.26 christos { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_SEMC_DSS20 },
145 1.26 christos { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_LK202_24_USB },
146 1.26 christos { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_LK204_24_USB },
147 1.26 christos { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_MX200_USB },
148 1.27 xtraeme { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_MX4_MX5_USB },
149 1.26 christos { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_631 },
150 1.26 christos { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_632 },
151 1.26 christos { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_633 },
152 1.26 christos { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_634 },
153 1.27 xtraeme { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_LCD_CFA_635 },
154 1.52 matt { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_OPENRD_JTAGKEY },
155 1.54 jakllsch { USB_VENDOR_FTDI, USB_PRODUCT_FTDI_MAXSTREAM_PKG_U },
156 1.48 matt { USB_VENDOR_xxFTDI, USB_PRODUCT_xxFTDI_SHEEVAPLUG_JTAG },
157 1.26 christos { USB_VENDOR_INTREPIDCS, USB_PRODUCT_INTREPIDCS_VALUECAN },
158 1.26 christos { USB_VENDOR_INTREPIDCS, USB_PRODUCT_INTREPIDCS_NEOVI },
159 1.40 taca { USB_VENDOR_RATOC, USB_PRODUCT_RATOC_REXUSB60F },
160 1.26 christos { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_USBSERIAL },
161 1.39 rmind { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_SEAPORT4P1 },
162 1.39 rmind { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_SEAPORT4P2 },
163 1.39 rmind { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_SEAPORT4P3 },
164 1.39 rmind { USB_VENDOR_SEALEVEL, USB_PRODUCT_SEALEVEL_SEAPORT4P4 },
165 1.26 christos { USB_VENDOR_SIIG2, USB_PRODUCT_SIIG2_US2308 },
166 1.51 plunky { USB_VENDOR_MISC, USB_PRODUCT_MISC_TELLSTICK },
167 1.51 plunky { USB_VENDOR_MISC, USB_PRODUCT_MISC_TELLSTICK_DUO },
168 1.26 christos };
169 1.26 christos #define uftdi_lookup(v, p) usb_lookup(uftdi_devs, v, p)
170 1.26 christos
171 1.38 cube int uftdi_match(device_t, cfdata_t, void *);
172 1.35 dyoung void uftdi_attach(device_t, device_t, void *);
173 1.35 dyoung void uftdi_childdet(device_t, device_t);
174 1.35 dyoung int uftdi_detach(device_t, int);
175 1.35 dyoung int uftdi_activate(device_t, enum devact);
176 1.35 dyoung extern struct cfdriver uftdi_cd;
177 1.38 cube CFATTACH_DECL2_NEW(uftdi, sizeof(struct uftdi_softc), uftdi_match,
178 1.35 dyoung uftdi_attach, uftdi_detach, uftdi_activate, NULL, uftdi_childdet);
179 1.1 augustss
180 1.50 jakllsch int
181 1.47 dyoung uftdi_match(device_t parent, cfdata_t match, void *aux)
182 1.1 augustss {
183 1.47 dyoung struct usb_attach_arg *uaa = aux;
184 1.11 augustss
185 1.1 augustss DPRINTFN(20,("uftdi: vendor=0x%x, product=0x%x\n",
186 1.1 augustss uaa->vendor, uaa->product));
187 1.1 augustss
188 1.26 christos return (uftdi_lookup(uaa->vendor, uaa->product) != NULL ?
189 1.26 christos UMATCH_VENDOR_PRODUCT : UMATCH_NONE);
190 1.1 augustss }
191 1.1 augustss
192 1.50 jakllsch void
193 1.47 dyoung uftdi_attach(device_t parent, device_t self, void *aux)
194 1.1 augustss {
195 1.47 dyoung struct uftdi_softc *sc = device_private(self);
196 1.47 dyoung struct usb_attach_arg *uaa = aux;
197 1.1 augustss usbd_device_handle dev = uaa->device;
198 1.1 augustss usbd_interface_handle iface;
199 1.41 nisimura usb_device_descriptor_t *ddesc;
200 1.1 augustss usb_interface_descriptor_t *id;
201 1.1 augustss usb_endpoint_descriptor_t *ed;
202 1.24 augustss char *devinfop;
203 1.38 cube const char *devname = device_xname(self);
204 1.32 riz int i,idx;
205 1.1 augustss usbd_status err;
206 1.1 augustss struct ucom_attach_args uca;
207 1.1 augustss
208 1.1 augustss DPRINTFN(10,("\nuftdi_attach: sc=%p\n", sc));
209 1.1 augustss
210 1.43 plunky aprint_naive("\n");
211 1.43 plunky aprint_normal("\n");
212 1.43 plunky
213 1.43 plunky devinfop = usbd_devinfo_alloc(dev, 0);
214 1.43 plunky aprint_normal_dev(self, "%s\n", devinfop);
215 1.43 plunky usbd_devinfo_free(devinfop);
216 1.43 plunky
217 1.1 augustss /* Move the device into the configured state. */
218 1.1 augustss err = usbd_set_config_index(dev, UFTDI_CONFIG_INDEX, 1);
219 1.1 augustss if (err) {
220 1.38 cube aprint_error("\n%s: failed to set configuration, err=%s\n",
221 1.1 augustss devname, usbd_errstr(err));
222 1.1 augustss goto bad;
223 1.1 augustss }
224 1.1 augustss
225 1.38 cube sc->sc_dev = self;
226 1.1 augustss sc->sc_udev = dev;
227 1.32 riz sc->sc_numports = 1;
228 1.41 nisimura sc->sc_type = UFTDI_TYPE_8U232AM; /* most devices are post-8U232AM */
229 1.41 nisimura sc->sc_hdrlen = 0;
230 1.41 nisimura if (uaa->vendor == USB_VENDOR_FTDI
231 1.41 nisimura && uaa->product == USB_PRODUCT_FTDI_SERIAL_8U100AX) {
232 1.41 nisimura sc->sc_type = UFTDI_TYPE_SIO;
233 1.41 nisimura sc->sc_hdrlen = 1;
234 1.41 nisimura }
235 1.41 nisimura
236 1.41 nisimura ddesc = usbd_get_device_descriptor(dev);
237 1.41 nisimura sc->sc_chiptype = UGETW(ddesc->bcdDevice);
238 1.41 nisimura switch (sc->sc_chiptype) {
239 1.41 nisimura case 0x500: /* 2232D */
240 1.41 nisimura case 0x700: /* 2232H */
241 1.41 nisimura sc->sc_numports = 2;
242 1.41 nisimura break;
243 1.41 nisimura case 0x800: /* 4232H */
244 1.41 nisimura sc->sc_numports = 4;
245 1.41 nisimura break;
246 1.41 nisimura case 0x200: /* 232/245AM */
247 1.41 nisimura case 0x400: /* 232/245BL */
248 1.41 nisimura case 0x600: /* 232/245R */
249 1.41 nisimura default:
250 1.10 scw break;
251 1.10 scw }
252 1.10 scw
253 1.32 riz for (idx = UFTDI_IFACE_INDEX; idx < sc->sc_numports; idx++) {
254 1.32 riz err = usbd_device2interface_handle(dev, idx, &iface);
255 1.32 riz if (err) {
256 1.38 cube aprint_error(
257 1.38 cube "\n%s: failed to get interface idx=%d, err=%s\n",
258 1.38 cube devname, idx, usbd_errstr(err));
259 1.1 augustss goto bad;
260 1.1 augustss }
261 1.11 augustss
262 1.32 riz id = usbd_get_interface_descriptor(iface);
263 1.32 riz
264 1.32 riz sc->sc_iface[idx] = iface;
265 1.32 riz
266 1.32 riz uca.bulkin = uca.bulkout = -1;
267 1.53 riastrad uca.ibufsize = uca.obufsize = 0;
268 1.32 riz for (i = 0; i < id->bNumEndpoints; i++) {
269 1.32 riz int addr, dir, attr;
270 1.32 riz ed = usbd_interface2endpoint_descriptor(iface, i);
271 1.32 riz if (ed == NULL) {
272 1.38 cube aprint_error_dev(self,
273 1.38 cube "could not read endpoint descriptor: %s\n",
274 1.38 cube usbd_errstr(err));
275 1.32 riz goto bad;
276 1.32 riz }
277 1.32 riz
278 1.32 riz addr = ed->bEndpointAddress;
279 1.32 riz dir = UE_GET_DIR(ed->bEndpointAddress);
280 1.32 riz attr = ed->bmAttributes & UE_XFERTYPE;
281 1.53 riastrad if (dir == UE_DIR_IN && attr == UE_BULK) {
282 1.32 riz uca.bulkin = addr;
283 1.53 riastrad uca.ibufsize = UGETW(ed->wMaxPacketSize);
284 1.53 riastrad if (uca.ibufsize >= UFTDI_MAX_IBUFSIZE)
285 1.53 riastrad uca.ibufsize = UFTDI_MAX_IBUFSIZE;
286 1.53 riastrad } else if (dir == UE_DIR_OUT && attr == UE_BULK) {
287 1.32 riz uca.bulkout = addr;
288 1.53 riastrad uca.obufsize = UGETW(ed->wMaxPacketSize)
289 1.53 riastrad - sc->sc_hdrlen;
290 1.53 riastrad if (uca.obufsize >= UFTDI_MAX_OBUFSIZE)
291 1.53 riastrad uca.obufsize = UFTDI_MAX_OBUFSIZE;
292 1.53 riastrad /* Limit length if we have a 6-bit header. */
293 1.53 riastrad if ((sc->sc_hdrlen > 0) &&
294 1.53 riastrad (uca.obufsize > UFTDIOBUFSIZE))
295 1.53 riastrad uca.obufsize = UFTDIOBUFSIZE;
296 1.53 riastrad } else {
297 1.38 cube aprint_error_dev(self,
298 1.38 cube "unexpected endpoint\n");
299 1.32 riz goto bad;
300 1.32 riz }
301 1.32 riz }
302 1.32 riz if (uca.bulkin == -1) {
303 1.38 cube aprint_error_dev(self,
304 1.38 cube "Could not find data bulk in\n");
305 1.1 augustss goto bad;
306 1.1 augustss }
307 1.32 riz if (uca.bulkout == -1) {
308 1.38 cube aprint_error_dev(self,
309 1.38 cube "Could not find data bulk out\n");
310 1.32 riz goto bad;
311 1.32 riz }
312 1.32 riz
313 1.32 riz uca.portno = FTDI_PIT_SIOA + idx;
314 1.32 riz /* bulkin, bulkout set above */
315 1.53 riastrad if (uca.ibufsize == 0)
316 1.53 riastrad uca.ibufsize = UFTDIIBUFSIZE;
317 1.53 riastrad uca.ibufsizepad = uca.ibufsize;
318 1.53 riastrad if (uca.obufsize == 0)
319 1.53 riastrad uca.obufsize = UFTDIOBUFSIZE - sc->sc_hdrlen;
320 1.32 riz uca.opkthdrlen = sc->sc_hdrlen;
321 1.32 riz uca.device = dev;
322 1.32 riz uca.iface = iface;
323 1.32 riz uca.methods = &uftdi_methods;
324 1.32 riz uca.arg = sc;
325 1.32 riz uca.info = NULL;
326 1.32 riz
327 1.53 riastrad DPRINTF(("uftdi: in=0x%x out=0x%x isize=0x%x osize=0x%x\n",
328 1.53 riastrad uca.bulkin, uca.bulkout,
329 1.53 riastrad uca.ibufsize, uca.obufsize));
330 1.41 nisimura sc->sc_subdev[idx] = config_found_sm_loc(self, "ucombus", NULL,
331 1.41 nisimura &uca, ucomprint, ucomsubmatch);
332 1.1 augustss }
333 1.1 augustss
334 1.5 augustss usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
335 1.47 dyoung sc->sc_dev);
336 1.5 augustss
337 1.47 dyoung return;
338 1.1 augustss
339 1.1 augustss bad:
340 1.1 augustss DPRINTF(("uftdi_attach: ATTACH ERROR\n"));
341 1.1 augustss sc->sc_dying = 1;
342 1.47 dyoung return;
343 1.1 augustss }
344 1.1 augustss
345 1.1 augustss int
346 1.38 cube uftdi_activate(device_t self, enum devact act)
347 1.1 augustss {
348 1.38 cube struct uftdi_softc *sc = device_private(self);
349 1.1 augustss
350 1.1 augustss switch (act) {
351 1.1 augustss case DVACT_DEACTIVATE:
352 1.1 augustss sc->sc_dying = 1;
353 1.44 dyoung return 0;
354 1.44 dyoung default:
355 1.44 dyoung return EOPNOTSUPP;
356 1.1 augustss }
357 1.1 augustss }
358 1.1 augustss
359 1.35 dyoung void
360 1.35 dyoung uftdi_childdet(device_t self, device_t child)
361 1.35 dyoung {
362 1.35 dyoung int i;
363 1.35 dyoung struct uftdi_softc *sc = device_private(self);
364 1.35 dyoung
365 1.35 dyoung for (i = 0; i < sc->sc_numports; i++) {
366 1.35 dyoung if (sc->sc_subdev[i] == child)
367 1.35 dyoung break;
368 1.35 dyoung }
369 1.35 dyoung KASSERT(i < sc->sc_numports);
370 1.35 dyoung sc->sc_subdev[i] = NULL;
371 1.35 dyoung }
372 1.35 dyoung
373 1.1 augustss int
374 1.35 dyoung uftdi_detach(device_t self, int flags)
375 1.1 augustss {
376 1.35 dyoung struct uftdi_softc *sc = device_private(self);
377 1.32 riz int i;
378 1.1 augustss
379 1.1 augustss DPRINTF(("uftdi_detach: sc=%p flags=%d\n", sc, flags));
380 1.1 augustss sc->sc_dying = 1;
381 1.35 dyoung for (i=0; i < sc->sc_numports; i++) {
382 1.35 dyoung if (sc->sc_subdev[i] != NULL)
383 1.32 riz config_detach(sc->sc_subdev[i], flags);
384 1.35 dyoung }
385 1.5 augustss
386 1.5 augustss usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
387 1.47 dyoung sc->sc_dev);
388 1.5 augustss
389 1.1 augustss return (0);
390 1.1 augustss }
391 1.1 augustss
392 1.1 augustss Static int
393 1.2 augustss uftdi_open(void *vsc, int portno)
394 1.1 augustss {
395 1.1 augustss struct uftdi_softc *sc = vsc;
396 1.1 augustss usb_device_request_t req;
397 1.1 augustss usbd_status err;
398 1.1 augustss struct termios t;
399 1.1 augustss
400 1.1 augustss DPRINTF(("uftdi_open: sc=%p\n", sc));
401 1.1 augustss
402 1.1 augustss if (sc->sc_dying)
403 1.1 augustss return (EIO);
404 1.1 augustss
405 1.1 augustss /* Perform a full reset on the device */
406 1.1 augustss req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
407 1.1 augustss req.bRequest = FTDI_SIO_RESET;
408 1.1 augustss USETW(req.wValue, FTDI_SIO_RESET_SIO);
409 1.1 augustss USETW(req.wIndex, portno);
410 1.1 augustss USETW(req.wLength, 0);
411 1.1 augustss err = usbd_do_request(sc->sc_udev, &req, NULL);
412 1.1 augustss if (err)
413 1.1 augustss return (EIO);
414 1.1 augustss
415 1.1 augustss /* Set 9600 baud, 2 stop bits, no parity, 8 bits */
416 1.1 augustss t.c_ospeed = 9600;
417 1.1 augustss t.c_cflag = CSTOPB | CS8;
418 1.1 augustss (void)uftdi_param(sc, portno, &t);
419 1.1 augustss
420 1.1 augustss /* Turn on RTS/CTS flow control */
421 1.1 augustss req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
422 1.1 augustss req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
423 1.1 augustss USETW(req.wValue, 0);
424 1.1 augustss USETW2(req.wIndex, FTDI_SIO_RTS_CTS_HS, portno);
425 1.1 augustss USETW(req.wLength, 0);
426 1.1 augustss err = usbd_do_request(sc->sc_udev, &req, NULL);
427 1.1 augustss if (err)
428 1.1 augustss return (EIO);
429 1.1 augustss
430 1.1 augustss return (0);
431 1.1 augustss }
432 1.1 augustss
433 1.1 augustss Static void
434 1.29 christos uftdi_read(void *vsc, int portno, u_char **ptr, u_int32_t *count)
435 1.1 augustss {
436 1.1 augustss struct uftdi_softc *sc = vsc;
437 1.1 augustss u_char msr, lsr;
438 1.1 augustss
439 1.1 augustss DPRINTFN(15,("uftdi_read: sc=%p, port=%d count=%d\n", sc, portno,
440 1.1 augustss *count));
441 1.1 augustss
442 1.1 augustss msr = FTDI_GET_MSR(*ptr);
443 1.1 augustss lsr = FTDI_GET_LSR(*ptr);
444 1.1 augustss
445 1.1 augustss #ifdef UFTDI_DEBUG
446 1.1 augustss if (*count != 2)
447 1.1 augustss DPRINTFN(10,("uftdi_read: sc=%p, port=%d count=%d data[0]="
448 1.1 augustss "0x%02x\n", sc, portno, *count, (*ptr)[2]));
449 1.1 augustss #endif
450 1.1 augustss
451 1.1 augustss if (sc->sc_msr != msr ||
452 1.1 augustss (sc->sc_lsr & FTDI_LSR_MASK) != (lsr & FTDI_LSR_MASK)) {
453 1.1 augustss DPRINTF(("uftdi_read: status change msr=0x%02x(0x%02x) "
454 1.1 augustss "lsr=0x%02x(0x%02x)\n", msr, sc->sc_msr,
455 1.1 augustss lsr, sc->sc_lsr));
456 1.1 augustss sc->sc_msr = msr;
457 1.1 augustss sc->sc_lsr = lsr;
458 1.38 cube ucom_status_change(device_private(sc->sc_subdev[portno-1]));
459 1.1 augustss }
460 1.1 augustss
461 1.46 scw /* Adjust buffer pointer to skip status prefix */
462 1.1 augustss *ptr += 2;
463 1.1 augustss }
464 1.1 augustss
465 1.1 augustss Static void
466 1.2 augustss uftdi_write(void *vsc, int portno, u_char *to, u_char *from, u_int32_t *count)
467 1.1 augustss {
468 1.10 scw struct uftdi_softc *sc = vsc;
469 1.10 scw
470 1.1 augustss DPRINTFN(10,("uftdi_write: sc=%p, port=%d count=%u data[0]=0x%02x\n",
471 1.1 augustss vsc, portno, *count, from[0]));
472 1.1 augustss
473 1.1 augustss /* Make length tag and copy data */
474 1.10 scw if (sc->sc_hdrlen > 0)
475 1.10 scw *to = FTDI_OUT_TAG(*count, portno);
476 1.10 scw
477 1.10 scw memcpy(to + sc->sc_hdrlen, from, *count);
478 1.10 scw *count += sc->sc_hdrlen;
479 1.1 augustss }
480 1.1 augustss
481 1.1 augustss Static void
482 1.2 augustss uftdi_set(void *vsc, int portno, int reg, int onoff)
483 1.1 augustss {
484 1.1 augustss struct uftdi_softc *sc = vsc;
485 1.1 augustss usb_device_request_t req;
486 1.1 augustss int ctl;
487 1.1 augustss
488 1.1 augustss DPRINTF(("uftdi_set: sc=%p, port=%d reg=%d onoff=%d\n", vsc, portno,
489 1.1 augustss reg, onoff));
490 1.1 augustss
491 1.1 augustss switch (reg) {
492 1.1 augustss case UCOM_SET_DTR:
493 1.1 augustss ctl = onoff ? FTDI_SIO_SET_DTR_HIGH : FTDI_SIO_SET_DTR_LOW;
494 1.1 augustss break;
495 1.1 augustss case UCOM_SET_RTS:
496 1.1 augustss ctl = onoff ? FTDI_SIO_SET_RTS_HIGH : FTDI_SIO_SET_RTS_LOW;
497 1.1 augustss break;
498 1.1 augustss case UCOM_SET_BREAK:
499 1.8 ichiro uftdi_break(sc, portno, onoff);
500 1.1 augustss return;
501 1.1 augustss default:
502 1.1 augustss return;
503 1.1 augustss }
504 1.1 augustss req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
505 1.1 augustss req.bRequest = FTDI_SIO_MODEM_CTRL;
506 1.1 augustss USETW(req.wValue, ctl);
507 1.1 augustss USETW(req.wIndex, portno);
508 1.1 augustss USETW(req.wLength, 0);
509 1.1 augustss DPRINTFN(2,("uftdi_set: reqtype=0x%02x req=0x%02x value=0x%04x "
510 1.1 augustss "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
511 1.1 augustss UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
512 1.1 augustss (void)usbd_do_request(sc->sc_udev, &req, NULL);
513 1.1 augustss }
514 1.1 augustss
515 1.1 augustss Static int
516 1.2 augustss uftdi_param(void *vsc, int portno, struct termios *t)
517 1.1 augustss {
518 1.1 augustss struct uftdi_softc *sc = vsc;
519 1.1 augustss usb_device_request_t req;
520 1.1 augustss usbd_status err;
521 1.12 scw int rate, data, flow;
522 1.1 augustss
523 1.1 augustss DPRINTF(("uftdi_param: sc=%p\n", sc));
524 1.1 augustss
525 1.1 augustss if (sc->sc_dying)
526 1.1 augustss return (EIO);
527 1.1 augustss
528 1.10 scw switch (sc->sc_type) {
529 1.10 scw case UFTDI_TYPE_SIO:
530 1.10 scw switch (t->c_ospeed) {
531 1.10 scw case 300: rate = ftdi_sio_b300; break;
532 1.10 scw case 600: rate = ftdi_sio_b600; break;
533 1.10 scw case 1200: rate = ftdi_sio_b1200; break;
534 1.10 scw case 2400: rate = ftdi_sio_b2400; break;
535 1.10 scw case 4800: rate = ftdi_sio_b4800; break;
536 1.10 scw case 9600: rate = ftdi_sio_b9600; break;
537 1.10 scw case 19200: rate = ftdi_sio_b19200; break;
538 1.10 scw case 38400: rate = ftdi_sio_b38400; break;
539 1.10 scw case 57600: rate = ftdi_sio_b57600; break;
540 1.10 scw case 115200: rate = ftdi_sio_b115200; break;
541 1.10 scw default:
542 1.10 scw return (EINVAL);
543 1.10 scw }
544 1.10 scw break;
545 1.10 scw
546 1.10 scw case UFTDI_TYPE_8U232AM:
547 1.10 scw switch(t->c_ospeed) {
548 1.10 scw case 300: rate = ftdi_8u232am_b300; break;
549 1.10 scw case 600: rate = ftdi_8u232am_b600; break;
550 1.10 scw case 1200: rate = ftdi_8u232am_b1200; break;
551 1.10 scw case 2400: rate = ftdi_8u232am_b2400; break;
552 1.10 scw case 4800: rate = ftdi_8u232am_b4800; break;
553 1.10 scw case 9600: rate = ftdi_8u232am_b9600; break;
554 1.10 scw case 19200: rate = ftdi_8u232am_b19200; break;
555 1.10 scw case 38400: rate = ftdi_8u232am_b38400; break;
556 1.10 scw case 57600: rate = ftdi_8u232am_b57600; break;
557 1.10 scw case 115200: rate = ftdi_8u232am_b115200; break;
558 1.10 scw case 230400: rate = ftdi_8u232am_b230400; break;
559 1.10 scw case 460800: rate = ftdi_8u232am_b460800; break;
560 1.10 scw case 921600: rate = ftdi_8u232am_b921600; break;
561 1.10 scw default:
562 1.10 scw return (EINVAL);
563 1.10 scw }
564 1.10 scw break;
565 1.16 mycroft
566 1.15 christos default:
567 1.16 mycroft return (EINVAL);
568 1.1 augustss }
569 1.1 augustss req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
570 1.1 augustss req.bRequest = FTDI_SIO_SET_BAUD_RATE;
571 1.1 augustss USETW(req.wValue, rate);
572 1.1 augustss USETW(req.wIndex, portno);
573 1.1 augustss USETW(req.wLength, 0);
574 1.1 augustss DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x "
575 1.1 augustss "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
576 1.1 augustss UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
577 1.1 augustss err = usbd_do_request(sc->sc_udev, &req, NULL);
578 1.1 augustss if (err)
579 1.1 augustss return (EIO);
580 1.1 augustss
581 1.1 augustss if (ISSET(t->c_cflag, CSTOPB))
582 1.1 augustss data = FTDI_SIO_SET_DATA_STOP_BITS_2;
583 1.1 augustss else
584 1.1 augustss data = FTDI_SIO_SET_DATA_STOP_BITS_1;
585 1.1 augustss if (ISSET(t->c_cflag, PARENB)) {
586 1.1 augustss if (ISSET(t->c_cflag, PARODD))
587 1.1 augustss data |= FTDI_SIO_SET_DATA_PARITY_ODD;
588 1.1 augustss else
589 1.1 augustss data |= FTDI_SIO_SET_DATA_PARITY_EVEN;
590 1.1 augustss } else
591 1.1 augustss data |= FTDI_SIO_SET_DATA_PARITY_NONE;
592 1.1 augustss switch (ISSET(t->c_cflag, CSIZE)) {
593 1.1 augustss case CS5:
594 1.1 augustss data |= FTDI_SIO_SET_DATA_BITS(5);
595 1.1 augustss break;
596 1.1 augustss case CS6:
597 1.1 augustss data |= FTDI_SIO_SET_DATA_BITS(6);
598 1.1 augustss break;
599 1.1 augustss case CS7:
600 1.1 augustss data |= FTDI_SIO_SET_DATA_BITS(7);
601 1.1 augustss break;
602 1.1 augustss case CS8:
603 1.1 augustss data |= FTDI_SIO_SET_DATA_BITS(8);
604 1.1 augustss break;
605 1.1 augustss }
606 1.8 ichiro sc->last_lcr = data;
607 1.8 ichiro
608 1.1 augustss req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
609 1.1 augustss req.bRequest = FTDI_SIO_SET_DATA;
610 1.1 augustss USETW(req.wValue, data);
611 1.1 augustss USETW(req.wIndex, portno);
612 1.1 augustss USETW(req.wLength, 0);
613 1.1 augustss DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x "
614 1.1 augustss "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
615 1.1 augustss UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
616 1.12 scw err = usbd_do_request(sc->sc_udev, &req, NULL);
617 1.12 scw if (err)
618 1.12 scw return (EIO);
619 1.12 scw
620 1.12 scw if (ISSET(t->c_cflag, CRTSCTS)) {
621 1.12 scw flow = FTDI_SIO_RTS_CTS_HS;
622 1.12 scw USETW(req.wValue, 0);
623 1.12 scw } else if (ISSET(t->c_iflag, IXON|IXOFF)) {
624 1.12 scw flow = FTDI_SIO_XON_XOFF_HS;
625 1.12 scw USETW2(req.wValue, t->c_cc[VSTOP], t->c_cc[VSTART]);
626 1.12 scw } else {
627 1.12 scw flow = FTDI_SIO_DISABLE_FLOW_CTRL;
628 1.12 scw USETW(req.wValue, 0);
629 1.12 scw }
630 1.12 scw req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
631 1.12 scw req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
632 1.12 scw USETW2(req.wIndex, flow, portno);
633 1.12 scw USETW(req.wLength, 0);
634 1.1 augustss err = usbd_do_request(sc->sc_udev, &req, NULL);
635 1.1 augustss if (err)
636 1.1 augustss return (EIO);
637 1.1 augustss
638 1.1 augustss return (0);
639 1.1 augustss }
640 1.1 augustss
641 1.1 augustss void
642 1.29 christos uftdi_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
643 1.1 augustss {
644 1.1 augustss struct uftdi_softc *sc = vsc;
645 1.1 augustss
646 1.1 augustss DPRINTF(("uftdi_status: msr=0x%02x lsr=0x%02x\n",
647 1.1 augustss sc->sc_msr, sc->sc_lsr));
648 1.1 augustss
649 1.1 augustss if (msr != NULL)
650 1.1 augustss *msr = sc->sc_msr;
651 1.1 augustss if (lsr != NULL)
652 1.1 augustss *lsr = sc->sc_lsr;
653 1.8 ichiro }
654 1.8 ichiro
655 1.8 ichiro void
656 1.8 ichiro uftdi_break(void *vsc, int portno, int onoff)
657 1.8 ichiro {
658 1.8 ichiro struct uftdi_softc *sc = vsc;
659 1.9 ichiro usb_device_request_t req;
660 1.8 ichiro int data;
661 1.8 ichiro
662 1.8 ichiro DPRINTF(("uftdi_break: sc=%p, port=%d onoff=%d\n", vsc, portno,
663 1.11 augustss onoff));
664 1.8 ichiro
665 1.8 ichiro if (onoff) {
666 1.8 ichiro data = sc->last_lcr | FTDI_SIO_SET_BREAK;
667 1.8 ichiro } else {
668 1.8 ichiro data = sc->last_lcr;
669 1.8 ichiro }
670 1.8 ichiro
671 1.8 ichiro req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
672 1.8 ichiro req.bRequest = FTDI_SIO_SET_DATA;
673 1.8 ichiro USETW(req.wValue, data);
674 1.8 ichiro USETW(req.wIndex, portno);
675 1.8 ichiro USETW(req.wLength, 0);
676 1.8 ichiro (void)usbd_do_request(sc->sc_udev, &req, NULL);
677 1.1 augustss }
678