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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [io/] [usb/] [eth/] [slave/] [current/] [include/] [usbs_eth.h] - Blame information for rev 786

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
#ifndef CYGONCE_USBS_ETH_H
2
#define  CYGONCE_USBS_ETH_H_
3
//==========================================================================
4
//
5
//      include/usbs_eth.h
6
//
7
//      Description of the USB slave-side ethernet support
8
//
9
//==========================================================================
10
// ####ECOSGPLCOPYRIGHTBEGIN####                                            
11
// -------------------------------------------                              
12
// This file is part of eCos, the Embedded Configurable Operating System.   
13
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
14
//
15
// eCos is free software; you can redistribute it and/or modify it under    
16
// the terms of the GNU General Public License as published by the Free     
17
// Software Foundation; either version 2 or (at your option) any later      
18
// version.                                                                 
19
//
20
// eCos is distributed in the hope that it will be useful, but WITHOUT      
21
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or    
22
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License    
23
// for more details.                                                        
24
//
25
// You should have received a copy of the GNU General Public License        
26
// along with eCos; if not, write to the Free Software Foundation, Inc.,    
27
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.            
28
//
29
// As a special exception, if other files instantiate templates or use      
30
// macros or inline functions from this file, or you compile this file      
31
// and link it with other works to produce a work based on this file,       
32
// this file does not by itself cause the resulting work to be covered by   
33
// the GNU General Public License. However the source code for this file    
34
// must still be made available in accordance with section (3) of the GNU   
35
// General Public License v2.                                               
36
//
37
// This exception does not invalidate any other reasons why a work based    
38
// on this file might be covered by the GNU General Public License.         
39
// -------------------------------------------                              
40
// ####ECOSGPLCOPYRIGHTEND####                                              
41
//==========================================================================
42
//#####DESCRIPTIONBEGIN####
43
//
44
// Author(s):    bartv
45
// Contributors: bartv
46
// Date:         2000-10-04
47
// Purpose:
48
// Description:  USB slave-side ethernet support
49
//
50
//
51
//####DESCRIPTIONEND####
52
//==========================================================================
53
 
