Revert "Added extrarules to iptables to get UDP request."
[tomato.git] / release / src / router / rc / usb.c
blob59216b1bc1261acea70033e28843563c56645e4b
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("/proc/leds-usb/gpio_pin", param, 0, 0);
104 #endif
105 if (nvram_get_int("usb_storage")) {
106 /* insert scsi and storage modules before usb drivers */
107 modprobe(SCSI_MOD);
108 #ifdef LINUX26
109 modprobe(SCSI_WAIT_MOD);
110 #endif
111 modprobe(SD_MOD);
112 modprobe(USBSTORAGE_MOD);
114 if (nvram_get_int("usb_fs_ext3")) {
115 #ifdef LINUX26
116 modprobe("mbcache"); // used by ext2/ext3
117 #endif
118 /* insert ext3 first so that lazy mount tries ext3 before ext2 */
119 modprobe("jbd");
120 modprobe("ext3");
121 modprobe("ext2");
124 if (nvram_get_int("usb_fs_fat")) {
125 modprobe("fat");
126 modprobe("vfat");
129 if (nvram_get_int("usb_fs_hfs")) {
130 modprobe("hfs");
133 if (nvram_get_int("usb_fs_hfsplus")) {
134 modprobe("hfsplus");
137 #if defined(LINUX26) && defined(TCONFIG_USB_EXTRAS)
138 if (nvram_get_int("usb_mmc") == 1) {
139 /* insert SD/MMC modules if present */
140 modprobe("mmc_core");
141 modprobe("mmc_block");
142 modprobe("sdhci");
144 #endif
147 /* if enabled, force USB2 before USB1.1 */
148 if (nvram_get_int("usb_usb2") == 1) {
149 i = nvram_get_int("usb_irq_thresh");
150 if ((i < 0) || (i > 6))
151 i = 0;
152 sprintf(param, "log2_irq_thresh=%d", i);
153 modprobe(USB20_MOD, param);
156 if (nvram_get_int("usb_uhci") == 1) {
157 modprobe(USBUHCI_MOD);
160 if (nvram_get_int("usb_ohci") == 1) {
161 modprobe(USBOHCI_MOD);
164 if (nvram_get_int("usb_printer")) {
165 symlink("/dev/usb", "/dev/printers");
166 modprobe(USBPRINTER_MOD);
168 /* start printer server only if not already running */
169 if (p9100d_sig(0) != 0) {
170 eval("p910nd",
171 nvram_get_int("usb_printer_bidirect") ? "-b" : "", //bidirectional
172 "-f", "/dev/usb/lp0", // device
173 "0" // listen port
178 #ifdef LINUX26
179 if (nvram_get_int("idle_enable") == 1) {
180 xstart( "sd-idle" );
182 #endif
186 void stop_usb(void)
188 int disabled = !nvram_get_int("usb_enable");
190 // only find and kill the printer server we started (port 0)
191 p9100d_sig(SIGTERM);
192 modprobe_r(USBPRINTER_MOD);
194 // only stop storage services if disabled
195 if (disabled || !nvram_get_int("usb_storage")) {
196 // Unmount all partitions
197 remove_storage_main(0);
199 // Stop storage services
200 modprobe_r("ext2");
201 modprobe_r("ext3");
202 modprobe_r("jbd");
203 #ifdef LINUX26
204 modprobe_r("mbcache");
205 #endif
206 modprobe_r("vfat");
207 modprobe_r("fat");
208 modprobe_r("fuse");
209 #ifdef TCONFIG_HFS
210 modprobe_r("hfs");
211 modprobe_r("hfsplus");
212 #endif
213 sleep(1);
214 #ifdef TCONFIG_SAMBASRV
215 modprobe_r("nls_cp437");
216 modprobe_r("nls_cp850");
217 modprobe_r("nls_cp852");
218 modprobe_r("nls_cp866");
219 #ifdef LINUX26
220 modprobe_r("nls_cp932");
221 modprobe_r("nls_cp936");
222 modprobe_r("nls_cp949");
223 modprobe_r("nls_cp950");
224 #endif
225 #endif
226 modprobe_r(USBSTORAGE_MOD);
227 modprobe_r(SD_MOD);
228 #ifdef LINUX26
229 modprobe_r(SCSI_WAIT_MOD);
230 #endif
231 modprobe_r(SCSI_MOD);
234 #if defined(LINUX26) && defined(TCONFIG_USB_EXTRAS)
235 if (disabled || !nvram_get_int("usb_storage") || nvram_get_int("usb_mmc") != 1) {
236 modprobe_r("sdhci");
237 modprobe_r("mmc_block");
238 modprobe_r("mmc_core");
240 #endif
242 if (disabled || nvram_get_int("usb_ohci") != 1) modprobe_r(USBOHCI_MOD);
243 if (disabled || nvram_get_int("usb_uhci") != 1) modprobe_r(USBUHCI_MOD);
244 if (disabled || nvram_get_int("usb_usb2") != 1) modprobe_r(USB20_MOD);
246 #ifdef LINUX26
247 modprobe_r("leds-usb");
248 modprobe_r("ledtrig-usbdev");
249 led(LED_USB, LED_OFF);
250 #endif
252 // only unload core modules if usb is disabled
253 if (disabled) {
254 umount("/proc/bus/usb"); // unmount usb device filesystem
255 modprobe_r(USBOHCI_MOD);
256 modprobe_r(USBUHCI_MOD);
257 modprobe_r(USB20_MOD);
258 modprobe_r(USBCORE_MOD);
261 #ifdef LINUX26
262 if (nvram_get_int("idle_enable") == 0) {
263 killall("sd-idle", SIGTERM);
266 if (nvram_match("3g_usb", "0") ) {
267 if (nvram_match("3g_module", "sierra") ) {
268 modprobe_r("sierra");
269 modprobe_r("usbserial");
271 if (nvram_match("3g_module", "option") ) {
272 modprobe_r("option");
273 modprobe_r("usbserial");
275 // shibby
276 // when modem use usbserial module and we will try remove module, module will crash
277 // the only solution at the moment is rebbot router
278 // FIX THIS
280 #endif
284 #define MOUNT_VAL_FAIL 0
285 #define MOUNT_VAL_RONLY 1
286 #define MOUNT_VAL_RW 2
287 #define MOUNT_VAL_EXIST 3
289 int mount_r(char *mnt_dev, char *mnt_dir, char *type)
291 struct mntent *mnt;
292 int ret;
293 char options[140];
294 char flagfn[128];
295 int dir_made;
297 if ((mnt = findmntents(mnt_dev, 0, NULL, 0))) {
298 syslog(LOG_INFO, "USB partition at %s already mounted on %s",
299 mnt_dev, mnt->mnt_dir);
300 return MOUNT_VAL_EXIST;
303 options[0] = 0;
305 if (type) {
306 unsigned long flags = MS_NOATIME | MS_NODEV;
308 if (strcmp(type, "swap") == 0 || strcmp(type, "mbr") == 0) {
309 /* not a mountable partition */
310 flags = 0;
312 else if (strcmp(type, "ext2") == 0 || strcmp(type, "ext3") == 0) {
313 if (nvram_invmatch("usb_ext_opt", ""))
314 sprintf(options, nvram_safe_get("usb_ext_opt"));
316 else if (strcmp(type, "vfat") == 0) {
317 if (nvram_invmatch("smbd_cset", ""))
318 sprintf(options, "iocharset=%s%s",
319 isdigit(nvram_get("smbd_cset")[0]) ? "cp" : "",
320 nvram_get("smbd_cset"));
321 if (nvram_invmatch("smbd_cpage", "")) {
322 char *cp = nvram_safe_get("smbd_cpage");
323 sprintf(options + strlen(options), ",codepage=%s" + (options[0] ? 0 : 1), cp);
324 sprintf(flagfn, "nls_cp%s", cp);
326 cp = nvram_get("smbd_nlsmod");
327 if ((cp) && (*cp != 0) && (strcmp(cp, flagfn) != 0))
328 modprobe_r(cp);
330 modprobe(flagfn);
331 nvram_set("smbd_nlsmod", flagfn);
333 sprintf(options + strlen(options), ",shortname=winnt" + (options[0] ? 0 : 1));
334 #ifdef LINUX26
335 sprintf(options + strlen(options), ",flush" + (options[0] ? 0 : 1));
336 #endif
337 if (nvram_invmatch("usb_fat_opt", ""))
338 sprintf(options + strlen(options), "%s%s", options[0] ? "," : "", nvram_safe_get("usb_fat_opt"));
340 else if (strncmp(type, "ntfs", 4) == 0) {
341 if (nvram_invmatch("smbd_cset", ""))
342 sprintf(options, "iocharset=%s%s",
343 isdigit(nvram_get("smbd_cset")[0]) ? "cp" : "",
344 nvram_get("smbd_cset"));
345 if (nvram_invmatch("usb_ntfs_opt", ""))
346 sprintf(options + strlen(options), "%s%s", options[0] ? "," : "", nvram_safe_get("usb_ntfs_opt"));
349 if (flags) {
350 if ((dir_made = mkdir_if_none(mnt_dir))) {
351 /* Create the flag file for remove the directory on dismount. */
352 sprintf(flagfn, "%s/.autocreated-dir", mnt_dir);
353 f_write(flagfn, NULL, 0, 0, 0);
356 ret = mount(mnt_dev, mnt_dir, type, flags, options[0] ? options : "");
358 /* try ntfs-3g in case it's installed */
359 if (ret != 0 && strncmp(type, "ntfs", 4) == 0) {
360 sprintf(options + strlen(options), ",noatime,nodev" + (options[0] ? 0 : 1));
361 #ifdef TCONFIG_NTFS
362 if (nvram_get_int("usb_fs_ntfs"))
363 #endif
364 ret = eval("ntfs-3g", "-o", options, mnt_dev, mnt_dir);
367 if (ret != 0 && strncmp(type, "hfs", "") == 0) {
368 ret = eval("mount", "-o", "noatime,nodev", mnt_dev, mnt_dir);
371 if (ret != 0 && strncmp(type, "hfsplus", "") == 0) {
372 ret = eval("mount", "-o", "noatime,nodev", mnt_dev, mnt_dir);
375 if (ret != 0) /* give it another try - guess fs */
376 ret = eval("mount", "-o", "noatime,nodev", mnt_dev, mnt_dir);
378 if (ret == 0) {
379 syslog(LOG_INFO, "USB %s%s fs at %s mounted on %s",
380 type, (flags & MS_RDONLY) ? " (ro)" : "", mnt_dev, mnt_dir);
381 return (flags & MS_RDONLY) ? MOUNT_VAL_RONLY : MOUNT_VAL_RW;
384 if (dir_made) {
385 unlink(flagfn);
386 rmdir(mnt_dir);
390 return MOUNT_VAL_FAIL;
394 struct mntent *mount_fstab(char *dev_name, char *type, char *label, char *uuid)
396 struct mntent *mnt = NULL;
397 #if 0
398 if (eval("mount", "-a") == 0)
399 mnt = findmntents(dev_name, 0, NULL, 0);
400 #else
401 char spec[PATH_MAX+1];
403 if (label && *label) {
404 sprintf(spec, "LABEL=%s", label);
405 if (eval("mount", spec) == 0)
406 mnt = findmntents(dev_name, 0, NULL, 0);
409 if (!mnt && uuid && *uuid) {
410 sprintf(spec, "UUID=%s", uuid);
411 if (eval("mount", spec) == 0)
412 mnt = findmntents(dev_name, 0, NULL, 0);
415 if (!mnt) {
416 if (eval("mount", dev_name) == 0)
417 mnt = findmntents(dev_name, 0, NULL, 0);
420 if (!mnt) {
421 /* Still did not find what we are looking for, try absolute path */
422 if (realpath(dev_name, spec)) {
423 if (eval("mount", spec) == 0)
424 mnt = findmntents(dev_name, 0, NULL, 0);
427 #endif
429 if (mnt)
430 syslog(LOG_INFO, "USB %s fs at %s mounted on %s", type, dev_name, mnt->mnt_dir);
431 return (mnt);
435 /* Check if the UFD is still connected because the links created in /dev/discs
436 * are not removed when the UFD is unplugged.
437 * The file to read is: /proc/scsi/usb-storage-#/#, where # is the host no.
438 * We are looking for "Attached: Yes".
440 static int usb_ufd_connected(int host_no)
442 char proc_file[128];
443 #ifndef LINUX26
444 char line[256];
445 #endif
446 FILE *fp;
448 sprintf(proc_file, "%s/%s-%d/%d", PROC_SCSI_ROOT, USB_STORAGE, host_no, host_no);
449 fp = fopen(proc_file, "r");
451 if (!fp) {
452 /* try the way it's implemented in newer kernels: /proc/scsi/usb-storage/[host] */
453 sprintf(proc_file, "%s/%s/%d", PROC_SCSI_ROOT, USB_STORAGE, host_no);
454 fp = fopen(proc_file, "r");
457 if (fp) {
458 #ifdef LINUX26
459 fclose(fp);
460 return 1;
461 #else
462 while (fgets(line, sizeof(line), fp) != NULL) {
463 if (strstr(line, "Attached: Yes")) {
464 fclose(fp);
465 return 1;
468 fclose(fp);
469 #endif
472 return 0;
476 #ifndef MNT_DETACH
477 #define MNT_DETACH 0x00000002 /* from linux/fs.h - just detach from the tree */
478 #endif
479 int umount_mountpoint(struct mntent *mnt, uint flags);
480 int uswap_mountpoint(struct mntent *mnt, uint flags);
482 /* Unmount this partition from all its mountpoints. Note that it may
483 * actually be mounted several times, either with different names or
484 * with "-o bind" flag.
485 * If the special flagfile is now revealed, delete it and [attempt to] delete
486 * the directory.
488 int umount_partition(char *dev_name, int host_num, char *dsc_name, char *pt_name, uint flags)
490 sync(); /* This won't matter if the device is unplugged, though. */
492 if (flags & EFH_HUNKNOWN) {
493 /* EFH_HUNKNOWN flag is passed if the host was unknown.
494 * Only unmount disconnected drives in this case.
496 if (usb_ufd_connected(host_num))
497 return 0;
500 /* Find all the active swaps that are on this device and stop them. */
501 findmntents(dev_name, 1, uswap_mountpoint, flags);
503 /* Find all the mountpoints that are for this device and unmount them. */
504 findmntents(dev_name, 0, umount_mountpoint, flags);
505 return 0;
508 int uswap_mountpoint(struct mntent *mnt, uint flags)
510 swapoff(mnt->mnt_fsname);
511 return 0;
514 int umount_mountpoint(struct mntent *mnt, uint flags)
516 int ret = 1, count;
517 char flagfn[128];
519 sprintf(flagfn, "%s/.autocreated-dir", mnt->mnt_dir);
521 /* Run user pre-unmount scripts if any. It might be too late if
522 * the drive has been disconnected, but we'll try it anyway.
524 if (nvram_get_int("usb_automount"))
525 run_nvscript("script_usbumount", mnt->mnt_dir, 3);
526 /* Run *.autostop scripts located in the root of the partition being unmounted if any. */
527 run_userfile(mnt->mnt_dir, ".autostop", mnt->mnt_dir, 5);
528 run_nvscript("script_autostop", mnt->mnt_dir, 5);
530 count = 0;
531 while ((ret = umount(mnt->mnt_dir)) && (count < 2)) {
532 count++;
533 /* If we could not unmount the drive on the 1st try,
534 * kill all NAS applications so they are not keeping the device busy -
535 * unless it's an unmount request from the Web GUI.
537 if ((count == 1) && ((flags & EFH_USER) == 0))
538 restart_nas_services(1, 0);
539 sleep(1);
542 if (ret == 0)
543 syslog(LOG_INFO, "USB partition unmounted from %s", mnt->mnt_dir);
545 if (ret && ((flags & EFH_SHUTDN) != 0)) {
546 /* If system is stopping (not restarting), and we couldn't unmount the
547 * partition, try to remount it as read-only. Ignore the return code -
548 * we can still try to do a lazy unmount.
550 eval("mount", "-o", "remount,ro", mnt->mnt_dir);
553 if (ret && ((flags & EFH_USER) == 0)) {
554 /* Make one more try to do a lazy unmount unless it's an unmount
555 * request from the Web GUI.
556 * MNT_DETACH will expose the underlying mountpoint directory to all
557 * except whatever has cd'ed to the mountpoint (thereby making it busy).
558 * So the unmount can't actually fail. It disappears from the ken of
559 * everyone else immediately, and from the ken of whomever is keeping it
560 * busy when they move away from it. And then it disappears for real.
562 ret = umount2(mnt->mnt_dir, MNT_DETACH);
563 syslog(LOG_INFO, "USB partition busy - will unmount ASAP from %s", mnt->mnt_dir);
566 if (ret == 0) {
567 if ((unlink(flagfn) == 0)) {
568 // Only delete the directory if it was auto-created
569 rmdir(mnt->mnt_dir);
572 return (ret == 0);
576 /* Mount this partition on this disc.
577 * If the device is already mounted on any mountpoint, don't mount it again.
578 * If this is a swap partition, try swapon -a.
579 * If this is a regular partition, try mount -a.
581 * Before we mount any partitions:
582 * If the type is swap and /etc/fstab exists, do "swapon -a"
583 * If /etc/fstab exists, try mounting using fstab.
584 * We delay invoking mount because mount will probe all the partitions
585 * to read the labels, and we don't want it to do that early on.
586 * We don't invoke swapon until we actually find a swap partition.
588 * If the mount succeeds, execute the *.autorun scripts in the top
589 * directory of the newly mounted partition.
590 * Returns NZ for success, 0 if we did not mount anything.
592 int mount_partition(char *dev_name, int host_num, char *dsc_name, char *pt_name, uint flags)
594 char the_label[128], mountpoint[128], uuid[40];
595 int ret;
596 char *type, *p;
597 static char *swp_argv[] = { "swapon", "-a", NULL };
598 struct mntent *mnt;
600 if ((type = find_label_or_uuid(dev_name, the_label, uuid)) == NULL)
601 return 0;
603 if (f_exists("/etc/fstab")) {
604 if (strcmp(type, "swap") == 0) {
605 _eval(swp_argv, NULL, 0, NULL);
606 return 0;
609 if (mount_r(dev_name, NULL, NULL) == MOUNT_VAL_EXIST)
610 return 0;
612 if ((mnt = mount_fstab(dev_name, type, the_label, uuid))) {
613 strcpy(mountpoint, mnt->mnt_dir);
614 ret = MOUNT_VAL_RW;
615 goto done;
619 if (*the_label != 0) {
620 for (p = the_label; *p; p++) {
621 if (!isalnum(*p) && !strchr("+-&.@", *p))
622 *p = '_';
624 sprintf(mountpoint, "%s/%s", MOUNT_ROOT, the_label);
625 if ((ret = mount_r(dev_name, mountpoint, type)))
626 goto done;
629 /* Can't mount to /mnt/LABEL, so try mounting to /mnt/discDN_PN */
630 sprintf(mountpoint, "%s/%s", MOUNT_ROOT, pt_name);
631 ret = mount_r(dev_name, mountpoint, type);
632 done:
633 if (ret == MOUNT_VAL_RONLY || ret == MOUNT_VAL_RW)
635 /* Run user *.autorun and post-mount scripts if any. */
636 run_userfile(mountpoint, ".autorun", mountpoint, 3);
637 if (nvram_get_int("usb_automount"))
638 run_nvscript("script_usbmount", mountpoint, 3);
640 return (ret == MOUNT_VAL_RONLY || ret == MOUNT_VAL_RW);
644 #if 0 /* LINUX26 */
647 * Finds SCSI Host number. Returns the host number >=0 if found, or (-1) otherwise.
648 * The name and host number of scsi block device in kernel 2.6 (for attached devices) can be found as
649 * /sys($DEVPATH)/host<host_no>/target<*>/<id>/block:[sda|sdb|...]
650 * where $DEVPATH is passed to hotplug events, and looks like
651 * /devices/pci0000:00/0000:00:04.1/usb1/1-1/1-1:1.2
653 * For printers this function finds a minor assigned to a printer
654 * /sys($DEVPATH)/usb:lp[0|1|2|...]
656 int find_dev_host(const char *devpath)
658 DIR *usb_devpath;
659 struct dirent *dp;
660 char buf[256];
661 int host = -1; /* Scsi Host */
663 sprintf(buf, "/sys%s", devpath);
664 if ((usb_devpath = opendir(buf))) {
665 while ((dp = readdir(usb_devpath))) {
666 errno = 0;
667 if (strncmp(dp->d_name, "host", 4) == 0) {
668 host = strtol(dp->d_name + 4, (char **)NULL, 10);
669 if (errno)
670 host = -1;
671 else
672 break;
674 else if (strncmp(dp->d_name, "usb:lp", 6) == 0) {
675 host = strtol(dp->d_name + 6, (char **)NULL, 10);
676 if (errno)
677 host = -1;
678 else
679 break;
681 else
682 continue;
684 closedir(usb_devpath);
686 return (host);
689 #endif /* LINUX26 */
691 int dir_is_mountpoint(const char *root, const char *dir)
693 char path[256];
694 struct stat sb;
695 int thisdev;
697 snprintf(path, sizeof(path), "%s%s%s", root ? : "", root ? "/" : "", dir);
699 /* Check if this is a directory */
700 sb.st_mode = S_IFDIR; /* failsafe */
701 stat(path, &sb);
703 if (S_ISDIR(sb.st_mode)) {
705 /* If this dir & its parent dir are on the same device, it is not a mountpoint */
706 strcat(path, "/.");
707 stat(path, &sb);
708 thisdev = sb.st_dev;
709 strcat(path, ".");
710 ++sb.st_dev; /* failsafe */
711 stat(path, &sb);
713 return (thisdev != sb.st_dev);
716 return 0;
719 /* Mount or unmount all partitions on this controller.
720 * Parameter: action_add:
721 * 0 = unmount
722 * >0 = mount only if automount config option is enabled.
723 * <0 = mount regardless of config option.
725 void hotplug_usb_storage_device(int host_no, int action_add, uint flags)
727 if (!nvram_get_int("usb_enable"))
728 return;
729 _dprintf("%s: host %d action: %d\n", __FUNCTION__, host_no, action_add);
731 if (action_add) {
732 if (nvram_get_int("usb_storage") && (nvram_get_int("usb_automount") || action_add < 0)) {
733 /* Do not probe the device here. It's either initiated by user,
734 * or hotplug_usb() already did.
736 if (exec_for_host(host_no, 0x00, flags, mount_partition)) {
737 restart_nas_services(0, 1); // restart all NAS applications
741 else {
742 if (nvram_get_int("usb_storage") || ((flags & EFH_USER) == 0)) {
743 /* When unplugged, unmount the device even if
744 * usb storage is disabled in the GUI.
746 exec_for_host(host_no, (flags & EFH_USER) ? 0x00 : 0x02, flags, umount_partition);
747 /* Restart NAS applications (they could be killed by umount_mountpoint),
748 * or just re-read the configuration.
750 restart_nas_services(0, 1);
756 /* This gets called at reboot or upgrade. The system is stopping. */
757 void remove_storage_main(int shutdn)
759 if (shutdn)
760 restart_nas_services(1, 0);
761 /* Unmount all partitions */
762 exec_for_host(-1, 0x02, shutdn ? EFH_SHUTDN : 0, umount_partition);
766 /*******
767 * All the complex locking & checking code was removed when the kernel USB-storage
768 * bugs were fixed.
769 * The crash bug was with overlapped I/O to different USB drives, not specifically
770 * with mount processing.
772 * And for USB devices that are slow to come up. The kernel now waits until the
773 * USB drive has settled, and it correctly reads the partition table before calling
774 * the hotplug agent.
776 * The kernel patch was cleaning up data structures on an unplug. It
777 * needs to wait until the disk is unmounted. We have 20 seconds to do
778 * the unmounts.
779 *******/
781 #ifdef LINUX26
782 static inline void usbled_proc(char *device, int add)
784 char *p;
785 char param[32];
787 if (do_led(LED_USB, LED_PROBE) != 255) {
788 strncpy(param, device, sizeof(param));
789 if ((p = strchr(param, ':')) != NULL)
790 *p = 0;
792 /* verify if we need to ignore this device (i.e. an internal SD/MMC slot ) */
793 p = nvram_safe_get("usb_noled");
794 if (strcmp(p, param) == 0)
795 return;
797 f_write_string(add ? "/proc/leds-usb/add" : "/proc/leds-usb/remove", param, 0, 0);
800 #endif
802 /* Plugging or removing usb device
804 * On an occurrance, multiple hotplug events may be fired off.
805 * For example, if a hub is plugged or unplugged, an event
806 * will be generated for everything downstream of it, plus one for
807 * the hub itself. These are fired off simultaneously, not serially.
808 * This means that many many hotplug processes will be running at
809 * the same time.
811 * The hotplug event generated by the kernel gives us several pieces
812 * of information:
813 * PRODUCT is vendorid/productid/rev#.
814 * DEVICE is /proc/bus/usb/bus#/dev#
815 * ACTION is add or remove
816 * SCSI_HOST is the host (controller) number (this relies on the custom kernel patch)
818 * Note that when we get a hotplug add event, the USB susbsystem may
819 * or may not have yet tried to read the partition table of the
820 * device. For a new controller that has never been seen before,
821 * generally yes. For a re-plug of a controller that has been seen
822 * before, generally no.
824 * On a remove, the partition info has not yet been expunged. The
825 * partitions show up as /dev/discs/disc#/part#, and /proc/partitions.
826 * It appears that doing a "stat" for a non-existant partition will
827 * causes the kernel to re-validate the device and update the
828 * partition table info. However, it won't re-validate if the disc is
829 * mounted--you'll get a "Device busy for revalidation (usage=%d)" in
830 * syslog.
832 * The $INTERFACE is "class/subclass/protocol"
833 * Some interesting classes:
834 * 8 = mass storage
835 * 7 = printer
836 * 3 = HID. 3/1/2 = mouse.
837 * 6 = still image (6/1/1 = Digital camera Camera)
838 * 9 = Hub
839 * 255 = scanner (255/255/255)
841 * Observed:
842 * Hub seems to have no INTERFACE (null), and TYPE of "9/0/0"
843 * Flash disk seems to have INTERFACE of "8/6/80", and TYPE of "0/0/0"
845 * When a hub is unplugged, a hotplug event is generated for it and everything
846 * downstream from it. You cannot depend on getting these events in any
847 * particular order, since there will be many hotplug programs all fired off
848 * at almost the same time.
849 * On a remove, don't try to access the downstream devices right away, give the
850 * kernel time to finish cleaning up all the data structures, which will be
851 * in the process of being torn down.
853 * On the initial plugin, the first time the kernel usb-storage subsystem sees
854 * the host (identified by GUID), it automatically reads the partition table.
855 * On subsequent plugins, it does not.
857 * Special values for Web Administration to unmount or remount
858 * all partitions of the host:
859 * INTERFACE=TOMATO/...
860 * ACTION=add/remove
861 * SCSI_HOST=<host_no>
862 * If host_no is negative, we unmount all partions of *all* hosts.
864 void hotplug_usb(void)
866 int add;
867 int host = -1;
868 char *interface = getenv("INTERFACE");
869 char *action = getenv("ACTION");
870 char *product = getenv("PRODUCT");
871 #ifdef LINUX26
872 char *device = getenv("DEVICENAME");
873 int is_block = strcmp(getenv("SUBSYSTEM") ? : "", "block") == 0;
874 #else
875 char *device = getenv("DEVICE");
876 #endif
877 char *scsi_host = getenv("SCSI_HOST");
879 _dprintf("%s hotplug INTERFACE=%s ACTION=%s PRODUCT=%s HOST=%s DEVICE=%s\n",
880 getenv("SUBSYSTEM") ? : "USB", interface, action, product, scsi_host, device);
882 if (!nvram_get_int("usb_enable")) return;
883 #ifdef LINUX26
884 if (!action || ((!interface || !product) && !is_block))
885 #else
886 if (!interface || !action || !product) /* Hubs bail out here. */
887 #endif
888 return;
890 if (scsi_host)
891 host = atoi(scsi_host);
893 if (!wait_action_idle(10)) return;
895 add = (strcmp(action, "add") == 0);
896 if (add && (strncmp(interface ? : "", "TOMATO/", 7) != 0)) {
897 #ifdef LINUX26
898 if (!is_block && device)
899 #endif
900 syslog(LOG_DEBUG, "Attached USB device %s [INTERFACE=%s PRODUCT=%s]",
901 device, interface, product);
902 #ifndef LINUX26
903 /* To allow automount to be blocked on startup.
904 * In kernel 2.6 we still need to serialize mount/umount calls -
905 * so the lock is down below in the "block" hotplug processing.
907 file_unlock(file_lock("usb"));
908 #endif
911 if (strncmp(interface ? : "", "TOMATO/", 7) == 0) { /* web admin */
912 if (scsi_host == NULL)
913 host = atoi(product); // for backward compatibility
914 /* If host is negative, unmount all partitions of *all* hosts.
915 * If host == -1, execute "soft" unmount (do not kill NAS apps, no "lazy" umount).
916 * If host == -2, run "hard" unmount, as if the drive is unplugged.
917 * This feature can be used in custom scripts as following:
919 * # INTERFACE=TOMATO/1 ACTION=remove PRODUCT=-1 SCSI_HOST=-1 hotplug usb
921 * PRODUCT is required to pass the env variables verification.
923 /* Unmount or remount all partitions of the host. */
924 hotplug_usb_storage_device(host < 0 ? -1 : host, add ? -1 : 0,
925 host == -2 ? 0 : EFH_USER);
927 #ifdef LINUX26
928 else if (is_block && strcmp(getenv("MAJOR") ? : "", "8") == 0 && strcmp(getenv("PHYSDEVBUS") ? : "", "scsi") == 0) {
929 /* scsi partition */
930 char devname[64];
931 int lock;
933 sprintf(devname, "/dev/%s", device);
934 lock = file_lock("usb");
935 if (add) {
936 if (nvram_get_int("usb_storage") && nvram_get_int("usb_automount")) {
937 int minor = atoi(getenv("MINOR") ? : "0");
938 if ((minor % 16) == 0 && !is_no_partition(device)) {
939 /* This is a disc, and not a "no-partition" device,
940 * like APPLE iPOD shuffle. We can't mount it.
942 return;
944 if (mount_partition(devname, host, NULL, device, EFH_HP_ADD)) {
945 restart_nas_services(0, 1); // restart all NAS applications
949 else {
950 /* When unplugged, unmount the device even if usb storage is disabled in the GUI */
951 umount_partition(devname, host, NULL, device, EFH_HP_REMOVE);
952 /* Restart NAS applications (they could be killed by umount_mountpoint),
953 * or just re-read the configuration.
955 restart_nas_services(0, 1);
957 file_unlock(lock);
959 #endif
960 else if (strncmp(interface ? : "", "8/", 2) == 0) { /* usb storage */
961 #ifdef LINUX26
962 usbled_proc(device, add);
963 #endif
964 run_nvscript("script_usbhotplug", NULL, 2);
965 #ifndef LINUX26
966 hotplug_usb_storage_device(host, add, (add ? EFH_HP_ADD : EFH_HP_REMOVE) | (host < 0 ? EFH_HUNKNOWN : 0));
967 #endif
969 else { /* It's some other type of USB device, not storage. */
970 #ifdef LINUX26
971 if (is_block) return;
972 #endif
973 #ifdef LINUX26
974 if (strncmp(interface ? : "", "7/", 2) == 0) /* printer */
975 usbled_proc(device, add);
976 #endif
977 /* Do nothing. The user's hotplug script must do it all. */
978 run_nvscript("script_usbhotplug", NULL, 2);