Subversion Repositories Kolibri OS

Rev

Rev 4080 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
4075 Serge 1
#include 
2
#include 
3
 
4
#include 
5
#include 
6
 
7
#include "vmwgfx_drv.h"
8
 
9
#include 
10
#include 
11
#include 
12
#include 
13
 
14
#include "bitmap.h"
15
 
16
struct pci_device {
17
    uint16_t    domain;
18
    uint8_t     bus;
19
    uint8_t     dev;
20
    uint8_t     func;
21
    uint16_t    vendor_id;
22
    uint16_t    device_id;
23
    uint16_t    subvendor_id;
24
    uint16_t    subdevice_id;
25
    uint32_t    device_class;
26
    uint8_t     revision;
27
};
28
 
29
extern struct drm_device *main_device;
30
extern struct drm_file   *drm_file_handlers[256];
31
 
32
int vmw_init(void);
33
void cpu_detect();
34
 
35
void parse_cmdline(char *cmdline, char *log);
36
int _stdcall display_handler(ioctl_t *io);
37
 
38
int srv_blit_bitmap(u32 hbitmap, int  dst_x, int dst_y,
39
               int src_x, int src_y, u32 w, u32 h);
40
 
41
int blit_textured(u32 hbitmap, int  dst_x, int dst_y,
42
               int src_x, int src_y, u32 w, u32 h);
43
 
44
int blit_tex(u32 hbitmap, int  dst_x, int dst_y,
45
             int src_x, int src_y, u32 w, u32 h);
46
 
47
void get_pci_info(struct pci_device *dev);
48
int gem_getparam(struct drm_device *dev, void *data);
49
 
50
int i915_mask_update(struct drm_device *dev, void *data,
51
            struct drm_file *file);
52
 
53
 
54
static char  log[256];
55
 
56
struct workqueue_struct *system_wq;
57
int driver_wq_state;
58
 
59
int x86_clflush_size;
60
unsigned int tsc_khz;
61
 
62
int i915_modeset = 1;
63
 
64
u32_t  __attribute__((externally_visible)) drvEntry(int action, char *cmdline)
65
{
66
 
67
    int     err = 0;
68
 
69
    if(action != 1)
70
    {
71
        driver_wq_state = 0;
72
        return 0;
73
    };
74
 
75
    if( GetService("DISPLAY") != 0 )
76
        return 0;
77
 
78
    if( cmdline && *cmdline )
79
        parse_cmdline(cmdline, log);
80
 
81
    if(!dbg_open(log))
82
    {
83
//        strcpy(log, "/tmp1/1/vmw.log");
84
//        strcpy(log, "/RD/1/DRIVERS/VMW.log");
85
        strcpy(log, "/HD0/1/vmw.log");
86
 
87
        if(!dbg_open(log))
88
        {
89
            printf("Can't open %s\nExit\n", log);
90
            return 0;
91
        };
92
    }
93
    dbgprintf(" vmw v3.10\n cmdline: %s\n", cmdline);
94
 
95
    cpu_detect();
96
    dbgprintf("\ncache line size %d\n", x86_clflush_size);
97
 
98
    enum_pci_devices();
99
 
100
    err = vmw_init();
101
 
102
    if(err)
103
    {
104
        dbgprintf("Epic Fail :(\n");
105
        return 0;
106
    };
107
 
108
    err = RegService("DISPLAY", display_handler);
109
 
110
    if( err != 0)
111
        dbgprintf("Set DISPLAY handler\n");
112
 
113
//    struct drm_i915_private *dev_priv = main_device->dev_private;
114
//    driver_wq_state = 1;
115
//    run_workqueue(dev_priv->wq);
116
 
117
    return err;
118
};
119
 
120
 
121
#define CURRENT_API     0x0200      /*      2.00     */
122
#define COMPATIBLE_API  0x0100      /*      1.00     */
123
 
124
#define API_VERSION     (COMPATIBLE_API << 16) | CURRENT_API
125
#define DISPLAY_VERSION  API_VERSION
126
 
127
 
128
#define SRV_GETVERSION          0
129
#define SRV_ENUM_MODES          1
130
#define SRV_SET_MODE            2
131
#define SRV_GET_CAPS            3
132
 
