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#include <linux/config.h> 26#include <linux/version.h> 27 28#include <linux/module.h> 29#include <linux/init.h> 30 31#include <linux/kernel.h> 32#include <linux/parport.h> 33#include <linux/interrupt.h> 34#include <linux/delay.h> 35#include <linux/smp_lock.h> 36 37#include <linux/kmod.h> 38 39/* #define _CPIA_DEBUG_ define for verbose debug output */ 40#include "cpia.h" 41 42static int cpia_pp_open(void *privdata); 43static int cpia_pp_registerCallback(void *privdata, void (*cb) (void *cbdata), 44 void *cbdata); 45static int cpia_pp_transferCmd(void *privdata, u8 *command, u8 *data); 46static int cpia_pp_streamStart(void *privdata); 47static int cpia_pp_streamStop(void *privdata); 48static int cpia_pp_streamRead(void *privdata, u8 *buffer, int noblock); 49static int cpia_pp_close(void *privdata); 50 51#define ABOUT "Parallel port driver for Vision CPiA based cameras" 52 53/* IEEE 1284 Compatiblity Mode signal names */ 54#define nStrobe PARPORT_CONTROL_STROBE /* inverted */ 55#define nAutoFd PARPORT_CONTROL_AUTOFD /* inverted */ 56#define nInit PARPORT_CONTROL_INIT 57#define nSelectIn PARPORT_CONTROL_SELECT 58#define IntrEnable PARPORT_CONTROL_INTEN /* normally zero for no IRQ */ 59#define DirBit PARPORT_CONTROL_DIRECTION /* 0 = Forward, 1 = Reverse */ 60 61#define nFault PARPORT_STATUS_ERROR 62#define Select PARPORT_STATUS_SELECT 63#define PError PARPORT_STATUS_PAPEROUT 64#define nAck PARPORT_STATUS_ACK 65#define Busy PARPORT_STATUS_BUSY /* inverted */ 66 67/* some more */ 68#define HostClk nStrobe 69#define HostAck nAutoFd 70#define nReverseRequest nInit 71#define Active_1284 nSelectIn 72#define nPeriphRequest nFault 73#define XFlag Select 74#define nAckReverse PError 75#define PeriphClk nAck 76#define PeriphAck Busy 77 78/* these can be used to correct for the inversion on some bits */ 79#define STATUS_INVERSION_MASK (Busy) 80#define CONTROL_INVERSION_MASK (nStrobe|nAutoFd|nSelectIn) 81 82#define ECR_empty 0x01 83#define ECR_full 0x02 84#define ECR_serviceIntr 0x04 85#define ECR_dmaEn 0x08 86#define ECR_nErrIntrEn 0x10 87 88#define ECR_mode_mask 0xE0 89#define ECR_SPP_mode 0x00 90#define ECR_PS2_mode 0x20 91#define ECR_FIFO_mode 0x40 92#define ECR_ECP_mode 0x60 93 94#define ECP_FIFO_SIZE 16 95#define DMA_BUFFER_SIZE PAGE_SIZE 96 /* for 16bit DMA make sure DMA_BUFFER_SIZE is 16 bit aligned */ 97#define PARPORT_CHUNK_SIZE PAGE_SIZE/* >=2.3.x */ 98 /* we read this many bytes at once */ 99 100#define GetECRMasked(port,mask) (parport_read_econtrol(port) & (mask)) 101#define GetStatus(port) ((parport_read_status(port)^STATUS_INVERSION_MASK)&(0xf8)) 102#define SetStatus(port,val) parport_write_status(port,(val)^STATUS_INVERSION_MASK) 103#define GetControl(port) ((parport_read_control(port)^CONTROL_INVERSION_MASK)&(0x3f)) 104#define SetControl(port,val) parport_write_control(port,(val)^CONTROL_INVERSION_MASK) 105 106#define GetStatusMasked(port,mask) (GetStatus(port) & (mask)) 107#define GetControlMasked(port,mask) (GetControl(port) & (mask)) 108#define SetControlMasked(port,mask) SetControl(port,GetControl(port) | (mask)); 109#define ClearControlMasked(port,mask) SetControl(port,GetControl(port)&~(mask)); 110#define FrobControlBit(port,mask,value) SetControl(port,(GetControl(port)&~(mask))|((value)&(mask))); 111 112#define PACKET_LENGTH 8 113 114/* Magic numbers for defining port-device mappings */ 115#define PPCPIA_PARPORT_UNSPEC -4 116#define PPCPIA_PARPORT_AUTO -3 117#define PPCPIA_PARPORT_OFF -2 118#define PPCPIA_PARPORT_NONE -1 119 120#ifdef MODULE 121static int parport_nr[PARPORT_MAX] = {[0 ... PARPORT_MAX - 1] = PPCPIA_PARPORT_UNSPEC}; 122static char *parport[PARPORT_MAX] = {NULL,}; 123 124MODULE_AUTHOR("B. Huisman <bhuism@cs.utwente.nl> & Peter Pregler <Peter_Pregler@email.com>"); 125MODULE_DESCRIPTION("Parallel port driver for Vision CPiA based cameras"); 126MODULE_LICENSE("GPL"); 127 128MODULE_PARM(parport, "1-" __MODULE_STRING(PARPORT_MAX) "s"); 129MODULE_PARM_DESC(parport, "'auto' or a list of parallel port numbers. Just like lp."); 130#else 131static int parport_nr[PARPORT_MAX] __initdata = 132 {[0 ... PARPORT_MAX - 1] = PPCPIA_PARPORT_UNSPEC}; 133static int parport_ptr = 0; 134#endif 135 136struct pp_cam_entry { 137 struct pardevice *pdev; 138 struct parport *port; 139 struct tq_struct cb_task; 140 int open_count; 141 wait_queue_head_t wq_stream; 142 /* image state flags */ 143 int image_ready; /* we got an interrupt */ 144 int image_complete; /* we have seen 4 EOI */ 145 146 int streaming; /* we are in streaming mode */ 147 int stream_irq; 148}; 149 150static struct cpia_camera_ops cpia_pp_ops = 151{ 152 cpia_pp_open, 153 cpia_pp_registerCallback, 154 cpia_pp_transferCmd, 155 cpia_pp_streamStart, 156 cpia_pp_streamStop, 157 cpia_pp_streamRead, 158 cpia_pp_close, 159 1 160}; 161 162static struct cam_data *cam_list; 163static spinlock_t cam_list_lock_pp; 164 165#ifdef _CPIA_DEBUG_ 166#define DEB_PORT(port) { \ 167u8 controll = GetControl(port); \ 168u8 statusss = GetStatus(port); \ 169DBG("nsel %c per %c naut %c nstrob %c nak %c busy %c nfaul %c sel %c init %c dir %c\n",\ 170((controll & nSelectIn) ? 'U' : 'D'), \ 171((statusss & PError) ? 'U' : 'D'), \ 172((controll & nAutoFd) ? 'U' : 'D'), \ 173((controll & nStrobe) ? 'U' : 'D'), \ 174((statusss & nAck) ? 'U' : 'D'), \ 175((statusss & Busy) ? 'U' : 'D'), \ 176((statusss & nFault) ? 'U' : 'D'), \ 177((statusss & Select) ? 'U' : 'D'), \ 178((controll & nInit) ? 'U' : 'D'), \ 179((controll & DirBit) ? 'R' : 'F') \ 180); } 181#else 182#define DEB_PORT(port) {} 183#endif 184 185#define WHILE_OUT_TIMEOUT (HZ/10) 186#define DMA_TIMEOUT 10*HZ 187 188static void cpia_parport_enable_irq( struct parport *port ) { 189 parport_enable_irq(port); 190 mdelay(10); 191 return; 192} 193 194static void cpia_parport_disable_irq( struct parport *port ) { 195 parport_disable_irq(port); 196 mdelay(10); 197 return; 198} 199 200/**************************************************************************** 201 * 202 * EndTransferMode 203 * 204 ***************************************************************************/ 205static void EndTransferMode(struct pp_cam_entry *cam) 206{ 207 parport_negotiate(cam->port, IEEE1284_MODE_COMPAT); 208} 209 210/**************************************************************************** 211 * 212 * ForwardSetup 213 * 214 ***************************************************************************/ 215static int ForwardSetup(struct pp_cam_entry *cam) 216{ 217 int retry; 218 219 /* After some commands the camera needs extra time before 220 * it will respond again, so we try up to 3 times */ 221 for(retry=0; retry<3; ++retry) { 222 if(!parport_negotiate(cam->port, IEEE1284_MODE_ECP)) { 223 break; 224 } 225 } 226 if(retry == 3) { 227 DBG("Unable to negotiate ECP mode\n"); 228 return -1; 229 } 230 return 0; 231} 232 233/**************************************************************************** 234 * 235 * ReverseSetup 236 * 237 ***************************************************************************/ 238static int ReverseSetup(struct pp_cam_entry *cam, int extensibility) 239{ 240 int retry; 241 int mode = IEEE1284_MODE_ECP; 242 if(extensibility) mode = 8|3|IEEE1284_EXT_LINK; 243 244 /* After some commands the camera needs extra time before 245 * it will respond again, so we try up to 3 times */ 246 for(retry=0; retry<3; ++retry) { 247 if(!parport_negotiate(cam->port, mode)) { 248 break; 249 } 250 } 251 if(retry == 3) { 252 if(extensibility) 253 DBG("Unable to negotiate extensibility mode\n"); 254 else 255 DBG("Unable to negotiate ECP mode\n"); 256 return -1; 257 } 258 if(extensibility) cam->port->ieee1284.mode = IEEE1284_MODE_ECP; 259 return 0; 260} 261 262/**************************************************************************** 263 * 264 * WritePacket 265 * 266 ***************************************************************************/ 267static int WritePacket(struct pp_cam_entry *cam, const u8 *packet, size_t size) 268{ 269 int retval=0; 270 int size_written; 271 272 if (packet == NULL) { 273 return -EINVAL; 274 } 275 if (ForwardSetup(cam)) { 276 DBG("Write failed in setup\n"); 277 return -EIO; 278 } 279 size_written = parport_write(cam->port, packet, size); 280 if(size_written != size) { 281 DBG("Write failed, wrote %d/%d\n", size_written, size); 282 retval = -EIO; 283 } 284 EndTransferMode(cam); 285 return retval; 286} 287 288/**************************************************************************** 289 * 290 * ReadPacket 291 * 292 ***************************************************************************/ 293static int ReadPacket(struct pp_cam_entry *cam, u8 *packet, size_t size) 294{ 295 int retval=0; 296 if (packet == NULL) { 297 return -EINVAL; 298 } 299 if (ReverseSetup(cam, 0)) { 300 return -EIO; 301 } 302 if(parport_read(cam->port, packet, size) != size) { 303 retval = -EIO; 304 } 305 EndTransferMode(cam); 306 return retval; 307} 308 309/**************************************************************************** 310 * 311 * cpia_pp_streamStart 312 * 313 ***************************************************************************/ 314static int cpia_pp_streamStart(void *privdata) 315{ 316 struct pp_cam_entry *cam = privdata; 317 DBG("\n"); 318 cam->streaming=1; 319 cam->image_ready=0; 320 //if (ReverseSetup(cam,1)) return -EIO; 321 if(cam->stream_irq) cpia_parport_enable_irq(cam->port); 322 return 0; 323} 324 325/**************************************************************************** 326 * 327 * cpia_pp_streamStop 328 * 329 ***************************************************************************/ 330static int cpia_pp_streamStop(void *privdata) 331{ 332 struct pp_cam_entry *cam = privdata; 333 334 DBG("\n"); 335 cam->streaming=0; 336 cpia_parport_disable_irq(cam->port); 337 //EndTransferMode(cam); 338 339 return 0; 340} 341 342/**************************************************************************** 343 * 344 * cpia_pp_streamRead 345 * 346 ***************************************************************************/ 347static int cpia_pp_streamRead(void *privdata, u8 *buffer, int noblock) 348{ 349 struct pp_cam_entry *cam = privdata; 350 int read_bytes = 0; 351 int i, endseen; 352 353 if(cam == NULL) { 354 DBG("Internal driver error: cam is NULL\n"); 355 return -EINVAL; 356 } 357 if(buffer == NULL) { 358 DBG("Internal driver error: buffer is NULL\n"); 359 return -EINVAL; 360 } 361 //if(cam->streaming) DBG("%d / %d\n", cam->image_ready, noblock); 362 if( cam->stream_irq ) { 363 DBG("%d\n", cam->image_ready); 364 cam->image_ready--; 365 } 366 cam->image_complete=0; 367 if (0/*cam->streaming*/) { 368 if(!cam->image_ready) { 369 if(noblock) return -EWOULDBLOCK; 370 interruptible_sleep_on(&cam->wq_stream); 371 if( signal_pending(current) ) return -EINTR; 372 DBG("%d\n", cam->image_ready); 373 } 374 } else { 375 if (ReverseSetup(cam, 1)) { 376 DBG("unable to ReverseSetup\n"); 377 return -EIO; 378 } 379 } 380 read_bytes = parport_read(cam->port, buffer, CPIA_MAX_IMAGE_SIZE ); 381 382 EndTransferMode(cam); 383 DBG("read %d bytes\n", read_bytes); 384 if( read_bytes<0) return -EIO; 385 endseen = 0; 386 endseen = 0; 387 for( i=0; i<read_bytes && endseen<4; i++ ) { 388 if( *buffer==EOI ) { 389 endseen++; 390 } else { 391 endseen=0; 392 } 393 buffer++; 394 } 395 if( endseen>3 ) { 396 cam->image_complete=1; 397 DBG("endseen at %d bytes\n", i); 398 } 399 return cam->image_complete ? read_bytes : -EIO; 400} 401 402/**************************************************************************** 403 * 404 * cpia_pp_transferCmd 405 * 406 ***************************************************************************/ 407static int cpia_pp_transferCmd(void *privdata, u8 *command, u8 *data) 408{ 409 int err; 410 int retval=0; 411 int databytes; 412 struct pp_cam_entry *cam = privdata; 413 414 if(cam == NULL) { 415 DBG("Internal driver error: cam is NULL\n"); 416 return -EINVAL; 417 } 418 if(command == NULL) { 419 DBG("Internal driver error: command is NULL\n"); 420 return -EINVAL; 421 } 422 databytes = (((int)command[7])<<8) | command[6]; 423 if ((err = WritePacket(cam, command, PACKET_LENGTH)) < 0) { 424 DBG("Error writing command\n"); 425 return err; 426 } 427 if(command[0] == DATA_IN) { 428 u8 buffer[8]; 429 if(data == NULL) { 430 DBG("Internal driver error: data is NULL\n"); 431 return -EINVAL; 432 } 433 if((err = ReadPacket(cam, buffer, 8)) < 0) { 434 return err; 435 DBG("Error reading command result\n"); 436 } 437 memcpy(data, buffer, databytes); 438 } else if(command[0] == DATA_OUT) { 439 if(databytes > 0) { 440 if(data == NULL) { 441 DBG("Internal driver error: data is NULL\n"); 442 retval = -EINVAL; 443 } else { 444 if((err=WritePacket(cam, data, databytes)) < 0){ 445 DBG("Error writing command data\n"); 446 return err; 447 } 448 } 449 } 450 } else { 451 DBG("Unexpected first byte of command: %x\n", command[0]); 452 retval = -EINVAL; 453 } 454 return retval; 455} 456 457/**************************************************************************** 458 * 459 * cpia_pp_open 460 * 461 ***************************************************************************/ 462static int cpia_pp_open(void *privdata) 463{ 464 struct pp_cam_entry *cam = (struct pp_cam_entry *)privdata; 465 466 if (cam == NULL) 467 return -EINVAL; 468 469 if(cam->open_count == 0) { 470 if (parport_claim(cam->pdev)) { 471 DBG("failed to claim the port\n"); 472 return -EBUSY; 473 } 474 parport_negotiate(cam->port, IEEE1284_MODE_COMPAT); 475 parport_data_forward(cam->port); 476 parport_write_control(cam->port, PARPORT_CONTROL_SELECT); 477 udelay(50); 478 parport_write_control(cam->port, 479 PARPORT_CONTROL_SELECT 480 | PARPORT_CONTROL_INIT); 481 } 482 483 ++cam->open_count; 484 485 return 0; 486} 487 488/**************************************************************************** 489 * 490 * cpia_pp_registerCallback 491 * 492 ***************************************************************************/ 493static int cpia_pp_registerCallback(void *privdata, void (*cb)(void *cbdata), void *cbdata) 494{ 495 struct pp_cam_entry *cam = privdata; 496 int retval = 0; 497 498 if(cam->port->irq != PARPORT_IRQ_NONE) { 499 cam->cb_task.routine = cb; 500 cam->cb_task.data = cbdata; 501 } else { 502 retval = -1; 503 } 504 return retval; 505} 506 507/**************************************************************************** 508 * 509 * cpia_pp_close 510 * 511 ***************************************************************************/ 512static int cpia_pp_close(void *privdata) 513{ 514 struct pp_cam_entry *cam = privdata; 515 if (--cam->open_count == 0) { 516 parport_release(cam->pdev); 517 } 518 return 0; 519} 520 521/**************************************************************************** 522 * 523 * cpia_pp_register 524 * 525 ***************************************************************************/ 526static int cpia_pp_register(struct parport *port) 527{ 528 struct pardevice *pdev = NULL; 529 struct pp_cam_entry *cam; 530 struct cam_data *cpia; 531 532 if (!(port->modes & PARPORT_MODE_ECP) && 533 !(port->modes & PARPORT_MODE_TRISTATE)) { 534 LOG("port is not ECP capable\n"); 535 return -ENXIO; 536 } 537 538 cam = kmalloc(sizeof(struct pp_cam_entry), GFP_KERNEL); 539 if (cam == NULL) { 540 LOG("failed to allocate camera structure\n"); 541 return -ENOMEM; 542 } 543 memset(cam,0,sizeof(struct pp_cam_entry)); 544 545 pdev = parport_register_device(port, "cpia_pp", NULL, NULL, 546 NULL, 0, cam); 547 548 if (!pdev) { 549 LOG("failed to parport_register_device\n"); 550 kfree(cam); 551 return -ENXIO; 552 } 553 554 cam->pdev = pdev; 555 cam->port = port; 556 init_waitqueue_head(&cam->wq_stream); 557 558 cam->streaming = 0; 559 cam->stream_irq = 0; 560 561 if((cpia = cpia_register_camera(&cpia_pp_ops, cam)) == NULL) { 562 LOG("failed to cpia_register_camera\n"); 563 parport_unregister_device(pdev); 564 kfree(cam); 565 return -ENXIO; 566 } 567 spin_lock( &cam_list_lock_pp ); 568 cpia_add_to_list(cam_list, cpia); 569 spin_unlock( &cam_list_lock_pp ); 570 571 return 0; 572} 573 574static void cpia_pp_detach (struct parport *port) 575{ 576 struct cam_data *cpia; 577 578 spin_lock( &cam_list_lock_pp ); 579 for(cpia = cam_list; cpia != NULL; cpia = cpia->next) { 580 struct pp_cam_entry *cam = cpia->lowlevel_data; 581 if (cam && cam->port->number == port->number) { 582 cpia_remove_from_list(cpia); 583 spin_unlock( &cam_list_lock_pp ); 584 cpia_unregister_camera(cpia); 585 586 if(cam->open_count > 0) { 587 cpia_pp_close(cam); 588 } 589 590 parport_unregister_device(cam->pdev); 591 592 kfree(cam); 593 cpia->lowlevel_data = NULL; 594 break; 595 } 596 } 597} 598 599static void cpia_pp_attach (struct parport *port) 600{ 601 unsigned int i; 602 603 switch (parport_nr[0]) 604 { 605 case PPCPIA_PARPORT_UNSPEC: 606 case PPCPIA_PARPORT_AUTO: 607 if (port->probe_info[0].class != PARPORT_CLASS_MEDIA || 608 port->probe_info[0].cmdset == NULL || 609 strncmp(port->probe_info[0].cmdset, "CPIA_1", 6) != 0) 610 return; 611 612 cpia_pp_register(port); 613 614 break; 615 616 default: 617 for (i = 0; i < PARPORT_MAX; ++i) { 618 if (port->number == parport_nr[i]) { 619 cpia_pp_register(port); 620 break; 621 } 622 } 623 break; 624 } 625} 626 627static struct parport_driver cpia_pp_driver = { 628 "cpia_pp", 629 cpia_pp_attach, 630 cpia_pp_detach, 631 NULL 632}; 633 634int cpia_pp_init(void) 635{ 636 printk(KERN_INFO "%s v%d.%d.%d\n",ABOUT, 637 CPIA_PP_MAJ_VER,CPIA_PP_MIN_VER,CPIA_PP_PATCH_VER); 638 639 if(parport_nr[0] == PPCPIA_PARPORT_OFF) { 640 printk(" disabled\n"); 641 return 0; 642 } 643 644 if (parport_register_driver (&cpia_pp_driver)) { 645 LOG ("unable to register with parport\n"); 646 return -EIO; 647 } 648 649 cam_list = NULL; 650 spin_lock_init( &cam_list_lock_pp ); 651 652 return 0; 653} 654 655#ifdef MODULE 656int init_module(void) 657{ 658 if (parport[0]) { 659 /* The user gave some parameters. Let's see what they were. */ 660 if (!strncmp(parport[0], "auto", 4)) { 661 parport_nr[0] = PPCPIA_PARPORT_AUTO; 662 } else { 663 int n; 664 for (n = 0; n < PARPORT_MAX && parport[n]; n++) { 665 if (!strncmp(parport[n], "none", 4)) { 666 parport_nr[n] = PPCPIA_PARPORT_NONE; 667 } else { 668 char *ep; 669 unsigned long r = simple_strtoul(parport[n], &ep, 0); 670 if (ep != parport[n]) { 671 parport_nr[n] = r; 672 } else { 673 LOG("bad port specifier `%s'\n", parport[n]); 674 return -ENODEV; 675 } 676 } 677 } 678 } 679 } 680#if defined(CONFIG_KMOD) && defined(CONFIG_PNP_PARPORT_MODULE) 681 if(parport_enumerate() && !parport_enumerate()->probe_info.model) { 682 request_module("parport_probe"); 683 } 684#endif 685 return cpia_pp_init(); 686} 687 688void cleanup_module(void) 689{ 690 parport_unregister_driver (&cpia_pp_driver); 691 return; 692} 693 694#else /* !MODULE */ 695 696static int __init cpia_pp_setup(char *str) 697{ 698 if (!strncmp(str, "parport", 7)) { 699 int n = simple_strtoul(str + 7, NULL, 10); 700 if (parport_ptr < PARPORT_MAX) { 701 parport_nr[parport_ptr++] = n; 702 } else { 703 LOG("too many ports, %s ignored.\n", str); 704 } 705 } else if (!strcmp(str, "auto")) { 706 parport_nr[0] = PPCPIA_PARPORT_AUTO; 707 } else if (!strcmp(str, "none")) { 708 parport_nr[parport_ptr++] = PPCPIA_PARPORT_NONE; 709 } 710 711 return 0; 712} 713 714__setup("cpia_pp=", cpia_pp_setup); 715 716#endif /* !MODULE */ 717