54
#ifdef __cplusplus
55
extern "C" {
56
#endif
57
 
58
//
59
// The primary purpose of the USB slave-side ethernet code is to
60
// provide an ethernet service for the host. Essentially this means
61
// the following:
62
//
63
// 1) the host can transmit an ethernet frame to the USB peripheral.
64
//    This frame is received by the code in this package and then
65
//    passed up to higher-level code for processing. Typically the
66
//    frame will originate from a TCP/IP stack running inside the
67
//    host, and the higher-level code will forward the frame via a
68
//    real ethernet chip or some other ethernet-style device.
69
//
70
// 2) higher-level code will provide ethernet frames to be sent to
71
//    the host, usually to a TCP/IP stack running on the host. The
72
//    exact source of the ethernet frame is not known.
73
//
74
// 3) the host may initiate a number of control operations, for
75
//    example it may request the MAC address or it may want to
76
//    control the filtering mode (e.g. enable promiscuous mode).
77
//
78
// 4) there are USB control-related operations, for example actions
79
//    to be taken when the peripheral is disconnected from the
80
//    bus or when the host wants to disable the ethernet interface.
81
//
82
// It is possible to develop a USB ethernet peripheral that does not
83
// involve a TCP/IP stack inside the peripheral, in fact that is the
84
// most common implementation. Instead a typical peripheral would
85
// involve a USB port, an ethernet port, and a cheap microcontroller
86
// just powerful enough to forward packets between the two. The eCos
87
// USB code can be used in this way, and the primary external
88
// interface provides enough functionality for this to work.
89
//
90
//                  +---------------+   ethernet
91
//   +----+         |               |     |
92
//   |    |   USB   |      app      |     |
93
//   |host|---------|     /   \     |-----o
94
//   |    |         |    /     \    |     |
95
//   +----+         | USB-eth   eth |     |
96
//                  +---------------+     |
97
//                   USB peripheral
98
//
99
// Note that the USB-ethernet code does not know anything about the
100
// real ethernet device or what the application gets up to, it just
101
// provides an interface to the app. The above represents just one
102
// possible use for a USB-ethernet device.
103
//
104
// Also worth mentioning: when the host TCP/IP stack requests the MAC
105
// address USB-eth would normally respond with the MAC address for the
106
// real ethernet device. That way things like host-side DHCP should
107
// just work.
108
//
109
// Alternatively for some applications it is desirable to run a TCP/IP
110
// stack inside the peripheral as well as on the host. This makes
111
// things a fair bit more complicated, something like this.
112
//
113
//                  +---------------+
114
//                  |      app      |
115
//                  |       |       |  ethernet
116
//   +----+         |       |       |     |
117
//   |    |   USB   |     TCP/IP    |     |
118
//   |host|---------|     /   \     |-----o
119
//   |    |         |    /     \    |     |
120
//   +----+         | USB-eth   eth |     |
121
//                  +---------------+     |
122
//                   USB peripheral
123
//
124
// 
125
// Usually this will involve enabling the bridge code in the TCP/IP
126
// stack, or possibly performing some sort of bridging below the
127
// TCP/IP stack. One way of getting things to work is to view the
128
// USB connection as a small ethernet segment with just two
129
// attached machines, the host and the peripheral. The two will
130
// need separate MAC addresses, in addition to the MAC address
131
// for the real ethernet device. This way the bridge code
132
// sees things the way it expects.    
133
//
134
// There will still be some subtle differences between a setup like
135
// this and a conventional ethernet bridge, mainly because there
136
// is a host-side TCP/IP stack which can perform control operations.
137
// For example the host stack may request that USB-eth go into
138
// promiscuous mode. A conventional ethernet bridge just deals
139
// with ethernet segments and does not need to worry about
140
// control requests coming in from one of the segments.
141
//
142
// It is not absolutely essential that there is another network.
143
// However without another network this setup would look to the host
144
// like an ethernet segment with just two machines attached to it, the
145
// host itself and the USB peripheral, yet it still involves all the
146
// complexities of ethernet such as broadcast masks and IP subnets.
147
// Anything along these lines is likely to prove somewhat confusing,
148
// and the USB peripheral should probably act like some other class
149
// of USB device instead.
150
//
151
// One special setup has the host acting as a bridge to another
152
// network, rather than the peripheral. This might make sense for
153
// mobile peripherals such as PDA's, as a way of connecting the
154
// peripheral to an existing LAN without needing a LAN adapter.
155
// Enabling bridging in the host may be a complex operation, limiting
156
// the applicability of such a setup.
157
//
158
// This package will only implement the eCos network driver interface
159
// if explicitly enabled. The package-specific interface is always
160
// provided, although trying to mix and match the two may lead to
161
// terrible confusion: once the network driver is active nothing else
162
// should use the lower-level USB ethernet code. However application
163
// code is responsible for initializing the package, and specifically
164
// for providing details of the USB endpoints that should be used.
165
//
166
// The package assumes that it needs to provide just one
167
// instantiation. Conceivably there may be applications where it makes
168
// sense for a USB peripheral to supply two separate ethernet devices
169
// to the host, but that would be an unusual setup. Also a peripheral
170
// might provide two or more USB slave ports to allow multiple hosts
171
// to be connected, with a separate USB-ethernet instantiation for
172
// each port, but again that would be an unusual setup. Applications
173
// which do require more than one instantiation are responsible
174
// for doing this inside the application code.
175
 
176
// The public interface depends on configuration options.
177
#include <pkgconf/io_usb_slave_eth.h>
178
 
179
// Define the interface in terms of eCos data types.
180
#include <cyg/infra/cyg_type.h>
181
 
182
// The generic USB support
183
#include <cyg/io/usb/usbs.h>
184
 
185
// Network driver definition, to support cloning of usbs_eth_netdev0
186
#ifdef CYGPKG_USBS_ETHDRV
187
# include <cyg/io/eth/netdev.h>
188
#endif
189
 
190
// Cache details, to allow alignment to cache line boundaries etc.
191
#include <cyg/hal/hal_cache.h>    
192
 
193
// ----------------------------------------------------------------------------
194
// Maximum transfer size. This is not specified by io/eth. It can be
195
// determined from <netinet/if_ether.h> but the TCP/IP stack may not
196
// be loaded so that header file cannot be used.
197
//
198
// Some (most?) USB implementations have implementation problems. For
199
// example the SA11x0 family cannot support transfers that are exact
200
// multiples of the 64-byte USB bulk packet size, instead it is
201
// necessary to add explicit size information. This can be encoded
202
// conveniently at the start of the buffer.
203
//
204
// So the actual MTU consists of:
205
//  1) a 1500 byte payload
206
//  2) the usual ethernet header with a six-byte source MAC
207
//     address, a six-byte destination MAC address, and a
208
//     two-byte protocol or length field, for a total header
209
//     size of 14 bytes.
210
//  3) an extra two bytes of size info.
211
//
212
// For a total of 1516 bytes.
213
#define CYGNUM_USBS_ETH_MAX_FRAME_SIZE 1514
214
#define CYGNUM_USBS_ETH_MAXTU (CYGNUM_USBS_ETH_MAX_FRAME_SIZE + 2)
215
 
216
// Although the minimum ethernet frame size is 60 bytes, this includes
217
// padding which is not needed when transferring over USB. Hence the
218
// actual minimum is just the 14 byte ethernet header plus two bytes
219
// for the length.
220
#define CYGNUM_USBS_ETH_MIN_FRAME_SIZE 14
221
#define CYGNUM_USBS_ETH_MINTU (CYGNUM_USBS_ETH_MIN_FRAME_SIZE + 2)
222
 
223
// Typical USB devices involve DMA operations and hence confusion
224
// between cached and uncached memory. To make life easier for
225
// the underlying USB device drivers, this package ensures that
226
// receive operations always involve buffers that are aligned to
227
// a cache-line boundary and that are a multiple of the cacheline
228
// size.
229
#ifndef HAL_DCACHE_LINE_SIZE
230
# define CYGNUM_USBS_ETH_RXBUFSIZE      CYGNUM_USBS_ETH_MAXTU
231
# define CYGNUM_USBS_ETH_RXSIZE         CYGNUM_USBS_ETH_MAXTU    
232
#else
233
# define CYGNUM_USBS_ETH_RXBUFSIZE      ((CYGNUM_USBS_ETH_MAXTU + HAL_DCACHE_LINE_SIZE + HAL_DCACHE_LINE_SIZE - 1) \
234
                                         & ~(HAL_DCACHE_LINE_SIZE - 1))
