em28xx-video.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596
  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 <linux/slab.h>
  33. #include "em28xx.h"
  34. #include <media/v4l2-common.h>
  35. #include <media/v4l2-ioctl.h>
  36. #include <media/v4l2-chip-ident.h>
  37. #include <media/msp3400.h>
  38. #include <media/tuner.h>
  39. #define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
  40. "Markus Rechberger <mrechberger@gmail.com>, " \
  41. "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
  42. "Sascha Sommer <saschasommer@freenet.de>"
  43. #define DRIVER_DESC "Empia em28xx based USB video device driver"
  44. #define EM28XX_VERSION_CODE KERNEL_VERSION(0, 1, 2)
  45. #define em28xx_videodbg(fmt, arg...) do {\
  46. if (video_debug) \
  47. printk(KERN_INFO "%s %s :"fmt, \
  48. dev->name, __func__ , ##arg); } while (0)
  49. static unsigned int isoc_debug;
  50. module_param(isoc_debug, int, 0644);
  51. MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
  52. #define em28xx_isocdbg(fmt, arg...) \
  53. do {\
  54. if (isoc_debug) { \
  55. printk(KERN_INFO "%s %s :"fmt, \
  56. dev->name, __func__ , ##arg); \
  57. } \
  58. } while (0)
  59. MODULE_AUTHOR(DRIVER_AUTHOR);
  60. MODULE_DESCRIPTION(DRIVER_DESC);
  61. MODULE_LICENSE("GPL");
  62. static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
  63. static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
  64. static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
  65. module_param_array(video_nr, int, NULL, 0444);
  66. module_param_array(vbi_nr, int, NULL, 0444);
  67. module_param_array(radio_nr, int, NULL, 0444);
  68. MODULE_PARM_DESC(video_nr, "video device numbers");
  69. MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
  70. MODULE_PARM_DESC(radio_nr, "radio device numbers");
  71. static unsigned int video_debug;
  72. module_param(video_debug, int, 0644);
  73. MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
  74. /* supported video standards */
  75. static struct em28xx_fmt format[] = {
  76. {
  77. .name = "16 bpp YUY2, 4:2:2, packed",
  78. .fourcc = V4L2_PIX_FMT_YUYV,
  79. .depth = 16,
  80. .reg = EM28XX_OUTFMT_YUV422_Y0UY1V,
  81. }, {
  82. .name = "16 bpp RGB 565, LE",
  83. .fourcc = V4L2_PIX_FMT_RGB565,
  84. .depth = 16,
  85. .reg = EM28XX_OUTFMT_RGB_16_656,
  86. }, {
  87. .name = "8 bpp Bayer BGBG..GRGR",
  88. .fourcc = V4L2_PIX_FMT_SBGGR8,
  89. .depth = 8,
  90. .reg = EM28XX_OUTFMT_RGB_8_BGBG,
  91. }, {
  92. .name = "8 bpp Bayer GRGR..BGBG",
  93. .fourcc = V4L2_PIX_FMT_SGRBG8,
  94. .depth = 8,
  95. .reg = EM28XX_OUTFMT_RGB_8_GRGR,
  96. }, {
  97. .name = "8 bpp Bayer GBGB..RGRG",
  98. .fourcc = V4L2_PIX_FMT_SGBRG8,
  99. .depth = 8,
  100. .reg = EM28XX_OUTFMT_RGB_8_GBGB,
  101. }, {
  102. .name = "12 bpp YUV411",
  103. .fourcc = V4L2_PIX_FMT_YUV411P,
  104. .depth = 12,
  105. .reg = EM28XX_OUTFMT_YUV411,
  106. },
  107. };
  108. /* supported controls */
  109. /* Common to all boards */
  110. static struct v4l2_queryctrl ac97_qctrl[] = {
  111. {
  112. .id = V4L2_CID_AUDIO_VOLUME,
  113. .type = V4L2_CTRL_TYPE_INTEGER,
  114. .name = "Volume",
  115. .minimum = 0x0,
  116. .maximum = 0x1f,
  117. .step = 0x1,
  118. .default_value = 0x1f,
  119. .flags = V4L2_CTRL_FLAG_SLIDER,
  120. }, {
  121. .id = V4L2_CID_AUDIO_MUTE,
  122. .type = V4L2_CTRL_TYPE_BOOLEAN,
  123. .name = "Mute",
  124. .minimum = 0,
  125. .maximum = 1,
  126. .step = 1,
  127. .default_value = 1,
  128. .flags = 0,
  129. }
  130. };
  131. /* ------------------------------------------------------------------
  132. DMA and thread functions
  133. ------------------------------------------------------------------*/
  134. /*
  135. * Announces that a buffer were filled and request the next
  136. */
  137. static inline void buffer_filled(struct em28xx *dev,
  138. struct em28xx_dmaqueue *dma_q,
  139. struct em28xx_buffer *buf)
  140. {
  141. /* Advice that buffer was filled */
  142. em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
  143. buf->vb.state = VIDEOBUF_DONE;
  144. buf->vb.field_count++;
  145. do_gettimeofday(&buf->vb.ts);
  146. dev->isoc_ctl.vid_buf = NULL;
  147. list_del(&buf->vb.queue);
  148. wake_up(&buf->vb.done);
  149. }
  150. static inline void vbi_buffer_filled(struct em28xx *dev,
  151. struct em28xx_dmaqueue *dma_q,
  152. struct em28xx_buffer *buf)
  153. {
  154. /* Advice that buffer was filled */
  155. em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
  156. buf->vb.state = VIDEOBUF_DONE;
  157. buf->vb.field_count++;
  158. do_gettimeofday(&buf->vb.ts);
  159. dev->isoc_ctl.vbi_buf = NULL;
  160. list_del(&buf->vb.queue);
  161. wake_up(&buf->vb.done);
  162. }
  163. /*
  164. * Identify the buffer header type and properly handles
  165. */
  166. static void em28xx_copy_video(struct em28xx *dev,
  167. struct em28xx_dmaqueue *dma_q,
  168. struct em28xx_buffer *buf,
  169. unsigned char *p,
  170. unsigned char *outp, unsigned long len)
  171. {
  172. void *fieldstart, *startwrite, *startread;
  173. int linesdone, currlinedone, offset, lencopy, remain;
  174. int bytesperline = dev->width << 1;
  175. if (dma_q->pos + len > buf->vb.size)
  176. len = buf->vb.size - dma_q->pos;
  177. startread = p;
  178. remain = len;
  179. if (dev->progressive)
  180. fieldstart = outp;
  181. else {
  182. /* Interlaces two half frames */
  183. if (buf->top_field)
  184. fieldstart = outp;
  185. else
  186. fieldstart = outp + bytesperline;
  187. }
  188. linesdone = dma_q->pos / bytesperline;
  189. currlinedone = dma_q->pos % bytesperline;
  190. if (dev->progressive)
  191. offset = linesdone * bytesperline + currlinedone;
  192. else
  193. offset = linesdone * bytesperline * 2 + currlinedone;
  194. startwrite = fieldstart + offset;
  195. lencopy = bytesperline - currlinedone;
  196. lencopy = lencopy > remain ? remain : lencopy;
  197. if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
  198. em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
  199. ((char *)startwrite + lencopy) -
  200. ((char *)outp + buf->vb.size));
  201. remain = (char *)outp + buf->vb.size - (char *)startwrite;
  202. lencopy = remain;
  203. }
  204. if (lencopy <= 0)
  205. return;
  206. memcpy(startwrite, startread, lencopy);
  207. remain -= lencopy;
  208. while (remain > 0) {
  209. startwrite += lencopy + bytesperline;
  210. startread += lencopy;
  211. if (bytesperline > remain)
  212. lencopy = remain;
  213. else
  214. lencopy = bytesperline;
  215. if ((char *)startwrite + lencopy > (char *)outp +
  216. buf->vb.size) {
  217. em28xx_isocdbg("Overflow of %zi bytes past buffer end"
  218. "(2)\n",
  219. ((char *)startwrite + lencopy) -
  220. ((char *)outp + buf->vb.size));
  221. lencopy = remain = (char *)outp + buf->vb.size -
  222. (char *)startwrite;
  223. }
  224. if (lencopy <= 0)
  225. break;
  226. memcpy(startwrite, startread, lencopy);
  227. remain -= lencopy;
  228. }
  229. dma_q->pos += len;
  230. }
  231. static void em28xx_copy_vbi(struct em28xx *dev,
  232. struct em28xx_dmaqueue *dma_q,
  233. struct em28xx_buffer *buf,
  234. unsigned char *p,
  235. unsigned char *outp, unsigned long len)
  236. {
  237. void *startwrite, *startread;
  238. int offset;
  239. int bytesperline = dev->vbi_width;
  240. if (dev == NULL) {
  241. em28xx_isocdbg("dev is null\n");
  242. return;
  243. }
  244. if (dma_q == NULL) {
  245. em28xx_isocdbg("dma_q is null\n");
  246. return;
  247. }
  248. if (buf == NULL) {
  249. return;
  250. }
  251. if (p == NULL) {
  252. em28xx_isocdbg("p is null\n");
  253. return;
  254. }
  255. if (outp == NULL) {
  256. em28xx_isocdbg("outp is null\n");
  257. return;
  258. }
  259. if (dma_q->pos + len > buf->vb.size)
  260. len = buf->vb.size - dma_q->pos;
  261. startread = p;
  262. startwrite = outp + dma_q->pos;
  263. offset = dma_q->pos;
  264. /* Make sure the bottom field populates the second half of the frame */
  265. if (buf->top_field == 0) {
  266. startwrite += bytesperline * dev->vbi_height;
  267. offset += bytesperline * dev->vbi_height;
  268. }
  269. memcpy(startwrite, startread, len);
  270. dma_q->pos += len;
  271. }
  272. static inline void print_err_status(struct em28xx *dev,
  273. int packet, int status)
  274. {
  275. char *errmsg = "Unknown";
  276. switch (status) {
  277. case -ENOENT:
  278. errmsg = "unlinked synchronuously";
  279. break;
  280. case -ECONNRESET:
  281. errmsg = "unlinked asynchronuously";
  282. break;
  283. case -ENOSR:
  284. errmsg = "Buffer error (overrun)";
  285. break;
  286. case -EPIPE:
  287. errmsg = "Stalled (device not responding)";
  288. break;
  289. case -EOVERFLOW:
  290. errmsg = "Babble (bad cable?)";
  291. break;
  292. case -EPROTO:
  293. errmsg = "Bit-stuff error (bad cable?)";
  294. break;
  295. case -EILSEQ:
  296. errmsg = "CRC/Timeout (could be anything)";
  297. break;
  298. case -ETIME:
  299. errmsg = "Device does not respond";
  300. break;
  301. }
  302. if (packet < 0) {
  303. em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
  304. } else {
  305. em28xx_isocdbg("URB packet %d, status %d [%s].\n",
  306. packet, status, errmsg);
  307. }
  308. }
  309. /*
  310. * video-buf generic routine to get the next available buffer
  311. */
  312. static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
  313. struct em28xx_buffer **buf)
  314. {
  315. struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
  316. char *outp;
  317. if (list_empty(&dma_q->active)) {
  318. em28xx_isocdbg("No active queue to serve\n");
  319. dev->isoc_ctl.vid_buf = NULL;
  320. *buf = NULL;
  321. return;
  322. }
  323. /* Get the next buffer */
  324. *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
  325. /* Cleans up buffer - Usefull for testing for frame/URB loss */
  326. outp = videobuf_to_vmalloc(&(*buf)->vb);
  327. memset(outp, 0, (*buf)->vb.size);
  328. dev->isoc_ctl.vid_buf = *buf;
  329. return;
  330. }
  331. /*
  332. * video-buf generic routine to get the next available VBI buffer
  333. */
  334. static inline void vbi_get_next_buf(struct em28xx_dmaqueue *dma_q,
  335. struct em28xx_buffer **buf)
  336. {
  337. struct em28xx *dev = container_of(dma_q, struct em28xx, vbiq);
  338. char *outp;
  339. if (list_empty(&dma_q->active)) {
  340. em28xx_isocdbg("No active queue to serve\n");
  341. dev->isoc_ctl.vbi_buf = NULL;
  342. *buf = NULL;
  343. return;
  344. }
  345. /* Get the next buffer */
  346. *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
  347. /* Cleans up buffer - Usefull for testing for frame/URB loss */
  348. outp = videobuf_to_vmalloc(&(*buf)->vb);
  349. memset(outp, 0x00, (*buf)->vb.size);
  350. dev->isoc_ctl.vbi_buf = *buf;
  351. return;
  352. }
  353. /*
  354. * Controls the isoc copy of each urb packet
  355. */
  356. static inline int em28xx_isoc_copy(struct em28xx *dev, struct urb *urb)
  357. {
  358. struct em28xx_buffer *buf;
  359. struct em28xx_dmaqueue *dma_q = &dev->vidq;
  360. unsigned char *outp = NULL;
  361. int i, len = 0, rc = 1;
  362. unsigned char *p;
  363. if (!dev)
  364. return 0;
  365. if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
  366. return 0;
  367. if (urb->status < 0) {
  368. print_err_status(dev, -1, urb->status);
  369. if (urb->status == -ENOENT)
  370. return 0;
  371. }
  372. buf = dev->isoc_ctl.vid_buf;
  373. if (buf != NULL)
  374. outp = videobuf_to_vmalloc(&buf->vb);
  375. for (i = 0; i < urb->number_of_packets; i++) {
  376. int status = urb->iso_frame_desc[i].status;
  377. if (status < 0) {
  378. print_err_status(dev, i, status);
  379. if (urb->iso_frame_desc[i].status != -EPROTO)
  380. continue;
  381. }
  382. len = urb->iso_frame_desc[i].actual_length - 4;
  383. if (urb->iso_frame_desc[i].actual_length <= 0) {
  384. /* em28xx_isocdbg("packet %d is empty",i); - spammy */
  385. continue;
  386. }
  387. if (urb->iso_frame_desc[i].actual_length >
  388. dev->max_pkt_size) {
  389. em28xx_isocdbg("packet bigger than packet size");
  390. continue;
  391. }
  392. p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  393. /* FIXME: incomplete buffer checks where removed to make
  394. logic simpler. Impacts of those changes should be evaluated
  395. */
  396. if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
  397. em28xx_isocdbg("VBI HEADER!!!\n");
  398. /* FIXME: Should add vbi copy */
  399. continue;
  400. }
  401. if (p[0] == 0x22 && p[1] == 0x5a) {
  402. em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
  403. len, (p[2] & 1) ? "odd" : "even");
  404. if (dev->progressive || !(p[2] & 1)) {
  405. if (buf != NULL)
  406. buffer_filled(dev, dma_q, buf);
  407. get_next_buf(dma_q, &buf);
  408. if (buf == NULL)
  409. outp = NULL;
  410. else
  411. outp = videobuf_to_vmalloc(&buf->vb);
  412. }
  413. if (buf != NULL) {
  414. if (p[2] & 1)
  415. buf->top_field = 0;
  416. else
  417. buf->top_field = 1;
  418. }
  419. dma_q->pos = 0;
  420. }
  421. if (buf != NULL) {
  422. if (p[0] != 0x88 && p[0] != 0x22) {
  423. em28xx_isocdbg("frame is not complete\n");
  424. len += 4;
  425. } else {
  426. p += 4;
  427. }
  428. em28xx_copy_video(dev, dma_q, buf, p, outp, len);
  429. }
  430. }
  431. return rc;
  432. }
  433. /* Version of isoc handler that takes into account a mixture of video and
  434. VBI data */
  435. static inline int em28xx_isoc_copy_vbi(struct em28xx *dev, struct urb *urb)
  436. {
  437. struct em28xx_buffer *buf, *vbi_buf;
  438. struct em28xx_dmaqueue *dma_q = &dev->vidq;
  439. struct em28xx_dmaqueue *vbi_dma_q = &dev->vbiq;
  440. unsigned char *outp = NULL;
  441. unsigned char *vbioutp = NULL;
  442. int i, len = 0, rc = 1;
  443. unsigned char *p;
  444. int vbi_size;
  445. if (!dev)
  446. return 0;
  447. if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
  448. return 0;
  449. if (urb->status < 0) {
  450. print_err_status(dev, -1, urb->status);
  451. if (urb->status == -ENOENT)
  452. return 0;
  453. }
  454. buf = dev->isoc_ctl.vid_buf;
  455. if (buf != NULL)
  456. outp = videobuf_to_vmalloc(&buf->vb);
  457. vbi_buf = dev->isoc_ctl.vbi_buf;
  458. if (vbi_buf != NULL)
  459. vbioutp = videobuf_to_vmalloc(&vbi_buf->vb);
  460. for (i = 0; i < urb->number_of_packets; i++) {
  461. int status = urb->iso_frame_desc[i].status;
  462. if (status < 0) {
  463. print_err_status(dev, i, status);
  464. if (urb->iso_frame_desc[i].status != -EPROTO)
  465. continue;
  466. }
  467. len = urb->iso_frame_desc[i].actual_length;
  468. if (urb->iso_frame_desc[i].actual_length <= 0) {
  469. /* em28xx_isocdbg("packet %d is empty",i); - spammy */
  470. continue;
  471. }
  472. if (urb->iso_frame_desc[i].actual_length >
  473. dev->max_pkt_size) {
  474. em28xx_isocdbg("packet bigger than packet size");
  475. continue;
  476. }
  477. p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  478. /* capture type 0 = vbi start
  479. capture type 1 = video start
  480. capture type 2 = video in progress */
  481. if (p[0] == 0x33 && p[1] == 0x95) {
  482. dev->capture_type = 0;
  483. dev->vbi_read = 0;
  484. em28xx_isocdbg("VBI START HEADER!!!\n");
  485. dev->cur_field = p[2];
  486. p += 4;
  487. len -= 4;
  488. } else if (p[0] == 0x88 && p[1] == 0x88 &&
  489. p[2] == 0x88 && p[3] == 0x88) {
  490. /* continuation */
  491. p += 4;
  492. len -= 4;
  493. } else if (p[0] == 0x22 && p[1] == 0x5a) {
  494. /* start video */
  495. p += 4;
  496. len -= 4;
  497. }
  498. vbi_size = dev->vbi_width * dev->vbi_height;
  499. if (dev->capture_type == 0) {
  500. if (dev->vbi_read >= vbi_size) {
  501. /* We've already read all the VBI data, so
  502. treat the rest as video */
  503. em28xx_isocdbg("dev->vbi_read > vbi_size\n");
  504. } else if ((dev->vbi_read + len) < vbi_size) {
  505. /* This entire frame is VBI data */
  506. if (dev->vbi_read == 0 &&
  507. (!(dev->cur_field & 1))) {
  508. /* Brand new frame */
  509. if (vbi_buf != NULL)
  510. vbi_buffer_filled(dev,
  511. vbi_dma_q,
  512. vbi_buf);
  513. vbi_get_next_buf(vbi_dma_q, &vbi_buf);
  514. if (vbi_buf == NULL)
  515. vbioutp = NULL;
  516. else
  517. vbioutp = videobuf_to_vmalloc(
  518. &vbi_buf->vb);
  519. }
  520. if (dev->vbi_read == 0) {
  521. vbi_dma_q->pos = 0;
  522. if (vbi_buf != NULL) {
  523. if (dev->cur_field & 1)
  524. vbi_buf->top_field = 0;
  525. else
  526. vbi_buf->top_field = 1;
  527. }
  528. }
  529. dev->vbi_read += len;
  530. em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
  531. vbioutp, len);
  532. } else {
  533. /* Some of this frame is VBI data and some is
  534. video data */
  535. int vbi_data_len = vbi_size - dev->vbi_read;
  536. dev->vbi_read += vbi_data_len;
  537. em28xx_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
  538. vbioutp, vbi_data_len);
  539. dev->capture_type = 1;
  540. p += vbi_data_len;
  541. len -= vbi_data_len;
  542. }
  543. }
  544. if (dev->capture_type == 1) {
  545. dev->capture_type = 2;
  546. if (dev->progressive || !(dev->cur_field & 1)) {
  547. if (buf != NULL)
  548. buffer_filled(dev, dma_q, buf);
  549. get_next_buf(dma_q, &buf);
  550. if (buf == NULL)
  551. outp = NULL;
  552. else
  553. outp = videobuf_to_vmalloc(&buf->vb);
  554. }
  555. if (buf != NULL) {
  556. if (dev->cur_field & 1)
  557. buf->top_field = 0;
  558. else
  559. buf->top_field = 1;
  560. }
  561. dma_q->pos = 0;
  562. }
  563. if (buf != NULL && dev->capture_type == 2) {
  564. if (len > 4 && p[0] == 0x88 && p[1] == 0x88 &&
  565. p[2] == 0x88 && p[3] == 0x88) {
  566. p += 4;
  567. len -= 4;
  568. }
  569. if (len > 4 && p[0] == 0x22 && p[1] == 0x5a) {
  570. em28xx_isocdbg("Video frame %d, len=%i, %s\n",
  571. p[2], len, (p[2] & 1) ?
  572. "odd" : "even");
  573. p += 4;
  574. len -= 4;
  575. }
  576. if (len > 0)
  577. em28xx_copy_video(dev, dma_q, buf, p, outp,
  578. len);
  579. }
  580. }
  581. return rc;
  582. }
  583. /* ------------------------------------------------------------------
  584. Videobuf operations
  585. ------------------------------------------------------------------*/
  586. static int
  587. buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
  588. {
  589. struct em28xx_fh *fh = vq->priv_data;
  590. struct em28xx *dev = fh->dev;
  591. struct v4l2_frequency f;
  592. *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)
  593. >> 3;
  594. if (0 == *count)
  595. *count = EM28XX_DEF_BUF;
  596. if (*count < EM28XX_MIN_BUF)
  597. *count = EM28XX_MIN_BUF;
  598. /* Ask tuner to go to analog or radio mode */
  599. memset(&f, 0, sizeof(f));
  600. f.frequency = dev->ctl_freq;
  601. f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  602. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
  603. return 0;
  604. }
  605. /* This is called *without* dev->slock held; please keep it that way */
  606. static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
  607. {
  608. struct em28xx_fh *fh = vq->priv_data;
  609. struct em28xx *dev = fh->dev;
  610. unsigned long flags = 0;
  611. if (in_interrupt())
  612. BUG();
  613. /* We used to wait for the buffer to finish here, but this didn't work
  614. because, as we were keeping the state as VIDEOBUF_QUEUED,
  615. videobuf_queue_cancel marked it as finished for us.
  616. (Also, it could wedge forever if the hardware was misconfigured.)
  617. This should be safe; by the time we get here, the buffer isn't
  618. queued anymore. If we ever start marking the buffers as
  619. VIDEOBUF_ACTIVE, it won't be, though.
  620. */
  621. spin_lock_irqsave(&dev->slock, flags);
  622. if (dev->isoc_ctl.vid_buf == buf)
  623. dev->isoc_ctl.vid_buf = NULL;
  624. spin_unlock_irqrestore(&dev->slock, flags);
  625. videobuf_vmalloc_free(&buf->vb);
  626. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  627. }
  628. static int
  629. buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
  630. enum v4l2_field field)
  631. {
  632. struct em28xx_fh *fh = vq->priv_data;
  633. struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
  634. struct em28xx *dev = fh->dev;
  635. int rc = 0, urb_init = 0;
  636. buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
  637. + 7) >> 3;
  638. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  639. return -EINVAL;
  640. buf->vb.width = dev->width;
  641. buf->vb.height = dev->height;
  642. buf->vb.field = field;
  643. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  644. rc = videobuf_iolock(vq, &buf->vb, NULL);
  645. if (rc < 0)
  646. goto fail;
  647. }
  648. if (!dev->isoc_ctl.num_bufs)
  649. urb_init = 1;
  650. if (urb_init) {
  651. if (em28xx_vbi_supported(dev) == 1)
  652. rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
  653. EM28XX_NUM_BUFS,
  654. dev->max_pkt_size,
  655. em28xx_isoc_copy_vbi);
  656. else
  657. rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
  658. EM28XX_NUM_BUFS,
  659. dev->max_pkt_size,
  660. em28xx_isoc_copy);
  661. if (rc < 0)
  662. goto fail;
  663. }
  664. buf->vb.state = VIDEOBUF_PREPARED;
  665. return 0;
  666. fail:
  667. free_buffer(vq, buf);
  668. return rc;
  669. }
  670. static void
  671. buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  672. {
  673. struct em28xx_buffer *buf = container_of(vb,
  674. struct em28xx_buffer,
  675. vb);
  676. struct em28xx_fh *fh = vq->priv_data;
  677. struct em28xx *dev = fh->dev;
  678. struct em28xx_dmaqueue *vidq = &dev->vidq;
  679. buf->vb.state = VIDEOBUF_QUEUED;
  680. list_add_tail(&buf->vb.queue, &vidq->active);
  681. }
  682. static void buffer_release(struct videobuf_queue *vq,
  683. struct videobuf_buffer *vb)
  684. {
  685. struct em28xx_buffer *buf = container_of(vb,
  686. struct em28xx_buffer,
  687. vb);
  688. struct em28xx_fh *fh = vq->priv_data;
  689. struct em28xx *dev = (struct em28xx *)fh->dev;
  690. em28xx_isocdbg("em28xx: called buffer_release\n");
  691. free_buffer(vq, buf);
  692. }
  693. static struct videobuf_queue_ops em28xx_video_qops = {
  694. .buf_setup = buffer_setup,
  695. .buf_prepare = buffer_prepare,
  696. .buf_queue = buffer_queue,
  697. .buf_release = buffer_release,
  698. };
  699. /********************* v4l2 interface **************************************/
  700. static void video_mux(struct em28xx *dev, int index)
  701. {
  702. dev->ctl_input = index;
  703. dev->ctl_ainput = INPUT(index)->amux;
  704. dev->ctl_aoutput = INPUT(index)->aout;
  705. if (!dev->ctl_aoutput)
  706. dev->ctl_aoutput = EM28XX_AOUT_MASTER;
  707. v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
  708. INPUT(index)->vmux, 0, 0);
  709. if (dev->board.has_msp34xx) {
  710. if (dev->i2s_speed) {
  711. v4l2_device_call_all(&dev->v4l2_dev, 0, audio,
  712. s_i2s_clock_freq, dev->i2s_speed);
  713. }
  714. /* Note: this is msp3400 specific */
  715. v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
  716. dev->ctl_ainput, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
  717. }
  718. if (dev->board.adecoder != EM28XX_NOADECODER) {
  719. v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
  720. dev->ctl_ainput, dev->ctl_aoutput, 0);
  721. }
  722. em28xx_audio_analog_set(dev);
  723. }
  724. /* Usage lock check functions */
  725. static int res_get(struct em28xx_fh *fh, unsigned int bit)
  726. {
  727. struct em28xx *dev = fh->dev;
  728. if (fh->resources & bit)
  729. /* have it already allocated */
  730. return 1;
  731. /* is it free? */
  732. mutex_lock(&dev->lock);
  733. if (dev->resources & bit) {
  734. /* no, someone else uses it */
  735. mutex_unlock(&dev->lock);
  736. return 0;
  737. }
  738. /* it's free, grab it */
  739. fh->resources |= bit;
  740. dev->resources |= bit;
  741. em28xx_videodbg("res: get %d\n", bit);
  742. mutex_unlock(&dev->lock);
  743. return 1;
  744. }
  745. static int res_check(struct em28xx_fh *fh, unsigned int bit)
  746. {
  747. return fh->resources & bit;
  748. }
  749. static int res_locked(struct em28xx *dev, unsigned int bit)
  750. {
  751. return dev->resources & bit;
  752. }
  753. static void res_free(struct em28xx_fh *fh, unsigned int bits)
  754. {
  755. struct em28xx *dev = fh->dev;
  756. BUG_ON((fh->resources & bits) != bits);
  757. mutex_lock(&dev->lock);
  758. fh->resources &= ~bits;
  759. dev->resources &= ~bits;
  760. em28xx_videodbg("res: put %d\n", bits);
  761. mutex_unlock(&dev->lock);
  762. }
  763. static int get_ressource(struct em28xx_fh *fh)
  764. {
  765. switch (fh->type) {
  766. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  767. return EM28XX_RESOURCE_VIDEO;
  768. case V4L2_BUF_TYPE_VBI_CAPTURE:
  769. return EM28XX_RESOURCE_VBI;
  770. default:
  771. BUG();
  772. return 0;
  773. }
  774. }
  775. /*
  776. * ac97_queryctrl()
  777. * return the ac97 supported controls
  778. */
  779. static int ac97_queryctrl(struct v4l2_queryctrl *qc)
  780. {
  781. int i;
  782. for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
  783. if (qc->id && qc->id == ac97_qctrl[i].id) {
  784. memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
  785. return 0;
  786. }
  787. }
  788. /* Control is not ac97 related */
  789. return 1;
  790. }
  791. /*
  792. * ac97_get_ctrl()
  793. * return the current values for ac97 mute and volume
  794. */
  795. static int ac97_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
  796. {
  797. switch (ctrl->id) {
  798. case V4L2_CID_AUDIO_MUTE:
  799. ctrl->value = dev->mute;
  800. return 0;
  801. case V4L2_CID_AUDIO_VOLUME:
  802. ctrl->value = dev->volume;
  803. return 0;
  804. default:
  805. /* Control is not ac97 related */
  806. return 1;
  807. }
  808. }
  809. /*
  810. * ac97_set_ctrl()
  811. * set values for ac97 mute and volume
  812. */
  813. static int ac97_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
  814. {
  815. int i;
  816. for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++)
  817. if (ctrl->id == ac97_qctrl[i].id)
  818. goto handle;
  819. /* Announce that hasn't handle it */
  820. return 1;
  821. handle:
  822. if (ctrl->value < ac97_qctrl[i].minimum ||
  823. ctrl->value > ac97_qctrl[i].maximum)
  824. return -ERANGE;
  825. switch (ctrl->id) {
  826. case V4L2_CID_AUDIO_MUTE:
  827. dev->mute = ctrl->value;
  828. break;
  829. case V4L2_CID_AUDIO_VOLUME:
  830. dev->volume = ctrl->value;
  831. break;
  832. }
  833. return em28xx_audio_analog_set(dev);
  834. }
  835. static int check_dev(struct em28xx *dev)
  836. {
  837. if (dev->state & DEV_DISCONNECTED) {
  838. em28xx_errdev("v4l2 ioctl: device not present\n");
  839. return -ENODEV;
  840. }
  841. if (dev->state & DEV_MISCONFIGURED) {
  842. em28xx_errdev("v4l2 ioctl: device is misconfigured; "
  843. "close and open it again\n");
  844. return -EIO;
  845. }
  846. return 0;
  847. }
  848. static void get_scale(struct em28xx *dev,
  849. unsigned int width, unsigned int height,
  850. unsigned int *hscale, unsigned int *vscale)
  851. {
  852. unsigned int maxw = norm_maxw(dev);
  853. unsigned int maxh = norm_maxh(dev);
  854. *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
  855. if (*hscale >= 0x4000)
  856. *hscale = 0x3fff;
  857. *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
  858. if (*vscale >= 0x4000)
  859. *vscale = 0x3fff;
  860. }
  861. /* ------------------------------------------------------------------
  862. IOCTL vidioc handling
  863. ------------------------------------------------------------------*/
  864. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  865. struct v4l2_format *f)
  866. {
  867. struct em28xx_fh *fh = priv;
  868. struct em28xx *dev = fh->dev;
  869. mutex_lock(&dev->lock);
  870. f->fmt.pix.width = dev->width;
  871. f->fmt.pix.height = dev->height;
  872. f->fmt.pix.pixelformat = dev->format->fourcc;
  873. f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
  874. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
  875. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  876. /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
  877. if (dev->progressive)
  878. f->fmt.pix.field = V4L2_FIELD_NONE;
  879. else
  880. f->fmt.pix.field = dev->interlaced ?
  881. V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
  882. mutex_unlock(&dev->lock);
  883. return 0;
  884. }
  885. static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
  886. {
  887. unsigned int i;
  888. for (i = 0; i < ARRAY_SIZE(format); i++)
  889. if (format[i].fourcc == fourcc)
  890. return &format[i];
  891. return NULL;
  892. }
  893. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  894. struct v4l2_format *f)
  895. {
  896. struct em28xx_fh *fh = priv;
  897. struct em28xx *dev = fh->dev;
  898. unsigned int width = f->fmt.pix.width;
  899. unsigned int height = f->fmt.pix.height;
  900. unsigned int maxw = norm_maxw(dev);
  901. unsigned int maxh = norm_maxh(dev);
  902. unsigned int hscale, vscale;
  903. struct em28xx_fmt *fmt;
  904. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  905. if (!fmt) {
  906. em28xx_videodbg("Fourcc format (%08x) invalid.\n",
  907. f->fmt.pix.pixelformat);
  908. return -EINVAL;
  909. }
  910. if (dev->board.is_em2800) {
  911. /* the em2800 can only scale down to 50% */
  912. height = height > (3 * maxh / 4) ? maxh : maxh / 2;
  913. width = width > (3 * maxw / 4) ? maxw : maxw / 2;
  914. } else {
  915. /* width must even because of the YUYV format
  916. height must be even because of interlacing */
  917. v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh,
  918. 1, 0);
  919. }
  920. get_scale(dev, width, height, &hscale, &vscale);
  921. width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
  922. height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
  923. f->fmt.pix.width = width;
  924. f->fmt.pix.height = height;
  925. f->fmt.pix.pixelformat = fmt->fourcc;
  926. f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
  927. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
  928. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  929. if (dev->progressive)
  930. f->fmt.pix.field = V4L2_FIELD_NONE;
  931. else
  932. f->fmt.pix.field = dev->interlaced ?
  933. V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
  934. return 0;
  935. }
  936. static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
  937. unsigned width, unsigned height)
  938. {
  939. struct em28xx_fmt *fmt;
  940. fmt = format_by_fourcc(fourcc);
  941. if (!fmt)
  942. return -EINVAL;
  943. dev->format = fmt;
  944. dev->width = width;
  945. dev->height = height;
  946. /* set new image size */
  947. get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
  948. em28xx_set_alternate(dev);
  949. em28xx_resolution_set(dev);
  950. return 0;
  951. }
  952. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  953. struct v4l2_format *f)
  954. {
  955. struct em28xx_fh *fh = priv;
  956. struct em28xx *dev = fh->dev;
  957. int rc;
  958. rc = check_dev(dev);
  959. if (rc < 0)
  960. return rc;
  961. mutex_lock(&dev->lock);
  962. vidioc_try_fmt_vid_cap(file, priv, f);
  963. if (videobuf_queue_is_busy(&fh->vb_vidq)) {
  964. em28xx_errdev("%s queue busy\n", __func__);
  965. rc = -EBUSY;
  966. goto out;
  967. }
  968. rc = em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
  969. f->fmt.pix.width, f->fmt.pix.height);
  970. out:
  971. mutex_unlock(&dev->lock);
  972. return rc;
  973. }
  974. static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
  975. {
  976. struct em28xx_fh *fh = priv;
  977. struct em28xx *dev = fh->dev;
  978. int rc;
  979. rc = check_dev(dev);
  980. if (rc < 0)
  981. return rc;
  982. *norm = dev->norm;
  983. return 0;
  984. }
  985. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
  986. {
  987. struct em28xx_fh *fh = priv;
  988. struct em28xx *dev = fh->dev;
  989. struct v4l2_format f;
  990. int rc;
  991. rc = check_dev(dev);
  992. if (rc < 0)
  993. return rc;
  994. mutex_lock(&dev->lock);
  995. dev->norm = *norm;
  996. /* Adjusts width/height, if needed */
  997. f.fmt.pix.width = dev->width;
  998. f.fmt.pix.height = dev->height;
  999. vidioc_try_fmt_vid_cap(file, priv, &f);
  1000. /* set new image size */
  1001. dev->width = f.fmt.pix.width;
  1002. dev->height = f.fmt.pix.height;
  1003. get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
  1004. em28xx_resolution_set(dev);
  1005. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
  1006. mutex_unlock(&dev->lock);
  1007. return 0;
  1008. }
  1009. static int vidioc_g_parm(struct file *file, void *priv,
  1010. struct v4l2_streamparm *p)
  1011. {
  1012. struct em28xx_fh *fh = priv;
  1013. struct em28xx *dev = fh->dev;
  1014. int rc = 0;
  1015. if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1016. return -EINVAL;
  1017. if (dev->board.is_webcam)
  1018. rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0,
  1019. video, g_parm, p);
  1020. else
  1021. v4l2_video_std_frame_period(dev->norm,
  1022. &p->parm.capture.timeperframe);
  1023. return rc;
  1024. }
  1025. static int vidioc_s_parm(struct file *file, void *priv,
  1026. struct v4l2_streamparm *p)
  1027. {
  1028. struct em28xx_fh *fh = priv;
  1029. struct em28xx *dev = fh->dev;
  1030. if (!dev->board.is_webcam)
  1031. return -EINVAL;
  1032. if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1033. return -EINVAL;
  1034. return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p);
  1035. }
  1036. static const char *iname[] = {
  1037. [EM28XX_VMUX_COMPOSITE1] = "Composite1",
  1038. [EM28XX_VMUX_COMPOSITE2] = "Composite2",
  1039. [EM28XX_VMUX_COMPOSITE3] = "Composite3",
  1040. [EM28XX_VMUX_COMPOSITE4] = "Composite4",
  1041. [EM28XX_VMUX_SVIDEO] = "S-Video",
  1042. [EM28XX_VMUX_TELEVISION] = "Television",
  1043. [EM28XX_VMUX_CABLE] = "Cable TV",
  1044. [EM28XX_VMUX_DVB] = "DVB",
  1045. [EM28XX_VMUX_DEBUG] = "for debug only",
  1046. };
  1047. static int vidioc_enum_input(struct file *file, void *priv,
  1048. struct v4l2_input *i)
  1049. {
  1050. struct em28xx_fh *fh = priv;
  1051. struct em28xx *dev = fh->dev;
  1052. unsigned int n;
  1053. n = i->index;
  1054. if (n >= MAX_EM28XX_INPUT)
  1055. return -EINVAL;
  1056. if (0 == INPUT(n)->type)
  1057. return -EINVAL;
  1058. i->index = n;
  1059. i->type = V4L2_INPUT_TYPE_CAMERA;
  1060. strcpy(i->name, iname[INPUT(n)->type]);
  1061. if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
  1062. (EM28XX_VMUX_CABLE == INPUT(n)->type))
  1063. i->type = V4L2_INPUT_TYPE_TUNER;
  1064. i->std = dev->vdev->tvnorms;
  1065. return 0;
  1066. }
  1067. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  1068. {
  1069. struct em28xx_fh *fh = priv;
  1070. struct em28xx *dev = fh->dev;
  1071. *i = dev->ctl_input;
  1072. return 0;
  1073. }
  1074. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  1075. {
  1076. struct em28xx_fh *fh = priv;
  1077. struct em28xx *dev = fh->dev;
  1078. int rc;
  1079. rc = check_dev(dev);
  1080. if (rc < 0)
  1081. return rc;
  1082. if (i >= MAX_EM28XX_INPUT)
  1083. return -EINVAL;
  1084. if (0 == INPUT(i)->type)
  1085. return -EINVAL;
  1086. dev->ctl_input = i;
  1087. mutex_lock(&dev->lock);
  1088. video_mux(dev, dev->ctl_input);
  1089. mutex_unlock(&dev->lock);
  1090. return 0;
  1091. }
  1092. static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  1093. {
  1094. struct em28xx_fh *fh = priv;
  1095. struct em28xx *dev = fh->dev;
  1096. if (!dev->audio_mode.has_audio)
  1097. return -EINVAL;
  1098. switch (a->index) {
  1099. case EM28XX_AMUX_VIDEO:
  1100. strcpy(a->name, "Television");
  1101. break;
  1102. case EM28XX_AMUX_LINE_IN:
  1103. strcpy(a->name, "Line In");
  1104. break;
  1105. case EM28XX_AMUX_VIDEO2:
  1106. strcpy(a->name, "Television alt");
  1107. break;
  1108. case EM28XX_AMUX_PHONE:
  1109. strcpy(a->name, "Phone");
  1110. break;
  1111. case EM28XX_AMUX_MIC:
  1112. strcpy(a->name, "Mic");
  1113. break;
  1114. case EM28XX_AMUX_CD:
  1115. strcpy(a->name, "CD");
  1116. break;
  1117. case EM28XX_AMUX_AUX:
  1118. strcpy(a->name, "Aux");
  1119. break;
  1120. case EM28XX_AMUX_PCM_OUT:
  1121. strcpy(a->name, "PCM");
  1122. break;
  1123. default:
  1124. return -EINVAL;
  1125. }
  1126. a->index = dev->ctl_ainput;
  1127. a->capability = V4L2_AUDCAP_STEREO;
  1128. return 0;
  1129. }
  1130. static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
  1131. {
  1132. struct em28xx_fh *fh = priv;
  1133. struct em28xx *dev = fh->dev;
  1134. if (!dev->audio_mode.has_audio)
  1135. return -EINVAL;
  1136. if (a->index >= MAX_EM28XX_INPUT)
  1137. return -EINVAL;
  1138. if (0 == INPUT(a->index)->type)
  1139. return -EINVAL;
  1140. mutex_lock(&dev->lock);
  1141. dev->ctl_ainput = INPUT(a->index)->amux;
  1142. dev->ctl_aoutput = INPUT(a->index)->aout;
  1143. if (!dev->ctl_aoutput)
  1144. dev->ctl_aoutput = EM28XX_AOUT_MASTER;
  1145. mutex_unlock(&dev->lock);
  1146. return 0;
  1147. }
  1148. static int vidioc_queryctrl(struct file *file, void *priv,
  1149. struct v4l2_queryctrl *qc)
  1150. {
  1151. struct em28xx_fh *fh = priv;
  1152. struct em28xx *dev = fh->dev;
  1153. int id = qc->id;
  1154. int rc;
  1155. rc = check_dev(dev);
  1156. if (rc < 0)
  1157. return rc;
  1158. memset(qc, 0, sizeof(*qc));
  1159. qc->id = id;
  1160. /* enumberate AC97 controls */
  1161. if (dev->audio_mode.ac97 != EM28XX_NO_AC97) {
  1162. rc = ac97_queryctrl(qc);
  1163. if (!rc)
  1164. return 0;
  1165. }
  1166. /* enumberate V4L2 device controls */
  1167. mutex_lock(&dev->lock);
  1168. v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
  1169. mutex_unlock(&dev->lock);
  1170. if (qc->type)
  1171. return 0;
  1172. else
  1173. return -EINVAL;
  1174. }
  1175. static int vidioc_g_ctrl(struct file *file, void *priv,
  1176. struct v4l2_control *ctrl)
  1177. {
  1178. struct em28xx_fh *fh = priv;
  1179. struct em28xx *dev = fh->dev;
  1180. int rc;
  1181. rc = check_dev(dev);
  1182. if (rc < 0)
  1183. return rc;
  1184. rc = 0;
  1185. mutex_lock(&dev->lock);
  1186. /* Set an AC97 control */
  1187. if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
  1188. rc = ac97_get_ctrl(dev, ctrl);
  1189. else
  1190. rc = 1;
  1191. /* It were not an AC97 control. Sends it to the v4l2 dev interface */
  1192. if (rc == 1) {
  1193. v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
  1194. rc = 0;
  1195. }
  1196. mutex_unlock(&dev->lock);
  1197. return rc;
  1198. }
  1199. static int vidioc_s_ctrl(struct file *file, void *priv,
  1200. struct v4l2_control *ctrl)
  1201. {
  1202. struct em28xx_fh *fh = priv;
  1203. struct em28xx *dev = fh->dev;
  1204. int rc;
  1205. rc = check_dev(dev);
  1206. if (rc < 0)
  1207. return rc;
  1208. mutex_lock(&dev->lock);
  1209. /* Set an AC97 control */
  1210. if (dev->audio_mode.ac97 != EM28XX_NO_AC97)
  1211. rc = ac97_set_ctrl(dev, ctrl);
  1212. else
  1213. rc = 1;
  1214. /* It isn't an AC97 control. Sends it to the v4l2 dev interface */
  1215. if (rc == 1) {
  1216. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_ctrl, ctrl);
  1217. /*
  1218. * In the case of non-AC97 volume controls, we still need
  1219. * to do some setups at em28xx, in order to mute/unmute
  1220. * and to adjust audio volume. However, the value ranges
  1221. * should be checked by the corresponding V4L subdriver.
  1222. */
  1223. switch (ctrl->id) {
  1224. case V4L2_CID_AUDIO_MUTE:
  1225. dev->mute = ctrl->value;
  1226. rc = em28xx_audio_analog_set(dev);
  1227. break;
  1228. case V4L2_CID_AUDIO_VOLUME:
  1229. dev->volume = ctrl->value;
  1230. rc = em28xx_audio_analog_set(dev);
  1231. }
  1232. }
  1233. mutex_unlock(&dev->lock);
  1234. return rc;
  1235. }
  1236. static int vidioc_g_tuner(struct file *file, void *priv,
  1237. struct v4l2_tuner *t)
  1238. {
  1239. struct em28xx_fh *fh = priv;
  1240. struct em28xx *dev = fh->dev;
  1241. int rc;
  1242. rc = check_dev(dev);
  1243. if (rc < 0)
  1244. return rc;
  1245. if (0 != t->index)
  1246. return -EINVAL;
  1247. strcpy(t->name, "Tuner");
  1248. mutex_lock(&dev->lock);
  1249. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
  1250. mutex_unlock(&dev->lock);
  1251. return 0;
  1252. }
  1253. static int vidioc_s_tuner(struct file *file, void *priv,
  1254. struct v4l2_tuner *t)
  1255. {
  1256. struct em28xx_fh *fh = priv;
  1257. struct em28xx *dev = fh->dev;
  1258. int rc;
  1259. rc = check_dev(dev);
  1260. if (rc < 0)
  1261. return rc;
  1262. if (0 != t->index)
  1263. return -EINVAL;
  1264. mutex_lock(&dev->lock);
  1265. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
  1266. mutex_unlock(&dev->lock);
  1267. return 0;
  1268. }
  1269. static int vidioc_g_frequency(struct file *file, void *priv,
  1270. struct v4l2_frequency *f)
  1271. {
  1272. struct em28xx_fh *fh = priv;
  1273. struct em28xx *dev = fh->dev;
  1274. mutex_lock(&dev->lock);
  1275. f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1276. f->frequency = dev->ctl_freq;
  1277. mutex_unlock(&dev->lock);
  1278. return 0;
  1279. }
  1280. static int vidioc_s_frequency(struct file *file, void *priv,
  1281. struct v4l2_frequency *f)
  1282. {
  1283. struct em28xx_fh *fh = priv;
  1284. struct em28xx *dev = fh->dev;
  1285. int rc;
  1286. rc = check_dev(dev);
  1287. if (rc < 0)
  1288. return rc;
  1289. if (0 != f->tuner)
  1290. return -EINVAL;
  1291. if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
  1292. return -EINVAL;
  1293. if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
  1294. return -EINVAL;
  1295. mutex_lock(&dev->lock);
  1296. dev->ctl_freq = f->frequency;
  1297. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
  1298. mutex_unlock(&dev->lock);
  1299. return 0;
  1300. }
  1301. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1302. static int em28xx_reg_len(int reg)
  1303. {
  1304. switch (reg) {
  1305. case EM28XX_R40_AC97LSB:
  1306. case EM28XX_R30_HSCALELOW:
  1307. case EM28XX_R32_VSCALELOW:
  1308. return 2;
  1309. default:
  1310. return 1;
  1311. }
  1312. }
  1313. static int vidioc_g_chip_ident(struct file *file, void *priv,
  1314. struct v4l2_dbg_chip_ident *chip)
  1315. {
  1316. struct em28xx_fh *fh = priv;
  1317. struct em28xx *dev = fh->dev;
  1318. chip->ident = V4L2_IDENT_NONE;
  1319. chip->revision = 0;
  1320. v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
  1321. return 0;
  1322. }
  1323. static int vidioc_g_register(struct file *file, void *priv,
  1324. struct v4l2_dbg_register *reg)
  1325. {
  1326. struct em28xx_fh *fh = priv;
  1327. struct em28xx *dev = fh->dev;
  1328. int ret;
  1329. switch (reg->match.type) {
  1330. case V4L2_CHIP_MATCH_AC97:
  1331. mutex_lock(&dev->lock);
  1332. ret = em28xx_read_ac97(dev, reg->reg);
  1333. mutex_unlock(&dev->lock);
  1334. if (ret < 0)
  1335. return ret;
  1336. reg->val = ret;
  1337. reg->size = 1;
  1338. return 0;
  1339. case V4L2_CHIP_MATCH_I2C_DRIVER:
  1340. v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
  1341. return 0;
  1342. case V4L2_CHIP_MATCH_I2C_ADDR:
  1343. /* TODO: is this correct? */
  1344. v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
  1345. return 0;
  1346. default:
  1347. if (!v4l2_chip_match_host(&reg->match))
  1348. return -EINVAL;
  1349. }
  1350. /* Match host */
  1351. reg->size = em28xx_reg_len(reg->reg);
  1352. if (reg->size == 1) {
  1353. mutex_lock(&dev->lock);
  1354. ret = em28xx_read_reg(dev, reg->reg);
  1355. mutex_unlock(&dev->lock);
  1356. if (ret < 0)
  1357. return ret;
  1358. reg->val = ret;
  1359. } else {
  1360. __le16 val = 0;
  1361. mutex_lock(&dev->lock);
  1362. ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
  1363. reg->reg, (char *)&val, 2);
  1364. mutex_unlock(&dev->lock);
  1365. if (ret < 0)
  1366. return ret;
  1367. reg->val = le16_to_cpu(val);
  1368. }
  1369. return 0;
  1370. }
  1371. static int vidioc_s_register(struct file *file, void *priv,
  1372. struct v4l2_dbg_register *reg)
  1373. {
  1374. struct em28xx_fh *fh = priv;
  1375. struct em28xx *dev = fh->dev;
  1376. __le16 buf;
  1377. int rc;
  1378. switch (reg->match.type) {
  1379. case V4L2_CHIP_MATCH_AC97:
  1380. mutex_lock(&dev->lock);
  1381. rc = em28xx_write_ac97(dev, reg->reg, reg->val);
  1382. mutex_unlock(&dev->lock);
  1383. return rc;
  1384. case V4L2_CHIP_MATCH_I2C_DRIVER:
  1385. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
  1386. return 0;
  1387. case V4L2_CHIP_MATCH_I2C_ADDR:
  1388. /* TODO: is this correct? */
  1389. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
  1390. return 0;
  1391. default:
  1392. if (!v4l2_chip_match_host(&reg->match))
  1393. return -EINVAL;
  1394. }
  1395. /* Match host */
  1396. buf = cpu_to_le16(reg->val);
  1397. mutex_lock(&dev->lock);
  1398. rc = em28xx_write_regs(dev, reg->reg, (char *)&buf,
  1399. em28xx_reg_len(reg->reg));
  1400. mutex_unlock(&dev->lock);
  1401. return rc;
  1402. }
  1403. #endif
  1404. static int vidioc_cropcap(struct file *file, void *priv,
  1405. struct v4l2_cropcap *cc)
  1406. {
  1407. struct em28xx_fh *fh = priv;
  1408. struct em28xx *dev = fh->dev;
  1409. if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1410. return -EINVAL;
  1411. cc->bounds.left = 0;
  1412. cc->bounds.top = 0;
  1413. cc->bounds.width = dev->width;
  1414. cc->bounds.height = dev->height;
  1415. cc->defrect = cc->bounds;
  1416. cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
  1417. cc->pixelaspect.denominator = 59;
  1418. return 0;
  1419. }
  1420. static int vidioc_streamon(struct file *file, void *priv,
  1421. enum v4l2_buf_type type)
  1422. {
  1423. struct em28xx_fh *fh = priv;
  1424. struct em28xx *dev = fh->dev;
  1425. int rc = -EINVAL;
  1426. rc = check_dev(dev);
  1427. if (rc < 0)
  1428. return rc;
  1429. if (unlikely(type != fh->type))
  1430. return -EINVAL;
  1431. em28xx_videodbg("vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n",
  1432. fh, type, fh->resources, dev->resources);
  1433. if (unlikely(!res_get(fh, get_ressource(fh))))
  1434. return -EBUSY;
  1435. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1436. rc = videobuf_streamon(&fh->vb_vidq);
  1437. else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
  1438. rc = videobuf_streamon(&fh->vb_vbiq);
  1439. return rc;
  1440. }
  1441. static int vidioc_streamoff(struct file *file, void *priv,
  1442. enum v4l2_buf_type type)
  1443. {
  1444. struct em28xx_fh *fh = priv;
  1445. struct em28xx *dev = fh->dev;
  1446. int rc;
  1447. rc = check_dev(dev);
  1448. if (rc < 0)
  1449. return rc;
  1450. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  1451. fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)
  1452. return -EINVAL;
  1453. if (type != fh->type)
  1454. return -EINVAL;
  1455. em28xx_videodbg("vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n",
  1456. fh, type, fh->resources, dev->resources);
  1457. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  1458. videobuf_streamoff(&fh->vb_vidq);
  1459. res_free(fh, EM28XX_RESOURCE_VIDEO);
  1460. } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
  1461. videobuf_streamoff(&fh->vb_vbiq);
  1462. res_free(fh, EM28XX_RESOURCE_VBI);
  1463. }
  1464. return 0;
  1465. }
  1466. static int vidioc_querycap(struct file *file, void *priv,
  1467. struct v4l2_capability *cap)
  1468. {
  1469. struct em28xx_fh *fh = priv;
  1470. struct em28xx *dev = fh->dev;
  1471. strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
  1472. strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
  1473. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  1474. cap->version = EM28XX_VERSION_CODE;
  1475. cap->capabilities =
  1476. V4L2_CAP_SLICED_VBI_CAPTURE |
  1477. V4L2_CAP_VIDEO_CAPTURE |
  1478. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  1479. if (dev->vbi_dev)
  1480. cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
  1481. if (dev->audio_mode.has_audio)
  1482. cap->capabilities |= V4L2_CAP_AUDIO;
  1483. if (dev->tuner_type != TUNER_ABSENT)
  1484. cap->capabilities |= V4L2_CAP_TUNER;
  1485. return 0;
  1486. }
  1487. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  1488. struct v4l2_fmtdesc *f)
  1489. {
  1490. if (unlikely(f->index >= ARRAY_SIZE(format)))
  1491. return -EINVAL;
  1492. strlcpy(f->description, format[f->index].name, sizeof(f->description));
  1493. f->pixelformat = format[f->index].fourcc;
  1494. return 0;
  1495. }
  1496. /* Sliced VBI ioctls */
  1497. static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
  1498. struct v4l2_format *f)
  1499. {
  1500. struct em28xx_fh *fh = priv;
  1501. struct em28xx *dev = fh->dev;
  1502. int rc;
  1503. rc = check_dev(dev);
  1504. if (rc < 0)
  1505. return rc;
  1506. mutex_lock(&dev->lock);
  1507. f->fmt.sliced.service_set = 0;
  1508. v4l2_device_call_all(&dev->v4l2_dev, 0, video, g_fmt, f);
  1509. if (f->fmt.sliced.service_set == 0)
  1510. rc = -EINVAL;
  1511. mutex_unlock(&dev->lock);
  1512. return rc;
  1513. }
  1514. static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
  1515. struct v4l2_format *f)
  1516. {
  1517. struct em28xx_fh *fh = priv;
  1518. struct em28xx *dev = fh->dev;
  1519. int rc;
  1520. rc = check_dev(dev);
  1521. if (rc < 0)
  1522. return rc;
  1523. mutex_lock(&dev->lock);
  1524. v4l2_device_call_all(&dev->v4l2_dev, 0, video, g_fmt, f);
  1525. mutex_unlock(&dev->lock);
  1526. if (f->fmt.sliced.service_set == 0)
  1527. return -EINVAL;
  1528. return 0;
  1529. }
  1530. /* RAW VBI ioctls */
  1531. static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
  1532. struct v4l2_format *format)
  1533. {
  1534. struct em28xx_fh *fh = priv;
  1535. struct em28xx *dev = fh->dev;
  1536. format->fmt.vbi.samples_per_line = dev->vbi_width;
  1537. format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  1538. format->fmt.vbi.offset = 0;
  1539. format->fmt.vbi.flags = 0;
  1540. format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
  1541. format->fmt.vbi.count[0] = dev->vbi_height;
  1542. format->fmt.vbi.count[1] = dev->vbi_height;
  1543. /* Varies by video standard (NTSC, PAL, etc.) */
  1544. if (dev->norm & V4L2_STD_525_60) {
  1545. /* NTSC */
  1546. format->fmt.vbi.start[0] = 10;
  1547. format->fmt.vbi.start[1] = 273;
  1548. } else if (dev->norm & V4L2_STD_625_50) {
  1549. /* PAL */
  1550. format->fmt.vbi.start[0] = 6;
  1551. format->fmt.vbi.start[1] = 318;
  1552. }
  1553. return 0;
  1554. }
  1555. static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
  1556. struct v4l2_format *format)
  1557. {
  1558. struct em28xx_fh *fh = priv;
  1559. struct em28xx *dev = fh->dev;
  1560. format->fmt.vbi.samples_per_line = dev->vbi_width;
  1561. format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  1562. format->fmt.vbi.offset = 0;
  1563. format->fmt.vbi.flags = 0;
  1564. format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
  1565. format->fmt.vbi.count[0] = dev->vbi_height;
  1566. format->fmt.vbi.count[1] = dev->vbi_height;
  1567. /* Varies by video standard (NTSC, PAL, etc.) */
  1568. if (dev->norm & V4L2_STD_525_60) {
  1569. /* NTSC */
  1570. format->fmt.vbi.start[0] = 10;
  1571. format->fmt.vbi.start[1] = 273;
  1572. } else if (dev->norm & V4L2_STD_625_50) {
  1573. /* PAL */
  1574. format->fmt.vbi.start[0] = 6;
  1575. format->fmt.vbi.start[1] = 318;
  1576. }
  1577. return 0;
  1578. }
  1579. static int vidioc_reqbufs(struct file *file, void *priv,
  1580. struct v4l2_requestbuffers *rb)
  1581. {
  1582. struct em28xx_fh *fh = priv;
  1583. struct em28xx *dev = fh->dev;
  1584. int rc;
  1585. rc = check_dev(dev);
  1586. if (rc < 0)
  1587. return rc;
  1588. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1589. return videobuf_reqbufs(&fh->vb_vidq, rb);
  1590. else
  1591. return videobuf_reqbufs(&fh->vb_vbiq, rb);
  1592. }
  1593. static int vidioc_querybuf(struct file *file, void *priv,
  1594. struct v4l2_buffer *b)
  1595. {
  1596. struct em28xx_fh *fh = priv;
  1597. struct em28xx *dev = fh->dev;
  1598. int rc;
  1599. rc = check_dev(dev);
  1600. if (rc < 0)
  1601. return rc;
  1602. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1603. return videobuf_querybuf(&fh->vb_vidq, b);
  1604. else {
  1605. /* FIXME: I'm not sure yet whether this is a bug in zvbi or
  1606. the videobuf framework, but we probably shouldn't be
  1607. returning a buffer larger than that which was asked for.
  1608. At a minimum, it causes a crash in zvbi since it does
  1609. a memcpy based on the source buffer length */
  1610. int result = videobuf_querybuf(&fh->vb_vbiq, b);
  1611. b->length = dev->vbi_width * dev->vbi_height * 2;
  1612. return result;
  1613. }
  1614. }
  1615. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1616. {
  1617. struct em28xx_fh *fh = priv;
  1618. struct em28xx *dev = fh->dev;
  1619. int rc;
  1620. rc = check_dev(dev);
  1621. if (rc < 0)
  1622. return rc;
  1623. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1624. return videobuf_qbuf(&fh->vb_vidq, b);
  1625. else
  1626. return videobuf_qbuf(&fh->vb_vbiq, b);
  1627. }
  1628. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1629. {
  1630. struct em28xx_fh *fh = priv;
  1631. struct em28xx *dev = fh->dev;
  1632. int rc;
  1633. rc = check_dev(dev);
  1634. if (rc < 0)
  1635. return rc;
  1636. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1637. return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags &
  1638. O_NONBLOCK);
  1639. else
  1640. return videobuf_dqbuf(&fh->vb_vbiq, b, file->f_flags &
  1641. O_NONBLOCK);
  1642. }
  1643. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1644. static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
  1645. {
  1646. struct em28xx_fh *fh = priv;
  1647. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1648. return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
  1649. else
  1650. return videobuf_cgmbuf(&fh->vb_vbiq, mbuf, 8);
  1651. }
  1652. #endif
  1653. /* ----------------------------------------------------------- */
  1654. /* RADIO ESPECIFIC IOCTLS */
  1655. /* ----------------------------------------------------------- */
  1656. static int radio_querycap(struct file *file, void *priv,
  1657. struct v4l2_capability *cap)
  1658. {
  1659. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1660. strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
  1661. strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
  1662. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  1663. cap->version = EM28XX_VERSION_CODE;
  1664. cap->capabilities = V4L2_CAP_TUNER;
  1665. return 0;
  1666. }
  1667. static int radio_g_tuner(struct file *file, void *priv,
  1668. struct v4l2_tuner *t)
  1669. {
  1670. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1671. if (unlikely(t->index > 0))
  1672. return -EINVAL;
  1673. strcpy(t->name, "Radio");
  1674. t->type = V4L2_TUNER_RADIO;
  1675. mutex_lock(&dev->lock);
  1676. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
  1677. mutex_unlock(&dev->lock);
  1678. return 0;
  1679. }
  1680. static int radio_enum_input(struct file *file, void *priv,
  1681. struct v4l2_input *i)
  1682. {
  1683. if (i->index != 0)
  1684. return -EINVAL;
  1685. strcpy(i->name, "Radio");
  1686. i->type = V4L2_INPUT_TYPE_TUNER;
  1687. return 0;
  1688. }
  1689. static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  1690. {
  1691. if (unlikely(a->index))
  1692. return -EINVAL;
  1693. strcpy(a->name, "Radio");
  1694. return 0;
  1695. }
  1696. static int radio_s_tuner(struct file *file, void *priv,
  1697. struct v4l2_tuner *t)
  1698. {
  1699. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1700. if (0 != t->index)
  1701. return -EINVAL;
  1702. mutex_lock(&dev->lock);
  1703. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
  1704. mutex_unlock(&dev->lock);
  1705. return 0;
  1706. }
  1707. static int radio_s_audio(struct file *file, void *fh,
  1708. struct v4l2_audio *a)
  1709. {
  1710. return 0;
  1711. }
  1712. static int radio_s_input(struct file *file, void *fh, unsigned int i)
  1713. {
  1714. return 0;
  1715. }
  1716. static int radio_queryctrl(struct file *file, void *priv,
  1717. struct v4l2_queryctrl *qc)
  1718. {
  1719. int i;
  1720. if (qc->id < V4L2_CID_BASE ||
  1721. qc->id >= V4L2_CID_LASTP1)
  1722. return -EINVAL;
  1723. for (i = 0; i < ARRAY_SIZE(ac97_qctrl); i++) {
  1724. if (qc->id && qc->id == ac97_qctrl[i].id) {
  1725. memcpy(qc, &(ac97_qctrl[i]), sizeof(*qc));
  1726. return 0;
  1727. }
  1728. }
  1729. return -EINVAL;
  1730. }
  1731. /*
  1732. * em28xx_v4l2_open()
  1733. * inits the device and starts isoc transfer
  1734. */
  1735. static int em28xx_v4l2_open(struct file *filp)
  1736. {
  1737. int errCode = 0, radio = 0;
  1738. struct video_device *vdev = video_devdata(filp);
  1739. struct em28xx *dev = video_drvdata(filp);
  1740. enum v4l2_buf_type fh_type = 0;
  1741. struct em28xx_fh *fh;
  1742. enum v4l2_field field;
  1743. switch (vdev->vfl_type) {
  1744. case VFL_TYPE_GRABBER:
  1745. fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1746. break;
  1747. case VFL_TYPE_VBI:
  1748. fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
  1749. break;
  1750. case VFL_TYPE_RADIO:
  1751. radio = 1;
  1752. break;
  1753. }
  1754. mutex_lock(&dev->lock);
  1755. em28xx_videodbg("open dev=%s type=%s users=%d\n",
  1756. video_device_node_name(vdev), v4l2_type_names[fh_type],
  1757. dev->users);
  1758. fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
  1759. if (!fh) {
  1760. em28xx_errdev("em28xx-video.c: Out of memory?!\n");
  1761. mutex_unlock(&dev->lock);
  1762. return -ENOMEM;
  1763. }
  1764. fh->dev = dev;
  1765. fh->radio = radio;
  1766. fh->type = fh_type;
  1767. filp->private_data = fh;
  1768. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
  1769. em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
  1770. em28xx_set_alternate(dev);
  1771. em28xx_resolution_set(dev);
  1772. /* Needed, since GPIO might have disabled power of
  1773. some i2c device
  1774. */
  1775. em28xx_wake_i2c(dev);
  1776. }
  1777. if (fh->radio) {
  1778. em28xx_videodbg("video_open: setting radio device\n");
  1779. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
  1780. }
  1781. dev->users++;
  1782. if (dev->progressive)
  1783. field = V4L2_FIELD_NONE;
  1784. else
  1785. field = V4L2_FIELD_INTERLACED;
  1786. videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
  1787. NULL, &dev->slock,
  1788. V4L2_BUF_TYPE_VIDEO_CAPTURE, field,
  1789. sizeof(struct em28xx_buffer), fh);
  1790. videobuf_queue_vmalloc_init(&fh->vb_vbiq, &em28xx_vbi_qops,
  1791. NULL, &dev->slock,
  1792. V4L2_BUF_TYPE_VBI_CAPTURE,
  1793. V4L2_FIELD_SEQ_TB,
  1794. sizeof(struct em28xx_buffer), fh);
  1795. mutex_unlock(&dev->lock);
  1796. return errCode;
  1797. }
  1798. /*
  1799. * em28xx_realease_resources()
  1800. * unregisters the v4l2,i2c and usb devices
  1801. * called when the device gets disconected or at module unload
  1802. */
  1803. void em28xx_release_analog_resources(struct em28xx *dev)
  1804. {
  1805. /*FIXME: I2C IR should be disconnected */
  1806. if (dev->radio_dev) {
  1807. if (video_is_registered(dev->radio_dev))
  1808. video_unregister_device(dev->radio_dev);
  1809. else
  1810. video_device_release(dev->radio_dev);
  1811. dev->radio_dev = NULL;
  1812. }
  1813. if (dev->vbi_dev) {
  1814. em28xx_info("V4L2 device %s deregistered\n",
  1815. video_device_node_name(dev->vbi_dev));
  1816. if (video_is_registered(dev->vbi_dev))
  1817. video_unregister_device(dev->vbi_dev);
  1818. else
  1819. video_device_release(dev->vbi_dev);
  1820. dev->vbi_dev = NULL;
  1821. }
  1822. if (dev->vdev) {
  1823. em28xx_info("V4L2 device %s deregistered\n",
  1824. video_device_node_name(dev->vdev));
  1825. if (video_is_registered(dev->vdev))
  1826. video_unregister_device(dev->vdev);
  1827. else
  1828. video_device_release(dev->vdev);
  1829. dev->vdev = NULL;
  1830. }
  1831. }
  1832. /*
  1833. * em28xx_v4l2_close()
  1834. * stops streaming and deallocates all resources allocated by the v4l2
  1835. * calls and ioctls
  1836. */
  1837. static int em28xx_v4l2_close(struct file *filp)
  1838. {
  1839. struct em28xx_fh *fh = filp->private_data;
  1840. struct em28xx *dev = fh->dev;
  1841. int errCode;
  1842. em28xx_videodbg("users=%d\n", dev->users);
  1843. if (res_check(fh, EM28XX_RESOURCE_VIDEO)) {
  1844. videobuf_stop(&fh->vb_vidq);
  1845. res_free(fh, EM28XX_RESOURCE_VIDEO);
  1846. }
  1847. if (res_check(fh, EM28XX_RESOURCE_VBI)) {
  1848. videobuf_stop(&fh->vb_vbiq);
  1849. res_free(fh, EM28XX_RESOURCE_VBI);
  1850. }
  1851. if (dev->users == 1) {
  1852. /* the device is already disconnect,
  1853. free the remaining resources */
  1854. if (dev->state & DEV_DISCONNECTED) {
  1855. em28xx_release_resources(dev);
  1856. kfree(dev);
  1857. return 0;
  1858. }
  1859. /* Save some power by putting tuner to sleep */
  1860. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
  1861. /* do this before setting alternate! */
  1862. em28xx_uninit_isoc(dev);
  1863. em28xx_set_mode(dev, EM28XX_SUSPEND);
  1864. /* set alternate 0 */
  1865. dev->alt = 0;
  1866. em28xx_videodbg("setting alternate 0\n");
  1867. errCode = usb_set_interface(dev->udev, 0, 0);
  1868. if (errCode < 0) {
  1869. em28xx_errdev("cannot change alternate number to "
  1870. "0 (error=%i)\n", errCode);
  1871. }
  1872. }
  1873. videobuf_mmap_free(&fh->vb_vidq);
  1874. videobuf_mmap_free(&fh->vb_vbiq);
  1875. kfree(fh);
  1876. dev->users--;
  1877. wake_up_interruptible_nr(&dev->open, 1);
  1878. return 0;
  1879. }
  1880. /*
  1881. * em28xx_v4l2_read()
  1882. * will allocate buffers when called for the first time
  1883. */
  1884. static ssize_t
  1885. em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
  1886. loff_t *pos)
  1887. {
  1888. struct em28xx_fh *fh = filp->private_data;
  1889. struct em28xx *dev = fh->dev;
  1890. int rc;
  1891. rc = check_dev(dev);
  1892. if (rc < 0)
  1893. return rc;
  1894. /* FIXME: read() is not prepared to allow changing the video
  1895. resolution while streaming. Seems a bug at em28xx_set_fmt
  1896. */
  1897. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  1898. if (res_locked(dev, EM28XX_RESOURCE_VIDEO))
  1899. return -EBUSY;
  1900. return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
  1901. filp->f_flags & O_NONBLOCK);
  1902. }
  1903. if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
  1904. if (!res_get(fh, EM28XX_RESOURCE_VBI))
  1905. return -EBUSY;
  1906. return videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0,
  1907. filp->f_flags & O_NONBLOCK);
  1908. }
  1909. return 0;
  1910. }
  1911. /*
  1912. * em28xx_v4l2_poll()
  1913. * will allocate buffers when called for the first time
  1914. */
  1915. static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
  1916. {
  1917. struct em28xx_fh *fh = filp->private_data;
  1918. struct em28xx *dev = fh->dev;
  1919. int rc;
  1920. rc = check_dev(dev);
  1921. if (rc < 0)
  1922. return rc;
  1923. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  1924. if (!res_get(fh, EM28XX_RESOURCE_VIDEO))
  1925. return POLLERR;
  1926. return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
  1927. } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
  1928. if (!res_get(fh, EM28XX_RESOURCE_VBI))
  1929. return POLLERR;
  1930. return videobuf_poll_stream(filp, &fh->vb_vbiq, wait);
  1931. } else {
  1932. return POLLERR;
  1933. }
  1934. }
  1935. /*
  1936. * em28xx_v4l2_mmap()
  1937. */
  1938. static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
  1939. {
  1940. struct em28xx_fh *fh = filp->private_data;
  1941. struct em28xx *dev = fh->dev;
  1942. int rc;
  1943. rc = check_dev(dev);
  1944. if (rc < 0)
  1945. return rc;
  1946. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1947. rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
  1948. else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
  1949. rc = videobuf_mmap_mapper(&fh->vb_vbiq, vma);
  1950. em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
  1951. (unsigned long)vma->vm_start,
  1952. (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
  1953. rc);
  1954. return rc;
  1955. }
  1956. static const struct v4l2_file_operations em28xx_v4l_fops = {
  1957. .owner = THIS_MODULE,
  1958. .open = em28xx_v4l2_open,
  1959. .release = em28xx_v4l2_close,
  1960. .read = em28xx_v4l2_read,
  1961. .poll = em28xx_v4l2_poll,
  1962. .mmap = em28xx_v4l2_mmap,
  1963. .ioctl = video_ioctl2,
  1964. };
  1965. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1966. .vidioc_querycap = vidioc_querycap,
  1967. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1968. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1969. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1970. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1971. .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
  1972. .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
  1973. .vidioc_g_audio = vidioc_g_audio,
  1974. .vidioc_s_audio = vidioc_s_audio,
  1975. .vidioc_cropcap = vidioc_cropcap,
  1976. .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
  1977. .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
  1978. .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
  1979. .vidioc_reqbufs = vidioc_reqbufs,
  1980. .vidioc_querybuf = vidioc_querybuf,
  1981. .vidioc_qbuf = vidioc_qbuf,
  1982. .vidioc_dqbuf = vidioc_dqbuf,
  1983. .vidioc_g_std = vidioc_g_std,
  1984. .vidioc_s_std = vidioc_s_std,
  1985. .vidioc_g_parm = vidioc_g_parm,
  1986. .vidioc_s_parm = vidioc_s_parm,
  1987. .vidioc_enum_input = vidioc_enum_input,
  1988. .vidioc_g_input = vidioc_g_input,
  1989. .vidioc_s_input = vidioc_s_input,
  1990. .vidioc_queryctrl = vidioc_queryctrl,
  1991. .vidioc_g_ctrl = vidioc_g_ctrl,
  1992. .vidioc_s_ctrl = vidioc_s_ctrl,
  1993. .vidioc_streamon = vidioc_streamon,
  1994. .vidioc_streamoff = vidioc_streamoff,
  1995. .vidioc_g_tuner = vidioc_g_tuner,
  1996. .vidioc_s_tuner = vidioc_s_tuner,
  1997. .vidioc_g_frequency = vidioc_g_frequency,
  1998. .vidioc_s_frequency = vidioc_s_frequency,
  1999. #ifdef CONFIG_VIDEO_ADV_DEBUG
  2000. .vidioc_g_register = vidioc_g_register,
  2001. .vidioc_s_register = vidioc_s_register,
  2002. .vidioc_g_chip_ident = vidioc_g_chip_ident,
  2003. #endif
  2004. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  2005. .vidiocgmbuf = vidiocgmbuf,
  2006. #endif
  2007. };
  2008. static const struct video_device em28xx_video_template = {
  2009. .fops = &em28xx_v4l_fops,
  2010. .release = video_device_release,
  2011. .ioctl_ops = &video_ioctl_ops,
  2012. .tvnorms = V4L2_STD_ALL,
  2013. .current_norm = V4L2_STD_PAL,
  2014. };
  2015. static const struct v4l2_file_operations radio_fops = {
  2016. .owner = THIS_MODULE,
  2017. .open = em28xx_v4l2_open,
  2018. .release = em28xx_v4l2_close,
  2019. .ioctl = video_ioctl2,
  2020. };
  2021. static const struct v4l2_ioctl_ops radio_ioctl_ops = {
  2022. .vidioc_querycap = radio_querycap,
  2023. .vidioc_g_tuner = radio_g_tuner,
  2024. .vidioc_enum_input = radio_enum_input,
  2025. .vidioc_g_audio = radio_g_audio,
  2026. .vidioc_s_tuner = radio_s_tuner,
  2027. .vidioc_s_audio = radio_s_audio,
  2028. .vidioc_s_input = radio_s_input,
  2029. .vidioc_queryctrl = radio_queryctrl,
  2030. .vidioc_g_ctrl = vidioc_g_ctrl,
  2031. .vidioc_s_ctrl = vidioc_s_ctrl,
  2032. .vidioc_g_frequency = vidioc_g_frequency,
  2033. .vidioc_s_frequency = vidioc_s_frequency,
  2034. #ifdef CONFIG_VIDEO_ADV_DEBUG
  2035. .vidioc_g_register = vidioc_g_register,
  2036. .vidioc_s_register = vidioc_s_register,
  2037. #endif
  2038. };
  2039. static struct video_device em28xx_radio_template = {
  2040. .name = "em28xx-radio",
  2041. .fops = &radio_fops,
  2042. .ioctl_ops = &radio_ioctl_ops,
  2043. };
  2044. /******************************** usb interface ******************************/
  2045. static struct video_device *em28xx_vdev_init(struct em28xx *dev,
  2046. const struct video_device *template,
  2047. const char *type_name)
  2048. {
  2049. struct video_device *vfd;
  2050. vfd = video_device_alloc();
  2051. if (NULL == vfd)
  2052. return NULL;
  2053. *vfd = *template;
  2054. vfd->v4l2_dev = &dev->v4l2_dev;
  2055. vfd->release = video_device_release;
  2056. vfd->debug = video_debug;
  2057. snprintf(vfd->name, sizeof(vfd->name), "%s %s",
  2058. dev->name, type_name);
  2059. video_set_drvdata(vfd, dev);
  2060. return vfd;
  2061. }
  2062. int em28xx_register_analog_devices(struct em28xx *dev)
  2063. {
  2064. u8 val;
  2065. int ret;
  2066. printk(KERN_INFO "%s: v4l2 driver version %d.%d.%d\n",
  2067. dev->name,
  2068. (EM28XX_VERSION_CODE >> 16) & 0xff,
  2069. (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
  2070. /* set default norm */
  2071. dev->norm = em28xx_video_template.current_norm;
  2072. dev->interlaced = EM28XX_INTERLACED_DEFAULT;
  2073. dev->ctl_input = 0;
  2074. /* Analog specific initialization */
  2075. dev->format = &format[0];
  2076. em28xx_set_video_format(dev, format[0].fourcc,
  2077. norm_maxw(dev), norm_maxh(dev));
  2078. video_mux(dev, dev->ctl_input);
  2079. /* Audio defaults */
  2080. dev->mute = 1;
  2081. dev->volume = 0x1f;
  2082. /* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
  2083. val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
  2084. em28xx_write_reg(dev, EM28XX_R0F_XCLK,
  2085. (EM28XX_XCLK_AUDIO_UNMUTE | val));
  2086. em28xx_set_outfmt(dev);
  2087. em28xx_colorlevels_set_default(dev);
  2088. em28xx_compression_disable(dev);
  2089. /* allocate and fill video video_device struct */
  2090. dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
  2091. if (!dev->vdev) {
  2092. em28xx_errdev("cannot allocate video_device.\n");
  2093. return -ENODEV;
  2094. }
  2095. /* register v4l2 video video_device */
  2096. ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
  2097. video_nr[dev->devno]);
  2098. if (ret) {
  2099. em28xx_errdev("unable to register video device (error=%i).\n",
  2100. ret);
  2101. return ret;
  2102. }
  2103. /* Allocate and fill vbi video_device struct */
  2104. if (em28xx_vbi_supported(dev) == 1) {
  2105. dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
  2106. "vbi");
  2107. /* register v4l2 vbi video_device */
  2108. ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
  2109. vbi_nr[dev->devno]);
  2110. if (ret < 0) {
  2111. em28xx_errdev("unable to register vbi device\n");
  2112. return ret;
  2113. }
  2114. }
  2115. if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
  2116. dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
  2117. "radio");
  2118. if (!dev->radio_dev) {
  2119. em28xx_errdev("cannot allocate video_device.\n");
  2120. return -ENODEV;
  2121. }
  2122. ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
  2123. radio_nr[dev->devno]);
  2124. if (ret < 0) {
  2125. em28xx_errdev("can't register radio device\n");
  2126. return ret;
  2127. }
  2128. em28xx_info("Registered radio device as %s\n",
  2129. video_device_node_name(dev->radio_dev));
  2130. }
  2131. em28xx_info("V4L2 video device registered as %s\n",
  2132. video_device_node_name(dev->vdev));
  2133. if (dev->vbi_dev)
  2134. em28xx_info("V4L2 VBI device registered as %s\n",
  2135. video_device_node_name(dev->vbi_dev));
  2136. return 0;
  2137. }