1 |
|
/* |
2 |
|
* ether_beos.cpp - Ethernet device driver, BeOS specific stuff |
3 |
|
* |
4 |
< |
* Basilisk II (C) 1997-2001 Christian Bauer |
5 |
< |
* Portions (C) 1997-1999 Marc Hellwig |
4 |
> |
* Basilisk II (C) 1997-2002 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 |
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> |
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" |
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); |
368 |
|
// Copy packet to buffer |
369 |
|
int len = ether_wds_to_buffer(wds, p->data); |
370 |
|
|
366 |
– |
// Set source address |
367 |
– |
memcpy(p->data + 6, ether_addr, 6); |
368 |
– |
|
371 |
|
#if MONITOR |
372 |
|
bug("Sending Ethernet packet:\n"); |
373 |
|
for (int i=0; i<len; i++) { |
387 |
|
|
388 |
|
|
389 |
|
/* |
390 |
< |
* Packet reception thread |
390 |
> |
* Packet reception thread (non-UDP) |
391 |
|
*/ |
392 |
|
|
393 |
|
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 |
|
|
462 |
|
{ |
463 |
|
D(bug("EtherIRQ\n")); |
464 |
|
|
465 |
< |
// Call protocol handler for received packets |
466 |
< |
net_packet *p = &net_buffer_ptr->read[rd_pos]; |
467 |
< |
while (p->cmd & IN_USE) { |
468 |
< |
if ((p->cmd >> 8) == SHEEP_PACKET) { |
465 |
> |
if (udp_tunnel) { |
466 |
> |
|
467 |
> |
uint8 packet[1514]; |
468 |
> |
ssize_t length; |
469 |
> |
|
470 |
> |
// Read packets from socket and hand to ether_udp_read() for processing |
471 |
> |
while (true) { |
472 |
> |
struct sockaddr_in from; |
473 |
> |
socklen_t from_len = sizeof(from); |
474 |
> |
length = recvfrom(fd, packet, 1514, 0, (struct sockaddr *)&from, &from_len); |
475 |
> |
if (length < 14) |
476 |
> |
break; |
477 |
> |
ether_udp_read(packet, length, &from); |
478 |
> |
} |
479 |
> |
|
480 |
> |
} else { |
481 |
> |
|
482 |
> |
// Call protocol handler for received packets |
483 |
> |
net_packet *p = &net_buffer_ptr->read[rd_pos]; |
484 |
> |
while (p->cmd & IN_USE) { |
485 |
> |
if ((p->cmd >> 8) == SHEEP_PACKET) { |
486 |
|
#if MONITOR |
487 |
< |
bug("Receiving Ethernet packet:\n"); |
488 |
< |
for (int i=0; i<p->length; i++) { |
489 |
< |
bug("%02x ", p->data[i]); |
490 |
< |
} |
491 |
< |
bug("\n"); |
487 |
> |
bug("Receiving Ethernet packet:\n"); |
488 |
> |
for (int i=0; i<p->length; i++) { |
489 |
> |
bug("%02x ", p->data[i]); |
490 |
> |
} |
491 |
> |
bug("\n"); |
492 |
|
#endif |
493 |
< |
// Get packet type |
494 |
< |
uint16 type = ntohs(*(uint16 *)(p->data + 12)); |
493 |
> |
// Get packet type |
494 |
> |
uint16 type = ntohs(*(uint16 *)(p->data + 12)); |
495 |
|
|
496 |
< |
// Look for protocol |
497 |
< |
NetProtocol *prot = find_protocol(type); |
498 |
< |
if (prot == NULL) |
499 |
< |
goto next; |
500 |
< |
|
501 |
< |
// No default handler |
502 |
< |
if (prot->handler == 0) |
503 |
< |
goto next; |
504 |
< |
|
505 |
< |
// Copy header to RHA |
506 |
< |
Host2Mac_memcpy(ether_data + ed_RHA, p->data, 14); |
507 |
< |
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))); |
508 |
< |
|
509 |
< |
// Call protocol handler |
510 |
< |
M68kRegisters r; |
511 |
< |
r.d[0] = type; // Packet type |
512 |
< |
r.d[1] = p->length - 14; // Remaining packet length (without header, for ReadPacket) |
513 |
< |
r.a[0] = (uint32)p->data + 14; // Pointer to packet (host address, for ReadPacket) |
514 |
< |
r.a[3] = ether_data + ed_RHA + 14; // Pointer behind header in RHA |
515 |
< |
r.a[4] = ether_data + ed_ReadPacket; // Pointer to ReadPacket/ReadRest routines |
516 |
< |
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])); |
517 |
< |
Execute68k(prot->handler, &r); |
496 |
> |
// Look for protocol |
497 |
> |
NetProtocol *prot = find_protocol(type); |
498 |
> |
if (prot == NULL) |
499 |
> |
goto next; |
500 |
> |
|
501 |
> |
// No default handler |
502 |
> |
if (prot->handler == 0) |
503 |
> |
goto next; |
504 |
> |
|
505 |
> |
// Copy header to RHA |
506 |
> |
Host2Mac_memcpy(ether_data + ed_RHA, p->data, 14); |
507 |
> |
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))); |
508 |
> |
|
509 |
> |
// Call protocol handler |
510 |
> |
M68kRegisters r; |
511 |
> |
r.d[0] = type; // Packet type |
512 |
> |
r.d[1] = p->length - 14; // Remaining packet length (without header, for ReadPacket) |
513 |
> |
r.a[0] = (uint32)p->data + 14; // Pointer to packet (host address, for ReadPacket) |
514 |
> |
r.a[3] = ether_data + ed_RHA + 14; // Pointer behind header in RHA |
515 |
> |
r.a[4] = ether_data + ed_ReadPacket; // Pointer to ReadPacket/ReadRest routines |
516 |
> |
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])); |
517 |
> |
Execute68k(prot->handler, &r); |
518 |
> |
} |
519 |
> |
next: p->cmd = 0; // Free packet |
520 |
> |
rd_pos = (rd_pos + 1) % READ_PACKET_COUNT; |
521 |
> |
p = &net_buffer_ptr->read[rd_pos]; |
522 |
|
} |
450 |
– |
next: p->cmd = 0; // Free packet |
451 |
– |
rd_pos = (rd_pos + 1) % READ_PACKET_COUNT; |
452 |
– |
p = &net_buffer_ptr->read[rd_pos]; |
523 |
|
} |
524 |
|
D(bug(" EtherIRQ done\n")); |
525 |
|
} |