cpia2_v4l.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776
  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. int retval = 0;
  223. if (!cam) {
  224. ERR("Internal error, camera_data not found!\n");
  225. return -ENODEV;
  226. }
  227. if(mutex_lock_interruptible(&cam->busy_lock))
  228. return -ERESTARTSYS;
  229. if(!cam->present) {
  230. retval = -ENODEV;
  231. goto err_return;
  232. }
  233. if (cam->open_count > 0) {
  234. goto skip_init;
  235. }
  236. if (cpia2_allocate_buffers(cam)) {
  237. retval = -ENOMEM;
  238. goto err_return;
  239. }
  240. /* reset the camera */
  241. if (cpia2_reset_camera(cam) < 0) {
  242. retval = -EIO;
  243. goto err_return;
  244. }
  245. cam->APP_len = 0;
  246. cam->COM_len = 0;
  247. skip_init:
  248. {
  249. struct cpia2_fh *fh = kmalloc(sizeof(*fh),GFP_KERNEL);
  250. if(!fh) {
  251. retval = -ENOMEM;
  252. goto err_return;
  253. }
  254. file->private_data = fh;
  255. fh->prio = V4L2_PRIORITY_UNSET;
  256. v4l2_prio_open(&cam->prio, &fh->prio);
  257. fh->mmapped = 0;
  258. }
  259. ++cam->open_count;
  260. cpia2_dbg_dump_registers(cam);
  261. err_return:
  262. mutex_unlock(&cam->busy_lock);
  263. return retval;
  264. }
  265. /******************************************************************************
  266. *
  267. * cpia2_close
  268. *
  269. *****************************************************************************/
  270. static int cpia2_close(struct file *file)
  271. {
  272. struct video_device *dev = video_devdata(file);
  273. struct camera_data *cam = video_get_drvdata(dev);
  274. struct cpia2_fh *fh = file->private_data;
  275. mutex_lock(&cam->busy_lock);
  276. if (cam->present &&
  277. (cam->open_count == 1
  278. || fh->prio == V4L2_PRIORITY_RECORD
  279. )) {
  280. cpia2_usb_stream_stop(cam);
  281. if(cam->open_count == 1) {
  282. /* save camera state for later open */
  283. cpia2_save_camera_state(cam);
  284. cpia2_set_low_power(cam);
  285. cpia2_free_buffers(cam);
  286. }
  287. }
  288. {
  289. if(fh->mmapped)
  290. cam->mmapped = 0;
  291. v4l2_prio_close(&cam->prio, fh->prio);
  292. file->private_data = NULL;
  293. kfree(fh);
  294. }
  295. if (--cam->open_count == 0) {
  296. cpia2_free_buffers(cam);
  297. if (!cam->present) {
  298. video_unregister_device(dev);
  299. mutex_unlock(&cam->busy_lock);
  300. kfree(cam);
  301. return 0;
  302. }
  303. }
  304. mutex_unlock(&cam->busy_lock);
  305. return 0;
  306. }
  307. /******************************************************************************
  308. *
  309. * cpia2_v4l_read
  310. *
  311. *****************************************************************************/
  312. static ssize_t cpia2_v4l_read(struct file *file, char __user *buf, size_t count,
  313. loff_t *off)
  314. {
  315. struct camera_data *cam = video_drvdata(file);
  316. int noblock = file->f_flags&O_NONBLOCK;
  317. struct cpia2_fh *fh = file->private_data;
  318. if(!cam)
  319. return -EINVAL;
  320. /* Priority check */
  321. if(fh->prio != V4L2_PRIORITY_RECORD) {
  322. return -EBUSY;
  323. }
  324. return cpia2_read(cam, buf, count, noblock);
  325. }
  326. /******************************************************************************
  327. *
  328. * cpia2_v4l_poll
  329. *
  330. *****************************************************************************/
  331. static unsigned int cpia2_v4l_poll(struct file *filp, struct poll_table_struct *wait)
  332. {
  333. struct camera_data *cam = video_drvdata(filp);
  334. struct cpia2_fh *fh = filp->private_data;
  335. if(!cam)
  336. return POLLERR;
  337. /* Priority check */
  338. if(fh->prio != V4L2_PRIORITY_RECORD) {
  339. return POLLERR;
  340. }
  341. return cpia2_poll(cam, filp, wait);
  342. }
  343. static int sync(struct camera_data *cam, int frame_nr)
  344. {
  345. struct framebuf *frame = &cam->buffers[frame_nr];
  346. while (1) {
  347. if (frame->status == FRAME_READY)
  348. return 0;
  349. if (!cam->streaming) {
  350. frame->status = FRAME_READY;
  351. frame->length = 0;
  352. return 0;
  353. }
  354. mutex_unlock(&cam->busy_lock);
  355. wait_event_interruptible(cam->wq_stream,
  356. !cam->streaming ||
  357. frame->status == FRAME_READY);
  358. mutex_lock(&cam->busy_lock);
  359. if (signal_pending(current))
  360. return -ERESTARTSYS;
  361. if(!cam->present)
  362. return -ENOTTY;
  363. }
  364. }
  365. /******************************************************************************
  366. *
  367. * ioctl_get_mbuf
  368. *
  369. *****************************************************************************/
  370. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  371. static int ioctl_get_mbuf(void *arg, struct camera_data *cam)
  372. {
  373. struct video_mbuf *vm;
  374. int i;
  375. vm = arg;
  376. memset(vm, 0, sizeof(*vm));
  377. vm->size = cam->frame_size*cam->num_frames;
  378. vm->frames = cam->num_frames;
  379. for (i = 0; i < cam->num_frames; i++)
  380. vm->offsets[i] = cam->frame_size * i;
  381. return 0;
  382. }
  383. #endif
  384. /******************************************************************************
  385. *
  386. * ioctl_set_gpio
  387. *
  388. *****************************************************************************/
  389. static int ioctl_set_gpio(void *arg, struct camera_data *cam)
  390. {
  391. __u32 gpio_val;
  392. gpio_val = *(__u32*) arg;
  393. if (gpio_val &~ 0xFFU)
  394. return -EINVAL;
  395. return cpia2_set_gpio(cam, (unsigned char)gpio_val);
  396. }
  397. /******************************************************************************
  398. *
  399. * ioctl_querycap
  400. *
  401. * V4L2 device capabilities
  402. *
  403. *****************************************************************************/
  404. static int ioctl_querycap(void *arg, struct camera_data *cam)
  405. {
  406. struct v4l2_capability *vc = arg;
  407. memset(vc, 0, sizeof(*vc));
  408. strcpy(vc->driver, "cpia2");
  409. if (cam->params.pnp_id.product == 0x151)
  410. strcpy(vc->card, "QX5 Microscope");
  411. else
  412. strcpy(vc->card, "CPiA2 Camera");
  413. switch (cam->params.pnp_id.device_type) {
  414. case DEVICE_STV_672:
  415. strcat(vc->card, " (672/");
  416. break;
  417. case DEVICE_STV_676:
  418. strcat(vc->card, " (676/");
  419. break;
  420. default:
  421. strcat(vc->card, " (???/");
  422. break;
  423. }
  424. switch (cam->params.version.sensor_flags) {
  425. case CPIA2_VP_SENSOR_FLAGS_404:
  426. strcat(vc->card, "404)");
  427. break;
  428. case CPIA2_VP_SENSOR_FLAGS_407:
  429. strcat(vc->card, "407)");
  430. break;
  431. case CPIA2_VP_SENSOR_FLAGS_409:
  432. strcat(vc->card, "409)");
  433. break;
  434. case CPIA2_VP_SENSOR_FLAGS_410:
  435. strcat(vc->card, "410)");
  436. break;
  437. case CPIA2_VP_SENSOR_FLAGS_500:
  438. strcat(vc->card, "500)");
  439. break;
  440. default:
  441. strcat(vc->card, "???)");
  442. break;
  443. }
  444. if (usb_make_path(cam->dev, vc->bus_info, sizeof(vc->bus_info)) <0)
  445. memset(vc->bus_info,0, sizeof(vc->bus_info));
  446. vc->version = KERNEL_VERSION(CPIA2_MAJ_VER, CPIA2_MIN_VER,
  447. CPIA2_PATCH_VER);
  448. vc->capabilities = V4L2_CAP_VIDEO_CAPTURE |
  449. V4L2_CAP_READWRITE |
  450. V4L2_CAP_STREAMING;
  451. return 0;
  452. }
  453. /******************************************************************************
  454. *
  455. * ioctl_input
  456. *
  457. * V4L2 input get/set/enumerate
  458. *
  459. *****************************************************************************/
  460. static int ioctl_input(unsigned int ioclt_nr,void *arg,struct camera_data *cam)
  461. {
  462. struct v4l2_input *i = arg;
  463. if(ioclt_nr != VIDIOC_G_INPUT) {
  464. if (i->index != 0)
  465. return -EINVAL;
  466. }
  467. memset(i, 0, sizeof(*i));
  468. strcpy(i->name, "Camera");
  469. i->type = V4L2_INPUT_TYPE_CAMERA;
  470. return 0;
  471. }
  472. /******************************************************************************
  473. *
  474. * ioctl_enum_fmt
  475. *
  476. * V4L2 format enumerate
  477. *
  478. *****************************************************************************/
  479. static int ioctl_enum_fmt(void *arg,struct camera_data *cam)
  480. {
  481. struct v4l2_fmtdesc *f = arg;
  482. int index = f->index;
  483. if (index < 0 || index > 1)
  484. return -EINVAL;
  485. memset(f, 0, sizeof(*f));
  486. f->index = index;
  487. f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  488. f->flags = V4L2_FMT_FLAG_COMPRESSED;
  489. switch(index) {
  490. case 0:
  491. strcpy(f->description, "MJPEG");
  492. f->pixelformat = V4L2_PIX_FMT_MJPEG;
  493. break;
  494. case 1:
  495. strcpy(f->description, "JPEG");
  496. f->pixelformat = V4L2_PIX_FMT_JPEG;
  497. break;
  498. default:
  499. return -EINVAL;
  500. }
  501. return 0;
  502. }
  503. /******************************************************************************
  504. *
  505. * ioctl_try_fmt
  506. *
  507. * V4L2 format try
  508. *
  509. *****************************************************************************/
  510. static int ioctl_try_fmt(void *arg,struct camera_data *cam)
  511. {
  512. struct v4l2_format *f = arg;
  513. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  514. return -EINVAL;
  515. if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG &&
  516. f->fmt.pix.pixelformat != V4L2_PIX_FMT_JPEG)
  517. return -EINVAL;
  518. f->fmt.pix.field = V4L2_FIELD_NONE;
  519. f->fmt.pix.bytesperline = 0;
  520. f->fmt.pix.sizeimage = cam->frame_size;
  521. f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
  522. f->fmt.pix.priv = 0;
  523. switch (cpia2_match_video_size(f->fmt.pix.width, f->fmt.pix.height)) {
  524. case VIDEOSIZE_VGA:
  525. f->fmt.pix.width = 640;
  526. f->fmt.pix.height = 480;
  527. break;
  528. case VIDEOSIZE_CIF:
  529. f->fmt.pix.width = 352;
  530. f->fmt.pix.height = 288;
  531. break;
  532. case VIDEOSIZE_QVGA:
  533. f->fmt.pix.width = 320;
  534. f->fmt.pix.height = 240;
  535. break;
  536. case VIDEOSIZE_288_216:
  537. f->fmt.pix.width = 288;
  538. f->fmt.pix.height = 216;
  539. break;
  540. case VIDEOSIZE_256_192:
  541. f->fmt.pix.width = 256;
  542. f->fmt.pix.height = 192;
  543. break;
  544. case VIDEOSIZE_224_168:
  545. f->fmt.pix.width = 224;
  546. f->fmt.pix.height = 168;
  547. break;
  548. case VIDEOSIZE_192_144:
  549. f->fmt.pix.width = 192;
  550. f->fmt.pix.height = 144;
  551. break;
  552. case VIDEOSIZE_QCIF:
  553. default:
  554. f->fmt.pix.width = 176;
  555. f->fmt.pix.height = 144;
  556. break;
  557. }
  558. return 0;
  559. }
  560. /******************************************************************************
  561. *
  562. * ioctl_set_fmt
  563. *
  564. * V4L2 format set
  565. *
  566. *****************************************************************************/
  567. static int ioctl_set_fmt(void *arg,struct camera_data *cam, struct cpia2_fh *fh)
  568. {
  569. struct v4l2_format *f = arg;
  570. int err, frame;
  571. err = ioctl_try_fmt(arg, cam);
  572. if(err != 0)
  573. return err;
  574. /* Ensure that only this process can change the format. */
  575. err = v4l2_prio_change(&cam->prio, &fh->prio, V4L2_PRIORITY_RECORD);
  576. if(err != 0) {
  577. return err;
  578. }
  579. cam->pixelformat = f->fmt.pix.pixelformat;
  580. /* NOTE: This should be set to 1 for MJPEG, but some apps don't handle
  581. * the missing Huffman table properly. */
  582. cam->params.compression.inhibit_htables = 0;
  583. /*f->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG;*/
  584. /* we set the video window to something smaller or equal to what
  585. * is requested by the user???
  586. */
  587. DBG("Requested width = %d, height = %d\n",
  588. f->fmt.pix.width, f->fmt.pix.height);
  589. if (f->fmt.pix.width != cam->width ||
  590. f->fmt.pix.height != cam->height) {
  591. cam->width = f->fmt.pix.width;
  592. cam->height = f->fmt.pix.height;
  593. cam->params.roi.width = f->fmt.pix.width;
  594. cam->params.roi.height = f->fmt.pix.height;
  595. cpia2_set_format(cam);
  596. }
  597. for (frame = 0; frame < cam->num_frames; ++frame) {
  598. if (cam->buffers[frame].status == FRAME_READING)
  599. if ((err = sync(cam, frame)) < 0)
  600. return err;
  601. cam->buffers[frame].status = FRAME_EMPTY;
  602. }
  603. return 0;
  604. }
  605. /******************************************************************************
  606. *
  607. * ioctl_get_fmt
  608. *
  609. * V4L2 format get
  610. *
  611. *****************************************************************************/
  612. static int ioctl_get_fmt(void *arg,struct camera_data *cam)
  613. {
  614. struct v4l2_format *f = arg;
  615. if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  616. return -EINVAL;
  617. f->fmt.pix.width = cam->width;
  618. f->fmt.pix.height = cam->height;
  619. f->fmt.pix.pixelformat = cam->pixelformat;
  620. f->fmt.pix.field = V4L2_FIELD_NONE;
  621. f->fmt.pix.bytesperline = 0;
  622. f->fmt.pix.sizeimage = cam->frame_size;
  623. f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
  624. f->fmt.pix.priv = 0;
  625. return 0;
  626. }
  627. /******************************************************************************
  628. *
  629. * ioctl_cropcap
  630. *
  631. * V4L2 query cropping capabilities
  632. * NOTE: cropping is currently disabled
  633. *
  634. *****************************************************************************/
  635. static int ioctl_cropcap(void *arg,struct camera_data *cam)
  636. {
  637. struct v4l2_cropcap *c = arg;
  638. if (c->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  639. return -EINVAL;
  640. c->bounds.left = 0;
  641. c->bounds.top = 0;
  642. c->bounds.width = cam->width;
  643. c->bounds.height = cam->height;
  644. c->defrect.left = 0;
  645. c->defrect.top = 0;
  646. c->defrect.width = cam->width;
  647. c->defrect.height = cam->height;
  648. c->pixelaspect.numerator = 1;
  649. c->pixelaspect.denominator = 1;
  650. return 0;
  651. }
  652. /******************************************************************************
  653. *
  654. * ioctl_queryctrl
  655. *
  656. * V4L2 query possible control variables
  657. *
  658. *****************************************************************************/
  659. static int ioctl_queryctrl(void *arg,struct camera_data *cam)
  660. {
  661. struct v4l2_queryctrl *c = arg;
  662. int i;
  663. for(i=0; i<NUM_CONTROLS; ++i) {
  664. if(c->id == controls[i].id) {
  665. memcpy(c, controls+i, sizeof(*c));
  666. break;
  667. }
  668. }
  669. if(i == NUM_CONTROLS)
  670. return -EINVAL;
  671. /* Some devices have additional limitations */
  672. switch(c->id) {
  673. case V4L2_CID_BRIGHTNESS:
  674. /***
  675. * Don't let the register be set to zero - bug in VP4
  676. * flash of full brightness
  677. ***/
  678. if (cam->params.pnp_id.device_type == DEVICE_STV_672)
  679. c->minimum = 1;
  680. break;
  681. case V4L2_CID_VFLIP:
  682. // VP5 Only
  683. if(cam->params.pnp_id.device_type == DEVICE_STV_672)
  684. c->flags |= V4L2_CTRL_FLAG_DISABLED;
  685. break;
  686. case CPIA2_CID_FRAMERATE:
  687. if(cam->params.pnp_id.device_type == DEVICE_STV_672 &&
  688. cam->params.version.sensor_flags==CPIA2_VP_SENSOR_FLAGS_500){
  689. // Maximum 15fps
  690. for(i=0; i<c->maximum; ++i) {
  691. if(framerate_controls[i].value ==
  692. CPIA2_VP_FRAMERATE_15) {
  693. c->maximum = i;
  694. c->default_value = i;
  695. }
  696. }
  697. }
  698. break;
  699. case CPIA2_CID_FLICKER_MODE:
  700. // Flicker control only valid for 672.
  701. if(cam->params.pnp_id.device_type != DEVICE_STV_672)
  702. c->flags |= V4L2_CTRL_FLAG_DISABLED;
  703. break;
  704. case CPIA2_CID_LIGHTS:
  705. // Light control only valid for the QX5 Microscope.
  706. if(cam->params.pnp_id.product != 0x151)
  707. c->flags |= V4L2_CTRL_FLAG_DISABLED;
  708. break;
  709. default:
  710. break;
  711. }
  712. return 0;
  713. }
  714. /******************************************************************************
  715. *
  716. * ioctl_querymenu
  717. *
  718. * V4L2 query possible control variables
  719. *
  720. *****************************************************************************/
  721. static int ioctl_querymenu(void *arg,struct camera_data *cam)
  722. {
  723. struct v4l2_querymenu *m = arg;
  724. memset(m->name, 0, sizeof(m->name));
  725. m->reserved = 0;
  726. switch(m->id) {
  727. case CPIA2_CID_FLICKER_MODE:
  728. if (m->index >= NUM_FLICKER_CONTROLS)
  729. return -EINVAL;
  730. strcpy(m->name, flicker_controls[m->index].name);
  731. break;
  732. case CPIA2_CID_FRAMERATE:
  733. {
  734. int maximum = NUM_FRAMERATE_CONTROLS - 1;
  735. if(cam->params.pnp_id.device_type == DEVICE_STV_672 &&
  736. cam->params.version.sensor_flags==CPIA2_VP_SENSOR_FLAGS_500){
  737. // Maximum 15fps
  738. int i;
  739. for(i=0; i<maximum; ++i) {
  740. if(framerate_controls[i].value ==
  741. CPIA2_VP_FRAMERATE_15)
  742. maximum = i;
  743. }
  744. }
  745. if (m->index > maximum)
  746. return -EINVAL;
  747. strcpy(m->name, framerate_controls[m->index].name);
  748. break;
  749. }
  750. case CPIA2_CID_LIGHTS:
  751. if (m->index >= NUM_LIGHTS_CONTROLS)
  752. return -EINVAL;
  753. strcpy(m->name, lights_controls[m->index].name);
  754. break;
  755. default:
  756. return -EINVAL;
  757. }
  758. return 0;
  759. }
  760. /******************************************************************************
  761. *
  762. * ioctl_g_ctrl
  763. *
  764. * V4L2 get the value of a control variable
  765. *
  766. *****************************************************************************/
  767. static int ioctl_g_ctrl(void *arg,struct camera_data *cam)
  768. {
  769. struct v4l2_control *c = arg;
  770. switch(c->id) {
  771. case V4L2_CID_BRIGHTNESS:
  772. cpia2_do_command(cam, CPIA2_CMD_GET_VP_BRIGHTNESS,
  773. TRANSFER_READ, 0);
  774. c->value = cam->params.color_params.brightness;
  775. break;
  776. case V4L2_CID_CONTRAST:
  777. cpia2_do_command(cam, CPIA2_CMD_GET_CONTRAST,
  778. TRANSFER_READ, 0);
  779. c->value = cam->params.color_params.contrast;
  780. break;
  781. case V4L2_CID_SATURATION:
  782. cpia2_do_command(cam, CPIA2_CMD_GET_VP_SATURATION,
  783. TRANSFER_READ, 0);
  784. c->value = cam->params.color_params.saturation;
  785. break;
  786. case V4L2_CID_HFLIP:
  787. cpia2_do_command(cam, CPIA2_CMD_GET_USER_EFFECTS,
  788. TRANSFER_READ, 0);
  789. c->value = (cam->params.vp_params.user_effects &
  790. CPIA2_VP_USER_EFFECTS_MIRROR) != 0;
  791. break;
  792. case V4L2_CID_VFLIP:
  793. cpia2_do_command(cam, CPIA2_CMD_GET_USER_EFFECTS,
  794. TRANSFER_READ, 0);
  795. c->value = (cam->params.vp_params.user_effects &
  796. CPIA2_VP_USER_EFFECTS_FLIP) != 0;
  797. break;
  798. case CPIA2_CID_TARGET_KB:
  799. c->value = cam->params.vc_params.target_kb;
  800. break;
  801. case CPIA2_CID_GPIO:
  802. cpia2_do_command(cam, CPIA2_CMD_GET_VP_GPIO_DATA,
  803. TRANSFER_READ, 0);
  804. c->value = cam->params.vp_params.gpio_data;
  805. break;
  806. case CPIA2_CID_FLICKER_MODE:
  807. {
  808. int i, mode;
  809. cpia2_do_command(cam, CPIA2_CMD_GET_FLICKER_MODES,
  810. TRANSFER_READ, 0);
  811. if(cam->params.flicker_control.cam_register &
  812. CPIA2_VP_FLICKER_MODES_NEVER_FLICKER) {
  813. mode = NEVER_FLICKER;
  814. } else {
  815. if(cam->params.flicker_control.cam_register &
  816. CPIA2_VP_FLICKER_MODES_50HZ) {
  817. mode = FLICKER_50;
  818. } else {
  819. mode = FLICKER_60;
  820. }
  821. }
  822. for(i=0; i<NUM_FLICKER_CONTROLS; i++) {
  823. if(flicker_controls[i].value == mode) {
  824. c->value = i;
  825. break;
  826. }
  827. }
  828. if(i == NUM_FLICKER_CONTROLS)
  829. return -EINVAL;
  830. break;
  831. }
  832. case CPIA2_CID_FRAMERATE:
  833. {
  834. int maximum = NUM_FRAMERATE_CONTROLS - 1;
  835. int i;
  836. for(i=0; i<= maximum; i++) {
  837. if(cam->params.vp_params.frame_rate ==
  838. framerate_controls[i].value)
  839. break;
  840. }
  841. if(i > maximum)
  842. return -EINVAL;
  843. c->value = i;
  844. break;
  845. }
  846. case CPIA2_CID_USB_ALT:
  847. c->value = cam->params.camera_state.stream_mode;
  848. break;
  849. case CPIA2_CID_LIGHTS:
  850. {
  851. int i;
  852. cpia2_do_command(cam, CPIA2_CMD_GET_VP_GPIO_DATA,
  853. TRANSFER_READ, 0);
  854. for(i=0; i<NUM_LIGHTS_CONTROLS; i++) {
  855. if((cam->params.vp_params.gpio_data&GPIO_LIGHTS_MASK) ==
  856. lights_controls[i].value) {
  857. break;
  858. }
  859. }
  860. if(i == NUM_LIGHTS_CONTROLS)
  861. return -EINVAL;
  862. c->value = i;
  863. break;
  864. }
  865. case CPIA2_CID_RESET_CAMERA:
  866. return -EINVAL;
  867. default:
  868. return -EINVAL;
  869. }
  870. DBG("Get control id:%d, value:%d\n", c->id, c->value);
  871. return 0;
  872. }
  873. /******************************************************************************
  874. *
  875. * ioctl_s_ctrl
  876. *
  877. * V4L2 set the value of a control variable
  878. *
  879. *****************************************************************************/
  880. static int ioctl_s_ctrl(void *arg,struct camera_data *cam)
  881. {
  882. struct v4l2_control *c = arg;
  883. int i;
  884. int retval = 0;
  885. DBG("Set control id:%d, value:%d\n", c->id, c->value);
  886. /* Check that the value is in range */
  887. for(i=0; i<NUM_CONTROLS; i++) {
  888. if(c->id == controls[i].id) {
  889. if(c->value < controls[i].minimum ||
  890. c->value > controls[i].maximum) {
  891. return -EINVAL;
  892. }
  893. break;
  894. }
  895. }
  896. if(i == NUM_CONTROLS)
  897. return -EINVAL;
  898. switch(c->id) {
  899. case V4L2_CID_BRIGHTNESS:
  900. cpia2_set_brightness(cam, c->value);
  901. break;
  902. case V4L2_CID_CONTRAST:
  903. cpia2_set_contrast(cam, c->value);
  904. break;
  905. case V4L2_CID_SATURATION:
  906. cpia2_set_saturation(cam, c->value);
  907. break;
  908. case V4L2_CID_HFLIP:
  909. cpia2_set_property_mirror(cam, c->value);
  910. break;
  911. case V4L2_CID_VFLIP:
  912. cpia2_set_property_flip(cam, c->value);
  913. break;
  914. case CPIA2_CID_TARGET_KB:
  915. retval = cpia2_set_target_kb(cam, c->value);
  916. break;
  917. case CPIA2_CID_GPIO:
  918. retval = cpia2_set_gpio(cam, c->value);
  919. break;
  920. case CPIA2_CID_FLICKER_MODE:
  921. retval = cpia2_set_flicker_mode(cam,
  922. flicker_controls[c->value].value);
  923. break;
  924. case CPIA2_CID_FRAMERATE:
  925. retval = cpia2_set_fps(cam, framerate_controls[c->value].value);
  926. break;
  927. case CPIA2_CID_USB_ALT:
  928. retval = cpia2_usb_change_streaming_alternate(cam, c->value);
  929. break;
  930. case CPIA2_CID_LIGHTS:
  931. retval = cpia2_set_gpio(cam, lights_controls[c->value].value);
  932. break;
  933. case CPIA2_CID_RESET_CAMERA:
  934. cpia2_usb_stream_pause(cam);
  935. cpia2_reset_camera(cam);
  936. cpia2_usb_stream_resume(cam);
  937. break;
  938. default:
  939. retval = -EINVAL;
  940. }
  941. return retval;
  942. }
  943. /******************************************************************************
  944. *
  945. * ioctl_g_jpegcomp
  946. *
  947. * V4L2 get the JPEG compression parameters
  948. *
  949. *****************************************************************************/
  950. static int ioctl_g_jpegcomp(void *arg,struct camera_data *cam)
  951. {
  952. struct v4l2_jpegcompression *parms = arg;
  953. memset(parms, 0, sizeof(*parms));
  954. parms->quality = 80; // TODO: Can this be made meaningful?
  955. parms->jpeg_markers = V4L2_JPEG_MARKER_DQT | V4L2_JPEG_MARKER_DRI;
  956. if(!cam->params.compression.inhibit_htables) {
  957. parms->jpeg_markers |= V4L2_JPEG_MARKER_DHT;
  958. }
  959. parms->APPn = cam->APPn;
  960. parms->APP_len = cam->APP_len;
  961. if(cam->APP_len > 0) {
  962. memcpy(parms->APP_data, cam->APP_data, cam->APP_len);
  963. parms->jpeg_markers |= V4L2_JPEG_MARKER_APP;
  964. }
  965. parms->COM_len = cam->COM_len;
  966. if(cam->COM_len > 0) {
  967. memcpy(parms->COM_data, cam->COM_data, cam->COM_len);
  968. parms->jpeg_markers |= JPEG_MARKER_COM;
  969. }
  970. DBG("G_JPEGCOMP APP_len:%d COM_len:%d\n",
  971. parms->APP_len, parms->COM_len);
  972. return 0;
  973. }
  974. /******************************************************************************
  975. *
  976. * ioctl_s_jpegcomp
  977. *
  978. * V4L2 set the JPEG compression parameters
  979. * NOTE: quality and some jpeg_markers are ignored.
  980. *
  981. *****************************************************************************/
  982. static int ioctl_s_jpegcomp(void *arg,struct camera_data *cam)
  983. {
  984. struct v4l2_jpegcompression *parms = arg;
  985. DBG("S_JPEGCOMP APP_len:%d COM_len:%d\n",
  986. parms->APP_len, parms->COM_len);
  987. cam->params.compression.inhibit_htables =
  988. !(parms->jpeg_markers & V4L2_JPEG_MARKER_DHT);
  989. if(parms->APP_len != 0) {
  990. if(parms->APP_len > 0 &&
  991. parms->APP_len <= sizeof(cam->APP_data) &&
  992. parms->APPn >= 0 && parms->APPn <= 15) {
  993. cam->APPn = parms->APPn;
  994. cam->APP_len = parms->APP_len;
  995. memcpy(cam->APP_data, parms->APP_data, parms->APP_len);
  996. } else {
  997. LOG("Bad APPn Params n=%d len=%d\n",
  998. parms->APPn, parms->APP_len);
  999. return -EINVAL;
  1000. }
  1001. } else {
  1002. cam->APP_len = 0;
  1003. }
  1004. if(parms->COM_len != 0) {
  1005. if(parms->COM_len > 0 &&
  1006. parms->COM_len <= sizeof(cam->COM_data)) {
  1007. cam->COM_len = parms->COM_len;
  1008. memcpy(cam->COM_data, parms->COM_data, parms->COM_len);
  1009. } else {
  1010. LOG("Bad COM_len=%d\n", parms->COM_len);
  1011. return -EINVAL;
  1012. }
  1013. }
  1014. return 0;
  1015. }
  1016. /******************************************************************************
  1017. *
  1018. * ioctl_reqbufs
  1019. *
  1020. * V4L2 Initiate memory mapping.
  1021. * NOTE: The user's request is ignored. For now the buffers are fixed.
  1022. *
  1023. *****************************************************************************/
  1024. static int ioctl_reqbufs(void *arg,struct camera_data *cam)
  1025. {
  1026. struct v4l2_requestbuffers *req = arg;
  1027. if(req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  1028. req->memory != V4L2_MEMORY_MMAP)
  1029. return -EINVAL;
  1030. DBG("REQBUFS requested:%d returning:%d\n", req->count, cam->num_frames);
  1031. req->count = cam->num_frames;
  1032. memset(&req->reserved, 0, sizeof(req->reserved));
  1033. return 0;
  1034. }
  1035. /******************************************************************************
  1036. *
  1037. * ioctl_querybuf
  1038. *
  1039. * V4L2 Query memory buffer status.
  1040. *
  1041. *****************************************************************************/
  1042. static int ioctl_querybuf(void *arg,struct camera_data *cam)
  1043. {
  1044. struct v4l2_buffer *buf = arg;
  1045. if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  1046. buf->index > cam->num_frames)
  1047. return -EINVAL;
  1048. buf->m.offset = cam->buffers[buf->index].data - cam->frame_buffer;
  1049. buf->length = cam->frame_size;
  1050. buf->memory = V4L2_MEMORY_MMAP;
  1051. if(cam->mmapped)
  1052. buf->flags = V4L2_BUF_FLAG_MAPPED;
  1053. else
  1054. buf->flags = 0;
  1055. switch (cam->buffers[buf->index].status) {
  1056. case FRAME_EMPTY:
  1057. case FRAME_ERROR:
  1058. case FRAME_READING:
  1059. buf->bytesused = 0;
  1060. buf->flags = V4L2_BUF_FLAG_QUEUED;
  1061. break;
  1062. case FRAME_READY:
  1063. buf->bytesused = cam->buffers[buf->index].length;
  1064. buf->timestamp = cam->buffers[buf->index].timestamp;
  1065. buf->sequence = cam->buffers[buf->index].seq;
  1066. buf->flags = V4L2_BUF_FLAG_DONE;
  1067. break;
  1068. }
  1069. DBG("QUERYBUF index:%d offset:%d flags:%d seq:%d bytesused:%d\n",
  1070. buf->index, buf->m.offset, buf->flags, buf->sequence,
  1071. buf->bytesused);
  1072. return 0;
  1073. }
  1074. /******************************************************************************
  1075. *
  1076. * ioctl_qbuf
  1077. *
  1078. * V4L2 User is freeing buffer
  1079. *
  1080. *****************************************************************************/
  1081. static int ioctl_qbuf(void *arg,struct camera_data *cam)
  1082. {
  1083. struct v4l2_buffer *buf = arg;
  1084. if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  1085. buf->memory != V4L2_MEMORY_MMAP ||
  1086. buf->index > cam->num_frames)
  1087. return -EINVAL;
  1088. DBG("QBUF #%d\n", buf->index);
  1089. if(cam->buffers[buf->index].status == FRAME_READY)
  1090. cam->buffers[buf->index].status = FRAME_EMPTY;
  1091. return 0;
  1092. }
  1093. /******************************************************************************
  1094. *
  1095. * find_earliest_filled_buffer
  1096. *
  1097. * Helper for ioctl_dqbuf. Find the next ready buffer.
  1098. *
  1099. *****************************************************************************/
  1100. static int find_earliest_filled_buffer(struct camera_data *cam)
  1101. {
  1102. int i;
  1103. int found = -1;
  1104. for (i=0; i<cam->num_frames; i++) {
  1105. if(cam->buffers[i].status == FRAME_READY) {
  1106. if(found < 0) {
  1107. found = i;
  1108. } else {
  1109. /* find which buffer is earlier */
  1110. struct timeval *tv1, *tv2;
  1111. tv1 = &cam->buffers[i].timestamp;
  1112. tv2 = &cam->buffers[found].timestamp;
  1113. if(tv1->tv_sec < tv2->tv_sec ||
  1114. (tv1->tv_sec == tv2->tv_sec &&
  1115. tv1->tv_usec < tv2->tv_usec))
  1116. found = i;
  1117. }
  1118. }
  1119. }
  1120. return found;
  1121. }
  1122. /******************************************************************************
  1123. *
  1124. * ioctl_dqbuf
  1125. *
  1126. * V4L2 User is asking for a filled buffer.
  1127. *
  1128. *****************************************************************************/
  1129. static int ioctl_dqbuf(void *arg,struct camera_data *cam, struct file *file)
  1130. {
  1131. struct v4l2_buffer *buf = arg;
  1132. int frame;
  1133. if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  1134. buf->memory != V4L2_MEMORY_MMAP)
  1135. return -EINVAL;
  1136. frame = find_earliest_filled_buffer(cam);
  1137. if(frame < 0 && file->f_flags&O_NONBLOCK)
  1138. return -EAGAIN;
  1139. if(frame < 0) {
  1140. /* Wait for a frame to become available */
  1141. struct framebuf *cb=cam->curbuff;
  1142. mutex_unlock(&cam->busy_lock);
  1143. wait_event_interruptible(cam->wq_stream,
  1144. !cam->present ||
  1145. (cb=cam->curbuff)->status == FRAME_READY);
  1146. mutex_lock(&cam->busy_lock);
  1147. if (signal_pending(current))
  1148. return -ERESTARTSYS;
  1149. if(!cam->present)
  1150. return -ENOTTY;
  1151. frame = cb->num;
  1152. }
  1153. buf->index = frame;
  1154. buf->bytesused = cam->buffers[buf->index].length;
  1155. buf->flags = V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_DONE;
  1156. buf->field = V4L2_FIELD_NONE;
  1157. buf->timestamp = cam->buffers[buf->index].timestamp;
  1158. buf->sequence = cam->buffers[buf->index].seq;
  1159. buf->m.offset = cam->buffers[buf->index].data - cam->frame_buffer;
  1160. buf->length = cam->frame_size;
  1161. buf->input = 0;
  1162. buf->reserved = 0;
  1163. memset(&buf->timecode, 0, sizeof(buf->timecode));
  1164. DBG("DQBUF #%d status:%d seq:%d length:%d\n", buf->index,
  1165. cam->buffers[buf->index].status, buf->sequence, buf->bytesused);
  1166. return 0;
  1167. }
  1168. /******************************************************************************
  1169. *
  1170. * cpia2_ioctl
  1171. *
  1172. *****************************************************************************/
  1173. static long cpia2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
  1174. {
  1175. struct camera_data *cam = video_drvdata(file);
  1176. long retval = 0;
  1177. if (!cam)
  1178. return -ENOTTY;
  1179. /* make this _really_ smp-safe */
  1180. if (mutex_lock_interruptible(&cam->busy_lock))
  1181. return -ERESTARTSYS;
  1182. if (!cam->present) {
  1183. mutex_unlock(&cam->busy_lock);
  1184. return -ENODEV;
  1185. }
  1186. /* Priority check */
  1187. switch (cmd) {
  1188. case VIDIOC_S_FMT:
  1189. {
  1190. struct cpia2_fh *fh = file->private_data;
  1191. retval = v4l2_prio_check(&cam->prio, fh->prio);
  1192. if(retval) {
  1193. mutex_unlock(&cam->busy_lock);
  1194. return retval;
  1195. }
  1196. break;
  1197. }
  1198. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1199. case VIDIOCGMBUF:
  1200. {
  1201. struct cpia2_fh *fh = file->private_data;
  1202. if(fh->prio != V4L2_PRIORITY_RECORD) {
  1203. mutex_unlock(&cam->busy_lock);
  1204. return -EBUSY;
  1205. }
  1206. break;
  1207. }
  1208. #endif
  1209. default:
  1210. break;
  1211. }
  1212. switch (cmd) {
  1213. /* CPIA2 extension to Video4Linux API */
  1214. case CPIA2_IOC_SET_GPIO:
  1215. retval = ioctl_set_gpio(arg, cam);
  1216. break;
  1217. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1218. case VIDIOCGMBUF: /* mmap interface */
  1219. retval = ioctl_get_mbuf(arg, cam);
  1220. break;
  1221. #endif
  1222. case VIDIOC_QUERYCAP:
  1223. retval = ioctl_querycap(arg,cam);
  1224. break;
  1225. case VIDIOC_ENUMINPUT:
  1226. case VIDIOC_G_INPUT:
  1227. case VIDIOC_S_INPUT:
  1228. retval = ioctl_input(cmd, arg, cam);
  1229. break;
  1230. case VIDIOC_ENUM_FMT:
  1231. retval = ioctl_enum_fmt(arg,cam);
  1232. break;
  1233. case VIDIOC_TRY_FMT:
  1234. retval = ioctl_try_fmt(arg,cam);
  1235. break;
  1236. case VIDIOC_G_FMT:
  1237. retval = ioctl_get_fmt(arg,cam);
  1238. break;
  1239. case VIDIOC_S_FMT:
  1240. retval = ioctl_set_fmt(arg,cam,file->private_data);
  1241. break;
  1242. case VIDIOC_CROPCAP:
  1243. retval = ioctl_cropcap(arg,cam);
  1244. break;
  1245. case VIDIOC_G_CROP:
  1246. case VIDIOC_S_CROP:
  1247. // TODO: I think cropping can be implemented - SJB
  1248. retval = -EINVAL;
  1249. break;
  1250. case VIDIOC_QUERYCTRL:
  1251. retval = ioctl_queryctrl(arg,cam);
  1252. break;
  1253. case VIDIOC_QUERYMENU:
  1254. retval = ioctl_querymenu(arg,cam);
  1255. break;
  1256. case VIDIOC_G_CTRL:
  1257. retval = ioctl_g_ctrl(arg,cam);
  1258. break;
  1259. case VIDIOC_S_CTRL:
  1260. retval = ioctl_s_ctrl(arg,cam);
  1261. break;
  1262. case VIDIOC_G_JPEGCOMP:
  1263. retval = ioctl_g_jpegcomp(arg,cam);
  1264. break;
  1265. case VIDIOC_S_JPEGCOMP:
  1266. retval = ioctl_s_jpegcomp(arg,cam);
  1267. break;
  1268. case VIDIOC_G_PRIORITY:
  1269. {
  1270. struct cpia2_fh *fh = file->private_data;
  1271. *(enum v4l2_priority*)arg = fh->prio;
  1272. break;
  1273. }
  1274. case VIDIOC_S_PRIORITY:
  1275. {
  1276. struct cpia2_fh *fh = file->private_data;
  1277. enum v4l2_priority prio;
  1278. prio = *(enum v4l2_priority*)arg;
  1279. if(cam->streaming &&
  1280. prio != fh->prio &&
  1281. fh->prio == V4L2_PRIORITY_RECORD) {
  1282. /* Can't drop record priority while streaming */
  1283. retval = -EBUSY;
  1284. } else if(prio == V4L2_PRIORITY_RECORD &&
  1285. prio != fh->prio &&
  1286. v4l2_prio_max(&cam->prio) == V4L2_PRIORITY_RECORD) {
  1287. /* Only one program can record at a time */
  1288. retval = -EBUSY;
  1289. } else {
  1290. retval = v4l2_prio_change(&cam->prio, &fh->prio, prio);
  1291. }
  1292. break;
  1293. }
  1294. case VIDIOC_REQBUFS:
  1295. retval = ioctl_reqbufs(arg,cam);
  1296. break;
  1297. case VIDIOC_QUERYBUF:
  1298. retval = ioctl_querybuf(arg,cam);
  1299. break;
  1300. case VIDIOC_QBUF:
  1301. retval = ioctl_qbuf(arg,cam);
  1302. break;
  1303. case VIDIOC_DQBUF:
  1304. retval = ioctl_dqbuf(arg,cam,file);
  1305. break;
  1306. case VIDIOC_STREAMON:
  1307. {
  1308. int type;
  1309. DBG("VIDIOC_STREAMON, streaming=%d\n", cam->streaming);
  1310. type = *(int*)arg;
  1311. if(!cam->mmapped || type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1312. retval = -EINVAL;
  1313. if(!cam->streaming) {
  1314. retval = cpia2_usb_stream_start(cam,
  1315. cam->params.camera_state.stream_mode);
  1316. } else {
  1317. retval = -EINVAL;
  1318. }
  1319. break;
  1320. }
  1321. case VIDIOC_STREAMOFF:
  1322. {
  1323. int type;
  1324. DBG("VIDIOC_STREAMOFF, streaming=%d\n", cam->streaming);
  1325. type = *(int*)arg;
  1326. if(!cam->mmapped || type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1327. retval = -EINVAL;
  1328. if(cam->streaming) {
  1329. retval = cpia2_usb_stream_stop(cam);
  1330. } else {
  1331. retval = -EINVAL;
  1332. }
  1333. break;
  1334. }
  1335. case VIDIOC_ENUMOUTPUT:
  1336. case VIDIOC_G_OUTPUT:
  1337. case VIDIOC_S_OUTPUT:
  1338. case VIDIOC_G_MODULATOR:
  1339. case VIDIOC_S_MODULATOR:
  1340. case VIDIOC_ENUMAUDIO:
  1341. case VIDIOC_G_AUDIO:
  1342. case VIDIOC_S_AUDIO:
  1343. case VIDIOC_ENUMAUDOUT:
  1344. case VIDIOC_G_AUDOUT:
  1345. case VIDIOC_S_AUDOUT:
  1346. case VIDIOC_ENUMSTD:
  1347. case VIDIOC_QUERYSTD:
  1348. case VIDIOC_G_STD:
  1349. case VIDIOC_S_STD:
  1350. case VIDIOC_G_TUNER:
  1351. case VIDIOC_S_TUNER:
  1352. case VIDIOC_G_FREQUENCY:
  1353. case VIDIOC_S_FREQUENCY:
  1354. case VIDIOC_OVERLAY:
  1355. case VIDIOC_G_FBUF:
  1356. case VIDIOC_S_FBUF:
  1357. case VIDIOC_G_PARM:
  1358. case VIDIOC_S_PARM:
  1359. retval = -EINVAL;
  1360. break;
  1361. default:
  1362. retval = -ENOIOCTLCMD;
  1363. break;
  1364. }
  1365. mutex_unlock(&cam->busy_lock);
  1366. return retval;
  1367. }
  1368. static long cpia2_ioctl(struct file *file,
  1369. unsigned int cmd, unsigned long arg)
  1370. {
  1371. return video_usercopy(file, cmd, arg, cpia2_do_ioctl);
  1372. }
  1373. /******************************************************************************
  1374. *
  1375. * cpia2_mmap
  1376. *
  1377. *****************************************************************************/
  1378. static int cpia2_mmap(struct file *file, struct vm_area_struct *area)
  1379. {
  1380. struct camera_data *cam = video_drvdata(file);
  1381. int retval;
  1382. /* Priority check */
  1383. struct cpia2_fh *fh = file->private_data;
  1384. if(fh->prio != V4L2_PRIORITY_RECORD) {
  1385. return -EBUSY;
  1386. }
  1387. retval = cpia2_remap_buffer(cam, area);
  1388. if(!retval)
  1389. fh->mmapped = 1;
  1390. return retval;
  1391. }
  1392. /******************************************************************************
  1393. *
  1394. * reset_camera_struct_v4l
  1395. *
  1396. * Sets all values to the defaults
  1397. *****************************************************************************/
  1398. static void reset_camera_struct_v4l(struct camera_data *cam)
  1399. {
  1400. cam->width = cam->params.roi.width;
  1401. cam->height = cam->params.roi.height;
  1402. cam->frame_size = buffer_size;
  1403. cam->num_frames = num_buffers;
  1404. /* FlickerModes */
  1405. cam->params.flicker_control.flicker_mode_req = flicker_mode;
  1406. cam->params.flicker_control.mains_frequency = flicker_freq;
  1407. /* streamMode */
  1408. cam->params.camera_state.stream_mode = alternate;
  1409. cam->pixelformat = V4L2_PIX_FMT_JPEG;
  1410. v4l2_prio_init(&cam->prio);
  1411. }
  1412. /***
  1413. * The v4l video device structure initialized for this device
  1414. ***/
  1415. static const struct v4l2_file_operations cpia2_fops = {
  1416. .owner = THIS_MODULE,
  1417. .open = cpia2_open,
  1418. .release = cpia2_close,
  1419. .read = cpia2_v4l_read,
  1420. .poll = cpia2_v4l_poll,
  1421. .ioctl = cpia2_ioctl,
  1422. .mmap = cpia2_mmap,
  1423. };
  1424. static struct video_device cpia2_template = {
  1425. /* I could not find any place for the old .initialize initializer?? */
  1426. .name = "CPiA2 Camera",
  1427. .fops = &cpia2_fops,
  1428. .release = video_device_release,
  1429. };
  1430. /******************************************************************************
  1431. *
  1432. * cpia2_register_camera
  1433. *
  1434. *****************************************************************************/
  1435. int cpia2_register_camera(struct camera_data *cam)
  1436. {
  1437. cam->vdev = video_device_alloc();
  1438. if(!cam->vdev)
  1439. return -ENOMEM;
  1440. memcpy(cam->vdev, &cpia2_template, sizeof(cpia2_template));
  1441. video_set_drvdata(cam->vdev, cam);
  1442. reset_camera_struct_v4l(cam);
  1443. /* register v4l device */
  1444. if (video_register_device(cam->vdev, VFL_TYPE_GRABBER, video_nr) < 0) {
  1445. ERR("video_register_device failed\n");
  1446. video_device_release(cam->vdev);
  1447. return -ENODEV;
  1448. }
  1449. return 0;
  1450. }
  1451. /******************************************************************************
  1452. *
  1453. * cpia2_unregister_camera
  1454. *
  1455. *****************************************************************************/
  1456. void cpia2_unregister_camera(struct camera_data *cam)
  1457. {
  1458. if (!cam->open_count) {
  1459. video_unregister_device(cam->vdev);
  1460. } else {
  1461. LOG("%s removed while open, deferring "
  1462. "video_unregister_device\n",
  1463. video_device_node_name(cam->vdev));
  1464. }
  1465. }
  1466. /******************************************************************************
  1467. *
  1468. * check_parameters
  1469. *
  1470. * Make sure that all user-supplied parameters are sensible
  1471. *****************************************************************************/
  1472. static void __init check_parameters(void)
  1473. {
  1474. if(buffer_size < PAGE_SIZE) {
  1475. buffer_size = PAGE_SIZE;
  1476. LOG("buffer_size too small, setting to %d\n", buffer_size);
  1477. } else if(buffer_size > 1024*1024) {
  1478. /* arbitrary upper limiit */
  1479. buffer_size = 1024*1024;
  1480. LOG("buffer_size ridiculously large, setting to %d\n",
  1481. buffer_size);
  1482. } else {
  1483. buffer_size += PAGE_SIZE-1;
  1484. buffer_size &= ~(PAGE_SIZE-1);
  1485. }
  1486. if(num_buffers < 1) {
  1487. num_buffers = 1;
  1488. LOG("num_buffers too small, setting to %d\n", num_buffers);
  1489. } else if(num_buffers > VIDEO_MAX_FRAME) {
  1490. num_buffers = VIDEO_MAX_FRAME;
  1491. LOG("num_buffers too large, setting to %d\n", num_buffers);
  1492. }
  1493. if(alternate < USBIF_ISO_1 || alternate > USBIF_ISO_6) {
  1494. alternate = DEFAULT_ALT;
  1495. LOG("alternate specified is invalid, using %d\n", alternate);
  1496. }
  1497. if (flicker_mode != NEVER_FLICKER && flicker_mode != ANTI_FLICKER_ON) {
  1498. flicker_mode = NEVER_FLICKER;
  1499. LOG("Flicker mode specified is invalid, using %d\n",
  1500. flicker_mode);
  1501. }
  1502. if (flicker_freq != FLICKER_50 && flicker_freq != FLICKER_60) {
  1503. flicker_freq = FLICKER_60;
  1504. LOG("Flicker mode specified is invalid, using %d\n",
  1505. flicker_freq);
  1506. }
  1507. if(video_nr < -1 || video_nr > 64) {
  1508. video_nr = -1;
  1509. LOG("invalid video_nr specified, must be -1 to 64\n");
  1510. }
  1511. DBG("Using %d buffers, each %d bytes, alternate=%d\n",
  1512. num_buffers, buffer_size, alternate);
  1513. }
  1514. /************ Module Stuff ***************/
  1515. /******************************************************************************
  1516. *
  1517. * cpia2_init/module_init
  1518. *
  1519. *****************************************************************************/
  1520. static int __init cpia2_init(void)
  1521. {
  1522. LOG("%s v%d.%d.%d\n",
  1523. ABOUT, CPIA2_MAJ_VER, CPIA2_MIN_VER, CPIA2_PATCH_VER);
  1524. check_parameters();
  1525. cpia2_usb_init();
  1526. return 0;
  1527. }
  1528. /******************************************************************************
  1529. *
  1530. * cpia2_exit/module_exit
  1531. *
  1532. *****************************************************************************/
  1533. static void __exit cpia2_exit(void)
  1534. {
  1535. cpia2_usb_cleanup();
  1536. schedule_timeout(2 * HZ);
  1537. }
  1538. module_init(cpia2_init);
  1539. module_exit(cpia2_exit);