em28xx-video.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529
  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)
  704. {
  705. struct em28xx *dev = fh->dev;
  706. int rc = 0;
  707. /* This instance already has stream_on */
  708. if (fh->stream_on)
  709. return rc;
  710. if (dev->stream_on)
  711. return -EBUSY;
  712. dev->stream_on = 1;
  713. fh->stream_on = 1;
  714. return rc;
  715. }
  716. static int res_check(struct em28xx_fh *fh)
  717. {
  718. return fh->stream_on;
  719. }
  720. static void res_free(struct em28xx_fh *fh)
  721. {
  722. struct em28xx *dev = fh->dev;
  723. fh->stream_on = 0;
  724. dev->stream_on = 0;
  725. }
  726. /*
  727. * ac97_queryctrl()
  728. * return the ac97 supported controls
  729. */
  730. static int ac97_queryctrl(struct v4l2_queryctrl *qc)
  731. {
  732. int i;
  733. for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
  734. if (qc->id && qc->id == ac97_qctrl[i].id) {
  735. memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
  736. return 0;
  737. }
  738. }
  739. /* Control is not ac97 related */
  740. return 1;
  741. }
  742. /*
  743. * ac97_get_ctrl()
  744. * return the current values for ac97 mute and volume
  745. */
  746. static int ac97_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
  747. {
  748. switch (ctrl->id) {
  749. case V4L2_CID_AUDIO_MUTE:
  750. ctrl->value = dev->mute;
  751. return 0;
  752. case V4L2_CID_AUDIO_VOLUME:
  753. ctrl->value = dev->volume;
  754. return 0;
  755. default:
  756. /* Control is not ac97 related */
  757. return 1;
  758. }
  759. }
  760. /*
  761. * ac97_set_ctrl()
  762. * set values for ac97 mute and volume
  763. */
  764. static int ac97_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
  765. {
  766. int i;
  767. for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++)
  768. if (ctrl->id == ac97_qctrl[i].id)
  769. goto handle;
  770. /* Announce that hasn't handle it */
  771. return 1;
  772. handle:
  773. if (ctrl->value < ac97_qctrl[i].minimum ||
  774. ctrl->value > ac97_qctrl[i].maximum)
  775. return -ERANGE;
  776. switch (ctrl->id) {
  777. case V4L2_CID_AUDIO_MUTE:
  778. dev->mute = ctrl->value;
  779. break;
  780. case V4L2_CID_AUDIO_VOLUME:
  781. dev->volume = ctrl->value;
  782. break;
  783. }
  784. return em28xx_audio_analog_set(dev);
  785. }
  786. static int check_dev(struct em28xx *dev)
  787. {
  788. if (dev->state & DEV_DISCONNECTED) {
  789. em28xx_errdev("v4l2 ioctl: device not present\n");
  790. return -ENODEV;
  791. }
  792. if (dev->state & DEV_MISCONFIGURED) {
  793. em28xx_errdev("v4l2 ioctl: device is misconfigured; "
  794. "close and open it again\n");
  795. return -EIO;
  796. }
  797. return 0;
  798. }
  799. static void get_scale(struct em28xx *dev,
  800. unsigned int width, unsigned int height,
  801. unsigned int *hscale, unsigned int *vscale)
  802. {
  803. unsigned int maxw = norm_maxw(dev);
  804. unsigned int maxh = norm_maxh(dev);
  805. *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
  806. if (*hscale >= 0x4000)
  807. *hscale = 0x3fff;
  808. *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
  809. if (*vscale >= 0x4000)
  810. *vscale = 0x3fff;
  811. }
  812. /* ------------------------------------------------------------------
  813. IOCTL vidioc handling
  814. ------------------------------------------------------------------*/
  815. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  816. struct v4l2_format *f)
  817. {
  818. struct em28xx_fh *fh = priv;
  819. struct em28xx *dev = fh->dev;
  820. mutex_lock(&dev->lock);
  821. f->fmt.pix.width = dev->width;
  822. f->fmt.pix.height = dev->height;
  823. f->fmt.pix.pixelformat = dev->format->fourcc;
  824. f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
  825. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
  826. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  827. /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
  828. if (dev->progressive)
  829. f->fmt.pix.field = V4L2_FIELD_NONE;
  830. else
  831. f->fmt.pix.field = dev->interlaced ?
  832. V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
  833. mutex_unlock(&dev->lock);
  834. return 0;
  835. }
  836. static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
  837. {
  838. unsigned int i;
  839. for (i = 0; i < ARRAY_SIZE(format); i++)
  840. if (format[i].fourcc == fourcc)
  841. return &format[i];
  842. return NULL;
  843. }
  844. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  845. struct v4l2_format *f)
  846. {
  847. struct em28xx_fh *fh = priv;
  848. struct em28xx *dev = fh->dev;
  849. unsigned int width = f->fmt.pix.width;
  850. unsigned int height = f->fmt.pix.height;
  851. unsigned int maxw = norm_maxw(dev);
  852. unsigned int maxh = norm_maxh(dev);
  853. unsigned int hscale, vscale;
  854. struct em28xx_fmt *fmt;
  855. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  856. if (!fmt) {
  857. em28xx_videodbg("Fourcc format (%08x) invalid.\n",
  858. f->fmt.pix.pixelformat);
  859. return -EINVAL;
  860. }
  861. if (dev->board.is_em2800) {
  862. /* the em2800 can only scale down to 50% */
  863. height = height > (3 * maxh / 4) ? maxh : maxh / 2;
  864. width = width > (3 * maxw / 4) ? maxw : maxw / 2;
  865. /* According to empiatech support the MaxPacketSize is too small
  866. * to support framesizes larger than 640x480 @ 30 fps or 640x576
  867. * @ 25 fps. As this would cut of a part of the image we prefer
  868. * 360x576 or 360x480 for now */
  869. if (width == maxw && height == maxh)
  870. width /= 2;
  871. } else {
  872. /* width must even because of the YUYV format
  873. height must be even because of interlacing */
  874. v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh, 1, 0);
  875. }
  876. get_scale(dev, width, height, &hscale, &vscale);
  877. width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
  878. height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
  879. f->fmt.pix.width = width;
  880. f->fmt.pix.height = height;
  881. f->fmt.pix.pixelformat = fmt->fourcc;
  882. f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
  883. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
  884. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  885. if (dev->progressive)
  886. f->fmt.pix.field = V4L2_FIELD_NONE;
  887. else
  888. f->fmt.pix.field = dev->interlaced ?
  889. V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
  890. return 0;
  891. }
  892. static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
  893. unsigned width, unsigned height)
  894. {
  895. struct em28xx_fmt *fmt;
  896. fmt = format_by_fourcc(fourcc);
  897. if (!fmt)
  898. return -EINVAL;
  899. dev->format = fmt;
  900. dev->width = width;
  901. dev->height = height;
  902. /* set new image size */
  903. get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
  904. em28xx_set_alternate(dev);
  905. em28xx_resolution_set(dev);
  906. return 0;
  907. }
  908. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  909. struct v4l2_format *f)
  910. {
  911. struct em28xx_fh *fh = priv;
  912. struct em28xx *dev = fh->dev;
  913. int rc;
  914. rc = check_dev(dev);
  915. if (rc < 0)
  916. return rc;
  917. mutex_lock(&dev->lock);
  918. vidioc_try_fmt_vid_cap(file, priv, f);
  919. if (videobuf_queue_is_busy(&fh->vb_vidq)) {
  920. em28xx_errdev("%s queue busy\n", __func__);
  921. rc = -EBUSY;
  922. goto out;
  923. }
  924. if (dev->stream_on && !fh->stream_on) {
  925. em28xx_errdev("%s device in use by another fh\n", __func__);
  926. rc = -EBUSY;
  927. goto out;
  928. }
  929. rc = em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
  930. f->fmt.pix.width, f->fmt.pix.height);
  931. out:
  932. mutex_unlock(&dev->lock);
  933. return rc;
  934. }
  935. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
  936. {
  937. struct em28xx_fh *fh = priv;
  938. struct em28xx *dev = fh->dev;
  939. struct v4l2_format f;
  940. int rc;
  941. rc = check_dev(dev);
  942. if (rc < 0)
  943. return rc;
  944. mutex_lock(&dev->lock);
  945. dev->norm = *norm;
  946. /* Adjusts width/height, if needed */
  947. f.fmt.pix.width = dev->width;
  948. f.fmt.pix.height = dev->height;
  949. vidioc_try_fmt_vid_cap(file, priv, &f);
  950. /* set new image size */
  951. dev->width = f.fmt.pix.width;
  952. dev->height = f.fmt.pix.height;
  953. get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
  954. em28xx_resolution_set(dev);
  955. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
  956. mutex_unlock(&dev->lock);
  957. return 0;
  958. }
  959. static int vidioc_g_parm(struct file *file, void *priv,
  960. struct v4l2_streamparm *p)
  961. {
  962. struct em28xx_fh *fh = priv;
  963. struct em28xx *dev = fh->dev;
  964. int rc = 0;
  965. if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  966. return -EINVAL;
  967. if (dev->board.is_webcam)
  968. rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0,
  969. video, g_parm, p);
  970. else
  971. v4l2_video_std_frame_period(dev->norm,
  972. &p->parm.capture.timeperframe);
  973. return rc;
  974. }
  975. static int vidioc_s_parm(struct file *file, void *priv,
  976. struct v4l2_streamparm *p)
  977. {
  978. struct em28xx_fh *fh = priv;
  979. struct em28xx *dev = fh->dev;
  980. if (!dev->board.is_webcam)
  981. return -EINVAL;
  982. if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  983. return -EINVAL;
  984. return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p);
  985. }
  986. static const char *iname[] = {
  987. [EM28XX_VMUX_COMPOSITE1] = "Composite1",
  988. [EM28XX_VMUX_COMPOSITE2] = "Composite2",
  989. [EM28XX_VMUX_COMPOSITE3] = "Composite3",
  990. [EM28XX_VMUX_COMPOSITE4] = "Composite4",
  991. [EM28XX_VMUX_SVIDEO] = "S-Video",
  992. [EM28XX_VMUX_TELEVISION] = "Television",
  993. [EM28XX_VMUX_CABLE] = "Cable TV",
  994. [EM28XX_VMUX_DVB] = "DVB",
  995. [EM28XX_VMUX_DEBUG] = "for debug only",
  996. };
  997. static int vidioc_enum_input(struct file *file, void *priv,
  998. struct v4l2_input *i)
  999. {
  1000. struct em28xx_fh *fh = priv;
  1001. struct em28xx *dev = fh->dev;
  1002. unsigned int n;
  1003. n = i->index;
  1004. if (n >= MAX_EM28XX_INPUT)
  1005. return -EINVAL;
  1006. if (0 == INPUT(n)->type)
  1007. return -EINVAL;
  1008. i->index = n;
  1009. i->type = V4L2_INPUT_TYPE_CAMERA;
  1010. strcpy(i->name, iname[INPUT(n)->type]);
  1011. if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
  1012. (EM28XX_VMUX_CABLE == INPUT(n)->type))
  1013. i->type = V4L2_INPUT_TYPE_TUNER;
  1014. i->std = dev->vdev->tvnorms;
  1015. return 0;
  1016. }
  1017. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  1018. {
  1019. struct em28xx_fh *fh = priv;
  1020. struct em28xx *dev = fh->dev;
  1021. *i = dev->ctl_input;
  1022. return 0;
  1023. }
  1024. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  1025. {
  1026. struct em28xx_fh *fh = priv;
  1027. struct em28xx *dev = fh->dev;
  1028. int rc;
  1029. rc = check_dev(dev);
  1030. if (rc < 0)
  1031. return rc;
  1032. if (i >= MAX_EM28XX_INPUT)
  1033. return -EINVAL;
  1034. if (0 == INPUT(i)->type)
  1035. return -EINVAL;
  1036. dev->ctl_input = i;
  1037. mutex_lock(&dev->lock);
  1038. video_mux(dev, dev->ctl_input);
  1039. mutex_unlock(&dev->lock);
  1040. return 0;
  1041. }
  1042. static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  1043. {
  1044. struct em28xx_fh *fh = priv;
  1045. struct em28xx *dev = fh->dev;
  1046. if (!dev->audio_mode.has_audio)
  1047. return -EINVAL;
  1048. switch (a->index) {
  1049. case EM28XX_AMUX_VIDEO:
  1050. strcpy(a->name, "Television");
  1051. break;
  1052. case EM28XX_AMUX_LINE_IN:
  1053. strcpy(a->name, "Line In");
  1054. break;
  1055. case EM28XX_AMUX_VIDEO2:
  1056. strcpy(a->name, "Television alt");
  1057. break;
  1058. case EM28XX_AMUX_PHONE:
  1059. strcpy(a->name, "Phone");
  1060. break;
  1061. case EM28XX_AMUX_MIC:
  1062. strcpy(a->name, "Mic");
  1063. break;
  1064. case EM28XX_AMUX_CD:
  1065. strcpy(a->name, "CD");
  1066. break;
  1067. case EM28XX_AMUX_AUX:
  1068. strcpy(a->name, "Aux");
  1069. break;
  1070. case EM28XX_AMUX_PCM_OUT:
  1071. strcpy(a->name, "PCM");
  1072. break;
  1073. default:
  1074. return -EINVAL;
  1075. }
  1076. a->index = dev->ctl_ainput;
  1077. a->capability = V4L2_AUDCAP_STEREO;
  1078. return 0;
  1079. }
  1080. static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
  1081. {
  1082. struct em28xx_fh *fh = priv;
  1083. struct em28xx *dev = fh->dev;
  1084. if (!dev->audio_mode.has_audio)
  1085. return -EINVAL;
  1086. if (a->index >= MAX_EM28XX_INPUT)
  1087. return -EINVAL;
  1088. if (0 == INPUT(a->index)->type)
  1089. return -EINVAL;
  1090. mutex_lock(&dev->lock);
  1091. dev->ctl_ainput = INPUT(a->index)->amux;
  1092. dev->ctl_aoutput = INPUT(a->index)->aout;
  1093. if (!dev->ctl_aoutput)
  1094. dev->ctl_aoutput = EM28XX_AOUT_MASTER;
  1095. mutex_unlock(&dev->lock);
  1096. return 0;
  1097. }
  1098. static int vidioc_queryctrl(struct file *file, void *priv,
  1099. struct v4l2_queryctrl *qc)
  1100. {
  1101. struct em28xx_fh *fh = priv;
  1102. struct em28xx *dev = fh->dev;
  1103. int id = qc->id;
  1104. int rc;
  1105. rc = check_dev(dev);
  1106. if (rc < 0)
  1107. return rc;
  1108. memset(qc, 0, sizeof(*qc));
  1109. qc->id = id;
  1110. /* enumberate AC97 controls */
  1111. if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
  1112. rc = ac97_queryctrl(qc);
  1113. if (!rc)
  1114. return 0;
  1115. }
  1116. /* enumberate V4L2 device controls */
  1117. mutex_lock(&dev->lock);
  1118. v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
  1119. mutex_unlock(&dev->lock);
  1120. if (qc->type)
  1121. return 0;
  1122. else
  1123. return -EINVAL;
  1124. }
  1125. static int vidioc_g_ctrl(struct file *file, void *priv,
  1126. struct v4l2_control *ctrl)
  1127. {
  1128. struct em28xx_fh *fh = priv;
  1129. struct em28xx *dev = fh->dev;
  1130. int rc;
  1131. rc = check_dev(dev);
  1132. if (rc < 0)
  1133. return rc;
  1134. rc = 0;
  1135. mutex_lock(&dev->lock);
  1136. /* Set an AC97 control */
  1137. if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
  1138. rc = ac97_get_ctrl(dev, ctrl);
  1139. else
  1140. rc = 1;
  1141. /* It were not an AC97 control. Sends it to the v4l2 dev interface */
  1142. if (rc == 1) {
  1143. v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
  1144. rc = 0;
  1145. }
  1146. mutex_unlock(&dev->lock);
  1147. return rc;
  1148. }
  1149. static int vidioc_s_ctrl(struct file *file, void *priv,
  1150. struct v4l2_control *ctrl)
  1151. {
  1152. struct em28xx_fh *fh = priv;
  1153. struct em28xx *dev = fh->dev;
  1154. int rc;
  1155. rc = check_dev(dev);
  1156. if (rc < 0)
  1157. return rc;
  1158. mutex_lock(&dev->lock);
  1159. /* Set an AC97 control */
  1160. if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
  1161. rc = ac97_set_ctrl(dev, ctrl);
  1162. else
  1163. rc = 1;
  1164. /* It isn't an AC97 control. Sends it to the v4l2 dev interface */
  1165. if (rc == 1) {
  1166. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_ctrl, ctrl);
  1167. /*
  1168. * In the case of non-AC97 volume controls, we still need
  1169. * to do some setups at em28xx, in order to mute/unmute
  1170. * and to adjust audio volume. However, the value ranges
  1171. * should be checked by the corresponding V4L subdriver.
  1172. */
  1173. switch (ctrl->id) {
  1174. case V4L2_CID_AUDIO_MUTE:
  1175. dev->mute = ctrl->value;
  1176. rc = em28xx_audio_analog_set(dev);
  1177. break;
  1178. case V4L2_CID_AUDIO_VOLUME:
  1179. dev->volume = ctrl->value;
  1180. rc = em28xx_audio_analog_set(dev);
  1181. }
  1182. }
  1183. mutex_unlock(&dev->lock);
  1184. return rc;
  1185. }
  1186. static int vidioc_g_tuner(struct file *file, void *priv,
  1187. struct v4l2_tuner *t)
  1188. {
  1189. struct em28xx_fh *fh = priv;
  1190. struct em28xx *dev = fh->dev;
  1191. int rc;
  1192. rc = check_dev(dev);
  1193. if (rc < 0)
  1194. return rc;
  1195. if (0 != t->index)
  1196. return -EINVAL;
  1197. strcpy(t->name, "Tuner");
  1198. mutex_lock(&dev->lock);
  1199. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
  1200. mutex_unlock(&dev->lock);
  1201. return 0;
  1202. }
  1203. static int vidioc_s_tuner(struct file *file, void *priv,
  1204. struct v4l2_tuner *t)
  1205. {
  1206. struct em28xx_fh *fh = priv;
  1207. struct em28xx *dev = fh->dev;
  1208. int rc;
  1209. rc = check_dev(dev);
  1210. if (rc < 0)
  1211. return rc;
  1212. if (0 != t->index)
  1213. return -EINVAL;
  1214. mutex_lock(&dev->lock);
  1215. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
  1216. mutex_unlock(&dev->lock);
  1217. return 0;
  1218. }
  1219. static int vidioc_g_frequency(struct file *file, void *priv,
  1220. struct v4l2_frequency *f)
  1221. {
  1222. struct em28xx_fh *fh = priv;
  1223. struct em28xx *dev = fh->dev;
  1224. mutex_lock(&dev->lock);
  1225. f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1226. f->frequency = dev->ctl_freq;
  1227. mutex_unlock(&dev->lock);
  1228. return 0;
  1229. }
  1230. static int vidioc_s_frequency(struct file *file, void *priv,
  1231. struct v4l2_frequency *f)
  1232. {
  1233. struct em28xx_fh *fh = priv;
  1234. struct em28xx *dev = fh->dev;
  1235. int rc;
  1236. rc = check_dev(dev);
  1237. if (rc < 0)
  1238. return rc;
  1239. if (0 != f->tuner)
  1240. return -EINVAL;
  1241. if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
  1242. return -EINVAL;
  1243. if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
  1244. return -EINVAL;
  1245. mutex_lock(&dev->lock);
  1246. dev->ctl_freq = f->frequency;
  1247. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
  1248. mutex_unlock(&dev->lock);
  1249. return 0;
  1250. }
  1251. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1252. static int em28xx_reg_len(int reg)
  1253. {
  1254. switch (reg) {
  1255. case EM28XX_R40_AC97LSB:
  1256. case EM28XX_R30_HSCALELOW:
  1257. case EM28XX_R32_VSCALELOW:
  1258. return 2;
  1259. default:
  1260. return 1;
  1261. }
  1262. }
  1263. static int vidioc_g_chip_ident(struct file *file, void *priv,
  1264. struct v4l2_dbg_chip_ident *chip)
  1265. {
  1266. struct em28xx_fh *fh = priv;
  1267. struct em28xx *dev = fh->dev;
  1268. chip->ident = V4L2_IDENT_NONE;
  1269. chip->revision = 0;
  1270. v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
  1271. return 0;
  1272. }
  1273. static int vidioc_g_register(struct file *file, void *priv,
  1274. struct v4l2_dbg_register *reg)
  1275. {
  1276. struct em28xx_fh *fh = priv;
  1277. struct em28xx *dev = fh->dev;
  1278. int ret;
  1279. switch (reg->match.type) {
  1280. case V4L2_CHIP_MATCH_AC97:
  1281. mutex_lock(&dev->lock);
  1282. ret = em28xx_read_ac97(dev, reg->reg);
  1283. mutex_unlock(&dev->lock);
  1284. if (ret < 0)
  1285. return ret;
  1286. reg->val = ret;
  1287. reg->size = 1;
  1288. return 0;
  1289. case V4L2_CHIP_MATCH_I2C_DRIVER:
  1290. v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
  1291. return 0;
  1292. case V4L2_CHIP_MATCH_I2C_ADDR:
  1293. /* TODO: is this correct? */
  1294. v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
  1295. return 0;
  1296. default:
  1297. if (!v4l2_chip_match_host(&reg->match))
  1298. return -EINVAL;
  1299. }
  1300. /* Match host */
  1301. reg->size = em28xx_reg_len(reg->reg);
  1302. if (reg->size == 1) {
  1303. mutex_lock(&dev->lock);
  1304. ret = em28xx_read_reg(dev, reg->reg);
  1305. mutex_unlock(&dev->lock);
  1306. if (ret < 0)
  1307. return ret;
  1308. reg->val = ret;
  1309. } else {
  1310. __le16 val = 0;
  1311. mutex_lock(&dev->lock);
  1312. ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
  1313. reg->reg, (char *)&val, 2);
  1314. mutex_unlock(&dev->lock);
  1315. if (ret < 0)
  1316. return ret;
  1317. reg->val = le16_to_cpu(val);
  1318. }
  1319. return 0;
  1320. }
  1321. static int vidioc_s_register(struct file *file, void *priv,
  1322. struct v4l2_dbg_register *reg)
  1323. {
  1324. struct em28xx_fh *fh = priv;
  1325. struct em28xx *dev = fh->dev;
  1326. __le16 buf;
  1327. int rc;
  1328. switch (reg->match.type) {
  1329. case V4L2_CHIP_MATCH_AC97:
  1330. mutex_lock(&dev->lock);
  1331. rc = em28xx_write_ac97(dev, reg->reg, reg->val);
  1332. mutex_unlock(&dev->lock);
  1333. return rc;
  1334. case V4L2_CHIP_MATCH_I2C_DRIVER:
  1335. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
  1336. return 0;
  1337. case V4L2_CHIP_MATCH_I2C_ADDR:
  1338. /* TODO: is this correct? */
  1339. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
  1340. return 0;
  1341. default:
  1342. if (!v4l2_chip_match_host(&reg->match))
  1343. return -EINVAL;
  1344. }
  1345. /* Match host */
  1346. buf = cpu_to_le16(reg->val);
  1347. mutex_lock(&dev->lock);
  1348. rc = em28xx_write_regs(dev, reg->reg, (char *)&buf,
  1349. em28xx_reg_len(reg->reg));
  1350. mutex_unlock(&dev->lock);
  1351. return rc;
  1352. }
  1353. #endif
  1354. static int vidioc_cropcap(struct file *file, void *priv,
  1355. struct v4l2_cropcap *cc)
  1356. {
  1357. struct em28xx_fh *fh = priv;
  1358. struct em28xx *dev = fh->dev;
  1359. if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1360. return -EINVAL;
  1361. cc->bounds.left = 0;
  1362. cc->bounds.top = 0;
  1363. cc->bounds.width = dev->width;
  1364. cc->bounds.height = dev->height;
  1365. cc->defrect = cc->bounds;
  1366. cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
  1367. cc->pixelaspect.denominator = 59;
  1368. return 0;
  1369. }
  1370. static int vidioc_streamon(struct file *file, void *priv,
  1371. enum v4l2_buf_type type)
  1372. {
  1373. struct em28xx_fh *fh = priv;
  1374. struct em28xx *dev = fh->dev;
  1375. int rc;
  1376. rc = check_dev(dev);
  1377. if (rc < 0)
  1378. return rc;
  1379. mutex_lock(&dev->lock);
  1380. rc = res_get(fh);
  1381. if (likely(rc >= 0)) {
  1382. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1383. rc = videobuf_streamon(&fh->vb_vidq);
  1384. else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
  1385. rc = videobuf_streamon(&fh->vb_vbiq);
  1386. }
  1387. mutex_unlock(&dev->lock);
  1388. return rc;
  1389. }
  1390. static int vidioc_streamoff(struct file *file, void *priv,
  1391. enum v4l2_buf_type type)
  1392. {
  1393. struct em28xx_fh *fh = priv;
  1394. struct em28xx *dev = fh->dev;
  1395. int rc;
  1396. rc = check_dev(dev);
  1397. if (rc < 0)
  1398. return rc;
  1399. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  1400. fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)
  1401. return -EINVAL;
  1402. if (type != fh->type)
  1403. return -EINVAL;
  1404. mutex_lock(&dev->lock);
  1405. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1406. videobuf_streamoff(&fh->vb_vidq);
  1407. else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
  1408. videobuf_streamoff(&fh->vb_vbiq);
  1409. res_free(fh);
  1410. mutex_unlock(&dev->lock);
  1411. return 0;
  1412. }
  1413. static int vidioc_querycap(struct file *file, void *priv,
  1414. struct v4l2_capability *cap)
  1415. {
  1416. struct em28xx_fh *fh = priv;
  1417. struct em28xx *dev = fh->dev;
  1418. strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
  1419. strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
  1420. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  1421. cap->version = EM28XX_VERSION_CODE;
  1422. cap->capabilities =
  1423. V4L2_CAP_VBI_CAPTURE |
  1424. V4L2_CAP_SLICED_VBI_CAPTURE |
  1425. V4L2_CAP_VIDEO_CAPTURE |
  1426. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  1427. if (dev->audio_mode.has_audio)
  1428. cap->capabilities |= V4L2_CAP_AUDIO;
  1429. if (dev->tuner_type != TUNER_ABSENT)
  1430. cap->capabilities |= V4L2_CAP_TUNER;
  1431. return 0;
  1432. }
  1433. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  1434. struct v4l2_fmtdesc *f)
  1435. {
  1436. if (unlikely(f->index >= ARRAY_SIZE(format)))
  1437. return -EINVAL;
  1438. strlcpy(f->description, format[f->index].name, sizeof(f->description));
  1439. f->pixelformat = format[f->index].fourcc;
  1440. return 0;
  1441. }
  1442. /* Sliced VBI ioctls */
  1443. static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
  1444. struct v4l2_format *f)
  1445. {
  1446. struct em28xx_fh *fh = priv;
  1447. struct em28xx *dev = fh->dev;
  1448. int rc;
  1449. rc = check_dev(dev);
  1450. if (rc < 0)
  1451. return rc;
  1452. mutex_lock(&dev->lock);
  1453. f->fmt.sliced.service_set = 0;
  1454. v4l2_device_call_all(&dev->v4l2_dev, 0, video, g_fmt, f);
  1455. if (f->fmt.sliced.service_set == 0)
  1456. rc = -EINVAL;
  1457. mutex_unlock(&dev->lock);
  1458. return rc;
  1459. }
  1460. static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
  1461. struct v4l2_format *f)
  1462. {
  1463. struct em28xx_fh *fh = priv;
  1464. struct em28xx *dev = fh->dev;
  1465. int rc;
  1466. rc = check_dev(dev);
  1467. if (rc < 0)
  1468. return rc;
  1469. mutex_lock(&dev->lock);
  1470. v4l2_device_call_all(&dev->v4l2_dev, 0, video, g_fmt, f);
  1471. mutex_unlock(&dev->lock);
  1472. if (f->fmt.sliced.service_set == 0)
  1473. return -EINVAL;
  1474. return 0;
  1475. }
  1476. /* RAW VBI ioctls */
  1477. static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
  1478. struct v4l2_format *format)
  1479. {
  1480. format->fmt.vbi.samples_per_line = 720;
  1481. format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  1482. format->fmt.vbi.offset = 0;
  1483. format->fmt.vbi.flags = 0;
  1484. /* Varies by video standard (NTSC, PAL, etc.) */
  1485. /* FIXME: hard-coded for NTSC support */
  1486. format->fmt.vbi.sampling_rate = 6750000 * 4 / 2; /* FIXME: ??? */
  1487. format->fmt.vbi.count[0] = 12;
  1488. format->fmt.vbi.count[1] = 12;
  1489. format->fmt.vbi.start[0] = 10;
  1490. format->fmt.vbi.start[1] = 273;
  1491. return 0;
  1492. }
  1493. static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
  1494. struct v4l2_format *format)
  1495. {
  1496. format->fmt.vbi.samples_per_line = 720;
  1497. format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  1498. format->fmt.vbi.offset = 0;
  1499. format->fmt.vbi.flags = 0;
  1500. /* Varies by video standard (NTSC, PAL, etc.) */
  1501. /* FIXME: hard-coded for NTSC support */
  1502. format->fmt.vbi.sampling_rate = 6750000 * 4 / 2; /* FIXME: ??? */
  1503. format->fmt.vbi.count[0] = 12;
  1504. format->fmt.vbi.count[1] = 12;
  1505. format->fmt.vbi.start[0] = 10;
  1506. format->fmt.vbi.start[1] = 273;
  1507. return 0;
  1508. }
  1509. static int vidioc_reqbufs(struct file *file, void *priv,
  1510. struct v4l2_requestbuffers *rb)
  1511. {
  1512. struct em28xx_fh *fh = priv;
  1513. struct em28xx *dev = fh->dev;
  1514. int rc;
  1515. rc = check_dev(dev);
  1516. if (rc < 0)
  1517. return rc;
  1518. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1519. return videobuf_reqbufs(&fh->vb_vidq, rb);
  1520. else
  1521. return videobuf_reqbufs(&fh->vb_vbiq, rb);
  1522. }
  1523. static int vidioc_querybuf(struct file *file, void *priv,
  1524. struct v4l2_buffer *b)
  1525. {
  1526. struct em28xx_fh *fh = priv;
  1527. struct em28xx *dev = fh->dev;
  1528. int rc;
  1529. rc = check_dev(dev);
  1530. if (rc < 0)
  1531. return rc;
  1532. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1533. return videobuf_querybuf(&fh->vb_vidq, b);
  1534. else {
  1535. /* FIXME: I'm not sure yet whether this is a bug in zvbi or
  1536. the videobuf framework, but we probably shouldn't be
  1537. returning a buffer larger than that which was asked for.
  1538. At a minimum, it causes a crash in zvbi since it does
  1539. a memcpy based on the source buffer length */
  1540. int result = videobuf_querybuf(&fh->vb_vbiq, b);
  1541. b->length = 17280;
  1542. return result;
  1543. }
  1544. }
  1545. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1546. {
  1547. struct em28xx_fh *fh = priv;
  1548. struct em28xx *dev = fh->dev;
  1549. int rc;
  1550. rc = check_dev(dev);
  1551. if (rc < 0)
  1552. return rc;
  1553. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1554. return videobuf_qbuf(&fh->vb_vidq, b);
  1555. else {
  1556. return videobuf_qbuf(&fh->vb_vbiq, b);
  1557. }
  1558. }
  1559. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1560. {
  1561. struct em28xx_fh *fh = priv;
  1562. struct em28xx *dev = fh->dev;
  1563. int rc;
  1564. rc = check_dev(dev);
  1565. if (rc < 0)
  1566. return rc;
  1567. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1568. return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags &
  1569. O_NONBLOCK);
  1570. else
  1571. return videobuf_dqbuf(&fh->vb_vbiq, b, file->f_flags &
  1572. O_NONBLOCK);
  1573. }
  1574. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1575. static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
  1576. {
  1577. struct em28xx_fh *fh = priv;
  1578. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1579. return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
  1580. else
  1581. return videobuf_cgmbuf(&fh->vb_vbiq, mbuf, 8);
  1582. }
  1583. #endif
  1584. /* ----------------------------------------------------------- */
  1585. /* RADIO ESPECIFIC IOCTLS */
  1586. /* ----------------------------------------------------------- */
  1587. static int radio_querycap(struct file *file, void *priv,
  1588. struct v4l2_capability *cap)
  1589. {
  1590. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1591. strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
  1592. strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
  1593. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  1594. cap->version = EM28XX_VERSION_CODE;
  1595. cap->capabilities = V4L2_CAP_TUNER;
  1596. return 0;
  1597. }
  1598. static int radio_g_tuner(struct file *file, void *priv,
  1599. struct v4l2_tuner *t)
  1600. {
  1601. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1602. if (unlikely(t->index > 0))
  1603. return -EINVAL;
  1604. strcpy(t->name, "Radio");
  1605. t->type = V4L2_TUNER_RADIO;
  1606. mutex_lock(&dev->lock);
  1607. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
  1608. mutex_unlock(&dev->lock);
  1609. return 0;
  1610. }
  1611. static int radio_enum_input(struct file *file, void *priv,
  1612. struct v4l2_input *i)
  1613. {
  1614. if (i->index != 0)
  1615. return -EINVAL;
  1616. strcpy(i->name, "Radio");
  1617. i->type = V4L2_INPUT_TYPE_TUNER;
  1618. return 0;
  1619. }
  1620. static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  1621. {
  1622. if (unlikely(a->index))
  1623. return -EINVAL;
  1624. strcpy(a->name, "Radio");
  1625. return 0;
  1626. }
  1627. static int radio_s_tuner(struct file *file, void *priv,
  1628. struct v4l2_tuner *t)
  1629. {
  1630. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1631. if (0 != t->index)
  1632. return -EINVAL;
  1633. mutex_lock(&dev->lock);
  1634. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
  1635. mutex_unlock(&dev->lock);
  1636. return 0;
  1637. }
  1638. static int radio_s_audio(struct file *file, void *fh,
  1639. struct v4l2_audio *a)
  1640. {
  1641. return 0;
  1642. }
  1643. static int radio_s_input(struct file *file, void *fh, unsigned int i)
  1644. {
  1645. return 0;
  1646. }
  1647. static int radio_queryctrl(struct file *file, void *priv,
  1648. struct v4l2_queryctrl *qc)
  1649. {
  1650. int i;
  1651. if (qc->id < V4L2_CID_BASE ||
  1652. qc->id >= V4L2_CID_LASTP1)
  1653. return -EINVAL;
  1654. for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
  1655. if (qc->id && qc->id == ac97_qctrl[i].id) {
  1656. memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
  1657. return 0;
  1658. }
  1659. }
  1660. return -EINVAL;
  1661. }
  1662. /*
  1663. * em28xx_v4l2_open()
  1664. * inits the device and starts isoc transfer
  1665. */
  1666. static int em28xx_v4l2_open(struct file *filp)
  1667. {
  1668. int minor = video_devdata(filp)->minor;
  1669. int errCode = 0, radio;
  1670. struct em28xx *dev;
  1671. enum v4l2_buf_type fh_type;
  1672. struct em28xx_fh *fh;
  1673. enum v4l2_field field;
  1674. dev = em28xx_get_device(minor, &fh_type, &radio);
  1675. if (NULL == dev)
  1676. return -ENODEV;
  1677. mutex_lock(&dev->lock);
  1678. em28xx_videodbg("open minor=%d type=%s users=%d\n",
  1679. minor, v4l2_type_names[fh_type], dev->users);
  1680. fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
  1681. if (!fh) {
  1682. em28xx_errdev("em28xx-video.c: Out of memory?!\n");
  1683. mutex_unlock(&dev->lock);
  1684. return -ENOMEM;
  1685. }
  1686. fh->dev = dev;
  1687. fh->radio = radio;
  1688. fh->type = fh_type;
  1689. filp->private_data = fh;
  1690. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
  1691. em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
  1692. em28xx_set_alternate(dev);
  1693. em28xx_resolution_set(dev);
  1694. /* Needed, since GPIO might have disabled power of
  1695. some i2c device
  1696. */
  1697. em28xx_wake_i2c(dev);
  1698. }
  1699. if (fh->radio) {
  1700. em28xx_videodbg("video_open: setting radio device\n");
  1701. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
  1702. }
  1703. dev->users++;
  1704. if (dev->progressive)
  1705. field = V4L2_FIELD_NONE;
  1706. else
  1707. field = V4L2_FIELD_INTERLACED;
  1708. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1709. videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
  1710. NULL, &dev->slock, fh->type, field,
  1711. sizeof(struct em28xx_buffer), fh);
  1712. if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
  1713. videobuf_queue_vmalloc_init(&fh->vb_vbiq, &em28xx_vbi_qops,
  1714. NULL, &dev->slock,
  1715. V4L2_BUF_TYPE_VBI_CAPTURE,
  1716. V4L2_FIELD_SEQ_TB,
  1717. sizeof(struct em28xx_buffer), fh);
  1718. mutex_unlock(&dev->lock);
  1719. return errCode;
  1720. }
  1721. /*
  1722. * em28xx_realease_resources()
  1723. * unregisters the v4l2,i2c and usb devices
  1724. * called when the device gets disconected or at module unload
  1725. */
  1726. void em28xx_release_analog_resources(struct em28xx *dev)
  1727. {
  1728. /*FIXME: I2C IR should be disconnected */
  1729. if (dev->radio_dev) {
  1730. if (-1 != dev->radio_dev->minor)
  1731. video_unregister_device(dev->radio_dev);
  1732. else
  1733. video_device_release(dev->radio_dev);
  1734. dev->radio_dev = NULL;
  1735. }
  1736. if (dev->vbi_dev) {
  1737. em28xx_info("V4L2 device /dev/vbi%d deregistered\n",
  1738. dev->vbi_dev->num);
  1739. if (-1 != dev->vbi_dev->minor)
  1740. video_unregister_device(dev->vbi_dev);
  1741. else
  1742. video_device_release(dev->vbi_dev);
  1743. dev->vbi_dev = NULL;
  1744. }
  1745. if (dev->vdev) {
  1746. em28xx_info("V4L2 device /dev/video%d deregistered\n",
  1747. dev->vdev->num);
  1748. if (-1 != dev->vdev->minor)
  1749. video_unregister_device(dev->vdev);
  1750. else
  1751. video_device_release(dev->vdev);
  1752. dev->vdev = NULL;
  1753. }
  1754. }
  1755. /*
  1756. * em28xx_v4l2_close()
  1757. * stops streaming and deallocates all resources allocated by the v4l2
  1758. * calls and ioctls
  1759. */
  1760. static int em28xx_v4l2_close(struct file *filp)
  1761. {
  1762. struct em28xx_fh *fh = filp->private_data;
  1763. struct em28xx *dev = fh->dev;
  1764. int errCode;
  1765. em28xx_videodbg("users=%d\n", dev->users);
  1766. mutex_lock(&dev->lock);
  1767. if (res_check(fh))
  1768. res_free(fh);
  1769. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 1) {
  1770. videobuf_stop(&fh->vb_vidq);
  1771. videobuf_mmap_free(&fh->vb_vidq);
  1772. /* the device is already disconnect,
  1773. free the remaining resources */
  1774. if (dev->state & DEV_DISCONNECTED) {
  1775. em28xx_release_resources(dev);
  1776. mutex_unlock(&dev->lock);
  1777. kfree(dev);
  1778. return 0;
  1779. }
  1780. /* Save some power by putting tuner to sleep */
  1781. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_standby);
  1782. /* do this before setting alternate! */
  1783. em28xx_uninit_isoc(dev);
  1784. em28xx_set_mode(dev, EM28XX_SUSPEND);
  1785. /* set alternate 0 */
  1786. dev->alt = 0;
  1787. em28xx_videodbg("setting alternate 0\n");
  1788. errCode = usb_set_interface(dev->udev, 0, 0);
  1789. if (errCode < 0) {
  1790. em28xx_errdev("cannot change alternate number to "
  1791. "0 (error=%i)\n", errCode);
  1792. }
  1793. }
  1794. if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
  1795. videobuf_stop(&fh->vb_vbiq);
  1796. videobuf_mmap_free(&fh->vb_vbiq);
  1797. }
  1798. kfree(fh);
  1799. dev->users--;
  1800. wake_up_interruptible_nr(&dev->open, 1);
  1801. mutex_unlock(&dev->lock);
  1802. return 0;
  1803. }
  1804. /*
  1805. * em28xx_v4l2_read()
  1806. * will allocate buffers when called for the first time
  1807. */
  1808. static ssize_t
  1809. em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
  1810. loff_t *pos)
  1811. {
  1812. struct em28xx_fh *fh = filp->private_data;
  1813. struct em28xx *dev = fh->dev;
  1814. int rc;
  1815. rc = check_dev(dev);
  1816. if (rc < 0)
  1817. return rc;
  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. mutex_lock(&dev->lock);
  1823. rc = res_get(fh);
  1824. mutex_unlock(&dev->lock);
  1825. if (unlikely(rc < 0))
  1826. return rc;
  1827. return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
  1828. filp->f_flags & O_NONBLOCK);
  1829. }
  1830. if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
  1831. mutex_lock(&dev->lock);
  1832. rc = res_get(fh);
  1833. mutex_unlock(&dev->lock);
  1834. return videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
  1835. filp->f_flags & O_NONBLOCK);
  1836. }
  1837. return 0;
  1838. }
  1839. /*
  1840. * em28xx_v4l2_poll()
  1841. * will allocate buffers when called for the first time
  1842. */
  1843. static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
  1844. {
  1845. struct em28xx_fh *fh = filp->private_data;
  1846. struct em28xx *dev = fh->dev;
  1847. int rc;
  1848. rc = check_dev(dev);
  1849. if (rc < 0)
  1850. return rc;
  1851. mutex_lock(&dev->lock);
  1852. rc = res_get(fh);
  1853. mutex_unlock(&dev->lock);
  1854. if (unlikely(rc < 0))
  1855. return POLLERR;
  1856. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1857. return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
  1858. else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
  1859. return videobuf_poll_stream(filp, &fh->vb_vbiq, wait);
  1860. else
  1861. return POLLERR;
  1862. }
  1863. /*
  1864. * em28xx_v4l2_mmap()
  1865. */
  1866. static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
  1867. {
  1868. struct em28xx_fh *fh = filp->private_data;
  1869. struct em28xx *dev = fh->dev;
  1870. int rc;
  1871. rc = check_dev(dev);
  1872. if (rc < 0)
  1873. return rc;
  1874. mutex_lock(&dev->lock);
  1875. rc = res_get(fh);
  1876. mutex_unlock(&dev->lock);
  1877. if (unlikely(rc < 0))
  1878. return rc;
  1879. rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
  1880. em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
  1881. (unsigned long)vma->vm_start,
  1882. (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
  1883. rc);
  1884. return rc;
  1885. }
  1886. static const struct v4l2_file_operations em28xx_v4l_fops = {
  1887. .owner = THIS_MODULE,
  1888. .open = em28xx_v4l2_open,
  1889. .release = em28xx_v4l2_close,
  1890. .read = em28xx_v4l2_read,
  1891. .poll = em28xx_v4l2_poll,
  1892. .mmap = em28xx_v4l2_mmap,
  1893. .ioctl = video_ioctl2,
  1894. };
  1895. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1896. .vidioc_querycap = vidioc_querycap,
  1897. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1898. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1899. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1900. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1901. .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
  1902. .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
  1903. .vidioc_g_audio = vidioc_g_audio,
  1904. .vidioc_s_audio = vidioc_s_audio,
  1905. .vidioc_cropcap = vidioc_cropcap,
  1906. .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
  1907. .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
  1908. .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
  1909. .vidioc_reqbufs = vidioc_reqbufs,
  1910. .vidioc_querybuf = vidioc_querybuf,
  1911. .vidioc_qbuf = vidioc_qbuf,
  1912. .vidioc_dqbuf = vidioc_dqbuf,
  1913. .vidioc_s_std = vidioc_s_std,
  1914. .vidioc_g_parm = vidioc_g_parm,
  1915. .vidioc_s_parm = vidioc_s_parm,
  1916. .vidioc_enum_input = vidioc_enum_input,
  1917. .vidioc_g_input = vidioc_g_input,
  1918. .vidioc_s_input = vidioc_s_input,
  1919. .vidioc_queryctrl = vidioc_queryctrl,
  1920. .vidioc_g_ctrl = vidioc_g_ctrl,
  1921. .vidioc_s_ctrl = vidioc_s_ctrl,
  1922. .vidioc_streamon = vidioc_streamon,
  1923. .vidioc_streamoff = vidioc_streamoff,
  1924. .vidioc_g_tuner = vidioc_g_tuner,
  1925. .vidioc_s_tuner = vidioc_s_tuner,
  1926. .vidioc_g_frequency = vidioc_g_frequency,
  1927. .vidioc_s_frequency = vidioc_s_frequency,
  1928. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1929. .vidioc_g_register = vidioc_g_register,
  1930. .vidioc_s_register = vidioc_s_register,
  1931. .vidioc_g_chip_ident = vidioc_g_chip_ident,
  1932. #endif
  1933. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1934. .vidiocgmbuf = vidiocgmbuf,
  1935. #endif
  1936. };
  1937. static const struct video_device em28xx_video_template = {
  1938. .fops = &em28xx_v4l_fops,
  1939. .release = video_device_release,
  1940. .ioctl_ops = &video_ioctl_ops,
  1941. .minor = -1,
  1942. .tvnorms = V4L2_STD_ALL,
  1943. /* FIXME: we need this to be NTSC for VBI to work - it should
  1944. be moved to a per-board definition */
  1945. .current_norm = V4L2_STD_NTSC,
  1946. };
  1947. static const struct v4l2_file_operations radio_fops = {
  1948. .owner = THIS_MODULE,
  1949. .open = em28xx_v4l2_open,
  1950. .release = em28xx_v4l2_close,
  1951. .ioctl = video_ioctl2,
  1952. };
  1953. static const struct v4l2_ioctl_ops radio_ioctl_ops = {
  1954. .vidioc_querycap = radio_querycap,
  1955. .vidioc_g_tuner = radio_g_tuner,
  1956. .vidioc_enum_input = radio_enum_input,
  1957. .vidioc_g_audio = radio_g_audio,
  1958. .vidioc_s_tuner = radio_s_tuner,
  1959. .vidioc_s_audio = radio_s_audio,
  1960. .vidioc_s_input = radio_s_input,
  1961. .vidioc_queryctrl = radio_queryctrl,
  1962. .vidioc_g_ctrl = vidioc_g_ctrl,
  1963. .vidioc_s_ctrl = vidioc_s_ctrl,
  1964. .vidioc_g_frequency = vidioc_g_frequency,
  1965. .vidioc_s_frequency = vidioc_s_frequency,
  1966. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1967. .vidioc_g_register = vidioc_g_register,
  1968. .vidioc_s_register = vidioc_s_register,
  1969. #endif
  1970. };
  1971. static struct video_device em28xx_radio_template = {
  1972. .name = "em28xx-radio",
  1973. .fops = &radio_fops,
  1974. .ioctl_ops = &radio_ioctl_ops,
  1975. .minor = -1,
  1976. };
  1977. /******************************** usb interface ******************************/
  1978. static struct video_device *em28xx_vdev_init(struct em28xx *dev,
  1979. const struct video_device *template,
  1980. const char *type_name)
  1981. {
  1982. struct video_device *vfd;
  1983. vfd = video_device_alloc();
  1984. if (NULL == vfd)
  1985. return NULL;
  1986. *vfd = *template;
  1987. vfd->minor = -1;
  1988. vfd->v4l2_dev = &dev->v4l2_dev;
  1989. vfd->release = video_device_release;
  1990. vfd->debug = video_debug;
  1991. snprintf(vfd->name, sizeof(vfd->name), "%s %s",
  1992. dev->name, type_name);
  1993. return vfd;
  1994. }
  1995. int em28xx_register_analog_devices(struct em28xx *dev)
  1996. {
  1997. u8 val;
  1998. int ret;
  1999. printk(KERN_INFO "%s: v4l2 driver version %d.%d.%d\n",
  2000. dev->name,
  2001. (EM28XX_VERSION_CODE >> 16) & 0xff,
  2002. (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
  2003. /* set default norm */
  2004. dev->norm = em28xx_video_template.current_norm;
  2005. dev->interlaced = EM28XX_INTERLACED_DEFAULT;
  2006. dev->ctl_input = 0;
  2007. /* Analog specific initialization */
  2008. dev->format = &format[0];
  2009. em28xx_set_video_format(dev, format[0].fourcc,
  2010. norm_maxw(dev), norm_maxh(dev));
  2011. video_mux(dev, dev->ctl_input);
  2012. /* Audio defaults */
  2013. dev->mute = 1;
  2014. dev->volume = 0x1f;
  2015. /* enable vbi capturing */
  2016. /* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
  2017. val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
  2018. em28xx_write_reg(dev, EM28XX_R0F_XCLK,
  2019. (EM28XX_XCLK_AUDIO_UNMUTE | val));
  2020. em28xx_write_reg(dev, EM28XX_R11_VINCTRL, 0x51);
  2021. em28xx_set_outfmt(dev);
  2022. em28xx_colorlevels_set_default(dev);
  2023. em28xx_compression_disable(dev);
  2024. /* allocate and fill video video_device struct */
  2025. dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
  2026. if (!dev->vdev) {
  2027. em28xx_errdev("cannot allocate video_device.\n");
  2028. return -ENODEV;
  2029. }
  2030. /* register v4l2 video video_device */
  2031. ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
  2032. video_nr[dev->devno]);
  2033. if (ret) {
  2034. em28xx_errdev("unable to register video device (error=%i).\n",
  2035. ret);
  2036. return ret;
  2037. }
  2038. /* Allocate and fill vbi video_device struct */
  2039. dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template, "vbi");
  2040. /* register v4l2 vbi video_device */
  2041. ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
  2042. vbi_nr[dev->devno]);
  2043. if (ret < 0) {
  2044. em28xx_errdev("unable to register vbi device\n");
  2045. return ret;
  2046. }
  2047. if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
  2048. dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
  2049. "radio");
  2050. if (!dev->radio_dev) {
  2051. em28xx_errdev("cannot allocate video_device.\n");
  2052. return -ENODEV;
  2053. }
  2054. ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
  2055. radio_nr[dev->devno]);
  2056. if (ret < 0) {
  2057. em28xx_errdev("can't register radio device\n");
  2058. return ret;
  2059. }
  2060. em28xx_info("Registered radio device as /dev/radio%d\n",
  2061. dev->radio_dev->num);
  2062. }
  2063. em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
  2064. dev->vdev->num, dev->vbi_dev->num);
  2065. return 0;
  2066. }