diff --git a/sys/compat/linsysfs/linsysfs.c b/sys/compat/linsysfs/linsysfs.c index d771b46d9652..ce1262cd0908 100644 --- a/sys/compat/linsysfs/linsysfs.c +++ b/sys/compat/linsysfs/linsysfs.c @@ -1,344 +1,517 @@ /*- * Copyright (c) 2006 IronPort Systems * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include struct scsi_host_queue { TAILQ_ENTRY(scsi_host_queue) scsi_host_next; char *path; char *name; }; TAILQ_HEAD(,scsi_host_queue) scsi_host_q; static int host_number = 0; static int atoi(const char *str) { return (int)strtol(str, (char **)NULL, 10); } /* * Filler function for proc_name */ static int linsysfs_scsiname(PFS_FILL_ARGS) { struct scsi_host_queue *scsi_host; int index; if (strncmp(pn->pn_parent->pn_name, "host", 4) == 0) { index = atoi(&pn->pn_parent->pn_name[4]); } else { sbuf_printf(sb, "unknown\n"); return (0); } TAILQ_FOREACH(scsi_host, &scsi_host_q, scsi_host_next) { if (index-- == 0) { sbuf_printf(sb, "%s\n", scsi_host->name); return (0); } } sbuf_printf(sb, "unknown\n"); return (0); } /* * Filler function for device sym-link */ static int linsysfs_link_scsi_host(PFS_FILL_ARGS) { struct scsi_host_queue *scsi_host; int index; if (strncmp(pn->pn_parent->pn_name, "host", 4) == 0) { index = atoi(&pn->pn_parent->pn_name[4]); } else { sbuf_printf(sb, "unknown\n"); return (0); } TAILQ_FOREACH(scsi_host, &scsi_host_q, scsi_host_next) { if (index-- == 0) { sbuf_printf(sb, "../../../devices%s", scsi_host->path); return(0); } } sbuf_printf(sb, "unknown\n"); return (0); } +static int +linsysfs_fill_data(PFS_FILL_ARGS) +{ + sbuf_printf(sb, "%s", pn->pn_data); + return (0); +} + +static int +linsysfs_fill_vendor(PFS_FILL_ARGS) +{ + sbuf_printf(sb, "0x%04x\n", pci_get_vendor((device_t)pn->pn_data)); + return (0); +} + +static int +linsysfs_fill_device(PFS_FILL_ARGS) +{ + sbuf_printf(sb, "0x%04x\n", pci_get_device((device_t)pn->pn_data)); + return (0); +} + +static int +linsysfs_fill_subvendor(PFS_FILL_ARGS) +{ + sbuf_printf(sb, "0x%04x\n", pci_get_subvendor((device_t)pn->pn_data)); + return (0); +} + +static int +linsysfs_fill_subdevice(PFS_FILL_ARGS) +{ + sbuf_printf(sb, "0x%04x\n", pci_get_subdevice((device_t)pn->pn_data)); + return (0); +} + +static int +linsysfs_fill_revid(PFS_FILL_ARGS) +{ + sbuf_printf(sb, "0x%x\n", pci_get_revid((device_t)pn->pn_data)); + return (0); +} + +/* + * Filler function for PCI uevent file + */ +static int +linsysfs_fill_uevent_pci(PFS_FILL_ARGS) +{ + device_t dev; + + dev = (device_t)pn->pn_data; + sbuf_printf(sb, "DRIVER=%s\nPCI_CLASS=%X\nPCI_ID=%04X:%04X\n" + "PCI_SUBSYS_ID=%04X:%04X\nPCI_SLOT_NAME=%04d:%02x:%02x.%x\n", + linux_driver_get_name_dev(dev), pci_get_class(dev), + pci_get_vendor(dev), pci_get_device(dev), pci_get_subvendor(dev), + pci_get_subdevice(dev), pci_get_domain(dev), pci_get_bus(dev), + pci_get_slot(dev), pci_get_function(dev)); + return (0); +} + +/* + * Filler function for drm uevent file + */ +static int +linsysfs_fill_uevent_drm(PFS_FILL_ARGS) +{ + device_t dev; + int unit; + + dev = (device_t)pn->pn_data; + unit = device_get_unit(dev); + sbuf_printf(sb, + "MAJOR=226\nMINOR=%d\nDEVNAME=drm/%d\nDEVTYPE=dri_minor\n", unit, + unit); + return (0); +} + +static char * +get_full_pfs_path(struct pfs_node *cur) +{ + char *temp, *path; + + temp = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); + path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); + path[0] = '\0'; + + do { + snprintf(temp, MAXPATHLEN, "%s/%s", cur->pn_name, path); + strlcpy(path, temp, MAXPATHLEN); + cur = cur->pn_parent; + } while (cur->pn_parent != NULL); + + path[strlen(path) - 1] = '\0'; /* remove extra slash */ + free(temp, M_TEMP); + return (path); +} + +/* + * Filler function for symlink from drm char device to PCI device + */ +static int +linsysfs_fill_vgapci(PFS_FILL_ARGS) +{ + char *path; + + path = get_full_pfs_path((struct pfs_node*)pn->pn_data); + sbuf_printf(sb, "../../../%s", path); + free(path, M_TEMP); + return (0); +} + #define PCI_DEV "pci" +#define DRMN_DEV "drmn" static int -linsysfs_run_bus(device_t dev, struct pfs_node *dir, struct pfs_node *scsi, char *path, - char *prefix) +linsysfs_run_bus(device_t dev, struct pfs_node *dir, struct pfs_node *scsi, struct pfs_node *chardev, + char *path, char *prefix) { struct scsi_host_queue *scsi_host; - struct pfs_node *sub_dir; + struct pfs_node *sub_dir, *cur_file, *cur_chardev; int i, nchildren; device_t *children, parent; devclass_t devclass; const char *name = NULL; struct pci_devinfo *dinfo; - char *device, *host, *new_path = path; + char *device, *host, *new_path, *chardevname; + + new_path = path; + chardevname = malloc(16, M_TEMP, M_WAITOK); parent = device_get_parent(dev); if (parent) { devclass = device_get_devclass(parent); if (devclass != NULL) name = devclass_get_name(devclass); if (name && strcmp(name, PCI_DEV) == 0) { dinfo = device_get_ivars(dev); if (dinfo) { device = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); new_path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); new_path[0] = '\000'; strcpy(new_path, path); host = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); device[0] = '\000'; sprintf(device, "%s:%02x:%02x.%x", prefix, dinfo->cfg.bus, dinfo->cfg.slot, dinfo->cfg.func); strcat(new_path, "/"); strcat(new_path, device); dir = pfs_create_dir(dir, device, NULL, NULL, NULL, 0); + cur_file = pfs_create_file(dir, "vendor", + &linsysfs_fill_vendor, NULL, NULL, NULL, + PFS_RD); + cur_file->pn_data = (void*)dev; + cur_file = pfs_create_file(dir, "device", + &linsysfs_fill_device, NULL, NULL, NULL, + PFS_RD); + cur_file->pn_data = (void*)dev; + cur_file = pfs_create_file(dir, + "subsystem_vendor", + &linsysfs_fill_subvendor, NULL, NULL, NULL, + PFS_RD); + cur_file->pn_data = (void*)dev; + cur_file = pfs_create_file(dir, + "subsystem_device", + &linsysfs_fill_subdevice, NULL, NULL, NULL, + PFS_RD); + cur_file->pn_data = (void*)dev; + cur_file = pfs_create_file(dir, "revision", + &linsysfs_fill_revid, NULL, NULL, NULL, + PFS_RD); + cur_file->pn_data = (void*)dev; + cur_file = pfs_create_file(dir, "uevent", + &linsysfs_fill_uevent_pci, NULL, NULL, + NULL, PFS_RD); + cur_file->pn_data = (void*)dev; + cur_file = pfs_create_link(dir, "subsystem", + &linsysfs_fill_data, NULL, NULL, NULL, 0); + /* libdrm just checks that the link ends in "/pci" */ + cur_file->pn_data = "/sys/bus/pci"; if (dinfo->cfg.baseclass == PCIC_STORAGE) { /* DJA only make this if needed */ sprintf(host, "host%d", host_number++); strcat(new_path, "/"); strcat(new_path, host); pfs_create_dir(dir, host, NULL, NULL, NULL, 0); scsi_host = malloc(sizeof( struct scsi_host_queue), M_DEVBUF, M_NOWAIT); scsi_host->path = malloc( strlen(new_path) + 1, M_DEVBUF, M_NOWAIT); scsi_host->path[0] = '\000'; bcopy(new_path, scsi_host->path, strlen(new_path) + 1); scsi_host->name = "unknown"; sub_dir = pfs_create_dir(scsi, host, NULL, NULL, NULL, 0); pfs_create_link(sub_dir, "device", &linsysfs_link_scsi_host, NULL, NULL, NULL, 0); pfs_create_file(sub_dir, "proc_name", &linsysfs_scsiname, NULL, NULL, NULL, PFS_RD); scsi_host->name = linux_driver_get_name_dev(dev); TAILQ_INSERT_TAIL(&scsi_host_q, scsi_host, scsi_host_next); } free(device, M_TEMP); free(host, M_TEMP); } } + + dinfo = device_get_ivars(parent); + if (dinfo != NULL && dinfo->cfg.baseclass == PCIC_DISPLAY) { + devclass = device_get_devclass(dev); + if (devclass != NULL) + name = devclass_get_name(devclass); + if (name != NULL && strcmp(name, DRMN_DEV) == 0 && + device_get_unit(dev) >= 0) { + sprintf(chardevname, "226:%d", + device_get_unit(dev)); + cur_chardev = pfs_create_dir(chardev, + chardevname, NULL, NULL, NULL, 0); + cur_file = pfs_create_link(cur_chardev, + "device", &linsysfs_fill_vgapci, NULL, + NULL, NULL, PFS_RD); + cur_file->pn_data = (void*)dir; + cur_file = pfs_create_file(cur_chardev, + "uevent", &linsysfs_fill_uevent_drm, NULL, + NULL, NULL, PFS_RD); + cur_file->pn_data = (void*)dev; + } + } } device_get_children(dev, &children, &nchildren); for (i = 0; i < nchildren; i++) { if (children[i]) - linsysfs_run_bus(children[i], dir, scsi, new_path, prefix); + linsysfs_run_bus(children[i], dir, scsi, chardev, new_path, prefix); } if (new_path != path) free(new_path, M_TEMP); + free(chardevname, M_TEMP); return (1); } /* * Filler function for sys/devices/system/cpu/online */ static int linsysfs_cpuonline(PFS_FILL_ARGS) { sbuf_printf(sb, "%d-%d\n", CPU_FIRST(), mp_maxid); return (0); } /* * Filler function for sys/devices/system/cpu/cpuX/online */ static int linsysfs_cpuxonline(PFS_FILL_ARGS) { sbuf_printf(sb, "1\n"); return (0); } static void linsysfs_listcpus(struct pfs_node *dir) { struct pfs_node *cpu; char *name; int i, count, len; len = 1; count = mp_maxcpus; while (count > 10) { count /= 10; len++; } len += sizeof("cpu"); name = malloc(len, M_TEMP, M_WAITOK); for (i = 0; i < mp_ncpus; ++i) { /* /sys/devices/system/cpu/cpuX */ sprintf(name, "cpu%d", i); cpu = pfs_create_dir(dir, name, NULL, NULL, NULL, 0); pfs_create_file(cpu, "online", &linsysfs_cpuxonline, NULL, NULL, NULL, PFS_RD); } free(name, M_TEMP); } /* * Constructor */ static int linsysfs_init(PFS_INIT_ARGS) { struct pfs_node *root; struct pfs_node *dir, *sys, *cpu; struct pfs_node *pci; struct pfs_node *scsi; + struct pfs_node *devdir, *chardev; devclass_t devclass; device_t dev; TAILQ_INIT(&scsi_host_q); root = pi->pi_root; /* /sys/class/... */ scsi = pfs_create_dir(root, "class", NULL, NULL, NULL, 0); scsi = pfs_create_dir(scsi, "scsi_host", NULL, NULL, NULL, 0); /* /sys/devices */ dir = pfs_create_dir(root, "devices", NULL, NULL, NULL, 0); /* /sys/devices/pci0000:00 */ pci = pfs_create_dir(dir, "pci0000:00", NULL, NULL, NULL, 0); + /* /sys/dev/char */ + devdir = pfs_create_dir(root, "dev", NULL, NULL, NULL, 0); + chardev = pfs_create_dir(devdir, "char", NULL, NULL, NULL, 0); + devclass = devclass_find("root"); if (devclass == NULL) { return (0); } dev = devclass_get_device(devclass, 0); - linsysfs_run_bus(dev, pci, scsi, "/pci0000:00", "0000"); + linsysfs_run_bus(dev, pci, scsi, chardev, "/pci0000:00", "0000"); /* /sys/devices/system */ sys = pfs_create_dir(dir, "system", NULL, NULL, NULL, 0); /* /sys/devices/system/cpu */ cpu = pfs_create_dir(sys, "cpu", NULL, NULL, NULL, 0); pfs_create_file(cpu, "online", &linsysfs_cpuonline, NULL, NULL, NULL, PFS_RD); linsysfs_listcpus(cpu); return (0); } /* * Destructor */ static int linsysfs_uninit(PFS_INIT_ARGS) { struct scsi_host_queue *scsi_host, *scsi_host_tmp; TAILQ_FOREACH_SAFE(scsi_host, &scsi_host_q, scsi_host_next, scsi_host_tmp) { TAILQ_REMOVE(&scsi_host_q, scsi_host, scsi_host_next); free(scsi_host->path, M_TEMP); free(scsi_host, M_TEMP); } return (0); } PSEUDOFS(linsysfs, 1, PR_ALLOW_MOUNT_LINSYSFS); #if defined(__amd64__) MODULE_DEPEND(linsysfs, linux_common, 1, 1, 1); #else MODULE_DEPEND(linsysfs, linux, 1, 1, 1); #endif diff --git a/sys/compat/linux/linux_util.c b/sys/compat/linux/linux_util.c index 466c588157c5..61be7b9ab87e 100644 --- a/sys/compat/linux/linux_util.c +++ b/sys/compat/linux/linux_util.c @@ -1,245 +1,253 @@ /*- * Copyright (c) 1994 Christos Zoulas * Copyright (c) 1995 Frank van der Linden * Copyright (c) 1995 Scott Bartram * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * from: svr4_util.c,v 1.5 1995/01/22 23:44:50 christos Exp */ #include __FBSDID("$FreeBSD$"); #include "opt_compat.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include MALLOC_DEFINE(M_LINUX, "linux", "Linux mode structures"); MALLOC_DEFINE(M_EPOLL, "lepoll", "Linux events structures"); MALLOC_DEFINE(M_FUTEX, "futex", "Linux futexes"); MALLOC_DEFINE(M_FUTEX_WP, "futex wp", "Linux futex waiting proc"); const char linux_emul_path[] = "/compat/linux"; /* * Search an alternate path before passing pathname arguments on to * system calls. Useful for keeping a separate 'emulation tree'. * * If cflag is set, we check if an attempt can be made to create the * named file, i.e. we check if the directory it should be in exists. */ int linux_emul_convpath(struct thread *td, const char *path, enum uio_seg pathseg, char **pbuf, int cflag, int dfd) { int retval; retval = kern_alternate_path(td, linux_emul_path, path, pathseg, pbuf, cflag, dfd); return (retval); } void linux_msg(const struct thread *td, const char *fmt, ...) { va_list ap; struct proc *p; p = td->td_proc; printf("linux: pid %d (%s): ", (int)p->p_pid, p->p_comm); va_start(ap, fmt); vprintf(fmt, ap); va_end(ap); printf("\n"); } struct device_element { TAILQ_ENTRY(device_element) list; struct linux_device_handler entry; }; static TAILQ_HEAD(, device_element) devices = TAILQ_HEAD_INITIALIZER(devices); static struct linux_device_handler null_handler = { "mem", "mem", "null", "null", 1, 3, 1}; DATA_SET(linux_device_handler_set, null_handler); char * linux_driver_get_name_dev(device_t dev) { struct device_element *de; const char *device_name = device_get_name(dev); if (device_name == NULL) return NULL; TAILQ_FOREACH(de, &devices, list) { if (strcmp(device_name, de->entry.bsd_driver_name) == 0) return (de->entry.linux_driver_name); } return (NULL); } int linux_driver_get_major_minor(const char *node, int *major, int *minor) { struct device_element *de; + unsigned long devno; if (node == NULL || major == NULL || minor == NULL) return 1; if (strlen(node) > strlen("pts/") && strncmp(node, "pts/", strlen("pts/")) == 0) { - unsigned long devno; - /* * Linux checks major and minors of the slave device * to make sure it's a pty device, so let's make him * believe it is. */ devno = strtoul(node + strlen("pts/"), NULL, 10); *major = 136 + (devno / 256); *minor = devno % 256; return (0); } + if ((strlen(node) > strlen("drm/") && + strncmp(node, "drm/", strlen("drm/")) == 0) ) { + devno = strtoul(node + strlen("drm/"), NULL, 10); + *major = 226 + (devno / 256); + *minor = devno % 256; + return (0); + } + + TAILQ_FOREACH(de, &devices, list) { if (strcmp(node, de->entry.bsd_device_name) == 0) { *major = de->entry.linux_major; *minor = de->entry.linux_minor; return (0); } } return (1); } char * linux_get_char_devices() { struct device_element *de; char *temp, *string, *last; char formated[256]; int current_size = 0, string_size = 1024; string = malloc(string_size, M_LINUX, M_WAITOK); string[0] = '\000'; last = ""; TAILQ_FOREACH(de, &devices, list) { if (!de->entry.linux_char_device) continue; temp = string; if (strcmp(last, de->entry.bsd_driver_name) != 0) { last = de->entry.bsd_driver_name; snprintf(formated, sizeof(formated), "%3d %s\n", de->entry.linux_major, de->entry.linux_device_name); if (strlen(formated) + current_size >= string_size) { string_size *= 2; string = malloc(string_size, M_LINUX, M_WAITOK); bcopy(temp, string, current_size); free(temp, M_LINUX); } strcat(string, formated); current_size = strlen(string); } } return (string); } void linux_free_get_char_devices(char *string) { free(string, M_LINUX); } static int linux_major_starting = 200; int linux_device_register_handler(struct linux_device_handler *d) { struct device_element *de; if (d == NULL) return (EINVAL); de = malloc(sizeof(*de), M_LINUX, M_WAITOK); if (d->linux_major < 0) { d->linux_major = linux_major_starting++; } bcopy(d, &de->entry, sizeof(*d)); /* Add the element to the list, sorted on span. */ TAILQ_INSERT_TAIL(&devices, de, list); return (0); } int linux_device_unregister_handler(struct linux_device_handler *d) { struct device_element *de; if (d == NULL) return (EINVAL); TAILQ_FOREACH(de, &devices, list) { if (bcmp(d, &de->entry, sizeof(*d)) == 0) { TAILQ_REMOVE(&devices, de, list); free(de, M_LINUX); return (0); } } return (EINVAL); }