Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / bus / usb / usb_ethersubr.c
1 /*
2  * Copyright (c) 1997, 1998, 1999, 2000
3  *      Bill Paul <wpaul@ee.columbia.edu>.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by Bill Paul.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * $FreeBSD: src/sys/dev/usb/usb_ethersubr.c,v 1.4.2.4 2002/11/06 14:23:20 joe Exp $
33  */
34
35 /*
36  * Callbacks in the USB code operate at splusb() (actually splbio()
37  * in FreeBSD). However adding packets to the input queues has to be
38  * done at splimp(). It is conceivable that this arrangement could
39  * trigger a condition where the splimp() is ignored and the input
40  * queues could get trampled in spite of our best effors to prevent
41  * it. To work around this, we implement a special input queue for
42  * USB ethernet adapter drivers. Rather than passing the frames directly
43  * to ether_input(), we pass them here, then schedule a soft interrupt
44  * to hand them to ether_input() later, outside of the USB interrupt
45  * context.
46  *
47  * It's questional as to whether this code should be expanded to
48  * handle other kinds of devices, or handle USB transfer callbacks
49  * in general. Right now, I need USB network interfaces to work
50  * properly.
51  */
52
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/sockio.h>
56 #include <sys/mbuf.h>
57 #include <sys/malloc.h>
58 #include <sys/socket.h>
59
60 #include <net/if.h>
61 #include <net/if_types.h>
62 #include <net/if_arp.h>
63 #include <net/ethernet.h>
64 #include <net/netisr.h>
65 #include <net/bpf.h>
66
67 #include <dev/usb/usb.h>
68 #include <dev/usb/usb_ethersubr.h>
69
70 #ifndef lint
71 static const char rcsid[] =
72   "$FreeBSD: src/sys/dev/usb/usb_ethersubr.c,v 1.4.2.4 2002/11/06 14:23:20 joe Exp $";
73 #endif
74
75 Static struct ifqueue usbq_rx;
76 Static struct ifqueue usbq_tx;
77
78 Static void usbintr             (void);
79
80 Static void usbintr()
81 {
82         struct ether_header     *eh;
83         struct mbuf             *m;
84         struct usb_qdat         *q;
85         struct ifnet            *ifp;
86         int                     s;
87
88         s = splimp();
89
90         /* Check the RX queue */
91         while(1) {
92                 IF_DEQUEUE(&usbq_rx, m);
93                 if (m == NULL)
94                         break;
95                 eh = mtod(m, struct ether_header *);
96                 q = (struct usb_qdat *)m->m_pkthdr.rcvif;
97                 ifp = q->ifp;
98                 m->m_pkthdr.rcvif = ifp;
99                 m_adj(m, sizeof(struct ether_header));
100                 ether_input(ifp, eh, m);
101
102                 /* Re-arm the receiver */
103                 (*q->if_rxstart)(ifp);
104                 if (ifp->if_snd.ifq_head != NULL)
105                         (*ifp->if_start)(ifp);
106         }
107
108         /* Check the TX queue */
109         while(1) {
110                 IF_DEQUEUE(&usbq_tx, m);
111                 if (m == NULL)
112                         break;
113                 ifp = m->m_pkthdr.rcvif;
114                 m_freem(m);
115                 if (ifp->if_snd.ifq_head != NULL)
116                         (*ifp->if_start)(ifp);
117         }
118
119         splx(s);
120
121         return;
122 }
123
124 void usb_register_netisr()
125 {
126         register_netisr(NETISR_USB, usbintr);
127         return;
128 }
129
130 /*
131  * Must be called at splusp() (actually splbio()). This should be
132  * the case when called from a transfer callback routine.
133  */
134 void usb_ether_input(m)
135         struct mbuf             *m;
136 {
137         int                     s;
138         s = splimp();
139         IF_ENQUEUE(&usbq_rx, m);
140         schednetisr(NETISR_USB);
141         splx(s);
142         return;
143 }
144
145 void usb_tx_done(m)
146         struct mbuf             *m;
147 {
148         int                     s;
149         s = splimp();
150         IF_ENQUEUE(&usbq_tx, m);
151         schednetisr(NETISR_USB);
152         splx(s);
153         return;
154 }