cpia2_v4l.c 50 KB

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