root/trunk/drv_USBLCD.c

Revision 899, 11.2 kB (checked in by bwalle, 6 weeks ago)

Replace write to external variable usb_debug by calling usb_set_debug().
This fixes build on openSUSE Factory.

  • Property svn:keywords set to Id URL Rev
Line 
1/* $Id$
2 * $URL$
3 *
4 * new style driver for USBLCD displays
5 *
6 * Copyright (C) 2003 Michael Reinelt <michael@reinelt.co.at>
7 * Copyright (C) 2004 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
8 *
9 * based on the old-style USBLCD driver which is
10 * Copyright (C) 2002 Robin Adams, Adams IT Services <info@usblcd.de>
11 *
12 * This file is part of LCD4Linux.
13 *
14 * LCD4Linux is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2, or (at your option)
17 * any later version.
18 *
19 * LCD4Linux is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 *
28 */
29
30/*
31 *
32 * exported fuctions:
33 *
34 * struct DRIVER drv_USBLCD
35 *
36 */
37
38#include "config.h"
39
40#include <stdlib.h>
41#include <stdio.h>
42#include <string.h>
43#include <errno.h>
44#include <unistd.h>
45#include <termios.h>
46#include <fcntl.h>
47#include <sys/ioctl.h>
48#include <sys/time.h>
49
50#ifdef HAVE_USB_H
51#include <usb.h>
52#endif
53
54#include "debug.h"
55#include "cfg.h"
56#include "qprintf.h"
57#include "udelay.h"
58#include "plugin.h"
59#include "widget.h"
60#include "widget_text.h"
61#include "widget_icon.h"
62#include "widget_bar.h"
63#include "drv.h"
64#include "drv_generic_text.h"
65
66
67#define USBLCD_VENDOR  0x10D2
68#define USBLCD_VENDOR2 0x1212
69#define USBLCD_DEVICE  0x0001
70
71#define IOC_GET_HARD_VERSION 1
72#define IOC_GET_DRV_VERSION  2
73
74
75static char Name[] = "USBLCD";
76
77static char *Port = NULL;
78static int use_libusb = 0;
79static int usblcd_file;
80static char *Buffer;
81static char *BufPtr;
82
83
84#ifdef HAVE_USB_H
85
86static usb_dev_handle *lcd;
87static int interface;
88
89#endif
90
91
92
93/****************************************/
94/***  hardware dependant functions    ***/
95/****************************************/
96
97#ifdef HAVE_USB_H
98
99static int drv_UL_open(void)
100{
101    struct usb_bus *busses, *bus;
102    struct usb_device *dev;
103
104    lcd = NULL;
105
106    info("%s: scanning for USBLCD...", Name);
107
108    usb_set_debug(0);
109
110    usb_init();
111    usb_find_busses();
112    usb_find_devices();
113    busses = usb_get_busses();
114
115    for (bus = busses; bus; bus = bus->next) {
116  for (dev = bus->devices; dev; dev = dev->next) {
117      if (((dev->descriptor.idVendor == USBLCD_VENDOR) ||
118     (dev->descriptor.idVendor == USBLCD_VENDOR2)) && (dev->descriptor.idProduct == USBLCD_DEVICE)) {
119
120    unsigned int v = dev->descriptor.bcdDevice;
121
122    info("%s: found USBLCD V%1d%1d.%1d%1d on bus %s device %s", Name,
123         (v & 0xF000) >> 12, (v & 0xF00) >> 8, (v & 0xF0) >> 4, (v & 0xF), bus->dirname, dev->filename);
124
125    interface = 0;
126    lcd = usb_open(dev);
127    if (usb_claim_interface(lcd, interface) < 0) {
128        error("%s: usb_claim_interface() failed!", Name);
129        error("%s: maybe you have the usblcd module loaded?", Name);
130        return -1;
131    }
132    return 0;
133      }
134  }
135    }
136    error("%s: could not find a USBLCD", Name);
137    return -1;
138}
139
140
141static int drv_UL_close(void)
142{
143    usb_release_interface(lcd, interface);
144    usb_close(lcd);
145
146    return 0;
147}
148
149#endif
150
151
152static void drv_UL_send(void)
153{
154
155#if 0
156    struct timeval now, end;
157    gettimeofday(&now, NULL);
158#endif
159
160    if (use_libusb) {
161#ifdef HAVE_USB_H
162  /* Fixme: Endpoint hardcoded to 1 ??? */
163  usb_bulk_write(lcd, 1, Buffer, BufPtr - Buffer, 1000);
164#endif
165    } else {
166  write(usblcd_file, Buffer, BufPtr - Buffer);
167    }
168
169
170#if 0
171    gettimeofday(&end, NULL);
172    debug("send %d bytes in %d usec (%d usec/byte)", BufPtr - Buffer,
173    (1000000 * (end.tv_sec - now.tv_sec) + end.tv_usec - now.tv_usec),
174    (1000000 * (end.tv_sec - now.tv_sec) + end.tv_usec - now.tv_usec) / (BufPtr - Buffer));
175#endif
176
177    BufPtr = Buffer;
178}
179
180
181static void drv_UL_command(const unsigned char cmd)
182{
183    *BufPtr++ = '\0';
184    *BufPtr++ = cmd;
185}
186
187
188static void drv_UL_clear(void)
189{
190    drv_UL_command(0x01); /* clear display */
191    drv_UL_command(0x03); /* return home */
192    drv_UL_send();    /* flush buffer */
193}
194
195
196static void drv_UL_write(const int row, const int col, const char *data, int len)
197{
198    int pos;
199
200    /* 16x4 Displays use a slightly different layout */
201    if (DCOLS == 16 && DROWS == 4) {
202  pos = (row % 2) * 64 + (row / 2) * 16 + col;
203    } else {
204  pos = (row % 2) * 64 + (row / 2) * 20 + col;
205    }
206
207    drv_UL_command(0x80 | pos);
208
209    while (len--) {
210  if (*data == 0)
211      *BufPtr++ = 0;
212  *BufPtr++ = *data++;
213    }
214
215    drv_UL_send();
216}
217
218static void drv_UL_defchar(const int ascii, const unsigned char *matrix)
219{
220    int i;
221
222    drv_UL_command(0x40 | 8 * ascii);
223
224    for (i = 0; i < 8; i++) {
225  if ((*matrix & 0x1f) == 0)
226      *BufPtr++ = 0;
227  *BufPtr++ = *matrix++ & 0x1f;
228    }
229
230    drv_UL_send();
231}
232
233
234static int drv_UL_start(const char *section, const int quiet)
235{
236    int rows = -1, cols = -1;
237    int major, minor;
238    char *port, *s;
239    char buf[128];
240
241    if (Port) {
242  free(Port);
243  Port = NULL;
244    }
245
246    if ((port = cfg_get(section, "Port", NULL)) == NULL || *port == '\0') {
247  error("%s: no '%s.Port' entry from %s", Name, section, cfg_source());
248  return -1;
249    }
250
251    if (strcasecmp(port, "libusb") == 0) {
252#ifdef HAVE_USB_H
253  use_libusb = 1;
254  debug("using libusb");
255#else
256  error("%s: cannot use 'libusb' port.", Name);
257  error("%s: lcd4linux was compiled without libusb support!", Name);
258  return -1;
259#endif
260    } else {
261  if (port[0] == '/') {
262      Port = strdup(port);
263  } else {
264      int len = 5 + strlen(port) + 1;
265      Port = malloc(len);
266      qprintf(Port, len, "/dev/%s", port);
267  }
268  debug("using device %s ", Port);
269    }
270
271    s = cfg_get(section, "Size", NULL);
272    if (s == NULL || *s == '\0') {
273  error("%s: no '%s.Size' entry from %s", Name, section, cfg_source());
274  return -1;
275    }
276    if (sscanf(s, "%dx%d", &cols, &rows) != 2 || rows < 1 || cols < 1) {
277  error("%s: bad %s.Size '%s' from %s", Name, section, s, cfg_source());
278  free(s);
279  return -1;
280    }
281
282    DROWS = rows;
283    DCOLS = cols;
284
285    if (use_libusb) {
286
287#ifdef HAVE_USB_H
288  if (drv_UL_open() < 0) {
289      return -1;
290  }
291#endif
292
293    } else {
294
295  /* open port */
296  usblcd_file = open(Port, O_WRONLY);
297  if (usblcd_file == -1) {
298      error("%s: open(%s) failed: %s", Name, Port, strerror(errno));
299      return -1;
300  }
301
302  /* get driver version */
303  memset(buf, 0, sizeof(buf));
304  if (ioctl(usblcd_file, IOC_GET_DRV_VERSION, buf) != 0) {
305      error("%s: ioctl() failed, could not get Driver Version!", Name);
306      return -1;
307  }
308  info("%s: Driver Version: %s", Name, buf);
309
310  if (sscanf(buf, "USBLCD Driver Version %d.%d", &major, &minor) != 2) {
311      error("%s: could not read Driver Version!", Name);
312      return -1;
313  }
314  if (major != 1) {
315      error("%s: Driver Version %d not supported!", Name, major);
316      return -1;
317  }
318
319  memset(buf, 0, sizeof(buf));
320  if (ioctl(usblcd_file, IOC_GET_HARD_VERSION, buf) != 0) {
321      error("%s: ioctl() failed, could not get Hardware Version!", Name);
322      return -1;
323  }
324  info("%s: Hardware Version: %s", Name, buf);
325
326  if (sscanf(buf, "%d.%d", &major, &minor) != 2) {
327      error("%s: could not read Hardware Version!", Name);
328      return -1;
329  }
330
331  if (major != 1) {
332      error("%s: Hardware Version %d not supported!", Name, major);
333      return -1;
334  }
335    }
336
337    /* Init the command buffer */
338    Buffer = (char *) malloc(1024);
339    if (Buffer == NULL) {
340  error("%s: coommand buffer could not be allocated: malloc() failed", Name);
341  return -1;
342    }
343    BufPtr = Buffer;
344
345    /* initialize display */
346    drv_UL_command(0x29); /* 8 Bit mode, 1/16 duty cycle, 5x8 font */
347    drv_UL_command(0x08); /* Display off, cursor off, blink off */
348    drv_UL_command(0x0c); /* Display on, cursor off, blink off */
349    drv_UL_command(0x06); /* curser moves to right, no shift */
350
351    drv_UL_clear();   /* clear display */
352
353    if (!quiet) {
354  char buffer[40];
355  qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS);
356  if (drv_generic_text_greet(buffer, "http://www.usblcd.de")) {
357      sleep(3);
358      drv_UL_clear();
359  }
360    }
361
362    return 0;
363}
364
365
366/****************************************/
367/***            plugins               ***/
368/****************************************/
369
370/* none at the moment... */
371
372
373/****************************************/
374/***        widget callbacks          ***/
375/****************************************/
376
377
378/* using drv_generic_text_draw(W) */
379/* using drv_generic_text_icon_draw(W) */
380/* using drv_generic_text_bar_draw(W) */
381
382
383/****************************************/
384/***        exported functions        ***/
385/****************************************/
386
387
388/* list models */
389int drv_UL_list(void)
390{
391    printf("generic");
392    return 0;
393}
394
395
396/* initialize driver & display */
397int drv_UL_init(const char *section, const int quiet)
398{
399    WIDGET_CLASS wc;
400    int asc255bug;
401    int ret;
402
403    info("%s: %s", Name, "$Rev$");
404
405    /* display preferences */
406    XRES = 5;     /* pixel width of one char  */
407    YRES = 8;     /* pixel height of one char  */
408    CHARS = 8;      /* number of user-defineable characters */
409    CHAR0 = 0;      /* ASCII of first user-defineable char */
410    GOTO_COST = 2;    /* number of bytes a goto command requires */
411
412    /* real worker functions */
413    drv_generic_text_real_write = drv_UL_write;
414    drv_generic_text_real_defchar = drv_UL_defchar;
415
416
417    /* start display */
418    if ((ret = drv_UL_start(section, quiet)) != 0)
419  return ret;
420
421    /* initialize generic text driver */
422    if ((ret = drv_generic_text_init(section, Name)) != 0)
423  return ret;
424
425    /* initialize generic icon driver */
426    if ((ret = drv_generic_text_icon_init()) != 0)
427  return ret;
428
429    /* initialize generic bar driver */
430    if ((ret = drv_generic_text_bar_init(0)) != 0)
431  return ret;
432
433    /* add fixed chars to the bar driver */
434    /* most displays have a full block on ascii 255, but some have kind of  */
435    /* an 'inverted P'. If you specify 'asc255bug 1 in the config, this */
436    /* char will not be used, but rendered by the bar driver */
437    cfg_number(section, "asc255bug", 0, 0, 1, &asc255bug);
438    drv_generic_text_bar_add_segment(0, 0, 255, 32);  /* ASCII  32 = blank */
439    if (!asc255bug)
440  drv_generic_text_bar_add_segment(255, 255, 255, 255); /* ASCII 255 = block */
441
442    /* register text widget */
443    wc = Widget_Text;
444    wc.draw = drv_generic_text_draw;
445    widget_register(&wc);
446
447    /* register icon widget */
448    wc = Widget_Icon;
449    wc.draw = drv_generic_text_icon_draw;
450    widget_register(&wc);
451
452    /* register bar widget */
453    wc = Widget_Bar;
454    wc.draw = drv_generic_text_bar_draw;
455    widget_register(&wc);
456
457    /* register plugins */
458    /* none at the moment... */
459
460
461    return 0;
462}
463
464
465/* close driver & display */
466int drv_UL_quit(const int quiet)
467{
468
469    info("%s: shutting down.", Name);
470
471    /* flush buffer */
472    drv_UL_send();
473
474    drv_generic_text_quit();
475
476    /* clear display */
477    drv_UL_clear();
478
479    /* say goodbye... */
480    if (!quiet) {
481  drv_generic_text_greet("goodbye!", NULL);
482    }
483
484    if (use_libusb) {
485#ifdef HAVE_USB_H
486  drv_UL_close();
487#endif
488    } else {
489  debug("closing port %s", Port);
490  close(usblcd_file);
491    }
492
493    if (Buffer) {
494  free(Buffer);
495  Buffer = NULL;
496  BufPtr = Buffer;
497    }
498
499    return (0);
500}
501
502
503DRIVER drv_USBLCD = {
504    .name = Name,
505    .list = drv_UL_list,
506    .init = drv_UL_init,
507    .quit = drv_UL_quit,
508};
Note: See TracBrowser for help on using the browser.