cpia2_v4l.c 50 KB

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