phidgetkit.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. /*
  2. * USB PhidgetInterfaceKit driver 1.0
  3. *
  4. * Copyright (C) 2004, 2006 Sean Young <sean@mess.org>
  5. * Copyright (C) 2005 Daniel Saakes <daniel@saakes.net>
  6. * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This is a driver for the USB PhidgetInterfaceKit.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/errno.h>
  17. #include <linux/init.h>
  18. #include <linux/slab.h>
  19. #include <linux/module.h>
  20. #include <linux/usb.h>
  21. #include "phidget.h"
  22. #define DRIVER_AUTHOR "Sean Young <sean@mess.org>"
  23. #define DRIVER_DESC "USB PhidgetInterfaceKit Driver"
  24. #define USB_VENDOR_ID_GLAB 0x06c2
  25. #define USB_DEVICE_ID_INTERFACEKIT004 0x0040
  26. #define USB_DEVICE_ID_INTERFACEKIT01616 0x0044
  27. #define USB_DEVICE_ID_INTERFACEKIT888 0x0045
  28. #define USB_DEVICE_ID_INTERFACEKIT047 0x0051
  29. #define USB_DEVICE_ID_INTERFACEKIT088 0x0053
  30. #define USB_VENDOR_ID_WISEGROUP 0x0925
  31. #define USB_DEVICE_ID_INTERFACEKIT884 0x8201
  32. #define MAX_INTERFACES 16
  33. #define URB_INT_SIZE 8
  34. struct driver_interfacekit {
  35. int sensors;
  36. int inputs;
  37. int outputs;
  38. int has_lcd;
  39. };
  40. #define ifkit(_sensors, _inputs, _outputs, _lcd) \
  41. static struct driver_interfacekit ph_##_sensors##_inputs##_outputs = { \
  42. .sensors = _sensors, \
  43. .inputs = _inputs, \
  44. .outputs = _outputs, \
  45. .has_lcd = _lcd, \
  46. };
  47. ifkit(0, 0, 4, 0);
  48. ifkit(8, 8, 8, 0);
  49. ifkit(0, 4, 7, 1);
  50. ifkit(8, 8, 4, 0);
  51. ifkit(0, 8, 8, 1);
  52. ifkit(0, 16, 16, 0);
  53. static unsigned long device_no;
  54. struct interfacekit {
  55. struct usb_device *udev;
  56. struct usb_interface *intf;
  57. struct driver_interfacekit *ifkit;
  58. struct device *dev;
  59. unsigned long outputs;
  60. int dev_no;
  61. u8 inputs[MAX_INTERFACES];
  62. u16 sensors[MAX_INTERFACES];
  63. u8 lcd_files_on;
  64. struct urb *irq;
  65. unsigned char *data;
  66. dma_addr_t data_dma;
  67. struct work_struct do_notify;
  68. unsigned long input_events;
  69. unsigned long sensor_events;
  70. };
  71. static struct usb_device_id id_table[] = {
  72. {USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_INTERFACEKIT004),
  73. .driver_info = (kernel_ulong_t)&ph_004},
  74. {USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_INTERFACEKIT888),
  75. .driver_info = (kernel_ulong_t)&ph_888},
  76. {USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_INTERFACEKIT047),
  77. .driver_info = (kernel_ulong_t)&ph_047},
  78. {USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_INTERFACEKIT088),
  79. .driver_info = (kernel_ulong_t)&ph_088},
  80. {USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_INTERFACEKIT01616),
  81. .driver_info = (kernel_ulong_t)&ph_01616},
  82. {USB_DEVICE(USB_VENDOR_ID_WISEGROUP, USB_DEVICE_ID_INTERFACEKIT884),
  83. .driver_info = (kernel_ulong_t)&ph_884},
  84. {}
  85. };
  86. MODULE_DEVICE_TABLE(usb, id_table);
  87. static int change_outputs(struct interfacekit *kit, int output_num, int enable)
  88. {
  89. u8 *buffer;
  90. int retval;
  91. if (enable)
  92. set_bit(output_num, &kit->outputs);
  93. else
  94. clear_bit(output_num, &kit->outputs);
  95. buffer = kzalloc(4, GFP_KERNEL);
  96. if (!buffer) {
  97. dev_err(&kit->udev->dev, "%s - out of memory\n", __FUNCTION__);
  98. return -ENOMEM;
  99. }
  100. buffer[0] = (u8)kit->outputs;
  101. buffer[1] = (u8)(kit->outputs >> 8);
  102. dev_dbg(&kit->udev->dev, "sending data: 0x%04x\n", (u16)kit->outputs);
  103. retval = usb_control_msg(kit->udev,
  104. usb_sndctrlpipe(kit->udev, 0),
  105. 0x09, 0x21, 0x0200, 0x0000, buffer, 4, 2000);
  106. if (retval != 4)
  107. dev_err(&kit->udev->dev, "usb_control_msg returned %d\n",
  108. retval);
  109. kfree(buffer);
  110. return retval < 0 ? retval : 0;
  111. }
  112. static int change_string(struct interfacekit *kit, const char *display, unsigned char row)
  113. {
  114. unsigned char *buffer;
  115. unsigned char *form_buffer;
  116. int retval = -ENOMEM;
  117. int i,j, len, buf_ptr;
  118. buffer = kmalloc(8, GFP_KERNEL);
  119. form_buffer = kmalloc(30, GFP_KERNEL);
  120. if ((!buffer) || (!form_buffer)) {
  121. dev_err(&kit->udev->dev, "%s - out of memory\n", __FUNCTION__);
  122. goto exit;
  123. }
  124. len = strlen(display);
  125. if (len > 20)
  126. len = 20;
  127. dev_dbg(&kit->udev->dev, "Setting LCD line %d to %s\n", row, display);
  128. form_buffer[0] = row * 0x40 + 0x80;
  129. form_buffer[1] = 0x02;
  130. buf_ptr = 2;
  131. for (i = 0; i<len; i++)
  132. form_buffer[buf_ptr++] = display[i];
  133. for (i = 0; i < (20 - len); i++)
  134. form_buffer[buf_ptr++] = 0x20;
  135. form_buffer[buf_ptr++] = 0x01;
  136. form_buffer[buf_ptr++] = row * 0x40 + 0x80 + strlen(display);
  137. for (i = 0; i < buf_ptr; i += 7) {
  138. if ((buf_ptr - i) > 7)
  139. len = 7;
  140. else
  141. len = (buf_ptr - i);
  142. for (j = 0; j < len; j++)
  143. buffer[j] = form_buffer[i + j];
  144. buffer[7] = len;
  145. retval = usb_control_msg(kit->udev,
  146. usb_sndctrlpipe(kit->udev, 0),
  147. 0x09, 0x21, 0x0200, 0x0000, buffer, 8, 2000);
  148. if (retval < 0)
  149. goto exit;
  150. }
  151. retval = 0;
  152. exit:
  153. kfree(buffer);
  154. kfree(form_buffer);
  155. return retval;
  156. }
  157. #define set_lcd_line(number) \
  158. static ssize_t lcd_line_##number(struct device *dev, \
  159. struct device_attribute *attr, \
  160. const char *buf, size_t count) \
  161. { \
  162. struct interfacekit *kit = dev_get_drvdata(dev); \
  163. change_string(kit, buf, number - 1); \
  164. return count; \
  165. } \
  166. static DEVICE_ATTR(lcd_line_##number, S_IWUGO, NULL, lcd_line_##number);
  167. set_lcd_line(1);
  168. set_lcd_line(2);
  169. static ssize_t set_backlight(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  170. {
  171. struct interfacekit *kit = dev_get_drvdata(dev);
  172. int enabled;
  173. unsigned char *buffer;
  174. int retval = -ENOMEM;
  175. buffer = kzalloc(8, GFP_KERNEL);
  176. if (!buffer) {
  177. dev_err(&kit->udev->dev, "%s - out of memory\n", __FUNCTION__);
  178. goto exit;
  179. }
  180. if (sscanf(buf, "%d", &enabled) < 1) {
  181. retval = -EINVAL;
  182. goto exit;
  183. }
  184. if (enabled)
  185. buffer[0] = 0x01;
  186. buffer[7] = 0x11;
  187. dev_dbg(&kit->udev->dev, "Setting backlight to %s\n", enabled ? "on" : "off");
  188. retval = usb_control_msg(kit->udev,
  189. usb_sndctrlpipe(kit->udev, 0),
  190. 0x09, 0x21, 0x0200, 0x0000, buffer, 8, 2000);
  191. if (retval < 0)
  192. goto exit;
  193. retval = count;
  194. exit:
  195. kfree(buffer);
  196. return retval;
  197. }
  198. static DEVICE_ATTR(backlight, S_IWUGO, NULL, set_backlight);
  199. static void remove_lcd_files(struct interfacekit *kit)
  200. {
  201. if (kit->lcd_files_on) {
  202. dev_dbg(&kit->udev->dev, "Removing lcd files\n");
  203. device_remove_file(kit->dev, &dev_attr_lcd_line_1);
  204. device_remove_file(kit->dev, &dev_attr_lcd_line_2);
  205. device_remove_file(kit->dev, &dev_attr_backlight);
  206. }
  207. }
  208. static ssize_t enable_lcd_files(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  209. {
  210. struct interfacekit *kit = dev_get_drvdata(dev);
  211. int enable;
  212. if (kit->ifkit->has_lcd == 0)
  213. return -ENODEV;
  214. if (sscanf(buf, "%d", &enable) < 1)
  215. return -EINVAL;
  216. if (enable) {
  217. if (!kit->lcd_files_on) {
  218. dev_dbg(&kit->udev->dev, "Adding lcd files\n");
  219. device_create_file(kit->dev, &dev_attr_lcd_line_1);
  220. device_create_file(kit->dev, &dev_attr_lcd_line_2);
  221. device_create_file(kit->dev, &dev_attr_backlight);
  222. kit->lcd_files_on = 1;
  223. }
  224. } else {
  225. if (kit->lcd_files_on) {
  226. remove_lcd_files(kit);
  227. kit->lcd_files_on = 0;
  228. }
  229. }
  230. return count;
  231. }
  232. static DEVICE_ATTR(lcd, S_IWUGO, NULL, enable_lcd_files);
  233. static void interfacekit_irq(struct urb *urb, struct pt_regs *regs)
  234. {
  235. struct interfacekit *kit = urb->context;
  236. unsigned char *buffer = kit->data;
  237. int i, level, sensor;
  238. int status;
  239. switch (urb->status) {
  240. case 0: /* success */
  241. break;
  242. case -ECONNRESET: /* unlink */
  243. case -ENOENT:
  244. case -ESHUTDOWN:
  245. return;
  246. /* -EPIPE: should clear the halt */
  247. default: /* error */
  248. goto resubmit;
  249. }
  250. /* digital inputs */
  251. if (kit->ifkit->inputs == 16) {
  252. for (i=0; i < 8; i++) {
  253. level = (buffer[0] >> i) & 1;
  254. if (kit->inputs[i] != level) {
  255. kit->inputs[i] = level;
  256. set_bit(i, &kit->input_events);
  257. }
  258. level = (buffer[1] >> i) & 1;
  259. if (kit->inputs[8 + i] != level) {
  260. kit->inputs[8 + i] = level;
  261. set_bit(8 + i, &kit->input_events);
  262. }
  263. }
  264. }
  265. else if (kit->ifkit->inputs == 8) {
  266. for (i=0; i < 8; i++) {
  267. level = (buffer[1] >> i) & 1;
  268. if (kit->inputs[i] != level) {
  269. kit->inputs[i] = level;
  270. set_bit(i, &kit->input_events);
  271. }
  272. }
  273. }
  274. /* analog inputs */
  275. if (kit->ifkit->sensors) {
  276. sensor = (buffer[0] & 1) ? 4 : 0;
  277. level = buffer[2] + (buffer[3] & 0x0f) * 256;
  278. if (level != kit->sensors[sensor]) {
  279. kit->sensors[sensor] = level;
  280. set_bit(sensor, &kit->sensor_events);
  281. }
  282. sensor++;
  283. level = buffer[4] + (buffer[3] & 0xf0) * 16;
  284. if (level != kit->sensors[sensor]) {
  285. kit->sensors[sensor] = level;
  286. set_bit(sensor, &kit->sensor_events);
  287. }
  288. sensor++;
  289. level = buffer[5] + (buffer[6] & 0x0f) * 256;
  290. if (level != kit->sensors[sensor]) {
  291. kit->sensors[sensor] = level;
  292. set_bit(sensor, &kit->sensor_events);
  293. }
  294. sensor++;
  295. level = buffer[7] + (buffer[6] & 0xf0) * 16;
  296. if (level != kit->sensors[sensor]) {
  297. kit->sensors[sensor] = level;
  298. set_bit(sensor, &kit->sensor_events);
  299. }
  300. }
  301. if (kit->input_events || kit->sensor_events)
  302. schedule_work(&kit->do_notify);
  303. resubmit:
  304. status = usb_submit_urb(urb, SLAB_ATOMIC);
  305. if (status)
  306. err("can't resubmit intr, %s-%s/interfacekit0, status %d",
  307. kit->udev->bus->bus_name,
  308. kit->udev->devpath, status);
  309. }
  310. static void do_notify(void *data)
  311. {
  312. struct interfacekit *kit = data;
  313. int i;
  314. char sysfs_file[8];
  315. for (i=0; i<kit->ifkit->inputs; i++) {
  316. if (test_and_clear_bit(i, &kit->input_events)) {
  317. sprintf(sysfs_file, "input%d", i + 1);
  318. sysfs_notify(&kit->dev->kobj, NULL, sysfs_file);
  319. }
  320. }
  321. for (i=0; i<kit->ifkit->sensors; i++) {
  322. if (test_and_clear_bit(i, &kit->sensor_events)) {
  323. sprintf(sysfs_file, "sensor%d", i + 1);
  324. sysfs_notify(&kit->dev->kobj, NULL, sysfs_file);
  325. }
  326. }
  327. }
  328. #define show_set_output(value) \
  329. static ssize_t set_output##value(struct device *dev, \
  330. struct device_attribute *attr, \
  331. const char *buf, size_t count) \
  332. { \
  333. struct interfacekit *kit = dev_get_drvdata(dev); \
  334. int enabled; \
  335. int retval; \
  336. \
  337. if (sscanf(buf, "%d", &enabled) < 1) \
  338. return -EINVAL; \
  339. \
  340. retval = change_outputs(kit, value - 1, enabled); \
  341. \
  342. return retval ? retval : count; \
  343. } \
  344. \
  345. static ssize_t show_output##value(struct device *dev, \
  346. struct device_attribute *attr, \
  347. char *buf) \
  348. { \
  349. struct interfacekit *kit = dev_get_drvdata(dev); \
  350. \
  351. return sprintf(buf, "%d\n", !!test_bit(value - 1, &kit->outputs));\
  352. } \
  353. static DEVICE_ATTR(output##value, S_IWUGO | S_IRUGO, \
  354. show_output##value, set_output##value);
  355. show_set_output(1);
  356. show_set_output(2);
  357. show_set_output(3);
  358. show_set_output(4);
  359. show_set_output(5);
  360. show_set_output(6);
  361. show_set_output(7);
  362. show_set_output(8);
  363. show_set_output(9);
  364. show_set_output(10);
  365. show_set_output(11);
  366. show_set_output(12);
  367. show_set_output(13);
  368. show_set_output(14);
  369. show_set_output(15);
  370. show_set_output(16);
  371. #define show_input(value) \
  372. static ssize_t show_input##value(struct device *dev, struct device_attribute *attr, char *buf) \
  373. { \
  374. struct interfacekit *kit = dev_get_drvdata(dev); \
  375. \
  376. return sprintf(buf, "%d\n", (int)kit->inputs[value - 1]); \
  377. } \
  378. static DEVICE_ATTR(input##value, S_IRUGO, show_input##value, NULL);
  379. show_input(1);
  380. show_input(2);
  381. show_input(3);
  382. show_input(4);
  383. show_input(5);
  384. show_input(6);
  385. show_input(7);
  386. show_input(8);
  387. show_input(9);
  388. show_input(10);
  389. show_input(11);
  390. show_input(12);
  391. show_input(13);
  392. show_input(14);
  393. show_input(15);
  394. show_input(16);
  395. #define show_sensor(value) \
  396. static ssize_t show_sensor##value(struct device *dev, \
  397. struct device_attribute *attr, \
  398. char *buf) \
  399. { \
  400. struct interfacekit *kit = dev_get_drvdata(dev); \
  401. \
  402. return sprintf(buf, "%d\n", (int)kit->sensors[value - 1]); \
  403. } \
  404. static DEVICE_ATTR(sensor##value, S_IRUGO, show_sensor##value, NULL);
  405. show_sensor(1);
  406. show_sensor(2);
  407. show_sensor(3);
  408. show_sensor(4);
  409. show_sensor(5);
  410. show_sensor(6);
  411. show_sensor(7);
  412. show_sensor(8);
  413. static int interfacekit_probe(struct usb_interface *intf, const struct usb_device_id *id)
  414. {
  415. struct usb_device *dev = interface_to_usbdev(intf);
  416. struct usb_host_interface *interface;
  417. struct usb_endpoint_descriptor *endpoint;
  418. struct interfacekit *kit;
  419. struct driver_interfacekit *ifkit;
  420. int pipe, maxp, rc = -ENOMEM;
  421. int bit, value;
  422. ifkit = (struct driver_interfacekit *)id->driver_info;
  423. if (!ifkit)
  424. return -ENODEV;
  425. interface = intf->cur_altsetting;
  426. if (interface->desc.bNumEndpoints != 1)
  427. return -ENODEV;
  428. endpoint = &interface->endpoint[0].desc;
  429. if (!(endpoint->bEndpointAddress & 0x80))
  430. return -ENODEV;
  431. /*
  432. * bmAttributes
  433. */
  434. pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
  435. maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
  436. kit = kzalloc(sizeof(*kit), GFP_KERNEL);
  437. if (!kit)
  438. goto out;
  439. kit->dev_no = -1;
  440. kit->ifkit = ifkit;
  441. kit->data = usb_buffer_alloc(dev, URB_INT_SIZE, SLAB_ATOMIC, &kit->data_dma);
  442. if (!kit->data)
  443. goto out;
  444. kit->irq = usb_alloc_urb(0, GFP_KERNEL);
  445. if (!kit->irq)
  446. goto out;
  447. kit->udev = usb_get_dev(dev);
  448. kit->intf = intf;
  449. INIT_WORK(&kit->do_notify, do_notify, kit);
  450. usb_fill_int_urb(kit->irq, kit->udev, pipe, kit->data,
  451. maxp > URB_INT_SIZE ? URB_INT_SIZE : maxp,
  452. interfacekit_irq, kit, endpoint->bInterval);
  453. kit->irq->transfer_dma = kit->data_dma;
  454. kit->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  455. usb_set_intfdata(intf, kit);
  456. do {
  457. bit = find_first_zero_bit(&device_no, sizeof(device_no));
  458. value = test_and_set_bit(bit, &device_no);
  459. } while(value);
  460. kit->dev_no = bit;
  461. kit->dev = device_create(phidget_class, &kit->udev->dev, 0,
  462. "interfacekit%d", kit->dev_no);
  463. if (IS_ERR(kit->dev)) {
  464. rc = PTR_ERR(kit->dev);
  465. kit->dev = NULL;
  466. goto out;
  467. }
  468. dev_set_drvdata(kit->dev, kit);
  469. if (usb_submit_urb(kit->irq, GFP_KERNEL)) {
  470. rc = -EIO;
  471. goto out;
  472. }
  473. if (ifkit->outputs >= 4) {
  474. device_create_file(kit->dev, &dev_attr_output1);
  475. device_create_file(kit->dev, &dev_attr_output2);
  476. device_create_file(kit->dev, &dev_attr_output3);
  477. device_create_file(kit->dev, &dev_attr_output4);
  478. }
  479. if (ifkit->outputs >= 8) {
  480. device_create_file(kit->dev, &dev_attr_output5);
  481. device_create_file(kit->dev, &dev_attr_output6);
  482. device_create_file(kit->dev, &dev_attr_output7);
  483. device_create_file(kit->dev, &dev_attr_output8);
  484. }
  485. if (ifkit->outputs == 16) {
  486. device_create_file(kit->dev, &dev_attr_output9);
  487. device_create_file(kit->dev, &dev_attr_output10);
  488. device_create_file(kit->dev, &dev_attr_output11);
  489. device_create_file(kit->dev, &dev_attr_output12);
  490. device_create_file(kit->dev, &dev_attr_output13);
  491. device_create_file(kit->dev, &dev_attr_output14);
  492. device_create_file(kit->dev, &dev_attr_output15);
  493. device_create_file(kit->dev, &dev_attr_output16);
  494. }
  495. if (ifkit->inputs >= 4) {
  496. device_create_file(kit->dev, &dev_attr_input1);
  497. device_create_file(kit->dev, &dev_attr_input2);
  498. device_create_file(kit->dev, &dev_attr_input3);
  499. device_create_file(kit->dev, &dev_attr_input4);
  500. }
  501. if (ifkit->inputs >= 8) {
  502. device_create_file(kit->dev, &dev_attr_input5);
  503. device_create_file(kit->dev, &dev_attr_input6);
  504. device_create_file(kit->dev, &dev_attr_input7);
  505. device_create_file(kit->dev, &dev_attr_input8);
  506. }
  507. if (ifkit->inputs == 16) {
  508. device_create_file(kit->dev, &dev_attr_input9);
  509. device_create_file(kit->dev, &dev_attr_input10);
  510. device_create_file(kit->dev, &dev_attr_input11);
  511. device_create_file(kit->dev, &dev_attr_input12);
  512. device_create_file(kit->dev, &dev_attr_input13);
  513. device_create_file(kit->dev, &dev_attr_input14);
  514. device_create_file(kit->dev, &dev_attr_input15);
  515. device_create_file(kit->dev, &dev_attr_input16);
  516. }
  517. if (ifkit->sensors >= 4) {
  518. device_create_file(kit->dev, &dev_attr_sensor1);
  519. device_create_file(kit->dev, &dev_attr_sensor2);
  520. device_create_file(kit->dev, &dev_attr_sensor3);
  521. device_create_file(kit->dev, &dev_attr_sensor4);
  522. }
  523. if (ifkit->sensors >= 7) {
  524. device_create_file(kit->dev, &dev_attr_sensor5);
  525. device_create_file(kit->dev, &dev_attr_sensor6);
  526. device_create_file(kit->dev, &dev_attr_sensor7);
  527. }
  528. if (ifkit->sensors == 8)
  529. device_create_file(kit->dev, &dev_attr_sensor8);
  530. if (ifkit->has_lcd)
  531. device_create_file(kit->dev, &dev_attr_lcd);
  532. dev_info(&intf->dev, "USB PhidgetInterfaceKit %d/%d/%d attached\n",
  533. ifkit->sensors, ifkit->inputs, ifkit->outputs);
  534. return 0;
  535. out:
  536. if (kit) {
  537. if (kit->irq)
  538. usb_free_urb(kit->irq);
  539. if (kit->data)
  540. usb_buffer_free(dev, URB_INT_SIZE, kit->data, kit->data_dma);
  541. if (kit->dev)
  542. device_unregister(kit->dev);
  543. if (kit->dev_no >= 0)
  544. clear_bit(kit->dev_no, &device_no);
  545. kfree(kit);
  546. }
  547. return rc;
  548. }
  549. static void interfacekit_disconnect(struct usb_interface *interface)
  550. {
  551. struct interfacekit *kit;
  552. kit = usb_get_intfdata(interface);
  553. usb_set_intfdata(interface, NULL);
  554. if (!kit)
  555. return;
  556. usb_kill_urb(kit->irq);
  557. usb_free_urb(kit->irq);
  558. usb_buffer_free(kit->udev, URB_INT_SIZE, kit->data, kit->data_dma);
  559. cancel_delayed_work(&kit->do_notify);
  560. if (kit->ifkit->outputs >= 4) {
  561. device_remove_file(kit->dev, &dev_attr_output1);
  562. device_remove_file(kit->dev, &dev_attr_output2);
  563. device_remove_file(kit->dev, &dev_attr_output3);
  564. device_remove_file(kit->dev, &dev_attr_output4);
  565. }
  566. if (kit->ifkit->outputs >= 8) {
  567. device_remove_file(kit->dev, &dev_attr_output5);
  568. device_remove_file(kit->dev, &dev_attr_output6);
  569. device_remove_file(kit->dev, &dev_attr_output7);
  570. device_remove_file(kit->dev, &dev_attr_output8);
  571. }
  572. if (kit->ifkit->outputs == 16) {
  573. device_remove_file(kit->dev, &dev_attr_output9);
  574. device_remove_file(kit->dev, &dev_attr_output10);
  575. device_remove_file(kit->dev, &dev_attr_output11);
  576. device_remove_file(kit->dev, &dev_attr_output12);
  577. device_remove_file(kit->dev, &dev_attr_output13);
  578. device_remove_file(kit->dev, &dev_attr_output14);
  579. device_remove_file(kit->dev, &dev_attr_output15);
  580. device_remove_file(kit->dev, &dev_attr_output16);
  581. }
  582. if (kit->ifkit->inputs >= 4) {
  583. device_remove_file(kit->dev, &dev_attr_input1);
  584. device_remove_file(kit->dev, &dev_attr_input2);
  585. device_remove_file(kit->dev, &dev_attr_input3);
  586. device_remove_file(kit->dev, &dev_attr_input4);
  587. }
  588. if (kit->ifkit->inputs >= 8) {
  589. device_remove_file(kit->dev, &dev_attr_input5);
  590. device_remove_file(kit->dev, &dev_attr_input6);
  591. device_remove_file(kit->dev, &dev_attr_input7);
  592. device_remove_file(kit->dev, &dev_attr_input8);
  593. }
  594. if (kit->ifkit->inputs == 16) {
  595. device_remove_file(kit->dev, &dev_attr_input9);
  596. device_remove_file(kit->dev, &dev_attr_input10);
  597. device_remove_file(kit->dev, &dev_attr_input11);
  598. device_remove_file(kit->dev, &dev_attr_input12);
  599. device_remove_file(kit->dev, &dev_attr_input13);
  600. device_remove_file(kit->dev, &dev_attr_input14);
  601. device_remove_file(kit->dev, &dev_attr_input15);
  602. device_remove_file(kit->dev, &dev_attr_input16);
  603. }
  604. if (kit->ifkit->sensors >= 4) {
  605. device_remove_file(kit->dev, &dev_attr_sensor1);
  606. device_remove_file(kit->dev, &dev_attr_sensor2);
  607. device_remove_file(kit->dev, &dev_attr_sensor3);
  608. device_remove_file(kit->dev, &dev_attr_sensor4);
  609. }
  610. if (kit->ifkit->sensors >= 7) {
  611. device_remove_file(kit->dev, &dev_attr_sensor5);
  612. device_remove_file(kit->dev, &dev_attr_sensor6);
  613. device_remove_file(kit->dev, &dev_attr_sensor7);
  614. }
  615. if (kit->ifkit->sensors == 8)
  616. device_remove_file(kit->dev, &dev_attr_sensor8);
  617. if (kit->ifkit->has_lcd)
  618. device_remove_file(kit->dev, &dev_attr_lcd);
  619. device_unregister(kit->dev);
  620. dev_info(&interface->dev, "USB PhidgetInterfaceKit %d/%d/%d detached\n",
  621. kit->ifkit->sensors, kit->ifkit->inputs, kit->ifkit->outputs);
  622. usb_put_dev(kit->udev);
  623. clear_bit(kit->dev_no, &device_no);
  624. kfree(kit);
  625. }
  626. static struct usb_driver interfacekit_driver = {
  627. .name = "phidgetkit",
  628. .probe = interfacekit_probe,
  629. .disconnect = interfacekit_disconnect,
  630. .id_table = id_table
  631. };
  632. static int __init interfacekit_init(void)
  633. {
  634. int retval = 0;
  635. retval = usb_register(&interfacekit_driver);
  636. if (retval)
  637. err("usb_register failed. Error number %d", retval);
  638. return retval;
  639. }
  640. static void __exit interfacekit_exit(void)
  641. {
  642. usb_deregister(&interfacekit_driver);
  643. }
  644. module_init(interfacekit_init);
  645. module_exit(interfacekit_exit);
  646. MODULE_AUTHOR(DRIVER_AUTHOR);
  647. MODULE_DESCRIPTION(DRIVER_DESC);
  648. MODULE_LICENSE("GPL");