cx231xx-video.c 55 KB

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