4
* driver for Attiny2313 USB LCD display interface
5
* see http://ast.m-faq.de/USB-LCD/USB-LCD.htm for schematics
7
* Copyright 2005 Till Harbaum <till@harbaum.org>
8
* Copyright 2005 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
10
* This file is part of LCD4Linux.
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)
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.
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.
32
* struct DRIVER drv_LCD2USB
46
#include <sys/ioctl.h>
57
#include "widget_text.h"
58
#include "widget_icon.h"
59
#include "widget_bar.h"
60
#include "widget_keypad.h"
62
#include "drv_generic_text.h"
65
#define LCD_USB_VENDOR 0x03eb
66
#define LCD_USB_DEVICE 0x7a53
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
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)
81
static char Name[] = "ASTUSB";
82
static char *device_id = NULL, *bus_id = NULL;
84
static usb_dev_handle *lcd;
85
static int controllers = 0;
87
extern int got_signal;
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
100
/****************************************/
101
/*** hardware dependant functions ***/
102
/****************************************/
104
static int drv_AST_HW_open(char *bus_id, char *device_id)
106
struct usb_bus *busses, *bus;
107
struct usb_device *dev;
111
info("%s: scanning USB for ASTUSB interface ...", Name);
114
info("%s: scanning for bus id: %s", Name, bus_id);
116
if (device_id != NULL)
117
info("%s: scanning for device id: %s", Name, device_id);
124
busses = usb_get_busses();
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))) {
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))) {
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);
137
if (usb_claim_interface(lcd, 0) < 0) {
138
error("%s: usb_claim_interface() failed!", Name);
150
static int drv_AST_HW_close(void)
152
usb_release_interface(lcd, 0);
158
static int drv_AST_HW_data(unsigned char *buffer, short length, int __attribute__ ((unused)) index)
160
int request = CMD_LCD_DATA;
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);
167
usb_release_interface(lcd, 0);
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);
176
/* and try to re-send command */
178
(lcd, USB_TYPE_VENDOR | USB_RECIP_DEVICE, request, value, 0 /*index */ , (char *) buffer, length,
180
error("%s: retried USB request failed, aborting!", Name);
185
info("%s: Device successfully reconnected.", Name);
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 */
198
static int drv_AST_HW_cmd(unsigned char cmd, short value, int __attribute__ ((unused)) index)
200
unsigned char buffer[2];
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);
209
error("%s: USB request failed!", Name);
213
return buffer[0] + 256 * buffer[1];
216
int drv_AST_HW_echo(void)
222
for (i = 0; i < ECHO_NUM; i++) {
223
val = rand() & 0xff; //8 bit number
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);
232
if (val != buffer[1])
237
error("%s: ERROR, %d out of %d echo transfers failed!", Name, errors, ECHO_NUM);
240
info("%s: echo test successful", Name);
251
* TT = target bit map
252
* R = reserved for future use, set to 0
253
* LL = number of bytes in transfer - 1
256
short drv_AST_LCD_BL(int backlight)
261
else if (backlight > 1)
264
drv_AST_HW_cmd(CMD_LCD_BLIGHT, backlight, 0);
270
static void drv_AST_clear(void)
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 */
276
static void drv_AST_write(const int row, const int col, const char *data, int len)
278
int pos, ctrl = LCD_CTRL_0;
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 */
289
/* 16x4 Displays use a slightly different layout */
290
if (DCOLS == 16 && DROWS == 4) {
291
pos = (tmpRow % 2) * 64 + (tmpRow / 2) * 16 + col;
293
pos = (tmpRow % 2) * 64 + (tmpRow / 2) * 20 + col;
296
drv_AST_HW_cmd(CMD_LCD_COMMAND, 0x80 | pos, (ctrl & controllers));
297
drv_AST_HW_data((unsigned char *) data, len, (ctrl & controllers));
301
static void drv_AST_defchar(const int ascii, const unsigned char *matrix)
304
unsigned char buffer[8];
307
for (i = 0; i < 8; i++) {
308
buffer[i] = *matrix++ & 0x1f;
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);
316
static int drv_AST_start(const char *section, const int quiet)
318
// int contrast, brightness;
320
int rows = -1, cols = -1;
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());
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());
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);
343
if (drv_AST_HW_open(bus_id, device_id) < 0) {
344
error("%s: could not find a ASTUSB USB LCD", Name);
348
/* test interface reliability */
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);
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);
362
drv_AST_clear(); /* clear display */
366
qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS);
367
if (drv_generic_text_greet(buffer, "AST USB-LCD")) {
377
/****************************************/
379
/****************************************/
381
static void plugin_backlight(RESULT * result, RESULT * arg1)
385
backlight = drv_AST_LCD_BL(R2N(arg1));
386
SetResult(&result, R_NUMBER, &backlight);
389
/****************************************/
390
/*** widget callbacks ***/
391
/****************************************/
394
/* using drv_generic_text_draw(W) */
395
/* using drv_generic_text_icon_draw(W) */
396
/* using drv_generic_text_bar_draw(W) */
399
/****************************************/
400
/*** exported functions ***/
401
/****************************************/
405
int drv_AST_list(void)
407
printf("ASTUSB display interface");
412
/* initialize driver & display */
413
int drv_AST_init(const char *section, const int quiet)
419
info("%s: %s", Name, "$Rev: 975 $");
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 */
428
/* real worker functions */
429
drv_generic_text_real_write = drv_AST_write;
430
drv_generic_text_real_defchar = drv_AST_defchar;
433
if ((ret = drv_AST_start(section, quiet)) != 0)
436
/* initialize generic text driver */
437
if ((ret = drv_generic_text_init(section, Name)) != 0)
440
/* initialize generic icon driver */
441
if ((ret = drv_generic_text_icon_init()) != 0)
444
/* initialize generic bar driver */
445
if ((ret = drv_generic_text_bar_init(0)) != 0)
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 */
455
drv_generic_text_bar_add_segment(255, 255, 255, 255); /* ASCII 255 = block */
458
/* register text widget */
460
wc.draw = drv_generic_text_draw;
461
widget_register(&wc);
463
/* register icon widget */
465
wc.draw = drv_generic_text_icon_draw;
466
widget_register(&wc);
468
/* register bar widget */
470
wc.draw = drv_generic_text_bar_draw;
471
widget_register(&wc);
473
/* register plugins */
474
// AddFunction("LCD::contrast", 1, plugin_contrast);
475
// AddFunction("LCD::brightness", 1, plugin_brightness);
476
AddFunction("LCD::backlight", 1, plugin_backlight);
482
/* close driver & display */
483
int drv_AST_quit(const int quiet)
486
info("%s: shutting down.", Name);
488
drv_generic_text_quit();
495
drv_generic_text_greet("That's all, folks!", NULL);
500
debug("closing USB connection");
507
DRIVER drv_ASTUSB = {
509
.list = drv_AST_list,
510
.init = drv_AST_init,
511
.quit = drv_AST_quit,