]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/usb/usb_ethersubr.c
Use usbd_clear_endpoint_stall_async() when clearing endpoint stalls in
[FreeBSD/FreeBSD.git] / sys / dev / 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
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 /*
37  * Callbacks in the USB code operate at splusb() (actually splbio()
38  * in FreeBSD). However adding packets to the input queues has to be
39  * done at splimp(). It is conceivable that this arrangement could
40  * trigger a condition where the splimp() is ignored and the input
41  * queues could get trampled in spite of our best effors to prevent
42  * it. To work around this, we implement a special input queue for
43  * USB ethernet adapter drivers. Rather than passing the frames directly
44  * to ether_input(), we pass them here, then schedule a soft interrupt
45  * to hand them to ether_input() later, outside of the USB interrupt
46  * context.
47  *
48  * It's questional as to whether this code should be expanded to
49  * handle other kinds of devices, or handle USB transfer callbacks
50  * in general. Right now, I need USB network interfaces to work
51  * properly.
52  */
53
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/sockio.h>
57 #include <sys/mbuf.h>
58 #include <sys/malloc.h>
59 #include <sys/socket.h>
60
61 #include <net/if.h>
62 #include <net/if_types.h>
63 #include <net/if_arp.h>
64 #include <net/ethernet.h>
65 #include <net/netisr.h>
66 #include <net/bpf.h>
67
68 #include <dev/usb/usb.h>
69 #include <dev/usb/usb_ethersubr.h>
70
71 static struct ifqueue usbq_rx;
72 static struct ifqueue usbq_tx;
73 static int mtx_inited = 0;
74
75 static void usbintr             (void);
76
77 static void usbintr(void)
78 {
79         struct mbuf             *m;
80         struct usb_qdat         *q;
81         struct ifnet            *ifp;
82
83         /* Check the RX queue */
84         while(1) {
85                 IF_DEQUEUE(&usbq_rx, m);
86                 if (m == NULL)
87                         break;
88                 q = (struct usb_qdat *)m->m_pkthdr.rcvif;
89                 ifp = q->ifp;
90                 m->m_pkthdr.rcvif = ifp;
91                 (*ifp->if_input)(ifp, m);
92
93                 /* Re-arm the receiver */
94                 (*q->if_rxstart)(ifp);
95                 if (ifp->if_snd.ifq_head != NULL)
96                         (*ifp->if_start)(ifp);
97         }
98
99         /* Check the TX queue */
100         while(1) {
101                 IF_DEQUEUE(&usbq_tx, m);
102                 if (m == NULL)
103                         break;
104                 ifp = m->m_pkthdr.rcvif;
105                 m_freem(m);
106                 if (ifp->if_snd.ifq_head != NULL)
107                         (*ifp->if_start)(ifp);
108         }
109
110         return;
111 }
112
113 void usb_register_netisr()
114 {
115         if (mtx_inited)
116                 return;
117         netisr_register(NETISR_USB, (netisr_t *)usbintr, NULL, 0);
118         mtx_init(&usbq_tx.ifq_mtx, "usbq_tx_mtx", NULL, MTX_DEF);
119         mtx_init(&usbq_rx.ifq_mtx, "usbq_rx_mtx", NULL, MTX_DEF);
120         mtx_inited++;
121         return;
122 }
123
124 /*
125  * Must be called at splusb() (actually splbio()). This should be
126  * the case when called from a transfer callback routine.
127  */
128 void usb_ether_input(m)
129         struct mbuf             *m;
130 {
131         IF_ENQUEUE(&usbq_rx, m);
132         schednetisr(NETISR_USB);
133
134         return;
135 }
136
137 void usb_tx_done(m)
138         struct mbuf             *m;
139 {
140         IF_ENQUEUE(&usbq_tx, m);
141         schednetisr(NETISR_USB);
142
143         return;
144 }
145
146 struct mbuf *
147 usb_ether_newbuf(void)
148 {
149         struct mbuf *m_new;
150
151         m_new = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
152         if (m_new == NULL)
153                 return (NULL);
154         m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
155
156         m_adj(m_new, ETHER_ALIGN);
157         return (m_new);
158 }
159
160 int
161 usb_ether_rx_list_init(void *sc, struct ue_cdata *cd,
162     usbd_device_handle ue_udev)
163 {
164         struct ue_chain *c;
165         int i;
166
167         for (i = 0; i < UE_RX_LIST_CNT; i++) {
168                 c = &cd->ue_rx_chain[i];
169                 c->ue_sc = sc;
170                 c->ue_idx = i;
171                 c->ue_mbuf = usb_ether_newbuf();
172                 if (c->ue_mbuf == NULL)
173                         return (ENOBUFS);
174                 if (c->ue_xfer == NULL) {
175                         c->ue_xfer = usbd_alloc_xfer(ue_udev);
176                         if (c->ue_xfer == NULL)
177                                 return (ENOBUFS);
178                         c->ue_buf = usbd_alloc_buffer(c->ue_xfer, UE_BUFSZ);
179                         if (c->ue_buf == NULL)
180                                 return (ENOBUFS);
181                 }
182         }
183
184         return (0);
185 }
186
187 int
188 usb_ether_tx_list_init(void *sc, struct ue_cdata *cd,
189     usbd_device_handle ue_udev)
190 {
191         struct ue_chain *c;
192         int i;
193
194         for (i = 0; i < UE_TX_LIST_CNT; i++) {
195                 c = &cd->ue_tx_chain[i];
196                 c->ue_sc = sc;
197                 c->ue_idx = i;
198                 c->ue_mbuf = NULL;
199                 if (c->ue_xfer == NULL) {
200                         c->ue_xfer = usbd_alloc_xfer(ue_udev);
201                         if (c->ue_xfer == NULL)
202                                 return (ENOBUFS);
203                         c->ue_buf = usbd_alloc_buffer(c->ue_xfer, UE_BUFSZ);
204                         if (c->ue_buf == NULL)
205                                 return (ENOBUFS);
206                 }
207         }
208
209         return (0);
210 }
211
212 void
213 usb_ether_rx_list_free(struct ue_cdata *cd)
214 {
215         int i;
216
217         for (i = 0; i < UE_RX_LIST_CNT; i++) {
218                 if (cd->ue_rx_chain[i].ue_mbuf != NULL) {
219                         m_freem(cd->ue_rx_chain[i].ue_mbuf);
220                         cd->ue_rx_chain[i].ue_mbuf = NULL;
221                 }
222                 if (cd->ue_rx_chain[i].ue_xfer != NULL) {
223                         usbd_free_xfer(cd->ue_rx_chain[i].ue_xfer);
224                         cd->ue_rx_chain[i].ue_xfer = NULL;
225                 }
226         }
227 }
228
229 void
230 usb_ether_tx_list_free(struct ue_cdata *cd)
231 {
232         int i;
233
234         for (i = 0; i < UE_RX_LIST_CNT; i++) {
235                 if (cd->ue_tx_chain[i].ue_mbuf != NULL) {
236                         m_freem(cd->ue_tx_chain[i].ue_mbuf);
237                         cd->ue_tx_chain[i].ue_mbuf = NULL;
238                 }
239                 if (cd->ue_tx_chain[i].ue_xfer != NULL) {
240                         usbd_free_xfer(cd->ue_tx_chain[i].ue_xfer);
241                         cd->ue_tx_chain[i].ue_xfer = NULL;
242                 }
243         }
244 }