cx231xx-video.c 58 KB

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