Home | History | Annotate | Line # | Download | only in usb
if_upl.c revision 1.64.2.2
      1  1.64.2.2    martin /*	$NetBSD: if_upl.c,v 1.64.2.2 2019/09/13 06:51:58 martin Exp $	*/
      2  1.64.2.1    martin 
      3       1.1  augustss /*
      4       1.1  augustss  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5       1.1  augustss  * All rights reserved.
      6       1.1  augustss  *
      7       1.1  augustss  * This code is derived from software contributed to The NetBSD Foundation
      8       1.3  augustss  * by Lennart Augustsson (lennart (at) augustsson.net) at
      9       1.1  augustss  * Carlstedt Research & Technology.
     10       1.1  augustss  *
     11       1.1  augustss  * Redistribution and use in source and binary forms, with or without
     12       1.1  augustss  * modification, are permitted provided that the following conditions
     13       1.1  augustss  * are met:
     14       1.1  augustss  * 1. Redistributions of source code must retain the above copyright
     15       1.1  augustss  *    notice, this list of conditions and the following disclaimer.
     16       1.1  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     17       1.1  augustss  *    notice, this list of conditions and the following disclaimer in the
     18       1.1  augustss  *    documentation and/or other materials provided with the distribution.
     19       1.1  augustss  *
     20       1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21       1.1  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22       1.1  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23       1.1  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24       1.1  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25       1.1  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26       1.1  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27       1.1  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28       1.1  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29       1.1  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30       1.1  augustss  * POSSIBILITY OF SUCH DAMAGE.
     31       1.1  augustss  */
     32       1.1  augustss 
     33       1.1  augustss /*
     34       1.1  augustss  * Prolific PL2301/PL2302 driver
     35       1.1  augustss  */
     36      1.16     lukem 
     37      1.16     lukem #include <sys/cdefs.h>
     38  1.64.2.2    martin __KERNEL_RCSID(0, "$NetBSD: if_upl.c,v 1.64.2.2 2019/09/13 06:51:58 martin Exp $");
     39       1.1  augustss 
     40      1.44  christos #ifdef _KERNEL_OPT
     41       1.1  augustss #include "opt_inet.h"
     42      1.57     skrll #include "opt_usb.h"
     43      1.44  christos #endif
     44       1.1  augustss 
     45       1.1  augustss #include <sys/param.h>
     46       1.1  augustss 
     47  1.64.2.1    martin #include <dev/usb/usbnet.h>
     48       1.1  augustss 
     49       1.1  augustss #include <net/if_types.h>
     50       1.1  augustss 
     51       1.1  augustss #ifdef INET
     52      1.19  augustss #include <netinet/in.h>
     53      1.19  augustss #include <netinet/in_var.h>
     54       1.1  augustss #include <netinet/if_inarp.h>
     55       1.1  augustss #endif
     56       1.1  augustss 
     57       1.1  augustss /*
     58       1.1  augustss  * 7  6  5  4  3  2  1  0
     59       1.6  augustss  * tx rx 1  0
     60       1.1  augustss  * 1110 0000 rxdata
     61       1.1  augustss  * 1010 0000 idle
     62       1.1  augustss  * 0010 0000 tx over
     63       1.1  augustss  * 0110      tx over + rxd
     64       1.1  augustss  */
     65       1.1  augustss 
     66       1.1  augustss #define UPL_RXDATA		0x40
     67       1.1  augustss #define UPL_TXOK		0x80
     68       1.1  augustss 
     69       1.1  augustss #define UPL_CONFIG_NO		1
     70       1.1  augustss #define UPL_IFACE_IDX		0
     71       1.1  augustss 
     72       1.1  augustss /***/
     73       1.1  augustss 
     74       1.1  augustss #define UPL_INTR_INTERVAL	20
     75       1.1  augustss 
     76       1.1  augustss #define UPL_BUFSZ		1024
     77       1.1  augustss 
     78       1.1  augustss #define UPL_RX_LIST_CNT		1
     79       1.1  augustss #define UPL_TX_LIST_CNT		1
     80       1.1  augustss 
     81       1.1  augustss #ifdef UPL_DEBUG
     82      1.38    dyoung #define DPRINTF(x)	if (upldebug) printf x
     83      1.38    dyoung #define DPRINTFN(n,x)	if (upldebug >= (n)) printf x
     84       1.1  augustss int	upldebug = 0;
     85       1.1  augustss #else
     86       1.1  augustss #define DPRINTF(x)
     87       1.1  augustss #define DPRINTFN(n,x)
     88       1.1  augustss #endif
     89       1.1  augustss 
     90       1.1  augustss /*
     91       1.1  augustss  * Various supported device vendors/products.
     92       1.1  augustss  */
     93  1.64.2.1    martin static struct usb_devno sc_devs[] = {
     94       1.1  augustss 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2301 },
     95       1.1  augustss 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL2302 },
     96  1.64.2.1    martin 	{ USB_VENDOR_PROLIFIC, USB_PRODUCT_PROLIFIC_PL25A1 },
     97  1.64.2.1    martin 	{ USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F5U258 },
     98  1.64.2.2    martin 	{ USB_VENDOR_NI, USB_PRODUCT_NI_HTOH_7825 }
     99       1.1  augustss };
    100       1.1  augustss 
    101      1.56   msaitoh int	upl_match(device_t, cfdata_t, void *);
    102      1.56   msaitoh void	upl_attach(device_t, device_t, void *);
    103      1.63       mrg 
    104  1.64.2.1    martin CFATTACH_DECL_NEW(upl, sizeof(struct usbnet), upl_match, upl_attach,
    105  1.64.2.1    martin     usbnet_detach, usbnet_activate);
    106       1.1  augustss 
    107  1.64.2.1    martin #if 0
    108  1.64.2.1    martin static void upl_intr_cb(struct usbnet *, usbd_status);
    109  1.64.2.1    martin #endif
    110  1.64.2.1    martin static void upl_rx_loop(struct usbnet *, struct usbnet_chain *, uint32_t);
    111  1.64.2.1    martin static unsigned upl_tx_prepare(struct usbnet *, struct mbuf *,
    112  1.64.2.1    martin 			       struct usbnet_chain *);
    113  1.64.2.1    martin static int upl_ioctl_cb(struct ifnet *, u_long, void *);
    114  1.64.2.1    martin static int upl_init(struct ifnet *);
    115  1.64.2.1    martin 
    116  1.64.2.1    martin static struct usbnet_ops upl_ops = {
    117  1.64.2.1    martin 	.uno_init = upl_init,
    118  1.64.2.1    martin 	.uno_tx_prepare = upl_tx_prepare,
    119  1.64.2.1    martin 	.uno_rx_loop = upl_rx_loop,
    120  1.64.2.1    martin 	.uno_ioctl = upl_ioctl_cb,
    121  1.64.2.1    martin #if 0
    122  1.64.2.1    martin 	.uno_intr = upl_intr_cb,
    123  1.64.2.1    martin #endif
    124  1.64.2.1    martin };
    125       1.4  augustss 
    126  1.64.2.1    martin static int upl_output(struct ifnet *, struct mbuf *, const struct sockaddr *,
    127      1.52     ozaki 		      const struct rtentry *);
    128  1.64.2.1    martin static void upl_input(struct ifnet *, struct mbuf *);
    129       1.1  augustss 
    130       1.1  augustss /*
    131       1.1  augustss  * Probe for a Prolific chip.
    132       1.1  augustss  */
    133      1.44  christos int
    134      1.38    dyoung upl_match(device_t parent, cfdata_t match, void *aux)
    135       1.1  augustss {
    136      1.38    dyoung 	struct usb_attach_arg *uaa = aux;
    137       1.1  augustss 
    138  1.64.2.1    martin 	return usb_lookup(sc_devs, uaa->uaa_vendor, uaa->uaa_product) != NULL ?
    139  1.64.2.1    martin 		UMATCH_VENDOR_PRODUCT : UMATCH_NONE;
    140       1.1  augustss }
    141       1.1  augustss 
    142      1.44  christos void
    143      1.38    dyoung upl_attach(device_t parent, device_t self, void *aux)
    144       1.1  augustss {
    145  1.64.2.1    martin 	struct usbnet * const	un = device_private(self);
    146  1.64.2.1    martin 	struct usb_attach_arg	*uaa = aux;
    147      1.22  augustss 	char			*devinfop;
    148      1.51     skrll 	struct usbd_device *	dev = uaa->uaa_device;
    149       1.1  augustss 	usbd_status		err;
    150       1.1  augustss 	usb_interface_descriptor_t	*id;
    151       1.1  augustss 	usb_endpoint_descriptor_t	*ed;
    152       1.1  augustss 	int			i;
    153       1.1  augustss 
    154  1.64.2.1    martin 	DPRINTFN(5,(" : upl_attach: un=%p, dev=%p", un, dev));
    155      1.32      cube 
    156      1.34    plunky 	aprint_naive("\n");
    157      1.34    plunky 	aprint_normal("\n");
    158      1.22  augustss 	devinfop = usbd_devinfo_alloc(dev, 0);
    159      1.32      cube 	aprint_normal_dev(self, "%s\n", devinfop);
    160      1.22  augustss 	usbd_devinfo_free(devinfop);
    161       1.1  augustss 
    162  1.64.2.1    martin 	un->un_dev = self;
    163  1.64.2.1    martin 	un->un_udev = dev;
    164  1.64.2.1    martin 	un->un_sc = un;
    165  1.64.2.1    martin 	un->un_ops = &upl_ops;
    166  1.64.2.1    martin 	un->un_rx_xfer_flags = USBD_SHORT_XFER_OK;
    167  1.64.2.1    martin 	un->un_tx_xfer_flags = USBD_FORCE_SHORT_XFER;
    168  1.64.2.1    martin 	un->un_rx_list_cnt = UPL_RX_LIST_CNT;
    169  1.64.2.1    martin 	un->un_tx_list_cnt = UPL_TX_LIST_CNT;
    170  1.64.2.1    martin 	un->un_rx_bufsz = UPL_BUFSZ;
    171  1.64.2.1    martin 	un->un_tx_bufsz = UPL_BUFSZ;
    172  1.64.2.1    martin 
    173       1.9  augustss 	err = usbd_set_config_no(dev, UPL_CONFIG_NO, 1);
    174       1.1  augustss 	if (err) {
    175      1.43     skrll 		aprint_error_dev(self, "failed to set configuration"
    176      1.43     skrll 		    ", err=%s\n", usbd_errstr(err));
    177      1.38    dyoung 		return;
    178       1.1  augustss 	}
    179       1.1  augustss 
    180  1.64.2.1    martin 	err = usbd_device2interface_handle(dev, UPL_IFACE_IDX, &un->un_iface);
    181       1.1  augustss 	if (err) {
    182      1.32      cube 		aprint_error_dev(self, "getting interface handle failed\n");
    183      1.38    dyoung 		return;
    184       1.1  augustss 	}
    185       1.1  augustss 
    186  1.64.2.1    martin 	id = usbd_get_interface_descriptor(un->un_iface);
    187       1.1  augustss 
    188       1.1  augustss 	/* Find endpoints. */
    189       1.1  augustss 	for (i = 0; i < id->bNumEndpoints; i++) {
    190  1.64.2.1    martin 		ed = usbd_interface2endpoint_descriptor(un->un_iface, i);
    191       1.1  augustss 		if (ed == NULL) {
    192      1.32      cube 			aprint_error_dev(self, "couldn't get ep %d\n", i);
    193      1.38    dyoung 			return;
    194       1.1  augustss 		}
    195       1.1  augustss 		if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
    196       1.1  augustss 		    UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    197  1.64.2.1    martin 			un->un_ed[USBNET_ENDPT_RX] = ed->bEndpointAddress;
    198       1.1  augustss 		} else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
    199       1.1  augustss 			   UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
    200  1.64.2.1    martin 			un->un_ed[USBNET_ENDPT_TX] = ed->bEndpointAddress;
    201       1.1  augustss 		}
    202       1.1  augustss 	}
    203       1.1  augustss 
    204  1.64.2.1    martin 	if (un->un_ed[USBNET_ENDPT_RX] == 0 || un->un_ed[USBNET_ENDPT_TX] == 0 /*||
    205  1.64.2.1    martin 	    un->un_ed[USBNET_ENDPT_INTR] == 0*/) {
    206      1.32      cube 		aprint_error_dev(self, "missing endpoint\n");
    207      1.38    dyoung 		return;
    208       1.1  augustss 	}
    209       1.1  augustss 
    210  1.64.2.1    martin 	usbnet_attach(un, "upldet");
    211       1.1  augustss 
    212       1.1  augustss 	/* Initialize interface info.*/
    213  1.64.2.1    martin 	struct ifnet *ifp = usbnet_ifp(un);
    214       1.1  augustss 	ifp->if_mtu = UPL_BUFSZ;
    215       1.1  augustss 	ifp->if_type = IFT_OTHER;
    216       1.1  augustss 	ifp->if_addrlen = 0;
    217       1.1  augustss 	ifp->if_hdrlen = 0;
    218       1.1  augustss 	ifp->if_output = upl_output;
    219      1.49     ozaki 	ifp->_if_input = upl_input;
    220       1.1  augustss 	ifp->if_baudrate = 12000000;
    221      1.12  augustss 	ifp->if_dlt = DLT_RAW;
    222       1.1  augustss 
    223  1.64.2.1    martin 	usbnet_attach_ifp(un, IFF_POINTOPOINT | IFF_NOARP | IFF_SIMPLEX,
    224  1.64.2.1    martin 	    0, NULL);
    225       1.1  augustss }
    226       1.1  augustss 
    227  1.64.2.1    martin static void
    228  1.64.2.1    martin upl_rx_loop(struct usbnet * un, struct usbnet_chain *c, uint32_t total_len)
    229       1.1  augustss {
    230  1.64.2.1    martin 	usbnet_isowned_rx(un);
    231       1.1  augustss 
    232       1.1  augustss 	DPRINTFN(9,("%s: %s: enter status=%d length=%d\n",
    233  1.64.2.1    martin 		    device_xname(un->un_dev), __func__, status, total_len));
    234       1.1  augustss 
    235  1.64.2.1    martin 	usbnet_input(un, c->unc_buf, total_len);
    236       1.1  augustss }
    237       1.1  augustss 
    238  1.64.2.1    martin static unsigned
    239  1.64.2.1    martin upl_tx_prepare(struct usbnet *un, struct mbuf *m, struct usbnet_chain *c)
    240       1.1  augustss {
    241  1.64.2.1    martin 	int	total_len;
    242       1.1  augustss 
    243  1.64.2.1    martin 	if ((unsigned)m->m_pkthdr.len > un->un_tx_bufsz)
    244  1.64.2.1    martin 		return 0;
    245       1.1  augustss 
    246  1.64.2.1    martin 	m_copydata(m, 0, m->m_pkthdr.len, c->unc_buf);
    247       1.1  augustss 	total_len = m->m_pkthdr.len;
    248       1.1  augustss 
    249       1.1  augustss 	DPRINTFN(10,("%s: %s: total_len=%d\n",
    250  1.64.2.1    martin 		     device_xname(un->un_dev), __func__, total_len));
    251       1.1  augustss 
    252  1.64.2.1    martin 	return total_len;
    253       1.1  augustss }
    254       1.1  augustss 
    255  1.64.2.1    martin static int
    256  1.64.2.1    martin upl_init(struct ifnet *ifp)
    257       1.1  augustss {
    258  1.64.2.1    martin 	struct usbnet * const un = ifp->if_softc;
    259  1.64.2.1    martin 	int rv;
    260       1.1  augustss 
    261  1.64.2.1    martin 	usbnet_lock(un);
    262  1.64.2.1    martin 	if (usbnet_isdying(un))
    263  1.64.2.1    martin 		rv = EIO;
    264  1.64.2.1    martin 	else
    265  1.64.2.1    martin 		rv = usbnet_init_rx_tx(un);
    266  1.64.2.1    martin 	usbnet_unlock(un);
    267       1.1  augustss 
    268  1.64.2.1    martin 	return rv;
    269       1.1  augustss }
    270       1.1  augustss 
    271  1.64.2.1    martin static int
    272  1.64.2.1    martin upl_ioctl_cb(struct ifnet *ifp, u_long cmd, void *data)
    273       1.1  augustss {
    274  1.64.2.1    martin 	if (cmd == SIOCSIFMTU) {
    275  1.64.2.1    martin 		struct ifreq *ifr = data;
    276       1.1  augustss 
    277       1.1  augustss 		if (ifr->ifr_mtu > UPL_BUFSZ)
    278  1.64.2.1    martin 			return EINVAL;
    279      1.51     skrll 	}
    280  1.64.2.1    martin 	return 0;
    281       1.1  augustss }
    282       1.1  augustss 
    283  1.64.2.1    martin static int
    284      1.27    dyoung upl_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
    285      1.52     ozaki     const struct rtentry *rt0)
    286       1.1  augustss {
    287  1.64.2.1    martin 	struct usbnet * const un __unused = ifp->if_softc;
    288       1.1  augustss 
    289  1.64.2.1    martin 	DPRINTFN(10,("%s: %s: enter\n", device_xname(un->un_dev), __func__));
    290       1.1  augustss 
    291  1.64.2.1    martin 	/* If the queueing discipline needs packet classification, do it now. */
    292      1.50  knakahar 	IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family);
    293      1.17    itojun 
    294       1.1  augustss 	/*
    295       1.1  augustss 	 * Queue message on interface, and start output if interface
    296       1.1  augustss 	 * not yet active.
    297       1.1  augustss 	 */
    298  1.64.2.1    martin 	return if_transmit_lock(ifp, m);
    299       1.1  augustss }
    300       1.1  augustss 
    301  1.64.2.1    martin static void
    302       1.4  augustss upl_input(struct ifnet *ifp, struct mbuf *m)
    303       1.1  augustss {
    304      1.21  christos #ifdef INET
    305      1.46     rmind 	size_t pktlen = m->m_len;
    306       1.1  augustss 	int s;
    307       1.1  augustss 
    308      1.14   thorpej 	s = splnet();
    309      1.46     rmind 	if (__predict_false(!pktq_enqueue(ip_pktq, m, 0))) {
    310       1.1  augustss 		ifp->if_iqdrops++;
    311      1.46     rmind 		m_freem(m);
    312      1.46     rmind 	} else {
    313      1.46     rmind 		ifp->if_ipackets++;
    314      1.46     rmind 		ifp->if_ibytes += pktlen;
    315       1.1  augustss 	}
    316       1.1  augustss 	splx(s);
    317      1.21  christos #endif
    318       1.1  augustss }
    319  1.64.2.1    martin 
    320  1.64.2.1    martin #ifdef _MODULE
    321  1.64.2.1    martin #include "ioconf.c"
    322  1.64.2.1    martin #endif
    323  1.64.2.1    martin 
    324  1.64.2.1    martin USBNET_MODULE(upl)
    325