em28xx-video.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066
  1. /*
  2. em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB
  3. video capture devices
  4. Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
  5. Markus Rechberger <mrechberger@gmail.com>
  6. Mauro Carvalho Chehab <mchehab@infradead.org>
  7. Sascha Sommer <saschasommer@freenet.de>
  8. Some parts based on SN9C10x PC Camera Controllers GPL driver made
  9. by Luca Risolia <luca.risolia@studio.unibo.it>
  10. This program is free software; you can redistribute it and/or modify
  11. it under the terms of the GNU General Public License as published by
  12. the Free Software Foundation; either version 2 of the License, or
  13. (at your option) any later version.
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. GNU General Public License for more details.
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <linux/init.h>
  23. #include <linux/list.h>
  24. #include <linux/module.h>
  25. #include <linux/kernel.h>
  26. #include <linux/bitmap.h>
  27. #include <linux/usb.h>
  28. #include <linux/i2c.h>
  29. #include <linux/version.h>
  30. #include <linux/mm.h>
  31. #include <linux/mutex.h>
  32. #include "em28xx.h"
  33. #include <media/v4l2-common.h>
  34. #include <media/v4l2-ioctl.h>
  35. #include <media/v4l2-chip-ident.h>
  36. #include <media/msp3400.h>
  37. #include <media/tuner.h>
  38. #define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
  39. "Markus Rechberger <mrechberger@gmail.com>, " \
  40. "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
  41. "Sascha Sommer <saschasommer@freenet.de>"
  42. #define DRIVER_DESC "Empia em28xx based USB video device driver"
  43. #define EM28XX_VERSION_CODE KERNEL_VERSION(0, 1, 2)
  44. #define em28xx_videodbg(fmt, arg...) do {\
  45. if (video_debug) \
  46. printk(KERN_INFO "%s %s :"fmt, \
  47. dev->name, __func__ , ##arg); } while (0)
  48. static unsigned int isoc_debug;
  49. module_param(isoc_debug, int, 0644);
  50. MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
  51. #define em28xx_isocdbg(fmt, arg...) \
  52. do {\
  53. if (isoc_debug) { \
  54. printk(KERN_INFO "%s %s :"fmt, \
  55. dev->name, __func__ , ##arg); \
  56. } \
  57. } while (0)
  58. MODULE_AUTHOR(DRIVER_AUTHOR);
  59. MODULE_DESCRIPTION(DRIVER_DESC);
  60. MODULE_LICENSE("GPL");
  61. static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
  62. static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
  63. static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
  64. module_param_array(video_nr, int, NULL, 0444);
  65. module_param_array(vbi_nr, int, NULL, 0444);
  66. module_param_array(radio_nr, int, NULL, 0444);
  67. MODULE_PARM_DESC(video_nr, "video device numbers");
  68. MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
  69. MODULE_PARM_DESC(radio_nr, "radio device numbers");
  70. static unsigned int video_debug;
  71. module_param(video_debug, int, 0644);
  72. MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
  73. /* supported video standards */
  74. static struct em28xx_fmt format[] = {
  75. {
  76. .name = "16 bpp YUY2, 4:2:2, packed",
  77. .fourcc = V4L2_PIX_FMT_YUYV,
  78. .depth = 16,
  79. .reg = EM28XX_OUTFMT_YUV422_Y0UY1V,
  80. }, {
  81. .name = "16 bpp RGB 565, LE",
  82. .fourcc = V4L2_PIX_FMT_RGB565,
  83. .depth = 16,
  84. .reg = EM28XX_OUTFMT_RGB_16_656,
  85. }, {
  86. .name = "8 bpp Bayer BGBG..GRGR",
  87. .fourcc = V4L2_PIX_FMT_SBGGR8,
  88. .depth = 8,
  89. .reg = EM28XX_OUTFMT_RGB_8_BGBG,
  90. }, {
  91. .name = "8 bpp Bayer GRGR..BGBG",
  92. .fourcc = V4L2_PIX_FMT_SGRBG8,
  93. .depth = 8,
  94. .reg = EM28XX_OUTFMT_RGB_8_GRGR,
  95. }, {
  96. .name = "8 bpp Bayer GBGB..RGRG",
  97. .fourcc = V4L2_PIX_FMT_SGBRG8,
  98. .depth = 8,
  99. .reg = EM28XX_OUTFMT_RGB_8_GBGB,
  100. }, {
  101. .name = "12 bpp YUV411",
  102. .fourcc = V4L2_PIX_FMT_YUV411P,
  103. .depth = 12,
  104. .reg = EM28XX_OUTFMT_YUV411,
  105. },
  106. };
  107. /* supported controls */
  108. /* Common to all boards */
  109. static struct v4l2_queryctrl em28xx_qctrl[] = {
  110. {
  111. .id = V4L2_CID_AUDIO_VOLUME,
  112. .type = V4L2_CTRL_TYPE_INTEGER,
  113. .name = "Volume",
  114. .minimum = 0x0,
  115. .maximum = 0x1f,
  116. .step = 0x1,
  117. .default_value = 0x1f,
  118. .flags = 0,
  119. }, {
  120. .id = V4L2_CID_AUDIO_MUTE,
  121. .type = V4L2_CTRL_TYPE_BOOLEAN,
  122. .name = "Mute",
  123. .minimum = 0,
  124. .maximum = 1,
  125. .step = 1,
  126. .default_value = 1,
  127. .flags = 0,
  128. }
  129. };
  130. /* ------------------------------------------------------------------
  131. DMA and thread functions
  132. ------------------------------------------------------------------*/
  133. /*
  134. * Announces that a buffer were filled and request the next
  135. */
  136. static inline void buffer_filled(struct em28xx *dev,
  137. struct em28xx_dmaqueue *dma_q,
  138. struct em28xx_buffer *buf)
  139. {
  140. /* Advice that buffer was filled */
  141. em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
  142. buf->vb.state = VIDEOBUF_DONE;
  143. buf->vb.field_count++;
  144. do_gettimeofday(&buf->vb.ts);
  145. dev->isoc_ctl.buf = NULL;
  146. list_del(&buf->vb.queue);
  147. wake_up(&buf->vb.done);
  148. }
  149. /*
  150. * Identify the buffer header type and properly handles
  151. */
  152. static void em28xx_copy_video(struct em28xx *dev,
  153. struct em28xx_dmaqueue *dma_q,
  154. struct em28xx_buffer *buf,
  155. unsigned char *p,
  156. unsigned char *outp, unsigned long len)
  157. {
  158. void *fieldstart, *startwrite, *startread;
  159. int linesdone, currlinedone, offset, lencopy, remain;
  160. int bytesperline = dev->width << 1;
  161. if (dma_q->pos + len > buf->vb.size)
  162. len = buf->vb.size - dma_q->pos;
  163. if (p[0] != 0x88 && p[0] != 0x22) {
  164. em28xx_isocdbg("frame is not complete\n");
  165. len += 4;
  166. } else
  167. p += 4;
  168. startread = p;
  169. remain = len;
  170. /* Interlaces frame */
  171. if (buf->top_field)
  172. fieldstart = outp;
  173. else
  174. fieldstart = outp + bytesperline;
  175. linesdone = dma_q->pos / bytesperline;
  176. currlinedone = dma_q->pos % bytesperline;
  177. offset = linesdone * bytesperline * 2 + currlinedone;
  178. startwrite = fieldstart + offset;
  179. lencopy = bytesperline - currlinedone;
  180. lencopy = lencopy > remain ? remain : lencopy;
  181. if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
  182. em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
  183. ((char *)startwrite + lencopy) -
  184. ((char *)outp + buf->vb.size));
  185. remain = (char *)outp + buf->vb.size - (char *)startwrite;
  186. lencopy = remain;
  187. }
  188. if (lencopy <= 0)
  189. return;
  190. memcpy(startwrite, startread, lencopy);
  191. remain -= lencopy;
  192. while (remain > 0) {
  193. startwrite += lencopy + bytesperline;
  194. startread += lencopy;
  195. if (bytesperline > remain)
  196. lencopy = remain;
  197. else
  198. lencopy = bytesperline;
  199. if ((char *)startwrite + lencopy > (char *)outp +
  200. buf->vb.size) {
  201. em28xx_isocdbg("Overflow of %zi bytes past buffer end (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. dma_q->pos += len;
  213. }
  214. static inline void print_err_status(struct em28xx *dev,
  215. int packet, int status)
  216. {
  217. char *errmsg = "Unknown";
  218. switch (status) {
  219. case -ENOENT:
  220. errmsg = "unlinked synchronuously";
  221. break;
  222. case -ECONNRESET:
  223. errmsg = "unlinked asynchronuously";
  224. break;
  225. case -ENOSR:
  226. errmsg = "Buffer error (overrun)";
  227. break;
  228. case -EPIPE:
  229. errmsg = "Stalled (device not responding)";
  230. break;
  231. case -EOVERFLOW:
  232. errmsg = "Babble (bad cable?)";
  233. break;
  234. case -EPROTO:
  235. errmsg = "Bit-stuff error (bad cable?)";
  236. break;
  237. case -EILSEQ:
  238. errmsg = "CRC/Timeout (could be anything)";
  239. break;
  240. case -ETIME:
  241. errmsg = "Device does not respond";
  242. break;
  243. }
  244. if (packet < 0) {
  245. em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
  246. } else {
  247. em28xx_isocdbg("URB packet %d, status %d [%s].\n",
  248. packet, status, errmsg);
  249. }
  250. }
  251. /*
  252. * video-buf generic routine to get the next available buffer
  253. */
  254. static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
  255. struct em28xx_buffer **buf)
  256. {
  257. struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
  258. char *outp;
  259. if (list_empty(&dma_q->active)) {
  260. em28xx_isocdbg("No active queue to serve\n");
  261. dev->isoc_ctl.buf = NULL;
  262. *buf = NULL;
  263. return;
  264. }
  265. /* Get the next buffer */
  266. *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
  267. /* Cleans up buffer - Usefull for testing for frame/URB loss */
  268. outp = videobuf_to_vmalloc(&(*buf)->vb);
  269. memset(outp, 0, (*buf)->vb.size);
  270. dev->isoc_ctl.buf = *buf;
  271. return;
  272. }
  273. /*
  274. * Controls the isoc copy of each urb packet
  275. */
  276. static inline int em28xx_isoc_copy(struct em28xx *dev, struct urb *urb)
  277. {
  278. struct em28xx_buffer *buf;
  279. struct em28xx_dmaqueue *dma_q = urb->context;
  280. unsigned char *outp = NULL;
  281. int i, len = 0, rc = 1;
  282. unsigned char *p;
  283. if (!dev)
  284. return 0;
  285. if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
  286. return 0;
  287. if (urb->status < 0) {
  288. print_err_status(dev, -1, urb->status);
  289. if (urb->status == -ENOENT)
  290. return 0;
  291. }
  292. buf = dev->isoc_ctl.buf;
  293. if (buf != NULL)
  294. outp = videobuf_to_vmalloc(&buf->vb);
  295. for (i = 0; i < urb->number_of_packets; i++) {
  296. int status = urb->iso_frame_desc[i].status;
  297. if (status < 0) {
  298. print_err_status(dev, i, status);
  299. if (urb->iso_frame_desc[i].status != -EPROTO)
  300. continue;
  301. }
  302. len = urb->iso_frame_desc[i].actual_length - 4;
  303. if (urb->iso_frame_desc[i].actual_length <= 0) {
  304. /* em28xx_isocdbg("packet %d is empty",i); - spammy */
  305. continue;
  306. }
  307. if (urb->iso_frame_desc[i].actual_length >
  308. dev->max_pkt_size) {
  309. em28xx_isocdbg("packet bigger than packet size");
  310. continue;
  311. }
  312. p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  313. /* FIXME: incomplete buffer checks where removed to make
  314. logic simpler. Impacts of those changes should be evaluated
  315. */
  316. if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
  317. em28xx_isocdbg("VBI HEADER!!!\n");
  318. /* FIXME: Should add vbi copy */
  319. continue;
  320. }
  321. if (p[0] == 0x22 && p[1] == 0x5a) {
  322. em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
  323. len, (p[2] & 1) ? "odd" : "even");
  324. if (!(p[2] & 1)) {
  325. if (buf != NULL)
  326. buffer_filled(dev, dma_q, buf);
  327. get_next_buf(dma_q, &buf);
  328. if (buf == NULL)
  329. outp = NULL;
  330. else
  331. outp = videobuf_to_vmalloc(&buf->vb);
  332. }
  333. if (buf != NULL) {
  334. if (p[2] & 1)
  335. buf->top_field = 0;
  336. else
  337. buf->top_field = 1;
  338. }
  339. dma_q->pos = 0;
  340. }
  341. if (buf != NULL)
  342. em28xx_copy_video(dev, dma_q, buf, p, outp, len);
  343. }
  344. return rc;
  345. }
  346. /* ------------------------------------------------------------------
  347. Videobuf operations
  348. ------------------------------------------------------------------*/
  349. static int
  350. buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
  351. {
  352. struct em28xx_fh *fh = vq->priv_data;
  353. struct em28xx *dev = fh->dev;
  354. struct v4l2_frequency f;
  355. *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7) >> 3;
  356. if (0 == *count)
  357. *count = EM28XX_DEF_BUF;
  358. if (*count < EM28XX_MIN_BUF)
  359. *count = EM28XX_MIN_BUF;
  360. /* Ask tuner to go to analog or radio mode */
  361. memset(&f, 0, sizeof(f));
  362. f.frequency = dev->ctl_freq;
  363. f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  364. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
  365. return 0;
  366. }
  367. /* This is called *without* dev->slock held; please keep it that way */
  368. static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
  369. {
  370. struct em28xx_fh *fh = vq->priv_data;
  371. struct em28xx *dev = fh->dev;
  372. unsigned long flags = 0;
  373. if (in_interrupt())
  374. BUG();
  375. /* We used to wait for the buffer to finish here, but this didn't work
  376. because, as we were keeping the state as VIDEOBUF_QUEUED,
  377. videobuf_queue_cancel marked it as finished for us.
  378. (Also, it could wedge forever if the hardware was misconfigured.)
  379. This should be safe; by the time we get here, the buffer isn't
  380. queued anymore. If we ever start marking the buffers as
  381. VIDEOBUF_ACTIVE, it won't be, though.
  382. */
  383. spin_lock_irqsave(&dev->slock, flags);
  384. if (dev->isoc_ctl.buf == buf)
  385. dev->isoc_ctl.buf = NULL;
  386. spin_unlock_irqrestore(&dev->slock, flags);
  387. videobuf_vmalloc_free(&buf->vb);
  388. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  389. }
  390. static int
  391. buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
  392. enum v4l2_field field)
  393. {
  394. struct em28xx_fh *fh = vq->priv_data;
  395. struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
  396. struct em28xx *dev = fh->dev;
  397. int rc = 0, urb_init = 0;
  398. buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth + 7) >> 3;
  399. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  400. return -EINVAL;
  401. buf->vb.width = dev->width;
  402. buf->vb.height = dev->height;
  403. buf->vb.field = field;
  404. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  405. rc = videobuf_iolock(vq, &buf->vb, NULL);
  406. if (rc < 0)
  407. goto fail;
  408. }
  409. if (!dev->isoc_ctl.num_bufs)
  410. urb_init = 1;
  411. if (urb_init) {
  412. rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
  413. EM28XX_NUM_BUFS, dev->max_pkt_size,
  414. em28xx_isoc_copy);
  415. if (rc < 0)
  416. goto fail;
  417. }
  418. buf->vb.state = VIDEOBUF_PREPARED;
  419. return 0;
  420. fail:
  421. free_buffer(vq, buf);
  422. return rc;
  423. }
  424. static void
  425. buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  426. {
  427. struct em28xx_buffer *buf = container_of(vb,
  428. struct em28xx_buffer,
  429. vb);
  430. struct em28xx_fh *fh = vq->priv_data;
  431. struct em28xx *dev = fh->dev;
  432. struct em28xx_dmaqueue *vidq = &dev->vidq;
  433. buf->vb.state = VIDEOBUF_QUEUED;
  434. list_add_tail(&buf->vb.queue, &vidq->active);
  435. }
  436. static void buffer_release(struct videobuf_queue *vq,
  437. struct videobuf_buffer *vb)
  438. {
  439. struct em28xx_buffer *buf = container_of(vb,
  440. struct em28xx_buffer,
  441. vb);
  442. struct em28xx_fh *fh = vq->priv_data;
  443. struct em28xx *dev = (struct em28xx *)fh->dev;
  444. em28xx_isocdbg("em28xx: called buffer_release\n");
  445. free_buffer(vq, buf);
  446. }
  447. static struct videobuf_queue_ops em28xx_video_qops = {
  448. .buf_setup = buffer_setup,
  449. .buf_prepare = buffer_prepare,
  450. .buf_queue = buffer_queue,
  451. .buf_release = buffer_release,
  452. };
  453. /********************* v4l2 interface **************************************/
  454. static void video_mux(struct em28xx *dev, int index)
  455. {
  456. dev->ctl_input = index;
  457. dev->ctl_ainput = INPUT(index)->amux;
  458. dev->ctl_aoutput = INPUT(index)->aout;
  459. if (!dev->ctl_aoutput)
  460. dev->ctl_aoutput = EM28XX_AOUT_MASTER;
  461. v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
  462. INPUT(index)->vmux, 0, 0);
  463. if (dev->board.has_msp34xx) {
  464. if (dev->i2s_speed) {
  465. v4l2_device_call_all(&dev->v4l2_dev, 0, audio,
  466. s_i2s_clock_freq, dev->i2s_speed);
  467. }
  468. /* Note: this is msp3400 specific */
  469. v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
  470. dev->ctl_ainput, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
  471. }
  472. if (dev->board.adecoder != EM28XX_NOADECODER) {
  473. v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
  474. dev->ctl_ainput, dev->ctl_aoutput, 0);
  475. }
  476. em28xx_audio_analog_set(dev);
  477. }
  478. /* Usage lock check functions */
  479. static int res_get(struct em28xx_fh *fh)
  480. {
  481. struct em28xx *dev = fh->dev;
  482. int rc = 0;
  483. /* This instance already has stream_on */
  484. if (fh->stream_on)
  485. return rc;
  486. if (dev->stream_on)
  487. return -EBUSY;
  488. dev->stream_on = 1;
  489. fh->stream_on = 1;
  490. return rc;
  491. }
  492. static int res_check(struct em28xx_fh *fh)
  493. {
  494. return fh->stream_on;
  495. }
  496. static void res_free(struct em28xx_fh *fh)
  497. {
  498. struct em28xx *dev = fh->dev;
  499. fh->stream_on = 0;
  500. dev->stream_on = 0;
  501. }
  502. /*
  503. * em28xx_get_ctrl()
  504. * return the current saturation, brightness or contrast, mute state
  505. */
  506. static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
  507. {
  508. switch (ctrl->id) {
  509. case V4L2_CID_AUDIO_MUTE:
  510. ctrl->value = dev->mute;
  511. return 0;
  512. case V4L2_CID_AUDIO_VOLUME:
  513. ctrl->value = dev->volume;
  514. return 0;
  515. default:
  516. return -EINVAL;
  517. }
  518. }
  519. /*
  520. * em28xx_set_ctrl()
  521. * mute or set new saturation, brightness or contrast
  522. */
  523. static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
  524. {
  525. switch (ctrl->id) {
  526. case V4L2_CID_AUDIO_MUTE:
  527. if (ctrl->value != dev->mute) {
  528. dev->mute = ctrl->value;
  529. return em28xx_audio_analog_set(dev);
  530. }
  531. return 0;
  532. case V4L2_CID_AUDIO_VOLUME:
  533. dev->volume = ctrl->value;
  534. return em28xx_audio_analog_set(dev);
  535. default:
  536. return -EINVAL;
  537. }
  538. }
  539. static int check_dev(struct em28xx *dev)
  540. {
  541. if (dev->state & DEV_DISCONNECTED) {
  542. em28xx_errdev("v4l2 ioctl: device not present\n");
  543. return -ENODEV;
  544. }
  545. if (dev->state & DEV_MISCONFIGURED) {
  546. em28xx_errdev("v4l2 ioctl: device is misconfigured; "
  547. "close and open it again\n");
  548. return -EIO;
  549. }
  550. return 0;
  551. }
  552. static void get_scale(struct em28xx *dev,
  553. unsigned int width, unsigned int height,
  554. unsigned int *hscale, unsigned int *vscale)
  555. {
  556. unsigned int maxw = norm_maxw(dev);
  557. unsigned int maxh = norm_maxh(dev);
  558. *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
  559. if (*hscale >= 0x4000)
  560. *hscale = 0x3fff;
  561. *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
  562. if (*vscale >= 0x4000)
  563. *vscale = 0x3fff;
  564. }
  565. /* ------------------------------------------------------------------
  566. IOCTL vidioc handling
  567. ------------------------------------------------------------------*/
  568. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  569. struct v4l2_format *f)
  570. {
  571. struct em28xx_fh *fh = priv;
  572. struct em28xx *dev = fh->dev;
  573. mutex_lock(&dev->lock);
  574. f->fmt.pix.width = dev->width;
  575. f->fmt.pix.height = dev->height;
  576. f->fmt.pix.pixelformat = dev->format->fourcc;
  577. f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
  578. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
  579. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  580. /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
  581. f->fmt.pix.field = dev->interlaced ?
  582. V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
  583. mutex_unlock(&dev->lock);
  584. return 0;
  585. }
  586. static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
  587. {
  588. unsigned int i;
  589. for (i = 0; i < ARRAY_SIZE(format); i++)
  590. if (format[i].fourcc == fourcc)
  591. return &format[i];
  592. return NULL;
  593. }
  594. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  595. struct v4l2_format *f)
  596. {
  597. struct em28xx_fh *fh = priv;
  598. struct em28xx *dev = fh->dev;
  599. unsigned int width = f->fmt.pix.width;
  600. unsigned int height = f->fmt.pix.height;
  601. unsigned int maxw = norm_maxw(dev);
  602. unsigned int maxh = norm_maxh(dev);
  603. unsigned int hscale, vscale;
  604. struct em28xx_fmt *fmt;
  605. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  606. if (!fmt) {
  607. em28xx_videodbg("Fourcc format (%08x) invalid.\n",
  608. f->fmt.pix.pixelformat);
  609. return -EINVAL;
  610. }
  611. if (dev->board.is_em2800) {
  612. /* the em2800 can only scale down to 50% */
  613. height = height > (3 * maxh / 4) ? maxh : maxh / 2;
  614. width = width > (3 * maxw / 4) ? maxw : maxw / 2;
  615. /* According to empiatech support the MaxPacketSize is too small
  616. * to support framesizes larger than 640x480 @ 30 fps or 640x576
  617. * @ 25 fps. As this would cut of a part of the image we prefer
  618. * 360x576 or 360x480 for now */
  619. if (width == maxw && height == maxh)
  620. width /= 2;
  621. } else {
  622. /* width must even because of the YUYV format
  623. height must be even because of interlacing */
  624. v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh, 1, 0);
  625. }
  626. get_scale(dev, width, height, &hscale, &vscale);
  627. width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
  628. height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
  629. f->fmt.pix.width = width;
  630. f->fmt.pix.height = height;
  631. f->fmt.pix.pixelformat = fmt->fourcc;
  632. f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
  633. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
  634. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  635. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  636. return 0;
  637. }
  638. static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
  639. unsigned width, unsigned height)
  640. {
  641. struct em28xx_fmt *fmt;
  642. fmt = format_by_fourcc(fourcc);
  643. if (!fmt)
  644. return -EINVAL;
  645. dev->format = fmt;
  646. dev->width = width;
  647. dev->height = height;
  648. /* set new image size */
  649. get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
  650. em28xx_set_alternate(dev);
  651. em28xx_resolution_set(dev);
  652. return 0;
  653. }
  654. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  655. struct v4l2_format *f)
  656. {
  657. struct em28xx_fh *fh = priv;
  658. struct em28xx *dev = fh->dev;
  659. int rc;
  660. rc = check_dev(dev);
  661. if (rc < 0)
  662. return rc;
  663. mutex_lock(&dev->lock);
  664. vidioc_try_fmt_vid_cap(file, priv, f);
  665. if (videobuf_queue_is_busy(&fh->vb_vidq)) {
  666. em28xx_errdev("%s queue busy\n", __func__);
  667. rc = -EBUSY;
  668. goto out;
  669. }
  670. if (dev->stream_on && !fh->stream_on) {
  671. em28xx_errdev("%s device in use by another fh\n", __func__);
  672. rc = -EBUSY;
  673. goto out;
  674. }
  675. rc = em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
  676. f->fmt.pix.width, f->fmt.pix.height);
  677. out:
  678. mutex_unlock(&dev->lock);
  679. return rc;
  680. }
  681. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
  682. {
  683. struct em28xx_fh *fh = priv;
  684. struct em28xx *dev = fh->dev;
  685. struct v4l2_format f;
  686. int rc;
  687. rc = check_dev(dev);
  688. if (rc < 0)
  689. return rc;
  690. mutex_lock(&dev->lock);
  691. dev->norm = *norm;
  692. /* Adjusts width/height, if needed */
  693. f.fmt.pix.width = dev->width;
  694. f.fmt.pix.height = dev->height;
  695. vidioc_try_fmt_vid_cap(file, priv, &f);
  696. /* set new image size */
  697. dev->width = f.fmt.pix.width;
  698. dev->height = f.fmt.pix.height;
  699. get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
  700. em28xx_resolution_set(dev);
  701. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
  702. mutex_unlock(&dev->lock);
  703. return 0;
  704. }
  705. static const char *iname[] = {
  706. [EM28XX_VMUX_COMPOSITE1] = "Composite1",
  707. [EM28XX_VMUX_COMPOSITE2] = "Composite2",
  708. [EM28XX_VMUX_COMPOSITE3] = "Composite3",
  709. [EM28XX_VMUX_COMPOSITE4] = "Composite4",
  710. [EM28XX_VMUX_SVIDEO] = "S-Video",
  711. [EM28XX_VMUX_TELEVISION] = "Television",
  712. [EM28XX_VMUX_CABLE] = "Cable TV",
  713. [EM28XX_VMUX_DVB] = "DVB",
  714. [EM28XX_VMUX_DEBUG] = "for debug only",
  715. };
  716. static int vidioc_enum_input(struct file *file, void *priv,
  717. struct v4l2_input *i)
  718. {
  719. struct em28xx_fh *fh = priv;
  720. struct em28xx *dev = fh->dev;
  721. unsigned int n;
  722. n = i->index;
  723. if (n >= MAX_EM28XX_INPUT)
  724. return -EINVAL;
  725. if (0 == INPUT(n)->type)
  726. return -EINVAL;
  727. i->index = n;
  728. i->type = V4L2_INPUT_TYPE_CAMERA;
  729. strcpy(i->name, iname[INPUT(n)->type]);
  730. if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
  731. (EM28XX_VMUX_CABLE == INPUT(n)->type))
  732. i->type = V4L2_INPUT_TYPE_TUNER;
  733. i->std = dev->vdev->tvnorms;
  734. return 0;
  735. }
  736. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  737. {
  738. struct em28xx_fh *fh = priv;
  739. struct em28xx *dev = fh->dev;
  740. *i = dev->ctl_input;
  741. return 0;
  742. }
  743. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  744. {
  745. struct em28xx_fh *fh = priv;
  746. struct em28xx *dev = fh->dev;
  747. int rc;
  748. rc = check_dev(dev);
  749. if (rc < 0)
  750. return rc;
  751. if (i >= MAX_EM28XX_INPUT)
  752. return -EINVAL;
  753. if (0 == INPUT(i)->type)
  754. return -EINVAL;
  755. dev->ctl_input = i;
  756. mutex_lock(&dev->lock);
  757. video_mux(dev, dev->ctl_input);
  758. mutex_unlock(&dev->lock);
  759. return 0;
  760. }
  761. static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  762. {
  763. struct em28xx_fh *fh = priv;
  764. struct em28xx *dev = fh->dev;
  765. switch (a->index) {
  766. case EM28XX_AMUX_VIDEO:
  767. strcpy(a->name, "Television");
  768. break;
  769. case EM28XX_AMUX_LINE_IN:
  770. strcpy(a->name, "Line In");
  771. break;
  772. case EM28XX_AMUX_VIDEO2:
  773. strcpy(a->name, "Television alt");
  774. break;
  775. case EM28XX_AMUX_PHONE:
  776. strcpy(a->name, "Phone");
  777. break;
  778. case EM28XX_AMUX_MIC:
  779. strcpy(a->name, "Mic");
  780. break;
  781. case EM28XX_AMUX_CD:
  782. strcpy(a->name, "CD");
  783. break;
  784. case EM28XX_AMUX_AUX:
  785. strcpy(a->name, "Aux");
  786. break;
  787. case EM28XX_AMUX_PCM_OUT:
  788. strcpy(a->name, "PCM");
  789. break;
  790. default:
  791. return -EINVAL;
  792. }
  793. a->index = dev->ctl_ainput;
  794. a->capability = V4L2_AUDCAP_STEREO;
  795. return 0;
  796. }
  797. static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
  798. {
  799. struct em28xx_fh *fh = priv;
  800. struct em28xx *dev = fh->dev;
  801. if (a->index >= MAX_EM28XX_INPUT)
  802. return -EINVAL;
  803. if (0 == INPUT(a->index)->type)
  804. return -EINVAL;
  805. mutex_lock(&dev->lock);
  806. dev->ctl_ainput = INPUT(a->index)->amux;
  807. dev->ctl_aoutput = INPUT(a->index)->aout;
  808. if (!dev->ctl_aoutput)
  809. dev->ctl_aoutput = EM28XX_AOUT_MASTER;
  810. mutex_unlock(&dev->lock);
  811. return 0;
  812. }
  813. static int vidioc_queryctrl(struct file *file, void *priv,
  814. struct v4l2_queryctrl *qc)
  815. {
  816. struct em28xx_fh *fh = priv;
  817. struct em28xx *dev = fh->dev;
  818. int id = qc->id;
  819. int i;
  820. int rc;
  821. rc = check_dev(dev);
  822. if (rc < 0)
  823. return rc;
  824. memset(qc, 0, sizeof(*qc));
  825. qc->id = id;
  826. if (!dev->board.has_msp34xx) {
  827. for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
  828. if (qc->id && qc->id == em28xx_qctrl[i].id) {
  829. memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
  830. return 0;
  831. }
  832. }
  833. }
  834. mutex_lock(&dev->lock);
  835. v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
  836. mutex_unlock(&dev->lock);
  837. if (qc->type)
  838. return 0;
  839. else
  840. return -EINVAL;
  841. }
  842. static int vidioc_g_ctrl(struct file *file, void *priv,
  843. struct v4l2_control *ctrl)
  844. {
  845. struct em28xx_fh *fh = priv;
  846. struct em28xx *dev = fh->dev;
  847. int rc;
  848. rc = check_dev(dev);
  849. if (rc < 0)
  850. return rc;
  851. rc = 0;
  852. mutex_lock(&dev->lock);
  853. if (dev->board.has_msp34xx)
  854. v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
  855. else {
  856. rc = em28xx_get_ctrl(dev, ctrl);
  857. if (rc < 0) {
  858. v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
  859. rc = 0;
  860. }
  861. }
  862. mutex_unlock(&dev->lock);
  863. return rc;
  864. }
  865. static int vidioc_s_ctrl(struct file *file, void *priv,
  866. struct v4l2_control *ctrl)
  867. {
  868. struct em28xx_fh *fh = priv;
  869. struct em28xx *dev = fh->dev;
  870. u8 i;
  871. int rc;
  872. rc = check_dev(dev);
  873. if (rc < 0)
  874. return rc;
  875. mutex_lock(&dev->lock);
  876. if (dev->board.has_msp34xx)
  877. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_ctrl, ctrl);
  878. else {
  879. rc = 1;
  880. for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
  881. if (ctrl->id == em28xx_qctrl[i].id) {
  882. if (ctrl->value < em28xx_qctrl[i].minimum ||
  883. ctrl->value > em28xx_qctrl[i].maximum) {
  884. rc = -ERANGE;
  885. break;
  886. }
  887. rc = em28xx_set_ctrl(dev, ctrl);
  888. break;
  889. }
  890. }
  891. }
  892. /* Control not found - try to send it to the attached devices */
  893. if (rc == 1) {
  894. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_ctrl, ctrl);
  895. rc = 0;
  896. }
  897. mutex_unlock(&dev->lock);
  898. return rc;
  899. }
  900. static int vidioc_g_tuner(struct file *file, void *priv,
  901. struct v4l2_tuner *t)
  902. {
  903. struct em28xx_fh *fh = priv;
  904. struct em28xx *dev = fh->dev;
  905. int rc;
  906. rc = check_dev(dev);
  907. if (rc < 0)
  908. return rc;
  909. if (0 != t->index)
  910. return -EINVAL;
  911. strcpy(t->name, "Tuner");
  912. mutex_lock(&dev->lock);
  913. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
  914. mutex_unlock(&dev->lock);
  915. return 0;
  916. }
  917. static int vidioc_s_tuner(struct file *file, void *priv,
  918. struct v4l2_tuner *t)
  919. {
  920. struct em28xx_fh *fh = priv;
  921. struct em28xx *dev = fh->dev;
  922. int rc;
  923. rc = check_dev(dev);
  924. if (rc < 0)
  925. return rc;
  926. if (0 != t->index)
  927. return -EINVAL;
  928. mutex_lock(&dev->lock);
  929. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
  930. mutex_unlock(&dev->lock);
  931. return 0;
  932. }
  933. static int vidioc_g_frequency(struct file *file, void *priv,
  934. struct v4l2_frequency *f)
  935. {
  936. struct em28xx_fh *fh = priv;
  937. struct em28xx *dev = fh->dev;
  938. mutex_lock(&dev->lock);
  939. f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  940. f->frequency = dev->ctl_freq;
  941. mutex_unlock(&dev->lock);
  942. return 0;
  943. }
  944. static int vidioc_s_frequency(struct file *file, void *priv,
  945. struct v4l2_frequency *f)
  946. {
  947. struct em28xx_fh *fh = priv;
  948. struct em28xx *dev = fh->dev;
  949. int rc;
  950. rc = check_dev(dev);
  951. if (rc < 0)
  952. return rc;
  953. if (0 != f->tuner)
  954. return -EINVAL;
  955. if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
  956. return -EINVAL;
  957. if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
  958. return -EINVAL;
  959. mutex_lock(&dev->lock);
  960. dev->ctl_freq = f->frequency;
  961. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
  962. mutex_unlock(&dev->lock);
  963. return 0;
  964. }
  965. #ifdef CONFIG_VIDEO_ADV_DEBUG
  966. static int em28xx_reg_len(int reg)
  967. {
  968. switch (reg) {
  969. case EM28XX_R40_AC97LSB:
  970. case EM28XX_R30_HSCALELOW:
  971. case EM28XX_R32_VSCALELOW:
  972. return 2;
  973. default:
  974. return 1;
  975. }
  976. }
  977. static int vidioc_g_chip_ident(struct file *file, void *priv,
  978. struct v4l2_dbg_chip_ident *chip)
  979. {
  980. struct em28xx_fh *fh = priv;
  981. struct em28xx *dev = fh->dev;
  982. chip->ident = V4L2_IDENT_NONE;
  983. chip->revision = 0;
  984. v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
  985. return 0;
  986. }
  987. static int vidioc_g_register(struct file *file, void *priv,
  988. struct v4l2_dbg_register *reg)
  989. {
  990. struct em28xx_fh *fh = priv;
  991. struct em28xx *dev = fh->dev;
  992. int ret;
  993. switch (reg->match.type) {
  994. case V4L2_CHIP_MATCH_AC97:
  995. mutex_lock(&dev->lock);
  996. ret = em28xx_read_ac97(dev, reg->reg);
  997. mutex_unlock(&dev->lock);
  998. if (ret < 0)
  999. return ret;
  1000. reg->val = ret;
  1001. reg->size = 1;
  1002. return 0;
  1003. case V4L2_CHIP_MATCH_I2C_DRIVER:
  1004. v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
  1005. return 0;
  1006. case V4L2_CHIP_MATCH_I2C_ADDR:
  1007. /* Not supported yet */
  1008. return -EINVAL;
  1009. default:
  1010. if (!v4l2_chip_match_host(&reg->match))
  1011. return -EINVAL;
  1012. }
  1013. /* Match host */
  1014. reg->size = em28xx_reg_len(reg->reg);
  1015. if (reg->size == 1) {
  1016. mutex_lock(&dev->lock);
  1017. ret = em28xx_read_reg(dev, reg->reg);
  1018. mutex_unlock(&dev->lock);
  1019. if (ret < 0)
  1020. return ret;
  1021. reg->val = ret;
  1022. } else {
  1023. __le16 val = 0;
  1024. mutex_lock(&dev->lock);
  1025. ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
  1026. reg->reg, (char *)&val, 2);
  1027. mutex_unlock(&dev->lock);
  1028. if (ret < 0)
  1029. return ret;
  1030. reg->val = le16_to_cpu(val);
  1031. }
  1032. return 0;
  1033. }
  1034. static int vidioc_s_register(struct file *file, void *priv,
  1035. struct v4l2_dbg_register *reg)
  1036. {
  1037. struct em28xx_fh *fh = priv;
  1038. struct em28xx *dev = fh->dev;
  1039. __le16 buf;
  1040. int rc;
  1041. switch (reg->match.type) {
  1042. case V4L2_CHIP_MATCH_AC97:
  1043. mutex_lock(&dev->lock);
  1044. rc = em28xx_write_ac97(dev, reg->reg, reg->val);
  1045. mutex_unlock(&dev->lock);
  1046. return rc;
  1047. case V4L2_CHIP_MATCH_I2C_DRIVER:
  1048. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
  1049. return 0;
  1050. case V4L2_CHIP_MATCH_I2C_ADDR:
  1051. /* Not supported yet */
  1052. return -EINVAL;
  1053. default:
  1054. if (!v4l2_chip_match_host(&reg->match))
  1055. return -EINVAL;
  1056. }
  1057. /* Match host */
  1058. buf = cpu_to_le16(reg->val);
  1059. mutex_lock(&dev->lock);
  1060. rc = em28xx_write_regs(dev, reg->reg, (char *)&buf,
  1061. em28xx_reg_len(reg->reg));
  1062. mutex_unlock(&dev->lock);
  1063. return rc;
  1064. }
  1065. #endif
  1066. static int vidioc_cropcap(struct file *file, void *priv,
  1067. struct v4l2_cropcap *cc)
  1068. {
  1069. struct em28xx_fh *fh = priv;
  1070. struct em28xx *dev = fh->dev;
  1071. if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1072. return -EINVAL;
  1073. cc->bounds.left = 0;
  1074. cc->bounds.top = 0;
  1075. cc->bounds.width = dev->width;
  1076. cc->bounds.height = dev->height;
  1077. cc->defrect = cc->bounds;
  1078. cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
  1079. cc->pixelaspect.denominator = 59;
  1080. return 0;
  1081. }
  1082. static int vidioc_streamon(struct file *file, void *priv,
  1083. enum v4l2_buf_type type)
  1084. {
  1085. struct em28xx_fh *fh = priv;
  1086. struct em28xx *dev = fh->dev;
  1087. int rc;
  1088. rc = check_dev(dev);
  1089. if (rc < 0)
  1090. return rc;
  1091. mutex_lock(&dev->lock);
  1092. rc = res_get(fh);
  1093. if (likely(rc >= 0))
  1094. rc = videobuf_streamon(&fh->vb_vidq);
  1095. mutex_unlock(&dev->lock);
  1096. return rc;
  1097. }
  1098. static int vidioc_streamoff(struct file *file, void *priv,
  1099. enum v4l2_buf_type type)
  1100. {
  1101. struct em28xx_fh *fh = priv;
  1102. struct em28xx *dev = fh->dev;
  1103. int rc;
  1104. rc = check_dev(dev);
  1105. if (rc < 0)
  1106. return rc;
  1107. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1108. return -EINVAL;
  1109. if (type != fh->type)
  1110. return -EINVAL;
  1111. mutex_lock(&dev->lock);
  1112. videobuf_streamoff(&fh->vb_vidq);
  1113. res_free(fh);
  1114. mutex_unlock(&dev->lock);
  1115. return 0;
  1116. }
  1117. static int vidioc_querycap(struct file *file, void *priv,
  1118. struct v4l2_capability *cap)
  1119. {
  1120. struct em28xx_fh *fh = priv;
  1121. struct em28xx *dev = fh->dev;
  1122. strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
  1123. strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
  1124. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  1125. cap->version = EM28XX_VERSION_CODE;
  1126. cap->capabilities =
  1127. V4L2_CAP_SLICED_VBI_CAPTURE |
  1128. V4L2_CAP_VIDEO_CAPTURE |
  1129. V4L2_CAP_AUDIO |
  1130. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  1131. if (dev->tuner_type != TUNER_ABSENT)
  1132. cap->capabilities |= V4L2_CAP_TUNER;
  1133. return 0;
  1134. }
  1135. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  1136. struct v4l2_fmtdesc *f)
  1137. {
  1138. if (unlikely(f->index >= ARRAY_SIZE(format)))
  1139. return -EINVAL;
  1140. strlcpy(f->description, format[f->index].name, sizeof(f->description));
  1141. f->pixelformat = format[f->index].fourcc;
  1142. return 0;
  1143. }
  1144. /* Sliced VBI ioctls */
  1145. static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
  1146. struct v4l2_format *f)
  1147. {
  1148. struct em28xx_fh *fh = priv;
  1149. struct em28xx *dev = fh->dev;
  1150. int rc;
  1151. rc = check_dev(dev);
  1152. if (rc < 0)
  1153. return rc;
  1154. mutex_lock(&dev->lock);
  1155. f->fmt.sliced.service_set = 0;
  1156. v4l2_device_call_all(&dev->v4l2_dev, 0, video, g_fmt, f);
  1157. if (f->fmt.sliced.service_set == 0)
  1158. rc = -EINVAL;
  1159. mutex_unlock(&dev->lock);
  1160. return rc;
  1161. }
  1162. static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
  1163. struct v4l2_format *f)
  1164. {
  1165. struct em28xx_fh *fh = priv;
  1166. struct em28xx *dev = fh->dev;
  1167. int rc;
  1168. rc = check_dev(dev);
  1169. if (rc < 0)
  1170. return rc;
  1171. mutex_lock(&dev->lock);
  1172. v4l2_device_call_all(&dev->v4l2_dev, 0, video, g_fmt, f);
  1173. mutex_unlock(&dev->lock);
  1174. if (f->fmt.sliced.service_set == 0)
  1175. return -EINVAL;
  1176. return 0;
  1177. }
  1178. static int vidioc_reqbufs(struct file *file, void *priv,
  1179. struct v4l2_requestbuffers *rb)
  1180. {
  1181. struct em28xx_fh *fh = priv;
  1182. struct em28xx *dev = fh->dev;
  1183. int rc;
  1184. rc = check_dev(dev);
  1185. if (rc < 0)
  1186. return rc;
  1187. return videobuf_reqbufs(&fh->vb_vidq, rb);
  1188. }
  1189. static int vidioc_querybuf(struct file *file, void *priv,
  1190. struct v4l2_buffer *b)
  1191. {
  1192. struct em28xx_fh *fh = priv;
  1193. struct em28xx *dev = fh->dev;
  1194. int rc;
  1195. rc = check_dev(dev);
  1196. if (rc < 0)
  1197. return rc;
  1198. return videobuf_querybuf(&fh->vb_vidq, b);
  1199. }
  1200. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1201. {
  1202. struct em28xx_fh *fh = priv;
  1203. struct em28xx *dev = fh->dev;
  1204. int rc;
  1205. rc = check_dev(dev);
  1206. if (rc < 0)
  1207. return rc;
  1208. return videobuf_qbuf(&fh->vb_vidq, b);
  1209. }
  1210. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1211. {
  1212. struct em28xx_fh *fh = priv;
  1213. struct em28xx *dev = fh->dev;
  1214. int rc;
  1215. rc = check_dev(dev);
  1216. if (rc < 0)
  1217. return rc;
  1218. return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK);
  1219. }
  1220. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1221. static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
  1222. {
  1223. struct em28xx_fh *fh = priv;
  1224. return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
  1225. }
  1226. #endif
  1227. /* ----------------------------------------------------------- */
  1228. /* RADIO ESPECIFIC IOCTLS */
  1229. /* ----------------------------------------------------------- */
  1230. static int radio_querycap(struct file *file, void *priv,
  1231. struct v4l2_capability *cap)
  1232. {
  1233. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1234. strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
  1235. strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
  1236. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  1237. cap->version = EM28XX_VERSION_CODE;
  1238. cap->capabilities = V4L2_CAP_TUNER;
  1239. return 0;
  1240. }
  1241. static int radio_g_tuner(struct file *file, void *priv,
  1242. struct v4l2_tuner *t)
  1243. {
  1244. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1245. if (unlikely(t->index > 0))
  1246. return -EINVAL;
  1247. strcpy(t->name, "Radio");
  1248. t->type = V4L2_TUNER_RADIO;
  1249. mutex_lock(&dev->lock);
  1250. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
  1251. mutex_unlock(&dev->lock);
  1252. return 0;
  1253. }
  1254. static int radio_enum_input(struct file *file, void *priv,
  1255. struct v4l2_input *i)
  1256. {
  1257. if (i->index != 0)
  1258. return -EINVAL;
  1259. strcpy(i->name, "Radio");
  1260. i->type = V4L2_INPUT_TYPE_TUNER;
  1261. return 0;
  1262. }
  1263. static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  1264. {
  1265. if (unlikely(a->index))
  1266. return -EINVAL;
  1267. strcpy(a->name, "Radio");
  1268. return 0;
  1269. }
  1270. static int radio_s_tuner(struct file *file, void *priv,
  1271. struct v4l2_tuner *t)
  1272. {
  1273. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1274. if (0 != t->index)
  1275. return -EINVAL;
  1276. mutex_lock(&dev->lock);
  1277. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
  1278. mutex_unlock(&dev->lock);
  1279. return 0;
  1280. }
  1281. static int radio_s_audio(struct file *file, void *fh,
  1282. struct v4l2_audio *a)
  1283. {
  1284. return 0;
  1285. }
  1286. static int radio_s_input(struct file *file, void *fh, unsigned int i)
  1287. {
  1288. return 0;
  1289. }
  1290. static int radio_queryctrl(struct file *file, void *priv,
  1291. struct v4l2_queryctrl *qc)
  1292. {
  1293. int i;
  1294. if (qc->id < V4L2_CID_BASE ||
  1295. qc->id >= V4L2_CID_LASTP1)
  1296. return -EINVAL;
  1297. for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
  1298. if (qc->id && qc->id == em28xx_qctrl[i].id) {
  1299. memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
  1300. return 0;
  1301. }
  1302. }
  1303. return -EINVAL;
  1304. }
  1305. /*
  1306. * em28xx_v4l2_open()
  1307. * inits the device and starts isoc transfer
  1308. */
  1309. static int em28xx_v4l2_open(struct file *filp)
  1310. {
  1311. int minor = video_devdata(filp)->minor;
  1312. int errCode = 0, radio;
  1313. struct em28xx *dev;
  1314. enum v4l2_buf_type fh_type;
  1315. struct em28xx_fh *fh;
  1316. dev = em28xx_get_device(minor, &fh_type, &radio);
  1317. if (NULL == dev)
  1318. return -ENODEV;
  1319. mutex_lock(&dev->lock);
  1320. em28xx_videodbg("open minor=%d type=%s users=%d\n",
  1321. minor, v4l2_type_names[fh_type], dev->users);
  1322. fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
  1323. if (!fh) {
  1324. em28xx_errdev("em28xx-video.c: Out of memory?!\n");
  1325. mutex_unlock(&dev->lock);
  1326. return -ENOMEM;
  1327. }
  1328. fh->dev = dev;
  1329. fh->radio = radio;
  1330. fh->type = fh_type;
  1331. filp->private_data = fh;
  1332. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
  1333. em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
  1334. em28xx_set_alternate(dev);
  1335. em28xx_resolution_set(dev);
  1336. /* Needed, since GPIO might have disabled power of
  1337. some i2c device
  1338. */
  1339. em28xx_wake_i2c(dev);
  1340. }
  1341. if (fh->radio) {
  1342. em28xx_videodbg("video_open: setting radio device\n");
  1343. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
  1344. }
  1345. dev->users++;
  1346. videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
  1347. NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
  1348. sizeof(struct em28xx_buffer), fh);
  1349. mutex_unlock(&dev->lock);
  1350. return errCode;
  1351. }
  1352. /*
  1353. * em28xx_realease_resources()
  1354. * unregisters the v4l2,i2c and usb devices
  1355. * called when the device gets disconected or at module unload
  1356. */
  1357. void em28xx_release_analog_resources(struct em28xx *dev)
  1358. {
  1359. /*FIXME: I2C IR should be disconnected */
  1360. if (dev->radio_dev) {
  1361. if (-1 != dev->radio_dev->minor)
  1362. video_unregister_device(dev->radio_dev);
  1363. else
  1364. video_device_release(dev->radio_dev);
  1365. dev->radio_dev = NULL;
  1366. }
  1367. if (dev->vbi_dev) {
  1368. em28xx_info("V4L2 device /dev/vbi%d deregistered\n",
  1369. dev->vbi_dev->num);
  1370. if (-1 != dev->vbi_dev->minor)
  1371. video_unregister_device(dev->vbi_dev);
  1372. else
  1373. video_device_release(dev->vbi_dev);
  1374. dev->vbi_dev = NULL;
  1375. }
  1376. if (dev->vdev) {
  1377. em28xx_info("V4L2 device /dev/video%d deregistered\n",
  1378. dev->vdev->num);
  1379. if (-1 != dev->vdev->minor)
  1380. video_unregister_device(dev->vdev);
  1381. else
  1382. video_device_release(dev->vdev);
  1383. dev->vdev = NULL;
  1384. }
  1385. }
  1386. /*
  1387. * em28xx_v4l2_close()
  1388. * stops streaming and deallocates all resources allocated by the v4l2
  1389. * calls and ioctls
  1390. */
  1391. static int em28xx_v4l2_close(struct file *filp)
  1392. {
  1393. struct em28xx_fh *fh = filp->private_data;
  1394. struct em28xx *dev = fh->dev;
  1395. int errCode;
  1396. em28xx_videodbg("users=%d\n", dev->users);
  1397. mutex_lock(&dev->lock);
  1398. if (res_check(fh))
  1399. res_free(fh);
  1400. if (dev->users == 1) {
  1401. videobuf_stop(&fh->vb_vidq);
  1402. videobuf_mmap_free(&fh->vb_vidq);
  1403. /* the device is already disconnect,
  1404. free the remaining resources */
  1405. if (dev->state & DEV_DISCONNECTED) {
  1406. em28xx_release_resources(dev);
  1407. mutex_unlock(&dev->lock);
  1408. kfree(dev);
  1409. return 0;
  1410. }
  1411. /* Save some power by putting tuner to sleep */
  1412. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_standby);
  1413. /* do this before setting alternate! */
  1414. em28xx_uninit_isoc(dev);
  1415. em28xx_set_mode(dev, EM28XX_SUSPEND);
  1416. /* set alternate 0 */
  1417. dev->alt = 0;
  1418. em28xx_videodbg("setting alternate 0\n");
  1419. errCode = usb_set_interface(dev->udev, 0, 0);
  1420. if (errCode < 0) {
  1421. em28xx_errdev("cannot change alternate number to "
  1422. "0 (error=%i)\n", errCode);
  1423. }
  1424. }
  1425. kfree(fh);
  1426. dev->users--;
  1427. wake_up_interruptible_nr(&dev->open, 1);
  1428. mutex_unlock(&dev->lock);
  1429. return 0;
  1430. }
  1431. /*
  1432. * em28xx_v4l2_read()
  1433. * will allocate buffers when called for the first time
  1434. */
  1435. static ssize_t
  1436. em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
  1437. loff_t *pos)
  1438. {
  1439. struct em28xx_fh *fh = filp->private_data;
  1440. struct em28xx *dev = fh->dev;
  1441. int rc;
  1442. rc = check_dev(dev);
  1443. if (rc < 0)
  1444. return rc;
  1445. /* FIXME: read() is not prepared to allow changing the video
  1446. resolution while streaming. Seems a bug at em28xx_set_fmt
  1447. */
  1448. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  1449. mutex_lock(&dev->lock);
  1450. rc = res_get(fh);
  1451. mutex_unlock(&dev->lock);
  1452. if (unlikely(rc < 0))
  1453. return rc;
  1454. return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
  1455. filp->f_flags & O_NONBLOCK);
  1456. }
  1457. return 0;
  1458. }
  1459. /*
  1460. * em28xx_v4l2_poll()
  1461. * will allocate buffers when called for the first time
  1462. */
  1463. static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
  1464. {
  1465. struct em28xx_fh *fh = filp->private_data;
  1466. struct em28xx *dev = fh->dev;
  1467. int rc;
  1468. rc = check_dev(dev);
  1469. if (rc < 0)
  1470. return rc;
  1471. mutex_lock(&dev->lock);
  1472. rc = res_get(fh);
  1473. mutex_unlock(&dev->lock);
  1474. if (unlikely(rc < 0))
  1475. return POLLERR;
  1476. if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
  1477. return POLLERR;
  1478. return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
  1479. }
  1480. /*
  1481. * em28xx_v4l2_mmap()
  1482. */
  1483. static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
  1484. {
  1485. struct em28xx_fh *fh = filp->private_data;
  1486. struct em28xx *dev = fh->dev;
  1487. int rc;
  1488. rc = check_dev(dev);
  1489. if (rc < 0)
  1490. return rc;
  1491. mutex_lock(&dev->lock);
  1492. rc = res_get(fh);
  1493. mutex_unlock(&dev->lock);
  1494. if (unlikely(rc < 0))
  1495. return rc;
  1496. rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
  1497. em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
  1498. (unsigned long)vma->vm_start,
  1499. (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
  1500. rc);
  1501. return rc;
  1502. }
  1503. static const struct v4l2_file_operations em28xx_v4l_fops = {
  1504. .owner = THIS_MODULE,
  1505. .open = em28xx_v4l2_open,
  1506. .release = em28xx_v4l2_close,
  1507. .read = em28xx_v4l2_read,
  1508. .poll = em28xx_v4l2_poll,
  1509. .mmap = em28xx_v4l2_mmap,
  1510. .ioctl = video_ioctl2,
  1511. };
  1512. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1513. .vidioc_querycap = vidioc_querycap,
  1514. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1515. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1516. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1517. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1518. .vidioc_g_audio = vidioc_g_audio,
  1519. .vidioc_s_audio = vidioc_s_audio,
  1520. .vidioc_cropcap = vidioc_cropcap,
  1521. .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
  1522. .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
  1523. .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
  1524. .vidioc_reqbufs = vidioc_reqbufs,
  1525. .vidioc_querybuf = vidioc_querybuf,
  1526. .vidioc_qbuf = vidioc_qbuf,
  1527. .vidioc_dqbuf = vidioc_dqbuf,
  1528. .vidioc_s_std = vidioc_s_std,
  1529. .vidioc_enum_input = vidioc_enum_input,
  1530. .vidioc_g_input = vidioc_g_input,
  1531. .vidioc_s_input = vidioc_s_input,
  1532. .vidioc_queryctrl = vidioc_queryctrl,
  1533. .vidioc_g_ctrl = vidioc_g_ctrl,
  1534. .vidioc_s_ctrl = vidioc_s_ctrl,
  1535. .vidioc_streamon = vidioc_streamon,
  1536. .vidioc_streamoff = vidioc_streamoff,
  1537. .vidioc_g_tuner = vidioc_g_tuner,
  1538. .vidioc_s_tuner = vidioc_s_tuner,
  1539. .vidioc_g_frequency = vidioc_g_frequency,
  1540. .vidioc_s_frequency = vidioc_s_frequency,
  1541. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1542. .vidioc_g_register = vidioc_g_register,
  1543. .vidioc_s_register = vidioc_s_register,
  1544. .vidioc_g_chip_ident = vidioc_g_chip_ident,
  1545. #endif
  1546. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1547. .vidiocgmbuf = vidiocgmbuf,
  1548. #endif
  1549. };
  1550. static const struct video_device em28xx_video_template = {
  1551. .fops = &em28xx_v4l_fops,
  1552. .release = video_device_release,
  1553. .ioctl_ops = &video_ioctl_ops,
  1554. .minor = -1,
  1555. .tvnorms = V4L2_STD_ALL,
  1556. .current_norm = V4L2_STD_PAL,
  1557. };
  1558. static const struct v4l2_file_operations radio_fops = {
  1559. .owner = THIS_MODULE,
  1560. .open = em28xx_v4l2_open,
  1561. .release = em28xx_v4l2_close,
  1562. .ioctl = video_ioctl2,
  1563. };
  1564. static const struct v4l2_ioctl_ops radio_ioctl_ops = {
  1565. .vidioc_querycap = radio_querycap,
  1566. .vidioc_g_tuner = radio_g_tuner,
  1567. .vidioc_enum_input = radio_enum_input,
  1568. .vidioc_g_audio = radio_g_audio,
  1569. .vidioc_s_tuner = radio_s_tuner,
  1570. .vidioc_s_audio = radio_s_audio,
  1571. .vidioc_s_input = radio_s_input,
  1572. .vidioc_queryctrl = radio_queryctrl,
  1573. .vidioc_g_ctrl = vidioc_g_ctrl,
  1574. .vidioc_s_ctrl = vidioc_s_ctrl,
  1575. .vidioc_g_frequency = vidioc_g_frequency,
  1576. .vidioc_s_frequency = vidioc_s_frequency,
  1577. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1578. .vidioc_g_register = vidioc_g_register,
  1579. .vidioc_s_register = vidioc_s_register,
  1580. #endif
  1581. };
  1582. static struct video_device em28xx_radio_template = {
  1583. .name = "em28xx-radio",
  1584. .fops = &radio_fops,
  1585. .ioctl_ops = &radio_ioctl_ops,
  1586. .minor = -1,
  1587. };
  1588. /******************************** usb interface ******************************/
  1589. static struct video_device *em28xx_vdev_init(struct em28xx *dev,
  1590. const struct video_device *template,
  1591. const char *type_name)
  1592. {
  1593. struct video_device *vfd;
  1594. vfd = video_device_alloc();
  1595. if (NULL == vfd)
  1596. return NULL;
  1597. *vfd = *template;
  1598. vfd->minor = -1;
  1599. vfd->v4l2_dev = &dev->v4l2_dev;
  1600. vfd->release = video_device_release;
  1601. vfd->debug = video_debug;
  1602. snprintf(vfd->name, sizeof(vfd->name), "%s %s",
  1603. dev->name, type_name);
  1604. return vfd;
  1605. }
  1606. int em28xx_register_analog_devices(struct em28xx *dev)
  1607. {
  1608. u8 val;
  1609. int ret;
  1610. printk(KERN_INFO "%s: v4l2 driver version %d.%d.%d\n",
  1611. dev->name,
  1612. (EM28XX_VERSION_CODE >> 16) & 0xff,
  1613. (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
  1614. /* set default norm */
  1615. dev->norm = em28xx_video_template.current_norm;
  1616. dev->interlaced = EM28XX_INTERLACED_DEFAULT;
  1617. dev->ctl_input = 0;
  1618. /* Analog specific initialization */
  1619. dev->format = &format[0];
  1620. em28xx_set_video_format(dev, format[0].fourcc,
  1621. norm_maxw(dev), norm_maxh(dev));
  1622. video_mux(dev, dev->ctl_input);
  1623. /* Audio defaults */
  1624. dev->mute = 1;
  1625. dev->volume = 0x1f;
  1626. /* enable vbi capturing */
  1627. /* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
  1628. val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
  1629. em28xx_write_reg(dev, EM28XX_R0F_XCLK,
  1630. (EM28XX_XCLK_AUDIO_UNMUTE | val));
  1631. em28xx_write_reg(dev, EM28XX_R11_VINCTRL, 0x51);
  1632. em28xx_set_outfmt(dev);
  1633. em28xx_colorlevels_set_default(dev);
  1634. em28xx_compression_disable(dev);
  1635. /* allocate and fill video video_device struct */
  1636. dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
  1637. if (!dev->vdev) {
  1638. em28xx_errdev("cannot allocate video_device.\n");
  1639. return -ENODEV;
  1640. }
  1641. /* register v4l2 video video_device */
  1642. ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
  1643. video_nr[dev->devno]);
  1644. if (ret) {
  1645. em28xx_errdev("unable to register video device (error=%i).\n",
  1646. ret);
  1647. return ret;
  1648. }
  1649. /* Allocate and fill vbi video_device struct */
  1650. dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template, "vbi");
  1651. /* register v4l2 vbi video_device */
  1652. ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
  1653. vbi_nr[dev->devno]);
  1654. if (ret < 0) {
  1655. em28xx_errdev("unable to register vbi device\n");
  1656. return ret;
  1657. }
  1658. if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
  1659. dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
  1660. "radio");
  1661. if (!dev->radio_dev) {
  1662. em28xx_errdev("cannot allocate video_device.\n");
  1663. return -ENODEV;
  1664. }
  1665. ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
  1666. radio_nr[dev->devno]);
  1667. if (ret < 0) {
  1668. em28xx_errdev("can't register radio device\n");
  1669. return ret;
  1670. }
  1671. em28xx_info("Registered radio device as /dev/radio%d\n",
  1672. dev->radio_dev->num);
  1673. }
  1674. em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
  1675. dev->vdev->num, dev->vbi_dev->num);
  1676. return 0;
  1677. }