hp_sdc_rtc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  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/seq_file.h>
  44. #include <linux/poll.h>
  45. #include <linux/rtc.h>
  46. #include <linux/mutex.h>
  47. #include <linux/semaphore.h>
  48. MODULE_AUTHOR("Brian S. Julin <bri@calyx.com>");
  49. MODULE_DESCRIPTION("HP i8042 SDC + MSM-58321 RTC Driver");
  50. MODULE_LICENSE("Dual BSD/GPL");
  51. #define RTC_VERSION "1.10d"
  52. static DEFINE_MUTEX(hp_sdc_rtc_mutex);
  53. static unsigned long epoch = 2000;
  54. static struct semaphore i8042tregs;
  55. static hp_sdc_irqhook hp_sdc_rtc_isr;
  56. static struct fasync_struct *hp_sdc_rtc_async_queue;
  57. static DECLARE_WAIT_QUEUE_HEAD(hp_sdc_rtc_wait);
  58. static ssize_t hp_sdc_rtc_read(struct file *file, char __user *buf,
  59. size_t count, loff_t *ppos);
  60. static long hp_sdc_rtc_unlocked_ioctl(struct file *file,
  61. unsigned int cmd, unsigned long arg);
  62. static unsigned int hp_sdc_rtc_poll(struct file *file, poll_table *wait);
  63. static int hp_sdc_rtc_open(struct inode *inode, struct file *file);
  64. static int hp_sdc_rtc_fasync (int fd, struct file *filp, int on);
  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. sema_init(&tsem, 0);
  92. if (hp_sdc_enqueue_transaction(&t)) return -1;
  93. /* Put ourselves to sleep for results. */
  94. if (WARN_ON(down_interruptible(&tsem)))
  95. return -1;
  96. /* Check for nonpresence of BBRTC */
  97. if (!((tseq[83] | tseq[90] | tseq[69] | tseq[76] |
  98. tseq[55] | tseq[62] | tseq[34] | tseq[41] |
  99. tseq[20] | tseq[27] | tseq[6] | tseq[13]) & 0x0f))
  100. return -1;
  101. memset(rtctm, 0, sizeof(struct rtc_time));
  102. rtctm->tm_year = (tseq[83] & 0x0f) + (tseq[90] & 0x0f) * 10;
  103. rtctm->tm_mon = (tseq[69] & 0x0f) + (tseq[76] & 0x0f) * 10;
  104. rtctm->tm_mday = (tseq[55] & 0x0f) + (tseq[62] & 0x0f) * 10;
  105. rtctm->tm_wday = (tseq[48] & 0x0f);
  106. rtctm->tm_hour = (tseq[34] & 0x0f) + (tseq[41] & 0x0f) * 10;
  107. rtctm->tm_min = (tseq[20] & 0x0f) + (tseq[27] & 0x0f) * 10;
  108. rtctm->tm_sec = (tseq[6] & 0x0f) + (tseq[13] & 0x0f) * 10;
  109. return 0;
  110. }
  111. static int hp_sdc_rtc_read_bbrtc (struct rtc_time *rtctm)
  112. {
  113. struct rtc_time tm, tm_last;
  114. int i = 0;
  115. /* MSM-58321 has no read latch, so must read twice and compare. */
  116. if (hp_sdc_rtc_do_read_bbrtc(&tm_last)) return -1;
  117. if (hp_sdc_rtc_do_read_bbrtc(&tm)) return -1;
  118. while (memcmp(&tm, &tm_last, sizeof(struct rtc_time))) {
  119. if (i++ > 4) return -1;
  120. memcpy(&tm_last, &tm, sizeof(struct rtc_time));
  121. if (hp_sdc_rtc_do_read_bbrtc(&tm)) return -1;
  122. }
  123. memcpy(rtctm, &tm, sizeof(struct rtc_time));
  124. return 0;
  125. }
  126. static int64_t hp_sdc_rtc_read_i8042timer (uint8_t loadcmd, int numreg)
  127. {
  128. hp_sdc_transaction t;
  129. uint8_t tseq[26] = {
  130. HP_SDC_ACT_PRECMD | HP_SDC_ACT_POSTCMD | HP_SDC_ACT_DATAIN,
  131. 0,
  132. HP_SDC_CMD_READ_T1, 2, 0, 0,
  133. HP_SDC_ACT_POSTCMD | HP_SDC_ACT_DATAIN,
  134. HP_SDC_CMD_READ_T2, 2, 0, 0,
  135. HP_SDC_ACT_POSTCMD | HP_SDC_ACT_DATAIN,
  136. HP_SDC_CMD_READ_T3, 2, 0, 0,
  137. HP_SDC_ACT_POSTCMD | HP_SDC_ACT_DATAIN,
  138. HP_SDC_CMD_READ_T4, 2, 0, 0,
  139. HP_SDC_ACT_POSTCMD | HP_SDC_ACT_DATAIN,
  140. HP_SDC_CMD_READ_T5, 2, 0, 0
  141. };
  142. t.endidx = numreg * 5;
  143. tseq[1] = loadcmd;
  144. tseq[t.endidx - 4] |= HP_SDC_ACT_SEMAPHORE; /* numreg assumed > 1 */
  145. t.seq = tseq;
  146. t.act.semaphore = &i8042tregs;
  147. /* Sleep if output regs in use. */
  148. if (WARN_ON(down_interruptible(&i8042tregs)))
  149. return -1;
  150. if (hp_sdc_enqueue_transaction(&t)) return -1;
  151. /* Sleep until results come back. */
  152. if (WARN_ON(down_interruptible(&i8042tregs)))
  153. return -1;
  154. up(&i8042tregs);
  155. return (tseq[5] |
  156. ((uint64_t)(tseq[10]) << 8) | ((uint64_t)(tseq[15]) << 16) |
  157. ((uint64_t)(tseq[20]) << 24) | ((uint64_t)(tseq[25]) << 32));
  158. }
  159. /* Read the i8042 real-time clock */
  160. static inline int hp_sdc_rtc_read_rt(struct timeval *res) {
  161. int64_t raw;
  162. uint32_t tenms;
  163. unsigned int days;
  164. raw = hp_sdc_rtc_read_i8042timer(HP_SDC_CMD_LOAD_RT, 5);
  165. if (raw < 0) return -1;
  166. tenms = (uint32_t)raw & 0xffffff;
  167. days = (unsigned int)(raw >> 24) & 0xffff;
  168. res->tv_usec = (suseconds_t)(tenms % 100) * 10000;
  169. res->tv_sec = (time_t)(tenms / 100) + days * 86400;
  170. return 0;
  171. }
  172. /* Read the i8042 fast handshake timer */
  173. static inline int hp_sdc_rtc_read_fhs(struct timeval *res) {
  174. int64_t raw;
  175. unsigned int tenms;
  176. raw = hp_sdc_rtc_read_i8042timer(HP_SDC_CMD_LOAD_FHS, 2);
  177. if (raw < 0) return -1;
  178. tenms = (unsigned int)raw & 0xffff;
  179. res->tv_usec = (suseconds_t)(tenms % 100) * 10000;
  180. res->tv_sec = (time_t)(tenms / 100);
  181. return 0;
  182. }
  183. /* Read the i8042 match timer (a.k.a. alarm) */
  184. static inline int hp_sdc_rtc_read_mt(struct timeval *res) {
  185. int64_t raw;
  186. uint32_t tenms;
  187. raw = hp_sdc_rtc_read_i8042timer(HP_SDC_CMD_LOAD_MT, 3);
  188. if (raw < 0) return -1;
  189. tenms = (uint32_t)raw & 0xffffff;
  190. res->tv_usec = (suseconds_t)(tenms % 100) * 10000;
  191. res->tv_sec = (time_t)(tenms / 100);
  192. return 0;
  193. }
  194. /* Read the i8042 delay timer */
  195. static inline int hp_sdc_rtc_read_dt(struct timeval *res) {
  196. int64_t raw;
  197. uint32_t tenms;
  198. raw = hp_sdc_rtc_read_i8042timer(HP_SDC_CMD_LOAD_DT, 3);
  199. if (raw < 0) return -1;
  200. tenms = (uint32_t)raw & 0xffffff;
  201. res->tv_usec = (suseconds_t)(tenms % 100) * 10000;
  202. res->tv_sec = (time_t)(tenms / 100);
  203. return 0;
  204. }
  205. /* Read the i8042 cycle timer (a.k.a. periodic) */
  206. static inline int hp_sdc_rtc_read_ct(struct timeval *res) {
  207. int64_t raw;
  208. uint32_t tenms;
  209. raw = hp_sdc_rtc_read_i8042timer(HP_SDC_CMD_LOAD_CT, 3);
  210. if (raw < 0) return -1;
  211. tenms = (uint32_t)raw & 0xffffff;
  212. res->tv_usec = (suseconds_t)(tenms % 100) * 10000;
  213. res->tv_sec = (time_t)(tenms / 100);
  214. return 0;
  215. }
  216. #if 0 /* not used yet */
  217. /* Set the i8042 real-time clock */
  218. static int hp_sdc_rtc_set_rt (struct timeval *setto)
  219. {
  220. uint32_t tenms;
  221. unsigned int days;
  222. hp_sdc_transaction t;
  223. uint8_t tseq[11] = {
  224. HP_SDC_ACT_PRECMD | HP_SDC_ACT_DATAOUT,
  225. HP_SDC_CMD_SET_RTMS, 3, 0, 0, 0,
  226. HP_SDC_ACT_PRECMD | HP_SDC_ACT_DATAOUT,
  227. HP_SDC_CMD_SET_RTD, 2, 0, 0
  228. };
  229. t.endidx = 10;
  230. if (0xffff < setto->tv_sec / 86400) return -1;
  231. days = setto->tv_sec / 86400;
  232. if (0xffff < setto->tv_usec / 1000000 / 86400) return -1;
  233. days += ((setto->tv_sec % 86400) + setto->tv_usec / 1000000) / 86400;
  234. if (days > 0xffff) return -1;
  235. if (0xffffff < setto->tv_sec) return -1;
  236. tenms = setto->tv_sec * 100;
  237. if (0xffffff < setto->tv_usec / 10000) return -1;
  238. tenms += setto->tv_usec / 10000;
  239. if (tenms > 0xffffff) return -1;
  240. tseq[3] = (uint8_t)(tenms & 0xff);
  241. tseq[4] = (uint8_t)((tenms >> 8) & 0xff);
  242. tseq[5] = (uint8_t)((tenms >> 16) & 0xff);
  243. tseq[9] = (uint8_t)(days & 0xff);
  244. tseq[10] = (uint8_t)((days >> 8) & 0xff);
  245. t.seq = tseq;
  246. if (hp_sdc_enqueue_transaction(&t)) return -1;
  247. return 0;
  248. }
  249. /* Set the i8042 fast handshake timer */
  250. static int hp_sdc_rtc_set_fhs (struct timeval *setto)
  251. {
  252. uint32_t tenms;
  253. hp_sdc_transaction t;
  254. uint8_t tseq[5] = {
  255. HP_SDC_ACT_PRECMD | HP_SDC_ACT_DATAOUT,
  256. HP_SDC_CMD_SET_FHS, 2, 0, 0
  257. };
  258. t.endidx = 4;
  259. if (0xffff < setto->tv_sec) return -1;
  260. tenms = setto->tv_sec * 100;
  261. if (0xffff < setto->tv_usec / 10000) return -1;
  262. tenms += setto->tv_usec / 10000;
  263. if (tenms > 0xffff) return -1;
  264. tseq[3] = (uint8_t)(tenms & 0xff);
  265. tseq[4] = (uint8_t)((tenms >> 8) & 0xff);
  266. t.seq = tseq;
  267. if (hp_sdc_enqueue_transaction(&t)) return -1;
  268. return 0;
  269. }
  270. /* Set the i8042 match timer (a.k.a. alarm) */
  271. #define hp_sdc_rtc_set_mt (setto) \
  272. hp_sdc_rtc_set_i8042timer(setto, HP_SDC_CMD_SET_MT)
  273. /* Set the i8042 delay timer */
  274. #define hp_sdc_rtc_set_dt (setto) \
  275. hp_sdc_rtc_set_i8042timer(setto, HP_SDC_CMD_SET_DT)
  276. /* Set the i8042 cycle timer (a.k.a. periodic) */
  277. #define hp_sdc_rtc_set_ct (setto) \
  278. hp_sdc_rtc_set_i8042timer(setto, HP_SDC_CMD_SET_CT)
  279. /* Set one of the i8042 3-byte wide timers */
  280. static int hp_sdc_rtc_set_i8042timer (struct timeval *setto, uint8_t setcmd)
  281. {
  282. uint32_t tenms;
  283. hp_sdc_transaction t;
  284. uint8_t tseq[6] = {
  285. HP_SDC_ACT_PRECMD | HP_SDC_ACT_DATAOUT,
  286. 0, 3, 0, 0, 0
  287. };
  288. t.endidx = 6;
  289. if (0xffffff < setto->tv_sec) return -1;
  290. tenms = setto->tv_sec * 100;
  291. if (0xffffff < setto->tv_usec / 10000) return -1;
  292. tenms += setto->tv_usec / 10000;
  293. if (tenms > 0xffffff) return -1;
  294. tseq[1] = setcmd;
  295. tseq[3] = (uint8_t)(tenms & 0xff);
  296. tseq[4] = (uint8_t)((tenms >> 8) & 0xff);
  297. tseq[5] = (uint8_t)((tenms >> 16) & 0xff);
  298. t.seq = tseq;
  299. if (hp_sdc_enqueue_transaction(&t)) {
  300. return -1;
  301. }
  302. return 0;
  303. }
  304. #endif
  305. static ssize_t hp_sdc_rtc_read(struct file *file, char __user *buf,
  306. size_t count, loff_t *ppos) {
  307. ssize_t retval;
  308. if (count < sizeof(unsigned long))
  309. return -EINVAL;
  310. retval = put_user(68, (unsigned long __user *)buf);
  311. return retval;
  312. }
  313. static unsigned int hp_sdc_rtc_poll(struct file *file, poll_table *wait)
  314. {
  315. unsigned long l;
  316. l = 0;
  317. if (l != 0)
  318. return POLLIN | POLLRDNORM;
  319. return 0;
  320. }
  321. static int hp_sdc_rtc_open(struct inode *inode, struct file *file)
  322. {
  323. return 0;
  324. }
  325. static int hp_sdc_rtc_fasync (int fd, struct file *filp, int on)
  326. {
  327. return fasync_helper (fd, filp, on, &hp_sdc_rtc_async_queue);
  328. }
  329. static int hp_sdc_rtc_proc_show(struct seq_file *m, void *v)
  330. {
  331. #define YN(bit) ("no")
  332. #define NY(bit) ("yes")
  333. struct rtc_time tm;
  334. struct timeval tv;
  335. memset(&tm, 0, sizeof(struct rtc_time));
  336. if (hp_sdc_rtc_read_bbrtc(&tm)) {
  337. seq_puts(m, "BBRTC\t\t: READ FAILED!\n");
  338. } else {
  339. seq_printf(m,
  340. "rtc_time\t: %02d:%02d:%02d\n"
  341. "rtc_date\t: %04d-%02d-%02d\n"
  342. "rtc_epoch\t: %04lu\n",
  343. tm.tm_hour, tm.tm_min, tm.tm_sec,
  344. tm.tm_year + 1900, tm.tm_mon + 1,
  345. tm.tm_mday, epoch);
  346. }
  347. if (hp_sdc_rtc_read_rt(&tv)) {
  348. seq_puts(m, "i8042 rtc\t: READ FAILED!\n");
  349. } else {
  350. seq_printf(m, "i8042 rtc\t: %ld.%02d seconds\n",
  351. tv.tv_sec, (int)tv.tv_usec/1000);
  352. }
  353. if (hp_sdc_rtc_read_fhs(&tv)) {
  354. seq_puts(m, "handshake\t: READ FAILED!\n");
  355. } else {
  356. seq_printf(m, "handshake\t: %ld.%02d seconds\n",
  357. tv.tv_sec, (int)tv.tv_usec/1000);
  358. }
  359. if (hp_sdc_rtc_read_mt(&tv)) {
  360. seq_puts(m, "alarm\t\t: READ FAILED!\n");
  361. } else {
  362. seq_printf(m, "alarm\t\t: %ld.%02d seconds\n",
  363. tv.tv_sec, (int)tv.tv_usec/1000);
  364. }
  365. if (hp_sdc_rtc_read_dt(&tv)) {
  366. seq_puts(m, "delay\t\t: READ FAILED!\n");
  367. } else {
  368. seq_printf(m, "delay\t\t: %ld.%02d seconds\n",
  369. tv.tv_sec, (int)tv.tv_usec/1000);
  370. }
  371. if (hp_sdc_rtc_read_ct(&tv)) {
  372. seq_puts(m, "periodic\t: READ FAILED!\n");
  373. } else {
  374. seq_printf(m, "periodic\t: %ld.%02d seconds\n",
  375. tv.tv_sec, (int)tv.tv_usec/1000);
  376. }
  377. seq_printf(m,
  378. "DST_enable\t: %s\n"
  379. "BCD\t\t: %s\n"
  380. "24hr\t\t: %s\n"
  381. "square_wave\t: %s\n"
  382. "alarm_IRQ\t: %s\n"
  383. "update_IRQ\t: %s\n"
  384. "periodic_IRQ\t: %s\n"
  385. "periodic_freq\t: %ld\n"
  386. "batt_status\t: %s\n",
  387. YN(RTC_DST_EN),
  388. NY(RTC_DM_BINARY),
  389. YN(RTC_24H),
  390. YN(RTC_SQWE),
  391. YN(RTC_AIE),
  392. YN(RTC_UIE),
  393. YN(RTC_PIE),
  394. 1UL,
  395. 1 ? "okay" : "dead");
  396. return 0;
  397. #undef YN
  398. #undef NY
  399. }
  400. static int hp_sdc_rtc_proc_open(struct inode *inode, struct file *file)
  401. {
  402. return single_open(file, hp_sdc_rtc_proc_show, NULL);
  403. }
  404. static const struct file_operations hp_sdc_rtc_proc_fops = {
  405. .open = hp_sdc_rtc_proc_open,
  406. .read = seq_read,
  407. .llseek = seq_lseek,
  408. .release = single_release,
  409. };
  410. static int hp_sdc_rtc_ioctl(struct file *file,
  411. unsigned int cmd, unsigned long arg)
  412. {
  413. #if 1
  414. return -EINVAL;
  415. #else
  416. struct rtc_time wtime;
  417. struct timeval ttime;
  418. int use_wtime = 0;
  419. /* This needs major work. */
  420. switch (cmd) {
  421. case RTC_AIE_OFF: /* Mask alarm int. enab. bit */
  422. case RTC_AIE_ON: /* Allow alarm interrupts. */
  423. case RTC_PIE_OFF: /* Mask periodic int. enab. bit */
  424. case RTC_PIE_ON: /* Allow periodic ints */
  425. case RTC_UIE_ON: /* Allow ints for RTC updates. */
  426. case RTC_UIE_OFF: /* Allow ints for RTC updates. */
  427. {
  428. /* We cannot mask individual user timers and we
  429. cannot tell them apart when they occur, so it
  430. would be disingenuous to succeed these IOCTLs */
  431. return -EINVAL;
  432. }
  433. case RTC_ALM_READ: /* Read the present alarm time */
  434. {
  435. if (hp_sdc_rtc_read_mt(&ttime)) return -EFAULT;
  436. if (hp_sdc_rtc_read_bbrtc(&wtime)) return -EFAULT;
  437. wtime.tm_hour = ttime.tv_sec / 3600; ttime.tv_sec %= 3600;
  438. wtime.tm_min = ttime.tv_sec / 60; ttime.tv_sec %= 60;
  439. wtime.tm_sec = ttime.tv_sec;
  440. break;
  441. }
  442. case RTC_IRQP_READ: /* Read the periodic IRQ rate. */
  443. {
  444. return put_user(hp_sdc_rtc_freq, (unsigned long *)arg);
  445. }
  446. case RTC_IRQP_SET: /* Set periodic IRQ rate. */
  447. {
  448. /*
  449. * The max we can do is 100Hz.
  450. */
  451. if ((arg < 1) || (arg > 100)) return -EINVAL;
  452. ttime.tv_sec = 0;
  453. ttime.tv_usec = 1000000 / arg;
  454. if (hp_sdc_rtc_set_ct(&ttime)) return -EFAULT;
  455. hp_sdc_rtc_freq = arg;
  456. return 0;
  457. }
  458. case RTC_ALM_SET: /* Store a time into the alarm */
  459. {
  460. /*
  461. * This expects a struct hp_sdc_rtc_time. Writing 0xff means
  462. * "don't care" or "match all" for PC timers. The HP SDC
  463. * does not support that perk, but it could be emulated fairly
  464. * easily. Only the tm_hour, tm_min and tm_sec are used.
  465. * We could do it with 10ms accuracy with the HP SDC, if the
  466. * rtc interface left us a way to do that.
  467. */
  468. struct hp_sdc_rtc_time alm_tm;
  469. if (copy_from_user(&alm_tm, (struct hp_sdc_rtc_time*)arg,
  470. sizeof(struct hp_sdc_rtc_time)))
  471. return -EFAULT;
  472. if (alm_tm.tm_hour > 23) return -EINVAL;
  473. if (alm_tm.tm_min > 59) return -EINVAL;
  474. if (alm_tm.tm_sec > 59) return -EINVAL;
  475. ttime.sec = alm_tm.tm_hour * 3600 +
  476. alm_tm.tm_min * 60 + alm_tm.tm_sec;
  477. ttime.usec = 0;
  478. if (hp_sdc_rtc_set_mt(&ttime)) return -EFAULT;
  479. return 0;
  480. }
  481. case RTC_RD_TIME: /* Read the time/date from RTC */
  482. {
  483. if (hp_sdc_rtc_read_bbrtc(&wtime)) return -EFAULT;
  484. break;
  485. }
  486. case RTC_SET_TIME: /* Set the RTC */
  487. {
  488. struct rtc_time hp_sdc_rtc_tm;
  489. unsigned char mon, day, hrs, min, sec, leap_yr;
  490. unsigned int yrs;
  491. if (!capable(CAP_SYS_TIME))
  492. return -EACCES;
  493. if (copy_from_user(&hp_sdc_rtc_tm, (struct rtc_time *)arg,
  494. sizeof(struct rtc_time)))
  495. return -EFAULT;
  496. yrs = hp_sdc_rtc_tm.tm_year + 1900;
  497. mon = hp_sdc_rtc_tm.tm_mon + 1; /* tm_mon starts at zero */
  498. day = hp_sdc_rtc_tm.tm_mday;
  499. hrs = hp_sdc_rtc_tm.tm_hour;
  500. min = hp_sdc_rtc_tm.tm_min;
  501. sec = hp_sdc_rtc_tm.tm_sec;
  502. if (yrs < 1970)
  503. return -EINVAL;
  504. leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400));
  505. if ((mon > 12) || (day == 0))
  506. return -EINVAL;
  507. if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr)))
  508. return -EINVAL;
  509. if ((hrs >= 24) || (min >= 60) || (sec >= 60))
  510. return -EINVAL;
  511. if ((yrs -= eH) > 255) /* They are unsigned */
  512. return -EINVAL;
  513. return 0;
  514. }
  515. case RTC_EPOCH_READ: /* Read the epoch. */
  516. {
  517. return put_user (epoch, (unsigned long *)arg);
  518. }
  519. case RTC_EPOCH_SET: /* Set the epoch. */
  520. {
  521. /*
  522. * There were no RTC clocks before 1900.
  523. */
  524. if (arg < 1900)
  525. return -EINVAL;
  526. if (!capable(CAP_SYS_TIME))
  527. return -EACCES;
  528. epoch = arg;
  529. return 0;
  530. }
  531. default:
  532. return -EINVAL;
  533. }
  534. return copy_to_user((void *)arg, &wtime, sizeof wtime) ? -EFAULT : 0;
  535. #endif
  536. }
  537. static long hp_sdc_rtc_unlocked_ioctl(struct file *file,
  538. unsigned int cmd, unsigned long arg)
  539. {
  540. int ret;
  541. mutex_lock(&hp_sdc_rtc_mutex);
  542. ret = hp_sdc_rtc_ioctl(file, cmd, arg);
  543. mutex_unlock(&hp_sdc_rtc_mutex);
  544. return ret;
  545. }
  546. static const struct file_operations hp_sdc_rtc_fops = {
  547. .owner = THIS_MODULE,
  548. .llseek = no_llseek,
  549. .read = hp_sdc_rtc_read,
  550. .poll = hp_sdc_rtc_poll,
  551. .unlocked_ioctl = hp_sdc_rtc_unlocked_ioctl,
  552. .open = hp_sdc_rtc_open,
  553. .fasync = hp_sdc_rtc_fasync,
  554. };
  555. static struct miscdevice hp_sdc_rtc_dev = {
  556. .minor = RTC_MINOR,
  557. .name = "rtc_HIL",
  558. .fops = &hp_sdc_rtc_fops
  559. };
  560. static int __init hp_sdc_rtc_init(void)
  561. {
  562. int ret;
  563. #ifdef __mc68000__
  564. if (!MACH_IS_HP300)
  565. return -ENODEV;
  566. #endif
  567. sema_init(&i8042tregs, 1);
  568. if ((ret = hp_sdc_request_timer_irq(&hp_sdc_rtc_isr)))
  569. return ret;
  570. if (misc_register(&hp_sdc_rtc_dev) != 0)
  571. printk(KERN_INFO "Could not register misc. dev for i8042 rtc\n");
  572. proc_create("driver/rtc", 0, NULL, &hp_sdc_rtc_proc_fops);
  573. printk(KERN_INFO "HP i8042 SDC + MSM-58321 RTC support loaded "
  574. "(RTC v " RTC_VERSION ")\n");
  575. return 0;
  576. }
  577. static void __exit hp_sdc_rtc_exit(void)
  578. {
  579. remove_proc_entry ("driver/rtc", NULL);
  580. misc_deregister(&hp_sdc_rtc_dev);
  581. hp_sdc_release_timer_irq(hp_sdc_rtc_isr);
  582. printk(KERN_INFO "HP i8042 SDC + MSM-58321 RTC support unloaded\n");
  583. }
  584. module_init(hp_sdc_rtc_init);
  585. module_exit(hp_sdc_rtc_exit);