em28xx-video.c 50 KB

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