summaryrefslogtreecommitdiff
path: root/drivers/usb/misc/gotemp.c
blob: f181a8847e51d9d72f683402d69616426b09a5b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
 * USB GoTemp driver
 *
 * Copyright (C) 2005 Greg Kroah-Hartman (greg@kroah.com)
 *
 *	This program is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU General Public License as
 *	published by the Free Software Foundation, version 2.
 *
 */

#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/usb.h>


#define DRIVER_AUTHOR "Greg Kroah-Hartman, greg@kroah.com"
#define DRIVER_DESC "USB GoTemp driver"

#define VENDOR_ID	0x08f7
#define PRODUCT_ID	0x0002

/* table of devices that work with this driver */
static struct usb_device_id id_table [] = {
	{ USB_DEVICE(VENDOR_ID, PRODUCT_ID) },
	{ },
};
MODULE_DEVICE_TABLE(usb, id_table);

struct gotemp {
	struct usb_device *udev;
	int temp;
	unsigned char *int_in_buffer;
	__u8 int_in_endpointAddr;
	struct urb *int_in_urb;
};

#define CMD_ID_GET_STATUS			0x10
#define CMD_ID_WRITE_LOCAL_NV_MEM_1BYTE		0x11
#define CMD_ID_WRITE_LOCAL_NV_MEM_2BYTES	0x12
#define CMD_ID_WRITE_LOCAL_NV_MEM_3BYTES	0x13
#define CMD_ID_WRITE_LOCAL_NV_MEM_4BYTES	0x14
#define CMD_ID_WRITE_LOCAL_NV_MEM_5BYTES	0x15
#define CMD_ID_WRITE_LOCAL_NV_MEM_6BYTES	0x16
#define CMD_ID_READ_LOCAL_NV_MEM		0x17
#define CMD_ID_START_MEASUREMENTS		0x18
#define CMD_ID_STOP_MEASUREMENTS		0x19
#define CMD_ID_INIT				0x1A
#define CMD_ID_SET_MEASUREMENT_PERIOD		0x1B
#define CMD_ID_GET_MEASUREMENT_PERIOD		0x1C
#define CMD_ID_SET_LED_STATE			0x1D
#define CMD_ID_GET_LED_STATE			0x1E
#define CMD_ID_GET_SERIAL_NUMBER		0x20

struct output_packet {
	u8	cmd;
	u8	params[7];
} __attribute__ ((packed));

struct measurement_packet {
	u8	measurements_in_packet;
	u8	rolling_counter;
	__le16	measurement0;
	__le16	measurement1;
	__le16	measurement2;
} __attribute__ ((packed));

static int send_cmd(struct gotemp *gdev, u8 cmd)
{
	struct output_packet *pkt;
	int retval;

	pkt = kzalloc(sizeof(*pkt), GFP_ATOMIC);
	if (!pkt)
		return -ENOMEM;
	pkt->cmd = cmd;

	retval = usb_control_msg(gdev->udev,
				 usb_sndctrlpipe(gdev->udev, 0),
				 0x09,		/* bRequest = SET_REPORT */
				 0x21,		/* bRequestType = 00100001 */
				 0x0200,	/* or is it 0x0002? */
				 0x0000,	/* interface 0 */
				 pkt, sizeof(*pkt), 10000);
	dev_dbg(&gdev->udev->dev, "retval=%d\n", retval);
	if (retval == sizeof(*pkt))
		retval = 0;

	kfree(pkt);
	return retval;
}

static void init_dev(struct gotemp *gdev)
{
	int retval;

	/* First send an init message */
	send_cmd(gdev, CMD_ID_INIT);

	/* hack hack hack */
	/* problem is, we want a usb_interrupt_msg() call to read the interrupt
	 * endpoint right now.  only after it is flushed, can we properly start
	 * up the measurements.  */
	msleep(1000);

	/* kick off interrupt urb */
	retval = usb_submit_urb(gdev->int_in_urb, GFP_KERNEL);
	if (retval)
		dev_err(&gdev->udev->dev,
			"%s - Error %d submitting interrupt urb\n",
			__func__, retval);

	msleep(3000);
	send_cmd(gdev, CMD_ID_START_MEASUREMENTS);
}

static ssize_t show_temp(struct device *dev, struct device_attribute *attr,
			 char *buf)
{
	struct usb_interface *intf = to_usb_interface(dev);
	struct gotemp *gdev = usb_get_intfdata(intf);

	return sprintf(buf, "%d\n", gdev->temp);
}

static DEVICE_ATTR(temp, S_IRUGO, show_temp, NULL);

