em28xx-video.c 76 KB

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