em28xx-video.c 62 KB

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