em28xx-video.c 48 KB

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