Home | History | Annotate | Line # | Download | only in usb
uplcom.c revision 1.6
      1 /*	$NetBSD: uplcom.c,v 1.6 2001/01/23 13:48:06 augustss 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.nisseisg.co.jp/jyouhou/_cp/@gif/2303.pdf
     41  * 	(english)
     42  *
     43  */
     44 
     45 #include <sys/param.h>
     46 #include <sys/systm.h>
     47 #include <sys/kernel.h>
     48 #include <sys/ioctl.h>
     49 #include <sys/conf.h>
     50 #include <sys/tty.h>
     51 #include <sys/file.h>
     52 #include <sys/select.h>
     53 #include <sys/proc.h>
     54 #include <sys/vnode.h>
     55 #include <sys/device.h>
     56 #include <sys/poll.h>
     57 
     58 #include <dev/usb/usb.h>
     59 #include <dev/usb/usbcdc.h>
     60 
     61 #include <dev/usb/usbdi.h>
     62 #include <dev/usb/usbdi_util.h>
     63 #include <dev/usb/usbdevs.h>
     64 #include <dev/usb/usb_quirks.h>
     65 
     66 #include <dev/usb/usbdevs.h>
     67 #include <dev/usb/ucomvar.h>
     68 
     69 #ifdef UPLCOM_DEBUG
     70 #define DPRINTFN(n, x)  if (uplcomdebug > (n)) logprintf x
     71 int	uplcomdebug = 0xff;
     72 #else
     73 #define DPRINTFN(n, x)
     74 #endif
     75 #define DPRINTF(x) DPRINTFN(0, x)
     76 
     77 #define	UPLCOM_CONFIG_INDEX	0
     78 #define UPLCOM_IFACE_INDEX	0
     79 #define	UPLCOM_RESET		0
     80 
     81 struct	uplcom_softc {
     82 	USBBASEDEVICE		sc_dev;		/* base device */
     83 	usbd_device_handle	sc_udev;	/* USB device */
     84 	usbd_interface_handle	sc_iface;	/* interface */
     85 	int			sc_iface_number;	/* interface number */
     86 
     87 	usb_cdc_line_state_t	sc_line_state;	/* current line state */
     88 	u_char			sc_dtr;		/* current DTR state */
     89 	u_char			sc_rts;		/* current RTS state */
     90 
     91 	device_ptr_t		sc_subdev;
     92 
     93 	u_char			sc_dying;
     94 };
     95 
     96 /*
     97  * These are the maximum number of bytes transferred per frame.
     98  * The output buffer size cannot be increased due to the size encoding.
     99  */
    100 #define UPLCOMIBUFSIZE 256
    101 #define UPLCOMOBUFSIZE 256
    102 
    103 Static	usbd_status uplcom_init(struct uplcom_softc *);
    104 Static	usbd_status uplcom_set_line_coding(struct uplcom_softc *sc,
    105 					   usb_cdc_line_state_t *state);
    106 
    107 Static	void	uplcom_set(void *, int, int, int);
    108 Static	void	uplcom_dtr(struct uplcom_softc *, int);
    109 Static	void	uplcom_rts(struct uplcom_softc *, int);
    110 Static	void	uplcom_break(struct uplcom_softc *, int);
    111 Static	void	uplcom_set_line_state(struct uplcom_softc *);
    112 Static	int	uplcom_param(void *, int, struct termios *);
    113 
    114 struct	ucom_methods uplcom_methods = {
    115 	NULL,
    116 	uplcom_set,
    117 	uplcom_param,
    118 	NULL,
    119 	NULL,
    120 	NULL,
    121 	NULL,
    122 	NULL,
    123 };
    124 
    125 static const struct uplcom_product {
    126 	uint16_t	vendor;
    127 	uint16_t	product;
    128 } uplcom_products [] = {
    129 	/* I/O DATA USB-RSAQ2 */
    130 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2303 },
    131 
    132 	{ 0, 0 }
    133 };
    134 
    135 USB_DECLARE_DRIVER(uplcom);
    136 
    137 USB_MATCH(uplcom)
    138 {
    139 	USB_MATCH_START(uplcom, uaa);
    140 	int i;
    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 	/* Move the device into the configured state. */
    171 	err = usbd_set_config_index(dev, UPLCOM_CONFIG_INDEX, 1);
    172 	if (err) {
    173 		printf("\n%s: failed to set configuration, err=%s\n",
    174 			devname, usbd_errstr(err));
    175 		USB_ATTACH_ERROR_RETURN;
    176 	}
    177 
    178 	err = usbd_device2interface_handle(dev, UPLCOM_IFACE_INDEX, &iface);
    179 	if (err) {
    180 		printf("\n%s: failed to get interface, err=%s\n",
    181 			devname, usbd_errstr(err));
    182 		USB_ATTACH_ERROR_RETURN;
    183 	}
    184 
    185 	usbd_devinfo(dev, 0, devinfo);
    186 	USB_ATTACH_SETUP;
    187 	printf("%s: %s\n", devname, devinfo);
    188 
    189 	id = usbd_get_interface_descriptor(iface);
    190 
    191 	sc->sc_udev = dev;
    192 	sc->sc_iface = iface;
    193 
    194 	sc->sc_iface_number = id->bInterfaceNumber;
    195 
    196 	uca.bulkin = uca.bulkout = -1;
    197 	for (i = 0; i < id->bNumEndpoints; i++) {
    198 		ed = usbd_interface2endpoint_descriptor(iface, i);
    199 		if (ed == NULL) {
    200 			printf("%s: no endpoint descriptor for %d\n",
    201 				USBDEVNAME(sc->sc_dev), i);
    202 			USB_ATTACH_ERROR_RETURN;
    203 		}
    204 
    205 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    206 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    207 			uca.bulkin = ed->bEndpointAddress;
    208 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    209 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
    210 			DPRINTF(("interrupt endpoint addr = 0x%x\n",
    211 				 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 = 0;
    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 		sc->sc_subdev = NULL;
    265 	}
    266 
    267 	return (rv);
    268 }
    269 
    270 int
    271 uplcom_activate(device_ptr_t self, enum devact act)
    272 {
    273 	struct uplcom_softc *sc = (struct uplcom_softc *)self;
    274 	int rv = 0;
    275 
    276 	switch (act) {
    277 	case DVACT_ACTIVATE:
    278 		return (EOPNOTSUPP);
    279 		break;
    280 
    281 	case DVACT_DEACTIVATE:
    282 		if (sc->sc_subdev != NULL)
    283 			rv = config_deactivate(sc->sc_subdev);
    284 		sc->sc_dying = 1;
    285 		break;
    286 	}
    287 	return (rv);
    288 }
    289 
    290 
    291 usbd_status
    292 uplcom_init(struct uplcom_softc *sc)
    293 {
    294         usb_device_request_t req;
    295 	usbd_status err;
    296 
    297         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    298         req.bRequest = UPLCOM_RESET;
    299         USETW(req.wValue, UPLCOM_RESET);
    300         USETW(req.wIndex, sc->sc_iface_number);
    301         USETW(req.wLength, 0);
    302 
    303         err = usbd_do_request(sc->sc_udev, &req, 0);
    304 	if (err)
    305 		return (EIO);
    306 
    307 	return (0);
    308 }
    309 
    310 void
    311 uplcom_set_line_state(struct uplcom_softc *sc)
    312 {
    313 	usb_device_request_t req;
    314 	int ls;
    315 
    316 	ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
    317 		(sc->sc_rts ? UCDC_LINE_RTS : 0);
    318 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    319 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
    320 	USETW(req.wValue, ls);
    321 	USETW(req.wIndex, sc->sc_iface_number);
    322 	USETW(req.wLength, 0);
    323 
    324 	(void)usbd_do_request(sc->sc_udev, &req, 0);
    325 
    326 }
    327 
    328 void
    329 uplcom_set(void *addr, int portno, int reg, int onoff)
    330 {
    331 	struct uplcom_softc *sc = addr;
    332 
    333 	switch (reg) {
    334 	case UCOM_SET_DTR:
    335 		uplcom_dtr(sc, onoff);
    336 		break;
    337 	case UCOM_SET_RTS:
    338 		uplcom_rts(sc, onoff);
    339 		break;
    340 	case UCOM_SET_BREAK:
    341 		uplcom_break(sc, onoff);
    342 		break;
    343 	default:
    344 		break;
    345 	}
    346 }
    347 
    348 void
    349 uplcom_dtr(struct uplcom_softc *sc, int onoff)
    350 {
    351 
    352 	DPRINTF(("uplcom: onoff=%d\n", onoff));
    353 
    354 	if (sc->sc_dtr == onoff)
    355 		return;
    356 	sc->sc_dtr = onoff;
    357 
    358 	uplcom_set_line_state(sc);
    359 }
    360 
    361 void
    362 uplcom_rts(struct uplcom_softc *sc, int onoff)
    363 {
    364 	DPRINTF(("uplcom_rts: onoff=%d\n", onoff));
    365 
    366 	if (sc->sc_rts == onoff)
    367 		return;
    368 	sc->sc_rts = onoff;
    369 
    370 	uplcom_set_line_state(sc);
    371 }
    372 
    373 void
    374 uplcom_break(struct uplcom_softc *sc, int onoff)
    375 {
    376 	usb_device_request_t req;
    377 
    378 	DPRINTF(("uplcom_break: onoff=%d\n", onoff));
    379 
    380 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    381 	req.bRequest = UCDC_SEND_BREAK;
    382 	USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
    383 	USETW(req.wIndex, sc->sc_iface_number);
    384 	USETW(req.wLength, 0);
    385 
    386 	(void)usbd_do_request(sc->sc_udev, &req, 0);
    387 }
    388 
    389 usbd_status
    390 uplcom_set_line_coding(struct uplcom_softc *sc, usb_cdc_line_state_t *state)
    391 {
    392 	usb_device_request_t req;
    393 	usbd_status err;
    394 
    395 	DPRINTF(("uplcom_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
    396 		UGETDW(state->dwDTERate), state->bCharFormat,
    397 		state->bParityType, state->bDataBits));
    398 
    399 	if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
    400 		DPRINTF(("uplcom_set_line_coding: already set\n"));
    401 		return (USBD_NORMAL_COMPLETION);
    402 	}
    403 
    404 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    405 	req.bRequest = UCDC_SET_LINE_CODING;
    406 	USETW(req.wValue, 0);
    407 	USETW(req.wIndex, sc->sc_iface_number);
    408 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
    409 
    410 	err = usbd_do_request(sc->sc_udev, &req, state);
    411 	if (err) {
    412 		DPRINTF(("uplcom_set_line_coding: failed, err=%s\n",
    413 			usbd_errstr(err)));
    414 		return (err);
    415 	}
    416 
    417 	sc->sc_line_state = *state;
    418 
    419 	return (USBD_NORMAL_COMPLETION);
    420 }
    421 
    422 int
    423 uplcom_param(void *addr, int portno, struct termios *t)
    424 {
    425 	struct uplcom_softc *sc = addr;
    426 	usbd_status err;
    427 	usb_cdc_line_state_t ls;
    428 
    429 	DPRINTF(("uplcom_param: sc=%p\n", sc));
    430 
    431 	USETDW(ls.dwDTERate, t->c_ospeed);
    432 	if (ISSET(t->c_cflag, CSTOPB))
    433 		ls.bCharFormat = UCDC_STOP_BIT_2;
    434 	else
    435 		ls.bCharFormat = UCDC_STOP_BIT_1;
    436 	if (ISSET(t->c_cflag, PARENB)) {
    437 		if (ISSET(t->c_cflag, PARODD))
    438 			ls.bParityType = UCDC_PARITY_ODD;
    439 		else
    440 			ls.bParityType = UCDC_PARITY_EVEN;
    441 	} else
    442 		ls.bParityType = UCDC_PARITY_NONE;
    443 	switch (ISSET(t->c_cflag, CSIZE)) {
    444 	case CS5:
    445 		ls.bDataBits = 5;
    446 		break;
    447 	case CS6:
    448 		ls.bDataBits = 6;
    449 		break;
    450 	case CS7:
    451 		ls.bDataBits = 7;
    452 		break;
    453 	case CS8:
    454 		ls.bDataBits = 8;
    455 		break;
    456 	}
    457 
    458 	err = uplcom_set_line_coding(sc, &ls);
    459 	if (err) {
    460 		DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err)));
    461 		return (EIO);
    462 	}
    463 	return (0);
    464 }
    465