cpia2_v4l.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062
  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/videodev.h>
  38. #include <media/v4l2-ioctl.h>
  39. #include "cpia2.h"
  40. #include "cpia2dev.h"
  41. //#define _CPIA2_DEBUG_
  42. #define MAKE_STRING_1(x) #x
  43. #define MAKE_STRING(x) MAKE_STRING_1(x)
  44. static int video_nr = -1;
  45. module_param(video_nr, int, 0);
  46. MODULE_PARM_DESC(video_nr,"video device to register (0=/dev/video0, etc)");
  47. static int buffer_size = 68*1024;
  48. module_param(buffer_size, int, 0);
  49. MODULE_PARM_DESC(buffer_size, "Size for each frame buffer in bytes (default 68k)");
  50. static int num_buffers = 3;
  51. module_param(num_buffers, int, 0);
  52. MODULE_PARM_DESC(num_buffers, "Number of frame buffers (1-"
  53. MAKE_STRING(VIDEO_MAX_FRAME) ", default 3)");
  54. static int alternate = DEFAULT_ALT;
  55. module_param(alternate, int, 0);
  56. MODULE_PARM_DESC(alternate, "USB Alternate (" MAKE_STRING(USBIF_ISO_1) "-"
  57. MAKE_STRING(USBIF_ISO_6) ", default "
  58. MAKE_STRING(DEFAULT_ALT) ")");
  59. static int flicker_freq = 60;
  60. module_param(flicker_freq, int, 0);
  61. MODULE_PARM_DESC(flicker_freq, "Flicker frequency (" MAKE_STRING(50) "or"
  62. MAKE_STRING(60) ", default "
  63. MAKE_STRING(60) ")");
  64. static int flicker_mode = NEVER_FLICKER;
  65. module_param(flicker_mode, int, 0);
  66. MODULE_PARM_DESC(flicker_mode,
  67. "Flicker supression (" MAKE_STRING(NEVER_FLICKER) "or"
  68. MAKE_STRING(ANTI_FLICKER_ON) ", default "
  69. MAKE_STRING(NEVER_FLICKER) ")");
  70. MODULE_AUTHOR("Steve Miller (STMicroelectronics) <steve.miller@st.com>");
  71. MODULE_DESCRIPTION("V4L-driver for STMicroelectronics CPiA2 based cameras");
  72. MODULE_SUPPORTED_DEVICE("video");
  73. MODULE_LICENSE("GPL");
  74. #define ABOUT "V4L-Driver for Vision CPiA2 based cameras"
  75. struct control_menu_info {
  76. int value;
  77. char name[32];
  78. };
  79. static struct control_menu_info framerate_controls[] =
  80. {
  81. { CPIA2_VP_FRAMERATE_6_25, "6.25 fps" },
  82. { CPIA2_VP_FRAMERATE_7_5, "7.5 fps" },
  83. { CPIA2_VP_FRAMERATE_12_5, "12.5 fps" },
  84. { CPIA2_VP_FRAMERATE_15, "15 fps" },
  85. { CPIA2_VP_FRAMERATE_25, "25 fps" },
  86. { CPIA2_VP_FRAMERATE_30, "30 fps" },
  87. };
  88. #define NUM_FRAMERATE_CONTROLS (ARRAY_SIZE(framerate_controls))
  89. static struct control_menu_info flicker_controls[] =
  90. {
  91. { NEVER_FLICKER, "Off" },
  92. { FLICKER_50, "50 Hz" },
  93. { FLICKER_60, "60 Hz" },
  94. };
  95. #define NUM_FLICKER_CONTROLS (ARRAY_SIZE(flicker_controls))
  96. static struct control_menu_info lights_controls[] =
  97. {
  98. { 0, "Off" },
  99. { 64, "Top" },
  100. { 128, "Bottom" },
  101. { 192, "Both" },
  102. };
  103. #define NUM_LIGHTS_CONTROLS (ARRAY_SIZE(lights_controls))
  104. #define GPIO_LIGHTS_MASK 192
  105. static struct v4l2_queryctrl controls[] = {
  106. {
  107. .id = V4L2_CID_BRIGHTNESS,
  108. .type = V4L2_CTRL_TYPE_INTEGER,
  109. .name = "Brightness",
  110. .minimum = 0,
  111. .maximum = 255,
  112. .step = 1,
  113. .default_value = DEFAULT_BRIGHTNESS,
  114. },
  115. {
  116. .id = V4L2_CID_CONTRAST,
  117. .type = V4L2_CTRL_TYPE_INTEGER,
  118. .name = "Contrast",
  119. .minimum = 0,
  120. .maximum = 255,
  121. .step = 1,
  122. .default_value = DEFAULT_CONTRAST,
  123. },
  124. {
  125. .id = V4L2_CID_SATURATION,
  126. .type = V4L2_CTRL_TYPE_INTEGER,
  127. .name = "Saturation",
  128. .minimum = 0,
  129. .maximum = 255,
  130. .step = 1,
  131. .default_value = DEFAULT_SATURATION,
  132. },
  133. {
  134. .id = V4L2_CID_HFLIP,
  135. .type = V4L2_CTRL_TYPE_BOOLEAN,
  136. .name = "Mirror Horizontally",
  137. .minimum = 0,
  138. .maximum = 1,
  139. .step = 1,
  140. .default_value = 0,
  141. },
  142. {
  143. .id = V4L2_CID_VFLIP,
  144. .type = V4L2_CTRL_TYPE_BOOLEAN,
  145. .name = "Flip Vertically",
  146. .minimum = 0,
  147. .maximum = 1,
  148. .step = 1,
  149. .default_value = 0,
  150. },
  151. {
  152. .id = CPIA2_CID_TARGET_KB,
  153. .type = V4L2_CTRL_TYPE_INTEGER,
  154. .name = "Target KB",
  155. .minimum = 0,
  156. .maximum = 255,
  157. .step = 1,
  158. .default_value = DEFAULT_TARGET_KB,
  159. },
  160. {
  161. .id = CPIA2_CID_GPIO,
  162. .type = V4L2_CTRL_TYPE_INTEGER,
  163. .name = "GPIO",
  164. .minimum = 0,
  165. .maximum = 255,
  166. .step = 1,
  167. .default_value = 0,
  168. },
  169. {
  170. .id = CPIA2_CID_FLICKER_MODE,
  171. .type = V4L2_CTRL_TYPE_MENU,
  172. .name = "Flicker Reduction",
  173. .minimum = 0,
  174. .maximum = NUM_FLICKER_CONTROLS-1,
  175. .step = 1,
  176. .default_value = 0,
  177. },
  178. {
  179. .id = CPIA2_CID_FRAMERATE,
  180. .type = V4L2_CTRL_TYPE_MENU,
  181. .name = "Framerate",
  182. .minimum = 0,
  183. .maximum = NUM_FRAMERATE_CONTROLS-1,
  184. .step = 1,
  185. .default_value = NUM_FRAMERATE_CONTROLS-1,
  186. },
  187. {
  188. .id = CPIA2_CID_USB_ALT,
  189. .type = V4L2_CTRL_TYPE_INTEGER,
  190. .name = "USB Alternate",
  191. .minimum = USBIF_ISO_1,
  192. .maximum = USBIF_ISO_6,
  193. .step = 1,
  194. .default_value = DEFAULT_ALT,
  195. },
  196. {
  197. .id = CPIA2_CID_LIGHTS,
  198. .type = V4L2_CTRL_TYPE_MENU,
  199. .name = "Lights",
  200. .minimum = 0,
  201. .maximum = NUM_LIGHTS_CONTROLS-1,
  202. .step = 1,
  203. .default_value = 0,
  204. },
  205. {
  206. .id = CPIA2_CID_RESET_CAMERA,
  207. .type = V4L2_CTRL_TYPE_BUTTON,
  208. .name = "Reset Camera",
  209. .minimum = 0,
  210. .maximum = 0,
  211. .step = 0,
  212. .default_value = 0,
  213. },
  214. };
  215. #define NUM_CONTROLS (ARRAY_SIZE(controls))
  216. /******************************************************************************
  217. *
  218. * cpia2_open
  219. *
  220. *****************************************************************************/
  221. static int cpia2_open(struct file *file)
  222. {
  223. struct camera_data *cam = video_drvdata(file);
  224. int retval = 0;
  225. if (!cam) {
  226. ERR("Internal error, camera_data not found!\n");
  227. return -ENODEV;
  228. }
  229. if(mutex_lock_interruptible(&cam->busy_lock))
  230. return -ERESTARTSYS;
  231. if(!cam->present) {
  232. retval = -ENODEV;
  233. goto err_return;
  234. }
  235. if (cam->open_count > 0) {
  236. goto skip_init;
  237. }
  238. if (cpia2_allocate_buffers(cam)) {
  239. retval = -ENOMEM;
  240. goto err_return;
  241. }
  242. /* reset the camera */
  243. if (cpia2_reset_camera(cam) < 0) {
  244. retval = -EIO;
  245. goto err_return;
  246. }
  247. cam->APP_len = 0;
  248. cam->COM_len = 0;
  249. skip_init:
  250. {
  251. struct cpia2_fh *fh = kmalloc(sizeof(*fh),GFP_KERNEL);
  252. if(!fh) {
  253. retval = -ENOMEM;
  254. goto err_return;
  255. }
  256. file->private_data = fh;
  257. fh->prio = V4L2_PRIORITY_UNSET;
  258. v4l2_prio_open(&cam->prio, &fh->prio);
  259. fh->mmapped = 0;
  260. }
  261. ++cam->open_count;
  262. cpia2_dbg_dump_registers(cam);
  263. err_return:
  264. mutex_unlock(&cam->busy_lock);
  265. return retval;
  266. }
  267. /******************************************************************************
  268. *
  269. * cpia2_close
  270. *
  271. *****************************************************************************/
  272. static int cpia2_close(struct file *file)
  273. {
  274. struct video_device *dev = video_devdata(file);
  275. struct camera_data *cam = video_get_drvdata(dev);
  276. struct cpia2_fh *fh = file->private_data;
  277. mutex_lock(&cam->busy_lock);
  278. if (cam->present &&
  279. (cam->open_count == 1
  280. || fh->prio == V4L2_PRIORITY_RECORD
  281. )) {
  282. cpia2_usb_stream_stop(cam);
  283. if(cam->open_count == 1) {
  284. /* save camera state for later open */
  285. cpia2_save_camera_state(cam);
  286. cpia2_set_low_power(cam);
  287. cpia2_free_buffers(cam);
  288. }
  289. }
  290. {
  291. if(fh->mmapped)
  292. cam->mmapped = 0;
  293. v4l2_prio_close(&cam->prio,&fh->prio);
  294. file->private_data = NULL;
  295. kfree(fh);
  296. }
  297. if (--cam->open_count == 0) {
  298. cpia2_free_buffers(cam);
  299. if (!cam->present) {
  300. video_unregister_device(dev);
  301. mutex_unlock(&cam->busy_lock);
  302. kfree(cam);
  303. return 0;
  304. }
  305. }
  306. mutex_unlock(&cam->busy_lock);
  307. return 0;
  308. }
  309. /******************************************************************************
  310. *
  311. * cpia2_v4l_read
  312. *
  313. *****************************************************************************/
  314. static ssize_t cpia2_v4l_read(struct file *file, char __user *buf, size_t count,
  315. loff_t *off)
  316. {
  317. struct camera_data *cam = video_drvdata(file);
  318. int noblock = file->f_flags&O_NONBLOCK;
  319. struct cpia2_fh *fh = file->private_data;
  320. if(!cam)
  321. return -EINVAL;
  322. /* Priority check */
  323. if(fh->prio != V4L2_PRIORITY_RECORD) {
  324. return -EBUSY;
  325. }
  326. return cpia2_read(cam, buf, count, noblock);
  327. }
  328. /******************************************************************************
  329. *
  330. * cpia2_v4l_poll
  331. *
  332. *****************************************************************************/
  333. static unsigned int cpia2_v4l_poll(struct file *filp, struct poll_table_struct *wait)
  334. {
  335. struct camera_data *cam = video_drvdata(filp);
  336. struct cpia2_fh *fh = filp->private_data;
  337. if(!cam)
  338. return POLLERR;
  339. /* Priority check */
  340. if(fh->prio != V4L2_PRIORITY_RECORD) {
  341. return POLLERR;
  342. }
  343. return cpia2_poll(cam, filp, wait);
  344. }
  345. /******************************************************************************
  346. *
  347. * ioctl_cap_query
  348. *
  349. *****************************************************************************/
  350. static int ioctl_cap_query(void *arg, struct camera_data *cam)
  351. {
  352. struct video_capability *vc;
  353. int retval = 0;
  354. vc = arg;
  355. if (cam->params.pnp_id.product == 0x151)
  356. strcpy(vc->name, "QX5 Microscope");
  357. else
  358. strcpy(vc->name, "CPiA2 Camera");
  359. vc->type = VID_TYPE_CAPTURE | VID_TYPE_MJPEG_ENCODER;
  360. vc->channels = 1;
  361. vc->audios = 0;
  362. vc->minwidth = 176; /* VIDEOSIZE_QCIF */
  363. vc->minheight = 144;
  364. switch (cam->params.version.sensor_flags) {
  365. case CPIA2_VP_SENSOR_FLAGS_500:
  366. vc->maxwidth = STV_IMAGE_VGA_COLS;
  367. vc->maxheight = STV_IMAGE_VGA_ROWS;
  368. break;
  369. case CPIA2_VP_SENSOR_FLAGS_410:
  370. vc->maxwidth = STV_IMAGE_CIF_COLS;
  371. vc->maxheight = STV_IMAGE_CIF_ROWS;
  372. break;
  373. default:
  374. return -EINVAL;
  375. }
  376. return retval;
  377. }
  378. /******************************************************************************
  379. *
  380. * ioctl_get_channel
  381. *
  382. *****************************************************************************/
  383. static int ioctl_get_channel(void *arg)
  384. {
  385. int retval = 0;
  386. struct video_channel *v;
  387. v = arg;
  388. if (v->channel != 0)
  389. return -EINVAL;
  390. v->channel = 0;
  391. strcpy(v->name, "Camera");
  392. v->tuners = 0;
  393. v->flags = 0;
  394. v->type = VIDEO_TYPE_CAMERA;
  395. v->norm = 0;
  396. return retval;
  397. }
  398. /******************************************************************************
  399. *
  400. * ioctl_set_channel
  401. *
  402. *****************************************************************************/
  403. static int ioctl_set_channel(void *arg)
  404. {
  405. struct video_channel *v;
  406. int retval = 0;
  407. v = arg;
  408. if (retval == 0 && v->channel != 0)
  409. retval = -EINVAL;
  410. return retval;
  411. }
  412. /******************************************************************************
  413. *
  414. * ioctl_set_image_prop
  415. *
  416. *****************************************************************************/
  417. static int ioctl_set_image_prop(void *arg, struct camera_data *cam)
  418. {
  419. struct video_picture *vp;
  420. int retval = 0;
  421. vp = arg;
  422. /* brightness, color, contrast need no check 0-65535 */
  423. memcpy(&cam->vp, vp, sizeof(*vp));
  424. /* update cam->params.colorParams */
  425. cam->params.color_params.brightness = vp->brightness / 256;
  426. cam->params.color_params.saturation = vp->colour / 256;
  427. cam->params.color_params.contrast = vp->contrast / 256;
  428. DBG("Requested params: bright 0x%X, sat 0x%X, contrast 0x%X\n",
  429. cam->params.color_params.brightness,
  430. cam->params.color_params.saturation,
  431. cam->params.color_params.contrast);
  432. cpia2_set_color_params(cam);
  433. return retval;
  434. }
  435. static int sync(struct camera_data *cam, int frame_nr)
  436. {
  437. struct framebuf *frame = &cam->buffers[frame_nr];
  438. while (1) {
  439. if (frame->status == FRAME_READY)
  440. return 0;
  441. if (!cam->streaming) {
  442. frame->status = FRAME_READY;
  443. frame->length = 0;
  444. return 0;
  445. }
  446. mutex_unlock(&cam->busy_lock);
  447. wait_event_interruptible(cam->wq_stream,
  448. !cam->streaming ||
  449. frame->status == FRAME_READY);
  450. mutex_lock(&cam->busy_lock);
  451. if (signal_pending(current))
  452. return -ERESTARTSYS;
  453. if(!cam->present)
  454. return -ENOTTY;
  455. }
  456. }
  457. /******************************************************************************
  458. *
  459. * ioctl_set_window_size
  460. *
  461. *****************************************************************************/
  462. static int ioctl_set_window_size(void *arg, struct camera_data *cam,
  463. struct cpia2_fh *fh)
  464. {
  465. /* copy_from_user, check validity, copy to internal structure */
  466. struct video_window *vw;
  467. int frame, err;
  468. vw = arg;
  469. if (vw->clipcount != 0) /* clipping not supported */
  470. return -EINVAL;
  471. if (vw->clips != NULL) /* clipping not supported */
  472. return -EINVAL;
  473. /* Ensure that only this process can change the format. */
  474. err = v4l2_prio_change(&cam->prio, &fh->prio, V4L2_PRIORITY_RECORD);
  475. if(err != 0)
  476. return err;
  477. cam->pixelformat = V4L2_PIX_FMT_JPEG;
  478. /* Be sure to supply the Huffman tables, this isn't MJPEG */
  479. cam->params.compression.inhibit_htables = 0;
  480. /* we set the video window to something smaller or equal to what
  481. * is requested by the user???
  482. */
  483. DBG("Requested width = %d, height = %d\n", vw->width, vw->height);
  484. if (vw->width != cam->vw.width || vw->height != cam->vw.height) {
  485. cam->vw.width = vw->width;
  486. cam->vw.height = vw->height;
  487. cam->params.roi.width = vw->width;
  488. cam->params.roi.height = vw->height;
  489. cpia2_set_format(cam);
  490. }
  491. for (frame = 0; frame < cam->num_frames; ++frame) {
  492. if (cam->buffers[frame].status == FRAME_READING)
  493. if ((err = sync(cam, frame)) < 0)
  494. return err;
  495. cam->buffers[frame].status = FRAME_EMPTY;
  496. }
  497. return 0;
  498. }
  499. /******************************************************************************
  500. *
  501. * ioctl_get_mbuf
  502. *
  503. *****************************************************************************/
  504. static int ioctl_get_mbuf(void *arg, struct camera_data *cam)
  505. {
  506. struct video_mbuf *vm;
  507. int i;
  508. vm = arg;
  509. memset(vm, 0, sizeof(*vm));
  510. vm->size = cam->frame_size*cam->num_frames;
  511. vm->frames = cam->num_frames;
  512. for (i = 0; i < cam->num_frames; i++)
  513. vm->offsets[i] = cam->frame_size * i;
  514. return 0;
  515. }
  516. /******************************************************************************
  517. *
  518. * ioctl_mcapture
  519. *
  520. *****************************************************************************/
  521. static int ioctl_mcapture(void *arg, struct camera_data *cam,
  522. struct cpia2_fh *fh)
  523. {
  524. struct video_mmap *vm;
  525. int video_size, err;
  526. vm = arg;
  527. if (vm->frame < 0 || vm->frame >= cam->num_frames)
  528. return -EINVAL;
  529. /* set video size */
  530. video_size = cpia2_match_video_size(vm->width, vm->height);
  531. if (cam->video_size < 0) {
  532. return -EINVAL;
  533. }
  534. /* Ensure that only this process can change the format. */
  535. err = v4l2_prio_change(&cam->prio, &fh->prio, V4L2_PRIORITY_RECORD);
  536. if(err != 0)
  537. return err;
  538. if (video_size != cam->video_size) {
  539. cam->video_size = video_size;
  540. cam->params.roi.width = vm->width;
  541. cam->params.roi.height = vm->height;
  542. cpia2_set_format(cam);
  543. }
  544. if (cam->buffers[vm->frame].status == FRAME_READING)
  545. if ((err=sync(cam, vm->frame)) < 0)
  546. return err;
  547. cam->buffers[vm->frame].status = FRAME_EMPTY;
  548. return cpia2_usb_stream_start(cam,cam->params.camera_state.stream_mode);
  549. }
  550. /******************************************************************************
  551. *
  552. * ioctl_sync
  553. *
  554. *****************************************************************************/
  555. static int ioctl_sync(void *arg, struct camera_data *cam)
  556. {
  557. int frame;
  558. frame = *(int*)arg;
  559. if (frame < 0 || frame >= cam->num_frames)
  560. return -EINVAL;
  561. return sync(cam, frame);
  562. }
  563. /******************************************************************************
  564. *
  565. * ioctl_set_gpio
  566. *
  567. *****************************************************************************/
  568. static int ioctl_set_gpio(void *arg, struct camera_data *cam)
  569. {
  570. __u32 gpio_val;
  571. gpio_val = *(__u32*) arg;
  572. if (gpio_val &~ 0xFFU)
  573. return -EINVAL;
  574. return cpia2_set_gpio(cam, (unsigned char)gpio_val);
  575. }
  576. /******************************************************************************
  577. *
  578. * ioctl_querycap
  579. *
  580. * V4L2 device capabilities
  581. *
  582. *****************************************************************************/
  583. static int ioctl_querycap(void *arg, struct camera_data *cam)
  584. {
  585. struct v4l2_capability *vc = arg;
  586. memset(vc, 0, sizeof(*vc));
  587. strcpy(vc->driver, "cpia2");
  588. if (cam->params.pnp_id.product == 0x151)
  589. strcpy(vc->card, "QX5 Microscope");
  590. else
  591. strcpy(vc->card, "CPiA2 Camera");
  592. switch (cam->params.pnp_id.device_type) {
  593. case DEVICE_STV_672:
  594. strcat(vc->card, " (672/");
  595. break;
  596. case DEVICE_STV_676:
  597. strcat(vc->card, " (676/");
  598. break;
  599. default:
  600. strcat(vc->card, " (???/");
  601. break;
  602. }
  603. switch (cam->params.version.sensor_flags) {
  604. case CPIA2_VP_SENSOR_FLAGS_404:
  605. strcat(vc->card, "404)");
  606. break;
  607. case CPIA2_VP_SENSOR_FLAGS_407:
  608. strcat(vc->card, "407)");
  609. break;
  610. case CPIA2_VP_SENSOR_FLAGS_409:
  611. strcat(vc->card, "409)");
  612. break;
  613. case CPIA2_VP_SENSOR_FLAGS_410:
  614. strcat(vc->card, "410)");
  615. break;
  616. case CPIA2_VP_SENSOR_FLAGS_500:
  617. strcat(vc->card, "500)");
  618. break;
  619. default:
  620. strcat(vc->card, "???)");
  621. break;
  622. }
  623. if (usb_make_path(cam->dev, vc->bus_info, sizeof(vc->bus_info)) <0)
  624. memset(vc->bus_info,0, sizeof(vc->bus_info));
  625. vc->version = KERNEL_VERSION(CPIA2_MAJ_VER, CPIA2_MIN_VER,
  626. CPIA2_PATCH_VER);
  627. vc->capabilities = V4L2_CAP_VIDEO_CAPTURE |
  628. V4L2_CAP_READWRITE |
  629. V4L2_CAP_STREAMING;
  630. return 0;
  631. }
  632. /******************************************************************************
  633. *
  634. * ioctl_input
  635. *
  636. * V4L2 input get/set/enumerate
  637. *
  638. *****************************************************************************/
  639. static int ioctl_input(unsigned int ioclt_nr,void *arg,struct camera_data *cam)
  640. {
  641. struct v4l2_input *i = arg;
  642. if(ioclt_nr != VIDIOC_G_INPUT) {
  643. if (i->index != 0)
  644. return -EINVAL;
  645. }
  646. memset(i, 0, sizeof(*i));
  647. strcpy(i->name, "Camera");
  648. i->type = V4L2_INPUT_TYPE_CAMERA;
  649. return 0;
  650. }
  651. /******************************************************************************
  652. *
  653. * ioctl_enum_fmt
  654. *
  655. * V4L2 format enumerate
  656. *
  657. *****************************************************************************/
  658. static int ioctl_enum_fmt(void *arg,struct camera_data *cam)
  659. {
  660. struct v4l2_fmtdesc *f = arg;
  661. int index = f->index;
  662. if (index < 0 || index > 1)
  663. return -EINVAL;
  664. memset(f, 0, sizeof(*f));
  665. f->index = index;
  666. f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  667. f->flags = V4L2_FMT_FLAG_COMPRESSED;
  668. switch(index) {
  669. case 0:
  670. strcpy(f->description, "MJPEG");
  671. f->pixelformat = V4L2_PIX_FMT_MJPEG;
  672. break;
  673. case 1:
  674. strcpy(f->description, "JPEG");
  675. f->pixelformat = V4L2_PIX_FMT_JPEG;
  676. break;
  677. default:
  678. return -EINVAL;
  679. }
  680. return 0;
  681. }
  682. /******************************************************************************
  683. *
  684. * ioctl_try_fmt
  685. *
  686. * V4L2 format try
  687. *
  688. *****************************************************************************/
  689. static int ioctl_try_fmt(void *arg,struct camera_data *cam)
  690. {
  691. struct v4l2_format *f = arg;
  692. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  693. return -EINVAL;
  694. if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG &&
  695. f->fmt.pix.pixelformat != V4L2_PIX_FMT_JPEG)
  696. return -EINVAL;
  697. f->fmt.pix.field = V4L2_FIELD_NONE;
  698. f->fmt.pix.bytesperline = 0;
  699. f->fmt.pix.sizeimage = cam->frame_size;
  700. f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
  701. f->fmt.pix.priv = 0;
  702. switch (cpia2_match_video_size(f->fmt.pix.width, f->fmt.pix.height)) {
  703. case VIDEOSIZE_VGA:
  704. f->fmt.pix.width = 640;
  705. f->fmt.pix.height = 480;
  706. break;
  707. case VIDEOSIZE_CIF:
  708. f->fmt.pix.width = 352;
  709. f->fmt.pix.height = 288;
  710. break;
  711. case VIDEOSIZE_QVGA:
  712. f->fmt.pix.width = 320;
  713. f->fmt.pix.height = 240;
  714. break;
  715. case VIDEOSIZE_288_216:
  716. f->fmt.pix.width = 288;
  717. f->fmt.pix.height = 216;
  718. break;
  719. case VIDEOSIZE_256_192:
  720. f->fmt.pix.width = 256;
  721. f->fmt.pix.height = 192;
  722. break;
  723. case VIDEOSIZE_224_168:
  724. f->fmt.pix.width = 224;
  725. f->fmt.pix.height = 168;
  726. break;
  727. case VIDEOSIZE_192_144:
  728. f->fmt.pix.width = 192;
  729. f->fmt.pix.height = 144;
  730. break;
  731. case VIDEOSIZE_QCIF:
  732. default:
  733. f->fmt.pix.width = 176;
  734. f->fmt.pix.height = 144;
  735. break;
  736. }
  737. return 0;
  738. }
  739. /******************************************************************************
  740. *
  741. * ioctl_set_fmt
  742. *
  743. * V4L2 format set
  744. *
  745. *****************************************************************************/
  746. static int ioctl_set_fmt(void *arg,struct camera_data *cam, struct cpia2_fh *fh)
  747. {
  748. struct v4l2_format *f = arg;
  749. int err, frame;
  750. err = ioctl_try_fmt(arg, cam);
  751. if(err != 0)
  752. return err;
  753. /* Ensure that only this process can change the format. */
  754. err = v4l2_prio_change(&cam->prio, &fh->prio, V4L2_PRIORITY_RECORD);
  755. if(err != 0) {
  756. return err;
  757. }
  758. cam->pixelformat = f->fmt.pix.pixelformat;
  759. /* NOTE: This should be set to 1 for MJPEG, but some apps don't handle
  760. * the missing Huffman table properly. */
  761. cam->params.compression.inhibit_htables = 0;
  762. /*f->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG;*/
  763. /* we set the video window to something smaller or equal to what
  764. * is requested by the user???
  765. */
  766. DBG("Requested width = %d, height = %d\n",
  767. f->fmt.pix.width, f->fmt.pix.height);
  768. if (f->fmt.pix.width != cam->vw.width ||
  769. f->fmt.pix.height != cam->vw.height) {
  770. cam->vw.width = f->fmt.pix.width;
  771. cam->vw.height = f->fmt.pix.height;
  772. cam->params.roi.width = f->fmt.pix.width;
  773. cam->params.roi.height = f->fmt.pix.height;
  774. cpia2_set_format(cam);
  775. }
  776. for (frame = 0; frame < cam->num_frames; ++frame) {
  777. if (cam->buffers[frame].status == FRAME_READING)
  778. if ((err = sync(cam, frame)) < 0)
  779. return err;
  780. cam->buffers[frame].status = FRAME_EMPTY;
  781. }
  782. return 0;
  783. }
  784. /******************************************************************************
  785. *
  786. * ioctl_get_fmt
  787. *
  788. * V4L2 format get
  789. *
  790. *****************************************************************************/
  791. static int ioctl_get_fmt(void *arg,struct camera_data *cam)
  792. {
  793. struct v4l2_format *f = arg;
  794. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  795. return -EINVAL;
  796. f->fmt.pix.width = cam->vw.width;
  797. f->fmt.pix.height = cam->vw.height;
  798. f->fmt.pix.pixelformat = cam->pixelformat;
  799. f->fmt.pix.field = V4L2_FIELD_NONE;
  800. f->fmt.pix.bytesperline = 0;
  801. f->fmt.pix.sizeimage = cam->frame_size;
  802. f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
  803. f->fmt.pix.priv = 0;
  804. return 0;
  805. }
  806. /******************************************************************************
  807. *
  808. * ioctl_cropcap
  809. *
  810. * V4L2 query cropping capabilities
  811. * NOTE: cropping is currently disabled
  812. *
  813. *****************************************************************************/
  814. static int ioctl_cropcap(void *arg,struct camera_data *cam)
  815. {
  816. struct v4l2_cropcap *c = arg;
  817. if (c->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  818. return -EINVAL;
  819. c->bounds.left = 0;
  820. c->bounds.top = 0;
  821. c->bounds.width = cam->vw.width;
  822. c->bounds.height = cam->vw.height;
  823. c->defrect.left = 0;
  824. c->defrect.top = 0;
  825. c->defrect.width = cam->vw.width;
  826. c->defrect.height = cam->vw.height;
  827. c->pixelaspect.numerator = 1;
  828. c->pixelaspect.denominator = 1;
  829. return 0;
  830. }
  831. /******************************************************************************
  832. *
  833. * ioctl_queryctrl
  834. *
  835. * V4L2 query possible control variables
  836. *
  837. *****************************************************************************/
  838. static int ioctl_queryctrl(void *arg,struct camera_data *cam)
  839. {
  840. struct v4l2_queryctrl *c = arg;
  841. int i;
  842. for(i=0; i<NUM_CONTROLS; ++i) {
  843. if(c->id == controls[i].id) {
  844. memcpy(c, controls+i, sizeof(*c));
  845. break;
  846. }
  847. }
  848. if(i == NUM_CONTROLS)
  849. return -EINVAL;
  850. /* Some devices have additional limitations */
  851. switch(c->id) {
  852. case V4L2_CID_BRIGHTNESS:
  853. /***
  854. * Don't let the register be set to zero - bug in VP4
  855. * flash of full brightness
  856. ***/
  857. if (cam->params.pnp_id.device_type == DEVICE_STV_672)
  858. c->minimum = 1;
  859. break;
  860. case V4L2_CID_VFLIP:
  861. // VP5 Only
  862. if(cam->params.pnp_id.device_type == DEVICE_STV_672)
  863. c->flags |= V4L2_CTRL_FLAG_DISABLED;
  864. break;
  865. case CPIA2_CID_FRAMERATE:
  866. if(cam->params.pnp_id.device_type == DEVICE_STV_672 &&
  867. cam->params.version.sensor_flags==CPIA2_VP_SENSOR_FLAGS_500){
  868. // Maximum 15fps
  869. for(i=0; i<c->maximum; ++i) {
  870. if(framerate_controls[i].value ==
  871. CPIA2_VP_FRAMERATE_15) {
  872. c->maximum = i;
  873. c->default_value = i;
  874. }
  875. }
  876. }
  877. break;
  878. case CPIA2_CID_FLICKER_MODE:
  879. // Flicker control only valid for 672.
  880. if(cam->params.pnp_id.device_type != DEVICE_STV_672)
  881. c->flags |= V4L2_CTRL_FLAG_DISABLED;
  882. break;
  883. case CPIA2_CID_LIGHTS:
  884. // Light control only valid for the QX5 Microscope.
  885. if(cam->params.pnp_id.product != 0x151)
  886. c->flags |= V4L2_CTRL_FLAG_DISABLED;
  887. break;
  888. default:
  889. break;
  890. }
  891. return 0;
  892. }
  893. /******************************************************************************
  894. *
  895. * ioctl_querymenu
  896. *
  897. * V4L2 query possible control variables
  898. *
  899. *****************************************************************************/
  900. static int ioctl_querymenu(void *arg,struct camera_data *cam)
  901. {
  902. struct v4l2_querymenu *m = arg;
  903. memset(m->name, 0, sizeof(m->name));
  904. m->reserved = 0;
  905. switch(m->id) {
  906. case CPIA2_CID_FLICKER_MODE:
  907. if (m->index >= NUM_FLICKER_CONTROLS)
  908. return -EINVAL;
  909. strcpy(m->name, flicker_controls[m->index].name);
  910. break;
  911. case CPIA2_CID_FRAMERATE:
  912. {
  913. int maximum = NUM_FRAMERATE_CONTROLS - 1;
  914. if(cam->params.pnp_id.device_type == DEVICE_STV_672 &&
  915. cam->params.version.sensor_flags==CPIA2_VP_SENSOR_FLAGS_500){
  916. // Maximum 15fps
  917. int i;
  918. for(i=0; i<maximum; ++i) {
  919. if(framerate_controls[i].value ==
  920. CPIA2_VP_FRAMERATE_15)
  921. maximum = i;
  922. }
  923. }
  924. if (m->index > maximum)
  925. return -EINVAL;
  926. strcpy(m->name, framerate_controls[m->index].name);
  927. break;
  928. }
  929. case CPIA2_CID_LIGHTS:
  930. if (m->index >= NUM_LIGHTS_CONTROLS)
  931. return -EINVAL;
  932. strcpy(m->name, lights_controls[m->index].name);
  933. break;
  934. default:
  935. return -EINVAL;
  936. }
  937. return 0;
  938. }
  939. /******************************************************************************
  940. *
  941. * ioctl_g_ctrl
  942. *
  943. * V4L2 get the value of a control variable
  944. *
  945. *****************************************************************************/
  946. static int ioctl_g_ctrl(void *arg,struct camera_data *cam)
  947. {
  948. struct v4l2_control *c = arg;
  949. switch(c->id) {
  950. case V4L2_CID_BRIGHTNESS:
  951. cpia2_do_command(cam, CPIA2_CMD_GET_VP_BRIGHTNESS,
  952. TRANSFER_READ, 0);
  953. c->value = cam->params.color_params.brightness;
  954. break;
  955. case V4L2_CID_CONTRAST:
  956. cpia2_do_command(cam, CPIA2_CMD_GET_CONTRAST,
  957. TRANSFER_READ, 0);
  958. c->value = cam->params.color_params.contrast;
  959. break;
  960. case V4L2_CID_SATURATION:
  961. cpia2_do_command(cam, CPIA2_CMD_GET_VP_SATURATION,
  962. TRANSFER_READ, 0);
  963. c->value = cam->params.color_params.saturation;
  964. break;
  965. case V4L2_CID_HFLIP:
  966. cpia2_do_command(cam, CPIA2_CMD_GET_USER_EFFECTS,
  967. TRANSFER_READ, 0);
  968. c->value = (cam->params.vp_params.user_effects &
  969. CPIA2_VP_USER_EFFECTS_MIRROR) != 0;
  970. break;
  971. case V4L2_CID_VFLIP:
  972. cpia2_do_command(cam, CPIA2_CMD_GET_USER_EFFECTS,
  973. TRANSFER_READ, 0);
  974. c->value = (cam->params.vp_params.user_effects &
  975. CPIA2_VP_USER_EFFECTS_FLIP) != 0;
  976. break;
  977. case CPIA2_CID_TARGET_KB:
  978. c->value = cam->params.vc_params.target_kb;
  979. break;
  980. case CPIA2_CID_GPIO:
  981. cpia2_do_command(cam, CPIA2_CMD_GET_VP_GPIO_DATA,
  982. TRANSFER_READ, 0);
  983. c->value = cam->params.vp_params.gpio_data;
  984. break;
  985. case CPIA2_CID_FLICKER_MODE:
  986. {
  987. int i, mode;
  988. cpia2_do_command(cam, CPIA2_CMD_GET_FLICKER_MODES,
  989. TRANSFER_READ, 0);
  990. if(cam->params.flicker_control.cam_register &
  991. CPIA2_VP_FLICKER_MODES_NEVER_FLICKER) {
  992. mode = NEVER_FLICKER;
  993. } else {
  994. if(cam->params.flicker_control.cam_register &
  995. CPIA2_VP_FLICKER_MODES_50HZ) {
  996. mode = FLICKER_50;
  997. } else {
  998. mode = FLICKER_60;
  999. }
  1000. }
  1001. for(i=0; i<NUM_FLICKER_CONTROLS; i++) {
  1002. if(flicker_controls[i].value == mode) {
  1003. c->value = i;
  1004. break;
  1005. }
  1006. }
  1007. if(i == NUM_FLICKER_CONTROLS)
  1008. return -EINVAL;
  1009. break;
  1010. }
  1011. case CPIA2_CID_FRAMERATE:
  1012. {
  1013. int maximum = NUM_FRAMERATE_CONTROLS - 1;
  1014. int i;
  1015. for(i=0; i<= maximum; i++) {
  1016. if(cam->params.vp_params.frame_rate ==
  1017. framerate_controls[i].value)
  1018. break;
  1019. }
  1020. if(i > maximum)
  1021. return -EINVAL;
  1022. c->value = i;
  1023. break;
  1024. }
  1025. case CPIA2_CID_USB_ALT:
  1026. c->value = cam->params.camera_state.stream_mode;
  1027. break;
  1028. case CPIA2_CID_LIGHTS:
  1029. {
  1030. int i;
  1031. cpia2_do_command(cam, CPIA2_CMD_GET_VP_GPIO_DATA,
  1032. TRANSFER_READ, 0);
  1033. for(i=0; i<NUM_LIGHTS_CONTROLS; i++) {
  1034. if((cam->params.vp_params.gpio_data&GPIO_LIGHTS_MASK) ==
  1035. lights_controls[i].value) {
  1036. break;
  1037. }
  1038. }
  1039. if(i == NUM_LIGHTS_CONTROLS)
  1040. return -EINVAL;
  1041. c->value = i;
  1042. break;
  1043. }
  1044. case CPIA2_CID_RESET_CAMERA:
  1045. return -EINVAL;
  1046. default:
  1047. return -EINVAL;
  1048. }
  1049. DBG("Get control id:%d, value:%d\n", c->id, c->value);
  1050. return 0;
  1051. }
  1052. /******************************************************************************
  1053. *
  1054. * ioctl_s_ctrl
  1055. *
  1056. * V4L2 set the value of a control variable
  1057. *
  1058. *****************************************************************************/
  1059. static int ioctl_s_ctrl(void *arg,struct camera_data *cam)
  1060. {
  1061. struct v4l2_control *c = arg;
  1062. int i;
  1063. int retval = 0;
  1064. DBG("Set control id:%d, value:%d\n", c->id, c->value);
  1065. /* Check that the value is in range */
  1066. for(i=0; i<NUM_CONTROLS; i++) {
  1067. if(c->id == controls[i].id) {
  1068. if(c->value < controls[i].minimum ||
  1069. c->value > controls[i].maximum) {
  1070. return -EINVAL;
  1071. }
  1072. break;
  1073. }
  1074. }
  1075. if(i == NUM_CONTROLS)
  1076. return -EINVAL;
  1077. switch(c->id) {
  1078. case V4L2_CID_BRIGHTNESS:
  1079. cpia2_set_brightness(cam, c->value);
  1080. break;
  1081. case V4L2_CID_CONTRAST:
  1082. cpia2_set_contrast(cam, c->value);
  1083. break;
  1084. case V4L2_CID_SATURATION:
  1085. cpia2_set_saturation(cam, c->value);
  1086. break;
  1087. case V4L2_CID_HFLIP:
  1088. cpia2_set_property_mirror(cam, c->value);
  1089. break;
  1090. case V4L2_CID_VFLIP:
  1091. cpia2_set_property_flip(cam, c->value);
  1092. break;
  1093. case CPIA2_CID_TARGET_KB:
  1094. retval = cpia2_set_target_kb(cam, c->value);
  1095. break;
  1096. case CPIA2_CID_GPIO:
  1097. retval = cpia2_set_gpio(cam, c->value);
  1098. break;
  1099. case CPIA2_CID_FLICKER_MODE:
  1100. retval = cpia2_set_flicker_mode(cam,
  1101. flicker_controls[c->value].value);
  1102. break;
  1103. case CPIA2_CID_FRAMERATE:
  1104. retval = cpia2_set_fps(cam, framerate_controls[c->value].value);
  1105. break;
  1106. case CPIA2_CID_USB_ALT:
  1107. retval = cpia2_usb_change_streaming_alternate(cam, c->value);
  1108. break;
  1109. case CPIA2_CID_LIGHTS:
  1110. retval = cpia2_set_gpio(cam, lights_controls[c->value].value);
  1111. break;
  1112. case CPIA2_CID_RESET_CAMERA:
  1113. cpia2_usb_stream_pause(cam);
  1114. cpia2_reset_camera(cam);
  1115. cpia2_usb_stream_resume(cam);
  1116. break;
  1117. default:
  1118. retval = -EINVAL;
  1119. }
  1120. return retval;
  1121. }
  1122. /******************************************************************************
  1123. *
  1124. * ioctl_g_jpegcomp
  1125. *
  1126. * V4L2 get the JPEG compression parameters
  1127. *
  1128. *****************************************************************************/
  1129. static int ioctl_g_jpegcomp(void *arg,struct camera_data *cam)
  1130. {
  1131. struct v4l2_jpegcompression *parms = arg;
  1132. memset(parms, 0, sizeof(*parms));
  1133. parms->quality = 80; // TODO: Can this be made meaningful?
  1134. parms->jpeg_markers = V4L2_JPEG_MARKER_DQT | V4L2_JPEG_MARKER_DRI;
  1135. if(!cam->params.compression.inhibit_htables) {
  1136. parms->jpeg_markers |= V4L2_JPEG_MARKER_DHT;
  1137. }
  1138. parms->APPn = cam->APPn;
  1139. parms->APP_len = cam->APP_len;
  1140. if(cam->APP_len > 0) {
  1141. memcpy(parms->APP_data, cam->APP_data, cam->APP_len);
  1142. parms->jpeg_markers |= V4L2_JPEG_MARKER_APP;
  1143. }
  1144. parms->COM_len = cam->COM_len;
  1145. if(cam->COM_len > 0) {
  1146. memcpy(parms->COM_data, cam->COM_data, cam->COM_len);
  1147. parms->jpeg_markers |= JPEG_MARKER_COM;
  1148. }
  1149. DBG("G_JPEGCOMP APP_len:%d COM_len:%d\n",
  1150. parms->APP_len, parms->COM_len);
  1151. return 0;
  1152. }
  1153. /******************************************************************************
  1154. *
  1155. * ioctl_s_jpegcomp
  1156. *
  1157. * V4L2 set the JPEG compression parameters
  1158. * NOTE: quality and some jpeg_markers are ignored.
  1159. *
  1160. *****************************************************************************/
  1161. static int ioctl_s_jpegcomp(void *arg,struct camera_data *cam)
  1162. {
  1163. struct v4l2_jpegcompression *parms = arg;
  1164. DBG("S_JPEGCOMP APP_len:%d COM_len:%d\n",
  1165. parms->APP_len, parms->COM_len);
  1166. cam->params.compression.inhibit_htables =
  1167. !(parms->jpeg_markers & V4L2_JPEG_MARKER_DHT);
  1168. if(parms->APP_len != 0) {
  1169. if(parms->APP_len > 0 &&
  1170. parms->APP_len <= sizeof(cam->APP_data) &&
  1171. parms->APPn >= 0 && parms->APPn <= 15) {
  1172. cam->APPn = parms->APPn;
  1173. cam->APP_len = parms->APP_len;
  1174. memcpy(cam->APP_data, parms->APP_data, parms->APP_len);
  1175. } else {
  1176. LOG("Bad APPn Params n=%d len=%d\n",
  1177. parms->APPn, parms->APP_len);
  1178. return -EINVAL;
  1179. }
  1180. } else {
  1181. cam->APP_len = 0;
  1182. }
  1183. if(parms->COM_len != 0) {
  1184. if(parms->COM_len > 0 &&
  1185. parms->COM_len <= sizeof(cam->COM_data)) {
  1186. cam->COM_len = parms->COM_len;
  1187. memcpy(cam->COM_data, parms->COM_data, parms->COM_len);
  1188. } else {
  1189. LOG("Bad COM_len=%d\n", parms->COM_len);
  1190. return -EINVAL;
  1191. }
  1192. }
  1193. return 0;
  1194. }
  1195. /******************************************************************************
  1196. *
  1197. * ioctl_reqbufs
  1198. *
  1199. * V4L2 Initiate memory mapping.
  1200. * NOTE: The user's request is ignored. For now the buffers are fixed.
  1201. *
  1202. *****************************************************************************/
  1203. static int ioctl_reqbufs(void *arg,struct camera_data *cam)
  1204. {
  1205. struct v4l2_requestbuffers *req = arg;
  1206. if(req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  1207. req->memory != V4L2_MEMORY_MMAP)
  1208. return -EINVAL;
  1209. DBG("REQBUFS requested:%d returning:%d\n", req->count, cam->num_frames);
  1210. req->count = cam->num_frames;
  1211. memset(&req->reserved, 0, sizeof(req->reserved));
  1212. return 0;
  1213. }
  1214. /******************************************************************************
  1215. *
  1216. * ioctl_querybuf
  1217. *
  1218. * V4L2 Query memory buffer status.
  1219. *
  1220. *****************************************************************************/
  1221. static int ioctl_querybuf(void *arg,struct camera_data *cam)
  1222. {
  1223. struct v4l2_buffer *buf = arg;
  1224. if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  1225. buf->index > cam->num_frames)
  1226. return -EINVAL;
  1227. buf->m.offset = cam->buffers[buf->index].data - cam->frame_buffer;
  1228. buf->length = cam->frame_size;
  1229. buf->memory = V4L2_MEMORY_MMAP;
  1230. if(cam->mmapped)
  1231. buf->flags = V4L2_BUF_FLAG_MAPPED;
  1232. else
  1233. buf->flags = 0;
  1234. switch (cam->buffers[buf->index].status) {
  1235. case FRAME_EMPTY:
  1236. case FRAME_ERROR:
  1237. case FRAME_READING:
  1238. buf->bytesused = 0;
  1239. buf->flags = V4L2_BUF_FLAG_QUEUED;
  1240. break;
  1241. case FRAME_READY:
  1242. buf->bytesused = cam->buffers[buf->index].length;
  1243. buf->timestamp = cam->buffers[buf->index].timestamp;
  1244. buf->sequence = cam->buffers[buf->index].seq;
  1245. buf->flags = V4L2_BUF_FLAG_DONE;
  1246. break;
  1247. }
  1248. DBG("QUERYBUF index:%d offset:%d flags:%d seq:%d bytesused:%d\n",
  1249. buf->index, buf->m.offset, buf->flags, buf->sequence,
  1250. buf->bytesused);
  1251. return 0;
  1252. }
  1253. /******************************************************************************
  1254. *
  1255. * ioctl_qbuf
  1256. *
  1257. * V4L2 User is freeing buffer
  1258. *
  1259. *****************************************************************************/
  1260. static int ioctl_qbuf(void *arg,struct camera_data *cam)
  1261. {
  1262. struct v4l2_buffer *buf = arg;
  1263. if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  1264. buf->memory != V4L2_MEMORY_MMAP ||
  1265. buf->index > cam->num_frames)
  1266. return -EINVAL;
  1267. DBG("QBUF #%d\n", buf->index);
  1268. if(cam->buffers[buf->index].status == FRAME_READY)
  1269. cam->buffers[buf->index].status = FRAME_EMPTY;
  1270. return 0;
  1271. }
  1272. /******************************************************************************
  1273. *
  1274. * find_earliest_filled_buffer
  1275. *
  1276. * Helper for ioctl_dqbuf. Find the next ready buffer.
  1277. *
  1278. *****************************************************************************/
  1279. static int find_earliest_filled_buffer(struct camera_data *cam)
  1280. {
  1281. int i;
  1282. int found = -1;
  1283. for (i=0; i<cam->num_frames; i++) {
  1284. if(cam->buffers[i].status == FRAME_READY) {
  1285. if(found < 0) {
  1286. found = i;
  1287. } else {
  1288. /* find which buffer is earlier */
  1289. struct timeval *tv1, *tv2;
  1290. tv1 = &cam->buffers[i].timestamp;
  1291. tv2 = &cam->buffers[found].timestamp;
  1292. if(tv1->tv_sec < tv2->tv_sec ||
  1293. (tv1->tv_sec == tv2->tv_sec &&
  1294. tv1->tv_usec < tv2->tv_usec))
  1295. found = i;
  1296. }
  1297. }
  1298. }
  1299. return found;
  1300. }
  1301. /******************************************************************************
  1302. *
  1303. * ioctl_dqbuf
  1304. *
  1305. * V4L2 User is asking for a filled buffer.
  1306. *
  1307. *****************************************************************************/
  1308. static int ioctl_dqbuf(void *arg,struct camera_data *cam, struct file *file)
  1309. {
  1310. struct v4l2_buffer *buf = arg;
  1311. int frame;
  1312. if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  1313. buf->memory != V4L2_MEMORY_MMAP)
  1314. return -EINVAL;
  1315. frame = find_earliest_filled_buffer(cam);
  1316. if(frame < 0 && file->f_flags&O_NONBLOCK)
  1317. return -EAGAIN;
  1318. if(frame < 0) {
  1319. /* Wait for a frame to become available */
  1320. struct framebuf *cb=cam->curbuff;
  1321. mutex_unlock(&cam->busy_lock);
  1322. wait_event_interruptible(cam->wq_stream,
  1323. !cam->present ||
  1324. (cb=cam->curbuff)->status == FRAME_READY);
  1325. mutex_lock(&cam->busy_lock);
  1326. if (signal_pending(current))
  1327. return -ERESTARTSYS;
  1328. if(!cam->present)
  1329. return -ENOTTY;
  1330. frame = cb->num;
  1331. }
  1332. buf->index = frame;
  1333. buf->bytesused = cam->buffers[buf->index].length;
  1334. buf->flags = V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_DONE;
  1335. buf->field = V4L2_FIELD_NONE;
  1336. buf->timestamp = cam->buffers[buf->index].timestamp;
  1337. buf->sequence = cam->buffers[buf->index].seq;
  1338. buf->m.offset = cam->buffers[buf->index].data - cam->frame_buffer;
  1339. buf->length = cam->frame_size;
  1340. buf->input = 0;
  1341. buf->reserved = 0;
  1342. memset(&buf->timecode, 0, sizeof(buf->timecode));
  1343. DBG("DQBUF #%d status:%d seq:%d length:%d\n", buf->index,
  1344. cam->buffers[buf->index].status, buf->sequence, buf->bytesused);
  1345. return 0;
  1346. }
  1347. /******************************************************************************
  1348. *
  1349. * cpia2_ioctl
  1350. *
  1351. *****************************************************************************/
  1352. static long cpia2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
  1353. {
  1354. struct camera_data *cam = video_drvdata(file);
  1355. long retval = 0;
  1356. if (!cam)
  1357. return -ENOTTY;
  1358. /* make this _really_ smp-safe */
  1359. if (mutex_lock_interruptible(&cam->busy_lock))
  1360. return -ERESTARTSYS;
  1361. if (!cam->present) {
  1362. mutex_unlock(&cam->busy_lock);
  1363. return -ENODEV;
  1364. }
  1365. /* Priority check */
  1366. switch (cmd) {
  1367. case VIDIOCSWIN:
  1368. case VIDIOCMCAPTURE:
  1369. case VIDIOC_S_FMT:
  1370. {
  1371. struct cpia2_fh *fh = file->private_data;
  1372. retval = v4l2_prio_check(&cam->prio, &fh->prio);
  1373. if(retval) {
  1374. mutex_unlock(&cam->busy_lock);
  1375. return retval;
  1376. }
  1377. break;
  1378. }
  1379. case VIDIOCGMBUF:
  1380. case VIDIOCSYNC:
  1381. {
  1382. struct cpia2_fh *fh = file->private_data;
  1383. if(fh->prio != V4L2_PRIORITY_RECORD) {
  1384. mutex_unlock(&cam->busy_lock);
  1385. return -EBUSY;
  1386. }
  1387. break;
  1388. }
  1389. default:
  1390. break;
  1391. }
  1392. switch (cmd) {
  1393. case VIDIOCGCAP: /* query capabilities */
  1394. retval = ioctl_cap_query(arg, cam);
  1395. break;
  1396. case VIDIOCGCHAN: /* get video source - we are a camera, nothing else */
  1397. retval = ioctl_get_channel(arg);
  1398. break;
  1399. case VIDIOCSCHAN: /* set video source - we are a camera, nothing else */
  1400. retval = ioctl_set_channel(arg);
  1401. break;
  1402. case VIDIOCGPICT: /* image properties */
  1403. memcpy(arg, &cam->vp, sizeof(struct video_picture));
  1404. break;
  1405. case VIDIOCSPICT:
  1406. retval = ioctl_set_image_prop(arg, cam);
  1407. break;
  1408. case VIDIOCGWIN: /* get/set capture window */
  1409. memcpy(arg, &cam->vw, sizeof(struct video_window));
  1410. break;
  1411. case VIDIOCSWIN:
  1412. retval = ioctl_set_window_size(arg, cam, file->private_data);
  1413. break;
  1414. case VIDIOCGMBUF: /* mmap interface */
  1415. retval = ioctl_get_mbuf(arg, cam);
  1416. break;
  1417. case VIDIOCMCAPTURE:
  1418. retval = ioctl_mcapture(arg, cam, file->private_data);
  1419. break;
  1420. case VIDIOCSYNC:
  1421. retval = ioctl_sync(arg, cam);
  1422. break;
  1423. /* pointless to implement overlay with this camera */
  1424. case VIDIOCCAPTURE:
  1425. case VIDIOCGFBUF:
  1426. case VIDIOCSFBUF:
  1427. case VIDIOCKEY:
  1428. retval = -EINVAL;
  1429. break;
  1430. /* tuner interface - we have none */
  1431. case VIDIOCGTUNER:
  1432. case VIDIOCSTUNER:
  1433. case VIDIOCGFREQ:
  1434. case VIDIOCSFREQ:
  1435. retval = -EINVAL;
  1436. break;
  1437. /* audio interface - we have none */
  1438. case VIDIOCGAUDIO:
  1439. case VIDIOCSAUDIO:
  1440. retval = -EINVAL;
  1441. break;
  1442. /* CPIA2 extension to Video4Linux API */
  1443. case CPIA2_IOC_SET_GPIO:
  1444. retval = ioctl_set_gpio(arg, cam);
  1445. break;
  1446. case VIDIOC_QUERYCAP:
  1447. retval = ioctl_querycap(arg,cam);
  1448. break;
  1449. case VIDIOC_ENUMINPUT:
  1450. case VIDIOC_G_INPUT:
  1451. case VIDIOC_S_INPUT:
  1452. retval = ioctl_input(cmd, arg, cam);
  1453. break;
  1454. case VIDIOC_ENUM_FMT:
  1455. retval = ioctl_enum_fmt(arg,cam);
  1456. break;
  1457. case VIDIOC_TRY_FMT:
  1458. retval = ioctl_try_fmt(arg,cam);
  1459. break;
  1460. case VIDIOC_G_FMT:
  1461. retval = ioctl_get_fmt(arg,cam);
  1462. break;
  1463. case VIDIOC_S_FMT:
  1464. retval = ioctl_set_fmt(arg,cam,file->private_data);
  1465. break;
  1466. case VIDIOC_CROPCAP:
  1467. retval = ioctl_cropcap(arg,cam);
  1468. break;
  1469. case VIDIOC_G_CROP:
  1470. case VIDIOC_S_CROP:
  1471. // TODO: I think cropping can be implemented - SJB
  1472. retval = -EINVAL;
  1473. break;
  1474. case VIDIOC_QUERYCTRL:
  1475. retval = ioctl_queryctrl(arg,cam);
  1476. break;
  1477. case VIDIOC_QUERYMENU:
  1478. retval = ioctl_querymenu(arg,cam);
  1479. break;
  1480. case VIDIOC_G_CTRL:
  1481. retval = ioctl_g_ctrl(arg,cam);
  1482. break;
  1483. case VIDIOC_S_CTRL:
  1484. retval = ioctl_s_ctrl(arg,cam);
  1485. break;
  1486. case VIDIOC_G_JPEGCOMP:
  1487. retval = ioctl_g_jpegcomp(arg,cam);
  1488. break;
  1489. case VIDIOC_S_JPEGCOMP:
  1490. retval = ioctl_s_jpegcomp(arg,cam);
  1491. break;
  1492. case VIDIOC_G_PRIORITY:
  1493. {
  1494. struct cpia2_fh *fh = file->private_data;
  1495. *(enum v4l2_priority*)arg = fh->prio;
  1496. break;
  1497. }
  1498. case VIDIOC_S_PRIORITY:
  1499. {
  1500. struct cpia2_fh *fh = file->private_data;
  1501. enum v4l2_priority prio;
  1502. prio = *(enum v4l2_priority*)arg;
  1503. if(cam->streaming &&
  1504. prio != fh->prio &&
  1505. fh->prio == V4L2_PRIORITY_RECORD) {
  1506. /* Can't drop record priority while streaming */
  1507. retval = -EBUSY;
  1508. } else if(prio == V4L2_PRIORITY_RECORD &&
  1509. prio != fh->prio &&
  1510. v4l2_prio_max(&cam->prio) == V4L2_PRIORITY_RECORD) {
  1511. /* Only one program can record at a time */
  1512. retval = -EBUSY;
  1513. } else {
  1514. retval = v4l2_prio_change(&cam->prio, &fh->prio, prio);
  1515. }
  1516. break;
  1517. }
  1518. case VIDIOC_REQBUFS:
  1519. retval = ioctl_reqbufs(arg,cam);
  1520. break;
  1521. case VIDIOC_QUERYBUF:
  1522. retval = ioctl_querybuf(arg,cam);
  1523. break;
  1524. case VIDIOC_QBUF:
  1525. retval = ioctl_qbuf(arg,cam);
  1526. break;
  1527. case VIDIOC_DQBUF:
  1528. retval = ioctl_dqbuf(arg,cam,file);
  1529. break;
  1530. case VIDIOC_STREAMON:
  1531. {
  1532. int type;
  1533. DBG("VIDIOC_STREAMON, streaming=%d\n", cam->streaming);
  1534. type = *(int*)arg;
  1535. if(!cam->mmapped || type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1536. retval = -EINVAL;
  1537. if(!cam->streaming) {
  1538. retval = cpia2_usb_stream_start(cam,
  1539. cam->params.camera_state.stream_mode);
  1540. } else {
  1541. retval = -EINVAL;
  1542. }
  1543. break;
  1544. }
  1545. case VIDIOC_STREAMOFF:
  1546. {
  1547. int type;
  1548. DBG("VIDIOC_STREAMOFF, streaming=%d\n", cam->streaming);
  1549. type = *(int*)arg;
  1550. if(!cam->mmapped || type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1551. retval = -EINVAL;
  1552. if(cam->streaming) {
  1553. retval = cpia2_usb_stream_stop(cam);
  1554. } else {
  1555. retval = -EINVAL;
  1556. }
  1557. break;
  1558. }
  1559. case VIDIOC_ENUMOUTPUT:
  1560. case VIDIOC_G_OUTPUT:
  1561. case VIDIOC_S_OUTPUT:
  1562. case VIDIOC_G_MODULATOR:
  1563. case VIDIOC_S_MODULATOR:
  1564. case VIDIOC_ENUMAUDIO:
  1565. case VIDIOC_G_AUDIO:
  1566. case VIDIOC_S_AUDIO:
  1567. case VIDIOC_ENUMAUDOUT:
  1568. case VIDIOC_G_AUDOUT:
  1569. case VIDIOC_S_AUDOUT:
  1570. case VIDIOC_ENUMSTD:
  1571. case VIDIOC_QUERYSTD:
  1572. case VIDIOC_G_STD:
  1573. case VIDIOC_S_STD:
  1574. case VIDIOC_G_TUNER:
  1575. case VIDIOC_S_TUNER:
  1576. case VIDIOC_G_FREQUENCY:
  1577. case VIDIOC_S_FREQUENCY:
  1578. case VIDIOC_OVERLAY:
  1579. case VIDIOC_G_FBUF:
  1580. case VIDIOC_S_FBUF:
  1581. case VIDIOC_G_PARM:
  1582. case VIDIOC_S_PARM:
  1583. retval = -EINVAL;
  1584. break;
  1585. default:
  1586. retval = -ENOIOCTLCMD;
  1587. break;
  1588. }
  1589. mutex_unlock(&cam->busy_lock);
  1590. return retval;
  1591. }
  1592. static long cpia2_ioctl(struct file *file,
  1593. unsigned int cmd, unsigned long arg)
  1594. {
  1595. return video_usercopy(file, cmd, arg, cpia2_do_ioctl);
  1596. }
  1597. /******************************************************************************
  1598. *
  1599. * cpia2_mmap
  1600. *
  1601. *****************************************************************************/
  1602. static int cpia2_mmap(struct file *file, struct vm_area_struct *area)
  1603. {
  1604. struct camera_data *cam = video_drvdata(file);
  1605. int retval;
  1606. /* Priority check */
  1607. struct cpia2_fh *fh = file->private_data;
  1608. if(fh->prio != V4L2_PRIORITY_RECORD) {
  1609. return -EBUSY;
  1610. }
  1611. retval = cpia2_remap_buffer(cam, area);
  1612. if(!retval)
  1613. fh->mmapped = 1;
  1614. return retval;
  1615. }
  1616. /******************************************************************************
  1617. *
  1618. * reset_camera_struct_v4l
  1619. *
  1620. * Sets all values to the defaults
  1621. *****************************************************************************/
  1622. static void reset_camera_struct_v4l(struct camera_data *cam)
  1623. {
  1624. /***
  1625. * Fill in the v4l structures. video_cap is filled in inside the VIDIOCCAP
  1626. * Ioctl. Here, just do the window and picture stucts.
  1627. ***/
  1628. cam->vp.palette = (u16) VIDEO_PALETTE_RGB24; /* Is this right? */
  1629. cam->vp.brightness = (u16) cam->params.color_params.brightness * 256;
  1630. cam->vp.colour = (u16) cam->params.color_params.saturation * 256;
  1631. cam->vp.contrast = (u16) cam->params.color_params.contrast * 256;
  1632. cam->vw.x = 0;
  1633. cam->vw.y = 0;
  1634. cam->vw.width = cam->params.roi.width;
  1635. cam->vw.height = cam->params.roi.height;
  1636. cam->vw.flags = 0;
  1637. cam->vw.clipcount = 0;
  1638. cam->frame_size = buffer_size;
  1639. cam->num_frames = num_buffers;
  1640. /* FlickerModes */
  1641. cam->params.flicker_control.flicker_mode_req = flicker_mode;
  1642. cam->params.flicker_control.mains_frequency = flicker_freq;
  1643. /* streamMode */
  1644. cam->params.camera_state.stream_mode = alternate;
  1645. cam->pixelformat = V4L2_PIX_FMT_JPEG;
  1646. v4l2_prio_init(&cam->prio);
  1647. return;
  1648. }
  1649. /***
  1650. * The v4l video device structure initialized for this device
  1651. ***/
  1652. static const struct v4l2_file_operations fops_template = {
  1653. .owner = THIS_MODULE,
  1654. .open = cpia2_open,
  1655. .release = cpia2_close,
  1656. .read = cpia2_v4l_read,
  1657. .poll = cpia2_v4l_poll,
  1658. .ioctl = cpia2_ioctl,
  1659. .mmap = cpia2_mmap,
  1660. };
  1661. static struct video_device cpia2_template = {
  1662. /* I could not find any place for the old .initialize initializer?? */
  1663. .name= "CPiA2 Camera",
  1664. .minor= -1,
  1665. .fops= &fops_template,
  1666. .release= video_device_release,
  1667. };
  1668. /******************************************************************************
  1669. *
  1670. * cpia2_register_camera
  1671. *
  1672. *****************************************************************************/
  1673. int cpia2_register_camera(struct camera_data *cam)
  1674. {
  1675. cam->vdev = video_device_alloc();
  1676. if(!cam->vdev)
  1677. return -ENOMEM;
  1678. memcpy(cam->vdev, &cpia2_template, sizeof(cpia2_template));
  1679. video_set_drvdata(cam->vdev, cam);
  1680. reset_camera_struct_v4l(cam);
  1681. /* register v4l device */
  1682. if (video_register_device(cam->vdev, VFL_TYPE_GRABBER, video_nr) < 0) {
  1683. ERR("video_register_device failed\n");
  1684. video_device_release(cam->vdev);
  1685. return -ENODEV;
  1686. }
  1687. return 0;
  1688. }
  1689. /******************************************************************************
  1690. *
  1691. * cpia2_unregister_camera
  1692. *
  1693. *****************************************************************************/
  1694. void cpia2_unregister_camera(struct camera_data *cam)
  1695. {
  1696. if (!cam->open_count) {
  1697. video_unregister_device(cam->vdev);
  1698. } else {
  1699. LOG("/dev/video%d removed while open, "
  1700. "deferring video_unregister_device\n",
  1701. cam->vdev->num);
  1702. }
  1703. }
  1704. /******************************************************************************
  1705. *
  1706. * check_parameters
  1707. *
  1708. * Make sure that all user-supplied parameters are sensible
  1709. *****************************************************************************/
  1710. static void __init check_parameters(void)
  1711. {
  1712. if(buffer_size < PAGE_SIZE) {
  1713. buffer_size = PAGE_SIZE;
  1714. LOG("buffer_size too small, setting to %d\n", buffer_size);
  1715. } else if(buffer_size > 1024*1024) {
  1716. /* arbitrary upper limiit */
  1717. buffer_size = 1024*1024;
  1718. LOG("buffer_size ridiculously large, setting to %d\n",
  1719. buffer_size);
  1720. } else {
  1721. buffer_size += PAGE_SIZE-1;
  1722. buffer_size &= ~(PAGE_SIZE-1);
  1723. }
  1724. if(num_buffers < 1) {
  1725. num_buffers = 1;
  1726. LOG("num_buffers too small, setting to %d\n", num_buffers);
  1727. } else if(num_buffers > VIDEO_MAX_FRAME) {
  1728. num_buffers = VIDEO_MAX_FRAME;
  1729. LOG("num_buffers too large, setting to %d\n", num_buffers);
  1730. }
  1731. if(alternate < USBIF_ISO_1 || alternate > USBIF_ISO_6) {
  1732. alternate = DEFAULT_ALT;
  1733. LOG("alternate specified is invalid, using %d\n", alternate);
  1734. }
  1735. if (flicker_mode != NEVER_FLICKER && flicker_mode != ANTI_FLICKER_ON) {
  1736. flicker_mode = NEVER_FLICKER;
  1737. LOG("Flicker mode specified is invalid, using %d\n",
  1738. flicker_mode);
  1739. }
  1740. if (flicker_freq != FLICKER_50 && flicker_freq != FLICKER_60) {
  1741. flicker_freq = FLICKER_60;
  1742. LOG("Flicker mode specified is invalid, using %d\n",
  1743. flicker_freq);
  1744. }
  1745. if(video_nr < -1 || video_nr > 64) {
  1746. video_nr = -1;
  1747. LOG("invalid video_nr specified, must be -1 to 64\n");
  1748. }
  1749. DBG("Using %d buffers, each %d bytes, alternate=%d\n",
  1750. num_buffers, buffer_size, alternate);
  1751. }
  1752. /************ Module Stuff ***************/
  1753. /******************************************************************************
  1754. *
  1755. * cpia2_init/module_init
  1756. *
  1757. *****************************************************************************/
  1758. static int __init cpia2_init(void)
  1759. {
  1760. LOG("%s v%d.%d.%d\n",
  1761. ABOUT, CPIA2_MAJ_VER, CPIA2_MIN_VER, CPIA2_PATCH_VER);
  1762. check_parameters();
  1763. cpia2_usb_init();
  1764. return 0;
  1765. }
  1766. /******************************************************************************
  1767. *
  1768. * cpia2_exit/module_exit
  1769. *
  1770. *****************************************************************************/
  1771. static void __exit cpia2_exit(void)
  1772. {
  1773. cpia2_usb_cleanup();
  1774. schedule_timeout(2 * HZ);
  1775. }
  1776. module_init(cpia2_init);
  1777. module_exit(cpia2_exit);