hp_sdc_rtc.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:20k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * HP i8042 SDC + MSM-58321 BBRTC driver.
  3.  *
  4.  * Copyright (c) 2001 Brian S. Julin
  5.  * All rights reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions
  9.  * are met:
  10.  * 1. Redistributions of source code must retain the above copyright
  11.  *    notice, this list of conditions, and the following disclaimer,
  12.  *    without modification.
  13.  * 2. The name of the author may not be used to endorse or promote products
  14.  *    derived from this software without specific prior written permission.
  15.  *
  16.  * Alternatively, this software may be distributed under the terms of the
  17.  * GNU General Public License ("GPL").
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  20.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22.  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
  23.  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28.  *
  29.  * References:
  30.  * System Device Controller Microprocessor Firmware Theory of Operation
  31.  *      for Part Number 1820-4784 Revision B.  Dwg No. A-1820-4784-2
  32.  * efirtc.c by Stephane Eranian/Hewlett Packard
  33.  *
  34.  */
  35. #include <linux/hp_sdc.h>
  36. #include <linux/errno.h>
  37. #include <linux/types.h>
  38. #include <linux/init.h>
  39. #include <linux/module.h>
  40. #include <linux/time.h>
  41. #include <linux/miscdevice.h>
  42. #include <linux/proc_fs.h>
  43. #include <linux/poll.h>
  44. #include <linux/rtc.h>
  45. MODULE_AUTHOR("Brian S. Julin <bri@calyx.com>");
  46. MODULE_DESCRIPTION("HP i8042 SDC + MSM-58321 RTC Driver");
  47. MODULE_LICENSE("Dual BSD/GPL");
  48. #define RTC_VERSION "1.10d"
  49. static unsigned long epoch = 2000;
  50. static struct semaphore i8042tregs;
  51. static hp_sdc_irqhook hp_sdc_rtc_isr;
  52. static struct fasync_struct *hp_sdc_rtc_async_queue;
  53. static DECLARE_WAIT_QUEUE_HEAD(hp_sdc_rtc_wait);
  54. static loff_t hp_sdc_rtc_llseek(struct file *file, loff_t offset, int origin);
  55. static ssize_t hp_sdc_rtc_read(struct file *file, char *buf,
  56.        size_t count, loff_t *ppos);
  57. static int hp_sdc_rtc_ioctl(struct inode *inode, struct file *file,
  58.     unsigned int cmd, unsigned long arg);
  59. static unsigned int hp_sdc_rtc_poll(struct file *file, poll_table *wait);
  60. static int hp_sdc_rtc_open(struct inode *inode, struct file *file);
  61. static int hp_sdc_rtc_release(struct inode *inode, struct file *file);
  62. static int hp_sdc_rtc_fasync (int fd, struct file *filp, int on);
  63. static int hp_sdc_rtc_read_proc(char *page, char **start, off_t off,
  64. int count, int *eof, void *data);
  65. static void hp_sdc_rtc_isr (int irq, void *dev_id, 
  66.     uint8_t status, uint8_t data) 
  67. {
  68. return;
  69. }
  70. static int hp_sdc_rtc_do_read_bbrtc (struct rtc_time *rtctm)
  71. {
  72. struct semaphore tsem;
  73. hp_sdc_transaction t;
  74. uint8_t tseq[91];
  75. int i;
  76. i = 0;
  77. while (i < 91) {
  78. tseq[i++] = HP_SDC_ACT_DATAREG |
  79. HP_SDC_ACT_POSTCMD | HP_SDC_ACT_DATAIN;
  80. tseq[i++] = 0x01; /* write i8042[0x70] */
  81.    tseq[i]   = i / 7; /* BBRTC reg address */
  82. i++;
  83. tseq[i++] = HP_SDC_CMD_DO_RTCR; /* Trigger command   */
  84. tseq[i++] = 2; /* expect 1 stat/dat pair back.   */
  85. i++; i++;               /* buffer for stat/dat pair       */
  86. }
  87. tseq[84] |= HP_SDC_ACT_SEMAPHORE;
  88. t.endidx = 91;
  89. t.seq = tseq;
  90. t.act.semaphore = &tsem;
  91. init_MUTEX_LOCKED(&tsem);
  92. if (hp_sdc_enqueue_transaction(&t)) return -1;
  93. down_interruptible(&tsem);  /* Put ourselves to sleep for results. */
  94. /* Check for nonpresence of BBRTC */
  95. if (!((tseq[83] | tseq[90] | tseq[69] | tseq[76] |
  96.        tseq[55] | tseq[62] | tseq[34] | tseq[41] |
  97.        tseq[20] | tseq[27] | tseq[6]  | tseq[13]) & 0x0f))
  98. return -1;
  99. memset(rtctm, 0, sizeof(struct rtc_time));
  100. rtctm->tm_year = (tseq[83] & 0x0f) + (tseq[90] & 0x0f) * 10;
  101. rtctm->tm_mon  = (tseq[69] & 0x0f) + (tseq[76] & 0x0f) * 10;
  102. rtctm->tm_mday = (tseq[55] & 0x0f) + (tseq[62] & 0x0f) * 10;
  103. rtctm->tm_wday = (tseq[48] & 0x0f);
  104. rtctm->tm_hour = (tseq[34] & 0x0f) + (tseq[41] & 0x0f) * 10;
  105. rtctm->tm_min  = (tseq[20] & 0x0f) + (tseq[27] & 0x0f) * 10;
  106. rtctm->tm_sec  = (tseq[6]  & 0x0f) + (tseq[13] & 0x0f) * 10;
  107. return 0;
  108. }
  109. static int hp_sdc_rtc_read_bbrtc (struct rtc_time *rtctm)
  110. {
  111. struct rtc_time tm, tm_last;
  112. int i = 0;
  113. /* MSM-58321 has no read latch, so must read twice and compare. */
  114. if (hp_sdc_rtc_do_read_bbrtc(&tm_last)) return -1;
  115. if (hp_sdc_rtc_do_read_bbrtc(&tm)) return -1;
  116. while (memcmp(&tm, &tm_last, sizeof(struct rtc_time))) {
  117. if (i++ > 4) return -1;
  118. memcpy(&tm_last, &tm, sizeof(struct rtc_time));
  119. if (hp_sdc_rtc_do_read_bbrtc(&tm)) return -1;
  120. }
  121. memcpy(rtctm, &tm, sizeof(struct rtc_time));
  122. return 0;
  123. }
  124. static int64_t hp_sdc_rtc_read_i8042timer (uint8_t loadcmd, int numreg)
  125. {
  126. hp_sdc_transaction t;
  127. uint8_t tseq[26] = {
  128. HP_SDC_ACT_PRECMD | HP_SDC_ACT_POSTCMD | HP_SDC_ACT_DATAIN,
  129. 0,
  130. HP_SDC_CMD_READ_T1, 2, 0, 0,
  131. HP_SDC_ACT_POSTCMD | HP_SDC_ACT_DATAIN, 
  132. HP_SDC_CMD_READ_T2, 2, 0, 0,
  133. HP_SDC_ACT_POSTCMD | HP_SDC_ACT_DATAIN, 
  134. HP_SDC_CMD_READ_T3, 2, 0, 0,
  135. HP_SDC_ACT_POSTCMD | HP_SDC_ACT_DATAIN, 
  136. HP_SDC_CMD_READ_T4, 2, 0, 0,
  137. HP_SDC_ACT_POSTCMD | HP_SDC_ACT_DATAIN, 
  138. HP_SDC_CMD_READ_T5, 2, 0, 0
  139. };
  140. t.endidx = numreg * 5;
  141. tseq[1] = loadcmd;
  142. tseq[t.endidx - 4] |= HP_SDC_ACT_SEMAPHORE; /* numreg assumed > 1 */
  143. t.seq = tseq;
  144. t.act.semaphore = &i8042tregs;
  145. down_interruptible(&i8042tregs);  /* Sleep if output regs in use. */
  146. if (hp_sdc_enqueue_transaction(&t)) return -1;
  147. down_interruptible(&i8042tregs);  /* Sleep until results come back. */
  148. up(&i8042tregs);
  149. return (tseq[5] | 
  150. ((uint64_t)(tseq[10]) << 8)  | ((uint64_t)(tseq[15]) << 16) |
  151. ((uint64_t)(tseq[20]) << 24) | ((uint64_t)(tseq[25]) << 32));
  152. }
  153. /* Read the i8042 real-time clock */
  154. static inline int hp_sdc_rtc_read_rt(struct timeval *res) {
  155. int64_t raw;
  156. uint32_t tenms; 
  157. unsigned int days;
  158. raw = hp_sdc_rtc_read_i8042timer(HP_SDC_CMD_LOAD_RT, 5);
  159. if (raw < 0) return -1;
  160. tenms = (uint32_t)raw & 0xffffff;
  161. days  = (unsigned int)(raw >> 24) & 0xffff;
  162. res->tv_usec = (suseconds_t)(tenms % 100) * 10000;
  163. res->tv_sec =  (time_t)(tenms / 100) + days * 86400;
  164. return 0;
  165. }
  166. /* Read the i8042 fast handshake timer */
  167. static inline int hp_sdc_rtc_read_fhs(struct timeval *res) {
  168. uint64_t raw;
  169. unsigned int tenms;
  170. raw = hp_sdc_rtc_read_i8042timer(HP_SDC_CMD_LOAD_FHS, 2);
  171. if (raw < 0) return -1;
  172. tenms = (unsigned int)raw & 0xffff;
  173. res->tv_usec = (suseconds_t)(tenms % 100) * 10000;
  174. res->tv_sec  = (time_t)(tenms / 100);
  175. return 0;
  176. }
  177. /* Read the i8042 match timer (a.k.a. alarm) */
  178. static inline int hp_sdc_rtc_read_mt(struct timeval *res) {
  179. int64_t raw;
  180. uint32_t tenms; 
  181. raw = hp_sdc_rtc_read_i8042timer(HP_SDC_CMD_LOAD_MT, 3);
  182. if (raw < 0) return -1;
  183. tenms = (uint32_t)raw & 0xffffff;
  184. res->tv_usec = (suseconds_t)(tenms % 100) * 10000;
  185. res->tv_sec  = (time_t)(tenms / 100);
  186. return 0;
  187. }
  188. /* Read the i8042 delay timer */
  189. static inline int hp_sdc_rtc_read_dt(struct timeval *res) {
  190. int64_t raw;
  191. uint32_t tenms;
  192. raw = hp_sdc_rtc_read_i8042timer(HP_SDC_CMD_LOAD_DT, 3);
  193. if (raw < 0) return -1;
  194. tenms = (uint32_t)raw & 0xffffff;
  195. res->tv_usec = (suseconds_t)(tenms % 100) * 10000;
  196. res->tv_sec  = (time_t)(tenms / 100);
  197. return 0;
  198. }
  199. /* Read the i8042 cycle timer (a.k.a. periodic) */
  200. static inline int hp_sdc_rtc_read_ct(struct timeval *res) {
  201. int64_t raw;
  202. uint32_t tenms;
  203. raw = hp_sdc_rtc_read_i8042timer(HP_SDC_CMD_LOAD_CT, 3);
  204. if (raw < 0) return -1;
  205. tenms = (uint32_t)raw & 0xffffff;
  206. res->tv_usec = (suseconds_t)(tenms % 100) * 10000;
  207. res->tv_sec  = (time_t)(tenms / 100);
  208. return 0;
  209. }
  210. /* Set the i8042 real-time clock */
  211. static int hp_sdc_rtc_set_rt (struct timeval *setto)
  212. {
  213. uint32_t tenms;
  214. unsigned int days;
  215. hp_sdc_transaction t;
  216. uint8_t tseq[11] = {
  217. HP_SDC_ACT_PRECMD | HP_SDC_ACT_DATAOUT,
  218. HP_SDC_CMD_SET_RTMS, 3, 0, 0, 0,
  219. HP_SDC_ACT_PRECMD | HP_SDC_ACT_DATAOUT,
  220. HP_SDC_CMD_SET_RTD, 2, 0, 0 
  221. };
  222. t.endidx = 10;
  223. if (0xffff < setto->tv_sec / 86400) return -1;
  224. days = setto->tv_sec / 86400;
  225. if (0xffff < setto->tv_usec / 1000000 / 86400) return -1;
  226. days += ((setto->tv_sec % 86400) + setto->tv_usec / 1000000) / 86400;
  227. if (days > 0xffff) return -1;
  228. if (0xffffff < setto->tv_sec) return -1;
  229. tenms  = setto->tv_sec * 100;
  230. if (0xffffff < setto->tv_usec / 10000) return -1;
  231. tenms += setto->tv_usec / 10000;
  232. if (tenms > 0xffffff) return -1;
  233. tseq[3] = (uint8_t)(tenms & 0xff);
  234. tseq[4] = (uint8_t)((tenms >> 8)  & 0xff);
  235. tseq[5] = (uint8_t)((tenms >> 16) & 0xff);
  236. tseq[9] = (uint8_t)(days & 0xff);
  237. tseq[10] = (uint8_t)((days >> 8) & 0xff);
  238. t.seq = tseq;
  239. if (hp_sdc_enqueue_transaction(&t)) return -1;
  240. return 0;
  241. }
  242. /* Set the i8042 fast handshake timer */
  243. static int hp_sdc_rtc_set_fhs (struct timeval *setto)
  244. {
  245. uint32_t tenms;
  246. hp_sdc_transaction t;
  247. uint8_t tseq[5] = {
  248. HP_SDC_ACT_PRECMD | HP_SDC_ACT_DATAOUT,
  249. HP_SDC_CMD_SET_FHS, 2, 0, 0
  250. };
  251. t.endidx = 4;
  252. if (0xffff < setto->tv_sec) return -1;
  253. tenms  = setto->tv_sec * 100;
  254. if (0xffff < setto->tv_usec / 10000) return -1;
  255. tenms += setto->tv_usec / 10000;
  256. if (tenms > 0xffff) return -1;
  257. tseq[3] = (uint8_t)(tenms & 0xff);
  258. tseq[4] = (uint8_t)((tenms >> 8)  & 0xff);
  259. t.seq = tseq;
  260. if (hp_sdc_enqueue_transaction(&t)) return -1;
  261. return 0;
  262. }
  263. /* Set the i8042 match timer (a.k.a. alarm) */
  264. #define hp_sdc_rtc_set_mt (setto) 
  265. hp_sdc_rtc_set_i8042timer(setto, HP_SDC_CMD_SET_MT)
  266. /* Set the i8042 delay timer */
  267. #define hp_sdc_rtc_set_dt (setto) 
  268. hp_sdc_rtc_set_i8042timer(setto, HP_SDC_CMD_SET_DT)
  269. /* Set the i8042 cycle timer (a.k.a. periodic) */
  270. #define hp_sdc_rtc_set_ct (setto) 
  271. hp_sdc_rtc_set_i8042timer(setto, HP_SDC_CMD_SET_CT)
  272. /* Set one of the i8042 3-byte wide timers */
  273. static int hp_sdc_rtc_set_i8042timer (struct timeval *setto, uint8_t setcmd)
  274. {
  275. uint32_t tenms;
  276. hp_sdc_transaction t;
  277. uint8_t tseq[6] = {
  278. HP_SDC_ACT_PRECMD | HP_SDC_ACT_DATAOUT,
  279. 0, 3, 0, 0, 0
  280. };
  281. t.endidx = 6;
  282. if (0xffffff < setto->tv_sec) return -1;
  283. tenms  = setto->tv_sec * 100;
  284. if (0xffffff < setto->tv_usec / 10000) return -1;
  285. tenms += setto->tv_usec / 10000;
  286. if (tenms > 0xffffff) return -1;
  287. tseq[1] = setcmd;
  288. tseq[3] = (uint8_t)(tenms & 0xff);
  289. tseq[4] = (uint8_t)((tenms >> 8)  & 0xff);
  290. tseq[5] = (uint8_t)((tenms >> 16)  & 0xff);
  291. t.seq = tseq;
  292. if (hp_sdc_enqueue_transaction(&t)) { 
  293. return -1;
  294. }
  295. return 0;
  296. }
  297. static loff_t hp_sdc_rtc_llseek(struct file *file, loff_t offset, int origin)
  298. {
  299.         return -ESPIPE;
  300. }
  301. static ssize_t hp_sdc_rtc_read(struct file *file, char *buf,
  302.        size_t count, loff_t *ppos) {
  303. ssize_t retval;
  304.         if (count < sizeof(unsigned long))
  305.                 return -EINVAL;
  306. retval = put_user(68, (unsigned long *)buf);
  307. return retval;
  308. }
  309. static unsigned int hp_sdc_rtc_poll(struct file *file, poll_table *wait)
  310. {
  311.         unsigned long l;
  312. l = 0;
  313.         if (l != 0)
  314.                 return POLLIN | POLLRDNORM;
  315.         return 0;
  316. }
  317. static int hp_sdc_rtc_open(struct inode *inode, struct file *file)
  318. {
  319. MOD_INC_USE_COUNT;
  320.         return 0;
  321. }
  322. static int hp_sdc_rtc_release(struct inode *inode, struct file *file)
  323. {
  324. /* Turn off interrupts? */
  325.         if (file->f_flags & FASYNC) {
  326.                 hp_sdc_rtc_fasync (-1, file, 0);
  327.         }
  328. MOD_DEC_USE_COUNT;
  329.         return 0;
  330. }
  331. static int hp_sdc_rtc_fasync (int fd, struct file *filp, int on)
  332. {
  333.         return fasync_helper (fd, filp, on, &hp_sdc_rtc_async_queue);
  334. }
  335. static int hp_sdc_rtc_proc_output (char *buf)
  336. {
  337. #define YN(bit) ("no")
  338. #define NY(bit) ("yes")
  339.         char *p;
  340.         struct rtc_time tm;
  341. struct timeval tv;
  342. memset(&tm, 0, sizeof(struct rtc_time));
  343. p = buf;
  344. if (hp_sdc_rtc_read_bbrtc(&tm)) {
  345. p += sprintf(p, "BBRTCtt: READ FAILED!n");
  346. } else {
  347. p += sprintf(p,
  348.      "rtc_timet: %02d:%02d:%02dn"
  349.      "rtc_datet: %04d-%02d-%02dn"
  350.      "rtc_epocht: %04lun",
  351.      tm.tm_hour, tm.tm_min, tm.tm_sec,
  352.      tm.tm_year + 1900, tm.tm_mon + 1, 
  353.      tm.tm_mday, epoch);
  354. }
  355. if (hp_sdc_rtc_read_rt(&tv)) {
  356. p += sprintf(p, "i8042 rtct: READ FAILED!n");
  357. } else {
  358. p += sprintf(p, "i8042 rtct: %d.%02d secondsn", 
  359.      tv.tv_sec, tv.tv_usec/1000);
  360. }
  361. if (hp_sdc_rtc_read_fhs(&tv)) {
  362. p += sprintf(p, "handshaket: READ FAILED!n");
  363. } else {
  364.          p += sprintf(p, "handshaket: %d.%02d secondsn", 
  365.      tv.tv_sec, tv.tv_usec/1000);
  366. }
  367. if (hp_sdc_rtc_read_mt(&tv)) {
  368. p += sprintf(p, "alarmtt: READ FAILED!n");
  369. } else {
  370. p += sprintf(p, "alarmtt: %d.%02d secondsn", 
  371.      tv.tv_sec, tv.tv_usec/1000);
  372. }
  373. if (hp_sdc_rtc_read_dt(&tv)) {
  374. p += sprintf(p, "delaytt: READ FAILED!n");
  375. } else {
  376. p += sprintf(p, "delaytt: %d.%02d secondsn", 
  377.      tv.tv_sec, tv.tv_usec/1000);
  378. }
  379. if (hp_sdc_rtc_read_ct(&tv)) {
  380. p += sprintf(p, "periodict: READ FAILED!n");
  381. } else {
  382. p += sprintf(p, "periodict: %d.%02d secondsn", 
  383.      tv.tv_sec, tv.tv_usec/1000);
  384. }
  385.         p += sprintf(p,
  386.                      "DST_enablet: %sn"
  387.                      "BCDtt: %sn"
  388.                      "24hrtt: %sn"
  389.                      "square_wavet: %sn"
  390.                      "alarm_IRQt: %sn"
  391.                      "update_IRQt: %sn"
  392.                      "periodic_IRQt: %sn"
  393.      "periodic_freqt: %ldn"
  394.                      "batt_statust: %sn",
  395.                      YN(RTC_DST_EN),
  396.                      NY(RTC_DM_BINARY),
  397.                      YN(RTC_24H),
  398.                      YN(RTC_SQWE),
  399.                      YN(RTC_AIE),
  400.                      YN(RTC_UIE),
  401.                      YN(RTC_PIE),
  402.                      1UL,
  403.                      1 ? "okay" : "dead");
  404.         return  p - buf;
  405. #undef YN
  406. #undef NY
  407. }
  408. static int hp_sdc_rtc_read_proc(char *page, char **start, off_t off,
  409.                          int count, int *eof, void *data)
  410. {
  411. int len = hp_sdc_rtc_proc_output (page);
  412.         if (len <= off+count) *eof = 1;
  413.         *start = page + off;
  414.         len -= off;
  415.         if (len>count) len = count;
  416.         if (len<0) len = 0;
  417.         return len;
  418. }
  419. static int hp_sdc_rtc_ioctl(struct inode *inode, struct file *file, 
  420.     unsigned int cmd, unsigned long arg)
  421. {
  422. #if 1
  423. return -EINVAL;
  424. #else
  425.         struct rtc_time wtime; 
  426. struct timeval ttime;
  427. int use_wtime = 0;
  428. /* This needs major work. */
  429.         switch (cmd) {
  430.         case RTC_AIE_OFF:       /* Mask alarm int. enab. bit    */
  431.         case RTC_AIE_ON:        /* Allow alarm interrupts.      */
  432. case RTC_PIE_OFF:       /* Mask periodic int. enab. bit */
  433.         case RTC_PIE_ON:        /* Allow periodic ints          */
  434.         case RTC_UIE_ON:        /* Allow ints for RTC updates.  */
  435.         case RTC_UIE_OFF:       /* Allow ints for RTC updates.  */
  436.         {
  437. /* We cannot mask individual user timers and we
  438.    cannot tell them apart when they occur, so it 
  439.    would be disingenuous to succeed these IOCTLs */
  440. return -EINVAL;
  441.         }
  442.         case RTC_ALM_READ:      /* Read the present alarm time */
  443.         {
  444. if (hp_sdc_rtc_read_mt(&ttime)) return -EFAULT;
  445.                 break;
  446.         }
  447.         case RTC_IRQP_READ:     /* Read the periodic IRQ rate.  */
  448.         {
  449.                 return put_user(hp_sdc_rtc_freq, (unsigned long *)arg);
  450.         }
  451.         case RTC_IRQP_SET:      /* Set periodic IRQ rate.       */
  452.         {
  453.                 /* 
  454.                  * The max we can do is 100Hz.
  455.  */
  456.                 if ((arg < 1) || (arg > 100)) return -EINVAL;
  457. ttime.tv_sec = 0;
  458. ttime.tv_usec = 1000000 / arg;
  459. if (hp_sdc_rtc_set_ct(&ttime)) return -EFAULT;
  460. hp_sdc_rtc_freq = arg;
  461.                 return 0;
  462.         }
  463.         case RTC_ALM_SET:       /* Store a time into the alarm */
  464.         {
  465.                 /*
  466.                  * This expects a struct hp_sdc_rtc_time. Writing 0xff means
  467.                  * "don't care" or "match all" for PC timers.  The HP SDC
  468.  * does not support that perk, but it could be emulated fairly
  469.  * easily.  Only the tm_hour, tm_min and tm_sec are used.
  470.  * We could do it with 10ms accuracy with the HP SDC, if the 
  471.  * rtc interface left us a way to do that.
  472.                  */
  473.                 struct hp_sdc_rtc_time alm_tm;
  474.                 if (copy_from_user(&alm_tm, (struct hp_sdc_rtc_time*)arg,
  475.                                    sizeof(struct hp_sdc_rtc_time)))
  476.                        return -EFAULT;
  477.                 if (alm_tm.tm_hour > 23) return -EINVAL;
  478. if (alm_tm.tm_min  > 59) return -EINVAL;
  479. if (alm_tm.tm_sec  > 59) return -EINVAL;  
  480. ttime.sec = alm_tm.tm_hour * 3600 + 
  481.   alm_tm.tm_min * 60 + alm_tm.tm_sec;
  482. ttime.usec = 0;
  483. if (hp_sdc_rtc_set_mt(&ttime)) return -EFAULT;
  484.                 return 0;
  485.         }
  486.         case RTC_RD_TIME:       /* Read the time/date from RTC  */
  487.         {
  488. if (hp_sdc_rtc_read_bbrtc(&wtime)) return -EFAULT;
  489.                 break;
  490.         }
  491.         case RTC_SET_TIME:      /* Set the RTC */
  492.         {
  493.                 struct rtc_time hp_sdc_rtc_tm;
  494.                 unsigned char mon, day, hrs, min, sec, leap_yr;
  495.                 unsigned int yrs;
  496.                 if (!capable(CAP_SYS_TIME))
  497.                         return -EACCES;
  498. if (copy_from_user(&hp_sdc_rtc_tm, (struct rtc_time *)arg,
  499.                                    sizeof(struct rtc_time)))
  500.                         return -EFAULT;
  501.                 yrs = hp_sdc_rtc_tm.tm_year + 1900;
  502.                 mon = hp_sdc_rtc_tm.tm_mon + 1;   /* tm_mon starts at zero */
  503.                 day = hp_sdc_rtc_tm.tm_mday;
  504.                 hrs = hp_sdc_rtc_tm.tm_hour;
  505.                 min = hp_sdc_rtc_tm.tm_min;
  506.                 sec = hp_sdc_rtc_tm.tm_sec;
  507.                 if (yrs < 1970)
  508.                         return -EINVAL;
  509.                 leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400));
  510.                 if ((mon > 12) || (day == 0))
  511.                         return -EINVAL;
  512.                 if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr)))
  513.                         return -EINVAL;
  514. if ((hrs >= 24) || (min >= 60) || (sec >= 60))
  515.                         return -EINVAL;
  516.                 if ((yrs -= eH) > 255)    /* They are unsigned */
  517.                         return -EINVAL;
  518.                 return 0;
  519.         }
  520.         case RTC_epoch_READ:    /* Read the epoch.      */
  521.         {
  522.                 return put_user (epoch, (unsigned long *)arg);
  523.         }
  524.         case RTC_EPOCH_SET:     /* Set the epoch.       */
  525.         {
  526.                 /* 
  527.                  * There were no RTC clocks before 1900.
  528.                  */
  529.                 if (arg < 1900)
  530.   return -EINVAL;
  531. if (!capable(CAP_SYS_TIME))
  532.   return -EACCES;
  533.                 epoch = arg;
  534.                 return 0;
  535.         }
  536.         default:
  537.                 return -EINVAL;
  538.         }
  539.         return copy_to_user((void *)arg, &wtime, sizeof wtime) ? -EFAULT : 0;
  540. #endif
  541. }
  542. static struct file_operations hp_sdc_rtc_fops = {
  543.         owner:          THIS_MODULE,
  544.         llseek:         hp_sdc_rtc_llseek,
  545.         read:           hp_sdc_rtc_read,
  546.         poll:           hp_sdc_rtc_poll,
  547.         ioctl:          hp_sdc_rtc_ioctl,
  548.         open:           hp_sdc_rtc_open,
  549.         release:        hp_sdc_rtc_release,
  550.         fasync:         hp_sdc_rtc_fasync,
  551. };
  552. static struct miscdevice hp_sdc_rtc_dev = {
  553.         minor: RTC_MINOR,
  554.         name: "rtc",
  555.         fops: &hp_sdc_rtc_fops
  556. };
  557. static int __init hp_sdc_rtc_init(void)
  558. {
  559. int ret;
  560. init_MUTEX(&i8042tregs);
  561. if ((ret = hp_sdc_request_timer_irq(&hp_sdc_rtc_isr)))
  562. return ret;
  563. misc_register(&hp_sdc_rtc_dev);
  564.         create_proc_read_entry ("driver/rtc", 0, 0, 
  565. hp_sdc_rtc_read_proc, NULL);
  566. printk(KERN_INFO "HP i8042 SDC + MSM-58321 RTC support loaded "
  567.  "(RTC v " RTC_VERSION ")n");
  568. return 0;
  569. }
  570. static void __exit hp_sdc_rtc_exit(void)
  571. {
  572. remove_proc_entry ("driver/rtc", NULL);
  573.         misc_deregister(&hp_sdc_rtc_dev);
  574. hp_sdc_release_timer_irq(hp_sdc_rtc_isr);
  575.         printk(KERN_INFO "HP i8042 SDC + MSM-58321 RTC support unloadedn");
  576. }
  577. module_init(hp_sdc_rtc_init);
  578. module_exit(hp_sdc_rtc_exit);