OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [io/] [usb/] [eth/] [slave/] [current/] [src/] [usbseth.c] - Blame information for rev 786

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
2
//
3
//      usbseth.c
4
//
5
//      Support for USB-ethernet devices, slave-side.
6
//
7
//==========================================================================
8
// ####ECOSGPLCOPYRIGHTBEGIN####                                            
9
// -------------------------------------------                              
10
// This file is part of eCos, the Embedded Configurable Operating System.   
11
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
12
//
13
// eCos is free software; you can redistribute it and/or modify it under    
14
// the terms of the GNU General Public License as published by the Free     
15
// Software Foundation; either version 2 or (at your option) any later      
16
// version.                                                                 
17
//
18
// eCos is distributed in the hope that it will be useful, but WITHOUT      
19
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or    
20
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License    
21
// for more details.                                                        
22
//
23
// You should have received a copy of the GNU General Public License        
24
// along with eCos; if not, write to the Free Software Foundation, Inc.,    
25
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.            
26
//
27
// As a special exception, if other files instantiate templates or use      
28
// macros or inline functions from this file, or you compile this file      
29
// and link it with other works to produce a work based on this file,       
30
// this file does not by itself cause the resulting work to be covered by   
31
// the GNU General Public License. However the source code for this file    
32
// must still be made available in accordance with section (3) of the GNU   
33
// General Public License v2.                                               
34
//
35
// This exception does not invalidate any other reasons why a work based    
36
// on this file might be covered by the GNU General Public License.         
37
// -------------------------------------------                              
38
// ####ECOSGPLCOPYRIGHTEND####                                              
39
//==========================================================================
40
//#####DESCRIPTIONBEGIN####
41
//
42
// Author(s):    bartv
43
// Contributors: bartv
44
// Date:         2000-10-04
45
//
46
//####DESCRIPTIONEND####
47
//
48
//==========================================================================
49
 
50
#include <cyg/infra/cyg_type.h>
51
#include <cyg/infra/cyg_ass.h>
52
#include <cyg/infra/cyg_trac.h>
53
#include <cyg/infra/diag.h>
54
#include <cyg/hal/hal_arch.h>
55
#include <cyg/infra/diag.h>
56
#include <cyg/hal/drv_api.h>
57
 
58
#include <pkgconf/io_usb_slave_eth.h>
59
 
60
#define __ECOS 1
61
#include <cyg/io/usb/usbs_eth.h>
62
 
63
#ifdef CYGPKG_USBS_ETHDRV
64
#include <cyg/io/eth/netdev.h>
65
#include <cyg/io/eth/eth_drv.h>
66
#endif
67
 
68
// ----------------------------------------------------------------------------
69
// Static data.
70
//
71
// usbs_eth0 contains the per-device data, both the low-level data
72
// such as which endpoints to use and the network-driver data such as
73
// SNMP statistics. If this package is loaded then the assumption
74
// is that there should be at least one USB-ethernet device. Additional
75
// ones can be instantiated in application code if necessary. A call
76
// to usbs_eth_init() is required for initialization.
77
usbs_eth usbs_eth0;
78
 
79
// ----------------------------------------------------------------------------
80
// Initialization. This should be called explicitly by application code
81
// at an appropriate point in the system startup.
82
void
83
usbs_eth_init(usbs_eth* eth, usbs_control_endpoint* ctrl, usbs_rx_endpoint* rx, usbs_tx_endpoint* tx, unsigned char* mac)
84
{
85
    eth->control_endpoint       = ctrl;
86
    eth->rx_endpoint            = rx;
87
    eth->tx_endpoint            = tx;
88
    eth->host_up                = false;
89
    eth->host_promiscuous       = false;
90
    memcpy(eth->host_MAC, mac, 6);
91
    eth->rx_pending_buf         = (unsigned char*) 0;
92
 
93
    // Install default handlers for some messages. Higher level code
94
    // may override this.
95
    ctrl->state_change_fn       = &usbs_eth_state_change_handler;
96
    ctrl->state_change_data     = (void*) eth;
97
    ctrl->class_control_fn      = &usbs_eth_class_control_handler;
98
    ctrl->class_control_data    = (void*) eth;
99
 
100
#ifdef CYGPKG_USBS_ETHDRV
101
    eth->ecos_up                = false;
102
    eth->rx_active              = false;
103
# ifdef CYGFUN_USBS_ETHDRV_STATISTICS    
104
    eth->interrupts             = 0;
105
    eth->tx_count               = 0;
106
    eth->rx_count               = 0;
107
# endif
108
# ifndef HAL_DCACHE_LINE_SIZE
109
    eth->rx_bufptr              = eth->rx_buffer;
110
# else
111
# endif    
112
    eth->rx_bufptr              = (unsigned char*) ((((cyg_uint32)eth->rx_buffer) + HAL_DCACHE_LINE_SIZE - 1)
113
                                                    & ~(HAL_DCACHE_LINE_SIZE - 1));
114
    eth->rx_buffer_full         = false;
115
    eth->tx_in_send             = false;
116
    eth->tx_buffer_full         = false;
117
    eth->tx_done                = false;
118
#endif
119
}
120
 
