cx231xx-video.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615
  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-chip-ident.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. /* supported controls */
  84. /* Common to all boards */
  85. /* ------------------------------------------------------------------- */
  86. static const struct v4l2_queryctrl no_ctl = {
  87. .name = "42",
  88. .flags = V4L2_CTRL_FLAG_DISABLED,
  89. };
  90. static struct cx231xx_ctrl cx231xx_ctls[] = {
  91. /* --- video --- */
  92. {
  93. .v = {
  94. .id = V4L2_CID_BRIGHTNESS,
  95. .name = "Brightness",
  96. .minimum = 0x00,
  97. .maximum = 0xff,
  98. .step = 1,
  99. .default_value = 0x7f,
  100. .type = V4L2_CTRL_TYPE_INTEGER,
  101. },
  102. .off = 128,
  103. .reg = LUMA_CTRL,
  104. .mask = 0x00ff,
  105. .shift = 0,
  106. }, {
  107. .v = {
  108. .id = V4L2_CID_CONTRAST,
  109. .name = "Contrast",
  110. .minimum = 0,
  111. .maximum = 0xff,
  112. .step = 1,
  113. .default_value = 0x3f,
  114. .type = V4L2_CTRL_TYPE_INTEGER,
  115. },
  116. .off = 0,
  117. .reg = LUMA_CTRL,
  118. .mask = 0xff00,
  119. .shift = 8,
  120. }, {
  121. .v = {
  122. .id = V4L2_CID_HUE,
  123. .name = "Hue",
  124. .minimum = 0,
  125. .maximum = 0xff,
  126. .step = 1,
  127. .default_value = 0x7f,
  128. .type = V4L2_CTRL_TYPE_INTEGER,
  129. },
  130. .off = 128,
  131. .reg = CHROMA_CTRL,
  132. .mask = 0xff0000,
  133. .shift = 16,
  134. }, {
  135. /* strictly, this only describes only U saturation.
  136. * V saturation is handled specially through code.
  137. */
  138. .v = {
  139. .id = V4L2_CID_SATURATION,
  140. .name = "Saturation",
  141. .minimum = 0,
  142. .maximum = 0xff,
  143. .step = 1,
  144. .default_value = 0x7f,
  145. .type = V4L2_CTRL_TYPE_INTEGER,
  146. },
  147. .off = 0,
  148. .reg = CHROMA_CTRL,
  149. .mask = 0x00ff,
  150. .shift = 0,
  151. }, {
  152. /* --- audio --- */
  153. .v = {
  154. .id = V4L2_CID_AUDIO_MUTE,
  155. .name = "Mute",
  156. .minimum = 0,
  157. .maximum = 1,
  158. .default_value = 1,
  159. .type = V4L2_CTRL_TYPE_BOOLEAN,
  160. },
  161. .reg = PATH1_CTL1,
  162. .mask = (0x1f << 24),
  163. .shift = 24,
  164. }, {
  165. .v = {
  166. .id = V4L2_CID_AUDIO_VOLUME,
  167. .name = "Volume",
  168. .minimum = 0,
  169. .maximum = 0x3f,
  170. .step = 1,
  171. .default_value = 0x3f,
  172. .type = V4L2_CTRL_TYPE_INTEGER,
  173. },
  174. .reg = PATH1_VOL_CTL,
  175. .mask = 0xff,
  176. .shift = 0,
  177. }
  178. };
  179. static const int CX231XX_CTLS = ARRAY_SIZE(cx231xx_ctls);
  180. static const u32 cx231xx_user_ctrls[] = {
  181. V4L2_CID_USER_CLASS,
  182. V4L2_CID_BRIGHTNESS,
  183. V4L2_CID_CONTRAST,
  184. V4L2_CID_SATURATION,
  185. V4L2_CID_HUE,
  186. V4L2_CID_AUDIO_VOLUME,
  187. #if 0
  188. V4L2_CID_AUDIO_BALANCE,
  189. #endif
  190. V4L2_CID_AUDIO_MUTE,
  191. 0
  192. };
  193. static const u32 *ctrl_classes[] = {
  194. cx231xx_user_ctrls,
  195. NULL
  196. };
  197. /* ------------------------------------------------------------------
  198. Video buffer and parser functions
  199. ------------------------------------------------------------------*/
  200. /*
  201. * Announces that a buffer were filled and request the next
  202. */
  203. static inline void buffer_filled(struct cx231xx *dev,
  204. struct cx231xx_dmaqueue *dma_q,
  205. struct cx231xx_buffer *buf)
  206. {
  207. /* Advice that buffer was filled */
  208. cx231xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
  209. buf->vb.state = VIDEOBUF_DONE;
  210. buf->vb.field_count++;
  211. v4l2_get_timestamp(&buf->vb.ts);
  212. if (dev->USE_ISO)
  213. dev->video_mode.isoc_ctl.buf = NULL;
  214. else
  215. dev->video_mode.bulk_ctl.buf = NULL;
  216. list_del(&buf->vb.queue);
  217. wake_up(&buf->vb.done);
  218. }
  219. static inline void print_err_status(struct cx231xx *dev, int packet, int status)
  220. {
  221. char *errmsg = "Unknown";
  222. switch (status) {
  223. case -ENOENT:
  224. errmsg = "unlinked synchronuously";
  225. break;
  226. case -ECONNRESET:
  227. errmsg = "unlinked asynchronuously";
  228. break;
  229. case -ENOSR:
  230. errmsg = "Buffer error (overrun)";
  231. break;
  232. case -EPIPE:
  233. errmsg = "Stalled (device not responding)";
  234. break;
  235. case -EOVERFLOW:
  236. errmsg = "Babble (bad cable?)";
  237. break;
  238. case -EPROTO:
  239. errmsg = "Bit-stuff error (bad cable?)";
  240. break;
  241. case -EILSEQ:
  242. errmsg = "CRC/Timeout (could be anything)";
  243. break;
  244. case -ETIME:
  245. errmsg = "Device does not respond";
  246. break;
  247. }
  248. if (packet < 0) {
  249. cx231xx_isocdbg("URB status %d [%s].\n", status, errmsg);
  250. } else {
  251. cx231xx_isocdbg("URB packet %d, status %d [%s].\n",
  252. packet, status, errmsg);
  253. }
  254. }
  255. /*
  256. * video-buf generic routine to get the next available buffer
  257. */
  258. static inline void get_next_buf(struct cx231xx_dmaqueue *dma_q,
  259. struct cx231xx_buffer **buf)
  260. {
  261. struct cx231xx_video_mode *vmode =
  262. container_of(dma_q, struct cx231xx_video_mode, vidq);
  263. struct cx231xx *dev = container_of(vmode, struct cx231xx, video_mode);
  264. char *outp;
  265. if (list_empty(&dma_q->active)) {
  266. cx231xx_isocdbg("No active queue to serve\n");
  267. if (dev->USE_ISO)
  268. dev->video_mode.isoc_ctl.buf = NULL;
  269. else
  270. dev->video_mode.bulk_ctl.buf = NULL;
  271. *buf = NULL;
  272. return;
  273. }
  274. /* Get the next buffer */
  275. *buf = list_entry(dma_q->active.next, struct cx231xx_buffer, vb.queue);
  276. /* Cleans up buffer - Useful for testing for frame/URB loss */
  277. outp = videobuf_to_vmalloc(&(*buf)->vb);
  278. memset(outp, 0, (*buf)->vb.size);
  279. if (dev->USE_ISO)
  280. dev->video_mode.isoc_ctl.buf = *buf;
  281. else
  282. dev->video_mode.bulk_ctl.buf = *buf;
  283. return;
  284. }
  285. /*
  286. * Controls the isoc copy of each urb packet
  287. */
  288. static inline int cx231xx_isoc_copy(struct cx231xx *dev, struct urb *urb)
  289. {
  290. struct cx231xx_dmaqueue *dma_q = urb->context;
  291. int i, rc = 1;
  292. unsigned char *p_buffer;
  293. u32 bytes_parsed = 0, buffer_size = 0;
  294. u8 sav_eav = 0;
  295. if (!dev)
  296. return 0;
  297. if (dev->state & DEV_DISCONNECTED)
  298. return 0;
  299. if (urb->status < 0) {
  300. print_err_status(dev, -1, urb->status);
  301. if (urb->status == -ENOENT)
  302. return 0;
  303. }
  304. for (i = 0; i < urb->number_of_packets; i++) {
  305. int status = urb->iso_frame_desc[i].status;
  306. if (status < 0) {
  307. print_err_status(dev, i, status);
  308. if (urb->iso_frame_desc[i].status != -EPROTO)
  309. continue;
  310. }
  311. if (urb->iso_frame_desc[i].actual_length <= 0) {
  312. /* cx231xx_isocdbg("packet %d is empty",i); - spammy */
  313. continue;
  314. }
  315. if (urb->iso_frame_desc[i].actual_length >
  316. dev->video_mode.max_pkt_size) {
  317. cx231xx_isocdbg("packet bigger than packet size");
  318. continue;
  319. }
  320. /* get buffer pointer and length */
  321. p_buffer = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  322. buffer_size = urb->iso_frame_desc[i].actual_length;
  323. bytes_parsed = 0;
  324. if (dma_q->is_partial_line) {
  325. /* Handle the case of a partial line */
  326. sav_eav = dma_q->last_sav;
  327. } else {
  328. /* Check for a SAV/EAV overlapping
  329. the buffer boundary */
  330. sav_eav =
  331. cx231xx_find_boundary_SAV_EAV(p_buffer,
  332. dma_q->partial_buf,
  333. &bytes_parsed);
  334. }
  335. sav_eav &= 0xF0;
  336. /* Get the first line if we have some portion of an SAV/EAV from
  337. the last buffer or a partial line */
  338. if (sav_eav) {
  339. bytes_parsed += cx231xx_get_video_line(dev, dma_q,
  340. sav_eav, /* SAV/EAV */
  341. p_buffer + bytes_parsed, /* p_buffer */
  342. buffer_size - bytes_parsed);/* buf size */
  343. }
  344. /* Now parse data that is completely in this buffer */
  345. /* dma_q->is_partial_line = 0; */
  346. while (bytes_parsed < buffer_size) {
  347. u32 bytes_used = 0;
  348. sav_eav = cx231xx_find_next_SAV_EAV(
  349. p_buffer + bytes_parsed, /* p_buffer */
  350. buffer_size - bytes_parsed, /* buf size */
  351. &bytes_used);/* bytes used to get SAV/EAV */
  352. bytes_parsed += bytes_used;
  353. sav_eav &= 0xF0;
  354. if (sav_eav && (bytes_parsed < buffer_size)) {
  355. bytes_parsed += cx231xx_get_video_line(dev,
  356. dma_q, sav_eav, /* SAV/EAV */
  357. p_buffer + bytes_parsed,/* p_buffer */
  358. buffer_size - bytes_parsed);/*buf size*/
  359. }
  360. }
  361. /* Save the last four bytes of the buffer so we can check the
  362. buffer boundary condition next time */
  363. memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4);
  364. bytes_parsed = 0;
  365. }
  366. return rc;
  367. }
  368. static inline int cx231xx_bulk_copy(struct cx231xx *dev, struct urb *urb)
  369. {
  370. struct cx231xx_dmaqueue *dma_q = urb->context;
  371. int rc = 1;
  372. unsigned char *p_buffer;
  373. u32 bytes_parsed = 0, buffer_size = 0;
  374. u8 sav_eav = 0;
  375. if (!dev)
  376. return 0;
  377. if (dev->state & DEV_DISCONNECTED)
  378. return 0;
  379. if (urb->status < 0) {
  380. print_err_status(dev, -1, urb->status);
  381. if (urb->status == -ENOENT)
  382. return 0;
  383. }
  384. if (1) {
  385. /* get buffer pointer and length */
  386. p_buffer = urb->transfer_buffer;
  387. buffer_size = urb->actual_length;
  388. bytes_parsed = 0;
  389. if (dma_q->is_partial_line) {
  390. /* Handle the case of a partial line */
  391. sav_eav = dma_q->last_sav;
  392. } else {
  393. /* Check for a SAV/EAV overlapping
  394. the buffer boundary */
  395. sav_eav =
  396. cx231xx_find_boundary_SAV_EAV(p_buffer,
  397. dma_q->partial_buf,
  398. &bytes_parsed);
  399. }
  400. sav_eav &= 0xF0;
  401. /* Get the first line if we have some portion of an SAV/EAV from
  402. the last buffer or a partial line */
  403. if (sav_eav) {
  404. bytes_parsed += cx231xx_get_video_line(dev, dma_q,
  405. sav_eav, /* SAV/EAV */
  406. p_buffer + bytes_parsed, /* p_buffer */
  407. buffer_size - bytes_parsed);/* buf size */
  408. }
  409. /* Now parse data that is completely in this buffer */
  410. /* dma_q->is_partial_line = 0; */
  411. while (bytes_parsed < buffer_size) {
  412. u32 bytes_used = 0;
  413. sav_eav = cx231xx_find_next_SAV_EAV(
  414. p_buffer + bytes_parsed, /* p_buffer */
  415. buffer_size - bytes_parsed, /* buf size */
  416. &bytes_used);/* bytes used to get SAV/EAV */
  417. bytes_parsed += bytes_used;
  418. sav_eav &= 0xF0;
  419. if (sav_eav && (bytes_parsed < buffer_size)) {
  420. bytes_parsed += cx231xx_get_video_line(dev,
  421. dma_q, sav_eav, /* SAV/EAV */
  422. p_buffer + bytes_parsed,/* p_buffer */
  423. buffer_size - bytes_parsed);/*buf size*/
  424. }
  425. }
  426. /* Save the last four bytes of the buffer so we can check the
  427. buffer boundary condition next time */
  428. memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4);
  429. bytes_parsed = 0;
  430. }
  431. return rc;
  432. }
  433. u8 cx231xx_find_boundary_SAV_EAV(u8 *p_buffer, u8 *partial_buf,
  434. u32 *p_bytes_used)
  435. {
  436. u32 bytes_used;
  437. u8 boundary_bytes[8];
  438. u8 sav_eav = 0;
  439. *p_bytes_used = 0;
  440. /* Create an array of the last 4 bytes of the last buffer and the first
  441. 4 bytes of the current buffer. */
  442. memcpy(boundary_bytes, partial_buf, 4);
  443. memcpy(boundary_bytes + 4, p_buffer, 4);
  444. /* Check for the SAV/EAV in the boundary buffer */
  445. sav_eav = cx231xx_find_next_SAV_EAV((u8 *)&boundary_bytes, 8,
  446. &bytes_used);
  447. if (sav_eav) {
  448. /* found a boundary SAV/EAV. Updates the bytes used to reflect
  449. only those used in the new buffer */
  450. *p_bytes_used = bytes_used - 4;
  451. }
  452. return sav_eav;
  453. }
  454. u8 cx231xx_find_next_SAV_EAV(u8 *p_buffer, u32 buffer_size, u32 *p_bytes_used)
  455. {
  456. u32 i;
  457. u8 sav_eav = 0;
  458. /*
  459. * Don't search if the buffer size is less than 4. It causes a page
  460. * fault since buffer_size - 4 evaluates to a large number in that
  461. * case.
  462. */
  463. if (buffer_size < 4) {
  464. *p_bytes_used = buffer_size;
  465. return 0;
  466. }
  467. for (i = 0; i < (buffer_size - 3); i++) {
  468. if ((p_buffer[i] == 0xFF) &&
  469. (p_buffer[i + 1] == 0x00) && (p_buffer[i + 2] == 0x00)) {
  470. *p_bytes_used = i + 4;
  471. sav_eav = p_buffer[i + 3];
  472. return sav_eav;
  473. }
  474. }
  475. *p_bytes_used = buffer_size;
  476. return 0;
  477. }
  478. u32 cx231xx_get_video_line(struct cx231xx *dev,
  479. struct cx231xx_dmaqueue *dma_q, u8 sav_eav,
  480. u8 *p_buffer, u32 buffer_size)
  481. {
  482. u32 bytes_copied = 0;
  483. int current_field = -1;
  484. switch (sav_eav) {
  485. case SAV_ACTIVE_VIDEO_FIELD1:
  486. /* looking for skipped line which occurred in PAL 720x480 mode.
  487. In this case, there will be no active data contained
  488. between the SAV and EAV */
  489. if ((buffer_size > 3) && (p_buffer[0] == 0xFF) &&
  490. (p_buffer[1] == 0x00) && (p_buffer[2] == 0x00) &&
  491. ((p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD1) ||
  492. (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD2) ||
  493. (p_buffer[3] == EAV_VBLANK_FIELD1) ||
  494. (p_buffer[3] == EAV_VBLANK_FIELD2)))
  495. return bytes_copied;
  496. current_field = 1;
  497. break;
  498. case SAV_ACTIVE_VIDEO_FIELD2:
  499. /* looking for skipped line which occurred in PAL 720x480 mode.
  500. In this case, there will be no active data contained between
  501. the SAV and EAV */
  502. if ((buffer_size > 3) && (p_buffer[0] == 0xFF) &&
  503. (p_buffer[1] == 0x00) && (p_buffer[2] == 0x00) &&
  504. ((p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD1) ||
  505. (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD2) ||
  506. (p_buffer[3] == EAV_VBLANK_FIELD1) ||
  507. (p_buffer[3] == EAV_VBLANK_FIELD2)))
  508. return bytes_copied;
  509. current_field = 2;
  510. break;
  511. }
  512. dma_q->last_sav = sav_eav;
  513. bytes_copied = cx231xx_copy_video_line(dev, dma_q, p_buffer,
  514. buffer_size, current_field);
  515. return bytes_copied;
  516. }
  517. u32 cx231xx_copy_video_line(struct cx231xx *dev,
  518. struct cx231xx_dmaqueue *dma_q, u8 *p_line,
  519. u32 length, int field_number)
  520. {
  521. u32 bytes_to_copy;
  522. struct cx231xx_buffer *buf;
  523. u32 _line_size = dev->width * 2;
  524. if (dma_q->current_field != field_number)
  525. cx231xx_reset_video_buffer(dev, dma_q);
  526. /* get the buffer pointer */
  527. if (dev->USE_ISO)
  528. buf = dev->video_mode.isoc_ctl.buf;
  529. else
  530. buf = dev->video_mode.bulk_ctl.buf;
  531. /* Remember the field number for next time */
  532. dma_q->current_field = field_number;
  533. bytes_to_copy = dma_q->bytes_left_in_line;
  534. if (bytes_to_copy > length)
  535. bytes_to_copy = length;
  536. if (dma_q->lines_completed >= dma_q->lines_per_field) {
  537. dma_q->bytes_left_in_line -= bytes_to_copy;
  538. dma_q->is_partial_line = (dma_q->bytes_left_in_line == 0) ?
  539. 0 : 1;
  540. return 0;
  541. }
  542. dma_q->is_partial_line = 1;
  543. /* If we don't have a buffer, just return the number of bytes we would
  544. have copied if we had a buffer. */
  545. if (!buf) {
  546. dma_q->bytes_left_in_line -= bytes_to_copy;
  547. dma_q->is_partial_line = (dma_q->bytes_left_in_line == 0)
  548. ? 0 : 1;
  549. return bytes_to_copy;
  550. }
  551. /* copy the data to video buffer */
  552. cx231xx_do_copy(dev, dma_q, p_line, bytes_to_copy);
  553. dma_q->pos += bytes_to_copy;
  554. dma_q->bytes_left_in_line -= bytes_to_copy;
  555. if (dma_q->bytes_left_in_line == 0) {
  556. dma_q->bytes_left_in_line = _line_size;
  557. dma_q->lines_completed++;
  558. dma_q->is_partial_line = 0;
  559. if (cx231xx_is_buffer_done(dev, dma_q) && buf) {
  560. buffer_filled(dev, dma_q, buf);
  561. dma_q->pos = 0;
  562. buf = NULL;
  563. dma_q->lines_completed = 0;
  564. }
  565. }
  566. return bytes_to_copy;
  567. }
  568. void cx231xx_reset_video_buffer(struct cx231xx *dev,
  569. struct cx231xx_dmaqueue *dma_q)
  570. {
  571. struct cx231xx_buffer *buf;
  572. /* handle the switch from field 1 to field 2 */
  573. if (dma_q->current_field == 1) {
  574. if (dma_q->lines_completed >= dma_q->lines_per_field)
  575. dma_q->field1_done = 1;
  576. else
  577. dma_q->field1_done = 0;
  578. }
  579. if (dev->USE_ISO)
  580. buf = dev->video_mode.isoc_ctl.buf;
  581. else
  582. buf = dev->video_mode.bulk_ctl.buf;
  583. if (buf == NULL) {
  584. /* first try to get the buffer */
  585. get_next_buf(dma_q, &buf);
  586. dma_q->pos = 0;
  587. dma_q->field1_done = 0;
  588. dma_q->current_field = -1;
  589. }
  590. /* reset the counters */
  591. dma_q->bytes_left_in_line = dev->width << 1;
  592. dma_q->lines_completed = 0;
  593. }
  594. int cx231xx_do_copy(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
  595. u8 *p_buffer, u32 bytes_to_copy)
  596. {
  597. u8 *p_out_buffer = NULL;
  598. u32 current_line_bytes_copied = 0;
  599. struct cx231xx_buffer *buf;
  600. u32 _line_size = dev->width << 1;
  601. void *startwrite;
  602. int offset, lencopy;
  603. if (dev->USE_ISO)
  604. buf = dev->video_mode.isoc_ctl.buf;
  605. else
  606. buf = dev->video_mode.bulk_ctl.buf;
  607. if (buf == NULL)
  608. return -1;
  609. p_out_buffer = videobuf_to_vmalloc(&buf->vb);
  610. current_line_bytes_copied = _line_size - dma_q->bytes_left_in_line;
  611. /* Offset field 2 one line from the top of the buffer */
  612. offset = (dma_q->current_field == 1) ? 0 : _line_size;
  613. /* Offset for field 2 */
  614. startwrite = p_out_buffer + offset;
  615. /* lines already completed in the current field */
  616. startwrite += (dma_q->lines_completed * _line_size * 2);
  617. /* bytes already completed in the current line */
  618. startwrite += current_line_bytes_copied;
  619. lencopy = dma_q->bytes_left_in_line > bytes_to_copy ?
  620. bytes_to_copy : dma_q->bytes_left_in_line;
  621. if ((u8 *)(startwrite + lencopy) > (u8 *)(p_out_buffer + buf->vb.size))
  622. return 0;
  623. /* The below copies the UYVY data straight into video buffer */
  624. cx231xx_swab((u16 *) p_buffer, (u16 *) startwrite, (u16) lencopy);
  625. return 0;
  626. }
  627. void cx231xx_swab(u16 *from, u16 *to, u16 len)
  628. {
  629. u16 i;
  630. if (len <= 0)
  631. return;
  632. for (i = 0; i < len / 2; i++)
  633. to[i] = (from[i] << 8) | (from[i] >> 8);
  634. }
  635. u8 cx231xx_is_buffer_done(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q)
  636. {
  637. u8 buffer_complete = 0;
  638. /* Dual field stream */
  639. buffer_complete = ((dma_q->current_field == 2) &&
  640. (dma_q->lines_completed >= dma_q->lines_per_field) &&
  641. dma_q->field1_done);
  642. return buffer_complete;
  643. }
  644. /* ------------------------------------------------------------------
  645. Videobuf operations
  646. ------------------------------------------------------------------*/
  647. static int
  648. buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
  649. {
  650. struct cx231xx_fh *fh = vq->priv_data;
  651. struct cx231xx *dev = fh->dev;
  652. *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)>>3;
  653. if (0 == *count)
  654. *count = CX231XX_DEF_BUF;
  655. if (*count < CX231XX_MIN_BUF)
  656. *count = CX231XX_MIN_BUF;
  657. return 0;
  658. }
  659. /* This is called *without* dev->slock held; please keep it that way */
  660. static void free_buffer(struct videobuf_queue *vq, struct cx231xx_buffer *buf)
  661. {
  662. struct cx231xx_fh *fh = vq->priv_data;
  663. struct cx231xx *dev = fh->dev;
  664. unsigned long flags = 0;
  665. if (in_interrupt())
  666. BUG();
  667. /* We used to wait for the buffer to finish here, but this didn't work
  668. because, as we were keeping the state as VIDEOBUF_QUEUED,
  669. videobuf_queue_cancel marked it as finished for us.
  670. (Also, it could wedge forever if the hardware was misconfigured.)
  671. This should be safe; by the time we get here, the buffer isn't
  672. queued anymore. If we ever start marking the buffers as
  673. VIDEOBUF_ACTIVE, it won't be, though.
  674. */
  675. spin_lock_irqsave(&dev->video_mode.slock, flags);
  676. if (dev->USE_ISO) {
  677. if (dev->video_mode.isoc_ctl.buf == buf)
  678. dev->video_mode.isoc_ctl.buf = NULL;
  679. } else {
  680. if (dev->video_mode.bulk_ctl.buf == buf)
  681. dev->video_mode.bulk_ctl.buf = NULL;
  682. }
  683. spin_unlock_irqrestore(&dev->video_mode.slock, flags);
  684. videobuf_vmalloc_free(&buf->vb);
  685. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  686. }
  687. static int
  688. buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
  689. enum v4l2_field field)
  690. {
  691. struct cx231xx_fh *fh = vq->priv_data;
  692. struct cx231xx_buffer *buf =
  693. container_of(vb, struct cx231xx_buffer, vb);
  694. struct cx231xx *dev = fh->dev;
  695. int rc = 0, urb_init = 0;
  696. /* The only currently supported format is 16 bits/pixel */
  697. buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
  698. + 7) >> 3;
  699. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  700. return -EINVAL;
  701. buf->vb.width = dev->width;
  702. buf->vb.height = dev->height;
  703. buf->vb.field = field;
  704. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  705. rc = videobuf_iolock(vq, &buf->vb, NULL);
  706. if (rc < 0)
  707. goto fail;
  708. }
  709. if (dev->USE_ISO) {
  710. if (!dev->video_mode.isoc_ctl.num_bufs)
  711. urb_init = 1;
  712. } else {
  713. if (!dev->video_mode.bulk_ctl.num_bufs)
  714. urb_init = 1;
  715. }
  716. /*cx231xx_info("urb_init=%d dev->video_mode.max_pkt_size=%d\n",
  717. urb_init, dev->video_mode.max_pkt_size);*/
  718. if (urb_init) {
  719. dev->mode_tv = 0;
  720. if (dev->USE_ISO)
  721. rc = cx231xx_init_isoc(dev, CX231XX_NUM_PACKETS,
  722. CX231XX_NUM_BUFS,
  723. dev->video_mode.max_pkt_size,
  724. cx231xx_isoc_copy);
  725. else
  726. rc = cx231xx_init_bulk(dev, CX231XX_NUM_PACKETS,
  727. CX231XX_NUM_BUFS,
  728. dev->video_mode.max_pkt_size,
  729. cx231xx_bulk_copy);
  730. if (rc < 0)
  731. goto fail;
  732. }
  733. buf->vb.state = VIDEOBUF_PREPARED;
  734. return 0;
  735. fail:
  736. free_buffer(vq, buf);
  737. return rc;
  738. }
  739. static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  740. {
  741. struct cx231xx_buffer *buf =
  742. container_of(vb, struct cx231xx_buffer, vb);
  743. struct cx231xx_fh *fh = vq->priv_data;
  744. struct cx231xx *dev = fh->dev;
  745. struct cx231xx_dmaqueue *vidq = &dev->video_mode.vidq;
  746. buf->vb.state = VIDEOBUF_QUEUED;
  747. list_add_tail(&buf->vb.queue, &vidq->active);
  748. }
  749. static void buffer_release(struct videobuf_queue *vq,
  750. struct videobuf_buffer *vb)
  751. {
  752. struct cx231xx_buffer *buf =
  753. container_of(vb, struct cx231xx_buffer, vb);
  754. struct cx231xx_fh *fh = vq->priv_data;
  755. struct cx231xx *dev = (struct cx231xx *)fh->dev;
  756. cx231xx_isocdbg("cx231xx: called buffer_release\n");
  757. free_buffer(vq, buf);
  758. }
  759. static struct videobuf_queue_ops cx231xx_video_qops = {
  760. .buf_setup = buffer_setup,
  761. .buf_prepare = buffer_prepare,
  762. .buf_queue = buffer_queue,
  763. .buf_release = buffer_release,
  764. };
  765. /********************* v4l2 interface **************************************/
  766. void video_mux(struct cx231xx *dev, int index)
  767. {
  768. dev->video_input = index;
  769. dev->ctl_ainput = INPUT(index)->amux;
  770. cx231xx_set_video_input_mux(dev, index);
  771. cx25840_call(dev, video, s_routing, INPUT(index)->vmux, 0, 0);
  772. cx231xx_set_audio_input(dev, dev->ctl_ainput);
  773. cx231xx_info("video_mux : %d\n", index);
  774. /* do mode control overrides if required */
  775. cx231xx_do_mode_ctrl_overrides(dev);
  776. }
  777. /* Usage lock check functions */
  778. static int res_get(struct cx231xx_fh *fh)
  779. {
  780. struct cx231xx *dev = fh->dev;
  781. int rc = 0;
  782. /* This instance already has stream_on */
  783. if (fh->stream_on)
  784. return rc;
  785. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  786. if (dev->stream_on)
  787. return -EBUSY;
  788. dev->stream_on = 1;
  789. } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
  790. if (dev->vbi_stream_on)
  791. return -EBUSY;
  792. dev->vbi_stream_on = 1;
  793. } else
  794. return -EINVAL;
  795. fh->stream_on = 1;
  796. return rc;
  797. }
  798. static int res_check(struct cx231xx_fh *fh)
  799. {
  800. return fh->stream_on;
  801. }
  802. static void res_free(struct cx231xx_fh *fh)
  803. {
  804. struct cx231xx *dev = fh->dev;
  805. fh->stream_on = 0;
  806. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  807. dev->stream_on = 0;
  808. if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
  809. dev->vbi_stream_on = 0;
  810. }
  811. static int check_dev(struct cx231xx *dev)
  812. {
  813. if (dev->state & DEV_DISCONNECTED) {
  814. cx231xx_errdev("v4l2 ioctl: device not present\n");
  815. return -ENODEV;
  816. }
  817. return 0;
  818. }
  819. /* ------------------------------------------------------------------
  820. IOCTL vidioc handling
  821. ------------------------------------------------------------------*/
  822. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  823. struct v4l2_format *f)
  824. {
  825. struct cx231xx_fh *fh = priv;
  826. struct cx231xx *dev = fh->dev;
  827. f->fmt.pix.width = dev->width;
  828. f->fmt.pix.height = dev->height;
  829. f->fmt.pix.pixelformat = dev->format->fourcc;
  830. f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
  831. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
  832. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  833. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  834. f->fmt.pix.priv = 0;
  835. return 0;
  836. }
  837. static struct cx231xx_fmt *format_by_fourcc(unsigned int fourcc)
  838. {
  839. unsigned int i;
  840. for (i = 0; i < ARRAY_SIZE(format); i++)
  841. if (format[i].fourcc == fourcc)
  842. return &format[i];
  843. return NULL;
  844. }
  845. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  846. struct v4l2_format *f)
  847. {
  848. struct cx231xx_fh *fh = priv;
  849. struct cx231xx *dev = fh->dev;
  850. unsigned int width = f->fmt.pix.width;
  851. unsigned int height = f->fmt.pix.height;
  852. unsigned int maxw = norm_maxw(dev);
  853. unsigned int maxh = norm_maxh(dev);
  854. struct cx231xx_fmt *fmt;
  855. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  856. if (!fmt) {
  857. cx231xx_videodbg("Fourcc format (%08x) invalid.\n",
  858. f->fmt.pix.pixelformat);
  859. return -EINVAL;
  860. }
  861. /* width must even because of the YUYV format
  862. height must be even because of interlacing */
  863. v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh, 1, 0);
  864. f->fmt.pix.width = width;
  865. f->fmt.pix.height = height;
  866. f->fmt.pix.pixelformat = fmt->fourcc;
  867. f->fmt.pix.bytesperline = (width * fmt->depth + 7) >> 3;
  868. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
  869. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  870. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  871. f->fmt.pix.priv = 0;
  872. return 0;
  873. }
  874. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  875. struct v4l2_format *f)
  876. {
  877. struct cx231xx_fh *fh = priv;
  878. struct cx231xx *dev = fh->dev;
  879. int rc;
  880. struct cx231xx_fmt *fmt;
  881. struct v4l2_mbus_framefmt mbus_fmt;
  882. rc = check_dev(dev);
  883. if (rc < 0)
  884. return rc;
  885. vidioc_try_fmt_vid_cap(file, priv, f);
  886. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  887. if (!fmt)
  888. return -EINVAL;
  889. if (videobuf_queue_is_busy(&fh->vb_vidq)) {
  890. cx231xx_errdev("%s queue busy\n", __func__);
  891. return -EBUSY;
  892. }
  893. if (dev->stream_on && !fh->stream_on) {
  894. cx231xx_errdev("%s device in use by another fh\n", __func__);
  895. return -EBUSY;
  896. }
  897. /* set new image size */
  898. dev->width = f->fmt.pix.width;
  899. dev->height = f->fmt.pix.height;
  900. dev->format = fmt;
  901. v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
  902. call_all(dev, video, s_mbus_fmt, &mbus_fmt);
  903. v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
  904. return rc;
  905. }
  906. static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
  907. {
  908. struct cx231xx_fh *fh = priv;
  909. struct cx231xx *dev = fh->dev;
  910. *id = dev->norm;
  911. return 0;
  912. }
  913. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
  914. {
  915. struct cx231xx_fh *fh = priv;
  916. struct cx231xx *dev = fh->dev;
  917. struct v4l2_mbus_framefmt mbus_fmt;
  918. struct v4l2_format f;
  919. int rc;
  920. rc = check_dev(dev);
  921. if (rc < 0)
  922. return rc;
  923. cx231xx_info("vidioc_s_std : 0x%x\n", (unsigned int)*norm);
  924. dev->norm = *norm;
  925. /* Adjusts width/height, if needed */
  926. f.fmt.pix.width = dev->width;
  927. f.fmt.pix.height = dev->height;
  928. vidioc_try_fmt_vid_cap(file, priv, &f);
  929. call_all(dev, core, s_std, dev->norm);
  930. /* We need to reset basic properties in the decoder related to
  931. resolution (since a standard change effects things like the number
  932. of lines in VACT, etc) */
  933. v4l2_fill_mbus_format(&mbus_fmt, &f.fmt.pix, V4L2_MBUS_FMT_FIXED);
  934. call_all(dev, video, s_mbus_fmt, &mbus_fmt);
  935. v4l2_fill_pix_format(&f.fmt.pix, &mbus_fmt);
  936. /* set new image size */
  937. dev->width = f.fmt.pix.width;
  938. dev->height = f.fmt.pix.height;
  939. /* do mode control overrides */
  940. cx231xx_do_mode_ctrl_overrides(dev);
  941. return 0;
  942. }
  943. static const char *iname[] = {
  944. [CX231XX_VMUX_COMPOSITE1] = "Composite1",
  945. [CX231XX_VMUX_SVIDEO] = "S-Video",
  946. [CX231XX_VMUX_TELEVISION] = "Television",
  947. [CX231XX_VMUX_CABLE] = "Cable TV",
  948. [CX231XX_VMUX_DVB] = "DVB",
  949. [CX231XX_VMUX_DEBUG] = "for debug only",
  950. };
  951. static int vidioc_enum_input(struct file *file, void *priv,
  952. struct v4l2_input *i)
  953. {
  954. struct cx231xx_fh *fh = priv;
  955. struct cx231xx *dev = fh->dev;
  956. u32 gen_stat;
  957. unsigned int ret, n;
  958. n = i->index;
  959. if (n >= MAX_CX231XX_INPUT)
  960. return -EINVAL;
  961. if (0 == INPUT(n)->type)
  962. return -EINVAL;
  963. i->index = n;
  964. i->type = V4L2_INPUT_TYPE_CAMERA;
  965. strcpy(i->name, iname[INPUT(n)->type]);
  966. if ((CX231XX_VMUX_TELEVISION == INPUT(n)->type) ||
  967. (CX231XX_VMUX_CABLE == INPUT(n)->type))
  968. i->type = V4L2_INPUT_TYPE_TUNER;
  969. i->std = dev->vdev->tvnorms;
  970. /* If they are asking about the active input, read signal status */
  971. if (n == dev->video_input) {
  972. ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
  973. GEN_STAT, 2, &gen_stat, 4);
  974. if (ret > 0) {
  975. if ((gen_stat & FLD_VPRES) == 0x00)
  976. i->status |= V4L2_IN_ST_NO_SIGNAL;
  977. if ((gen_stat & FLD_HLOCK) == 0x00)
  978. i->status |= V4L2_IN_ST_NO_H_LOCK;
  979. }
  980. }
  981. return 0;
  982. }
  983. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  984. {
  985. struct cx231xx_fh *fh = priv;
  986. struct cx231xx *dev = fh->dev;
  987. *i = dev->video_input;
  988. return 0;
  989. }
  990. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  991. {
  992. struct cx231xx_fh *fh = priv;
  993. struct cx231xx *dev = fh->dev;
  994. int rc;
  995. dev->mode_tv = 0;
  996. rc = check_dev(dev);
  997. if (rc < 0)
  998. return rc;
  999. if (i >= MAX_CX231XX_INPUT)
  1000. return -EINVAL;
  1001. if (0 == INPUT(i)->type)
  1002. return -EINVAL;
  1003. video_mux(dev, i);
  1004. if (INPUT(i)->type == CX231XX_VMUX_TELEVISION ||
  1005. INPUT(i)->type == CX231XX_VMUX_CABLE) {
  1006. /* There's a tuner, so reset the standard and put it on the
  1007. last known frequency (since it was probably powered down
  1008. until now */
  1009. call_all(dev, core, s_std, dev->norm);
  1010. }
  1011. return 0;
  1012. }
  1013. static int vidioc_queryctrl(struct file *file, void *priv,
  1014. struct v4l2_queryctrl *qc)
  1015. {
  1016. struct cx231xx_fh *fh = priv;
  1017. struct cx231xx *dev = fh->dev;
  1018. int id = qc->id;
  1019. int i;
  1020. int rc;
  1021. rc = check_dev(dev);
  1022. if (rc < 0)
  1023. return rc;
  1024. qc->id = v4l2_ctrl_next(ctrl_classes, qc->id);
  1025. if (unlikely(qc->id == 0))
  1026. return -EINVAL;
  1027. memset(qc, 0, sizeof(*qc));
  1028. qc->id = id;
  1029. if (qc->id < V4L2_CID_BASE || qc->id >= V4L2_CID_LASTP1)
  1030. return -EINVAL;
  1031. for (i = 0; i < CX231XX_CTLS; i++)
  1032. if (cx231xx_ctls[i].v.id == qc->id)
  1033. break;
  1034. if (i == CX231XX_CTLS) {
  1035. *qc = no_ctl;
  1036. return 0;
  1037. }
  1038. *qc = cx231xx_ctls[i].v;
  1039. call_all(dev, core, queryctrl, qc);
  1040. if (qc->type)
  1041. return 0;
  1042. else
  1043. return -EINVAL;
  1044. }
  1045. static int vidioc_g_ctrl(struct file *file, void *priv,
  1046. struct v4l2_control *ctrl)
  1047. {
  1048. struct cx231xx_fh *fh = priv;
  1049. struct cx231xx *dev = fh->dev;
  1050. int rc;
  1051. rc = check_dev(dev);
  1052. if (rc < 0)
  1053. return rc;
  1054. call_all(dev, core, g_ctrl, ctrl);
  1055. return rc;
  1056. }
  1057. static int vidioc_s_ctrl(struct file *file, void *priv,
  1058. struct v4l2_control *ctrl)
  1059. {
  1060. struct cx231xx_fh *fh = priv;
  1061. struct cx231xx *dev = fh->dev;
  1062. int rc;
  1063. rc = check_dev(dev);
  1064. if (rc < 0)
  1065. return rc;
  1066. call_all(dev, core, s_ctrl, ctrl);
  1067. return rc;
  1068. }
  1069. static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
  1070. {
  1071. struct cx231xx_fh *fh = priv;
  1072. struct cx231xx *dev = fh->dev;
  1073. int rc;
  1074. rc = check_dev(dev);
  1075. if (rc < 0)
  1076. return rc;
  1077. if (0 != t->index)
  1078. return -EINVAL;
  1079. strcpy(t->name, "Tuner");
  1080. t->type = V4L2_TUNER_ANALOG_TV;
  1081. t->capability = V4L2_TUNER_CAP_NORM;
  1082. t->rangehigh = 0xffffffffUL;
  1083. t->signal = 0xffff; /* LOCKED */
  1084. call_all(dev, tuner, g_tuner, t);
  1085. return 0;
  1086. }
  1087. static int vidioc_s_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
  1088. {
  1089. struct cx231xx_fh *fh = priv;
  1090. struct cx231xx *dev = fh->dev;
  1091. int rc;
  1092. rc = check_dev(dev);
  1093. if (rc < 0)
  1094. return rc;
  1095. if (0 != t->index)
  1096. return -EINVAL;
  1097. #if 0
  1098. call_all(dev, tuner, s_tuner, t);
  1099. #endif
  1100. return 0;
  1101. }
  1102. static int vidioc_g_frequency(struct file *file, void *priv,
  1103. struct v4l2_frequency *f)
  1104. {
  1105. struct cx231xx_fh *fh = priv;
  1106. struct cx231xx *dev = fh->dev;
  1107. if (f->tuner)
  1108. return -EINVAL;
  1109. f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1110. f->frequency = dev->ctl_freq;
  1111. call_all(dev, tuner, g_frequency, f);
  1112. return 0;
  1113. }
  1114. static int vidioc_s_frequency(struct file *file, void *priv,
  1115. struct v4l2_frequency *f)
  1116. {
  1117. struct cx231xx_fh *fh = priv;
  1118. struct cx231xx *dev = fh->dev;
  1119. int rc;
  1120. u32 if_frequency = 5400000;
  1121. cx231xx_info("Enter vidioc_s_frequency()f->frequency=%d;f->type=%d\n",
  1122. f->frequency, f->type);
  1123. /*cx231xx_info("f->type: 1-radio 2-analogTV 3-digitalTV\n");*/
  1124. rc = check_dev(dev);
  1125. if (rc < 0)
  1126. return rc;
  1127. if (0 != f->tuner)
  1128. return -EINVAL;
  1129. if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
  1130. return -EINVAL;
  1131. if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
  1132. return -EINVAL;
  1133. /* set pre channel change settings in DIF first */
  1134. rc = cx231xx_tuner_pre_channel_change(dev);
  1135. dev->ctl_freq = f->frequency;
  1136. call_all(dev, tuner, s_frequency, f);
  1137. /* set post channel change settings in DIF first */
  1138. rc = cx231xx_tuner_post_channel_change(dev);
  1139. if (dev->tuner_type == TUNER_NXP_TDA18271) {
  1140. if (dev->norm & (V4L2_STD_MN | V4L2_STD_NTSC_443))
  1141. if_frequency = 5400000; /*5.4MHz */
  1142. else if (dev->norm & V4L2_STD_B)
  1143. if_frequency = 6000000; /*6.0MHz */
  1144. else if (dev->norm & (V4L2_STD_PAL_DK | V4L2_STD_SECAM_DK))
  1145. if_frequency = 6900000; /*6.9MHz */
  1146. else if (dev->norm & V4L2_STD_GH)
  1147. if_frequency = 7100000; /*7.1MHz */
  1148. else if (dev->norm & V4L2_STD_PAL_I)
  1149. if_frequency = 7250000; /*7.25MHz */
  1150. else if (dev->norm & V4L2_STD_SECAM_L)
  1151. if_frequency = 6900000; /*6.9MHz */
  1152. else if (dev->norm & V4L2_STD_SECAM_LC)
  1153. if_frequency = 1250000; /*1.25MHz */
  1154. cx231xx_info("if_frequency is set to %d\n", if_frequency);
  1155. cx231xx_set_Colibri_For_LowIF(dev, if_frequency, 1, 1);
  1156. update_HH_register_after_set_DIF(dev);
  1157. }
  1158. cx231xx_info("Set New FREQUENCY to %d\n", f->frequency);
  1159. return rc;
  1160. }
  1161. static int vidioc_g_chip_ident(struct file *file, void *fh,
  1162. struct v4l2_dbg_chip_ident *chip)
  1163. {
  1164. chip->ident = V4L2_IDENT_NONE;
  1165. chip->revision = 0;
  1166. if (chip->match.type == V4L2_CHIP_MATCH_HOST) {
  1167. if (v4l2_chip_match_host(&chip->match))
  1168. chip->ident = V4L2_IDENT_CX23100;
  1169. return 0;
  1170. }
  1171. return -EINVAL;
  1172. }
  1173. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1174. /*
  1175. -R, --list-registers=type=<host/i2cdrv/i2caddr>,
  1176. chip=<chip>[,min=<addr>,max=<addr>]
  1177. dump registers from <min> to <max> [VIDIOC_DBG_G_REGISTER]
  1178. -r, --set-register=type=<host/i2cdrv/i2caddr>,
  1179. chip=<chip>,reg=<addr>,val=<val>
  1180. set the register [VIDIOC_DBG_S_REGISTER]
  1181. if type == host, then <chip> is the hosts chip ID (default 0)
  1182. if type == i2cdrv (default), then <chip> is the I2C driver name or ID
  1183. if type == i2caddr, then <chip> is the 7-bit I2C address
  1184. */
  1185. static int vidioc_g_register(struct file *file, void *priv,
  1186. struct v4l2_dbg_register *reg)
  1187. {
  1188. struct cx231xx_fh *fh = priv;
  1189. struct cx231xx *dev = fh->dev;
  1190. int ret = 0;
  1191. u8 value[4] = { 0, 0, 0, 0 };
  1192. u32 data = 0;
  1193. switch (reg->match.type) {
  1194. case V4L2_CHIP_MATCH_HOST:
  1195. switch (reg->match.addr) {
  1196. case 0: /* Cx231xx - internal registers */
  1197. ret = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER,
  1198. (u16)reg->reg, value, 4);
  1199. reg->val = value[0] | value[1] << 8 |
  1200. value[2] << 16 | value[3] << 24;
  1201. break;
  1202. case 1: /* AFE - read byte */
  1203. ret = cx231xx_read_i2c_data(dev, AFE_DEVICE_ADDRESS,
  1204. (u16)reg->reg, 2, &data, 1);
  1205. reg->val = le32_to_cpu(data & 0xff);
  1206. break;
  1207. case 14: /* AFE - read dword */
  1208. ret = cx231xx_read_i2c_data(dev, AFE_DEVICE_ADDRESS,
  1209. (u16)reg->reg, 2, &data, 4);
  1210. reg->val = le32_to_cpu(data);
  1211. break;
  1212. case 2: /* Video Block - read byte */
  1213. ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
  1214. (u16)reg->reg, 2, &data, 1);
  1215. reg->val = le32_to_cpu(data & 0xff);
  1216. break;
  1217. case 24: /* Video Block - read dword */
  1218. ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
  1219. (u16)reg->reg, 2, &data, 4);
  1220. reg->val = le32_to_cpu(data);
  1221. break;
  1222. case 3: /* I2S block - read byte */
  1223. ret = cx231xx_read_i2c_data(dev,
  1224. I2S_BLK_DEVICE_ADDRESS,
  1225. (u16)reg->reg, 1,
  1226. &data, 1);
  1227. reg->val = le32_to_cpu(data & 0xff);
  1228. break;
  1229. case 34: /* I2S Block - read dword */
  1230. ret =
  1231. cx231xx_read_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
  1232. (u16)reg->reg, 1, &data, 4);
  1233. reg->val = le32_to_cpu(data);
  1234. break;
  1235. }
  1236. return ret < 0 ? ret : 0;
  1237. case V4L2_CHIP_MATCH_I2C_DRIVER:
  1238. call_all(dev, core, g_register, reg);
  1239. return 0;
  1240. case V4L2_CHIP_MATCH_I2C_ADDR:/*for register debug*/
  1241. switch (reg->match.addr) {
  1242. case 0: /* Cx231xx - internal registers */
  1243. ret = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER,
  1244. (u16)reg->reg, value, 4);
  1245. reg->val = value[0] | value[1] << 8 |
  1246. value[2] << 16 | value[3] << 24;
  1247. break;
  1248. case 0x600:/* AFE - read byte */
  1249. ret = cx231xx_read_i2c_master(dev, AFE_DEVICE_ADDRESS,
  1250. (u16)reg->reg, 2,
  1251. &data, 1 , 0);
  1252. reg->val = le32_to_cpu(data & 0xff);
  1253. break;
  1254. case 0x880:/* Video Block - read byte */
  1255. if (reg->reg < 0x0b) {
  1256. ret = cx231xx_read_i2c_master(dev,
  1257. VID_BLK_I2C_ADDRESS,
  1258. (u16)reg->reg, 2,
  1259. &data, 1 , 0);
  1260. reg->val = le32_to_cpu(data & 0xff);
  1261. } else {
  1262. ret = cx231xx_read_i2c_master(dev,
  1263. VID_BLK_I2C_ADDRESS,
  1264. (u16)reg->reg, 2,
  1265. &data, 4 , 0);
  1266. reg->val = le32_to_cpu(data);
  1267. }
  1268. break;
  1269. case 0x980:
  1270. ret = cx231xx_read_i2c_master(dev,
  1271. I2S_BLK_DEVICE_ADDRESS,
  1272. (u16)reg->reg, 1,
  1273. &data, 1 , 0);
  1274. reg->val = le32_to_cpu(data & 0xff);
  1275. break;
  1276. case 0x400:
  1277. ret =
  1278. cx231xx_read_i2c_master(dev, 0x40,
  1279. (u16)reg->reg, 1,
  1280. &data, 1 , 0);
  1281. reg->val = le32_to_cpu(data & 0xff);
  1282. break;
  1283. case 0xc01:
  1284. ret =
  1285. cx231xx_read_i2c_master(dev, 0xc0,
  1286. (u16)reg->reg, 2,
  1287. &data, 38, 1);
  1288. reg->val = le32_to_cpu(data);
  1289. break;
  1290. case 0x022:
  1291. ret =
  1292. cx231xx_read_i2c_master(dev, 0x02,
  1293. (u16)reg->reg, 1,
  1294. &data, 1, 2);
  1295. reg->val = le32_to_cpu(data & 0xff);
  1296. break;
  1297. case 0x322:
  1298. ret = cx231xx_read_i2c_master(dev,
  1299. 0x32,
  1300. (u16)reg->reg, 1,
  1301. &data, 4 , 2);
  1302. reg->val = le32_to_cpu(data);
  1303. break;
  1304. case 0x342:
  1305. ret = cx231xx_read_i2c_master(dev,
  1306. 0x34,
  1307. (u16)reg->reg, 1,
  1308. &data, 4 , 2);
  1309. reg->val = le32_to_cpu(data);
  1310. break;
  1311. default:
  1312. cx231xx_info("no match device address!!\n");
  1313. break;
  1314. }
  1315. return ret < 0 ? ret : 0;
  1316. /*return -EINVAL;*/
  1317. default:
  1318. if (!v4l2_chip_match_host(&reg->match))
  1319. return -EINVAL;
  1320. }
  1321. call_all(dev, core, g_register, reg);
  1322. return ret;
  1323. }
  1324. static int vidioc_s_register(struct file *file, void *priv,
  1325. struct v4l2_dbg_register *reg)
  1326. {
  1327. struct cx231xx_fh *fh = priv;
  1328. struct cx231xx *dev = fh->dev;
  1329. int ret = 0;
  1330. __le64 buf;
  1331. u32 value;
  1332. u8 data[4] = { 0, 0, 0, 0 };
  1333. buf = cpu_to_le64(reg->val);
  1334. switch (reg->match.type) {
  1335. case V4L2_CHIP_MATCH_HOST:
  1336. {
  1337. value = (u32) buf & 0xffffffff;
  1338. switch (reg->match.addr) {
  1339. case 0: /* cx231xx internal registers */
  1340. data[0] = (u8) value;
  1341. data[1] = (u8) (value >> 8);
  1342. data[2] = (u8) (value >> 16);
  1343. data[3] = (u8) (value >> 24);
  1344. ret = cx231xx_write_ctrl_reg(dev,
  1345. VRT_SET_REGISTER,
  1346. (u16)reg->reg, data,
  1347. 4);
  1348. break;
  1349. case 1: /* AFE - read byte */
  1350. ret = cx231xx_write_i2c_data(dev,
  1351. AFE_DEVICE_ADDRESS,
  1352. (u16)reg->reg, 2,
  1353. value, 1);
  1354. break;
  1355. case 14: /* AFE - read dword */
  1356. ret = cx231xx_write_i2c_data(dev,
  1357. AFE_DEVICE_ADDRESS,
  1358. (u16)reg->reg, 2,
  1359. value, 4);
  1360. break;
  1361. case 2: /* Video Block - read byte */
  1362. ret =
  1363. cx231xx_write_i2c_data(dev,
  1364. VID_BLK_I2C_ADDRESS,
  1365. (u16)reg->reg, 2,
  1366. value, 1);
  1367. break;
  1368. case 24: /* Video Block - read dword */
  1369. ret =
  1370. cx231xx_write_i2c_data(dev,
  1371. VID_BLK_I2C_ADDRESS,
  1372. (u16)reg->reg, 2,
  1373. value, 4);
  1374. break;
  1375. case 3: /* I2S block - read byte */
  1376. ret =
  1377. cx231xx_write_i2c_data(dev,
  1378. I2S_BLK_DEVICE_ADDRESS,
  1379. (u16)reg->reg, 1,
  1380. value, 1);
  1381. break;
  1382. case 34: /* I2S block - read dword */
  1383. ret =
  1384. cx231xx_write_i2c_data(dev,
  1385. I2S_BLK_DEVICE_ADDRESS,
  1386. (u16)reg->reg, 1,
  1387. value, 4);
  1388. break;
  1389. }
  1390. }
  1391. return ret < 0 ? ret : 0;
  1392. case V4L2_CHIP_MATCH_I2C_ADDR:
  1393. {
  1394. value = (u32) buf & 0xffffffff;
  1395. switch (reg->match.addr) {
  1396. case 0:/*cx231xx internal registers*/
  1397. data[0] = (u8) value;
  1398. data[1] = (u8) (value >> 8);
  1399. data[2] = (u8) (value >> 16);
  1400. data[3] = (u8) (value >> 24);
  1401. ret = cx231xx_write_ctrl_reg(dev,
  1402. VRT_SET_REGISTER,
  1403. (u16)reg->reg, data,
  1404. 4);
  1405. break;
  1406. case 0x600:/* AFE - read byte */
  1407. ret = cx231xx_write_i2c_master(dev,
  1408. AFE_DEVICE_ADDRESS,
  1409. (u16)reg->reg, 2,
  1410. value, 1 , 0);
  1411. break;
  1412. case 0x880:/* Video Block - read byte */
  1413. if (reg->reg < 0x0b)
  1414. cx231xx_write_i2c_master(dev,
  1415. VID_BLK_I2C_ADDRESS,
  1416. (u16)reg->reg, 2,
  1417. value, 1, 0);
  1418. else
  1419. cx231xx_write_i2c_master(dev,
  1420. VID_BLK_I2C_ADDRESS,
  1421. (u16)reg->reg, 2,
  1422. value, 4, 0);
  1423. break;
  1424. case 0x980:
  1425. ret =
  1426. cx231xx_write_i2c_master(dev,
  1427. I2S_BLK_DEVICE_ADDRESS,
  1428. (u16)reg->reg, 1,
  1429. value, 1, 0);
  1430. break;
  1431. case 0x400:
  1432. ret =
  1433. cx231xx_write_i2c_master(dev,
  1434. 0x40,
  1435. (u16)reg->reg, 1,
  1436. value, 1, 0);
  1437. break;
  1438. case 0xc01:
  1439. ret =
  1440. cx231xx_write_i2c_master(dev,
  1441. 0xc0,
  1442. (u16)reg->reg, 1,
  1443. value, 1, 1);
  1444. break;
  1445. case 0x022:
  1446. ret =
  1447. cx231xx_write_i2c_master(dev,
  1448. 0x02,
  1449. (u16)reg->reg, 1,
  1450. value, 1, 2);
  1451. break;
  1452. case 0x322:
  1453. ret =
  1454. cx231xx_write_i2c_master(dev,
  1455. 0x32,
  1456. (u16)reg->reg, 1,
  1457. value, 4, 2);
  1458. break;
  1459. case 0x342:
  1460. ret =
  1461. cx231xx_write_i2c_master(dev,
  1462. 0x34,
  1463. (u16)reg->reg, 1,
  1464. value, 4, 2);
  1465. break;
  1466. default:
  1467. cx231xx_info("no match device address, "
  1468. "the value is %x\n", reg->match.addr);
  1469. break;
  1470. }
  1471. }
  1472. default:
  1473. break;
  1474. }
  1475. call_all(dev, core, s_register, reg);
  1476. return ret;
  1477. }
  1478. #endif
  1479. static int vidioc_cropcap(struct file *file, void *priv,
  1480. struct v4l2_cropcap *cc)
  1481. {
  1482. struct cx231xx_fh *fh = priv;
  1483. struct cx231xx *dev = fh->dev;
  1484. if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1485. return -EINVAL;
  1486. cc->bounds.left = 0;
  1487. cc->bounds.top = 0;
  1488. cc->bounds.width = dev->width;
  1489. cc->bounds.height = dev->height;
  1490. cc->defrect = cc->bounds;
  1491. cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
  1492. cc->pixelaspect.denominator = 59;
  1493. return 0;
  1494. }
  1495. static int vidioc_streamon(struct file *file, void *priv,
  1496. enum v4l2_buf_type type)
  1497. {
  1498. struct cx231xx_fh *fh = priv;
  1499. struct cx231xx *dev = fh->dev;
  1500. int rc;
  1501. rc = check_dev(dev);
  1502. if (rc < 0)
  1503. return rc;
  1504. rc = res_get(fh);
  1505. if (likely(rc >= 0))
  1506. rc = videobuf_streamon(&fh->vb_vidq);
  1507. call_all(dev, video, s_stream, 1);
  1508. return rc;
  1509. }
  1510. static int vidioc_streamoff(struct file *file, void *priv,
  1511. enum v4l2_buf_type type)
  1512. {
  1513. struct cx231xx_fh *fh = priv;
  1514. struct cx231xx *dev = fh->dev;
  1515. int rc;
  1516. rc = check_dev(dev);
  1517. if (rc < 0)
  1518. return rc;
  1519. if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
  1520. (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE))
  1521. return -EINVAL;
  1522. if (type != fh->type)
  1523. return -EINVAL;
  1524. cx25840_call(dev, video, s_stream, 0);
  1525. videobuf_streamoff(&fh->vb_vidq);
  1526. res_free(fh);
  1527. return 0;
  1528. }
  1529. static int vidioc_querycap(struct file *file, void *priv,
  1530. struct v4l2_capability *cap)
  1531. {
  1532. struct video_device *vdev = video_devdata(file);
  1533. struct cx231xx_fh *fh = priv;
  1534. struct cx231xx *dev = fh->dev;
  1535. strlcpy(cap->driver, "cx231xx", sizeof(cap->driver));
  1536. strlcpy(cap->card, cx231xx_boards[dev->model].name, sizeof(cap->card));
  1537. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  1538. if (vdev->vfl_type == VFL_TYPE_RADIO)
  1539. cap->device_caps = V4L2_CAP_RADIO;
  1540. else {
  1541. cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  1542. if (vdev->vfl_type == VFL_TYPE_VBI)
  1543. cap->device_caps |= V4L2_CAP_VBI_CAPTURE;
  1544. else
  1545. cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
  1546. }
  1547. if (dev->tuner_type != TUNER_ABSENT)
  1548. cap->device_caps |= V4L2_CAP_TUNER;
  1549. cap->capabilities = cap->device_caps | V4L2_CAP_READWRITE |
  1550. V4L2_CAP_VBI_CAPTURE | V4L2_CAP_VIDEO_CAPTURE |
  1551. V4L2_CAP_STREAMING | V4L2_CAP_DEVICE_CAPS;
  1552. if (dev->radio_dev)
  1553. cap->capabilities |= V4L2_CAP_RADIO;
  1554. return 0;
  1555. }
  1556. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  1557. struct v4l2_fmtdesc *f)
  1558. {
  1559. if (unlikely(f->index >= ARRAY_SIZE(format)))
  1560. return -EINVAL;
  1561. strlcpy(f->description, format[f->index].name, sizeof(f->description));
  1562. f->pixelformat = format[f->index].fourcc;
  1563. return 0;
  1564. }
  1565. /* Sliced VBI ioctls */
  1566. static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
  1567. struct v4l2_format *f)
  1568. {
  1569. struct cx231xx_fh *fh = priv;
  1570. struct cx231xx *dev = fh->dev;
  1571. int rc;
  1572. rc = check_dev(dev);
  1573. if (rc < 0)
  1574. return rc;
  1575. f->fmt.sliced.service_set = 0;
  1576. call_all(dev, vbi, g_sliced_fmt, &f->fmt.sliced);
  1577. if (f->fmt.sliced.service_set == 0)
  1578. rc = -EINVAL;
  1579. return rc;
  1580. }
  1581. static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
  1582. struct v4l2_format *f)
  1583. {
  1584. struct cx231xx_fh *fh = priv;
  1585. struct cx231xx *dev = fh->dev;
  1586. int rc;
  1587. rc = check_dev(dev);
  1588. if (rc < 0)
  1589. return rc;
  1590. call_all(dev, vbi, g_sliced_fmt, &f->fmt.sliced);
  1591. if (f->fmt.sliced.service_set == 0)
  1592. return -EINVAL;
  1593. return 0;
  1594. }
  1595. /* RAW VBI ioctls */
  1596. static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
  1597. struct v4l2_format *f)
  1598. {
  1599. struct cx231xx_fh *fh = priv;
  1600. struct cx231xx *dev = fh->dev;
  1601. f->fmt.vbi.sampling_rate = 6750000 * 4;
  1602. f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
  1603. f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  1604. f->fmt.vbi.offset = 0;
  1605. f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
  1606. PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
  1607. f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
  1608. PAL_VBI_LINES : NTSC_VBI_LINES;
  1609. f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
  1610. PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
  1611. f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
  1612. return 0;
  1613. }
  1614. static int vidioc_try_fmt_vbi_cap(struct file *file, void *priv,
  1615. struct v4l2_format *f)
  1616. {
  1617. struct cx231xx_fh *fh = priv;
  1618. struct cx231xx *dev = fh->dev;
  1619. if (dev->vbi_stream_on && !fh->stream_on) {
  1620. cx231xx_errdev("%s device in use by another fh\n", __func__);
  1621. return -EBUSY;
  1622. }
  1623. f->type = V4L2_BUF_TYPE_VBI_CAPTURE;
  1624. f->fmt.vbi.sampling_rate = 6750000 * 4;
  1625. f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
  1626. f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  1627. f->fmt.vbi.offset = 0;
  1628. f->fmt.vbi.flags = 0;
  1629. f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
  1630. PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
  1631. f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
  1632. PAL_VBI_LINES : NTSC_VBI_LINES;
  1633. f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
  1634. PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
  1635. f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
  1636. return 0;
  1637. }
  1638. static int vidioc_reqbufs(struct file *file, void *priv,
  1639. struct v4l2_requestbuffers *rb)
  1640. {
  1641. struct cx231xx_fh *fh = priv;
  1642. struct cx231xx *dev = fh->dev;
  1643. int rc;
  1644. rc = check_dev(dev);
  1645. if (rc < 0)
  1646. return rc;
  1647. return videobuf_reqbufs(&fh->vb_vidq, rb);
  1648. }
  1649. static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1650. {
  1651. struct cx231xx_fh *fh = priv;
  1652. struct cx231xx *dev = fh->dev;
  1653. int rc;
  1654. rc = check_dev(dev);
  1655. if (rc < 0)
  1656. return rc;
  1657. return videobuf_querybuf(&fh->vb_vidq, b);
  1658. }
  1659. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1660. {
  1661. struct cx231xx_fh *fh = priv;
  1662. struct cx231xx *dev = fh->dev;
  1663. int rc;
  1664. rc = check_dev(dev);
  1665. if (rc < 0)
  1666. return rc;
  1667. return videobuf_qbuf(&fh->vb_vidq, b);
  1668. }
  1669. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1670. {
  1671. struct cx231xx_fh *fh = priv;
  1672. struct cx231xx *dev = fh->dev;
  1673. int rc;
  1674. rc = check_dev(dev);
  1675. if (rc < 0)
  1676. return rc;
  1677. return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK);
  1678. }
  1679. /* ----------------------------------------------------------- */
  1680. /* RADIO ESPECIFIC IOCTLS */
  1681. /* ----------------------------------------------------------- */
  1682. static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
  1683. {
  1684. struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
  1685. if (t->index)
  1686. return -EINVAL;
  1687. strcpy(t->name, "Radio");
  1688. call_all(dev, tuner, g_tuner, t);
  1689. return 0;
  1690. }
  1691. static int radio_s_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
  1692. {
  1693. struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
  1694. if (0 != t->index)
  1695. return -EINVAL;
  1696. call_all(dev, tuner, s_tuner, t);
  1697. return 0;
  1698. }
  1699. static int radio_queryctrl(struct file *file, void *priv,
  1700. struct v4l2_queryctrl *c)
  1701. {
  1702. int i;
  1703. if (c->id < V4L2_CID_BASE || c->id >= V4L2_CID_LASTP1)
  1704. return -EINVAL;
  1705. if (c->id == V4L2_CID_AUDIO_MUTE) {
  1706. for (i = 0; i < CX231XX_CTLS; i++) {
  1707. if (cx231xx_ctls[i].v.id == c->id)
  1708. break;
  1709. }
  1710. if (i == CX231XX_CTLS)
  1711. return -EINVAL;
  1712. *c = cx231xx_ctls[i].v;
  1713. } else
  1714. *c = no_ctl;
  1715. return 0;
  1716. }
  1717. /*
  1718. * cx231xx_v4l2_open()
  1719. * inits the device and starts isoc transfer
  1720. */
  1721. static int cx231xx_v4l2_open(struct file *filp)
  1722. {
  1723. int errCode = 0, radio = 0;
  1724. struct video_device *vdev = video_devdata(filp);
  1725. struct cx231xx *dev = video_drvdata(filp);
  1726. struct cx231xx_fh *fh;
  1727. enum v4l2_buf_type fh_type = 0;
  1728. switch (vdev->vfl_type) {
  1729. case VFL_TYPE_GRABBER:
  1730. fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1731. break;
  1732. case VFL_TYPE_VBI:
  1733. fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
  1734. break;
  1735. case VFL_TYPE_RADIO:
  1736. radio = 1;
  1737. break;
  1738. }
  1739. cx231xx_videodbg("open dev=%s type=%s users=%d\n",
  1740. video_device_node_name(vdev), v4l2_type_names[fh_type],
  1741. dev->users);
  1742. #if 0
  1743. errCode = cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
  1744. if (errCode < 0) {
  1745. cx231xx_errdev
  1746. ("Device locked on digital mode. Can't open analog\n");
  1747. return -EBUSY;
  1748. }
  1749. #endif
  1750. fh = kzalloc(sizeof(struct cx231xx_fh), GFP_KERNEL);
  1751. if (!fh) {
  1752. cx231xx_errdev("cx231xx-video.c: Out of memory?!\n");
  1753. return -ENOMEM;
  1754. }
  1755. if (mutex_lock_interruptible(&dev->lock)) {
  1756. kfree(fh);
  1757. return -ERESTARTSYS;
  1758. }
  1759. fh->dev = dev;
  1760. fh->radio = radio;
  1761. fh->type = fh_type;
  1762. filp->private_data = fh;
  1763. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
  1764. dev->width = norm_maxw(dev);
  1765. dev->height = norm_maxh(dev);
  1766. /* Power up in Analog TV mode */
  1767. if (dev->board.external_av)
  1768. cx231xx_set_power_mode(dev,
  1769. POLARIS_AVMODE_ENXTERNAL_AV);
  1770. else
  1771. cx231xx_set_power_mode(dev, POLARIS_AVMODE_ANALOGT_TV);
  1772. #if 0
  1773. cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
  1774. #endif
  1775. /* set video alternate setting */
  1776. cx231xx_set_video_alternate(dev);
  1777. /* Needed, since GPIO might have disabled power of
  1778. some i2c device */
  1779. cx231xx_config_i2c(dev);
  1780. /* device needs to be initialized before isoc transfer */
  1781. dev->video_input = dev->video_input > 2 ? 2 : dev->video_input;
  1782. }
  1783. if (fh->radio) {
  1784. cx231xx_videodbg("video_open: setting radio device\n");
  1785. /* cx231xx_start_radio(dev); */
  1786. call_all(dev, tuner, s_radio);
  1787. }
  1788. dev->users++;
  1789. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1790. videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_video_qops,
  1791. NULL, &dev->video_mode.slock,
  1792. fh->type, V4L2_FIELD_INTERLACED,
  1793. sizeof(struct cx231xx_buffer),
  1794. fh, &dev->lock);
  1795. if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
  1796. /* Set the required alternate setting VBI interface works in
  1797. Bulk mode only */
  1798. cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
  1799. videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_vbi_qops,
  1800. NULL, &dev->vbi_mode.slock,
  1801. fh->type, V4L2_FIELD_SEQ_TB,
  1802. sizeof(struct cx231xx_buffer),
  1803. fh, &dev->lock);
  1804. }
  1805. mutex_unlock(&dev->lock);
  1806. return errCode;
  1807. }
  1808. /*
  1809. * cx231xx_realease_resources()
  1810. * unregisters the v4l2,i2c and usb devices
  1811. * called when the device gets disconected or at module unload
  1812. */
  1813. void cx231xx_release_analog_resources(struct cx231xx *dev)
  1814. {
  1815. /*FIXME: I2C IR should be disconnected */
  1816. if (dev->radio_dev) {
  1817. if (video_is_registered(dev->radio_dev))
  1818. video_unregister_device(dev->radio_dev);
  1819. else
  1820. video_device_release(dev->radio_dev);
  1821. dev->radio_dev = NULL;
  1822. }
  1823. if (dev->vbi_dev) {
  1824. cx231xx_info("V4L2 device %s deregistered\n",
  1825. video_device_node_name(dev->vbi_dev));
  1826. if (video_is_registered(dev->vbi_dev))
  1827. video_unregister_device(dev->vbi_dev);
  1828. else
  1829. video_device_release(dev->vbi_dev);
  1830. dev->vbi_dev = NULL;
  1831. }
  1832. if (dev->vdev) {
  1833. cx231xx_info("V4L2 device %s deregistered\n",
  1834. video_device_node_name(dev->vdev));
  1835. if (dev->board.has_417)
  1836. cx231xx_417_unregister(dev);
  1837. if (video_is_registered(dev->vdev))
  1838. video_unregister_device(dev->vdev);
  1839. else
  1840. video_device_release(dev->vdev);
  1841. dev->vdev = NULL;
  1842. }
  1843. }
  1844. /*
  1845. * cx231xx_close()
  1846. * stops streaming and deallocates all resources allocated by the v4l2
  1847. * calls and ioctls
  1848. */
  1849. static int cx231xx_close(struct file *filp)
  1850. {
  1851. struct cx231xx_fh *fh = filp->private_data;
  1852. struct cx231xx *dev = fh->dev;
  1853. cx231xx_videodbg("users=%d\n", dev->users);
  1854. cx231xx_videodbg("users=%d\n", dev->users);
  1855. if (res_check(fh))
  1856. res_free(fh);
  1857. /*
  1858. * To workaround error number=-71 on EP0 for VideoGrabber,
  1859. * need exclude following.
  1860. * FIXME: It is probably safe to remove most of these, as we're
  1861. * now avoiding the alternate setting for INDEX_VANC
  1862. */
  1863. if (!dev->board.no_alt_vanc)
  1864. if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
  1865. videobuf_stop(&fh->vb_vidq);
  1866. videobuf_mmap_free(&fh->vb_vidq);
  1867. /* the device is already disconnect,
  1868. free the remaining resources */
  1869. if (dev->state & DEV_DISCONNECTED) {
  1870. if (atomic_read(&dev->devlist_count) > 0) {
  1871. cx231xx_release_resources(dev);
  1872. fh->dev = NULL;
  1873. return 0;
  1874. }
  1875. return 0;
  1876. }
  1877. /* do this before setting alternate! */
  1878. cx231xx_uninit_vbi_isoc(dev);
  1879. /* set alternate 0 */
  1880. if (!dev->vbi_or_sliced_cc_mode)
  1881. cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
  1882. else
  1883. cx231xx_set_alt_setting(dev, INDEX_HANC, 0);
  1884. kfree(fh);
  1885. dev->users--;
  1886. wake_up_interruptible_nr(&dev->open, 1);
  1887. return 0;
  1888. }
  1889. dev->users--;
  1890. if (!dev->users) {
  1891. videobuf_stop(&fh->vb_vidq);
  1892. videobuf_mmap_free(&fh->vb_vidq);
  1893. /* the device is already disconnect,
  1894. free the remaining resources */
  1895. if (dev->state & DEV_DISCONNECTED) {
  1896. cx231xx_release_resources(dev);
  1897. fh->dev = NULL;
  1898. return 0;
  1899. }
  1900. /* Save some power by putting tuner to sleep */
  1901. call_all(dev, core, s_power, 0);
  1902. /* do this before setting alternate! */
  1903. if (dev->USE_ISO)
  1904. cx231xx_uninit_isoc(dev);
  1905. else
  1906. cx231xx_uninit_bulk(dev);
  1907. cx231xx_set_mode(dev, CX231XX_SUSPEND);
  1908. /* set alternate 0 */
  1909. cx231xx_set_alt_setting(dev, INDEX_VIDEO, 0);
  1910. }
  1911. kfree(fh);
  1912. wake_up_interruptible_nr(&dev->open, 1);
  1913. return 0;
  1914. }
  1915. static int cx231xx_v4l2_close(struct file *filp)
  1916. {
  1917. struct cx231xx_fh *fh = filp->private_data;
  1918. struct cx231xx *dev = fh->dev;
  1919. int rc;
  1920. mutex_lock(&dev->lock);
  1921. rc = cx231xx_close(filp);
  1922. mutex_unlock(&dev->lock);
  1923. return rc;
  1924. }
  1925. /*
  1926. * cx231xx_v4l2_read()
  1927. * will allocate buffers when called for the first time
  1928. */
  1929. static ssize_t
  1930. cx231xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
  1931. loff_t *pos)
  1932. {
  1933. struct cx231xx_fh *fh = filp->private_data;
  1934. struct cx231xx *dev = fh->dev;
  1935. int rc;
  1936. rc = check_dev(dev);
  1937. if (rc < 0)
  1938. return rc;
  1939. if ((fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
  1940. (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)) {
  1941. rc = res_get(fh);
  1942. if (unlikely(rc < 0))
  1943. return rc;
  1944. if (mutex_lock_interruptible(&dev->lock))
  1945. return -ERESTARTSYS;
  1946. rc = videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
  1947. filp->f_flags & O_NONBLOCK);
  1948. mutex_unlock(&dev->lock);
  1949. return rc;
  1950. }
  1951. return 0;
  1952. }
  1953. /*
  1954. * cx231xx_v4l2_poll()
  1955. * will allocate buffers when called for the first time
  1956. */
  1957. static unsigned int cx231xx_v4l2_poll(struct file *filp, poll_table *wait)
  1958. {
  1959. struct cx231xx_fh *fh = filp->private_data;
  1960. struct cx231xx *dev = fh->dev;
  1961. int rc;
  1962. rc = check_dev(dev);
  1963. if (rc < 0)
  1964. return rc;
  1965. rc = res_get(fh);
  1966. if (unlikely(rc < 0))
  1967. return POLLERR;
  1968. if ((V4L2_BUF_TYPE_VIDEO_CAPTURE == fh->type) ||
  1969. (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)) {
  1970. unsigned int res;
  1971. mutex_lock(&dev->lock);
  1972. res = videobuf_poll_stream(filp, &fh->vb_vidq, wait);
  1973. mutex_unlock(&dev->lock);
  1974. return res;
  1975. }
  1976. return POLLERR;
  1977. }
  1978. /*
  1979. * cx231xx_v4l2_mmap()
  1980. */
  1981. static int cx231xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
  1982. {
  1983. struct cx231xx_fh *fh = filp->private_data;
  1984. struct cx231xx *dev = fh->dev;
  1985. int rc;
  1986. rc = check_dev(dev);
  1987. if (rc < 0)
  1988. return rc;
  1989. rc = res_get(fh);
  1990. if (unlikely(rc < 0))
  1991. return rc;
  1992. if (mutex_lock_interruptible(&dev->lock))
  1993. return -ERESTARTSYS;
  1994. rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
  1995. mutex_unlock(&dev->lock);
  1996. cx231xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
  1997. (unsigned long)vma->vm_start,
  1998. (unsigned long)vma->vm_end -
  1999. (unsigned long)vma->vm_start, rc);
  2000. return rc;
  2001. }
  2002. static const struct v4l2_file_operations cx231xx_v4l_fops = {
  2003. .owner = THIS_MODULE,
  2004. .open = cx231xx_v4l2_open,
  2005. .release = cx231xx_v4l2_close,
  2006. .read = cx231xx_v4l2_read,
  2007. .poll = cx231xx_v4l2_poll,
  2008. .mmap = cx231xx_v4l2_mmap,
  2009. .unlocked_ioctl = video_ioctl2,
  2010. };
  2011. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  2012. .vidioc_querycap = vidioc_querycap,
  2013. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  2014. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  2015. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  2016. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  2017. .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
  2018. .vidioc_try_fmt_vbi_cap = vidioc_try_fmt_vbi_cap,
  2019. .vidioc_s_fmt_vbi_cap = vidioc_try_fmt_vbi_cap,
  2020. .vidioc_cropcap = vidioc_cropcap,
  2021. .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
  2022. .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
  2023. .vidioc_reqbufs = vidioc_reqbufs,
  2024. .vidioc_querybuf = vidioc_querybuf,
  2025. .vidioc_qbuf = vidioc_qbuf,
  2026. .vidioc_dqbuf = vidioc_dqbuf,
  2027. .vidioc_s_std = vidioc_s_std,
  2028. .vidioc_g_std = vidioc_g_std,
  2029. .vidioc_enum_input = vidioc_enum_input,
  2030. .vidioc_g_input = vidioc_g_input,
  2031. .vidioc_s_input = vidioc_s_input,
  2032. .vidioc_queryctrl = vidioc_queryctrl,
  2033. .vidioc_g_ctrl = vidioc_g_ctrl,
  2034. .vidioc_s_ctrl = vidioc_s_ctrl,
  2035. .vidioc_streamon = vidioc_streamon,
  2036. .vidioc_streamoff = vidioc_streamoff,
  2037. .vidioc_g_tuner = vidioc_g_tuner,
  2038. .vidioc_s_tuner = vidioc_s_tuner,
  2039. .vidioc_g_frequency = vidioc_g_frequency,
  2040. .vidioc_s_frequency = vidioc_s_frequency,
  2041. .vidioc_g_chip_ident = vidioc_g_chip_ident,
  2042. #ifdef CONFIG_VIDEO_ADV_DEBUG
  2043. .vidioc_g_register = vidioc_g_register,
  2044. .vidioc_s_register = vidioc_s_register,
  2045. #endif
  2046. };
  2047. static struct video_device cx231xx_vbi_template;
  2048. static const struct video_device cx231xx_video_template = {
  2049. .fops = &cx231xx_v4l_fops,
  2050. .release = video_device_release,
  2051. .ioctl_ops = &video_ioctl_ops,
  2052. .tvnorms = V4L2_STD_ALL,
  2053. .current_norm = V4L2_STD_PAL,
  2054. };
  2055. static const struct v4l2_file_operations radio_fops = {
  2056. .owner = THIS_MODULE,
  2057. .open = cx231xx_v4l2_open,
  2058. .release = cx231xx_v4l2_close,
  2059. .ioctl = video_ioctl2,
  2060. };
  2061. static const struct v4l2_ioctl_ops radio_ioctl_ops = {
  2062. .vidioc_querycap = vidioc_querycap,
  2063. .vidioc_g_tuner = radio_g_tuner,
  2064. .vidioc_s_tuner = radio_s_tuner,
  2065. .vidioc_queryctrl = radio_queryctrl,
  2066. .vidioc_g_ctrl = vidioc_g_ctrl,
  2067. .vidioc_s_ctrl = vidioc_s_ctrl,
  2068. .vidioc_g_frequency = vidioc_g_frequency,
  2069. .vidioc_s_frequency = vidioc_s_frequency,
  2070. .vidioc_g_chip_ident = vidioc_g_chip_ident,
  2071. #ifdef CONFIG_VIDEO_ADV_DEBUG
  2072. .vidioc_g_register = vidioc_g_register,
  2073. .vidioc_s_register = vidioc_s_register,
  2074. #endif
  2075. };
  2076. static struct video_device cx231xx_radio_template = {
  2077. .name = "cx231xx-radio",
  2078. .fops = &radio_fops,
  2079. .ioctl_ops = &radio_ioctl_ops,
  2080. };
  2081. /******************************** usb interface ******************************/
  2082. static struct video_device *cx231xx_vdev_init(struct cx231xx *dev,
  2083. const struct video_device
  2084. *template, const char *type_name)
  2085. {
  2086. struct video_device *vfd;
  2087. vfd = video_device_alloc();
  2088. if (NULL == vfd)
  2089. return NULL;
  2090. *vfd = *template;
  2091. vfd->v4l2_dev = &dev->v4l2_dev;
  2092. vfd->release = video_device_release;
  2093. vfd->debug = video_debug;
  2094. vfd->lock = &dev->lock;
  2095. snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name);
  2096. video_set_drvdata(vfd, dev);
  2097. if (dev->tuner_type == TUNER_ABSENT) {
  2098. v4l2_disable_ioctl(vfd, VIDIOC_G_FREQUENCY);
  2099. v4l2_disable_ioctl(vfd, VIDIOC_S_FREQUENCY);
  2100. v4l2_disable_ioctl(vfd, VIDIOC_G_TUNER);
  2101. v4l2_disable_ioctl(vfd, VIDIOC_S_TUNER);
  2102. }
  2103. return vfd;
  2104. }
  2105. int cx231xx_register_analog_devices(struct cx231xx *dev)
  2106. {
  2107. int ret;
  2108. cx231xx_info("%s: v4l2 driver version %s\n",
  2109. dev->name, CX231XX_VERSION);
  2110. /* set default norm */
  2111. /*dev->norm = cx231xx_video_template.current_norm; */
  2112. dev->width = norm_maxw(dev);
  2113. dev->height = norm_maxh(dev);
  2114. dev->interlaced = 0;
  2115. /* Analog specific initialization */
  2116. dev->format = &format[0];
  2117. /* Set the initial input */
  2118. video_mux(dev, dev->video_input);
  2119. /* Audio defaults */
  2120. dev->mute = 1;
  2121. dev->volume = 0x1f;
  2122. /* enable vbi capturing */
  2123. /* write code here... */
  2124. /* allocate and fill video video_device struct */
  2125. dev->vdev = cx231xx_vdev_init(dev, &cx231xx_video_template, "video");
  2126. if (!dev->vdev) {
  2127. cx231xx_errdev("cannot allocate video_device.\n");
  2128. return -ENODEV;
  2129. }
  2130. /* register v4l2 video video_device */
  2131. ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
  2132. video_nr[dev->devno]);
  2133. if (ret) {
  2134. cx231xx_errdev("unable to register video device (error=%i).\n",
  2135. ret);
  2136. return ret;
  2137. }
  2138. cx231xx_info("%s/0: registered device %s [v4l2]\n",
  2139. dev->name, video_device_node_name(dev->vdev));
  2140. /* Initialize VBI template */
  2141. cx231xx_vbi_template = cx231xx_video_template;
  2142. strcpy(cx231xx_vbi_template.name, "cx231xx-vbi");
  2143. /* Allocate and fill vbi video_device struct */
  2144. dev->vbi_dev = cx231xx_vdev_init(dev, &cx231xx_vbi_template, "vbi");
  2145. /* register v4l2 vbi video_device */
  2146. ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
  2147. vbi_nr[dev->devno]);
  2148. if (ret < 0) {
  2149. cx231xx_errdev("unable to register vbi device\n");
  2150. return ret;
  2151. }
  2152. cx231xx_info("%s/0: registered device %s\n",
  2153. dev->name, video_device_node_name(dev->vbi_dev));
  2154. if (cx231xx_boards[dev->model].radio.type == CX231XX_RADIO) {
  2155. dev->radio_dev = cx231xx_vdev_init(dev, &cx231xx_radio_template,
  2156. "radio");
  2157. if (!dev->radio_dev) {
  2158. cx231xx_errdev("cannot allocate video_device.\n");
  2159. return -ENODEV;
  2160. }
  2161. ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
  2162. radio_nr[dev->devno]);
  2163. if (ret < 0) {
  2164. cx231xx_errdev("can't register radio device\n");
  2165. return ret;
  2166. }
  2167. cx231xx_info("Registered radio device as %s\n",
  2168. video_device_node_name(dev->radio_dev));
  2169. }
  2170. cx231xx_info("V4L2 device registered as %s and %s\n",
  2171. video_device_node_name(dev->vdev),
  2172. video_device_node_name(dev->vbi_dev));
  2173. return 0;
  2174. }