em28xx-video.c 61 KB

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