cx231xx-video.c 55 KB

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