1 /* @file plustek-pp_misc.c
2  * @brief here we have some helpful functions
3 *
4  * based on sources acquired from Plustek Inc.
5  * Copyright (C) 1998 Plustek Inc.
6  * Copyright (C) 2000-2013 Gerhard Jaeger <gerhard@gjaeger.de>
7  * also based on the work done by Rick Bronson
8  *
9  * History:
10  * - 0.30 - initial version
11  * - 0.31 - no changes
12  * - 0.32 - moved the parport functions inside this module
13  *        - now using the information, the parport-driver provides
14  *        - for selecting the port-mode this driver uses
15  * - 0.33 - added code to use faster portmodes
16  * - 0.34 - added sample code for changing from ECP to PS/2 bidi mode
17  * - 0.35 - added Kevins' changes (new function miscSetFastMode())
18  *        - moved function initPageSettings() to module models.c
19  * - 0.36 - added random generator
20  *        - added additional debug messages
21  *        - changed prototype of MiscInitPorts()
22  *        - added miscPreemptionCallback()
23  * - 0.37 - changed inb_p/outb_p to macro calls (kernel-mode)
24  *        - added MiscGetModelName()
25  *        - added miscShowPortModes()
26  * - 0.38 - fixed a small bug in MiscGetModelName()
27  * - 0.39 - added forceMode support
28  * - 0.40 - no changes
29  * - 0.41 - merged Kevins' patch to make EPP(ECP) work
30  * - 0.42 - changed get_fast_time to _GET_TIME
31  *        - changed include names
32  * - 0.43 - added LINUX_26 stuff
33  *        - minor fixes
34  *        - removed floating point stuff
35  * - 0.44 - fix format string issues, as Long types default to int32_t
36  *          now
37  * .
38  * <hr>
39  * This file is part of the SANE package.
40  *
41  * This program is free software; you can redistribute it and/or
42  * modify it under the terms of the GNU General Public License as
43  * published by the Free Software Foundation; either version 2 of the
44  * License, or (at your option) any later version.
45  *
46  * This program is distributed in the hope that it will be useful, but
47  * WITHOUT ANY WARRANTY; without even the implied warranty of
48  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
49  * General Public License for more details.
50  *
51  * You should have received a copy of the GNU General Public License
52  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
53  *
54  * As a special exception, the authors of SANE give permission for
55  * additional uses of the libraries contained in this release of SANE.
56  *
57  * The exception is that, if you link a SANE library with other files
58  * to produce an executable, this does not by itself cause the
59  * resulting executable to be covered by the GNU General Public
60  * License.  Your use of that executable is in no way restricted on
61  * account of linking the SANE library code into it.
62  *
63  * This exception does not, however, invalidate any other reasons why
64  * the executable file might be covered by the GNU General Public
65  * License.
66  *
67  * If you submit changes to SANE to the maintainers to be included in
68  * a subsequent release, you agree by submitting the changes that
69  * those changes may be distributed with this exception intact.
70  *
71  * If you write modifications of your own for SANE, it is your choice
72  * whether to permit this exception to apply to your modifications.
73  * If you do not wish that, delete this exception notice.
74  * <hr>
75  */
76 #include "plustek-pp_scan.h"
77 
78 /*************************** some definitions ********************************/
79 
80 #ifndef __KERNEL__
81 # define PPA_PROBE_SPP   0x0001
82 # define PPA_PROBE_PS2   0x0002
83 # define PPA_PROBE_ECR   0x0010
84 # define PPA_PROBE_EPP17 0x0100
85 # define PPA_PROBE_EPP19 0x0200
86 #else
87 
88 /* the parport driver in Kernel 2.4 has changed. It does report the
89  * possible modes in a different, more general way. As long, as
90  * we do not use the parport-module change mode facility, I assume
91  * the following correlations
92  */
93 #if defined LINUX_24 || defined LINUX_26
94 # define PARPORT_MODE_PCPS2     PARPORT_MODE_TRISTATE
95 # define PARPORT_MODE_PCEPP     PARPORT_MODE_EPP
96 # define PARPORT_MODE_PCECPPS2  PARPORT_MODE_TRISTATE
97 # define PARPORT_MODE_PCECPEPP  PARPORT_MODE_EPP
98 # define PARPORT_MODE_PCECR     PARPORT_MODE_ECP
99 #endif
100 #endif
101 
102 #define _PP_A 16807         /**< multiplier */
103 #define _PP_M 2147483647L   /**< 2**31 - 1  */
104 
105 /*************************** some local vars *********************************/
106 
107 static int  port_feature = 0;
108 static long randomnum    = 1;
109 
110 #ifdef __KERNEL__
111 static int portIsClaimed[_MAX_PTDEVS] = { [0 ... (_MAX_PTDEVS-1)] = 0 };
112 
113 MODELSTR;	/**< a static char array (see plustek-pp.h) */
114 
115 #else
116 static int portIsClaimed[_MAX_PTDEVS] = { 0, 0, 0, 0 };
117 #endif
118 
119 /*************************** local functions *********************************/
120 
121 #ifdef __KERNEL__
122 #ifdef LINUX_26
123 
124 static pScanData __ps = NULL;
125 static int       __pa = -1;
126 
127 /** callback from parport driver
128  */
misc_attach(struct parport * port)129 static void misc_attach(struct parport *port)
130 {
131 	DBG( DBG_LOW, "misc_attach\n" );
132 
133 	__ps->pp = NULL;
134 	if( port->base == (unsigned long)__pa ) {
135 		DBG( DBG_LOW, "Requested port (0x%02x) found\n", __pa );
136 		DBG( DBG_LOW, "Port mode reported: (0x%04x)\n",  port->modes );
137 		__ps->pp = port;
138 	}
139 }
140 
misc_detach(struct parport * port)141 static void misc_detach( struct parport *port )
142 {
143 	DBG( DBG_LOW, "misc_detach\n" );
144 }
145 
146 static struct parport_driver pt_drv = {
147 	.name   = "pt_drv",
148 	.attach = misc_attach,
149 	.detach = misc_detach,
150 };
151 #endif
152 
153 /** display the available port-modes
154  */
155 #ifdef DEBUG
miscShowPortModes(int modes)156 static void miscShowPortModes( int modes )
157 {
158 	DBG( DBG_LOW, "parport-modi:" );
159 
160 	if( modes & PARPORT_MODE_PCSPP )
161 		DBG( DBG_LOW, " SPP" );
162 
163 	if( modes & PARPORT_MODE_PCPS2 )
164 		DBG( DBG_LOW, " PS/2" );
165 
166 	if( modes & PARPORT_MODE_PCEPP )
167 		DBG( DBG_LOW, " EPP" );
168 
169 	if( modes & PARPORT_MODE_PCECR )
170 		DBG( DBG_LOW, " ECP" );
171 
172 	if( modes & PARPORT_MODE_PCECPEPP )
173 		DBG( DBG_LOW, " EPP(ECP)" );
174 
175 	if( modes & PARPORT_MODE_PCECPPS2 )
176 		DBG( DBG_LOW, " PS/2(ECP)" );
177 
178 	DBG( DBG_LOW, "\n" );
179 }
180 #endif
181 
182 /** probe the parallel port
183  */
initPortProbe(pScanData ps)184 static int initPortProbe( pScanData ps )
185 {
186     int retv = 0;
187 
188 	/* clear the controls */
189 	ps->IO.lastPortMode = 0xFFFF;
190 
191 	if( NULL != ps->pardev )
192 		retv = ps->pardev->port->modes;
193     return retv;
194 }
195 
196 /** will be called by the parport module when we already have access, but
197  * another module wants access to the port...
198  */
miscPreemptionCallback(pVoid data)199 static int miscPreemptionCallback( pVoid data )
200 {
201 	pScanData ps = (pScanData)data;
202 
203 	if( NULL != ps ) {
204 
205 		/* never release during scanning */
206 		if( ps->DataInf.dwScanFlag & _SCANNER_SCANNING ) {
207 			DBG( DBG_LOW, "no way!!!\n" );
208 			return 1;
209 		}
210 	}
211 
212 	/* let the port go...*/
213 	return 0;
214 }
215 
216 /** depending on the reported possible port modes, we try to set a faster mode
217  * than SPP
218  */
miscSetFastMode(pScanData ps)219 static int miscSetFastMode( pScanData ps )
220 {
221 	UChar a, b;
222 
223 	/*
224 	 *  when previously found the EPP mode, break right here
225 	 */
226 	if (( _PORT_EPP == ps->IO.portMode ) && (!(port_feature & PARPORT_MODE_PCECR)))
227  		return _OK;
228 
229  	/* CHECK REMOVE: from here we should have SPP (Paranoia Code !) */
230 	if (( _PORT_SPP != ps->IO.portMode ) && (!(port_feature & PARPORT_MODE_PCECR)))
231  		return _OK;
232 
233 	DBG(DBG_LOW, "Trying faster mode...\n" );
234 
235 	/*
236 	 * ECP mode usually has sub-modes of EPP and/or PS2.
237 	 * First we try to set EPP
238 	 */
239     if((port_feature & PARPORT_MODE_PCECR) &&
240 									(port_feature & PARPORT_MODE_PCECPEPP)){
241 
242         DBG(DBG_LOW, "Attempting to set EPP from ECP mode.\n" );
243 
244         a = _INB_ECTL(ps);  				/* get current ECR				*/
245 		ps->IO.lastPortMode = a;	     	/* save it for restoring later	*/
246         a = (a & 0x1F) | 0x80;  	     	/* set to EPP					*/
247         _OUTB_ECTL(ps, a);					/* write it back				*/
248 		_DO_UDELAY(1);
249 
250 		/*
251 		 * It is probably unnecessary to
252 		 * do this check but it makes me feel better
253 		 */
254         b = _INB_ECTL(ps);					/* check to see if port set */
255         if( a == b ) {
256             DBG( DBG_LOW, "Port is set to (ECP) EPP mode.\n" );
257             ps->IO.portMode = _PORT_EPP;
258 			return _OK;
259 
260         } else {
261             DBG( DBG_LOW, "Port could not be set to (ECP) EPP mode. "
262 														"Using SPP mode.\n" );
263             _OUTB_ECTL(ps,(Byte)ps->IO.lastPortMode); 		/* restore */
264 			_DO_UDELAY(1);
265 		    ps->IO.portMode = _PORT_SPP;
266 
267 			/* go ahead and try with other settings...*/
268         }
269     }
270 
271 	/* If port cannot be set to EPP, try PS2 */
272     if((port_feature & PARPORT_MODE_PCECR) &&
273 									(port_feature & PARPORT_MODE_PCECPPS2)) {
274 
275         DBG(DBG_LOW, "Attempting to set PS2 from ECPPS2 mode.\n" );
276 
277         a = _INB_ECTL(ps);  				/* get current ECR				*/
278 		ps->IO.lastPortMode = a;	     	/* save it for restoring later 	*/
279 
280 		/* set to Fast Centronics/bi-directional/PS2 */
281         a = (a & 0x1F) | 0x20;
282         _OUTB_ECTL(ps,a);					/* write it back */
283 		_DO_UDELAY(1);
284 
285 		/*
286 		 * It is probably unnecessary to do this check
287 		 * but it makes me feel better
288 		 */
289         b = _INB_ECTL(ps);					/* check to see if port set */
290         if (a == b) {
291             DBG(DBG_LOW, "Port is set to (ECP) PS2 bidirectional mode.\n");
292             ps->IO.portMode = _PORT_BIDI;
293 			return _OK;
294         } else {
295         	DBG(DBG_LOW, "Port could not be set to (ECP) PS2 mode. "
296 														"Using SPP mode.\n");
297 			a = ps->IO.lastPortMode & 0x1F;
298             _OUTB_ECTL(ps, a);					/* set ECP ctrl to SPP */
299 			_DO_UDELAY(1);
300 		    ps->IO.portMode = _PORT_SPP;
301 
302 			/* next mode, last attempt... */
303         }
304 	}
305 
306 	/*
307 	 * Some BIOS/cards have only a Bi-directional/PS2 mode (no EPP).
308 	 * Make one last attempt to set to PS2 mode.
309 	 */
310 	if ( port_feature & PARPORT_MODE_PCPS2 ){
311 
312 		DBG(DBG_LOW, "Attempting to set PS2 mode.\n" );
313 
314 		a = _INB_CTRL(ps); 		    /* get current setting of control register*/
315 		ps->IO.lastPortMode = a;	/* save it for restoring later            */
316 		a = a | 0x20;  			    /* set bit 5 of control reg              */
317 		_OUTB_CTRL(ps,a); 		/* set to Fast Centronics/bi-directional/PS2 */
318 		_DO_UDELAY(1);
319 		a = 0;
320 
321 		_OUTB_DATA(ps,0x55);
322 		_DO_UDELAY(1);
323 		if ((inb(ps->IO.portBase)) != 0x55)	/* read data */
324 			a++;
325 
326 		_OUTB_DATA(ps,0xAA);
327 		_DO_UDELAY(1);
328 
329 		if (_INB_DATA(ps) != 0xAA)   /* read data */
330 			a++;
331 
332 		if( 2 == a ) {
333 			DBG(DBG_LOW, "Port is set to PS2 bidirectional mode.\n");
334 			ps->IO.portMode = _PORT_BIDI;
335 			return _OK;
336 
337 		} else {
338 			DBG(DBG_LOW, "Port could not be set to PS2 mode. "
339 														"Using SPP mode.\n");
340             _OUTB_CTRL(ps,(Byte)ps->IO.lastPortMode);		/* restore */
341 			_DO_UDELAY(1);
342 			ps->IO.portMode = _PORT_SPP;
343 		}
344 	}
345 
346 	/* reaching this point, we're back in SPP mode and there's no need
347 	 * to restore at shutdown...
348 	 */
349 	ps->IO.lastPortMode = 0xFFFF;
350 
351 	return _OK;
352 }
353 
354 /** check the state of the par-port and switch to EPP-mode if possible
355  */
miscSetPortMode(pScanData ps)356 static int miscSetPortMode( pScanData ps )
357 {
358 	/* try to detect the port settings, SPP seems to work in any case ! */
359 	port_feature = initPortProbe( ps );
360 
361 #ifdef DEBUG
362 	miscShowPortModes( port_feature );
363 #endif
364 
365 	switch( ps->IO.forceMode ) {
366 
367 		case 1:
368 			DBG( DBG_LOW, "Use of SPP-mode enforced\n" );
369 			ps->IO.portMode = _PORT_SPP;
370 			return _OK;
371 			break;
372 
373         case 2:
374 	    	DBG( DBG_LOW, "Use of EPP-mode enforced\n" );
375 	        ps->IO.portMode = _PORT_EPP;
376     		return _OK;
377             break;
378 
379         default:
380             break;
381 	}
382 
383 	if( !(port_feature & PARPORT_MODE_PCEPP)) {
384 
385 		if( !(port_feature & PARPORT_MODE_PCSPP )) {
386 			_PRINT("\nThis Port supports not the  SPP- or EPP-Mode\n" );
387 			_PRINT("Please activate SPP-Mode, EPP-Mode or\nEPP + ECP-Mode!\n");
388 			return _E_NOSUPP;
389 		} else {
390 			DBG(DBG_LOW, "Using SPP-mode\n" );
391 		    ps->IO.portMode = _PORT_SPP;
392 		}
393     } else {
394 		DBG(DBG_LOW, "Using EPP-mode\n" );
395 	    ps->IO.portMode = _PORT_EPP;
396 	}
397 
398 	/* else try to set to a faster mode than SPP */
399 	return miscSetFastMode( ps );
400 }
401 #endif
402 
403 /** miscNextLongRand() -- generate 2**31-2 random numbers
404 **
405 **  public domain by Ray Gardner
406 **
407 **  based on "Random Number Generators: Good Ones Are Hard to Find",
408 **  S.K. Park and K.W. Miller, Communications of the ACM 31:10 (Oct 1988),
409 **  and "Two Fast Implementations of the 'Minimal Standard' Random
410 **  Number Generator", David G. Carta, Comm. ACM 33, 1 (Jan 1990), p. 87-88
411 **
412 **  linear congruential generator f(z) = 16807 z mod (2 ** 31 - 1)
413 **
414 **  uses L. Schrage's method to avoid overflow problems
415 */
miscNextLongRand(Long seed)416 static Long miscNextLongRand( Long seed )
417 {
418 	ULong lo, hi;
419 
420     lo = _PP_A * (Long)(seed & 0xFFFF);
421     hi = _PP_A * (Long)((ULong)seed >> 16);
422     lo += (hi & 0x7FFF) << 16;
423     if (lo > _PP_M) {
424 
425 		lo &= _PP_M;
426         ++lo;
427 	}
428     lo += hi >> 15;
429     if (lo > _PP_M) {
430 		lo &= _PP_M;
431         ++lo;
432 	}
433 
434 	return (Long)lo;
435 }
436 
437 /** initialize the random number generator
438  */
miscSeedLongRand(long seed)439 static void miscSeedLongRand( long seed )
440 {
441 	randomnum = seed ? (seed & _PP_M) : 1;  /* nonzero seed */
442 }
443 
444 /************************ exported functions *********************************/
445 
446 /** allocate and initialize some memory for the scanner structure
447  */
MiscAllocAndInitStruct(void)448 _LOC pScanData MiscAllocAndInitStruct( void )
449 {
450 	pScanData ps;
451 
452 	ps = (pScanData)_KALLOC(sizeof(ScanData), GFP_KERNEL);
453 
454 	if( NULL != ps ) {
455 		MiscReinitStruct( ps );
456 	}
457 
458 	DBG( DBG_HIGH, "ScanData = 0x%08lx\n", (unsigned long)ps );
459 	return ps;
460 }
461 
462 /** re-initialize the memory for the scanner structure
463  */
MiscReinitStruct(pScanData ps)464 _LOC int MiscReinitStruct( pScanData ps )
465 {
466 	if( NULL == ps )
467 		return _E_NULLPTR;
468 
469 	memset( ps, 0, sizeof(ScanData));
470 
471 	/* first init all constant stuff in ScanData
472 	 */
473 	ps->bCurrentSpeed = 1;
474 	ps->pbMapRed      =  ps->a_bMapTable;
475 	ps->pbMapGreen    = &ps->a_bMapTable[256];
476 	ps->pbMapBlue     = &ps->a_bMapTable[512];
477 	ps->sCaps.wIOBase = _NO_BASE;
478 
479 	/* use memory address to seed the generator */
480 	miscSeedLongRand((long)ps);
481 
482 	DBG( DBG_HIGH, "Init settings done\n" );
483 	return _OK;
484 }
485 
486 /** in USER-Mode:   probe the specified port and try to get the port-mode
487  *  in KERNEL-Mode: only use the modes, the driver returns
488  */
MiscInitPorts(pScanData ps,int port)489 _LOC int MiscInitPorts( pScanData ps, int port )
490 {
491 #ifdef __KERNEL__
492 	int status;
493 
494 	if( NULL == ps )
495 		return _E_NULLPTR;
496 
497     /*
498      * Get access to the ports
499      */
500     ps->IO.portBase = (UShort)port;
501 
502 	status = miscSetPortMode(ps);
503 
504 	if( _OK != status ) {
505 		ps->sCaps.wIOBase = _NO_BASE;
506 		ps->IO.portBase = _NO_BASE;
507 		return status;
508 	}
509 
510 	/*
511  	 * the port settings
512 	 */
513     ps->IO.pbSppDataPort = (UShort)port;
514     ps->IO.pbStatusPort  = (UShort)port+1;
515     ps->IO.pbControlPort = (UShort)port+2;
516     ps->IO.pbEppDataPort = (UShort)port+4;
517 
518 #else
519 	int mode, mts;
520 
521 	if( NULL == ps )
522 		return _E_NULLPTR;
523 
524 	if( SANE_STATUS_GOOD != sanei_pp_getmodes( ps->pardev, &mode )) {
525 		DBG( DBG_HIGH, "Cannot get port mode!\n" );
526 		return _E_NO_PORT;
527 	}
528 
529 	ps->IO.portMode = _PORT_NONE;
530 	mts             = -1;
531 	if( mode & SANEI_PP_MODE_SPP ) {
532 		DBG( DBG_LOW, "Setting SPP-mode\n" );
533 		ps->IO.portMode = _PORT_SPP;
534 		mts = SANEI_PP_MODE_SPP;
535 	}
536 	if( mode & SANEI_PP_MODE_BIDI ) {
537 		DBG( DBG_LOW, "Setting PS/2-mode\n" );
538 		ps->IO.portMode = _PORT_BIDI;
539 		mts = SANEI_PP_MODE_BIDI;
540 	}
541 	if( mode & SANEI_PP_MODE_EPP ) {
542 		DBG( DBG_LOW, "Setting EPP-mode\n" );
543 		ps->IO.portMode = _PORT_EPP;
544 		mts = SANEI_PP_MODE_EPP;
545 	}
546 	if( mode & SANEI_PP_MODE_ECP ) {
547 		DBG( DBG_HIGH, "ECP detected --> not supported\n" );
548 	}
549 
550 	if( sanei_pp_uses_directio()) {
551 		DBG( DBG_LOW, "We're using direct I/O\n" );
552 	} else {
553 		DBG( DBG_LOW, "We're using libIEEE1284 I/O\n" );
554 	}
555 
556 	if( ps->IO.portMode == _PORT_NONE ) {
557 		DBG( DBG_HIGH, "None of the portmodes is supported.\n" );
558 		return _E_NOSUPP;
559 	}
560 
561 	sanei_pp_setmode( ps->pardev, mts );
562 	_VAR_NOT_USED( port );
563 #endif
564 	return _OK;
565 }
566 
567 /** Function to restore the port
568  */
MiscRestorePort(pScanData ps)569 _LOC void MiscRestorePort( pScanData ps )
570 {
571 #ifdef __KERNEL__
572 	if( 0 == ps->IO.pbSppDataPort )
573 		return;
574 #endif
575 
576     DBG(DBG_LOW,"MiscRestorePort()\n");
577 
578 	/* don't restore if not necessary */
579 	if( 0xFFFF == ps->IO.lastPortMode ) {
580 	    DBG(DBG_LOW,"- no need to restore portmode !\n");
581 		return;
582 	}
583 
584     /*Restore Port-Mode*/
585 #ifdef __KERNEL__
586 	if( port_feature & PARPORT_MODE_PCECR ){
587 		_OUTB_ECTL( ps, (Byte)ps->IO.lastPortMode );
588 		_DO_UDELAY(1);
589     } else {
590 		_OUTB_CTRL( ps, (Byte)ps->IO.lastPortMode );
591 		_DO_UDELAY(1);
592     }
593 #else
594     if( port_feature & PPA_PROBE_ECR ){
595 		_OUTB_ECTL(ps,ps->IO.lastPortMode);
596     }
597 #endif
598 }
599 
600 /** Initializes a timer.
601  * @param timer - pointer to the timer to start
602  * @param us    - timeout value in micro-seconds
603  */
MiscStartTimer(TimerDef * timer,unsigned long us)604 _LOC void MiscStartTimer( TimerDef *timer , unsigned long us)
605 {
606     struct timeval start_time;
607 
608 #ifdef __KERNEL__
609 	_GET_TIME( &start_time );
610 #else
611 	gettimeofday(&start_time, NULL);
612 #endif
613 
614     *timer = (TimerDef)start_time.tv_sec * 1000000 + (TimerDef)start_time.tv_usec + us;
615 }
616 
617 /** Checks if a timer has been expired or not. In Kernel-mode, the scheduler
618  *  will also be triggered, if the timer has not been expired.
619  * @param timer - pointer to the timer to check
620  * @return Function returns _E_TIMEOUT when the timer has been expired,
621  *         otherwise _OK;
622  */
MiscCheckTimer(TimerDef * timer)623 _LOC int MiscCheckTimer( TimerDef *timer )
624 {
625     struct timeval current_time;
626 
627 #ifdef __KERNEL__
628 	_GET_TIME( &current_time );
629 #else
630 	gettimeofday(&current_time, NULL);
631 #endif
632 
633     if ((TimerDef)current_time.tv_sec * 1000000 + (TimerDef)current_time.tv_usec > *timer) {
634 		return _E_TIMEOUT;
635     } else {
636 #ifdef __KERNEL__
637 		schedule();
638 /*#else
639 		sched_yield();
640 */
641 #endif
642 		return _OK;
643 	}
644 }
645 
646 /** Checks the function pointers
647  * @param ps - pointer to the scanner data structure.
648  * @return Function returns _TRUE if everything is okay and _FALSE if a NULL
649  *         ptr has been detected.
650  */
651 #ifdef DEBUG
MiscAllPointersSet(pScanData ps)652 _LOC Bool MiscAllPointersSet( pScanData ps )
653 {
654 	int  i;
655 	unsigned long *ptr;
656 
657 	for( ptr = (unsigned long *)&ps->OpenScanPath, i = 1;
658 		 ptr <= (unsigned long *)&ps->ReadOneImageLine; ptr++, i++ ) {
659 
660 		if( NULL == (pVoid)*ptr ) {
661 			DBG( DBG_HIGH, "Function pointer not set (pos = %d) !\n", i );
662 			return _FALSE;
663 		}
664 	}
665 
666 	return _TRUE;
667 }
668 #endif
669 
670 /** registers this driver to use port "portAddr" (KERNEL-Mode only)
671  * @param ps       - pointer to the scanner data structure.
672  * @param portAddr -
673  */
MiscRegisterPort(pScanData ps,int portAddr)674 _LOC int MiscRegisterPort( pScanData ps, int portAddr )
675 {
676 #ifndef __KERNEL__
677 	DBG( DBG_LOW, "Assigning port handle %i\n", portAddr );
678     ps->pardev = portAddr;
679 #else
680 
681 #ifdef LINUX_26
682 	__ps = ps;
683 	__pa = portAddr;
684 
685 	DBG( DBG_LOW, "Requested port at 0x%02x\n", portAddr );
686 
687 	if( parport_register_driver(&pt_drv)) {
688 		/* Failed; nothing we can do. */
689 		return _E_REGISTER;
690 	}
691 
692 #else
693 	struct parport *pp = NULL;
694 
695 	DBG( DBG_LOW, "Requested port at 0x%02x\n", portAddr );
696 
697 	pp         = parport_enumerate();
698 	ps->pardev = NULL;
699 
700 	if( NULL == pp ) {
701 		return _E_PORTSEARCH;
702 	}
703 
704 	/* go through the list
705 	 */
706 	for( ps->pp = NULL; NULL != pp; ) {
707 
708 		if( pp->base == (unsigned long)portAddr ) {
709 			DBG( DBG_LOW, "Requested port (0x%02x) found\n", portAddr );
710 			DBG( DBG_LOW, "Port mode reported: (0x%04x)\n",  pp->modes );
711 			ps->pp = pp;
712 			break;
713 		}
714 		pp = pp->next;
715 	}
716 #endif
717 
718 	if( NULL == ps->pp ) {
719 		printk("PORT not found!!!\n");
720 		return _E_NO_PORT;
721 	}
722 
723 	/*
724 	 * register this device
725 	 */
726 	ps->pardev = parport_register_device( ps->pp, "Plustek Driver",
727 	                    miscPreemptionCallback, NULL, NULL, 0, (pVoid)ps );
728 
729 	if( NULL == ps->pardev ) {
730 		return _E_REGISTER;
731 	}
732 
733 	DBG( DBG_LOW, "Port for device %u registered\n", ps->devno );
734 #endif
735 
736 	portIsClaimed[ps->devno] = 0;
737 	return _OK;
738 }
739 
740 /** unregisters the port from driver
741  */
MiscUnregisterPort(pScanData ps)742 _LOC void MiscUnregisterPort( pScanData ps )
743 {
744 #ifdef __KERNEL__
745 	if( NULL != ps->pardev ) {
746 		DBG( DBG_LOW, "Port unregistered\n" );
747 		parport_unregister_device( ps->pardev );
748 	}
749 #ifdef LINUX_26
750 	parport_unregister_driver( &pt_drv );
751 #endif
752 #else
753 	sanei_pp_close( ps->pardev );
754 #endif
755 }
756 
757 /** Try to claim the port
758  * @param ps - pointer to the scanner data structure.
759  * @return Function returns _OK on success, otherwise _E_BUSY.
760  */
MiscClaimPort(pScanData ps)761 _LOC int MiscClaimPort( pScanData ps )
762 {
763 	if( 0 == portIsClaimed[ps->devno] ) {
764 
765 		DBG( DBG_HIGH, "Try to claim the parport\n" );
766 #ifdef __KERNEL__
767 		if( 0 != parport_claim( ps->pardev )) {
768 #else
769 		if( SANE_STATUS_GOOD != sanei_pp_claim( ps->pardev )) {
770 #endif
771 			return _E_BUSY;
772 		}
773 	}
774 	portIsClaimed[ps->devno]++;
775 	return _OK;
776 }
777 
778 /** Release previously claimed port
779  * @param ps - pointer to the scanner data structure
780  */
781 _LOC void MiscReleasePort( pScanData ps )
782 {
783 	if( portIsClaimed[ps->devno] > 0 ) {
784 		portIsClaimed[ps->devno]--;
785 
786 		if( 0 == portIsClaimed[ps->devno] ) {
787 			DBG( DBG_HIGH, "Releasing parport\n" );
788 #ifdef __KERNEL__
789 			parport_release( ps->pardev );
790 #else
791 			sanei_pp_release( ps->pardev );
792 #endif
793 		}
794 	}
795 }
796 
797 /** Get random number
798  * @return a random number.
799  */
800 _LOC Long MiscLongRand( void )
801 {
802 	randomnum = miscNextLongRand( randomnum );
803 
804 	return randomnum;
805 }
806 
807 /** According to the id, the function returns a pointer to the model name
808  * @param id - internal id of the various scanner models.
809  * @return a pointer to the model-string.
810  */
811 _LOC const char *MiscGetModelName( UShort id )
812 {
813 	DBG( DBG_HIGH, "MiscGetModelName - id = %i\n", id );
814 
815 	if( MODEL_OP_PT12 < id )
816 		return ModelStr[0];
817 
818 	return ModelStr[id];
819 }
820 
821 /* END PLUSTEK-PP_MISC.C ....................................................*/
822