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", __FUNCTION__);
  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 status;
  82. switch (urb->status) {
  83. case 0: /* success */
  84. break;
  85. case -ECONNRESET: /* unlink */
  86. case -ENOENT:
  87. case -ESHUTDOWN:
  88. return;
  89. /* -EPIPE: should clear the halt */
  90. default: /* error */
  91. goto resubmit;
  92. }
  93. /* digital inputs */
  94. for (i=0; i<4; i++) {
  95. level = (buffer[0] >> i) & 1;
  96. if (mc->inputs[i] != level) {
  97. mc->inputs[i] = level;
  98. set_bit(i, &mc->input_events);
  99. }
  100. }
  101. /* motor speed */
  102. if (buffer[2] == 0) {
  103. for (i=0; i<2; i++) {
  104. level = ((s8)buffer[4+i]) * 100 / 127;
  105. if (mc->speed[i] != level) {
  106. mc->speed[i] = level;
  107. set_bit(i, &mc->speed_events);
  108. }
  109. }
  110. } else {
  111. int index = buffer[3] & 1;
  112. level = ((s8)buffer[4] << 8) | buffer[5];
  113. level = level * 100 / 29440;
  114. if (mc->speed[index] != level) {
  115. mc->speed[index] = level;
  116. set_bit(index, &mc->speed_events);
  117. }
  118. level = ((s8)buffer[6] << 8) | buffer[7];
  119. mc->_current[index] = level * 100 / 1572;
  120. }
  121. if (buffer[1] & 1)
  122. set_bit(0, &mc->exceed_events);
  123. if (buffer[1] & 2)
  124. set_bit(1, &mc->exceed_events);
  125. if (mc->input_events || mc->exceed_events || mc->speed_events)
  126. schedule_delayed_work(&mc->do_notify, 0);
  127. resubmit:
  128. status = usb_submit_urb(urb, GFP_ATOMIC);
  129. if (status)
  130. dev_err(&mc->intf->dev,
  131. "can't resubmit intr, %s-%s/motorcontrol0, status %d",
  132. mc->udev->bus->bus_name,
  133. mc->udev->devpath, status);
  134. }
  135. static void do_notify(struct work_struct *work)
  136. {
  137. struct motorcontrol *mc =
  138. container_of(work, struct motorcontrol, do_notify.work);
  139. int i;
  140. char sysfs_file[8];
  141. for (i=0; i<4; i++) {
  142. if (test_and_clear_bit(i, &mc->input_events)) {
  143. sprintf(sysfs_file, "input%d", i);
  144. sysfs_notify(&mc->dev->kobj, NULL, sysfs_file);
  145. }
  146. }
  147. for (i=0; i<2; i++) {
  148. if (test_and_clear_bit(i, &mc->speed_events)) {
  149. sprintf(sysfs_file, "speed%d", i);
  150. sysfs_notify(&mc->dev->kobj, NULL, sysfs_file);
  151. }
  152. }
  153. for (i=0; i<2; i++) {
  154. if (test_and_clear_bit(i, &mc->exceed_events))
  155. dev_warn(&mc->intf->dev,
  156. "motor #%d exceeds 1.5 Amp current limit\n", i);
  157. }
  158. }
  159. #define show_set_speed(value) \
  160. static ssize_t set_speed##value(struct device *dev, \
  161. struct device_attribute *attr, \
  162. const char *buf, size_t count) \
  163. { \
  164. struct motorcontrol *mc = dev_get_drvdata(dev); \
  165. int speed; \
  166. int retval; \
  167. \
  168. if (sscanf(buf, "%d", &speed) < 1) \
  169. return -EINVAL; \
  170. \
  171. if (speed < -100 || speed > 100) \
  172. return -EINVAL; \
  173. \
  174. mc->desired_speed[value] = speed; \
  175. \
  176. retval = set_motor(mc, value); \
  177. \
  178. return retval ? retval : count; \
  179. } \
  180. \
  181. static ssize_t show_speed##value(struct device *dev, \
  182. struct device_attribute *attr, \
  183. char *buf) \
  184. { \
  185. struct motorcontrol *mc = dev_get_drvdata(dev); \
  186. \
  187. return sprintf(buf, "%d\n", mc->speed[value]); \
  188. }
  189. #define speed_attr(value) \
  190. __ATTR(speed##value, S_IWUGO | S_IRUGO, \
  191. show_speed##value, set_speed##value)
  192. show_set_speed(0);
  193. show_set_speed(1);
  194. #define show_set_acceleration(value) \
  195. static ssize_t set_acceleration##value(struct device *dev, \
  196. struct device_attribute *attr, \
  197. const char *buf, size_t count) \
  198. { \
  199. struct motorcontrol *mc = dev_get_drvdata(dev); \
  200. int acceleration; \
  201. int retval; \
  202. \
  203. if (sscanf(buf, "%d", &acceleration) < 1) \
  204. return -EINVAL; \
  205. \
  206. if (acceleration < 0 || acceleration > 100) \
  207. return -EINVAL; \
  208. \
  209. mc->acceleration[value] = acceleration; \
  210. \
  211. retval = set_motor(mc, value); \
  212. \
  213. return retval ? retval : count; \
  214. } \
  215. \
  216. static ssize_t show_acceleration##value(struct device *dev, \
  217. struct device_attribute *attr, \
  218. char *buf) \
  219. { \
  220. struct motorcontrol *mc = dev_get_drvdata(dev); \
  221. \
  222. return sprintf(buf, "%d\n", mc->acceleration[value]); \
  223. }
  224. #define acceleration_attr(value) \
  225. __ATTR(acceleration##value, S_IWUGO | S_IRUGO, \
  226. show_acceleration##value, set_acceleration##value)
  227. show_set_acceleration(0);
  228. show_set_acceleration(1);
  229. #define show_current(value) \
  230. static ssize_t show_current##value(struct device *dev, \
  231. struct device_attribute *attr, \
  232. char *buf) \
  233. { \
  234. struct motorcontrol *mc = dev_get_drvdata(dev); \
  235. \
  236. return sprintf(buf, "%dmA\n", (int)mc->_current[value]); \
  237. }
  238. #define current_attr(value) \
  239. __ATTR(current##value, S_IRUGO, show_current##value, NULL)
  240. show_current(0);
  241. show_current(1);
  242. #define show_input(value) \
  243. static ssize_t show_input##value(struct device *dev, \
  244. struct device_attribute *attr, \
  245. char *buf) \
  246. { \
  247. struct motorcontrol *mc = dev_get_drvdata(dev); \
  248. \
  249. return sprintf(buf, "%d\n", (int)mc->inputs[value]); \
  250. }
  251. #define input_attr(value) \
  252. __ATTR(input##value, S_IRUGO, show_input##value, NULL)
  253. show_input(0);
  254. show_input(1);
  255. show_input(2);
  256. show_input(3);
  257. static struct device_attribute dev_attrs[] = {
  258. input_attr(0),
  259. input_attr(1),
  260. input_attr(2),
  261. input_attr(3),
  262. speed_attr(0),
  263. speed_attr(1),
  264. acceleration_attr(0),
  265. acceleration_attr(1),
  266. current_attr(0),
  267. current_attr(1)
  268. };
  269. static int motorcontrol_probe(struct usb_interface *intf, const struct usb_device_id *id)
  270. {
  271. struct usb_device *dev = interface_to_usbdev(intf);
  272. struct usb_host_interface *interface;
  273. struct usb_endpoint_descriptor *endpoint;
  274. struct motorcontrol *mc;
  275. int pipe, maxp, rc = -ENOMEM;
  276. int bit, value, i;
  277. interface = intf->cur_altsetting;
  278. if (interface->desc.bNumEndpoints != 1)
  279. return -ENODEV;
  280. endpoint = &interface->endpoint[0].desc;
  281. if (!usb_endpoint_dir_in(endpoint))
  282. return -ENODEV;
  283. /*
  284. * bmAttributes
  285. */
  286. pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
  287. maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
  288. mc = kzalloc(sizeof(*mc), GFP_KERNEL);
  289. if (!mc)
  290. goto out;
  291. mc->dev_no = -1;
  292. mc->data = usb_buffer_alloc(dev, URB_INT_SIZE, GFP_ATOMIC, &mc->data_dma);
  293. if (!mc->data)
  294. goto out;
  295. mc->irq = usb_alloc_urb(0, GFP_KERNEL);
  296. if (!mc->irq)
  297. goto out;
  298. mc->udev = usb_get_dev(dev);
  299. mc->intf = intf;
  300. mc->acceleration[0] = mc->acceleration[1] = 10;
  301. INIT_DELAYED_WORK(&mc->do_notify, do_notify);
  302. usb_fill_int_urb(mc->irq, mc->udev, pipe, mc->data,
  303. maxp > URB_INT_SIZE ? URB_INT_SIZE : maxp,
  304. motorcontrol_irq, mc, endpoint->bInterval);
  305. mc->irq->transfer_dma = mc->data_dma;
  306. mc->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  307. usb_set_intfdata(intf, mc);
  308. do {
  309. bit = find_first_zero_bit(&device_no, sizeof(device_no));
  310. value = test_and_set_bit(bit, &device_no);
  311. } while(value);
  312. mc->dev_no = bit;
  313. mc->dev = device_create(phidget_class, &mc->udev->dev, 0,
  314. "motorcontrol%d", mc->dev_no);
  315. if (IS_ERR(mc->dev)) {
  316. rc = PTR_ERR(mc->dev);
  317. mc->dev = NULL;
  318. goto out;
  319. }
  320. dev_set_drvdata(mc->dev, mc);
  321. if (usb_submit_urb(mc->irq, GFP_KERNEL)) {
  322. rc = -EIO;
  323. goto out;
  324. }
  325. for (i=0; i<ARRAY_SIZE(dev_attrs); i++) {
  326. rc = device_create_file(mc->dev, &dev_attrs[i]);
  327. if (rc)
  328. goto out2;
  329. }
  330. dev_info(&intf->dev, "USB PhidgetMotorControl attached\n");
  331. return 0;
  332. out2:
  333. while (i-- > 0)
  334. device_remove_file(mc->dev, &dev_attrs[i]);
  335. out:
  336. if (mc) {
  337. usb_free_urb(mc->irq);
  338. if (mc->data)
  339. usb_buffer_free(dev, URB_INT_SIZE, mc->data, mc->data_dma);
  340. if (mc->dev)
  341. device_unregister(mc->dev);
  342. if (mc->dev_no >= 0)
  343. clear_bit(mc->dev_no, &device_no);
  344. kfree(mc);
  345. }
  346. return rc;
  347. }
  348. static void motorcontrol_disconnect(struct usb_interface *interface)
  349. {
  350. struct motorcontrol *mc;
  351. int i;
  352. mc = usb_get_intfdata(interface);
  353. usb_set_intfdata(interface, NULL);
  354. if (!mc)
  355. return;
  356. usb_kill_urb(mc->irq);
  357. usb_free_urb(mc->irq);
  358. usb_buffer_free(mc->udev, URB_INT_SIZE, mc->data, mc->data_dma);
  359. cancel_delayed_work(&mc->do_notify);
  360. for (i=0; i<ARRAY_SIZE(dev_attrs); i++)
  361. device_remove_file(mc->dev, &dev_attrs[i]);
  362. device_unregister(mc->dev);
  363. usb_put_dev(mc->udev);
  364. clear_bit(mc->dev_no, &device_no);
  365. kfree(mc);
  366. dev_info(&interface->dev, "USB PhidgetMotorControl detached\n");
  367. }
  368. static struct usb_driver motorcontrol_driver = {
  369. .name = "phidgetmotorcontrol",
  370. .probe = motorcontrol_probe,
  371. .disconnect = motorcontrol_disconnect,
  372. .id_table = id_table
  373. };
  374. static int __init motorcontrol_init(void)
  375. {
  376. int retval = 0;
  377. retval = usb_register(&motorcontrol_driver);
  378. if (retval)
  379. err("usb_register failed. Error number %d", retval);
  380. return retval;
  381. }
  382. static void __exit motorcontrol_exit(void)
  383. {
  384. usb_deregister(&motorcontrol_driver);
  385. }
  386. module_init(motorcontrol_init);
  387. module_exit(motorcontrol_exit);
  388. MODULE_AUTHOR(DRIVER_AUTHOR);
  389. MODULE_DESCRIPTION(DRIVER_DESC);
  390. MODULE_LICENSE("GPL");