em28xx-video.c 64 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. Some parts based on SN9C10x PC Camera Controllers GPL driver made
  9. by Luca Risolia <luca.risolia@studio.unibo.it>
  10. This program is free software; you can redistribute it and/or modify
  11. it under the terms of the GNU General Public License as published by
  12. the Free Software Foundation; either version 2 of the License, or
  13. (at your option) any later version.
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. GNU General Public License for more details.
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <linux/init.h>
  23. #include <linux/list.h>
  24. #include <linux/module.h>
  25. #include <linux/kernel.h>
  26. #include <linux/bitmap.h>
  27. #include <linux/usb.h>
  28. #include <linux/i2c.h>
  29. #include <linux/mm.h>
  30. #include <linux/mutex.h>
  31. #include <linux/slab.h>
  32. #include "em28xx.h"
  33. #include <media/v4l2-common.h>
  34. #include <media/v4l2-ioctl.h>
  35. #include <media/v4l2-chip-ident.h>
  36. #include <media/msp3400.h>
  37. #include <media/tuner.h>
  38. #define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
  39. "Markus Rechberger <mrechberger@gmail.com>, " \
  40. "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
  41. "Sascha Sommer <saschasommer@freenet.de>"
  42. #define DRIVER_DESC "Empia em28xx based USB video device driver"
  43. #define EM28XX_VERSION "0.1.3"
  44. #define em28xx_videodbg(fmt, arg...) do {\
  45. if (video_debug) \
  46. printk(KERN_INFO "%s %s :"fmt, \
  47. dev->name, __func__ , ##arg); } while (0)
  48. static unsigned int isoc_debug;
  49. module_param(isoc_debug, int, 0644);
  50. MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
  51. #define em28xx_isocdbg(fmt, arg...) \
  52. do {\
  53. if (isoc_debug) { \
  54. printk(KERN_INFO "%s %s :"fmt, \
  55. dev->name, __func__ , ##arg); \
  56. } \
  57. } while (0)
  58. MODULE_AUTHOR(DRIVER_AUTHOR);
  59. MODULE_DESCRIPTION(DRIVER_DESC);
  60. MODULE_LICENSE("GPL");
  61. MODULE_VERSION(EM28XX_VERSION);
  62. static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
  63. static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
  64. static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
  65. module_param_array(video_nr, int, NULL, 0444);
  66. module_param_array(vbi_nr, int, NULL, 0444);
  67. module_param_array(radio_nr, int, NULL, 0444);
  68. MODULE_PARM_DESC(video_nr, "video device numbers");
  69. MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
  70. MODULE_PARM_DESC(radio_nr, "radio device numbers");
  71. static unsigned int video_debug;
  72. module_param(video_debug, int, 0644);
  73. MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
  74. /* supported video standards */
  75. static struct em28xx_fmt format[] = {
  76. {
  77. .name = "16 bpp YUY2, 4:2:2, packed",
  78. .fourcc = V4L2_PIX_FMT_YUYV,
  79. .depth = 16,
  80. .reg = EM28XX_OUTFMT_YUV422_Y0UY1V,
  81. }, {
  82. .name = "16 bpp RGB 565, LE",
  83. .fourcc = V4L2_PIX_FMT_RGB565,
  84. .depth = 16,
  85. .reg = EM28XX_OUTFMT_RGB_16_656,
  86. }, {
  87. .name = "8 bpp Bayer BGBG..GRGR",
  88. .fourcc = V4L2_PIX_FMT_SBGGR8,
  89. .depth = 8,
  90. .reg = EM28XX_OUTFMT_RGB_8_BGBG,
  91. }, {
  92. .name = "8 bpp Bayer GRGR..BGBG",
  93. .fourcc = V4L2_PIX_FMT_SGRBG8,
  94. .depth = 8,
  95. .reg = EM28XX_OUTFMT_RGB_8_GRGR,
  96. }, {
  97. .name = "8 bpp Bayer GBGB..RGRG",
  98. .fourcc = V4L2_PIX_FMT_SGBRG8,
  99. .depth = 8,
  100. .reg = EM28XX_OUTFMT_RGB_8_GBGB,
  101. }, {
  102. .name = "12 bpp YUV411",
  103. .fourcc = V4L2_PIX_FMT_YUV411P,
  104. .depth = 12,
  105. .reg = EM28XX_OUTFMT_YUV411,
  106. },
  107. };
  108. /* supported controls */
  109. /* Common to all boards */
  110. static struct v4l2_queryctrl ac97_qctrl[] = {
  111. {
  112. .id = V4L2_CID_AUDIO_VOLUME,
  113. .type = V4L2_CTRL_TYPE_INTEGER,
  114. .name = "Volume",
  115. .minimum = 0x0,
  116. .maximum = 0x1f,
  117. .step = 0x1,
  118. .default_value = 0x1f,
  119. .flags = V4L2_CTRL_FLAG_SLIDER,
  120. }, {
  121. .id = V4L2_CID_AUDIO_MUTE,
  122. .type = V4L2_CTRL_TYPE_BOOLEAN,
  123. .name = "Mute",
  124. .minimum = 0,
  125. .maximum = 1,
  126. .step = 1,
  127. .default_value = 1,
  128. .flags = 0,
  129. }
  130. };
  131. /* ------------------------------------------------------------------
  132. DMA and thread functions
  133. ------------------------------------------------------------------*/
  134. /*
  135. * Announces that a buffer were filled and request the next
  136. */
  137. static inline void buffer_filled(struct em28xx *dev,
  138. struct em28xx_dmaqueue *dma_q,
  139. struct em28xx_buffer *buf)
  140. {
  141. /* Advice that buffer was filled */
  142. em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
  143. buf->vb.state = VIDEOBUF_DONE;
  144. buf->vb.field_count++;
  145. v4l2_get_timestamp(&buf->vb.ts);
  146. dev->isoc_ctl.vid_buf = NULL;
  147. list_del(&buf->vb.queue);
  148. wake_up(&buf->vb.done);
  149. }
  150. static inline void vbi_buffer_filled(struct em28xx *dev,
  151. struct em28xx_dmaqueue *dma_q,
  152. struct em28xx_buffer *buf)
  153. {
  154. /* Advice that buffer was filled */
  155. em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
  156. buf->vb.state = VIDEOBUF_DONE;
  157. buf->vb.field_count++;
  158. v4l2_get_timestamp(&buf->vb.ts);
  159. dev->isoc_ctl.vbi_buf = NULL;
  160. list_del(&buf->vb.queue);
  161. wake_up(&buf->vb.done);
  162. }
  163. /*
  164. * Identify the buffer header type and properly handles
  165. */
  166. static void em28xx_copy_video(struct em28xx *dev,
  167. struct em28xx_dmaqueue *dma_q,
  168. struct em28xx_buffer *buf,
  169. unsigned char *p,
  170. unsigned char *outp, unsigned long len)
  171. {
  172. void *fieldstart, *startwrite, *startread;
  173. int linesdone, currlinedone, offset, lencopy, remain;
  174. int bytesperline = dev->width << 1;
  175. if (dma_q->pos + len > buf->vb.size)
  176. len = buf->vb.size - dma_q->pos;
  177. startread = p;
  178. remain = len;
  179. if (dev->progressive || buf->top_field)
  180. fieldstart = outp;
  181. else /* interlaced mode, even nr. of lines */
  182. fieldstart = outp + bytesperline;
  183. linesdone = dma_q->pos / bytesperline;
  184. currlinedone = dma_q->pos % bytesperline;
  185. if (dev->progressive)
  186. offset = linesdone * bytesperline + currlinedone;
  187. else
  188. offset = linesdone * bytesperline * 2 + currlinedone;
  189. startwrite = fieldstart + offset;
  190. lencopy = bytesperline - currlinedone;
  191. lencopy = lencopy > remain ? remain : lencopy;
  192. if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
  193. em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
  194. ((char *)startwrite + lencopy) -
  195. ((char *)outp + buf->vb.size));
  196. remain = (char *)outp + buf->vb.size - (char *)startwrite;
  197. lencopy = remain;
  198. }
  199. if (lencopy <= 0)
  200. return;
  201. memcpy(startwrite, startread, lencopy);
  202. remain -= lencopy;
  203. while (remain > 0) {
  204. if (dev->progressive)
  205. startwrite += lencopy;
  206. else
  207. startwrite += lencopy + bytesperline;
  208. startread += lencopy;
  209. if (bytesperline > remain)
  210. lencopy = remain;
  211. else
  212. lencopy = bytesperline;
  213. if ((char *)startwrite + lencopy > (char *)outp +
  214. buf->vb.size) {
  215. em28xx_isocdbg("Overflow of %zi bytes past buffer end"
  216. "(2)\n",
  217. ((char *)startwrite + lencopy) -
  218. ((char *)outp + buf->vb.size));
  219. lencopy = remain = (char *)outp + buf->vb.size -
  220. (char *)startwrite;
  221. }
  222. if (lencopy <= 0)
  223. break;
  224. memcpy(startwrite, startread, lencopy);
  225. remain -= lencopy;
  226. }
  227. dma_q->pos += len;
  228. }
  229. static void em28xx_copy_vbi(struct em28xx *dev,
  230. struct em28xx_dmaqueue *dma_q,
  231. struct em28xx_buffer *buf,
  232. unsigned char *p,
  233. unsigned char *outp, unsigned long len)
  234. {
  235. void *startwrite, *startread;
  236. int offset;
  237. int bytesperline;
  238. if (dev == NULL) {
  239. em28xx_isocdbg("dev is null\n");
  240. return;
  241. }
  242. bytesperline = dev->vbi_width;
  243. if (dma_q == NULL) {
  244. em28xx_isocdbg("dma_q is null\n");
  245. return;
  246. }
  247. if (buf == NULL) {
  248. return;
  249. }
  250. if (p == NULL) {
  251. em28xx_isocdbg("p is null\n");
  252. return;
  253. }
  254. if (outp == NULL) {
  255. em28xx_isocdbg("outp is null\n");
  256. return;
  257. }
  258. if (dma_q->pos + len > buf->vb.size)
  259. len = buf->vb.size - dma_q->pos;
  260. startread = p;
  261. startwrite = outp + dma_q->pos;
  262. offset = dma_q->pos;
  263. /* Make sure the bottom field populates the second half of the frame */
  264. if (buf->top_field == 0) {
  265. startwrite += bytesperline * dev->vbi_height;
  266. offset += bytesperline * dev->vbi_height;
  267. }
  268. memcpy(startwrite, startread, len);
  269. dma_q->pos += len;
  270. }
  271. static inline void print_err_status(struct em28xx *dev,
  272. int packet, int status)
  273. {
  274. char *errmsg = "Unknown";
  275. switch (status) {
  276. case -ENOENT:
  277. errmsg = "unlinked synchronuously";
  278. break;
  279. case -ECONNRESET:
  280. errmsg = "unlinked asynchronuously";
  281. break;
  282. case -ENOSR:
  283. errmsg = "Buffer error (overrun)";
  284. break;
  285. case -EPIPE:
  286. errmsg = "Stalled (device not responding)";
  287. break;
  288. case -EOVERFLOW:
  289. errmsg = "Babble (bad cable?)";
  290. break;
  291. case -EPROTO:
  292. errmsg = "Bit-stuff error (bad cable?)";
  293. break;
  294. case -EILSEQ:
  295. errmsg = "CRC/Timeout (could be anything)";
  296. break;
  297. case -ETIME:
  298. errmsg = "Device does not respond";
  299. break;
  300. }
  301. if (packet < 0) {
  302. em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
  303. } else {
  304. em28xx_isocdbg("URB packet %d, status %d [%s].\n",
  305. packet, status, errmsg);
  306. }
  307. }
  308. /*
  309. * video-buf generic routine to get the next available buffer
  310. */
  311. static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
  312. struct em28xx_buffer **buf)
  313. {
  314. struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
  315. char *outp;
  316. if (list_empty(&dma_q->active)) {
  317. em28xx_isocdbg("No active queue to serve\n");
  318. dev->isoc_ctl.vid_buf = NULL;
  319. *buf = NULL;
  320. return;
  321. }
  322. /* Get the next buffer */
  323. *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
  324. /* Cleans up buffer - Useful for testing for frame/URB loss */
  325. outp = videobuf_to_vmalloc(&(*buf)->vb);
  326. memset(outp, 0, (*buf)->vb.size);
  327. dev->isoc_ctl.vid_buf = *buf;
  328. return;
  329. }
  330. /*
  331. * video-buf generic routine to get the next available VBI buffer
  332. */
  333. static inline void vbi_get_next_buf(struct em28xx_dmaqueue *dma_q,
  334. struct em28xx_buffer **buf)
  335. {
  336. struct em28xx *dev = container_of(dma_q, struct em28xx, vbiq);
  337. char *outp;
  338. if (list_empty(&dma_q->active)) {
  339. em28xx_isocdbg("No active queue to serve\n");
  340. dev->isoc_ctl.vbi_buf = NULL;
  341. *buf = NULL;
  342. return;
  343. }
  344. /* Get the next buffer */
  345. *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
  346. /* Cleans up buffer - Useful for testing for frame/URB loss */
  347. outp = videobuf_to_vmalloc(&(*buf)->vb);
  348. memset(outp, 0x00, (*buf)->vb.size);
  349. dev->isoc_ctl.vbi_buf = *buf;
  350. return;
  351. }
  352. /*
  353. * Controls the isoc copy of each urb packet
  354. */
  355. static inline int em28xx_isoc_copy(struct em28xx *dev, struct urb *urb)
  356. {
  357. struct em28xx_buffer *buf;
  358. struct em28xx_dmaqueue *dma_q = &dev->vidq;
  359. unsigned char *outp = NULL;
  360. int i, len = 0, rc = 1;
  361. unsigned char *p;
  362. if (!dev)
  363. return 0;
  364. if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
  365. return 0;
  366. if (urb->status < 0) {
  367. print_err_status(dev, -1, urb->status);
  368. if (urb->status == -ENOENT)
  369. return 0;
  370. }
  371. buf = dev->isoc_ctl.vid_buf;
  372. if (buf != NULL)
  373. outp = videobuf_to_vmalloc(&buf->vb);
  374. for (i = 0; i < urb->number_of_packets; i++) {
  375. int status = urb->iso_frame_desc[i].status;
  376. if (status < 0) {
  377. print_err_status(dev, i, status);
  378. if (urb->iso_frame_desc[i].status != -EPROTO)
  379. continue;
  380. }
  381. len = urb->iso_frame_desc[i].actual_length - 4;
  382. if (urb->iso_frame_desc[i].actual_length <= 0) {
  383. /* em28xx_isocdbg("packet %d is empty",i); - spammy */
  384. continue;
  385. }
  386. if (urb->iso_frame_desc[i].actual_length >
  387. dev->max_pkt_size) {
  388. em28xx_isocdbg("packet bigger than packet size");
  389. continue;
  390. }
  391. p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  392. /* FIXME: incomplete buffer checks where removed to make
  393. logic simpler. Impacts of those changes should be evaluated
  394. */
  395. if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
  396. em28xx_isocdbg("VBI HEADER!!!\n");
  397. /* FIXME: Should add vbi copy */
  398. continue;
  399. }
  400. if (p[0] == 0x22 && p[1] == 0x5a) {
  401. em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
  402. len, (p[2] & 1) ? "odd" : "even");
  403. if (dev->progressive || !(p[2] & 1)) {
  404. if (buf != NULL)
  405. buffer_filled(dev, dma_q, buf);
  406. get_next_buf(dma_q, &buf);
  407. if (buf == NULL)
  408. outp = NULL;
  409. else
  410. outp = videobuf_to_vmalloc(&buf->vb);
  411. }
  412. if (buf != NULL) {
  413. if (p[2] & 1)
  414. buf->top_field = 0;
  415. else
  416. buf->top_field = 1;
  417. }
  418. dma_q->pos = 0;
  419. }
  420. if (buf != NULL) {
  421. if (p[0] != 0x88 && p[0] != 0x22) {
  422. em28xx_isocdbg("frame is not complete\n");
  423. len += 4;
  424. } else {
  425. p += 4;
  426. }
  427. em28xx_copy_video(dev, dma_q, buf, p, outp, len);
  428. }
  429. }
  430. return rc;
  431. }
  432. /* Version of isoc handler that takes into account a mixture of video and
  433. VBI data */
  434. static inline int em28xx_isoc_copy_vbi(struct em28xx *dev, struct urb *urb)
  435. {
  436. struct em28xx_buffer *buf, *vbi_buf;
  437. struct em28xx_dmaqueue *dma_q = &dev->vidq;
  438. struct em28xx_dmaqueue *vbi_dma_q = &dev->vbiq;
  439. unsigned char *outp = NULL;
  440. unsigned char *vbioutp = NULL;
  441. int i, len = 0, rc = 1;
  442. unsigned char *p;
  443. int vbi_size;
  444. if (!dev)
  445. return 0;
  446. if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
  447. return 0;
  448. if (urb->status < 0) {
  449. print_err_status(dev, -1, urb->status);
  450. if (urb->status == -ENOENT)
  451. return 0;
  452. }
  453. buf = dev->isoc_ctl.vid_buf;
  454. if (buf != NULL)
  455. outp = videobuf_to_vmalloc(&buf->vb);
  456. vbi_buf = dev->isoc_ctl.vbi_buf;
  457. if (vbi_buf != NULL)
  458. vbioutp = videobuf_to_vmalloc(&vbi_buf->vb);
  459. for (i = 0; i < urb->number_of_packets; i++) {
  460. int status = urb->iso_frame_desc[i].status;
  461. if (status < 0) {
  462. print_err_status(dev, i, status);
  463. if (urb->iso_frame_desc[i].status != -EPROTO)
  464. continue;
  465. }
  466. len = urb->iso_frame_desc[i].actual_length;
  467. if (urb->iso_frame_desc[i].actual_length <= 0) {
  468. /* em28xx_isocdbg("packet %d is empty",i); - spammy */
  469. continue;
  470. }
  471. if (urb->iso_frame_desc[i].actual_length >
  472. dev->max_pkt_size) {
  473. em28xx_isocdbg("packet bigger than packet size");
  474. continue;
  475. }
  476. p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  477. /* capture type 0 = vbi start
  478. capture type 1 = video start
  479. capture type 2 = video in progress */
  480. if (p[0] == 0x33 && p[1] == 0x95) {
  481. dev->capture_type = 0;
  482. dev->vbi_read = 0;
  483. em28xx_isocdbg("VBI START HEADER!!!\n");
  484. dev->cur_field = p[2];
  485. p += 4;
  486. len -= 4;
  487. } else if (p[0] == 0x88 && p[1] == 0x88 &&
  488. p[2] == 0x88 && p[3] == 0x88) {
  489. /* continuation */
  490. p += 4;
  491. len -= 4;
  492. } else if (p[0] == 0x22 && p[1] == 0x5a) {
  493. /* start video */
  494. p += 4;
  495. len -= 4;
  496. }
  497. vbi_size = dev->vbi_width * dev->vbi_height;
  498. if (dev->capture_type == 0) {
  499. if (dev->vbi_read >= vbi_size) {
  500. /* We've already read all the VBI data, so
  501. treat the rest as video */
  502. em28xx_isocdbg("dev->vbi_read > vbi_size\n");
  503. } else if ((dev->vbi_read + len) < vbi_size) {
  504. /* This entire frame is VBI data */
  505. if (dev->vbi_read == 0 &&
  506. (!(dev->cur_field & 1))) {
  507. /* Brand new frame */
  508. if (vbi_buf != NULL)
  509. vbi_buffer_filled(dev,
  510. vbi_dma_q,
  511. vbi_buf);
  512. vbi_get_next_buf(vbi_dma_q, &vbi_buf);
  513. if (vbi_buf == NULL)
  514. vbioutp = NULL;
  515. else
  516. vbioutp = videobuf_to_vmalloc(
  517. &vbi_buf->vb);
  518. }
  519. if (dev->vbi_read == 0) {
  520. vbi_dma_q->pos = 0;
  521. if (vbi_buf != NULL) {
  522. if (dev->cur_field & 1)
  523. vbi_buf->top_field = 0;
  524. else
  525. vbi_buf->top_field = 1;
  526. }
  527. }
  528. dev->vbi_read += len;
  529. em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
  530. vbioutp, len);
  531. } else {
  532. /* Some of this frame is VBI data and some is
  533. video data */
  534. int vbi_data_len = vbi_size - dev->vbi_read;
  535. dev->vbi_read += vbi_data_len;
  536. em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
  537. vbioutp, vbi_data_len);
  538. dev->capture_type = 1;
  539. p += vbi_data_len;
  540. len -= vbi_data_len;
  541. }
  542. }
  543. if (dev->capture_type == 1) {
  544. dev->capture_type = 2;
  545. if (dev->progressive || !(dev->cur_field & 1)) {
  546. if (buf != NULL)
  547. buffer_filled(dev, dma_q, buf);
  548. get_next_buf(dma_q, &buf);
  549. if (buf == NULL)
  550. outp = NULL;
  551. else
  552. outp = videobuf_to_vmalloc(&buf->vb);
  553. }
  554. if (buf != NULL) {
  555. if (dev->cur_field & 1)
  556. buf->top_field = 0;
  557. else
  558. buf->top_field = 1;
  559. }
  560. dma_q->pos = 0;
  561. }
  562. if (buf != NULL && dev->capture_type == 2) {
  563. if (len >= 4 && p[0] == 0x88 && p[1] == 0x88 &&
  564. p[2] == 0x88 && p[3] == 0x88) {
  565. p += 4;
  566. len -= 4;
  567. }
  568. if (len >= 4 && p[0] == 0x22 && p[1] == 0x5a) {
  569. em28xx_isocdbg("Video frame %d, len=%i, %s\n",
  570. p[2], len, (p[2] & 1) ?
  571. "odd" : "even");
  572. p += 4;
  573. len -= 4;
  574. }
  575. if (len > 0)
  576. em28xx_copy_video(dev, dma_q, buf, p, outp,
  577. len);
  578. }
  579. }
  580. return rc;
  581. }
  582. /* ------------------------------------------------------------------
  583. Videobuf operations
  584. ------------------------------------------------------------------*/
  585. static int
  586. buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
  587. {
  588. struct em28xx_fh *fh = vq->priv_data;
  589. struct em28xx *dev = fh->dev;
  590. struct v4l2_frequency f;
  591. *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)
  592. >> 3;
  593. if (0 == *count)
  594. *count = EM28XX_DEF_BUF;
  595. if (*count < EM28XX_MIN_BUF)
  596. *count = EM28XX_MIN_BUF;
  597. /* Ask tuner to go to analog or radio mode */
  598. memset(&f, 0, sizeof(f));
  599. f.frequency = dev->ctl_freq;
  600. f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  601. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
  602. return 0;
  603. }
  604. /* This is called *without* dev->slock held; please keep it that way */
  605. static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
  606. {
  607. struct em28xx_fh *fh = vq->priv_data;
  608. struct em28xx *dev = fh->dev;
  609. unsigned long flags = 0;
  610. if (in_interrupt())
  611. BUG();
  612. /* We used to wait for the buffer to finish here, but this didn't work
  613. because, as we were keeping the state as VIDEOBUF_QUEUED,
  614. videobuf_queue_cancel marked it as finished for us.
  615. (Also, it could wedge forever if the hardware was misconfigured.)
  616. This should be safe; by the time we get here, the buffer isn't
  617. queued anymore. If we ever start marking the buffers as
  618. VIDEOBUF_ACTIVE, it won't be, though.
  619. */
  620. spin_lock_irqsave(&dev->slock, flags);
  621. if (dev->isoc_ctl.vid_buf == buf)
  622. dev->isoc_ctl.vid_buf = NULL;
  623. spin_unlock_irqrestore(&dev->slock, flags);
  624. videobuf_vmalloc_free(&buf->vb);
  625. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  626. }
  627. static int
  628. buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
  629. enum v4l2_field field)
  630. {
  631. struct em28xx_fh *fh = vq->priv_data;
  632. struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
  633. struct em28xx *dev = fh->dev;
  634. int rc = 0, urb_init = 0;
  635. buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
  636. + 7) >> 3;
  637. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  638. return -EINVAL;
  639. buf->vb.width = dev->width;
  640. buf->vb.height = dev->height;
  641. buf->vb.field = field;
  642. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  643. rc = videobuf_iolock(vq, &buf->vb, NULL);
  644. if (rc < 0)
  645. goto fail;
  646. }
  647. if (!dev->isoc_ctl.analog_bufs.num_bufs)
  648. urb_init = 1;
  649. if (urb_init) {
  650. if (em28xx_vbi_supported(dev) == 1)
  651. rc = em28xx_init_isoc(dev, EM28XX_ANALOG_MODE,
  652. EM28XX_NUM_ISOC_PACKETS,
  653. EM28XX_NUM_BUFS,
  654. dev->max_pkt_size,
  655. em28xx_isoc_copy_vbi);
  656. else
  657. rc = em28xx_init_isoc(dev, EM28XX_ANALOG_MODE,
  658. EM28XX_NUM_ISOC_PACKETS,
  659. EM28XX_NUM_BUFS,
  660. dev->max_pkt_size,
  661. em28xx_isoc_copy);
  662. if (rc < 0)
  663. goto fail;
  664. }
  665. buf->vb.state = VIDEOBUF_PREPARED;
  666. return 0;
  667. fail:
  668. free_buffer(vq, buf);
  669. return rc;
  670. }
  671. static void
  672. buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  673. {
  674. struct em28xx_buffer *buf = container_of(vb,
  675. struct em28xx_buffer,
  676. vb);
  677. struct em28xx_fh *fh = vq->priv_data;
  678. struct em28xx *dev = fh->dev;
  679. struct em28xx_dmaqueue *vidq = &dev->vidq;
  680. buf->vb.state = VIDEOBUF_QUEUED;
  681. list_add_tail(&buf->vb.queue, &vidq->active);
  682. }
  683. static void buffer_release(struct videobuf_queue *vq,
  684. struct videobuf_buffer *vb)
  685. {
  686. struct em28xx_buffer *buf = container_of(vb,
  687. struct em28xx_buffer,
  688. vb);
  689. struct em28xx_fh *fh = vq->priv_data;
  690. struct em28xx *dev = (struct em28xx *)fh->dev;
  691. em28xx_isocdbg("em28xx: called buffer_release\n");
  692. free_buffer(vq, buf);
  693. }
  694. static struct videobuf_queue_ops em28xx_video_qops = {
  695. .buf_setup = buffer_setup,
  696. .buf_prepare = buffer_prepare,
  697. .buf_queue = buffer_queue,
  698. .buf_release = buffer_release,
  699. };
  700. /********************* v4l2 interface **************************************/
  701. static void video_mux(struct em28xx *dev, int index)
  702. {
  703. dev->ctl_input = index;
  704. dev->ctl_ainput = INPUT(index)->amux;
  705. dev->ctl_aoutput = INPUT(index)->aout;
  706. if (!dev->ctl_aoutput)
  707. dev->ctl_aoutput = EM28XX_AOUT_MASTER;
  708. v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
  709. INPUT(index)->vmux, 0, 0);
  710. if (dev->board.has_msp34xx) {
  711. if (dev->i2s_speed) {
  712. v4l2_device_call_all(&dev->v4l2_dev, 0, audio,
  713. s_i2s_clock_freq, dev->i2s_speed);
  714. }
  715. /* Note: this is msp3400 specific */
  716. v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
  717. dev->ctl_ainput, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
  718. }
  719. if (dev->board.adecoder != EM28XX_NOADECODER) {
  720. v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
  721. dev->ctl_ainput, dev->ctl_aoutput, 0);
  722. }
  723. em28xx_audio_analog_set(dev);
  724. }
  725. /* Usage lock check functions */
  726. static int res_get(struct em28xx_fh *fh, unsigned int bit)
  727. {
  728. struct em28xx *dev = fh->dev;
  729. if (fh->resources & bit)
  730. /* have it already allocated */
  731. return 1;
  732. /* is it free? */
  733. if (dev->resources & bit) {
  734. /* no, someone else uses it */
  735. return 0;
  736. }
  737. /* it's free, grab it */
  738. fh->resources |= bit;
  739. dev->resources |= bit;
  740. em28xx_videodbg("res: get %d\n", bit);
  741. return 1;
  742. }
  743. static int res_check(struct em28xx_fh *fh, unsigned int bit)
  744. {
  745. return fh->resources & bit;
  746. }
  747. static int res_locked(struct em28xx *dev, unsigned int bit)
  748. {
  749. return dev->resources & bit;
  750. }
  751. static void res_free(struct em28xx_fh *fh, unsigned int bits)
  752. {
  753. struct em28xx *dev = fh->dev;
  754. BUG_ON((fh->resources & bits) != bits);
  755. fh->resources &= ~bits;
  756. dev->resources &= ~bits;
  757. em28xx_videodbg("res: put %d\n", bits);
  758. }
  759. static int get_ressource(struct em28xx_fh *fh)
  760. {
  761. switch (fh->type) {
  762. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  763. return EM28XX_RESOURCE_VIDEO;
  764. case V4L2_BUF_TYPE_VBI_CAPTURE:
  765. return EM28XX_RESOURCE_VBI;
  766. default:
  767. BUG();
  768. return 0;
  769. }
  770. }
  771. /*
  772. * ac97_queryctrl()
  773. * return the ac97 supported controls
  774. */
  775. static int ac97_queryctrl(struct v4l2_queryctrl *qc)
  776. {
  777. int i;
  778. for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
  779. if (qc->id && qc->id == ac97_qctrl[i].id) {
  780. memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
  781. return 0;
  782. }
  783. }
  784. /* Control is not ac97 related */
  785. return 1;
  786. }
  787. /*
  788. * ac97_get_ctrl()
  789. * return the current values for ac97 mute and volume
  790. */
  791. static int ac97_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
  792. {
  793. switch (ctrl->id) {
  794. case V4L2_CID_AUDIO_MUTE:
  795. ctrl->value = dev->mute;
  796. return 0;
  797. case V4L2_CID_AUDIO_VOLUME:
  798. ctrl->value = dev->volume;
  799. return 0;
  800. default:
  801. /* Control is not ac97 related */
  802. return 1;
  803. }
  804. }
  805. /*
  806. * ac97_set_ctrl()
  807. * set values for ac97 mute and volume
  808. */
  809. static int ac97_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
  810. {
  811. int i;
  812. for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++)
  813. if (ctrl->id == ac97_qctrl[i].id)
  814. goto handle;
  815. /* Announce that hasn't handle it */
  816. return 1;
  817. handle:
  818. if (ctrl->value < ac97_qctrl[i].minimum ||
  819. ctrl->value > ac97_qctrl[i].maximum)
  820. return -ERANGE;
  821. switch (ctrl->id) {
  822. case V4L2_CID_AUDIO_MUTE:
  823. dev->mute = ctrl->value;
  824. break;
  825. case V4L2_CID_AUDIO_VOLUME:
  826. dev->volume = ctrl->value;
  827. break;
  828. }
  829. return em28xx_audio_analog_set(dev);
  830. }
  831. static int check_dev(struct em28xx *dev)
  832. {
  833. if (dev->state & DEV_DISCONNECTED) {
  834. em28xx_errdev("v4l2 ioctl: device not present\n");
  835. return -ENODEV;
  836. }
  837. if (dev->state & DEV_MISCONFIGURED) {
  838. em28xx_errdev("v4l2 ioctl: device is misconfigured; "
  839. "close and open it again\n");
  840. return -EIO;
  841. }
  842. return 0;
  843. }
  844. static void get_scale(struct em28xx *dev,
  845. unsigned int width, unsigned int height,
  846. unsigned int *hscale, unsigned int *vscale)
  847. {
  848. unsigned int maxw = norm_maxw(dev);
  849. unsigned int maxh = norm_maxh(dev);
  850. *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
  851. if (*hscale >= 0x4000)
  852. *hscale = 0x3fff;
  853. *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
  854. if (*vscale >= 0x4000)
  855. *vscale = 0x3fff;
  856. }
  857. /* ------------------------------------------------------------------
  858. IOCTL vidioc handling
  859. ------------------------------------------------------------------*/
  860. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  861. struct v4l2_format *f)
  862. {
  863. struct em28xx_fh *fh = priv;
  864. struct em28xx *dev = fh->dev;
  865. f->fmt.pix.width = dev->width;
  866. f->fmt.pix.height = dev->height;
  867. f->fmt.pix.pixelformat = dev->format->fourcc;
  868. f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
  869. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
  870. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  871. /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
  872. if (dev->progressive)
  873. f->fmt.pix.field = V4L2_FIELD_NONE;
  874. else
  875. f->fmt.pix.field = dev->interlaced ?
  876. V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
  877. return 0;
  878. }
  879. static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
  880. {
  881. unsigned int i;
  882. for (i = 0; i < ARRAY_SIZE(format); i++)
  883. if (format[i].fourcc == fourcc)
  884. return &format[i];
  885. return NULL;
  886. }
  887. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  888. struct v4l2_format *f)
  889. {
  890. struct em28xx_fh *fh = priv;
  891. struct em28xx *dev = fh->dev;
  892. unsigned int width = f->fmt.pix.width;
  893. unsigned int height = f->fmt.pix.height;
  894. unsigned int maxw = norm_maxw(dev);
  895. unsigned int maxh = norm_maxh(dev);
  896. unsigned int hscale, vscale;
  897. struct em28xx_fmt *fmt;
  898. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  899. if (!fmt) {
  900. em28xx_videodbg("Fourcc format (%08x) invalid.\n",
  901. f->fmt.pix.pixelformat);
  902. return -EINVAL;
  903. }
  904. if (dev->board.is_em2800) {
  905. /* the em2800 can only scale down to 50% */
  906. height = height > (3 * maxh / 4) ? maxh : maxh / 2;
  907. width = width > (3 * maxw / 4) ? maxw : maxw / 2;
  908. /* MaxPacketSize for em2800 is too small to capture at full resolution
  909. * use half of maxw as the scaler can only scale to 50% */
  910. if (width == maxw && height == maxh)
  911. width /= 2;
  912. } else {
  913. /* width must even because of the YUYV format
  914. height must be even because of interlacing */
  915. v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh,
  916. 1, 0);
  917. }
  918. get_scale(dev, width, height, &hscale, &vscale);
  919. width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
  920. height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
  921. f->fmt.pix.width = width;
  922. f->fmt.pix.height = height;
  923. f->fmt.pix.pixelformat = fmt->fourcc;
  924. f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
  925. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
  926. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  927. if (dev->progressive)
  928. f->fmt.pix.field = V4L2_FIELD_NONE;
  929. else
  930. f->fmt.pix.field = dev->interlaced ?
  931. V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
  932. return 0;
  933. }
  934. static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
  935. unsigned width, unsigned height)
  936. {
  937. struct em28xx_fmt *fmt;
  938. fmt = format_by_fourcc(fourcc);
  939. if (!fmt)
  940. return -EINVAL;
  941. dev->format = fmt;
  942. dev->width = width;
  943. dev->height = height;
  944. /* set new image size */
  945. get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
  946. em28xx_set_alternate(dev);
  947. em28xx_resolution_set(dev);
  948. return 0;
  949. }
  950. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  951. struct v4l2_format *f)
  952. {
  953. struct em28xx_fh *fh = priv;
  954. struct em28xx *dev = fh->dev;
  955. int rc;
  956. rc = check_dev(dev);
  957. if (rc < 0)
  958. return rc;
  959. vidioc_try_fmt_vid_cap(file, priv, f);
  960. if (videobuf_queue_is_busy(&fh->vb_vidq)) {
  961. em28xx_errdev("%s queue busy\n", __func__);
  962. return -EBUSY;
  963. }
  964. return em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
  965. f->fmt.pix.width, f->fmt.pix.height);
  966. }
  967. static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
  968. {
  969. struct em28xx_fh *fh = priv;
  970. struct em28xx *dev = fh->dev;
  971. int rc;
  972. rc = check_dev(dev);
  973. if (rc < 0)
  974. return rc;
  975. *norm = dev->norm;
  976. return 0;
  977. }
  978. static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *norm)
  979. {
  980. struct em28xx_fh *fh = priv;
  981. struct em28xx *dev = fh->dev;
  982. int rc;
  983. rc = check_dev(dev);
  984. if (rc < 0)
  985. return rc;
  986. v4l2_device_call_all(&dev->v4l2_dev, 0, video, querystd, norm);
  987. return 0;
  988. }
  989. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
  990. {
  991. struct em28xx_fh *fh = priv;
  992. struct em28xx *dev = fh->dev;
  993. struct v4l2_format f;
  994. int rc;
  995. rc = check_dev(dev);
  996. if (rc < 0)
  997. return rc;
  998. dev->norm = *norm;
  999. /* Adjusts width/height, if needed */
  1000. f.fmt.pix.width = dev->width;
  1001. f.fmt.pix.height = dev->height;
  1002. vidioc_try_fmt_vid_cap(file, priv, &f);
  1003. /* set new image size */
  1004. dev->width = f.fmt.pix.width;
  1005. dev->height = f.fmt.pix.height;
  1006. get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
  1007. em28xx_resolution_set(dev);
  1008. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
  1009. return 0;
  1010. }
  1011. static int vidioc_g_parm(struct file *file, void *priv,
  1012. struct v4l2_streamparm *p)
  1013. {
  1014. struct em28xx_fh *fh = priv;
  1015. struct em28xx *dev = fh->dev;
  1016. int rc = 0;
  1017. if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1018. return -EINVAL;
  1019. if (dev->board.is_webcam)
  1020. rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0,
  1021. video, g_parm, p);
  1022. else
  1023. v4l2_video_std_frame_period(dev->norm,
  1024. &p->parm.capture.timeperframe);
  1025. return rc;
  1026. }
  1027. static int vidioc_s_parm(struct file *file, void *priv,
  1028. struct v4l2_streamparm *p)
  1029. {
  1030. struct em28xx_fh *fh = priv;
  1031. struct em28xx *dev = fh->dev;
  1032. if (!dev->board.is_webcam)
  1033. return -EINVAL;
  1034. if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1035. return -EINVAL;
  1036. return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p);
  1037. }
  1038. static const char *iname[] = {
  1039. [EM28XX_VMUX_COMPOSITE1] = "Composite1",
  1040. [EM28XX_VMUX_COMPOSITE2] = "Composite2",
  1041. [EM28XX_VMUX_COMPOSITE3] = "Composite3",
  1042. [EM28XX_VMUX_COMPOSITE4] = "Composite4",
  1043. [EM28XX_VMUX_SVIDEO] = "S-Video",
  1044. [EM28XX_VMUX_TELEVISION] = "Television",
  1045. [EM28XX_VMUX_CABLE] = "Cable TV",
  1046. [EM28XX_VMUX_DVB] = "DVB",
  1047. [EM28XX_VMUX_DEBUG] = "for debug only",
  1048. };
  1049. static int vidioc_enum_input(struct file *file, void *priv,
  1050. struct v4l2_input *i)
  1051. {
  1052. struct em28xx_fh *fh = priv;
  1053. struct em28xx *dev = fh->dev;
  1054. unsigned int n;
  1055. n = i->index;
  1056. if (n >= MAX_EM28XX_INPUT)
  1057. return -EINVAL;
  1058. if (0 == INPUT(n)->type)
  1059. return -EINVAL;
  1060. i->index = n;
  1061. i->type = V4L2_INPUT_TYPE_CAMERA;
  1062. strcpy(i->name, iname[INPUT(n)->type]);
  1063. if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
  1064. (EM28XX_VMUX_CABLE == INPUT(n)->type))
  1065. i->type = V4L2_INPUT_TYPE_TUNER;
  1066. i->std = dev->vdev->tvnorms;
  1067. return 0;
  1068. }
  1069. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  1070. {
  1071. struct em28xx_fh *fh = priv;
  1072. struct em28xx *dev = fh->dev;
  1073. *i = dev->ctl_input;
  1074. return 0;
  1075. }
  1076. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  1077. {
  1078. struct em28xx_fh *fh = priv;
  1079. struct em28xx *dev = fh->dev;
  1080. int rc;
  1081. rc = check_dev(dev);
  1082. if (rc < 0)
  1083. return rc;
  1084. if (i >= MAX_EM28XX_INPUT)
  1085. return -EINVAL;
  1086. if (0 == INPUT(i)->type)
  1087. return -EINVAL;
  1088. video_mux(dev, i);
  1089. return 0;
  1090. }
  1091. static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  1092. {
  1093. struct em28xx_fh *fh = priv;
  1094. struct em28xx *dev = fh->dev;
  1095. if (!dev->audio_mode.has_audio)
  1096. return -EINVAL;
  1097. switch (a->index) {
  1098. case EM28XX_AMUX_VIDEO:
  1099. strcpy(a->name, "Television");
  1100. break;
  1101. case EM28XX_AMUX_LINE_IN:
  1102. strcpy(a->name, "Line In");
  1103. break;
  1104. case EM28XX_AMUX_VIDEO2:
  1105. strcpy(a->name, "Television alt");
  1106. break;
  1107. case EM28XX_AMUX_PHONE:
  1108. strcpy(a->name, "Phone");
  1109. break;
  1110. case EM28XX_AMUX_MIC:
  1111. strcpy(a->name, "Mic");
  1112. break;
  1113. case EM28XX_AMUX_CD:
  1114. strcpy(a->name, "CD");
  1115. break;
  1116. case EM28XX_AMUX_AUX:
  1117. strcpy(a->name, "Aux");
  1118. break;
  1119. case EM28XX_AMUX_PCM_OUT:
  1120. strcpy(a->name, "PCM");
  1121. break;
  1122. default:
  1123. return -EINVAL;
  1124. }
  1125. a->index = dev->ctl_ainput;
  1126. a->capability = V4L2_AUDCAP_STEREO;
  1127. return 0;
  1128. }
  1129. static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
  1130. {
  1131. struct em28xx_fh *fh = priv;
  1132. struct em28xx *dev = fh->dev;
  1133. if (!dev->audio_mode.has_audio)
  1134. return -EINVAL;
  1135. if (a->index >= MAX_EM28XX_INPUT)
  1136. return -EINVAL;
  1137. if (0 == INPUT(a->index)->type)
  1138. return -EINVAL;
  1139. dev->ctl_ainput = INPUT(a->index)->amux;
  1140. dev->ctl_aoutput = INPUT(a->index)->aout;
  1141. if (!dev->ctl_aoutput)
  1142. dev->ctl_aoutput = EM28XX_AOUT_MASTER;
  1143. return 0;
  1144. }
  1145. static int vidioc_queryctrl(struct file *file, void *priv,
  1146. struct v4l2_queryctrl *qc)
  1147. {
  1148. struct em28xx_fh *fh = priv;
  1149. struct em28xx *dev = fh->dev;
  1150. int id = qc->id;
  1151. int rc;
  1152. rc = check_dev(dev);
  1153. if (rc < 0)
  1154. return rc;
  1155. memset(qc, 0, sizeof(*qc));
  1156. qc->id = id;
  1157. /* enumerate AC97 controls */
  1158. if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
  1159. rc = ac97_queryctrl(qc);
  1160. if (!rc)
  1161. return 0;
  1162. }
  1163. /* enumerate V4L2 device controls */
  1164. v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
  1165. if (qc->type)
  1166. return 0;
  1167. else
  1168. return -EINVAL;
  1169. }
  1170. /*
  1171. * FIXME: This is an indirect way to check if a control exists at a
  1172. * subdev. Instead of that hack, maybe the better would be to change all
  1173. * subdevs to return -ENOIOCTLCMD, if an ioctl is not supported.
  1174. */
  1175. static int check_subdev_ctrl(struct em28xx *dev, int id)
  1176. {
  1177. struct v4l2_queryctrl qc;
  1178. memset(&qc, 0, sizeof(qc));
  1179. qc.id = id;
  1180. /* enumerate V4L2 device controls */
  1181. v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, &qc);
  1182. if (qc.type)
  1183. return 0;
  1184. else
  1185. return -EINVAL;
  1186. }
  1187. static int vidioc_g_ctrl(struct file *file, void *priv,
  1188. struct v4l2_control *ctrl)
  1189. {
  1190. struct em28xx_fh *fh = priv;
  1191. struct em28xx *dev = fh->dev;
  1192. int rc;
  1193. rc = check_dev(dev);
  1194. if (rc < 0)
  1195. return rc;
  1196. rc = 0;
  1197. /* Set an AC97 control */
  1198. if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
  1199. rc = ac97_get_ctrl(dev, ctrl);
  1200. else
  1201. rc = 1;
  1202. /* It were not an AC97 control. Sends it to the v4l2 dev interface */
  1203. if (rc == 1) {
  1204. if (check_subdev_ctrl(dev, ctrl->id))
  1205. return -EINVAL;
  1206. v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
  1207. rc = 0;
  1208. }
  1209. return rc;
  1210. }
  1211. static int vidioc_s_ctrl(struct file *file, void *priv,
  1212. struct v4l2_control *ctrl)
  1213. {
  1214. struct em28xx_fh *fh = priv;
  1215. struct em28xx *dev = fh->dev;
  1216. int rc;
  1217. rc = check_dev(dev);
  1218. if (rc < 0)
  1219. return rc;
  1220. /* Set an AC97 control */
  1221. if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
  1222. rc = ac97_set_ctrl(dev, ctrl);
  1223. else
  1224. rc = 1;
  1225. /* It isn't an AC97 control. Sends it to the v4l2 dev interface */
  1226. if (rc == 1) {
  1227. rc = check_subdev_ctrl(dev, ctrl->id);
  1228. if (!rc)
  1229. v4l2_device_call_all(&dev->v4l2_dev, 0,
  1230. core, s_ctrl, ctrl);
  1231. /*
  1232. * In the case of non-AC97 volume controls, we still need
  1233. * to do some setups at em28xx, in order to mute/unmute
  1234. * and to adjust audio volume. However, the value ranges
  1235. * should be checked by the corresponding V4L subdriver.
  1236. */
  1237. switch (ctrl->id) {
  1238. case V4L2_CID_AUDIO_MUTE:
  1239. dev->mute = ctrl->value;
  1240. rc = em28xx_audio_analog_set(dev);
  1241. break;
  1242. case V4L2_CID_AUDIO_VOLUME:
  1243. dev->volume = ctrl->value;
  1244. rc = em28xx_audio_analog_set(dev);
  1245. }
  1246. }
  1247. return (rc < 0) ? rc : 0;
  1248. }
  1249. static int vidioc_g_tuner(struct file *file, void *priv,
  1250. struct v4l2_tuner *t)
  1251. {
  1252. struct em28xx_fh *fh = priv;
  1253. struct em28xx *dev = fh->dev;
  1254. int rc;
  1255. rc = check_dev(dev);
  1256. if (rc < 0)
  1257. return rc;
  1258. if (0 != t->index)
  1259. return -EINVAL;
  1260. strcpy(t->name, "Tuner");
  1261. t->type = V4L2_TUNER_ANALOG_TV;
  1262. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
  1263. return 0;
  1264. }
  1265. static int vidioc_s_tuner(struct file *file, void *priv,
  1266. struct v4l2_tuner *t)
  1267. {
  1268. struct em28xx_fh *fh = priv;
  1269. struct em28xx *dev = fh->dev;
  1270. int rc;
  1271. rc = check_dev(dev);
  1272. if (rc < 0)
  1273. return rc;
  1274. if (0 != t->index)
  1275. return -EINVAL;
  1276. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
  1277. return 0;
  1278. }
  1279. static int vidioc_g_frequency(struct file *file, void *priv,
  1280. struct v4l2_frequency *f)
  1281. {
  1282. struct em28xx_fh *fh = priv;
  1283. struct em28xx *dev = fh->dev;
  1284. f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1285. f->frequency = dev->ctl_freq;
  1286. return 0;
  1287. }
  1288. static int vidioc_s_frequency(struct file *file, void *priv,
  1289. struct v4l2_frequency *f)
  1290. {
  1291. struct em28xx_fh *fh = priv;
  1292. struct em28xx *dev = fh->dev;
  1293. int rc;
  1294. rc = check_dev(dev);
  1295. if (rc < 0)
  1296. return rc;
  1297. if (0 != f->tuner)
  1298. return -EINVAL;
  1299. if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
  1300. return -EINVAL;
  1301. if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
  1302. return -EINVAL;
  1303. dev->ctl_freq = f->frequency;
  1304. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
  1305. return 0;
  1306. }
  1307. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1308. static int em28xx_reg_len(int reg)
  1309. {
  1310. switch (reg) {
  1311. case EM28XX_R40_AC97LSB:
  1312. case EM28XX_R30_HSCALELOW:
  1313. case EM28XX_R32_VSCALELOW:
  1314. return 2;
  1315. default:
  1316. return 1;
  1317. }
  1318. }
  1319. static int vidioc_g_chip_ident(struct file *file, void *priv,
  1320. struct v4l2_dbg_chip_ident *chip)
  1321. {
  1322. struct em28xx_fh *fh = priv;
  1323. struct em28xx *dev = fh->dev;
  1324. chip->ident = V4L2_IDENT_NONE;
  1325. chip->revision = 0;
  1326. v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
  1327. return 0;
  1328. }
  1329. static int vidioc_g_register(struct file *file, void *priv,
  1330. struct v4l2_dbg_register *reg)
  1331. {
  1332. struct em28xx_fh *fh = priv;
  1333. struct em28xx *dev = fh->dev;
  1334. int ret;
  1335. switch (reg->match.type) {
  1336. case V4L2_CHIP_MATCH_AC97:
  1337. ret = em28xx_read_ac97(dev, reg->reg);
  1338. if (ret < 0)
  1339. return ret;
  1340. reg->val = ret;
  1341. reg->size = 1;
  1342. return 0;
  1343. case V4L2_CHIP_MATCH_I2C_DRIVER:
  1344. v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
  1345. return 0;
  1346. case V4L2_CHIP_MATCH_I2C_ADDR:
  1347. /* TODO: is this correct? */
  1348. v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
  1349. return 0;
  1350. default:
  1351. if (!v4l2_chip_match_host(&reg->match))
  1352. return -EINVAL;
  1353. }
  1354. /* Match host */
  1355. reg->size = em28xx_reg_len(reg->reg);
  1356. if (reg->size == 1) {
  1357. ret = em28xx_read_reg(dev, reg->reg);
  1358. if (ret < 0)
  1359. return ret;
  1360. reg->val = ret;
  1361. } else {
  1362. __le16 val = 0;
  1363. ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
  1364. reg->reg, (char *)&val, 2);
  1365. if (ret < 0)
  1366. return ret;
  1367. reg->val = le16_to_cpu(val);
  1368. }
  1369. return 0;
  1370. }
  1371. static int vidioc_s_register(struct file *file, void *priv,
  1372. struct v4l2_dbg_register *reg)
  1373. {
  1374. struct em28xx_fh *fh = priv;
  1375. struct em28xx *dev = fh->dev;
  1376. __le16 buf;
  1377. switch (reg->match.type) {
  1378. case V4L2_CHIP_MATCH_AC97:
  1379. return em28xx_write_ac97(dev, reg->reg, reg->val);
  1380. case V4L2_CHIP_MATCH_I2C_DRIVER:
  1381. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
  1382. return 0;
  1383. case V4L2_CHIP_MATCH_I2C_ADDR:
  1384. /* TODO: is this correct? */
  1385. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
  1386. return 0;
  1387. default:
  1388. if (!v4l2_chip_match_host(&reg->match))
  1389. return -EINVAL;
  1390. }
  1391. /* Match host */
  1392. buf = cpu_to_le16(reg->val);
  1393. return em28xx_write_regs(dev, reg->reg, (char *)&buf,
  1394. em28xx_reg_len(reg->reg));
  1395. }
  1396. #endif
  1397. static int vidioc_cropcap(struct file *file, void *priv,
  1398. struct v4l2_cropcap *cc)
  1399. {
  1400. struct em28xx_fh *fh = priv;
  1401. struct em28xx *dev = fh->dev;
  1402. if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1403. return -EINVAL;
  1404. cc->bounds.left = 0;
  1405. cc->bounds.top = 0;
  1406. cc->bounds.width = dev->width;
  1407. cc->bounds.height = dev->height;
  1408. cc->defrect = cc->bounds;
  1409. cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
  1410. cc->pixelaspect.denominator = 59;
  1411. return 0;
  1412. }
  1413. static int vidioc_streamon(struct file *file, void *priv,
  1414. enum v4l2_buf_type type)
  1415. {
  1416. struct em28xx_fh *fh = priv;
  1417. struct em28xx *dev = fh->dev;
  1418. int rc = -EINVAL;
  1419. rc = check_dev(dev);
  1420. if (rc < 0)
  1421. return rc;
  1422. if (unlikely(type != fh->type))
  1423. return -EINVAL;
  1424. em28xx_videodbg("vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
  1425. fh, type, fh->resources, dev->resources);
  1426. if (unlikely(!res_get(fh, get_ressource(fh))))
  1427. return -EBUSY;
  1428. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1429. rc = videobuf_streamon(&fh->vb_vidq);
  1430. else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
  1431. rc = videobuf_streamon(&fh->vb_vbiq);
  1432. return rc;
  1433. }
  1434. static int vidioc_streamoff(struct file *file, void *priv,
  1435. enum v4l2_buf_type type)
  1436. {
  1437. struct em28xx_fh *fh = priv;
  1438. struct em28xx *dev = fh->dev;
  1439. int rc;
  1440. rc = check_dev(dev);
  1441. if (rc < 0)
  1442. return rc;
  1443. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  1444. fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)
  1445. return -EINVAL;
  1446. if (type != fh->type)
  1447. return -EINVAL;
  1448. em28xx_videodbg("vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
  1449. fh, type, fh->resources, dev->resources);
  1450. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  1451. if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
  1452. videobuf_streamoff(&fh->vb_vidq);
  1453. res_free(fh, EM28XX_RESOURCE_VIDEO);
  1454. }
  1455. } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
  1456. if (res_check(fh, EM28XX_RESOURCE_VBI)) {
  1457. videobuf_streamoff(&fh->vb_vbiq);
  1458. res_free(fh, EM28XX_RESOURCE_VBI);
  1459. }
  1460. }
  1461. return 0;
  1462. }
  1463. static int vidioc_querycap(struct file *file, void *priv,
  1464. struct v4l2_capability *cap)
  1465. {
  1466. struct em28xx_fh *fh = priv;
  1467. struct em28xx *dev = fh->dev;
  1468. strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
  1469. strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
  1470. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  1471. cap->capabilities =
  1472. V4L2_CAP_SLICED_VBI_CAPTURE |
  1473. V4L2_CAP_VIDEO_CAPTURE |
  1474. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  1475. if (dev->vbi_dev)
  1476. cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
  1477. if (dev->audio_mode.has_audio)
  1478. cap->capabilities |= V4L2_CAP_AUDIO;
  1479. if (dev->tuner_type != TUNER_ABSENT)
  1480. cap->capabilities |= V4L2_CAP_TUNER;
  1481. return 0;
  1482. }
  1483. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  1484. struct v4l2_fmtdesc *f)
  1485. {
  1486. if (unlikely(f->index >= ARRAY_SIZE(format)))
  1487. return -EINVAL;
  1488. strlcpy(f->description, format[f->index].name, sizeof(f->description));
  1489. f->pixelformat = format[f->index].fourcc;
  1490. return 0;
  1491. }
  1492. static int vidioc_enum_framesizes(struct file *file, void *priv,
  1493. struct v4l2_frmsizeenum *fsize)
  1494. {
  1495. struct em28xx_fh *fh = priv;
  1496. struct em28xx *dev = fh->dev;
  1497. struct em28xx_fmt *fmt;
  1498. unsigned int maxw = norm_maxw(dev);
  1499. unsigned int maxh = norm_maxh(dev);
  1500. fmt = format_by_fourcc(fsize->pixel_format);
  1501. if (!fmt) {
  1502. em28xx_videodbg("Fourcc format (%08x) invalid.\n",
  1503. fsize->pixel_format);
  1504. return -EINVAL;
  1505. }
  1506. if (dev->board.is_em2800) {
  1507. if (fsize->index > 1)
  1508. return -EINVAL;
  1509. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  1510. fsize->discrete.width = maxw / (1 + fsize->index);
  1511. fsize->discrete.height = maxh / (1 + fsize->index);
  1512. return 0;
  1513. }
  1514. if (fsize->index != 0)
  1515. return -EINVAL;
  1516. /* Report a continuous range */
  1517. fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
  1518. fsize->stepwise.min_width = 48;
  1519. fsize->stepwise.min_height = 32;
  1520. fsize->stepwise.max_width = maxw;
  1521. fsize->stepwise.max_height = maxh;
  1522. fsize->stepwise.step_width = 1;
  1523. fsize->stepwise.step_height = 1;
  1524. return 0;
  1525. }
  1526. /* Sliced VBI ioctls */
  1527. static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
  1528. struct v4l2_format *f)
  1529. {
  1530. struct em28xx_fh *fh = priv;
  1531. struct em28xx *dev = fh->dev;
  1532. int rc;
  1533. rc = check_dev(dev);
  1534. if (rc < 0)
  1535. return rc;
  1536. f->fmt.sliced.service_set = 0;
  1537. v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
  1538. if (f->fmt.sliced.service_set == 0)
  1539. rc = -EINVAL;
  1540. return rc;
  1541. }
  1542. static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
  1543. struct v4l2_format *f)
  1544. {
  1545. struct em28xx_fh *fh = priv;
  1546. struct em28xx *dev = fh->dev;
  1547. int rc;
  1548. rc = check_dev(dev);
  1549. if (rc < 0)
  1550. return rc;
  1551. v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
  1552. if (f->fmt.sliced.service_set == 0)
  1553. return -EINVAL;
  1554. return 0;
  1555. }
  1556. /* RAW VBI ioctls */
  1557. static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
  1558. struct v4l2_format *format)
  1559. {
  1560. struct em28xx_fh *fh = priv;
  1561. struct em28xx *dev = fh->dev;
  1562. format->fmt.vbi.samples_per_line = dev->vbi_width;
  1563. format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  1564. format->fmt.vbi.offset = 0;
  1565. format->fmt.vbi.flags = 0;
  1566. format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
  1567. format->fmt.vbi.count[0] = dev->vbi_height;
  1568. format->fmt.vbi.count[1] = dev->vbi_height;
  1569. /* Varies by video standard (NTSC, PAL, etc.) */
  1570. if (dev->norm & V4L2_STD_525_60) {
  1571. /* NTSC */
  1572. format->fmt.vbi.start[0] = 10;
  1573. format->fmt.vbi.start[1] = 273;
  1574. } else if (dev->norm & V4L2_STD_625_50) {
  1575. /* PAL */
  1576. format->fmt.vbi.start[0] = 6;
  1577. format->fmt.vbi.start[1] = 318;
  1578. }
  1579. return 0;
  1580. }
  1581. static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
  1582. struct v4l2_format *format)
  1583. {
  1584. struct em28xx_fh *fh = priv;
  1585. struct em28xx *dev = fh->dev;
  1586. format->fmt.vbi.samples_per_line = dev->vbi_width;
  1587. format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  1588. format->fmt.vbi.offset = 0;
  1589. format->fmt.vbi.flags = 0;
  1590. format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
  1591. format->fmt.vbi.count[0] = dev->vbi_height;
  1592. format->fmt.vbi.count[1] = dev->vbi_height;
  1593. /* Varies by video standard (NTSC, PAL, etc.) */
  1594. if (dev->norm & V4L2_STD_525_60) {
  1595. /* NTSC */
  1596. format->fmt.vbi.start[0] = 10;
  1597. format->fmt.vbi.start[1] = 273;
  1598. } else if (dev->norm & V4L2_STD_625_50) {
  1599. /* PAL */
  1600. format->fmt.vbi.start[0] = 6;
  1601. format->fmt.vbi.start[1] = 318;
  1602. }
  1603. return 0;
  1604. }
  1605. static int vidioc_reqbufs(struct file *file, void *priv,
  1606. struct v4l2_requestbuffers *rb)
  1607. {
  1608. struct em28xx_fh *fh = priv;
  1609. struct em28xx *dev = fh->dev;
  1610. int rc;
  1611. rc = check_dev(dev);
  1612. if (rc < 0)
  1613. return rc;
  1614. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1615. return videobuf_reqbufs(&fh->vb_vidq, rb);
  1616. else
  1617. return videobuf_reqbufs(&fh->vb_vbiq, rb);
  1618. }
  1619. static int vidioc_querybuf(struct file *file, void *priv,
  1620. struct v4l2_buffer *b)
  1621. {
  1622. struct em28xx_fh *fh = priv;
  1623. struct em28xx *dev = fh->dev;
  1624. int rc;
  1625. rc = check_dev(dev);
  1626. if (rc < 0)
  1627. return rc;
  1628. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1629. return videobuf_querybuf(&fh->vb_vidq, b);
  1630. else {
  1631. /* FIXME: I'm not sure yet whether this is a bug in zvbi or
  1632. the videobuf framework, but we probably shouldn't be
  1633. returning a buffer larger than that which was asked for.
  1634. At a minimum, it causes a crash in zvbi since it does
  1635. a memcpy based on the source buffer length */
  1636. int result = videobuf_querybuf(&fh->vb_vbiq, b);
  1637. b->length = dev->vbi_width * dev->vbi_height * 2;
  1638. return result;
  1639. }
  1640. }
  1641. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1642. {
  1643. struct em28xx_fh *fh = priv;
  1644. struct em28xx *dev = fh->dev;
  1645. int rc;
  1646. rc = check_dev(dev);
  1647. if (rc < 0)
  1648. return rc;
  1649. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1650. return videobuf_qbuf(&fh->vb_vidq, b);
  1651. else
  1652. return videobuf_qbuf(&fh->vb_vbiq, b);
  1653. }
  1654. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1655. {
  1656. struct em28xx_fh *fh = priv;
  1657. struct em28xx *dev = fh->dev;
  1658. int rc;
  1659. rc = check_dev(dev);
  1660. if (rc < 0)
  1661. return rc;
  1662. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1663. return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags &
  1664. O_NONBLOCK);
  1665. else
  1666. return videobuf_dqbuf(&fh->vb_vbiq, b, file->f_flags &
  1667. O_NONBLOCK);
  1668. }
  1669. /* ----------------------------------------------------------- */
  1670. /* RADIO ESPECIFIC IOCTLS */
  1671. /* ----------------------------------------------------------- */
  1672. static int radio_querycap(struct file *file, void *priv,
  1673. struct v4l2_capability *cap)
  1674. {
  1675. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1676. strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
  1677. strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
  1678. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  1679. cap->capabilities = V4L2_CAP_TUNER;
  1680. return 0;
  1681. }
  1682. static int radio_g_tuner(struct file *file, void *priv,
  1683. struct v4l2_tuner *t)
  1684. {
  1685. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1686. if (unlikely(t->index > 0))
  1687. return -EINVAL;
  1688. strcpy(t->name, "Radio");
  1689. t->type = V4L2_TUNER_RADIO;
  1690. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
  1691. return 0;
  1692. }
  1693. static int radio_enum_input(struct file *file, void *priv,
  1694. struct v4l2_input *i)
  1695. {
  1696. if (i->index != 0)
  1697. return -EINVAL;
  1698. strcpy(i->name, "Radio");
  1699. i->type = V4L2_INPUT_TYPE_TUNER;
  1700. return 0;
  1701. }
  1702. static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  1703. {
  1704. if (unlikely(a->index))
  1705. return -EINVAL;
  1706. strcpy(a->name, "Radio");
  1707. return 0;
  1708. }
  1709. static int radio_s_tuner(struct file *file, void *priv,
  1710. struct v4l2_tuner *t)
  1711. {
  1712. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1713. if (0 != t->index)
  1714. return -EINVAL;
  1715. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
  1716. return 0;
  1717. }
  1718. static int radio_s_audio(struct file *file, void *fh,
  1719. const struct v4l2_audio *a)
  1720. {
  1721. return 0;
  1722. }
  1723. static int radio_s_input(struct file *file, void *fh, unsigned int i)
  1724. {
  1725. return 0;
  1726. }
  1727. static int radio_queryctrl(struct file *file, void *priv,
  1728. struct v4l2_queryctrl *qc)
  1729. {
  1730. int i;
  1731. if (qc->id < V4L2_CID_BASE ||
  1732. qc->id >= V4L2_CID_LASTP1)
  1733. return -EINVAL;
  1734. for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
  1735. if (qc->id && qc->id == ac97_qctrl[i].id) {
  1736. memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
  1737. return 0;
  1738. }
  1739. }
  1740. return -EINVAL;
  1741. }
  1742. /*
  1743. * em28xx_v4l2_open()
  1744. * inits the device and starts isoc transfer
  1745. */
  1746. static int em28xx_v4l2_open(struct file *filp)
  1747. {
  1748. int errCode = 0, radio = 0;
  1749. struct video_device *vdev = video_devdata(filp);
  1750. struct em28xx *dev = video_drvdata(filp);
  1751. enum v4l2_buf_type fh_type = 0;
  1752. struct em28xx_fh *fh;
  1753. enum v4l2_field field;
  1754. switch (vdev->vfl_type) {
  1755. case VFL_TYPE_GRABBER:
  1756. fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1757. break;
  1758. case VFL_TYPE_VBI:
  1759. fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
  1760. break;
  1761. case VFL_TYPE_RADIO:
  1762. radio = 1;
  1763. break;
  1764. }
  1765. em28xx_videodbg("open dev=%s type=%s users=%d\n",
  1766. video_device_node_name(vdev), v4l2_type_names[fh_type],
  1767. dev->users);
  1768. if (mutex_lock_interruptible(&dev->lock))
  1769. return -ERESTARTSYS;
  1770. fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
  1771. if (!fh) {
  1772. em28xx_errdev("em28xx-video.c: Out of memory?!\n");
  1773. mutex_unlock(&dev->lock);
  1774. return -ENOMEM;
  1775. }
  1776. fh->dev = dev;
  1777. fh->radio = radio;
  1778. fh->type = fh_type;
  1779. filp->private_data = fh;
  1780. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
  1781. em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
  1782. em28xx_set_alternate(dev);
  1783. em28xx_resolution_set(dev);
  1784. /* Needed, since GPIO might have disabled power of
  1785. some i2c device
  1786. */
  1787. em28xx_wake_i2c(dev);
  1788. }
  1789. if (fh->radio) {
  1790. em28xx_videodbg("video_open: setting radio device\n");
  1791. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
  1792. }
  1793. dev->users++;
  1794. if (dev->progressive)
  1795. field = V4L2_FIELD_NONE;
  1796. else
  1797. field = V4L2_FIELD_INTERLACED;
  1798. videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
  1799. NULL, &dev->slock,
  1800. V4L2_BUF_TYPE_VIDEO_CAPTURE, field,
  1801. sizeof(struct em28xx_buffer), fh, &dev->lock);
  1802. videobuf_queue_vmalloc_init(&fh->vb_vbiq, &em28xx_vbi_qops,
  1803. NULL, &dev->slock,
  1804. V4L2_BUF_TYPE_VBI_CAPTURE,
  1805. V4L2_FIELD_SEQ_TB,
  1806. sizeof(struct em28xx_buffer), fh, &dev->lock);
  1807. mutex_unlock(&dev->lock);
  1808. return errCode;
  1809. }
  1810. /*
  1811. * em28xx_realease_resources()
  1812. * unregisters the v4l2,i2c and usb devices
  1813. * called when the device gets disconected or at module unload
  1814. */
  1815. void em28xx_release_analog_resources(struct em28xx *dev)
  1816. {
  1817. /*FIXME: I2C IR should be disconnected */
  1818. if (dev->radio_dev) {
  1819. if (video_is_registered(dev->radio_dev))
  1820. video_unregister_device(dev->radio_dev);
  1821. else
  1822. video_device_release(dev->radio_dev);
  1823. dev->radio_dev = NULL;
  1824. }
  1825. if (dev->vbi_dev) {
  1826. em28xx_info("V4L2 device %s deregistered\n",
  1827. video_device_node_name(dev->vbi_dev));
  1828. if (video_is_registered(dev->vbi_dev))
  1829. video_unregister_device(dev->vbi_dev);
  1830. else
  1831. video_device_release(dev->vbi_dev);
  1832. dev->vbi_dev = NULL;
  1833. }
  1834. if (dev->vdev) {
  1835. em28xx_info("V4L2 device %s deregistered\n",
  1836. video_device_node_name(dev->vdev));
  1837. if (video_is_registered(dev->vdev))
  1838. video_unregister_device(dev->vdev);
  1839. else
  1840. video_device_release(dev->vdev);
  1841. dev->vdev = NULL;
  1842. }
  1843. }
  1844. /*
  1845. * em28xx_v4l2_close()
  1846. * stops streaming and deallocates all resources allocated by the v4l2
  1847. * calls and ioctls
  1848. */
  1849. static int em28xx_v4l2_close(struct file *filp)
  1850. {
  1851. struct em28xx_fh *fh = filp->private_data;
  1852. struct em28xx *dev = fh->dev;
  1853. int errCode;
  1854. em28xx_videodbg("users=%d\n", dev->users);
  1855. mutex_lock(&dev->lock);
  1856. if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
  1857. videobuf_stop(&fh->vb_vidq);
  1858. res_free(fh, EM28XX_RESOURCE_VIDEO);
  1859. }
  1860. if (res_check(fh, EM28XX_RESOURCE_VBI)) {
  1861. videobuf_stop(&fh->vb_vbiq);
  1862. res_free(fh, EM28XX_RESOURCE_VBI);
  1863. }
  1864. if (dev->users == 1) {
  1865. /* the device is already disconnect,
  1866. free the remaining resources */
  1867. if (dev->state & DEV_DISCONNECTED) {
  1868. em28xx_release_resources(dev);
  1869. kfree(dev->alt_max_pkt_size);
  1870. mutex_unlock(&dev->lock);
  1871. kfree(dev);
  1872. kfree(fh);
  1873. return 0;
  1874. }
  1875. /* Save some power by putting tuner to sleep */
  1876. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
  1877. /* do this before setting alternate! */
  1878. em28xx_uninit_isoc(dev, EM28XX_ANALOG_MODE);
  1879. em28xx_set_mode(dev, EM28XX_SUSPEND);
  1880. /* set alternate 0 */
  1881. dev->alt = 0;
  1882. em28xx_videodbg("setting alternate 0\n");
  1883. errCode = usb_set_interface(dev->udev, 0, 0);
  1884. if (errCode < 0) {
  1885. em28xx_errdev("cannot change alternate number to "
  1886. "0 (error=%i)\n", errCode);
  1887. }
  1888. }
  1889. videobuf_mmap_free(&fh->vb_vidq);
  1890. videobuf_mmap_free(&fh->vb_vbiq);
  1891. kfree(fh);
  1892. dev->users--;
  1893. mutex_unlock(&dev->lock);
  1894. return 0;
  1895. }
  1896. /*
  1897. * em28xx_v4l2_read()
  1898. * will allocate buffers when called for the first time
  1899. */
  1900. static ssize_t
  1901. em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
  1902. loff_t *pos)
  1903. {
  1904. struct em28xx_fh *fh = filp->private_data;
  1905. struct em28xx *dev = fh->dev;
  1906. int rc;
  1907. rc = check_dev(dev);
  1908. if (rc < 0)
  1909. return rc;
  1910. if (mutex_lock_interruptible(&dev->lock))
  1911. return -ERESTARTSYS;
  1912. /* FIXME: read() is not prepared to allow changing the video
  1913. resolution while streaming. Seems a bug at em28xx_set_fmt
  1914. */
  1915. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  1916. if (res_locked(dev, EM28XX_RESOURCE_VIDEO))
  1917. rc = -EBUSY;
  1918. else
  1919. rc = videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
  1920. filp->f_flags & O_NONBLOCK);
  1921. } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
  1922. if (!res_get(fh, EM28XX_RESOURCE_VBI))
  1923. rc = -EBUSY;
  1924. else
  1925. rc = videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
  1926. filp->f_flags & O_NONBLOCK);
  1927. }
  1928. mutex_unlock(&dev->lock);
  1929. return rc;
  1930. }
  1931. /*
  1932. * em28xx_poll()
  1933. * will allocate buffers when called for the first time
  1934. */
  1935. static unsigned int em28xx_poll(struct file *filp, poll_table *wait)
  1936. {
  1937. struct em28xx_fh *fh = filp->private_data;
  1938. struct em28xx *dev = fh->dev;
  1939. int rc;
  1940. rc = check_dev(dev);
  1941. if (rc < 0)
  1942. return rc;
  1943. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  1944. if (!res_get(fh, EM28XX_RESOURCE_VIDEO))
  1945. return POLLERR;
  1946. return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
  1947. } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
  1948. if (!res_get(fh, EM28XX_RESOURCE_VBI))
  1949. return POLLERR;
  1950. return videobuf_poll_stream(filp, &fh->vb_vbiq, wait);
  1951. } else {
  1952. return POLLERR;
  1953. }
  1954. }
  1955. static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
  1956. {
  1957. struct em28xx_fh *fh = filp->private_data;
  1958. struct em28xx *dev = fh->dev;
  1959. unsigned int res;
  1960. mutex_lock(&dev->lock);
  1961. res = em28xx_poll(filp, wait);
  1962. mutex_unlock(&dev->lock);
  1963. return res;
  1964. }
  1965. /*
  1966. * em28xx_v4l2_mmap()
  1967. */
  1968. static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
  1969. {
  1970. struct em28xx_fh *fh = filp->private_data;
  1971. struct em28xx *dev = fh->dev;
  1972. int rc;
  1973. rc = check_dev(dev);
  1974. if (rc < 0)
  1975. return rc;
  1976. if (mutex_lock_interruptible(&dev->lock))
  1977. return -ERESTARTSYS;
  1978. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1979. rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
  1980. else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
  1981. rc = videobuf_mmap_mapper(&fh->vb_vbiq, vma);
  1982. mutex_unlock(&dev->lock);
  1983. em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
  1984. (unsigned long)vma->vm_start,
  1985. (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
  1986. rc);
  1987. return rc;
  1988. }
  1989. static const struct v4l2_file_operations em28xx_v4l_fops = {
  1990. .owner = THIS_MODULE,
  1991. .open = em28xx_v4l2_open,
  1992. .release = em28xx_v4l2_close,
  1993. .read = em28xx_v4l2_read,
  1994. .poll = em28xx_v4l2_poll,
  1995. .mmap = em28xx_v4l2_mmap,
  1996. .unlocked_ioctl = video_ioctl2,
  1997. };
  1998. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1999. .vidioc_querycap = vidioc_querycap,
  2000. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  2001. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  2002. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  2003. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  2004. .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
  2005. .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
  2006. .vidioc_enum_framesizes = vidioc_enum_framesizes,
  2007. .vidioc_g_audio = vidioc_g_audio,
  2008. .vidioc_s_audio = vidioc_s_audio,
  2009. .vidioc_cropcap = vidioc_cropcap,
  2010. .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
  2011. .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
  2012. .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
  2013. .vidioc_reqbufs = vidioc_reqbufs,
  2014. .vidioc_querybuf = vidioc_querybuf,
  2015. .vidioc_qbuf = vidioc_qbuf,
  2016. .vidioc_dqbuf = vidioc_dqbuf,
  2017. .vidioc_g_std = vidioc_g_std,
  2018. .vidioc_querystd = vidioc_querystd,
  2019. .vidioc_s_std = vidioc_s_std,
  2020. .vidioc_g_parm = vidioc_g_parm,
  2021. .vidioc_s_parm = vidioc_s_parm,
  2022. .vidioc_enum_input = vidioc_enum_input,
  2023. .vidioc_g_input = vidioc_g_input,
  2024. .vidioc_s_input = vidioc_s_input,
  2025. .vidioc_queryctrl = vidioc_queryctrl,
  2026. .vidioc_g_ctrl = vidioc_g_ctrl,
  2027. .vidioc_s_ctrl = vidioc_s_ctrl,
  2028. .vidioc_streamon = vidioc_streamon,
  2029. .vidioc_streamoff = vidioc_streamoff,
  2030. .vidioc_g_tuner = vidioc_g_tuner,
  2031. .vidioc_s_tuner = vidioc_s_tuner,
  2032. .vidioc_g_frequency = vidioc_g_frequency,
  2033. .vidioc_s_frequency = vidioc_s_frequency,
  2034. #ifdef CONFIG_VIDEO_ADV_DEBUG
  2035. .vidioc_g_register = vidioc_g_register,
  2036. .vidioc_s_register = vidioc_s_register,
  2037. .vidioc_g_chip_ident = vidioc_g_chip_ident,
  2038. #endif
  2039. };
  2040. static const struct video_device em28xx_video_template = {
  2041. .fops = &em28xx_v4l_fops,
  2042. .release = video_device_release,
  2043. .ioctl_ops = &video_ioctl_ops,
  2044. .tvnorms = V4L2_STD_ALL,
  2045. .current_norm = V4L2_STD_PAL,
  2046. };
  2047. static const struct v4l2_file_operations radio_fops = {
  2048. .owner = THIS_MODULE,
  2049. .open = em28xx_v4l2_open,
  2050. .release = em28xx_v4l2_close,
  2051. .unlocked_ioctl = video_ioctl2,
  2052. };
  2053. static const struct v4l2_ioctl_ops radio_ioctl_ops = {
  2054. .vidioc_querycap = radio_querycap,
  2055. .vidioc_g_tuner = radio_g_tuner,
  2056. .vidioc_enum_input = radio_enum_input,
  2057. .vidioc_g_audio = radio_g_audio,
  2058. .vidioc_s_tuner = radio_s_tuner,
  2059. .vidioc_s_audio = radio_s_audio,
  2060. .vidioc_s_input = radio_s_input,
  2061. .vidioc_queryctrl = radio_queryctrl,
  2062. .vidioc_g_ctrl = vidioc_g_ctrl,
  2063. .vidioc_s_ctrl = vidioc_s_ctrl,
  2064. .vidioc_g_frequency = vidioc_g_frequency,
  2065. .vidioc_s_frequency = vidioc_s_frequency,
  2066. #ifdef CONFIG_VIDEO_ADV_DEBUG
  2067. .vidioc_g_register = vidioc_g_register,
  2068. .vidioc_s_register = vidioc_s_register,
  2069. #endif
  2070. };
  2071. static struct video_device em28xx_radio_template = {
  2072. .name = "em28xx-radio",
  2073. .fops = &radio_fops,
  2074. .ioctl_ops = &radio_ioctl_ops,
  2075. };
  2076. /******************************** usb interface ******************************/
  2077. static struct video_device *em28xx_vdev_init(struct em28xx *dev,
  2078. const struct video_device *template,
  2079. const char *type_name)
  2080. {
  2081. struct video_device *vfd;
  2082. vfd = video_device_alloc();
  2083. if (NULL == vfd)
  2084. return NULL;
  2085. *vfd = *template;
  2086. vfd->v4l2_dev = &dev->v4l2_dev;
  2087. vfd->release = video_device_release;
  2088. vfd->debug = video_debug;
  2089. vfd->lock = &dev->lock;
  2090. snprintf(vfd->name, sizeof(vfd->name), "%s %s",
  2091. dev->name, type_name);
  2092. video_set_drvdata(vfd, dev);
  2093. return vfd;
  2094. }
  2095. int em28xx_register_analog_devices(struct em28xx *dev)
  2096. {
  2097. u8 val;
  2098. int ret;
  2099. unsigned int maxw;
  2100. printk(KERN_INFO "%s: v4l2 driver version %s\n",
  2101. dev->name, EM28XX_VERSION);
  2102. /* set default norm */
  2103. dev->norm = em28xx_video_template.current_norm;
  2104. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
  2105. dev->interlaced = EM28XX_INTERLACED_DEFAULT;
  2106. /* Analog specific initialization */
  2107. dev->format = &format[0];
  2108. maxw = norm_maxw(dev);
  2109. /* MaxPacketSize for em2800 is too small to capture at full resolution
  2110. * use half of maxw as the scaler can only scale to 50% */
  2111. if (dev->board.is_em2800)
  2112. maxw /= 2;
  2113. em28xx_set_video_format(dev, format[0].fourcc,
  2114. maxw, norm_maxh(dev));
  2115. video_mux(dev, 0);
  2116. /* Audio defaults */
  2117. dev->mute = 1;
  2118. dev->volume = 0x1f;
  2119. /* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
  2120. val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
  2121. em28xx_write_reg(dev, EM28XX_R0F_XCLK,
  2122. (EM28XX_XCLK_AUDIO_UNMUTE | val));
  2123. em28xx_set_outfmt(dev);
  2124. em28xx_colorlevels_set_default(dev);
  2125. em28xx_compression_disable(dev);
  2126. /* allocate and fill video video_device struct */
  2127. dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
  2128. if (!dev->vdev) {
  2129. em28xx_errdev("cannot allocate video_device.\n");
  2130. return -ENODEV;
  2131. }
  2132. /* register v4l2 video video_device */
  2133. ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
  2134. video_nr[dev->devno]);
  2135. if (ret) {
  2136. em28xx_errdev("unable to register video device (error=%i).\n",
  2137. ret);
  2138. return ret;
  2139. }
  2140. /* Allocate and fill vbi video_device struct */
  2141. if (em28xx_vbi_supported(dev) == 1) {
  2142. dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
  2143. "vbi");
  2144. /* register v4l2 vbi video_device */
  2145. ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
  2146. vbi_nr[dev->devno]);
  2147. if (ret < 0) {
  2148. em28xx_errdev("unable to register vbi device\n");
  2149. return ret;
  2150. }
  2151. }
  2152. if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
  2153. dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
  2154. "radio");
  2155. if (!dev->radio_dev) {
  2156. em28xx_errdev("cannot allocate video_device.\n");
  2157. return -ENODEV;
  2158. }
  2159. ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
  2160. radio_nr[dev->devno]);
  2161. if (ret < 0) {
  2162. em28xx_errdev("can't register radio device\n");
  2163. return ret;
  2164. }
  2165. em28xx_info("Registered radio device as %s\n",
  2166. video_device_node_name(dev->radio_dev));
  2167. }
  2168. em28xx_info("V4L2 video device registered as %s\n",
  2169. video_device_node_name(dev->vdev));
  2170. if (dev->vbi_dev)
  2171. em28xx_info("V4L2 VBI device registered as %s\n",
  2172. video_device_node_name(dev->vbi_dev));
  2173. return 0;
  2174. }