UPS: move service to usb
[tomato.git] / release / src / router / rc / usb.c
blob601f09f397edf7ccad9650edc4921a78d62b1723
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");
129 #ifdef TCONFIG_HFS
130 if (nvram_get_int("usb_fs_hfs")) {
131 modprobe("hfs");
132 modprobe("hfsplus");
134 #endif
137 /* if enabled, force USB2 before USB1.1 */
138 if (nvram_get_int("usb_usb2") == 1) {
139 i = nvram_get_int("usb_irq_thresh");
140 if ((i < 0) || (i > 6))
141 i = 0;
142 sprintf(param, "log2_irq_thresh=%d", i);
143 modprobe(USB20_MOD, param);
146 if (nvram_get_int("usb_uhci") == 1) {
147 modprobe(USBUHCI_MOD);
150 if (nvram_get_int("usb_ohci") == 1) {
151 modprobe(USBOHCI_MOD);
154 if (nvram_get_int("usb_printer")) {
155 symlink("/dev/usb", "/dev/printers");
156 modprobe(USBPRINTER_MOD);
158 /* start printer server only if not already running */
159 if (p9100d_sig(0) != 0) {
160 eval("p910nd",
161 nvram_get_int("usb_printer_bidirect") ? "-b" : "", //bidirectional
162 "-f", "/dev/usb/lp0", // device
163 "0" // listen port
167 #ifdef LINUX26
168 if (nvram_get_int("idle_enable") == 1) {
169 xstart( "sd-idle" );
171 #endif
173 #ifdef TCONFIG_UPS
174 start_ups();
175 #endif
180 void stop_usb(void)
182 int disabled = !nvram_get_int("usb_enable");
184 // only find and kill the printer server we started (port 0)
185 p9100d_sig(SIGTERM);
186 modprobe_r(USBPRINTER_MOD);
188 // only stop storage services if disabled
189 if (disabled || !nvram_get_int("usb_storage")) {
190 // Unmount all partitions
191 remove_storage_main(0);
193 // Stop storage services
194 modprobe_r("ext2");
195 modprobe_r("ext3");
196 modprobe_r("jbd");
197 #ifdef LINUX26
198 modprobe_r("mbcache");
199 #endif
200 modprobe_r("vfat");
201 modprobe_r("fat");
202 modprobe_r("fuse");
203 #ifdef TCONFIG_SAMBASRV
204 modprobe_r("hfs");
205 modprobe_r("hfsplus");
206 #endif
207 sleep(1);
208 #ifdef TCONFIG_SAMBASRV
209 modprobe_r("nls_cp437");
210 modprobe_r("nls_cp850");
211 modprobe_r("nls_cp852");
212 modprobe_r("nls_cp866");
213 #ifdef LINUX26
214 modprobe_r("nls_cp932");
215 modprobe_r("nls_cp936");
216 modprobe_r("nls_cp949");
217 modprobe_r("nls_cp950");
218 #endif
219 #endif
220 modprobe_r(USBSTORAGE_MOD);
221 modprobe_r(SD_MOD);
222 #ifdef LINUX26
223 modprobe_r(SCSI_WAIT_MOD);
224 #endif
225 modprobe_r(SCSI_MOD);
228 if (disabled || nvram_get_int("usb_ohci") != 1) modprobe_r(USBOHCI_MOD);
229 if (disabled || nvram_get_int("usb_uhci") != 1) modprobe_r(USBUHCI_MOD);
230 if (disabled || nvram_get_int("usb_usb2") != 1) modprobe_r(USB20_MOD);
232 #ifdef LINUX26
233 modprobe_r("leds-usb");
234 modprobe_r("ledtrig-usbdev");
235 led(LED_USB, LED_OFF);
236 #endif
238 // only unload core modules if usb is disabled
239 if (disabled) {
240 umount("/proc/bus/usb"); // unmount usb device filesystem
241 modprobe_r(USBOHCI_MOD);
242 modprobe_r(USBUHCI_MOD);
243 modprobe_r(USB20_MOD);
244 modprobe_r(USBCORE_MOD);
247 #ifdef LINUX26
248 if (nvram_get_int("idle_enable") == 0) {
249 killall("sd-idle", SIGTERM);
252 #ifdef TCONFIG_UPS
253 stop_ups();
254 #endif
256 if (nvram_match("3g_usb", "0") ) {
257 if (nvram_match("3g_module", "sierra") ) {
258 modprobe_r("sierra");
259 modprobe_r("usbserial");
262 if (nvram_match("3g_module", "option") ) {
263 modprobe_r("option");
264 modprobe_r("usbserial");
267 // shibby
268 // when modem use usbserial module and we will try remove module, module will crash
269 // the only solution at the moment is reboot router
270 // FIX THIS
271 if (nvram_match("3g_module", "usbserial") ) {
272 modprobe_r("usbserial");
276 #endif
281 #define MOUNT_VAL_FAIL 0
282 #define MOUNT_VAL_RONLY 1
283 #define MOUNT_VAL_RW 2
284 #define MOUNT_VAL_EXIST 3
286 int mount_r(char *mnt_dev, char *mnt_dir, char *type)
288 struct mntent *mnt;
289 int ret;
290 char options[140];
291 char flagfn[128];
292 int dir_made;
294 if ((mnt = findmntents(mnt_dev, 0, NULL, 0))) {
295 syslog(LOG_INFO, "USB partition at %s already mounted on %s",
296 mnt_dev, mnt->mnt_dir);
297 return MOUNT_VAL_EXIST;
300 options[0] = 0;
302 if (type) {
303 unsigned long flags = MS_NOATIME | MS_NODEV;
305 if (strcmp(type, "swap") == 0 || strcmp(type, "mbr") == 0) {
306 /* not a mountable partition */
307 flags = 0;
309 else if (strcmp(type, "ext2") == 0 || strcmp(type, "ext3") == 0) {
310 if (nvram_invmatch("usb_ext_opt", ""))
311 sprintf(options, nvram_safe_get("usb_ext_opt"));
313 else if (strcmp(type, "vfat") == 0) {
314 if (nvram_invmatch("smbd_cset", ""))
315 sprintf(options, "iocharset=%s%s",
316 isdigit(nvram_get("smbd_cset")[0]) ? "cp" : "",
317 nvram_get("smbd_cset"));
318 if (nvram_invmatch("smbd_cpage", "")) {
319 char *cp = nvram_safe_get("smbd_cpage");
320 sprintf(options + strlen(options), ",codepage=%s" + (options[0] ? 0 : 1), cp);
321 sprintf(flagfn, "nls_cp%s", cp);
323 cp = nvram_get("smbd_nlsmod");
324 if ((cp) && (*cp != 0) && (strcmp(cp, flagfn) != 0))
325 modprobe_r(cp);
327 modprobe(flagfn);
328 nvram_set("smbd_nlsmod", flagfn);
330 sprintf(options + strlen(options), ",shortname=winnt" + (options[0] ? 0 : 1));
331 #ifdef LINUX26
332 sprintf(options + strlen(options), ",flush" + (options[0] ? 0 : 1));
333 #endif
334 if (nvram_invmatch("usb_fat_opt", ""))
335 sprintf(options + strlen(options), "%s%s", options[0] ? "," : "", nvram_safe_get("usb_fat_opt"));
337 else if (strncmp(type, "ntfs", 4) == 0) {
338 if (nvram_invmatch("smbd_cset", ""))
339 sprintf(options, "iocharset=%s%s",
340 isdigit(nvram_get("smbd_cset")[0]) ? "cp" : "",
341 nvram_get("smbd_cset"));
342 if (nvram_invmatch("usb_ntfs_opt", ""))
343 sprintf(options + strlen(options), "%s%s", options[0] ? "," : "", nvram_safe_get("usb_ntfs_opt"));
346 if (flags) {
347 if ((dir_made = mkdir_if_none(mnt_dir))) {
348 /* Create the flag file for remove the directory on dismount. */
349 sprintf(flagfn, "%s/.autocreated-dir", mnt_dir);
350 f_write(flagfn, NULL, 0, 0, 0);
353 ret = mount(mnt_dev, mnt_dir, type, flags, options[0] ? options : "");
355 /* try ntfs-3g in case it's installed */
356 if (ret != 0 && strncmp(type, "ntfs", 4) == 0) {
357 sprintf(options + strlen(options), ",noatime,nodev" + (options[0] ? 0 : 1));
358 #ifdef TCONFIG_NTFS
359 if (nvram_get_int("usb_fs_ntfs"))
360 #endif
361 ret = eval("ntfs-3g", "-o", options, mnt_dev, mnt_dir);
364 #ifdef TCONFIG_HFS
365 if (ret != 0 && strncmp(type, "hfs", "") == 0) {
366 ret = eval("mount", "-o", "noatime,nodev", mnt_dev, mnt_dir);
369 if (ret != 0 && strncmp(type, "hfsplus", "") == 0) {
370 ret = eval("mount", "-o", "noatime,nodev", mnt_dev, mnt_dir);
372 #endif
374 if (ret != 0) /* give it another try - guess fs */
375 ret = eval("mount", "-o", "noatime,nodev", mnt_dev, mnt_dir);
377 if (ret == 0) {
378 syslog(LOG_INFO, "USB %s%s fs at %s mounted on %s",
379 type, (flags & MS_RDONLY) ? " (ro)" : "", mnt_dev, mnt_dir);
380 return (flags & MS_RDONLY) ? MOUNT_VAL_RONLY : MOUNT_VAL_RW;
383 if (dir_made) {
384 unlink(flagfn);
385 rmdir(mnt_dir);
389 return MOUNT_VAL_FAIL;
393 struct mntent *mount_fstab(char *dev_name, char *type, char *label, char *uuid)
395 struct mntent *mnt = NULL;
396 #if 0
397 if (eval("mount", "-a") == 0)
398 mnt = findmntents(dev_name, 0, NULL, 0);
399 #else
400 char spec[PATH_MAX+1];
402 if (label && *label) {
403 sprintf(spec, "LABEL=%s", label);
404 if (eval("mount", spec) == 0)
405 mnt = findmntents(dev_name, 0, NULL, 0);
408 if (!mnt && uuid && *uuid) {
409 sprintf(spec, "UUID=%s", uuid);
410 if (eval("mount", spec) == 0)
411 mnt = findmntents(dev_name, 0, NULL, 0);
414 if (!mnt) {
415 if (eval("mount", dev_name) == 0)
416 mnt = findmntents(dev_name, 0, NULL, 0);
419 if (!mnt) {
420 /* Still did not find what we are looking for, try absolute path */
421 if (realpath(dev_name, spec)) {
422 if (eval("mount", spec) == 0)
423 mnt = findmntents(dev_name, 0, NULL, 0);
426 #endif
428 if (mnt)
429 syslog(LOG_INFO, "USB %s fs at %s mounted on %s", type, dev_name, mnt->mnt_dir);
430 return (mnt);
434 /* Check if the UFD is still connected because the links created in /dev/discs
435 * are not removed when the UFD is unplugged.
436 * The file to read is: /proc/scsi/usb-storage-#/#, where # is the host no.
437 * We are looking for "Attached: Yes".
439 static int usb_ufd_connected(int host_no)
441 char proc_file[128];
442 #ifndef LINUX26
443 char line[256];
444 #endif
445 FILE *fp;
447 sprintf(proc_file, "%s/%s-%d/%d", PROC_SCSI_ROOT, USB_STORAGE, host_no, host_no);
448 fp = fopen(proc_file, "r");
450 if (!fp) {
451 /* try the way it's implemented in newer kernels: /proc/scsi/usb-storage/[host] */
452 sprintf(proc_file, "%s/%s/%d", PROC_SCSI_ROOT, USB_STORAGE, host_no);
453 fp = fopen(proc_file, "r");
456 if (fp) {
457 #ifdef LINUX26
458 fclose(fp);
459 return 1;
460 #else
461 while (fgets(line, sizeof(line), fp) != NULL) {
462 if (strstr(line, "Attached: Yes")) {
463 fclose(fp);
464 return 1;
467 fclose(fp);
468 #endif
471 return 0;
475 #ifndef MNT_DETACH
476 #define MNT_DETACH 0x00000002 /* from linux/fs.h - just detach from the tree */
477 #endif
478 int umount_mountpoint(struct mntent *mnt, uint flags);
479 int uswap_mountpoint(struct mntent *mnt, uint flags);
481 /* Unmount this partition from all its mountpoints. Note that it may
482 * actually be mounted several times, either with different names or
483 * with "-o bind" flag.
484 * If the special flagfile is now revealed, delete it and [attempt to] delete
485 * the directory.
487 int umount_partition(char *dev_name, int host_num, char *dsc_name, char *pt_name, uint flags)
489 sync(); /* This won't matter if the device is unplugged, though. */
491 if (flags & EFH_HUNKNOWN) {
492 /* EFH_HUNKNOWN flag is passed if the host was unknown.
493 * Only unmount disconnected drives in this case.
495 if (usb_ufd_connected(host_num))
496 return 0;
499 /* Find all the active swaps that are on this device and stop them. */
500 findmntents(dev_name, 1, uswap_mountpoint, flags);
502 /* Find all the mountpoints that are for this device and unmount them. */
503 findmntents(dev_name, 0, umount_mountpoint, flags);
504 return 0;
507 int uswap_mountpoint(struct mntent *mnt, uint flags)
509 swapoff(mnt->mnt_fsname);
510 return 0;
513 int umount_mountpoint(struct mntent *mnt, uint flags)
515 int ret = 1, count;
516 char flagfn[128];
518 sprintf(flagfn, "%s/.autocreated-dir", mnt->mnt_dir);
520 /* Run user pre-unmount scripts if any. It might be too late if
521 * the drive has been disconnected, but we'll try it anyway.
523 if (nvram_get_int("usb_automount"))
524 run_nvscript("script_usbumount", mnt->mnt_dir, 3);
525 /* Run *.autostop scripts located in the root of the partition being unmounted if any. */
526 run_userfile(mnt->mnt_dir, ".autostop", mnt->mnt_dir, 5);
527 run_nvscript("script_autostop", mnt->mnt_dir, 5);
529 count = 0;
530 while ((ret = umount(mnt->mnt_dir)) && (count < 2)) {
531 count++;
532 /* If we could not unmount the drive on the 1st try,
533 * kill all NAS applications so they are not keeping the device busy -
534 * unless it's an unmount request from the Web GUI.
536 if ((count == 1) && ((flags & EFH_USER) == 0))
537 restart_nas_services(1, 0);
538 sleep(1);
541 if (ret == 0)
542 syslog(LOG_INFO, "USB partition unmounted from %s", mnt->mnt_dir);
544 if (ret && ((flags & EFH_SHUTDN) != 0)) {
545 /* If system is stopping (not restarting), and we couldn't unmount the
546 * partition, try to remount it as read-only. Ignore the return code -
547 * we can still try to do a lazy unmount.
549 eval("mount", "-o", "remount,ro", mnt->mnt_dir);
552 if (ret && ((flags & EFH_USER) == 0)) {
553 /* Make one more try to do a lazy unmount unless it's an unmount
554 * request from the Web GUI.
555 * MNT_DETACH will expose the underlying mountpoint directory to all
556 * except whatever has cd'ed to the mountpoint (thereby making it busy).
557 * So the unmount can't actually fail. It disappears from the ken of
558 * everyone else immediately, and from the ken of whomever is keeping it
559 * busy when they move away from it. And then it disappears for real.
561 ret = umount2(mnt->mnt_dir, MNT_DETACH);
562 syslog(LOG_INFO, "USB partition busy - will unmount ASAP from %s", mnt->mnt_dir);
565 if (ret == 0) {
566 if ((unlink(flagfn) == 0)) {
567 // Only delete the directory if it was auto-created
568 rmdir(mnt->mnt_dir);
571 return (ret == 0);
575 /* Mount this partition on this disc.
576 * If the device is already mounted on any mountpoint, don't mount it again.
577 * If this is a swap partition, try swapon -a.
578 * If this is a regular partition, try mount -a.
580 * Before we mount any partitions:
581 * If the type is swap and /etc/fstab exists, do "swapon -a"
582 * If /etc/fstab exists, try mounting using fstab.
583 * We delay invoking mount because mount will probe all the partitions
584 * to read the labels, and we don't want it to do that early on.
585 * We don't invoke swapon until we actually find a swap partition.
587 * If the mount succeeds, execute the *.autorun scripts in the top
588 * directory of the newly mounted partition.
589 * Returns NZ for success, 0 if we did not mount anything.
591 int mount_partition(char *dev_name, int host_num, char *dsc_name, char *pt_name, uint flags)
593 char the_label[128], mountpoint[128], uuid[40];
594 int ret;
595 char *type, *p;
596 static char *swp_argv[] = { "swapon", "-a", NULL };
597 struct mntent *mnt;
599 if ((type = find_label_or_uuid(dev_name, the_label, uuid)) == NULL)
600 return 0;
602 if (f_exists("/etc/fstab")) {
603 if (strcmp(type, "swap") == 0) {
604 _eval(swp_argv, NULL, 0, NULL);
605 return 0;
608 if (mount_r(dev_name, NULL, NULL) == MOUNT_VAL_EXIST)
609 return 0;
611 if ((mnt = mount_fstab(dev_name, type, the_label, uuid))) {
612 strcpy(mountpoint, mnt->mnt_dir);
613 ret = MOUNT_VAL_RW;
614 goto done;
618 if (*the_label != 0) {
619 for (p = the_label; *p; p++) {
620 if (!isalnum(*p) && !strchr("+-&.@", *p))
621 *p = '_';
623 sprintf(mountpoint, "%s/%s", MOUNT_ROOT, the_label);
624 if ((ret = mount_r(dev_name, mountpoint, type)))
625 goto done;
628 /* Can't mount to /mnt/LABEL, so try mounting to /mnt/discDN_PN */
629 sprintf(mountpoint, "%s/%s", MOUNT_ROOT, pt_name);
630 ret = mount_r(dev_name, mountpoint, type);
631 done:
632 if (ret == MOUNT_VAL_RONLY || ret == MOUNT_VAL_RW)
634 /* Run user *.autorun and post-mount scripts if any. */
635 run_userfile(mountpoint, ".autorun", mountpoint, 3);
636 if (nvram_get_int("usb_automount"))
637 run_nvscript("script_usbmount", mountpoint, 3);
639 return (ret == MOUNT_VAL_RONLY || ret == MOUNT_VAL_RW);
643 #if 0 /* LINUX26 */
646 * Finds SCSI Host number. Returns the host number >=0 if found, or (-1) otherwise.
647 * The name and host number of scsi block device in kernel 2.6 (for attached devices) can be found as
648 * /sys($DEVPATH)/host<host_no>/target<*>/<id>/block:[sda|sdb|...]
649 * where $DEVPATH is passed to hotplug events, and looks like
650 * /devices/pci0000:00/0000:00:04.1/usb1/1-1/1-1:1.2
652 * For printers this function finds a minor assigned to a printer
653 * /sys($DEVPATH)/usb:lp[0|1|2|...]
655 int find_dev_host(const char *devpath)
657 DIR *usb_devpath;
658 struct dirent *dp;
659 char buf[256];
660 int host = -1; /* Scsi Host */
662 sprintf(buf, "/sys%s", devpath);
663 if ((usb_devpath = opendir(buf))) {
664 while ((dp = readdir(usb_devpath))) {
665 errno = 0;
666 if (strncmp(dp->d_name, "host", 4) == 0) {
667 host = strtol(dp->d_name + 4, (char **)NULL, 10);
668 if (errno)
669 host = -1;
670 else
671 break;
673 else if (strncmp(dp->d_name, "usb:lp", 6) == 0) {
674 host = strtol(dp->d_name + 6, (char **)NULL, 10);
675 if (errno)
676 host = -1;
677 else
678 break;
680 else
681 continue;
683 closedir(usb_devpath);
685 return (host);
688 #endif /* LINUX26 */
690 int dir_is_mountpoint(const char *root, const char *dir)
692 char path[256];
693 struct stat sb;
694 int thisdev;
696 snprintf(path, sizeof(path), "%s%s%s", root ? : "", root ? "/" : "", dir);
698 /* Check if this is a directory */
699 sb.st_mode = S_IFDIR; /* failsafe */
700 stat(path, &sb);
702 if (S_ISDIR(sb.st_mode)) {
704 /* If this dir & its parent dir are on the same device, it is not a mountpoint */
705 strcat(path, "/.");
706 stat(path, &sb);
707 thisdev = sb.st_dev;
708 strcat(path, ".");
709 ++sb.st_dev; /* failsafe */
710 stat(path, &sb);
712 return (thisdev != sb.st_dev);
715 return 0;
718 /* Mount or unmount all partitions on this controller.
719 * Parameter: action_add:
720 * 0 = unmount
721 * >0 = mount only if automount config option is enabled.
722 * <0 = mount regardless of config option.
724 void hotplug_usb_storage_device(int host_no, int action_add, uint flags)
726 if (!nvram_get_int("usb_enable"))
727 return;
728 _dprintf("%s: host %d action: %d\n", __FUNCTION__, host_no, action_add);
730 if (action_add) {
731 if (nvram_get_int("usb_storage") && (nvram_get_int("usb_automount") || action_add < 0)) {
732 /* Do not probe the device here. It's either initiated by user,
733 * or hotplug_usb() already did.
735 if (exec_for_host(host_no, 0x00, flags, mount_partition)) {
736 restart_nas_services(0, 1); // restart all NAS applications
740 else {
741 if (nvram_get_int("usb_storage") || ((flags & EFH_USER) == 0)) {
742 /* When unplugged, unmount the device even if
743 * usb storage is disabled in the GUI.
745 exec_for_host(host_no, (flags & EFH_USER) ? 0x00 : 0x02, flags, umount_partition);
746 /* Restart NAS applications (they could be killed by umount_mountpoint),
747 * or just re-read the configuration.
749 restart_nas_services(0, 1);
755 /* This gets called at reboot or upgrade. The system is stopping. */
756 void remove_storage_main(int shutdn)
758 if (shutdn)
759 restart_nas_services(1, 0);
760 /* Unmount all partitions */
761 exec_for_host(-1, 0x02, shutdn ? EFH_SHUTDN : 0, umount_partition);
765 /*******
766 * All the complex locking & checking code was removed when the kernel USB-storage
767 * bugs were fixed.
768 * The crash bug was with overlapped I/O to different USB drives, not specifically
769 * with mount processing.
771 * And for USB devices that are slow to come up. The kernel now waits until the
772 * USB drive has settled, and it correctly reads the partition table before calling
773 * the hotplug agent.
775 * The kernel patch was cleaning up data structures on an unplug. It
776 * needs to wait until the disk is unmounted. We have 20 seconds to do
777 * the unmounts.
778 *******/
781 /* Plugging or removing usb device
783 * On an occurrance, multiple hotplug events may be fired off.
784 * For example, if a hub is plugged or unplugged, an event
785 * will be generated for everything downstream of it, plus one for
786 * the hub itself. These are fired off simultaneously, not serially.
787 * This means that many many hotplug processes will be running at
788 * the same time.
790 * The hotplug event generated by the kernel gives us several pieces
791 * of information:
792 * PRODUCT is vendorid/productid/rev#.
793 * DEVICE is /proc/bus/usb/bus#/dev#
794 * ACTION is add or remove
795 * SCSI_HOST is the host (controller) number (this relies on the custom kernel patch)
797 * Note that when we get a hotplug add event, the USB susbsystem may
798 * or may not have yet tried to read the partition table of the
799 * device. For a new controller that has never been seen before,
800 * generally yes. For a re-plug of a controller that has been seen
801 * before, generally no.
803 * On a remove, the partition info has not yet been expunged. The
804 * partitions show up as /dev/discs/disc#/part#, and /proc/partitions.
805 * It appears that doing a "stat" for a non-existant partition will
806 * causes the kernel to re-validate the device and update the
807 * partition table info. However, it won't re-validate if the disc is
808 * mounted--you'll get a "Device busy for revalidation (usage=%d)" in
809 * syslog.
811 * The $INTERFACE is "class/subclass/protocol"
812 * Some interesting classes:
813 * 8 = mass storage
814 * 7 = printer
815 * 3 = HID. 3/1/2 = mouse.
816 * 6 = still image (6/1/1 = Digital camera Camera)
817 * 9 = Hub
818 * 255 = scanner (255/255/255)
820 * Observed:
821 * Hub seems to have no INTERFACE (null), and TYPE of "9/0/0"
822 * Flash disk seems to have INTERFACE of "8/6/80", and TYPE of "0/0/0"
824 * When a hub is unplugged, a hotplug event is generated for it and everything
825 * downstream from it. You cannot depend on getting these events in any
826 * particular order, since there will be many hotplug programs all fired off
827 * at almost the same time.
828 * On a remove, don't try to access the downstream devices right away, give the
829 * kernel time to finish cleaning up all the data structures, which will be
830 * in the process of being torn down.
832 * On the initial plugin, the first time the kernel usb-storage subsystem sees
833 * the host (identified by GUID), it automatically reads the partition table.
834 * On subsequent plugins, it does not.
836 * Special values for Web Administration to unmount or remount
837 * all partitions of the host:
838 * INTERFACE=TOMATO/...
839 * ACTION=add/remove
840 * SCSI_HOST=<host_no>
841 * If host_no is negative, we unmount all partions of *all* hosts.
843 void hotplug_usb(void)
845 int add;
846 int host = -1;
847 char *interface = getenv("INTERFACE");
848 char *action = getenv("ACTION");
849 char *product = getenv("PRODUCT");
850 #ifdef LINUX26
851 char *device = getenv("DEVICENAME");
852 int is_block = strcmp(getenv("SUBSYSTEM") ? : "", "block") == 0;
853 #else
854 char *device = getenv("DEVICE");
855 #endif
856 char *scsi_host = getenv("SCSI_HOST");
858 _dprintf("%s hotplug INTERFACE=%s ACTION=%s PRODUCT=%s HOST=%s DEVICE=%s\n",
859 getenv("SUBSYSTEM") ? : "USB", interface, action, product, scsi_host, device);
861 if (!nvram_get_int("usb_enable")) return;
862 #ifdef LINUX26
863 if (!action || ((!interface || !product) && !is_block))
864 #else
865 if (!interface || !action || !product) /* Hubs bail out here. */
866 #endif
867 return;
869 if (scsi_host)
870 host = atoi(scsi_host);
872 if (!wait_action_idle(10)) return;
874 add = (strcmp(action, "add") == 0);
875 if (add && (strncmp(interface ? : "", "TOMATO/", 7) != 0)) {
876 #ifdef LINUX26
877 if (!is_block && device)
878 #endif
879 syslog(LOG_DEBUG, "Attached USB device %s [INTERFACE=%s PRODUCT=%s]",
880 device, interface, product);
881 #ifndef LINUX26
882 /* To allow automount to be blocked on startup.
883 * In kernel 2.6 we still need to serialize mount/umount calls -
884 * so the lock is down below in the "block" hotplug processing.
886 file_unlock(file_lock("usb"));
887 #endif
890 if (strncmp(interface ? : "", "TOMATO/", 7) == 0) { /* web admin */
891 if (scsi_host == NULL)
892 host = atoi(product); // for backward compatibility
893 /* If host is negative, unmount all partitions of *all* hosts.
894 * If host == -1, execute "soft" unmount (do not kill NAS apps, no "lazy" umount).
895 * If host == -2, run "hard" unmount, as if the drive is unplugged.
896 * This feature can be used in custom scripts as following:
898 * # INTERFACE=TOMATO/1 ACTION=remove PRODUCT=-1 SCSI_HOST=-1 hotplug usb
900 * PRODUCT is required to pass the env variables verification.
902 /* Unmount or remount all partitions of the host. */
903 hotplug_usb_storage_device(host < 0 ? -1 : host, add ? -1 : 0,
904 host == -2 ? 0 : EFH_USER);
906 #ifdef LINUX26
907 else if (is_block && strcmp(getenv("MAJOR") ? : "", "8") == 0 && strcmp(getenv("PHYSDEVBUS") ? : "", "scsi") == 0) {
908 /* scsi partition */
909 char devname[64];
910 int lock;
912 sprintf(devname, "/dev/%s", device);
913 lock = file_lock("usb");
914 if (add) {
915 if (nvram_get_int("usb_storage") && nvram_get_int("usb_automount")) {
916 int minor = atoi(getenv("MINOR") ? : "0");
917 if ((minor % 16) == 0 && !is_no_partition(device)) {
918 /* This is a disc, and not a "no-partition" device,
919 * like APPLE iPOD shuffle. We can't mount it.
921 return;
923 if (mount_partition(devname, host, NULL, device, EFH_HP_ADD)) {
924 restart_nas_services(0, 1); // restart all NAS applications
928 else {
929 /* When unplugged, unmount the device even if usb storage is disabled in the GUI */
930 umount_partition(devname, host, NULL, device, EFH_HP_REMOVE);
931 /* Restart NAS applications (they could be killed by umount_mountpoint),
932 * or just re-read the configuration.
934 restart_nas_services(0, 1);
936 file_unlock(lock);
938 #endif
939 else if (strncmp(interface ? : "", "8/", 2) == 0) { /* usb storage */
940 run_nvscript("script_usbhotplug", NULL, 2);
941 #ifndef LINUX26
942 hotplug_usb_storage_device(host, add, (add ? EFH_HP_ADD : EFH_HP_REMOVE) | (host < 0 ? EFH_HUNKNOWN : 0));
943 #endif
945 else { /* It's some other type of USB device, not storage. */
946 #ifdef LINUX26
947 if (is_block) return;
948 #endif
949 /* Do nothing. The user's hotplug script must do it all. */
950 run_nvscript("script_usbhotplug", NULL, 2);