+struct sensor_t sensor_desc[MAX_SENSORS]; /* Android-level descriptors */
+sensor_info_t sensor[MAX_SENSORS]; /* Internal descriptors */
+int sensor_count; /* Detected sensors */
+
+
+/* if the sensor has an _en attribute, we need to enable it */
+int get_needs_enable(int dev_num, const char *tag)
+{
+ char sysfs_path[PATH_MAX];
+ int fd;
+
+ sprintf(sysfs_path, SENSOR_ENABLE_PATH, dev_num, tag);
+
+ fd = open(sysfs_path, O_RDWR);
+ if (fd == -1)
+ return 0;
+
+ close(fd);
+ return 1;
+}
+
+static void setup_properties_from_pld (int s, int panel, int rotation,
+ int num_channels)
+{
+ /*
+ * Generate suitable order and opt_scale directives from the PLD panel
+ * and rotation codes we got. This can later be superseded by the usual
+ * properties if necessary. Eventually we'll need to replace these
+ * mechanisms by a less convoluted one, such as a 3x3 placement matrix.
+ */
+
+ int x = 1;
+ int y = 1;
+ int z = 1;
+ int xy_swap = 0;
+ int angle = rotation * 45;
+
+ /* Only deal with 3 axis chips for now */
+ if (num_channels < 3)
+ return;
+
+ if (panel == PANEL_BACK) {
+ /* Chip placed on the back panel ; negate x and z */
+ x = -x;
+ z = -z;
+ }
+
+ switch (angle) {
+ case 90: /* 90° clockwise: negate y then swap x,y */
+ xy_swap = 1;
+ y = -y;
+ break;
+
+ case 180: /* Upside down: negate x and y */
+ x = -x;
+ y = -y;
+ break;
+
+ case 270: /* 90° counter clockwise: negate x then swap x,y */
+ x = -x;
+ xy_swap = 1;
+ break;
+ }
+
+ if (xy_swap) {
+ sensor[s].order[0] = 1;
+ sensor[s].order[1] = 0;
+ sensor[s].order[2] = 2;
+ sensor[s].quirks |= QUIRK_FIELD_ORDERING;
+ }
+
+ sensor[s].channel[0].opt_scale = x;
+ sensor[s].channel[1].opt_scale = y;
+ sensor[s].channel[2].opt_scale = z;
+}
+
+
+static int is_valid_pld (int panel, int rotation)
+{
+ if (panel != PANEL_FRONT && panel != PANEL_BACK) {
+ ALOGW("Unhandled PLD panel spec: %d\n", panel);
+ return 0;
+ }
+
+ /* Only deal with 90° rotations for now */
+ if (rotation < 0 || rotation > 7 || (rotation & 1)) {
+ ALOGW("Unhandled PLD rotation spec: %d\n", rotation);
+ return 0;
+ }
+
+ return 1;
+}
+
+
+static int read_pld_from_properties (int s, int* panel, int* rotation)
+{
+ int p, r;
+
+ if (sensor_get_prop(s, "panel", &p))
+ return -1;
+
+ if (sensor_get_prop(s, "rotation", &r))
+ return -1;
+
+ if (!is_valid_pld(p, r))
+ return -1;
+
+ *panel = p;
+ *rotation = r;
+
+ ALOGI("S%d PLD from properties: panel=%d, rotation=%d\n", s, p, r);
+
+ return 0;
+}
+
+
+static int read_pld_from_sysfs (int s, int dev_num, int* panel, int* rotation)
+{
+ char sysfs_path[PATH_MAX];
+ int p,r;
+
+ sprintf(sysfs_path, BASE_PATH "../firmware_node/pld/panel", dev_num);
+
+ if (sysfs_read_int(sysfs_path, &p))
+ return -1;
+
+ sprintf(sysfs_path, BASE_PATH "../firmware_node/pld/rotation", dev_num);
+
+ if (sysfs_read_int(sysfs_path, &r))
+ return -1;
+
+ if (!is_valid_pld(p, r))
+ return -1;
+
+ *panel = p;
+ *rotation = r;
+
+ ALOGI("S%d PLD from sysfs: panel=%d, rotation=%d\n", s, p, r);
+
+ return 0;
+}
+
+
+static void decode_placement_information (int dev_num, int num_channels, int s)
+{
+ /*
+ * See if we have optional "physical location of device" ACPI tags.
+ * We're only interested in panel and rotation specifiers. Use the
+ * .panel and .rotation properties in priority, and the actual ACPI
+ * values as a second source.
+ */
+
+ int panel;
+ int rotation;
+
+ if (read_pld_from_properties(s, &panel, &rotation) &&
+ read_pld_from_sysfs(s, dev_num, &panel, &rotation))
+ return; /* No PLD data available */
+
+ /* Map that to field ordering and scaling mechanisms */
+ setup_properties_from_pld(s, panel, rotation, num_channels);
+}
+
+
+static void populate_descriptors (int s, int sensor_type)
+{
+ int32_t min_delay_us;
+ max_delay_t max_delay_us;
+
+ /* Initialize Android-visible descriptor */
+ sensor_desc[s].name = sensor_get_name(s);
+ sensor_desc[s].vendor = sensor_get_vendor(s);
+ sensor_desc[s].version = sensor_get_version(s);
+ sensor_desc[s].handle = s;
+ sensor_desc[s].type = sensor_type;
+
+ sensor_desc[s].maxRange = sensor_get_max_range(s);
+ sensor_desc[s].resolution = sensor_get_resolution(s);
+ sensor_desc[s].power = sensor_get_power(s);
+ sensor_desc[s].stringType = sensor_get_string_type(s);
+
+ /* None of our supported sensors requires a special permission */
+ sensor_desc[s].requiredPermission = "";
+
+ sensor_desc[s].flags = sensor_get_flags(s);
+ sensor_desc[s].minDelay = sensor_get_min_delay(s);
+ sensor_desc[s].maxDelay = sensor_get_max_delay(s);
+
+ ALOGV("Sensor %d (%s) type(%d) minD(%d) maxD(%d) flags(%2.2x)\n",
+ s, sensor[s].friendly_name, sensor_desc[s].type,
+ sensor_desc[s].minDelay, sensor_desc[s].maxDelay,
+ sensor_desc[s].flags);
+
+ /* We currently do not implement batching */
+ sensor_desc[s].fifoReservedEventCount = 0;
+ sensor_desc[s].fifoMaxEventCount = 0;
+
+ min_delay_us = sensor_desc[s].minDelay;
+ max_delay_us = sensor_desc[s].maxDelay;
+
+ sensor[s].min_supported_rate = max_delay_us ? 1000000.0 / max_delay_us : 1;
+ sensor[s].max_supported_rate = min_delay_us && min_delay_us != -1 ? 1000000.0 / min_delay_us : 0;
+}
+
+
+static void add_virtual_sensor (int catalog_index)
+{
+ int s;
+ int sensor_type;
+
+ if (sensor_count == MAX_SENSORS) {
+ ALOGE("Too many sensors!\n");
+ return;
+ }
+
+ sensor_type = sensor_catalog[catalog_index].type;
+
+ s = sensor_count;
+
+ sensor[s].is_virtual = 1;
+ sensor[s].catalog_index = catalog_index;
+ sensor[s].type = sensor_type;