au0828-video.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050
  1. /*
  2. * Auvitek AU0828 USB Bridge (Analog video support)
  3. *
  4. * Copyright (C) 2009 Devin Heitmueller <dheitmueller@linuxtv.org>
  5. * Copyright (C) 2005-2008 Auvitek International, Ltd.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * As published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. /* Developer Notes:
  18. *
  19. * The hardware scaler supported is unimplemented
  20. * AC97 audio support is unimplemented (only i2s audio mode)
  21. *
  22. */
  23. #include "au0828.h"
  24. #include "au8522.h"
  25. #include <linux/module.h>
  26. #include <linux/slab.h>
  27. #include <linux/init.h>
  28. #include <linux/device.h>
  29. #include <media/v4l2-common.h>
  30. #include <media/v4l2-mc.h>
  31. #include <media/v4l2-ioctl.h>
  32. #include <media/v4l2-event.h>
  33. #include <media/tuner.h>
  34. #include "au0828-reg.h"
  35. static DEFINE_MUTEX(au0828_sysfs_lock);
  36. /* ------------------------------------------------------------------
  37. Videobuf operations
  38. ------------------------------------------------------------------*/
  39. static unsigned int isoc_debug;
  40. module_param(isoc_debug, int, 0644);
  41. MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
  42. #define au0828_isocdbg(fmt, arg...) \
  43. do {\
  44. if (isoc_debug) { \
  45. pr_info("au0828 %s :"fmt, \
  46. __func__ , ##arg); \
  47. } \
  48. } while (0)
  49. static inline void i2c_gate_ctrl(struct au0828_dev *dev, int val)
  50. {
  51. if (dev->dvb.frontend && dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl)
  52. dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl(dev->dvb.frontend, val);
  53. }
  54. static inline void print_err_status(struct au0828_dev *dev,
  55. int packet, int status)
  56. {
  57. char *errmsg = "Unknown";
  58. switch (status) {
  59. case -ENOENT:
  60. errmsg = "unlinked synchronously";
  61. break;
  62. case -ECONNRESET:
  63. errmsg = "unlinked asynchronously";
  64. break;
  65. case -ENOSR:
  66. errmsg = "Buffer error (overrun)";
  67. break;
  68. case -EPIPE:
  69. errmsg = "Stalled (device not responding)";
  70. break;
  71. case -EOVERFLOW:
  72. errmsg = "Babble (bad cable?)";
  73. break;
  74. case -EPROTO:
  75. errmsg = "Bit-stuff error (bad cable?)";
  76. break;
  77. case -EILSEQ:
  78. errmsg = "CRC/Timeout (could be anything)";
  79. break;
  80. case -ETIME:
  81. errmsg = "Device does not respond";
  82. break;
  83. }
  84. if (packet < 0) {
  85. au0828_isocdbg("URB status %d [%s].\n", status, errmsg);
  86. } else {
  87. au0828_isocdbg("URB packet %d, status %d [%s].\n",
  88. packet, status, errmsg);
  89. }
  90. }
  91. static int check_dev(struct au0828_dev *dev)
  92. {
  93. if (test_bit(DEV_DISCONNECTED, &dev->dev_state)) {
  94. pr_info("v4l2 ioctl: device not present\n");
  95. return -ENODEV;
  96. }
  97. if (test_bit(DEV_MISCONFIGURED, &dev->dev_state)) {
  98. pr_info("v4l2 ioctl: device is misconfigured; close and open it again\n");
  99. return -EIO;
  100. }
  101. return 0;
  102. }
  103. /*
  104. * IRQ callback, called by URB callback
  105. */
  106. static void au0828_irq_callback(struct urb *urb)
  107. {
  108. struct au0828_dmaqueue *dma_q = urb->context;
  109. struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vidq);
  110. unsigned long flags = 0;
  111. int i;
  112. switch (urb->status) {
  113. case 0: /* success */
  114. case -ETIMEDOUT: /* NAK */
  115. break;
  116. case -ECONNRESET: /* kill */
  117. case -ENOENT:
  118. case -ESHUTDOWN:
  119. au0828_isocdbg("au0828_irq_callback called: status kill\n");
  120. return;
  121. default: /* unknown error */
  122. au0828_isocdbg("urb completion error %d.\n", urb->status);
  123. break;
  124. }
  125. /* Copy data from URB */
  126. spin_lock_irqsave(&dev->slock, flags);
  127. dev->isoc_ctl.isoc_copy(dev, urb);
  128. spin_unlock_irqrestore(&dev->slock, flags);
  129. /* Reset urb buffers */
  130. for (i = 0; i < urb->number_of_packets; i++) {
  131. urb->iso_frame_desc[i].status = 0;
  132. urb->iso_frame_desc[i].actual_length = 0;
  133. }
  134. urb->status = 0;
  135. urb->status = usb_submit_urb(urb, GFP_ATOMIC);
  136. if (urb->status) {
  137. au0828_isocdbg("urb resubmit failed (error=%i)\n",
  138. urb->status);
  139. }
  140. dev->stream_state = STREAM_ON;
  141. }
  142. /*
  143. * Stop and Deallocate URBs
  144. */
  145. static void au0828_uninit_isoc(struct au0828_dev *dev)
  146. {
  147. struct urb *urb;
  148. int i;
  149. au0828_isocdbg("au0828: called au0828_uninit_isoc\n");
  150. dev->isoc_ctl.nfields = -1;
  151. for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
  152. urb = dev->isoc_ctl.urb[i];
  153. if (urb) {
  154. if (!irqs_disabled())
  155. usb_kill_urb(urb);
  156. else
  157. usb_unlink_urb(urb);
  158. if (dev->isoc_ctl.transfer_buffer[i]) {
  159. usb_free_coherent(dev->usbdev,
  160. urb->transfer_buffer_length,
  161. dev->isoc_ctl.transfer_buffer[i],
  162. urb->transfer_dma);
  163. }
  164. usb_free_urb(urb);
  165. dev->isoc_ctl.urb[i] = NULL;
  166. }
  167. dev->isoc_ctl.transfer_buffer[i] = NULL;
  168. }
  169. kfree(dev->isoc_ctl.urb);
  170. kfree(dev->isoc_ctl.transfer_buffer);
  171. dev->isoc_ctl.urb = NULL;
  172. dev->isoc_ctl.transfer_buffer = NULL;
  173. dev->isoc_ctl.num_bufs = 0;
  174. dev->stream_state = STREAM_OFF;
  175. }
  176. /*
  177. * Allocate URBs and start IRQ
  178. */
  179. static int au0828_init_isoc(struct au0828_dev *dev, int max_packets,
  180. int num_bufs, int max_pkt_size,
  181. int (*isoc_copy) (struct au0828_dev *dev, struct urb *urb))
  182. {
  183. struct au0828_dmaqueue *dma_q = &dev->vidq;
  184. int i;
  185. int sb_size, pipe;
  186. struct urb *urb;
  187. int j, k;
  188. int rc;
  189. au0828_isocdbg("au0828: called au0828_prepare_isoc\n");
  190. dev->isoc_ctl.isoc_copy = isoc_copy;
  191. dev->isoc_ctl.num_bufs = num_bufs;
  192. dev->isoc_ctl.urb = kcalloc(num_bufs, sizeof(void *), GFP_KERNEL);
  193. if (!dev->isoc_ctl.urb) {
  194. au0828_isocdbg("cannot alloc memory for usb buffers\n");
  195. return -ENOMEM;
  196. }
  197. dev->isoc_ctl.transfer_buffer = kcalloc(num_bufs, sizeof(void *),
  198. GFP_KERNEL);
  199. if (!dev->isoc_ctl.transfer_buffer) {
  200. au0828_isocdbg("cannot allocate memory for usb transfer\n");
  201. kfree(dev->isoc_ctl.urb);
  202. return -ENOMEM;
  203. }
  204. dev->isoc_ctl.max_pkt_size = max_pkt_size;
  205. dev->isoc_ctl.buf = NULL;
  206. sb_size = max_packets * dev->isoc_ctl.max_pkt_size;
  207. /* allocate urbs and transfer buffers */
  208. for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
  209. urb = usb_alloc_urb(max_packets, GFP_KERNEL);
  210. if (!urb) {
  211. au0828_uninit_isoc(dev);
  212. return -ENOMEM;
  213. }
  214. dev->isoc_ctl.urb[i] = urb;
  215. dev->isoc_ctl.transfer_buffer[i] = usb_alloc_coherent(dev->usbdev,
  216. sb_size, GFP_KERNEL, &urb->transfer_dma);
  217. if (!dev->isoc_ctl.transfer_buffer[i]) {
  218. printk("unable to allocate %i bytes for transfer buffer %i%s\n",
  219. sb_size, i,
  220. in_interrupt() ? " while in int" : "");
  221. au0828_uninit_isoc(dev);
  222. return -ENOMEM;
  223. }
  224. memset(dev->isoc_ctl.transfer_buffer[i], 0, sb_size);
  225. pipe = usb_rcvisocpipe(dev->usbdev,
  226. dev->isoc_in_endpointaddr),
  227. usb_fill_int_urb(urb, dev->usbdev, pipe,
  228. dev->isoc_ctl.transfer_buffer[i], sb_size,
  229. au0828_irq_callback, dma_q, 1);
  230. urb->number_of_packets = max_packets;
  231. urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
  232. k = 0;
  233. for (j = 0; j < max_packets; j++) {
  234. urb->iso_frame_desc[j].offset = k;
  235. urb->iso_frame_desc[j].length =
  236. dev->isoc_ctl.max_pkt_size;
  237. k += dev->isoc_ctl.max_pkt_size;
  238. }
  239. }
  240. /* submit urbs and enables IRQ */
  241. for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
  242. rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
  243. if (rc) {
  244. au0828_isocdbg("submit of urb %i failed (error=%i)\n",
  245. i, rc);
  246. au0828_uninit_isoc(dev);
  247. return rc;
  248. }
  249. }
  250. return 0;
  251. }
  252. /*
  253. * Announces that a buffer were filled and request the next
  254. */
  255. static inline void buffer_filled(struct au0828_dev *dev,
  256. struct au0828_dmaqueue *dma_q,
  257. struct au0828_buffer *buf)
  258. {
  259. struct vb2_v4l2_buffer *vb = &buf->vb;
  260. struct vb2_queue *q = vb->vb2_buf.vb2_queue;
  261. /* Advice that buffer was filled */
  262. au0828_isocdbg("[%p/%d] wakeup\n", buf, buf->top_field);
  263. if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  264. vb->sequence = dev->frame_count++;
  265. else
  266. vb->sequence = dev->vbi_frame_count++;
  267. vb->field = V4L2_FIELD_INTERLACED;
  268. vb->vb2_buf.timestamp = ktime_get_ns();
  269. vb2_buffer_done(&vb->vb2_buf, VB2_BUF_STATE_DONE);
  270. }
  271. /*
  272. * Identify the buffer header type and properly handles
  273. */
  274. static void au0828_copy_video(struct au0828_dev *dev,
  275. struct au0828_dmaqueue *dma_q,
  276. struct au0828_buffer *buf,
  277. unsigned char *p,
  278. unsigned char *outp, unsigned long len)
  279. {
  280. void *fieldstart, *startwrite, *startread;
  281. int linesdone, currlinedone, offset, lencopy, remain;
  282. int bytesperline = dev->width << 1; /* Assumes 16-bit depth @@@@ */
  283. if (len == 0)
  284. return;
  285. if (dma_q->pos + len > buf->length)
  286. len = buf->length - dma_q->pos;
  287. startread = p;
  288. remain = len;
  289. /* Interlaces frame */
  290. if (buf->top_field)
  291. fieldstart = outp;
  292. else
  293. fieldstart = outp + bytesperline;
  294. linesdone = dma_q->pos / bytesperline;
  295. currlinedone = dma_q->pos % bytesperline;
  296. offset = linesdone * bytesperline * 2 + currlinedone;
  297. startwrite = fieldstart + offset;
  298. lencopy = bytesperline - currlinedone;
  299. lencopy = lencopy > remain ? remain : lencopy;
  300. if ((char *)startwrite + lencopy > (char *)outp + buf->length) {
  301. au0828_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
  302. ((char *)startwrite + lencopy) -
  303. ((char *)outp + buf->length));
  304. remain = (char *)outp + buf->length - (char *)startwrite;
  305. lencopy = remain;
  306. }
  307. if (lencopy <= 0)
  308. return;
  309. memcpy(startwrite, startread, lencopy);
  310. remain -= lencopy;
  311. while (remain > 0) {
  312. startwrite += lencopy + bytesperline;
  313. startread += lencopy;
  314. if (bytesperline > remain)
  315. lencopy = remain;
  316. else
  317. lencopy = bytesperline;
  318. if ((char *)startwrite + lencopy > (char *)outp +
  319. buf->length) {
  320. au0828_isocdbg("Overflow %zi bytes past buf end (2)\n",
  321. ((char *)startwrite + lencopy) -
  322. ((char *)outp + buf->length));
  323. lencopy = remain = (char *)outp + buf->length -
  324. (char *)startwrite;
  325. }
  326. if (lencopy <= 0)
  327. break;
  328. memcpy(startwrite, startread, lencopy);
  329. remain -= lencopy;
  330. }
  331. if (offset > 1440) {
  332. /* We have enough data to check for greenscreen */
  333. if (outp[0] < 0x60 && outp[1440] < 0x60)
  334. dev->greenscreen_detected = 1;
  335. }
  336. dma_q->pos += len;
  337. }
  338. /*
  339. * video-buf generic routine to get the next available buffer
  340. */
  341. static inline void get_next_buf(struct au0828_dmaqueue *dma_q,
  342. struct au0828_buffer **buf)
  343. {
  344. struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vidq);
  345. if (list_empty(&dma_q->active)) {
  346. au0828_isocdbg("No active queue to serve\n");
  347. dev->isoc_ctl.buf = NULL;
  348. *buf = NULL;
  349. return;
  350. }
  351. /* Get the next buffer */
  352. *buf = list_entry(dma_q->active.next, struct au0828_buffer, list);
  353. /* Cleans up buffer - Useful for testing for frame/URB loss */
  354. list_del(&(*buf)->list);
  355. dma_q->pos = 0;
  356. (*buf)->vb_buf = (*buf)->mem;
  357. dev->isoc_ctl.buf = *buf;
  358. return;
  359. }
  360. static void au0828_copy_vbi(struct au0828_dev *dev,
  361. struct au0828_dmaqueue *dma_q,
  362. struct au0828_buffer *buf,
  363. unsigned char *p,
  364. unsigned char *outp, unsigned long len)
  365. {
  366. unsigned char *startwrite, *startread;
  367. int bytesperline;
  368. int i, j = 0;
  369. if (dev == NULL) {
  370. au0828_isocdbg("dev is null\n");
  371. return;
  372. }
  373. if (dma_q == NULL) {
  374. au0828_isocdbg("dma_q is null\n");
  375. return;
  376. }
  377. if (buf == NULL)
  378. return;
  379. if (p == NULL) {
  380. au0828_isocdbg("p is null\n");
  381. return;
  382. }
  383. if (outp == NULL) {
  384. au0828_isocdbg("outp is null\n");
  385. return;
  386. }
  387. bytesperline = dev->vbi_width;
  388. if (dma_q->pos + len > buf->length)
  389. len = buf->length - dma_q->pos;
  390. startread = p;
  391. startwrite = outp + (dma_q->pos / 2);
  392. /* Make sure the bottom field populates the second half of the frame */
  393. if (buf->top_field == 0)
  394. startwrite += bytesperline * dev->vbi_height;
  395. for (i = 0; i < len; i += 2)
  396. startwrite[j++] = startread[i+1];
  397. dma_q->pos += len;
  398. }
  399. /*
  400. * video-buf generic routine to get the next available VBI buffer
  401. */
  402. static inline void vbi_get_next_buf(struct au0828_dmaqueue *dma_q,
  403. struct au0828_buffer **buf)
  404. {
  405. struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vbiq);
  406. if (list_empty(&dma_q->active)) {
  407. au0828_isocdbg("No active queue to serve\n");
  408. dev->isoc_ctl.vbi_buf = NULL;
  409. *buf = NULL;
  410. return;
  411. }
  412. /* Get the next buffer */
  413. *buf = list_entry(dma_q->active.next, struct au0828_buffer, list);
  414. /* Cleans up buffer - Useful for testing for frame/URB loss */
  415. list_del(&(*buf)->list);
  416. dma_q->pos = 0;
  417. (*buf)->vb_buf = (*buf)->mem;
  418. dev->isoc_ctl.vbi_buf = *buf;
  419. return;
  420. }
  421. /*
  422. * Controls the isoc copy of each urb packet
  423. */
  424. static inline int au0828_isoc_copy(struct au0828_dev *dev, struct urb *urb)
  425. {
  426. struct au0828_buffer *buf;
  427. struct au0828_buffer *vbi_buf;
  428. struct au0828_dmaqueue *dma_q = urb->context;
  429. struct au0828_dmaqueue *vbi_dma_q = &dev->vbiq;
  430. unsigned char *outp = NULL;
  431. unsigned char *vbioutp = NULL;
  432. int i, len = 0, rc = 1;
  433. unsigned char *p;
  434. unsigned char fbyte;
  435. unsigned int vbi_field_size;
  436. unsigned int remain, lencopy;
  437. if (!dev)
  438. return 0;
  439. if (test_bit(DEV_DISCONNECTED, &dev->dev_state) ||
  440. test_bit(DEV_MISCONFIGURED, &dev->dev_state))
  441. return 0;
  442. if (urb->status < 0) {
  443. print_err_status(dev, -1, urb->status);
  444. if (urb->status == -ENOENT)
  445. return 0;
  446. }
  447. buf = dev->isoc_ctl.buf;
  448. if (buf != NULL)
  449. outp = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
  450. vbi_buf = dev->isoc_ctl.vbi_buf;
  451. if (vbi_buf != NULL)
  452. vbioutp = vb2_plane_vaddr(&vbi_buf->vb.vb2_buf, 0);
  453. for (i = 0; i < urb->number_of_packets; i++) {
  454. int status = urb->iso_frame_desc[i].status;
  455. if (status < 0) {
  456. print_err_status(dev, i, status);
  457. if (urb->iso_frame_desc[i].status != -EPROTO)
  458. continue;
  459. }
  460. if (urb->iso_frame_desc[i].actual_length <= 0)
  461. continue;
  462. if (urb->iso_frame_desc[i].actual_length >
  463. dev->max_pkt_size) {
  464. au0828_isocdbg("packet bigger than packet size");
  465. continue;
  466. }
  467. p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  468. fbyte = p[0];
  469. len = urb->iso_frame_desc[i].actual_length - 4;
  470. p += 4;
  471. if (fbyte & 0x80) {
  472. len -= 4;
  473. p += 4;
  474. au0828_isocdbg("Video frame %s\n",
  475. (fbyte & 0x40) ? "odd" : "even");
  476. if (fbyte & 0x40) {
  477. /* VBI */
  478. if (vbi_buf != NULL)
  479. buffer_filled(dev, vbi_dma_q, vbi_buf);
  480. vbi_get_next_buf(vbi_dma_q, &vbi_buf);
  481. if (vbi_buf == NULL)
  482. vbioutp = NULL;
  483. else
  484. vbioutp = vb2_plane_vaddr(
  485. &vbi_buf->vb.vb2_buf, 0);
  486. /* Video */
  487. if (buf != NULL)
  488. buffer_filled(dev, dma_q, buf);
  489. get_next_buf(dma_q, &buf);
  490. if (buf == NULL)
  491. outp = NULL;
  492. else
  493. outp = vb2_plane_vaddr(
  494. &buf->vb.vb2_buf, 0);
  495. /* As long as isoc traffic is arriving, keep
  496. resetting the timer */
  497. if (dev->vid_timeout_running)
  498. mod_timer(&dev->vid_timeout,
  499. jiffies + (HZ / 10));
  500. if (dev->vbi_timeout_running)
  501. mod_timer(&dev->vbi_timeout,
  502. jiffies + (HZ / 10));
  503. }
  504. if (buf != NULL) {
  505. if (fbyte & 0x40)
  506. buf->top_field = 1;
  507. else
  508. buf->top_field = 0;
  509. }
  510. if (vbi_buf != NULL) {
  511. if (fbyte & 0x40)
  512. vbi_buf->top_field = 1;
  513. else
  514. vbi_buf->top_field = 0;
  515. }
  516. dev->vbi_read = 0;
  517. vbi_dma_q->pos = 0;
  518. dma_q->pos = 0;
  519. }
  520. vbi_field_size = dev->vbi_width * dev->vbi_height * 2;
  521. if (dev->vbi_read < vbi_field_size) {
  522. remain = vbi_field_size - dev->vbi_read;
  523. if (len < remain)
  524. lencopy = len;
  525. else
  526. lencopy = remain;
  527. if (vbi_buf != NULL)
  528. au0828_copy_vbi(dev, vbi_dma_q, vbi_buf, p,
  529. vbioutp, len);
  530. len -= lencopy;
  531. p += lencopy;
  532. dev->vbi_read += lencopy;
  533. }
  534. if (dev->vbi_read >= vbi_field_size && buf != NULL)
  535. au0828_copy_video(dev, dma_q, buf, p, outp, len);
  536. }
  537. return rc;
  538. }
  539. void au0828_usb_v4l2_media_release(struct au0828_dev *dev)
  540. {
  541. #ifdef CONFIG_MEDIA_CONTROLLER
  542. int i;
  543. for (i = 0; i < AU0828_MAX_INPUT; i++) {
  544. if (AUVI_INPUT(i).type == AU0828_VMUX_UNDEFINED)
  545. return;
  546. media_device_unregister_entity(&dev->input_ent[i]);
  547. }
  548. #endif
  549. }
  550. static void au0828_usb_v4l2_release(struct v4l2_device *v4l2_dev)
  551. {
  552. struct au0828_dev *dev =
  553. container_of(v4l2_dev, struct au0828_dev, v4l2_dev);
  554. v4l2_ctrl_handler_free(&dev->v4l2_ctrl_hdl);
  555. v4l2_device_unregister(&dev->v4l2_dev);
  556. au0828_usb_v4l2_media_release(dev);
  557. au0828_usb_release(dev);
  558. }
  559. int au0828_v4l2_device_register(struct usb_interface *interface,
  560. struct au0828_dev *dev)
  561. {
  562. int retval;
  563. if (AUVI_INPUT(0).type == AU0828_VMUX_UNDEFINED)
  564. return 0;
  565. /* Create the v4l2_device */
  566. #ifdef CONFIG_MEDIA_CONTROLLER
  567. dev->v4l2_dev.mdev = dev->media_dev;
  568. #endif
  569. retval = v4l2_device_register(&interface->dev, &dev->v4l2_dev);
  570. if (retval) {
  571. pr_err("%s() v4l2_device_register failed\n",
  572. __func__);
  573. return retval;
  574. }
  575. dev->v4l2_dev.release = au0828_usb_v4l2_release;
  576. /* This control handler will inherit the controls from au8522 */
  577. retval = v4l2_ctrl_handler_init(&dev->v4l2_ctrl_hdl, 4);
  578. if (retval) {
  579. pr_err("%s() v4l2_ctrl_handler_init failed\n",
  580. __func__);
  581. return retval;
  582. }
  583. dev->v4l2_dev.ctrl_handler = &dev->v4l2_ctrl_hdl;
  584. return 0;
  585. }
  586. static int queue_setup(struct vb2_queue *vq,
  587. unsigned int *nbuffers, unsigned int *nplanes,
  588. unsigned int sizes[], struct device *alloc_devs[])
  589. {
  590. struct au0828_dev *dev = vb2_get_drv_priv(vq);
  591. unsigned long size = dev->height * dev->bytesperline;
  592. if (*nplanes)
  593. return sizes[0] < size ? -EINVAL : 0;
  594. *nplanes = 1;
  595. sizes[0] = size;
  596. return 0;
  597. }
  598. static int
  599. buffer_prepare(struct vb2_buffer *vb)
  600. {
  601. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  602. struct au0828_buffer *buf = container_of(vbuf,
  603. struct au0828_buffer, vb);
  604. struct au0828_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
  605. buf->length = dev->height * dev->bytesperline;
  606. if (vb2_plane_size(vb, 0) < buf->length) {
  607. pr_err("%s data will not fit into plane (%lu < %lu)\n",
  608. __func__, vb2_plane_size(vb, 0), buf->length);
  609. return -EINVAL;
  610. }
  611. vb2_set_plane_payload(&buf->vb.vb2_buf, 0, buf->length);
  612. return 0;
  613. }
  614. static void
  615. buffer_queue(struct vb2_buffer *vb)
  616. {
  617. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  618. struct au0828_buffer *buf = container_of(vbuf,
  619. struct au0828_buffer,
  620. vb);
  621. struct au0828_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
  622. struct au0828_dmaqueue *vidq = &dev->vidq;
  623. unsigned long flags = 0;
  624. buf->mem = vb2_plane_vaddr(vb, 0);
  625. buf->length = vb2_plane_size(vb, 0);
  626. spin_lock_irqsave(&dev->slock, flags);
  627. list_add_tail(&buf->list, &vidq->active);
  628. spin_unlock_irqrestore(&dev->slock, flags);
  629. }
  630. static int au0828_i2s_init(struct au0828_dev *dev)
  631. {
  632. /* Enable i2s mode */
  633. au0828_writereg(dev, AU0828_AUDIOCTRL_50C, 0x01);
  634. return 0;
  635. }
  636. /*
  637. * Auvitek au0828 analog stream enable
  638. */
  639. static int au0828_analog_stream_enable(struct au0828_dev *d)
  640. {
  641. struct usb_interface *iface;
  642. int ret, h, w;
  643. dprintk(1, "au0828_analog_stream_enable called\n");
  644. if (test_bit(DEV_DISCONNECTED, &d->dev_state))
  645. return -ENODEV;
  646. iface = usb_ifnum_to_if(d->usbdev, 0);
  647. if (iface && iface->cur_altsetting->desc.bAlternateSetting != 5) {
  648. dprintk(1, "Changing intf#0 to alt 5\n");
  649. /* set au0828 interface0 to AS5 here again */
  650. ret = usb_set_interface(d->usbdev, 0, 5);
  651. if (ret < 0) {
  652. pr_info("Au0828 can't set alt setting to 5!\n");
  653. return -EBUSY;
  654. }
  655. }
  656. h = d->height / 2 + 2;
  657. w = d->width * 2;
  658. au0828_writereg(d, AU0828_SENSORCTRL_VBI_103, 0x00);
  659. au0828_writereg(d, 0x106, 0x00);
  660. /* set x position */
  661. au0828_writereg(d, 0x110, 0x00);
  662. au0828_writereg(d, 0x111, 0x00);
  663. au0828_writereg(d, 0x114, w & 0xff);
  664. au0828_writereg(d, 0x115, w >> 8);
  665. /* set y position */
  666. au0828_writereg(d, 0x112, 0x00);
  667. au0828_writereg(d, 0x113, 0x00);
  668. au0828_writereg(d, 0x116, h & 0xff);
  669. au0828_writereg(d, 0x117, h >> 8);
  670. au0828_writereg(d, AU0828_SENSORCTRL_100, 0xb3);
  671. return 0;
  672. }
  673. static int au0828_analog_stream_disable(struct au0828_dev *d)
  674. {
  675. dprintk(1, "au0828_analog_stream_disable called\n");
  676. au0828_writereg(d, AU0828_SENSORCTRL_100, 0x0);
  677. return 0;
  678. }
  679. static void au0828_analog_stream_reset(struct au0828_dev *dev)
  680. {
  681. dprintk(1, "au0828_analog_stream_reset called\n");
  682. au0828_writereg(dev, AU0828_SENSORCTRL_100, 0x0);
  683. mdelay(30);
  684. au0828_writereg(dev, AU0828_SENSORCTRL_100, 0xb3);
  685. }
  686. /*
  687. * Some operations needs to stop current streaming
  688. */
  689. static int au0828_stream_interrupt(struct au0828_dev *dev)
  690. {
  691. dev->stream_state = STREAM_INTERRUPT;
  692. if (test_bit(DEV_DISCONNECTED, &dev->dev_state))
  693. return -ENODEV;
  694. return 0;
  695. }
  696. int au0828_start_analog_streaming(struct vb2_queue *vq, unsigned int count)
  697. {
  698. struct au0828_dev *dev = vb2_get_drv_priv(vq);
  699. int rc = 0;
  700. dprintk(1, "au0828_start_analog_streaming called %d\n",
  701. dev->streaming_users);
  702. if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  703. dev->frame_count = 0;
  704. else
  705. dev->vbi_frame_count = 0;
  706. if (dev->streaming_users == 0) {
  707. /* If we were doing ac97 instead of i2s, it would go here...*/
  708. au0828_i2s_init(dev);
  709. rc = au0828_init_isoc(dev, AU0828_ISO_PACKETS_PER_URB,
  710. AU0828_MAX_ISO_BUFS, dev->max_pkt_size,
  711. au0828_isoc_copy);
  712. if (rc < 0) {
  713. pr_info("au0828_init_isoc failed\n");
  714. return rc;
  715. }
  716. v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 1);
  717. if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  718. dev->vid_timeout_running = 1;
  719. mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
  720. } else if (vq->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
  721. dev->vbi_timeout_running = 1;
  722. mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
  723. }
  724. }
  725. dev->streaming_users++;
  726. return rc;
  727. }
  728. static void au0828_stop_streaming(struct vb2_queue *vq)
  729. {
  730. struct au0828_dev *dev = vb2_get_drv_priv(vq);
  731. struct au0828_dmaqueue *vidq = &dev->vidq;
  732. unsigned long flags = 0;
  733. dprintk(1, "au0828_stop_streaming called %d\n", dev->streaming_users);
  734. if (dev->streaming_users-- == 1) {
  735. au0828_uninit_isoc(dev);
  736. v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0);
  737. }
  738. dev->vid_timeout_running = 0;
  739. del_timer_sync(&dev->vid_timeout);
  740. spin_lock_irqsave(&dev->slock, flags);
  741. if (dev->isoc_ctl.buf != NULL) {
  742. vb2_buffer_done(&dev->isoc_ctl.buf->vb.vb2_buf,
  743. VB2_BUF_STATE_ERROR);
  744. dev->isoc_ctl.buf = NULL;
  745. }
  746. while (!list_empty(&vidq->active)) {
  747. struct au0828_buffer *buf;
  748. buf = list_entry(vidq->active.next, struct au0828_buffer, list);
  749. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  750. list_del(&buf->list);
  751. }
  752. spin_unlock_irqrestore(&dev->slock, flags);
  753. }
  754. void au0828_stop_vbi_streaming(struct vb2_queue *vq)
  755. {
  756. struct au0828_dev *dev = vb2_get_drv_priv(vq);
  757. struct au0828_dmaqueue *vbiq = &dev->vbiq;
  758. unsigned long flags = 0;
  759. dprintk(1, "au0828_stop_vbi_streaming called %d\n",
  760. dev->streaming_users);
  761. if (dev->streaming_users-- == 1) {
  762. au0828_uninit_isoc(dev);
  763. v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0);
  764. }
  765. spin_lock_irqsave(&dev->slock, flags);
  766. if (dev->isoc_ctl.vbi_buf != NULL) {
  767. vb2_buffer_done(&dev->isoc_ctl.vbi_buf->vb.vb2_buf,
  768. VB2_BUF_STATE_ERROR);
  769. dev->isoc_ctl.vbi_buf = NULL;
  770. }
  771. while (!list_empty(&vbiq->active)) {
  772. struct au0828_buffer *buf;
  773. buf = list_entry(vbiq->active.next, struct au0828_buffer, list);
  774. list_del(&buf->list);
  775. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  776. }
  777. spin_unlock_irqrestore(&dev->slock, flags);
  778. dev->vbi_timeout_running = 0;
  779. del_timer_sync(&dev->vbi_timeout);
  780. }
  781. static const struct vb2_ops au0828_video_qops = {
  782. .queue_setup = queue_setup,
  783. .buf_prepare = buffer_prepare,
  784. .buf_queue = buffer_queue,
  785. .start_streaming = au0828_start_analog_streaming,
  786. .stop_streaming = au0828_stop_streaming,
  787. .wait_prepare = vb2_ops_wait_prepare,
  788. .wait_finish = vb2_ops_wait_finish,
  789. };
  790. /* ------------------------------------------------------------------
  791. V4L2 interface
  792. ------------------------------------------------------------------*/
  793. /*
  794. * au0828_analog_unregister
  795. * unregister v4l2 devices
  796. */
  797. int au0828_analog_unregister(struct au0828_dev *dev)
  798. {
  799. dprintk(1, "au0828_analog_unregister called\n");
  800. /* No analog TV */
  801. if (AUVI_INPUT(0).type == AU0828_VMUX_UNDEFINED)
  802. return 0;
  803. mutex_lock(&au0828_sysfs_lock);
  804. video_unregister_device(&dev->vdev);
  805. video_unregister_device(&dev->vbi_dev);
  806. mutex_unlock(&au0828_sysfs_lock);
  807. v4l2_device_disconnect(&dev->v4l2_dev);
  808. v4l2_device_put(&dev->v4l2_dev);
  809. return 1;
  810. }
  811. /* This function ensures that video frames continue to be delivered even if
  812. the ITU-656 input isn't receiving any data (thereby preventing applications
  813. such as tvtime from hanging) */
  814. static void au0828_vid_buffer_timeout(struct timer_list *t)
  815. {
  816. struct au0828_dev *dev = from_timer(dev, t, vid_timeout);
  817. struct au0828_dmaqueue *dma_q = &dev->vidq;
  818. struct au0828_buffer *buf;
  819. unsigned char *vid_data;
  820. unsigned long flags = 0;
  821. spin_lock_irqsave(&dev->slock, flags);
  822. buf = dev->isoc_ctl.buf;
  823. if (buf != NULL) {
  824. vid_data = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
  825. memset(vid_data, 0x00, buf->length); /* Blank green frame */
  826. buffer_filled(dev, dma_q, buf);
  827. }
  828. get_next_buf(dma_q, &buf);
  829. if (dev->vid_timeout_running == 1)
  830. mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
  831. spin_unlock_irqrestore(&dev->slock, flags);
  832. }
  833. static void au0828_vbi_buffer_timeout(struct timer_list *t)
  834. {
  835. struct au0828_dev *dev = from_timer(dev, t, vbi_timeout);
  836. struct au0828_dmaqueue *dma_q = &dev->vbiq;
  837. struct au0828_buffer *buf;
  838. unsigned char *vbi_data;
  839. unsigned long flags = 0;
  840. spin_lock_irqsave(&dev->slock, flags);
  841. buf = dev->isoc_ctl.vbi_buf;
  842. if (buf != NULL) {
  843. vbi_data = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
  844. memset(vbi_data, 0x00, buf->length);
  845. buffer_filled(dev, dma_q, buf);
  846. }
  847. vbi_get_next_buf(dma_q, &buf);
  848. if (dev->vbi_timeout_running == 1)
  849. mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
  850. spin_unlock_irqrestore(&dev->slock, flags);
  851. }
  852. static int au0828_v4l2_open(struct file *filp)
  853. {
  854. struct au0828_dev *dev = video_drvdata(filp);
  855. int ret;
  856. dprintk(1,
  857. "%s called std_set %d dev_state %ld stream users %d users %d\n",
  858. __func__, dev->std_set_in_tuner_core, dev->dev_state,
  859. dev->streaming_users, dev->users);
  860. if (mutex_lock_interruptible(&dev->lock))
  861. return -ERESTARTSYS;
  862. ret = v4l2_fh_open(filp);
  863. if (ret) {
  864. au0828_isocdbg("%s: v4l2_fh_open() returned error %d\n",
  865. __func__, ret);
  866. mutex_unlock(&dev->lock);
  867. return ret;
  868. }
  869. if (dev->users == 0) {
  870. au0828_analog_stream_enable(dev);
  871. au0828_analog_stream_reset(dev);
  872. dev->stream_state = STREAM_OFF;
  873. set_bit(DEV_INITIALIZED, &dev->dev_state);
  874. }
  875. dev->users++;
  876. mutex_unlock(&dev->lock);
  877. return ret;
  878. }
  879. static int au0828_v4l2_close(struct file *filp)
  880. {
  881. int ret;
  882. struct au0828_dev *dev = video_drvdata(filp);
  883. struct video_device *vdev = video_devdata(filp);
  884. dprintk(1,
  885. "%s called std_set %d dev_state %ld stream users %d users %d\n",
  886. __func__, dev->std_set_in_tuner_core, dev->dev_state,
  887. dev->streaming_users, dev->users);
  888. mutex_lock(&dev->lock);
  889. if (vdev->vfl_type == VFL_TYPE_GRABBER && dev->vid_timeout_running) {
  890. /* Cancel timeout thread in case they didn't call streamoff */
  891. dev->vid_timeout_running = 0;
  892. del_timer_sync(&dev->vid_timeout);
  893. } else if (vdev->vfl_type == VFL_TYPE_VBI &&
  894. dev->vbi_timeout_running) {
  895. /* Cancel timeout thread in case they didn't call streamoff */
  896. dev->vbi_timeout_running = 0;
  897. del_timer_sync(&dev->vbi_timeout);
  898. }
  899. if (test_bit(DEV_DISCONNECTED, &dev->dev_state))
  900. goto end;
  901. if (dev->users == 1) {
  902. /*
  903. * Avoid putting tuner in sleep if DVB or ALSA are
  904. * streaming.
  905. *
  906. * On most USB devices like au0828 the tuner can
  907. * be safely put in sleep stare here if ALSA isn't
  908. * streaming. Exceptions are some very old USB tuner
  909. * models such as em28xx-based WinTV USB2 which have
  910. * a separate audio output jack. The devices that have
  911. * a separate audio output jack have analog tuners,
  912. * like Philips FM1236. Those devices are always on,
  913. * so the s_power callback are silently ignored.
  914. * So, the current logic here does the following:
  915. * Disable (put tuner to sleep) when
  916. * - ALSA and DVB aren't not streaming;
  917. * - the last V4L2 file handler is closed.
  918. *
  919. * FIXME:
  920. *
  921. * Additionally, this logic could be improved to
  922. * disable the media source if the above conditions
  923. * are met and if the device:
  924. * - doesn't have a separate audio out plug (or
  925. * - doesn't use a silicon tuner like xc2028/3028/4000/5000).
  926. *
  927. * Once this additional logic is in place, a callback
  928. * is needed to enable the media source and power on
  929. * the tuner, for radio to work.
  930. */
  931. ret = v4l_enable_media_source(vdev);
  932. if (ret == 0)
  933. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner,
  934. standby);
  935. dev->std_set_in_tuner_core = 0;
  936. /* When close the device, set the usb intf0 into alt0 to free
  937. USB bandwidth */
  938. ret = usb_set_interface(dev->usbdev, 0, 0);
  939. if (ret < 0)
  940. pr_info("Au0828 can't set alternate to 0!\n");
  941. }
  942. end:
  943. _vb2_fop_release(filp, NULL);
  944. dev->users--;
  945. mutex_unlock(&dev->lock);
  946. return 0;
  947. }
  948. /* Must be called with dev->lock held */
  949. static void au0828_init_tuner(struct au0828_dev *dev)
  950. {
  951. struct v4l2_frequency f = {
  952. .frequency = dev->ctrl_freq,
  953. .type = V4L2_TUNER_ANALOG_TV,
  954. };
  955. dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
  956. dev->std_set_in_tuner_core, dev->dev_state);
  957. if (dev->std_set_in_tuner_core)
  958. return;
  959. dev->std_set_in_tuner_core = 1;
  960. i2c_gate_ctrl(dev, 1);
  961. /* If we've never sent the standard in tuner core, do so now.
  962. We don't do this at device probe because we don't want to
  963. incur the cost of a firmware load */
  964. v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, dev->std);
  965. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
  966. i2c_gate_ctrl(dev, 0);
  967. }
  968. static int au0828_set_format(struct au0828_dev *dev, unsigned int cmd,
  969. struct v4l2_format *format)
  970. {
  971. int ret;
  972. int width = format->fmt.pix.width;
  973. int height = format->fmt.pix.height;
  974. /* If they are demanding a format other than the one we support,
  975. bail out (tvtime asks for UYVY and then retries with YUYV) */
  976. if (format->fmt.pix.pixelformat != V4L2_PIX_FMT_UYVY)
  977. return -EINVAL;
  978. /* format->fmt.pix.width only support 720 and height 480 */
  979. if (width != 720)
  980. width = 720;
  981. if (height != 480)
  982. height = 480;
  983. format->fmt.pix.width = width;
  984. format->fmt.pix.height = height;
  985. format->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
  986. format->fmt.pix.bytesperline = width * 2;
  987. format->fmt.pix.sizeimage = width * height * 2;
  988. format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  989. format->fmt.pix.field = V4L2_FIELD_INTERLACED;
  990. format->fmt.pix.priv = 0;
  991. if (cmd == VIDIOC_TRY_FMT)
  992. return 0;
  993. /* maybe set new image format, driver current only support 720*480 */
  994. dev->width = width;
  995. dev->height = height;
  996. dev->frame_size = width * height * 2;
  997. dev->field_size = width * height;
  998. dev->bytesperline = width * 2;
  999. if (dev->stream_state == STREAM_ON) {
  1000. dprintk(1, "VIDIOC_SET_FMT: interrupting stream!\n");
  1001. ret = au0828_stream_interrupt(dev);
  1002. if (ret != 0) {
  1003. dprintk(1, "error interrupting video stream!\n");
  1004. return ret;
  1005. }
  1006. }
  1007. au0828_analog_stream_enable(dev);
  1008. return 0;
  1009. }
  1010. static int vidioc_querycap(struct file *file, void *priv,
  1011. struct v4l2_capability *cap)
  1012. {
  1013. struct video_device *vdev = video_devdata(file);
  1014. struct au0828_dev *dev = video_drvdata(file);
  1015. dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
  1016. dev->std_set_in_tuner_core, dev->dev_state);
  1017. strlcpy(cap->driver, "au0828", sizeof(cap->driver));
  1018. strlcpy(cap->card, dev->board.name, sizeof(cap->card));
  1019. usb_make_path(dev->usbdev, cap->bus_info, sizeof(cap->bus_info));
  1020. /* set the device capabilities */
  1021. cap->device_caps = V4L2_CAP_AUDIO |
  1022. V4L2_CAP_READWRITE |
  1023. V4L2_CAP_STREAMING |
  1024. V4L2_CAP_TUNER;
  1025. if (vdev->vfl_type == VFL_TYPE_GRABBER)
  1026. cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
  1027. else
  1028. cap->device_caps |= V4L2_CAP_VBI_CAPTURE;
  1029. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS |
  1030. V4L2_CAP_VBI_CAPTURE | V4L2_CAP_VIDEO_CAPTURE;
  1031. return 0;
  1032. }
  1033. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  1034. struct v4l2_fmtdesc *f)
  1035. {
  1036. if (f->index)
  1037. return -EINVAL;
  1038. dprintk(1, "%s called\n", __func__);
  1039. f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1040. strcpy(f->description, "Packed YUV2");
  1041. f->flags = 0;
  1042. f->pixelformat = V4L2_PIX_FMT_UYVY;
  1043. return 0;
  1044. }
  1045. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  1046. struct v4l2_format *f)
  1047. {
  1048. struct au0828_dev *dev = video_drvdata(file);
  1049. dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
  1050. dev->std_set_in_tuner_core, dev->dev_state);
  1051. f->fmt.pix.width = dev->width;
  1052. f->fmt.pix.height = dev->height;
  1053. f->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
  1054. f->fmt.pix.bytesperline = dev->bytesperline;
  1055. f->fmt.pix.sizeimage = dev->frame_size;
  1056. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; /* NTSC/PAL */
  1057. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  1058. f->fmt.pix.priv = 0;
  1059. return 0;
  1060. }
  1061. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  1062. struct v4l2_format *f)
  1063. {
  1064. struct au0828_dev *dev = video_drvdata(file);
  1065. dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
  1066. dev->std_set_in_tuner_core, dev->dev_state);
  1067. return au0828_set_format(dev, VIDIOC_TRY_FMT, f);
  1068. }
  1069. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  1070. struct v4l2_format *f)
  1071. {
  1072. struct au0828_dev *dev = video_drvdata(file);
  1073. int rc;
  1074. dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
  1075. dev->std_set_in_tuner_core, dev->dev_state);
  1076. rc = check_dev(dev);
  1077. if (rc < 0)
  1078. return rc;
  1079. if (vb2_is_busy(&dev->vb_vidq)) {
  1080. pr_info("%s queue busy\n", __func__);
  1081. rc = -EBUSY;
  1082. goto out;
  1083. }
  1084. rc = au0828_set_format(dev, VIDIOC_S_FMT, f);
  1085. out:
  1086. return rc;
  1087. }
  1088. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
  1089. {
  1090. struct au0828_dev *dev = video_drvdata(file);
  1091. dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
  1092. dev->std_set_in_tuner_core, dev->dev_state);
  1093. if (norm == dev->std)
  1094. return 0;
  1095. if (dev->streaming_users > 0)
  1096. return -EBUSY;
  1097. dev->std = norm;
  1098. au0828_init_tuner(dev);
  1099. i2c_gate_ctrl(dev, 1);
  1100. /*
  1101. * FIXME: when we support something other than 60Hz standards,
  1102. * we are going to have to make the au0828 bridge adjust the size
  1103. * of its capture buffer, which is currently hardcoded at 720x480
  1104. */
  1105. v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, norm);
  1106. i2c_gate_ctrl(dev, 0);
  1107. return 0;
  1108. }
  1109. static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
  1110. {
  1111. struct au0828_dev *dev = video_drvdata(file);
  1112. dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
  1113. dev->std_set_in_tuner_core, dev->dev_state);
  1114. *norm = dev->std;
  1115. return 0;
  1116. }
  1117. static int vidioc_enum_input(struct file *file, void *priv,
  1118. struct v4l2_input *input)
  1119. {
  1120. struct au0828_dev *dev = video_drvdata(file);
  1121. unsigned int tmp;
  1122. static const char *inames[] = {
  1123. [AU0828_VMUX_UNDEFINED] = "Undefined",
  1124. [AU0828_VMUX_COMPOSITE] = "Composite",
  1125. [AU0828_VMUX_SVIDEO] = "S-Video",
  1126. [AU0828_VMUX_CABLE] = "Cable TV",
  1127. [AU0828_VMUX_TELEVISION] = "Television",
  1128. [AU0828_VMUX_DVB] = "DVB",
  1129. };
  1130. dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
  1131. dev->std_set_in_tuner_core, dev->dev_state);
  1132. tmp = input->index;
  1133. if (tmp >= AU0828_MAX_INPUT)
  1134. return -EINVAL;
  1135. if (AUVI_INPUT(tmp).type == 0)
  1136. return -EINVAL;
  1137. input->index = tmp;
  1138. strcpy(input->name, inames[AUVI_INPUT(tmp).type]);
  1139. if ((AUVI_INPUT(tmp).type == AU0828_VMUX_TELEVISION) ||
  1140. (AUVI_INPUT(tmp).type == AU0828_VMUX_CABLE)) {
  1141. input->type |= V4L2_INPUT_TYPE_TUNER;
  1142. input->audioset = 1;
  1143. } else {
  1144. input->type |= V4L2_INPUT_TYPE_CAMERA;
  1145. input->audioset = 2;
  1146. }
  1147. input->std = dev->vdev.tvnorms;
  1148. return 0;
  1149. }
  1150. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  1151. {
  1152. struct au0828_dev *dev = video_drvdata(file);
  1153. dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
  1154. dev->std_set_in_tuner_core, dev->dev_state);
  1155. *i = dev->ctrl_input;
  1156. return 0;
  1157. }
  1158. static void au0828_s_input(struct au0828_dev *dev, int index)
  1159. {
  1160. int i;
  1161. dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
  1162. dev->std_set_in_tuner_core, dev->dev_state);
  1163. switch (AUVI_INPUT(index).type) {
  1164. case AU0828_VMUX_SVIDEO:
  1165. dev->input_type = AU0828_VMUX_SVIDEO;
  1166. dev->ctrl_ainput = 1;
  1167. break;
  1168. case AU0828_VMUX_COMPOSITE:
  1169. dev->input_type = AU0828_VMUX_COMPOSITE;
  1170. dev->ctrl_ainput = 1;
  1171. break;
  1172. case AU0828_VMUX_TELEVISION:
  1173. dev->input_type = AU0828_VMUX_TELEVISION;
  1174. dev->ctrl_ainput = 0;
  1175. break;
  1176. default:
  1177. dprintk(1, "unknown input type set [%d]\n",
  1178. AUVI_INPUT(index).type);
  1179. return;
  1180. }
  1181. dev->ctrl_input = index;
  1182. v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
  1183. AUVI_INPUT(index).vmux, 0, 0);
  1184. for (i = 0; i < AU0828_MAX_INPUT; i++) {
  1185. int enable = 0;
  1186. if (AUVI_INPUT(i).audio_setup == NULL)
  1187. continue;
  1188. if (i == index)
  1189. enable = 1;
  1190. else
  1191. enable = 0;
  1192. if (enable) {
  1193. (AUVI_INPUT(i).audio_setup)(dev, enable);
  1194. } else {
  1195. /* Make sure we leave it turned on if some
  1196. other input is routed to this callback */
  1197. if ((AUVI_INPUT(i).audio_setup) !=
  1198. ((AUVI_INPUT(index).audio_setup))) {
  1199. (AUVI_INPUT(i).audio_setup)(dev, enable);
  1200. }
  1201. }
  1202. }
  1203. v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
  1204. AUVI_INPUT(index).amux, 0, 0);
  1205. }
  1206. static int vidioc_s_input(struct file *file, void *priv, unsigned int index)
  1207. {
  1208. struct au0828_dev *dev = video_drvdata(file);
  1209. struct video_device *vfd = video_devdata(file);
  1210. dprintk(1, "VIDIOC_S_INPUT in function %s, input=%d\n", __func__,
  1211. index);
  1212. if (index >= AU0828_MAX_INPUT)
  1213. return -EINVAL;
  1214. if (AUVI_INPUT(index).type == 0)
  1215. return -EINVAL;
  1216. if (dev->ctrl_input == index)
  1217. return 0;
  1218. au0828_s_input(dev, index);
  1219. /*
  1220. * Input has been changed. Disable the media source
  1221. * associated with the old input and enable source
  1222. * for the newly set input
  1223. */
  1224. v4l_disable_media_source(vfd);
  1225. return v4l_enable_media_source(vfd);
  1226. }
  1227. static int vidioc_enumaudio(struct file *file, void *priv, struct v4l2_audio *a)
  1228. {
  1229. if (a->index > 1)
  1230. return -EINVAL;
  1231. dprintk(1, "%s called\n", __func__);
  1232. if (a->index == 0)
  1233. strcpy(a->name, "Television");
  1234. else
  1235. strcpy(a->name, "Line in");
  1236. a->capability = V4L2_AUDCAP_STEREO;
  1237. return 0;
  1238. }
  1239. static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  1240. {
  1241. struct au0828_dev *dev = video_drvdata(file);
  1242. dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
  1243. dev->std_set_in_tuner_core, dev->dev_state);
  1244. a->index = dev->ctrl_ainput;
  1245. if (a->index == 0)
  1246. strcpy(a->name, "Television");
  1247. else
  1248. strcpy(a->name, "Line in");
  1249. a->capability = V4L2_AUDCAP_STEREO;
  1250. return 0;
  1251. }
  1252. static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
  1253. {
  1254. struct au0828_dev *dev = video_drvdata(file);
  1255. if (a->index != dev->ctrl_ainput)
  1256. return -EINVAL;
  1257. dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
  1258. dev->std_set_in_tuner_core, dev->dev_state);
  1259. return 0;
  1260. }
  1261. static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
  1262. {
  1263. struct au0828_dev *dev = video_drvdata(file);
  1264. struct video_device *vfd = video_devdata(file);
  1265. int ret;
  1266. if (t->index != 0)
  1267. return -EINVAL;
  1268. ret = v4l_enable_media_source(vfd);
  1269. if (ret)
  1270. return ret;
  1271. dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
  1272. dev->std_set_in_tuner_core, dev->dev_state);
  1273. strcpy(t->name, "Auvitek tuner");
  1274. au0828_init_tuner(dev);
  1275. i2c_gate_ctrl(dev, 1);
  1276. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
  1277. i2c_gate_ctrl(dev, 0);
  1278. return 0;
  1279. }
  1280. static int vidioc_s_tuner(struct file *file, void *priv,
  1281. const struct v4l2_tuner *t)
  1282. {
  1283. struct au0828_dev *dev = video_drvdata(file);
  1284. if (t->index != 0)
  1285. return -EINVAL;
  1286. dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
  1287. dev->std_set_in_tuner_core, dev->dev_state);
  1288. au0828_init_tuner(dev);
  1289. i2c_gate_ctrl(dev, 1);
  1290. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
  1291. i2c_gate_ctrl(dev, 0);
  1292. dprintk(1, "VIDIOC_S_TUNER: signal = %x, afc = %x\n", t->signal,
  1293. t->afc);
  1294. return 0;
  1295. }
  1296. static int vidioc_g_frequency(struct file *file, void *priv,
  1297. struct v4l2_frequency *freq)
  1298. {
  1299. struct au0828_dev *dev = video_drvdata(file);
  1300. if (freq->tuner != 0)
  1301. return -EINVAL;
  1302. dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
  1303. dev->std_set_in_tuner_core, dev->dev_state);
  1304. freq->frequency = dev->ctrl_freq;
  1305. return 0;
  1306. }
  1307. static int vidioc_s_frequency(struct file *file, void *priv,
  1308. const struct v4l2_frequency *freq)
  1309. {
  1310. struct au0828_dev *dev = video_drvdata(file);
  1311. struct v4l2_frequency new_freq = *freq;
  1312. if (freq->tuner != 0)
  1313. return -EINVAL;
  1314. dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
  1315. dev->std_set_in_tuner_core, dev->dev_state);
  1316. au0828_init_tuner(dev);
  1317. i2c_gate_ctrl(dev, 1);
  1318. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, freq);
  1319. /* Get the actual set (and possibly clamped) frequency */
  1320. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, &new_freq);
  1321. dev->ctrl_freq = new_freq.frequency;
  1322. i2c_gate_ctrl(dev, 0);
  1323. au0828_analog_stream_reset(dev);
  1324. return 0;
  1325. }
  1326. /* RAW VBI ioctls */
  1327. static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
  1328. struct v4l2_format *format)
  1329. {
  1330. struct au0828_dev *dev = video_drvdata(file);
  1331. dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
  1332. dev->std_set_in_tuner_core, dev->dev_state);
  1333. format->fmt.vbi.samples_per_line = dev->vbi_width;
  1334. format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  1335. format->fmt.vbi.offset = 0;
  1336. format->fmt.vbi.flags = 0;
  1337. format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
  1338. format->fmt.vbi.count[0] = dev->vbi_height;
  1339. format->fmt.vbi.count[1] = dev->vbi_height;
  1340. format->fmt.vbi.start[0] = 21;
  1341. format->fmt.vbi.start[1] = 284;
  1342. memset(format->fmt.vbi.reserved, 0, sizeof(format->fmt.vbi.reserved));
  1343. return 0;
  1344. }
  1345. static int vidioc_cropcap(struct file *file, void *priv,
  1346. struct v4l2_cropcap *cc)
  1347. {
  1348. struct au0828_dev *dev = video_drvdata(file);
  1349. if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1350. return -EINVAL;
  1351. dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
  1352. dev->std_set_in_tuner_core, dev->dev_state);
  1353. cc->bounds.left = 0;
  1354. cc->bounds.top = 0;
  1355. cc->bounds.width = dev->width;
  1356. cc->bounds.height = dev->height;
  1357. cc->defrect = cc->bounds;
  1358. cc->pixelaspect.numerator = 54;
  1359. cc->pixelaspect.denominator = 59;
  1360. return 0;
  1361. }
  1362. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1363. static int vidioc_g_register(struct file *file, void *priv,
  1364. struct v4l2_dbg_register *reg)
  1365. {
  1366. struct au0828_dev *dev = video_drvdata(file);
  1367. dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
  1368. dev->std_set_in_tuner_core, dev->dev_state);
  1369. reg->val = au0828_read(dev, reg->reg);
  1370. reg->size = 1;
  1371. return 0;
  1372. }
  1373. static int vidioc_s_register(struct file *file, void *priv,
  1374. const struct v4l2_dbg_register *reg)
  1375. {
  1376. struct au0828_dev *dev = video_drvdata(file);
  1377. dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
  1378. dev->std_set_in_tuner_core, dev->dev_state);
  1379. return au0828_writereg(dev, reg->reg, reg->val);
  1380. }
  1381. #endif
  1382. static int vidioc_log_status(struct file *file, void *fh)
  1383. {
  1384. struct video_device *vdev = video_devdata(file);
  1385. dprintk(1, "%s called\n", __func__);
  1386. v4l2_ctrl_log_status(file, fh);
  1387. v4l2_device_call_all(vdev->v4l2_dev, 0, core, log_status);
  1388. return 0;
  1389. }
  1390. void au0828_v4l2_suspend(struct au0828_dev *dev)
  1391. {
  1392. struct urb *urb;
  1393. int i;
  1394. pr_info("stopping V4L2\n");
  1395. if (dev->stream_state == STREAM_ON) {
  1396. pr_info("stopping V4L2 active URBs\n");
  1397. au0828_analog_stream_disable(dev);
  1398. /* stop urbs */
  1399. for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
  1400. urb = dev->isoc_ctl.urb[i];
  1401. if (urb) {
  1402. if (!irqs_disabled())
  1403. usb_kill_urb(urb);
  1404. else
  1405. usb_unlink_urb(urb);
  1406. }
  1407. }
  1408. }
  1409. if (dev->vid_timeout_running)
  1410. del_timer_sync(&dev->vid_timeout);
  1411. if (dev->vbi_timeout_running)
  1412. del_timer_sync(&dev->vbi_timeout);
  1413. }
  1414. void au0828_v4l2_resume(struct au0828_dev *dev)
  1415. {
  1416. int i, rc;
  1417. pr_info("restarting V4L2\n");
  1418. if (dev->stream_state == STREAM_ON) {
  1419. au0828_stream_interrupt(dev);
  1420. au0828_init_tuner(dev);
  1421. }
  1422. if (dev->vid_timeout_running)
  1423. mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
  1424. if (dev->vbi_timeout_running)
  1425. mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
  1426. /* If we were doing ac97 instead of i2s, it would go here...*/
  1427. au0828_i2s_init(dev);
  1428. au0828_analog_stream_enable(dev);
  1429. if (!(dev->stream_state == STREAM_ON)) {
  1430. au0828_analog_stream_reset(dev);
  1431. /* submit urbs */
  1432. for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
  1433. rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
  1434. if (rc) {
  1435. au0828_isocdbg("submit of urb %i failed (error=%i)\n",
  1436. i, rc);
  1437. au0828_uninit_isoc(dev);
  1438. }
  1439. }
  1440. }
  1441. }
  1442. static const struct v4l2_file_operations au0828_v4l_fops = {
  1443. .owner = THIS_MODULE,
  1444. .open = au0828_v4l2_open,
  1445. .release = au0828_v4l2_close,
  1446. .read = vb2_fop_read,
  1447. .poll = vb2_fop_poll,
  1448. .mmap = vb2_fop_mmap,
  1449. .unlocked_ioctl = video_ioctl2,
  1450. };
  1451. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1452. .vidioc_querycap = vidioc_querycap,
  1453. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1454. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1455. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1456. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1457. .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
  1458. .vidioc_try_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
  1459. .vidioc_s_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
  1460. .vidioc_enumaudio = vidioc_enumaudio,
  1461. .vidioc_g_audio = vidioc_g_audio,
  1462. .vidioc_s_audio = vidioc_s_audio,
  1463. .vidioc_cropcap = vidioc_cropcap,
  1464. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  1465. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  1466. .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  1467. .vidioc_querybuf = vb2_ioctl_querybuf,
  1468. .vidioc_qbuf = vb2_ioctl_qbuf,
  1469. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  1470. .vidioc_expbuf = vb2_ioctl_expbuf,
  1471. .vidioc_s_std = vidioc_s_std,
  1472. .vidioc_g_std = vidioc_g_std,
  1473. .vidioc_enum_input = vidioc_enum_input,
  1474. .vidioc_g_input = vidioc_g_input,
  1475. .vidioc_s_input = vidioc_s_input,
  1476. .vidioc_streamon = vb2_ioctl_streamon,
  1477. .vidioc_streamoff = vb2_ioctl_streamoff,
  1478. .vidioc_g_tuner = vidioc_g_tuner,
  1479. .vidioc_s_tuner = vidioc_s_tuner,
  1480. .vidioc_g_frequency = vidioc_g_frequency,
  1481. .vidioc_s_frequency = vidioc_s_frequency,
  1482. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1483. .vidioc_g_register = vidioc_g_register,
  1484. .vidioc_s_register = vidioc_s_register,
  1485. #endif
  1486. .vidioc_log_status = vidioc_log_status,
  1487. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1488. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1489. };
  1490. static const struct video_device au0828_video_template = {
  1491. .fops = &au0828_v4l_fops,
  1492. .release = video_device_release_empty,
  1493. .ioctl_ops = &video_ioctl_ops,
  1494. .tvnorms = V4L2_STD_NTSC_M | V4L2_STD_PAL_M,
  1495. };
  1496. static int au0828_vb2_setup(struct au0828_dev *dev)
  1497. {
  1498. int rc;
  1499. struct vb2_queue *q;
  1500. /* Setup Videobuf2 for Video capture */
  1501. q = &dev->vb_vidq;
  1502. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1503. q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
  1504. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1505. q->drv_priv = dev;
  1506. q->buf_struct_size = sizeof(struct au0828_buffer);
  1507. q->ops = &au0828_video_qops;
  1508. q->mem_ops = &vb2_vmalloc_memops;
  1509. rc = vb2_queue_init(q);
  1510. if (rc < 0)
  1511. return rc;
  1512. /* Setup Videobuf2 for VBI capture */
  1513. q = &dev->vb_vbiq;
  1514. q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
  1515. q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
  1516. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1517. q->drv_priv = dev;
  1518. q->buf_struct_size = sizeof(struct au0828_buffer);
  1519. q->ops = &au0828_vbi_qops;
  1520. q->mem_ops = &vb2_vmalloc_memops;
  1521. rc = vb2_queue_init(q);
  1522. if (rc < 0)
  1523. return rc;
  1524. return 0;
  1525. }
  1526. static void au0828_analog_create_entities(struct au0828_dev *dev)
  1527. {
  1528. #if defined(CONFIG_MEDIA_CONTROLLER)
  1529. static const char * const inames[] = {
  1530. [AU0828_VMUX_COMPOSITE] = "Composite",
  1531. [AU0828_VMUX_SVIDEO] = "S-Video",
  1532. [AU0828_VMUX_CABLE] = "Cable TV",
  1533. [AU0828_VMUX_TELEVISION] = "Television",
  1534. [AU0828_VMUX_DVB] = "DVB",
  1535. };
  1536. int ret, i;
  1537. /* Initialize Video and VBI pads */
  1538. dev->video_pad.flags = MEDIA_PAD_FL_SINK;
  1539. ret = media_entity_pads_init(&dev->vdev.entity, 1, &dev->video_pad);
  1540. if (ret < 0)
  1541. pr_err("failed to initialize video media entity!\n");
  1542. dev->vbi_pad.flags = MEDIA_PAD_FL_SINK;
  1543. ret = media_entity_pads_init(&dev->vbi_dev.entity, 1, &dev->vbi_pad);
  1544. if (ret < 0)
  1545. pr_err("failed to initialize vbi media entity!\n");
  1546. /* Create entities for each input connector */
  1547. for (i = 0; i < AU0828_MAX_INPUT; i++) {
  1548. struct media_entity *ent = &dev->input_ent[i];
  1549. if (AUVI_INPUT(i).type == AU0828_VMUX_UNDEFINED)
  1550. break;
  1551. ent->name = inames[AUVI_INPUT(i).type];
  1552. ent->flags = MEDIA_ENT_FL_CONNECTOR;
  1553. dev->input_pad[i].flags = MEDIA_PAD_FL_SOURCE;
  1554. switch (AUVI_INPUT(i).type) {
  1555. case AU0828_VMUX_COMPOSITE:
  1556. ent->function = MEDIA_ENT_F_CONN_COMPOSITE;
  1557. break;
  1558. case AU0828_VMUX_SVIDEO:
  1559. ent->function = MEDIA_ENT_F_CONN_SVIDEO;
  1560. break;
  1561. case AU0828_VMUX_CABLE:
  1562. case AU0828_VMUX_TELEVISION:
  1563. case AU0828_VMUX_DVB:
  1564. default: /* Just to shut up a warning */
  1565. ent->function = MEDIA_ENT_F_CONN_RF;
  1566. break;
  1567. }
  1568. ret = media_entity_pads_init(ent, 1, &dev->input_pad[i]);
  1569. if (ret < 0)
  1570. pr_err("failed to initialize input pad[%d]!\n", i);
  1571. ret = media_device_register_entity(dev->media_dev, ent);
  1572. if (ret < 0)
  1573. pr_err("failed to register input entity %d!\n", i);
  1574. }
  1575. #endif
  1576. }
  1577. /**************************************************************************/
  1578. int au0828_analog_register(struct au0828_dev *dev,
  1579. struct usb_interface *interface)
  1580. {
  1581. int retval = -ENOMEM;
  1582. struct usb_host_interface *iface_desc;
  1583. struct usb_endpoint_descriptor *endpoint;
  1584. int i, ret;
  1585. dprintk(1, "au0828_analog_register called for intf#%d!\n",
  1586. interface->cur_altsetting->desc.bInterfaceNumber);
  1587. /* No analog TV */
  1588. if (AUVI_INPUT(0).type == AU0828_VMUX_UNDEFINED)
  1589. return 0;
  1590. /* set au0828 usb interface0 to as5 */
  1591. retval = usb_set_interface(dev->usbdev,
  1592. interface->cur_altsetting->desc.bInterfaceNumber, 5);
  1593. if (retval != 0) {
  1594. pr_info("Failure setting usb interface0 to as5\n");
  1595. return retval;
  1596. }
  1597. /* Figure out which endpoint has the isoc interface */
  1598. iface_desc = interface->cur_altsetting;
  1599. for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
  1600. endpoint = &iface_desc->endpoint[i].desc;
  1601. if (((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
  1602. == USB_DIR_IN) &&
  1603. ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
  1604. == USB_ENDPOINT_XFER_ISOC)) {
  1605. /* we find our isoc in endpoint */
  1606. u16 tmp = le16_to_cpu(endpoint->wMaxPacketSize);
  1607. dev->max_pkt_size = (tmp & 0x07ff) *
  1608. (((tmp & 0x1800) >> 11) + 1);
  1609. dev->isoc_in_endpointaddr = endpoint->bEndpointAddress;
  1610. dprintk(1,
  1611. "Found isoc endpoint 0x%02x, max size = %d\n",
  1612. dev->isoc_in_endpointaddr, dev->max_pkt_size);
  1613. }
  1614. }
  1615. if (!(dev->isoc_in_endpointaddr)) {
  1616. pr_info("Could not locate isoc endpoint\n");
  1617. return -ENODEV;
  1618. }
  1619. init_waitqueue_head(&dev->open);
  1620. spin_lock_init(&dev->slock);
  1621. /* init video dma queues */
  1622. INIT_LIST_HEAD(&dev->vidq.active);
  1623. INIT_LIST_HEAD(&dev->vbiq.active);
  1624. timer_setup(&dev->vid_timeout, au0828_vid_buffer_timeout, 0);
  1625. timer_setup(&dev->vbi_timeout, au0828_vbi_buffer_timeout, 0);
  1626. dev->width = NTSC_STD_W;
  1627. dev->height = NTSC_STD_H;
  1628. dev->field_size = dev->width * dev->height;
  1629. dev->frame_size = dev->field_size << 1;
  1630. dev->bytesperline = dev->width << 1;
  1631. dev->vbi_width = 720;
  1632. dev->vbi_height = 1;
  1633. dev->ctrl_ainput = 0;
  1634. dev->ctrl_freq = 960;
  1635. dev->std = V4L2_STD_NTSC_M;
  1636. /* Default input is TV Tuner */
  1637. au0828_s_input(dev, 0);
  1638. mutex_init(&dev->vb_queue_lock);
  1639. mutex_init(&dev->vb_vbi_queue_lock);
  1640. /* Fill the video capture device struct */
  1641. dev->vdev = au0828_video_template;
  1642. dev->vdev.v4l2_dev = &dev->v4l2_dev;
  1643. dev->vdev.lock = &dev->lock;
  1644. dev->vdev.queue = &dev->vb_vidq;
  1645. dev->vdev.queue->lock = &dev->vb_queue_lock;
  1646. strcpy(dev->vdev.name, "au0828a video");
  1647. /* Setup the VBI device */
  1648. dev->vbi_dev = au0828_video_template;
  1649. dev->vbi_dev.v4l2_dev = &dev->v4l2_dev;
  1650. dev->vbi_dev.lock = &dev->lock;
  1651. dev->vbi_dev.queue = &dev->vb_vbiq;
  1652. dev->vbi_dev.queue->lock = &dev->vb_vbi_queue_lock;
  1653. strcpy(dev->vbi_dev.name, "au0828a vbi");
  1654. /* Init entities at the Media Controller */
  1655. au0828_analog_create_entities(dev);
  1656. /* initialize videobuf2 stuff */
  1657. retval = au0828_vb2_setup(dev);
  1658. if (retval != 0) {
  1659. dprintk(1, "unable to setup videobuf2 queues (error = %d).\n",
  1660. retval);
  1661. return -ENODEV;
  1662. }
  1663. /* Register the v4l2 device */
  1664. video_set_drvdata(&dev->vdev, dev);
  1665. retval = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
  1666. if (retval != 0) {
  1667. dprintk(1, "unable to register video device (error = %d).\n",
  1668. retval);
  1669. ret = -ENODEV;
  1670. goto err_reg_vdev;
  1671. }
  1672. /* Register the vbi device */
  1673. video_set_drvdata(&dev->vbi_dev, dev);
  1674. retval = video_register_device(&dev->vbi_dev, VFL_TYPE_VBI, -1);
  1675. if (retval != 0) {
  1676. dprintk(1, "unable to register vbi device (error = %d).\n",
  1677. retval);
  1678. ret = -ENODEV;
  1679. goto err_reg_vbi_dev;
  1680. }
  1681. #ifdef CONFIG_MEDIA_CONTROLLER
  1682. retval = v4l2_mc_create_media_graph(dev->media_dev);
  1683. if (retval) {
  1684. pr_err("%s() au0282_dev_register failed to create graph\n",
  1685. __func__);
  1686. ret = -ENODEV;
  1687. goto err_reg_vbi_dev;
  1688. }
  1689. #endif
  1690. dprintk(1, "%s completed!\n", __func__);
  1691. return 0;
  1692. err_reg_vbi_dev:
  1693. video_unregister_device(&dev->vdev);
  1694. err_reg_vdev:
  1695. vb2_queue_release(&dev->vb_vidq);
  1696. vb2_queue_release(&dev->vb_vbiq);
  1697. return ret;
  1698. }