au0828-video.c 52 KB

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