phidgetkit.c 19.9 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3
/*
 * USB PhidgetInterfaceKit driver 1.0
 *
4 5
 * Copyright (C) 2004, 2006 Sean Young <sean@mess.org>
 * Copyright (C) 2005 Daniel Saakes <daniel@saakes.net>
L
Linus Torvalds 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 * Copyright (C) 2004 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; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This is a driver for the USB PhidgetInterfaceKit.
 */

#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 "Sean Young <sean@mess.org>"
#define DRIVER_DESC "USB PhidgetInterfaceKit Driver"

#define USB_VENDOR_ID_GLAB		0x06c2
#define USB_DEVICE_ID_INTERFACEKIT004	0x0040
28
#define USB_DEVICE_ID_INTERFACEKIT01616	0x0044
L
Linus Torvalds 已提交
29 30 31 32 33 34 35
#define USB_DEVICE_ID_INTERFACEKIT888	0x0045
#define USB_DEVICE_ID_INTERFACEKIT047	0x0051
#define USB_DEVICE_ID_INTERFACEKIT088	0x0053

#define USB_VENDOR_ID_WISEGROUP		0x0925
#define USB_DEVICE_ID_INTERFACEKIT884	0x8201

36 37 38
#define MAX_INTERFACES			16

#define URB_INT_SIZE			8
L
Linus Torvalds 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

struct driver_interfacekit {
	int sensors;
	int inputs;
	int outputs;
	int has_lcd;
};
#define ifkit(_sensors, _inputs, _outputs, _lcd)			\
static struct driver_interfacekit ph_##_sensors##_inputs##_outputs = {	\
	.sensors	= _sensors,					\
	.inputs		= _inputs,					\
	.outputs	= _outputs,					\
	.has_lcd	= _lcd,						\
};
ifkit(0, 0, 4, 0);
ifkit(8, 8, 8, 0);
ifkit(0, 4, 7, 1);
ifkit(8, 8, 4, 0);
ifkit(0, 8, 8, 1);
58
ifkit(0, 16, 16, 0);
L
Linus Torvalds 已提交
59

60
struct interfacekit {
L
Linus Torvalds 已提交
61 62 63
	struct usb_device *udev;
	struct usb_interface *intf;
	struct driver_interfacekit *ifkit;
64 65 66
	unsigned long outputs;
	u8 inputs[MAX_INTERFACES];
	u16 sensors[MAX_INTERFACES];
L
Linus Torvalds 已提交
67 68 69 70 71
	u8 lcd_files_on;

	struct urb *irq;
	unsigned char *data;
	dma_addr_t data_dma;
72 73 74 75

	struct work_struct do_notify;
	unsigned long input_events;
	unsigned long sensor_events;
L
Linus Torvalds 已提交
76 77 78 79 80 81 82 83 84 85 86
};

static struct usb_device_id id_table[] = {
	{USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_INTERFACEKIT004),
		.driver_info = (kernel_ulong_t)&ph_004},
	{USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_INTERFACEKIT888),
		.driver_info = (kernel_ulong_t)&ph_888},
	{USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_INTERFACEKIT047),
		.driver_info = (kernel_ulong_t)&ph_047},
	{USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_INTERFACEKIT088),
		.driver_info = (kernel_ulong_t)&ph_088},
87 88
	{USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_INTERFACEKIT01616),
		.driver_info = (kernel_ulong_t)&ph_01616},
L
Linus Torvalds 已提交
89 90 91 92 93 94
	{USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_INTERFACEKIT884),
		.driver_info = (kernel_ulong_t)&ph_884},
	{}
};
MODULE_DEVICE_TABLE(usb, id_table);

