source: mainline/uspace/srv/hw/netif/dp8390/dp8390_module.c@ 9ee9d5d5

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9ee9d5d5 was 3a5d238f, checked in by Jakub Jermar <jakub@…>, 15 years ago

Get rid of the ERROR_CODE madness in dp8390.

  • Property mode set to 100644
File size: 9.4 KB
Line 
1/*
2 * Copyright (c) 2009 Lukas Mejdrech
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - 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 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup dp8390
30 * @{
31 */
32
33/** @file
34 * DP8390 network interface implementation.
35 */
36
37#include <assert.h>
38#include <async.h>
39#include <ddi.h>
40#include <errno.h>
41#include <err.h>
42#include <malloc.h>
43#include <ipc/ipc.h>
44#include <ipc/services.h>
45
46#include <net/modules.h>
47#include <packet_client.h>
48#include <adt/measured_strings.h>
49#include <net/device.h>
50#include <nil_interface.h>
51#include <netif_interface.h>
52#include <netif_local.h>
53
54#include "dp8390.h"
55#include "dp8390_drv.h"
56#include "dp8390_port.h"
57
58/** DP8390 module name.
59 */
60#define NAME "dp8390"
61
62/** Returns the device from the interrupt call.
63 * @param[in] call The interrupt call.
64 */
65#define IRQ_GET_DEVICE(call) (device_id_t) IPC_GET_METHOD(*call)
66
67/** Returns the interrupt status register from the interrupt call.
68 * @param[in] call The interrupt call.
69 */
70#define IPC_GET_ISR(call) (int) IPC_GET_ARG2(*call)
71
72/** DP8390 kernel interrupt command sequence.
73 */
74static irq_cmd_t dp8390_cmds[] = {
75 { .cmd = CMD_PIO_READ_8,
76 .addr = NULL,
77 .dstarg = 2
78 },
79 {
80 .cmd = CMD_PREDICATE,
81 .value = 1,
82 .srcarg = 2
83 },
84 {
85 .cmd = CMD_ACCEPT
86 }
87};
88
89/** DP8390 kernel interrupt code.
90 */
91static irq_code_t dp8390_code = {
92 sizeof(dp8390_cmds) / sizeof(irq_cmd_t),
93 dp8390_cmds
94};
95
96/** Handles the interrupt messages.
97 * This is the interrupt handler callback function.
98 * @param[in] iid The interrupt message identifier.
99 * @param[in] call The interrupt message.
100 */
101static void irq_handler(ipc_callid_t iid, ipc_call_t * call)
102{
103 netif_device_t * device;
104 dpeth_t * dep;
105 packet_t received;
106 device_id_t device_id;
107 int phone;
108
109 device_id = IRQ_GET_DEVICE(call);
110 fibril_rwlock_write_lock(&netif_globals.lock);
111 if(find_device(device_id, &device) != EOK){
112 fibril_rwlock_write_unlock(&netif_globals.lock);
113 return;
114 }
115 dep = (dpeth_t *) device->specific;
116 if (dep->de_mode != DEM_ENABLED){
117 fibril_rwlock_write_unlock(&netif_globals.lock);
118 return;
119 }
120 assert(dep->de_flags &DEF_ENABLED);
121 dep->de_int_pending = 0;
122 dp_check_ints(dep, IPC_GET_ISR(call));
123 if(dep->received_queue){
124 received = dep->received_queue;
125 phone = device->nil_phone;
126 dep->received_queue = NULL;
127 dep->received_count = 0;
128 fibril_rwlock_write_unlock(&netif_globals.lock);
129 nil_received_msg(phone, device_id, received, NULL);
130 }else{
131 fibril_rwlock_write_unlock(&netif_globals.lock);
132 }
133 ipc_answer_0(iid, EOK);
134}
135
136/** Changes the network interface state.
137 * @param[in,out] device The network interface.
138 * @param[in] state The new state.
139 * @returns The new state.
140 */
141static int change_state(netif_device_t * device, device_state_t state)
142{
143 if (device->state != state) {
144 device->state = state;
145
146 printf("%s: State changed to %s\n", NAME,
147 (state == NETIF_ACTIVE) ? "active" : "stopped");
148
149 return state;
150 }
151
152 return EOK;
153}
154
155int netif_specific_message(ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count){
156 return ENOTSUP;
157}
158
159int netif_get_device_stats(device_id_t device_id, device_stats_ref stats){
160 netif_device_t * device;
161 eth_stat_t * de_stat;
162 int rc;
163
164 if(! stats){
165 return EBADMEM;
166 }
167 rc = find_device(device_id, &device);
168 if (rc != EOK)
169 return rc;
170 de_stat = &((dpeth_t *) device->specific)->de_stat;
171 null_device_stats(stats);
172 stats->receive_errors = de_stat->ets_recvErr;
173 stats->send_errors = de_stat->ets_sendErr;
174 stats->receive_crc_errors = de_stat->ets_CRCerr;
175 stats->receive_frame_errors = de_stat->ets_frameAll;
176 stats->receive_missed_errors = de_stat->ets_missedP;
177 stats->receive_packets = de_stat->ets_packetR;
178 stats->send_packets = de_stat->ets_packetT;
179 stats->collisions = de_stat->ets_collision;
180 stats->send_aborted_errors = de_stat->ets_transAb;
181 stats->send_carrier_errors = de_stat->ets_carrSense;
182 stats->send_heartbeat_errors = de_stat->ets_CDheartbeat;
183 stats->send_window_errors = de_stat->ets_OWC;
184 return EOK;
185}
186
187int netif_get_addr_message(device_id_t device_id, measured_string_ref address){
188 netif_device_t * device;
189 int rc;
190
191 if(! address){
192 return EBADMEM;
193 }
194 rc = find_device(device_id, &device);
195 if (rc != EOK)
196 return rc;
197 address->value = (char *) (&((dpeth_t *) device->specific)->de_address);
198 address->length = CONVERT_SIZE(ether_addr_t, char, 1);
199 return EOK;
200}
201
202
203
204int netif_probe_message(device_id_t device_id, int irq, uintptr_t io){
205 netif_device_t * device;
206 dpeth_t * dep;
207 int rc;
208
209 device = (netif_device_t *) malloc(sizeof(netif_device_t));
210 if(! device){
211 return ENOMEM;
212 }
213 dep = (dpeth_t *) malloc(sizeof(dpeth_t));
214 if(! dep){
215 free(device);
216 return ENOMEM;
217 }
218 bzero(device, sizeof(netif_device_t));
219 bzero(dep, sizeof(dpeth_t));
220 device->device_id = device_id;
221 device->nil_phone = -1;
222 device->specific = (void *) dep;
223 device->state = NETIF_STOPPED;
224 dep->de_irq = irq;
225 dep->de_mode = DEM_DISABLED;
226 //TODO address?
227 rc = pio_enable((void *) io, DP8390_IO_SIZE, (void **) &dep->de_base_port);
228 if (rc != EOK) {
229 free(dep);
230 free(device);
231 return rc;
232 }
233 rc = do_probe(dep);
234 if (rc != EOK) {
235 free(dep);
236 free(device);
237 return rc;
238 }
239 rc = netif_device_map_add(&netif_globals.device_map, device->device_id, device);
240 if (rc != EOK){
241 free(dep);
242 free(device);
243 return rc;
244 }
245 return EOK;
246}
247
248int netif_send_message(device_id_t device_id, packet_t packet, services_t sender){
249 netif_device_t * device;
250 dpeth_t * dep;
251 packet_t next;
252 int rc;
253
254 rc = find_device(device_id, &device);
255 if (rc != EOK)
256 return rc;
257 if(device->state != NETIF_ACTIVE){
258 netif_pq_release(packet_get_id(packet));
259 return EFORWARD;
260 }
261 dep = (dpeth_t *) device->specific;
262 // process packet queue
263 do{
264 next = pq_detach(packet);
265 if(do_pwrite(dep, packet, FALSE) != EBUSY){
266 netif_pq_release(packet_get_id(packet));
267 }
268 packet = next;
269 }while(packet);
270 return EOK;
271}
272
273int netif_start_message(netif_device_t * device){
274 dpeth_t * dep;
275 int rc;
276
277 if(device->state != NETIF_ACTIVE){
278 dep = (dpeth_t *) device->specific;
279 dp8390_cmds[0].addr = (void *) (uintptr_t) (dep->de_dp8390_port + DP_ISR);
280 dp8390_cmds[2].addr = dp8390_cmds[0].addr;
281 rc = ipc_register_irq(dep->de_irq, device->device_id, device->device_id, &dp8390_code);
282 if (rc != EOK)
283 return rc;
284 rc = do_init(dep, DL_BROAD_REQ);
285 if (rc != EOK) {
286 ipc_unregister_irq(dep->de_irq, device->device_id);
287 return rc;
288 }
289 return change_state(device, NETIF_ACTIVE);
290 }
291 return EOK;
292}
293
294int netif_stop_message(netif_device_t * device){
295 dpeth_t * dep;
296
297 if(device->state != NETIF_STOPPED){
298 dep = (dpeth_t *) device->specific;
299 do_stop(dep);
300 ipc_unregister_irq(dep->de_irq, device->device_id);
301 return change_state(device, NETIF_STOPPED);
302 }
303 return EOK;
304}
305
306int netif_initialize(void){
307 ipcarg_t phonehash;
308
309 async_set_interrupt_received(irq_handler);
310
311 return REGISTER_ME(SERVICE_DP8390, &phonehash);
312}
313
314/** Default thread for new connections.
315 *
316 * @param[in] iid The initial message identifier.
317 * @param[in] icall The initial message call structure.
318 *
319 */
320static void netif_client_connection(ipc_callid_t iid, ipc_call_t * icall)
321{
322 /*
323 * Accept the connection
324 * - Answer the first IPC_M_CONNECT_ME_TO call.
325 */
326 ipc_answer_0(iid, EOK);
327
328 while(true) {
329 ipc_call_t answer;
330 int answer_count;
331
332 /* Clear the answer structure */
333 refresh_answer(&answer, &answer_count);
334
335 /* Fetch the next message */
336 ipc_call_t call;
337 ipc_callid_t callid = async_get_call(&call);
338
339 /* Process the message */
340 int res = netif_module_message(NAME, callid, &call, &answer,
341 &answer_count);
342
343 /* End if said to either by the message or the processing result */
344 if ((IPC_GET_METHOD(call) == IPC_M_PHONE_HUNGUP) || (res == EHANGUP))
345 return;
346
347 /* Answer the message */
348 answer_call(callid, res, &answer, answer_count);
349 }
350}
351
352/** Starts the module.
353 *
354 * @param argc The count of the command line arguments. Ignored parameter.
355 * @param argv The command line parameters. Ignored parameter.
356 *
357 * @returns EOK on success.
358 * @returns Other error codes as defined for each specific module start function.
359 *
360 */
361int main(int argc, char *argv[])
362{
363 int rc;
364
365 /* Start the module */
366 rc = netif_module_start(netif_client_connection);
367 return rc;
368}
369
370/** @}
371 */
Note: See TracBrowser for help on using the repository browser.