133
#define SRV_CREATE_SURFACE      10
134
#define SRV_DESTROY_SURFACE     11
135
#define SRV_LOCK_SURFACE        12
136
#define SRV_UNLOCK_SURFACE      13
137
#define SRV_RESIZE_SURFACE      14
138
#define SRV_BLIT_BITMAP         15
139
#define SRV_BLIT_TEXTURE        16
140
#define SRV_BLIT_VIDEO          17
141
 
142
 
143
#define SRV_GET_PCI_INFO            20
144
#define SRV_GET_PARAM               21
145
#define SRV_I915_GEM_CREATE         22
146
#define SRV_DRM_GEM_CLOSE           23
147
#define SRV_I915_GEM_PIN            24
148
#define SRV_I915_GEM_SET_CACHEING   25
149
#define SRV_I915_GEM_GET_APERTURE   26
150
#define SRV_I915_GEM_PWRITE         27
151
#define SRV_I915_GEM_BUSY           28
152
#define SRV_I915_GEM_SET_DOMAIN     29
153
#define SRV_I915_GEM_MMAP           30
154
#define SRV_I915_GEM_MMAP_GTT       31
155
#define SRV_I915_GEM_THROTTLE       32
156
#define SRV_FBINFO                  33
157
#define SRV_I915_GEM_EXECBUFFER2    34
158
#define SRV_MASK_UPDATE             35
159
 
160
 
161
 
162
#define check_input(size) \
163
    if( unlikely((inp==NULL)||(io->inp_size != (size))) )   \
164
        break;
165
 
166
#define check_output(size) \
167
    if( unlikely((outp==NULL)||(io->out_size != (size))) )   \
168
        break;
169
 