235
# define CYGNUM_USBS_ETH_RXSIZE         ((CYGNUM_USBS_ETH_MAXTU + HAL_DCACHE_LINE_SIZE - 1) & ~(HAL_DCACHE_LINE_SIZE - 1))
236
#endif    
237
 
238
// ----------------------------------------------------------------------------
239
// This data structure serves two purposes. First, it keeps track of
240
// the information needed by the low-level USB ethernet code, for
241
// example which endpoints should be used for incoming and outgoing
242
// packets. Second, if the support for the TCP/IP stack is enabled
243
// then there are additional fields to support that (e.g. for keeping
244
// track of statistics).
245
//
246
// Arguably the two uses should be separated into distinct data
247
// structures. That would make it possible to instantiate multiple
248
// low-level USB-ethernet devices but only have a network driver for
249
// one of them. Achieving that flexibility would require some extra
250
// indirection, affecting performance and code-size, and it is not
251
// clear that that flexibility would ever prove useful. For now having
252
// a single data structure seems more appropriate.
253
 
254
typedef struct usbs_eth {
255
 
256
    // What endpoints should be used for communication?
257
    usbs_control_endpoint*      control_endpoint;
258
    usbs_rx_endpoint*           rx_endpoint;
259
    usbs_tx_endpoint*           tx_endpoint;
260
 
261
    // Is the host ready to receive packets? This state is determined
262
    // largely by control packets sent from the host. It can change at
263
    // DSR level.
264
    volatile cyg_bool   host_up;
265
 
266
    // Has the host-side set promiscuous mode? This is relevant to the
267
    // network driver which may need to do filtering based on the MAC
268
    // address and host-side promiscuity.
269
    volatile cyg_bool   host_promiscuous;
270
 
271
    // The host MAC address. This is the address supplied to the
272
    // host's TCP/IP stack and filled in by the init function. There
273
    // is no real hardware to extract the address from.
274
    unsigned char       host_MAC[6];
275
 
276
    // Needed for callback operations.
277
    void                (*tx_callback_fn)(struct usbs_eth*, void*, int);
278
    void*               tx_callback_arg;
279
 
280
    void                (*rx_callback_fn)(struct usbs_eth*, void*, int);
281
    void*               rx_callback_arg;
282
 
283
    // RX operations just block if the host is not connected, resuming
284
    // when a connection is established. This means saving the buffer
285
    // pointer so that when the host comes back up the rx operation
286
    // proper can start. This is not quite consistent because if the
287
    // connection breaks while an RX is in progress there will be a
288
    // callback with an error code whereas an RX on a broken
289
    // connection just blocks, but this does fit neatly into an
290
    // event-driven I/O model.
291
    unsigned char*      rx_pending_buf;
292
 
293
#ifdef CYGPKG_USBS_ETHDRV
294
    // Has the TCP/IP stack brought up this interface yet?
295
    cyg_bool            ecos_up;
296
 
297
    // Is there an ongoing receive? Cancelling a receive operation
298
    // during a stop() may be difficult, and a stop() may be followed
299
    // immediately by a restart.
300
    cyg_bool            rx_active;
301
 
302
    // The eCos-side MAC. If the host and the eCos stack are to
303
    // communicate then they must be able to address each other, i.e.
304
    // they need separate addresses. Again there is no real hardware
305
    // to extract the address from so it has to be supplied by higher
306
    // level code via e.g. an ioctl().
307
    unsigned char       ecos_MAC[6];
308
 
309
    // SNMP statistics
310
# ifdef CYGFUN_USBS_ETHDRV_STATISTICS
311
    unsigned int        interrupts;
312
    unsigned int        tx_count;
313
    unsigned int        rx_count;
314
    unsigned int        rx_short_frames;
315
    unsigned int        rx_too_long_frames;
316
# endif
317
 
318
    // The need for a receive buffer is unavoidable for now because
319
    // the network driver interface does not support pre-allocating an
320
    // mbuf and then passing it back to the stack later. Ideally the
321
    // rx operation would read a single USB packet, determine the
322
    // required mbuf size from the 2-byte header, copy the initial
323
    // data, and then read more USB packets. Alternatively, a
324
    // 1516 byte mbuf could be pre-allocated and then the whole
325
    // transfer could go there, potentially wasting some mbuf space.
326
    // None of this is possible at present.
327
    //
328
    // Also, typically there will be complications because of
329
    // dependencies on DMA, cached vs. uncached memory, etc.
330
    unsigned char       rx_buffer[CYGNUM_USBS_ETH_RXBUFSIZE];
331
    unsigned char*      rx_bufptr;
332
    cyg_bool            rx_buffer_full;
333
 
334
    // It should be possible to eliminate the tx buffer. The problem
335
    // is that the protocol requires 2 bytes to be prepended, and that
336
    // may not be possible with the buffer supplied by higher-level
337
    // code. Eliminating this buffer would either require USB
338
    // device drivers to implement gather functionality on transmits,
339
    // or it would impose a dependency on higher-level code.
340
    unsigned char       tx_buffer[CYGNUM_USBS_ETH_MAXTU];
341
    cyg_bool            tx_buffer_full;
342
    cyg_bool            tx_done;
343
    unsigned long       tx_key;
344
 
345
    // Prevent recursion send()->tx_done()->can_send()/send()
346
    cyg_bool            tx_in_send;
347
#endif
348
 
349
} usbs_eth;
350
 
