phidgetmotorcontrol.c 10.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * USB Phidget MotorControl driver
 *
 * Copyright (C) 2006  Sean Young <sean@mess.org>
 *
 * 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; either version 2 of the License, or
 * (at your option) any later version.
 */

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

18 19
#include "phidget.h"

20 21 22 23 24 25 26 27
#define DRIVER_AUTHOR "Sean Young <sean@mess.org>"
#define DRIVER_DESC "USB PhidgetMotorControl Driver"

#define USB_VENDOR_ID_GLAB		0x06c2
#define USB_DEVICE_ID_MOTORCONTROL	0x0058

#define URB_INT_SIZE			8

28 29
static unsigned long device_no;

30 31 32
struct motorcontrol {
	struct usb_device *udev;
	struct usb_interface *intf;
33 34
	struct device *dev;
	int dev_no;
35 36 37 38 39 40 41 42 43
	u8 inputs[4];
	s8 desired_speed[2];
	s8 speed[2];
	s16 _current[2];
	s8 acceleration[2];
	struct urb *irq;
	unsigned char *data;
	dma_addr_t data_dma;

D
David Howells 已提交
44
	struct delayed_work do_notify;
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
	unsigned long input_events;
	unsigned long speed_events;
	unsigned long exceed_events;
};

static struct usb_device_id id_table[] = {
	{ USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_MOTORCONTROL) },
	{}
};
MODULE_DEVICE_TABLE(usb, id_table);

static int set_motor(struct motorcontrol *mc, int motor)
{
	u8 *buffer;
	int speed, speed2, acceleration;
	int retval;

	buffer = kzalloc(8, GFP_KERNEL);
	if (!buffer) {
64
		dev_err(&mc->intf->dev, "%s - out of memory\n", __func__);
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
		return -ENOMEM;
	}

	acceleration = mc->acceleration[motor] * 10;
	/* -127 <= speed <= 127 */
	speed = (mc->desired_speed[motor] * 127) / 100;
	/* -0x7300 <= speed2 <= 0x7300 */
	speed2 = (mc->desired_speed[motor] * 230 * 128) / 100;

	buffer[0] = motor;
	buffer[1] = speed;
	buffer[2] = acceleration >> 8;
	buffer[3] = acceleration;
	buffer[4] = speed2 >> 8;
	buffer[5] = speed2;

	retval = usb_control_msg(mc->udev,
			 usb_sndctrlpipe(mc->udev, 0),
			 0x09, 0x21, 0x0200, 0x0000, buffer, 8, 2000);

	if (retval != 8)
		dev_err(&mc->intf->dev, "usb_control_msg returned %d\n",
				retval);
	kfree(buffer);

	return retval < 0 ? retval : 0;
}

93
static void motorcontrol_irq(struct urb *urb)
94 95 96 97
{
	struct motorcontrol *mc = urb->context;
	unsigned char *buffer = mc->data;
	int i, level;
98 99
	int retval;
	int status = urb->status;;
100

101
	switch (status) {
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
	case 0:			/* success */
		break;
	case -ECONNRESET:	/* unlink */
	case -ENOENT:
	case -ESHUTDOWN:
		return;
	/* -EPIPE:  should clear the halt */
	default:		/* error */
		goto resubmit;
	}

	/* digital inputs */
	for (i=0; i<4; i++) {
		level = (buffer[0] >> i) & 1;
		if (mc->inputs[i] != level) {
			mc->inputs[i] = level;
			set_bit(i, &mc->input_events);
		}
	}

	/* motor speed */
	if (buffer[2] == 0) {
		for (i=0; i<2; i++) {
		level = ((s8)buffer[4+i]) * 100 / 127;
			if (mc->speed[i] != level) {
				mc->speed[i] = level;
				set_bit(i, &mc->speed_events);
			}
		}
	} else {
		int index = buffer[3] & 1;

		level = ((s8)buffer[4] << 8) | buffer[5];
		level = level * 100 / 29440;
		if (mc->speed[index] != level) {
			mc->speed[index] = level;
			set_bit(index, &mc->speed_events);
		}

		level = ((s8)buffer[6] << 8) | buffer[7];
		mc->_current[index] = level * 100 / 1572;
	}

	if (buffer[1] & 1)
		set_bit(0, &mc->exceed_events);

	if (buffer[1] & 2)
		set_bit(1, &mc->exceed_events);

	if (mc->input_events || mc->exceed_events || mc->speed_events)
D
David Howells 已提交
152
		schedule_delayed_work(&mc->do_notify, 0);
153 154

resubmit:
155 156
	retval = usb_submit_urb(urb, GFP_ATOMIC);
	if (retval)
157
		dev_err(&mc->intf->dev,
158
			"can't resubmit intr, %s-%s/motorcontrol0, retval %d\n",
159
			mc->udev->bus->bus_name,
160
			mc->udev->devpath, retval);
161 162
}