170
int _stdcall display_handler(ioctl_t *io)
171
{
172
    struct drm_file *file;
173
 
174
    int    retval = -1;
175
    u32_t *inp;
176
    u32_t *outp;
177
 
178
    inp = io->input;
179
    outp = io->output;
180
 
181
    file = drm_file_handlers[0];
182
 
183
    switch(io->io_code)
184
    {
185
        case SRV_GETVERSION:
186
            check_output(4);
187
            *outp  = DISPLAY_VERSION;
188
            retval = 0;
189
            break;
190
#if 0
191
        case SRV_ENUM_MODES:
192
//            dbgprintf("SRV_ENUM_MODES inp %x inp_size %x out_size %x\n",
193
//                       inp, io->inp_size, io->out_size );
194
            check_output(4);
195
//            check_input(*outp * sizeof(videomode_t));
196
            if( i915_modeset)
197
                retval = get_videomodes((videomode_t*)inp, outp);
198
            break;
199
 
200
        case SRV_SET_MODE:
201
//            dbgprintf("SRV_SET_MODE inp %x inp_size %x\n",
202
//                       inp, io->inp_size);
203
            check_input(sizeof(videomode_t));
204
            if( i915_modeset )
205
                retval = set_user_mode((videomode_t*)inp);
206
            break;
207
 
208
        case SRV_GET_CAPS:
209
            retval = get_driver_caps((hwcaps_t*)inp);
210
            break;
211
 
212
        case SRV_CREATE_SURFACE:
213
//            check_input(8);
214
//            retval = create_surface(main_device, (struct io_call_10*)inp);
215
            break;
216
 
217
        case SRV_LOCK_SURFACE:
218
//            retval = lock_surface((struct io_call_12*)inp);
219
            break;
220
 
221
        case SRV_RESIZE_SURFACE:
222
//            retval = resize_surface((struct io_call_14*)inp);
223
            break;
224
 
225
        case SRV_BLIT_BITMAP:
226
//            srv_blit_bitmap( inp[0], inp[1], inp[2],
227
//                        inp[3], inp[4], inp[5], inp[6]);
228
 
229
//            blit_tex( inp[0], inp[1], inp[2],
230
//                    inp[3], inp[4], inp[5], inp[6]);
231
 
232
            break;
233
 
234
        case SRV_GET_PCI_INFO:
235
            get_pci_info((struct pci_device *)inp);
236
            retval = 0;
237
            break;
238
 
239
        case SRV_GET_PARAM:
240
            retval = gem_getparam(main_device, inp);
241
            break;
242
 
243
        case SRV_I915_GEM_CREATE:
244
            retval = i915_gem_create_ioctl(main_device, inp, file);
245
            break;
246
 
247
        case SRV_DRM_GEM_CLOSE:
248
            retval = drm_gem_close_ioctl(main_device, inp, file);
249
            break;
250
 
251
        case SRV_I915_GEM_PIN:
252
            retval = i915_gem_pin_ioctl(main_device, inp, file);
253
            break;
254
 
255
        case SRV_I915_GEM_SET_CACHEING:
256
            retval = i915_gem_set_caching_ioctl(main_device, inp, file);
257
            break;
258
 
259
        case SRV_I915_GEM_GET_APERTURE:
260
            retval = i915_gem_get_aperture_ioctl(main_device, inp, file);
261
            break;
262
 
263
        case SRV_I915_GEM_PWRITE:
264
            retval = i915_gem_pwrite_ioctl(main_device, inp, file);
265
            break;
266
 
267
        case SRV_I915_GEM_BUSY:
268
            retval = i915_gem_busy_ioctl(main_device, inp, file);
269
            break;
270
 
271
        case SRV_I915_GEM_SET_DOMAIN:
272
            retval = i915_gem_set_domain_ioctl(main_device, inp, file);
273
            break;
274
 
275
        case SRV_I915_GEM_THROTTLE:
276
            retval = i915_gem_throttle_ioctl(main_device, inp, file);
277
            break;
278
 
279
        case SRV_I915_GEM_MMAP:
280
            retval = i915_gem_mmap_ioctl(main_device, inp, file);
281
            break;
282
 
283
        case SRV_I915_GEM_MMAP_GTT:
284
            retval = i915_gem_mmap_gtt_ioctl(main_device, inp, file);
285
            break;
286
 
287
 
288
        case SRV_FBINFO:
289
            retval = i915_fbinfo(inp);
290
            break;
291
 
292
        case SRV_I915_GEM_EXECBUFFER2:
293
            retval = i915_gem_execbuffer2(main_device, inp, file);
294
            break;
295
 
296
        case SRV_MASK_UPDATE:
297
            retval = i915_mask_update(main_device, inp, file);
298
            break;
299
#endif
300
 
301
    };
302
 
303
    return retval;
304
}
305
 
306
 
307
#define PCI_CLASS_REVISION      0x08
308
#define PCI_CLASS_DISPLAY_VGA   0x0300
309
#define PCI_CLASS_BRIDGE_HOST   0x0600
310
#define PCI_CLASS_BRIDGE_ISA    0x0601
311
 
312
int pci_scan_filter(u32_t id, u32_t busnr, u32_t devfn)
313
{
314
    u16_t vendor, device;
315
    u32_t class;
316
    int   ret = 0;
317
 
318
    vendor   = id & 0xffff;
319
    device   = (id >> 16) & 0xffff;
320
 
321
    if(vendor == 0x15AD )
322
    {
323
        class = PciRead32(busnr, devfn, PCI_CLASS_REVISION);
324
        class >>= 16;
325
 
326
        if( class == PCI_CLASS_DISPLAY_VGA )
327
            ret = 1;
328
    }
329
    return ret;
330
};
331
 
332
 
333
static char* parse_path(char *p, char *log)
334
{
335
    char  c;
336
 
337
    while( (c = *p++) == ' ');
338
        p--;
339
    while( (c = *log++ = *p++) && (c != ' '));
340
    *log = 0;
341
 
342
    return p;
343
};
344
 
345
void parse_cmdline(char *cmdline, char *log)
346
{
347
    char *p = cmdline;
348
 
349
    char c = *p++;
350
 
351
    while( c )
352
    {
353
        if( c == '-')
354
        {
355
            switch(*p++)
356
            {
357
                case 'l':
358
                    p = parse_path(p, log);
359
                    break;
360
            };
361
        };
362
        c = *p++;
363
    };
364
};
365
 
