au0828-video.c 53 KB

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