Home | History | Annotate | Line # | Download | only in usb
uplcom.c revision 1.9
      1 /*	$NetBSD: uplcom.c,v 1.9 2001/01/24 14:46:49 ichiro Exp $	*/
      2 /*
      3  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      4  * All rights reserved.
      5  *
      6  * This code is derived from software contributed to The NetBSD Foundation
      7  * by FUKUHARA ichiro (ichiro (at) ichiro.org).
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *        This product includes software developed by the NetBSD
     20  *        Foundation, Inc. and its contributors.
     21  * 4. Neither the name of The NetBSD Foundation nor the names of its
     22  *    contributors may be used to endorse or promote products derived
     23  *    from this software without specific prior written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     26  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 /*
     39  * Simple datasheet
     40  * http://www.prolific.com.tw/download/DataSheet/pl2303_ds11.PDF
     41  * http://www.nisseisg.co.jp/jyouhou/_cp/@gif/2303.pdf
     42  * 	(english)
     43  *
     44  */
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/kernel.h>
     49 #include <sys/ioctl.h>
     50 #include <sys/conf.h>
     51 #include <sys/tty.h>
     52 #include <sys/file.h>
     53 #include <sys/select.h>
     54 #include <sys/proc.h>
     55 #include <sys/vnode.h>
     56 #include <sys/device.h>
     57 #include <sys/poll.h>
     58 
     59 #include <dev/usb/usb.h>
     60 #include <dev/usb/usbcdc.h>
     61 
     62 #include <dev/usb/usbdi.h>
     63 #include <dev/usb/usbdi_util.h>
     64 #include <dev/usb/usbdevs.h>
     65 #include <dev/usb/usb_quirks.h>
     66 
     67 #include <dev/usb/usbdevs.h>
     68 #include <dev/usb/ucomvar.h>
     69 
     70 #ifdef UPLCOM_DEBUG
     71 #define DPRINTFN(n, x)  if (uplcomdebug > (n)) logprintf x
     72 int	uplcomdebug = 0xff;
     73 #else
     74 #define DPRINTFN(n, x)
     75 #endif
     76 #define DPRINTF(x) DPRINTFN(0, x)
     77 
     78 #define	UPLCOM_CONFIG_INDEX	0
     79 #define UPLCOM_IFACE_INDEX	0
     80 #define	UPLCOM_RESET		0
     81 
     82 struct	uplcom_softc {
     83 	USBBASEDEVICE		sc_dev;		/* base device */
     84 	usbd_device_handle	sc_udev;	/* USB device */
     85 	usbd_interface_handle	sc_iface;	/* interface */
     86 	int			sc_iface_number;	/* interface number */
     87 
     88 	usb_cdc_line_state_t	sc_line_state;	/* current line state */
     89 	u_char			sc_dtr;		/* current DTR state */
     90 	u_char			sc_rts;		/* current RTS state */
     91 
     92 	device_ptr_t		sc_subdev;
     93 
     94 	u_char			sc_dying;
     95 };
     96 
     97 /*
     98  * These are the maximum number of bytes transferred per frame.
     99  * The output buffer size cannot be increased due to the size encoding.
    100  */
    101 #define UPLCOMIBUFSIZE 256
    102 #define UPLCOMOBUFSIZE 256
    103 
    104 Static	usbd_status uplcom_init(struct uplcom_softc *);
    105 Static	usbd_status uplcom_set_line_coding(struct uplcom_softc *sc,
    106 					   usb_cdc_line_state_t *state);
    107 
    108 Static	void	uplcom_set(void *, int, int, int);
    109 Static	void	uplcom_dtr(struct uplcom_softc *, int);
    110 Static	void	uplcom_rts(struct uplcom_softc *, int);
    111 Static	void	uplcom_break(struct uplcom_softc *, int);
    112 Static	void	uplcom_set_line_state(struct uplcom_softc *);
    113 Static	int	uplcom_param(void *, int, struct termios *);
    114 
    115 struct	ucom_methods uplcom_methods = {
    116 	NULL,
    117 	uplcom_set,
    118 	uplcom_param,
    119 	NULL,
    120 	NULL,
    121 	NULL,
    122 	NULL,
    123 	NULL,
    124 };
    125 
    126 static const struct uplcom_product {
    127 	uint16_t	vendor;
    128 	uint16_t	product;
    129 } uplcom_products [] = {
    130 	/* I/O DATA USB-RSAQ2 */
    131 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303 },
    132 
    133 	{ 0, 0 }
    134 };
    135 
    136 USB_DECLARE_DRIVER(uplcom);
    137 
    138 USB_MATCH(uplcom)
    139 {
    140 	USB_MATCH_START(uplcom, uaa);
    141 	int i;
    142 
    143 	if (uaa->iface != NULL)
    144 		return (UMATCH_NONE);
    145 
    146 	for (i = 0; uplcom_products[i].vendor != 0; i++) {
    147 		if (uplcom_products[i].vendor == uaa->vendor &&
    148  		    uplcom_products[i].product == uaa->product) {
    149 			return (UMATCH_VENDOR_PRODUCT);
    150 		}
    151 	}
    152 	return (UMATCH_NONE);
    153 }
    154 
    155 USB_ATTACH(uplcom)
    156 {
    157 	USB_ATTACH_START(uplcom, sc, uaa);
    158 	usbd_device_handle dev = uaa->device;
    159 	usbd_interface_handle iface;
    160 	usb_interface_descriptor_t *id;
    161 	usb_endpoint_descriptor_t *ed;
    162 
    163 	char devinfo[1024];
    164 	char *devname = USBDEVNAME(sc->sc_dev);
    165 	usbd_status err;
    166 	int i;
    167 	struct ucom_attach_args uca;
    168 
    169 	DPRINTF(("\n\nuplcom attach: sc=%p\n", sc));
    170 
    171 	/* Move the device into the configured state. */
    172 	err = usbd_set_config_index(dev, UPLCOM_CONFIG_INDEX, 1);
    173 	if (err) {
    174 		printf("\n%s: failed to set configuration, err=%s\n",
    175 			devname, usbd_errstr(err));
    176 		USB_ATTACH_ERROR_RETURN;
    177 	}
    178 
    179 	err = usbd_device2interface_handle(dev, UPLCOM_IFACE_INDEX, &iface);
    180 	if (err) {
    181 		printf("\n%s: failed to get interface, err=%s\n",
    182 			devname, usbd_errstr(err));
    183 		USB_ATTACH_ERROR_RETURN;
    184 	}
    185 
    186 	usbd_devinfo(dev, 0, devinfo);
    187 	USB_ATTACH_SETUP;
    188 	printf("%s: %s\n", devname, devinfo);
    189 
    190 	id = usbd_get_interface_descriptor(iface);
    191 
    192 	sc->sc_udev = dev;
    193 	sc->sc_iface = iface;
    194 
    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(("interrupt endpoint addr = 0x%x\n",
    212 				 ed->bEndpointAddress));
    213 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    214 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    215 			uca.bulkout = ed->bEndpointAddress;
    216 		}
    217 	}
    218 
    219 	if (uca.bulkin == -1) {
    220 		printf("%s: Could not find data bulk in\n",
    221 			USBDEVNAME(sc->sc_dev));
    222 		USB_ATTACH_ERROR_RETURN;
    223 	}
    224 
    225 	if (uca.bulkout == -1) {
    226 		printf("%s: Could not find data bulk out\n",
    227 			USBDEVNAME(sc->sc_dev));
    228 		USB_ATTACH_ERROR_RETURN;
    229 	}
    230 
    231 	sc->sc_dtr = -1;
    232 	uca.portno = UCOM_UNK_PORTNO;
    233 	/* bulkin, bulkout set above */
    234 	uca.ibufsize = UPLCOMIBUFSIZE;
    235 	uca.obufsize = UPLCOMOBUFSIZE;
    236 	uca.ibufsizepad = UPLCOMIBUFSIZE;
    237 	uca.opkthdrlen = 0;
    238 	uca.device = dev;
    239 	uca.iface = iface;
    240 	uca.methods = &uplcom_methods;
    241 	uca.arg = sc;
    242 	uca.info = NULL;
    243 
    244 	err = uplcom_init(sc);
    245 	if (err) {
    246 		printf("%s: init failed, %s\n", USBDEVNAME(sc->sc_dev),
    247 			usbd_errstr(err));
    248 		USB_ATTACH_ERROR_RETURN;
    249 	}
    250 
    251 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    252 			   USBDEV(sc->sc_dev));
    253 
    254 	DPRINTF(("uplcom: in=0x%x out=0x%x\n", uca.bulkin, uca.bulkout));
    255 	sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch);
    256 
    257 	USB_ATTACH_SUCCESS_RETURN;
    258 }
    259 
    260 USB_DETACH(uplcom)
    261 {
    262 	USB_DETACH_START(uplcom, sc);
    263 	int rv = 0;
    264 
    265 	DPRINTF(("uplcom_detach: sc=%p flags=%d\n", sc, flags));
    266 	sc->sc_dying = 1;
    267 	if (sc->sc_subdev != NULL) {
    268 		rv = config_detach(sc->sc_subdev, flags);
    269 		sc->sc_subdev = NULL;
    270 	}
    271 
    272 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    273 			   USBDEV(sc->sc_dev));
    274 
    275 	return (rv);
    276 }
    277 
    278 int
    279 uplcom_activate(device_ptr_t self, enum devact act)
    280 {
    281 	struct uplcom_softc *sc = (struct uplcom_softc *)self;
    282 	int rv = 0;
    283 
    284 	switch (act) {
    285 	case DVACT_ACTIVATE:
    286 		return (EOPNOTSUPP);
    287 		break;
    288 
    289 	case DVACT_DEACTIVATE:
    290 		if (sc->sc_subdev != NULL)
    291 			rv = config_deactivate(sc->sc_subdev);
    292 		sc->sc_dying = 1;
    293 		break;
    294 	}
    295 	return (rv);
    296 }
    297 
    298 
    299 usbd_status
    300 uplcom_init(struct uplcom_softc *sc)
    301 {
    302         usb_device_request_t req;
    303 	usbd_status err;
    304 
    305         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    306         req.bRequest = UPLCOM_RESET;
    307         USETW(req.wValue, UPLCOM_RESET);
    308         USETW(req.wIndex, sc->sc_iface_number);
    309         USETW(req.wLength, 0);
    310 
    311         err = usbd_do_request(sc->sc_udev, &req, 0);
    312 	if (err)
    313 		return (EIO);
    314 
    315 	return (0);
    316 }
    317 
    318 void
    319 uplcom_set_line_state(struct uplcom_softc *sc)
    320 {
    321 	usb_device_request_t req;
    322 	int ls;
    323 
    324 	ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
    325 		(sc->sc_rts ? UCDC_LINE_RTS : 0);
    326 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    327 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
    328 	USETW(req.wValue, ls);
    329 	USETW(req.wIndex, sc->sc_iface_number);
    330 	USETW(req.wLength, 0);
    331 
    332 	(void)usbd_do_request(sc->sc_udev, &req, 0);
    333 
    334 }
    335 
    336 void
    337 uplcom_set(void *addr, int portno, int reg, int onoff)
    338 {
    339 	struct uplcom_softc *sc = addr;
    340 
    341 	switch (reg) {
    342 	case UCOM_SET_DTR:
    343 		uplcom_dtr(sc, onoff);
    344 		break;
    345 	case UCOM_SET_RTS:
    346 		uplcom_rts(sc, onoff);
    347 		break;
    348 	case UCOM_SET_BREAK:
    349 		uplcom_break(sc, onoff);
    350 		break;
    351 	default:
    352 		break;
    353 	}
    354 }
    355 
    356 void
    357 uplcom_dtr(struct uplcom_softc *sc, int onoff)
    358 {
    359 
    360 	DPRINTF(("uplcom: onoff=%d\n", onoff));
    361 
    362 	if (sc->sc_dtr == onoff)
    363 		return;
    364 	sc->sc_dtr = onoff;
    365 
    366 	uplcom_set_line_state(sc);
    367 }
    368 
    369 void
    370 uplcom_rts(struct uplcom_softc *sc, int onoff)
    371 {
    372 	DPRINTF(("uplcom_rts: onoff=%d\n", onoff));
    373 
    374 	if (sc->sc_rts == onoff)
    375 		return;
    376 	sc->sc_rts = onoff;
    377 
    378 	uplcom_set_line_state(sc);
    379 }
    380 
    381 void
    382 uplcom_break(struct uplcom_softc *sc, int onoff)
    383 {
    384 	usb_device_request_t req;
    385 
    386 	DPRINTF(("uplcom_break: onoff=%d\n", onoff));
    387 
    388 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    389 	req.bRequest = UCDC_SEND_BREAK;
    390 	USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
    391 	USETW(req.wIndex, sc->sc_iface_number);
    392 	USETW(req.wLength, 0);
    393 
    394 	(void)usbd_do_request(sc->sc_udev, &req, 0);
    395 }
    396 
    397 usbd_status
    398 uplcom_set_line_coding(struct uplcom_softc *sc, usb_cdc_line_state_t *state)
    399 {
    400 	usb_device_request_t req;
    401 	usbd_status err;
    402 
    403 	DPRINTF(("uplcom_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
    404 		UGETDW(state->dwDTERate), state->bCharFormat,
    405 		state->bParityType, state->bDataBits));
    406 
    407 	if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
    408 		DPRINTF(("uplcom_set_line_coding: already set\n"));
    409 		return (USBD_NORMAL_COMPLETION);
    410 	}
    411 
    412 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    413 	req.bRequest = UCDC_SET_LINE_CODING;
    414 	USETW(req.wValue, 0);
    415 	USETW(req.wIndex, sc->sc_iface_number);
    416 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
    417 
    418 	err = usbd_do_request(sc->sc_udev, &req, state);
    419 	if (err) {
    420 		DPRINTF(("uplcom_set_line_coding: failed, err=%s\n",
    421 			usbd_errstr(err)));
    422 		return (err);
    423 	}
    424 
    425 	sc->sc_line_state = *state;
    426 
    427 	return (USBD_NORMAL_COMPLETION);
    428 }
    429 
    430 int
    431 uplcom_param(void *addr, int portno, struct termios *t)
    432 {
    433 	struct uplcom_softc *sc = addr;
    434 	usbd_status err;
    435 	usb_cdc_line_state_t ls;
    436 
    437 	DPRINTF(("uplcom_param: sc=%p\n", sc));
    438 
    439 	USETDW(ls.dwDTERate, t->c_ospeed);
    440 	if (ISSET(t->c_cflag, CSTOPB))
    441 		ls.bCharFormat = UCDC_STOP_BIT_2;
    442 	else
    443 		ls.bCharFormat = UCDC_STOP_BIT_1;
    444 	if (ISSET(t->c_cflag, PARENB)) {
    445 		if (ISSET(t->c_cflag, PARODD))
    446 			ls.bParityType = UCDC_PARITY_ODD;
    447 		else
    448 			ls.bParityType = UCDC_PARITY_EVEN;
    449 	} else
    450 		ls.bParityType = UCDC_PARITY_NONE;
    451 	switch (ISSET(t->c_cflag, CSIZE)) {
    452 	case CS5:
    453 		ls.bDataBits = 5;
    454 		break;
    455 	case CS6:
    456 		ls.bDataBits = 6;
    457 		break;
    458 	case CS7:
    459 		ls.bDataBits = 7;
    460 		break;
    461 	case CS8:
    462 		ls.bDataBits = 8;
    463 		break;
    464 	}
    465 
    466 	err = uplcom_set_line_coding(sc, &ls);
    467 	if (err) {
    468 		DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err)));
    469 		return (EIO);
    470 	}
    471 	return (0);
    472 }
    473