366
 
367
static inline void __cpuid(unsigned int *eax, unsigned int *ebx,
368
                unsigned int *ecx, unsigned int *edx)
369
{
370
    /* ecx is often an input as well as an output. */
371
    asm volatile("cpuid"
372
        : "=a" (*eax),
373
          "=b" (*ebx),
374
          "=c" (*ecx),
375
          "=d" (*edx)
376
        : "0" (*eax), "2" (*ecx)
377
        : "memory");
378
}
379
 
380
 
381
 
382
static inline void cpuid(unsigned int op,
383
                         unsigned int *eax, unsigned int *ebx,
384
                         unsigned int *ecx, unsigned int *edx)
385
{
386
        *eax = op;
387
        *ecx = 0;
388
        __cpuid(eax, ebx, ecx, edx);
389
}
390
 
391
void cpu_detect()
392
{
393
    u32 junk, tfms, cap0, misc;
394
 
395
    cpuid(0x00000001, &tfms, &misc, &junk, &cap0);
396
 
397
    if (cap0 & (1<<19))
398
    {
399
        x86_clflush_size = ((misc >> 8) & 0xff) * 8;
400
    }
401
 
402
    tsc_khz = (unsigned int)(GetCpuFreq()/1000);
403
}
404
 
405
/*
406
int get_driver_caps(hwcaps_t *caps)
407
{
408
    int ret = 0;
409
 
410
    switch(caps->idx)
411
    {
412
        case 0:
413
            caps->opt[0] = 0;
414
            caps->opt[1] = 0;
415
            break;
416
 
417
        case 1:
418
            caps->cap1.max_tex_width  = 4096;
419
            caps->cap1.max_tex_height = 4096;
420
            break;
421
        default:
422
            ret = 1;
423
    };
424
    caps->idx = 1;
425
    return ret;
426
}
427
 
428
 
429
void get_pci_info(struct pci_device *dev)
430
{
431
    struct pci_dev *pdev = main_device->pdev;
432
 
433
    memset(dev, sizeof(*dev), 0);
434
 
435
    dev->domain     = 0;
436
    dev->bus        = pdev->busnr;
437
    dev->dev        = pdev->devfn >> 3;
438
    dev->func       = pdev->devfn & 7;
439
    dev->vendor_id  = pdev->vendor;
440
    dev->device_id  = pdev->device;
441
    dev->revision   = pdev->revision;
442
};
443
 
444
*/
445
 
446
#include 
447
#include 
448
#include 
449
#include 
450
#include 
451
 
452
/**
453
 * hdmi_avi_infoframe_init() - initialize an HDMI AVI infoframe
454
 * @frame: HDMI AVI infoframe
455
 *
456
 * Returns 0 on success or a negative error code on failure.
457
 */
458
int hdmi_avi_infoframe_init(struct hdmi_avi_infoframe *frame)
459
{
460
    memset(frame, 0, sizeof(*frame));
461
 
462
    frame->type = HDMI_INFOFRAME_TYPE_AVI;
463
    frame->version = 2;
464
    frame->length = 13;
465
 
466
    return 0;
467
}
468
 
469
 
470
static void *check_bytes8(const u8 *start, u8 value, unsigned int bytes)
471
{
472
        while (bytes) {
473
                if (*start != value)
474
                        return (void *)start;
475
                start++;
476
                bytes--;
477
        }
478
        return NULL;
479
}
480
 
481
/**
482
 * memchr_inv - Find an unmatching character in an area of memory.
483
 * @start: The memory area
484
 * @c: Find a character other than c
485
 * @bytes: The size of the area.
486
 *
487
 * returns the address of the first character other than @c, or %NULL
488
 * if the whole buffer contains just @c.
489
 */
