em28xx-video.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287
  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 ac97_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 = V4L2_CTRL_FLAG_SLIDER,
  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 = &dev->vidq;
  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. /* Version of isoc handler that takes into account a mixture of video and
  354. VBI data */
  355. static inline int em28xx_isoc_copy_vbi(struct em28xx *dev, struct urb *urb)
  356. {
  357. struct em28xx_buffer *buf, *vbi_buf;
  358. struct em28xx_dmaqueue *dma_q = &dev->vidq;
  359. unsigned char *outp = NULL;
  360. unsigned char *vbioutp = NULL;
  361. int i, len = 0, rc = 1;
  362. unsigned char *p;
  363. int vbi_size;
  364. if (!dev)
  365. return 0;
  366. if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
  367. return 0;
  368. if (urb->status < 0) {
  369. print_err_status(dev, -1, urb->status);
  370. if (urb->status == -ENOENT)
  371. return 0;
  372. }
  373. buf = dev->isoc_ctl.buf;
  374. if (buf != NULL)
  375. outp = videobuf_to_vmalloc(&buf->vb);
  376. for (i = 0; i < urb->number_of_packets; i++) {
  377. int status = urb->iso_frame_desc[i].status;
  378. if (status < 0) {
  379. print_err_status(dev, i, status);
  380. if (urb->iso_frame_desc[i].status != -EPROTO)
  381. continue;
  382. }
  383. len = urb->iso_frame_desc[i].actual_length - 4;
  384. if (urb->iso_frame_desc[i].actual_length <= 0) {
  385. /* em28xx_isocdbg("packet %d is empty",i); - spammy */
  386. continue;
  387. }
  388. if (urb->iso_frame_desc[i].actual_length >
  389. dev->max_pkt_size) {
  390. em28xx_isocdbg("packet bigger than packet size");
  391. continue;
  392. }
  393. p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  394. /* capture type 0 = vbi start
  395. capture type 1 = video start
  396. capture type 2 = video in progress */
  397. if (p[0] == 0x33 && p[1] == 0x95) {
  398. dev->capture_type = 0;
  399. dev->vbi_read = 0;
  400. em28xx_isocdbg("VBI START HEADER!!!\n");
  401. dev->cur_field = p[2];
  402. }
  403. /* FIXME: get rid of hard-coded value */
  404. vbi_size = 720 * 0x0c;
  405. if (dev->capture_type == 0) {
  406. if (dev->vbi_read >= vbi_size) {
  407. /* We've already read all the VBI data, so
  408. treat the rest as video */
  409. printk("djh c should never happen\n");
  410. } else if ((dev->vbi_read + len) < vbi_size) {
  411. /* This entire frame is VBI data */
  412. dev->vbi_read += len;
  413. } else {
  414. /* Some of this frame is VBI data and some is
  415. video data */
  416. int vbi_data_len = vbi_size - dev->vbi_read;
  417. dev->vbi_read += vbi_data_len;
  418. dev->capture_type = 1;
  419. p += vbi_data_len;
  420. len -= vbi_data_len;
  421. }
  422. }
  423. if (dev->capture_type == 1) {
  424. dev->capture_type = 2;
  425. em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
  426. len, (p[2] & 1) ? "odd" : "even");
  427. if (dev->progressive || !(dev->cur_field & 1)) {
  428. if (buf != NULL)
  429. buffer_filled(dev, dma_q, buf);
  430. get_next_buf(dma_q, &buf);
  431. if (buf == NULL)
  432. outp = NULL;
  433. else
  434. outp = videobuf_to_vmalloc(&buf->vb);
  435. }
  436. if (buf != NULL) {
  437. if (dev->cur_field & 1)
  438. buf->top_field = 0;
  439. else
  440. buf->top_field = 1;
  441. }
  442. dma_q->pos = 0;
  443. }
  444. if (buf != NULL && dev->capture_type == 2)
  445. em28xx_copy_video(dev, dma_q, buf, p, outp, len);
  446. }
  447. return rc;
  448. }
  449. /* ------------------------------------------------------------------
  450. Videobuf operations
  451. ------------------------------------------------------------------*/
  452. static int
  453. buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
  454. {
  455. struct em28xx_fh *fh = vq->priv_data;
  456. struct em28xx *dev = fh->dev;
  457. struct v4l2_frequency f;
  458. *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7) >> 3;
  459. if (0 == *count)
  460. *count = EM28XX_DEF_BUF;
  461. if (*count < EM28XX_MIN_BUF)
  462. *count = EM28XX_MIN_BUF;
  463. /* Ask tuner to go to analog or radio mode */
  464. memset(&f, 0, sizeof(f));
  465. f.frequency = dev->ctl_freq;
  466. f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  467. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
  468. return 0;
  469. }
  470. /* This is called *without* dev->slock held; please keep it that way */
  471. static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
  472. {
  473. struct em28xx_fh *fh = vq->priv_data;
  474. struct em28xx *dev = fh->dev;
  475. unsigned long flags = 0;
  476. if (in_interrupt())
  477. BUG();
  478. /* We used to wait for the buffer to finish here, but this didn't work
  479. because, as we were keeping the state as VIDEOBUF_QUEUED,
  480. videobuf_queue_cancel marked it as finished for us.
  481. (Also, it could wedge forever if the hardware was misconfigured.)
  482. This should be safe; by the time we get here, the buffer isn't
  483. queued anymore. If we ever start marking the buffers as
  484. VIDEOBUF_ACTIVE, it won't be, though.
  485. */
  486. spin_lock_irqsave(&dev->slock, flags);
  487. if (dev->isoc_ctl.buf == buf)
  488. dev->isoc_ctl.buf = NULL;
  489. spin_unlock_irqrestore(&dev->slock, flags);
  490. videobuf_vmalloc_free(&buf->vb);
  491. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  492. }
  493. static int
  494. buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
  495. enum v4l2_field field)
  496. {
  497. struct em28xx_fh *fh = vq->priv_data;
  498. struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
  499. struct em28xx *dev = fh->dev;
  500. int rc = 0, urb_init = 0;
  501. buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth + 7) >> 3;
  502. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  503. return -EINVAL;
  504. buf->vb.width = dev->width;
  505. buf->vb.height = dev->height;
  506. buf->vb.field = field;
  507. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  508. rc = videobuf_iolock(vq, &buf->vb, NULL);
  509. if (rc < 0)
  510. goto fail;
  511. }
  512. if (!dev->isoc_ctl.num_bufs)
  513. urb_init = 1;
  514. if (urb_init) {
  515. if (em28xx_vbi_supported(dev) == 1)
  516. rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
  517. EM28XX_NUM_BUFS,
  518. dev->max_pkt_size,
  519. em28xx_isoc_copy_vbi);
  520. else
  521. rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
  522. EM28XX_NUM_BUFS,
  523. dev->max_pkt_size,
  524. em28xx_isoc_copy);
  525. if (rc < 0)
  526. goto fail;
  527. }
  528. buf->vb.state = VIDEOBUF_PREPARED;
  529. return 0;
  530. fail:
  531. free_buffer(vq, buf);
  532. return rc;
  533. }
  534. static void
  535. buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  536. {
  537. struct em28xx_buffer *buf = container_of(vb,
  538. struct em28xx_buffer,
  539. vb);
  540. struct em28xx_fh *fh = vq->priv_data;
  541. struct em28xx *dev = fh->dev;
  542. struct em28xx_dmaqueue *vidq = &dev->vidq;
  543. buf->vb.state = VIDEOBUF_QUEUED;
  544. list_add_tail(&buf->vb.queue, &vidq->active);
  545. }
  546. static void buffer_release(struct videobuf_queue *vq,
  547. struct videobuf_buffer *vb)
  548. {
  549. struct em28xx_buffer *buf = container_of(vb,
  550. struct em28xx_buffer,
  551. vb);
  552. struct em28xx_fh *fh = vq->priv_data;
  553. struct em28xx *dev = (struct em28xx *)fh->dev;
  554. em28xx_isocdbg("em28xx: called buffer_release\n");
  555. free_buffer(vq, buf);
  556. }
  557. static struct videobuf_queue_ops em28xx_video_qops = {
  558. .buf_setup = buffer_setup,
  559. .buf_prepare = buffer_prepare,
  560. .buf_queue = buffer_queue,
  561. .buf_release = buffer_release,
  562. };
  563. /********************* v4l2 interface **************************************/
  564. static void video_mux(struct em28xx *dev, int index)
  565. {
  566. dev->ctl_input = index;
  567. dev->ctl_ainput = INPUT(index)->amux;
  568. dev->ctl_aoutput = INPUT(index)->aout;
  569. if (!dev->ctl_aoutput)
  570. dev->ctl_aoutput = EM28XX_AOUT_MASTER;
  571. v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
  572. INPUT(index)->vmux, 0, 0);
  573. if (dev->board.has_msp34xx) {
  574. if (dev->i2s_speed) {
  575. v4l2_device_call_all(&dev->v4l2_dev, 0, audio,
  576. s_i2s_clock_freq, dev->i2s_speed);
  577. }
  578. /* Note: this is msp3400 specific */
  579. v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
  580. dev->ctl_ainput, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
  581. }
  582. if (dev->board.adecoder != EM28XX_NOADECODER) {
  583. v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
  584. dev->ctl_ainput, dev->ctl_aoutput, 0);
  585. }
  586. em28xx_audio_analog_set(dev);
  587. }
  588. /* Usage lock check functions */
  589. static int res_get(struct em28xx_fh *fh)
  590. {
  591. struct em28xx *dev = fh->dev;
  592. int rc = 0;
  593. /* This instance already has stream_on */
  594. if (fh->stream_on)
  595. return rc;
  596. if (dev->stream_on)
  597. return -EBUSY;
  598. dev->stream_on = 1;
  599. fh->stream_on = 1;
  600. return rc;
  601. }
  602. static int res_check(struct em28xx_fh *fh)
  603. {
  604. return fh->stream_on;
  605. }
  606. static void res_free(struct em28xx_fh *fh)
  607. {
  608. struct em28xx *dev = fh->dev;
  609. fh->stream_on = 0;
  610. dev->stream_on = 0;
  611. }
  612. /*
  613. * ac97_queryctrl()
  614. * return the ac97 supported controls
  615. */
  616. static int ac97_queryctrl(struct v4l2_queryctrl *qc)
  617. {
  618. int i;
  619. for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
  620. if (qc->id && qc->id == ac97_qctrl[i].id) {
  621. memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
  622. return 0;
  623. }
  624. }
  625. /* Control is not ac97 related */
  626. return 1;
  627. }
  628. /*
  629. * ac97_get_ctrl()
  630. * return the current values for ac97 mute and volume
  631. */
  632. static int ac97_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
  633. {
  634. switch (ctrl->id) {
  635. case V4L2_CID_AUDIO_MUTE:
  636. ctrl->value = dev->mute;
  637. return 0;
  638. case V4L2_CID_AUDIO_VOLUME:
  639. ctrl->value = dev->volume;
  640. return 0;
  641. default:
  642. /* Control is not ac97 related */
  643. return 1;
  644. }
  645. }
  646. /*
  647. * ac97_set_ctrl()
  648. * set values for ac97 mute and volume
  649. */
  650. static int ac97_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
  651. {
  652. int i;
  653. for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++)
  654. if (ctrl->id == ac97_qctrl[i].id)
  655. goto handle;
  656. /* Announce that hasn't handle it */
  657. return 1;
  658. handle:
  659. if (ctrl->value < ac97_qctrl[i].minimum ||
  660. ctrl->value > ac97_qctrl[i].maximum)
  661. return -ERANGE;
  662. switch (ctrl->id) {
  663. case V4L2_CID_AUDIO_MUTE:
  664. dev->mute = ctrl->value;
  665. break;
  666. case V4L2_CID_AUDIO_VOLUME:
  667. dev->volume = ctrl->value;
  668. break;
  669. }
  670. return em28xx_audio_analog_set(dev);
  671. }
  672. static int check_dev(struct em28xx *dev)
  673. {
  674. if (dev->state & DEV_DISCONNECTED) {
  675. em28xx_errdev("v4l2 ioctl: device not present\n");
  676. return -ENODEV;
  677. }
  678. if (dev->state & DEV_MISCONFIGURED) {
  679. em28xx_errdev("v4l2 ioctl: device is misconfigured; "
  680. "close and open it again\n");
  681. return -EIO;
  682. }
  683. return 0;
  684. }
  685. static void get_scale(struct em28xx *dev,
  686. unsigned int width, unsigned int height,
  687. unsigned int *hscale, unsigned int *vscale)
  688. {
  689. unsigned int maxw = norm_maxw(dev);
  690. unsigned int maxh = norm_maxh(dev);
  691. *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
  692. if (*hscale >= 0x4000)
  693. *hscale = 0x3fff;
  694. *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
  695. if (*vscale >= 0x4000)
  696. *vscale = 0x3fff;
  697. }
  698. /* ------------------------------------------------------------------
  699. IOCTL vidioc handling
  700. ------------------------------------------------------------------*/
  701. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  702. struct v4l2_format *f)
  703. {
  704. struct em28xx_fh *fh = priv;
  705. struct em28xx *dev = fh->dev;
  706. mutex_lock(&dev->lock);
  707. f->fmt.pix.width = dev->width;
  708. f->fmt.pix.height = dev->height;
  709. f->fmt.pix.pixelformat = dev->format->fourcc;
  710. f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
  711. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
  712. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  713. /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
  714. if (dev->progressive)
  715. f->fmt.pix.field = V4L2_FIELD_NONE;
  716. else
  717. f->fmt.pix.field = dev->interlaced ?
  718. V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
  719. mutex_unlock(&dev->lock);
  720. return 0;
  721. }
  722. static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
  723. {
  724. unsigned int i;
  725. for (i = 0; i < ARRAY_SIZE(format); i++)
  726. if (format[i].fourcc == fourcc)
  727. return &format[i];
  728. return NULL;
  729. }
  730. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  731. struct v4l2_format *f)
  732. {
  733. struct em28xx_fh *fh = priv;
  734. struct em28xx *dev = fh->dev;
  735. unsigned int width = f->fmt.pix.width;
  736. unsigned int height = f->fmt.pix.height;
  737. unsigned int maxw = norm_maxw(dev);
  738. unsigned int maxh = norm_maxh(dev);
  739. unsigned int hscale, vscale;
  740. struct em28xx_fmt *fmt;
  741. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  742. if (!fmt) {
  743. em28xx_videodbg("Fourcc format (%08x) invalid.\n",
  744. f->fmt.pix.pixelformat);
  745. return -EINVAL;
  746. }
  747. if (dev->board.is_em2800) {
  748. /* the em2800 can only scale down to 50% */
  749. height = height > (3 * maxh / 4) ? maxh : maxh / 2;
  750. width = width > (3 * maxw / 4) ? maxw : maxw / 2;
  751. /* According to empiatech support the MaxPacketSize is too small
  752. * to support framesizes larger than 640x480 @ 30 fps or 640x576
  753. * @ 25 fps. As this would cut of a part of the image we prefer
  754. * 360x576 or 360x480 for now */
  755. if (width == maxw && height == maxh)
  756. width /= 2;
  757. } else {
  758. /* width must even because of the YUYV format
  759. height must be even because of interlacing */
  760. v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh, 1, 0);
  761. }
  762. get_scale(dev, width, height, &hscale, &vscale);
  763. width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
  764. height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
  765. f->fmt.pix.width = width;
  766. f->fmt.pix.height = height;
  767. f->fmt.pix.pixelformat = fmt->fourcc;
  768. f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
  769. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
  770. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  771. if (dev->progressive)
  772. f->fmt.pix.field = V4L2_FIELD_NONE;
  773. else
  774. f->fmt.pix.field = dev->interlaced ?
  775. V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
  776. return 0;
  777. }
  778. static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
  779. unsigned width, unsigned height)
  780. {
  781. struct em28xx_fmt *fmt;
  782. fmt = format_by_fourcc(fourcc);
  783. if (!fmt)
  784. return -EINVAL;
  785. dev->format = fmt;
  786. dev->width = width;
  787. dev->height = height;
  788. /* set new image size */
  789. get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
  790. em28xx_set_alternate(dev);
  791. em28xx_resolution_set(dev);
  792. return 0;
  793. }
  794. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  795. struct v4l2_format *f)
  796. {
  797. struct em28xx_fh *fh = priv;
  798. struct em28xx *dev = fh->dev;
  799. int rc;
  800. rc = check_dev(dev);
  801. if (rc < 0)
  802. return rc;
  803. mutex_lock(&dev->lock);
  804. vidioc_try_fmt_vid_cap(file, priv, f);
  805. if (videobuf_queue_is_busy(&fh->vb_vidq)) {
  806. em28xx_errdev("%s queue busy\n", __func__);
  807. rc = -EBUSY;
  808. goto out;
  809. }
  810. if (dev->stream_on && !fh->stream_on) {
  811. em28xx_errdev("%s device in use by another fh\n", __func__);
  812. rc = -EBUSY;
  813. goto out;
  814. }
  815. rc = em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
  816. f->fmt.pix.width, f->fmt.pix.height);
  817. out:
  818. mutex_unlock(&dev->lock);
  819. return rc;
  820. }
  821. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
  822. {
  823. struct em28xx_fh *fh = priv;
  824. struct em28xx *dev = fh->dev;
  825. struct v4l2_format f;
  826. int rc;
  827. rc = check_dev(dev);
  828. if (rc < 0)
  829. return rc;
  830. mutex_lock(&dev->lock);
  831. dev->norm = *norm;
  832. /* Adjusts width/height, if needed */
  833. f.fmt.pix.width = dev->width;
  834. f.fmt.pix.height = dev->height;
  835. vidioc_try_fmt_vid_cap(file, priv, &f);
  836. /* set new image size */
  837. dev->width = f.fmt.pix.width;
  838. dev->height = f.fmt.pix.height;
  839. get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
  840. em28xx_resolution_set(dev);
  841. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
  842. mutex_unlock(&dev->lock);
  843. return 0;
  844. }
  845. static int vidioc_g_parm(struct file *file, void *priv,
  846. struct v4l2_streamparm *p)
  847. {
  848. struct em28xx_fh *fh = priv;
  849. struct em28xx *dev = fh->dev;
  850. int rc = 0;
  851. if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  852. return -EINVAL;
  853. if (dev->board.is_webcam)
  854. rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0,
  855. video, g_parm, p);
  856. else
  857. v4l2_video_std_frame_period(dev->norm,
  858. &p->parm.capture.timeperframe);
  859. return rc;
  860. }
  861. static int vidioc_s_parm(struct file *file, void *priv,
  862. struct v4l2_streamparm *p)
  863. {
  864. struct em28xx_fh *fh = priv;
  865. struct em28xx *dev = fh->dev;
  866. if (!dev->board.is_webcam)
  867. return -EINVAL;
  868. if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  869. return -EINVAL;
  870. return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p);
  871. }
  872. static const char *iname[] = {
  873. [EM28XX_VMUX_COMPOSITE1] = "Composite1",
  874. [EM28XX_VMUX_COMPOSITE2] = "Composite2",
  875. [EM28XX_VMUX_COMPOSITE3] = "Composite3",
  876. [EM28XX_VMUX_COMPOSITE4] = "Composite4",
  877. [EM28XX_VMUX_SVIDEO] = "S-Video",
  878. [EM28XX_VMUX_TELEVISION] = "Television",
  879. [EM28XX_VMUX_CABLE] = "Cable TV",
  880. [EM28XX_VMUX_DVB] = "DVB",
  881. [EM28XX_VMUX_DEBUG] = "for debug only",
  882. };
  883. static int vidioc_enum_input(struct file *file, void *priv,
  884. struct v4l2_input *i)
  885. {
  886. struct em28xx_fh *fh = priv;
  887. struct em28xx *dev = fh->dev;
  888. unsigned int n;
  889. n = i->index;
  890. if (n >= MAX_EM28XX_INPUT)
  891. return -EINVAL;
  892. if (0 == INPUT(n)->type)
  893. return -EINVAL;
  894. i->index = n;
  895. i->type = V4L2_INPUT_TYPE_CAMERA;
  896. strcpy(i->name, iname[INPUT(n)->type]);
  897. if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
  898. (EM28XX_VMUX_CABLE == INPUT(n)->type))
  899. i->type = V4L2_INPUT_TYPE_TUNER;
  900. i->std = dev->vdev->tvnorms;
  901. return 0;
  902. }
  903. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  904. {
  905. struct em28xx_fh *fh = priv;
  906. struct em28xx *dev = fh->dev;
  907. *i = dev->ctl_input;
  908. return 0;
  909. }
  910. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  911. {
  912. struct em28xx_fh *fh = priv;
  913. struct em28xx *dev = fh->dev;
  914. int rc;
  915. rc = check_dev(dev);
  916. if (rc < 0)
  917. return rc;
  918. if (i >= MAX_EM28XX_INPUT)
  919. return -EINVAL;
  920. if (0 == INPUT(i)->type)
  921. return -EINVAL;
  922. dev->ctl_input = i;
  923. mutex_lock(&dev->lock);
  924. video_mux(dev, dev->ctl_input);
  925. mutex_unlock(&dev->lock);
  926. return 0;
  927. }
  928. static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  929. {
  930. struct em28xx_fh *fh = priv;
  931. struct em28xx *dev = fh->dev;
  932. if (!dev->audio_mode.has_audio)
  933. return -EINVAL;
  934. switch (a->index) {
  935. case EM28XX_AMUX_VIDEO:
  936. strcpy(a->name, "Television");
  937. break;
  938. case EM28XX_AMUX_LINE_IN:
  939. strcpy(a->name, "Line In");
  940. break;
  941. case EM28XX_AMUX_VIDEO2:
  942. strcpy(a->name, "Television alt");
  943. break;
  944. case EM28XX_AMUX_PHONE:
  945. strcpy(a->name, "Phone");
  946. break;
  947. case EM28XX_AMUX_MIC:
  948. strcpy(a->name, "Mic");
  949. break;
  950. case EM28XX_AMUX_CD:
  951. strcpy(a->name, "CD");
  952. break;
  953. case EM28XX_AMUX_AUX:
  954. strcpy(a->name, "Aux");
  955. break;
  956. case EM28XX_AMUX_PCM_OUT:
  957. strcpy(a->name, "PCM");
  958. break;
  959. default:
  960. return -EINVAL;
  961. }
  962. a->index = dev->ctl_ainput;
  963. a->capability = V4L2_AUDCAP_STEREO;
  964. return 0;
  965. }
  966. static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
  967. {
  968. struct em28xx_fh *fh = priv;
  969. struct em28xx *dev = fh->dev;
  970. if (!dev->audio_mode.has_audio)
  971. return -EINVAL;
  972. if (a->index >= MAX_EM28XX_INPUT)
  973. return -EINVAL;
  974. if (0 == INPUT(a->index)->type)
  975. return -EINVAL;
  976. mutex_lock(&dev->lock);
  977. dev->ctl_ainput = INPUT(a->index)->amux;
  978. dev->ctl_aoutput = INPUT(a->index)->aout;
  979. if (!dev->ctl_aoutput)
  980. dev->ctl_aoutput = EM28XX_AOUT_MASTER;
  981. mutex_unlock(&dev->lock);
  982. return 0;
  983. }
  984. static int vidioc_queryctrl(struct file *file, void *priv,
  985. struct v4l2_queryctrl *qc)
  986. {
  987. struct em28xx_fh *fh = priv;
  988. struct em28xx *dev = fh->dev;
  989. int id = qc->id;
  990. int rc;
  991. rc = check_dev(dev);
  992. if (rc < 0)
  993. return rc;
  994. memset(qc, 0, sizeof(*qc));
  995. qc->id = id;
  996. /* enumberate AC97 controls */
  997. if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
  998. rc = ac97_queryctrl(qc);
  999. if (!rc)
  1000. return 0;
  1001. }
  1002. /* enumberate V4L2 device controls */
  1003. mutex_lock(&dev->lock);
  1004. v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
  1005. mutex_unlock(&dev->lock);
  1006. if (qc->type)
  1007. return 0;
  1008. else
  1009. return -EINVAL;
  1010. }
  1011. static int vidioc_g_ctrl(struct file *file, void *priv,
  1012. struct v4l2_control *ctrl)
  1013. {
  1014. struct em28xx_fh *fh = priv;
  1015. struct em28xx *dev = fh->dev;
  1016. int rc;
  1017. rc = check_dev(dev);
  1018. if (rc < 0)
  1019. return rc;
  1020. rc = 0;
  1021. mutex_lock(&dev->lock);
  1022. /* Set an AC97 control */
  1023. if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
  1024. rc = ac97_get_ctrl(dev, ctrl);
  1025. else
  1026. rc = 1;
  1027. /* It were not an AC97 control. Sends it to the v4l2 dev interface */
  1028. if (rc == 1) {
  1029. v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
  1030. rc = 0;
  1031. }
  1032. mutex_unlock(&dev->lock);
  1033. return rc;
  1034. }
  1035. static int vidioc_s_ctrl(struct file *file, void *priv,
  1036. struct v4l2_control *ctrl)
  1037. {
  1038. struct em28xx_fh *fh = priv;
  1039. struct em28xx *dev = fh->dev;
  1040. int rc;
  1041. rc = check_dev(dev);
  1042. if (rc < 0)
  1043. return rc;
  1044. mutex_lock(&dev->lock);
  1045. /* Set an AC97 control */
  1046. if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
  1047. rc = ac97_set_ctrl(dev, ctrl);
  1048. else
  1049. rc = 1;
  1050. /* It isn't an AC97 control. Sends it to the v4l2 dev interface */
  1051. if (rc == 1) {
  1052. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_ctrl, ctrl);
  1053. /*
  1054. * In the case of non-AC97 volume controls, we still need
  1055. * to do some setups at em28xx, in order to mute/unmute
  1056. * and to adjust audio volume. However, the value ranges
  1057. * should be checked by the corresponding V4L subdriver.
  1058. */
  1059. switch (ctrl->id) {
  1060. case V4L2_CID_AUDIO_MUTE:
  1061. dev->mute = ctrl->value;
  1062. rc = em28xx_audio_analog_set(dev);
  1063. break;
  1064. case V4L2_CID_AUDIO_VOLUME:
  1065. dev->volume = ctrl->value;
  1066. rc = em28xx_audio_analog_set(dev);
  1067. }
  1068. }
  1069. mutex_unlock(&dev->lock);
  1070. return rc;
  1071. }
  1072. static int vidioc_g_tuner(struct file *file, void *priv,
  1073. struct v4l2_tuner *t)
  1074. {
  1075. struct em28xx_fh *fh = priv;
  1076. struct em28xx *dev = fh->dev;
  1077. int rc;
  1078. rc = check_dev(dev);
  1079. if (rc < 0)
  1080. return rc;
  1081. if (0 != t->index)
  1082. return -EINVAL;
  1083. strcpy(t->name, "Tuner");
  1084. mutex_lock(&dev->lock);
  1085. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
  1086. mutex_unlock(&dev->lock);
  1087. return 0;
  1088. }
  1089. static int vidioc_s_tuner(struct file *file, void *priv,
  1090. struct v4l2_tuner *t)
  1091. {
  1092. struct em28xx_fh *fh = priv;
  1093. struct em28xx *dev = fh->dev;
  1094. int rc;
  1095. rc = check_dev(dev);
  1096. if (rc < 0)
  1097. return rc;
  1098. if (0 != t->index)
  1099. return -EINVAL;
  1100. mutex_lock(&dev->lock);
  1101. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
  1102. mutex_unlock(&dev->lock);
  1103. return 0;
  1104. }
  1105. static int vidioc_g_frequency(struct file *file, void *priv,
  1106. struct v4l2_frequency *f)
  1107. {
  1108. struct em28xx_fh *fh = priv;
  1109. struct em28xx *dev = fh->dev;
  1110. mutex_lock(&dev->lock);
  1111. f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1112. f->frequency = dev->ctl_freq;
  1113. mutex_unlock(&dev->lock);
  1114. return 0;
  1115. }
  1116. static int vidioc_s_frequency(struct file *file, void *priv,
  1117. struct v4l2_frequency *f)
  1118. {
  1119. struct em28xx_fh *fh = priv;
  1120. struct em28xx *dev = fh->dev;
  1121. int rc;
  1122. rc = check_dev(dev);
  1123. if (rc < 0)
  1124. return rc;
  1125. if (0 != f->tuner)
  1126. return -EINVAL;
  1127. if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
  1128. return -EINVAL;
  1129. if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
  1130. return -EINVAL;
  1131. mutex_lock(&dev->lock);
  1132. dev->ctl_freq = f->frequency;
  1133. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
  1134. mutex_unlock(&dev->lock);
  1135. return 0;
  1136. }
  1137. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1138. static int em28xx_reg_len(int reg)
  1139. {
  1140. switch (reg) {
  1141. case EM28XX_R40_AC97LSB:
  1142. case EM28XX_R30_HSCALELOW:
  1143. case EM28XX_R32_VSCALELOW:
  1144. return 2;
  1145. default:
  1146. return 1;
  1147. }
  1148. }
  1149. static int vidioc_g_chip_ident(struct file *file, void *priv,
  1150. struct v4l2_dbg_chip_ident *chip)
  1151. {
  1152. struct em28xx_fh *fh = priv;
  1153. struct em28xx *dev = fh->dev;
  1154. chip->ident = V4L2_IDENT_NONE;
  1155. chip->revision = 0;
  1156. v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
  1157. return 0;
  1158. }
  1159. static int vidioc_g_register(struct file *file, void *priv,
  1160. struct v4l2_dbg_register *reg)
  1161. {
  1162. struct em28xx_fh *fh = priv;
  1163. struct em28xx *dev = fh->dev;
  1164. int ret;
  1165. switch (reg->match.type) {
  1166. case V4L2_CHIP_MATCH_AC97:
  1167. mutex_lock(&dev->lock);
  1168. ret = em28xx_read_ac97(dev, reg->reg);
  1169. mutex_unlock(&dev->lock);
  1170. if (ret < 0)
  1171. return ret;
  1172. reg->val = ret;
  1173. reg->size = 1;
  1174. return 0;
  1175. case V4L2_CHIP_MATCH_I2C_DRIVER:
  1176. v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
  1177. return 0;
  1178. case V4L2_CHIP_MATCH_I2C_ADDR:
  1179. /* TODO: is this correct? */
  1180. v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
  1181. return 0;
  1182. default:
  1183. if (!v4l2_chip_match_host(&reg->match))
  1184. return -EINVAL;
  1185. }
  1186. /* Match host */
  1187. reg->size = em28xx_reg_len(reg->reg);
  1188. if (reg->size == 1) {
  1189. mutex_lock(&dev->lock);
  1190. ret = em28xx_read_reg(dev, reg->reg);
  1191. mutex_unlock(&dev->lock);
  1192. if (ret < 0)
  1193. return ret;
  1194. reg->val = ret;
  1195. } else {
  1196. __le16 val = 0;
  1197. mutex_lock(&dev->lock);
  1198. ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
  1199. reg->reg, (char *)&val, 2);
  1200. mutex_unlock(&dev->lock);
  1201. if (ret < 0)
  1202. return ret;
  1203. reg->val = le16_to_cpu(val);
  1204. }
  1205. return 0;
  1206. }
  1207. static int vidioc_s_register(struct file *file, void *priv,
  1208. struct v4l2_dbg_register *reg)
  1209. {
  1210. struct em28xx_fh *fh = priv;
  1211. struct em28xx *dev = fh->dev;
  1212. __le16 buf;
  1213. int rc;
  1214. switch (reg->match.type) {
  1215. case V4L2_CHIP_MATCH_AC97:
  1216. mutex_lock(&dev->lock);
  1217. rc = em28xx_write_ac97(dev, reg->reg, reg->val);
  1218. mutex_unlock(&dev->lock);
  1219. return rc;
  1220. case V4L2_CHIP_MATCH_I2C_DRIVER:
  1221. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
  1222. return 0;
  1223. case V4L2_CHIP_MATCH_I2C_ADDR:
  1224. /* TODO: is this correct? */
  1225. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
  1226. return 0;
  1227. default:
  1228. if (!v4l2_chip_match_host(&reg->match))
  1229. return -EINVAL;
  1230. }
  1231. /* Match host */
  1232. buf = cpu_to_le16(reg->val);
  1233. mutex_lock(&dev->lock);
  1234. rc = em28xx_write_regs(dev, reg->reg, (char *)&buf,
  1235. em28xx_reg_len(reg->reg));
  1236. mutex_unlock(&dev->lock);
  1237. return rc;
  1238. }
  1239. #endif
  1240. static int vidioc_cropcap(struct file *file, void *priv,
  1241. struct v4l2_cropcap *cc)
  1242. {
  1243. struct em28xx_fh *fh = priv;
  1244. struct em28xx *dev = fh->dev;
  1245. if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1246. return -EINVAL;
  1247. cc->bounds.left = 0;
  1248. cc->bounds.top = 0;
  1249. cc->bounds.width = dev->width;
  1250. cc->bounds.height = dev->height;
  1251. cc->defrect = cc->bounds;
  1252. cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
  1253. cc->pixelaspect.denominator = 59;
  1254. return 0;
  1255. }
  1256. static int vidioc_streamon(struct file *file, void *priv,
  1257. enum v4l2_buf_type type)
  1258. {
  1259. struct em28xx_fh *fh = priv;
  1260. struct em28xx *dev = fh->dev;
  1261. int rc;
  1262. rc = check_dev(dev);
  1263. if (rc < 0)
  1264. return rc;
  1265. mutex_lock(&dev->lock);
  1266. rc = res_get(fh);
  1267. if (likely(rc >= 0))
  1268. rc = videobuf_streamon(&fh->vb_vidq);
  1269. mutex_unlock(&dev->lock);
  1270. return rc;
  1271. }
  1272. static int vidioc_streamoff(struct file *file, void *priv,
  1273. enum v4l2_buf_type type)
  1274. {
  1275. struct em28xx_fh *fh = priv;
  1276. struct em28xx *dev = fh->dev;
  1277. int rc;
  1278. rc = check_dev(dev);
  1279. if (rc < 0)
  1280. return rc;
  1281. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1282. return -EINVAL;
  1283. if (type != fh->type)
  1284. return -EINVAL;
  1285. mutex_lock(&dev->lock);
  1286. videobuf_streamoff(&fh->vb_vidq);
  1287. res_free(fh);
  1288. mutex_unlock(&dev->lock);
  1289. return 0;
  1290. }
  1291. static int vidioc_querycap(struct file *file, void *priv,
  1292. struct v4l2_capability *cap)
  1293. {
  1294. struct em28xx_fh *fh = priv;
  1295. struct em28xx *dev = fh->dev;
  1296. strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
  1297. strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
  1298. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  1299. cap->version = EM28XX_VERSION_CODE;
  1300. cap->capabilities =
  1301. V4L2_CAP_SLICED_VBI_CAPTURE |
  1302. V4L2_CAP_VIDEO_CAPTURE |
  1303. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  1304. if (dev->audio_mode.has_audio)
  1305. cap->capabilities |= V4L2_CAP_AUDIO;
  1306. if (dev->tuner_type != TUNER_ABSENT)
  1307. cap->capabilities |= V4L2_CAP_TUNER;
  1308. return 0;
  1309. }
  1310. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  1311. struct v4l2_fmtdesc *f)
  1312. {
  1313. if (unlikely(f->index >= ARRAY_SIZE(format)))
  1314. return -EINVAL;
  1315. strlcpy(f->description, format[f->index].name, sizeof(f->description));
  1316. f->pixelformat = format[f->index].fourcc;
  1317. return 0;
  1318. }
  1319. /* Sliced VBI ioctls */
  1320. static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
  1321. struct v4l2_format *f)
  1322. {
  1323. struct em28xx_fh *fh = priv;
  1324. struct em28xx *dev = fh->dev;
  1325. int rc;
  1326. rc = check_dev(dev);
  1327. if (rc < 0)
  1328. return rc;
  1329. mutex_lock(&dev->lock);
  1330. f->fmt.sliced.service_set = 0;
  1331. v4l2_device_call_all(&dev->v4l2_dev, 0, video, g_fmt, f);
  1332. if (f->fmt.sliced.service_set == 0)
  1333. rc = -EINVAL;
  1334. mutex_unlock(&dev->lock);
  1335. return rc;
  1336. }
  1337. static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
  1338. struct v4l2_format *f)
  1339. {
  1340. struct em28xx_fh *fh = priv;
  1341. struct em28xx *dev = fh->dev;
  1342. int rc;
  1343. rc = check_dev(dev);
  1344. if (rc < 0)
  1345. return rc;
  1346. mutex_lock(&dev->lock);
  1347. v4l2_device_call_all(&dev->v4l2_dev, 0, video, g_fmt, f);
  1348. mutex_unlock(&dev->lock);
  1349. if (f->fmt.sliced.service_set == 0)
  1350. return -EINVAL;
  1351. return 0;
  1352. }
  1353. static int vidioc_reqbufs(struct file *file, void *priv,
  1354. struct v4l2_requestbuffers *rb)
  1355. {
  1356. struct em28xx_fh *fh = priv;
  1357. struct em28xx *dev = fh->dev;
  1358. int rc;
  1359. rc = check_dev(dev);
  1360. if (rc < 0)
  1361. return rc;
  1362. return videobuf_reqbufs(&fh->vb_vidq, rb);
  1363. }
  1364. static int vidioc_querybuf(struct file *file, void *priv,
  1365. struct v4l2_buffer *b)
  1366. {
  1367. struct em28xx_fh *fh = priv;
  1368. struct em28xx *dev = fh->dev;
  1369. int rc;
  1370. rc = check_dev(dev);
  1371. if (rc < 0)
  1372. return rc;
  1373. return videobuf_querybuf(&fh->vb_vidq, b);
  1374. }
  1375. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1376. {
  1377. struct em28xx_fh *fh = priv;
  1378. struct em28xx *dev = fh->dev;
  1379. int rc;
  1380. rc = check_dev(dev);
  1381. if (rc < 0)
  1382. return rc;
  1383. return videobuf_qbuf(&fh->vb_vidq, b);
  1384. }
  1385. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1386. {
  1387. struct em28xx_fh *fh = priv;
  1388. struct em28xx *dev = fh->dev;
  1389. int rc;
  1390. rc = check_dev(dev);
  1391. if (rc < 0)
  1392. return rc;
  1393. return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK);
  1394. }
  1395. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1396. static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
  1397. {
  1398. struct em28xx_fh *fh = priv;
  1399. return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
  1400. }
  1401. #endif
  1402. /* ----------------------------------------------------------- */
  1403. /* RADIO ESPECIFIC IOCTLS */
  1404. /* ----------------------------------------------------------- */
  1405. static int radio_querycap(struct file *file, void *priv,
  1406. struct v4l2_capability *cap)
  1407. {
  1408. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1409. strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
  1410. strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
  1411. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  1412. cap->version = EM28XX_VERSION_CODE;
  1413. cap->capabilities = V4L2_CAP_TUNER;
  1414. return 0;
  1415. }
  1416. static int radio_g_tuner(struct file *file, void *priv,
  1417. struct v4l2_tuner *t)
  1418. {
  1419. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1420. if (unlikely(t->index > 0))
  1421. return -EINVAL;
  1422. strcpy(t->name, "Radio");
  1423. t->type = V4L2_TUNER_RADIO;
  1424. mutex_lock(&dev->lock);
  1425. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
  1426. mutex_unlock(&dev->lock);
  1427. return 0;
  1428. }
  1429. static int radio_enum_input(struct file *file, void *priv,
  1430. struct v4l2_input *i)
  1431. {
  1432. if (i->index != 0)
  1433. return -EINVAL;
  1434. strcpy(i->name, "Radio");
  1435. i->type = V4L2_INPUT_TYPE_TUNER;
  1436. return 0;
  1437. }
  1438. static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  1439. {
  1440. if (unlikely(a->index))
  1441. return -EINVAL;
  1442. strcpy(a->name, "Radio");
  1443. return 0;
  1444. }
  1445. static int radio_s_tuner(struct file *file, void *priv,
  1446. struct v4l2_tuner *t)
  1447. {
  1448. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1449. if (0 != t->index)
  1450. return -EINVAL;
  1451. mutex_lock(&dev->lock);
  1452. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
  1453. mutex_unlock(&dev->lock);
  1454. return 0;
  1455. }
  1456. static int radio_s_audio(struct file *file, void *fh,
  1457. struct v4l2_audio *a)
  1458. {
  1459. return 0;
  1460. }
  1461. static int radio_s_input(struct file *file, void *fh, unsigned int i)
  1462. {
  1463. return 0;
  1464. }
  1465. static int radio_queryctrl(struct file *file, void *priv,
  1466. struct v4l2_queryctrl *qc)
  1467. {
  1468. int i;
  1469. if (qc->id < V4L2_CID_BASE ||
  1470. qc->id >= V4L2_CID_LASTP1)
  1471. return -EINVAL;
  1472. for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
  1473. if (qc->id && qc->id == ac97_qctrl[i].id) {
  1474. memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
  1475. return 0;
  1476. }
  1477. }
  1478. return -EINVAL;
  1479. }
  1480. /*
  1481. * em28xx_v4l2_open()
  1482. * inits the device and starts isoc transfer
  1483. */
  1484. static int em28xx_v4l2_open(struct file *filp)
  1485. {
  1486. int minor = video_devdata(filp)->minor;
  1487. int errCode = 0, radio;
  1488. struct em28xx *dev;
  1489. enum v4l2_buf_type fh_type;
  1490. struct em28xx_fh *fh;
  1491. enum v4l2_field field;
  1492. dev = em28xx_get_device(minor, &fh_type, &radio);
  1493. if (NULL == dev)
  1494. return -ENODEV;
  1495. mutex_lock(&dev->lock);
  1496. em28xx_videodbg("open minor=%d type=%s users=%d\n",
  1497. minor, v4l2_type_names[fh_type], dev->users);
  1498. fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
  1499. if (!fh) {
  1500. em28xx_errdev("em28xx-video.c: Out of memory?!\n");
  1501. mutex_unlock(&dev->lock);
  1502. return -ENOMEM;
  1503. }
  1504. fh->dev = dev;
  1505. fh->radio = radio;
  1506. fh->type = fh_type;
  1507. filp->private_data = fh;
  1508. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
  1509. em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
  1510. em28xx_set_alternate(dev);
  1511. em28xx_resolution_set(dev);
  1512. /* Needed, since GPIO might have disabled power of
  1513. some i2c device
  1514. */
  1515. em28xx_wake_i2c(dev);
  1516. }
  1517. if (fh->radio) {
  1518. em28xx_videodbg("video_open: setting radio device\n");
  1519. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
  1520. }
  1521. dev->users++;
  1522. if (dev->progressive)
  1523. field = V4L2_FIELD_NONE;
  1524. else
  1525. field = V4L2_FIELD_INTERLACED;
  1526. videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
  1527. NULL, &dev->slock, fh->type, field,
  1528. sizeof(struct em28xx_buffer), fh);
  1529. mutex_unlock(&dev->lock);
  1530. return errCode;
  1531. }
  1532. /*
  1533. * em28xx_realease_resources()
  1534. * unregisters the v4l2,i2c and usb devices
  1535. * called when the device gets disconected or at module unload
  1536. */
  1537. void em28xx_release_analog_resources(struct em28xx *dev)
  1538. {
  1539. /*FIXME: I2C IR should be disconnected */
  1540. if (dev->radio_dev) {
  1541. if (-1 != dev->radio_dev->minor)
  1542. video_unregister_device(dev->radio_dev);
  1543. else
  1544. video_device_release(dev->radio_dev);
  1545. dev->radio_dev = NULL;
  1546. }
  1547. if (dev->vbi_dev) {
  1548. em28xx_info("V4L2 device /dev/vbi%d deregistered\n",
  1549. dev->vbi_dev->num);
  1550. if (-1 != dev->vbi_dev->minor)
  1551. video_unregister_device(dev->vbi_dev);
  1552. else
  1553. video_device_release(dev->vbi_dev);
  1554. dev->vbi_dev = NULL;
  1555. }
  1556. if (dev->vdev) {
  1557. em28xx_info("V4L2 device /dev/video%d deregistered\n",
  1558. dev->vdev->num);
  1559. if (-1 != dev->vdev->minor)
  1560. video_unregister_device(dev->vdev);
  1561. else
  1562. video_device_release(dev->vdev);
  1563. dev->vdev = NULL;
  1564. }
  1565. }
  1566. /*
  1567. * em28xx_v4l2_close()
  1568. * stops streaming and deallocates all resources allocated by the v4l2
  1569. * calls and ioctls
  1570. */
  1571. static int em28xx_v4l2_close(struct file *filp)
  1572. {
  1573. struct em28xx_fh *fh = filp->private_data;
  1574. struct em28xx *dev = fh->dev;
  1575. int errCode;
  1576. em28xx_videodbg("users=%d\n", dev->users);
  1577. mutex_lock(&dev->lock);
  1578. if (res_check(fh))
  1579. res_free(fh);
  1580. if (dev->users == 1) {
  1581. videobuf_stop(&fh->vb_vidq);
  1582. videobuf_mmap_free(&fh->vb_vidq);
  1583. /* the device is already disconnect,
  1584. free the remaining resources */
  1585. if (dev->state & DEV_DISCONNECTED) {
  1586. em28xx_release_resources(dev);
  1587. mutex_unlock(&dev->lock);
  1588. kfree(dev);
  1589. return 0;
  1590. }
  1591. /* Save some power by putting tuner to sleep */
  1592. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_standby);
  1593. /* do this before setting alternate! */
  1594. em28xx_uninit_isoc(dev);
  1595. em28xx_set_mode(dev, EM28XX_SUSPEND);
  1596. /* set alternate 0 */
  1597. dev->alt = 0;
  1598. em28xx_videodbg("setting alternate 0\n");
  1599. errCode = usb_set_interface(dev->udev, 0, 0);
  1600. if (errCode < 0) {
  1601. em28xx_errdev("cannot change alternate number to "
  1602. "0 (error=%i)\n", errCode);
  1603. }
  1604. }
  1605. kfree(fh);
  1606. dev->users--;
  1607. wake_up_interruptible_nr(&dev->open, 1);
  1608. mutex_unlock(&dev->lock);
  1609. return 0;
  1610. }
  1611. /*
  1612. * em28xx_v4l2_read()
  1613. * will allocate buffers when called for the first time
  1614. */
  1615. static ssize_t
  1616. em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
  1617. loff_t *pos)
  1618. {
  1619. struct em28xx_fh *fh = filp->private_data;
  1620. struct em28xx *dev = fh->dev;
  1621. int rc;
  1622. rc = check_dev(dev);
  1623. if (rc < 0)
  1624. return rc;
  1625. /* FIXME: read() is not prepared to allow changing the video
  1626. resolution while streaming. Seems a bug at em28xx_set_fmt
  1627. */
  1628. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  1629. mutex_lock(&dev->lock);
  1630. rc = res_get(fh);
  1631. mutex_unlock(&dev->lock);
  1632. if (unlikely(rc < 0))
  1633. return rc;
  1634. return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
  1635. filp->f_flags & O_NONBLOCK);
  1636. }
  1637. return 0;
  1638. }
  1639. /*
  1640. * em28xx_v4l2_poll()
  1641. * will allocate buffers when called for the first time
  1642. */
  1643. static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
  1644. {
  1645. struct em28xx_fh *fh = filp->private_data;
  1646. struct em28xx *dev = fh->dev;
  1647. int rc;
  1648. rc = check_dev(dev);
  1649. if (rc < 0)
  1650. return rc;
  1651. mutex_lock(&dev->lock);
  1652. rc = res_get(fh);
  1653. mutex_unlock(&dev->lock);
  1654. if (unlikely(rc < 0))
  1655. return POLLERR;
  1656. if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
  1657. return POLLERR;
  1658. return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
  1659. }
  1660. /*
  1661. * em28xx_v4l2_mmap()
  1662. */
  1663. static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
  1664. {
  1665. struct em28xx_fh *fh = filp->private_data;
  1666. struct em28xx *dev = fh->dev;
  1667. int rc;
  1668. rc = check_dev(dev);
  1669. if (rc < 0)
  1670. return rc;
  1671. mutex_lock(&dev->lock);
  1672. rc = res_get(fh);
  1673. mutex_unlock(&dev->lock);
  1674. if (unlikely(rc < 0))
  1675. return rc;
  1676. rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
  1677. em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
  1678. (unsigned long)vma->vm_start,
  1679. (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
  1680. rc);
  1681. return rc;
  1682. }
  1683. static const struct v4l2_file_operations em28xx_v4l_fops = {
  1684. .owner = THIS_MODULE,
  1685. .open = em28xx_v4l2_open,
  1686. .release = em28xx_v4l2_close,
  1687. .read = em28xx_v4l2_read,
  1688. .poll = em28xx_v4l2_poll,
  1689. .mmap = em28xx_v4l2_mmap,
  1690. .ioctl = video_ioctl2,
  1691. };
  1692. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1693. .vidioc_querycap = vidioc_querycap,
  1694. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1695. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1696. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1697. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1698. .vidioc_g_audio = vidioc_g_audio,
  1699. .vidioc_s_audio = vidioc_s_audio,
  1700. .vidioc_cropcap = vidioc_cropcap,
  1701. .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
  1702. .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
  1703. .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
  1704. .vidioc_reqbufs = vidioc_reqbufs,
  1705. .vidioc_querybuf = vidioc_querybuf,
  1706. .vidioc_qbuf = vidioc_qbuf,
  1707. .vidioc_dqbuf = vidioc_dqbuf,
  1708. .vidioc_s_std = vidioc_s_std,
  1709. .vidioc_g_parm = vidioc_g_parm,
  1710. .vidioc_s_parm = vidioc_s_parm,
  1711. .vidioc_enum_input = vidioc_enum_input,
  1712. .vidioc_g_input = vidioc_g_input,
  1713. .vidioc_s_input = vidioc_s_input,
  1714. .vidioc_queryctrl = vidioc_queryctrl,
  1715. .vidioc_g_ctrl = vidioc_g_ctrl,
  1716. .vidioc_s_ctrl = vidioc_s_ctrl,
  1717. .vidioc_streamon = vidioc_streamon,
  1718. .vidioc_streamoff = vidioc_streamoff,
  1719. .vidioc_g_tuner = vidioc_g_tuner,
  1720. .vidioc_s_tuner = vidioc_s_tuner,
  1721. .vidioc_g_frequency = vidioc_g_frequency,
  1722. .vidioc_s_frequency = vidioc_s_frequency,
  1723. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1724. .vidioc_g_register = vidioc_g_register,
  1725. .vidioc_s_register = vidioc_s_register,
  1726. .vidioc_g_chip_ident = vidioc_g_chip_ident,
  1727. #endif
  1728. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1729. .vidiocgmbuf = vidiocgmbuf,
  1730. #endif
  1731. };
  1732. static const struct video_device em28xx_video_template = {
  1733. .fops = &em28xx_v4l_fops,
  1734. .release = video_device_release,
  1735. .ioctl_ops = &video_ioctl_ops,
  1736. .minor = -1,
  1737. .tvnorms = V4L2_STD_ALL,
  1738. .current_norm = V4L2_STD_PAL,
  1739. };
  1740. static const struct v4l2_file_operations radio_fops = {
  1741. .owner = THIS_MODULE,
  1742. .open = em28xx_v4l2_open,
  1743. .release = em28xx_v4l2_close,
  1744. .ioctl = video_ioctl2,
  1745. };
  1746. static const struct v4l2_ioctl_ops radio_ioctl_ops = {
  1747. .vidioc_querycap = radio_querycap,
  1748. .vidioc_g_tuner = radio_g_tuner,
  1749. .vidioc_enum_input = radio_enum_input,
  1750. .vidioc_g_audio = radio_g_audio,
  1751. .vidioc_s_tuner = radio_s_tuner,
  1752. .vidioc_s_audio = radio_s_audio,
  1753. .vidioc_s_input = radio_s_input,
  1754. .vidioc_queryctrl = radio_queryctrl,
  1755. .vidioc_g_ctrl = vidioc_g_ctrl,
  1756. .vidioc_s_ctrl = vidioc_s_ctrl,
  1757. .vidioc_g_frequency = vidioc_g_frequency,
  1758. .vidioc_s_frequency = vidioc_s_frequency,
  1759. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1760. .vidioc_g_register = vidioc_g_register,
  1761. .vidioc_s_register = vidioc_s_register,
  1762. #endif
  1763. };
  1764. static struct video_device em28xx_radio_template = {
  1765. .name = "em28xx-radio",
  1766. .fops = &radio_fops,
  1767. .ioctl_ops = &radio_ioctl_ops,
  1768. .minor = -1,
  1769. };
  1770. /******************************** usb interface ******************************/
  1771. static struct video_device *em28xx_vdev_init(struct em28xx *dev,
  1772. const struct video_device *template,
  1773. const char *type_name)
  1774. {
  1775. struct video_device *vfd;
  1776. vfd = video_device_alloc();
  1777. if (NULL == vfd)
  1778. return NULL;
  1779. *vfd = *template;
  1780. vfd->minor = -1;
  1781. vfd->v4l2_dev = &dev->v4l2_dev;
  1782. vfd->release = video_device_release;
  1783. vfd->debug = video_debug;
  1784. snprintf(vfd->name, sizeof(vfd->name), "%s %s",
  1785. dev->name, type_name);
  1786. return vfd;
  1787. }
  1788. int em28xx_register_analog_devices(struct em28xx *dev)
  1789. {
  1790. u8 val;
  1791. int ret;
  1792. printk(KERN_INFO "%s: v4l2 driver version %d.%d.%d\n",
  1793. dev->name,
  1794. (EM28XX_VERSION_CODE >> 16) & 0xff,
  1795. (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
  1796. /* set default norm */
  1797. dev->norm = em28xx_video_template.current_norm;
  1798. dev->interlaced = EM28XX_INTERLACED_DEFAULT;
  1799. dev->ctl_input = 0;
  1800. /* Analog specific initialization */
  1801. dev->format = &format[0];
  1802. em28xx_set_video_format(dev, format[0].fourcc,
  1803. norm_maxw(dev), norm_maxh(dev));
  1804. video_mux(dev, dev->ctl_input);
  1805. /* Audio defaults */
  1806. dev->mute = 1;
  1807. dev->volume = 0x1f;
  1808. /* enable vbi capturing */
  1809. /* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
  1810. val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
  1811. em28xx_write_reg(dev, EM28XX_R0F_XCLK,
  1812. (EM28XX_XCLK_AUDIO_UNMUTE | val));
  1813. em28xx_write_reg(dev, EM28XX_R11_VINCTRL, 0x51);
  1814. em28xx_set_outfmt(dev);
  1815. em28xx_colorlevels_set_default(dev);
  1816. em28xx_compression_disable(dev);
  1817. /* allocate and fill video video_device struct */
  1818. dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
  1819. if (!dev->vdev) {
  1820. em28xx_errdev("cannot allocate video_device.\n");
  1821. return -ENODEV;
  1822. }
  1823. /* register v4l2 video video_device */
  1824. ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
  1825. video_nr[dev->devno]);
  1826. if (ret) {
  1827. em28xx_errdev("unable to register video device (error=%i).\n",
  1828. ret);
  1829. return ret;
  1830. }
  1831. /* Allocate and fill vbi video_device struct */
  1832. dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template, "vbi");
  1833. /* register v4l2 vbi video_device */
  1834. ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
  1835. vbi_nr[dev->devno]);
  1836. if (ret < 0) {
  1837. em28xx_errdev("unable to register vbi device\n");
  1838. return ret;
  1839. }
  1840. if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
  1841. dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
  1842. "radio");
  1843. if (!dev->radio_dev) {
  1844. em28xx_errdev("cannot allocate video_device.\n");
  1845. return -ENODEV;
  1846. }
  1847. ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
  1848. radio_nr[dev->devno]);
  1849. if (ret < 0) {
  1850. em28xx_errdev("can't register radio device\n");
  1851. return ret;
  1852. }
  1853. em28xx_info("Registered radio device as /dev/radio%d\n",
  1854. dev->radio_dev->num);
  1855. }
  1856. em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
  1857. dev->vdev->num, dev->vbi_dev->num);
  1858. return 0;
  1859. }