cx231xx-video.c 64 KB

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