23 #include <linux/module.h>
24 #include <linux/kernel.h>
28 #include <linux/slab.h>
34 #define USBTMC_MINOR_BASE 176
40 #define USBTMC_SIZE_IOBUFFER 2048
43 #define USBTMC_TIMEOUT 5000
50 #define USBTMC_MAX_READS_TO_CLEAR_BULK_IN 100
98 #define to_usbtmc_data(d) container_of(d, struct usbtmc_device_data, kref)
101 static struct usb_driver usbtmc_driver;
103 static void usbtmc_delete(
struct kref *
kref)
120 ": can not find device for minor %d", iminor(inode));
125 data = usb_get_intfdata(intf);
126 kref_get(&data->
kref);
135 static int usbtmc_release(
struct inode *inode,
struct file *
file)
139 kref_put(&data->
kref, usbtmc_delete);
150 struct usb_host_interface *current_setting;
153 dev = &data->
intf->dev;
159 usb_rcvctrlpipe(data->
usb_dev, 0),
166 dev_err(dev,
"usb_control_msg returned %d\n", rv);
170 dev_dbg(dev,
"INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
178 dev_err(dev,
"INITIATE_ABORT_BULK_IN returned %x\n",
185 current_setting = data->
intf->cur_altsetting;
186 for (n = 0; n < current_setting->desc.bNumEndpoints; n++)
187 if (current_setting->endpoint[n].desc.bEndpointAddress ==
189 max_size = usb_endpoint_maxp(¤t_setting->endpoint[n].desc);
192 dev_err(dev,
"Couldn't get wMaxPacketSize\n");
197 dev_dbg(&data->
intf->dev,
"wMaxPacketSize is %d\n", max_size);
202 dev_dbg(dev,
"Reading from bulk in EP\n");
213 dev_err(dev,
"usb_bulk_msg returned %d\n", rv);
216 }
while ((actual == max_size) &&
219 if (actual == max_size) {
220 dev_err(dev,
"Couldn't clear device buffer within %d cycles\n",
228 usbtmc_abort_bulk_in_status:
230 usb_rcvctrlpipe(data->
usb_dev, 0),
233 0, data->
bulk_in, buffer, 0x08,
237 dev_err(dev,
"usb_control_msg returned %d\n", rv);
241 dev_dbg(dev,
"INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
249 dev_err(dev,
"INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
256 dev_dbg(dev,
"Reading from bulk in EP\n");
267 dev_err(dev,
"usb_bulk_msg returned %d\n", rv);
270 }
while ((actual == max_size) &&
273 if (actual == max_size) {
274 dev_err(dev,
"Couldn't clear device buffer within %d cycles\n",
280 goto usbtmc_abort_bulk_in_status;
295 dev = &data->
intf->dev;
302 usb_rcvctrlpipe(data->
usb_dev, 0),
309 dev_err(dev,
"usb_control_msg returned %d\n", rv);
313 dev_dbg(dev,
"INITIATE_ABORT_BULK_OUT returned %x\n", buffer[0]);
316 dev_err(dev,
"INITIATE_ABORT_BULK_OUT returned %x\n",
324 usbtmc_abort_bulk_out_check_status:
326 usb_rcvctrlpipe(data->
usb_dev, 0),
333 dev_err(dev,
"usb_control_msg returned %d\n", rv);
337 dev_dbg(dev,
"CHECK_ABORT_BULK_OUT returned %x\n", buffer[0]);
340 goto usbtmc_abort_bulk_out_clear_halt;
344 goto usbtmc_abort_bulk_out_check_status;
349 usbtmc_abort_bulk_out_clear_halt:
354 dev_err(dev,
"usb_control_msg returned %d\n", rv);
365 size_t count, loff_t *f_pos)
379 dev = &data->
intf->dev;
394 while (remaining > 0) {
398 this_part = remaining;
404 buffer[1] = data->
bTag;
405 buffer[2] = ~(data->
bTag);
432 dev_err(dev,
"usb_bulk_msg returned %d\n", retval);
434 usbtmc_ioctl_abort_bulk_out(data);
449 dev_err(dev,
"Unable to read data, error %d\n", retval);
451 usbtmc_ioctl_abort_bulk_in(data);
456 n_characters = buffer[4] +
462 if(n_characters > actual - 12) {
463 dev_err(dev,
"Device lies about message size: %u > %d\n", n_characters, actual - 12);
464 n_characters = actual - 12;
468 if(n_characters > this_part) {
469 dev_err(dev,
"Device returns more than requested: %zu > %zu\n", done + n_characters, done + this_part);
474 if (n_characters > this_part)
478 if (
copy_to_user(buf + done, &buffer[12], n_characters)) {
484 done += n_characters;
486 if ((buffer[8] & 0x01) && (actual >= n_characters + 12))
489 remaining -= n_characters;
493 *f_pos = *f_pos +
done;
502 static ssize_t usbtmc_write(
struct file *filp,
const char __user *buf,
503 size_t count, loff_t *f_pos)
509 unsigned long int n_bytes;
529 while (remaining > 0) {
534 this_part = remaining;
540 buffer[1] = data->
bTag;
541 buffer[2] = ~(data->
bTag);
543 buffer[4] = this_part & 255;
544 buffer[5] = (this_part >> 8) & 255;
545 buffer[6] = (this_part >> 16) & 255;
546 buffer[7] = (this_part >> 24) & 255;
557 n_bytes =
roundup(12 + this_part, 4);
558 memset(buffer + 12 + this_part, 0, n_bytes - (12 + this_part));
579 "Unable to send data, error %d\n", retval);
581 usbtmc_ioctl_abort_bulk_out(data);
598 struct usb_host_interface *current_setting;
607 dev = &data->
intf->dev;
609 dev_dbg(dev,
"Sending INITIATE_CLEAR request\n");
616 usb_rcvctrlpipe(data->
usb_dev, 0),
621 dev_err(dev,
"usb_control_msg returned %d\n", rv);
625 dev_dbg(dev,
"INITIATE_CLEAR returned %x\n", buffer[0]);
628 dev_err(dev,
"INITIATE_CLEAR returned %x\n", buffer[0]);
634 current_setting = data->
intf->cur_altsetting;
635 for (n = 0; n < current_setting->desc.bNumEndpoints; n++) {
636 desc = ¤t_setting->endpoint[
n].desc;
638 max_size = usb_endpoint_maxp(desc);
642 dev_err(dev,
"Couldn't get wMaxPacketSize\n");
647 dev_dbg(dev,
"wMaxPacketSize is %d\n", max_size);
651 usbtmc_clear_check_status:
653 dev_dbg(dev,
"Sending CHECK_CLEAR_STATUS request\n");
656 usb_rcvctrlpipe(data->
usb_dev, 0),
661 dev_err(dev,
"usb_control_msg returned %d\n", rv);
665 dev_dbg(dev,
"CHECK_CLEAR_STATUS returned %x\n", buffer[0]);
668 goto usbtmc_clear_bulk_out_halt;
671 dev_err(dev,
"CHECK_CLEAR_STATUS returned %x\n", buffer[0]);
678 dev_dbg(dev,
"Reading from bulk in EP\n");
688 dev_err(dev,
"usb_control_msg returned %d\n",
692 }
while ((actual == max_size) &&
695 if (actual == max_size) {
696 dev_err(dev,
"Couldn't clear device buffer within %d cycles\n",
702 goto usbtmc_clear_check_status;
704 usbtmc_clear_bulk_out_halt:
709 dev_err(dev,
"usb_control_msg returned %d\n", rv);
782 dev_err(dev,
"usb_control_msg returned %d\n", rv);
786 dev_dbg(dev,
"GET_CAPABILITIES returned %x\n", buffer[0]);
788 dev_err(dev,
"GET_CAPABILITIES returned %x\n", buffer[0]);
792 dev_dbg(dev,
"Interface capabilities are %x\n", buffer[4]);
793 dev_dbg(dev,
"Device capabilities are %x\n", buffer[5]);
794 dev_dbg(dev,
"USB488 interface capabilities are %x\n", buffer[14]);
795 dev_dbg(dev,
"USB488 device capabilities are %x\n", buffer[15]);
799 data->
capabilities.usb488_interface_capabilities = buffer[14];
800 data->
capabilities.usb488_device_capabilities = buffer[15];
808 #define capability_attribute(name) \
809 static ssize_t show_##name(struct device *dev, \
810 struct device_attribute *attr, char *buf) \
812 struct usb_interface *intf = to_usb_interface(dev); \
813 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
815 return sprintf(buf, "%d\n", data->capabilities.name); \
817 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
824 static struct attribute *capability_attrs[] = {
825 &dev_attr_interface_capabilities.attr,
826 &dev_attr_device_capabilities.attr,
827 &dev_attr_usb488_interface_capabilities.attr,
828 &dev_attr_usb488_device_capabilities.attr,
833 .attrs = capability_attrs,
847 const char *buf,
size_t count)
859 #define data_attribute(name) \
860 static ssize_t show_##name(struct device *dev, \
861 struct device_attribute *attr, char *buf) \
863 struct usb_interface *intf = to_usb_interface(dev); \
864 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
866 return sprintf(buf, "%d\n", data->name); \
868 static ssize_t store_##name(struct device *dev, \
869 struct device_attribute *attr, \
870 const char *buf, size_t count) \
872 struct usb_interface *intf = to_usb_interface(dev); \
873 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
877 result = sscanf(buf, "%u\n", &val); \
886 static DEVICE_ATTR(name, S_IRUGO, show_##name, store_##name)
891 static struct attribute *data_attrs[] = {
892 &dev_attr_TermChar.attr,
893 &dev_attr_TermCharEnabled.attr,
894 &dev_attr_auto_abort.attr,
908 dev = &data->
intf->dev;
915 usb_rcvctrlpipe(data->
usb_dev, 0),
921 dev_err(dev,
"usb_control_msg returned %d\n", rv);
925 dev_dbg(dev,
"INDICATOR_PULSE returned %x\n", buffer[0]);
928 dev_err(dev,
"INDICATOR_PULSE returned %x\n", buffer[0]);
939 static long usbtmc_ioctl(
struct file *
file,
unsigned int cmd,
unsigned long arg)
948 goto skip_io_on_zombie;
953 retval = usbtmc_ioctl_clear_out_halt(data);
957 retval = usbtmc_ioctl_clear_in_halt(data);
961 retval = usbtmc_ioctl_indicator_pulse(data);
965 retval = usbtmc_ioctl_clear(data);
969 retval = usbtmc_ioctl_abort_bulk_out(data);
973 retval = usbtmc_ioctl_abort_bulk_in(data);
985 .write = usbtmc_write,
987 .release = usbtmc_release,
988 .unlocked_ioctl = usbtmc_ioctl,
992 static struct usb_class_driver usbtmc_class = {
1003 struct usb_host_interface *iface_desc;
1008 dev_dbg(&intf->dev,
"%s called\n", __func__);
1012 dev_err(&intf->dev,
"Unable to allocate kernel memory\n");
1019 usb_set_intfdata(intf, data);
1020 kref_init(&data->
kref);
1030 iface_desc = data->
intf->cur_altsetting;
1033 for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) {
1034 endpoint = &iface_desc->endpoint[
n].desc;
1036 if (usb_endpoint_is_bulk_in(endpoint)) {
1038 dev_dbg(&intf->dev,
"Found bulk in endpoint at %u\n",
1045 for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) {
1046 endpoint = &iface_desc->endpoint[
n].desc;
1048 if (usb_endpoint_is_bulk_out(endpoint)) {
1050 dev_dbg(&intf->dev,
"Found Bulk out endpoint at %u\n",
1056 retcode = get_capabilities(data);
1058 dev_err(&intf->dev,
"can't read capabilities\n");
1061 &capability_attr_grp);
1067 dev_err(&intf->dev,
"Not able to get a minor"
1070 goto error_register;
1072 dev_dbg(&intf->dev,
"Using minor number %d\n", intf->minor);
1079 kref_put(&data->
kref, usbtmc_delete);
1087 dev_dbg(&intf->dev,
"usbtmc_disconnect called\n");
1089 data = usb_get_intfdata(intf);
1096 kref_put(&data->
kref, usbtmc_delete);
1110 static struct usb_driver usbtmc_driver = {
1112 .id_table = usbtmc_devices,
1113 .probe = usbtmc_probe,
1114 .disconnect = usbtmc_disconnect,
1115 .suspend = usbtmc_suspend,
1116 .resume = usbtmc_resume,