6 * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
7 * Copyright (C) 2003 IBM Corp.
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation version 2 of the License.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 675 Mass Ave, Cambridge, MA 02139, USA.
25 * udev database library
35 #include "udev_version.h"
41 static TDB_CONTEXT *udevdb;
44 * busdb_record - bus and id are keys to look up name of device
51 * classdb_record - class name and class device name used as keys to find
54 struct classdb_record {
59 * namedb_record - device name is key, remaining udevice info stored here.
61 struct namedb_record {
62 char sysfs_path[PATH_SIZE];
63 char class_dev_name[NAME_SIZE];
64 char class_name[NAME_SIZE];
67 char driver[NAME_SIZE];
75 * udevdb_close: close udev database
77 static void udevdb_close(void)
86 * udevdb_open: opens udev's database
87 * @method: database can either be in memory - UDEVDB_INTERNAL - or
88 * written to a file with UDEVDB_DEFAULT.
90 static int udevdb_open(int method)
92 udevdb = tdb_open(UDEV_CONFIG_DIR UDEV_DB, 0, method, O_RDWR | O_CREAT, 0644);
94 if (method == UDEVDB_INTERNAL)
95 dbg("Unable to initialize in-memory database");
97 dbg("Unable to initialize database at %s", UDEV_CONFIG_DIR UDEV_DB);
106 static struct busdb_record *busdb_fetch(const char *bus, const char *id)
109 char keystr[BUS_SIZE+ID_SIZE+2];
110 struct busdb_record *rec = NULL;
112 if (bus == NULL || id == NULL)
114 if (strlen(bus) >= BUS_SIZE || strlen(id) >= ID_SIZE)
117 memset(keystr, 0, (BUS_SIZE+ID_SIZE+2));
119 strcat(keystr, UDEVDB_DEL);
122 key.dptr = (void *)keystr;
123 key.dsize = strlen(keystr) + 1;
125 data = tdb_fetch(udevdb, key);
126 if (data.dptr == NULL || data.dsize == 0)
129 rec = (struct busdb_record *)malloc(sizeof(struct busdb_record));
135 memcpy(rec, data.dptr, sizeof(struct busdb_record));
144 static struct classdb_record *classdb_fetch(const char *cls,
148 char keystr[NAME_SIZE+NAME_SIZE+2];
149 struct classdb_record *rec = NULL;
151 if (cls == NULL || cls_dev == NULL)
153 if (strlen(cls) >= NAME_SIZE || strlen(cls_dev) >= NAME_SIZE)
156 memset(keystr, 0, (NAME_SIZE+NAME_SIZE+2));
158 strcat(keystr, UDEVDB_DEL);
159 strcat(keystr, cls_dev);
161 key.dptr = (void *)keystr;
162 key.dsize = strlen(keystr) + 1;
164 data = tdb_fetch(udevdb, key);
165 if (data.dptr == NULL || data.dsize == 0)
168 rec = (struct classdb_record *)malloc(sizeof(struct classdb_record));
174 memcpy(rec, data.dptr, sizeof(struct classdb_record));
183 static struct namedb_record *namedb_fetch(const char *name)
186 char nm_keystr[NAME_SIZE];
187 struct namedb_record *nrec = NULL;
191 if (strlen(name) >= NAME_SIZE)
194 memset(nm_keystr, 0, NAME_SIZE);
195 strcpy(nm_keystr, name);
197 key.dptr = (void *)nm_keystr;
198 key.dsize = strlen(nm_keystr) + 1;
200 data = tdb_fetch(udevdb, key);
201 if (data.dptr == NULL || data.dsize == 0)
204 nrec = (struct namedb_record *)malloc(sizeof(struct namedb_record));
210 memcpy(nrec, data.dptr, sizeof(struct namedb_record));
219 static int busdb_store(const struct udevice *dev)
222 char keystr[BUS_SIZE+ID_SIZE+2];
223 struct busdb_record rec;
229 memset(keystr, 0, (BUS_SIZE+ID_SIZE+2));
230 strcpy(keystr, dev->bus_name);
231 strcat(keystr, UDEVDB_DEL);
232 strcat(keystr, dev->bus_id);
234 key.dptr = (void *)keystr;
235 key.dsize = strlen(keystr) + 1;
237 strcpy(rec.name, dev->name);
239 data.dptr = (void *) &rec;
240 data.dsize = sizeof(rec);
242 retval = tdb_store(udevdb, key, data, TDB_REPLACE);
249 static int classdb_store(const struct udevice *dev)
252 char keystr[NAME_SIZE+NAME_SIZE+2];
253 struct classdb_record rec;
259 memset(keystr, 0, (NAME_SIZE+NAME_SIZE+2));
260 strcpy(keystr, dev->class_name);
261 strcat(keystr, UDEVDB_DEL);
262 strcat(keystr, dev->class_dev_name);
264 key.dptr = (void *)keystr;
265 key.dsize = strlen(keystr) + 1;
267 strcpy(rec.name, dev->name);
269 data.dptr = (void *) &rec;
270 data.dsize = sizeof(rec);
272 retval = tdb_store(udevdb, key, data, TDB_REPLACE);
279 static int namedb_store(const struct udevice *dev)
282 char keystr[NAME_SIZE];
283 struct namedb_record rec;
289 memset(keystr, 0, NAME_SIZE);
290 strcpy(keystr, dev->name);
292 key.dptr = (void *)keystr;
293 key.dsize = strlen(keystr) + 1;
295 strcpy(rec.sysfs_path, dev->sysfs_path);
296 strcpy(rec.bus, dev->bus_name);
297 strcpy(rec.id, dev->bus_id);
298 strcpy(rec.class_dev_name, dev->class_dev_name);
299 strcpy(rec.class_name, dev->class_name);
300 strcpy(rec.driver, dev->driver);
301 rec.type = dev->type;
302 rec.major = dev->major;
303 rec.minor = dev->minor;
304 rec.mode = dev->mode;
306 data.dptr = (void *) &rec;
307 data.dsize = sizeof(rec);
309 retval = tdb_store(udevdb, key, data, TDB_REPLACE);
316 static int busdb_delete(const char *bus, const char *id)
319 char keystr[BUS_SIZE+ID_SIZE+2];
322 if (bus == NULL || id == NULL)
324 if (strlen(bus) >= BUS_SIZE || strlen(id) >= ID_SIZE)
327 memset(keystr, 0, (BUS_SIZE+ID_SIZE+2));
329 strcat(keystr, UDEVDB_DEL);
332 key.dptr = (void *)keystr;
333 key.dsize = strlen(keystr) + 1;
335 retval = tdb_delete(udevdb, key);
342 static int classdb_delete(const char *cls, const char *cls_dev)
345 char keystr[NAME_SIZE+NAME_SIZE+2];
348 if (cls == NULL || cls_dev == NULL)
350 if (strlen(cls) >= NAME_SIZE || strlen(cls_dev) >= NAME_SIZE)
353 memset(keystr, 0, (NAME_SIZE+NAME_SIZE+2));
355 strcat(keystr, UDEVDB_DEL);
356 strcat(keystr, cls_dev);
358 key.dptr = (void *)keystr;
359 key.dsize = strlen(keystr) + 1;
361 retval = tdb_delete(udevdb, key);
368 static int namedb_delete(const char *name)
371 char keystr[NAME_SIZE];
376 if (strlen(name) >= NAME_SIZE)
379 memset(keystr, 0, NAME_SIZE);
380 strcpy(keystr, name);
382 key.dptr = (void *)keystr;
383 key.dsize = strlen(keystr) + 1;
385 retval = tdb_delete(udevdb, key);
390 * udevdb_delete_udevice
392 int udevdb_delete_udevice(const char *name)
394 struct namedb_record *nrec = NULL;
399 nrec = namedb_fetch(name);
403 busdb_delete(nrec->bus, nrec->id);
404 classdb_delete(nrec->class_name, nrec->class_dev_name);
412 * udevdb_add_udevice: adds udevice to database
414 int udevdb_add_udevice(const struct udevice *dev)
419 if ((busdb_store(dev)) != 0)
421 if ((classdb_store(dev)) != 0)
423 if ((namedb_store(dev)) != 0)
430 * udevdb_get_device: grab's device by name
432 struct udevice *udevdb_get_udevice(const char *name)
434 struct namedb_record *nrec = NULL;
435 struct udevice *dev = NULL;
440 nrec = namedb_fetch(name);
444 dev = (struct udevice *)malloc(sizeof(struct udevice));
450 strcpy(dev->name, name);
451 strcpy(dev->sysfs_path, nrec->sysfs_path);
452 strcpy(dev->class_dev_name, nrec->class_dev_name);
453 strcpy(dev->class_name, nrec->class_name);
454 strcpy(dev->bus_name, nrec->bus);
455 strcpy(dev->bus_id, nrec->id);
456 dev->type = nrec->type;
457 dev->major = nrec->major;
458 dev->minor = nrec->minor;
459 dev->mode = nrec->mode;
467 * udevdb_get_device_by_bus
469 struct udevice *udevdb_get_udevice_by_bus(const char *bus, const char *id)
471 struct busdb_record *brec = NULL;
472 struct udevice *dev = NULL;
474 if (bus == NULL || id == NULL)
477 brec = busdb_fetch(bus, id);
481 dev = udevdb_get_udevice(brec->name);
488 * udevdb_get_udevice_by_class
490 struct udevice *udevdb_get_udevice_by_class(const char *cls,
493 struct classdb_record *crec = NULL;
494 struct udevice *dev = NULL;
496 if (cls == NULL || cls_dev == NULL)
499 crec = classdb_fetch(cls, cls_dev);
503 dev = udevdb_get_udevice(crec->name);
510 * udevdb_exit: closes database
512 void udevdb_exit(void)
518 * udevdb_init: initializes database
520 int udevdb_init(int init_flag)
522 if (init_flag != UDEVDB_DEFAULT && init_flag != UDEVDB_INTERNAL)
525 return udevdb_open(init_flag);