cpia2_v4l.c 50 KB

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