home *** CD-ROM | disk | FTP | other *** search
- From: aakash@hpcupt3.cup.hp.com (Aakash Sahai)
- Date: Wed, 26 Aug 1992 18:14:19 GMT
- Subject: Re: How to sleep in device driver with a time out
- Message-ID: <46520024@hpcupt3.cup.hp.com>
- Organization: Hewlett Packard, Cupertino
- Path: sparky!uunet!usc!sdd.hp.com!hpscdc!hplextra!hpcc05!hpcuhb!hpcupt3!aakash
- Newsgroups: comp.unix.internals
- References: <1992Aug24.024748.865@candle.uucp>
- Lines: 97
-
- > I assume you call timeout(function_address, int_parameter, time_in_ms)
- > and then go to sleep(). If the timeout period expires, the function
- > gets called with your parameter and you can return to user mode normally.
- > Is this correct? Do I have the parameters correct?
-
- The last argument to timeout is generally specified in terms of HZ parameter.
- where HZ = number of ticks per second generated by timer. For example, if HZ
- equals 100 and you specify the last parameter as 7 then the timeout will occur
- after 7/100 th of a second ( 70 ms ).
-
- Normally, in such condition when an operation has to be aborted mid-way because
- of timeout, a code sequence of the following form can be used -
-
- /* Routine called by timeout. */
-
- int ErrorOccurred;
-
- OpnTimedOut(SleepAddr)
- caddr_t SleepAddr;
- {
- ErrorOccurred++;
- wakeup (SleepAddr);
- }
-
- ...
- /* Routine doing actual I/O and setting the timeout */
- {
- ...
-
- /* Start the actual I/O */
- /* Block device interrupts */
- if ( ! I/O Done ) { /* No need to sleep if we have already got */
- /* an interrupt quick enough */
- ErrorOccurred = 0;
- /* Block Clock interrupts */
- id = timeout ( OpnTimedOut, SleepAddr, timeout_value );
- sleep ( SleepAddr, priority );
- if ( ! ErrorOcurred ) { /* Device responded and we were */
- /* woken up by ISR */
- untimeout ( id );
- /* Unblock clock and device interrupts */
- /* Do the normal processing */
- } else {
- ErrorOcurred = 0;
- /* Unblock clock interrupt */
- /* Code to reset the controller etc. */
- /* Unblock device interrupt */
- /* Return error etc.*/
- }
- }
- ....
- }
-
- NOTE : This is just a skeleton and actual code may vary depending on the
- capability of the controller and/or driver design style.
-
- >
- > Also, how do I return a status to a C program from a device driver
- > ioctl function?
- >
-
- Probably, you can declare a structure and pass a pointer to this structure
- from the user level program as the third argument to ioctl. Then in your
- driver you can do a "copyout()" of the status to the area pointed to by
- this pointer.
-
- That is -
-
- /* User program */
- struct status ret_status;
-
- ....
- ....
-
- ret = ioctl ( fd, cmd, &ret_status );
-
- ...
-
- /* Kernel code in the driver */
-
- dev_ioctl ( dev, cmd, arg )
- {
- struct status dev_status;
- ....
- ....
-
- copyout (dev_status, (struct status *)arg, sizeof(struct status));
-
- ....
- }
-
-
- -- Aakash Sahai
-
- /*
- * Yeah, you are right - HP is not responsible for what I write.
- */
-