cpia2_v4l.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702
  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 int ioctl_set_gpio(void *arg, struct camera_data *cam)
  348. {
  349. __u32 gpio_val;
  350. gpio_val = *(__u32*) arg;
  351. if (gpio_val &~ 0xFFU)
  352. return -EINVAL;
  353. return cpia2_set_gpio(cam, (unsigned char)gpio_val);
  354. }
  355. /******************************************************************************
  356. *
  357. * ioctl_querycap
  358. *
  359. * V4L2 device capabilities
  360. *
  361. *****************************************************************************/
  362. static int ioctl_querycap(void *arg, struct camera_data *cam)
  363. {
  364. struct v4l2_capability *vc = arg;
  365. memset(vc, 0, sizeof(*vc));
  366. strcpy(vc->driver, "cpia2");
  367. if (cam->params.pnp_id.product == 0x151)
  368. strcpy(vc->card, "QX5 Microscope");
  369. else
  370. strcpy(vc->card, "CPiA2 Camera");
  371. switch (cam->params.pnp_id.device_type) {
  372. case DEVICE_STV_672:
  373. strcat(vc->card, " (672/");
  374. break;
  375. case DEVICE_STV_676:
  376. strcat(vc->card, " (676/");
  377. break;
  378. default:
  379. strcat(vc->card, " (???/");
  380. break;
  381. }
  382. switch (cam->params.version.sensor_flags) {
  383. case CPIA2_VP_SENSOR_FLAGS_404:
  384. strcat(vc->card, "404)");
  385. break;
  386. case CPIA2_VP_SENSOR_FLAGS_407:
  387. strcat(vc->card, "407)");
  388. break;
  389. case CPIA2_VP_SENSOR_FLAGS_409:
  390. strcat(vc->card, "409)");
  391. break;
  392. case CPIA2_VP_SENSOR_FLAGS_410:
  393. strcat(vc->card, "410)");
  394. break;
  395. case CPIA2_VP_SENSOR_FLAGS_500:
  396. strcat(vc->card, "500)");
  397. break;
  398. default:
  399. strcat(vc->card, "???)");
  400. break;
  401. }
  402. if (usb_make_path(cam->dev, vc->bus_info, sizeof(vc->bus_info)) <0)
  403. memset(vc->bus_info,0, sizeof(vc->bus_info));
  404. vc->version = KERNEL_VERSION(CPIA2_MAJ_VER, CPIA2_MIN_VER,
  405. CPIA2_PATCH_VER);
  406. vc->capabilities = V4L2_CAP_VIDEO_CAPTURE |
  407. V4L2_CAP_READWRITE |
  408. V4L2_CAP_STREAMING;
  409. return 0;
  410. }
  411. /******************************************************************************
  412. *
  413. * ioctl_input
  414. *
  415. * V4L2 input get/set/enumerate
  416. *
  417. *****************************************************************************/
  418. static int ioctl_input(unsigned int ioclt_nr,void *arg,struct camera_data *cam)
  419. {
  420. struct v4l2_input *i = arg;
  421. if(ioclt_nr != VIDIOC_G_INPUT) {
  422. if (i->index != 0)
  423. return -EINVAL;
  424. }
  425. memset(i, 0, sizeof(*i));
  426. strcpy(i->name, "Camera");
  427. i->type = V4L2_INPUT_TYPE_CAMERA;
  428. return 0;
  429. }
  430. /******************************************************************************
  431. *
  432. * ioctl_enum_fmt
  433. *
  434. * V4L2 format enumerate
  435. *
  436. *****************************************************************************/
  437. static int ioctl_enum_fmt(void *arg,struct camera_data *cam)
  438. {
  439. struct v4l2_fmtdesc *f = arg;
  440. int index = f->index;
  441. if (index < 0 || index > 1)
  442. return -EINVAL;
  443. memset(f, 0, sizeof(*f));
  444. f->index = index;
  445. f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  446. f->flags = V4L2_FMT_FLAG_COMPRESSED;
  447. switch(index) {
  448. case 0:
  449. strcpy(f->description, "MJPEG");
  450. f->pixelformat = V4L2_PIX_FMT_MJPEG;
  451. break;
  452. case 1:
  453. strcpy(f->description, "JPEG");
  454. f->pixelformat = V4L2_PIX_FMT_JPEG;
  455. break;
  456. default:
  457. return -EINVAL;
  458. }
  459. return 0;
  460. }
  461. /******************************************************************************
  462. *
  463. * ioctl_try_fmt
  464. *
  465. * V4L2 format try
  466. *
  467. *****************************************************************************/
  468. static int ioctl_try_fmt(void *arg,struct camera_data *cam)
  469. {
  470. struct v4l2_format *f = arg;
  471. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  472. return -EINVAL;
  473. if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG &&
  474. f->fmt.pix.pixelformat != V4L2_PIX_FMT_JPEG)
  475. return -EINVAL;
  476. f->fmt.pix.field = V4L2_FIELD_NONE;
  477. f->fmt.pix.bytesperline = 0;
  478. f->fmt.pix.sizeimage = cam->frame_size;
  479. f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
  480. f->fmt.pix.priv = 0;
  481. switch (cpia2_match_video_size(f->fmt.pix.width, f->fmt.pix.height)) {
  482. case VIDEOSIZE_VGA:
  483. f->fmt.pix.width = 640;
  484. f->fmt.pix.height = 480;
  485. break;
  486. case VIDEOSIZE_CIF:
  487. f->fmt.pix.width = 352;
  488. f->fmt.pix.height = 288;
  489. break;
  490. case VIDEOSIZE_QVGA:
  491. f->fmt.pix.width = 320;
  492. f->fmt.pix.height = 240;
  493. break;
  494. case VIDEOSIZE_288_216:
  495. f->fmt.pix.width = 288;
  496. f->fmt.pix.height = 216;
  497. break;
  498. case VIDEOSIZE_256_192:
  499. f->fmt.pix.width = 256;
  500. f->fmt.pix.height = 192;
  501. break;
  502. case VIDEOSIZE_224_168:
  503. f->fmt.pix.width = 224;
  504. f->fmt.pix.height = 168;
  505. break;
  506. case VIDEOSIZE_192_144:
  507. f->fmt.pix.width = 192;
  508. f->fmt.pix.height = 144;
  509. break;
  510. case VIDEOSIZE_QCIF:
  511. default:
  512. f->fmt.pix.width = 176;
  513. f->fmt.pix.height = 144;
  514. break;
  515. }
  516. return 0;
  517. }
  518. /******************************************************************************
  519. *
  520. * ioctl_set_fmt
  521. *
  522. * V4L2 format set
  523. *
  524. *****************************************************************************/
  525. static int ioctl_set_fmt(void *arg,struct camera_data *cam, struct cpia2_fh *fh)
  526. {
  527. struct v4l2_format *f = arg;
  528. int err, frame;
  529. err = ioctl_try_fmt(arg, cam);
  530. if(err != 0)
  531. return err;
  532. /* Ensure that only this process can change the format. */
  533. err = v4l2_prio_change(&cam->prio, &fh->prio, V4L2_PRIORITY_RECORD);
  534. if(err != 0) {
  535. return err;
  536. }
  537. cam->pixelformat = f->fmt.pix.pixelformat;
  538. /* NOTE: This should be set to 1 for MJPEG, but some apps don't handle
  539. * the missing Huffman table properly. */
  540. cam->params.compression.inhibit_htables = 0;
  541. /*f->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG;*/
  542. /* we set the video window to something smaller or equal to what
  543. * is requested by the user???
  544. */
  545. DBG("Requested width = %d, height = %d\n",
  546. f->fmt.pix.width, f->fmt.pix.height);
  547. if (f->fmt.pix.width != cam->width ||
  548. f->fmt.pix.height != cam->height) {
  549. cam->width = f->fmt.pix.width;
  550. cam->height = f->fmt.pix.height;
  551. cam->params.roi.width = f->fmt.pix.width;
  552. cam->params.roi.height = f->fmt.pix.height;
  553. cpia2_set_format(cam);
  554. }
  555. for (frame = 0; frame < cam->num_frames; ++frame) {
  556. if (cam->buffers[frame].status == FRAME_READING)
  557. if ((err = sync(cam, frame)) < 0)
  558. return err;
  559. cam->buffers[frame].status = FRAME_EMPTY;
  560. }
  561. return 0;
  562. }
  563. /******************************************************************************
  564. *
  565. * ioctl_get_fmt
  566. *
  567. * V4L2 format get
  568. *
  569. *****************************************************************************/
  570. static int ioctl_get_fmt(void *arg,struct camera_data *cam)
  571. {
  572. struct v4l2_format *f = arg;
  573. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  574. return -EINVAL;
  575. f->fmt.pix.width = cam->width;
  576. f->fmt.pix.height = cam->height;
  577. f->fmt.pix.pixelformat = cam->pixelformat;
  578. f->fmt.pix.field = V4L2_FIELD_NONE;
  579. f->fmt.pix.bytesperline = 0;
  580. f->fmt.pix.sizeimage = cam->frame_size;
  581. f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
  582. f->fmt.pix.priv = 0;
  583. return 0;
  584. }
  585. /******************************************************************************
  586. *
  587. * ioctl_cropcap
  588. *
  589. * V4L2 query cropping capabilities
  590. * NOTE: cropping is currently disabled
  591. *
  592. *****************************************************************************/
  593. static int ioctl_cropcap(void *arg,struct camera_data *cam)
  594. {
  595. struct v4l2_cropcap *c = arg;
  596. if (c->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  597. return -EINVAL;
  598. c->bounds.left = 0;
  599. c->bounds.top = 0;
  600. c->bounds.width = cam->width;
  601. c->bounds.height = cam->height;
  602. c->defrect.left = 0;
  603. c->defrect.top = 0;
  604. c->defrect.width = cam->width;
  605. c->defrect.height = cam->height;
  606. c->pixelaspect.numerator = 1;
  607. c->pixelaspect.denominator = 1;
  608. return 0;
  609. }
  610. /******************************************************************************
  611. *
  612. * ioctl_queryctrl
  613. *
  614. * V4L2 query possible control variables
  615. *
  616. *****************************************************************************/
  617. static int ioctl_queryctrl(void *arg,struct camera_data *cam)
  618. {
  619. struct v4l2_queryctrl *c = arg;
  620. int i;
  621. for(i=0; i<NUM_CONTROLS; ++i) {
  622. if(c->id == controls[i].id) {
  623. memcpy(c, controls+i, sizeof(*c));
  624. break;
  625. }
  626. }
  627. if(i == NUM_CONTROLS)
  628. return -EINVAL;
  629. /* Some devices have additional limitations */
  630. switch(c->id) {
  631. case V4L2_CID_BRIGHTNESS:
  632. /***
  633. * Don't let the register be set to zero - bug in VP4
  634. * flash of full brightness
  635. ***/
  636. if (cam->params.pnp_id.device_type == DEVICE_STV_672)
  637. c->minimum = 1;
  638. break;
  639. case V4L2_CID_VFLIP:
  640. // VP5 Only
  641. if(cam->params.pnp_id.device_type == DEVICE_STV_672)
  642. c->flags |= V4L2_CTRL_FLAG_DISABLED;
  643. break;
  644. case CPIA2_CID_FRAMERATE:
  645. if(cam->params.pnp_id.device_type == DEVICE_STV_672 &&
  646. cam->params.version.sensor_flags==CPIA2_VP_SENSOR_FLAGS_500){
  647. // Maximum 15fps
  648. for(i=0; i<c->maximum; ++i) {
  649. if(framerate_controls[i].value ==
  650. CPIA2_VP_FRAMERATE_15) {
  651. c->maximum = i;
  652. c->default_value = i;
  653. }
  654. }
  655. }
  656. break;
  657. case CPIA2_CID_FLICKER_MODE:
  658. // Flicker control only valid for 672.
  659. if(cam->params.pnp_id.device_type != DEVICE_STV_672)
  660. c->flags |= V4L2_CTRL_FLAG_DISABLED;
  661. break;
  662. case CPIA2_CID_LIGHTS:
  663. // Light control only valid for the QX5 Microscope.
  664. if(cam->params.pnp_id.product != 0x151)
  665. c->flags |= V4L2_CTRL_FLAG_DISABLED;
  666. break;
  667. default:
  668. break;
  669. }
  670. return 0;
  671. }
  672. /******************************************************************************
  673. *
  674. * ioctl_querymenu
  675. *
  676. * V4L2 query possible control variables
  677. *
  678. *****************************************************************************/
  679. static int ioctl_querymenu(void *arg,struct camera_data *cam)
  680. {
  681. struct v4l2_querymenu *m = arg;
  682. memset(m->name, 0, sizeof(m->name));
  683. m->reserved = 0;
  684. switch(m->id) {
  685. case CPIA2_CID_FLICKER_MODE:
  686. if (m->index >= NUM_FLICKER_CONTROLS)
  687. return -EINVAL;
  688. strcpy(m->name, flicker_controls[m->index].name);
  689. break;
  690. case CPIA2_CID_FRAMERATE:
  691. {
  692. int maximum = NUM_FRAMERATE_CONTROLS - 1;
  693. if(cam->params.pnp_id.device_type == DEVICE_STV_672 &&
  694. cam->params.version.sensor_flags==CPIA2_VP_SENSOR_FLAGS_500){
  695. // Maximum 15fps
  696. int i;
  697. for(i=0; i<maximum; ++i) {
  698. if(framerate_controls[i].value ==
  699. CPIA2_VP_FRAMERATE_15)
  700. maximum = i;
  701. }
  702. }
  703. if (m->index > maximum)
  704. return -EINVAL;
  705. strcpy(m->name, framerate_controls[m->index].name);
  706. break;
  707. }
  708. case CPIA2_CID_LIGHTS:
  709. if (m->index >= NUM_LIGHTS_CONTROLS)
  710. return -EINVAL;
  711. strcpy(m->name, lights_controls[m->index].name);
  712. break;
  713. default:
  714. return -EINVAL;
  715. }
  716. return 0;
  717. }
  718. /******************************************************************************
  719. *
  720. * ioctl_g_ctrl
  721. *
  722. * V4L2 get the value of a control variable
  723. *
  724. *****************************************************************************/
  725. static int ioctl_g_ctrl(void *arg,struct camera_data *cam)
  726. {
  727. struct v4l2_control *c = arg;
  728. switch(c->id) {
  729. case V4L2_CID_BRIGHTNESS:
  730. cpia2_do_command(cam, CPIA2_CMD_GET_VP_BRIGHTNESS,
  731. TRANSFER_READ, 0);
  732. c->value = cam->params.color_params.brightness;
  733. break;
  734. case V4L2_CID_CONTRAST:
  735. cpia2_do_command(cam, CPIA2_CMD_GET_CONTRAST,
  736. TRANSFER_READ, 0);
  737. c->value = cam->params.color_params.contrast;
  738. break;
  739. case V4L2_CID_SATURATION:
  740. cpia2_do_command(cam, CPIA2_CMD_GET_VP_SATURATION,
  741. TRANSFER_READ, 0);
  742. c->value = cam->params.color_params.saturation;
  743. break;
  744. case V4L2_CID_HFLIP:
  745. cpia2_do_command(cam, CPIA2_CMD_GET_USER_EFFECTS,
  746. TRANSFER_READ, 0);
  747. c->value = (cam->params.vp_params.user_effects &
  748. CPIA2_VP_USER_EFFECTS_MIRROR) != 0;
  749. break;
  750. case V4L2_CID_VFLIP:
  751. cpia2_do_command(cam, CPIA2_CMD_GET_USER_EFFECTS,
  752. TRANSFER_READ, 0);
  753. c->value = (cam->params.vp_params.user_effects &
  754. CPIA2_VP_USER_EFFECTS_FLIP) != 0;
  755. break;
  756. case CPIA2_CID_TARGET_KB:
  757. c->value = cam->params.vc_params.target_kb;
  758. break;
  759. case CPIA2_CID_GPIO:
  760. cpia2_do_command(cam, CPIA2_CMD_GET_VP_GPIO_DATA,
  761. TRANSFER_READ, 0);
  762. c->value = cam->params.vp_params.gpio_data;
  763. break;
  764. case CPIA2_CID_FLICKER_MODE:
  765. {
  766. int i, mode;
  767. cpia2_do_command(cam, CPIA2_CMD_GET_FLICKER_MODES,
  768. TRANSFER_READ, 0);
  769. if(cam->params.flicker_control.cam_register &
  770. CPIA2_VP_FLICKER_MODES_NEVER_FLICKER) {
  771. mode = NEVER_FLICKER;
  772. } else {
  773. if(cam->params.flicker_control.cam_register &
  774. CPIA2_VP_FLICKER_MODES_50HZ) {
  775. mode = FLICKER_50;
  776. } else {
  777. mode = FLICKER_60;
  778. }
  779. }
  780. for(i=0; i<NUM_FLICKER_CONTROLS; i++) {
  781. if(flicker_controls[i].value == mode) {
  782. c->value = i;
  783. break;
  784. }
  785. }
  786. if(i == NUM_FLICKER_CONTROLS)
  787. return -EINVAL;
  788. break;
  789. }
  790. case CPIA2_CID_FRAMERATE:
  791. {
  792. int maximum = NUM_FRAMERATE_CONTROLS - 1;
  793. int i;
  794. for(i=0; i<= maximum; i++) {
  795. if(cam->params.vp_params.frame_rate ==
  796. framerate_controls[i].value)
  797. break;
  798. }
  799. if(i > maximum)
  800. return -EINVAL;
  801. c->value = i;
  802. break;
  803. }
  804. case CPIA2_CID_USB_ALT:
  805. c->value = cam->params.camera_state.stream_mode;
  806. break;
  807. case CPIA2_CID_LIGHTS:
  808. {
  809. int i;
  810. cpia2_do_command(cam, CPIA2_CMD_GET_VP_GPIO_DATA,
  811. TRANSFER_READ, 0);
  812. for(i=0; i<NUM_LIGHTS_CONTROLS; i++) {
  813. if((cam->params.vp_params.gpio_data&GPIO_LIGHTS_MASK) ==
  814. lights_controls[i].value) {
  815. break;
  816. }
  817. }
  818. if(i == NUM_LIGHTS_CONTROLS)
  819. return -EINVAL;
  820. c->value = i;
  821. break;
  822. }
  823. case CPIA2_CID_RESET_CAMERA:
  824. return -EINVAL;
  825. default:
  826. return -EINVAL;
  827. }
  828. DBG("Get control id:%d, value:%d\n", c->id, c->value);
  829. return 0;
  830. }
  831. /******************************************************************************
  832. *
  833. * ioctl_s_ctrl
  834. *
  835. * V4L2 set the value of a control variable
  836. *
  837. *****************************************************************************/
  838. static int ioctl_s_ctrl(void *arg,struct camera_data *cam)
  839. {
  840. struct v4l2_control *c = arg;
  841. int i;
  842. int retval = 0;
  843. DBG("Set control id:%d, value:%d\n", c->id, c->value);
  844. /* Check that the value is in range */
  845. for(i=0; i<NUM_CONTROLS; i++) {
  846. if(c->id == controls[i].id) {
  847. if(c->value < controls[i].minimum ||
  848. c->value > controls[i].maximum) {
  849. return -EINVAL;
  850. }
  851. break;
  852. }
  853. }
  854. if(i == NUM_CONTROLS)
  855. return -EINVAL;
  856. switch(c->id) {
  857. case V4L2_CID_BRIGHTNESS:
  858. cpia2_set_brightness(cam, c->value);
  859. break;
  860. case V4L2_CID_CONTRAST:
  861. cpia2_set_contrast(cam, c->value);
  862. break;
  863. case V4L2_CID_SATURATION:
  864. cpia2_set_saturation(cam, c->value);
  865. break;
  866. case V4L2_CID_HFLIP:
  867. cpia2_set_property_mirror(cam, c->value);
  868. break;
  869. case V4L2_CID_VFLIP:
  870. cpia2_set_property_flip(cam, c->value);
  871. break;
  872. case CPIA2_CID_TARGET_KB:
  873. retval = cpia2_set_target_kb(cam, c->value);
  874. break;
  875. case CPIA2_CID_GPIO:
  876. retval = cpia2_set_gpio(cam, c->value);
  877. break;
  878. case CPIA2_CID_FLICKER_MODE:
  879. retval = cpia2_set_flicker_mode(cam,
  880. flicker_controls[c->value].value);
  881. break;
  882. case CPIA2_CID_FRAMERATE:
  883. retval = cpia2_set_fps(cam, framerate_controls[c->value].value);
  884. break;
  885. case CPIA2_CID_USB_ALT:
  886. retval = cpia2_usb_change_streaming_alternate(cam, c->value);
  887. break;
  888. case CPIA2_CID_LIGHTS:
  889. retval = cpia2_set_gpio(cam, lights_controls[c->value].value);
  890. break;
  891. case CPIA2_CID_RESET_CAMERA:
  892. cpia2_usb_stream_pause(cam);
  893. cpia2_reset_camera(cam);
  894. cpia2_usb_stream_resume(cam);
  895. break;
  896. default:
  897. retval = -EINVAL;
  898. }
  899. return retval;
  900. }
  901. /******************************************************************************
  902. *
  903. * ioctl_g_jpegcomp
  904. *
  905. * V4L2 get the JPEG compression parameters
  906. *
  907. *****************************************************************************/
  908. static int ioctl_g_jpegcomp(void *arg,struct camera_data *cam)
  909. {
  910. struct v4l2_jpegcompression *parms = arg;
  911. memset(parms, 0, sizeof(*parms));
  912. parms->quality = 80; // TODO: Can this be made meaningful?
  913. parms->jpeg_markers = V4L2_JPEG_MARKER_DQT | V4L2_JPEG_MARKER_DRI;
  914. if(!cam->params.compression.inhibit_htables) {
  915. parms->jpeg_markers |= V4L2_JPEG_MARKER_DHT;
  916. }
  917. parms->APPn = cam->APPn;
  918. parms->APP_len = cam->APP_len;
  919. if(cam->APP_len > 0) {
  920. memcpy(parms->APP_data, cam->APP_data, cam->APP_len);
  921. parms->jpeg_markers |= V4L2_JPEG_MARKER_APP;
  922. }
  923. parms->COM_len = cam->COM_len;
  924. if(cam->COM_len > 0) {
  925. memcpy(parms->COM_data, cam->COM_data, cam->COM_len);
  926. parms->jpeg_markers |= JPEG_MARKER_COM;
  927. }
  928. DBG("G_JPEGCOMP APP_len:%d COM_len:%d\n",
  929. parms->APP_len, parms->COM_len);
  930. return 0;
  931. }
  932. /******************************************************************************
  933. *
  934. * ioctl_s_jpegcomp
  935. *
  936. * V4L2 set the JPEG compression parameters
  937. * NOTE: quality and some jpeg_markers are ignored.
  938. *
  939. *****************************************************************************/
  940. static int ioctl_s_jpegcomp(void *arg,struct camera_data *cam)
  941. {
  942. struct v4l2_jpegcompression *parms = arg;
  943. DBG("S_JPEGCOMP APP_len:%d COM_len:%d\n",
  944. parms->APP_len, parms->COM_len);
  945. cam->params.compression.inhibit_htables =
  946. !(parms->jpeg_markers & V4L2_JPEG_MARKER_DHT);
  947. if(parms->APP_len != 0) {
  948. if(parms->APP_len > 0 &&
  949. parms->APP_len <= sizeof(cam->APP_data) &&
  950. parms->APPn >= 0 && parms->APPn <= 15) {
  951. cam->APPn = parms->APPn;
  952. cam->APP_len = parms->APP_len;
  953. memcpy(cam->APP_data, parms->APP_data, parms->APP_len);
  954. } else {
  955. LOG("Bad APPn Params n=%d len=%d\n",
  956. parms->APPn, parms->APP_len);
  957. return -EINVAL;
  958. }
  959. } else {
  960. cam->APP_len = 0;
  961. }
  962. if(parms->COM_len != 0) {
  963. if(parms->COM_len > 0 &&
  964. parms->COM_len <= sizeof(cam->COM_data)) {
  965. cam->COM_len = parms->COM_len;
  966. memcpy(cam->COM_data, parms->COM_data, parms->COM_len);
  967. } else {
  968. LOG("Bad COM_len=%d\n", parms->COM_len);
  969. return -EINVAL;
  970. }
  971. }
  972. return 0;
  973. }
  974. /******************************************************************************
  975. *
  976. * ioctl_reqbufs
  977. *
  978. * V4L2 Initiate memory mapping.
  979. * NOTE: The user's request is ignored. For now the buffers are fixed.
  980. *
  981. *****************************************************************************/
  982. static int ioctl_reqbufs(void *arg,struct camera_data *cam)
  983. {
  984. struct v4l2_requestbuffers *req = arg;
  985. if(req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  986. req->memory != V4L2_MEMORY_MMAP)
  987. return -EINVAL;
  988. DBG("REQBUFS requested:%d returning:%d\n", req->count, cam->num_frames);
  989. req->count = cam->num_frames;
  990. memset(&req->reserved, 0, sizeof(req->reserved));
  991. return 0;
  992. }
  993. /******************************************************************************
  994. *
  995. * ioctl_querybuf
  996. *
  997. * V4L2 Query memory buffer status.
  998. *
  999. *****************************************************************************/
  1000. static int ioctl_querybuf(void *arg,struct camera_data *cam)
  1001. {
  1002. struct v4l2_buffer *buf = arg;
  1003. if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  1004. buf->index > cam->num_frames)
  1005. return -EINVAL;
  1006. buf->m.offset = cam->buffers[buf->index].data - cam->frame_buffer;
  1007. buf->length = cam->frame_size;
  1008. buf->memory = V4L2_MEMORY_MMAP;
  1009. if(cam->mmapped)
  1010. buf->flags = V4L2_BUF_FLAG_MAPPED;
  1011. else
  1012. buf->flags = 0;
  1013. switch (cam->buffers[buf->index].status) {
  1014. case FRAME_EMPTY:
  1015. case FRAME_ERROR:
  1016. case FRAME_READING:
  1017. buf->bytesused = 0;
  1018. buf->flags = V4L2_BUF_FLAG_QUEUED;
  1019. break;
  1020. case FRAME_READY:
  1021. buf->bytesused = cam->buffers[buf->index].length;
  1022. buf->timestamp = cam->buffers[buf->index].timestamp;
  1023. buf->sequence = cam->buffers[buf->index].seq;
  1024. buf->flags = V4L2_BUF_FLAG_DONE;
  1025. break;
  1026. }
  1027. DBG("QUERYBUF index:%d offset:%d flags:%d seq:%d bytesused:%d\n",
  1028. buf->index, buf->m.offset, buf->flags, buf->sequence,
  1029. buf->bytesused);
  1030. return 0;
  1031. }
  1032. /******************************************************************************
  1033. *
  1034. * ioctl_qbuf
  1035. *
  1036. * V4L2 User is freeing buffer
  1037. *
  1038. *****************************************************************************/
  1039. static int ioctl_qbuf(void *arg,struct camera_data *cam)
  1040. {
  1041. struct v4l2_buffer *buf = arg;
  1042. if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  1043. buf->memory != V4L2_MEMORY_MMAP ||
  1044. buf->index > cam->num_frames)
  1045. return -EINVAL;
  1046. DBG("QBUF #%d\n", buf->index);
  1047. if(cam->buffers[buf->index].status == FRAME_READY)
  1048. cam->buffers[buf->index].status = FRAME_EMPTY;
  1049. return 0;
  1050. }
  1051. /******************************************************************************
  1052. *
  1053. * find_earliest_filled_buffer
  1054. *
  1055. * Helper for ioctl_dqbuf. Find the next ready buffer.
  1056. *
  1057. *****************************************************************************/
  1058. static int find_earliest_filled_buffer(struct camera_data *cam)
  1059. {
  1060. int i;
  1061. int found = -1;
  1062. for (i=0; i<cam->num_frames; i++) {
  1063. if(cam->buffers[i].status == FRAME_READY) {
  1064. if(found < 0) {
  1065. found = i;
  1066. } else {
  1067. /* find which buffer is earlier */
  1068. struct timeval *tv1, *tv2;
  1069. tv1 = &cam->buffers[i].timestamp;
  1070. tv2 = &cam->buffers[found].timestamp;
  1071. if(tv1->tv_sec < tv2->tv_sec ||
  1072. (tv1->tv_sec == tv2->tv_sec &&
  1073. tv1->tv_usec < tv2->tv_usec))
  1074. found = i;
  1075. }
  1076. }
  1077. }
  1078. return found;
  1079. }
  1080. /******************************************************************************
  1081. *
  1082. * ioctl_dqbuf
  1083. *
  1084. * V4L2 User is asking for a filled buffer.
  1085. *
  1086. *****************************************************************************/
  1087. static int ioctl_dqbuf(void *arg,struct camera_data *cam, struct file *file)
  1088. {
  1089. struct v4l2_buffer *buf = arg;
  1090. int frame;
  1091. if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  1092. buf->memory != V4L2_MEMORY_MMAP)
  1093. return -EINVAL;
  1094. frame = find_earliest_filled_buffer(cam);
  1095. if(frame < 0 && file->f_flags&O_NONBLOCK)
  1096. return -EAGAIN;
  1097. if(frame < 0) {
  1098. /* Wait for a frame to become available */
  1099. struct framebuf *cb=cam->curbuff;
  1100. mutex_unlock(&cam->v4l2_lock);
  1101. wait_event_interruptible(cam->wq_stream,
  1102. !cam->present ||
  1103. (cb=cam->curbuff)->status == FRAME_READY);
  1104. mutex_lock(&cam->v4l2_lock);
  1105. if (signal_pending(current))
  1106. return -ERESTARTSYS;
  1107. if(!cam->present)
  1108. return -ENOTTY;
  1109. frame = cb->num;
  1110. }
  1111. buf->index = frame;
  1112. buf->bytesused = cam->buffers[buf->index].length;
  1113. buf->flags = V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_DONE;
  1114. buf->field = V4L2_FIELD_NONE;
  1115. buf->timestamp = cam->buffers[buf->index].timestamp;
  1116. buf->sequence = cam->buffers[buf->index].seq;
  1117. buf->m.offset = cam->buffers[buf->index].data - cam->frame_buffer;
  1118. buf->length = cam->frame_size;
  1119. buf->input = 0;
  1120. buf->reserved = 0;
  1121. memset(&buf->timecode, 0, sizeof(buf->timecode));
  1122. DBG("DQBUF #%d status:%d seq:%d length:%d\n", buf->index,
  1123. cam->buffers[buf->index].status, buf->sequence, buf->bytesused);
  1124. return 0;
  1125. }
  1126. /******************************************************************************
  1127. *
  1128. * cpia2_ioctl
  1129. *
  1130. *****************************************************************************/
  1131. static long cpia2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
  1132. {
  1133. struct camera_data *cam = video_drvdata(file);
  1134. long retval = 0;
  1135. if (!cam)
  1136. return -ENOTTY;
  1137. if (!cam->present)
  1138. return -ENODEV;
  1139. /* Priority check */
  1140. switch (cmd) {
  1141. case VIDIOC_S_FMT:
  1142. {
  1143. struct cpia2_fh *fh = file->private_data;
  1144. retval = v4l2_prio_check(&cam->prio, fh->prio);
  1145. if (retval)
  1146. return retval;
  1147. break;
  1148. }
  1149. default:
  1150. break;
  1151. }
  1152. switch (cmd) {
  1153. /* CPIA2 extension to Video4Linux API */
  1154. case CPIA2_IOC_SET_GPIO:
  1155. retval = ioctl_set_gpio(arg, cam);
  1156. break;
  1157. case VIDIOC_QUERYCAP:
  1158. retval = ioctl_querycap(arg,cam);
  1159. break;
  1160. case VIDIOC_ENUMINPUT:
  1161. case VIDIOC_G_INPUT:
  1162. case VIDIOC_S_INPUT:
  1163. retval = ioctl_input(cmd, arg, cam);
  1164. break;
  1165. case VIDIOC_ENUM_FMT:
  1166. retval = ioctl_enum_fmt(arg,cam);
  1167. break;
  1168. case VIDIOC_TRY_FMT:
  1169. retval = ioctl_try_fmt(arg,cam);
  1170. break;
  1171. case VIDIOC_G_FMT:
  1172. retval = ioctl_get_fmt(arg,cam);
  1173. break;
  1174. case VIDIOC_S_FMT:
  1175. retval = ioctl_set_fmt(arg,cam,file->private_data);
  1176. break;
  1177. case VIDIOC_CROPCAP:
  1178. retval = ioctl_cropcap(arg,cam);
  1179. break;
  1180. case VIDIOC_G_CROP:
  1181. case VIDIOC_S_CROP:
  1182. // TODO: I think cropping can be implemented - SJB
  1183. retval = -EINVAL;
  1184. break;
  1185. case VIDIOC_QUERYCTRL:
  1186. retval = ioctl_queryctrl(arg,cam);
  1187. break;
  1188. case VIDIOC_QUERYMENU:
  1189. retval = ioctl_querymenu(arg,cam);
  1190. break;
  1191. case VIDIOC_G_CTRL:
  1192. retval = ioctl_g_ctrl(arg,cam);
  1193. break;
  1194. case VIDIOC_S_CTRL:
  1195. retval = ioctl_s_ctrl(arg,cam);
  1196. break;
  1197. case VIDIOC_G_JPEGCOMP:
  1198. retval = ioctl_g_jpegcomp(arg,cam);
  1199. break;
  1200. case VIDIOC_S_JPEGCOMP:
  1201. retval = ioctl_s_jpegcomp(arg,cam);
  1202. break;
  1203. case VIDIOC_G_PRIORITY:
  1204. {
  1205. struct cpia2_fh *fh = file->private_data;
  1206. *(enum v4l2_priority*)arg = fh->prio;
  1207. break;
  1208. }
  1209. case VIDIOC_S_PRIORITY:
  1210. {
  1211. struct cpia2_fh *fh = file->private_data;
  1212. enum v4l2_priority prio;
  1213. prio = *(enum v4l2_priority*)arg;
  1214. if(cam->streaming &&
  1215. prio != fh->prio &&
  1216. fh->prio == V4L2_PRIORITY_RECORD) {
  1217. /* Can't drop record priority while streaming */
  1218. retval = -EBUSY;
  1219. } else if(prio == V4L2_PRIORITY_RECORD &&
  1220. prio != fh->prio &&
  1221. v4l2_prio_max(&cam->prio) == V4L2_PRIORITY_RECORD) {
  1222. /* Only one program can record at a time */
  1223. retval = -EBUSY;
  1224. } else {
  1225. retval = v4l2_prio_change(&cam->prio, &fh->prio, prio);
  1226. }
  1227. break;
  1228. }
  1229. case VIDIOC_REQBUFS:
  1230. retval = ioctl_reqbufs(arg,cam);
  1231. break;
  1232. case VIDIOC_QUERYBUF:
  1233. retval = ioctl_querybuf(arg,cam);
  1234. break;
  1235. case VIDIOC_QBUF:
  1236. retval = ioctl_qbuf(arg,cam);
  1237. break;
  1238. case VIDIOC_DQBUF:
  1239. retval = ioctl_dqbuf(arg,cam,file);
  1240. break;
  1241. case VIDIOC_STREAMON:
  1242. {
  1243. int type;
  1244. DBG("VIDIOC_STREAMON, streaming=%d\n", cam->streaming);
  1245. type = *(int*)arg;
  1246. if(!cam->mmapped || type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1247. retval = -EINVAL;
  1248. if(!cam->streaming) {
  1249. retval = cpia2_usb_stream_start(cam,
  1250. cam->params.camera_state.stream_mode);
  1251. } else {
  1252. retval = -EINVAL;
  1253. }
  1254. break;
  1255. }
  1256. case VIDIOC_STREAMOFF:
  1257. {
  1258. int type;
  1259. DBG("VIDIOC_STREAMOFF, streaming=%d\n", cam->streaming);
  1260. type = *(int*)arg;
  1261. if(!cam->mmapped || type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1262. retval = -EINVAL;
  1263. if(cam->streaming) {
  1264. retval = cpia2_usb_stream_stop(cam);
  1265. } else {
  1266. retval = -EINVAL;
  1267. }
  1268. break;
  1269. }
  1270. case VIDIOC_ENUMOUTPUT:
  1271. case VIDIOC_G_OUTPUT:
  1272. case VIDIOC_S_OUTPUT:
  1273. case VIDIOC_G_MODULATOR:
  1274. case VIDIOC_S_MODULATOR:
  1275. case VIDIOC_ENUMAUDIO:
  1276. case VIDIOC_G_AUDIO:
  1277. case VIDIOC_S_AUDIO:
  1278. case VIDIOC_ENUMAUDOUT:
  1279. case VIDIOC_G_AUDOUT:
  1280. case VIDIOC_S_AUDOUT:
  1281. case VIDIOC_ENUMSTD:
  1282. case VIDIOC_QUERYSTD:
  1283. case VIDIOC_G_STD:
  1284. case VIDIOC_S_STD:
  1285. case VIDIOC_G_TUNER:
  1286. case VIDIOC_S_TUNER:
  1287. case VIDIOC_G_FREQUENCY:
  1288. case VIDIOC_S_FREQUENCY:
  1289. case VIDIOC_OVERLAY:
  1290. case VIDIOC_G_FBUF:
  1291. case VIDIOC_S_FBUF:
  1292. case VIDIOC_G_PARM:
  1293. case VIDIOC_S_PARM:
  1294. retval = -EINVAL;
  1295. break;
  1296. default:
  1297. retval = -ENOIOCTLCMD;
  1298. break;
  1299. }
  1300. return retval;
  1301. }
  1302. static long cpia2_ioctl(struct file *file,
  1303. unsigned int cmd, unsigned long arg)
  1304. {
  1305. return video_usercopy(file, cmd, arg, cpia2_do_ioctl);
  1306. }
  1307. /******************************************************************************
  1308. *
  1309. * cpia2_mmap
  1310. *
  1311. *****************************************************************************/
  1312. static int cpia2_mmap(struct file *file, struct vm_area_struct *area)
  1313. {
  1314. struct camera_data *cam = video_drvdata(file);
  1315. int retval;
  1316. /* Priority check */
  1317. struct cpia2_fh *fh = file->private_data;
  1318. if(fh->prio != V4L2_PRIORITY_RECORD) {
  1319. return -EBUSY;
  1320. }
  1321. retval = cpia2_remap_buffer(cam, area);
  1322. if(!retval)
  1323. fh->mmapped = 1;
  1324. return retval;
  1325. }
  1326. /******************************************************************************
  1327. *
  1328. * reset_camera_struct_v4l
  1329. *
  1330. * Sets all values to the defaults
  1331. *****************************************************************************/
  1332. static void reset_camera_struct_v4l(struct camera_data *cam)
  1333. {
  1334. cam->width = cam->params.roi.width;
  1335. cam->height = cam->params.roi.height;
  1336. cam->frame_size = buffer_size;
  1337. cam->num_frames = num_buffers;
  1338. /* FlickerModes */
  1339. cam->params.flicker_control.flicker_mode_req = flicker_mode;
  1340. cam->params.flicker_control.mains_frequency = flicker_freq;
  1341. /* streamMode */
  1342. cam->params.camera_state.stream_mode = alternate;
  1343. cam->pixelformat = V4L2_PIX_FMT_JPEG;
  1344. v4l2_prio_init(&cam->prio);
  1345. }
  1346. /***
  1347. * The v4l video device structure initialized for this device
  1348. ***/
  1349. static const struct v4l2_file_operations cpia2_fops = {
  1350. .owner = THIS_MODULE,
  1351. .open = cpia2_open,
  1352. .release = cpia2_close,
  1353. .read = cpia2_v4l_read,
  1354. .poll = cpia2_v4l_poll,
  1355. .unlocked_ioctl = cpia2_ioctl,
  1356. .mmap = cpia2_mmap,
  1357. };
  1358. static struct video_device cpia2_template = {
  1359. /* I could not find any place for the old .initialize initializer?? */
  1360. .name = "CPiA2 Camera",
  1361. .fops = &cpia2_fops,
  1362. .release = video_device_release,
  1363. };
  1364. /******************************************************************************
  1365. *
  1366. * cpia2_register_camera
  1367. *
  1368. *****************************************************************************/
  1369. int cpia2_register_camera(struct camera_data *cam)
  1370. {
  1371. cam->vdev = video_device_alloc();
  1372. if(!cam->vdev)
  1373. return -ENOMEM;
  1374. memcpy(cam->vdev, &cpia2_template, sizeof(cpia2_template));
  1375. video_set_drvdata(cam->vdev, cam);
  1376. cam->vdev->lock = &cam->v4l2_lock;
  1377. reset_camera_struct_v4l(cam);
  1378. /* register v4l device */
  1379. if (video_register_device(cam->vdev, VFL_TYPE_GRABBER, video_nr) < 0) {
  1380. ERR("video_register_device failed\n");
  1381. video_device_release(cam->vdev);
  1382. return -ENODEV;
  1383. }
  1384. return 0;
  1385. }
  1386. /******************************************************************************
  1387. *
  1388. * cpia2_unregister_camera
  1389. *
  1390. *****************************************************************************/
  1391. void cpia2_unregister_camera(struct camera_data *cam)
  1392. {
  1393. if (!cam->open_count) {
  1394. video_unregister_device(cam->vdev);
  1395. } else {
  1396. LOG("%s removed while open, deferring "
  1397. "video_unregister_device\n",
  1398. video_device_node_name(cam->vdev));
  1399. }
  1400. }
  1401. /******************************************************************************
  1402. *
  1403. * check_parameters
  1404. *
  1405. * Make sure that all user-supplied parameters are sensible
  1406. *****************************************************************************/
  1407. static void __init check_parameters(void)
  1408. {
  1409. if(buffer_size < PAGE_SIZE) {
  1410. buffer_size = PAGE_SIZE;
  1411. LOG("buffer_size too small, setting to %d\n", buffer_size);
  1412. } else if(buffer_size > 1024*1024) {
  1413. /* arbitrary upper limiit */
  1414. buffer_size = 1024*1024;
  1415. LOG("buffer_size ridiculously large, setting to %d\n",
  1416. buffer_size);
  1417. } else {
  1418. buffer_size += PAGE_SIZE-1;
  1419. buffer_size &= ~(PAGE_SIZE-1);
  1420. }
  1421. if(num_buffers < 1) {
  1422. num_buffers = 1;
  1423. LOG("num_buffers too small, setting to %d\n", num_buffers);
  1424. } else if(num_buffers > VIDEO_MAX_FRAME) {
  1425. num_buffers = VIDEO_MAX_FRAME;
  1426. LOG("num_buffers too large, setting to %d\n", num_buffers);
  1427. }
  1428. if(alternate < USBIF_ISO_1 || alternate > USBIF_ISO_6) {
  1429. alternate = DEFAULT_ALT;
  1430. LOG("alternate specified is invalid, using %d\n", alternate);
  1431. }
  1432. if (flicker_mode != NEVER_FLICKER && flicker_mode != ANTI_FLICKER_ON) {
  1433. flicker_mode = NEVER_FLICKER;
  1434. LOG("Flicker mode specified is invalid, using %d\n",
  1435. flicker_mode);
  1436. }
  1437. if (flicker_freq != FLICKER_50 && flicker_freq != FLICKER_60) {
  1438. flicker_freq = FLICKER_60;
  1439. LOG("Flicker mode specified is invalid, using %d\n",
  1440. flicker_freq);
  1441. }
  1442. if(video_nr < -1 || video_nr > 64) {
  1443. video_nr = -1;
  1444. LOG("invalid video_nr specified, must be -1 to 64\n");
  1445. }
  1446. DBG("Using %d buffers, each %d bytes, alternate=%d\n",
  1447. num_buffers, buffer_size, alternate);
  1448. }
  1449. /************ Module Stuff ***************/
  1450. /******************************************************************************
  1451. *
  1452. * cpia2_init/module_init
  1453. *
  1454. *****************************************************************************/
  1455. static int __init cpia2_init(void)
  1456. {
  1457. LOG("%s v%d.%d.%d\n",
  1458. ABOUT, CPIA2_MAJ_VER, CPIA2_MIN_VER, CPIA2_PATCH_VER);
  1459. check_parameters();
  1460. cpia2_usb_init();
  1461. return 0;
  1462. }
  1463. /******************************************************************************
  1464. *
  1465. * cpia2_exit/module_exit
  1466. *
  1467. *****************************************************************************/
  1468. static void __exit cpia2_exit(void)
  1469. {
  1470. cpia2_usb_cleanup();
  1471. schedule_timeout(2 * HZ);
  1472. }
  1473. module_init(cpia2_init);
  1474. module_exit(cpia2_exit);