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