em28xx-video.c 61 KB

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