phidgetkit.c 20 KB

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