em28xx-video.c 63 KB

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