au0828-video.c 53 KB

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