cpia2_v4l.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581
  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 <linux/videodev2.h>
  38. #include <linux/stringify.h>
  39. #include <media/v4l2-ioctl.h>
  40. #include "cpia2.h"
  41. #include "cpia2dev.h"
  42. static int video_nr = -1;
  43. module_param(video_nr, int, 0);
  44. MODULE_PARM_DESC(video_nr,"video device to register (0=/dev/video0, etc)");
  45. static int buffer_size = 68*1024;
  46. module_param(buffer_size, int, 0);
  47. MODULE_PARM_DESC(buffer_size, "Size for each frame buffer in bytes (default 68k)");
  48. static int num_buffers = 3;
  49. module_param(num_buffers, int, 0);
  50. MODULE_PARM_DESC(num_buffers, "Number of frame buffers (1-"
  51. __stringify(VIDEO_MAX_FRAME) ", default 3)");
  52. static int alternate = DEFAULT_ALT;
  53. module_param(alternate, int, 0);
  54. MODULE_PARM_DESC(alternate, "USB Alternate (" __stringify(USBIF_ISO_1) "-"
  55. __stringify(USBIF_ISO_6) ", default "
  56. __stringify(DEFAULT_ALT) ")");
  57. static int flicker_freq = 60;
  58. module_param(flicker_freq, int, 0);
  59. MODULE_PARM_DESC(flicker_freq, "Flicker frequency (" __stringify(50) "or"
  60. __stringify(60) ", default "
  61. __stringify(60) ")");
  62. static int flicker_mode = NEVER_FLICKER;
  63. module_param(flicker_mode, int, 0);
  64. MODULE_PARM_DESC(flicker_mode,
  65. "Flicker supression (" __stringify(NEVER_FLICKER) "or"
  66. __stringify(ANTI_FLICKER_ON) ", default "
  67. __stringify(NEVER_FLICKER) ")");
  68. MODULE_AUTHOR("Steve Miller (STMicroelectronics) <steve.miller@st.com>");
  69. MODULE_DESCRIPTION("V4L-driver for STMicroelectronics CPiA2 based cameras");
  70. MODULE_SUPPORTED_DEVICE("video");
  71. MODULE_LICENSE("GPL");
  72. #define ABOUT "V4L-Driver for Vision CPiA2 based cameras"
  73. struct control_menu_info {
  74. int value;
  75. char name[32];
  76. };
  77. static struct control_menu_info framerate_controls[] =
  78. {
  79. { CPIA2_VP_FRAMERATE_6_25, "6.25 fps" },
  80. { CPIA2_VP_FRAMERATE_7_5, "7.5 fps" },
  81. { CPIA2_VP_FRAMERATE_12_5, "12.5 fps" },
  82. { CPIA2_VP_FRAMERATE_15, "15 fps" },
  83. { CPIA2_VP_FRAMERATE_25, "25 fps" },
  84. { CPIA2_VP_FRAMERATE_30, "30 fps" },
  85. };
  86. #define NUM_FRAMERATE_CONTROLS (ARRAY_SIZE(framerate_controls))
  87. static struct control_menu_info flicker_controls[] =
  88. {
  89. { NEVER_FLICKER, "Off" },
  90. { FLICKER_50, "50 Hz" },
  91. { FLICKER_60, "60 Hz" },
  92. };
  93. #define NUM_FLICKER_CONTROLS (ARRAY_SIZE(flicker_controls))
  94. static struct control_menu_info lights_controls[] =
  95. {
  96. { 0, "Off" },
  97. { 64, "Top" },
  98. { 128, "Bottom" },
  99. { 192, "Both" },
  100. };
  101. #define NUM_LIGHTS_CONTROLS (ARRAY_SIZE(lights_controls))
  102. #define GPIO_LIGHTS_MASK 192
  103. static struct v4l2_queryctrl controls[] = {
  104. {
  105. .id = V4L2_CID_BRIGHTNESS,
  106. .type = V4L2_CTRL_TYPE_INTEGER,
  107. .name = "Brightness",
  108. .minimum = 0,
  109. .maximum = 255,
  110. .step = 1,
  111. .default_value = DEFAULT_BRIGHTNESS,
  112. },
  113. {
  114. .id = V4L2_CID_CONTRAST,
  115. .type = V4L2_CTRL_TYPE_INTEGER,
  116. .name = "Contrast",
  117. .minimum = 0,
  118. .maximum = 255,
  119. .step = 1,
  120. .default_value = DEFAULT_CONTRAST,
  121. },
  122. {
  123. .id = V4L2_CID_SATURATION,
  124. .type = V4L2_CTRL_TYPE_INTEGER,
  125. .name = "Saturation",
  126. .minimum = 0,
  127. .maximum = 255,
  128. .step = 1,
  129. .default_value = DEFAULT_SATURATION,
  130. },
  131. {
  132. .id = V4L2_CID_HFLIP,
  133. .type = V4L2_CTRL_TYPE_BOOLEAN,
  134. .name = "Mirror Horizontally",
  135. .minimum = 0,
  136. .maximum = 1,
  137. .step = 1,
  138. .default_value = 0,
  139. },
  140. {
  141. .id = V4L2_CID_VFLIP,
  142. .type = V4L2_CTRL_TYPE_BOOLEAN,
  143. .name = "Flip Vertically",
  144. .minimum = 0,
  145. .maximum = 1,
  146. .step = 1,
  147. .default_value = 0,
  148. },
  149. {
  150. .id = CPIA2_CID_TARGET_KB,
  151. .type = V4L2_CTRL_TYPE_INTEGER,
  152. .name = "Target KB",
  153. .minimum = 0,
  154. .maximum = 255,
  155. .step = 1,
  156. .default_value = DEFAULT_TARGET_KB,
  157. },
  158. {
  159. .id = CPIA2_CID_GPIO,
  160. .type = V4L2_CTRL_TYPE_INTEGER,
  161. .name = "GPIO",
  162. .minimum = 0,
  163. .maximum = 255,
  164. .step = 1,
  165. .default_value = 0,
  166. },
  167. {
  168. .id = CPIA2_CID_FLICKER_MODE,
  169. .type = V4L2_CTRL_TYPE_MENU,
  170. .name = "Flicker Reduction",
  171. .minimum = 0,
  172. .maximum = NUM_FLICKER_CONTROLS-1,
  173. .step = 1,
  174. .default_value = 0,
  175. },
  176. {
  177. .id = CPIA2_CID_FRAMERATE,
  178. .type = V4L2_CTRL_TYPE_MENU,
  179. .name = "Framerate",
  180. .minimum = 0,
  181. .maximum = NUM_FRAMERATE_CONTROLS-1,
  182. .step = 1,
  183. .default_value = NUM_FRAMERATE_CONTROLS-1,
  184. },
  185. {
  186. .id = CPIA2_CID_USB_ALT,
  187. .type = V4L2_CTRL_TYPE_INTEGER,
  188. .name = "USB Alternate",
  189. .minimum = USBIF_ISO_1,
  190. .maximum = USBIF_ISO_6,
  191. .step = 1,
  192. .default_value = DEFAULT_ALT,
  193. },
  194. {
  195. .id = CPIA2_CID_LIGHTS,
  196. .type = V4L2_CTRL_TYPE_MENU,
  197. .name = "Lights",
  198. .minimum = 0,
  199. .maximum = NUM_LIGHTS_CONTROLS-1,
  200. .step = 1,
  201. .default_value = 0,
  202. },
  203. {
  204. .id = CPIA2_CID_RESET_CAMERA,
  205. .type = V4L2_CTRL_TYPE_BUTTON,
  206. .name = "Reset Camera",
  207. .minimum = 0,
  208. .maximum = 0,
  209. .step = 0,
  210. .default_value = 0,
  211. },
  212. };
  213. #define NUM_CONTROLS (ARRAY_SIZE(controls))
  214. /******************************************************************************
  215. *
  216. * cpia2_open
  217. *
  218. *****************************************************************************/
  219. static int cpia2_open(struct file *file)
  220. {
  221. struct camera_data *cam = video_drvdata(file);
  222. struct cpia2_fh *fh;
  223. if (!cam) {
  224. ERR("Internal error, camera_data not found!\n");
  225. return -ENODEV;
  226. }
  227. if (!cam->present)
  228. return -ENODEV;
  229. if (cam->open_count == 0) {
  230. if (cpia2_allocate_buffers(cam))
  231. return -ENOMEM;
  232. /* reset the camera */
  233. if (cpia2_reset_camera(cam) < 0)
  234. return -EIO;
  235. cam->APP_len = 0;
  236. cam->COM_len = 0;
  237. }
  238. fh = kmalloc(sizeof(*fh), GFP_KERNEL);
  239. if (!fh)
  240. return -ENOMEM;
  241. file->private_data = fh;
  242. fh->prio = V4L2_PRIORITY_UNSET;
  243. v4l2_prio_open(&cam->prio, &fh->prio);
  244. fh->mmapped = 0;
  245. ++cam->open_count;
  246. cpia2_dbg_dump_registers(cam);
  247. return 0;
  248. }
  249. /******************************************************************************
  250. *
  251. * cpia2_close
  252. *
  253. *****************************************************************************/
  254. static int cpia2_close(struct file *file)
  255. {
  256. struct video_device *dev = video_devdata(file);
  257. struct camera_data *cam = video_get_drvdata(dev);
  258. struct cpia2_fh *fh = file->private_data;
  259. if (cam->present &&
  260. (cam->open_count == 1 || fh->prio == V4L2_PRIORITY_RECORD)) {
  261. cpia2_usb_stream_stop(cam);
  262. if (cam->open_count == 1) {
  263. /* save camera state for later open */
  264. cpia2_save_camera_state(cam);
  265. cpia2_set_low_power(cam);
  266. cpia2_free_buffers(cam);
  267. }
  268. }
  269. if (fh->mmapped)
  270. cam->mmapped = 0;
  271. v4l2_prio_close(&cam->prio, fh->prio);
  272. file->private_data = NULL;
  273. kfree(fh);
  274. if (--cam->open_count == 0) {
  275. cpia2_free_buffers(cam);
  276. if (!cam->present) {
  277. video_unregister_device(dev);
  278. kfree(cam);
  279. return 0;
  280. }
  281. }
  282. return 0;
  283. }
  284. /******************************************************************************
  285. *
  286. * cpia2_v4l_read
  287. *
  288. *****************************************************************************/
  289. static ssize_t cpia2_v4l_read(struct file *file, char __user *buf, size_t count,
  290. loff_t *off)
  291. {
  292. struct camera_data *cam = video_drvdata(file);
  293. int noblock = file->f_flags&O_NONBLOCK;
  294. struct cpia2_fh *fh = file->private_data;
  295. if(!cam)
  296. return -EINVAL;
  297. /* Priority check */
  298. if(fh->prio != V4L2_PRIORITY_RECORD) {
  299. return -EBUSY;
  300. }
  301. return cpia2_read(cam, buf, count, noblock);
  302. }
  303. /******************************************************************************
  304. *
  305. * cpia2_v4l_poll
  306. *
  307. *****************************************************************************/
  308. static unsigned int cpia2_v4l_poll(struct file *filp, struct poll_table_struct *wait)
  309. {
  310. struct camera_data *cam = video_drvdata(filp);
  311. struct cpia2_fh *fh = filp->private_data;
  312. if(!cam)
  313. return POLLERR;
  314. /* Priority check */
  315. if(fh->prio != V4L2_PRIORITY_RECORD) {
  316. return POLLERR;
  317. }
  318. return cpia2_poll(cam, filp, wait);
  319. }
  320. static int sync(struct camera_data *cam, int frame_nr)
  321. {
  322. struct framebuf *frame = &cam->buffers[frame_nr];
  323. while (1) {
  324. if (frame->status == FRAME_READY)
  325. return 0;
  326. if (!cam->streaming) {
  327. frame->status = FRAME_READY;
  328. frame->length = 0;
  329. return 0;
  330. }
  331. mutex_unlock(&cam->v4l2_lock);
  332. wait_event_interruptible(cam->wq_stream,
  333. !cam->streaming ||
  334. frame->status == FRAME_READY);
  335. mutex_lock(&cam->v4l2_lock);
  336. if (signal_pending(current))
  337. return -ERESTARTSYS;
  338. if(!cam->present)
  339. return -ENOTTY;
  340. }
  341. }
  342. /******************************************************************************
  343. *
  344. * ioctl_set_gpio
  345. *
  346. *****************************************************************************/
  347. static long cpia2_default(struct file *file, void *fh, int cmd, void *arg)
  348. {
  349. struct camera_data *cam = video_drvdata(file);
  350. __u32 gpio_val;
  351. if (cmd != CPIA2_CID_GPIO)
  352. return -EINVAL;
  353. gpio_val = *(__u32*) arg;
  354. if (gpio_val &~ 0xFFU)
  355. return -EINVAL;
  356. return cpia2_set_gpio(cam, (unsigned char)gpio_val);
  357. }
  358. /******************************************************************************
  359. *
  360. * ioctl_querycap
  361. *
  362. * V4L2 device capabilities
  363. *
  364. *****************************************************************************/
  365. static int cpia2_querycap(struct file *file, void *fh, struct v4l2_capability *vc)
  366. {
  367. struct camera_data *cam = video_drvdata(file);
  368. strcpy(vc->driver, "cpia2");
  369. if (cam->params.pnp_id.product == 0x151)
  370. strcpy(vc->card, "QX5 Microscope");
  371. else
  372. strcpy(vc->card, "CPiA2 Camera");
  373. switch (cam->params.pnp_id.device_type) {
  374. case DEVICE_STV_672:
  375. strcat(vc->card, " (672/");
  376. break;
  377. case DEVICE_STV_676:
  378. strcat(vc->card, " (676/");
  379. break;
  380. default:
  381. strcat(vc->card, " (???/");
  382. break;
  383. }
  384. switch (cam->params.version.sensor_flags) {
  385. case CPIA2_VP_SENSOR_FLAGS_404:
  386. strcat(vc->card, "404)");
  387. break;
  388. case CPIA2_VP_SENSOR_FLAGS_407:
  389. strcat(vc->card, "407)");
  390. break;
  391. case CPIA2_VP_SENSOR_FLAGS_409:
  392. strcat(vc->card, "409)");
  393. break;
  394. case CPIA2_VP_SENSOR_FLAGS_410:
  395. strcat(vc->card, "410)");
  396. break;
  397. case CPIA2_VP_SENSOR_FLAGS_500:
  398. strcat(vc->card, "500)");
  399. break;
  400. default:
  401. strcat(vc->card, "???)");
  402. break;
  403. }
  404. if (usb_make_path(cam->dev, vc->bus_info, sizeof(vc->bus_info)) <0)
  405. memset(vc->bus_info,0, sizeof(vc->bus_info));
  406. vc->version = KERNEL_VERSION(CPIA2_MAJ_VER, CPIA2_MIN_VER,
  407. CPIA2_PATCH_VER);
  408. vc->capabilities = V4L2_CAP_VIDEO_CAPTURE |
  409. V4L2_CAP_READWRITE |
  410. V4L2_CAP_STREAMING;
  411. return 0;
  412. }
  413. /******************************************************************************
  414. *
  415. * ioctl_input
  416. *
  417. * V4L2 input get/set/enumerate
  418. *
  419. *****************************************************************************/
  420. static int cpia2_enum_input(struct file *file, void *fh, struct v4l2_input *i)
  421. {
  422. if (i->index)
  423. return -EINVAL;
  424. strcpy(i->name, "Camera");
  425. i->type = V4L2_INPUT_TYPE_CAMERA;
  426. return 0;
  427. }
  428. static int cpia2_g_input(struct file *file, void *fh, unsigned int *i)
  429. {
  430. *i = 0;
  431. return 0;
  432. }
  433. static int cpia2_s_input(struct file *file, void *fh, unsigned int i)
  434. {
  435. return i ? -EINVAL : 0;
  436. }
  437. /******************************************************************************
  438. *
  439. * ioctl_enum_fmt
  440. *
  441. * V4L2 format enumerate
  442. *
  443. *****************************************************************************/
  444. static int cpia2_enum_fmt_vid_cap(struct file *file, void *fh,
  445. struct v4l2_fmtdesc *f)
  446. {
  447. int index = f->index;
  448. if (index < 0 || index > 1)
  449. return -EINVAL;
  450. memset(f, 0, sizeof(*f));
  451. f->index = index;
  452. f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  453. f->flags = V4L2_FMT_FLAG_COMPRESSED;
  454. switch(index) {
  455. case 0:
  456. strcpy(f->description, "MJPEG");
  457. f->pixelformat = V4L2_PIX_FMT_MJPEG;
  458. break;
  459. case 1:
  460. strcpy(f->description, "JPEG");
  461. f->pixelformat = V4L2_PIX_FMT_JPEG;
  462. break;
  463. default:
  464. return -EINVAL;
  465. }
  466. return 0;
  467. }
  468. /******************************************************************************
  469. *
  470. * ioctl_try_fmt
  471. *
  472. * V4L2 format try
  473. *
  474. *****************************************************************************/
  475. static int cpia2_try_fmt_vid_cap(struct file *file, void *fh,
  476. struct v4l2_format *f)
  477. {
  478. struct camera_data *cam = video_drvdata(file);
  479. if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG &&
  480. f->fmt.pix.pixelformat != V4L2_PIX_FMT_JPEG)
  481. return -EINVAL;
  482. f->fmt.pix.field = V4L2_FIELD_NONE;
  483. f->fmt.pix.bytesperline = 0;
  484. f->fmt.pix.sizeimage = cam->frame_size;
  485. f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
  486. f->fmt.pix.priv = 0;
  487. switch (cpia2_match_video_size(f->fmt.pix.width, f->fmt.pix.height)) {
  488. case VIDEOSIZE_VGA:
  489. f->fmt.pix.width = 640;
  490. f->fmt.pix.height = 480;
  491. break;
  492. case VIDEOSIZE_CIF:
  493. f->fmt.pix.width = 352;
  494. f->fmt.pix.height = 288;
  495. break;
  496. case VIDEOSIZE_QVGA:
  497. f->fmt.pix.width = 320;
  498. f->fmt.pix.height = 240;
  499. break;
  500. case VIDEOSIZE_288_216:
  501. f->fmt.pix.width = 288;
  502. f->fmt.pix.height = 216;
  503. break;
  504. case VIDEOSIZE_256_192:
  505. f->fmt.pix.width = 256;
  506. f->fmt.pix.height = 192;
  507. break;
  508. case VIDEOSIZE_224_168:
  509. f->fmt.pix.width = 224;
  510. f->fmt.pix.height = 168;
  511. break;
  512. case VIDEOSIZE_192_144:
  513. f->fmt.pix.width = 192;
  514. f->fmt.pix.height = 144;
  515. break;
  516. case VIDEOSIZE_QCIF:
  517. default:
  518. f->fmt.pix.width = 176;
  519. f->fmt.pix.height = 144;
  520. break;
  521. }
  522. return 0;
  523. }
  524. /******************************************************************************
  525. *
  526. * ioctl_set_fmt
  527. *
  528. * V4L2 format set
  529. *
  530. *****************************************************************************/
  531. static int cpia2_s_fmt_vid_cap(struct file *file, void *_fh,
  532. struct v4l2_format *f)
  533. {
  534. struct camera_data *cam = video_drvdata(file);
  535. struct cpia2_fh *fh = _fh;
  536. int err, frame;
  537. err = v4l2_prio_check(&cam->prio, fh->prio);
  538. if (err)
  539. return err;
  540. err = cpia2_try_fmt_vid_cap(file, _fh, f);
  541. if(err != 0)
  542. return err;
  543. /* Ensure that only this process can change the format. */
  544. err = v4l2_prio_change(&cam->prio, &fh->prio, V4L2_PRIORITY_RECORD);
  545. if(err != 0) {
  546. return err;
  547. }
  548. cam->pixelformat = f->fmt.pix.pixelformat;
  549. /* NOTE: This should be set to 1 for MJPEG, but some apps don't handle
  550. * the missing Huffman table properly. */
  551. cam->params.compression.inhibit_htables = 0;
  552. /*f->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG;*/
  553. /* we set the video window to something smaller or equal to what
  554. * is requested by the user???
  555. */
  556. DBG("Requested width = %d, height = %d\n",
  557. f->fmt.pix.width, f->fmt.pix.height);
  558. if (f->fmt.pix.width != cam->width ||
  559. f->fmt.pix.height != cam->height) {
  560. cam->width = f->fmt.pix.width;
  561. cam->height = f->fmt.pix.height;
  562. cam->params.roi.width = f->fmt.pix.width;
  563. cam->params.roi.height = f->fmt.pix.height;
  564. cpia2_set_format(cam);
  565. }
  566. for (frame = 0; frame < cam->num_frames; ++frame) {
  567. if (cam->buffers[frame].status == FRAME_READING)
  568. if ((err = sync(cam, frame)) < 0)
  569. return err;
  570. cam->buffers[frame].status = FRAME_EMPTY;
  571. }
  572. return 0;
  573. }
  574. /******************************************************************************
  575. *
  576. * ioctl_get_fmt
  577. *
  578. * V4L2 format get
  579. *
  580. *****************************************************************************/
  581. static int cpia2_g_fmt_vid_cap(struct file *file, void *fh,
  582. struct v4l2_format *f)
  583. {
  584. struct camera_data *cam = video_drvdata(file);
  585. f->fmt.pix.width = cam->width;
  586. f->fmt.pix.height = cam->height;
  587. f->fmt.pix.pixelformat = cam->pixelformat;
  588. f->fmt.pix.field = V4L2_FIELD_NONE;
  589. f->fmt.pix.bytesperline = 0;
  590. f->fmt.pix.sizeimage = cam->frame_size;
  591. f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
  592. f->fmt.pix.priv = 0;
  593. return 0;
  594. }
  595. /******************************************************************************
  596. *
  597. * ioctl_cropcap
  598. *
  599. * V4L2 query cropping capabilities
  600. * NOTE: cropping is currently disabled
  601. *
  602. *****************************************************************************/
  603. static int cpia2_cropcap(struct file *file, void *fh, struct v4l2_cropcap *c)
  604. {
  605. struct camera_data *cam = video_drvdata(file);
  606. if (c->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  607. return -EINVAL;
  608. c->bounds.left = 0;
  609. c->bounds.top = 0;
  610. c->bounds.width = cam->width;
  611. c->bounds.height = cam->height;
  612. c->defrect.left = 0;
  613. c->defrect.top = 0;
  614. c->defrect.width = cam->width;
  615. c->defrect.height = cam->height;
  616. c->pixelaspect.numerator = 1;
  617. c->pixelaspect.denominator = 1;
  618. return 0;
  619. }
  620. /******************************************************************************
  621. *
  622. * ioctl_queryctrl
  623. *
  624. * V4L2 query possible control variables
  625. *
  626. *****************************************************************************/
  627. static int cpia2_queryctrl(struct file *file, void *fh, struct v4l2_queryctrl *c)
  628. {
  629. struct camera_data *cam = video_drvdata(file);
  630. int i;
  631. for(i=0; i<NUM_CONTROLS; ++i) {
  632. if(c->id == controls[i].id) {
  633. memcpy(c, controls+i, sizeof(*c));
  634. break;
  635. }
  636. }
  637. if(i == NUM_CONTROLS)
  638. return -EINVAL;
  639. /* Some devices have additional limitations */
  640. switch(c->id) {
  641. case V4L2_CID_BRIGHTNESS:
  642. /***
  643. * Don't let the register be set to zero - bug in VP4
  644. * flash of full brightness
  645. ***/
  646. if (cam->params.pnp_id.device_type == DEVICE_STV_672)
  647. c->minimum = 1;
  648. break;
  649. case V4L2_CID_VFLIP:
  650. // VP5 Only
  651. if(cam->params.pnp_id.device_type == DEVICE_STV_672)
  652. c->flags |= V4L2_CTRL_FLAG_DISABLED;
  653. break;
  654. case CPIA2_CID_FRAMERATE:
  655. if(cam->params.pnp_id.device_type == DEVICE_STV_672 &&
  656. cam->params.version.sensor_flags==CPIA2_VP_SENSOR_FLAGS_500){
  657. // Maximum 15fps
  658. for(i=0; i<c->maximum; ++i) {
  659. if(framerate_controls[i].value ==
  660. CPIA2_VP_FRAMERATE_15) {
  661. c->maximum = i;
  662. c->default_value = i;
  663. }
  664. }
  665. }
  666. break;
  667. case CPIA2_CID_FLICKER_MODE:
  668. // Flicker control only valid for 672.
  669. if(cam->params.pnp_id.device_type != DEVICE_STV_672)
  670. c->flags |= V4L2_CTRL_FLAG_DISABLED;
  671. break;
  672. case CPIA2_CID_LIGHTS:
  673. // Light control only valid for the QX5 Microscope.
  674. if(cam->params.pnp_id.product != 0x151)
  675. c->flags |= V4L2_CTRL_FLAG_DISABLED;
  676. break;
  677. default:
  678. break;
  679. }
  680. return 0;
  681. }
  682. /******************************************************************************
  683. *
  684. * ioctl_querymenu
  685. *
  686. * V4L2 query possible control variables
  687. *
  688. *****************************************************************************/
  689. static int cpia2_querymenu(struct file *file, void *fh, struct v4l2_querymenu *m)
  690. {
  691. struct camera_data *cam = video_drvdata(file);
  692. switch(m->id) {
  693. case CPIA2_CID_FLICKER_MODE:
  694. if (m->index >= NUM_FLICKER_CONTROLS)
  695. return -EINVAL;
  696. strcpy(m->name, flicker_controls[m->index].name);
  697. break;
  698. case CPIA2_CID_FRAMERATE:
  699. {
  700. int maximum = NUM_FRAMERATE_CONTROLS - 1;
  701. if(cam->params.pnp_id.device_type == DEVICE_STV_672 &&
  702. cam->params.version.sensor_flags==CPIA2_VP_SENSOR_FLAGS_500){
  703. // Maximum 15fps
  704. int i;
  705. for(i=0; i<maximum; ++i) {
  706. if(framerate_controls[i].value ==
  707. CPIA2_VP_FRAMERATE_15)
  708. maximum = i;
  709. }
  710. }
  711. if (m->index > maximum)
  712. return -EINVAL;
  713. strcpy(m->name, framerate_controls[m->index].name);
  714. break;
  715. }
  716. case CPIA2_CID_LIGHTS:
  717. if (m->index >= NUM_LIGHTS_CONTROLS)
  718. return -EINVAL;
  719. strcpy(m->name, lights_controls[m->index].name);
  720. break;
  721. default:
  722. return -EINVAL;
  723. }
  724. return 0;
  725. }
  726. /******************************************************************************
  727. *
  728. * ioctl_g_ctrl
  729. *
  730. * V4L2 get the value of a control variable
  731. *
  732. *****************************************************************************/
  733. static int cpia2_g_ctrl(struct file *file, void *fh, struct v4l2_control *c)
  734. {
  735. struct camera_data *cam = video_drvdata(file);
  736. switch(c->id) {
  737. case V4L2_CID_BRIGHTNESS:
  738. cpia2_do_command(cam, CPIA2_CMD_GET_VP_BRIGHTNESS,
  739. TRANSFER_READ, 0);
  740. c->value = cam->params.color_params.brightness;
  741. break;
  742. case V4L2_CID_CONTRAST:
  743. cpia2_do_command(cam, CPIA2_CMD_GET_CONTRAST,
  744. TRANSFER_READ, 0);
  745. c->value = cam->params.color_params.contrast;
  746. break;
  747. case V4L2_CID_SATURATION:
  748. cpia2_do_command(cam, CPIA2_CMD_GET_VP_SATURATION,
  749. TRANSFER_READ, 0);
  750. c->value = cam->params.color_params.saturation;
  751. break;
  752. case V4L2_CID_HFLIP:
  753. cpia2_do_command(cam, CPIA2_CMD_GET_USER_EFFECTS,
  754. TRANSFER_READ, 0);
  755. c->value = (cam->params.vp_params.user_effects &
  756. CPIA2_VP_USER_EFFECTS_MIRROR) != 0;
  757. break;
  758. case V4L2_CID_VFLIP:
  759. cpia2_do_command(cam, CPIA2_CMD_GET_USER_EFFECTS,
  760. TRANSFER_READ, 0);
  761. c->value = (cam->params.vp_params.user_effects &
  762. CPIA2_VP_USER_EFFECTS_FLIP) != 0;
  763. break;
  764. case CPIA2_CID_TARGET_KB:
  765. c->value = cam->params.vc_params.target_kb;
  766. break;
  767. case CPIA2_CID_GPIO:
  768. cpia2_do_command(cam, CPIA2_CMD_GET_VP_GPIO_DATA,
  769. TRANSFER_READ, 0);
  770. c->value = cam->params.vp_params.gpio_data;
  771. break;
  772. case CPIA2_CID_FLICKER_MODE:
  773. {
  774. int i, mode;
  775. cpia2_do_command(cam, CPIA2_CMD_GET_FLICKER_MODES,
  776. TRANSFER_READ, 0);
  777. if(cam->params.flicker_control.cam_register &
  778. CPIA2_VP_FLICKER_MODES_NEVER_FLICKER) {
  779. mode = NEVER_FLICKER;
  780. } else {
  781. if(cam->params.flicker_control.cam_register &
  782. CPIA2_VP_FLICKER_MODES_50HZ) {
  783. mode = FLICKER_50;
  784. } else {
  785. mode = FLICKER_60;
  786. }
  787. }
  788. for(i=0; i<NUM_FLICKER_CONTROLS; i++) {
  789. if(flicker_controls[i].value == mode) {
  790. c->value = i;
  791. break;
  792. }
  793. }
  794. if(i == NUM_FLICKER_CONTROLS)
  795. return -EINVAL;
  796. break;
  797. }
  798. case CPIA2_CID_FRAMERATE:
  799. {
  800. int maximum = NUM_FRAMERATE_CONTROLS - 1;
  801. int i;
  802. for(i=0; i<= maximum; i++) {
  803. if(cam->params.vp_params.frame_rate ==
  804. framerate_controls[i].value)
  805. break;
  806. }
  807. if(i > maximum)
  808. return -EINVAL;
  809. c->value = i;
  810. break;
  811. }
  812. case CPIA2_CID_USB_ALT:
  813. c->value = cam->params.camera_state.stream_mode;
  814. break;
  815. case CPIA2_CID_LIGHTS:
  816. {
  817. int i;
  818. cpia2_do_command(cam, CPIA2_CMD_GET_VP_GPIO_DATA,
  819. TRANSFER_READ, 0);
  820. for(i=0; i<NUM_LIGHTS_CONTROLS; i++) {
  821. if((cam->params.vp_params.gpio_data&GPIO_LIGHTS_MASK) ==
  822. lights_controls[i].value) {
  823. break;
  824. }
  825. }
  826. if(i == NUM_LIGHTS_CONTROLS)
  827. return -EINVAL;
  828. c->value = i;
  829. break;
  830. }
  831. case CPIA2_CID_RESET_CAMERA:
  832. return -EINVAL;
  833. default:
  834. return -EINVAL;
  835. }
  836. DBG("Get control id:%d, value:%d\n", c->id, c->value);
  837. return 0;
  838. }
  839. /******************************************************************************
  840. *
  841. * ioctl_s_ctrl
  842. *
  843. * V4L2 set the value of a control variable
  844. *
  845. *****************************************************************************/
  846. static int cpia2_s_ctrl(struct file *file, void *fh, struct v4l2_control *c)
  847. {
  848. struct camera_data *cam = video_drvdata(file);
  849. int i;
  850. int retval = 0;
  851. DBG("Set control id:%d, value:%d\n", c->id, c->value);
  852. /* Check that the value is in range */
  853. for(i=0; i<NUM_CONTROLS; i++) {
  854. if(c->id == controls[i].id) {
  855. if(c->value < controls[i].minimum ||
  856. c->value > controls[i].maximum) {
  857. return -EINVAL;
  858. }
  859. break;
  860. }
  861. }
  862. if(i == NUM_CONTROLS)
  863. return -EINVAL;
  864. switch(c->id) {
  865. case V4L2_CID_BRIGHTNESS:
  866. cpia2_set_brightness(cam, c->value);
  867. break;
  868. case V4L2_CID_CONTRAST:
  869. cpia2_set_contrast(cam, c->value);
  870. break;
  871. case V4L2_CID_SATURATION:
  872. cpia2_set_saturation(cam, c->value);
  873. break;
  874. case V4L2_CID_HFLIP:
  875. cpia2_set_property_mirror(cam, c->value);
  876. break;
  877. case V4L2_CID_VFLIP:
  878. cpia2_set_property_flip(cam, c->value);
  879. break;
  880. case CPIA2_CID_TARGET_KB:
  881. retval = cpia2_set_target_kb(cam, c->value);
  882. break;
  883. case CPIA2_CID_GPIO:
  884. retval = cpia2_set_gpio(cam, c->value);
  885. break;
  886. case CPIA2_CID_FLICKER_MODE:
  887. retval = cpia2_set_flicker_mode(cam,
  888. flicker_controls[c->value].value);
  889. break;
  890. case CPIA2_CID_FRAMERATE:
  891. retval = cpia2_set_fps(cam, framerate_controls[c->value].value);
  892. break;
  893. case CPIA2_CID_USB_ALT:
  894. retval = cpia2_usb_change_streaming_alternate(cam, c->value);
  895. break;
  896. case CPIA2_CID_LIGHTS:
  897. retval = cpia2_set_gpio(cam, lights_controls[c->value].value);
  898. break;
  899. case CPIA2_CID_RESET_CAMERA:
  900. cpia2_usb_stream_pause(cam);
  901. cpia2_reset_camera(cam);
  902. cpia2_usb_stream_resume(cam);
  903. break;
  904. default:
  905. retval = -EINVAL;
  906. }
  907. return retval;
  908. }
  909. /******************************************************************************
  910. *
  911. * ioctl_g_jpegcomp
  912. *
  913. * V4L2 get the JPEG compression parameters
  914. *
  915. *****************************************************************************/
  916. static int cpia2_g_jpegcomp(struct file *file, void *fh, struct v4l2_jpegcompression *parms)
  917. {
  918. struct camera_data *cam = video_drvdata(file);
  919. memset(parms, 0, sizeof(*parms));
  920. parms->quality = 80; // TODO: Can this be made meaningful?
  921. parms->jpeg_markers = V4L2_JPEG_MARKER_DQT | V4L2_JPEG_MARKER_DRI;
  922. if(!cam->params.compression.inhibit_htables) {
  923. parms->jpeg_markers |= V4L2_JPEG_MARKER_DHT;
  924. }
  925. parms->APPn = cam->APPn;
  926. parms->APP_len = cam->APP_len;
  927. if(cam->APP_len > 0) {
  928. memcpy(parms->APP_data, cam->APP_data, cam->APP_len);
  929. parms->jpeg_markers |= V4L2_JPEG_MARKER_APP;
  930. }
  931. parms->COM_len = cam->COM_len;
  932. if(cam->COM_len > 0) {
  933. memcpy(parms->COM_data, cam->COM_data, cam->COM_len);
  934. parms->jpeg_markers |= JPEG_MARKER_COM;
  935. }
  936. DBG("G_JPEGCOMP APP_len:%d COM_len:%d\n",
  937. parms->APP_len, parms->COM_len);
  938. return 0;
  939. }
  940. /******************************************************************************
  941. *
  942. * ioctl_s_jpegcomp
  943. *
  944. * V4L2 set the JPEG compression parameters
  945. * NOTE: quality and some jpeg_markers are ignored.
  946. *
  947. *****************************************************************************/
  948. static int cpia2_s_jpegcomp(struct file *file, void *fh, struct v4l2_jpegcompression *parms)
  949. {
  950. struct camera_data *cam = video_drvdata(file);
  951. DBG("S_JPEGCOMP APP_len:%d COM_len:%d\n",
  952. parms->APP_len, parms->COM_len);
  953. cam->params.compression.inhibit_htables =
  954. !(parms->jpeg_markers & V4L2_JPEG_MARKER_DHT);
  955. if(parms->APP_len != 0) {
  956. if(parms->APP_len > 0 &&
  957. parms->APP_len <= sizeof(cam->APP_data) &&
  958. parms->APPn >= 0 && parms->APPn <= 15) {
  959. cam->APPn = parms->APPn;
  960. cam->APP_len = parms->APP_len;
  961. memcpy(cam->APP_data, parms->APP_data, parms->APP_len);
  962. } else {
  963. LOG("Bad APPn Params n=%d len=%d\n",
  964. parms->APPn, parms->APP_len);
  965. return -EINVAL;
  966. }
  967. } else {
  968. cam->APP_len = 0;
  969. }
  970. if(parms->COM_len != 0) {
  971. if(parms->COM_len > 0 &&
  972. parms->COM_len <= sizeof(cam->COM_data)) {
  973. cam->COM_len = parms->COM_len;
  974. memcpy(cam->COM_data, parms->COM_data, parms->COM_len);
  975. } else {
  976. LOG("Bad COM_len=%d\n", parms->COM_len);
  977. return -EINVAL;
  978. }
  979. }
  980. return 0;
  981. }
  982. /******************************************************************************
  983. *
  984. * ioctl_reqbufs
  985. *
  986. * V4L2 Initiate memory mapping.
  987. * NOTE: The user's request is ignored. For now the buffers are fixed.
  988. *
  989. *****************************************************************************/
  990. static int cpia2_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *req)
  991. {
  992. struct camera_data *cam = video_drvdata(file);
  993. if(req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  994. req->memory != V4L2_MEMORY_MMAP)
  995. return -EINVAL;
  996. DBG("REQBUFS requested:%d returning:%d\n", req->count, cam->num_frames);
  997. req->count = cam->num_frames;
  998. memset(&req->reserved, 0, sizeof(req->reserved));
  999. return 0;
  1000. }
  1001. /******************************************************************************
  1002. *
  1003. * ioctl_querybuf
  1004. *
  1005. * V4L2 Query memory buffer status.
  1006. *
  1007. *****************************************************************************/
  1008. static int cpia2_querybuf(struct file *file, void *fh, struct v4l2_buffer *buf)
  1009. {
  1010. struct camera_data *cam = video_drvdata(file);
  1011. if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  1012. buf->index > cam->num_frames)
  1013. return -EINVAL;
  1014. buf->m.offset = cam->buffers[buf->index].data - cam->frame_buffer;
  1015. buf->length = cam->frame_size;
  1016. buf->memory = V4L2_MEMORY_MMAP;
  1017. if(cam->mmapped)
  1018. buf->flags = V4L2_BUF_FLAG_MAPPED;
  1019. else
  1020. buf->flags = 0;
  1021. switch (cam->buffers[buf->index].status) {
  1022. case FRAME_EMPTY:
  1023. case FRAME_ERROR:
  1024. case FRAME_READING:
  1025. buf->bytesused = 0;
  1026. buf->flags = V4L2_BUF_FLAG_QUEUED;
  1027. break;
  1028. case FRAME_READY:
  1029. buf->bytesused = cam->buffers[buf->index].length;
  1030. buf->timestamp = cam->buffers[buf->index].timestamp;
  1031. buf->sequence = cam->buffers[buf->index].seq;
  1032. buf->flags = V4L2_BUF_FLAG_DONE;
  1033. break;
  1034. }
  1035. DBG("QUERYBUF index:%d offset:%d flags:%d seq:%d bytesused:%d\n",
  1036. buf->index, buf->m.offset, buf->flags, buf->sequence,
  1037. buf->bytesused);
  1038. return 0;
  1039. }
  1040. /******************************************************************************
  1041. *
  1042. * ioctl_qbuf
  1043. *
  1044. * V4L2 User is freeing buffer
  1045. *
  1046. *****************************************************************************/
  1047. static int cpia2_qbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
  1048. {
  1049. struct camera_data *cam = video_drvdata(file);
  1050. if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  1051. buf->memory != V4L2_MEMORY_MMAP ||
  1052. buf->index > cam->num_frames)
  1053. return -EINVAL;
  1054. DBG("QBUF #%d\n", buf->index);
  1055. if(cam->buffers[buf->index].status == FRAME_READY)
  1056. cam->buffers[buf->index].status = FRAME_EMPTY;
  1057. return 0;
  1058. }
  1059. /******************************************************************************
  1060. *
  1061. * find_earliest_filled_buffer
  1062. *
  1063. * Helper for ioctl_dqbuf. Find the next ready buffer.
  1064. *
  1065. *****************************************************************************/
  1066. static int find_earliest_filled_buffer(struct camera_data *cam)
  1067. {
  1068. int i;
  1069. int found = -1;
  1070. for (i=0; i<cam->num_frames; i++) {
  1071. if(cam->buffers[i].status == FRAME_READY) {
  1072. if(found < 0) {
  1073. found = i;
  1074. } else {
  1075. /* find which buffer is earlier */
  1076. struct timeval *tv1, *tv2;
  1077. tv1 = &cam->buffers[i].timestamp;
  1078. tv2 = &cam->buffers[found].timestamp;
  1079. if(tv1->tv_sec < tv2->tv_sec ||
  1080. (tv1->tv_sec == tv2->tv_sec &&
  1081. tv1->tv_usec < tv2->tv_usec))
  1082. found = i;
  1083. }
  1084. }
  1085. }
  1086. return found;
  1087. }
  1088. /******************************************************************************
  1089. *
  1090. * ioctl_dqbuf
  1091. *
  1092. * V4L2 User is asking for a filled buffer.
  1093. *
  1094. *****************************************************************************/
  1095. static int cpia2_dqbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
  1096. {
  1097. struct camera_data *cam = video_drvdata(file);
  1098. int frame;
  1099. if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  1100. buf->memory != V4L2_MEMORY_MMAP)
  1101. return -EINVAL;
  1102. frame = find_earliest_filled_buffer(cam);
  1103. if(frame < 0 && file->f_flags&O_NONBLOCK)
  1104. return -EAGAIN;
  1105. if(frame < 0) {
  1106. /* Wait for a frame to become available */
  1107. struct framebuf *cb=cam->curbuff;
  1108. mutex_unlock(&cam->v4l2_lock);
  1109. wait_event_interruptible(cam->wq_stream,
  1110. !cam->present ||
  1111. (cb=cam->curbuff)->status == FRAME_READY);
  1112. mutex_lock(&cam->v4l2_lock);
  1113. if (signal_pending(current))
  1114. return -ERESTARTSYS;
  1115. if(!cam->present)
  1116. return -ENOTTY;
  1117. frame = cb->num;
  1118. }
  1119. buf->index = frame;
  1120. buf->bytesused = cam->buffers[buf->index].length;
  1121. buf->flags = V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_DONE;
  1122. buf->field = V4L2_FIELD_NONE;
  1123. buf->timestamp = cam->buffers[buf->index].timestamp;
  1124. buf->sequence = cam->buffers[buf->index].seq;
  1125. buf->m.offset = cam->buffers[buf->index].data - cam->frame_buffer;
  1126. buf->length = cam->frame_size;
  1127. buf->input = 0;
  1128. buf->reserved = 0;
  1129. memset(&buf->timecode, 0, sizeof(buf->timecode));
  1130. DBG("DQBUF #%d status:%d seq:%d length:%d\n", buf->index,
  1131. cam->buffers[buf->index].status, buf->sequence, buf->bytesused);
  1132. return 0;
  1133. }
  1134. static int cpia2_g_priority(struct file *file, void *_fh, enum v4l2_priority *p)
  1135. {
  1136. struct cpia2_fh *fh = _fh;
  1137. *p = fh->prio;
  1138. return 0;
  1139. }
  1140. static int cpia2_s_priority(struct file *file, void *_fh, enum v4l2_priority prio)
  1141. {
  1142. struct camera_data *cam = video_drvdata(file);
  1143. struct cpia2_fh *fh = fh;
  1144. if (cam->streaming && prio != fh->prio &&
  1145. fh->prio == V4L2_PRIORITY_RECORD)
  1146. /* Can't drop record priority while streaming */
  1147. return -EBUSY;
  1148. if (prio == V4L2_PRIORITY_RECORD && prio != fh->prio &&
  1149. v4l2_prio_max(&cam->prio) == V4L2_PRIORITY_RECORD)
  1150. /* Only one program can record at a time */
  1151. return -EBUSY;
  1152. return v4l2_prio_change(&cam->prio, &fh->prio, prio);
  1153. }
  1154. static int cpia2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
  1155. {
  1156. struct camera_data *cam = video_drvdata(file);
  1157. DBG("VIDIOC_STREAMON, streaming=%d\n", cam->streaming);
  1158. if (!cam->mmapped || type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1159. return -EINVAL;
  1160. if (!cam->streaming)
  1161. return cpia2_usb_stream_start(cam,
  1162. cam->params.camera_state.stream_mode);
  1163. return -EINVAL;
  1164. }
  1165. static int cpia2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
  1166. {
  1167. struct camera_data *cam = video_drvdata(file);
  1168. DBG("VIDIOC_STREAMOFF, streaming=%d\n", cam->streaming);
  1169. if (!cam->mmapped || type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1170. return -EINVAL;
  1171. if (cam->streaming)
  1172. return cpia2_usb_stream_stop(cam);
  1173. return -EINVAL;
  1174. }
  1175. /******************************************************************************
  1176. *
  1177. * cpia2_mmap
  1178. *
  1179. *****************************************************************************/
  1180. static int cpia2_mmap(struct file *file, struct vm_area_struct *area)
  1181. {
  1182. struct camera_data *cam = video_drvdata(file);
  1183. int retval;
  1184. /* Priority check */
  1185. struct cpia2_fh *fh = file->private_data;
  1186. if(fh->prio != V4L2_PRIORITY_RECORD) {
  1187. return -EBUSY;
  1188. }
  1189. retval = cpia2_remap_buffer(cam, area);
  1190. if(!retval)
  1191. fh->mmapped = 1;
  1192. return retval;
  1193. }
  1194. /******************************************************************************
  1195. *
  1196. * reset_camera_struct_v4l
  1197. *
  1198. * Sets all values to the defaults
  1199. *****************************************************************************/
  1200. static void reset_camera_struct_v4l(struct camera_data *cam)
  1201. {
  1202. cam->width = cam->params.roi.width;
  1203. cam->height = cam->params.roi.height;
  1204. cam->frame_size = buffer_size;
  1205. cam->num_frames = num_buffers;
  1206. /* FlickerModes */
  1207. cam->params.flicker_control.flicker_mode_req = flicker_mode;
  1208. cam->params.flicker_control.mains_frequency = flicker_freq;
  1209. /* streamMode */
  1210. cam->params.camera_state.stream_mode = alternate;
  1211. cam->pixelformat = V4L2_PIX_FMT_JPEG;
  1212. v4l2_prio_init(&cam->prio);
  1213. }
  1214. static const struct v4l2_ioctl_ops cpia2_ioctl_ops = {
  1215. .vidioc_querycap = cpia2_querycap,
  1216. .vidioc_enum_input = cpia2_enum_input,
  1217. .vidioc_g_input = cpia2_g_input,
  1218. .vidioc_s_input = cpia2_s_input,
  1219. .vidioc_enum_fmt_vid_cap = cpia2_enum_fmt_vid_cap,
  1220. .vidioc_g_fmt_vid_cap = cpia2_g_fmt_vid_cap,
  1221. .vidioc_s_fmt_vid_cap = cpia2_s_fmt_vid_cap,
  1222. .vidioc_try_fmt_vid_cap = cpia2_try_fmt_vid_cap,
  1223. .vidioc_queryctrl = cpia2_queryctrl,
  1224. .vidioc_querymenu = cpia2_querymenu,
  1225. .vidioc_g_ctrl = cpia2_g_ctrl,
  1226. .vidioc_s_ctrl = cpia2_s_ctrl,
  1227. .vidioc_g_jpegcomp = cpia2_g_jpegcomp,
  1228. .vidioc_s_jpegcomp = cpia2_s_jpegcomp,
  1229. .vidioc_cropcap = cpia2_cropcap,
  1230. .vidioc_reqbufs = cpia2_reqbufs,
  1231. .vidioc_querybuf = cpia2_querybuf,
  1232. .vidioc_qbuf = cpia2_qbuf,
  1233. .vidioc_dqbuf = cpia2_dqbuf,
  1234. .vidioc_streamon = cpia2_streamon,
  1235. .vidioc_streamoff = cpia2_streamoff,
  1236. .vidioc_g_priority = cpia2_g_priority,
  1237. .vidioc_s_priority = cpia2_s_priority,
  1238. .vidioc_default = cpia2_default,
  1239. };
  1240. /***
  1241. * The v4l video device structure initialized for this device
  1242. ***/
  1243. static const struct v4l2_file_operations cpia2_fops = {
  1244. .owner = THIS_MODULE,
  1245. .open = cpia2_open,
  1246. .release = cpia2_close,
  1247. .read = cpia2_v4l_read,
  1248. .poll = cpia2_v4l_poll,
  1249. .unlocked_ioctl = video_ioctl2,
  1250. .mmap = cpia2_mmap,
  1251. };
  1252. static struct video_device cpia2_template = {
  1253. /* I could not find any place for the old .initialize initializer?? */
  1254. .name = "CPiA2 Camera",
  1255. .fops = &cpia2_fops,
  1256. .ioctl_ops = &cpia2_ioctl_ops,
  1257. .release = video_device_release,
  1258. };
  1259. /******************************************************************************
  1260. *
  1261. * cpia2_register_camera
  1262. *
  1263. *****************************************************************************/
  1264. int cpia2_register_camera(struct camera_data *cam)
  1265. {
  1266. cam->vdev = video_device_alloc();
  1267. if(!cam->vdev)
  1268. return -ENOMEM;
  1269. memcpy(cam->vdev, &cpia2_template, sizeof(cpia2_template));
  1270. video_set_drvdata(cam->vdev, cam);
  1271. cam->vdev->lock = &cam->v4l2_lock;
  1272. reset_camera_struct_v4l(cam);
  1273. /* register v4l device */
  1274. if (video_register_device(cam->vdev, VFL_TYPE_GRABBER, video_nr) < 0) {
  1275. ERR("video_register_device failed\n");
  1276. video_device_release(cam->vdev);
  1277. return -ENODEV;
  1278. }
  1279. return 0;
  1280. }
  1281. /******************************************************************************
  1282. *
  1283. * cpia2_unregister_camera
  1284. *
  1285. *****************************************************************************/
  1286. void cpia2_unregister_camera(struct camera_data *cam)
  1287. {
  1288. if (!cam->open_count) {
  1289. video_unregister_device(cam->vdev);
  1290. } else {
  1291. LOG("%s removed while open, deferring "
  1292. "video_unregister_device\n",
  1293. video_device_node_name(cam->vdev));
  1294. }
  1295. }
  1296. /******************************************************************************
  1297. *
  1298. * check_parameters
  1299. *
  1300. * Make sure that all user-supplied parameters are sensible
  1301. *****************************************************************************/
  1302. static void __init check_parameters(void)
  1303. {
  1304. if(buffer_size < PAGE_SIZE) {
  1305. buffer_size = PAGE_SIZE;
  1306. LOG("buffer_size too small, setting to %d\n", buffer_size);
  1307. } else if(buffer_size > 1024*1024) {
  1308. /* arbitrary upper limiit */
  1309. buffer_size = 1024*1024;
  1310. LOG("buffer_size ridiculously large, setting to %d\n",
  1311. buffer_size);
  1312. } else {
  1313. buffer_size += PAGE_SIZE-1;
  1314. buffer_size &= ~(PAGE_SIZE-1);
  1315. }
  1316. if(num_buffers < 1) {
  1317. num_buffers = 1;
  1318. LOG("num_buffers too small, setting to %d\n", num_buffers);
  1319. } else if(num_buffers > VIDEO_MAX_FRAME) {
  1320. num_buffers = VIDEO_MAX_FRAME;
  1321. LOG("num_buffers too large, setting to %d\n", num_buffers);
  1322. }
  1323. if(alternate < USBIF_ISO_1 || alternate > USBIF_ISO_6) {
  1324. alternate = DEFAULT_ALT;
  1325. LOG("alternate specified is invalid, using %d\n", alternate);
  1326. }
  1327. if (flicker_mode != NEVER_FLICKER && flicker_mode != ANTI_FLICKER_ON) {
  1328. flicker_mode = NEVER_FLICKER;
  1329. LOG("Flicker mode specified is invalid, using %d\n",
  1330. flicker_mode);
  1331. }
  1332. if (flicker_freq != FLICKER_50 && flicker_freq != FLICKER_60) {
  1333. flicker_freq = FLICKER_60;
  1334. LOG("Flicker mode specified is invalid, using %d\n",
  1335. flicker_freq);
  1336. }
  1337. if(video_nr < -1 || video_nr > 64) {
  1338. video_nr = -1;
  1339. LOG("invalid video_nr specified, must be -1 to 64\n");
  1340. }
  1341. DBG("Using %d buffers, each %d bytes, alternate=%d\n",
  1342. num_buffers, buffer_size, alternate);
  1343. }
  1344. /************ Module Stuff ***************/
  1345. /******************************************************************************
  1346. *
  1347. * cpia2_init/module_init
  1348. *
  1349. *****************************************************************************/
  1350. static int __init cpia2_init(void)
  1351. {
  1352. LOG("%s v%d.%d.%d\n",
  1353. ABOUT, CPIA2_MAJ_VER, CPIA2_MIN_VER, CPIA2_PATCH_VER);
  1354. check_parameters();
  1355. cpia2_usb_init();
  1356. return 0;
  1357. }
  1358. /******************************************************************************
  1359. *
  1360. * cpia2_exit/module_exit
  1361. *
  1362. *****************************************************************************/
  1363. static void __exit cpia2_exit(void)
  1364. {
  1365. cpia2_usb_cleanup();
  1366. schedule_timeout(2 * HZ);
  1367. }
  1368. module_init(cpia2_init);
  1369. module_exit(cpia2_exit);