cx231xx-video.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442
  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. struct v4l2_routing route;
  680. route.input = INPUT(index)->vmux;
  681. route.output = 0;
  682. dev->video_input = index;
  683. dev->ctl_ainput = INPUT(index)->amux;
  684. cx231xx_set_video_input_mux(dev, index);
  685. cx25840_call(dev, video, s_routing, &route);
  686. cx231xx_set_audio_input(dev, dev->ctl_ainput);
  687. cx231xx_info("video_mux : %d\n", index);
  688. /* do mode control overrides if required */
  689. cx231xx_do_mode_ctrl_overrides(dev);
  690. }
  691. /* Usage lock check functions */
  692. static int res_get(struct cx231xx_fh *fh)
  693. {
  694. struct cx231xx *dev = fh->dev;
  695. int rc = 0;
  696. /* This instance already has stream_on */
  697. if (fh->stream_on)
  698. return rc;
  699. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  700. if (dev->stream_on)
  701. return -EBUSY;
  702. dev->stream_on = 1;
  703. } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
  704. if (dev->vbi_stream_on)
  705. return -EBUSY;
  706. dev->vbi_stream_on = 1;
  707. } else
  708. return -EINVAL;
  709. fh->stream_on = 1;
  710. return rc;
  711. }
  712. static int res_check(struct cx231xx_fh *fh)
  713. {
  714. return fh->stream_on;
  715. }
  716. static void res_free(struct cx231xx_fh *fh)
  717. {
  718. struct cx231xx *dev = fh->dev;
  719. fh->stream_on = 0;
  720. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  721. dev->stream_on = 0;
  722. if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
  723. dev->vbi_stream_on = 0;
  724. }
  725. static int check_dev(struct cx231xx *dev)
  726. {
  727. if (dev->state & DEV_DISCONNECTED) {
  728. cx231xx_errdev("v4l2 ioctl: device not present\n");
  729. return -ENODEV;
  730. }
  731. if (dev->state & DEV_MISCONFIGURED) {
  732. cx231xx_errdev("v4l2 ioctl: device is misconfigured; "
  733. "close and open it again\n");
  734. return -EIO;
  735. }
  736. return 0;
  737. }
  738. void get_scale(struct cx231xx *dev,
  739. unsigned int width, unsigned int height,
  740. unsigned int *hscale, unsigned int *vscale)
  741. {
  742. unsigned int maxw = norm_maxw(dev);
  743. unsigned int maxh = norm_maxh(dev);
  744. *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
  745. if (*hscale >= 0x4000)
  746. *hscale = 0x3fff;
  747. *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
  748. if (*vscale >= 0x4000)
  749. *vscale = 0x3fff;
  750. dev->hscale = *hscale;
  751. dev->vscale = *vscale;
  752. }
  753. /* ------------------------------------------------------------------
  754. IOCTL vidioc handling
  755. ------------------------------------------------------------------*/
  756. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  757. struct v4l2_format *f)
  758. {
  759. struct cx231xx_fh *fh = priv;
  760. struct cx231xx *dev = fh->dev;
  761. mutex_lock(&dev->lock);
  762. f->fmt.pix.width = dev->width;
  763. f->fmt.pix.height = dev->height;
  764. f->fmt.pix.pixelformat = dev->format->fourcc;;
  765. f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;;
  766. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
  767. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  768. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  769. mutex_unlock(&dev->lock);
  770. return 0;
  771. }
  772. static struct cx231xx_fmt *format_by_fourcc(unsigned int fourcc)
  773. {
  774. unsigned int i;
  775. for (i = 0; i < ARRAY_SIZE(format); i++)
  776. if (format[i].fourcc == fourcc)
  777. return &format[i];
  778. return NULL;
  779. }
  780. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  781. struct v4l2_format *f)
  782. {
  783. struct cx231xx_fh *fh = priv;
  784. struct cx231xx *dev = fh->dev;
  785. int width = f->fmt.pix.width;
  786. int height = f->fmt.pix.height;
  787. unsigned int maxw = norm_maxw(dev);
  788. unsigned int maxh = norm_maxh(dev);
  789. unsigned int hscale, vscale;
  790. struct cx231xx_fmt *fmt;
  791. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  792. if (!fmt) {
  793. cx231xx_videodbg("Fourcc format (%08x) invalid.\n",
  794. f->fmt.pix.pixelformat);
  795. return -EINVAL;
  796. }
  797. /* width must even because of the YUYV format
  798. height must be even because of interlacing */
  799. height &= 0xfffe;
  800. width &= 0xfffe;
  801. if (unlikely(height < 32))
  802. height = 32;
  803. if (unlikely(height > maxh))
  804. height = maxh;
  805. if (unlikely(width < 48))
  806. width = 48;
  807. if (unlikely(width > maxw))
  808. width = maxw;
  809. get_scale(dev, width, height, &hscale, &vscale);
  810. width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
  811. height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
  812. f->fmt.pix.width = width;
  813. f->fmt.pix.height = height;
  814. f->fmt.pix.pixelformat = fmt->fourcc;
  815. f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
  816. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
  817. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  818. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  819. return 0;
  820. }
  821. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  822. struct v4l2_format *f)
  823. {
  824. struct cx231xx_fh *fh = priv;
  825. struct cx231xx *dev = fh->dev;
  826. int rc;
  827. struct cx231xx_fmt *fmt;
  828. rc = check_dev(dev);
  829. if (rc < 0)
  830. return rc;
  831. mutex_lock(&dev->lock);
  832. vidioc_try_fmt_vid_cap(file, priv, f);
  833. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  834. if (!fmt) {
  835. rc = -EINVAL;
  836. goto out;
  837. }
  838. if (videobuf_queue_is_busy(&fh->vb_vidq)) {
  839. cx231xx_errdev("%s queue busy\n", __func__);
  840. rc = -EBUSY;
  841. goto out;
  842. }
  843. if (dev->stream_on && !fh->stream_on) {
  844. cx231xx_errdev("%s device in use by another fh\n", __func__);
  845. rc = -EBUSY;
  846. goto out;
  847. }
  848. /* set new image size */
  849. dev->width = f->fmt.pix.width;
  850. dev->height = f->fmt.pix.height;
  851. dev->format = fmt;
  852. get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
  853. call_all(dev, video, s_fmt, f);
  854. /* Set the correct alternate setting for this resolution */
  855. cx231xx_resolution_set(dev);
  856. out:
  857. mutex_unlock(&dev->lock);
  858. return rc;
  859. }
  860. static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id * id)
  861. {
  862. struct cx231xx_fh *fh = priv;
  863. struct cx231xx *dev = fh->dev;
  864. *id = dev->norm;
  865. return 0;
  866. }
  867. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
  868. {
  869. struct cx231xx_fh *fh = priv;
  870. struct cx231xx *dev = fh->dev;
  871. struct v4l2_format f;
  872. int rc;
  873. rc = check_dev(dev);
  874. if (rc < 0)
  875. return rc;
  876. cx231xx_info("vidioc_s_std : 0x%x\n", (unsigned int)*norm);
  877. mutex_lock(&dev->lock);
  878. dev->norm = *norm;
  879. /* Adjusts width/height, if needed */
  880. f.fmt.pix.width = dev->width;
  881. f.fmt.pix.height = dev->height;
  882. vidioc_try_fmt_vid_cap(file, priv, &f);
  883. /* set new image size */
  884. dev->width = f.fmt.pix.width;
  885. dev->height = f.fmt.pix.height;
  886. get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
  887. call_all(dev, tuner, s_std, dev->norm);
  888. mutex_unlock(&dev->lock);
  889. cx231xx_resolution_set(dev);
  890. /* do mode control overrides */
  891. cx231xx_do_mode_ctrl_overrides(dev);
  892. return 0;
  893. }
  894. static const char *iname[] = {
  895. [CX231XX_VMUX_COMPOSITE1] = "Composite1",
  896. [CX231XX_VMUX_SVIDEO] = "S-Video",
  897. [CX231XX_VMUX_TELEVISION] = "Television",
  898. [CX231XX_VMUX_CABLE] = "Cable TV",
  899. [CX231XX_VMUX_DVB] = "DVB",
  900. [CX231XX_VMUX_DEBUG] = "for debug only",
  901. };
  902. static int vidioc_enum_input(struct file *file, void *priv,
  903. struct v4l2_input *i)
  904. {
  905. struct cx231xx_fh *fh = priv;
  906. struct cx231xx *dev = fh->dev;
  907. unsigned int n;
  908. n = i->index;
  909. if (n >= MAX_CX231XX_INPUT)
  910. return -EINVAL;
  911. if (0 == INPUT(n)->type)
  912. return -EINVAL;
  913. i->index = n;
  914. i->type = V4L2_INPUT_TYPE_CAMERA;
  915. strcpy(i->name, iname[INPUT(n)->type]);
  916. if ((CX231XX_VMUX_TELEVISION == INPUT(n)->type) ||
  917. (CX231XX_VMUX_CABLE == INPUT(n)->type))
  918. i->type = V4L2_INPUT_TYPE_TUNER;
  919. i->std = dev->vdev->tvnorms;
  920. return 0;
  921. }
  922. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  923. {
  924. struct cx231xx_fh *fh = priv;
  925. struct cx231xx *dev = fh->dev;
  926. *i = dev->video_input;
  927. return 0;
  928. }
  929. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  930. {
  931. struct cx231xx_fh *fh = priv;
  932. struct cx231xx *dev = fh->dev;
  933. int rc;
  934. rc = check_dev(dev);
  935. if (rc < 0)
  936. return rc;
  937. if (i >= MAX_CX231XX_INPUT)
  938. return -EINVAL;
  939. if (0 == INPUT(i)->type)
  940. return -EINVAL;
  941. mutex_lock(&dev->lock);
  942. video_mux(dev, i);
  943. mutex_unlock(&dev->lock);
  944. return 0;
  945. }
  946. static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  947. {
  948. struct cx231xx_fh *fh = priv;
  949. struct cx231xx *dev = fh->dev;
  950. switch (a->index) {
  951. case CX231XX_AMUX_VIDEO:
  952. strcpy(a->name, "Television");
  953. break;
  954. case CX231XX_AMUX_LINE_IN:
  955. strcpy(a->name, "Line In");
  956. break;
  957. default:
  958. return -EINVAL;
  959. }
  960. a->index = dev->ctl_ainput;
  961. a->capability = V4L2_AUDCAP_STEREO;
  962. return 0;
  963. }
  964. static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
  965. {
  966. struct cx231xx_fh *fh = priv;
  967. struct cx231xx *dev = fh->dev;
  968. int status = 0;
  969. /* Doesn't allow manual routing */
  970. if (a->index != dev->ctl_ainput)
  971. return -EINVAL;
  972. dev->ctl_ainput = INPUT(a->index)->amux;
  973. status = cx231xx_set_audio_input(dev, dev->ctl_ainput);
  974. return status;
  975. }
  976. static int vidioc_queryctrl(struct file *file, void *priv,
  977. struct v4l2_queryctrl *qc)
  978. {
  979. struct cx231xx_fh *fh = priv;
  980. struct cx231xx *dev = fh->dev;
  981. int id = qc->id;
  982. int i;
  983. int rc;
  984. rc = check_dev(dev);
  985. if (rc < 0)
  986. return rc;
  987. qc->id = v4l2_ctrl_next(ctrl_classes, qc->id);
  988. if (unlikely(qc->id == 0))
  989. return -EINVAL;
  990. memset(qc, 0, sizeof(*qc));
  991. qc->id = id;
  992. if (qc->id < V4L2_CID_BASE || qc->id >= V4L2_CID_LASTP1)
  993. return -EINVAL;
  994. for (i = 0; i < CX231XX_CTLS; i++)
  995. if (cx231xx_ctls[i].v.id == qc->id)
  996. break;
  997. if (i == CX231XX_CTLS) {
  998. *qc = no_ctl;
  999. return 0;
  1000. }
  1001. *qc = cx231xx_ctls[i].v;
  1002. mutex_lock(&dev->lock);
  1003. call_all(dev, core, queryctrl, qc);
  1004. mutex_unlock(&dev->lock);
  1005. if (qc->type)
  1006. return 0;
  1007. else
  1008. return -EINVAL;
  1009. }
  1010. static int vidioc_g_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, g_ctrl, ctrl);
  1021. mutex_unlock(&dev->lock);
  1022. return rc;
  1023. }
  1024. static int vidioc_s_ctrl(struct file *file, void *priv,
  1025. struct v4l2_control *ctrl)
  1026. {
  1027. struct cx231xx_fh *fh = priv;
  1028. struct cx231xx *dev = fh->dev;
  1029. int rc;
  1030. rc = check_dev(dev);
  1031. if (rc < 0)
  1032. return rc;
  1033. mutex_lock(&dev->lock);
  1034. call_all(dev, core, s_ctrl, ctrl);
  1035. mutex_unlock(&dev->lock);
  1036. return rc;
  1037. }
  1038. static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
  1039. {
  1040. struct cx231xx_fh *fh = priv;
  1041. struct cx231xx *dev = fh->dev;
  1042. int rc;
  1043. rc = check_dev(dev);
  1044. if (rc < 0)
  1045. return rc;
  1046. if (0 != t->index)
  1047. return -EINVAL;
  1048. strcpy(t->name, "Tuner");
  1049. t->type = V4L2_TUNER_ANALOG_TV;
  1050. t->capability = V4L2_TUNER_CAP_NORM;
  1051. t->rangehigh = 0xffffffffUL;
  1052. t->signal = 0xffff; /* LOCKED */
  1053. return 0;
  1054. }
  1055. static int vidioc_s_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
  1056. {
  1057. struct cx231xx_fh *fh = priv;
  1058. struct cx231xx *dev = fh->dev;
  1059. int rc;
  1060. rc = check_dev(dev);
  1061. if (rc < 0)
  1062. return rc;
  1063. if (0 != t->index)
  1064. return -EINVAL;
  1065. #if 0
  1066. mutex_lock(&dev->lock);
  1067. call_all(dev, tuner, s_tuner, t);
  1068. mutex_unlock(&dev->lock);
  1069. #endif
  1070. return 0;
  1071. }
  1072. static int vidioc_g_frequency(struct file *file, void *priv,
  1073. struct v4l2_frequency *f)
  1074. {
  1075. struct cx231xx_fh *fh = priv;
  1076. struct cx231xx *dev = fh->dev;
  1077. mutex_lock(&dev->lock);
  1078. f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1079. f->frequency = dev->ctl_freq;
  1080. call_all(dev, tuner, g_frequency, f);
  1081. mutex_unlock(&dev->lock);
  1082. return 0;
  1083. }
  1084. static int vidioc_s_frequency(struct file *file, void *priv,
  1085. struct v4l2_frequency *f)
  1086. {
  1087. struct cx231xx_fh *fh = priv;
  1088. struct cx231xx *dev = fh->dev;
  1089. int rc;
  1090. rc = check_dev(dev);
  1091. if (rc < 0)
  1092. return rc;
  1093. if (0 != f->tuner)
  1094. return -EINVAL;
  1095. if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
  1096. return -EINVAL;
  1097. if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
  1098. return -EINVAL;
  1099. /* set pre channel change settings in DIF first */
  1100. rc = cx231xx_tuner_pre_channel_change(dev);
  1101. mutex_lock(&dev->lock);
  1102. dev->ctl_freq = f->frequency;
  1103. if (dev->tuner_type == TUNER_XC5000) {
  1104. if (dev->cx231xx_set_analog_freq != NULL)
  1105. dev->cx231xx_set_analog_freq(dev, f->frequency);
  1106. } else
  1107. call_all(dev, tuner, s_frequency, f);
  1108. mutex_unlock(&dev->lock);
  1109. /* set post channel change settings in DIF first */
  1110. rc = cx231xx_tuner_post_channel_change(dev);
  1111. cx231xx_info("Set New FREQUENCY to %d\n", f->frequency);
  1112. return rc;
  1113. }
  1114. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1115. /*
  1116. -R, --list-registers=type=<host/i2cdrv/i2caddr>,
  1117. 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>,
  1120. chip=<chip>,reg=<addr>,val=<val>
  1121. set the register [VIDIOC_DBG_S_REGISTER]
  1122. if type == host, then <chip> is the hosts chip ID (default 0)
  1123. if type == i2cdrv (default), then <chip> is the I2C driver name or ID
  1124. if type == i2caddr, then <chip> is the 7-bit I2C address
  1125. */
  1126. static int vidioc_g_register(struct file *file, void *priv,
  1127. struct v4l2_dbg_register *reg)
  1128. {
  1129. struct cx231xx_fh *fh = priv;
  1130. struct cx231xx *dev = fh->dev;
  1131. int ret = 0;
  1132. u8 value[4] = { 0, 0, 0, 0 };
  1133. u32 data = 0;
  1134. switch (reg->match.type) {
  1135. case V4L2_CHIP_MATCH_HOST:
  1136. switch (reg->match.addr) {
  1137. case 0: /* Cx231xx - internal registers */
  1138. ret = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER,
  1139. (u16)reg->reg, value, 4);
  1140. reg->val = value[0] | value[1] << 8 |
  1141. value[2] << 16 | value[3] << 24;
  1142. break;
  1143. case 1: /* Colibri - read byte */
  1144. ret = cx231xx_read_i2c_data(dev, Colibri_DEVICE_ADDRESS,
  1145. (u16)reg->reg, 2, &data, 1);
  1146. reg->val = le32_to_cpu(data & 0xff);
  1147. break;
  1148. case 14: /* Colibri - read dword */
  1149. ret = cx231xx_read_i2c_data(dev, Colibri_DEVICE_ADDRESS,
  1150. (u16)reg->reg, 2, &data, 4);
  1151. reg->val = le32_to_cpu(data);
  1152. break;
  1153. case 2: /* Hammerhead - read byte */
  1154. ret = cx231xx_read_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
  1155. (u16)reg->reg, 2, &data, 1);
  1156. reg->val = le32_to_cpu(data & 0xff);
  1157. break;
  1158. case 24: /* Hammerhead - read dword */
  1159. ret = cx231xx_read_i2c_data(dev, HAMMERHEAD_I2C_ADDRESS,
  1160. (u16)reg->reg, 2, &data, 4);
  1161. reg->val = le32_to_cpu(data);
  1162. break;
  1163. case 3: /* flatiron - read byte */
  1164. ret = cx231xx_read_i2c_data(dev,
  1165. Flatrion_DEVICE_ADDRESS,
  1166. (u16)reg->reg, 1,
  1167. &data, 1);
  1168. reg->val = le32_to_cpu(data & 0xff);
  1169. break;
  1170. case 34: /* flatiron - read dword */
  1171. ret =
  1172. cx231xx_read_i2c_data(dev, Flatrion_DEVICE_ADDRESS,
  1173. (u16)reg->reg, 1, &data, 4);
  1174. reg->val = le32_to_cpu(data);
  1175. break;
  1176. }
  1177. return ret < 0 ? ret : 0;
  1178. case V4L2_CHIP_MATCH_I2C_DRIVER:
  1179. call_all(dev, core, 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. call_all(dev, core, 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. call_all(dev, core, 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. call_all(dev, video, s_stream, 1);
  1300. mutex_unlock(&dev->lock);
  1301. return rc;
  1302. }
  1303. static int vidioc_streamoff(struct file *file, void *priv,
  1304. enum v4l2_buf_type type)
  1305. {
  1306. struct cx231xx_fh *fh = priv;
  1307. struct cx231xx *dev = fh->dev;
  1308. int rc;
  1309. rc = check_dev(dev);
  1310. if (rc < 0)
  1311. return rc;
  1312. if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
  1313. (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE))
  1314. return -EINVAL;
  1315. if (type != fh->type)
  1316. return -EINVAL;
  1317. mutex_lock(&dev->lock);
  1318. cx25840_call(dev, video, s_stream, 0);
  1319. videobuf_streamoff(&fh->vb_vidq);
  1320. res_free(fh);
  1321. mutex_unlock(&dev->lock);
  1322. return 0;
  1323. }
  1324. static int vidioc_querycap(struct file *file, void *priv,
  1325. struct v4l2_capability *cap)
  1326. {
  1327. struct cx231xx_fh *fh = priv;
  1328. struct cx231xx *dev = fh->dev;
  1329. strlcpy(cap->driver, "cx231xx", sizeof(cap->driver));
  1330. strlcpy(cap->card, cx231xx_boards[dev->model].name, sizeof(cap->card));
  1331. strlcpy(cap->bus_info, dev->v4l2_dev.name,
  1332. sizeof(cap->bus_info));
  1333. cap->version = CX231XX_VERSION_CODE;
  1334. cap->capabilities = V4L2_CAP_VBI_CAPTURE |
  1335. #if 0
  1336. V4L2_CAP_SLICED_VBI_CAPTURE |
  1337. #endif
  1338. V4L2_CAP_VIDEO_CAPTURE |
  1339. V4L2_CAP_AUDIO |
  1340. V4L2_CAP_READWRITE |
  1341. V4L2_CAP_STREAMING;
  1342. if (dev->tuner_type != TUNER_ABSENT)
  1343. cap->capabilities |= V4L2_CAP_TUNER;
  1344. return 0;
  1345. }
  1346. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  1347. struct v4l2_fmtdesc *f)
  1348. {
  1349. if (unlikely(f->index >= ARRAY_SIZE(format)))
  1350. return -EINVAL;
  1351. strlcpy(f->description, format[f->index].name, sizeof(f->description));
  1352. f->pixelformat = format[f->index].fourcc;
  1353. return 0;
  1354. }
  1355. /* Sliced VBI ioctls */
  1356. static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
  1357. struct v4l2_format *f)
  1358. {
  1359. struct cx231xx_fh *fh = priv;
  1360. struct cx231xx *dev = fh->dev;
  1361. int rc;
  1362. rc = check_dev(dev);
  1363. if (rc < 0)
  1364. return rc;
  1365. mutex_lock(&dev->lock);
  1366. f->fmt.sliced.service_set = 0;
  1367. call_all(dev, video, g_fmt, f);
  1368. if (f->fmt.sliced.service_set == 0)
  1369. rc = -EINVAL;
  1370. mutex_unlock(&dev->lock);
  1371. return rc;
  1372. }
  1373. static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
  1374. struct v4l2_format *f)
  1375. {
  1376. struct cx231xx_fh *fh = priv;
  1377. struct cx231xx *dev = fh->dev;
  1378. int rc;
  1379. rc = check_dev(dev);
  1380. if (rc < 0)
  1381. return rc;
  1382. mutex_lock(&dev->lock);
  1383. call_all(dev, video, g_fmt, f);
  1384. mutex_unlock(&dev->lock);
  1385. if (f->fmt.sliced.service_set == 0)
  1386. return -EINVAL;
  1387. return 0;
  1388. }
  1389. /* RAW VBI ioctls */
  1390. static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
  1391. struct v4l2_format *f)
  1392. {
  1393. struct cx231xx_fh *fh = priv;
  1394. struct cx231xx *dev = fh->dev;
  1395. f->fmt.vbi.sampling_rate = (dev->norm & V4L2_STD_625_50) ?
  1396. 35468950 : 28636363;
  1397. f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
  1398. f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  1399. f->fmt.vbi.offset = 64 * 4;
  1400. f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
  1401. PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
  1402. f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
  1403. PAL_VBI_LINES : NTSC_VBI_LINES;
  1404. f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
  1405. PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
  1406. f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
  1407. return 0;
  1408. }
  1409. static int vidioc_try_fmt_vbi_cap(struct file *file, void *priv,
  1410. struct v4l2_format *f)
  1411. {
  1412. struct cx231xx_fh *fh = priv;
  1413. struct cx231xx *dev = fh->dev;
  1414. if (dev->vbi_stream_on && !fh->stream_on) {
  1415. cx231xx_errdev("%s device in use by another fh\n", __func__);
  1416. return -EBUSY;
  1417. }
  1418. f->type = V4L2_BUF_TYPE_VBI_CAPTURE;
  1419. f->fmt.vbi.sampling_rate = (dev->norm & V4L2_STD_625_50) ?
  1420. 35468950 : 28636363;
  1421. f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
  1422. f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  1423. f->fmt.vbi.offset = 244;
  1424. f->fmt.vbi.flags = 0;
  1425. f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
  1426. PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
  1427. f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
  1428. PAL_VBI_LINES : NTSC_VBI_LINES;
  1429. f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
  1430. PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
  1431. f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
  1432. return 0;
  1433. }
  1434. static int vidioc_reqbufs(struct file *file, void *priv,
  1435. struct v4l2_requestbuffers *rb)
  1436. {
  1437. struct cx231xx_fh *fh = priv;
  1438. struct cx231xx *dev = fh->dev;
  1439. int rc;
  1440. rc = check_dev(dev);
  1441. if (rc < 0)
  1442. return rc;
  1443. return videobuf_reqbufs(&fh->vb_vidq, rb);
  1444. }
  1445. static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1446. {
  1447. struct cx231xx_fh *fh = priv;
  1448. struct cx231xx *dev = fh->dev;
  1449. int rc;
  1450. rc = check_dev(dev);
  1451. if (rc < 0)
  1452. return rc;
  1453. return videobuf_querybuf(&fh->vb_vidq, b);
  1454. }
  1455. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1456. {
  1457. struct cx231xx_fh *fh = priv;
  1458. struct cx231xx *dev = fh->dev;
  1459. int rc;
  1460. rc = check_dev(dev);
  1461. if (rc < 0)
  1462. return rc;
  1463. return videobuf_qbuf(&fh->vb_vidq, b);
  1464. }
  1465. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1466. {
  1467. struct cx231xx_fh *fh = priv;
  1468. struct cx231xx *dev = fh->dev;
  1469. int rc;
  1470. rc = check_dev(dev);
  1471. if (rc < 0)
  1472. return rc;
  1473. return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK);
  1474. }
  1475. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1476. static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
  1477. {
  1478. struct cx231xx_fh *fh = priv;
  1479. return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
  1480. }
  1481. #endif
  1482. /* ----------------------------------------------------------- */
  1483. /* RADIO ESPECIFIC IOCTLS */
  1484. /* ----------------------------------------------------------- */
  1485. static int radio_querycap(struct file *file, void *priv,
  1486. struct v4l2_capability *cap)
  1487. {
  1488. struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
  1489. strlcpy(cap->driver, "cx231xx", sizeof(cap->driver));
  1490. strlcpy(cap->card, cx231xx_boards[dev->model].name, sizeof(cap->card));
  1491. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  1492. cap->version = CX231XX_VERSION_CODE;
  1493. cap->capabilities = V4L2_CAP_TUNER;
  1494. return 0;
  1495. }
  1496. static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
  1497. {
  1498. struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
  1499. if (unlikely(t->index > 0))
  1500. return -EINVAL;
  1501. strcpy(t->name, "Radio");
  1502. t->type = V4L2_TUNER_RADIO;
  1503. mutex_lock(&dev->lock);
  1504. call_all(dev, tuner, s_tuner, t);
  1505. mutex_unlock(&dev->lock);
  1506. return 0;
  1507. }
  1508. static int radio_enum_input(struct file *file, void *priv, struct v4l2_input *i)
  1509. {
  1510. if (i->index != 0)
  1511. return -EINVAL;
  1512. strcpy(i->name, "Radio");
  1513. i->type = V4L2_INPUT_TYPE_TUNER;
  1514. return 0;
  1515. }
  1516. static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  1517. {
  1518. if (unlikely(a->index))
  1519. return -EINVAL;
  1520. strcpy(a->name, "Radio");
  1521. return 0;
  1522. }
  1523. static int radio_s_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
  1524. {
  1525. struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
  1526. if (0 != t->index)
  1527. return -EINVAL;
  1528. mutex_lock(&dev->lock);
  1529. call_all(dev, tuner, s_tuner, t);
  1530. mutex_unlock(&dev->lock);
  1531. return 0;
  1532. }
  1533. static int radio_s_audio(struct file *file, void *fh, struct v4l2_audio *a)
  1534. {
  1535. return 0;
  1536. }
  1537. static int radio_s_input(struct file *file, void *fh, unsigned int i)
  1538. {
  1539. return 0;
  1540. }
  1541. static int radio_queryctrl(struct file *file, void *priv,
  1542. struct v4l2_queryctrl *c)
  1543. {
  1544. int i;
  1545. if (c->id < V4L2_CID_BASE || c->id >= V4L2_CID_LASTP1)
  1546. return -EINVAL;
  1547. if (c->id == V4L2_CID_AUDIO_MUTE) {
  1548. for (i = 0; i < CX231XX_CTLS; i++)
  1549. if (cx231xx_ctls[i].v.id == c->id)
  1550. break;
  1551. *c = cx231xx_ctls[i].v;
  1552. } else
  1553. *c = no_ctl;
  1554. return 0;
  1555. }
  1556. /*
  1557. * cx231xx_v4l2_open()
  1558. * inits the device and starts isoc transfer
  1559. */
  1560. static int cx231xx_v4l2_open(struct file *filp)
  1561. {
  1562. int minor = video_devdata(filp)->minor;
  1563. int errCode = 0, radio = 0;
  1564. struct cx231xx *dev = NULL;
  1565. struct cx231xx_fh *fh;
  1566. enum v4l2_buf_type fh_type = 0;
  1567. dev = cx231xx_get_device(minor, &fh_type, &radio);
  1568. if (NULL == dev)
  1569. return -ENODEV;
  1570. mutex_lock(&dev->lock);
  1571. cx231xx_videodbg("open minor=%d type=%s users=%d\n",
  1572. minor, v4l2_type_names[fh_type], dev->users);
  1573. #if 0
  1574. errCode = cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
  1575. if (errCode < 0) {
  1576. cx231xx_errdev
  1577. ("Device locked on digital mode. Can't open analog\n");
  1578. mutex_unlock(&dev->lock);
  1579. return -EBUSY;
  1580. }
  1581. #endif
  1582. fh = kzalloc(sizeof(struct cx231xx_fh), GFP_KERNEL);
  1583. if (!fh) {
  1584. cx231xx_errdev("cx231xx-video.c: Out of memory?!\n");
  1585. mutex_unlock(&dev->lock);
  1586. return -ENOMEM;
  1587. }
  1588. fh->dev = dev;
  1589. fh->radio = radio;
  1590. fh->type = fh_type;
  1591. filp->private_data = fh;
  1592. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
  1593. dev->width = norm_maxw(dev);
  1594. dev->height = norm_maxh(dev);
  1595. dev->hscale = 0;
  1596. dev->vscale = 0;
  1597. /* Power up in Analog TV mode */
  1598. cx231xx_set_power_mode(dev, POLARIS_AVMODE_ANALOGT_TV);
  1599. #if 0
  1600. cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
  1601. #endif
  1602. cx231xx_resolution_set(dev);
  1603. /* set video alternate setting */
  1604. cx231xx_set_video_alternate(dev);
  1605. /* Needed, since GPIO might have disabled power of
  1606. some i2c device */
  1607. cx231xx_config_i2c(dev);
  1608. /* device needs to be initialized before isoc transfer */
  1609. dev->video_input = dev->video_input > 2 ? 2 : dev->video_input;
  1610. video_mux(dev, dev->video_input);
  1611. }
  1612. if (fh->radio) {
  1613. cx231xx_videodbg("video_open: setting radio device\n");
  1614. /* cx231xx_start_radio(dev); */
  1615. call_all(dev, tuner, s_radio);
  1616. }
  1617. dev->users++;
  1618. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1619. videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_video_qops,
  1620. NULL, &dev->video_mode.slock,
  1621. fh->type, V4L2_FIELD_INTERLACED,
  1622. sizeof(struct cx231xx_buffer), fh);
  1623. if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
  1624. /* Set the required alternate setting VBI interface works in
  1625. Bulk mode only */
  1626. cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
  1627. videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_vbi_qops,
  1628. NULL, &dev->vbi_mode.slock,
  1629. fh->type, V4L2_FIELD_SEQ_TB,
  1630. sizeof(struct cx231xx_buffer), fh);
  1631. }
  1632. mutex_unlock(&dev->lock);
  1633. return errCode;
  1634. }
  1635. /*
  1636. * cx231xx_realease_resources()
  1637. * unregisters the v4l2,i2c and usb devices
  1638. * called when the device gets disconected or at module unload
  1639. */
  1640. void cx231xx_release_analog_resources(struct cx231xx *dev)
  1641. {
  1642. /*FIXME: I2C IR should be disconnected */
  1643. if (dev->radio_dev) {
  1644. if (-1 != dev->radio_dev->minor)
  1645. video_unregister_device(dev->radio_dev);
  1646. else
  1647. video_device_release(dev->radio_dev);
  1648. dev->radio_dev = NULL;
  1649. }
  1650. if (dev->vbi_dev) {
  1651. cx231xx_info("V4L2 device /dev/vbi%d deregistered\n",
  1652. dev->vbi_dev->num);
  1653. if (-1 != dev->vbi_dev->minor)
  1654. video_unregister_device(dev->vbi_dev);
  1655. else
  1656. video_device_release(dev->vbi_dev);
  1657. dev->vbi_dev = NULL;
  1658. }
  1659. if (dev->vdev) {
  1660. cx231xx_info("V4L2 device /dev/video%d deregistered\n",
  1661. dev->vdev->num);
  1662. if (-1 != dev->vdev->minor)
  1663. video_unregister_device(dev->vdev);
  1664. else
  1665. video_device_release(dev->vdev);
  1666. dev->vdev = NULL;
  1667. }
  1668. }
  1669. /*
  1670. * cx231xx_v4l2_close()
  1671. * stops streaming and deallocates all resources allocated by the v4l2
  1672. * calls and ioctls
  1673. */
  1674. static int cx231xx_v4l2_close(struct file *filp)
  1675. {
  1676. struct cx231xx_fh *fh = filp->private_data;
  1677. struct cx231xx *dev = fh->dev;
  1678. cx231xx_videodbg("users=%d\n", dev->users);
  1679. mutex_lock(&dev->lock);
  1680. if (res_check(fh))
  1681. res_free(fh);
  1682. if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
  1683. videobuf_stop(&fh->vb_vidq);
  1684. videobuf_mmap_free(&fh->vb_vidq);
  1685. /* the device is already disconnect,
  1686. free the remaining resources */
  1687. if (dev->state & DEV_DISCONNECTED) {
  1688. cx231xx_release_resources(dev);
  1689. mutex_unlock(&dev->lock);
  1690. kfree(dev);
  1691. return 0;
  1692. }
  1693. /* do this before setting alternate! */
  1694. cx231xx_uninit_vbi_isoc(dev);
  1695. /* set alternate 0 */
  1696. if (!dev->vbi_or_sliced_cc_mode)
  1697. cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
  1698. else
  1699. cx231xx_set_alt_setting(dev, INDEX_HANC, 0);
  1700. kfree(fh);
  1701. dev->users--;
  1702. wake_up_interruptible_nr(&dev->open, 1);
  1703. mutex_unlock(&dev->lock);
  1704. return 0;
  1705. }
  1706. if (dev->users == 1) {
  1707. videobuf_stop(&fh->vb_vidq);
  1708. videobuf_mmap_free(&fh->vb_vidq);
  1709. /* the device is already disconnect,
  1710. free the remaining resources */
  1711. if (dev->state & DEV_DISCONNECTED) {
  1712. cx231xx_release_resources(dev);
  1713. mutex_unlock(&dev->lock);
  1714. kfree(dev);
  1715. return 0;
  1716. }
  1717. /* Save some power by putting tuner to sleep */
  1718. call_all(dev, core, s_standby, 0);
  1719. /* do this before setting alternate! */
  1720. cx231xx_uninit_isoc(dev);
  1721. cx231xx_set_mode(dev, CX231XX_SUSPEND);
  1722. /* set alternate 0 */
  1723. cx231xx_set_alt_setting(dev, INDEX_VIDEO, 0);
  1724. }
  1725. kfree(fh);
  1726. dev->users--;
  1727. wake_up_interruptible_nr(&dev->open, 1);
  1728. mutex_unlock(&dev->lock);
  1729. return 0;
  1730. }
  1731. /*
  1732. * cx231xx_v4l2_read()
  1733. * will allocate buffers when called for the first time
  1734. */
  1735. static ssize_t
  1736. cx231xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
  1737. loff_t *pos)
  1738. {
  1739. struct cx231xx_fh *fh = filp->private_data;
  1740. struct cx231xx *dev = fh->dev;
  1741. int rc;
  1742. rc = check_dev(dev);
  1743. if (rc < 0)
  1744. return rc;
  1745. if ((fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
  1746. (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)) {
  1747. mutex_lock(&dev->lock);
  1748. rc = res_get(fh);
  1749. mutex_unlock(&dev->lock);
  1750. if (unlikely(rc < 0))
  1751. return rc;
  1752. return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
  1753. filp->f_flags & O_NONBLOCK);
  1754. }
  1755. return 0;
  1756. }
  1757. /*
  1758. * cx231xx_v4l2_poll()
  1759. * will allocate buffers when called for the first time
  1760. */
  1761. static unsigned int cx231xx_v4l2_poll(struct file *filp, poll_table * wait)
  1762. {
  1763. struct cx231xx_fh *fh = filp->private_data;
  1764. struct cx231xx *dev = fh->dev;
  1765. int rc;
  1766. rc = check_dev(dev);
  1767. if (rc < 0)
  1768. return rc;
  1769. mutex_lock(&dev->lock);
  1770. rc = res_get(fh);
  1771. mutex_unlock(&dev->lock);
  1772. if (unlikely(rc < 0))
  1773. return POLLERR;
  1774. if ((V4L2_BUF_TYPE_VIDEO_CAPTURE == fh->type) ||
  1775. (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type))
  1776. return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
  1777. else
  1778. return POLLERR;
  1779. }
  1780. /*
  1781. * cx231xx_v4l2_mmap()
  1782. */
  1783. static int cx231xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
  1784. {
  1785. struct cx231xx_fh *fh = filp->private_data;
  1786. struct cx231xx *dev = fh->dev;
  1787. int rc;
  1788. rc = check_dev(dev);
  1789. if (rc < 0)
  1790. return rc;
  1791. mutex_lock(&dev->lock);
  1792. rc = res_get(fh);
  1793. mutex_unlock(&dev->lock);
  1794. if (unlikely(rc < 0))
  1795. return rc;
  1796. rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
  1797. cx231xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
  1798. (unsigned long)vma->vm_start,
  1799. (unsigned long)vma->vm_end -
  1800. (unsigned long)vma->vm_start, rc);
  1801. return rc;
  1802. }
  1803. static const struct v4l2_file_operations cx231xx_v4l_fops = {
  1804. .owner = THIS_MODULE,
  1805. .open = cx231xx_v4l2_open,
  1806. .release = cx231xx_v4l2_close,
  1807. .read = cx231xx_v4l2_read,
  1808. .poll = cx231xx_v4l2_poll,
  1809. .mmap = cx231xx_v4l2_mmap,
  1810. .ioctl = video_ioctl2,
  1811. };
  1812. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1813. .vidioc_querycap = vidioc_querycap,
  1814. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1815. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1816. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1817. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1818. .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
  1819. .vidioc_try_fmt_vbi_cap = vidioc_try_fmt_vbi_cap,
  1820. .vidioc_s_fmt_vbi_cap = vidioc_try_fmt_vbi_cap,
  1821. .vidioc_g_audio = vidioc_g_audio,
  1822. .vidioc_s_audio = vidioc_s_audio,
  1823. .vidioc_cropcap = vidioc_cropcap,
  1824. .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
  1825. .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
  1826. .vidioc_reqbufs = vidioc_reqbufs,
  1827. .vidioc_querybuf = vidioc_querybuf,
  1828. .vidioc_qbuf = vidioc_qbuf,
  1829. .vidioc_dqbuf = vidioc_dqbuf,
  1830. .vidioc_s_std = vidioc_s_std,
  1831. .vidioc_g_std = vidioc_g_std,
  1832. .vidioc_enum_input = vidioc_enum_input,
  1833. .vidioc_g_input = vidioc_g_input,
  1834. .vidioc_s_input = vidioc_s_input,
  1835. .vidioc_queryctrl = vidioc_queryctrl,
  1836. .vidioc_g_ctrl = vidioc_g_ctrl,
  1837. .vidioc_s_ctrl = vidioc_s_ctrl,
  1838. .vidioc_streamon = vidioc_streamon,
  1839. .vidioc_streamoff = vidioc_streamoff,
  1840. .vidioc_g_tuner = vidioc_g_tuner,
  1841. .vidioc_s_tuner = vidioc_s_tuner,
  1842. .vidioc_g_frequency = vidioc_g_frequency,
  1843. .vidioc_s_frequency = vidioc_s_frequency,
  1844. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1845. .vidioc_g_register = vidioc_g_register,
  1846. .vidioc_s_register = vidioc_s_register,
  1847. #endif
  1848. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1849. .vidiocgmbuf = vidiocgmbuf,
  1850. #endif
  1851. };
  1852. static struct video_device cx231xx_vbi_template;
  1853. static const struct video_device cx231xx_video_template = {
  1854. .fops = &cx231xx_v4l_fops,
  1855. .release = video_device_release,
  1856. .ioctl_ops = &video_ioctl_ops,
  1857. .minor = -1,
  1858. .tvnorms = V4L2_STD_ALL,
  1859. .current_norm = V4L2_STD_PAL,
  1860. };
  1861. static const struct v4l2_file_operations radio_fops = {
  1862. .owner = THIS_MODULE,
  1863. .open = cx231xx_v4l2_open,
  1864. .release = cx231xx_v4l2_close,
  1865. .ioctl = video_ioctl2,
  1866. };
  1867. static const struct v4l2_ioctl_ops radio_ioctl_ops = {
  1868. .vidioc_querycap = radio_querycap,
  1869. .vidioc_g_tuner = radio_g_tuner,
  1870. .vidioc_enum_input = radio_enum_input,
  1871. .vidioc_g_audio = radio_g_audio,
  1872. .vidioc_s_tuner = radio_s_tuner,
  1873. .vidioc_s_audio = radio_s_audio,
  1874. .vidioc_s_input = radio_s_input,
  1875. .vidioc_queryctrl = radio_queryctrl,
  1876. .vidioc_g_ctrl = vidioc_g_ctrl,
  1877. .vidioc_s_ctrl = vidioc_s_ctrl,
  1878. .vidioc_g_frequency = vidioc_g_frequency,
  1879. .vidioc_s_frequency = vidioc_s_frequency,
  1880. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1881. .vidioc_g_register = vidioc_g_register,
  1882. .vidioc_s_register = vidioc_s_register,
  1883. #endif
  1884. };
  1885. static struct video_device cx231xx_radio_template = {
  1886. .name = "cx231xx-radio",
  1887. .fops = &radio_fops,
  1888. .ioctl_ops = &radio_ioctl_ops,
  1889. .minor = -1,
  1890. };
  1891. /******************************** usb interface ******************************/
  1892. static struct video_device *cx231xx_vdev_init(struct cx231xx *dev,
  1893. const struct video_device
  1894. *template, const char *type_name)
  1895. {
  1896. struct video_device *vfd;
  1897. vfd = video_device_alloc();
  1898. if (NULL == vfd)
  1899. return NULL;
  1900. *vfd = *template;
  1901. vfd->minor = -1;
  1902. vfd->v4l2_dev = &dev->v4l2_dev;
  1903. vfd->release = video_device_release;
  1904. vfd->debug = video_debug;
  1905. snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name);
  1906. return vfd;
  1907. }
  1908. int cx231xx_register_analog_devices(struct cx231xx *dev)
  1909. {
  1910. int ret;
  1911. cx231xx_info("%s()\n", __func__);
  1912. cx231xx_info("%s: v4l2 driver version %d.%d.%d\n",
  1913. dev->name,
  1914. (CX231XX_VERSION_CODE >> 16) & 0xff,
  1915. (CX231XX_VERSION_CODE >> 8) & 0xff,
  1916. CX231XX_VERSION_CODE & 0xff);
  1917. /* set default norm */
  1918. /*dev->norm = cx231xx_video_template.current_norm; */
  1919. dev->width = norm_maxw(dev);
  1920. dev->height = norm_maxh(dev);
  1921. dev->interlaced = 0;
  1922. dev->hscale = 0;
  1923. dev->vscale = 0;
  1924. /* Analog specific initialization */
  1925. dev->format = &format[0];
  1926. /* video_mux(dev, dev->video_input); */
  1927. /* Audio defaults */
  1928. dev->mute = 1;
  1929. dev->volume = 0x1f;
  1930. /* enable vbi capturing */
  1931. /* write code here... */
  1932. /* allocate and fill video video_device struct */
  1933. dev->vdev = cx231xx_vdev_init(dev, &cx231xx_video_template, "video");
  1934. if (!dev->vdev) {
  1935. cx231xx_errdev("cannot allocate video_device.\n");
  1936. return -ENODEV;
  1937. }
  1938. /* register v4l2 video video_device */
  1939. ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
  1940. video_nr[dev->devno]);
  1941. if (ret) {
  1942. cx231xx_errdev("unable to register video device (error=%i).\n",
  1943. ret);
  1944. return ret;
  1945. }
  1946. cx231xx_info("%s/0: registered device video%d [v4l2]\n",
  1947. dev->name, dev->vdev->num);
  1948. /* Initialize VBI template */
  1949. memcpy(&cx231xx_vbi_template, &cx231xx_video_template,
  1950. sizeof(cx231xx_vbi_template));
  1951. strcpy(cx231xx_vbi_template.name, "cx231xx-vbi");
  1952. /* Allocate and fill vbi video_device struct */
  1953. dev->vbi_dev = cx231xx_vdev_init(dev, &cx231xx_vbi_template, "vbi");
  1954. /* register v4l2 vbi video_device */
  1955. ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
  1956. vbi_nr[dev->devno]);
  1957. if (ret < 0) {
  1958. cx231xx_errdev("unable to register vbi device\n");
  1959. return ret;
  1960. }
  1961. cx231xx_info("%s/0: registered device vbi%d\n",
  1962. dev->name, dev->vbi_dev->num);
  1963. if (cx231xx_boards[dev->model].radio.type == CX231XX_RADIO) {
  1964. dev->radio_dev = cx231xx_vdev_init(dev, &cx231xx_radio_template,
  1965. "radio");
  1966. if (!dev->radio_dev) {
  1967. cx231xx_errdev("cannot allocate video_device.\n");
  1968. return -ENODEV;
  1969. }
  1970. ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
  1971. radio_nr[dev->devno]);
  1972. if (ret < 0) {
  1973. cx231xx_errdev("can't register radio device\n");
  1974. return ret;
  1975. }
  1976. cx231xx_info("Registered radio device as /dev/radio%d\n",
  1977. dev->radio_dev->num);
  1978. }
  1979. cx231xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
  1980. dev->vdev->num, dev->vbi_dev->num);
  1981. return 0;
  1982. }