uevent机制
uevent把事件上报给用户空间有两种途径:
1. 通过kmod模块,直接调用用户空间的可执行程序或脚本
2. 通过netlink通信机制,将事件从内核空间传递到用户空间
modesetting_drv中关于hotplug接口实现与调用
void
drmmode_uevent_init(ScrnInfoPtr scrn, drmmode_ptr drmmode)
{
#ifdef CONFIG_UDEV_KMS
struct udev *u;
struct udev_monitor *mon;
u = udev_new();
if (!u)
return;
mon = udev_monitor_new_from_netlink(u, "udev");
if (!mon) {
udev_unref(u);
return;
}
if (udev_monitor_filter_add_match_subsystem_devtype(mon,
"drm",
"drm_minor") < 0 ||
udev_monitor_enable_receiving(mon) < 0) {
udev_monitor_unref(mon);
udev_unref(u);
return;
}
drmmode->uevent_handler =
xf86AddGeneralHandler(udev_monitor_get_fd(mon),
drmmode_handle_uevents, drmmode);
drmmode->uevent_monitor = mon;
#endif
}
如下为处理函数:
#ifdef CONFIG_UDEV_KMS
static void
drmmode_handle_uevents(int fd, void *closure)
{
drmmode_ptr drmmode = closure;
struct udev_device *dev;
Bool found = FALSE;
while ((dev = udev_monitor_receive_device(drmmode->uevent_monitor))) {
udev_device_unref(dev);
found = TRUE;
}
if (!found)
return;
drmmode_update_kms_state(drmmode);
}
#endif
主要处理内容为drmmode_update_kms_state(),如下代码所示:
#define DRM_MODE_LINK_STATUS_GOOD 0
#define DRM_MODE_LINK_STATUS_BAD 1
void
drmmode_update_kms_state(drmmode_ptr drmmode)
{
ScrnInfoPtr scrn = drmmode->scrn;
drmModeResPtr mode_res;
xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
int i, j;
Bool found = FALSE;
Bool changed = FALSE;
/* Try to re-set the mode on all the connectors with a BAD link-state:
* This may happen if a link degrades and a new modeset is necessary, using
* different link-training parameters. If the kernel found that the current
* mode is not achievable anymore, it should have pruned the mode before
* sending the hotplug event. Try to re-set the currently-set mode to keep
* the display alive, this will fail if the mode has been pruned.
* In any case, we will send randr events for the Desktop Environment to
* deal with it, if it wants to.
*/
for (i = 0; i < config->num_output; i++) {
xf86OutputPtr output = config->output[i];
drmmode_output_private_ptr drmmode_output = output->driver_private;
drmmode_output_detect(output);
/* Get an updated view of the properties for the current connector and
* look for the link-status property
*/
for (j = 0; j < drmmode_output->num_props; j++) {
drmmode_prop_ptr p = &drmmode_output->props[j];
if (!strcmp(p->mode_prop->name, "link-status")) {
if (p->value == DRM_MODE_LINK_STATUS_BAD) {
xf86CrtcPtr crtc = output->crtc;
if (!crtc)
continue;
/* the connector got a link failure, re-set the current mode */
drmmode_set_mode_major(crtc, &crtc->mode, crtc->rotation,
crtc->x, crtc->y);
xf86DrvMsg(scrn->scrnIndex, X_WARNING,
"hotplug event: connector %u's link-state is BAD, "
"tried resetting the current mode. You may be left"
"with a black screen if this fails...\n",
drmmode_output->mode_output->connector_id);
}
break;
}
}
}
mode_res = drmModeGetResources(drmmode->fd);
if (!mode_res)
goto out;
if (mode_res->count_crtcs != config->num_crtc) {
/* this triggers with Zaphod mode where we don't currently support connector hotplug or MST. */
goto out_free_res;
}
/* figure out if we have gotten rid of any connectors
traverse old output list looking for outputs */
for (i = 0; i < config->num_output; i++) {
xf86OutputPtr output = config->output[i];
drmmode_output_private_ptr drmmode_output;
drmmode_output = output->driver_private;
found = FALSE;
for (j = 0; j < mode_res->count_connectors; j++) {
if (mode_res->connectors[j] == drmmode_output->output_id) {
found = TRUE;
break;
}
}
if (found)
continue;
drmModeFreeConnector(drmmode_output->mode_output);
drmmode_output->mode_output = NULL;
drmmode_output->output_id = -1;
changed = TRUE;
}
/* find new output ids we don't have outputs for */
for (i = 0; i < mode_res->count_connectors; i++) {
found = FALSE;
for (j = 0; j < config->num_output; j++) {
xf86OutputPtr output = config->output[j];
drmmode_output_private_ptr drmmode_output;
drmmode_output = output->driver_private;
if (mode_res->connectors[i] == drmmode_output->output_id) {
found = TRUE;
break;
}
}
if (found)
continue;
changed = TRUE;
drmmode_output_init(scrn, drmmode, mode_res, i, TRUE, 0);
}
if (changed) {
RRSetChanged(xf86ScrnToScreen(scrn));
RRTellChanged(xf86ScrnToScreen(scrn));
}
out_free_res:
/* Check to see if a lessee has disappeared */
drmmode_validate_leases(scrn);
drmModeFreeResources(mode_res);
out:
RRGetInfo(xf86ScrnToScreen(scrn), TRUE);
}
#undef DRM_MODE_LINK_STATUS_BAD
#undef DRM_MODE_LINK_STATUS_GOOD
下面关注drmmode_output_detect():
static xf86OutputStatus
drmmode_output_detect(xf86OutputPtr output)
{
/* go to the hw and retrieve a new output struct */
drmmode_output_private_ptr drmmode_output = output->driver_private;
drmmode_ptr drmmode = drmmode_output->drmmode;
xf86OutputStatus status;
if (drmmode_output->output_id == -1)
return XF86OutputStatusDisconnected;
drmModeFreeConnector(drmmode_output->mode_output);
drmmode_output->mode_output =
drmModeGetConnector(drmmode->fd, drmmode_output->output_id);
if (!drmmode_output->mode_output) {
drmmode_output->output_id = -1;
return XF86OutputStatusDisconnected;
}
drmmode_output_update_properties(output);
switch (drmmode_output->mode_output->connection) {
case DRM_MODE_CONNECTED:
status = XF86OutputStatusConnected;
break;
case DRM_MODE_DISCONNECTED:
status = XF86OutputStatusDisconnected;
break;
default:
case DRM_MODE_UNKNOWNCONNECTION:
status = XF86OutputStatusUnknown;
break;
}
return status;
}
其中output_id为connector_id;drmModeFreeConnector释放drmModeGetConnector分配的drmModeConnectorPtr结构;drmModeGetConnector的作用在于如果connector_id是有效的,获取一个drmModeConnectorPtr结构。下面主要看下drmmode_output_update_properties():
/*
* Update all of the property values for an output
*/
static void
drmmode_output_update_properties(xf86OutputPtr output)
{
drmmode_output_private_ptr drmmode_output = output->driver_private;
int i, j, k;
int err;
drmModeConnectorPtr koutput;
/* Use the most recently fetched values from the kernel */
koutput = drmmode_output->mode_output;
if (!koutput)
return;
for (i = 0; i < drmmode_output->num_props; i++) {
drmmode_prop_ptr p = &drmmode_output->props[i];
for (j = 0; koutput && j < koutput->count_props; j++) {
if (koutput->props[j] == p->mode_prop->prop_id) {
/* Check to see if the property value has changed */
if (koutput->prop_values[j] != p->value) {
p->value = koutput->prop_values[j];
if (p->mode_prop->flags & DRM_MODE_PROP_RANGE) {
INT32 value = p->value;
err = RRChangeOutputProperty(output->randr_output, p->atoms[0],
XA_INTEGER, 32, PropModeReplace, 1,
&value, FALSE, TRUE);
if (err != 0) {
xf86DrvMsg(output->scrn->scrnIndex, X_ERROR,
"RRChangeOutputProperty error, %d\n", err);
}
}
else if (p->mode_prop->flags & DRM_MODE_PROP_ENUM) {
for (k = 0; k < p->mode_prop->count_enums; k++)
if (p->mode_prop->enums[k].value == p->value)
break;
if (k < p->mode_prop->count_enums) {
err = RRChangeOutputProperty(output->randr_output, p->atoms[0],
XA_ATOM, 32, PropModeReplace, 1,
&p->atoms[k + 1], FALSE, TRUE);
if (err != 0) {
xf86DrvMsg(output->scrn->scrnIndex, X_ERROR,
"RRChangeOutputProperty error, %d\n", err);
}
}
}
}
break;
}
}
}
/* Update the CTM property */
if (drmmode_output->ctm_atom) {
err = RRChangeOutputProperty(output->randr_output,
drmmode_output->ctm_atom,
XA_INTEGER, 32, PropModeReplace, 18,
&drmmode_output->ctm,
FALSE, TRUE);
if (err != 0) {
xf86DrvMsg(output->scrn->scrnIndex, X_ERROR,
"RRChangeOutputProperty error, %d\n", err);
}
}
}
通过RRChangeOutputProperty()更新randr显示参数。