em28xx-video.c 61 KB

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