phidgetmotorcontrol.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. /*
  2. * USB Phidget MotorControl driver
  3. *
  4. * Copyright (C) 2006 Sean Young <sean@mess.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/errno.h>
  13. #include <linux/init.h>
  14. #include <linux/module.h>
  15. #include <linux/usb.h>
  16. #include "phidget.h"
  17. #define DRIVER_AUTHOR "Sean Young <sean@mess.org>"
  18. #define DRIVER_DESC "USB PhidgetMotorControl Driver"
  19. #define USB_VENDOR_ID_GLAB 0x06c2
  20. #define USB_DEVICE_ID_MOTORCONTROL 0x0058
  21. #define URB_INT_SIZE 8
  22. static unsigned long device_no;
  23. struct motorcontrol {
  24. struct usb_device *udev;
  25. struct usb_interface *intf;
  26. struct device *dev;
  27. int dev_no;
  28. u8 inputs[4];
  29. s8 desired_speed[2];
  30. s8 speed[2];
  31. s16 _current[2];
  32. s8 acceleration[2];
  33. struct urb *irq;
  34. unsigned char *data;
  35. dma_addr_t data_dma;
  36. struct delayed_work do_notify;
  37. unsigned long input_events;
  38. unsigned long speed_events;
  39. unsigned long exceed_events;
  40. };
  41. static struct usb_device_id id_table[] = {
  42. { USB_DEVICE(USB_VENDOR_ID_GLAB, USB_DEVICE_ID_MOTORCONTROL) },
  43. {}
  44. };
  45. MODULE_DEVICE_TABLE(usb, id_table);
  46. static int set_motor(struct motorcontrol *mc, int motor)
  47. {
  48. u8 *buffer;
  49. int speed, speed2, acceleration;
  50. int retval;
  51. buffer = kzalloc(8, GFP_KERNEL);
  52. if (!buffer) {
  53. dev_err(&mc->intf->dev, "%s - out of memory\n", __func__);
  54. return -ENOMEM;
  55. }
  56. acceleration = mc->acceleration[motor] * 10;
  57. /* -127 <= speed <= 127 */
  58. speed = (mc->desired_speed[motor] * 127) / 100;
  59. /* -0x7300 <= speed2 <= 0x7300 */
  60. speed2 = (mc->desired_speed[motor] * 230 * 128) / 100;
  61. buffer[0] = motor;
  62. buffer[1] = speed;
  63. buffer[2] = acceleration >> 8;
  64. buffer[3] = acceleration;
  65. buffer[4] = speed2 >> 8;
  66. buffer[5] = speed2;
  67. retval = usb_control_msg(mc->udev,
  68. usb_sndctrlpipe(mc->udev, 0),
  69. 0x09, 0x21, 0x0200, 0x0000, buffer, 8, 2000);
  70. if (retval != 8)
  71. dev_err(&mc->intf->dev, "usb_control_msg returned %d\n",
  72. retval);
  73. kfree(buffer);
  74. return retval < 0 ? retval : 0;
  75. }
  76. static void motorcontrol_irq(struct urb *urb)
  77. {
  78. struct motorcontrol *mc = urb->context;
  79. unsigned char *buffer = mc->data;
  80. int i, level;
  81. int retval;
  82. int status = urb->status;;
  83. switch (status) {
  84. case 0: /* success */
  85. break;
  86. case -ECONNRESET: /* unlink */
  87. case -ENOENT:
  88. case -ESHUTDOWN:
  89. return;
  90. /* -EPIPE: should clear the halt */
  91. default: /* error */
  92. goto resubmit;
  93. }
  94. /* digital inputs */
  95. for (i=0; i<4; i++) {
  96. level = (buffer[0] >> i) & 1;
  97. if (mc->inputs[i] != level) {
  98. mc->inputs[i] = level;
  99. set_bit(i, &mc->input_events);
  100. }
  101. }
  102. /* motor speed */
  103. if (buffer[2] == 0) {
  104. for (i=0; i<2; i++) {
  105. level = ((s8)buffer[4+i]) * 100 / 127;
  106. if (mc->speed[i] != level) {
  107. mc->speed[i] = level;
  108. set_bit(i, &mc->speed_events);
  109. }
  110. }
  111. } else {
  112. int index = buffer[3] & 1;
  113. level = ((s8)buffer[4] << 8) | buffer[5];
  114. level = level * 100 / 29440;
  115. if (mc->speed[index] != level) {
  116. mc->speed[index] = level;
  117. set_bit(index, &mc->speed_events);
  118. }
  119. level = ((s8)buffer[6] << 8) | buffer[7];
  120. mc->_current[index] = level * 100 / 1572;
  121. }
  122. if (buffer[1] & 1)
  123. set_bit(0, &mc->exceed_events);
  124. if (buffer[1] & 2)
  125. set_bit(1, &mc->exceed_events);
  126. if (mc->input_events || mc->exceed_events || mc->speed_events)
  127. schedule_delayed_work(&mc->do_notify, 0);
  128. resubmit:
  129. retval = usb_submit_urb(urb, GFP_ATOMIC);
  130. if (retval)
  131. dev_err(&mc->intf->dev,
  132. "can't resubmit intr, %s-%s/motorcontrol0, retval %d\n",
  133. mc->udev->bus->bus_name,
  134. mc->udev->devpath, retval);
  135. }
  136. static void do_notify(struct work_struct *work)
  137. {
  138. struct motorcontrol *mc =
  139. container_of(work, struct motorcontrol, do_notify.work);
  140. int i;
  141. char sysfs_file[8];
  142. for (i=0; i<4; i++) {
  143. if (test_and_clear_bit(i, &mc->input_events)) {
  144. sprintf(sysfs_file, "input%d", i);
  145. sysfs_notify(&mc->dev->kobj, NULL, sysfs_file);
  146. }
  147. }
  148. for (i=0; i<2; i++) {
  149. if (test_and_clear_bit(i, &mc->speed_events)) {
  150. sprintf(sysfs_file, "speed%d", i);
  151. sysfs_notify(&mc->dev->kobj, NULL, sysfs_file);
  152. }
  153. }
  154. for (i=0; i<2; i++) {
  155. if (test_and_clear_bit(i, &mc->exceed_events))
  156. dev_warn(&mc->intf->dev,
  157. "motor #%d exceeds 1.5 Amp current limit\n", i);
  158. }
  159. }
  160. #define show_set_speed(value) \
  161. static ssize_t set_speed##value(struct device *dev, \
  162. struct device_attribute *attr, \
  163. const char *buf, size_t count) \
  164. { \
  165. struct motorcontrol *mc = dev_get_drvdata(dev); \
  166. int speed; \
  167. int retval; \
  168. \
  169. if (sscanf(buf, "%d", &speed) < 1) \
  170. return -EINVAL; \
  171. \
  172. if (speed < -100 || speed > 100) \
  173. return -EINVAL; \
  174. \
  175. mc->desired_speed[value] = speed; \
  176. \
  177. retval = set_motor(mc, value); \
  178. \
  179. return retval ? retval : count; \
  180. } \
  181. \
  182. static ssize_t show_speed##value(struct device *dev, \
  183. struct device_attribute *attr, \
  184. char *buf) \
  185. { \
  186. struct motorcontrol *mc = dev_get_drvdata(dev); \
  187. \
  188. return sprintf(buf, "%d\n", mc->speed[value]); \
  189. }
  190. #define speed_attr(value) \
  191. __ATTR(speed##value, S_IWUGO | S_IRUGO, \
  192. show_speed##value, set_speed##value)
  193. show_set_speed(0);
  194. show_set_speed(1);
  195. #define show_set_acceleration(value) \
  196. static ssize_t set_acceleration##value(struct device *dev, \
  197. struct device_attribute *attr, \
  198. const char *buf, size_t count) \
  199. { \
  200. struct motorcontrol *mc = dev_get_drvdata(dev); \
  201. int acceleration; \
  202. int retval; \
  203. \
  204. if (sscanf(buf, "%d", &acceleration) < 1) \
  205. return -EINVAL; \
  206. \
  207. if (acceleration < 0 || acceleration > 100) \
  208. return -EINVAL; \
  209. \
  210. mc->acceleration[value] = acceleration; \
  211. \
  212. retval = set_motor(mc, value); \
  213. \
  214. return retval ? retval : count; \
  215. } \
  216. \
  217. static ssize_t show_acceleration##value(struct device *dev, \
  218. struct device_attribute *attr, \
  219. char *buf) \
  220. { \
  221. struct motorcontrol *mc = dev_get_drvdata(dev); \
  222. \
  223. return sprintf(buf, "%d\n", mc->acceleration[value]); \
  224. }
  225. #define acceleration_attr(value) \
  226. __ATTR(acceleration##value, S_IWUGO | S_IRUGO, \
  227. show_acceleration##value, set_acceleration##value)
  228. show_set_acceleration(0);
  229. show_set_acceleration(1);
  230. #define show_current(value) \
  231. static ssize_t show_current##value(struct device *dev, \
  232. struct device_attribute *attr, \
  233. char *buf) \
  234. { \
  235. struct motorcontrol *mc = dev_get_drvdata(dev); \
  236. \
  237. return sprintf(buf, "%dmA\n", (int)mc->_current[value]); \
  238. }
  239. #define current_attr(value) \
  240. __ATTR(current##value, S_IRUGO, show_current##value, NULL)
  241. show_current(0);
  242. show_current(1);
  243. #define show_input(value) \
  244. static ssize_t show_input##value(struct device *dev, \
  245. struct device_attribute *attr, \
  246. char *buf) \
  247. { \
  248. struct motorcontrol *mc = dev_get_drvdata(dev); \
  249. \
  250. return sprintf(buf, "%d\n", (int)mc->inputs[value]); \
  251. }
  252. #define input_attr(value) \
  253. __ATTR(input##value, S_IRUGO, show_input##value, NULL)
  254. show_input(0);
  255. show_input(1);
  256. show_input(2);
  257. show_input(3);
  258. static struct device_attribute dev_attrs[] = {
  259. input_attr(0),
  260. input_attr(1),
  261. input_attr(2),
  262. input_attr(3),
  263. speed_attr(0),
  264. speed_attr(1),
  265. acceleration_attr(0),
  266. acceleration_attr(1),
  267. current_attr(0),
  268. current_attr(1)
  269. };
  270. static int motorcontrol_probe(struct usb_interface *intf, const struct usb_device_id *id)
  271. {
  272. struct usb_device *dev = interface_to_usbdev(intf);
  273. struct usb_host_interface *interface;
  274. struct usb_endpoint_descriptor *endpoint;
  275. struct motorcontrol *mc;
  276. int pipe, maxp, rc = -ENOMEM;
  277. int bit, value, i;
  278. interface = intf->cur_altsetting;
  279. if (interface->desc.bNumEndpoints != 1)
  280. return -ENODEV;
  281. endpoint = &interface->endpoint[0].desc;
  282. if (!usb_endpoint_dir_in(endpoint))
  283. return -ENODEV;
  284. /*
  285. * bmAttributes
  286. */
  287. pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
  288. maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
  289. mc = kzalloc(sizeof(*mc), GFP_KERNEL);
  290. if (!mc)
  291. goto out;
  292. mc->dev_no = -1;
  293. mc->data = usb_buffer_alloc(dev, URB_INT_SIZE, GFP_ATOMIC, &mc->data_dma);
  294. if (!mc->data)
  295. goto out;
  296. mc->irq = usb_alloc_urb(0, GFP_KERNEL);
  297. if (!mc->irq)
  298. goto out;
  299. mc->udev = usb_get_dev(dev);
  300. mc->intf = intf;
  301. mc->acceleration[0] = mc->acceleration[1] = 10;
  302. INIT_DELAYED_WORK(&mc->do_notify, do_notify);
  303. usb_fill_int_urb(mc->irq, mc->udev, pipe, mc->data,
  304. maxp > URB_INT_SIZE ? URB_INT_SIZE : maxp,
  305. motorcontrol_irq, mc, endpoint->bInterval);
  306. mc->irq->transfer_dma = mc->data_dma;
  307. mc->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  308. usb_set_intfdata(intf, mc);
  309. do {
  310. bit = find_first_zero_bit(&device_no, sizeof(device_no));
  311. value = test_and_set_bit(bit, &device_no);
  312. } while(value);
  313. mc->dev_no = bit;
  314. mc->dev = device_create_drvdata(phidget_class, &mc->udev->dev,
  315. MKDEV(0, 0), mc,
  316. "motorcontrol%d", mc->dev_no);
  317. if (IS_ERR(mc->dev)) {
  318. rc = PTR_ERR(mc->dev);
  319. mc->dev = NULL;
  320. goto out;
  321. }
  322. if (usb_submit_urb(mc->irq, GFP_KERNEL)) {
  323. rc = -EIO;
  324. goto out;
  325. }
  326. for (i=0; i<ARRAY_SIZE(dev_attrs); i++) {
  327. rc = device_create_file(mc->dev, &dev_attrs[i]);
  328. if (rc)
  329. goto out2;
  330. }
  331. dev_info(&intf->dev, "USB PhidgetMotorControl attached\n");
  332. return 0;
  333. out2:
  334. while (i-- > 0)
  335. device_remove_file(mc->dev, &dev_attrs[i]);
  336. out:
  337. if (mc) {
  338. usb_free_urb(mc->irq);
  339. if (mc->data)
  340. usb_buffer_free(dev, URB_INT_SIZE, mc->data, mc->data_dma);
  341. if (mc->dev)
  342. device_unregister(mc->dev);
  343. if (mc->dev_no >= 0)
  344. clear_bit(mc->dev_no, &device_no);
  345. kfree(mc);
  346. }
  347. return rc;
  348. }
  349. static void motorcontrol_disconnect(struct usb_interface *interface)
  350. {
  351. struct motorcontrol *mc;
  352. int i;
  353. mc = usb_get_intfdata(interface);
  354. usb_set_intfdata(interface, NULL);
  355. if (!mc)
  356. return;
  357. usb_kill_urb(mc->irq);
  358. usb_free_urb(mc->irq);
  359. usb_buffer_free(mc->udev, URB_INT_SIZE, mc->data, mc->data_dma);
  360. cancel_delayed_work(&mc->do_notify);
  361. for (i=0; i<ARRAY_SIZE(dev_attrs); i++)
  362. device_remove_file(mc->dev, &dev_attrs[i]);
  363. device_unregister(mc->dev);
  364. usb_put_dev(mc->udev);
  365. clear_bit(mc->dev_no, &device_no);
  366. kfree(mc);
  367. dev_info(&interface->dev, "USB PhidgetMotorControl detached\n");
  368. }
  369. static struct usb_driver motorcontrol_driver = {
  370. .name = "phidgetmotorcontrol",
  371. .probe = motorcontrol_probe,
  372. .disconnect = motorcontrol_disconnect,
  373. .id_table = id_table
  374. };
  375. static int __init motorcontrol_init(void)
  376. {
  377. int retval = 0;
  378. retval = usb_register(&motorcontrol_driver);
  379. if (retval)
  380. err("usb_register failed. Error number %d", retval);
  381. return retval;
  382. }
  383. static void __exit motorcontrol_exit(void)
  384. {
  385. usb_deregister(&motorcontrol_driver);
  386. }
  387. module_init(motorcontrol_init);
  388. module_exit(motorcontrol_exit);
  389. MODULE_AUTHOR(DRIVER_AUTHOR);
  390. MODULE_DESCRIPTION(DRIVER_DESC);
  391. MODULE_LICENSE("GPL");