2 * Copyright (c) 1997, 1998, 1999, 2000
3 * Bill Paul <wpaul@ee.columbia.edu>. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
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
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
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/sockio.h>
58 #include <sys/malloc.h>
59 #include <sys/socket.h>
60 #include <sys/taskqueue.h>
63 #include <net/if_types.h>
64 #include <net/if_arp.h>
65 #include <net/ethernet.h>
66 #include <net/netisr.h>
69 #include <dev/usb/usb.h>
70 #include <dev/usb/usb_ethersubr.h>
72 static struct ifqueue usbq_rx;
73 static struct ifqueue usbq_tx;
74 static int mtx_inited = 0;
76 static void usbintr (void);
85 /* Check the RX queue */
87 IF_DEQUEUE(&usbq_rx, m);
90 q = (struct usb_qdat *)m->m_pkthdr.rcvif;
92 m->m_pkthdr.rcvif = ifp;
93 (*ifp->if_input)(ifp, m);
95 /* Re-arm the receiver */
96 (*q->if_rxstart)(ifp);
97 if (ifp->if_snd.ifq_head != NULL)
98 (*ifp->if_start)(ifp);
101 /* Check the TX queue */
103 IF_DEQUEUE(&usbq_tx, m);
106 ifp = m->m_pkthdr.rcvif;
108 if (ifp->if_snd.ifq_head != NULL)
109 (*ifp->if_start)(ifp);
116 usb_register_netisr(void)
120 netisr_register(NETISR_USB, (netisr_t *)usbintr, NULL, 0);
121 mtx_init(&usbq_tx.ifq_mtx, "usbq_tx_mtx", NULL, MTX_DEF);
122 mtx_init(&usbq_rx.ifq_mtx, "usbq_rx_mtx", NULL, MTX_DEF);
128 * Must be called at splusb() (actually splbio()). This should be
129 * the case when called from a transfer callback routine.
135 IF_ENQUEUE(&usbq_rx, m);
136 schednetisr(NETISR_USB);
145 IF_ENQUEUE(&usbq_tx, m);
146 schednetisr(NETISR_USB);
152 usb_ether_newbuf(void)
156 m_new = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
159 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
161 m_adj(m_new, ETHER_ALIGN);
166 usb_ether_rx_list_init(void *sc, struct ue_cdata *cd,
167 usbd_device_handle ue_udev)
172 for (i = 0; i < UE_RX_LIST_CNT; i++) {
173 c = &cd->ue_rx_chain[i];
176 c->ue_mbuf = usb_ether_newbuf();
177 if (c->ue_mbuf == NULL)
179 if (c->ue_xfer == NULL) {
180 c->ue_xfer = usbd_alloc_xfer(ue_udev);
181 if (c->ue_xfer == NULL)
183 c->ue_buf = usbd_alloc_buffer(c->ue_xfer, UE_BUFSZ);
184 if (c->ue_buf == NULL)
193 usb_ether_tx_list_init(void *sc, struct ue_cdata *cd,
194 usbd_device_handle ue_udev)
199 for (i = 0; i < UE_TX_LIST_CNT; i++) {
200 c = &cd->ue_tx_chain[i];
204 if (c->ue_xfer == NULL) {
205 c->ue_xfer = usbd_alloc_xfer(ue_udev);
206 if (c->ue_xfer == NULL)
208 c->ue_buf = usbd_alloc_buffer(c->ue_xfer, UE_BUFSZ);
209 if (c->ue_buf == NULL)
218 usb_ether_rx_list_free(struct ue_cdata *cd)
222 for (i = 0; i < UE_RX_LIST_CNT; i++) {
223 if (cd->ue_rx_chain[i].ue_mbuf != NULL) {
224 m_freem(cd->ue_rx_chain[i].ue_mbuf);
225 cd->ue_rx_chain[i].ue_mbuf = NULL;
227 if (cd->ue_rx_chain[i].ue_xfer != NULL) {
228 usbd_free_xfer(cd->ue_rx_chain[i].ue_xfer);
229 cd->ue_rx_chain[i].ue_xfer = NULL;
235 usb_ether_tx_list_free(struct ue_cdata *cd)
239 for (i = 0; i < UE_RX_LIST_CNT; i++) {
240 if (cd->ue_tx_chain[i].ue_mbuf != NULL) {
241 m_freem(cd->ue_tx_chain[i].ue_mbuf);
242 cd->ue_tx_chain[i].ue_mbuf = NULL;
244 if (cd->ue_tx_chain[i].ue_xfer != NULL) {
245 usbd_free_xfer(cd->ue_tx_chain[i].ue_xfer);
246 cd->ue_tx_chain[i].ue_xfer = NULL;
252 usb_ether_task_init(device_t dev, int flags, struct usb_taskqueue *tq)
255 /* nothing for now. */
259 usb_ether_task_enqueue(struct usb_taskqueue *tq, struct task *task)
262 taskqueue_enqueue(taskqueue_thread, task);
266 usb_ether_task_drain(struct usb_taskqueue *tq, struct task *task)
269 taskqueue_drain(taskqueue_thread, task);
273 usb_ether_task_destroy(struct usb_taskqueue *tq)
276 /* nothing for now */