cpia_pp.c 22 KB

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