em28xx-video.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038
  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-event.h>
  37. #include <media/v4l2-chip-ident.h>
  38. #include <media/msp3400.h>
  39. #include <media/tuner.h>
  40. #define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
  41. "Markus Rechberger <mrechberger@gmail.com>, " \
  42. "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
  43. "Sascha Sommer <saschasommer@freenet.de>"
  44. #define DRIVER_DESC "Empia em28xx based USB video device driver"
  45. #define EM28XX_VERSION "0.2.0"
  46. #define em28xx_videodbg(fmt, arg...) do {\
  47. if (video_debug) \
  48. printk(KERN_INFO "%s %s :"fmt, \
  49. dev->name, __func__ , ##arg); } while (0)
  50. static unsigned int isoc_debug;
  51. module_param(isoc_debug, int, 0644);
  52. MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
  53. #define em28xx_isocdbg(fmt, arg...) \
  54. do {\
  55. if (isoc_debug) { \
  56. printk(KERN_INFO "%s %s :"fmt, \
  57. dev->name, __func__ , ##arg); \
  58. } \
  59. } while (0)
  60. MODULE_AUTHOR(DRIVER_AUTHOR);
  61. MODULE_DESCRIPTION(DRIVER_DESC);
  62. MODULE_LICENSE("GPL");
  63. MODULE_VERSION(EM28XX_VERSION);
  64. #define EM25XX_FRMDATAHDR_BYTE1 0x02
  65. #define EM25XX_FRMDATAHDR_BYTE2_STILL_IMAGE 0x20
  66. #define EM25XX_FRMDATAHDR_BYTE2_FRAME_END 0x02
  67. #define EM25XX_FRMDATAHDR_BYTE2_FRAME_ID 0x01
  68. #define EM25XX_FRMDATAHDR_BYTE2_MASK (EM25XX_FRMDATAHDR_BYTE2_STILL_IMAGE | \
  69. EM25XX_FRMDATAHDR_BYTE2_FRAME_END | \
  70. EM25XX_FRMDATAHDR_BYTE2_FRAME_ID)
  71. static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = -1U };
  72. static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = -1U };
  73. static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = -1U };
  74. module_param_array(video_nr, int, NULL, 0444);
  75. module_param_array(vbi_nr, int, NULL, 0444);
  76. module_param_array(radio_nr, int, NULL, 0444);
  77. MODULE_PARM_DESC(video_nr, "video device numbers");
  78. MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
  79. MODULE_PARM_DESC(radio_nr, "radio device numbers");
  80. static unsigned int video_debug;
  81. module_param(video_debug, int, 0644);
  82. MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
  83. /* supported video standards */
  84. static struct em28xx_fmt format[] = {
  85. {
  86. .name = "16 bpp YUY2, 4:2:2, packed",
  87. .fourcc = V4L2_PIX_FMT_YUYV,
  88. .depth = 16,
  89. .reg = EM28XX_OUTFMT_YUV422_Y0UY1V,
  90. }, {
  91. .name = "16 bpp RGB 565, LE",
  92. .fourcc = V4L2_PIX_FMT_RGB565,
  93. .depth = 16,
  94. .reg = EM28XX_OUTFMT_RGB_16_656,
  95. }, {
  96. .name = "8 bpp Bayer BGBG..GRGR",
  97. .fourcc = V4L2_PIX_FMT_SBGGR8,
  98. .depth = 8,
  99. .reg = EM28XX_OUTFMT_RGB_8_BGBG,
  100. }, {
  101. .name = "8 bpp Bayer GRGR..BGBG",
  102. .fourcc = V4L2_PIX_FMT_SGRBG8,
  103. .depth = 8,
  104. .reg = EM28XX_OUTFMT_RGB_8_GRGR,
  105. }, {
  106. .name = "8 bpp Bayer GBGB..RGRG",
  107. .fourcc = V4L2_PIX_FMT_SGBRG8,
  108. .depth = 8,
  109. .reg = EM28XX_OUTFMT_RGB_8_GBGB,
  110. }, {
  111. .name = "12 bpp YUV411",
  112. .fourcc = V4L2_PIX_FMT_YUV411P,
  113. .depth = 12,
  114. .reg = EM28XX_OUTFMT_YUV411,
  115. },
  116. };
  117. /* ------------------------------------------------------------------
  118. DMA and thread functions
  119. ------------------------------------------------------------------*/
  120. /*
  121. * Finish the current buffer
  122. */
  123. static inline void finish_buffer(struct em28xx *dev,
  124. struct em28xx_buffer *buf)
  125. {
  126. em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->top_field);
  127. buf->vb.v4l2_buf.sequence = dev->field_count++;
  128. buf->vb.v4l2_buf.field = V4L2_FIELD_INTERLACED;
  129. v4l2_get_timestamp(&buf->vb.v4l2_buf.timestamp);
  130. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_DONE);
  131. }
  132. /*
  133. * Copy picture data from USB buffer to videobuf buffer
  134. */
  135. static void em28xx_copy_video(struct em28xx *dev,
  136. struct em28xx_buffer *buf,
  137. unsigned char *usb_buf,
  138. unsigned long len)
  139. {
  140. void *fieldstart, *startwrite, *startread;
  141. int linesdone, currlinedone, offset, lencopy, remain;
  142. int bytesperline = dev->width << 1;
  143. if (buf->pos + len > buf->length)
  144. len = buf->length - buf->pos;
  145. startread = usb_buf;
  146. remain = len;
  147. if (dev->progressive || buf->top_field)
  148. fieldstart = buf->vb_buf;
  149. else /* interlaced mode, even nr. of lines */
  150. fieldstart = buf->vb_buf + bytesperline;
  151. linesdone = buf->pos / bytesperline;
  152. currlinedone = buf->pos % bytesperline;
  153. if (dev->progressive)
  154. offset = linesdone * bytesperline + currlinedone;
  155. else
  156. offset = linesdone * bytesperline * 2 + currlinedone;
  157. startwrite = fieldstart + offset;
  158. lencopy = bytesperline - currlinedone;
  159. lencopy = lencopy > remain ? remain : lencopy;
  160. if ((char *)startwrite + lencopy > (char *)buf->vb_buf + buf->length) {
  161. em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
  162. ((char *)startwrite + lencopy) -
  163. ((char *)buf->vb_buf + buf->length));
  164. remain = (char *)buf->vb_buf + buf->length -
  165. (char *)startwrite;
  166. lencopy = remain;
  167. }
  168. if (lencopy <= 0)
  169. return;
  170. memcpy(startwrite, startread, lencopy);
  171. remain -= lencopy;
  172. while (remain > 0) {
  173. if (dev->progressive)
  174. startwrite += lencopy;
  175. else
  176. startwrite += lencopy + bytesperline;
  177. startread += lencopy;
  178. if (bytesperline > remain)
  179. lencopy = remain;
  180. else
  181. lencopy = bytesperline;
  182. if ((char *)startwrite + lencopy > (char *)buf->vb_buf +
  183. buf->length) {
  184. em28xx_isocdbg("Overflow of %zi bytes past buffer end"
  185. "(2)\n",
  186. ((char *)startwrite + lencopy) -
  187. ((char *)buf->vb_buf + buf->length));
  188. lencopy = remain = (char *)buf->vb_buf + buf->length -
  189. (char *)startwrite;
  190. }
  191. if (lencopy <= 0)
  192. break;
  193. memcpy(startwrite, startread, lencopy);
  194. remain -= lencopy;
  195. }
  196. buf->pos += len;
  197. }
  198. /*
  199. * Copy VBI data from USB buffer to videobuf buffer
  200. */
  201. static void em28xx_copy_vbi(struct em28xx *dev,
  202. struct em28xx_buffer *buf,
  203. unsigned char *usb_buf,
  204. unsigned long len)
  205. {
  206. unsigned int offset;
  207. if (buf->pos + len > buf->length)
  208. len = buf->length - buf->pos;
  209. offset = buf->pos;
  210. /* Make sure the bottom field populates the second half of the frame */
  211. if (buf->top_field == 0)
  212. offset += dev->vbi_width * dev->vbi_height;
  213. memcpy(buf->vb_buf + offset, usb_buf, len);
  214. buf->pos += len;
  215. }
  216. static inline void print_err_status(struct em28xx *dev,
  217. int packet, int status)
  218. {
  219. char *errmsg = "Unknown";
  220. switch (status) {
  221. case -ENOENT:
  222. errmsg = "unlinked synchronuously";
  223. break;
  224. case -ECONNRESET:
  225. errmsg = "unlinked asynchronuously";
  226. break;
  227. case -ENOSR:
  228. errmsg = "Buffer error (overrun)";
  229. break;
  230. case -EPIPE:
  231. errmsg = "Stalled (device not responding)";
  232. break;
  233. case -EOVERFLOW:
  234. errmsg = "Babble (bad cable?)";
  235. break;
  236. case -EPROTO:
  237. errmsg = "Bit-stuff error (bad cable?)";
  238. break;
  239. case -EILSEQ:
  240. errmsg = "CRC/Timeout (could be anything)";
  241. break;
  242. case -ETIME:
  243. errmsg = "Device does not respond";
  244. break;
  245. }
  246. if (packet < 0) {
  247. em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
  248. } else {
  249. em28xx_isocdbg("URB packet %d, status %d [%s].\n",
  250. packet, status, errmsg);
  251. }
  252. }
  253. /*
  254. * get the next available buffer from dma queue
  255. */
  256. static inline struct em28xx_buffer *get_next_buf(struct em28xx *dev,
  257. struct em28xx_dmaqueue *dma_q)
  258. {
  259. struct em28xx_buffer *buf;
  260. if (list_empty(&dma_q->active)) {
  261. em28xx_isocdbg("No active queue to serve\n");
  262. return NULL;
  263. }
  264. /* Get the next buffer */
  265. buf = list_entry(dma_q->active.next, struct em28xx_buffer, list);
  266. /* Cleans up buffer - Useful for testing for frame/URB loss */
  267. list_del(&buf->list);
  268. buf->pos = 0;
  269. buf->vb_buf = buf->mem;
  270. return buf;
  271. }
  272. /*
  273. * Finish the current buffer if completed and prepare for the next field
  274. */
  275. static struct em28xx_buffer *
  276. finish_field_prepare_next(struct em28xx *dev,
  277. struct em28xx_buffer *buf,
  278. struct em28xx_dmaqueue *dma_q)
  279. {
  280. if (dev->progressive || dev->top_field) { /* Brand new frame */
  281. if (buf != NULL)
  282. finish_buffer(dev, buf);
  283. buf = get_next_buf(dev, dma_q);
  284. }
  285. if (buf != NULL) {
  286. buf->top_field = dev->top_field;
  287. buf->pos = 0;
  288. }
  289. return buf;
  290. }
  291. /*
  292. * Process data packet according to the em2710/em2750/em28xx frame data format
  293. */
  294. static inline void process_frame_data_em28xx(struct em28xx *dev,
  295. unsigned char *data_pkt,
  296. unsigned int data_len)
  297. {
  298. struct em28xx_buffer *buf = dev->usb_ctl.vid_buf;
  299. struct em28xx_buffer *vbi_buf = dev->usb_ctl.vbi_buf;
  300. struct em28xx_dmaqueue *dma_q = &dev->vidq;
  301. struct em28xx_dmaqueue *vbi_dma_q = &dev->vbiq;
  302. /* capture type 0 = vbi start
  303. capture type 1 = vbi in progress
  304. capture type 2 = video start
  305. capture type 3 = video in progress */
  306. if (data_len >= 4) {
  307. /* NOTE: Headers are always 4 bytes and
  308. * never split across packets */
  309. if (data_pkt[0] == 0x88 && data_pkt[1] == 0x88 &&
  310. data_pkt[2] == 0x88 && data_pkt[3] == 0x88) {
  311. /* Continuation */
  312. data_pkt += 4;
  313. data_len -= 4;
  314. } else if (data_pkt[0] == 0x33 && data_pkt[1] == 0x95) {
  315. /* Field start (VBI mode) */
  316. dev->capture_type = 0;
  317. dev->vbi_read = 0;
  318. em28xx_isocdbg("VBI START HEADER !!!\n");
  319. dev->top_field = !(data_pkt[2] & 1);
  320. data_pkt += 4;
  321. data_len -= 4;
  322. } else if (data_pkt[0] == 0x22 && data_pkt[1] == 0x5a) {
  323. /* Field start (VBI disabled) */
  324. dev->capture_type = 2;
  325. em28xx_isocdbg("VIDEO START HEADER !!!\n");
  326. dev->top_field = !(data_pkt[2] & 1);
  327. data_pkt += 4;
  328. data_len -= 4;
  329. }
  330. }
  331. /* NOTE: With bulk transfers, intermediate data packets
  332. * have no continuation header */
  333. if (dev->capture_type == 0) {
  334. vbi_buf = finish_field_prepare_next(dev, vbi_buf, vbi_dma_q);
  335. dev->usb_ctl.vbi_buf = vbi_buf;
  336. dev->capture_type = 1;
  337. }
  338. if (dev->capture_type == 1) {
  339. int vbi_size = dev->vbi_width * dev->vbi_height;
  340. int vbi_data_len = ((dev->vbi_read + data_len) > vbi_size) ?
  341. (vbi_size - dev->vbi_read) : data_len;
  342. /* Copy VBI data */
  343. if (vbi_buf != NULL)
  344. em28xx_copy_vbi(dev, vbi_buf, data_pkt, vbi_data_len);
  345. dev->vbi_read += vbi_data_len;
  346. if (vbi_data_len < data_len) {
  347. /* Continue with copying video data */
  348. dev->capture_type = 2;
  349. data_pkt += vbi_data_len;
  350. data_len -= vbi_data_len;
  351. }
  352. }
  353. if (dev->capture_type == 2) {
  354. buf = finish_field_prepare_next(dev, buf, dma_q);
  355. dev->usb_ctl.vid_buf = buf;
  356. dev->capture_type = 3;
  357. }
  358. if (dev->capture_type == 3 && buf != NULL && data_len > 0)
  359. em28xx_copy_video(dev, buf, data_pkt, data_len);
  360. }
  361. /*
  362. * Process data packet according to the em25xx/em276x/7x/8x frame data format
  363. */
  364. static inline void process_frame_data_em25xx(struct em28xx *dev,
  365. unsigned char *data_pkt,
  366. unsigned int data_len)
  367. {
  368. struct em28xx_buffer *buf = dev->usb_ctl.vid_buf;
  369. struct em28xx_dmaqueue *dmaq = &dev->vidq;
  370. bool frame_end = 0;
  371. /* Check for header */
  372. /* NOTE: at least with bulk transfers, only the first packet
  373. * has a header and has always set the FRAME_END bit */
  374. if (data_len >= 2) { /* em25xx header is only 2 bytes long */
  375. if ((data_pkt[0] == EM25XX_FRMDATAHDR_BYTE1) &&
  376. ((data_pkt[1] & ~EM25XX_FRMDATAHDR_BYTE2_MASK) == 0x00)) {
  377. dev->top_field = !(data_pkt[1] &
  378. EM25XX_FRMDATAHDR_BYTE2_FRAME_ID);
  379. frame_end = data_pkt[1] &
  380. EM25XX_FRMDATAHDR_BYTE2_FRAME_END;
  381. data_pkt += 2;
  382. data_len -= 2;
  383. }
  384. /* Finish field and prepare next (BULK only) */
  385. if (dev->analog_xfer_bulk && frame_end) {
  386. buf = finish_field_prepare_next(dev, buf, dmaq);
  387. dev->usb_ctl.vid_buf = buf;
  388. }
  389. /* NOTE: in ISOC mode when a new frame starts and buf==NULL,
  390. * we COULD already prepare a buffer here to avoid skipping the
  391. * first frame.
  392. */
  393. }
  394. /* Copy data */
  395. if (buf != NULL && data_len > 0)
  396. em28xx_copy_video(dev, buf, data_pkt, data_len);
  397. /* Finish frame (ISOC only) => avoids lag of 1 frame */
  398. if (!dev->analog_xfer_bulk && frame_end) {
  399. buf = finish_field_prepare_next(dev, buf, dmaq);
  400. dev->usb_ctl.vid_buf = buf;
  401. }
  402. /* NOTE: Tested with USB bulk transfers only !
  403. * The wording in the datasheet suggests that isoc might work different.
  404. * The current code assumes that with isoc transfers each packet has a
  405. * header like with the other em28xx devices.
  406. */
  407. /* NOTE: Support for interlaced mode is pure theory. It has not been
  408. * tested and it is unknown if these devices actually support it. */
  409. /* NOTE: No VBI support yet (these chips likely do not support VBI). */
  410. }
  411. /* Processes and copies the URB data content (video and VBI data) */
  412. static inline int em28xx_urb_data_copy(struct em28xx *dev, struct urb *urb)
  413. {
  414. int xfer_bulk, num_packets, i;
  415. unsigned char *usb_data_pkt;
  416. unsigned int usb_data_len;
  417. if (!dev)
  418. return 0;
  419. if (dev->disconnected)
  420. return 0;
  421. if (urb->status < 0)
  422. print_err_status(dev, -1, urb->status);
  423. xfer_bulk = usb_pipebulk(urb->pipe);
  424. if (xfer_bulk) /* bulk */
  425. num_packets = 1;
  426. else /* isoc */
  427. num_packets = urb->number_of_packets;
  428. for (i = 0; i < num_packets; i++) {
  429. if (xfer_bulk) { /* bulk */
  430. usb_data_len = urb->actual_length;
  431. usb_data_pkt = urb->transfer_buffer;
  432. } else { /* isoc */
  433. if (urb->iso_frame_desc[i].status < 0) {
  434. print_err_status(dev, i,
  435. urb->iso_frame_desc[i].status);
  436. if (urb->iso_frame_desc[i].status != -EPROTO)
  437. continue;
  438. }
  439. usb_data_len = urb->iso_frame_desc[i].actual_length;
  440. if (usb_data_len > dev->max_pkt_size) {
  441. em28xx_isocdbg("packet bigger than packet size");
  442. continue;
  443. }
  444. usb_data_pkt = urb->transfer_buffer +
  445. urb->iso_frame_desc[i].offset;
  446. }
  447. if (usb_data_len == 0) {
  448. /* NOTE: happens very often with isoc transfers */
  449. /* em28xx_usbdbg("packet %d is empty",i); - spammy */
  450. continue;
  451. }
  452. if (dev->is_em25xx)
  453. process_frame_data_em25xx(dev,
  454. usb_data_pkt, usb_data_len);
  455. else
  456. process_frame_data_em28xx(dev,
  457. usb_data_pkt, usb_data_len);
  458. }
  459. return 1;
  460. }
  461. static int get_ressource(enum v4l2_buf_type f_type)
  462. {
  463. switch (f_type) {
  464. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  465. return EM28XX_RESOURCE_VIDEO;
  466. case V4L2_BUF_TYPE_VBI_CAPTURE:
  467. return EM28XX_RESOURCE_VBI;
  468. default:
  469. BUG();
  470. return 0;
  471. }
  472. }
  473. /* Usage lock check functions */
  474. static int res_get(struct em28xx *dev, enum v4l2_buf_type f_type)
  475. {
  476. int res_type = get_ressource(f_type);
  477. /* is it free? */
  478. if (dev->resources & res_type) {
  479. /* no, someone else uses it */
  480. return -EBUSY;
  481. }
  482. /* it's free, grab it */
  483. dev->resources |= res_type;
  484. em28xx_videodbg("res: get %d\n", res_type);
  485. return 0;
  486. }
  487. static void res_free(struct em28xx *dev, enum v4l2_buf_type f_type)
  488. {
  489. int res_type = get_ressource(f_type);
  490. dev->resources &= ~res_type;
  491. em28xx_videodbg("res: put %d\n", res_type);
  492. }
  493. /* ------------------------------------------------------------------
  494. Videobuf2 operations
  495. ------------------------------------------------------------------*/
  496. static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
  497. unsigned int *nbuffers, unsigned int *nplanes,
  498. unsigned int sizes[], void *alloc_ctxs[])
  499. {
  500. struct em28xx *dev = vb2_get_drv_priv(vq);
  501. unsigned long size;
  502. if (fmt)
  503. size = fmt->fmt.pix.sizeimage;
  504. else
  505. size = (dev->width * dev->height * dev->format->depth + 7) >> 3;
  506. if (size == 0)
  507. return -EINVAL;
  508. if (0 == *nbuffers)
  509. *nbuffers = 32;
  510. *nplanes = 1;
  511. sizes[0] = size;
  512. return 0;
  513. }
  514. static int
  515. buffer_prepare(struct vb2_buffer *vb)
  516. {
  517. struct em28xx *dev = vb2_get_drv_priv(vb->vb2_queue);
  518. struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
  519. unsigned long size;
  520. em28xx_videodbg("%s, field=%d\n", __func__, vb->v4l2_buf.field);
  521. size = (dev->width * dev->height * dev->format->depth + 7) >> 3;
  522. if (vb2_plane_size(vb, 0) < size) {
  523. em28xx_videodbg("%s data will not fit into plane (%lu < %lu)\n",
  524. __func__, vb2_plane_size(vb, 0), size);
  525. return -EINVAL;
  526. }
  527. vb2_set_plane_payload(&buf->vb, 0, size);
  528. return 0;
  529. }
  530. int em28xx_start_analog_streaming(struct vb2_queue *vq, unsigned int count)
  531. {
  532. struct em28xx *dev = vb2_get_drv_priv(vq);
  533. struct v4l2_frequency f;
  534. int rc = 0;
  535. em28xx_videodbg("%s\n", __func__);
  536. /* Make sure streaming is not already in progress for this type
  537. of filehandle (e.g. video, vbi) */
  538. rc = res_get(dev, vq->type);
  539. if (rc)
  540. return rc;
  541. if (dev->streaming_users++ == 0) {
  542. /* First active streaming user, so allocate all the URBs */
  543. /* Allocate the USB bandwidth */
  544. em28xx_set_alternate(dev);
  545. /* Needed, since GPIO might have disabled power of
  546. some i2c device
  547. */
  548. em28xx_wake_i2c(dev);
  549. dev->capture_type = -1;
  550. rc = em28xx_init_usb_xfer(dev, EM28XX_ANALOG_MODE,
  551. dev->analog_xfer_bulk,
  552. EM28XX_NUM_BUFS,
  553. dev->max_pkt_size,
  554. dev->packet_multiplier,
  555. em28xx_urb_data_copy);
  556. if (rc < 0)
  557. goto fail;
  558. /*
  559. * djh: it's not clear whether this code is still needed. I'm
  560. * leaving it in here for now entirely out of concern for
  561. * backward compatibility (the old code did it)
  562. */
  563. /* Ask tuner to go to analog or radio mode */
  564. memset(&f, 0, sizeof(f));
  565. f.frequency = dev->ctl_freq;
  566. if (vq->owner && vq->owner->vdev->vfl_type == VFL_TYPE_RADIO)
  567. f.type = V4L2_TUNER_RADIO;
  568. else
  569. f.type = V4L2_TUNER_ANALOG_TV;
  570. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
  571. }
  572. fail:
  573. return rc;
  574. }
  575. static int em28xx_stop_streaming(struct vb2_queue *vq)
  576. {
  577. struct em28xx *dev = vb2_get_drv_priv(vq);
  578. struct em28xx_dmaqueue *vidq = &dev->vidq;
  579. unsigned long flags = 0;
  580. em28xx_videodbg("%s\n", __func__);
  581. res_free(dev, vq->type);
  582. if (dev->streaming_users-- == 1) {
  583. /* Last active user, so shutdown all the URBS */
  584. em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
  585. }
  586. spin_lock_irqsave(&dev->slock, flags);
  587. while (!list_empty(&vidq->active)) {
  588. struct em28xx_buffer *buf;
  589. buf = list_entry(vidq->active.next, struct em28xx_buffer, list);
  590. list_del(&buf->list);
  591. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  592. }
  593. dev->usb_ctl.vid_buf = NULL;
  594. spin_unlock_irqrestore(&dev->slock, flags);
  595. return 0;
  596. }
  597. int em28xx_stop_vbi_streaming(struct vb2_queue *vq)
  598. {
  599. struct em28xx *dev = vb2_get_drv_priv(vq);
  600. struct em28xx_dmaqueue *vbiq = &dev->vbiq;
  601. unsigned long flags = 0;
  602. em28xx_videodbg("%s\n", __func__);
  603. res_free(dev, vq->type);
  604. if (dev->streaming_users-- == 1) {
  605. /* Last active user, so shutdown all the URBS */
  606. em28xx_uninit_usb_xfer(dev, EM28XX_ANALOG_MODE);
  607. }
  608. spin_lock_irqsave(&dev->slock, flags);
  609. while (!list_empty(&vbiq->active)) {
  610. struct em28xx_buffer *buf;
  611. buf = list_entry(vbiq->active.next, struct em28xx_buffer, list);
  612. list_del(&buf->list);
  613. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  614. }
  615. dev->usb_ctl.vbi_buf = NULL;
  616. spin_unlock_irqrestore(&dev->slock, flags);
  617. return 0;
  618. }
  619. static void
  620. buffer_queue(struct vb2_buffer *vb)
  621. {
  622. struct em28xx *dev = vb2_get_drv_priv(vb->vb2_queue);
  623. struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
  624. struct em28xx_dmaqueue *vidq = &dev->vidq;
  625. unsigned long flags = 0;
  626. em28xx_videodbg("%s\n", __func__);
  627. buf->mem = vb2_plane_vaddr(vb, 0);
  628. buf->length = vb2_plane_size(vb, 0);
  629. spin_lock_irqsave(&dev->slock, flags);
  630. list_add_tail(&buf->list, &vidq->active);
  631. spin_unlock_irqrestore(&dev->slock, flags);
  632. }
  633. static struct vb2_ops em28xx_video_qops = {
  634. .queue_setup = queue_setup,
  635. .buf_prepare = buffer_prepare,
  636. .buf_queue = buffer_queue,
  637. .start_streaming = em28xx_start_analog_streaming,
  638. .stop_streaming = em28xx_stop_streaming,
  639. .wait_prepare = vb2_ops_wait_prepare,
  640. .wait_finish = vb2_ops_wait_finish,
  641. };
  642. int em28xx_vb2_setup(struct em28xx *dev)
  643. {
  644. int rc;
  645. struct vb2_queue *q;
  646. /* Setup Videobuf2 for Video capture */
  647. q = &dev->vb_vidq;
  648. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  649. q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
  650. q->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  651. q->drv_priv = dev;
  652. q->buf_struct_size = sizeof(struct em28xx_buffer);
  653. q->ops = &em28xx_video_qops;
  654. q->mem_ops = &vb2_vmalloc_memops;
  655. rc = vb2_queue_init(q);
  656. if (rc < 0)
  657. return rc;
  658. /* Setup Videobuf2 for VBI capture */
  659. q = &dev->vb_vbiq;
  660. q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
  661. q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR;
  662. q->timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  663. q->drv_priv = dev;
  664. q->buf_struct_size = sizeof(struct em28xx_buffer);
  665. q->ops = &em28xx_vbi_qops;
  666. q->mem_ops = &vb2_vmalloc_memops;
  667. rc = vb2_queue_init(q);
  668. if (rc < 0)
  669. return rc;
  670. return 0;
  671. }
  672. /********************* v4l2 interface **************************************/
  673. static void video_mux(struct em28xx *dev, int index)
  674. {
  675. dev->ctl_input = index;
  676. dev->ctl_ainput = INPUT(index)->amux;
  677. dev->ctl_aoutput = INPUT(index)->aout;
  678. if (!dev->ctl_aoutput)
  679. dev->ctl_aoutput = EM28XX_AOUT_MASTER;
  680. v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
  681. INPUT(index)->vmux, 0, 0);
  682. if (dev->board.has_msp34xx) {
  683. if (dev->i2s_speed) {
  684. v4l2_device_call_all(&dev->v4l2_dev, 0, audio,
  685. s_i2s_clock_freq, dev->i2s_speed);
  686. }
  687. /* Note: this is msp3400 specific */
  688. v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
  689. dev->ctl_ainput, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
  690. }
  691. if (dev->board.adecoder != EM28XX_NOADECODER) {
  692. v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
  693. dev->ctl_ainput, dev->ctl_aoutput, 0);
  694. }
  695. em28xx_audio_analog_set(dev);
  696. }
  697. void em28xx_ctrl_notify(struct v4l2_ctrl *ctrl, void *priv)
  698. {
  699. struct em28xx *dev = priv;
  700. /*
  701. * In the case of non-AC97 volume controls, we still need
  702. * to do some setups at em28xx, in order to mute/unmute
  703. * and to adjust audio volume. However, the value ranges
  704. * should be checked by the corresponding V4L subdriver.
  705. */
  706. switch (ctrl->id) {
  707. case V4L2_CID_AUDIO_MUTE:
  708. dev->mute = ctrl->val;
  709. em28xx_audio_analog_set(dev);
  710. break;
  711. case V4L2_CID_AUDIO_VOLUME:
  712. dev->volume = ctrl->val;
  713. em28xx_audio_analog_set(dev);
  714. break;
  715. }
  716. }
  717. static int em28xx_s_ctrl(struct v4l2_ctrl *ctrl)
  718. {
  719. struct em28xx *dev = container_of(ctrl->handler, struct em28xx, ctrl_handler);
  720. int ret = -EINVAL;
  721. switch (ctrl->id) {
  722. case V4L2_CID_AUDIO_MUTE:
  723. dev->mute = ctrl->val;
  724. ret = em28xx_audio_analog_set(dev);
  725. break;
  726. case V4L2_CID_AUDIO_VOLUME:
  727. dev->volume = ctrl->val;
  728. ret = em28xx_audio_analog_set(dev);
  729. break;
  730. case V4L2_CID_CONTRAST:
  731. ret = em28xx_write_reg(dev, EM28XX_R20_YGAIN, ctrl->val);
  732. break;
  733. case V4L2_CID_BRIGHTNESS:
  734. ret = em28xx_write_reg(dev, EM28XX_R21_YOFFSET, ctrl->val);
  735. break;
  736. case V4L2_CID_SATURATION:
  737. ret = em28xx_write_reg(dev, EM28XX_R22_UVGAIN, ctrl->val);
  738. break;
  739. case V4L2_CID_BLUE_BALANCE:
  740. ret = em28xx_write_reg(dev, EM28XX_R23_UOFFSET, ctrl->val);
  741. break;
  742. case V4L2_CID_RED_BALANCE:
  743. ret = em28xx_write_reg(dev, EM28XX_R24_VOFFSET, ctrl->val);
  744. break;
  745. case V4L2_CID_SHARPNESS:
  746. ret = em28xx_write_reg(dev, EM28XX_R25_SHARPNESS, ctrl->val);
  747. break;
  748. }
  749. return (ret < 0) ? ret : 0;
  750. }
  751. const struct v4l2_ctrl_ops em28xx_ctrl_ops = {
  752. .s_ctrl = em28xx_s_ctrl,
  753. };
  754. static void size_to_scale(struct em28xx *dev,
  755. unsigned int width, unsigned int height,
  756. unsigned int *hscale, unsigned int *vscale)
  757. {
  758. unsigned int maxw = norm_maxw(dev);
  759. unsigned int maxh = norm_maxh(dev);
  760. *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
  761. if (*hscale > EM28XX_HVSCALE_MAX)
  762. *hscale = EM28XX_HVSCALE_MAX;
  763. *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
  764. if (*vscale > EM28XX_HVSCALE_MAX)
  765. *vscale = EM28XX_HVSCALE_MAX;
  766. }
  767. static void scale_to_size(struct em28xx *dev,
  768. unsigned int hscale, unsigned int vscale,
  769. unsigned int *width, unsigned int *height)
  770. {
  771. unsigned int maxw = norm_maxw(dev);
  772. unsigned int maxh = norm_maxh(dev);
  773. *width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
  774. *height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
  775. }
  776. /* ------------------------------------------------------------------
  777. IOCTL vidioc handling
  778. ------------------------------------------------------------------*/
  779. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  780. struct v4l2_format *f)
  781. {
  782. struct em28xx_fh *fh = priv;
  783. struct em28xx *dev = fh->dev;
  784. f->fmt.pix.width = dev->width;
  785. f->fmt.pix.height = dev->height;
  786. f->fmt.pix.pixelformat = dev->format->fourcc;
  787. f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
  788. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
  789. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  790. /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
  791. if (dev->progressive)
  792. f->fmt.pix.field = V4L2_FIELD_NONE;
  793. else
  794. f->fmt.pix.field = dev->interlaced ?
  795. V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
  796. return 0;
  797. }
  798. static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
  799. {
  800. unsigned int i;
  801. for (i = 0; i < ARRAY_SIZE(format); i++)
  802. if (format[i].fourcc == fourcc)
  803. return &format[i];
  804. return NULL;
  805. }
  806. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  807. struct v4l2_format *f)
  808. {
  809. struct em28xx_fh *fh = priv;
  810. struct em28xx *dev = fh->dev;
  811. unsigned int width = f->fmt.pix.width;
  812. unsigned int height = f->fmt.pix.height;
  813. unsigned int maxw = norm_maxw(dev);
  814. unsigned int maxh = norm_maxh(dev);
  815. unsigned int hscale, vscale;
  816. struct em28xx_fmt *fmt;
  817. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  818. if (!fmt) {
  819. em28xx_videodbg("Fourcc format (%08x) invalid.\n",
  820. f->fmt.pix.pixelformat);
  821. return -EINVAL;
  822. }
  823. if (dev->board.is_em2800) {
  824. /* the em2800 can only scale down to 50% */
  825. height = height > (3 * maxh / 4) ? maxh : maxh / 2;
  826. width = width > (3 * maxw / 4) ? maxw : maxw / 2;
  827. /*
  828. * MaxPacketSize for em2800 is too small to capture at full
  829. * resolution use half of maxw as the scaler can only scale
  830. * to 50%
  831. */
  832. if (width == maxw && height == maxh)
  833. width /= 2;
  834. } else {
  835. /* width must even because of the YUYV format
  836. height must be even because of interlacing */
  837. v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh,
  838. 1, 0);
  839. }
  840. size_to_scale(dev, width, height, &hscale, &vscale);
  841. scale_to_size(dev, hscale, vscale, &width, &height);
  842. f->fmt.pix.width = width;
  843. f->fmt.pix.height = height;
  844. f->fmt.pix.pixelformat = fmt->fourcc;
  845. f->fmt.pix.bytesperline = (width * fmt->depth + 7) >> 3;
  846. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
  847. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  848. if (dev->progressive)
  849. f->fmt.pix.field = V4L2_FIELD_NONE;
  850. else
  851. f->fmt.pix.field = dev->interlaced ?
  852. V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
  853. return 0;
  854. }
  855. static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
  856. unsigned width, unsigned height)
  857. {
  858. struct em28xx_fmt *fmt;
  859. fmt = format_by_fourcc(fourcc);
  860. if (!fmt)
  861. return -EINVAL;
  862. dev->format = fmt;
  863. dev->width = width;
  864. dev->height = height;
  865. /* set new image size */
  866. size_to_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
  867. em28xx_resolution_set(dev);
  868. return 0;
  869. }
  870. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  871. struct v4l2_format *f)
  872. {
  873. struct em28xx *dev = video_drvdata(file);
  874. if (dev->streaming_users > 0)
  875. return -EBUSY;
  876. vidioc_try_fmt_vid_cap(file, priv, f);
  877. return em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
  878. f->fmt.pix.width, f->fmt.pix.height);
  879. }
  880. static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
  881. {
  882. struct em28xx_fh *fh = priv;
  883. struct em28xx *dev = fh->dev;
  884. *norm = dev->norm;
  885. return 0;
  886. }
  887. static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *norm)
  888. {
  889. struct em28xx_fh *fh = priv;
  890. struct em28xx *dev = fh->dev;
  891. v4l2_device_call_all(&dev->v4l2_dev, 0, video, querystd, norm);
  892. return 0;
  893. }
  894. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
  895. {
  896. struct em28xx_fh *fh = priv;
  897. struct em28xx *dev = fh->dev;
  898. struct v4l2_format f;
  899. if (norm == dev->norm)
  900. return 0;
  901. if (dev->streaming_users > 0)
  902. return -EBUSY;
  903. dev->norm = norm;
  904. /* Adjusts width/height, if needed */
  905. f.fmt.pix.width = 720;
  906. f.fmt.pix.height = (norm & V4L2_STD_525_60) ? 480 : 576;
  907. vidioc_try_fmt_vid_cap(file, priv, &f);
  908. /* set new image size */
  909. dev->width = f.fmt.pix.width;
  910. dev->height = f.fmt.pix.height;
  911. size_to_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
  912. em28xx_resolution_set(dev);
  913. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
  914. return 0;
  915. }
  916. static int vidioc_g_parm(struct file *file, void *priv,
  917. struct v4l2_streamparm *p)
  918. {
  919. struct em28xx_fh *fh = priv;
  920. struct em28xx *dev = fh->dev;
  921. int rc = 0;
  922. p->parm.capture.readbuffers = EM28XX_MIN_BUF;
  923. if (dev->board.is_webcam)
  924. rc = v4l2_device_call_until_err(&dev->v4l2_dev, 0,
  925. video, g_parm, p);
  926. else
  927. v4l2_video_std_frame_period(dev->norm,
  928. &p->parm.capture.timeperframe);
  929. return rc;
  930. }
  931. static int vidioc_s_parm(struct file *file, void *priv,
  932. struct v4l2_streamparm *p)
  933. {
  934. struct em28xx_fh *fh = priv;
  935. struct em28xx *dev = fh->dev;
  936. p->parm.capture.readbuffers = EM28XX_MIN_BUF;
  937. return v4l2_device_call_until_err(&dev->v4l2_dev, 0, video, s_parm, p);
  938. }
  939. static const char *iname[] = {
  940. [EM28XX_VMUX_COMPOSITE1] = "Composite1",
  941. [EM28XX_VMUX_COMPOSITE2] = "Composite2",
  942. [EM28XX_VMUX_COMPOSITE3] = "Composite3",
  943. [EM28XX_VMUX_COMPOSITE4] = "Composite4",
  944. [EM28XX_VMUX_SVIDEO] = "S-Video",
  945. [EM28XX_VMUX_TELEVISION] = "Television",
  946. [EM28XX_VMUX_CABLE] = "Cable TV",
  947. [EM28XX_VMUX_DVB] = "DVB",
  948. [EM28XX_VMUX_DEBUG] = "for debug only",
  949. };
  950. static int vidioc_enum_input(struct file *file, void *priv,
  951. struct v4l2_input *i)
  952. {
  953. struct em28xx_fh *fh = priv;
  954. struct em28xx *dev = fh->dev;
  955. unsigned int n;
  956. n = i->index;
  957. if (n >= MAX_EM28XX_INPUT)
  958. return -EINVAL;
  959. if (0 == INPUT(n)->type)
  960. return -EINVAL;
  961. i->index = n;
  962. i->type = V4L2_INPUT_TYPE_CAMERA;
  963. strcpy(i->name, iname[INPUT(n)->type]);
  964. if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
  965. (EM28XX_VMUX_CABLE == INPUT(n)->type))
  966. i->type = V4L2_INPUT_TYPE_TUNER;
  967. i->std = dev->vdev->tvnorms;
  968. /* webcams do not have the STD API */
  969. if (dev->board.is_webcam)
  970. i->capabilities = 0;
  971. return 0;
  972. }
  973. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  974. {
  975. struct em28xx_fh *fh = priv;
  976. struct em28xx *dev = fh->dev;
  977. *i = dev->ctl_input;
  978. return 0;
  979. }
  980. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  981. {
  982. struct em28xx_fh *fh = priv;
  983. struct em28xx *dev = fh->dev;
  984. if (i >= MAX_EM28XX_INPUT)
  985. return -EINVAL;
  986. if (0 == INPUT(i)->type)
  987. return -EINVAL;
  988. video_mux(dev, i);
  989. return 0;
  990. }
  991. static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  992. {
  993. struct em28xx_fh *fh = priv;
  994. struct em28xx *dev = fh->dev;
  995. switch (a->index) {
  996. case EM28XX_AMUX_VIDEO:
  997. strcpy(a->name, "Television");
  998. break;
  999. case EM28XX_AMUX_LINE_IN:
  1000. strcpy(a->name, "Line In");
  1001. break;
  1002. case EM28XX_AMUX_VIDEO2:
  1003. strcpy(a->name, "Television alt");
  1004. break;
  1005. case EM28XX_AMUX_PHONE:
  1006. strcpy(a->name, "Phone");
  1007. break;
  1008. case EM28XX_AMUX_MIC:
  1009. strcpy(a->name, "Mic");
  1010. break;
  1011. case EM28XX_AMUX_CD:
  1012. strcpy(a->name, "CD");
  1013. break;
  1014. case EM28XX_AMUX_AUX:
  1015. strcpy(a->name, "Aux");
  1016. break;
  1017. case EM28XX_AMUX_PCM_OUT:
  1018. strcpy(a->name, "PCM");
  1019. break;
  1020. default:
  1021. return -EINVAL;
  1022. }
  1023. a->index = dev->ctl_ainput;
  1024. a->capability = V4L2_AUDCAP_STEREO;
  1025. return 0;
  1026. }
  1027. static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
  1028. {
  1029. struct em28xx_fh *fh = priv;
  1030. struct em28xx *dev = fh->dev;
  1031. if (a->index >= MAX_EM28XX_INPUT)
  1032. return -EINVAL;
  1033. if (0 == INPUT(a->index)->type)
  1034. return -EINVAL;
  1035. dev->ctl_ainput = INPUT(a->index)->amux;
  1036. dev->ctl_aoutput = INPUT(a->index)->aout;
  1037. if (!dev->ctl_aoutput)
  1038. dev->ctl_aoutput = EM28XX_AOUT_MASTER;
  1039. return 0;
  1040. }
  1041. static int vidioc_g_tuner(struct file *file, void *priv,
  1042. struct v4l2_tuner *t)
  1043. {
  1044. struct em28xx_fh *fh = priv;
  1045. struct em28xx *dev = fh->dev;
  1046. if (0 != t->index)
  1047. return -EINVAL;
  1048. strcpy(t->name, "Tuner");
  1049. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
  1050. return 0;
  1051. }
  1052. static int vidioc_s_tuner(struct file *file, void *priv,
  1053. const struct v4l2_tuner *t)
  1054. {
  1055. struct em28xx_fh *fh = priv;
  1056. struct em28xx *dev = fh->dev;
  1057. if (0 != t->index)
  1058. return -EINVAL;
  1059. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
  1060. return 0;
  1061. }
  1062. static int vidioc_g_frequency(struct file *file, void *priv,
  1063. struct v4l2_frequency *f)
  1064. {
  1065. struct em28xx_fh *fh = priv;
  1066. struct em28xx *dev = fh->dev;
  1067. if (0 != f->tuner)
  1068. return -EINVAL;
  1069. f->frequency = dev->ctl_freq;
  1070. return 0;
  1071. }
  1072. static int vidioc_s_frequency(struct file *file, void *priv,
  1073. const struct v4l2_frequency *f)
  1074. {
  1075. struct v4l2_frequency new_freq = *f;
  1076. struct em28xx_fh *fh = priv;
  1077. struct em28xx *dev = fh->dev;
  1078. if (0 != f->tuner)
  1079. return -EINVAL;
  1080. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
  1081. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, &new_freq);
  1082. dev->ctl_freq = new_freq.frequency;
  1083. return 0;
  1084. }
  1085. static int vidioc_g_chip_ident(struct file *file, void *priv,
  1086. struct v4l2_dbg_chip_ident *chip)
  1087. {
  1088. struct em28xx_fh *fh = priv;
  1089. struct em28xx *dev = fh->dev;
  1090. chip->ident = V4L2_IDENT_NONE;
  1091. chip->revision = 0;
  1092. if (chip->match.type == V4L2_CHIP_MATCH_BRIDGE) {
  1093. if (chip->match.addr > 1)
  1094. return -EINVAL;
  1095. return 0;
  1096. }
  1097. if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
  1098. chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
  1099. return -EINVAL;
  1100. v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
  1101. return 0;
  1102. }
  1103. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1104. static int vidioc_g_chip_info(struct file *file, void *priv,
  1105. struct v4l2_dbg_chip_info *chip)
  1106. {
  1107. struct em28xx_fh *fh = priv;
  1108. struct em28xx *dev = fh->dev;
  1109. if (chip->match.addr > 1)
  1110. return -EINVAL;
  1111. if (chip->match.addr == 1)
  1112. strlcpy(chip->name, "ac97", sizeof(chip->name));
  1113. else
  1114. strlcpy(chip->name, dev->v4l2_dev.name, sizeof(chip->name));
  1115. return 0;
  1116. }
  1117. static int em28xx_reg_len(int reg)
  1118. {
  1119. switch (reg) {
  1120. case EM28XX_R40_AC97LSB:
  1121. case EM28XX_R30_HSCALELOW:
  1122. case EM28XX_R32_VSCALELOW:
  1123. return 2;
  1124. default:
  1125. return 1;
  1126. }
  1127. }
  1128. static int vidioc_g_register(struct file *file, void *priv,
  1129. struct v4l2_dbg_register *reg)
  1130. {
  1131. struct em28xx_fh *fh = priv;
  1132. struct em28xx *dev = fh->dev;
  1133. int ret;
  1134. switch (reg->match.type) {
  1135. case V4L2_CHIP_MATCH_BRIDGE:
  1136. if (reg->match.addr > 1)
  1137. return -EINVAL;
  1138. if (!reg->match.addr)
  1139. break;
  1140. /* fall-through */
  1141. case V4L2_CHIP_MATCH_AC97:
  1142. ret = em28xx_read_ac97(dev, reg->reg);
  1143. if (ret < 0)
  1144. return ret;
  1145. reg->val = ret;
  1146. reg->size = 1;
  1147. return 0;
  1148. case V4L2_CHIP_MATCH_I2C_DRIVER:
  1149. v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
  1150. return 0;
  1151. case V4L2_CHIP_MATCH_I2C_ADDR:
  1152. /* TODO: is this correct? */
  1153. v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
  1154. return 0;
  1155. default:
  1156. return -EINVAL;
  1157. }
  1158. /* Match host */
  1159. reg->size = em28xx_reg_len(reg->reg);
  1160. if (reg->size == 1) {
  1161. ret = em28xx_read_reg(dev, reg->reg);
  1162. if (ret < 0)
  1163. return ret;
  1164. reg->val = ret;
  1165. } else {
  1166. __le16 val = 0;
  1167. ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
  1168. reg->reg, (char *)&val, 2);
  1169. if (ret < 0)
  1170. return ret;
  1171. reg->val = le16_to_cpu(val);
  1172. }
  1173. return 0;
  1174. }
  1175. static int vidioc_s_register(struct file *file, void *priv,
  1176. const struct v4l2_dbg_register *reg)
  1177. {
  1178. struct em28xx_fh *fh = priv;
  1179. struct em28xx *dev = fh->dev;
  1180. __le16 buf;
  1181. switch (reg->match.type) {
  1182. case V4L2_CHIP_MATCH_BRIDGE:
  1183. if (reg->match.addr > 1)
  1184. return -EINVAL;
  1185. if (!reg->match.addr)
  1186. break;
  1187. /* fall-through */
  1188. case V4L2_CHIP_MATCH_AC97:
  1189. return em28xx_write_ac97(dev, reg->reg, reg->val);
  1190. case V4L2_CHIP_MATCH_I2C_DRIVER:
  1191. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
  1192. return 0;
  1193. case V4L2_CHIP_MATCH_I2C_ADDR:
  1194. /* TODO: is this correct? */
  1195. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
  1196. return 0;
  1197. default:
  1198. return -EINVAL;
  1199. }
  1200. /* Match host */
  1201. buf = cpu_to_le16(reg->val);
  1202. return em28xx_write_regs(dev, reg->reg, (char *)&buf,
  1203. em28xx_reg_len(reg->reg));
  1204. }
  1205. #endif
  1206. static int vidioc_querycap(struct file *file, void *priv,
  1207. struct v4l2_capability *cap)
  1208. {
  1209. struct video_device *vdev = video_devdata(file);
  1210. struct em28xx_fh *fh = priv;
  1211. struct em28xx *dev = fh->dev;
  1212. strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
  1213. strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
  1214. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  1215. if (vdev->vfl_type == VFL_TYPE_GRABBER)
  1216. cap->device_caps = V4L2_CAP_READWRITE |
  1217. V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  1218. else if (vdev->vfl_type == VFL_TYPE_RADIO)
  1219. cap->device_caps = V4L2_CAP_RADIO;
  1220. else
  1221. cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_VBI_CAPTURE;
  1222. if (dev->audio_mode.has_audio)
  1223. cap->device_caps |= V4L2_CAP_AUDIO;
  1224. if (dev->tuner_type != TUNER_ABSENT)
  1225. cap->device_caps |= V4L2_CAP_TUNER;
  1226. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS |
  1227. V4L2_CAP_READWRITE | V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
  1228. if (dev->vbi_dev)
  1229. cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
  1230. if (dev->radio_dev)
  1231. cap->capabilities |= V4L2_CAP_RADIO;
  1232. return 0;
  1233. }
  1234. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  1235. struct v4l2_fmtdesc *f)
  1236. {
  1237. if (unlikely(f->index >= ARRAY_SIZE(format)))
  1238. return -EINVAL;
  1239. strlcpy(f->description, format[f->index].name, sizeof(f->description));
  1240. f->pixelformat = format[f->index].fourcc;
  1241. return 0;
  1242. }
  1243. static int vidioc_enum_framesizes(struct file *file, void *priv,
  1244. struct v4l2_frmsizeenum *fsize)
  1245. {
  1246. struct em28xx_fh *fh = priv;
  1247. struct em28xx *dev = fh->dev;
  1248. struct em28xx_fmt *fmt;
  1249. unsigned int maxw = norm_maxw(dev);
  1250. unsigned int maxh = norm_maxh(dev);
  1251. fmt = format_by_fourcc(fsize->pixel_format);
  1252. if (!fmt) {
  1253. em28xx_videodbg("Fourcc format (%08x) invalid.\n",
  1254. fsize->pixel_format);
  1255. return -EINVAL;
  1256. }
  1257. if (dev->board.is_em2800) {
  1258. if (fsize->index > 1)
  1259. return -EINVAL;
  1260. fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  1261. fsize->discrete.width = maxw / (1 + fsize->index);
  1262. fsize->discrete.height = maxh / (1 + fsize->index);
  1263. return 0;
  1264. }
  1265. if (fsize->index != 0)
  1266. return -EINVAL;
  1267. /* Report a continuous range */
  1268. fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
  1269. scale_to_size(dev, EM28XX_HVSCALE_MAX, EM28XX_HVSCALE_MAX,
  1270. &fsize->stepwise.min_width, &fsize->stepwise.min_height);
  1271. if (fsize->stepwise.min_width < 48)
  1272. fsize->stepwise.min_width = 48;
  1273. if (fsize->stepwise.min_height < 38)
  1274. fsize->stepwise.min_height = 38;
  1275. fsize->stepwise.max_width = maxw;
  1276. fsize->stepwise.max_height = maxh;
  1277. fsize->stepwise.step_width = 1;
  1278. fsize->stepwise.step_height = 1;
  1279. return 0;
  1280. }
  1281. /* RAW VBI ioctls */
  1282. static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
  1283. struct v4l2_format *format)
  1284. {
  1285. struct em28xx_fh *fh = priv;
  1286. struct em28xx *dev = fh->dev;
  1287. format->fmt.vbi.samples_per_line = dev->vbi_width;
  1288. format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  1289. format->fmt.vbi.offset = 0;
  1290. format->fmt.vbi.flags = 0;
  1291. format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
  1292. format->fmt.vbi.count[0] = dev->vbi_height;
  1293. format->fmt.vbi.count[1] = dev->vbi_height;
  1294. memset(format->fmt.vbi.reserved, 0, sizeof(format->fmt.vbi.reserved));
  1295. /* Varies by video standard (NTSC, PAL, etc.) */
  1296. if (dev->norm & V4L2_STD_525_60) {
  1297. /* NTSC */
  1298. format->fmt.vbi.start[0] = 10;
  1299. format->fmt.vbi.start[1] = 273;
  1300. } else if (dev->norm & V4L2_STD_625_50) {
  1301. /* PAL */
  1302. format->fmt.vbi.start[0] = 6;
  1303. format->fmt.vbi.start[1] = 318;
  1304. }
  1305. return 0;
  1306. }
  1307. /* ----------------------------------------------------------- */
  1308. /* RADIO ESPECIFIC IOCTLS */
  1309. /* ----------------------------------------------------------- */
  1310. static int radio_g_tuner(struct file *file, void *priv,
  1311. struct v4l2_tuner *t)
  1312. {
  1313. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1314. if (unlikely(t->index > 0))
  1315. return -EINVAL;
  1316. strcpy(t->name, "Radio");
  1317. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
  1318. return 0;
  1319. }
  1320. static int radio_s_tuner(struct file *file, void *priv,
  1321. const struct v4l2_tuner *t)
  1322. {
  1323. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1324. if (0 != t->index)
  1325. return -EINVAL;
  1326. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
  1327. return 0;
  1328. }
  1329. /*
  1330. * em28xx_v4l2_open()
  1331. * inits the device and starts isoc transfer
  1332. */
  1333. static int em28xx_v4l2_open(struct file *filp)
  1334. {
  1335. struct video_device *vdev = video_devdata(filp);
  1336. struct em28xx *dev = video_drvdata(filp);
  1337. enum v4l2_buf_type fh_type = 0;
  1338. struct em28xx_fh *fh;
  1339. switch (vdev->vfl_type) {
  1340. case VFL_TYPE_GRABBER:
  1341. fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1342. break;
  1343. case VFL_TYPE_VBI:
  1344. fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
  1345. break;
  1346. }
  1347. em28xx_videodbg("open dev=%s type=%s users=%d\n",
  1348. video_device_node_name(vdev), v4l2_type_names[fh_type],
  1349. dev->users);
  1350. if (mutex_lock_interruptible(&dev->lock))
  1351. return -ERESTARTSYS;
  1352. fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
  1353. if (!fh) {
  1354. em28xx_errdev("em28xx-video.c: Out of memory?!\n");
  1355. mutex_unlock(&dev->lock);
  1356. return -ENOMEM;
  1357. }
  1358. v4l2_fh_init(&fh->fh, vdev);
  1359. fh->dev = dev;
  1360. fh->type = fh_type;
  1361. filp->private_data = fh;
  1362. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
  1363. em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
  1364. em28xx_resolution_set(dev);
  1365. /* Needed, since GPIO might have disabled power of
  1366. some i2c device
  1367. */
  1368. em28xx_wake_i2c(dev);
  1369. }
  1370. if (vdev->vfl_type == VFL_TYPE_RADIO) {
  1371. em28xx_videodbg("video_open: setting radio device\n");
  1372. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
  1373. }
  1374. dev->users++;
  1375. mutex_unlock(&dev->lock);
  1376. v4l2_fh_add(&fh->fh);
  1377. return 0;
  1378. }
  1379. /*
  1380. * em28xx_realease_resources()
  1381. * unregisters the v4l2,i2c and usb devices
  1382. * called when the device gets disconected or at module unload
  1383. */
  1384. void em28xx_release_analog_resources(struct em28xx *dev)
  1385. {
  1386. /*FIXME: I2C IR should be disconnected */
  1387. if (dev->radio_dev) {
  1388. if (video_is_registered(dev->radio_dev))
  1389. video_unregister_device(dev->radio_dev);
  1390. else
  1391. video_device_release(dev->radio_dev);
  1392. dev->radio_dev = NULL;
  1393. }
  1394. if (dev->vbi_dev) {
  1395. em28xx_info("V4L2 device %s deregistered\n",
  1396. video_device_node_name(dev->vbi_dev));
  1397. if (video_is_registered(dev->vbi_dev))
  1398. video_unregister_device(dev->vbi_dev);
  1399. else
  1400. video_device_release(dev->vbi_dev);
  1401. dev->vbi_dev = NULL;
  1402. }
  1403. if (dev->vdev) {
  1404. em28xx_info("V4L2 device %s deregistered\n",
  1405. video_device_node_name(dev->vdev));
  1406. if (video_is_registered(dev->vdev))
  1407. video_unregister_device(dev->vdev);
  1408. else
  1409. video_device_release(dev->vdev);
  1410. dev->vdev = NULL;
  1411. }
  1412. }
  1413. /*
  1414. * em28xx_v4l2_close()
  1415. * stops streaming and deallocates all resources allocated by the v4l2
  1416. * calls and ioctls
  1417. */
  1418. static int em28xx_v4l2_close(struct file *filp)
  1419. {
  1420. struct em28xx_fh *fh = filp->private_data;
  1421. struct em28xx *dev = fh->dev;
  1422. int errCode;
  1423. em28xx_videodbg("users=%d\n", dev->users);
  1424. mutex_lock(&dev->lock);
  1425. vb2_fop_release(filp);
  1426. if (dev->users == 1) {
  1427. /* the device is already disconnect,
  1428. free the remaining resources */
  1429. if (dev->disconnected) {
  1430. em28xx_release_resources(dev);
  1431. kfree(dev->alt_max_pkt_size_isoc);
  1432. mutex_unlock(&dev->lock);
  1433. kfree(dev);
  1434. return 0;
  1435. }
  1436. /* Save some power by putting tuner to sleep */
  1437. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
  1438. /* do this before setting alternate! */
  1439. em28xx_set_mode(dev, EM28XX_SUSPEND);
  1440. /* set alternate 0 */
  1441. dev->alt = 0;
  1442. em28xx_videodbg("setting alternate 0\n");
  1443. errCode = usb_set_interface(dev->udev, 0, 0);
  1444. if (errCode < 0) {
  1445. em28xx_errdev("cannot change alternate number to "
  1446. "0 (error=%i)\n", errCode);
  1447. }
  1448. }
  1449. dev->users--;
  1450. mutex_unlock(&dev->lock);
  1451. return 0;
  1452. }
  1453. static const struct v4l2_file_operations em28xx_v4l_fops = {
  1454. .owner = THIS_MODULE,
  1455. .open = em28xx_v4l2_open,
  1456. .release = em28xx_v4l2_close,
  1457. .read = vb2_fop_read,
  1458. .poll = vb2_fop_poll,
  1459. .mmap = vb2_fop_mmap,
  1460. .unlocked_ioctl = video_ioctl2,
  1461. };
  1462. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1463. .vidioc_querycap = vidioc_querycap,
  1464. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1465. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1466. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1467. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1468. .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
  1469. .vidioc_try_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
  1470. .vidioc_s_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
  1471. .vidioc_enum_framesizes = vidioc_enum_framesizes,
  1472. .vidioc_g_audio = vidioc_g_audio,
  1473. .vidioc_s_audio = vidioc_s_audio,
  1474. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  1475. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  1476. .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  1477. .vidioc_querybuf = vb2_ioctl_querybuf,
  1478. .vidioc_qbuf = vb2_ioctl_qbuf,
  1479. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  1480. .vidioc_g_std = vidioc_g_std,
  1481. .vidioc_querystd = vidioc_querystd,
  1482. .vidioc_s_std = vidioc_s_std,
  1483. .vidioc_g_parm = vidioc_g_parm,
  1484. .vidioc_s_parm = vidioc_s_parm,
  1485. .vidioc_enum_input = vidioc_enum_input,
  1486. .vidioc_g_input = vidioc_g_input,
  1487. .vidioc_s_input = vidioc_s_input,
  1488. .vidioc_streamon = vb2_ioctl_streamon,
  1489. .vidioc_streamoff = vb2_ioctl_streamoff,
  1490. .vidioc_g_tuner = vidioc_g_tuner,
  1491. .vidioc_s_tuner = vidioc_s_tuner,
  1492. .vidioc_g_frequency = vidioc_g_frequency,
  1493. .vidioc_s_frequency = vidioc_s_frequency,
  1494. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1495. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1496. .vidioc_g_chip_ident = vidioc_g_chip_ident,
  1497. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1498. .vidioc_g_chip_info = vidioc_g_chip_info,
  1499. .vidioc_g_register = vidioc_g_register,
  1500. .vidioc_s_register = vidioc_s_register,
  1501. #endif
  1502. };
  1503. static const struct video_device em28xx_video_template = {
  1504. .fops = &em28xx_v4l_fops,
  1505. .release = video_device_release_empty,
  1506. .ioctl_ops = &video_ioctl_ops,
  1507. .tvnorms = V4L2_STD_ALL,
  1508. };
  1509. static const struct v4l2_file_operations radio_fops = {
  1510. .owner = THIS_MODULE,
  1511. .open = em28xx_v4l2_open,
  1512. .release = em28xx_v4l2_close,
  1513. .unlocked_ioctl = video_ioctl2,
  1514. };
  1515. static const struct v4l2_ioctl_ops radio_ioctl_ops = {
  1516. .vidioc_querycap = vidioc_querycap,
  1517. .vidioc_g_tuner = radio_g_tuner,
  1518. .vidioc_s_tuner = radio_s_tuner,
  1519. .vidioc_g_frequency = vidioc_g_frequency,
  1520. .vidioc_s_frequency = vidioc_s_frequency,
  1521. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1522. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1523. .vidioc_g_chip_ident = vidioc_g_chip_ident,
  1524. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1525. .vidioc_g_chip_info = vidioc_g_chip_info,
  1526. .vidioc_g_register = vidioc_g_register,
  1527. .vidioc_s_register = vidioc_s_register,
  1528. #endif
  1529. };
  1530. static struct video_device em28xx_radio_template = {
  1531. .name = "em28xx-radio",
  1532. .fops = &radio_fops,
  1533. .ioctl_ops = &radio_ioctl_ops,
  1534. };
  1535. /******************************** usb interface ******************************/
  1536. static struct video_device *em28xx_vdev_init(struct em28xx *dev,
  1537. const struct video_device *template,
  1538. const char *type_name)
  1539. {
  1540. struct video_device *vfd;
  1541. vfd = video_device_alloc();
  1542. if (NULL == vfd)
  1543. return NULL;
  1544. *vfd = *template;
  1545. vfd->v4l2_dev = &dev->v4l2_dev;
  1546. vfd->debug = video_debug;
  1547. vfd->lock = &dev->lock;
  1548. set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
  1549. if (dev->board.is_webcam)
  1550. vfd->tvnorms = 0;
  1551. snprintf(vfd->name, sizeof(vfd->name), "%s %s",
  1552. dev->name, type_name);
  1553. video_set_drvdata(vfd, dev);
  1554. return vfd;
  1555. }
  1556. int em28xx_register_analog_devices(struct em28xx *dev)
  1557. {
  1558. u8 val;
  1559. int ret;
  1560. unsigned int maxw;
  1561. printk(KERN_INFO "%s: v4l2 driver version %s\n",
  1562. dev->name, EM28XX_VERSION);
  1563. /* set default norm */
  1564. dev->norm = V4L2_STD_PAL;
  1565. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
  1566. dev->interlaced = EM28XX_INTERLACED_DEFAULT;
  1567. /* Analog specific initialization */
  1568. dev->format = &format[0];
  1569. maxw = norm_maxw(dev);
  1570. /* MaxPacketSize for em2800 is too small to capture at full resolution
  1571. * use half of maxw as the scaler can only scale to 50% */
  1572. if (dev->board.is_em2800)
  1573. maxw /= 2;
  1574. em28xx_set_video_format(dev, format[0].fourcc,
  1575. maxw, norm_maxh(dev));
  1576. video_mux(dev, 0);
  1577. /* Audio defaults */
  1578. dev->mute = 1;
  1579. dev->volume = 0x1f;
  1580. /* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
  1581. val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
  1582. em28xx_write_reg(dev, EM28XX_R0F_XCLK,
  1583. (EM28XX_XCLK_AUDIO_UNMUTE | val));
  1584. em28xx_set_outfmt(dev);
  1585. em28xx_compression_disable(dev);
  1586. /* Add image controls */
  1587. /* NOTE: at this point, the subdevices are already registered, so bridge
  1588. * controls are only added/enabled when no subdevice provides them */
  1589. if (NULL == v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_CONTRAST))
  1590. v4l2_ctrl_new_std(&dev->ctrl_handler, &em28xx_ctrl_ops,
  1591. V4L2_CID_CONTRAST,
  1592. 0, 0x1f, 1, CONTRAST_DEFAULT);
  1593. if (NULL == v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_BRIGHTNESS))
  1594. v4l2_ctrl_new_std(&dev->ctrl_handler, &em28xx_ctrl_ops,
  1595. V4L2_CID_BRIGHTNESS,
  1596. -0x80, 0x7f, 1, BRIGHTNESS_DEFAULT);
  1597. if (NULL == v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_SATURATION))
  1598. v4l2_ctrl_new_std(&dev->ctrl_handler, &em28xx_ctrl_ops,
  1599. V4L2_CID_SATURATION,
  1600. 0, 0x1f, 1, SATURATION_DEFAULT);
  1601. if (NULL == v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_BLUE_BALANCE))
  1602. v4l2_ctrl_new_std(&dev->ctrl_handler, &em28xx_ctrl_ops,
  1603. V4L2_CID_BLUE_BALANCE,
  1604. -0x30, 0x30, 1, BLUE_BALANCE_DEFAULT);
  1605. if (NULL == v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_RED_BALANCE))
  1606. v4l2_ctrl_new_std(&dev->ctrl_handler, &em28xx_ctrl_ops,
  1607. V4L2_CID_RED_BALANCE,
  1608. -0x30, 0x30, 1, RED_BALANCE_DEFAULT);
  1609. if (NULL == v4l2_ctrl_find(&dev->ctrl_handler, V4L2_CID_SHARPNESS))
  1610. v4l2_ctrl_new_std(&dev->ctrl_handler, &em28xx_ctrl_ops,
  1611. V4L2_CID_SHARPNESS,
  1612. 0, 0x0f, 1, SHARPNESS_DEFAULT);
  1613. /* Reset image controls */
  1614. em28xx_colorlevels_set_default(dev);
  1615. v4l2_ctrl_handler_setup(&dev->ctrl_handler);
  1616. if (dev->ctrl_handler.error)
  1617. return dev->ctrl_handler.error;
  1618. /* allocate and fill video video_device struct */
  1619. dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
  1620. if (!dev->vdev) {
  1621. em28xx_errdev("cannot allocate video_device.\n");
  1622. return -ENODEV;
  1623. }
  1624. dev->vdev->queue = &dev->vb_vidq;
  1625. dev->vdev->queue->lock = &dev->vb_queue_lock;
  1626. /* disable inapplicable ioctls */
  1627. if (dev->board.is_webcam) {
  1628. v4l2_disable_ioctl(dev->vdev, VIDIOC_QUERYSTD);
  1629. v4l2_disable_ioctl(dev->vdev, VIDIOC_G_STD);
  1630. v4l2_disable_ioctl(dev->vdev, VIDIOC_S_STD);
  1631. } else {
  1632. v4l2_disable_ioctl(dev->vdev, VIDIOC_S_PARM);
  1633. }
  1634. if (dev->tuner_type == TUNER_ABSENT) {
  1635. v4l2_disable_ioctl(dev->vdev, VIDIOC_G_TUNER);
  1636. v4l2_disable_ioctl(dev->vdev, VIDIOC_S_TUNER);
  1637. v4l2_disable_ioctl(dev->vdev, VIDIOC_G_FREQUENCY);
  1638. v4l2_disable_ioctl(dev->vdev, VIDIOC_S_FREQUENCY);
  1639. }
  1640. if (!dev->audio_mode.has_audio) {
  1641. v4l2_disable_ioctl(dev->vdev, VIDIOC_G_AUDIO);
  1642. v4l2_disable_ioctl(dev->vdev, VIDIOC_S_AUDIO);
  1643. }
  1644. /* register v4l2 video video_device */
  1645. ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
  1646. video_nr[dev->devno]);
  1647. if (ret) {
  1648. em28xx_errdev("unable to register video device (error=%i).\n",
  1649. ret);
  1650. return ret;
  1651. }
  1652. /* Allocate and fill vbi video_device struct */
  1653. if (em28xx_vbi_supported(dev) == 1) {
  1654. dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template,
  1655. "vbi");
  1656. dev->vbi_dev->queue = &dev->vb_vbiq;
  1657. dev->vbi_dev->queue->lock = &dev->vb_vbi_queue_lock;
  1658. /* disable inapplicable ioctls */
  1659. v4l2_disable_ioctl(dev->vdev, VIDIOC_S_PARM);
  1660. if (dev->tuner_type == TUNER_ABSENT) {
  1661. v4l2_disable_ioctl(dev->vbi_dev, VIDIOC_G_TUNER);
  1662. v4l2_disable_ioctl(dev->vbi_dev, VIDIOC_S_TUNER);
  1663. v4l2_disable_ioctl(dev->vbi_dev, VIDIOC_G_FREQUENCY);
  1664. v4l2_disable_ioctl(dev->vbi_dev, VIDIOC_S_FREQUENCY);
  1665. }
  1666. if (!dev->audio_mode.has_audio) {
  1667. v4l2_disable_ioctl(dev->vbi_dev, VIDIOC_G_AUDIO);
  1668. v4l2_disable_ioctl(dev->vbi_dev, VIDIOC_S_AUDIO);
  1669. }
  1670. /* register v4l2 vbi video_device */
  1671. ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
  1672. vbi_nr[dev->devno]);
  1673. if (ret < 0) {
  1674. em28xx_errdev("unable to register vbi device\n");
  1675. return ret;
  1676. }
  1677. }
  1678. if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
  1679. dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
  1680. "radio");
  1681. if (!dev->radio_dev) {
  1682. em28xx_errdev("cannot allocate video_device.\n");
  1683. return -ENODEV;
  1684. }
  1685. ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
  1686. radio_nr[dev->devno]);
  1687. if (ret < 0) {
  1688. em28xx_errdev("can't register radio device\n");
  1689. return ret;
  1690. }
  1691. em28xx_info("Registered radio device as %s\n",
  1692. video_device_node_name(dev->radio_dev));
  1693. }
  1694. em28xx_info("V4L2 video device registered as %s\n",
  1695. video_device_node_name(dev->vdev));
  1696. if (dev->vbi_dev)
  1697. em28xx_info("V4L2 VBI device registered as %s\n",
  1698. video_device_node_name(dev->vbi_dev));
  1699. return 0;
  1700. }