exfat_oal.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* Some of the source code in this file came from "linux/fs/fat/misc.c". */
  2. /*
  3. * linux/fs/fat/misc.c
  4. *
  5. * Written 1992,1993 by Werner Almesberger
  6. * 22/11/2000 - Fixed fat_date_unix2dos for dates earlier than 01/01/1980
  7. * and date_dos2unix for date==0 by Igor Zhbanov(bsg@uniyar.ac.ru)
  8. */
  9. /*
  10. * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version 2
  15. * of the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  25. */
  26. #include <linux/semaphore.h>
  27. #include <linux/time.h>
  28. #include "exfat_config.h"
  29. #include "exfat_global.h"
  30. #include "exfat_api.h"
  31. #include "exfat_oal.h"
  32. DECLARE_MUTEX(z_sem);
  33. INT32 sm_init(struct semaphore *sm)
  34. {
  35. sema_init(sm, 1);
  36. return(0);
  37. }
  38. INT32 sm_P(struct semaphore *sm)
  39. {
  40. down(sm);
  41. return 0;
  42. }
  43. void sm_V(struct semaphore *sm)
  44. {
  45. up(sm);
  46. }
  47. extern struct timezone sys_tz;
  48. #define UNIX_SECS_1980 315532800L
  49. #if BITS_PER_LONG == 64
  50. #define UNIX_SECS_2108 4354819200L
  51. #endif
  52. #define DAYS_DELTA_DECADE (365 * 10 + 2)
  53. #define NO_LEAP_YEAR_2100 (120)
  54. #define IS_LEAP_YEAR(y) (!((y) & 3) && (y) != NO_LEAP_YEAR_2100)
  55. #define SECS_PER_MIN (60)
  56. #define SECS_PER_HOUR (60 * SECS_PER_MIN)
  57. #define SECS_PER_DAY (24 * SECS_PER_HOUR)
  58. #define MAKE_LEAP_YEAR(leap_year, year) \
  59. do { \
  60. if (unlikely(year > NO_LEAP_YEAR_2100)) \
  61. leap_year = ((year + 3) / 4) - 1; \
  62. else \
  63. leap_year = ((year + 3) / 4); \
  64. } while(0)
  65. static time_t accum_days_in_year[] = {
  66. 0, 0, 31, 59, 90,120,151,181,212,243,273,304,334, 0, 0, 0,
  67. };
  68. TIMESTAMP_T *tm_current(TIMESTAMP_T *tp, UINT8 tz_utc)
  69. {
  70. struct timespec ts = CURRENT_TIME_SEC;
  71. time_t second = ts.tv_sec;
  72. time_t day, leap_day, month, year;
  73. if (!tz_utc)
  74. second -= sys_tz.tz_minuteswest * SECS_PER_MIN;
  75. if (second < UNIX_SECS_1980) {
  76. tp->sec = 0;
  77. tp->min = 0;
  78. tp->hour = 0;
  79. tp->day = 1;
  80. tp->mon = 1;
  81. tp->year = 0;
  82. return(tp);
  83. }
  84. #if BITS_PER_LONG == 64
  85. if (second >= UNIX_SECS_2108) {
  86. tp->sec = 59;
  87. tp->min = 59;
  88. tp->hour = 23;
  89. tp->day = 31;
  90. tp->mon = 12;
  91. tp->year = 127;
  92. return(tp);
  93. }
  94. #endif
  95. day = second / SECS_PER_DAY - DAYS_DELTA_DECADE;
  96. year = day / 365;
  97. MAKE_LEAP_YEAR(leap_day, year);
  98. if (year * 365 + leap_day > day)
  99. year--;
  100. MAKE_LEAP_YEAR(leap_day, year);
  101. day -= year * 365 + leap_day;
  102. if (IS_LEAP_YEAR(year) && day == accum_days_in_year[3]) {
  103. month = 2;
  104. } else {
  105. if (IS_LEAP_YEAR(year) && day > accum_days_in_year[3])
  106. day--;
  107. for (month = 1; month < 12; month++) {
  108. if (accum_days_in_year[month + 1] > day)
  109. break;
  110. }
  111. }
  112. day -= accum_days_in_year[month];
  113. tp->sec = second % SECS_PER_MIN;
  114. tp->min = (second / SECS_PER_MIN) % 60;
  115. tp->hour = (second / SECS_PER_HOUR) % 24;
  116. tp->day = day + 1;
  117. tp->mon = month;
  118. tp->year = year;
  119. return(tp);
  120. }