cx231xx-video.c 62 KB

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