hp_sdc_rtc.c 20 KB

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