cpia2_v4l.c 50 KB

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