em28xx-video.c 51 KB

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