hp_sdc_rtc.c 20 KB

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