-
Notifications
You must be signed in to change notification settings - Fork 33
/
drv_SamsungSPF.c
511 lines (405 loc) · 12.2 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
/* $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 <sascha@plazar.de>
* Copyright (C) 2005, 2006, 2007 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
*
* 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"
// 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-75H",
.vendorID = 0x04e8,
.productID = {0x200e, 0x200f},
.xRes = 800,
.yRes = 480,
},
{
.type = "SPF-85H",
.vendorID = 0x04e8,
.productID = {0x2012, 0x2013},
.xRes = 800,
.yRes = 600,
},
{
.type = "SPF-107H",
.vendorID = 0x04e8,
.productID = {0x2027, 0x2028},
.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;
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 (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);
setuid(getuid());
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 ); */
// 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[12] = { 0xa5, 0x5a, 0x18, 0x04, 0xff, 0xff,
0xff, 0xff, 0x48, 0x00, 0x00, 0x00
};
char buffer[1] = { 0x0 };
int usb_timeout = 1000;
int usb_endpoint = 0x2;
int ret;
*(int *) (usb_hdr + 4) = len;
debug("bytes_to_send: %d, offset: %d", len, 12);
/* 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;
}
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();
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,
};