rc: USB LED support
[tomato.git] / release / src / router / rc / usb.c
blobcc9ded34b5e9508fe9fdd7a655ce38b55ffbb72c
1 /*
3 USB Support
5 */
6 #include "rc.h"
8 #include <sys/types.h>
9 #include <unistd.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <arpa/inet.h>
13 #include <time.h>
14 #include <sys/time.h>
15 #include <errno.h>
17 #include <sys/mount.h>
18 #include <mntent.h>
19 #include <dirent.h>
20 #include <sys/file.h>
21 #include <sys/swap.h>
23 /* Adjust bdflush parameters.
24 * Do this here, because Tomato doesn't have the sysctl command.
25 * With these values, a disk block should be written to disk within 2 seconds.
27 #ifdef LINUX26
28 void tune_bdflush(void)
30 f_write_string("/proc/sys/vm/dirty_expire_centisecs", "200", 0, 0);
31 f_write_string("/proc/sys/vm/dirty_writeback_centisecs", "200", 0, 0);
33 #else
34 #include <sys/kdaemon.h>
35 #define SET_PARM(n) (n * 2 | 1)
36 void tune_bdflush(void)
38 bdflush(SET_PARM(5), 100);
39 bdflush(SET_PARM(6), 100);
40 bdflush(SET_PARM(8), 0);
42 #endif // LINUX26
44 #define USBCORE_MOD "usbcore"
45 #define USB20_MOD "ehci-hcd"
46 #define USBSTORAGE_MOD "usb-storage"
47 #define SCSI_MOD "scsi_mod"
48 #define SD_MOD "sd_mod"
49 #ifdef LINUX26
50 #define USBOHCI_MOD "ohci-hcd"
51 #define USBUHCI_MOD "uhci-hcd"
52 #define USBPRINTER_MOD "usblp"
53 #define SCSI_WAIT_MOD "scsi_wait_scan"
54 #define USBFS "usbfs"
55 #else
56 #define USBOHCI_MOD "usb-ohci"
57 #define USBUHCI_MOD "usb-uhci"
58 #define USBPRINTER_MOD "printer"
59 #define USBFS "usbdevfs"
60 #endif
62 static int p9100d_sig(int sig)
64 const char p910pid[] = "/var/run/p9100d.pid";
65 char s[32];
66 int pid;
68 if (f_read_string(p910pid, s, sizeof(s)) > 0) {
69 if ((pid = atoi(s)) > 1) {
70 if (kill(pid, sig) == 0) {
71 if (sig == SIGTERM) {
72 sleep(1);
73 unlink(p910pid);
75 return 0;
79 return -1;
82 void start_usb(void)
84 char param[32];
85 int i;
87 _dprintf("%s\n", __FUNCTION__);
88 tune_bdflush();
90 if (nvram_get_int("usb_enable")) {
91 modprobe(USBCORE_MOD);
93 /* mount usb device filesystem */
94 mount(USBFS, "/proc/bus/usb", USBFS, MS_MGC_VAL, NULL);
96 #ifdef LINUX26
97 i = do_led(LED_USB, LED_PROBE);
98 if (i != 255) {
99 modprobe("ledtrig-usbdev");
100 modprobe("leds-usb");
101 sprintf(param, "%d", i);
102 f_write_string("/sys/class/leds/usb-led/gpio_pin", param, 0, 0);
103 f_write_string("/sys/class/leds/usb-led/device_name", "1-1", 0, 0);
105 #endif
106 if (nvram_get_int("usb_storage")) {
107 /* insert scsi and storage modules before usb drivers */
108 modprobe(SCSI_MOD);
109 #ifdef LINUX26
110 modprobe(SCSI_WAIT_MOD);
111 #endif
112 modprobe(SD_MOD);
113 modprobe(USBSTORAGE_MOD);
115 if (nvram_get_int("usb_fs_ext3")) {
116 #ifdef LINUX26
117 modprobe("mbcache"); // used by ext2/ext3
118 #endif
119 /* insert ext3 first so that lazy mount tries ext3 before ext2 */
120 modprobe("jbd");
121 modprobe("ext3");
122 modprobe("ext2");
125 if (nvram_get_int("usb_fs_fat")) {
126 modprobe("fat");
127 modprobe("vfat");
131 /* if enabled, force USB2 before USB1.1 */
132 if (nvram_get_int("usb_usb2") == 1) {
133 i = nvram_get_int("usb_irq_thresh");
134 if ((i < 0) || (i > 6))
135 i = 0;
136 sprintf(param, "log2_irq_thresh=%d", i);
137 modprobe(USB20_MOD, param);
140 if (nvram_get_int("usb_uhci") == 1) {
141 modprobe(USBUHCI_MOD);
144 if (nvram_get_int("usb_ohci") == 1) {
145 modprobe(USBOHCI_MOD);
148 if (nvram_get_int("usb_printer")) {
149 symlink("/dev/usb", "/dev/printers");
150 modprobe(USBPRINTER_MOD);
152 /* start printer server only if not already running */
153 if (p9100d_sig(0) != 0) {
154 eval("p910nd",
155 nvram_get_int("usb_printer_bidirect") ? "-b" : "", //bidirectional
156 "-f", "/dev/usb/lp0", // device
157 "0" // listen port
164 void stop_usb(void)
166 // only find and kill the printer server we started (port 0)
167 p9100d_sig(SIGTERM);
168 modprobe_r(USBPRINTER_MOD);
170 // only stop storage services if disabled
171 if (!nvram_get_int("usb_enable") || !nvram_get_int("usb_storage")) {
172 // Unmount all partitions
173 remove_storage_main(0);
175 // Stop storage services
176 modprobe_r("ext2");
177 modprobe_r("ext3");
178 modprobe_r("jbd");
179 #ifdef LINUX26
180 modprobe_r("mbcache");
181 #endif
182 modprobe_r("vfat");
183 modprobe_r("fat");
184 modprobe_r("fuse");
185 sleep(1);
186 #ifdef TCONFIG_SAMBASRV
187 modprobe_r("nls_cp437");
188 modprobe_r("nls_cp850");
189 modprobe_r("nls_cp852");
190 modprobe_r("nls_cp866");
191 #ifdef LINUX26
192 modprobe_r("nls_cp932");
193 modprobe_r("nls_cp936");
194 modprobe_r("nls_cp949");
195 modprobe_r("nls_cp950");
196 #endif
197 #endif
198 modprobe_r(USBSTORAGE_MOD);
199 modprobe_r(SD_MOD);
200 #ifdef LINUX26
201 modprobe_r(SCSI_WAIT_MOD);
202 #endif
203 modprobe_r(SCSI_MOD);
206 if (nvram_get_int("usb_ohci") != 1) modprobe_r(USBOHCI_MOD);
207 if (nvram_get_int("usb_uhci") != 1) modprobe_r(USBUHCI_MOD);
208 if (nvram_get_int("usb_usb2") != 1) modprobe_r(USB20_MOD);
210 #ifdef LINUX26
211 modprobe_r("leds-usb");
212 modprobe_r("ledtrig-usbdev");
213 led(LED_USB, LED_OFF);
214 #endif
216 // only unload core modules if usb is disabled
217 if (!nvram_get_int("usb_enable")) {
218 umount("/proc/bus/usb"); // unmount usb device filesystem
219 modprobe_r(USBOHCI_MOD);
220 modprobe_r(USBUHCI_MOD);
221 modprobe_r(USB20_MOD);
222 modprobe_r(USBCORE_MOD);
227 #define MOUNT_VAL_FAIL 0
228 #define MOUNT_VAL_RONLY 1
229 #define MOUNT_VAL_RW 2
230 #define MOUNT_VAL_EXIST 3
232 int mount_r(char *mnt_dev, char *mnt_dir, char *type)
234 struct mntent *mnt;
235 int ret;
236 char options[140];
237 char flagfn[128];
238 int dir_made;
240 if ((mnt = findmntents(mnt_dev, 0, NULL, 0))) {
241 syslog(LOG_INFO, "USB partition at %s already mounted on %s",
242 mnt_dev, mnt->mnt_dir);
243 return MOUNT_VAL_EXIST;
246 options[0] = 0;
248 if (type) {
249 unsigned long flags = MS_NOATIME | MS_NODEV;
251 if (strcmp(type, "swap") == 0 || strcmp(type, "mbr") == 0) {
252 /* not a mountable partition */
253 flags = 0;
255 else if (strcmp(type, "ext2") == 0 || strcmp(type, "ext3") == 0) {
256 if (nvram_invmatch("usb_ext_opt", ""))
257 sprintf(options, nvram_safe_get("usb_ext_opt"));
259 else if (strcmp(type, "vfat") == 0) {
260 if (nvram_invmatch("smbd_cset", ""))
261 sprintf(options, "iocharset=%s%s",
262 isdigit(nvram_get("smbd_cset")[0]) ? "cp" : "",
263 nvram_get("smbd_cset"));
264 if (nvram_invmatch("smbd_cpage", "")) {
265 char *cp = nvram_safe_get("smbd_cpage");
266 sprintf(options + strlen(options), ",codepage=%s" + (options[0] ? 0 : 1), cp);
267 sprintf(flagfn, "nls_cp%s", cp);
269 cp = nvram_get("smbd_nlsmod");
270 if ((cp) && (*cp != 0) && (strcmp(cp, flagfn) != 0))
271 modprobe_r(cp);
273 modprobe(flagfn);
274 nvram_set("smbd_nlsmod", flagfn);
276 sprintf(options + strlen(options), ",shortname=winnt" + (options[0] ? 0 : 1));
277 #ifdef LINUX26
278 sprintf(options + strlen(options), ",flush" + (options[0] ? 0 : 1));
279 #endif
280 if (nvram_invmatch("usb_fat_opt", ""))
281 sprintf(options + strlen(options), "%s%s", options[0] ? "," : "", nvram_safe_get("usb_fat_opt"));
283 else if (strncmp(type, "ntfs", 4) == 0) {
284 if (nvram_invmatch("smbd_cset", ""))
285 sprintf(options, "iocharset=%s%s",
286 isdigit(nvram_get("smbd_cset")[0]) ? "cp" : "",
287 nvram_get("smbd_cset"));
288 if (nvram_invmatch("usb_ntfs_opt", ""))
289 sprintf(options + strlen(options), "%s%s", options[0] ? "," : "", nvram_safe_get("usb_ntfs_opt"));
292 if (flags) {
293 if ((dir_made = mkdir_if_none(mnt_dir))) {
294 /* Create the flag file for remove the directory on dismount. */
295 sprintf(flagfn, "%s/.autocreated-dir", mnt_dir);
296 f_write(flagfn, NULL, 0, 0, 0);
299 ret = mount(mnt_dev, mnt_dir, type, flags, options[0] ? options : "");
301 /* try ntfs-3g in case it's installed */
302 if (ret != 0 && strncmp(type, "ntfs", 4) == 0) {
303 sprintf(options + strlen(options), ",noatime,nodev" + (options[0] ? 0 : 1));
304 #ifdef TCONFIG_NTFS
305 if (nvram_get_int("usb_fs_ntfs"))
306 #endif
307 ret = eval("ntfs-3g", "-o", options, mnt_dev, mnt_dir);
309 if (ret != 0) /* give it another try - guess fs */
310 ret = eval("mount", "-o", "noatime,nodev", mnt_dev, mnt_dir);
312 if (ret == 0) {
313 syslog(LOG_INFO, "USB %s%s fs at %s mounted on %s",
314 type, (flags & MS_RDONLY) ? " (ro)" : "", mnt_dev, mnt_dir);
315 return (flags & MS_RDONLY) ? MOUNT_VAL_RONLY : MOUNT_VAL_RW;
318 if (dir_made) {
319 unlink(flagfn);
320 rmdir(mnt_dir);
324 return MOUNT_VAL_FAIL;
328 struct mntent *mount_fstab(char *dev_name, char *type, char *label, char *uuid)
330 struct mntent *mnt = NULL;
331 #if 0
332 if (eval("mount", "-a") == 0)
333 mnt = findmntents(dev_name, 0, NULL, 0);
334 #else
335 char spec[PATH_MAX+1];
337 if (label && *label) {
338 sprintf(spec, "LABEL=%s", label);
339 if (eval("mount", spec) == 0)
340 mnt = findmntents(dev_name, 0, NULL, 0);
343 if (!mnt && uuid && *uuid) {
344 sprintf(spec, "UUID=%s", uuid);
345 if (eval("mount", spec) == 0)
346 mnt = findmntents(dev_name, 0, NULL, 0);
349 if (!mnt) {
350 if (eval("mount", dev_name) == 0)
351 mnt = findmntents(dev_name, 0, NULL, 0);
354 if (!mnt) {
355 /* Still did not find what we are looking for, try absolute path */
356 if (realpath(dev_name, spec)) {
357 if (eval("mount", spec) == 0)
358 mnt = findmntents(dev_name, 0, NULL, 0);
361 #endif
363 if (mnt)
364 syslog(LOG_INFO, "USB %s fs at %s mounted on %s", type, dev_name, mnt->mnt_dir);
365 return (mnt);
369 /* Check if the UFD is still connected because the links created in /dev/discs
370 * are not removed when the UFD is unplugged.
371 * The file to read is: /proc/scsi/usb-storage-#/#, where # is the host no.
372 * We are looking for "Attached: Yes".
374 static int usb_ufd_connected(int host_no)
376 char proc_file[128];
377 #ifndef LINUX26
378 char line[256];
379 #endif
380 FILE *fp;
382 sprintf(proc_file, "%s/%s-%d/%d", PROC_SCSI_ROOT, USB_STORAGE, host_no, host_no);
383 fp = fopen(proc_file, "r");
385 if (!fp) {
386 /* try the way it's implemented in newer kernels: /proc/scsi/usb-storage/[host] */
387 sprintf(proc_file, "%s/%s/%d", PROC_SCSI_ROOT, USB_STORAGE, host_no);
388 fp = fopen(proc_file, "r");
391 if (fp) {
392 #ifdef LINUX26
393 fclose(fp);
394 return 1;
395 #else
396 while (fgets(line, sizeof(line), fp) != NULL) {
397 if (strstr(line, "Attached: Yes")) {
398 fclose(fp);
399 return 1;
402 fclose(fp);
403 #endif
406 return 0;
410 #ifndef MNT_DETACH
411 #define MNT_DETACH 0x00000002 /* from linux/fs.h - just detach from the tree */
412 #endif
413 int umount_mountpoint(struct mntent *mnt, uint flags);
414 int uswap_mountpoint(struct mntent *mnt, uint flags);
416 /* Unmount this partition from all its mountpoints. Note that it may
417 * actually be mounted several times, either with different names or
418 * with "-o bind" flag.
419 * If the special flagfile is now revealed, delete it and [attempt to] delete
420 * the directory.
422 int umount_partition(char *dev_name, int host_num, char *dsc_name, char *pt_name, uint flags)
424 sync(); /* This won't matter if the device is unplugged, though. */
426 if (flags & EFH_HUNKNOWN) {
427 /* EFH_HUNKNOWN flag is passed if the host was unknown.
428 * Only unmount disconnected drives in this case.
430 if (usb_ufd_connected(host_num))
431 return 0;
434 /* Find all the active swaps that are on this device and stop them. */
435 findmntents(dev_name, 1, uswap_mountpoint, flags);
437 /* Find all the mountpoints that are for this device and unmount them. */
438 findmntents(dev_name, 0, umount_mountpoint, flags);
439 return 0;
442 int uswap_mountpoint(struct mntent *mnt, uint flags)
444 swapoff(mnt->mnt_fsname);
445 return 0;
448 int umount_mountpoint(struct mntent *mnt, uint flags)
450 int ret = 1, count;
451 char flagfn[128];
453 sprintf(flagfn, "%s/.autocreated-dir", mnt->mnt_dir);
455 /* Run user pre-unmount scripts if any. It might be too late if
456 * the drive has been disconnected, but we'll try it anyway.
458 if (nvram_get_int("usb_automount"))
459 run_nvscript("script_usbumount", mnt->mnt_dir, 3);
460 if (flags & EFH_USER) {
461 /* Unmount from Web. Run *.autostop scripts if any. */
462 run_userfile(mnt->mnt_dir, ".autostop", mnt->mnt_dir, 5);
464 run_nvscript("script_autostop", mnt->mnt_dir, 5);
466 count = 0;
467 while ((ret = umount(mnt->mnt_dir)) && (count < 2)) {
468 count++;
469 /* If we could not unmount the drive on the 1st try,
470 * kill all NAS applications so they are not keeping the device busy -
471 * unless it's an unmount request from the Web GUI.
473 if ((count == 1) && ((flags & EFH_USER) == 0))
474 restart_nas_services(1, 0);
475 sleep(1);
478 if (ret == 0)
479 syslog(LOG_INFO, "USB partition unmounted from %s", mnt->mnt_dir);
481 if (ret && ((flags & EFH_SHUTDN) != 0)) {
482 /* If system is stopping (not restarting), and we couldn't unmount the
483 * partition, try to remount it as read-only. Ignore the return code -
484 * we can still try to do a lazy unmount.
486 eval("mount", "-o", "remount,ro", mnt->mnt_dir);
489 if (ret && ((flags & EFH_USER) == 0)) {
490 /* Make one more try to do a lazy unmount unless it's an unmount
491 * request from the Web GUI.
492 * MNT_DETACH will expose the underlying mountpoint directory to all
493 * except whatever has cd'ed to the mountpoint (thereby making it busy).
494 * So the unmount can't actually fail. It disappears from the ken of
495 * everyone else immediately, and from the ken of whomever is keeping it
496 * busy when they move away from it. And then it disappears for real.
498 ret = umount2(mnt->mnt_dir, MNT_DETACH);
499 syslog(LOG_INFO, "USB partition busy - will unmount ASAP from %s", mnt->mnt_dir);
502 if (ret == 0) {
503 if ((unlink(flagfn) == 0)) {
504 // Only delete the directory if it was auto-created
505 rmdir(mnt->mnt_dir);
508 return (ret == 0);
512 /* Mount this partition on this disc.
513 * If the device is already mounted on any mountpoint, don't mount it again.
514 * If this is a swap partition, try swapon -a.
515 * If this is a regular partition, try mount -a.
517 * Before we mount any partitions:
518 * If the type is swap and /etc/fstab exists, do "swapon -a"
519 * If /etc/fstab exists, try mounting using fstab.
520 * We delay invoking mount because mount will probe all the partitions
521 * to read the labels, and we don't want it to do that early on.
522 * We don't invoke swapon until we actually find a swap partition.
524 * If the mount succeeds, execute the *.autorun scripts in the top
525 * directory of the newly mounted partition.
526 * Returns NZ for success, 0 if we did not mount anything.
528 int mount_partition(char *dev_name, int host_num, char *dsc_name, char *pt_name, uint flags)
530 char the_label[128], mountpoint[128], uuid[40];
531 int ret;
532 char *type, *p;
533 static char *swp_argv[] = { "swapon", "-a", NULL };
534 struct mntent *mnt;
536 if ((type = find_label_or_uuid(dev_name, the_label, uuid)) == NULL)
537 return 0;
539 if (f_exists("/etc/fstab")) {
540 if (strcmp(type, "swap") == 0) {
541 _eval(swp_argv, NULL, 0, NULL);
542 return 0;
545 if (mount_r(dev_name, NULL, NULL) == MOUNT_VAL_EXIST)
546 return 0;
548 if ((mnt = mount_fstab(dev_name, type, the_label, uuid))) {
549 strcpy(mountpoint, mnt->mnt_dir);
550 ret = MOUNT_VAL_RW;
551 goto done;
555 if (*the_label != 0) {
556 for (p = the_label; *p; p++) {
557 if (!isalnum(*p) && !strchr("+-&.@", *p))
558 *p = '_';
560 sprintf(mountpoint, "%s/%s", MOUNT_ROOT, the_label);
561 if ((ret = mount_r(dev_name, mountpoint, type)))
562 goto done;
565 /* Can't mount to /mnt/LABEL, so try mounting to /mnt/discDN_PN */
566 sprintf(mountpoint, "%s/%s", MOUNT_ROOT, pt_name);
567 ret = mount_r(dev_name, mountpoint, type);
568 done:
569 if (ret == MOUNT_VAL_RONLY || ret == MOUNT_VAL_RW)
571 /* Run user *.autorun and post-mount scripts if any. */
572 run_userfile(mountpoint, ".autorun", mountpoint, 3);
573 if (nvram_get_int("usb_automount"))
574 run_nvscript("script_usbmount", mountpoint, 3);
576 return (ret == MOUNT_VAL_RONLY || ret == MOUNT_VAL_RW);
580 #if 0 /* LINUX26 */
583 * Finds SCSI Host number. Returns the host number >=0 if found, or (-1) otherwise.
584 * The name and host number of scsi block device in kernel 2.6 (for attached devices) can be found as
585 * /sys($DEVPATH)/host<host_no>/target<*>/<id>/block:[sda|sdb|...]
586 * where $DEVPATH is passed to hotplug events, and looks like
587 * /devices/pci0000:00/0000:00:04.1/usb1/1-1/1-1:1.2
589 * For printers this function finds a minor assigned to a printer
590 * /sys($DEVPATH)/usb:lp[0|1|2|...]
592 int find_dev_host(const char *devpath)
594 DIR *usb_devpath;
595 struct dirent *dp;
596 char buf[256];
597 int host = -1; /* Scsi Host */
599 sprintf(buf, "/sys%s", devpath);
600 if ((usb_devpath = opendir(buf))) {
601 while ((dp = readdir(usb_devpath))) {
602 errno = 0;
603 if (strncmp(dp->d_name, "host", 4) == 0) {
604 host = strtol(dp->d_name + 4, (char **)NULL, 10);
605 if (errno)
606 host = -1;
607 else
608 break;
610 else if (strncmp(dp->d_name, "usb:lp", 6) == 0) {
611 host = strtol(dp->d_name + 6, (char **)NULL, 10);
612 if (errno)
613 host = -1;
614 else
615 break;
617 else
618 continue;
620 closedir(usb_devpath);
622 return (host);
625 #endif /* LINUX26 */
628 /* Mount or unmount all partitions on this controller.
629 * Parameter: action_add:
630 * 0 = unmount
631 * >0 = mount only if automount config option is enabled.
632 * <0 = mount regardless of config option.
634 void hotplug_usb_storage_device(int host_no, int action_add, uint flags)
636 if (!nvram_get_int("usb_enable"))
637 return;
638 _dprintf("%s: host %d action: %d\n", __FUNCTION__, host_no, action_add);
640 if (action_add) {
641 if (nvram_get_int("usb_storage") && (nvram_get_int("usb_automount") || action_add < 0)) {
642 /* Do not probe the device here. It's either initiated by user,
643 * or hotplug_usb() already did.
645 if (exec_for_host(host_no, 0x00, flags, mount_partition)) {
646 restart_nas_services(0, 1); // restart all NAS applications
650 else {
651 if (nvram_get_int("usb_storage") || ((flags & EFH_USER) == 0)) {
652 /* When unplugged, unmount the device even if
653 * usb storage is disabled in the GUI.
655 exec_for_host(host_no, (flags & EFH_USER) ? 0x00 : 0x02, flags, umount_partition);
656 /* Restart NAS applications (they could be killed by umount_mountpoint),
657 * or just re-read the configuration.
659 restart_nas_services(0, 1);
665 /* This gets called at reboot or upgrade. The system is stopping. */
666 void remove_storage_main(int shutdn)
668 if (shutdn)
669 restart_nas_services(1, 0);
670 /* Unmount all partitions */
671 exec_for_host(-1, 0x02, shutdn ? EFH_SHUTDN : 0, umount_partition);
675 /*******
676 * All the complex locking & checking code was removed when the kernel USB-storage
677 * bugs were fixed.
678 * The crash bug was with overlapped I/O to different USB drives, not specifically
679 * with mount processing.
681 * And for USB devices that are slow to come up. The kernel now waits until the
682 * USB drive has settled, and it correctly reads the partition table before calling
683 * the hotplug agent.
685 * The kernel patch was cleaning up data structures on an unplug. It
686 * needs to wait until the disk is unmounted. We have 20 seconds to do
687 * the unmounts.
688 *******/
691 /* Plugging or removing usb device
693 * On an occurrance, multiple hotplug events may be fired off.
694 * For example, if a hub is plugged or unplugged, an event
695 * will be generated for everything downstream of it, plus one for
696 * the hub itself. These are fired off simultaneously, not serially.
697 * This means that many many hotplug processes will be running at
698 * the same time.
700 * The hotplug event generated by the kernel gives us several pieces
701 * of information:
702 * PRODUCT is vendorid/productid/rev#.
703 * DEVICE is /proc/bus/usb/bus#/dev#
704 * ACTION is add or remove
705 * SCSI_HOST is the host (controller) number (this relies on the custom kernel patch)
707 * Note that when we get a hotplug add event, the USB susbsystem may
708 * or may not have yet tried to read the partition table of the
709 * device. For a new controller that has never been seen before,
710 * generally yes. For a re-plug of a controller that has been seen
711 * before, generally no.
713 * On a remove, the partition info has not yet been expunged. The
714 * partitions show up as /dev/discs/disc#/part#, and /proc/partitions.
715 * It appears that doing a "stat" for a non-existant partition will
716 * causes the kernel to re-validate the device and update the
717 * partition table info. However, it won't re-validate if the disc is
718 * mounted--you'll get a "Device busy for revalidation (usage=%d)" in
719 * syslog.
721 * The $INTERFACE is "class/subclass/protocol"
722 * Some interesting classes:
723 * 8 = mass storage
724 * 7 = printer
725 * 3 = HID. 3/1/2 = mouse.
726 * 6 = still image (6/1/1 = Digital camera Camera)
727 * 9 = Hub
728 * 255 = scanner (255/255/255)
730 * Observed:
731 * Hub seems to have no INTERFACE (null), and TYPE of "9/0/0"
732 * Flash disk seems to have INTERFACE of "8/6/80", and TYPE of "0/0/0"
734 * When a hub is unplugged, a hotplug event is generated for it and everything
735 * downstream from it. You cannot depend on getting these events in any
736 * particular order, since there will be many hotplug programs all fired off
737 * at almost the same time.
738 * On a remove, don't try to access the downstream devices right away, give the
739 * kernel time to finish cleaning up all the data structures, which will be
740 * in the process of being torn down.
742 * On the initial plugin, the first time the kernel usb-storage subsystem sees
743 * the host (identified by GUID), it automatically reads the partition table.
744 * On subsequent plugins, it does not.
746 * Special values for Web Administration to unmount or remount
747 * all partitions of the host:
748 * INTERFACE=TOMATO/...
749 * ACTION=add/remove
750 * SCSI_HOST=<host_no>
751 * If host_no is negative, we unmount all partions of *all* hosts.
753 void hotplug_usb(void)
755 int add;
756 int host = -1;
757 char *interface = getenv("INTERFACE");
758 char *action = getenv("ACTION");
759 char *product = getenv("PRODUCT");
760 #ifdef LINUX26
761 char *device = getenv("DEVICENAME");
762 int is_block = strcmp(getenv("SUBSYSTEM") ? : "", "block") == 0;
763 #else
764 char *device = getenv("DEVICE");
765 #endif
766 char *scsi_host = getenv("SCSI_HOST");
768 _dprintf("%s hotplug INTERFACE=%s ACTION=%s PRODUCT=%s HOST=%s DEVICE=%s\n",
769 getenv("SUBSYSTEM") ? : "USB", interface, action, product, scsi_host, device);
771 if (!nvram_get_int("usb_enable")) return;
772 #ifdef LINUX26
773 if (!action || ((!interface || !product) && !is_block))
774 #else
775 if (!interface || !action || !product) /* Hubs bail out here. */
776 #endif
777 return;
779 if (scsi_host)
780 host = atoi(scsi_host);
782 if (!wait_action_idle(10)) return;
784 add = (strcmp(action, "add") == 0);
785 if (add && (strncmp(interface ? : "", "TOMATO/", 7) != 0)) {
786 #ifdef LINUX26
787 if (!is_block && device)
788 #endif
789 syslog(LOG_DEBUG, "Attached USB device %s [INTERFACE=%s PRODUCT=%s]",
790 device, interface, product);
791 #ifndef LINUX26
792 /* To allow automount to be blocked on startup.
793 * In kernel 2.6 we still need to serialize mount/umount calls -
794 * so the lock is down below in the "block" hotplug processing.
796 file_unlock(file_lock("usb"));
797 #endif
800 if (strncmp(interface ? : "", "TOMATO/", 7) == 0) { /* web admin */
801 if (scsi_host == NULL)
802 host = atoi(product); // for backward compatibility
803 /* If host is negative, unmount all partitions of *all* hosts.
804 * This feature can be used in custom scripts as following:
806 * # INTERFACE=TOMATO/1 ACTION=remove PRODUCT=-1 SCSI_HOST=-1 hotplug usb
808 * PRODUCT is required to pass the env variables verification.
810 /* Unmount or remount all partitions of the host. */
811 hotplug_usb_storage_device(host, add ? -1 : 0, EFH_USER);
813 #ifdef LINUX26
814 else if (is_block && strcmp(getenv("MAJOR") ? : "", "8") == 0 && strcmp(getenv("PHYSDEVBUS") ? : "", "scsi") == 0) {
815 /* scsi partition */
816 char devname[64];
817 int lock;
819 sprintf(devname, "/dev/%s", device);
820 lock = file_lock("usb");
821 if (add) {
822 if (nvram_get_int("usb_storage") && nvram_get_int("usb_automount")) {
823 int minor = atoi(getenv("MINOR") ? : "0");
824 if ((minor % 16) == 0 && !is_no_partition(device)) {
825 /* This is a disc, and not a "no-partition" device,
826 * like APPLE iPOD shuffle. We can't mount it.
828 return;
830 if (mount_partition(devname, host, NULL, device, EFH_HP_ADD)) {
831 restart_nas_services(0, 1); // restart all NAS applications
835 else {
836 /* When unplugged, unmount the device even if usb storage is disabled in the GUI */
837 umount_partition(devname, host, NULL, device, EFH_HP_REMOVE);
838 /* Restart NAS applications (they could be killed by umount_mountpoint),
839 * or just re-read the configuration.
841 restart_nas_services(0, 1);
843 file_unlock(lock);
845 #endif
846 else if (strncmp(interface ? : "", "8/", 2) == 0) { /* usb storage */
847 run_nvscript("script_usbhotplug", NULL, 2);
848 #ifndef LINUX26
849 hotplug_usb_storage_device(host, add, (add ? EFH_HP_ADD : EFH_HP_REMOVE) | (host < 0 ? EFH_HUNKNOWN : 0));
850 #endif
852 else { /* It's some other type of USB device, not storage. */
853 #ifdef LINUX26
854 if (is_block) return;
855 #endif
856 /* Do nothing. The user's hotplug script must do it all. */
857 run_nvscript("script_usbhotplug", NULL, 2);