ivtvfb.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300
  1. /*
  2. On Screen Display cx23415 Framebuffer driver
  3. This module presents the cx23415 OSD (onscreen display) framebuffer memory
  4. as a standard Linux /dev/fb style framebuffer device. The framebuffer has
  5. support for 8, 16 & 32 bpp packed pixel formats with alpha channel. In 16bpp
  6. mode, there is a choice of a three color depths (12, 15 or 16 bits), but no
  7. local alpha. The colorspace is selectable between rgb & yuv.
  8. Depending on the TV standard configured in the ivtv module at load time,
  9. the initial resolution is either 640x400 (NTSC) or 640x480 (PAL) at 8bpp.
  10. Video timings are locked to ensure a vertical refresh rate of 50Hz (PAL)
  11. or 59.94 (NTSC)
  12. Copyright (c) 2003 Matt T. Yourst <yourst@yourst.com>
  13. Derived from drivers/video/vesafb.c
  14. Portions (c) 1998 Gerd Knorr <kraxel@goldbach.in-berlin.de>
  15. 2.6 kernel port:
  16. Copyright (C) 2004 Matthias Badaire
  17. Copyright (C) 2004 Chris Kennedy <c@groovy.org>
  18. Copyright (C) 2006 Ian Armstrong <ian@iarmst.demon.co.uk>
  19. This program is free software; you can redistribute it and/or modify
  20. it under the terms of the GNU General Public License as published by
  21. the Free Software Foundation; either version 2 of the License, or
  22. (at your option) any later version.
  23. This program is distributed in the hope that it will be useful,
  24. but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. GNU General Public License for more details.
  27. You should have received a copy of the GNU General Public License
  28. along with this program; if not, write to the Free Software
  29. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  30. */
  31. #include "ivtv-driver.h"
  32. #include "ivtv-cards.h"
  33. #include "ivtv-i2c.h"
  34. #include "ivtv-udma.h"
  35. #include "ivtv-mailbox.h"
  36. #include "ivtv-firmware.h"
  37. #include <linux/fb.h>
  38. #include <linux/ivtvfb.h>
  39. #ifdef CONFIG_X86_64
  40. #include <asm/pat.h>
  41. #endif
  42. /* card parameters */
  43. static int ivtvfb_card_id = -1;
  44. static int ivtvfb_debug = 0;
  45. static bool osd_laced;
  46. static int osd_depth;
  47. static int osd_upper;
  48. static int osd_left;
  49. static int osd_yres;
  50. static int osd_xres;
  51. module_param(ivtvfb_card_id, int, 0444);
  52. module_param_named(debug,ivtvfb_debug, int, 0644);
  53. module_param(osd_laced, bool, 0444);
  54. module_param(osd_depth, int, 0444);
  55. module_param(osd_upper, int, 0444);
  56. module_param(osd_left, int, 0444);
  57. module_param(osd_yres, int, 0444);
  58. module_param(osd_xres, int, 0444);
  59. MODULE_PARM_DESC(ivtvfb_card_id,
  60. "Only use framebuffer of the specified ivtv card (0-31)\n"
  61. "\t\t\tdefault -1: initialize all available framebuffers");
  62. MODULE_PARM_DESC(debug,
  63. "Debug level (bitmask). Default: errors only\n"
  64. "\t\t\t(debug = 3 gives full debugging)");
  65. /* Why upper, left, xres, yres, depth, laced ? To match terminology used
  66. by fbset.
  67. Why start at 1 for left & upper coordinate ? Because X doesn't allow 0 */
  68. MODULE_PARM_DESC(osd_laced,
  69. "Interlaced mode\n"
  70. "\t\t\t0=off\n"
  71. "\t\t\t1=on\n"
  72. "\t\t\tdefault off");
  73. MODULE_PARM_DESC(osd_depth,
  74. "Bits per pixel - 8, 16, 32\n"
  75. "\t\t\tdefault 8");
  76. MODULE_PARM_DESC(osd_upper,
  77. "Vertical start position\n"
  78. "\t\t\tdefault 0 (Centered)");
  79. MODULE_PARM_DESC(osd_left,
  80. "Horizontal start position\n"
  81. "\t\t\tdefault 0 (Centered)");
  82. MODULE_PARM_DESC(osd_yres,
  83. "Display height\n"
  84. "\t\t\tdefault 480 (PAL)\n"
  85. "\t\t\t 400 (NTSC)");
  86. MODULE_PARM_DESC(osd_xres,
  87. "Display width\n"
  88. "\t\t\tdefault 640");
  89. MODULE_AUTHOR("Kevin Thayer, Chris Kennedy, Hans Verkuil, John Harvey, Ian Armstrong");
  90. MODULE_LICENSE("GPL");
  91. /* --------------------------------------------------------------------- */
  92. #define IVTVFB_DBGFLG_WARN (1 << 0)
  93. #define IVTVFB_DBGFLG_INFO (1 << 1)
  94. #define IVTVFB_DEBUG(x, type, fmt, args...) \
  95. do { \
  96. if ((x) & ivtvfb_debug) \
  97. printk(KERN_INFO "ivtvfb%d " type ": " fmt, itv->instance , ## args); \
  98. } while (0)
  99. #define IVTVFB_DEBUG_WARN(fmt, args...) IVTVFB_DEBUG(IVTVFB_DBGFLG_WARN, "warning", fmt , ## args)
  100. #define IVTVFB_DEBUG_INFO(fmt, args...) IVTVFB_DEBUG(IVTVFB_DBGFLG_INFO, "info", fmt , ## args)
  101. /* Standard kernel messages */
  102. #define IVTVFB_ERR(fmt, args...) printk(KERN_ERR "ivtvfb%d: " fmt, itv->instance , ## args)
  103. #define IVTVFB_WARN(fmt, args...) printk(KERN_WARNING "ivtvfb%d: " fmt, itv->instance , ## args)
  104. #define IVTVFB_INFO(fmt, args...) printk(KERN_INFO "ivtvfb%d: " fmt, itv->instance , ## args)
  105. /* --------------------------------------------------------------------- */
  106. #define IVTV_OSD_MAX_WIDTH 720
  107. #define IVTV_OSD_MAX_HEIGHT 576
  108. #define IVTV_OSD_BPP_8 0x00
  109. #define IVTV_OSD_BPP_16_444 0x03
  110. #define IVTV_OSD_BPP_16_555 0x02
  111. #define IVTV_OSD_BPP_16_565 0x01
  112. #define IVTV_OSD_BPP_32 0x04
  113. struct osd_info {
  114. /* Physical base address */
  115. unsigned long video_pbase;
  116. /* Relative base address (relative to start of decoder memory) */
  117. u32 video_rbase;
  118. /* Mapped base address */
  119. volatile char __iomem *video_vbase;
  120. /* Buffer size */
  121. u32 video_buffer_size;
  122. /* video_base rounded down as required by hardware MTRRs */
  123. unsigned long fb_start_aligned_physaddr;
  124. /* video_base rounded up as required by hardware MTRRs */
  125. unsigned long fb_end_aligned_physaddr;
  126. int wc_cookie;
  127. /* Store the buffer offset */
  128. int set_osd_coords_x;
  129. int set_osd_coords_y;
  130. /* Current dimensions (NOT VISIBLE SIZE!) */
  131. int display_width;
  132. int display_height;
  133. int display_byte_stride;
  134. /* Current bits per pixel */
  135. int bits_per_pixel;
  136. int bytes_per_pixel;
  137. /* Frame buffer stuff */
  138. struct fb_info ivtvfb_info;
  139. struct fb_var_screeninfo ivtvfb_defined;
  140. struct fb_fix_screeninfo ivtvfb_fix;
  141. /* Used for a warm start */
  142. struct fb_var_screeninfo fbvar_cur;
  143. int blank_cur;
  144. u32 palette_cur[256];
  145. u32 pan_cur;
  146. };
  147. struct ivtv_osd_coords {
  148. unsigned long offset;
  149. unsigned long max_offset;
  150. int pixel_stride;
  151. int lines;
  152. int x;
  153. int y;
  154. };
  155. /* --------------------------------------------------------------------- */
  156. /* ivtv API calls for framebuffer related support */
  157. static int ivtvfb_get_framebuffer(struct ivtv *itv, u32 *fbbase,
  158. u32 *fblength)
  159. {
  160. u32 data[CX2341X_MBOX_MAX_DATA];
  161. int rc;
  162. ivtv_firmware_check(itv, "ivtvfb_get_framebuffer");
  163. rc = ivtv_vapi_result(itv, data, CX2341X_OSD_GET_FRAMEBUFFER, 0);
  164. *fbbase = data[0];
  165. *fblength = data[1];
  166. return rc;
  167. }
  168. static int ivtvfb_get_osd_coords(struct ivtv *itv,
  169. struct ivtv_osd_coords *osd)
  170. {
  171. struct osd_info *oi = itv->osd_info;
  172. u32 data[CX2341X_MBOX_MAX_DATA];
  173. ivtv_vapi_result(itv, data, CX2341X_OSD_GET_OSD_COORDS, 0);
  174. osd->offset = data[0] - oi->video_rbase;
  175. osd->max_offset = oi->display_width * oi->display_height * 4;
  176. osd->pixel_stride = data[1];
  177. osd->lines = data[2];
  178. osd->x = data[3];
  179. osd->y = data[4];
  180. return 0;
  181. }
  182. static int ivtvfb_set_osd_coords(struct ivtv *itv, const struct ivtv_osd_coords *osd)
  183. {
  184. struct osd_info *oi = itv->osd_info;
  185. oi->display_width = osd->pixel_stride;
  186. oi->display_byte_stride = osd->pixel_stride * oi->bytes_per_pixel;
  187. oi->set_osd_coords_x += osd->x;
  188. oi->set_osd_coords_y = osd->y;
  189. return ivtv_vapi(itv, CX2341X_OSD_SET_OSD_COORDS, 5,
  190. osd->offset + oi->video_rbase,
  191. osd->pixel_stride,
  192. osd->lines, osd->x, osd->y);
  193. }
  194. static int ivtvfb_set_display_window(struct ivtv *itv, struct v4l2_rect *ivtv_window)
  195. {
  196. int osd_height_limit = itv->is_out_50hz ? 576 : 480;
  197. /* Only fail if resolution too high, otherwise fudge the start coords. */
  198. if ((ivtv_window->height > osd_height_limit) || (ivtv_window->width > IVTV_OSD_MAX_WIDTH))
  199. return -EINVAL;
  200. /* Ensure we don't exceed display limits */
  201. if (ivtv_window->top + ivtv_window->height > osd_height_limit) {
  202. IVTVFB_DEBUG_WARN("ivtv_ioctl_fb_set_display_window - Invalid height setting (%d, %d)\n",
  203. ivtv_window->top, ivtv_window->height);
  204. ivtv_window->top = osd_height_limit - ivtv_window->height;
  205. }
  206. if (ivtv_window->left + ivtv_window->width > IVTV_OSD_MAX_WIDTH) {
  207. IVTVFB_DEBUG_WARN("ivtv_ioctl_fb_set_display_window - Invalid width setting (%d, %d)\n",
  208. ivtv_window->left, ivtv_window->width);
  209. ivtv_window->left = IVTV_OSD_MAX_WIDTH - ivtv_window->width;
  210. }
  211. /* Set the OSD origin */
  212. write_reg((ivtv_window->top << 16) | ivtv_window->left, 0x02a04);
  213. /* How much to display */
  214. write_reg(((ivtv_window->top+ivtv_window->height) << 16) | (ivtv_window->left+ivtv_window->width), 0x02a08);
  215. /* Pass this info back the yuv handler */
  216. itv->yuv_info.osd_vis_w = ivtv_window->width;
  217. itv->yuv_info.osd_vis_h = ivtv_window->height;
  218. itv->yuv_info.osd_x_offset = ivtv_window->left;
  219. itv->yuv_info.osd_y_offset = ivtv_window->top;
  220. return 0;
  221. }
  222. static int ivtvfb_prep_dec_dma_to_device(struct ivtv *itv,
  223. unsigned long ivtv_dest_addr, void __user *userbuf,
  224. int size_in_bytes)
  225. {
  226. DEFINE_WAIT(wait);
  227. int got_sig = 0;
  228. mutex_lock(&itv->udma.lock);
  229. /* Map User DMA */
  230. if (ivtv_udma_setup(itv, ivtv_dest_addr, userbuf, size_in_bytes) <= 0) {
  231. mutex_unlock(&itv->udma.lock);
  232. IVTVFB_WARN("ivtvfb_prep_dec_dma_to_device, Error with get_user_pages: %d bytes, %d pages returned\n",
  233. size_in_bytes, itv->udma.page_count);
  234. /* get_user_pages must have failed completely */
  235. return -EIO;
  236. }
  237. IVTVFB_DEBUG_INFO("ivtvfb_prep_dec_dma_to_device, %d bytes, %d pages\n",
  238. size_in_bytes, itv->udma.page_count);
  239. ivtv_udma_prepare(itv);
  240. prepare_to_wait(&itv->dma_waitq, &wait, TASK_INTERRUPTIBLE);
  241. /* if no UDMA is pending and no UDMA is in progress, then the DMA
  242. is finished */
  243. while (test_bit(IVTV_F_I_UDMA_PENDING, &itv->i_flags) ||
  244. test_bit(IVTV_F_I_UDMA, &itv->i_flags)) {
  245. /* don't interrupt if the DMA is in progress but break off
  246. a still pending DMA. */
  247. got_sig = signal_pending(current);
  248. if (got_sig && test_and_clear_bit(IVTV_F_I_UDMA_PENDING, &itv->i_flags))
  249. break;
  250. got_sig = 0;
  251. schedule();
  252. }
  253. finish_wait(&itv->dma_waitq, &wait);
  254. /* Unmap Last DMA Xfer */
  255. ivtv_udma_unmap(itv);
  256. mutex_unlock(&itv->udma.lock);
  257. if (got_sig) {
  258. IVTV_DEBUG_INFO("User stopped OSD\n");
  259. return -EINTR;
  260. }
  261. return 0;
  262. }
  263. static int ivtvfb_prep_frame(struct ivtv *itv, int cmd, void __user *source,
  264. unsigned long dest_offset, int count)
  265. {
  266. DEFINE_WAIT(wait);
  267. struct osd_info *oi = itv->osd_info;
  268. /* Nothing to do */
  269. if (count == 0) {
  270. IVTVFB_DEBUG_WARN("ivtvfb_prep_frame: Nothing to do. count = 0\n");
  271. return -EINVAL;
  272. }
  273. /* Check Total FB Size */
  274. if ((dest_offset + count) > oi->video_buffer_size) {
  275. IVTVFB_WARN("ivtvfb_prep_frame: Overflowing the framebuffer %ld, only %d available\n",
  276. dest_offset + count, oi->video_buffer_size);
  277. return -E2BIG;
  278. }
  279. /* Not fatal, but will have undesirable results */
  280. if ((unsigned long)source & 3)
  281. IVTVFB_WARN("ivtvfb_prep_frame: Source address not 32 bit aligned (%p)\n",
  282. source);
  283. if (dest_offset & 3)
  284. IVTVFB_WARN("ivtvfb_prep_frame: Dest offset not 32 bit aligned (%ld)\n", dest_offset);
  285. if (count & 3)
  286. IVTVFB_WARN("ivtvfb_prep_frame: Count not a multiple of 4 (%d)\n", count);
  287. /* Check Source */
  288. if (!access_ok(VERIFY_READ, source + dest_offset, count)) {
  289. IVTVFB_WARN("Invalid userspace pointer %p\n", source);
  290. IVTVFB_DEBUG_WARN("access_ok() failed for offset 0x%08lx source %p count %d\n",
  291. dest_offset, source, count);
  292. return -EINVAL;
  293. }
  294. /* OSD Address to send DMA to */
  295. dest_offset += IVTV_DECODER_OFFSET + oi->video_rbase;
  296. /* Fill Buffers */
  297. return ivtvfb_prep_dec_dma_to_device(itv, dest_offset, source, count);
  298. }
  299. static ssize_t ivtvfb_write(struct fb_info *info, const char __user *buf,
  300. size_t count, loff_t *ppos)
  301. {
  302. unsigned long p = *ppos;
  303. void *dst;
  304. int err = 0;
  305. int dma_err;
  306. unsigned long total_size;
  307. struct ivtv *itv = (struct ivtv *) info->par;
  308. unsigned long dma_offset =
  309. IVTV_DECODER_OFFSET + itv->osd_info->video_rbase;
  310. unsigned long dma_size;
  311. u16 lead = 0, tail = 0;
  312. if (info->state != FBINFO_STATE_RUNNING)
  313. return -EPERM;
  314. total_size = info->screen_size;
  315. if (total_size == 0)
  316. total_size = info->fix.smem_len;
  317. if (p > total_size)
  318. return -EFBIG;
  319. if (count > total_size) {
  320. err = -EFBIG;
  321. count = total_size;
  322. }
  323. if (count + p > total_size) {
  324. if (!err)
  325. err = -ENOSPC;
  326. count = total_size - p;
  327. }
  328. dst = (void __force *) (info->screen_base + p);
  329. if (info->fbops->fb_sync)
  330. info->fbops->fb_sync(info);
  331. /* If transfer size > threshold and both src/dst
  332. addresses are aligned, use DMA */
  333. if (count >= 4096 &&
  334. ((unsigned long)buf & 3) == ((unsigned long)dst & 3)) {
  335. /* Odd address = can't DMA. Align */
  336. if ((unsigned long)dst & 3) {
  337. lead = 4 - ((unsigned long)dst & 3);
  338. if (copy_from_user(dst, buf, lead))
  339. return -EFAULT;
  340. buf += lead;
  341. dst += lead;
  342. }
  343. /* DMA resolution is 32 bits */
  344. if ((count - lead) & 3)
  345. tail = (count - lead) & 3;
  346. /* DMA the data */
  347. dma_size = count - lead - tail;
  348. dma_err = ivtvfb_prep_dec_dma_to_device(itv,
  349. p + lead + dma_offset, (void __user *)buf, dma_size);
  350. if (dma_err)
  351. return dma_err;
  352. dst += dma_size;
  353. buf += dma_size;
  354. /* Copy any leftover data */
  355. if (tail && copy_from_user(dst, buf, tail))
  356. return -EFAULT;
  357. } else if (copy_from_user(dst, buf, count)) {
  358. return -EFAULT;
  359. }
  360. if (!err)
  361. *ppos += count;
  362. return (err) ? err : count;
  363. }
  364. static int ivtvfb_ioctl(struct fb_info *info, unsigned int cmd, unsigned long arg)
  365. {
  366. DEFINE_WAIT(wait);
  367. struct ivtv *itv = (struct ivtv *)info->par;
  368. int rc = 0;
  369. switch (cmd) {
  370. case FBIOGET_VBLANK: {
  371. struct fb_vblank vblank;
  372. u32 trace;
  373. memset(&vblank, 0, sizeof(struct fb_vblank));
  374. vblank.flags = FB_VBLANK_HAVE_COUNT |FB_VBLANK_HAVE_VCOUNT |
  375. FB_VBLANK_HAVE_VSYNC;
  376. trace = read_reg(IVTV_REG_DEC_LINE_FIELD) >> 16;
  377. if (itv->is_out_50hz && trace > 312)
  378. trace -= 312;
  379. else if (itv->is_out_60hz && trace > 262)
  380. trace -= 262;
  381. if (trace == 1)
  382. vblank.flags |= FB_VBLANK_VSYNCING;
  383. vblank.count = itv->last_vsync_field;
  384. vblank.vcount = trace;
  385. vblank.hcount = 0;
  386. if (copy_to_user((void __user *)arg, &vblank, sizeof(vblank)))
  387. return -EFAULT;
  388. return 0;
  389. }
  390. case FBIO_WAITFORVSYNC:
  391. prepare_to_wait(&itv->vsync_waitq, &wait, TASK_INTERRUPTIBLE);
  392. if (!schedule_timeout(msecs_to_jiffies(50)))
  393. rc = -ETIMEDOUT;
  394. finish_wait(&itv->vsync_waitq, &wait);
  395. return rc;
  396. case IVTVFB_IOC_DMA_FRAME: {
  397. struct ivtvfb_dma_frame args;
  398. IVTVFB_DEBUG_INFO("IVTVFB_IOC_DMA_FRAME\n");
  399. if (copy_from_user(&args, (void __user *)arg, sizeof(args)))
  400. return -EFAULT;
  401. return ivtvfb_prep_frame(itv, cmd, args.source, args.dest_offset, args.count);
  402. }
  403. default:
  404. IVTVFB_DEBUG_INFO("Unknown ioctl %08x\n", cmd);
  405. return -EINVAL;
  406. }
  407. return 0;
  408. }
  409. /* Framebuffer device handling */
  410. static int ivtvfb_set_var(struct ivtv *itv, struct fb_var_screeninfo *var)
  411. {
  412. struct osd_info *oi = itv->osd_info;
  413. struct ivtv_osd_coords ivtv_osd;
  414. struct v4l2_rect ivtv_window;
  415. int osd_mode = -1;
  416. IVTVFB_DEBUG_INFO("ivtvfb_set_var\n");
  417. /* Select color space */
  418. if (var->nonstd) /* YUV */
  419. write_reg(read_reg(0x02a00) | 0x0002000, 0x02a00);
  420. else /* RGB */
  421. write_reg(read_reg(0x02a00) & ~0x0002000, 0x02a00);
  422. /* Set the color mode */
  423. switch (var->bits_per_pixel) {
  424. case 8:
  425. osd_mode = IVTV_OSD_BPP_8;
  426. break;
  427. case 32:
  428. osd_mode = IVTV_OSD_BPP_32;
  429. break;
  430. case 16:
  431. switch (var->green.length) {
  432. case 4:
  433. osd_mode = IVTV_OSD_BPP_16_444;
  434. break;
  435. case 5:
  436. osd_mode = IVTV_OSD_BPP_16_555;
  437. break;
  438. case 6:
  439. osd_mode = IVTV_OSD_BPP_16_565;
  440. break;
  441. default:
  442. IVTVFB_DEBUG_WARN("ivtvfb_set_var - Invalid bpp\n");
  443. }
  444. break;
  445. default:
  446. IVTVFB_DEBUG_WARN("ivtvfb_set_var - Invalid bpp\n");
  447. }
  448. /* Set video mode. Although rare, the display can become scrambled even
  449. if we don't change mode. Always 'bounce' to osd_mode via mode 0 */
  450. if (osd_mode != -1) {
  451. ivtv_vapi(itv, CX2341X_OSD_SET_PIXEL_FORMAT, 1, 0);
  452. ivtv_vapi(itv, CX2341X_OSD_SET_PIXEL_FORMAT, 1, osd_mode);
  453. }
  454. oi->bits_per_pixel = var->bits_per_pixel;
  455. oi->bytes_per_pixel = var->bits_per_pixel / 8;
  456. /* Set the flicker filter */
  457. switch (var->vmode & FB_VMODE_MASK) {
  458. case FB_VMODE_NONINTERLACED: /* Filter on */
  459. ivtv_vapi(itv, CX2341X_OSD_SET_FLICKER_STATE, 1, 1);
  460. break;
  461. case FB_VMODE_INTERLACED: /* Filter off */
  462. ivtv_vapi(itv, CX2341X_OSD_SET_FLICKER_STATE, 1, 0);
  463. break;
  464. default:
  465. IVTVFB_DEBUG_WARN("ivtvfb_set_var - Invalid video mode\n");
  466. }
  467. /* Read the current osd info */
  468. ivtvfb_get_osd_coords(itv, &ivtv_osd);
  469. /* Now set the OSD to the size we want */
  470. ivtv_osd.pixel_stride = var->xres_virtual;
  471. ivtv_osd.lines = var->yres_virtual;
  472. ivtv_osd.x = 0;
  473. ivtv_osd.y = 0;
  474. ivtvfb_set_osd_coords(itv, &ivtv_osd);
  475. /* Can't seem to find the right API combo for this.
  476. Use another function which does what we need through direct register access. */
  477. ivtv_window.width = var->xres;
  478. ivtv_window.height = var->yres;
  479. /* Minimum margin cannot be 0, as X won't allow such a mode */
  480. if (!var->upper_margin)
  481. var->upper_margin++;
  482. if (!var->left_margin)
  483. var->left_margin++;
  484. ivtv_window.top = var->upper_margin - 1;
  485. ivtv_window.left = var->left_margin - 1;
  486. ivtvfb_set_display_window(itv, &ivtv_window);
  487. /* Pass screen size back to yuv handler */
  488. itv->yuv_info.osd_full_w = ivtv_osd.pixel_stride;
  489. itv->yuv_info.osd_full_h = ivtv_osd.lines;
  490. /* Force update of yuv registers */
  491. itv->yuv_info.yuv_forced_update = 1;
  492. /* Keep a copy of these settings */
  493. memcpy(&oi->fbvar_cur, var, sizeof(oi->fbvar_cur));
  494. IVTVFB_DEBUG_INFO("Display size: %dx%d (virtual %dx%d) @ %dbpp\n",
  495. var->xres, var->yres,
  496. var->xres_virtual, var->yres_virtual,
  497. var->bits_per_pixel);
  498. IVTVFB_DEBUG_INFO("Display position: %d, %d\n",
  499. var->left_margin, var->upper_margin);
  500. IVTVFB_DEBUG_INFO("Display filter: %s\n",
  501. (var->vmode & FB_VMODE_MASK) == FB_VMODE_NONINTERLACED ? "on" : "off");
  502. IVTVFB_DEBUG_INFO("Color space: %s\n", var->nonstd ? "YUV" : "RGB");
  503. return 0;
  504. }
  505. static int ivtvfb_get_fix(struct ivtv *itv, struct fb_fix_screeninfo *fix)
  506. {
  507. struct osd_info *oi = itv->osd_info;
  508. IVTVFB_DEBUG_INFO("ivtvfb_get_fix\n");
  509. memset(fix, 0, sizeof(struct fb_fix_screeninfo));
  510. strlcpy(fix->id, "cx23415 TV out", sizeof(fix->id));
  511. fix->smem_start = oi->video_pbase;
  512. fix->smem_len = oi->video_buffer_size;
  513. fix->type = FB_TYPE_PACKED_PIXELS;
  514. fix->visual = (oi->bits_per_pixel == 8) ? FB_VISUAL_PSEUDOCOLOR : FB_VISUAL_TRUECOLOR;
  515. fix->xpanstep = 1;
  516. fix->ypanstep = 1;
  517. fix->ywrapstep = 0;
  518. fix->line_length = oi->display_byte_stride;
  519. fix->accel = FB_ACCEL_NONE;
  520. return 0;
  521. }
  522. /* Check the requested display mode, returning -EINVAL if we can't
  523. handle it. */
  524. static int _ivtvfb_check_var(struct fb_var_screeninfo *var, struct ivtv *itv)
  525. {
  526. struct osd_info *oi = itv->osd_info;
  527. int osd_height_limit;
  528. u32 pixclock, hlimit, vlimit;
  529. IVTVFB_DEBUG_INFO("ivtvfb_check_var\n");
  530. /* Set base references for mode calcs. */
  531. if (itv->is_out_50hz) {
  532. pixclock = 84316;
  533. hlimit = 776;
  534. vlimit = 591;
  535. osd_height_limit = 576;
  536. }
  537. else {
  538. pixclock = 83926;
  539. hlimit = 776;
  540. vlimit = 495;
  541. osd_height_limit = 480;
  542. }
  543. if (var->bits_per_pixel == 8 || var->bits_per_pixel == 32) {
  544. var->transp.offset = 24;
  545. var->transp.length = 8;
  546. var->red.offset = 16;
  547. var->red.length = 8;
  548. var->green.offset = 8;
  549. var->green.length = 8;
  550. var->blue.offset = 0;
  551. var->blue.length = 8;
  552. }
  553. else if (var->bits_per_pixel == 16) {
  554. /* To find out the true mode, check green length */
  555. switch (var->green.length) {
  556. case 4:
  557. var->red.offset = 8;
  558. var->red.length = 4;
  559. var->green.offset = 4;
  560. var->green.length = 4;
  561. var->blue.offset = 0;
  562. var->blue.length = 4;
  563. var->transp.offset = 12;
  564. var->transp.length = 1;
  565. break;
  566. case 5:
  567. var->red.offset = 10;
  568. var->red.length = 5;
  569. var->green.offset = 5;
  570. var->green.length = 5;
  571. var->blue.offset = 0;
  572. var->blue.length = 5;
  573. var->transp.offset = 15;
  574. var->transp.length = 1;
  575. break;
  576. default:
  577. var->red.offset = 11;
  578. var->red.length = 5;
  579. var->green.offset = 5;
  580. var->green.length = 6;
  581. var->blue.offset = 0;
  582. var->blue.length = 5;
  583. var->transp.offset = 0;
  584. var->transp.length = 0;
  585. break;
  586. }
  587. }
  588. else {
  589. IVTVFB_DEBUG_WARN("Invalid colour mode: %d\n", var->bits_per_pixel);
  590. return -EINVAL;
  591. }
  592. /* Check the resolution */
  593. if (var->xres > IVTV_OSD_MAX_WIDTH || var->yres > osd_height_limit) {
  594. IVTVFB_DEBUG_WARN("Invalid resolution: %dx%d\n",
  595. var->xres, var->yres);
  596. return -EINVAL;
  597. }
  598. /* Max horizontal size is 1023 @ 32bpp, 2046 & 16bpp, 4092 @ 8bpp */
  599. if (var->xres_virtual > 4095 / (var->bits_per_pixel / 8) ||
  600. var->xres_virtual * var->yres_virtual * (var->bits_per_pixel / 8) > oi->video_buffer_size ||
  601. var->xres_virtual < var->xres ||
  602. var->yres_virtual < var->yres) {
  603. IVTVFB_DEBUG_WARN("Invalid virtual resolution: %dx%d\n",
  604. var->xres_virtual, var->yres_virtual);
  605. return -EINVAL;
  606. }
  607. /* Some extra checks if in 8 bit mode */
  608. if (var->bits_per_pixel == 8) {
  609. /* Width must be a multiple of 4 */
  610. if (var->xres & 3) {
  611. IVTVFB_DEBUG_WARN("Invalid resolution for 8bpp: %d\n", var->xres);
  612. return -EINVAL;
  613. }
  614. if (var->xres_virtual & 3) {
  615. IVTVFB_DEBUG_WARN("Invalid virtual resolution for 8bpp: %d)\n", var->xres_virtual);
  616. return -EINVAL;
  617. }
  618. }
  619. else if (var->bits_per_pixel == 16) {
  620. /* Width must be a multiple of 2 */
  621. if (var->xres & 1) {
  622. IVTVFB_DEBUG_WARN("Invalid resolution for 16bpp: %d\n", var->xres);
  623. return -EINVAL;
  624. }
  625. if (var->xres_virtual & 1) {
  626. IVTVFB_DEBUG_WARN("Invalid virtual resolution for 16bpp: %d)\n", var->xres_virtual);
  627. return -EINVAL;
  628. }
  629. }
  630. /* Now check the offsets */
  631. if (var->xoffset >= var->xres_virtual || var->yoffset >= var->yres_virtual) {
  632. IVTVFB_DEBUG_WARN("Invalid offset: %d (%d) %d (%d)\n",
  633. var->xoffset, var->xres_virtual, var->yoffset, var->yres_virtual);
  634. return -EINVAL;
  635. }
  636. /* Check pixel format */
  637. if (var->nonstd > 1) {
  638. IVTVFB_DEBUG_WARN("Invalid nonstd % d\n", var->nonstd);
  639. return -EINVAL;
  640. }
  641. /* Check video mode */
  642. if (((var->vmode & FB_VMODE_MASK) != FB_VMODE_NONINTERLACED) &&
  643. ((var->vmode & FB_VMODE_MASK) != FB_VMODE_INTERLACED)) {
  644. IVTVFB_DEBUG_WARN("Invalid video mode: %d\n", var->vmode & FB_VMODE_MASK);
  645. return -EINVAL;
  646. }
  647. /* Check the left & upper margins
  648. If the margins are too large, just center the screen
  649. (enforcing margins causes too many problems) */
  650. if (var->left_margin + var->xres > IVTV_OSD_MAX_WIDTH + 1)
  651. var->left_margin = 1 + ((IVTV_OSD_MAX_WIDTH - var->xres) / 2);
  652. if (var->upper_margin + var->yres > (itv->is_out_50hz ? 577 : 481))
  653. var->upper_margin = 1 + (((itv->is_out_50hz ? 576 : 480) -
  654. var->yres) / 2);
  655. /* Maintain overall 'size' for a constant refresh rate */
  656. var->right_margin = hlimit - var->left_margin - var->xres;
  657. var->lower_margin = vlimit - var->upper_margin - var->yres;
  658. /* Fixed sync times */
  659. var->hsync_len = 24;
  660. var->vsync_len = 2;
  661. /* Non-interlaced / interlaced mode is used to switch the OSD filter
  662. on or off. Adjust the clock timings to maintain a constant
  663. vertical refresh rate. */
  664. if ((var->vmode & FB_VMODE_MASK) == FB_VMODE_NONINTERLACED)
  665. var->pixclock = pixclock / 2;
  666. else
  667. var->pixclock = pixclock;
  668. itv->osd_rect.width = var->xres;
  669. itv->osd_rect.height = var->yres;
  670. IVTVFB_DEBUG_INFO("Display size: %dx%d (virtual %dx%d) @ %dbpp\n",
  671. var->xres, var->yres,
  672. var->xres_virtual, var->yres_virtual,
  673. var->bits_per_pixel);
  674. IVTVFB_DEBUG_INFO("Display position: %d, %d\n",
  675. var->left_margin, var->upper_margin);
  676. IVTVFB_DEBUG_INFO("Display filter: %s\n",
  677. (var->vmode & FB_VMODE_MASK) == FB_VMODE_NONINTERLACED ? "on" : "off");
  678. IVTVFB_DEBUG_INFO("Color space: %s\n", var->nonstd ? "YUV" : "RGB");
  679. return 0;
  680. }
  681. static int ivtvfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
  682. {
  683. struct ivtv *itv = (struct ivtv *) info->par;
  684. IVTVFB_DEBUG_INFO("ivtvfb_check_var\n");
  685. return _ivtvfb_check_var(var, itv);
  686. }
  687. static int ivtvfb_pan_display(struct fb_var_screeninfo *var, struct fb_info *info)
  688. {
  689. u32 osd_pan_index;
  690. struct ivtv *itv = (struct ivtv *) info->par;
  691. if (var->yoffset + info->var.yres > info->var.yres_virtual ||
  692. var->xoffset + info->var.xres > info->var.xres_virtual)
  693. return -EINVAL;
  694. osd_pan_index = var->yoffset * info->fix.line_length
  695. + var->xoffset * info->var.bits_per_pixel / 8;
  696. write_reg(osd_pan_index, 0x02A0C);
  697. /* Pass this info back the yuv handler */
  698. itv->yuv_info.osd_x_pan = var->xoffset;
  699. itv->yuv_info.osd_y_pan = var->yoffset;
  700. /* Force update of yuv registers */
  701. itv->yuv_info.yuv_forced_update = 1;
  702. /* Remember this value */
  703. itv->osd_info->pan_cur = osd_pan_index;
  704. return 0;
  705. }
  706. static int ivtvfb_set_par(struct fb_info *info)
  707. {
  708. int rc = 0;
  709. struct ivtv *itv = (struct ivtv *) info->par;
  710. IVTVFB_DEBUG_INFO("ivtvfb_set_par\n");
  711. rc = ivtvfb_set_var(itv, &info->var);
  712. ivtvfb_pan_display(&info->var, info);
  713. ivtvfb_get_fix(itv, &info->fix);
  714. ivtv_firmware_check(itv, "ivtvfb_set_par");
  715. return rc;
  716. }
  717. static int ivtvfb_setcolreg(unsigned regno, unsigned red, unsigned green,
  718. unsigned blue, unsigned transp,
  719. struct fb_info *info)
  720. {
  721. u32 color, *palette;
  722. struct ivtv *itv = (struct ivtv *)info->par;
  723. if (regno >= info->cmap.len)
  724. return -EINVAL;
  725. color = ((transp & 0xFF00) << 16) |((red & 0xFF00) << 8) | (green & 0xFF00) | ((blue & 0xFF00) >> 8);
  726. if (info->var.bits_per_pixel <= 8) {
  727. write_reg(regno, 0x02a30);
  728. write_reg(color, 0x02a34);
  729. itv->osd_info->palette_cur[regno] = color;
  730. return 0;
  731. }
  732. if (regno >= 16)
  733. return -EINVAL;
  734. palette = info->pseudo_palette;
  735. if (info->var.bits_per_pixel == 16) {
  736. switch (info->var.green.length) {
  737. case 4:
  738. color = ((red & 0xf000) >> 4) |
  739. ((green & 0xf000) >> 8) |
  740. ((blue & 0xf000) >> 12);
  741. break;
  742. case 5:
  743. color = ((red & 0xf800) >> 1) |
  744. ((green & 0xf800) >> 6) |
  745. ((blue & 0xf800) >> 11);
  746. break;
  747. case 6:
  748. color = (red & 0xf800 ) |
  749. ((green & 0xfc00) >> 5) |
  750. ((blue & 0xf800) >> 11);
  751. break;
  752. }
  753. }
  754. palette[regno] = color;
  755. return 0;
  756. }
  757. /* We don't really support blanking. All this does is enable or
  758. disable the OSD. */
  759. static int ivtvfb_blank(int blank_mode, struct fb_info *info)
  760. {
  761. struct ivtv *itv = (struct ivtv *)info->par;
  762. IVTVFB_DEBUG_INFO("Set blanking mode : %d\n", blank_mode);
  763. switch (blank_mode) {
  764. case FB_BLANK_UNBLANK:
  765. ivtv_vapi(itv, CX2341X_OSD_SET_STATE, 1, 1);
  766. ivtv_call_hw(itv, IVTV_HW_SAA7127, video, s_stream, 1);
  767. break;
  768. case FB_BLANK_NORMAL:
  769. case FB_BLANK_HSYNC_SUSPEND:
  770. case FB_BLANK_VSYNC_SUSPEND:
  771. ivtv_vapi(itv, CX2341X_OSD_SET_STATE, 1, 0);
  772. ivtv_call_hw(itv, IVTV_HW_SAA7127, video, s_stream, 1);
  773. break;
  774. case FB_BLANK_POWERDOWN:
  775. ivtv_call_hw(itv, IVTV_HW_SAA7127, video, s_stream, 0);
  776. ivtv_vapi(itv, CX2341X_OSD_SET_STATE, 1, 0);
  777. break;
  778. }
  779. itv->osd_info->blank_cur = blank_mode;
  780. return 0;
  781. }
  782. static struct fb_ops ivtvfb_ops = {
  783. .owner = THIS_MODULE,
  784. .fb_write = ivtvfb_write,
  785. .fb_check_var = ivtvfb_check_var,
  786. .fb_set_par = ivtvfb_set_par,
  787. .fb_setcolreg = ivtvfb_setcolreg,
  788. .fb_fillrect = cfb_fillrect,
  789. .fb_copyarea = cfb_copyarea,
  790. .fb_imageblit = cfb_imageblit,
  791. .fb_cursor = NULL,
  792. .fb_ioctl = ivtvfb_ioctl,
  793. .fb_pan_display = ivtvfb_pan_display,
  794. .fb_blank = ivtvfb_blank,
  795. };
  796. /* Restore hardware after firmware restart */
  797. static void ivtvfb_restore(struct ivtv *itv)
  798. {
  799. struct osd_info *oi = itv->osd_info;
  800. int i;
  801. ivtvfb_set_var(itv, &oi->fbvar_cur);
  802. ivtvfb_blank(oi->blank_cur, &oi->ivtvfb_info);
  803. for (i = 0; i < 256; i++) {
  804. write_reg(i, 0x02a30);
  805. write_reg(oi->palette_cur[i], 0x02a34);
  806. }
  807. write_reg(oi->pan_cur, 0x02a0c);
  808. }
  809. /* Initialization */
  810. /* Setup our initial video mode */
  811. static int ivtvfb_init_vidmode(struct ivtv *itv)
  812. {
  813. struct osd_info *oi = itv->osd_info;
  814. struct v4l2_rect start_window;
  815. int max_height;
  816. /* Color mode */
  817. if (osd_depth != 8 && osd_depth != 16 && osd_depth != 32)
  818. osd_depth = 8;
  819. oi->bits_per_pixel = osd_depth;
  820. oi->bytes_per_pixel = oi->bits_per_pixel / 8;
  821. /* Horizontal size & position */
  822. if (osd_xres > 720)
  823. osd_xres = 720;
  824. /* Must be a multiple of 4 for 8bpp & 2 for 16bpp */
  825. if (osd_depth == 8)
  826. osd_xres &= ~3;
  827. else if (osd_depth == 16)
  828. osd_xres &= ~1;
  829. start_window.width = osd_xres ? osd_xres : 640;
  830. /* Check horizontal start (osd_left). */
  831. if (osd_left && osd_left + start_window.width > 721) {
  832. IVTVFB_ERR("Invalid osd_left - assuming default\n");
  833. osd_left = 0;
  834. }
  835. /* Hardware coords start at 0, user coords start at 1. */
  836. osd_left--;
  837. start_window.left = osd_left >= 0 ?
  838. osd_left : ((IVTV_OSD_MAX_WIDTH - start_window.width) / 2);
  839. oi->display_byte_stride =
  840. start_window.width * oi->bytes_per_pixel;
  841. /* Vertical size & position */
  842. max_height = itv->is_out_50hz ? 576 : 480;
  843. if (osd_yres > max_height)
  844. osd_yres = max_height;
  845. start_window.height = osd_yres ?
  846. osd_yres : itv->is_out_50hz ? 480 : 400;
  847. /* Check vertical start (osd_upper). */
  848. if (osd_upper + start_window.height > max_height + 1) {
  849. IVTVFB_ERR("Invalid osd_upper - assuming default\n");
  850. osd_upper = 0;
  851. }
  852. /* Hardware coords start at 0, user coords start at 1. */
  853. osd_upper--;
  854. start_window.top = osd_upper >= 0 ? osd_upper : ((max_height - start_window.height) / 2);
  855. oi->display_width = start_window.width;
  856. oi->display_height = start_window.height;
  857. /* Generate a valid fb_var_screeninfo */
  858. oi->ivtvfb_defined.xres = oi->display_width;
  859. oi->ivtvfb_defined.yres = oi->display_height;
  860. oi->ivtvfb_defined.xres_virtual = oi->display_width;
  861. oi->ivtvfb_defined.yres_virtual = oi->display_height;
  862. oi->ivtvfb_defined.bits_per_pixel = oi->bits_per_pixel;
  863. oi->ivtvfb_defined.vmode = (osd_laced ? FB_VMODE_INTERLACED : FB_VMODE_NONINTERLACED);
  864. oi->ivtvfb_defined.left_margin = start_window.left + 1;
  865. oi->ivtvfb_defined.upper_margin = start_window.top + 1;
  866. oi->ivtvfb_defined.accel_flags = FB_ACCEL_NONE;
  867. oi->ivtvfb_defined.nonstd = 0;
  868. /* We've filled in the most data, let the usual mode check
  869. routine fill in the rest. */
  870. _ivtvfb_check_var(&oi->ivtvfb_defined, itv);
  871. /* Generate valid fb_fix_screeninfo */
  872. ivtvfb_get_fix(itv, &oi->ivtvfb_fix);
  873. /* Generate valid fb_info */
  874. oi->ivtvfb_info.node = -1;
  875. oi->ivtvfb_info.flags = FBINFO_FLAG_DEFAULT;
  876. oi->ivtvfb_info.fbops = &ivtvfb_ops;
  877. oi->ivtvfb_info.par = itv;
  878. oi->ivtvfb_info.var = oi->ivtvfb_defined;
  879. oi->ivtvfb_info.fix = oi->ivtvfb_fix;
  880. oi->ivtvfb_info.screen_base = (u8 __iomem *)oi->video_vbase;
  881. oi->ivtvfb_info.fbops = &ivtvfb_ops;
  882. /* Supply some monitor specs. Bogus values will do for now */
  883. oi->ivtvfb_info.monspecs.hfmin = 8000;
  884. oi->ivtvfb_info.monspecs.hfmax = 70000;
  885. oi->ivtvfb_info.monspecs.vfmin = 10;
  886. oi->ivtvfb_info.monspecs.vfmax = 100;
  887. /* Allocate color map */
  888. if (fb_alloc_cmap(&oi->ivtvfb_info.cmap, 256, 1)) {
  889. IVTVFB_ERR("abort, unable to alloc cmap\n");
  890. return -ENOMEM;
  891. }
  892. /* Allocate the pseudo palette */
  893. oi->ivtvfb_info.pseudo_palette =
  894. kmalloc_array(16, sizeof(u32), GFP_KERNEL|__GFP_NOWARN);
  895. if (!oi->ivtvfb_info.pseudo_palette) {
  896. IVTVFB_ERR("abort, unable to alloc pseudo palette\n");
  897. return -ENOMEM;
  898. }
  899. return 0;
  900. }
  901. /* Find OSD buffer base & size. Add to mtrr. Zero osd buffer. */
  902. static int ivtvfb_init_io(struct ivtv *itv)
  903. {
  904. struct osd_info *oi = itv->osd_info;
  905. /* Find the largest power of two that maps the whole buffer */
  906. int size_shift = 31;
  907. mutex_lock(&itv->serialize_lock);
  908. if (ivtv_init_on_first_open(itv)) {
  909. mutex_unlock(&itv->serialize_lock);
  910. IVTVFB_ERR("Failed to initialize ivtv\n");
  911. return -ENXIO;
  912. }
  913. mutex_unlock(&itv->serialize_lock);
  914. if (ivtvfb_get_framebuffer(itv, &oi->video_rbase,
  915. &oi->video_buffer_size) < 0) {
  916. IVTVFB_ERR("Firmware failed to respond\n");
  917. return -EIO;
  918. }
  919. /* The osd buffer size depends on the number of video buffers allocated
  920. on the PVR350 itself. For now we'll hardcode the smallest osd buffer
  921. size to prevent any overlap. */
  922. oi->video_buffer_size = 1704960;
  923. oi->video_pbase = itv->base_addr + IVTV_DECODER_OFFSET + oi->video_rbase;
  924. oi->video_vbase = itv->dec_mem + oi->video_rbase;
  925. if (!oi->video_vbase) {
  926. IVTVFB_ERR("abort, video memory 0x%x @ 0x%lx isn't mapped!\n",
  927. oi->video_buffer_size, oi->video_pbase);
  928. return -EIO;
  929. }
  930. IVTVFB_INFO("Framebuffer at 0x%lx, mapped to 0x%p, size %dk\n",
  931. oi->video_pbase, oi->video_vbase,
  932. oi->video_buffer_size / 1024);
  933. while (!(oi->video_buffer_size & (1 << size_shift)))
  934. size_shift--;
  935. size_shift++;
  936. oi->fb_start_aligned_physaddr = oi->video_pbase & ~((1 << size_shift) - 1);
  937. oi->fb_end_aligned_physaddr = oi->video_pbase + oi->video_buffer_size;
  938. oi->fb_end_aligned_physaddr += (1 << size_shift) - 1;
  939. oi->fb_end_aligned_physaddr &= ~((1 << size_shift) - 1);
  940. oi->wc_cookie = arch_phys_wc_add(oi->fb_start_aligned_physaddr,
  941. oi->fb_end_aligned_physaddr -
  942. oi->fb_start_aligned_physaddr);
  943. /* Blank the entire osd. */
  944. memset_io(oi->video_vbase, 0, oi->video_buffer_size);
  945. return 0;
  946. }
  947. /* Release any memory we've grabbed & remove mtrr entry */
  948. static void ivtvfb_release_buffers (struct ivtv *itv)
  949. {
  950. struct osd_info *oi = itv->osd_info;
  951. /* Release cmap */
  952. if (oi->ivtvfb_info.cmap.len)
  953. fb_dealloc_cmap(&oi->ivtvfb_info.cmap);
  954. /* Release pseudo palette */
  955. kfree(oi->ivtvfb_info.pseudo_palette);
  956. arch_phys_wc_del(oi->wc_cookie);
  957. kfree(oi);
  958. itv->osd_info = NULL;
  959. }
  960. /* Initialize the specified card */
  961. static int ivtvfb_init_card(struct ivtv *itv)
  962. {
  963. int rc;
  964. #ifdef CONFIG_X86_64
  965. if (pat_enabled()) {
  966. pr_warn("ivtvfb needs PAT disabled, boot with nopat kernel parameter\n");
  967. return -ENODEV;
  968. }
  969. #endif
  970. if (itv->osd_info) {
  971. IVTVFB_ERR("Card %d already initialised\n", ivtvfb_card_id);
  972. return -EBUSY;
  973. }
  974. itv->osd_info = kzalloc(sizeof(struct osd_info),
  975. GFP_KERNEL|__GFP_NOWARN);
  976. if (itv->osd_info == NULL) {
  977. IVTVFB_ERR("Failed to allocate memory for osd_info\n");
  978. return -ENOMEM;
  979. }
  980. /* Find & setup the OSD buffer */
  981. rc = ivtvfb_init_io(itv);
  982. if (rc) {
  983. ivtvfb_release_buffers(itv);
  984. return rc;
  985. }
  986. /* Set the startup video mode information */
  987. if ((rc = ivtvfb_init_vidmode(itv))) {
  988. ivtvfb_release_buffers(itv);
  989. return rc;
  990. }
  991. /* Register the framebuffer */
  992. if (register_framebuffer(&itv->osd_info->ivtvfb_info) < 0) {
  993. ivtvfb_release_buffers(itv);
  994. return -EINVAL;
  995. }
  996. itv->osd_video_pbase = itv->osd_info->video_pbase;
  997. /* Set the card to the requested mode */
  998. ivtvfb_set_par(&itv->osd_info->ivtvfb_info);
  999. /* Set color 0 to black */
  1000. write_reg(0, 0x02a30);
  1001. write_reg(0, 0x02a34);
  1002. /* Enable the osd */
  1003. ivtvfb_blank(FB_BLANK_UNBLANK, &itv->osd_info->ivtvfb_info);
  1004. /* Enable restart */
  1005. itv->ivtvfb_restore = ivtvfb_restore;
  1006. /* Allocate DMA */
  1007. ivtv_udma_alloc(itv);
  1008. return 0;
  1009. }
  1010. static int __init ivtvfb_callback_init(struct device *dev, void *p)
  1011. {
  1012. struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
  1013. struct ivtv *itv = container_of(v4l2_dev, struct ivtv, v4l2_dev);
  1014. if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT) {
  1015. if (ivtvfb_init_card(itv) == 0) {
  1016. IVTVFB_INFO("Framebuffer registered on %s\n",
  1017. itv->v4l2_dev.name);
  1018. (*(int *)p)++;
  1019. }
  1020. }
  1021. return 0;
  1022. }
  1023. static int ivtvfb_callback_cleanup(struct device *dev, void *p)
  1024. {
  1025. struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
  1026. struct ivtv *itv = container_of(v4l2_dev, struct ivtv, v4l2_dev);
  1027. struct osd_info *oi = itv->osd_info;
  1028. if (itv->v4l2_cap & V4L2_CAP_VIDEO_OUTPUT) {
  1029. if (unregister_framebuffer(&itv->osd_info->ivtvfb_info)) {
  1030. IVTVFB_WARN("Framebuffer %d is in use, cannot unload\n",
  1031. itv->instance);
  1032. return 0;
  1033. }
  1034. IVTVFB_INFO("Unregister framebuffer %d\n", itv->instance);
  1035. itv->ivtvfb_restore = NULL;
  1036. ivtvfb_blank(FB_BLANK_VSYNC_SUSPEND, &oi->ivtvfb_info);
  1037. ivtvfb_release_buffers(itv);
  1038. itv->osd_video_pbase = 0;
  1039. }
  1040. return 0;
  1041. }
  1042. static int __init ivtvfb_init(void)
  1043. {
  1044. struct device_driver *drv;
  1045. int registered = 0;
  1046. int err;
  1047. if (ivtvfb_card_id < -1 || ivtvfb_card_id >= IVTV_MAX_CARDS) {
  1048. pr_err("ivtvfb_card_id parameter is out of range (valid range: -1 - %d)\n",
  1049. IVTV_MAX_CARDS - 1);
  1050. return -EINVAL;
  1051. }
  1052. drv = driver_find("ivtv", &pci_bus_type);
  1053. err = driver_for_each_device(drv, NULL, &registered, ivtvfb_callback_init);
  1054. (void)err; /* suppress compiler warning */
  1055. if (!registered) {
  1056. pr_err("no cards found\n");
  1057. return -ENODEV;
  1058. }
  1059. return 0;
  1060. }
  1061. static void ivtvfb_cleanup(void)
  1062. {
  1063. struct device_driver *drv;
  1064. int err;
  1065. pr_info("Unloading framebuffer module\n");
  1066. drv = driver_find("ivtv", &pci_bus_type);
  1067. err = driver_for_each_device(drv, NULL, NULL, ivtvfb_callback_cleanup);
  1068. (void)err; /* suppress compiler warning */
  1069. }
  1070. module_init(ivtvfb_init);
  1071. module_exit(ivtvfb_cleanup);