cx231xx-video.c 58 KB

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