cpia2_v4l.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582
  1. /****************************************************************************
  2. *
  3. * Filename: cpia2_v4l.c
  4. *
  5. * Copyright 2001, STMicrolectronics, Inc.
  6. * Contact: steve.miller@st.com
  7. * Copyright 2001,2005, Scott J. Bertin <scottbertin@yahoo.com>
  8. *
  9. * Description:
  10. * This is a USB driver for CPia2 based video cameras.
  11. * The infrastructure of this driver is based on the cpia usb driver by
  12. * Jochen Scharrlach and Johannes Erdfeldt.
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  27. *
  28. * Stripped of 2.4 stuff ready for main kernel submit by
  29. * Alan Cox <alan@lxorguk.ukuu.org.uk>
  30. ****************************************************************************/
  31. #include <linux/version.h>
  32. #include <linux/module.h>
  33. #include <linux/time.h>
  34. #include <linux/sched.h>
  35. #include <linux/slab.h>
  36. #include <linux/init.h>
  37. #include <linux/videodev2.h>
  38. #include <linux/stringify.h>
  39. #include <media/v4l2-ioctl.h>
  40. #include "cpia2.h"
  41. #include "cpia2dev.h"
  42. static int video_nr = -1;
  43. module_param(video_nr, int, 0);
  44. MODULE_PARM_DESC(video_nr,"video device to register (0=/dev/video0, etc)");
  45. static int buffer_size = 68*1024;
  46. module_param(buffer_size, int, 0);
  47. MODULE_PARM_DESC(buffer_size, "Size for each frame buffer in bytes (default 68k)");
  48. static int num_buffers = 3;
  49. module_param(num_buffers, int, 0);
  50. MODULE_PARM_DESC(num_buffers, "Number of frame buffers (1-"
  51. __stringify(VIDEO_MAX_FRAME) ", default 3)");
  52. static int alternate = DEFAULT_ALT;
  53. module_param(alternate, int, 0);
  54. MODULE_PARM_DESC(alternate, "USB Alternate (" __stringify(USBIF_ISO_1) "-"
  55. __stringify(USBIF_ISO_6) ", default "
  56. __stringify(DEFAULT_ALT) ")");
  57. static int flicker_freq = 60;
  58. module_param(flicker_freq, int, 0);
  59. MODULE_PARM_DESC(flicker_freq, "Flicker frequency (" __stringify(50) "or"
  60. __stringify(60) ", default "
  61. __stringify(60) ")");
  62. static int flicker_mode = NEVER_FLICKER;
  63. module_param(flicker_mode, int, 0);
  64. MODULE_PARM_DESC(flicker_mode,
  65. "Flicker supression (" __stringify(NEVER_FLICKER) "or"
  66. __stringify(ANTI_FLICKER_ON) ", default "
  67. __stringify(NEVER_FLICKER) ")");
  68. MODULE_AUTHOR("Steve Miller (STMicroelectronics) <steve.miller@st.com>");
  69. MODULE_DESCRIPTION("V4L-driver for STMicroelectronics CPiA2 based cameras");
  70. MODULE_SUPPORTED_DEVICE("video");
  71. MODULE_LICENSE("GPL");
  72. #define ABOUT "V4L-Driver for Vision CPiA2 based cameras"
  73. struct control_menu_info {
  74. int value;
  75. char name[32];
  76. };
  77. static struct control_menu_info framerate_controls[] =
  78. {
  79. { CPIA2_VP_FRAMERATE_6_25, "6.25 fps" },
  80. { CPIA2_VP_FRAMERATE_7_5, "7.5 fps" },
  81. { CPIA2_VP_FRAMERATE_12_5, "12.5 fps" },
  82. { CPIA2_VP_FRAMERATE_15, "15 fps" },
  83. { CPIA2_VP_FRAMERATE_25, "25 fps" },
  84. { CPIA2_VP_FRAMERATE_30, "30 fps" },
  85. };
  86. #define NUM_FRAMERATE_CONTROLS (ARRAY_SIZE(framerate_controls))
  87. static struct control_menu_info flicker_controls[] =
  88. {
  89. { NEVER_FLICKER, "Off" },
  90. { FLICKER_50, "50 Hz" },
  91. { FLICKER_60, "60 Hz" },
  92. };
  93. #define NUM_FLICKER_CONTROLS (ARRAY_SIZE(flicker_controls))
  94. static struct control_menu_info lights_controls[] =
  95. {
  96. { 0, "Off" },
  97. { 64, "Top" },
  98. { 128, "Bottom" },
  99. { 192, "Both" },
  100. };
  101. #define NUM_LIGHTS_CONTROLS (ARRAY_SIZE(lights_controls))
  102. #define GPIO_LIGHTS_MASK 192
  103. static struct v4l2_queryctrl controls[] = {
  104. {
  105. .id = V4L2_CID_BRIGHTNESS,
  106. .type = V4L2_CTRL_TYPE_INTEGER,
  107. .name = "Brightness",
  108. .minimum = 0,
  109. .maximum = 255,
  110. .step = 1,
  111. .default_value = DEFAULT_BRIGHTNESS,
  112. },
  113. {
  114. .id = V4L2_CID_CONTRAST,
  115. .type = V4L2_CTRL_TYPE_INTEGER,
  116. .name = "Contrast",
  117. .minimum = 0,
  118. .maximum = 255,
  119. .step = 1,
  120. .default_value = DEFAULT_CONTRAST,
  121. },
  122. {
  123. .id = V4L2_CID_SATURATION,
  124. .type = V4L2_CTRL_TYPE_INTEGER,
  125. .name = "Saturation",
  126. .minimum = 0,
  127. .maximum = 255,
  128. .step = 1,
  129. .default_value = DEFAULT_SATURATION,
  130. },
  131. {
  132. .id = V4L2_CID_HFLIP,
  133. .type = V4L2_CTRL_TYPE_BOOLEAN,
  134. .name = "Mirror Horizontally",
  135. .minimum = 0,
  136. .maximum = 1,
  137. .step = 1,
  138. .default_value = 0,
  139. },
  140. {
  141. .id = V4L2_CID_VFLIP,
  142. .type = V4L2_CTRL_TYPE_BOOLEAN,
  143. .name = "Flip Vertically",
  144. .minimum = 0,
  145. .maximum = 1,
  146. .step = 1,
  147. .default_value = 0,
  148. },
  149. {
  150. .id = CPIA2_CID_TARGET_KB,
  151. .type = V4L2_CTRL_TYPE_INTEGER,
  152. .name = "Target KB",
  153. .minimum = 0,
  154. .maximum = 255,
  155. .step = 1,
  156. .default_value = DEFAULT_TARGET_KB,
  157. },
  158. {
  159. .id = CPIA2_CID_GPIO,
  160. .type = V4L2_CTRL_TYPE_INTEGER,
  161. .name = "GPIO",
  162. .minimum = 0,
  163. .maximum = 255,
  164. .step = 1,
  165. .default_value = 0,
  166. },
  167. {
  168. .id = CPIA2_CID_FLICKER_MODE,
  169. .type = V4L2_CTRL_TYPE_MENU,
  170. .name = "Flicker Reduction",
  171. .minimum = 0,
  172. .maximum = NUM_FLICKER_CONTROLS-1,
  173. .step = 1,
  174. .default_value = 0,
  175. },
  176. {
  177. .id = CPIA2_CID_FRAMERATE,
  178. .type = V4L2_CTRL_TYPE_MENU,
  179. .name = "Framerate",
  180. .minimum = 0,
  181. .maximum = NUM_FRAMERATE_CONTROLS-1,
  182. .step = 1,
  183. .default_value = NUM_FRAMERATE_CONTROLS-1,
  184. },
  185. {
  186. .id = CPIA2_CID_USB_ALT,
  187. .type = V4L2_CTRL_TYPE_INTEGER,
  188. .name = "USB Alternate",
  189. .minimum = USBIF_ISO_1,
  190. .maximum = USBIF_ISO_6,
  191. .step = 1,
  192. .default_value = DEFAULT_ALT,
  193. },
  194. {
  195. .id = CPIA2_CID_LIGHTS,
  196. .type = V4L2_CTRL_TYPE_MENU,
  197. .name = "Lights",
  198. .minimum = 0,
  199. .maximum = NUM_LIGHTS_CONTROLS-1,
  200. .step = 1,
  201. .default_value = 0,
  202. },
  203. {
  204. .id = CPIA2_CID_RESET_CAMERA,
  205. .type = V4L2_CTRL_TYPE_BUTTON,
  206. .name = "Reset Camera",
  207. .minimum = 0,
  208. .maximum = 0,
  209. .step = 0,
  210. .default_value = 0,
  211. },
  212. };
  213. #define NUM_CONTROLS (ARRAY_SIZE(controls))
  214. /******************************************************************************
  215. *
  216. * cpia2_open
  217. *
  218. *****************************************************************************/
  219. static int cpia2_open(struct file *file)
  220. {
  221. struct camera_data *cam = video_drvdata(file);
  222. struct cpia2_fh *fh;
  223. if (!cam) {
  224. ERR("Internal error, camera_data not found!\n");
  225. return -ENODEV;
  226. }
  227. if (!cam->present)
  228. return -ENODEV;
  229. if (cam->open_count == 0) {
  230. if (cpia2_allocate_buffers(cam))
  231. return -ENOMEM;
  232. /* reset the camera */
  233. if (cpia2_reset_camera(cam) < 0)
  234. return -EIO;
  235. cam->APP_len = 0;
  236. cam->COM_len = 0;
  237. }
  238. fh = kmalloc(sizeof(*fh), GFP_KERNEL);
  239. if (!fh)
  240. return -ENOMEM;
  241. file->private_data = fh;
  242. fh->prio = V4L2_PRIORITY_UNSET;
  243. v4l2_prio_open(&cam->prio, &fh->prio);
  244. fh->mmapped = 0;
  245. ++cam->open_count;
  246. cpia2_dbg_dump_registers(cam);
  247. return 0;
  248. }
  249. /******************************************************************************
  250. *
  251. * cpia2_close
  252. *
  253. *****************************************************************************/
  254. static int cpia2_close(struct file *file)
  255. {
  256. struct video_device *dev = video_devdata(file);
  257. struct camera_data *cam = video_get_drvdata(dev);
  258. struct cpia2_fh *fh = file->private_data;
  259. if (cam->present &&
  260. (cam->open_count == 1 || fh->prio == V4L2_PRIORITY_RECORD)) {
  261. cpia2_usb_stream_stop(cam);
  262. if (cam->open_count == 1) {
  263. /* save camera state for later open */
  264. cpia2_save_camera_state(cam);
  265. cpia2_set_low_power(cam);
  266. cpia2_free_buffers(cam);
  267. }
  268. }
  269. if (fh->mmapped)
  270. cam->mmapped = 0;
  271. v4l2_prio_close(&cam->prio, fh->prio);
  272. file->private_data = NULL;
  273. kfree(fh);
  274. if (--cam->open_count == 0) {
  275. cpia2_free_buffers(cam);
  276. if (!cam->present) {
  277. video_unregister_device(dev);
  278. kfree(cam);
  279. return 0;
  280. }
  281. }
  282. return 0;
  283. }
  284. /******************************************************************************
  285. *
  286. * cpia2_v4l_read
  287. *
  288. *****************************************************************************/
  289. static ssize_t cpia2_v4l_read(struct file *file, char __user *buf, size_t count,
  290. loff_t *off)
  291. {
  292. struct camera_data *cam = video_drvdata(file);
  293. int noblock = file->f_flags&O_NONBLOCK;
  294. struct cpia2_fh *fh = file->private_data;
  295. if(!cam)
  296. return -EINVAL;
  297. /* Priority check */
  298. if(fh->prio != V4L2_PRIORITY_RECORD) {
  299. return -EBUSY;
  300. }
  301. return cpia2_read(cam, buf, count, noblock);
  302. }
  303. /******************************************************************************
  304. *
  305. * cpia2_v4l_poll
  306. *
  307. *****************************************************************************/
  308. static unsigned int cpia2_v4l_poll(struct file *filp, struct poll_table_struct *wait)
  309. {
  310. struct camera_data *cam = video_drvdata(filp);
  311. struct cpia2_fh *fh = filp->private_data;
  312. if(!cam)
  313. return POLLERR;
  314. /* Priority check */
  315. if(fh->prio != V4L2_PRIORITY_RECORD) {
  316. return POLLERR;
  317. }
  318. return cpia2_poll(cam, filp, wait);
  319. }
  320. static int sync(struct camera_data *cam, int frame_nr)
  321. {
  322. struct framebuf *frame = &cam->buffers[frame_nr];
  323. while (1) {
  324. if (frame->status == FRAME_READY)
  325. return 0;
  326. if (!cam->streaming) {
  327. frame->status = FRAME_READY;
  328. frame->length = 0;
  329. return 0;
  330. }
  331. mutex_unlock(&cam->v4l2_lock);
  332. wait_event_interruptible(cam->wq_stream,
  333. !cam->streaming ||
  334. frame->status == FRAME_READY);
  335. mutex_lock(&cam->v4l2_lock);
  336. if (signal_pending(current))
  337. return -ERESTARTSYS;
  338. if(!cam->present)
  339. return -ENOTTY;
  340. }
  341. }
  342. /******************************************************************************
  343. *
  344. * ioctl_set_gpio
  345. *
  346. *****************************************************************************/
  347. static long cpia2_default(struct file *file, void *fh, bool valid_prio,
  348. int cmd, void *arg)
  349. {
  350. struct camera_data *cam = video_drvdata(file);
  351. __u32 gpio_val;
  352. if (cmd != CPIA2_CID_GPIO)
  353. return -EINVAL;
  354. gpio_val = *(__u32*) arg;
  355. if (gpio_val &~ 0xFFU)
  356. return -EINVAL;
  357. return cpia2_set_gpio(cam, (unsigned char)gpio_val);
  358. }
  359. /******************************************************************************
  360. *
  361. * ioctl_querycap
  362. *
  363. * V4L2 device capabilities
  364. *
  365. *****************************************************************************/
  366. static int cpia2_querycap(struct file *file, void *fh, struct v4l2_capability *vc)
  367. {
  368. struct camera_data *cam = video_drvdata(file);
  369. strcpy(vc->driver, "cpia2");
  370. if (cam->params.pnp_id.product == 0x151)
  371. strcpy(vc->card, "QX5 Microscope");
  372. else
  373. strcpy(vc->card, "CPiA2 Camera");
  374. switch (cam->params.pnp_id.device_type) {
  375. case DEVICE_STV_672:
  376. strcat(vc->card, " (672/");
  377. break;
  378. case DEVICE_STV_676:
  379. strcat(vc->card, " (676/");
  380. break;
  381. default:
  382. strcat(vc->card, " (???/");
  383. break;
  384. }
  385. switch (cam->params.version.sensor_flags) {
  386. case CPIA2_VP_SENSOR_FLAGS_404:
  387. strcat(vc->card, "404)");
  388. break;
  389. case CPIA2_VP_SENSOR_FLAGS_407:
  390. strcat(vc->card, "407)");
  391. break;
  392. case CPIA2_VP_SENSOR_FLAGS_409:
  393. strcat(vc->card, "409)");
  394. break;
  395. case CPIA2_VP_SENSOR_FLAGS_410:
  396. strcat(vc->card, "410)");
  397. break;
  398. case CPIA2_VP_SENSOR_FLAGS_500:
  399. strcat(vc->card, "500)");
  400. break;
  401. default:
  402. strcat(vc->card, "???)");
  403. break;
  404. }
  405. if (usb_make_path(cam->dev, vc->bus_info, sizeof(vc->bus_info)) <0)
  406. memset(vc->bus_info,0, sizeof(vc->bus_info));
  407. vc->version = KERNEL_VERSION(CPIA2_MAJ_VER, CPIA2_MIN_VER,
  408. CPIA2_PATCH_VER);
  409. vc->capabilities = V4L2_CAP_VIDEO_CAPTURE |
  410. V4L2_CAP_READWRITE |
  411. V4L2_CAP_STREAMING;
  412. return 0;
  413. }
  414. /******************************************************************************
  415. *
  416. * ioctl_input
  417. *
  418. * V4L2 input get/set/enumerate
  419. *
  420. *****************************************************************************/
  421. static int cpia2_enum_input(struct file *file, void *fh, struct v4l2_input *i)
  422. {
  423. if (i->index)
  424. return -EINVAL;
  425. strcpy(i->name, "Camera");
  426. i->type = V4L2_INPUT_TYPE_CAMERA;
  427. return 0;
  428. }
  429. static int cpia2_g_input(struct file *file, void *fh, unsigned int *i)
  430. {
  431. *i = 0;
  432. return 0;
  433. }
  434. static int cpia2_s_input(struct file *file, void *fh, unsigned int i)
  435. {
  436. return i ? -EINVAL : 0;
  437. }
  438. /******************************************************************************
  439. *
  440. * ioctl_enum_fmt
  441. *
  442. * V4L2 format enumerate
  443. *
  444. *****************************************************************************/
  445. static int cpia2_enum_fmt_vid_cap(struct file *file, void *fh,
  446. struct v4l2_fmtdesc *f)
  447. {
  448. int index = f->index;
  449. if (index < 0 || index > 1)
  450. return -EINVAL;
  451. memset(f, 0, sizeof(*f));
  452. f->index = index;
  453. f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  454. f->flags = V4L2_FMT_FLAG_COMPRESSED;
  455. switch(index) {
  456. case 0:
  457. strcpy(f->description, "MJPEG");
  458. f->pixelformat = V4L2_PIX_FMT_MJPEG;
  459. break;
  460. case 1:
  461. strcpy(f->description, "JPEG");
  462. f->pixelformat = V4L2_PIX_FMT_JPEG;
  463. break;
  464. default:
  465. return -EINVAL;
  466. }
  467. return 0;
  468. }
  469. /******************************************************************************
  470. *
  471. * ioctl_try_fmt
  472. *
  473. * V4L2 format try
  474. *
  475. *****************************************************************************/
  476. static int cpia2_try_fmt_vid_cap(struct file *file, void *fh,
  477. struct v4l2_format *f)
  478. {
  479. struct camera_data *cam = video_drvdata(file);
  480. if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG &&
  481. f->fmt.pix.pixelformat != V4L2_PIX_FMT_JPEG)
  482. return -EINVAL;
  483. f->fmt.pix.field = V4L2_FIELD_NONE;
  484. f->fmt.pix.bytesperline = 0;
  485. f->fmt.pix.sizeimage = cam->frame_size;
  486. f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
  487. f->fmt.pix.priv = 0;
  488. switch (cpia2_match_video_size(f->fmt.pix.width, f->fmt.pix.height)) {
  489. case VIDEOSIZE_VGA:
  490. f->fmt.pix.width = 640;
  491. f->fmt.pix.height = 480;
  492. break;
  493. case VIDEOSIZE_CIF:
  494. f->fmt.pix.width = 352;
  495. f->fmt.pix.height = 288;
  496. break;
  497. case VIDEOSIZE_QVGA:
  498. f->fmt.pix.width = 320;
  499. f->fmt.pix.height = 240;
  500. break;
  501. case VIDEOSIZE_288_216:
  502. f->fmt.pix.width = 288;
  503. f->fmt.pix.height = 216;
  504. break;
  505. case VIDEOSIZE_256_192:
  506. f->fmt.pix.width = 256;
  507. f->fmt.pix.height = 192;
  508. break;
  509. case VIDEOSIZE_224_168:
  510. f->fmt.pix.width = 224;
  511. f->fmt.pix.height = 168;
  512. break;
  513. case VIDEOSIZE_192_144:
  514. f->fmt.pix.width = 192;
  515. f->fmt.pix.height = 144;
  516. break;
  517. case VIDEOSIZE_QCIF:
  518. default:
  519. f->fmt.pix.width = 176;
  520. f->fmt.pix.height = 144;
  521. break;
  522. }
  523. return 0;
  524. }
  525. /******************************************************************************
  526. *
  527. * ioctl_set_fmt
  528. *
  529. * V4L2 format set
  530. *
  531. *****************************************************************************/
  532. static int cpia2_s_fmt_vid_cap(struct file *file, void *_fh,
  533. struct v4l2_format *f)
  534. {
  535. struct camera_data *cam = video_drvdata(file);
  536. struct cpia2_fh *fh = _fh;
  537. int err, frame;
  538. err = v4l2_prio_check(&cam->prio, fh->prio);
  539. if (err)
  540. return err;
  541. err = cpia2_try_fmt_vid_cap(file, _fh, f);
  542. if(err != 0)
  543. return err;
  544. /* Ensure that only this process can change the format. */
  545. err = v4l2_prio_change(&cam->prio, &fh->prio, V4L2_PRIORITY_RECORD);
  546. if(err != 0) {
  547. return err;
  548. }
  549. cam->pixelformat = f->fmt.pix.pixelformat;
  550. /* NOTE: This should be set to 1 for MJPEG, but some apps don't handle
  551. * the missing Huffman table properly. */
  552. cam->params.compression.inhibit_htables = 0;
  553. /*f->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG;*/
  554. /* we set the video window to something smaller or equal to what
  555. * is requested by the user???
  556. */
  557. DBG("Requested width = %d, height = %d\n",
  558. f->fmt.pix.width, f->fmt.pix.height);
  559. if (f->fmt.pix.width != cam->width ||
  560. f->fmt.pix.height != cam->height) {
  561. cam->width = f->fmt.pix.width;
  562. cam->height = f->fmt.pix.height;
  563. cam->params.roi.width = f->fmt.pix.width;
  564. cam->params.roi.height = f->fmt.pix.height;
  565. cpia2_set_format(cam);
  566. }
  567. for (frame = 0; frame < cam->num_frames; ++frame) {
  568. if (cam->buffers[frame].status == FRAME_READING)
  569. if ((err = sync(cam, frame)) < 0)
  570. return err;
  571. cam->buffers[frame].status = FRAME_EMPTY;
  572. }
  573. return 0;
  574. }
  575. /******************************************************************************
  576. *
  577. * ioctl_get_fmt
  578. *
  579. * V4L2 format get
  580. *
  581. *****************************************************************************/
  582. static int cpia2_g_fmt_vid_cap(struct file *file, void *fh,
  583. struct v4l2_format *f)
  584. {
  585. struct camera_data *cam = video_drvdata(file);
  586. f->fmt.pix.width = cam->width;
  587. f->fmt.pix.height = cam->height;
  588. f->fmt.pix.pixelformat = cam->pixelformat;
  589. f->fmt.pix.field = V4L2_FIELD_NONE;
  590. f->fmt.pix.bytesperline = 0;
  591. f->fmt.pix.sizeimage = cam->frame_size;
  592. f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
  593. f->fmt.pix.priv = 0;
  594. return 0;
  595. }
  596. /******************************************************************************
  597. *
  598. * ioctl_cropcap
  599. *
  600. * V4L2 query cropping capabilities
  601. * NOTE: cropping is currently disabled
  602. *
  603. *****************************************************************************/
  604. static int cpia2_cropcap(struct file *file, void *fh, struct v4l2_cropcap *c)
  605. {
  606. struct camera_data *cam = video_drvdata(file);
  607. if (c->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  608. return -EINVAL;
  609. c->bounds.left = 0;
  610. c->bounds.top = 0;
  611. c->bounds.width = cam->width;
  612. c->bounds.height = cam->height;
  613. c->defrect.left = 0;
  614. c->defrect.top = 0;
  615. c->defrect.width = cam->width;
  616. c->defrect.height = cam->height;
  617. c->pixelaspect.numerator = 1;
  618. c->pixelaspect.denominator = 1;
  619. return 0;
  620. }
  621. /******************************************************************************
  622. *
  623. * ioctl_queryctrl
  624. *
  625. * V4L2 query possible control variables
  626. *
  627. *****************************************************************************/
  628. static int cpia2_queryctrl(struct file *file, void *fh, struct v4l2_queryctrl *c)
  629. {
  630. struct camera_data *cam = video_drvdata(file);
  631. int i;
  632. for(i=0; i<NUM_CONTROLS; ++i) {
  633. if(c->id == controls[i].id) {
  634. memcpy(c, controls+i, sizeof(*c));
  635. break;
  636. }
  637. }
  638. if(i == NUM_CONTROLS)
  639. return -EINVAL;
  640. /* Some devices have additional limitations */
  641. switch(c->id) {
  642. case V4L2_CID_BRIGHTNESS:
  643. /***
  644. * Don't let the register be set to zero - bug in VP4
  645. * flash of full brightness
  646. ***/
  647. if (cam->params.pnp_id.device_type == DEVICE_STV_672)
  648. c->minimum = 1;
  649. break;
  650. case V4L2_CID_VFLIP:
  651. // VP5 Only
  652. if(cam->params.pnp_id.device_type == DEVICE_STV_672)
  653. c->flags |= V4L2_CTRL_FLAG_DISABLED;
  654. break;
  655. case CPIA2_CID_FRAMERATE:
  656. if(cam->params.pnp_id.device_type == DEVICE_STV_672 &&
  657. cam->params.version.sensor_flags==CPIA2_VP_SENSOR_FLAGS_500){
  658. // Maximum 15fps
  659. for(i=0; i<c->maximum; ++i) {
  660. if(framerate_controls[i].value ==
  661. CPIA2_VP_FRAMERATE_15) {
  662. c->maximum = i;
  663. c->default_value = i;
  664. }
  665. }
  666. }
  667. break;
  668. case CPIA2_CID_FLICKER_MODE:
  669. // Flicker control only valid for 672.
  670. if(cam->params.pnp_id.device_type != DEVICE_STV_672)
  671. c->flags |= V4L2_CTRL_FLAG_DISABLED;
  672. break;
  673. case CPIA2_CID_LIGHTS:
  674. // Light control only valid for the QX5 Microscope.
  675. if(cam->params.pnp_id.product != 0x151)
  676. c->flags |= V4L2_CTRL_FLAG_DISABLED;
  677. break;
  678. default:
  679. break;
  680. }
  681. return 0;
  682. }
  683. /******************************************************************************
  684. *
  685. * ioctl_querymenu
  686. *
  687. * V4L2 query possible control variables
  688. *
  689. *****************************************************************************/
  690. static int cpia2_querymenu(struct file *file, void *fh, struct v4l2_querymenu *m)
  691. {
  692. struct camera_data *cam = video_drvdata(file);
  693. switch(m->id) {
  694. case CPIA2_CID_FLICKER_MODE:
  695. if (m->index >= NUM_FLICKER_CONTROLS)
  696. return -EINVAL;
  697. strcpy(m->name, flicker_controls[m->index].name);
  698. break;
  699. case CPIA2_CID_FRAMERATE:
  700. {
  701. int maximum = NUM_FRAMERATE_CONTROLS - 1;
  702. if(cam->params.pnp_id.device_type == DEVICE_STV_672 &&
  703. cam->params.version.sensor_flags==CPIA2_VP_SENSOR_FLAGS_500){
  704. // Maximum 15fps
  705. int i;
  706. for(i=0; i<maximum; ++i) {
  707. if(framerate_controls[i].value ==
  708. CPIA2_VP_FRAMERATE_15)
  709. maximum = i;
  710. }
  711. }
  712. if (m->index > maximum)
  713. return -EINVAL;
  714. strcpy(m->name, framerate_controls[m->index].name);
  715. break;
  716. }
  717. case CPIA2_CID_LIGHTS:
  718. if (m->index >= NUM_LIGHTS_CONTROLS)
  719. return -EINVAL;
  720. strcpy(m->name, lights_controls[m->index].name);
  721. break;
  722. default:
  723. return -EINVAL;
  724. }
  725. return 0;
  726. }
  727. /******************************************************************************
  728. *
  729. * ioctl_g_ctrl
  730. *
  731. * V4L2 get the value of a control variable
  732. *
  733. *****************************************************************************/
  734. static int cpia2_g_ctrl(struct file *file, void *fh, struct v4l2_control *c)
  735. {
  736. struct camera_data *cam = video_drvdata(file);
  737. switch(c->id) {
  738. case V4L2_CID_BRIGHTNESS:
  739. cpia2_do_command(cam, CPIA2_CMD_GET_VP_BRIGHTNESS,
  740. TRANSFER_READ, 0);
  741. c->value = cam->params.color_params.brightness;
  742. break;
  743. case V4L2_CID_CONTRAST:
  744. cpia2_do_command(cam, CPIA2_CMD_GET_CONTRAST,
  745. TRANSFER_READ, 0);
  746. c->value = cam->params.color_params.contrast;
  747. break;
  748. case V4L2_CID_SATURATION:
  749. cpia2_do_command(cam, CPIA2_CMD_GET_VP_SATURATION,
  750. TRANSFER_READ, 0);
  751. c->value = cam->params.color_params.saturation;
  752. break;
  753. case V4L2_CID_HFLIP:
  754. cpia2_do_command(cam, CPIA2_CMD_GET_USER_EFFECTS,
  755. TRANSFER_READ, 0);
  756. c->value = (cam->params.vp_params.user_effects &
  757. CPIA2_VP_USER_EFFECTS_MIRROR) != 0;
  758. break;
  759. case V4L2_CID_VFLIP:
  760. cpia2_do_command(cam, CPIA2_CMD_GET_USER_EFFECTS,
  761. TRANSFER_READ, 0);
  762. c->value = (cam->params.vp_params.user_effects &
  763. CPIA2_VP_USER_EFFECTS_FLIP) != 0;
  764. break;
  765. case CPIA2_CID_TARGET_KB:
  766. c->value = cam->params.vc_params.target_kb;
  767. break;
  768. case CPIA2_CID_GPIO:
  769. cpia2_do_command(cam, CPIA2_CMD_GET_VP_GPIO_DATA,
  770. TRANSFER_READ, 0);
  771. c->value = cam->params.vp_params.gpio_data;
  772. break;
  773. case CPIA2_CID_FLICKER_MODE:
  774. {
  775. int i, mode;
  776. cpia2_do_command(cam, CPIA2_CMD_GET_FLICKER_MODES,
  777. TRANSFER_READ, 0);
  778. if(cam->params.flicker_control.cam_register &
  779. CPIA2_VP_FLICKER_MODES_NEVER_FLICKER) {
  780. mode = NEVER_FLICKER;
  781. } else {
  782. if(cam->params.flicker_control.cam_register &
  783. CPIA2_VP_FLICKER_MODES_50HZ) {
  784. mode = FLICKER_50;
  785. } else {
  786. mode = FLICKER_60;
  787. }
  788. }
  789. for(i=0; i<NUM_FLICKER_CONTROLS; i++) {
  790. if(flicker_controls[i].value == mode) {
  791. c->value = i;
  792. break;
  793. }
  794. }
  795. if(i == NUM_FLICKER_CONTROLS)
  796. return -EINVAL;
  797. break;
  798. }
  799. case CPIA2_CID_FRAMERATE:
  800. {
  801. int maximum = NUM_FRAMERATE_CONTROLS - 1;
  802. int i;
  803. for(i=0; i<= maximum; i++) {
  804. if(cam->params.vp_params.frame_rate ==
  805. framerate_controls[i].value)
  806. break;
  807. }
  808. if(i > maximum)
  809. return -EINVAL;
  810. c->value = i;
  811. break;
  812. }
  813. case CPIA2_CID_USB_ALT:
  814. c->value = cam->params.camera_state.stream_mode;
  815. break;
  816. case CPIA2_CID_LIGHTS:
  817. {
  818. int i;
  819. cpia2_do_command(cam, CPIA2_CMD_GET_VP_GPIO_DATA,
  820. TRANSFER_READ, 0);
  821. for(i=0; i<NUM_LIGHTS_CONTROLS; i++) {
  822. if((cam->params.vp_params.gpio_data&GPIO_LIGHTS_MASK) ==
  823. lights_controls[i].value) {
  824. break;
  825. }
  826. }
  827. if(i == NUM_LIGHTS_CONTROLS)
  828. return -EINVAL;
  829. c->value = i;
  830. break;
  831. }
  832. case CPIA2_CID_RESET_CAMERA:
  833. return -EINVAL;
  834. default:
  835. return -EINVAL;
  836. }
  837. DBG("Get control id:%d, value:%d\n", c->id, c->value);
  838. return 0;
  839. }
  840. /******************************************************************************
  841. *
  842. * ioctl_s_ctrl
  843. *
  844. * V4L2 set the value of a control variable
  845. *
  846. *****************************************************************************/
  847. static int cpia2_s_ctrl(struct file *file, void *fh, struct v4l2_control *c)
  848. {
  849. struct camera_data *cam = video_drvdata(file);
  850. int i;
  851. int retval = 0;
  852. DBG("Set control id:%d, value:%d\n", c->id, c->value);
  853. /* Check that the value is in range */
  854. for(i=0; i<NUM_CONTROLS; i++) {
  855. if(c->id == controls[i].id) {
  856. if(c->value < controls[i].minimum ||
  857. c->value > controls[i].maximum) {
  858. return -EINVAL;
  859. }
  860. break;
  861. }
  862. }
  863. if(i == NUM_CONTROLS)
  864. return -EINVAL;
  865. switch(c->id) {
  866. case V4L2_CID_BRIGHTNESS:
  867. cpia2_set_brightness(cam, c->value);
  868. break;
  869. case V4L2_CID_CONTRAST:
  870. cpia2_set_contrast(cam, c->value);
  871. break;
  872. case V4L2_CID_SATURATION:
  873. cpia2_set_saturation(cam, c->value);
  874. break;
  875. case V4L2_CID_HFLIP:
  876. cpia2_set_property_mirror(cam, c->value);
  877. break;
  878. case V4L2_CID_VFLIP:
  879. cpia2_set_property_flip(cam, c->value);
  880. break;
  881. case CPIA2_CID_TARGET_KB:
  882. retval = cpia2_set_target_kb(cam, c->value);
  883. break;
  884. case CPIA2_CID_GPIO:
  885. retval = cpia2_set_gpio(cam, c->value);
  886. break;
  887. case CPIA2_CID_FLICKER_MODE:
  888. retval = cpia2_set_flicker_mode(cam,
  889. flicker_controls[c->value].value);
  890. break;
  891. case CPIA2_CID_FRAMERATE:
  892. retval = cpia2_set_fps(cam, framerate_controls[c->value].value);
  893. break;
  894. case CPIA2_CID_USB_ALT:
  895. retval = cpia2_usb_change_streaming_alternate(cam, c->value);
  896. break;
  897. case CPIA2_CID_LIGHTS:
  898. retval = cpia2_set_gpio(cam, lights_controls[c->value].value);
  899. break;
  900. case CPIA2_CID_RESET_CAMERA:
  901. cpia2_usb_stream_pause(cam);
  902. cpia2_reset_camera(cam);
  903. cpia2_usb_stream_resume(cam);
  904. break;
  905. default:
  906. retval = -EINVAL;
  907. }
  908. return retval;
  909. }
  910. /******************************************************************************
  911. *
  912. * ioctl_g_jpegcomp
  913. *
  914. * V4L2 get the JPEG compression parameters
  915. *
  916. *****************************************************************************/
  917. static int cpia2_g_jpegcomp(struct file *file, void *fh, struct v4l2_jpegcompression *parms)
  918. {
  919. struct camera_data *cam = video_drvdata(file);
  920. memset(parms, 0, sizeof(*parms));
  921. parms->quality = 80; // TODO: Can this be made meaningful?
  922. parms->jpeg_markers = V4L2_JPEG_MARKER_DQT | V4L2_JPEG_MARKER_DRI;
  923. if(!cam->params.compression.inhibit_htables) {
  924. parms->jpeg_markers |= V4L2_JPEG_MARKER_DHT;
  925. }
  926. parms->APPn = cam->APPn;
  927. parms->APP_len = cam->APP_len;
  928. if(cam->APP_len > 0) {
  929. memcpy(parms->APP_data, cam->APP_data, cam->APP_len);
  930. parms->jpeg_markers |= V4L2_JPEG_MARKER_APP;
  931. }
  932. parms->COM_len = cam->COM_len;
  933. if(cam->COM_len > 0) {
  934. memcpy(parms->COM_data, cam->COM_data, cam->COM_len);
  935. parms->jpeg_markers |= JPEG_MARKER_COM;
  936. }
  937. DBG("G_JPEGCOMP APP_len:%d COM_len:%d\n",
  938. parms->APP_len, parms->COM_len);
  939. return 0;
  940. }
  941. /******************************************************************************
  942. *
  943. * ioctl_s_jpegcomp
  944. *
  945. * V4L2 set the JPEG compression parameters
  946. * NOTE: quality and some jpeg_markers are ignored.
  947. *
  948. *****************************************************************************/
  949. static int cpia2_s_jpegcomp(struct file *file, void *fh, struct v4l2_jpegcompression *parms)
  950. {
  951. struct camera_data *cam = video_drvdata(file);
  952. DBG("S_JPEGCOMP APP_len:%d COM_len:%d\n",
  953. parms->APP_len, parms->COM_len);
  954. cam->params.compression.inhibit_htables =
  955. !(parms->jpeg_markers & V4L2_JPEG_MARKER_DHT);
  956. if(parms->APP_len != 0) {
  957. if(parms->APP_len > 0 &&
  958. parms->APP_len <= sizeof(cam->APP_data) &&
  959. parms->APPn >= 0 && parms->APPn <= 15) {
  960. cam->APPn = parms->APPn;
  961. cam->APP_len = parms->APP_len;
  962. memcpy(cam->APP_data, parms->APP_data, parms->APP_len);
  963. } else {
  964. LOG("Bad APPn Params n=%d len=%d\n",
  965. parms->APPn, parms->APP_len);
  966. return -EINVAL;
  967. }
  968. } else {
  969. cam->APP_len = 0;
  970. }
  971. if(parms->COM_len != 0) {
  972. if(parms->COM_len > 0 &&
  973. parms->COM_len <= sizeof(cam->COM_data)) {
  974. cam->COM_len = parms->COM_len;
  975. memcpy(cam->COM_data, parms->COM_data, parms->COM_len);
  976. } else {
  977. LOG("Bad COM_len=%d\n", parms->COM_len);
  978. return -EINVAL;
  979. }
  980. }
  981. return 0;
  982. }
  983. /******************************************************************************
  984. *
  985. * ioctl_reqbufs
  986. *
  987. * V4L2 Initiate memory mapping.
  988. * NOTE: The user's request is ignored. For now the buffers are fixed.
  989. *
  990. *****************************************************************************/
  991. static int cpia2_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *req)
  992. {
  993. struct camera_data *cam = video_drvdata(file);
  994. if(req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  995. req->memory != V4L2_MEMORY_MMAP)
  996. return -EINVAL;
  997. DBG("REQBUFS requested:%d returning:%d\n", req->count, cam->num_frames);
  998. req->count = cam->num_frames;
  999. memset(&req->reserved, 0, sizeof(req->reserved));
  1000. return 0;
  1001. }
  1002. /******************************************************************************
  1003. *
  1004. * ioctl_querybuf
  1005. *
  1006. * V4L2 Query memory buffer status.
  1007. *
  1008. *****************************************************************************/
  1009. static int cpia2_querybuf(struct file *file, void *fh, struct v4l2_buffer *buf)
  1010. {
  1011. struct camera_data *cam = video_drvdata(file);
  1012. if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  1013. buf->index > cam->num_frames)
  1014. return -EINVAL;
  1015. buf->m.offset = cam->buffers[buf->index].data - cam->frame_buffer;
  1016. buf->length = cam->frame_size;
  1017. buf->memory = V4L2_MEMORY_MMAP;
  1018. if(cam->mmapped)
  1019. buf->flags = V4L2_BUF_FLAG_MAPPED;
  1020. else
  1021. buf->flags = 0;
  1022. switch (cam->buffers[buf->index].status) {
  1023. case FRAME_EMPTY:
  1024. case FRAME_ERROR:
  1025. case FRAME_READING:
  1026. buf->bytesused = 0;
  1027. buf->flags = V4L2_BUF_FLAG_QUEUED;
  1028. break;
  1029. case FRAME_READY:
  1030. buf->bytesused = cam->buffers[buf->index].length;
  1031. buf->timestamp = cam->buffers[buf->index].timestamp;
  1032. buf->sequence = cam->buffers[buf->index].seq;
  1033. buf->flags = V4L2_BUF_FLAG_DONE;
  1034. break;
  1035. }
  1036. DBG("QUERYBUF index:%d offset:%d flags:%d seq:%d bytesused:%d\n",
  1037. buf->index, buf->m.offset, buf->flags, buf->sequence,
  1038. buf->bytesused);
  1039. return 0;
  1040. }
  1041. /******************************************************************************
  1042. *
  1043. * ioctl_qbuf
  1044. *
  1045. * V4L2 User is freeing buffer
  1046. *
  1047. *****************************************************************************/
  1048. static int cpia2_qbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
  1049. {
  1050. struct camera_data *cam = video_drvdata(file);
  1051. if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  1052. buf->memory != V4L2_MEMORY_MMAP ||
  1053. buf->index > cam->num_frames)
  1054. return -EINVAL;
  1055. DBG("QBUF #%d\n", buf->index);
  1056. if(cam->buffers[buf->index].status == FRAME_READY)
  1057. cam->buffers[buf->index].status = FRAME_EMPTY;
  1058. return 0;
  1059. }
  1060. /******************************************************************************
  1061. *
  1062. * find_earliest_filled_buffer
  1063. *
  1064. * Helper for ioctl_dqbuf. Find the next ready buffer.
  1065. *
  1066. *****************************************************************************/
  1067. static int find_earliest_filled_buffer(struct camera_data *cam)
  1068. {
  1069. int i;
  1070. int found = -1;
  1071. for (i=0; i<cam->num_frames; i++) {
  1072. if(cam->buffers[i].status == FRAME_READY) {
  1073. if(found < 0) {
  1074. found = i;
  1075. } else {
  1076. /* find which buffer is earlier */
  1077. struct timeval *tv1, *tv2;
  1078. tv1 = &cam->buffers[i].timestamp;
  1079. tv2 = &cam->buffers[found].timestamp;
  1080. if(tv1->tv_sec < tv2->tv_sec ||
  1081. (tv1->tv_sec == tv2->tv_sec &&
  1082. tv1->tv_usec < tv2->tv_usec))
  1083. found = i;
  1084. }
  1085. }
  1086. }
  1087. return found;
  1088. }
  1089. /******************************************************************************
  1090. *
  1091. * ioctl_dqbuf
  1092. *
  1093. * V4L2 User is asking for a filled buffer.
  1094. *
  1095. *****************************************************************************/
  1096. static int cpia2_dqbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
  1097. {
  1098. struct camera_data *cam = video_drvdata(file);
  1099. int frame;
  1100. if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  1101. buf->memory != V4L2_MEMORY_MMAP)
  1102. return -EINVAL;
  1103. frame = find_earliest_filled_buffer(cam);
  1104. if(frame < 0 && file->f_flags&O_NONBLOCK)
  1105. return -EAGAIN;
  1106. if(frame < 0) {
  1107. /* Wait for a frame to become available */
  1108. struct framebuf *cb=cam->curbuff;
  1109. mutex_unlock(&cam->v4l2_lock);
  1110. wait_event_interruptible(cam->wq_stream,
  1111. !cam->present ||
  1112. (cb=cam->curbuff)->status == FRAME_READY);
  1113. mutex_lock(&cam->v4l2_lock);
  1114. if (signal_pending(current))
  1115. return -ERESTARTSYS;
  1116. if(!cam->present)
  1117. return -ENOTTY;
  1118. frame = cb->num;
  1119. }
  1120. buf->index = frame;
  1121. buf->bytesused = cam->buffers[buf->index].length;
  1122. buf->flags = V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_DONE;
  1123. buf->field = V4L2_FIELD_NONE;
  1124. buf->timestamp = cam->buffers[buf->index].timestamp;
  1125. buf->sequence = cam->buffers[buf->index].seq;
  1126. buf->m.offset = cam->buffers[buf->index].data - cam->frame_buffer;
  1127. buf->length = cam->frame_size;
  1128. buf->input = 0;
  1129. buf->reserved = 0;
  1130. memset(&buf->timecode, 0, sizeof(buf->timecode));
  1131. DBG("DQBUF #%d status:%d seq:%d length:%d\n", buf->index,
  1132. cam->buffers[buf->index].status, buf->sequence, buf->bytesused);
  1133. return 0;
  1134. }
  1135. static int cpia2_g_priority(struct file *file, void *_fh, enum v4l2_priority *p)
  1136. {
  1137. struct cpia2_fh *fh = _fh;
  1138. *p = fh->prio;
  1139. return 0;
  1140. }
  1141. static int cpia2_s_priority(struct file *file, void *_fh, enum v4l2_priority prio)
  1142. {
  1143. struct camera_data *cam = video_drvdata(file);
  1144. struct cpia2_fh *fh = fh;
  1145. if (cam->streaming && prio != fh->prio &&
  1146. fh->prio == V4L2_PRIORITY_RECORD)
  1147. /* Can't drop record priority while streaming */
  1148. return -EBUSY;
  1149. if (prio == V4L2_PRIORITY_RECORD && prio != fh->prio &&
  1150. v4l2_prio_max(&cam->prio) == V4L2_PRIORITY_RECORD)
  1151. /* Only one program can record at a time */
  1152. return -EBUSY;
  1153. return v4l2_prio_change(&cam->prio, &fh->prio, prio);
  1154. }
  1155. static int cpia2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
  1156. {
  1157. struct camera_data *cam = video_drvdata(file);
  1158. DBG("VIDIOC_STREAMON, streaming=%d\n", cam->streaming);
  1159. if (!cam->mmapped || type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1160. return -EINVAL;
  1161. if (!cam->streaming)
  1162. return cpia2_usb_stream_start(cam,
  1163. cam->params.camera_state.stream_mode);
  1164. return -EINVAL;
  1165. }
  1166. static int cpia2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
  1167. {
  1168. struct camera_data *cam = video_drvdata(file);
  1169. DBG("VIDIOC_STREAMOFF, streaming=%d\n", cam->streaming);
  1170. if (!cam->mmapped || type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1171. return -EINVAL;
  1172. if (cam->streaming)
  1173. return cpia2_usb_stream_stop(cam);
  1174. return -EINVAL;
  1175. }
  1176. /******************************************************************************
  1177. *
  1178. * cpia2_mmap
  1179. *
  1180. *****************************************************************************/
  1181. static int cpia2_mmap(struct file *file, struct vm_area_struct *area)
  1182. {
  1183. struct camera_data *cam = video_drvdata(file);
  1184. int retval;
  1185. /* Priority check */
  1186. struct cpia2_fh *fh = file->private_data;
  1187. if(fh->prio != V4L2_PRIORITY_RECORD) {
  1188. return -EBUSY;
  1189. }
  1190. retval = cpia2_remap_buffer(cam, area);
  1191. if(!retval)
  1192. fh->mmapped = 1;
  1193. return retval;
  1194. }
  1195. /******************************************************************************
  1196. *
  1197. * reset_camera_struct_v4l
  1198. *
  1199. * Sets all values to the defaults
  1200. *****************************************************************************/
  1201. static void reset_camera_struct_v4l(struct camera_data *cam)
  1202. {
  1203. cam->width = cam->params.roi.width;
  1204. cam->height = cam->params.roi.height;
  1205. cam->frame_size = buffer_size;
  1206. cam->num_frames = num_buffers;
  1207. /* FlickerModes */
  1208. cam->params.flicker_control.flicker_mode_req = flicker_mode;
  1209. cam->params.flicker_control.mains_frequency = flicker_freq;
  1210. /* streamMode */
  1211. cam->params.camera_state.stream_mode = alternate;
  1212. cam->pixelformat = V4L2_PIX_FMT_JPEG;
  1213. v4l2_prio_init(&cam->prio);
  1214. }
  1215. static const struct v4l2_ioctl_ops cpia2_ioctl_ops = {
  1216. .vidioc_querycap = cpia2_querycap,
  1217. .vidioc_enum_input = cpia2_enum_input,
  1218. .vidioc_g_input = cpia2_g_input,
  1219. .vidioc_s_input = cpia2_s_input,
  1220. .vidioc_enum_fmt_vid_cap = cpia2_enum_fmt_vid_cap,
  1221. .vidioc_g_fmt_vid_cap = cpia2_g_fmt_vid_cap,
  1222. .vidioc_s_fmt_vid_cap = cpia2_s_fmt_vid_cap,
  1223. .vidioc_try_fmt_vid_cap = cpia2_try_fmt_vid_cap,
  1224. .vidioc_queryctrl = cpia2_queryctrl,
  1225. .vidioc_querymenu = cpia2_querymenu,
  1226. .vidioc_g_ctrl = cpia2_g_ctrl,
  1227. .vidioc_s_ctrl = cpia2_s_ctrl,
  1228. .vidioc_g_jpegcomp = cpia2_g_jpegcomp,
  1229. .vidioc_s_jpegcomp = cpia2_s_jpegcomp,
  1230. .vidioc_cropcap = cpia2_cropcap,
  1231. .vidioc_reqbufs = cpia2_reqbufs,
  1232. .vidioc_querybuf = cpia2_querybuf,
  1233. .vidioc_qbuf = cpia2_qbuf,
  1234. .vidioc_dqbuf = cpia2_dqbuf,
  1235. .vidioc_streamon = cpia2_streamon,
  1236. .vidioc_streamoff = cpia2_streamoff,
  1237. .vidioc_g_priority = cpia2_g_priority,
  1238. .vidioc_s_priority = cpia2_s_priority,
  1239. .vidioc_default = cpia2_default,
  1240. };
  1241. /***
  1242. * The v4l video device structure initialized for this device
  1243. ***/
  1244. static const struct v4l2_file_operations cpia2_fops = {
  1245. .owner = THIS_MODULE,
  1246. .open = cpia2_open,
  1247. .release = cpia2_close,
  1248. .read = cpia2_v4l_read,
  1249. .poll = cpia2_v4l_poll,
  1250. .unlocked_ioctl = video_ioctl2,
  1251. .mmap = cpia2_mmap,
  1252. };
  1253. static struct video_device cpia2_template = {
  1254. /* I could not find any place for the old .initialize initializer?? */
  1255. .name = "CPiA2 Camera",
  1256. .fops = &cpia2_fops,
  1257. .ioctl_ops = &cpia2_ioctl_ops,
  1258. .release = video_device_release,
  1259. };
  1260. /******************************************************************************
  1261. *
  1262. * cpia2_register_camera
  1263. *
  1264. *****************************************************************************/
  1265. int cpia2_register_camera(struct camera_data *cam)
  1266. {
  1267. cam->vdev = video_device_alloc();
  1268. if(!cam->vdev)
  1269. return -ENOMEM;
  1270. memcpy(cam->vdev, &cpia2_template, sizeof(cpia2_template));
  1271. video_set_drvdata(cam->vdev, cam);
  1272. cam->vdev->lock = &cam->v4l2_lock;
  1273. reset_camera_struct_v4l(cam);
  1274. /* register v4l device */
  1275. if (video_register_device(cam->vdev, VFL_TYPE_GRABBER, video_nr) < 0) {
  1276. ERR("video_register_device failed\n");
  1277. video_device_release(cam->vdev);
  1278. return -ENODEV;
  1279. }
  1280. return 0;
  1281. }
  1282. /******************************************************************************
  1283. *
  1284. * cpia2_unregister_camera
  1285. *
  1286. *****************************************************************************/
  1287. void cpia2_unregister_camera(struct camera_data *cam)
  1288. {
  1289. if (!cam->open_count) {
  1290. video_unregister_device(cam->vdev);
  1291. } else {
  1292. LOG("%s removed while open, deferring "
  1293. "video_unregister_device\n",
  1294. video_device_node_name(cam->vdev));
  1295. }
  1296. }
  1297. /******************************************************************************
  1298. *
  1299. * check_parameters
  1300. *
  1301. * Make sure that all user-supplied parameters are sensible
  1302. *****************************************************************************/
  1303. static void __init check_parameters(void)
  1304. {
  1305. if(buffer_size < PAGE_SIZE) {
  1306. buffer_size = PAGE_SIZE;
  1307. LOG("buffer_size too small, setting to %d\n", buffer_size);
  1308. } else if(buffer_size > 1024*1024) {
  1309. /* arbitrary upper limiit */
  1310. buffer_size = 1024*1024;
  1311. LOG("buffer_size ridiculously large, setting to %d\n",
  1312. buffer_size);
  1313. } else {
  1314. buffer_size += PAGE_SIZE-1;
  1315. buffer_size &= ~(PAGE_SIZE-1);
  1316. }
  1317. if(num_buffers < 1) {
  1318. num_buffers = 1;
  1319. LOG("num_buffers too small, setting to %d\n", num_buffers);
  1320. } else if(num_buffers > VIDEO_MAX_FRAME) {
  1321. num_buffers = VIDEO_MAX_FRAME;
  1322. LOG("num_buffers too large, setting to %d\n", num_buffers);
  1323. }
  1324. if(alternate < USBIF_ISO_1 || alternate > USBIF_ISO_6) {
  1325. alternate = DEFAULT_ALT;
  1326. LOG("alternate specified is invalid, using %d\n", alternate);
  1327. }
  1328. if (flicker_mode != NEVER_FLICKER && flicker_mode != ANTI_FLICKER_ON) {
  1329. flicker_mode = NEVER_FLICKER;
  1330. LOG("Flicker mode specified is invalid, using %d\n",
  1331. flicker_mode);
  1332. }
  1333. if (flicker_freq != FLICKER_50 && flicker_freq != FLICKER_60) {
  1334. flicker_freq = FLICKER_60;
  1335. LOG("Flicker mode specified is invalid, using %d\n",
  1336. flicker_freq);
  1337. }
  1338. if(video_nr < -1 || video_nr > 64) {
  1339. video_nr = -1;
  1340. LOG("invalid video_nr specified, must be -1 to 64\n");
  1341. }
  1342. DBG("Using %d buffers, each %d bytes, alternate=%d\n",
  1343. num_buffers, buffer_size, alternate);
  1344. }
  1345. /************ Module Stuff ***************/
  1346. /******************************************************************************
  1347. *
  1348. * cpia2_init/module_init
  1349. *
  1350. *****************************************************************************/
  1351. static int __init cpia2_init(void)
  1352. {
  1353. LOG("%s v%d.%d.%d\n",
  1354. ABOUT, CPIA2_MAJ_VER, CPIA2_MIN_VER, CPIA2_PATCH_VER);
  1355. check_parameters();
  1356. cpia2_usb_init();
  1357. return 0;
  1358. }
  1359. /******************************************************************************
  1360. *
  1361. * cpia2_exit/module_exit
  1362. *
  1363. *****************************************************************************/
  1364. static void __exit cpia2_exit(void)
  1365. {
  1366. cpia2_usb_cleanup();
  1367. schedule_timeout(2 * HZ);
  1368. }
  1369. module_init(cpia2_init);
  1370. module_exit(cpia2_exit);