490
void *memchr_inv(const void *start, int c, size_t bytes)
491
{
492
        u8 value = c;
493
        u64 value64;
494
        unsigned int words, prefix;
495
 
496
        if (bytes <= 16)
497
                return check_bytes8(start, value, bytes);
498
 
499
        value64 = value;
500
#if defined(ARCH_HAS_FAST_MULTIPLIER) && BITS_PER_LONG == 64
501
        value64 *= 0x0101010101010101;
502
#elif defined(ARCH_HAS_FAST_MULTIPLIER)
503
        value64 *= 0x01010101;
504
        value64 |= value64 << 32;
505
#else
506
        value64 |= value64 << 8;
507
        value64 |= value64 << 16;
508
        value64 |= value64 << 32;
509
#endif
510
 
511
        prefix = (unsigned long)start % 8;
512
        if (prefix) {
513
                u8 *r;
514
 
515
                prefix = 8 - prefix;
516
                r = check_bytes8(start, value, prefix);
517
                if (r)
518
                        return r;
519
                start += prefix;
520
                bytes -= prefix;
521
        }
522
 
523
        words = bytes / 8;
524
 
525
        while (words) {
526
                if (*(u64 *)start != value64)
527
                        return check_bytes8(start, value, 8);
528
                start += 8;
529
                words--;
530
        }
531
 
532
        return check_bytes8(start, value, bytes % 8);
533
}
534
 
535
int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
536
{
537
    int i;
538
 
539
    i = vsnprintf(buf, size, fmt, args);
540
 
541
    if (likely(i < size))
542
            return i;
543
    if (size != 0)
544
            return size - 1;
545
    return 0;
546
}
547
 
548
 
549
int scnprintf(char *buf, size_t size, const char *fmt, ...)
550
{
551
        va_list args;
552
        int i;
553
 
554
        va_start(args, fmt);
555
        i = vscnprintf(buf, size, fmt, args);
556
        va_end(args);
557
 
558
        return i;
559
}
560
 
561
 
562
 
563
#define _U  0x01    /* upper */
564
#define _L  0x02    /* lower */
565
#define _D  0x04    /* digit */
566
#define _C  0x08    /* cntrl */
567
#define _P  0x10    /* punct */
568
#define _S  0x20    /* white space (space/lf/tab) */
569
#define _X  0x40    /* hex digit */
570
#define _SP 0x80    /* hard space (0x20) */
571
 
572
extern const unsigned char _ctype[];
573
 
574
#define __ismask(x) (_ctype[(int)(unsigned char)(x)])
575
 
576
#define isalnum(c)  ((__ismask(c)&(_U|_L|_D)) != 0)
577
#define isalpha(c)  ((__ismask(c)&(_U|_L)) != 0)
578
#define iscntrl(c)  ((__ismask(c)&(_C)) != 0)
579
#define isdigit(c)  ((__ismask(c)&(_D)) != 0)
580
#define isgraph(c)  ((__ismask(c)&(_P|_U|_L|_D)) != 0)
581
#define islower(c)  ((__ismask(c)&(_L)) != 0)
582
#define isprint(c)  ((__ismask(c)&(_P|_U|_L|_D|_SP)) != 0)
583
#define ispunct(c)  ((__ismask(c)&(_P)) != 0)
584
/* Note: isspace() must return false for %NUL-terminator */
585
#define isspace(c)  ((__ismask(c)&(_S)) != 0)
586
#define isupper(c)  ((__ismask(c)&(_U)) != 0)
587
#define isxdigit(c) ((__ismask(c)&(_D|_X)) != 0)
588
 
589
#define isascii(c) (((unsigned char)(c))<=0x7f)
590
#define toascii(c) (((unsigned char)(c))&0x7f)
591
 
592
 
593
 
594
//const char hex_asc[] = "0123456789abcdef";
595
 
596
/**
597
 * hex_to_bin - convert a hex digit to its real value
598
 * @ch: ascii character represents hex digit
599
 *
600
 * hex_to_bin() converts one hex digit to its actual value or -1 in case of bad
601
 * input.
602
 */
603
int hex_to_bin(char ch)
604
{
605
    if ((ch >= '0') && (ch <= '9'))
606
        return ch - '0';
607
    ch = tolower(ch);
608
    if ((ch >= 'a') && (ch <= 'f'))
609
        return ch - 'a' + 10;
610
    return -1;
611
}
612
EXPORT_SYMBOL(hex_to_bin);
613
 
