cpia_pp.c 22 KB

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