Subject: MFC: more changes for envsys2
To: None <tech-kern@netbsd.org>
From: Juan RP <juan@xtrarom.org>
List: tech-kern
Date: 11/09/2007 04:26:42
Hey,

I'm going to make the following changes to envsys2 and all its users:

- There's now a TAILQ with envsys_data_t structs for the list of sensors
  that a sysmon envsys device maintains.

- There's now a linked list for the events that a sysmon_envsys device
  maintains.

- No more global sysctl for the workqueue thread, there's now a
  workqueue/callout per device and the refresh timeout is configurable
  per device and via envsys.conf (or the appropiate ioctl).

The API has been modified as follow:

struct sysmon_envsys *sysmon_envsys_create(void)

- Added sysmon_envsys_create() that allocates a struct sysmon_envsys
  and initializes the TAILQ for sensors and the linked list for the events.

void sysmon_envsys_destroy(struct sysmon_envsys *)

- Added sysmon_envsys_destroy() that removes all envsys_data_t structs
  from the TAILQ and frees the sysmon_envsys struct.

int sysmon_envsys_sensor_attach(struct sysmon_envsys *, envsys_data_t *)

- Added sysmon_envsys_sensor_attach() that registers a new sensor into the
  TAILQ of the sysmon_envsys device passed as first argument. This also
  checks for duplicate and empty descriptions and increments the
  sme_nsensors count.

int sysmon_envsys_sensor_detach(struct sysmon_envsys *, envsys_data_t *)

- Added sysmon_envsys_sensor_detach() that unregisters a sensor from the
  TAILQ of the sysmon_envsys device passed as first argument.

void (*sme_refresh)(struct sysmon_envsys *, envsys_data_t *)

- the sme_gtredata function callback has been renamed to sme_refresh and
  returns void. There's no point in returning int when the returned error
  is simply ignored.

Example of the new code I've specified above: (note that I simply destroy
the sysmon_envsys object and return if there's an error on
sysmon_envsys_sensor_attach(), you could simply ignore the error and
continue attaching the next sensors)

struct foo_softc {
	...
	struct sysmon_envsys *sc_sme;
	envsys_data_t sc_sensor[MAX_SENSORS];
	...
};

void
foo_attach(...) {
	...
	sc->sc_sme = sysmon_envsys_create();
	for (i = 0; i < MAX_SENSORS; i++) {
		sc->sc_sensor[i].units = ENVSYS_STEMP;
		strlcpy(sc->sc_sensor[i].desc, ...);
		if (sysmon_envsys_sensor_attach(sc->sc_sme,
						&sc->sc_sensor[i])) {
			sysmon_envsys_destroy(sc->sc_sme);
			return;
	}

	sc->sc_sme->sme_name = device_xname(self);
	sc->sc_sme->sme_cookie = sc;
	sc->sc_sme->sme_refresh = aiboost_refresh_sensors;
	if ((err = sysmon_envsys_register(sc->sc_sme))) {
		aprint_error_dev(self, "blah blah");
		sysmon_envsys_destroy(sc->sc_sme);
	}
	...
}

That's it... the new rules are:

- Use sysmon_envsys_create() to allocate the sysmon_envsys object
  (and initialize the TAILQ for envsys_data_t's and the linked list for the
  events).
- Initialize the sensors with proper units type and description (no more
  sensor index or state stuff).
- Attach the sensor with sysmon_envsys_sensor_attach() to the sysmon_envsys 
  object specified.
- sysmon_envsys_destroy() must be used to release the sysmon_envsys object
  and remove all sensors from the TAILQ.

Do anyone have any comment? (I'm not interested on naming or
style bikesheds, so please only technical comments).

If no comments, I'll change the API and all its users in 1 week.

-- 
Juan Romero Pardines	- The NetBSD Project
http://plog.xtrarom.org	- NetBSD/pkgsrc news in Spanish