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