cpia_pp.c 22 KB

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