/* * Copyright (c) 2011 Benjamin Franzke * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ #define _GNU_SOURCE #include #include #include #include #include #include "wfhandle.h" #include "wfddevice.h" #include "wfdport.h" #include "wfdregistry.h" #include #include #include #include #include #include struct wfd_device { int device_id; int fd; int device_index; struct udev_device *dev; WFDErrorCode error; drmModeResPtr resources; uint32_t count_connectors; drmModeConnectorPtr connectors; uint32_t choosen_crtcs; struct { WFDint port_id; WFDboolean state; } attach; }; static int udev_list_get_num_entries(struct udev_list_entry *list) { struct udev_list_entry *list_entry; int num = 0; udev_list_entry_foreach(list_entry, list) num++; return num; } static int open_from_syspath(struct udev *udev, const char *syspath) { struct udev_device *device; int fd; device = udev_device_new_from_syspath(udev, syspath); if (device == NULL) return -1; fd = open(udev_device_get_devnode(device), O_RDWR | O_CLOEXEC); udev_device_unref(device); return fd; } static int enumerate_devices(struct wfd_registry *registry) { struct udev_enumerate *enumerate; struct udev_list_entry *devices, *list_entry; int device_count = 0; int i; enumerate = udev_enumerate_new(registry->udev); if (enumerate == NULL) return -1; udev_enumerate_add_match_subsystem(enumerate, "drm"); udev_enumerate_add_match_property(enumerate, "MINOR", "0"); udev_enumerate_scan_devices(enumerate); devices = udev_enumerate_get_list_entry(enumerate); device_count = udev_list_get_num_entries(devices); registry->devices = calloc(device_count, sizeof *registry->devices); if (registry->devices == NULL) return -1; i = 0; udev_list_entry_foreach(list_entry, devices) { registry->devices[i].fd = open_from_syspath(registry->udev, udev_list_entry_get_name(list_entry)); if (registry->devices[i].fd < 0) { registry->devices[i].fd = 0; continue; } else if (registry->devices[i].fd == 0) { int tmp; tmp = fcntl(registry->devices[i].fd, F_DUPFD_CLOEXEC, 0); close(registry->devices[i].fd); registry->devices[i].fd = tmp; } i++; } registry->device_count = i; udev_enumerate_unref(enumerate); return 0; } #define MIN(a,b) ((a) < (b) ? (a) : (b)) WFDint wfd_devices_enumerate(WFDint *device_ids, WFDint device_id_count, const WFDint *filter_list) { WFDint count = 0; int i; struct wfd_registry *registry = wfd_get_registry(); if (registry->devices == NULL) enumerate_devices(registry); if (device_ids == NULL) return registry->device_count; for (i = 0; i < registry->device_count && count < device_id_count; ++i) { device_ids[count] = registry->devices[i].fd; count++; } return count; } WFDErrorCode wfd_device_get_error(struct wfd_device *device) { return device->error; } void wfd_device_set_error(struct wfd_device *device, WFDErrorCode error) { device->error = error; } void wfd_device_destroy(struct wfd_device *device) { struct wfd_registry *registry = wfd_get_registry(); registry->devices[device->device_index].created = 0; free(device->connectors); drmModeFreeResources(device->resources); udev_device_unref(device->dev); close(device->fd); free(device); } struct wfd_device * wfd_create_device(WFDint device_id, const WFDint *attrib_list) { struct wfd_device *device; struct wfd_registry *registry = wfd_get_registry(); struct stat buf; int device_index = -1, already_created = 0; int i; if (registry->devices == NULL) enumerate_devices(registry); if (registry->device_count <= 0) return NULL; if (device_id == WFD_DEFAULT_DEVICE_ID) device_id = registry->devices[0].fd; for (i = 0; i < registry->device_count; ++i) { if (registry->devices[i].fd == device_id) { device_index = i; already_created = registry->devices[i].created; break; } } if (device_index == -1 || already_created) return NULL; device = calloc(1, sizeof *device); if (device == NULL) return NULL; device->device_id = device_id; device->device_index = device_index; device->error = WFD_ERROR_NONE; device->choosen_crtcs = 0; device->fd = fcntl(device_id, F_DUPFD_CLOEXEC, 0); if (device->fd < 0) goto cleanup_device; if (fstat(device->fd, &buf) < 0) goto cleanup_fd; if (!S_ISCHR(buf.st_mode)) goto cleanup_fd; device->dev = udev_device_new_from_devnum(registry->udev, 'c', buf.st_rdev); if (device->dev == NULL) goto cleanup_device; device->resources = drmModeGetResources(device->fd); if (device->resources == NULL) { fprintf(stderr, "Failed to get resources: %m. consider chvt.\n"); goto cleanup_udev_device; } registry->devices[device_index].created = 1; device->connectors = calloc(device->resources->count_connectors, sizeof *device->connectors); if (device->connectors == NULL) goto cleanup_resources; device->count_connectors = 0; for (i = 0; i < device->resources->count_connectors; ++i) { drmModeConnectorPtr connector = drmModeGetConnector(device->fd, device->resources->connectors[i]); if (connector == NULL) continue; device->connectors[device->count_connectors++] = *connector; drmModeFreeConnector(connector); } return device; cleanup_resources: drmModeFreeResources(device->resources); cleanup_udev_device: udev_device_unref(device->dev); cleanup_fd: close(device->fd); cleanup_device: free(device); return NULL; } WFDint wfd_device_get_attribi(struct wfd_device *device, WFDDeviceAttrib attrib) { switch (attrib) { case WFD_DEVICE_ID: return device->device_id; default: wfd_device_set_error(device, WFD_ERROR_BAD_ATTRIBUTE); break; } return 0; } void * wfd_device_get_udev_device(struct wfd_device *device) { return device->dev; } void * wfd_device_get_resources(struct wfd_device *device) { return device->resources; } uint32_t * wfd_device_get_choosen_crtcs(struct wfd_device *device) { return &device->choosen_crtcs; } int wfd_device_get_fd(struct wfd_device *device) { return device->fd; } static void wfd_device_commit_port(void *port, void *device) { wfd_port_commit(device, port); } int wfd_device_commit(struct wfd_device *device) { wf_handle_foreach(PORT_HANDLE, wfd_device_commit_port, device); return 0; } WFDEventType wfd_device_find_attach_detach(struct wfd_device *device, WFDint *port_id, WFDboolean *state) { drmModeConnectorPtr connector = NULL; WFDEventType event_type = WFD_EVENT_NONE; int i; for (i = 0; i < device->count_connectors && event_type == WFD_EVENT_NONE; ++i) { if (connector) drmModeFreeConnector(connector); connector = drmModeGetConnector(device->fd, device->connectors[i].connector_id); if (connector == NULL) continue; if (connector->connection != device->connectors[i].connection) { event_type = WFD_EVENT_PORT_ATTACH_DETACH; device->connectors[i] = *connector; *port_id = connector->connector_id; *state = connector->connection == DRM_MODE_CONNECTED; /* FIXME: do not break here? * search for more, and queue them? */ break; } } if (connector) drmModeFreeConnector(connector); return event_type; }