vpif_capture.c 63 KB

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