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