vpif_capture.c 55 KB

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