[Lcdproc] Fixing non portable code in picolcd

Robert Buchholz rbu@gentoo.org
Thu Oct 18 08:35:02 2007


--nextPart6744893.WTs78otitx
Content-Type: multipart/mixed;
  boundary="Boundary-01=_NqxFHgJbyOJIFUH"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

--Boundary-01=_NqxFHgJbyOJIFUH
Content-Type: text/plain;
  charset="iso-8859-2"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

On Thursday, 4. October 2007, Markus Dolze wrote:
> Hello,
>
> the attached diff should fix this.

The patch does not cleanly apply to 0.5.2, but I see that is intentional=20
as it's for inclusion in HEAD. I applied the attached patch to 0.5.2=20
and it compiles and works fine.


Thanks,

Robert

--Boundary-01=_NqxFHgJbyOJIFUH
Content-Type: text/x-csrc;
  charset="iso-8859-2";
  name="patch-server__drivers__picolcd.c"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="patch-server__drivers__picolcd.c"

Index: lcdproc-0.5.2/server/drivers/picolcd.c
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
=2D-- lcdproc-0.5.2.orig/server/drivers/picolcd.c
+++ lcdproc-0.5.2/server/drivers/picolcd.c
@@ -102,7 +102,6 @@ static char * keymap[KEYPAD_MAX] =3D {
 };
=20
 /* Private function definitions */
