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/version.h>
  28. #include <linux/mm.h>
  29. #include <linux/mutex.h>
  30. #include <linux/slab.h>
  31. #include <media/v4l2-common.h>
  32. #include <media/v4l2-ioctl.h>
  33. #include <media/v4l2-chip-ident.h>
  34. #include <media/msp3400.h>
  35. #include <media/tuner.h>
  36. #include "dvb_frontend.h"
  37. #include "cx231xx.h"
  38. #include "cx231xx-vbi.h"
  39. #define CX231XX_VERSION_CODE KERNEL_VERSION(0, 0, 1)
  40. #define DRIVER_AUTHOR "Srinivasa Deevi <srinivasa.deevi@conexant.com>"
  41. #define DRIVER_DESC "Conexant cx231xx based USB video device driver"
  42. #define cx231xx_videodbg(fmt, arg...) do {\
  43. if (video_debug) \
  44. printk(KERN_INFO "%s %s :"fmt, \
  45. dev->name, __func__ , ##arg); } while (0)
  46. static unsigned int isoc_debug;
  47. module_param(isoc_debug, int, 0644);
  48. MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
  49. #define cx231xx_isocdbg(fmt, arg...) \
  50. do {\
  51. if (isoc_debug) { \
  52. printk(KERN_INFO "%s %s :"fmt, \
  53. dev->name, __func__ , ##arg); \
  54. } \
  55. } while (0)
  56. MODULE_AUTHOR(DRIVER_AUTHOR);
  57. MODULE_DESCRIPTION(DRIVER_DESC);
  58. MODULE_LICENSE("GPL");
  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) || (dev->state & DEV_MISCONFIGURED))
  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) || (dev->state & DEV_MISCONFIGURED))
  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. if (dev->state & DEV_MISCONFIGURED) {
  831. cx231xx_errdev("v4l2 ioctl: device is misconfigured; "
  832. "close and open it again\n");
  833. return -EIO;
  834. }
  835. return 0;
  836. }
  837. /* ------------------------------------------------------------------
  838. IOCTL vidioc handling
  839. ------------------------------------------------------------------*/
  840. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  841. struct v4l2_format *f)
  842. {
  843. struct cx231xx_fh *fh = priv;
  844. struct cx231xx *dev = fh->dev;
  845. f->fmt.pix.width = dev->width;
  846. f->fmt.pix.height = dev->height;
  847. f->fmt.pix.pixelformat = dev->format->fourcc;
  848. f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
  849. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
  850. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  851. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  852. return 0;
  853. }
  854. static struct cx231xx_fmt *format_by_fourcc(unsigned int fourcc)
  855. {
  856. unsigned int i;
  857. for (i = 0; i < ARRAY_SIZE(format); i++)
  858. if (format[i].fourcc == fourcc)
  859. return &format[i];
  860. return NULL;
  861. }
  862. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  863. struct v4l2_format *f)
  864. {
  865. struct cx231xx_fh *fh = priv;
  866. struct cx231xx *dev = fh->dev;
  867. unsigned int width = f->fmt.pix.width;
  868. unsigned int height = f->fmt.pix.height;
  869. unsigned int maxw = norm_maxw(dev);
  870. unsigned int maxh = norm_maxh(dev);
  871. struct cx231xx_fmt *fmt;
  872. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  873. if (!fmt) {
  874. cx231xx_videodbg("Fourcc format (%08x) invalid.\n",
  875. f->fmt.pix.pixelformat);
  876. return -EINVAL;
  877. }
  878. /* width must even because of the YUYV format
  879. height must be even because of interlacing */
  880. v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh, 1, 0);
  881. f->fmt.pix.width = width;
  882. f->fmt.pix.height = height;
  883. f->fmt.pix.pixelformat = fmt->fourcc;
  884. f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
  885. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
  886. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  887. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  888. return 0;
  889. }
  890. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  891. struct v4l2_format *f)
  892. {
  893. struct cx231xx_fh *fh = priv;
  894. struct cx231xx *dev = fh->dev;
  895. int rc;
  896. struct cx231xx_fmt *fmt;
  897. struct v4l2_mbus_framefmt mbus_fmt;
  898. rc = check_dev(dev);
  899. if (rc < 0)
  900. return rc;
  901. vidioc_try_fmt_vid_cap(file, priv, f);
  902. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  903. if (!fmt)
  904. return -EINVAL;
  905. if (videobuf_queue_is_busy(&fh->vb_vidq)) {
  906. cx231xx_errdev("%s queue busy\n", __func__);
  907. return -EBUSY;
  908. }
  909. if (dev->stream_on && !fh->stream_on) {
  910. cx231xx_errdev("%s device in use by another fh\n", __func__);
  911. return -EBUSY;
  912. }
  913. /* set new image size */
  914. dev->width = f->fmt.pix.width;
  915. dev->height = f->fmt.pix.height;
  916. dev->format = fmt;
  917. v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
  918. call_all(dev, video, s_mbus_fmt, &mbus_fmt);
  919. v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
  920. return rc;
  921. }
  922. static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
  923. {
  924. struct cx231xx_fh *fh = priv;
  925. struct cx231xx *dev = fh->dev;
  926. *id = dev->norm;
  927. return 0;
  928. }
  929. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
  930. {
  931. struct cx231xx_fh *fh = priv;
  932. struct cx231xx *dev = fh->dev;
  933. struct v4l2_mbus_framefmt mbus_fmt;
  934. struct v4l2_format f;
  935. int rc;
  936. rc = check_dev(dev);
  937. if (rc < 0)
  938. return rc;
  939. cx231xx_info("vidioc_s_std : 0x%x\n", (unsigned int)*norm);
  940. dev->norm = *norm;
  941. /* Adjusts width/height, if needed */
  942. f.fmt.pix.width = dev->width;
  943. f.fmt.pix.height = dev->height;
  944. vidioc_try_fmt_vid_cap(file, priv, &f);
  945. call_all(dev, core, s_std, dev->norm);
  946. /* We need to reset basic properties in the decoder related to
  947. resolution (since a standard change effects things like the number
  948. of lines in VACT, etc) */
  949. v4l2_fill_mbus_format(&mbus_fmt, &f.fmt.pix, V4L2_MBUS_FMT_FIXED);
  950. call_all(dev, video, s_mbus_fmt, &mbus_fmt);
  951. v4l2_fill_pix_format(&f.fmt.pix, &mbus_fmt);
  952. /* set new image size */
  953. dev->width = f.fmt.pix.width;
  954. dev->height = f.fmt.pix.height;
  955. /* do mode control overrides */
  956. cx231xx_do_mode_ctrl_overrides(dev);
  957. return 0;
  958. }
  959. static const char *iname[] = {
  960. [CX231XX_VMUX_COMPOSITE1] = "Composite1",
  961. [CX231XX_VMUX_SVIDEO] = "S-Video",
  962. [CX231XX_VMUX_TELEVISION] = "Television",
  963. [CX231XX_VMUX_CABLE] = "Cable TV",
  964. [CX231XX_VMUX_DVB] = "DVB",
  965. [CX231XX_VMUX_DEBUG] = "for debug only",
  966. };
  967. static int vidioc_enum_input(struct file *file, void *priv,
  968. struct v4l2_input *i)
  969. {
  970. struct cx231xx_fh *fh = priv;
  971. struct cx231xx *dev = fh->dev;
  972. unsigned int n;
  973. n = i->index;
  974. if (n >= MAX_CX231XX_INPUT)
  975. return -EINVAL;
  976. if (0 == INPUT(n)->type)
  977. return -EINVAL;
  978. i->index = n;
  979. i->type = V4L2_INPUT_TYPE_CAMERA;
  980. strcpy(i->name, iname[INPUT(n)->type]);
  981. if ((CX231XX_VMUX_TELEVISION == INPUT(n)->type) ||
  982. (CX231XX_VMUX_CABLE == INPUT(n)->type))
  983. i->type = V4L2_INPUT_TYPE_TUNER;
  984. i->std = dev->vdev->tvnorms;
  985. return 0;
  986. }
  987. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  988. {
  989. struct cx231xx_fh *fh = priv;
  990. struct cx231xx *dev = fh->dev;
  991. *i = dev->video_input;
  992. return 0;
  993. }
  994. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  995. {
  996. struct cx231xx_fh *fh = priv;
  997. struct cx231xx *dev = fh->dev;
  998. int rc;
  999. dev->mode_tv = 0;
  1000. rc = check_dev(dev);
  1001. if (rc < 0)
  1002. return rc;
  1003. if (i >= MAX_CX231XX_INPUT)
  1004. return -EINVAL;
  1005. if (0 == INPUT(i)->type)
  1006. return -EINVAL;
  1007. video_mux(dev, i);
  1008. if (INPUT(i)->type == CX231XX_VMUX_TELEVISION ||
  1009. INPUT(i)->type == CX231XX_VMUX_CABLE) {
  1010. /* There's a tuner, so reset the standard and put it on the
  1011. last known frequency (since it was probably powered down
  1012. until now */
  1013. call_all(dev, core, s_std, dev->norm);
  1014. }
  1015. return 0;
  1016. }
  1017. static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  1018. {
  1019. struct cx231xx_fh *fh = priv;
  1020. struct cx231xx *dev = fh->dev;
  1021. switch (a->index) {
  1022. case CX231XX_AMUX_VIDEO:
  1023. strcpy(a->name, "Television");
  1024. break;
  1025. case CX231XX_AMUX_LINE_IN:
  1026. strcpy(a->name, "Line In");
  1027. break;
  1028. default:
  1029. return -EINVAL;
  1030. }
  1031. a->index = dev->ctl_ainput;
  1032. a->capability = V4L2_AUDCAP_STEREO;
  1033. return 0;
  1034. }
  1035. static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
  1036. {
  1037. struct cx231xx_fh *fh = priv;
  1038. struct cx231xx *dev = fh->dev;
  1039. int status = 0;
  1040. /* Doesn't allow manual routing */
  1041. if (a->index != dev->ctl_ainput)
  1042. return -EINVAL;
  1043. dev->ctl_ainput = INPUT(a->index)->amux;
  1044. status = cx231xx_set_audio_input(dev, dev->ctl_ainput);
  1045. return status;
  1046. }
  1047. static int vidioc_queryctrl(struct file *file, void *priv,
  1048. struct v4l2_queryctrl *qc)
  1049. {
  1050. struct cx231xx_fh *fh = priv;
  1051. struct cx231xx *dev = fh->dev;
  1052. int id = qc->id;
  1053. int i;
  1054. int rc;
  1055. rc = check_dev(dev);
  1056. if (rc < 0)
  1057. return rc;
  1058. qc->id = v4l2_ctrl_next(ctrl_classes, qc->id);
  1059. if (unlikely(qc->id == 0))
  1060. return -EINVAL;
  1061. memset(qc, 0, sizeof(*qc));
  1062. qc->id = id;
  1063. if (qc->id < V4L2_CID_BASE || qc->id >= V4L2_CID_LASTP1)
  1064. return -EINVAL;
  1065. for (i = 0; i < CX231XX_CTLS; i++)
  1066. if (cx231xx_ctls[i].v.id == qc->id)
  1067. break;
  1068. if (i == CX231XX_CTLS) {
  1069. *qc = no_ctl;
  1070. return 0;
  1071. }
  1072. *qc = cx231xx_ctls[i].v;
  1073. call_all(dev, core, queryctrl, qc);
  1074. if (qc->type)
  1075. return 0;
  1076. else
  1077. return -EINVAL;
  1078. }
  1079. static int vidioc_g_ctrl(struct file *file, void *priv,
  1080. struct v4l2_control *ctrl)
  1081. {
  1082. struct cx231xx_fh *fh = priv;
  1083. struct cx231xx *dev = fh->dev;
  1084. int rc;
  1085. rc = check_dev(dev);
  1086. if (rc < 0)
  1087. return rc;
  1088. call_all(dev, core, g_ctrl, ctrl);
  1089. return rc;
  1090. }
  1091. static int vidioc_s_ctrl(struct file *file, void *priv,
  1092. struct v4l2_control *ctrl)
  1093. {
  1094. struct cx231xx_fh *fh = priv;
  1095. struct cx231xx *dev = fh->dev;
  1096. int rc;
  1097. rc = check_dev(dev);
  1098. if (rc < 0)
  1099. return rc;
  1100. call_all(dev, core, s_ctrl, ctrl);
  1101. return rc;
  1102. }
  1103. static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
  1104. {
  1105. struct cx231xx_fh *fh = priv;
  1106. struct cx231xx *dev = fh->dev;
  1107. int rc;
  1108. rc = check_dev(dev);
  1109. if (rc < 0)
  1110. return rc;
  1111. if (0 != t->index)
  1112. return -EINVAL;
  1113. strcpy(t->name, "Tuner");
  1114. t->type = V4L2_TUNER_ANALOG_TV;
  1115. t->capability = V4L2_TUNER_CAP_NORM;
  1116. t->rangehigh = 0xffffffffUL;
  1117. t->signal = 0xffff; /* LOCKED */
  1118. return 0;
  1119. }
  1120. static int vidioc_s_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
  1121. {
  1122. struct cx231xx_fh *fh = priv;
  1123. struct cx231xx *dev = fh->dev;
  1124. int rc;
  1125. rc = check_dev(dev);
  1126. if (rc < 0)
  1127. return rc;
  1128. if (0 != t->index)
  1129. return -EINVAL;
  1130. #if 0
  1131. call_all(dev, tuner, s_tuner, t);
  1132. #endif
  1133. return 0;
  1134. }
  1135. static int vidioc_g_frequency(struct file *file, void *priv,
  1136. struct v4l2_frequency *f)
  1137. {
  1138. struct cx231xx_fh *fh = priv;
  1139. struct cx231xx *dev = fh->dev;
  1140. f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1141. f->frequency = dev->ctl_freq;
  1142. call_all(dev, tuner, g_frequency, f);
  1143. return 0;
  1144. }
  1145. static int vidioc_s_frequency(struct file *file, void *priv,
  1146. struct v4l2_frequency *f)
  1147. {
  1148. struct cx231xx_fh *fh = priv;
  1149. struct cx231xx *dev = fh->dev;
  1150. int rc;
  1151. u32 if_frequency = 5400000;
  1152. cx231xx_info("Enter vidioc_s_frequency()f->frequency=%d;f->type=%d\n",
  1153. f->frequency, f->type);
  1154. /*cx231xx_info("f->type: 1-radio 2-analogTV 3-digitalTV\n");*/
  1155. rc = check_dev(dev);
  1156. if (rc < 0)
  1157. return rc;
  1158. if (0 != f->tuner)
  1159. return -EINVAL;
  1160. if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
  1161. return -EINVAL;
  1162. if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
  1163. return -EINVAL;
  1164. /* set pre channel change settings in DIF first */
  1165. rc = cx231xx_tuner_pre_channel_change(dev);
  1166. dev->ctl_freq = f->frequency;
  1167. call_all(dev, tuner, s_frequency, f);
  1168. /* set post channel change settings in DIF first */
  1169. rc = cx231xx_tuner_post_channel_change(dev);
  1170. if (dev->tuner_type == TUNER_NXP_TDA18271) {
  1171. if (dev->norm & (V4L2_STD_MN | V4L2_STD_NTSC_443))
  1172. if_frequency = 5400000; /*5.4MHz */
  1173. else if (dev->norm & V4L2_STD_B)
  1174. if_frequency = 6000000; /*6.0MHz */
  1175. else if (dev->norm & (V4L2_STD_PAL_DK | V4L2_STD_SECAM_DK))
  1176. if_frequency = 6900000; /*6.9MHz */
  1177. else if (dev->norm & V4L2_STD_GH)
  1178. if_frequency = 7100000; /*7.1MHz */
  1179. else if (dev->norm & V4L2_STD_PAL_I)
  1180. if_frequency = 7250000; /*7.25MHz */
  1181. else if (dev->norm & V4L2_STD_SECAM_L)
  1182. if_frequency = 6900000; /*6.9MHz */
  1183. else if (dev->norm & V4L2_STD_SECAM_LC)
  1184. if_frequency = 1250000; /*1.25MHz */
  1185. cx231xx_info("if_frequency is set to %d\n", if_frequency);
  1186. cx231xx_set_Colibri_For_LowIF(dev, if_frequency, 1, 1);
  1187. update_HH_register_after_set_DIF(dev);
  1188. }
  1189. cx231xx_info("Set New FREQUENCY to %d\n", f->frequency);
  1190. return rc;
  1191. }
  1192. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1193. /*
  1194. -R, --list-registers=type=<host/i2cdrv/i2caddr>,
  1195. chip=<chip>[,min=<addr>,max=<addr>]
  1196. dump registers from <min> to <max> [VIDIOC_DBG_G_REGISTER]
  1197. -r, --set-register=type=<host/i2cdrv/i2caddr>,
  1198. chip=<chip>,reg=<addr>,val=<val>
  1199. set the register [VIDIOC_DBG_S_REGISTER]
  1200. if type == host, then <chip> is the hosts chip ID (default 0)
  1201. if type == i2cdrv (default), then <chip> is the I2C driver name or ID
  1202. if type == i2caddr, then <chip> is the 7-bit I2C address
  1203. */
  1204. static int vidioc_g_register(struct file *file, void *priv,
  1205. struct v4l2_dbg_register *reg)
  1206. {
  1207. struct cx231xx_fh *fh = priv;
  1208. struct cx231xx *dev = fh->dev;
  1209. int ret = 0;
  1210. u8 value[4] = { 0, 0, 0, 0 };
  1211. u32 data = 0;
  1212. switch (reg->match.type) {
  1213. case V4L2_CHIP_MATCH_HOST:
  1214. switch (reg->match.addr) {
  1215. case 0: /* Cx231xx - internal registers */
  1216. ret = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER,
  1217. (u16)reg->reg, value, 4);
  1218. reg->val = value[0] | value[1] << 8 |
  1219. value[2] << 16 | value[3] << 24;
  1220. break;
  1221. case 1: /* AFE - read byte */
  1222. ret = cx231xx_read_i2c_data(dev, AFE_DEVICE_ADDRESS,
  1223. (u16)reg->reg, 2, &data, 1);
  1224. reg->val = le32_to_cpu(data & 0xff);
  1225. break;
  1226. case 14: /* AFE - read dword */
  1227. ret = cx231xx_read_i2c_data(dev, AFE_DEVICE_ADDRESS,
  1228. (u16)reg->reg, 2, &data, 4);
  1229. reg->val = le32_to_cpu(data);
  1230. break;
  1231. case 2: /* Video Block - read byte */
  1232. ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
  1233. (u16)reg->reg, 2, &data, 1);
  1234. reg->val = le32_to_cpu(data & 0xff);
  1235. break;
  1236. case 24: /* Video Block - read dword */
  1237. ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
  1238. (u16)reg->reg, 2, &data, 4);
  1239. reg->val = le32_to_cpu(data);
  1240. break;
  1241. case 3: /* I2S block - read byte */
  1242. ret = cx231xx_read_i2c_data(dev,
  1243. I2S_BLK_DEVICE_ADDRESS,
  1244. (u16)reg->reg, 1,
  1245. &data, 1);
  1246. reg->val = le32_to_cpu(data & 0xff);
  1247. break;
  1248. case 34: /* I2S Block - read dword */
  1249. ret =
  1250. cx231xx_read_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
  1251. (u16)reg->reg, 1, &data, 4);
  1252. reg->val = le32_to_cpu(data);
  1253. break;
  1254. }
  1255. return ret < 0 ? ret : 0;
  1256. case V4L2_CHIP_MATCH_I2C_DRIVER:
  1257. call_all(dev, core, g_register, reg);
  1258. return 0;
  1259. case V4L2_CHIP_MATCH_I2C_ADDR:/*for register debug*/
  1260. switch (reg->match.addr) {
  1261. case 0: /* Cx231xx - internal registers */
  1262. ret = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER,
  1263. (u16)reg->reg, value, 4);
  1264. reg->val = value[0] | value[1] << 8 |
  1265. value[2] << 16 | value[3] << 24;
  1266. break;
  1267. case 0x600:/* AFE - read byte */
  1268. ret = cx231xx_read_i2c_master(dev, AFE_DEVICE_ADDRESS,
  1269. (u16)reg->reg, 2,
  1270. &data, 1 , 0);
  1271. reg->val = le32_to_cpu(data & 0xff);
  1272. break;
  1273. case 0x880:/* Video Block - read byte */
  1274. if (reg->reg < 0x0b) {
  1275. ret = cx231xx_read_i2c_master(dev,
  1276. VID_BLK_I2C_ADDRESS,
  1277. (u16)reg->reg, 2,
  1278. &data, 1 , 0);
  1279. reg->val = le32_to_cpu(data & 0xff);
  1280. } else {
  1281. ret = cx231xx_read_i2c_master(dev,
  1282. VID_BLK_I2C_ADDRESS,
  1283. (u16)reg->reg, 2,
  1284. &data, 4 , 0);
  1285. reg->val = le32_to_cpu(data);
  1286. }
  1287. break;
  1288. case 0x980:
  1289. ret = cx231xx_read_i2c_master(dev,
  1290. I2S_BLK_DEVICE_ADDRESS,
  1291. (u16)reg->reg, 1,
  1292. &data, 1 , 0);
  1293. reg->val = le32_to_cpu(data & 0xff);
  1294. break;
  1295. case 0x400:
  1296. ret =
  1297. cx231xx_read_i2c_master(dev, 0x40,
  1298. (u16)reg->reg, 1,
  1299. &data, 1 , 0);
  1300. reg->val = le32_to_cpu(data & 0xff);
  1301. break;
  1302. case 0xc01:
  1303. ret =
  1304. cx231xx_read_i2c_master(dev, 0xc0,
  1305. (u16)reg->reg, 2,
  1306. &data, 38, 1);
  1307. reg->val = le32_to_cpu(data);
  1308. break;
  1309. case 0x022:
  1310. ret =
  1311. cx231xx_read_i2c_master(dev, 0x02,
  1312. (u16)reg->reg, 1,
  1313. &data, 1, 2);
  1314. reg->val = le32_to_cpu(data & 0xff);
  1315. break;
  1316. case 0x322:
  1317. ret = cx231xx_read_i2c_master(dev,
  1318. 0x32,
  1319. (u16)reg->reg, 1,
  1320. &data, 4 , 2);
  1321. reg->val = le32_to_cpu(data);
  1322. break;
  1323. case 0x342:
  1324. ret = cx231xx_read_i2c_master(dev,
  1325. 0x34,
  1326. (u16)reg->reg, 1,
  1327. &data, 4 , 2);
  1328. reg->val = le32_to_cpu(data);
  1329. break;
  1330. default:
  1331. cx231xx_info("no match device address!!\n");
  1332. break;
  1333. }
  1334. return ret < 0 ? ret : 0;
  1335. /*return -EINVAL;*/
  1336. default:
  1337. if (!v4l2_chip_match_host(&reg->match))
  1338. return -EINVAL;
  1339. }
  1340. call_all(dev, core, g_register, reg);
  1341. return ret;
  1342. }
  1343. static int vidioc_s_register(struct file *file, void *priv,
  1344. struct v4l2_dbg_register *reg)
  1345. {
  1346. struct cx231xx_fh *fh = priv;
  1347. struct cx231xx *dev = fh->dev;
  1348. int ret = 0;
  1349. __le64 buf;
  1350. u32 value;
  1351. u8 data[4] = { 0, 0, 0, 0 };
  1352. buf = cpu_to_le64(reg->val);
  1353. switch (reg->match.type) {
  1354. case V4L2_CHIP_MATCH_HOST:
  1355. {
  1356. value = (u32) buf & 0xffffffff;
  1357. switch (reg->match.addr) {
  1358. case 0: /* cx231xx internal registers */
  1359. data[0] = (u8) value;
  1360. data[1] = (u8) (value >> 8);
  1361. data[2] = (u8) (value >> 16);
  1362. data[3] = (u8) (value >> 24);
  1363. ret = cx231xx_write_ctrl_reg(dev,
  1364. VRT_SET_REGISTER,
  1365. (u16)reg->reg, data,
  1366. 4);
  1367. break;
  1368. case 1: /* AFE - read byte */
  1369. ret = cx231xx_write_i2c_data(dev,
  1370. AFE_DEVICE_ADDRESS,
  1371. (u16)reg->reg, 2,
  1372. value, 1);
  1373. break;
  1374. case 14: /* AFE - read dword */
  1375. ret = cx231xx_write_i2c_data(dev,
  1376. AFE_DEVICE_ADDRESS,
  1377. (u16)reg->reg, 2,
  1378. value, 4);
  1379. break;
  1380. case 2: /* Video Block - read byte */
  1381. ret =
  1382. cx231xx_write_i2c_data(dev,
  1383. VID_BLK_I2C_ADDRESS,
  1384. (u16)reg->reg, 2,
  1385. value, 1);
  1386. break;
  1387. case 24: /* Video Block - read dword */
  1388. ret =
  1389. cx231xx_write_i2c_data(dev,
  1390. VID_BLK_I2C_ADDRESS,
  1391. (u16)reg->reg, 2,
  1392. value, 4);
  1393. break;
  1394. case 3: /* I2S block - read byte */
  1395. ret =
  1396. cx231xx_write_i2c_data(dev,
  1397. I2S_BLK_DEVICE_ADDRESS,
  1398. (u16)reg->reg, 1,
  1399. value, 1);
  1400. break;
  1401. case 34: /* I2S block - read dword */
  1402. ret =
  1403. cx231xx_write_i2c_data(dev,
  1404. I2S_BLK_DEVICE_ADDRESS,
  1405. (u16)reg->reg, 1,
  1406. value, 4);
  1407. break;
  1408. }
  1409. }
  1410. return ret < 0 ? ret : 0;
  1411. case V4L2_CHIP_MATCH_I2C_ADDR:
  1412. {
  1413. value = (u32) buf & 0xffffffff;
  1414. switch (reg->match.addr) {
  1415. case 0:/*cx231xx internal registers*/
  1416. data[0] = (u8) value;
  1417. data[1] = (u8) (value >> 8);
  1418. data[2] = (u8) (value >> 16);
  1419. data[3] = (u8) (value >> 24);
  1420. ret = cx231xx_write_ctrl_reg(dev,
  1421. VRT_SET_REGISTER,
  1422. (u16)reg->reg, data,
  1423. 4);
  1424. break;
  1425. case 0x600:/* AFE - read byte */
  1426. ret = cx231xx_write_i2c_master(dev,
  1427. AFE_DEVICE_ADDRESS,
  1428. (u16)reg->reg, 2,
  1429. value, 1 , 0);
  1430. break;
  1431. case 0x880:/* Video Block - read byte */
  1432. if (reg->reg < 0x0b)
  1433. cx231xx_write_i2c_master(dev,
  1434. VID_BLK_I2C_ADDRESS,
  1435. (u16)reg->reg, 2,
  1436. value, 1, 0);
  1437. else
  1438. cx231xx_write_i2c_master(dev,
  1439. VID_BLK_I2C_ADDRESS,
  1440. (u16)reg->reg, 2,
  1441. value, 4, 0);
  1442. break;
  1443. case 0x980:
  1444. ret =
  1445. cx231xx_write_i2c_master(dev,
  1446. I2S_BLK_DEVICE_ADDRESS,
  1447. (u16)reg->reg, 1,
  1448. value, 1, 0);
  1449. break;
  1450. case 0x400:
  1451. ret =
  1452. cx231xx_write_i2c_master(dev,
  1453. 0x40,
  1454. (u16)reg->reg, 1,
  1455. value, 1, 0);
  1456. break;
  1457. case 0xc01:
  1458. ret =
  1459. cx231xx_write_i2c_master(dev,
  1460. 0xc0,
  1461. (u16)reg->reg, 1,
  1462. value, 1, 1);
  1463. break;
  1464. case 0x022:
  1465. ret =
  1466. cx231xx_write_i2c_master(dev,
  1467. 0x02,
  1468. (u16)reg->reg, 1,
  1469. value, 1, 2);
  1470. case 0x322:
  1471. ret =
  1472. cx231xx_write_i2c_master(dev,
  1473. 0x32,
  1474. (u16)reg->reg, 1,
  1475. value, 4, 2);
  1476. break;
  1477. case 0x342:
  1478. ret =
  1479. cx231xx_write_i2c_master(dev,
  1480. 0x34,
  1481. (u16)reg->reg, 1,
  1482. value, 4, 2);
  1483. break;
  1484. default:
  1485. cx231xx_info("no match device address, "
  1486. "the value is %x\n", reg->match.addr);
  1487. break;
  1488. }
  1489. }
  1490. default:
  1491. break;
  1492. }
  1493. call_all(dev, core, s_register, reg);
  1494. return ret;
  1495. }
  1496. #endif
  1497. static int vidioc_cropcap(struct file *file, void *priv,
  1498. struct v4l2_cropcap *cc)
  1499. {
  1500. struct cx231xx_fh *fh = priv;
  1501. struct cx231xx *dev = fh->dev;
  1502. if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1503. return -EINVAL;
  1504. cc->bounds.left = 0;
  1505. cc->bounds.top = 0;
  1506. cc->bounds.width = dev->width;
  1507. cc->bounds.height = dev->height;
  1508. cc->defrect = cc->bounds;
  1509. cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
  1510. cc->pixelaspect.denominator = 59;
  1511. return 0;
  1512. }
  1513. static int vidioc_streamon(struct file *file, void *priv,
  1514. enum v4l2_buf_type type)
  1515. {
  1516. struct cx231xx_fh *fh = priv;
  1517. struct cx231xx *dev = fh->dev;
  1518. int rc;
  1519. rc = check_dev(dev);
  1520. if (rc < 0)
  1521. return rc;
  1522. rc = res_get(fh);
  1523. if (likely(rc >= 0))
  1524. rc = videobuf_streamon(&fh->vb_vidq);
  1525. call_all(dev, video, s_stream, 1);
  1526. return rc;
  1527. }
  1528. static int vidioc_streamoff(struct file *file, void *priv,
  1529. enum v4l2_buf_type type)
  1530. {
  1531. struct cx231xx_fh *fh = priv;
  1532. struct cx231xx *dev = fh->dev;
  1533. int rc;
  1534. rc = check_dev(dev);
  1535. if (rc < 0)
  1536. return rc;
  1537. if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
  1538. (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE))
  1539. return -EINVAL;
  1540. if (type != fh->type)
  1541. return -EINVAL;
  1542. cx25840_call(dev, video, s_stream, 0);
  1543. videobuf_streamoff(&fh->vb_vidq);
  1544. res_free(fh);
  1545. return 0;
  1546. }
  1547. static int vidioc_querycap(struct file *file, void *priv,
  1548. struct v4l2_capability *cap)
  1549. {
  1550. struct cx231xx_fh *fh = priv;
  1551. struct cx231xx *dev = fh->dev;
  1552. strlcpy(cap->driver, "cx231xx", sizeof(cap->driver));
  1553. strlcpy(cap->card, cx231xx_boards[dev->model].name, sizeof(cap->card));
  1554. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  1555. cap->version = CX231XX_VERSION_CODE;
  1556. cap->capabilities = V4L2_CAP_VBI_CAPTURE |
  1557. #if 0
  1558. V4L2_CAP_SLICED_VBI_CAPTURE |
  1559. #endif
  1560. V4L2_CAP_VIDEO_CAPTURE |
  1561. V4L2_CAP_AUDIO |
  1562. V4L2_CAP_READWRITE |
  1563. V4L2_CAP_STREAMING;
  1564. if (dev->tuner_type != TUNER_ABSENT)
  1565. cap->capabilities |= V4L2_CAP_TUNER;
  1566. return 0;
  1567. }
  1568. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  1569. struct v4l2_fmtdesc *f)
  1570. {
  1571. if (unlikely(f->index >= ARRAY_SIZE(format)))
  1572. return -EINVAL;
  1573. strlcpy(f->description, format[f->index].name, sizeof(f->description));
  1574. f->pixelformat = format[f->index].fourcc;
  1575. return 0;
  1576. }
  1577. /* Sliced VBI ioctls */
  1578. static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
  1579. struct v4l2_format *f)
  1580. {
  1581. struct cx231xx_fh *fh = priv;
  1582. struct cx231xx *dev = fh->dev;
  1583. int rc;
  1584. rc = check_dev(dev);
  1585. if (rc < 0)
  1586. return rc;
  1587. f->fmt.sliced.service_set = 0;
  1588. call_all(dev, vbi, g_sliced_fmt, &f->fmt.sliced);
  1589. if (f->fmt.sliced.service_set == 0)
  1590. rc = -EINVAL;
  1591. return rc;
  1592. }
  1593. static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
  1594. struct v4l2_format *f)
  1595. {
  1596. struct cx231xx_fh *fh = priv;
  1597. struct cx231xx *dev = fh->dev;
  1598. int rc;
  1599. rc = check_dev(dev);
  1600. if (rc < 0)
  1601. return rc;
  1602. call_all(dev, vbi, g_sliced_fmt, &f->fmt.sliced);
  1603. if (f->fmt.sliced.service_set == 0)
  1604. return -EINVAL;
  1605. return 0;
  1606. }
  1607. /* RAW VBI ioctls */
  1608. static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
  1609. struct v4l2_format *f)
  1610. {
  1611. struct cx231xx_fh *fh = priv;
  1612. struct cx231xx *dev = fh->dev;
  1613. f->fmt.vbi.sampling_rate = 6750000 * 4;
  1614. f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
  1615. f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  1616. f->fmt.vbi.offset = 0;
  1617. f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
  1618. PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
  1619. f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
  1620. PAL_VBI_LINES : NTSC_VBI_LINES;
  1621. f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
  1622. PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
  1623. f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
  1624. return 0;
  1625. }
  1626. static int vidioc_try_fmt_vbi_cap(struct file *file, void *priv,
  1627. struct v4l2_format *f)
  1628. {
  1629. struct cx231xx_fh *fh = priv;
  1630. struct cx231xx *dev = fh->dev;
  1631. if (dev->vbi_stream_on && !fh->stream_on) {
  1632. cx231xx_errdev("%s device in use by another fh\n", __func__);
  1633. return -EBUSY;
  1634. }
  1635. f->type = V4L2_BUF_TYPE_VBI_CAPTURE;
  1636. f->fmt.vbi.sampling_rate = 6750000 * 4;
  1637. f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
  1638. f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  1639. f->fmt.vbi.offset = 0;
  1640. f->fmt.vbi.flags = 0;
  1641. f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
  1642. PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
  1643. f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
  1644. PAL_VBI_LINES : NTSC_VBI_LINES;
  1645. f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
  1646. PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
  1647. f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
  1648. return 0;
  1649. }
  1650. static int vidioc_reqbufs(struct file *file, void *priv,
  1651. struct v4l2_requestbuffers *rb)
  1652. {
  1653. struct cx231xx_fh *fh = priv;
  1654. struct cx231xx *dev = fh->dev;
  1655. int rc;
  1656. rc = check_dev(dev);
  1657. if (rc < 0)
  1658. return rc;
  1659. return videobuf_reqbufs(&fh->vb_vidq, rb);
  1660. }
  1661. static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1662. {
  1663. struct cx231xx_fh *fh = priv;
  1664. struct cx231xx *dev = fh->dev;
  1665. int rc;
  1666. rc = check_dev(dev);
  1667. if (rc < 0)
  1668. return rc;
  1669. return videobuf_querybuf(&fh->vb_vidq, b);
  1670. }
  1671. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1672. {
  1673. struct cx231xx_fh *fh = priv;
  1674. struct cx231xx *dev = fh->dev;
  1675. int rc;
  1676. rc = check_dev(dev);
  1677. if (rc < 0)
  1678. return rc;
  1679. return videobuf_qbuf(&fh->vb_vidq, b);
  1680. }
  1681. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1682. {
  1683. struct cx231xx_fh *fh = priv;
  1684. struct cx231xx *dev = fh->dev;
  1685. int rc;
  1686. rc = check_dev(dev);
  1687. if (rc < 0)
  1688. return rc;
  1689. return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK);
  1690. }
  1691. /* ----------------------------------------------------------- */
  1692. /* RADIO ESPECIFIC IOCTLS */
  1693. /* ----------------------------------------------------------- */
  1694. static int radio_querycap(struct file *file, void *priv,
  1695. struct v4l2_capability *cap)
  1696. {
  1697. struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
  1698. strlcpy(cap->driver, "cx231xx", sizeof(cap->driver));
  1699. strlcpy(cap->card, cx231xx_boards[dev->model].name, sizeof(cap->card));
  1700. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  1701. cap->version = CX231XX_VERSION_CODE;
  1702. cap->capabilities = V4L2_CAP_TUNER;
  1703. return 0;
  1704. }
  1705. static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
  1706. {
  1707. struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
  1708. if (unlikely(t->index > 0))
  1709. return -EINVAL;
  1710. strcpy(t->name, "Radio");
  1711. t->type = V4L2_TUNER_RADIO;
  1712. call_all(dev, tuner, s_tuner, t);
  1713. return 0;
  1714. }
  1715. static int radio_enum_input(struct file *file, void *priv, struct v4l2_input *i)
  1716. {
  1717. if (i->index != 0)
  1718. return -EINVAL;
  1719. strcpy(i->name, "Radio");
  1720. i->type = V4L2_INPUT_TYPE_TUNER;
  1721. return 0;
  1722. }
  1723. static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  1724. {
  1725. if (unlikely(a->index))
  1726. return -EINVAL;
  1727. strcpy(a->name, "Radio");
  1728. return 0;
  1729. }
  1730. static int radio_s_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
  1731. {
  1732. struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
  1733. if (0 != t->index)
  1734. return -EINVAL;
  1735. call_all(dev, tuner, s_tuner, t);
  1736. return 0;
  1737. }
  1738. static int radio_s_audio(struct file *file, void *fh, struct v4l2_audio *a)
  1739. {
  1740. return 0;
  1741. }
  1742. static int radio_s_input(struct file *file, void *fh, unsigned int i)
  1743. {
  1744. return 0;
  1745. }
  1746. static int radio_queryctrl(struct file *file, void *priv,
  1747. struct v4l2_queryctrl *c)
  1748. {
  1749. int i;
  1750. if (c->id < V4L2_CID_BASE || c->id >= V4L2_CID_LASTP1)
  1751. return -EINVAL;
  1752. if (c->id == V4L2_CID_AUDIO_MUTE) {
  1753. for (i = 0; i < CX231XX_CTLS; i++) {
  1754. if (cx231xx_ctls[i].v.id == c->id)
  1755. break;
  1756. }
  1757. if (i == CX231XX_CTLS)
  1758. return -EINVAL;
  1759. *c = cx231xx_ctls[i].v;
  1760. } else
  1761. *c = no_ctl;
  1762. return 0;
  1763. }
  1764. /*
  1765. * cx231xx_v4l2_open()
  1766. * inits the device and starts isoc transfer
  1767. */
  1768. static int cx231xx_v4l2_open(struct file *filp)
  1769. {
  1770. int errCode = 0, radio = 0;
  1771. struct video_device *vdev = video_devdata(filp);
  1772. struct cx231xx *dev = video_drvdata(filp);
  1773. struct cx231xx_fh *fh;
  1774. enum v4l2_buf_type fh_type = 0;
  1775. switch (vdev->vfl_type) {
  1776. case VFL_TYPE_GRABBER:
  1777. fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1778. break;
  1779. case VFL_TYPE_VBI:
  1780. fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
  1781. break;
  1782. case VFL_TYPE_RADIO:
  1783. radio = 1;
  1784. break;
  1785. }
  1786. cx231xx_videodbg("open dev=%s type=%s users=%d\n",
  1787. video_device_node_name(vdev), v4l2_type_names[fh_type],
  1788. dev->users);
  1789. #if 0
  1790. errCode = cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
  1791. if (errCode < 0) {
  1792. cx231xx_errdev
  1793. ("Device locked on digital mode. Can't open analog\n");
  1794. return -EBUSY;
  1795. }
  1796. #endif
  1797. fh = kzalloc(sizeof(struct cx231xx_fh), GFP_KERNEL);
  1798. if (!fh) {
  1799. cx231xx_errdev("cx231xx-video.c: Out of memory?!\n");
  1800. return -ENOMEM;
  1801. }
  1802. fh->dev = dev;
  1803. fh->radio = radio;
  1804. fh->type = fh_type;
  1805. filp->private_data = fh;
  1806. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
  1807. dev->width = norm_maxw(dev);
  1808. dev->height = norm_maxh(dev);
  1809. /* Power up in Analog TV mode */
  1810. if (dev->board.external_av)
  1811. cx231xx_set_power_mode(dev,
  1812. POLARIS_AVMODE_ENXTERNAL_AV);
  1813. else
  1814. cx231xx_set_power_mode(dev, POLARIS_AVMODE_ANALOGT_TV);
  1815. #if 0
  1816. cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
  1817. #endif
  1818. /* set video alternate setting */
  1819. cx231xx_set_video_alternate(dev);
  1820. /* Needed, since GPIO might have disabled power of
  1821. some i2c device */
  1822. cx231xx_config_i2c(dev);
  1823. /* device needs to be initialized before isoc transfer */
  1824. dev->video_input = dev->video_input > 2 ? 2 : dev->video_input;
  1825. }
  1826. if (fh->radio) {
  1827. cx231xx_videodbg("video_open: setting radio device\n");
  1828. /* cx231xx_start_radio(dev); */
  1829. call_all(dev, tuner, s_radio);
  1830. }
  1831. dev->users++;
  1832. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1833. videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_video_qops,
  1834. NULL, &dev->video_mode.slock,
  1835. fh->type, V4L2_FIELD_INTERLACED,
  1836. sizeof(struct cx231xx_buffer),
  1837. fh, &dev->lock);
  1838. if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
  1839. /* Set the required alternate setting VBI interface works in
  1840. Bulk mode only */
  1841. cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
  1842. videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_vbi_qops,
  1843. NULL, &dev->vbi_mode.slock,
  1844. fh->type, V4L2_FIELD_SEQ_TB,
  1845. sizeof(struct cx231xx_buffer),
  1846. fh, &dev->lock);
  1847. }
  1848. return errCode;
  1849. }
  1850. /*
  1851. * cx231xx_realease_resources()
  1852. * unregisters the v4l2,i2c and usb devices
  1853. * called when the device gets disconected or at module unload
  1854. */
  1855. void cx231xx_release_analog_resources(struct cx231xx *dev)
  1856. {
  1857. /*FIXME: I2C IR should be disconnected */
  1858. if (dev->radio_dev) {
  1859. if (video_is_registered(dev->radio_dev))
  1860. video_unregister_device(dev->radio_dev);
  1861. else
  1862. video_device_release(dev->radio_dev);
  1863. dev->radio_dev = NULL;
  1864. }
  1865. if (dev->vbi_dev) {
  1866. cx231xx_info("V4L2 device %s deregistered\n",
  1867. video_device_node_name(dev->vbi_dev));
  1868. if (video_is_registered(dev->vbi_dev))
  1869. video_unregister_device(dev->vbi_dev);
  1870. else
  1871. video_device_release(dev->vbi_dev);
  1872. dev->vbi_dev = NULL;
  1873. }
  1874. if (dev->vdev) {
  1875. cx231xx_info("V4L2 device %s deregistered\n",
  1876. video_device_node_name(dev->vdev));
  1877. if (dev->board.has_417)
  1878. cx231xx_417_unregister(dev);
  1879. if (video_is_registered(dev->vdev))
  1880. video_unregister_device(dev->vdev);
  1881. else
  1882. video_device_release(dev->vdev);
  1883. dev->vdev = NULL;
  1884. }
  1885. }
  1886. /*
  1887. * cx231xx_v4l2_close()
  1888. * stops streaming and deallocates all resources allocated by the v4l2
  1889. * calls and ioctls
  1890. */
  1891. static int cx231xx_v4l2_close(struct file *filp)
  1892. {
  1893. struct cx231xx_fh *fh = filp->private_data;
  1894. struct cx231xx *dev = fh->dev;
  1895. cx231xx_videodbg("users=%d\n", dev->users);
  1896. cx231xx_videodbg("users=%d\n", dev->users);
  1897. if (res_check(fh))
  1898. res_free(fh);
  1899. /*
  1900. * To workaround error number=-71 on EP0 for VideoGrabber,
  1901. * need exclude following.
  1902. * FIXME: It is probably safe to remove most of these, as we're
  1903. * now avoiding the alternate setting for INDEX_VANC
  1904. */
  1905. if (!dev->board.no_alt_vanc)
  1906. if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
  1907. videobuf_stop(&fh->vb_vidq);
  1908. videobuf_mmap_free(&fh->vb_vidq);
  1909. /* the device is already disconnect,
  1910. free the remaining resources */
  1911. if (dev->state & DEV_DISCONNECTED) {
  1912. if (atomic_read(&dev->devlist_count) > 0) {
  1913. cx231xx_release_resources(dev);
  1914. kfree(dev);
  1915. dev = NULL;
  1916. return 0;
  1917. }
  1918. return 0;
  1919. }
  1920. /* do this before setting alternate! */
  1921. cx231xx_uninit_vbi_isoc(dev);
  1922. /* set alternate 0 */
  1923. if (!dev->vbi_or_sliced_cc_mode)
  1924. cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
  1925. else
  1926. cx231xx_set_alt_setting(dev, INDEX_HANC, 0);
  1927. kfree(fh);
  1928. dev->users--;
  1929. wake_up_interruptible_nr(&dev->open, 1);
  1930. return 0;
  1931. }
  1932. if (dev->users == 1) {
  1933. videobuf_stop(&fh->vb_vidq);
  1934. videobuf_mmap_free(&fh->vb_vidq);
  1935. /* the device is already disconnect,
  1936. free the remaining resources */
  1937. if (dev->state & DEV_DISCONNECTED) {
  1938. cx231xx_release_resources(dev);
  1939. kfree(dev);
  1940. dev = NULL;
  1941. return 0;
  1942. }
  1943. /* Save some power by putting tuner to sleep */
  1944. call_all(dev, core, s_power, 0);
  1945. /* do this before setting alternate! */
  1946. if (dev->USE_ISO)
  1947. cx231xx_uninit_isoc(dev);
  1948. else
  1949. cx231xx_uninit_bulk(dev);
  1950. cx231xx_set_mode(dev, CX231XX_SUSPEND);
  1951. /* set alternate 0 */
  1952. cx231xx_set_alt_setting(dev, INDEX_VIDEO, 0);
  1953. }
  1954. kfree(fh);
  1955. dev->users--;
  1956. wake_up_interruptible_nr(&dev->open, 1);
  1957. return 0;
  1958. }
  1959. /*
  1960. * cx231xx_v4l2_read()
  1961. * will allocate buffers when called for the first time
  1962. */
  1963. static ssize_t
  1964. cx231xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
  1965. loff_t *pos)
  1966. {
  1967. struct cx231xx_fh *fh = filp->private_data;
  1968. struct cx231xx *dev = fh->dev;
  1969. int rc;
  1970. rc = check_dev(dev);
  1971. if (rc < 0)
  1972. return rc;
  1973. if ((fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
  1974. (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)) {
  1975. rc = res_get(fh);
  1976. if (unlikely(rc < 0))
  1977. return rc;
  1978. return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
  1979. filp->f_flags & O_NONBLOCK);
  1980. }
  1981. return 0;
  1982. }
  1983. /*
  1984. * cx231xx_v4l2_poll()
  1985. * will allocate buffers when called for the first time
  1986. */
  1987. static unsigned int cx231xx_v4l2_poll(struct file *filp, poll_table *wait)
  1988. {
  1989. struct cx231xx_fh *fh = filp->private_data;
  1990. struct cx231xx *dev = fh->dev;
  1991. int rc;
  1992. rc = check_dev(dev);
  1993. if (rc < 0)
  1994. return rc;
  1995. rc = res_get(fh);
  1996. if (unlikely(rc < 0))
  1997. return POLLERR;
  1998. if ((V4L2_BUF_TYPE_VIDEO_CAPTURE == fh->type) ||
  1999. (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type))
  2000. return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
  2001. else
  2002. return POLLERR;
  2003. }
  2004. /*
  2005. * cx231xx_v4l2_mmap()
  2006. */
  2007. static int cx231xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
  2008. {
  2009. struct cx231xx_fh *fh = filp->private_data;
  2010. struct cx231xx *dev = fh->dev;
  2011. int rc;
  2012. rc = check_dev(dev);
  2013. if (rc < 0)
  2014. return rc;
  2015. rc = res_get(fh);
  2016. if (unlikely(rc < 0))
  2017. return rc;
  2018. rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
  2019. cx231xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
  2020. (unsigned long)vma->vm_start,
  2021. (unsigned long)vma->vm_end -
  2022. (unsigned long)vma->vm_start, rc);
  2023. return rc;
  2024. }
  2025. static const struct v4l2_file_operations cx231xx_v4l_fops = {
  2026. .owner = THIS_MODULE,
  2027. .open = cx231xx_v4l2_open,
  2028. .release = cx231xx_v4l2_close,
  2029. .read = cx231xx_v4l2_read,
  2030. .poll = cx231xx_v4l2_poll,
  2031. .mmap = cx231xx_v4l2_mmap,
  2032. .unlocked_ioctl = video_ioctl2,
  2033. };
  2034. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  2035. .vidioc_querycap = vidioc_querycap,
  2036. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  2037. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  2038. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  2039. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  2040. .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
  2041. .vidioc_try_fmt_vbi_cap = vidioc_try_fmt_vbi_cap,
  2042. .vidioc_s_fmt_vbi_cap = vidioc_try_fmt_vbi_cap,
  2043. .vidioc_g_audio = vidioc_g_audio,
  2044. .vidioc_s_audio = vidioc_s_audio,
  2045. .vidioc_cropcap = vidioc_cropcap,
  2046. .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
  2047. .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
  2048. .vidioc_reqbufs = vidioc_reqbufs,
  2049. .vidioc_querybuf = vidioc_querybuf,
  2050. .vidioc_qbuf = vidioc_qbuf,
  2051. .vidioc_dqbuf = vidioc_dqbuf,
  2052. .vidioc_s_std = vidioc_s_std,
  2053. .vidioc_g_std = vidioc_g_std,
  2054. .vidioc_enum_input = vidioc_enum_input,
  2055. .vidioc_g_input = vidioc_g_input,
  2056. .vidioc_s_input = vidioc_s_input,
  2057. .vidioc_queryctrl = vidioc_queryctrl,
  2058. .vidioc_g_ctrl = vidioc_g_ctrl,
  2059. .vidioc_s_ctrl = vidioc_s_ctrl,
  2060. .vidioc_streamon = vidioc_streamon,
  2061. .vidioc_streamoff = vidioc_streamoff,
  2062. .vidioc_g_tuner = vidioc_g_tuner,
  2063. .vidioc_s_tuner = vidioc_s_tuner,
  2064. .vidioc_g_frequency = vidioc_g_frequency,
  2065. .vidioc_s_frequency = vidioc_s_frequency,
  2066. #ifdef CONFIG_VIDEO_ADV_DEBUG
  2067. .vidioc_g_register = vidioc_g_register,
  2068. .vidioc_s_register = vidioc_s_register,
  2069. #endif
  2070. };
  2071. static struct video_device cx231xx_vbi_template;
  2072. static const struct video_device cx231xx_video_template = {
  2073. .fops = &cx231xx_v4l_fops,
  2074. .release = video_device_release,
  2075. .ioctl_ops = &video_ioctl_ops,
  2076. .tvnorms = V4L2_STD_ALL,
  2077. .current_norm = V4L2_STD_PAL,
  2078. };
  2079. static const struct v4l2_file_operations radio_fops = {
  2080. .owner = THIS_MODULE,
  2081. .open = cx231xx_v4l2_open,
  2082. .release = cx231xx_v4l2_close,
  2083. .ioctl = video_ioctl2,
  2084. };
  2085. static const struct v4l2_ioctl_ops radio_ioctl_ops = {
  2086. .vidioc_querycap = radio_querycap,
  2087. .vidioc_g_tuner = radio_g_tuner,
  2088. .vidioc_enum_input = radio_enum_input,
  2089. .vidioc_g_audio = radio_g_audio,
  2090. .vidioc_s_tuner = radio_s_tuner,
  2091. .vidioc_s_audio = radio_s_audio,
  2092. .vidioc_s_input = radio_s_input,
  2093. .vidioc_queryctrl = radio_queryctrl,
  2094. .vidioc_g_ctrl = vidioc_g_ctrl,
  2095. .vidioc_s_ctrl = vidioc_s_ctrl,
  2096. .vidioc_g_frequency = vidioc_g_frequency,
  2097. .vidioc_s_frequency = vidioc_s_frequency,
  2098. #ifdef CONFIG_VIDEO_ADV_DEBUG
  2099. .vidioc_g_register = vidioc_g_register,
  2100. .vidioc_s_register = vidioc_s_register,
  2101. #endif
  2102. };
  2103. static struct video_device cx231xx_radio_template = {
  2104. .name = "cx231xx-radio",
  2105. .fops = &radio_fops,
  2106. .ioctl_ops = &radio_ioctl_ops,
  2107. };
  2108. /******************************** usb interface ******************************/
  2109. static struct video_device *cx231xx_vdev_init(struct cx231xx *dev,
  2110. const struct video_device
  2111. *template, const char *type_name)
  2112. {
  2113. struct video_device *vfd;
  2114. vfd = video_device_alloc();
  2115. if (NULL == vfd)
  2116. return NULL;
  2117. *vfd = *template;
  2118. vfd->v4l2_dev = &dev->v4l2_dev;
  2119. vfd->release = video_device_release;
  2120. vfd->debug = video_debug;
  2121. vfd->lock = &dev->lock;
  2122. snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name);
  2123. video_set_drvdata(vfd, dev);
  2124. return vfd;
  2125. }
  2126. int cx231xx_register_analog_devices(struct cx231xx *dev)
  2127. {
  2128. int ret;
  2129. cx231xx_info("%s: v4l2 driver version %d.%d.%d\n",
  2130. dev->name,
  2131. (CX231XX_VERSION_CODE >> 16) & 0xff,
  2132. (CX231XX_VERSION_CODE >> 8) & 0xff,
  2133. CX231XX_VERSION_CODE & 0xff);
  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. }