D
David Howells 已提交
163
static void do_notify(struct work_struct *work)
164
{
D
David Howells 已提交
165 166
	struct motorcontrol *mc =
		container_of(work, struct motorcontrol, do_notify.work);
167 168 169 170 171 172
	int i;
	char sysfs_file[8];

	for (i=0; i<4; i++) {
		if (test_and_clear_bit(i, &mc->input_events)) {
			sprintf(sysfs_file, "input%d", i);
173
			sysfs_notify(&mc->dev->kobj, NULL, sysfs_file);
174 175 176 177 178 179
		}
	}

	for (i=0; i<2; i++) {
		if (test_and_clear_bit(i, &mc->speed_events)) {
			sprintf(sysfs_file, "speed%d", i);
180
			sysfs_notify(&mc->dev->kobj, NULL, sysfs_file);
181 182 183 184 185 186 187 188 189 190 191
		}
	}

	for (i=0; i<2; i++) {
		if (test_and_clear_bit(i, &mc->exceed_events))
			dev_warn(&mc->intf->dev,
				"motor #%d exceeds 1.5 Amp current limit\n", i);
	}
}

#define show_set_speed(value)		\
192 193 194
static ssize_t set_speed##value(struct device *dev,			\
					struct device_attribute *attr,	\
					const char *buf, size_t count)	\
195
{									\
196
	struct motorcontrol *mc = dev_get_drvdata(dev);			\
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
	int speed;							\
	int retval;							\
									\
	if (sscanf(buf, "%d", &speed) < 1)				\
		return -EINVAL;						\
									\
	if (speed < -100 || speed > 100)				\
		return -EINVAL;						\
									\
	mc->desired_speed[value] = speed;				\
									\
	retval = set_motor(mc, value);					\
									\
	return retval ? retval : count;					\
}									\
									\
213 214 215
static ssize_t show_speed##value(struct device *dev,			\
					struct device_attribute *attr,	\
					char *buf)			\
216
{									\
217
	struct motorcontrol *mc = dev_get_drvdata(dev);			\
218 219
									\
	return sprintf(buf, "%d\n", mc->speed[value]);			\
220 221 222 223 224 225
}

#define speed_attr(value) 						\
	__ATTR(speed##value, S_IWUGO | S_IRUGO, 			\
		show_speed##value, set_speed##value)

226 227 228 229 230
show_set_speed(0);
show_set_speed(1);

#define show_set_acceleration(value)		\
static ssize_t set_acceleration##value(struct device *dev, 		\
231 232
					struct device_attribute *attr,	\
					const char *buf, size_t count)	\
233
{									\
234
	struct motorcontrol *mc = dev_get_drvdata(dev);			\
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
	int acceleration;						\
	int retval;							\
									\
	if (sscanf(buf, "%d", &acceleration) < 1)			\
		return -EINVAL;						\
									\
	if (acceleration < 0 || acceleration > 100)			\
		return -EINVAL;						\
									\
	mc->acceleration[value] = acceleration;				\
									\
	retval = set_motor(mc, value);					\
									\
	return retval ? retval : count;					\
}									\
									\
251 252 253
static ssize_t show_acceleration##value(struct device *dev,	 	\
					struct device_attribute *attr,	\
							char *buf)	\
254
{									\
255
	struct motorcontrol *mc = dev_get_drvdata(dev);			\
256 257
									\
	return sprintf(buf, "%d\n", mc->acceleration[value]);		\
258 259 260 261 262 263
}

#define acceleration_attr(value)	\
	__ATTR(acceleration##value, S_IWUGO | S_IRUGO,			\
		show_acceleration##value, set_acceleration##value)

264 265 266 267
show_set_acceleration(0);
show_set_acceleration(1);

#define show_current(value)	\
268 269 270
static ssize_t show_current##value(struct device *dev,			\
					struct device_attribute *attr,	\
					char *buf)			\
271
{									\
272
	struct motorcontrol *mc = dev_get_drvdata(dev);			\
273 274
									\
	return sprintf(buf, "%dmA\n", (int)mc->_current[value]);	\
275 276 277 278
}

#define current_attr(value)	\
	__ATTR(current##value, S_IRUGO, show_current##value, NULL)
279 280 281 282 283

show_current(0);
show_current(1);

#define show_input(value)	\
284 285 286
static ssize_t show_input##value(struct device *dev,			\
					struct device_attribute *attr,	\
					char *buf)			\
287
{									\
288
	struct motorcontrol *mc = dev_get_drvdata(dev);			\
289 290
									\
	return sprintf(buf, "%d\n", (int)mc->inputs[value]);		\
291 292 293 294
}

