em28xx-video.c 61 KB

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