vpif_capture.c 55 KB

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