351
// The package automatically instantiates one USB ethernet device.
352
extern usbs_eth usbs_eth0;
353
 
354
// ----------------------------------------------------------------------------
355
// If the network driver option is enabled then the package also
356
// provides a single cyg_netdevtab_entry. This is exported so that
357
// application code can clone the entry.
358
#ifdef CYGPKG_USBS_ETHDRV
359
extern cyg_netdevtab_entry_t usbs_eth_netdev0;
360
#endif    
361
 
362
// ----------------------------------------------------------------------------
363
// A C interface to the low-level USB code.
364
 
365
// Initialize the USBS-eth support for a particular usbs_eth device.
366
// This associates a usbs_eth structure with specific endpoints.
367
extern void usbs_eth_init(usbs_eth*, usbs_control_endpoint*, usbs_rx_endpoint*, usbs_tx_endpoint*, unsigned char*);
368
 
369
// Start an asynchronous transmit of a single buffer of up to
370
// CYGNUM_USBS_ETH_MAXTU bytes. This buffer should contain a 2-byte
371
// size field, a 14-byte ethernet header, and upto 1500 bytes of
372
// payload. When the transmit has completed the callback function (if
373
// any) will be invoked with the specified pointer. NOTE: figure out
374
// what to do about error reporting
375
extern void usbs_eth_start_tx(usbs_eth*, unsigned char*, void (*)(usbs_eth*, void*, int), void*);
376
 
