em28xx-video.c 64 KB

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