Exemplo n.º 1
0
Arquivo: usb.go Projeto: thequux/gousb
// Claim this interface. Fails if the interface is already claimed by another process.
func (i *Interface) Claim() *UsbError {
	if err := returnUsbError(C.libusb_claim_interface(i.handle.handle, i.num)); err != nil {
		return err
	}
	i.claimed++
	return nil
}
Exemplo n.º 2
0
// Open opens an attached device and claims the interface. To ensure proper
// reference counting, Open must be called within the context of a Walk.
func (d *Device) Open() error {
	if err := C.libusb_open(d.dev, &d.handle); err != C.LIBUSB_SUCCESS {
		return &libusbError{err}
	}
	if err := C.libusb_claim_interface(d.handle, interfaceNum); err != C.LIBUSB_SUCCESS {
		C.libusb_close(d.handle)
		return &libusbError{err}
	}
	return nil
}
Exemplo n.º 3
0
func Start(color chan byte, output chan guinput.KeyEvent) *C.struct_libusb_device_handle {

	C.libusb_init(nil)
	fmt.Println(KEYMAP[0x21])

	key_device_handle := C.libusb_open_device_with_vid_pid(nil, 0x046d, 0xc22d)

	//detach any necessary kernel drivers from MY keyboard
	if C.libusb_kernel_driver_active(key_device_handle, 0) == 1 {
		log.Print("kernel driver active on main interface")
		e := C.libusb_detach_kernel_driver(key_device_handle, 0)
		if e != 0 {
			log.Fatal("Can't detach kernel driver")
		}
	}
	if C.libusb_kernel_driver_active(key_device_handle, 1) == 1 {
		fmt.Println("kernel driver active")
		e := C.libusb_detach_kernel_driver(key_device_handle, 1)
		if e != 0 {
			log.Fatal("Can't detach kernel driver")
		}
	}

	//Claim the interfaces we'll be listening on
	r := C.libusb_claim_interface(key_device_handle, 0)
	if r != 0 {
		log.Fatal("Can't claim main interface")
	}
	r = C.libusb_claim_interface(key_device_handle, 1)
	if r != 0 {
		log.Fatal("Can't claim special interface")
	}

	log.Print("Starting libusb goroutines\n")

	go ColorChange(key_device_handle, color)
	//go LCDChange(key_device_handle, lcd)
	go NormalKeyMonitor(key_device_handle, output)
	go SpecialKeyMonitor(key_device_handle, output)

	log.Print("Libusb Goroutines started\n")
	return key_device_handle
}
Exemplo n.º 4
0
// First returns the first supported device attached to the host. Unlike Walk,
// the returned Device is opened automatically. This function exists primarily
// for testing.
func First() (*Device, error) {
	handle := C.libusb_open_device_with_vid_pid(context, idVendor, idProduct)
	if handle == nil {
		return nil, errors.New("no devices found")
	}
	if err := C.libusb_claim_interface(handle, interfaceNum); err != C.LIBUSB_SUCCESS {
		C.libusb_close(handle)
		return nil, &libusbError{err}
	}
	return &Device{
		dev:    C.libusb_get_device(handle),
		handle: handle,
	}, nil
}
Exemplo n.º 5
0
func StartLibUsb() error {
	Connected = true

	key_device_handle := C.libusb_open_device_with_vid_pid(nil, 0x046d, 0xc22d)
	if key_device_handle != nil {

		if C.libusb_kernel_driver_active(key_device_handle, 1) == 1 {
			e := C.libusb_detach_kernel_driver(key_device_handle, 1)
			if e != 0 {
				log.Fatal("Can't detach kernel driver")
			}
		}

		r := C.libusb_claim_interface(key_device_handle, 1)
		if r != 0 {
			log.Fatal("Can't claim special interface")
		}

		keyboardHandle = key_device_handle

		return nil
	}
	return errors.New("could not open driver")
}
Exemplo n.º 6
0
Arquivo: usb.go Projeto: hanwen/usb
// Claim an interface on a given device handle.
func (h *DeviceHandle) ClaimInterface(num byte) error {
	return toErr(C.libusb_claim_interface(h.me(), C.int(num)))
}
Exemplo n.º 7
0
func (d *Device) OpenEndpoint(conf, iface, setup, epoint uint8) (Endpoint, error) {
	end := &endpoint{
		Device: d,
	}

	var setAlternate bool
	for _, c := range d.Configs {
		if c.Config != conf {
			continue
		}
		debug.Printf("found conf: %#v\n", c)
		for _, i := range c.Interfaces {
			if i.Number != iface {
				continue
			}
			debug.Printf("found iface: %#v\n", i)
			for i, s := range i.Setups {
				if s.Alternate != setup {
					continue
				}
				setAlternate = i != 0

				debug.Printf("found setup: %#v [default: %v]\n", s, !setAlternate)
				for _, e := range s.Endpoints {
					debug.Printf("ep %02x search: %#v\n", epoint, s)
					if e.Address != epoint {
						continue
					}
					end.InterfaceSetup = s
					end.EndpointInfo = e
					switch tt := TransferType(e.Attributes) & TRANSFER_TYPE_MASK; tt {
					case TRANSFER_TYPE_BULK:
						end.xfer = bulk_xfer
					case TRANSFER_TYPE_INTERRUPT:
						end.xfer = interrupt_xfer
					case TRANSFER_TYPE_ISOCHRONOUS:
						end.xfer = isochronous_xfer
					default:
						return nil, fmt.Errorf("usb: %s transfer is unsupported", tt)
					}
					goto found
				}
				return nil, fmt.Errorf("usb: unknown endpoint %02x", epoint)
			}
			return nil, fmt.Errorf("usb: unknown setup %02x", setup)
		}
		return nil, fmt.Errorf("usb: unknown interface %02x", iface)
	}
	return nil, fmt.Errorf("usb: unknown configuration %02x", conf)

found:

	// Set the configuration
	var activeConf C.int
	if errno := C.libusb_get_configuration(d.handle, &activeConf); errno < 0 {
		return nil, fmt.Errorf("usb: getcfg: %s", usbError(errno))
	}
	if int(activeConf) != int(conf) {
		if errno := C.libusb_set_configuration(d.handle, C.int(conf)); errno < 0 {
			return nil, fmt.Errorf("usb: setcfg: %s", usbError(errno))
		}
	}

	// Claim the interface
	if errno := C.libusb_claim_interface(d.handle, C.int(iface)); errno < 0 {
		return nil, fmt.Errorf("usb: claim: %s", usbError(errno))
	}

	// Increment the claim count
	d.lock.Lock()
	d.claimed[iface]++
	d.lock.Unlock() // unlock immediately because the next calls may block

	// Choose the alternate
	if setAlternate {
		if errno := C.libusb_set_interface_alt_setting(d.handle, C.int(iface), C.int(setup)); errno < 0 {
			debug.Printf("altsetting error: %s", usbError(errno))
			return nil, fmt.Errorf("usb: setalt: %s", usbError(errno))
		}
	}

	return end, nil
}
Exemplo n.º 8
0
func (d *Device) OpenEndpoint(conf, iface, setup, epoint uint8) (Endpoint, error) {
	end := &endpoint{
		Device: d,
	}

	for _, c := range d.Configs {
		if c.Config != conf {
			continue
		}
		fmt.Printf("found conf: %#v\n", c)
		for _, i := range c.Interfaces {
			if i.Number != iface {
				continue
			}
			fmt.Printf("found iface: %#v\n", i)
			for _, s := range i.Setups {
				if s.Alternate != setup {
					continue
				}
				fmt.Printf("found setup: %#v\n", s)
				for _, e := range s.Endpoints {
					fmt.Printf("ep %02x search: %#v\n", epoint, s)
					if e.Address != epoint {
						continue
					}
					end.InterfaceSetup = s
					end.EndpointInfo = e
					switch tt := TransferType(e.Attributes) & TRANSFER_TYPE_MASK; tt {
					case TRANSFER_TYPE_BULK:
						end.xfer = bulk_xfer
					case TRANSFER_TYPE_INTERRUPT:
						end.xfer = interrupt_xfer
					case TRANSFER_TYPE_ISOCHRONOUS:
						end.xfer = isochronous_xfer
					default:
						return nil, fmt.Errorf("usb: %s transfer is unsupported", tt)
					}
					goto found
				}
				return nil, fmt.Errorf("usb: unknown endpoint %02x", epoint)
			}
			return nil, fmt.Errorf("usb: unknown setup %02x", setup)
		}
		return nil, fmt.Errorf("usb: unknown interface %02x", iface)
	}
	return nil, fmt.Errorf("usb: unknown configuration %02x", conf)

found:
	// Increment the claim count
	d.lock.Lock()
	defer d.lock.Unlock()

	// Only claim when needed
	if d.claimed[iface] <= 0 {
		// Set the configuration
		if errno := C.libusb_set_configuration(d.handle, C.int(conf)); errno < 0 {
			return nil, fmt.Errorf("usb: setcfg: %s", usbError(errno))
		}

		fmt.Printf("claiming interface %v", iface)
		// Claim the interface
		if errno := C.libusb_claim_interface(d.handle, C.int(iface)); errno < 0 {
			return nil, fmt.Errorf("usb: claim: %s", usbError(errno))

			d.claimed[iface]++
		}

		// Choose the alternate
		// if errno := C.libusb_set_interface_alt_setting(d.handle, C.int(iface), C.int(setup)); errno < 0 {
		// 	// This doesn't seem to work on Mac OS X, it works on my Ubuntu machine.
		// 	log.Printf("ignoring altsetting error: %s", usbError(errno))
		// }
	}

	return end, nil
}