phidgetkit.c 20 KB

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