cx231xx-video.c 57 KB

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