em28xx-video.c 61 KB

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