int rtl8169_detect(rtl8169 **rtl8169_list) { unsigned int i, j; //pci_module_hooks *pci; //pci_info pinfo; rtl8169 *r; *rtl8169_list = NULL; if(module_get(PCI_BUS_MODULE_NAME, 0, (void **)(void *)&pci) < 0) { SHOW_INFO0(1, "rtl8169_detect: no pci bus found..\n"); return -1; } for (i = 0; pci->get_nth_pci_info(i, &pinfo) >= NO_ERROR; i++) { for (j = 0; j < sizeof(match)/sizeof(match[0]); j++) { if (pinfo.vendor_id == match[j].vendor && pinfo.device_id == match[j].device) { // we found one SHOW_INFO(1, "rtl8169_detect: found device at pci %d:%d:%d\n", pinfo.bus, pinfo.device, pinfo.function); r = kmalloc(sizeof(rtl8169)); if (r == NULL) { SHOW_ERROR0(1, "rtl8169_detect: error allocating memory for rtl8169 structure\n"); continue; } memset(r, 0, sizeof(rtl8169)); r->irq = pinfo.u.h0.interrupt_line; // find the memory-mapped base int range; for (range = 0; range < 6; range++) { if (pinfo.u.h0.base_registers[range] > 0xffff) { r->phys_base = pinfo.u.h0.base_registers[range]; r->phys_size = pinfo.u.h0.base_register_sizes[range]; break; } else if (pinfo.u.h0.base_registers[range] > 0) { r->io_port = pinfo.u.h0.base_registers[range]; } } if (r->phys_base == 0) { kfree(r); r = NULL; continue; } SHOW_INFO(1, "detected rtl8169 at irq %d, memory base 0x%lx, size 0x%lx, io base 0x%lx\n", r->irq, r->phys_base, r->phys_size, r->io_port); // add it to the list r->next = *rtl8169_list; *rtl8169_list = r; } } } module_put(PCI_BUS_MODULE_NAME); return *rtl8169_list ? 0 : ERR_NOT_FOUND; }
/** * @brief Useless interface to project of ChinaTelecom * @param env * @param thisz * @return */ int Java_com_pma_ex_jni_MappingNative_MappingStop(JNIEnv* env, jobject thisz) { SHOW_INFO("liufei : virtual device: MappingStop"); C_RESULT ret = 0; return ret; }
/** * @brief Transform and post the joystick information to libcloud * @param env * @param thisz * @param handle * @param key_value * @return */ int Java_com_pma_ex_jni_MappingNative_MappingJoystickInput( JNIEnv* env, jobject thisz, jint handle, jbyteArray key_value) { if(global_count % 100 == 0) { global_count++; } C_U8 tmp[11] = {0}; int ret = 0; C_JoyStick key_joystick; memset(&key_joystick, 0, sizeof(C_JoyStick)); (*env)->GetByteArrayRegion(env, key_value, 0, 11, tmp); key_joystick.handle_ = (C_U32)handle; key_joystick.uJoystickId = 1; key_joystick.uX = tmp[0]; key_joystick.uY = tmp[1]; key_joystick.uZ = tmp[2]; key_joystick.uRx = tmp[3]; key_joystick.uRy = tmp[4]; key_joystick.uRz = tmp[5]; key_joystick.uHatSwitch = tmp[6]; memcpy(&(key_joystick.uButton), &tmp[7], 4); Cloud_OnKey(KeyType_Joystick, sizeof(C_JoyStick), (C_U8*)&key_joystick); SHOW_INFO("liufei : virtual device: MappingJoystickInput, handle:%x, %x, %x, %x, %x, %x, %x, %d, %d, %d, %d, %d", handle, key_joystick.uX, key_joystick.uY, key_joystick.uZ, key_joystick.uRx, key_joystick.uRy, key_joystick.uRz, key_joystick.uHatSwitch, key_joystick.uButton[0], key_joystick.uButton[1], key_joystick.uButton[2], key_joystick.uButton[3]); return ret; }
// Cancel the process number obtained from // the top of the stack int32_t threadKill(ContextType *context,int32_t value) { UNUSED(value); int32_t nth; // Try to get the thread number if (PstackPop(context,&nth)) return 0; // Check number range if ((nth<1)||(nth>MAX_THREADS)) { if (SHOW_ERROR(context)) runtimeErrorMessage(context,"Invalid thread number"); return 0; } // Check if this thread is active if (FThreads[nth-1].status==FTS_NONE) { if (SHOW_ERROR(context)) consoleErrorMessage(context,"This thread is not running"); return 0; } // Set abort bit (FThreads[nth-1].context.Flags)|=CFLAG_ABORT; // Show info if enabled if (SHOW_INFO(context)) consolePrintf("Thread [%d] set to abort%s",nth,BREAK); return 0; }
// gather list of supported devices // (currently, we rely on proper BIOS detection, which // only works for primary graphics adapter, so multiple // devices won't really work) void Radeon_ProbeDevices( void ) { uint32 pci_index = 0; uint32 count = 0; device_info *di = devices->di; while( count < MAX_DEVICES ) { memset( di, 0, sizeof( *di )); if( (*pci_bus->get_nth_pci_info)(pci_index, &(di->pcii)) != B_NO_ERROR) break; if( probeDevice( di )) { devices->device_names[2*count] = di->name; devices->device_names[2*count+1] = di->video_name; di++; count++; } pci_index++; } devices->count = count; devices->device_names[2*count] = NULL; SHOW_INFO( 0, "%ld supported devices", count ); }
static bool scsi_alloc_dma_buffer_sg_orig(dma_buffer *buffer, size_t size) { // free old list first scsi_free_dma_buffer_sg_orig(buffer); size = (size * sizeof(physical_entry) + B_PAGE_SIZE - 1) & ~(B_PAGE_SIZE - 1); buffer->sg_orig = create_area("S/G to original data", (void **)&buffer->sg_list_orig, B_ANY_KERNEL_ADDRESS, size, B_NO_LOCK, 0); if (buffer->sg_orig < 0) { SHOW_ERROR(2, "Cannot S/G list buffer to original data of %" B_PRIuSIZE " bytes", size); return false; } buffer->sg_count_max_orig = size / sizeof(physical_entry); SHOW_INFO(3, "Got up to %" B_PRIuSIZE " S/G entries to original data", buffer->sg_count_max_orig); return true; }
/** * @brief Useless interface to project of ChinaTelecom * @param env * @param thisz * @param protocol * @param server_ip * @param server_port * @param session_id * @param cmpaid * @param flag * @return */ int Java_com_pma_ex_jni_MappingNative_MappingStart(JNIEnv* env, jobject thisz, jint protocol, jint server_ip, jshort server_port, jbyteArray session_id, jstring cmpaid, jint flag) { SHOW_INFO("liufei : virtual device: MappingStart"); C_RESULT ret = 0; return ret; }
errno_t lookup_fs(phantom_disk_partition_t *p) { char pname[128]; partGetName( p, pname, sizeof(pname) ); SHOW_INFO( 0, "Look for filesystems on partition %s", pname ); unsigned int i; for( i = 0; i < sizeof(fs_drivers)/sizeof(fs_probe_t); i++ ) { fs_probe_t *fp = &fs_drivers[i]; SHOW_INFO( 0, "probe %s fs on %s", fp->name, pname ); errno_t ret = fp->probe_f( p ); if( ret ) continue; SHOW_INFO( 0, "%s file sysem found on partition %s", fp->name, pname ); if(!fp->use_f) { SHOW_ERROR( 0, "%s file sysem is not implemented yet", fp->name ); continue; } #if FS_START_THREAD // BUG HACK - activate phantom fs syncronously, or else we will die attempting to use it if(fp->use_f == fs_use_phantom) fp->use_f( p ); else hal_start_kernel_thread_arg( (void (*)(void *))fp->use_f, p ); #else ret = fp->use_f( p ); if( ret ) { SHOW_ERROR( 0, "%s file sysem driver rejected partition %s", fp->name, pname ); continue; } SHOW_INFO( 0, "%s file sysem driver occupies partition %s", fp->name, pname ); #endif return 0; } return EINVAL; }
/** * @brief Initialize the global references * @param env * @param thiz */ void Java_com_pma_ex_jni_MappingNative_mappingNativeInit(JNIEnv* env, jobject thiz) { SHOW_INFO("liufei : virtual device: mappingNativeInit"); g_env = env ; mObjRef = (*env)->NewGlobalRef(env, thiz); mClzRef = (*env)->GetObjectClass(env, mObjRef); mClzRef = (*env)->NewGlobalRef(env, mClzRef); Cloud_set_output_hid_callback(transform_HID_information); }
static errno_t auto_run( const char *mpath ) { errno_t err; SHOW_INFO( 0, "Atthempt autorun @ %s", mpath ); int pid = uu_create_process(-1); const char* av[] = { "sh", "-s", "phantom.rc", 0 }; uu_proc_setargs( pid, av, 0 ); char run_path[512]; snprintf( run_path, sizeof(run_path), "%s/bin/sh", mpath ); SHOW_FLOW( 4, "Attempt to run shell @ %s", run_path ); err = uu_run_file( pid, run_path ); if(!err) SHOW_INFO( 0, "Autorun shell @ %s", run_path ); // FIXME TODO ERR pid is not freed if no shell found snprintf( run_path, sizeof(run_path), "%s/autorun.inf", mpath ); SHOW_FLOW( 4, "Attempt to run autorun.inf @ %s err = %d", run_path, err ); void *arinf = 0; size_t arinf_sz; err = k_load_file( &arinf, &arinf_sz, run_path ); if( (!err) && arinf_sz && arinf ) { SHOW_INFO( 0, "Autorun autorun.inf @ %s", run_path ); char *arun = arinf; SHOW_INFO( 0, "Autorun text='%s'", arun ); if( strnicmp( arun, "[autorun]", 9 ) ) goto noarun; } noarun: if( arinf_sz && arinf ) { arinf_sz = 0; free(arinf); arinf = 0; } return 0; }
/** * @brief Transform and post the keyboard information to libcloud * @param env * @param thisz * @param handle * @param modifier_key * @param key_code * @return */ int Java_com_pma_ex_jni_MappingNative_MappingKeyboardInput( JNIEnv* env, jobject thisz, jint handle, jbyte modifier_key, jbyteArray key_code) { SHOW_INFO("liufei : virtual device: MappingKeyboardInput . handle :%d", handle); if(global_count % 100 == 0) { global_count++; } C_Keyboard key_keybord; int ret = 0; memset(&key_keybord, 0, sizeof(key_keybord)); (*env)->GetByteArrayRegion(env, key_code, 0, 6, key_keybord.uKeyValue); key_keybord.handle_ = (C_U32)handle; key_keybord.uModifierKey = (C_U8)modifier_key; key_keybord.uLeds = 0; SHOW_INFO("liufei : virtual device: MappingKeyboardInput . key code: %d", key_keybord.uKeyValue[0]); Cloud_OnKey(KeyType_Keyboard, sizeof(C_Keyboard), (C_U8*)&key_keybord); return ret; }
static int smp_process_pending_ici(int curr_cpu) { struct smp_msg *msg; bool do_halt = false; int source_mailbox = 0; int retval = INT_NO_RESCHEDULE; msg = smp_check_for_message(curr_cpu, &source_mailbox); if(msg == 0) return retval; // dprintf(" message = %d\n", msg->message); switch(msg->message) { case SMP_MSG_INVL_PAGE_RANGE: arch_cpu_invalidate_TLB_range((addr_t)msg->data, (addr_t)msg->data2); break; case SMP_MSG_INVL_PAGE_LIST: arch_cpu_invalidate_TLB_list((addr_t *)msg->data, (int)msg->data2); break; case SMP_MSG_GLOBAL_INVL_PAGE: //arch_cpu_global_TLB_invalidate(); invltlb(); break; case SMP_MSG_RESCHEDULE: retval = INT_RESCHEDULE; break; case SMP_MSG_CPU_HALT: do_halt = true; SHOW_INFO( 0, "cpu %d halted!", curr_cpu); break; case SMP_MSG_1: default: SHOW_ERROR( 0, "smp_intercpu_int_handler: got unknown message %d", msg->message); } // finish dealing with this message, possibly removing it from the list smp_finish_message_processing(curr_cpu, msg, source_mailbox); // special case for the halt message // we otherwise wouldn't have gotten the opportunity to clean up if(do_halt) { //int_disable_interrupts(); hal_cli(); halt(); for(;;); } return retval; }
static int stringarg( const char *arg ) { if( *arg == '-' ) arg++; if( *arg == '-' ) arg++; SHOW_INFO( 5, "arg = %s", arg ); ISARG("pause", debug_boot_pause ); ISARG("novesa", bootflag_no_vesa ); ISARG("nocom", bootflag_no_comcon ); ISARG("unattended", bootflag_unattended ); return 0; }
/** * @brief Transform and post the remote control information to libcloud * @param env * @param thisz * @param key_code * @return */ int Java_com_pma_ex_jni_MappingNative_MappingRemoteControlInput( JNIEnv* env, jobject thisz, jbyteArray key_code) { SHOW_INFO("liufei : virtual device: MappingRemoteControlInput ."); if(global_count % 100 == 0) { global_count++; } int ret = 0; C_RC key_rc; C_U8 keytmp[6] = {0}; memset(&key_rc, 0, sizeof(key_rc)); (*env)->GetByteArrayRegion(env, key_code, 0, 6, keytmp); key_rc.uKeyValue = keytmp[0]; key_rc.uButton = 1; Cloud_OnKey(KeyType_RC, sizeof(C_RC), (C_U8 *)&key_rc); SHOW_INFO("liufei : virtual device: MappingRemoteControlInput . code : %d", keytmp[0]); key_rc.uButton = 0; Cloud_OnKey(KeyType_RC, sizeof(C_RC), (C_U8 *)&key_rc); return ret; }
// Indicates if there is anything running in the background int32_t anythingBackground(void) { int32_t i, any=0; for (i=0;i<MAX_THREADS;i++) { if (FThreads[i].status!=FTS_NONE) { if (SHOW_INFO((&MainContext))) {consolePrintf("Thread [%d] is active%s",i+1,BREAK);} any=1; } } return any; }
/** * @brief Called by java virtual machine * @param vm * @param reserved * @return */ jint JNI_OnLoad(JavaVM* vm, void* reserved) { SHOW_INFO("liufei : virtual device: JNI_OnLoad"); jint result = -1; g_vm = vm ; if ((*vm)->GetEnv(vm,(void**) &g_env, JNI_VERSION_1_4) != JNI_OK) { //LOGE("ERROR: GetEnv failed\n"); goto bail; } /* success -- return valid version number */ result = JNI_VERSION_1_4; bail: return result; }
// check, whether there is *any* supported card plugged in bool Radeon_CardDetect( void ) { long pci_index = 0; pci_info pcii; bool found_one = FALSE; if (get_module(B_PCI_MODULE_NAME, (module_info **)&pci_bus) != B_OK) return B_ERROR; while ((*pci_bus->get_nth_pci_info)(pci_index, &pcii) == B_NO_ERROR) { int vendor = 0; while (SupportedVendors[vendor].vendor_id) { if (SupportedVendors[vendor].vendor_id == pcii.vendor_id) { RadeonDevice *devices = SupportedVendors[vendor].devices; while (devices->device_id) { if (devices->device_id == pcii.device_id ) { rom_info ri; if( Radeon_MapBIOS( &pcii, &ri ) == B_OK ) { Radeon_UnmapBIOS( &ri ); SHOW_INFO( 0, "found supported device pci index %ld, device 0x%04x/0x%04x", pci_index, pcii.vendor_id, pcii.device_id ); found_one = TRUE; goto done; } } devices++; } } vendor++; } pci_index++; } SHOW_INFO0( 0, "no supported devices found" ); done: put_module(B_PCI_MODULE_NAME); return (found_one ? B_OK : B_ERROR); }
errno_t auto_mount( const char *name, uufs_t *fs, char *out_mnt_path, size_t outsz, int flags ) { static int am_index = 0; static char mpath[32]; snprintf( mpath, sizeof(mpath), "/amnt%d", am_index ++ ); SHOW_INFO( 0, "Partition %s mounted @ %s", name, mpath ); if( outsz && out_mnt_path ) strlcpy( out_mnt_path, mpath, outsz ); errno_t rc = add_mount( mpath, name, fs ); if( !rc && (flags & AUTO_MOUNT_FLAG_AUTORUN) ) hal_start_kernel_thread_arg( auto_run_thread, strdup(mpath) ); return rc; }
static errno_t check_pl192_sanity(int iobase) { u_int32_t id = R32(iobase+VIC_REG_HID0); if( (id & 0xFF) != 0x192 ) { SHOW_ERROR( 0, "Part number is %x", id & 0xFF ); return ENXIO; } SHOW_INFO( 1, "Id is %x", id ); check_reg(iobase+VIC_REG_PCELLID0, 0x0D ); check_reg(iobase+VIC_REG_PCELLID1, 0xF0 ); check_reg(iobase+VIC_REG_PCELLID2, 0x05 ); check_reg(iobase+VIC_REG_PCELLID3, 0xB1 ); return 0; }
// Cancel all running background threads int32_t threadKillAll(ContextType *context,int32_t value) { UNUSED(value); int32_t i; // Search all threads for(i=0;i<MAX_THREADS;i++) if (FThreads[i].status==FTS_RUNNNING) { // Set abort bit (FThreads[i].context.Flags)|=CFLAG_ABORT; // Show info if enabled if (SHOW_INFO(context)) consolePrintf("Thread [%d] set to abort%s",i+1,BREAK); } return 0; }
/** * @brief Transform and post the relative mouse information to libcloud * @param env * @param thisz * @param handle * @param button * @param x_offset * @param y_offset * @param wheel_offset * @return */ int Java_com_pma_ex_jni_MappingNative_MappingMouseRelInput( JNIEnv* env,jobject thisz, jint handle, jbyte button, jbyte x_offset, jbyte y_offset, jbyte wheel_offset) { SHOW_INFO("liufei : virtual device: MappingMouseRelInput"); if(global_count % 100 == 0) { global_count++; } C_MouseRel mouse; int ret = 0; mouse.handle_ = (C_U32)handle; mouse.uButton = (C_U8)button; mouse.uXPosition = (C_S8)x_offset; mouse.uYPosition = (C_S8)y_offset; mouse.uWheel = (C_S8)wheel_offset; Cloud_OnKey(KeyType_MouseRel, sizeof(C_MouseRel), (C_U8*)&mouse); return ret; }
/** * @brief Transform and post the absolute mouse information to libcloud * @param env * @param thisz * @param handle * @param button * @param x * @param y * @param wheel * @return */ int Java_com_pma_ex_jni_MappingNative_MappingMouseAbsPosition( JNIEnv* env, jobject thisz, jint handle, jbyte button, jshort x, jshort y, jbyte wheel) { SHOW_INFO("liufei : virtual device: MappingMouseAbsPosition"); if(global_count % 100 == 0) { global_count++; } C_MouseAbs key_mouse_abs; int ret = 0; memset(&key_mouse_abs, 0, sizeof(key_mouse_abs)); key_mouse_abs.handle_ = (C_U32)handle; key_mouse_abs.uButton = (C_U8)button; key_mouse_abs.uXPosition = (C_U16)x; key_mouse_abs.uYPosition = (C_U16)y; key_mouse_abs.uWheel = (C_S8)wheel; Cloud_OnKey(KeyType_MouseAbs, sizeof(C_MouseAbs), (C_U8*) &key_mouse_abs); return ret; }
int int_enable_irq( int shared, int irqNum, unsigned int bmAddr, unsigned int ataAddr ) { // error if interrupts enabled now // error if invalid irq number // error if bmAddr is < 100H // error if shared and bmAddr is 0 // error if ataAddr is < 100H if ( ata->int_use_intr_flag ) return 1; if ( ( irqNum < 1 ) || ( irqNum > 15 ) ) return 2; if ( irqNum == 2 ) return 2; if ( bmAddr && ( bmAddr < 0x0100 ) ) return 3; if ( shared && ( ! bmAddr ) ) return 4; if ( ataAddr < 0x0100 ) return 5; // save the input parameters //int_shared = shared; ata->int_irq_number = irqNum; ata->int_bmide_addr = bmAddr; ata->int_ata_addr = ataAddr; SHOW_INFO( 0, "IDE Set IRQ %d\n", irqNum ); if( hal_irq_alloc( irqNum, int_handler, 0, HAL_IRQ_SHAREABLE ) ) return 2; // interrupts use is now enabled ata->int_use_intr_flag = 1; //ata->int_got_it_now = 0; // Done. return 0; }
// Start device (begin using) static int com_start(phantom_device_t *dev) { int addr = dev->iobase; int unit = dev->seq_number; //take_dev_irq(dev); SHOW_INFO( 1, "start com port = %x, unit %d", addr, unit); //cominfo[unit] = dev; /* comcarrier[unit] = addr->flags;*/ //commodem[unit] = 0; outb(INTR_ENAB(addr), 0); outb(MODEM_CTL(addr), 0); while( !(inb(INTR_ID(addr))&1) ) { (void) inb(LINE_STAT (addr)); /* reset overrun error etc */ (void) inb(TXRX (addr)); /* reset data-ready */ (void) inb(MODEM_STAT(addr)); /* reset modem status reg */ } return 0; }
phantom_device_t * driver_etc_smbios_probe( const char *name, int stage ) { (void) stage; struct smbios_eps * ep = smbios_identify(); if( seq_number || ep == 0 ) return 0; if(smbios_cksum(ep)) { SHOW_ERROR0( 0, "SMBios checksum failed"); //error = ENXIO; return 0; } SHOW_INFO( 0, "SMBios Version: %u.%u", ep->SMBIOS_Major, ep->SMBIOS_Minor); if(bcd2bin(ep->SMBIOS_BCD_Revision)) SHOW_INFO( 4, "SMBios BCD Revision: %u.%u", bcd2bin(ep->SMBIOS_BCD_Revision >> 4), bcd2bin(ep->SMBIOS_BCD_Revision & 0x0f)); phantom_device_t * dev = malloc(sizeof(phantom_device_t)); dev->name = name; dev->seq_number = seq_number++; dev->drv_private = ep; dev->dops.read = smbios_read; /* dev->dops.stop = beep_stop; dev->dops.write = beep_write; */ return dev; }
phantom_device_t * driver_rtl_8169_probe( pci_cfg_t *pci, int stage ) { (void) stage; rtl8169 * nic = NULL; static int seq_number = 0; return 0; // on real hw beheaves strangely SHOW_FLOW0( 1, "probe" ); //nic = rtl8169_new(); nic = calloc(1, sizeof(rtl8169)); if (nic == NULL) { SHOW_ERROR0( 0, "out of mem"); return 0; } nic->irq = pci->interrupt; int i; for (i = 0; i < 6; i++) { if (pci->base[i] > 0xffff) { nic->phys_base = pci->base[i]; nic->phys_size = pci->size[i]; SHOW_INFO( 0, "base 0x%lx, size 0x%lx", nic->phys_base, nic->phys_size ); } else if( pci->base[i] > 0) { nic->io_port = pci->base[i]; SHOW_INFO( 0, "io_port 0x%x", nic->io_port ); } } #if 0 SHOW_FLOW0( 1, "stop" ); rtl8169_stop(nic); hal_sleep_msec(10); #endif SHOW_FLOW0( 1, "init"); if (rtl8169_init(nic) < 0) { SHOW_ERROR0( 0, "init failed"); return 0; } phantom_device_t * dev = malloc(sizeof(phantom_device_t)); dev->name = "rtl8169"; dev->seq_number = seq_number++; dev->drv_private = nic; dev->dops.read = rtl8169_read; dev->dops.write = rtl8169_write; dev->dops.get_address = rtl8169_get_address; ifnet *interface; if( if_register_interface( IF_TYPE_ETHERNET, &interface, dev) ) { SHOW_ERROR( 0, "Failed to register interface for %s", dev->name ); } else { if_simple_setup( interface, WIRED_ADDRESS, WIRED_NETMASK, WIRED_BROADCAST, WIRED_NET, WIRED_ROUTER, DEF_ROUTE_ROUTER ); } return dev; }
static int rtl8169_init(rtl8169 *r) { //bigtime_t time; int err = -1; //addr_t temp; //int i; hal_mutex_init(&r->lock,DEBUG_MSG_PREFIX); SHOW_FLOW(2, "rtl8169_init: r %p\n", r); /* r->region = vm_map_physical_memory(vm_get_kernel_aspace_id(), "rtl8169_region", (void **)&r->virt_base, REGION_ADDR_ANY_ADDRESS, r->phys_size, LOCK_KERNEL|LOCK_RW, r->phys_base); if(r->region < 0) { SHOW_ERROR0(1, "rtl8169_init: error creating memory mapped region\n"); err = -1; goto err; }*/ size_t n_pages = BYTES_TO_PAGES(r->phys_size); hal_alloc_vaddress( (void **)&r->virt_base, n_pages); // alloc address of a page, but not memory hal_pages_control_etc( r->phys_base, (void *)r->virt_base, n_pages, page_map_io, page_rw, 0 ); SHOW_INFO(2, "rtl8169 mapped at address 0x%lx\n", r->virt_base); #if 0 /* create regions for tx and rx descriptors */ r->rxdesc_region = vm_create_anonymous_region(vm_get_kernel_aspace_id(), "rtl8169_rxdesc", (void **)&r->rxdesc, REGION_ADDR_ANY_ADDRESS, NUM_RX_DESCRIPTORS * DESCRIPTOR_LEN, REGION_WIRING_WIRED_CONTIG, LOCK_KERNEL|LOCK_RW); r->rxdesc_phys = vtophys(r->rxdesc); SHOW_INFO(2, "rtl8169: rx descriptors at %p, phys 0x%x\n", r->rxdesc, r->rxdesc_phys); r->txdesc_region = vm_create_anonymous_region(vm_get_kernel_aspace_id(), "rtl8169_txdesc", (void **)&r->txdesc, REGION_ADDR_ANY_ADDRESS, NUM_TX_DESCRIPTORS * DESCRIPTOR_LEN, REGION_WIRING_WIRED_CONTIG, LOCK_KERNEL|LOCK_RW); r->txdesc_phys = vtophys(r->txdesc); SHOW_INFO(2, "rtl8169: tx descriptors at %p, phys 0x%x\n", r->txdesc, r->txdesc_phys); r->reg_spinlock = 0; /* create a large tx and rx buffer for the descriptors to point to */ r->rxbuf_region = vm_create_anonymous_region(vm_get_kernel_aspace_id(), "rtl8169_rxbuf", (void **)&r->rxbuf, REGION_ADDR_ANY_ADDRESS, NUM_RX_DESCRIPTORS * BUFSIZE_PER_FRAME, REGION_WIRING_WIRED, LOCK_KERNEL|LOCK_RW); r->txbuf_region = vm_create_anonymous_region(vm_get_kernel_aspace_id(), "rtl8169_txbuf", (void **)&r->txbuf, REGION_ADDR_ANY_ADDRESS, NUM_TX_DESCRIPTORS * BUFSIZE_PER_FRAME, REGION_WIRING_WIRED, LOCK_KERNEL|LOCK_RW); #endif hal_pv_alloc( &r->rxdesc_phys, (void**)&r->rxdesc, NUM_RX_DESCRIPTORS * DESCRIPTOR_LEN ); hal_pv_alloc( &r->txdesc_phys, (void**)&r->txdesc, NUM_TX_DESCRIPTORS * DESCRIPTOR_LEN ); SHOW_INFO(2, "rx descriptors at %p, phys 0x%x\n", r->rxdesc, r->rxdesc_phys); SHOW_INFO(2, "tx descriptors at %p, phys 0x%x\n", r->txdesc, r->txdesc_phys); hal_pv_alloc( &r->rxbuf_phys, (void**)&r->rxbuf, NUM_RX_DESCRIPTORS * BUFSIZE_PER_FRAME ); hal_pv_alloc( &r->txbuf_phys, (void**)&r->txbuf, NUM_TX_DESCRIPTORS * BUFSIZE_PER_FRAME ); /* create a receive sem */ hal_sem_init( &r->rx_sem, "rtl8169 rx_sem"); /* transmit sem */ hal_sem_init( &r->tx_sem, "rtl8169 tx_sem"); /* reset the chip */ int repeats = 100; RTL_WRITE_8(r, REG_CR, (1<<4)); // reset the chip, disable tx/rx do { hal_sleep_msec(10); // 10ms if(repeats -- <= 0 ) break; } while(RTL_READ_8(r, REG_CR) & (1<<4)); /* read in the mac address */ r->mac_addr[0] = RTL_READ_8(r, REG_IDR0); r->mac_addr[1] = RTL_READ_8(r, REG_IDR1); r->mac_addr[2] = RTL_READ_8(r, REG_IDR2); r->mac_addr[3] = RTL_READ_8(r, REG_IDR3); r->mac_addr[4] = RTL_READ_8(r, REG_IDR4); r->mac_addr[5] = RTL_READ_8(r, REG_IDR5); SHOW_INFO(2, "rtl8169: mac addr %x:%x:%x:%x:%x:%x\n", r->mac_addr[0], r->mac_addr[1], r->mac_addr[2], r->mac_addr[3], r->mac_addr[4], r->mac_addr[5]); /* some voodoo from BSD driver */ RTL_WRITE_16(r, REG_CCR, RTL_READ_16(r, REG_CCR)); RTL_SETBITS_16(r, REG_CCR, 0x3); /* mask all interrupts */ RTL_WRITE_16(r, REG_IMR, 0); /* set up the tx/rx descriptors */ rtl8169_setup_descriptors(r); /* enable tx/rx */ RTL_SETBITS_8(r, REG_CR, (1<<3)|(1<<2)); /* set up the rx state */ /* 1024 byte dma threshold, 1024 dma max burst, CRC calc 8 byte+, accept all packets */ RTL_WRITE_32(r, REG_RCR, (1<<16) | (6<<13) | (6<<8) | (0xf << 0)); RTL_SETBITS_16(r, REG_CCR, (1<<5)); // rx checksum enable RTL_WRITE_16(r, REG_RMS, 1518); // rx mtu /* set up the tx state */ RTL_WRITE_32(r, REG_TCR, (RTL_READ_32(r, REG_TCR) & ~0x1ff) | (6<<8)); // 1024 max burst dma RTL_WRITE_8(r, REG_MTPS, 0x3f); // max tx packet size (must be careful to not actually transmit more than mtu) /* set up the interrupt handler */ //int_set_io_interrupt_handler(r->irq, &rtl8169_int, r, "rtl8169"); if(hal_irq_alloc( r->irq, &rtl8169_int, r, HAL_IRQ_SHAREABLE )) { SHOW_ERROR( 0, "unable to allocate irq %d", r->irq ); goto err1; } /* clear all pending interrupts */ RTL_WRITE_16(r, REG_ISR, 0xffff); /* unmask interesting interrupts */ RTL_WRITE_16(r, REG_IMR, IMR_SYSERR | IMR_LINKCHG | IMR_TER | IMR_TOK | IMR_RER | IMR_ROK | IMR_RXOVL); return 0; err1: // TODO free what? //vm_delete_region(vm_get_kernel_aspace_id(), r->region); //err: return err; }
// init GATT (could be used for both PCI and AGP) static status_t initGATT( GART_info *gart ) { area_id map_area; uint32 map_area_size; physical_entry *map; physical_entry PTB_map[1]; size_t map_count; uint32 i; uint32 *gatt_entry; size_t num_pages; SHOW_FLOW0( 3, "" ); num_pages = (gart->buffer.size + B_PAGE_SIZE - 1) & ~(B_PAGE_SIZE - 1); // GART must be contiguous gart->GATT.area = create_area("Radeon GATT", (void **)&gart->GATT.ptr, B_ANY_KERNEL_ADDRESS, (num_pages * sizeof( uint32 ) + B_PAGE_SIZE - 1) & ~(B_PAGE_SIZE - 1), B_32_BIT_CONTIGUOUS, // TODO: Physical address is cast to 32 bit below! Use B_CONTIGUOUS, // when that is (/can be) fixed! #ifdef HAIKU_TARGET_PLATFORM_HAIKU // TODO: really user read/write? B_READ_AREA | B_WRITE_AREA | B_USER_CLONEABLE_AREA #else 0 #endif ); if (gart->GATT.area < 0) { SHOW_ERROR(1, "cannot create GATT table (%s)", strerror(gart->GATT.area)); return gart->GATT.area; } get_memory_map(gart->GATT.ptr, B_PAGE_SIZE, PTB_map, 1); gart->GATT.phys = PTB_map[0].address; SHOW_INFO(3, "GATT_ptr=%p, GATT_phys=%p", gart->GATT.ptr, (void *)gart->GATT.phys); // get address mapping memset(gart->GATT.ptr, 0, num_pages * sizeof(uint32)); map_count = num_pages + 1; // align size to B_PAGE_SIZE map_area_size = map_count * sizeof(physical_entry); if ((map_area_size / B_PAGE_SIZE) * B_PAGE_SIZE != map_area_size) map_area_size = ((map_area_size / B_PAGE_SIZE) + 1) * B_PAGE_SIZE; // temporary area where we fill in the memory map (deleted below) map_area = create_area("pci_gart_map_area", (void **)&map, B_ANY_ADDRESS, map_area_size, B_FULL_LOCK, B_READ_AREA | B_WRITE_AREA); // TODO: We actually have a working malloc() in the kernel. Why create // an area? dprintf("pci_gart_map_area: %ld\n", map_area); get_memory_map( gart->buffer.ptr, gart->buffer.size, map, map_count ); // the following looks a bit strange as the kernel // combines successive entries gatt_entry = gart->GATT.ptr; for( i = 0; i < map_count; ++i ) { phys_addr_t addr = map[i].address; size_t size = map[i].size; if( size == 0 ) break; while( size > 0 ) { *gatt_entry++ = addr; //SHOW_FLOW( 3, "%lx", *(gart_entry-1) ); addr += ATI_PCIGART_PAGE_SIZE; size -= ATI_PCIGART_PAGE_SIZE; } } delete_area(map_area); if( i == map_count ) { // this case should never happen SHOW_ERROR0( 0, "memory map of GART buffer too large!" ); delete_area( gart->GATT.area ); gart->GATT.area = -1; return B_ERROR; } // this might be a bit more than needed, as // 1. Intel CPUs have "processor order", i.e. writes appear to external // devices in program order, so a simple final write should be sufficient // 2. if it is a PCI GART, bus snooping should provide cache coherence // 3. this function is a no-op :( clear_caches( gart->GATT.ptr, num_pages * sizeof( uint32 ), B_FLUSH_DCACHE ); // back to real live - some chipsets have write buffers that // proove all previous assumptions wrong // (don't know whether this really helps though) asm volatile ( "wbinvd" ::: "memory" ); return B_OK; }
int rtl8169_init(rtl8169 *r) { bigtime_t time; int err = -1; addr_t temp; int i; SHOW_FLOW(2, "rtl8169_init: r %p\n", r); r->region = vm_map_physical_memory(vm_get_kernel_aspace_id(), "rtl8169_region", (void **)&r->virt_base, REGION_ADDR_ANY_ADDRESS, r->phys_size, LOCK_KERNEL|LOCK_RW, r->phys_base); if(r->region < 0) { SHOW_ERROR0(1, "rtl8169_init: error creating memory mapped region\n"); err = -1; goto err; } SHOW_INFO(2, "rtl8169 mapped at address 0x%lx\n", r->virt_base); /* create regions for tx and rx descriptors */ r->rxdesc_region = vm_create_anonymous_region(vm_get_kernel_aspace_id(), "rtl8169_rxdesc", (void **)&r->rxdesc, REGION_ADDR_ANY_ADDRESS, NUM_RX_DESCRIPTORS * DESCRIPTOR_LEN, REGION_WIRING_WIRED_CONTIG, LOCK_KERNEL|LOCK_RW); r->rxdesc_phys = vtophys(r->rxdesc); SHOW_INFO(2, "rtl8169: rx descriptors at %p, phys 0x%x\n", r->rxdesc, r->rxdesc_phys); r->txdesc_region = vm_create_anonymous_region(vm_get_kernel_aspace_id(), "rtl8169_txdesc", (void **)&r->txdesc, REGION_ADDR_ANY_ADDRESS, NUM_TX_DESCRIPTORS * DESCRIPTOR_LEN, REGION_WIRING_WIRED_CONTIG, LOCK_KERNEL|LOCK_RW); r->txdesc_phys = vtophys(r->txdesc); SHOW_INFO(2, "rtl8169: tx descriptors at %p, phys 0x%x\n", r->txdesc, r->txdesc_phys); r->reg_spinlock = 0; /* create a large tx and rx buffer for the descriptors to point to */ r->rxbuf_region = vm_create_anonymous_region(vm_get_kernel_aspace_id(), "rtl8169_rxbuf", (void **)&r->rxbuf, REGION_ADDR_ANY_ADDRESS, NUM_RX_DESCRIPTORS * BUFSIZE_PER_FRAME, REGION_WIRING_WIRED, LOCK_KERNEL|LOCK_RW); r->txbuf_region = vm_create_anonymous_region(vm_get_kernel_aspace_id(), "rtl8169_txbuf", (void **)&r->txbuf, REGION_ADDR_ANY_ADDRESS, NUM_TX_DESCRIPTORS * BUFSIZE_PER_FRAME, REGION_WIRING_WIRED, LOCK_KERNEL|LOCK_RW); /* create a receive sem */ r->rx_sem = sem_create(0, "rtl8169 rx_sem"); /* transmit sem */ r->tx_sem = sem_create(1, "rtl8169 tx_sem"); /* reset the chip */ time = system_time(); RTL_WRITE_8(r, REG_CR, (1<<4)); // reset the chip, disable tx/rx do { thread_snooze(10000); // 10ms if(system_time() - time > 1000000) { break; } } while(RTL_READ_8(r, REG_CR) & (1<<4)); /* read in the mac address */ r->mac_addr[0] = RTL_READ_8(r, REG_IDR0); r->mac_addr[1] = RTL_READ_8(r, REG_IDR1); r->mac_addr[2] = RTL_READ_8(r, REG_IDR2); r->mac_addr[3] = RTL_READ_8(r, REG_IDR3); r->mac_addr[4] = RTL_READ_8(r, REG_IDR4); r->mac_addr[5] = RTL_READ_8(r, REG_IDR5); SHOW_INFO(2, "rtl8169: mac addr %x:%x:%x:%x:%x:%x\n", r->mac_addr[0], r->mac_addr[1], r->mac_addr[2], r->mac_addr[3], r->mac_addr[4], r->mac_addr[5]); /* some voodoo from BSD driver */ RTL_WRITE_16(r, REG_CCR, RTL_READ_16(r, REG_CCR)); RTL_SETBITS_16(r, REG_CCR, 0x3); /* mask all interrupts */ RTL_WRITE_16(r, REG_IMR, 0); /* set up the tx/rx descriptors */ rtl8169_setup_descriptors(r); /* enable tx/rx */ RTL_SETBITS_8(r, REG_CR, (1<<3)|(1<<2)); /* set up the rx state */ /* 1024 byte dma threshold, 1024 dma max burst, CRC calc 8 byte+, accept all packets */ RTL_WRITE_32(r, REG_RCR, (1<<16) | (6<<13) | (6<<8) | (0xf << 0)); RTL_SETBITS_16(r, REG_CCR, (1<<5)); // rx checksum enable RTL_WRITE_16(r, REG_RMS, 1518); // rx mtu /* set up the tx state */ RTL_WRITE_32(r, REG_TCR, (RTL_READ_32(r, REG_TCR) & ~0x1ff) | (6<<8)); // 1024 max burst dma RTL_WRITE_8(r, REG_MTPS, 0x3f); // max tx packet size (must be careful to not actually transmit more than mtu) /* set up the interrupt handler */ int_set_io_interrupt_handler(r->irq, &rtl8169_int, r, "rtl8169"); /* clear all pending interrupts */ RTL_WRITE_16(r, REG_ISR, 0xffff); /* unmask interesting interrupts */ RTL_WRITE_16(r, REG_IMR, IMR_SYSERR | IMR_LINKCHG | IMR_TER | IMR_TOK | IMR_RER | IMR_ROK | IMR_RXOVL); return 0; err1: vm_delete_region(vm_get_kernel_aspace_id(), r->region); err: return err; }
int AacPcm::getInfos(MediaInfo *infos) { if(!infos) return 1; if(hDecoder) { SHOW_INFO() return 0; } IsAAC=strcmpi(infos->getFilename()+lstrlen(infos->getFilename())-4,".aac")==0; if(!IsAAC) // MP4 file --------------------------------------------------------------------- { MP4Duration length; unsigned __int32 buffer_size; mp4AudioSpecificConfig mp4ASC; if(!(mp4File=MP4Read(infos->getFilename(), 0))) ERROR_getInfos("Error opening file"); if((track=GetAACTrack(mp4File))<0) ERROR_getInfos(0); //"Unable to find correct AAC sound track"); if(!(hDecoder=faacDecOpen())) ERROR_getInfos("Error initializing decoder library"); MP4GetTrackESConfiguration(mp4File, track, (unsigned __int8 **)&buffer, &buffer_size); if(!buffer) ERROR_getInfos("MP4GetTrackESConfiguration"); AudioSpecificConfig(buffer, buffer_size, &mp4ASC); Channels=mp4ASC.channelsConfiguration; if(faacDecInit2(hDecoder, buffer, buffer_size, &Samplerate, &Channels) < 0) ERROR_getInfos("Error initializing decoder library"); FREE_ARRAY(buffer); length=MP4GetTrackDuration(mp4File, track); len_ms=(DWORD)MP4ConvertFromTrackDuration(mp4File, track, length, MP4_MSECS_TIME_SCALE); file_info.bitrate=MP4GetTrackBitRate(mp4File, track); file_info.version=MP4GetTrackAudioType(mp4File, track)==MP4_MPEG4_AUDIO_TYPE ? 4 : 2; numSamples=MP4GetTrackNumberOfSamples(mp4File, track); sampleId=1; } else // AAC file ------------------------------------------------------------------------------ { DWORD read, tmp; BYTE Channels4Raw=0; if(!(aacFile=fopen(infos->getFilename(),"rb"))) ERROR_getInfos("Error opening file"); // use bufferized stream setvbuf(aacFile,NULL,_IOFBF,32767); // get size of file fseek(aacFile, 0, SEEK_END); src_size=ftell(aacFile); fseek(aacFile, 0, SEEK_SET); if(!(buffer=(BYTE *)malloc(FAAD_STREAMSIZE))) ERROR_getInfos("Memory allocation error: buffer") tmp=src_size<FAAD_STREAMSIZE ? src_size : FAAD_STREAMSIZE; read=fread(buffer, 1, tmp, aacFile); if(read==tmp) { bytes_read=read; bytes_into_buffer=read; } else ERROR_getInfos("Read failed!") if(tagsize=id3v2_tag(buffer)) { if(tagsize>(long)src_size) ERROR_getInfos("Corrupt stream!"); if(tagsize<bytes_into_buffer) { bytes_into_buffer-=tagsize; memcpy(buffer,buffer+tagsize,bytes_into_buffer); } else { bytes_read=tagsize; bytes_into_buffer=0; if(tagsize>bytes_into_buffer) fseek(aacFile, tagsize, SEEK_SET); } if(src_size<bytes_read+FAAD_STREAMSIZE-bytes_into_buffer) tmp=src_size-bytes_read; else tmp=FAAD_STREAMSIZE-bytes_into_buffer; read=fread(buffer+bytes_into_buffer, 1, tmp, aacFile); if(read==tmp) { bytes_read+=read; bytes_into_buffer+=read; } else ERROR_getInfos("Read failed!"); } if(get_AAC_format((char *)infos->getFilename(), &file_info, &seek_table, &seek_table_length, 0)) ERROR_getInfos("get_AAC_format"); IsSeekable=file_info.headertype==ADTS && seek_table && seek_table_length>0; BlockSeeking=!IsSeekable; if(!(hDecoder=faacDecOpen())) ERROR_getInfos("Can't open library"); if(file_info.headertype==RAW) { faacDecConfiguration config; config.defSampleRate=atoi(cfg_samplerate); switch(cfg_profile[1]) { case 'a': config.defObjectType=MAIN; break; case 'o': config.defObjectType=LOW; break; case 'S': config.defObjectType=SSR; break; case 'T': config.defObjectType=LTP; break; } switch(cfg_bps[0]) { case '1': config.outputFormat=FAAD_FMT_16BIT; break; case '2': config.outputFormat=FAAD_FMT_24BIT; break; case '3': config.outputFormat=FAAD_FMT_32BIT; break; case 'F': config.outputFormat=FAAD_FMT_24BIT; break; } faacDecSetConfiguration(hDecoder, &config); if(!FindBitrate) { AacPcm *NewInst; if(!(NewInst=new AacPcm())) ERROR_getInfos("Memory allocation error: NewInst"); NewInst->FindBitrate=TRUE; if(NewInst->getInfos(infos)) ERROR_getInfos(0); Channels4Raw=NewInst->frameInfo.channels; file_info.bitrate=NewInst->file_info.bitrate*Channels4Raw; delete NewInst; } else { DWORD Samples, BytesConsumed; if((bytes_consumed=faacDecInit(hDecoder,buffer,bytes_into_buffer,&Samplerate,&Channels))<0) ERROR_getInfos("Can't init library"); bytes_into_buffer-=bytes_consumed; if(!processData(infos,0,0)) ERROR_getInfos(0); Samples=frameInfo.samples/sizeof(short); BytesConsumed=frameInfo.bytesconsumed; processData(infos,0,0); if(BytesConsumed<frameInfo.bytesconsumed) BytesConsumed=frameInfo.bytesconsumed; file_info.bitrate=(BytesConsumed*8*Samplerate)/Samples; if(!file_info.bitrate) file_info.bitrate=1000; // try to continue decoding return 0; } } if((bytes_consumed=faacDecInit(hDecoder, buffer, bytes_into_buffer, &Samplerate, &Channels))<0) ERROR_getInfos("faacDecInit failed!") bytes_into_buffer-=bytes_consumed; if(Channels4Raw) Channels=Channels4Raw; len_ms=(DWORD)((1000*((float)src_size*8))/file_info.bitrate); } SHOW_INFO(); return 0; }