377
// Start an asynchronous receive of an ethernet packet. The supplied
378
// buffer should be at least CYGNUM_USBS_ETH_MAXTU bytes. When a
379
// complete ethernet frame has been received or when some sort of
380
// error occurs the callback function will be invoked. The third
381
// argument
382
extern void usbs_eth_start_rx(usbs_eth*, unsigned char*, void (*)(usbs_eth*, void*, int), void*);
383
 
384
// The handler for application class control messages. The init call
385
// will install this in the control endpoint by default. However the
386
// handler is fairly dumb: it assumes that all application control
387
// messages are for the ethernet interface and does not bother to
388
// check the control message's destination. This is fine for simple
389
// USB ethernet devices, but for any kind of multi-function peripheral
390
// higher-level code will have to perform multiplexing and invoke this
391
// handler only when appropriate.
392
extern usbs_control_return usbs_eth_class_control_handler(usbs_control_endpoint*, void*);
393
 
394
// Similarly a handler for state change messages. Installing this
395
// means that the ethernet code will have sufficient knowledge about
396
// the state of the USB connection for simple ethernet-only
397
// peripherals, but not for anything more complicated. In the latter
398
// case higher-level code will need to keep track of which
399
// configuration, interfaces, etc. are currently active and explicitly
400
// enable or disable the ethernet device using the functions below.
401
extern void usbs_eth_state_change_handler(usbs_control_endpoint*, void*, usbs_state_change, int);
402
extern void usbs_eth_disable(usbs_eth*);
403
extern void usbs_eth_enable(usbs_eth*);
404
 
405
#ifdef __cplusplus
406
} // extern "C"
407
#endif
408
 
409
#endif // CYGONCE_USBS_ETH_H_

powered by: WebSVN 2.1.0

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