~siretart/lcd4linux/debian

« back to all changes in this revision

Viewing changes to drv_ASTUSB.c

  • Committer: Reinhard Tartler
  • Date: 2011-04-27 17:24:15 UTC
  • mto: This revision was merged to the branch mainline in revision 750.
  • Revision ID: siretart@tauware.de-20110427172415-6n4aptmvmz0eztvm
Tags: upstream-0.11.0~svn1143
ImportĀ upstreamĀ versionĀ 0.11.0~svn1143

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id:  $
 
2
 * $URL: $
 
3
 *
 
4
 * driver for Attiny2313 USB LCD display interface
 
5
 * see http://ast.m-faq.de/USB-LCD/USB-LCD.htm for schematics
 
6
 *
 
7
 * Copyright 2005 Till Harbaum <till@harbaum.org>
 
8
 * Copyright 2005 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
 
9
 *
 
10
 * This file is part of LCD4Linux.
 
11
 *
 
12
 * LCD4Linux is free software; you can redistribute it and/or modify
 
13
 * it under the terms of the GNU General Public License as published by
 
14
 * the Free Software Foundation; either version 2, or (at your option)
 
15
 * any later version.
 
16
 *
 
17
 * LCD4Linux is distributed in the hope that it will be useful,
 
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
20
 * GNU General Public License for more details.
 
21
 *
 
22
 * You should have received a copy of the GNU General Public License
 
23
 * along with this program; if not, write to the Free Software
 
24
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
25
 *
 
26
 */
 
27
 
 
28
/* 
 
29
 *
 
30
 * exported fuctions:
 
31
 *
 
32
 * struct DRIVER drv_LCD2USB
 
33
 *
 
34
 */
 
35
 
 
36
#include "config.h"
 
37
 
 
38
#include <stdlib.h>
 
39
#include <stdio.h>
 
40
#include <string.h>
 
41
#include <errno.h>
 
42
#include <unistd.h>
 
43
#include <termios.h>
 
44
#include <fcntl.h>
 
45
#include <ctype.h>
 
46
#include <sys/ioctl.h>
 
47
#include <sys/time.h>
 
48
 
 
49
#include <usb.h>
 
50
 
 
51
#include "debug.h"
 
52
#include "cfg.h"
 
53
#include "timer.h"
 
54
#include "qprintf.h"
 
55
#include "plugin.h"
 
56
#include "widget.h"
 
57
#include "widget_text.h"
 
58
#include "widget_icon.h"
 
59
#include "widget_bar.h"
 
60
#include "widget_keypad.h"
 
61
#include "drv.h"
 
62
#include "drv_generic_text.h"
 
63
 
 
64
 
 
65
#define LCD_USB_VENDOR    0x03eb
 
66
#define LCD_USB_DEVICE   0x7a53
 
67
 
 
68
#define CMD_LCD_ECHO           (0<<5)
 
69
#define CMD_LCD_INIT            1
 
70
#define CMD_LCD_COMMAND         2
 
71
#define CMD_LCD_DATA            3
 
72
#define CMD_LCD_BLIGHT          4
 
73
 
 
74
 
 
75
/* target is a bit map for CMD/DATA */
 
76
#define LCD_CTRL_0              (1<<0)
 
77
#define LCD_CTRL_1              (1<<1)
 
78
#define LCD_BOTH           (LCD_CTRL_0 | LCD_CTRL_1)
 
79
 
 
80
 
 
81
static char Name[] = "ASTUSB";
 
82
static char *device_id = NULL, *bus_id = NULL;
 
83
 
 
84
static usb_dev_handle *lcd;
 
85
static int controllers = 0;
 
86
 
 
87
extern int got_signal;
 
88
 
 
89
enum {
 
90
    LCD_INIT_INCREASE = 1 << 0,
 
91
    LCD_INIT_SHIFT = 1 << 1,
 
92
    LCD_INIT_CON = 1 << 2,
 
93
    LCD_INIT_BON = 1 << 3,
 
94
    LCD_INIT_DSHIFT = 1 << 4,
 
95
    LCD_INIT_RSHIFT = 1 << 5,
 
96
    LCD_INIT_2LINES = 1 << 6,
 
97
    LCD_INIT_FONT = 1 << 7
 
98
};
 
