2 * CPC-USB CAN Interface Kernel Driver
4 * Copyright (C) 2004-2009 EMS Dr. Thomas Wuensche
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published
8 * by the Free Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <linux/kernel.h>
20 #include <linux/errno.h>
21 #include <linux/init.h>
22 #include <linux/slab.h>
23 #include <linux/vmalloc.h>
24 #include <linux/module.h>
25 #include <linux/poll.h>
26 #include <linux/smp_lock.h>
27 #include <linux/completion.h>
28 #include <asm/uaccess.h>
29 #include <linux/usb.h>
31 #include <linux/version.h>
33 #include <linux/proc_fs.h>
42 /* Version Information */
43 #define DRIVER_AUTHOR "Sebastian Haas <haas@ems-wuensche.com>"
44 #define DRIVER_DESC "CPC-USB Driver for Linux Kernel 2.6"
45 #define DRIVER_VERSION CPC_DRIVER_VERSION
47 MODULE_AUTHOR(DRIVER_AUTHOR);
48 MODULE_DESCRIPTION(DRIVER_DESC);
49 MODULE_VERSION(DRIVER_VERSION);
50 MODULE_LICENSE("GPL v2");
52 /* Define these values to match your devices */
53 #define USB_CPCUSB_VENDOR_ID 0x12D6
55 #define USB_CPCUSB_M16C_PRODUCT_ID 0x0888
56 #define USB_CPCUSB_LPC2119_PRODUCT_ID 0x0444
58 #define CPC_USB_PROC_DIR CPC_PROC_DIR "cpc-usb"
60 static struct proc_dir_entry *procDir;
61 static struct proc_dir_entry *procEntry;
63 /* Module parameters */
65 module_param(debug, int, S_IRUGO);
67 /* table of devices that work with this driver */
68 static struct usb_device_id cpcusb_table[] = {
69 {USB_DEVICE(USB_CPCUSB_VENDOR_ID, USB_CPCUSB_M16C_PRODUCT_ID)},
70 {USB_DEVICE(USB_CPCUSB_VENDOR_ID, USB_CPCUSB_LPC2119_PRODUCT_ID)},
71 {} /* Terminating entry */
74 MODULE_DEVICE_TABLE(usb, cpcusb_table);
76 /* use to prevent kernel panic if driver is unloaded
77 * while a programm has still open the device
79 DECLARE_WAIT_QUEUE_HEAD(rmmodWq);
82 static CPC_USB_T *CPCUSB_Table[CPC_USB_CARD_CNT] = { 0 };
83 static unsigned int CPCUsbCnt;
85 /* prevent races between open() and disconnect() */
86 static DECLARE_MUTEX(disconnect_sem);
88 /* local function prototypes */
89 static ssize_t cpcusb_read(struct file *file, char *buffer, size_t count,
91 static ssize_t cpcusb_write(struct file *file, const char *buffer,
92 size_t count, loff_t *ppos);
93 static unsigned int cpcusb_poll(struct file *file, poll_table * wait);
94 static int cpcusb_open(struct inode *inode, struct file *file);
95 static int cpcusb_release(struct inode *inode, struct file *file);
97 static int cpcusb_probe(struct usb_interface *interface,
98 const struct usb_device_id *id);
99 static void cpcusb_disconnect(struct usb_interface *interface);
101 static void cpcusb_read_bulk_callback(struct urb *urb);
102 static void cpcusb_write_bulk_callback(struct urb *urb);
103 static void cpcusb_read_interrupt_callback(struct urb *urb);
105 static int cpcusb_setup_intrep(CPC_USB_T *card);
107 static struct file_operations cpcusb_fops = {
109 * The owner field is part of the module-locking
110 * mechanism. The idea is that the kernel knows
111 * which module to increment the use-counter of
112 * BEFORE it calls the device's open() function.
113 * This also means that the kernel can decrement
114 * the use-counter again before calling release()
115 * or should the open() function fail.
117 .owner = THIS_MODULE,
120 .write = cpcusb_write,
123 .release = cpcusb_release,
127 * usb class driver info in order to get a minor number from the usb core,
128 * and to have the device registered with devfs and the driver core
130 static struct usb_class_driver cpcusb_class = {
131 .name = "usb/cpc_usb%d",
132 .fops = &cpcusb_fops,
133 .minor_base = CPC_USB_BASE_MNR,
136 /* usb specific object needed to register this driver with the usb subsystem */
137 static struct usb_driver cpcusb_driver = {
139 .probe = cpcusb_probe,
140 .disconnect = cpcusb_disconnect,
141 .id_table = cpcusb_table,
144 static int cpcusb_create_info_output(char *buf)
148 for (j = 0; j < CPC_USB_CARD_CNT; j++) {
149 if (CPCUSB_Table[j]) {
150 CPC_USB_T *card = CPCUSB_Table[j];
151 CPC_CHAN_T *chan = card->chan;
153 /* MINOR CHANNELNO BUSNO SLOTNO */
154 i += sprintf(&buf[i], "%d %s\n", chan->minor,
162 static int cpcusb_proc_read_info(char *page, char **start, off_t off,
163 int count, int *eof, void *data)
165 int len = cpcusb_create_info_output(page);
167 if (len <= off + count)
180 * Remove CPC-USB and cleanup
182 static inline void cpcusb_delete(CPC_USB_T *card)
187 vfree(card->chan->buf);
189 if (card->chan->CPCWait_q)
190 kfree(card->chan->CPCWait_q);
195 CPCUSB_Table[card->idx] = NULL;
201 * setup the interrupt IN endpoint of a specific CPC-USB device
203 static int cpcusb_setup_intrep(CPC_USB_T *card)
206 struct usb_endpoint_descriptor *ep;
208 ep = &card->interface->altsetting[0].endpoint[card->num_intr_in].desc;
210 card->intr_in_buffer[0] = 0;
211 card->free_slots = 15; /* initial size */
214 usb_fill_int_urb(card->intr_in_urb, card->udev,
215 usb_rcvintpipe(card->udev, card->num_intr_in),
216 card->intr_in_buffer,
217 sizeof(card->intr_in_buffer),
218 cpcusb_read_interrupt_callback,
222 card->intr_in_urb->status = 0; /* needed! */
225 retval = usb_submit_urb(card->intr_in_urb, GFP_KERNEL);
228 err("%s - failed submitting intr urb, error %d", __func__,
234 static int cpcusb_open(struct inode *inode, struct file *file)
236 CPC_USB_T *card = NULL;
237 struct usb_interface *interface;
241 subminor = iminor(inode);
243 /* prevent disconnects */
244 down(&disconnect_sem);
246 interface = usb_find_interface(&cpcusb_driver, subminor);
248 err("%s - error, can't find device for minor %d",
250 retval = CPC_ERR_NO_INTERFACE_PRESENT;
254 card = usb_get_intfdata(interface);
256 retval = CPC_ERR_NO_INTERFACE_PRESENT;
260 /* lock this device */
263 /* increment our usage count for the driver */
265 dbg("device already opened");
266 retval = CPC_ERR_CHANNEL_ALREADY_OPEN;
270 /* save our object in the file's private structure */
271 file->private_data = card;
272 for (j = 0; j < CPC_USB_URB_CNT; j++) {
273 usb_fill_bulk_urb(card->urbs[j].urb, card->udev,
274 usb_rcvbulkpipe(card->udev, card->num_bulk_in),
275 card->urbs[j].buffer, card->urbs[j].size,
276 cpcusb_read_bulk_callback, card);
278 retval = usb_submit_urb(card->urbs[j].urb, GFP_KERNEL);
281 err("%s - failed submitting read urb, error %d",
283 retval = CPC_ERR_TRANSMISSION_FAILED;
288 info("%s - %d URB's submitted", __func__, j);
290 ResetBuffer(card->chan);
292 cpcusb_setup_intrep(card);
295 atomic_inc(&useCount);
298 /* unlock this device */
307 static unsigned int cpcusb_poll(struct file *file, poll_table * wait)
309 CPC_USB_T *card = (CPC_USB_T *) file->private_data;
310 unsigned int retval = 0;
313 err("%s - device object lost", __func__);
317 poll_wait(file, card->chan->CPCWait_q, wait);
319 if (IsBufferNotEmpty(card->chan) || !(card->present))
320 retval |= (POLLIN | POLLRDNORM);
322 if (card->free_slots)
323 retval |= (POLLOUT | POLLWRNORM);
328 static int cpcusb_release(struct inode *inode, struct file *file)
330 CPC_USB_T *card = (CPC_USB_T *) file->private_data;
334 dbg("%s - object is NULL", __func__);
335 return CPC_ERR_NO_INTERFACE_PRESENT;
338 /* lock our device */
342 dbg("%s - device not opened", __func__);
343 retval = CPC_ERR_NO_INTERFACE_PRESENT;
344 goto exit_not_opened;
347 /* if device wasn't unplugged kill all urbs */
350 for (j = 0; j < CPC_USB_URB_CNT; j++) {
351 usb_kill_urb(card->urbs[j].urb);
355 usb_kill_urb(card->intr_in_urb);
357 /* kill write urbs */
358 for (j = 0; j < CPC_USB_URB_CNT; j++) {
359 if (atomic_read(&card->wrUrbs[j].busy)) {
360 usb_kill_urb(card->wrUrbs[j].urb);
361 wait_for_completion(&card->wrUrbs[j].finished);
366 atomic_dec(&useCount);
368 /* last process detached */
369 if (atomic_read(&useCount) == 0) {
373 if (!card->present && card->open) {
374 /* the device was unplugged before the file was released */
388 static ssize_t cpcusb_read(struct file *file, char *buffer, size_t count,
391 CPC_USB_T *card = (CPC_USB_T *) file->private_data;
395 if (count < sizeof(CPC_MSG_T))
396 return CPC_ERR_UNKNOWN;
398 /* check if can read from the given address */
399 if (!access_ok(VERIFY_WRITE, buffer, count))
400 return CPC_ERR_UNKNOWN;
402 /* lock this object */
405 /* verify that the device wasn't unplugged */
406 if (!card->present) {
408 return CPC_ERR_NO_INTERFACE_PRESENT;
411 if (IsBufferEmpty(card->chan)) {
417 /* convert LPC2119 params back to SJA1000 params */
418 if (card->deviceRevision >= 0x0200
419 && chan->buf[chan->oidx].type == CPC_MSG_T_CAN_PRMS) {
420 LPC2119_TO_SJA1000_Params(&chan->buf[chan->oidx]);
424 if (copy_to_user(buffer, &chan->buf[chan->oidx], count) != 0) {
425 retval = CPC_ERR_IO_TRANSFER;
427 chan->oidx = (chan->oidx + 1) % CPC_MSG_BUF_CNT;
429 retval = sizeof(CPC_MSG_T);
432 /* spin_unlock_irqrestore(&card->slock, flags); */
434 /* unlock the device */
441 static inline void cpcusb_align_buffer_alignment(unsigned char *buf)
443 /* CPC-USB uploads packed bytes. */
444 CPC_MSG_T *cpc = (CPC_MSG_T *) buf;
447 for (i = 0; i < cpc->length + (2 * sizeof(unsigned long)); i++) {
448 ((unsigned char *) &cpc->msgid)[1 + i] =
449 ((unsigned char *) &cpc->msgid)[1 + SHIFT + i];
453 static int cpc_get_buffer_count(CPC_CHAN_T *chan)
455 /* check the buffer parameters */
456 if (chan->iidx == chan->oidx)
457 return !chan->WnR ? CPC_MSG_BUF_CNT : 0;
458 else if (chan->iidx >= chan->oidx)
459 return (chan->iidx - chan->oidx) % CPC_MSG_BUF_CNT;
461 return (chan->iidx + CPC_MSG_BUF_CNT - chan->oidx) % CPC_MSG_BUF_CNT;
464 static ssize_t cpcusb_write(struct file *file, const char *buffer,
465 size_t count, loff_t *ppos)
467 CPC_USB_T *card = (CPC_USB_T *) file->private_data;
468 CPC_USB_WRITE_URB_T *wrUrb = NULL;
470 ssize_t bytes_written = 0;
474 unsigned char *obuf = NULL;
475 unsigned char type = 0;
476 CPC_MSG_T *info = NULL;
478 dbg("%s - entered minor %d, count = %zu, present = %d",
479 __func__, card->minor, count, card->present);
481 if (count > sizeof(CPC_MSG_T))
482 return CPC_ERR_UNKNOWN;
484 /* check if can read from the given address */
485 if (!access_ok(VERIFY_READ, buffer, count))
486 return CPC_ERR_UNKNOWN;
488 /* lock this object */
491 /* verify that the device wasn't unplugged */
492 if (!card->present) {
493 retval = CPC_ERR_NO_INTERFACE_PRESENT;
497 /* verify that we actually have some data to write */
499 dbg("%s - write request of 0 bytes", __func__);
503 if (card->free_slots <= 5) {
504 info = (CPC_MSG_T *) buffer;
506 if (info->type != CPC_CMD_T_CLEAR_CMD_QUEUE
507 || card->free_slots <= 0) {
508 dbg("%s - send buffer full please try again %d",
509 __func__, card->free_slots);
510 retval = CPC_ERR_CAN_NO_TRANSMIT_BUF;
515 /* Find a free write urb */
516 for (j = 0; j < CPC_USB_URB_CNT; j++) {
517 if (!atomic_read(&card->wrUrbs[j].busy)) {
518 wrUrb = &card->wrUrbs[j]; /* remember found URB */
519 atomic_set(&wrUrb->busy, 1); /* lock this URB */
520 init_completion(&wrUrb->finished); /* init completion */
521 dbg("WR URB no. %d started", j);
526 /* don't found write urb say error */
528 dbg("%s - no free send urb available", __func__);
529 retval = CPC_ERR_CAN_NO_TRANSMIT_BUF;
532 dbg("URB write req");
534 obuf = (unsigned char *) wrUrb->urb->transfer_buffer;
536 /* copy the data from userspace into our transfer buffer;
537 * this is the only copy required.
539 if (copy_from_user(&obuf[4], buffer, count) != 0) {
540 atomic_set(&wrUrb->busy, 0); /* release urb */
541 retval = CPC_ERR_IO_TRANSFER;
545 /* check if it is a DRIVER information message, so we can
546 * response to that message and not the USB
548 info = (CPC_MSG_T *) &obuf[4];
550 bytes_written = 11 + info->length;
551 if (bytes_written >= wrUrb->size) {
552 retval = CPC_ERR_IO_TRANSFER;
556 switch (info->type) {
557 case CPC_CMD_T_CLEAR_MSG_QUEUE:
558 ResetBuffer(card->chan);
561 case CPC_CMD_T_INQ_MSG_QUEUE_CNT:
562 retval = cpc_get_buffer_count(card->chan);
563 atomic_set(&wrUrb->busy, 0);
567 case CPC_CMD_T_INQ_INFO:
568 if (info->msg.info.source == CPC_INFOMSG_T_DRIVER) {
569 /* release urb cause we'll use it for driver
572 atomic_set(&wrUrb->busy, 0);
573 if (IsBufferFull(card->chan)) {
574 retval = CPC_ERR_IO_TRANSFER;
578 /* it is a driver information request message and we have
579 * free rx slots to store the response
581 type = info->msg.info.type;
582 info = &card->chan->buf[card->chan->iidx];
584 info->type = CPC_MSG_T_INFO;
585 info->msg.info.source = CPC_INFOMSG_T_DRIVER;
586 info->msg.info.type = type;
589 case CPC_INFOMSG_T_VERSION:
590 info->length = strlen(CPC_DRIVER_VERSION) + 2;
591 sprintf(info->msg.info.msg, "%s\n",
595 case CPC_INFOMSG_T_SERIAL:
596 info->length = strlen(CPC_DRIVER_SERIAL) + 2;
597 sprintf(info->msg.info.msg, "%s\n",
603 info->msg.info.type =
604 CPC_INFOMSG_T_UNKNOWN_TYPE;
609 (card->chan->iidx + 1) % CPC_MSG_BUF_CNT;
611 retval = info->length;
615 case CPC_CMD_T_CAN_PRMS:
616 /* Check the controller type. If it's the new CPC-USB, make sure if these are SJA1000 params */
617 if (info->msg.canparams.cc_type != SJA1000
618 && info->msg.canparams.cc_type != M16C_BASIC
619 && (card->productId == USB_CPCUSB_LPC2119_PRODUCT_ID
620 && info->msg.canparams.cc_type != SJA1000)) {
621 /* don't forget to release the urb */
622 atomic_set(&wrUrb->busy, 0);
623 retval = CPC_ERR_WRONG_CONTROLLER_TYPE;
629 /* just convert the params if it is an old CPC-USB with M16C controller */
630 if (card->productId == USB_CPCUSB_M16C_PRODUCT_ID) {
631 /* if it is a parameter message convert it from SJA1000 controller
632 * settings to M16C Basic controller settings
634 SJA1000_TO_M16C_BASIC_Params((CPC_MSG_T *) &obuf[4]);
637 /* don't forget the byte alignment */
638 cpcusb_align_buffer_alignment(&obuf[4]);
640 /* setup a the 4 byte header */
641 obuf[0] = obuf[1] = obuf[2] = obuf[3] = 0;
643 /* this urb was already set up, except for this write size */
644 wrUrb->urb->transfer_buffer_length = bytes_written + 4;
646 /* send the data out the bulk port */
647 /* a character device write uses GFP_KERNEL,
648 unless a spinlock is held */
649 retval = usb_submit_urb(wrUrb->urb, GFP_KERNEL);
651 atomic_set(&wrUrb->busy, 0); /* release urb */
652 err("%s - failed submitting write urb, error %d",
655 retval = bytes_written;
659 /* unlock the device */
662 dbg("%s - leaved", __func__);
668 * callback for interrupt IN urb
670 static void cpcusb_read_interrupt_callback(struct urb *urb)
672 CPC_USB_T *card = (CPC_USB_T *) urb->context;
676 spin_lock_irqsave(&card->slock, flags);
678 if (!card->present) {
679 spin_unlock_irqrestore(&card->slock, flags);
680 info("%s - no such device", __func__);
684 switch (urb->status) {
685 case 0: /* success */
686 card->free_slots = card->intr_in_buffer[1];
692 spin_unlock_irqrestore(&card->slock, flags);
693 dbg("%s - intr urb killed", __func__);
696 info("%s - nonzero urb status %d", __func__, urb->status);
700 retval = usb_submit_urb(urb, GFP_ATOMIC);
702 err("%s - failed resubmitting intr urb, error %d",
706 spin_unlock_irqrestore(&card->slock, flags);
707 wake_up_interruptible(card->chan->CPCWait_q);
713 #define CPCMSG_HEADER_LEN_FIRMWARE 11
714 static inline int cpcusb_unalign_and_copy_buffy(unsigned char *out,
719 for (i = 0; i < 3; i++)
722 for (j = 0; j < (in[1] + (CPCMSG_HEADER_LEN_FIRMWARE - 3)); j++)
723 out[j + i + UN_SHIFT] = in[j + i];
729 * callback for bulk IN urb
731 static void cpcusb_read_bulk_callback(struct urb *urb)
733 CPC_USB_T *card = (CPC_USB_T *) urb->context;
735 unsigned char *ibuf = urb->transfer_buffer;
736 int retval, msgCnt, start, again = 0;
740 err("%s - device object lost", __func__);
744 spin_lock_irqsave(&card->slock, flags);
746 if (!card->present) {
747 spin_unlock_irqrestore(&card->slock, flags);
748 info("%s - no such device", __func__);
752 switch (urb->status) {
753 case 0: /* success */
759 spin_unlock_irqrestore(&card->slock, flags);
760 dbg("%s - read urb killed", __func__);
763 info("%s - nonzero urb status %d", __func__, urb->status);
767 if (urb->actual_length) {
768 msgCnt = ibuf[0] & ~0x80;
769 again = ibuf[0] & 0x80;
771 /* we have a 4 byte header */
775 if (!(IsBufferFull(card->chan))) {
777 cpcusb_unalign_and_copy_buffy((unsigned char *)
778 &chan->buf[chan->iidx], &ibuf[start]);
780 if (start > urb->transfer_buffer_length) {
781 err("%d > %d", start, urb->transfer_buffer_length);
786 chan->iidx = (chan->iidx + 1) % CPC_MSG_BUF_CNT;
794 usb_fill_bulk_urb(urb, card->udev,
795 usb_rcvbulkpipe(card->udev, card->num_bulk_in),
796 urb->transfer_buffer,
797 urb->transfer_buffer_length,
798 cpcusb_read_bulk_callback, card);
800 retval = usb_submit_urb(urb, GFP_ATOMIC);
803 err("%s - failed resubmitting read urb, error %d", __func__, retval);
806 spin_unlock_irqrestore(&card->slock, flags);
808 wake_up_interruptible(card->chan->CPCWait_q);
812 * callback for bulk IN urb
814 static void cpcusb_write_bulk_callback(struct urb *urb)
816 CPC_USB_T *card = (CPC_USB_T *) urb->context;
820 spin_lock_irqsave(&card->slock, flags);
823 for (j = 0; j < CPC_USB_URB_CNT; j++) {
824 if (card->wrUrbs[j].urb == urb) {
825 dbg("URB found no. %d", j);
826 /* notify anyone waiting that the write has finished */
827 complete(&card->wrUrbs[j].finished);
828 atomic_set(&card->wrUrbs[j].busy, 0);
833 switch (urb->status) {
834 case 0: /* success */
840 spin_unlock_irqrestore(&card->slock, flags);
841 dbg("%s - write urb no. %d killed", __func__, j);
844 info("%s - nonzero urb status %d", __func__, urb->status);
848 spin_unlock_irqrestore(&card->slock, flags);
850 wake_up_interruptible(card->chan->CPCWait_q);
853 static inline int cpcusb_get_free_slot(void)
857 for (i = 0; i < CPC_USB_CARD_CNT; i++) {
858 if (!CPCUSB_Table[i])
866 * probe function for new CPC-USB devices
868 static int cpcusb_probe(struct usb_interface *interface,
869 const struct usb_device_id *id)
871 CPC_USB_T *card = NULL;
872 CPC_CHAN_T *chan = NULL;
874 struct usb_device *udev = interface_to_usbdev(interface);
875 struct usb_host_interface *iface_desc;
876 struct usb_endpoint_descriptor *endpoint;
878 int i, j, retval = -ENOMEM, slot;
880 slot = cpcusb_get_free_slot();
882 info("No more devices supported");
886 /* allocate memory for our device state and initialize it */
887 card = kzalloc(sizeof(CPC_USB_T), GFP_KERNEL);
889 err("Out of memory");
892 CPCUSB_Table[slot] = card;
894 /* allocate and initialize the channel struct */
895 card->chan = kmalloc(sizeof(CPC_CHAN_T), GFP_KERNEL);
898 err("Out of memory");
903 memset(chan, 0, sizeof(CPC_CHAN_T));
906 init_MUTEX(&card->sem);
907 spin_lock_init(&card->slock);
910 card->interface = interface;
911 if (udev->descriptor.iSerialNumber) {
912 usb_string(udev, udev->descriptor.iSerialNumber, card->serialNumber,
914 info("Serial %s", card->serialNumber);
917 card->productId = udev->descriptor.idProduct;
919 card->productId == USB_CPCUSB_LPC2119_PRODUCT_ID ?
920 "CPC-USB/ARM7" : "CPC-USB/M16C");
922 /* set up the endpoint information */
923 /* check out the endpoints */
924 /* use only the first bulk-in and bulk-out endpoints */
925 iface_desc = &interface->altsetting[0];
926 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
927 endpoint = &iface_desc->endpoint[i].desc;
929 if (!card->num_intr_in &&
930 (endpoint->bEndpointAddress & USB_DIR_IN) &&
931 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
932 == USB_ENDPOINT_XFER_INT)) {
933 card->intr_in_urb = usb_alloc_urb(0, GFP_KERNEL);
934 card->num_intr_in = 1;
936 if (!card->intr_in_urb) {
937 err("No free urbs available");
941 dbg("intr_in urb %d", card->num_intr_in);
944 if (!card->num_bulk_in &&
945 (endpoint->bEndpointAddress & USB_DIR_IN) &&
946 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
947 == USB_ENDPOINT_XFER_BULK)) {
948 card->num_bulk_in = 2;
949 for (j = 0; j < CPC_USB_URB_CNT; j++) {
950 card->urbs[j].size = endpoint->wMaxPacketSize;
951 card->urbs[j].urb = usb_alloc_urb(0, GFP_KERNEL);
952 if (!card->urbs[j].urb) {
953 err("No free urbs available");
956 card->urbs[j].buffer =
957 usb_buffer_alloc(udev,
960 &card->urbs[j].urb->transfer_dma);
961 if (!card->urbs[j].buffer) {
962 err("Couldn't allocate bulk_in_buffer");
966 info("%s - %d reading URB's allocated",
967 __func__, CPC_USB_URB_CNT);
970 if (!card->num_bulk_out &&
971 !(endpoint->bEndpointAddress & USB_DIR_IN) &&
972 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
973 == USB_ENDPOINT_XFER_BULK)) {
975 card->num_bulk_out = 2;
977 for (j = 0; j < CPC_USB_URB_CNT; j++) {
978 card->wrUrbs[j].size =
979 endpoint->wMaxPacketSize;
980 card->wrUrbs[j].urb =
981 usb_alloc_urb(0, GFP_KERNEL);
982 if (!card->wrUrbs[j].urb) {
983 err("No free urbs available");
986 card->wrUrbs[j].buffer = usb_buffer_alloc(udev,
987 card->wrUrbs[j].size, GFP_KERNEL,
988 &card->wrUrbs[j].urb->transfer_dma);
990 if (!card->wrUrbs[j].buffer) {
991 err("Couldn't allocate bulk_out_buffer");
995 usb_fill_bulk_urb(card->wrUrbs[j].urb, udev,
996 usb_sndbulkpipe(udev, endpoint->bEndpointAddress),
997 card->wrUrbs[j].buffer,
998 card->wrUrbs[j].size,
999 cpcusb_write_bulk_callback,
1003 info("%s - %d writing URB's allocated", __func__, CPC_USB_URB_CNT);
1007 if (!(card->num_bulk_in && card->num_bulk_out)) {
1008 err("Couldn't find both bulk-in and bulk-out endpoints");
1012 /* allow device read, write and ioctl */
1015 /* we can register the device now, as it is ready */
1016 usb_set_intfdata(interface, card);
1017 retval = usb_register_dev(interface, &cpcusb_class);
1020 /* something prevented us from registering this driver */
1021 err("Not able to get a minor for this device.");
1022 usb_set_intfdata(interface, NULL);
1026 card->chan->minor = card->minor = interface->minor;
1028 chan->buf = vmalloc(sizeof(CPC_MSG_T) * CPC_MSG_BUF_CNT);
1029 if (chan->buf == NULL) {
1030 err("Out of memory");
1034 info("Allocated memory for %d messages (%lu kbytes)",
1035 CPC_MSG_BUF_CNT, (long unsigned int)(sizeof(CPC_MSG_T) * CPC_MSG_BUF_CNT) / 1000);
1036 memset(chan->buf, 0, sizeof(CPC_MSG_T) * CPC_MSG_BUF_CNT);
1040 card->chan->CPCWait_q = kmalloc(sizeof(wait_queue_head_t), GFP_KERNEL);
1041 if (!card->chan->CPCWait_q) {
1042 err("Out of memory");
1046 init_waitqueue_head(card->chan->CPCWait_q);
1048 CPCUSB_Table[slot] = card;
1052 /* let the user know what node this device is now attached to */
1053 info("Device now attached to USB-%d", card->minor);
1057 for (j = 0; j < CPC_USB_URB_CNT; j++) {
1058 if (card->urbs[j].buffer) {
1059 usb_buffer_free(card->udev, card->urbs[j].size,
1060 card->urbs[j].buffer,
1061 card->urbs[j].urb->transfer_dma);
1062 card->urbs[j].buffer = NULL;
1064 if (card->urbs[j].urb) {
1065 usb_free_urb(card->urbs[j].urb);
1066 card->urbs[j].urb = NULL;
1070 cpcusb_delete(card);
1075 * called by the usb core when the device is removed from the system
1077 static void cpcusb_disconnect(struct usb_interface *interface)
1079 CPC_USB_T *card = NULL;
1082 /* prevent races with open() */
1083 down(&disconnect_sem);
1085 card = usb_get_intfdata(interface);
1086 usb_set_intfdata(interface, NULL);
1090 /* prevent device read, write and ioctl */
1093 minor = card->minor;
1095 /* free all urbs and their buffers */
1096 for (j = 0; j < CPC_USB_URB_CNT; j++) {
1097 /* terminate an ongoing write */
1098 if (atomic_read(&card->wrUrbs[j].busy)) {
1099 usb_kill_urb(card->wrUrbs[j].urb);
1100 wait_for_completion(&card->wrUrbs[j].finished);
1102 usb_buffer_free(card->udev, card->wrUrbs[j].size,
1103 card->wrUrbs[j].buffer,
1104 card->wrUrbs[j].urb->transfer_dma);
1105 usb_free_urb(card->wrUrbs[j].urb);
1107 info("%d write URBs freed", CPC_USB_URB_CNT);
1109 /* free all urbs and their buffers */
1110 for (j = 0; j < CPC_USB_URB_CNT; j++) {
1111 usb_buffer_free(card->udev, card->urbs[j].size,
1112 card->urbs[j].buffer,
1113 card->urbs[j].urb->transfer_dma);
1114 usb_free_urb(card->urbs[j].urb);
1116 info("%d read URBs freed", CPC_USB_URB_CNT);
1117 usb_free_urb(card->intr_in_urb);
1119 /* give back our minor */
1120 usb_deregister_dev(interface, &cpcusb_class);
1124 /* if the device is opened, cpcusb_release will clean this up */
1126 cpcusb_delete(card);
1128 wake_up_interruptible(card->chan->CPCWait_q);
1130 up(&disconnect_sem);
1133 info("USB-%d now disconnected", minor);
1136 static int __init CPCUsb_Init(void)
1140 info(DRIVER_DESC " v" DRIVER_VERSION);
1141 info("Build on " __DATE__ " at " __TIME__);
1143 for (i = 0; i < CPC_USB_CARD_CNT; i++)
1144 CPCUSB_Table[i] = 0;
1146 /* register this driver with the USB subsystem */
1147 result = usb_register(&cpcusb_driver);
1149 err("usb_register failed. Error number %d", result);
1153 procDir = proc_mkdir(CPC_USB_PROC_DIR, NULL);
1155 err("Could not create proc entry");
1157 procEntry = create_proc_read_entry("info", 0444, procDir,
1158 cpcusb_proc_read_info,
1161 err("Could not create proc entry %s", CPC_USB_PROC_DIR "/info");
1162 remove_proc_entry(CPC_USB_PROC_DIR, NULL);
1170 static void __exit CPCUsb_Exit(void)
1172 wait_event(rmmodWq, !atomic_read(&useCount));
1174 /* deregister this driver with the USB subsystem */
1175 usb_deregister(&cpcusb_driver);
1179 remove_proc_entry("info", procDir);
1180 remove_proc_entry(CPC_USB_PROC_DIR, NULL);
1184 module_init(CPCUsb_Init);
1185 module_exit(CPCUsb_Exit);