cx231xx-video.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238
  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. BUG_ON(in_interrupt());
  611. /* We used to wait for the buffer to finish here, but this didn't work
  612. because, as we were keeping the state as VIDEOBUF_QUEUED,
  613. videobuf_queue_cancel marked it as finished for us.
  614. (Also, it could wedge forever if the hardware was misconfigured.)
  615. This should be safe; by the time we get here, the buffer isn't
  616. queued anymore. If we ever start marking the buffers as
  617. VIDEOBUF_ACTIVE, it won't be, though.
  618. */
  619. spin_lock_irqsave(&dev->video_mode.slock, flags);
  620. if (dev->USE_ISO) {
  621. if (dev->video_mode.isoc_ctl.buf == buf)
  622. dev->video_mode.isoc_ctl.buf = NULL;
  623. } else {
  624. if (dev->video_mode.bulk_ctl.buf == buf)
  625. dev->video_mode.bulk_ctl.buf = NULL;
  626. }
  627. spin_unlock_irqrestore(&dev->video_mode.slock, flags);
  628. videobuf_vmalloc_free(&buf->vb);
  629. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  630. }
  631. static int
  632. buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
  633. enum v4l2_field field)
  634. {
  635. struct cx231xx_fh *fh = vq->priv_data;
  636. struct cx231xx_buffer *buf =
  637. container_of(vb, struct cx231xx_buffer, vb);
  638. struct cx231xx *dev = fh->dev;
  639. int rc = 0, urb_init = 0;
  640. /* The only currently supported format is 16 bits/pixel */
  641. buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
  642. + 7) >> 3;
  643. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  644. return -EINVAL;
  645. buf->vb.width = dev->width;
  646. buf->vb.height = dev->height;
  647. buf->vb.field = field;
  648. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  649. rc = videobuf_iolock(vq, &buf->vb, NULL);
  650. if (rc < 0)
  651. goto fail;
  652. }
  653. if (dev->USE_ISO) {
  654. if (!dev->video_mode.isoc_ctl.num_bufs)
  655. urb_init = 1;
  656. } else {
  657. if (!dev->video_mode.bulk_ctl.num_bufs)
  658. urb_init = 1;
  659. }
  660. dev_dbg(dev->dev,
  661. "urb_init=%d dev->video_mode.max_pkt_size=%d\n",
  662. urb_init, dev->video_mode.max_pkt_size);
  663. if (urb_init) {
  664. dev->mode_tv = 0;
  665. if (dev->USE_ISO)
  666. rc = cx231xx_init_isoc(dev, CX231XX_NUM_PACKETS,
  667. CX231XX_NUM_BUFS,
  668. dev->video_mode.max_pkt_size,
  669. cx231xx_isoc_copy);
  670. else
  671. rc = cx231xx_init_bulk(dev, CX231XX_NUM_PACKETS,
  672. CX231XX_NUM_BUFS,
  673. dev->video_mode.max_pkt_size,
  674. cx231xx_bulk_copy);
  675. if (rc < 0)
  676. goto fail;
  677. }
  678. buf->vb.state = VIDEOBUF_PREPARED;
  679. return 0;
  680. fail:
  681. free_buffer(vq, buf);
  682. return rc;
  683. }
  684. static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  685. {
  686. struct cx231xx_buffer *buf =
  687. container_of(vb, struct cx231xx_buffer, vb);
  688. struct cx231xx_fh *fh = vq->priv_data;
  689. struct cx231xx *dev = fh->dev;
  690. struct cx231xx_dmaqueue *vidq = &dev->video_mode.vidq;
  691. buf->vb.state = VIDEOBUF_QUEUED;
  692. list_add_tail(&buf->vb.queue, &vidq->active);
  693. }
  694. static void buffer_release(struct videobuf_queue *vq,
  695. struct videobuf_buffer *vb)
  696. {
  697. struct cx231xx_buffer *buf =
  698. container_of(vb, struct cx231xx_buffer, vb);
  699. struct cx231xx_fh *fh = vq->priv_data;
  700. struct cx231xx *dev = (struct cx231xx *)fh->dev;
  701. cx231xx_isocdbg("cx231xx: called buffer_release\n");
  702. free_buffer(vq, buf);
  703. }
  704. static struct videobuf_queue_ops cx231xx_video_qops = {
  705. .buf_setup = buffer_setup,
  706. .buf_prepare = buffer_prepare,
  707. .buf_queue = buffer_queue,
  708. .buf_release = buffer_release,
  709. };
  710. /********************* v4l2 interface **************************************/
  711. void video_mux(struct cx231xx *dev, int index)
  712. {
  713. dev->video_input = index;
  714. dev->ctl_ainput = INPUT(index)->amux;
  715. cx231xx_set_video_input_mux(dev, index);
  716. cx25840_call(dev, video, s_routing, INPUT(index)->vmux, 0, 0);
  717. cx231xx_set_audio_input(dev, dev->ctl_ainput);
  718. dev_dbg(dev->dev, "video_mux : %d\n", index);
  719. /* do mode control overrides if required */
  720. cx231xx_do_mode_ctrl_overrides(dev);
  721. }
  722. /* Usage lock check functions */
  723. static int res_get(struct cx231xx_fh *fh)
  724. {
  725. struct cx231xx *dev = fh->dev;
  726. int rc = 0;
  727. /* This instance already has stream_on */
  728. if (fh->stream_on)
  729. return rc;
  730. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  731. if (dev->stream_on)
  732. return -EBUSY;
  733. dev->stream_on = 1;
  734. } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
  735. if (dev->vbi_stream_on)
  736. return -EBUSY;
  737. dev->vbi_stream_on = 1;
  738. } else
  739. return -EINVAL;
  740. fh->stream_on = 1;
  741. return rc;
  742. }
  743. static int res_check(struct cx231xx_fh *fh)
  744. {
  745. return fh->stream_on;
  746. }
  747. static void res_free(struct cx231xx_fh *fh)
  748. {
  749. struct cx231xx *dev = fh->dev;
  750. fh->stream_on = 0;
  751. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  752. dev->stream_on = 0;
  753. if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
  754. dev->vbi_stream_on = 0;
  755. }
  756. static int check_dev(struct cx231xx *dev)
  757. {
  758. if (dev->state & DEV_DISCONNECTED) {
  759. dev_err(dev->dev, "v4l2 ioctl: device not present\n");
  760. return -ENODEV;
  761. }
  762. return 0;
  763. }
  764. /* ------------------------------------------------------------------
  765. IOCTL vidioc handling
  766. ------------------------------------------------------------------*/
  767. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  768. struct v4l2_format *f)
  769. {
  770. struct cx231xx_fh *fh = priv;
  771. struct cx231xx *dev = fh->dev;
  772. f->fmt.pix.width = dev->width;
  773. f->fmt.pix.height = dev->height;
  774. f->fmt.pix.pixelformat = dev->format->fourcc;
  775. f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
  776. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
  777. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  778. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  779. return 0;
  780. }
  781. static struct cx231xx_fmt *format_by_fourcc(unsigned int fourcc)
  782. {
  783. unsigned int i;
  784. for (i = 0; i < ARRAY_SIZE(format); i++)
  785. if (format[i].fourcc == fourcc)
  786. return &format[i];
  787. return NULL;
  788. }
  789. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  790. struct v4l2_format *f)
  791. {
  792. struct cx231xx_fh *fh = priv;
  793. struct cx231xx *dev = fh->dev;
  794. unsigned int width = f->fmt.pix.width;
  795. unsigned int height = f->fmt.pix.height;
  796. unsigned int maxw = norm_maxw(dev);
  797. unsigned int maxh = norm_maxh(dev);
  798. struct cx231xx_fmt *fmt;
  799. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  800. if (!fmt) {
  801. cx231xx_videodbg("Fourcc format (%08x) invalid.\n",
  802. f->fmt.pix.pixelformat);
  803. return -EINVAL;
  804. }
  805. /* width must even because of the YUYV format
  806. height must be even because of interlacing */
  807. v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh, 1, 0);
  808. f->fmt.pix.width = width;
  809. f->fmt.pix.height = height;
  810. f->fmt.pix.pixelformat = fmt->fourcc;
  811. f->fmt.pix.bytesperline = (width * fmt->depth + 7) >> 3;
  812. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
  813. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  814. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  815. return 0;
  816. }
  817. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  818. struct v4l2_format *f)
  819. {
  820. struct cx231xx_fh *fh = priv;
  821. struct cx231xx *dev = fh->dev;
  822. int rc;
  823. struct cx231xx_fmt *fmt;
  824. struct v4l2_subdev_format format = {
  825. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  826. };
  827. rc = check_dev(dev);
  828. if (rc < 0)
  829. return rc;
  830. vidioc_try_fmt_vid_cap(file, priv, f);
  831. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  832. if (!fmt)
  833. return -EINVAL;
  834. if (videobuf_queue_is_busy(&fh->vb_vidq)) {
  835. dev_err(dev->dev, "%s: queue busy\n", __func__);
  836. return -EBUSY;
  837. }
  838. if (dev->stream_on && !fh->stream_on) {
  839. dev_err(dev->dev,
  840. "%s: device in use by another fh\n", __func__);
  841. return -EBUSY;
  842. }
  843. /* set new image size */
  844. dev->width = f->fmt.pix.width;
  845. dev->height = f->fmt.pix.height;
  846. dev->format = fmt;
  847. v4l2_fill_mbus_format(&format.format, &f->fmt.pix, MEDIA_BUS_FMT_FIXED);
  848. call_all(dev, pad, set_fmt, NULL, &format);
  849. v4l2_fill_pix_format(&f->fmt.pix, &format.format);
  850. return rc;
  851. }
  852. static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
  853. {
  854. struct cx231xx_fh *fh = priv;
  855. struct cx231xx *dev = fh->dev;
  856. *id = dev->norm;
  857. return 0;
  858. }
  859. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
  860. {
  861. struct cx231xx_fh *fh = priv;
  862. struct cx231xx *dev = fh->dev;
  863. struct v4l2_subdev_format format = {
  864. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  865. };
  866. int rc;
  867. rc = check_dev(dev);
  868. if (rc < 0)
  869. return rc;
  870. if (dev->norm == norm)
  871. return 0;
  872. if (videobuf_queue_is_busy(&fh->vb_vidq))
  873. return -EBUSY;
  874. dev->norm = norm;
  875. /* Adjusts width/height, if needed */
  876. dev->width = 720;
  877. dev->height = (dev->norm & V4L2_STD_625_50) ? 576 : 480;
  878. call_all(dev, video, s_std, dev->norm);
  879. /* We need to reset basic properties in the decoder related to
  880. resolution (since a standard change effects things like the number
  881. of lines in VACT, etc) */
  882. format.format.code = MEDIA_BUS_FMT_FIXED;
  883. format.format.width = dev->width;
  884. format.format.height = dev->height;
  885. call_all(dev, pad, set_fmt, NULL, &format);
  886. /* do mode control overrides */
  887. cx231xx_do_mode_ctrl_overrides(dev);
  888. return 0;
  889. }
  890. static const char *iname[] = {
  891. [CX231XX_VMUX_COMPOSITE1] = "Composite1",
  892. [CX231XX_VMUX_SVIDEO] = "S-Video",
  893. [CX231XX_VMUX_TELEVISION] = "Television",
  894. [CX231XX_VMUX_CABLE] = "Cable TV",
  895. [CX231XX_VMUX_DVB] = "DVB",
  896. [CX231XX_VMUX_DEBUG] = "for debug only",
  897. };
  898. int cx231xx_enum_input(struct file *file, void *priv,
  899. struct v4l2_input *i)
  900. {
  901. struct cx231xx_fh *fh = priv;
  902. struct cx231xx *dev = fh->dev;
  903. u32 gen_stat;
  904. unsigned int ret, n;
  905. n = i->index;
  906. if (n >= MAX_CX231XX_INPUT)
  907. return -EINVAL;
  908. if (0 == INPUT(n)->type)
  909. return -EINVAL;
  910. i->index = n;
  911. i->type = V4L2_INPUT_TYPE_CAMERA;
  912. strcpy(i->name, iname[INPUT(n)->type]);
  913. if ((CX231XX_VMUX_TELEVISION == INPUT(n)->type) ||
  914. (CX231XX_VMUX_CABLE == INPUT(n)->type))
  915. i->type = V4L2_INPUT_TYPE_TUNER;
  916. i->std = dev->vdev.tvnorms;
  917. /* If they are asking about the active input, read signal status */
  918. if (n == dev->video_input) {
  919. ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
  920. GEN_STAT, 2, &gen_stat, 4);
  921. if (ret > 0) {
  922. if ((gen_stat & FLD_VPRES) == 0x00)
  923. i->status |= V4L2_IN_ST_NO_SIGNAL;
  924. if ((gen_stat & FLD_HLOCK) == 0x00)
  925. i->status |= V4L2_IN_ST_NO_H_LOCK;
  926. }
  927. }
  928. return 0;
  929. }
  930. int cx231xx_g_input(struct file *file, void *priv, unsigned int *i)
  931. {
  932. struct cx231xx_fh *fh = priv;
  933. struct cx231xx *dev = fh->dev;
  934. *i = dev->video_input;
  935. return 0;
  936. }
  937. int cx231xx_s_input(struct file *file, void *priv, unsigned int i)
  938. {
  939. struct cx231xx_fh *fh = priv;
  940. struct cx231xx *dev = fh->dev;
  941. int rc;
  942. dev->mode_tv = 0;
  943. rc = check_dev(dev);
  944. if (rc < 0)
  945. return rc;
  946. if (i >= MAX_CX231XX_INPUT)
  947. return -EINVAL;
  948. if (0 == INPUT(i)->type)
  949. return -EINVAL;
  950. video_mux(dev, i);
  951. if (INPUT(i)->type == CX231XX_VMUX_TELEVISION ||
  952. INPUT(i)->type == CX231XX_VMUX_CABLE) {
  953. /* There's a tuner, so reset the standard and put it on the
  954. last known frequency (since it was probably powered down
  955. until now */
  956. call_all(dev, video, s_std, dev->norm);
  957. }
  958. return 0;
  959. }
  960. int cx231xx_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
  961. {
  962. struct cx231xx_fh *fh = priv;
  963. struct cx231xx *dev = fh->dev;
  964. int rc;
  965. rc = check_dev(dev);
  966. if (rc < 0)
  967. return rc;
  968. if (0 != t->index)
  969. return -EINVAL;
  970. strcpy(t->name, "Tuner");
  971. t->type = V4L2_TUNER_ANALOG_TV;
  972. t->capability = V4L2_TUNER_CAP_NORM;
  973. t->rangehigh = 0xffffffffUL;
  974. t->signal = 0xffff; /* LOCKED */
  975. call_all(dev, tuner, g_tuner, t);
  976. return 0;
  977. }
  978. int cx231xx_s_tuner(struct file *file, void *priv, const struct v4l2_tuner *t)
  979. {
  980. struct cx231xx_fh *fh = priv;
  981. struct cx231xx *dev = fh->dev;
  982. int rc;
  983. rc = check_dev(dev);
  984. if (rc < 0)
  985. return rc;
  986. if (0 != t->index)
  987. return -EINVAL;
  988. #if 0
  989. call_all(dev, tuner, s_tuner, t);
  990. #endif
  991. return 0;
  992. }
  993. int cx231xx_g_frequency(struct file *file, void *priv,
  994. struct v4l2_frequency *f)
  995. {
  996. struct cx231xx_fh *fh = priv;
  997. struct cx231xx *dev = fh->dev;
  998. if (f->tuner)
  999. return -EINVAL;
  1000. f->frequency = dev->ctl_freq;
  1001. return 0;
  1002. }
  1003. int cx231xx_s_frequency(struct file *file, void *priv,
  1004. const struct v4l2_frequency *f)
  1005. {
  1006. struct cx231xx_fh *fh = priv;
  1007. struct cx231xx *dev = fh->dev;
  1008. struct v4l2_frequency new_freq = *f;
  1009. int rc;
  1010. u32 if_frequency = 5400000;
  1011. dev_dbg(dev->dev,
  1012. "Enter vidioc_s_frequency()f->frequency=%d;f->type=%d\n",
  1013. f->frequency, f->type);
  1014. rc = check_dev(dev);
  1015. if (rc < 0)
  1016. return rc;
  1017. if (0 != f->tuner)
  1018. return -EINVAL;
  1019. /* set pre channel change settings in DIF first */
  1020. rc = cx231xx_tuner_pre_channel_change(dev);
  1021. call_all(dev, tuner, s_frequency, f);
  1022. call_all(dev, tuner, g_frequency, &new_freq);
  1023. dev->ctl_freq = new_freq.frequency;
  1024. /* set post channel change settings in DIF first */
  1025. rc = cx231xx_tuner_post_channel_change(dev);
  1026. if (dev->tuner_type == TUNER_NXP_TDA18271) {
  1027. if (dev->norm & (V4L2_STD_MN | V4L2_STD_NTSC_443))
  1028. if_frequency = 5400000; /*5.4MHz */
  1029. else if (dev->norm & V4L2_STD_B)
  1030. if_frequency = 6000000; /*6.0MHz */
  1031. else if (dev->norm & (V4L2_STD_PAL_DK | V4L2_STD_SECAM_DK))
  1032. if_frequency = 6900000; /*6.9MHz */
  1033. else if (dev->norm & V4L2_STD_GH)
  1034. if_frequency = 7100000; /*7.1MHz */
  1035. else if (dev->norm & V4L2_STD_PAL_I)
  1036. if_frequency = 7250000; /*7.25MHz */
  1037. else if (dev->norm & V4L2_STD_SECAM_L)
  1038. if_frequency = 6900000; /*6.9MHz */
  1039. else if (dev->norm & V4L2_STD_SECAM_LC)
  1040. if_frequency = 1250000; /*1.25MHz */
  1041. dev_dbg(dev->dev,
  1042. "if_frequency is set to %d\n", if_frequency);
  1043. cx231xx_set_Colibri_For_LowIF(dev, if_frequency, 1, 1);
  1044. update_HH_register_after_set_DIF(dev);
  1045. }
  1046. dev_dbg(dev->dev, "Set New FREQUENCY to %d\n", f->frequency);
  1047. return rc;
  1048. }
  1049. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1050. int cx231xx_g_chip_info(struct file *file, void *fh,
  1051. struct v4l2_dbg_chip_info *chip)
  1052. {
  1053. switch (chip->match.addr) {
  1054. case 0: /* Cx231xx - internal registers */
  1055. return 0;
  1056. case 1: /* AFE - read byte */
  1057. strlcpy(chip->name, "AFE (byte)", sizeof(chip->name));
  1058. return 0;
  1059. case 2: /* Video Block - read byte */
  1060. strlcpy(chip->name, "Video (byte)", sizeof(chip->name));
  1061. return 0;
  1062. case 3: /* I2S block - read byte */
  1063. strlcpy(chip->name, "I2S (byte)", sizeof(chip->name));
  1064. return 0;
  1065. case 4: /* AFE - read dword */
  1066. strlcpy(chip->name, "AFE (dword)", sizeof(chip->name));
  1067. return 0;
  1068. case 5: /* Video Block - read dword */
  1069. strlcpy(chip->name, "Video (dword)", sizeof(chip->name));
  1070. return 0;
  1071. case 6: /* I2S Block - read dword */
  1072. strlcpy(chip->name, "I2S (dword)", sizeof(chip->name));
  1073. return 0;
  1074. }
  1075. return -EINVAL;
  1076. }
  1077. int cx231xx_g_register(struct file *file, void *priv,
  1078. struct v4l2_dbg_register *reg)
  1079. {
  1080. struct cx231xx_fh *fh = priv;
  1081. struct cx231xx *dev = fh->dev;
  1082. int ret;
  1083. u8 value[4] = { 0, 0, 0, 0 };
  1084. u32 data = 0;
  1085. switch (reg->match.addr) {
  1086. case 0: /* Cx231xx - internal registers */
  1087. ret = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER,
  1088. (u16)reg->reg, value, 4);
  1089. reg->val = value[0] | value[1] << 8 |
  1090. value[2] << 16 | value[3] << 24;
  1091. reg->size = 4;
  1092. break;
  1093. case 1: /* AFE - read byte */
  1094. ret = cx231xx_read_i2c_data(dev, AFE_DEVICE_ADDRESS,
  1095. (u16)reg->reg, 2, &data, 1);
  1096. reg->val = data;
  1097. reg->size = 1;
  1098. break;
  1099. case 2: /* Video Block - read byte */
  1100. ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
  1101. (u16)reg->reg, 2, &data, 1);
  1102. reg->val = data;
  1103. reg->size = 1;
  1104. break;
  1105. case 3: /* I2S block - read byte */
  1106. ret = cx231xx_read_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
  1107. (u16)reg->reg, 1, &data, 1);
  1108. reg->val = data;
  1109. reg->size = 1;
  1110. break;
  1111. case 4: /* AFE - read dword */
  1112. ret = cx231xx_read_i2c_data(dev, AFE_DEVICE_ADDRESS,
  1113. (u16)reg->reg, 2, &data, 4);
  1114. reg->val = data;
  1115. reg->size = 4;
  1116. break;
  1117. case 5: /* Video Block - read dword */
  1118. ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
  1119. (u16)reg->reg, 2, &data, 4);
  1120. reg->val = data;
  1121. reg->size = 4;
  1122. break;
  1123. case 6: /* I2S Block - read dword */
  1124. ret = cx231xx_read_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
  1125. (u16)reg->reg, 1, &data, 4);
  1126. reg->val = data;
  1127. reg->size = 4;
  1128. break;
  1129. default:
  1130. return -EINVAL;
  1131. }
  1132. return ret < 0 ? ret : 0;
  1133. }
  1134. int cx231xx_s_register(struct file *file, void *priv,
  1135. const struct v4l2_dbg_register *reg)
  1136. {
  1137. struct cx231xx_fh *fh = priv;
  1138. struct cx231xx *dev = fh->dev;
  1139. int ret;
  1140. u8 data[4] = { 0, 0, 0, 0 };
  1141. switch (reg->match.addr) {
  1142. case 0: /* cx231xx internal registers */
  1143. data[0] = (u8) reg->val;
  1144. data[1] = (u8) (reg->val >> 8);
  1145. data[2] = (u8) (reg->val >> 16);
  1146. data[3] = (u8) (reg->val >> 24);
  1147. ret = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
  1148. (u16)reg->reg, data, 4);
  1149. break;
  1150. case 1: /* AFE - write byte */
  1151. ret = cx231xx_write_i2c_data(dev, AFE_DEVICE_ADDRESS,
  1152. (u16)reg->reg, 2, reg->val, 1);
  1153. break;
  1154. case 2: /* Video Block - write byte */
  1155. ret = cx231xx_write_i2c_data(dev, VID_BLK_I2C_ADDRESS,
  1156. (u16)reg->reg, 2, reg->val, 1);
  1157. break;
  1158. case 3: /* I2S block - write byte */
  1159. ret = cx231xx_write_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
  1160. (u16)reg->reg, 1, reg->val, 1);
  1161. break;
  1162. case 4: /* AFE - write dword */
  1163. ret = cx231xx_write_i2c_data(dev, AFE_DEVICE_ADDRESS,
  1164. (u16)reg->reg, 2, reg->val, 4);
  1165. break;
  1166. case 5: /* Video Block - write dword */
  1167. ret = cx231xx_write_i2c_data(dev, VID_BLK_I2C_ADDRESS,
  1168. (u16)reg->reg, 2, reg->val, 4);
  1169. break;
  1170. case 6: /* I2S block - write dword */
  1171. ret = cx231xx_write_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
  1172. (u16)reg->reg, 1, reg->val, 4);
  1173. break;
  1174. default:
  1175. return -EINVAL;
  1176. }
  1177. return ret < 0 ? ret : 0;
  1178. }
  1179. #endif
  1180. static int vidioc_cropcap(struct file *file, void *priv,
  1181. struct v4l2_cropcap *cc)
  1182. {
  1183. struct cx231xx_fh *fh = priv;
  1184. struct cx231xx *dev = fh->dev;
  1185. if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1186. return -EINVAL;
  1187. cc->bounds.left = 0;
  1188. cc->bounds.top = 0;
  1189. cc->bounds.width = dev->width;
  1190. cc->bounds.height = dev->height;
  1191. cc->defrect = cc->bounds;
  1192. cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
  1193. cc->pixelaspect.denominator = 59;
  1194. return 0;
  1195. }
  1196. static int vidioc_streamon(struct file *file, void *priv,
  1197. enum v4l2_buf_type type)
  1198. {
  1199. struct cx231xx_fh *fh = priv;
  1200. struct cx231xx *dev = fh->dev;
  1201. int rc;
  1202. rc = check_dev(dev);
  1203. if (rc < 0)
  1204. return rc;
  1205. rc = res_get(fh);
  1206. if (likely(rc >= 0))
  1207. rc = videobuf_streamon(&fh->vb_vidq);
  1208. call_all(dev, video, s_stream, 1);
  1209. return rc;
  1210. }
  1211. static int vidioc_streamoff(struct file *file, void *priv,
  1212. enum v4l2_buf_type type)
  1213. {
  1214. struct cx231xx_fh *fh = priv;
  1215. struct cx231xx *dev = fh->dev;
  1216. int rc;
  1217. rc = check_dev(dev);
  1218. if (rc < 0)
  1219. return rc;
  1220. if (type != fh->type)
  1221. return -EINVAL;
  1222. cx25840_call(dev, video, s_stream, 0);
  1223. videobuf_streamoff(&fh->vb_vidq);
  1224. res_free(fh);
  1225. return 0;
  1226. }
  1227. int cx231xx_querycap(struct file *file, void *priv,
  1228. struct v4l2_capability *cap)
  1229. {
  1230. struct video_device *vdev = video_devdata(file);
  1231. struct cx231xx_fh *fh = priv;
  1232. struct cx231xx *dev = fh->dev;
  1233. strlcpy(cap->driver, "cx231xx", sizeof(cap->driver));
  1234. strlcpy(cap->card, cx231xx_boards[dev->model].name, sizeof(cap->card));
  1235. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  1236. if (vdev->vfl_type == VFL_TYPE_RADIO)
  1237. cap->device_caps = V4L2_CAP_RADIO;
  1238. else {
  1239. cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  1240. if (vdev->vfl_type == VFL_TYPE_VBI)
  1241. cap->device_caps |= V4L2_CAP_VBI_CAPTURE;
  1242. else
  1243. cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
  1244. }
  1245. if (dev->tuner_type != TUNER_ABSENT)
  1246. cap->device_caps |= V4L2_CAP_TUNER;
  1247. cap->capabilities = cap->device_caps | V4L2_CAP_READWRITE |
  1248. V4L2_CAP_VBI_CAPTURE | V4L2_CAP_VIDEO_CAPTURE |
  1249. V4L2_CAP_STREAMING | V4L2_CAP_DEVICE_CAPS;
  1250. if (video_is_registered(&dev->radio_dev))
  1251. cap->capabilities |= V4L2_CAP_RADIO;
  1252. return 0;
  1253. }
  1254. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  1255. struct v4l2_fmtdesc *f)
  1256. {
  1257. if (unlikely(f->index >= ARRAY_SIZE(format)))
  1258. return -EINVAL;
  1259. strlcpy(f->description, format[f->index].name, sizeof(f->description));
  1260. f->pixelformat = format[f->index].fourcc;
  1261. return 0;
  1262. }
  1263. /* RAW VBI ioctls */
  1264. static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
  1265. struct v4l2_format *f)
  1266. {
  1267. struct cx231xx_fh *fh = priv;
  1268. struct cx231xx *dev = fh->dev;
  1269. f->fmt.vbi.sampling_rate = 6750000 * 4;
  1270. f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
  1271. f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  1272. f->fmt.vbi.offset = 0;
  1273. f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
  1274. PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
  1275. f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
  1276. PAL_VBI_LINES : NTSC_VBI_LINES;
  1277. f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
  1278. PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
  1279. f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
  1280. memset(f->fmt.vbi.reserved, 0, sizeof(f->fmt.vbi.reserved));
  1281. return 0;
  1282. }
  1283. static int vidioc_try_fmt_vbi_cap(struct file *file, void *priv,
  1284. struct v4l2_format *f)
  1285. {
  1286. struct cx231xx_fh *fh = priv;
  1287. struct cx231xx *dev = fh->dev;
  1288. f->fmt.vbi.sampling_rate = 6750000 * 4;
  1289. f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
  1290. f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  1291. f->fmt.vbi.offset = 0;
  1292. f->fmt.vbi.flags = 0;
  1293. f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
  1294. PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
  1295. f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
  1296. PAL_VBI_LINES : NTSC_VBI_LINES;
  1297. f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
  1298. PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
  1299. f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
  1300. memset(f->fmt.vbi.reserved, 0, sizeof(f->fmt.vbi.reserved));
  1301. return 0;
  1302. }
  1303. static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
  1304. struct v4l2_format *f)
  1305. {
  1306. struct cx231xx_fh *fh = priv;
  1307. struct cx231xx *dev = fh->dev;
  1308. if (dev->vbi_stream_on && !fh->stream_on) {
  1309. dev_err(dev->dev,
  1310. "%s device in use by another fh\n", __func__);
  1311. return -EBUSY;
  1312. }
  1313. return vidioc_try_fmt_vbi_cap(file, priv, f);
  1314. }
  1315. static int vidioc_reqbufs(struct file *file, void *priv,
  1316. struct v4l2_requestbuffers *rb)
  1317. {
  1318. struct cx231xx_fh *fh = priv;
  1319. struct cx231xx *dev = fh->dev;
  1320. int rc;
  1321. rc = check_dev(dev);
  1322. if (rc < 0)
  1323. return rc;
  1324. return videobuf_reqbufs(&fh->vb_vidq, rb);
  1325. }
  1326. static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1327. {
  1328. struct cx231xx_fh *fh = priv;
  1329. struct cx231xx *dev = fh->dev;
  1330. int rc;
  1331. rc = check_dev(dev);
  1332. if (rc < 0)
  1333. return rc;
  1334. return videobuf_querybuf(&fh->vb_vidq, b);
  1335. }
  1336. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1337. {
  1338. struct cx231xx_fh *fh = priv;
  1339. struct cx231xx *dev = fh->dev;
  1340. int rc;
  1341. rc = check_dev(dev);
  1342. if (rc < 0)
  1343. return rc;
  1344. return videobuf_qbuf(&fh->vb_vidq, b);
  1345. }
  1346. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1347. {
  1348. struct cx231xx_fh *fh = priv;
  1349. struct cx231xx *dev = fh->dev;
  1350. int rc;
  1351. rc = check_dev(dev);
  1352. if (rc < 0)
  1353. return rc;
  1354. return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK);
  1355. }
  1356. /* ----------------------------------------------------------- */
  1357. /* RADIO ESPECIFIC IOCTLS */
  1358. /* ----------------------------------------------------------- */
  1359. static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
  1360. {
  1361. struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
  1362. if (t->index)
  1363. return -EINVAL;
  1364. strcpy(t->name, "Radio");
  1365. call_all(dev, tuner, g_tuner, t);
  1366. return 0;
  1367. }
  1368. static int radio_s_tuner(struct file *file, void *priv, const struct v4l2_tuner *t)
  1369. {
  1370. struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
  1371. if (t->index)
  1372. return -EINVAL;
  1373. call_all(dev, tuner, s_tuner, t);
  1374. return 0;
  1375. }
  1376. /*
  1377. * cx231xx_v4l2_open()
  1378. * inits the device and starts isoc transfer
  1379. */
  1380. static int cx231xx_v4l2_open(struct file *filp)
  1381. {
  1382. int radio = 0;
  1383. struct video_device *vdev = video_devdata(filp);
  1384. struct cx231xx *dev = video_drvdata(filp);
  1385. struct cx231xx_fh *fh;
  1386. enum v4l2_buf_type fh_type = 0;
  1387. switch (vdev->vfl_type) {
  1388. case VFL_TYPE_GRABBER:
  1389. fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1390. break;
  1391. case VFL_TYPE_VBI:
  1392. fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
  1393. break;
  1394. case VFL_TYPE_RADIO:
  1395. radio = 1;
  1396. break;
  1397. }
  1398. cx231xx_videodbg("open dev=%s type=%s users=%d\n",
  1399. video_device_node_name(vdev), v4l2_type_names[fh_type],
  1400. dev->users);
  1401. #if 0
  1402. errCode = cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
  1403. if (errCode < 0) {
  1404. dev_err(dev->dev,
  1405. "Device locked on digital mode. Can't open analog\n");
  1406. return -EBUSY;
  1407. }
  1408. #endif
  1409. fh = kzalloc(sizeof(struct cx231xx_fh), GFP_KERNEL);
  1410. if (!fh)
  1411. return -ENOMEM;
  1412. if (mutex_lock_interruptible(&dev->lock)) {
  1413. kfree(fh);
  1414. return -ERESTARTSYS;
  1415. }
  1416. fh->dev = dev;
  1417. fh->type = fh_type;
  1418. filp->private_data = fh;
  1419. v4l2_fh_init(&fh->fh, vdev);
  1420. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
  1421. /* Power up in Analog TV mode */
  1422. if (dev->board.external_av)
  1423. cx231xx_set_power_mode(dev,
  1424. POLARIS_AVMODE_ENXTERNAL_AV);
  1425. else
  1426. cx231xx_set_power_mode(dev, POLARIS_AVMODE_ANALOGT_TV);
  1427. #if 0
  1428. cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
  1429. #endif
  1430. /* set video alternate setting */
  1431. cx231xx_set_video_alternate(dev);
  1432. /* Needed, since GPIO might have disabled power of
  1433. some i2c device */
  1434. cx231xx_config_i2c(dev);
  1435. /* device needs to be initialized before isoc transfer */
  1436. dev->video_input = dev->video_input > 2 ? 2 : dev->video_input;
  1437. }
  1438. if (radio) {
  1439. cx231xx_videodbg("video_open: setting radio device\n");
  1440. /* cx231xx_start_radio(dev); */
  1441. call_all(dev, tuner, s_radio);
  1442. }
  1443. dev->users++;
  1444. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1445. videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_video_qops,
  1446. NULL, &dev->video_mode.slock,
  1447. fh->type, V4L2_FIELD_INTERLACED,
  1448. sizeof(struct cx231xx_buffer),
  1449. fh, &dev->lock);
  1450. if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
  1451. /* Set the required alternate setting VBI interface works in
  1452. Bulk mode only */
  1453. cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
  1454. videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_vbi_qops,
  1455. NULL, &dev->vbi_mode.slock,
  1456. fh->type, V4L2_FIELD_SEQ_TB,
  1457. sizeof(struct cx231xx_buffer),
  1458. fh, &dev->lock);
  1459. }
  1460. mutex_unlock(&dev->lock);
  1461. v4l2_fh_add(&fh->fh);
  1462. return 0;
  1463. }
  1464. /*
  1465. * cx231xx_realease_resources()
  1466. * unregisters the v4l2,i2c and usb devices
  1467. * called when the device gets disconected or at module unload
  1468. */
  1469. void cx231xx_release_analog_resources(struct cx231xx *dev)
  1470. {
  1471. /*FIXME: I2C IR should be disconnected */
  1472. if (video_is_registered(&dev->radio_dev))
  1473. video_unregister_device(&dev->radio_dev);
  1474. if (video_is_registered(&dev->vbi_dev)) {
  1475. dev_info(dev->dev, "V4L2 device %s deregistered\n",
  1476. video_device_node_name(&dev->vbi_dev));
  1477. video_unregister_device(&dev->vbi_dev);
  1478. }
  1479. if (video_is_registered(&dev->vdev)) {
  1480. dev_info(dev->dev, "V4L2 device %s deregistered\n",
  1481. video_device_node_name(&dev->vdev));
  1482. if (dev->board.has_417)
  1483. cx231xx_417_unregister(dev);
  1484. video_unregister_device(&dev->vdev);
  1485. }
  1486. v4l2_ctrl_handler_free(&dev->ctrl_handler);
  1487. v4l2_ctrl_handler_free(&dev->radio_ctrl_handler);
  1488. }
  1489. /*
  1490. * cx231xx_close()
  1491. * stops streaming and deallocates all resources allocated by the v4l2
  1492. * calls and ioctls
  1493. */
  1494. static int cx231xx_close(struct file *filp)
  1495. {
  1496. struct cx231xx_fh *fh = filp->private_data;
  1497. struct cx231xx *dev = fh->dev;
  1498. cx231xx_videodbg("users=%d\n", dev->users);
  1499. cx231xx_videodbg("users=%d\n", dev->users);
  1500. if (res_check(fh))
  1501. res_free(fh);
  1502. /*
  1503. * To workaround error number=-71 on EP0 for VideoGrabber,
  1504. * need exclude following.
  1505. * FIXME: It is probably safe to remove most of these, as we're
  1506. * now avoiding the alternate setting for INDEX_VANC
  1507. */
  1508. if (!dev->board.no_alt_vanc)
  1509. if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
  1510. videobuf_stop(&fh->vb_vidq);
  1511. videobuf_mmap_free(&fh->vb_vidq);
  1512. /* the device is already disconnect,
  1513. free the remaining resources */
  1514. if (dev->state & DEV_DISCONNECTED) {
  1515. if (atomic_read(&dev->devlist_count) > 0) {
  1516. cx231xx_release_resources(dev);
  1517. fh->dev = NULL;
  1518. return 0;
  1519. }
  1520. return 0;
  1521. }
  1522. /* do this before setting alternate! */
  1523. cx231xx_uninit_vbi_isoc(dev);
  1524. /* set alternate 0 */
  1525. if (!dev->vbi_or_sliced_cc_mode)
  1526. cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
  1527. else
  1528. cx231xx_set_alt_setting(dev, INDEX_HANC, 0);
  1529. v4l2_fh_del(&fh->fh);
  1530. v4l2_fh_exit(&fh->fh);
  1531. kfree(fh);
  1532. dev->users--;
  1533. wake_up_interruptible_nr(&dev->open, 1);
  1534. return 0;
  1535. }
  1536. v4l2_fh_del(&fh->fh);
  1537. dev->users--;
  1538. if (!dev->users) {
  1539. videobuf_stop(&fh->vb_vidq);
  1540. videobuf_mmap_free(&fh->vb_vidq);
  1541. /* the device is already disconnect,
  1542. free the remaining resources */
  1543. if (dev->state & DEV_DISCONNECTED) {
  1544. cx231xx_release_resources(dev);
  1545. fh->dev = NULL;
  1546. return 0;
  1547. }
  1548. /* Save some power by putting tuner to sleep */
  1549. call_all(dev, core, s_power, 0);
  1550. /* do this before setting alternate! */
  1551. if (dev->USE_ISO)
  1552. cx231xx_uninit_isoc(dev);
  1553. else
  1554. cx231xx_uninit_bulk(dev);
  1555. cx231xx_set_mode(dev, CX231XX_SUSPEND);
  1556. /* set alternate 0 */
  1557. cx231xx_set_alt_setting(dev, INDEX_VIDEO, 0);
  1558. }
  1559. v4l2_fh_exit(&fh->fh);
  1560. kfree(fh);
  1561. wake_up_interruptible_nr(&dev->open, 1);
  1562. return 0;
  1563. }
  1564. static int cx231xx_v4l2_close(struct file *filp)
  1565. {
  1566. struct cx231xx_fh *fh = filp->private_data;
  1567. struct cx231xx *dev = fh->dev;
  1568. int rc;
  1569. mutex_lock(&dev->lock);
  1570. rc = cx231xx_close(filp);
  1571. mutex_unlock(&dev->lock);
  1572. return rc;
  1573. }
  1574. /*
  1575. * cx231xx_v4l2_read()
  1576. * will allocate buffers when called for the first time
  1577. */
  1578. static ssize_t
  1579. cx231xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
  1580. loff_t *pos)
  1581. {
  1582. struct cx231xx_fh *fh = filp->private_data;
  1583. struct cx231xx *dev = fh->dev;
  1584. int rc;
  1585. rc = check_dev(dev);
  1586. if (rc < 0)
  1587. return rc;
  1588. if ((fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
  1589. (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)) {
  1590. rc = res_get(fh);
  1591. if (unlikely(rc < 0))
  1592. return rc;
  1593. if (mutex_lock_interruptible(&dev->lock))
  1594. return -ERESTARTSYS;
  1595. rc = videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
  1596. filp->f_flags & O_NONBLOCK);
  1597. mutex_unlock(&dev->lock);
  1598. return rc;
  1599. }
  1600. return 0;
  1601. }
  1602. /*
  1603. * cx231xx_v4l2_poll()
  1604. * will allocate buffers when called for the first time
  1605. */
  1606. static unsigned int cx231xx_v4l2_poll(struct file *filp, poll_table *wait)
  1607. {
  1608. unsigned long req_events = poll_requested_events(wait);
  1609. struct cx231xx_fh *fh = filp->private_data;
  1610. struct cx231xx *dev = fh->dev;
  1611. unsigned res = 0;
  1612. int rc;
  1613. rc = check_dev(dev);
  1614. if (rc < 0)
  1615. return POLLERR;
  1616. rc = res_get(fh);
  1617. if (unlikely(rc < 0))
  1618. return POLLERR;
  1619. if (v4l2_event_pending(&fh->fh))
  1620. res |= POLLPRI;
  1621. else
  1622. poll_wait(filp, &fh->fh.wait, wait);
  1623. if (!(req_events & (POLLIN | POLLRDNORM)))
  1624. return res;
  1625. if ((V4L2_BUF_TYPE_VIDEO_CAPTURE == fh->type) ||
  1626. (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)) {
  1627. mutex_lock(&dev->lock);
  1628. res |= videobuf_poll_stream(filp, &fh->vb_vidq, wait);
  1629. mutex_unlock(&dev->lock);
  1630. return res;
  1631. }
  1632. return res | POLLERR;
  1633. }
  1634. /*
  1635. * cx231xx_v4l2_mmap()
  1636. */
  1637. static int cx231xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
  1638. {
  1639. struct cx231xx_fh *fh = filp->private_data;
  1640. struct cx231xx *dev = fh->dev;
  1641. int rc;
  1642. rc = check_dev(dev);
  1643. if (rc < 0)
  1644. return rc;
  1645. rc = res_get(fh);
  1646. if (unlikely(rc < 0))
  1647. return rc;
  1648. if (mutex_lock_interruptible(&dev->lock))
  1649. return -ERESTARTSYS;
  1650. rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
  1651. mutex_unlock(&dev->lock);
  1652. cx231xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
  1653. (unsigned long)vma->vm_start,
  1654. (unsigned long)vma->vm_end -
  1655. (unsigned long)vma->vm_start, rc);
  1656. return rc;
  1657. }
  1658. static const struct v4l2_file_operations cx231xx_v4l_fops = {
  1659. .owner = THIS_MODULE,
  1660. .open = cx231xx_v4l2_open,
  1661. .release = cx231xx_v4l2_close,
  1662. .read = cx231xx_v4l2_read,
  1663. .poll = cx231xx_v4l2_poll,
  1664. .mmap = cx231xx_v4l2_mmap,
  1665. .unlocked_ioctl = video_ioctl2,
  1666. };
  1667. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1668. .vidioc_querycap = cx231xx_querycap,
  1669. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1670. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1671. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1672. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1673. .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
  1674. .vidioc_try_fmt_vbi_cap = vidioc_try_fmt_vbi_cap,
  1675. .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
  1676. .vidioc_cropcap = vidioc_cropcap,
  1677. .vidioc_reqbufs = vidioc_reqbufs,
  1678. .vidioc_querybuf = vidioc_querybuf,
  1679. .vidioc_qbuf = vidioc_qbuf,
  1680. .vidioc_dqbuf = vidioc_dqbuf,
  1681. .vidioc_s_std = vidioc_s_std,
  1682. .vidioc_g_std = vidioc_g_std,
  1683. .vidioc_enum_input = cx231xx_enum_input,
  1684. .vidioc_g_input = cx231xx_g_input,
  1685. .vidioc_s_input = cx231xx_s_input,
  1686. .vidioc_streamon = vidioc_streamon,
  1687. .vidioc_streamoff = vidioc_streamoff,
  1688. .vidioc_g_tuner = cx231xx_g_tuner,
  1689. .vidioc_s_tuner = cx231xx_s_tuner,
  1690. .vidioc_g_frequency = cx231xx_g_frequency,
  1691. .vidioc_s_frequency = cx231xx_s_frequency,
  1692. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1693. .vidioc_g_chip_info = cx231xx_g_chip_info,
  1694. .vidioc_g_register = cx231xx_g_register,
  1695. .vidioc_s_register = cx231xx_s_register,
  1696. #endif
  1697. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1698. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1699. };
  1700. static struct video_device cx231xx_vbi_template;
  1701. static const struct video_device cx231xx_video_template = {
  1702. .fops = &cx231xx_v4l_fops,
  1703. .release = video_device_release_empty,
  1704. .ioctl_ops = &video_ioctl_ops,
  1705. .tvnorms = V4L2_STD_ALL,
  1706. };
  1707. static const struct v4l2_file_operations radio_fops = {
  1708. .owner = THIS_MODULE,
  1709. .open = cx231xx_v4l2_open,
  1710. .release = cx231xx_v4l2_close,
  1711. .poll = v4l2_ctrl_poll,
  1712. .unlocked_ioctl = video_ioctl2,
  1713. };
  1714. static const struct v4l2_ioctl_ops radio_ioctl_ops = {
  1715. .vidioc_querycap = cx231xx_querycap,
  1716. .vidioc_g_tuner = radio_g_tuner,
  1717. .vidioc_s_tuner = radio_s_tuner,
  1718. .vidioc_g_frequency = cx231xx_g_frequency,
  1719. .vidioc_s_frequency = cx231xx_s_frequency,
  1720. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1721. .vidioc_g_chip_info = cx231xx_g_chip_info,
  1722. .vidioc_g_register = cx231xx_g_register,
  1723. .vidioc_s_register = cx231xx_s_register,
  1724. #endif
  1725. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  1726. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  1727. };
  1728. static struct video_device cx231xx_radio_template = {
  1729. .name = "cx231xx-radio",
  1730. .fops = &radio_fops,
  1731. .ioctl_ops = &radio_ioctl_ops,
  1732. };
  1733. /******************************** usb interface ******************************/
  1734. static void cx231xx_vdev_init(struct cx231xx *dev,
  1735. struct video_device *vfd,
  1736. const struct video_device *template,
  1737. const char *type_name)
  1738. {
  1739. *vfd = *template;
  1740. vfd->v4l2_dev = &dev->v4l2_dev;
  1741. vfd->release = video_device_release_empty;
  1742. vfd->lock = &dev->lock;
  1743. snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name);
  1744. video_set_drvdata(vfd, dev);
  1745. if (dev->tuner_type == TUNER_ABSENT) {
  1746. v4l2_disable_ioctl(vfd, VIDIOC_G_FREQUENCY);
  1747. v4l2_disable_ioctl(vfd, VIDIOC_S_FREQUENCY);
  1748. v4l2_disable_ioctl(vfd, VIDIOC_G_TUNER);
  1749. v4l2_disable_ioctl(vfd, VIDIOC_S_TUNER);
  1750. }
  1751. }
  1752. int cx231xx_register_analog_devices(struct cx231xx *dev)
  1753. {
  1754. int ret;
  1755. dev_info(dev->dev, "v4l2 driver version %s\n", CX231XX_VERSION);
  1756. /* set default norm */
  1757. dev->norm = V4L2_STD_PAL;
  1758. dev->width = norm_maxw(dev);
  1759. dev->height = norm_maxh(dev);
  1760. dev->interlaced = 0;
  1761. /* Analog specific initialization */
  1762. dev->format = &format[0];
  1763. /* Set the initial input */
  1764. video_mux(dev, dev->video_input);
  1765. call_all(dev, video, s_std, dev->norm);
  1766. v4l2_ctrl_handler_init(&dev->ctrl_handler, 10);
  1767. v4l2_ctrl_handler_init(&dev->radio_ctrl_handler, 5);
  1768. if (dev->sd_cx25840) {
  1769. v4l2_ctrl_add_handler(&dev->ctrl_handler,
  1770. dev->sd_cx25840->ctrl_handler, NULL);
  1771. v4l2_ctrl_add_handler(&dev->radio_ctrl_handler,
  1772. dev->sd_cx25840->ctrl_handler,
  1773. v4l2_ctrl_radio_filter);
  1774. }
  1775. if (dev->ctrl_handler.error)
  1776. return dev->ctrl_handler.error;
  1777. if (dev->radio_ctrl_handler.error)
  1778. return dev->radio_ctrl_handler.error;
  1779. /* enable vbi capturing */
  1780. /* write code here... */
  1781. /* allocate and fill video video_device struct */
  1782. cx231xx_vdev_init(dev, &dev->vdev, &cx231xx_video_template, "video");
  1783. #if defined(CONFIG_MEDIA_CONTROLLER)
  1784. dev->video_pad.flags = MEDIA_PAD_FL_SINK;
  1785. ret = media_entity_init(&dev->vdev.entity, 1, &dev->video_pad, 0);
  1786. if (ret < 0)
  1787. dev_err(dev->dev, "failed to initialize video media entity!\n");
  1788. #endif
  1789. dev->vdev.ctrl_handler = &dev->ctrl_handler;
  1790. /* register v4l2 video video_device */
  1791. ret = video_register_device(&dev->vdev, VFL_TYPE_GRABBER,
  1792. video_nr[dev->devno]);
  1793. if (ret) {
  1794. dev_err(dev->dev,
  1795. "unable to register video device (error=%i).\n",
  1796. ret);
  1797. return ret;
  1798. }
  1799. dev_info(dev->dev, "Registered video device %s [v4l2]\n",
  1800. video_device_node_name(&dev->vdev));
  1801. /* Initialize VBI template */
  1802. cx231xx_vbi_template = cx231xx_video_template;
  1803. strcpy(cx231xx_vbi_template.name, "cx231xx-vbi");
  1804. /* Allocate and fill vbi video_device struct */
  1805. cx231xx_vdev_init(dev, &dev->vbi_dev, &cx231xx_vbi_template, "vbi");
  1806. #if defined(CONFIG_MEDIA_CONTROLLER)
  1807. dev->vbi_pad.flags = MEDIA_PAD_FL_SINK;
  1808. ret = media_entity_init(&dev->vbi_dev.entity, 1, &dev->vbi_pad, 0);
  1809. if (ret < 0)
  1810. dev_err(dev->dev, "failed to initialize vbi media entity!\n");
  1811. #endif
  1812. dev->vbi_dev.ctrl_handler = &dev->ctrl_handler;
  1813. /* register v4l2 vbi video_device */
  1814. ret = video_register_device(&dev->vbi_dev, VFL_TYPE_VBI,
  1815. vbi_nr[dev->devno]);
  1816. if (ret < 0) {
  1817. dev_err(dev->dev, "unable to register vbi device\n");
  1818. return ret;
  1819. }
  1820. dev_info(dev->dev, "Registered VBI device %s\n",
  1821. video_device_node_name(&dev->vbi_dev));
  1822. if (cx231xx_boards[dev->model].radio.type == CX231XX_RADIO) {
  1823. cx231xx_vdev_init(dev, &dev->radio_dev,
  1824. &cx231xx_radio_template, "radio");
  1825. dev->radio_dev.ctrl_handler = &dev->radio_ctrl_handler;
  1826. ret = video_register_device(&dev->radio_dev, VFL_TYPE_RADIO,
  1827. radio_nr[dev->devno]);
  1828. if (ret < 0) {
  1829. dev_err(dev->dev,
  1830. "can't register radio device\n");
  1831. return ret;
  1832. }
  1833. dev_info(dev->dev, "Registered radio device as %s\n",
  1834. video_device_node_name(&dev->radio_dev));
  1835. }
  1836. return 0;
  1837. }