em28xx-video.c 55 KB

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