cx231xx-video.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189
  1. /*
  2. cx231xx-video.c - driver for Conexant Cx23100/101/102
  3. USB video capture devices
  4. Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
  5. Based on em28xx driver
  6. Based on cx23885 driver
  7. Based on cx88 driver
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  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. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include <linux/init.h>
  21. #include <linux/list.h>
  22. #include <linux/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/bitmap.h>
  25. #include <linux/usb.h>
  26. #include <linux/i2c.h>
  27. #include <linux/mm.h>
  28. #include <linux/mutex.h>
  29. #include <linux/slab.h>
  30. #include <media/v4l2-common.h>
  31. #include <media/v4l2-ioctl.h>
  32. #include <media/v4l2-event.h>
  33. #include <media/msp3400.h>
  34. #include <media/tuner.h>
  35. #include "dvb_frontend.h"
  36. #include "cx231xx.h"
  37. #include "cx231xx-vbi.h"
  38. #define CX231XX_VERSION "0.0.2"
  39. #define DRIVER_AUTHOR "Srinivasa Deevi <srinivasa.deevi@conexant.com>"
  40. #define DRIVER_DESC "Conexant cx231xx based USB video device driver"
  41. #define cx231xx_videodbg(fmt, arg...) do {\
  42. if (video_debug) \
  43. printk(KERN_INFO "%s %s :"fmt, \
  44. dev->name, __func__ , ##arg); } while (0)
  45. static unsigned int isoc_debug;
  46. module_param(isoc_debug, int, 0644);
  47. MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
  48. #define cx231xx_isocdbg(fmt, arg...) \
  49. do {\
  50. if (isoc_debug) { \
  51. printk(KERN_INFO "%s %s :"fmt, \
  52. dev->name, __func__ , ##arg); \
  53. } \
  54. } while (0)
  55. MODULE_AUTHOR(DRIVER_AUTHOR);
  56. MODULE_DESCRIPTION(DRIVER_DESC);
  57. MODULE_LICENSE("GPL");
  58. MODULE_VERSION(CX231XX_VERSION);
  59. static unsigned int card[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
  60. static unsigned int video_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
  61. static unsigned int vbi_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
  62. static unsigned int radio_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
  63. module_param_array(card, int, NULL, 0444);
  64. module_param_array(video_nr, int, NULL, 0444);
  65. module_param_array(vbi_nr, int, NULL, 0444);
  66. module_param_array(radio_nr, int, NULL, 0444);
  67. MODULE_PARM_DESC(card, "card type");
  68. MODULE_PARM_DESC(video_nr, "video device numbers");
  69. MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
  70. MODULE_PARM_DESC(radio_nr, "radio device numbers");
  71. static unsigned int video_debug;
  72. module_param(video_debug, int, 0644);
  73. MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
  74. /* supported video standards */
  75. static struct cx231xx_fmt format[] = {
  76. {
  77. .name = "16bpp YUY2, 4:2:2, packed",
  78. .fourcc = V4L2_PIX_FMT_YUYV,
  79. .depth = 16,
  80. .reg = 0,
  81. },
  82. };
  83. /* ------------------------------------------------------------------
  84. Video buffer and parser functions
  85. ------------------------------------------------------------------*/
  86. /*
  87. * Announces that a buffer were filled and request the next
  88. */
  89. static inline void buffer_filled(struct cx231xx *dev,
  90. struct cx231xx_dmaqueue *dma_q,
  91. struct cx231xx_buffer *buf)
  92. {
  93. /* Advice that buffer was filled */
  94. cx231xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
  95. buf->vb.state = VIDEOBUF_DONE;
  96. buf->vb.field_count++;
  97. v4l2_get_timestamp(&buf->vb.ts);
  98. if (dev->USE_ISO)
  99. dev->video_mode.isoc_ctl.buf = NULL;
  100. else
  101. dev->video_mode.bulk_ctl.buf = NULL;
  102. list_del(&buf->vb.queue);
  103. wake_up(&buf->vb.done);
  104. }
  105. static inline void print_err_status(struct cx231xx *dev, int packet, int status)
  106. {
  107. char *errmsg = "Unknown";
  108. switch (status) {
  109. case -ENOENT:
  110. errmsg = "unlinked synchronuously";
  111. break;
  112. case -ECONNRESET:
  113. errmsg = "unlinked asynchronuously";
  114. break;
  115. case -ENOSR:
  116. errmsg = "Buffer error (overrun)";
  117. break;
  118. case -EPIPE:
  119. errmsg = "Stalled (device not responding)";
  120. break;
  121. case -EOVERFLOW:
  122. errmsg = "Babble (bad cable?)";
  123. break;
  124. case -EPROTO:
  125. errmsg = "Bit-stuff error (bad cable?)";
  126. break;
  127. case -EILSEQ:
  128. errmsg = "CRC/Timeout (could be anything)";
  129. break;
  130. case -ETIME:
  131. errmsg = "Device does not respond";
  132. break;
  133. }
  134. if (packet < 0) {
  135. cx231xx_isocdbg("URB status %d [%s].\n", status, errmsg);
  136. } else {
  137. cx231xx_isocdbg("URB packet %d, status %d [%s].\n",
  138. packet, status, errmsg);
  139. }
  140. }
  141. /*
  142. * video-buf generic routine to get the next available buffer
  143. */
  144. static inline void get_next_buf(struct cx231xx_dmaqueue *dma_q,
  145. struct cx231xx_buffer **buf)
  146. {
  147. struct cx231xx_video_mode *vmode =
  148. container_of(dma_q, struct cx231xx_video_mode, vidq);
  149. struct cx231xx *dev = container_of(vmode, struct cx231xx, video_mode);
  150. char *outp;
  151. if (list_empty(&dma_q->active)) {
  152. cx231xx_isocdbg("No active queue to serve\n");
  153. if (dev->USE_ISO)
  154. dev->video_mode.isoc_ctl.buf = NULL;
  155. else
  156. dev->video_mode.bulk_ctl.buf = NULL;
  157. *buf = NULL;
  158. return;
  159. }
  160. /* Get the next buffer */
  161. *buf = list_entry(dma_q->active.next, struct cx231xx_buffer, vb.queue);
  162. /* Cleans up buffer - Useful for testing for frame/URB loss */
  163. outp = videobuf_to_vmalloc(&(*buf)->vb);
  164. memset(outp, 0, (*buf)->vb.size);
  165. if (dev->USE_ISO)
  166. dev->video_mode.isoc_ctl.buf = *buf;
  167. else
  168. dev->video_mode.bulk_ctl.buf = *buf;
  169. return;
  170. }
  171. /*
  172. * Controls the isoc copy of each urb packet
  173. */
  174. static inline int cx231xx_isoc_copy(struct cx231xx *dev, struct urb *urb)
  175. {
  176. struct cx231xx_dmaqueue *dma_q = urb->context;
  177. int i, rc = 1;
  178. unsigned char *p_buffer;
  179. u32 bytes_parsed = 0, buffer_size = 0;
  180. u8 sav_eav = 0;
  181. if (!dev)
  182. return 0;
  183. if (dev->state & DEV_DISCONNECTED)
  184. return 0;
  185. if (urb->status < 0) {
  186. print_err_status(dev, -1, urb->status);
  187. if (urb->status == -ENOENT)
  188. return 0;
  189. }
  190. for (i = 0; i < urb->number_of_packets; i++) {
  191. int status = urb->iso_frame_desc[i].status;
  192. if (status < 0) {
  193. print_err_status(dev, i, status);
  194. if (urb->iso_frame_desc[i].status != -EPROTO)
  195. continue;
  196. }
  197. if (urb->iso_frame_desc[i].actual_length <= 0) {
  198. /* cx231xx_isocdbg("packet %d is empty",i); - spammy */
  199. continue;
  200. }
  201. if (urb->iso_frame_desc[i].actual_length >
  202. dev->video_mode.max_pkt_size) {
  203. cx231xx_isocdbg("packet bigger than packet size");
  204. continue;
  205. }
  206. /* get buffer pointer and length */
  207. p_buffer = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  208. buffer_size = urb->iso_frame_desc[i].actual_length;
  209. bytes_parsed = 0;
  210. if (dma_q->is_partial_line) {
  211. /* Handle the case of a partial line */
  212. sav_eav = dma_q->last_sav;
  213. } else {
  214. /* Check for a SAV/EAV overlapping
  215. the buffer boundary */
  216. sav_eav =
  217. cx231xx_find_boundary_SAV_EAV(p_buffer,
  218. dma_q->partial_buf,
  219. &bytes_parsed);
  220. }
  221. sav_eav &= 0xF0;
  222. /* Get the first line if we have some portion of an SAV/EAV from
  223. the last buffer or a partial line */
  224. if (sav_eav) {
  225. bytes_parsed += cx231xx_get_video_line(dev, dma_q,
  226. sav_eav, /* SAV/EAV */
  227. p_buffer + bytes_parsed, /* p_buffer */
  228. buffer_size - bytes_parsed);/* buf size */
  229. }
  230. /* Now parse data that is completely in this buffer */
  231. /* dma_q->is_partial_line = 0; */
  232. while (bytes_parsed < buffer_size) {
  233. u32 bytes_used = 0;
  234. sav_eav = cx231xx_find_next_SAV_EAV(
  235. p_buffer + bytes_parsed, /* p_buffer */
  236. buffer_size - bytes_parsed, /* buf size */
  237. &bytes_used);/* bytes used to get SAV/EAV */
  238. bytes_parsed += bytes_used;
  239. sav_eav &= 0xF0;
  240. if (sav_eav && (bytes_parsed < buffer_size)) {
  241. bytes_parsed += cx231xx_get_video_line(dev,
  242. dma_q, sav_eav, /* SAV/EAV */
  243. p_buffer + bytes_parsed,/* p_buffer */
  244. buffer_size - bytes_parsed);/*buf size*/
  245. }
  246. }
  247. /* Save the last four bytes of the buffer so we can check the
  248. buffer boundary condition next time */
  249. memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4);
  250. bytes_parsed = 0;
  251. }
  252. return rc;
  253. }
  254. static inline int cx231xx_bulk_copy(struct cx231xx *dev, struct urb *urb)
  255. {
  256. struct cx231xx_dmaqueue *dma_q = urb->context;
  257. int rc = 1;
  258. unsigned char *p_buffer;
  259. u32 bytes_parsed = 0, buffer_size = 0;
  260. u8 sav_eav = 0;
  261. if (!dev)
  262. return 0;
  263. if (dev->state & DEV_DISCONNECTED)
  264. return 0;
  265. if (urb->status < 0) {
  266. print_err_status(dev, -1, urb->status);
  267. if (urb->status == -ENOENT)
  268. return 0;
  269. }
  270. if (1) {
  271. /* get buffer pointer and length */
  272. p_buffer = urb->transfer_buffer;
  273. buffer_size = urb->actual_length;
  274. bytes_parsed = 0;
  275. if (dma_q->is_partial_line) {
  276. /* Handle the case of a partial line */
  277. sav_eav = dma_q->last_sav;
  278. } else {
  279. /* Check for a SAV/EAV overlapping
  280. the buffer boundary */
  281. sav_eav =
  282. cx231xx_find_boundary_SAV_EAV(p_buffer,
  283. dma_q->partial_buf,
  284. &bytes_parsed);
  285. }
  286. sav_eav &= 0xF0;
  287. /* Get the first line if we have some portion of an SAV/EAV from
  288. the last buffer or a partial line */
  289. if (sav_eav) {
  290. bytes_parsed += cx231xx_get_video_line(dev, dma_q,
  291. sav_eav, /* SAV/EAV */
  292. p_buffer + bytes_parsed, /* p_buffer */
  293. buffer_size - bytes_parsed);/* buf size */
  294. }
  295. /* Now parse data that is completely in this buffer */
  296. /* dma_q->is_partial_line = 0; */
  297. while (bytes_parsed < buffer_size) {
  298. u32 bytes_used = 0;
  299. sav_eav = cx231xx_find_next_SAV_EAV(
  300. p_buffer + bytes_parsed, /* p_buffer */
  301. buffer_size - bytes_parsed, /* buf size */
  302. &bytes_used);/* bytes used to get SAV/EAV */
  303. bytes_parsed += bytes_used;
  304. sav_eav &= 0xF0;
  305. if (sav_eav && (bytes_parsed < buffer_size)) {
  306. bytes_parsed += cx231xx_get_video_line(dev,
  307. dma_q, sav_eav, /* SAV/EAV */
  308. p_buffer + bytes_parsed,/* p_buffer */
  309. buffer_size - bytes_parsed);/*buf size*/
  310. }
  311. }
  312. /* Save the last four bytes of the buffer so we can check the
  313. buffer boundary condition next time */
  314. memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4);
  315. bytes_parsed = 0;
  316. }
  317. return rc;
  318. }
  319. u8 cx231xx_find_boundary_SAV_EAV(u8 *p_buffer, u8 *partial_buf,
  320. u32 *p_bytes_used)
  321. {
  322. u32 bytes_used;
  323. u8 boundary_bytes[8];
  324. u8 sav_eav = 0;
  325. *p_bytes_used = 0;
  326. /* Create an array of the last 4 bytes of the last buffer and the first
  327. 4 bytes of the current buffer. */
  328. memcpy(boundary_bytes, partial_buf, 4);
  329. memcpy(boundary_bytes + 4, p_buffer, 4);
  330. /* Check for the SAV/EAV in the boundary buffer */
  331. sav_eav = cx231xx_find_next_SAV_EAV((u8 *)&boundary_bytes, 8,
  332. &bytes_used);
  333. if (sav_eav) {
  334. /* found a boundary SAV/EAV. Updates the bytes used to reflect
  335. only those used in the new buffer */
  336. *p_bytes_used = bytes_used - 4;
  337. }
  338. return sav_eav;
  339. }
  340. u8 cx231xx_find_next_SAV_EAV(u8 *p_buffer, u32 buffer_size, u32 *p_bytes_used)
  341. {
  342. u32 i;
  343. u8 sav_eav = 0;
  344. /*
  345. * Don't search if the buffer size is less than 4. It causes a page
  346. * fault since buffer_size - 4 evaluates to a large number in that
  347. * case.
  348. */
  349. if (buffer_size < 4) {
  350. *p_bytes_used = buffer_size;
  351. return 0;
  352. }
  353. for (i = 0; i < (buffer_size - 3); i++) {
  354. if ((p_buffer[i] == 0xFF) &&
  355. (p_buffer[i + 1] == 0x00) && (p_buffer[i + 2] == 0x00)) {
  356. *p_bytes_used = i + 4;
  357. sav_eav = p_buffer[i + 3];
  358. return sav_eav;
  359. }
  360. }
  361. *p_bytes_used = buffer_size;
  362. return 0;
  363. }
  364. u32 cx231xx_get_video_line(struct cx231xx *dev,
  365. struct cx231xx_dmaqueue *dma_q, u8 sav_eav,
  366. u8 *p_buffer, u32 buffer_size)
  367. {
  368. u32 bytes_copied = 0;
  369. int current_field = -1;
  370. switch (sav_eav) {
  371. case SAV_ACTIVE_VIDEO_FIELD1:
  372. /* looking for skipped line which occurred in PAL 720x480 mode.
  373. In this case, there will be no active data contained
  374. between the SAV and EAV */
  375. if ((buffer_size > 3) && (p_buffer[0] == 0xFF) &&
  376. (p_buffer[1] == 0x00) && (p_buffer[2] == 0x00) &&
  377. ((p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD1) ||
  378. (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD2) ||
  379. (p_buffer[3] == EAV_VBLANK_FIELD1) ||
  380. (p_buffer[3] == EAV_VBLANK_FIELD2)))
  381. return bytes_copied;
  382. current_field = 1;
  383. break;
  384. case SAV_ACTIVE_VIDEO_FIELD2:
  385. /* looking for skipped line which occurred in PAL 720x480 mode.
  386. In this case, there will be no active data contained between
  387. the SAV and EAV */
  388. if ((buffer_size > 3) && (p_buffer[0] == 0xFF) &&
  389. (p_buffer[1] == 0x00) && (p_buffer[2] == 0x00) &&
  390. ((p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD1) ||
  391. (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD2) ||
  392. (p_buffer[3] == EAV_VBLANK_FIELD1) ||
  393. (p_buffer[3] == EAV_VBLANK_FIELD2)))
  394. return bytes_copied;
  395. current_field = 2;
  396. break;
  397. }
  398. dma_q->last_sav = sav_eav;
  399. bytes_copied = cx231xx_copy_video_line(dev, dma_q, p_buffer,
  400. buffer_size, current_field);
  401. return bytes_copied;
  402. }
  403. u32 cx231xx_copy_video_line(struct cx231xx *dev,
  404. struct cx231xx_dmaqueue *dma_q, u8 *p_line,
  405. u32 length, int field_number)
  406. {
  407. u32 bytes_to_copy;
  408. struct cx231xx_buffer *buf;
  409. u32 _line_size = dev->width * 2;
  410. if (dma_q->current_field != field_number)
  411. cx231xx_reset_video_buffer(dev, dma_q);
  412. /* get the buffer pointer */
  413. if (dev->USE_ISO)
  414. buf = dev->video_mode.isoc_ctl.buf;
  415. else
  416. buf = dev->video_mode.bulk_ctl.buf;
  417. /* Remember the field number for next time */
  418. dma_q->current_field = field_number;
  419. bytes_to_copy = dma_q->bytes_left_in_line;
  420. if (bytes_to_copy > length)
  421. bytes_to_copy = length;
  422. if (dma_q->lines_completed >= dma_q->lines_per_field) {
  423. dma_q->bytes_left_in_line -= bytes_to_copy;
  424. dma_q->is_partial_line = (dma_q->bytes_left_in_line == 0) ?
  425. 0 : 1;
  426. return 0;
  427. }
  428. dma_q->is_partial_line = 1;
  429. /* If we don't have a buffer, just return the number of bytes we would
  430. have copied if we had a buffer. */
  431. if (!buf) {
  432. dma_q->bytes_left_in_line -= bytes_to_copy;
  433. dma_q->is_partial_line = (dma_q->bytes_left_in_line == 0)
  434. ? 0 : 1;
  435. return bytes_to_copy;
  436. }
  437. /* copy the data to video buffer */
  438. cx231xx_do_copy(dev, dma_q, p_line, bytes_to_copy);
  439. dma_q->pos += bytes_to_copy;
  440. dma_q->bytes_left_in_line -= bytes_to_copy;
  441. if (dma_q->bytes_left_in_line == 0) {
  442. dma_q->bytes_left_in_line = _line_size;
  443. dma_q->lines_completed++;
  444. dma_q->is_partial_line = 0;
  445. if (cx231xx_is_buffer_done(dev, dma_q) && buf) {
  446. buffer_filled(dev, dma_q, buf);
  447. dma_q->pos = 0;
  448. buf = NULL;
  449. dma_q->lines_completed = 0;
  450. }
  451. }
  452. return bytes_to_copy;
  453. }
  454. void cx231xx_reset_video_buffer(struct cx231xx *dev,
  455. struct cx231xx_dmaqueue *dma_q)
  456. {
  457. struct cx231xx_buffer *buf;
  458. /* handle the switch from field 1 to field 2 */
  459. if (dma_q->current_field == 1) {
  460. if (dma_q->lines_completed >= dma_q->lines_per_field)
  461. dma_q->field1_done = 1;
  462. else
  463. dma_q->field1_done = 0;
  464. }
  465. if (dev->USE_ISO)
  466. buf = dev->video_mode.isoc_ctl.buf;
  467. else
  468. buf = dev->video_mode.bulk_ctl.buf;
  469. if (buf == NULL) {
  470. /* first try to get the buffer */
  471. get_next_buf(dma_q, &buf);
  472. dma_q->pos = 0;
  473. dma_q->field1_done = 0;
  474. dma_q->current_field = -1;
  475. }
  476. /* reset the counters */
  477. dma_q->bytes_left_in_line = dev->width << 1;
  478. dma_q->lines_completed = 0;
  479. }
  480. int cx231xx_do_copy(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
  481. u8 *p_buffer, u32 bytes_to_copy)
  482. {
  483. u8 *p_out_buffer = NULL;
  484. u32 current_line_bytes_copied = 0;
  485. struct cx231xx_buffer *buf;
  486. u32 _line_size = dev->width << 1;
  487. void *startwrite;
  488. int offset, lencopy;
  489. if (dev->USE_ISO)
  490. buf = dev->video_mode.isoc_ctl.buf;
  491. else
  492. buf = dev->video_mode.bulk_ctl.buf;
  493. if (buf == NULL)
  494. return -1;
  495. p_out_buffer = videobuf_to_vmalloc(&buf->vb);
  496. current_line_bytes_copied = _line_size - dma_q->bytes_left_in_line;
  497. /* Offset field 2 one line from the top of the buffer */
  498. offset = (dma_q->current_field == 1) ? 0 : _line_size;
  499. /* Offset for field 2 */
  500. startwrite = p_out_buffer + offset;
  501. /* lines already completed in the current field */
  502. startwrite += (dma_q->lines_completed * _line_size * 2);
  503. /* bytes already completed in the current line */
  504. startwrite += current_line_bytes_copied;
  505. lencopy = dma_q->bytes_left_in_line > bytes_to_copy ?
  506. bytes_to_copy : dma_q->bytes_left_in_line;
  507. if ((u8 *)(startwrite + lencopy) > (u8 *)(p_out_buffer + buf->vb.size))
  508. return 0;
  509. /* The below copies the UYVY data straight into video buffer */
  510. cx231xx_swab((u16 *) p_buffer, (u16 *) startwrite, (u16) lencopy);
  511. return 0;
  512. }
  513. void cx231xx_swab(u16 *from, u16 *to, u16 len)
  514. {
  515. u16 i;
  516. if (len <= 0)
  517. return;
  518. for (i = 0; i < len / 2; i++)
  519. to[i] = (from[i] << 8) | (from[i] >> 8);
  520. }
  521. u8 cx231xx_is_buffer_done(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q)
  522. {
  523. u8 buffer_complete = 0;
  524. /* Dual field stream */
  525. buffer_complete = ((dma_q->current_field == 2) &&
  526. (dma_q->lines_completed >= dma_q->lines_per_field) &&
  527. dma_q->field1_done);
  528. return buffer_complete;
  529. }
  530. /* ------------------------------------------------------------------
  531. Videobuf operations
  532. ------------------------------------------------------------------*/
  533. static int
  534. buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
  535. {
  536. struct cx231xx_fh *fh = vq->priv_data;
  537. struct cx231xx *dev = fh->dev;
  538. *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)>>3;
  539. if (0 == *count)
  540. *count = CX231XX_DEF_BUF;
  541. if (*count < CX231XX_MIN_BUF)
  542. *count = CX231XX_MIN_BUF;
  543. return 0;
  544. }
  545. /* This is called *without* dev->slock held; please keep it that way */
  546. static void free_buffer(struct videobuf_queue *vq, struct cx231xx_buffer *buf)
  547. {
  548. struct cx231xx_fh *fh = vq->priv_data;
  549. struct cx231xx *dev = fh->dev;
  550. unsigned long flags = 0;
  551. if (in_interrupt())
  552. BUG();
  553. /* We used to wait for the buffer to finish here, but this didn't work
  554. because, as we were keeping the state as VIDEOBUF_QUEUED,
  555. videobuf_queue_cancel marked it as finished for us.
  556. (Also, it could wedge forever if the hardware was misconfigured.)
  557. This should be safe; by the time we get here, the buffer isn't
  558. queued anymore. If we ever start marking the buffers as
  559. VIDEOBUF_ACTIVE, it won't be, though.
  560. */
  561. spin_lock_irqsave(&dev->video_mode.slock, flags);
  562. if (dev->USE_ISO) {
  563. if (dev->video_mode.isoc_ctl.buf == buf)
  564. dev->video_mode.isoc_ctl.buf = NULL;
  565. } else {
  566. if (dev->video_mode.bulk_ctl.buf == buf)
  567. dev->video_mode.bulk_ctl.buf = NULL;
  568. }
  569. spin_unlock_irqrestore(&dev->video_mode.slock, flags);
  570. videobuf_vmalloc_free(&buf->vb);
  571. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  572. }
  573. static int
  574. buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
  575. enum v4l2_field field)
  576. {
  577. struct cx231xx_fh *fh = vq->priv_data;
  578. struct cx231xx_buffer *buf =
  579. container_of(vb, struct cx231xx_buffer, vb);
  580. struct cx231xx *dev = fh->dev;
  581. int rc = 0, urb_init = 0;
  582. /* The only currently supported format is 16 bits/pixel */
  583. buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
  584. + 7) >> 3;
  585. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  586. return -EINVAL;
  587. buf->vb.width = dev->width;
  588. buf->vb.height = dev->height;
  589. buf->vb.field = field;
  590. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  591. rc = videobuf_iolock(vq, &buf->vb, NULL);
  592. if (rc < 0)
  593. goto fail;
  594. }
  595. if (dev->USE_ISO) {
  596. if (!dev->video_mode.isoc_ctl.num_bufs)
  597. urb_init = 1;
  598. } else {
  599. if (!dev->video_mode.bulk_ctl.num_bufs)
  600. urb_init = 1;
  601. }
  602. /*cx231xx_info("urb_init=%d dev->video_mode.max_pkt_size=%d\n",
  603. urb_init, dev->video_mode.max_pkt_size);*/
  604. if (urb_init) {
  605. dev->mode_tv = 0;
  606. if (dev->USE_ISO)
  607. rc = cx231xx_init_isoc(dev, CX231XX_NUM_PACKETS,
  608. CX231XX_NUM_BUFS,
  609. dev->video_mode.max_pkt_size,
  610. cx231xx_isoc_copy);
  611. else
  612. rc = cx231xx_init_bulk(dev, CX231XX_NUM_PACKETS,
  613. CX231XX_NUM_BUFS,
  614. dev->video_mode.max_pkt_size,
  615. cx231xx_bulk_copy);
  616. if (rc < 0)
  617. goto fail;
  618. }
  619. buf->vb.state = VIDEOBUF_PREPARED;
  620. return 0;
  621. fail:
  622. free_buffer(vq, buf);
  623. return rc;
  624. }
  625. static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  626. {
  627. struct cx231xx_buffer *buf =
  628. container_of(vb, struct cx231xx_buffer, vb);
  629. struct cx231xx_fh *fh = vq->priv_data;
  630. struct cx231xx *dev = fh->dev;
  631. struct cx231xx_dmaqueue *vidq = &dev->video_mode.vidq;
  632. buf->vb.state = VIDEOBUF_QUEUED;
  633. list_add_tail(&buf->vb.queue, &vidq->active);
  634. }
  635. static void buffer_release(struct videobuf_queue *vq,
  636. struct videobuf_buffer *vb)
  637. {
  638. struct cx231xx_buffer *buf =
  639. container_of(vb, struct cx231xx_buffer, vb);
  640. struct cx231xx_fh *fh = vq->priv_data;
  641. struct cx231xx *dev = (struct cx231xx *)fh->dev;
  642. cx231xx_isocdbg("cx231xx: called buffer_release\n");
  643. free_buffer(vq, buf);
  644. }
  645. static struct videobuf_queue_ops cx231xx_video_qops = {
  646. .buf_setup = buffer_setup,
  647. .buf_prepare = buffer_prepare,
  648. .buf_queue = buffer_queue,
  649. .buf_release = buffer_release,
  650. };
  651. /********************* v4l2 interface **************************************/
  652. void video_mux(struct cx231xx *dev, int index)
  653. {
  654. dev->video_input = index;
  655. dev->ctl_ainput = INPUT(index)->amux;
  656. cx231xx_set_video_input_mux(dev, index);
  657. cx25840_call(dev, video, s_routing, INPUT(index)->vmux, 0, 0);
  658. cx231xx_set_audio_input(dev, dev->ctl_ainput);
  659. cx231xx_info("video_mux : %d\n", index);
  660. /* do mode control overrides if required */
  661. cx231xx_do_mode_ctrl_overrides(dev);
  662. }
  663. /* Usage lock check functions */
  664. static int res_get(struct cx231xx_fh *fh)
  665. {
  666. struct cx231xx *dev = fh->dev;
  667. int rc = 0;
  668. /* This instance already has stream_on */
  669. if (fh->stream_on)
  670. return rc;
  671. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  672. if (dev->stream_on)
  673. return -EBUSY;
  674. dev->stream_on = 1;
  675. } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
  676. if (dev->vbi_stream_on)
  677. return -EBUSY;
  678. dev->vbi_stream_on = 1;
  679. } else
  680. return -EINVAL;
  681. fh->stream_on = 1;
  682. return rc;
  683. }
  684. static int res_check(struct cx231xx_fh *fh)
  685. {
  686. return fh->stream_on;
  687. }
  688. static void res_free(struct cx231xx_fh *fh)
  689. {
  690. struct cx231xx *dev = fh->dev;
  691. fh->stream_on = 0;
  692. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  693. dev->stream_on = 0;
  694. if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
  695. dev->vbi_stream_on = 0;
  696. }
  697. static int check_dev(struct cx231xx *dev)
  698. {
  699. if (dev->state & DEV_DISCONNECTED) {
  700. cx231xx_errdev("v4l2 ioctl: device not present\n");
  701. return -ENODEV;
  702. }
  703. return 0;
  704. }
  705. /* ------------------------------------------------------------------
  706. IOCTL vidioc handling
  707. ------------------------------------------------------------------*/
  708. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  709. struct v4l2_format *f)
  710. {
  711. struct cx231xx_fh *fh = priv;
  712. struct cx231xx *dev = fh->dev;
  713. f->fmt.pix.width = dev->width;
  714. f->fmt.pix.height = dev->height;
  715. f->fmt.pix.pixelformat = dev->format->fourcc;
  716. f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
  717. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
  718. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  719. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  720. f->fmt.pix.priv = 0;
  721. return 0;
  722. }
  723. static struct cx231xx_fmt *format_by_fourcc(unsigned int fourcc)
  724. {
  725. unsigned int i;
  726. for (i = 0; i < ARRAY_SIZE(format); i++)
  727. if (format[i].fourcc == fourcc)
  728. return &format[i];
  729. return NULL;
  730. }
  731. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  732. struct v4l2_format *f)
  733. {
  734. struct cx231xx_fh *fh = priv;
  735. struct cx231xx *dev = fh->dev;
  736. unsigned int width = f->fmt.pix.width;
  737. unsigned int height = f->fmt.pix.height;
  738. unsigned int maxw = norm_maxw(dev);
  739. unsigned int maxh = norm_maxh(dev);
  740. struct cx231xx_fmt *fmt;
  741. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  742. if (!fmt) {
  743. cx231xx_videodbg("Fourcc format (%08x) invalid.\n",
  744. f->fmt.pix.pixelformat);
  745. return -EINVAL;
  746. }
  747. /* width must even because of the YUYV format
  748. height must be even because of interlacing */
  749. v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh, 1, 0);
  750. f->fmt.pix.width = width;
  751. f->fmt.pix.height = height;
  752. f->fmt.pix.pixelformat = fmt->fourcc;
  753. f->fmt.pix.bytesperline = (width * fmt->depth + 7) >> 3;
  754. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
  755. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  756. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  757. f->fmt.pix.priv = 0;
  758. return 0;
  759. }
  760. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  761. struct v4l2_format *f)
  762. {
  763. struct cx231xx_fh *fh = priv;
  764. struct cx231xx *dev = fh->dev;
  765. int rc;
  766. struct cx231xx_fmt *fmt;
  767. struct v4l2_mbus_framefmt mbus_fmt;
  768. rc = check_dev(dev);
  769. if (rc < 0)
  770. return rc;
  771. vidioc_try_fmt_vid_cap(file, priv, f);
  772. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  773. if (!fmt)
  774. return -EINVAL;
  775. if (videobuf_queue_is_busy(&fh->vb_vidq)) {
  776. cx231xx_errdev("%s queue busy\n", __func__);
  777. return -EBUSY;
  778. }
  779. if (dev->stream_on && !fh->stream_on) {
  780. cx231xx_errdev("%s device in use by another fh\n", __func__);
  781. return -EBUSY;
  782. }
  783. /* set new image size */
  784. dev->width = f->fmt.pix.width;
  785. dev->height = f->fmt.pix.height;
  786. dev->format = fmt;
  787. v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
  788. call_all(dev, video, s_mbus_fmt, &mbus_fmt);
  789. v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
  790. return rc;
  791. }
  792. static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
  793. {
  794. struct cx231xx_fh *fh = priv;
  795. struct cx231xx *dev = fh->dev;
  796. *id = dev->norm;
  797. return 0;
  798. }
  799. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
  800. {
  801. struct cx231xx_fh *fh = priv;
  802. struct cx231xx *dev = fh->dev;
  803. struct v4l2_mbus_framefmt mbus_fmt;
  804. int rc;
  805. rc = check_dev(dev);
  806. if (rc < 0)
  807. return rc;
  808. if (dev->norm == norm)
  809. return 0;
  810. if (videobuf_queue_is_busy(&fh->vb_vidq))
  811. return -EBUSY;
  812. dev->norm = norm;
  813. /* Adjusts width/height, if needed */
  814. dev->width = 720;
  815. dev->height = (dev->norm & V4L2_STD_625_50) ? 576 : 480;
  816. call_all(dev, core, s_std, dev->norm);
  817. /* We need to reset basic properties in the decoder related to
  818. resolution (since a standard change effects things like the number
  819. of lines in VACT, etc) */
  820. memset(&mbus_fmt, 0, sizeof(mbus_fmt));
  821. mbus_fmt.code = V4L2_MBUS_FMT_FIXED;
  822. mbus_fmt.width = dev->width;
  823. mbus_fmt.height = dev->height;
  824. call_all(dev, video, s_mbus_fmt, &mbus_fmt);
  825. /* do mode control overrides */
  826. cx231xx_do_mode_ctrl_overrides(dev);
  827. return 0;
  828. }
  829. static const char *iname[] = {
  830. [CX231XX_VMUX_COMPOSITE1] = "Composite1",
  831. [CX231XX_VMUX_SVIDEO] = "S-Video",
  832. [CX231XX_VMUX_TELEVISION] = "Television",
  833. [CX231XX_VMUX_CABLE] = "Cable TV",
  834. [CX231XX_VMUX_DVB] = "DVB",
  835. [CX231XX_VMUX_DEBUG] = "for debug only",
  836. };
  837. int cx231xx_enum_input(struct file *file, void *priv,
  838. struct v4l2_input *i)
  839. {
  840. struct cx231xx_fh *fh = priv;
  841. struct cx231xx *dev = fh->dev;
  842. u32 gen_stat;
  843. unsigned int ret, n;
  844. n = i->index;
  845. if (n >= MAX_CX231XX_INPUT)
  846. return -EINVAL;
  847. if (0 == INPUT(n)->type)
  848. return -EINVAL;
  849. i->index = n;
  850. i->type = V4L2_INPUT_TYPE_CAMERA;
  851. strcpy(i->name, iname[INPUT(n)->type]);
  852. if ((CX231XX_VMUX_TELEVISION == INPUT(n)->type) ||
  853. (CX231XX_VMUX_CABLE == INPUT(n)->type))
  854. i->type = V4L2_INPUT_TYPE_TUNER;
  855. i->std = dev->vdev->tvnorms;
  856. /* If they are asking about the active input, read signal status */
  857. if (n == dev->video_input) {
  858. ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
  859. GEN_STAT, 2, &gen_stat, 4);
  860. if (ret > 0) {
  861. if ((gen_stat & FLD_VPRES) == 0x00)
  862. i->status |= V4L2_IN_ST_NO_SIGNAL;
  863. if ((gen_stat & FLD_HLOCK) == 0x00)
  864. i->status |= V4L2_IN_ST_NO_H_LOCK;
  865. }
  866. }
  867. return 0;
  868. }
  869. int cx231xx_g_input(struct file *file, void *priv, unsigned int *i)
  870. {
  871. struct cx231xx_fh *fh = priv;
  872. struct cx231xx *dev = fh->dev;
  873. *i = dev->video_input;
  874. return 0;
  875. }
  876. int cx231xx_s_input(struct file *file, void *priv, unsigned int i)
  877. {
  878. struct cx231xx_fh *fh = priv;
  879. struct cx231xx *dev = fh->dev;
  880. int rc;
  881. dev->mode_tv = 0;
  882. rc = check_dev(dev);
  883. if (rc < 0)
  884. return rc;
  885. if (i >= MAX_CX231XX_INPUT)
  886. return -EINVAL;
  887. if (0 == INPUT(i)->type)
  888. return -EINVAL;
  889. video_mux(dev, i);
  890. if (INPUT(i)->type == CX231XX_VMUX_TELEVISION ||
  891. INPUT(i)->type == CX231XX_VMUX_CABLE) {
  892. /* There's a tuner, so reset the standard and put it on the
  893. last known frequency (since it was probably powered down
  894. until now */
  895. call_all(dev, core, s_std, dev->norm);
  896. }
  897. return 0;
  898. }
  899. int cx231xx_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
  900. {
  901. struct cx231xx_fh *fh = priv;
  902. struct cx231xx *dev = fh->dev;
  903. int rc;
  904. rc = check_dev(dev);
  905. if (rc < 0)
  906. return rc;
  907. if (0 != t->index)
  908. return -EINVAL;
  909. strcpy(t->name, "Tuner");
  910. t->type = V4L2_TUNER_ANALOG_TV;
  911. t->capability = V4L2_TUNER_CAP_NORM;
  912. t->rangehigh = 0xffffffffUL;
  913. t->signal = 0xffff; /* LOCKED */
  914. call_all(dev, tuner, g_tuner, t);
  915. return 0;
  916. }
  917. int cx231xx_s_tuner(struct file *file, void *priv, const struct v4l2_tuner *t)
  918. {
  919. struct cx231xx_fh *fh = priv;
  920. struct cx231xx *dev = fh->dev;
  921. int rc;
  922. rc = check_dev(dev);
  923. if (rc < 0)
  924. return rc;
  925. if (0 != t->index)
  926. return -EINVAL;
  927. #if 0
  928. call_all(dev, tuner, s_tuner, t);
  929. #endif
  930. return 0;
  931. }
  932. int cx231xx_g_frequency(struct file *file, void *priv,
  933. struct v4l2_frequency *f)
  934. {
  935. struct cx231xx_fh *fh = priv;
  936. struct cx231xx *dev = fh->dev;
  937. if (f->tuner)
  938. return -EINVAL;
  939. f->frequency = dev->ctl_freq;
  940. return 0;
  941. }
  942. int cx231xx_s_frequency(struct file *file, void *priv,
  943. const struct v4l2_frequency *f)
  944. {
  945. struct cx231xx_fh *fh = priv;
  946. struct cx231xx *dev = fh->dev;
  947. struct v4l2_frequency new_freq = *f;
  948. int rc;
  949. u32 if_frequency = 5400000;
  950. cx231xx_info("Enter vidioc_s_frequency()f->frequency=%d;f->type=%d\n",
  951. f->frequency, f->type);
  952. /*cx231xx_info("f->type: 1-radio 2-analogTV 3-digitalTV\n");*/
  953. rc = check_dev(dev);
  954. if (rc < 0)
  955. return rc;
  956. if (0 != f->tuner)
  957. return -EINVAL;
  958. /* set pre channel change settings in DIF first */
  959. rc = cx231xx_tuner_pre_channel_change(dev);
  960. call_all(dev, tuner, s_frequency, f);
  961. call_all(dev, tuner, g_frequency, &new_freq);
  962. dev->ctl_freq = new_freq.frequency;
  963. /* set post channel change settings in DIF first */
  964. rc = cx231xx_tuner_post_channel_change(dev);
  965. if (dev->tuner_type == TUNER_NXP_TDA18271) {
  966. if (dev->norm & (V4L2_STD_MN | V4L2_STD_NTSC_443))
  967. if_frequency = 5400000; /*5.4MHz */
  968. else if (dev->norm & V4L2_STD_B)
  969. if_frequency = 6000000; /*6.0MHz */
  970. else if (dev->norm & (V4L2_STD_PAL_DK | V4L2_STD_SECAM_DK))
  971. if_frequency = 6900000; /*6.9MHz */
  972. else if (dev->norm & V4L2_STD_GH)
  973. if_frequency = 7100000; /*7.1MHz */
  974. else if (dev->norm & V4L2_STD_PAL_I)
  975. if_frequency = 7250000; /*7.25MHz */
  976. else if (dev->norm & V4L2_STD_SECAM_L)
  977. if_frequency = 6900000; /*6.9MHz */
  978. else if (dev->norm & V4L2_STD_SECAM_LC)
  979. if_frequency = 1250000; /*1.25MHz */
  980. cx231xx_info("if_frequency is set to %d\n", if_frequency);
  981. cx231xx_set_Colibri_For_LowIF(dev, if_frequency, 1, 1);
  982. update_HH_register_after_set_DIF(dev);
  983. }
  984. cx231xx_info("Set New FREQUENCY to %d\n", f->frequency);
  985. return rc;
  986. }
  987. #ifdef CONFIG_VIDEO_ADV_DEBUG
  988. int cx231xx_g_chip_info(struct file *file, void *fh,
  989. struct v4l2_dbg_chip_info *chip)
  990. {
  991. switch (chip->match.addr) {
  992. case 0: /* Cx231xx - internal registers */
  993. return 0;
  994. case 1: /* AFE - read byte */
  995. strlcpy(chip->name, "AFE (byte)", sizeof(chip->name));
  996. return 0;
  997. case 2: /* Video Block - read byte */
  998. strlcpy(chip->name, "Video (byte)", sizeof(chip->name));
  999. return 0;
  1000. case 3: /* I2S block - read byte */
  1001. strlcpy(chip->name, "I2S (byte)", sizeof(chip->name));
  1002. return 0;
  1003. case 4: /* AFE - read dword */
  1004. strlcpy(chip->name, "AFE (dword)", sizeof(chip->name));
  1005. return 0;
  1006. case 5: /* Video Block - read dword */
  1007. strlcpy(chip->name, "Video (dword)", sizeof(chip->name));
  1008. return 0;
  1009. case 6: /* I2S Block - read dword */
  1010. strlcpy(chip->name, "I2S (dword)", sizeof(chip->name));
  1011. return 0;
  1012. }
  1013. return -EINVAL;
  1014. }
  1015. int cx231xx_g_register(struct file *file, void *priv,
  1016. struct v4l2_dbg_register *reg)
  1017. {
  1018. struct cx231xx_fh *fh = priv;
  1019. struct cx231xx *dev = fh->dev;
  1020. int ret;
  1021. u8 value[4] = { 0, 0, 0, 0 };
  1022. u32 data = 0;
  1023. switch (reg->match.addr) {
  1024. case 0: /* Cx231xx - internal registers */
  1025. ret = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER,
  1026. (u16)reg->reg, value, 4);
  1027. reg->val = value[0] | value[1] << 8 |
  1028. value[2] << 16 | value[3] << 24;
  1029. reg->size = 4;
  1030. break;
  1031. case 1: /* AFE - read byte */
  1032. ret = cx231xx_read_i2c_data(dev, AFE_DEVICE_ADDRESS,
  1033. (u16)reg->reg, 2, &data, 1);
  1034. reg->val = data;
  1035. reg->size = 1;
  1036. break;
  1037. case 2: /* Video Block - read byte */
  1038. ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
  1039. (u16)reg->reg, 2, &data, 1);
  1040. reg->val = data;
  1041. reg->size = 1;
  1042. break;
  1043. case 3: /* I2S block - read byte */
  1044. ret = cx231xx_read_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
  1045. (u16)reg->reg, 1, &data, 1);
  1046. reg->val = data;
  1047. reg->size = 1;
  1048. break;
  1049. case 4: /* AFE - read dword */
  1050. ret = cx231xx_read_i2c_data(dev, AFE_DEVICE_ADDRESS,
  1051. (u16)reg->reg, 2, &data, 4);
  1052. reg->val = data;
  1053. reg->size = 4;
  1054. break;
  1055. case 5: /* Video Block - read dword */
  1056. ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
  1057. (u16)reg->reg, 2, &data, 4);
  1058. reg->val = data;
  1059. reg->size = 4;
  1060. break;
  1061. case 6: /* I2S Block - read dword */
  1062. ret = cx231xx_read_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
  1063. (u16)reg->reg, 1, &data, 4);
  1064. reg->val = data;
  1065. reg->size = 4;
  1066. break;
  1067. default:
  1068. return -EINVAL;
  1069. }
  1070. return ret < 0 ? ret : 0;
  1071. }
  1072. int cx231xx_s_register(struct file *file, void *priv,
  1073. const struct v4l2_dbg_register *reg)
  1074. {
  1075. struct cx231xx_fh *fh = priv;
  1076. struct cx231xx *dev = fh->dev;
  1077. int ret;
  1078. u8 data[4] = { 0, 0, 0, 0 };
  1079. switch (reg->match.addr) {
  1080. case 0: /* cx231xx internal registers */
  1081. data[0] = (u8) reg->val;
  1082. data[1] = (u8) (reg->val >> 8);
  1083. data[2] = (u8) (reg->val >> 16);
  1084. data[3] = (u8) (reg->val >> 24);
  1085. ret = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
  1086. (u16)reg->reg, data, 4);
  1087. break;
  1088. case 1: /* AFE - write byte */
  1089. ret = cx231xx_write_i2c_data(dev, AFE_DEVICE_ADDRESS,
  1090. (u16)reg->reg, 2, reg->val, 1);
  1091. break;
  1092. case 2: /* Video Block - write byte */
  1093. ret = cx231xx_write_i2c_data(dev, VID_BLK_I2C_ADDRESS,
  1094. (u16)reg->reg, 2, reg->val, 1);
  1095. break;
  1096. case 3: /* I2S block - write byte */
  1097. ret = cx231xx_write_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
  1098. (u16)reg->reg, 1, reg->val, 1);
  1099. break;
  1100. case 4: /* AFE - write dword */
  1101. ret = cx231xx_write_i2c_data(dev, AFE_DEVICE_ADDRESS,
  1102. (u16)reg->reg, 2, reg->val, 4);
  1103. break;
  1104. case 5: /* Video Block - write dword */
  1105. ret = cx231xx_write_i2c_data(dev, VID_BLK_I2C_ADDRESS,
  1106. (u16)reg->reg, 2, reg->val, 4);
  1107. break;
  1108. case 6: /* I2S block - write dword */
  1109. ret = cx231xx_write_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
  1110. (u16)reg->reg, 1, reg->val, 4);
  1111. break;
  1112. default:
  1113. return -EINVAL;
  1114. }
  1115. return ret < 0 ? ret : 0;
  1116. }
  1117. #endif
  1118. static int vidioc_cropcap(struct file *file, void *priv,
  1119. struct v4l2_cropcap *cc)
  1120. {
  1121. struct cx231xx_fh *fh = priv;
  1122. struct cx231xx *dev = fh->dev;
  1123. if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1124. return -EINVAL;
  1125. cc->bounds.left = 0;
  1126. cc->bounds.top = 0;
  1127. cc->bounds.width = dev->width;
  1128. cc->bounds.height = dev->height;
  1129. cc->defrect = cc->bounds;
  1130. cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
  1131. cc->pixelaspect.denominator = 59;
  1132. return 0;
  1133. }
  1134. static int vidioc_streamon(struct file *file, void *priv,
  1135. enum v4l2_buf_type type)
  1136. {
  1137. struct cx231xx_fh *fh = priv;
  1138. struct cx231xx *dev = fh->dev;
  1139. int rc;
  1140. rc = check_dev(dev);
  1141. if (rc < 0)
  1142. return rc;
  1143. rc = res_get(fh);
  1144. if (likely(rc >= 0))
  1145. rc = videobuf_streamon(&fh->vb_vidq);
  1146. call_all(dev, video, s_stream, 1);
  1147. return rc;
  1148. }
  1149. static int vidioc_streamoff(struct file *file, void *priv,
  1150. enum v4l2_buf_type type)
  1151. {
  1152. struct cx231xx_fh *fh = priv;
  1153. struct cx231xx *dev = fh->dev;
  1154. int rc;
  1155. rc = check_dev(dev);
  1156. if (rc < 0)
  1157. return rc;
  1158. if (type != fh->type)
  1159. return -EINVAL;
  1160. cx25840_call(dev, video, s_stream, 0);
  1161. videobuf_streamoff(&fh->vb_vidq);
  1162. res_free(fh);
  1163. return 0;
  1164. }
  1165. int cx231xx_querycap(struct file *file, void *priv,
  1166. struct v4l2_capability *cap)
  1167. {
  1168. struct video_device *vdev = video_devdata(file);
  1169. struct cx231xx_fh *fh = priv;
  1170. struct cx231xx *dev = fh->dev;
  1171. strlcpy(cap->driver, "cx231xx", sizeof(cap->driver));
  1172. strlcpy(cap->card, cx231xx_boards[dev->model].name, sizeof(cap->card));
  1173. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  1174. if (vdev->vfl_type == VFL_TYPE_RADIO)
  1175. cap->device_caps = V4L2_CAP_RADIO;
  1176. else {
  1177. cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  1178. if (vdev->vfl_type == VFL_TYPE_VBI)
  1179. cap->device_caps |= V4L2_CAP_VBI_CAPTURE;
  1180. else
  1181. cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
  1182. }
  1183. if (dev->tuner_type != TUNER_ABSENT)
  1184. cap->device_caps |= V4L2_CAP_TUNER;
  1185. cap->capabilities = cap->device_caps | V4L2_CAP_READWRITE |
  1186. V4L2_CAP_VBI_CAPTURE | V4L2_CAP_VIDEO_CAPTURE |
  1187. V4L2_CAP_STREAMING | V4L2_CAP_DEVICE_CAPS;
  1188. if (dev->radio_dev)
  1189. cap->capabilities |= V4L2_CAP_RADIO;
  1190. return 0;
  1191. }
  1192. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  1193. struct v4l2_fmtdesc *f)
  1194. {
  1195. if (unlikely(f->index >= ARRAY_SIZE(format)))
  1196. return -EINVAL;
  1197. strlcpy(f->description, format[f->index].name, sizeof(f->description));
  1198. f->pixelformat = format[f->index].fourcc;
  1199. return 0;
  1200. }
  1201. /* RAW VBI ioctls */
  1202. static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
  1203. struct v4l2_format *f)
  1204. {
  1205. struct cx231xx_fh *fh = priv;
  1206. struct cx231xx *dev = fh->dev;
  1207. f->fmt.vbi.sampling_rate = 6750000 * 4;
  1208. f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
  1209. f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  1210. f->fmt.vbi.offset = 0;
  1211. f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
  1212. PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
  1213. f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
  1214. PAL_VBI_LINES : NTSC_VBI_LINES;
  1215. f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
  1216. PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
  1217. f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
  1218. memset(f->fmt.vbi.reserved, 0, sizeof(f->fmt.vbi.reserved));
  1219. return 0;
  1220. }
  1221. static int vidioc_try_fmt_vbi_cap(struct file *file, void *priv,
  1222. struct v4l2_format *f)
  1223. {
  1224. struct cx231xx_fh *fh = priv;
  1225. struct cx231xx *dev = fh->dev;
  1226. f->fmt.vbi.sampling_rate = 6750000 * 4;
  1227. f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
  1228. f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  1229. f->fmt.vbi.offset = 0;
  1230. f->fmt.vbi.flags = 0;
  1231. f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
  1232. PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
  1233. f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
  1234. PAL_VBI_LINES : NTSC_VBI_LINES;
  1235. f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
  1236. PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
  1237. f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
  1238. memset(f->fmt.vbi.reserved, 0, sizeof(f->fmt.vbi.reserved));
  1239. return 0;
  1240. }
  1241. static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
  1242. struct v4l2_format *f)
  1243. {
  1244. struct cx231xx_fh *fh = priv;
  1245. struct cx231xx *dev = fh->dev;
  1246. if (dev->vbi_stream_on && !fh->stream_on) {
  1247. cx231xx_errdev("%s device in use by another fh\n", __func__);
  1248. return -EBUSY;
  1249. }
  1250. return vidioc_try_fmt_vbi_cap(file, priv, f);
  1251. }
  1252. static int vidioc_reqbufs(struct file *file, void *priv,
  1253. struct v4l2_requestbuffers *rb)
  1254. {
  1255. struct cx231xx_fh *fh = priv;
  1256. struct cx231xx *dev = fh->dev;
  1257. int rc;
  1258. rc = check_dev(dev);
  1259. if (rc < 0)
  1260. return rc;
  1261. return videobuf_reqbufs(&fh->vb_vidq, rb);
  1262. }
  1263. static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1264. {
  1265. struct cx231xx_fh *fh = priv;
  1266. struct cx231xx *dev = fh->dev;
  1267. int rc;
  1268. rc = check_dev(dev);
  1269. if (rc < 0)
  1270. return rc;
  1271. return videobuf_querybuf(&fh->vb_vidq, b);
  1272. }
  1273. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1274. {
  1275. struct cx231xx_fh *fh = priv;
  1276. struct cx231xx *dev = fh->dev;
  1277. int rc;
  1278. rc = check_dev(dev);
  1279. if (rc < 0)
  1280. return rc;
  1281. return videobuf_qbuf(&fh->vb_vidq, b);
  1282. }
  1283. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1284. {
  1285. struct cx231xx_fh *fh = priv;
  1286. struct cx231xx *dev = fh->dev;
  1287. int rc;
  1288. rc = check_dev(dev);
  1289. if (rc < 0)
  1290. return rc;
  1291. return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK);
  1292. }
  1293. /* ----------------------------------------------------------- */
  1294. /* RADIO ESPECIFIC IOCTLS */
  1295. /* ----------------------------------------------------------- */
  1296. static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
  1297. {
  1298. struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
  1299. if (t->index)
  1300. return -EINVAL;
  1301. strcpy(t->name, "Radio");
  1302. call_all(dev, tuner, g_tuner, t);
  1303. return 0;
  1304. }
  1305. static int radio_s_tuner(struct file *file, void *priv, const struct v4l2_tuner *t)
  1306. {
  1307. struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
  1308. if (t->index)
  1309. return -EINVAL;
  1310. call_all(dev, tuner, s_tuner, t);
  1311. return 0;
  1312. }
  1313. /*
  1314. * cx231xx_v4l2_open()
  1315. * inits the device and starts isoc transfer
  1316. */
  1317. static int cx231xx_v4l2_open(struct file *filp)
  1318. {
  1319. int errCode = 0, radio = 0;
  1320. struct video_device *vdev = video_devdata(filp);
  1321. struct cx231xx *dev = video_drvdata(filp);
  1322. struct cx231xx_fh *fh;
  1323. enum v4l2_buf_type fh_type = 0;
  1324. switch (vdev->vfl_type) {
  1325. case VFL_TYPE_GRABBER:
  1326. fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1327. break;
  1328. case VFL_TYPE_VBI:
  1329. fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
  1330. break;
  1331. case VFL_TYPE_RADIO:
  1332. radio = 1;
  1333. break;
  1334. }
  1335. cx231xx_videodbg("open dev=%s type=%s users=%d\n",
  1336. video_device_node_name(vdev), v4l2_type_names[fh_type],
  1337. dev->users);
  1338. #if 0
  1339. errCode = cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
  1340. if (errCode < 0) {
  1341. cx231xx_errdev
  1342. ("Device locked on digital mode. Can't open analog\n");
  1343. return -EBUSY;
  1344. }
  1345. #endif
  1346. fh = kzalloc(sizeof(struct cx231xx_fh), GFP_KERNEL);
  1347. if (!fh) {
  1348. cx231xx_errdev("cx231xx-video.c: Out of memory?!\n");
  1349. return -ENOMEM;
  1350. }
  1351. if (mutex_lock_interruptible(&dev->lock)) {
  1352. kfree(fh);
  1353. return -ERESTARTSYS;
  1354. }
  1355. fh->dev = dev;
  1356. fh->type = fh_type;
  1357. filp->private_data = fh;
  1358. v4l2_fh_init(&fh->fh, vdev);
  1359. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
  1360. /* Power up in Analog TV mode */
  1361. if (dev->board.external_av)
  1362. cx231xx_set_power_mode(dev,
  1363. POLARIS_AVMODE_ENXTERNAL_AV);
  1364. else
  1365. cx231xx_set_power_mode(dev, POLARIS_AVMODE_ANALOGT_TV);
  1366. #if 0
  1367. cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
  1368. #endif
  1369. /* set video alternate setting */
  1370. cx231xx_set_video_alternate(dev);
  1371. /* Needed, since GPIO might have disabled power of
  1372. some i2c device */
  1373. cx231xx_config_i2c(dev);
  1374. /* device needs to be initialized before isoc transfer */
  1375. dev->video_input = dev->video_input > 2 ? 2 : dev->video_input;
  1376. }
  1377. if (radio) {
  1378. cx231xx_videodbg("video_open: setting radio device\n");
  1379. /* cx231xx_start_radio(dev); */
  1380. call_all(dev, tuner, s_radio);
  1381. }
  1382. dev->users++;
  1383. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1384. videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_video_qops,
  1385. NULL, &dev->video_mode.slock,
  1386. fh->type, V4L2_FIELD_INTERLACED,
  1387. sizeof(struct cx231xx_buffer),
  1388. fh, &dev->lock);
  1389. if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
  1390. /* Set the required alternate setting VBI interface works in
  1391. Bulk mode only */
  1392. cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
  1393. videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_vbi_qops,
  1394. NULL, &dev->vbi_mode.slock,
  1395. fh->type, V4L2_FIELD_SEQ_TB,
  1396. sizeof(struct cx231xx_buffer),
  1397. fh, &dev->lock);
  1398. }
  1399. mutex_unlock(&dev->lock);
  1400. v4l2_fh_add(&fh->fh);
  1401. return errCode;
  1402. }
  1403. /*
  1404. * cx231xx_realease_resources()
  1405. * unregisters the v4l2,i2c and usb devices
  1406. * called when the device gets disconected or at module unload
  1407. */
  1408. void cx231xx_release_analog_resources(struct cx231xx *dev)
  1409. {
  1410. /*FIXME: I2C IR should be disconnected */
  1411. if (dev->radio_dev) {
  1412. if (video_is_registered(dev->radio_dev))
  1413. video_unregister_device(dev->radio_dev);
  1414. else
  1415. video_device_release(dev->radio_dev);
  1416. dev->radio_dev = NULL;
  1417. }
  1418. if (dev->vbi_dev) {
  1419. cx231xx_info("V4L2 device %s deregistered\n",
  1420. video_device_node_name(dev->vbi_dev));
  1421. if (video_is_registered(dev->vbi_dev))
  1422. video_unregister_device(dev->vbi_dev);
  1423. else
  1424. video_device_release(dev->vbi_dev);
  1425. dev->vbi_dev = NULL;
  1426. }
  1427. if (dev->vdev) {
  1428. cx231xx_info("V4L2 device %s deregistered\n",
  1429. video_device_node_name(dev->vdev));
  1430. if (dev->board.has_417)
  1431. cx231xx_417_unregister(dev);
  1432. if (video_is_registered(dev->vdev))
  1433. video_unregister_device(dev->vdev);
  1434. else
  1435. video_device_release(dev->vdev);
  1436. dev->vdev = NULL;
  1437. }
  1438. v4l2_ctrl_handler_free(&dev->ctrl_handler);
  1439. v4l2_ctrl_handler_free(&dev->radio_ctrl_handler);
  1440. }
  1441. /*
  1442. * cx231xx_close()
  1443. * stops streaming and deallocates all resources allocated by the v4l2
  1444. * calls and ioctls
  1445. */
  1446. static int cx231xx_close(struct file *filp)
  1447. {
  1448. struct cx231xx_fh *fh = filp->private_data;
  1449. struct cx231xx *dev = fh->dev;
  1450. cx231xx_videodbg("users=%d\n", dev->users);
  1451. cx231xx_videodbg("users=%d\n", dev->users);
  1452. if (res_check(fh))
  1453. res_free(fh);
  1454. /*
  1455. * To workaround error number=-71 on EP0 for VideoGrabber,
  1456. * need exclude following.
  1457. * FIXME: It is probably safe to remove most of these, as we're
  1458. * now avoiding the alternate setting for INDEX_VANC
  1459. */
  1460. if (!dev->board.no_alt_vanc)
  1461. if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
  1462. videobuf_stop(&fh->vb_vidq);
  1463. videobuf_mmap_free(&fh->vb_vidq);
  1464. /* the device is already disconnect,
  1465. free the remaining resources */
  1466. if (dev->state & DEV_DISCONNECTED) {
  1467. if (atomic_read(&dev->devlist_count) > 0) {
  1468. cx231xx_release_resources(dev);
  1469. fh->dev = NULL;
  1470. return 0;
  1471. }
  1472. return 0;
  1473. }
  1474. /* do this before setting alternate! */
  1475. cx231xx_uninit_vbi_isoc(dev);
  1476. /* set alternate 0 */
  1477. if (!dev->vbi_or_sliced_cc_mode)
  1478. cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
  1479. else
  1480. cx231xx_set_alt_setting(dev, INDEX_HANC, 0);
  1481. v4l2_fh_del(&fh->fh);
  1482. v4l2_fh_exit(&fh->fh);
  1483. kfree(fh);
  1484. dev->users--;
  1485. wake_up_interruptible_nr(&dev->open, 1);
  1486. return 0;
  1487. }
  1488. v4l2_fh_del(&fh->fh);
  1489. dev->users--;
  1490. if (!dev->users) {
  1491. videobuf_stop(&fh->vb_vidq);
  1492. videobuf_mmap_free(&fh->vb_vidq);
  1493. /* the device is already disconnect,
  1494. free the remaining resources */
  1495. if (dev->state & DEV_DISCONNECTED) {
  1496. cx231xx_release_resources(dev);
  1497. fh->dev = NULL;
  1498. return 0;
  1499. }
  1500. /* Save some power by putting tuner to sleep */
  1501. call_all(dev, core, s_power, 0);
  1502. /* do this before setting alternate! */
  1503. if (dev->USE_ISO)
  1504. cx231xx_uninit_isoc(dev);
  1505. else
  1506. cx231xx_uninit_bulk(dev);
  1507. cx231xx_set_mode(dev, CX231XX_SUSPEND);
  1508. /* set alternate 0 */
  1509. cx231xx_set_alt_setting(dev, INDEX_VIDEO, 0);
  1510. }
  1511. v4l2_fh_exit(&fh->fh);
  1512. kfree(fh);
  1513. wake_up_interruptible_nr(&dev->open, 1);
  1514. return 0;
  1515. }
  1516. static int cx231xx_v4l2_close(struct file *filp)
  1517. {
  1518. struct cx231xx_fh *fh = filp->private_data;
  1519. struct cx231xx *dev = fh->dev;
  1520. int rc;
  1521. mutex_lock(&dev->lock);
  1522. rc = cx231xx_close(filp);
  1523. mutex_unlock(&dev->lock);
  1524. return rc;
  1525. }
  1526. /*
  1527. * cx231xx_v4l2_read()
  1528. * will allocate buffers when called for the first time
  1529. */
  1530. static ssize_t
  1531. cx231xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
  1532. loff_t *pos)
  1533. {
  1534. struct cx231xx_fh *fh = filp->private_data;
  1535. struct cx231xx *dev = fh->dev;
  1536. int rc;
  1537. rc = check_dev(dev);
  1538. if (rc < 0)
  1539. return rc;
  1540. if ((fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
  1541. (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)) {
  1542. rc = res_get(fh);
  1543. if (unlikely(rc < 0))
  1544. return rc;
  1545. if (mutex_lock_interruptible(&dev->lock))
  1546. return -ERESTARTSYS;
  1547. rc = videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
  1548. filp->f_flags & O_NONBLOCK);
  1549. mutex_unlock(&dev->lock);
  1550. return rc;
  1551. }
  1552. return 0;
  1553. }
  1554. /*
  1555. * cx231xx_v4l2_poll()
  1556. * will allocate buffers when called for the first time
  1557. */
  1558. static unsigned int cx231xx_v4l2_poll(struct file *filp, poll_table *wait)
  1559. {
  1560. unsigned long req_events = poll_requested_events(wait);
  1561. struct cx231xx_fh *fh = filp->private_data;
  1562. struct cx231xx *dev = fh->dev;
  1563. unsigned res = 0;
  1564. int rc;
  1565. rc = check_dev(dev);
  1566. if (rc < 0)
  1567. return POLLERR;
  1568. rc = res_get(fh);
  1569. if (unlikely(rc < 0))
  1570. return POLLERR;
  1571. if (v4l2_event_pending(&fh->fh))
  1572. res |= POLLPRI;
  1573. else
  1574. poll_wait(filp, &fh->fh.wait, wait);
  1575. if (!(req_events & (POLLIN | POLLRDNORM)))
  1576. return res;
  1577. if ((V4L2_BUF_TYPE_VIDEO_CAPTURE == fh->type) ||
  1578. (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)) {
  1579. mutex_lock(&dev->lock);
  1580. res |= videobuf_poll_stream(filp, &fh->vb_vidq, wait);
  1581. mutex_unlock(&dev->lock);
  1582. return res;
  1583. }
  1584. return res | POLLERR;
  1585. }
  1586. /*
  1587. * cx231xx_v4l2_mmap()
  1588. */
  1589. static int cx231xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
  1590. {
  1591. struct cx231xx_fh *fh = filp->private_data;
  1592. struct cx231xx *dev = fh->dev;
  1593. int rc;
  1594. rc = check_dev(dev);
  1595. if (rc < 0)
  1596. return rc;
  1597. rc = res_get(fh);
  1598. if (unlikely(rc < 0))
  1599. return rc;
  1600. if (mutex_lock_interruptible(&dev->lock))
  1601. return -ERESTARTSYS;
  1602. rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
  1603. mutex_unlock(&dev->lock);
  1604. cx231xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
  1605. (unsigned long)vma->vm_start,
  1606. (unsigned long)vma->vm_end -
  1607. (unsigned long)vma->vm_start, rc);
  1608. return rc;
  1609. }
  1610. static const struct v4l2_file_operations cx231xx_v4l_fops = {
  1611. .owner = THIS_MODULE,
  1612. .open = cx231xx_v4l2_open,
  1613. .release = cx231xx_v4l2_close,
  1614. .read = cx231xx_v4l2_read,
  1615. .poll = cx231xx_v4l2_poll,
  1616. .mmap = cx231xx_v4l2_mmap,
  1617. .unlocked_ioctl = video_ioctl2,
  1618. };
  1619. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1620. .vidioc_querycap = cx231xx_querycap,
  1621. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1622. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1623. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1624. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1625. .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
  1626. .vidioc_try_fmt_vbi_cap = vidioc_try_fmt_vbi_cap,
  1627. .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
  1628. .vidioc_cropcap = vidioc_cropcap,
  1629. .vidioc_reqbufs = vidioc_reqbufs,
  1630. .vidioc_querybuf = vidioc_querybuf,
  1631. .vidioc_qbuf = vidioc_qbuf,
  1632. .vidioc_dqbuf = vidioc_dqbuf,
  1633. .vidioc_s_std = vidioc_s_std,
  1634. .vidioc_g_std = vidioc_g_std,
  1635. .vidioc_enum_input = cx231xx_enum_input,
  1636. .vidioc_g_input = cx231xx_g_input,
  1637. .vidioc_s_input = cx231xx_s_input,
  1638. .vidioc_streamon = vidioc_streamon,
  1639. .vidioc_streamoff = vidioc_streamoff,
  1640. .vidioc_g_tuner = cx231xx_g_tuner,
  1641. .vidioc_s_tuner = cx231xx_s_tuner,
  1642. .vidioc_g_frequency = cx231xx_g_frequency,
  1643. .vidioc_s_frequency = cx231xx_s_frequency,
  1644. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1645. .vidioc_g_chip_info = cx231xx_g_chip_info,
  1646. .vidioc_g_register = cx231xx_g_register,
  1647. .vidioc_s_register = cx231xx_s_register,
  1648. #endif
  1649. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1650. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1651. };
  1652. static struct video_device cx231xx_vbi_template;
  1653. static const struct video_device cx231xx_video_template = {
  1654. .fops = &cx231xx_v4l_fops,
  1655. .release = video_device_release,
  1656. .ioctl_ops = &video_ioctl_ops,
  1657. .tvnorms = V4L2_STD_ALL,
  1658. };
  1659. static const struct v4l2_file_operations radio_fops = {
  1660. .owner = THIS_MODULE,
  1661. .open = cx231xx_v4l2_open,
  1662. .release = cx231xx_v4l2_close,
  1663. .poll = v4l2_ctrl_poll,
  1664. .unlocked_ioctl = video_ioctl2,
  1665. };
  1666. static const struct v4l2_ioctl_ops radio_ioctl_ops = {
  1667. .vidioc_querycap = cx231xx_querycap,
  1668. .vidioc_g_tuner = radio_g_tuner,
  1669. .vidioc_s_tuner = radio_s_tuner,
  1670. .vidioc_g_frequency = cx231xx_g_frequency,
  1671. .vidioc_s_frequency = cx231xx_s_frequency,
  1672. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1673. .vidioc_g_chip_info = cx231xx_g_chip_info,
  1674. .vidioc_g_register = cx231xx_g_register,
  1675. .vidioc_s_register = cx231xx_s_register,
  1676. #endif
  1677. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1678. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1679. };
  1680. static struct video_device cx231xx_radio_template = {
  1681. .name = "cx231xx-radio",
  1682. .fops = &radio_fops,
  1683. .ioctl_ops = &radio_ioctl_ops,
  1684. };
  1685. /******************************** usb interface ******************************/
  1686. static struct video_device *cx231xx_vdev_init(struct cx231xx *dev,
  1687. const struct video_device
  1688. *template, const char *type_name)
  1689. {
  1690. struct video_device *vfd;
  1691. vfd = video_device_alloc();
  1692. if (NULL == vfd)
  1693. return NULL;
  1694. *vfd = *template;
  1695. vfd->v4l2_dev = &dev->v4l2_dev;
  1696. vfd->release = video_device_release;
  1697. vfd->debug = video_debug;
  1698. vfd->lock = &dev->lock;
  1699. set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
  1700. snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name);
  1701. video_set_drvdata(vfd, dev);
  1702. if (dev->tuner_type == TUNER_ABSENT) {
  1703. v4l2_disable_ioctl(vfd, VIDIOC_G_FREQUENCY);
  1704. v4l2_disable_ioctl(vfd, VIDIOC_S_FREQUENCY);
  1705. v4l2_disable_ioctl(vfd, VIDIOC_G_TUNER);
  1706. v4l2_disable_ioctl(vfd, VIDIOC_S_TUNER);
  1707. }
  1708. return vfd;
  1709. }
  1710. int cx231xx_register_analog_devices(struct cx231xx *dev)
  1711. {
  1712. int ret;
  1713. cx231xx_info("%s: v4l2 driver version %s\n",
  1714. dev->name, CX231XX_VERSION);
  1715. /* set default norm */
  1716. dev->norm = V4L2_STD_PAL;
  1717. dev->width = norm_maxw(dev);
  1718. dev->height = norm_maxh(dev);
  1719. dev->interlaced = 0;
  1720. /* Analog specific initialization */
  1721. dev->format = &format[0];
  1722. /* Set the initial input */
  1723. video_mux(dev, dev->video_input);
  1724. call_all(dev, core, s_std, dev->norm);
  1725. v4l2_ctrl_handler_init(&dev->ctrl_handler, 10);
  1726. v4l2_ctrl_handler_init(&dev->radio_ctrl_handler, 5);
  1727. if (dev->sd_cx25840) {
  1728. v4l2_ctrl_add_handler(&dev->ctrl_handler,
  1729. dev->sd_cx25840->ctrl_handler, NULL);
  1730. v4l2_ctrl_add_handler(&dev->radio_ctrl_handler,
  1731. dev->sd_cx25840->ctrl_handler,
  1732. v4l2_ctrl_radio_filter);
  1733. }
  1734. if (dev->ctrl_handler.error)
  1735. return dev->ctrl_handler.error;
  1736. if (dev->radio_ctrl_handler.error)
  1737. return dev->radio_ctrl_handler.error;
  1738. /* enable vbi capturing */
  1739. /* write code here... */
  1740. /* allocate and fill video video_device struct */
  1741. dev->vdev = cx231xx_vdev_init(dev, &cx231xx_video_template, "video");
  1742. if (!dev->vdev) {
  1743. cx231xx_errdev("cannot allocate video_device.\n");
  1744. return -ENODEV;
  1745. }
  1746. dev->vdev->ctrl_handler = &dev->ctrl_handler;
  1747. /* register v4l2 video video_device */
  1748. ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
  1749. video_nr[dev->devno]);
  1750. if (ret) {
  1751. cx231xx_errdev("unable to register video device (error=%i).\n",
  1752. ret);
  1753. return ret;
  1754. }
  1755. cx231xx_info("%s/0: registered device %s [v4l2]\n",
  1756. dev->name, video_device_node_name(dev->vdev));
  1757. /* Initialize VBI template */
  1758. cx231xx_vbi_template = cx231xx_video_template;
  1759. strcpy(cx231xx_vbi_template.name, "cx231xx-vbi");
  1760. /* Allocate and fill vbi video_device struct */
  1761. dev->vbi_dev = cx231xx_vdev_init(dev, &cx231xx_vbi_template, "vbi");
  1762. if (!dev->vbi_dev) {
  1763. cx231xx_errdev("cannot allocate video_device.\n");
  1764. return -ENODEV;
  1765. }
  1766. dev->vbi_dev->ctrl_handler = &dev->ctrl_handler;
  1767. /* register v4l2 vbi video_device */
  1768. ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
  1769. vbi_nr[dev->devno]);
  1770. if (ret < 0) {
  1771. cx231xx_errdev("unable to register vbi device\n");
  1772. return ret;
  1773. }
  1774. cx231xx_info("%s/0: registered device %s\n",
  1775. dev->name, video_device_node_name(dev->vbi_dev));
  1776. if (cx231xx_boards[dev->model].radio.type == CX231XX_RADIO) {
  1777. dev->radio_dev = cx231xx_vdev_init(dev, &cx231xx_radio_template,
  1778. "radio");
  1779. if (!dev->radio_dev) {
  1780. cx231xx_errdev("cannot allocate video_device.\n");
  1781. return -ENODEV;
  1782. }
  1783. dev->radio_dev->ctrl_handler = &dev->radio_ctrl_handler;
  1784. ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
  1785. radio_nr[dev->devno]);
  1786. if (ret < 0) {
  1787. cx231xx_errdev("can't register radio device\n");
  1788. return ret;
  1789. }
  1790. cx231xx_info("Registered radio device as %s\n",
  1791. video_device_node_name(dev->radio_dev));
  1792. }
  1793. cx231xx_info("V4L2 device registered as %s and %s\n",
  1794. video_device_node_name(dev->vdev),
  1795. video_device_node_name(dev->vbi_dev));
  1796. return 0;
  1797. }