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