cx231xx-video.c 64 KB

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