95
static int change_outputs(struct interfacekit *kit, int output_num, int enable)
L
Linus Torvalds 已提交
96
{
97
	u8 *buffer;
L
Linus Torvalds 已提交
98
	int retval;
99 100 101 102 103

	if (enable)
		set_bit(output_num, &kit->outputs);
	else
		clear_bit(output_num, &kit->outputs);
L
Linus Torvalds 已提交
104

105
	buffer = kzalloc(4, GFP_KERNEL);
L
Linus Torvalds 已提交
106
	if (!buffer) {
107
		dev_err(&kit->udev->dev, "%s - out of memory\n", __FUNCTION__);
L
Linus Torvalds 已提交
108 109
		return -ENOMEM;
	}
110 111
	buffer[0] = (u8)kit->outputs;
	buffer[1] = (u8)(kit->outputs >> 8);
L
Linus Torvalds 已提交
112

113
	dev_dbg(&kit->udev->dev, "sending data: 0x%04x\n", (u16)kit->outputs);
L
Linus Torvalds 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126

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

	if (retval != 4)
		dev_err(&kit->udev->dev, "usb_control_msg returned %d\n", 
				retval);
	kfree(buffer);

	return retval < 0 ? retval : 0;
}

127
static int change_string(struct interfacekit *kit, const char *display, unsigned char row)
L
Linus Torvalds 已提交
128 129
{
	unsigned char *buffer;
130
	unsigned char *form_buffer;
L
Linus Torvalds 已提交
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
	int retval = -ENOMEM;
	int i,j, len, buf_ptr;
	
	buffer = kmalloc(8, GFP_KERNEL);
	form_buffer = kmalloc(30, GFP_KERNEL);
	if ((!buffer) || (!form_buffer)) {
		dev_err(&kit->udev->dev, "%s - out of memory\n", __FUNCTION__);
		goto exit;
	}

	len = strlen(display);
	if (len > 20)
		len = 20;

	dev_dbg(&kit->udev->dev, "Setting LCD line %d to %s\n", row, display);

	form_buffer[0] = row * 0x40 + 0x80;
	form_buffer[1] = 0x02;
	buf_ptr = 2;
	for (i = 0; i<len; i++)
		form_buffer[buf_ptr++] = display[i];

	for (i = 0; i < (20 - len); i++)
		form_buffer[buf_ptr++] = 0x20;
	form_buffer[buf_ptr++] = 0x01;
	form_buffer[buf_ptr++] = row * 0x40 + 0x80 + strlen(display);

	for (i = 0; i < buf_ptr; i += 7) {
		if ((buf_ptr - i) > 7)
			len = 7;
		else
			len = (buf_ptr - i);
		for (j = 0; j < len; j++)
			buffer[j] = form_buffer[i + j];
		buffer[7] = len;

		retval = usb_control_msg(kit->udev,
				 usb_sndctrlpipe(kit->udev, 0),
				 0x09, 0x21, 0x0200, 0x0000, buffer, 8, 2000);
		if (retval < 0)
			goto exit;
	}

	retval = 0;
exit:
	kfree(buffer);
	kfree(form_buffer);

	return retval;
}