99
 
 
100
/****************************************/
 
101
/***  hardware dependant functions    ***/
 
102
/****************************************/
 
103
 
 
104
static int drv_AST_HW_open(char *bus_id, char *device_id)
 
105
{
 
106
    struct usb_bus *busses, *bus;
 
107
    struct usb_device *dev;
 
108
 
 
109
    lcd = NULL;
 
110
 
 
111
    info("%s: scanning USB for ASTUSB interface ...", Name);
 
112
 
 
113
    if (bus_id != NULL)
 
114
        info("%s: scanning for bus id: %s", Name, bus_id);
 
115
 
 
116
    if (device_id != NULL)
 
117
        info("%s: scanning for device id: %s", Name, device_id);
 
118
 
 
119
    usb_set_debug(0);
 
120
 
 
121
    usb_init();
 
122
    usb_find_busses();
 
123
    usb_find_devices();
 
124
    busses = usb_get_busses();
 
125
 
 
126
    for (bus = busses; bus; bus = bus->next) {
 
127
        /* search this bus if no bus id was given or if this is the given bus id */
 
128
        if (!bus_id || (bus_id && !strcasecmp(bus->dirname, bus_id))) {
 
129
 
 
130
            for (dev = bus->devices; dev; dev = dev->next) {
 
131
                /* search this device if no device id was given or if this is the given device id */
 
132
                if (!device_id || (device_id && !strcasecmp(dev->filename, device_id))) {
 
133
 
 
134
                    if ((dev->descriptor.idVendor == LCD_USB_VENDOR) && (dev->descriptor.idProduct == LCD_USB_DEVICE)) {
 
135
                        info("%s: found ASTUSB interface on bus %s device %s", Name, bus->dirname, dev->filename);
 
136
                        lcd = usb_open(dev);
 
137
                        if (usb_claim_interface(lcd, 0) < 0) {
 
138
                            error("%s: usb_claim_interface() failed!", Name);
 
139
                            return -1;
 
140
                        }
 
141
                        return 0;
 
142
                    }
 
143
                }
 
144
            }
 
145
        }
 
146
    }
 
147
    return -1;
 
148
}
 
149
 
 
150
static int drv_AST_HW_close(void)
 
151
{
 
152
    usb_release_interface(lcd, 0);
 
153
    usb_close(lcd);
 
154
 
 
155
    return 0;
 
156
}
 
157
 
 
158
static int drv_AST_HW_data(unsigned char *buffer, short length, int __attribute__ ((unused)) index)
 
159
{
 
160
    int request = CMD_LCD_DATA;
 
161
    int value = 0;
 
162
 
 
163
    if (usb_control_msg
 
164
        (lcd, USB_TYPE_VENDOR | USB_RECIP_DEVICE, request, value, 0 /*index */ , (char *) buffer, length, 1000) < 0) {
 
165
        error("%s: USB request failed! Trying to reconnect device.", Name);
 
166
 
 
167
        usb_release_interface(lcd, 0);
 
168
        usb_close(lcd);
 
169
 
 
170
        /* try to close and reopen connection */
 
171
        if (drv_AST_HW_open(bus_id, device_id) < 0) {
 
172
            error("%s: could not re-detect ASTUSB USB LCD", Name);
 
173
            got_signal = -1;
 
174
            return -1;
 
175
        }
 
176
        /* and try to re-send command */
 
177
        if (usb_control_msg
 
178
            (lcd, USB_TYPE_VENDOR | USB_RECIP_DEVICE, request, value, 0 /*index */ , (char *) buffer, length,
 
179
             1000) < 0) {
 
180
            error("%s: retried USB request failed, aborting!", Name);
 
181
            got_signal = -1;
 
182
            return -1;
 
183
        }
 
184
 
 
185
        info("%s: Device successfully reconnected.", Name);
 
186
    }
 
187
 
 
188
    return 0;
 
189
}
 
190
 
 
191
/* send a number of 16 bit words to the lcd2usb interface */
 
192
/* and verify that they are correctly returned by the echo */
 
193
/* command. This may be used to check the reliability of */
 
194
/* the usb interfacing */
 
195
#define ECHO_NUM 10
 
