cx231xx-video.c 64 KB

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