em28xx-video.c 60 KB

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