em28xx-video.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535
  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. do_gettimeofday(&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. do_gettimeofday(&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)
  180. fieldstart = outp;
  181. else {
  182. /* Interlaces two half frames */
  183. if (buf->top_field)
  184. fieldstart = outp;
  185. else
  186. fieldstart = outp + bytesperline;
  187. }
  188. linesdone = dma_q->pos / bytesperline;
  189. currlinedone = dma_q->pos % bytesperline;
  190. if (dev->progressive)
  191. offset = linesdone * bytesperline + currlinedone;
  192. else
  193. offset = linesdone * bytesperline * 2 + currlinedone;
  194. startwrite = fieldstart + offset;
  195. lencopy = bytesperline - currlinedone;
  196. lencopy = lencopy > remain ? remain : lencopy;
  197. if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
  198. em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
  199. ((char *)startwrite + lencopy) -
  200. ((char *)outp + buf->vb.size));
  201. remain = (char *)outp + buf->vb.size - (char *)startwrite;
  202. lencopy = remain;
  203. }
  204. if (lencopy <= 0)
  205. return;
  206. memcpy(startwrite, startread, lencopy);
  207. remain -= lencopy;
  208. while (remain > 0) {
  209. startwrite += lencopy + bytesperline;
  210. startread += lencopy;
  211. if (bytesperline > remain)
  212. lencopy = remain;
  213. else
  214. lencopy = bytesperline;
  215. if ((char *)startwrite + lencopy > (char *)outp +
  216. buf->vb.size) {
  217. em28xx_isocdbg("Overflow of %zi bytes past buffer end"
  218. "(2)\n",
  219. ((char *)startwrite + lencopy) -
  220. ((char *)outp + buf->vb.size));
  221. lencopy = remain = (char *)outp + buf->vb.size -
  222. (char *)startwrite;
  223. }
  224. if (lencopy <= 0)
  225. break;
  226. memcpy(startwrite, startread, lencopy);
  227. remain -= lencopy;
  228. }
  229. dma_q->pos += len;
  230. }
  231. static void em28xx_copy_vbi(struct em28xx *dev,
  232. struct em28xx_dmaqueue *dma_q,
  233. struct em28xx_buffer *buf,
  234. unsigned char *p,
  235. unsigned char *outp, unsigned long len)
  236. {
  237. void *startwrite, *startread;
  238. int offset;
  239. int bytesperline;
  240. if (dev == NULL) {
  241. em28xx_isocdbg("dev is null\n");
  242. return;
  243. }
  244. bytesperline = dev->vbi_width;
  245. if (dma_q == NULL) {
  246. em28xx_isocdbg("dma_q is null\n");
  247. return;
  248. }
  249. if (buf == NULL) {
  250. return;
  251. }
  252. if (p == NULL) {
  253. em28xx_isocdbg("p is null\n");
  254. return;
  255. }
  256. if (outp == NULL) {
  257. em28xx_isocdbg("outp is null\n");
  258. return;
  259. }
  260. if (dma_q->pos + len > buf->vb.size)
  261. len = buf->vb.size - dma_q->pos;
  262. startread = p;
  263. startwrite = outp + dma_q->pos;
  264. offset = dma_q->pos;
  265. /* Make sure the bottom field populates the second half of the frame */
  266. if (buf->top_field == 0) {
  267. startwrite += bytesperline * dev->vbi_height;
  268. offset += bytesperline * dev->vbi_height;
  269. }
  270. memcpy(startwrite, startread, len);
  271. dma_q->pos += len;
  272. }
  273. static inline void print_err_status(struct em28xx *dev,
  274. int packet, int status)
  275. {
  276. char *errmsg = "Unknown";
  277. switch (status) {
  278. case -ENOENT:
  279. errmsg = "unlinked synchronuously";
  280. break;
  281. case -ECONNRESET:
  282. errmsg = "unlinked asynchronuously";
  283. break;
  284. case -ENOSR:
  285. errmsg = "Buffer error (overrun)";
  286. break;
  287. case -EPIPE:
  288. errmsg = "Stalled (device not responding)";
  289. break;
  290. case -EOVERFLOW:
  291. errmsg = "Babble (bad cable?)";
  292. break;
  293. case -EPROTO:
  294. errmsg = "Bit-stuff error (bad cable?)";
  295. break;
  296. case -EILSEQ:
  297. errmsg = "CRC/Timeout (could be anything)";
  298. break;
  299. case -ETIME:
  300. errmsg = "Device does not respond";
  301. break;
  302. }
  303. if (packet < 0) {
  304. em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
  305. } else {
  306. em28xx_isocdbg("URB packet %d, status %d [%s].\n",
  307. packet, status, errmsg);
  308. }
  309. }
  310. /*
  311. * video-buf generic routine to get the next available buffer
  312. */
  313. static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
  314. struct em28xx_buffer **buf)
  315. {
  316. struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
  317. char *outp;
  318. if (list_empty(&dma_q->active)) {
  319. em28xx_isocdbg("No active queue to serve\n");
  320. dev->isoc_ctl.vid_buf = NULL;
  321. *buf = NULL;
  322. return;
  323. }
  324. /* Get the next buffer */
  325. *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
  326. /* Cleans up buffer - Useful for testing for frame/URB loss */
  327. outp = videobuf_to_vmalloc(&(*buf)->vb);
  328. memset(outp, 0, (*buf)->vb.size);
  329. dev->isoc_ctl.vid_buf = *buf;
  330. return;
  331. }
  332. /*
  333. * video-buf generic routine to get the next available VBI buffer
  334. */
  335. static inline void vbi_get_next_buf(struct em28xx_dmaqueue *dma_q,
  336. struct em28xx_buffer **buf)
  337. {
  338. struct em28xx *dev = container_of(dma_q, struct em28xx, vbiq);
  339. char *outp;
  340. if (list_empty(&dma_q->active)) {
  341. em28xx_isocdbg("No active queue to serve\n");
  342. dev->isoc_ctl.vbi_buf = NULL;
  343. *buf = NULL;
  344. return;
  345. }
  346. /* Get the next buffer */
  347. *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
  348. /* Cleans up buffer - Useful for testing for frame/URB loss */
  349. outp = videobuf_to_vmalloc(&(*buf)->vb);
  350. memset(outp, 0x00, (*buf)->vb.size);
  351. dev->isoc_ctl.vbi_buf = *buf;
  352. return;
  353. }
  354. /*
  355. * Controls the isoc copy of each urb packet
  356. */
  357. static inline int em28xx_isoc_copy(struct em28xx *dev, struct urb *urb)
  358. {
  359. struct em28xx_buffer *buf;
  360. struct em28xx_dmaqueue *dma_q = &dev->vidq;
  361. unsigned char *outp = NULL;
  362. int i, len = 0, rc = 1;
  363. unsigned char *p;
  364. if (!dev)
  365. return 0;
  366. if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
  367. return 0;
  368. if (urb->status < 0) {
  369. print_err_status(dev, -1, urb->status);
  370. if (urb->status == -ENOENT)
  371. return 0;
  372. }
  373. buf = dev->isoc_ctl.vid_buf;
  374. if (buf != NULL)
  375. outp = videobuf_to_vmalloc(&buf->vb);
  376. for (i = 0; i < urb->number_of_packets; i++) {
  377. int status = urb->iso_frame_desc[i].status;
  378. if (status < 0) {
  379. print_err_status(dev, i, status);
  380. if (urb->iso_frame_desc[i].status != -EPROTO)
  381. continue;
  382. }
  383. len = urb->iso_frame_desc[i].actual_length - 4;
  384. if (urb->iso_frame_desc[i].actual_length <= 0) {
  385. /* em28xx_isocdbg("packet %d is empty",i); - spammy */
  386. continue;
  387. }
  388. if (urb->iso_frame_desc[i].actual_length >
  389. dev->max_pkt_size) {
  390. em28xx_isocdbg("packet bigger than packet size");
  391. continue;
  392. }
  393. p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  394. /* FIXME: incomplete buffer checks where removed to make
  395. logic simpler. Impacts of those changes should be evaluated
  396. */
  397. if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
  398. em28xx_isocdbg("VBI HEADER!!!\n");
  399. /* FIXME: Should add vbi copy */
  400. continue;
  401. }
  402. if (p[0] == 0x22 && p[1] == 0x5a) {
  403. em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
  404. len, (p[2] & 1) ? "odd" : "even");
  405. if (dev->progressive || !(p[2] & 1)) {
  406. if (buf != NULL)
  407. buffer_filled(dev, dma_q, buf);
  408. get_next_buf(dma_q, &buf);
  409. if (buf == NULL)
  410. outp = NULL;
  411. else
  412. outp = videobuf_to_vmalloc(&buf->vb);
  413. }
  414. if (buf != NULL) {
  415. if (p[2] & 1)
  416. buf->top_field = 0;
  417. else
  418. buf->top_field = 1;
  419. }
  420. dma_q->pos = 0;
  421. }
  422. if (buf != NULL) {
  423. if (p[0] != 0x88 && p[0] != 0x22) {
  424. em28xx_isocdbg("frame is not complete\n");
  425. len += 4;
  426. } else {
  427. p += 4;
  428. }
  429. em28xx_copy_video(dev, dma_q, buf, p, outp, len);
  430. }
  431. }
  432. return rc;
  433. }
  434. /* Version of isoc handler that takes into account a mixture of video and
  435. VBI data */
  436. static inline int em28xx_isoc_copy_vbi(struct em28xx *dev, struct urb *urb)
  437. {
  438. struct em28xx_buffer *buf, *vbi_buf;
  439. struct em28xx_dmaqueue *dma_q = &dev->vidq;
  440. struct em28xx_dmaqueue *vbi_dma_q = &dev->vbiq;
  441. unsigned char *outp = NULL;
  442. unsigned char *vbioutp = NULL;
  443. int i, len = 0, rc = 1;
  444. unsigned char *p;
  445. int vbi_size;
  446. if (!dev)
  447. return 0;
  448. if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
  449. return 0;
  450. if (urb->status < 0) {
  451. print_err_status(dev, -1, urb->status);
  452. if (urb->status == -ENOENT)
  453. return 0;
  454. }
  455. buf = dev->isoc_ctl.vid_buf;
  456. if (buf != NULL)
  457. outp = videobuf_to_vmalloc(&buf->vb);
  458. vbi_buf = dev->isoc_ctl.vbi_buf;
  459. if (vbi_buf != NULL)
  460. vbioutp = videobuf_to_vmalloc(&vbi_buf->vb);
  461. for (i = 0; i < urb->number_of_packets; i++) {
  462. int status = urb->iso_frame_desc[i].status;
  463. if (status < 0) {
  464. print_err_status(dev, i, status);
  465. if (urb->iso_frame_desc[i].status != -EPROTO)
  466. continue;
  467. }
  468. len = urb->iso_frame_desc[i].actual_length;
  469. if (urb->iso_frame_desc[i].actual_length <= 0) {
  470. /* em28xx_isocdbg("packet %d is empty",i); - spammy */
  471. continue;
  472. }
  473. if (urb->iso_frame_desc[i].actual_length >
  474. dev->max_pkt_size) {
  475. em28xx_isocdbg("packet bigger than packet size");
  476. continue;
  477. }
  478. p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  479. /* capture type 0 = vbi start
  480. capture type 1 = video start
  481. capture type 2 = video in progress */
  482. if (p[0] == 0x33 && p[1] == 0x95) {
  483. dev->capture_type = 0;
  484. dev->vbi_read = 0;
  485. em28xx_isocdbg("VBI START HEADER!!!\n");
  486. dev->cur_field = p[2];
  487. p += 4;
  488. len -= 4;
  489. } else if (p[0] == 0x88 && p[1] == 0x88 &&
  490. p[2] == 0x88 && p[3] == 0x88) {
  491. /* continuation */
  492. p += 4;
  493. len -= 4;
  494. } else if (p[0] == 0x22 && p[1] == 0x5a) {
  495. /* start video */
  496. p += 4;
  497. len -= 4;
  498. }
  499. vbi_size = dev->vbi_width * dev->vbi_height;
  500. if (dev->capture_type == 0) {
  501. if (dev->vbi_read >= vbi_size) {
  502. /* We've already read all the VBI data, so
  503. treat the rest as video */
  504. em28xx_isocdbg("dev->vbi_read > vbi_size\n");
  505. } else if ((dev->vbi_read + len) < vbi_size) {
  506. /* This entire frame is VBI data */
  507. if (dev->vbi_read == 0 &&
  508. (!(dev->cur_field & 1))) {
  509. /* Brand new frame */
  510. if (vbi_buf != NULL)
  511. vbi_buffer_filled(dev,
  512. vbi_dma_q,
  513. vbi_buf);
  514. vbi_get_next_buf(vbi_dma_q, &vbi_buf);
  515. if (vbi_buf == NULL)
  516. vbioutp = NULL;
  517. else
  518. vbioutp = videobuf_to_vmalloc(
  519. &vbi_buf->vb);
  520. }
  521. if (dev->vbi_read == 0) {
  522. vbi_dma_q->pos = 0;
  523. if (vbi_buf != NULL) {
  524. if (dev->cur_field & 1)
  525. vbi_buf->top_field = 0;
  526. else
  527. vbi_buf->top_field = 1;
  528. }
  529. }
  530. dev->vbi_read += len;
  531. em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
  532. vbioutp, len);
  533. } else {
  534. /* Some of this frame is VBI data and some is
  535. video data */
  536. int vbi_data_len = vbi_size - dev->vbi_read;
  537. dev->vbi_read += vbi_data_len;
  538. em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
  539. vbioutp, vbi_data_len);
  540. dev->capture_type = 1;
  541. p += vbi_data_len;
  542. len -= vbi_data_len;
  543. }
  544. }
  545. if (dev->capture_type == 1) {
  546. dev->capture_type = 2;
  547. if (dev->progressive || !(dev->cur_field & 1)) {
  548. if (buf != NULL)
  549. buffer_filled(dev, dma_q, buf);
  550. get_next_buf(dma_q, &buf);
  551. if (buf == NULL)
  552. outp = NULL;
  553. else
  554. outp = videobuf_to_vmalloc(&buf->vb);
  555. }
  556. if (buf != NULL) {
  557. if (dev->cur_field & 1)
  558. buf->top_field = 0;
  559. else
  560. buf->top_field = 1;
  561. }
  562. dma_q->pos = 0;
  563. }
  564. if (buf != NULL && dev->capture_type == 2) {
  565. if (len >= 4 && p[0] == 0x88 && p[1] == 0x88 &&
  566. p[2] == 0x88 && p[3] == 0x88) {
  567. p += 4;
  568. len -= 4;
  569. }
  570. if (len >= 4 && p[0] == 0x22 && p[1] == 0x5a) {
  571. em28xx_isocdbg("Video frame %d, len=%i, %s\n",
  572. p[2], len, (p[2] & 1) ?
  573. "odd" : "even");
  574. p += 4;
  575. len -= 4;
  576. }
  577. if (len > 0)
  578. em28xx_copy_video(dev, dma_q, buf, p, outp,
  579. len);
  580. }
  581. }
  582. return rc;
  583. }
  584. /* ------------------------------------------------------------------
  585. Videobuf operations
  586. ------------------------------------------------------------------*/
  587. static int
  588. buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
  589. {
  590. struct em28xx_fh *fh = vq->priv_data;
  591. struct em28xx *dev = fh->dev;
  592. struct v4l2_frequency f;
  593. *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)
  594. >> 3;
  595. if (0 == *count)
  596. *count = EM28XX_DEF_BUF;
  597. if (*count < EM28XX_MIN_BUF)
  598. *count = EM28XX_MIN_BUF;
  599. /* Ask tuner to go to analog or radio mode */
  600. memset(&f, 0, sizeof(f));
  601. f.frequency = dev->ctl_freq;
  602. f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  603. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
  604. return 0;
  605. }
  606. /* This is called *without* dev->slock held; please keep it that way */
  607. static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
  608. {
  609. struct em28xx_fh *fh = vq->priv_data;
  610. struct em28xx *dev = fh->dev;
  611. unsigned long flags = 0;
  612. if (in_interrupt())
  613. BUG();
  614. /* We used to wait for the buffer to finish here, but this didn't work
  615. because, as we were keeping the state as VIDEOBUF_QUEUED,
  616. videobuf_queue_cancel marked it as finished for us.
  617. (Also, it could wedge forever if the hardware was misconfigured.)
  618. This should be safe; by the time we get here, the buffer isn't
  619. queued anymore. If we ever start marking the buffers as
  620. VIDEOBUF_ACTIVE, it won't be, though.
  621. */
  622. spin_lock_irqsave(&dev->slock, flags);
  623. if (dev->isoc_ctl.vid_buf == buf)
  624. dev->isoc_ctl.vid_buf = NULL;
  625. spin_unlock_irqrestore(&dev->slock, flags);
  626. videobuf_vmalloc_free(&buf->vb);
  627. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  628. }
  629. static int
  630. buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
  631. enum v4l2_field field)
  632. {
  633. struct em28xx_fh *fh = vq->priv_data;
  634. struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
  635. struct em28xx *dev = fh->dev;
  636. int rc = 0, urb_init = 0;
  637. buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
  638. + 7) >> 3;
  639. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  640. return -EINVAL;
  641. buf->vb.width = dev->width;
  642. buf->vb.height = dev->height;
  643. buf->vb.field = field;
  644. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  645. rc = videobuf_iolock(vq, &buf->vb, NULL);
  646. if (rc < 0)
  647. goto fail;
  648. }
  649. if (!dev->isoc_ctl.num_bufs)
  650. urb_init = 1;
  651. if (urb_init) {
  652. if (em28xx_vbi_supported(dev) == 1)
  653. rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
  654. EM28XX_NUM_BUFS,
  655. dev->max_pkt_size,
  656. em28xx_isoc_copy_vbi);
  657. else
  658. rc = em28xx_init_isoc(dev, EM28XX_NUM_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. } else {
  909. /* width must even because of the YUYV format
  910. height must be even because of interlacing */
  911. v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh,
  912. 1, 0);
  913. }
  914. get_scale(dev, width, height, &hscale, &vscale);
  915. width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
  916. height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
  917. f->fmt.pix.width = width;
  918. f->fmt.pix.height = height;
  919. f->fmt.pix.pixelformat = fmt->fourcc;
  920. f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
  921. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
  922. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  923. if (dev->progressive)
  924. f->fmt.pix.field = V4L2_FIELD_NONE;
  925. else
  926. f->fmt.pix.field = dev->interlaced ?
  927. V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
  928. return 0;
  929. }
  930. static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
  931. unsigned width, unsigned height)
  932. {
  933. struct em28xx_fmt *fmt;
  934. fmt = format_by_fourcc(fourcc);
  935. if (!fmt)
  936. return -EINVAL;
  937. dev->format = fmt;
  938. dev->width = width;
  939. dev->height = height;
  940. /* set new image size */
  941. get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
  942. em28xx_set_alternate(dev);
  943. em28xx_resolution_set(dev);
  944. return 0;
  945. }
  946. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  947. struct v4l2_format *f)
  948. {
  949. struct em28xx_fh *fh = priv;
  950. struct em28xx *dev = fh->dev;
  951. int rc;
  952. rc = check_dev(dev);
  953. if (rc < 0)
  954. return rc;
  955. vidioc_try_fmt_vid_cap(file, priv, f);
  956. if (videobuf_queue_is_busy(&fh->vb_vidq)) {
  957. em28xx_errdev("%s queue busy\n", __func__);
  958. return -EBUSY;
  959. }
  960. return em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
  961. f->fmt.pix.width, f->fmt.pix.height);
  962. }
  963. static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
  964. {
  965. struct em28xx_fh *fh = priv;
  966. struct em28xx *dev = fh->dev;
  967. int rc;
  968. rc = check_dev(dev);
  969. if (rc < 0)
  970. return rc;
  971. *norm = dev->norm;
  972. return 0;
  973. }
  974. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
  975. {
  976. struct em28xx_fh *fh = priv;
  977. struct em28xx *dev = fh->dev;
  978. struct v4l2_format f;
  979. int rc;
  980. rc = check_dev(dev);
  981. if (rc < 0)
  982. return rc;
  983. dev->norm = *norm;
  984. /* Adjusts width/height, if needed */
  985. f.fmt.pix.width = dev->width;
  986. f.fmt.pix.height = dev->height;
  987. vidioc_try_fmt_vid_cap(file, priv, &f);
  988. /* set new image size */
  989. dev->width = f.fmt.pix.width;
  990. dev->height = f.fmt.pix.height;
  991. get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
  992. em28xx_resolution_set(dev);
  993. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
  994. return 0;
  995. }
  996. static int vidioc_g_parm(struct file *file, void *priv,
  997. struct v4l2_streamparm *p)
  998. {
  999. struct em28xx_fh *fh = priv;
  1000. struct em28xx *dev = fh->dev;
  1001. int rc = 0;
  1002. if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1003. return -EINVAL;
  1004. if (dev->board.is_webcam)
  1005. rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0,
  1006. video, g_parm, p);
  1007. else
  1008. v4l2_video_std_frame_period(dev->norm,
  1009. &p->parm.capture.timeperframe);
  1010. return rc;
  1011. }
  1012. static int vidioc_s_parm(struct file *file, void *priv,
  1013. struct v4l2_streamparm *p)
  1014. {
  1015. struct em28xx_fh *fh = priv;
  1016. struct em28xx *dev = fh->dev;
  1017. if (!dev->board.is_webcam)
  1018. return -EINVAL;
  1019. if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1020. return -EINVAL;
  1021. return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p);
  1022. }
  1023. static const char *iname[] = {
  1024. [EM28XX_VMUX_COMPOSITE1] = "Composite1",
  1025. [EM28XX_VMUX_COMPOSITE2] = "Composite2",
  1026. [EM28XX_VMUX_COMPOSITE3] = "Composite3",
  1027. [EM28XX_VMUX_COMPOSITE4] = "Composite4",
  1028. [EM28XX_VMUX_SVIDEO] = "S-Video",
  1029. [EM28XX_VMUX_TELEVISION] = "Television",
  1030. [EM28XX_VMUX_CABLE] = "Cable TV",
  1031. [EM28XX_VMUX_DVB] = "DVB",
  1032. [EM28XX_VMUX_DEBUG] = "for debug only",
  1033. };
  1034. static int vidioc_enum_input(struct file *file, void *priv,
  1035. struct v4l2_input *i)
  1036. {
  1037. struct em28xx_fh *fh = priv;
  1038. struct em28xx *dev = fh->dev;
  1039. unsigned int n;
  1040. n = i->index;
  1041. if (n >= MAX_EM28XX_INPUT)
  1042. return -EINVAL;
  1043. if (0 == INPUT(n)->type)
  1044. return -EINVAL;
  1045. i->index = n;
  1046. i->type = V4L2_INPUT_TYPE_CAMERA;
  1047. strcpy(i->name, iname[INPUT(n)->type]);
  1048. if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
  1049. (EM28XX_VMUX_CABLE == INPUT(n)->type))
  1050. i->type = V4L2_INPUT_TYPE_TUNER;
  1051. i->std = dev->vdev->tvnorms;
  1052. return 0;
  1053. }
  1054. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  1055. {
  1056. struct em28xx_fh *fh = priv;
  1057. struct em28xx *dev = fh->dev;
  1058. *i = dev->ctl_input;
  1059. return 0;
  1060. }
  1061. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  1062. {
  1063. struct em28xx_fh *fh = priv;
  1064. struct em28xx *dev = fh->dev;
  1065. int rc;
  1066. rc = check_dev(dev);
  1067. if (rc < 0)
  1068. return rc;
  1069. if (i >= MAX_EM28XX_INPUT)
  1070. return -EINVAL;
  1071. if (0 == INPUT(i)->type)
  1072. return -EINVAL;
  1073. dev->ctl_input = i;
  1074. video_mux(dev, dev->ctl_input);
  1075. return 0;
  1076. }
  1077. static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  1078. {
  1079. struct em28xx_fh *fh = priv;
  1080. struct em28xx *dev = fh->dev;
  1081. if (!dev->audio_mode.has_audio)
  1082. return -EINVAL;
  1083. switch (a->index) {
  1084. case EM28XX_AMUX_VIDEO:
  1085. strcpy(a->name, "Television");
  1086. break;
  1087. case EM28XX_AMUX_LINE_IN:
  1088. strcpy(a->name, "Line In");
  1089. break;
  1090. case EM28XX_AMUX_VIDEO2:
  1091. strcpy(a->name, "Television alt");
  1092. break;
  1093. case EM28XX_AMUX_PHONE:
  1094. strcpy(a->name, "Phone");
  1095. break;
  1096. case EM28XX_AMUX_MIC:
  1097. strcpy(a->name, "Mic");
  1098. break;
  1099. case EM28XX_AMUX_CD:
  1100. strcpy(a->name, "CD");
  1101. break;
  1102. case EM28XX_AMUX_AUX:
  1103. strcpy(a->name, "Aux");
  1104. break;
  1105. case EM28XX_AMUX_PCM_OUT:
  1106. strcpy(a->name, "PCM");
  1107. break;
  1108. default:
  1109. return -EINVAL;
  1110. }
  1111. a->index = dev->ctl_ainput;
  1112. a->capability = V4L2_AUDCAP_STEREO;
  1113. return 0;
  1114. }
  1115. static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
  1116. {
  1117. struct em28xx_fh *fh = priv;
  1118. struct em28xx *dev = fh->dev;
  1119. if (!dev->audio_mode.has_audio)
  1120. return -EINVAL;
  1121. if (a->index >= MAX_EM28XX_INPUT)
  1122. return -EINVAL;
  1123. if (0 == INPUT(a->index)->type)
  1124. return -EINVAL;
  1125. dev->ctl_ainput = INPUT(a->index)->amux;
  1126. dev->ctl_aoutput = INPUT(a->index)->aout;
  1127. if (!dev->ctl_aoutput)
  1128. dev->ctl_aoutput = EM28XX_AOUT_MASTER;
  1129. return 0;
  1130. }
  1131. static int vidioc_queryctrl(struct file *file, void *priv,
  1132. struct v4l2_queryctrl *qc)
  1133. {
  1134. struct em28xx_fh *fh = priv;
  1135. struct em28xx *dev = fh->dev;
  1136. int id = qc->id;
  1137. int rc;
  1138. rc = check_dev(dev);
  1139. if (rc < 0)
  1140. return rc;
  1141. memset(qc, 0, sizeof(*qc));
  1142. qc->id = id;
  1143. /* enumerate AC97 controls */
  1144. if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
  1145. rc = ac97_queryctrl(qc);
  1146. if (!rc)
  1147. return 0;
  1148. }
  1149. /* enumerate V4L2 device controls */
  1150. v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
  1151. if (qc->type)
  1152. return 0;
  1153. else
  1154. return -EINVAL;
  1155. }
  1156. /*
  1157. * FIXME: This is an indirect way to check if a control exists at a
  1158. * subdev. Instead of that hack, maybe the better would be to change all
  1159. * subdevs to return -ENOIOCTLCMD, if an ioctl is not supported.
  1160. */
  1161. static int check_subdev_ctrl(struct em28xx *dev, int id)
  1162. {
  1163. struct v4l2_queryctrl qc;
  1164. memset(&qc, 0, sizeof(qc));
  1165. qc.id = id;
  1166. /* enumerate V4L2 device controls */
  1167. v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, &qc);
  1168. if (qc.type)
  1169. return 0;
  1170. else
  1171. return -EINVAL;
  1172. }
  1173. static int vidioc_g_ctrl(struct file *file, void *priv,
  1174. struct v4l2_control *ctrl)
  1175. {
  1176. struct em28xx_fh *fh = priv;
  1177. struct em28xx *dev = fh->dev;
  1178. int rc;
  1179. rc = check_dev(dev);
  1180. if (rc < 0)
  1181. return rc;
  1182. rc = 0;
  1183. /* Set an AC97 control */
  1184. if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
  1185. rc = ac97_get_ctrl(dev, ctrl);
  1186. else
  1187. rc = 1;
  1188. /* It were not an AC97 control. Sends it to the v4l2 dev interface */
  1189. if (rc == 1) {
  1190. if (check_subdev_ctrl(dev, ctrl->id))
  1191. return -EINVAL;
  1192. v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
  1193. rc = 0;
  1194. }
  1195. return rc;
  1196. }
  1197. static int vidioc_s_ctrl(struct file *file, void *priv,
  1198. struct v4l2_control *ctrl)
  1199. {
  1200. struct em28xx_fh *fh = priv;
  1201. struct em28xx *dev = fh->dev;
  1202. int rc;
  1203. rc = check_dev(dev);
  1204. if (rc < 0)
  1205. return rc;
  1206. /* Set an AC97 control */
  1207. if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
  1208. rc = ac97_set_ctrl(dev, ctrl);
  1209. else
  1210. rc = 1;
  1211. /* It isn't an AC97 control. Sends it to the v4l2 dev interface */
  1212. if (rc == 1) {
  1213. rc = check_subdev_ctrl(dev, ctrl->id);
  1214. if (!rc)
  1215. v4l2_device_call_all(&dev->v4l2_dev, 0,
  1216. core, s_ctrl, ctrl);
  1217. /*
  1218. * In the case of non-AC97 volume controls, we still need
  1219. * to do some setups at em28xx, in order to mute/unmute
  1220. * and to adjust audio volume. However, the value ranges
  1221. * should be checked by the corresponding V4L subdriver.
  1222. */
  1223. switch (ctrl->id) {
  1224. case V4L2_CID_AUDIO_MUTE:
  1225. dev->mute = ctrl->value;
  1226. rc = em28xx_audio_analog_set(dev);
  1227. break;
  1228. case V4L2_CID_AUDIO_VOLUME:
  1229. dev->volume = ctrl->value;
  1230. rc = em28xx_audio_analog_set(dev);
  1231. }
  1232. }
  1233. return (rc < 0) ? rc : 0;
  1234. }
  1235. static int vidioc_g_tuner(struct file *file, void *priv,
  1236. struct v4l2_tuner *t)
  1237. {
  1238. struct em28xx_fh *fh = priv;
  1239. struct em28xx *dev = fh->dev;
  1240. int rc;
  1241. rc = check_dev(dev);
  1242. if (rc < 0)
  1243. return rc;
  1244. if (0 != t->index)
  1245. return -EINVAL;
  1246. strcpy(t->name, "Tuner");
  1247. t->type = V4L2_TUNER_ANALOG_TV;
  1248. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
  1249. return 0;
  1250. }
  1251. static int vidioc_s_tuner(struct file *file, void *priv,
  1252. struct v4l2_tuner *t)
  1253. {
  1254. struct em28xx_fh *fh = priv;
  1255. struct em28xx *dev = fh->dev;
  1256. int rc;
  1257. rc = check_dev(dev);
  1258. if (rc < 0)
  1259. return rc;
  1260. if (0 != t->index)
  1261. return -EINVAL;
  1262. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
  1263. return 0;
  1264. }
  1265. static int vidioc_g_frequency(struct file *file, void *priv,
  1266. struct v4l2_frequency *f)
  1267. {
  1268. struct em28xx_fh *fh = priv;
  1269. struct em28xx *dev = fh->dev;
  1270. f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1271. f->frequency = dev->ctl_freq;
  1272. return 0;
  1273. }
  1274. static int vidioc_s_frequency(struct file *file, void *priv,
  1275. struct v4l2_frequency *f)
  1276. {
  1277. struct em28xx_fh *fh = priv;
  1278. struct em28xx *dev = fh->dev;
  1279. int rc;
  1280. rc = check_dev(dev);
  1281. if (rc < 0)
  1282. return rc;
  1283. if (0 != f->tuner)
  1284. return -EINVAL;
  1285. if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
  1286. return -EINVAL;
  1287. if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
  1288. return -EINVAL;
  1289. dev->ctl_freq = f->frequency;
  1290. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
  1291. return 0;
  1292. }
  1293. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1294. static int em28xx_reg_len(int reg)
  1295. {
  1296. switch (reg) {
  1297. case EM28XX_R40_AC97LSB:
  1298. case EM28XX_R30_HSCALELOW:
  1299. case EM28XX_R32_VSCALELOW:
  1300. return 2;
  1301. default:
  1302. return 1;
  1303. }
  1304. }
  1305. static int vidioc_g_chip_ident(struct file *file, void *priv,
  1306. struct v4l2_dbg_chip_ident *chip)
  1307. {
  1308. struct em28xx_fh *fh = priv;
  1309. struct em28xx *dev = fh->dev;
  1310. chip->ident = V4L2_IDENT_NONE;
  1311. chip->revision = 0;
  1312. v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
  1313. return 0;
  1314. }
  1315. static int vidioc_g_register(struct file *file, void *priv,
  1316. struct v4l2_dbg_register *reg)
  1317. {
  1318. struct em28xx_fh *fh = priv;
  1319. struct em28xx *dev = fh->dev;
  1320. int ret;
  1321. switch (reg->match.type) {
  1322. case V4L2_CHIP_MATCH_AC97:
  1323. ret = em28xx_read_ac97(dev, reg->reg);
  1324. if (ret < 0)
  1325. return ret;
  1326. reg->val = ret;
  1327. reg->size = 1;
  1328. return 0;
  1329. case V4L2_CHIP_MATCH_I2C_DRIVER:
  1330. v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
  1331. return 0;
  1332. case V4L2_CHIP_MATCH_I2C_ADDR:
  1333. /* TODO: is this correct? */
  1334. v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
  1335. return 0;
  1336. default:
  1337. if (!v4l2_chip_match_host(&reg->match))
  1338. return -EINVAL;
  1339. }
  1340. /* Match host */
  1341. reg->size = em28xx_reg_len(reg->reg);
  1342. if (reg->size == 1) {
  1343. ret = em28xx_read_reg(dev, reg->reg);
  1344. if (ret < 0)
  1345. return ret;
  1346. reg->val = ret;
  1347. } else {
  1348. __le16 val = 0;
  1349. ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
  1350. reg->reg, (char *)&val, 2);
  1351. if (ret < 0)
  1352. return ret;
  1353. reg->val = le16_to_cpu(val);
  1354. }
  1355. return 0;
  1356. }
  1357. static int vidioc_s_register(struct file *file, void *priv,
  1358. struct v4l2_dbg_register *reg)
  1359. {
  1360. struct em28xx_fh *fh = priv;
  1361. struct em28xx *dev = fh->dev;
  1362. __le16 buf;
  1363. switch (reg->match.type) {
  1364. case V4L2_CHIP_MATCH_AC97:
  1365. return em28xx_write_ac97(dev, reg->reg, reg->val);
  1366. case V4L2_CHIP_MATCH_I2C_DRIVER:
  1367. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
  1368. return 0;
  1369. case V4L2_CHIP_MATCH_I2C_ADDR:
  1370. /* TODO: is this correct? */
  1371. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
  1372. return 0;
  1373. default:
  1374. if (!v4l2_chip_match_host(&reg->match))
  1375. return -EINVAL;
  1376. }
  1377. /* Match host */
  1378. buf = cpu_to_le16(reg->val);
  1379. return em28xx_write_regs(dev, reg->reg, (char *)&buf,
  1380. em28xx_reg_len(reg->reg));
  1381. }
  1382. #endif
  1383. static int vidioc_cropcap(struct file *file, void *priv,
  1384. struct v4l2_cropcap *cc)
  1385. {
  1386. struct em28xx_fh *fh = priv;
  1387. struct em28xx *dev = fh->dev;
  1388. if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1389. return -EINVAL;
  1390. cc->bounds.left = 0;
  1391. cc->bounds.top = 0;
  1392. cc->bounds.width = dev->width;
  1393. cc->bounds.height = dev->height;
  1394. cc->defrect = cc->bounds;
  1395. cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
  1396. cc->pixelaspect.denominator = 59;
  1397. return 0;
  1398. }
  1399. static int vidioc_streamon(struct file *file, void *priv,
  1400. enum v4l2_buf_type type)
  1401. {
  1402. struct em28xx_fh *fh = priv;
  1403. struct em28xx *dev = fh->dev;
  1404. int rc = -EINVAL;
  1405. rc = check_dev(dev);
  1406. if (rc < 0)
  1407. return rc;
  1408. if (unlikely(type != fh->type))
  1409. return -EINVAL;
  1410. em28xx_videodbg("vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
  1411. fh, type, fh->resources, dev->resources);
  1412. if (unlikely(!res_get(fh, get_ressource(fh))))
  1413. return -EBUSY;
  1414. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1415. rc = videobuf_streamon(&fh->vb_vidq);
  1416. else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
  1417. rc = videobuf_streamon(&fh->vb_vbiq);
  1418. return rc;
  1419. }
  1420. static int vidioc_streamoff(struct file *file, void *priv,
  1421. enum v4l2_buf_type type)
  1422. {
  1423. struct em28xx_fh *fh = priv;
  1424. struct em28xx *dev = fh->dev;
  1425. int rc;
  1426. rc = check_dev(dev);
  1427. if (rc < 0)
  1428. return rc;
  1429. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  1430. fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)
  1431. return -EINVAL;
  1432. if (type != fh->type)
  1433. return -EINVAL;
  1434. em28xx_videodbg("vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
  1435. fh, type, fh->resources, dev->resources);
  1436. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  1437. if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
  1438. videobuf_streamoff(&fh->vb_vidq);
  1439. res_free(fh, EM28XX_RESOURCE_VIDEO);
  1440. }
  1441. } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
  1442. if (res_check(fh, EM28XX_RESOURCE_VBI)) {
  1443. videobuf_streamoff(&fh->vb_vbiq);
  1444. res_free(fh, EM28XX_RESOURCE_VBI);
  1445. }
  1446. }
  1447. return 0;
  1448. }
  1449. static int vidioc_querycap(struct file *file, void *priv,
  1450. struct v4l2_capability *cap)
  1451. {
  1452. struct em28xx_fh *fh = priv;
  1453. struct em28xx *dev = fh->dev;
  1454. strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
  1455. strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
  1456. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  1457. cap->capabilities =
  1458. V4L2_CAP_SLICED_VBI_CAPTURE |
  1459. V4L2_CAP_VIDEO_CAPTURE |
  1460. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  1461. if (dev->vbi_dev)
  1462. cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
  1463. if (dev->audio_mode.has_audio)
  1464. cap->capabilities |= V4L2_CAP_AUDIO;
  1465. if (dev->tuner_type != TUNER_ABSENT)
  1466. cap->capabilities |= V4L2_CAP_TUNER;
  1467. return 0;
  1468. }
  1469. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  1470. struct v4l2_fmtdesc *f)
  1471. {
  1472. if (unlikely(f->index >= ARRAY_SIZE(format)))
  1473. return -EINVAL;
  1474. strlcpy(f->description, format[f->index].name, sizeof(f->description));
  1475. f->pixelformat = format[f->index].fourcc;
  1476. return 0;
  1477. }
  1478. /* Sliced VBI ioctls */
  1479. static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
  1480. struct v4l2_format *f)
  1481. {
  1482. struct em28xx_fh *fh = priv;
  1483. struct em28xx *dev = fh->dev;
  1484. int rc;
  1485. rc = check_dev(dev);
  1486. if (rc < 0)
  1487. return rc;
  1488. f->fmt.sliced.service_set = 0;
  1489. v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
  1490. if (f->fmt.sliced.service_set == 0)
  1491. rc = -EINVAL;
  1492. return rc;
  1493. }
  1494. static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
  1495. struct v4l2_format *f)
  1496. {
  1497. struct em28xx_fh *fh = priv;
  1498. struct em28xx *dev = fh->dev;
  1499. int rc;
  1500. rc = check_dev(dev);
  1501. if (rc < 0)
  1502. return rc;
  1503. v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
  1504. if (f->fmt.sliced.service_set == 0)
  1505. return -EINVAL;
  1506. return 0;
  1507. }
  1508. /* RAW VBI ioctls */
  1509. static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
  1510. struct v4l2_format *format)
  1511. {
  1512. struct em28xx_fh *fh = priv;
  1513. struct em28xx *dev = fh->dev;
  1514. format->fmt.vbi.samples_per_line = dev->vbi_width;
  1515. format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  1516. format->fmt.vbi.offset = 0;
  1517. format->fmt.vbi.flags = 0;
  1518. format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
  1519. format->fmt.vbi.count[0] = dev->vbi_height;
  1520. format->fmt.vbi.count[1] = dev->vbi_height;
  1521. /* Varies by video standard (NTSC, PAL, etc.) */
  1522. if (dev->norm & V4L2_STD_525_60) {
  1523. /* NTSC */
  1524. format->fmt.vbi.start[0] = 10;
  1525. format->fmt.vbi.start[1] = 273;
  1526. } else if (dev->norm & V4L2_STD_625_50) {
  1527. /* PAL */
  1528. format->fmt.vbi.start[0] = 6;
  1529. format->fmt.vbi.start[1] = 318;
  1530. }
  1531. return 0;
  1532. }
  1533. static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
  1534. struct v4l2_format *format)
  1535. {
  1536. struct em28xx_fh *fh = priv;
  1537. struct em28xx *dev = fh->dev;
  1538. format->fmt.vbi.samples_per_line = dev->vbi_width;
  1539. format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  1540. format->fmt.vbi.offset = 0;
  1541. format->fmt.vbi.flags = 0;
  1542. format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
  1543. format->fmt.vbi.count[0] = dev->vbi_height;
  1544. format->fmt.vbi.count[1] = dev->vbi_height;
  1545. /* Varies by video standard (NTSC, PAL, etc.) */
  1546. if (dev->norm & V4L2_STD_525_60) {
  1547. /* NTSC */
  1548. format->fmt.vbi.start[0] = 10;
  1549. format->fmt.vbi.start[1] = 273;
  1550. } else if (dev->norm & V4L2_STD_625_50) {
  1551. /* PAL */
  1552. format->fmt.vbi.start[0] = 6;
  1553. format->fmt.vbi.start[1] = 318;
  1554. }
  1555. return 0;
  1556. }
  1557. static int vidioc_reqbufs(struct file *file, void *priv,
  1558. struct v4l2_requestbuffers *rb)
  1559. {
  1560. struct em28xx_fh *fh = priv;
  1561. struct em28xx *dev = fh->dev;
  1562. int rc;
  1563. rc = check_dev(dev);
  1564. if (rc < 0)
  1565. return rc;
  1566. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1567. return videobuf_reqbufs(&fh->vb_vidq, rb);
  1568. else
  1569. return videobuf_reqbufs(&fh->vb_vbiq, rb);
  1570. }
  1571. static int vidioc_querybuf(struct file *file, void *priv,
  1572. struct v4l2_buffer *b)
  1573. {
  1574. struct em28xx_fh *fh = priv;
  1575. struct em28xx *dev = fh->dev;
  1576. int rc;
  1577. rc = check_dev(dev);
  1578. if (rc < 0)
  1579. return rc;
  1580. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1581. return videobuf_querybuf(&fh->vb_vidq, b);
  1582. else {
  1583. /* FIXME: I'm not sure yet whether this is a bug in zvbi or
  1584. the videobuf framework, but we probably shouldn't be
  1585. returning a buffer larger than that which was asked for.
  1586. At a minimum, it causes a crash in zvbi since it does
  1587. a memcpy based on the source buffer length */
  1588. int result = videobuf_querybuf(&fh->vb_vbiq, b);
  1589. b->length = dev->vbi_width * dev->vbi_height * 2;
  1590. return result;
  1591. }
  1592. }
  1593. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1594. {
  1595. struct em28xx_fh *fh = priv;
  1596. struct em28xx *dev = fh->dev;
  1597. int rc;
  1598. rc = check_dev(dev);
  1599. if (rc < 0)
  1600. return rc;
  1601. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1602. return videobuf_qbuf(&fh->vb_vidq, b);
  1603. else
  1604. return videobuf_qbuf(&fh->vb_vbiq, b);
  1605. }
  1606. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  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_dqbuf(&fh->vb_vidq, b, file->f_flags &
  1616. O_NONBLOCK);
  1617. else
  1618. return videobuf_dqbuf(&fh->vb_vbiq, b, file->f_flags &
  1619. O_NONBLOCK);
  1620. }
  1621. /* ----------------------------------------------------------- */
  1622. /* RADIO ESPECIFIC IOCTLS */
  1623. /* ----------------------------------------------------------- */
  1624. static int radio_querycap(struct file *file, void *priv,
  1625. struct v4l2_capability *cap)
  1626. {
  1627. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1628. strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
  1629. strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
  1630. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  1631. cap->capabilities = V4L2_CAP_TUNER;
  1632. return 0;
  1633. }
  1634. static int radio_g_tuner(struct file *file, void *priv,
  1635. struct v4l2_tuner *t)
  1636. {
  1637. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1638. if (unlikely(t->index > 0))
  1639. return -EINVAL;
  1640. strcpy(t->name, "Radio");
  1641. t->type = V4L2_TUNER_RADIO;
  1642. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
  1643. return 0;
  1644. }
  1645. static int radio_enum_input(struct file *file, void *priv,
  1646. struct v4l2_input *i)
  1647. {
  1648. if (i->index != 0)
  1649. return -EINVAL;
  1650. strcpy(i->name, "Radio");
  1651. i->type = V4L2_INPUT_TYPE_TUNER;
  1652. return 0;
  1653. }
  1654. static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  1655. {
  1656. if (unlikely(a->index))
  1657. return -EINVAL;
  1658. strcpy(a->name, "Radio");
  1659. return 0;
  1660. }
  1661. static int radio_s_tuner(struct file *file, void *priv,
  1662. struct v4l2_tuner *t)
  1663. {
  1664. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1665. if (0 != t->index)
  1666. return -EINVAL;
  1667. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
  1668. return 0;
  1669. }
  1670. static int radio_s_audio(struct file *file, void *fh,
  1671. struct v4l2_audio *a)
  1672. {
  1673. return 0;
  1674. }
  1675. static int radio_s_input(struct file *file, void *fh, unsigned int i)
  1676. {
  1677. return 0;
  1678. }
  1679. static int radio_queryctrl(struct file *file, void *priv,
  1680. struct v4l2_queryctrl *qc)
  1681. {
  1682. int i;
  1683. if (qc->id < V4L2_CID_BASE ||
  1684. qc->id >= V4L2_CID_LASTP1)
  1685. return -EINVAL;
  1686. for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
  1687. if (qc->id && qc->id == ac97_qctrl[i].id) {
  1688. memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
  1689. return 0;
  1690. }
  1691. }
  1692. return -EINVAL;
  1693. }
  1694. /*
  1695. * em28xx_v4l2_open()
  1696. * inits the device and starts isoc transfer
  1697. */
  1698. static int em28xx_v4l2_open(struct file *filp)
  1699. {
  1700. int errCode = 0, radio = 0;
  1701. struct video_device *vdev = video_devdata(filp);
  1702. struct em28xx *dev = video_drvdata(filp);
  1703. enum v4l2_buf_type fh_type = 0;
  1704. struct em28xx_fh *fh;
  1705. enum v4l2_field field;
  1706. switch (vdev->vfl_type) {
  1707. case VFL_TYPE_GRABBER:
  1708. fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1709. break;
  1710. case VFL_TYPE_VBI:
  1711. fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
  1712. break;
  1713. case VFL_TYPE_RADIO:
  1714. radio = 1;
  1715. break;
  1716. }
  1717. em28xx_videodbg("open dev=%s type=%s users=%d\n",
  1718. video_device_node_name(vdev), v4l2_type_names[fh_type],
  1719. dev->users);
  1720. fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
  1721. if (!fh) {
  1722. em28xx_errdev("em28xx-video.c: Out of memory?!\n");
  1723. return -ENOMEM;
  1724. }
  1725. fh->dev = dev;
  1726. fh->radio = radio;
  1727. fh->type = fh_type;
  1728. filp->private_data = fh;
  1729. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
  1730. em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
  1731. em28xx_set_alternate(dev);
  1732. em28xx_resolution_set(dev);
  1733. /* Needed, since GPIO might have disabled power of
  1734. some i2c device
  1735. */
  1736. em28xx_wake_i2c(dev);
  1737. }
  1738. if (fh->radio) {
  1739. em28xx_videodbg("video_open: setting radio device\n");
  1740. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
  1741. }
  1742. dev->users++;
  1743. if (dev->progressive)
  1744. field = V4L2_FIELD_NONE;
  1745. else
  1746. field = V4L2_FIELD_INTERLACED;
  1747. videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
  1748. NULL, &dev->slock,
  1749. V4L2_BUF_TYPE_VIDEO_CAPTURE, field,
  1750. sizeof(struct em28xx_buffer), fh, &dev->lock);
  1751. videobuf_queue_vmalloc_init(&fh->vb_vbiq, &em28xx_vbi_qops,
  1752. NULL, &dev->slock,
  1753. V4L2_BUF_TYPE_VBI_CAPTURE,
  1754. V4L2_FIELD_SEQ_TB,
  1755. sizeof(struct em28xx_buffer), fh, &dev->lock);
  1756. return errCode;
  1757. }
  1758. /*
  1759. * em28xx_realease_resources()
  1760. * unregisters the v4l2,i2c and usb devices
  1761. * called when the device gets disconected or at module unload
  1762. */
  1763. void em28xx_release_analog_resources(struct em28xx *dev)
  1764. {
  1765. /*FIXME: I2C IR should be disconnected */
  1766. if (dev->radio_dev) {
  1767. if (video_is_registered(dev->radio_dev))
  1768. video_unregister_device(dev->radio_dev);
  1769. else
  1770. video_device_release(dev->radio_dev);
  1771. dev->radio_dev = NULL;
  1772. }
  1773. if (dev->vbi_dev) {
  1774. em28xx_info("V4L2 device %s deregistered\n",
  1775. video_device_node_name(dev->vbi_dev));
  1776. if (video_is_registered(dev->vbi_dev))
  1777. video_unregister_device(dev->vbi_dev);
  1778. else
  1779. video_device_release(dev->vbi_dev);
  1780. dev->vbi_dev = NULL;
  1781. }
  1782. if (dev->vdev) {
  1783. em28xx_info("V4L2 device %s deregistered\n",
  1784. video_device_node_name(dev->vdev));
  1785. if (video_is_registered(dev->vdev))
  1786. video_unregister_device(dev->vdev);
  1787. else
  1788. video_device_release(dev->vdev);
  1789. dev->vdev = NULL;
  1790. }
  1791. }
  1792. /*
  1793. * em28xx_v4l2_close()
  1794. * stops streaming and deallocates all resources allocated by the v4l2
  1795. * calls and ioctls
  1796. */
  1797. static int em28xx_v4l2_close(struct file *filp)
  1798. {
  1799. struct em28xx_fh *fh = filp->private_data;
  1800. struct em28xx *dev = fh->dev;
  1801. int errCode;
  1802. em28xx_videodbg("users=%d\n", dev->users);
  1803. if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
  1804. videobuf_stop(&fh->vb_vidq);
  1805. res_free(fh, EM28XX_RESOURCE_VIDEO);
  1806. }
  1807. if (res_check(fh, EM28XX_RESOURCE_VBI)) {
  1808. videobuf_stop(&fh->vb_vbiq);
  1809. res_free(fh, EM28XX_RESOURCE_VBI);
  1810. }
  1811. if (dev->users == 1) {
  1812. /* the device is already disconnect,
  1813. free the remaining resources */
  1814. if (dev->state & DEV_DISCONNECTED) {
  1815. em28xx_release_resources(dev);
  1816. kfree(dev);
  1817. return 0;
  1818. }
  1819. /* Save some power by putting tuner to sleep */
  1820. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
  1821. /* do this before setting alternate! */
  1822. em28xx_uninit_isoc(dev);
  1823. em28xx_set_mode(dev, EM28XX_SUSPEND);
  1824. /* set alternate 0 */
  1825. dev->alt = 0;
  1826. em28xx_videodbg("setting alternate 0\n");
  1827. errCode = usb_set_interface(dev->udev, 0, 0);
  1828. if (errCode < 0) {
  1829. em28xx_errdev("cannot change alternate number to "
  1830. "0 (error=%i)\n", errCode);
  1831. }
  1832. }
  1833. videobuf_mmap_free(&fh->vb_vidq);
  1834. videobuf_mmap_free(&fh->vb_vbiq);
  1835. kfree(fh);
  1836. dev->users--;
  1837. wake_up_interruptible_nr(&dev->open, 1);
  1838. return 0;
  1839. }
  1840. /*
  1841. * em28xx_v4l2_read()
  1842. * will allocate buffers when called for the first time
  1843. */
  1844. static ssize_t
  1845. em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
  1846. loff_t *pos)
  1847. {
  1848. struct em28xx_fh *fh = filp->private_data;
  1849. struct em28xx *dev = fh->dev;
  1850. int rc;
  1851. rc = check_dev(dev);
  1852. if (rc < 0)
  1853. return rc;
  1854. /* FIXME: read() is not prepared to allow changing the video
  1855. resolution while streaming. Seems a bug at em28xx_set_fmt
  1856. */
  1857. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  1858. if (res_locked(dev, EM28XX_RESOURCE_VIDEO))
  1859. return -EBUSY;
  1860. return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
  1861. filp->f_flags & O_NONBLOCK);
  1862. }
  1863. if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
  1864. if (!res_get(fh, EM28XX_RESOURCE_VBI))
  1865. return -EBUSY;
  1866. return videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
  1867. filp->f_flags & O_NONBLOCK);
  1868. }
  1869. return 0;
  1870. }
  1871. /*
  1872. * em28xx_v4l2_poll()
  1873. * will allocate buffers when called for the first time
  1874. */
  1875. static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
  1876. {
  1877. struct em28xx_fh *fh = filp->private_data;
  1878. struct em28xx *dev = fh->dev;
  1879. int rc;
  1880. rc = check_dev(dev);
  1881. if (rc < 0)
  1882. return rc;
  1883. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  1884. if (!res_get(fh, EM28XX_RESOURCE_VIDEO))
  1885. return POLLERR;
  1886. return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
  1887. } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
  1888. if (!res_get(fh, EM28XX_RESOURCE_VBI))
  1889. return POLLERR;
  1890. return videobuf_poll_stream(filp, &fh->vb_vbiq, wait);
  1891. } else {
  1892. return POLLERR;
  1893. }
  1894. }
  1895. /*
  1896. * em28xx_v4l2_mmap()
  1897. */
  1898. static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
  1899. {
  1900. struct em28xx_fh *fh = filp->private_data;
  1901. struct em28xx *dev = fh->dev;
  1902. int rc;
  1903. rc = check_dev(dev);
  1904. if (rc < 0)
  1905. return rc;
  1906. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1907. rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
  1908. else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
  1909. rc = videobuf_mmap_mapper(&fh->vb_vbiq, vma);
  1910. em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
  1911. (unsigned long)vma->vm_start,
  1912. (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
  1913. rc);
  1914. return rc;
  1915. }
  1916. static const struct v4l2_file_operations em28xx_v4l_fops = {
  1917. .owner = THIS_MODULE,
  1918. .open = em28xx_v4l2_open,
  1919. .release = em28xx_v4l2_close,
  1920. .read = em28xx_v4l2_read,
  1921. .poll = em28xx_v4l2_poll,
  1922. .mmap = em28xx_v4l2_mmap,
  1923. .unlocked_ioctl = video_ioctl2,
  1924. };
  1925. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1926. .vidioc_querycap = vidioc_querycap,
  1927. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1928. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1929. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1930. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1931. .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
  1932. .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
  1933. .vidioc_g_audio = vidioc_g_audio,
  1934. .vidioc_s_audio = vidioc_s_audio,
  1935. .vidioc_cropcap = vidioc_cropcap,
  1936. .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
  1937. .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
  1938. .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
  1939. .vidioc_reqbufs = vidioc_reqbufs,
  1940. .vidioc_querybuf = vidioc_querybuf,
  1941. .vidioc_qbuf = vidioc_qbuf,
  1942. .vidioc_dqbuf = vidioc_dqbuf,
  1943. .vidioc_g_std = vidioc_g_std,
  1944. .vidioc_s_std = vidioc_s_std,
  1945. .vidioc_g_parm = vidioc_g_parm,
  1946. .vidioc_s_parm = vidioc_s_parm,
  1947. .vidioc_enum_input = vidioc_enum_input,
  1948. .vidioc_g_input = vidioc_g_input,
  1949. .vidioc_s_input = vidioc_s_input,
  1950. .vidioc_queryctrl = vidioc_queryctrl,
  1951. .vidioc_g_ctrl = vidioc_g_ctrl,
  1952. .vidioc_s_ctrl = vidioc_s_ctrl,
  1953. .vidioc_streamon = vidioc_streamon,
  1954. .vidioc_streamoff = vidioc_streamoff,
  1955. .vidioc_g_tuner = vidioc_g_tuner,
  1956. .vidioc_s_tuner = vidioc_s_tuner,
  1957. .vidioc_g_frequency = vidioc_g_frequency,
  1958. .vidioc_s_frequency = vidioc_s_frequency,
  1959. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1960. .vidioc_g_register = vidioc_g_register,
  1961. .vidioc_s_register = vidioc_s_register,
  1962. .vidioc_g_chip_ident = vidioc_g_chip_ident,
  1963. #endif
  1964. };
  1965. static const struct video_device em28xx_video_template = {
  1966. .fops = &em28xx_v4l_fops,
  1967. .release = video_device_release,
  1968. .ioctl_ops = &video_ioctl_ops,
  1969. .tvnorms = V4L2_STD_ALL,
  1970. .current_norm = V4L2_STD_PAL,
  1971. };
  1972. static const struct v4l2_file_operations radio_fops = {
  1973. .owner = THIS_MODULE,
  1974. .open = em28xx_v4l2_open,
  1975. .release = em28xx_v4l2_close,
  1976. .unlocked_ioctl = video_ioctl2,
  1977. };
  1978. static const struct v4l2_ioctl_ops radio_ioctl_ops = {
  1979. .vidioc_querycap = radio_querycap,
  1980. .vidioc_g_tuner = radio_g_tuner,
  1981. .vidioc_enum_input = radio_enum_input,
  1982. .vidioc_g_audio = radio_g_audio,
  1983. .vidioc_s_tuner = radio_s_tuner,
  1984. .vidioc_s_audio = radio_s_audio,
  1985. .vidioc_s_input = radio_s_input,
  1986. .vidioc_queryctrl = radio_queryctrl,
  1987. .vidioc_g_ctrl = vidioc_g_ctrl,
  1988. .vidioc_s_ctrl = vidioc_s_ctrl,
  1989. .vidioc_g_frequency = vidioc_g_frequency,
  1990. .vidioc_s_frequency = vidioc_s_frequency,
  1991. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1992. .vidioc_g_register = vidioc_g_register,
  1993. .vidioc_s_register = vidioc_s_register,
  1994. #endif
  1995. };
  1996. static struct video_device em28xx_radio_template = {
  1997. .name = "em28xx-radio",
  1998. .fops = &radio_fops,
  1999. .ioctl_ops = &radio_ioctl_ops,
  2000. };
  2001. /******************************** usb interface ******************************/
  2002. static struct video_device *em28xx_vdev_init(struct em28xx *dev,
  2003. const struct video_device *template,
  2004. const char *type_name)
  2005. {
  2006. struct video_device *vfd;
  2007. vfd = video_device_alloc();
  2008. if (NULL == vfd)
  2009. return NULL;
  2010. *vfd = *template;
  2011. vfd->v4l2_dev = &dev->v4l2_dev;
  2012. vfd->release = video_device_release;
  2013. vfd->debug = video_debug;
  2014. vfd->lock = &dev->lock;
  2015. snprintf(vfd->name, sizeof(vfd->name), "%s %s",
  2016. dev->name, type_name);
  2017. video_set_drvdata(vfd, dev);
  2018. return vfd;
  2019. }
  2020. int em28xx_register_analog_devices(struct em28xx *dev)
  2021. {
  2022. u8 val;
  2023. int ret;
  2024. printk(KERN_INFO "%s: v4l2 driver version %s\n",
  2025. dev->name, EM28XX_VERSION);
  2026. /* set default norm */
  2027. dev->norm = em28xx_video_template.current_norm;
  2028. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
  2029. dev->interlaced = EM28XX_INTERLACED_DEFAULT;
  2030. dev->ctl_input = 0;
  2031. /* Analog specific initialization */
  2032. dev->format = &format[0];
  2033. em28xx_set_video_format(dev, format[0].fourcc,
  2034. norm_maxw(dev), norm_maxh(dev));
  2035. video_mux(dev, dev->ctl_input);
  2036. /* Audio defaults */
  2037. dev->mute = 1;
  2038. dev->volume = 0x1f;
  2039. /* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
  2040. val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
  2041. em28xx_write_reg(dev, EM28XX_R0F_XCLK,
  2042. (EM28XX_XCLK_AUDIO_UNMUTE | val));
  2043. em28xx_set_outfmt(dev);
  2044. em28xx_colorlevels_set_default(dev);
  2045. em28xx_compression_disable(dev);
  2046. /* allocate and fill video video_device struct */
  2047. dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
  2048. if (!dev->vdev) {
  2049. em28xx_errdev("cannot allocate video_device.\n");
  2050. return -ENODEV;
  2051. }
  2052. /* register v4l2 video video_device */
  2053. ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
  2054. video_nr[dev->devno]);
  2055. if (ret) {
  2056. em28xx_errdev("unable to register video device (error=%i).\n",
  2057. ret);
  2058. return ret;
  2059. }
  2060. /* Allocate and fill vbi video_device struct */
  2061. if (em28xx_vbi_supported(dev) == 1) {
  2062. dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
  2063. "vbi");
  2064. /* register v4l2 vbi video_device */
  2065. ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
  2066. vbi_nr[dev->devno]);
  2067. if (ret < 0) {
  2068. em28xx_errdev("unable to register vbi device\n");
  2069. return ret;
  2070. }
  2071. }
  2072. if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
  2073. dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
  2074. "radio");
  2075. if (!dev->radio_dev) {
  2076. em28xx_errdev("cannot allocate video_device.\n");
  2077. return -ENODEV;
  2078. }
  2079. ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
  2080. radio_nr[dev->devno]);
  2081. if (ret < 0) {
  2082. em28xx_errdev("can't register radio device\n");
  2083. return ret;
  2084. }
  2085. em28xx_info("Registered radio device as %s\n",
  2086. video_device_node_name(dev->radio_dev));
  2087. }
  2088. em28xx_info("V4L2 video device registered as %s\n",
  2089. video_device_node_name(dev->vdev));
  2090. if (dev->vbi_dev)
  2091. em28xx_info("V4L2 VBI device registered as %s\n",
  2092. video_device_node_name(dev->vbi_dev));
  2093. return 0;
  2094. }