au0828-video.c 52 KB

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