196
 
 
197
 
 
198
static int drv_AST_HW_cmd(unsigned char cmd, short value, int __attribute__ ((unused)) index)
 
199
{
 
200
    unsigned char buffer[2];
 
201
    int nBytes;
 
202
 
 
203
    /* send control request and accept return value */
 
204
    nBytes = usb_control_msg(lcd,
 
205
                             USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
 
206
                             cmd, value, 0 /*index */ , (char *) buffer, sizeof(buffer), 1000);
 
207
 
 
208
    if (nBytes < 0) {
 
209
        error("%s: USB request failed!", Name);
 
210
        return -1;
 
211
    }
 
212
 
 
213
    return buffer[0] + 256 * buffer[1];
 
214
}
 
215
 
 
216
int drv_AST_HW_echo(void)
 
217
{
 
218
    int i, errors = 0;
 
219
    char val;
 
220
    char buffer[2];
 
221
 
 
222
    for (i = 0; i < ECHO_NUM; i++) {
 
223
        val = rand() & 0xff;    //8 bit number
 
224
 
 
225
        if (usb_control_msg(lcd,
 
226
                            USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
 
227
                            CMD_LCD_ECHO, val, 0, buffer, sizeof(buffer), 1000) < 0) {
 
228
            error("%s: USB request failed!", Name);
 
229
            return -1;
 
230
        }
 
231
 
 
232
        if (val != buffer[1])
 
233
            errors++;
 
234
    }
 
235
 
 
236
    if (errors) {
 
237
        error("%s: ERROR, %d out of %d echo transfers failed!", Name, errors, ECHO_NUM);
 
238
        return -1;
 
239
    } else
 
240
        info("%s: echo test successful", Name);
 
241
 
 
242
 
 
243
    return 0;
 
244
}
 
245
 
 
246
 
 
247
/* command format: 
 
248
 * 7 6 5 4 3 2 1 0
 
249
 * C C C T T R L L
 
250
 *
 
251
 * TT = target bit map 
 
252
 * R = reserved for future use, set to 0
 
253
 * LL = number of bytes in transfer - 1 
 
254
 */
 
255
 
 
256
short drv_AST_LCD_BL(int backlight)
 
257
{
 
258
 
 
259
    if (backlight < 0)
 
260
        backlight = 0;
 
261
    else if (backlight > 1)
 
262
        backlight = 1;
 
263
 
 
264
    drv_AST_HW_cmd(CMD_LCD_BLIGHT, backlight, 0);
 
265
 
 
266
    return backlight;
 
267
}
 
268
 
 
269
 
 
270
static void drv_AST_clear(void)
 
271
{
 
272
    drv_AST_HW_cmd(CMD_LCD_COMMAND, 0x01, LCD_BOTH & controllers);      /* clear display */
 
273
    drv_AST_HW_cmd(CMD_LCD_COMMAND, 0x03, LCD_BOTH & controllers);      /* return home */
 
274
}
 
275
 
 
276
static void drv_AST_write(const int row, const int col, const char *data, int len)
 
277
{
 
278
    int pos, ctrl = LCD_CTRL_0;
 
279
    int tmpRow = row;
 
280
 
 
281
    /* displays with more two rows and 20 columns have a logical width */
 
282
    /* of 40 chars and use more than one controller */
 
283
    if ((DROWS > 2) && (DCOLS > 20) && (row > 1)) {
 
284
        /* use second controller */
 
285
        tmpRow -= 2;
 
286
        ctrl = LCD_CTRL_1;
 
287
    }
 
288
 
 
289
    /* 16x4 Displays use a slightly different layout */
 
290
    if (DCOLS == 16 && DROWS == 4) {
 
291
        pos = (tmpRow % 2) * 64 + (tmpRow / 2) * 16 + col;
 
292
    } else {
 
293
        pos = (tmpRow % 2) * 64 + (tmpRow / 2) * 20 + col;
 
294
    }
 
295
 
 
296
    drv_AST_HW_cmd(CMD_LCD_COMMAND, 0x80 | pos, (ctrl & controllers));
 
297
    drv_AST_HW_data((unsigned char *) data, len, (ctrl & controllers));
 
298
 
 
299
}
 
300
 
 
301
static void drv_AST_defchar(const int ascii, const unsigned char *matrix)
 
