uftdi.c revision 1.1 1 /* $NetBSD: uftdi.c,v 1.1 2000/04/14 14:51:22 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/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/device.h>
52 #include <sys/conf.h>
53 #include <sys/tty.h>
54
55 #include <dev/usb/usb.h>
56 #include <dev/usb/usbhid.h>
57
58 #include <dev/usb/usbdi.h>
59 #include <dev/usb/usbdi_util.h>
60 #include <dev/usb/usbdevs.h>
61
62 #include <dev/usb/ucomvar.h>
63
64 #include <dev/usb/uftdireg.h>
65
66 #ifdef UFTDI_DEBUG
67 #define DPRINTF(x) if (uftdidebug) printf x
68 #define DPRINTFN(n,x) if (uftdidebug>(n)) printf x
69 int uftdidebug = 15;
70 #else
71 #define DPRINTF(x)
72 #define DPRINTFN(n,x)
73 #endif
74
75 #define UFTDI_CONFIG_INDEX 0
76 #define UFTDI_IFACE_INDEX 0
77
78
79 /*
80 * These are the maximum number of bytes transferred per frame.
81 * The output buffer size cannot be increased due to the size encoding.
82 */
83 #define UFTDIIBUFSIZE 64
84 #define UFTDIOBUFSIZE 64
85
86 struct uftdi_softc {
87 USBBASEDEVICE sc_dev; /* base device */
88 usbd_device_handle sc_udev; /* device */
89 usbd_interface_handle sc_iface; /* interface */
90
91 u_char sc_msr;
92 u_char sc_lsr;
93
94 device_ptr_t sc_subdev;
95
96 u_char sc_dying;
97 };
98
99 Static void uftdi_get_status __P((void *, int portno, u_char *lsr,
100 u_char *msr));
101 Static void uftdi_set __P((void *, int, int, int));
102 Static int uftdi_param __P((void *, int, struct termios *));
103 Static int uftdi_open __P((void *sc, int portno));
104 Static void uftdi_read __P((void *sc, int portno, u_char **ptr,
105 u_int32_t *count));
106 Static void uftdi_write __P((void *sc, int portno, u_char *to,
107 u_char *from, u_int32_t *count));
108
109 struct ucom_methods uftdi_methods = {
110 uftdi_get_status,
111 uftdi_set,
112 uftdi_param,
113 NULL,
114 uftdi_open,
115 NULL,
116 uftdi_read,
117 uftdi_write,
118 };
119
120 USB_DECLARE_DRIVER(uftdi);
121
122 USB_MATCH(uftdi)
123 {
124 USB_MATCH_START(uftdi, uaa);
125
126 if (uaa->iface != NULL)
127 return (UMATCH_NONE);
128
129 DPRINTFN(20,("uftdi: vendor=0x%x, product=0x%x\n",
130 uaa->vendor, uaa->product));
131
132 if (uaa->vendor == USB_VENDOR_FTDI &&
133 uaa->product == USB_PRODUCT_FTDI_SERIAL)
134 return (UMATCH_VENDOR_PRODUCT);
135
136 return (UMATCH_NONE);
137 }
138
139 USB_ATTACH(uftdi)
140 {
141 USB_ATTACH_START(uftdi, sc, uaa);
142 usbd_device_handle dev = uaa->device;
143 usbd_interface_handle iface;
144 usb_interface_descriptor_t *id;
145 usb_endpoint_descriptor_t *ed;
146 char devinfo[1024];
147 char *devname = USBDEVNAME(sc->sc_dev);
148 int i;
149 usbd_status err;
150 struct ucom_attach_args uca;
151
152 DPRINTFN(10,("\nuftdi_attach: sc=%p\n", sc));
153
154 /* Move the device into the configured state. */
155 err = usbd_set_config_index(dev, UFTDI_CONFIG_INDEX, 1);
156 if (err) {
157 printf("\n%s: failed to set configuration, err=%s\n",
158 devname, usbd_errstr(err));
159 goto bad;
160 }
161
162 err = usbd_device2interface_handle(dev, UFTDI_IFACE_INDEX, &iface);
163 if (err) {
164 printf("\n%s: failed to get interface, err=%s\n",
165 devname, usbd_errstr(err));
166 goto bad;
167 }
168
169 usbd_devinfo(dev, 0, devinfo);
170 USB_ATTACH_SETUP;
171 printf("%s: %s\n", devname, devinfo);
172
173 id = usbd_get_interface_descriptor(iface);
174
175 sc->sc_udev = dev;
176 sc->sc_iface = iface;
177
178 uca.bulkin = uca.bulkout = -1;
179 for (i = 0; i < id->bNumEndpoints; i++) {
180 int addr, dir, attr;
181 ed = usbd_interface2endpoint_descriptor(iface, i);
182 if (ed == NULL) {
183 printf("%s: could not read endpoint descriptor"
184 ": %s\n", devname, usbd_errstr(err));
185 goto bad;
186 }
187
188 addr = ed->bEndpointAddress;
189 dir = UE_GET_DIR(ed->bEndpointAddress);
190 attr = ed->bmAttributes & UE_XFERTYPE;
191 if (dir == UE_DIR_IN && attr == UE_BULK)
192 uca.bulkin = addr;
193 else if (dir == UE_DIR_OUT && attr == UE_BULK)
194 uca.bulkout = addr;
195 else {
196 printf("%s: unexpected endpoint\n", devname);
197 goto bad;
198 }
199 }
200 if (uca.bulkin == -1) {
201 printf("%s: Could not find data bulk in\n",
202 USBDEVNAME(sc->sc_dev));
203 goto bad;
204 }
205 if (uca.bulkout == -1) {
206 printf("%s: Could not find data bulk out\n",
207 USBDEVNAME(sc->sc_dev));
208 goto bad;
209 }
210
211 uca.portno = FTDI_PIT_SIOA;
212 /* bulkin, bulkout set above */
213 uca.ibufsize = UFTDIIBUFSIZE;
214 uca.obufsize = UFTDIOBUFSIZE - 1;
215 uca.ibufsizepad = UFTDIIBUFSIZE;
216 uca.obufsizepad = UFTDIOBUFSIZE;
217 uca.device = dev;
218 uca.iface = iface;
219 uca.methods = &uftdi_methods;
220 uca.arg = sc;
221
222 DPRINTF(("uftdi: in=0x%x out=0x%x\n", uca.bulkin, uca.bulkout));
223 sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch);
224
225 USB_ATTACH_SUCCESS_RETURN;
226
227 bad:
228 DPRINTF(("uftdi_attach: ATTACH ERROR\n"));
229 sc->sc_dying = 1;
230 USB_ATTACH_ERROR_RETURN;
231 }
232
233 int
234 uftdi_activate(self, act)
235 device_ptr_t self;
236 enum devact act;
237 {
238 struct uftdi_softc *sc = (struct uftdi_softc *)self;
239 int rv = 0;
240
241 switch (act) {
242 case DVACT_ACTIVATE:
243 return (EOPNOTSUPP);
244 break;
245
246 case DVACT_DEACTIVATE:
247 if (sc->sc_subdev != NULL)
248 rv = config_deactivate(sc->sc_subdev);
249 sc->sc_dying = 1;
250 break;
251 }
252 return (rv);
253 }
254
255 int
256 uftdi_detach(self, flags)
257 device_ptr_t self;
258 int flags;
259 {
260 struct uftdi_softc *sc = (struct uftdi_softc *)self;
261 int rv = 0;
262
263 DPRINTF(("uftdi_detach: sc=%p flags=%d\n", sc, flags));
264 sc->sc_dying = 1;
265 if (sc->sc_subdev != NULL) {
266 rv = config_detach(sc->sc_subdev, flags);
267 sc->sc_subdev = NULL;
268 }
269 return (0);
270 }
271
272 Static int
273 uftdi_open(vsc, portno)
274 void *vsc;
275 int portno;
276 {
277 struct uftdi_softc *sc = vsc;
278 usb_device_request_t req;
279 usbd_status err;
280 struct termios t;
281
282 DPRINTF(("uftdi_open: sc=%p\n", sc));
283
284 if (sc->sc_dying)
285 return (EIO);
286
287 /* Perform a full reset on the device */
288 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
289 req.bRequest = FTDI_SIO_RESET;
290 USETW(req.wValue, FTDI_SIO_RESET_SIO);
291 USETW(req.wIndex, portno);
292 USETW(req.wLength, 0);
293 err = usbd_do_request(sc->sc_udev, &req, NULL);
294 if (err)
295 return (EIO);
296
297 /* Set 9600 baud, 2 stop bits, no parity, 8 bits */
298 t.c_ospeed = 9600;
299 t.c_cflag = CSTOPB | CS8;
300 (void)uftdi_param(sc, portno, &t);
301
302 /* Turn on RTS/CTS flow control */
303 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
304 req.bRequest = FTDI_SIO_SET_FLOW_CTRL;
305 USETW(req.wValue, 0);
306 USETW2(req.wIndex, FTDI_SIO_RTS_CTS_HS, portno);
307 USETW(req.wLength, 0);
308 err = usbd_do_request(sc->sc_udev, &req, NULL);
309 if (err)
310 return (EIO);
311
312 return (0);
313 }
314
315 Static void
316 uftdi_read(vsc, portno, ptr, count)
317 void *vsc;
318 int portno;
319 u_char **ptr;
320 u_int32_t *count;
321 {
322 struct uftdi_softc *sc = vsc;
323 u_char msr, lsr;
324
325 DPRINTFN(15,("uftdi_read: sc=%p, port=%d count=%d\n", sc, portno,
326 *count));
327
328 msr = FTDI_GET_MSR(*ptr);
329 lsr = FTDI_GET_LSR(*ptr);
330
331 #ifdef UFTDI_DEBUG
332 if (*count != 2)
333 DPRINTFN(10,("uftdi_read: sc=%p, port=%d count=%d data[0]="
334 "0x%02x\n", sc, portno, *count, (*ptr)[2]));
335 #endif
336
337 if (sc->sc_msr != msr ||
338 (sc->sc_lsr & FTDI_LSR_MASK) != (lsr & FTDI_LSR_MASK)) {
339 DPRINTF(("uftdi_read: status change msr=0x%02x(0x%02x) "
340 "lsr=0x%02x(0x%02x)\n", msr, sc->sc_msr,
341 lsr, sc->sc_lsr));
342 sc->sc_msr = msr;
343 sc->sc_lsr = lsr;
344 ucom_status_change((struct ucom_softc *)sc->sc_subdev);
345 }
346
347 /* Pick up status and adjust data part. */
348 *ptr += 2;
349 *count -= 2;
350 }
351
352 Static void
353 uftdi_write(vsc, portno, to, from, count)
354 void *vsc;
355 int portno;
356 u_char *to;
357 u_char *from;
358 u_int32_t *count;
359 {
360 DPRINTFN(10,("uftdi_write: sc=%p, port=%d count=%u data[0]=0x%02x\n",
361 vsc, portno, *count, from[0]));
362
363 /* Make length tag and copy data */
364 *to = FTDI_OUT_TAG(*count, portno);
365 memcpy(to+1, from, *count);
366 ++*count;
367 }
368
369 Static void
370 uftdi_set(vsc, portno, reg, onoff)
371 void *vsc;
372 int portno;
373 int reg;
374 int onoff;
375 {
376 struct uftdi_softc *sc = vsc;
377 usb_device_request_t req;
378 int ctl;
379
380 DPRINTF(("uftdi_set: sc=%p, port=%d reg=%d onoff=%d\n", vsc, portno,
381 reg, onoff));
382
383 switch (reg) {
384 case UCOM_SET_DTR:
385 ctl = onoff ? FTDI_SIO_SET_DTR_HIGH : FTDI_SIO_SET_DTR_LOW;
386 break;
387 case UCOM_SET_RTS:
388 ctl = onoff ? FTDI_SIO_SET_RTS_HIGH : FTDI_SIO_SET_RTS_LOW;
389 break;
390 case UCOM_SET_BREAK:
391 /* XXX how do we set break? */
392 return;
393 default:
394 return;
395 }
396 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
397 req.bRequest = FTDI_SIO_MODEM_CTRL;
398 USETW(req.wValue, ctl);
399 USETW(req.wIndex, portno);
400 USETW(req.wLength, 0);
401 DPRINTFN(2,("uftdi_set: reqtype=0x%02x req=0x%02x value=0x%04x "
402 "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
403 UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
404 (void)usbd_do_request(sc->sc_udev, &req, NULL);
405 }
406
407 Static int
408 uftdi_param(vsc, portno, t)
409 void *vsc;
410 int portno;
411 struct termios *t;
412 {
413 struct uftdi_softc *sc = vsc;
414 usb_device_request_t req;
415 usbd_status err;
416 int rate, data;
417
418 DPRINTF(("uftdi_param: sc=%p\n", sc));
419
420 if (sc->sc_dying)
421 return (EIO);
422
423 switch (t->c_ospeed) {
424 case 300: rate = ftdi_sio_b300; break;
425 case 600: rate = ftdi_sio_b600; break;
426 case 1200: rate = ftdi_sio_b1200; break;
427 case 2400: rate = ftdi_sio_b2400; break;
428 case 4800: rate = ftdi_sio_b4800; break;
429 case 9600: rate = ftdi_sio_b9600; break;
430 case 19200: rate = ftdi_sio_b19200; break;
431 case 38400: rate = ftdi_sio_b38400; break;
432 case 57600: rate = ftdi_sio_b57600; break;
433 case 115200: rate = ftdi_sio_b115200; break;
434 default:
435 return (EINVAL);
436 }
437 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
438 req.bRequest = FTDI_SIO_SET_BAUD_RATE;
439 USETW(req.wValue, rate);
440 USETW(req.wIndex, portno);
441 USETW(req.wLength, 0);
442 DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x "
443 "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
444 UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
445 err = usbd_do_request(sc->sc_udev, &req, NULL);
446 if (err)
447 return (EIO);
448
449 if (ISSET(t->c_cflag, CSTOPB))
450 data = FTDI_SIO_SET_DATA_STOP_BITS_2;
451 else
452 data = FTDI_SIO_SET_DATA_STOP_BITS_1;
453 if (ISSET(t->c_cflag, PARENB)) {
454 if (ISSET(t->c_cflag, PARODD))
455 data |= FTDI_SIO_SET_DATA_PARITY_ODD;
456 else
457 data |= FTDI_SIO_SET_DATA_PARITY_EVEN;
458 } else
459 data |= FTDI_SIO_SET_DATA_PARITY_NONE;
460 switch (ISSET(t->c_cflag, CSIZE)) {
461 case CS5:
462 data |= FTDI_SIO_SET_DATA_BITS(5);
463 break;
464 case CS6:
465 data |= FTDI_SIO_SET_DATA_BITS(6);
466 break;
467 case CS7:
468 data |= FTDI_SIO_SET_DATA_BITS(7);
469 break;
470 case CS8:
471 data |= FTDI_SIO_SET_DATA_BITS(8);
472 break;
473 }
474 req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
475 req.bRequest = FTDI_SIO_SET_DATA;
476 USETW(req.wValue, data);
477 USETW(req.wIndex, portno);
478 USETW(req.wLength, 0);
479 DPRINTFN(2,("uftdi_param: reqtype=0x%02x req=0x%02x value=0x%04x "
480 "index=0x%04x len=%d\n", req.bmRequestType, req.bRequest,
481 UGETW(req.wValue), UGETW(req.wIndex), UGETW(req.wLength)));
482 err = usbd_do_request(sc->sc_udev, &req, NULL);
483 if (err)
484 return (EIO);
485
486 return (0);
487 }
488
489 void
490 uftdi_get_status(vsc, portno, lsr, msr)
491 void *vsc;
492 int portno;
493 u_char *lsr, *msr;
494 {
495 struct uftdi_softc *sc = vsc;
496
497 DPRINTF(("uftdi_status: msr=0x%02x lsr=0x%02x\n",
498 sc->sc_msr, sc->sc_lsr));
499
500 if (msr != NULL)
501 *msr = sc->sc_msr;
502 if (lsr != NULL)
503 *lsr = sc->sc_lsr;
504 }
505