Home | History | Annotate | Line # | Download | only in usb
uplcom.c revision 1.1
      1 /*	$NetBSD: uplcom.c,v 1.1 2001/01/23 01:24:10 ichiro Exp $	*/
      2 #define UPLCOM_DEBUG
      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 FUKUHARA ichiro (ichiro (at) ichiro.org).
      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  */
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/kernel.h>
     45 #include <sys/ioctl.h>
     46 #include <sys/conf.h>
     47 #include <sys/tty.h>
     48 #include <sys/file.h>
     49 #include <sys/select.h>
     50 #include <sys/proc.h>
     51 #include <sys/vnode.h>
     52 #include <sys/device.h>
     53 #include <sys/poll.h>
     54 
     55 #include <dev/usb/usb.h>
     56 #include <dev/usb/usbcdc.h>
     57 
     58 #include <dev/usb/usbdi.h>
     59 #include <dev/usb/usbdi_util.h>
     60 #include <dev/usb/usbdevs.h>
     61 #include <dev/usb/usb_quirks.h>
     62 
     63 #include <dev/usb/usbdevs.h>
     64 #include <dev/usb/ucomvar.h>
     65 
     66 #ifdef UPLCOM_DEBUG
     67 #define DPRINTFN(n, x)  if (uplcomdebug > (n)) logprintf x
     68 int	uplcomdebug = 0xff;
     69 #else
     70 #define DPRINTFN(n, x)
     71 #endif
     72 #define DPRINTF(x) DPRINTFN(0, x)
     73 
     74 #define	UPLCOM_CONFIG_INDEX	0
     75 #define UPLCOM_IFACE_INDEX	0
     76 #define	UPLCOM_RESET		0
     77 
     78 struct	uplcom_softc {
     79 	USBBASEDEVICE		sc_dev;		/* base device */
     80 	usbd_device_handle	sc_udev;	/* USB device */
     81 	usbd_interface_handle	sc_iface;	/* interface */
     82 	int			sc_iface_number;	/* interface number */
     83 
     84 	usb_cdc_line_state_t	sc_line_state;	/* current line state */
     85 	u_char			sc_dtr;		/* current DTR state */
     86 	u_char			sc_rts;		/* current RTS state */
     87 
     88 	device_ptr_t		sc_subdev;
     89 
     90 	u_char			sc_dying;
     91 };
     92 
     93 /*
     94  * These are the maximum number of bytes transferred per frame.
     95  * The output buffer size cannot be increased due to the size encoding.
     96  */
     97 #define UPLCOMIBUFSIZE 64;
     98 #define UPLCOMOBUFSIZE 64;
     99 
    100 Static	usbd_status uplcom_init(struct uplcom_softc *);
    101 Static	usbd_status uplcom_set_line_coding(struct uplcom_softc *sc,
    102 							usb_cdc_line_state_t *state);
    103 
    104 Static	void	uplcom_set(void *, int, int, int);
    105 Static	void	uplcom_dtr(struct uplcom_softc *, int);
    106 Static	void	uplcom_rts(struct uplcom_softc *, int);
    107 #if 0
    108 Static	void	uplcom_break(struct uplcom_softc *, int);
    109 #endif
    110 Static	void	uplcom_set_line_state(struct uplcom_softc *);
    111 Static	int	uplcom_param(void *, int, struct termios *);
    112 
    113 struct	ucom_methods uplcom_methods = {
    114 	NULL,
    115 	uplcom_set,
    116 	uplcom_param,
    117 	NULL,
    118 	NULL,
    119 	NULL,
    120 	NULL,
    121 	NULL,
    122 };
    123 
    124 static const struct uplcom_product {
    125 	uint16_t	vendor;
    126 	uint16_t	product;
    127 } uplcom_products [] = {
    128 	/* I/O DATA USB-RSAQ2 */
    129 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303 },
    130 
    131 	{ 0, 0 }
    132 };
    133 
    134 USB_DECLARE_DRIVER(uplcom);
    135 
    136 USB_MATCH(uplcom)
    137 {
    138 	USB_MATCH_START(uplcom, uaa);
    139 	int i;
    140 
    141 
    142 	if (uaa->iface == NULL)
    143 		return (UMATCH_NONE);
    144 
    145 	for (i = 0; uplcom_products[i].vendor != 0; i++) {
    146 		if (uplcom_products[i].vendor == uaa->vendor &&
    147  		    uplcom_products[i].product == uaa->product) {
    148 			return (UMATCH_VENDOR_PRODUCT);
    149 		}
    150 	}
    151 	return (UMATCH_NONE);
    152 }
    153 
    154 USB_ATTACH(uplcom)
    155 {
    156 	USB_ATTACH_START(uplcom, sc, uaa);
    157 	usbd_device_handle dev = uaa->device;
    158 	usbd_interface_handle iface;
    159 	usb_interface_descriptor_t *id;
    160 	usb_endpoint_descriptor_t *ed;
    161 
    162 	char devinfo[1024];
    163 	char *devname = USBDEVNAME(sc->sc_dev);
    164 	usbd_status err;
    165 	int i;
    166 	struct ucom_attach_args uca;
    167 
    168 	DPRINTF(("\n\nuplcom attach: sc=%p\n", sc));
    169 
    170 	err = usbd_set_config_index(dev, UPLCOM_CONFIG_INDEX, 1);
    171 	if (err) {
    172 		printf("\n%s: failed to set configuration, err=%s\n",
    173 			devname, usbd_errstr(err));
    174 		USB_ATTACH_ERROR_RETURN;
    175 	}
    176 
    177 	err = usbd_device2interface_handle(dev, UPLCOM_IFACE_INDEX, &iface);
    178 	if (err) {
    179 		printf("\n%s: failed to get interface, err=%s\n",
    180 			devname, usbd_errstr(err));
    181 		USB_ATTACH_ERROR_RETURN;
    182 	}
    183 
    184 	usbd_devinfo(dev, 0, devinfo);
    185 	USB_ATTACH_SETUP;
    186 	printf("%s: %s\n", devname, devinfo);
    187 
    188 	id = usbd_get_interface_descriptor(iface);
    189 
    190 	sc->sc_udev = dev;
    191 	sc->sc_iface = iface;
    192 
    193 	printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
    194 		devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
    195 	sc->sc_iface_number = id->bInterfaceNumber;
    196 
    197 	uca.bulkin = uca.bulkout = -1;
    198 	for (i = 0; i < id->bNumEndpoints; i++) {
    199 		ed = usbd_interface2endpoint_descriptor(iface, i);
    200 		if (ed == NULL) {
    201 			printf("%s: no endpoint descriptor for %d\n",
    202 				USBDEVNAME(sc->sc_dev), i);
    203 			USB_ATTACH_ERROR_RETURN;
    204 		}
    205 
    206 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    207 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    208 			uca.bulkin = ed->bEndpointAddress;
    209 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    210 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    211 			DPRINTF(("interrupu endpoint addr = 0x%x\n", ed->bEndpointAddress));
    212 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    213 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    214 			uca.bulkout = ed->bEndpointAddress;
    215 		}
    216 	}
    217 
    218 	if (uca.bulkin == -1) {
    219 		printf("%s: Could not find data bulk in\n",
    220 			USBDEVNAME(sc->sc_dev));
    221 		USB_ATTACH_ERROR_RETURN;
    222 	}
    223 
    224 	if (uca.bulkout == -1) {
    225 		printf("%s: Could not find data bulk out\n",
    226 			USBDEVNAME(sc->sc_dev));
    227 		USB_ATTACH_ERROR_RETURN;
    228 	}
    229 
    230 	sc->sc_dtr = -1;
    231 	uca.portno = UCOM_UNK_PORTNO;
    232 	/* bulkin, bulkout set above */
    233 	uca.ibufsize = UPLCOMIBUFSIZE;
    234 	uca.obufsize = UPLCOMOBUFSIZE;
    235 	uca.ibufsizepad = UPLCOMIBUFSIZE;
    236 	uca.opkthdrlen = 1;
    237 	uca.device = dev;
    238 	uca.iface = iface;
    239 	uca.methods = &uplcom_methods;
    240 	uca.arg = sc;
    241 
    242 	err = uplcom_init(sc);
    243 	if (err) {
    244 		printf("%s: init failed, %s\n", USBDEVNAME(sc->sc_dev),
    245 			usbd_errstr(err));
    246 		USB_ATTACH_ERROR_RETURN;
    247 	}
    248 
    249 	DPRINTF(("uplcom: 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 
    255 USB_DETACH(uplcom)
    256 {
    257 	USB_DETACH_START(uplcom, sc);
    258 	int rv = 0;
    259 
    260 	DPRINTF(("uplcom_detach: sc=%p flags=%d\n", sc, flags));
    261 	sc->sc_dying = 1;
    262 	if (sc->sc_subdev != NULL)
    263 		rv = config_detach(sc->sc_subdev, flags);
    264 
    265 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    266 			USBDEV(sc->sc_dev));
    267 
    268 	return (rv);
    269 }
    270 
    271 int
    272 uplcom_activate(device_ptr_t self, enum devact act)
    273 {
    274 	struct uplcom_softc *sc = (struct uplcom_softc *)self;
    275 	int rv = 0;
    276 
    277 	switch (act) {
    278 	case DVACT_ACTIVATE:
    279 		return (EOPNOTSUPP);
    280 		break;
    281 
    282 	case DVACT_DEACTIVATE:
    283 		if (sc->sc_subdev != NULL)
    284 			rv = config_deactivate(sc->sc_subdev);
    285 		sc->sc_dying = 1;
    286 		break;
    287 	}
    288 	return (rv);
    289 }
    290 
    291 
    292 usbd_status
    293 uplcom_init(struct uplcom_softc *sc)
    294 {
    295         usb_device_request_t req;
    296 	usbd_status err;
    297 
    298         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    299         req.bRequest = UPLCOM_RESET;
    300         USETW(req.wValue, UPLCOM_RESET);
    301         USETW(req.wIndex, sc->sc_iface_number);
    302         USETW(req.wLength, 0);
    303 
    304         err = usbd_do_request(sc->sc_udev, &req, 0);
    305 	if (err)
    306 		return (EIO);
    307 
    308 	return (0);
    309 }
    310 
    311 void
    312 uplcom_set_line_state(struct uplcom_softc *sc)
    313 {
    314 	usb_device_request_t req;
    315 	int ls;
    316 
    317 	ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
    318 		(sc->sc_rts ? UCDC_LINE_RTS : 0);
    319 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    320 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
    321 	USETW(req.wValue, ls);
    322 	USETW(req.wIndex, sc->sc_iface_number);
    323 	USETW(req.wLength, 0);
    324 
    325 	(void)usbd_do_request(sc->sc_udev, &req, 0);
    326 
    327 }
    328 
    329 void
    330 uplcom_set(void *addr, int portno, int reg, int onoff)
    331 {
    332 	struct uplcom_softc *sc = addr;
    333 
    334 	switch (reg) {
    335 	case UCOM_SET_DTR:
    336 		uplcom_dtr(sc, onoff);
    337 		break;
    338 	case UCOM_SET_RTS:
    339 		uplcom_rts(sc, onoff);
    340 		break;
    341 	case UCOM_SET_BREAK:
    342 #if 0
    343 		uplcom_break(sc, onoff);
    344 #endif
    345 		break;
    346 	default:
    347 		break;
    348 	}
    349 }
    350 
    351 void
    352 uplcom_dtr(struct uplcom_softc *sc, int onoff)
    353 {
    354 
    355 	DPRINTF(("uplcom: onoff=%d\n", onoff));
    356 
    357 	if (sc->sc_dtr == onoff)
    358 		return;
    359 	sc->sc_dtr = onoff;
    360 
    361 	uplcom_set_line_state(sc);
    362 }
    363 
    364 void
    365 uplcom_rts(struct uplcom_softc *sc, int onoff)
    366 {
    367 
    368 	DPRINTF(("uplcom_rts: onoff=%d\n", onoff));
    369 
    370 	if (sc->sc_rts == onoff)
    371 		return;
    372 	sc->sc_rts = onoff;
    373 
    374 	uplcom_set_line_state(sc);
    375 }
    376 
    377 usbd_status
    378 uplcom_set_line_coding(struct uplcom_softc *sc, usb_cdc_line_state_t *state)
    379 {
    380 	usb_device_request_t req;
    381 	usbd_status err;
    382 
    383 	DPRINTF(("uplcom_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
    384 		UGETDW(state->dwDTERate), state->bCharFormat,
    385 		state->bParityType, state->bDataBits));
    386 
    387 	if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
    388 		DPRINTF(("uplcom_set_line_coding: already set\n"));
    389 		return (USBD_NORMAL_COMPLETION);
    390 	}
    391 
    392 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    393 	req.bRequest = UCDC_SET_LINE_CODING;
    394 	USETW(req.wValue, 0);
    395 	USETW(req.wIndex, sc->sc_iface_number);
    396 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
    397 
    398 	err = usbd_do_request(sc->sc_udev, &req, state);
    399 	if (err) {
    400 		DPRINTF(("uplcom_set_line_coding: failed, err=%s\n",
    401 			usbd_errstr(err)));
    402 		return (err);
    403 	}
    404 
    405 	sc->sc_line_state = *state;
    406 
    407 	return (USBD_NORMAL_COMPLETION);
    408 }
    409 
    410 int
    411 uplcom_param(void *addr, int portno, struct termios *t)
    412 {
    413 	struct uplcom_softc *sc = addr;
    414 	usbd_status err;
    415 	usb_cdc_line_state_t ls;
    416 
    417 	DPRINTF(("uplcom_param: sc=%p\n", sc));
    418 
    419 	USETDW(ls.dwDTERate, t->c_ospeed);
    420 	if (ISSET(t->c_cflag, CSTOPB))
    421 		ls.bCharFormat = UCDC_STOP_BIT_2;
    422 	else
    423 		ls.bCharFormat = UCDC_STOP_BIT_1;
    424 	if (ISSET(t->c_cflag, PARENB)) {
    425 		if (ISSET(t->c_cflag, PARODD))
    426 			ls.bParityType = UCDC_PARITY_ODD;
    427 		else
    428 			ls.bParityType = UCDC_PARITY_EVEN;
    429 	} else
    430 		ls.bParityType = UCDC_PARITY_NONE;
    431 	switch (ISSET(t->c_cflag, CSIZE)) {
    432 	case CS5:
    433 		ls.bDataBits = 5;
    434 		break;
    435 	case CS6:
    436 		ls.bDataBits = 6;
    437 		break;
    438 	case CS7:
    439 		ls.bDataBits = 7;
    440 		break;
    441 	case CS8:
    442 		ls.bDataBits = 8;
    443 		break;
    444 	}
    445 
    446 	err = uplcom_set_line_coding(sc, &ls);
    447 	if (err) {
    448 		DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err)));
    449 		return (EIO);
    450 	}
    451 	return (0);
    452 }
    453