em28xx-video.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624
  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.analog_bufs.num_bufs)
  650. urb_init = 1;
  651. if (urb_init) {
  652. if (em28xx_vbi_supported(dev) == 1)
  653. rc = em28xx_init_isoc(dev, EM28XX_ANALOG_MODE,
  654. EM28XX_NUM_PACKETS,
  655. EM28XX_NUM_BUFS,
  656. dev->max_pkt_size,
  657. em28xx_isoc_copy_vbi);
  658. else
  659. rc = em28xx_init_isoc(dev, EM28XX_ANALOG_MODE,
  660. EM28XX_NUM_PACKETS,
  661. EM28XX_NUM_BUFS,
  662. dev->max_pkt_size,
  663. em28xx_isoc_copy);
  664. if (rc < 0)
  665. goto fail;
  666. }
  667. buf->vb.state = VIDEOBUF_PREPARED;
  668. return 0;
  669. fail:
  670. free_buffer(vq, buf);
  671. return rc;
  672. }
  673. static void
  674. buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  675. {
  676. struct em28xx_buffer *buf = container_of(vb,
  677. struct em28xx_buffer,
  678. vb);
  679. struct em28xx_fh *fh = vq->priv_data;
  680. struct em28xx *dev = fh->dev;
  681. struct em28xx_dmaqueue *vidq = &dev->vidq;
  682. buf->vb.state = VIDEOBUF_QUEUED;
  683. list_add_tail(&buf->vb.queue, &vidq->active);
  684. }
  685. static void buffer_release(struct videobuf_queue *vq,
  686. struct videobuf_buffer *vb)
  687. {
  688. struct em28xx_buffer *buf = container_of(vb,
  689. struct em28xx_buffer,
  690. vb);
  691. struct em28xx_fh *fh = vq->priv_data;
  692. struct em28xx *dev = (struct em28xx *)fh->dev;
  693. em28xx_isocdbg("em28xx: called buffer_release\n");
  694. free_buffer(vq, buf);
  695. }
  696. static struct videobuf_queue_ops em28xx_video_qops = {
  697. .buf_setup = buffer_setup,
  698. .buf_prepare = buffer_prepare,
  699. .buf_queue = buffer_queue,
  700. .buf_release = buffer_release,
  701. };
  702. /********************* v4l2 interface **************************************/
  703. static void video_mux(struct em28xx *dev, int index)
  704. {
  705. dev->ctl_input = index;
  706. dev->ctl_ainput = INPUT(index)->amux;
  707. dev->ctl_aoutput = INPUT(index)->aout;
  708. if (!dev->ctl_aoutput)
  709. dev->ctl_aoutput = EM28XX_AOUT_MASTER;
  710. v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
  711. INPUT(index)->vmux, 0, 0);
  712. if (dev->board.has_msp34xx) {
  713. if (dev->i2s_speed) {
  714. v4l2_device_call_all(&dev->v4l2_dev, 0, audio,
  715. s_i2s_clock_freq, dev->i2s_speed);
  716. }
  717. /* Note: this is msp3400 specific */
  718. v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
  719. dev->ctl_ainput, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
  720. }
  721. if (dev->board.adecoder != EM28XX_NOADECODER) {
  722. v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
  723. dev->ctl_ainput, dev->ctl_aoutput, 0);
  724. }
  725. em28xx_audio_analog_set(dev);
  726. }
  727. /* Usage lock check functions */
  728. static int res_get(struct em28xx_fh *fh, unsigned int bit)
  729. {
  730. struct em28xx *dev = fh->dev;
  731. if (fh->resources & bit)
  732. /* have it already allocated */
  733. return 1;
  734. /* is it free? */
  735. if (dev->resources & bit) {
  736. /* no, someone else uses it */
  737. return 0;
  738. }
  739. /* it's free, grab it */
  740. fh->resources |= bit;
  741. dev->resources |= bit;
  742. em28xx_videodbg("res: get %d\n", bit);
  743. return 1;
  744. }
  745. static int res_check(struct em28xx_fh *fh, unsigned int bit)
  746. {
  747. return fh->resources & bit;
  748. }
  749. static int res_locked(struct em28xx *dev, unsigned int bit)
  750. {
  751. return dev->resources & bit;
  752. }
  753. static void res_free(struct em28xx_fh *fh, unsigned int bits)
  754. {
  755. struct em28xx *dev = fh->dev;
  756. BUG_ON((fh->resources & bits) != bits);
  757. fh->resources &= ~bits;
  758. dev->resources &= ~bits;
  759. em28xx_videodbg("res: put %d\n", bits);
  760. }
  761. static int get_ressource(struct em28xx_fh *fh)
  762. {
  763. switch (fh->type) {
  764. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  765. return EM28XX_RESOURCE_VIDEO;
  766. case V4L2_BUF_TYPE_VBI_CAPTURE:
  767. return EM28XX_RESOURCE_VBI;
  768. default:
  769. BUG();
  770. return 0;
  771. }
  772. }
  773. /*
  774. * ac97_queryctrl()
  775. * return the ac97 supported controls
  776. */
  777. static int ac97_queryctrl(struct v4l2_queryctrl *qc)
  778. {
  779. int i;
  780. for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
  781. if (qc->id && qc->id == ac97_qctrl[i].id) {
  782. memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
  783. return 0;
  784. }
  785. }
  786. /* Control is not ac97 related */
  787. return 1;
  788. }
  789. /*
  790. * ac97_get_ctrl()
  791. * return the current values for ac97 mute and volume
  792. */
  793. static int ac97_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
  794. {
  795. switch (ctrl->id) {
  796. case V4L2_CID_AUDIO_MUTE:
  797. ctrl->value = dev->mute;
  798. return 0;
  799. case V4L2_CID_AUDIO_VOLUME:
  800. ctrl->value = dev->volume;
  801. return 0;
  802. default:
  803. /* Control is not ac97 related */
  804. return 1;
  805. }
  806. }
  807. /*
  808. * ac97_set_ctrl()
  809. * set values for ac97 mute and volume
  810. */
  811. static int ac97_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
  812. {
  813. int i;
  814. for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++)
  815. if (ctrl->id == ac97_qctrl[i].id)
  816. goto handle;
  817. /* Announce that hasn't handle it */
  818. return 1;
  819. handle:
  820. if (ctrl->value < ac97_qctrl[i].minimum ||
  821. ctrl->value > ac97_qctrl[i].maximum)
  822. return -ERANGE;
  823. switch (ctrl->id) {
  824. case V4L2_CID_AUDIO_MUTE:
  825. dev->mute = ctrl->value;
  826. break;
  827. case V4L2_CID_AUDIO_VOLUME:
  828. dev->volume = ctrl->value;
  829. break;
  830. }
  831. return em28xx_audio_analog_set(dev);
  832. }
  833. static int check_dev(struct em28xx *dev)
  834. {
  835. if (dev->state & DEV_DISCONNECTED) {
  836. em28xx_errdev("v4l2 ioctl: device not present\n");
  837. return -ENODEV;
  838. }
  839. if (dev->state & DEV_MISCONFIGURED) {
  840. em28xx_errdev("v4l2 ioctl: device is misconfigured; "
  841. "close and open it again\n");
  842. return -EIO;
  843. }
  844. return 0;
  845. }
  846. static void get_scale(struct em28xx *dev,
  847. unsigned int width, unsigned int height,
  848. unsigned int *hscale, unsigned int *vscale)
  849. {
  850. unsigned int maxw = norm_maxw(dev);
  851. unsigned int maxh = norm_maxh(dev);
  852. *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
  853. if (*hscale >= 0x4000)
  854. *hscale = 0x3fff;
  855. *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
  856. if (*vscale >= 0x4000)
  857. *vscale = 0x3fff;
  858. }
  859. /* ------------------------------------------------------------------
  860. IOCTL vidioc handling
  861. ------------------------------------------------------------------*/
  862. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  863. struct v4l2_format *f)
  864. {
  865. struct em28xx_fh *fh = priv;
  866. struct em28xx *dev = fh->dev;
  867. f->fmt.pix.width = dev->width;
  868. f->fmt.pix.height = dev->height;
  869. f->fmt.pix.pixelformat = dev->format->fourcc;
  870. f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
  871. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
  872. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  873. /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
  874. if (dev->progressive)
  875. f->fmt.pix.field = V4L2_FIELD_NONE;
  876. else
  877. f->fmt.pix.field = dev->interlaced ?
  878. V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
  879. return 0;
  880. }
  881. static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
  882. {
  883. unsigned int i;
  884. for (i = 0; i < ARRAY_SIZE(format); i++)
  885. if (format[i].fourcc == fourcc)
  886. return &format[i];
  887. return NULL;
  888. }
  889. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  890. struct v4l2_format *f)
  891. {
  892. struct em28xx_fh *fh = priv;
  893. struct em28xx *dev = fh->dev;
  894. unsigned int width = f->fmt.pix.width;
  895. unsigned int height = f->fmt.pix.height;
  896. unsigned int maxw = norm_maxw(dev);
  897. unsigned int maxh = norm_maxh(dev);
  898. unsigned int hscale, vscale;
  899. struct em28xx_fmt *fmt;
  900. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  901. if (!fmt) {
  902. em28xx_videodbg("Fourcc format (%08x) invalid.\n",
  903. f->fmt.pix.pixelformat);
  904. return -EINVAL;
  905. }
  906. if (dev->board.is_em2800) {
  907. /* the em2800 can only scale down to 50% */
  908. height = height > (3 * maxh / 4) ? maxh : maxh / 2;
  909. width = width > (3 * maxw / 4) ? maxw : maxw / 2;
  910. /* MaxPacketSize for em2800 is too small to capture at full resolution
  911. * use half of maxw as the scaler can only scale to 50% */
  912. if (width == maxw && height == maxh)
  913. width /= 2;
  914. } else {
  915. /* width must even because of the YUYV format
  916. height must be even because of interlacing */
  917. v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh,
  918. 1, 0);
  919. }
  920. get_scale(dev, width, height, &hscale, &vscale);
  921. width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
  922. height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
  923. f->fmt.pix.width = width;
  924. f->fmt.pix.height = height;
  925. f->fmt.pix.pixelformat = fmt->fourcc;
  926. f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
  927. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
  928. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  929. if (dev->progressive)
  930. f->fmt.pix.field = V4L2_FIELD_NONE;
  931. else
  932. f->fmt.pix.field = dev->interlaced ?
  933. V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
  934. return 0;
  935. }
  936. static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
  937. unsigned width, unsigned height)
  938. {
  939. struct em28xx_fmt *fmt;
  940. fmt = format_by_fourcc(fourcc);
  941. if (!fmt)
  942. return -EINVAL;
  943. dev->format = fmt;
  944. dev->width = width;
  945. dev->height = height;
  946. /* set new image size */
  947. get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
  948. em28xx_set_alternate(dev);
  949. em28xx_resolution_set(dev);
  950. return 0;
  951. }
  952. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  953. struct v4l2_format *f)
  954. {
  955. struct em28xx_fh *fh = priv;
  956. struct em28xx *dev = fh->dev;
  957. int rc;
  958. rc = check_dev(dev);
  959. if (rc < 0)
  960. return rc;
  961. vidioc_try_fmt_vid_cap(file, priv, f);
  962. if (videobuf_queue_is_busy(&fh->vb_vidq)) {
  963. em28xx_errdev("%s queue busy\n", __func__);
  964. return -EBUSY;
  965. }
  966. return em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
  967. f->fmt.pix.width, f->fmt.pix.height);
  968. }
  969. static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
  970. {
  971. struct em28xx_fh *fh = priv;
  972. struct em28xx *dev = fh->dev;
  973. int rc;
  974. rc = check_dev(dev);
  975. if (rc < 0)
  976. return rc;
  977. *norm = dev->norm;
  978. return 0;
  979. }
  980. static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *norm)
  981. {
  982. struct em28xx_fh *fh = priv;
  983. struct em28xx *dev = fh->dev;
  984. int rc;
  985. rc = check_dev(dev);
  986. if (rc < 0)
  987. return rc;
  988. v4l2_device_call_all(&dev->v4l2_dev, 0, video, querystd, norm);
  989. return 0;
  990. }
  991. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
  992. {
  993. struct em28xx_fh *fh = priv;
  994. struct em28xx *dev = fh->dev;
  995. struct v4l2_format f;
  996. int rc;
  997. rc = check_dev(dev);
  998. if (rc < 0)
  999. return rc;
  1000. dev->norm = *norm;
  1001. /* Adjusts width/height, if needed */
  1002. f.fmt.pix.width = dev->width;
  1003. f.fmt.pix.height = dev->height;
  1004. vidioc_try_fmt_vid_cap(file, priv, &f);
  1005. /* set new image size */
  1006. dev->width = f.fmt.pix.width;
  1007. dev->height = f.fmt.pix.height;
  1008. get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
  1009. em28xx_resolution_set(dev);
  1010. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
  1011. return 0;
  1012. }
  1013. static int vidioc_g_parm(struct file *file, void *priv,
  1014. struct v4l2_streamparm *p)
  1015. {
  1016. struct em28xx_fh *fh = priv;
  1017. struct em28xx *dev = fh->dev;
  1018. int rc = 0;
  1019. if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1020. return -EINVAL;
  1021. if (dev->board.is_webcam)
  1022. rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0,
  1023. video, g_parm, p);
  1024. else
  1025. v4l2_video_std_frame_period(dev->norm,
  1026. &p->parm.capture.timeperframe);
  1027. return rc;
  1028. }
  1029. static int vidioc_s_parm(struct file *file, void *priv,
  1030. struct v4l2_streamparm *p)
  1031. {
  1032. struct em28xx_fh *fh = priv;
  1033. struct em28xx *dev = fh->dev;
  1034. if (!dev->board.is_webcam)
  1035. return -EINVAL;
  1036. if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1037. return -EINVAL;
  1038. return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p);
  1039. }
  1040. static const char *iname[] = {
  1041. [EM28XX_VMUX_COMPOSITE1] = "Composite1",
  1042. [EM28XX_VMUX_COMPOSITE2] = "Composite2",
  1043. [EM28XX_VMUX_COMPOSITE3] = "Composite3",
  1044. [EM28XX_VMUX_COMPOSITE4] = "Composite4",
  1045. [EM28XX_VMUX_SVIDEO] = "S-Video",
  1046. [EM28XX_VMUX_TELEVISION] = "Television",
  1047. [EM28XX_VMUX_CABLE] = "Cable TV",
  1048. [EM28XX_VMUX_DVB] = "DVB",
  1049. [EM28XX_VMUX_DEBUG] = "for debug only",
  1050. };
  1051. static int vidioc_enum_input(struct file *file, void *priv,
  1052. struct v4l2_input *i)
  1053. {
  1054. struct em28xx_fh *fh = priv;
  1055. struct em28xx *dev = fh->dev;
  1056. unsigned int n;
  1057. n = i->index;
  1058. if (n >= MAX_EM28XX_INPUT)
  1059. return -EINVAL;
  1060. if (0 == INPUT(n)->type)
  1061. return -EINVAL;
  1062. i->index = n;
  1063. i->type = V4L2_INPUT_TYPE_CAMERA;
  1064. strcpy(i->name, iname[INPUT(n)->type]);
  1065. if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
  1066. (EM28XX_VMUX_CABLE == INPUT(n)->type))
  1067. i->type = V4L2_INPUT_TYPE_TUNER;
  1068. i->std = dev->vdev->tvnorms;
  1069. return 0;
  1070. }
  1071. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  1072. {
  1073. struct em28xx_fh *fh = priv;
  1074. struct em28xx *dev = fh->dev;
  1075. *i = dev->ctl_input;
  1076. return 0;
  1077. }
  1078. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  1079. {
  1080. struct em28xx_fh *fh = priv;
  1081. struct em28xx *dev = fh->dev;
  1082. int rc;
  1083. rc = check_dev(dev);
  1084. if (rc < 0)
  1085. return rc;
  1086. if (i >= MAX_EM28XX_INPUT)
  1087. return -EINVAL;
  1088. if (0 == INPUT(i)->type)
  1089. return -EINVAL;
  1090. video_mux(dev, i);
  1091. return 0;
  1092. }
  1093. static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  1094. {
  1095. struct em28xx_fh *fh = priv;
  1096. struct em28xx *dev = fh->dev;
  1097. if (!dev->audio_mode.has_audio)
  1098. return -EINVAL;
  1099. switch (a->index) {
  1100. case EM28XX_AMUX_VIDEO:
  1101. strcpy(a->name, "Television");
  1102. break;
  1103. case EM28XX_AMUX_LINE_IN:
  1104. strcpy(a->name, "Line In");
  1105. break;
  1106. case EM28XX_AMUX_VIDEO2:
  1107. strcpy(a->name, "Television alt");
  1108. break;
  1109. case EM28XX_AMUX_PHONE:
  1110. strcpy(a->name, "Phone");
  1111. break;
  1112. case EM28XX_AMUX_MIC:
  1113. strcpy(a->name, "Mic");
  1114. break;
  1115. case EM28XX_AMUX_CD:
  1116. strcpy(a->name, "CD");
  1117. break;
  1118. case EM28XX_AMUX_AUX:
  1119. strcpy(a->name, "Aux");
  1120. break;
  1121. case EM28XX_AMUX_PCM_OUT:
  1122. strcpy(a->name, "PCM");
  1123. break;
  1124. default:
  1125. return -EINVAL;
  1126. }
  1127. a->index = dev->ctl_ainput;
  1128. a->capability = V4L2_AUDCAP_STEREO;
  1129. return 0;
  1130. }
  1131. static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
  1132. {
  1133. struct em28xx_fh *fh = priv;
  1134. struct em28xx *dev = fh->dev;
  1135. if (!dev->audio_mode.has_audio)
  1136. return -EINVAL;
  1137. if (a->index >= MAX_EM28XX_INPUT)
  1138. return -EINVAL;
  1139. if (0 == INPUT(a->index)->type)
  1140. return -EINVAL;
  1141. dev->ctl_ainput = INPUT(a->index)->amux;
  1142. dev->ctl_aoutput = INPUT(a->index)->aout;
  1143. if (!dev->ctl_aoutput)
  1144. dev->ctl_aoutput = EM28XX_AOUT_MASTER;
  1145. return 0;
  1146. }
  1147. static int vidioc_queryctrl(struct file *file, void *priv,
  1148. struct v4l2_queryctrl *qc)
  1149. {
  1150. struct em28xx_fh *fh = priv;
  1151. struct em28xx *dev = fh->dev;
  1152. int id = qc->id;
  1153. int rc;
  1154. rc = check_dev(dev);
  1155. if (rc < 0)
  1156. return rc;
  1157. memset(qc, 0, sizeof(*qc));
  1158. qc->id = id;
  1159. /* enumerate AC97 controls */
  1160. if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
  1161. rc = ac97_queryctrl(qc);
  1162. if (!rc)
  1163. return 0;
  1164. }
  1165. /* enumerate V4L2 device controls */
  1166. v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
  1167. if (qc->type)
  1168. return 0;
  1169. else
  1170. return -EINVAL;
  1171. }
  1172. /*
  1173. * FIXME: This is an indirect way to check if a control exists at a
  1174. * subdev. Instead of that hack, maybe the better would be to change all
  1175. * subdevs to return -ENOIOCTLCMD, if an ioctl is not supported.
  1176. */
  1177. static int check_subdev_ctrl(struct em28xx *dev, int id)
  1178. {
  1179. struct v4l2_queryctrl qc;
  1180. memset(&qc, 0, sizeof(qc));
  1181. qc.id = id;
  1182. /* enumerate V4L2 device controls */
  1183. v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, &qc);
  1184. if (qc.type)
  1185. return 0;
  1186. else
  1187. return -EINVAL;
  1188. }
  1189. static int vidioc_g_ctrl(struct file *file, void *priv,
  1190. struct v4l2_control *ctrl)
  1191. {
  1192. struct em28xx_fh *fh = priv;
  1193. struct em28xx *dev = fh->dev;
  1194. int rc;
  1195. rc = check_dev(dev);
  1196. if (rc < 0)
  1197. return rc;
  1198. rc = 0;
  1199. /* Set an AC97 control */
  1200. if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
  1201. rc = ac97_get_ctrl(dev, ctrl);
  1202. else
  1203. rc = 1;
  1204. /* It were not an AC97 control. Sends it to the v4l2 dev interface */
  1205. if (rc == 1) {
  1206. if (check_subdev_ctrl(dev, ctrl->id))
  1207. return -EINVAL;
  1208. v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
  1209. rc = 0;
  1210. }
  1211. return rc;
  1212. }
  1213. static int vidioc_s_ctrl(struct file *file, void *priv,
  1214. struct v4l2_control *ctrl)
  1215. {
  1216. struct em28xx_fh *fh = priv;
  1217. struct em28xx *dev = fh->dev;
  1218. int rc;
  1219. rc = check_dev(dev);
  1220. if (rc < 0)
  1221. return rc;
  1222. /* Set an AC97 control */
  1223. if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
  1224. rc = ac97_set_ctrl(dev, ctrl);
  1225. else
  1226. rc = 1;
  1227. /* It isn't an AC97 control. Sends it to the v4l2 dev interface */
  1228. if (rc == 1) {
  1229. rc = check_subdev_ctrl(dev, ctrl->id);
  1230. if (!rc)
  1231. v4l2_device_call_all(&dev->v4l2_dev, 0,
  1232. core, s_ctrl, ctrl);
  1233. /*
  1234. * In the case of non-AC97 volume controls, we still need
  1235. * to do some setups at em28xx, in order to mute/unmute
  1236. * and to adjust audio volume. However, the value ranges
  1237. * should be checked by the corresponding V4L subdriver.
  1238. */
  1239. switch (ctrl->id) {
  1240. case V4L2_CID_AUDIO_MUTE:
  1241. dev->mute = ctrl->value;
  1242. rc = em28xx_audio_analog_set(dev);
  1243. break;
  1244. case V4L2_CID_AUDIO_VOLUME:
  1245. dev->volume = ctrl->value;
  1246. rc = em28xx_audio_analog_set(dev);
  1247. }
  1248. }
  1249. return (rc < 0) ? rc : 0;
  1250. }
  1251. static int vidioc_g_tuner(struct file *file, void *priv,
  1252. struct v4l2_tuner *t)
  1253. {
  1254. struct em28xx_fh *fh = priv;
  1255. struct em28xx *dev = fh->dev;
  1256. int rc;
  1257. rc = check_dev(dev);
  1258. if (rc < 0)
  1259. return rc;
  1260. if (0 != t->index)
  1261. return -EINVAL;
  1262. strcpy(t->name, "Tuner");
  1263. t->type = V4L2_TUNER_ANALOG_TV;
  1264. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
  1265. return 0;
  1266. }
  1267. static int vidioc_s_tuner(struct file *file, void *priv,
  1268. struct v4l2_tuner *t)
  1269. {
  1270. struct em28xx_fh *fh = priv;
  1271. struct em28xx *dev = fh->dev;
  1272. int rc;
  1273. rc = check_dev(dev);
  1274. if (rc < 0)
  1275. return rc;
  1276. if (0 != t->index)
  1277. return -EINVAL;
  1278. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
  1279. return 0;
  1280. }
  1281. static int vidioc_g_frequency(struct file *file, void *priv,
  1282. struct v4l2_frequency *f)
  1283. {
  1284. struct em28xx_fh *fh = priv;
  1285. struct em28xx *dev = fh->dev;
  1286. f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1287. f->frequency = dev->ctl_freq;
  1288. return 0;
  1289. }
  1290. static int vidioc_s_frequency(struct file *file, void *priv,
  1291. struct v4l2_frequency *f)
  1292. {
  1293. struct em28xx_fh *fh = priv;
  1294. struct em28xx *dev = fh->dev;
  1295. int rc;
  1296. rc = check_dev(dev);
  1297. if (rc < 0)
  1298. return rc;
  1299. if (0 != f->tuner)
  1300. return -EINVAL;
  1301. if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
  1302. return -EINVAL;
  1303. if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
  1304. return -EINVAL;
  1305. dev->ctl_freq = f->frequency;
  1306. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
  1307. return 0;
  1308. }
  1309. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1310. static int em28xx_reg_len(int reg)
  1311. {
  1312. switch (reg) {
  1313. case EM28XX_R40_AC97LSB:
  1314. case EM28XX_R30_HSCALELOW:
  1315. case EM28XX_R32_VSCALELOW:
  1316. return 2;
  1317. default:
  1318. return 1;
  1319. }
  1320. }
  1321. static int vidioc_g_chip_ident(struct file *file, void *priv,
  1322. struct v4l2_dbg_chip_ident *chip)
  1323. {
  1324. struct em28xx_fh *fh = priv;
  1325. struct em28xx *dev = fh->dev;
  1326. chip->ident = V4L2_IDENT_NONE;
  1327. chip->revision = 0;
  1328. v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
  1329. return 0;
  1330. }
  1331. static int vidioc_g_register(struct file *file, void *priv,
  1332. struct v4l2_dbg_register *reg)
  1333. {
  1334. struct em28xx_fh *fh = priv;
  1335. struct em28xx *dev = fh->dev;
  1336. int ret;
  1337. switch (reg->match.type) {
  1338. case V4L2_CHIP_MATCH_AC97:
  1339. ret = em28xx_read_ac97(dev, reg->reg);
  1340. if (ret < 0)
  1341. return ret;
  1342. reg->val = ret;
  1343. reg->size = 1;
  1344. return 0;
  1345. case V4L2_CHIP_MATCH_I2C_DRIVER:
  1346. v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
  1347. return 0;
  1348. case V4L2_CHIP_MATCH_I2C_ADDR:
  1349. /* TODO: is this correct? */
  1350. v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
  1351. return 0;
  1352. default:
  1353. if (!v4l2_chip_match_host(&reg->match))
  1354. return -EINVAL;
  1355. }
  1356. /* Match host */
  1357. reg->size = em28xx_reg_len(reg->reg);
  1358. if (reg->size == 1) {
  1359. ret = em28xx_read_reg(dev, reg->reg);
  1360. if (ret < 0)
  1361. return ret;
  1362. reg->val = ret;
  1363. } else {
  1364. __le16 val = 0;
  1365. ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
  1366. reg->reg, (char *)&val, 2);
  1367. if (ret < 0)
  1368. return ret;
  1369. reg->val = le16_to_cpu(val);
  1370. }
  1371. return 0;
  1372. }
  1373. static int vidioc_s_register(struct file *file, void *priv,
  1374. struct v4l2_dbg_register *reg)
  1375. {
  1376. struct em28xx_fh *fh = priv;
  1377. struct em28xx *dev = fh->dev;
  1378. __le16 buf;
  1379. switch (reg->match.type) {
  1380. case V4L2_CHIP_MATCH_AC97:
  1381. return em28xx_write_ac97(dev, reg->reg, reg->val);
  1382. case V4L2_CHIP_MATCH_I2C_DRIVER:
  1383. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
  1384. return 0;
  1385. case V4L2_CHIP_MATCH_I2C_ADDR:
  1386. /* TODO: is this correct? */
  1387. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
  1388. return 0;
  1389. default:
  1390. if (!v4l2_chip_match_host(&reg->match))
  1391. return -EINVAL;
  1392. }
  1393. /* Match host */
  1394. buf = cpu_to_le16(reg->val);
  1395. return em28xx_write_regs(dev, reg->reg, (char *)&buf,
  1396. em28xx_reg_len(reg->reg));
  1397. }
  1398. #endif
  1399. static int vidioc_cropcap(struct file *file, void *priv,
  1400. struct v4l2_cropcap *cc)
  1401. {
  1402. struct em28xx_fh *fh = priv;
  1403. struct em28xx *dev = fh->dev;
  1404. if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1405. return -EINVAL;
  1406. cc->bounds.left = 0;
  1407. cc->bounds.top = 0;
  1408. cc->bounds.width = dev->width;
  1409. cc->bounds.height = dev->height;
  1410. cc->defrect = cc->bounds;
  1411. cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
  1412. cc->pixelaspect.denominator = 59;
  1413. return 0;
  1414. }
  1415. static int vidioc_streamon(struct file *file, void *priv,
  1416. enum v4l2_buf_type type)
  1417. {
  1418. struct em28xx_fh *fh = priv;
  1419. struct em28xx *dev = fh->dev;
  1420. int rc = -EINVAL;
  1421. rc = check_dev(dev);
  1422. if (rc < 0)
  1423. return rc;
  1424. if (unlikely(type != fh->type))
  1425. return -EINVAL;
  1426. em28xx_videodbg("vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
  1427. fh, type, fh->resources, dev->resources);
  1428. if (unlikely(!res_get(fh, get_ressource(fh))))
  1429. return -EBUSY;
  1430. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1431. rc = videobuf_streamon(&fh->vb_vidq);
  1432. else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
  1433. rc = videobuf_streamon(&fh->vb_vbiq);
  1434. return rc;
  1435. }
  1436. static int vidioc_streamoff(struct file *file, void *priv,
  1437. enum v4l2_buf_type type)
  1438. {
  1439. struct em28xx_fh *fh = priv;
  1440. struct em28xx *dev = fh->dev;
  1441. int rc;
  1442. rc = check_dev(dev);
  1443. if (rc < 0)
  1444. return rc;
  1445. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  1446. fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)
  1447. return -EINVAL;
  1448. if (type != fh->type)
  1449. return -EINVAL;
  1450. em28xx_videodbg("vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
  1451. fh, type, fh->resources, dev->resources);
  1452. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  1453. if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
  1454. videobuf_streamoff(&fh->vb_vidq);
  1455. res_free(fh, EM28XX_RESOURCE_VIDEO);
  1456. }
  1457. } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
  1458. if (res_check(fh, EM28XX_RESOURCE_VBI)) {
  1459. videobuf_streamoff(&fh->vb_vbiq);
  1460. res_free(fh, EM28XX_RESOURCE_VBI);
  1461. }
  1462. }
  1463. return 0;
  1464. }
  1465. static int vidioc_querycap(struct file *file, void *priv,
  1466. struct v4l2_capability *cap)
  1467. {
  1468. struct em28xx_fh *fh = priv;
  1469. struct em28xx *dev = fh->dev;
  1470. strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
  1471. strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
  1472. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  1473. cap->capabilities =
  1474. V4L2_CAP_SLICED_VBI_CAPTURE |
  1475. V4L2_CAP_VIDEO_CAPTURE |
  1476. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  1477. if (dev->vbi_dev)
  1478. cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
  1479. if (dev->audio_mode.has_audio)
  1480. cap->capabilities |= V4L2_CAP_AUDIO;
  1481. if (dev->tuner_type != TUNER_ABSENT)
  1482. cap->capabilities |= V4L2_CAP_TUNER;
  1483. return 0;
  1484. }
  1485. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  1486. struct v4l2_fmtdesc *f)
  1487. {
  1488. if (unlikely(f->index >= ARRAY_SIZE(format)))
  1489. return -EINVAL;
  1490. strlcpy(f->description, format[f->index].name, sizeof(f->description));
  1491. f->pixelformat = format[f->index].fourcc;
  1492. return 0;
  1493. }
  1494. static int vidioc_enum_framesizes(struct file *file, void *priv,
  1495. struct v4l2_frmsizeenum *fsize)
  1496. {
  1497. struct em28xx_fh *fh = priv;
  1498. struct em28xx *dev = fh->dev;
  1499. struct em28xx_fmt *fmt;
  1500. unsigned int maxw = norm_maxw(dev);
  1501. unsigned int maxh = norm_maxh(dev);
  1502. fmt = format_by_fourcc(fsize->pixel_format);
  1503. if (!fmt) {
  1504. em28xx_videodbg("Fourcc format (%08x) invalid.\n",
  1505. fsize->pixel_format);
  1506. return -EINVAL;
  1507. }
  1508. if (dev->board.is_em2800) {
  1509. if (fsize->index > 1)
  1510. return -EINVAL;
  1511. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  1512. fsize->discrete.width = maxw / (1 + fsize->index);
  1513. fsize->discrete.height = maxh / (1 + fsize->index);
  1514. return 0;
  1515. }
  1516. if (fsize->index != 0)
  1517. return -EINVAL;
  1518. /* Report a continuous range */
  1519. fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
  1520. fsize->stepwise.min_width = 48;
  1521. fsize->stepwise.min_height = 32;
  1522. fsize->stepwise.max_width = maxw;
  1523. fsize->stepwise.max_height = maxh;
  1524. fsize->stepwise.step_width = 1;
  1525. fsize->stepwise.step_height = 1;
  1526. return 0;
  1527. }
  1528. /* Sliced VBI ioctls */
  1529. static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
  1530. struct v4l2_format *f)
  1531. {
  1532. struct em28xx_fh *fh = priv;
  1533. struct em28xx *dev = fh->dev;
  1534. int rc;
  1535. rc = check_dev(dev);
  1536. if (rc < 0)
  1537. return rc;
  1538. f->fmt.sliced.service_set = 0;
  1539. v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
  1540. if (f->fmt.sliced.service_set == 0)
  1541. rc = -EINVAL;
  1542. return rc;
  1543. }
  1544. static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
  1545. struct v4l2_format *f)
  1546. {
  1547. struct em28xx_fh *fh = priv;
  1548. struct em28xx *dev = fh->dev;
  1549. int rc;
  1550. rc = check_dev(dev);
  1551. if (rc < 0)
  1552. return rc;
  1553. v4l2_device_call_all(&dev->v4l2_dev, 0, vbi, g_sliced_fmt, &f->fmt.sliced);
  1554. if (f->fmt.sliced.service_set == 0)
  1555. return -EINVAL;
  1556. return 0;
  1557. }
  1558. /* RAW VBI ioctls */
  1559. static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
  1560. struct v4l2_format *format)
  1561. {
  1562. struct em28xx_fh *fh = priv;
  1563. struct em28xx *dev = fh->dev;
  1564. format->fmt.vbi.samples_per_line = dev->vbi_width;
  1565. format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  1566. format->fmt.vbi.offset = 0;
  1567. format->fmt.vbi.flags = 0;
  1568. format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
  1569. format->fmt.vbi.count[0] = dev->vbi_height;
  1570. format->fmt.vbi.count[1] = dev->vbi_height;
  1571. /* Varies by video standard (NTSC, PAL, etc.) */
  1572. if (dev->norm & V4L2_STD_525_60) {
  1573. /* NTSC */
  1574. format->fmt.vbi.start[0] = 10;
  1575. format->fmt.vbi.start[1] = 273;
  1576. } else if (dev->norm & V4L2_STD_625_50) {
  1577. /* PAL */
  1578. format->fmt.vbi.start[0] = 6;
  1579. format->fmt.vbi.start[1] = 318;
  1580. }
  1581. return 0;
  1582. }
  1583. static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
  1584. struct v4l2_format *format)
  1585. {
  1586. struct em28xx_fh *fh = priv;
  1587. struct em28xx *dev = fh->dev;
  1588. format->fmt.vbi.samples_per_line = dev->vbi_width;
  1589. format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  1590. format->fmt.vbi.offset = 0;
  1591. format->fmt.vbi.flags = 0;
  1592. format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
  1593. format->fmt.vbi.count[0] = dev->vbi_height;
  1594. format->fmt.vbi.count[1] = dev->vbi_height;
  1595. /* Varies by video standard (NTSC, PAL, etc.) */
  1596. if (dev->norm & V4L2_STD_525_60) {
  1597. /* NTSC */
  1598. format->fmt.vbi.start[0] = 10;
  1599. format->fmt.vbi.start[1] = 273;
  1600. } else if (dev->norm & V4L2_STD_625_50) {
  1601. /* PAL */
  1602. format->fmt.vbi.start[0] = 6;
  1603. format->fmt.vbi.start[1] = 318;
  1604. }
  1605. return 0;
  1606. }
  1607. static int vidioc_reqbufs(struct file *file, void *priv,
  1608. struct v4l2_requestbuffers *rb)
  1609. {
  1610. struct em28xx_fh *fh = priv;
  1611. struct em28xx *dev = fh->dev;
  1612. int rc;
  1613. rc = check_dev(dev);
  1614. if (rc < 0)
  1615. return rc;
  1616. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1617. return videobuf_reqbufs(&fh->vb_vidq, rb);
  1618. else
  1619. return videobuf_reqbufs(&fh->vb_vbiq, rb);
  1620. }
  1621. static int vidioc_querybuf(struct file *file, void *priv,
  1622. struct v4l2_buffer *b)
  1623. {
  1624. struct em28xx_fh *fh = priv;
  1625. struct em28xx *dev = fh->dev;
  1626. int rc;
  1627. rc = check_dev(dev);
  1628. if (rc < 0)
  1629. return rc;
  1630. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1631. return videobuf_querybuf(&fh->vb_vidq, b);
  1632. else {
  1633. /* FIXME: I'm not sure yet whether this is a bug in zvbi or
  1634. the videobuf framework, but we probably shouldn't be
  1635. returning a buffer larger than that which was asked for.
  1636. At a minimum, it causes a crash in zvbi since it does
  1637. a memcpy based on the source buffer length */
  1638. int result = videobuf_querybuf(&fh->vb_vbiq, b);
  1639. b->length = dev->vbi_width * dev->vbi_height * 2;
  1640. return result;
  1641. }
  1642. }
  1643. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1644. {
  1645. struct em28xx_fh *fh = priv;
  1646. struct em28xx *dev = fh->dev;
  1647. int rc;
  1648. rc = check_dev(dev);
  1649. if (rc < 0)
  1650. return rc;
  1651. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1652. return videobuf_qbuf(&fh->vb_vidq, b);
  1653. else
  1654. return videobuf_qbuf(&fh->vb_vbiq, b);
  1655. }
  1656. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1657. {
  1658. struct em28xx_fh *fh = priv;
  1659. struct em28xx *dev = fh->dev;
  1660. int rc;
  1661. rc = check_dev(dev);
  1662. if (rc < 0)
  1663. return rc;
  1664. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1665. return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags &
  1666. O_NONBLOCK);
  1667. else
  1668. return videobuf_dqbuf(&fh->vb_vbiq, b, file->f_flags &
  1669. O_NONBLOCK);
  1670. }
  1671. /* ----------------------------------------------------------- */
  1672. /* RADIO ESPECIFIC IOCTLS */
  1673. /* ----------------------------------------------------------- */
  1674. static int radio_querycap(struct file *file, void *priv,
  1675. struct v4l2_capability *cap)
  1676. {
  1677. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1678. strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
  1679. strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
  1680. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  1681. cap->capabilities = V4L2_CAP_TUNER;
  1682. return 0;
  1683. }
  1684. static int radio_g_tuner(struct file *file, void *priv,
  1685. struct v4l2_tuner *t)
  1686. {
  1687. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1688. if (unlikely(t->index > 0))
  1689. return -EINVAL;
  1690. strcpy(t->name, "Radio");
  1691. t->type = V4L2_TUNER_RADIO;
  1692. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
  1693. return 0;
  1694. }
  1695. static int radio_enum_input(struct file *file, void *priv,
  1696. struct v4l2_input *i)
  1697. {
  1698. if (i->index != 0)
  1699. return -EINVAL;
  1700. strcpy(i->name, "Radio");
  1701. i->type = V4L2_INPUT_TYPE_TUNER;
  1702. return 0;
  1703. }
  1704. static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  1705. {
  1706. if (unlikely(a->index))
  1707. return -EINVAL;
  1708. strcpy(a->name, "Radio");
  1709. return 0;
  1710. }
  1711. static int radio_s_tuner(struct file *file, void *priv,
  1712. struct v4l2_tuner *t)
  1713. {
  1714. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1715. if (0 != t->index)
  1716. return -EINVAL;
  1717. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
  1718. return 0;
  1719. }
  1720. static int radio_s_audio(struct file *file, void *fh,
  1721. const struct v4l2_audio *a)
  1722. {
  1723. return 0;
  1724. }
  1725. static int radio_s_input(struct file *file, void *fh, unsigned int i)
  1726. {
  1727. return 0;
  1728. }
  1729. static int radio_queryctrl(struct file *file, void *priv,
  1730. struct v4l2_queryctrl *qc)
  1731. {
  1732. int i;
  1733. if (qc->id < V4L2_CID_BASE ||
  1734. qc->id >= V4L2_CID_LASTP1)
  1735. return -EINVAL;
  1736. for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
  1737. if (qc->id && qc->id == ac97_qctrl[i].id) {
  1738. memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
  1739. return 0;
  1740. }
  1741. }
  1742. return -EINVAL;
  1743. }
  1744. /*
  1745. * em28xx_v4l2_open()
  1746. * inits the device and starts isoc transfer
  1747. */
  1748. static int em28xx_v4l2_open(struct file *filp)
  1749. {
  1750. int errCode = 0, radio = 0;
  1751. struct video_device *vdev = video_devdata(filp);
  1752. struct em28xx *dev = video_drvdata(filp);
  1753. enum v4l2_buf_type fh_type = 0;
  1754. struct em28xx_fh *fh;
  1755. enum v4l2_field field;
  1756. switch (vdev->vfl_type) {
  1757. case VFL_TYPE_GRABBER:
  1758. fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1759. break;
  1760. case VFL_TYPE_VBI:
  1761. fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
  1762. break;
  1763. case VFL_TYPE_RADIO:
  1764. radio = 1;
  1765. break;
  1766. }
  1767. em28xx_videodbg("open dev=%s type=%s users=%d\n",
  1768. video_device_node_name(vdev), v4l2_type_names[fh_type],
  1769. dev->users);
  1770. if (mutex_lock_interruptible(&dev->lock))
  1771. return -ERESTARTSYS;
  1772. fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
  1773. if (!fh) {
  1774. em28xx_errdev("em28xx-video.c: Out of memory?!\n");
  1775. mutex_unlock(&dev->lock);
  1776. return -ENOMEM;
  1777. }
  1778. fh->dev = dev;
  1779. fh->radio = radio;
  1780. fh->type = fh_type;
  1781. filp->private_data = fh;
  1782. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
  1783. em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
  1784. em28xx_set_alternate(dev);
  1785. em28xx_resolution_set(dev);
  1786. /* Needed, since GPIO might have disabled power of
  1787. some i2c device
  1788. */
  1789. em28xx_wake_i2c(dev);
  1790. }
  1791. if (fh->radio) {
  1792. em28xx_videodbg("video_open: setting radio device\n");
  1793. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
  1794. }
  1795. dev->users++;
  1796. if (dev->progressive)
  1797. field = V4L2_FIELD_NONE;
  1798. else
  1799. field = V4L2_FIELD_INTERLACED;
  1800. videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
  1801. NULL, &dev->slock,
  1802. V4L2_BUF_TYPE_VIDEO_CAPTURE, field,
  1803. sizeof(struct em28xx_buffer), fh, &dev->lock);
  1804. videobuf_queue_vmalloc_init(&fh->vb_vbiq, &em28xx_vbi_qops,
  1805. NULL, &dev->slock,
  1806. V4L2_BUF_TYPE_VBI_CAPTURE,
  1807. V4L2_FIELD_SEQ_TB,
  1808. sizeof(struct em28xx_buffer), fh, &dev->lock);
  1809. mutex_unlock(&dev->lock);
  1810. return errCode;
  1811. }
  1812. /*
  1813. * em28xx_realease_resources()
  1814. * unregisters the v4l2,i2c and usb devices
  1815. * called when the device gets disconected or at module unload
  1816. */
  1817. void em28xx_release_analog_resources(struct em28xx *dev)
  1818. {
  1819. /*FIXME: I2C IR should be disconnected */
  1820. if (dev->radio_dev) {
  1821. if (video_is_registered(dev->radio_dev))
  1822. video_unregister_device(dev->radio_dev);
  1823. else
  1824. video_device_release(dev->radio_dev);
  1825. dev->radio_dev = NULL;
  1826. }
  1827. if (dev->vbi_dev) {
  1828. em28xx_info("V4L2 device %s deregistered\n",
  1829. video_device_node_name(dev->vbi_dev));
  1830. if (video_is_registered(dev->vbi_dev))
  1831. video_unregister_device(dev->vbi_dev);
  1832. else
  1833. video_device_release(dev->vbi_dev);
  1834. dev->vbi_dev = NULL;
  1835. }
  1836. if (dev->vdev) {
  1837. em28xx_info("V4L2 device %s deregistered\n",
  1838. video_device_node_name(dev->vdev));
  1839. if (video_is_registered(dev->vdev))
  1840. video_unregister_device(dev->vdev);
  1841. else
  1842. video_device_release(dev->vdev);
  1843. dev->vdev = NULL;
  1844. }
  1845. }
  1846. /*
  1847. * em28xx_v4l2_close()
  1848. * stops streaming and deallocates all resources allocated by the v4l2
  1849. * calls and ioctls
  1850. */
  1851. static int em28xx_v4l2_close(struct file *filp)
  1852. {
  1853. struct em28xx_fh *fh = filp->private_data;
  1854. struct em28xx *dev = fh->dev;
  1855. int errCode;
  1856. em28xx_videodbg("users=%d\n", dev->users);
  1857. mutex_lock(&dev->lock);
  1858. if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
  1859. videobuf_stop(&fh->vb_vidq);
  1860. res_free(fh, EM28XX_RESOURCE_VIDEO);
  1861. }
  1862. if (res_check(fh, EM28XX_RESOURCE_VBI)) {
  1863. videobuf_stop(&fh->vb_vbiq);
  1864. res_free(fh, EM28XX_RESOURCE_VBI);
  1865. }
  1866. if (dev->users == 1) {
  1867. /* the device is already disconnect,
  1868. free the remaining resources */
  1869. if (dev->state & DEV_DISCONNECTED) {
  1870. em28xx_release_resources(dev);
  1871. kfree(dev->alt_max_pkt_size);
  1872. mutex_unlock(&dev->lock);
  1873. kfree(dev);
  1874. kfree(fh);
  1875. return 0;
  1876. }
  1877. /* Save some power by putting tuner to sleep */
  1878. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
  1879. /* do this before setting alternate! */
  1880. em28xx_uninit_isoc(dev, EM28XX_ANALOG_MODE);
  1881. em28xx_set_mode(dev, EM28XX_SUSPEND);
  1882. /* set alternate 0 */
  1883. dev->alt = 0;
  1884. em28xx_videodbg("setting alternate 0\n");
  1885. errCode = usb_set_interface(dev->udev, 0, 0);
  1886. if (errCode < 0) {
  1887. em28xx_errdev("cannot change alternate number to "
  1888. "0 (error=%i)\n", errCode);
  1889. }
  1890. }
  1891. videobuf_mmap_free(&fh->vb_vidq);
  1892. videobuf_mmap_free(&fh->vb_vbiq);
  1893. kfree(fh);
  1894. dev->users--;
  1895. mutex_unlock(&dev->lock);
  1896. return 0;
  1897. }
  1898. /*
  1899. * em28xx_v4l2_read()
  1900. * will allocate buffers when called for the first time
  1901. */
  1902. static ssize_t
  1903. em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
  1904. loff_t *pos)
  1905. {
  1906. struct em28xx_fh *fh = filp->private_data;
  1907. struct em28xx *dev = fh->dev;
  1908. int rc;
  1909. rc = check_dev(dev);
  1910. if (rc < 0)
  1911. return rc;
  1912. if (mutex_lock_interruptible(&dev->lock))
  1913. return -ERESTARTSYS;
  1914. /* FIXME: read() is not prepared to allow changing the video
  1915. resolution while streaming. Seems a bug at em28xx_set_fmt
  1916. */
  1917. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  1918. if (res_locked(dev, EM28XX_RESOURCE_VIDEO))
  1919. rc = -EBUSY;
  1920. else
  1921. rc = videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
  1922. filp->f_flags & O_NONBLOCK);
  1923. } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
  1924. if (!res_get(fh, EM28XX_RESOURCE_VBI))
  1925. rc = -EBUSY;
  1926. else
  1927. rc = videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
  1928. filp->f_flags & O_NONBLOCK);
  1929. }
  1930. mutex_unlock(&dev->lock);
  1931. return rc;
  1932. }
  1933. /*
  1934. * em28xx_poll()
  1935. * will allocate buffers when called for the first time
  1936. */
  1937. static unsigned int em28xx_poll(struct file *filp, poll_table *wait)
  1938. {
  1939. struct em28xx_fh *fh = filp->private_data;
  1940. struct em28xx *dev = fh->dev;
  1941. int rc;
  1942. rc = check_dev(dev);
  1943. if (rc < 0)
  1944. return rc;
  1945. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  1946. if (!res_get(fh, EM28XX_RESOURCE_VIDEO))
  1947. return POLLERR;
  1948. return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
  1949. } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
  1950. if (!res_get(fh, EM28XX_RESOURCE_VBI))
  1951. return POLLERR;
  1952. return videobuf_poll_stream(filp, &fh->vb_vbiq, wait);
  1953. } else {
  1954. return POLLERR;
  1955. }
  1956. }
  1957. static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
  1958. {
  1959. struct em28xx_fh *fh = filp->private_data;
  1960. struct em28xx *dev = fh->dev;
  1961. unsigned int res;
  1962. mutex_lock(&dev->lock);
  1963. res = em28xx_poll(filp, wait);
  1964. mutex_unlock(&dev->lock);
  1965. return res;
  1966. }
  1967. /*
  1968. * em28xx_v4l2_mmap()
  1969. */
  1970. static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
  1971. {
  1972. struct em28xx_fh *fh = filp->private_data;
  1973. struct em28xx *dev = fh->dev;
  1974. int rc;
  1975. rc = check_dev(dev);
  1976. if (rc < 0)
  1977. return rc;
  1978. if (mutex_lock_interruptible(&dev->lock))
  1979. return -ERESTARTSYS;
  1980. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1981. rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
  1982. else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
  1983. rc = videobuf_mmap_mapper(&fh->vb_vbiq, vma);
  1984. mutex_unlock(&dev->lock);
  1985. em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
  1986. (unsigned long)vma->vm_start,
  1987. (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
  1988. rc);
  1989. return rc;
  1990. }
  1991. static const struct v4l2_file_operations em28xx_v4l_fops = {
  1992. .owner = THIS_MODULE,
  1993. .open = em28xx_v4l2_open,
  1994. .release = em28xx_v4l2_close,
  1995. .read = em28xx_v4l2_read,
  1996. .poll = em28xx_v4l2_poll,
  1997. .mmap = em28xx_v4l2_mmap,
  1998. .unlocked_ioctl = video_ioctl2,
  1999. };
  2000. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  2001. .vidioc_querycap = vidioc_querycap,
  2002. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  2003. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  2004. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  2005. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  2006. .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
  2007. .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
  2008. .vidioc_enum_framesizes = vidioc_enum_framesizes,
  2009. .vidioc_g_audio = vidioc_g_audio,
  2010. .vidioc_s_audio = vidioc_s_audio,
  2011. .vidioc_cropcap = vidioc_cropcap,
  2012. .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
  2013. .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
  2014. .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
  2015. .vidioc_reqbufs = vidioc_reqbufs,
  2016. .vidioc_querybuf = vidioc_querybuf,
  2017. .vidioc_qbuf = vidioc_qbuf,
  2018. .vidioc_dqbuf = vidioc_dqbuf,
  2019. .vidioc_g_std = vidioc_g_std,
  2020. .vidioc_querystd = vidioc_querystd,
  2021. .vidioc_s_std = vidioc_s_std,
  2022. .vidioc_g_parm = vidioc_g_parm,
  2023. .vidioc_s_parm = vidioc_s_parm,
  2024. .vidioc_enum_input = vidioc_enum_input,
  2025. .vidioc_g_input = vidioc_g_input,
  2026. .vidioc_s_input = vidioc_s_input,
  2027. .vidioc_queryctrl = vidioc_queryctrl,
  2028. .vidioc_g_ctrl = vidioc_g_ctrl,
  2029. .vidioc_s_ctrl = vidioc_s_ctrl,
  2030. .vidioc_streamon = vidioc_streamon,
  2031. .vidioc_streamoff = vidioc_streamoff,
  2032. .vidioc_g_tuner = vidioc_g_tuner,
  2033. .vidioc_s_tuner = vidioc_s_tuner,
  2034. .vidioc_g_frequency = vidioc_g_frequency,
  2035. .vidioc_s_frequency = vidioc_s_frequency,
  2036. #ifdef CONFIG_VIDEO_ADV_DEBUG
  2037. .vidioc_g_register = vidioc_g_register,
  2038. .vidioc_s_register = vidioc_s_register,
  2039. .vidioc_g_chip_ident = vidioc_g_chip_ident,
  2040. #endif
  2041. };
  2042. static const struct video_device em28xx_video_template = {
  2043. .fops = &em28xx_v4l_fops,
  2044. .release = video_device_release,
  2045. .ioctl_ops = &video_ioctl_ops,
  2046. .tvnorms = V4L2_STD_ALL,
  2047. .current_norm = V4L2_STD_PAL,
  2048. };
  2049. static const struct v4l2_file_operations radio_fops = {
  2050. .owner = THIS_MODULE,
  2051. .open = em28xx_v4l2_open,
  2052. .release = em28xx_v4l2_close,
  2053. .unlocked_ioctl = video_ioctl2,
  2054. };
  2055. static const struct v4l2_ioctl_ops radio_ioctl_ops = {
  2056. .vidioc_querycap = radio_querycap,
  2057. .vidioc_g_tuner = radio_g_tuner,
  2058. .vidioc_enum_input = radio_enum_input,
  2059. .vidioc_g_audio = radio_g_audio,
  2060. .vidioc_s_tuner = radio_s_tuner,
  2061. .vidioc_s_audio = radio_s_audio,
  2062. .vidioc_s_input = radio_s_input,
  2063. .vidioc_queryctrl = radio_queryctrl,
  2064. .vidioc_g_ctrl = vidioc_g_ctrl,
  2065. .vidioc_s_ctrl = vidioc_s_ctrl,
  2066. .vidioc_g_frequency = vidioc_g_frequency,
  2067. .vidioc_s_frequency = vidioc_s_frequency,
  2068. #ifdef CONFIG_VIDEO_ADV_DEBUG
  2069. .vidioc_g_register = vidioc_g_register,
  2070. .vidioc_s_register = vidioc_s_register,
  2071. #endif
  2072. };
  2073. static struct video_device em28xx_radio_template = {
  2074. .name = "em28xx-radio",
  2075. .fops = &radio_fops,
  2076. .ioctl_ops = &radio_ioctl_ops,
  2077. };
  2078. /******************************** usb interface ******************************/
  2079. static struct video_device *em28xx_vdev_init(struct em28xx *dev,
  2080. const struct video_device *template,
  2081. const char *type_name)
  2082. {
  2083. struct video_device *vfd;
  2084. vfd = video_device_alloc();
  2085. if (NULL == vfd)
  2086. return NULL;
  2087. *vfd = *template;
  2088. vfd->v4l2_dev = &dev->v4l2_dev;
  2089. vfd->release = video_device_release;
  2090. vfd->debug = video_debug;
  2091. vfd->lock = &dev->lock;
  2092. snprintf(vfd->name, sizeof(vfd->name), "%s %s",
  2093. dev->name, type_name);
  2094. video_set_drvdata(vfd, dev);
  2095. return vfd;
  2096. }
  2097. int em28xx_register_analog_devices(struct em28xx *dev)
  2098. {
  2099. u8 val;
  2100. int ret;
  2101. unsigned int maxw;
  2102. printk(KERN_INFO "%s: v4l2 driver version %s\n",
  2103. dev->name, EM28XX_VERSION);
  2104. /* set default norm */
  2105. dev->norm = em28xx_video_template.current_norm;
  2106. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
  2107. dev->interlaced = EM28XX_INTERLACED_DEFAULT;
  2108. /* Analog specific initialization */
  2109. dev->format = &format[0];
  2110. maxw = norm_maxw(dev);
  2111. /* MaxPacketSize for em2800 is too small to capture at full resolution
  2112. * use half of maxw as the scaler can only scale to 50% */
  2113. if (dev->board.is_em2800)
  2114. maxw /= 2;
  2115. em28xx_set_video_format(dev, format[0].fourcc,
  2116. maxw, norm_maxh(dev));
  2117. video_mux(dev, 0);
  2118. /* Audio defaults */
  2119. dev->mute = 1;
  2120. dev->volume = 0x1f;
  2121. /* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
  2122. val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
  2123. em28xx_write_reg(dev, EM28XX_R0F_XCLK,
  2124. (EM28XX_XCLK_AUDIO_UNMUTE | val));
  2125. em28xx_set_outfmt(dev);
  2126. em28xx_colorlevels_set_default(dev);
  2127. em28xx_compression_disable(dev);
  2128. /* allocate and fill video video_device struct */
  2129. dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
  2130. if (!dev->vdev) {
  2131. em28xx_errdev("cannot allocate video_device.\n");
  2132. return -ENODEV;
  2133. }
  2134. /* register v4l2 video video_device */
  2135. ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
  2136. video_nr[dev->devno]);
  2137. if (ret) {
  2138. em28xx_errdev("unable to register video device (error=%i).\n",
  2139. ret);
  2140. return ret;
  2141. }
  2142. /* Allocate and fill vbi video_device struct */
  2143. if (em28xx_vbi_supported(dev) == 1) {
  2144. dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
  2145. "vbi");
  2146. /* register v4l2 vbi video_device */
  2147. ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
  2148. vbi_nr[dev->devno]);
  2149. if (ret < 0) {
  2150. em28xx_errdev("unable to register vbi device\n");
  2151. return ret;
  2152. }
  2153. }
  2154. if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
  2155. dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
  2156. "radio");
  2157. if (!dev->radio_dev) {
  2158. em28xx_errdev("cannot allocate video_device.\n");
  2159. return -ENODEV;
  2160. }
  2161. ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
  2162. radio_nr[dev->devno]);
  2163. if (ret < 0) {
  2164. em28xx_errdev("can't register radio device\n");
  2165. return ret;
  2166. }
  2167. em28xx_info("Registered radio device as %s\n",
  2168. video_device_node_name(dev->radio_dev));
  2169. }
  2170. em28xx_info("V4L2 video device registered as %s\n",
  2171. video_device_node_name(dev->vdev));
  2172. if (dev->vbi_dev)
  2173. em28xx_info("V4L2 VBI device registered as %s\n",
  2174. video_device_node_name(dev->vbi_dev));
  2175. return 0;
  2176. }