em28xx-video.c 48 KB

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