cpia2_v4l.c 50 KB

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