Merge rsync://rsync.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[linux-2.6] / drivers / media / video / cpia_pp.c
1 /*
2  * cpia_pp CPiA Parallel Port driver
3  *
4  * Supports CPiA based parallel port Video Camera's.
5  *
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>
9  *
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.
14  *
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.
19  *
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.
23  */
24
25 /* define _CPIA_DEBUG_ for verbose debug output (see cpia.h) */
26 /* #define _CPIA_DEBUG_  1 */
27
28 #include <linux/config.h>
29
30 #include <linux/module.h>
31 #include <linux/init.h>
32
33 #include <linux/kernel.h>
34 #include <linux/parport.h>
35 #include <linux/interrupt.h>
36 #include <linux/delay.h>
37 #include <linux/workqueue.h>
38 #include <linux/smp_lock.h>
39 #include <linux/sched.h>
40
41 #include <linux/kmod.h>
42
43 /* #define _CPIA_DEBUG_         define for verbose debug output */
44 #include "cpia.h"
45
46 static int cpia_pp_open(void *privdata);
47 static int cpia_pp_registerCallback(void *privdata, void (*cb) (void *cbdata),
48                                     void *cbdata);
49 static int cpia_pp_transferCmd(void *privdata, u8 *command, u8 *data);
50 static int cpia_pp_streamStart(void *privdata);
51 static int cpia_pp_streamStop(void *privdata);
52 static int cpia_pp_streamRead(void *privdata, u8 *buffer, int noblock);
53 static int cpia_pp_close(void *privdata);
54
55
56 #define ABOUT "Parallel port driver for Vision CPiA based cameras"
57
58 #define PACKET_LENGTH  8
59
60 /* Magic numbers for defining port-device mappings */
61 #define PPCPIA_PARPORT_UNSPEC -4
62 #define PPCPIA_PARPORT_AUTO -3
63 #define PPCPIA_PARPORT_OFF -2
64 #define PPCPIA_PARPORT_NONE -1
65
66 #ifdef MODULE
67 static int parport_nr[PARPORT_MAX] = {[0 ... PARPORT_MAX - 1] = PPCPIA_PARPORT_UNSPEC};
68 static char *parport[PARPORT_MAX] = {NULL,};
69
70 MODULE_AUTHOR("B. Huisman <bhuism@cs.utwente.nl> & Peter Pregler <Peter_Pregler@email.com>");
71 MODULE_DESCRIPTION("Parallel port driver for Vision CPiA based cameras");
72 MODULE_LICENSE("GPL");
73
74 module_param_array(parport, charp, NULL, 0);
75 MODULE_PARM_DESC(parport, "'auto' or a list of parallel port numbers. Just like lp.");
76 #else
77 static int parport_nr[PARPORT_MAX] __initdata =
78         {[0 ... PARPORT_MAX - 1] = PPCPIA_PARPORT_UNSPEC};
79 static int parport_ptr = 0;
80 #endif
81
82 struct pp_cam_entry {
83         struct pardevice *pdev;
84         struct parport *port;
85         struct work_struct cb_task;
86         int open_count;
87         wait_queue_head_t wq_stream;
88         /* image state flags */
89         int image_ready;        /* we got an interrupt */
90         int image_complete;     /* we have seen 4 EOI */
91
92         int streaming; /* we are in streaming mode */
93         int stream_irq;
94 };
95
96 static struct cpia_camera_ops cpia_pp_ops =
97 {
98         cpia_pp_open,
99         cpia_pp_registerCallback,
100         cpia_pp_transferCmd,
101         cpia_pp_streamStart,
102         cpia_pp_streamStop,
103         cpia_pp_streamRead,
104         cpia_pp_close,
105         1,
106         THIS_MODULE
107 };
108
109 static LIST_HEAD(cam_list);
110 static spinlock_t cam_list_lock_pp;
111
112 /* FIXME */
113 static void cpia_parport_enable_irq( struct parport *port ) {
114         parport_enable_irq(port);
115         mdelay(10);
116         return;
117 }
118
119 static void cpia_parport_disable_irq( struct parport *port ) {
120         parport_disable_irq(port);
121         mdelay(10);
122         return;
123 }
124
125 /* Special CPiA PPC modes: These are invoked by using the 1284 Extensibility
126  * Link Flag during negotiation */
127 #define UPLOAD_FLAG  0x08
128 #define NIBBLE_TRANSFER 0x01
129 #define ECP_TRANSFER 0x03
130
131 #define PARPORT_CHUNK_SIZE      PAGE_SIZE
132
133
134 /****************************************************************************
135  *
136  *  CPiA-specific  low-level parport functions for nibble uploads
137  *
138  ***************************************************************************/
139 /*  CPiA nonstandard "Nibble" mode (no nDataAvail signal after each byte). */
140 /* The standard kernel parport_ieee1284_read_nibble() fails with the CPiA... */
141
142 static size_t cpia_read_nibble (struct parport *port,
143                          void *buffer, size_t len,
144                          int flags)
145 {
146         /* adapted verbatim, with one change, from
147            parport_ieee1284_read_nibble() in drivers/parport/ieee1284-ops.c */
148
149         unsigned char *buf = buffer;
150         int i;
151         unsigned char byte = 0;
152
153         len *= 2; /* in nibbles */
154         for (i=0; i < len; i++) {
155                 unsigned char nibble;
156
157                 /* The CPiA firmware suppresses the use of nDataAvail (nFault LO)
158                  * after every second nibble to signal that more
159                  * data is available.  (the total number of Bytes that
160                  * should be sent is known; if too few are received, an error
161                  * will be recorded after a timeout).
162                  * This is incompatible with parport_ieee1284_read_nibble(),
163                  * which expects to find nFault LO after every second nibble.
164                  */
165
166                 /* Solution: modify cpia_read_nibble to only check for
167                  * nDataAvail before the first nibble is sent.
168                  */
169
170                 /* Does the error line indicate end of data? */
171                 if (((i /*& 1*/) == 0) &&
172                     (parport_read_status(port) & PARPORT_STATUS_ERROR)) {
173                         DBG("%s: No more nibble data (%d bytes)\n",
174                             port->name, i/2);
175                         goto end_of_data;
176                 }
177
178                 /* Event 7: Set nAutoFd low. */
179                 parport_frob_control (port,
180                                       PARPORT_CONTROL_AUTOFD,
181                                       PARPORT_CONTROL_AUTOFD);
182
183                 /* Event 9: nAck goes low. */
184                 port->ieee1284.phase = IEEE1284_PH_REV_DATA;
185                 if (parport_wait_peripheral (port,
186                                              PARPORT_STATUS_ACK, 0)) {
187                         /* Timeout -- no more data? */
188                                  DBG("%s: Nibble timeout at event 9 (%d bytes)\n",
189                                  port->name, i/2);
190                         parport_frob_control (port, PARPORT_CONTROL_AUTOFD, 0);
191                         break;
192                 }
193
194
195                 /* Read a nibble. */
196                 nibble = parport_read_status (port) >> 3;
197                 nibble &= ~8;
198                 if ((nibble & 0x10) == 0)
199                         nibble |= 8;
200                 nibble &= 0xf;
201
202                 /* Event 10: Set nAutoFd high. */
203                 parport_frob_control (port, PARPORT_CONTROL_AUTOFD, 0);
204
205                 /* Event 11: nAck goes high. */
206                 if (parport_wait_peripheral (port,
207                                              PARPORT_STATUS_ACK,
208                                              PARPORT_STATUS_ACK)) {
209                         /* Timeout -- no more data? */
210                         DBG("%s: Nibble timeout at event 11\n",
211                                  port->name);
212                         break;
213                 }
214
215                 if (i & 1) {
216                         /* Second nibble */
217                         byte |= nibble << 4;
218                         *buf++ = byte;
219                 } else
220                         byte = nibble;
221         }
222
223         if (i == len) {
224                 /* Read the last nibble without checking data avail. */
225                 if (parport_read_status (port) & PARPORT_STATUS_ERROR) {
226                 end_of_data:
227                         /* Go to reverse idle phase. */
228                         parport_frob_control (port,
229                                               PARPORT_CONTROL_AUTOFD,
230                                               PARPORT_CONTROL_AUTOFD);
231                         port->physport->ieee1284.phase = IEEE1284_PH_REV_IDLE;
232                 }
233                 else
234                         port->physport->ieee1284.phase = IEEE1284_PH_HBUSY_DAVAIL;
235         }
236
237         return i/2;
238 }
239
240 /* CPiA nonstandard "Nibble Stream" mode (2 nibbles per cycle, instead of 1)
241  * (See CPiA Data sheet p. 31)
242  *
243  * "Nibble Stream" mode used by CPiA for uploads to non-ECP ports is a
244  * nonstandard variant of nibble mode which allows the same (mediocre)
245  * data flow of 8 bits per cycle as software-enabled ECP by TRISTATE-capable
246  * parallel ports, but works also for  non-TRISTATE-capable ports.
247  * (Standard nibble mode only send 4 bits per cycle)
248  *
249  */
250
251 static size_t cpia_read_nibble_stream(struct parport *port,
252                                void *buffer, size_t len,
253                                int flags)
254 {
255         int i;
256         unsigned char *buf = buffer;
257         int endseen = 0;
258
259         for (i=0; i < len; i++) {
260                 unsigned char nibble[2], byte = 0;
261                 int j;
262
263                 /* Image Data is complete when 4 consecutive EOI bytes (0xff) are seen */
264                 if (endseen > 3 )
265                         break;
266
267                 /* Event 7: Set nAutoFd low. */
268                 parport_frob_control (port,
269                                       PARPORT_CONTROL_AUTOFD,
270                                       PARPORT_CONTROL_AUTOFD);
271
272                 /* Event 9: nAck goes low. */
273                 port->ieee1284.phase = IEEE1284_PH_REV_DATA;
274                 if (parport_wait_peripheral (port,
275                                              PARPORT_STATUS_ACK, 0)) {
276                         /* Timeout -- no more data? */
277                                  DBG("%s: Nibble timeout at event 9 (%d bytes)\n",
278                                  port->name, i/2);
279                         parport_frob_control (port, PARPORT_CONTROL_AUTOFD, 0);
280                         break;
281                 }
282
283                 /* Read lower nibble */
284                 nibble[0] = parport_read_status (port) >>3;
285
286                 /* Event 10: Set nAutoFd high. */
287                 parport_frob_control (port, PARPORT_CONTROL_AUTOFD, 0);
288
289                 /* Event 11: nAck goes high. */
290                 if (parport_wait_peripheral (port,
291                                              PARPORT_STATUS_ACK,
292                                              PARPORT_STATUS_ACK)) {
293                         /* Timeout -- no more data? */
294                         DBG("%s: Nibble timeout at event 11\n",
295                                  port->name);
296                         break;
297                 }
298
299                 /* Read upper nibble */
300                 nibble[1] = parport_read_status (port) >>3;
301
302                 /* reassemble the byte */
303                 for (j = 0; j < 2 ; j++ ) {
304                         nibble[j] &= ~8;
305                         if ((nibble[j] & 0x10) == 0)
306                                 nibble[j] |= 8;
307                         nibble[j] &= 0xf;
308                 }
309                 byte = (nibble[0] |(nibble[1] << 4));
310                 *buf++ = byte;
311
312                 if(byte == EOI)
313                   endseen++;
314                 else
315                   endseen = 0;
316         }
317         return i;
318 }
319
320 /****************************************************************************
321  *
322  *  EndTransferMode
323  *
324  ***************************************************************************/
325 static void EndTransferMode(struct pp_cam_entry *cam)
326 {
327         parport_negotiate(cam->port, IEEE1284_MODE_COMPAT);
328 }
329
330 /****************************************************************************
331  *
332  *  ForwardSetup
333  *
334  ***************************************************************************/
335 static int ForwardSetup(struct pp_cam_entry *cam)
336 {
337         int retry;
338
339         /* The CPiA uses ECP protocol for Downloads from the Host to the camera.
340          * This will be software-emulated if ECP hardware is not present
341          */
342
343         /* the usual camera maximum response time is 10ms, but after receiving
344          * some commands, it needs up to 40ms. (Data Sheet p. 32)*/
345
346         for(retry = 0; retry < 4; ++retry) {
347                 if(!parport_negotiate(cam->port, IEEE1284_MODE_ECP)) {
348                         break;
349                 }
350                 mdelay(10);
351         }
352         if(retry == 4) {
353                 DBG("Unable to negotiate IEEE1284 ECP Download mode\n");
354                 return -1;
355         }
356         return 0;
357 }
358 /****************************************************************************
359  *
360  *  ReverseSetup
361  *
362  ***************************************************************************/
363 static int ReverseSetup(struct pp_cam_entry *cam, int extensibility)
364 {
365         int retry;
366         int upload_mode, mode = IEEE1284_MODE_ECP;
367         int transfer_mode = ECP_TRANSFER;
368
369         if (!(cam->port->modes & PARPORT_MODE_ECP) &&
370              !(cam->port->modes & PARPORT_MODE_TRISTATE)) {
371                 mode = IEEE1284_MODE_NIBBLE;
372                 transfer_mode = NIBBLE_TRANSFER;
373         }
374
375         upload_mode = mode;
376         if(extensibility) mode = UPLOAD_FLAG|transfer_mode|IEEE1284_EXT_LINK;
377
378         /* the usual camera maximum response time is 10ms, but after
379          * receiving some commands, it needs up to 40ms. */
380
381         for(retry = 0; retry < 4; ++retry) {
382                 if(!parport_negotiate(cam->port, mode)) {
383                         break;
384                 }
385                 mdelay(10);
386         }
387         if(retry == 4) {
388                 if(extensibility)
389                         DBG("Unable to negotiate upload extensibility mode\n");
390                 else
391                         DBG("Unable to negotiate upload mode\n");
392                 return -1;
393         }
394         if(extensibility) cam->port->ieee1284.mode = upload_mode;
395         return 0;
396 }
397
398 /****************************************************************************
399  *
400  *  WritePacket
401  *
402  ***************************************************************************/
403 static int WritePacket(struct pp_cam_entry *cam, const u8 *packet, size_t size)
404 {
405         int retval=0;
406         int size_written;
407
408         if (packet == NULL) {
409                 return -EINVAL;
410         }
411         if (ForwardSetup(cam)) {
412                 DBG("Write failed in setup\n");
413                 return -EIO;
414         }
415         size_written = parport_write(cam->port, packet, size);
416         if(size_written != size) {
417                 DBG("Write failed, wrote %d/%d\n", size_written, size);
418                 retval = -EIO;
419         }
420         EndTransferMode(cam);
421         return retval;
422 }
423
424 /****************************************************************************
425  *
426  *  ReadPacket
427  *
428  ***************************************************************************/
429 static int ReadPacket(struct pp_cam_entry *cam, u8 *packet, size_t size)
430 {
431         int retval=0;
432
433         if (packet == NULL) {
434                 return -EINVAL;
435         }
436         if (ReverseSetup(cam, 0)) {
437                 return -EIO;
438         }
439
440         /* support for CPiA variant nibble reads */
441         if(cam->port->ieee1284.mode == IEEE1284_MODE_NIBBLE) {
442                 if(cpia_read_nibble(cam->port, packet, size, 0) != size)
443                         retval = -EIO;
444         } else {
445                 if(parport_read(cam->port, packet, size) != size)
446                         retval = -EIO;
447         }
448         EndTransferMode(cam);
449         return retval;
450 }
451
452 /****************************************************************************
453  *
454  *  cpia_pp_streamStart
455  *
456  ***************************************************************************/
457 static int cpia_pp_streamStart(void *privdata)
458 {
459         struct pp_cam_entry *cam = privdata;
460         DBG("\n");
461         cam->streaming=1;
462         cam->image_ready=0;
463         //if (ReverseSetup(cam,1)) return -EIO;
464         if(cam->stream_irq) cpia_parport_enable_irq(cam->port);
465         return 0;
466 }
467
468 /****************************************************************************
469  *
470  *  cpia_pp_streamStop
471  *
472  ***************************************************************************/
473 static int cpia_pp_streamStop(void *privdata)
474 {
475         struct pp_cam_entry *cam = privdata;
476
477         DBG("\n");
478         cam->streaming=0;
479         cpia_parport_disable_irq(cam->port);
480         //EndTransferMode(cam);
481
482         return 0;
483 }
484
485 /****************************************************************************
486  *
487  *  cpia_pp_streamRead
488  *
489  ***************************************************************************/
490 static int cpia_pp_read(struct parport *port, u8 *buffer, int len)
491 {
492         int bytes_read;
493
494         /* support for CPiA variant "nibble stream" reads */
495         if(port->ieee1284.mode == IEEE1284_MODE_NIBBLE)
496                 bytes_read = cpia_read_nibble_stream(port,buffer,len,0);
497         else {
498                 int new_bytes;
499                 for(bytes_read=0; bytes_read<len; bytes_read += new_bytes) {
500                         new_bytes = parport_read(port, buffer+bytes_read,
501                                                  len-bytes_read);
502                         if(new_bytes < 0) break;
503                 }
504         }
505         return bytes_read;
506 }
507
508 static int cpia_pp_streamRead(void *privdata, u8 *buffer, int noblock)
509 {
510         struct pp_cam_entry *cam = privdata;
511         int read_bytes = 0;
512         int i, endseen, block_size, new_bytes;
513
514         if(cam == NULL) {
515                 DBG("Internal driver error: cam is NULL\n");
516                 return -EINVAL;
517         }
518         if(buffer == NULL) {
519                 DBG("Internal driver error: buffer is NULL\n");
520                 return -EINVAL;
521         }
522         //if(cam->streaming) DBG("%d / %d\n", cam->image_ready, noblock);
523         if( cam->stream_irq ) {
524                 DBG("%d\n", cam->image_ready);
525                 cam->image_ready--;
526         }
527         cam->image_complete=0;
528         if (0/*cam->streaming*/) {
529                 if(!cam->image_ready) {
530                         if(noblock) return -EWOULDBLOCK;
531                         interruptible_sleep_on(&cam->wq_stream);
532                         if( signal_pending(current) ) return -EINTR;
533                         DBG("%d\n", cam->image_ready);
534                 }
535         } else {
536                 if (ReverseSetup(cam, 1)) {
537                         DBG("unable to ReverseSetup\n");
538                         return -EIO;
539                 }
540         }
541         endseen = 0;
542         block_size = PARPORT_CHUNK_SIZE;
543         while( !cam->image_complete ) {
544                 cond_resched();
545
546                 new_bytes = cpia_pp_read(cam->port, buffer, block_size );
547                 if( new_bytes <= 0 ) {
548                         break;
549                 }
550                 i=-1;
551                 while(++i<new_bytes && endseen<4) {
552                         if(*buffer==EOI) {
553                                 endseen++;
554                         } else {
555                                 endseen=0;
556                         }
557                         buffer++;
558                 }
559                 read_bytes += i;
560                 if( endseen==4 ) {
561                         cam->image_complete=1;
562                         break;
563                 }
564                 if( CPIA_MAX_IMAGE_SIZE-read_bytes <= PARPORT_CHUNK_SIZE ) {
565                         block_size=CPIA_MAX_IMAGE_SIZE-read_bytes;
566                 }
567         }
568         EndTransferMode(cam);
569         return cam->image_complete ? read_bytes : -EIO;
570 }
571 /****************************************************************************
572  *
573  *  cpia_pp_transferCmd
574  *
575  ***************************************************************************/
576 static int cpia_pp_transferCmd(void *privdata, u8 *command, u8 *data)
577 {
578         int err;
579         int retval=0;
580         int databytes;
581         struct pp_cam_entry *cam = privdata;
582
583         if(cam == NULL) {
584                 DBG("Internal driver error: cam is NULL\n");
585                 return -EINVAL;
586         }
587         if(command == NULL) {
588                 DBG("Internal driver error: command is NULL\n");
589                 return -EINVAL;
590         }
591         databytes = (((int)command[7])<<8) | command[6];
592         if ((err = WritePacket(cam, command, PACKET_LENGTH)) < 0) {
593                 DBG("Error writing command\n");
594                 return err;
595         }
596         if(command[0] == DATA_IN) {
597                 u8 buffer[8];
598                 if(data == NULL) {
599                         DBG("Internal driver error: data is NULL\n");
600                         return -EINVAL;
601                 }
602                 if((err = ReadPacket(cam, buffer, 8)) < 0) {
603                         DBG("Error reading command result\n");
604                        return err;
605                 }
606                 memcpy(data, buffer, databytes);
607         } else if(command[0] == DATA_OUT) {
608                 if(databytes > 0) {
609                         if(data == NULL) {
610                                 DBG("Internal driver error: data is NULL\n");
611                                 retval = -EINVAL;
612                         } else {
613                                 if((err=WritePacket(cam, data, databytes)) < 0){
614                                         DBG("Error writing command data\n");
615                                         return err;
616                                 }
617                         }
618                 }
619         } else {
620                 DBG("Unexpected first byte of command: %x\n", command[0]);
621                 retval = -EINVAL;
622         }
623         return retval;
624 }
625
626 /****************************************************************************
627  *
628  *  cpia_pp_open
629  *
630  ***************************************************************************/
631 static int cpia_pp_open(void *privdata)
632 {
633         struct pp_cam_entry *cam = (struct pp_cam_entry *)privdata;
634
635         if (cam == NULL)
636                 return -EINVAL;
637
638         if(cam->open_count == 0) {
639                 if (parport_claim(cam->pdev)) {
640                         DBG("failed to claim the port\n");
641                         return -EBUSY;
642                 }
643                 parport_negotiate(cam->port, IEEE1284_MODE_COMPAT);
644                 parport_data_forward(cam->port);
645                 parport_write_control(cam->port, PARPORT_CONTROL_SELECT);
646                 udelay(50);
647                 parport_write_control(cam->port,
648                                       PARPORT_CONTROL_SELECT
649                                       | PARPORT_CONTROL_INIT);
650         }
651
652         ++cam->open_count;
653
654         return 0;
655 }
656
657 /****************************************************************************
658  *
659  *  cpia_pp_registerCallback
660  *
661  ***************************************************************************/
662 static int cpia_pp_registerCallback(void *privdata, void (*cb)(void *cbdata), void *cbdata)
663 {
664         struct pp_cam_entry *cam = privdata;
665         int retval = 0;
666
667         if(cam->port->irq != PARPORT_IRQ_NONE) {
668                 INIT_WORK(&cam->cb_task, cb, cbdata);
669         } else {
670                 retval = -1;
671         }
672         return retval;
673 }
674
675 /****************************************************************************
676  *
677  *  cpia_pp_close
678  *
679  ***************************************************************************/
680 static int cpia_pp_close(void *privdata)
681 {
682         struct pp_cam_entry *cam = privdata;
683         if (--cam->open_count == 0) {
684                 parport_release(cam->pdev);
685         }
686         return 0;
687 }
688
689 /****************************************************************************
690  *
691  *  cpia_pp_register
692  *
693  ***************************************************************************/
694 static int cpia_pp_register(struct parport *port)
695 {
696         struct pardevice *pdev = NULL;
697         struct pp_cam_entry *cam;
698         struct cam_data *cpia;
699
700         if (!(port->modes & PARPORT_MODE_PCSPP)) {
701                 LOG("port is not supported by CPiA driver\n");
702                 return -ENXIO;
703         }
704
705         cam = kzalloc(sizeof(struct pp_cam_entry), GFP_KERNEL);
706         if (cam == NULL) {
707                 LOG("failed to allocate camera structure\n");
708                 return -ENOMEM;
709         }
710
711         pdev = parport_register_device(port, "cpia_pp", NULL, NULL,
712                                        NULL, 0, cam);
713
714         if (!pdev) {
715                 LOG("failed to parport_register_device\n");
716                 kfree(cam);
717                 return -ENXIO;
718         }
719
720         cam->pdev = pdev;
721         cam->port = port;
722         init_waitqueue_head(&cam->wq_stream);
723
724         cam->streaming = 0;
725         cam->stream_irq = 0;
726
727         if((cpia = cpia_register_camera(&cpia_pp_ops, cam)) == NULL) {
728                 LOG("failed to cpia_register_camera\n");
729                 parport_unregister_device(pdev);
730                 kfree(cam);
731                 return -ENXIO;
732         }
733         spin_lock( &cam_list_lock_pp );
734         list_add( &cpia->cam_data_list, &cam_list );
735         spin_unlock( &cam_list_lock_pp );
736
737         return 0;
738 }
739
740 static void cpia_pp_detach (struct parport *port)
741 {
742         struct list_head *tmp;
743         struct cam_data *cpia = NULL;
744         struct pp_cam_entry *cam;
745
746         spin_lock( &cam_list_lock_pp );
747         list_for_each (tmp, &cam_list) {
748                 cpia = list_entry(tmp, struct cam_data, cam_data_list);
749                 cam = (struct pp_cam_entry *) cpia->lowlevel_data;
750                 if (cam && cam->port->number == port->number) {
751                         list_del(&cpia->cam_data_list);
752                         break;
753                 }
754                 cpia = NULL;
755         }
756         spin_unlock( &cam_list_lock_pp );
757
758         if (!cpia) {
759                 DBG("cpia_pp_detach failed to find cam_data in cam_list\n");
760                 return;
761         }
762
763         cam = (struct pp_cam_entry *) cpia->lowlevel_data;
764         cpia_unregister_camera(cpia);
765         if(cam->open_count > 0)
766                 cpia_pp_close(cam);
767         parport_unregister_device(cam->pdev);
768         cpia->lowlevel_data = NULL;
769         kfree(cam);
770 }
771
772 static void cpia_pp_attach (struct parport *port)
773 {
774         unsigned int i;
775
776         switch (parport_nr[0])
777         {
778         case PPCPIA_PARPORT_UNSPEC:
779         case PPCPIA_PARPORT_AUTO:
780                 if (port->probe_info[0].class != PARPORT_CLASS_MEDIA ||
781                     port->probe_info[0].cmdset == NULL ||
782                     strncmp(port->probe_info[0].cmdset, "CPIA_1", 6) != 0)
783                         return;
784
785                 cpia_pp_register(port);
786
787                 break;
788
789         default:
790                 for (i = 0; i < PARPORT_MAX; ++i) {
791                         if (port->number == parport_nr[i]) {
792                                 cpia_pp_register(port);
793                                 break;
794                         }
795                 }
796                 break;
797         }
798 }
799
800 static struct parport_driver cpia_pp_driver = {
801         .name = "cpia_pp",
802         .attach = cpia_pp_attach,
803         .detach = cpia_pp_detach,
804 };
805
806 static int cpia_pp_init(void)
807 {
808         printk(KERN_INFO "%s v%d.%d.%d\n",ABOUT,
809                CPIA_PP_MAJ_VER,CPIA_PP_MIN_VER,CPIA_PP_PATCH_VER);
810
811         if(parport_nr[0] == PPCPIA_PARPORT_OFF) {
812                 printk("  disabled\n");
813                 return 0;
814         }
815
816         spin_lock_init( &cam_list_lock_pp );
817
818         if (parport_register_driver (&cpia_pp_driver)) {
819                 LOG ("unable to register with parport\n");
820                 return -EIO;
821         }
822         return 0;
823 }
824
825 #ifdef MODULE
826 int init_module(void)
827 {
828         if (parport[0]) {
829                 /* The user gave some parameters.  Let's see what they were. */
830                 if (!strncmp(parport[0], "auto", 4)) {
831                         parport_nr[0] = PPCPIA_PARPORT_AUTO;
832                 } else {
833                         int n;
834                         for (n = 0; n < PARPORT_MAX && parport[n]; n++) {
835                                 if (!strncmp(parport[n], "none", 4)) {
836                                         parport_nr[n] = PPCPIA_PARPORT_NONE;
837                                 } else {
838                                         char *ep;
839                                         unsigned long r = simple_strtoul(parport[n], &ep, 0);
840                                         if (ep != parport[n]) {
841                                                 parport_nr[n] = r;
842                                         } else {
843                                                 LOG("bad port specifier `%s'\n", parport[n]);
844                                                 return -ENODEV;
845                                         }
846                                 }
847                         }
848                 }
849         }
850         return cpia_pp_init();
851 }
852
853 void cleanup_module(void)
854 {
855         parport_unregister_driver (&cpia_pp_driver);
856         return;
857 }
858
859 #else /* !MODULE */
860
861 static int __init cpia_pp_setup(char *str)
862 {
863         int err;
864
865         if (!strncmp(str, "parport", 7)) {
866                 int n = simple_strtoul(str + 7, NULL, 10);
867                 if (parport_ptr < PARPORT_MAX) {
868                         parport_nr[parport_ptr++] = n;
869                 } else {
870                         LOG("too many ports, %s ignored.\n", str);
871                 }
872         } else if (!strcmp(str, "auto")) {
873                 parport_nr[0] = PPCPIA_PARPORT_AUTO;
874         } else if (!strcmp(str, "none")) {
875                 parport_nr[parport_ptr++] = PPCPIA_PARPORT_NONE;
876         }
877
878         err=cpia_pp_init();
879         if (err)
880                 return err;
881
882         return 1;
883 }
884
885 __setup("cpia_pp=", cpia_pp_setup);
886
887 #endif /* !MODULE */