vpif_capture.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170
  1. /*
  2. * Copyright (C) 2009 Texas Instruments 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 as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. * TODO : add support for VBI & HBI data service
  19. * add static buffer allocation
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/init.h>
  23. #include <linux/module.h>
  24. #include <linux/errno.h>
  25. #include <linux/fs.h>
  26. #include <linux/mm.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/workqueue.h>
  29. #include <linux/string.h>
  30. #include <linux/videodev2.h>
  31. #include <linux/wait.h>
  32. #include <linux/time.h>
  33. #include <linux/i2c.h>
  34. #include <linux/platform_device.h>
  35. #include <linux/io.h>
  36. #include <linux/version.h>
  37. #include <linux/slab.h>
  38. #include <media/v4l2-device.h>
  39. #include <media/v4l2-ioctl.h>
  40. #include "vpif_capture.h"
  41. #include "vpif.h"
  42. MODULE_DESCRIPTION("TI DaVinci VPIF Capture driver");
  43. MODULE_LICENSE("GPL");
  44. #define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
  45. #define vpif_dbg(level, debug, fmt, arg...) \
  46. v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
  47. static int debug = 1;
  48. static u32 ch0_numbuffers = 3;
  49. static u32 ch1_numbuffers = 3;
  50. static u32 ch0_bufsize = 1920 * 1080 * 2;
  51. static u32 ch1_bufsize = 720 * 576 * 2;
  52. module_param(debug, int, 0644);
  53. module_param(ch0_numbuffers, uint, S_IRUGO);
  54. module_param(ch1_numbuffers, uint, S_IRUGO);
  55. module_param(ch0_bufsize, uint, S_IRUGO);
  56. module_param(ch1_bufsize, uint, S_IRUGO);
  57. MODULE_PARM_DESC(debug, "Debug level 0-1");
  58. MODULE_PARM_DESC(ch2_numbuffers, "Channel0 buffer count (default:3)");
  59. MODULE_PARM_DESC(ch3_numbuffers, "Channel1 buffer count (default:3)");
  60. MODULE_PARM_DESC(ch2_bufsize, "Channel0 buffer size (default:1920 x 1080 x 2)");
  61. MODULE_PARM_DESC(ch3_bufsize, "Channel1 buffer size (default:720 x 576 x 2)");
  62. static struct vpif_config_params config_params = {
  63. .min_numbuffers = 3,
  64. .numbuffers[0] = 3,
  65. .numbuffers[1] = 3,
  66. .min_bufsize[0] = 720 * 480 * 2,
  67. .min_bufsize[1] = 720 * 480 * 2,
  68. .channel_bufsize[0] = 1920 * 1080 * 2,
  69. .channel_bufsize[1] = 720 * 576 * 2,
  70. };
  71. /* global variables */
  72. static struct vpif_device vpif_obj = { {NULL} };
  73. static struct device *vpif_dev;
  74. /**
  75. * ch_params: video standard configuration parameters for vpif
  76. */
  77. static const struct vpif_channel_config_params ch_params[] = {
  78. {
  79. "NTSC_M", 720, 480, 30, 0, 1, 268, 1440, 1, 23, 263, 266,
  80. 286, 525, 525, 0, 1, 0, V4L2_STD_525_60,
  81. },
  82. {
  83. "PAL_BDGHIK", 720, 576, 25, 0, 1, 280, 1440, 1, 23, 311, 313,
  84. 336, 624, 625, 0, 1, 0, V4L2_STD_625_50,
  85. },
  86. };
  87. /**
  88. * vpif_uservirt_to_phys : translate user/virtual address to phy address
  89. * @virtp: user/virtual address
  90. *
  91. * This inline function is used to convert user space virtual address to
  92. * physical address.
  93. */
  94. static inline u32 vpif_uservirt_to_phys(u32 virtp)
  95. {
  96. unsigned long physp = 0;
  97. struct mm_struct *mm = current->mm;
  98. struct vm_area_struct *vma;
  99. vma = find_vma(mm, virtp);
  100. /* For kernel direct-mapped memory, take the easy way */
  101. if (virtp >= PAGE_OFFSET)
  102. physp = virt_to_phys((void *)virtp);
  103. else if (vma && (vma->vm_flags & VM_IO) && (vma->vm_pgoff))
  104. /**
  105. * this will catch, kernel-allocated, mmaped-to-usermode
  106. * addresses
  107. */
  108. physp = (vma->vm_pgoff << PAGE_SHIFT) + (virtp - vma->vm_start);
  109. else {
  110. /* otherwise, use get_user_pages() for general userland pages */
  111. int res, nr_pages = 1;
  112. struct page *pages;
  113. down_read(&current->mm->mmap_sem);
  114. res = get_user_pages(current, current->mm,
  115. virtp, nr_pages, 1, 0, &pages, NULL);
  116. up_read(&current->mm->mmap_sem);
  117. if (res == nr_pages)
  118. physp = __pa(page_address(&pages[0]) +
  119. (virtp & ~PAGE_MASK));
  120. else {
  121. vpif_err("get_user_pages failed\n");
  122. return 0;
  123. }
  124. }
  125. return physp;
  126. }
  127. /**
  128. * buffer_prepare : callback function for buffer prepare
  129. * @q : buffer queue ptr
  130. * @vb: ptr to video buffer
  131. * @field: field info
  132. *
  133. * This is the callback function for buffer prepare when videobuf_qbuf()
  134. * function is called. The buffer is prepared and user space virtual address
  135. * or user address is converted into physical address
  136. */
  137. static int vpif_buffer_prepare(struct videobuf_queue *q,
  138. struct videobuf_buffer *vb,
  139. enum v4l2_field field)
  140. {
  141. /* Get the file handle object and channel object */
  142. struct vpif_fh *fh = q->priv_data;
  143. struct channel_obj *ch = fh->channel;
  144. struct common_obj *common;
  145. unsigned long addr;
  146. vpif_dbg(2, debug, "vpif_buffer_prepare\n");
  147. common = &ch->common[VPIF_VIDEO_INDEX];
  148. /* If buffer is not initialized, initialize it */
  149. if (VIDEOBUF_NEEDS_INIT == vb->state) {
  150. vb->width = common->width;
  151. vb->height = common->height;
  152. vb->size = vb->width * vb->height;
  153. vb->field = field;
  154. }
  155. vb->state = VIDEOBUF_PREPARED;
  156. /**
  157. * if user pointer memory mechanism is used, get the physical
  158. * address of the buffer
  159. */
  160. if (V4L2_MEMORY_USERPTR == common->memory) {
  161. if (0 == vb->baddr) {
  162. vpif_dbg(1, debug, "buffer address is 0\n");
  163. return -EINVAL;
  164. }
  165. vb->boff = vpif_uservirt_to_phys(vb->baddr);
  166. if (!IS_ALIGNED(vb->boff, 8))
  167. goto exit;
  168. }
  169. addr = vb->boff;
  170. if (q->streaming) {
  171. if (!IS_ALIGNED((addr + common->ytop_off), 8) ||
  172. !IS_ALIGNED((addr + common->ybtm_off), 8) ||
  173. !IS_ALIGNED((addr + common->ctop_off), 8) ||
  174. !IS_ALIGNED((addr + common->cbtm_off), 8))
  175. goto exit;
  176. }
  177. return 0;
  178. exit:
  179. vpif_dbg(1, debug, "buffer_prepare:offset is not aligned to 8 bytes\n");
  180. return -EINVAL;
  181. }
  182. /**
  183. * vpif_buffer_setup : Callback function for buffer setup.
  184. * @q: buffer queue ptr
  185. * @count: number of buffers
  186. * @size: size of the buffer
  187. *
  188. * This callback function is called when reqbuf() is called to adjust
  189. * the buffer count and buffer size
  190. */
  191. static int vpif_buffer_setup(struct videobuf_queue *q, unsigned int *count,
  192. unsigned int *size)
  193. {
  194. /* Get the file handle object and channel object */
  195. struct vpif_fh *fh = q->priv_data;
  196. struct channel_obj *ch = fh->channel;
  197. struct common_obj *common;
  198. common = &ch->common[VPIF_VIDEO_INDEX];
  199. vpif_dbg(2, debug, "vpif_buffer_setup\n");
  200. /* If memory type is not mmap, return */
  201. if (V4L2_MEMORY_MMAP != common->memory)
  202. return 0;
  203. /* Calculate the size of the buffer */
  204. *size = config_params.channel_bufsize[ch->channel_id];
  205. if (*count < config_params.min_numbuffers)
  206. *count = config_params.min_numbuffers;
  207. return 0;
  208. }
  209. /**
  210. * vpif_buffer_queue : Callback function to add buffer to DMA queue
  211. * @q: ptr to videobuf_queue
  212. * @vb: ptr to videobuf_buffer
  213. */
  214. static void vpif_buffer_queue(struct videobuf_queue *q,
  215. struct videobuf_buffer *vb)
  216. {
  217. /* Get the file handle object and channel object */
  218. struct vpif_fh *fh = q->priv_data;
  219. struct channel_obj *ch = fh->channel;
  220. struct common_obj *common;
  221. common = &ch->common[VPIF_VIDEO_INDEX];
  222. vpif_dbg(2, debug, "vpif_buffer_queue\n");
  223. /* add the buffer to the DMA queue */
  224. list_add_tail(&vb->queue, &common->dma_queue);
  225. /* Change state of the buffer */
  226. vb->state = VIDEOBUF_QUEUED;
  227. }
  228. /**
  229. * vpif_buffer_release : Callback function to free buffer
  230. * @q: buffer queue ptr
  231. * @vb: ptr to video buffer
  232. *
  233. * This function is called from the videobuf layer to free memory
  234. * allocated to the buffers
  235. */
  236. static void vpif_buffer_release(struct videobuf_queue *q,
  237. struct videobuf_buffer *vb)
  238. {
  239. /* Get the file handle object and channel object */
  240. struct vpif_fh *fh = q->priv_data;
  241. struct channel_obj *ch = fh->channel;
  242. struct common_obj *common;
  243. common = &ch->common[VPIF_VIDEO_INDEX];
  244. videobuf_dma_contig_free(q, vb);
  245. vb->state = VIDEOBUF_NEEDS_INIT;
  246. }
  247. static struct videobuf_queue_ops video_qops = {
  248. .buf_setup = vpif_buffer_setup,
  249. .buf_prepare = vpif_buffer_prepare,
  250. .buf_queue = vpif_buffer_queue,
  251. .buf_release = vpif_buffer_release,
  252. };
  253. static u8 channel_first_int[VPIF_NUMBER_OF_OBJECTS][2] =
  254. { {1, 1} };
  255. /**
  256. * vpif_process_buffer_complete: process a completed buffer
  257. * @common: ptr to common channel object
  258. *
  259. * This function time stamp the buffer and mark it as DONE. It also
  260. * wake up any process waiting on the QUEUE and set the next buffer
  261. * as current
  262. */
  263. static void vpif_process_buffer_complete(struct common_obj *common)
  264. {
  265. do_gettimeofday(&common->cur_frm->ts);
  266. common->cur_frm->state = VIDEOBUF_DONE;
  267. wake_up_interruptible(&common->cur_frm->done);
  268. /* Make curFrm pointing to nextFrm */
  269. common->cur_frm = common->next_frm;
  270. }
  271. /**
  272. * vpif_schedule_next_buffer: set next buffer address for capture
  273. * @common : ptr to common channel object
  274. *
  275. * This function will get next buffer from the dma queue and
  276. * set the buffer address in the vpif register for capture.
  277. * the buffer is marked active
  278. */
  279. static void vpif_schedule_next_buffer(struct common_obj *common)
  280. {
  281. unsigned long addr = 0;
  282. common->next_frm = list_entry(common->dma_queue.next,
  283. struct videobuf_buffer, queue);
  284. /* Remove that buffer from the buffer queue */
  285. list_del(&common->next_frm->queue);
  286. common->next_frm->state = VIDEOBUF_ACTIVE;
  287. if (V4L2_MEMORY_USERPTR == common->memory)
  288. addr = common->next_frm->boff;
  289. else
  290. addr = videobuf_to_dma_contig(common->next_frm);
  291. /* Set top and bottom field addresses in VPIF registers */
  292. common->set_addr(addr + common->ytop_off,
  293. addr + common->ybtm_off,
  294. addr + common->ctop_off,
  295. addr + common->cbtm_off);
  296. }
  297. /**
  298. * vpif_channel_isr : ISR handler for vpif capture
  299. * @irq: irq number
  300. * @dev_id: dev_id ptr
  301. *
  302. * It changes status of the captured buffer, takes next buffer from the queue
  303. * and sets its address in VPIF registers
  304. */
  305. static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
  306. {
  307. struct vpif_device *dev = &vpif_obj;
  308. struct common_obj *common;
  309. struct channel_obj *ch;
  310. enum v4l2_field field;
  311. int channel_id = 0;
  312. int fid = -1, i;
  313. channel_id = *(int *)(dev_id);
  314. ch = dev->dev[channel_id];
  315. field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
  316. for (i = 0; i < VPIF_NUMBER_OF_OBJECTS; i++) {
  317. common = &ch->common[i];
  318. /* skip If streaming is not started in this channel */
  319. if (0 == common->started)
  320. continue;
  321. /* Check the field format */
  322. if (1 == ch->vpifparams.std_info.frm_fmt) {
  323. /* Progressive mode */
  324. if (list_empty(&common->dma_queue))
  325. continue;
  326. if (!channel_first_int[i][channel_id])
  327. vpif_process_buffer_complete(common);
  328. channel_first_int[i][channel_id] = 0;
  329. vpif_schedule_next_buffer(common);
  330. channel_first_int[i][channel_id] = 0;
  331. } else {
  332. /**
  333. * Interlaced mode. If it is first interrupt, ignore
  334. * it
  335. */
  336. if (channel_first_int[i][channel_id]) {
  337. channel_first_int[i][channel_id] = 0;
  338. continue;
  339. }
  340. if (0 == i) {
  341. ch->field_id ^= 1;
  342. /* Get field id from VPIF registers */
  343. fid = vpif_channel_getfid(ch->channel_id);
  344. if (fid != ch->field_id) {
  345. /**
  346. * If field id does not match stored
  347. * field id, make them in sync
  348. */
  349. if (0 == fid)
  350. ch->field_id = fid;
  351. return IRQ_HANDLED;
  352. }
  353. }
  354. /* device field id and local field id are in sync */
  355. if (0 == fid) {
  356. /* this is even field */
  357. if (common->cur_frm == common->next_frm)
  358. continue;
  359. /* mark the current buffer as done */
  360. vpif_process_buffer_complete(common);
  361. } else if (1 == fid) {
  362. /* odd field */
  363. if (list_empty(&common->dma_queue) ||
  364. (common->cur_frm != common->next_frm))
  365. continue;
  366. vpif_schedule_next_buffer(common);
  367. }
  368. }
  369. }
  370. return IRQ_HANDLED;
  371. }
  372. /**
  373. * vpif_update_std_info() - update standard related info
  374. * @ch: ptr to channel object
  375. *
  376. * For a given standard selected by application, update values
  377. * in the device data structures
  378. */
  379. static int vpif_update_std_info(struct channel_obj *ch)
  380. {
  381. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  382. struct vpif_params *vpifparams = &ch->vpifparams;
  383. const struct vpif_channel_config_params *config;
  384. struct vpif_channel_config_params *std_info;
  385. struct video_obj *vid_ch = &ch->video;
  386. int index;
  387. vpif_dbg(2, debug, "vpif_update_std_info\n");
  388. std_info = &vpifparams->std_info;
  389. for (index = 0; index < ARRAY_SIZE(ch_params); index++) {
  390. config = &ch_params[index];
  391. if (config->stdid & vid_ch->stdid) {
  392. memcpy(std_info, config, sizeof(*config));
  393. break;
  394. }
  395. }
  396. /* standard not found */
  397. if (index == ARRAY_SIZE(ch_params))
  398. return -EINVAL;
  399. common->fmt.fmt.pix.width = std_info->width;
  400. common->width = std_info->width;
  401. common->fmt.fmt.pix.height = std_info->height;
  402. common->height = std_info->height;
  403. common->fmt.fmt.pix.bytesperline = std_info->width;
  404. vpifparams->video_params.hpitch = std_info->width;
  405. vpifparams->video_params.storage_mode = std_info->frm_fmt;
  406. return 0;
  407. }
  408. /**
  409. * vpif_calculate_offsets : This function calculates buffers offsets
  410. * @ch : ptr to channel object
  411. *
  412. * This function calculates buffer offsets for Y and C in the top and
  413. * bottom field
  414. */
  415. static void vpif_calculate_offsets(struct channel_obj *ch)
  416. {
  417. unsigned int hpitch, vpitch, sizeimage;
  418. struct video_obj *vid_ch = &(ch->video);
  419. struct vpif_params *vpifparams = &ch->vpifparams;
  420. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  421. enum v4l2_field field = common->fmt.fmt.pix.field;
  422. vpif_dbg(2, debug, "vpif_calculate_offsets\n");
  423. if (V4L2_FIELD_ANY == field) {
  424. if (vpifparams->std_info.frm_fmt)
  425. vid_ch->buf_field = V4L2_FIELD_NONE;
  426. else
  427. vid_ch->buf_field = V4L2_FIELD_INTERLACED;
  428. } else
  429. vid_ch->buf_field = common->fmt.fmt.pix.field;
  430. if (V4L2_MEMORY_USERPTR == common->memory)
  431. sizeimage = common->fmt.fmt.pix.sizeimage;
  432. else
  433. sizeimage = config_params.channel_bufsize[ch->channel_id];
  434. hpitch = common->fmt.fmt.pix.bytesperline;
  435. vpitch = sizeimage / (hpitch * 2);
  436. if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
  437. (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
  438. /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
  439. common->ytop_off = 0;
  440. common->ybtm_off = hpitch;
  441. common->ctop_off = sizeimage / 2;
  442. common->cbtm_off = sizeimage / 2 + hpitch;
  443. } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
  444. /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
  445. common->ytop_off = 0;
  446. common->ybtm_off = sizeimage / 4;
  447. common->ctop_off = sizeimage / 2;
  448. common->cbtm_off = common->ctop_off + sizeimage / 4;
  449. } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
  450. /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
  451. common->ybtm_off = 0;
  452. common->ytop_off = sizeimage / 4;
  453. common->cbtm_off = sizeimage / 2;
  454. common->ctop_off = common->cbtm_off + sizeimage / 4;
  455. }
  456. if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
  457. (V4L2_FIELD_INTERLACED == vid_ch->buf_field))
  458. vpifparams->video_params.storage_mode = 1;
  459. else
  460. vpifparams->video_params.storage_mode = 0;
  461. if (1 == vpifparams->std_info.frm_fmt)
  462. vpifparams->video_params.hpitch =
  463. common->fmt.fmt.pix.bytesperline;
  464. else {
  465. if ((field == V4L2_FIELD_ANY)
  466. || (field == V4L2_FIELD_INTERLACED))
  467. vpifparams->video_params.hpitch =
  468. common->fmt.fmt.pix.bytesperline * 2;
  469. else
  470. vpifparams->video_params.hpitch =
  471. common->fmt.fmt.pix.bytesperline;
  472. }
  473. ch->vpifparams.video_params.stdid = vpifparams->std_info.stdid;
  474. }
  475. /**
  476. * vpif_config_format: configure default frame format in the device
  477. * ch : ptr to channel object
  478. */
  479. static void vpif_config_format(struct channel_obj *ch)
  480. {
  481. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  482. vpif_dbg(2, debug, "vpif_config_format\n");
  483. common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
  484. if (config_params.numbuffers[ch->channel_id] == 0)
  485. common->memory = V4L2_MEMORY_USERPTR;
  486. else
  487. common->memory = V4L2_MEMORY_MMAP;
  488. common->fmt.fmt.pix.sizeimage
  489. = config_params.channel_bufsize[ch->channel_id];
  490. if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER)
  491. common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
  492. else
  493. common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
  494. common->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  495. }
  496. /**
  497. * vpif_get_default_field() - Get default field type based on interface
  498. * @vpif_params - ptr to vpif params
  499. */
  500. static inline enum v4l2_field vpif_get_default_field(
  501. struct vpif_interface *iface)
  502. {
  503. return (iface->if_type == VPIF_IF_RAW_BAYER) ? V4L2_FIELD_NONE :
  504. V4L2_FIELD_INTERLACED;
  505. }
  506. /**
  507. * vpif_check_format() - check given pixel format for compatibility
  508. * @ch - channel ptr
  509. * @pixfmt - Given pixel format
  510. * @update - update the values as per hardware requirement
  511. *
  512. * Check the application pixel format for S_FMT and update the input
  513. * values as per hardware limits for TRY_FMT. The default pixel and
  514. * field format is selected based on interface type.
  515. */
  516. static int vpif_check_format(struct channel_obj *ch,
  517. struct v4l2_pix_format *pixfmt,
  518. int update)
  519. {
  520. struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
  521. struct vpif_params *vpif_params = &ch->vpifparams;
  522. enum v4l2_field field = pixfmt->field;
  523. u32 sizeimage, hpitch, vpitch;
  524. int ret = -EINVAL;
  525. vpif_dbg(2, debug, "vpif_check_format\n");
  526. /**
  527. * first check for the pixel format. If if_type is Raw bayer,
  528. * only V4L2_PIX_FMT_SBGGR8 format is supported. Otherwise only
  529. * V4L2_PIX_FMT_YUV422P is supported
  530. */
  531. if (vpif_params->iface.if_type == VPIF_IF_RAW_BAYER) {
  532. if (pixfmt->pixelformat != V4L2_PIX_FMT_SBGGR8) {
  533. if (!update) {
  534. vpif_dbg(2, debug, "invalid pix format\n");
  535. goto exit;
  536. }
  537. pixfmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
  538. }
  539. } else {
  540. if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P) {
  541. if (!update) {
  542. vpif_dbg(2, debug, "invalid pixel format\n");
  543. goto exit;
  544. }
  545. pixfmt->pixelformat = V4L2_PIX_FMT_YUV422P;
  546. }
  547. }
  548. if (!(VPIF_VALID_FIELD(field))) {
  549. if (!update) {
  550. vpif_dbg(2, debug, "invalid field format\n");
  551. goto exit;
  552. }
  553. /**
  554. * By default use FIELD_NONE for RAW Bayer capture
  555. * and FIELD_INTERLACED for other interfaces
  556. */
  557. field = vpif_get_default_field(&vpif_params->iface);
  558. } else if (field == V4L2_FIELD_ANY)
  559. /* unsupported field. Use default */
  560. field = vpif_get_default_field(&vpif_params->iface);
  561. /* validate the hpitch */
  562. hpitch = pixfmt->bytesperline;
  563. if (hpitch < vpif_params->std_info.width) {
  564. if (!update) {
  565. vpif_dbg(2, debug, "invalid hpitch\n");
  566. goto exit;
  567. }
  568. hpitch = vpif_params->std_info.width;
  569. }
  570. if (V4L2_MEMORY_USERPTR == common->memory)
  571. sizeimage = pixfmt->sizeimage;
  572. else
  573. sizeimage = config_params.channel_bufsize[ch->channel_id];
  574. vpitch = sizeimage / (hpitch * 2);
  575. /* validate the vpitch */
  576. if (vpitch < vpif_params->std_info.height) {
  577. if (!update) {
  578. vpif_dbg(2, debug, "Invalid vpitch\n");
  579. goto exit;
  580. }
  581. vpitch = vpif_params->std_info.height;
  582. }
  583. /* Check for 8 byte alignment */
  584. if (!ALIGN(hpitch, 8)) {
  585. if (!update) {
  586. vpif_dbg(2, debug, "invalid pitch alignment\n");
  587. goto exit;
  588. }
  589. /* adjust to next 8 byte boundary */
  590. hpitch = (((hpitch + 7) / 8) * 8);
  591. }
  592. /* if update is set, modify the bytesperline and sizeimage */
  593. if (update) {
  594. pixfmt->bytesperline = hpitch;
  595. pixfmt->sizeimage = hpitch * vpitch * 2;
  596. }
  597. /**
  598. * Image width and height is always based on current standard width and
  599. * height
  600. */
  601. pixfmt->width = common->fmt.fmt.pix.width;
  602. pixfmt->height = common->fmt.fmt.pix.height;
  603. return 0;
  604. exit:
  605. return ret;
  606. }
  607. /**
  608. * vpif_config_addr() - function to configure buffer address in vpif
  609. * @ch - channel ptr
  610. * @muxmode - channel mux mode
  611. */
  612. static void vpif_config_addr(struct channel_obj *ch, int muxmode)
  613. {
  614. struct common_obj *common;
  615. vpif_dbg(2, debug, "vpif_config_addr\n");
  616. common = &(ch->common[VPIF_VIDEO_INDEX]);
  617. if (VPIF_CHANNEL1_VIDEO == ch->channel_id)
  618. common->set_addr = ch1_set_videobuf_addr;
  619. else if (2 == muxmode)
  620. common->set_addr = ch0_set_videobuf_addr_yc_nmux;
  621. else
  622. common->set_addr = ch0_set_videobuf_addr;
  623. }
  624. /**
  625. * vpfe_mmap : It is used to map kernel space buffers into user spaces
  626. * @filep: file pointer
  627. * @vma: ptr to vm_area_struct
  628. */
  629. static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
  630. {
  631. /* Get the channel object and file handle object */
  632. struct vpif_fh *fh = filep->private_data;
  633. struct channel_obj *ch = fh->channel;
  634. struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
  635. vpif_dbg(2, debug, "vpif_mmap\n");
  636. return videobuf_mmap_mapper(&common->buffer_queue, vma);
  637. }
  638. /**
  639. * vpif_poll: It is used for select/poll system call
  640. * @filep: file pointer
  641. * @wait: poll table to wait
  642. */
  643. static unsigned int vpif_poll(struct file *filep, poll_table * wait)
  644. {
  645. int err = 0;
  646. struct vpif_fh *fh = filep->private_data;
  647. struct channel_obj *channel = fh->channel;
  648. struct common_obj *common = &(channel->common[VPIF_VIDEO_INDEX]);
  649. vpif_dbg(2, debug, "vpif_poll\n");
  650. if (common->started)
  651. err = videobuf_poll_stream(filep, &common->buffer_queue, wait);
  652. return 0;
  653. }
  654. /**
  655. * vpif_open : vpif open handler
  656. * @filep: file ptr
  657. *
  658. * It creates object of file handle structure and stores it in private_data
  659. * member of filepointer
  660. */
  661. static int vpif_open(struct file *filep)
  662. {
  663. struct vpif_capture_config *config = vpif_dev->platform_data;
  664. struct video_device *vdev = video_devdata(filep);
  665. struct common_obj *common;
  666. struct video_obj *vid_ch;
  667. struct channel_obj *ch;
  668. struct vpif_fh *fh;
  669. int i, ret = 0;
  670. vpif_dbg(2, debug, "vpif_open\n");
  671. ch = video_get_drvdata(vdev);
  672. vid_ch = &ch->video;
  673. common = &ch->common[VPIF_VIDEO_INDEX];
  674. if (mutex_lock_interruptible(&common->lock))
  675. return -ERESTARTSYS;
  676. if (NULL == ch->curr_subdev_info) {
  677. /**
  678. * search through the sub device to see a registered
  679. * sub device and make it as current sub device
  680. */
  681. for (i = 0; i < config->subdev_count; i++) {
  682. if (vpif_obj.sd[i]) {
  683. /* the sub device is registered */
  684. ch->curr_subdev_info = &config->subdev_info[i];
  685. /* make first input as the current input */
  686. vid_ch->input_idx = 0;
  687. break;
  688. }
  689. }
  690. if (i == config->subdev_count) {
  691. vpif_err("No sub device registered\n");
  692. ret = -ENOENT;
  693. goto exit;
  694. }
  695. }
  696. /* Allocate memory for the file handle object */
  697. fh = kmalloc(sizeof(struct vpif_fh), GFP_KERNEL);
  698. if (NULL == fh) {
  699. vpif_err("unable to allocate memory for file handle object\n");
  700. ret = -ENOMEM;
  701. goto exit;
  702. }
  703. /* store pointer to fh in private_data member of filep */
  704. filep->private_data = fh;
  705. fh->channel = ch;
  706. fh->initialized = 0;
  707. /* If decoder is not initialized. initialize it */
  708. if (!ch->initialized) {
  709. fh->initialized = 1;
  710. ch->initialized = 1;
  711. memset(&(ch->vpifparams), 0, sizeof(struct vpif_params));
  712. }
  713. /* Increment channel usrs counter */
  714. ch->usrs++;
  715. /* Set io_allowed member to false */
  716. fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
  717. /* Initialize priority of this instance to default priority */
  718. fh->prio = V4L2_PRIORITY_UNSET;
  719. v4l2_prio_open(&ch->prio, &fh->prio);
  720. exit:
  721. mutex_unlock(&common->lock);
  722. return ret;
  723. }
  724. /**
  725. * vpif_release : function to clean up file close
  726. * @filep: file pointer
  727. *
  728. * This function deletes buffer queue, frees the buffers and the vpfe file
  729. * handle
  730. */
  731. static int vpif_release(struct file *filep)
  732. {
  733. struct vpif_fh *fh = filep->private_data;
  734. struct channel_obj *ch = fh->channel;
  735. struct common_obj *common;
  736. vpif_dbg(2, debug, "vpif_release\n");
  737. common = &ch->common[VPIF_VIDEO_INDEX];
  738. if (mutex_lock_interruptible(&common->lock))
  739. return -ERESTARTSYS;
  740. /* if this instance is doing IO */
  741. if (fh->io_allowed[VPIF_VIDEO_INDEX]) {
  742. /* Reset io_usrs member of channel object */
  743. common->io_usrs = 0;
  744. /* Disable channel as per its device type and channel id */
  745. if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
  746. enable_channel0(0);
  747. channel0_intr_enable(0);
  748. }
  749. if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
  750. (2 == common->started)) {
  751. enable_channel1(0);
  752. channel1_intr_enable(0);
  753. }
  754. common->started = 0;
  755. /* Free buffers allocated */
  756. videobuf_queue_cancel(&common->buffer_queue);
  757. videobuf_mmap_free(&common->buffer_queue);
  758. }
  759. /* Decrement channel usrs counter */
  760. ch->usrs--;
  761. /* unlock mutex on channel object */
  762. mutex_unlock(&common->lock);
  763. /* Close the priority */
  764. v4l2_prio_close(&ch->prio, fh->prio);
  765. if (fh->initialized)
  766. ch->initialized = 0;
  767. filep->private_data = NULL;
  768. kfree(fh);
  769. return 0;
  770. }
  771. /**
  772. * vpif_reqbufs() - request buffer handler
  773. * @file: file ptr
  774. * @priv: file handle
  775. * @reqbuf: request buffer structure ptr
  776. */
  777. static int vpif_reqbufs(struct file *file, void *priv,
  778. struct v4l2_requestbuffers *reqbuf)
  779. {
  780. struct vpif_fh *fh = priv;
  781. struct channel_obj *ch = fh->channel;
  782. struct common_obj *common;
  783. u8 index = 0;
  784. int ret = 0;
  785. vpif_dbg(2, debug, "vpif_reqbufs\n");
  786. /**
  787. * This file handle has not initialized the channel,
  788. * It is not allowed to do settings
  789. */
  790. if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)
  791. || (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
  792. if (!fh->initialized) {
  793. vpif_dbg(1, debug, "Channel Busy\n");
  794. return -EBUSY;
  795. }
  796. }
  797. if (V4L2_BUF_TYPE_VIDEO_CAPTURE != reqbuf->type)
  798. return -EINVAL;
  799. index = VPIF_VIDEO_INDEX;
  800. common = &ch->common[index];
  801. if (mutex_lock_interruptible(&common->lock))
  802. return -ERESTARTSYS;
  803. if (0 != common->io_usrs) {
  804. ret = -EBUSY;
  805. goto reqbuf_exit;
  806. }
  807. /* Initialize videobuf queue as per the buffer type */
  808. videobuf_queue_dma_contig_init(&common->buffer_queue,
  809. &video_qops, NULL,
  810. &common->irqlock,
  811. reqbuf->type,
  812. common->fmt.fmt.pix.field,
  813. sizeof(struct videobuf_buffer), fh);
  814. /* Set io allowed member of file handle to TRUE */
  815. fh->io_allowed[index] = 1;
  816. /* Increment io usrs member of channel object to 1 */
  817. common->io_usrs = 1;
  818. /* Store type of memory requested in channel object */
  819. common->memory = reqbuf->memory;
  820. INIT_LIST_HEAD(&common->dma_queue);
  821. /* Allocate buffers */
  822. ret = videobuf_reqbufs(&common->buffer_queue, reqbuf);
  823. reqbuf_exit:
  824. mutex_unlock(&common->lock);
  825. return ret;
  826. }
  827. /**
  828. * vpif_querybuf() - query buffer handler
  829. * @file: file ptr
  830. * @priv: file handle
  831. * @buf: v4l2 buffer structure ptr
  832. */
  833. static int vpif_querybuf(struct file *file, void *priv,
  834. struct v4l2_buffer *buf)
  835. {
  836. struct vpif_fh *fh = priv;
  837. struct channel_obj *ch = fh->channel;
  838. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  839. vpif_dbg(2, debug, "vpif_querybuf\n");
  840. if (common->fmt.type != buf->type)
  841. return -EINVAL;
  842. if (common->memory != V4L2_MEMORY_MMAP) {
  843. vpif_dbg(1, debug, "Invalid memory\n");
  844. return -EINVAL;
  845. }
  846. return videobuf_querybuf(&common->buffer_queue, buf);
  847. }
  848. /**
  849. * vpif_qbuf() - query buffer handler
  850. * @file: file ptr
  851. * @priv: file handle
  852. * @buf: v4l2 buffer structure ptr
  853. */
  854. static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  855. {
  856. struct vpif_fh *fh = priv;
  857. struct channel_obj *ch = fh->channel;
  858. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  859. struct v4l2_buffer tbuf = *buf;
  860. struct videobuf_buffer *buf1;
  861. unsigned long addr = 0;
  862. unsigned long flags;
  863. int ret = 0;
  864. vpif_dbg(2, debug, "vpif_qbuf\n");
  865. if (common->fmt.type != tbuf.type) {
  866. vpif_err("invalid buffer type\n");
  867. return -EINVAL;
  868. }
  869. if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
  870. vpif_err("fh io not allowed \n");
  871. return -EACCES;
  872. }
  873. if (!(list_empty(&common->dma_queue)) ||
  874. (common->cur_frm != common->next_frm) ||
  875. !common->started ||
  876. (common->started && (0 == ch->field_id)))
  877. return videobuf_qbuf(&common->buffer_queue, buf);
  878. /* bufferqueue is empty store buffer address in VPIF registers */
  879. mutex_lock(&common->buffer_queue.vb_lock);
  880. buf1 = common->buffer_queue.bufs[tbuf.index];
  881. if ((buf1->state == VIDEOBUF_QUEUED) ||
  882. (buf1->state == VIDEOBUF_ACTIVE)) {
  883. vpif_err("invalid state\n");
  884. goto qbuf_exit;
  885. }
  886. switch (buf1->memory) {
  887. case V4L2_MEMORY_MMAP:
  888. if (buf1->baddr == 0)
  889. goto qbuf_exit;
  890. break;
  891. case V4L2_MEMORY_USERPTR:
  892. if (tbuf.length < buf1->bsize)
  893. goto qbuf_exit;
  894. if ((VIDEOBUF_NEEDS_INIT != buf1->state)
  895. && (buf1->baddr != tbuf.m.userptr)) {
  896. vpif_buffer_release(&common->buffer_queue, buf1);
  897. buf1->baddr = tbuf.m.userptr;
  898. }
  899. break;
  900. default:
  901. goto qbuf_exit;
  902. }
  903. local_irq_save(flags);
  904. ret = vpif_buffer_prepare(&common->buffer_queue, buf1,
  905. common->buffer_queue.field);
  906. if (ret < 0) {
  907. local_irq_restore(flags);
  908. goto qbuf_exit;
  909. }
  910. buf1->state = VIDEOBUF_ACTIVE;
  911. if (V4L2_MEMORY_USERPTR == common->memory)
  912. addr = buf1->boff;
  913. else
  914. addr = videobuf_to_dma_contig(buf1);
  915. common->next_frm = buf1;
  916. common->set_addr(addr + common->ytop_off,
  917. addr + common->ybtm_off,
  918. addr + common->ctop_off,
  919. addr + common->cbtm_off);
  920. local_irq_restore(flags);
  921. list_add_tail(&buf1->stream, &common->buffer_queue.stream);
  922. mutex_unlock(&common->buffer_queue.vb_lock);
  923. return 0;
  924. qbuf_exit:
  925. mutex_unlock(&common->buffer_queue.vb_lock);
  926. return -EINVAL;
  927. }
  928. /**
  929. * vpif_dqbuf() - query buffer handler
  930. * @file: file ptr
  931. * @priv: file handle
  932. * @buf: v4l2 buffer structure ptr
  933. */
  934. static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  935. {
  936. struct vpif_fh *fh = priv;
  937. struct channel_obj *ch = fh->channel;
  938. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  939. vpif_dbg(2, debug, "vpif_dqbuf\n");
  940. return videobuf_dqbuf(&common->buffer_queue, buf,
  941. file->f_flags & O_NONBLOCK);
  942. }
  943. /**
  944. * vpif_streamon() - streamon handler
  945. * @file: file ptr
  946. * @priv: file handle
  947. * @buftype: v4l2 buffer type
  948. */
  949. static int vpif_streamon(struct file *file, void *priv,
  950. enum v4l2_buf_type buftype)
  951. {
  952. struct vpif_capture_config *config = vpif_dev->platform_data;
  953. struct vpif_fh *fh = priv;
  954. struct channel_obj *ch = fh->channel;
  955. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  956. struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
  957. struct vpif_params *vpif;
  958. unsigned long addr = 0;
  959. int ret = 0;
  960. vpif_dbg(2, debug, "vpif_streamon\n");
  961. vpif = &ch->vpifparams;
  962. if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  963. vpif_dbg(1, debug, "buffer type not supported\n");
  964. return -EINVAL;
  965. }
  966. /* If file handle is not allowed IO, return error */
  967. if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
  968. vpif_dbg(1, debug, "io not allowed\n");
  969. return -EACCES;
  970. }
  971. /* If Streaming is already started, return error */
  972. if (common->started) {
  973. vpif_dbg(1, debug, "channel->started\n");
  974. return -EBUSY;
  975. }
  976. if ((ch->channel_id == VPIF_CHANNEL0_VIDEO &&
  977. oth_ch->common[VPIF_VIDEO_INDEX].started &&
  978. vpif->std_info.ycmux_mode == 0) ||
  979. ((ch->channel_id == VPIF_CHANNEL1_VIDEO) &&
  980. (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
  981. vpif_dbg(1, debug, "other channel is being used\n");
  982. return -EBUSY;
  983. }
  984. ret = vpif_check_format(ch, &common->fmt.fmt.pix, 0);
  985. if (ret)
  986. return ret;
  987. /* Enable streamon on the sub device */
  988. ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], video,
  989. s_stream, 1);
  990. if (ret && (ret != -ENOIOCTLCMD)) {
  991. vpif_dbg(1, debug, "stream on failed in subdev\n");
  992. return ret;
  993. }
  994. /* Call videobuf_streamon to start streaming in videobuf */
  995. ret = videobuf_streamon(&common->buffer_queue);
  996. if (ret) {
  997. vpif_dbg(1, debug, "videobuf_streamon\n");
  998. return ret;
  999. }
  1000. if (mutex_lock_interruptible(&common->lock)) {
  1001. ret = -ERESTARTSYS;
  1002. goto streamoff_exit;
  1003. }
  1004. /* If buffer queue is empty, return error */
  1005. if (list_empty(&common->dma_queue)) {
  1006. vpif_dbg(1, debug, "buffer queue is empty\n");
  1007. ret = -EIO;
  1008. goto exit;
  1009. }
  1010. /* Get the next frame from the buffer queue */
  1011. common->cur_frm = list_entry(common->dma_queue.next,
  1012. struct videobuf_buffer, queue);
  1013. common->next_frm = common->cur_frm;
  1014. /* Remove buffer from the buffer queue */
  1015. list_del(&common->cur_frm->queue);
  1016. /* Mark state of the current frame to active */
  1017. common->cur_frm->state = VIDEOBUF_ACTIVE;
  1018. /* Initialize field_id and started member */
  1019. ch->field_id = 0;
  1020. common->started = 1;
  1021. if (V4L2_MEMORY_USERPTR == common->memory)
  1022. addr = common->cur_frm->boff;
  1023. else
  1024. addr = videobuf_to_dma_contig(common->cur_frm);
  1025. /* Calculate the offset for Y and C data in the buffer */
  1026. vpif_calculate_offsets(ch);
  1027. if ((vpif->std_info.frm_fmt &&
  1028. ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE) &&
  1029. (common->fmt.fmt.pix.field != V4L2_FIELD_ANY))) ||
  1030. (!vpif->std_info.frm_fmt &&
  1031. (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
  1032. vpif_dbg(1, debug, "conflict in field format and std format\n");
  1033. ret = -EINVAL;
  1034. goto exit;
  1035. }
  1036. /* configure 1 or 2 channel mode */
  1037. ret = config->setup_input_channel_mode(vpif->std_info.ycmux_mode);
  1038. if (ret < 0) {
  1039. vpif_dbg(1, debug, "can't set vpif channel mode\n");
  1040. goto exit;
  1041. }
  1042. /* Call vpif_set_params function to set the parameters and addresses */
  1043. ret = vpif_set_video_params(vpif, ch->channel_id);
  1044. if (ret < 0) {
  1045. vpif_dbg(1, debug, "can't set video params\n");
  1046. goto exit;
  1047. }
  1048. common->started = ret;
  1049. vpif_config_addr(ch, ret);
  1050. common->set_addr(addr + common->ytop_off,
  1051. addr + common->ybtm_off,
  1052. addr + common->ctop_off,
  1053. addr + common->cbtm_off);
  1054. /**
  1055. * Set interrupt for both the fields in VPIF Register enable channel in
  1056. * VPIF register
  1057. */
  1058. if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)) {
  1059. channel0_intr_assert();
  1060. channel0_intr_enable(1);
  1061. enable_channel0(1);
  1062. }
  1063. if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
  1064. (common->started == 2)) {
  1065. channel1_intr_assert();
  1066. channel1_intr_enable(1);
  1067. enable_channel1(1);
  1068. }
  1069. channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
  1070. mutex_unlock(&common->lock);
  1071. return ret;
  1072. exit:
  1073. mutex_unlock(&common->lock);
  1074. streamoff_exit:
  1075. ret = videobuf_streamoff(&common->buffer_queue);
  1076. return ret;
  1077. }
  1078. /**
  1079. * vpif_streamoff() - streamoff handler
  1080. * @file: file ptr
  1081. * @priv: file handle
  1082. * @buftype: v4l2 buffer type
  1083. */
  1084. static int vpif_streamoff(struct file *file, void *priv,
  1085. enum v4l2_buf_type buftype)
  1086. {
  1087. struct vpif_fh *fh = priv;
  1088. struct channel_obj *ch = fh->channel;
  1089. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  1090. int ret;
  1091. vpif_dbg(2, debug, "vpif_streamoff\n");
  1092. if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  1093. vpif_dbg(1, debug, "buffer type not supported\n");
  1094. return -EINVAL;
  1095. }
  1096. /* If io is allowed for this file handle, return error */
  1097. if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
  1098. vpif_dbg(1, debug, "io not allowed\n");
  1099. return -EACCES;
  1100. }
  1101. /* If streaming is not started, return error */
  1102. if (!common->started) {
  1103. vpif_dbg(1, debug, "channel->started\n");
  1104. return -EINVAL;
  1105. }
  1106. if (mutex_lock_interruptible(&common->lock))
  1107. return -ERESTARTSYS;
  1108. /* disable channel */
  1109. if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
  1110. enable_channel0(0);
  1111. channel0_intr_enable(0);
  1112. } else {
  1113. enable_channel1(0);
  1114. channel1_intr_enable(0);
  1115. }
  1116. common->started = 0;
  1117. ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], video,
  1118. s_stream, 0);
  1119. if (ret && (ret != -ENOIOCTLCMD))
  1120. vpif_dbg(1, debug, "stream off failed in subdev\n");
  1121. mutex_unlock(&common->lock);
  1122. return videobuf_streamoff(&common->buffer_queue);
  1123. }
  1124. /**
  1125. * vpif_map_sub_device_to_input() - Maps sub device to input
  1126. * @ch - ptr to channel
  1127. * @config - ptr to capture configuration
  1128. * @input_index - Given input index from application
  1129. * @sub_device_index - index into sd table
  1130. *
  1131. * lookup the sub device information for a given input index.
  1132. * we report all the inputs to application. inputs table also
  1133. * has sub device name for the each input
  1134. */
  1135. static struct vpif_subdev_info *vpif_map_sub_device_to_input(
  1136. struct channel_obj *ch,
  1137. struct vpif_capture_config *vpif_cfg,
  1138. int input_index,
  1139. int *sub_device_index)
  1140. {
  1141. struct vpif_capture_chan_config *chan_cfg;
  1142. struct vpif_subdev_info *subdev_info = NULL;
  1143. const char *subdev_name = NULL;
  1144. int i;
  1145. vpif_dbg(2, debug, "vpif_map_sub_device_to_input\n");
  1146. chan_cfg = &vpif_cfg->chan_config[ch->channel_id];
  1147. /**
  1148. * search through the inputs to find the sub device supporting
  1149. * the input
  1150. */
  1151. for (i = 0; i < chan_cfg->input_count; i++) {
  1152. /* For each sub device, loop through input */
  1153. if (i == input_index) {
  1154. subdev_name = chan_cfg->inputs[i].subdev_name;
  1155. break;
  1156. }
  1157. }
  1158. /* if reached maximum. return null */
  1159. if (i == chan_cfg->input_count || (NULL == subdev_name))
  1160. return subdev_info;
  1161. /* loop through the sub device list to get the sub device info */
  1162. for (i = 0; i < vpif_cfg->subdev_count; i++) {
  1163. subdev_info = &vpif_cfg->subdev_info[i];
  1164. if (!strcmp(subdev_info->name, subdev_name))
  1165. break;
  1166. }
  1167. if (i == vpif_cfg->subdev_count)
  1168. return subdev_info;
  1169. /* check if the sub device is registered */
  1170. if (NULL == vpif_obj.sd[i])
  1171. return NULL;
  1172. *sub_device_index = i;
  1173. return subdev_info;
  1174. }
  1175. /**
  1176. * vpif_querystd() - querystd handler
  1177. * @file: file ptr
  1178. * @priv: file handle
  1179. * @std_id: ptr to std id
  1180. *
  1181. * This function is called to detect standard at the selected input
  1182. */
  1183. static int vpif_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
  1184. {
  1185. struct vpif_fh *fh = priv;
  1186. struct channel_obj *ch = fh->channel;
  1187. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  1188. int ret = 0;
  1189. vpif_dbg(2, debug, "vpif_querystd\n");
  1190. if (mutex_lock_interruptible(&common->lock))
  1191. return -ERESTARTSYS;
  1192. /* Call querystd function of decoder device */
  1193. ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], video,
  1194. querystd, std_id);
  1195. if (ret < 0)
  1196. vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
  1197. mutex_unlock(&common->lock);
  1198. return ret;
  1199. }
  1200. /**
  1201. * vpif_g_std() - get STD handler
  1202. * @file: file ptr
  1203. * @priv: file handle
  1204. * @std_id: ptr to std id
  1205. */
  1206. static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
  1207. {
  1208. struct vpif_fh *fh = priv;
  1209. struct channel_obj *ch = fh->channel;
  1210. vpif_dbg(2, debug, "vpif_g_std\n");
  1211. *std = ch->video.stdid;
  1212. return 0;
  1213. }
  1214. /**
  1215. * vpif_s_std() - set STD handler
  1216. * @file: file ptr
  1217. * @priv: file handle
  1218. * @std_id: ptr to std id
  1219. */
  1220. static int vpif_s_std(struct file *file, void *priv, v4l2_std_id *std_id)
  1221. {
  1222. struct vpif_fh *fh = priv;
  1223. struct channel_obj *ch = fh->channel;
  1224. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  1225. int ret = 0;
  1226. vpif_dbg(2, debug, "vpif_s_std\n");
  1227. if (common->started) {
  1228. vpif_err("streaming in progress\n");
  1229. return -EBUSY;
  1230. }
  1231. if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
  1232. (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
  1233. if (!fh->initialized) {
  1234. vpif_dbg(1, debug, "Channel Busy\n");
  1235. return -EBUSY;
  1236. }
  1237. }
  1238. ret = v4l2_prio_check(&ch->prio, fh->prio);
  1239. if (0 != ret)
  1240. return ret;
  1241. fh->initialized = 1;
  1242. /* Call encoder subdevice function to set the standard */
  1243. if (mutex_lock_interruptible(&common->lock))
  1244. return -ERESTARTSYS;
  1245. ch->video.stdid = *std_id;
  1246. /* Get the information about the standard */
  1247. if (vpif_update_std_info(ch)) {
  1248. ret = -EINVAL;
  1249. vpif_err("Error getting the standard info\n");
  1250. goto s_std_exit;
  1251. }
  1252. /* Configure the default format information */
  1253. vpif_config_format(ch);
  1254. /* set standard in the sub device */
  1255. ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], core,
  1256. s_std, *std_id);
  1257. if (ret < 0)
  1258. vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
  1259. s_std_exit:
  1260. mutex_unlock(&common->lock);
  1261. return ret;
  1262. }
  1263. /**
  1264. * vpif_enum_input() - ENUMINPUT handler
  1265. * @file: file ptr
  1266. * @priv: file handle
  1267. * @input: ptr to input structure
  1268. */
  1269. static int vpif_enum_input(struct file *file, void *priv,
  1270. struct v4l2_input *input)
  1271. {
  1272. struct vpif_capture_config *config = vpif_dev->platform_data;
  1273. struct vpif_capture_chan_config *chan_cfg;
  1274. struct vpif_fh *fh = priv;
  1275. struct channel_obj *ch = fh->channel;
  1276. chan_cfg = &config->chan_config[ch->channel_id];
  1277. if (input->index >= chan_cfg->input_count) {
  1278. vpif_dbg(1, debug, "Invalid input index\n");
  1279. return -EINVAL;
  1280. }
  1281. memcpy(input, &chan_cfg->inputs[input->index].input,
  1282. sizeof(*input));
  1283. return 0;
  1284. }
  1285. /**
  1286. * vpif_g_input() - Get INPUT handler
  1287. * @file: file ptr
  1288. * @priv: file handle
  1289. * @index: ptr to input index
  1290. */
  1291. static int vpif_g_input(struct file *file, void *priv, unsigned int *index)
  1292. {
  1293. struct vpif_fh *fh = priv;
  1294. struct channel_obj *ch = fh->channel;
  1295. struct video_obj *vid_ch = &ch->video;
  1296. *index = vid_ch->input_idx;
  1297. return 0;
  1298. }
  1299. /**
  1300. * vpif_s_input() - Set INPUT handler
  1301. * @file: file ptr
  1302. * @priv: file handle
  1303. * @index: input index
  1304. */
  1305. static int vpif_s_input(struct file *file, void *priv, unsigned int index)
  1306. {
  1307. struct vpif_capture_config *config = vpif_dev->platform_data;
  1308. struct vpif_capture_chan_config *chan_cfg;
  1309. struct vpif_fh *fh = priv;
  1310. struct channel_obj *ch = fh->channel;
  1311. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  1312. struct video_obj *vid_ch = &ch->video;
  1313. struct vpif_subdev_info *subdev_info;
  1314. int ret = 0, sd_index = 0;
  1315. u32 input = 0, output = 0;
  1316. chan_cfg = &config->chan_config[ch->channel_id];
  1317. if (common->started) {
  1318. vpif_err("Streaming in progress\n");
  1319. return -EBUSY;
  1320. }
  1321. if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
  1322. (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
  1323. if (!fh->initialized) {
  1324. vpif_dbg(1, debug, "Channel Busy\n");
  1325. return -EBUSY;
  1326. }
  1327. }
  1328. ret = v4l2_prio_check(&ch->prio, fh->prio);
  1329. if (0 != ret)
  1330. return ret;
  1331. fh->initialized = 1;
  1332. subdev_info = vpif_map_sub_device_to_input(ch, config, index,
  1333. &sd_index);
  1334. if (NULL == subdev_info) {
  1335. vpif_dbg(1, debug,
  1336. "couldn't lookup sub device for the input index\n");
  1337. return -EINVAL;
  1338. }
  1339. if (mutex_lock_interruptible(&common->lock))
  1340. return -ERESTARTSYS;
  1341. /* first setup input path from sub device to vpif */
  1342. if (config->setup_input_path) {
  1343. ret = config->setup_input_path(ch->channel_id,
  1344. subdev_info->name);
  1345. if (ret < 0) {
  1346. vpif_dbg(1, debug, "couldn't setup input path for the"
  1347. " sub device %s, for input index %d\n",
  1348. subdev_info->name, index);
  1349. goto exit;
  1350. }
  1351. }
  1352. if (subdev_info->can_route) {
  1353. input = subdev_info->input;
  1354. output = subdev_info->output;
  1355. ret = v4l2_subdev_call(vpif_obj.sd[sd_index], video, s_routing,
  1356. input, output, 0);
  1357. if (ret < 0) {
  1358. vpif_dbg(1, debug, "Failed to set input\n");
  1359. goto exit;
  1360. }
  1361. }
  1362. vid_ch->input_idx = index;
  1363. ch->curr_subdev_info = subdev_info;
  1364. ch->curr_sd_index = sd_index;
  1365. /* copy interface parameters to vpif */
  1366. ch->vpifparams.iface = subdev_info->vpif_if;
  1367. /* update tvnorms from the sub device input info */
  1368. ch->video_dev->tvnorms = chan_cfg->inputs[index].input.std;
  1369. exit:
  1370. mutex_unlock(&common->lock);
  1371. return ret;
  1372. }
  1373. /**
  1374. * vpif_enum_fmt_vid_cap() - ENUM_FMT handler
  1375. * @file: file ptr
  1376. * @priv: file handle
  1377. * @index: input index
  1378. */
  1379. static int vpif_enum_fmt_vid_cap(struct file *file, void *priv,
  1380. struct v4l2_fmtdesc *fmt)
  1381. {
  1382. struct vpif_fh *fh = priv;
  1383. struct channel_obj *ch = fh->channel;
  1384. if (fmt->index != 0) {
  1385. vpif_dbg(1, debug, "Invalid format index\n");
  1386. return -EINVAL;
  1387. }
  1388. /* Fill in the information about format */
  1389. if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER) {
  1390. fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1391. strcpy(fmt->description, "Raw Mode -Bayer Pattern GrRBGb");
  1392. fmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
  1393. } else {
  1394. fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1395. strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
  1396. fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
  1397. }
  1398. return 0;
  1399. }
  1400. /**
  1401. * vpif_try_fmt_vid_cap() - TRY_FMT handler
  1402. * @file: file ptr
  1403. * @priv: file handle
  1404. * @fmt: ptr to v4l2 format structure
  1405. */
  1406. static int vpif_try_fmt_vid_cap(struct file *file, void *priv,
  1407. struct v4l2_format *fmt)
  1408. {
  1409. struct vpif_fh *fh = priv;
  1410. struct channel_obj *ch = fh->channel;
  1411. struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
  1412. return vpif_check_format(ch, pixfmt, 1);
  1413. }
  1414. /**
  1415. * vpif_g_fmt_vid_cap() - Set INPUT handler
  1416. * @file: file ptr
  1417. * @priv: file handle
  1418. * @fmt: ptr to v4l2 format structure
  1419. */
  1420. static int vpif_g_fmt_vid_cap(struct file *file, void *priv,
  1421. struct v4l2_format *fmt)
  1422. {
  1423. struct vpif_fh *fh = priv;
  1424. struct channel_obj *ch = fh->channel;
  1425. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  1426. /* Check the validity of the buffer type */
  1427. if (common->fmt.type != fmt->type)
  1428. return -EINVAL;
  1429. /* Fill in the information about format */
  1430. if (mutex_lock_interruptible(&common->lock))
  1431. return -ERESTARTSYS;
  1432. *fmt = common->fmt;
  1433. mutex_unlock(&common->lock);
  1434. return 0;
  1435. }
  1436. /**
  1437. * vpif_s_fmt_vid_cap() - Set FMT handler
  1438. * @file: file ptr
  1439. * @priv: file handle
  1440. * @fmt: ptr to v4l2 format structure
  1441. */
  1442. static int vpif_s_fmt_vid_cap(struct file *file, void *priv,
  1443. struct v4l2_format *fmt)
  1444. {
  1445. struct vpif_fh *fh = priv;
  1446. struct channel_obj *ch = fh->channel;
  1447. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  1448. struct v4l2_pix_format *pixfmt;
  1449. int ret = 0;
  1450. vpif_dbg(2, debug, "VIDIOC_S_FMT\n");
  1451. /* If streaming is started, return error */
  1452. if (common->started) {
  1453. vpif_dbg(1, debug, "Streaming is started\n");
  1454. return -EBUSY;
  1455. }
  1456. if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
  1457. (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
  1458. if (!fh->initialized) {
  1459. vpif_dbg(1, debug, "Channel Busy\n");
  1460. return -EBUSY;
  1461. }
  1462. }
  1463. ret = v4l2_prio_check(&ch->prio, fh->prio);
  1464. if (0 != ret)
  1465. return ret;
  1466. fh->initialized = 1;
  1467. pixfmt = &fmt->fmt.pix;
  1468. /* Check for valid field format */
  1469. ret = vpif_check_format(ch, pixfmt, 0);
  1470. if (ret)
  1471. return ret;
  1472. /* store the format in the channel object */
  1473. if (mutex_lock_interruptible(&common->lock))
  1474. return -ERESTARTSYS;
  1475. common->fmt = *fmt;
  1476. mutex_unlock(&common->lock);
  1477. return 0;
  1478. }
  1479. /**
  1480. * vpif_querycap() - QUERYCAP handler
  1481. * @file: file ptr
  1482. * @priv: file handle
  1483. * @cap: ptr to v4l2_capability structure
  1484. */
  1485. static int vpif_querycap(struct file *file, void *priv,
  1486. struct v4l2_capability *cap)
  1487. {
  1488. struct vpif_capture_config *config = vpif_dev->platform_data;
  1489. cap->version = VPIF_CAPTURE_VERSION_CODE;
  1490. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  1491. strlcpy(cap->driver, "vpif capture", sizeof(cap->driver));
  1492. strlcpy(cap->bus_info, "DM646x Platform", sizeof(cap->bus_info));
  1493. strlcpy(cap->card, config->card_name, sizeof(cap->card));
  1494. return 0;
  1495. }
  1496. /**
  1497. * vpif_g_priority() - get priority handler
  1498. * @file: file ptr
  1499. * @priv: file handle
  1500. * @prio: ptr to v4l2_priority structure
  1501. */
  1502. static int vpif_g_priority(struct file *file, void *priv,
  1503. enum v4l2_priority *prio)
  1504. {
  1505. struct vpif_fh *fh = priv;
  1506. struct channel_obj *ch = fh->channel;
  1507. *prio = v4l2_prio_max(&ch->prio);
  1508. return 0;
  1509. }
  1510. /**
  1511. * vpif_s_priority() - set priority handler
  1512. * @file: file ptr
  1513. * @priv: file handle
  1514. * @prio: ptr to v4l2_priority structure
  1515. */
  1516. static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
  1517. {
  1518. struct vpif_fh *fh = priv;
  1519. struct channel_obj *ch = fh->channel;
  1520. return v4l2_prio_change(&ch->prio, &fh->prio, p);
  1521. }
  1522. /**
  1523. * vpif_cropcap() - cropcap handler
  1524. * @file: file ptr
  1525. * @priv: file handle
  1526. * @crop: ptr to v4l2_cropcap structure
  1527. */
  1528. static int vpif_cropcap(struct file *file, void *priv,
  1529. struct v4l2_cropcap *crop)
  1530. {
  1531. struct vpif_fh *fh = priv;
  1532. struct channel_obj *ch = fh->channel;
  1533. struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
  1534. if (V4L2_BUF_TYPE_VIDEO_CAPTURE != crop->type)
  1535. return -EINVAL;
  1536. crop->bounds.left = 0;
  1537. crop->bounds.top = 0;
  1538. crop->bounds.height = common->height;
  1539. crop->bounds.width = common->width;
  1540. crop->defrect = crop->bounds;
  1541. return 0;
  1542. }
  1543. /* vpif capture ioctl operations */
  1544. static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
  1545. .vidioc_querycap = vpif_querycap,
  1546. .vidioc_g_priority = vpif_g_priority,
  1547. .vidioc_s_priority = vpif_s_priority,
  1548. .vidioc_enum_fmt_vid_cap = vpif_enum_fmt_vid_cap,
  1549. .vidioc_g_fmt_vid_cap = vpif_g_fmt_vid_cap,
  1550. .vidioc_s_fmt_vid_cap = vpif_s_fmt_vid_cap,
  1551. .vidioc_try_fmt_vid_cap = vpif_try_fmt_vid_cap,
  1552. .vidioc_enum_input = vpif_enum_input,
  1553. .vidioc_s_input = vpif_s_input,
  1554. .vidioc_g_input = vpif_g_input,
  1555. .vidioc_reqbufs = vpif_reqbufs,
  1556. .vidioc_querybuf = vpif_querybuf,
  1557. .vidioc_querystd = vpif_querystd,
  1558. .vidioc_s_std = vpif_s_std,
  1559. .vidioc_g_std = vpif_g_std,
  1560. .vidioc_qbuf = vpif_qbuf,
  1561. .vidioc_dqbuf = vpif_dqbuf,
  1562. .vidioc_streamon = vpif_streamon,
  1563. .vidioc_streamoff = vpif_streamoff,
  1564. .vidioc_cropcap = vpif_cropcap,
  1565. };
  1566. /* vpif file operations */
  1567. static struct v4l2_file_operations vpif_fops = {
  1568. .owner = THIS_MODULE,
  1569. .open = vpif_open,
  1570. .release = vpif_release,
  1571. .ioctl = video_ioctl2,
  1572. .mmap = vpif_mmap,
  1573. .poll = vpif_poll
  1574. };
  1575. /* vpif video template */
  1576. static struct video_device vpif_video_template = {
  1577. .name = "vpif",
  1578. .fops = &vpif_fops,
  1579. .minor = -1,
  1580. .ioctl_ops = &vpif_ioctl_ops,
  1581. };
  1582. /**
  1583. * initialize_vpif() - Initialize vpif data structures
  1584. *
  1585. * Allocate memory for data structures and initialize them
  1586. */
  1587. static int initialize_vpif(void)
  1588. {
  1589. int err = 0, i, j;
  1590. int free_channel_objects_index;
  1591. /* Default number of buffers should be 3 */
  1592. if ((ch0_numbuffers > 0) &&
  1593. (ch0_numbuffers < config_params.min_numbuffers))
  1594. ch0_numbuffers = config_params.min_numbuffers;
  1595. if ((ch1_numbuffers > 0) &&
  1596. (ch1_numbuffers < config_params.min_numbuffers))
  1597. ch1_numbuffers = config_params.min_numbuffers;
  1598. /* Set buffer size to min buffers size if it is invalid */
  1599. if (ch0_bufsize < config_params.min_bufsize[VPIF_CHANNEL0_VIDEO])
  1600. ch0_bufsize =
  1601. config_params.min_bufsize[VPIF_CHANNEL0_VIDEO];
  1602. if (ch1_bufsize < config_params.min_bufsize[VPIF_CHANNEL1_VIDEO])
  1603. ch1_bufsize =
  1604. config_params.min_bufsize[VPIF_CHANNEL1_VIDEO];
  1605. config_params.numbuffers[VPIF_CHANNEL0_VIDEO] = ch0_numbuffers;
  1606. config_params.numbuffers[VPIF_CHANNEL1_VIDEO] = ch1_numbuffers;
  1607. if (ch0_numbuffers) {
  1608. config_params.channel_bufsize[VPIF_CHANNEL0_VIDEO]
  1609. = ch0_bufsize;
  1610. }
  1611. if (ch1_numbuffers) {
  1612. config_params.channel_bufsize[VPIF_CHANNEL1_VIDEO]
  1613. = ch1_bufsize;
  1614. }
  1615. /* Allocate memory for six channel objects */
  1616. for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
  1617. vpif_obj.dev[i] =
  1618. kzalloc(sizeof(*vpif_obj.dev[i]), GFP_KERNEL);
  1619. /* If memory allocation fails, return error */
  1620. if (!vpif_obj.dev[i]) {
  1621. free_channel_objects_index = i;
  1622. err = -ENOMEM;
  1623. goto vpif_init_free_channel_objects;
  1624. }
  1625. }
  1626. return 0;
  1627. vpif_init_free_channel_objects:
  1628. for (j = 0; j < free_channel_objects_index; j++)
  1629. kfree(vpif_obj.dev[j]);
  1630. return err;
  1631. }
  1632. /**
  1633. * vpif_probe : This function probes the vpif capture driver
  1634. * @pdev: platform device pointer
  1635. *
  1636. * This creates device entries by register itself to the V4L2 driver and
  1637. * initializes fields of each channel objects
  1638. */
  1639. static __init int vpif_probe(struct platform_device *pdev)
  1640. {
  1641. struct vpif_subdev_info *subdevdata;
  1642. struct vpif_capture_config *config;
  1643. int i, j, k, m, q, err;
  1644. struct i2c_adapter *i2c_adap;
  1645. struct channel_obj *ch;
  1646. struct common_obj *common;
  1647. struct video_device *vfd;
  1648. struct resource *res;
  1649. int subdev_count;
  1650. vpif_dev = &pdev->dev;
  1651. err = initialize_vpif();
  1652. if (err) {
  1653. v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
  1654. return err;
  1655. }
  1656. k = 0;
  1657. while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, k))) {
  1658. for (i = res->start; i <= res->end; i++) {
  1659. if (request_irq(i, vpif_channel_isr, IRQF_DISABLED,
  1660. "DM646x_Capture",
  1661. (void *)(&vpif_obj.dev[k]->channel_id))) {
  1662. err = -EBUSY;
  1663. i--;
  1664. goto vpif_int_err;
  1665. }
  1666. }
  1667. k++;
  1668. }
  1669. for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
  1670. /* Get the pointer to the channel object */
  1671. ch = vpif_obj.dev[i];
  1672. /* Allocate memory for video device */
  1673. vfd = video_device_alloc();
  1674. if (NULL == vfd) {
  1675. for (j = 0; j < i; j++) {
  1676. ch = vpif_obj.dev[j];
  1677. video_device_release(ch->video_dev);
  1678. }
  1679. err = -ENOMEM;
  1680. goto vpif_dev_alloc_err;
  1681. }
  1682. /* Initialize field of video device */
  1683. *vfd = vpif_video_template;
  1684. vfd->v4l2_dev = &vpif_obj.v4l2_dev;
  1685. vfd->release = video_device_release;
  1686. snprintf(vfd->name, sizeof(vfd->name),
  1687. "DM646x_VPIFCapture_DRIVER_V%d.%d.%d",
  1688. (VPIF_CAPTURE_VERSION_CODE >> 16) & 0xff,
  1689. (VPIF_CAPTURE_VERSION_CODE >> 8) & 0xff,
  1690. (VPIF_CAPTURE_VERSION_CODE) & 0xff);
  1691. /* Set video_dev to the video device */
  1692. ch->video_dev = vfd;
  1693. }
  1694. for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
  1695. ch = vpif_obj.dev[j];
  1696. ch->channel_id = j;
  1697. common = &(ch->common[VPIF_VIDEO_INDEX]);
  1698. spin_lock_init(&common->irqlock);
  1699. mutex_init(&common->lock);
  1700. /* Initialize prio member of channel object */
  1701. v4l2_prio_init(&ch->prio);
  1702. err = video_register_device(ch->video_dev,
  1703. VFL_TYPE_GRABBER, (j ? 1 : 0));
  1704. if (err)
  1705. goto probe_out;
  1706. video_set_drvdata(ch->video_dev, ch);
  1707. }
  1708. i2c_adap = i2c_get_adapter(1);
  1709. config = pdev->dev.platform_data;
  1710. subdev_count = config->subdev_count;
  1711. vpif_obj.sd = kmalloc(sizeof(struct v4l2_subdev *) * subdev_count,
  1712. GFP_KERNEL);
  1713. if (vpif_obj.sd == NULL) {
  1714. vpif_err("unable to allocate memory for subdevice pointers\n");
  1715. err = -ENOMEM;
  1716. goto probe_out;
  1717. }
  1718. err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
  1719. if (err) {
  1720. v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
  1721. goto probe_subdev_out;
  1722. }
  1723. for (i = 0; i < subdev_count; i++) {
  1724. subdevdata = &config->subdev_info[i];
  1725. vpif_obj.sd[i] =
  1726. v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
  1727. i2c_adap,
  1728. subdevdata->name,
  1729. &subdevdata->board_info,
  1730. NULL);
  1731. if (!vpif_obj.sd[i]) {
  1732. vpif_err("Error registering v4l2 subdevice\n");
  1733. goto probe_subdev_out;
  1734. }
  1735. v4l2_info(&vpif_obj.v4l2_dev, "registered sub device %s\n",
  1736. subdevdata->name);
  1737. if (vpif_obj.sd[i])
  1738. vpif_obj.sd[i]->grp_id = 1 << i;
  1739. }
  1740. v4l2_info(&vpif_obj.v4l2_dev, "DM646x VPIF Capture driver"
  1741. " initialized\n");
  1742. return 0;
  1743. probe_subdev_out:
  1744. /* free sub devices memory */
  1745. kfree(vpif_obj.sd);
  1746. j = VPIF_CAPTURE_MAX_DEVICES;
  1747. probe_out:
  1748. v4l2_device_unregister(&vpif_obj.v4l2_dev);
  1749. for (k = 0; k < j; k++) {
  1750. /* Get the pointer to the channel object */
  1751. ch = vpif_obj.dev[k];
  1752. /* Unregister video device */
  1753. video_unregister_device(ch->video_dev);
  1754. }
  1755. vpif_dev_alloc_err:
  1756. k = VPIF_CAPTURE_MAX_DEVICES-1;
  1757. res = platform_get_resource(pdev, IORESOURCE_IRQ, k);
  1758. i = res->end;
  1759. vpif_int_err:
  1760. for (q = k; q >= 0; q--) {
  1761. for (m = i; m >= (int)res->start; m--)
  1762. free_irq(m, (void *)(&vpif_obj.dev[q]->channel_id));
  1763. res = platform_get_resource(pdev, IORESOURCE_IRQ, q-1);
  1764. if (res)
  1765. i = res->end;
  1766. }
  1767. return err;
  1768. }
  1769. /**
  1770. * vpif_remove() - driver remove handler
  1771. * @device: ptr to platform device structure
  1772. *
  1773. * The vidoe device is unregistered
  1774. */
  1775. static int vpif_remove(struct platform_device *device)
  1776. {
  1777. int i;
  1778. struct channel_obj *ch;
  1779. v4l2_device_unregister(&vpif_obj.v4l2_dev);
  1780. /* un-register device */
  1781. for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
  1782. /* Get the pointer to the channel object */
  1783. ch = vpif_obj.dev[i];
  1784. /* Unregister video device */
  1785. video_unregister_device(ch->video_dev);
  1786. }
  1787. return 0;
  1788. }
  1789. /**
  1790. * vpif_suspend: vpif device suspend
  1791. *
  1792. * TODO: Add suspend code here
  1793. */
  1794. static int
  1795. vpif_suspend(struct device *dev)
  1796. {
  1797. return -1;
  1798. }
  1799. /**
  1800. * vpif_resume: vpif device suspend
  1801. *
  1802. * TODO: Add resume code here
  1803. */
  1804. static int
  1805. vpif_resume(struct device *dev)
  1806. {
  1807. return -1;
  1808. }
  1809. static const struct dev_pm_ops vpif_dev_pm_ops = {
  1810. .suspend = vpif_suspend,
  1811. .resume = vpif_resume,
  1812. };
  1813. static struct platform_driver vpif_driver = {
  1814. .driver = {
  1815. .name = "vpif_capture",
  1816. .owner = THIS_MODULE,
  1817. .pm = &vpif_dev_pm_ops,
  1818. },
  1819. .probe = vpif_probe,
  1820. .remove = vpif_remove,
  1821. };
  1822. /**
  1823. * vpif_init: initialize the vpif driver
  1824. *
  1825. * This function registers device and driver to the kernel, requests irq
  1826. * handler and allocates memory
  1827. * for channel objects
  1828. */
  1829. static __init int vpif_init(void)
  1830. {
  1831. return platform_driver_register(&vpif_driver);
  1832. }
  1833. /**
  1834. * vpif_cleanup : This function clean up the vpif capture resources
  1835. *
  1836. * This will un-registers device and driver to the kernel, frees
  1837. * requested irq handler and de-allocates memory allocated for channel
  1838. * objects.
  1839. */
  1840. static void vpif_cleanup(void)
  1841. {
  1842. struct platform_device *pdev;
  1843. struct resource *res;
  1844. int irq_num;
  1845. int i = 0;
  1846. pdev = container_of(vpif_dev, struct platform_device, dev);
  1847. while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, i))) {
  1848. for (irq_num = res->start; irq_num <= res->end; irq_num++)
  1849. free_irq(irq_num,
  1850. (void *)(&vpif_obj.dev[i]->channel_id));
  1851. i++;
  1852. }
  1853. platform_driver_unregister(&vpif_driver);
  1854. kfree(vpif_obj.sd);
  1855. for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++)
  1856. kfree(vpif_obj.dev[i]);
  1857. }
  1858. /* Function for module initialization and cleanup */
  1859. module_init(vpif_init);
  1860. module_exit(vpif_cleanup);