LibXenon
Bare-metal Xbox 360 homebrew library
Loading...
Searching...
No Matches
usbeth.c
Go to the documentation of this file.
1/* *********************************************************************
2 * Broadcom Common Firmware Environment (CFE)
3 *
4 * USB Ethernet File: usbeth.c
5 *
6 * Generic top-level interface for USB Ethernet devices
7 *
8 *********************************************************************
9 *
10 * Copyright 2000,2001,2002,2003,2005
11 * Broadcom Corporation. All rights reserved.
12 *
13 * This software is furnished under license and may be used and
14 * copied only in accordance with the following terms and
15 * conditions. Subject to these conditions, you may download,
16 * copy, install, use, modify and distribute modified or unmodified
17 * copies of this software in source and/or binary form. No title
18 * or ownership is transferred hereby.
19 *
20 * 1) Any source code used, modified or distributed must reproduce
21 * and retain this copyright notice and list of conditions
22 * as they appear in the source file.
23 *
24 * 2) No right is granted to use any trade name, trademark, or
25 * logo of Broadcom Corporation. The "Broadcom Corporation"
26 * name may not be used to endorse or promote products derived
27 * from this software without the prior written permission of
28 * Broadcom Corporation.
29 *
30 * 3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR
31 * IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED
32 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
33 * PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT
34 * SHALL BROADCOM BE LIABLE FOR ANY DAMAGES WHATSOEVER, AND IN
35 * PARTICULAR, BROADCOM SHALL NOT BE LIABLE FOR DIRECT, INDIRECT,
36 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
37 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
38 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
39 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
40 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
41 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE), EVEN IF ADVISED OF
42 * THE POSSIBILITY OF SUCH DAMAGE.
43 ********************************************************************* */
44
45/* *********************************************************************
46 * USB-Ethernet driver - CFE Network Layer Interfaces
47 ********************************************************************* */
48
49#include "cfe.h"
50
51#include "usbd.h"
52#include "usbeth.h"
53
54
55typedef struct usbeth_unit_s {
56 int unit;
58 void *softc;
60
61
62/* *********************************************************************
63 * CFE Device registration stuff
64 *
65 * Like the USB serial device, board packages instantiate
66 * a "usbeth" device and as the actual usb devices come and
67 * go they attach to an array of pointers to devices, below.
68 *
69 * The USB device attach process eventually calls usbeth_register
70 * to attach the dynamic device to the static CFE device,
71 * and usbeth_unregister to remove it. the CFE device
72 * stays around.
73 ********************************************************************* */
74
75#define USBETH_MAXUNITS 4
76static usbeth_unit_t *usbeth_units[USBETH_MAXUNITS] = {0};
77
78int usbeth_register(usbeth_disp_t *disp,void *softc)
79{
80 int idx;
81 usbeth_unit_t *unit;
82
83 for (idx = 0; idx < USBETH_MAXUNITS; idx++) {
84 unit = usbeth_units[idx];
85 if (!unit) continue;
86 if (!unit->dispatch) {
87 unit->dispatch = disp;
88 unit->softc = softc;
89 return idx;
90 }
91 }
92
93 return -1;
94}
95
96
97void usbeth_unregister(void *softc)
98{
99 int idx;
100 usbeth_unit_t *unit;
101
102 /*
103 * We had better find the softc. If we fail to find it,
104 * something is very wrong!
105 */
106
107 for (idx = 0; idx < USBETH_MAXUNITS; idx++) {
108 unit = usbeth_units[idx];
109 if (!unit) continue;
110 if (unit->softc == softc) {
111 unit->dispatch = NULL;
112 unit->softc = NULL;
113 }
114 }
115}
116
117/* *********************************************************************
118 * CFE-Ethernet device interfaces
119 ********************************************************************* */
120
121
122static int usb_ether_open(cfe_devctx_t *ctx)
123{
124 return 0;
125}
126
127static int usb_ether_read( cfe_devctx_t *ctx, iocb_buffer_t *buffer )
128{
129 usbeth_unit_t *unit = (usbeth_unit_t *) ctx->dev_softc;
130
131 /* If no device is hooked up, pretend like the cable is disconnected */
132 if (unit->softc == NULL) {
133 return CFE_ERR_NOTREADY;
134 }
135
136 buffer->buf_retlen = (*(unit->dispatch->read))(unit->softc,buffer->buf_ptr);
137
138 return 0;
139}
140
141
142static int usb_ether_inpstat( cfe_devctx_t *ctx, iocb_inpstat_t *inpstat )
143{
144 usbeth_unit_t *unit = (usbeth_unit_t *) ctx->dev_softc;
145
146 /* If no device is hooked up, pretend like the cable is disconnected */
147 if (unit->softc == NULL) {
148 inpstat->inp_status = 0;
149 return 0;
150 }
151
152 inpstat->inp_status = (*(unit->dispatch->inpstat))(unit->softc);
153
154 return 0;
155}
156
157
158static int usb_ether_write(cfe_devctx_t *ctx,iocb_buffer_t *buffer)
159{
160 usbeth_unit_t *unit = (usbeth_unit_t *) ctx->dev_softc;
161
162 /* If no device is hooked up, pretend like the cable is disconnected */
163 if (unit->softc == NULL) {
164 return CFE_ERR_NOTREADY;
165 }
166
167 /* Block until hw notifies you data is sent. */
168 (*(unit->dispatch->write))(unit->softc,buffer->buf_ptr, buffer->buf_length );
169
170 return 0;
171}
172
173
174static int usb_ether_ioctl(cfe_devctx_t *ctx,iocb_buffer_t *buffer)
175{
176 usbeth_unit_t *unit = (usbeth_unit_t *) ctx->dev_softc;
177 int retval = 0;
178
179 /* If no device is hooked up, pretend like the cable is disconnected */
180 if (unit->softc == NULL) {
181 return CFE_ERR_UNSUPPORTED;
182 }
183
184 switch( (int)buffer->buf_ioctlcmd ) {
185 case IOCTL_ETHER_GETHWADDR:
186 (*(unit->dispatch->getaddr))(unit->softc,buffer->buf_ptr );
187 break;
188 default:
189 retval = CFE_ERR_UNSUPPORTED;
190 }
191
192 return retval;
193}
194
195
196static int usb_ether_close(cfe_devctx_t *ctx)
197{
198 return 0;
199}
200
201static void usb_ether_probe(cfe_driver_t *drv,
202 unsigned long probe_a, unsigned long probe_b,
203 void *probe_ptr)
204{
205 usbeth_unit_t *unit;
206 char descr[80];
207
208 unit = (usbeth_unit_t *) KMALLOC(sizeof(usbeth_unit_t),0);
209 memset(unit,0,sizeof(usbeth_unit_t));
210 unit->unit = (int)probe_a;
211
212 usbeth_units[unit->unit] = unit;
213
214 xsprintf(descr,"USB Ethernet unit %d",(int)probe_a);
215 cfe_attach(drv,unit,NULL,descr);
216}
217
218
219/* CFE ethernet device interface structures */
220
221const static cfe_devdisp_t usb_ether_dispatch =
222{
223 usb_ether_open,
224 usb_ether_read,
225 usb_ether_inpstat,
226 usb_ether_write,
227 usb_ether_ioctl,
228 usb_ether_close,
229 NULL,
230 NULL
231};
232
233const cfe_driver_t usb_ether =
234{
235 "USB-Ethernet Device",
236 "eth",
237 CFE_DEV_NETWORK,
238 &usb_ether_dispatch,
239 usb_ether_probe,
240};
241
#define NULL
Definition: def.h:47
#define KMALLOC(size, align)
Definition: lib_malloc.h:92
int(* read)(void *, hsaddr_t buf)
Definition: usbeth.h:178
int(* write)(void *, hsaddr_t buf, int len)
Definition: usbeth.h:180
int(* getaddr)(void *, hsaddr_t addr)
Definition: usbeth.h:181
int(* inpstat)(void *)
Definition: usbeth.h:179
usbeth_disp_t * dispatch
Definition: usbeth.c:57
void * softc
Definition: usbeth.c:58
int unit
Definition: usbeth.c:56
const cfe_driver_t usb_ether
Definition: usbeth.c:233
#define USBETH_MAXUNITS
Definition: usbeth.c:75
int usbeth_register(usbeth_disp_t *disp, void *softc)
Definition: usbeth.c:78
struct usbeth_unit_s usbeth_unit_t
void usbeth_unregister(void *softc)
Definition: usbeth.c:97