302
{
 
303
    int i;
 
304
    unsigned char buffer[8];
 
305
 
 
306
    //build buffer
 
307
    for (i = 0; i < 8; i++) {
 
308
        buffer[i] = *matrix++ & 0x1f;
 
309
    }
 
310
 
 
311
    //send to HW
 
312
    drv_AST_HW_cmd(CMD_LCD_COMMAND, 0x40 | 8 * ascii, LCD_BOTH & controllers);
 
313
    drv_AST_HW_data((unsigned char *) buffer, sizeof(buffer), LCD_BOTH & controllers);
 
314
}
 
315
 
 
316
static int drv_AST_start(const char *section, const int quiet)
 
317
{
 
318
//    int contrast, brightness;
 
319
    int backlight;
 
320
    int rows = -1, cols = -1;
 
321
    char *s;
 
322
 
 
323
    s = cfg_get(section, "Size", NULL);
 
324
    if (s == NULL || *s == '\0') {
 
325
        error("%s: no '%s.Size' entry from %s", Name, section, cfg_source());
 
326
        return -1;
 
327
    }
 
328
    if (sscanf(s, "%dx%d", &cols, &rows) != 2 || rows < 1 || cols < 1) {
 
329
        error("%s: bad %s.Size '%s' from %s", Name, section, s, cfg_source());
 
330
        free(s);
 
331
        return -1;
 
332
    }
 
333
 
 
334
    DROWS = rows;
 
335
    DCOLS = cols;
 
336
 
 
337
    /* bus id and device id are strings and not just intergers, since */
 
338
    /* the windows port of libusb treats them as strings. And this way */
 
339
    /* we keep windows compatibility ... just in case ... */
 
340
    bus_id = cfg_get(section, "Bus", NULL);
 
341
    device_id = cfg_get(section, "Device", NULL);
 
342
 
 
343
    if (drv_AST_HW_open(bus_id, device_id) < 0) {
 
344
        error("%s: could not find a ASTUSB USB LCD", Name);
 
345
        return -1;
 
346
    }
 
347
 
 
348
    /* test interface reliability */
 
349
    drv_AST_HW_echo();
 
350
 
 
351
    /*low level init of init LCD */
 
352
    if (drv_AST_HW_cmd(CMD_LCD_INIT, LCD_INIT_INCREASE | (DROWS > 1 ? LCD_INIT_2LINES : 1), LCD_BOTH & controllers) < 0) {
 
353
        error("%s: could not init LCD", Name);
 
354
        return -1;
 
355
    }
 
356
 
 
357
    if (cfg_number(section, "Backlight", 0, 0, 1, &backlight) > 0) {
 
358
        info("%s: backlight %s", Name, backlight ? "enabled" : "disabled");
 
359
        drv_AST_LCD_BL(backlight);
 
360
    }
 
361
 
 
362
    drv_AST_clear();            /* clear display */
 
363
 
 
364
    if (!quiet) {
 
365
        char buffer[40];
 
366
        qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS);
 
367
        if (drv_generic_text_greet(buffer, "AST USB-LCD")) {
 
368
            sleep(3);
 
369
            drv_AST_clear();
 
370
        }
 
371
    }
 
372
 
 
373
    return 0;
 
374
}
 
375
 
 
376
 
 
377
/****************************************/
 
378
/***            plugins               ***/
 
379
/****************************************/
 
380
 
 
381
static void plugin_backlight(RESULT * result, RESULT * arg1)
 
382
{
 
383
    double backlight;
 
384
 
 
385
    backlight = drv_AST_LCD_BL(R2N(arg1));
 
386
    SetResult(&result, R_NUMBER, &backlight);
 
387
}
 
388
 
 
389
/****************************************/
 
390
/***        widget callbacks          ***/
 
391
/****************************************/
 
392
 
 
393
 
 
394
/* using drv_generic_text_draw(W) */
 
395
/* using drv_generic_text_icon_draw(W) */
 
396
/* using drv_generic_text_bar_draw(W) */
 
397
 
 
398
 
 
399
/****************************************/
 
400
/***        exported functions        ***/
 
401
/****************************************/
 
402
 
 
403
 
 
404
/* list models */
 
405
int drv_AST_list(void)
 
