cx231xx-video.c 56 KB

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