em28xx-video.c 60 KB

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