예제 #1
0
int get_pch_sbreg_addr(struct pci_access *pci, pciaddr_t *sbreg_addr) {
  MSG("Checking for a Series 10 PCH system");

  struct pci_dev *d31f1 = pci_get_dev(pci, 0, 0, 31, 1);
  pci_fill_info(d31f1, PCI_FILL_IDENT);
  if(d31f1->vendor_id == 0xffff) {
    MSG("Cannot find D31:F1, assuming it is hidden by firmware");

    uint32_t p2sb_ctrl = pci_read_long(d31f1, REG_P2SB_CTRL);
    MSG("P2SB_CTRL=%02x", p2sb_ctrl);
    if(!(p2sb_ctrl & REG_P2SB_CTRL_HIDE)) {
      ERR("D31:F1 is hidden but P2SB_E1 is not 0xff, bailing out");
    }

    MSG("Unhiding P2SB");
    pci_write_long(d31f1, REG_P2SB_CTRL, p2sb_ctrl & ~REG_P2SB_CTRL_HIDE);

    p2sb_ctrl = pci_read_long(d31f1, REG_P2SB_CTRL);
    MSG("P2SB_CTRL=%02x", p2sb_ctrl);
    if(p2sb_ctrl & REG_P2SB_CTRL_HIDE) {
      ERR("Cannot unhide PS2B");
    }

    pci_fill_info(d31f1, PCI_FILL_RESCAN | PCI_FILL_IDENT);
    if(d31f1->vendor_id == 0xffff) {
      ERR("P2SB unhidden but does not enumerate, bailing out");
    }
  }

  pci_fill_info(d31f1, PCI_FILL_RESCAN | PCI_FILL_IDENT | PCI_FILL_BASES);
  if(d31f1->vendor_id != 0x8086) {
    ERR("Vendor of D31:F1 is not Intel");
  } else if((uint32_t)d31f1->base_addr[0] == 0xffffffff) {
    ERR("SBREG_BAR is not implemented in D31:F1");
  }

  *sbreg_addr = d31f1->base_addr[0] &~ 0xf;
  MSG("SBREG_ADDR=%08lx", *sbreg_addr);

  MSG("Hiding P2SB again");
  uint32_t p2sb_ctrl = pci_read_long(d31f1, REG_P2SB_CTRL);
  pci_write_long(d31f1, REG_P2SB_CTRL, p2sb_ctrl | REG_P2SB_CTRL_HIDE);

  pci_fill_info(d31f1, PCI_FILL_RESCAN | PCI_FILL_IDENT);
  if(d31f1->vendor_id != 0xffff) {
    ERR("Cannot hide P2SB");
  }

  return 0;
}
static struct pci_dev *
create_pci_dev(struct pci_access * pci, char * slot)
{
    struct pci_filter filter;
    pci_filter_init(pci, &filter);
    if (pci_filter_parse_slot(&filter, slot))
    {
        fprintf(stderr, "Failed to parse device id %s\n", slot);
        goto pci_filter_parse_failed;
    }

    pci_init(pci);

    struct pci_dev * dev = pci_get_dev(pci,
                                       filter.domain,
                                       filter.bus,
                                       filter.slot,
                                       filter.func);
    if (! dev)
    {
        fprintf(stderr, "Failed to allocate dev\n");
        goto pci_get_dev_failed;
    }

    pci_fill_info(dev, PCI_FILL_IDENT);

    return dev;

pci_get_dev_failed:
pci_filter_parse_failed:
    return NULL;
}
예제 #3
0
파일: drv_pci22.c 프로젝트: PA10ros/PA10ros
/* --- implementations --- */
int detect_pci22(void){
  
  unsigned int c;
  char counter=0;

  
  pacc = pci_alloc();           /* Get the pci_access structure */
  
  pci_init(pacc);               /* Initialize the PCI library */
  pci_scan_bus(pacc);           /* We want to get the list of devices */


  /* Iterate over all PCI devices */
  for(pci22_dev=pacc->devices; pci22_dev; pci22_dev=pci22_dev->next){
    
    pci_fill_info(pci22_dev, PCI_FILL_IDENT | PCI_FILL_BASES); 
    
    // Detect the specified device
    if( (pci22_dev->vendor_id == PCI22_PCI_VENDOR_ID) && 
	(pci22_dev->device_id == PCI22_PCI_DEVICE_ID) 
	){
      break;
    }

  }
  if (pci22_dev==NULL){
    printf("\n\nERROR: PCI22 card not detected\n\n\n");
    exit(0);
  }


  printf("\n\nPCI22 card detected on %02x:%02x.%d\nVendorID=%04x DeviceID=%04x irq=%d\n",
	 pci22_dev->bus,pci22_dev->dev,
	 pci22_dev->func,pci22_dev->vendor_id,
	 pci22_dev->device_id,
	 pci22_dev->irq);


  printf("\n\n--- Baseaddresses ---\n");
  printf("BAR1 %x\n",pci22_dev->base_addr[0]);
  printf("BAR2 %x\n",pci22_dev->base_addr[1]);
  printf("BAR3 %x\n",pci22_dev->base_addr[2]);
  printf("BAR4 %x\n",pci22_dev->base_addr[3]);
  printf("BAR5 %x\n",pci22_dev->base_addr[4]);
  printf("BAR6 %x\n",pci22_dev->base_addr[6]);
  printf("\n");


  //printPCIDevice(pci22_dev);

/* The baseaddress of the COM20022 is at BAR2 */

//g_pci22dipswitch = pci22_dev->base_addr[1]; //Nonworking
g_pci22base = pci22_dev->base_addr[2];

  /* Close everything */
  pci_cleanup(pacc);

  return 0;
}
예제 #4
0
static int
pci_connect( device_t *igb_dev )
{
	struct  pci_access      *pacc;
	struct  pci_dev *dev;
	int             err;
	char    devpath[IGB_BIND_NAMESZ];

	memset( igb_dev, 0, sizeof(device_t));

	pacc    =       pci_alloc();
	pci_init(pacc);
	pci_scan_bus(pacc);
	for     (dev=pacc->devices;     dev;    dev=dev->next)
    {
		pci_fill_info(dev, PCI_FILL_IDENT | PCI_FILL_BASES | PCI_FILL_CLASS);
      
		igb_dev->pci_vendor_id = dev->vendor_id;
		igb_dev->pci_device_id = dev->device_id;
		igb_dev->domain = dev->domain;
		igb_dev->bus = dev->bus;
		igb_dev->dev = dev->dev;
		igb_dev->func = dev->func;
		
		snprintf
			(devpath, IGB_BIND_NAMESZ, "%04x:%02x:%02x.%d", dev->domain,
			 dev->bus, dev->dev, dev->func );

		err = igb_probe( igb_dev );
      
		if (err) {
			continue;
		}

		printf ("attaching to %s\n", devpath);
		err = igb_attach( devpath, igb_dev );
      
		if (err) {
			printf ("attach failed! (%s)\n", strerror(errno));
			continue;
		}
		goto out;
    }

	pci_cleanup(pacc);
	return  ENXIO;
  
out:
	pci_cleanup(pacc);
	return  0;
}
예제 #5
0
int create_pci(int method, struct pci_access **pci_out)  {
  struct pci_access *pci = pci_alloc();
  pci->method = method;
  pci_init(pci);
  pci_scan_bus(pci);

  struct pci_dev *d31f0 = pci_find_dev(pci, 0, 31, 0);
  if(!d31f0) {
    ERR("Cannot find D31:F0");
  }

  pci_fill_info(d31f0, PCI_FILL_IDENT | PCI_FILL_BASES);
  if(d31f0->vendor_id != 0x8086) {
    ERR("Vendor of D31:F0 is not Intel");
  }

  *pci_out = pci;
  return 0;
}
예제 #6
0
int show_pci(void)
{
  struct pci_access *pacc;
  struct pci_dev *dev;
  unsigned int c;

  pacc = pci_alloc();		/* Get the pci_access structure */
  /* Set all options you want -- here we stick with the defaults */
  pacc->debugging=1;
  pci_init(pacc);		/* Initialize the PCI library */
  pci_scan_bus(pacc);		/* We want to get the list of devices */
  for(dev=pacc->devices; dev; dev=dev->next)	/* Iterate over all devices */
    {
      pci_fill_info(dev, PCI_FILL_IDENT | PCI_FILL_BASES);	/* Fill in header info we need */
      c = pci_read_word(dev, PCI_CLASS_DEVICE);	/* Read config register directly */
      printf("%02x:%02x.%d vendor=%04x device=%04x class=%04x irq=%d base0=%lx\n",
	     dev->bus, dev->dev, dev->func, dev->vendor_id, dev->device_id,
	     c, dev->irq, dev->base_addr[0]);
    }
  pci_cleanup(pacc);		/* Close everything */
  return 0;
}
nsresult nsSystemInfo::Init()
{
    struct pci_access *pacc;
    struct pci_dev *p;
    pciaddr_t ram = 0;
    char buf[128];
    int i, found, v, n;
    Display *dpy;
    char *display = NULL;
    Window root;
    XVisualInfo *info, templ;
    XWindowAttributes wts;
    XPixmapFormatValues *pf;
    XSetWindowAttributes attr;
    Window win;

    mWidth = 0;
    mHeight = 0;
    mDepth = 0;

    pacc = pci_alloc();
    pci_init(pacc);

    pci_scan_bus(pacc);
    for (p = pacc->devices; p; p=p->next) {
        pci_fill_info(p,
                      PCI_FILL_IDENT | PCI_FILL_CLASS | PCI_FILL_BASES | PCI_FILL_SIZES);
        if (p->device_class == PCI_CLASS_DISPLAY_VGA) {
            pci_lookup_name(pacc, buf, sizeof(buf),
                            PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
                            p->vendor_id, p->device_id);
            mDeviceName.AssignLiteral(buf);
            sprintf(buf, "0x%04X", p->vendor_id);
            mVendorID.AssignLiteral(buf);
            sprintf(buf, "0x%04X", p->device_id);
            mDeviceID.AssignLiteral(buf);
            for (i=0; i<6; i++) {
                pciaddr_t len = (p->known_fields & PCI_FILL_SIZES) ? p->size[i] : 0;
                if (len > ram) ram = len;
            }
            vram = ram / 1024 / 1024;
        }
        else {
            Log("Grafx Bot: No PCI VGA device found");
        }
    }

    pci_cleanup(pacc);

    if (NULL != (display = getenv("DISPLAY"))) {
        if (display[0] != ':') {
            display = strchr(display, ':');
            if (NULL == display) {
                Log("Grafx Bot: unable to find display");
                return NS_OK;
            }
        }
        if (NULL == (dpy = XOpenDisplay(display))) {
            Log("Grafx Bot: unable to find X display");
            return NS_OK;
        }
        root = DefaultRootWindow(dpy);
        XGetWindowAttributes(dpy, root, &wts);
        mWidth = wts.width;
        mHeight = wts.height;

        templ.screen = XDefaultScreen(dpy);
        info = XGetVisualInfo(dpy, VisualScreenMask, &templ, &found);
        v = -1;
        for (i = 0; v == -1 && i < found; i++) {
            if (info[i].depth >= 15)
                v = i;
        }
        for (i = 0; v == -1 && i < found; i++) {
            if (info[i].depth == 8)
                v = i;
        }
        if (-1 == v) {
            Log("Grafx Bot: can't find visual");
            return NS_OK;
        }

        pf = XListPixmapFormats(dpy, &n);
        for (i = 0; i < n; i++) {
            if (pf[i].depth == info[v].depth) {
                mDepth = pf[i].depth;
            }
        }

        if (gGLXWrap.OpenLibrary("libGL.so.1") && gGLXWrap.Init()) {
            attr.background_pixel = 0;
            attr.border_pixel = 0;
            attr.colormap  = XCreateColormap(dpy, root, info[v].visual, AllocNone);
            attr.event_mask = StructureNotifyMask | ExposureMask;
            win = XCreateWindow(dpy, root, 0, 0, 100, 100, 0, info[v].depth,
                                InputOutput, info[v].visual,
                                CWBackPixel | CWBorderPixel | CWColormap | CWEventMask, &attr);
            GLXContext ctx = gGLXWrap.fCreateContext(dpy, info, NULL, true);
            if (ctx) {
                if (gGLXWrap.fMakeCurrent(dpy, win, ctx)) {
                    mDriverVersion.AssignLiteral((char*)gGLXWrap.fGetString(LOCAL_GL_VERSION));
                    mDriver.AssignLiteral((char*)gGLXWrap.fGetString(LOCAL_GL_RENDERER));
                    mDriver.AppendLiteral(" (");
                    mDriver.AppendLiteral((char*)gGLXWrap.fGetString(LOCAL_GL_VENDOR));
                    mDriver.AppendLiteral(")");
                }
                else {
                    Log("Grafx Bot: unable to make current");
                }
                gGLXWrap.fDestroyContext(dpy, ctx);
            }
            else {
                Log("Grafx Bot: unable to create context");
            }
            XDestroyWindow(dpy, win);
        }
        else {
            Log("Grafx Bot: can't init libGL.so.1");
        }
    }

    return NS_OK;
}
예제 #8
0
int
main (int argc, char *argv[])
{
 	off_t address = 0;
 	size_t length = 0;
 	int fd;
 	int state;
	int retval = 0;
	struct pci_access *pacc;
 	struct pci_dev *dev;

	DBusError err;

	setup_logger ();
	udi = getenv ("UDI");

	HAL_DEBUG (("udi=%s", udi));
	if (udi == NULL) {
		HAL_ERROR (("No device specified"));
		return -2;
	}

	dbus_error_init (&err);
	if ((halctx = libhal_ctx_init_direct (&err)) == NULL) {
		HAL_ERROR (("Cannot connect to hald"));
		retval = -3;
		goto out;
	}

	if (!libhal_device_addon_is_ready (halctx, udi, &err)) {
		retval = -4;
		goto out;
	}


	conn = libhal_ctx_get_dbus_connection (halctx);
	dbus_connection_setup_with_g_main (conn, NULL);
	dbus_connection_set_exit_on_disconnect (conn, 0);

	dbus_connection_add_filter (conn, filter_function, NULL, NULL);

 	/* Search for the graphics card. */
 	/* Default values: */
 	/* address = 0x90300000; */
 	/* length = 0x20000; */

 	pacc = pci_alloc();
 	pci_init(pacc);
 	pci_scan_bus(pacc);
 	for(dev=pacc->devices; dev; dev=dev->next) {	/* Iterate over all devices */
 		pci_fill_info(dev, PCI_FILL_IDENT | PCI_FILL_BASES);
 		if ((dev->vendor_id == 0x1002) && (dev->device_id == 0x71c5)) { // ATI X1600
 			address = dev->base_addr[2];
 			length = dev->size[2];
 		}
 	}
 	pci_cleanup(pacc);

	HAL_DEBUG (("addr 0x%x len=%d", address, length));
 
 	if (!address) {
 		HAL_DEBUG (("Failed to detect ATI X1600, aborting..."));
		retval = 1;
		goto out;
 	}
 
 	fd = open ("/dev/mem", O_RDWR);
 	
 	if (fd < 0) {
 		HAL_DEBUG (("cannot open /dev/mem"));
		retval = 1;
		goto out;
 	}
 
 	memory = mmap (NULL, length, PROT_READ|PROT_WRITE, MAP_SHARED, fd, address);
 
 	if (memory == MAP_FAILED) {
 		HAL_ERROR (("mmap failed"));
		retval = 2;
		goto out;
 	}
 
 	/* Is it really necessary ? */
 	OUTREG(0x4dc, 0x00000005);
 	state = INREG(0x7ae4);
 	OUTREG(0x7ae4, state);

	/* Allow access to porta 0x300 through 0x304 */
	if (ioperm (0x300, 5, 1) < 0) {
		HAL_ERROR (("ioperm failed (you should be root)."));
		exit(1);
	}

	/* this works because we hardcoded the udi's in the <spawn> in the fdi files */
	if (!libhal_device_claim_interface (halctx, 
					    "/org/freedesktop/Hal/devices/macbook_pro_lcd_panel", 
					    "org.freedesktop.Hal.Device.LaptopPanel", 
					    "    <method name=\"SetBrightness\">\n"
					    "      <arg name=\"brightness_value\" direction=\"in\" type=\"i\"/>\n"
					    "      <arg name=\"return_code\" direction=\"out\" type=\"i\"/>\n"
					    "    </method>\n"
					    "    <method name=\"GetBrightness\">\n"
					    "      <arg name=\"brightness_value\" direction=\"out\" type=\"i\"/>\n"
					    "    </method>\n",
					    &err)) {
		HAL_ERROR (("Cannot claim interface 'org.freedesktop.Hal.Device.LaptopPanel'"));
		retval = -4;
		goto out;
	}
	if (!libhal_device_claim_interface (halctx, 
					    "/org/freedesktop/Hal/devices/macbook_pro_light_sensor",
					    "org.freedesktop.Hal.Device.LightSensor", 
					    "    <method name=\"GetBrightness\">\n"
					    "      <arg name=\"brightness_value\" direction=\"out\" type=\"ai\"/>\n"
					    "    </method>\n",
					    &err)) {
		HAL_ERROR (("Cannot claim interface 'org.freedesktop.Hal.Device.LightSensor'"));
		retval = -4;
		goto out;
	}
	if (!libhal_device_claim_interface (halctx, 
					    "/org/freedesktop/Hal/devices/macbook_pro_keyboard_backlight",
					    "org.freedesktop.Hal.Device.KeyboardBacklight", 
					    "    <method name=\"GetBrightness\">\n"
					    "      <arg name=\"brightness_value\" direction=\"out\" type=\"i\"/>\n"
					    "    </method>\n"
					    "    <method name=\"SetBrightness\">\n"
					    "      <arg name=\"brightness_value\" direction=\"in\" type=\"i\"/>\n"
					    "    </method>\n",
					    &err)) {
		HAL_ERROR (("Cannot claim interface 'org.freedesktop.Hal.Device.KeyboardBacklight'"));
		retval = -4;
		goto out;
	}

	main_loop = g_main_loop_new (NULL, FALSE);
	g_main_loop_run (main_loop);
	return 0;

out:
        HAL_DEBUG (("An error occured, exiting cleanly"));

        LIBHAL_FREE_DBUS_ERROR (&err);

        if (halctx != NULL) {
                libhal_ctx_shutdown (halctx, &err);
                LIBHAL_FREE_DBUS_ERROR (&err);
                libhal_ctx_free (halctx);
        }

        return retval;
}
예제 #9
0
파일: pci.c 프로젝트: cbm-fles/pda
static inline
PdaDebugReturnCode
PciDevice_invoke_libpci
(
    PciDevice  *device,
    const char *bus_id
)
{
    DEBUG_PRINTF(PDADEBUG_ENTER, "");

    if(device == NULL)
    { RETURN( ERROR( EINVAL, "Invalid pointer!\n") ); }

    if(bus_id == NULL)
    { RETURN( ERROR( EINVAL, "Invalid pointer!\n") ); }

    if
    (
        (strlen(bus_id) != 12) ||
        (bus_id[4]  != ':')    ||
        (bus_id[7]  != ':')    ||
        (bus_id[10] != '.')
    )
    { RETURN( ERROR( EINVAL, "Invalid bus ID string!\n") ); }

    struct pci_access *pci_access_handler = pci_alloc();
    pci_init(pci_access_handler);
    pci_scan_bus(pci_access_handler);
    for
    (
        struct pci_dev *pci_device = pci_access_handler->devices;
        pci_device;
        pci_device = pci_device->next
    )
    {
        pci_fill_info(pci_device,
                      PCI_FILL_IDENT      |
                      PCI_FILL_IRQ        |
                      PCI_FILL_BASES      |
                      PCI_FILL_ROM_BASE   |
                      PCI_FILL_SIZES      |
                      PCI_FILL_CLASS      |
                      PCI_FILL_CAPS       |
                      PCI_FILL_EXT_CAPS   |
                      PCI_FILL_PHYS_SLOT
                      );

        char device_bus_id[] = "0000:00:00.0";
        snprintf(device_bus_id, 13,"%04x:%02x:%02x.%d", pci_device->domain, pci_device->bus,
                pci_device->dev, pci_device->func);

        if(strcmp(device_bus_id, bus_id) == 0)
        {
            device->domain_id   = pci_device->domain;
            device->bus_id      = pci_device->bus;
            device->device_id   = pci_device->dev;
            device->function_id = pci_device->func;

            for(uint32_t i = 0; i < PDA_MAX_PCI_32_BARS; i++)
            {
                uint32_t bar       = pci_device->base_addr[i];

                DEBUG_PRINTF(PDADEBUG_VALUE, "bar%d : 0x%x\n", i, bar);

                uint8_t  config[64];
                pci_read_block(pci_device, 0, config, 64);
                uint64_t addr      = PCI_BASE_ADDRESS_0 + (4*i);
                uint32_t virt_flag =
                    (config[addr+0]       ) |
                    (config[addr+1] << 8  ) |
                    (config[addr+2] << 16 ) |
                    (config[addr+3] << 24 ) ;

                if(bar && !virt_flag)
                {
                    device->bar_types[i] = PCIBARTYPES_NOT_MAPPED;
                    DEBUG_PRINTF(PDADEBUG_CONTROL_FLOW, "bar%u is virtual\n", i);
                    continue;
                }

                if(bar == 0x0)
                {
                    if(device->bar_types[i] != PCIBARTYPES_NOT_MAPPED)
                    {
                        DEBUG_PRINTF(PDADEBUG_CONTROL_FLOW,
                                     "Inconsistency detected -> bar%u not classified correctly\n", i);
                        device->bar_types[i] = PCIBARTYPES_NOT_MAPPED;
                    }
                    PciDevice_invoke_libpci_set_barsize(device, i, pci_device->size[i]);
                    continue;
                }

                if( (bar & 0x00000001) == 0x1)
                {
                    if(device->bar_types[i] != PCIBARTYPES_IO)
                    {
                        DEBUG_PRINTF(PDADEBUG_CONTROL_FLOW,
                                     "Inconsistency detected -> bar%u not classified correctly\n", i);
                        device->bar_types[i] = PCIBARTYPES_IO;
                    }
                    PciDevice_invoke_libpci_set_barsize(device, i, pci_device->size[i]);
                    continue;
                }

                uint32_t width_bit = bar & 0x00000006;
                if(width_bit == 0x00000000)
                {
                    if(device->bar_types[i] != PCIBARTYPES_BAR32)
                    {
                        DEBUG_PRINTF(PDADEBUG_CONTROL_FLOW,
                                     "Inconsistency detected -> bar%u not classified correctly\n", i);
                        device->bar_types[i] = PCIBARTYPES_BAR32;
                    }
                    PciDevice_invoke_libpci_set_barsize(device, i, pci_device->size[i]);
                    continue;
                }

                if(width_bit == 0x00000004)
                {
                    if(device->bar_types[i] != PCIBARTYPES_BAR64)
                    {
                        DEBUG_PRINTF(PDADEBUG_CONTROL_FLOW,
                                     "Inconsistency detected -> bar%u not classified correctly\n", i);
                        device->bar_types[i] = PCIBARTYPES_BAR64;
                        device->bar_types[i + 1] = PCIBARTYPES_NOT_MAPPED;
                    }
                    PciDevice_invoke_libpci_set_barsize(device, i, pci_device->size[i]);
                    continue;
                }
            }

            /** Older versions of libpci leak memory when this function is called. **/
#if PCI_LIB_VERSION >= 0x030301
            pci_lookup_name(pci_access_handler, device->description, PDA_STRING_LIMIT,
                            PCI_LOOKUP_DEVICE, pci_device->vendor_id,
                            pci_device->device_id);
#else
            #warning "At least version 3.3.1 of libpci is needed. Otherwise the device description can't be returned."
            strcpy(device->description, "");
#endif

            DEBUG_PRINTF(PDADEBUG_VALUE, " (%s) %s\n", device->description, device_bus_id);
        }
    }
    pci_cleanup(pci_access_handler);

    RETURN(PDA_SUCCESS);
}
예제 #10
0
파일: sbone_dev.c 프로젝트: Jameshzc/vpcie
sbone_err_t sbone_dev_open_pcie
(sbone_dev_t* dev, int dom, int bus, int _dev, int fun)
{
  static const size_t nbar = sizeof(dev->bar_addrs) / sizeof(dev->bar_addrs[0]);
  int mem_fd;
  size_t i;
  struct pci_access* pci_access;
  struct pci_dev* pci_dev;
  int err = -1;

  /* zero device */

  for (i = 0; i < nbar; ++i) dev->bar_sizes[i] = 0;

  dev->nid = -1;

  /* find the pci device */

  pci_access = pci_alloc();
  if (pci_access == NULL) { PERROR(); goto on_error_0; }
  pci_init(pci_access);
  pci_scan_bus(pci_access);

  pci_dev = pci_get_dev(pci_access, dom, bus, _dev, fun);
  if (pci_dev == NULL) { PERROR(); goto on_error_1; }
  pci_fill_info(pci_dev, PCI_FILL_IDENT | PCI_FILL_BASES);

  /* map bars */

  mem_fd = open("/dev/mem", O_RDWR | O_SYNC);
  if (mem_fd == -1) { PERROR(); goto on_error_2; }

  for (i = 0; i < nbar; ++i)
  {
#if 1 /* FIXME: pci_dev->size[i] not detected by pci_fill_info ... */
    if (pci_dev->base_addr[i] != 0)
      pci_dev->size[i] = get_bar_size(pci_access, pci_dev, i);
#endif /* FIXME */

    dev->bar_sizes[i] = pci_dev->size[i];
    if (dev->bar_sizes[i] == 0) continue ;

    dev->bar_addrs[i] = (uintptr_t)mmap
    (
     NULL, pci_dev->size[i],
     PROT_READ | PROT_WRITE,
     MAP_SHARED | MAP_NORESERVE,
     mem_fd,
     pci_dev->base_addr[i]
    );

    if (dev->bar_addrs[i] == (uintptr_t)MAP_FAILED)
    {
      size_t j;
      for (j = 0; j < i; ++j)
	munmap((void*)dev->bar_addrs[i], dev->bar_sizes[i]);
      PERROR();
      goto on_error_3;
    }
  }

  if ((dev->fd = open("/dev/sbone", O_RDWR)) == -1)
  {
    PERROR();
    goto on_error_3;
  }

  /* success */

  err = 0;

 on_error_3:
  close(mem_fd);
 on_error_2:
  pci_free_dev(pci_dev);
 on_error_1:
  pci_cleanup(pci_access);
 on_error_0:
  return err;

}
예제 #11
0
int main (int argc, char *argv[])
{
	struct pci_access *pacc;
	struct pci_dev *dev;
	struct pci_filter filter;
	char *msg;
	uint16_t command;
	int v1_1 = 0, i2c;

	if (argc != 2 &&
	    ((argc != 3) ||
	     (!(v1_1 = !strcmp(argv[2], "1.1")) && strcmp(argv[2], "1.0")))) {
		fprintf(stderr,
			"VT6307 OHCI mode config\n"
			"Version 0.9\n"
			"Copyright (C) 2007 Krzysztof Halasa <*****@*****.**>\n"
			"\n"
			"Usage: vt6307ohciver <pci_device> [ 1.0 | 1.1 ]\n");
		exit(1);
	}

	if (iopl(3)) {
		fprintf(stderr, "iopl() failed (must be root)\n");
		exit(1);
	}

	pacc = pci_alloc();
	pci_filter_init(pacc, &filter);
	if ((msg = pci_filter_parse_slot(&filter, argv[1]))) {
		fprintf(stderr, "Invalid pci_device\n");
		exit(1);
	}

	filter.vendor = VENDOR_ID;
	filter.device = DEVICE_ID;

	pci_init(pacc);
	pci_scan_bus(pacc);

	for (dev = pacc->devices; dev; dev = dev->next)
		if (pci_filter_match(&filter, dev))
			break;

	if (!dev) {
		fprintf(stderr, "Device %s not found\n", argv[2]);
		exit(1);
	}

	pci_fill_info(dev, PCI_FILL_BASES | PCI_FILL_SIZES);

	if (dev->size[0] != MEM_SIZE || dev->size[1] != IO_SIZE) {
		fprintf(stderr, "Unexpected MEM/IO region size, is it"
			" VT6307 chip?\n");
		exit(1);
	}

	command = pci_read_word(dev, PCI_COMMAND);

	if ((command & PCI_COMMAND_IO) == 0) {
		fprintf(stderr, "Device disabled, trying to enable it\n");
		pci_write_word(dev, PCI_COMMAND, command | PCI_COMMAND_IO);
	}

	io_ports = dev->base_addr[1] & PCI_BASE_ADDRESS_IO_MASK;

	i2c = (inl(io_ports + 0x20) & 0x80) ? 0 : 1;

	fprintf(stderr, "I/O region #1 is at %04X\n", io_ports);
	fprintf(stderr, "It seems your VT6307 chip is connected to %s "
		"EEPROM\n", i2c ? "I^2C (24c01 or similar)" : "93c46");

	if (argc == 3) {
		/* enable direct access to pins */
		outl_p(inl(io_ports) | 0x80, io_ports);
		if (i2c)
			write_i2c(0x22, v1_1 ? 8 : 0);
		else
			write_4w(0x11, v1_1 ? 8 : 0);
		/* disable direct access to pins */
		outl_p(0x20, io_ports + 0x20);
		fprintf(stderr, "Please reboot\n");
	} else
		display(dev->base_addr[0] & PCI_BASE_ADDRESS_MEM_MASK);

	if ((command & PCI_COMMAND_IO) == 0) {
		fprintf(stderr, "Disabling device\n");
		pci_write_word(dev, PCI_COMMAND, command);
	}

	exit(0);
}
예제 #12
0
파일: pci.cpp 프로젝트: DrakXtools/ldetect
void pci::probe(void) {
    pci_scan_bus(_pacc);

    uint8_t buf[CONFIG_SPACE_SIZE] = {0};
    char classbuf[128] = {0}, vendorbuf[128] {0}, devbuf[128] = {0};

    for (struct pci_dev *dev = _pacc->devices; dev && _entries.size() < MAX_DEVICES; dev = dev->next) {
	_entries.push_back(pciEntry());
	pciEntry &e = _entries.back();
	memset(buf, 0, sizeof(buf));
	memset(classbuf, 0, sizeof(classbuf));
	memset(vendorbuf, 0, sizeof(vendorbuf));
	memset(devbuf, 0, sizeof(devbuf));

	pci_setup_cache(dev, buf, CONFIG_SPACE_SIZE);
	pci_read_block(dev, 0, buf, CONFIG_SPACE_SIZE);
	pci_fill_info(dev, PCI_FILL_IDENT | PCI_FILL_CLASS | PCI_FILL_CAPS);

	pci_lookup_name(_pacc, vendorbuf, sizeof(vendorbuf), PCI_LOOKUP_VENDOR, dev->vendor_id, dev->device_id);
	pci_lookup_name(_pacc, devbuf,    sizeof(devbuf),    PCI_LOOKUP_DEVICE, dev->vendor_id, dev->device_id);
	e.text.append(vendorbuf).append("|").append(devbuf);
	e.class_type += classbuf;
	e.vendor =     dev->vendor_id;
	e.device =     dev->device_id;
	e.pci_domain = dev->domain;
	e.bus =        dev->bus;
	e.pciusb_device = dev->dev;
	e.pci_function = dev->func;

	e.class_id = dev->device_class;
	e.subvendor = pci_read_word(dev, PCI_SUBSYSTEM_VENDOR_ID);
	e.subdevice = pci_read_word(dev, PCI_SUBSYSTEM_ID);
	e.pci_revision = pci_read_byte(dev, PCI_REVISION_ID);

	if ((e.subvendor == 0 && e.subdevice == 0) ||
		(e.subvendor == e.vendor && e.subdevice == e.device)) {
	    e.subvendor = 0xffff;
	    e.subdevice = 0xffff;
	}

	if (pci_find_cap(dev,PCI_CAP_ID_EXP, PCI_CAP_NORMAL))
	    e.is_pciexpress = true;

	/* special case for realtek 8139 that has two drivers */
	if (e.vendor == 0x10ec && e.device == 0x8139) {
	    if (e.pci_revision < 0x20)
		e.module = "8139too";
	    else
		e.module = "8139cp";
	}
    }

    // fake two PCI controllers for xen
    struct stat sb;
    if (!stat("/sys/bus/xen", &sb)) {
	// FIXME: use C++ streams..
	FILE *f;
	if ((f = fopen("/sys/hypervisor/uuid", "r"))) {
	    char buf[38];
	    fgets(buf, sizeof(buf) - 1, f);
	    fclose(f);
	    if (strncmp(buf, "00000000-0000-0000-0000-000000000000", sizeof(buf))) {
		// We're now sure to be in a Xen guest:
		{
		    _entries.push_back(pciEntry());
		    pciEntry &e = _entries.back();
		    e.text.append("XenSource, Inc.|Block Frontend");
		    e.class_id = 0x0106; // STORAGE_SATA

		    e.vendor =  0x1a71; // XenSource
		    e.device =  0xfffa; // fake
		    e.subvendor = 0;
		    e.subdevice = 0;
		    e.class_id = 0x0106;
		    e.module = "xen_blkfront";
		}
		{
		    _entries.push_back(pciEntry());
		    pciEntry &e = _entries.back();
		    e.text.append("XenSource, Inc.|Network Frontend");
		    e.class_id = 0x0200; // NETWORK_ETHERNET

		    e.vendor =  0x1a71; // XenSource
		    e.device =  0xfffb; // fake
		    e.subvendor = 0;
		    e.subdevice = 0;
		    e.class_id = 0x0200;
		    e.module = "xen_netfront";
		}
	    }
	}
    }
    findModules("pcitable", false);
}