cpia_pp.c 22 KB

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