uftdi.c revision 1.19 1 /* $NetBSD: uftdi.c,v 1.19 2004/01/05 13:49:40 augustss 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.19 2004/01/05 13:49:40 augustss 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 uaa->product == USB_PRODUCT_FTDI_SEMC_DSS20 ||
143 uaa->product == USB_PRODUCT_FTDI_LCD_LK202_24_USB ||
144 uaa->product == USB_PRODUCT_FTDI_LCD_MX200_USB))
145 return (UMATCH_VENDOR_PRODUCT);
146
147 return (UMATCH_NONE);
148 }
149
150 USB_ATTACH(uftdi)
151 {
152 USB_ATTACH_START(uftdi, sc, uaa);
153 usbd_device_handle dev = uaa->device;
154 usbd_interface_handle iface;
155 usb_interface_descriptor_t *id;
156 usb_endpoint_descriptor_t *ed;
157 char devinfo[1024];
158 char *devname = USBDEVNAME(sc->sc_dev);
159 int i;
160 usbd_status err;
161 struct ucom_attach_args uca;
162
163 DPRINTFN(10,("\nuftdi_attach: sc=%p\n", sc));
164
165 /* Move the device into the configured state. */
166 err = usbd_set_config_index(dev, UFTDI_CONFIG_INDEX, 1);
167 if (err) {
168 printf("\n%s: failed to set configuration, err=%s\n",
169 devname, usbd_errstr(err));
170 goto bad;
171 }
172
173 err = usbd_device2interface_handle(dev, UFTDI_IFACE_INDEX, &iface);
174 if (err) {
175 printf("\n%s: failed to get interface, err=%s\n",
176 devname, usbd_errstr(err));
177 goto bad;
178 }
179
180 usbd_devinfo(dev, 0, devinfo);
181 USB_ATTACH_SETUP;
182 printf("%s: %s\n", devname, devinfo);
183
184 id = usbd_get_interface_descriptor(iface);
185
186 sc->sc_udev = dev;
187 sc->sc_iface = iface;
188
189 switch (uaa->product) {
190 case USB_PRODUCT_FTDI_SERIAL_8U100AX:
191 sc->sc_type = UFTDI_TYPE_SIO;
192 sc->sc_hdrlen = 1;
193 break;
194
195 case USB_PRODUCT_FTDI_SEMC_DSS20:
196 case USB_PRODUCT_FTDI_SERIAL_8U232AM:
197 case USB_PRODUCT_FTDI_LCD_LK202_24_USB:
198 case USB_PRODUCT_FTDI_LCD_MX200_USB:
199 sc->sc_type = UFTDI_TYPE_8U232AM;
200 sc->sc_hdrlen = 0;
201 break;
202
203 default: /* Can't happen */
204 goto bad;
205 }
206
207 uca.bulkin = uca.bulkout = -1;
208 for (i = 0; i < id->bNumEndpoints; i++) {
209 int addr, dir, attr;
210 ed = usbd_interface2endpoint_descriptor(iface, i);
211 if (ed == NULL) {
212 printf("%s: could not read endpoint descriptor"
213 ": %s\n", devname, usbd_errstr(err));
214 goto bad;
215 }
216
217 addr = ed->bEndpointAddress;
218 dir = UE_GET_DIR(ed->bEndpointAddress);
219 attr = ed->bmAttributes & UE_XFERTYPE;
220 if (dir == UE_DIR_IN && attr == UE_BULK)
221 uca.bulkin = addr;
222 else if (dir == UE_DIR_OUT && attr == UE_BULK)
223 uca.bulkout = addr;
224 else {
225 printf("%s: unexpected endpoint\n", devname);
226 goto bad;
227 }
228 }
229 if (uca.bulkin == -1) {
230 printf("%s: Could not find data bulk in\n",
231 USBDEVNAME(sc->sc_dev));
232 goto bad;
233 }
234 if (uca.bulkout == -1) {
235 printf("%s: Could not find data bulk out\n",
236 USBDEVNAME(sc->sc_dev));
237 goto bad;
238 }
239
240 uca.portno = FTDI_PIT_SIOA;
241 /* bulkin, bulkout set above */
242 uca.ibufsize = UFTDIIBUFSIZE;
243 uca.obufsize = UFTDIOBUFSIZE - sc->sc_hdrlen;
244 uca.ibufsizepad = UFTDIIBUFSIZE;
245 uca.opkthdrlen = sc->sc_hdrlen;
246 uca.device = dev;
247 uca.iface = iface;
248 uca.methods = &uftdi_methods;
249 uca.arg = sc;
250 uca.info = NULL;
251
252 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
253 USBDEV(sc->sc_dev));
254
255 DPRINTF(("uftdi: in=0x%x out=0x%x\n", uca.bulkin, uca.bulkout));
256 sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch);
257
258 USB_ATTACH_SUCCESS_RETURN;
259
260 bad:
261 DPRINTF(("uftdi_attach: ATTACH ERROR\n"));
262 sc->sc_dying = 1;
263 USB_ATTACH_ERROR_RETURN;
264 }
265
266 int
267 uftdi_activate(device_ptr_t self, enum devact act)
268 {
269 struct uftdi_softc *sc = (struct uftdi_softc *)self;
270 int rv = 0;
271
272 switch (act) {
273 case DVACT_ACTIVATE:
274 return (EOPNOTSUPP);
275
276 case DVACT_DEACTIVATE:
277 if (sc->sc_subdev != NULL)
278 rv = config_deactivate(sc->sc_subdev);
279 sc->sc_dying = 1;
280 break;
281 }
282 return (rv);
283 }
284
285 int
286 uftdi_detach(device_ptr_t self, int flags)
287 {
288 struct uftdi_softc *sc = (struct uftdi_softc *)self;
289
290 DPRINTF(("uftdi_detach: sc=%p flags=%d\n", sc, flags));
291 sc->sc_dying = 1;
292 if (sc->sc_subdev != NULL) {
293 config_detach(sc->sc_subdev, flags);
294 sc->sc_subdev = NULL;
295 }
296
297 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
298 USBDEV(sc->sc_dev));
299
300 return (0);
301 }
302
303 Static int
304 uftdi_open(void *vsc, int portno)
305 {
306 struct uftdi_softc *sc = vsc;
307 usb_device_request_t req;
308 usbd_status err;
309 struct termios t;
310
311 DPRINTF(("uftdi_open: sc=%p\n", sc));
312
313 if (sc->sc_dying)
314 return (EIO);
315
316 /* Perform a full reset on the device */
317 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
318 req.bRequest = FTDI_SIO_RESET;
319 USETW(req.wValue, FTDI_SIO_RESET_SIO);
320 USETW(req.wIndex, portno);
321 USETW(req.wLength, 0);
322 err = usbd_do_request(sc->sc_udev, &req, NULL);
323 if (err)
324 return (EIO);
325
326 /* Set 9600 baud, 2 stop bits, no parity, 8 bits */
327 t.c_ospeed = 9600;
328 t.c_cflag = CSTOPB | CS8;
329 (void)uftdi_param(sc, portno, &t);
330
331 /* Turn on RTS/CTS flow control */
332 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
333 req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
334 USETW(req.wValue, 0);
335 USETW2(req.wIndex, FTDI_SIO_RTS_CTS_HS, portno);
336 USETW(req.wLength, 0);
337 err = usbd_do_request(sc->sc_udev, &req, NULL);
338 if (err)
339 return (EIO);
340
341 return (0);
342 }
343
344 Static void
345 uftdi_read(void *vsc, int portno, u_char **ptr, u_int32_t *count)
346 {
347 struct uftdi_softc *sc = vsc;
348 u_char msr, lsr;
349
350 DPRINTFN(15,("uftdi_read: sc=%p, port=%d count=%d\n", sc, portno,
351 *count));
352
353 msr = FTDI_GET_MSR(*ptr);
354 lsr = FTDI_GET_LSR(*ptr);
355
356 #ifdef UFTDI_DEBUG
357 if (*count != 2)
358 DPRINTFN(10,("uftdi_read: sc=%p, port=%d count=%d data[0]="
359 "0x%02x\n", sc, portno, *count, (*ptr)[2]));
360 #endif
361
362 if (sc->sc_msr != msr ||
363 (sc->sc_lsr & FTDI_LSR_MASK) != (lsr & FTDI_LSR_MASK)) {
364 DPRINTF(("uftdi_read: status change msr=0x%02x(0x%02x) "
365 "lsr=0x%02x(0x%02x)\n", msr, sc->sc_msr,
366 lsr, sc->sc_lsr));
367 sc->sc_msr = msr;
368 sc->sc_lsr = lsr;
369 ucom_status_change((struct ucom_softc *)sc->sc_subdev);
370 }
371
372 /* Pick up status and adjust data part. */
373 *ptr += 2;
374 *count -= 2;
375 }
376
377 Static void
378 uftdi_write(void *vsc, int portno, u_char *to, u_char *from, u_int32_t *count)
379 {
380 struct uftdi_softc *sc = vsc;
381
382 DPRINTFN(10,("uftdi_write: sc=%p, port=%d count=%u data[0]=0x%02x\n",
383 vsc, portno, *count, from[0]));
384
385 /* Make length tag and copy data */
386 if (sc->sc_hdrlen > 0)
387 *to = FTDI_OUT_TAG(*count, portno);
388
389 memcpy(to + sc->sc_hdrlen, from, *count);
390 *count += sc->sc_hdrlen;
391 }
392
393 Static void
394 uftdi_set(void *vsc, int portno, int reg, int onoff)
395 {
396 struct uftdi_softc *sc = vsc;
397 usb_device_request_t req;
398 int ctl;
399
400 DPRINTF(("uftdi_set: sc=%p, port=%d reg=%d onoff=%d\n", vsc, portno,
401 reg, onoff));
402
403 switch (reg) {
404 case UCOM_SET_DTR:
405 ctl = onoff ? FTDI_SIO_SET_DTR_HIGH : FTDI_SIO_SET_DTR_LOW;
406 break;
407 case UCOM_SET_RTS:
408 ctl = onoff ? FTDI_SIO_SET_RTS_HIGH : FTDI_SIO_SET_RTS_LOW;
409 break;
410 case UCOM_SET_BREAK:
411 uftdi_break(sc, portno, onoff);
412 return;
413 default:
414 return;
415 }
416 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
417 req.bRequest = FTDI_SIO_MODEM_CTRL;
418 USETW(req.wValue, ctl);
419 USETW(req.wIndex, portno);
420 USETW(req.wLength, 0);
421 DPRINTFN(2,("uftdi_set: reqtype=0x%02x req=0x%02x value=0x%04x "
422 "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
423 UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
424 (void)usbd_do_request(sc->sc_udev, &req, NULL);
425 }
426
427 Static int
428 uftdi_param(void *vsc, int portno, struct termios *t)
429 {
430 struct uftdi_softc *sc = vsc;
431 usb_device_request_t req;
432 usbd_status err;
433 int rate, data, flow;
434
435 DPRINTF(("uftdi_param: sc=%p\n", sc));
436
437 if (sc->sc_dying)
438 return (EIO);
439
440 switch (sc->sc_type) {
441 case UFTDI_TYPE_SIO:
442 switch (t->c_ospeed) {
443 case 300: rate = ftdi_sio_b300; break;
444 case 600: rate = ftdi_sio_b600; break;
445 case 1200: rate = ftdi_sio_b1200; break;
446 case 2400: rate = ftdi_sio_b2400; break;
447 case 4800: rate = ftdi_sio_b4800; break;
448 case 9600: rate = ftdi_sio_b9600; break;
449 case 19200: rate = ftdi_sio_b19200; break;
450 case 38400: rate = ftdi_sio_b38400; break;
451 case 57600: rate = ftdi_sio_b57600; break;
452 case 115200: rate = ftdi_sio_b115200; break;
453 default:
454 return (EINVAL);
455 }
456 break;
457
458 case UFTDI_TYPE_8U232AM:
459 switch(t->c_ospeed) {
460 case 300: rate = ftdi_8u232am_b300; break;
461 case 600: rate = ftdi_8u232am_b600; break;
462 case 1200: rate = ftdi_8u232am_b1200; break;
463 case 2400: rate = ftdi_8u232am_b2400; break;
464 case 4800: rate = ftdi_8u232am_b4800; break;
465 case 9600: rate = ftdi_8u232am_b9600; break;
466 case 19200: rate = ftdi_8u232am_b19200; break;
467 case 38400: rate = ftdi_8u232am_b38400; break;
468 case 57600: rate = ftdi_8u232am_b57600; break;
469 case 115200: rate = ftdi_8u232am_b115200; break;
470 case 230400: rate = ftdi_8u232am_b230400; break;
471 case 460800: rate = ftdi_8u232am_b460800; break;
472 case 921600: rate = ftdi_8u232am_b921600; break;
473 default:
474 return (EINVAL);
475 }
476 break;
477
478 default:
479 return (EINVAL);
480 }
481 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
482 req.bRequest = FTDI_SIO_SET_BAUD_RATE;
483 USETW(req.wValue, rate);
484 USETW(req.wIndex, portno);
485 USETW(req.wLength, 0);
486 DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x "
487 "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
488 UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
489 err = usbd_do_request(sc->sc_udev, &req, NULL);
490 if (err)
491 return (EIO);
492
493 if (ISSET(t->c_cflag, CSTOPB))
494 data = FTDI_SIO_SET_DATA_STOP_BITS_2;
495 else
496 data = FTDI_SIO_SET_DATA_STOP_BITS_1;
497 if (ISSET(t->c_cflag, PARENB)) {
498 if (ISSET(t->c_cflag, PARODD))
499 data |= FTDI_SIO_SET_DATA_PARITY_ODD;
500 else
501 data |= FTDI_SIO_SET_DATA_PARITY_EVEN;
502 } else
503 data |= FTDI_SIO_SET_DATA_PARITY_NONE;
504 switch (ISSET(t->c_cflag, CSIZE)) {
505 case CS5:
506 data |= FTDI_SIO_SET_DATA_BITS(5);
507 break;
508 case CS6:
509 data |= FTDI_SIO_SET_DATA_BITS(6);
510 break;
511 case CS7:
512 data |= FTDI_SIO_SET_DATA_BITS(7);
513 break;
514 case CS8:
515 data |= FTDI_SIO_SET_DATA_BITS(8);
516 break;
517 }
518 sc->last_lcr = data;
519
520 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
521 req.bRequest = FTDI_SIO_SET_DATA;
522 USETW(req.wValue, data);
523 USETW(req.wIndex, portno);
524 USETW(req.wLength, 0);
525 DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x "
526 "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
527 UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
528 err = usbd_do_request(sc->sc_udev, &req, NULL);
529 if (err)
530 return (EIO);
531
532 if (ISSET(t->c_cflag, CRTSCTS)) {
533 flow = FTDI_SIO_RTS_CTS_HS;
534 USETW(req.wValue, 0);
535 } else if (ISSET(t->c_iflag, IXON|IXOFF)) {
536 flow = FTDI_SIO_XON_XOFF_HS;
537 USETW2(req.wValue, t->c_cc[VSTOP], t->c_cc[VSTART]);
538 } else {
539 flow = FTDI_SIO_DISABLE_FLOW_CTRL;
540 USETW(req.wValue, 0);
541 }
542 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
543 req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
544 USETW2(req.wIndex, flow, portno);
545 USETW(req.wLength, 0);
546 err = usbd_do_request(sc->sc_udev, &req, NULL);
547 if (err)
548 return (EIO);
549
550 return (0);
551 }
552
553 void
554 uftdi_get_status(void *vsc, int portno, u_char *lsr, u_char *msr)
555 {
556 struct uftdi_softc *sc = vsc;
557
558 DPRINTF(("uftdi_status: msr=0x%02x lsr=0x%02x\n",
559 sc->sc_msr, sc->sc_lsr));
560
561 if (msr != NULL)
562 *msr = sc->sc_msr;
563 if (lsr != NULL)
564 *lsr = sc->sc_lsr;
565 }
566
567 void
568 uftdi_break(void *vsc, int portno, int onoff)
569 {
570 struct uftdi_softc *sc = vsc;
571 usb_device_request_t req;
572 int data;
573
574 DPRINTF(("uftdi_break: sc=%p, port=%d onoff=%d\n", vsc, portno,
575 onoff));
576
577 if (onoff) {
578 data = sc->last_lcr | FTDI_SIO_SET_BREAK;
579 } else {
580 data = sc->last_lcr;
581 }
582
583 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
584 req.bRequest = FTDI_SIO_SET_DATA;
585 USETW(req.wValue, data);
586 USETW(req.wIndex, portno);
587 USETW(req.wLength, 0);
588 (void)usbd_do_request(sc->sc_udev, &req, NULL);
589 }
590