au0828-video.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868
  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 <linux/module.h>
  30. #include <linux/slab.h>
  31. #include <linux/init.h>
  32. #include <linux/device.h>
  33. #include <media/v4l2-common.h>
  34. #include <media/v4l2-ioctl.h>
  35. #include <media/v4l2-event.h>
  36. #include <media/tuner.h>
  37. #include "au0828-reg.h"
  38. static DEFINE_MUTEX(au0828_sysfs_lock);
  39. /* ------------------------------------------------------------------
  40. Videobuf operations
  41. ------------------------------------------------------------------*/
  42. static unsigned int isoc_debug;
  43. module_param(isoc_debug, int, 0644);
  44. MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
  45. #define au0828_isocdbg(fmt, arg...) \
  46. do {\
  47. if (isoc_debug) { \
  48. pr_info("au0828 %s :"fmt, \
  49. __func__ , ##arg); \
  50. } \
  51. } while (0)
  52. static inline void i2c_gate_ctrl(struct au0828_dev *dev, int val)
  53. {
  54. if (dev->dvb.frontend && dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl)
  55. dev->dvb.frontend->ops.analog_ops.i2c_gate_ctrl(dev->dvb.frontend, val);
  56. }
  57. static inline void print_err_status(struct au0828_dev *dev,
  58. int packet, int status)
  59. {
  60. char *errmsg = "Unknown";
  61. switch (status) {
  62. case -ENOENT:
  63. errmsg = "unlinked synchronuously";
  64. break;
  65. case -ECONNRESET:
  66. errmsg = "unlinked asynchronuously";
  67. break;
  68. case -ENOSR:
  69. errmsg = "Buffer error (overrun)";
  70. break;
  71. case -EPIPE:
  72. errmsg = "Stalled (device not responding)";
  73. break;
  74. case -EOVERFLOW:
  75. errmsg = "Babble (bad cable?)";
  76. break;
  77. case -EPROTO:
  78. errmsg = "Bit-stuff error (bad cable?)";
  79. break;
  80. case -EILSEQ:
  81. errmsg = "CRC/Timeout (could be anything)";
  82. break;
  83. case -ETIME:
  84. errmsg = "Device does not respond";
  85. break;
  86. }
  87. if (packet < 0) {
  88. au0828_isocdbg("URB status %d [%s].\n", status, errmsg);
  89. } else {
  90. au0828_isocdbg("URB packet %d, status %d [%s].\n",
  91. packet, status, errmsg);
  92. }
  93. }
  94. static int check_dev(struct au0828_dev *dev)
  95. {
  96. if (test_bit(DEV_DISCONNECTED, &dev->dev_state)) {
  97. pr_info("v4l2 ioctl: device not present\n");
  98. return -ENODEV;
  99. }
  100. if (test_bit(DEV_MISCONFIGURED, &dev->dev_state)) {
  101. pr_info("v4l2 ioctl: device is misconfigured; close and open it again\n");
  102. return -EIO;
  103. }
  104. return 0;
  105. }
  106. /*
  107. * IRQ callback, called by URB callback
  108. */
  109. static void au0828_irq_callback(struct urb *urb)
  110. {
  111. struct au0828_dmaqueue *dma_q = urb->context;
  112. struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vidq);
  113. unsigned long flags = 0;
  114. int i;
  115. switch (urb->status) {
  116. case 0: /* success */
  117. case -ETIMEDOUT: /* NAK */
  118. break;
  119. case -ECONNRESET: /* kill */
  120. case -ENOENT:
  121. case -ESHUTDOWN:
  122. au0828_isocdbg("au0828_irq_callback called: status kill\n");
  123. return;
  124. default: /* unknown error */
  125. au0828_isocdbg("urb completition error %d.\n", urb->status);
  126. break;
  127. }
  128. /* Copy data from URB */
  129. spin_lock_irqsave(&dev->slock, flags);
  130. dev->isoc_ctl.isoc_copy(dev, urb);
  131. spin_unlock_irqrestore(&dev->slock, flags);
  132. /* Reset urb buffers */
  133. for (i = 0; i < urb->number_of_packets; i++) {
  134. urb->iso_frame_desc[i].status = 0;
  135. urb->iso_frame_desc[i].actual_length = 0;
  136. }
  137. urb->status = 0;
  138. urb->status = usb_submit_urb(urb, GFP_ATOMIC);
  139. if (urb->status) {
  140. au0828_isocdbg("urb resubmit failed (error=%i)\n",
  141. urb->status);
  142. }
  143. dev->stream_state = STREAM_ON;
  144. }
  145. /*
  146. * Stop and Deallocate URBs
  147. */
  148. static void au0828_uninit_isoc(struct au0828_dev *dev)
  149. {
  150. struct urb *urb;
  151. int i;
  152. au0828_isocdbg("au0828: called au0828_uninit_isoc\n");
  153. dev->isoc_ctl.nfields = -1;
  154. for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
  155. urb = dev->isoc_ctl.urb[i];
  156. if (urb) {
  157. if (!irqs_disabled())
  158. usb_kill_urb(urb);
  159. else
  160. usb_unlink_urb(urb);
  161. if (dev->isoc_ctl.transfer_buffer[i]) {
  162. usb_free_coherent(dev->usbdev,
  163. urb->transfer_buffer_length,
  164. dev->isoc_ctl.transfer_buffer[i],
  165. urb->transfer_dma);
  166. }
  167. usb_free_urb(urb);
  168. dev->isoc_ctl.urb[i] = NULL;
  169. }
  170. dev->isoc_ctl.transfer_buffer[i] = NULL;
  171. }
  172. kfree(dev->isoc_ctl.urb);
  173. kfree(dev->isoc_ctl.transfer_buffer);
  174. dev->isoc_ctl.urb = NULL;
  175. dev->isoc_ctl.transfer_buffer = NULL;
  176. dev->isoc_ctl.num_bufs = 0;
  177. dev->stream_state = STREAM_OFF;
  178. }
  179. /*
  180. * Allocate URBs and start IRQ
  181. */
  182. static int au0828_init_isoc(struct au0828_dev *dev, int max_packets,
  183. int num_bufs, int max_pkt_size,
  184. int (*isoc_copy) (struct au0828_dev *dev, struct urb *urb))
  185. {
  186. struct au0828_dmaqueue *dma_q = &dev->vidq;
  187. int i;
  188. int sb_size, pipe;
  189. struct urb *urb;
  190. int j, k;
  191. int rc;
  192. au0828_isocdbg("au0828: called au0828_prepare_isoc\n");
  193. dev->isoc_ctl.isoc_copy = isoc_copy;
  194. dev->isoc_ctl.num_bufs = num_bufs;
  195. dev->isoc_ctl.urb = kzalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
  196. if (!dev->isoc_ctl.urb) {
  197. au0828_isocdbg("cannot alloc memory for usb buffers\n");
  198. return -ENOMEM;
  199. }
  200. dev->isoc_ctl.transfer_buffer = kzalloc(sizeof(void *)*num_bufs,
  201. GFP_KERNEL);
  202. if (!dev->isoc_ctl.transfer_buffer) {
  203. au0828_isocdbg("cannot allocate memory for usb transfer\n");
  204. kfree(dev->isoc_ctl.urb);
  205. return -ENOMEM;
  206. }
  207. dev->isoc_ctl.max_pkt_size = max_pkt_size;
  208. dev->isoc_ctl.buf = NULL;
  209. sb_size = max_packets * dev->isoc_ctl.max_pkt_size;
  210. /* allocate urbs and transfer buffers */
  211. for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
  212. urb = usb_alloc_urb(max_packets, GFP_KERNEL);
  213. if (!urb) {
  214. au0828_isocdbg("cannot alloc isoc_ctl.urb %i\n", i);
  215. au0828_uninit_isoc(dev);
  216. return -ENOMEM;
  217. }
  218. dev->isoc_ctl.urb[i] = urb;
  219. dev->isoc_ctl.transfer_buffer[i] = usb_alloc_coherent(dev->usbdev,
  220. sb_size, GFP_KERNEL, &urb->transfer_dma);
  221. if (!dev->isoc_ctl.transfer_buffer[i]) {
  222. printk("unable to allocate %i bytes for transfer"
  223. " 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. v4l2_get_timestamp(&vb->timestamp);
  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. static int queue_setup(struct vb2_queue *vq, const void *parg,
  545. unsigned int *nbuffers, unsigned int *nplanes,
  546. unsigned int sizes[], void *alloc_ctxs[])
  547. {
  548. const struct v4l2_format *fmt = parg;
  549. struct au0828_dev *dev = vb2_get_drv_priv(vq);
  550. unsigned long img_size = dev->height * dev->bytesperline;
  551. unsigned long size;
  552. size = fmt ? fmt->fmt.pix.sizeimage : img_size;
  553. if (size < img_size)
  554. return -EINVAL;
  555. *nplanes = 1;
  556. sizes[0] = size;
  557. return 0;
  558. }
  559. static int
  560. buffer_prepare(struct vb2_buffer *vb)
  561. {
  562. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  563. struct au0828_buffer *buf = container_of(vbuf,
  564. struct au0828_buffer, vb);
  565. struct au0828_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
  566. buf->length = dev->height * dev->bytesperline;
  567. if (vb2_plane_size(vb, 0) < buf->length) {
  568. pr_err("%s data will not fit into plane (%lu < %lu)\n",
  569. __func__, vb2_plane_size(vb, 0), buf->length);
  570. return -EINVAL;
  571. }
  572. vb2_set_plane_payload(&buf->vb.vb2_buf, 0, buf->length);
  573. return 0;
  574. }
  575. static void
  576. buffer_queue(struct vb2_buffer *vb)
  577. {
  578. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  579. struct au0828_buffer *buf = container_of(vbuf,
  580. struct au0828_buffer,
  581. vb);
  582. struct au0828_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
  583. struct au0828_dmaqueue *vidq = &dev->vidq;
  584. unsigned long flags = 0;
  585. buf->mem = vb2_plane_vaddr(vb, 0);
  586. buf->length = vb2_plane_size(vb, 0);
  587. spin_lock_irqsave(&dev->slock, flags);
  588. list_add_tail(&buf->list, &vidq->active);
  589. spin_unlock_irqrestore(&dev->slock, flags);
  590. }
  591. static int au0828_i2s_init(struct au0828_dev *dev)
  592. {
  593. /* Enable i2s mode */
  594. au0828_writereg(dev, AU0828_AUDIOCTRL_50C, 0x01);
  595. return 0;
  596. }
  597. /*
  598. * Auvitek au0828 analog stream enable
  599. */
  600. static int au0828_analog_stream_enable(struct au0828_dev *d)
  601. {
  602. struct usb_interface *iface;
  603. int ret, h, w;
  604. dprintk(1, "au0828_analog_stream_enable called\n");
  605. iface = usb_ifnum_to_if(d->usbdev, 0);
  606. if (iface && iface->cur_altsetting->desc.bAlternateSetting != 5) {
  607. dprintk(1, "Changing intf#0 to alt 5\n");
  608. /* set au0828 interface0 to AS5 here again */
  609. ret = usb_set_interface(d->usbdev, 0, 5);
  610. if (ret < 0) {
  611. pr_info("Au0828 can't set alt setting to 5!\n");
  612. return -EBUSY;
  613. }
  614. }
  615. h = d->height / 2 + 2;
  616. w = d->width * 2;
  617. au0828_writereg(d, AU0828_SENSORCTRL_VBI_103, 0x00);
  618. au0828_writereg(d, 0x106, 0x00);
  619. /* set x position */
  620. au0828_writereg(d, 0x110, 0x00);
  621. au0828_writereg(d, 0x111, 0x00);
  622. au0828_writereg(d, 0x114, w & 0xff);
  623. au0828_writereg(d, 0x115, w >> 8);
  624. /* set y position */
  625. au0828_writereg(d, 0x112, 0x00);
  626. au0828_writereg(d, 0x113, 0x00);
  627. au0828_writereg(d, 0x116, h & 0xff);
  628. au0828_writereg(d, 0x117, h >> 8);
  629. au0828_writereg(d, AU0828_SENSORCTRL_100, 0xb3);
  630. return 0;
  631. }
  632. static int au0828_analog_stream_disable(struct au0828_dev *d)
  633. {
  634. dprintk(1, "au0828_analog_stream_disable called\n");
  635. au0828_writereg(d, AU0828_SENSORCTRL_100, 0x0);
  636. return 0;
  637. }
  638. static void au0828_analog_stream_reset(struct au0828_dev *dev)
  639. {
  640. dprintk(1, "au0828_analog_stream_reset called\n");
  641. au0828_writereg(dev, AU0828_SENSORCTRL_100, 0x0);
  642. mdelay(30);
  643. au0828_writereg(dev, AU0828_SENSORCTRL_100, 0xb3);
  644. }
  645. /*
  646. * Some operations needs to stop current streaming
  647. */
  648. static int au0828_stream_interrupt(struct au0828_dev *dev)
  649. {
  650. int ret = 0;
  651. dev->stream_state = STREAM_INTERRUPT;
  652. if (test_bit(DEV_DISCONNECTED, &dev->dev_state))
  653. return -ENODEV;
  654. else if (ret) {
  655. set_bit(DEV_MISCONFIGURED, &dev->dev_state);
  656. dprintk(1, "%s device is misconfigured!\n", __func__);
  657. return ret;
  658. }
  659. return 0;
  660. }
  661. int au0828_start_analog_streaming(struct vb2_queue *vq, unsigned int count)
  662. {
  663. struct au0828_dev *dev = vb2_get_drv_priv(vq);
  664. int rc = 0;
  665. dprintk(1, "au0828_start_analog_streaming called %d\n",
  666. dev->streaming_users);
  667. if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  668. dev->frame_count = 0;
  669. else
  670. dev->vbi_frame_count = 0;
  671. if (dev->streaming_users == 0) {
  672. /* If we were doing ac97 instead of i2s, it would go here...*/
  673. au0828_i2s_init(dev);
  674. rc = au0828_init_isoc(dev, AU0828_ISO_PACKETS_PER_URB,
  675. AU0828_MAX_ISO_BUFS, dev->max_pkt_size,
  676. au0828_isoc_copy);
  677. if (rc < 0) {
  678. pr_info("au0828_init_isoc failed\n");
  679. return rc;
  680. }
  681. if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  682. v4l2_device_call_all(&dev->v4l2_dev, 0, video,
  683. s_stream, 1);
  684. dev->vid_timeout_running = 1;
  685. mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
  686. } else if (vq->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
  687. dev->vbi_timeout_running = 1;
  688. mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
  689. }
  690. }
  691. dev->streaming_users++;
  692. return rc;
  693. }
  694. static void au0828_stop_streaming(struct vb2_queue *vq)
  695. {
  696. struct au0828_dev *dev = vb2_get_drv_priv(vq);
  697. struct au0828_dmaqueue *vidq = &dev->vidq;
  698. unsigned long flags = 0;
  699. dprintk(1, "au0828_stop_streaming called %d\n", dev->streaming_users);
  700. if (dev->streaming_users-- == 1)
  701. au0828_uninit_isoc(dev);
  702. v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0);
  703. dev->vid_timeout_running = 0;
  704. del_timer_sync(&dev->vid_timeout);
  705. spin_lock_irqsave(&dev->slock, flags);
  706. if (dev->isoc_ctl.buf != NULL) {
  707. vb2_buffer_done(&dev->isoc_ctl.buf->vb.vb2_buf,
  708. VB2_BUF_STATE_ERROR);
  709. dev->isoc_ctl.buf = NULL;
  710. }
  711. while (!list_empty(&vidq->active)) {
  712. struct au0828_buffer *buf;
  713. buf = list_entry(vidq->active.next, struct au0828_buffer, list);
  714. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  715. list_del(&buf->list);
  716. }
  717. spin_unlock_irqrestore(&dev->slock, flags);
  718. }
  719. void au0828_stop_vbi_streaming(struct vb2_queue *vq)
  720. {
  721. struct au0828_dev *dev = vb2_get_drv_priv(vq);
  722. struct au0828_dmaqueue *vbiq = &dev->vbiq;
  723. unsigned long flags = 0;
  724. dprintk(1, "au0828_stop_vbi_streaming called %d\n",
  725. dev->streaming_users);
  726. if (dev->streaming_users-- == 1)
  727. au0828_uninit_isoc(dev);
  728. spin_lock_irqsave(&dev->slock, flags);
  729. if (dev->isoc_ctl.vbi_buf != NULL) {
  730. vb2_buffer_done(&dev->isoc_ctl.vbi_buf->vb.vb2_buf,
  731. VB2_BUF_STATE_ERROR);
  732. dev->isoc_ctl.vbi_buf = NULL;
  733. }
  734. while (!list_empty(&vbiq->active)) {
  735. struct au0828_buffer *buf;
  736. buf = list_entry(vbiq->active.next, struct au0828_buffer, list);
  737. list_del(&buf->list);
  738. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  739. }
  740. spin_unlock_irqrestore(&dev->slock, flags);
  741. dev->vbi_timeout_running = 0;
  742. del_timer_sync(&dev->vbi_timeout);
  743. }
  744. static struct vb2_ops au0828_video_qops = {
  745. .queue_setup = queue_setup,
  746. .buf_prepare = buffer_prepare,
  747. .buf_queue = buffer_queue,
  748. .start_streaming = au0828_start_analog_streaming,
  749. .stop_streaming = au0828_stop_streaming,
  750. .wait_prepare = vb2_ops_wait_prepare,
  751. .wait_finish = vb2_ops_wait_finish,
  752. };
  753. /* ------------------------------------------------------------------
  754. V4L2 interface
  755. ------------------------------------------------------------------*/
  756. /*
  757. * au0828_analog_unregister
  758. * unregister v4l2 devices
  759. */
  760. void au0828_analog_unregister(struct au0828_dev *dev)
  761. {
  762. dprintk(1, "au0828_analog_unregister called\n");
  763. mutex_lock(&au0828_sysfs_lock);
  764. video_unregister_device(&dev->vdev);
  765. video_unregister_device(&dev->vbi_dev);
  766. mutex_unlock(&au0828_sysfs_lock);
  767. }
  768. /* This function ensures that video frames continue to be delivered even if
  769. the ITU-656 input isn't receiving any data (thereby preventing applications
  770. such as tvtime from hanging) */
  771. static void au0828_vid_buffer_timeout(unsigned long data)
  772. {
  773. struct au0828_dev *dev = (struct au0828_dev *) data;
  774. struct au0828_dmaqueue *dma_q = &dev->vidq;
  775. struct au0828_buffer *buf;
  776. unsigned char *vid_data;
  777. unsigned long flags = 0;
  778. spin_lock_irqsave(&dev->slock, flags);
  779. buf = dev->isoc_ctl.buf;
  780. if (buf != NULL) {
  781. vid_data = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
  782. memset(vid_data, 0x00, buf->length); /* Blank green frame */
  783. buffer_filled(dev, dma_q, buf);
  784. }
  785. get_next_buf(dma_q, &buf);
  786. if (dev->vid_timeout_running == 1)
  787. mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
  788. spin_unlock_irqrestore(&dev->slock, flags);
  789. }
  790. static void au0828_vbi_buffer_timeout(unsigned long data)
  791. {
  792. struct au0828_dev *dev = (struct au0828_dev *) data;
  793. struct au0828_dmaqueue *dma_q = &dev->vbiq;
  794. struct au0828_buffer *buf;
  795. unsigned char *vbi_data;
  796. unsigned long flags = 0;
  797. spin_lock_irqsave(&dev->slock, flags);
  798. buf = dev->isoc_ctl.vbi_buf;
  799. if (buf != NULL) {
  800. vbi_data = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
  801. memset(vbi_data, 0x00, buf->length);
  802. buffer_filled(dev, dma_q, buf);
  803. }
  804. vbi_get_next_buf(dma_q, &buf);
  805. if (dev->vbi_timeout_running == 1)
  806. mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
  807. spin_unlock_irqrestore(&dev->slock, flags);
  808. }
  809. static int au0828_v4l2_open(struct file *filp)
  810. {
  811. struct au0828_dev *dev = video_drvdata(filp);
  812. int ret;
  813. dprintk(1,
  814. "%s called std_set %d dev_state %ld stream users %d users %d\n",
  815. __func__, dev->std_set_in_tuner_core, dev->dev_state,
  816. dev->streaming_users, dev->users);
  817. if (mutex_lock_interruptible(&dev->lock))
  818. return -ERESTARTSYS;
  819. ret = v4l2_fh_open(filp);
  820. if (ret) {
  821. au0828_isocdbg("%s: v4l2_fh_open() returned error %d\n",
  822. __func__, ret);
  823. mutex_unlock(&dev->lock);
  824. return ret;
  825. }
  826. if (dev->users == 0) {
  827. au0828_analog_stream_enable(dev);
  828. au0828_analog_stream_reset(dev);
  829. dev->stream_state = STREAM_OFF;
  830. set_bit(DEV_INITIALIZED, &dev->dev_state);
  831. }
  832. dev->users++;
  833. mutex_unlock(&dev->lock);
  834. return ret;
  835. }
  836. static int au0828_v4l2_close(struct file *filp)
  837. {
  838. int ret;
  839. struct au0828_dev *dev = video_drvdata(filp);
  840. struct video_device *vdev = video_devdata(filp);
  841. dprintk(1,
  842. "%s called std_set %d dev_state %ld stream users %d users %d\n",
  843. __func__, dev->std_set_in_tuner_core, dev->dev_state,
  844. dev->streaming_users, dev->users);
  845. mutex_lock(&dev->lock);
  846. if (vdev->vfl_type == VFL_TYPE_GRABBER && dev->vid_timeout_running) {
  847. /* Cancel timeout thread in case they didn't call streamoff */
  848. dev->vid_timeout_running = 0;
  849. del_timer_sync(&dev->vid_timeout);
  850. } else if (vdev->vfl_type == VFL_TYPE_VBI &&
  851. dev->vbi_timeout_running) {
  852. /* Cancel timeout thread in case they didn't call streamoff */
  853. dev->vbi_timeout_running = 0;
  854. del_timer_sync(&dev->vbi_timeout);
  855. }
  856. if (test_bit(DEV_DISCONNECTED, &dev->dev_state))
  857. goto end;
  858. if (dev->users == 1) {
  859. /* Save some power by putting tuner to sleep */
  860. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0);
  861. dev->std_set_in_tuner_core = 0;
  862. /* When close the device, set the usb intf0 into alt0 to free
  863. USB bandwidth */
  864. ret = usb_set_interface(dev->usbdev, 0, 0);
  865. if (ret < 0)
  866. pr_info("Au0828 can't set alternate to 0!\n");
  867. }
  868. end:
  869. _vb2_fop_release(filp, NULL);
  870. dev->users--;
  871. mutex_unlock(&dev->lock);
  872. return 0;
  873. }
  874. /* Must be called with dev->lock held */
  875. static void au0828_init_tuner(struct au0828_dev *dev)
  876. {
  877. struct v4l2_frequency f = {
  878. .frequency = dev->ctrl_freq,
  879. .type = V4L2_TUNER_ANALOG_TV,
  880. };
  881. dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
  882. dev->std_set_in_tuner_core, dev->dev_state);
  883. if (dev->std_set_in_tuner_core)
  884. return;
  885. dev->std_set_in_tuner_core = 1;
  886. i2c_gate_ctrl(dev, 1);
  887. /* If we've never sent the standard in tuner core, do so now.
  888. We don't do this at device probe because we don't want to
  889. incur the cost of a firmware load */
  890. v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, dev->std);
  891. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
  892. i2c_gate_ctrl(dev, 0);
  893. }
  894. static int au0828_set_format(struct au0828_dev *dev, unsigned int cmd,
  895. struct v4l2_format *format)
  896. {
  897. int ret;
  898. int width = format->fmt.pix.width;
  899. int height = format->fmt.pix.height;
  900. /* If they are demanding a format other than the one we support,
  901. bail out (tvtime asks for UYVY and then retries with YUYV) */
  902. if (format->fmt.pix.pixelformat != V4L2_PIX_FMT_UYVY)
  903. return -EINVAL;
  904. /* format->fmt.pix.width only support 720 and height 480 */
  905. if (width != 720)
  906. width = 720;
  907. if (height != 480)
  908. height = 480;
  909. format->fmt.pix.width = width;
  910. format->fmt.pix.height = height;
  911. format->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
  912. format->fmt.pix.bytesperline = width * 2;
  913. format->fmt.pix.sizeimage = width * height * 2;
  914. format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  915. format->fmt.pix.field = V4L2_FIELD_INTERLACED;
  916. format->fmt.pix.priv = 0;
  917. if (cmd == VIDIOC_TRY_FMT)
  918. return 0;
  919. /* maybe set new image format, driver current only support 720*480 */
  920. dev->width = width;
  921. dev->height = height;
  922. dev->frame_size = width * height * 2;
  923. dev->field_size = width * height;
  924. dev->bytesperline = width * 2;
  925. if (dev->stream_state == STREAM_ON) {
  926. dprintk(1, "VIDIOC_SET_FMT: interrupting stream!\n");
  927. ret = au0828_stream_interrupt(dev);
  928. if (ret != 0) {
  929. dprintk(1, "error interrupting video stream!\n");
  930. return ret;
  931. }
  932. }
  933. au0828_analog_stream_enable(dev);
  934. return 0;
  935. }
  936. static int vidioc_querycap(struct file *file, void *priv,
  937. struct v4l2_capability *cap)
  938. {
  939. struct video_device *vdev = video_devdata(file);
  940. struct au0828_dev *dev = video_drvdata(file);
  941. dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
  942. dev->std_set_in_tuner_core, dev->dev_state);
  943. strlcpy(cap->driver, "au0828", sizeof(cap->driver));
  944. strlcpy(cap->card, dev->board.name, sizeof(cap->card));
  945. usb_make_path(dev->usbdev, cap->bus_info, sizeof(cap->bus_info));
  946. /* set the device capabilities */
  947. cap->device_caps = V4L2_CAP_AUDIO |
  948. V4L2_CAP_READWRITE |
  949. V4L2_CAP_STREAMING |
  950. V4L2_CAP_TUNER;
  951. if (vdev->vfl_type == VFL_TYPE_GRABBER)
  952. cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
  953. else
  954. cap->device_caps |= V4L2_CAP_VBI_CAPTURE;
  955. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS |
  956. V4L2_CAP_VBI_CAPTURE | V4L2_CAP_VIDEO_CAPTURE;
  957. return 0;
  958. }
  959. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  960. struct v4l2_fmtdesc *f)
  961. {
  962. if (f->index)
  963. return -EINVAL;
  964. dprintk(1, "%s called\n", __func__);
  965. f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  966. strcpy(f->description, "Packed YUV2");
  967. f->flags = 0;
  968. f->pixelformat = V4L2_PIX_FMT_UYVY;
  969. return 0;
  970. }
  971. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  972. struct v4l2_format *f)
  973. {
  974. struct au0828_dev *dev = video_drvdata(file);
  975. dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
  976. dev->std_set_in_tuner_core, dev->dev_state);
  977. f->fmt.pix.width = dev->width;
  978. f->fmt.pix.height = dev->height;
  979. f->fmt.pix.pixelformat = V4L2_PIX_FMT_UYVY;
  980. f->fmt.pix.bytesperline = dev->bytesperline;
  981. f->fmt.pix.sizeimage = dev->frame_size;
  982. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; /* NTSC/PAL */
  983. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  984. f->fmt.pix.priv = 0;
  985. return 0;
  986. }
  987. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  988. struct v4l2_format *f)
  989. {
  990. struct au0828_dev *dev = video_drvdata(file);
  991. dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
  992. dev->std_set_in_tuner_core, dev->dev_state);
  993. return au0828_set_format(dev, VIDIOC_TRY_FMT, f);
  994. }
  995. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  996. struct v4l2_format *f)
  997. {
  998. struct au0828_dev *dev = video_drvdata(file);
  999. int rc;
  1000. dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
  1001. dev->std_set_in_tuner_core, dev->dev_state);
  1002. rc = check_dev(dev);
  1003. if (rc < 0)
  1004. return rc;
  1005. if (vb2_is_busy(&dev->vb_vidq)) {
  1006. pr_info("%s queue busy\n", __func__);
  1007. rc = -EBUSY;
  1008. goto out;
  1009. }
  1010. rc = au0828_set_format(dev, VIDIOC_S_FMT, f);
  1011. out:
  1012. return rc;
  1013. }
  1014. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
  1015. {
  1016. struct au0828_dev *dev = video_drvdata(file);
  1017. dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
  1018. dev->std_set_in_tuner_core, dev->dev_state);
  1019. if (norm == dev->std)
  1020. return 0;
  1021. if (dev->streaming_users > 0)
  1022. return -EBUSY;
  1023. dev->std = norm;
  1024. au0828_init_tuner(dev);
  1025. i2c_gate_ctrl(dev, 1);
  1026. /*
  1027. * FIXME: when we support something other than 60Hz standards,
  1028. * we are going to have to make the au0828 bridge adjust the size
  1029. * of its capture buffer, which is currently hardcoded at 720x480
  1030. */
  1031. v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, norm);
  1032. i2c_gate_ctrl(dev, 0);
  1033. return 0;
  1034. }
  1035. static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm)
  1036. {
  1037. struct au0828_dev *dev = video_drvdata(file);
  1038. dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
  1039. dev->std_set_in_tuner_core, dev->dev_state);
  1040. *norm = dev->std;
  1041. return 0;
  1042. }
  1043. static int vidioc_enum_input(struct file *file, void *priv,
  1044. struct v4l2_input *input)
  1045. {
  1046. struct au0828_dev *dev = video_drvdata(file);
  1047. unsigned int tmp;
  1048. static const char *inames[] = {
  1049. [AU0828_VMUX_UNDEFINED] = "Undefined",
  1050. [AU0828_VMUX_COMPOSITE] = "Composite",
  1051. [AU0828_VMUX_SVIDEO] = "S-Video",
  1052. [AU0828_VMUX_CABLE] = "Cable TV",
  1053. [AU0828_VMUX_TELEVISION] = "Television",
  1054. [AU0828_VMUX_DVB] = "DVB",
  1055. [AU0828_VMUX_DEBUG] = "tv debug"
  1056. };
  1057. dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
  1058. dev->std_set_in_tuner_core, dev->dev_state);
  1059. tmp = input->index;
  1060. if (tmp >= AU0828_MAX_INPUT)
  1061. return -EINVAL;
  1062. if (AUVI_INPUT(tmp).type == 0)
  1063. return -EINVAL;
  1064. input->index = tmp;
  1065. strcpy(input->name, inames[AUVI_INPUT(tmp).type]);
  1066. if ((AUVI_INPUT(tmp).type == AU0828_VMUX_TELEVISION) ||
  1067. (AUVI_INPUT(tmp).type == AU0828_VMUX_CABLE)) {
  1068. input->type |= V4L2_INPUT_TYPE_TUNER;
  1069. input->audioset = 1;
  1070. } else {
  1071. input->type |= V4L2_INPUT_TYPE_CAMERA;
  1072. input->audioset = 2;
  1073. }
  1074. input->std = dev->vdev.tvnorms;
  1075. return 0;
  1076. }
  1077. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  1078. {
  1079. struct au0828_dev *dev = video_drvdata(file);
  1080. dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
  1081. dev->std_set_in_tuner_core, dev->dev_state);
  1082. *i = dev->ctrl_input;
  1083. return 0;
  1084. }
  1085. static void au0828_s_input(struct au0828_dev *dev, int index)
  1086. {
  1087. int i;
  1088. dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
  1089. dev->std_set_in_tuner_core, dev->dev_state);
  1090. switch (AUVI_INPUT(index).type) {
  1091. case AU0828_VMUX_SVIDEO:
  1092. dev->input_type = AU0828_VMUX_SVIDEO;
  1093. dev->ctrl_ainput = 1;
  1094. break;
  1095. case AU0828_VMUX_COMPOSITE:
  1096. dev->input_type = AU0828_VMUX_COMPOSITE;
  1097. dev->ctrl_ainput = 1;
  1098. break;
  1099. case AU0828_VMUX_TELEVISION:
  1100. dev->input_type = AU0828_VMUX_TELEVISION;
  1101. dev->ctrl_ainput = 0;
  1102. break;
  1103. default:
  1104. dprintk(1, "unknown input type set [%d]\n",
  1105. AUVI_INPUT(index).type);
  1106. break;
  1107. }
  1108. v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
  1109. AUVI_INPUT(index).vmux, 0, 0);
  1110. for (i = 0; i < AU0828_MAX_INPUT; i++) {
  1111. int enable = 0;
  1112. if (AUVI_INPUT(i).audio_setup == NULL)
  1113. continue;
  1114. if (i == index)
  1115. enable = 1;
  1116. else
  1117. enable = 0;
  1118. if (enable) {
  1119. (AUVI_INPUT(i).audio_setup)(dev, enable);
  1120. } else {
  1121. /* Make sure we leave it turned on if some
  1122. other input is routed to this callback */
  1123. if ((AUVI_INPUT(i).audio_setup) !=
  1124. ((AUVI_INPUT(index).audio_setup))) {
  1125. (AUVI_INPUT(i).audio_setup)(dev, enable);
  1126. }
  1127. }
  1128. }
  1129. v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
  1130. AUVI_INPUT(index).amux, 0, 0);
  1131. }
  1132. static int vidioc_s_input(struct file *file, void *priv, unsigned int index)
  1133. {
  1134. struct au0828_dev *dev = video_drvdata(file);
  1135. dprintk(1, "VIDIOC_S_INPUT in function %s, input=%d\n", __func__,
  1136. index);
  1137. if (index >= AU0828_MAX_INPUT)
  1138. return -EINVAL;
  1139. if (AUVI_INPUT(index).type == 0)
  1140. return -EINVAL;
  1141. dev->ctrl_input = index;
  1142. au0828_s_input(dev, index);
  1143. return 0;
  1144. }
  1145. static int vidioc_enumaudio(struct file *file, void *priv, struct v4l2_audio *a)
  1146. {
  1147. if (a->index > 1)
  1148. return -EINVAL;
  1149. dprintk(1, "%s called\n", __func__);
  1150. if (a->index == 0)
  1151. strcpy(a->name, "Television");
  1152. else
  1153. strcpy(a->name, "Line in");
  1154. a->capability = V4L2_AUDCAP_STEREO;
  1155. return 0;
  1156. }
  1157. static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  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. a->index = dev->ctrl_ainput;
  1163. if (a->index == 0)
  1164. strcpy(a->name, "Television");
  1165. else
  1166. strcpy(a->name, "Line in");
  1167. a->capability = V4L2_AUDCAP_STEREO;
  1168. return 0;
  1169. }
  1170. static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a)
  1171. {
  1172. struct au0828_dev *dev = video_drvdata(file);
  1173. if (a->index != dev->ctrl_ainput)
  1174. return -EINVAL;
  1175. dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
  1176. dev->std_set_in_tuner_core, dev->dev_state);
  1177. return 0;
  1178. }
  1179. static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
  1180. {
  1181. struct au0828_dev *dev = video_drvdata(file);
  1182. if (t->index != 0)
  1183. return -EINVAL;
  1184. dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
  1185. dev->std_set_in_tuner_core, dev->dev_state);
  1186. strcpy(t->name, "Auvitek tuner");
  1187. au0828_init_tuner(dev);
  1188. i2c_gate_ctrl(dev, 1);
  1189. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
  1190. i2c_gate_ctrl(dev, 0);
  1191. return 0;
  1192. }
  1193. static int vidioc_s_tuner(struct file *file, void *priv,
  1194. const struct v4l2_tuner *t)
  1195. {
  1196. struct au0828_dev *dev = video_drvdata(file);
  1197. if (t->index != 0)
  1198. return -EINVAL;
  1199. dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
  1200. dev->std_set_in_tuner_core, dev->dev_state);
  1201. au0828_init_tuner(dev);
  1202. i2c_gate_ctrl(dev, 1);
  1203. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
  1204. i2c_gate_ctrl(dev, 0);
  1205. dprintk(1, "VIDIOC_S_TUNER: signal = %x, afc = %x\n", t->signal,
  1206. t->afc);
  1207. return 0;
  1208. }
  1209. static int vidioc_g_frequency(struct file *file, void *priv,
  1210. struct v4l2_frequency *freq)
  1211. {
  1212. struct au0828_dev *dev = video_drvdata(file);
  1213. if (freq->tuner != 0)
  1214. return -EINVAL;
  1215. dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
  1216. dev->std_set_in_tuner_core, dev->dev_state);
  1217. freq->frequency = dev->ctrl_freq;
  1218. return 0;
  1219. }
  1220. static int vidioc_s_frequency(struct file *file, void *priv,
  1221. const struct v4l2_frequency *freq)
  1222. {
  1223. struct au0828_dev *dev = video_drvdata(file);
  1224. struct v4l2_frequency new_freq = *freq;
  1225. if (freq->tuner != 0)
  1226. return -EINVAL;
  1227. dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
  1228. dev->std_set_in_tuner_core, dev->dev_state);
  1229. au0828_init_tuner(dev);
  1230. i2c_gate_ctrl(dev, 1);
  1231. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, freq);
  1232. /* Get the actual set (and possibly clamped) frequency */
  1233. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, &new_freq);
  1234. dev->ctrl_freq = new_freq.frequency;
  1235. i2c_gate_ctrl(dev, 0);
  1236. au0828_analog_stream_reset(dev);
  1237. return 0;
  1238. }
  1239. /* RAW VBI ioctls */
  1240. static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
  1241. struct v4l2_format *format)
  1242. {
  1243. struct au0828_dev *dev = video_drvdata(file);
  1244. dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
  1245. dev->std_set_in_tuner_core, dev->dev_state);
  1246. format->fmt.vbi.samples_per_line = dev->vbi_width;
  1247. format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  1248. format->fmt.vbi.offset = 0;
  1249. format->fmt.vbi.flags = 0;
  1250. format->fmt.vbi.sampling_rate = 6750000 * 4 / 2;
  1251. format->fmt.vbi.count[0] = dev->vbi_height;
  1252. format->fmt.vbi.count[1] = dev->vbi_height;
  1253. format->fmt.vbi.start[0] = 21;
  1254. format->fmt.vbi.start[1] = 284;
  1255. memset(format->fmt.vbi.reserved, 0, sizeof(format->fmt.vbi.reserved));
  1256. return 0;
  1257. }
  1258. static int vidioc_cropcap(struct file *file, void *priv,
  1259. struct v4l2_cropcap *cc)
  1260. {
  1261. struct au0828_dev *dev = video_drvdata(file);
  1262. if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  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. cc->bounds.left = 0;
  1267. cc->bounds.top = 0;
  1268. cc->bounds.width = dev->width;
  1269. cc->bounds.height = dev->height;
  1270. cc->defrect = cc->bounds;
  1271. cc->pixelaspect.numerator = 54;
  1272. cc->pixelaspect.denominator = 59;
  1273. return 0;
  1274. }
  1275. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1276. static int vidioc_g_register(struct file *file, void *priv,
  1277. struct v4l2_dbg_register *reg)
  1278. {
  1279. struct au0828_dev *dev = video_drvdata(file);
  1280. dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
  1281. dev->std_set_in_tuner_core, dev->dev_state);
  1282. reg->val = au0828_read(dev, reg->reg);
  1283. reg->size = 1;
  1284. return 0;
  1285. }
  1286. static int vidioc_s_register(struct file *file, void *priv,
  1287. const struct v4l2_dbg_register *reg)
  1288. {
  1289. struct au0828_dev *dev = video_drvdata(file);
  1290. dprintk(1, "%s called std_set %d dev_state %ld\n", __func__,
  1291. dev->std_set_in_tuner_core, dev->dev_state);
  1292. return au0828_writereg(dev, reg->reg, reg->val);
  1293. }
  1294. #endif
  1295. static int vidioc_log_status(struct file *file, void *fh)
  1296. {
  1297. struct video_device *vdev = video_devdata(file);
  1298. dprintk(1, "%s called\n", __func__);
  1299. v4l2_ctrl_log_status(file, fh);
  1300. v4l2_device_call_all(vdev->v4l2_dev, 0, core, log_status);
  1301. return 0;
  1302. }
  1303. void au0828_v4l2_suspend(struct au0828_dev *dev)
  1304. {
  1305. struct urb *urb;
  1306. int i;
  1307. pr_info("stopping V4L2\n");
  1308. if (dev->stream_state == STREAM_ON) {
  1309. pr_info("stopping V4L2 active URBs\n");
  1310. au0828_analog_stream_disable(dev);
  1311. /* stop urbs */
  1312. for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
  1313. urb = dev->isoc_ctl.urb[i];
  1314. if (urb) {
  1315. if (!irqs_disabled())
  1316. usb_kill_urb(urb);
  1317. else
  1318. usb_unlink_urb(urb);
  1319. }
  1320. }
  1321. }
  1322. if (dev->vid_timeout_running)
  1323. del_timer_sync(&dev->vid_timeout);
  1324. if (dev->vbi_timeout_running)
  1325. del_timer_sync(&dev->vbi_timeout);
  1326. }
  1327. void au0828_v4l2_resume(struct au0828_dev *dev)
  1328. {
  1329. int i, rc;
  1330. pr_info("restarting V4L2\n");
  1331. if (dev->stream_state == STREAM_ON) {
  1332. au0828_stream_interrupt(dev);
  1333. au0828_init_tuner(dev);
  1334. }
  1335. if (dev->vid_timeout_running)
  1336. mod_timer(&dev->vid_timeout, jiffies + (HZ / 10));
  1337. if (dev->vbi_timeout_running)
  1338. mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10));
  1339. /* If we were doing ac97 instead of i2s, it would go here...*/
  1340. au0828_i2s_init(dev);
  1341. au0828_analog_stream_enable(dev);
  1342. if (!(dev->stream_state == STREAM_ON)) {
  1343. au0828_analog_stream_reset(dev);
  1344. /* submit urbs */
  1345. for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
  1346. rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC);
  1347. if (rc) {
  1348. au0828_isocdbg("submit of urb %i failed (error=%i)\n",
  1349. i, rc);
  1350. au0828_uninit_isoc(dev);
  1351. }
  1352. }
  1353. }
  1354. }
  1355. static struct v4l2_file_operations au0828_v4l_fops = {
  1356. .owner = THIS_MODULE,
  1357. .open = au0828_v4l2_open,
  1358. .release = au0828_v4l2_close,
  1359. .read = vb2_fop_read,
  1360. .poll = vb2_fop_poll,
  1361. .mmap = vb2_fop_mmap,
  1362. .unlocked_ioctl = video_ioctl2,
  1363. };
  1364. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1365. .vidioc_querycap = vidioc_querycap,
  1366. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1367. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1368. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1369. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1370. .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
  1371. .vidioc_try_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
  1372. .vidioc_s_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
  1373. .vidioc_enumaudio = vidioc_enumaudio,
  1374. .vidioc_g_audio = vidioc_g_audio,
  1375. .vidioc_s_audio = vidioc_s_audio,
  1376. .vidioc_cropcap = vidioc_cropcap,
  1377. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  1378. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  1379. .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  1380. .vidioc_querybuf = vb2_ioctl_querybuf,
  1381. .vidioc_qbuf = vb2_ioctl_qbuf,
  1382. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  1383. .vidioc_expbuf = vb2_ioctl_expbuf,
  1384. .vidioc_s_std = vidioc_s_std,
  1385. .vidioc_g_std = vidioc_g_std,
  1386. .vidioc_enum_input = vidioc_enum_input,
  1387. .vidioc_g_input = vidioc_g_input,
  1388. .vidioc_s_input = vidioc_s_input,
  1389. .vidioc_streamon = vb2_ioctl_streamon,
  1390. .vidioc_streamoff = vb2_ioctl_streamoff,
  1391. .vidioc_g_tuner = vidioc_g_tuner,
  1392. .vidioc_s_tuner = vidioc_s_tuner,
  1393. .vidioc_g_frequency = vidioc_g_frequency,
  1394. .vidioc_s_frequency = vidioc_s_frequency,
  1395. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1396. .vidioc_g_register = vidioc_g_register,
  1397. .vidioc_s_register = vidioc_s_register,
  1398. #endif
  1399. .vidioc_log_status = vidioc_log_status,
  1400. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1401. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1402. };
  1403. static const struct video_device au0828_video_template = {
  1404. .fops = &au0828_v4l_fops,
  1405. .release = video_device_release_empty,
  1406. .ioctl_ops = &video_ioctl_ops,
  1407. .tvnorms = V4L2_STD_NTSC_M | V4L2_STD_PAL_M,
  1408. };
  1409. static int au0828_vb2_setup(struct au0828_dev *dev)
  1410. {
  1411. int rc;
  1412. struct vb2_queue *q;
  1413. /* Setup Videobuf2 for Video capture */
  1414. q = &dev->vb_vidq;
  1415. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1416. q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
  1417. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1418. q->drv_priv = dev;
  1419. q->buf_struct_size = sizeof(struct au0828_buffer);
  1420. q->ops = &au0828_video_qops;
  1421. q->mem_ops = &vb2_vmalloc_memops;
  1422. rc = vb2_queue_init(q);
  1423. if (rc < 0)
  1424. return rc;
  1425. /* Setup Videobuf2 for VBI capture */
  1426. q = &dev->vb_vbiq;
  1427. q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
  1428. q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
  1429. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  1430. q->drv_priv = dev;
  1431. q->buf_struct_size = sizeof(struct au0828_buffer);
  1432. q->ops = &au0828_vbi_qops;
  1433. q->mem_ops = &vb2_vmalloc_memops;
  1434. rc = vb2_queue_init(q);
  1435. if (rc < 0)
  1436. return rc;
  1437. return 0;
  1438. }
  1439. /**************************************************************************/
  1440. int au0828_analog_register(struct au0828_dev *dev,
  1441. struct usb_interface *interface)
  1442. {
  1443. int retval = -ENOMEM;
  1444. struct usb_host_interface *iface_desc;
  1445. struct usb_endpoint_descriptor *endpoint;
  1446. int i, ret;
  1447. dprintk(1, "au0828_analog_register called for intf#%d!\n",
  1448. interface->cur_altsetting->desc.bInterfaceNumber);
  1449. /* set au0828 usb interface0 to as5 */
  1450. retval = usb_set_interface(dev->usbdev,
  1451. interface->cur_altsetting->desc.bInterfaceNumber, 5);
  1452. if (retval != 0) {
  1453. pr_info("Failure setting usb interface0 to as5\n");
  1454. return retval;
  1455. }
  1456. /* Figure out which endpoint has the isoc interface */
  1457. iface_desc = interface->cur_altsetting;
  1458. for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
  1459. endpoint = &iface_desc->endpoint[i].desc;
  1460. if (((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
  1461. == USB_DIR_IN) &&
  1462. ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
  1463. == USB_ENDPOINT_XFER_ISOC)) {
  1464. /* we find our isoc in endpoint */
  1465. u16 tmp = le16_to_cpu(endpoint->wMaxPacketSize);
  1466. dev->max_pkt_size = (tmp & 0x07ff) *
  1467. (((tmp & 0x1800) >> 11) + 1);
  1468. dev->isoc_in_endpointaddr = endpoint->bEndpointAddress;
  1469. dprintk(1,
  1470. "Found isoc endpoint 0x%02x, max size = %d\n",
  1471. dev->isoc_in_endpointaddr, dev->max_pkt_size);
  1472. }
  1473. }
  1474. if (!(dev->isoc_in_endpointaddr)) {
  1475. pr_info("Could not locate isoc endpoint\n");
  1476. return -ENODEV;
  1477. }
  1478. init_waitqueue_head(&dev->open);
  1479. spin_lock_init(&dev->slock);
  1480. /* init video dma queues */
  1481. INIT_LIST_HEAD(&dev->vidq.active);
  1482. INIT_LIST_HEAD(&dev->vbiq.active);
  1483. setup_timer(&dev->vid_timeout, au0828_vid_buffer_timeout,
  1484. (unsigned long)dev);
  1485. setup_timer(&dev->vbi_timeout, au0828_vbi_buffer_timeout,
  1486. (unsigned long)dev);
  1487. dev->width = NTSC_STD_W;
  1488. dev->height = NTSC_STD_H;
  1489. dev->field_size = dev->width * dev->height;
  1490. dev->frame_size = dev->field_size << 1;
  1491. dev->bytesperline = dev->width << 1;
  1492. dev->vbi_width = 720;
  1493. dev->vbi_height = 1;
  1494. dev->ctrl_ainput = 0;
  1495. dev->ctrl_freq = 960;
  1496. dev->std = V4L2_STD_NTSC_M;
  1497. au0828_s_input(dev, 0);
  1498. mutex_init(&dev->vb_queue_lock);
  1499. mutex_init(&dev->vb_vbi_queue_lock);
  1500. /* Fill the video capture device struct */
  1501. dev->vdev = au0828_video_template;
  1502. dev->vdev.v4l2_dev = &dev->v4l2_dev;
  1503. dev->vdev.lock = &dev->lock;
  1504. dev->vdev.queue = &dev->vb_vidq;
  1505. dev->vdev.queue->lock = &dev->vb_queue_lock;
  1506. strcpy(dev->vdev.name, "au0828a video");
  1507. /* Setup the VBI device */
  1508. dev->vbi_dev = au0828_video_template;
  1509. dev->vbi_dev.v4l2_dev = &dev->v4l2_dev;
  1510. dev->vbi_dev.lock = &dev->lock;
  1511. dev->vbi_dev.queue = &dev->vb_vbiq;
  1512. dev->vbi_dev.queue->lock = &dev->vb_vbi_queue_lock;
  1513. strcpy(dev->vbi_dev.name, "au0828a vbi");
  1514. /* initialize videobuf2 stuff */
  1515. retval = au0828_vb2_setup(dev);
  1516. if (retval != 0) {
  1517. dprintk(1, "unable to setup videobuf2 queues (error = %d).\n",
  1518. retval);
  1519. return -ENODEV;
  1520. }
  1521. /* Register the v4l2 device */
  1522. video_set_drvdata(&dev->vdev, dev);
  1523. retval = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
  1524. if (retval != 0) {
  1525. dprintk(1, "unable to register video device (error = %d).\n",
  1526. retval);
  1527. ret = -ENODEV;
  1528. goto err_reg_vdev;
  1529. }
  1530. /* Register the vbi device */
  1531. video_set_drvdata(&dev->vbi_dev, dev);
  1532. retval = video_register_device(&dev->vbi_dev, VFL_TYPE_VBI, -1);
  1533. if (retval != 0) {
  1534. dprintk(1, "unable to register vbi device (error = %d).\n",
  1535. retval);
  1536. ret = -ENODEV;
  1537. goto err_reg_vbi_dev;
  1538. }
  1539. dprintk(1, "%s completed!\n", __func__);
  1540. return 0;
  1541. err_reg_vbi_dev:
  1542. video_unregister_device(&dev->vdev);
  1543. err_reg_vdev:
  1544. vb2_queue_release(&dev->vb_vidq);
  1545. vb2_queue_release(&dev->vb_vbiq);
  1546. return ret;
  1547. }