em28xx-video.c 59 KB

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