em28xx-video.c 64 KB

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