cx231xx-video.c 59 KB

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