em28xx-video.c 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB
  4. // video capture devices
  5. //
  6. // Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
  7. // Markus Rechberger <mrechberger@gmail.com>
  8. // Mauro Carvalho Chehab <mchehab@kernel.org>
  9. // Sascha Sommer <saschasommer@freenet.de>
  10. // Copyright (C) 2012 Frank Schäfer <fschaefer.oss@googlemail.com>
  11. //
  12. // Some parts based on SN9C10x PC Camera Controllers GPL driver made
  13. // by Luca Risolia <luca.risolia@studio.unibo.it>
  14. //
  15. // This program is free software; you can redistribute it and/or modify
  16. // it under the terms of the GNU General Public License as published by
  17. // the Free Software Foundation; either version 2 of the License, or
  18. // (at your option) any later version.
  19. //
  20. // This program is distributed in the hope that it will be useful,
  21. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. // GNU General Public License for more details.
  24. #include "em28xx.h"
  25. #include <linux/init.h>
  26. #include <linux/list.h>
  27. #include <linux/module.h>
  28. #include <linux/kernel.h>
  29. #include <linux/bitmap.h>
  30. #include <linux/usb.h>
  31. #include <linux/i2c.h>
  32. #include <linux/mm.h>
  33. #include <linux/mutex.h>
  34. #include <linux/slab.h>
  35. #include "em28xx-v4l.h"
  36. #include <media/v4l2-common.h>
  37. #include <media/v4l2-ioctl.h>
  38. #include <media/v4l2-event.h>
  39. #include <media/drv-intf/msp3400.h>
  40. #include <media/tuner.h>
  41. #define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
  42. "Markus Rechberger <mrechberger@gmail.com>, " \
  43. "Mauro Carvalho Chehab <mchehab@kernel.org>, " \
  44. "Sascha Sommer <saschasommer@freenet.de>"
  45. static unsigned int isoc_debug;
  46. module_param(isoc_debug, int, 0644);
  47. MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
  48. static unsigned int disable_vbi;
  49. module_param(disable_vbi, int, 0644);
  50. MODULE_PARM_DESC(disable_vbi, "disable vbi support");
  51. static int alt;
  52. module_param(alt, int, 0644);
  53. MODULE_PARM_DESC(alt, "alternate setting to use for video endpoint");
  54. #define em28xx_videodbg(fmt, arg...) do { \
  55. if (video_debug) \
  56. dev_printk(KERN_DEBUG, &dev->intf->dev, \
  57. "video: %s: " fmt, __func__, ## arg); \
  58. } while (0)
  59. #define em28xx_isocdbg(fmt, arg...) do {\
  60. if (isoc_debug) \
  61. dev_printk(KERN_DEBUG, &dev->intf->dev, \
  62. "isoc: %s: " fmt, __func__, ## arg); \
  63. } while (0)
  64. MODULE_AUTHOR(DRIVER_AUTHOR);
  65. MODULE_DESCRIPTION(DRIVER_DESC " - v4l2 interface");
  66. MODULE_LICENSE("GPL v2");
  67. MODULE_VERSION(EM28XX_VERSION);
  68. #define EM25XX_FRMDATAHDR_BYTE1 0x02
  69. #define EM25XX_FRMDATAHDR_BYTE2_STILL_IMAGE 0x20
  70. #define EM25XX_FRMDATAHDR_BYTE2_FRAME_END 0x02
  71. #define EM25XX_FRMDATAHDR_BYTE2_FRAME_ID 0x01
  72. #define EM25XX_FRMDATAHDR_BYTE2_MASK (EM25XX_FRMDATAHDR_BYTE2_STILL_IMAGE | \
  73. EM25XX_FRMDATAHDR_BYTE2_FRAME_END | \
  74. EM25XX_FRMDATAHDR_BYTE2_FRAME_ID)
  75. static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = -1U };
  76. static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = -1U };
  77. static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = -1U };
  78. module_param_array(video_nr, int, NULL, 0444);
  79. module_param_array(vbi_nr, int, NULL, 0444);
  80. module_param_array(radio_nr, int, NULL, 0444);
  81. MODULE_PARM_DESC(video_nr, "video device numbers");
  82. MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
  83. MODULE_PARM_DESC(radio_nr, "radio device numbers");
  84. static unsigned int video_debug;
  85. module_param(video_debug, int, 0644);
  86. MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
  87. /* supported video standards */
  88. static struct em28xx_fmt format[] = {
  89. {
  90. .name = "16 bpp YUY2, 4:2:2, packed",
  91. .fourcc = V4L2_PIX_FMT_YUYV,
  92. .depth = 16,
  93. .reg = EM28XX_OUTFMT_YUV422_Y0UY1V,
  94. }, {
  95. .name = "16 bpp RGB 565, LE",
  96. .fourcc = V4L2_PIX_FMT_RGB565,
  97. .depth = 16,
  98. .reg = EM28XX_OUTFMT_RGB_16_656,
  99. }, {
  100. .name = "8 bpp Bayer RGRG..GBGB",
  101. .fourcc = V4L2_PIX_FMT_SRGGB8,
  102. .depth = 8,
  103. .reg = EM28XX_OUTFMT_RGB_8_RGRG,
  104. }, {
  105. .name = "8 bpp Bayer BGBG..GRGR",
  106. .fourcc = V4L2_PIX_FMT_SBGGR8,
  107. .depth = 8,
  108. .reg = EM28XX_OUTFMT_RGB_8_BGBG,
  109. }, {
  110. .name = "8 bpp Bayer GRGR..BGBG",
  111. .fourcc = V4L2_PIX_FMT_SGRBG8,
  112. .depth = 8,
  113. .reg = EM28XX_OUTFMT_RGB_8_GRGR,
  114. }, {
  115. .name = "8 bpp Bayer GBGB..RGRG",
  116. .fourcc = V4L2_PIX_FMT_SGBRG8,
  117. .depth = 8,
  118. .reg = EM28XX_OUTFMT_RGB_8_GBGB,
  119. }, {
  120. .name = "12 bpp YUV411",
  121. .fourcc = V4L2_PIX_FMT_YUV411P,
  122. .depth = 12,
  123. .reg = EM28XX_OUTFMT_YUV411,
  124. },
  125. };
  126. /*FIXME: maxw should be dependent of alt mode */
  127. static inline unsigned int norm_maxw(struct em28xx *dev)
  128. {
  129. struct em28xx_v4l2 *v4l2 = dev->v4l2;
  130. if (dev->is_webcam)
  131. return v4l2->sensor_xres;
  132. if (dev->board.max_range_640_480)
  133. return 640;
  134. return 720;
  135. }
  136. static inline unsigned int norm_maxh(struct em28xx *dev)
  137. {
  138. struct em28xx_v4l2 *v4l2 = dev->v4l2;
  139. if (dev->is_webcam)
  140. return v4l2->sensor_yres;
  141. if (dev->board.max_range_640_480)
  142. return 480;
  143. return (v4l2->norm & V4L2_STD_625_50) ? 576 : 480;
  144. }
  145. static int em28xx_vbi_supported(struct em28xx *dev)
  146. {
  147. /* Modprobe option to manually disable */
  148. if (disable_vbi == 1)
  149. return 0;
  150. if (dev->is_webcam)
  151. return 0;
  152. /* FIXME: check subdevices for VBI support */
  153. if (dev->chip_id == CHIP_ID_EM2860 ||
  154. dev->chip_id == CHIP_ID_EM2883)
  155. return 1;
  156. /* Version of em28xx that does not support VBI */
  157. return 0;
  158. }
  159. /*
  160. * em28xx_wake_i2c()
  161. * configure i2c attached devices
  162. */
  163. static void em28xx_wake_i2c(struct em28xx *dev)
  164. {
  165. struct v4l2_device *v4l2_dev = &dev->v4l2->v4l2_dev;
  166. v4l2_device_call_all(v4l2_dev, 0, core, reset, 0);
  167. v4l2_device_call_all(v4l2_dev, 0, video, s_routing,
  168. INPUT(dev->ctl_input)->vmux, 0, 0);
  169. }
  170. static int em28xx_colorlevels_set_default(struct em28xx *dev)
  171. {
  172. em28xx_write_reg(dev, EM28XX_R20_YGAIN, CONTRAST_DEFAULT);
  173. em28xx_write_reg(dev, EM28XX_R21_YOFFSET, BRIGHTNESS_DEFAULT);
  174. em28xx_write_reg(dev, EM28XX_R22_UVGAIN, SATURATION_DEFAULT);
  175. em28xx_write_reg(dev, EM28XX_R23_UOFFSET, BLUE_BALANCE_DEFAULT);
  176. em28xx_write_reg(dev, EM28XX_R24_VOFFSET, RED_BALANCE_DEFAULT);
  177. em28xx_write_reg(dev, EM28XX_R25_SHARPNESS, SHARPNESS_DEFAULT);
  178. em28xx_write_reg(dev, EM28XX_R14_GAMMA, 0x20);
  179. em28xx_write_reg(dev, EM28XX_R15_RGAIN, 0x20);
  180. em28xx_write_reg(dev, EM28XX_R16_GGAIN, 0x20);
  181. em28xx_write_reg(dev, EM28XX_R17_BGAIN, 0x20);
  182. em28xx_write_reg(dev, EM28XX_R18_ROFFSET, 0x00);
  183. em28xx_write_reg(dev, EM28XX_R19_GOFFSET, 0x00);
  184. return em28xx_write_reg(dev, EM28XX_R1A_BOFFSET, 0x00);
  185. }
  186. static int em28xx_set_outfmt(struct em28xx *dev)
  187. {
  188. int ret;
  189. u8 fmt, vinctrl;
  190. struct em28xx_v4l2 *v4l2 = dev->v4l2;
  191. fmt = v4l2->format->reg;
  192. if (!dev->is_em25xx)
  193. fmt |= 0x20;
  194. /*
  195. * NOTE: it's not clear if this is really needed !
  196. * The datasheets say bit 5 is a reserved bit and devices seem to work
  197. * fine without it. But the Windows driver sets it for em2710/50+em28xx
  198. * devices and we've always been setting it, too.
  199. *
  200. * em2765 (em25xx, em276x/7x/8x) devices do NOT work with this bit set,
  201. * it's likely used for an additional (compressed ?) format there.
  202. */
  203. ret = em28xx_write_reg(dev, EM28XX_R27_OUTFMT, fmt);
  204. if (ret < 0)
  205. return ret;
  206. ret = em28xx_write_reg(dev, EM28XX_R10_VINMODE, v4l2->vinmode);
  207. if (ret < 0)
  208. return ret;
  209. vinctrl = v4l2->vinctl;
  210. if (em28xx_vbi_supported(dev) == 1) {
  211. vinctrl |= EM28XX_VINCTRL_VBI_RAW;
  212. em28xx_write_reg(dev, EM28XX_R34_VBI_START_H, 0x00);
  213. em28xx_write_reg(dev, EM28XX_R36_VBI_WIDTH,
  214. v4l2->vbi_width / 4);
  215. em28xx_write_reg(dev, EM28XX_R37_VBI_HEIGHT, v4l2->vbi_height);
  216. if (v4l2->norm & V4L2_STD_525_60) {
  217. /* NTSC */
  218. em28xx_write_reg(dev, EM28XX_R35_VBI_START_V, 0x09);
  219. } else if (v4l2->norm & V4L2_STD_625_50) {
  220. /* PAL */
  221. em28xx_write_reg(dev, EM28XX_R35_VBI_START_V, 0x07);
  222. }
  223. }
  224. return em28xx_write_reg(dev, EM28XX_R11_VINCTRL, vinctrl);
  225. }
  226. static int em28xx_accumulator_set(struct em28xx *dev, u8 xmin, u8 xmax,
  227. u8 ymin, u8 ymax)
  228. {
  229. em28xx_videodbg("em28xx Scale: (%d,%d)-(%d,%d)\n",
  230. xmin, ymin, xmax, ymax);
  231. em28xx_write_regs(dev, EM28XX_R28_XMIN, &xmin, 1);
  232. em28xx_write_regs(dev, EM28XX_R29_XMAX, &xmax, 1);
  233. em28xx_write_regs(dev, EM28XX_R2A_YMIN, &ymin, 1);
  234. return em28xx_write_regs(dev, EM28XX_R2B_YMAX, &ymax, 1);
  235. }
  236. static void em28xx_capture_area_set(struct em28xx *dev, u8 hstart, u8 vstart,
  237. u16 width, u16 height)
  238. {
  239. u8 cwidth = width >> 2;
  240. u8 cheight = height >> 2;
  241. u8 overflow = (height >> 9 & 0x02) | (width >> 10 & 0x01);
  242. /* NOTE: size limit: 2047x1023 = 2MPix */
  243. em28xx_videodbg("capture area set to (%d,%d): %dx%d\n",
  244. hstart, vstart,
  245. ((overflow & 2) << 9 | cwidth << 2),
  246. ((overflow & 1) << 10 | cheight << 2));
  247. em28xx_write_regs(dev, EM28XX_R1C_HSTART, &hstart, 1);
  248. em28xx_write_regs(dev, EM28XX_R1D_VSTART, &vstart, 1);
  249. em28xx_write_regs(dev, EM28XX_R1E_CWIDTH, &cwidth, 1);
  250. em28xx_write_regs(dev, EM28XX_R1F_CHEIGHT, &cheight, 1);
  251. em28xx_write_regs(dev, EM28XX_R1B_OFLOW, &overflow, 1);
  252. /* FIXME: function/meaning of these registers ? */
  253. /* FIXME: align width+height to multiples of 4 ?! */
  254. if (dev->is_em25xx) {
  255. em28xx_write_reg(dev, 0x34, width >> 4);
  256. em28xx_write_reg(dev, 0x35, height >> 4);
  257. }
  258. }
  259. static int em28xx_scaler_set(struct em28xx *dev, u16 h, u16 v)
  260. {
  261. u8 mode = 0x00;
  262. /* the em2800 scaler only supports scaling down to 50% */
  263. if (dev->board.is_em2800) {
  264. mode = (v ? 0x20 : 0x00) | (h ? 0x10 : 0x00);
  265. } else {
  266. u8 buf[2];
  267. buf[0] = h;
  268. buf[1] = h >> 8;
  269. em28xx_write_regs(dev, EM28XX_R30_HSCALELOW, (char *)buf, 2);
  270. buf[0] = v;
  271. buf[1] = v >> 8;
  272. em28xx_write_regs(dev, EM28XX_R32_VSCALELOW, (char *)buf, 2);
  273. /*
  274. * it seems that both H and V scalers must be active
  275. * to work correctly
  276. */
  277. mode = (h || v) ? 0x30 : 0x00;
  278. }
  279. return em28xx_write_reg(dev, EM28XX_R26_COMPR, mode);
  280. }
  281. /* FIXME: this only function read values from dev */
  282. static int em28xx_resolution_set(struct em28xx *dev)
  283. {
  284. struct em28xx_v4l2 *v4l2 = dev->v4l2;
  285. int width = norm_maxw(dev);
  286. int height = norm_maxh(dev);
  287. /* Properly setup VBI */
  288. v4l2->vbi_width = 720;
  289. if (v4l2->norm & V4L2_STD_525_60)
  290. v4l2->vbi_height = 12;
  291. else
  292. v4l2->vbi_height = 18;
  293. em28xx_set_outfmt(dev);
  294. em28xx_accumulator_set(dev, 1, (width - 4) >> 2, 1, (height - 4) >> 2);
  295. /*
  296. * If we don't set the start position to 2 in VBI mode, we end up
  297. * with line 20/21 being YUYV encoded instead of being in 8-bit
  298. * greyscale. The core of the issue is that line 21 (and line 23 for
  299. * PAL WSS) are inside of active video region, and as a result they
  300. * get the pixelformatting associated with that area. So by cropping
  301. * it out, we end up with the same format as the rest of the VBI
  302. * region
  303. */
  304. if (em28xx_vbi_supported(dev) == 1)
  305. em28xx_capture_area_set(dev, 0, 2, width, height);
  306. else
  307. em28xx_capture_area_set(dev, 0, 0, width, height);
  308. return em28xx_scaler_set(dev, v4l2->hscale, v4l2->vscale);
  309. }
  310. /* Set USB alternate setting for analog video */
  311. static int em28xx_set_alternate(struct em28xx *dev)
  312. {
  313. struct em28xx_v4l2 *v4l2 = dev->v4l2;
  314. struct usb_device *udev = interface_to_usbdev(dev->intf);
  315. int err;
  316. int i;
  317. unsigned int min_pkt_size = v4l2->width * 2 + 4;
  318. /*
  319. * NOTE: for isoc transfers, only alt settings > 0 are allowed
  320. * bulk transfers seem to work only with alt=0 !
  321. */
  322. dev->alt = 0;
  323. if (alt > 0 && alt < dev->num_alt) {
  324. em28xx_videodbg("alternate forced to %d\n", dev->alt);
  325. dev->alt = alt;
  326. goto set_alt;
  327. }
  328. if (dev->analog_xfer_bulk)
  329. goto set_alt;
  330. /*
  331. * When image size is bigger than a certain value,
  332. * the frame size should be increased, otherwise, only
  333. * green screen will be received.
  334. */
  335. if (v4l2->width * 2 * v4l2->height > 720 * 240 * 2)
  336. min_pkt_size *= 2;
  337. for (i = 0; i < dev->num_alt; i++) {
  338. /* stop when the selected alt setting offers enough bandwidth */
  339. if (dev->alt_max_pkt_size_isoc[i] >= min_pkt_size) {
  340. dev->alt = i;
  341. break;
  342. /*
  343. * otherwise make sure that we end up with the maximum
  344. * bandwidth because the min_pkt_size equation might be wrong.
  345. *
  346. */
  347. } else if (dev->alt_max_pkt_size_isoc[i] >
  348. dev->alt_max_pkt_size_isoc[dev->alt])
  349. dev->alt = i;
  350. }
  351. set_alt:
  352. /*
  353. * NOTE: for bulk transfers, we need to call usb_set_interface()
  354. * even if the previous settings were the same. Otherwise streaming
  355. * fails with all urbs having status = -EOVERFLOW !
  356. */
  357. if (dev->analog_xfer_bulk) {
  358. dev->max_pkt_size = 512; /* USB 2.0 spec */
  359. dev->packet_multiplier = EM28XX_BULK_PACKET_MULTIPLIER;
  360. } else { /* isoc */
  361. em28xx_videodbg("minimum isoc packet size: %u (alt=%d)\n",
  362. min_pkt_size, dev->alt);
  363. dev->max_pkt_size =
  364. dev->alt_max_pkt_size_isoc[dev->alt];
  365. dev->packet_multiplier = EM28XX_NUM_ISOC_PACKETS;
  366. }
  367. em28xx_videodbg("setting alternate %d with wMaxPacketSize=%u\n",
  368. dev->alt, dev->max_pkt_size);
  369. err = usb_set_interface(udev, dev->ifnum, dev->alt);
  370. if (err < 0) {
  371. dev_err(&dev->intf->dev,
  372. "cannot change alternate number to %d (error=%i)\n",
  373. dev->alt, err);
  374. return err;
  375. }
  376. return 0;
  377. }
  378. /*
  379. * DMA and thread functions
  380. */
  381. /*
  382. * Finish the current buffer
  383. */
  384. static inline void finish_buffer(struct em28xx *dev,
  385. struct em28xx_buffer *buf)
  386. {
  387. em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->top_field);
  388. buf->vb.sequence = dev->v4l2->field_count++;
  389. if (dev->v4l2->progressive)
  390. buf->vb.field = V4L2_FIELD_NONE;
  391. else
  392. buf->vb.field = V4L2_FIELD_INTERLACED;
  393. buf->vb.vb2_buf.timestamp = ktime_get_ns();
  394. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
  395. }
  396. /*
  397. * Copy picture data from USB buffer to videobuf buffer
  398. */
  399. static void em28xx_copy_video(struct em28xx *dev,
  400. struct em28xx_buffer *buf,
  401. unsigned char *usb_buf,
  402. unsigned long len)
  403. {
  404. struct em28xx_v4l2 *v4l2 = dev->v4l2;
  405. void *fieldstart, *startwrite, *startread;
  406. int linesdone, currlinedone, offset, lencopy, remain;
  407. int bytesperline = v4l2->width << 1;
  408. if (buf->pos + len > buf->length)
  409. len = buf->length - buf->pos;
  410. startread = usb_buf;
  411. remain = len;
  412. if (v4l2->progressive || buf->top_field)
  413. fieldstart = buf->vb_buf;
  414. else /* interlaced mode, even nr. of lines */
  415. fieldstart = buf->vb_buf + bytesperline;
  416. linesdone = buf->pos / bytesperline;
  417. currlinedone = buf->pos % bytesperline;
  418. if (v4l2->progressive)
  419. offset = linesdone * bytesperline + currlinedone;
  420. else
  421. offset = linesdone * bytesperline * 2 + currlinedone;
  422. startwrite = fieldstart + offset;
  423. lencopy = bytesperline - currlinedone;
  424. lencopy = lencopy > remain ? remain : lencopy;
  425. if ((char *)startwrite + lencopy > (char *)buf->vb_buf + buf->length) {
  426. em28xx_isocdbg("Overflow of %zu bytes past buffer end (1)\n",
  427. ((char *)startwrite + lencopy) -
  428. ((char *)buf->vb_buf + buf->length));
  429. remain = (char *)buf->vb_buf + buf->length -
  430. (char *)startwrite;
  431. lencopy = remain;
  432. }
  433. if (lencopy <= 0)
  434. return;
  435. memcpy(startwrite, startread, lencopy);
  436. remain -= lencopy;
  437. while (remain > 0) {
  438. if (v4l2->progressive)
  439. startwrite += lencopy;
  440. else
  441. startwrite += lencopy + bytesperline;
  442. startread += lencopy;
  443. if (bytesperline > remain)
  444. lencopy = remain;
  445. else
  446. lencopy = bytesperline;
  447. if ((char *)startwrite + lencopy > (char *)buf->vb_buf +
  448. buf->length) {
  449. em28xx_isocdbg("Overflow of %zu bytes past buffer end(2)\n",
  450. ((char *)startwrite + lencopy) -
  451. ((char *)buf->vb_buf + buf->length));
  452. remain = (char *)buf->vb_buf + buf->length -
  453. (char *)startwrite;
  454. lencopy = remain;
  455. }
  456. if (lencopy <= 0)
  457. break;
  458. memcpy(startwrite, startread, lencopy);
  459. remain -= lencopy;
  460. }
  461. buf->pos += len;
  462. }
  463. /*
  464. * Copy VBI data from USB buffer to videobuf buffer
  465. */
  466. static void em28xx_copy_vbi(struct em28xx *dev,
  467. struct em28xx_buffer *buf,
  468. unsigned char *usb_buf,
  469. unsigned long len)
  470. {
  471. unsigned int offset;
  472. if (buf->pos + len > buf->length)
  473. len = buf->length - buf->pos;
  474. offset = buf->pos;
  475. /* Make sure the bottom field populates the second half of the frame */
  476. if (buf->top_field == 0)
  477. offset += dev->v4l2->vbi_width * dev->v4l2->vbi_height;
  478. memcpy(buf->vb_buf + offset, usb_buf, len);
  479. buf->pos += len;
  480. }
  481. static inline void print_err_status(struct em28xx *dev,
  482. int packet, int status)
  483. {
  484. char *errmsg = "Unknown";
  485. switch (status) {
  486. case -ENOENT:
  487. errmsg = "unlinked synchronously";
  488. break;
  489. case -ECONNRESET:
  490. errmsg = "unlinked asynchronously";
  491. break;
  492. case -ENOSR:
  493. errmsg = "Buffer error (overrun)";
  494. break;
  495. case -EPIPE:
  496. errmsg = "Stalled (device not responding)";
  497. break;
  498. case -EOVERFLOW:
  499. errmsg = "Babble (bad cable?)";
  500. break;
  501. case -EPROTO:
  502. errmsg = "Bit-stuff error (bad cable?)";
  503. break;
  504. case -EILSEQ:
  505. errmsg = "CRC/Timeout (could be anything)";
  506. break;
  507. case -ETIME:
  508. errmsg = "Device does not respond";
  509. break;
  510. }
  511. if (packet < 0) {
  512. em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
  513. } else {
  514. em28xx_isocdbg("URB packet %d, status %d [%s].\n",
  515. packet, status, errmsg);
  516. }
  517. }
  518. /*
  519. * get the next available buffer from dma queue
  520. */
  521. static inline struct em28xx_buffer *get_next_buf(struct em28xx *dev,
  522. struct em28xx_dmaqueue *dma_q)
  523. {
  524. struct em28xx_buffer *buf;
  525. if (list_empty(&dma_q->active)) {
  526. em28xx_isocdbg("No active queue to serve\n");
  527. return NULL;
  528. }
  529. /* Get the next buffer */
  530. buf = list_entry(dma_q->active.next, struct em28xx_buffer, list);
  531. /* Cleans up buffer - Useful for testing for frame/URB loss */
  532. list_del(&buf->list);
  533. buf->pos = 0;
  534. buf->vb_buf = buf->mem;
  535. return buf;
  536. }
  537. /*
  538. * Finish the current buffer if completed and prepare for the next field
  539. */
  540. static struct em28xx_buffer *
  541. finish_field_prepare_next(struct em28xx *dev,
  542. struct em28xx_buffer *buf,
  543. struct em28xx_dmaqueue *dma_q)
  544. {
  545. struct em28xx_v4l2 *v4l2 = dev->v4l2;
  546. if (v4l2->progressive || v4l2->top_field) { /* Brand new frame */
  547. if (buf)
  548. finish_buffer(dev, buf);
  549. buf = get_next_buf(dev, dma_q);
  550. }
  551. if (buf) {
  552. buf->top_field = v4l2->top_field;
  553. buf->pos = 0;
  554. }
  555. return buf;
  556. }
  557. /*
  558. * Process data packet according to the em2710/em2750/em28xx frame data format
  559. */
  560. static inline void process_frame_data_em28xx(struct em28xx *dev,
  561. unsigned char *data_pkt,
  562. unsigned int data_len)
  563. {
  564. struct em28xx_v4l2 *v4l2 = dev->v4l2;
  565. struct em28xx_buffer *buf = dev->usb_ctl.vid_buf;
  566. struct em28xx_buffer *vbi_buf = dev->usb_ctl.vbi_buf;
  567. struct em28xx_dmaqueue *dma_q = &dev->vidq;
  568. struct em28xx_dmaqueue *vbi_dma_q = &dev->vbiq;
  569. /*
  570. * capture type 0 = vbi start
  571. * capture type 1 = vbi in progress
  572. * capture type 2 = video start
  573. * capture type 3 = video in progress
  574. */
  575. if (data_len >= 4) {
  576. /*
  577. * NOTE: Headers are always 4 bytes and
  578. * never split across packets
  579. */
  580. if (data_pkt[0] == 0x88 && data_pkt[1] == 0x88 &&
  581. data_pkt[2] == 0x88 && data_pkt[3] == 0x88) {
  582. /* Continuation */
  583. data_pkt += 4;
  584. data_len -= 4;
  585. } else if (data_pkt[0] == 0x33 && data_pkt[1] == 0x95) {
  586. /* Field start (VBI mode) */
  587. v4l2->capture_type = 0;
  588. v4l2->vbi_read = 0;
  589. em28xx_isocdbg("VBI START HEADER !!!\n");
  590. v4l2->top_field = !(data_pkt[2] & 1);
  591. data_pkt += 4;
  592. data_len -= 4;
  593. } else if (data_pkt[0] == 0x22 && data_pkt[1] == 0x5a) {
  594. /* Field start (VBI disabled) */
  595. v4l2->capture_type = 2;
  596. em28xx_isocdbg("VIDEO START HEADER !!!\n");
  597. v4l2->top_field = !(data_pkt[2] & 1);
  598. data_pkt += 4;
  599. data_len -= 4;
  600. }
  601. }
  602. /*
  603. * NOTE: With bulk transfers, intermediate data packets
  604. * have no continuation header
  605. */
  606. if (v4l2->capture_type == 0) {
  607. vbi_buf = finish_field_prepare_next(dev, vbi_buf, vbi_dma_q);
  608. dev->usb_ctl.vbi_buf = vbi_buf;
  609. v4l2->capture_type = 1;
  610. }
  611. if (v4l2->capture_type == 1) {
  612. int vbi_size = v4l2->vbi_width * v4l2->vbi_height;
  613. int vbi_data_len = ((v4l2->vbi_read + data_len) > vbi_size) ?
  614. (vbi_size - v4l2->vbi_read) : data_len;
  615. /* Copy VBI data */
  616. if (vbi_buf)
  617. em28xx_copy_vbi(dev, vbi_buf, data_pkt, vbi_data_len);
  618. v4l2->vbi_read += vbi_data_len;
  619. if (vbi_data_len < data_len) {
  620. /* Continue with copying video data */
  621. v4l2->capture_type = 2;
  622. data_pkt += vbi_data_len;
  623. data_len -= vbi_data_len;
  624. }
  625. }
  626. if (v4l2->capture_type == 2) {
  627. buf = finish_field_prepare_next(dev, buf, dma_q);
  628. dev->usb_ctl.vid_buf = buf;
  629. v4l2->capture_type = 3;
  630. }
  631. if (v4l2->capture_type == 3 && buf && data_len > 0)
  632. em28xx_copy_video(dev, buf, data_pkt, data_len);
  633. }
  634. /*
  635. * Process data packet according to the em25xx/em276x/7x/8x frame data format
  636. */
  637. static inline void process_frame_data_em25xx(struct em28xx *dev,
  638. unsigned char *data_pkt,
  639. unsigned int data_len)
  640. {
  641. struct em28xx_buffer *buf = dev->usb_ctl.vid_buf;
  642. struct em28xx_dmaqueue *dmaq = &dev->vidq;
  643. struct em28xx_v4l2 *v4l2 = dev->v4l2;
  644. bool frame_end = false;
  645. /* Check for header */
  646. /*
  647. * NOTE: at least with bulk transfers, only the first packet
  648. * has a header and has always set the FRAME_END bit
  649. */
  650. if (data_len >= 2) { /* em25xx header is only 2 bytes long */
  651. if ((data_pkt[0] == EM25XX_FRMDATAHDR_BYTE1) &&
  652. ((data_pkt[1] & ~EM25XX_FRMDATAHDR_BYTE2_MASK) == 0x00)) {
  653. v4l2->top_field = !(data_pkt[1] &
  654. EM25XX_FRMDATAHDR_BYTE2_FRAME_ID);
  655. frame_end = data_pkt[1] &
  656. EM25XX_FRMDATAHDR_BYTE2_FRAME_END;
  657. data_pkt += 2;
  658. data_len -= 2;
  659. }
  660. /* Finish field and prepare next (BULK only) */
  661. if (dev->analog_xfer_bulk && frame_end) {
  662. buf = finish_field_prepare_next(dev, buf, dmaq);
  663. dev->usb_ctl.vid_buf = buf;
  664. }
  665. /*
  666. * NOTE: in ISOC mode when a new frame starts and buf==NULL,
  667. * we COULD already prepare a buffer here to avoid skipping the
  668. * first frame.
  669. */
  670. }
  671. /* Copy data */
  672. if (buf && data_len > 0)
  673. em28xx_copy_video(dev, buf, data_pkt, data_len);
  674. /* Finish frame (ISOC only) => avoids lag of 1 frame */
  675. if (!dev->analog_xfer_bulk && frame_end) {
  676. buf = finish_field_prepare_next(dev, buf, dmaq);
  677. dev->usb_ctl.vid_buf = buf;
  678. }
  679. /*
  680. * NOTES:
  681. *
  682. * 1) Tested with USB bulk transfers only !
  683. * The wording in the datasheet suggests that isoc might work different.
  684. * The current code assumes that with isoc transfers each packet has a
  685. * header like with the other em28xx devices.
  686. *
  687. * 2) Support for interlaced mode is pure theory. It has not been
  688. * tested and it is unknown if these devices actually support it.
  689. */
  690. }
  691. /* Processes and copies the URB data content (video and VBI data) */
  692. static inline int em28xx_urb_data_copy(struct em28xx *dev, struct urb *urb)
  693. {
  694. int xfer_bulk, num_packets, i;
  695. unsigned char *usb_data_pkt;
  696. unsigned int usb_data_len;
  697. if (!dev)
  698. return 0;
  699. if (dev->disconnected)
  700. return 0;
  701. if (urb->status < 0)
  702. print_err_status(dev, -1, urb->status);
  703. xfer_bulk = usb_pipebulk(urb->pipe);
  704. if (xfer_bulk) /* bulk */
  705. num_packets = 1;
  706. else /* isoc */
  707. num_packets = urb->number_of_packets;
  708. for (i = 0; i < num_packets; i++) {
  709. if (xfer_bulk) { /* bulk */
  710. usb_data_len = urb->actual_length;
  711. usb_data_pkt = urb->transfer_buffer;
  712. } else { /* isoc */
  713. if (urb->iso_frame_desc[i].status < 0) {
  714. print_err_status(dev, i,
  715. urb->iso_frame_desc[i].status);
  716. if (urb->iso_frame_desc[i].status != -EPROTO)
  717. continue;
  718. }
  719. usb_data_len = urb->iso_frame_desc[i].actual_length;
  720. if (usb_data_len > dev->max_pkt_size) {
  721. em28xx_isocdbg("packet bigger than packet size");
  722. continue;
  723. }
  724. usb_data_pkt = urb->transfer_buffer +
  725. urb->iso_frame_desc[i].offset;
  726. }
  727. if (usb_data_len == 0) {
  728. /* NOTE: happens very often with isoc transfers */
  729. /* em28xx_usbdbg("packet %d is empty",i); - spammy */
  730. continue;
  731. }
  732. if (dev->is_em25xx)
  733. process_frame_data_em25xx(dev,
  734. usb_data_pkt, usb_data_len);
  735. else
  736. process_frame_data_em28xx(dev,
  737. usb_data_pkt, usb_data_len);
  738. }
  739. return 1;
  740. }
  741. static int get_resource(enum v4l2_buf_type f_type)
  742. {
  743. switch (f_type) {
  744. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  745. return EM28XX_RESOURCE_VIDEO;
  746. case V4L2_BUF_TYPE_VBI_CAPTURE:
  747. return EM28XX_RESOURCE_VBI;
  748. default:
  749. WARN_ON(1);
  750. return -1; /* Indicate that device is busy */
  751. }
  752. }
  753. /* Usage lock check functions */
  754. static int res_get(struct em28xx *dev, enum v4l2_buf_type f_type)
  755. {
  756. int res_type = get_resource(f_type);
  757. /* is it free? */
  758. if (dev->resources & res_type) {
  759. /* no, someone else uses it */
  760. return -EBUSY;
  761. }
  762. /* it's free, grab it */
  763. dev->resources |= res_type;
  764. em28xx_videodbg("res: get %d\n", res_type);
  765. return 0;
  766. }
  767. static void res_free(struct em28xx *dev, enum v4l2_buf_type f_type)
  768. {
  769. int res_type = get_resource(f_type);
  770. dev->resources &= ~res_type;
  771. em28xx_videodbg("res: put %d\n", res_type);
  772. }
  773. static void em28xx_v4l2_media_release(struct em28xx *dev)
  774. {
  775. #ifdef CONFIG_MEDIA_CONTROLLER
  776. int i;
  777. for (i = 0; i < MAX_EM28XX_INPUT; i++) {
  778. if (!INPUT(i)->type)
  779. return;
  780. media_device_unregister_entity(&dev->input_ent[i]);
  781. }
  782. #endif
  783. }
  784. /*
  785. * Media Controller helper functions
  786. */
  787. static int em28xx_enable_analog_tuner(struct em28xx *dev)
  788. {
  789. #ifdef CONFIG_MEDIA_CONTROLLER
  790. struct media_device *mdev = dev->media_dev;
  791. struct em28xx_v4l2 *v4l2 = dev->v4l2;
  792. struct media_entity *source;
  793. struct media_link *link, *found_link = NULL;
  794. int ret, active_links = 0;
  795. if (!mdev || !v4l2->decoder)
  796. return 0;
  797. /*
  798. * This will find the tuner that is connected into the decoder.
  799. * Technically, this is not 100% correct, as the device may be
  800. * using an analog input instead of the tuner. However, as we can't
  801. * do DVB streaming while the DMA engine is being used for V4L2,
  802. * this should be enough for the actual needs.
  803. */
  804. list_for_each_entry(link, &v4l2->decoder->links, list) {
  805. if (link->sink->entity == v4l2->decoder) {
  806. found_link = link;
  807. if (link->flags & MEDIA_LNK_FL_ENABLED)
  808. active_links++;
  809. break;
  810. }
  811. }
  812. if (active_links == 1 || !found_link)
  813. return 0;
  814. source = found_link->source->entity;
  815. list_for_each_entry(link, &source->links, list) {
  816. struct media_entity *sink;
  817. int flags = 0;
  818. sink = link->sink->entity;
  819. if (sink == v4l2->decoder)
  820. flags = MEDIA_LNK_FL_ENABLED;
  821. ret = media_entity_setup_link(link, flags);
  822. if (ret) {
  823. dev_err(&dev->intf->dev,
  824. "Couldn't change link %s->%s to %s. Error %d\n",
  825. source->name, sink->name,
  826. flags ? "enabled" : "disabled",
  827. ret);
  828. return ret;
  829. }
  830. em28xx_videodbg("link %s->%s was %s\n",
  831. source->name, sink->name,
  832. flags ? "ENABLED" : "disabled");
  833. }
  834. #endif
  835. return 0;
  836. }
  837. static const char * const iname[] = {
  838. [EM28XX_VMUX_COMPOSITE] = "Composite",
  839. [EM28XX_VMUX_SVIDEO] = "S-Video",
  840. [EM28XX_VMUX_TELEVISION] = "Television",
  841. [EM28XX_RADIO] = "Radio",
  842. };
  843. static void em28xx_v4l2_create_entities(struct em28xx *dev)
  844. {
  845. #if defined(CONFIG_MEDIA_CONTROLLER)
  846. struct em28xx_v4l2 *v4l2 = dev->v4l2;
  847. int ret, i;
  848. /* Initialize Video, VBI and Radio pads */
  849. v4l2->video_pad.flags = MEDIA_PAD_FL_SINK;
  850. ret = media_entity_pads_init(&v4l2->vdev.entity, 1, &v4l2->video_pad);
  851. if (ret < 0)
  852. dev_err(&dev->intf->dev,
  853. "failed to initialize video media entity!\n");
  854. if (em28xx_vbi_supported(dev)) {
  855. v4l2->vbi_pad.flags = MEDIA_PAD_FL_SINK;
  856. ret = media_entity_pads_init(&v4l2->vbi_dev.entity, 1,
  857. &v4l2->vbi_pad);
  858. if (ret < 0)
  859. dev_err(&dev->intf->dev,
  860. "failed to initialize vbi media entity!\n");
  861. }
  862. /* Webcams don't have input connectors */
  863. if (dev->is_webcam)
  864. return;
  865. /* Create entities for each input connector */
  866. for (i = 0; i < MAX_EM28XX_INPUT; i++) {
  867. struct media_entity *ent = &dev->input_ent[i];
  868. if (!INPUT(i)->type)
  869. break;
  870. ent->name = iname[INPUT(i)->type];
  871. ent->flags = MEDIA_ENT_FL_CONNECTOR;
  872. dev->input_pad[i].flags = MEDIA_PAD_FL_SOURCE;
  873. switch (INPUT(i)->type) {
  874. case EM28XX_VMUX_COMPOSITE:
  875. ent->function = MEDIA_ENT_F_CONN_COMPOSITE;
  876. break;
  877. case EM28XX_VMUX_SVIDEO:
  878. ent->function = MEDIA_ENT_F_CONN_SVIDEO;
  879. break;
  880. default: /* EM28XX_VMUX_TELEVISION or EM28XX_RADIO */
  881. if (dev->tuner_type != TUNER_ABSENT)
  882. ent->function = MEDIA_ENT_F_CONN_RF;
  883. break;
  884. }
  885. ret = media_entity_pads_init(ent, 1, &dev->input_pad[i]);
  886. if (ret < 0)
  887. dev_err(&dev->intf->dev,
  888. "failed to initialize input pad[%d]!\n", i);
  889. ret = media_device_register_entity(dev->media_dev, ent);
  890. if (ret < 0)
  891. dev_err(&dev->intf->dev,
  892. "failed to register input entity %d!\n", i);
  893. }
  894. #endif
  895. }
  896. /*
  897. * Videobuf2 operations
  898. */
  899. static int queue_setup(struct vb2_queue *vq,
  900. unsigned int *nbuffers, unsigned int *nplanes,
  901. unsigned int sizes[], struct device *alloc_devs[])
  902. {
  903. struct em28xx *dev = vb2_get_drv_priv(vq);
  904. struct em28xx_v4l2 *v4l2 = dev->v4l2;
  905. unsigned long size =
  906. (v4l2->width * v4l2->height * v4l2->format->depth + 7) >> 3;
  907. if (*nplanes)
  908. return sizes[0] < size ? -EINVAL : 0;
  909. *nplanes = 1;
  910. sizes[0] = size;
  911. em28xx_enable_analog_tuner(dev);
  912. return 0;
  913. }
  914. static int
  915. buffer_prepare(struct vb2_buffer *vb)
  916. {
  917. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  918. struct em28xx *dev = vb2_get_drv_priv(vb->vb2_queue);
  919. struct em28xx_v4l2 *v4l2 = dev->v4l2;
  920. unsigned long size;
  921. em28xx_videodbg("%s, field=%d\n", __func__, vbuf->field);
  922. size = (v4l2->width * v4l2->height * v4l2->format->depth + 7) >> 3;
  923. if (vb2_plane_size(vb, 0) < size) {
  924. em28xx_videodbg("%s data will not fit into plane (%lu < %lu)\n",
  925. __func__, vb2_plane_size(vb, 0), size);
  926. return -EINVAL;
  927. }
  928. vb2_set_plane_payload(vb, 0, size);
  929. return 0;
  930. }
  931. int em28xx_start_analog_streaming(struct vb2_queue *vq, unsigned int count)
  932. {
  933. struct em28xx *dev = vb2_get_drv_priv(vq);
  934. struct em28xx_v4l2 *v4l2 = dev->v4l2;
  935. struct v4l2_frequency f;
  936. struct v4l2_fh *owner;
  937. int rc = 0;
  938. em28xx_videodbg("%s\n", __func__);
  939. dev->v4l2->field_count = 0;
  940. /*
  941. * Make sure streaming is not already in progress for this type
  942. * of filehandle (e.g. video, vbi)
  943. */
  944. rc = res_get(dev, vq->type);
  945. if (rc)
  946. return rc;
  947. if (v4l2->streaming_users == 0) {
  948. /* First active streaming user, so allocate all the URBs */
  949. /* Allocate the USB bandwidth */
  950. em28xx_set_alternate(dev);
  951. /*
  952. * Needed, since GPIO might have disabled power of
  953. * some i2c device
  954. */
  955. em28xx_wake_i2c(dev);
  956. v4l2->capture_type = -1;
  957. rc = em28xx_init_usb_xfer(dev, EM28XX_ANALOG_MODE,
  958. dev->analog_xfer_bulk,
  959. EM28XX_NUM_BUFS,
  960. dev->max_pkt_size,
  961. dev->packet_multiplier,
  962. em28xx_urb_data_copy);
  963. if (rc < 0)
  964. return rc;
  965. /*
  966. * djh: it's not clear whether this code is still needed. I'm
  967. * leaving it in here for now entirely out of concern for
  968. * backward compatibility (the old code did it)
  969. */
  970. /* Ask tuner to go to analog or radio mode */
  971. memset(&f, 0, sizeof(f));
  972. f.frequency = v4l2->frequency;
  973. owner = (struct v4l2_fh *)vq->owner;
  974. if (owner && owner->vdev->vfl_type == VFL_TYPE_RADIO)
  975. f.type = V4L2_TUNER_RADIO;
  976. else
  977. f.type = V4L2_TUNER_ANALOG_TV;
  978. v4l2_device_call_all(&v4l2->v4l2_dev,
  979. 0, tuner, s_frequency, &f);
  980. /* Enable video stream at TV decoder */
  981. v4l2_device_call_all(&v4l2->v4l2_dev, 0, video, s_stream, 1);
  982. }
  983. v4l2->streaming_users++;
  984. return rc;
  985. }
  986. static void em28xx_stop_streaming(struct vb2_queue *vq)
  987. {
  988. struct em28xx *dev = vb2_get_drv_priv(vq);
  989. struct em28xx_v4l2 *v4l2 = dev->v4l2;
  990. struct em28xx_dmaqueue *vidq = &dev->vidq;
  991. unsigned long flags = 0;
  992. em28xx_videodbg("%s\n", __func__);
  993. res_free(dev, vq->type);
  994. if (v4l2->streaming_users-- == 1) {
  995. /* Disable video stream at TV decoder */
  996. v4l2_device_call_all(&v4l2->v4l2_dev, 0, video, s_stream, 0);
  997. /* Last active user, so shutdown all the URBS */
  998. em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
  999. }
  1000. spin_lock_irqsave(&dev->slock, flags);
  1001. if (dev->usb_ctl.vid_buf) {
  1002. vb2_buffer_done(&dev->usb_ctl.vid_buf->vb.vb2_buf,
  1003. VB2_BUF_STATE_ERROR);
  1004. dev->usb_ctl.vid_buf = NULL;
  1005. }
  1006. while (!list_empty(&vidq->active)) {
  1007. struct em28xx_buffer *buf;
  1008. buf = list_entry(vidq->active.next, struct em28xx_buffer, list);
  1009. list_del(&buf->list);
  1010. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  1011. }
  1012. spin_unlock_irqrestore(&dev->slock, flags);
  1013. }
  1014. void em28xx_stop_vbi_streaming(struct vb2_queue *vq)
  1015. {
  1016. struct em28xx *dev = vb2_get_drv_priv(vq);
  1017. struct em28xx_v4l2 *v4l2 = dev->v4l2;
  1018. struct em28xx_dmaqueue *vbiq = &dev->vbiq;
  1019. unsigned long flags = 0;
  1020. em28xx_videodbg("%s\n", __func__);
  1021. res_free(dev, vq->type);
  1022. if (v4l2->streaming_users-- == 1) {
  1023. /* Disable video stream at TV decoder */
  1024. v4l2_device_call_all(&v4l2->v4l2_dev, 0, video, s_stream, 0);
  1025. /* Last active user, so shutdown all the URBS */
  1026. em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
  1027. }
  1028. spin_lock_irqsave(&dev->slock, flags);
  1029. if (dev->usb_ctl.vbi_buf) {
  1030. vb2_buffer_done(&dev->usb_ctl.vbi_buf->vb.vb2_buf,
  1031. VB2_BUF_STATE_ERROR);
  1032. dev->usb_ctl.vbi_buf = NULL;
  1033. }
  1034. while (!list_empty(&vbiq->active)) {
  1035. struct em28xx_buffer *buf;
  1036. buf = list_entry(vbiq->active.next, struct em28xx_buffer, list);
  1037. list_del(&buf->list);
  1038. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  1039. }
  1040. spin_unlock_irqrestore(&dev->slock, flags);
  1041. }
  1042. static void
  1043. buffer_queue(struct vb2_buffer *vb)
  1044. {
  1045. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  1046. struct em28xx *dev = vb2_get_drv_priv(vb->vb2_queue);
  1047. struct em28xx_buffer *buf =
  1048. container_of(vbuf, struct em28xx_buffer, vb);
  1049. struct em28xx_dmaqueue *vidq = &dev->vidq;
  1050. unsigned long flags = 0;
  1051. em28xx_videodbg("%s\n", __func__);
  1052. buf->mem = vb2_plane_vaddr(vb, 0);
  1053. buf->length = vb2_plane_size(vb, 0);
  1054. spin_lock_irqsave(&dev->slock, flags);
  1055. list_add_tail(&buf->list, &vidq->active);
  1056. spin_unlock_irqrestore(&dev->slock, flags);
  1057. }
  1058. static const struct vb2_ops em28xx_video_qops = {
  1059. .queue_setup = queue_setup,
  1060. .buf_prepare = buffer_prepare,
  1061. .buf_queue = buffer_queue,
  1062. .start_streaming = em28xx_start_analog_streaming,
  1063. .stop_streaming = em28xx_stop_streaming,
  1064. .wait_prepare = vb2_ops_wait_prepare,
  1065. .wait_finish = vb2_ops_wait_finish,
  1066. };
  1067. static int em28xx_vb2_setup(struct em28xx *dev)
  1068. {
  1069. int rc;
  1070. struct vb2_queue *q;
  1071. struct em28xx_v4l2 *v4l2 = dev->v4l2;
  1072. /* Setup Videobuf2 for Video capture */
  1073. q = &v4l2->vb_vidq;
  1074. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1075. q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
  1076. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1077. q->drv_priv = dev;
  1078. q->buf_struct_size = sizeof(struct em28xx_buffer);
  1079. q->ops = &em28xx_video_qops;
  1080. q->mem_ops = &vb2_vmalloc_memops;
  1081. rc = vb2_queue_init(q);
  1082. if (rc < 0)
  1083. return rc;
  1084. /* Setup Videobuf2 for VBI capture */
  1085. q = &v4l2->vb_vbiq;
  1086. q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
  1087. q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR;
  1088. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1089. q->drv_priv = dev;
  1090. q->buf_struct_size = sizeof(struct em28xx_buffer);
  1091. q->ops = &em28xx_vbi_qops;
  1092. q->mem_ops = &vb2_vmalloc_memops;
  1093. rc = vb2_queue_init(q);
  1094. if (rc < 0)
  1095. return rc;
  1096. return 0;
  1097. }
  1098. /*
  1099. * v4l2 interface
  1100. */
  1101. static void video_mux(struct em28xx *dev, int index)
  1102. {
  1103. struct v4l2_device *v4l2_dev = &dev->v4l2->v4l2_dev;
  1104. dev->ctl_input = index;
  1105. dev->ctl_ainput = INPUT(index)->amux;
  1106. dev->ctl_aoutput = INPUT(index)->aout;
  1107. if (!dev->ctl_aoutput)
  1108. dev->ctl_aoutput = EM28XX_AOUT_MASTER;
  1109. v4l2_device_call_all(v4l2_dev, 0, video, s_routing,
  1110. INPUT(index)->vmux, 0, 0);
  1111. if (dev->has_msp34xx) {
  1112. if (dev->i2s_speed) {
  1113. v4l2_device_call_all(v4l2_dev, 0, audio,
  1114. s_i2s_clock_freq, dev->i2s_speed);
  1115. }
  1116. /* Note: this is msp3400 specific */
  1117. v4l2_device_call_all(v4l2_dev, 0, audio, s_routing,
  1118. dev->ctl_ainput,
  1119. MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
  1120. }
  1121. if (dev->board.adecoder != EM28XX_NOADECODER) {
  1122. v4l2_device_call_all(v4l2_dev, 0, audio, s_routing,
  1123. dev->ctl_ainput, dev->ctl_aoutput, 0);
  1124. }
  1125. em28xx_audio_analog_set(dev);
  1126. }
  1127. static void em28xx_ctrl_notify(struct v4l2_ctrl *ctrl, void *priv)
  1128. {
  1129. struct em28xx *dev = priv;
  1130. /*
  1131. * In the case of non-AC97 volume controls, we still need
  1132. * to do some setups at em28xx, in order to mute/unmute
  1133. * and to adjust audio volume. However, the value ranges
  1134. * should be checked by the corresponding V4L subdriver.
  1135. */
  1136. switch (ctrl->id) {
  1137. case V4L2_CID_AUDIO_MUTE:
  1138. dev->mute = ctrl->val;
  1139. em28xx_audio_analog_set(dev);
  1140. break;
  1141. case V4L2_CID_AUDIO_VOLUME:
  1142. dev->volume = ctrl->val;
  1143. em28xx_audio_analog_set(dev);
  1144. break;
  1145. }
  1146. }
  1147. static int em28xx_s_ctrl(struct v4l2_ctrl *ctrl)
  1148. {
  1149. struct em28xx_v4l2 *v4l2 =
  1150. container_of(ctrl->handler, struct em28xx_v4l2, ctrl_handler);
  1151. struct em28xx *dev = v4l2->dev;
  1152. int ret = -EINVAL;
  1153. switch (ctrl->id) {
  1154. case V4L2_CID_AUDIO_MUTE:
  1155. dev->mute = ctrl->val;
  1156. ret = em28xx_audio_analog_set(dev);
  1157. break;
  1158. case V4L2_CID_AUDIO_VOLUME:
  1159. dev->volume = ctrl->val;
  1160. ret = em28xx_audio_analog_set(dev);
  1161. break;
  1162. case V4L2_CID_CONTRAST:
  1163. ret = em28xx_write_reg(dev, EM28XX_R20_YGAIN, ctrl->val);
  1164. break;
  1165. case V4L2_CID_BRIGHTNESS:
  1166. ret = em28xx_write_reg(dev, EM28XX_R21_YOFFSET, ctrl->val);
  1167. break;
  1168. case V4L2_CID_SATURATION:
  1169. ret = em28xx_write_reg(dev, EM28XX_R22_UVGAIN, ctrl->val);
  1170. break;
  1171. case V4L2_CID_BLUE_BALANCE:
  1172. ret = em28xx_write_reg(dev, EM28XX_R23_UOFFSET, ctrl->val);
  1173. break;
  1174. case V4L2_CID_RED_BALANCE:
  1175. ret = em28xx_write_reg(dev, EM28XX_R24_VOFFSET, ctrl->val);
  1176. break;
  1177. case V4L2_CID_SHARPNESS:
  1178. ret = em28xx_write_reg(dev, EM28XX_R25_SHARPNESS, ctrl->val);
  1179. break;
  1180. }
  1181. return (ret < 0) ? ret : 0;
  1182. }
  1183. static const struct v4l2_ctrl_ops em28xx_ctrl_ops = {
  1184. .s_ctrl = em28xx_s_ctrl,
  1185. };
  1186. static void size_to_scale(struct em28xx *dev,
  1187. unsigned int width, unsigned int height,
  1188. unsigned int *hscale, unsigned int *vscale)
  1189. {
  1190. unsigned int maxw = norm_maxw(dev);
  1191. unsigned int maxh = norm_maxh(dev);
  1192. *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
  1193. if (*hscale > EM28XX_HVSCALE_MAX)
  1194. *hscale = EM28XX_HVSCALE_MAX;
  1195. *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
  1196. if (*vscale > EM28XX_HVSCALE_MAX)
  1197. *vscale = EM28XX_HVSCALE_MAX;
  1198. }
  1199. static void scale_to_size(struct em28xx *dev,
  1200. unsigned int hscale, unsigned int vscale,
  1201. unsigned int *width, unsigned int *height)
  1202. {
  1203. unsigned int maxw = norm_maxw(dev);
  1204. unsigned int maxh = norm_maxh(dev);
  1205. *width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
  1206. *height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
  1207. /* Don't let width or height to be zero */
  1208. if (*width < 1)
  1209. *width = 1;
  1210. if (*height < 1)
  1211. *height = 1;
  1212. }
  1213. /*
  1214. * IOCTL vidioc handling
  1215. */
  1216. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  1217. struct v4l2_format *f)
  1218. {
  1219. struct em28xx *dev = video_drvdata(file);
  1220. struct em28xx_v4l2 *v4l2 = dev->v4l2;
  1221. f->fmt.pix.width = v4l2->width;
  1222. f->fmt.pix.height = v4l2->height;
  1223. f->fmt.pix.pixelformat = v4l2->format->fourcc;
  1224. f->fmt.pix.bytesperline = (v4l2->width * v4l2->format->depth + 7) >> 3;
  1225. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * v4l2->height;
  1226. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  1227. /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
  1228. if (v4l2->progressive)
  1229. f->fmt.pix.field = V4L2_FIELD_NONE;
  1230. else
  1231. f->fmt.pix.field = v4l2->interlaced_fieldmode ?
  1232. V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
  1233. return 0;
  1234. }
  1235. static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
  1236. {
  1237. unsigned int i;
  1238. for (i = 0; i < ARRAY_SIZE(format); i++)
  1239. if (format[i].fourcc == fourcc)
  1240. return &format[i];
  1241. return NULL;
  1242. }
  1243. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  1244. struct v4l2_format *f)
  1245. {
  1246. struct em28xx *dev = video_drvdata(file);
  1247. struct em28xx_v4l2 *v4l2 = dev->v4l2;
  1248. unsigned int width = f->fmt.pix.width;
  1249. unsigned int height = f->fmt.pix.height;
  1250. unsigned int maxw = norm_maxw(dev);
  1251. unsigned int maxh = norm_maxh(dev);
  1252. unsigned int hscale, vscale;
  1253. struct em28xx_fmt *fmt;
  1254. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  1255. if (!fmt) {
  1256. fmt = &format[0];
  1257. em28xx_videodbg("Fourcc format (%08x) invalid. Using default (%08x).\n",
  1258. f->fmt.pix.pixelformat, fmt->fourcc);
  1259. }
  1260. if (dev->board.is_em2800) {
  1261. /* the em2800 can only scale down to 50% */
  1262. height = height > (3 * maxh / 4) ? maxh : maxh / 2;
  1263. width = width > (3 * maxw / 4) ? maxw : maxw / 2;
  1264. /*
  1265. * MaxPacketSize for em2800 is too small to capture at full
  1266. * resolution use half of maxw as the scaler can only scale
  1267. * to 50%
  1268. */
  1269. if (width == maxw && height == maxh)
  1270. width /= 2;
  1271. } else {
  1272. /*
  1273. * width must even because of the YUYV format
  1274. * height must be even because of interlacing
  1275. */
  1276. v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh,
  1277. 1, 0);
  1278. }
  1279. /* Avoid division by zero at size_to_scale */
  1280. if (width < 1)
  1281. width = 1;
  1282. if (height < 1)
  1283. height = 1;
  1284. size_to_scale(dev, width, height, &hscale, &vscale);
  1285. scale_to_size(dev, hscale, vscale, &width, &height);
  1286. f->fmt.pix.width = width;
  1287. f->fmt.pix.height = height;
  1288. f->fmt.pix.pixelformat = fmt->fourcc;
  1289. f->fmt.pix.bytesperline = (width * fmt->depth + 7) >> 3;
  1290. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
  1291. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  1292. if (v4l2->progressive)
  1293. f->fmt.pix.field = V4L2_FIELD_NONE;
  1294. else
  1295. f->fmt.pix.field = v4l2->interlaced_fieldmode ?
  1296. V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
  1297. f->fmt.pix.priv = 0;
  1298. return 0;
  1299. }
  1300. static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
  1301. unsigned int width, unsigned int height)
  1302. {
  1303. struct em28xx_fmt *fmt;
  1304. struct em28xx_v4l2 *v4l2 = dev->v4l2;
  1305. fmt = format_by_fourcc(fourcc);
  1306. if (!fmt)
  1307. return -EINVAL;
  1308. v4l2->format = fmt;
  1309. v4l2->width = width;
  1310. v4l2->height = height;
  1311. /* set new image size */
  1312. size_to_scale(dev, v4l2->width, v4l2->height,
  1313. &v4l2->hscale, &v4l2->vscale);
  1314. em28xx_resolution_set(dev);
  1315. return 0;
  1316. }
  1317. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  1318. struct v4l2_format *f)
  1319. {
  1320. struct em28xx *dev = video_drvdata(file);
  1321. struct em28xx_v4l2 *v4l2 = dev->v4l2;
  1322. if (vb2_is_busy(&v4l2->vb_vidq))
  1323. return -EBUSY;
  1324. vidioc_try_fmt_vid_cap(file, priv, f);
  1325. return em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
  1326. f->fmt.pix.width, f->fmt.pix.height);
  1327. }
  1328. static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
  1329. {
  1330. struct em28xx *dev = video_drvdata(file);
  1331. *norm = dev->v4l2->norm;
  1332. return 0;
  1333. }
  1334. static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *norm)
  1335. {
  1336. struct em28xx *dev = video_drvdata(file);
  1337. v4l2_device_call_all(&dev->v4l2->v4l2_dev, 0, video, querystd, norm);
  1338. return 0;
  1339. }
  1340. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
  1341. {
  1342. struct em28xx *dev = video_drvdata(file);
  1343. struct em28xx_v4l2 *v4l2 = dev->v4l2;
  1344. struct v4l2_format f;
  1345. if (norm == v4l2->norm)
  1346. return 0;
  1347. if (v4l2->streaming_users > 0)
  1348. return -EBUSY;
  1349. v4l2->norm = norm;
  1350. /* Adjusts width/height, if needed */
  1351. f.fmt.pix.width = 720;
  1352. f.fmt.pix.height = (norm & V4L2_STD_525_60) ? 480 : 576;
  1353. vidioc_try_fmt_vid_cap(file, priv, &f);
  1354. /* set new image size */
  1355. v4l2->width = f.fmt.pix.width;
  1356. v4l2->height = f.fmt.pix.height;
  1357. size_to_scale(dev, v4l2->width, v4l2->height,
  1358. &v4l2->hscale, &v4l2->vscale);
  1359. em28xx_resolution_set(dev);
  1360. v4l2_device_call_all(&v4l2->v4l2_dev, 0, video, s_std, v4l2->norm);
  1361. return 0;
  1362. }
  1363. static int vidioc_g_parm(struct file *file, void *priv,
  1364. struct v4l2_streamparm *p)
  1365. {
  1366. struct v4l2_subdev_frame_interval ival = { 0 };
  1367. struct em28xx *dev = video_drvdata(file);
  1368. struct em28xx_v4l2 *v4l2 = dev->v4l2;
  1369. int rc = 0;
  1370. if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  1371. p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  1372. return -EINVAL;
  1373. p->parm.capture.readbuffers = EM28XX_MIN_BUF;
  1374. p->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
  1375. if (dev->is_webcam) {
  1376. rc = v4l2_device_call_until_err(&v4l2->v4l2_dev, 0,
  1377. video, g_frame_interval, &ival);
  1378. if (!rc)
  1379. p->parm.capture.timeperframe = ival.interval;
  1380. } else {
  1381. v4l2_video_std_frame_period(v4l2->norm,
  1382. &p->parm.capture.timeperframe);
  1383. }
  1384. return rc;
  1385. }
  1386. static int vidioc_s_parm(struct file *file, void *priv,
  1387. struct v4l2_streamparm *p)
  1388. {
  1389. struct em28xx *dev = video_drvdata(file);
  1390. struct v4l2_subdev_frame_interval ival = {
  1391. 0,
  1392. p->parm.capture.timeperframe
  1393. };
  1394. int rc = 0;
  1395. if (!dev->is_webcam)
  1396. return -ENOTTY;
  1397. if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  1398. p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  1399. return -EINVAL;
  1400. memset(&p->parm, 0, sizeof(p->parm));
  1401. p->parm.capture.readbuffers = EM28XX_MIN_BUF;
  1402. p->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
  1403. rc = v4l2_device_call_until_err(&dev->v4l2->v4l2_dev, 0,
  1404. video, s_frame_interval, &ival);
  1405. if (!rc)
  1406. p->parm.capture.timeperframe = ival.interval;
  1407. return rc;
  1408. }
  1409. static int vidioc_enum_input(struct file *file, void *priv,
  1410. struct v4l2_input *i)
  1411. {
  1412. struct em28xx *dev = video_drvdata(file);
  1413. unsigned int n;
  1414. int j;
  1415. n = i->index;
  1416. if (n >= MAX_EM28XX_INPUT)
  1417. return -EINVAL;
  1418. if (!INPUT(n)->type)
  1419. return -EINVAL;
  1420. i->type = V4L2_INPUT_TYPE_CAMERA;
  1421. strcpy(i->name, iname[INPUT(n)->type]);
  1422. if (INPUT(n)->type == EM28XX_VMUX_TELEVISION)
  1423. i->type = V4L2_INPUT_TYPE_TUNER;
  1424. i->std = dev->v4l2->vdev.tvnorms;
  1425. /* webcams do not have the STD API */
  1426. if (dev->is_webcam)
  1427. i->capabilities = 0;
  1428. /* Dynamically generates an audioset bitmask */
  1429. i->audioset = 0;
  1430. for (j = 0; j < MAX_EM28XX_INPUT; j++)
  1431. if (dev->amux_map[j] != EM28XX_AMUX_UNUSED)
  1432. i->audioset |= 1 << j;
  1433. return 0;
  1434. }
  1435. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  1436. {
  1437. struct em28xx *dev = video_drvdata(file);
  1438. *i = dev->ctl_input;
  1439. return 0;
  1440. }
  1441. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  1442. {
  1443. struct em28xx *dev = video_drvdata(file);
  1444. if (i >= MAX_EM28XX_INPUT)
  1445. return -EINVAL;
  1446. if (!INPUT(i)->type)
  1447. return -EINVAL;
  1448. video_mux(dev, i);
  1449. return 0;
  1450. }
  1451. static int em28xx_fill_audio_input(struct em28xx *dev,
  1452. const char *s,
  1453. struct v4l2_audio *a,
  1454. unsigned int index)
  1455. {
  1456. unsigned int idx = dev->amux_map[index];
  1457. /*
  1458. * With msp3400, almost all mappings use the default (amux = 0).
  1459. * The only one may use a different value is WinTV USB2, where it
  1460. * can also be SCART1 input.
  1461. * As it is very doubtful that we would see new boards with msp3400,
  1462. * let's just reuse the existing switch.
  1463. */
  1464. if (dev->has_msp34xx && idx != EM28XX_AMUX_UNUSED)
  1465. idx = EM28XX_AMUX_LINE_IN;
  1466. switch (idx) {
  1467. case EM28XX_AMUX_VIDEO:
  1468. strcpy(a->name, "Television");
  1469. break;
  1470. case EM28XX_AMUX_LINE_IN:
  1471. strcpy(a->name, "Line In");
  1472. break;
  1473. case EM28XX_AMUX_VIDEO2:
  1474. strcpy(a->name, "Television alt");
  1475. break;
  1476. case EM28XX_AMUX_PHONE:
  1477. strcpy(a->name, "Phone");
  1478. break;
  1479. case EM28XX_AMUX_MIC:
  1480. strcpy(a->name, "Mic");
  1481. break;
  1482. case EM28XX_AMUX_CD:
  1483. strcpy(a->name, "CD");
  1484. break;
  1485. case EM28XX_AMUX_AUX:
  1486. strcpy(a->name, "Aux");
  1487. break;
  1488. case EM28XX_AMUX_PCM_OUT:
  1489. strcpy(a->name, "PCM");
  1490. break;
  1491. case EM28XX_AMUX_UNUSED:
  1492. default:
  1493. return -EINVAL;
  1494. }
  1495. a->index = index;
  1496. a->capability = V4L2_AUDCAP_STEREO;
  1497. em28xx_videodbg("%s: audio input index %d is '%s'\n",
  1498. s, a->index, a->name);
  1499. return 0;
  1500. }
  1501. static int vidioc_enumaudio(struct file *file, void *fh, struct v4l2_audio *a)
  1502. {
  1503. struct em28xx *dev = video_drvdata(file);
  1504. if (a->index >= MAX_EM28XX_INPUT)
  1505. return -EINVAL;
  1506. return em28xx_fill_audio_input(dev, __func__, a, a->index);
  1507. }
  1508. static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  1509. {
  1510. struct em28xx *dev = video_drvdata(file);
  1511. int i;
  1512. for (i = 0; i < MAX_EM28XX_INPUT; i++)
  1513. if (dev->ctl_ainput == dev->amux_map[i])
  1514. return em28xx_fill_audio_input(dev, __func__, a, i);
  1515. /* Should never happen! */
  1516. return -EINVAL;
  1517. }
  1518. static int vidioc_s_audio(struct file *file, void *priv,
  1519. const struct v4l2_audio *a)
  1520. {
  1521. struct em28xx *dev = video_drvdata(file);
  1522. int idx, i;
  1523. if (a->index >= MAX_EM28XX_INPUT)
  1524. return -EINVAL;
  1525. idx = dev->amux_map[a->index];
  1526. if (idx == EM28XX_AMUX_UNUSED)
  1527. return -EINVAL;
  1528. dev->ctl_ainput = idx;
  1529. /*
  1530. * FIXME: This is wrong, as different inputs at em28xx_cards
  1531. * may have different audio outputs. So, the right thing
  1532. * to do is to implement VIDIOC_G_AUDOUT/VIDIOC_S_AUDOUT.
  1533. * With the current board definitions, this would work fine,
  1534. * as, currently, all boards fit.
  1535. */
  1536. for (i = 0; i < MAX_EM28XX_INPUT; i++)
  1537. if (idx == dev->amux_map[i])
  1538. break;
  1539. if (i == MAX_EM28XX_INPUT)
  1540. return -EINVAL;
  1541. dev->ctl_aoutput = INPUT(i)->aout;
  1542. if (!dev->ctl_aoutput)
  1543. dev->ctl_aoutput = EM28XX_AOUT_MASTER;
  1544. em28xx_videodbg("%s: set audio input to %d\n", __func__,
  1545. dev->ctl_ainput);
  1546. return 0;
  1547. }
  1548. static int vidioc_g_tuner(struct file *file, void *priv,
  1549. struct v4l2_tuner *t)
  1550. {
  1551. struct em28xx *dev = video_drvdata(file);
  1552. if (t->index != 0)
  1553. return -EINVAL;
  1554. strcpy(t->name, "Tuner");
  1555. v4l2_device_call_all(&dev->v4l2->v4l2_dev, 0, tuner, g_tuner, t);
  1556. return 0;
  1557. }
  1558. static int vidioc_s_tuner(struct file *file, void *priv,
  1559. const struct v4l2_tuner *t)
  1560. {
  1561. struct em28xx *dev = video_drvdata(file);
  1562. if (t->index != 0)
  1563. return -EINVAL;
  1564. v4l2_device_call_all(&dev->v4l2->v4l2_dev, 0, tuner, s_tuner, t);
  1565. return 0;
  1566. }
  1567. static int vidioc_g_frequency(struct file *file, void *priv,
  1568. struct v4l2_frequency *f)
  1569. {
  1570. struct em28xx *dev = video_drvdata(file);
  1571. struct em28xx_v4l2 *v4l2 = dev->v4l2;
  1572. if (f->tuner != 0)
  1573. return -EINVAL;
  1574. f->frequency = v4l2->frequency;
  1575. return 0;
  1576. }
  1577. static int vidioc_s_frequency(struct file *file, void *priv,
  1578. const struct v4l2_frequency *f)
  1579. {
  1580. struct v4l2_frequency new_freq = *f;
  1581. struct em28xx *dev = video_drvdata(file);
  1582. struct em28xx_v4l2 *v4l2 = dev->v4l2;
  1583. if (f->tuner != 0)
  1584. return -EINVAL;
  1585. v4l2_device_call_all(&v4l2->v4l2_dev, 0, tuner, s_frequency, f);
  1586. v4l2_device_call_all(&v4l2->v4l2_dev, 0, tuner, g_frequency, &new_freq);
  1587. v4l2->frequency = new_freq.frequency;
  1588. return 0;
  1589. }
  1590. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1591. static int vidioc_g_chip_info(struct file *file, void *priv,
  1592. struct v4l2_dbg_chip_info *chip)
  1593. {
  1594. struct em28xx *dev = video_drvdata(file);
  1595. if (chip->match.addr > 1)
  1596. return -EINVAL;
  1597. if (chip->match.addr == 1)
  1598. strlcpy(chip->name, "ac97", sizeof(chip->name));
  1599. else
  1600. strlcpy(chip->name,
  1601. dev->v4l2->v4l2_dev.name, sizeof(chip->name));
  1602. return 0;
  1603. }
  1604. static int em28xx_reg_len(int reg)
  1605. {
  1606. switch (reg) {
  1607. case EM28XX_R40_AC97LSB:
  1608. case EM28XX_R30_HSCALELOW:
  1609. case EM28XX_R32_VSCALELOW:
  1610. return 2;
  1611. default:
  1612. return 1;
  1613. }
  1614. }
  1615. static int vidioc_g_register(struct file *file, void *priv,
  1616. struct v4l2_dbg_register *reg)
  1617. {
  1618. struct em28xx *dev = video_drvdata(file);
  1619. int ret;
  1620. if (reg->match.addr > 1)
  1621. return -EINVAL;
  1622. if (reg->match.addr) {
  1623. ret = em28xx_read_ac97(dev, reg->reg);
  1624. if (ret < 0)
  1625. return ret;
  1626. reg->val = ret;
  1627. reg->size = 1;
  1628. return 0;
  1629. }
  1630. /* Match host */
  1631. reg->size = em28xx_reg_len(reg->reg);
  1632. if (reg->size == 1) {
  1633. ret = em28xx_read_reg(dev, reg->reg);
  1634. if (ret < 0)
  1635. return ret;
  1636. reg->val = ret;
  1637. } else {
  1638. __le16 val = 0;
  1639. ret = dev->em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
  1640. reg->reg, (char *)&val, 2);
  1641. if (ret < 0)
  1642. return ret;
  1643. reg->val = le16_to_cpu(val);
  1644. }
  1645. return 0;
  1646. }
  1647. static int vidioc_s_register(struct file *file, void *priv,
  1648. const struct v4l2_dbg_register *reg)
  1649. {
  1650. struct em28xx *dev = video_drvdata(file);
  1651. __le16 buf;
  1652. if (reg->match.addr > 1)
  1653. return -EINVAL;
  1654. if (reg->match.addr)
  1655. return em28xx_write_ac97(dev, reg->reg, reg->val);
  1656. /* Match host */
  1657. buf = cpu_to_le16(reg->val);
  1658. return em28xx_write_regs(dev, reg->reg, (char *)&buf,
  1659. em28xx_reg_len(reg->reg));
  1660. }
  1661. #endif
  1662. static int vidioc_querycap(struct file *file, void *priv,
  1663. struct v4l2_capability *cap)
  1664. {
  1665. struct video_device *vdev = video_devdata(file);
  1666. struct em28xx *dev = video_drvdata(file);
  1667. struct em28xx_v4l2 *v4l2 = dev->v4l2;
  1668. struct usb_device *udev = interface_to_usbdev(dev->intf);
  1669. strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
  1670. strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
  1671. usb_make_path(udev, cap->bus_info, sizeof(cap->bus_info));
  1672. if (vdev->vfl_type == VFL_TYPE_GRABBER)
  1673. cap->device_caps = V4L2_CAP_READWRITE |
  1674. V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  1675. else if (vdev->vfl_type == VFL_TYPE_RADIO)
  1676. cap->device_caps = V4L2_CAP_RADIO;
  1677. else
  1678. cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_VBI_CAPTURE;
  1679. if (dev->int_audio_type != EM28XX_INT_AUDIO_NONE)
  1680. cap->device_caps |= V4L2_CAP_AUDIO;
  1681. if (dev->tuner_type != TUNER_ABSENT)
  1682. cap->device_caps |= V4L2_CAP_TUNER;
  1683. cap->capabilities = cap->device_caps |
  1684. V4L2_CAP_DEVICE_CAPS | V4L2_CAP_READWRITE |
  1685. V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  1686. if (video_is_registered(&v4l2->vbi_dev))
  1687. cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
  1688. if (video_is_registered(&v4l2->radio_dev))
  1689. cap->capabilities |= V4L2_CAP_RADIO;
  1690. return 0;
  1691. }
  1692. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  1693. struct v4l2_fmtdesc *f)
  1694. {
  1695. if (unlikely(f->index >= ARRAY_SIZE(format)))
  1696. return -EINVAL;
  1697. strlcpy(f->description, format[f->index].name, sizeof(f->description));
  1698. f->pixelformat = format[f->index].fourcc;
  1699. return 0;
  1700. }
  1701. static int vidioc_enum_framesizes(struct file *file, void *priv,
  1702. struct v4l2_frmsizeenum *fsize)
  1703. {
  1704. struct em28xx *dev = video_drvdata(file);
  1705. struct em28xx_fmt *fmt;
  1706. unsigned int maxw = norm_maxw(dev);
  1707. unsigned int maxh = norm_maxh(dev);
  1708. fmt = format_by_fourcc(fsize->pixel_format);
  1709. if (!fmt) {
  1710. em28xx_videodbg("Fourcc format (%08x) invalid.\n",
  1711. fsize->pixel_format);
  1712. return -EINVAL;
  1713. }
  1714. if (dev->board.is_em2800) {
  1715. if (fsize->index > 1)
  1716. return -EINVAL;
  1717. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  1718. fsize->discrete.width = maxw / (1 + fsize->index);
  1719. fsize->discrete.height = maxh / (1 + fsize->index);
  1720. return 0;
  1721. }
  1722. if (fsize->index != 0)
  1723. return -EINVAL;
  1724. /* Report a continuous range */
  1725. fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
  1726. scale_to_size(dev, EM28XX_HVSCALE_MAX, EM28XX_HVSCALE_MAX,
  1727. &fsize->stepwise.min_width, &fsize->stepwise.min_height);
  1728. if (fsize->stepwise.min_width < 48)
  1729. fsize->stepwise.min_width = 48;
  1730. if (fsize->stepwise.min_height < 38)
  1731. fsize->stepwise.min_height = 38;
  1732. fsize->stepwise.max_width = maxw;
  1733. fsize->stepwise.max_height = maxh;
  1734. fsize->stepwise.step_width = 1;
  1735. fsize->stepwise.step_height = 1;
  1736. return 0;
  1737. }
  1738. /* RAW VBI ioctls */
  1739. static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
  1740. struct v4l2_format *format)
  1741. {
  1742. struct em28xx *dev = video_drvdata(file);
  1743. struct em28xx_v4l2 *v4l2 = dev->v4l2;
  1744. format->fmt.vbi.samples_per_line = v4l2->vbi_width;
  1745. format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  1746. format->fmt.vbi.offset = 0;
  1747. format->fmt.vbi.flags = 0;
  1748. format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
  1749. format->fmt.vbi.count[0] = v4l2->vbi_height;
  1750. format->fmt.vbi.count[1] = v4l2->vbi_height;
  1751. memset(format->fmt.vbi.reserved, 0, sizeof(format->fmt.vbi.reserved));
  1752. /* Varies by video standard (NTSC, PAL, etc.) */
  1753. if (v4l2->norm & V4L2_STD_525_60) {
  1754. /* NTSC */
  1755. format->fmt.vbi.start[0] = 10;
  1756. format->fmt.vbi.start[1] = 273;
  1757. } else if (v4l2->norm & V4L2_STD_625_50) {
  1758. /* PAL */
  1759. format->fmt.vbi.start[0] = 6;
  1760. format->fmt.vbi.start[1] = 318;
  1761. }
  1762. return 0;
  1763. }
  1764. /*
  1765. * RADIO ESPECIFIC IOCTLS
  1766. */
  1767. static int radio_g_tuner(struct file *file, void *priv,
  1768. struct v4l2_tuner *t)
  1769. {
  1770. struct em28xx *dev = video_drvdata(file);
  1771. if (unlikely(t->index > 0))
  1772. return -EINVAL;
  1773. strcpy(t->name, "Radio");
  1774. v4l2_device_call_all(&dev->v4l2->v4l2_dev, 0, tuner, g_tuner, t);
  1775. return 0;
  1776. }
  1777. static int radio_s_tuner(struct file *file, void *priv,
  1778. const struct v4l2_tuner *t)
  1779. {
  1780. struct em28xx *dev = video_drvdata(file);
  1781. if (t->index != 0)
  1782. return -EINVAL;
  1783. v4l2_device_call_all(&dev->v4l2->v4l2_dev, 0, tuner, s_tuner, t);
  1784. return 0;
  1785. }
  1786. /*
  1787. * em28xx_free_v4l2() - Free struct em28xx_v4l2
  1788. *
  1789. * @ref: struct kref for struct em28xx_v4l2
  1790. *
  1791. * Called when all users of struct em28xx_v4l2 are gone
  1792. */
  1793. static void em28xx_free_v4l2(struct kref *ref)
  1794. {
  1795. struct em28xx_v4l2 *v4l2 = container_of(ref, struct em28xx_v4l2, ref);
  1796. v4l2->dev->v4l2 = NULL;
  1797. kfree(v4l2);
  1798. }
  1799. /*
  1800. * em28xx_v4l2_open()
  1801. * inits the device and starts isoc transfer
  1802. */
  1803. static int em28xx_v4l2_open(struct file *filp)
  1804. {
  1805. struct video_device *vdev = video_devdata(filp);
  1806. struct em28xx *dev = video_drvdata(filp);
  1807. struct em28xx_v4l2 *v4l2 = dev->v4l2;
  1808. enum v4l2_buf_type fh_type = 0;
  1809. int ret;
  1810. switch (vdev->vfl_type) {
  1811. case VFL_TYPE_GRABBER:
  1812. fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1813. break;
  1814. case VFL_TYPE_VBI:
  1815. fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
  1816. break;
  1817. case VFL_TYPE_RADIO:
  1818. break;
  1819. default:
  1820. return -EINVAL;
  1821. }
  1822. em28xx_videodbg("open dev=%s type=%s users=%d\n",
  1823. video_device_node_name(vdev), v4l2_type_names[fh_type],
  1824. v4l2->users);
  1825. if (mutex_lock_interruptible(&dev->lock))
  1826. return -ERESTARTSYS;
  1827. ret = v4l2_fh_open(filp);
  1828. if (ret) {
  1829. dev_err(&dev->intf->dev,
  1830. "%s: v4l2_fh_open() returned error %d\n",
  1831. __func__, ret);
  1832. mutex_unlock(&dev->lock);
  1833. return ret;
  1834. }
  1835. if (v4l2->users == 0) {
  1836. em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
  1837. if (vdev->vfl_type != VFL_TYPE_RADIO)
  1838. em28xx_resolution_set(dev);
  1839. /*
  1840. * Needed, since GPIO might have disabled power
  1841. * of some i2c devices
  1842. */
  1843. em28xx_wake_i2c(dev);
  1844. }
  1845. if (vdev->vfl_type == VFL_TYPE_RADIO) {
  1846. em28xx_videodbg("video_open: setting radio device\n");
  1847. v4l2_device_call_all(&v4l2->v4l2_dev, 0, tuner, s_radio);
  1848. }
  1849. kref_get(&dev->ref);
  1850. kref_get(&v4l2->ref);
  1851. v4l2->users++;
  1852. mutex_unlock(&dev->lock);
  1853. return 0;
  1854. }
  1855. /*
  1856. * em28xx_v4l2_fini()
  1857. * unregisters the v4l2,i2c and usb devices
  1858. * called when the device gets disconected or at module unload
  1859. */
  1860. static int em28xx_v4l2_fini(struct em28xx *dev)
  1861. {
  1862. struct em28xx_v4l2 *v4l2 = dev->v4l2;
  1863. if (dev->is_audio_only) {
  1864. /* Shouldn't initialize IR for this interface */
  1865. return 0;
  1866. }
  1867. if (!dev->has_video) {
  1868. /* This device does not support the v4l2 extension */
  1869. return 0;
  1870. }
  1871. if (!v4l2)
  1872. return 0;
  1873. dev_info(&dev->intf->dev, "Closing video extension\n");
  1874. mutex_lock(&dev->lock);
  1875. v4l2_device_disconnect(&v4l2->v4l2_dev);
  1876. em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
  1877. em28xx_v4l2_media_release(dev);
  1878. if (video_is_registered(&v4l2->radio_dev)) {
  1879. dev_info(&dev->intf->dev, "V4L2 device %s deregistered\n",
  1880. video_device_node_name(&v4l2->radio_dev));
  1881. video_unregister_device(&v4l2->radio_dev);
  1882. }
  1883. if (video_is_registered(&v4l2->vbi_dev)) {
  1884. dev_info(&dev->intf->dev, "V4L2 device %s deregistered\n",
  1885. video_device_node_name(&v4l2->vbi_dev));
  1886. video_unregister_device(&v4l2->vbi_dev);
  1887. }
  1888. if (video_is_registered(&v4l2->vdev)) {
  1889. dev_info(&dev->intf->dev, "V4L2 device %s deregistered\n",
  1890. video_device_node_name(&v4l2->vdev));
  1891. video_unregister_device(&v4l2->vdev);
  1892. }
  1893. v4l2_ctrl_handler_free(&v4l2->ctrl_handler);
  1894. v4l2_device_unregister(&v4l2->v4l2_dev);
  1895. kref_put(&v4l2->ref, em28xx_free_v4l2);
  1896. mutex_unlock(&dev->lock);
  1897. kref_put(&dev->ref, em28xx_free_device);
  1898. return 0;
  1899. }
  1900. static int em28xx_v4l2_suspend(struct em28xx *dev)
  1901. {
  1902. if (dev->is_audio_only)
  1903. return 0;
  1904. if (!dev->has_video)
  1905. return 0;
  1906. dev_info(&dev->intf->dev, "Suspending video extension\n");
  1907. em28xx_stop_urbs(dev);
  1908. return 0;
  1909. }
  1910. static int em28xx_v4l2_resume(struct em28xx *dev)
  1911. {
  1912. if (dev->is_audio_only)
  1913. return 0;
  1914. if (!dev->has_video)
  1915. return 0;
  1916. dev_info(&dev->intf->dev, "Resuming video extension\n");
  1917. /* what do we do here */
  1918. return 0;
  1919. }
  1920. /*
  1921. * em28xx_v4l2_close()
  1922. * stops streaming and deallocates all resources allocated by the v4l2
  1923. * calls and ioctls
  1924. */
  1925. static int em28xx_v4l2_close(struct file *filp)
  1926. {
  1927. struct em28xx *dev = video_drvdata(filp);
  1928. struct em28xx_v4l2 *v4l2 = dev->v4l2;
  1929. struct usb_device *udev = interface_to_usbdev(dev->intf);
  1930. int err;
  1931. em28xx_videodbg("users=%d\n", v4l2->users);
  1932. vb2_fop_release(filp);
  1933. mutex_lock(&dev->lock);
  1934. if (v4l2->users == 1) {
  1935. /* No sense to try to write to the device */
  1936. if (dev->disconnected)
  1937. goto exit;
  1938. /* Save some power by putting tuner to sleep */
  1939. v4l2_device_call_all(&v4l2->v4l2_dev, 0, tuner, standby);
  1940. /* do this before setting alternate! */
  1941. em28xx_set_mode(dev, EM28XX_SUSPEND);
  1942. /* set alternate 0 */
  1943. dev->alt = 0;
  1944. em28xx_videodbg("setting alternate 0\n");
  1945. err = usb_set_interface(udev, 0, 0);
  1946. if (err < 0) {
  1947. dev_err(&dev->intf->dev,
  1948. "cannot change alternate number to 0 (error=%i)\n",
  1949. err);
  1950. }
  1951. }
  1952. exit:
  1953. v4l2->users--;
  1954. kref_put(&v4l2->ref, em28xx_free_v4l2);
  1955. mutex_unlock(&dev->lock);
  1956. kref_put(&dev->ref, em28xx_free_device);
  1957. return 0;
  1958. }
  1959. static const struct v4l2_file_operations em28xx_v4l_fops = {
  1960. .owner = THIS_MODULE,
  1961. .open = em28xx_v4l2_open,
  1962. .release = em28xx_v4l2_close,
  1963. .read = vb2_fop_read,
  1964. .poll = vb2_fop_poll,
  1965. .mmap = vb2_fop_mmap,
  1966. .unlocked_ioctl = video_ioctl2,
  1967. };
  1968. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1969. .vidioc_querycap = vidioc_querycap,
  1970. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1971. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1972. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1973. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1974. .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
  1975. .vidioc_try_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
  1976. .vidioc_s_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
  1977. .vidioc_enum_framesizes = vidioc_enum_framesizes,
  1978. .vidioc_enumaudio = vidioc_enumaudio,
  1979. .vidioc_g_audio = vidioc_g_audio,
  1980. .vidioc_s_audio = vidioc_s_audio,
  1981. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  1982. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  1983. .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  1984. .vidioc_querybuf = vb2_ioctl_querybuf,
  1985. .vidioc_qbuf = vb2_ioctl_qbuf,
  1986. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  1987. .vidioc_g_std = vidioc_g_std,
  1988. .vidioc_querystd = vidioc_querystd,
  1989. .vidioc_s_std = vidioc_s_std,
  1990. .vidioc_g_parm = vidioc_g_parm,
  1991. .vidioc_s_parm = vidioc_s_parm,
  1992. .vidioc_enum_input = vidioc_enum_input,
  1993. .vidioc_g_input = vidioc_g_input,
  1994. .vidioc_s_input = vidioc_s_input,
  1995. .vidioc_streamon = vb2_ioctl_streamon,
  1996. .vidioc_streamoff = vb2_ioctl_streamoff,
  1997. .vidioc_g_tuner = vidioc_g_tuner,
  1998. .vidioc_s_tuner = vidioc_s_tuner,
  1999. .vidioc_g_frequency = vidioc_g_frequency,
  2000. .vidioc_s_frequency = vidioc_s_frequency,
  2001. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  2002. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  2003. #ifdef CONFIG_VIDEO_ADV_DEBUG
  2004. .vidioc_g_chip_info = vidioc_g_chip_info,
  2005. .vidioc_g_register = vidioc_g_register,
  2006. .vidioc_s_register = vidioc_s_register,
  2007. #endif
  2008. };
  2009. static const struct video_device em28xx_video_template = {
  2010. .fops = &em28xx_v4l_fops,
  2011. .ioctl_ops = &video_ioctl_ops,
  2012. .release = video_device_release_empty,
  2013. .tvnorms = V4L2_STD_ALL,
  2014. };
  2015. static const struct v4l2_file_operations radio_fops = {
  2016. .owner = THIS_MODULE,
  2017. .open = em28xx_v4l2_open,
  2018. .release = em28xx_v4l2_close,
  2019. .unlocked_ioctl = video_ioctl2,
  2020. };
  2021. static const struct v4l2_ioctl_ops radio_ioctl_ops = {
  2022. .vidioc_querycap = vidioc_querycap,
  2023. .vidioc_g_tuner = radio_g_tuner,
  2024. .vidioc_s_tuner = radio_s_tuner,
  2025. .vidioc_g_frequency = vidioc_g_frequency,
  2026. .vidioc_s_frequency = vidioc_s_frequency,
  2027. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  2028. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  2029. #ifdef CONFIG_VIDEO_ADV_DEBUG
  2030. .vidioc_g_chip_info = vidioc_g_chip_info,
  2031. .vidioc_g_register = vidioc_g_register,
  2032. .vidioc_s_register = vidioc_s_register,
  2033. #endif
  2034. };
  2035. static struct video_device em28xx_radio_template = {
  2036. .fops = &radio_fops,
  2037. .ioctl_ops = &radio_ioctl_ops,
  2038. .release = video_device_release_empty,
  2039. };
  2040. /* I2C possible address to saa7115, tvp5150, msp3400, tvaudio */
  2041. static unsigned short saa711x_addrs[] = {
  2042. 0x4a >> 1, 0x48 >> 1, /* SAA7111, SAA7111A and SAA7113 */
  2043. 0x42 >> 1, 0x40 >> 1, /* SAA7114, SAA7115 and SAA7118 */
  2044. I2C_CLIENT_END };
  2045. static unsigned short tvp5150_addrs[] = {
  2046. 0xb8 >> 1,
  2047. 0xba >> 1,
  2048. I2C_CLIENT_END
  2049. };
  2050. static unsigned short msp3400_addrs[] = {
  2051. 0x80 >> 1,
  2052. 0x88 >> 1,
  2053. I2C_CLIENT_END
  2054. };
  2055. /******************************** usb interface ******************************/
  2056. static void em28xx_vdev_init(struct em28xx *dev,
  2057. struct video_device *vfd,
  2058. const struct video_device *template,
  2059. const char *type_name)
  2060. {
  2061. *vfd = *template;
  2062. vfd->v4l2_dev = &dev->v4l2->v4l2_dev;
  2063. vfd->lock = &dev->lock;
  2064. if (dev->is_webcam)
  2065. vfd->tvnorms = 0;
  2066. snprintf(vfd->name, sizeof(vfd->name), "%s %s",
  2067. dev_name(&dev->intf->dev), type_name);
  2068. video_set_drvdata(vfd, dev);
  2069. }
  2070. static void em28xx_tuner_setup(struct em28xx *dev, unsigned short tuner_addr)
  2071. {
  2072. struct em28xx_v4l2 *v4l2 = dev->v4l2;
  2073. struct v4l2_device *v4l2_dev = &v4l2->v4l2_dev;
  2074. struct tuner_setup tun_setup;
  2075. struct v4l2_frequency f;
  2076. memset(&tun_setup, 0, sizeof(tun_setup));
  2077. tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
  2078. tun_setup.tuner_callback = em28xx_tuner_callback;
  2079. if (dev->board.radio.type) {
  2080. tun_setup.type = dev->board.radio.type;
  2081. tun_setup.addr = dev->board.radio_addr;
  2082. v4l2_device_call_all(v4l2_dev,
  2083. 0, tuner, s_type_addr, &tun_setup);
  2084. }
  2085. if (dev->tuner_type != TUNER_ABSENT && dev->tuner_type) {
  2086. tun_setup.type = dev->tuner_type;
  2087. tun_setup.addr = tuner_addr;
  2088. v4l2_device_call_all(v4l2_dev,
  2089. 0, tuner, s_type_addr, &tun_setup);
  2090. }
  2091. if (dev->board.tda9887_conf) {
  2092. struct v4l2_priv_tun_config tda9887_cfg;
  2093. tda9887_cfg.tuner = TUNER_TDA9887;
  2094. tda9887_cfg.priv = &dev->board.tda9887_conf;
  2095. v4l2_device_call_all(v4l2_dev,
  2096. 0, tuner, s_config, &tda9887_cfg);
  2097. }
  2098. if (dev->tuner_type == TUNER_XC2028) {
  2099. struct v4l2_priv_tun_config xc2028_cfg;
  2100. struct xc2028_ctrl ctl;
  2101. memset(&xc2028_cfg, 0, sizeof(xc2028_cfg));
  2102. memset(&ctl, 0, sizeof(ctl));
  2103. em28xx_setup_xc3028(dev, &ctl);
  2104. xc2028_cfg.tuner = TUNER_XC2028;
  2105. xc2028_cfg.priv = &ctl;
  2106. v4l2_device_call_all(v4l2_dev, 0, tuner, s_config, &xc2028_cfg);
  2107. }
  2108. /* configure tuner */
  2109. f.tuner = 0;
  2110. f.type = V4L2_TUNER_ANALOG_TV;
  2111. f.frequency = 9076; /* just a magic number */
  2112. v4l2->frequency = f.frequency;
  2113. v4l2_device_call_all(v4l2_dev, 0, tuner, s_frequency, &f);
  2114. }
  2115. static int em28xx_v4l2_init(struct em28xx *dev)
  2116. {
  2117. u8 val;
  2118. int ret;
  2119. unsigned int maxw;
  2120. struct v4l2_ctrl_handler *hdl;
  2121. struct em28xx_v4l2 *v4l2;
  2122. if (dev->is_audio_only) {
  2123. /* Shouldn't initialize IR for this interface */
  2124. return 0;
  2125. }
  2126. if (!dev->has_video) {
  2127. /* This device does not support the v4l2 extension */
  2128. return 0;
  2129. }
  2130. dev_info(&dev->intf->dev, "Registering V4L2 extension\n");
  2131. mutex_lock(&dev->lock);
  2132. v4l2 = kzalloc(sizeof(*v4l2), GFP_KERNEL);
  2133. if (!v4l2) {
  2134. mutex_unlock(&dev->lock);
  2135. return -ENOMEM;
  2136. }
  2137. kref_init(&v4l2->ref);
  2138. v4l2->dev = dev;
  2139. dev->v4l2 = v4l2;
  2140. #ifdef CONFIG_MEDIA_CONTROLLER
  2141. v4l2->v4l2_dev.mdev = dev->media_dev;
  2142. #endif
  2143. ret = v4l2_device_register(&dev->intf->dev, &v4l2->v4l2_dev);
  2144. if (ret < 0) {
  2145. dev_err(&dev->intf->dev,
  2146. "Call to v4l2_device_register() failed!\n");
  2147. goto err;
  2148. }
  2149. hdl = &v4l2->ctrl_handler;
  2150. v4l2_ctrl_handler_init(hdl, 8);
  2151. v4l2->v4l2_dev.ctrl_handler = hdl;
  2152. if (dev->is_webcam)
  2153. v4l2->progressive = true;
  2154. /*
  2155. * Default format, used for tvp5150 or saa711x output formats
  2156. */
  2157. v4l2->vinmode = EM28XX_VINMODE_YUV422_CbYCrY;
  2158. v4l2->vinctl = EM28XX_VINCTRL_INTERLACED |
  2159. EM28XX_VINCTRL_CCIR656_ENABLE;
  2160. /* request some modules */
  2161. if (dev->has_msp34xx)
  2162. v4l2_i2c_new_subdev(&v4l2->v4l2_dev,
  2163. &dev->i2c_adap[dev->def_i2c_bus],
  2164. "msp3400", 0, msp3400_addrs);
  2165. if (dev->board.decoder == EM28XX_SAA711X)
  2166. v4l2_i2c_new_subdev(&v4l2->v4l2_dev,
  2167. &dev->i2c_adap[dev->def_i2c_bus],
  2168. "saa7115_auto", 0, saa711x_addrs);
  2169. if (dev->board.decoder == EM28XX_TVP5150)
  2170. v4l2_i2c_new_subdev(&v4l2->v4l2_dev,
  2171. &dev->i2c_adap[dev->def_i2c_bus],
  2172. "tvp5150", 0, tvp5150_addrs);
  2173. if (dev->board.adecoder == EM28XX_TVAUDIO)
  2174. v4l2_i2c_new_subdev(&v4l2->v4l2_dev,
  2175. &dev->i2c_adap[dev->def_i2c_bus],
  2176. "tvaudio", dev->board.tvaudio_addr, NULL);
  2177. /* Initialize tuner and camera */
  2178. if (dev->board.tuner_type != TUNER_ABSENT) {
  2179. unsigned short tuner_addr = dev->board.tuner_addr;
  2180. int has_demod = (dev->board.tda9887_conf & TDA9887_PRESENT);
  2181. if (dev->board.radio.type)
  2182. v4l2_i2c_new_subdev(&v4l2->v4l2_dev,
  2183. &dev->i2c_adap[dev->def_i2c_bus],
  2184. "tuner", dev->board.radio_addr,
  2185. NULL);
  2186. if (has_demod)
  2187. v4l2_i2c_new_subdev(&v4l2->v4l2_dev,
  2188. &dev->i2c_adap[dev->def_i2c_bus],
  2189. "tuner", 0,
  2190. v4l2_i2c_tuner_addrs(ADDRS_DEMOD));
  2191. if (tuner_addr == 0) {
  2192. enum v4l2_i2c_tuner_type type =
  2193. has_demod ? ADDRS_TV_WITH_DEMOD : ADDRS_TV;
  2194. struct v4l2_subdev *sd;
  2195. sd = v4l2_i2c_new_subdev(&v4l2->v4l2_dev,
  2196. &dev->i2c_adap[dev->def_i2c_bus],
  2197. "tuner", 0,
  2198. v4l2_i2c_tuner_addrs(type));
  2199. if (sd)
  2200. tuner_addr = v4l2_i2c_subdev_addr(sd);
  2201. } else {
  2202. v4l2_i2c_new_subdev(&v4l2->v4l2_dev,
  2203. &dev->i2c_adap[dev->def_i2c_bus],
  2204. "tuner", tuner_addr, NULL);
  2205. }
  2206. em28xx_tuner_setup(dev, tuner_addr);
  2207. }
  2208. if (dev->em28xx_sensor != EM28XX_NOSENSOR)
  2209. em28xx_init_camera(dev);
  2210. /* Configure audio */
  2211. ret = em28xx_audio_setup(dev);
  2212. if (ret < 0) {
  2213. dev_err(&dev->intf->dev,
  2214. "%s: Error while setting audio - error [%d]!\n",
  2215. __func__, ret);
  2216. goto unregister_dev;
  2217. }
  2218. if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
  2219. v4l2_ctrl_new_std(hdl, &em28xx_ctrl_ops,
  2220. V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
  2221. v4l2_ctrl_new_std(hdl, &em28xx_ctrl_ops,
  2222. V4L2_CID_AUDIO_VOLUME, 0, 0x1f, 1, 0x1f);
  2223. } else {
  2224. /* install the em28xx notify callback */
  2225. v4l2_ctrl_notify(v4l2_ctrl_find(hdl, V4L2_CID_AUDIO_MUTE),
  2226. em28xx_ctrl_notify, dev);
  2227. v4l2_ctrl_notify(v4l2_ctrl_find(hdl, V4L2_CID_AUDIO_VOLUME),
  2228. em28xx_ctrl_notify, dev);
  2229. }
  2230. /* wake i2c devices */
  2231. em28xx_wake_i2c(dev);
  2232. /* init video dma queues */
  2233. INIT_LIST_HEAD(&dev->vidq.active);
  2234. INIT_LIST_HEAD(&dev->vbiq.active);
  2235. if (dev->has_msp34xx) {
  2236. /* Send a reset to other chips via gpio */
  2237. ret = em28xx_write_reg(dev, EM2820_R08_GPIO_CTRL, 0xf7);
  2238. if (ret < 0) {
  2239. dev_err(&dev->intf->dev,
  2240. "%s: em28xx_write_reg - msp34xx(1) failed! error [%d]\n",
  2241. __func__, ret);
  2242. goto unregister_dev;
  2243. }
  2244. usleep_range(10000, 11000);
  2245. ret = em28xx_write_reg(dev, EM2820_R08_GPIO_CTRL, 0xff);
  2246. if (ret < 0) {
  2247. dev_err(&dev->intf->dev,
  2248. "%s: em28xx_write_reg - msp34xx(2) failed! error [%d]\n",
  2249. __func__, ret);
  2250. goto unregister_dev;
  2251. }
  2252. usleep_range(10000, 11000);
  2253. }
  2254. /* set default norm */
  2255. v4l2->norm = V4L2_STD_PAL;
  2256. v4l2_device_call_all(&v4l2->v4l2_dev, 0, video, s_std, v4l2->norm);
  2257. v4l2->interlaced_fieldmode = EM28XX_INTERLACED_DEFAULT;
  2258. /* Analog specific initialization */
  2259. v4l2->format = &format[0];
  2260. maxw = norm_maxw(dev);
  2261. /*
  2262. * MaxPacketSize for em2800 is too small to capture at full resolution
  2263. * use half of maxw as the scaler can only scale to 50%
  2264. */
  2265. if (dev->board.is_em2800)
  2266. maxw /= 2;
  2267. em28xx_set_video_format(dev, format[0].fourcc,
  2268. maxw, norm_maxh(dev));
  2269. video_mux(dev, 0);
  2270. /* Audio defaults */
  2271. dev->mute = 1;
  2272. dev->volume = 0x1f;
  2273. /* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
  2274. val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
  2275. em28xx_write_reg(dev, EM28XX_R0F_XCLK,
  2276. (EM28XX_XCLK_AUDIO_UNMUTE | val));
  2277. em28xx_set_outfmt(dev);
  2278. /* Add image controls */
  2279. /*
  2280. * NOTE: at this point, the subdevices are already registered, so
  2281. * bridge controls are only added/enabled when no subdevice provides
  2282. * them
  2283. */
  2284. if (!v4l2_ctrl_find(hdl, V4L2_CID_CONTRAST))
  2285. v4l2_ctrl_new_std(hdl, &em28xx_ctrl_ops,
  2286. V4L2_CID_CONTRAST,
  2287. 0, 0x1f, 1, CONTRAST_DEFAULT);
  2288. if (!v4l2_ctrl_find(hdl, V4L2_CID_BRIGHTNESS))
  2289. v4l2_ctrl_new_std(hdl, &em28xx_ctrl_ops,
  2290. V4L2_CID_BRIGHTNESS,
  2291. -0x80, 0x7f, 1, BRIGHTNESS_DEFAULT);
  2292. if (!v4l2_ctrl_find(hdl, V4L2_CID_SATURATION))
  2293. v4l2_ctrl_new_std(hdl, &em28xx_ctrl_ops,
  2294. V4L2_CID_SATURATION,
  2295. 0, 0x1f, 1, SATURATION_DEFAULT);
  2296. if (!v4l2_ctrl_find(hdl, V4L2_CID_BLUE_BALANCE))
  2297. v4l2_ctrl_new_std(hdl, &em28xx_ctrl_ops,
  2298. V4L2_CID_BLUE_BALANCE,
  2299. -0x30, 0x30, 1, BLUE_BALANCE_DEFAULT);
  2300. if (!v4l2_ctrl_find(hdl, V4L2_CID_RED_BALANCE))
  2301. v4l2_ctrl_new_std(hdl, &em28xx_ctrl_ops,
  2302. V4L2_CID_RED_BALANCE,
  2303. -0x30, 0x30, 1, RED_BALANCE_DEFAULT);
  2304. if (!v4l2_ctrl_find(hdl, V4L2_CID_SHARPNESS))
  2305. v4l2_ctrl_new_std(hdl, &em28xx_ctrl_ops,
  2306. V4L2_CID_SHARPNESS,
  2307. 0, 0x0f, 1, SHARPNESS_DEFAULT);
  2308. /* Reset image controls */
  2309. em28xx_colorlevels_set_default(dev);
  2310. v4l2_ctrl_handler_setup(hdl);
  2311. ret = hdl->error;
  2312. if (ret)
  2313. goto unregister_dev;
  2314. /* allocate and fill video video_device struct */
  2315. em28xx_vdev_init(dev, &v4l2->vdev, &em28xx_video_template, "video");
  2316. mutex_init(&v4l2->vb_queue_lock);
  2317. mutex_init(&v4l2->vb_vbi_queue_lock);
  2318. v4l2->vdev.queue = &v4l2->vb_vidq;
  2319. v4l2->vdev.queue->lock = &v4l2->vb_queue_lock;
  2320. /* disable inapplicable ioctls */
  2321. if (dev->is_webcam) {
  2322. v4l2_disable_ioctl(&v4l2->vdev, VIDIOC_QUERYSTD);
  2323. v4l2_disable_ioctl(&v4l2->vdev, VIDIOC_G_STD);
  2324. v4l2_disable_ioctl(&v4l2->vdev, VIDIOC_S_STD);
  2325. } else {
  2326. v4l2_disable_ioctl(&v4l2->vdev, VIDIOC_S_PARM);
  2327. }
  2328. if (dev->tuner_type == TUNER_ABSENT) {
  2329. v4l2_disable_ioctl(&v4l2->vdev, VIDIOC_G_TUNER);
  2330. v4l2_disable_ioctl(&v4l2->vdev, VIDIOC_S_TUNER);
  2331. v4l2_disable_ioctl(&v4l2->vdev, VIDIOC_G_FREQUENCY);
  2332. v4l2_disable_ioctl(&v4l2->vdev, VIDIOC_S_FREQUENCY);
  2333. }
  2334. if (dev->int_audio_type == EM28XX_INT_AUDIO_NONE) {
  2335. v4l2_disable_ioctl(&v4l2->vdev, VIDIOC_G_AUDIO);
  2336. v4l2_disable_ioctl(&v4l2->vdev, VIDIOC_S_AUDIO);
  2337. }
  2338. /* register v4l2 video video_device */
  2339. ret = video_register_device(&v4l2->vdev, VFL_TYPE_GRABBER,
  2340. video_nr[dev->devno]);
  2341. if (ret) {
  2342. dev_err(&dev->intf->dev,
  2343. "unable to register video device (error=%i).\n", ret);
  2344. goto unregister_dev;
  2345. }
  2346. /* Allocate and fill vbi video_device struct */
  2347. if (em28xx_vbi_supported(dev) == 1) {
  2348. em28xx_vdev_init(dev, &v4l2->vbi_dev, &em28xx_video_template,
  2349. "vbi");
  2350. v4l2->vbi_dev.queue = &v4l2->vb_vbiq;
  2351. v4l2->vbi_dev.queue->lock = &v4l2->vb_vbi_queue_lock;
  2352. /* disable inapplicable ioctls */
  2353. v4l2_disable_ioctl(&v4l2->vbi_dev, VIDIOC_S_PARM);
  2354. if (dev->tuner_type == TUNER_ABSENT) {
  2355. v4l2_disable_ioctl(&v4l2->vbi_dev, VIDIOC_G_TUNER);
  2356. v4l2_disable_ioctl(&v4l2->vbi_dev, VIDIOC_S_TUNER);
  2357. v4l2_disable_ioctl(&v4l2->vbi_dev, VIDIOC_G_FREQUENCY);
  2358. v4l2_disable_ioctl(&v4l2->vbi_dev, VIDIOC_S_FREQUENCY);
  2359. }
  2360. if (dev->int_audio_type == EM28XX_INT_AUDIO_NONE) {
  2361. v4l2_disable_ioctl(&v4l2->vbi_dev, VIDIOC_G_AUDIO);
  2362. v4l2_disable_ioctl(&v4l2->vbi_dev, VIDIOC_S_AUDIO);
  2363. }
  2364. /* register v4l2 vbi video_device */
  2365. ret = video_register_device(&v4l2->vbi_dev, VFL_TYPE_VBI,
  2366. vbi_nr[dev->devno]);
  2367. if (ret < 0) {
  2368. dev_err(&dev->intf->dev,
  2369. "unable to register vbi device\n");
  2370. goto unregister_dev;
  2371. }
  2372. }
  2373. if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
  2374. em28xx_vdev_init(dev, &v4l2->radio_dev, &em28xx_radio_template,
  2375. "radio");
  2376. ret = video_register_device(&v4l2->radio_dev, VFL_TYPE_RADIO,
  2377. radio_nr[dev->devno]);
  2378. if (ret < 0) {
  2379. dev_err(&dev->intf->dev,
  2380. "can't register radio device\n");
  2381. goto unregister_dev;
  2382. }
  2383. dev_info(&dev->intf->dev,
  2384. "Registered radio device as %s\n",
  2385. video_device_node_name(&v4l2->radio_dev));
  2386. }
  2387. /* Init entities at the Media Controller */
  2388. em28xx_v4l2_create_entities(dev);
  2389. #ifdef CONFIG_MEDIA_CONTROLLER
  2390. ret = v4l2_mc_create_media_graph(dev->media_dev);
  2391. if (ret) {
  2392. dev_err(&dev->intf->dev,
  2393. "failed to create media graph\n");
  2394. em28xx_v4l2_media_release(dev);
  2395. goto unregister_dev;
  2396. }
  2397. #endif
  2398. dev_info(&dev->intf->dev,
  2399. "V4L2 video device registered as %s\n",
  2400. video_device_node_name(&v4l2->vdev));
  2401. if (video_is_registered(&v4l2->vbi_dev))
  2402. dev_info(&dev->intf->dev,
  2403. "V4L2 VBI device registered as %s\n",
  2404. video_device_node_name(&v4l2->vbi_dev));
  2405. /* Save some power by putting tuner to sleep */
  2406. v4l2_device_call_all(&v4l2->v4l2_dev, 0, tuner, standby);
  2407. /* initialize videobuf2 stuff */
  2408. em28xx_vb2_setup(dev);
  2409. dev_info(&dev->intf->dev,
  2410. "V4L2 extension successfully initialized\n");
  2411. kref_get(&dev->ref);
  2412. mutex_unlock(&dev->lock);
  2413. return 0;
  2414. unregister_dev:
  2415. if (video_is_registered(&v4l2->radio_dev)) {
  2416. dev_info(&dev->intf->dev,
  2417. "V4L2 device %s deregistered\n",
  2418. video_device_node_name(&v4l2->radio_dev));
  2419. video_unregister_device(&v4l2->radio_dev);
  2420. }
  2421. if (video_is_registered(&v4l2->vbi_dev)) {
  2422. dev_info(&dev->intf->dev,
  2423. "V4L2 device %s deregistered\n",
  2424. video_device_node_name(&v4l2->vbi_dev));
  2425. video_unregister_device(&v4l2->vbi_dev);
  2426. }
  2427. if (video_is_registered(&v4l2->vdev)) {
  2428. dev_info(&dev->intf->dev,
  2429. "V4L2 device %s deregistered\n",
  2430. video_device_node_name(&v4l2->vdev));
  2431. video_unregister_device(&v4l2->vdev);
  2432. }
  2433. v4l2_ctrl_handler_free(&v4l2->ctrl_handler);
  2434. v4l2_device_unregister(&v4l2->v4l2_dev);
  2435. err:
  2436. dev->v4l2 = NULL;
  2437. kref_put(&v4l2->ref, em28xx_free_v4l2);
  2438. mutex_unlock(&dev->lock);
  2439. return ret;
  2440. }
  2441. static struct em28xx_ops v4l2_ops = {
  2442. .id = EM28XX_V4L2,
  2443. .name = "Em28xx v4l2 Extension",
  2444. .init = em28xx_v4l2_init,
  2445. .fini = em28xx_v4l2_fini,
  2446. .suspend = em28xx_v4l2_suspend,
  2447. .resume = em28xx_v4l2_resume,
  2448. };
  2449. static int __init em28xx_video_register(void)
  2450. {
  2451. return em28xx_register_extension(&v4l2_ops);
  2452. }
  2453. static void __exit em28xx_video_unregister(void)
  2454. {
  2455. em28xx_unregister_extension(&v4l2_ops);
  2456. }
  2457. module_init(em28xx_video_register);
  2458. module_exit(em28xx_video_unregister);