cx231xx-video.c 58 KB

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