cx231xx-video.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665
  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) || (dev->state & DEV_MISCONFIGURED))
  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) || (dev->state & DEV_MISCONFIGURED))
  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. if (dev->state & DEV_MISCONFIGURED) {
  831. cx231xx_errdev("v4l2 ioctl: device is misconfigured; "
  832. "close and open it again\n");
  833. return -EIO;
  834. }
  835. return 0;
  836. }
  837. /* ------------------------------------------------------------------
  838. IOCTL vidioc handling
  839. ------------------------------------------------------------------*/
  840. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  841. struct v4l2_format *f)
  842. {
  843. struct cx231xx_fh *fh = priv;
  844. struct cx231xx *dev = fh->dev;
  845. f->fmt.pix.width = dev->width;
  846. f->fmt.pix.height = dev->height;
  847. f->fmt.pix.pixelformat = dev->format->fourcc;
  848. f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
  849. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
  850. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  851. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  852. return 0;
  853. }
  854. static struct cx231xx_fmt *format_by_fourcc(unsigned int fourcc)
  855. {
  856. unsigned int i;
  857. for (i = 0; i < ARRAY_SIZE(format); i++)
  858. if (format[i].fourcc == fourcc)
  859. return &format[i];
  860. return NULL;
  861. }
  862. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  863. struct v4l2_format *f)
  864. {
  865. struct cx231xx_fh *fh = priv;
  866. struct cx231xx *dev = fh->dev;
  867. unsigned int width = f->fmt.pix.width;
  868. unsigned int height = f->fmt.pix.height;
  869. unsigned int maxw = norm_maxw(dev);
  870. unsigned int maxh = norm_maxh(dev);
  871. struct cx231xx_fmt *fmt;
  872. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  873. if (!fmt) {
  874. cx231xx_videodbg("Fourcc format (%08x) invalid.\n",
  875. f->fmt.pix.pixelformat);
  876. return -EINVAL;
  877. }
  878. /* width must even because of the YUYV format
  879. height must be even because of interlacing */
  880. v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh, 1, 0);
  881. f->fmt.pix.width = width;
  882. f->fmt.pix.height = height;
  883. f->fmt.pix.pixelformat = fmt->fourcc;
  884. f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
  885. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
  886. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  887. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  888. return 0;
  889. }
  890. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  891. struct v4l2_format *f)
  892. {
  893. struct cx231xx_fh *fh = priv;
  894. struct cx231xx *dev = fh->dev;
  895. int rc;
  896. struct cx231xx_fmt *fmt;
  897. struct v4l2_mbus_framefmt mbus_fmt;
  898. rc = check_dev(dev);
  899. if (rc < 0)
  900. return rc;
  901. vidioc_try_fmt_vid_cap(file, priv, f);
  902. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  903. if (!fmt)
  904. return -EINVAL;
  905. if (videobuf_queue_is_busy(&fh->vb_vidq)) {
  906. cx231xx_errdev("%s queue busy\n", __func__);
  907. return -EBUSY;
  908. }
  909. if (dev->stream_on && !fh->stream_on) {
  910. cx231xx_errdev("%s device in use by another fh\n", __func__);
  911. return -EBUSY;
  912. }
  913. /* set new image size */
  914. dev->width = f->fmt.pix.width;
  915. dev->height = f->fmt.pix.height;
  916. dev->format = fmt;
  917. v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
  918. call_all(dev, video, s_mbus_fmt, &mbus_fmt);
  919. v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
  920. return rc;
  921. }
  922. static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
  923. {
  924. struct cx231xx_fh *fh = priv;
  925. struct cx231xx *dev = fh->dev;
  926. *id = dev->norm;
  927. return 0;
  928. }
  929. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
  930. {
  931. struct cx231xx_fh *fh = priv;
  932. struct cx231xx *dev = fh->dev;
  933. struct v4l2_mbus_framefmt mbus_fmt;
  934. struct v4l2_format f;
  935. int rc;
  936. rc = check_dev(dev);
  937. if (rc < 0)
  938. return rc;
  939. cx231xx_info("vidioc_s_std : 0x%x\n", (unsigned int)*norm);
  940. dev->norm = *norm;
  941. /* Adjusts width/height, if needed */
  942. f.fmt.pix.width = dev->width;
  943. f.fmt.pix.height = dev->height;
  944. vidioc_try_fmt_vid_cap(file, priv, &f);
  945. call_all(dev, core, s_std, dev->norm);
  946. /* We need to reset basic properties in the decoder related to
  947. resolution (since a standard change effects things like the number
  948. of lines in VACT, etc) */
  949. v4l2_fill_mbus_format(&mbus_fmt, &f.fmt.pix, V4L2_MBUS_FMT_FIXED);
  950. call_all(dev, video, s_mbus_fmt, &mbus_fmt);
  951. v4l2_fill_pix_format(&f.fmt.pix, &mbus_fmt);
  952. /* set new image size */
  953. dev->width = f.fmt.pix.width;
  954. dev->height = f.fmt.pix.height;
  955. /* do mode control overrides */
  956. cx231xx_do_mode_ctrl_overrides(dev);
  957. return 0;
  958. }
  959. static const char *iname[] = {
  960. [CX231XX_VMUX_COMPOSITE1] = "Composite1",
  961. [CX231XX_VMUX_SVIDEO] = "S-Video",
  962. [CX231XX_VMUX_TELEVISION] = "Television",
  963. [CX231XX_VMUX_CABLE] = "Cable TV",
  964. [CX231XX_VMUX_DVB] = "DVB",
  965. [CX231XX_VMUX_DEBUG] = "for debug only",
  966. };
  967. static int vidioc_enum_input(struct file *file, void *priv,
  968. struct v4l2_input *i)
  969. {
  970. struct cx231xx_fh *fh = priv;
  971. struct cx231xx *dev = fh->dev;
  972. u32 gen_stat;
  973. unsigned int ret, n;
  974. n = i->index;
  975. if (n >= MAX_CX231XX_INPUT)
  976. return -EINVAL;
  977. if (0 == INPUT(n)->type)
  978. return -EINVAL;
  979. i->index = n;
  980. i->type = V4L2_INPUT_TYPE_CAMERA;
  981. strcpy(i->name, iname[INPUT(n)->type]);
  982. if ((CX231XX_VMUX_TELEVISION == INPUT(n)->type) ||
  983. (CX231XX_VMUX_CABLE == INPUT(n)->type))
  984. i->type = V4L2_INPUT_TYPE_TUNER;
  985. i->std = dev->vdev->tvnorms;
  986. /* If they are asking about the active input, read signal status */
  987. if (n == dev->video_input) {
  988. ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
  989. GEN_STAT, 2, &gen_stat, 4);
  990. if (ret > 0) {
  991. if ((gen_stat & FLD_VPRES) == 0x00)
  992. i->status |= V4L2_IN_ST_NO_SIGNAL;
  993. if ((gen_stat & FLD_HLOCK) == 0x00)
  994. i->status |= V4L2_IN_ST_NO_H_LOCK;
  995. }
  996. }
  997. return 0;
  998. }
  999. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  1000. {
  1001. struct cx231xx_fh *fh = priv;
  1002. struct cx231xx *dev = fh->dev;
  1003. *i = dev->video_input;
  1004. return 0;
  1005. }
  1006. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  1007. {
  1008. struct cx231xx_fh *fh = priv;
  1009. struct cx231xx *dev = fh->dev;
  1010. int rc;
  1011. dev->mode_tv = 0;
  1012. rc = check_dev(dev);
  1013. if (rc < 0)
  1014. return rc;
  1015. if (i >= MAX_CX231XX_INPUT)
  1016. return -EINVAL;
  1017. if (0 == INPUT(i)->type)
  1018. return -EINVAL;
  1019. video_mux(dev, i);
  1020. if (INPUT(i)->type == CX231XX_VMUX_TELEVISION ||
  1021. INPUT(i)->type == CX231XX_VMUX_CABLE) {
  1022. /* There's a tuner, so reset the standard and put it on the
  1023. last known frequency (since it was probably powered down
  1024. until now */
  1025. call_all(dev, core, s_std, dev->norm);
  1026. }
  1027. return 0;
  1028. }
  1029. static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  1030. {
  1031. struct cx231xx_fh *fh = priv;
  1032. struct cx231xx *dev = fh->dev;
  1033. switch (a->index) {
  1034. case CX231XX_AMUX_VIDEO:
  1035. strcpy(a->name, "Television");
  1036. break;
  1037. case CX231XX_AMUX_LINE_IN:
  1038. strcpy(a->name, "Line In");
  1039. break;
  1040. default:
  1041. return -EINVAL;
  1042. }
  1043. a->index = dev->ctl_ainput;
  1044. a->capability = V4L2_AUDCAP_STEREO;
  1045. return 0;
  1046. }
  1047. static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
  1048. {
  1049. struct cx231xx_fh *fh = priv;
  1050. struct cx231xx *dev = fh->dev;
  1051. int status = 0;
  1052. /* Doesn't allow manual routing */
  1053. if (a->index != dev->ctl_ainput)
  1054. return -EINVAL;
  1055. dev->ctl_ainput = INPUT(a->index)->amux;
  1056. status = cx231xx_set_audio_input(dev, dev->ctl_ainput);
  1057. return status;
  1058. }
  1059. static int vidioc_queryctrl(struct file *file, void *priv,
  1060. struct v4l2_queryctrl *qc)
  1061. {
  1062. struct cx231xx_fh *fh = priv;
  1063. struct cx231xx *dev = fh->dev;
  1064. int id = qc->id;
  1065. int i;
  1066. int rc;
  1067. rc = check_dev(dev);
  1068. if (rc < 0)
  1069. return rc;
  1070. qc->id = v4l2_ctrl_next(ctrl_classes, qc->id);
  1071. if (unlikely(qc->id == 0))
  1072. return -EINVAL;
  1073. memset(qc, 0, sizeof(*qc));
  1074. qc->id = id;
  1075. if (qc->id < V4L2_CID_BASE || qc->id >= V4L2_CID_LASTP1)
  1076. return -EINVAL;
  1077. for (i = 0; i < CX231XX_CTLS; i++)
  1078. if (cx231xx_ctls[i].v.id == qc->id)
  1079. break;
  1080. if (i == CX231XX_CTLS) {
  1081. *qc = no_ctl;
  1082. return 0;
  1083. }
  1084. *qc = cx231xx_ctls[i].v;
  1085. call_all(dev, core, queryctrl, qc);
  1086. if (qc->type)
  1087. return 0;
  1088. else
  1089. return -EINVAL;
  1090. }
  1091. static int vidioc_g_ctrl(struct file *file, void *priv,
  1092. struct v4l2_control *ctrl)
  1093. {
  1094. struct cx231xx_fh *fh = priv;
  1095. struct cx231xx *dev = fh->dev;
  1096. int rc;
  1097. rc = check_dev(dev);
  1098. if (rc < 0)
  1099. return rc;
  1100. call_all(dev, core, g_ctrl, ctrl);
  1101. return rc;
  1102. }
  1103. static int vidioc_s_ctrl(struct file *file, void *priv,
  1104. struct v4l2_control *ctrl)
  1105. {
  1106. struct cx231xx_fh *fh = priv;
  1107. struct cx231xx *dev = fh->dev;
  1108. int rc;
  1109. rc = check_dev(dev);
  1110. if (rc < 0)
  1111. return rc;
  1112. call_all(dev, core, s_ctrl, ctrl);
  1113. return rc;
  1114. }
  1115. static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
  1116. {
  1117. struct cx231xx_fh *fh = priv;
  1118. struct cx231xx *dev = fh->dev;
  1119. int rc;
  1120. rc = check_dev(dev);
  1121. if (rc < 0)
  1122. return rc;
  1123. if (0 != t->index)
  1124. return -EINVAL;
  1125. strcpy(t->name, "Tuner");
  1126. t->type = V4L2_TUNER_ANALOG_TV;
  1127. t->capability = V4L2_TUNER_CAP_NORM;
  1128. t->rangehigh = 0xffffffffUL;
  1129. t->signal = 0xffff; /* LOCKED */
  1130. return 0;
  1131. }
  1132. static int vidioc_s_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
  1133. {
  1134. struct cx231xx_fh *fh = priv;
  1135. struct cx231xx *dev = fh->dev;
  1136. int rc;
  1137. rc = check_dev(dev);
  1138. if (rc < 0)
  1139. return rc;
  1140. if (0 != t->index)
  1141. return -EINVAL;
  1142. #if 0
  1143. call_all(dev, tuner, s_tuner, t);
  1144. #endif
  1145. return 0;
  1146. }
  1147. static int vidioc_g_frequency(struct file *file, void *priv,
  1148. struct v4l2_frequency *f)
  1149. {
  1150. struct cx231xx_fh *fh = priv;
  1151. struct cx231xx *dev = fh->dev;
  1152. f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1153. f->frequency = dev->ctl_freq;
  1154. call_all(dev, tuner, g_frequency, f);
  1155. return 0;
  1156. }
  1157. static int vidioc_s_frequency(struct file *file, void *priv,
  1158. struct v4l2_frequency *f)
  1159. {
  1160. struct cx231xx_fh *fh = priv;
  1161. struct cx231xx *dev = fh->dev;
  1162. int rc;
  1163. u32 if_frequency = 5400000;
  1164. cx231xx_info("Enter vidioc_s_frequency()f->frequency=%d;f->type=%d\n",
  1165. f->frequency, f->type);
  1166. /*cx231xx_info("f->type: 1-radio 2-analogTV 3-digitalTV\n");*/
  1167. rc = check_dev(dev);
  1168. if (rc < 0)
  1169. return rc;
  1170. if (0 != f->tuner)
  1171. return -EINVAL;
  1172. if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
  1173. return -EINVAL;
  1174. if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
  1175. return -EINVAL;
  1176. /* set pre channel change settings in DIF first */
  1177. rc = cx231xx_tuner_pre_channel_change(dev);
  1178. dev->ctl_freq = f->frequency;
  1179. call_all(dev, tuner, s_frequency, f);
  1180. /* set post channel change settings in DIF first */
  1181. rc = cx231xx_tuner_post_channel_change(dev);
  1182. if (dev->tuner_type == TUNER_NXP_TDA18271) {
  1183. if (dev->norm & (V4L2_STD_MN | V4L2_STD_NTSC_443))
  1184. if_frequency = 5400000; /*5.4MHz */
  1185. else if (dev->norm & V4L2_STD_B)
  1186. if_frequency = 6000000; /*6.0MHz */
  1187. else if (dev->norm & (V4L2_STD_PAL_DK | V4L2_STD_SECAM_DK))
  1188. if_frequency = 6900000; /*6.9MHz */
  1189. else if (dev->norm & V4L2_STD_GH)
  1190. if_frequency = 7100000; /*7.1MHz */
  1191. else if (dev->norm & V4L2_STD_PAL_I)
  1192. if_frequency = 7250000; /*7.25MHz */
  1193. else if (dev->norm & V4L2_STD_SECAM_L)
  1194. if_frequency = 6900000; /*6.9MHz */
  1195. else if (dev->norm & V4L2_STD_SECAM_LC)
  1196. if_frequency = 1250000; /*1.25MHz */
  1197. cx231xx_info("if_frequency is set to %d\n", if_frequency);
  1198. cx231xx_set_Colibri_For_LowIF(dev, if_frequency, 1, 1);
  1199. update_HH_register_after_set_DIF(dev);
  1200. }
  1201. cx231xx_info("Set New FREQUENCY to %d\n", f->frequency);
  1202. return rc;
  1203. }
  1204. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1205. /*
  1206. -R, --list-registers=type=<host/i2cdrv/i2caddr>,
  1207. chip=<chip>[,min=<addr>,max=<addr>]
  1208. dump registers from <min> to <max> [VIDIOC_DBG_G_REGISTER]
  1209. -r, --set-register=type=<host/i2cdrv/i2caddr>,
  1210. chip=<chip>,reg=<addr>,val=<val>
  1211. set the register [VIDIOC_DBG_S_REGISTER]
  1212. if type == host, then <chip> is the hosts chip ID (default 0)
  1213. if type == i2cdrv (default), then <chip> is the I2C driver name or ID
  1214. if type == i2caddr, then <chip> is the 7-bit I2C address
  1215. */
  1216. static int vidioc_g_register(struct file *file, void *priv,
  1217. struct v4l2_dbg_register *reg)
  1218. {
  1219. struct cx231xx_fh *fh = priv;
  1220. struct cx231xx *dev = fh->dev;
  1221. int ret = 0;
  1222. u8 value[4] = { 0, 0, 0, 0 };
  1223. u32 data = 0;
  1224. switch (reg->match.type) {
  1225. case V4L2_CHIP_MATCH_HOST:
  1226. switch (reg->match.addr) {
  1227. case 0: /* Cx231xx - internal registers */
  1228. ret = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER,
  1229. (u16)reg->reg, value, 4);
  1230. reg->val = value[0] | value[1] << 8 |
  1231. value[2] << 16 | value[3] << 24;
  1232. break;
  1233. case 1: /* AFE - read byte */
  1234. ret = cx231xx_read_i2c_data(dev, AFE_DEVICE_ADDRESS,
  1235. (u16)reg->reg, 2, &data, 1);
  1236. reg->val = le32_to_cpu(data & 0xff);
  1237. break;
  1238. case 14: /* AFE - read dword */
  1239. ret = cx231xx_read_i2c_data(dev, AFE_DEVICE_ADDRESS,
  1240. (u16)reg->reg, 2, &data, 4);
  1241. reg->val = le32_to_cpu(data);
  1242. break;
  1243. case 2: /* Video Block - read byte */
  1244. ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
  1245. (u16)reg->reg, 2, &data, 1);
  1246. reg->val = le32_to_cpu(data & 0xff);
  1247. break;
  1248. case 24: /* Video Block - read dword */
  1249. ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
  1250. (u16)reg->reg, 2, &data, 4);
  1251. reg->val = le32_to_cpu(data);
  1252. break;
  1253. case 3: /* I2S block - read byte */
  1254. ret = cx231xx_read_i2c_data(dev,
  1255. I2S_BLK_DEVICE_ADDRESS,
  1256. (u16)reg->reg, 1,
  1257. &data, 1);
  1258. reg->val = le32_to_cpu(data & 0xff);
  1259. break;
  1260. case 34: /* I2S Block - read dword */
  1261. ret =
  1262. cx231xx_read_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
  1263. (u16)reg->reg, 1, &data, 4);
  1264. reg->val = le32_to_cpu(data);
  1265. break;
  1266. }
  1267. return ret < 0 ? ret : 0;
  1268. case V4L2_CHIP_MATCH_I2C_DRIVER:
  1269. call_all(dev, core, g_register, reg);
  1270. return 0;
  1271. case V4L2_CHIP_MATCH_I2C_ADDR:/*for register debug*/
  1272. switch (reg->match.addr) {
  1273. case 0: /* Cx231xx - internal registers */
  1274. ret = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER,
  1275. (u16)reg->reg, value, 4);
  1276. reg->val = value[0] | value[1] << 8 |
  1277. value[2] << 16 | value[3] << 24;
  1278. break;
  1279. case 0x600:/* AFE - read byte */
  1280. ret = cx231xx_read_i2c_master(dev, AFE_DEVICE_ADDRESS,
  1281. (u16)reg->reg, 2,
  1282. &data, 1 , 0);
  1283. reg->val = le32_to_cpu(data & 0xff);
  1284. break;
  1285. case 0x880:/* Video Block - read byte */
  1286. if (reg->reg < 0x0b) {
  1287. ret = cx231xx_read_i2c_master(dev,
  1288. VID_BLK_I2C_ADDRESS,
  1289. (u16)reg->reg, 2,
  1290. &data, 1 , 0);
  1291. reg->val = le32_to_cpu(data & 0xff);
  1292. } else {
  1293. ret = cx231xx_read_i2c_master(dev,
  1294. VID_BLK_I2C_ADDRESS,
  1295. (u16)reg->reg, 2,
  1296. &data, 4 , 0);
  1297. reg->val = le32_to_cpu(data);
  1298. }
  1299. break;
  1300. case 0x980:
  1301. ret = cx231xx_read_i2c_master(dev,
  1302. I2S_BLK_DEVICE_ADDRESS,
  1303. (u16)reg->reg, 1,
  1304. &data, 1 , 0);
  1305. reg->val = le32_to_cpu(data & 0xff);
  1306. break;
  1307. case 0x400:
  1308. ret =
  1309. cx231xx_read_i2c_master(dev, 0x40,
  1310. (u16)reg->reg, 1,
  1311. &data, 1 , 0);
  1312. reg->val = le32_to_cpu(data & 0xff);
  1313. break;
  1314. case 0xc01:
  1315. ret =
  1316. cx231xx_read_i2c_master(dev, 0xc0,
  1317. (u16)reg->reg, 2,
  1318. &data, 38, 1);
  1319. reg->val = le32_to_cpu(data);
  1320. break;
  1321. case 0x022:
  1322. ret =
  1323. cx231xx_read_i2c_master(dev, 0x02,
  1324. (u16)reg->reg, 1,
  1325. &data, 1, 2);
  1326. reg->val = le32_to_cpu(data & 0xff);
  1327. break;
  1328. case 0x322:
  1329. ret = cx231xx_read_i2c_master(dev,
  1330. 0x32,
  1331. (u16)reg->reg, 1,
  1332. &data, 4 , 2);
  1333. reg->val = le32_to_cpu(data);
  1334. break;
  1335. case 0x342:
  1336. ret = cx231xx_read_i2c_master(dev,
  1337. 0x34,
  1338. (u16)reg->reg, 1,
  1339. &data, 4 , 2);
  1340. reg->val = le32_to_cpu(data);
  1341. break;
  1342. default:
  1343. cx231xx_info("no match device address!!\n");
  1344. break;
  1345. }
  1346. return ret < 0 ? ret : 0;
  1347. /*return -EINVAL;*/
  1348. default:
  1349. if (!v4l2_chip_match_host(&reg->match))
  1350. return -EINVAL;
  1351. }
  1352. call_all(dev, core, g_register, reg);
  1353. return ret;
  1354. }
  1355. static int vidioc_s_register(struct file *file, void *priv,
  1356. struct v4l2_dbg_register *reg)
  1357. {
  1358. struct cx231xx_fh *fh = priv;
  1359. struct cx231xx *dev = fh->dev;
  1360. int ret = 0;
  1361. __le64 buf;
  1362. u32 value;
  1363. u8 data[4] = { 0, 0, 0, 0 };
  1364. buf = cpu_to_le64(reg->val);
  1365. switch (reg->match.type) {
  1366. case V4L2_CHIP_MATCH_HOST:
  1367. {
  1368. value = (u32) buf & 0xffffffff;
  1369. switch (reg->match.addr) {
  1370. case 0: /* cx231xx internal registers */
  1371. data[0] = (u8) value;
  1372. data[1] = (u8) (value >> 8);
  1373. data[2] = (u8) (value >> 16);
  1374. data[3] = (u8) (value >> 24);
  1375. ret = cx231xx_write_ctrl_reg(dev,
  1376. VRT_SET_REGISTER,
  1377. (u16)reg->reg, data,
  1378. 4);
  1379. break;
  1380. case 1: /* AFE - read byte */
  1381. ret = cx231xx_write_i2c_data(dev,
  1382. AFE_DEVICE_ADDRESS,
  1383. (u16)reg->reg, 2,
  1384. value, 1);
  1385. break;
  1386. case 14: /* AFE - read dword */
  1387. ret = cx231xx_write_i2c_data(dev,
  1388. AFE_DEVICE_ADDRESS,
  1389. (u16)reg->reg, 2,
  1390. value, 4);
  1391. break;
  1392. case 2: /* Video Block - read byte */
  1393. ret =
  1394. cx231xx_write_i2c_data(dev,
  1395. VID_BLK_I2C_ADDRESS,
  1396. (u16)reg->reg, 2,
  1397. value, 1);
  1398. break;
  1399. case 24: /* Video Block - read dword */
  1400. ret =
  1401. cx231xx_write_i2c_data(dev,
  1402. VID_BLK_I2C_ADDRESS,
  1403. (u16)reg->reg, 2,
  1404. value, 4);
  1405. break;
  1406. case 3: /* I2S block - read byte */
  1407. ret =
  1408. cx231xx_write_i2c_data(dev,
  1409. I2S_BLK_DEVICE_ADDRESS,
  1410. (u16)reg->reg, 1,
  1411. value, 1);
  1412. break;
  1413. case 34: /* I2S block - read dword */
  1414. ret =
  1415. cx231xx_write_i2c_data(dev,
  1416. I2S_BLK_DEVICE_ADDRESS,
  1417. (u16)reg->reg, 1,
  1418. value, 4);
  1419. break;
  1420. }
  1421. }
  1422. return ret < 0 ? ret : 0;
  1423. case V4L2_CHIP_MATCH_I2C_ADDR:
  1424. {
  1425. value = (u32) buf & 0xffffffff;
  1426. switch (reg->match.addr) {
  1427. case 0:/*cx231xx internal registers*/
  1428. data[0] = (u8) value;
  1429. data[1] = (u8) (value >> 8);
  1430. data[2] = (u8) (value >> 16);
  1431. data[3] = (u8) (value >> 24);
  1432. ret = cx231xx_write_ctrl_reg(dev,
  1433. VRT_SET_REGISTER,
  1434. (u16)reg->reg, data,
  1435. 4);
  1436. break;
  1437. case 0x600:/* AFE - read byte */
  1438. ret = cx231xx_write_i2c_master(dev,
  1439. AFE_DEVICE_ADDRESS,
  1440. (u16)reg->reg, 2,
  1441. value, 1 , 0);
  1442. break;
  1443. case 0x880:/* Video Block - read byte */
  1444. if (reg->reg < 0x0b)
  1445. cx231xx_write_i2c_master(dev,
  1446. VID_BLK_I2C_ADDRESS,
  1447. (u16)reg->reg, 2,
  1448. value, 1, 0);
  1449. else
  1450. cx231xx_write_i2c_master(dev,
  1451. VID_BLK_I2C_ADDRESS,
  1452. (u16)reg->reg, 2,
  1453. value, 4, 0);
  1454. break;
  1455. case 0x980:
  1456. ret =
  1457. cx231xx_write_i2c_master(dev,
  1458. I2S_BLK_DEVICE_ADDRESS,
  1459. (u16)reg->reg, 1,
  1460. value, 1, 0);
  1461. break;
  1462. case 0x400:
  1463. ret =
  1464. cx231xx_write_i2c_master(dev,
  1465. 0x40,
  1466. (u16)reg->reg, 1,
  1467. value, 1, 0);
  1468. break;
  1469. case 0xc01:
  1470. ret =
  1471. cx231xx_write_i2c_master(dev,
  1472. 0xc0,
  1473. (u16)reg->reg, 1,
  1474. value, 1, 1);
  1475. break;
  1476. case 0x022:
  1477. ret =
  1478. cx231xx_write_i2c_master(dev,
  1479. 0x02,
  1480. (u16)reg->reg, 1,
  1481. value, 1, 2);
  1482. case 0x322:
  1483. ret =
  1484. cx231xx_write_i2c_master(dev,
  1485. 0x32,
  1486. (u16)reg->reg, 1,
  1487. value, 4, 2);
  1488. break;
  1489. case 0x342:
  1490. ret =
  1491. cx231xx_write_i2c_master(dev,
  1492. 0x34,
  1493. (u16)reg->reg, 1,
  1494. value, 4, 2);
  1495. break;
  1496. default:
  1497. cx231xx_info("no match device address, "
  1498. "the value is %x\n", reg->match.addr);
  1499. break;
  1500. }
  1501. }
  1502. default:
  1503. break;
  1504. }
  1505. call_all(dev, core, s_register, reg);
  1506. return ret;
  1507. }
  1508. #endif
  1509. static int vidioc_cropcap(struct file *file, void *priv,
  1510. struct v4l2_cropcap *cc)
  1511. {
  1512. struct cx231xx_fh *fh = priv;
  1513. struct cx231xx *dev = fh->dev;
  1514. if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1515. return -EINVAL;
  1516. cc->bounds.left = 0;
  1517. cc->bounds.top = 0;
  1518. cc->bounds.width = dev->width;
  1519. cc->bounds.height = dev->height;
  1520. cc->defrect = cc->bounds;
  1521. cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
  1522. cc->pixelaspect.denominator = 59;
  1523. return 0;
  1524. }
  1525. static int vidioc_streamon(struct file *file, void *priv,
  1526. enum v4l2_buf_type type)
  1527. {
  1528. struct cx231xx_fh *fh = priv;
  1529. struct cx231xx *dev = fh->dev;
  1530. int rc;
  1531. rc = check_dev(dev);
  1532. if (rc < 0)
  1533. return rc;
  1534. rc = res_get(fh);
  1535. if (likely(rc >= 0))
  1536. rc = videobuf_streamon(&fh->vb_vidq);
  1537. call_all(dev, video, s_stream, 1);
  1538. return rc;
  1539. }
  1540. static int vidioc_streamoff(struct file *file, void *priv,
  1541. enum v4l2_buf_type type)
  1542. {
  1543. struct cx231xx_fh *fh = priv;
  1544. struct cx231xx *dev = fh->dev;
  1545. int rc;
  1546. rc = check_dev(dev);
  1547. if (rc < 0)
  1548. return rc;
  1549. if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
  1550. (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE))
  1551. return -EINVAL;
  1552. if (type != fh->type)
  1553. return -EINVAL;
  1554. cx25840_call(dev, video, s_stream, 0);
  1555. videobuf_streamoff(&fh->vb_vidq);
  1556. res_free(fh);
  1557. return 0;
  1558. }
  1559. static int vidioc_querycap(struct file *file, void *priv,
  1560. struct v4l2_capability *cap)
  1561. {
  1562. struct cx231xx_fh *fh = priv;
  1563. struct cx231xx *dev = fh->dev;
  1564. strlcpy(cap->driver, "cx231xx", sizeof(cap->driver));
  1565. strlcpy(cap->card, cx231xx_boards[dev->model].name, sizeof(cap->card));
  1566. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  1567. cap->capabilities = V4L2_CAP_VBI_CAPTURE |
  1568. #if 0
  1569. V4L2_CAP_SLICED_VBI_CAPTURE |
  1570. #endif
  1571. V4L2_CAP_VIDEO_CAPTURE |
  1572. V4L2_CAP_AUDIO |
  1573. V4L2_CAP_READWRITE |
  1574. V4L2_CAP_STREAMING;
  1575. if (dev->tuner_type != TUNER_ABSENT)
  1576. cap->capabilities |= V4L2_CAP_TUNER;
  1577. return 0;
  1578. }
  1579. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  1580. struct v4l2_fmtdesc *f)
  1581. {
  1582. if (unlikely(f->index >= ARRAY_SIZE(format)))
  1583. return -EINVAL;
  1584. strlcpy(f->description, format[f->index].name, sizeof(f->description));
  1585. f->pixelformat = format[f->index].fourcc;
  1586. return 0;
  1587. }
  1588. /* Sliced VBI ioctls */
  1589. static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
  1590. struct v4l2_format *f)
  1591. {
  1592. struct cx231xx_fh *fh = priv;
  1593. struct cx231xx *dev = fh->dev;
  1594. int rc;
  1595. rc = check_dev(dev);
  1596. if (rc < 0)
  1597. return rc;
  1598. f->fmt.sliced.service_set = 0;
  1599. call_all(dev, vbi, g_sliced_fmt, &f->fmt.sliced);
  1600. if (f->fmt.sliced.service_set == 0)
  1601. rc = -EINVAL;
  1602. return rc;
  1603. }
  1604. static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
  1605. struct v4l2_format *f)
  1606. {
  1607. struct cx231xx_fh *fh = priv;
  1608. struct cx231xx *dev = fh->dev;
  1609. int rc;
  1610. rc = check_dev(dev);
  1611. if (rc < 0)
  1612. return rc;
  1613. call_all(dev, vbi, g_sliced_fmt, &f->fmt.sliced);
  1614. if (f->fmt.sliced.service_set == 0)
  1615. return -EINVAL;
  1616. return 0;
  1617. }
  1618. /* RAW VBI ioctls */
  1619. static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
  1620. struct v4l2_format *f)
  1621. {
  1622. struct cx231xx_fh *fh = priv;
  1623. struct cx231xx *dev = fh->dev;
  1624. f->fmt.vbi.sampling_rate = 6750000 * 4;
  1625. f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
  1626. f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  1627. f->fmt.vbi.offset = 0;
  1628. f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
  1629. PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
  1630. f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
  1631. PAL_VBI_LINES : NTSC_VBI_LINES;
  1632. f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
  1633. PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
  1634. f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
  1635. return 0;
  1636. }
  1637. static int vidioc_try_fmt_vbi_cap(struct file *file, void *priv,
  1638. struct v4l2_format *f)
  1639. {
  1640. struct cx231xx_fh *fh = priv;
  1641. struct cx231xx *dev = fh->dev;
  1642. if (dev->vbi_stream_on && !fh->stream_on) {
  1643. cx231xx_errdev("%s device in use by another fh\n", __func__);
  1644. return -EBUSY;
  1645. }
  1646. f->type = V4L2_BUF_TYPE_VBI_CAPTURE;
  1647. f->fmt.vbi.sampling_rate = 6750000 * 4;
  1648. f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
  1649. f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  1650. f->fmt.vbi.offset = 0;
  1651. f->fmt.vbi.flags = 0;
  1652. f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
  1653. PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
  1654. f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
  1655. PAL_VBI_LINES : NTSC_VBI_LINES;
  1656. f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
  1657. PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
  1658. f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
  1659. return 0;
  1660. }
  1661. static int vidioc_reqbufs(struct file *file, void *priv,
  1662. struct v4l2_requestbuffers *rb)
  1663. {
  1664. struct cx231xx_fh *fh = priv;
  1665. struct cx231xx *dev = fh->dev;
  1666. int rc;
  1667. rc = check_dev(dev);
  1668. if (rc < 0)
  1669. return rc;
  1670. return videobuf_reqbufs(&fh->vb_vidq, rb);
  1671. }
  1672. static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1673. {
  1674. struct cx231xx_fh *fh = priv;
  1675. struct cx231xx *dev = fh->dev;
  1676. int rc;
  1677. rc = check_dev(dev);
  1678. if (rc < 0)
  1679. return rc;
  1680. return videobuf_querybuf(&fh->vb_vidq, b);
  1681. }
  1682. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1683. {
  1684. struct cx231xx_fh *fh = priv;
  1685. struct cx231xx *dev = fh->dev;
  1686. int rc;
  1687. rc = check_dev(dev);
  1688. if (rc < 0)
  1689. return rc;
  1690. return videobuf_qbuf(&fh->vb_vidq, b);
  1691. }
  1692. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1693. {
  1694. struct cx231xx_fh *fh = priv;
  1695. struct cx231xx *dev = fh->dev;
  1696. int rc;
  1697. rc = check_dev(dev);
  1698. if (rc < 0)
  1699. return rc;
  1700. return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK);
  1701. }
  1702. /* ----------------------------------------------------------- */
  1703. /* RADIO ESPECIFIC IOCTLS */
  1704. /* ----------------------------------------------------------- */
  1705. static int radio_querycap(struct file *file, void *priv,
  1706. struct v4l2_capability *cap)
  1707. {
  1708. struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
  1709. strlcpy(cap->driver, "cx231xx", sizeof(cap->driver));
  1710. strlcpy(cap->card, cx231xx_boards[dev->model].name, sizeof(cap->card));
  1711. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  1712. cap->capabilities = V4L2_CAP_TUNER;
  1713. return 0;
  1714. }
  1715. static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
  1716. {
  1717. struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
  1718. if (unlikely(t->index > 0))
  1719. return -EINVAL;
  1720. strcpy(t->name, "Radio");
  1721. t->type = V4L2_TUNER_RADIO;
  1722. call_all(dev, tuner, s_tuner, t);
  1723. return 0;
  1724. }
  1725. static int radio_enum_input(struct file *file, void *priv, struct v4l2_input *i)
  1726. {
  1727. if (i->index != 0)
  1728. return -EINVAL;
  1729. strcpy(i->name, "Radio");
  1730. i->type = V4L2_INPUT_TYPE_TUNER;
  1731. return 0;
  1732. }
  1733. static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  1734. {
  1735. if (unlikely(a->index))
  1736. return -EINVAL;
  1737. strcpy(a->name, "Radio");
  1738. return 0;
  1739. }
  1740. static int radio_s_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
  1741. {
  1742. struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
  1743. if (0 != t->index)
  1744. return -EINVAL;
  1745. call_all(dev, tuner, s_tuner, t);
  1746. return 0;
  1747. }
  1748. static int radio_s_audio(struct file *file, void *fh, struct v4l2_audio *a)
  1749. {
  1750. return 0;
  1751. }
  1752. static int radio_s_input(struct file *file, void *fh, unsigned int i)
  1753. {
  1754. return 0;
  1755. }
  1756. static int radio_queryctrl(struct file *file, void *priv,
  1757. struct v4l2_queryctrl *c)
  1758. {
  1759. int i;
  1760. if (c->id < V4L2_CID_BASE || c->id >= V4L2_CID_LASTP1)
  1761. return -EINVAL;
  1762. if (c->id == V4L2_CID_AUDIO_MUTE) {
  1763. for (i = 0; i < CX231XX_CTLS; i++) {
  1764. if (cx231xx_ctls[i].v.id == c->id)
  1765. break;
  1766. }
  1767. if (i == CX231XX_CTLS)
  1768. return -EINVAL;
  1769. *c = cx231xx_ctls[i].v;
  1770. } else
  1771. *c = no_ctl;
  1772. return 0;
  1773. }
  1774. /*
  1775. * cx231xx_v4l2_open()
  1776. * inits the device and starts isoc transfer
  1777. */
  1778. static int cx231xx_v4l2_open(struct file *filp)
  1779. {
  1780. int errCode = 0, radio = 0;
  1781. struct video_device *vdev = video_devdata(filp);
  1782. struct cx231xx *dev = video_drvdata(filp);
  1783. struct cx231xx_fh *fh;
  1784. enum v4l2_buf_type fh_type = 0;
  1785. switch (vdev->vfl_type) {
  1786. case VFL_TYPE_GRABBER:
  1787. fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1788. break;
  1789. case VFL_TYPE_VBI:
  1790. fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
  1791. break;
  1792. case VFL_TYPE_RADIO:
  1793. radio = 1;
  1794. break;
  1795. }
  1796. cx231xx_videodbg("open dev=%s type=%s users=%d\n",
  1797. video_device_node_name(vdev), v4l2_type_names[fh_type],
  1798. dev->users);
  1799. #if 0
  1800. errCode = cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
  1801. if (errCode < 0) {
  1802. cx231xx_errdev
  1803. ("Device locked on digital mode. Can't open analog\n");
  1804. return -EBUSY;
  1805. }
  1806. #endif
  1807. fh = kzalloc(sizeof(struct cx231xx_fh), GFP_KERNEL);
  1808. if (!fh) {
  1809. cx231xx_errdev("cx231xx-video.c: Out of memory?!\n");
  1810. return -ENOMEM;
  1811. }
  1812. fh->dev = dev;
  1813. fh->radio = radio;
  1814. fh->type = fh_type;
  1815. filp->private_data = fh;
  1816. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
  1817. dev->width = norm_maxw(dev);
  1818. dev->height = norm_maxh(dev);
  1819. /* Power up in Analog TV mode */
  1820. if (dev->board.external_av)
  1821. cx231xx_set_power_mode(dev,
  1822. POLARIS_AVMODE_ENXTERNAL_AV);
  1823. else
  1824. cx231xx_set_power_mode(dev, POLARIS_AVMODE_ANALOGT_TV);
  1825. #if 0
  1826. cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
  1827. #endif
  1828. /* set video alternate setting */
  1829. cx231xx_set_video_alternate(dev);
  1830. /* Needed, since GPIO might have disabled power of
  1831. some i2c device */
  1832. cx231xx_config_i2c(dev);
  1833. /* device needs to be initialized before isoc transfer */
  1834. dev->video_input = dev->video_input > 2 ? 2 : dev->video_input;
  1835. }
  1836. if (fh->radio) {
  1837. cx231xx_videodbg("video_open: setting radio device\n");
  1838. /* cx231xx_start_radio(dev); */
  1839. call_all(dev, tuner, s_radio);
  1840. }
  1841. dev->users++;
  1842. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1843. videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_video_qops,
  1844. NULL, &dev->video_mode.slock,
  1845. fh->type, V4L2_FIELD_INTERLACED,
  1846. sizeof(struct cx231xx_buffer),
  1847. fh, &dev->lock);
  1848. if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
  1849. /* Set the required alternate setting VBI interface works in
  1850. Bulk mode only */
  1851. cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
  1852. videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_vbi_qops,
  1853. NULL, &dev->vbi_mode.slock,
  1854. fh->type, V4L2_FIELD_SEQ_TB,
  1855. sizeof(struct cx231xx_buffer),
  1856. fh, &dev->lock);
  1857. }
  1858. return errCode;
  1859. }
  1860. /*
  1861. * cx231xx_realease_resources()
  1862. * unregisters the v4l2,i2c and usb devices
  1863. * called when the device gets disconected or at module unload
  1864. */
  1865. void cx231xx_release_analog_resources(struct cx231xx *dev)
  1866. {
  1867. /*FIXME: I2C IR should be disconnected */
  1868. if (dev->radio_dev) {
  1869. if (video_is_registered(dev->radio_dev))
  1870. video_unregister_device(dev->radio_dev);
  1871. else
  1872. video_device_release(dev->radio_dev);
  1873. dev->radio_dev = NULL;
  1874. }
  1875. if (dev->vbi_dev) {
  1876. cx231xx_info("V4L2 device %s deregistered\n",
  1877. video_device_node_name(dev->vbi_dev));
  1878. if (video_is_registered(dev->vbi_dev))
  1879. video_unregister_device(dev->vbi_dev);
  1880. else
  1881. video_device_release(dev->vbi_dev);
  1882. dev->vbi_dev = NULL;
  1883. }
  1884. if (dev->vdev) {
  1885. cx231xx_info("V4L2 device %s deregistered\n",
  1886. video_device_node_name(dev->vdev));
  1887. if (dev->board.has_417)
  1888. cx231xx_417_unregister(dev);
  1889. if (video_is_registered(dev->vdev))
  1890. video_unregister_device(dev->vdev);
  1891. else
  1892. video_device_release(dev->vdev);
  1893. dev->vdev = NULL;
  1894. }
  1895. }
  1896. /*
  1897. * cx231xx_v4l2_close()
  1898. * stops streaming and deallocates all resources allocated by the v4l2
  1899. * calls and ioctls
  1900. */
  1901. static int cx231xx_v4l2_close(struct file *filp)
  1902. {
  1903. struct cx231xx_fh *fh = filp->private_data;
  1904. struct cx231xx *dev = fh->dev;
  1905. cx231xx_videodbg("users=%d\n", dev->users);
  1906. cx231xx_videodbg("users=%d\n", dev->users);
  1907. if (res_check(fh))
  1908. res_free(fh);
  1909. /*
  1910. * To workaround error number=-71 on EP0 for VideoGrabber,
  1911. * need exclude following.
  1912. * FIXME: It is probably safe to remove most of these, as we're
  1913. * now avoiding the alternate setting for INDEX_VANC
  1914. */
  1915. if (!dev->board.no_alt_vanc)
  1916. if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
  1917. videobuf_stop(&fh->vb_vidq);
  1918. videobuf_mmap_free(&fh->vb_vidq);
  1919. /* the device is already disconnect,
  1920. free the remaining resources */
  1921. if (dev->state & DEV_DISCONNECTED) {
  1922. if (atomic_read(&dev->devlist_count) > 0) {
  1923. cx231xx_release_resources(dev);
  1924. kfree(dev);
  1925. dev = NULL;
  1926. return 0;
  1927. }
  1928. return 0;
  1929. }
  1930. /* do this before setting alternate! */
  1931. cx231xx_uninit_vbi_isoc(dev);
  1932. /* set alternate 0 */
  1933. if (!dev->vbi_or_sliced_cc_mode)
  1934. cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
  1935. else
  1936. cx231xx_set_alt_setting(dev, INDEX_HANC, 0);
  1937. kfree(fh);
  1938. dev->users--;
  1939. wake_up_interruptible_nr(&dev->open, 1);
  1940. return 0;
  1941. }
  1942. if (dev->users == 1) {
  1943. videobuf_stop(&fh->vb_vidq);
  1944. videobuf_mmap_free(&fh->vb_vidq);
  1945. /* the device is already disconnect,
  1946. free the remaining resources */
  1947. if (dev->state & DEV_DISCONNECTED) {
  1948. cx231xx_release_resources(dev);
  1949. kfree(dev);
  1950. dev = NULL;
  1951. return 0;
  1952. }
  1953. /* Save some power by putting tuner to sleep */
  1954. call_all(dev, core, s_power, 0);
  1955. /* do this before setting alternate! */
  1956. if (dev->USE_ISO)
  1957. cx231xx_uninit_isoc(dev);
  1958. else
  1959. cx231xx_uninit_bulk(dev);
  1960. cx231xx_set_mode(dev, CX231XX_SUSPEND);
  1961. /* set alternate 0 */
  1962. cx231xx_set_alt_setting(dev, INDEX_VIDEO, 0);
  1963. }
  1964. kfree(fh);
  1965. dev->users--;
  1966. wake_up_interruptible_nr(&dev->open, 1);
  1967. return 0;
  1968. }
  1969. /*
  1970. * cx231xx_v4l2_read()
  1971. * will allocate buffers when called for the first time
  1972. */
  1973. static ssize_t
  1974. cx231xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
  1975. loff_t *pos)
  1976. {
  1977. struct cx231xx_fh *fh = filp->private_data;
  1978. struct cx231xx *dev = fh->dev;
  1979. int rc;
  1980. rc = check_dev(dev);
  1981. if (rc < 0)
  1982. return rc;
  1983. if ((fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
  1984. (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)) {
  1985. rc = res_get(fh);
  1986. if (unlikely(rc < 0))
  1987. return rc;
  1988. return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
  1989. filp->f_flags & O_NONBLOCK);
  1990. }
  1991. return 0;
  1992. }
  1993. /*
  1994. * cx231xx_v4l2_poll()
  1995. * will allocate buffers when called for the first time
  1996. */
  1997. static unsigned int cx231xx_v4l2_poll(struct file *filp, poll_table *wait)
  1998. {
  1999. struct cx231xx_fh *fh = filp->private_data;
  2000. struct cx231xx *dev = fh->dev;
  2001. int rc;
  2002. rc = check_dev(dev);
  2003. if (rc < 0)
  2004. return rc;
  2005. rc = res_get(fh);
  2006. if (unlikely(rc < 0))
  2007. return POLLERR;
  2008. if ((V4L2_BUF_TYPE_VIDEO_CAPTURE == fh->type) ||
  2009. (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type))
  2010. return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
  2011. else
  2012. return POLLERR;
  2013. }
  2014. /*
  2015. * cx231xx_v4l2_mmap()
  2016. */
  2017. static int cx231xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
  2018. {
  2019. struct cx231xx_fh *fh = filp->private_data;
  2020. struct cx231xx *dev = fh->dev;
  2021. int rc;
  2022. rc = check_dev(dev);
  2023. if (rc < 0)
  2024. return rc;
  2025. rc = res_get(fh);
  2026. if (unlikely(rc < 0))
  2027. return rc;
  2028. rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
  2029. cx231xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
  2030. (unsigned long)vma->vm_start,
  2031. (unsigned long)vma->vm_end -
  2032. (unsigned long)vma->vm_start, rc);
  2033. return rc;
  2034. }
  2035. static const struct v4l2_file_operations cx231xx_v4l_fops = {
  2036. .owner = THIS_MODULE,
  2037. .open = cx231xx_v4l2_open,
  2038. .release = cx231xx_v4l2_close,
  2039. .read = cx231xx_v4l2_read,
  2040. .poll = cx231xx_v4l2_poll,
  2041. .mmap = cx231xx_v4l2_mmap,
  2042. .unlocked_ioctl = video_ioctl2,
  2043. };
  2044. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  2045. .vidioc_querycap = vidioc_querycap,
  2046. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  2047. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  2048. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  2049. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  2050. .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
  2051. .vidioc_try_fmt_vbi_cap = vidioc_try_fmt_vbi_cap,
  2052. .vidioc_s_fmt_vbi_cap = vidioc_try_fmt_vbi_cap,
  2053. .vidioc_g_audio = vidioc_g_audio,
  2054. .vidioc_s_audio = vidioc_s_audio,
  2055. .vidioc_cropcap = vidioc_cropcap,
  2056. .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
  2057. .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
  2058. .vidioc_reqbufs = vidioc_reqbufs,
  2059. .vidioc_querybuf = vidioc_querybuf,
  2060. .vidioc_qbuf = vidioc_qbuf,
  2061. .vidioc_dqbuf = vidioc_dqbuf,
  2062. .vidioc_s_std = vidioc_s_std,
  2063. .vidioc_g_std = vidioc_g_std,
  2064. .vidioc_enum_input = vidioc_enum_input,
  2065. .vidioc_g_input = vidioc_g_input,
  2066. .vidioc_s_input = vidioc_s_input,
  2067. .vidioc_queryctrl = vidioc_queryctrl,
  2068. .vidioc_g_ctrl = vidioc_g_ctrl,
  2069. .vidioc_s_ctrl = vidioc_s_ctrl,
  2070. .vidioc_streamon = vidioc_streamon,
  2071. .vidioc_streamoff = vidioc_streamoff,
  2072. .vidioc_g_tuner = vidioc_g_tuner,
  2073. .vidioc_s_tuner = vidioc_s_tuner,
  2074. .vidioc_g_frequency = vidioc_g_frequency,
  2075. .vidioc_s_frequency = vidioc_s_frequency,
  2076. #ifdef CONFIG_VIDEO_ADV_DEBUG
  2077. .vidioc_g_register = vidioc_g_register,
  2078. .vidioc_s_register = vidioc_s_register,
  2079. #endif
  2080. };
  2081. static struct video_device cx231xx_vbi_template;
  2082. static const struct video_device cx231xx_video_template = {
  2083. .fops = &cx231xx_v4l_fops,
  2084. .release = video_device_release,
  2085. .ioctl_ops = &video_ioctl_ops,
  2086. .tvnorms = V4L2_STD_ALL,
  2087. .current_norm = V4L2_STD_PAL,
  2088. };
  2089. static const struct v4l2_file_operations radio_fops = {
  2090. .owner = THIS_MODULE,
  2091. .open = cx231xx_v4l2_open,
  2092. .release = cx231xx_v4l2_close,
  2093. .ioctl = video_ioctl2,
  2094. };
  2095. static const struct v4l2_ioctl_ops radio_ioctl_ops = {
  2096. .vidioc_querycap = radio_querycap,
  2097. .vidioc_g_tuner = radio_g_tuner,
  2098. .vidioc_enum_input = radio_enum_input,
  2099. .vidioc_g_audio = radio_g_audio,
  2100. .vidioc_s_tuner = radio_s_tuner,
  2101. .vidioc_s_audio = radio_s_audio,
  2102. .vidioc_s_input = radio_s_input,
  2103. .vidioc_queryctrl = radio_queryctrl,
  2104. .vidioc_g_ctrl = vidioc_g_ctrl,
  2105. .vidioc_s_ctrl = vidioc_s_ctrl,
  2106. .vidioc_g_frequency = vidioc_g_frequency,
  2107. .vidioc_s_frequency = vidioc_s_frequency,
  2108. #ifdef CONFIG_VIDEO_ADV_DEBUG
  2109. .vidioc_g_register = vidioc_g_register,
  2110. .vidioc_s_register = vidioc_s_register,
  2111. #endif
  2112. };
  2113. static struct video_device cx231xx_radio_template = {
  2114. .name = "cx231xx-radio",
  2115. .fops = &radio_fops,
  2116. .ioctl_ops = &radio_ioctl_ops,
  2117. };
  2118. /******************************** usb interface ******************************/
  2119. static struct video_device *cx231xx_vdev_init(struct cx231xx *dev,
  2120. const struct video_device
  2121. *template, const char *type_name)
  2122. {
  2123. struct video_device *vfd;
  2124. vfd = video_device_alloc();
  2125. if (NULL == vfd)
  2126. return NULL;
  2127. *vfd = *template;
  2128. vfd->v4l2_dev = &dev->v4l2_dev;
  2129. vfd->release = video_device_release;
  2130. vfd->debug = video_debug;
  2131. vfd->lock = &dev->lock;
  2132. snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name);
  2133. video_set_drvdata(vfd, dev);
  2134. return vfd;
  2135. }
  2136. int cx231xx_register_analog_devices(struct cx231xx *dev)
  2137. {
  2138. int ret;
  2139. cx231xx_info("%s: v4l2 driver version %s\n",
  2140. dev->name, CX231XX_VERSION);
  2141. /* set default norm */
  2142. /*dev->norm = cx231xx_video_template.current_norm; */
  2143. dev->width = norm_maxw(dev);
  2144. dev->height = norm_maxh(dev);
  2145. dev->interlaced = 0;
  2146. /* Analog specific initialization */
  2147. dev->format = &format[0];
  2148. /* Set the initial input */
  2149. video_mux(dev, dev->video_input);
  2150. /* Audio defaults */
  2151. dev->mute = 1;
  2152. dev->volume = 0x1f;
  2153. /* enable vbi capturing */
  2154. /* write code here... */
  2155. /* allocate and fill video video_device struct */
  2156. dev->vdev = cx231xx_vdev_init(dev, &cx231xx_video_template, "video");
  2157. if (!dev->vdev) {
  2158. cx231xx_errdev("cannot allocate video_device.\n");
  2159. return -ENODEV;
  2160. }
  2161. /* register v4l2 video video_device */
  2162. ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
  2163. video_nr[dev->devno]);
  2164. if (ret) {
  2165. cx231xx_errdev("unable to register video device (error=%i).\n",
  2166. ret);
  2167. return ret;
  2168. }
  2169. cx231xx_info("%s/0: registered device %s [v4l2]\n",
  2170. dev->name, video_device_node_name(dev->vdev));
  2171. /* Initialize VBI template */
  2172. memcpy(&cx231xx_vbi_template, &cx231xx_video_template,
  2173. sizeof(cx231xx_vbi_template));
  2174. strcpy(cx231xx_vbi_template.name, "cx231xx-vbi");
  2175. /* Allocate and fill vbi video_device struct */
  2176. dev->vbi_dev = cx231xx_vdev_init(dev, &cx231xx_vbi_template, "vbi");
  2177. /* register v4l2 vbi video_device */
  2178. ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
  2179. vbi_nr[dev->devno]);
  2180. if (ret < 0) {
  2181. cx231xx_errdev("unable to register vbi device\n");
  2182. return ret;
  2183. }
  2184. cx231xx_info("%s/0: registered device %s\n",
  2185. dev->name, video_device_node_name(dev->vbi_dev));
  2186. if (cx231xx_boards[dev->model].radio.type == CX231XX_RADIO) {
  2187. dev->radio_dev = cx231xx_vdev_init(dev, &cx231xx_radio_template,
  2188. "radio");
  2189. if (!dev->radio_dev) {
  2190. cx231xx_errdev("cannot allocate video_device.\n");
  2191. return -ENODEV;
  2192. }
  2193. ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
  2194. radio_nr[dev->devno]);
  2195. if (ret < 0) {
  2196. cx231xx_errdev("can't register radio device\n");
  2197. return ret;
  2198. }
  2199. cx231xx_info("Registered radio device as %s\n",
  2200. video_device_node_name(dev->radio_dev));
  2201. }
  2202. cx231xx_info("V4L2 device registered as %s and %s\n",
  2203. video_device_node_name(dev->vdev),
  2204. video_device_node_name(dev->vbi_dev));
  2205. return 0;
  2206. }