cpia_pp.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  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. DBG("%s: No more nibble data (%d bytes)\n",
  144. port->name, i/2);
  145. goto end_of_data;
  146. }
  147. /* Event 7: Set nAutoFd low. */
  148. parport_frob_control (port,
  149. PARPORT_CONTROL_AUTOFD,
  150. PARPORT_CONTROL_AUTOFD);
  151. /* Event 9: nAck goes low. */
  152. port->ieee1284.phase = IEEE1284_PH_REV_DATA;
  153. if (parport_wait_peripheral (port,
  154. PARPORT_STATUS_ACK, 0)) {
  155. /* Timeout -- no more data? */
  156. DBG("%s: Nibble timeout at event 9 (%d bytes)\n",
  157. port->name, i/2);
  158. parport_frob_control (port, PARPORT_CONTROL_AUTOFD, 0);
  159. break;
  160. }
  161. /* Read a nibble. */
  162. nibble = parport_read_status (port) >> 3;
  163. nibble &= ~8;
  164. if ((nibble & 0x10) == 0)
  165. nibble |= 8;
  166. nibble &= 0xf;
  167. /* Event 10: Set nAutoFd high. */
  168. parport_frob_control (port, PARPORT_CONTROL_AUTOFD, 0);
  169. /* Event 11: nAck goes high. */
  170. if (parport_wait_peripheral (port,
  171. PARPORT_STATUS_ACK,
  172. PARPORT_STATUS_ACK)) {
  173. /* Timeout -- no more data? */
  174. DBG("%s: Nibble timeout at event 11\n",
  175. port->name);
  176. break;
  177. }
  178. if (i & 1) {
  179. /* Second nibble */
  180. byte |= nibble << 4;
  181. *buf++ = byte;
  182. } else
  183. byte = nibble;
  184. }
  185. if (i == len) {
  186. /* Read the last nibble without checking data avail. */
  187. if (parport_read_status (port) & PARPORT_STATUS_ERROR) {
  188. end_of_data:
  189. /* Go to reverse idle phase. */
  190. parport_frob_control (port,
  191. PARPORT_CONTROL_AUTOFD,
  192. PARPORT_CONTROL_AUTOFD);
  193. port->physport->ieee1284.phase = IEEE1284_PH_REV_IDLE;
  194. }
  195. else
  196. port->physport->ieee1284.phase = IEEE1284_PH_HBUSY_DAVAIL;
  197. }
  198. return i/2;
  199. }
  200. /* CPiA nonstandard "Nibble Stream" mode (2 nibbles per cycle, instead of 1)
  201. * (See CPiA Data sheet p. 31)
  202. *
  203. * "Nibble Stream" mode used by CPiA for uploads to non-ECP ports is a
  204. * nonstandard variant of nibble mode which allows the same (mediocre)
  205. * data flow of 8 bits per cycle as software-enabled ECP by TRISTATE-capable
  206. * parallel ports, but works also for non-TRISTATE-capable ports.
  207. * (Standard nibble mode only send 4 bits per cycle)
  208. *
  209. */
  210. static size_t cpia_read_nibble_stream(struct parport *port,
  211. void *buffer, size_t len,
  212. int flags)
  213. {
  214. int i;
  215. unsigned char *buf = buffer;
  216. int endseen = 0;
  217. for (i=0; i < len; i++) {
  218. unsigned char nibble[2], byte = 0;
  219. int j;
  220. /* Image Data is complete when 4 consecutive EOI bytes (0xff) are seen */
  221. if (endseen > 3 )
  222. break;
  223. /* Event 7: Set nAutoFd low. */
  224. parport_frob_control (port,
  225. PARPORT_CONTROL_AUTOFD,
  226. PARPORT_CONTROL_AUTOFD);
  227. /* Event 9: nAck goes low. */
  228. port->ieee1284.phase = IEEE1284_PH_REV_DATA;
  229. if (parport_wait_peripheral (port,
  230. PARPORT_STATUS_ACK, 0)) {
  231. /* Timeout -- no more data? */
  232. DBG("%s: Nibble timeout at event 9 (%d bytes)\n",
  233. port->name, i/2);
  234. parport_frob_control (port, PARPORT_CONTROL_AUTOFD, 0);
  235. break;
  236. }
  237. /* Read lower nibble */
  238. nibble[0] = parport_read_status (port) >>3;
  239. /* Event 10: Set nAutoFd high. */
  240. parport_frob_control (port, PARPORT_CONTROL_AUTOFD, 0);
  241. /* Event 11: nAck goes high. */
  242. if (parport_wait_peripheral (port,
  243. PARPORT_STATUS_ACK,
  244. PARPORT_STATUS_ACK)) {
  245. /* Timeout -- no more data? */
  246. DBG("%s: Nibble timeout at event 11\n",
  247. port->name);
  248. break;
  249. }
  250. /* Read upper nibble */
  251. nibble[1] = parport_read_status (port) >>3;
  252. /* reassemble the byte */
  253. for (j = 0; j < 2 ; j++ ) {
  254. nibble[j] &= ~8;
  255. if ((nibble[j] & 0x10) == 0)
  256. nibble[j] |= 8;
  257. nibble[j] &= 0xf;
  258. }
  259. byte = (nibble[0] |(nibble[1] << 4));
  260. *buf++ = byte;
  261. if(byte == EOI)
  262. endseen++;
  263. else
  264. endseen = 0;
  265. }
  266. return i;
  267. }
  268. /****************************************************************************
  269. *
  270. * EndTransferMode
  271. *
  272. ***************************************************************************/
  273. static void EndTransferMode(struct pp_cam_entry *cam)
  274. {
  275. parport_negotiate(cam->port, IEEE1284_MODE_COMPAT);
  276. }
  277. /****************************************************************************
  278. *
  279. * ForwardSetup
  280. *
  281. ***************************************************************************/
  282. static int ForwardSetup(struct pp_cam_entry *cam)
  283. {
  284. int retry;
  285. /* The CPiA uses ECP protocol for Downloads from the Host to the camera.
  286. * This will be software-emulated if ECP hardware is not present
  287. */
  288. /* the usual camera maximum response time is 10ms, but after receiving
  289. * some commands, it needs up to 40ms. (Data Sheet p. 32)*/
  290. for(retry = 0; retry < 4; ++retry) {
  291. if(!parport_negotiate(cam->port, IEEE1284_MODE_ECP)) {
  292. break;
  293. }
  294. mdelay(10);
  295. }
  296. if(retry == 4) {
  297. DBG("Unable to negotiate IEEE1284 ECP Download mode\n");
  298. return -1;
  299. }
  300. return 0;
  301. }
  302. /****************************************************************************
  303. *
  304. * ReverseSetup
  305. *
  306. ***************************************************************************/
  307. static int ReverseSetup(struct pp_cam_entry *cam, int extensibility)
  308. {
  309. int retry;
  310. int upload_mode, mode = IEEE1284_MODE_ECP;
  311. int transfer_mode = ECP_TRANSFER;
  312. if (!(cam->port->modes & PARPORT_MODE_ECP) &&
  313. !(cam->port->modes & PARPORT_MODE_TRISTATE)) {
  314. mode = IEEE1284_MODE_NIBBLE;
  315. transfer_mode = NIBBLE_TRANSFER;
  316. }
  317. upload_mode = mode;
  318. if(extensibility) mode = UPLOAD_FLAG|transfer_mode|IEEE1284_EXT_LINK;
  319. /* the usual camera maximum response time is 10ms, but after
  320. * receiving some commands, it needs up to 40ms. */
  321. for(retry = 0; retry < 4; ++retry) {
  322. if(!parport_negotiate(cam->port, mode)) {
  323. break;
  324. }
  325. mdelay(10);
  326. }
  327. if(retry == 4) {
  328. if(extensibility)
  329. DBG("Unable to negotiate upload extensibility mode\n");
  330. else
  331. DBG("Unable to negotiate upload mode\n");
  332. return -1;
  333. }
  334. if(extensibility) cam->port->ieee1284.mode = upload_mode;
  335. return 0;
  336. }
  337. /****************************************************************************
  338. *
  339. * WritePacket
  340. *
  341. ***************************************************************************/
  342. static int WritePacket(struct pp_cam_entry *cam, const u8 *packet, size_t size)
  343. {
  344. int retval=0;
  345. int size_written;
  346. if (packet == NULL) {
  347. return -EINVAL;
  348. }
  349. if (ForwardSetup(cam)) {
  350. DBG("Write failed in setup\n");
  351. return -EIO;
  352. }
  353. size_written = parport_write(cam->port, packet, size);
  354. if(size_written != size) {
  355. DBG("Write failed, wrote %d/%d\n", size_written, size);
  356. retval = -EIO;
  357. }
  358. EndTransferMode(cam);
  359. return retval;
  360. }
  361. /****************************************************************************
  362. *
  363. * ReadPacket
  364. *
  365. ***************************************************************************/
  366. static int ReadPacket(struct pp_cam_entry *cam, u8 *packet, size_t size)
  367. {
  368. int retval=0;
  369. if (packet == NULL) {
  370. return -EINVAL;
  371. }
  372. if (ReverseSetup(cam, 0)) {
  373. return -EIO;
  374. }
  375. /* support for CPiA variant nibble reads */
  376. if(cam->port->ieee1284.mode == IEEE1284_MODE_NIBBLE) {
  377. if(cpia_read_nibble(cam->port, packet, size, 0) != size)
  378. retval = -EIO;
  379. } else {
  380. if(parport_read(cam->port, packet, size) != size)
  381. retval = -EIO;
  382. }
  383. EndTransferMode(cam);
  384. return retval;
  385. }
  386. /****************************************************************************
  387. *
  388. * cpia_pp_streamStart
  389. *
  390. ***************************************************************************/
  391. static int cpia_pp_streamStart(void *privdata)
  392. {
  393. struct pp_cam_entry *cam = privdata;
  394. DBG("\n");
  395. cam->streaming=1;
  396. cam->image_ready=0;
  397. //if (ReverseSetup(cam,1)) return -EIO;
  398. if(cam->stream_irq) cpia_parport_enable_irq(cam->port);
  399. return 0;
  400. }
  401. /****************************************************************************
  402. *
  403. * cpia_pp_streamStop
  404. *
  405. ***************************************************************************/
  406. static int cpia_pp_streamStop(void *privdata)
  407. {
  408. struct pp_cam_entry *cam = privdata;
  409. DBG("\n");
  410. cam->streaming=0;
  411. cpia_parport_disable_irq(cam->port);
  412. //EndTransferMode(cam);
  413. return 0;
  414. }
  415. /****************************************************************************
  416. *
  417. * cpia_pp_streamRead
  418. *
  419. ***************************************************************************/
  420. static int cpia_pp_read(struct parport *port, u8 *buffer, int len)
  421. {
  422. int bytes_read;
  423. /* support for CPiA variant "nibble stream" reads */
  424. if(port->ieee1284.mode == IEEE1284_MODE_NIBBLE)
  425. bytes_read = cpia_read_nibble_stream(port,buffer,len,0);
  426. else {
  427. int new_bytes;
  428. for(bytes_read=0; bytes_read<len; bytes_read += new_bytes) {
  429. new_bytes = parport_read(port, buffer+bytes_read,
  430. len-bytes_read);
  431. if(new_bytes < 0) break;
  432. }
  433. }
  434. return bytes_read;
  435. }
  436. static int cpia_pp_streamRead(void *privdata, u8 *buffer, int noblock)
  437. {
  438. struct pp_cam_entry *cam = privdata;
  439. int read_bytes = 0;
  440. int i, endseen, block_size, new_bytes;
  441. if(cam == NULL) {
  442. DBG("Internal driver error: cam is NULL\n");
  443. return -EINVAL;
  444. }
  445. if(buffer == NULL) {
  446. DBG("Internal driver error: buffer is NULL\n");
  447. return -EINVAL;
  448. }
  449. //if(cam->streaming) DBG("%d / %d\n", cam->image_ready, noblock);
  450. if( cam->stream_irq ) {
  451. DBG("%d\n", cam->image_ready);
  452. cam->image_ready--;
  453. }
  454. cam->image_complete=0;
  455. if (0/*cam->streaming*/) {
  456. if(!cam->image_ready) {
  457. if(noblock) return -EWOULDBLOCK;
  458. interruptible_sleep_on(&cam->wq_stream);
  459. if( signal_pending(current) ) return -EINTR;
  460. DBG("%d\n", cam->image_ready);
  461. }
  462. } else {
  463. if (ReverseSetup(cam, 1)) {
  464. DBG("unable to ReverseSetup\n");
  465. return -EIO;
  466. }
  467. }
  468. endseen = 0;
  469. block_size = PARPORT_CHUNK_SIZE;
  470. while( !cam->image_complete ) {
  471. cond_resched();
  472. new_bytes = cpia_pp_read(cam->port, buffer, block_size );
  473. if( new_bytes <= 0 ) {
  474. break;
  475. }
  476. i=-1;
  477. while(++i<new_bytes && endseen<4) {
  478. if(*buffer==EOI) {
  479. endseen++;
  480. } else {
  481. endseen=0;
  482. }
  483. buffer++;
  484. }
  485. read_bytes += i;
  486. if( endseen==4 ) {
  487. cam->image_complete=1;
  488. break;
  489. }
  490. if( CPIA_MAX_IMAGE_SIZE-read_bytes <= PARPORT_CHUNK_SIZE ) {
  491. block_size=CPIA_MAX_IMAGE_SIZE-read_bytes;
  492. }
  493. }
  494. EndTransferMode(cam);
  495. return cam->image_complete ? read_bytes : -EIO;
  496. }
  497. /****************************************************************************
  498. *
  499. * cpia_pp_transferCmd
  500. *
  501. ***************************************************************************/
  502. static int cpia_pp_transferCmd(void *privdata, u8 *command, u8 *data)
  503. {
  504. int err;
  505. int retval=0;
  506. int databytes;
  507. struct pp_cam_entry *cam = privdata;
  508. if(cam == NULL) {
  509. DBG("Internal driver error: cam is NULL\n");
  510. return -EINVAL;
  511. }
  512. if(command == NULL) {
  513. DBG("Internal driver error: command is NULL\n");
  514. return -EINVAL;
  515. }
  516. databytes = (((int)command[7])<<8) | command[6];
  517. if ((err = WritePacket(cam, command, PACKET_LENGTH)) < 0) {
  518. DBG("Error writing command\n");
  519. return err;
  520. }
  521. if(command[0] == DATA_IN) {
  522. u8 buffer[8];
  523. if(data == NULL) {
  524. DBG("Internal driver error: data is NULL\n");
  525. return -EINVAL;
  526. }
  527. if((err = ReadPacket(cam, buffer, 8)) < 0) {
  528. DBG("Error reading command result\n");
  529. return err;
  530. }
  531. memcpy(data, buffer, databytes);
  532. } else if(command[0] == DATA_OUT) {
  533. if(databytes > 0) {
  534. if(data == NULL) {
  535. DBG("Internal driver error: data is NULL\n");
  536. retval = -EINVAL;
  537. } else {
  538. if((err=WritePacket(cam, data, databytes)) < 0){
  539. DBG("Error writing command data\n");
  540. return err;
  541. }
  542. }
  543. }
  544. } else {
  545. DBG("Unexpected first byte of command: %x\n", command[0]);
  546. retval = -EINVAL;
  547. }
  548. return retval;
  549. }
  550. /****************************************************************************
  551. *
  552. * cpia_pp_open
  553. *
  554. ***************************************************************************/
  555. static int cpia_pp_open(void *privdata)
  556. {
  557. struct pp_cam_entry *cam = (struct pp_cam_entry *)privdata;
  558. if (cam == NULL)
  559. return -EINVAL;
  560. if(cam->open_count == 0) {
  561. if (parport_claim(cam->pdev)) {
  562. DBG("failed to claim the port\n");
  563. return -EBUSY;
  564. }
  565. parport_negotiate(cam->port, IEEE1284_MODE_COMPAT);
  566. parport_data_forward(cam->port);
  567. parport_write_control(cam->port, PARPORT_CONTROL_SELECT);
  568. udelay(50);
  569. parport_write_control(cam->port,
  570. PARPORT_CONTROL_SELECT
  571. | PARPORT_CONTROL_INIT);
  572. }
  573. ++cam->open_count;
  574. return 0;
  575. }
  576. /****************************************************************************
  577. *
  578. * cpia_pp_registerCallback
  579. *
  580. ***************************************************************************/
  581. static int cpia_pp_registerCallback(void *privdata, void (*cb)(void *cbdata), void *cbdata)
  582. {
  583. struct pp_cam_entry *cam = privdata;
  584. int retval = 0;
  585. if(cam->port->irq != PARPORT_IRQ_NONE) {
  586. INIT_WORK(&cam->cb_task, cb, cbdata);
  587. } else {
  588. retval = -1;
  589. }
  590. return retval;
  591. }
  592. /****************************************************************************
  593. *
  594. * cpia_pp_close
  595. *
  596. ***************************************************************************/
  597. static int cpia_pp_close(void *privdata)
  598. {
  599. struct pp_cam_entry *cam = privdata;
  600. if (--cam->open_count == 0) {
  601. parport_release(cam->pdev);
  602. }
  603. return 0;
  604. }
  605. /****************************************************************************
  606. *
  607. * cpia_pp_register
  608. *
  609. ***************************************************************************/
  610. static int cpia_pp_register(struct parport *port)
  611. {
  612. struct pardevice *pdev = NULL;
  613. struct pp_cam_entry *cam;
  614. struct cam_data *cpia;
  615. if (!(port->modes & PARPORT_MODE_PCSPP)) {
  616. LOG("port is not supported by CPiA driver\n");
  617. return -ENXIO;
  618. }
  619. cam = kzalloc(sizeof(struct pp_cam_entry), GFP_KERNEL);
  620. if (cam == NULL) {
  621. LOG("failed to allocate camera structure\n");
  622. return -ENOMEM;
  623. }
  624. pdev = parport_register_device(port, "cpia_pp", NULL, NULL,
  625. NULL, 0, cam);
  626. if (!pdev) {
  627. LOG("failed to parport_register_device\n");
  628. kfree(cam);
  629. return -ENXIO;
  630. }
  631. cam->pdev = pdev;
  632. cam->port = port;
  633. init_waitqueue_head(&cam->wq_stream);
  634. cam->streaming = 0;
  635. cam->stream_irq = 0;
  636. if((cpia = cpia_register_camera(&cpia_pp_ops, cam)) == NULL) {
  637. LOG("failed to cpia_register_camera\n");
  638. parport_unregister_device(pdev);
  639. kfree(cam);
  640. return -ENXIO;
  641. }
  642. spin_lock( &cam_list_lock_pp );
  643. list_add( &cpia->cam_data_list, &cam_list );
  644. spin_unlock( &cam_list_lock_pp );
  645. return 0;
  646. }
  647. static void cpia_pp_detach (struct parport *port)
  648. {
  649. struct list_head *tmp;
  650. struct cam_data *cpia = NULL;
  651. struct pp_cam_entry *cam;
  652. spin_lock( &cam_list_lock_pp );
  653. list_for_each (tmp, &cam_list) {
  654. cpia = list_entry(tmp, struct cam_data, cam_data_list);
  655. cam = (struct pp_cam_entry *) cpia->lowlevel_data;
  656. if (cam && cam->port->number == port->number) {
  657. list_del(&cpia->cam_data_list);
  658. break;
  659. }
  660. cpia = NULL;
  661. }
  662. spin_unlock( &cam_list_lock_pp );
  663. if (!cpia) {
  664. DBG("cpia_pp_detach failed to find cam_data in cam_list\n");
  665. return;
  666. }
  667. cam = (struct pp_cam_entry *) cpia->lowlevel_data;
  668. cpia_unregister_camera(cpia);
  669. if(cam->open_count > 0)
  670. cpia_pp_close(cam);
  671. parport_unregister_device(cam->pdev);
  672. cpia->lowlevel_data = NULL;
  673. kfree(cam);
  674. }
  675. static void cpia_pp_attach (struct parport *port)
  676. {
  677. unsigned int i;
  678. switch (parport_nr[0])
  679. {
  680. case PPCPIA_PARPORT_UNSPEC:
  681. case PPCPIA_PARPORT_AUTO:
  682. if (port->probe_info[0].class != PARPORT_CLASS_MEDIA ||
  683. port->probe_info[0].cmdset == NULL ||
  684. strncmp(port->probe_info[0].cmdset, "CPIA_1", 6) != 0)
  685. return;
  686. cpia_pp_register(port);
  687. break;
  688. default:
  689. for (i = 0; i < PARPORT_MAX; ++i) {
  690. if (port->number == parport_nr[i]) {
  691. cpia_pp_register(port);
  692. break;
  693. }
  694. }
  695. break;
  696. }
  697. }
  698. static struct parport_driver cpia_pp_driver = {
  699. .name = "cpia_pp",
  700. .attach = cpia_pp_attach,
  701. .detach = cpia_pp_detach,
  702. };
  703. int cpia_pp_init(void)
  704. {
  705. printk(KERN_INFO "%s v%d.%d.%d\n",ABOUT,
  706. CPIA_PP_MAJ_VER,CPIA_PP_MIN_VER,CPIA_PP_PATCH_VER);
  707. if(parport_nr[0] == PPCPIA_PARPORT_OFF) {
  708. printk(" disabled\n");
  709. return 0;
  710. }
  711. spin_lock_init( &cam_list_lock_pp );
  712. if (parport_register_driver (&cpia_pp_driver)) {
  713. LOG ("unable to register with parport\n");
  714. return -EIO;
  715. }
  716. return 0;
  717. }
  718. #ifdef MODULE
  719. int init_module(void)
  720. {
  721. if (parport[0]) {
  722. /* The user gave some parameters. Let's see what they were. */
  723. if (!strncmp(parport[0], "auto", 4)) {
  724. parport_nr[0] = PPCPIA_PARPORT_AUTO;
  725. } else {
  726. int n;
  727. for (n = 0; n < PARPORT_MAX && parport[n]; n++) {
  728. if (!strncmp(parport[n], "none", 4)) {
  729. parport_nr[n] = PPCPIA_PARPORT_NONE;
  730. } else {
  731. char *ep;
  732. unsigned long r = simple_strtoul(parport[n], &ep, 0);
  733. if (ep != parport[n]) {
  734. parport_nr[n] = r;
  735. } else {
  736. LOG("bad port specifier `%s'\n", parport[n]);
  737. return -ENODEV;
  738. }
  739. }
  740. }
  741. }
  742. }
  743. return cpia_pp_init();
  744. }
  745. void cleanup_module(void)
  746. {
  747. parport_unregister_driver (&cpia_pp_driver);
  748. return;
  749. }
  750. #else /* !MODULE */
  751. static int __init cpia_pp_setup(char *str)
  752. {
  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. return 1;
  766. }
  767. __setup("cpia_pp=", cpia_pp_setup);
  768. #endif /* !MODULE */