121
 
122
// ----------------------------------------------------------------------------
123
// Generic transmit and receive operations. These can be called
124
// explicitly by application code, or implicitly via the eCos ethernet
125
// device driver code in usbsethdrv.c. These two modes of operation
126
// should not be mixed since the routines do not perform any
127
// synchronization themselves, instead they rely on higher level code.
128
 
129
// Packet transmission. The exported function is usbs_eth_start_tx(),
130
// which can be invoked from thread context or DSR context. The
131
// supplied buffer must already be in a form that can be transmitted
132
// directly out of the USB endpoint with no further processing
133
// (although it is necessary to extract the size information from the
134
// buffer).
135
//
136
// When the underlying USB transfer has completed the USB code will invoke
137
// usbs_eth_tx_callback(), usually in DSR context although possibly in
138
// thread context depending on the specific USB implementation. The
139
// underlying USB driver may have had to do some padding so the amount
140
// transferred may be slightly greater than requested.
141
 
142
static void
143
usbs_eth_tx_callback(void* usbs_callback_arg, int size)
144
{
145
    usbs_eth*   eth = (usbs_eth*) usbs_callback_arg;
146
    CYG_ASSERT( (size < 0) || (size >= CYGNUM_USBS_ETH_MINTU), "returned size must be valid.");
147
    (*eth->tx_callback_fn)(eth, eth->tx_callback_arg, size);
148
}
149
 
