]>
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> |
dac056aa | 33 | #include <sys/stat.h> |
2232cac8 GKH |
34 | |
35 | #include "list.h" | |
36 | #include "udev.h" | |
37 | #include "udev_version.h" | |
38 | #include "namedev.h" | |
185a35a4 | 39 | #include "libsysfs/libsysfs.h" |
2023350e | 40 | #include "klibc_fixups.h" |
2232cac8 | 41 | |
19feb351 | 42 | LIST_HEAD(config_device_list); |
61219c75 | 43 | LIST_HEAD(perm_device_list); |
2232cac8 | 44 | |
9f1da361 KS |
45 | /* compare string with pattern (supports * ? [0-9] [!A-Z]) */ |
46 | static int strcmp_pattern(const char *p, const char *s) | |
c124eafa | 47 | { |
9f1da361 KS |
48 | if (*s == '\0') { |
49 | while (*p == '*') | |
50 | p++; | |
51 | return (*p != '\0'); | |
52 | } | |
53 | switch (*p) { | |
54 | case '[': | |
55 | { | |
56 | int not = 0; | |
57 | p++; | |
58 | if (*p == '!') { | |
59 | not = 1; | |
60 | p++; | |
61 | } | |
62 | while (*p && (*p != ']')) { | |
63 | int match = 0; | |
64 | if (p[1] == '-') { | |
65 | if ((*s >= *p) && (*s <= p[2])) | |
66 | match = 1; | |
67 | p += 3; | |
68 | } else { | |
69 | match = (*p == *s); | |
70 | p++; | |
71 | } | |
72 | if (match ^ not) { | |
73 | while (*p && (*p != ']')) | |
74 | p++; | |
75 | return strcmp_pattern(p+1, s+1); | |
76 | } | |
77 | } | |
78 | } | |
79 | break; | |
80 | case '*': | |
81 | if (strcmp_pattern(p, s+1)) | |
82 | return strcmp_pattern(p+1, s); | |
83 | return 0; | |
84 | case '\0': | |
85 | if (*s == '\0') { | |
86 | return 0; | |
87 | } | |
88 | break; | |
89 | default: | |
90 | if ((*p == *s) || (*p == '?')) | |
91 | return strcmp_pattern(p+1, s+1); | |
92 | break; | |
93 | } | |
94 | return 1; | |
c124eafa KS |
95 | } |
96 | ||
2232cac8 GKH |
97 | #define copy_var(a, b, var) \ |
98 | if (b->var) \ | |
469c7cff | 99 | a->var = b->var; |
2232cac8 GKH |
100 | |
101 | #define copy_string(a, b, var) \ | |
102 | if (strlen(b->var)) \ | |
469c7cff | 103 | strcpy(a->var, b->var); |
2232cac8 | 104 | |
61219c75 GKH |
105 | int add_perm_dev(struct perm_device *new_dev) |
106 | { | |
107 | struct list_head *tmp; | |
108 | struct perm_device *tmp_dev; | |
109 | ||
110 | /* update the values if we already have the device */ | |
111 | list_for_each(tmp, &perm_device_list) { | |
112 | struct perm_device *dev = list_entry(tmp, struct perm_device, node); | |
113 | if (strcmp_pattern(new_dev->name, dev->name)) | |
114 | continue; | |
115 | copy_var(dev, new_dev, mode); | |
116 | copy_string(dev, new_dev, owner); | |
117 | copy_string(dev, new_dev, group); | |
118 | return 0; | |
119 | } | |
120 | ||
121 | /* not found, add new structure to the perm list */ | |
122 | tmp_dev = malloc(sizeof(*tmp_dev)); | |
123 | if (!tmp_dev) | |
124 | return -ENOMEM; | |
125 | memcpy(tmp_dev, new_dev, sizeof(*tmp_dev)); | |
126 | list_add_tail(&tmp_dev->node, &perm_device_list); | |
127 | //dump_perm_dev(tmp_dev); | |
128 | return 0; | |
129 | } | |
130 | ||
131 | static struct perm_device *find_perm(char *name) | |
132 | { | |
133 | struct list_head *tmp; | |
134 | struct perm_device *perm = NULL; | |
135 | ||
136 | list_for_each(tmp, &perm_device_list) { | |
137 | perm = list_entry(tmp, struct perm_device, node); | |
138 | if (strcmp_pattern(perm->name, name)) | |
139 | continue; | |
140 | return perm; | |
141 | } | |
142 | return NULL; | |
143 | } | |
144 | ||
c2405f50 | 145 | static mode_t get_default_mode(struct sysfs_class_device *class_dev) |
185a35a4 | 146 | { |
89571022 GKH |
147 | mode_t mode = 0600; /* default to owner rw only */ |
148 | ||
149 | if (strlen(default_mode_str) != 0) { | |
150 | mode = strtol(default_mode_str, NULL, 8); | |
151 | } | |
152 | return mode; | |
185a35a4 GKH |
153 | } |
154 | ||
f3b04a2e GKH |
155 | static void apply_format(struct udevice *udev, unsigned char *string) |
156 | { | |
157 | char name[NAME_SIZE]; | |
b1c5e333 KS |
158 | char temp[NAME_SIZE]; |
159 | char *tail; | |
f3b04a2e | 160 | char *pos; |
b1c5e333 KS |
161 | char *pos2; |
162 | char *pos3; | |
163 | int num; | |
f3b04a2e GKH |
164 | |
165 | while (1) { | |
b1c5e333 | 166 | num = 0; |
f3b04a2e GKH |
167 | pos = strchr(string, '%'); |
168 | ||
169 | if (pos) { | |
b1c5e333 KS |
170 | *pos = '\0'; |
171 | tail = pos+1; | |
172 | if (isdigit(tail[0])) { | |
173 | num = (int) strtoul(&pos[1], &tail, 10); | |
174 | if (tail == NULL) { | |
175 | dbg("format parsing error '%s'", pos+1); | |
176 | break; | |
177 | } | |
178 | } | |
179 | strfieldcpy(name, tail+1); | |
180 | ||
181 | switch (tail[0]) { | |
f3b04a2e GKH |
182 | case 'b': |
183 | if (strlen(udev->bus_id) == 0) | |
184 | break; | |
6968d494 | 185 | strcat(pos, udev->bus_id); |
f3b04a2e GKH |
186 | dbg("substitute bus_id '%s'", udev->bus_id); |
187 | break; | |
3e540368 KS |
188 | case 'k': |
189 | if (strlen(udev->kernel_name) == 0) | |
190 | break; | |
191 | strcat(pos, udev->kernel_name); | |
192 | dbg("substitute kernel name '%s'", udev->kernel_name); | |
193 | break; | |
f3b04a2e GKH |
194 | case 'n': |
195 | if (strlen(udev->kernel_number) == 0) | |
196 | break; | |
197 | strcat(pos, udev->kernel_number); | |
198 | dbg("substitute kernel number '%s'", udev->kernel_number); | |
199 | break; | |
5c6f0f14 AB |
200 | case 'D': |
201 | if (strlen(udev->kernel_number) == 0) { | |
525d07e7 | 202 | strcat(pos, "disc"); |
5c6f0f14 AB |
203 | break; |
204 | } | |
205 | strcat(pos, "part"); | |
206 | strcat(pos, udev->kernel_number); | |
207 | dbg("substitute kernel number '%s'", udev->kernel_number); | |
208 | break; | |
f3b04a2e GKH |
209 | case 'm': |
210 | sprintf(pos, "%u", udev->minor); | |
211 | dbg("substitute minor number '%u'", udev->minor); | |
212 | break; | |
213 | case 'M': | |
214 | sprintf(pos, "%u", udev->major); | |
215 | dbg("substitute major number '%u'", udev->major); | |
216 | break; | |
217 | case 'c': | |
218 | if (strlen(udev->callout_value) == 0) | |
219 | break; | |
b1c5e333 KS |
220 | if (num) { |
221 | /* get part of return string */ | |
222 | strncpy(temp, udev->callout_value, sizeof(temp)); | |
223 | pos2 = temp; | |
224 | while (num) { | |
225 | num--; | |
226 | pos3 = strsep(&pos2, " "); | |
227 | if (pos3 == NULL) { | |
228 | dbg("requested part of callout string not found"); | |
229 | break; | |
230 | } | |
231 | } | |
232 | strcat(pos, pos3); | |
233 | dbg("substitute partial callout output '%s'", pos3); | |
234 | } else { | |
235 | strcat(pos, udev->callout_value); | |
236 | dbg("substitute callout output '%s'", udev->callout_value); | |
237 | } | |
f3b04a2e GKH |
238 | break; |
239 | default: | |
240 | dbg("unknown substitution type '%%%c'", pos[1]); | |
241 | break; | |
242 | } | |
243 | strcat(string, name); | |
244 | } else | |
245 | break; | |
246 | } | |
247 | } | |
248 | ||
dac056aa GKH |
249 | static struct bus_file { |
250 | char *bus; | |
251 | char *file; | |
252 | } bus_files[] = { | |
253 | { .bus = "scsi", .file = "vendor" }, | |
254 | { .bus = "usb", .file = "idVendor" }, | |
2441c207 | 255 | { .bus = "ide", .file = "detach_state" }, |
48076332 | 256 | { .bus = "pci", .file = "vendor" }, |
dac056aa GKH |
257 | {} |
258 | }; | |
259 | ||
260 | #define SECONDS_TO_WAIT_FOR_FILE 10 | |
261 | static void wait_for_device_to_initialize(struct sysfs_device *sysfs_device) | |
262 | { | |
263 | /* sleep until we see the file for this specific bus type show up this | |
264 | * is needed because we can easily out-run the kernel in looking for | |
265 | * these files before the paticular subsystem has created them in the | |
266 | * sysfs tree properly. | |
267 | * | |
268 | * And people thought that the /sbin/hotplug event system was going to | |
269 | * be slow, poo on you for arguing that before even testing it... | |
270 | */ | |
271 | struct bus_file *b = &bus_files[0]; | |
272 | struct sysfs_attribute *tmpattr; | |
273 | int loop; | |
274 | ||
275 | while (1) { | |
276 | if (b->bus == NULL) | |
277 | break; | |
278 | if (strcmp(sysfs_device->bus, b->bus) == 0) { | |
279 | tmpattr = NULL; | |
280 | loop = SECONDS_TO_WAIT_FOR_FILE; | |
281 | while (loop--) { | |
282 | dbg("looking for file '%s' on bus '%s'", b->file, b->bus); | |
283 | tmpattr = sysfs_get_device_attr(sysfs_device, b->file); | |
284 | if (tmpattr) { | |
285 | /* found it! */ | |
286 | goto exit; | |
287 | } | |
288 | /* sleep to give the kernel a chance to create the file */ | |
289 | sleep(1); | |
290 | } | |
291 | dbg("Timed out waiting for '%s' file, continuing on anyway...", b->file); | |
292 | goto exit; | |
293 | } | |
294 | b++; | |
295 | } | |
296 | dbg("Did not find bus type '%s' on list of bus_id_files, contact greg@kroah.com", sysfs_device->bus); | |
297 | exit: | |
298 | return; /* here to prevent compiler warning... */ | |
299 | } | |
19dc5d4c | 300 | |
c27e6911 PM |
301 | static int exec_callout(struct config_device *dev, char *value, int len) |
302 | { | |
303 | int retval; | |
304 | int res; | |
305 | int status; | |
306 | int fds[2]; | |
307 | pid_t pid; | |
308 | int value_set = 0; | |
309 | char buffer[256]; | |
20524642 | 310 | char *pos; |
bc434511 KS |
311 | char *args[CALLOUT_MAXARG]; |
312 | int i; | |
c27e6911 | 313 | |
8f43a65e | 314 | dbg("callout to '%s'", dev->exec_program); |
c27e6911 PM |
315 | retval = pipe(fds); |
316 | if (retval != 0) { | |
317 | dbg("pipe failed"); | |
318 | return -1; | |
319 | } | |
320 | pid = fork(); | |
321 | if (pid == -1) { | |
322 | dbg("fork failed"); | |
323 | return -1; | |
324 | } | |
325 | ||
326 | if (pid == 0) { | |
8f43a65e | 327 | /* child */ |
c27e6911 PM |
328 | close(STDOUT_FILENO); |
329 | dup(fds[1]); /* dup write side of pipe to STDOUT */ | |
bc434511 KS |
330 | if (strchr(dev->exec_program, ' ')) { |
331 | /* callout with arguments */ | |
20524642 | 332 | pos = dev->exec_program; |
bc434511 | 333 | for (i=0; i < CALLOUT_MAXARG-1; i++) { |
20524642 | 334 | args[i] = strsep(&pos, " "); |
bc434511 KS |
335 | if (args[i] == NULL) |
336 | break; | |
337 | } | |
338 | if (args[i]) { | |
53dc383e | 339 | dbg("too many args - %d", i); |
bc434511 KS |
340 | args[i] = NULL; |
341 | } | |
342 | retval = execve(args[0], args, main_envp); | |
343 | } else { | |
344 | retval = execve(dev->exec_program, main_argv, main_envp); | |
345 | } | |
c27e6911 PM |
346 | if (retval != 0) { |
347 | dbg("child execve failed"); | |
348 | exit(1); | |
349 | } | |
350 | return -1; /* avoid compiler warning */ | |
351 | } else { | |
8f43a65e | 352 | /* parent reads from fds[0] */ |
c27e6911 PM |
353 | close(fds[1]); |
354 | retval = 0; | |
355 | while (1) { | |
356 | res = read(fds[0], buffer, sizeof(buffer) - 1); | |
357 | if (res <= 0) | |
358 | break; | |
359 | buffer[res] = '\0'; | |
360 | if (res > len) { | |
f7b4eca4 | 361 | dbg("callout len %d too short", len); |
c27e6911 PM |
362 | retval = -1; |
363 | } | |
364 | if (value_set) { | |
365 | dbg("callout value already set"); | |
366 | retval = -1; | |
367 | } else { | |
368 | value_set = 1; | |
369 | strncpy(value, buffer, len); | |
20524642 KS |
370 | pos = value + strlen(value)-1; |
371 | if (pos[0] == '\n') | |
372 | pos[0] = '\0'; | |
373 | dbg("callout returned '%s'", value); | |
c27e6911 PM |
374 | } |
375 | } | |
376 | close(fds[0]); | |
377 | res = wait(&status); | |
378 | if (res < 0) { | |
379 | dbg("wait failed result %d", res); | |
380 | retval = -1; | |
381 | } | |
382 | ||
1e959a4b | 383 | #ifndef __KLIBC__ |
c27e6911 PM |
384 | if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) { |
385 | dbg("callout program status 0x%x", status); | |
386 | retval = -1; | |
387 | } | |
1e959a4b | 388 | #endif |
c27e6911 PM |
389 | } |
390 | return retval; | |
391 | } | |
392 | ||
137af0cc | 393 | static int do_callout(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device) |
120d45d0 GKH |
394 | { |
395 | struct config_device *dev; | |
396 | struct list_head *tmp; | |
120d45d0 GKH |
397 | |
398 | list_for_each(tmp, &config_device_list) { | |
399 | dev = list_entry(tmp, struct config_device, node); | |
400 | if (dev->type != CALLOUT) | |
401 | continue; | |
f3b04a2e | 402 | |
1d936fbc GKH |
403 | if (dev->bus[0] != '\0') { |
404 | /* as the user specified a bus, we must match it up */ | |
405 | if (!sysfs_device) | |
406 | continue; | |
19feb351 | 407 | dbg("dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus); |
137af0cc GKH |
408 | if (strcasecmp(dev->bus, sysfs_device->bus) != 0) |
409 | continue; | |
410 | } | |
411 | ||
f3b04a2e GKH |
412 | /* substitute anything that needs to be in the program name */ |
413 | apply_format(udev, dev->exec_program); | |
414 | if (exec_callout(dev, udev->callout_value, NAME_SIZE)) | |
120d45d0 | 415 | continue; |
9f1da361 | 416 | if (strcmp_pattern(dev->id, udev->callout_value) != 0) |
120d45d0 | 417 | continue; |
70033702 | 418 | strfieldcpy(udev->name, dev->name); |
3d150dfb | 419 | strfieldcpy(udev->symlink, dev->symlink); |
61219c75 GKH |
420 | dbg("callout returned matching value '%s', '%s' becomes '%s'", |
421 | dev->id, class_dev->name, udev->name); | |
120d45d0 GKH |
422 | return 0; |
423 | } | |
424 | return -ENODEV; | |
425 | } | |
426 | ||
a8b01705 | 427 | static int match_pair(struct sysfs_class_device *class_dev, struct sysfs_device *sysfs_device, struct sysfs_pair *pair) |
ca1cc0fe GKH |
428 | { |
429 | struct sysfs_attribute *tmpattr = NULL; | |
a8b01705 GKH |
430 | char *c; |
431 | ||
432 | if ((pair == NULL) || (pair->file[0] == '\0') || (pair->value == '\0')) | |
433 | return -ENODEV; | |
434 | ||
435 | dbg("look for device attribute '%s'", pair->file); | |
436 | /* try to find the attribute in the class device directory */ | |
437 | tmpattr = sysfs_get_classdev_attr(class_dev, pair->file); | |
438 | if (tmpattr) | |
439 | goto label_found; | |
440 | ||
441 | /* look in the class device directory if present */ | |
442 | if (sysfs_device) { | |
443 | tmpattr = sysfs_get_device_attr(sysfs_device, pair->file); | |
444 | if (tmpattr) | |
445 | goto label_found; | |
446 | } | |
447 | ||
448 | return -ENODEV; | |
449 | ||
450 | label_found: | |
451 | c = tmpattr->value + strlen(tmpattr->value)-1; | |
452 | if (*c == '\n') | |
453 | *c = 0x00; | |
454 | dbg("compare attribute '%s' value '%s' with '%s'", | |
455 | pair->file, tmpattr->value, pair->value); | |
456 | if (strcmp_pattern(pair->value, tmpattr->value) != 0) | |
457 | return -ENODEV; | |
458 | ||
459 | dbg("found matching attribute '%s' with value '%s'", | |
460 | pair->file, pair->value); | |
461 | return 0; | |
462 | } | |
463 | ||
464 | static int do_label(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device) | |
465 | { | |
466 | struct sysfs_pair *pair; | |
ca1cc0fe GKH |
467 | struct config_device *dev; |
468 | struct list_head *tmp; | |
a8b01705 GKH |
469 | int i; |
470 | int match; | |
ca1cc0fe GKH |
471 | |
472 | list_for_each(tmp, &config_device_list) { | |
473 | dev = list_entry(tmp, struct config_device, node); | |
474 | if (dev->type != LABEL) | |
475 | continue; | |
476 | ||
a7402175 GKH |
477 | if (dev->bus[0] != '\0') { |
478 | /* as the user specified a bus, we must match it up */ | |
479 | if (!sysfs_device) | |
480 | continue; | |
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 | } | |
485 | ||
a8b01705 GKH |
486 | match = 1; |
487 | for (i = 0; i < MAX_SYSFS_PAIRS; ++i) { | |
488 | pair = &dev->sysfs_pair[i]; | |
489 | if ((pair->file[0] == '\0') || (pair->value[0] == '\0')) | |
490 | break; | |
491 | if (match_pair(class_dev, sysfs_device, pair) != 0) { | |
492 | match = 0; | |
493 | break; | |
494 | } | |
ca1cc0fe | 495 | } |
a8b01705 | 496 | if (match == 0) |
ca1cc0fe GKH |
497 | continue; |
498 | ||
a8b01705 | 499 | /* found match */ |
70033702 | 500 | strfieldcpy(udev->name, dev->name); |
3d150dfb | 501 | strfieldcpy(udev->symlink, dev->symlink); |
a8b01705 GKH |
502 | dbg("found matching attribute, '%s' becomes '%s' ", |
503 | class_dev->name, udev->name); | |
ca1cc0fe GKH |
504 | |
505 | return 0; | |
506 | } | |
507 | return -ENODEV; | |
508 | } | |
509 | ||
510 | static int do_number(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device) | |
511 | { | |
512 | struct config_device *dev; | |
513 | struct list_head *tmp; | |
514 | char path[SYSFS_PATH_MAX]; | |
515 | int found; | |
516 | char *temp = NULL; | |
517 | ||
518 | /* we have to have a sysfs device for NUMBER to work */ | |
519 | if (!sysfs_device) | |
520 | return -ENODEV; | |
521 | ||
522 | list_for_each(tmp, &config_device_list) { | |
523 | dev = list_entry(tmp, struct config_device, node); | |
524 | if (dev->type != NUMBER) | |
525 | continue; | |
526 | ||
19feb351 | 527 | dbg("dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus); |
137af0cc GKH |
528 | if (strcasecmp(dev->bus, sysfs_device->bus) != 0) |
529 | continue; | |
530 | ||
ca1cc0fe | 531 | found = 0; |
70033702 | 532 | strfieldcpy(path, sysfs_device->path); |
ca1cc0fe | 533 | temp = strrchr(path, '/'); |
19feb351 | 534 | dbg("search '%s' in '%s', path='%s'", dev->id, temp, path); |
ca1cc0fe GKH |
535 | if (strstr(temp, dev->id) != NULL) { |
536 | found = 1; | |
537 | } else { | |
538 | *temp = 0x00; | |
539 | temp = strrchr(path, '/'); | |
19feb351 | 540 | dbg("search '%s' in '%s', path='%s'", dev->id, temp, path); |
ca1cc0fe GKH |
541 | if (strstr(temp, dev->id) != NULL) |
542 | found = 1; | |
543 | } | |
544 | if (!found) | |
545 | continue; | |
70033702 | 546 | strfieldcpy(udev->name, dev->name); |
3d150dfb | 547 | strfieldcpy(udev->symlink, dev->symlink); |
61219c75 GKH |
548 | dbg("found matching id '%s', '%s' becomes '%s'", |
549 | dev->id, class_dev->name, udev->name); | |
ca1cc0fe GKH |
550 | return 0; |
551 | } | |
552 | return -ENODEV; | |
553 | } | |
554 | ||
8c51bbfe GKH |
555 | static int do_topology(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device) |
556 | { | |
557 | struct config_device *dev; | |
558 | struct list_head *tmp; | |
559 | char path[SYSFS_PATH_MAX]; | |
560 | int found; | |
561 | char *temp = NULL; | |
562 | ||
563 | /* we have to have a sysfs device for TOPOLOGY to work */ | |
564 | if (!sysfs_device) | |
565 | return -ENODEV; | |
566 | ||
567 | list_for_each(tmp, &config_device_list) { | |
568 | dev = list_entry(tmp, struct config_device, node); | |
569 | if (dev->type != TOPOLOGY) | |
570 | continue; | |
571 | ||
19feb351 | 572 | dbg("dev->bus='%s' sysfs_device->bus='%s'", dev->bus, sysfs_device->bus); |
137af0cc GKH |
573 | if (strcasecmp(dev->bus, sysfs_device->bus) != 0) |
574 | continue; | |
575 | ||
20ff86bd | 576 | found = 0; |
70033702 | 577 | strfieldcpy(path, sysfs_device->path); |
8c51bbfe | 578 | temp = strrchr(path, '/'); |
19feb351 | 579 | dbg("search '%s' in '%s', path='%s'", dev->place, temp, path); |
8c51bbfe GKH |
580 | if (strstr(temp, dev->place) != NULL) { |
581 | found = 1; | |
582 | } else { | |
583 | *temp = 0x00; | |
584 | temp = strrchr(path, '/'); | |
19feb351 | 585 | dbg("search '%s' in '%s', path='%s'", dev->place, temp, path); |
8c51bbfe GKH |
586 | if (strstr(temp, dev->place) != NULL) |
587 | found = 1; | |
588 | } | |
589 | if (!found) | |
590 | continue; | |
591 | ||
70033702 | 592 | strfieldcpy(udev->name, dev->name); |
3d150dfb | 593 | strfieldcpy(udev->symlink, dev->symlink); |
61219c75 GKH |
594 | dbg("found matching place '%s', '%s' becomes '%s'", |
595 | dev->place, class_dev->name, udev->name); | |
8c51bbfe GKH |
596 | return 0; |
597 | } | |
598 | return -ENODEV; | |
599 | } | |
600 | ||
137af0cc | 601 | static int do_replace(struct sysfs_class_device *class_dev, struct udevice *udev, struct sysfs_device *sysfs_device) |
120d45d0 GKH |
602 | { |
603 | struct config_device *dev; | |
604 | struct list_head *tmp; | |
605 | ||
606 | list_for_each(tmp, &config_device_list) { | |
607 | dev = list_entry(tmp, struct config_device, node); | |
608 | if (dev->type != REPLACE) | |
609 | continue; | |
610 | ||
19feb351 | 611 | dbg("compare name '%s' with '%s'", dev->kernel_name, class_dev->name); |
9f1da361 | 612 | if (strcmp_pattern(dev->kernel_name, class_dev->name) != 0) |
120d45d0 GKH |
613 | continue; |
614 | ||
70033702 | 615 | strfieldcpy(udev->name, dev->name); |
3d150dfb | 616 | strfieldcpy(udev->symlink, dev->symlink); |
61219c75 | 617 | dbg("found name, '%s' becomes '%s'", dev->kernel_name, udev->name); |
120d45d0 GKH |
618 | |
619 | return 0; | |
620 | } | |
621 | return -ENODEV; | |
622 | } | |
623 | ||
09e52d51 KS |
624 | static void do_kernelname(struct sysfs_class_device *class_dev, struct udevice *udev) |
625 | { | |
61219c75 | 626 | /* heh, this is pretty simple... */ |
09e52d51 | 627 | strfieldcpy(udev->name, class_dev->name); |
09e52d51 KS |
628 | } |
629 | ||
dac056aa | 630 | static struct sysfs_device *get_sysfs_device(struct sysfs_class_device *class_dev) |
185a35a4 | 631 | { |
dac056aa GKH |
632 | struct sysfs_device *sysfs_device; |
633 | struct sysfs_class_device *class_dev_parent; | |
634 | int loop; | |
07562d6e GKH |
635 | char filename[SYSFS_PATH_MAX + 6]; |
636 | int retval; | |
637 | char *temp; | |
638 | int partition = 0; | |
639 | ||
640 | /* Figure out where the device symlink is at. For char devices this will | |
641 | * always be in the class_dev->path. But for block devices, it's different. | |
642 | * The main block device will have the device symlink in it's path, but | |
643 | * all partitions have the symlink in its parent directory. | |
644 | * But we need to watch out for block devices that do not have parents, yet | |
645 | * look like a partition (fd0, loop0, etc.) They all do not have a device | |
646 | * symlink yet. We do sit and spin on waiting for them right now, we should | |
647 | * possibly have a whitelist for these devices here... | |
dac056aa | 648 | */ |
07562d6e GKH |
649 | strcpy(filename, class_dev->path); |
650 | dbg("filename = %s", filename); | |
651 | if (strcmp(class_dev->classname, SYSFS_BLOCK_NAME) == 0) { | |
652 | if (isdigit(class_dev->path[strlen(class_dev->path)-1])) { | |
653 | temp = strrchr(filename, '/'); | |
654 | if (temp) { | |
655 | partition = 1; | |
656 | *temp = 0x00; | |
657 | char *temp2 = strrchr(filename, '/'); | |
658 | dbg("temp2 = %s", temp2); | |
659 | if (temp2 && (strcmp(temp2, "/block") == 0)) { | |
660 | /* oops, we have no parent block device, so go back to original directory */ | |
661 | strcpy(filename, class_dev->path); | |
662 | partition = 0; | |
663 | } | |
664 | } | |
665 | } | |
666 | } | |
667 | strcat(filename, "/device"); | |
1edbb8d3 | 668 | |
07562d6e | 669 | loop = 2; |
dac056aa GKH |
670 | while (loop--) { |
671 | struct stat buf; | |
dac056aa GKH |
672 | dbg("looking for '%s'", filename); |
673 | retval = stat(filename, &buf); | |
674 | if (!retval) | |
675 | break; | |
dac056aa GKH |
676 | /* sleep to give the kernel a chance to create the device file */ |
677 | sleep(1); | |
678 | } | |
07562d6e | 679 | |
dac056aa GKH |
680 | loop = 1; /* FIXME put a real value in here for when everything is fixed... */ |
681 | while (loop--) { | |
682 | /* find the sysfs_device for this class device */ | |
683 | /* Wouldn't it really be nice if libsysfs could do this for us? */ | |
684 | sysfs_device = sysfs_get_classdev_device(class_dev); | |
685 | if (sysfs_device != NULL) | |
686 | goto exit; | |
7bd22a78 | 687 | |
07562d6e GKH |
688 | /* if it's a partition, we need to get the parent device */ |
689 | if (partition) { | |
690 | /* FIXME HACK HACK HACK HACK | |
691 | * for some reason partitions need this extra sleep here, in order | |
692 | * to wait for the device properly. Once the libsysfs code is | |
693 | * fixed properly, this sleep should go away, and we can just loop above. | |
694 | */ | |
695 | sleep(1); | |
696 | dbg("really is a partition"); | |
697 | class_dev_parent = sysfs_get_classdev_parent(class_dev); | |
698 | if (class_dev_parent == NULL) { | |
699 | dbg("sysfs_get_classdev_parent for class device '%s' failed", class_dev->name); | |
700 | } else { | |
701 | dbg("class_dev_parent->name='%s'", class_dev_parent->name); | |
702 | sysfs_device = sysfs_get_classdev_device(class_dev_parent); | |
703 | if (sysfs_device != NULL) | |
704 | goto exit; | |
7bd22a78 GKH |
705 | } |
706 | } | |
dac056aa | 707 | /* sleep to give the kernel a chance to create the link */ |
07562d6e GKH |
708 | /* FIXME remove comment... |
709 | sleep(1); */ | |
7bd22a78 | 710 | } |
1edbb8d3 | 711 | dbg("Timed out waiting for device symlink, continuing on anyway..."); |
dac056aa GKH |
712 | exit: |
713 | return sysfs_device; | |
714 | } | |
715 | ||
716 | int namedev_name_device(struct sysfs_class_device *class_dev, struct udevice *udev) | |
717 | { | |
718 | struct sysfs_device *sysfs_device = NULL; | |
dac056aa GKH |
719 | int retval = 0; |
720 | struct perm_device *perm; | |
3e540368 | 721 | char *pos; |
dac056aa GKH |
722 | |
723 | udev->mode = 0; | |
28d6536a | 724 | |
dac056aa GKH |
725 | /* find the sysfs_device associated with this class device */ |
726 | sysfs_device = get_sysfs_device(class_dev); | |
7bd22a78 | 727 | if (sysfs_device) { |
19feb351 GKH |
728 | dbg("sysfs_device->path='%s'", sysfs_device->path); |
729 | dbg("sysfs_device->bus_id='%s'", sysfs_device->bus_id); | |
730 | dbg("sysfs_device->bus='%s'", sysfs_device->bus); | |
f3b04a2e | 731 | strfieldcpy(udev->bus_id, sysfs_device->bus_id); |
dac056aa | 732 | wait_for_device_to_initialize(sysfs_device); |
03e64c8f | 733 | } else { |
19feb351 | 734 | dbg("class_dev->name = '%s'", class_dev->name); |
03e64c8f | 735 | } |
120d45d0 | 736 | |
3e540368 KS |
737 | strfieldcpy(udev->kernel_name, class_dev->name); |
738 | ||
739 | /* get kernel number */ | |
740 | pos = class_dev->name + strlen(class_dev->name); | |
741 | while (isdigit(*(pos-1))) | |
742 | pos--; | |
743 | strfieldcpy(udev->kernel_number, pos); | |
744 | dbg("kernel_number='%s'", udev->kernel_number); | |
f3b04a2e | 745 | |
120d45d0 | 746 | /* rules are looked at in priority order */ |
137af0cc | 747 | retval = do_callout(class_dev, udev, sysfs_device); |
120d45d0 | 748 | if (retval == 0) |
09e52d51 | 749 | goto found; |
120d45d0 | 750 | |
ca1cc0fe GKH |
751 | retval = do_label(class_dev, udev, sysfs_device); |
752 | if (retval == 0) | |
09e52d51 | 753 | goto found; |
ca1cc0fe GKH |
754 | |
755 | retval = do_number(class_dev, udev, sysfs_device); | |
756 | if (retval == 0) | |
09e52d51 | 757 | goto found; |
ca1cc0fe | 758 | |
8c51bbfe GKH |
759 | retval = do_topology(class_dev, udev, sysfs_device); |
760 | if (retval == 0) | |
09e52d51 | 761 | goto found; |
8c51bbfe | 762 | |
137af0cc | 763 | retval = do_replace(class_dev, udev, sysfs_device); |
120d45d0 | 764 | if (retval == 0) |
09e52d51 | 765 | goto found; |
120d45d0 | 766 | |
09e52d51 KS |
767 | do_kernelname(class_dev, udev); |
768 | goto done; | |
c2405f50 | 769 | |
09e52d51 | 770 | found: |
3d150dfb | 771 | /* substitute placeholder */ |
f3b04a2e | 772 | apply_format(udev, udev->name); |
3d150dfb | 773 | apply_format(udev, udev->symlink); |
98b88dbf | 774 | |
09e52d51 | 775 | done: |
61219c75 GKH |
776 | perm = find_perm(udev->name); |
777 | if (perm) { | |
778 | udev->mode = perm->mode; | |
779 | strfieldcpy(udev->owner, perm->owner); | |
780 | strfieldcpy(udev->group, perm->group); | |
781 | } else { | |
782 | /* no matching perms found :( */ | |
9d496c74 GKH |
783 | udev->mode = get_default_mode(class_dev); |
784 | udev->owner[0] = 0x00; | |
785 | udev->group[0] = 0x00; | |
615e05f8 | 786 | } |
61219c75 GKH |
787 | dbg("name, '%s' is going to have owner='%s', group='%s', mode = %#o", |
788 | udev->name, udev->owner, udev->group, udev->mode); | |
7bd22a78 | 789 | |
120d45d0 | 790 | return 0; |
185a35a4 GKH |
791 | } |
792 | ||
2232cac8 GKH |
793 | int namedev_init(void) |
794 | { | |
795 | int retval; | |
28d6536a | 796 | |
e8baccca | 797 | retval = namedev_init_rules(); |
2232cac8 GKH |
798 | if (retval) |
799 | return retval; | |
800 | ||
801 | retval = namedev_init_permissions(); | |
802 | if (retval) | |
803 | return retval; | |
804 | ||
19feb351 | 805 | dump_config_dev_list(); |
61219c75 | 806 | dump_perm_dev_list(); |
2232cac8 GKH |
807 | return retval; |
808 | } |