cpia2_v4l.c 50 KB

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