614
/**
615
 * hex2bin - convert an ascii hexadecimal string to its binary representation
616
 * @dst: binary result
617
 * @src: ascii hexadecimal string
618
 * @count: result length
619
 *
620
 * Return 0 on success, -1 in case of bad input.
621
 */
622
int hex2bin(u8 *dst, const char *src, size_t count)
623
{
624
    while (count--) {
625
        int hi = hex_to_bin(*src++);
626
        int lo = hex_to_bin(*src++);
627
 
628
        if ((hi < 0) || (lo < 0))
629
            return -1;
630
 
631
        *dst++ = (hi << 4) | lo;
632
    }
633
    return 0;
634
}
635
EXPORT_SYMBOL(hex2bin);
636
 
637
/**
638
 * hex_dump_to_buffer - convert a blob of data to "hex ASCII" in memory
639
 * @buf: data blob to dump
640
 * @len: number of bytes in the @buf
641
 * @rowsize: number of bytes to print per line; must be 16 or 32
642
 * @groupsize: number of bytes to print at a time (1, 2, 4, 8; default = 1)
643
 * @linebuf: where to put the converted data
644
 * @linebuflen: total size of @linebuf, including space for terminating NUL
645
 * @ascii: include ASCII after the hex output
646
 *
647
 * hex_dump_to_buffer() works on one "line" of output at a time, i.e.,
648
 * 16 or 32 bytes of input data converted to hex + ASCII output.
649
 *
650
 * Given a buffer of u8 data, hex_dump_to_buffer() converts the input data
651
 * to a hex + ASCII dump at the supplied memory location.
652
 * The converted output is always NUL-terminated.
653
 *
654
 * E.g.:
655
 *   hex_dump_to_buffer(frame->data, frame->len, 16, 1,
656
 *          linebuf, sizeof(linebuf), true);
657
 *
658
 * example output buffer:
659
 * 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f  @ABCDEFGHIJKLMNO
660
 */
661
void hex_dump_to_buffer(const void *buf, size_t len, int rowsize,
662
            int groupsize, char *linebuf, size_t linebuflen,
663
            bool ascii)
664
{
665
    const u8 *ptr = buf;
666
    u8 ch;
667
    int j, lx = 0;
668
    int ascii_column;
669
 
670
    if (rowsize != 16 && rowsize != 32)
671
        rowsize = 16;
672
 
673
    if (!len)
674
        goto nil;
675
    if (len > rowsize)      /* limit to one line at a time */
676
        len = rowsize;
677
    if ((len % groupsize) != 0) /* no mixed size output */
678
        groupsize = 1;
679
 
680
    switch (groupsize) {
681
    case 8: {
682
        const u64 *ptr8 = buf;
683
        int ngroups = len / groupsize;
684
 
685
        for (j = 0; j < ngroups; j++)
686
            lx += scnprintf(linebuf + lx, linebuflen - lx,
687
                    "%s%16.16llx", j ? " " : "",
688
                    (unsigned long long)*(ptr8 + j));
689
        ascii_column = 17 * ngroups + 2;
690
        break;
691
    }
692
 
693
    case 4: {
694
        const u32 *ptr4 = buf;
695
        int ngroups = len / groupsize;
696
 
697
        for (j = 0; j < ngroups; j++)
698
            lx += scnprintf(linebuf + lx, linebuflen - lx,
699
                    "%s%8.8x", j ? " " : "", *(ptr4 + j));
700
        ascii_column = 9 * ngroups + 2;
701
        break;
702
    }
703
 
704
    case 2: {
705
        const u16 *ptr2 = buf;
706
        int ngroups = len / groupsize;
707
 
708
        for (j = 0; j < ngroups; j++)
709
            lx += scnprintf(linebuf + lx, linebuflen - lx,
710
                    "%s%4.4x", j ? " " : "", *(ptr2 + j));
711
        ascii_column = 5 * ngroups + 2;
712
        break;
713
    }
714
 
715
    default:
716
        for (j = 0; (j < len) && (lx + 3) <= linebuflen; j++) {
717
            ch = ptr[j];
718
            linebuf[lx++] = hex_asc_hi(ch);
719
            linebuf[lx++] = hex_asc_lo(ch);
720
            linebuf[lx++] = ' ';
721
        }
722
        if (j)
723
            lx--;
724
 
725
        ascii_column = 3 * rowsize + 2;
726
        break;
727
    }
728
    if (!ascii)
729
        goto nil;
730
 
731
    while (lx < (linebuflen - 1) && lx < (ascii_column - 1))
732
        linebuf[lx++] = ' ';
733
    for (j = 0; (j < len) && (lx + 2) < linebuflen; j++) {
734
        ch = ptr[j];
735
        linebuf[lx++] = (isascii(ch) && isprint(ch)) ? ch : '.';
736
    }
737
nil:
738
    linebuf[lx++] = '\0';
739
}
740
 
