em28xx-video.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035
  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. mutex_lock(&dev->lock);
  733. video_mux(dev, i);
  734. mutex_unlock(&dev->lock);
  735. return 0;
  736. }
  737. static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  738. {
  739. struct em28xx_fh *fh = priv;
  740. struct em28xx *dev = fh->dev;
  741. switch (a->index) {
  742. case EM28XX_AMUX_VIDEO:
  743. strcpy(a->name, "Television");
  744. break;
  745. case EM28XX_AMUX_LINE_IN:
  746. strcpy(a->name, "Line In");
  747. break;
  748. case EM28XX_AMUX_VIDEO2:
  749. strcpy(a->name, "Television alt");
  750. break;
  751. case EM28XX_AMUX_PHONE:
  752. strcpy(a->name, "Phone");
  753. break;
  754. case EM28XX_AMUX_MIC:
  755. strcpy(a->name, "Mic");
  756. break;
  757. case EM28XX_AMUX_CD:
  758. strcpy(a->name, "CD");
  759. break;
  760. case EM28XX_AMUX_AUX:
  761. strcpy(a->name, "Aux");
  762. break;
  763. case EM28XX_AMUX_PCM_OUT:
  764. strcpy(a->name, "PCM");
  765. break;
  766. default:
  767. return -EINVAL;
  768. }
  769. a->index = dev->ctl_ainput;
  770. a->capability = V4L2_AUDCAP_STEREO;
  771. return 0;
  772. }
  773. static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
  774. {
  775. struct em28xx_fh *fh = priv;
  776. struct em28xx *dev = fh->dev;
  777. mutex_lock(&dev->lock);
  778. dev->ctl_ainput = INPUT(a->index)->amux;
  779. dev->ctl_aoutput = INPUT(a->index)->aout;
  780. if (!dev->ctl_aoutput)
  781. dev->ctl_aoutput = EM28XX_AOUT_MASTER;
  782. mutex_unlock(&dev->lock);
  783. return 0;
  784. }
  785. static int vidioc_queryctrl(struct file *file, void *priv,
  786. struct v4l2_queryctrl *qc)
  787. {
  788. struct em28xx_fh *fh = priv;
  789. struct em28xx *dev = fh->dev;
  790. int id = qc->id;
  791. int i;
  792. int rc;
  793. rc = check_dev(dev);
  794. if (rc < 0)
  795. return rc;
  796. memset(qc, 0, sizeof(*qc));
  797. qc->id = id;
  798. if (!dev->board.has_msp34xx) {
  799. for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
  800. if (qc->id && qc->id == em28xx_qctrl[i].id) {
  801. memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
  802. return 0;
  803. }
  804. }
  805. }
  806. mutex_lock(&dev->lock);
  807. em28xx_i2c_call_clients(dev, VIDIOC_QUERYCTRL, qc);
  808. mutex_unlock(&dev->lock);
  809. if (qc->type)
  810. return 0;
  811. else
  812. return -EINVAL;
  813. }
  814. static int vidioc_g_ctrl(struct file *file, void *priv,
  815. struct v4l2_control *ctrl)
  816. {
  817. struct em28xx_fh *fh = priv;
  818. struct em28xx *dev = fh->dev;
  819. int rc;
  820. rc = check_dev(dev);
  821. if (rc < 0)
  822. return rc;
  823. rc = 0;
  824. mutex_lock(&dev->lock);
  825. if (dev->board.has_msp34xx)
  826. em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl);
  827. else
  828. rc = em28xx_get_ctrl(dev, ctrl);
  829. mutex_unlock(&dev->lock);
  830. return rc;
  831. }
  832. static int vidioc_s_ctrl(struct file *file, void *priv,
  833. struct v4l2_control *ctrl)
  834. {
  835. struct em28xx_fh *fh = priv;
  836. struct em28xx *dev = fh->dev;
  837. u8 i;
  838. int rc;
  839. rc = check_dev(dev);
  840. if (rc < 0)
  841. return rc;
  842. mutex_lock(&dev->lock);
  843. if (dev->board.has_msp34xx)
  844. em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
  845. else {
  846. rc = 1;
  847. for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
  848. if (ctrl->id == em28xx_qctrl[i].id) {
  849. if (ctrl->value < em28xx_qctrl[i].minimum ||
  850. ctrl->value > em28xx_qctrl[i].maximum) {
  851. rc = -ERANGE;
  852. break;
  853. }
  854. rc = em28xx_set_ctrl(dev, ctrl);
  855. break;
  856. }
  857. }
  858. }
  859. /* Control not found - try to send it to the attached devices */
  860. if (rc == 1) {
  861. em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
  862. rc = 0;
  863. }
  864. mutex_unlock(&dev->lock);
  865. return rc;
  866. }
  867. static int vidioc_g_tuner(struct file *file, void *priv,
  868. struct v4l2_tuner *t)
  869. {
  870. struct em28xx_fh *fh = priv;
  871. struct em28xx *dev = fh->dev;
  872. int rc;
  873. rc = check_dev(dev);
  874. if (rc < 0)
  875. return rc;
  876. if (0 != t->index)
  877. return -EINVAL;
  878. strcpy(t->name, "Tuner");
  879. mutex_lock(&dev->lock);
  880. em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
  881. mutex_unlock(&dev->lock);
  882. return 0;
  883. }
  884. static int vidioc_s_tuner(struct file *file, void *priv,
  885. struct v4l2_tuner *t)
  886. {
  887. struct em28xx_fh *fh = priv;
  888. struct em28xx *dev = fh->dev;
  889. int rc;
  890. rc = check_dev(dev);
  891. if (rc < 0)
  892. return rc;
  893. if (0 != t->index)
  894. return -EINVAL;
  895. mutex_lock(&dev->lock);
  896. em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
  897. mutex_unlock(&dev->lock);
  898. return 0;
  899. }
  900. static int vidioc_g_frequency(struct file *file, void *priv,
  901. struct v4l2_frequency *f)
  902. {
  903. struct em28xx_fh *fh = priv;
  904. struct em28xx *dev = fh->dev;
  905. mutex_lock(&dev->lock);
  906. f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  907. f->frequency = dev->ctl_freq;
  908. mutex_unlock(&dev->lock);
  909. return 0;
  910. }
  911. static int vidioc_s_frequency(struct file *file, void *priv,
  912. struct v4l2_frequency *f)
  913. {
  914. struct em28xx_fh *fh = priv;
  915. struct em28xx *dev = fh->dev;
  916. int rc;
  917. rc = check_dev(dev);
  918. if (rc < 0)
  919. return rc;
  920. if (0 != f->tuner)
  921. return -EINVAL;
  922. if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
  923. return -EINVAL;
  924. if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
  925. return -EINVAL;
  926. mutex_lock(&dev->lock);
  927. dev->ctl_freq = f->frequency;
  928. em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
  929. mutex_unlock(&dev->lock);
  930. return 0;
  931. }
  932. #ifdef CONFIG_VIDEO_ADV_DEBUG
  933. static int em28xx_reg_len(int reg)
  934. {
  935. switch (reg) {
  936. case EM28XX_R40_AC97LSB:
  937. case EM28XX_R30_HSCALELOW:
  938. case EM28XX_R32_VSCALELOW:
  939. return 2;
  940. default:
  941. return 1;
  942. }
  943. }
  944. static int vidioc_g_chip_ident(struct file *file, void *priv,
  945. struct v4l2_dbg_chip_ident *chip)
  946. {
  947. struct em28xx_fh *fh = priv;
  948. struct em28xx *dev = fh->dev;
  949. chip->ident = V4L2_IDENT_NONE;
  950. chip->revision = 0;
  951. em28xx_i2c_call_clients(dev, VIDIOC_DBG_G_CHIP_IDENT, chip);
  952. return 0;
  953. }
  954. static int vidioc_g_register(struct file *file, void *priv,
  955. struct v4l2_dbg_register *reg)
  956. {
  957. struct em28xx_fh *fh = priv;
  958. struct em28xx *dev = fh->dev;
  959. int ret;
  960. switch (reg->match.type) {
  961. case V4L2_CHIP_MATCH_AC97:
  962. mutex_lock(&dev->lock);
  963. ret = em28xx_read_ac97(dev, reg->reg);
  964. mutex_unlock(&dev->lock);
  965. if (ret < 0)
  966. return ret;
  967. reg->val = ret;
  968. reg->size = 1;
  969. return 0;
  970. case V4L2_CHIP_MATCH_I2C_DRIVER:
  971. em28xx_i2c_call_clients(dev, VIDIOC_DBG_G_REGISTER, reg);
  972. return 0;
  973. case V4L2_CHIP_MATCH_I2C_ADDR:
  974. /* Not supported yet */
  975. return -EINVAL;
  976. default:
  977. if (!v4l2_chip_match_host(&reg->match))
  978. return -EINVAL;
  979. }
  980. /* Match host */
  981. reg->size = em28xx_reg_len(reg->reg);
  982. if (reg->size == 1) {
  983. mutex_lock(&dev->lock);
  984. ret = em28xx_read_reg(dev, reg->reg);
  985. mutex_unlock(&dev->lock);
  986. if (ret < 0)
  987. return ret;
  988. reg->val = ret;
  989. } else {
  990. __le16 val = 0;
  991. mutex_lock(&dev->lock);
  992. ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
  993. reg->reg, (char *)&val, 2);
  994. mutex_unlock(&dev->lock);
  995. if (ret < 0)
  996. return ret;
  997. reg->val = le16_to_cpu(val);
  998. }
  999. return 0;
  1000. }
  1001. static int vidioc_s_register(struct file *file, void *priv,
  1002. struct v4l2_dbg_register *reg)
  1003. {
  1004. struct em28xx_fh *fh = priv;
  1005. struct em28xx *dev = fh->dev;
  1006. __le16 buf;
  1007. int rc;
  1008. switch (reg->match.type) {
  1009. case V4L2_CHIP_MATCH_AC97:
  1010. mutex_lock(&dev->lock);
  1011. rc = em28xx_write_ac97(dev, reg->reg, reg->val);
  1012. mutex_unlock(&dev->lock);
  1013. return rc;
  1014. case V4L2_CHIP_MATCH_I2C_DRIVER:
  1015. em28xx_i2c_call_clients(dev, VIDIOC_DBG_S_REGISTER, reg);
  1016. return 0;
  1017. case V4L2_CHIP_MATCH_I2C_ADDR:
  1018. /* Not supported yet */
  1019. return -EINVAL;
  1020. default:
  1021. if (!v4l2_chip_match_host(&reg->match))
  1022. return -EINVAL;
  1023. }
  1024. /* Match host */
  1025. buf = cpu_to_le16(reg->val);
  1026. mutex_lock(&dev->lock);
  1027. rc = em28xx_write_regs(dev, reg->reg, (char *)&buf,
  1028. em28xx_reg_len(reg->reg));
  1029. mutex_unlock(&dev->lock);
  1030. return rc;
  1031. }
  1032. #endif
  1033. static int vidioc_cropcap(struct file *file, void *priv,
  1034. struct v4l2_cropcap *cc)
  1035. {
  1036. struct em28xx_fh *fh = priv;
  1037. struct em28xx *dev = fh->dev;
  1038. if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1039. return -EINVAL;
  1040. cc->bounds.left = 0;
  1041. cc->bounds.top = 0;
  1042. cc->bounds.width = dev->width;
  1043. cc->bounds.height = dev->height;
  1044. cc->defrect = cc->bounds;
  1045. cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
  1046. cc->pixelaspect.denominator = 59;
  1047. return 0;
  1048. }
  1049. static int vidioc_streamon(struct file *file, void *priv,
  1050. enum v4l2_buf_type type)
  1051. {
  1052. struct em28xx_fh *fh = priv;
  1053. struct em28xx *dev = fh->dev;
  1054. int rc;
  1055. rc = check_dev(dev);
  1056. if (rc < 0)
  1057. return rc;
  1058. mutex_lock(&dev->lock);
  1059. rc = res_get(fh);
  1060. if (likely(rc >= 0))
  1061. rc = videobuf_streamon(&fh->vb_vidq);
  1062. mutex_unlock(&dev->lock);
  1063. return rc;
  1064. }
  1065. static int vidioc_streamoff(struct file *file, void *priv,
  1066. enum v4l2_buf_type type)
  1067. {
  1068. struct em28xx_fh *fh = priv;
  1069. struct em28xx *dev = fh->dev;
  1070. int rc;
  1071. rc = check_dev(dev);
  1072. if (rc < 0)
  1073. return rc;
  1074. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1075. return -EINVAL;
  1076. if (type != fh->type)
  1077. return -EINVAL;
  1078. mutex_lock(&dev->lock);
  1079. videobuf_streamoff(&fh->vb_vidq);
  1080. res_free(fh);
  1081. mutex_unlock(&dev->lock);
  1082. return 0;
  1083. }
  1084. static int vidioc_querycap(struct file *file, void *priv,
  1085. struct v4l2_capability *cap)
  1086. {
  1087. struct em28xx_fh *fh = priv;
  1088. struct em28xx *dev = fh->dev;
  1089. strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
  1090. strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
  1091. strlcpy(cap->bus_info, dev_name(&dev->udev->dev), sizeof(cap->bus_info));
  1092. cap->version = EM28XX_VERSION_CODE;
  1093. cap->capabilities =
  1094. V4L2_CAP_SLICED_VBI_CAPTURE |
  1095. V4L2_CAP_VIDEO_CAPTURE |
  1096. V4L2_CAP_AUDIO |
  1097. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  1098. if (dev->tuner_type != TUNER_ABSENT)
  1099. cap->capabilities |= V4L2_CAP_TUNER;
  1100. return 0;
  1101. }
  1102. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  1103. struct v4l2_fmtdesc *f)
  1104. {
  1105. if (unlikely(f->index >= ARRAY_SIZE(format)))
  1106. return -EINVAL;
  1107. strlcpy(f->description, format[f->index].name, sizeof(f->description));
  1108. f->pixelformat = format[f->index].fourcc;
  1109. return 0;
  1110. }
  1111. /* Sliced VBI ioctls */
  1112. static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
  1113. struct v4l2_format *f)
  1114. {
  1115. struct em28xx_fh *fh = priv;
  1116. struct em28xx *dev = fh->dev;
  1117. int rc;
  1118. rc = check_dev(dev);
  1119. if (rc < 0)
  1120. return rc;
  1121. mutex_lock(&dev->lock);
  1122. f->fmt.sliced.service_set = 0;
  1123. em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
  1124. if (f->fmt.sliced.service_set == 0)
  1125. rc = -EINVAL;
  1126. mutex_unlock(&dev->lock);
  1127. return rc;
  1128. }
  1129. static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
  1130. struct v4l2_format *f)
  1131. {
  1132. struct em28xx_fh *fh = priv;
  1133. struct em28xx *dev = fh->dev;
  1134. int rc;
  1135. rc = check_dev(dev);
  1136. if (rc < 0)
  1137. return rc;
  1138. mutex_lock(&dev->lock);
  1139. em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
  1140. mutex_unlock(&dev->lock);
  1141. if (f->fmt.sliced.service_set == 0)
  1142. return -EINVAL;
  1143. return 0;
  1144. }
  1145. static int vidioc_reqbufs(struct file *file, void *priv,
  1146. struct v4l2_requestbuffers *rb)
  1147. {
  1148. struct em28xx_fh *fh = priv;
  1149. struct em28xx *dev = fh->dev;
  1150. int rc;
  1151. rc = check_dev(dev);
  1152. if (rc < 0)
  1153. return rc;
  1154. return (videobuf_reqbufs(&fh->vb_vidq, rb));
  1155. }
  1156. static int vidioc_querybuf(struct file *file, void *priv,
  1157. struct v4l2_buffer *b)
  1158. {
  1159. struct em28xx_fh *fh = priv;
  1160. struct em28xx *dev = fh->dev;
  1161. int rc;
  1162. rc = check_dev(dev);
  1163. if (rc < 0)
  1164. return rc;
  1165. return (videobuf_querybuf(&fh->vb_vidq, b));
  1166. }
  1167. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1168. {
  1169. struct em28xx_fh *fh = priv;
  1170. struct em28xx *dev = fh->dev;
  1171. int rc;
  1172. rc = check_dev(dev);
  1173. if (rc < 0)
  1174. return rc;
  1175. return (videobuf_qbuf(&fh->vb_vidq, b));
  1176. }
  1177. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1178. {
  1179. struct em28xx_fh *fh = priv;
  1180. struct em28xx *dev = fh->dev;
  1181. int rc;
  1182. rc = check_dev(dev);
  1183. if (rc < 0)
  1184. return rc;
  1185. return (videobuf_dqbuf(&fh->vb_vidq, b,
  1186. file->f_flags & O_NONBLOCK));
  1187. }
  1188. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1189. static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
  1190. {
  1191. struct em28xx_fh *fh = priv;
  1192. return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
  1193. }
  1194. #endif
  1195. /* ----------------------------------------------------------- */
  1196. /* RADIO ESPECIFIC IOCTLS */
  1197. /* ----------------------------------------------------------- */
  1198. static int radio_querycap(struct file *file, void *priv,
  1199. struct v4l2_capability *cap)
  1200. {
  1201. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1202. strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
  1203. strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
  1204. strlcpy(cap->bus_info, dev_name(&dev->udev->dev), sizeof(cap->bus_info));
  1205. cap->version = EM28XX_VERSION_CODE;
  1206. cap->capabilities = V4L2_CAP_TUNER;
  1207. return 0;
  1208. }
  1209. static int radio_g_tuner(struct file *file, void *priv,
  1210. struct v4l2_tuner *t)
  1211. {
  1212. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1213. if (unlikely(t->index > 0))
  1214. return -EINVAL;
  1215. strcpy(t->name, "Radio");
  1216. t->type = V4L2_TUNER_RADIO;
  1217. mutex_lock(&dev->lock);
  1218. em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
  1219. mutex_unlock(&dev->lock);
  1220. return 0;
  1221. }
  1222. static int radio_enum_input(struct file *file, void *priv,
  1223. struct v4l2_input *i)
  1224. {
  1225. if (i->index != 0)
  1226. return -EINVAL;
  1227. strcpy(i->name, "Radio");
  1228. i->type = V4L2_INPUT_TYPE_TUNER;
  1229. return 0;
  1230. }
  1231. static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  1232. {
  1233. if (unlikely(a->index))
  1234. return -EINVAL;
  1235. strcpy(a->name, "Radio");
  1236. return 0;
  1237. }
  1238. static int radio_s_tuner(struct file *file, void *priv,
  1239. struct v4l2_tuner *t)
  1240. {
  1241. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1242. if (0 != t->index)
  1243. return -EINVAL;
  1244. mutex_lock(&dev->lock);
  1245. em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
  1246. mutex_unlock(&dev->lock);
  1247. return 0;
  1248. }
  1249. static int radio_s_audio(struct file *file, void *fh,
  1250. struct v4l2_audio *a)
  1251. {
  1252. return 0;
  1253. }
  1254. static int radio_s_input(struct file *file, void *fh, unsigned int i)
  1255. {
  1256. return 0;
  1257. }
  1258. static int radio_queryctrl(struct file *file, void *priv,
  1259. struct v4l2_queryctrl *qc)
  1260. {
  1261. int i;
  1262. if (qc->id < V4L2_CID_BASE ||
  1263. qc->id >= V4L2_CID_LASTP1)
  1264. return -EINVAL;
  1265. for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
  1266. if (qc->id && qc->id == em28xx_qctrl[i].id) {
  1267. memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
  1268. return 0;
  1269. }
  1270. }
  1271. return -EINVAL;
  1272. }
  1273. /*
  1274. * em28xx_v4l2_open()
  1275. * inits the device and starts isoc transfer
  1276. */
  1277. static int em28xx_v4l2_open(struct file *filp)
  1278. {
  1279. int minor = video_devdata(filp)->minor;
  1280. int errCode = 0, radio;
  1281. struct em28xx *dev;
  1282. enum v4l2_buf_type fh_type;
  1283. struct em28xx_fh *fh;
  1284. dev = em28xx_get_device(minor, &fh_type, &radio);
  1285. if (NULL == dev)
  1286. return -ENODEV;
  1287. mutex_lock(&dev->lock);
  1288. em28xx_videodbg("open minor=%d type=%s users=%d\n",
  1289. minor, v4l2_type_names[fh_type], dev->users);
  1290. fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
  1291. if (!fh) {
  1292. em28xx_errdev("em28xx-video.c: Out of memory?!\n");
  1293. mutex_unlock(&dev->lock);
  1294. return -ENOMEM;
  1295. }
  1296. fh->dev = dev;
  1297. fh->radio = radio;
  1298. fh->type = fh_type;
  1299. filp->private_data = fh;
  1300. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
  1301. dev->width = norm_maxw(dev);
  1302. dev->height = norm_maxh(dev);
  1303. dev->hscale = 0;
  1304. dev->vscale = 0;
  1305. em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
  1306. em28xx_set_alternate(dev);
  1307. em28xx_resolution_set(dev);
  1308. /* Needed, since GPIO might have disabled power of
  1309. some i2c device
  1310. */
  1311. em28xx_wake_i2c(dev);
  1312. }
  1313. if (fh->radio) {
  1314. em28xx_videodbg("video_open: setting radio device\n");
  1315. em28xx_i2c_call_clients(dev, AUDC_SET_RADIO, NULL);
  1316. }
  1317. dev->users++;
  1318. videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
  1319. NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
  1320. sizeof(struct em28xx_buffer), fh);
  1321. mutex_unlock(&dev->lock);
  1322. return errCode;
  1323. }
  1324. /*
  1325. * em28xx_realease_resources()
  1326. * unregisters the v4l2,i2c and usb devices
  1327. * called when the device gets disconected or at module unload
  1328. */
  1329. void em28xx_release_analog_resources(struct em28xx *dev)
  1330. {
  1331. /*FIXME: I2C IR should be disconnected */
  1332. if (dev->radio_dev) {
  1333. if (-1 != dev->radio_dev->minor)
  1334. video_unregister_device(dev->radio_dev);
  1335. else
  1336. video_device_release(dev->radio_dev);
  1337. dev->radio_dev = NULL;
  1338. }
  1339. if (dev->vbi_dev) {
  1340. em28xx_info("V4L2 device /dev/vbi%d deregistered\n",
  1341. dev->vbi_dev->num);
  1342. if (-1 != dev->vbi_dev->minor)
  1343. video_unregister_device(dev->vbi_dev);
  1344. else
  1345. video_device_release(dev->vbi_dev);
  1346. dev->vbi_dev = NULL;
  1347. }
  1348. if (dev->vdev) {
  1349. em28xx_info("V4L2 device /dev/video%d deregistered\n",
  1350. dev->vdev->num);
  1351. if (-1 != dev->vdev->minor)
  1352. video_unregister_device(dev->vdev);
  1353. else
  1354. video_device_release(dev->vdev);
  1355. dev->vdev = NULL;
  1356. }
  1357. }
  1358. /*
  1359. * em28xx_v4l2_close()
  1360. * stops streaming and deallocates all resources allocated by the v4l2
  1361. * calls and ioctls
  1362. */
  1363. static int em28xx_v4l2_close(struct file *filp)
  1364. {
  1365. struct em28xx_fh *fh = filp->private_data;
  1366. struct em28xx *dev = fh->dev;
  1367. int errCode;
  1368. em28xx_videodbg("users=%d\n", dev->users);
  1369. mutex_lock(&dev->lock);
  1370. if (res_check(fh))
  1371. res_free(fh);
  1372. if (dev->users == 1) {
  1373. videobuf_stop(&fh->vb_vidq);
  1374. videobuf_mmap_free(&fh->vb_vidq);
  1375. /* the device is already disconnect,
  1376. free the remaining resources */
  1377. if (dev->state & DEV_DISCONNECTED) {
  1378. em28xx_release_resources(dev);
  1379. mutex_unlock(&dev->lock);
  1380. kfree(dev);
  1381. return 0;
  1382. }
  1383. /* Save some power by putting tuner to sleep */
  1384. em28xx_i2c_call_clients(dev, TUNER_SET_STANDBY, NULL);
  1385. /* do this before setting alternate! */
  1386. em28xx_uninit_isoc(dev);
  1387. em28xx_set_mode(dev, EM28XX_SUSPEND);
  1388. /* set alternate 0 */
  1389. dev->alt = 0;
  1390. em28xx_videodbg("setting alternate 0\n");
  1391. errCode = usb_set_interface(dev->udev, 0, 0);
  1392. if (errCode < 0) {
  1393. em28xx_errdev("cannot change alternate number to "
  1394. "0 (error=%i)\n", errCode);
  1395. }
  1396. }
  1397. kfree(fh);
  1398. dev->users--;
  1399. wake_up_interruptible_nr(&dev->open, 1);
  1400. mutex_unlock(&dev->lock);
  1401. return 0;
  1402. }
  1403. /*
  1404. * em28xx_v4l2_read()
  1405. * will allocate buffers when called for the first time
  1406. */
  1407. static ssize_t
  1408. em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
  1409. loff_t *pos)
  1410. {
  1411. struct em28xx_fh *fh = filp->private_data;
  1412. struct em28xx *dev = fh->dev;
  1413. int rc;
  1414. rc = check_dev(dev);
  1415. if (rc < 0)
  1416. return rc;
  1417. /* FIXME: read() is not prepared to allow changing the video
  1418. resolution while streaming. Seems a bug at em28xx_set_fmt
  1419. */
  1420. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  1421. mutex_lock(&dev->lock);
  1422. rc = res_get(fh);
  1423. mutex_unlock(&dev->lock);
  1424. if (unlikely(rc < 0))
  1425. return rc;
  1426. return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
  1427. filp->f_flags & O_NONBLOCK);
  1428. }
  1429. return 0;
  1430. }
  1431. /*
  1432. * em28xx_v4l2_poll()
  1433. * will allocate buffers when called for the first time
  1434. */
  1435. static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
  1436. {
  1437. struct em28xx_fh *fh = filp->private_data;
  1438. struct em28xx *dev = fh->dev;
  1439. int rc;
  1440. rc = check_dev(dev);
  1441. if (rc < 0)
  1442. return rc;
  1443. mutex_lock(&dev->lock);
  1444. rc = res_get(fh);
  1445. mutex_unlock(&dev->lock);
  1446. if (unlikely(rc < 0))
  1447. return POLLERR;
  1448. if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
  1449. return POLLERR;
  1450. return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
  1451. }
  1452. /*
  1453. * em28xx_v4l2_mmap()
  1454. */
  1455. static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
  1456. {
  1457. struct em28xx_fh *fh = filp->private_data;
  1458. struct em28xx *dev = fh->dev;
  1459. int rc;
  1460. rc = check_dev(dev);
  1461. if (rc < 0)
  1462. return rc;
  1463. mutex_lock(&dev->lock);
  1464. rc = res_get(fh);
  1465. mutex_unlock(&dev->lock);
  1466. if (unlikely(rc < 0))
  1467. return rc;
  1468. rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
  1469. em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
  1470. (unsigned long)vma->vm_start,
  1471. (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
  1472. rc);
  1473. return rc;
  1474. }
  1475. static const struct v4l2_file_operations em28xx_v4l_fops = {
  1476. .owner = THIS_MODULE,
  1477. .open = em28xx_v4l2_open,
  1478. .release = em28xx_v4l2_close,
  1479. .read = em28xx_v4l2_read,
  1480. .poll = em28xx_v4l2_poll,
  1481. .mmap = em28xx_v4l2_mmap,
  1482. .ioctl = video_ioctl2,
  1483. };
  1484. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1485. .vidioc_querycap = vidioc_querycap,
  1486. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1487. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1488. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1489. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1490. .vidioc_g_audio = vidioc_g_audio,
  1491. .vidioc_s_audio = vidioc_s_audio,
  1492. .vidioc_cropcap = vidioc_cropcap,
  1493. .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
  1494. .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
  1495. .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
  1496. .vidioc_reqbufs = vidioc_reqbufs,
  1497. .vidioc_querybuf = vidioc_querybuf,
  1498. .vidioc_qbuf = vidioc_qbuf,
  1499. .vidioc_dqbuf = vidioc_dqbuf,
  1500. .vidioc_s_std = vidioc_s_std,
  1501. .vidioc_enum_input = vidioc_enum_input,
  1502. .vidioc_g_input = vidioc_g_input,
  1503. .vidioc_s_input = vidioc_s_input,
  1504. .vidioc_queryctrl = vidioc_queryctrl,
  1505. .vidioc_g_ctrl = vidioc_g_ctrl,
  1506. .vidioc_s_ctrl = vidioc_s_ctrl,
  1507. .vidioc_streamon = vidioc_streamon,
  1508. .vidioc_streamoff = vidioc_streamoff,
  1509. .vidioc_g_tuner = vidioc_g_tuner,
  1510. .vidioc_s_tuner = vidioc_s_tuner,
  1511. .vidioc_g_frequency = vidioc_g_frequency,
  1512. .vidioc_s_frequency = vidioc_s_frequency,
  1513. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1514. .vidioc_g_register = vidioc_g_register,
  1515. .vidioc_s_register = vidioc_s_register,
  1516. .vidioc_g_chip_ident = vidioc_g_chip_ident,
  1517. #endif
  1518. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1519. .vidiocgmbuf = vidiocgmbuf,
  1520. #endif
  1521. };
  1522. static const struct video_device em28xx_video_template = {
  1523. .fops = &em28xx_v4l_fops,
  1524. .release = video_device_release,
  1525. .ioctl_ops = &video_ioctl_ops,
  1526. .minor = -1,
  1527. .tvnorms = V4L2_STD_ALL,
  1528. .current_norm = V4L2_STD_PAL,
  1529. };
  1530. static const struct v4l2_file_operations radio_fops = {
  1531. .owner = THIS_MODULE,
  1532. .open = em28xx_v4l2_open,
  1533. .release = em28xx_v4l2_close,
  1534. .ioctl = video_ioctl2,
  1535. };
  1536. static const struct v4l2_ioctl_ops radio_ioctl_ops = {
  1537. .vidioc_querycap = radio_querycap,
  1538. .vidioc_g_tuner = radio_g_tuner,
  1539. .vidioc_enum_input = radio_enum_input,
  1540. .vidioc_g_audio = radio_g_audio,
  1541. .vidioc_s_tuner = radio_s_tuner,
  1542. .vidioc_s_audio = radio_s_audio,
  1543. .vidioc_s_input = radio_s_input,
  1544. .vidioc_queryctrl = radio_queryctrl,
  1545. .vidioc_g_ctrl = vidioc_g_ctrl,
  1546. .vidioc_s_ctrl = vidioc_s_ctrl,
  1547. .vidioc_g_frequency = vidioc_g_frequency,
  1548. .vidioc_s_frequency = vidioc_s_frequency,
  1549. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1550. .vidioc_g_register = vidioc_g_register,
  1551. .vidioc_s_register = vidioc_s_register,
  1552. #endif
  1553. };
  1554. static struct video_device em28xx_radio_template = {
  1555. .name = "em28xx-radio",
  1556. .fops = &radio_fops,
  1557. .ioctl_ops = &radio_ioctl_ops,
  1558. .minor = -1,
  1559. };
  1560. /******************************** usb interface ******************************/
  1561. static struct video_device *em28xx_vdev_init(struct em28xx *dev,
  1562. const struct video_device *template,
  1563. const char *type_name)
  1564. {
  1565. struct video_device *vfd;
  1566. vfd = video_device_alloc();
  1567. if (NULL == vfd)
  1568. return NULL;
  1569. *vfd = *template;
  1570. vfd->minor = -1;
  1571. vfd->parent = &dev->udev->dev;
  1572. vfd->release = video_device_release;
  1573. vfd->debug = video_debug;
  1574. snprintf(vfd->name, sizeof(vfd->name), "%s %s",
  1575. dev->name, type_name);
  1576. return vfd;
  1577. }
  1578. int em28xx_register_analog_devices(struct em28xx *dev)
  1579. {
  1580. int ret;
  1581. printk(KERN_INFO "%s: v4l2 driver version %d.%d.%d\n",
  1582. dev->name,
  1583. (EM28XX_VERSION_CODE >> 16) & 0xff,
  1584. (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
  1585. /* Analog specific initialization */
  1586. dev->format = &format[0];
  1587. video_mux(dev, 0);
  1588. /* enable vbi capturing */
  1589. /* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
  1590. /* em28xx_write_reg(dev, EM28XX_R0F_XCLK, 0x80); clk register */
  1591. em28xx_write_reg(dev, EM28XX_R11_VINCTRL, 0x51);
  1592. dev->mute = 1; /* maybe not the right place... */
  1593. dev->volume = 0x1f;
  1594. em28xx_set_outfmt(dev);
  1595. em28xx_colorlevels_set_default(dev);
  1596. em28xx_compression_disable(dev);
  1597. /* set default norm */
  1598. dev->norm = em28xx_video_template.current_norm;
  1599. dev->width = norm_maxw(dev);
  1600. dev->height = norm_maxh(dev);
  1601. dev->interlaced = EM28XX_INTERLACED_DEFAULT;
  1602. dev->hscale = 0;
  1603. dev->vscale = 0;
  1604. /* FIXME: This is a very bad hack! Not all devices have TV on input 2 */
  1605. dev->ctl_input = 2;
  1606. /* allocate and fill video video_device struct */
  1607. dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
  1608. if (!dev->vdev) {
  1609. em28xx_errdev("cannot allocate video_device.\n");
  1610. return -ENODEV;
  1611. }
  1612. /* register v4l2 video video_device */
  1613. ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
  1614. video_nr[dev->devno]);
  1615. if (ret) {
  1616. em28xx_errdev("unable to register video device (error=%i).\n",
  1617. ret);
  1618. return ret;
  1619. }
  1620. /* Allocate and fill vbi video_device struct */
  1621. dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template, "vbi");
  1622. /* register v4l2 vbi video_device */
  1623. ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
  1624. vbi_nr[dev->devno]);
  1625. if (ret < 0) {
  1626. em28xx_errdev("unable to register vbi device\n");
  1627. return ret;
  1628. }
  1629. if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
  1630. dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template, "radio");
  1631. if (!dev->radio_dev) {
  1632. em28xx_errdev("cannot allocate video_device.\n");
  1633. return -ENODEV;
  1634. }
  1635. ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
  1636. radio_nr[dev->devno]);
  1637. if (ret < 0) {
  1638. em28xx_errdev("can't register radio device\n");
  1639. return ret;
  1640. }
  1641. em28xx_info("Registered radio device as /dev/radio%d\n",
  1642. dev->radio_dev->num);
  1643. }
  1644. em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
  1645. dev->vdev->num, dev->vbi_dev->num);
  1646. return 0;
  1647. }