em28xx-video.c 65 KB

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