2 * cpia_pp CPiA Parallel Port driver
4 * Supports CPiA based parallel port Video Camera's.
6 * (C) Copyright 1999 Bas Huisman <bhuism@cs.utwente.nl>
7 * (C) Copyright 1999-2000 Scott J. Bertin <sbertin@securenym.net>,
8 * (C) Copyright 1999-2000 Peter Pregler <Peter_Pregler@email.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 /* define _CPIA_DEBUG_ for verbose debug output (see cpia.h) */
26 /* #define _CPIA_DEBUG_ 1 */
29 #include <linux/module.h>
30 #include <linux/init.h>
32 #include <linux/kernel.h>
33 #include <linux/parport.h>
34 #include <linux/interrupt.h>
35 #include <linux/delay.h>
36 #include <linux/workqueue.h>
37 #include <linux/smp_lock.h>
38 #include <linux/sched.h>
40 #include <linux/kmod.h>
42 /* #define _CPIA_DEBUG_ define for verbose debug output */
45 static int cpia_pp_open(void *privdata
);
46 static int cpia_pp_registerCallback(void *privdata
, void (*cb
) (void *cbdata
),
48 static int cpia_pp_transferCmd(void *privdata
, u8
*command
, u8
*data
);
49 static int cpia_pp_streamStart(void *privdata
);
50 static int cpia_pp_streamStop(void *privdata
);
51 static int cpia_pp_streamRead(void *privdata
, u8
*buffer
, int noblock
);
52 static int cpia_pp_close(void *privdata
);
55 #define ABOUT "Parallel port driver for Vision CPiA based cameras"
57 #define PACKET_LENGTH 8
59 /* Magic numbers for defining port-device mappings */
60 #define PPCPIA_PARPORT_UNSPEC -4
61 #define PPCPIA_PARPORT_AUTO -3
62 #define PPCPIA_PARPORT_OFF -2
63 #define PPCPIA_PARPORT_NONE -1
66 static int parport_nr
[PARPORT_MAX
] = {[0 ... PARPORT_MAX
- 1] = PPCPIA_PARPORT_UNSPEC
};
67 static char *parport
[PARPORT_MAX
] = {NULL
,};
69 MODULE_AUTHOR("B. Huisman <bhuism@cs.utwente.nl> & Peter Pregler <Peter_Pregler@email.com>");
70 MODULE_DESCRIPTION("Parallel port driver for Vision CPiA based cameras");
71 MODULE_LICENSE("GPL");
73 module_param_array(parport
, charp
, NULL
, 0);
74 MODULE_PARM_DESC(parport
, "'auto' or a list of parallel port numbers. Just like lp.");
76 static int parport_nr
[PARPORT_MAX
] __initdata
=
77 {[0 ... PARPORT_MAX
- 1] = PPCPIA_PARPORT_UNSPEC
};
78 static int parport_ptr
= 0;
82 struct pardevice
*pdev
;
84 struct work_struct cb_task
;
85 void (*cb_func
)(void *cbdata
);
88 wait_queue_head_t wq_stream
;
89 /* image state flags */
90 int image_ready
; /* we got an interrupt */
91 int image_complete
; /* we have seen 4 EOI */
93 int streaming
; /* we are in streaming mode */
97 static struct cpia_camera_ops cpia_pp_ops
=
100 cpia_pp_registerCallback
,
110 static LIST_HEAD(cam_list
);
111 static spinlock_t cam_list_lock_pp
;
114 static void cpia_parport_enable_irq( struct parport
*port
) {
115 parport_enable_irq(port
);
120 static void cpia_parport_disable_irq( struct parport
*port
) {
121 parport_disable_irq(port
);
126 /* Special CPiA PPC modes: These are invoked by using the 1284 Extensibility
127 * Link Flag during negotiation */
128 #define UPLOAD_FLAG 0x08
129 #define NIBBLE_TRANSFER 0x01
130 #define ECP_TRANSFER 0x03
132 #define PARPORT_CHUNK_SIZE PAGE_SIZE
135 static void cpia_pp_run_callback(struct work_struct
*work
)
137 void (*cb_func
)(void *cbdata
);
139 struct pp_cam_entry
*cam
;
141 cam
= container_of(work
, struct pp_cam_entry
, cb_task
);
142 cb_func
= cam
->cb_func
;
143 cb_data
= cam
->cb_data
;
149 /****************************************************************************
151 * CPiA-specific low-level parport functions for nibble uploads
153 ***************************************************************************/
154 /* CPiA nonstandard "Nibble" mode (no nDataAvail signal after each byte). */
155 /* The standard kernel parport_ieee1284_read_nibble() fails with the CPiA... */
157 static size_t cpia_read_nibble (struct parport
*port
,
158 void *buffer
, size_t len
,
161 /* adapted verbatim, with one change, from
162 parport_ieee1284_read_nibble() in drivers/parport/ieee1284-ops.c */
164 unsigned char *buf
= buffer
;
166 unsigned char byte
= 0;
168 len
*= 2; /* in nibbles */
169 for (i
=0; i
< len
; i
++) {
170 unsigned char nibble
;
172 /* The CPiA firmware suppresses the use of nDataAvail (nFault LO)
173 * after every second nibble to signal that more
174 * data is available. (the total number of Bytes that
175 * should be sent is known; if too few are received, an error
176 * will be recorded after a timeout).
177 * This is incompatible with parport_ieee1284_read_nibble(),
178 * which expects to find nFault LO after every second nibble.
181 /* Solution: modify cpia_read_nibble to only check for
182 * nDataAvail before the first nibble is sent.
185 /* Does the error line indicate end of data? */
186 if (((i
/*& 1*/) == 0) &&
187 (parport_read_status(port
) & PARPORT_STATUS_ERROR
)) {
188 DBG("%s: No more nibble data (%d bytes)\n",
193 /* Event 7: Set nAutoFd low. */
194 parport_frob_control (port
,
195 PARPORT_CONTROL_AUTOFD
,
196 PARPORT_CONTROL_AUTOFD
);
198 /* Event 9: nAck goes low. */
199 port
->ieee1284
.phase
= IEEE1284_PH_REV_DATA
;
200 if (parport_wait_peripheral (port
,
201 PARPORT_STATUS_ACK
, 0)) {
202 /* Timeout -- no more data? */
203 DBG("%s: Nibble timeout at event 9 (%d bytes)\n",
205 parport_frob_control (port
, PARPORT_CONTROL_AUTOFD
, 0);
211 nibble
= parport_read_status (port
) >> 3;
213 if ((nibble
& 0x10) == 0)
217 /* Event 10: Set nAutoFd high. */
218 parport_frob_control (port
, PARPORT_CONTROL_AUTOFD
, 0);
220 /* Event 11: nAck goes high. */
221 if (parport_wait_peripheral (port
,
223 PARPORT_STATUS_ACK
)) {
224 /* Timeout -- no more data? */
225 DBG("%s: Nibble timeout at event 11\n",
239 /* Read the last nibble without checking data avail. */
240 if (parport_read_status (port
) & PARPORT_STATUS_ERROR
) {
242 /* Go to reverse idle phase. */
243 parport_frob_control (port
,
244 PARPORT_CONTROL_AUTOFD
,
245 PARPORT_CONTROL_AUTOFD
);
246 port
->physport
->ieee1284
.phase
= IEEE1284_PH_REV_IDLE
;
249 port
->physport
->ieee1284
.phase
= IEEE1284_PH_HBUSY_DAVAIL
;
255 /* CPiA nonstandard "Nibble Stream" mode (2 nibbles per cycle, instead of 1)
256 * (See CPiA Data sheet p. 31)
258 * "Nibble Stream" mode used by CPiA for uploads to non-ECP ports is a
259 * nonstandard variant of nibble mode which allows the same (mediocre)
260 * data flow of 8 bits per cycle as software-enabled ECP by TRISTATE-capable
261 * parallel ports, but works also for non-TRISTATE-capable ports.
262 * (Standard nibble mode only send 4 bits per cycle)
266 static size_t cpia_read_nibble_stream(struct parport
*port
,
267 void *buffer
, size_t len
,
271 unsigned char *buf
= buffer
;
274 for (i
=0; i
< len
; i
++) {
275 unsigned char nibble
[2], byte
= 0;
278 /* Image Data is complete when 4 consecutive EOI bytes (0xff) are seen */
282 /* Event 7: Set nAutoFd low. */
283 parport_frob_control (port
,
284 PARPORT_CONTROL_AUTOFD
,
285 PARPORT_CONTROL_AUTOFD
);
287 /* Event 9: nAck goes low. */
288 port
->ieee1284
.phase
= IEEE1284_PH_REV_DATA
;
289 if (parport_wait_peripheral (port
,
290 PARPORT_STATUS_ACK
, 0)) {
291 /* Timeout -- no more data? */
292 DBG("%s: Nibble timeout at event 9 (%d bytes)\n",
294 parport_frob_control (port
, PARPORT_CONTROL_AUTOFD
, 0);
298 /* Read lower nibble */
299 nibble
[0] = parport_read_status (port
) >>3;
301 /* Event 10: Set nAutoFd high. */
302 parport_frob_control (port
, PARPORT_CONTROL_AUTOFD
, 0);
304 /* Event 11: nAck goes high. */
305 if (parport_wait_peripheral (port
,
307 PARPORT_STATUS_ACK
)) {
308 /* Timeout -- no more data? */
309 DBG("%s: Nibble timeout at event 11\n",
314 /* Read upper nibble */
315 nibble
[1] = parport_read_status (port
) >>3;
317 /* reassemble the byte */
318 for (j
= 0; j
< 2 ; j
++ ) {
320 if ((nibble
[j
] & 0x10) == 0)
324 byte
= (nibble
[0] |(nibble
[1] << 4));
335 /****************************************************************************
339 ***************************************************************************/
340 static void EndTransferMode(struct pp_cam_entry
*cam
)
342 parport_negotiate(cam
->port
, IEEE1284_MODE_COMPAT
);
345 /****************************************************************************
349 ***************************************************************************/
350 static int ForwardSetup(struct pp_cam_entry
*cam
)
354 /* The CPiA uses ECP protocol for Downloads from the Host to the camera.
355 * This will be software-emulated if ECP hardware is not present
358 /* the usual camera maximum response time is 10ms, but after receiving
359 * some commands, it needs up to 40ms. (Data Sheet p. 32)*/
361 for(retry
= 0; retry
< 4; ++retry
) {
362 if(!parport_negotiate(cam
->port
, IEEE1284_MODE_ECP
)) {
368 DBG("Unable to negotiate IEEE1284 ECP Download mode\n");
373 /****************************************************************************
377 ***************************************************************************/
378 static int ReverseSetup(struct pp_cam_entry
*cam
, int extensibility
)
381 int upload_mode
, mode
= IEEE1284_MODE_ECP
;
382 int transfer_mode
= ECP_TRANSFER
;
384 if (!(cam
->port
->modes
& PARPORT_MODE_ECP
) &&
385 !(cam
->port
->modes
& PARPORT_MODE_TRISTATE
)) {
386 mode
= IEEE1284_MODE_NIBBLE
;
387 transfer_mode
= NIBBLE_TRANSFER
;
391 if(extensibility
) mode
= UPLOAD_FLAG
|transfer_mode
|IEEE1284_EXT_LINK
;
393 /* the usual camera maximum response time is 10ms, but after
394 * receiving some commands, it needs up to 40ms. */
396 for(retry
= 0; retry
< 4; ++retry
) {
397 if(!parport_negotiate(cam
->port
, mode
)) {
404 DBG("Unable to negotiate upload extensibility mode\n");
406 DBG("Unable to negotiate upload mode\n");
409 if(extensibility
) cam
->port
->ieee1284
.mode
= upload_mode
;
413 /****************************************************************************
417 ***************************************************************************/
418 static int WritePacket(struct pp_cam_entry
*cam
, const u8
*packet
, size_t size
)
423 if (packet
== NULL
) {
426 if (ForwardSetup(cam
)) {
427 DBG("Write failed in setup\n");
430 size_written
= parport_write(cam
->port
, packet
, size
);
431 if(size_written
!= size
) {
432 DBG("Write failed, wrote %d/%d\n", size_written
, size
);
435 EndTransferMode(cam
);
439 /****************************************************************************
443 ***************************************************************************/
444 static int ReadPacket(struct pp_cam_entry
*cam
, u8
*packet
, size_t size
)
448 if (packet
== NULL
) {
451 if (ReverseSetup(cam
, 0)) {
455 /* support for CPiA variant nibble reads */
456 if(cam
->port
->ieee1284
.mode
== IEEE1284_MODE_NIBBLE
) {
457 if(cpia_read_nibble(cam
->port
, packet
, size
, 0) != size
)
460 if(parport_read(cam
->port
, packet
, size
) != size
)
463 EndTransferMode(cam
);
467 /****************************************************************************
469 * cpia_pp_streamStart
471 ***************************************************************************/
472 static int cpia_pp_streamStart(void *privdata
)
474 struct pp_cam_entry
*cam
= privdata
;
478 //if (ReverseSetup(cam,1)) return -EIO;
479 if(cam
->stream_irq
) cpia_parport_enable_irq(cam
->port
);
483 /****************************************************************************
487 ***************************************************************************/
488 static int cpia_pp_streamStop(void *privdata
)
490 struct pp_cam_entry
*cam
= privdata
;
494 cpia_parport_disable_irq(cam
->port
);
495 //EndTransferMode(cam);
500 /****************************************************************************
504 ***************************************************************************/
505 static int cpia_pp_read(struct parport
*port
, u8
*buffer
, int len
)
509 /* support for CPiA variant "nibble stream" reads */
510 if(port
->ieee1284
.mode
== IEEE1284_MODE_NIBBLE
)
511 bytes_read
= cpia_read_nibble_stream(port
,buffer
,len
,0);
514 for(bytes_read
=0; bytes_read
<len
; bytes_read
+= new_bytes
) {
515 new_bytes
= parport_read(port
, buffer
+bytes_read
,
517 if(new_bytes
< 0) break;
523 static int cpia_pp_streamRead(void *privdata
, u8
*buffer
, int noblock
)
525 struct pp_cam_entry
*cam
= privdata
;
527 int i
, endseen
, block_size
, new_bytes
;
530 DBG("Internal driver error: cam is NULL\n");
534 DBG("Internal driver error: buffer is NULL\n");
537 //if(cam->streaming) DBG("%d / %d\n", cam->image_ready, noblock);
538 if( cam
->stream_irq
) {
539 DBG("%d\n", cam
->image_ready
);
542 cam
->image_complete
=0;
543 if (0/*cam->streaming*/) {
544 if(!cam
->image_ready
) {
545 if(noblock
) return -EWOULDBLOCK
;
546 interruptible_sleep_on(&cam
->wq_stream
);
547 if( signal_pending(current
) ) return -EINTR
;
548 DBG("%d\n", cam
->image_ready
);
551 if (ReverseSetup(cam
, 1)) {
552 DBG("unable to ReverseSetup\n");
557 block_size
= PARPORT_CHUNK_SIZE
;
558 while( !cam
->image_complete
) {
561 new_bytes
= cpia_pp_read(cam
->port
, buffer
, block_size
);
562 if( new_bytes
<= 0 ) {
566 while(++i
<new_bytes
&& endseen
<4) {
576 cam
->image_complete
=1;
579 if( CPIA_MAX_IMAGE_SIZE
-read_bytes
<= PARPORT_CHUNK_SIZE
) {
580 block_size
=CPIA_MAX_IMAGE_SIZE
-read_bytes
;
583 EndTransferMode(cam
);
584 return cam
->image_complete
? read_bytes
: -EIO
;
586 /****************************************************************************
588 * cpia_pp_transferCmd
590 ***************************************************************************/
591 static int cpia_pp_transferCmd(void *privdata
, u8
*command
, u8
*data
)
596 struct pp_cam_entry
*cam
= privdata
;
599 DBG("Internal driver error: cam is NULL\n");
602 if(command
== NULL
) {
603 DBG("Internal driver error: command is NULL\n");
606 databytes
= (((int)command
[7])<<8) | command
[6];
607 if ((err
= WritePacket(cam
, command
, PACKET_LENGTH
)) < 0) {
608 DBG("Error writing command\n");
611 if(command
[0] == DATA_IN
) {
614 DBG("Internal driver error: data is NULL\n");
617 if((err
= ReadPacket(cam
, buffer
, 8)) < 0) {
618 DBG("Error reading command result\n");
621 memcpy(data
, buffer
, databytes
);
622 } else if(command
[0] == DATA_OUT
) {
625 DBG("Internal driver error: data is NULL\n");
628 if((err
=WritePacket(cam
, data
, databytes
)) < 0){
629 DBG("Error writing command data\n");
635 DBG("Unexpected first byte of command: %x\n", command
[0]);
641 /****************************************************************************
645 ***************************************************************************/
646 static int cpia_pp_open(void *privdata
)
648 struct pp_cam_entry
*cam
= (struct pp_cam_entry
*)privdata
;
653 if(cam
->open_count
== 0) {
654 if (parport_claim(cam
->pdev
)) {
655 DBG("failed to claim the port\n");
658 parport_negotiate(cam
->port
, IEEE1284_MODE_COMPAT
);
659 parport_data_forward(cam
->port
);
660 parport_write_control(cam
->port
, PARPORT_CONTROL_SELECT
);
662 parport_write_control(cam
->port
,
663 PARPORT_CONTROL_SELECT
664 | PARPORT_CONTROL_INIT
);
672 /****************************************************************************
674 * cpia_pp_registerCallback
676 ***************************************************************************/
677 static int cpia_pp_registerCallback(void *privdata
, void (*cb
)(void *cbdata
), void *cbdata
)
679 struct pp_cam_entry
*cam
= privdata
;
682 if(cam
->port
->irq
!= PARPORT_IRQ_NONE
) {
684 cam
->cb_data
= cbdata
;
685 INIT_WORK_NAR(&cam
->cb_task
, cpia_pp_run_callback
);
692 /****************************************************************************
696 ***************************************************************************/
697 static int cpia_pp_close(void *privdata
)
699 struct pp_cam_entry
*cam
= privdata
;
700 if (--cam
->open_count
== 0) {
701 parport_release(cam
->pdev
);
706 /****************************************************************************
710 ***************************************************************************/
711 static int cpia_pp_register(struct parport
*port
)
713 struct pardevice
*pdev
= NULL
;
714 struct pp_cam_entry
*cam
;
715 struct cam_data
*cpia
;
717 if (!(port
->modes
& PARPORT_MODE_PCSPP
)) {
718 LOG("port is not supported by CPiA driver\n");
722 cam
= kzalloc(sizeof(struct pp_cam_entry
), GFP_KERNEL
);
724 LOG("failed to allocate camera structure\n");
728 pdev
= parport_register_device(port
, "cpia_pp", NULL
, NULL
,
732 LOG("failed to parport_register_device\n");
739 init_waitqueue_head(&cam
->wq_stream
);
744 if((cpia
= cpia_register_camera(&cpia_pp_ops
, cam
)) == NULL
) {
745 LOG("failed to cpia_register_camera\n");
746 parport_unregister_device(pdev
);
750 spin_lock( &cam_list_lock_pp
);
751 list_add( &cpia
->cam_data_list
, &cam_list
);
752 spin_unlock( &cam_list_lock_pp
);
757 static void cpia_pp_detach (struct parport
*port
)
759 struct list_head
*tmp
;
760 struct cam_data
*cpia
= NULL
;
761 struct pp_cam_entry
*cam
;
763 spin_lock( &cam_list_lock_pp
);
764 list_for_each (tmp
, &cam_list
) {
765 cpia
= list_entry(tmp
, struct cam_data
, cam_data_list
);
766 cam
= (struct pp_cam_entry
*) cpia
->lowlevel_data
;
767 if (cam
&& cam
->port
->number
== port
->number
) {
768 list_del(&cpia
->cam_data_list
);
773 spin_unlock( &cam_list_lock_pp
);
776 DBG("cpia_pp_detach failed to find cam_data in cam_list\n");
780 cam
= (struct pp_cam_entry
*) cpia
->lowlevel_data
;
781 cpia_unregister_camera(cpia
);
782 if(cam
->open_count
> 0)
784 parport_unregister_device(cam
->pdev
);
785 cpia
->lowlevel_data
= NULL
;
789 static void cpia_pp_attach (struct parport
*port
)
793 switch (parport_nr
[0])
795 case PPCPIA_PARPORT_UNSPEC
:
796 case PPCPIA_PARPORT_AUTO
:
797 if (port
->probe_info
[0].class != PARPORT_CLASS_MEDIA
||
798 port
->probe_info
[0].cmdset
== NULL
||
799 strncmp(port
->probe_info
[0].cmdset
, "CPIA_1", 6) != 0)
802 cpia_pp_register(port
);
807 for (i
= 0; i
< PARPORT_MAX
; ++i
) {
808 if (port
->number
== parport_nr
[i
]) {
809 cpia_pp_register(port
);
817 static struct parport_driver cpia_pp_driver
= {
819 .attach
= cpia_pp_attach
,
820 .detach
= cpia_pp_detach
,
823 static int cpia_pp_init(void)
825 printk(KERN_INFO
"%s v%d.%d.%d\n",ABOUT
,
826 CPIA_PP_MAJ_VER
,CPIA_PP_MIN_VER
,CPIA_PP_PATCH_VER
);
828 if(parport_nr
[0] == PPCPIA_PARPORT_OFF
) {
829 printk(" disabled\n");
833 spin_lock_init( &cam_list_lock_pp
);
835 if (parport_register_driver (&cpia_pp_driver
)) {
836 LOG ("unable to register with parport\n");
843 int init_module(void)
846 /* The user gave some parameters. Let's see what they were. */
847 if (!strncmp(parport
[0], "auto", 4)) {
848 parport_nr
[0] = PPCPIA_PARPORT_AUTO
;
851 for (n
= 0; n
< PARPORT_MAX
&& parport
[n
]; n
++) {
852 if (!strncmp(parport
[n
], "none", 4)) {
853 parport_nr
[n
] = PPCPIA_PARPORT_NONE
;
856 unsigned long r
= simple_strtoul(parport
[n
], &ep
, 0);
857 if (ep
!= parport
[n
]) {
860 LOG("bad port specifier `%s'\n", parport
[n
]);
867 return cpia_pp_init();
870 void cleanup_module(void)
872 parport_unregister_driver (&cpia_pp_driver
);
878 static int __init
cpia_pp_setup(char *str
)
882 if (!strncmp(str
, "parport", 7)) {
883 int n
= simple_strtoul(str
+ 7, NULL
, 10);
884 if (parport_ptr
< PARPORT_MAX
) {
885 parport_nr
[parport_ptr
++] = n
;
887 LOG("too many ports, %s ignored.\n", str
);
889 } else if (!strcmp(str
, "auto")) {
890 parport_nr
[0] = PPCPIA_PARPORT_AUTO
;
891 } else if (!strcmp(str
, "none")) {
892 parport_nr
[parport_ptr
++] = PPCPIA_PARPORT_NONE
;
902 __setup("cpia_pp=", cpia_pp_setup
);