1/*
2 * Haiku Backend for libusb
3 * Copyright © 2014 Akshay Jaggi <akshay1994.leo@gmail.com>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#include <List.h>
21#include <Locker.h>
22#include <Autolock.h>
23#include <USBKit.h>
24#include <map>
25#include "libusbi.h"
26#include "haiku_usb_raw.h"
27
28using namespace std;
29
30class USBDevice;
31class USBDeviceHandle;
32class USBTransfer;
33
34class USBDevice {
35public:
36						USBDevice(const char *);
37	virtual					~USBDevice();
38	const char*				Location() const;
39	uint8					CountConfigurations() const;
40	const usb_device_descriptor*		Descriptor() const;
41	const usb_configuration_descriptor*	ConfigurationDescriptor(uint32) const;
42	const usb_configuration_descriptor*	ActiveConfiguration() const;
43	uint8					EndpointToIndex(uint8) const;
44	uint8					EndpointToInterface(uint8) const;
45	int					ClaimInterface(int);
46	int					ReleaseInterface(int);
47	int					CheckInterfacesFree(int);
48	int					SetActiveConfiguration(int);
49	int					ActiveConfigurationIndex() const;
50	bool					InitCheck();
51private:
52	int					Initialise();
53	unsigned int				fClaimedInterfaces;	// Max Interfaces can be 32. Using a bitmask
54	usb_device_descriptor			fDeviceDescriptor;
55	unsigned char**				fConfigurationDescriptors;
56	int					fActiveConfiguration;
57	char*					fPath;
58	map<uint8,uint8>			fConfigToIndex;
59	map<uint8,uint8>*			fEndpointToIndex;
60	map<uint8,uint8>*			fEndpointToInterface;
61	bool					fInitCheck;
62};
63
64class USBDeviceHandle {
65public:
66				USBDeviceHandle(USBDevice *dev);
67	virtual			~USBDeviceHandle();
68	int			ClaimInterface(int);
69	int			ReleaseInterface(int);
70	int			SetConfiguration(int);
71	int			SetAltSetting(int, int);
72	status_t		SubmitTransfer(struct usbi_transfer *);
73	status_t		CancelTransfer(USBTransfer *);
74	bool			InitCheck();
75private:
76	int			fRawFD;
77	static status_t		TransfersThread(void *);
78	void			TransfersWorker();
79	USBDevice*		fUSBDevice;
80	unsigned int		fClaimedInterfaces;
81	BList			fTransfers;
82	BLocker			fTransfersLock;
83	sem_id			fTransfersSem;
84	thread_id		fTransfersThread;
85	bool			fInitCheck;
86};
87
88class USBTransfer {
89public:
90					USBTransfer(struct usbi_transfer *, USBDevice *);
91	virtual				~USBTransfer();
92	void				Do(int);
93	struct usbi_transfer*		UsbiTransfer();
94	void				SetCancelled();
95	bool				IsCancelled();
96private:
97	struct usbi_transfer*		fUsbiTransfer;
98	struct libusb_transfer*		fLibusbTransfer;
99	USBDevice*			fUSBDevice;
100	BLocker				fStatusLock;
101	bool				fCancelled;
102};
103
104class USBRoster {
105public:
106			USBRoster();
107	virtual		~USBRoster();
108	int		Start();
109	void		Stop();
110private:
111	void*		fLooper;
112};
113