#include "../include/devcommon.h"
#include "../include/setup.h"
#include "../include/usbcan.h"
-
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20))
#include <linux/freezer.h>
#endif
+#include <linux/smp_lock.h>
static int usbcan_probe(struct usb_interface *interface, const struct usb_device_id *id);
static void usbcan_disconnect(struct usb_interface *interface);
+#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,10))
+void release_device(struct kref *refcount);
+#else
+void release_device(struct candevice_t *candev);
+#endif
volatile int usbcan_chip_count=0;
return 0;
}
-/* !!! Don't change this function !!! */
int usbcan_register(struct hwspecops_t *hwspecops)
{
hwspecops->request_io = usbcan_request_io;
hwspecops->write_register = NULL;
hwspecops->read_register = NULL;
hwspecops->program_irq = usbcan_program_irq;
+#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,10))
+ hwspecops->release_device = release_device;
+#endif
return 0;
}
*
* File: src/usbcan.c
*/
-void usbcan_register_devs(struct canchip_t *chip,void *data){
+int usbcan_register_devs(struct canchip_t *chip,void *data){
struct usbcan_devs *usbdevs=(struct usbcan_devs *)data;
if (!usbdevs){
CANMSG("Bad structure given\n");
- return;
+ return -1;
}
if (chip->chip_idx>=usbdevs->count) {
CANMSG("Requested chip number is bigger than chip count\n");
- return;
+ return -1;
}
usbdevs->devs[chip->chip_idx]->chip=chip;
chip->chip_data=(void *)usbdevs->devs[chip->chip_idx];
+ return 0;
}
/**
/* terminate the kernel thread */
set_bit(USBCAN_TERMINATE,&dev->flags);
- wake_up_process(dev->comthread);
-// can_kthread_stop(dev->comthread);
+// wake_up_process(dev->comthread);
+ can_kthread_stop(dev->comthread);
return 0;
}
case -ESHUTDOWN:
/* this urb is terminated, clean up */
CANMSG("%s > Urb shutting down with status: %d\n", __FUNCTION__, urb->status);
- set_bit(USBCAN_TERMINATE,&m->dev->flags);
+// set_bit(USBCAN_TERMINATE,&m->dev->flags);
set_bit(USBCAN_MESSAGE_TERMINATE,&m->flags);
clear_bit(USBCAN_MESSAGE_URB_PENDING,&m->flags);
return;
case -ESHUTDOWN:
/* this urb is terminated, clean up */
CANMSG("%s > Urb shutting down with status: %d\n", __FUNCTION__, urb->status);
- set_bit(USBCAN_TERMINATE,&m->dev->flags);
+// set_bit(USBCAN_TERMINATE,&m->dev->flags);
set_bit(USBCAN_MESSAGE_TERMINATE,&m->flags);
clear_bit(USBCAN_MESSAGE_URB_PENDING,&m->flags);
return;
goto error;
obj=dev->chip->msgobj[0];
-
INIT_LIST_HEAD(&dev->rx_pend_list);
INIT_LIST_HEAD(&dev->rx_ready_list);
INIT_LIST_HEAD(&dev->tx_idle_list);
the flags are visible on all CPUs. */
mb();
/* fall asleep */
- if (!(can_kthread_should_stop() || test_bit(USBCAN_TERMINATE,&dev->flags))){
- if (usbcan_sleep_thread(dev)<0)
- break;
-/* wait_event_interruptible(dev->queue,
- can_kthread_should_stop() ||
- test_bit(USBCAN_DATA_OK,&dev->flags) ||
- test_bit(USBCAN_TX_PENDING,&dev->flags) ||
- test_bit(USBCAN_TERMINATE,&dev->flags) ||
- test_bit(USBCAN_ERROR,&dev->flags)
- );*/
- }
- /* We need to do a memory barrier here to be sure that
- the flags are visible on all CPUs. */
- mb();
-
- /* here we are back from sleep because we caught a signal. */
- if (can_kthread_should_stop()){
- /* we received a request to terminate ourself */
+ if (!can_kthread_should_stop() && !test_bit(USBCAN_TERMINATE,&dev->flags) && (usbcan_sleep_thread(dev)<0)){
break;
}
+ /* We need to do a memory barrier here to be sure that the flags are visible on all CPUs. */
+ mb();
- /* here we are back from sleep because we caught a signal. */
- if (test_bit(USBCAN_TERMINATE,&dev->flags)){
- /* we received a request to terminate ourself */
+ if (can_kthread_should_stop() || test_bit(USBCAN_TERMINATE,&dev->flags)){
break;
}
set_bit(USBCAN_TERMINATE,&dev->flags);
exit:
- /* here we go only in case of termination of the thread */
usbcan_kthread_free_urbs(dev);
-
-
clear_bit(USBCAN_THREAD_RUNNING,&dev->flags);
CANMSG ("usbcan thread finished!\n");
usbdevs = (struct usbcan_devs *) can_checked_malloc(sizeof(struct usbcan_devs));
if (!usbdevs) {
- CANMSG("Out of memory");
- goto error;
+ goto noalloc;
}
memset(usbdevs, 0, sizeof(struct usbcan_devs));
- usbdevs->count=usbcan_chip_count;
+ usbdevs->count = usbcan_chip_count;
+ usbdevs->udev = interface_to_usbdev(interface);
usbdevs->devs = (struct usbcan_usb **) can_checked_malloc(usbcan_chip_count * sizeof(struct usbcan_usb *));
if (!usbdevs->devs) {
- CANMSG("Out of memory");
goto error;
}
memset(usbdevs->devs, 0, usbcan_chip_count * sizeof(struct usbcan_usb *));
/* allocate memory for our device state and initialize it */
usbdevs->devs[j] = (struct usbcan_usb *) can_checked_malloc(sizeof(struct usbcan_usb));
if (!usbdevs->devs[j]) {
- CANMSG("Out of memory");
goto error;
}
memset(usbdevs->devs[j], 0, sizeof(struct usbcan_usb));
dev=usbdevs->devs[j];
- spin_lock_init(&usbdevs->devs[j]->list_lock);
+ spin_lock_init(&dev->list_lock);
mutex_init(&dev->io_mutex);
init_waitqueue_head(&dev->queue);
- dev->udev = interface_to_usbdev(interface);
+ dev->udev = usbdevs->udev;
dev->interface = interface;
/* set up the endpoint information */
goto error;
}
}
+
+ usb_get_dev(usbdevs->udev);
+
/* save our data pointer in this interface device */
usb_set_intfdata(interface, usbdevs);
- if (!(usbdevs->candev=register_usbdev("usbcan",(void *) usbdevs, usbcan_register_devs)))
+ usbdevs->candev=register_hotplug_dev("usbcan", usbcan_register_devs,(void *) usbdevs);
+ if (!(usbdevs->candev)){
+ CANMSG("register_hotplug_dev() failed\n");
goto register_error;
+ }
/* let the user know what node this device is now attached to */
CANMSG("USBCAN device now attached\n");
return 0;
register_error:
- cleanup_usbdev(usbdevs->candev);
+// cleanup_hotplug_dev(usbdevs->candev);
+ usb_put_dev(usbdevs->udev);
error:
if (usbdevs){
if (usbdevs->devs){
- if (usbdevs->devs[0]){
- usb_put_dev(usbdevs->devs[0]->udev);
- }
for (j=0;j<usbdevs->count;j++){
if (!usbdevs->devs[j]) continue;
return retval;
}
-// Physically disconnected device
-static void usbcan_disconnect(struct usb_interface *interface)
-{
- struct usbcan_devs *usbdevs;
+#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,10))
+void release_device(struct kref *refcount){
+ struct candevice_t *candev = container_of(refcount,struct candevice_t,refcount);
+#else
+void release_device(struct candevice_t *candev){
+#endif
+ struct usbcan_devs *usbdevs = (struct usbcan_devs *)candev->sysdevptr.anydev;
int j;
- usbdevs = usb_get_intfdata(interface);
- if (usbdevs==NULL){
- CANMSG("USBCAN device seems to be removed\n");
+
+ if (!usbdevs)
return;
- }
- usb_set_intfdata(interface, NULL);
+
+ cleanup_hotplug_dev(usbdevs->candev);
if (usbdevs->devs){
- usb_put_dev((*usbdevs->devs)->udev);
- }
- cleanup_usbdev(usbdevs->candev);
- if (usbdevs->devs){
+ /* Finally, release all structures in USB subsystem */
+ if (!usbdevs->udev)
+ panic("udev is already null on device release");
+ usb_put_dev(usbdevs->udev);
+
for (j=0;j<usbdevs->count;j++){
if (!usbdevs->devs[j]) continue;
- /* prevent more I/O from starting */
- mutex_lock(&usbdevs->devs[j]->io_mutex);
- usbdevs->devs[j]->interface = NULL;
- mutex_unlock(&usbdevs->devs[j]->io_mutex);
-
- while (test_bit(USBCAN_THREAD_RUNNING,&usbdevs->devs[j]->flags))
- {
- CANMSG("USBCAN thread has not stopped, trying to wake...\n");
- set_bit(USBCAN_TERMINATE,&usbdevs->devs[j]->flags);
- wake_up_process(usbdevs->devs[j]->comthread);
- schedule();
-// can_kthread_stop(dev->comthread);
- }
-
if (usbdevs->devs[j]->bulk_in_buffer)
can_checked_free(usbdevs->devs[j]->bulk_in_buffer);
- // if (usbdevs->devs[j]->chip){
- // usbdevs->devs[j]->chip->chip_data=NULL;
- // }
can_checked_free(usbdevs->devs[j]);
usbdevs->devs[j]=NULL;
}
CANMSG("USBCAN now disconnected\n");
}
+// Physically disconnected device
+static void usbcan_disconnect(struct usb_interface *interface)
+{
+ struct usbcan_devs *usbdevs;
+ int j;
+
+ /* prevent more I/O from starting */
+ lock_kernel();
+
+ usbdevs = usb_get_intfdata(interface);
+ if (usbdevs==NULL){
+ CANMSG("USBCAN device seems to be already removed\n");
+ unlock_kernel();
+ return;
+ }
+ usb_set_intfdata(interface, NULL);
+ deregister_hotplug_dev(usbdevs->candev);
+
+ for (j=0;j<usbdevs->count;j++){
+ if (!usbdevs->devs[j]) continue;
+ mutex_lock(&usbdevs->devs[j]->io_mutex);
+ usbdevs->devs[j]->interface = NULL;
+ mutex_unlock(&usbdevs->devs[j]->io_mutex);
+ }
+
+ unlock_kernel();
+
+#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,10))
+ kref_put(&usbdevs->candev->refcount,release_device);
+#else
+ release_device(&usbdevs->candev);
+#endif
+}
+
int usbcan_init(void){
return usb_register(&usbcan_driver);
}