#define set_lcd_line(number)	\
183
static ssize_t lcd_line_##number(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)	\
L
Linus Torvalds 已提交
184 185
{											\
	struct usb_interface *intf = to_usb_interface(dev);				\
186
	struct interfacekit *kit = usb_get_intfdata(intf);				\
L
Linus Torvalds 已提交
187 188 189 190 191 192 193
	change_string(kit, buf, number - 1);						\
	return count;									\
}											\
static DEVICE_ATTR(lcd_line_##number, S_IWUGO, NULL, lcd_line_##number);
set_lcd_line(1);
set_lcd_line(2);

194
static ssize_t set_backlight(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
L
Linus Torvalds 已提交
195 196
{
	struct usb_interface *intf = to_usb_interface(dev);
197
	struct interfacekit *kit = usb_get_intfdata(intf);
L
Linus Torvalds 已提交
198 199 200 201
	int enabled;
	unsigned char *buffer;
	int retval = -ENOMEM;
	
202
	buffer = kzalloc(8, GFP_KERNEL);
L
Linus Torvalds 已提交
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
	if (!buffer) {
		dev_err(&kit->udev->dev, "%s - out of memory\n", __FUNCTION__);
		goto exit;
	}

	if (sscanf(buf, "%d", &enabled) < 1) {
		retval = -EINVAL;
		goto exit;
	}
	if (enabled)
		buffer[0] = 0x01;
	buffer[7] = 0x11;

	dev_dbg(&kit->udev->dev, "Setting backlight to %s\n", enabled ? "on" : "off");
	
	retval = usb_control_msg(kit->udev,
			 usb_sndctrlpipe(kit->udev, 0),
			 0x09, 0x21, 0x0200, 0x0000, buffer, 8, 2000);
	if (retval < 0)
		goto exit;

	retval = count;
exit:
	kfree(buffer);
	return retval;
}
static DEVICE_ATTR(backlight, S_IWUGO, NULL, set_backlight);

231
static void remove_lcd_files(struct interfacekit *kit)
L
Linus Torvalds 已提交
232 233 234 235 236 237 238 239 240
{
	if (kit->lcd_files_on) {
		dev_dbg(&kit->udev->dev, "Removing lcd files\n");
		device_remove_file(&kit->intf->dev, &dev_attr_lcd_line_1);
		device_remove_file(&kit->intf->dev, &dev_attr_lcd_line_2);
		device_remove_file(&kit->intf->dev, &dev_attr_backlight);
	}
}

241
static ssize_t enable_lcd_files(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
L
Linus Torvalds 已提交
242 243
{
	struct usb_interface *intf = to_usb_interface(dev);
244
	struct interfacekit *kit = usb_get_intfdata(intf);
L
Linus Torvalds 已提交
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
	int enable;
	
	if (kit->ifkit->has_lcd == 0)
		return -ENODEV;

	if (sscanf(buf, "%d", &enable) < 1)
		return -EINVAL;

	if (enable) {
		if (!kit->lcd_files_on) {
			dev_dbg(&kit->udev->dev, "Adding lcd files\n");
			device_create_file(&kit->intf->dev, &dev_attr_lcd_line_1);
			device_create_file(&kit->intf->dev, &dev_attr_lcd_line_2);
			device_create_file(&kit->intf->dev, &dev_attr_backlight);
			kit->lcd_files_on = 1;
		}
	} else {
		if (kit->lcd_files_on) {
			remove_lcd_files(kit);
			kit->lcd_files_on = 0;
		}
	}
	
	return count;
}
static DEVICE_ATTR(lcd, S_IWUGO, NULL, enable_lcd_files);

static void interfacekit_irq(struct urb *urb, struct pt_regs *regs)
{
274
	struct interfacekit *kit = urb->context;
L
Linus Torvalds 已提交
275
	unsigned char *buffer = kit->data;
276
	int i, level, sensor;
L
Linus Torvalds 已提交
277 278 279 280 281 282 283 284 285 286 287 288 289 290
	int status;

	switch (urb->status) {
	case 0:			/* success */
		break;
	case -ECONNRESET:	/* unlink */
	case -ENOENT:
	case -ESHUTDOWN:
		return;
	/* -EPIPE:  should clear the halt */
	default:		/* error */
		goto resubmit;
	}

291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
	/* digital inputs */
	if (kit->ifkit->inputs == 16) {
		for (i=0; i < 8; i++) {
			level = (buffer[0] >> i) & 1;
			if (kit->inputs[i] != level) {
				kit->inputs[i] = level;
				set_bit(i, &kit->input_events);
			}
			level = (buffer[1] >> i) & 1;
			if (kit->inputs[8 + i] != level) {
				kit->inputs[8 + i] = level;
				set_bit(8 + i, &kit->input_events);
			}
		}
	}
	else if (kit->ifkit->inputs == 8) {
		for (i=0; i < 8; i++) {
			level = (buffer[1] >> i) & 1;
			if (kit->inputs[i] != level) {
				kit->inputs[i] = level;
				set_bit(i, &kit->input_events);
			}
		}
L
Linus Torvalds 已提交
314 315
	}

316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
	/* analog inputs */
	if (kit->ifkit->sensors) {
		sensor = (buffer[0] & 1) ? 4 : 0;

		level = buffer[2] + (buffer[3] & 0x0f) * 256;
		if (level != kit->sensors[sensor]) {
			kit->sensors[sensor] = level;
			set_bit(sensor, &kit->sensor_events);
		}
		sensor++;
		level = buffer[4] + (buffer[3] & 0xf0) * 16;
		if (level != kit->sensors[sensor]) {
			kit->sensors[sensor] = level;
			set_bit(sensor, &kit->sensor_events);
		}
		sensor++;
		level = buffer[5] + (buffer[6] & 0x0f) * 256;
		if (level != kit->sensors[sensor]) {
			kit->sensors[sensor] = level;
			set_bit(sensor, &kit->sensor_events);
		}
		sensor++;
		level = buffer[7] + (buffer[6] & 0xf0) * 16;
		if (level != kit->sensors[sensor]) {
			kit->sensors[sensor] = level;
			set_bit(sensor, &kit->sensor_events);
		}
L
Linus Torvalds 已提交
343 344
	}

345 346 347
	if (kit->input_events || kit->sensor_events)
		schedule_work(&kit->do_notify);

L
Linus Torvalds 已提交
348 349 350 351 352 353 354 355
resubmit:
	status = usb_submit_urb(urb, SLAB_ATOMIC);
	if (status)
		err("can't resubmit intr, %s-%s/interfacekit0, status %d",
			kit->udev->bus->bus_name,
			kit->udev->devpath, status);
}

356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
static void do_notify(void *data)
{
	struct interfacekit *kit = data;
	int i;
	char sysfs_file[8];

	for (i=0; i<kit->ifkit->inputs; i++) {
		if (test_and_clear_bit(i, &kit->input_events)) {
			sprintf(sysfs_file, "input%d", i + 1);
			sysfs_notify(&kit->intf->dev.kobj, NULL, sysfs_file);
		}
	}

	for (i=0; i<kit->ifkit->sensors; i++) {
		if (test_and_clear_bit(i, &kit->sensor_events)) {
			sprintf(sysfs_file, "sensor%d", i + 1);
			sysfs_notify(&kit->intf->dev.kobj, NULL, sysfs_file);
		}
	}
}

L
Linus Torvalds 已提交
377
#define show_set_output(value)		\
378
static ssize_t set_output##value(struct device *dev, struct device_attribute *attr, const char *buf,	\
L
Linus Torvalds 已提交
379 380 381
							size_t count)	\
{									\
	struct usb_interface *intf = to_usb_interface(dev);		\
382
	struct interfacekit *kit = usb_get_intfdata(intf);		\
L
Linus Torvalds 已提交
383 384 385
	int enabled;							\
	int retval;							\
									\
386
	if (sscanf(buf, "%d", &enabled) < 1)				\
L
Linus Torvalds 已提交
387 388
		return -EINVAL;						\
									\
389
	retval = change_outputs(kit, value - 1, enabled);		\
L
Linus Torvalds 已提交
390 391 392 393
									\
	return retval ? retval : count;					\
}									\
									\
394
static ssize_t show_output##value(struct device *dev, struct device_attribute *attr, char *buf)	\
L
Linus Torvalds 已提交
395 396
{									\
	struct usb_interface *intf = to_usb_interface(dev);		\
397
	struct interfacekit *kit = usb_get_intfdata(intf);		\
L
Linus Torvalds 已提交
398
									\
399
	return sprintf(buf, "%d\n", !!test_bit(value - 1, &kit->outputs));\
L
Linus Torvalds 已提交
400 401 402 403 404 405 406 407 408 409
}									\
static DEVICE_ATTR(output##value, S_IWUGO | S_IRUGO,			\
		show_output##value, set_output##value);
show_set_output(1);
show_set_output(2);
show_set_output(3);
show_set_output(4);
show_set_output(5);
show_set_output(6);
show_set_output(7);
410 411 412 413 414 415 416 417 418
show_set_output(8);
show_set_output(9);
show_set_output(10);
show_set_output(11);
show_set_output(12);
show_set_output(13);
show_set_output(14);
show_set_output(15);
show_set_output(16);
L
Linus Torvalds 已提交
419 420

#define show_input(value)	\
421
static ssize_t show_input##value(struct device *dev, struct device_attribute *attr, char *buf)	\
L
Linus Torvalds 已提交
422 423
{									\
	struct usb_interface *intf = to_usb_interface(dev);		\
424
	struct interfacekit *kit = usb_get_intfdata(intf);		\
L
Linus Torvalds 已提交
425
									\
426
	return sprintf(buf, "%d\n", (int)kit->inputs[value - 1]);	\
L
Linus Torvalds 已提交
427 428 429 430 431 432 433 434 435 436
}									\
static DEVICE_ATTR(input##value, S_IRUGO, show_input##value, NULL);

show_input(1);
show_input(2);
show_input(3);
show_input(4);
show_input(5);
show_input(6);
show_input(7);
437 438 439 440 441 442 443 444 445
show_input(8);
show_input(9);
show_input(10);
show_input(11);
show_input(12);
show_input(13);
show_input(14);
show_input(15);
show_input(16);
L
Linus Torvalds 已提交
446 447

#define show_sensor(value)	\
448
static ssize_t show_sensor##value(struct device *dev, struct device_attribute *attr, char *buf)	\
L
Linus Torvalds 已提交
449 450
{									\
	struct usb_interface *intf = to_usb_interface(dev);		\
451
	struct interfacekit *kit = usb_get_intfdata(intf);		\
L
Linus Torvalds 已提交
452
									\
453
	return sprintf(buf, "%d\n", (int)kit->sensors[value - 1]);	\
L
Linus Torvalds 已提交
454 455 456 457 458 459 460 461 462 463
}									\
static DEVICE_ATTR(sensor##value, S_IRUGO, show_sensor##value, NULL);

show_sensor(1);
show_sensor(2);
show_sensor(3);
show_sensor(4);
show_sensor(5);
show_sensor(6);
show_sensor(7);
464
show_sensor(8);
L
Linus Torvalds 已提交
465 466 467 468 469 470

static int interfacekit_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;
471
	struct interfacekit *kit;
L
Linus Torvalds 已提交
472
	struct driver_interfacekit *ifkit;
473
	int pipe, maxp, rc = -ENOMEM;
L
Linus Torvalds 已提交
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491

	ifkit = (struct driver_interfacekit *)id->driver_info;
	if (!ifkit)
		return -ENODEV;

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

	endpoint = &interface->endpoint[0].desc;
	if (!(endpoint->bEndpointAddress & 0x80)) 
		return -ENODEV;
	/*
	 * bmAttributes
	 */
	pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
	maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
	
492
	kit = kzalloc(sizeof(*kit), GFP_KERNEL);
493 494
	if (!kit)
		goto out;
L
Linus Torvalds 已提交
495

496 497 498 499
	kit->ifkit = ifkit;
	kit->data = usb_buffer_alloc(dev, URB_INT_SIZE, SLAB_ATOMIC, &kit->data_dma);
	if (!kit->data)
		goto out;
L
Linus Torvalds 已提交
500 501

	kit->irq = usb_alloc_urb(0, GFP_KERNEL);
502 503
	if (!kit->irq)
		goto out;
L
Linus Torvalds 已提交
504 505 506

	kit->udev = usb_get_dev(dev);
	kit->intf = intf;
507
	INIT_WORK(&kit->do_notify, do_notify, kit);
L
Linus Torvalds 已提交
508
	usb_fill_int_urb(kit->irq, kit->udev, pipe, kit->data,
509
			maxp > URB_INT_SIZE ? URB_INT_SIZE : maxp,
L
Linus Torvalds 已提交
510 511 512 513 514 515 516
			interfacekit_irq, kit, endpoint->bInterval);
	kit->irq->transfer_dma = kit->data_dma;
	kit->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;

	usb_set_intfdata(intf, kit);

	if (usb_submit_urb(kit->irq, GFP_KERNEL)) {
517 518
		rc = -EIO;
		goto out;
L
Linus Torvalds 已提交
519 520 521 522 523 524 525 526
	}

	if (ifkit->outputs >= 4) {
		device_create_file(&intf->dev, &dev_attr_output1);
		device_create_file(&intf->dev, &dev_attr_output2);
		device_create_file(&intf->dev, &dev_attr_output3);
		device_create_file(&intf->dev, &dev_attr_output4);
	}
527
	if (ifkit->outputs >= 8) {
L
Linus Torvalds 已提交
528 529 530 531 532
		device_create_file(&intf->dev, &dev_attr_output5);
		device_create_file(&intf->dev, &dev_attr_output6);
		device_create_file(&intf->dev, &dev_attr_output7);
		device_create_file(&intf->dev, &dev_attr_output8);
	} 
533 534 535 536 537 538 539 540 541 542
	if (ifkit->outputs == 16) {
		device_create_file(&intf->dev, &dev_attr_output9);
		device_create_file(&intf->dev, &dev_attr_output10);
		device_create_file(&intf->dev, &dev_attr_output11);
		device_create_file(&intf->dev, &dev_attr_output12);
		device_create_file(&intf->dev, &dev_attr_output13);
		device_create_file(&intf->dev, &dev_attr_output14);
		device_create_file(&intf->dev, &dev_attr_output15);
		device_create_file(&intf->dev, &dev_attr_output16);
	}
L
Linus Torvalds 已提交
543 544 545 546 547 548 549

	if (ifkit->inputs >= 4) {
		device_create_file(&intf->dev, &dev_attr_input1);
		device_create_file(&intf->dev, &dev_attr_input2);
		device_create_file(&intf->dev, &dev_attr_input3);
		device_create_file(&intf->dev, &dev_attr_input4);
	}
550
	if (ifkit->inputs >= 8) {
L
Linus Torvalds 已提交
551 552 553 554 555
		device_create_file(&intf->dev, &dev_attr_input5);
		device_create_file(&intf->dev, &dev_attr_input6);
		device_create_file(&intf->dev, &dev_attr_input7);
		device_create_file(&intf->dev, &dev_attr_input8);
	}
556 557 558 559 560 561 562 563 564 565
	if (ifkit->inputs == 16) {
		device_create_file(&intf->dev, &dev_attr_input9);
		device_create_file(&intf->dev, &dev_attr_input10);
		device_create_file(&intf->dev, &dev_attr_input11);
		device_create_file(&intf->dev, &dev_attr_input12);
		device_create_file(&intf->dev, &dev_attr_input13);
		device_create_file(&intf->dev, &dev_attr_input14);
		device_create_file(&intf->dev, &dev_attr_input15);
		device_create_file(&intf->dev, &dev_attr_input16);
	}
L
Linus Torvalds 已提交
566 567 568 569 570 571 572 573 574 575 576 577

	if (ifkit->sensors >= 4) {
		device_create_file(&intf->dev, &dev_attr_sensor1);
		device_create_file(&intf->dev, &dev_attr_sensor2);
		device_create_file(&intf->dev, &dev_attr_sensor3);
		device_create_file(&intf->dev, &dev_attr_sensor4);
	}
	if (ifkit->sensors >= 7) {
		device_create_file(&intf->dev, &dev_attr_sensor5);
		device_create_file(&intf->dev, &dev_attr_sensor6);
		device_create_file(&intf->dev, &dev_attr_sensor7);
	}
578
	if (ifkit->sensors == 8)
L
Linus Torvalds 已提交
579 580 581 582 583 584 585 586 587
		device_create_file(&intf->dev, &dev_attr_sensor8);

	if (ifkit->has_lcd)
		device_create_file(&intf->dev, &dev_attr_lcd);

	dev_info(&intf->dev, "USB PhidgetInterfaceKit %d/%d/%d attached\n",
			ifkit->sensors, ifkit->inputs, ifkit->outputs);

	return 0;
588 589 590 591 592 593 594 595 596 597 598

out:
	if (kit) {
		if (kit->irq)
			usb_free_urb(kit->irq);
		if (kit->data)
			usb_buffer_free(dev, URB_INT_SIZE, kit->data, kit->data_dma);
		kfree(kit);
	}

	return rc;
L
Linus Torvalds 已提交
599 600 601 602
}

static void interfacekit_disconnect(struct usb_interface *interface)
{
603
	struct interfacekit *kit;
L
Linus Torvalds 已提交
604 605 606 607 608 609

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

610 611 612 613 614 615
	usb_kill_urb(kit->irq);
	usb_free_urb(kit->irq);
	usb_buffer_free(kit->udev, URB_INT_SIZE, kit->data, kit->data_dma);

	cancel_delayed_work(&kit->do_notify);

L
Linus Torvalds 已提交
616 617 618 619 620 621
	if (kit->ifkit->outputs >= 4) {
		device_remove_file(&interface->dev, &dev_attr_output1);
		device_remove_file(&interface->dev, &dev_attr_output2);
		device_remove_file(&interface->dev, &dev_attr_output3);
		device_remove_file(&interface->dev, &dev_attr_output4);
	}
622
	if (kit->ifkit->outputs >= 8) {
L
Linus Torvalds 已提交
623 624 625 626 627
		device_remove_file(&interface->dev, &dev_attr_output5);
		device_remove_file(&interface->dev, &dev_attr_output6);
		device_remove_file(&interface->dev, &dev_attr_output7);
		device_remove_file(&interface->dev, &dev_attr_output8);
	}
628 629 630 631 632 633 634 635 636 637
	if (kit->ifkit->outputs == 16) {
		device_remove_file(&interface->dev, &dev_attr_output9);
		device_remove_file(&interface->dev, &dev_attr_output10);
		device_remove_file(&interface->dev, &dev_attr_output11);
		device_remove_file(&interface->dev, &dev_attr_output12);
		device_remove_file(&interface->dev, &dev_attr_output13);
		device_remove_file(&interface->dev, &dev_attr_output14);
		device_remove_file(&interface->dev, &dev_attr_output15);
		device_remove_file(&interface->dev, &dev_attr_output16);
	}
L
Linus Torvalds 已提交
638 639 640 641 642 643 644

	if (kit->ifkit->inputs >= 4) {
		device_remove_file(&interface->dev, &dev_attr_input1);
		device_remove_file(&interface->dev, &dev_attr_input2);
		device_remove_file(&interface->dev, &dev_attr_input3);
		device_remove_file(&interface->dev, &dev_attr_input4);
	}
645
	if (kit->ifkit->inputs >= 8) {
L
Linus Torvalds 已提交
646 647 648 649 650
		device_remove_file(&interface->dev, &dev_attr_input5);
		device_remove_file(&interface->dev, &dev_attr_input6);
		device_remove_file(&interface->dev, &dev_attr_input7);
		device_remove_file(&interface->dev, &dev_attr_input8);
	}
651 652 653 654 655 656 657 658 659 660
	if (kit->ifkit->inputs == 16) {
		device_remove_file(&interface->dev, &dev_attr_input9);
		device_remove_file(&interface->dev, &dev_attr_input10);
		device_remove_file(&interface->dev, &dev_attr_input11);
		device_remove_file(&interface->dev, &dev_attr_input12);
		device_remove_file(&interface->dev, &dev_attr_input13);
		device_remove_file(&interface->dev, &dev_attr_input14);
		device_remove_file(&interface->dev, &dev_attr_input15);
		device_remove_file(&interface->dev, &dev_attr_input16);
	}
L
Linus Torvalds 已提交
661 662 663 664 665 666 667 668 669 670 671 672

	if (kit->ifkit->sensors >= 4) {
		device_remove_file(&interface->dev, &dev_attr_sensor1);
		device_remove_file(&interface->dev, &dev_attr_sensor2);
		device_remove_file(&interface->dev, &dev_attr_sensor3);
		device_remove_file(&interface->dev, &dev_attr_sensor4);
	}
	if (kit->ifkit->sensors >= 7) {
		device_remove_file(&interface->dev, &dev_attr_sensor5);
		device_remove_file(&interface->dev, &dev_attr_sensor6);
		device_remove_file(&interface->dev, &dev_attr_sensor7);
	}
673
	if (kit->ifkit->sensors == 8)
L
Linus Torvalds 已提交
674
		device_remove_file(&interface->dev, &dev_attr_sensor8);
675

L
Linus Torvalds 已提交
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
	if (kit->ifkit->has_lcd)
		device_remove_file(&interface->dev, &dev_attr_lcd);

	dev_info(&interface->dev, "USB PhidgetInterfaceKit %d/%d/%d detached\n",
		kit->ifkit->sensors, kit->ifkit->inputs, kit->ifkit->outputs);

	usb_put_dev(kit->udev);
	kfree(kit);
}

static struct usb_driver interfacekit_driver = {
	.name = "phidgetkit",
	.probe = interfacekit_probe,
	.disconnect = interfacekit_disconnect,
	.id_table = id_table
};

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

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

	return retval;
}

static void __exit interfacekit_exit(void)
{
	usb_deregister(&interfacekit_driver);
}

module_init(interfacekit_init);
module_exit(interfacekit_exit);

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