em28xx-video.c 48 KB

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