1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
|
/*
* GraphLCD driver library
*
* ax206dpf.h - AX206dpf driver class
* Output goes to AX 206 based photoframe
*
* based on:
* simlcd device
* (c) 2001-2004 Carsten Siebholz <c.siebholz AT t-online.de>
* (c) 2011 Dirk Heiser
* (c) 2011 Wolfgang Astleitner <mrwastl AT users.sourceforge.net>
*
* includes code from:
* http://sourceforge.net/projects/dpf-ax/
*
* Original copyright:
*
* Copyright (C) 2008 Jeroen Domburg <picframe@spritesmods.com>
* Modified from sample code by:
* Copyright (C) 2005 Michael Reinelt <michael@reinelt.co.at>
* Copyright (C) 2005, 2006, 2007 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
* Mods by <hackfin@section5.ch>
*
* This file is released under the GNU General Public License. Refer
* to the COPYING file distributed with this package.
*
* (c) 2011 Lutz Neumann <superelchi AT wolke7.de>
*
* HISTORY
*
* v0.1 - 10 Aug 2011 - Inital release
*
*/
#include <stdio.h>
#include <syslog.h>
#include <cstring>
#include "common.h"
#include "config.h"
#include "ax206dpf.h"
namespace GLCD
{
static LIBDPF::DPFContext *dpfh;
cDriverAX206DPF::cDriverAX206DPF(cDriverConfig * config)
: config(config)
{
oldConfig = new cDriverConfig(*config);
}
cDriverAX206DPF::~cDriverAX206DPF()
{
delete oldConfig;
}
int cDriverAX206DPF::Init(void)
{
zoom = 1;
bool forcePortrait = false;
for (unsigned int i = 0; i < config->options.size(); i++)
{
if (config->options[i].name == "Portrait") {
forcePortrait = config->options[i].value == "yes";
}
else if (config->options[i].name == "Zoom") {
int z = strtol(config->options[i].value.c_str(), (char **) NULL, 0);
if (z > 0 && z <= 4)
zoom = z;
else
syslog(LOG_ERR, "%s error: zoom %d not supported, using default (%d)!\n",config->name.c_str(), z, zoom);
}
}
// Init display
int error;
char index;
if (config->device.length() != 4 || config->device.compare(0, 3, "dpf"))
index = '0';
else
index = config->device.at(3);
char usb_device[5];
sprintf(usb_device, "usb%c", index);
error = dpf_open(usb_device, &dpfh);
if (error < 0) {
syslog(LOG_INFO, "%s: can not open dpf device number '%c' (error %d).\n", config->name.c_str(), index, error);
return -1;
}
// See, if we have to rotate the display
isPortrait = dpfh->width < dpfh->height;
rotate90 = isPortrait != forcePortrait;
// Set width / height from display
unsigned int vwidth = (!rotate90) ? dpfh->width : dpfh->height;
unsigned int vheight = (!rotate90) ? dpfh->height : dpfh->width;
width = config->width;
if (width <= 0 || width > (int) vwidth)
width = (int) vwidth;
height = config->height;
if (height <= 0 || height > (int) vheight)
height = (int) vheight;
if (zoom > 1)
{
height /= zoom;
width /= zoom;
}
// setup lcd array
LCD = (unsigned char *) malloc(dpfh->height * dpfh->width * dpfh->bpp);
ResetMinMax();
*oldConfig = *config;
// Set Display Brightness -- NOT WORKING!
//SetBrightness(config->brightness ? config->brightness : 100);
syslog(LOG_INFO, "%s: AX206DPF initialized (%dx%d).\n", config->name.c_str(), width, height);
return 0;
}
int cDriverAX206DPF::DeInit(void)
{
dpf_close(dpfh);
// free lcd array
if (LCD)
{
free(LCD);
}
return 0;
}
void cDriverAX206DPF::ResetMinMax(void)
{
miny = dpfh->height - 1;
maxy = 0;
}
int cDriverAX206DPF::CheckSetup(void)
{
if (config->width != oldConfig->width ||
config->height != oldConfig->height)
{
DeInit();
Init();
return 0;
}
if (config->brightness != oldConfig->brightness)
{
oldConfig->brightness = config->brightness;
SetBrightness(config->brightness);
}
if (config->upsideDown != oldConfig->upsideDown ||
config->invert != oldConfig->invert)
{
oldConfig->upsideDown = config->upsideDown;
oldConfig->invert = config->invert;
return 1;
}
return 0;
}
void cDriverAX206DPF::Clear(void)
{
memset(LCD, 0, dpfh->height * dpfh->width * dpfh->bpp); //Black
miny = 0;
maxy = dpfh->height - 1;
}
#define _RGB565_0(p) \
(( ((p >> 16) & 0xf8) ) | (((p >> 8) & 0xe0) >> 5))
#define _RGB565_1(p) \
(( ((p >> 8) & 0x1c) << 3 ) | (((p) & 0xf8) >> 3))
void cDriverAX206DPF::SetPixel(int x, int y, uint32_t data)
{
bool flip = config->upsideDown;
if (!isPortrait && rotate90)
flip = !flip;
if (flip)
{
// upside down orientation
x = width - 1 - x;
y = height - 1 - y;
}
unsigned int i;
if (rotate90)
{
// wrong Orientation, rotate
int xn = y;
y = dpfh->height - 1 - x;
x = xn;
}
if (x >= ((int) dpfh->width / zoom) || y >= ((int) dpfh->height / zoom))
return;
y *= zoom;
if (zoom == 1)
{
i = (y * dpfh->width + x) * dpfh->bpp;
LCD[i] = _RGB565_0(data);
LCD[i+1] = _RGB565_1(data);
}
else
{
for (int dy = 0; dy < zoom; dy++)
{
i = ((y + dy) * dpfh->width + (x * zoom)) * dpfh->bpp;
for (int dx = 0; dx < zoom * dpfh->bpp; dx += dpfh->bpp)
{
LCD[i+dx] = _RGB565_0(data);
LCD[i+dx+1] = _RGB565_1(data);
}
}
}
if (y * zoom < miny) miny = y;
if (y * zoom > maxy) maxy = y;
}
void cDriverAX206DPF::Refresh(bool refreshAll)
{
if (CheckSetup() > 0)
refreshAll = true;
short rect[4];
if (!refreshAll)
{
rect[0] = 0; rect[1] = miny;
rect[2] = dpfh-> width; rect[3] = maxy + 1;
}
else
{
rect[0] = 0; rect[1] = 0;
rect[2] = dpfh->width; rect[3] = dpfh->height;
}
if (rect[1] > rect[3])
return;
dpf_screen_blit(dpfh, &LCD[rect[1] * dpfh->width * dpfh->bpp], rect);
ResetMinMax();
}
GLCD::cColor cDriverAX206DPF::GetBackgroundColor(void)
{
return GLCD::cColor::Black;
}
/* NOT WORKING - WILL FREEZE THE DISPLAY FROM TIME TO TIME!
void cDriverAX206DPF::SetBrightness(unsigned int percent)
{
LIBDPF::DPFValue val;
val.type = LIBDPF::TYPE_INTEGER;
// Brightness can be 0 .. 7
if (percent == 0)
val.value.integer = 0;
else if (percent >= 100)
val.value.integer = 7;
else
val.value.integer = (((percent * 10) + 167) * 6) / 1000;
dpf_setproperty(dpfh, PROPERTY_BRIGHTNESS, &val);
}
*/
bool cDriverAX206DPF::GetDriverFeature(const std::string & Feature, int & value)
{
if (strcasecmp(Feature.c_str(), "depth") == 0) {
value = 16;
return true;
} else if (strcasecmp(Feature.c_str(), "ismonochrome") == 0) {
value = false;
return true;
} else if (strcasecmp(Feature.c_str(), "isgreyscale") == 0 || strcasecmp(Feature.c_str(), "isgrayscale") == 0) {
value = false;
return true;
} else if (strcasecmp(Feature.c_str(), "iscolour") == 0 || strcasecmp(Feature.c_str(), "iscolor") == 0) {
value = true;
return true;
} else if (strcasecmp(Feature.c_str(), "touch") == 0 || strcasecmp(Feature.c_str(), "touchscreen") == 0) {
value = false;
return true;
}
value = 0;
return false;
}
} // end of namespace GLCD
//##########################################################################################
//
// ** START OF UGLY HACK ** START OF UGLY HACK ** START OF UGLY HACK ** START OF UGLY HACK *
//
// Because I choose NOT to include the static library libdpf.a and/or their header- and
// source-files from the original dpf-ax hack, I did some creative copy & paste from there
// to this place. I will delete this stuff when a usable shared library of libpdf exists.
//
//##########################################################################################
namespace LIBDPF
{
// -------------------------------------------------------------------
// START SELECTIVE COPY & PASTE "dpflib.c"
// -------------------------------------------------------------------
/** DPF access library for AX206 based HW
*
* 12/2010 <hackfin@section5.ch>
*
* This is an ugly hack, because we use existing SCSI vendor specific
* extensions to pass on our requests to the DPF.
*
* One day we might use a proper protocol like netpp.
*
*/
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <scsi/scsi.h>
#include <scsi/sg.h>
#include <sys/ioctl.h>
/** Vendor command for our hacks */
static
unsigned char g_excmd[16] = {
0xcd, 0, 0, 0,
0, 6, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0
};
int wrap_scsi(DPFContext *h, unsigned char *cmd, int cmdlen, char out,
unsigned char *data, unsigned long block_len)
{
int error;
//if (h->mode == MODE_USB) {
error = emulate_scsi(h->dev.udev, cmd, cmdlen, out, data, block_len);
//} else {
// error = do_scsi(h->dev.fd, cmd, cmdlen, out, data, block_len);
//}
return error;
}
static
int probe(DPFHANDLE h)
{
int ret;
// We abuse a command that just responds with a '0' status in the
// original firmware.
static unsigned char buf[5];
static
unsigned char cmd[16] = {
0xcd, 0, 0, 0,
0, 3, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0
};
cmd[5] = 3;
ret = wrap_scsi(h, cmd, sizeof(cmd), DIR_IN, 0, 0);
switch (ret) {
case 0:
// The original protocol.
fprintf(stderr,
"Warning: This firmware can not lock the flash\n");
break;
case 1:
// The improved hack
h->flags |= FLAG_CAN_LOCK;
break;
}
cmd[5] = 2; // get LCD parameters
ret = wrap_scsi(h, cmd, sizeof(cmd), DIR_IN, buf, 5);
h->width = (buf[0]) | (buf[1] << 8);
h->height = (buf[2]) | (buf[3] << 8);
h->bpp = 2;
return ret;
}
int dpf_open(const char *dev, DPFHANDLE *h)
{
int error = 0;
DPFContext *dpf;
int i;
usb_dev_handle *u;
//int fd;
if (!dev) {
fprintf(stderr, "Please specify a string like 'usb0' or a sg device\n");
return DEVERR_OPEN;
}
if (strncmp(dev, "usb", 3) == 0) {
i = dev[3] - '0';
u = dpf_usb_open(i);
if (!u) return DEVERR_OPEN;
//i = MODE_USB;
} else {
return DEVERR_OPEN;
}
//} else {
// fprintf(stderr, "Opening generic SCSI device '%s'\n", dev);
// if (sgdev_open(dev, &fd) < 0) return DEVERR_OPEN;
// i = MODE_SG;
//}
dpf = (DPFHANDLE) malloc(sizeof(DPFContext));
if (!dpf) return DEVERR_MALLOC;
dpf->flags = 0;
dpf->mode = i;
//if (dpf->mode == MODE_USB) {
dpf->dev.udev = u;
error = probe(dpf);
fprintf(stderr, "Got LCD dimensions: %dx%d\n", dpf->width, dpf->height);
//} else {
// dpf->dev.fd = fd;
//}
*h = dpf;
return error;
}
void dpf_close(DPFContext *h)
{
//switch (h->mode) {
// case MODE_SG:
// close(h->dev.fd);
// break;
// case MODE_USB:
usb_release_interface(h->dev.udev, 0);
usb_close(h->dev.udev);
// break;
//}
free(h);
}
const char *dev_errstr(int err)
{
switch (err) {
case DEVERR_FILE: return "File I/O error";
case DEVERR_OPEN: return "File open error";
case DEVERR_HEX: return "Hex file error";
case DEVERR_CHK: return "Checksum error";
case DEVERR_IO: return "Common I/O error";
default: return "Unknown error";
}
}
#define RGB565_0(r, g, b) \
(( ((r) & 0xf8) ) | (((g) & 0xe0) >> 5))
#define RGB565_1(r, g, b) \
(( ((g) & 0x1c) << 3 ) | (((b) & 0xf8) >> 3))
int dpf_setcol(DPFContext *h, const unsigned char *rgb)
{
unsigned char *cmd = g_excmd;
cmd[6] = USBCMD_SETPROPERTY;
cmd[7] = PROPERTY_FGCOLOR;
cmd[8] = PROPERTY_FGCOLOR >> 8;
cmd[9] = RGB565_0(rgb[0], rgb[1], rgb[2]);
cmd[10] = RGB565_1(rgb[0], rgb[1], rgb[2]);
return wrap_scsi(h, cmd, sizeof(g_excmd), DIR_OUT, NULL, 0);
}
int dpf_screen_blit(DPFContext *h,
const unsigned char *buf, short rect[4])
{
unsigned long len = (rect[2] - rect[0]) * (rect[3] - rect[1]);
len <<= 1;
unsigned char *cmd = g_excmd;
cmd[6] = USBCMD_BLIT;
cmd[7] = rect[0];
cmd[8] = rect[0] >> 8;
cmd[9] = rect[1];
cmd[10] = rect[1] >> 8;
cmd[11] = rect[2] - 1;
cmd[12] = (rect[2] - 1) >> 8;
cmd[13] = rect[3] - 1;
cmd[14] = (rect[3] - 1) >> 8;
cmd[15] = 0;
return wrap_scsi(h, cmd, sizeof(g_excmd), DIR_OUT,
(unsigned char*) buf, len);
}
int dpf_setproperty(DPFContext *h, int token, const DPFValue *value)
{
unsigned char *cmd = g_excmd;
cmd[6] = USBCMD_SETPROPERTY;
cmd[7] = token;
cmd[8] = token >> 8;
switch (value->type) {
case TYPE_INTEGER:
cmd[9] = value->value.integer;
cmd[10] = value->value.integer >> 8;
break;
default:
break;
}
return wrap_scsi(h, cmd, sizeof(g_excmd), DIR_OUT, NULL, 0);
}
// -------------------------------------------------------------------
// END SELECTIVE COPY & PASTE "dpflib.c"
// -------------------------------------------------------------------
// -------------------------------------------------------------------
// START SELECTIVE COPY & PASTE "rawusb.c"
// -------------------------------------------------------------------
/* Low level USB code to access DPF.
*
* (c) 2010, 2011 <hackfin@section5.ch>
*
* This currently uses the SCSI command set
*
* The reason for this is that we want to access the hacked frame
* non-root and without having to wait for the SCSI interface to
* intialize.
*
* Later, we'll replace the SCSI command stuff.
*/
#define ENDPT_OUT 1
#define ENDPT_IN 0x81
struct known_device {
const char *desc;
unsigned short vid;
unsigned short pid;
} g_known_devices[] = {
{ "AX206 DPF", 0x1908, 0x0102 },
{ 0 , 0, 0 } /* NEVER REMOVE THIS */
};
int handle_error(const char *txt)
{
fprintf(stderr, "Error: %s\n", txt);
return -1;
}
void usb_flush(usb_dev_handle *dev)
{
char buf[20];
usb_bulk_read(dev, ENDPT_IN, buf, 3, 1000);
}
int check_known_device(struct usb_device *d)
{
struct known_device *dev = g_known_devices;
while (dev->desc) {
if ((d->descriptor.idVendor == dev->vid) &&
(d->descriptor.idProduct == dev->pid)) {
fprintf(stderr, "Found %s\n", dev->desc);
return 1;
}
dev++;
}
return 0;
}
static struct usb_device *find_dev(int index)
{
struct usb_bus *b;
struct usb_device *d;
int enumeration = 0;
b = usb_get_busses();
while (b) {
d = b->devices;
while (d) {
if (check_known_device(d)) {
if (enumeration == index) return d;
else enumeration++;
}
#ifdef DEBUG
printf("%04x %04x\n",
d->descriptor.idVendor,
d->descriptor.idProduct);
#endif
d = d->next;
}
b = b->next;
}
return NULL;
}
char g_buf[] = {
0x55, 0x53, 0x42, 0x43, // dCBWSignature
0xde, 0xad, 0xbe, 0xef, // dCBWTag
0x00, 0x80, 0x00, 0x00, // dCBWLength
0x00, // bmCBWFlags: 0x80: data in (dev to host), 0x00: Data out
0x00, // bCBWLUN
0x10, // bCBWCBLength
// SCSI cmd:
0xcd, 0x00, 0x00, 0x00,
0x00, 0x06, 0x11, 0xf8,
0x70, 0x00, 0x40, 0x00,
0x00, 0x00, 0x00, 0x00,
};
int emulate_scsi(usb_dev_handle *dev, unsigned char *cmd, int cmdlen, char out,
unsigned char *data, unsigned long block_len)
{
int len;
int ret;
static unsigned char ansbuf[13]; // Do not change size.
g_buf[14] = cmdlen;
memcpy(&g_buf[15], cmd, cmdlen);
g_buf[8] = block_len;
g_buf[9] = block_len >> 8;
g_buf[10] = block_len >> 16;
g_buf[11] = block_len >> 24;
ret = usb_bulk_write(dev, ENDPT_OUT, g_buf, sizeof(g_buf), 1000);
if (ret < 0) return ret;
if (out == DIR_OUT) {
if (data) {
ret = usb_bulk_write(dev, ENDPT_OUT, (char* )data,
block_len, 3000);
if (ret != (int) block_len) {
perror("bulk write");
return ret;
}
}
} else if (data) {
ret = usb_bulk_read(dev, ENDPT_IN, (char *) data, block_len, 4000);
if (ret != (int) block_len) {
perror("bulk data read");
}
}
// get ACK:
len = sizeof(ansbuf);
int retry = 0;
do {
ret = usb_bulk_read(dev, ENDPT_IN, (char *) ansbuf, len, 5000);
if (ret != len) {
perror("bulk ACK read");
ret = DEVERR_TIMEOUT;
}
retry++;
} while (ret == DEVERR_TIMEOUT && retry < 5);
if (strncmp((char *) ansbuf, "USBS", 4)) {
return handle_error("Got invalid reply\n");
}
// pass back return code set by peer:
return ansbuf[12];
}
usb_dev_handle *dpf_usb_open(int index)
{
struct usb_device *d;
usb_dev_handle *usb_dev;
usb_init();
usb_find_busses();
usb_find_devices();
d = find_dev(index);
if (!d) {
handle_error("No matching USB device found!");
return NULL;
}
usb_dev = usb_open(d);
if (usb_dev == NULL) {
handle_error("Failed to open usb device!");
return NULL;
}
usb_claim_interface(usb_dev, 0);
return usb_dev;
}
// -------------------------------------------------------------------
// END SELECTIVE COPY & PASTE "rawusb.c"
// -------------------------------------------------------------------
} // end of namespace LIBDPF
//##########################################################################################
// ** END OF UGLY HACK ** END OF UGLY HACK ** END OF UGLY HACK ** END OF UGLY HACK *
//##########################################################################################
|