26 #define __NO_VERSION__
27 #include <linux/device.h>
28 #include <linux/module.h>
29 #include <linux/pci.h>
31 #include <linux/errno.h>
33 #include <linux/kernel.h>
34 #include <linux/sched.h>
35 #include <linux/fcntl.h>
39 #include <linux/slab.h>
64 if (num_subdevices < 1)
67 s = kcalloc(num_subdevices,
sizeof(*s),
GFP_KERNEL);
73 for (i = 0; i < num_subdevices; ++
i) {
113 comedi_set_hw_dev(dev,
NULL);
116 static void __comedi_device_detach(
struct comedi_device *dev)
123 "BUG: dev->driver=NULL in comedi_device_detach()\n");
131 __comedi_device_detach(dev);
136 static int comedi_device_postconfig(
struct comedi_device *dev)
138 int ret = postconfig(dev);
139 module_put(dev->
driver->module);
141 __comedi_device_detach(dev);
161 for (driv = comedi_drivers; driv; driv = driv->
next) {
162 if (!try_module_get(driv->
module))
175 for (driv = comedi_drivers; driv; driv = driv->
next) {
176 if (!try_module_get(driv->
module))
178 comedi_report_boards(driv);
186 "driver '%s' does not support attach using comedi_config\n",
194 ret = driv->
attach(dev, it);
196 module_put(dev->
driver->module);
197 __comedi_device_detach(dev);
200 return comedi_device_postconfig(dev);
223 if (dev_file_info ==
NULL)
225 dev = dev_file_info->
device;
231 "BUG! detaching device with use_count=%d\n",
238 if (comedi_drivers == driver) {
239 comedi_drivers = driver->
next;
243 for (prev = comedi_drivers; prev->
next; prev = prev->
next) {
244 if (prev->
next == driver) {
280 "failed to allocate async struct\n");
297 "Buffer allocation failed\n");
326 s->
poll = poll_invalid;
364 if (
strcmp(*name_ptr, name) == 0)
366 name_ptr = (
void *)name_ptr + driv->
offset;
375 const char *
const *name_ptr;
377 pr_info(
"comedi: valid board names for %s driver are:\n",
383 name_ptr = (
const char **)((
char *)name_ptr + driv->
offset);
407 static const unsigned channels_per_bitfield = 32;
410 const unsigned base_bitfield_channel =
411 (chan < channels_per_bitfield) ? 0 : chan;
412 unsigned int new_data[2];
413 memset(new_data, 0,
sizeof(new_data));
414 memset(&new_insn, 0,
sizeof(new_insn));
416 new_insn.chanspec = base_bitfield_channel;
418 new_insn.subdev = insn->
subdev;
423 new_data[0] = 1 << (chan - base_bitfield_channel);
424 new_data[1] = data[0] ? (1 << (chan - base_bitfield_channel))
428 ret = s->
insn_bits(dev, s, &new_insn, new_data);
433 data[0] = (new_data[1] >> (chan - base_bitfield_channel)) & 1;
439 unsigned long new_size)
495 for (i = 0; i < n_pages; i++) {
522 #ifdef PAGE_KERNEL_NOCACHE
533 for (i = 0; i < n_pages; i++) {
571 static unsigned int comedi_buf_munge(
struct comedi_async *async,
575 unsigned int count = 0;
584 num_bytes -= num_bytes % num_sample_bytes;
585 while (count < num_bytes) {
588 block_size = num_bytes -
count;
589 if (block_size < 0) {
591 "%s: %s: bug! block_size is negative\n",
595 if ((
int)(async->
munge_ptr + block_size -
605 async->
munge_chan += block_size / num_sample_bytes;
618 unsigned int free_end;
674 "attempted to write-free more bytes than have been write-allocated.\n");
711 "attempted to read-free more bytes than have been read-allocated.\n");
722 const void *data,
unsigned int num_bytes)
766 memcpy(dest, src, block_size);
794 if (n <
sizeof(
short))
807 if (n <
sizeof(
short)) {
837 comedi_auto_config_helper(
struct device *hardware_device,
855 comedi_dev = dev_file_info->
device;
860 else if (!try_module_get(driver->
module))
865 ret = (*attach_wrapper)(comedi_dev,
context);
867 module_put(driver->
module);
868 __comedi_device_detach(comedi_dev);
870 ret = comedi_device_postconfig(comedi_dev);
880 static int comedi_auto_config_wrapper(
struct comedi_device *dev,
void *context)
891 "auto config failed to find board entry '%s' for driver '%s'\n",
893 comedi_report_boards(driv);
899 "BUG! driver '%s' using old-style auto config but has no attach handler\n",
903 return driv->
attach(dev, it);
906 static int comedi_auto_config(
struct device *hardware_device,
908 unsigned num_options)
912 memset(&it, 0,
sizeof(it));
917 return comedi_auto_config_helper(hardware_device, driver,
918 comedi_auto_config_wrapper, &it);
921 static void comedi_auto_unconfig(
struct device *hardware_device)
925 if (hardware_device ==
NULL)
968 static int comedi_old_pci_auto_config(
struct pci_dev *
pcidev,
974 options[0] = pcidev->
bus->number;
978 return comedi_auto_config(&pcidev->
dev, driver,
982 static int comedi_pci_attach_wrapper(
struct comedi_device *dev,
void *pcidev)
984 return dev->
driver->attach_pci(dev, pcidev);
987 static int comedi_new_pci_auto_config(
struct pci_dev *pcidev,
990 return comedi_auto_config_helper(&pcidev->
dev, driver,
991 comedi_pci_attach_wrapper, pcidev);
998 return comedi_new_pci_auto_config(pcidev, driver);
1000 return comedi_old_pci_auto_config(pcidev, driver);
1006 comedi_auto_unconfig(&pcidev->
dev);
1020 if (!pci_driver->
name)
1023 ret = pci_register_driver(pci_driver);
1041 #if IS_ENABLED(CONFIG_USB)
1046 return comedi_auto_config(&intf->dev, driver,
NULL, 0);
1051 return dev->
driver->attach_usb(dev, intf);
1054 static int comedi_new_usb_auto_config(
struct usb_interface *intf,
1057 return comedi_auto_config_helper(&intf->dev, driver,
1058 comedi_usb_attach_wrapper, intf);
1066 return comedi_new_usb_auto_config(intf, driver);
1068 return comedi_old_usb_auto_config(intf, driver);
1075 comedi_auto_unconfig(&intf->dev);
1080 struct usb_driver *usb_driver)
1088 ret = usb_register(usb_driver);
1099 struct usb_driver *usb_driver)