ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/BeOS/ether_beos.cpp
(Generate patch)

Comparing BasiliskII/src/BeOS/ether_beos.cpp (file contents):
Revision 1.1.1.1 by cebix, 1999-10-03T14:16:25Z vs.
Revision 1.14 by gbeauche, 2008-01-01T09:40:32Z

# Line 1 | Line 1
1   /*
2   *  ether_beos.cpp - Ethernet device driver, BeOS specific stuff
3   *
4 < *  Basilisk II (C) 1997-1999 Christian Bauer
5 < *  Portions (C) 1997-1999 Marc Hellwig
4 > *  Basilisk II (C) 1997-2008 Christian Bauer
5 > *  Portions written by Marc Hellwig
6   *
7   *  This program is free software; you can redistribute it and/or modify
8   *  it under the terms of the GNU General Public License as published by
# Line 19 | Line 19
19   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20   */
21  
22 + #include "sysdeps.h"
23 +
24   #include <KernelKit.h>
25   #include <AppKit.h>
26   #include <StorageKit.h>
# Line 27 | Line 29
29   #include <stdio.h>
30   #include <stdlib.h>
31   #include <string.h>
32 + #include <sys/socket.h>
33  
31 #include "sysdeps.h"
34   #include "cpu_emulation.h"
35   #include "main.h"
36   #include "prefs.h"
# Line 64 | Line 66 | static uint32 rd_pos;                                          // Current re
66   static uint32 wr_pos;                                           // Current write position in packet buffer
67   static sem_id read_sem, write_sem;                      // Semaphores to trigger packet reading/writing
68  
69 + static int fd = -1;                                                     // UDP socket fd
70 + static bool udp_tunnel = false;
71 +
72  
73   // Prototypes
74   static status_t receive_proc(void *data);
# Line 90 | Line 95 | static NetProtocol *find_protocol(uint16
95  
96  
97   /*
98 + *  Remove all protocols
99 + */
100 +
101 + static void remove_all_protocols(void)
102 + {
103 +        NetProtocol *p;
104 +        while ((p = (NetProtocol *)prot_list.RemoveItem((long)0)) != NULL)
105 +                delete p;
106 + }
107 +
108 +
109 + /*
110   *  Initialization
111   */
112  
113 < void EtherInit(void)
113 > bool ether_init(void)
114   {
115          // Do nothing if no Ethernet device specified
116          if (PrefsFindString("ether") == NULL)
117 <                return;
117 >                return false;
118  
119          // Find net_server team
120   i_wanna_try_that_again:
# Line 136 | Line 153 | i_wanna_try_that_again:
153                  // It was found, so something else must be wrong
154                  if (sheep_net_found) {
155                          WarningAlert(GetString(STR_NO_NET_ADDON_WARN));
156 <                        return;
156 >                        return false;
157                  }
158  
159                  // Not found, inform the user
160                  if (!ChoiceAlert(GetString(STR_NET_CONFIG_MODIFY_WARN), GetString(STR_OK_BUTTON), GetString(STR_CANCEL_BUTTON)))
161 <                        return;
161 >                        return false;
162  
163                  // Change the network config file and restart the network
164                  fin = fopen("/boot/home/config/settings/network", "r");
# Line 196 | Line 213 | i_wanna_try_that_again:
213          area_id handler_buffer;
214          if ((handler_buffer = find_area("packet buffer")) < B_NO_ERROR) {
215                  WarningAlert(GetString(STR_NET_ADDON_INIT_FAILED));
216 <                return;
216 >                return false;
217          }
218          if ((buffer_area = clone_area("local packet buffer", (void **)&net_buffer_ptr, B_ANY_ADDRESS, B_READ_AREA | B_WRITE_AREA, handler_buffer)) < B_NO_ERROR) {
219                  D(bug("EtherInit: couldn't clone packet area\n"));
220                  WarningAlert(GetString(STR_NET_ADDON_CLONE_FAILED));
221 <                return;
221 >                return false;
222          }
223          if ((read_sem = create_sem(0, "ether read")) < B_NO_ERROR) {
224                  printf("FATAL: can't create Ethernet semaphore\n");
225 <                return;
225 >                return false;
226          }
227          net_buffer_ptr->read_sem = read_sem;
228          write_sem = net_buffer_ptr->write_sem;
# Line 221 | Line 238 | i_wanna_try_that_again:
238          D(bug("Ethernet address %02x %02x %02x %02x %02x %02x\n", ether_addr[0], ether_addr[1], ether_addr[2], ether_addr[3], ether_addr[4], ether_addr[5]));
239  
240          // Everything OK
241 <        net_open = true;
241 >        return true;
242   }
243  
244  
# Line 229 | Line 246 | i_wanna_try_that_again:
246   *  Deinitialization
247   */
248  
249 < void EtherExit(void)
249 > void ether_exit(void)
250   {
251 <        if (net_open) {
251 >        // Close communications with add-on
252 >        for (int i=0; i<WRITE_PACKET_COUNT; i++)
253 >                net_buffer_ptr->write[i].cmd = IN_USE | (DEACTIVATE_SHEEP_NET << 8);
254 >        release_sem(write_sem);
255  
256 <                // Close communications with add-on
257 <                for (int i=0; i<WRITE_PACKET_COUNT; i++)
258 <                        net_buffer_ptr->write[i].cmd = IN_USE | (DEACTIVATE_SHEEP_NET << 8);
259 <                release_sem(write_sem);
256 >        // Quit reception thread
257 >        ether_thread_active = false;
258 >        status_t result;
259 >        release_sem(read_sem);
260 >        wait_for_thread(read_thread, &result);
261  
262 <                // Quit reception thread
263 <                ether_thread_active = false;
264 <                status_t result;
265 <                release_sem(read_sem);
266 <                wait_for_thread(read_thread, &result);
246 <
247 <                delete_sem(read_sem);
248 <                delete_area(buffer_area);
249 <
250 <                // Remove all protocols
251 <                NetProtocol *p;
252 <                while ((p = (NetProtocol *)prot_list.RemoveItem((long)0)) != NULL)
253 <                        delete p;
254 <        }
262 >        delete_sem(read_sem);
263 >        delete_area(buffer_area);
264 >
265 >        // Remove all protocols
266 >        remove_all_protocols();
267   }
268  
269  
# Line 259 | Line 271 | void EtherExit(void)
271   *  Reset
272   */
273  
274 < void EtherReset(void)
274 > void ether_reset(void)
275   {
276 <        // Remove all protocols
265 <        NetProtocol *p;
266 <        while ((p = (NetProtocol *)prot_list.RemoveItem((long)0)) != NULL)
267 <                delete p;
276 >        remove_all_protocols();
277   }
278  
279  
# Line 274 | Line 283 | void EtherReset(void)
283  
284   int16 ether_add_multicast(uint32 pb)
285   {
277        uint8 *addr = Mac2HostAddr(pb + eMultiAddr);
286          net_packet *p = &net_buffer_ptr->write[wr_pos];
287          if (p->cmd & IN_USE) {
288                  D(bug("WARNING: Couldn't enable multicast address\n"));
289          } else {
290 <                memcpy(p->data, addr, 6);
290 >                Mac2Host_memcpy(p->data, pb + eMultiAddr, 6);
291                  p->length = 6;
292                  p->cmd = IN_USE | (ADD_MULTICAST << 8);
293                  wr_pos = (wr_pos + 1) % WRITE_PACKET_COUNT;
# Line 295 | Line 303 | int16 ether_add_multicast(uint32 pb)
303  
304   int16 ether_del_multicast(uint32 pb)
305   {
298        uint8 *addr = Mac2HostAddr(pb + eMultiAddr);
306          net_packet *p = &net_buffer_ptr->write[wr_pos];
307          if (p->cmd & IN_USE) {
308                  D(bug("WARNING: Couldn't enable multicast address\n"));
309          } else {
310 <                memcpy(p->data, addr, 6);
310 >                Mac2Host_memcpy(p->data, pb + eMultiAddr, 6);
311                  p->length = 6;
312                  p->cmd = IN_USE | (REMOVE_MULTICAST << 8);
313                  wr_pos = (wr_pos + 1) % WRITE_PACKET_COUNT;
# Line 359 | Line 366 | int16 ether_write(uint32 wds)
366          } else {
367  
368                  // Copy packet to buffer
369 <                uint8 *start;
363 <                uint8 *bp = start = p->data;
364 <                for (;;) {
365 <                        int len = ReadMacInt16(wds);
366 <                        if (len == 0)
367 <                                break;
368 <                        memcpy(bp, Mac2HostAddr(ReadMacInt32(wds + 2)), len);
369 <                        bp += len;
370 <                        wds += 6;
371 <                }
372 <
373 <                // Set source address
374 <                memcpy(start + 6, ether_addr, 6);
369 >                int len = ether_wds_to_buffer(wds, p->data);
370  
371   #if MONITOR
372                  bug("Sending Ethernet packet:\n");
373 <                for (int i=0; i<(uint32)(bp-start); i++) {
374 <                        bug("%02x ", start[i]);
373 >                for (int i=0; i<len; i++) {
374 >                        bug("%02x ", p->data[i]);
375                  }
376                  bug("\n");
377   #endif
378  
379                  // Notify add-on
380 <                p->length = (uint32)(bp - start);
380 >                p->length = len;
381                  p->cmd = IN_USE | (SHEEP_PACKET << 8);
382                  wr_pos = (wr_pos + 1) % WRITE_PACKET_COUNT;
383                  release_sem(write_sem);
# Line 392 | Line 387 | int16 ether_write(uint32 wds)
387  
388  
389   /*
390 < *  Packet reception thread
390 > *  Packet reception thread (non-UDP)
391   */
392  
393   static status_t receive_proc(void *data)
# Line 410 | Line 405 | static status_t receive_proc(void *data)
405  
406  
407   /*
408 + *  Packet reception thread (UDP)
409 + */
410 +
411 + static status_t receive_proc_udp(void *data)
412 + {
413 +        while (ether_thread_active) {
414 +                fd_set readfds;
415 +                FD_ZERO(&readfds);
416 +                FD_SET(fd, &readfds);
417 +                struct timeval timeout;
418 +                timeout.tv_sec = 1;
419 +                timeout.tv_usec = 0;
420 +                if (select(fd+1, &readfds, NULL, NULL, &timeout) > 0) {
421 +                        D(bug(" packet received, triggering Ethernet interrupt\n"));
422 +                        SetInterruptFlag(INTFLAG_ETHER);
423 +                        TriggerInterrupt();
424 +                }
425 +        }
426 +        return 0;
427 + }
428 +
429 +
430 + /*
431 + *  Start UDP packet reception thread
432 + */
433 +
434 + bool ether_start_udp_thread(int socket_fd)
435 + {
436 +        fd = socket_fd;
437 +        udp_tunnel = true;
438 +        ether_thread_active = true;
439 +        read_thread = spawn_thread(receive_proc_udp, "UDP Receiver", B_URGENT_DISPLAY_PRIORITY, NULL);
440 +        resume_thread(read_thread);
441 +        return true;
442 + }
443 +
444 +
445 + /*
446 + *  Stop UDP packet reception thread
447 + */
448 +
449 + void ether_stop_udp_thread(void)
450 + {
451 +        ether_thread_active = false;
452 +        status_t result;
453 +        wait_for_thread(read_thread, &result);
454 + }
455 +
456 +
457 + /*
458   *  Ethernet interrupt - activate deferred tasks to call IODone or protocol handlers
459   */
460  
461   void EtherInterrupt(void)
462   {
463          D(bug("EtherIRQ\n"));
464 +        EthernetPacket ether_packet;
465 +        uint32 packet = ether_packet.addr();
466 +
467 +        if (udp_tunnel) {
468 +
469 +                ssize_t length;
470  
471 <        // Call protocol handler for received packets
472 <        net_packet *p = &net_buffer_ptr->read[rd_pos];
473 <        while (p->cmd & IN_USE) {
474 <                if ((p->cmd >> 8) == SHEEP_PACKET) {
471 >                // Read packets from socket and hand to ether_udp_read() for processing
472 >                while (true) {
473 >                        struct sockaddr_in from;
474 >                        socklen_t from_len = sizeof(from);
475 >                        length = recvfrom(fd, Mac2HostAddr(packet), 1514, 0, (struct sockaddr *)&from, &from_len);
476 >                        if (length < 14)
477 >                                break;
478 >                        ether_udp_read(packet, length, &from);
479 >                }
480 >
481 >        } else {
482 >
483 >                // Call protocol handler for received packets
484 >                net_packet *p = &net_buffer_ptr->read[rd_pos];
485 >                while (p->cmd & IN_USE) {
486 >                        if ((p->cmd >> 8) == SHEEP_PACKET) {
487 >                                Host2Mac_memcpy(packet, p->data, p->length);
488   #if MONITOR
489 <                        bug("Receiving Ethernet packet:\n");
490 <                        for (int i=0; i<p->length; i++) {
491 <                                bug("%02x ", p->data[i]);
492 <                        }
493 <                        bug("\n");
489 >                                bug("Receiving Ethernet packet:\n");
490 >                                for (int i=0; i<p->length; i++) {
491 >                                        bug("%02x ", ReadMacInt8(packet + i));
492 >                                }
493 >                                bug("\n");
494   #endif
495 <                        // Get packet type
496 <                        uint16 type = ntohs(*(uint16 *)(p->data + 12));
495 >                                // Get packet type
496 >                                uint16 type = ReadMacInt16(packet + 12);
497  
498 <                        // Look for protocol
499 <                        NetProtocol *prot = find_protocol(type);
500 <                        if (prot == NULL)
501 <                                goto next;
502 <
503 <                        // No default handler
504 <                        if (prot->handler == 0)
505 <                                goto next;
506 <
507 <                        // Copy header to RHA
508 <                        memcpy(Mac2HostAddr(ether_data + ed_RHA), p->data, 14);
509 <                        D(bug(" header %08lx%04lx %08lx%04lx %04lx\n", ReadMacInt32(ether_data + ed_RHA), ReadMacInt16(ether_data + ed_RHA + 4), ReadMacInt32(ether_data + ed_RHA + 6), ReadMacInt16(ether_data + ed_RHA + 10), ReadMacInt16(ether_data + ed_RHA + 12)));
510 <
511 <                        // Call protocol handler
512 <                        M68kRegisters r;
513 <                        r.d[0] = type;                                                                  // Packet type
514 <                        r.d[1] = p->length - 14;                                                // Remaining packet length (without header, for ReadPacket)
515 <                        r.a[0] = (uint32)p->data + 14;                                  // Pointer to packet (host address, for ReadPacket)
516 <                        r.a[3] = ether_data + ed_RHA + 14;                              // Pointer behind header in RHA
517 <                        r.a[4] = ether_data + ed_ReadPacket;                    // Pointer to ReadPacket/ReadRest routines
518 <                        D(bug(" calling protocol handler %08lx, type %08lx, length %08lx, data %08lx, rha %08lx, read_packet %08lx\n", prot->handler, r.d[0], r.d[1], r.a[0], r.a[3], r.a[4]));
519 <                        Execute68k(prot->handler, &r);
498 >                                // Look for protocol
499 >                                NetProtocol *prot = find_protocol(type);
500 >                                if (prot == NULL)
501 >                                        goto next;
502 >
503 >                                // No default handler
504 >                                if (prot->handler == 0)
505 >                                        goto next;
506 >
507 >                                // Copy header to RHA
508 >                                Mac2Mac_memcpy(ether_data + ed_RHA, packet, 14);
509 >                                D(bug(" header %08lx%04lx %08lx%04lx %04lx\n", ReadMacInt32(ether_data + ed_RHA), ReadMacInt16(ether_data + ed_RHA + 4), ReadMacInt32(ether_data + ed_RHA + 6), ReadMacInt16(ether_data + ed_RHA + 10), ReadMacInt16(ether_data + ed_RHA + 12)));
510 >
511 >                                // Call protocol handler
512 >                                M68kRegisters r;
513 >                                r.d[0] = type;                                                                  // Packet type
514 >                                r.d[1] = p->length - 14;                                                // Remaining packet length (without header, for ReadPacket)
515 >                                r.a[0] = packet + 14;                                                   // Pointer to packet (Mac address, for ReadPacket)
516 >                                r.a[3] = ether_data + ed_RHA + 14;                              // Pointer behind header in RHA
517 >                                r.a[4] = ether_data + ed_ReadPacket;                    // Pointer to ReadPacket/ReadRest routines
518 >                                D(bug(" calling protocol handler %08lx, type %08lx, length %08lx, data %08lx, rha %08lx, read_packet %08lx\n", prot->handler, r.d[0], r.d[1], r.a[0], r.a[3], r.a[4]));
519 >                                Execute68k(prot->handler, &r);
520 >                        }
521 > next:           p->cmd = 0;     // Free packet
522 >                        rd_pos = (rd_pos + 1) % READ_PACKET_COUNT;
523 >                        p = &net_buffer_ptr->read[rd_pos];
524                  }
457 next:   p->cmd = 0;     // Free packet
458                rd_pos = (rd_pos + 1) % READ_PACKET_COUNT;
459                p = &net_buffer_ptr->read[rd_pos];
525          }
526          D(bug(" EtherIRQ done\n"));
527   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines