vpif_capture.c 57 KB

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