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