cx231xx-video.c 59 KB

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