Home | History | Annotate | Line # | Download | only in usb
uplcom.c revision 1.7
      1 /*	$NetBSD: uplcom.c,v 1.7 2001/01/23 14:04:14 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 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
    250 			   USBDEV(sc->sc_dev));
    251 
    252 	DPRINTF(("uplcom: in=0x%x out=0x%x\n", uca.bulkin, uca.bulkout));
    253 	sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch);
    254 
    255 	USB_ATTACH_SUCCESS_RETURN;
    256 }
    257 
    258 USB_DETACH(uplcom)
    259 {
    260 	USB_DETACH_START(uplcom, sc);
    261 	int rv = 0;
    262 
    263 	DPRINTF(("uplcom_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 
    270 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
    271 			   USBDEV(sc->sc_dev));
    272 
    273 	return (rv);
    274 }
    275 
    276 int
    277 uplcom_activate(device_ptr_t self, enum devact act)
    278 {
    279 	struct uplcom_softc *sc = (struct uplcom_softc *)self;
    280 	int rv = 0;
    281 
    282 	switch (act) {
    283 	case DVACT_ACTIVATE:
    284 		return (EOPNOTSUPP);
    285 		break;
    286 
    287 	case DVACT_DEACTIVATE:
    288 		if (sc->sc_subdev != NULL)
    289 			rv = config_deactivate(sc->sc_subdev);
    290 		sc->sc_dying = 1;
    291 		break;
    292 	}
    293 	return (rv);
    294 }
    295 
    296 
    297 usbd_status
    298 uplcom_init(struct uplcom_softc *sc)
    299 {
    300         usb_device_request_t req;
    301 	usbd_status err;
    302 
    303         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    304         req.bRequest = UPLCOM_RESET;
    305         USETW(req.wValue, UPLCOM_RESET);
    306         USETW(req.wIndex, sc->sc_iface_number);
    307         USETW(req.wLength, 0);
    308 
    309         err = usbd_do_request(sc->sc_udev, &req, 0);
    310 	if (err)
    311 		return (EIO);
    312 
    313 	return (0);
    314 }
    315 
    316 void
    317 uplcom_set_line_state(struct uplcom_softc *sc)
    318 {
    319 	usb_device_request_t req;
    320 	int ls;
    321 
    322 	ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
    323 		(sc->sc_rts ? UCDC_LINE_RTS : 0);
    324 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    325 	req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
    326 	USETW(req.wValue, ls);
    327 	USETW(req.wIndex, sc->sc_iface_number);
    328 	USETW(req.wLength, 0);
    329 
    330 	(void)usbd_do_request(sc->sc_udev, &req, 0);
    331 
    332 }
    333 
    334 void
    335 uplcom_set(void *addr, int portno, int reg, int onoff)
    336 {
    337 	struct uplcom_softc *sc = addr;
    338 
    339 	switch (reg) {
    340 	case UCOM_SET_DTR:
    341 		uplcom_dtr(sc, onoff);
    342 		break;
    343 	case UCOM_SET_RTS:
    344 		uplcom_rts(sc, onoff);
    345 		break;
    346 	case UCOM_SET_BREAK:
    347 		uplcom_break(sc, onoff);
    348 		break;
    349 	default:
    350 		break;
    351 	}
    352 }
    353 
    354 void
    355 uplcom_dtr(struct uplcom_softc *sc, int onoff)
    356 {
    357 
    358 	DPRINTF(("uplcom: onoff=%d\n", onoff));
    359 
    360 	if (sc->sc_dtr == onoff)
    361 		return;
    362 	sc->sc_dtr = onoff;
    363 
    364 	uplcom_set_line_state(sc);
    365 }
    366 
    367 void
    368 uplcom_rts(struct uplcom_softc *sc, int onoff)
    369 {
    370 	DPRINTF(("uplcom_rts: onoff=%d\n", onoff));
    371 
    372 	if (sc->sc_rts == onoff)
    373 		return;
    374 	sc->sc_rts = onoff;
    375 
    376 	uplcom_set_line_state(sc);
    377 }
    378 
    379 void
    380 uplcom_break(struct uplcom_softc *sc, int onoff)
    381 {
    382 	usb_device_request_t req;
    383 
    384 	DPRINTF(("uplcom_break: onoff=%d\n", onoff));
    385 
    386 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    387 	req.bRequest = UCDC_SEND_BREAK;
    388 	USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
    389 	USETW(req.wIndex, sc->sc_iface_number);
    390 	USETW(req.wLength, 0);
    391 
    392 	(void)usbd_do_request(sc->sc_udev, &req, 0);
    393 }
    394 
    395 usbd_status
    396 uplcom_set_line_coding(struct uplcom_softc *sc, usb_cdc_line_state_t *state)
    397 {
    398 	usb_device_request_t req;
    399 	usbd_status err;
    400 
    401 	DPRINTF(("uplcom_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
    402 		UGETDW(state->dwDTERate), state->bCharFormat,
    403 		state->bParityType, state->bDataBits));
    404 
    405 	if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
    406 		DPRINTF(("uplcom_set_line_coding: already set\n"));
    407 		return (USBD_NORMAL_COMPLETION);
    408 	}
    409 
    410 	req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
    411 	req.bRequest = UCDC_SET_LINE_CODING;
    412 	USETW(req.wValue, 0);
    413 	USETW(req.wIndex, sc->sc_iface_number);
    414 	USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
    415 
    416 	err = usbd_do_request(sc->sc_udev, &req, state);
    417 	if (err) {
    418 		DPRINTF(("uplcom_set_line_coding: failed, err=%s\n",
    419 			usbd_errstr(err)));
    420 		return (err);
    421 	}
    422 
    423 	sc->sc_line_state = *state;
    424 
    425 	return (USBD_NORMAL_COMPLETION);
    426 }
    427 
    428 int
    429 uplcom_param(void *addr, int portno, struct termios *t)
    430 {
    431 	struct uplcom_softc *sc = addr;
    432 	usbd_status err;
    433 	usb_cdc_line_state_t ls;
    434 
    435 	DPRINTF(("uplcom_param: sc=%p\n", sc));
    436 
    437 	USETDW(ls.dwDTERate, t->c_ospeed);
    438 	if (ISSET(t->c_cflag, CSTOPB))
    439 		ls.bCharFormat = UCDC_STOP_BIT_2;
    440 	else
    441 		ls.bCharFormat = UCDC_STOP_BIT_1;
    442 	if (ISSET(t->c_cflag, PARENB)) {
    443 		if (ISSET(t->c_cflag, PARODD))
    444 			ls.bParityType = UCDC_PARITY_ODD;
    445 		else
    446 			ls.bParityType = UCDC_PARITY_EVEN;
    447 	} else
    448 		ls.bParityType = UCDC_PARITY_NONE;
    449 	switch (ISSET(t->c_cflag, CSIZE)) {
    450 	case CS5:
    451 		ls.bDataBits = 5;
    452 		break;
    453 	case CS6:
    454 		ls.bDataBits = 6;
    455 		break;
    456 	case CS7:
    457 		ls.bDataBits = 7;
    458 		break;
    459 	case CS8:
    460 		ls.bDataBits = 8;
    461 		break;
    462 	}
    463 
    464 	err = uplcom_set_line_coding(sc, &ls);
    465 	if (err) {
    466 		DPRINTF(("uplcom_param: err=%s\n", usbd_errstr(err)));
    467 		return (EIO);
    468 	}
    469 	return (0);
    470 }
    471