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