static void read_int_callback(struct urb *urb)
{
	struct gotemp *gdev = urb->context;
	unsigned char *data = urb->transfer_buffer;
	struct measurement_packet *measurement = urb->transfer_buffer;
	int retval;
	int i;

	switch (urb->status) {
	case 0:
		/* success */
		break;
	case -ECONNRESET:
	case -ENOENT:
	case -ESHUTDOWN:
		/* this urb is terminated, clean up */
		dbg("%s - urb shutting down with status: %d",
		    __func__, urb->status);
		return;
	default:
		dbg("%s - nonzero urb status received: %d",
		    __func__, urb->status);
		goto exit;
	}

	dev_info(&urb->dev->dev, "int read data: ");
	for (i = 0; i < urb->actual_length; ++i)
		printk("%02x ", data[i]);
	printk("\n");

	dev_dbg(&urb->dev->dev, "counter %d, temp=%d\n",
		 measurement->rolling_counter,
		 measurement->measurement0);
	gdev->temp = le16_to_cpu(measurement->measurement0);

exit:
	retval = usb_submit_urb(urb, GFP_ATOMIC);
	if (retval)
		dev_err(&urb->dev->dev,
			"%s - Error %d submitting interrupt urb\n",
			__func__, retval);
}

static int gotemp_probe(struct usb_interface *interface,
			const struct usb_device_id *id)
{
	struct usb_device *udev = interface_to_usbdev(interface);
	struct gotemp *gdev = NULL;
	int retval = -ENOMEM;
	int i;
	struct usb_host_interface *iface_desc;
	struct usb_endpoint_descriptor *endpoint = NULL;
	size_t buffer_size = 0;

	gdev = kzalloc(sizeof(struct gotemp), GFP_KERNEL);
	if (gdev == NULL) {
		dev_err(&interface->dev, "Out of memory\n");
		goto error;
	}

	gdev->udev = usb_get_dev(udev);

	/* find the one control endpoint of this device */
	iface_desc = interface->cur_altsetting;
	for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
		endpoint = &iface_desc->endpoint[i].desc;

		if (usb_endpoint_is_int_in(endpoint)) {
			buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
			gdev->int_in_endpointAddr = endpoint->bEndpointAddress;
			gdev->int_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
			if (!gdev->int_in_buffer) {
				dev_err(&interface->dev,
					"Could not allocate buffer");
				goto error;
			}
			break;
		}
	}
	if (!gdev->int_in_endpointAddr) {
		dev_err(&interface->dev, "Could not find int-in endpoint");
		retval = -ENODEV;
		goto error;
	}

	gdev->int_in_urb = usb_alloc_urb(0, GFP_KERNEL);
	if (!gdev->int_in_urb) {
		dev_err(&interface->dev, "No free urbs available\n");
		goto error;
	}
	usb_fill_int_urb(gdev->int_in_urb, udev,
			 usb_rcvintpipe(udev,
					endpoint->bEndpointAddress),
			 gdev->int_in_buffer, buffer_size,
			 read_int_callback, gdev,
			 endpoint->bInterval);

	usb_set_intfdata(interface, gdev);

	retval = device_create_file(&interface->dev, &dev_attr_temp);
	if (retval)
		goto error;

	init_dev(gdev);

	dev_info(&interface->dev, "USB GoTemp device now attached\n");
	return 0;

error:
	usb_set_intfdata(interface, NULL);
	if (gdev) {
		usb_free_urb(gdev->int_in_urb);
		kfree(gdev->int_in_buffer);
	}
	kfree(gdev);
	return retval;
}

static void gotemp_disconnect(struct usb_interface *interface)
{
	struct gotemp *gdev;

	gdev = usb_get_intfdata(interface);
	usb_set_intfdata(interface, NULL);

	device_remove_file(&interface->dev, &dev_attr_temp);

	usb_put_dev(gdev->udev);

	usb_kill_urb(gdev->int_in_urb);
	usb_free_urb(gdev->int_in_urb);
	kfree(gdev->int_in_buffer);
	kfree(gdev);

	dev_info(&interface->dev, "USB GoTemp now disconnected\n");
}

static struct usb_driver gotemp_driver = {
	.name =		"gotemp",
	.probe =	gotemp_probe,
	.disconnect =	gotemp_disconnect,
	.id_table =	id_table,
};

static int __init gotemp_init(void)
{
	int retval = 0;

	retval = usb_register(&gotemp_driver);
	if (retval)
		err("usb_register failed. Error number %d", retval);
	return retval;
}

static void __exit gotemp_exit(void)
{
	usb_deregister(&gotemp_driver);
}

module_init(gotemp_init);
module_exit(gotemp_exit);

MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE("GPL");