em28xx-video.c 61 KB

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