=2Dstatic usb_dev_handle *picolcd_open(void);
 static void picolcd_send(usb_dev_handle *lcd, unsigned char *data, int siz=
e);
 static void picolcd_write(usb_dev_handle *lcd, const int row, const int co=
l, const unsigned char *data);
 static void get_key_event  (usb_dev_handle *lcd, lcd_packet *packet, int t=
imeout);
@@ -118,6 +117,8 @@ MODULE_EXPORT char *symbol_prefix     =3D=20
 MODULE_EXPORT int  picoLCD_init(Driver *drvthis) {
 	PrivateData *pd;
 	int x;
+	struct usb_bus *bus;
+	struct usb_device *dev;
=20
 	pd =3D (PrivateData *) malloc(sizeof(PrivateData));
=20
@@ -127,7 +128,62 @@ MODULE_EXPORT int  picoLCD_init(Driver *
 	if (drvthis->store_private_ptr(drvthis, pd))
 		return -1;
=20
=2D	pd->lcd =3D picolcd_open();
+	/* Try to find picolcd device */
+	usb_init();
+	usb_find_busses();
+	usb_find_devices();
+
+	pd->lcd =3D NULL;
+	for (bus =3D usb_get_busses(); bus !=3D NULL; bus =3D bus->next) {
+		for (dev =3D bus->devices; dev !=3D NULL; dev =3D dev->next) {
+			if ((dev->descriptor.idVendor =3D=3D picoLCD_VENDOR) &&
+				(dev->descriptor.idProduct =3D=3D picoLCD_DEVICE)) {
+			=09
+				report(RPT_INFO, "Found picoLCD on bus %s device %s", bus->dirname, de=
v->filename);
+				pd->lcd =3D usb_open(dev);
+				goto done;
+			}
+		}
+	}
+	done:
+
+	if (pd->lcd !=3D NULL) {
+		debug(RPT_DEBUG, "%s: opening device succeeded", drvthis->name);
+
+		if (usb_set_configuration(pd->lcd, 0) < 0) {
+			usb_close(pd->lcd);
+			report(RPT_ERR, "%s: unable to set configuration", drvthis->name);
+			return -1;
+		}
+		usleep(100);
+
+		if (usb_claim_interface(pd->lcd, 0) < 0) {
+#ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP
+			if ((usb_detach_kernel_driver_np(pd->lcd, 0) < 0) ||
+				(usb_claim_interface(pd->lcd, 0) < 0)) {
+#ifdef LIBUSB_HAS_GET_DRIVER_NP
+				char driver[1024];
+				if (usb_get_driver_np(pd->lcd, 0, driver, sizeof(driver)) =3D=3D 0)
+					report(RPT_WARNING, "Interface 0 already claimed by '%s'", driver);
+#endif
+				usb_close(pd->lcd);
+				report(RPT_ERR, "%s: unable to re-claim interface", drvthis->name);
+			        return -1;
+			}
+#else
+			report(RPT_ERR, "%s: failed to claim interface", drvthis->name);
+			usb_close(pd->lcd);
+			return -1;
+#endif
+		}
+
+		if (usb_set_altinterface(pd->lcd, 0) < 0)
+			report(RPT_WARNING, "%s: unable to set alternate configuration", drvthi=
s->name);
+	} else {
+		report(RPT_ERR, "%s: no device found", drvthis->name);
+		return -1;
+	}	=09
+=09
 	pd->width  =3D 20; /* hard coded (mfg spec) */
 	pd->height =3D 2;  /* hard coded (mfg spec) */
 	pd->info =3D "picoLCD: Supports the LCD as installed on the M300 (http://=
www.mini-box.com/Mini-Box-M300-LCD) ";
@@ -612,56 +668,6 @@ MODULE_EXPORT char *picoLCD_get_info(Dri
=20
 /* Private functions */
=20
=2Dstatic usb_dev_handle *picolcd_open(void)
=2D{
=2D   =20
=2D	usb_dev_handle *lcd;
=2D        struct usb_bus *busses, *bus;
=2D	struct usb_device *dev;
=2D        char driver[1024];
=2D        int ret;
=2D   =20
=2D	lcd =3D NULL;
=2D
=2D        debug(RPT_DEBUG, "picolcd: scanning for devices...\n");
=2D
=2D	usb_init();
=2D	usb_find_busses();
=2D	usb_find_devices();
=2D	busses =3D usb_get_busses();
=2D
=2D	for (bus =3D busses; bus; bus =3D bus->next) {
=2D		for (dev =3D bus->devices; dev; dev =3D dev->next) {
=2D			if ((dev->descriptor.idVendor =3D=3D picoLCD_VENDOR) && (dev->descrip=
tor.idProduct =3D=3D picoLCD_DEVICE)) {
=2D				debug(RPT_DEBUG, "Found picoLCD on bus %s device %s \n", bus->dirnam=
e, dev->filename);
=2D				lcd =3D usb_open(dev);
=2D				ret =3D usb_get_driver_np(lcd, 0, driver, sizeof(driver));
=2D				if (ret =3D=3D 0) {
=2D					debug(RPT_DEBUG, "Interface 0 already claimed by '%s' attempting to=
 detach driver...\n", driver);
=2D					if (usb_detach_kernel_driver_np(lcd, 0) < 0) {
=2D						debug(RPT_DEBUG, "Failed to detach '%s' driver !\n", driver);
=2D						return NULL;
=2D					}
=2D				}
=2D
=2D				usb_set_configuration(lcd, 1);
=2D				usleep(100);
=2D
=2D				if (usb_claim_interface(lcd, 0) < 0) {
=2D					debug(RPT_DEBUG, "Failed to claim interface !\n");
=2D					return NULL;
=2D				}
=2D
=2D				usb_set_altinterface(lcd, 0);
=2D				return lcd;
=2D			}
=2D		}
=2D	}
=2D=09
=2D	debug(RPT_DEBUG, "Could not find a picoLCD !\n");
=2D	return NULL;
=2D}
=2D
 static void picolcd_send(usb_dev_handle *lcd, unsigned char *data, int siz=
e)
 {
         usb_interrupt_write(lcd, USB_ENDPOINT_OUT + 1, (char *) data, size=
, 1000);

--Boundary-01=_NqxFHgJbyOJIFUH--

--nextPart6744893.WTs78otitx
Content-Type: application/pgp-signature; name=signature.asc 
Content-Description: This is a digitally signed message part.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQBHFxqNyZx3L/ph1soRAktIAJ9N1aXvhsr7vVA6mH4Ql1eun2YSWQCgkaBZ
MyZOTNCAscI5pSjc2plJXGM=
=zMFl
-----END PGP SIGNATURE-----

--nextPart6744893.WTs78otitx--