406
{
 
407
    printf("ASTUSB display interface");
 
408
    return 0;
 
409
}
 
410
 
 
411
 
 
412
/* initialize driver & display */
 
413
int drv_AST_init(const char *section, const int quiet)
 
414
{
 
415
    WIDGET_CLASS wc;
 
416
    int asc255bug;
 
417
    int ret;
 
418
 
 
419
    info("%s: %s", Name, "$Rev: 975 $");
 
420
 
 
421
    /* display preferences */
 
422
    XRES = 5;                   /* pixel width of one char  */
 
423
    YRES = 8;                   /* pixel height of one char  */
 
424
    CHARS = 8;                  /* number of user-defineable characters */
 
425
    CHAR0 = 0;                  /* ASCII of first user-defineable char */
 
426
    GOTO_COST = 2;              /* number of bytes a goto command requires */
 
427
 
 
428
    /* real worker functions */
 
429
    drv_generic_text_real_write = drv_AST_write;
 
430
    drv_generic_text_real_defchar = drv_AST_defchar;
 
431
 
 
432
    /* start display */
 
433
    if ((ret = drv_AST_start(section, quiet)) != 0)
 
434
        return ret;
 
435
 
 
436
    /* initialize generic text driver */
 
437
    if ((ret = drv_generic_text_init(section, Name)) != 0)
 
438
        return ret;
 
439
 
 
440
    /* initialize generic icon driver */
 
441
    if ((ret = drv_generic_text_icon_init()) != 0)
 
442
        return ret;
 
443
 
 
444
    /* initialize generic bar driver */
 
445
    if ((ret = drv_generic_text_bar_init(0)) != 0)
 
446
        return ret;
 
447
 
 
448
    /* add fixed chars to the bar driver */
 
449
    /* most displays have a full block on ascii 255, but some have kind of  */
 
450
    /* an 'inverted P'. If you specify 'asc255bug 1 in the config, this */
 
451
    /* char will not be used, but rendered by the bar driver */
 
452
    cfg_number(section, "asc255bug", 0, 0, 1, &asc255bug);
 
453
    drv_generic_text_bar_add_segment(0, 0, 255, 32);    /* ASCII  32 = blank */
 
454
    if (!asc255bug)
 
455
        drv_generic_text_bar_add_segment(255, 255, 255, 255);   /* ASCII 255 = block */
 
456
 
 
457
 
 
458
    /* register text widget */
 
459
    wc = Widget_Text;
 
460
    wc.draw = drv_generic_text_draw;
 
461
    widget_register(&wc);
 
462
 
 
463
    /* register icon widget */
 
464
    wc = Widget_Icon;
 
465
    wc.draw = drv_generic_text_icon_draw;
 
466
    widget_register(&wc);
 
467
 
 
468
    /* register bar widget */
 
469
    wc = Widget_Bar;
 
470
    wc.draw = drv_generic_text_bar_draw;
 
471
    widget_register(&wc);
 
472
 
 
473
    /* register plugins */
 
474
    // AddFunction("LCD::contrast", 1, plugin_contrast);
 
475
    // AddFunction("LCD::brightness", 1, plugin_brightness);
 
476
    AddFunction("LCD::backlight", 1, plugin_backlight);
 
477
 
 
478
    return 0;
 
479
}
 
480
 
 
481
 
 
482
/* close driver & display */
 
483
int drv_AST_quit(const int quiet)
 
484
{
 
485
 
 
486
    info("%s: shutting down.", Name);
 
487
 
 
488
    drv_generic_text_quit();
 
489
 
 
490
    /* clear display */
 
491
    drv_AST_clear();
 
492
 
 
493
    /* say goodbye... */
 
494
    if (!quiet) {
 
495
        drv_generic_text_greet("That's all, folks!", NULL);
 
496
    }
 
497
 
 
498
    drv_AST_LCD_BL(0);
 
499
 
 
500
    debug("closing USB connection");
 
501
    drv_AST_HW_close();
 
502
 
 
503
    return (0);
 
504
}
 
505
 
 
506
 
 
507
DRIVER drv_ASTUSB = {
 
508
    .name = Name,
 
509
    .list = drv_AST_list,
 
510
    .init = drv_AST_init,
 
511
    .quit = drv_AST_quit,
 
512
};