summaryrefslogtreecommitdiff
path: root/firmware/target/arm
diff options
context:
space:
mode:
authorMichael Sevakis <jethead71@rockbox.org>2013-08-05 22:02:45 -0400
committerMichael Sevakis <jethead71@rockbox.org>2014-08-30 03:48:23 +0200
commit7d1a47cf13726c95ac46027156cc12dd9da5b855 (patch)
treeeb20d07656806479a8e1fea25887a490ea30d1d8 /firmware/target/arm
parent95a4c3afcd53a1f8b835dec33de51f9c304de4d9 (diff)
downloadrockbox-7d1a47cf13726c95ac46027156cc12dd9da5b855.tar.gz
rockbox-7d1a47cf13726c95ac46027156cc12dd9da5b855.zip
Rewrite filesystem code (WIP)
This patch redoes the filesystem code from the FAT driver up to the clipboard code in onplay.c. Not every aspect of this is finished therefore it is still "WIP". I don't wish to do too much at once (haha!). What is left to do is get dircache back in the sim and find an implementation for the dircache indicies in the tagcache and playlist code or do something else that has the same benefit. Leaving these out for now does not make anything unusable. All the basics are done. Phone app code should probably get vetted (and app path handling just plain rewritten as environment expansions); the SDL app and Android run well. Main things addressed: 1) Thread safety: There is none right now in the trunk code. Most of what currently works is luck when multiple threads are involved or multiple descriptors to the same file are open. 2) POSIX compliance: Many of the functions behave nothing like their counterparts on a host system. This leads to inconsistent code or very different behavior from native to hosted. One huge offender was rename(). Going point by point would fill a book. 3) Actual running RAM usage: Many targets will use less RAM and less stack space (some more RAM because I upped the number of cache buffers for large memory). There's very little memory lying fallow in rarely-used areas (see 'Key core changes' below). Also, all targets may open the same number of directory streams whereas before those with less than 8MB RAM were limited to 8, not 12 implying those targets will save slightly less. 4) Performance: The test_disk plugin shows markedly improved performance, particularly in the area of (uncached) directory scanning, due partly to more optimal directory reading and to a better sector cache algorithm. Uncached times tend to be better while there is a bit of a slowdown in dircache due to it being a bit heavier of an implementation. It's not noticeable by a human as far as I can say. Key core changes: 1) Files and directories share core code and data structures. 2) The filesystem code knows which descriptors refer to same file. This ensures that changes from one stream are appropriately reflected in every open descriptor for that file (fileobj_mgr.c). 3) File and directory cache buffers are borrowed from the main sector cache. This means that when they are not in use by a file, they are not wasted, but used for the cache. Most of the time, only a few of them are needed. It also means that adding more file and directory handles is less expensive. All one must do in ensure a large enough cache to borrow from. 4) Relative path components are supported and the namespace is unified. It does not support full relative paths to an implied current directory; what is does support is use of "." and "..". Adding the former would not be very difficult. The namespace is unified in the sense that volumes may be specified several times along with relative parts, e.g.: "/<0>/foo/../../<1>/bar" :<=> "/<1>/bar". 5) Stack usage is down due to sharing of data, static allocation and less duplication of strings on the stack. This requires more serialization than I would like but since the number of threads is limited to a low number, the tradoff in favor of the stack seems reasonable. 6) Separates and heirarchicalizes (sic) the SIM and APP filesystem code. SIM path and volume handling is just like the target. Some aspects of the APP file code get more straightforward (e.g. no path hashing is needed). Dircache: Deserves its own section. Dircache is new but pays homage to the old. The old one was not compatible and so it, since it got redone, does all the stuff it always should have done such as: 1) It may be update and used at any time during the build process. No longer has one to wait for it to finish building to do basic file management (create, remove, rename, etc.). 2) It does not need to be either fully scanned or completely disabled; it can be incomplete (i.e. overfilled, missing paths), still be of benefit and be correct. 3) Handles mounting and dismounting of individual volumes which means a full rebuild is not needed just because you pop a new SD card in the slot. Now, because it reuses its freed entry data, may rebuild only that volume. 4) Much more fundamental to the file code. When it is built, it is the keeper of the master file list whether enabled or not ("disabled" is just a state of the cache). Its must always to ready to be started and bind all streams opened prior to being enabled. 5) Maintains any short filenames in OEM format which means that it does not need to be rebuilt when changing the default codepage. Miscellaneous Compatibility: 1) Update any other code that would otherwise not work such as the hotswap mounting code in various card drivers. 2) File management: Clipboard needed updating because of the behavioral changes. Still needs a little more work on some finer points. 3) Remove now-obsolete functionality such as the mutex's "no preempt" flag (which was only for the prior FAT driver). 4) struct dirinfo uses time_t rather than raw FAT directory entry time fields. I plan to follow up on genericizing everything there (i.e. no FAT attributes). 5) unicode.c needed some redoing so that the file code does not try try to load codepages during a scan, which is actually a problem with the current code. The default codepage, if any is required, is now kept in RAM separarately (bufalloced) from codepages specified to iso_decode() (which must not be bufalloced because the conversion may be done by playback threads). Brings with it some additional reusable core code: 1) Revised file functions: Reusable code that does things such as safe path concatenation and parsing without buffer limitations or data duplication. Variants that copy or alter the input path may be based off these. To do: 1) Put dircache functionality back in the sim. Treating it internally as a different kind of file system seems the best approach at this time. 2) Restore use of dircache indexes in the playlist and database or something effectively the same. Since the cache doesn't have to be complete in order to be used, not getting a hit on the cache doesn't unambiguously say if the path exists or not. Change-Id: Ia30f3082a136253e3a0eae0784e3091d138915c8 Reviewed-on: http://gerrit.rockbox.org/566 Reviewed-by: Michael Sevakis <jethead71@rockbox.org> Tested: Michael Sevakis <jethead71@rockbox.org>
Diffstat (limited to 'firmware/target/arm')
-rw-r--r--firmware/target/arm/as3525/sd-as3525.c44
-rw-r--r--firmware/target/arm/as3525/sd-as3525v2.c41
-rw-r--r--firmware/target/arm/imx233/sdmmc-imx233.c45
-rw-r--r--firmware/target/arm/pp/ata-sd-pp.c30
-rw-r--r--firmware/target/arm/rk27xx/sd-rk27xx.c52
-rw-r--r--firmware/target/arm/s3c2440/sd-s3c2440.c34
-rw-r--r--firmware/target/arm/s5l8702/ipod6g/storage_ata-ipod6g.c27
-rw-r--r--firmware/target/arm/tcc780x/sd-tcc780x.c32
-rw-r--r--firmware/target/arm/tms320dm320/sdmmc-dm320.c35
9 files changed, 140 insertions, 200 deletions
diff --git a/firmware/target/arm/as3525/sd-as3525.c b/firmware/target/arm/as3525/sd-as3525.c
index c80c7f7491..ead40eac3c 100644
--- a/firmware/target/arm/as3525/sd-as3525.c
+++ b/firmware/target/arm/as3525/sd-as3525.c
@@ -449,21 +449,12 @@ static void sd_thread(void)
449 { 449 {
450#ifdef HAVE_HOTSWAP 450#ifdef HAVE_HOTSWAP
451 case SYS_HOTSWAP_INSERTED: 451 case SYS_HOTSWAP_INSERTED:
452 case SYS_HOTSWAP_EXTRACTED: 452 case SYS_HOTSWAP_EXTRACTED:;
453 { 453 int success = 1;
454 int microsd_init = 1;
455 fat_lock(); /* lock-out FAT activity first -
456 prevent deadlocking via disk_mount that
457 would cause a reverse-order attempt with
458 another thread */
459 mutex_lock(&sd_mtx); /* lock-out card activity - direct calls
460 into driver that bypass the fat cache */
461 454
462 /* We now have exclusive control of fat cache and ata */ 455 disk_unmount(SD_SLOT_AS3525); /* release "by force" */
463 456
464 disk_unmount(SD_SLOT_AS3525); /* release "by force", ensure file 457 mutex_lock(&sd_mtx); /* lock-out card activity */
465 descriptors aren't leaked and any busy
466 ones are invalid if mounting */
467 458
468 /* Force card init for new card, re-init for re-inserted one or 459 /* Force card init for new card, re-init for re-inserted one or
469 * clear if the last attempt to init failed with an error. */ 460 * clear if the last attempt to init failed with an error. */
@@ -471,29 +462,32 @@ static void sd_thread(void)
471 462
472 if (ev.id == SYS_HOTSWAP_INSERTED) 463 if (ev.id == SYS_HOTSWAP_INSERTED)
473 { 464 {
465 success = 0;
474 sd_enable(true); 466 sd_enable(true);
475 init_pl180_controller(SD_SLOT_AS3525); 467 init_pl180_controller(SD_SLOT_AS3525);
476 microsd_init = sd_init_card(SD_SLOT_AS3525); 468 int rc = sd_init_card(SD_SLOT_AS3525);
477 if (microsd_init < 0) /* initialisation failed */ 469 sd_enable(false);
478 panicf("microSD init failed : %d", microsd_init); 470 if (rc >= 0)
479 471 success = 2;
480 microsd_init = disk_mount(SD_SLOT_AS3525); /* 0 if fail */ 472 else /* initialisation failed */
473 panicf("microSD init failed : %d", rc);
481 } 474 }
482 475
476 mutex_unlock(&sd_mtx);
477
478 if (success > 1)
479 success = disk_mount(SD_SLOT_AS3525); /* 0 if fail */
480
483 /* 481 /*
484 * Mount succeeded, or this was an EXTRACTED event, 482 * Mount succeeded, or this was an EXTRACTED event,
485 * in both cases notify the system about the changed filesystems 483 * in both cases notify the system about the changed filesystems
486 */ 484 */
487 if (microsd_init) 485 if (success)
488 queue_broadcast(SYS_FS_CHANGED, 0); 486 queue_broadcast(SYS_FS_CHANGED, 0);
489 487
490 /* Access is now safe */
491 mutex_unlock(&sd_mtx);
492 fat_unlock();
493 sd_enable(false);
494 }
495 break; 488 break;
496#endif 489#endif /* HAVE_HOTSWAP */
490
497 case SYS_TIMEOUT: 491 case SYS_TIMEOUT:
498 if (TIME_BEFORE(current_tick, last_disk_activity+(3*HZ))) 492 if (TIME_BEFORE(current_tick, last_disk_activity+(3*HZ)))
499 { 493 {
diff --git a/firmware/target/arm/as3525/sd-as3525v2.c b/firmware/target/arm/as3525/sd-as3525v2.c
index ae3dde4495..b4ac40152b 100644
--- a/firmware/target/arm/as3525/sd-as3525v2.c
+++ b/firmware/target/arm/as3525/sd-as3525v2.c
@@ -598,21 +598,13 @@ static void sd_thread(void)
598 { 598 {
599#ifdef HAVE_HOTSWAP 599#ifdef HAVE_HOTSWAP
600 case SYS_HOTSWAP_INSERTED: 600 case SYS_HOTSWAP_INSERTED:
601 case SYS_HOTSWAP_EXTRACTED: 601 case SYS_HOTSWAP_EXTRACTED:;
602 { 602 int success = 1;
603 int changed = 1; 603
604 fat_lock(); /* lock-out FAT activity first - 604 disk_unmount(SD_SLOT_AS3525); /* release "by force" */
605 prevent deadlocking via disk_mount that 605
606 would cause a reverse-order attempt with 606 mutex_lock(&sd_mtx); /* lock-out card activity */
607 another thread */ 607
608 mutex_lock(&sd_mtx); /* lock-out card activity - direct calls
609 into driver that bypass the fat cache */
610
611 /* We now have exclusive control of fat cache and ata */
612
613 disk_unmount(SD_SLOT_AS3525); /* release "by force", ensure file
614 descriptors aren't leaked and any busy
615 ones are invalid if mounting */
616 /* Force card init for new card, re-init for re-inserted one or 608 /* Force card init for new card, re-init for re-inserted one or
617 * clear if the last attempt to init failed with an error. */ 609 * clear if the last attempt to init failed with an error. */
618 card_info[SD_SLOT_AS3525].initialized = 0; 610 card_info[SD_SLOT_AS3525].initialized = 0;
@@ -620,24 +612,25 @@ static void sd_thread(void)
620 if (ev.id == SYS_HOTSWAP_INSERTED) 612 if (ev.id == SYS_HOTSWAP_INSERTED)
621 { 613 {
622 sd_enable(true); 614 sd_enable(true);
623 changed = (sd_init_card(SD_SLOT_AS3525) == 0) && disk_mount(SD_SLOT_AS3525); /* 0 if fail */ 615 success = sd_init_card(SD_SLOT_AS3525) == 0 ? 2 : 0;
616 sd_enable(false);
624 } 617 }
625 618
619 mutex_unlock(&sd_mtx);
620
621 if (success > 1)
622 success = disk_mount(SD_SLOT_AS3525); /* 0 if fail */
623
626 /* 624 /*
627 * Mount succeeded, or this was an EXTRACTED event, 625 * Mount succeeded, or this was an EXTRACTED event,
628 * in both cases notify the system about the changed filesystems 626 * in both cases notify the system about the changed filesystems
629 */ 627 */
630 if (changed) 628 if (success)
631 queue_broadcast(SYS_FS_CHANGED, 0); 629 queue_broadcast(SYS_FS_CHANGED, 0);
632 630
633 sd_enable(false);
634
635 /* Access is now safe */
636 mutex_unlock(&sd_mtx);
637 fat_unlock();
638 }
639 break; 631 break;
640#endif 632#endif /* HAVE_HOTSWAP */
633
641 case SYS_TIMEOUT: 634 case SYS_TIMEOUT:
642 if (TIME_BEFORE(current_tick, last_disk_activity+(3*HZ))) 635 if (TIME_BEFORE(current_tick, last_disk_activity+(3*HZ)))
643 { 636 {
diff --git a/firmware/target/arm/imx233/sdmmc-imx233.c b/firmware/target/arm/imx233/sdmmc-imx233.c
index 8f293543ab..87548aef53 100644
--- a/firmware/target/arm/imx233/sdmmc-imx233.c
+++ b/firmware/target/arm/imx233/sdmmc-imx233.c
@@ -766,14 +766,7 @@ static void sdmmc_thread(void)
766 case SYS_HOTSWAP_INSERTED: 766 case SYS_HOTSWAP_INSERTED:
767 case SYS_HOTSWAP_EXTRACTED: 767 case SYS_HOTSWAP_EXTRACTED:
768 { 768 {
769 int microsd_init = 1; 769 int microsd_init = ev.id == SYS_HOTSWAP_INSERTED ? 0 : 1;
770 /* lock-out FAT activity first -
771 * prevent deadlocking via disk_mount that
772 * would cause a reverse-order attempt with
773 * another thread */
774#ifdef HAVE_HOTSWAP
775 fat_lock();
776#endif
777 770
778 /* We now have exclusive control of fat cache and sd. 771 /* We now have exclusive control of fat cache and sd.
779 * Release "by force", ensure file 772 * Release "by force", ensure file
@@ -785,35 +778,37 @@ static void sdmmc_thread(void)
785 /* Skip non-removable drivers */ 778 /* Skip non-removable drivers */
786 if(!sdmmc_removable(drive)) 779 if(!sdmmc_removable(drive))
787 continue; 780 continue;
788 /* lock-out card activity - direct calls 781
789 * into driver that bypass the fat cache */
790 mutex_lock(&mutex[drive]);
791 disk_unmount(sd_first_drive + sd_drive); 782 disk_unmount(sd_first_drive + sd_drive);
783
784 mutex_lock(&mutex[drive]); /* lock-out card activity */
785
792 /* Force card init for new card, re-init for re-inserted one or 786 /* Force card init for new card, re-init for re-inserted one or
793 * clear if the last attempt to init failed with an error. */ 787 * clear if the last attempt to init failed with an error. */
794 SDMMC_INFO(sd_map[sd_drive]).initialized = 0; 788 SDMMC_INFO(sd_map[sd_drive]).initialized = 0;
795 789
790 int rc = -1;
796 if(ev.id == SYS_HOTSWAP_INSERTED) 791 if(ev.id == SYS_HOTSWAP_INSERTED)
797 { 792 {
798 microsd_init = init_drive(drive); 793 rc = init_drive(drive);
799 if(microsd_init < 0) /* initialisation failed */ 794 if(rc < 0) /* initialisation failed */
800 panicf("%s init failed : %d", SDMMC_CONF(sd_map[sd_drive]).name, microsd_init); 795 panicf("%s init failed : %d", SDMMC_CONF(sd_map[sd_drive]).name, rc);
801
802 microsd_init = disk_mount(sd_first_drive + sd_drive); /* 0 if fail */
803 } 796 }
804 /* 797
805 * Mount succeeded, or this was an EXTRACTED event,
806 * in both cases notify the system about the changed filesystems
807 */
808 if(microsd_init)
809 queue_broadcast(SYS_FS_CHANGED, 0);
810 /* unlock card */ 798 /* unlock card */
811 mutex_unlock(&mutex[drive]); 799 mutex_unlock(&mutex[drive]);
800
801 if (rc >= 0)
802 microsd_init += disk_mount(sd_first_drive + sd_drive); /* 0 if fail */
812 } 803 }
813 /* Access is now safe */ 804 /* Access is now safe */
814#ifdef HAVE_HOTSWAP 805 /*
815 fat_unlock(); 806 * One or more mounts succeeded, or this was an EXTRACTED event,
816#endif 807 * in both cases notify the system about the changed filesystems
808 */
809 if(microsd_init)
810 queue_broadcast(SYS_FS_CHANGED, 0);
811
817 break; 812 break;
818 } 813 }
819#endif 814#endif
diff --git a/firmware/target/arm/pp/ata-sd-pp.c b/firmware/target/arm/pp/ata-sd-pp.c
index bcf8a660c2..2a11b40fee 100644
--- a/firmware/target/arm/pp/ata-sd-pp.c
+++ b/firmware/target/arm/pp/ata-sd-pp.c
@@ -19,7 +19,6 @@
19 * 19 *
20 ****************************************************************************/ 20 ****************************************************************************/
21#include "config.h" /* for HAVE_MULTIDRIVE */ 21#include "config.h" /* for HAVE_MULTIDRIVE */
22#include "fat.h"
23#include "sdmmc.h" 22#include "sdmmc.h"
24#include "gcc_extensions.h" 23#include "gcc_extensions.h"
25#ifdef HAVE_HOTSWAP 24#ifdef HAVE_HOTSWAP
@@ -1125,35 +1124,28 @@ static void sd_thread(void)
1125 { 1124 {
1126#ifdef HAVE_HOTSWAP 1125#ifdef HAVE_HOTSWAP
1127 case SYS_HOTSWAP_INSERTED: 1126 case SYS_HOTSWAP_INSERTED:
1128 case SYS_HOTSWAP_EXTRACTED: 1127 case SYS_HOTSWAP_EXTRACTED:;
1129 fat_lock(); /* lock-out FAT activity first - 1128 int success = 1;
1130 prevent deadlocking via disk_mount that
1131 would cause a reverse-order attempt with
1132 another thread */
1133 mutex_lock(&sd_mtx); /* lock-out card activity - direct calls
1134 into driver that bypass the fat cache */
1135 1129
1136 /* We now have exclusive control of fat cache and ata */ 1130 disk_unmount(sd_first_drive+1); /* release "by force" */
1137 1131
1138 disk_unmount(sd_first_drive+1); /* release "by force", ensure file 1132 mutex_lock(&sd_mtx); /* lock-out card activity */
1139 descriptors aren't leaked and any busy
1140 ones are invalid if mounting */
1141 1133
1142 /* Force card init for new card, re-init for re-inserted one or 1134 /* Force card init for new card, re-init for re-inserted one or
1143 * clear if the last attempt to init failed with an error. */ 1135 * clear if the last attempt to init failed with an error. */
1144 card_info[1].initialized = 0; 1136 card_info[1].initialized = 0;
1145 sd_status[1].retry = 0; 1137 sd_status[1].retry = 0;
1146 1138
1147 if (ev.id == SYS_HOTSWAP_INSERTED)
1148 disk_mount(sd_first_drive+1);
1149
1150 queue_broadcast(SYS_FS_CHANGED, 0);
1151
1152 /* Access is now safe */ 1139 /* Access is now safe */
1153 mutex_unlock(&sd_mtx); 1140 mutex_unlock(&sd_mtx);
1154 fat_unlock(); 1141
1142 if (ev.id == SYS_HOTSWAP_INSERTED)
1143 success = disk_mount(sd_first_drive+1); /* 0 if fail */
1144
1145 if (success)
1146 queue_broadcast(SYS_FS_CHANGED, 0);
1155 break; 1147 break;
1156#endif 1148#endif /* HAVE_HOTSWAP */
1157 case SYS_TIMEOUT: 1149 case SYS_TIMEOUT:
1158 if (TIME_BEFORE(current_tick, last_disk_activity+(3*HZ))) 1150 if (TIME_BEFORE(current_tick, last_disk_activity+(3*HZ)))
1159 { 1151 {
diff --git a/firmware/target/arm/rk27xx/sd-rk27xx.c b/firmware/target/arm/rk27xx/sd-rk27xx.c
index deca8a1fa7..9d6821ee38 100644
--- a/firmware/target/arm/rk27xx/sd-rk27xx.c
+++ b/firmware/target/arm/rk27xx/sd-rk27xx.c
@@ -22,7 +22,6 @@
22 ****************************************************************************/ 22 ****************************************************************************/
23 23
24#include "config.h" /* for HAVE_MULTIVOLUME */ 24#include "config.h" /* for HAVE_MULTIVOLUME */
25#include "fat.h"
26#include "thread.h" 25#include "thread.h"
27#include "gcc_extensions.h" 26#include "gcc_extensions.h"
28#include "led.h" 27#include "led.h"
@@ -331,50 +330,45 @@ static void sd_thread(void)
331 { 330 {
332#ifdef HAVE_HOTSWAP 331#ifdef HAVE_HOTSWAP
333 case SYS_HOTSWAP_INSERTED: 332 case SYS_HOTSWAP_INSERTED:
334 case SYS_HOTSWAP_EXTRACTED: 333 case SYS_HOTSWAP_EXTRACTED:;
335 { 334 int success = 1;
336 int microsd_init = 1; 335
337 fat_lock(); /* lock-out FAT activity first - 336 disk_unmount(sd_first_drive); /* release "by force" */
338 prevent deadlocking via disk_mount that 337
339 would cause a reverse-order attempt with 338 mutex_lock(&sd_mtx); /* lock-out card activity */
340 another thread */ 339
341 mutex_lock(&sd_mtx); /* lock-out card activity - direct calls
342 into driver that bypass the fat cache */
343
344 /* We now have exclusive control of fat cache and ata */
345
346 disk_unmount(sd_first_drive); /* release "by force", ensure file
347 descriptors aren't leaked and any busy
348 ones are invalid if mounting */
349 /* Force card init for new card, re-init for re-inserted one or 340 /* Force card init for new card, re-init for re-inserted one or
350 * clear if the last attempt to init failed with an error. */ 341 * clear if the last attempt to init failed with an error. */
351 card_info.initialized = 0; 342 card_info.initialized = 0;
352 343
353 if (ev.id == SYS_HOTSWAP_INSERTED) 344 if (ev.id == SYS_HOTSWAP_INSERTED)
354 { 345 {
346 success = 0;
355 sd_enable(true); 347 sd_enable(true);
356 microsd_init = sd_init_card(sd_first_drive); 348 int rc = sd_init_card(sd_first_drive);
357 if (microsd_init < 0) /* initialisation failed */ 349 sd_enable(false);
358 panicf("microSD init failed : %d", microsd_init); 350 if (rc >= 0)
359 351 success = 2;
360 microsd_init = disk_mount(sd_first_drive); /* 0 if fail */ 352 else /* initialisation failed */
353 panicf("microSD init failed : %d", rc);
361 } 354 }
362 355
356 /* Access is now safe */
357 mutex_unlock(&sd_mtx);
358
359 if (success > 1)
360 success = disk_mount(sd_first_drive); /* 0 if fail */
361
363 /* 362 /*
364 * Mount succeeded, or this was an EXTRACTED event, 363 * Mount succeeded, or this was an EXTRACTED event,
365 * in both cases notify the system about the changed filesystems 364 * in both cases notify the system about the changed filesystems
366 */ 365 */
367 if (microsd_init) 366 if (success)
368 queue_broadcast(SYS_FS_CHANGED, 0); 367 queue_broadcast(SYS_FS_CHANGED, 0);
369 368
370 sd_enable(false);
371
372 /* Access is now safe */
373 mutex_unlock(&sd_mtx);
374 fat_unlock();
375 }
376 break; 369 break;
377#endif 370#endif /* HAVE_HOTSWAP */
371
378 case SYS_TIMEOUT: 372 case SYS_TIMEOUT:
379 if (TIME_BEFORE(current_tick, last_disk_activity+(3*HZ))) 373 if (TIME_BEFORE(current_tick, last_disk_activity+(3*HZ)))
380 { 374 {
diff --git a/firmware/target/arm/s3c2440/sd-s3c2440.c b/firmware/target/arm/s3c2440/sd-s3c2440.c
index 6658fa1515..e8de3ac78d 100644
--- a/firmware/target/arm/s3c2440/sd-s3c2440.c
+++ b/firmware/target/arm/s3c2440/sd-s3c2440.c
@@ -34,7 +34,6 @@
34#ifdef HAVE_HOTSWAP 34#ifdef HAVE_HOTSWAP
35#include "sdmmc.h" 35#include "sdmmc.h"
36#include "disk.h" 36#include "disk.h"
37#include "fat.h"
38#endif 37#endif
39#include "dma-target.h" 38#include "dma-target.h"
40#include "system-target.h" 39#include "system-target.h"
@@ -585,48 +584,29 @@ static void sd_thread(void)
585 { 584 {
586#ifdef HAVE_HOTSWAP 585#ifdef HAVE_HOTSWAP
587 case SYS_HOTSWAP_INSERTED: 586 case SYS_HOTSWAP_INSERTED:
588 case SYS_HOTSWAP_EXTRACTED: 587 case SYS_HOTSWAP_EXTRACTED:;
589 {
590 int success = 1; 588 int success = 1;
591 fat_lock(); /* lock-out FAT activity first -
592 prevent deadlocking via disk_mount that
593 would cause a reverse-order attempt with
594 another thread */
595 mutex_lock(&sd_mtx); /* lock-out card activity - direct calls
596 into driver that bypass the fat cache */
597 589
598 /* We now have exclusive control of fat cache and ata */ 590 disk_unmount(0); /* release "by force" */
599 591
600 disk_unmount(0); /* release "by force", ensure file 592 mutex_lock(&sd_mtx); /* lock-out card activity */
601 descriptors aren't leaked and any busy
602 ones are invalid if mounting */
603 593
604 /* Force card init for new card, re-init for re-inserted one or 594 /* Force card init for new card, re-init for re-inserted one or
605 * clear if the last attempt to init failed with an error. */ 595 * clear if the last attempt to init failed with an error. */
606 card_info[0].initialized = 0; 596 card_info[0].initialized = 0;
607 597
598 /* Access is now safe */
599 mutex_unlock(&sd_mtx);
600
608 if (ev.id == SYS_HOTSWAP_INSERTED) 601 if (ev.id == SYS_HOTSWAP_INSERTED)
609 {
610 /* FIXME: once sd_enabled is implement properly,
611 * reinitializing the controllers might be needed */
612 sd_enable(true);
613 if (success < 0) /* initialisation failed */
614 panicf("SD init failed : %d", success);
615 success = disk_mount(0); /* 0 if fail */ 602 success = disk_mount(0); /* 0 if fail */
616 }
617 603
618 /* notify the system about the changed filesystems 604 /* notify the system about the changed filesystems
619 */ 605 */
620 if (success) 606 if (success)
621 queue_broadcast(SYS_FS_CHANGED, 0); 607 queue_broadcast(SYS_FS_CHANGED, 0);
622
623 /* Access is now safe */
624 mutex_unlock(&sd_mtx);
625 fat_unlock();
626 sd_enable(false);
627 }
628 break; 608 break;
629#endif 609#endif /* HAVE_HOTSWAP */
630 } 610 }
631 } 611 }
632} 612}
diff --git a/firmware/target/arm/s5l8702/ipod6g/storage_ata-ipod6g.c b/firmware/target/arm/s5l8702/ipod6g/storage_ata-ipod6g.c
index 395c0f49e6..0fd74787d1 100644
--- a/firmware/target/arm/s5l8702/ipod6g/storage_ata-ipod6g.c
+++ b/firmware/target/arm/s5l8702/ipod6g/storage_ata-ipod6g.c
@@ -32,7 +32,7 @@
32#include "s5l8702.h" 32#include "s5l8702.h"
33#include "led.h" 33#include "led.h"
34#include "ata_idle_notify.h" 34#include "ata_idle_notify.h"
35#include "fat.h" 35#include "disk_cache.h"
36#include "splash.h" 36#include "splash.h"
37 37
38 38
@@ -68,6 +68,7 @@ static struct semaphore mmc_wakeup;
68static struct semaphore mmc_comp_wakeup; 68static struct semaphore mmc_comp_wakeup;
69static int spinup_time = 0; 69static int spinup_time = 0;
70static int dma_mode = 0; 70static int dma_mode = 0;
71static char aligned_buffer[SECTOR_SIZE] __attribute__((aligned(0x10)));
71 72
72 73
73#ifdef ATA_HAVE_BBT 74#ifdef ATA_HAVE_BBT
@@ -857,8 +858,25 @@ int ata_bbt_translate(uint64_t sector, uint32_t count, uint64_t* phys, uint32_t*
857static int ata_rw_sectors(uint64_t sector, uint32_t count, void* buffer, bool write) 858static int ata_rw_sectors(uint64_t sector, uint32_t count, void* buffer, bool write)
858{ 859{
859 if (((uint32_t)buffer) & 0xf) 860 if (((uint32_t)buffer) & 0xf)
860 panicf("ATA: Misaligned data buffer at %08X (sector %lu, count %lu)", 861 {
861 (unsigned int)buffer, (long unsigned int)sector, (long unsigned int)count); 862 while (count)
863 {
864 if (write)
865 memcpy(aligned_buffer, buffer, SECTOR_SIZE);
866
867 PASS_RC(ata_rw_sectors(sector, 1, aligned_buffer, write), 0, 0);
868
869 if (!write)
870 memcpy(buffer, aligned_buffer, SECTOR_SIZE);
871
872 buffer += SECTOR_SIZE;
873 sector++;
874 count--;
875 }
876
877 return 0;
878 }
879
862#ifdef ATA_HAVE_BBT 880#ifdef ATA_HAVE_BBT
863 if (sector + count > ata_virtual_sectors) RET_ERR(0); 881 if (sector + count > ata_virtual_sectors) RET_ERR(0);
864 if (ata_bbt) 882 if (ata_bbt)
@@ -1117,14 +1135,13 @@ int ata_init(void)
1117 -- Michael Sparmann (theseven), 2011-10-22 */ 1135 -- Michael Sparmann (theseven), 2011-10-22 */
1118 if (!ceata) 1136 if (!ceata)
1119 { 1137 {
1120 unsigned char* sector = fat_get_sector_buffer(); 1138 unsigned char* sector = aligned_buffer;
1121 ata_rw_sectors(0, 1, sector, false); 1139 ata_rw_sectors(0, 1, sector, false);
1122 if (sector[510] == 0xaa && sector[511] == 0x55) 1140 if (sector[510] == 0xaa && sector[511] == 0x55)
1123 { 1141 {
1124 ata_swap = true; 1142 ata_swap = true;
1125 splashf(5000, "Wrong HDD endianness, please update your emCORE version!"); 1143 splashf(5000, "Wrong HDD endianness, please update your emCORE version!");
1126 } 1144 }
1127 fat_release_sector_buffer();
1128 } 1145 }
1129 1146
1130 create_thread(ata_thread, ata_stack, 1147 create_thread(ata_thread, ata_stack,
diff --git a/firmware/target/arm/tcc780x/sd-tcc780x.c b/firmware/target/arm/tcc780x/sd-tcc780x.c
index 55ae4e7c70..b7abea8be4 100644
--- a/firmware/target/arm/tcc780x/sd-tcc780x.c
+++ b/firmware/target/arm/tcc780x/sd-tcc780x.c
@@ -28,7 +28,6 @@
28#include "led.h" 28#include "led.h"
29#include "thread.h" 29#include "thread.h"
30#include "disk.h" 30#include "disk.h"
31#include "fat.h"
32#include "ata_idle_notify.h" 31#include "ata_idle_notify.h"
33#include "usb.h" 32#include "usb.h"
34 33
@@ -657,35 +656,30 @@ static void sd_thread(void)
657 { 656 {
658#ifdef HAVE_HOTSWAP 657#ifdef HAVE_HOTSWAP
659 case SYS_HOTSWAP_INSERTED: 658 case SYS_HOTSWAP_INSERTED:
660 case SYS_HOTSWAP_EXTRACTED: 659 case SYS_HOTSWAP_EXTRACTED:;
661 fat_lock(); /* lock-out FAT activity first - 660 int success = 1;
662 prevent deadlocking via disk_mount that 661
663 would cause a reverse-order attempt with 662 /* Release "by force" */
664 another thread */
665 mutex_lock(&sd_mtx); /* lock-out card activity - direct calls
666 into driver that bypass the fat cache */
667
668 /* We now have exclusive control of fat cache and ata */
669
670 /* Release "by force", ensure file descriptors aren't leaked and
671 any busy ones are invalid if mounting */
672 disk_unmount(sd_first_drive + CARD_NUM_SLOT); 663 disk_unmount(sd_first_drive + CARD_NUM_SLOT);
673 664
665 mutex_lock(&sd_mtx); /* lock-out card activity */
666
674 /* Force card init for new card, re-init for re-inserted one or 667 /* Force card init for new card, re-init for re-inserted one or
675 * clear if the last attempt to init failed with an error. */ 668 * clear if the last attempt to init failed with an error. */
676 card_info[CARD_NUM_SLOT].initialized = 0; 669 card_info[CARD_NUM_SLOT].initialized = 0;
677 sd_status[CARD_NUM_SLOT].retry = 0; 670 sd_status[CARD_NUM_SLOT].retry = 0;
678 671
672 mutex_unlock(&sd_mtx);
673
679 if (ev.id == SYS_HOTSWAP_INSERTED) 674 if (ev.id == SYS_HOTSWAP_INSERTED)
680 disk_mount(sd_first_drive + CARD_NUM_SLOT); 675 success = disk_mount(sd_first_drive + CARD_NUM_SLOT);
681 676
682 queue_broadcast(SYS_FS_CHANGED, 0); 677 if (success)
678 queue_broadcast(SYS_FS_CHANGED, 0);
683 679
684 /* Access is now safe */
685 mutex_unlock(&sd_mtx);
686 fat_unlock();
687 break; 680 break;
688#endif 681#endif /* HAVE_HOTSWAP */
682
689 case SYS_TIMEOUT: 683 case SYS_TIMEOUT:
690 if (TIME_BEFORE(current_tick, last_disk_activity+(3*HZ))) 684 if (TIME_BEFORE(current_tick, last_disk_activity+(3*HZ)))
691 { 685 {
diff --git a/firmware/target/arm/tms320dm320/sdmmc-dm320.c b/firmware/target/arm/tms320dm320/sdmmc-dm320.c
index 284061e1ad..d46dbf5e40 100644
--- a/firmware/target/arm/tms320dm320/sdmmc-dm320.c
+++ b/firmware/target/arm/tms320dm320/sdmmc-dm320.c
@@ -592,48 +592,29 @@ static void sd_thread(void)
592 { 592 {
593#ifdef HAVE_HOTSWAP 593#ifdef HAVE_HOTSWAP
594 case SYS_HOTSWAP_INSERTED: 594 case SYS_HOTSWAP_INSERTED:
595 case SYS_HOTSWAP_EXTRACTED: 595 case SYS_HOTSWAP_EXTRACTED:;
596 {
597 int success = 1; 596 int success = 1;
598 fat_lock(); /* lock-out FAT activity first -
599 prevent deadlocking via disk_mount that
600 would cause a reverse-order attempt with
601 another thread */
602 mutex_lock(&sd_mtx); /* lock-out card activity - direct calls
603 into driver that bypass the fat cache */
604 597
605 /* We now have exclusive control of fat cache and ata */ 598 disk_unmount(0); /* release "by force" */
606 599
607 disk_unmount(0); /* release "by force", ensure file 600 mutex_lock(&sd_mtx); /* lock-out card activity */
608 descriptors aren't leaked and any busy
609 ones are invalid if mounting */
610 601
611 /* Force card init for new card, re-init for re-inserted one or 602 /* Force card init for new card, re-init for re-inserted one or
612 * clear if the last attempt to init failed with an error. */ 603 * clear if the last attempt to init failed with an error. */
613 card_info[0].initialized = 0; 604 card_info[0].initialized = 0;
614 605
606 mutex_unlock(&sd_mtx);
607
615 if (ev.id == SYS_HOTSWAP_INSERTED) 608 if (ev.id == SYS_HOTSWAP_INSERTED)
616 {
617 /* FIXME: once sd_enabled is implement properly,
618 * reinitializing the controllers might be needed */
619 sd_enable(true);
620 if (success < 0) /* initialisation failed */
621 panicf("SD init failed : %d", success);
622 success = disk_mount(0); /* 0 if fail */ 609 success = disk_mount(0); /* 0 if fail */
623 }
624 610
625 /* notify the system about the changed filesystems 611 /* notify the system about the changed filesystems */
626 */
627 if (success) 612 if (success)
628 queue_broadcast(SYS_FS_CHANGED, 0); 613 queue_broadcast(SYS_FS_CHANGED, 0);
629 614
630 /* Access is now safe */
631 mutex_unlock(&sd_mtx);
632 fat_unlock();
633 sd_enable(false);
634 }
635 break; 615 break;
636#endif 616#endif /* HAVE_HOTSWAP */
617
637 case SYS_TIMEOUT: 618 case SYS_TIMEOUT:
638 if (TIME_BEFORE(current_tick, last_disk_activity+(3*HZ))) 619 if (TIME_BEFORE(current_tick, last_disk_activity+(3*HZ)))
639 { 620 {