em28xx-video.c 52 KB

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