go7007-v4l2.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840
  1. /*
  2. * Copyright (C) 2005-2006 Micronas USA Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License (Version 2) as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software Foundation,
  15. * Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/init.h>
  19. #include <linux/version.h>
  20. #include <linux/delay.h>
  21. #include <linux/sched.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/slab.h>
  24. #include <linux/fs.h>
  25. #include <linux/unistd.h>
  26. #include <linux/time.h>
  27. #include <linux/vmalloc.h>
  28. #include <linux/pagemap.h>
  29. #include <linux/videodev2.h>
  30. #include <media/v4l2-common.h>
  31. #include <media/v4l2-ioctl.h>
  32. #include <media/v4l2-subdev.h>
  33. #include <linux/i2c.h>
  34. #include <linux/mutex.h>
  35. #include <linux/uaccess.h>
  36. #include <asm/system.h>
  37. #include "go7007.h"
  38. #include "go7007-priv.h"
  39. #include "wis-i2c.h"
  40. /* Temporary defines until accepted in v4l-dvb */
  41. #ifndef V4L2_MPEG_STREAM_TYPE_MPEG_ELEM
  42. #define V4L2_MPEG_STREAM_TYPE_MPEG_ELEM 6 /* MPEG elementary stream */
  43. #endif
  44. #ifndef V4L2_MPEG_VIDEO_ENCODING_MPEG_4
  45. #define V4L2_MPEG_VIDEO_ENCODING_MPEG_4 3
  46. #endif
  47. #define call_all(dev, o, f, args...) \
  48. v4l2_device_call_until_err(dev, 0, o, f, ##args)
  49. static void deactivate_buffer(struct go7007_buffer *gobuf)
  50. {
  51. int i;
  52. if (gobuf->state != BUF_STATE_IDLE) {
  53. list_del(&gobuf->stream);
  54. gobuf->state = BUF_STATE_IDLE;
  55. }
  56. if (gobuf->page_count > 0) {
  57. for (i = 0; i < gobuf->page_count; ++i)
  58. page_cache_release(gobuf->pages[i]);
  59. gobuf->page_count = 0;
  60. }
  61. }
  62. static void abort_queued(struct go7007 *go)
  63. {
  64. struct go7007_buffer *gobuf, *next;
  65. list_for_each_entry_safe(gobuf, next, &go->stream, stream) {
  66. deactivate_buffer(gobuf);
  67. }
  68. }
  69. static int go7007_streamoff(struct go7007 *go)
  70. {
  71. int retval = -EINVAL;
  72. unsigned long flags;
  73. mutex_lock(&go->hw_lock);
  74. if (go->streaming) {
  75. go->streaming = 0;
  76. go7007_stream_stop(go);
  77. spin_lock_irqsave(&go->spinlock, flags);
  78. abort_queued(go);
  79. spin_unlock_irqrestore(&go->spinlock, flags);
  80. go7007_reset_encoder(go);
  81. retval = 0;
  82. }
  83. mutex_unlock(&go->hw_lock);
  84. return 0;
  85. }
  86. static int go7007_open(struct file *file)
  87. {
  88. struct go7007 *go = video_get_drvdata(video_devdata(file));
  89. struct go7007_file *gofh;
  90. if (go->status != STATUS_ONLINE)
  91. return -EBUSY;
  92. gofh = kmalloc(sizeof(struct go7007_file), GFP_KERNEL);
  93. if (gofh == NULL)
  94. return -ENOMEM;
  95. ++go->ref_count;
  96. gofh->go = go;
  97. mutex_init(&gofh->lock);
  98. gofh->buf_count = 0;
  99. file->private_data = gofh;
  100. return 0;
  101. }
  102. static int go7007_release(struct file *file)
  103. {
  104. struct go7007_file *gofh = file->private_data;
  105. struct go7007 *go = gofh->go;
  106. if (gofh->buf_count > 0) {
  107. go7007_streamoff(go);
  108. go->in_use = 0;
  109. kfree(gofh->bufs);
  110. gofh->buf_count = 0;
  111. }
  112. kfree(gofh);
  113. if (--go->ref_count == 0)
  114. kfree(go);
  115. file->private_data = NULL;
  116. return 0;
  117. }
  118. static u32 get_frame_type_flag(struct go7007_buffer *gobuf, int format)
  119. {
  120. u8 *f = page_address(gobuf->pages[0]);
  121. switch (format) {
  122. case GO7007_FORMAT_MJPEG:
  123. return V4L2_BUF_FLAG_KEYFRAME;
  124. case GO7007_FORMAT_MPEG4:
  125. switch ((f[gobuf->frame_offset + 4] >> 6) & 0x3) {
  126. case 0:
  127. return V4L2_BUF_FLAG_KEYFRAME;
  128. case 1:
  129. return V4L2_BUF_FLAG_PFRAME;
  130. case 2:
  131. return V4L2_BUF_FLAG_BFRAME;
  132. default:
  133. return 0;
  134. }
  135. case GO7007_FORMAT_MPEG1:
  136. case GO7007_FORMAT_MPEG2:
  137. switch ((f[gobuf->frame_offset + 5] >> 3) & 0x7) {
  138. case 1:
  139. return V4L2_BUF_FLAG_KEYFRAME;
  140. case 2:
  141. return V4L2_BUF_FLAG_PFRAME;
  142. case 3:
  143. return V4L2_BUF_FLAG_BFRAME;
  144. default:
  145. return 0;
  146. }
  147. }
  148. return 0;
  149. }
  150. static int set_capture_size(struct go7007 *go, struct v4l2_format *fmt, int try)
  151. {
  152. int sensor_height = 0, sensor_width = 0;
  153. int width, height, i;
  154. if (fmt != NULL && fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG &&
  155. fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MPEG &&
  156. fmt->fmt.pix.pixelformat != V4L2_PIX_FMT_MPEG4)
  157. return -EINVAL;
  158. switch (go->standard) {
  159. case GO7007_STD_NTSC:
  160. sensor_width = 720;
  161. sensor_height = 480;
  162. break;
  163. case GO7007_STD_PAL:
  164. sensor_width = 720;
  165. sensor_height = 576;
  166. break;
  167. case GO7007_STD_OTHER:
  168. sensor_width = go->board_info->sensor_width;
  169. sensor_height = go->board_info->sensor_height;
  170. break;
  171. }
  172. if (fmt == NULL) {
  173. width = sensor_width;
  174. height = sensor_height;
  175. } else if (go->board_info->sensor_flags & GO7007_SENSOR_SCALING) {
  176. if (fmt->fmt.pix.width > sensor_width)
  177. width = sensor_width;
  178. else if (fmt->fmt.pix.width < 144)
  179. width = 144;
  180. else
  181. width = fmt->fmt.pix.width & ~0x0f;
  182. if (fmt->fmt.pix.height > sensor_height)
  183. height = sensor_height;
  184. else if (fmt->fmt.pix.height < 96)
  185. height = 96;
  186. else
  187. height = fmt->fmt.pix.height & ~0x0f;
  188. } else {
  189. int requested_size = fmt->fmt.pix.width * fmt->fmt.pix.height;
  190. int sensor_size = sensor_width * sensor_height;
  191. if (64 * requested_size < 9 * sensor_size) {
  192. width = sensor_width / 4;
  193. height = sensor_height / 4;
  194. } else if (64 * requested_size < 36 * sensor_size) {
  195. width = sensor_width / 2;
  196. height = sensor_height / 2;
  197. } else {
  198. width = sensor_width;
  199. height = sensor_height;
  200. }
  201. width &= ~0xf;
  202. height &= ~0xf;
  203. }
  204. if (fmt != NULL) {
  205. u32 pixelformat = fmt->fmt.pix.pixelformat;
  206. memset(fmt, 0, sizeof(*fmt));
  207. fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  208. fmt->fmt.pix.width = width;
  209. fmt->fmt.pix.height = height;
  210. fmt->fmt.pix.pixelformat = pixelformat;
  211. fmt->fmt.pix.field = V4L2_FIELD_NONE;
  212. fmt->fmt.pix.bytesperline = 0;
  213. fmt->fmt.pix.sizeimage = GO7007_BUF_SIZE;
  214. fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; /* ?? */
  215. }
  216. if (try)
  217. return 0;
  218. go->width = width;
  219. go->height = height;
  220. go->encoder_h_offset = go->board_info->sensor_h_offset;
  221. go->encoder_v_offset = go->board_info->sensor_v_offset;
  222. for (i = 0; i < 4; ++i)
  223. go->modet[i].enable = 0;
  224. for (i = 0; i < 1624; ++i)
  225. go->modet_map[i] = 0;
  226. if (go->board_info->sensor_flags & GO7007_SENSOR_SCALING) {
  227. struct v4l2_mbus_framefmt mbus_fmt;
  228. mbus_fmt.code = V4L2_MBUS_FMT_FIXED;
  229. if (fmt != NULL)
  230. mbus_fmt.width = fmt->fmt.pix.width;
  231. else
  232. mbus_fmt.width = width;
  233. if (height > sensor_height / 2) {
  234. mbus_fmt.height = height / 2;
  235. go->encoder_v_halve = 0;
  236. } else {
  237. mbus_fmt.height = height;
  238. go->encoder_v_halve = 1;
  239. }
  240. call_all(&go->v4l2_dev, video, s_mbus_fmt, &mbus_fmt);
  241. } else {
  242. if (width <= sensor_width / 4) {
  243. go->encoder_h_halve = 1;
  244. go->encoder_v_halve = 1;
  245. go->encoder_subsample = 1;
  246. } else if (width <= sensor_width / 2) {
  247. go->encoder_h_halve = 1;
  248. go->encoder_v_halve = 1;
  249. go->encoder_subsample = 0;
  250. } else {
  251. go->encoder_h_halve = 0;
  252. go->encoder_v_halve = 0;
  253. go->encoder_subsample = 0;
  254. }
  255. }
  256. if (fmt == NULL)
  257. return 0;
  258. switch (fmt->fmt.pix.pixelformat) {
  259. case V4L2_PIX_FMT_MPEG:
  260. if (go->format == GO7007_FORMAT_MPEG1 ||
  261. go->format == GO7007_FORMAT_MPEG2 ||
  262. go->format == GO7007_FORMAT_MPEG4)
  263. break;
  264. go->format = GO7007_FORMAT_MPEG1;
  265. go->pali = 0;
  266. go->aspect_ratio = GO7007_RATIO_1_1;
  267. go->gop_size = go->sensor_framerate / 1000;
  268. go->ipb = 0;
  269. go->closed_gop = 1;
  270. go->repeat_seqhead = 1;
  271. go->seq_header_enable = 1;
  272. go->gop_header_enable = 1;
  273. go->dvd_mode = 0;
  274. break;
  275. /* Backwards compatibility only! */
  276. case V4L2_PIX_FMT_MPEG4:
  277. if (go->format == GO7007_FORMAT_MPEG4)
  278. break;
  279. go->format = GO7007_FORMAT_MPEG4;
  280. go->pali = 0xf5;
  281. go->aspect_ratio = GO7007_RATIO_1_1;
  282. go->gop_size = go->sensor_framerate / 1000;
  283. go->ipb = 0;
  284. go->closed_gop = 1;
  285. go->repeat_seqhead = 1;
  286. go->seq_header_enable = 1;
  287. go->gop_header_enable = 1;
  288. go->dvd_mode = 0;
  289. break;
  290. case V4L2_PIX_FMT_MJPEG:
  291. go->format = GO7007_FORMAT_MJPEG;
  292. go->pali = 0;
  293. go->aspect_ratio = GO7007_RATIO_1_1;
  294. go->gop_size = 0;
  295. go->ipb = 0;
  296. go->closed_gop = 0;
  297. go->repeat_seqhead = 0;
  298. go->seq_header_enable = 0;
  299. go->gop_header_enable = 0;
  300. go->dvd_mode = 0;
  301. break;
  302. }
  303. return 0;
  304. }
  305. #if 0
  306. static int clip_to_modet_map(struct go7007 *go, int region,
  307. struct v4l2_clip *clip_list)
  308. {
  309. struct v4l2_clip clip, *clip_ptr;
  310. int x, y, mbnum;
  311. /* Check if coordinates are OK and if any macroblocks are already
  312. * used by other regions (besides 0) */
  313. clip_ptr = clip_list;
  314. while (clip_ptr) {
  315. if (copy_from_user(&clip, clip_ptr, sizeof(clip)))
  316. return -EFAULT;
  317. if (clip.c.left < 0 || (clip.c.left & 0xF) ||
  318. clip.c.width <= 0 || (clip.c.width & 0xF))
  319. return -EINVAL;
  320. if (clip.c.left + clip.c.width > go->width)
  321. return -EINVAL;
  322. if (clip.c.top < 0 || (clip.c.top & 0xF) ||
  323. clip.c.height <= 0 || (clip.c.height & 0xF))
  324. return -EINVAL;
  325. if (clip.c.top + clip.c.height > go->height)
  326. return -EINVAL;
  327. for (y = 0; y < clip.c.height; y += 16)
  328. for (x = 0; x < clip.c.width; x += 16) {
  329. mbnum = (go->width >> 4) *
  330. ((clip.c.top + y) >> 4) +
  331. ((clip.c.left + x) >> 4);
  332. if (go->modet_map[mbnum] != 0 &&
  333. go->modet_map[mbnum] != region)
  334. return -EBUSY;
  335. }
  336. clip_ptr = clip.next;
  337. }
  338. /* Clear old region macroblocks */
  339. for (mbnum = 0; mbnum < 1624; ++mbnum)
  340. if (go->modet_map[mbnum] == region)
  341. go->modet_map[mbnum] = 0;
  342. /* Claim macroblocks in this list */
  343. clip_ptr = clip_list;
  344. while (clip_ptr) {
  345. if (copy_from_user(&clip, clip_ptr, sizeof(clip)))
  346. return -EFAULT;
  347. for (y = 0; y < clip.c.height; y += 16)
  348. for (x = 0; x < clip.c.width; x += 16) {
  349. mbnum = (go->width >> 4) *
  350. ((clip.c.top + y) >> 4) +
  351. ((clip.c.left + x) >> 4);
  352. go->modet_map[mbnum] = region;
  353. }
  354. clip_ptr = clip.next;
  355. }
  356. return 0;
  357. }
  358. #endif
  359. static int mpeg_query_ctrl(struct v4l2_queryctrl *ctrl)
  360. {
  361. static const u32 mpeg_ctrls[] = {
  362. V4L2_CID_MPEG_CLASS,
  363. V4L2_CID_MPEG_STREAM_TYPE,
  364. V4L2_CID_MPEG_VIDEO_ENCODING,
  365. V4L2_CID_MPEG_VIDEO_ASPECT,
  366. V4L2_CID_MPEG_VIDEO_GOP_SIZE,
  367. V4L2_CID_MPEG_VIDEO_GOP_CLOSURE,
  368. V4L2_CID_MPEG_VIDEO_BITRATE,
  369. 0
  370. };
  371. static const u32 *ctrl_classes[] = {
  372. mpeg_ctrls,
  373. NULL
  374. };
  375. ctrl->id = v4l2_ctrl_next(ctrl_classes, ctrl->id);
  376. switch (ctrl->id) {
  377. case V4L2_CID_MPEG_CLASS:
  378. return v4l2_ctrl_query_fill(ctrl, 0, 0, 0, 0);
  379. case V4L2_CID_MPEG_STREAM_TYPE:
  380. return v4l2_ctrl_query_fill(ctrl,
  381. V4L2_MPEG_STREAM_TYPE_MPEG2_DVD,
  382. V4L2_MPEG_STREAM_TYPE_MPEG_ELEM, 1,
  383. V4L2_MPEG_STREAM_TYPE_MPEG_ELEM);
  384. case V4L2_CID_MPEG_VIDEO_ENCODING:
  385. return v4l2_ctrl_query_fill(ctrl,
  386. V4L2_MPEG_VIDEO_ENCODING_MPEG_1,
  387. V4L2_MPEG_VIDEO_ENCODING_MPEG_4, 1,
  388. V4L2_MPEG_VIDEO_ENCODING_MPEG_2);
  389. case V4L2_CID_MPEG_VIDEO_ASPECT:
  390. return v4l2_ctrl_query_fill(ctrl,
  391. V4L2_MPEG_VIDEO_ASPECT_1x1,
  392. V4L2_MPEG_VIDEO_ASPECT_16x9, 1,
  393. V4L2_MPEG_VIDEO_ASPECT_1x1);
  394. case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
  395. return v4l2_ctrl_query_fill(ctrl, 0, 34, 1, 15);
  396. case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
  397. return v4l2_ctrl_query_fill(ctrl, 0, 1, 1, 0);
  398. case V4L2_CID_MPEG_VIDEO_BITRATE:
  399. return v4l2_ctrl_query_fill(ctrl,
  400. 64000,
  401. 10000000, 1,
  402. 1500000);
  403. default:
  404. return -EINVAL;
  405. }
  406. return 0;
  407. }
  408. static int mpeg_s_ctrl(struct v4l2_control *ctrl, struct go7007 *go)
  409. {
  410. /* pretty sure we can't change any of these while streaming */
  411. if (go->streaming)
  412. return -EBUSY;
  413. switch (ctrl->id) {
  414. case V4L2_CID_MPEG_STREAM_TYPE:
  415. switch (ctrl->value) {
  416. case V4L2_MPEG_STREAM_TYPE_MPEG2_DVD:
  417. go->format = GO7007_FORMAT_MPEG2;
  418. go->bitrate = 9800000;
  419. go->gop_size = 15;
  420. go->pali = 0x48;
  421. go->closed_gop = 1;
  422. go->repeat_seqhead = 0;
  423. go->seq_header_enable = 1;
  424. go->gop_header_enable = 1;
  425. go->dvd_mode = 1;
  426. break;
  427. case V4L2_MPEG_STREAM_TYPE_MPEG_ELEM:
  428. /* todo: */
  429. break;
  430. default:
  431. return -EINVAL;
  432. }
  433. break;
  434. case V4L2_CID_MPEG_VIDEO_ENCODING:
  435. switch (ctrl->value) {
  436. case V4L2_MPEG_VIDEO_ENCODING_MPEG_1:
  437. go->format = GO7007_FORMAT_MPEG1;
  438. go->pali = 0;
  439. break;
  440. case V4L2_MPEG_VIDEO_ENCODING_MPEG_2:
  441. go->format = GO7007_FORMAT_MPEG2;
  442. /*if (mpeg->pali >> 24 == 2)
  443. go->pali = mpeg->pali & 0xff;
  444. else*/
  445. go->pali = 0x48;
  446. break;
  447. case V4L2_MPEG_VIDEO_ENCODING_MPEG_4:
  448. go->format = GO7007_FORMAT_MPEG4;
  449. /*if (mpeg->pali >> 24 == 4)
  450. go->pali = mpeg->pali & 0xff;
  451. else*/
  452. go->pali = 0xf5;
  453. break;
  454. default:
  455. return -EINVAL;
  456. }
  457. go->gop_header_enable =
  458. /*mpeg->flags & GO7007_MPEG_OMIT_GOP_HEADER
  459. ? 0 :*/ 1;
  460. /*if (mpeg->flags & GO7007_MPEG_REPEAT_SEQHEADER)
  461. go->repeat_seqhead = 1;
  462. else*/
  463. go->repeat_seqhead = 0;
  464. go->dvd_mode = 0;
  465. break;
  466. case V4L2_CID_MPEG_VIDEO_ASPECT:
  467. if (go->format == GO7007_FORMAT_MJPEG)
  468. return -EINVAL;
  469. switch (ctrl->value) {
  470. case V4L2_MPEG_VIDEO_ASPECT_1x1:
  471. go->aspect_ratio = GO7007_RATIO_1_1;
  472. break;
  473. case V4L2_MPEG_VIDEO_ASPECT_4x3:
  474. go->aspect_ratio = GO7007_RATIO_4_3;
  475. break;
  476. case V4L2_MPEG_VIDEO_ASPECT_16x9:
  477. go->aspect_ratio = GO7007_RATIO_16_9;
  478. break;
  479. case V4L2_MPEG_VIDEO_ASPECT_221x100:
  480. default:
  481. return -EINVAL;
  482. }
  483. break;
  484. case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
  485. if (ctrl->value < 0 || ctrl->value > 34)
  486. return -EINVAL;
  487. go->gop_size = ctrl->value;
  488. break;
  489. case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
  490. if (ctrl->value != 0 && ctrl->value != 1)
  491. return -EINVAL;
  492. go->closed_gop = ctrl->value;
  493. break;
  494. case V4L2_CID_MPEG_VIDEO_BITRATE:
  495. /* Upper bound is kind of arbitrary here */
  496. if (ctrl->value < 64000 || ctrl->value > 10000000)
  497. return -EINVAL;
  498. go->bitrate = ctrl->value;
  499. break;
  500. default:
  501. return -EINVAL;
  502. }
  503. return 0;
  504. }
  505. static int mpeg_g_ctrl(struct v4l2_control *ctrl, struct go7007 *go)
  506. {
  507. switch (ctrl->id) {
  508. case V4L2_CID_MPEG_STREAM_TYPE:
  509. if (go->dvd_mode)
  510. ctrl->value = V4L2_MPEG_STREAM_TYPE_MPEG2_DVD;
  511. else
  512. ctrl->value = V4L2_MPEG_STREAM_TYPE_MPEG_ELEM;
  513. break;
  514. case V4L2_CID_MPEG_VIDEO_ENCODING:
  515. switch (go->format) {
  516. case GO7007_FORMAT_MPEG1:
  517. ctrl->value = V4L2_MPEG_VIDEO_ENCODING_MPEG_1;
  518. break;
  519. case GO7007_FORMAT_MPEG2:
  520. ctrl->value = V4L2_MPEG_VIDEO_ENCODING_MPEG_2;
  521. break;
  522. case GO7007_FORMAT_MPEG4:
  523. ctrl->value = V4L2_MPEG_VIDEO_ENCODING_MPEG_4;
  524. break;
  525. default:
  526. return -EINVAL;
  527. }
  528. break;
  529. case V4L2_CID_MPEG_VIDEO_ASPECT:
  530. switch (go->aspect_ratio) {
  531. case GO7007_RATIO_1_1:
  532. ctrl->value = V4L2_MPEG_VIDEO_ASPECT_1x1;
  533. break;
  534. case GO7007_RATIO_4_3:
  535. ctrl->value = V4L2_MPEG_VIDEO_ASPECT_4x3;
  536. break;
  537. case GO7007_RATIO_16_9:
  538. ctrl->value = V4L2_MPEG_VIDEO_ASPECT_16x9;
  539. break;
  540. default:
  541. return -EINVAL;
  542. }
  543. break;
  544. case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
  545. ctrl->value = go->gop_size;
  546. break;
  547. case V4L2_CID_MPEG_VIDEO_GOP_CLOSURE:
  548. ctrl->value = go->closed_gop;
  549. break;
  550. case V4L2_CID_MPEG_VIDEO_BITRATE:
  551. ctrl->value = go->bitrate;
  552. break;
  553. default:
  554. return -EINVAL;
  555. }
  556. return 0;
  557. }
  558. static int vidioc_querycap(struct file *file, void *priv,
  559. struct v4l2_capability *cap)
  560. {
  561. struct go7007 *go = ((struct go7007_file *) priv)->go;
  562. strlcpy(cap->driver, "go7007", sizeof(cap->driver));
  563. strlcpy(cap->card, go->name, sizeof(cap->card));
  564. #if 0
  565. strlcpy(cap->bus_info, dev_name(&dev->udev->dev), sizeof(cap->bus_info));
  566. #endif
  567. cap->version = KERNEL_VERSION(0, 9, 8);
  568. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
  569. V4L2_CAP_STREAMING; /* | V4L2_CAP_AUDIO; */
  570. if (go->board_info->flags & GO7007_BOARD_HAS_TUNER)
  571. cap->capabilities |= V4L2_CAP_TUNER;
  572. return 0;
  573. }
  574. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  575. struct v4l2_fmtdesc *fmt)
  576. {
  577. char *desc = NULL;
  578. switch (fmt->index) {
  579. case 0:
  580. fmt->pixelformat = V4L2_PIX_FMT_MJPEG;
  581. desc = "Motion-JPEG";
  582. break;
  583. case 1:
  584. fmt->pixelformat = V4L2_PIX_FMT_MPEG;
  585. desc = "MPEG1/MPEG2/MPEG4";
  586. break;
  587. default:
  588. return -EINVAL;
  589. }
  590. fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  591. fmt->flags = V4L2_FMT_FLAG_COMPRESSED;
  592. strncpy(fmt->description, desc, sizeof(fmt->description));
  593. return 0;
  594. }
  595. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  596. struct v4l2_format *fmt)
  597. {
  598. struct go7007 *go = ((struct go7007_file *) priv)->go;
  599. fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  600. fmt->fmt.pix.width = go->width;
  601. fmt->fmt.pix.height = go->height;
  602. fmt->fmt.pix.pixelformat = (go->format == GO7007_FORMAT_MJPEG) ?
  603. V4L2_PIX_FMT_MJPEG : V4L2_PIX_FMT_MPEG;
  604. fmt->fmt.pix.field = V4L2_FIELD_NONE;
  605. fmt->fmt.pix.bytesperline = 0;
  606. fmt->fmt.pix.sizeimage = GO7007_BUF_SIZE;
  607. fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  608. return 0;
  609. }
  610. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  611. struct v4l2_format *fmt)
  612. {
  613. struct go7007 *go = ((struct go7007_file *) priv)->go;
  614. return set_capture_size(go, fmt, 1);
  615. }
  616. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  617. struct v4l2_format *fmt)
  618. {
  619. struct go7007 *go = ((struct go7007_file *) priv)->go;
  620. if (go->streaming)
  621. return -EBUSY;
  622. return set_capture_size(go, fmt, 0);
  623. }
  624. static int vidioc_reqbufs(struct file *file, void *priv,
  625. struct v4l2_requestbuffers *req)
  626. {
  627. struct go7007_file *gofh = priv;
  628. struct go7007 *go = gofh->go;
  629. int retval = -EBUSY;
  630. unsigned int count, i;
  631. if (go->streaming)
  632. return retval;
  633. if (req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  634. req->memory != V4L2_MEMORY_MMAP)
  635. return -EINVAL;
  636. mutex_lock(&gofh->lock);
  637. for (i = 0; i < gofh->buf_count; ++i)
  638. if (gofh->bufs[i].mapped > 0)
  639. goto unlock_and_return;
  640. mutex_lock(&go->hw_lock);
  641. if (go->in_use > 0 && gofh->buf_count == 0) {
  642. mutex_unlock(&go->hw_lock);
  643. goto unlock_and_return;
  644. }
  645. if (gofh->buf_count > 0)
  646. kfree(gofh->bufs);
  647. retval = -ENOMEM;
  648. count = req->count;
  649. if (count > 0) {
  650. if (count < 2)
  651. count = 2;
  652. if (count > 32)
  653. count = 32;
  654. gofh->bufs = kcalloc(count, sizeof(struct go7007_buffer),
  655. GFP_KERNEL);
  656. if (!gofh->bufs) {
  657. mutex_unlock(&go->hw_lock);
  658. goto unlock_and_return;
  659. }
  660. for (i = 0; i < count; ++i) {
  661. gofh->bufs[i].go = go;
  662. gofh->bufs[i].index = i;
  663. gofh->bufs[i].state = BUF_STATE_IDLE;
  664. gofh->bufs[i].mapped = 0;
  665. }
  666. go->in_use = 1;
  667. } else {
  668. go->in_use = 0;
  669. }
  670. gofh->buf_count = count;
  671. mutex_unlock(&go->hw_lock);
  672. mutex_unlock(&gofh->lock);
  673. memset(req, 0, sizeof(*req));
  674. req->count = count;
  675. req->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  676. req->memory = V4L2_MEMORY_MMAP;
  677. return 0;
  678. unlock_and_return:
  679. mutex_unlock(&gofh->lock);
  680. return retval;
  681. }
  682. static int vidioc_querybuf(struct file *file, void *priv,
  683. struct v4l2_buffer *buf)
  684. {
  685. struct go7007_file *gofh = priv;
  686. int retval = -EINVAL;
  687. unsigned int index;
  688. if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  689. return retval;
  690. index = buf->index;
  691. mutex_lock(&gofh->lock);
  692. if (index >= gofh->buf_count)
  693. goto unlock_and_return;
  694. memset(buf, 0, sizeof(*buf));
  695. buf->index = index;
  696. buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  697. switch (gofh->bufs[index].state) {
  698. case BUF_STATE_QUEUED:
  699. buf->flags = V4L2_BUF_FLAG_QUEUED;
  700. break;
  701. case BUF_STATE_DONE:
  702. buf->flags = V4L2_BUF_FLAG_DONE;
  703. break;
  704. default:
  705. buf->flags = 0;
  706. }
  707. if (gofh->bufs[index].mapped)
  708. buf->flags |= V4L2_BUF_FLAG_MAPPED;
  709. buf->memory = V4L2_MEMORY_MMAP;
  710. buf->m.offset = index * GO7007_BUF_SIZE;
  711. buf->length = GO7007_BUF_SIZE;
  712. mutex_unlock(&gofh->lock);
  713. return 0;
  714. unlock_and_return:
  715. mutex_unlock(&gofh->lock);
  716. return retval;
  717. }
  718. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  719. {
  720. struct go7007_file *gofh = priv;
  721. struct go7007 *go = gofh->go;
  722. struct go7007_buffer *gobuf;
  723. unsigned long flags;
  724. int retval = -EINVAL;
  725. int ret;
  726. if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  727. buf->memory != V4L2_MEMORY_MMAP)
  728. return retval;
  729. mutex_lock(&gofh->lock);
  730. if (buf->index < 0 || buf->index >= gofh->buf_count)
  731. goto unlock_and_return;
  732. gobuf = &gofh->bufs[buf->index];
  733. if (!gobuf->mapped)
  734. goto unlock_and_return;
  735. retval = -EBUSY;
  736. if (gobuf->state != BUF_STATE_IDLE)
  737. goto unlock_and_return;
  738. /* offset will be 0 until we really support USERPTR streaming */
  739. gobuf->offset = gobuf->user_addr & ~PAGE_MASK;
  740. gobuf->bytesused = 0;
  741. gobuf->frame_offset = 0;
  742. gobuf->modet_active = 0;
  743. if (gobuf->offset > 0)
  744. gobuf->page_count = GO7007_BUF_PAGES + 1;
  745. else
  746. gobuf->page_count = GO7007_BUF_PAGES;
  747. retval = -ENOMEM;
  748. down_read(&current->mm->mmap_sem);
  749. ret = get_user_pages(current, current->mm,
  750. gobuf->user_addr & PAGE_MASK, gobuf->page_count,
  751. 1, 1, gobuf->pages, NULL);
  752. up_read(&current->mm->mmap_sem);
  753. if (ret != gobuf->page_count) {
  754. int i;
  755. for (i = 0; i < ret; ++i)
  756. page_cache_release(gobuf->pages[i]);
  757. gobuf->page_count = 0;
  758. goto unlock_and_return;
  759. }
  760. gobuf->state = BUF_STATE_QUEUED;
  761. spin_lock_irqsave(&go->spinlock, flags);
  762. list_add_tail(&gobuf->stream, &go->stream);
  763. spin_unlock_irqrestore(&go->spinlock, flags);
  764. mutex_unlock(&gofh->lock);
  765. return 0;
  766. unlock_and_return:
  767. mutex_unlock(&gofh->lock);
  768. return retval;
  769. }
  770. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  771. {
  772. struct go7007_file *gofh = priv;
  773. struct go7007 *go = gofh->go;
  774. struct go7007_buffer *gobuf;
  775. int retval = -EINVAL;
  776. unsigned long flags;
  777. u32 frame_type_flag;
  778. DEFINE_WAIT(wait);
  779. if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  780. return retval;
  781. if (buf->memory != V4L2_MEMORY_MMAP)
  782. return retval;
  783. mutex_lock(&gofh->lock);
  784. if (list_empty(&go->stream))
  785. goto unlock_and_return;
  786. gobuf = list_entry(go->stream.next,
  787. struct go7007_buffer, stream);
  788. retval = -EAGAIN;
  789. if (gobuf->state != BUF_STATE_DONE &&
  790. !(file->f_flags & O_NONBLOCK)) {
  791. for (;;) {
  792. prepare_to_wait(&go->frame_waitq, &wait,
  793. TASK_INTERRUPTIBLE);
  794. if (gobuf->state == BUF_STATE_DONE)
  795. break;
  796. if (signal_pending(current)) {
  797. retval = -ERESTARTSYS;
  798. break;
  799. }
  800. schedule();
  801. }
  802. finish_wait(&go->frame_waitq, &wait);
  803. }
  804. if (gobuf->state != BUF_STATE_DONE)
  805. goto unlock_and_return;
  806. spin_lock_irqsave(&go->spinlock, flags);
  807. deactivate_buffer(gobuf);
  808. spin_unlock_irqrestore(&go->spinlock, flags);
  809. frame_type_flag = get_frame_type_flag(gobuf, go->format);
  810. gobuf->state = BUF_STATE_IDLE;
  811. memset(buf, 0, sizeof(*buf));
  812. buf->index = gobuf->index;
  813. buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  814. buf->bytesused = gobuf->bytesused;
  815. buf->flags = V4L2_BUF_FLAG_MAPPED | frame_type_flag;
  816. buf->field = V4L2_FIELD_NONE;
  817. buf->timestamp = gobuf->timestamp;
  818. buf->sequence = gobuf->seq;
  819. buf->memory = V4L2_MEMORY_MMAP;
  820. buf->m.offset = gobuf->index * GO7007_BUF_SIZE;
  821. buf->length = GO7007_BUF_SIZE;
  822. buf->reserved = gobuf->modet_active;
  823. mutex_unlock(&gofh->lock);
  824. return 0;
  825. unlock_and_return:
  826. mutex_unlock(&gofh->lock);
  827. return retval;
  828. }
  829. static int vidioc_streamon(struct file *file, void *priv,
  830. enum v4l2_buf_type type)
  831. {
  832. struct go7007_file *gofh = priv;
  833. struct go7007 *go = gofh->go;
  834. int retval = 0;
  835. if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  836. return -EINVAL;
  837. mutex_lock(&gofh->lock);
  838. mutex_lock(&go->hw_lock);
  839. if (!go->streaming) {
  840. go->streaming = 1;
  841. go->next_seq = 0;
  842. go->active_buf = NULL;
  843. if (go7007_start_encoder(go) < 0)
  844. retval = -EIO;
  845. else
  846. retval = 0;
  847. }
  848. mutex_unlock(&go->hw_lock);
  849. mutex_unlock(&gofh->lock);
  850. return retval;
  851. }
  852. static int vidioc_streamoff(struct file *file, void *priv,
  853. enum v4l2_buf_type type)
  854. {
  855. struct go7007_file *gofh = priv;
  856. struct go7007 *go = gofh->go;
  857. if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  858. return -EINVAL;
  859. mutex_lock(&gofh->lock);
  860. go7007_streamoff(go);
  861. mutex_unlock(&gofh->lock);
  862. return 0;
  863. }
  864. static int vidioc_queryctrl(struct file *file, void *priv,
  865. struct v4l2_queryctrl *query)
  866. {
  867. struct go7007 *go = ((struct go7007_file *) priv)->go;
  868. int id = query->id;
  869. if (0 == call_all(&go->v4l2_dev, core, queryctrl, query))
  870. return 0;
  871. query->id = id;
  872. return mpeg_query_ctrl(query);
  873. }
  874. static int vidioc_g_ctrl(struct file *file, void *priv,
  875. struct v4l2_control *ctrl)
  876. {
  877. struct go7007 *go = ((struct go7007_file *) priv)->go;
  878. if (0 == call_all(&go->v4l2_dev, core, g_ctrl, ctrl))
  879. return 0;
  880. return mpeg_g_ctrl(ctrl, go);
  881. }
  882. static int vidioc_s_ctrl(struct file *file, void *priv,
  883. struct v4l2_control *ctrl)
  884. {
  885. struct go7007 *go = ((struct go7007_file *) priv)->go;
  886. if (0 == call_all(&go->v4l2_dev, core, s_ctrl, ctrl))
  887. return 0;
  888. return mpeg_s_ctrl(ctrl, go);
  889. }
  890. static int vidioc_g_parm(struct file *filp, void *priv,
  891. struct v4l2_streamparm *parm)
  892. {
  893. struct go7007 *go = ((struct go7007_file *) priv)->go;
  894. struct v4l2_fract timeperframe = {
  895. .numerator = 1001 * go->fps_scale,
  896. .denominator = go->sensor_framerate,
  897. };
  898. if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  899. return -EINVAL;
  900. parm->parm.capture.capability |= V4L2_CAP_TIMEPERFRAME;
  901. parm->parm.capture.timeperframe = timeperframe;
  902. return 0;
  903. }
  904. static int vidioc_s_parm(struct file *filp, void *priv,
  905. struct v4l2_streamparm *parm)
  906. {
  907. struct go7007 *go = ((struct go7007_file *) priv)->go;
  908. unsigned int n, d;
  909. if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  910. return -EINVAL;
  911. if (parm->parm.capture.capturemode != 0)
  912. return -EINVAL;
  913. n = go->sensor_framerate *
  914. parm->parm.capture.timeperframe.numerator;
  915. d = 1001 * parm->parm.capture.timeperframe.denominator;
  916. if (n != 0 && d != 0 && n > d)
  917. go->fps_scale = (n + d/2) / d;
  918. else
  919. go->fps_scale = 1;
  920. return 0;
  921. }
  922. /* VIDIOC_ENUMSTD on go7007 were used for enumberating the supported fps and
  923. its resolution, when the device is not connected to TV.
  924. This were an API abuse, probably used by the lack of specific IOCTL's to
  925. enumberate it, by the time the driver were written.
  926. However, since kernel 2.6.19, two new ioctls (VIDIOC_ENUM_FRAMEINTERVALS
  927. and VIDIOC_ENUM_FRAMESIZES) were added for this purpose.
  928. The two functions bellow implements the newer ioctls
  929. */
  930. static int vidioc_enum_framesizes(struct file *filp, void *priv,
  931. struct v4l2_frmsizeenum *fsize)
  932. {
  933. struct go7007 *go = ((struct go7007_file *) priv)->go;
  934. /* Return -EINVAL, if it is a TV board */
  935. if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) ||
  936. (go->board_info->sensor_flags & GO7007_SENSOR_TV))
  937. return -EINVAL;
  938. if (fsize->index > 0)
  939. return -EINVAL;
  940. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  941. fsize->discrete.width = go->board_info->sensor_width;
  942. fsize->discrete.height = go->board_info->sensor_height;
  943. return 0;
  944. }
  945. static int vidioc_enum_frameintervals(struct file *filp, void *priv,
  946. struct v4l2_frmivalenum *fival)
  947. {
  948. struct go7007 *go = ((struct go7007_file *) priv)->go;
  949. /* Return -EINVAL, if it is a TV board */
  950. if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) ||
  951. (go->board_info->sensor_flags & GO7007_SENSOR_TV))
  952. return -EINVAL;
  953. if (fival->index > 0)
  954. return -EINVAL;
  955. fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
  956. fival->discrete.numerator = 1001;
  957. fival->discrete.denominator = go->board_info->sensor_framerate;
  958. return 0;
  959. }
  960. static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *std)
  961. {
  962. struct go7007 *go = ((struct go7007_file *) priv)->go;
  963. switch (go->standard) {
  964. case GO7007_STD_NTSC:
  965. *std = V4L2_STD_NTSC;
  966. break;
  967. case GO7007_STD_PAL:
  968. *std = V4L2_STD_PAL;
  969. break;
  970. default:
  971. return -EINVAL;
  972. }
  973. return 0;
  974. }
  975. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *std)
  976. {
  977. struct go7007 *go = ((struct go7007_file *) priv)->go;
  978. if (go->streaming)
  979. return -EBUSY;
  980. if (!(go->board_info->sensor_flags & GO7007_SENSOR_TV) && *std != 0)
  981. return -EINVAL;
  982. if (*std == 0)
  983. return -EINVAL;
  984. if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) &&
  985. go->input == go->board_info->num_inputs - 1) {
  986. if (!go->i2c_adapter_online)
  987. return -EIO;
  988. if (call_all(&go->v4l2_dev, core, s_std, *std) < 0)
  989. return -EINVAL;
  990. }
  991. if (*std & V4L2_STD_NTSC) {
  992. go->standard = GO7007_STD_NTSC;
  993. go->sensor_framerate = 30000;
  994. } else if (*std & V4L2_STD_PAL) {
  995. go->standard = GO7007_STD_PAL;
  996. go->sensor_framerate = 25025;
  997. } else if (*std & V4L2_STD_SECAM) {
  998. go->standard = GO7007_STD_PAL;
  999. go->sensor_framerate = 25025;
  1000. } else
  1001. return -EINVAL;
  1002. call_all(&go->v4l2_dev, core, s_std, *std);
  1003. set_capture_size(go, NULL, 0);
  1004. return 0;
  1005. }
  1006. static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *std)
  1007. {
  1008. struct go7007 *go = ((struct go7007_file *) priv)->go;
  1009. if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) &&
  1010. go->input == go->board_info->num_inputs - 1) {
  1011. if (!go->i2c_adapter_online)
  1012. return -EIO;
  1013. return call_all(&go->v4l2_dev, video, querystd, std);
  1014. } else if (go->board_info->sensor_flags & GO7007_SENSOR_TV)
  1015. *std = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM;
  1016. else
  1017. *std = 0;
  1018. return 0;
  1019. }
  1020. static int vidioc_enum_input(struct file *file, void *priv,
  1021. struct v4l2_input *inp)
  1022. {
  1023. struct go7007 *go = ((struct go7007_file *) priv)->go;
  1024. if (inp->index >= go->board_info->num_inputs)
  1025. return -EINVAL;
  1026. strncpy(inp->name, go->board_info->inputs[inp->index].name,
  1027. sizeof(inp->name));
  1028. /* If this board has a tuner, it will be the last input */
  1029. if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) &&
  1030. inp->index == go->board_info->num_inputs - 1)
  1031. inp->type = V4L2_INPUT_TYPE_TUNER;
  1032. else
  1033. inp->type = V4L2_INPUT_TYPE_CAMERA;
  1034. inp->audioset = 0;
  1035. inp->tuner = 0;
  1036. if (go->board_info->sensor_flags & GO7007_SENSOR_TV)
  1037. inp->std = V4L2_STD_NTSC | V4L2_STD_PAL |
  1038. V4L2_STD_SECAM;
  1039. else
  1040. inp->std = 0;
  1041. return 0;
  1042. }
  1043. static int vidioc_g_input(struct file *file, void *priv, unsigned int *input)
  1044. {
  1045. struct go7007 *go = ((struct go7007_file *) priv)->go;
  1046. *input = go->input;
  1047. return 0;
  1048. }
  1049. static int vidioc_s_input(struct file *file, void *priv, unsigned int input)
  1050. {
  1051. struct go7007 *go = ((struct go7007_file *) priv)->go;
  1052. if (input >= go->board_info->num_inputs)
  1053. return -EINVAL;
  1054. if (go->streaming)
  1055. return -EBUSY;
  1056. go->input = input;
  1057. return call_all(&go->v4l2_dev, video, s_routing, input, 0, 0);
  1058. }
  1059. static int vidioc_g_tuner(struct file *file, void *priv,
  1060. struct v4l2_tuner *t)
  1061. {
  1062. struct go7007 *go = ((struct go7007_file *) priv)->go;
  1063. if (!(go->board_info->flags & GO7007_BOARD_HAS_TUNER))
  1064. return -EINVAL;
  1065. if (t->index != 0)
  1066. return -EINVAL;
  1067. if (!go->i2c_adapter_online)
  1068. return -EIO;
  1069. return call_all(&go->v4l2_dev, tuner, g_tuner, t);
  1070. }
  1071. static int vidioc_s_tuner(struct file *file, void *priv,
  1072. struct v4l2_tuner *t)
  1073. {
  1074. struct go7007 *go = ((struct go7007_file *) priv)->go;
  1075. if (!(go->board_info->flags & GO7007_BOARD_HAS_TUNER))
  1076. return -EINVAL;
  1077. if (t->index != 0)
  1078. return -EINVAL;
  1079. if (!go->i2c_adapter_online)
  1080. return -EIO;
  1081. switch (go->board_id) {
  1082. case GO7007_BOARDID_PX_TV402U_NA:
  1083. case GO7007_BOARDID_PX_TV402U_JP:
  1084. /* No selectable options currently */
  1085. if (t->audmode != V4L2_TUNER_MODE_STEREO)
  1086. return -EINVAL;
  1087. break;
  1088. }
  1089. return call_all(&go->v4l2_dev, tuner, s_tuner, t);
  1090. }
  1091. static int vidioc_g_frequency(struct file *file, void *priv,
  1092. struct v4l2_frequency *f)
  1093. {
  1094. struct go7007 *go = ((struct go7007_file *) priv)->go;
  1095. if (!(go->board_info->flags & GO7007_BOARD_HAS_TUNER))
  1096. return -EINVAL;
  1097. if (!go->i2c_adapter_online)
  1098. return -EIO;
  1099. f->type = V4L2_TUNER_ANALOG_TV;
  1100. return call_all(&go->v4l2_dev, tuner, g_frequency, f);
  1101. }
  1102. static int vidioc_s_frequency(struct file *file, void *priv,
  1103. struct v4l2_frequency *f)
  1104. {
  1105. struct go7007 *go = ((struct go7007_file *) priv)->go;
  1106. if (!(go->board_info->flags & GO7007_BOARD_HAS_TUNER))
  1107. return -EINVAL;
  1108. if (!go->i2c_adapter_online)
  1109. return -EIO;
  1110. return call_all(&go->v4l2_dev, tuner, s_frequency, f);
  1111. }
  1112. static int vidioc_cropcap(struct file *file, void *priv,
  1113. struct v4l2_cropcap *cropcap)
  1114. {
  1115. struct go7007 *go = ((struct go7007_file *) priv)->go;
  1116. if (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1117. return -EINVAL;
  1118. /* These specify the raw input of the sensor */
  1119. switch (go->standard) {
  1120. case GO7007_STD_NTSC:
  1121. cropcap->bounds.top = 0;
  1122. cropcap->bounds.left = 0;
  1123. cropcap->bounds.width = 720;
  1124. cropcap->bounds.height = 480;
  1125. cropcap->defrect.top = 0;
  1126. cropcap->defrect.left = 0;
  1127. cropcap->defrect.width = 720;
  1128. cropcap->defrect.height = 480;
  1129. break;
  1130. case GO7007_STD_PAL:
  1131. cropcap->bounds.top = 0;
  1132. cropcap->bounds.left = 0;
  1133. cropcap->bounds.width = 720;
  1134. cropcap->bounds.height = 576;
  1135. cropcap->defrect.top = 0;
  1136. cropcap->defrect.left = 0;
  1137. cropcap->defrect.width = 720;
  1138. cropcap->defrect.height = 576;
  1139. break;
  1140. case GO7007_STD_OTHER:
  1141. cropcap->bounds.top = 0;
  1142. cropcap->bounds.left = 0;
  1143. cropcap->bounds.width = go->board_info->sensor_width;
  1144. cropcap->bounds.height = go->board_info->sensor_height;
  1145. cropcap->defrect.top = 0;
  1146. cropcap->defrect.left = 0;
  1147. cropcap->defrect.width = go->board_info->sensor_width;
  1148. cropcap->defrect.height = go->board_info->sensor_height;
  1149. break;
  1150. }
  1151. return 0;
  1152. }
  1153. static int vidioc_g_crop(struct file *file, void *priv, struct v4l2_crop *crop)
  1154. {
  1155. struct go7007 *go = ((struct go7007_file *) priv)->go;
  1156. if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1157. return -EINVAL;
  1158. crop->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1159. /* These specify the raw input of the sensor */
  1160. switch (go->standard) {
  1161. case GO7007_STD_NTSC:
  1162. crop->c.top = 0;
  1163. crop->c.left = 0;
  1164. crop->c.width = 720;
  1165. crop->c.height = 480;
  1166. break;
  1167. case GO7007_STD_PAL:
  1168. crop->c.top = 0;
  1169. crop->c.left = 0;
  1170. crop->c.width = 720;
  1171. crop->c.height = 576;
  1172. break;
  1173. case GO7007_STD_OTHER:
  1174. crop->c.top = 0;
  1175. crop->c.left = 0;
  1176. crop->c.width = go->board_info->sensor_width;
  1177. crop->c.height = go->board_info->sensor_height;
  1178. break;
  1179. }
  1180. return 0;
  1181. }
  1182. /* FIXME: vidioc_s_crop is not really implemented!!!
  1183. */
  1184. static int vidioc_s_crop(struct file *file, void *priv, struct v4l2_crop *crop)
  1185. {
  1186. if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1187. return -EINVAL;
  1188. return 0;
  1189. }
  1190. static int vidioc_g_jpegcomp(struct file *file, void *priv,
  1191. struct v4l2_jpegcompression *params)
  1192. {
  1193. memset(params, 0, sizeof(*params));
  1194. params->quality = 50; /* ?? */
  1195. params->jpeg_markers = V4L2_JPEG_MARKER_DHT |
  1196. V4L2_JPEG_MARKER_DQT;
  1197. return 0;
  1198. }
  1199. static int vidioc_s_jpegcomp(struct file *file, void *priv,
  1200. struct v4l2_jpegcompression *params)
  1201. {
  1202. if (params->quality != 50 ||
  1203. params->jpeg_markers != (V4L2_JPEG_MARKER_DHT |
  1204. V4L2_JPEG_MARKER_DQT))
  1205. return -EINVAL;
  1206. return 0;
  1207. }
  1208. /* FIXME:
  1209. Those ioctls are private, and not needed, since several standard
  1210. extended controls already provide streaming control.
  1211. So, those ioctls should be converted into vidioc_g_ext_ctrls()
  1212. and vidioc_s_ext_ctrls()
  1213. */
  1214. #if 0
  1215. /* Temporary ioctls for controlling compression characteristics */
  1216. case GO7007IOC_S_BITRATE:
  1217. {
  1218. int *bitrate = arg;
  1219. if (go->streaming)
  1220. return -EINVAL;
  1221. /* Upper bound is kind of arbitrary here */
  1222. if (*bitrate < 64000 || *bitrate > 10000000)
  1223. return -EINVAL;
  1224. go->bitrate = *bitrate;
  1225. return 0;
  1226. }
  1227. case GO7007IOC_G_BITRATE:
  1228. {
  1229. int *bitrate = arg;
  1230. *bitrate = go->bitrate;
  1231. return 0;
  1232. }
  1233. case GO7007IOC_S_COMP_PARAMS:
  1234. {
  1235. struct go7007_comp_params *comp = arg;
  1236. if (go->format == GO7007_FORMAT_MJPEG)
  1237. return -EINVAL;
  1238. if (comp->gop_size > 0)
  1239. go->gop_size = comp->gop_size;
  1240. else
  1241. go->gop_size = go->sensor_framerate / 1000;
  1242. if (go->gop_size != 15)
  1243. go->dvd_mode = 0;
  1244. /*go->ipb = comp->max_b_frames > 0;*/ /* completely untested */
  1245. if (go->board_info->sensor_flags & GO7007_SENSOR_TV) {
  1246. switch (comp->aspect_ratio) {
  1247. case GO7007_ASPECT_RATIO_4_3_NTSC:
  1248. case GO7007_ASPECT_RATIO_4_3_PAL:
  1249. go->aspect_ratio = GO7007_RATIO_4_3;
  1250. break;
  1251. case GO7007_ASPECT_RATIO_16_9_NTSC:
  1252. case GO7007_ASPECT_RATIO_16_9_PAL:
  1253. go->aspect_ratio = GO7007_RATIO_16_9;
  1254. break;
  1255. default:
  1256. go->aspect_ratio = GO7007_RATIO_1_1;
  1257. break;
  1258. }
  1259. }
  1260. if (comp->flags & GO7007_COMP_OMIT_SEQ_HEADER) {
  1261. go->dvd_mode = 0;
  1262. go->seq_header_enable = 0;
  1263. } else {
  1264. go->seq_header_enable = 1;
  1265. }
  1266. /* fall-through */
  1267. }
  1268. case GO7007IOC_G_COMP_PARAMS:
  1269. {
  1270. struct go7007_comp_params *comp = arg;
  1271. if (go->format == GO7007_FORMAT_MJPEG)
  1272. return -EINVAL;
  1273. memset(comp, 0, sizeof(*comp));
  1274. comp->gop_size = go->gop_size;
  1275. comp->max_b_frames = go->ipb ? 2 : 0;
  1276. switch (go->aspect_ratio) {
  1277. case GO7007_RATIO_4_3:
  1278. if (go->standard == GO7007_STD_NTSC)
  1279. comp->aspect_ratio =
  1280. GO7007_ASPECT_RATIO_4_3_NTSC;
  1281. else
  1282. comp->aspect_ratio =
  1283. GO7007_ASPECT_RATIO_4_3_PAL;
  1284. break;
  1285. case GO7007_RATIO_16_9:
  1286. if (go->standard == GO7007_STD_NTSC)
  1287. comp->aspect_ratio =
  1288. GO7007_ASPECT_RATIO_16_9_NTSC;
  1289. else
  1290. comp->aspect_ratio =
  1291. GO7007_ASPECT_RATIO_16_9_PAL;
  1292. break;
  1293. default:
  1294. comp->aspect_ratio = GO7007_ASPECT_RATIO_1_1;
  1295. break;
  1296. }
  1297. if (go->closed_gop)
  1298. comp->flags |= GO7007_COMP_CLOSED_GOP;
  1299. if (!go->seq_header_enable)
  1300. comp->flags |= GO7007_COMP_OMIT_SEQ_HEADER;
  1301. return 0;
  1302. }
  1303. case GO7007IOC_S_MPEG_PARAMS:
  1304. {
  1305. struct go7007_mpeg_params *mpeg = arg;
  1306. if (go->format != GO7007_FORMAT_MPEG1 &&
  1307. go->format != GO7007_FORMAT_MPEG2 &&
  1308. go->format != GO7007_FORMAT_MPEG4)
  1309. return -EINVAL;
  1310. if (mpeg->flags & GO7007_MPEG_FORCE_DVD_MODE) {
  1311. go->format = GO7007_FORMAT_MPEG2;
  1312. go->bitrate = 9800000;
  1313. go->gop_size = 15;
  1314. go->pali = 0x48;
  1315. go->closed_gop = 1;
  1316. go->repeat_seqhead = 0;
  1317. go->seq_header_enable = 1;
  1318. go->gop_header_enable = 1;
  1319. go->dvd_mode = 1;
  1320. } else {
  1321. switch (mpeg->mpeg_video_standard) {
  1322. case GO7007_MPEG_VIDEO_MPEG1:
  1323. go->format = GO7007_FORMAT_MPEG1;
  1324. go->pali = 0;
  1325. break;
  1326. case GO7007_MPEG_VIDEO_MPEG2:
  1327. go->format = GO7007_FORMAT_MPEG2;
  1328. if (mpeg->pali >> 24 == 2)
  1329. go->pali = mpeg->pali & 0xff;
  1330. else
  1331. go->pali = 0x48;
  1332. break;
  1333. case GO7007_MPEG_VIDEO_MPEG4:
  1334. go->format = GO7007_FORMAT_MPEG4;
  1335. if (mpeg->pali >> 24 == 4)
  1336. go->pali = mpeg->pali & 0xff;
  1337. else
  1338. go->pali = 0xf5;
  1339. break;
  1340. default:
  1341. return -EINVAL;
  1342. }
  1343. go->gop_header_enable =
  1344. mpeg->flags & GO7007_MPEG_OMIT_GOP_HEADER
  1345. ? 0 : 1;
  1346. if (mpeg->flags & GO7007_MPEG_REPEAT_SEQHEADER)
  1347. go->repeat_seqhead = 1;
  1348. else
  1349. go->repeat_seqhead = 0;
  1350. go->dvd_mode = 0;
  1351. }
  1352. /* fall-through */
  1353. }
  1354. case GO7007IOC_G_MPEG_PARAMS:
  1355. {
  1356. struct go7007_mpeg_params *mpeg = arg;
  1357. memset(mpeg, 0, sizeof(*mpeg));
  1358. switch (go->format) {
  1359. case GO7007_FORMAT_MPEG1:
  1360. mpeg->mpeg_video_standard = GO7007_MPEG_VIDEO_MPEG1;
  1361. mpeg->pali = 0;
  1362. break;
  1363. case GO7007_FORMAT_MPEG2:
  1364. mpeg->mpeg_video_standard = GO7007_MPEG_VIDEO_MPEG2;
  1365. mpeg->pali = GO7007_MPEG_PROFILE(2, go->pali);
  1366. break;
  1367. case GO7007_FORMAT_MPEG4:
  1368. mpeg->mpeg_video_standard = GO7007_MPEG_VIDEO_MPEG4;
  1369. mpeg->pali = GO7007_MPEG_PROFILE(4, go->pali);
  1370. break;
  1371. default:
  1372. return -EINVAL;
  1373. }
  1374. if (!go->gop_header_enable)
  1375. mpeg->flags |= GO7007_MPEG_OMIT_GOP_HEADER;
  1376. if (go->repeat_seqhead)
  1377. mpeg->flags |= GO7007_MPEG_REPEAT_SEQHEADER;
  1378. if (go->dvd_mode)
  1379. mpeg->flags |= GO7007_MPEG_FORCE_DVD_MODE;
  1380. return 0;
  1381. }
  1382. case GO7007IOC_S_MD_PARAMS:
  1383. {
  1384. struct go7007_md_params *mdp = arg;
  1385. if (mdp->region > 3)
  1386. return -EINVAL;
  1387. if (mdp->trigger > 0) {
  1388. go->modet[mdp->region].pixel_threshold =
  1389. mdp->pixel_threshold >> 1;
  1390. go->modet[mdp->region].motion_threshold =
  1391. mdp->motion_threshold >> 1;
  1392. go->modet[mdp->region].mb_threshold =
  1393. mdp->trigger >> 1;
  1394. go->modet[mdp->region].enable = 1;
  1395. } else
  1396. go->modet[mdp->region].enable = 0;
  1397. /* fall-through */
  1398. }
  1399. case GO7007IOC_G_MD_PARAMS:
  1400. {
  1401. struct go7007_md_params *mdp = arg;
  1402. int region = mdp->region;
  1403. if (mdp->region > 3)
  1404. return -EINVAL;
  1405. memset(mdp, 0, sizeof(struct go7007_md_params));
  1406. mdp->region = region;
  1407. if (!go->modet[region].enable)
  1408. return 0;
  1409. mdp->pixel_threshold =
  1410. (go->modet[region].pixel_threshold << 1) + 1;
  1411. mdp->motion_threshold =
  1412. (go->modet[region].motion_threshold << 1) + 1;
  1413. mdp->trigger =
  1414. (go->modet[region].mb_threshold << 1) + 1;
  1415. return 0;
  1416. }
  1417. case GO7007IOC_S_MD_REGION:
  1418. {
  1419. struct go7007_md_region *region = arg;
  1420. if (region->region < 1 || region->region > 3)
  1421. return -EINVAL;
  1422. return clip_to_modet_map(go, region->region, region->clips);
  1423. }
  1424. #endif
  1425. static ssize_t go7007_read(struct file *file, char __user *data,
  1426. size_t count, loff_t *ppos)
  1427. {
  1428. return -EINVAL;
  1429. }
  1430. static void go7007_vm_open(struct vm_area_struct *vma)
  1431. {
  1432. struct go7007_buffer *gobuf = vma->vm_private_data;
  1433. ++gobuf->mapped;
  1434. }
  1435. static void go7007_vm_close(struct vm_area_struct *vma)
  1436. {
  1437. struct go7007_buffer *gobuf = vma->vm_private_data;
  1438. unsigned long flags;
  1439. if (--gobuf->mapped == 0) {
  1440. spin_lock_irqsave(&gobuf->go->spinlock, flags);
  1441. deactivate_buffer(gobuf);
  1442. spin_unlock_irqrestore(&gobuf->go->spinlock, flags);
  1443. }
  1444. }
  1445. /* Copied from videobuf-dma-sg.c */
  1446. static int go7007_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1447. {
  1448. struct page *page;
  1449. page = alloc_page(GFP_USER | __GFP_DMA32);
  1450. if (!page)
  1451. return VM_FAULT_OOM;
  1452. clear_user_highpage(page, (unsigned long)vmf->virtual_address);
  1453. vmf->page = page;
  1454. return 0;
  1455. }
  1456. static struct vm_operations_struct go7007_vm_ops = {
  1457. .open = go7007_vm_open,
  1458. .close = go7007_vm_close,
  1459. .fault = go7007_vm_fault,
  1460. };
  1461. static int go7007_mmap(struct file *file, struct vm_area_struct *vma)
  1462. {
  1463. struct go7007_file *gofh = file->private_data;
  1464. unsigned int index;
  1465. if (gofh->go->status != STATUS_ONLINE)
  1466. return -EIO;
  1467. if (!(vma->vm_flags & VM_SHARED))
  1468. return -EINVAL; /* only support VM_SHARED mapping */
  1469. if (vma->vm_end - vma->vm_start != GO7007_BUF_SIZE)
  1470. return -EINVAL; /* must map exactly one full buffer */
  1471. mutex_lock(&gofh->lock);
  1472. index = vma->vm_pgoff / GO7007_BUF_PAGES;
  1473. if (index >= gofh->buf_count) {
  1474. mutex_unlock(&gofh->lock);
  1475. return -EINVAL; /* trying to map beyond requested buffers */
  1476. }
  1477. if (index * GO7007_BUF_PAGES != vma->vm_pgoff) {
  1478. mutex_unlock(&gofh->lock);
  1479. return -EINVAL; /* offset is not aligned on buffer boundary */
  1480. }
  1481. if (gofh->bufs[index].mapped > 0) {
  1482. mutex_unlock(&gofh->lock);
  1483. return -EBUSY;
  1484. }
  1485. gofh->bufs[index].mapped = 1;
  1486. gofh->bufs[index].user_addr = vma->vm_start;
  1487. vma->vm_ops = &go7007_vm_ops;
  1488. vma->vm_flags |= VM_DONTEXPAND;
  1489. vma->vm_flags &= ~VM_IO;
  1490. vma->vm_private_data = &gofh->bufs[index];
  1491. mutex_unlock(&gofh->lock);
  1492. return 0;
  1493. }
  1494. static unsigned int go7007_poll(struct file *file, poll_table *wait)
  1495. {
  1496. struct go7007_file *gofh = file->private_data;
  1497. struct go7007_buffer *gobuf;
  1498. if (list_empty(&gofh->go->stream))
  1499. return POLLERR;
  1500. gobuf = list_entry(gofh->go->stream.next, struct go7007_buffer, stream);
  1501. poll_wait(file, &gofh->go->frame_waitq, wait);
  1502. if (gobuf->state == BUF_STATE_DONE)
  1503. return POLLIN | POLLRDNORM;
  1504. return 0;
  1505. }
  1506. static void go7007_vfl_release(struct video_device *vfd)
  1507. {
  1508. struct go7007 *go = video_get_drvdata(vfd);
  1509. video_device_release(vfd);
  1510. if (--go->ref_count == 0)
  1511. kfree(go);
  1512. }
  1513. static struct v4l2_file_operations go7007_fops = {
  1514. .owner = THIS_MODULE,
  1515. .open = go7007_open,
  1516. .release = go7007_release,
  1517. .ioctl = video_ioctl2,
  1518. .read = go7007_read,
  1519. .mmap = go7007_mmap,
  1520. .poll = go7007_poll,
  1521. };
  1522. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1523. .vidioc_querycap = vidioc_querycap,
  1524. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1525. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1526. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1527. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1528. .vidioc_reqbufs = vidioc_reqbufs,
  1529. .vidioc_querybuf = vidioc_querybuf,
  1530. .vidioc_qbuf = vidioc_qbuf,
  1531. .vidioc_dqbuf = vidioc_dqbuf,
  1532. .vidioc_g_std = vidioc_g_std,
  1533. .vidioc_s_std = vidioc_s_std,
  1534. .vidioc_querystd = vidioc_querystd,
  1535. .vidioc_enum_input = vidioc_enum_input,
  1536. .vidioc_g_input = vidioc_g_input,
  1537. .vidioc_s_input = vidioc_s_input,
  1538. .vidioc_queryctrl = vidioc_queryctrl,
  1539. .vidioc_g_ctrl = vidioc_g_ctrl,
  1540. .vidioc_s_ctrl = vidioc_s_ctrl,
  1541. .vidioc_streamon = vidioc_streamon,
  1542. .vidioc_streamoff = vidioc_streamoff,
  1543. .vidioc_g_tuner = vidioc_g_tuner,
  1544. .vidioc_s_tuner = vidioc_s_tuner,
  1545. .vidioc_g_frequency = vidioc_g_frequency,
  1546. .vidioc_s_frequency = vidioc_s_frequency,
  1547. .vidioc_g_parm = vidioc_g_parm,
  1548. .vidioc_s_parm = vidioc_s_parm,
  1549. .vidioc_enum_framesizes = vidioc_enum_framesizes,
  1550. .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
  1551. .vidioc_cropcap = vidioc_cropcap,
  1552. .vidioc_g_crop = vidioc_g_crop,
  1553. .vidioc_s_crop = vidioc_s_crop,
  1554. .vidioc_g_jpegcomp = vidioc_g_jpegcomp,
  1555. .vidioc_s_jpegcomp = vidioc_s_jpegcomp,
  1556. };
  1557. static struct video_device go7007_template = {
  1558. .name = "go7007",
  1559. .fops = &go7007_fops,
  1560. .release = go7007_vfl_release,
  1561. .ioctl_ops = &video_ioctl_ops,
  1562. .tvnorms = V4L2_STD_ALL,
  1563. .current_norm = V4L2_STD_NTSC,
  1564. };
  1565. int go7007_v4l2_init(struct go7007 *go)
  1566. {
  1567. int rv;
  1568. go->video_dev = video_device_alloc();
  1569. if (go->video_dev == NULL)
  1570. return -ENOMEM;
  1571. *go->video_dev = go7007_template;
  1572. go->video_dev->parent = go->dev;
  1573. rv = video_register_device(go->video_dev, VFL_TYPE_GRABBER, -1);
  1574. if (rv < 0) {
  1575. video_device_release(go->video_dev);
  1576. go->video_dev = NULL;
  1577. return rv;
  1578. }
  1579. rv = v4l2_device_register(go->dev, &go->v4l2_dev);
  1580. if (rv < 0) {
  1581. video_device_release(go->video_dev);
  1582. go->video_dev = NULL;
  1583. return rv;
  1584. }
  1585. video_set_drvdata(go->video_dev, go);
  1586. ++go->ref_count;
  1587. printk(KERN_INFO "%s: registered device %s [v4l2]\n",
  1588. go->video_dev->name, video_device_node_name(go->video_dev));
  1589. return 0;
  1590. }
  1591. void go7007_v4l2_remove(struct go7007 *go)
  1592. {
  1593. unsigned long flags;
  1594. mutex_lock(&go->hw_lock);
  1595. if (go->streaming) {
  1596. go->streaming = 0;
  1597. go7007_stream_stop(go);
  1598. spin_lock_irqsave(&go->spinlock, flags);
  1599. abort_queued(go);
  1600. spin_unlock_irqrestore(&go->spinlock, flags);
  1601. }
  1602. mutex_unlock(&go->hw_lock);
  1603. if (go->video_dev)
  1604. video_unregister_device(go->video_dev);
  1605. v4l2_device_unregister(&go->v4l2_dev);
  1606. }