1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28#ifndef _USB_ETHERNET_H_
29#define	_USB_ETHERNET_H_
30
31#include "net/usb_eth_drv.h"
32
33/* FreeBSD net Interface flags */
34#define	IFF_DRV_RUNNING	0x40	/* (d) resources allocated */
35
36#ifndef	MCLSHIFT
37#define	MCLSHIFT	11	/* convert bytes to mbuf clusters */
38#endif					/* MCLSHIFT */
39
40#define	MCLBYTES	(1 << MCLSHIFT)	/* size of an mbuf cluster */
41
42/*
43 * Structure defining a queue for a network interface.
44 */
45struct	ifqueue {
46	struct	pbuf *ifq_head;
47	struct	pbuf *ifq_tail;
48	int	ifq_len;
49};
50
51#define	UE_LOCK(_ue)	mtx_lock((_ue)->ue_mtx)
52#define	UE_UNLOCK(_ue)	mtx_unlock((_ue)->ue_mtx)
53#define	UE_LOCK_ASSERT(_ue, t)	mtx_assert((_ue)->ue_mtx, t)
54
55#define	IF_ENQUEUE(ifq, m) do {				 \
56	if ((ifq)->ifq_tail == NULL)			 \
57		(ifq)->ifq_head = m;				 \
58	else									 \
59		(ifq)->ifq_tail->next = m;			 \
60	(ifq)->ifq_tail = m;					 \
61	(ifq)->ifq_len++;						 \
62} while (0)
63
64#define	IF_DEQUEUE(ifq, m) do {						 \
65	(m) = (ifq)->ifq_head;							 \
66	if (m) {										 \
67		if (((ifq)->ifq_head = (m)->next) == NULL)	 \
68			(ifq)->ifq_tail = NULL;					 \
69		(m)->next = NULL;							 \
70		(ifq)->ifq_len--;							 \
71	}												 \
72} while (0)
73
74#define	IF_PREPEND(ifq, m)						\
75do {											\
76	(m)->next = (ifq)->ifq_head;				\
77	if ((ifq)->ifq_tail == NULL)				\
78		(ifq)->ifq_tail = (m);					\
79	(ifq)->ifq_head = (m);						\
80	(ifq)->ifq_len++;							\
81} while (0)
82
83struct usb_ether;
84struct usb_device_request;
85
86typedef void (uether_fn_t)(struct usb_ether *);
87
88struct usb_ether_methods {
89	uether_fn_t		*ue_attach_post;
90	uether_fn_t		*ue_start;
91	uether_fn_t		*ue_init;
92	uether_fn_t		*ue_stop;
93	uether_fn_t		*ue_setmulti;
94	uether_fn_t		*ue_setpromisc;
95	uether_fn_t		*ue_tick;
96	int				(*ue_attach_post_sub)(struct usb_ether *);
97};
98
99struct usb_ether_cfg_task {
100	struct usb_proc_msg hdr;
101	struct usb_ether *ue;
102};
103
104struct usb_ether {
105	/* NOTE: the "ue_ifp" pointer must be first --hps */
106	struct los_eth_driver 	*ue_drv_sc;
107	EVENT_CB_S 				ue_event;
108	struct mtx 				*ue_mtx;
109	const struct usb_ether_methods *ue_methods;
110	void 					*ue_sc;
111	struct usb_device 		*ue_udev; /* used by uether_do_request() */
112	device_t 				ue_dev;
113	device_t 				ue_miibus;
114
115	struct usb_process	ue_tq;
116	struct ifqueue		ue_rxq;
117	struct ifqueue		ue_txq;
118	struct usb_callout	ue_watchdog;
119	struct usb_ether_cfg_task	ue_sync_task[2];
120	struct usb_ether_cfg_task	ue_media_task[2];
121	struct usb_ether_cfg_task	ue_multi_task[2];
122	struct usb_ether_cfg_task	ue_promisc_task[2];
123	struct usb_ether_cfg_task	ue_tick_task[2];
124
125	int			ue_unit;
126
127	/* ethernet address from eeprom */
128	uint8_t		ue_eaddr[NETIF_MAX_HWADDR_LEN];
129};
130
131#define	uether_do_request(ue,req,data,timo) \
132    usbd_do_request_proc((ue)->ue_udev,&(ue)->ue_tq,req,data,0,NULL,timo)
133
134uint8_t		uether_pause(struct usb_ether *, unsigned int);
135void		*uether_getsc(struct usb_ether *);
136int			uether_ifattach(struct usb_ether *);
137void		uether_ifattach_wait(struct usb_ether *);
138void		uether_ifdetach(struct usb_ether *);
139struct pbuf	*uether_newbuf(int length);
140void		uether_freebuf(struct pbuf *buf);
141int			uether_rxmbuf(struct usb_ether *, struct pbuf *,
142		    unsigned int);
143void		uether_rxflush(struct usb_ether *);
144uint8_t		uether_is_gone(struct usb_ether *);
145#endif					/* _USB_ETHERNET_H_ */
146