forked from TangoCash/lcd4linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drv_SamsungSPF.c
736 lines (620 loc) · 15.5 KB
/
drv_SamsungSPF.c
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
725
726
727
728
729
730
731
732
733
734
735
736
/* $Id: drv_SamsungSPF 975 2009-01-18 11:16:20Z michael $
* $URL: https://ssl.bulix.org/svn/lcd4linux/trunk/drv_SamsungSPF.c $
*
* SamsungSPF lcd4linux driver
*
* Copyright (C) 2012 Sascha Plazar <[email protected]>
* Copyright (C) 2005, 2006, 2007 The LCD4Linux Team <[email protected]>
*
* This driver is based on playusb.c created on Aug 2, 2010 by Andre Puschmann
* which is in turn based on code from Grace Woo:
* http://web.media.mit.edu/~gracewoo/stuff/picframe
*
* This file is part of LCD4Linux.
*
* LCD4Linux is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* LCD4Linux is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
/*
*
* exported fuctions:
*
* struct DRIVER drv_SamsungSPF
*
*/
#include "config.h"
#include <stdio.h>
#include <string.h>
#include <usb.h>
#include <jpeglib.h>
#include "debug.h"
#include "cfg.h"
#include "qprintf.h"
#include "timer.h"
#include "drv.h"
/* graphic display? */
#include "drv_generic_graphic.h"
#include "jpeg_mem_dest.h"
#define FILLSIZE 16384 // 16kB
#define HEADERSIZE 12
// Drivername for verbose output
static char Name[] = "SamsungSPF";
struct SPFdev
{
const char type[64];
const int vendorID;
struct
{
const int storageMode;
const int monitorMode;
} productID;
const unsigned int xRes;
const unsigned int yRes;
};
static struct SPFdev spfDevices[] =
{
{
.type = "SPF-AUTO",
.vendorID = 0x04e8,
.productID = {0xffff, 0xffff},
.xRes = 1,
.yRes = 1,
},
{
.type = "SPF-72H",
.vendorID = 0x04e8,
.productID = {0x200a, 0x200b},
.xRes = 800,
.yRes = 480,
},
{
.type = "SPF-75H",
.vendorID = 0x04e8,
.productID = {0x200e, 0x200f},
.xRes = 800,
.yRes = 480,
},
{
.type = "SPF-76H",
.vendorID = 0x04e8,
.productID = {0x200e, 0x200f},
.xRes = 800,
.yRes = 480,
},
{
.type = "SPF-83H",
.vendorID = 0x04e8,
.productID = {0x200c, 0x200d},
.xRes = 800,
.yRes = 600,
},
{
.type = "SPF-83M",
.vendorID = 0x04e8,
.productID = {0x2005, 0x2006},
.xRes = 800,
.yRes = 600,
},
{
.type = "SPF-85H",
.vendorID = 0x04e8,
.productID = {0x2012, 0x2013},
.xRes = 800,
.yRes = 600,
},
{
.type = "SPF-85P",
.vendorID = 0x04e8,
.productID = {0x2016, 0x2017},
.xRes = 800,
.yRes = 600,
},
{
.type = "SPF-86H",
.vendorID = 0x04e8,
.productID = {0x2012, 0x2013},
.xRes = 800,
.yRes = 600,
},
{
.type = "SPF-86P",
.vendorID = 0x04e8,
.productID = {0x2016, 0x2017},
.xRes = 800,
.yRes = 600,
},
{
.type = "SPF-87H",
.vendorID = 0x04e8,
.productID = {0x2025, 0x2026},
.xRes = 800,
.yRes = 480,
},
{
.type = "SPF-87H-v2",
.vendorID = 0x04e8,
.productID = {0x2033, 0x2034},
.xRes = 800,
.yRes = 480,
},
{
.type = "SPF-105P",
.vendorID = 0x04e8,
.productID = {0x201c, 0x201b},
.xRes = 1024,
.yRes = 600,
},
{
.type = "SPF-107H",
.vendorID = 0x04e8,
.productID = {0x2027, 0x2028},
.xRes = 1024,
.yRes = 600,
},
{
.type = "SPF-107H-v2",
.vendorID = 0x04e8,
.productID = {0x2035, 0x2036},
.xRes = 1024,
.yRes = 600,
},
{
.type = "SPF-700T",
.vendorID = 0x04e8,
.productID = {0x204f, 0x2050},
.xRes = 800,
.yRes = 600,
},
{
.type = "SPF-1000P",
.vendorID = 0x04e8,
.productID = {0x2039, 0x2040},
.xRes = 1024,
.yRes = 600,
},
};
static int numFrames = sizeof(spfDevices) / sizeof(spfDevices[0]);
struct usb_device *myDev;
usb_dev_handle *myDevHandle;
struct SPFdev *myFrame;
typedef struct
{
unsigned char R, G, B;
} RGB;
static struct
{
RGB *buf;
int dirty;
int fbsize;
} image;
static struct
{
unsigned char *buf;
unsigned long int size;
} jpegImage;
/****************************************/
/*** hardware dependant functions ***/
/****************************************/
/* please note that in-memory compression doesn't work satisfactory */
int convert2JPG()
{
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
int row_stride; /* physical row width in buffer */
JSAMPROW row_pointer[1]; /* pointer to a single row */
/* Initialize compression frame */
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
jpeg_mem_dest(&cinfo, &jpegImage.buf, &jpegImage.size);
cinfo.image_width = myFrame->xRes;
cinfo.image_height = myFrame->yRes;
cinfo.input_components = sizeof(RGB);
cinfo.in_color_space = JCS_RGB;
/* call some jpeg helpers */
jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo, 100, 1); /*set the quality [0..100] */
jpeg_start_compress(&cinfo, 1);
row_stride = cinfo.image_width;
/* Convert line by line */
while (cinfo.next_scanline < cinfo.image_height)
{
row_pointer[0] = (JSAMPROW) (image.buf + (cinfo.next_scanline * row_stride));
jpeg_write_scanlines(&cinfo, row_pointer, 1);
}
/* Finish compression and free internal memory */
jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo);
return 0;
}
// Find specific Samsung device
static void drv_SamsungSPF_find()
{
info("%s: Searching SPF.", Name);
/* open USB device */
struct usb_bus *bus;
struct usb_device *dev;
int i;
usb_init();
usb_find_busses();
usb_find_devices();
for (bus = usb_busses; bus; bus = bus->next)
{
for (dev = bus->devices; dev; dev = dev->next)
{
if (dev->descriptor.idVendor == myFrame->vendorID)
{
if (myFrame->productID.storageMode == 0xFFFF)
{
for (i = 0; i < numFrames; i++)
{
if (dev->descriptor.idProduct == spfDevices[i].productID.storageMode)
{
info("Samsung photoframe in Mass Storage mode found.");
myFrame = &spfDevices[i];
info("%s: Autodetect model %s.", Name, spfDevices[i].type);
myDev = dev;
return;
}
else if (dev->descriptor.idProduct == spfDevices[i].productID.monitorMode)
{
info("Samsung photoframe in Custom Product mode found.");
myFrame = &spfDevices[i];
info("%s: Autodetect model %s.", Name, spfDevices[i].type);
myDev = dev;
return;
}
}
}
else
{
if (dev->descriptor.idProduct == myFrame->productID.storageMode)
{
info("Samsung photoframe in Mass Storage mode found.");
myDev = dev;
return;
}
else if (dev->descriptor.idProduct == myFrame->productID.monitorMode)
{
info("Samsung photoframe in Custom Product mode found.");
myDev = dev;
return;
}
}
}
}
}
free(bus);
myDev = 0;
}
static int drv_SamsungSPF_open()
{
if (!myDev)
{
error("%s: No device specified!", Name);
return -1;
}
int res = -1;
char buf[256];
if (myDev->descriptor.idProduct == myFrame->productID.storageMode)
{
info("%s: Opening device and switching to monitor mode", Name);
myDevHandle = usb_open(myDev);
#if 0
setuid(getuid());
#endif
strcpy(buf, "** no string **");
res = usb_get_string_simple(myDevHandle, myDev->descriptor.iManufacturer, buf, sizeof(buf));
debug("usb_get_string_simple => %d, %s", res, buf);
memset(buf, 0, 256);
res = usb_control_msg(myDevHandle, USB_TYPE_STANDARD | USB_ENDPOINT_IN,
USB_REQ_GET_DESCRIPTOR, 0xfe, 0xfe, buf, 0xfe, 1000);
/* usb_close( myDev ); */
usb_close(myDevHandle);
// Sleep some time before research
sleep(1);
drv_SamsungSPF_find();
}
else
info("%s: No device in storage mode found", Name);
if (myDev->descriptor.idProduct == myFrame->productID.storageMode)
{
error("%s: Was not able to switch to monitor mode!", Name);
return -1;
}
if (myDev->descriptor.idProduct == myFrame->productID.monitorMode)
{
info("%s: Device '%s' is now in monitor mode.", Name, myFrame->type);
myDevHandle = usb_open(myDev);
return 0;
}
error("Unknown error: usb_control_msg() = %d", res);
return -1;
}
/* dummy function that sends something to the display */
static int drv_SamsungSPF_send(char *data, unsigned int len)
{
char usb_hdr[HEADERSIZE] = { 0xa5, 0x5a, 0x18, 0x04, 0xff, 0xff,
0xff, 0xff, 0x48, 0x00, 0x00, 0x00
};
#if 0
char buffer[1] = { 0x0 };
#endif
int usb_timeout = 1000;
int usb_endpoint = 0x2;
int ret;
*(int *) (usb_hdr + 4) = len;
debug("bytes_to_send: %d, offset: %d", len, HEADERSIZE);
#if 0
/* Send USB header */
if ((ret = usb_bulk_write(myDevHandle, usb_endpoint, usb_hdr, 12, usb_timeout)) < 0)
{
error("%s: Error occurred while writing data to device.", Name);
error("%s: usb_bulk_write returned: %d", Name, ret);
return -1;
}
/* Send JPEG image */
if ((ret = usb_bulk_write(myDevHandle, usb_endpoint, data, len, usb_timeout)) < 0)
{
error("%s: Error occurred while writing data to device.", Name);
error("%s: usb_bulk_write returned: %d", Name, ret);
return -1;
}
/* Finish transmission by sending zero */
if ((ret = usb_bulk_write(myDevHandle, usb_endpoint, buffer, 1, usb_timeout)) < 0)
{
error("%s: Error occurred while writing data to device.", Name);
error("%s: usb_bulk_write returned: %d", Name, ret);
return -1;
}
#endif
char *buffer;
unsigned int rest = FILLSIZE - ((len + HEADERSIZE) % FILLSIZE); /* +1; */
unsigned int full = HEADERSIZE + len + rest;
debug("calculated full bytes_to_send:: %d", full);
buffer = malloc(full + FILLSIZE); //added FILLSIZE is just for safety
if (buffer != NULL)
{
int i;
for (i = 0; i < HEADERSIZE; i++)
buffer[i] = usb_hdr[i];
memset(buffer + HEADERSIZE + len, 0x00, rest);
memcpy(buffer + HEADERSIZE, data, len);
if ((ret = usb_bulk_write(myDevHandle, usb_endpoint, buffer, full, usb_timeout)) < 0)
{
error("%s: Error occurred while writing data to device.", Name);
error("%s: usb_bulk_write returned: %d", Name, ret);
free(buffer);
return -1;
}
else
{
free(buffer);
}
}
else
{
error("%s: Not enough free memory.", Name);
return -1;
}
/* Keep SPF87h and friends in MiniMonitorMode */
char bytes[]= {0x09,0x04};
if ((ret = usb_control_msg(myDevHandle, 0xc0, 0x01, 0x0000, 0x0000, bytes, 0x02, 1000)) < 0)
{
error("%s: Error occurred while sending control_msg to device.", Name);
error("%s: usb_control_msg returned: %d", Name, ret);
};
return 0;
}
/* for graphic displays only */
static void drv_SamsungSPF_blit(const int row, const int col, const int height, const int width)
{
int r, c;
for (r = row; r < row + height; r++)
{
for (c = col; c < col + width; c++)
{
RGB p1 = image.buf[r * myFrame->xRes + c];
RGBA p2 = drv_generic_graphic_rgb(r, c);
if (p1.R != p2.R || p1.G != p2.G || p1.B != p2.B)
{
image.buf[r * myFrame->xRes + c].R = p2.R;
image.buf[r * myFrame->xRes + c].G = p2.G;
image.buf[r * myFrame->xRes + c].B = p2.B;
image.dirty = 1;
}
}
}
}
static void drv_SamsungSPF_timer( __attribute__ ((unused))
void *notused)
{
if (image.dirty)
{
debug("FB dirty, writing jpeg...");
convert2JPG();
/* Sent image to display */
if ((drv_SamsungSPF_send((char *) jpegImage.buf, jpegImage.size)) != 0)
{
error("%s: Error occurred while sending jpeg image to device.", Name);
}
/* Clean dirty bit */
image.dirty = 0;
/* Free JPEG buffer since a new is allocated each time an image is
compressed */
if (jpegImage.size)
free(jpegImage.buf);
jpegImage.size = 0;
}
}
/* start graphic display */
static int drv_SamsungSPF_start(const char *section)
{
int timerInterval = 1000;
char *s;
cfg_number(section, "update", timerInterval, 0, -1, &timerInterval);
debug("Updating display every %dms", timerInterval);
DROWS = myFrame->yRes;
DCOLS = myFrame->xRes;
info("%s: Using SPF with %dx%d pixels.", Name, DCOLS, DROWS);
s = cfg_get(section, "Font", "6x8");
if (s == NULL || *s == '\0')
{
error("%s: no '%s.Font' entry from %s", Name, section, cfg_source());
return -1;
}
XRES = -1;
YRES = -1;
if (sscanf(s, "%dx%d", &XRES, &YRES) != 2 || XRES < 1 || YRES < 1)
{
error("%s: bad Font '%s' from %s", Name, s, cfg_source());
return -1;
}
if (XRES < 6 || YRES < 8)
{
error("%s: bad Font '%s' from %s (must be at least 6x8)", Name, s, cfg_source());
return -1;
}
free(s);
/* Allocate framebuffer */
image.fbsize = myFrame->xRes * myFrame->yRes * sizeof(RGB);
image.buf = malloc(image.fbsize);
memset(image.buf, 128, image.fbsize);
image.dirty = 0;
/* JPEG buffer is allocated by jpeglib */
jpegImage.buf = 0;
jpegImage.size = 0;
/* regularly transmit the image */
timer_add(drv_SamsungSPF_timer, NULL, timerInterval, 0);
return 0;
}
/****************************************/
/*** exported functions ***/
/****************************************/
/* list models */
int drv_SamsungSPF_list(void)
{
int i;
printf("SamsungSPF driver, supported models [");
for (i = 0; i < numFrames; i++)
{
printf("%s", spfDevices[i].type);
if (i < numFrames - 1)
printf(", ");
}
printf("]\n");
return 0;
}
/* initialize driver & display */
/* use this function for a graphic display */
int drv_SamsungSPF_init(const char *section, const int quiet)
{
info("%s: Initializing SPF.", Name);
char *s;
int i;
myDev = 0;
myFrame = 0;
// Look for model entry in config
s = cfg_get(section, "Model", NULL);
if (s == NULL || *s != '\0')
{
s = cfg_get(section, "Model", NULL);
if (s == NULL || *s == '\0')
{
drv_SamsungSPF_list();
error("%s: no '%s.Model' entry from %s", Name, section, cfg_source());
return -1;
}
}
// Look for specified device
for (i = 0; i < numFrames; i++)
{
if (strcasecmp(s, spfDevices[i].type) == 0)
{
myFrame = &spfDevices[i];
info("%s: Configured for model %s.", Name, spfDevices[i].type);
break;
}
}
if (!myFrame)
{
drv_SamsungSPF_list();
error("%s: unknown model '%s'!", Name, s);
return -1;
}
free(s);
/* try to open USB device */
drv_SamsungSPF_find();
while (!myDev) //endless loop waitung for SPF
{
sleep(5);
drv_SamsungSPF_find();
}
if (!myDev)
{
error("%s: No Samsung '%s' found!", Name, myFrame->type);
return -1;
}
/* open display and switch to monitor mode if necessary */
if (drv_SamsungSPF_open() == -1)
return -1;
int ret;
/* real worker functions */
drv_generic_graphic_real_blit = drv_SamsungSPF_blit;
/* start display */
if ((ret = drv_SamsungSPF_start(section)) != 0)
return ret;
/* initialize generic graphic driver */
if ((ret = drv_generic_graphic_init(section, Name)) != 0)
return ret;
if (!quiet)
{
char buffer[40];
qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS);
if (drv_generic_graphic_greet(buffer, NULL))
{
sleep(3);
drv_generic_graphic_clear();
}
}
return 0;
}
/* close driver & display */
/* use this function for a graphic display */
int drv_SamsungSPF_quit(const int quiet)
{
info("%s: shutting down.", Name);
/* clear display */
drv_generic_graphic_clear();
/* say goodbye... */
if (!quiet)
{
drv_generic_graphic_greet("goodbye!", NULL);
}
drv_generic_graphic_quit();
debug("closing connection");
printf("%s: Closing driver...\n", Name);
usb_close(myDevHandle);
free(myDev);
free(myDevHandle);
return (0);
}
/* use this one for a graphic display */
DRIVER drv_SamsungSPF =
{
.name = Name,
.list = drv_SamsungSPF_list,
.init = drv_SamsungSPF_init,
.quit = drv_SamsungSPF_quit,
};