cx231xx-video.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434
  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. 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. dev->hscale = *hscale;
  748. dev->vscale = *vscale;
  749. }
  750. /* ------------------------------------------------------------------
  751. IOCTL vidioc handling
  752. ------------------------------------------------------------------*/
  753. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  754. struct v4l2_format *f)
  755. {
  756. struct cx231xx_fh *fh = priv;
  757. struct cx231xx *dev = fh->dev;
  758. mutex_lock(&dev->lock);
  759. f->fmt.pix.width = dev->width;
  760. f->fmt.pix.height = dev->height;
  761. f->fmt.pix.pixelformat = dev->format->fourcc;;
  762. f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;;
  763. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
  764. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  765. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  766. mutex_unlock(&dev->lock);
  767. return 0;
  768. }
  769. static struct cx231xx_fmt *format_by_fourcc(unsigned int fourcc)
  770. {
  771. unsigned int i;
  772. for (i = 0; i < ARRAY_SIZE(format); i++)
  773. if (format[i].fourcc == fourcc)
  774. return &format[i];
  775. return NULL;
  776. }
  777. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  778. struct v4l2_format *f)
  779. {
  780. struct cx231xx_fh *fh = priv;
  781. struct cx231xx *dev = fh->dev;
  782. int width = f->fmt.pix.width;
  783. int height = f->fmt.pix.height;
  784. unsigned int maxw = norm_maxw(dev);
  785. unsigned int maxh = norm_maxh(dev);
  786. unsigned int hscale, vscale;
  787. struct cx231xx_fmt *fmt;
  788. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  789. if (!fmt) {
  790. cx231xx_videodbg("Fourcc format (%08x) invalid.\n",
  791. f->fmt.pix.pixelformat);
  792. return -EINVAL;
  793. }
  794. /* width must even because of the YUYV format
  795. height must be even because of interlacing */
  796. height &= 0xfffe;
  797. width &= 0xfffe;
  798. if (unlikely(height < 32))
  799. height = 32;
  800. if (unlikely(height > maxh))
  801. height = maxh;
  802. if (unlikely(width < 48))
  803. width = 48;
  804. if (unlikely(width > maxw))
  805. width = maxw;
  806. get_scale(dev, width, height, &hscale, &vscale);
  807. width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
  808. height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
  809. f->fmt.pix.width = width;
  810. f->fmt.pix.height = height;
  811. f->fmt.pix.pixelformat = fmt->fourcc;
  812. f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
  813. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
  814. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  815. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  816. return 0;
  817. }
  818. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  819. struct v4l2_format *f)
  820. {
  821. struct cx231xx_fh *fh = priv;
  822. struct cx231xx *dev = fh->dev;
  823. int rc;
  824. struct cx231xx_fmt *fmt;
  825. rc = check_dev(dev);
  826. if (rc < 0)
  827. return rc;
  828. mutex_lock(&dev->lock);
  829. vidioc_try_fmt_vid_cap(file, priv, f);
  830. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  831. if (!fmt) {
  832. rc = -EINVAL;
  833. goto out;
  834. }
  835. if (videobuf_queue_is_busy(&fh->vb_vidq)) {
  836. cx231xx_errdev("%s queue busy\n", __func__);
  837. rc = -EBUSY;
  838. goto out;
  839. }
  840. if (dev->stream_on && !fh->stream_on) {
  841. cx231xx_errdev("%s device in use by another fh\n", __func__);
  842. rc = -EBUSY;
  843. goto out;
  844. }
  845. /* set new image size */
  846. dev->width = f->fmt.pix.width;
  847. dev->height = f->fmt.pix.height;
  848. dev->format = fmt;
  849. get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
  850. call_all(dev, video, s_fmt, f);
  851. /* Set the correct alternate setting for this resolution */
  852. cx231xx_resolution_set(dev);
  853. out:
  854. mutex_unlock(&dev->lock);
  855. return rc;
  856. }
  857. static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id * id)
  858. {
  859. struct cx231xx_fh *fh = priv;
  860. struct cx231xx *dev = fh->dev;
  861. *id = dev->norm;
  862. return 0;
  863. }
  864. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
  865. {
  866. struct cx231xx_fh *fh = priv;
  867. struct cx231xx *dev = fh->dev;
  868. struct v4l2_format f;
  869. int rc;
  870. rc = check_dev(dev);
  871. if (rc < 0)
  872. return rc;
  873. cx231xx_info("vidioc_s_std : 0x%x\n", (unsigned int)*norm);
  874. mutex_lock(&dev->lock);
  875. dev->norm = *norm;
  876. /* Adjusts width/height, if needed */
  877. f.fmt.pix.width = dev->width;
  878. f.fmt.pix.height = dev->height;
  879. vidioc_try_fmt_vid_cap(file, priv, &f);
  880. /* set new image size */
  881. dev->width = f.fmt.pix.width;
  882. dev->height = f.fmt.pix.height;
  883. get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
  884. call_all(dev, core, s_std, dev->norm);
  885. mutex_unlock(&dev->lock);
  886. cx231xx_resolution_set(dev);
  887. /* do mode control overrides */
  888. cx231xx_do_mode_ctrl_overrides(dev);
  889. return 0;
  890. }
  891. static const char *iname[] = {
  892. [CX231XX_VMUX_COMPOSITE1] = "Composite1",
  893. [CX231XX_VMUX_SVIDEO] = "S-Video",
  894. [CX231XX_VMUX_TELEVISION] = "Television",
  895. [CX231XX_VMUX_CABLE] = "Cable TV",
  896. [CX231XX_VMUX_DVB] = "DVB",
  897. [CX231XX_VMUX_DEBUG] = "for debug only",
  898. };
  899. static int vidioc_enum_input(struct file *file, void *priv,
  900. struct v4l2_input *i)
  901. {
  902. struct cx231xx_fh *fh = priv;
  903. struct cx231xx *dev = fh->dev;
  904. unsigned int n;
  905. n = i->index;
  906. if (n >= MAX_CX231XX_INPUT)
  907. return -EINVAL;
  908. if (0 == INPUT(n)->type)
  909. return -EINVAL;
  910. i->index = n;
  911. i->type = V4L2_INPUT_TYPE_CAMERA;
  912. strcpy(i->name, iname[INPUT(n)->type]);
  913. if ((CX231XX_VMUX_TELEVISION == INPUT(n)->type) ||
  914. (CX231XX_VMUX_CABLE == INPUT(n)->type))
  915. i->type = V4L2_INPUT_TYPE_TUNER;
  916. i->std = dev->vdev->tvnorms;
  917. return 0;
  918. }
  919. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  920. {
  921. struct cx231xx_fh *fh = priv;
  922. struct cx231xx *dev = fh->dev;
  923. *i = dev->video_input;
  924. return 0;
  925. }
  926. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  927. {
  928. struct cx231xx_fh *fh = priv;
  929. struct cx231xx *dev = fh->dev;
  930. int rc;
  931. rc = check_dev(dev);
  932. if (rc < 0)
  933. return rc;
  934. if (i >= MAX_CX231XX_INPUT)
  935. return -EINVAL;
  936. if (0 == INPUT(i)->type)
  937. return -EINVAL;
  938. mutex_lock(&dev->lock);
  939. video_mux(dev, i);
  940. mutex_unlock(&dev->lock);
  941. return 0;
  942. }
  943. static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  944. {
  945. struct cx231xx_fh *fh = priv;
  946. struct cx231xx *dev = fh->dev;
  947. switch (a->index) {
  948. case CX231XX_AMUX_VIDEO:
  949. strcpy(a->name, "Television");
  950. break;
  951. case CX231XX_AMUX_LINE_IN:
  952. strcpy(a->name, "Line In");
  953. break;
  954. default:
  955. return -EINVAL;
  956. }
  957. a->index = dev->ctl_ainput;
  958. a->capability = V4L2_AUDCAP_STEREO;
  959. return 0;
  960. }
  961. static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
  962. {
  963. struct cx231xx_fh *fh = priv;
  964. struct cx231xx *dev = fh->dev;
  965. int status = 0;
  966. /* Doesn't allow manual routing */
  967. if (a->index != dev->ctl_ainput)
  968. return -EINVAL;
  969. dev->ctl_ainput = INPUT(a->index)->amux;
  970. status = cx231xx_set_audio_input(dev, dev->ctl_ainput);
  971. return status;
  972. }
  973. static int vidioc_queryctrl(struct file *file, void *priv,
  974. struct v4l2_queryctrl *qc)
  975. {
  976. struct cx231xx_fh *fh = priv;
  977. struct cx231xx *dev = fh->dev;
  978. int id = qc->id;
  979. int i;
  980. int rc;
  981. rc = check_dev(dev);
  982. if (rc < 0)
  983. return rc;
  984. qc->id = v4l2_ctrl_next(ctrl_classes, qc->id);
  985. if (unlikely(qc->id == 0))
  986. return -EINVAL;
  987. memset(qc, 0, sizeof(*qc));
  988. qc->id = id;
  989. if (qc->id < V4L2_CID_BASE || qc->id >= V4L2_CID_LASTP1)
  990. return -EINVAL;
  991. for (i = 0; i < CX231XX_CTLS; i++)
  992. if (cx231xx_ctls[i].v.id == qc->id)
  993. break;
  994. if (i == CX231XX_CTLS) {
  995. *qc = no_ctl;
  996. return 0;
  997. }
  998. *qc = cx231xx_ctls[i].v;
  999. mutex_lock(&dev->lock);
  1000. call_all(dev, core, queryctrl, qc);
  1001. mutex_unlock(&dev->lock);
  1002. if (qc->type)
  1003. return 0;
  1004. else
  1005. return -EINVAL;
  1006. }
  1007. static int vidioc_g_ctrl(struct file *file, void *priv,
  1008. struct v4l2_control *ctrl)
  1009. {
  1010. struct cx231xx_fh *fh = priv;
  1011. struct cx231xx *dev = fh->dev;
  1012. int rc;
  1013. rc = check_dev(dev);
  1014. if (rc < 0)
  1015. return rc;
  1016. mutex_lock(&dev->lock);
  1017. call_all(dev, core, g_ctrl, ctrl);
  1018. mutex_unlock(&dev->lock);
  1019. return rc;
  1020. }
  1021. static int vidioc_s_ctrl(struct file *file, void *priv,
  1022. struct v4l2_control *ctrl)
  1023. {
  1024. struct cx231xx_fh *fh = priv;
  1025. struct cx231xx *dev = fh->dev;
  1026. int rc;
  1027. rc = check_dev(dev);
  1028. if (rc < 0)
  1029. return rc;
  1030. mutex_lock(&dev->lock);
  1031. call_all(dev, core, s_ctrl, ctrl);
  1032. mutex_unlock(&dev->lock);
  1033. return rc;
  1034. }
  1035. static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
  1036. {
  1037. struct cx231xx_fh *fh = priv;
  1038. struct cx231xx *dev = fh->dev;
  1039. int rc;
  1040. rc = check_dev(dev);
  1041. if (rc < 0)
  1042. return rc;
  1043. if (0 != t->index)
  1044. return -EINVAL;
  1045. strcpy(t->name, "Tuner");
  1046. t->type = V4L2_TUNER_ANALOG_TV;
  1047. t->capability = V4L2_TUNER_CAP_NORM;
  1048. t->rangehigh = 0xffffffffUL;
  1049. t->signal = 0xffff; /* LOCKED */
  1050. return 0;
  1051. }
  1052. static int vidioc_s_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
  1053. {
  1054. struct cx231xx_fh *fh = priv;
  1055. struct cx231xx *dev = fh->dev;
  1056. int rc;
  1057. rc = check_dev(dev);
  1058. if (rc < 0)
  1059. return rc;
  1060. if (0 != t->index)
  1061. return -EINVAL;
  1062. #if 0
  1063. mutex_lock(&dev->lock);
  1064. call_all(dev, tuner, s_tuner, t);
  1065. mutex_unlock(&dev->lock);
  1066. #endif
  1067. return 0;
  1068. }
  1069. static int vidioc_g_frequency(struct file *file, void *priv,
  1070. struct v4l2_frequency *f)
  1071. {
  1072. struct cx231xx_fh *fh = priv;
  1073. struct cx231xx *dev = fh->dev;
  1074. mutex_lock(&dev->lock);
  1075. f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1076. f->frequency = dev->ctl_freq;
  1077. call_all(dev, tuner, g_frequency, f);
  1078. mutex_unlock(&dev->lock);
  1079. return 0;
  1080. }
  1081. static int vidioc_s_frequency(struct file *file, void *priv,
  1082. struct v4l2_frequency *f)
  1083. {
  1084. struct cx231xx_fh *fh = priv;
  1085. struct cx231xx *dev = fh->dev;
  1086. int rc;
  1087. rc = check_dev(dev);
  1088. if (rc < 0)
  1089. return rc;
  1090. if (0 != f->tuner)
  1091. return -EINVAL;
  1092. if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
  1093. return -EINVAL;
  1094. if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
  1095. return -EINVAL;
  1096. /* set pre channel change settings in DIF first */
  1097. rc = cx231xx_tuner_pre_channel_change(dev);
  1098. mutex_lock(&dev->lock);
  1099. dev->ctl_freq = f->frequency;
  1100. if (dev->tuner_type == TUNER_XC5000) {
  1101. if (dev->cx231xx_set_analog_freq != NULL)
  1102. dev->cx231xx_set_analog_freq(dev, f->frequency);
  1103. } else
  1104. call_all(dev, tuner, s_frequency, f);
  1105. mutex_unlock(&dev->lock);
  1106. /* set post channel change settings in DIF first */
  1107. rc = cx231xx_tuner_post_channel_change(dev);
  1108. cx231xx_info("Set New FREQUENCY to %d\n", f->frequency);
  1109. return rc;
  1110. }
  1111. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1112. /*
  1113. -R, --list-registers=type=<host/i2cdrv/i2caddr>,
  1114. chip=<chip>[,min=<addr>,max=<addr>]
  1115. dump registers from <min> to <max> [VIDIOC_DBG_G_REGISTER]
  1116. -r, --set-register=type=<host/i2cdrv/i2caddr>,
  1117. chip=<chip>,reg=<addr>,val=<val>
  1118. set the register [VIDIOC_DBG_S_REGISTER]
  1119. if type == host, then <chip> is the hosts chip ID (default 0)
  1120. if type == i2cdrv (default), then <chip> is the I2C driver name or ID
  1121. if type == i2caddr, then <chip> is the 7-bit I2C address
  1122. */
  1123. static int vidioc_g_register(struct file *file, void *priv,
  1124. struct v4l2_dbg_register *reg)
  1125. {
  1126. struct cx231xx_fh *fh = priv;
  1127. struct cx231xx *dev = fh->dev;
  1128. int ret = 0;
  1129. u8 value[4] = { 0, 0, 0, 0 };
  1130. u32 data = 0;
  1131. switch (reg->match.type) {
  1132. case V4L2_CHIP_MATCH_HOST:
  1133. switch (reg->match.addr) {
  1134. case 0: /* Cx231xx - internal registers */
  1135. ret = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER,
  1136. (u16)reg->reg, value, 4);
  1137. reg->val = value[0] | value[1] << 8 |
  1138. value[2] << 16 | value[3] << 24;
  1139. break;
  1140. case 1: /* AFE - read byte */
  1141. ret = cx231xx_read_i2c_data(dev, AFE_DEVICE_ADDRESS,
  1142. (u16)reg->reg, 2, &data, 1);
  1143. reg->val = le32_to_cpu(data & 0xff);
  1144. break;
  1145. case 14: /* AFE - read dword */
  1146. ret = cx231xx_read_i2c_data(dev, AFE_DEVICE_ADDRESS,
  1147. (u16)reg->reg, 2, &data, 4);
  1148. reg->val = le32_to_cpu(data);
  1149. break;
  1150. case 2: /* Video Block - read byte */
  1151. ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
  1152. (u16)reg->reg, 2, &data, 1);
  1153. reg->val = le32_to_cpu(data & 0xff);
  1154. break;
  1155. case 24: /* Video Block - read dword */
  1156. ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
  1157. (u16)reg->reg, 2, &data, 4);
  1158. reg->val = le32_to_cpu(data);
  1159. break;
  1160. case 3: /* I2S block - read byte */
  1161. ret = cx231xx_read_i2c_data(dev,
  1162. I2S_BLK_DEVICE_ADDRESS,
  1163. (u16)reg->reg, 1,
  1164. &data, 1);
  1165. reg->val = le32_to_cpu(data & 0xff);
  1166. break;
  1167. case 34: /* I2S Block - read dword */
  1168. ret =
  1169. cx231xx_read_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
  1170. (u16)reg->reg, 1, &data, 4);
  1171. reg->val = le32_to_cpu(data);
  1172. break;
  1173. }
  1174. return ret < 0 ? ret : 0;
  1175. case V4L2_CHIP_MATCH_I2C_DRIVER:
  1176. call_all(dev, core, g_register, reg);
  1177. return 0;
  1178. case V4L2_CHIP_MATCH_I2C_ADDR:
  1179. /* Not supported yet */
  1180. return -EINVAL;
  1181. default:
  1182. if (!v4l2_chip_match_host(&reg->match))
  1183. return -EINVAL;
  1184. }
  1185. mutex_lock(&dev->lock);
  1186. call_all(dev, core, g_register, reg);
  1187. mutex_unlock(&dev->lock);
  1188. return ret;
  1189. }
  1190. static int vidioc_s_register(struct file *file, void *priv,
  1191. struct v4l2_dbg_register *reg)
  1192. {
  1193. struct cx231xx_fh *fh = priv;
  1194. struct cx231xx *dev = fh->dev;
  1195. int ret = 0;
  1196. __le64 buf;
  1197. u32 value;
  1198. u8 data[4] = { 0, 0, 0, 0 };
  1199. buf = cpu_to_le64(reg->val);
  1200. switch (reg->match.type) {
  1201. case V4L2_CHIP_MATCH_HOST:
  1202. {
  1203. value = (u32) buf & 0xffffffff;
  1204. switch (reg->match.addr) {
  1205. case 0: /* cx231xx internal registers */
  1206. data[0] = (u8) value;
  1207. data[1] = (u8) (value >> 8);
  1208. data[2] = (u8) (value >> 16);
  1209. data[3] = (u8) (value >> 24);
  1210. ret = cx231xx_write_ctrl_reg(dev,
  1211. VRT_SET_REGISTER,
  1212. (u16)reg->reg, data,
  1213. 4);
  1214. break;
  1215. case 1: /* AFE - read byte */
  1216. ret = cx231xx_write_i2c_data(dev,
  1217. AFE_DEVICE_ADDRESS,
  1218. (u16)reg->reg, 2,
  1219. value, 1);
  1220. break;
  1221. case 14: /* AFE - read dword */
  1222. ret = cx231xx_write_i2c_data(dev,
  1223. AFE_DEVICE_ADDRESS,
  1224. (u16)reg->reg, 2,
  1225. value, 4);
  1226. break;
  1227. case 2: /* Video Block - read byte */
  1228. ret =
  1229. cx231xx_write_i2c_data(dev,
  1230. VID_BLK_I2C_ADDRESS,
  1231. (u16)reg->reg, 2,
  1232. value, 1);
  1233. break;
  1234. case 24: /* Video Block - read dword */
  1235. ret =
  1236. cx231xx_write_i2c_data(dev,
  1237. VID_BLK_I2C_ADDRESS,
  1238. (u16)reg->reg, 2,
  1239. value, 4);
  1240. break;
  1241. case 3: /* I2S block - read byte */
  1242. ret =
  1243. cx231xx_write_i2c_data(dev,
  1244. I2S_BLK_DEVICE_ADDRESS,
  1245. (u16)reg->reg, 1,
  1246. value, 1);
  1247. break;
  1248. case 34: /* I2S block - read dword */
  1249. ret =
  1250. cx231xx_write_i2c_data(dev,
  1251. I2S_BLK_DEVICE_ADDRESS,
  1252. (u16)reg->reg, 1,
  1253. value, 4);
  1254. break;
  1255. }
  1256. }
  1257. return ret < 0 ? ret : 0;
  1258. default:
  1259. break;
  1260. }
  1261. mutex_lock(&dev->lock);
  1262. call_all(dev, core, s_register, reg);
  1263. mutex_unlock(&dev->lock);
  1264. return ret;
  1265. }
  1266. #endif
  1267. static int vidioc_cropcap(struct file *file, void *priv,
  1268. struct v4l2_cropcap *cc)
  1269. {
  1270. struct cx231xx_fh *fh = priv;
  1271. struct cx231xx *dev = fh->dev;
  1272. if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1273. return -EINVAL;
  1274. cc->bounds.left = 0;
  1275. cc->bounds.top = 0;
  1276. cc->bounds.width = dev->width;
  1277. cc->bounds.height = dev->height;
  1278. cc->defrect = cc->bounds;
  1279. cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
  1280. cc->pixelaspect.denominator = 59;
  1281. return 0;
  1282. }
  1283. static int vidioc_streamon(struct file *file, void *priv,
  1284. enum v4l2_buf_type type)
  1285. {
  1286. struct cx231xx_fh *fh = priv;
  1287. struct cx231xx *dev = fh->dev;
  1288. int rc;
  1289. rc = check_dev(dev);
  1290. if (rc < 0)
  1291. return rc;
  1292. mutex_lock(&dev->lock);
  1293. rc = res_get(fh);
  1294. if (likely(rc >= 0))
  1295. rc = videobuf_streamon(&fh->vb_vidq);
  1296. call_all(dev, video, s_stream, 1);
  1297. mutex_unlock(&dev->lock);
  1298. return rc;
  1299. }
  1300. static int vidioc_streamoff(struct file *file, void *priv,
  1301. enum v4l2_buf_type type)
  1302. {
  1303. struct cx231xx_fh *fh = priv;
  1304. struct cx231xx *dev = fh->dev;
  1305. int rc;
  1306. rc = check_dev(dev);
  1307. if (rc < 0)
  1308. return rc;
  1309. if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
  1310. (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE))
  1311. return -EINVAL;
  1312. if (type != fh->type)
  1313. return -EINVAL;
  1314. mutex_lock(&dev->lock);
  1315. cx25840_call(dev, video, s_stream, 0);
  1316. videobuf_streamoff(&fh->vb_vidq);
  1317. res_free(fh);
  1318. mutex_unlock(&dev->lock);
  1319. return 0;
  1320. }
  1321. static int vidioc_querycap(struct file *file, void *priv,
  1322. struct v4l2_capability *cap)
  1323. {
  1324. struct cx231xx_fh *fh = priv;
  1325. struct cx231xx *dev = fh->dev;
  1326. strlcpy(cap->driver, "cx231xx", sizeof(cap->driver));
  1327. strlcpy(cap->card, cx231xx_boards[dev->model].name, sizeof(cap->card));
  1328. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  1329. cap->version = CX231XX_VERSION_CODE;
  1330. cap->capabilities = V4L2_CAP_VBI_CAPTURE |
  1331. #if 0
  1332. V4L2_CAP_SLICED_VBI_CAPTURE |
  1333. #endif
  1334. V4L2_CAP_VIDEO_CAPTURE |
  1335. V4L2_CAP_AUDIO |
  1336. V4L2_CAP_READWRITE |
  1337. V4L2_CAP_STREAMING;
  1338. if (dev->tuner_type != TUNER_ABSENT)
  1339. cap->capabilities |= V4L2_CAP_TUNER;
  1340. return 0;
  1341. }
  1342. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  1343. struct v4l2_fmtdesc *f)
  1344. {
  1345. if (unlikely(f->index >= ARRAY_SIZE(format)))
  1346. return -EINVAL;
  1347. strlcpy(f->description, format[f->index].name, sizeof(f->description));
  1348. f->pixelformat = format[f->index].fourcc;
  1349. return 0;
  1350. }
  1351. /* Sliced VBI ioctls */
  1352. static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
  1353. struct v4l2_format *f)
  1354. {
  1355. struct cx231xx_fh *fh = priv;
  1356. struct cx231xx *dev = fh->dev;
  1357. int rc;
  1358. rc = check_dev(dev);
  1359. if (rc < 0)
  1360. return rc;
  1361. mutex_lock(&dev->lock);
  1362. f->fmt.sliced.service_set = 0;
  1363. call_all(dev, video, g_fmt, f);
  1364. if (f->fmt.sliced.service_set == 0)
  1365. rc = -EINVAL;
  1366. mutex_unlock(&dev->lock);
  1367. return rc;
  1368. }
  1369. static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
  1370. struct v4l2_format *f)
  1371. {
  1372. struct cx231xx_fh *fh = priv;
  1373. struct cx231xx *dev = fh->dev;
  1374. int rc;
  1375. rc = check_dev(dev);
  1376. if (rc < 0)
  1377. return rc;
  1378. mutex_lock(&dev->lock);
  1379. call_all(dev, video, g_fmt, f);
  1380. mutex_unlock(&dev->lock);
  1381. if (f->fmt.sliced.service_set == 0)
  1382. return -EINVAL;
  1383. return 0;
  1384. }
  1385. /* RAW VBI ioctls */
  1386. static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
  1387. struct v4l2_format *f)
  1388. {
  1389. struct cx231xx_fh *fh = priv;
  1390. struct cx231xx *dev = fh->dev;
  1391. f->fmt.vbi.sampling_rate = (dev->norm & V4L2_STD_625_50) ?
  1392. 35468950 : 28636363;
  1393. f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
  1394. f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  1395. f->fmt.vbi.offset = 64 * 4;
  1396. f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
  1397. PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
  1398. f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
  1399. PAL_VBI_LINES : NTSC_VBI_LINES;
  1400. f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
  1401. PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
  1402. f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
  1403. return 0;
  1404. }
  1405. static int vidioc_try_fmt_vbi_cap(struct file *file, void *priv,
  1406. struct v4l2_format *f)
  1407. {
  1408. struct cx231xx_fh *fh = priv;
  1409. struct cx231xx *dev = fh->dev;
  1410. if (dev->vbi_stream_on && !fh->stream_on) {
  1411. cx231xx_errdev("%s device in use by another fh\n", __func__);
  1412. return -EBUSY;
  1413. }
  1414. f->type = V4L2_BUF_TYPE_VBI_CAPTURE;
  1415. f->fmt.vbi.sampling_rate = (dev->norm & V4L2_STD_625_50) ?
  1416. 35468950 : 28636363;
  1417. f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
  1418. f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  1419. f->fmt.vbi.offset = 244;
  1420. f->fmt.vbi.flags = 0;
  1421. f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
  1422. PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
  1423. f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
  1424. PAL_VBI_LINES : NTSC_VBI_LINES;
  1425. f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
  1426. PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
  1427. f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
  1428. return 0;
  1429. }
  1430. static int vidioc_reqbufs(struct file *file, void *priv,
  1431. struct v4l2_requestbuffers *rb)
  1432. {
  1433. struct cx231xx_fh *fh = priv;
  1434. struct cx231xx *dev = fh->dev;
  1435. int rc;
  1436. rc = check_dev(dev);
  1437. if (rc < 0)
  1438. return rc;
  1439. return videobuf_reqbufs(&fh->vb_vidq, rb);
  1440. }
  1441. static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1442. {
  1443. struct cx231xx_fh *fh = priv;
  1444. struct cx231xx *dev = fh->dev;
  1445. int rc;
  1446. rc = check_dev(dev);
  1447. if (rc < 0)
  1448. return rc;
  1449. return videobuf_querybuf(&fh->vb_vidq, b);
  1450. }
  1451. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1452. {
  1453. struct cx231xx_fh *fh = priv;
  1454. struct cx231xx *dev = fh->dev;
  1455. int rc;
  1456. rc = check_dev(dev);
  1457. if (rc < 0)
  1458. return rc;
  1459. return videobuf_qbuf(&fh->vb_vidq, b);
  1460. }
  1461. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1462. {
  1463. struct cx231xx_fh *fh = priv;
  1464. struct cx231xx *dev = fh->dev;
  1465. int rc;
  1466. rc = check_dev(dev);
  1467. if (rc < 0)
  1468. return rc;
  1469. return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK);
  1470. }
  1471. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1472. static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
  1473. {
  1474. struct cx231xx_fh *fh = priv;
  1475. return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
  1476. }
  1477. #endif
  1478. /* ----------------------------------------------------------- */
  1479. /* RADIO ESPECIFIC IOCTLS */
  1480. /* ----------------------------------------------------------- */
  1481. static int radio_querycap(struct file *file, void *priv,
  1482. struct v4l2_capability *cap)
  1483. {
  1484. struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
  1485. strlcpy(cap->driver, "cx231xx", sizeof(cap->driver));
  1486. strlcpy(cap->card, cx231xx_boards[dev->model].name, sizeof(cap->card));
  1487. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  1488. cap->version = CX231XX_VERSION_CODE;
  1489. cap->capabilities = V4L2_CAP_TUNER;
  1490. return 0;
  1491. }
  1492. static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
  1493. {
  1494. struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
  1495. if (unlikely(t->index > 0))
  1496. return -EINVAL;
  1497. strcpy(t->name, "Radio");
  1498. t->type = V4L2_TUNER_RADIO;
  1499. mutex_lock(&dev->lock);
  1500. call_all(dev, tuner, s_tuner, t);
  1501. mutex_unlock(&dev->lock);
  1502. return 0;
  1503. }
  1504. static int radio_enum_input(struct file *file, void *priv, struct v4l2_input *i)
  1505. {
  1506. if (i->index != 0)
  1507. return -EINVAL;
  1508. strcpy(i->name, "Radio");
  1509. i->type = V4L2_INPUT_TYPE_TUNER;
  1510. return 0;
  1511. }
  1512. static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  1513. {
  1514. if (unlikely(a->index))
  1515. return -EINVAL;
  1516. strcpy(a->name, "Radio");
  1517. return 0;
  1518. }
  1519. static int radio_s_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
  1520. {
  1521. struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
  1522. if (0 != t->index)
  1523. return -EINVAL;
  1524. mutex_lock(&dev->lock);
  1525. call_all(dev, tuner, s_tuner, t);
  1526. mutex_unlock(&dev->lock);
  1527. return 0;
  1528. }
  1529. static int radio_s_audio(struct file *file, void *fh, struct v4l2_audio *a)
  1530. {
  1531. return 0;
  1532. }
  1533. static int radio_s_input(struct file *file, void *fh, unsigned int i)
  1534. {
  1535. return 0;
  1536. }
  1537. static int radio_queryctrl(struct file *file, void *priv,
  1538. struct v4l2_queryctrl *c)
  1539. {
  1540. int i;
  1541. if (c->id < V4L2_CID_BASE || c->id >= V4L2_CID_LASTP1)
  1542. return -EINVAL;
  1543. if (c->id == V4L2_CID_AUDIO_MUTE) {
  1544. for (i = 0; i < CX231XX_CTLS; i++)
  1545. if (cx231xx_ctls[i].v.id == c->id)
  1546. break;
  1547. *c = cx231xx_ctls[i].v;
  1548. } else
  1549. *c = no_ctl;
  1550. return 0;
  1551. }
  1552. /*
  1553. * cx231xx_v4l2_open()
  1554. * inits the device and starts isoc transfer
  1555. */
  1556. static int cx231xx_v4l2_open(struct file *filp)
  1557. {
  1558. int minor = video_devdata(filp)->minor;
  1559. int errCode = 0, radio = 0;
  1560. struct cx231xx *dev = NULL;
  1561. struct cx231xx_fh *fh;
  1562. enum v4l2_buf_type fh_type = 0;
  1563. dev = cx231xx_get_device(minor, &fh_type, &radio);
  1564. if (NULL == dev)
  1565. return -ENODEV;
  1566. mutex_lock(&dev->lock);
  1567. cx231xx_videodbg("open minor=%d type=%s users=%d\n",
  1568. minor, v4l2_type_names[fh_type], dev->users);
  1569. #if 0
  1570. errCode = cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
  1571. if (errCode < 0) {
  1572. cx231xx_errdev
  1573. ("Device locked on digital mode. Can't open analog\n");
  1574. mutex_unlock(&dev->lock);
  1575. return -EBUSY;
  1576. }
  1577. #endif
  1578. fh = kzalloc(sizeof(struct cx231xx_fh), GFP_KERNEL);
  1579. if (!fh) {
  1580. cx231xx_errdev("cx231xx-video.c: Out of memory?!\n");
  1581. mutex_unlock(&dev->lock);
  1582. return -ENOMEM;
  1583. }
  1584. fh->dev = dev;
  1585. fh->radio = radio;
  1586. fh->type = fh_type;
  1587. filp->private_data = fh;
  1588. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
  1589. dev->width = norm_maxw(dev);
  1590. dev->height = norm_maxh(dev);
  1591. dev->hscale = 0;
  1592. dev->vscale = 0;
  1593. /* Power up in Analog TV mode */
  1594. cx231xx_set_power_mode(dev, POLARIS_AVMODE_ANALOGT_TV);
  1595. #if 0
  1596. cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
  1597. #endif
  1598. cx231xx_resolution_set(dev);
  1599. /* set video alternate setting */
  1600. cx231xx_set_video_alternate(dev);
  1601. /* Needed, since GPIO might have disabled power of
  1602. some i2c device */
  1603. cx231xx_config_i2c(dev);
  1604. /* device needs to be initialized before isoc transfer */
  1605. dev->video_input = dev->video_input > 2 ? 2 : dev->video_input;
  1606. video_mux(dev, dev->video_input);
  1607. }
  1608. if (fh->radio) {
  1609. cx231xx_videodbg("video_open: setting radio device\n");
  1610. /* cx231xx_start_radio(dev); */
  1611. call_all(dev, tuner, s_radio);
  1612. }
  1613. dev->users++;
  1614. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1615. videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_video_qops,
  1616. NULL, &dev->video_mode.slock,
  1617. fh->type, V4L2_FIELD_INTERLACED,
  1618. sizeof(struct cx231xx_buffer), fh);
  1619. if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
  1620. /* Set the required alternate setting VBI interface works in
  1621. Bulk mode only */
  1622. cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
  1623. videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_vbi_qops,
  1624. NULL, &dev->vbi_mode.slock,
  1625. fh->type, V4L2_FIELD_SEQ_TB,
  1626. sizeof(struct cx231xx_buffer), fh);
  1627. }
  1628. mutex_unlock(&dev->lock);
  1629. return errCode;
  1630. }
  1631. /*
  1632. * cx231xx_realease_resources()
  1633. * unregisters the v4l2,i2c and usb devices
  1634. * called when the device gets disconected or at module unload
  1635. */
  1636. void cx231xx_release_analog_resources(struct cx231xx *dev)
  1637. {
  1638. /*FIXME: I2C IR should be disconnected */
  1639. if (dev->radio_dev) {
  1640. if (-1 != dev->radio_dev->minor)
  1641. video_unregister_device(dev->radio_dev);
  1642. else
  1643. video_device_release(dev->radio_dev);
  1644. dev->radio_dev = NULL;
  1645. }
  1646. if (dev->vbi_dev) {
  1647. cx231xx_info("V4L2 device /dev/vbi%d deregistered\n",
  1648. dev->vbi_dev->num);
  1649. if (-1 != dev->vbi_dev->minor)
  1650. video_unregister_device(dev->vbi_dev);
  1651. else
  1652. video_device_release(dev->vbi_dev);
  1653. dev->vbi_dev = NULL;
  1654. }
  1655. if (dev->vdev) {
  1656. cx231xx_info("V4L2 device /dev/video%d deregistered\n",
  1657. dev->vdev->num);
  1658. if (-1 != dev->vdev->minor)
  1659. video_unregister_device(dev->vdev);
  1660. else
  1661. video_device_release(dev->vdev);
  1662. dev->vdev = NULL;
  1663. }
  1664. }
  1665. /*
  1666. * cx231xx_v4l2_close()
  1667. * stops streaming and deallocates all resources allocated by the v4l2
  1668. * calls and ioctls
  1669. */
  1670. static int cx231xx_v4l2_close(struct file *filp)
  1671. {
  1672. struct cx231xx_fh *fh = filp->private_data;
  1673. struct cx231xx *dev = fh->dev;
  1674. cx231xx_videodbg("users=%d\n", dev->users);
  1675. mutex_lock(&dev->lock);
  1676. if (res_check(fh))
  1677. res_free(fh);
  1678. if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
  1679. videobuf_stop(&fh->vb_vidq);
  1680. videobuf_mmap_free(&fh->vb_vidq);
  1681. /* the device is already disconnect,
  1682. free the remaining resources */
  1683. if (dev->state & DEV_DISCONNECTED) {
  1684. cx231xx_release_resources(dev);
  1685. mutex_unlock(&dev->lock);
  1686. kfree(dev);
  1687. return 0;
  1688. }
  1689. /* do this before setting alternate! */
  1690. cx231xx_uninit_vbi_isoc(dev);
  1691. /* set alternate 0 */
  1692. if (!dev->vbi_or_sliced_cc_mode)
  1693. cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
  1694. else
  1695. cx231xx_set_alt_setting(dev, INDEX_HANC, 0);
  1696. kfree(fh);
  1697. dev->users--;
  1698. wake_up_interruptible_nr(&dev->open, 1);
  1699. mutex_unlock(&dev->lock);
  1700. return 0;
  1701. }
  1702. if (dev->users == 1) {
  1703. videobuf_stop(&fh->vb_vidq);
  1704. videobuf_mmap_free(&fh->vb_vidq);
  1705. /* the device is already disconnect,
  1706. free the remaining resources */
  1707. if (dev->state & DEV_DISCONNECTED) {
  1708. cx231xx_release_resources(dev);
  1709. mutex_unlock(&dev->lock);
  1710. kfree(dev);
  1711. return 0;
  1712. }
  1713. /* Save some power by putting tuner to sleep */
  1714. call_all(dev, tuner, s_standby);
  1715. /* do this before setting alternate! */
  1716. cx231xx_uninit_isoc(dev);
  1717. cx231xx_set_mode(dev, CX231XX_SUSPEND);
  1718. /* set alternate 0 */
  1719. cx231xx_set_alt_setting(dev, INDEX_VIDEO, 0);
  1720. }
  1721. kfree(fh);
  1722. dev->users--;
  1723. wake_up_interruptible_nr(&dev->open, 1);
  1724. mutex_unlock(&dev->lock);
  1725. return 0;
  1726. }
  1727. /*
  1728. * cx231xx_v4l2_read()
  1729. * will allocate buffers when called for the first time
  1730. */
  1731. static ssize_t
  1732. cx231xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
  1733. loff_t *pos)
  1734. {
  1735. struct cx231xx_fh *fh = filp->private_data;
  1736. struct cx231xx *dev = fh->dev;
  1737. int rc;
  1738. rc = check_dev(dev);
  1739. if (rc < 0)
  1740. return rc;
  1741. if ((fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
  1742. (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)) {
  1743. mutex_lock(&dev->lock);
  1744. rc = res_get(fh);
  1745. mutex_unlock(&dev->lock);
  1746. if (unlikely(rc < 0))
  1747. return rc;
  1748. return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
  1749. filp->f_flags & O_NONBLOCK);
  1750. }
  1751. return 0;
  1752. }
  1753. /*
  1754. * cx231xx_v4l2_poll()
  1755. * will allocate buffers when called for the first time
  1756. */
  1757. static unsigned int cx231xx_v4l2_poll(struct file *filp, poll_table * wait)
  1758. {
  1759. struct cx231xx_fh *fh = filp->private_data;
  1760. struct cx231xx *dev = fh->dev;
  1761. int rc;
  1762. rc = check_dev(dev);
  1763. if (rc < 0)
  1764. return rc;
  1765. mutex_lock(&dev->lock);
  1766. rc = res_get(fh);
  1767. mutex_unlock(&dev->lock);
  1768. if (unlikely(rc < 0))
  1769. return POLLERR;
  1770. if ((V4L2_BUF_TYPE_VIDEO_CAPTURE == fh->type) ||
  1771. (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type))
  1772. return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
  1773. else
  1774. return POLLERR;
  1775. }
  1776. /*
  1777. * cx231xx_v4l2_mmap()
  1778. */
  1779. static int cx231xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
  1780. {
  1781. struct cx231xx_fh *fh = filp->private_data;
  1782. struct cx231xx *dev = fh->dev;
  1783. int rc;
  1784. rc = check_dev(dev);
  1785. if (rc < 0)
  1786. return rc;
  1787. mutex_lock(&dev->lock);
  1788. rc = res_get(fh);
  1789. mutex_unlock(&dev->lock);
  1790. if (unlikely(rc < 0))
  1791. return rc;
  1792. rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
  1793. cx231xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
  1794. (unsigned long)vma->vm_start,
  1795. (unsigned long)vma->vm_end -
  1796. (unsigned long)vma->vm_start, rc);
  1797. return rc;
  1798. }
  1799. static const struct v4l2_file_operations cx231xx_v4l_fops = {
  1800. .owner = THIS_MODULE,
  1801. .open = cx231xx_v4l2_open,
  1802. .release = cx231xx_v4l2_close,
  1803. .read = cx231xx_v4l2_read,
  1804. .poll = cx231xx_v4l2_poll,
  1805. .mmap = cx231xx_v4l2_mmap,
  1806. .ioctl = video_ioctl2,
  1807. };
  1808. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1809. .vidioc_querycap = vidioc_querycap,
  1810. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1811. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1812. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1813. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1814. .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
  1815. .vidioc_try_fmt_vbi_cap = vidioc_try_fmt_vbi_cap,
  1816. .vidioc_s_fmt_vbi_cap = vidioc_try_fmt_vbi_cap,
  1817. .vidioc_g_audio = vidioc_g_audio,
  1818. .vidioc_s_audio = vidioc_s_audio,
  1819. .vidioc_cropcap = vidioc_cropcap,
  1820. .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
  1821. .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
  1822. .vidioc_reqbufs = vidioc_reqbufs,
  1823. .vidioc_querybuf = vidioc_querybuf,
  1824. .vidioc_qbuf = vidioc_qbuf,
  1825. .vidioc_dqbuf = vidioc_dqbuf,
  1826. .vidioc_s_std = vidioc_s_std,
  1827. .vidioc_g_std = vidioc_g_std,
  1828. .vidioc_enum_input = vidioc_enum_input,
  1829. .vidioc_g_input = vidioc_g_input,
  1830. .vidioc_s_input = vidioc_s_input,
  1831. .vidioc_queryctrl = vidioc_queryctrl,
  1832. .vidioc_g_ctrl = vidioc_g_ctrl,
  1833. .vidioc_s_ctrl = vidioc_s_ctrl,
  1834. .vidioc_streamon = vidioc_streamon,
  1835. .vidioc_streamoff = vidioc_streamoff,
  1836. .vidioc_g_tuner = vidioc_g_tuner,
  1837. .vidioc_s_tuner = vidioc_s_tuner,
  1838. .vidioc_g_frequency = vidioc_g_frequency,
  1839. .vidioc_s_frequency = vidioc_s_frequency,
  1840. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1841. .vidioc_g_register = vidioc_g_register,
  1842. .vidioc_s_register = vidioc_s_register,
  1843. #endif
  1844. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1845. .vidiocgmbuf = vidiocgmbuf,
  1846. #endif
  1847. };
  1848. static struct video_device cx231xx_vbi_template;
  1849. static const struct video_device cx231xx_video_template = {
  1850. .fops = &cx231xx_v4l_fops,
  1851. .release = video_device_release,
  1852. .ioctl_ops = &video_ioctl_ops,
  1853. .minor = -1,
  1854. .tvnorms = V4L2_STD_ALL,
  1855. .current_norm = V4L2_STD_PAL,
  1856. };
  1857. static const struct v4l2_file_operations radio_fops = {
  1858. .owner = THIS_MODULE,
  1859. .open = cx231xx_v4l2_open,
  1860. .release = cx231xx_v4l2_close,
  1861. .ioctl = video_ioctl2,
  1862. };
  1863. static const struct v4l2_ioctl_ops radio_ioctl_ops = {
  1864. .vidioc_querycap = radio_querycap,
  1865. .vidioc_g_tuner = radio_g_tuner,
  1866. .vidioc_enum_input = radio_enum_input,
  1867. .vidioc_g_audio = radio_g_audio,
  1868. .vidioc_s_tuner = radio_s_tuner,
  1869. .vidioc_s_audio = radio_s_audio,
  1870. .vidioc_s_input = radio_s_input,
  1871. .vidioc_queryctrl = radio_queryctrl,
  1872. .vidioc_g_ctrl = vidioc_g_ctrl,
  1873. .vidioc_s_ctrl = vidioc_s_ctrl,
  1874. .vidioc_g_frequency = vidioc_g_frequency,
  1875. .vidioc_s_frequency = vidioc_s_frequency,
  1876. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1877. .vidioc_g_register = vidioc_g_register,
  1878. .vidioc_s_register = vidioc_s_register,
  1879. #endif
  1880. };
  1881. static struct video_device cx231xx_radio_template = {
  1882. .name = "cx231xx-radio",
  1883. .fops = &radio_fops,
  1884. .ioctl_ops = &radio_ioctl_ops,
  1885. .minor = -1,
  1886. };
  1887. /******************************** usb interface ******************************/
  1888. static struct video_device *cx231xx_vdev_init(struct cx231xx *dev,
  1889. const struct video_device
  1890. *template, const char *type_name)
  1891. {
  1892. struct video_device *vfd;
  1893. vfd = video_device_alloc();
  1894. if (NULL == vfd)
  1895. return NULL;
  1896. *vfd = *template;
  1897. vfd->minor = -1;
  1898. vfd->v4l2_dev = &dev->v4l2_dev;
  1899. vfd->release = video_device_release;
  1900. vfd->debug = video_debug;
  1901. snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name);
  1902. return vfd;
  1903. }
  1904. int cx231xx_register_analog_devices(struct cx231xx *dev)
  1905. {
  1906. int ret;
  1907. cx231xx_info("%s: v4l2 driver version %d.%d.%d\n",
  1908. dev->name,
  1909. (CX231XX_VERSION_CODE >> 16) & 0xff,
  1910. (CX231XX_VERSION_CODE >> 8) & 0xff,
  1911. CX231XX_VERSION_CODE & 0xff);
  1912. /* set default norm */
  1913. /*dev->norm = cx231xx_video_template.current_norm; */
  1914. dev->width = norm_maxw(dev);
  1915. dev->height = norm_maxh(dev);
  1916. dev->interlaced = 0;
  1917. dev->hscale = 0;
  1918. dev->vscale = 0;
  1919. /* Analog specific initialization */
  1920. dev->format = &format[0];
  1921. /* video_mux(dev, dev->video_input); */
  1922. /* Audio defaults */
  1923. dev->mute = 1;
  1924. dev->volume = 0x1f;
  1925. /* enable vbi capturing */
  1926. /* write code here... */
  1927. /* allocate and fill video video_device struct */
  1928. dev->vdev = cx231xx_vdev_init(dev, &cx231xx_video_template, "video");
  1929. if (!dev->vdev) {
  1930. cx231xx_errdev("cannot allocate video_device.\n");
  1931. return -ENODEV;
  1932. }
  1933. /* register v4l2 video video_device */
  1934. ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
  1935. video_nr[dev->devno]);
  1936. if (ret) {
  1937. cx231xx_errdev("unable to register video device (error=%i).\n",
  1938. ret);
  1939. return ret;
  1940. }
  1941. cx231xx_info("%s/0: registered device video%d [v4l2]\n",
  1942. dev->name, dev->vdev->num);
  1943. /* Initialize VBI template */
  1944. memcpy(&cx231xx_vbi_template, &cx231xx_video_template,
  1945. sizeof(cx231xx_vbi_template));
  1946. strcpy(cx231xx_vbi_template.name, "cx231xx-vbi");
  1947. /* Allocate and fill vbi video_device struct */
  1948. dev->vbi_dev = cx231xx_vdev_init(dev, &cx231xx_vbi_template, "vbi");
  1949. /* register v4l2 vbi video_device */
  1950. ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
  1951. vbi_nr[dev->devno]);
  1952. if (ret < 0) {
  1953. cx231xx_errdev("unable to register vbi device\n");
  1954. return ret;
  1955. }
  1956. cx231xx_info("%s/0: registered device vbi%d\n",
  1957. dev->name, dev->vbi_dev->num);
  1958. if (cx231xx_boards[dev->model].radio.type == CX231XX_RADIO) {
  1959. dev->radio_dev = cx231xx_vdev_init(dev, &cx231xx_radio_template,
  1960. "radio");
  1961. if (!dev->radio_dev) {
  1962. cx231xx_errdev("cannot allocate video_device.\n");
  1963. return -ENODEV;
  1964. }
  1965. ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
  1966. radio_nr[dev->devno]);
  1967. if (ret < 0) {
  1968. cx231xx_errdev("can't register radio device\n");
  1969. return ret;
  1970. }
  1971. cx231xx_info("Registered radio device as /dev/radio%d\n",
  1972. dev->radio_dev->num);
  1973. }
  1974. cx231xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
  1975. dev->vdev->num, dev->vbi_dev->num);
  1976. return 0;
  1977. }