em28xx-video.c 70 KB

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