em28xx-video.c 61 KB

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