em28xx-video.c 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875
  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. /*
  940. * Make sure streaming is not already in progress for this type
  941. * of filehandle (e.g. video, vbi)
  942. */
  943. rc = res_get(dev, vq->type);
  944. if (rc)
  945. return rc;
  946. if (v4l2->streaming_users == 0) {
  947. /* First active streaming user, so allocate all the URBs */
  948. /* Allocate the USB bandwidth */
  949. em28xx_set_alternate(dev);
  950. /*
  951. * Needed, since GPIO might have disabled power of
  952. * some i2c device
  953. */
  954. em28xx_wake_i2c(dev);
  955. v4l2->capture_type = -1;
  956. rc = em28xx_init_usb_xfer(dev, EM28XX_ANALOG_MODE,
  957. dev->analog_xfer_bulk,
  958. EM28XX_NUM_BUFS,
  959. dev->max_pkt_size,
  960. dev->packet_multiplier,
  961. em28xx_urb_data_copy);
  962. if (rc < 0)
  963. return rc;
  964. /*
  965. * djh: it's not clear whether this code is still needed. I'm
  966. * leaving it in here for now entirely out of concern for
  967. * backward compatibility (the old code did it)
  968. */
  969. /* Ask tuner to go to analog or radio mode */
  970. memset(&f, 0, sizeof(f));
  971. f.frequency = v4l2->frequency;
  972. owner = (struct v4l2_fh *)vq->owner;
  973. if (owner && owner->vdev->vfl_type == VFL_TYPE_RADIO)
  974. f.type = V4L2_TUNER_RADIO;
  975. else
  976. f.type = V4L2_TUNER_ANALOG_TV;
  977. v4l2_device_call_all(&v4l2->v4l2_dev,
  978. 0, tuner, s_frequency, &f);
  979. /* Enable video stream at TV decoder */
  980. v4l2_device_call_all(&v4l2->v4l2_dev, 0, video, s_stream, 1);
  981. }
  982. v4l2->streaming_users++;
  983. return rc;
  984. }
  985. static void em28xx_stop_streaming(struct vb2_queue *vq)
  986. {
  987. struct em28xx *dev = vb2_get_drv_priv(vq);
  988. struct em28xx_v4l2 *v4l2 = dev->v4l2;
  989. struct em28xx_dmaqueue *vidq = &dev->vidq;
  990. unsigned long flags = 0;
  991. em28xx_videodbg("%s\n", __func__);
  992. res_free(dev, vq->type);
  993. if (v4l2->streaming_users-- == 1) {
  994. /* Disable video stream at TV decoder */
  995. v4l2_device_call_all(&v4l2->v4l2_dev, 0, video, s_stream, 0);
  996. /* Last active user, so shutdown all the URBS */
  997. em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
  998. }
  999. spin_lock_irqsave(&dev->slock, flags);
  1000. if (dev->usb_ctl.vid_buf) {
  1001. vb2_buffer_done(&dev->usb_ctl.vid_buf->vb.vb2_buf,
  1002. VB2_BUF_STATE_ERROR);
  1003. dev->usb_ctl.vid_buf = NULL;
  1004. }
  1005. while (!list_empty(&vidq->active)) {
  1006. struct em28xx_buffer *buf;
  1007. buf = list_entry(vidq->active.next, struct em28xx_buffer, list);
  1008. list_del(&buf->list);
  1009. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  1010. }
  1011. spin_unlock_irqrestore(&dev->slock, flags);
  1012. }
  1013. void em28xx_stop_vbi_streaming(struct vb2_queue *vq)
  1014. {
  1015. struct em28xx *dev = vb2_get_drv_priv(vq);
  1016. struct em28xx_v4l2 *v4l2 = dev->v4l2;
  1017. struct em28xx_dmaqueue *vbiq = &dev->vbiq;
  1018. unsigned long flags = 0;
  1019. em28xx_videodbg("%s\n", __func__);
  1020. res_free(dev, vq->type);
  1021. if (v4l2->streaming_users-- == 1) {
  1022. /* Disable video stream at TV decoder */
  1023. v4l2_device_call_all(&v4l2->v4l2_dev, 0, video, s_stream, 0);
  1024. /* Last active user, so shutdown all the URBS */
  1025. em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
  1026. }
  1027. spin_lock_irqsave(&dev->slock, flags);
  1028. if (dev->usb_ctl.vbi_buf) {
  1029. vb2_buffer_done(&dev->usb_ctl.vbi_buf->vb.vb2_buf,
  1030. VB2_BUF_STATE_ERROR);
  1031. dev->usb_ctl.vbi_buf = NULL;
  1032. }
  1033. while (!list_empty(&vbiq->active)) {
  1034. struct em28xx_buffer *buf;
  1035. buf = list_entry(vbiq->active.next, struct em28xx_buffer, list);
  1036. list_del(&buf->list);
  1037. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  1038. }
  1039. spin_unlock_irqrestore(&dev->slock, flags);
  1040. }
  1041. static void
  1042. buffer_queue(struct vb2_buffer *vb)
  1043. {
  1044. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  1045. struct em28xx *dev = vb2_get_drv_priv(vb->vb2_queue);
  1046. struct em28xx_buffer *buf =
  1047. container_of(vbuf, struct em28xx_buffer, vb);
  1048. struct em28xx_dmaqueue *vidq = &dev->vidq;
  1049. unsigned long flags = 0;
  1050. em28xx_videodbg("%s\n", __func__);
  1051. buf->mem = vb2_plane_vaddr(vb, 0);
  1052. buf->length = vb2_plane_size(vb, 0);
  1053. spin_lock_irqsave(&dev->slock, flags);
  1054. list_add_tail(&buf->list, &vidq->active);
  1055. spin_unlock_irqrestore(&dev->slock, flags);
  1056. }
  1057. static const struct vb2_ops em28xx_video_qops = {
  1058. .queue_setup = queue_setup,
  1059. .buf_prepare = buffer_prepare,
  1060. .buf_queue = buffer_queue,
  1061. .start_streaming = em28xx_start_analog_streaming,
  1062. .stop_streaming = em28xx_stop_streaming,
  1063. .wait_prepare = vb2_ops_wait_prepare,
  1064. .wait_finish = vb2_ops_wait_finish,
  1065. };
  1066. static int em28xx_vb2_setup(struct em28xx *dev)
  1067. {
  1068. int rc;
  1069. struct vb2_queue *q;
  1070. struct em28xx_v4l2 *v4l2 = dev->v4l2;
  1071. /* Setup Videobuf2 for Video capture */
  1072. q = &v4l2->vb_vidq;
  1073. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1074. q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
  1075. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1076. q->drv_priv = dev;
  1077. q->buf_struct_size = sizeof(struct em28xx_buffer);
  1078. q->ops = &em28xx_video_qops;
  1079. q->mem_ops = &vb2_vmalloc_memops;
  1080. rc = vb2_queue_init(q);
  1081. if (rc < 0)
  1082. return rc;
  1083. /* Setup Videobuf2 for VBI capture */
  1084. q = &v4l2->vb_vbiq;
  1085. q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
  1086. q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR;
  1087. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1088. q->drv_priv = dev;
  1089. q->buf_struct_size = sizeof(struct em28xx_buffer);
  1090. q->ops = &em28xx_vbi_qops;
  1091. q->mem_ops = &vb2_vmalloc_memops;
  1092. rc = vb2_queue_init(q);
  1093. if (rc < 0)
  1094. return rc;
  1095. return 0;
  1096. }
  1097. /*
  1098. * v4l2 interface
  1099. */
  1100. static void video_mux(struct em28xx *dev, int index)
  1101. {
  1102. struct v4l2_device *v4l2_dev = &dev->v4l2->v4l2_dev;
  1103. dev->ctl_input = index;
  1104. dev->ctl_ainput = INPUT(index)->amux;
  1105. dev->ctl_aoutput = INPUT(index)->aout;
  1106. if (!dev->ctl_aoutput)
  1107. dev->ctl_aoutput = EM28XX_AOUT_MASTER;
  1108. v4l2_device_call_all(v4l2_dev, 0, video, s_routing,
  1109. INPUT(index)->vmux, 0, 0);
  1110. if (dev->has_msp34xx) {
  1111. if (dev->i2s_speed) {
  1112. v4l2_device_call_all(v4l2_dev, 0, audio,
  1113. s_i2s_clock_freq, dev->i2s_speed);
  1114. }
  1115. /* Note: this is msp3400 specific */
  1116. v4l2_device_call_all(v4l2_dev, 0, audio, s_routing,
  1117. dev->ctl_ainput,
  1118. MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
  1119. }
  1120. if (dev->board.adecoder != EM28XX_NOADECODER) {
  1121. v4l2_device_call_all(v4l2_dev, 0, audio, s_routing,
  1122. dev->ctl_ainput, dev->ctl_aoutput, 0);
  1123. }
  1124. em28xx_audio_analog_set(dev);
  1125. }
  1126. static void em28xx_ctrl_notify(struct v4l2_ctrl *ctrl, void *priv)
  1127. {
  1128. struct em28xx *dev = priv;
  1129. /*
  1130. * In the case of non-AC97 volume controls, we still need
  1131. * to do some setups at em28xx, in order to mute/unmute
  1132. * and to adjust audio volume. However, the value ranges
  1133. * should be checked by the corresponding V4L subdriver.
  1134. */
  1135. switch (ctrl->id) {
  1136. case V4L2_CID_AUDIO_MUTE:
  1137. dev->mute = ctrl->val;
  1138. em28xx_audio_analog_set(dev);
  1139. break;
  1140. case V4L2_CID_AUDIO_VOLUME:
  1141. dev->volume = ctrl->val;
  1142. em28xx_audio_analog_set(dev);
  1143. break;
  1144. }
  1145. }
  1146. static int em28xx_s_ctrl(struct v4l2_ctrl *ctrl)
  1147. {
  1148. struct em28xx_v4l2 *v4l2 =
  1149. container_of(ctrl->handler, struct em28xx_v4l2, ctrl_handler);
  1150. struct em28xx *dev = v4l2->dev;
  1151. int ret = -EINVAL;
  1152. switch (ctrl->id) {
  1153. case V4L2_CID_AUDIO_MUTE:
  1154. dev->mute = ctrl->val;
  1155. ret = em28xx_audio_analog_set(dev);
  1156. break;
  1157. case V4L2_CID_AUDIO_VOLUME:
  1158. dev->volume = ctrl->val;
  1159. ret = em28xx_audio_analog_set(dev);
  1160. break;
  1161. case V4L2_CID_CONTRAST:
  1162. ret = em28xx_write_reg(dev, EM28XX_R20_YGAIN, ctrl->val);
  1163. break;
  1164. case V4L2_CID_BRIGHTNESS:
  1165. ret = em28xx_write_reg(dev, EM28XX_R21_YOFFSET, ctrl->val);
  1166. break;
  1167. case V4L2_CID_SATURATION:
  1168. ret = em28xx_write_reg(dev, EM28XX_R22_UVGAIN, ctrl->val);
  1169. break;
  1170. case V4L2_CID_BLUE_BALANCE:
  1171. ret = em28xx_write_reg(dev, EM28XX_R23_UOFFSET, ctrl->val);
  1172. break;
  1173. case V4L2_CID_RED_BALANCE:
  1174. ret = em28xx_write_reg(dev, EM28XX_R24_VOFFSET, ctrl->val);
  1175. break;
  1176. case V4L2_CID_SHARPNESS:
  1177. ret = em28xx_write_reg(dev, EM28XX_R25_SHARPNESS, ctrl->val);
  1178. break;
  1179. }
  1180. return (ret < 0) ? ret : 0;
  1181. }
  1182. static const struct v4l2_ctrl_ops em28xx_ctrl_ops = {
  1183. .s_ctrl = em28xx_s_ctrl,
  1184. };
  1185. static void size_to_scale(struct em28xx *dev,
  1186. unsigned int width, unsigned int height,
  1187. unsigned int *hscale, unsigned int *vscale)
  1188. {
  1189. unsigned int maxw = norm_maxw(dev);
  1190. unsigned int maxh = norm_maxh(dev);
  1191. *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
  1192. if (*hscale > EM28XX_HVSCALE_MAX)
  1193. *hscale = EM28XX_HVSCALE_MAX;
  1194. *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
  1195. if (*vscale > EM28XX_HVSCALE_MAX)
  1196. *vscale = EM28XX_HVSCALE_MAX;
  1197. }
  1198. static void scale_to_size(struct em28xx *dev,
  1199. unsigned int hscale, unsigned int vscale,
  1200. unsigned int *width, unsigned int *height)
  1201. {
  1202. unsigned int maxw = norm_maxw(dev);
  1203. unsigned int maxh = norm_maxh(dev);
  1204. *width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
  1205. *height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
  1206. /* Don't let width or height to be zero */
  1207. if (*width < 1)
  1208. *width = 1;
  1209. if (*height < 1)
  1210. *height = 1;
  1211. }
  1212. /*
  1213. * IOCTL vidioc handling
  1214. */
  1215. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  1216. struct v4l2_format *f)
  1217. {
  1218. struct em28xx *dev = video_drvdata(file);
  1219. struct em28xx_v4l2 *v4l2 = dev->v4l2;
  1220. f->fmt.pix.width = v4l2->width;
  1221. f->fmt.pix.height = v4l2->height;
  1222. f->fmt.pix.pixelformat = v4l2->format->fourcc;
  1223. f->fmt.pix.bytesperline = (v4l2->width * v4l2->format->depth + 7) >> 3;
  1224. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * v4l2->height;
  1225. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  1226. /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
  1227. if (v4l2->progressive)
  1228. f->fmt.pix.field = V4L2_FIELD_NONE;
  1229. else
  1230. f->fmt.pix.field = v4l2->interlaced_fieldmode ?
  1231. V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
  1232. return 0;
  1233. }
  1234. static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
  1235. {
  1236. unsigned int i;
  1237. for (i = 0; i < ARRAY_SIZE(format); i++)
  1238. if (format[i].fourcc == fourcc)
  1239. return &format[i];
  1240. return NULL;
  1241. }
  1242. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  1243. struct v4l2_format *f)
  1244. {
  1245. struct em28xx *dev = video_drvdata(file);
  1246. struct em28xx_v4l2 *v4l2 = dev->v4l2;
  1247. unsigned int width = f->fmt.pix.width;
  1248. unsigned int height = f->fmt.pix.height;
  1249. unsigned int maxw = norm_maxw(dev);
  1250. unsigned int maxh = norm_maxh(dev);
  1251. unsigned int hscale, vscale;
  1252. struct em28xx_fmt *fmt;
  1253. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  1254. if (!fmt) {
  1255. em28xx_videodbg("Fourcc format (%08x) invalid.\n",
  1256. f->fmt.pix.pixelformat);
  1257. return -EINVAL;
  1258. }
  1259. if (dev->board.is_em2800) {
  1260. /* the em2800 can only scale down to 50% */
  1261. height = height > (3 * maxh / 4) ? maxh : maxh / 2;
  1262. width = width > (3 * maxw / 4) ? maxw : maxw / 2;
  1263. /*
  1264. * MaxPacketSize for em2800 is too small to capture at full
  1265. * resolution use half of maxw as the scaler can only scale
  1266. * to 50%
  1267. */
  1268. if (width == maxw && height == maxh)
  1269. width /= 2;
  1270. } else {
  1271. /*
  1272. * width must even because of the YUYV format
  1273. * height must be even because of interlacing
  1274. */
  1275. v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh,
  1276. 1, 0);
  1277. }
  1278. /* Avoid division by zero at size_to_scale */
  1279. if (width < 1)
  1280. width = 1;
  1281. if (height < 1)
  1282. height = 1;
  1283. size_to_scale(dev, width, height, &hscale, &vscale);
  1284. scale_to_size(dev, hscale, vscale, &width, &height);
  1285. f->fmt.pix.width = width;
  1286. f->fmt.pix.height = height;
  1287. f->fmt.pix.pixelformat = fmt->fourcc;
  1288. f->fmt.pix.bytesperline = (width * fmt->depth + 7) >> 3;
  1289. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
  1290. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  1291. if (v4l2->progressive)
  1292. f->fmt.pix.field = V4L2_FIELD_NONE;
  1293. else
  1294. f->fmt.pix.field = v4l2->interlaced_fieldmode ?
  1295. V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
  1296. f->fmt.pix.priv = 0;
  1297. return 0;
  1298. }
  1299. static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
  1300. unsigned int width, unsigned int height)
  1301. {
  1302. struct em28xx_fmt *fmt;
  1303. struct em28xx_v4l2 *v4l2 = dev->v4l2;
  1304. fmt = format_by_fourcc(fourcc);
  1305. if (!fmt)
  1306. return -EINVAL;
  1307. v4l2->format = fmt;
  1308. v4l2->width = width;
  1309. v4l2->height = height;
  1310. /* set new image size */
  1311. size_to_scale(dev, v4l2->width, v4l2->height,
  1312. &v4l2->hscale, &v4l2->vscale);
  1313. em28xx_resolution_set(dev);
  1314. return 0;
  1315. }
  1316. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  1317. struct v4l2_format *f)
  1318. {
  1319. struct em28xx *dev = video_drvdata(file);
  1320. struct em28xx_v4l2 *v4l2 = dev->v4l2;
  1321. if (vb2_is_busy(&v4l2->vb_vidq))
  1322. return -EBUSY;
  1323. vidioc_try_fmt_vid_cap(file, priv, f);
  1324. return em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
  1325. f->fmt.pix.width, f->fmt.pix.height);
  1326. }
  1327. static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
  1328. {
  1329. struct em28xx *dev = video_drvdata(file);
  1330. *norm = dev->v4l2->norm;
  1331. return 0;
  1332. }
  1333. static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *norm)
  1334. {
  1335. struct em28xx *dev = video_drvdata(file);
  1336. v4l2_device_call_all(&dev->v4l2->v4l2_dev, 0, video, querystd, norm);
  1337. return 0;
  1338. }
  1339. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
  1340. {
  1341. struct em28xx *dev = video_drvdata(file);
  1342. struct em28xx_v4l2 *v4l2 = dev->v4l2;
  1343. struct v4l2_format f;
  1344. if (norm == v4l2->norm)
  1345. return 0;
  1346. if (v4l2->streaming_users > 0)
  1347. return -EBUSY;
  1348. v4l2->norm = norm;
  1349. /* Adjusts width/height, if needed */
  1350. f.fmt.pix.width = 720;
  1351. f.fmt.pix.height = (norm & V4L2_STD_525_60) ? 480 : 576;
  1352. vidioc_try_fmt_vid_cap(file, priv, &f);
  1353. /* set new image size */
  1354. v4l2->width = f.fmt.pix.width;
  1355. v4l2->height = f.fmt.pix.height;
  1356. size_to_scale(dev, v4l2->width, v4l2->height,
  1357. &v4l2->hscale, &v4l2->vscale);
  1358. em28xx_resolution_set(dev);
  1359. v4l2_device_call_all(&v4l2->v4l2_dev, 0, video, s_std, v4l2->norm);
  1360. return 0;
  1361. }
  1362. static int vidioc_g_parm(struct file *file, void *priv,
  1363. struct v4l2_streamparm *p)
  1364. {
  1365. struct v4l2_subdev_frame_interval ival = { 0 };
  1366. struct em28xx *dev = video_drvdata(file);
  1367. struct em28xx_v4l2 *v4l2 = dev->v4l2;
  1368. int rc = 0;
  1369. if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  1370. p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  1371. return -EINVAL;
  1372. p->parm.capture.readbuffers = EM28XX_MIN_BUF;
  1373. p->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
  1374. if (dev->is_webcam) {
  1375. rc = v4l2_device_call_until_err(&v4l2->v4l2_dev, 0,
  1376. video, g_frame_interval, &ival);
  1377. if (!rc)
  1378. p->parm.capture.timeperframe = ival.interval;
  1379. } else {
  1380. v4l2_video_std_frame_period(v4l2->norm,
  1381. &p->parm.capture.timeperframe);
  1382. }
  1383. return rc;
  1384. }
  1385. static int vidioc_s_parm(struct file *file, void *priv,
  1386. struct v4l2_streamparm *p)
  1387. {
  1388. struct em28xx *dev = video_drvdata(file);
  1389. struct v4l2_subdev_frame_interval ival = {
  1390. 0,
  1391. p->parm.capture.timeperframe
  1392. };
  1393. int rc = 0;
  1394. if (!dev->is_webcam)
  1395. return -ENOTTY;
  1396. if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  1397. p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
  1398. return -EINVAL;
  1399. memset(&p->parm, 0, sizeof(p->parm));
  1400. p->parm.capture.readbuffers = EM28XX_MIN_BUF;
  1401. p->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
  1402. rc = v4l2_device_call_until_err(&dev->v4l2->v4l2_dev, 0,
  1403. video, s_frame_interval, &ival);
  1404. if (!rc)
  1405. p->parm.capture.timeperframe = ival.interval;
  1406. return rc;
  1407. }
  1408. static int vidioc_enum_input(struct file *file, void *priv,
  1409. struct v4l2_input *i)
  1410. {
  1411. struct em28xx *dev = video_drvdata(file);
  1412. unsigned int n;
  1413. n = i->index;
  1414. if (n >= MAX_EM28XX_INPUT)
  1415. return -EINVAL;
  1416. if (!INPUT(n)->type)
  1417. return -EINVAL;
  1418. i->type = V4L2_INPUT_TYPE_CAMERA;
  1419. strcpy(i->name, iname[INPUT(n)->type]);
  1420. if (INPUT(n)->type == EM28XX_VMUX_TELEVISION)
  1421. i->type = V4L2_INPUT_TYPE_TUNER;
  1422. i->std = dev->v4l2->vdev.tvnorms;
  1423. /* webcams do not have the STD API */
  1424. if (dev->is_webcam)
  1425. i->capabilities = 0;
  1426. return 0;
  1427. }
  1428. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  1429. {
  1430. struct em28xx *dev = video_drvdata(file);
  1431. *i = dev->ctl_input;
  1432. return 0;
  1433. }
  1434. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  1435. {
  1436. struct em28xx *dev = video_drvdata(file);
  1437. if (i >= MAX_EM28XX_INPUT)
  1438. return -EINVAL;
  1439. if (!INPUT(i)->type)
  1440. return -EINVAL;
  1441. video_mux(dev, i);
  1442. return 0;
  1443. }
  1444. static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  1445. {
  1446. struct em28xx *dev = video_drvdata(file);
  1447. switch (a->index) {
  1448. case EM28XX_AMUX_VIDEO:
  1449. strcpy(a->name, "Television");
  1450. break;
  1451. case EM28XX_AMUX_LINE_IN:
  1452. strcpy(a->name, "Line In");
  1453. break;
  1454. case EM28XX_AMUX_VIDEO2:
  1455. strcpy(a->name, "Television alt");
  1456. break;
  1457. case EM28XX_AMUX_PHONE:
  1458. strcpy(a->name, "Phone");
  1459. break;
  1460. case EM28XX_AMUX_MIC:
  1461. strcpy(a->name, "Mic");
  1462. break;
  1463. case EM28XX_AMUX_CD:
  1464. strcpy(a->name, "CD");
  1465. break;
  1466. case EM28XX_AMUX_AUX:
  1467. strcpy(a->name, "Aux");
  1468. break;
  1469. case EM28XX_AMUX_PCM_OUT:
  1470. strcpy(a->name, "PCM");
  1471. break;
  1472. default:
  1473. return -EINVAL;
  1474. }
  1475. a->index = dev->ctl_ainput;
  1476. a->capability = V4L2_AUDCAP_STEREO;
  1477. return 0;
  1478. }
  1479. static int vidioc_s_audio(struct file *file, void *priv,
  1480. const struct v4l2_audio *a)
  1481. {
  1482. struct em28xx *dev = video_drvdata(file);
  1483. if (a->index >= MAX_EM28XX_INPUT)
  1484. return -EINVAL;
  1485. if (!INPUT(a->index)->type)
  1486. return -EINVAL;
  1487. dev->ctl_ainput = INPUT(a->index)->amux;
  1488. dev->ctl_aoutput = INPUT(a->index)->aout;
  1489. if (!dev->ctl_aoutput)
  1490. dev->ctl_aoutput = EM28XX_AOUT_MASTER;
  1491. return 0;
  1492. }
  1493. static int vidioc_g_tuner(struct file *file, void *priv,
  1494. struct v4l2_tuner *t)
  1495. {
  1496. struct em28xx *dev = video_drvdata(file);
  1497. if (t->index != 0)
  1498. return -EINVAL;
  1499. strcpy(t->name, "Tuner");
  1500. v4l2_device_call_all(&dev->v4l2->v4l2_dev, 0, tuner, g_tuner, t);
  1501. return 0;
  1502. }
  1503. static int vidioc_s_tuner(struct file *file, void *priv,
  1504. const struct v4l2_tuner *t)
  1505. {
  1506. struct em28xx *dev = video_drvdata(file);
  1507. if (t->index != 0)
  1508. return -EINVAL;
  1509. v4l2_device_call_all(&dev->v4l2->v4l2_dev, 0, tuner, s_tuner, t);
  1510. return 0;
  1511. }
  1512. static int vidioc_g_frequency(struct file *file, void *priv,
  1513. struct v4l2_frequency *f)
  1514. {
  1515. struct em28xx *dev = video_drvdata(file);
  1516. struct em28xx_v4l2 *v4l2 = dev->v4l2;
  1517. if (f->tuner != 0)
  1518. return -EINVAL;
  1519. f->frequency = v4l2->frequency;
  1520. return 0;
  1521. }
  1522. static int vidioc_s_frequency(struct file *file, void *priv,
  1523. const struct v4l2_frequency *f)
  1524. {
  1525. struct v4l2_frequency new_freq = *f;
  1526. struct em28xx *dev = video_drvdata(file);
  1527. struct em28xx_v4l2 *v4l2 = dev->v4l2;
  1528. if (f->tuner != 0)
  1529. return -EINVAL;
  1530. v4l2_device_call_all(&v4l2->v4l2_dev, 0, tuner, s_frequency, f);
  1531. v4l2_device_call_all(&v4l2->v4l2_dev, 0, tuner, g_frequency, &new_freq);
  1532. v4l2->frequency = new_freq.frequency;
  1533. return 0;
  1534. }
  1535. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1536. static int vidioc_g_chip_info(struct file *file, void *priv,
  1537. struct v4l2_dbg_chip_info *chip)
  1538. {
  1539. struct em28xx *dev = video_drvdata(file);
  1540. if (chip->match.addr > 1)
  1541. return -EINVAL;
  1542. if (chip->match.addr == 1)
  1543. strlcpy(chip->name, "ac97", sizeof(chip->name));
  1544. else
  1545. strlcpy(chip->name,
  1546. dev->v4l2->v4l2_dev.name, sizeof(chip->name));
  1547. return 0;
  1548. }
  1549. static int em28xx_reg_len(int reg)
  1550. {
  1551. switch (reg) {
  1552. case EM28XX_R40_AC97LSB:
  1553. case EM28XX_R30_HSCALELOW:
  1554. case EM28XX_R32_VSCALELOW:
  1555. return 2;
  1556. default:
  1557. return 1;
  1558. }
  1559. }
  1560. static int vidioc_g_register(struct file *file, void *priv,
  1561. struct v4l2_dbg_register *reg)
  1562. {
  1563. struct em28xx *dev = video_drvdata(file);
  1564. int ret;
  1565. if (reg->match.addr > 1)
  1566. return -EINVAL;
  1567. if (reg->match.addr) {
  1568. ret = em28xx_read_ac97(dev, reg->reg);
  1569. if (ret < 0)
  1570. return ret;
  1571. reg->val = ret;
  1572. reg->size = 1;
  1573. return 0;
  1574. }
  1575. /* Match host */
  1576. reg->size = em28xx_reg_len(reg->reg);
  1577. if (reg->size == 1) {
  1578. ret = em28xx_read_reg(dev, reg->reg);
  1579. if (ret < 0)
  1580. return ret;
  1581. reg->val = ret;
  1582. } else {
  1583. __le16 val = 0;
  1584. ret = dev->em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
  1585. reg->reg, (char *)&val, 2);
  1586. if (ret < 0)
  1587. return ret;
  1588. reg->val = le16_to_cpu(val);
  1589. }
  1590. return 0;
  1591. }
  1592. static int vidioc_s_register(struct file *file, void *priv,
  1593. const struct v4l2_dbg_register *reg)
  1594. {
  1595. struct em28xx *dev = video_drvdata(file);
  1596. __le16 buf;
  1597. if (reg->match.addr > 1)
  1598. return -EINVAL;
  1599. if (reg->match.addr)
  1600. return em28xx_write_ac97(dev, reg->reg, reg->val);
  1601. /* Match host */
  1602. buf = cpu_to_le16(reg->val);
  1603. return em28xx_write_regs(dev, reg->reg, (char *)&buf,
  1604. em28xx_reg_len(reg->reg));
  1605. }
  1606. #endif
  1607. static int vidioc_querycap(struct file *file, void *priv,
  1608. struct v4l2_capability *cap)
  1609. {
  1610. struct video_device *vdev = video_devdata(file);
  1611. struct em28xx *dev = video_drvdata(file);
  1612. struct em28xx_v4l2 *v4l2 = dev->v4l2;
  1613. struct usb_device *udev = interface_to_usbdev(dev->intf);
  1614. strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
  1615. strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
  1616. usb_make_path(udev, cap->bus_info, sizeof(cap->bus_info));
  1617. if (vdev->vfl_type == VFL_TYPE_GRABBER)
  1618. cap->device_caps = V4L2_CAP_READWRITE |
  1619. V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  1620. else if (vdev->vfl_type == VFL_TYPE_RADIO)
  1621. cap->device_caps = V4L2_CAP_RADIO;
  1622. else
  1623. cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_VBI_CAPTURE;
  1624. if (dev->int_audio_type != EM28XX_INT_AUDIO_NONE)
  1625. cap->device_caps |= V4L2_CAP_AUDIO;
  1626. if (dev->tuner_type != TUNER_ABSENT)
  1627. cap->device_caps |= V4L2_CAP_TUNER;
  1628. cap->capabilities = cap->device_caps |
  1629. V4L2_CAP_DEVICE_CAPS | V4L2_CAP_READWRITE |
  1630. V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  1631. if (video_is_registered(&v4l2->vbi_dev))
  1632. cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
  1633. if (video_is_registered(&v4l2->radio_dev))
  1634. cap->capabilities |= V4L2_CAP_RADIO;
  1635. return 0;
  1636. }
  1637. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  1638. struct v4l2_fmtdesc *f)
  1639. {
  1640. if (unlikely(f->index >= ARRAY_SIZE(format)))
  1641. return -EINVAL;
  1642. strlcpy(f->description, format[f->index].name, sizeof(f->description));
  1643. f->pixelformat = format[f->index].fourcc;
  1644. return 0;
  1645. }
  1646. static int vidioc_enum_framesizes(struct file *file, void *priv,
  1647. struct v4l2_frmsizeenum *fsize)
  1648. {
  1649. struct em28xx *dev = video_drvdata(file);
  1650. struct em28xx_fmt *fmt;
  1651. unsigned int maxw = norm_maxw(dev);
  1652. unsigned int maxh = norm_maxh(dev);
  1653. fmt = format_by_fourcc(fsize->pixel_format);
  1654. if (!fmt) {
  1655. em28xx_videodbg("Fourcc format (%08x) invalid.\n",
  1656. fsize->pixel_format);
  1657. return -EINVAL;
  1658. }
  1659. if (dev->board.is_em2800) {
  1660. if (fsize->index > 1)
  1661. return -EINVAL;
  1662. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  1663. fsize->discrete.width = maxw / (1 + fsize->index);
  1664. fsize->discrete.height = maxh / (1 + fsize->index);
  1665. return 0;
  1666. }
  1667. if (fsize->index != 0)
  1668. return -EINVAL;
  1669. /* Report a continuous range */
  1670. fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
  1671. scale_to_size(dev, EM28XX_HVSCALE_MAX, EM28XX_HVSCALE_MAX,
  1672. &fsize->stepwise.min_width, &fsize->stepwise.min_height);
  1673. if (fsize->stepwise.min_width < 48)
  1674. fsize->stepwise.min_width = 48;
  1675. if (fsize->stepwise.min_height < 38)
  1676. fsize->stepwise.min_height = 38;
  1677. fsize->stepwise.max_width = maxw;
  1678. fsize->stepwise.max_height = maxh;
  1679. fsize->stepwise.step_width = 1;
  1680. fsize->stepwise.step_height = 1;
  1681. return 0;
  1682. }
  1683. /* RAW VBI ioctls */
  1684. static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
  1685. struct v4l2_format *format)
  1686. {
  1687. struct em28xx *dev = video_drvdata(file);
  1688. struct em28xx_v4l2 *v4l2 = dev->v4l2;
  1689. format->fmt.vbi.samples_per_line = v4l2->vbi_width;
  1690. format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  1691. format->fmt.vbi.offset = 0;
  1692. format->fmt.vbi.flags = 0;
  1693. format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
  1694. format->fmt.vbi.count[0] = v4l2->vbi_height;
  1695. format->fmt.vbi.count[1] = v4l2->vbi_height;
  1696. memset(format->fmt.vbi.reserved, 0, sizeof(format->fmt.vbi.reserved));
  1697. /* Varies by video standard (NTSC, PAL, etc.) */
  1698. if (v4l2->norm & V4L2_STD_525_60) {
  1699. /* NTSC */
  1700. format->fmt.vbi.start[0] = 10;
  1701. format->fmt.vbi.start[1] = 273;
  1702. } else if (v4l2->norm & V4L2_STD_625_50) {
  1703. /* PAL */
  1704. format->fmt.vbi.start[0] = 6;
  1705. format->fmt.vbi.start[1] = 318;
  1706. }
  1707. return 0;
  1708. }
  1709. /*
  1710. * RADIO ESPECIFIC IOCTLS
  1711. */
  1712. static int radio_g_tuner(struct file *file, void *priv,
  1713. struct v4l2_tuner *t)
  1714. {
  1715. struct em28xx *dev = video_drvdata(file);
  1716. if (unlikely(t->index > 0))
  1717. return -EINVAL;
  1718. strcpy(t->name, "Radio");
  1719. v4l2_device_call_all(&dev->v4l2->v4l2_dev, 0, tuner, g_tuner, t);
  1720. return 0;
  1721. }
  1722. static int radio_s_tuner(struct file *file, void *priv,
  1723. const struct v4l2_tuner *t)
  1724. {
  1725. struct em28xx *dev = video_drvdata(file);
  1726. if (t->index != 0)
  1727. return -EINVAL;
  1728. v4l2_device_call_all(&dev->v4l2->v4l2_dev, 0, tuner, s_tuner, t);
  1729. return 0;
  1730. }
  1731. /*
  1732. * em28xx_free_v4l2() - Free struct em28xx_v4l2
  1733. *
  1734. * @ref: struct kref for struct em28xx_v4l2
  1735. *
  1736. * Called when all users of struct em28xx_v4l2 are gone
  1737. */
  1738. static void em28xx_free_v4l2(struct kref *ref)
  1739. {
  1740. struct em28xx_v4l2 *v4l2 = container_of(ref, struct em28xx_v4l2, ref);
  1741. v4l2->dev->v4l2 = NULL;
  1742. kfree(v4l2);
  1743. }
  1744. /*
  1745. * em28xx_v4l2_open()
  1746. * inits the device and starts isoc transfer
  1747. */
  1748. static int em28xx_v4l2_open(struct file *filp)
  1749. {
  1750. struct video_device *vdev = video_devdata(filp);
  1751. struct em28xx *dev = video_drvdata(filp);
  1752. struct em28xx_v4l2 *v4l2 = dev->v4l2;
  1753. enum v4l2_buf_type fh_type = 0;
  1754. int ret;
  1755. switch (vdev->vfl_type) {
  1756. case VFL_TYPE_GRABBER:
  1757. fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1758. break;
  1759. case VFL_TYPE_VBI:
  1760. fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
  1761. break;
  1762. case VFL_TYPE_RADIO:
  1763. break;
  1764. default:
  1765. return -EINVAL;
  1766. }
  1767. em28xx_videodbg("open dev=%s type=%s users=%d\n",
  1768. video_device_node_name(vdev), v4l2_type_names[fh_type],
  1769. v4l2->users);
  1770. if (mutex_lock_interruptible(&dev->lock))
  1771. return -ERESTARTSYS;
  1772. ret = v4l2_fh_open(filp);
  1773. if (ret) {
  1774. dev_err(&dev->intf->dev,
  1775. "%s: v4l2_fh_open() returned error %d\n",
  1776. __func__, ret);
  1777. mutex_unlock(&dev->lock);
  1778. return ret;
  1779. }
  1780. if (v4l2->users == 0) {
  1781. em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
  1782. if (vdev->vfl_type != VFL_TYPE_RADIO)
  1783. em28xx_resolution_set(dev);
  1784. /*
  1785. * Needed, since GPIO might have disabled power
  1786. * of some i2c devices
  1787. */
  1788. em28xx_wake_i2c(dev);
  1789. }
  1790. if (vdev->vfl_type == VFL_TYPE_RADIO) {
  1791. em28xx_videodbg("video_open: setting radio device\n");
  1792. v4l2_device_call_all(&v4l2->v4l2_dev, 0, tuner, s_radio);
  1793. }
  1794. kref_get(&dev->ref);
  1795. kref_get(&v4l2->ref);
  1796. v4l2->users++;
  1797. mutex_unlock(&dev->lock);
  1798. return 0;
  1799. }
  1800. /*
  1801. * em28xx_v4l2_fini()
  1802. * unregisters the v4l2,i2c and usb devices
  1803. * called when the device gets disconected or at module unload
  1804. */
  1805. static int em28xx_v4l2_fini(struct em28xx *dev)
  1806. {
  1807. struct em28xx_v4l2 *v4l2 = dev->v4l2;
  1808. if (dev->is_audio_only) {
  1809. /* Shouldn't initialize IR for this interface */
  1810. return 0;
  1811. }
  1812. if (!dev->has_video) {
  1813. /* This device does not support the v4l2 extension */
  1814. return 0;
  1815. }
  1816. if (!v4l2)
  1817. return 0;
  1818. dev_info(&dev->intf->dev, "Closing video extension\n");
  1819. mutex_lock(&dev->lock);
  1820. v4l2_device_disconnect(&v4l2->v4l2_dev);
  1821. em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
  1822. em28xx_v4l2_media_release(dev);
  1823. if (video_is_registered(&v4l2->radio_dev)) {
  1824. dev_info(&dev->intf->dev, "V4L2 device %s deregistered\n",
  1825. video_device_node_name(&v4l2->radio_dev));
  1826. video_unregister_device(&v4l2->radio_dev);
  1827. }
  1828. if (video_is_registered(&v4l2->vbi_dev)) {
  1829. dev_info(&dev->intf->dev, "V4L2 device %s deregistered\n",
  1830. video_device_node_name(&v4l2->vbi_dev));
  1831. video_unregister_device(&v4l2->vbi_dev);
  1832. }
  1833. if (video_is_registered(&v4l2->vdev)) {
  1834. dev_info(&dev->intf->dev, "V4L2 device %s deregistered\n",
  1835. video_device_node_name(&v4l2->vdev));
  1836. video_unregister_device(&v4l2->vdev);
  1837. }
  1838. v4l2_ctrl_handler_free(&v4l2->ctrl_handler);
  1839. v4l2_device_unregister(&v4l2->v4l2_dev);
  1840. kref_put(&v4l2->ref, em28xx_free_v4l2);
  1841. mutex_unlock(&dev->lock);
  1842. kref_put(&dev->ref, em28xx_free_device);
  1843. return 0;
  1844. }
  1845. static int em28xx_v4l2_suspend(struct em28xx *dev)
  1846. {
  1847. if (dev->is_audio_only)
  1848. return 0;
  1849. if (!dev->has_video)
  1850. return 0;
  1851. dev_info(&dev->intf->dev, "Suspending video extension\n");
  1852. em28xx_stop_urbs(dev);
  1853. return 0;
  1854. }
  1855. static int em28xx_v4l2_resume(struct em28xx *dev)
  1856. {
  1857. if (dev->is_audio_only)
  1858. return 0;
  1859. if (!dev->has_video)
  1860. return 0;
  1861. dev_info(&dev->intf->dev, "Resuming video extension\n");
  1862. /* what do we do here */
  1863. return 0;
  1864. }
  1865. /*
  1866. * em28xx_v4l2_close()
  1867. * stops streaming and deallocates all resources allocated by the v4l2
  1868. * calls and ioctls
  1869. */
  1870. static int em28xx_v4l2_close(struct file *filp)
  1871. {
  1872. struct em28xx *dev = video_drvdata(filp);
  1873. struct em28xx_v4l2 *v4l2 = dev->v4l2;
  1874. struct usb_device *udev = interface_to_usbdev(dev->intf);
  1875. int err;
  1876. em28xx_videodbg("users=%d\n", v4l2->users);
  1877. vb2_fop_release(filp);
  1878. mutex_lock(&dev->lock);
  1879. if (v4l2->users == 1) {
  1880. /* No sense to try to write to the device */
  1881. if (dev->disconnected)
  1882. goto exit;
  1883. /* Save some power by putting tuner to sleep */
  1884. v4l2_device_call_all(&v4l2->v4l2_dev, 0, tuner, standby);
  1885. /* do this before setting alternate! */
  1886. em28xx_set_mode(dev, EM28XX_SUSPEND);
  1887. /* set alternate 0 */
  1888. dev->alt = 0;
  1889. em28xx_videodbg("setting alternate 0\n");
  1890. err = usb_set_interface(udev, 0, 0);
  1891. if (err < 0) {
  1892. dev_err(&dev->intf->dev,
  1893. "cannot change alternate number to 0 (error=%i)\n",
  1894. err);
  1895. }
  1896. }
  1897. exit:
  1898. v4l2->users--;
  1899. kref_put(&v4l2->ref, em28xx_free_v4l2);
  1900. mutex_unlock(&dev->lock);
  1901. kref_put(&dev->ref, em28xx_free_device);
  1902. return 0;
  1903. }
  1904. static const struct v4l2_file_operations em28xx_v4l_fops = {
  1905. .owner = THIS_MODULE,
  1906. .open = em28xx_v4l2_open,
  1907. .release = em28xx_v4l2_close,
  1908. .read = vb2_fop_read,
  1909. .poll = vb2_fop_poll,
  1910. .mmap = vb2_fop_mmap,
  1911. .unlocked_ioctl = video_ioctl2,
  1912. };
  1913. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1914. .vidioc_querycap = vidioc_querycap,
  1915. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1916. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1917. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1918. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1919. .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
  1920. .vidioc_try_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
  1921. .vidioc_s_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
  1922. .vidioc_enum_framesizes = vidioc_enum_framesizes,
  1923. .vidioc_g_audio = vidioc_g_audio,
  1924. .vidioc_s_audio = vidioc_s_audio,
  1925. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  1926. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  1927. .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  1928. .vidioc_querybuf = vb2_ioctl_querybuf,
  1929. .vidioc_qbuf = vb2_ioctl_qbuf,
  1930. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  1931. .vidioc_g_std = vidioc_g_std,
  1932. .vidioc_querystd = vidioc_querystd,
  1933. .vidioc_s_std = vidioc_s_std,
  1934. .vidioc_g_parm = vidioc_g_parm,
  1935. .vidioc_s_parm = vidioc_s_parm,
  1936. .vidioc_enum_input = vidioc_enum_input,
  1937. .vidioc_g_input = vidioc_g_input,
  1938. .vidioc_s_input = vidioc_s_input,
  1939. .vidioc_streamon = vb2_ioctl_streamon,
  1940. .vidioc_streamoff = vb2_ioctl_streamoff,
  1941. .vidioc_g_tuner = vidioc_g_tuner,
  1942. .vidioc_s_tuner = vidioc_s_tuner,
  1943. .vidioc_g_frequency = vidioc_g_frequency,
  1944. .vidioc_s_frequency = vidioc_s_frequency,
  1945. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1946. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1947. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1948. .vidioc_g_chip_info = vidioc_g_chip_info,
  1949. .vidioc_g_register = vidioc_g_register,
  1950. .vidioc_s_register = vidioc_s_register,
  1951. #endif
  1952. };
  1953. static const struct video_device em28xx_video_template = {
  1954. .fops = &em28xx_v4l_fops,
  1955. .ioctl_ops = &video_ioctl_ops,
  1956. .release = video_device_release_empty,
  1957. .tvnorms = V4L2_STD_ALL,
  1958. };
  1959. static const struct v4l2_file_operations radio_fops = {
  1960. .owner = THIS_MODULE,
  1961. .open = em28xx_v4l2_open,
  1962. .release = em28xx_v4l2_close,
  1963. .unlocked_ioctl = video_ioctl2,
  1964. };
  1965. static const struct v4l2_ioctl_ops radio_ioctl_ops = {
  1966. .vidioc_querycap = vidioc_querycap,
  1967. .vidioc_g_tuner = radio_g_tuner,
  1968. .vidioc_s_tuner = radio_s_tuner,
  1969. .vidioc_g_frequency = vidioc_g_frequency,
  1970. .vidioc_s_frequency = vidioc_s_frequency,
  1971. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1972. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1973. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1974. .vidioc_g_chip_info = vidioc_g_chip_info,
  1975. .vidioc_g_register = vidioc_g_register,
  1976. .vidioc_s_register = vidioc_s_register,
  1977. #endif
  1978. };
  1979. static struct video_device em28xx_radio_template = {
  1980. .fops = &radio_fops,
  1981. .ioctl_ops = &radio_ioctl_ops,
  1982. .release = video_device_release_empty,
  1983. };
  1984. /* I2C possible address to saa7115, tvp5150, msp3400, tvaudio */
  1985. static unsigned short saa711x_addrs[] = {
  1986. 0x4a >> 1, 0x48 >> 1, /* SAA7111, SAA7111A and SAA7113 */
  1987. 0x42 >> 1, 0x40 >> 1, /* SAA7114, SAA7115 and SAA7118 */
  1988. I2C_CLIENT_END };
  1989. static unsigned short tvp5150_addrs[] = {
  1990. 0xb8 >> 1,
  1991. 0xba >> 1,
  1992. I2C_CLIENT_END
  1993. };
  1994. static unsigned short msp3400_addrs[] = {
  1995. 0x80 >> 1,
  1996. 0x88 >> 1,
  1997. I2C_CLIENT_END
  1998. };
  1999. /******************************** usb interface ******************************/
  2000. static void em28xx_vdev_init(struct em28xx *dev,
  2001. struct video_device *vfd,
  2002. const struct video_device *template,
  2003. const char *type_name)
  2004. {
  2005. *vfd = *template;
  2006. vfd->v4l2_dev = &dev->v4l2->v4l2_dev;
  2007. vfd->lock = &dev->lock;
  2008. if (dev->is_webcam)
  2009. vfd->tvnorms = 0;
  2010. snprintf(vfd->name, sizeof(vfd->name), "%s %s",
  2011. dev_name(&dev->intf->dev), type_name);
  2012. video_set_drvdata(vfd, dev);
  2013. }
  2014. static void em28xx_tuner_setup(struct em28xx *dev, unsigned short tuner_addr)
  2015. {
  2016. struct em28xx_v4l2 *v4l2 = dev->v4l2;
  2017. struct v4l2_device *v4l2_dev = &v4l2->v4l2_dev;
  2018. struct tuner_setup tun_setup;
  2019. struct v4l2_frequency f;
  2020. memset(&tun_setup, 0, sizeof(tun_setup));
  2021. tun_setup.mode_mask = T_ANALOG_TV | T_RADIO;
  2022. tun_setup.tuner_callback = em28xx_tuner_callback;
  2023. if (dev->board.radio.type) {
  2024. tun_setup.type = dev->board.radio.type;
  2025. tun_setup.addr = dev->board.radio_addr;
  2026. v4l2_device_call_all(v4l2_dev,
  2027. 0, tuner, s_type_addr, &tun_setup);
  2028. }
  2029. if (dev->tuner_type != TUNER_ABSENT && dev->tuner_type) {
  2030. tun_setup.type = dev->tuner_type;
  2031. tun_setup.addr = tuner_addr;
  2032. v4l2_device_call_all(v4l2_dev,
  2033. 0, tuner, s_type_addr, &tun_setup);
  2034. }
  2035. if (dev->board.tda9887_conf) {
  2036. struct v4l2_priv_tun_config tda9887_cfg;
  2037. tda9887_cfg.tuner = TUNER_TDA9887;
  2038. tda9887_cfg.priv = &dev->board.tda9887_conf;
  2039. v4l2_device_call_all(v4l2_dev,
  2040. 0, tuner, s_config, &tda9887_cfg);
  2041. }
  2042. if (dev->tuner_type == TUNER_XC2028) {
  2043. struct v4l2_priv_tun_config xc2028_cfg;
  2044. struct xc2028_ctrl ctl;
  2045. memset(&xc2028_cfg, 0, sizeof(xc2028_cfg));
  2046. memset(&ctl, 0, sizeof(ctl));
  2047. em28xx_setup_xc3028(dev, &ctl);
  2048. xc2028_cfg.tuner = TUNER_XC2028;
  2049. xc2028_cfg.priv = &ctl;
  2050. v4l2_device_call_all(v4l2_dev, 0, tuner, s_config, &xc2028_cfg);
  2051. }
  2052. /* configure tuner */
  2053. f.tuner = 0;
  2054. f.type = V4L2_TUNER_ANALOG_TV;
  2055. f.frequency = 9076; /* just a magic number */
  2056. v4l2->frequency = f.frequency;
  2057. v4l2_device_call_all(v4l2_dev, 0, tuner, s_frequency, &f);
  2058. }
  2059. static int em28xx_v4l2_init(struct em28xx *dev)
  2060. {
  2061. u8 val;
  2062. int ret;
  2063. unsigned int maxw;
  2064. struct v4l2_ctrl_handler *hdl;
  2065. struct em28xx_v4l2 *v4l2;
  2066. if (dev->is_audio_only) {
  2067. /* Shouldn't initialize IR for this interface */
  2068. return 0;
  2069. }
  2070. if (!dev->has_video) {
  2071. /* This device does not support the v4l2 extension */
  2072. return 0;
  2073. }
  2074. dev_info(&dev->intf->dev, "Registering V4L2 extension\n");
  2075. mutex_lock(&dev->lock);
  2076. v4l2 = kzalloc(sizeof(*v4l2), GFP_KERNEL);
  2077. if (!v4l2) {
  2078. mutex_unlock(&dev->lock);
  2079. return -ENOMEM;
  2080. }
  2081. kref_init(&v4l2->ref);
  2082. v4l2->dev = dev;
  2083. dev->v4l2 = v4l2;
  2084. #ifdef CONFIG_MEDIA_CONTROLLER
  2085. v4l2->v4l2_dev.mdev = dev->media_dev;
  2086. #endif
  2087. ret = v4l2_device_register(&dev->intf->dev, &v4l2->v4l2_dev);
  2088. if (ret < 0) {
  2089. dev_err(&dev->intf->dev,
  2090. "Call to v4l2_device_register() failed!\n");
  2091. goto err;
  2092. }
  2093. hdl = &v4l2->ctrl_handler;
  2094. v4l2_ctrl_handler_init(hdl, 8);
  2095. v4l2->v4l2_dev.ctrl_handler = hdl;
  2096. if (dev->is_webcam)
  2097. v4l2->progressive = true;
  2098. /*
  2099. * Default format, used for tvp5150 or saa711x output formats
  2100. */
  2101. v4l2->vinmode = EM28XX_VINMODE_YUV422_CbYCrY;
  2102. v4l2->vinctl = EM28XX_VINCTRL_INTERLACED |
  2103. EM28XX_VINCTRL_CCIR656_ENABLE;
  2104. /* request some modules */
  2105. if (dev->has_msp34xx)
  2106. v4l2_i2c_new_subdev(&v4l2->v4l2_dev,
  2107. &dev->i2c_adap[dev->def_i2c_bus],
  2108. "msp3400", 0, msp3400_addrs);
  2109. if (dev->board.decoder == EM28XX_SAA711X)
  2110. v4l2_i2c_new_subdev(&v4l2->v4l2_dev,
  2111. &dev->i2c_adap[dev->def_i2c_bus],
  2112. "saa7115_auto", 0, saa711x_addrs);
  2113. if (dev->board.decoder == EM28XX_TVP5150)
  2114. v4l2_i2c_new_subdev(&v4l2->v4l2_dev,
  2115. &dev->i2c_adap[dev->def_i2c_bus],
  2116. "tvp5150", 0, tvp5150_addrs);
  2117. if (dev->board.adecoder == EM28XX_TVAUDIO)
  2118. v4l2_i2c_new_subdev(&v4l2->v4l2_dev,
  2119. &dev->i2c_adap[dev->def_i2c_bus],
  2120. "tvaudio", dev->board.tvaudio_addr, NULL);
  2121. /* Initialize tuner and camera */
  2122. if (dev->board.tuner_type != TUNER_ABSENT) {
  2123. unsigned short tuner_addr = dev->board.tuner_addr;
  2124. int has_demod = (dev->board.tda9887_conf & TDA9887_PRESENT);
  2125. if (dev->board.radio.type)
  2126. v4l2_i2c_new_subdev(&v4l2->v4l2_dev,
  2127. &dev->i2c_adap[dev->def_i2c_bus],
  2128. "tuner", dev->board.radio_addr,
  2129. NULL);
  2130. if (has_demod)
  2131. v4l2_i2c_new_subdev(&v4l2->v4l2_dev,
  2132. &dev->i2c_adap[dev->def_i2c_bus],
  2133. "tuner", 0,
  2134. v4l2_i2c_tuner_addrs(ADDRS_DEMOD));
  2135. if (tuner_addr == 0) {
  2136. enum v4l2_i2c_tuner_type type =
  2137. has_demod ? ADDRS_TV_WITH_DEMOD : ADDRS_TV;
  2138. struct v4l2_subdev *sd;
  2139. sd = v4l2_i2c_new_subdev(&v4l2->v4l2_dev,
  2140. &dev->i2c_adap[dev->def_i2c_bus],
  2141. "tuner", 0,
  2142. v4l2_i2c_tuner_addrs(type));
  2143. if (sd)
  2144. tuner_addr = v4l2_i2c_subdev_addr(sd);
  2145. } else {
  2146. v4l2_i2c_new_subdev(&v4l2->v4l2_dev,
  2147. &dev->i2c_adap[dev->def_i2c_bus],
  2148. "tuner", tuner_addr, NULL);
  2149. }
  2150. em28xx_tuner_setup(dev, tuner_addr);
  2151. }
  2152. if (dev->em28xx_sensor != EM28XX_NOSENSOR)
  2153. em28xx_init_camera(dev);
  2154. /* Configure audio */
  2155. ret = em28xx_audio_setup(dev);
  2156. if (ret < 0) {
  2157. dev_err(&dev->intf->dev,
  2158. "%s: Error while setting audio - error [%d]!\n",
  2159. __func__, ret);
  2160. goto unregister_dev;
  2161. }
  2162. if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
  2163. v4l2_ctrl_new_std(hdl, &em28xx_ctrl_ops,
  2164. V4L2_CID_AUDIO_MUTE, 0, 1, 1, 1);
  2165. v4l2_ctrl_new_std(hdl, &em28xx_ctrl_ops,
  2166. V4L2_CID_AUDIO_VOLUME, 0, 0x1f, 1, 0x1f);
  2167. } else {
  2168. /* install the em28xx notify callback */
  2169. v4l2_ctrl_notify(v4l2_ctrl_find(hdl, V4L2_CID_AUDIO_MUTE),
  2170. em28xx_ctrl_notify, dev);
  2171. v4l2_ctrl_notify(v4l2_ctrl_find(hdl, V4L2_CID_AUDIO_VOLUME),
  2172. em28xx_ctrl_notify, dev);
  2173. }
  2174. /* wake i2c devices */
  2175. em28xx_wake_i2c(dev);
  2176. /* init video dma queues */
  2177. INIT_LIST_HEAD(&dev->vidq.active);
  2178. INIT_LIST_HEAD(&dev->vbiq.active);
  2179. if (dev->has_msp34xx) {
  2180. /* Send a reset to other chips via gpio */
  2181. ret = em28xx_write_reg(dev, EM2820_R08_GPIO_CTRL, 0xf7);
  2182. if (ret < 0) {
  2183. dev_err(&dev->intf->dev,
  2184. "%s: em28xx_write_reg - msp34xx(1) failed! error [%d]\n",
  2185. __func__, ret);
  2186. goto unregister_dev;
  2187. }
  2188. usleep_range(10000, 11000);
  2189. ret = em28xx_write_reg(dev, EM2820_R08_GPIO_CTRL, 0xff);
  2190. if (ret < 0) {
  2191. dev_err(&dev->intf->dev,
  2192. "%s: em28xx_write_reg - msp34xx(2) failed! error [%d]\n",
  2193. __func__, ret);
  2194. goto unregister_dev;
  2195. }
  2196. usleep_range(10000, 11000);
  2197. }
  2198. /* set default norm */
  2199. v4l2->norm = V4L2_STD_PAL;
  2200. v4l2_device_call_all(&v4l2->v4l2_dev, 0, video, s_std, v4l2->norm);
  2201. v4l2->interlaced_fieldmode = EM28XX_INTERLACED_DEFAULT;
  2202. /* Analog specific initialization */
  2203. v4l2->format = &format[0];
  2204. maxw = norm_maxw(dev);
  2205. /*
  2206. * MaxPacketSize for em2800 is too small to capture at full resolution
  2207. * use half of maxw as the scaler can only scale to 50%
  2208. */
  2209. if (dev->board.is_em2800)
  2210. maxw /= 2;
  2211. em28xx_set_video_format(dev, format[0].fourcc,
  2212. maxw, norm_maxh(dev));
  2213. video_mux(dev, 0);
  2214. /* Audio defaults */
  2215. dev->mute = 1;
  2216. dev->volume = 0x1f;
  2217. /* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
  2218. val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
  2219. em28xx_write_reg(dev, EM28XX_R0F_XCLK,
  2220. (EM28XX_XCLK_AUDIO_UNMUTE | val));
  2221. em28xx_set_outfmt(dev);
  2222. /* Add image controls */
  2223. /*
  2224. * NOTE: at this point, the subdevices are already registered, so
  2225. * bridge controls are only added/enabled when no subdevice provides
  2226. * them
  2227. */
  2228. if (!v4l2_ctrl_find(hdl, V4L2_CID_CONTRAST))
  2229. v4l2_ctrl_new_std(hdl, &em28xx_ctrl_ops,
  2230. V4L2_CID_CONTRAST,
  2231. 0, 0x1f, 1, CONTRAST_DEFAULT);
  2232. if (!v4l2_ctrl_find(hdl, V4L2_CID_BRIGHTNESS))
  2233. v4l2_ctrl_new_std(hdl, &em28xx_ctrl_ops,
  2234. V4L2_CID_BRIGHTNESS,
  2235. -0x80, 0x7f, 1, BRIGHTNESS_DEFAULT);
  2236. if (!v4l2_ctrl_find(hdl, V4L2_CID_SATURATION))
  2237. v4l2_ctrl_new_std(hdl, &em28xx_ctrl_ops,
  2238. V4L2_CID_SATURATION,
  2239. 0, 0x1f, 1, SATURATION_DEFAULT);
  2240. if (!v4l2_ctrl_find(hdl, V4L2_CID_BLUE_BALANCE))
  2241. v4l2_ctrl_new_std(hdl, &em28xx_ctrl_ops,
  2242. V4L2_CID_BLUE_BALANCE,
  2243. -0x30, 0x30, 1, BLUE_BALANCE_DEFAULT);
  2244. if (!v4l2_ctrl_find(hdl, V4L2_CID_RED_BALANCE))
  2245. v4l2_ctrl_new_std(hdl, &em28xx_ctrl_ops,
  2246. V4L2_CID_RED_BALANCE,
  2247. -0x30, 0x30, 1, RED_BALANCE_DEFAULT);
  2248. if (!v4l2_ctrl_find(hdl, V4L2_CID_SHARPNESS))
  2249. v4l2_ctrl_new_std(hdl, &em28xx_ctrl_ops,
  2250. V4L2_CID_SHARPNESS,
  2251. 0, 0x0f, 1, SHARPNESS_DEFAULT);
  2252. /* Reset image controls */
  2253. em28xx_colorlevels_set_default(dev);
  2254. v4l2_ctrl_handler_setup(hdl);
  2255. ret = hdl->error;
  2256. if (ret)
  2257. goto unregister_dev;
  2258. /* allocate and fill video video_device struct */
  2259. em28xx_vdev_init(dev, &v4l2->vdev, &em28xx_video_template, "video");
  2260. mutex_init(&v4l2->vb_queue_lock);
  2261. mutex_init(&v4l2->vb_vbi_queue_lock);
  2262. v4l2->vdev.queue = &v4l2->vb_vidq;
  2263. v4l2->vdev.queue->lock = &v4l2->vb_queue_lock;
  2264. /* disable inapplicable ioctls */
  2265. if (dev->is_webcam) {
  2266. v4l2_disable_ioctl(&v4l2->vdev, VIDIOC_QUERYSTD);
  2267. v4l2_disable_ioctl(&v4l2->vdev, VIDIOC_G_STD);
  2268. v4l2_disable_ioctl(&v4l2->vdev, VIDIOC_S_STD);
  2269. } else {
  2270. v4l2_disable_ioctl(&v4l2->vdev, VIDIOC_S_PARM);
  2271. }
  2272. if (dev->tuner_type == TUNER_ABSENT) {
  2273. v4l2_disable_ioctl(&v4l2->vdev, VIDIOC_G_TUNER);
  2274. v4l2_disable_ioctl(&v4l2->vdev, VIDIOC_S_TUNER);
  2275. v4l2_disable_ioctl(&v4l2->vdev, VIDIOC_G_FREQUENCY);
  2276. v4l2_disable_ioctl(&v4l2->vdev, VIDIOC_S_FREQUENCY);
  2277. }
  2278. if (dev->int_audio_type == EM28XX_INT_AUDIO_NONE) {
  2279. v4l2_disable_ioctl(&v4l2->vdev, VIDIOC_G_AUDIO);
  2280. v4l2_disable_ioctl(&v4l2->vdev, VIDIOC_S_AUDIO);
  2281. }
  2282. /* register v4l2 video video_device */
  2283. ret = video_register_device(&v4l2->vdev, VFL_TYPE_GRABBER,
  2284. video_nr[dev->devno]);
  2285. if (ret) {
  2286. dev_err(&dev->intf->dev,
  2287. "unable to register video device (error=%i).\n", ret);
  2288. goto unregister_dev;
  2289. }
  2290. /* Allocate and fill vbi video_device struct */
  2291. if (em28xx_vbi_supported(dev) == 1) {
  2292. em28xx_vdev_init(dev, &v4l2->vbi_dev, &em28xx_video_template,
  2293. "vbi");
  2294. v4l2->vbi_dev.queue = &v4l2->vb_vbiq;
  2295. v4l2->vbi_dev.queue->lock = &v4l2->vb_vbi_queue_lock;
  2296. /* disable inapplicable ioctls */
  2297. v4l2_disable_ioctl(&v4l2->vbi_dev, VIDIOC_S_PARM);
  2298. if (dev->tuner_type == TUNER_ABSENT) {
  2299. v4l2_disable_ioctl(&v4l2->vbi_dev, VIDIOC_G_TUNER);
  2300. v4l2_disable_ioctl(&v4l2->vbi_dev, VIDIOC_S_TUNER);
  2301. v4l2_disable_ioctl(&v4l2->vbi_dev, VIDIOC_G_FREQUENCY);
  2302. v4l2_disable_ioctl(&v4l2->vbi_dev, VIDIOC_S_FREQUENCY);
  2303. }
  2304. if (dev->int_audio_type == EM28XX_INT_AUDIO_NONE) {
  2305. v4l2_disable_ioctl(&v4l2->vbi_dev, VIDIOC_G_AUDIO);
  2306. v4l2_disable_ioctl(&v4l2->vbi_dev, VIDIOC_S_AUDIO);
  2307. }
  2308. /* register v4l2 vbi video_device */
  2309. ret = video_register_device(&v4l2->vbi_dev, VFL_TYPE_VBI,
  2310. vbi_nr[dev->devno]);
  2311. if (ret < 0) {
  2312. dev_err(&dev->intf->dev,
  2313. "unable to register vbi device\n");
  2314. goto unregister_dev;
  2315. }
  2316. }
  2317. if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
  2318. em28xx_vdev_init(dev, &v4l2->radio_dev, &em28xx_radio_template,
  2319. "radio");
  2320. ret = video_register_device(&v4l2->radio_dev, VFL_TYPE_RADIO,
  2321. radio_nr[dev->devno]);
  2322. if (ret < 0) {
  2323. dev_err(&dev->intf->dev,
  2324. "can't register radio device\n");
  2325. goto unregister_dev;
  2326. }
  2327. dev_info(&dev->intf->dev,
  2328. "Registered radio device as %s\n",
  2329. video_device_node_name(&v4l2->radio_dev));
  2330. }
  2331. /* Init entities at the Media Controller */
  2332. em28xx_v4l2_create_entities(dev);
  2333. #ifdef CONFIG_MEDIA_CONTROLLER
  2334. ret = v4l2_mc_create_media_graph(dev->media_dev);
  2335. if (ret) {
  2336. dev_err(&dev->intf->dev,
  2337. "failed to create media graph\n");
  2338. em28xx_v4l2_media_release(dev);
  2339. goto unregister_dev;
  2340. }
  2341. #endif
  2342. dev_info(&dev->intf->dev,
  2343. "V4L2 video device registered as %s\n",
  2344. video_device_node_name(&v4l2->vdev));
  2345. if (video_is_registered(&v4l2->vbi_dev))
  2346. dev_info(&dev->intf->dev,
  2347. "V4L2 VBI device registered as %s\n",
  2348. video_device_node_name(&v4l2->vbi_dev));
  2349. /* Save some power by putting tuner to sleep */
  2350. v4l2_device_call_all(&v4l2->v4l2_dev, 0, tuner, standby);
  2351. /* initialize videobuf2 stuff */
  2352. em28xx_vb2_setup(dev);
  2353. dev_info(&dev->intf->dev,
  2354. "V4L2 extension successfully initialized\n");
  2355. kref_get(&dev->ref);
  2356. mutex_unlock(&dev->lock);
  2357. return 0;
  2358. unregister_dev:
  2359. if (video_is_registered(&v4l2->radio_dev)) {
  2360. dev_info(&dev->intf->dev,
  2361. "V4L2 device %s deregistered\n",
  2362. video_device_node_name(&v4l2->radio_dev));
  2363. video_unregister_device(&v4l2->radio_dev);
  2364. }
  2365. if (video_is_registered(&v4l2->vbi_dev)) {
  2366. dev_info(&dev->intf->dev,
  2367. "V4L2 device %s deregistered\n",
  2368. video_device_node_name(&v4l2->vbi_dev));
  2369. video_unregister_device(&v4l2->vbi_dev);
  2370. }
  2371. if (video_is_registered(&v4l2->vdev)) {
  2372. dev_info(&dev->intf->dev,
  2373. "V4L2 device %s deregistered\n",
  2374. video_device_node_name(&v4l2->vdev));
  2375. video_unregister_device(&v4l2->vdev);
  2376. }
  2377. v4l2_ctrl_handler_free(&v4l2->ctrl_handler);
  2378. v4l2_device_unregister(&v4l2->v4l2_dev);
  2379. err:
  2380. dev->v4l2 = NULL;
  2381. kref_put(&v4l2->ref, em28xx_free_v4l2);
  2382. mutex_unlock(&dev->lock);
  2383. return ret;
  2384. }
  2385. static struct em28xx_ops v4l2_ops = {
  2386. .id = EM28XX_V4L2,
  2387. .name = "Em28xx v4l2 Extension",
  2388. .init = em28xx_v4l2_init,
  2389. .fini = em28xx_v4l2_fini,
  2390. .suspend = em28xx_v4l2_suspend,
  2391. .resume = em28xx_v4l2_resume,
  2392. };
  2393. static int __init em28xx_video_register(void)
  2394. {
  2395. return em28xx_register_extension(&v4l2_ops);
  2396. }
  2397. static void __exit em28xx_video_unregister(void)
  2398. {
  2399. em28xx_unregister_extension(&v4l2_ops);
  2400. }
  2401. module_init(em28xx_video_register);
  2402. module_exit(em28xx_video_unregister);