]>
Commit | Line | Data |
---|---|---|
2232cac8 GKH |
1 | /* |
2 | * namedev.c | |
3 | * | |
4 | * Userspace devfs | |
5 | * | |
6 | * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com> | |
7 | * | |
8 | * | |
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. | |
12 | * | |
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. | |
17 | * | |
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. | |
21 | * | |
22 | */ | |
23 | ||
24 | #include <stddef.h> | |
25 | #include <stdlib.h> | |
26 | #include <string.h> | |
27 | #include <stdio.h> | |
28 | #include <fcntl.h> | |
29 | #include <ctype.h> | |
30 | #include <unistd.h> | |
31 | #include <errno.h> | |
c27e6911 | 32 | #include <sys/wait.h> |
2232cac8 GKH |
33 | |
34 | #include "list.h" | |
35 | #include "udev.h" | |
36 | #include "udev_version.h" | |
37 | #include "namedev.h" | |
185a35a4 | 38 | #include "libsysfs/libsysfs.h" |
2023350e | 39 | #include "klibc_fixups.h" |
2232cac8 | 40 | |
19feb351 | 41 | LIST_HEAD(config_device_list); |
61219c75 | 42 | LIST_HEAD(perm_device_list); |
2232cac8 | 43 | |
9f1da361 KS |
44 | /* compare string with pattern (supports * ? [0-9] [!A-Z]) */ |
45 | static int strcmp_pattern(const char *p, const char *s) | |
c124eafa | 46 | { |
9f1da361 KS |
47 | if (*s == '\0') { |
48 | while (*p == '*') | |
49 | p++; | |
50 | return (*p != '\0'); | |
51 | } | |
52 | switch (*p) { | |
53 | case '[': | |
54 | { | |
55 | int not = 0; | |
56 | p++; | |
57 | if (*p == '!') { | |
58 | not = 1; | |
59 | p++; | |
60 | } | |
61 | while (*p && (*p != ']')) { | |
62 | int match = 0; | |
63 | if (p[1] == '-') { | |
64 | if ((*s >= *p) && (*s <= p[2])) | |
65 | match = 1; | |
66 | p += 3; | |
67 | } else { | |
68 | match = (*p == *s); | |
69 | p++; | |
70 | } | |
71 | if (match ^ not) { | |
72 | while (*p && (*p != ']')) | |
73 | p++; | |
74 | return strcmp_pattern(p+1, s+1); | |
75 | } | |
76 | } | |
77 | } | |
78 | break; | |
79 | case '*': | |
80 | if (strcmp_pattern(p, s+1)) | |
81 | return strcmp_pattern(p+1, s); | |
82 | return 0; | |
83 | case '\0': | |
84 | if (*s == '\0') { | |
85 | return 0; | |
86 | } | |
87 | break; | |
88 | default: | |
89 | if ((*p == *s) || (*p == '?')) | |
90 | return strcmp_pattern(p+1, s+1); | |
91 | break; | |
92 | } | |
93 | return 1; | |
c124eafa KS |
94 | } |
95 | ||
2232cac8 GKH |
96 | #define copy_var(a, b, var) \ |
97 | if (b->var) \ | |
469c7cff | 98 | a->var = b->var; |
2232cac8 GKH |
99 | |
100 | #define copy_string(a, b, var) \ | |
101 | if (strlen(b->var)) \ | |
469c7cff | 102 | strcpy(a->var, b->var); |
2232cac8 | 103 | |
19feb351 | 104 | int add_config_dev(struct config_device *new_dev) |
2232cac8 GKH |
105 | { |
106 | struct list_head *tmp; | |
107 | struct config_device *tmp_dev; | |
108 | ||
09e52d51 | 109 | /* update the values if we already have the device */ |
2232cac8 GKH |
110 | list_for_each(tmp, &config_device_list) { |
111 | struct config_device *dev = list_entry(tmp, struct config_device, node); | |
9f1da361 | 112 | if (strcmp_pattern(new_dev->name, dev->name)) |
c124eafa | 113 | continue; |
137af0cc GKH |
114 | if (strncmp(dev->bus, new_dev->bus, sizeof(dev->name))) |
115 | continue; | |
09e52d51 | 116 | copy_var(dev, new_dev, type); |
09e52d51 KS |
117 | copy_string(dev, new_dev, bus); |
118 | copy_string(dev, new_dev, sysfs_file); | |
119 | copy_string(dev, new_dev, sysfs_value); | |
120 | copy_string(dev, new_dev, id); | |
121 | copy_string(dev, new_dev, place); | |
122 | copy_string(dev, new_dev, kernel_name); | |
137af0cc | 123 | copy_string(dev, new_dev, exec_program); |
3d150dfb | 124 | copy_string(dev, new_dev, symlink); |
09e52d51 | 125 | return 0; |
2232cac8 GKH |
126 | } |
127 | ||
8f43a65e | 128 | /* not found, add new structure to the device list */ |
2232cac8 GKH |
129 | tmp_dev = malloc(sizeof(*tmp_dev)); |
130 | if (!tmp_dev) | |
131 | return -ENOMEM; | |
132 | memcpy(tmp_dev, new_dev, sizeof(*tmp_dev)); | |
8cf7ebe8 | 133 | list_add_tail(&tmp_dev->node, &config_device_list); |
19feb351 | 134 | //dump_config_dev(tmp_dev); |
469c7cff GKH |
135 | return 0; |
136 | } | |
2232cac8 | 137 | |
61219c75 GKH |
138 | int add_perm_dev(struct perm_device *new_dev) |
139 | { | |
140 | struct list_head *tmp; | |
141 | struct perm_device *tmp_dev; | |
142 | ||
143 | /* update the values if we already have the device */ | |
144 | list_for_each(tmp, &perm_device_list) { | |
145 | struct perm_device *dev = list_entry(tmp, struct perm_device, node); | |
146 | if (strcmp_pattern(new_dev->name, dev->name)) | |
147 | continue; | |
148 | copy_var(dev, new_dev, mode); | |
149 | copy_string(dev, new_dev, owner); | |
150 | copy_string(dev, new_dev, group); | |
151 | return 0; | |
152 | } | |
153 | ||
154 | /* not found, add new structure to the perm list */ | |
155 | tmp_dev = malloc(sizeof(*tmp_dev)); | |
156 | if (!tmp_dev) | |
157 | return -ENOMEM; | |
158 | memcpy(tmp_dev, new_dev, sizeof(*tmp_dev)); | |
159 | list_add_tail(&tmp_dev->node, &perm_device_list); | |
160 | //dump_perm_dev(tmp_dev); | |
161 | return 0; | |
162 | } | |
163 | ||
164 | static struct perm_device *find_perm(char *name) | |
165 | { | |
166 | struct list_head *tmp; | |
167 | struct perm_device *perm = NULL; | |
168 | ||
169 | list_for_each(tmp, &perm_device_list) { | |
170 | perm = list_entry(tmp, struct perm_device, node); | |
171 | if (strcmp_pattern(perm->name, name)) | |
172 | continue; | |
173 | return perm; | |
174 | } | |
175 | return NULL; | |
176 | } | |
177 | ||
c2405f50 | 178 | static mode_t get_default_mode(struct sysfs_class_device *class_dev) |
185a35a4 | 179 | { |
89571022 GKH |
180 | mode_t mode = 0600; /* default to owner rw only */ |
181 | ||
182 | if (strlen(default_mode_str) != 0) { | |
183 | mode = strtol(default_mode_str, NULL, 8); | |
184 | } | |
185 | return mode; | |
185a35a4 GKH |
186 | } |
187 | ||
f3b04a2e GKH |
188 | static void build_kernel_number(struct sysfs_class_device *class_dev, struct udevice *udev) |
189 | { | |
190 | char *dig; | |
191 | ||
192 | /* FIXME, figure out how to handle stuff like sdaj which will not work right now. */ | |
193 | dig = class_dev->name + strlen(class_dev->name); | |
194 | while (isdigit(*(dig-1))) | |
195 | dig--; | |
196 | strfieldcpy(udev->kernel_number, dig); | |
f7b4eca4 | 197 | dbg("kernel_number='%s'", udev->kernel_number); |
f3b04a2e GKH |
198 | } |
199 | ||
200 | static void apply_format(struct udevice *udev, unsigned char *string) | |
201 | { | |
202 | char name[NAME_SIZE]; | |
203 | char *pos; | |
204 | ||
205 | while (1) { | |
206 | pos = strchr(string, '%'); | |
207 | ||
208 | if (pos) { | |
209 | strfieldcpy(name, pos+2); | |
210 | *pos = 0x00; | |
211 | switch (pos[1]) { | |
212 | case 'b': | |
213 | if (strlen(udev->bus_id) == 0) | |
214 | break; | |
6968d494 | 215 | strcat(pos, udev->bus_id); |
f3b04a2e GKH |
216 | dbg("substitute bus_id '%s'", udev->bus_id); |
217 | break; | |
218 | case 'n': | |
219 | if (strlen(udev->kernel_number) == 0) | |
220 | break; | |
221 | strcat(pos, udev->kernel_number); | |
222 | dbg("substitute kernel number '%s'", udev->kernel_number); | |
223 | break; | |
5c6f0f14 AB |
224 | case 'D': |
225 | if (strlen(udev->kernel_number) == 0) { | |
226 | strcat(pos, "disk"); | |
227 | break; | |
228 | } | |
229 | strcat(pos, "part"); | |
230 | strcat(pos, udev->kernel_number); | |
231 | dbg("substitute kernel number '%s'", udev->kernel_number); | |
232 | break; | |
f3b04a2e GKH |
233 | case 'm': |
234 | sprintf(pos, "%u", udev->minor); | |
235 | dbg("substitute minor number '%u'", udev->minor); | |
236 | break; | |
237 | case 'M': | |
238 | sprintf(pos, "%u", udev->major); | |
239 | dbg("substitute major number '%u'", udev->major); | |
240 | break; | |
241 | case 'c': | |
242 | if (strlen(udev->callout_value) == 0) | |
243 | break; | |
244 | strcat(pos, udev->callout_value); | |
245 | dbg("substitute callout output '%s'", udev->callout_value); | |
246 | break; | |
247 | default: | |
248 | dbg("unknown substitution type '%%%c'", pos[1]); | |
249 | break; | |
250 | } | |
251 | strcat(string, name); | |
252 | } else | |
253 | break; | |
254 | } | |
255 | } | |
256 | ||
19dc5d4c | 257 | |
c27e6911 PM |
258 | static int exec_callout(struct config_device *dev, char *value, int len) |
259 | { | |
260 | int retval; | |
261 | int res; | |
262 | int status; | |
263 | int fds[2]; | |
264 | pid_t pid; | |
265 | int value_set = 0; | |
266 | char buffer[256]; | |
bc434511 KS |
267 | char *arg; |
268 | char *args[CALLOUT_MAXARG]; | |
269 | int i; | |
c27e6911 | 270 | |
8f43a65e | 271 | dbg("callout to '%s'", dev->exec_program); |
c27e6911 PM |
272 | retval = pipe(fds); |
273 | if (retval != 0) { | |
274 | dbg("pipe failed"); | |
275 | return -1; | |
276 | } | |
277 | pid = fork(); | |
278 | if (pid == -1) { | |
279 | dbg("fork failed"); | |
280 | return -1; | |
281 | } | |
282 | ||
283 | if (pid == 0) { | |
8f43a65e | 284 | /* child */ |
c27e6911 PM |
285 | close(STDOUT_FILENO); |
286 | dup(fds[1]); /* dup write side of pipe to STDOUT */ | |
bc434511 KS |
287 | if (strchr(dev->exec_program, ' ')) { |
288 | /* callout with arguments */ | |
289 | arg = dev->exec_program; | |
290 | for (i=0; i < CALLOUT_MAXARG-1; i++) { | |
291 | args[i] = strsep(&arg, " "); | |
292 | if (args[i] == NULL) | |
293 | break; | |
294 | } | |
295 | if (args[i]) { | |
53dc383e | 296 | dbg("too many args - %d", i); |
bc434511 KS |
297 | args[i] = NULL; |
298 | } | |
299 | retval = execve(args[0], args, main_envp); | |
300 | } else { | |
301 | retval = execve(dev->exec_program, main_argv, main_envp); | |
302 | } | |
c27e6911 PM |
303 | if (retval != 0) { |
304 | dbg("child execve failed"); | |
305 | exit(1); | |
306 | } | |
307 | return -1; /* avoid compiler warning */ | |
308 | } else { | |
8f43a65e | 309 | /* parent reads from fds[0] */ |
c27e6911 PM |
310 | close(fds[1]); |
311 | retval = 0; | |
312 | while (1) { | |
313 | res = read(fds[0], buffer, sizeof(buffer) - 1); | |
314 | if (res <= 0) | |
315 | break; | |
316 | buffer[res] = '\0'; | |
317 | if (res > len) { | |
f7b4eca4 | 318 | dbg("callout len %d too short", len); |
c27e6911 PM |
319 | retval = -1; |
320 | } | |
321 | if (value_set) { | |
322 | dbg("callout value already set"); | |
323 | retval = -1; | |
324 | } else { | |
325 | value_set = 1; | |
326 | strncpy(value, buffer, len); | |
327 | } | |
328 | } | |
bc434511 | 329 | dbg("callout returned '%s'", value); |
c27e6911 PM |
330 | close(fds[0]); |
331 | res = wait(&status); | |
332 | if (res < 0) { | |
333 | dbg("wait failed result %d", res); | |
334 | retval = -1; | |
335 | } | |
336 | ||
1e959a4b | 337 | #ifndef __KLIBC__ |
c27e6911 PM |
338 | if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) { |
339 | dbg("callout program status 0x%x", status); | |
340 | retval = -1; | |
341 | } | |
1e959a4b | 342 | #endif |
c27e6911 PM |
343 | } |
344 | return retval; | |
345 | } | |
346 | ||
137af0cc | 347 | static int do_callout(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device) |
120d45d0 GKH |
348 | { |
349 | struct config_device *dev; | |
350 | struct list_head *tmp; | |
120d45d0 GKH |
351 | |
352 | list_for_each(tmp, &config_device_list) { | |
353 | dev = list_entry(tmp, struct config_device, node); | |
354 | if (dev->type != CALLOUT) | |
355 | continue; | |
f3b04a2e | 356 | |
137af0cc | 357 | if (sysfs_device) { |
19feb351 | 358 | dbg("dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus); |
137af0cc GKH |
359 | if (strcasecmp(dev->bus, sysfs_device->bus) != 0) |
360 | continue; | |
361 | } | |
362 | ||
f3b04a2e GKH |
363 | /* substitute anything that needs to be in the program name */ |
364 | apply_format(udev, dev->exec_program); | |
365 | if (exec_callout(dev, udev->callout_value, NAME_SIZE)) | |
120d45d0 | 366 | continue; |
9f1da361 | 367 | if (strcmp_pattern(dev->id, udev->callout_value) != 0) |
120d45d0 | 368 | continue; |
70033702 | 369 | strfieldcpy(udev->name, dev->name); |
3d150dfb | 370 | strfieldcpy(udev->symlink, dev->symlink); |
61219c75 GKH |
371 | dbg("callout returned matching value '%s', '%s' becomes '%s'", |
372 | dev->id, class_dev->name, udev->name); | |
120d45d0 GKH |
373 | return 0; |
374 | } | |
375 | return -ENODEV; | |
376 | } | |
377 | ||
ca1cc0fe GKH |
378 | static int do_label(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device) |
379 | { | |
380 | struct sysfs_attribute *tmpattr = NULL; | |
381 | struct config_device *dev; | |
382 | struct list_head *tmp; | |
28d6536a | 383 | char *c; |
ca1cc0fe GKH |
384 | |
385 | list_for_each(tmp, &config_device_list) { | |
386 | dev = list_entry(tmp, struct config_device, node); | |
387 | if (dev->type != LABEL) | |
388 | continue; | |
389 | ||
137af0cc | 390 | if (sysfs_device) { |
19feb351 | 391 | dbg("dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus); |
137af0cc GKH |
392 | if (strcasecmp(dev->bus, sysfs_device->bus) != 0) |
393 | continue; | |
394 | } | |
395 | ||
19feb351 | 396 | dbg("look for device attribute '%s'", dev->sysfs_file); |
ca1cc0fe GKH |
397 | /* try to find the attribute in the class device directory */ |
398 | tmpattr = sysfs_get_classdev_attr(class_dev, dev->sysfs_file); | |
399 | if (tmpattr) | |
400 | goto label_found; | |
401 | ||
402 | /* look in the class device directory if present */ | |
403 | if (sysfs_device) { | |
404 | tmpattr = sysfs_get_device_attr(sysfs_device, dev->sysfs_file); | |
405 | if (tmpattr) | |
406 | goto label_found; | |
407 | } | |
408 | ||
409 | continue; | |
410 | ||
411 | label_found: | |
28d6536a KS |
412 | c = tmpattr->value + strlen(tmpattr->value)-1; |
413 | if (*c == '\n') | |
414 | *c = 0x00; | |
19feb351 | 415 | dbg("compare attribute '%s' value '%s' with '%s'", |
8f43a65e | 416 | dev->sysfs_file, tmpattr->value, dev->sysfs_value); |
83be97ba | 417 | if (strcmp_pattern(dev->sysfs_value, tmpattr->value) != 0) |
ca1cc0fe GKH |
418 | continue; |
419 | ||
70033702 | 420 | strfieldcpy(udev->name, dev->name); |
3d150dfb | 421 | strfieldcpy(udev->symlink, dev->symlink); |
61219c75 GKH |
422 | dbg("found matching attribute '%s', '%s' becomes '%s' ", |
423 | dev->sysfs_file, class_dev->name, udev->name); | |
ca1cc0fe GKH |
424 | |
425 | return 0; | |
426 | } | |
427 | return -ENODEV; | |
428 | } | |
429 | ||
430 | static int do_number(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device) | |
431 | { | |
432 | struct config_device *dev; | |
433 | struct list_head *tmp; | |
434 | char path[SYSFS_PATH_MAX]; | |
435 | int found; | |
436 | char *temp = NULL; | |
437 | ||
438 | /* we have to have a sysfs device for NUMBER to work */ | |
439 | if (!sysfs_device) | |
440 | return -ENODEV; | |
441 | ||
442 | list_for_each(tmp, &config_device_list) { | |
443 | dev = list_entry(tmp, struct config_device, node); | |
444 | if (dev->type != NUMBER) | |
445 | continue; | |
446 | ||
19feb351 | 447 | dbg("dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus); |
137af0cc GKH |
448 | if (strcasecmp(dev->bus, sysfs_device->bus) != 0) |
449 | continue; | |
450 | ||
ca1cc0fe | 451 | found = 0; |
70033702 | 452 | strfieldcpy(path, sysfs_device->path); |
ca1cc0fe | 453 | temp = strrchr(path, '/'); |
19feb351 | 454 | dbg("search '%s' in '%s', path='%s'", dev->id, temp, path); |
ca1cc0fe GKH |
455 | if (strstr(temp, dev->id) != NULL) { |
456 | found = 1; | |
457 | } else { | |
458 | *temp = 0x00; | |
459 | temp = strrchr(path, '/'); | |
19feb351 | 460 | dbg("search '%s' in '%s', path='%s'", dev->id, temp, path); |
ca1cc0fe GKH |
461 | if (strstr(temp, dev->id) != NULL) |
462 | found = 1; | |
463 | } | |
464 | if (!found) | |
465 | continue; | |
70033702 | 466 | strfieldcpy(udev->name, dev->name); |
3d150dfb | 467 | strfieldcpy(udev->symlink, dev->symlink); |
61219c75 GKH |
468 | dbg("found matching id '%s', '%s' becomes '%s'", |
469 | dev->id, class_dev->name, udev->name); | |
ca1cc0fe GKH |
470 | return 0; |
471 | } | |
472 | return -ENODEV; | |
473 | } | |
474 | ||
8c51bbfe GKH |
475 | static int do_topology(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device) |
476 | { | |
477 | struct config_device *dev; | |
478 | struct list_head *tmp; | |
479 | char path[SYSFS_PATH_MAX]; | |
480 | int found; | |
481 | char *temp = NULL; | |
482 | ||
483 | /* we have to have a sysfs device for TOPOLOGY to work */ | |
484 | if (!sysfs_device) | |
485 | return -ENODEV; | |
486 | ||
487 | list_for_each(tmp, &config_device_list) { | |
488 | dev = list_entry(tmp, struct config_device, node); | |
489 | if (dev->type != TOPOLOGY) | |
490 | continue; | |
491 | ||
19feb351 | 492 | dbg("dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus); |
137af0cc GKH |
493 | if (strcasecmp(dev->bus, sysfs_device->bus) != 0) |
494 | continue; | |
495 | ||
20ff86bd | 496 | found = 0; |
70033702 | 497 | strfieldcpy(path, sysfs_device->path); |
8c51bbfe | 498 | temp = strrchr(path, '/'); |
19feb351 | 499 | dbg("search '%s' in '%s', path='%s'", dev->place, temp, path); |
8c51bbfe GKH |
500 | if (strstr(temp, dev->place) != NULL) { |
501 | found = 1; | |
502 | } else { | |
503 | *temp = 0x00; | |
504 | temp = strrchr(path, '/'); | |
19feb351 | 505 | dbg("search '%s' in '%s', path='%s'", dev->place, temp, path); |
8c51bbfe GKH |
506 | if (strstr(temp, dev->place) != NULL) |
507 | found = 1; | |
508 | } | |
509 | if (!found) | |
510 | continue; | |
511 | ||
70033702 | 512 | strfieldcpy(udev->name, dev->name); |
3d150dfb | 513 | strfieldcpy(udev->symlink, dev->symlink); |
61219c75 GKH |
514 | dbg("found matching place '%s', '%s' becomes '%s'", |
515 | dev->place, class_dev->name, udev->name); | |
8c51bbfe GKH |
516 | return 0; |
517 | } | |
518 | return -ENODEV; | |
519 | } | |
520 | ||
137af0cc | 521 | static int do_replace(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device) |
120d45d0 GKH |
522 | { |
523 | struct config_device *dev; | |
524 | struct list_head *tmp; | |
525 | ||
526 | list_for_each(tmp, &config_device_list) { | |
527 | dev = list_entry(tmp, struct config_device, node); | |
528 | if (dev->type != REPLACE) | |
529 | continue; | |
530 | ||
19feb351 | 531 | dbg("compare name '%s' with '%s'", dev->kernel_name, class_dev->name); |
9f1da361 | 532 | if (strcmp_pattern(dev->kernel_name, class_dev->name) != 0) |
120d45d0 GKH |
533 | continue; |
534 | ||
70033702 | 535 | strfieldcpy(udev->name, dev->name); |
3d150dfb | 536 | strfieldcpy(udev->symlink, dev->symlink); |
61219c75 | 537 | dbg("found name, '%s' becomes '%s'", dev->kernel_name, udev->name); |
120d45d0 GKH |
538 | |
539 | return 0; | |
540 | } | |
541 | return -ENODEV; | |
542 | } | |
543 | ||
09e52d51 KS |
544 | static void do_kernelname(struct sysfs_class_device *class_dev, struct udevice *udev) |
545 | { | |
61219c75 | 546 | /* heh, this is pretty simple... */ |
09e52d51 | 547 | strfieldcpy(udev->name, class_dev->name); |
09e52d51 KS |
548 | } |
549 | ||
5c6f0fb0 | 550 | int namedev_name_device(struct sysfs_class_device *class_dev, struct udevice *udev) |
185a35a4 | 551 | { |
7bd22a78 GKH |
552 | struct sysfs_device *sysfs_device = NULL; |
553 | struct sysfs_class_device *class_dev_parent = NULL; | |
185a35a4 | 554 | int retval = 0; |
7bd22a78 | 555 | char *temp = NULL; |
61219c75 | 556 | struct perm_device *perm; |
185a35a4 | 557 | |
9d496c74 | 558 | udev->mode = 0; |
7bd22a78 GKH |
559 | |
560 | /* find the sysfs_device for this class device */ | |
8a0c11d3 | 561 | /* Wouldn't it really be nice if libsysfs could do this for us? */ |
03e64c8f | 562 | if (class_dev->sysdevice) { |
7bd22a78 GKH |
563 | sysfs_device = class_dev->sysdevice; |
564 | } else { | |
565 | /* bah, let's go backwards up a level to see if the device is there, | |
566 | * as block partitions don't point to the physical device. Need to fix that | |
567 | * up in the kernel... | |
568 | */ | |
569 | if (strstr(class_dev->path, "block")) { | |
19feb351 | 570 | dbg("looking at block device"); |
7bd22a78 GKH |
571 | if (isdigit(class_dev->path[strlen(class_dev->path)-1])) { |
572 | char path[SYSFS_PATH_MAX]; | |
573 | ||
19feb351 | 574 | dbg("really is a partition"); |
70033702 | 575 | strfieldcpy(path, class_dev->path); |
7bd22a78 GKH |
576 | temp = strrchr(path, '/'); |
577 | *temp = 0x00; | |
19feb351 | 578 | dbg("looking for a class device at '%s'", path); |
7bd22a78 GKH |
579 | class_dev_parent = sysfs_open_class_device(path); |
580 | if (class_dev_parent == NULL) { | |
581 | dbg("sysfs_open_class_device at '%s' failed", path); | |
582 | } else { | |
19feb351 | 583 | dbg("class_dev_parent->name='%s'", class_dev_parent->name); |
7bd22a78 GKH |
584 | if (class_dev_parent->sysdevice) |
585 | sysfs_device = class_dev_parent->sysdevice; | |
586 | } | |
587 | } | |
588 | } | |
589 | } | |
28d6536a | 590 | |
7bd22a78 | 591 | if (sysfs_device) { |
19feb351 GKH |
592 | dbg("sysfs_device->path='%s'", sysfs_device->path); |
593 | dbg("sysfs_device->bus_id='%s'", sysfs_device->bus_id); | |
594 | dbg("sysfs_device->bus='%s'", sysfs_device->bus); | |
f3b04a2e | 595 | strfieldcpy(udev->bus_id, sysfs_device->bus_id); |
03e64c8f | 596 | } else { |
19feb351 | 597 | dbg("class_dev->name = '%s'", class_dev->name); |
03e64c8f | 598 | } |
120d45d0 | 599 | |
f3b04a2e GKH |
600 | build_kernel_number(class_dev, udev); |
601 | ||
120d45d0 | 602 | /* rules are looked at in priority order */ |
137af0cc | 603 | retval = do_callout(class_dev, udev, sysfs_device); |
120d45d0 | 604 | if (retval == 0) |
09e52d51 | 605 | goto found; |
120d45d0 | 606 | |
ca1cc0fe GKH |
607 | retval = do_label(class_dev, udev, sysfs_device); |
608 | if (retval == 0) | |
09e52d51 | 609 | goto found; |
ca1cc0fe GKH |
610 | |
611 | retval = do_number(class_dev, udev, sysfs_device); | |
612 | if (retval == 0) | |
09e52d51 | 613 | goto found; |
ca1cc0fe | 614 | |
8c51bbfe GKH |
615 | retval = do_topology(class_dev, udev, sysfs_device); |
616 | if (retval == 0) | |
09e52d51 | 617 | goto found; |
8c51bbfe | 618 | |
137af0cc | 619 | retval = do_replace(class_dev, udev, sysfs_device); |
120d45d0 | 620 | if (retval == 0) |
09e52d51 | 621 | goto found; |
120d45d0 | 622 | |
09e52d51 KS |
623 | do_kernelname(class_dev, udev); |
624 | goto done; | |
c2405f50 | 625 | |
09e52d51 | 626 | found: |
3d150dfb | 627 | /* substitute placeholder */ |
f3b04a2e | 628 | apply_format(udev, udev->name); |
3d150dfb | 629 | apply_format(udev, udev->symlink); |
98b88dbf | 630 | |
09e52d51 | 631 | done: |
61219c75 GKH |
632 | perm = find_perm(udev->name); |
633 | if (perm) { | |
634 | udev->mode = perm->mode; | |
635 | strfieldcpy(udev->owner, perm->owner); | |
636 | strfieldcpy(udev->group, perm->group); | |
637 | } else { | |
638 | /* no matching perms found :( */ | |
9d496c74 GKH |
639 | udev->mode = get_default_mode(class_dev); |
640 | udev->owner[0] = 0x00; | |
641 | udev->group[0] = 0x00; | |
615e05f8 | 642 | } |
61219c75 GKH |
643 | dbg("name, '%s' is going to have owner='%s', group='%s', mode = %#o", |
644 | udev->name, udev->owner, udev->group, udev->mode); | |
7bd22a78 GKH |
645 | |
646 | if (class_dev_parent) | |
647 | sysfs_close_class_device(class_dev_parent); | |
648 | ||
120d45d0 | 649 | return 0; |
185a35a4 GKH |
650 | } |
651 | ||
2232cac8 GKH |
652 | int namedev_init(void) |
653 | { | |
654 | int retval; | |
28d6536a | 655 | |
e8baccca | 656 | retval = namedev_init_rules(); |
2232cac8 GKH |
657 | if (retval) |
658 | return retval; | |
659 | ||
660 | retval = namedev_init_permissions(); | |
661 | if (retval) | |
662 | return retval; | |
663 | ||
19feb351 | 664 | dump_config_dev_list(); |
61219c75 | 665 | dump_perm_dev_list(); |
2232cac8 GKH |
666 | return retval; |
667 | } |