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