#define input_attr(value)	\
	__ATTR(input##value, S_IRUGO, show_input##value, NULL)
295 296 297 298 299 300

show_input(0);
show_input(1);
show_input(2);
show_input(3);

301 302 303 304 305 306 307 308 309 310 311 312 313
static struct device_attribute dev_attrs[] = {
	input_attr(0),
	input_attr(1),
	input_attr(2),
	input_attr(3),
	speed_attr(0),
	speed_attr(1),
	acceleration_attr(0),
	acceleration_attr(1),
	current_attr(0),
	current_attr(1)
};

314 315 316 317 318 319 320
static int motorcontrol_probe(struct usb_interface *intf, const struct usb_device_id *id)
{
	struct usb_device *dev = interface_to_usbdev(intf);
	struct usb_host_interface *interface;
	struct usb_endpoint_descriptor *endpoint;
	struct motorcontrol *mc;
	int pipe, maxp, rc = -ENOMEM;
321
	int bit, value, i;
322 323 324 325 326 327

	interface = intf->cur_altsetting;
	if (interface->desc.bNumEndpoints != 1)
		return -ENODEV;

	endpoint = &interface->endpoint[0].desc;
328
	if (!usb_endpoint_dir_in(endpoint))
329 330 331 332 333 334 335 336 337 338 339 340
		return -ENODEV;

	/*
	 * bmAttributes
	 */
	pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
	maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));

	mc = kzalloc(sizeof(*mc), GFP_KERNEL);
	if (!mc)
		goto out;

341
	mc->dev_no = -1;
342
	mc->data = usb_buffer_alloc(dev, URB_INT_SIZE, GFP_ATOMIC, &mc->data_dma);
343 344 345 346 347 348 349 350 351 352
	if (!mc->data)
		goto out;

	mc->irq = usb_alloc_urb(0, GFP_KERNEL);
	if (!mc->irq)
		goto out;

	mc->udev = usb_get_dev(dev);
	mc->intf = intf;
	mc->acceleration[0] = mc->acceleration[1] = 10;
D
David Howells 已提交
353
	INIT_DELAYED_WORK(&mc->do_notify, do_notify);
354 355 356 357 358 359 360 361
	usb_fill_int_urb(mc->irq, mc->udev, pipe, mc->data,
			maxp > URB_INT_SIZE ? URB_INT_SIZE : maxp,
			motorcontrol_irq, mc, endpoint->bInterval);
	mc->irq->transfer_dma = mc->data_dma;
	mc->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;

	usb_set_intfdata(intf, mc);

362 363 364 365 366 367
	do {
		bit = find_first_zero_bit(&device_no, sizeof(device_no));
		value = test_and_set_bit(bit, &device_no);
	} while(value);
	mc->dev_no = bit;

368 369
	mc->dev = device_create(phidget_class, &mc->udev->dev, MKDEV(0, 0), mc,
				"motorcontrol%d", mc->dev_no);
370 371 372 373 374 375
	if (IS_ERR(mc->dev)) {
		rc = PTR_ERR(mc->dev);
		mc->dev = NULL;
		goto out;
	}

376 377 378 379 380
	if (usb_submit_urb(mc->irq, GFP_KERNEL)) {
		rc = -EIO;
		goto out;
	}

381 382 383 384 385
	for (i=0; i<ARRAY_SIZE(dev_attrs); i++) {
		rc = device_create_file(mc->dev, &dev_attrs[i]);
		if (rc)
			goto out2;
	}
386

387
	dev_info(&intf->dev, "USB PhidgetMotorControl attached\n");
388 389

	return 0;
390 391 392
out2:
	while (i-- > 0)
		device_remove_file(mc->dev, &dev_attrs[i]);
393 394
out:
	if (mc) {
395
		usb_free_urb(mc->irq);
396 397
		if (mc->data)
			usb_buffer_free(dev, URB_INT_SIZE, mc->data, mc->data_dma);
398 399 400 401 402
		if (mc->dev)
			device_unregister(mc->dev);
		if (mc->dev_no >= 0)
			clear_bit(mc->dev_no, &device_no);

403 404 405 406 407 408 409 410 411
		kfree(mc);
	}

	return rc;
}

static void motorcontrol_disconnect(struct usb_interface *interface)
{
	struct motorcontrol *mc;
412
	int i;
413 414 415 416 417 418 419 420 421 422 423 424

	mc = usb_get_intfdata(interface);
	usb_set_intfdata(interface, NULL);
	if (!mc)
		return;

	usb_kill_urb(mc->irq);
	usb_free_urb(mc->irq);
	usb_buffer_free(mc->udev, URB_INT_SIZE, mc->data, mc->data_dma);

	cancel_delayed_work(&mc->do_notify);

425 426
	for (i=0; i<ARRAY_SIZE(dev_attrs); i++)
		device_remove_file(mc->dev, &dev_attrs[i]);
427

428
	device_unregister(mc->dev);
429 430

	usb_put_dev(mc->udev);
431
	clear_bit(mc->dev_no, &device_no);
432
	kfree(mc);
433 434

	dev_info(&interface->dev, "USB PhidgetMotorControl detached\n");
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
}

static struct usb_driver motorcontrol_driver = {
	.name = "phidgetmotorcontrol",
	.probe = motorcontrol_probe,
	.disconnect = motorcontrol_disconnect,
	.id_table = id_table
};

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

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

	return retval;
}

static void __exit motorcontrol_exit(void)
{
	usb_deregister(&motorcontrol_driver);
}

module_init(motorcontrol_init);
module_exit(motorcontrol_exit);

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