150
void
151
usbs_eth_start_tx(usbs_eth* eth, unsigned char* buf, void (*callback_fn)(usbs_eth*, void*, int), void* callback_arg)
152
{
153
    int      size;
154
    cyg_bool address_ok = false;
155
    static const unsigned char broadcast_mac[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
156
 
157
    size = buf[0] + (buf[1] << 8);
158
    CYG_ASSERT( (size < 0) || ((size >= CYGNUM_USBS_ETH_MIN_FRAME_SIZE) && (size <= CYGNUM_USBS_ETH_MAX_FRAME_SIZE)), \
159
                "ethernet frame size constraints must be observed");
160
 
161
    if ((0 == memcmp(buf + 2, eth->host_MAC, 6)) ||
162
        (0 == memcmp(buf + 2, broadcast_mac, 6))) {
163
        address_ok = true;
164
    }
165
 
166
    // The following checks involve data that can change as a result
167
    // of control operations, so it is necessary to synchronize with
168
    // those. The control operations will typically run at DSR level
169
    // so a DSR lock has to be used. 
170
 
171
    cyg_drv_dsr_lock();
172
    if (eth->host_up && (address_ok || eth->host_promiscuous)) {
173
 
174
        eth->tx_callback_fn             = callback_fn;
175
        eth->tx_callback_arg            = callback_arg;
176
        eth->tx_endpoint->buffer        = buf;
177
        eth->tx_endpoint->buffer_size   = size + 2;
178
        eth->tx_endpoint->complete_fn   = &usbs_eth_tx_callback;
179
        eth->tx_endpoint->complete_data = (void*) eth;
180
        (*(eth->tx_endpoint->start_tx_fn))(eth->tx_endpoint);
181
 
182
    } else {
183
        // Packets not intended for the host can be discarded quietly.
184
        // A broken connection needs to be reported.
185
        (*callback_fn)(eth, callback_arg, eth->host_up ? size : -EPIPE);
186
    }
187
    cyg_drv_dsr_unlock();
188
}
189
 
190
// Packet reception. This simply involves starting a transfer for
191
// up to the maximum ethernet frame size. The lower-level USB code
192
// will detect the end of the transfer. The exported function is
193
// usbs_eth_start_rx(). 
194
static void
195
usbs_eth_rx_callback(void* usbs_callback_arg, int size)
196
{
197
    usbs_eth*   eth = (usbs_eth*) usbs_callback_arg;
198
 
199
    CYG_ASSERT( (size <= 0) || ((size >= CYGNUM_USBS_ETH_MINTU) && (size <= CYGNUM_USBS_ETH_MAXTU)), \
200
                "ethernet frame size constraints must be observed");
201
 
202
    (*eth->rx_callback_fn)(eth, eth->rx_callback_arg, size);
203
}
204
 
205
void
206
usbs_eth_start_rx(usbs_eth* eth, unsigned char* buf, void (*callback_fn)(usbs_eth*, void*, int), void* callback_arg)
207
{
208
    eth->rx_callback_fn  = callback_fn;
209
    eth->rx_callback_arg = callback_arg;
210
 
211
    cyg_drv_dsr_lock();
212
    if (eth->host_up) {
213
        eth->rx_endpoint->buffer        = buf;
214
        eth->rx_endpoint->buffer_size   = CYGNUM_USBS_ETH_RXSIZE;
215
        eth->rx_endpoint->complete_fn   = &usbs_eth_rx_callback;
216
        eth->rx_endpoint->complete_data = (void*) eth;
217
        (*(eth->rx_endpoint->start_rx_fn))(eth->rx_endpoint);
218
    } else {
219
        CYG_ASSERT( (void*) 0 == eth->rx_pending_buf, "No RX operation should be in progress");
220
        eth->rx_pending_buf = buf;
221
    }
222
    cyg_drv_dsr_unlock();
223
}
224
 
225
// ----------------------------------------------------------------------------
226
// Control operations. The host may send two types of application-specific
227
// control messages, one to get the MAC address and one to enable/disable
228
// promiscuous mode on the host side. This callback will typically be invoked
229
// in DSR context.
230
 
231
// These constants need to be shared somehow with the driver in ../host/,
232
// but if some variant of that driver becomes part of the Linux kernel
233
// then its sources must be self-contained with no dependencies on
234
// eCos sources or headers. Hence a duplicate definition for now.
235
#define USBS_ETH_CONTROL_GET_MAC_ADDRESS        0x01
236
#define USBS_ETH_CONTROL_SET_PROMISCUOUS_MODE   0x02
237
 
238
usbs_control_return
239
usbs_eth_class_control_handler(usbs_control_endpoint* endpoint, void* callback_data)
240
{
241
    usbs_control_return result = USBS_CONTROL_RETURN_STALL;
242
 
243
    usbs_eth*   eth    = (usbs_eth*)   callback_data;
244
    usb_devreq* devreq = (usb_devreq*) endpoint->control_buffer;
245
    int         size   = (devreq->length_hi << 8) + devreq->length_lo;
246
 
247
    CYG_ASSERT(endpoint == eth->control_endpoint, "USB ethernet control messages correctly routed");
248
 
249
    if (USBS_ETH_CONTROL_GET_MAC_ADDRESS == devreq->request) {
250
        // This should be an IN operation for at least six bytes.
251
        if ((size >= 6) &&
252
            (USB_DEVREQ_DIRECTION_IN == (devreq->type & USB_DEVREQ_DIRECTION_MASK))) {
253
 
254
            endpoint->buffer      = eth->host_MAC;
255
            endpoint->buffer_size = 6;
256
            result = USBS_CONTROL_RETURN_HANDLED;
257
        }
258
        // Otherwise drop through with a return value of STALL
259
 
260
    } else if (USBS_ETH_CONTROL_SET_PROMISCUOUS_MODE == devreq->request) {
261
        // The length should be 0, no more data is expected by either side.
262
        if (0 == size) {
263
            // The new promiscuity mode is encoded in value_lo;
264
            eth->host_promiscuous = devreq->value_lo;
265
            result = USBS_CONTROL_RETURN_HANDLED;
266
        }
267
    }
268
 
269
    return result;
270
}
271
 
272
// State changes. As far as the ethernet code is concerned, if there
273
// is a change to CONFIGURED state then the device has come up,
274
// otherwise if there is a change from CONFIGURED state it has gone
275
// down. All other state changes are irrelevant.
276
void
277
usbs_eth_state_change_handler(usbs_control_endpoint* endpoint, void* callback_data, usbs_state_change change, int old_state)
278
{
279
    usbs_eth* eth       = (usbs_eth*) callback_data;
280
    CYG_ASSERT(endpoint == eth->control_endpoint, "USB ethernet state changes correctly routed");
281
 
282
    if (USBS_STATE_CHANGE_CONFIGURED == change) {
283
        if (USBS_STATE_CONFIGURED != old_state) {
284
            usbs_eth_enable(eth);
285
        }
286
    } else if ((USBS_STATE_CHANGE_RESUMED == change) && (USBS_STATE_CONFIGURED == (USBS_STATE_MASK & old_state))) {
287
        usbs_eth_enable(eth);
288
    } else if (eth->host_up) {
289
        usbs_eth_disable(eth);
290
    }
291
}
292
 
293
// Disabling the ethernet device means clearing the host_up flag.
294
// This will block future transmits and receives but not any
295
// that are currently underway.
296
void
297
usbs_eth_disable(usbs_eth* eth)
298
{
299
    eth->host_up = false;
300
}
301
 
302
// Enabling the ethernet device means setting the host_up flag and
303
// possibly activating a pending rx operation.
304
void
305
usbs_eth_enable(usbs_eth* eth)
306
{
307
    if (!eth->host_up) {
308
        eth->host_up            = true;
309
        eth->host_promiscuous   = false;
310
        if ((void*) 0 != eth->rx_pending_buf) {
311
            eth->rx_endpoint->buffer            = eth->rx_pending_buf;
312
            eth->rx_endpoint->buffer_size       = CYGNUM_USBS_ETH_RXSIZE;
313
            eth->rx_endpoint->complete_fn       = &usbs_eth_rx_callback;
314
            eth->rx_endpoint->complete_data     = (void*) eth;
315
            eth->rx_pending_buf = (void*) 0;
316
            (*(eth->rx_endpoint->start_rx_fn))(eth->rx_endpoint);
317
        }
318
    }
319
}
320
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.