cpia2_v4l.c 50 KB

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