cpia2_v4l.c 40 KB

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