cpia2_v4l.c 42 KB

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