741
/**
742
 * print_hex_dump - print a text hex dump to syslog for a binary blob of data
743
 * @level: kernel log level (e.g. KERN_DEBUG)
744
 * @prefix_str: string to prefix each line with;
745
 *  caller supplies trailing spaces for alignment if desired
746
 * @prefix_type: controls whether prefix of an offset, address, or none
747
 *  is printed (%DUMP_PREFIX_OFFSET, %DUMP_PREFIX_ADDRESS, %DUMP_PREFIX_NONE)
748
 * @rowsize: number of bytes to print per line; must be 16 or 32
749
 * @groupsize: number of bytes to print at a time (1, 2, 4, 8; default = 1)
750
 * @buf: data blob to dump
751
 * @len: number of bytes in the @buf
752
 * @ascii: include ASCII after the hex output
753
 *
754
 * Given a buffer of u8 data, print_hex_dump() prints a hex + ASCII dump
755
 * to the kernel log at the specified kernel log level, with an optional
756
 * leading prefix.
757
 *
758
 * print_hex_dump() works on one "line" of output at a time, i.e.,
759
 * 16 or 32 bytes of input data converted to hex + ASCII output.
760
 * print_hex_dump() iterates over the entire input @buf, breaking it into
761
 * "line size" chunks to format and print.
762
 *
763
 * E.g.:
764
 *   print_hex_dump(KERN_DEBUG, "raw data: ", DUMP_PREFIX_ADDRESS,
765
 *          16, 1, frame->data, frame->len, true);
766
 *
767
 * Example output using %DUMP_PREFIX_OFFSET and 1-byte mode:
768
 * 0009ab42: 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f  @ABCDEFGHIJKLMNO
769
 * Example output using %DUMP_PREFIX_ADDRESS and 4-byte mode:
770
 * ffffffff88089af0: 73727170 77767574 7b7a7978 7f7e7d7c  pqrstuvwxyz{|}~.
771
 */
772
void print_hex_dump(const char *level, const char *prefix_str, int prefix_type,
773
            int rowsize, int groupsize,
774
            const void *buf, size_t len, bool ascii)
775
{
776
    const u8 *ptr = buf;
777
    int i, linelen, remaining = len;
778
    unsigned char linebuf[32 * 3 + 2 + 32 + 1];
779
 
780
    if (rowsize != 16 && rowsize != 32)
781
        rowsize = 16;
782
 
783
    for (i = 0; i < len; i += rowsize) {
784
        linelen = min(remaining, rowsize);
785
        remaining -= rowsize;
786
 
787
        hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
788
                   linebuf, sizeof(linebuf), ascii);
789
 
790
        switch (prefix_type) {
791
        case DUMP_PREFIX_ADDRESS:
792
            printk("%s%s%p: %s\n",
793
                   level, prefix_str, ptr + i, linebuf);
794
            break;
795
        case DUMP_PREFIX_OFFSET:
796
            printk("%s%s%.8x: %s\n", level, prefix_str, i, linebuf);
797
            break;
798
        default:
799
            printk("%s%s%s\n", level, prefix_str, linebuf);
800
            break;
801
        }
802
    }
803
}
804
 
805
void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
806
                          const void *buf, size_t len)
807
{
808
    print_hex_dump(KERN_DEBUG, prefix_str, prefix_type, 16, 1,
809
                       buf, len, true);
810
}
811
 
812