cpia2_v4l.c 50 KB

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