em28xx-video.c 60 KB

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