em28xx-video.c 61 KB

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