xref: /openbsd-src/sys/dev/usb/uftdi.c (revision db3296cf5c1dd9058ceecc3a29fe4aaa0bd26000)
1 /*	$OpenBSD: uftdi.c,v 1.12 2003/05/19 00:33:26 nate Exp $ 	*/
2 /*	$NetBSD: uftdi.c,v 1.14 2003/02/23 04:20:07 simonb Exp $	*/
3 
4 /*
5  * Copyright (c) 2000 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Lennart Augustsson (lennart@augustsson.net).
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *        This product includes software developed by the NetBSD
22  *        Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*
41  * FTDI FT8U100AX serial adapter driver
42  */
43 
44 /*
45  * XXX This driver will not support multiple serial ports.
46  * XXX The ucom layer needs to be extended first.
47  */
48 
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/kernel.h>
52 #include <sys/device.h>
53 #include <sys/conf.h>
54 #include <sys/tty.h>
55 
56 #include <dev/usb/usb.h>
57 #include <dev/usb/usbhid.h>
58 
59 #include <dev/usb/usbdi.h>
60 #include <dev/usb/usbdi_util.h>
61 #include <dev/usb/usbdevs.h>
62 
63 #include <dev/usb/ucomvar.h>
64 
65 #include <dev/usb/uftdireg.h>
66 
67 #ifdef UFTDI_DEBUG
68 #define DPRINTF(x)	if (uftdidebug) printf x
69 #define DPRINTFN(n,x)	if (uftdidebug>(n)) printf x
70 int uftdidebug = 0;
71 #else
72 #define DPRINTF(x)
73 #define DPRINTFN(n,x)
74 #endif
75 
76 #define UFTDI_CONFIG_INDEX	0
77 #define UFTDI_IFACE_INDEX	0
78 
79 
80 /*
81  * These are the maximum number of bytes transferred per frame.
82  * The output buffer size cannot be increased due to the size encoding.
83  */
84 #define UFTDIIBUFSIZE 64
85 #define UFTDIOBUFSIZE 64
86 
87 struct uftdi_softc {
88 	USBBASEDEVICE		sc_dev;		/* base device */
89 	usbd_device_handle	sc_udev;	/* device */
90 	usbd_interface_handle	sc_iface;	/* interface */
91 
92 	enum uftdi_type		sc_type;
93 	u_int			sc_hdrlen;
94 
95 	u_char			sc_msr;
96 	u_char			sc_lsr;
97 
98 	device_ptr_t		sc_subdev;
99 
100 	u_char			sc_dying;
101 
102 	u_int			last_lcr;
103 };
104 
105 Static void	uftdi_get_status(void *, int portno, u_char *lsr, u_char *msr);
106 Static void	uftdi_set(void *, int, int, int);
107 Static int	uftdi_param(void *, int, struct termios *);
108 Static int	uftdi_open(void *sc, int portno);
109 Static void	uftdi_read(void *sc, int portno, u_char **ptr,
110 			   u_int32_t *count);
111 Static void	uftdi_write(void *sc, int portno, u_char *to, u_char *from,
112 			    u_int32_t *count);
113 Static void	uftdi_break(void *sc, int portno, int onoff);
114 
115 struct ucom_methods uftdi_methods = {
116 	uftdi_get_status,
117 	uftdi_set,
118 	uftdi_param,
119 	NULL,
120 	uftdi_open,
121 	NULL,
122 	uftdi_read,
123 	uftdi_write,
124 };
125 
126 USB_DECLARE_DRIVER(uftdi);
127 
128 USB_MATCH(uftdi)
129 {
130 	USB_MATCH_START(uftdi, uaa);
131 
132 	if (uaa->iface != NULL)
133 		return (UMATCH_NONE);
134 
135 	DPRINTFN(20,("uftdi: vendor=0x%x, product=0x%x\n",
136 		     uaa->vendor, uaa->product));
137 
138 	if (uaa->vendor == USB_VENDOR_FTDI &&
139 	    (uaa->product == USB_PRODUCT_FTDI_SERIAL_8U100AX ||
140 	     uaa->product == USB_PRODUCT_FTDI_SERIAL_8U232AM))
141 		return (UMATCH_VENDOR_PRODUCT);
142 
143 	return (UMATCH_NONE);
144 }
145 
146 USB_ATTACH(uftdi)
147 {
148 	USB_ATTACH_START(uftdi, sc, uaa);
149 	usbd_device_handle dev = uaa->device;
150 	usbd_interface_handle iface;
151 	usb_interface_descriptor_t *id;
152 	usb_endpoint_descriptor_t *ed;
153 	char devinfo[1024];
154 	char *devname = USBDEVNAME(sc->sc_dev);
155 	int i;
156 	usbd_status err;
157 	struct ucom_attach_args uca;
158 
159 	DPRINTFN(10,("\nuftdi_attach: sc=%p\n", sc));
160 
161 	/* Move the device into the configured state. */
162 	err = usbd_set_config_index(dev, UFTDI_CONFIG_INDEX, 1);
163 	if (err) {
164 		printf("\n%s: failed to set configuration, err=%s\n",
165 		       devname, usbd_errstr(err));
166 		goto bad;
167 	}
168 
169 	err = usbd_device2interface_handle(dev, UFTDI_IFACE_INDEX, &iface);
170 	if (err) {
171 		printf("\n%s: failed to get interface, err=%s\n",
172 		       devname, usbd_errstr(err));
173 		goto bad;
174 	}
175 
176 	usbd_devinfo(dev, 0, devinfo, sizeof devinfo);
177 	USB_ATTACH_SETUP;
178 	printf("%s: %s\n", devname, devinfo);
179 
180 	id = usbd_get_interface_descriptor(iface);
181 
182 	sc->sc_udev = dev;
183 	sc->sc_iface = iface;
184 
185 	switch (uaa->product) {
186 	case USB_PRODUCT_FTDI_SERIAL_8U100AX:
187 		sc->sc_type = UFTDI_TYPE_SIO;
188 		sc->sc_hdrlen = 1;
189 		break;
190 
191 	case USB_PRODUCT_FTDI_SERIAL_8U232AM:
192 		sc->sc_type = UFTDI_TYPE_8U232AM;
193 		sc->sc_hdrlen = 0;
194 		break;
195 
196 	default:		/* Can't happen */
197 		goto bad;
198 	}
199 
200 	uca.bulkin = uca.bulkout = -1;
201 	for (i = 0; i < id->bNumEndpoints; i++) {
202 		int addr, dir, attr;
203 		ed = usbd_interface2endpoint_descriptor(iface, i);
204 		if (ed == NULL) {
205 			printf("%s: could not read endpoint descriptor"
206 			       ": %s\n", devname, usbd_errstr(err));
207 			goto bad;
208 		}
209 
210 		addr = ed->bEndpointAddress;
211 		dir = UE_GET_DIR(ed->bEndpointAddress);
212 		attr = ed->bmAttributes & UE_XFERTYPE;
213 		if (dir == UE_DIR_IN && attr == UE_BULK)
214 			uca.bulkin = addr;
215 		else if (dir == UE_DIR_OUT && attr == UE_BULK)
216 			uca.bulkout = addr;
217 		else {
218 			printf("%s: unexpected endpoint\n", devname);
219 			goto bad;
220 		}
221 	}
222 	if (uca.bulkin == -1) {
223 		printf("%s: Could not find data bulk in\n",
224 		       USBDEVNAME(sc->sc_dev));
225 		goto bad;
226 	}
227 	if (uca.bulkout == -1) {
228 		printf("%s: Could not find data bulk out\n",
229 		       USBDEVNAME(sc->sc_dev));
230 		goto bad;
231 	}
232 
233 	uca.portno = FTDI_PIT_SIOA;
234 	/* bulkin, bulkout set above */
235 	uca.ibufsize = UFTDIIBUFSIZE;
236 	uca.obufsize = UFTDIOBUFSIZE - sc->sc_hdrlen;
237 	uca.ibufsizepad = UFTDIIBUFSIZE;
238 	uca.opkthdrlen = sc->sc_hdrlen;
239 	uca.device = dev;
240 	uca.iface = iface;
241 	uca.methods = &uftdi_methods;
242 	uca.arg = sc;
243 	uca.info = NULL;
244 
245 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
246 			   USBDEV(sc->sc_dev));
247 
248 	DPRINTF(("uftdi: in=0x%x out=0x%x\n", uca.bulkin, uca.bulkout));
249 	sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch);
250 
251 	USB_ATTACH_SUCCESS_RETURN;
252 
253 bad:
254 	DPRINTF(("uftdi_attach: ATTACH ERROR\n"));
255 	sc->sc_dying = 1;
256 	USB_ATTACH_ERROR_RETURN;
257 }
258 
259 int
260 uftdi_activate(device_ptr_t self, enum devact act)
261 {
262 	struct uftdi_softc *sc = (struct uftdi_softc *)self;
263 	int rv = 0;
264 
265 	switch (act) {
266 	case DVACT_ACTIVATE:
267 		return (EOPNOTSUPP);
268 
269 	case DVACT_DEACTIVATE:
270 		if (sc->sc_subdev != NULL)
271 			rv = config_deactivate(sc->sc_subdev);
272 		sc->sc_dying = 1;
273 		break;
274 	}
275 	return (rv);
276 }
277 
278 int
279 uftdi_detach(device_ptr_t self, int flags)
280 {
281 	struct uftdi_softc *sc = (struct uftdi_softc *)self;
282 
283 	DPRINTF(("uftdi_detach: sc=%p flags=%d\n", sc, flags));
284 	sc->sc_dying = 1;
285 	if (sc->sc_subdev != NULL) {
286 		config_detach(sc->sc_subdev, flags);
287 		sc->sc_subdev = NULL;
288 	}
289 
290 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
291 			   USBDEV(sc->sc_dev));
292 
293 	return (0);
294 }
295 
296 Static int
297 uftdi_open(void *vsc, int portno)
298 {
299 	struct uftdi_softc *sc = vsc;
300 	usb_device_request_t req;
301 	usbd_status err;
302 	struct termios t;
303 
304 	DPRINTF(("uftdi_open: sc=%p\n", sc));
305 
306 	if (sc->sc_dying)
307 		return (EIO);
308 
309 	/* Perform a full reset on the device */
310 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
311 	req.bRequest = FTDI_SIO_RESET;
312 	USETW(req.wValue, FTDI_SIO_RESET_SIO);
313 	USETW(req.wIndex, portno);
314 	USETW(req.wLength, 0);
315 	err = usbd_do_request(sc->sc_udev, &req, NULL);
316 	if (err)
317 		return (EIO);
318 
319 	/* Set 9600 baud, 2 stop bits, no parity, 8 bits */
320 	t.c_ospeed = 9600;
321 	t.c_cflag = CSTOPB | CS8;
322 	(void)uftdi_param(sc, portno, &t);
323 
324 	/* Turn on RTS/CTS flow control */
325 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
326 	req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
327 	USETW(req.wValue, 0);
328 	USETW2(req.wIndex, FTDI_SIO_RTS_CTS_HS, portno);
329 	USETW(req.wLength, 0);
330 	err = usbd_do_request(sc->sc_udev, &req, NULL);
331 	if (err)
332 		return (EIO);
333 
334 	return (0);
335 }
336 
337 Static void
338 uftdi_read(void *vsc, int portno, u_char **ptr, u_int32_t *count)
339 {
340 	struct uftdi_softc *sc = vsc;
341 	u_char msr, lsr;
342 
343 	DPRINTFN(15,("uftdi_read: sc=%p, port=%d count=%d\n", sc, portno,
344 		     *count));
345 
346 	msr = FTDI_GET_MSR(*ptr);
347 	lsr = FTDI_GET_LSR(*ptr);
348 
349 #ifdef UFTDI_DEBUG
350 	if (*count != 2)
351 		DPRINTFN(10,("uftdi_read: sc=%p, port=%d count=%d data[0]="
352 			    "0x%02x\n", sc, portno, *count, (*ptr)[2]));
353 #endif
354 
355 	if (sc->sc_msr != msr ||
356 	    (sc->sc_lsr & FTDI_LSR_MASK) != (lsr & FTDI_LSR_MASK)) {
357 		DPRINTF(("uftdi_read: status change msr=0x%02x(0x%02x) "
358 			 "lsr=0x%02x(0x%02x)\n", msr, sc->sc_msr,
359 			 lsr, sc->sc_lsr));
360 		sc->sc_msr = msr;
361 		sc->sc_lsr = lsr;
362 		ucom_status_change((struct ucom_softc *)sc->sc_subdev);
363 	}
364 
365 	/* Pick up status and adjust data part. */
366 	*ptr += 2;
367 	*count -= 2;
368 }
369 
370 Static void
371 uftdi_write(void *vsc, int portno, u_char *to, u_char *from, u_int32_t *count)
372 {
373 	struct uftdi_softc *sc = vsc;
374 
375 	DPRINTFN(10,("uftdi_write: sc=%p, port=%d count=%u data[0]=0x%02x\n",
376 		     vsc, portno, *count, from[0]));
377 
378 	/* Make length tag and copy data */
379 	if (sc->sc_hdrlen > 0)
380 		*to = FTDI_OUT_TAG(*count, portno);
381 
382 	memcpy(to + sc->sc_hdrlen, from, *count);
383 	*count += sc->sc_hdrlen;
384 }
385 
386 Static void
387 uftdi_set(void *vsc, int portno, int reg, int onoff)
388 {
389 	struct uftdi_softc *sc = vsc;
390 	usb_device_request_t req;
391 	int ctl;
392 
393 	DPRINTF(("uftdi_set: sc=%p, port=%d reg=%d onoff=%d\n", vsc, portno,
394 		 reg, onoff));
395 
396 	switch (reg) {
397 	case UCOM_SET_DTR:
398 		ctl = onoff ? FTDI_SIO_SET_DTR_HIGH : FTDI_SIO_SET_DTR_LOW;
399 		break;
400 	case UCOM_SET_RTS:
401 		ctl = onoff ? FTDI_SIO_SET_RTS_HIGH : FTDI_SIO_SET_RTS_LOW;
402 		break;
403 	case UCOM_SET_BREAK:
404 		uftdi_break(sc, portno, onoff);
405 		return;
406 	default:
407 		return;
408 	}
409 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
410 	req.bRequest = FTDI_SIO_MODEM_CTRL;
411 	USETW(req.wValue, ctl);
412 	USETW(req.wIndex, portno);
413 	USETW(req.wLength, 0);
414 	DPRINTFN(2,("uftdi_set: reqtype=0x%02x req=0x%02x value=0x%04x "
415 		    "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
416 		    UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
417 	(void)usbd_do_request(sc->sc_udev, &req, NULL);
418 }
419 
420 Static int
421 uftdi_param(void *vsc, int portno, struct termios *t)
422 {
423 	struct uftdi_softc *sc = vsc;
424 	usb_device_request_t req;
425 	usbd_status err;
426 	int rate, data, flow;
427 
428 	DPRINTF(("uftdi_param: sc=%p\n", sc));
429 
430 	if (sc->sc_dying)
431 		return (EIO);
432 
433 	switch (sc->sc_type) {
434 	case UFTDI_TYPE_SIO:
435 		switch (t->c_ospeed) {
436 		case 300: rate = ftdi_sio_b300; break;
437 		case 600: rate = ftdi_sio_b600; break;
438 		case 1200: rate = ftdi_sio_b1200; break;
439 		case 2400: rate = ftdi_sio_b2400; break;
440 		case 4800: rate = ftdi_sio_b4800; break;
441 		case 9600: rate = ftdi_sio_b9600; break;
442 		case 19200: rate = ftdi_sio_b19200; break;
443 		case 38400: rate = ftdi_sio_b38400; break;
444 		case 57600: rate = ftdi_sio_b57600; break;
445 		case 115200: rate = ftdi_sio_b115200; break;
446 		default:
447 			return (EINVAL);
448 		}
449 		break;
450 
451 	case UFTDI_TYPE_8U232AM:
452 		switch(t->c_ospeed) {
453 		case 300: rate = ftdi_8u232am_b300; break;
454 		case 600: rate = ftdi_8u232am_b600; break;
455 		case 1200: rate = ftdi_8u232am_b1200; break;
456 		case 2400: rate = ftdi_8u232am_b2400; break;
457 		case 4800: rate = ftdi_8u232am_b4800; break;
458 		case 9600: rate = ftdi_8u232am_b9600; break;
459 		case 19200: rate = ftdi_8u232am_b19200; break;
460 		case 38400: rate = ftdi_8u232am_b38400; break;
461 		case 57600: rate = ftdi_8u232am_b57600; break;
462 		case 115200: rate = ftdi_8u232am_b115200; break;
463 		case 230400: rate = ftdi_8u232am_b230400; break;
464 		case 460800: rate = ftdi_8u232am_b460800; break;
465 		case 921600: rate = ftdi_8u232am_b921600; break;
466 		default:
467 			return (EINVAL);
468 		}
469 		break;
470 	}
471 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
472 	req.bRequest = FTDI_SIO_SET_BAUD_RATE;
473 	USETW(req.wValue, rate);
474 	USETW(req.wIndex, portno);
475 	USETW(req.wLength, 0);
476 	DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x "
477 		    "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
478 		    UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
479 	err = usbd_do_request(sc->sc_udev, &req, NULL);
480 	if (err)
481 		return (EIO);
482 
483 	if (ISSET(t->c_cflag, CSTOPB))
484 		data = FTDI_SIO_SET_DATA_STOP_BITS_2;
485 	else
486 		data = FTDI_SIO_SET_DATA_STOP_BITS_1;
487 	if (ISSET(t->c_cflag, PARENB)) {
488 		if (ISSET(t->c_cflag, PARODD))
489 			data |= FTDI_SIO_SET_DATA_PARITY_ODD;
490 		else
491 			data |= FTDI_SIO_SET_DATA_PARITY_EVEN;
492 	} else
493 		data |= FTDI_SIO_SET_DATA_PARITY_NONE;
494 	switch (ISSET(t->c_cflag, CSIZE)) {
495 	case CS5:
496 		data |= FTDI_SIO_SET_DATA_BITS(5);
497 		break;
498 	case CS6:
499 		data |= FTDI_SIO_SET_DATA_BITS(6);
500 		break;
501 	case CS7:
502 		data |= FTDI_SIO_SET_DATA_BITS(7);
503 		break;
504 	case CS8:
505 		data |= FTDI_SIO_SET_DATA_BITS(8);
506 		break;
507 	}
508 	sc->last_lcr = data;
509 
510 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
511 	req.bRequest = FTDI_SIO_SET_DATA;
512 	USETW(req.wValue, data);
513 	USETW(req.wIndex, portno);
514 	USETW(req.wLength, 0);
515 	DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x "
516 		    "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
517 		    UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
518 	err = usbd_do_request(sc->sc_udev, &req, NULL);
519 	if (err)
520 		return (EIO);
521 
522 	if (ISSET(t->c_cflag, CRTSCTS)) {
523 		flow = FTDI_SIO_RTS_CTS_HS;
524 		USETW(req.wValue, 0);
525 	} else if (ISSET(t->c_iflag, IXON|IXOFF)) {
526 		flow = FTDI_SIO_XON_XOFF_HS;
527 		USETW2(req.wValue, t->c_cc[VSTOP], t->c_cc[VSTART]);
528 	} else {
529 		flow = FTDI_SIO_DISABLE_FLOW_CTRL;
530 		USETW(req.wValue, 0);
531 	}
532 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
533 	req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
534 	USETW2(req.wIndex, flow, portno);
535 	USETW(req.wLength, 0);
536 	err = usbd_do_request(sc->sc_udev, &req, NULL);
537 	if (err)
538 		return (EIO);
539 
540 	return (0);
541 }
542 
543 void
544 uftdi_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
545 {
546 	struct uftdi_softc *sc = vsc;
547 
548 	DPRINTF(("uftdi_status: msr=0x%02x lsr=0x%02x\n",
549 		 sc->sc_msr, sc->sc_lsr));
550 
551 	if (msr != NULL)
552 		*msr = sc->sc_msr;
553 	if (lsr != NULL)
554 		*lsr = sc->sc_lsr;
555 }
556 
557 void
558 uftdi_break(void *vsc, int portno, int onoff)
559 {
560 	struct uftdi_softc *sc = vsc;
561 	usb_device_request_t req;
562 	int data;
563 
564 	DPRINTF(("uftdi_break: sc=%p, port=%d onoff=%d\n", vsc, portno,
565 		  onoff));
566 
567 	if (onoff) {
568 		data = sc->last_lcr | FTDI_SIO_SET_BREAK;
569 	} else {
570 		data = sc->last_lcr;
571 	}
572 
573 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
574 	req.bRequest = FTDI_SIO_SET_DATA;
575 	USETW(req.wValue, data);
576 	USETW(req.wIndex, portno);
577 	USETW(req.wLength, 0);
578 	(void)usbd_do_request(sc->sc_udev, &req, NULL);
579 }
580