]>
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]; | |
b1c5e333 KS |
203 | char temp[NAME_SIZE]; |
204 | char *tail; | |
f3b04a2e | 205 | char *pos; |
b1c5e333 KS |
206 | char *pos2; |
207 | char *pos3; | |
208 | int num; | |
f3b04a2e GKH |
209 | |
210 | while (1) { | |
b1c5e333 | 211 | num = 0; |
f3b04a2e GKH |
212 | pos = strchr(string, '%'); |
213 | ||
214 | if (pos) { | |
b1c5e333 KS |
215 | *pos = '\0'; |
216 | tail = pos+1; | |
217 | if (isdigit(tail[0])) { | |
218 | num = (int) strtoul(&pos[1], &tail, 10); | |
219 | if (tail == NULL) { | |
220 | dbg("format parsing error '%s'", pos+1); | |
221 | break; | |
222 | } | |
223 | } | |
224 | strfieldcpy(name, tail+1); | |
225 | ||
226 | switch (tail[0]) { | |
f3b04a2e GKH |
227 | case 'b': |
228 | if (strlen(udev->bus_id) == 0) | |
229 | break; | |
6968d494 | 230 | strcat(pos, udev->bus_id); |
f3b04a2e GKH |
231 | dbg("substitute bus_id '%s'", udev->bus_id); |
232 | break; | |
233 | case 'n': | |
234 | if (strlen(udev->kernel_number) == 0) | |
235 | break; | |
236 | strcat(pos, udev->kernel_number); | |
237 | dbg("substitute kernel number '%s'", udev->kernel_number); | |
238 | break; | |
5c6f0f14 AB |
239 | case 'D': |
240 | if (strlen(udev->kernel_number) == 0) { | |
525d07e7 | 241 | strcat(pos, "disc"); |
5c6f0f14 AB |
242 | break; |
243 | } | |
244 | strcat(pos, "part"); | |
245 | strcat(pos, udev->kernel_number); | |
246 | dbg("substitute kernel number '%s'", udev->kernel_number); | |
247 | break; | |
f3b04a2e GKH |
248 | case 'm': |
249 | sprintf(pos, "%u", udev->minor); | |
250 | dbg("substitute minor number '%u'", udev->minor); | |
251 | break; | |
252 | case 'M': | |
253 | sprintf(pos, "%u", udev->major); | |
254 | dbg("substitute major number '%u'", udev->major); | |
255 | break; | |
256 | case 'c': | |
257 | if (strlen(udev->callout_value) == 0) | |
258 | break; | |
b1c5e333 KS |
259 | if (num) { |
260 | /* get part of return string */ | |
261 | strncpy(temp, udev->callout_value, sizeof(temp)); | |
262 | pos2 = temp; | |
263 | while (num) { | |
264 | num--; | |
265 | pos3 = strsep(&pos2, " "); | |
266 | if (pos3 == NULL) { | |
267 | dbg("requested part of callout string not found"); | |
268 | break; | |
269 | } | |
270 | } | |
271 | strcat(pos, pos3); | |
272 | dbg("substitute partial callout output '%s'", pos3); | |
273 | } else { | |
274 | strcat(pos, udev->callout_value); | |
275 | dbg("substitute callout output '%s'", udev->callout_value); | |
276 | } | |
f3b04a2e GKH |
277 | break; |
278 | default: | |
279 | dbg("unknown substitution type '%%%c'", pos[1]); | |
280 | break; | |
281 | } | |
282 | strcat(string, name); | |
283 | } else | |
284 | break; | |
285 | } | |
286 | } | |
287 | ||
19dc5d4c | 288 | |
c27e6911 PM |
289 | static int exec_callout(struct config_device *dev, char *value, int len) |
290 | { | |
291 | int retval; | |
292 | int res; | |
293 | int status; | |
294 | int fds[2]; | |
295 | pid_t pid; | |
296 | int value_set = 0; | |
297 | char buffer[256]; | |
20524642 | 298 | char *pos; |
bc434511 KS |
299 | char *args[CALLOUT_MAXARG]; |
300 | int i; | |
c27e6911 | 301 | |
8f43a65e | 302 | dbg("callout to '%s'", dev->exec_program); |
c27e6911 PM |
303 | retval = pipe(fds); |
304 | if (retval != 0) { | |
305 | dbg("pipe failed"); | |
306 | return -1; | |
307 | } | |
308 | pid = fork(); | |
309 | if (pid == -1) { | |
310 | dbg("fork failed"); | |
311 | return -1; | |
312 | } | |
313 | ||
314 | if (pid == 0) { | |
8f43a65e | 315 | /* child */ |
c27e6911 PM |
316 | close(STDOUT_FILENO); |
317 | dup(fds[1]); /* dup write side of pipe to STDOUT */ | |
bc434511 KS |
318 | if (strchr(dev->exec_program, ' ')) { |
319 | /* callout with arguments */ | |
20524642 | 320 | pos = dev->exec_program; |
bc434511 | 321 | for (i=0; i < CALLOUT_MAXARG-1; i++) { |
20524642 | 322 | args[i] = strsep(&pos, " "); |
bc434511 KS |
323 | if (args[i] == NULL) |
324 | break; | |
325 | } | |
326 | if (args[i]) { | |
53dc383e | 327 | dbg("too many args - %d", i); |
bc434511 KS |
328 | args[i] = NULL; |
329 | } | |
330 | retval = execve(args[0], args, main_envp); | |
331 | } else { | |
332 | retval = execve(dev->exec_program, main_argv, main_envp); | |
333 | } | |
c27e6911 PM |
334 | if (retval != 0) { |
335 | dbg("child execve failed"); | |
336 | exit(1); | |
337 | } | |
338 | return -1; /* avoid compiler warning */ | |
339 | } else { | |
8f43a65e | 340 | /* parent reads from fds[0] */ |
c27e6911 PM |
341 | close(fds[1]); |
342 | retval = 0; | |
343 | while (1) { | |
344 | res = read(fds[0], buffer, sizeof(buffer) - 1); | |
345 | if (res <= 0) | |
346 | break; | |
347 | buffer[res] = '\0'; | |
348 | if (res > len) { | |
f7b4eca4 | 349 | dbg("callout len %d too short", len); |
c27e6911 PM |
350 | retval = -1; |
351 | } | |
352 | if (value_set) { | |
353 | dbg("callout value already set"); | |
354 | retval = -1; | |
355 | } else { | |
356 | value_set = 1; | |
357 | strncpy(value, buffer, len); | |
20524642 KS |
358 | pos = value + strlen(value)-1; |
359 | if (pos[0] == '\n') | |
360 | pos[0] = '\0'; | |
361 | dbg("callout returned '%s'", value); | |
c27e6911 PM |
362 | } |
363 | } | |
364 | close(fds[0]); | |
365 | res = wait(&status); | |
366 | if (res < 0) { | |
367 | dbg("wait failed result %d", res); | |
368 | retval = -1; | |
369 | } | |
370 | ||
1e959a4b | 371 | #ifndef __KLIBC__ |
c27e6911 PM |
372 | if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) { |
373 | dbg("callout program status 0x%x", status); | |
374 | retval = -1; | |
375 | } | |
1e959a4b | 376 | #endif |
c27e6911 PM |
377 | } |
378 | return retval; | |
379 | } | |
380 | ||
137af0cc | 381 | static int do_callout(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device) |
120d45d0 GKH |
382 | { |
383 | struct config_device *dev; | |
384 | struct list_head *tmp; | |
120d45d0 GKH |
385 | |
386 | list_for_each(tmp, &config_device_list) { | |
387 | dev = list_entry(tmp, struct config_device, node); | |
388 | if (dev->type != CALLOUT) | |
389 | continue; | |
f3b04a2e | 390 | |
137af0cc | 391 | if (sysfs_device) { |
19feb351 | 392 | dbg("dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus); |
137af0cc GKH |
393 | if (strcasecmp(dev->bus, sysfs_device->bus) != 0) |
394 | continue; | |
395 | } | |
396 | ||
f3b04a2e GKH |
397 | /* substitute anything that needs to be in the program name */ |
398 | apply_format(udev, dev->exec_program); | |
399 | if (exec_callout(dev, udev->callout_value, NAME_SIZE)) | |
120d45d0 | 400 | continue; |
9f1da361 | 401 | if (strcmp_pattern(dev->id, udev->callout_value) != 0) |
120d45d0 | 402 | continue; |
70033702 | 403 | strfieldcpy(udev->name, dev->name); |
3d150dfb | 404 | strfieldcpy(udev->symlink, dev->symlink); |
61219c75 GKH |
405 | dbg("callout returned matching value '%s', '%s' becomes '%s'", |
406 | dev->id, class_dev->name, udev->name); | |
120d45d0 GKH |
407 | return 0; |
408 | } | |
409 | return -ENODEV; | |
410 | } | |
411 | ||
ca1cc0fe GKH |
412 | static int do_label(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device) |
413 | { | |
414 | struct sysfs_attribute *tmpattr = NULL; | |
415 | struct config_device *dev; | |
416 | struct list_head *tmp; | |
28d6536a | 417 | char *c; |
ca1cc0fe GKH |
418 | |
419 | list_for_each(tmp, &config_device_list) { | |
420 | dev = list_entry(tmp, struct config_device, node); | |
421 | if (dev->type != LABEL) | |
422 | continue; | |
423 | ||
137af0cc | 424 | if (sysfs_device) { |
19feb351 | 425 | dbg("dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus); |
137af0cc GKH |
426 | if (strcasecmp(dev->bus, sysfs_device->bus) != 0) |
427 | continue; | |
428 | } | |
429 | ||
19feb351 | 430 | dbg("look for device attribute '%s'", dev->sysfs_file); |
ca1cc0fe GKH |
431 | /* try to find the attribute in the class device directory */ |
432 | tmpattr = sysfs_get_classdev_attr(class_dev, dev->sysfs_file); | |
433 | if (tmpattr) | |
434 | goto label_found; | |
435 | ||
436 | /* look in the class device directory if present */ | |
437 | if (sysfs_device) { | |
438 | tmpattr = sysfs_get_device_attr(sysfs_device, dev->sysfs_file); | |
439 | if (tmpattr) | |
440 | goto label_found; | |
441 | } | |
442 | ||
443 | continue; | |
444 | ||
445 | label_found: | |
28d6536a KS |
446 | c = tmpattr->value + strlen(tmpattr->value)-1; |
447 | if (*c == '\n') | |
448 | *c = 0x00; | |
19feb351 | 449 | dbg("compare attribute '%s' value '%s' with '%s'", |
8f43a65e | 450 | dev->sysfs_file, tmpattr->value, dev->sysfs_value); |
83be97ba | 451 | if (strcmp_pattern(dev->sysfs_value, tmpattr->value) != 0) |
ca1cc0fe GKH |
452 | continue; |
453 | ||
70033702 | 454 | strfieldcpy(udev->name, dev->name); |
3d150dfb | 455 | strfieldcpy(udev->symlink, dev->symlink); |
61219c75 GKH |
456 | dbg("found matching attribute '%s', '%s' becomes '%s' ", |
457 | dev->sysfs_file, class_dev->name, udev->name); | |
ca1cc0fe GKH |
458 | |
459 | return 0; | |
460 | } | |
461 | return -ENODEV; | |
462 | } | |
463 | ||
464 | static int do_number(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device) | |
465 | { | |
466 | struct config_device *dev; | |
467 | struct list_head *tmp; | |
468 | char path[SYSFS_PATH_MAX]; | |
469 | int found; | |
470 | char *temp = NULL; | |
471 | ||
472 | /* we have to have a sysfs device for NUMBER to work */ | |
473 | if (!sysfs_device) | |
474 | return -ENODEV; | |
475 | ||
476 | list_for_each(tmp, &config_device_list) { | |
477 | dev = list_entry(tmp, struct config_device, node); | |
478 | if (dev->type != NUMBER) | |
479 | continue; | |
480 | ||
19feb351 | 481 | dbg("dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus); |
137af0cc GKH |
482 | if (strcasecmp(dev->bus, sysfs_device->bus) != 0) |
483 | continue; | |
484 | ||
ca1cc0fe | 485 | found = 0; |
70033702 | 486 | strfieldcpy(path, sysfs_device->path); |
ca1cc0fe | 487 | temp = strrchr(path, '/'); |
19feb351 | 488 | dbg("search '%s' in '%s', path='%s'", dev->id, temp, path); |
ca1cc0fe GKH |
489 | if (strstr(temp, dev->id) != NULL) { |
490 | found = 1; | |
491 | } else { | |
492 | *temp = 0x00; | |
493 | temp = strrchr(path, '/'); | |
19feb351 | 494 | dbg("search '%s' in '%s', path='%s'", dev->id, temp, path); |
ca1cc0fe GKH |
495 | if (strstr(temp, dev->id) != NULL) |
496 | found = 1; | |
497 | } | |
498 | if (!found) | |
499 | continue; | |
70033702 | 500 | strfieldcpy(udev->name, dev->name); |
3d150dfb | 501 | strfieldcpy(udev->symlink, dev->symlink); |
61219c75 GKH |
502 | dbg("found matching id '%s', '%s' becomes '%s'", |
503 | dev->id, class_dev->name, udev->name); | |
ca1cc0fe GKH |
504 | return 0; |
505 | } | |
506 | return -ENODEV; | |
507 | } | |
508 | ||
8c51bbfe GKH |
509 | static int do_topology(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device) |
510 | { | |
511 | struct config_device *dev; | |
512 | struct list_head *tmp; | |
513 | char path[SYSFS_PATH_MAX]; | |
514 | int found; | |
515 | char *temp = NULL; | |
516 | ||
517 | /* we have to have a sysfs device for TOPOLOGY to work */ | |
518 | if (!sysfs_device) | |
519 | return -ENODEV; | |
520 | ||
521 | list_for_each(tmp, &config_device_list) { | |
522 | dev = list_entry(tmp, struct config_device, node); | |
523 | if (dev->type != TOPOLOGY) | |
524 | continue; | |
525 | ||
19feb351 | 526 | dbg("dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus); |
137af0cc GKH |
527 | if (strcasecmp(dev->bus, sysfs_device->bus) != 0) |
528 | continue; | |
529 | ||
20ff86bd | 530 | found = 0; |
70033702 | 531 | strfieldcpy(path, sysfs_device->path); |
8c51bbfe | 532 | temp = strrchr(path, '/'); |
19feb351 | 533 | dbg("search '%s' in '%s', path='%s'", dev->place, temp, path); |
8c51bbfe GKH |
534 | if (strstr(temp, dev->place) != NULL) { |
535 | found = 1; | |
536 | } else { | |
537 | *temp = 0x00; | |
538 | temp = strrchr(path, '/'); | |
19feb351 | 539 | dbg("search '%s' in '%s', path='%s'", dev->place, temp, path); |
8c51bbfe GKH |
540 | if (strstr(temp, dev->place) != NULL) |
541 | found = 1; | |
542 | } | |
543 | if (!found) | |
544 | continue; | |
545 | ||
70033702 | 546 | strfieldcpy(udev->name, dev->name); |
3d150dfb | 547 | strfieldcpy(udev->symlink, dev->symlink); |
61219c75 GKH |
548 | dbg("found matching place '%s', '%s' becomes '%s'", |
549 | dev->place, class_dev->name, udev->name); | |
8c51bbfe GKH |
550 | return 0; |
551 | } | |
552 | return -ENODEV; | |
553 | } | |
554 | ||
137af0cc | 555 | static int do_replace(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device) |
120d45d0 GKH |
556 | { |
557 | struct config_device *dev; | |
558 | struct list_head *tmp; | |
559 | ||
560 | list_for_each(tmp, &config_device_list) { | |
561 | dev = list_entry(tmp, struct config_device, node); | |
562 | if (dev->type != REPLACE) | |
563 | continue; | |
564 | ||
19feb351 | 565 | dbg("compare name '%s' with '%s'", dev->kernel_name, class_dev->name); |
9f1da361 | 566 | if (strcmp_pattern(dev->kernel_name, class_dev->name) != 0) |
120d45d0 GKH |
567 | continue; |
568 | ||
70033702 | 569 | strfieldcpy(udev->name, dev->name); |
3d150dfb | 570 | strfieldcpy(udev->symlink, dev->symlink); |
61219c75 | 571 | dbg("found name, '%s' becomes '%s'", dev->kernel_name, udev->name); |
120d45d0 GKH |
572 | |
573 | return 0; | |
574 | } | |
575 | return -ENODEV; | |
576 | } | |
577 | ||
09e52d51 KS |
578 | static void do_kernelname(struct sysfs_class_device *class_dev, struct udevice *udev) |
579 | { | |
61219c75 | 580 | /* heh, this is pretty simple... */ |
09e52d51 | 581 | strfieldcpy(udev->name, class_dev->name); |
09e52d51 KS |
582 | } |
583 | ||
5c6f0fb0 | 584 | int namedev_name_device(struct sysfs_class_device *class_dev, struct udevice *udev) |
185a35a4 | 585 | { |
7bd22a78 GKH |
586 | struct sysfs_device *sysfs_device = NULL; |
587 | struct sysfs_class_device *class_dev_parent = NULL; | |
185a35a4 | 588 | int retval = 0; |
61219c75 | 589 | struct perm_device *perm; |
185a35a4 | 590 | |
9d496c74 | 591 | udev->mode = 0; |
7bd22a78 GKH |
592 | |
593 | /* find the sysfs_device for this class device */ | |
8a0c11d3 | 594 | /* Wouldn't it really be nice if libsysfs could do this for us? */ |
5d4754f1 DS |
595 | sysfs_device = sysfs_get_classdev_device(class_dev); |
596 | if (sysfs_device == NULL) { | |
7bd22a78 GKH |
597 | /* bah, let's go backwards up a level to see if the device is there, |
598 | * as block partitions don't point to the physical device. Need to fix that | |
599 | * up in the kernel... | |
600 | */ | |
5d4754f1 | 601 | if (strcmp(class_dev->classname, SYSFS_BLOCK_NAME) == 0) { |
19feb351 | 602 | dbg("looking at block device"); |
7bd22a78 | 603 | if (isdigit(class_dev->path[strlen(class_dev->path)-1])) { |
19feb351 | 604 | dbg("really is a partition"); |
5d4754f1 DS |
605 | class_dev_parent = sysfs_get_classdev_parent |
606 | (class_dev); | |
7bd22a78 | 607 | if (class_dev_parent == NULL) { |
5d4754f1 | 608 | dbg("sysfs_get_classdev_parent for class device '%s' failed", class_dev->name); |
7bd22a78 | 609 | } else { |
19feb351 | 610 | dbg("class_dev_parent->name='%s'", class_dev_parent->name); |
5d4754f1 | 611 | sysfs_device = sysfs_get_classdev_device(class_dev_parent); |
7bd22a78 GKH |
612 | } |
613 | } | |
614 | } | |
615 | } | |
28d6536a | 616 | |
7bd22a78 | 617 | if (sysfs_device) { |
19feb351 GKH |
618 | dbg("sysfs_device->path='%s'", sysfs_device->path); |
619 | dbg("sysfs_device->bus_id='%s'", sysfs_device->bus_id); | |
620 | dbg("sysfs_device->bus='%s'", sysfs_device->bus); | |
f3b04a2e | 621 | strfieldcpy(udev->bus_id, sysfs_device->bus_id); |
03e64c8f | 622 | } else { |
19feb351 | 623 | dbg("class_dev->name = '%s'", class_dev->name); |
03e64c8f | 624 | } |
120d45d0 | 625 | |
f3b04a2e GKH |
626 | build_kernel_number(class_dev, udev); |
627 | ||
120d45d0 | 628 | /* rules are looked at in priority order */ |
137af0cc | 629 | retval = do_callout(class_dev, udev, sysfs_device); |
120d45d0 | 630 | if (retval == 0) |
09e52d51 | 631 | goto found; |
120d45d0 | 632 | |
ca1cc0fe GKH |
633 | retval = do_label(class_dev, udev, sysfs_device); |
634 | if (retval == 0) | |
09e52d51 | 635 | goto found; |
ca1cc0fe GKH |
636 | |
637 | retval = do_number(class_dev, udev, sysfs_device); | |
638 | if (retval == 0) | |
09e52d51 | 639 | goto found; |
ca1cc0fe | 640 | |
8c51bbfe GKH |
641 | retval = do_topology(class_dev, udev, sysfs_device); |
642 | if (retval == 0) | |
09e52d51 | 643 | goto found; |
8c51bbfe | 644 | |
137af0cc | 645 | retval = do_replace(class_dev, udev, sysfs_device); |
120d45d0 | 646 | if (retval == 0) |
09e52d51 | 647 | goto found; |
120d45d0 | 648 | |
09e52d51 KS |
649 | do_kernelname(class_dev, udev); |
650 | goto done; | |
c2405f50 | 651 | |
09e52d51 | 652 | found: |
3d150dfb | 653 | /* substitute placeholder */ |
f3b04a2e | 654 | apply_format(udev, udev->name); |
3d150dfb | 655 | apply_format(udev, udev->symlink); |
98b88dbf | 656 | |
09e52d51 | 657 | done: |
61219c75 GKH |
658 | perm = find_perm(udev->name); |
659 | if (perm) { | |
660 | udev->mode = perm->mode; | |
661 | strfieldcpy(udev->owner, perm->owner); | |
662 | strfieldcpy(udev->group, perm->group); | |
663 | } else { | |
664 | /* no matching perms found :( */ | |
9d496c74 GKH |
665 | udev->mode = get_default_mode(class_dev); |
666 | udev->owner[0] = 0x00; | |
667 | udev->group[0] = 0x00; | |
615e05f8 | 668 | } |
61219c75 GKH |
669 | dbg("name, '%s' is going to have owner='%s', group='%s', mode = %#o", |
670 | udev->name, udev->owner, udev->group, udev->mode); | |
7bd22a78 | 671 | |
120d45d0 | 672 | return 0; |
185a35a4 GKH |
673 | } |
674 | ||
2232cac8 GKH |
675 | int namedev_init(void) |
676 | { | |
677 | int retval; | |
28d6536a | 678 | |
e8baccca | 679 | retval = namedev_init_rules(); |
2232cac8 GKH |
680 | if (retval) |
681 | return retval; | |
682 | ||
683 | retval = namedev_init_permissions(); | |
684 | if (retval) | |
685 | return retval; | |
686 | ||
19feb351 | 687 | dump_config_dev_list(); |
61219c75 | 688 | dump_perm_dev_list(); |
2232cac8 GKH |
689 | return retval; |
690 | } |