cx231xx-video.c 55 KB

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