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>
32 #include <linux/proc_fs.h>
41 /* Version Information */
42 #define DRIVER_AUTHOR "Sebastian Haas <haas@ems-wuensche.com>"
43 #define DRIVER_DESC "CPC-USB Driver for Linux Kernel 2.6"
44 #define DRIVER_VERSION CPC_DRIVER_VERSION
46 MODULE_AUTHOR(DRIVER_AUTHOR);
47 MODULE_DESCRIPTION(DRIVER_DESC);
48 MODULE_VERSION(DRIVER_VERSION);
49 MODULE_LICENSE("GPL v2");
51 /* Define these values to match your devices */
52 #define USB_CPCUSB_VENDOR_ID 0x12D6
54 #define USB_CPCUSB_M16C_PRODUCT_ID 0x0888
55 #define USB_CPCUSB_LPC2119_PRODUCT_ID 0x0444
57 #define CPC_USB_PROC_DIR CPC_PROC_DIR "cpc-usb"
59 static struct proc_dir_entry *procDir;
60 static struct proc_dir_entry *procEntry;
62 /* Module parameters */
64 module_param(debug, int, S_IRUGO);
66 /* table of devices that work with this driver */
67 static struct usb_device_id cpcusb_table[] = {
68 {USB_DEVICE(USB_CPCUSB_VENDOR_ID, USB_CPCUSB_M16C_PRODUCT_ID)},
69 {USB_DEVICE(USB_CPCUSB_VENDOR_ID, USB_CPCUSB_LPC2119_PRODUCT_ID)},
70 {} /* Terminating entry */
73 MODULE_DEVICE_TABLE(usb, cpcusb_table);
75 /* use to prevent kernel panic if driver is unloaded
76 * while a programm has still open the device
78 DECLARE_WAIT_QUEUE_HEAD(rmmodWq);
81 static CPC_USB_T *CPCUSB_Table[CPC_USB_CARD_CNT] = { 0 };
82 static unsigned int CPCUsbCnt;
84 /* prevent races between open() and disconnect() */
85 static DECLARE_MUTEX(disconnect_sem);
87 /* local function prototypes */
88 static ssize_t cpcusb_read(struct file *file, char *buffer, size_t count,
90 static ssize_t cpcusb_write(struct file *file, const char *buffer,
91 size_t count, loff_t *ppos);
92 static unsigned int cpcusb_poll(struct file *file, poll_table * wait);
93 static int cpcusb_open(struct inode *inode, struct file *file);
94 static int cpcusb_release(struct inode *inode, struct file *file);
96 static int cpcusb_probe(struct usb_interface *interface,
97 const struct usb_device_id *id);
98 static void cpcusb_disconnect(struct usb_interface *interface);
100 static void cpcusb_read_bulk_callback(struct urb *urb);
101 static void cpcusb_write_bulk_callback(struct urb *urb);
102 static void cpcusb_read_interrupt_callback(struct urb *urb);
104 static int cpcusb_setup_intrep(CPC_USB_T *card);
106 static struct file_operations cpcusb_fops = {
108 * The owner field is part of the module-locking
109 * mechanism. The idea is that the kernel knows
110 * which module to increment the use-counter of
111 * BEFORE it calls the device's open() function.
112 * This also means that the kernel can decrement
113 * the use-counter again before calling release()
114 * or should the open() function fail.
116 .owner = THIS_MODULE,
119 .write = cpcusb_write,
122 .release = cpcusb_release,
126 * usb class driver info in order to get a minor number from the usb core,
127 * and to have the device registered with devfs and the driver core
129 static struct usb_class_driver cpcusb_class = {
130 .name = "usb/cpc_usb%d",
131 .fops = &cpcusb_fops,
132 .minor_base = CPC_USB_BASE_MNR,
135 /* usb specific object needed to register this driver with the usb subsystem */
136 static struct usb_driver cpcusb_driver = {
138 .probe = cpcusb_probe,
139 .disconnect = cpcusb_disconnect,
140 .id_table = cpcusb_table,
143 static int cpcusb_create_info_output(char *buf)
147 for (j = 0; j < CPC_USB_CARD_CNT; j++) {
148 if (CPCUSB_Table[j]) {
149 CPC_USB_T *card = CPCUSB_Table[j];
150 CPC_CHAN_T *chan = card->chan;
152 /* MINOR CHANNELNO BUSNO SLOTNO */
153 i += sprintf(&buf[i], "%d %s\n", chan->minor,
161 static int cpcusb_proc_read_info(char *page, char **start, off_t off,
162 int count, int *eof, void *data)
164 int len = cpcusb_create_info_output(page);
166 if (len <= off + count)
179 * Remove CPC-USB and cleanup
181 static inline void cpcusb_delete(CPC_USB_T *card)
186 vfree(card->chan->buf);
188 if (card->chan->CPCWait_q)
189 kfree(card->chan->CPCWait_q);
194 CPCUSB_Table[card->idx] = NULL;
200 * setup the interrupt IN endpoint of a specific CPC-USB device
202 static int cpcusb_setup_intrep(CPC_USB_T *card)
205 struct usb_endpoint_descriptor *ep;
207 ep = &card->interface->altsetting[0].endpoint[card->num_intr_in].desc;
209 card->intr_in_buffer[0] = 0;
210 card->free_slots = 15; /* initial size */
213 usb_fill_int_urb(card->intr_in_urb, card->udev,
214 usb_rcvintpipe(card->udev, card->num_intr_in),
215 card->intr_in_buffer,
216 sizeof(card->intr_in_buffer),
217 cpcusb_read_interrupt_callback,
221 card->intr_in_urb->status = 0; /* needed! */
224 retval = usb_submit_urb(card->intr_in_urb, GFP_KERNEL);
227 err("%s - failed submitting intr urb, error %d", __func__,
233 static int cpcusb_open(struct inode *inode, struct file *file)
235 CPC_USB_T *card = NULL;
236 struct usb_interface *interface;
240 subminor = iminor(inode);
242 /* prevent disconnects */
243 down(&disconnect_sem);
245 interface = usb_find_interface(&cpcusb_driver, subminor);
247 err("%s - error, can't find device for minor %d",
249 retval = CPC_ERR_NO_INTERFACE_PRESENT;
253 card = usb_get_intfdata(interface);
255 retval = CPC_ERR_NO_INTERFACE_PRESENT;
259 /* lock this device */
262 /* increment our usage count for the driver */
264 dbg("device already opened");
265 retval = CPC_ERR_CHANNEL_ALREADY_OPEN;
269 /* save our object in the file's private structure */
270 file->private_data = card;
271 for (j = 0; j < CPC_USB_URB_CNT; j++) {
272 usb_fill_bulk_urb(card->urbs[j].urb, card->udev,
273 usb_rcvbulkpipe(card->udev, card->num_bulk_in),
274 card->urbs[j].buffer, card->urbs[j].size,
275 cpcusb_read_bulk_callback, card);
277 retval = usb_submit_urb(card->urbs[j].urb, GFP_KERNEL);
280 err("%s - failed submitting read urb, error %d",
282 retval = CPC_ERR_TRANSMISSION_FAILED;
287 info("%s - %d URB's submitted", __func__, j);
289 ResetBuffer(card->chan);
291 cpcusb_setup_intrep(card);
294 atomic_inc(&useCount);
297 /* unlock this device */
306 static unsigned int cpcusb_poll(struct file *file, poll_table * wait)
308 CPC_USB_T *card = (CPC_USB_T *) file->private_data;
309 unsigned int retval = 0;
312 err("%s - device object lost", __func__);
316 poll_wait(file, card->chan->CPCWait_q, wait);
318 if (IsBufferNotEmpty(card->chan) || !(card->present))
319 retval |= (POLLIN | POLLRDNORM);
321 if (card->free_slots)
322 retval |= (POLLOUT | POLLWRNORM);
327 static int cpcusb_release(struct inode *inode, struct file *file)
329 CPC_USB_T *card = (CPC_USB_T *) file->private_data;
333 dbg("%s - object is NULL", __func__);
334 return CPC_ERR_NO_INTERFACE_PRESENT;
337 /* lock our device */
341 dbg("%s - device not opened", __func__);
342 retval = CPC_ERR_NO_INTERFACE_PRESENT;
343 goto exit_not_opened;
346 /* if device wasn't unplugged kill all urbs */
349 for (j = 0; j < CPC_USB_URB_CNT; j++) {
350 usb_kill_urb(card->urbs[j].urb);
354 usb_kill_urb(card->intr_in_urb);
356 /* kill write urbs */
357 for (j = 0; j < CPC_USB_URB_CNT; j++) {
358 if (atomic_read(&card->wrUrbs[j].busy)) {
359 usb_kill_urb(card->wrUrbs[j].urb);
360 wait_for_completion(&card->wrUrbs[j].finished);
365 atomic_dec(&useCount);
367 /* last process detached */
368 if (atomic_read(&useCount) == 0) {
372 if (!card->present && card->open) {
373 /* the device was unplugged before the file was released */
387 static ssize_t cpcusb_read(struct file *file, char *buffer, size_t count,
390 CPC_USB_T *card = (CPC_USB_T *) file->private_data;
394 if (count < sizeof(CPC_MSG_T))
395 return CPC_ERR_UNKNOWN;
397 /* check if can read from the given address */
398 if (!access_ok(VERIFY_WRITE, buffer, count))
399 return CPC_ERR_UNKNOWN;
401 /* lock this object */
404 /* verify that the device wasn't unplugged */
405 if (!card->present) {
407 return CPC_ERR_NO_INTERFACE_PRESENT;
410 if (IsBufferEmpty(card->chan)) {
416 /* convert LPC2119 params back to SJA1000 params */
417 if (card->deviceRevision >= 0x0200
418 && chan->buf[chan->oidx].type == CPC_MSG_T_CAN_PRMS) {
419 LPC2119_TO_SJA1000_Params(&chan->buf[chan->oidx]);
423 if (copy_to_user(buffer, &chan->buf[chan->oidx], count) != 0) {
424 retval = CPC_ERR_IO_TRANSFER;
426 chan->oidx = (chan->oidx + 1) % CPC_MSG_BUF_CNT;
428 retval = sizeof(CPC_MSG_T);
431 /* spin_unlock_irqrestore(&card->slock, flags); */
433 /* unlock the device */
440 static inline void cpcusb_align_buffer_alignment(unsigned char *buf)
442 /* CPC-USB uploads packed bytes. */
443 CPC_MSG_T *cpc = (CPC_MSG_T *) buf;
446 for (i = 0; i < cpc->length + (2 * sizeof(unsigned long)); i++) {
447 ((unsigned char *) &cpc->msgid)[1 + i] =
448 ((unsigned char *) &cpc->msgid)[1 + SHIFT + i];
452 static int cpc_get_buffer_count(CPC_CHAN_T *chan)
454 /* check the buffer parameters */
455 if (chan->iidx == chan->oidx)
456 return !chan->WnR ? CPC_MSG_BUF_CNT : 0;
457 else if (chan->iidx >= chan->oidx)
458 return (chan->iidx - chan->oidx) % CPC_MSG_BUF_CNT;
460 return (chan->iidx + CPC_MSG_BUF_CNT - chan->oidx) % CPC_MSG_BUF_CNT;
463 static ssize_t cpcusb_write(struct file *file, const char *buffer,
464 size_t count, loff_t *ppos)
466 CPC_USB_T *card = (CPC_USB_T *) file->private_data;
467 CPC_USB_WRITE_URB_T *wrUrb = NULL;
469 ssize_t bytes_written = 0;
473 unsigned char *obuf = NULL;
474 unsigned char type = 0;
475 CPC_MSG_T *info = NULL;
477 dbg("%s - entered minor %d, count = %zu, present = %d",
478 __func__, card->minor, count, card->present);
480 if (count > sizeof(CPC_MSG_T))
481 return CPC_ERR_UNKNOWN;
483 /* check if can read from the given address */
484 if (!access_ok(VERIFY_READ, buffer, count))
485 return CPC_ERR_UNKNOWN;
487 /* lock this object */
490 /* verify that the device wasn't unplugged */
491 if (!card->present) {
492 retval = CPC_ERR_NO_INTERFACE_PRESENT;
496 /* verify that we actually have some data to write */
498 dbg("%s - write request of 0 bytes", __func__);
502 if (card->free_slots <= 5) {
503 info = (CPC_MSG_T *) buffer;
505 if (info->type != CPC_CMD_T_CLEAR_CMD_QUEUE
506 || card->free_slots <= 0) {
507 dbg("%s - send buffer full please try again %d",
508 __func__, card->free_slots);
509 retval = CPC_ERR_CAN_NO_TRANSMIT_BUF;
514 /* Find a free write urb */
515 for (j = 0; j < CPC_USB_URB_CNT; j++) {
516 if (!atomic_read(&card->wrUrbs[j].busy)) {
517 wrUrb = &card->wrUrbs[j]; /* remember found URB */
518 atomic_set(&wrUrb->busy, 1); /* lock this URB */
519 init_completion(&wrUrb->finished); /* init completion */
520 dbg("WR URB no. %d started", j);
525 /* don't found write urb say error */
527 dbg("%s - no free send urb available", __func__);
528 retval = CPC_ERR_CAN_NO_TRANSMIT_BUF;
531 dbg("URB write req");
533 obuf = (unsigned char *) wrUrb->urb->transfer_buffer;
535 /* copy the data from userspace into our transfer buffer;
536 * this is the only copy required.
538 if (copy_from_user(&obuf[4], buffer, count) != 0) {
539 atomic_set(&wrUrb->busy, 0); /* release urb */
540 retval = CPC_ERR_IO_TRANSFER;
544 /* check if it is a DRIVER information message, so we can
545 * response to that message and not the USB
547 info = (CPC_MSG_T *) &obuf[4];
549 bytes_written = 11 + info->length;
550 if (bytes_written >= wrUrb->size) {
551 retval = CPC_ERR_IO_TRANSFER;
555 switch (info->type) {
556 case CPC_CMD_T_CLEAR_MSG_QUEUE:
557 ResetBuffer(card->chan);
560 case CPC_CMD_T_INQ_MSG_QUEUE_CNT:
561 retval = cpc_get_buffer_count(card->chan);
562 atomic_set(&wrUrb->busy, 0);
566 case CPC_CMD_T_INQ_INFO:
567 if (info->msg.info.source == CPC_INFOMSG_T_DRIVER) {
568 /* release urb cause we'll use it for driver
571 atomic_set(&wrUrb->busy, 0);
572 if (IsBufferFull(card->chan)) {
573 retval = CPC_ERR_IO_TRANSFER;
577 /* it is a driver information request message and we have
578 * free rx slots to store the response
580 type = info->msg.info.type;
581 info = &card->chan->buf[card->chan->iidx];
583 info->type = CPC_MSG_T_INFO;
584 info->msg.info.source = CPC_INFOMSG_T_DRIVER;
585 info->msg.info.type = type;
588 case CPC_INFOMSG_T_VERSION:
589 info->length = strlen(CPC_DRIVER_VERSION) + 2;
590 sprintf(info->msg.info.msg, "%s\n",
594 case CPC_INFOMSG_T_SERIAL:
595 info->length = strlen(CPC_DRIVER_SERIAL) + 2;
596 sprintf(info->msg.info.msg, "%s\n",
602 info->msg.info.type =
603 CPC_INFOMSG_T_UNKNOWN_TYPE;
608 (card->chan->iidx + 1) % CPC_MSG_BUF_CNT;
610 retval = info->length;
614 case CPC_CMD_T_CAN_PRMS:
615 /* Check the controller type. If it's the new CPC-USB, make sure if these are SJA1000 params */
616 if (info->msg.canparams.cc_type != SJA1000
617 && info->msg.canparams.cc_type != M16C_BASIC
618 && (card->productId == USB_CPCUSB_LPC2119_PRODUCT_ID
619 && info->msg.canparams.cc_type != SJA1000)) {
620 /* don't forget to release the urb */
621 atomic_set(&wrUrb->busy, 0);
622 retval = CPC_ERR_WRONG_CONTROLLER_TYPE;
628 /* just convert the params if it is an old CPC-USB with M16C controller */
629 if (card->productId == USB_CPCUSB_M16C_PRODUCT_ID) {
630 /* if it is a parameter message convert it from SJA1000 controller
631 * settings to M16C Basic controller settings
633 SJA1000_TO_M16C_BASIC_Params((CPC_MSG_T *) &obuf[4]);
636 /* don't forget the byte alignment */
637 cpcusb_align_buffer_alignment(&obuf[4]);
639 /* setup a the 4 byte header */
640 obuf[0] = obuf[1] = obuf[2] = obuf[3] = 0;
642 /* this urb was already set up, except for this write size */
643 wrUrb->urb->transfer_buffer_length = bytes_written + 4;
645 /* send the data out the bulk port */
646 /* a character device write uses GFP_KERNEL,
647 unless a spinlock is held */
648 retval = usb_submit_urb(wrUrb->urb, GFP_KERNEL);
650 atomic_set(&wrUrb->busy, 0); /* release urb */
651 err("%s - failed submitting write urb, error %d",
654 retval = bytes_written;
658 /* unlock the device */
661 dbg("%s - leaved", __func__);
667 * callback for interrupt IN urb
669 static void cpcusb_read_interrupt_callback(struct urb *urb)
671 CPC_USB_T *card = (CPC_USB_T *) urb->context;
675 spin_lock_irqsave(&card->slock, flags);
677 if (!card->present) {
678 spin_unlock_irqrestore(&card->slock, flags);
679 info("%s - no such device", __func__);
683 switch (urb->status) {
684 case 0: /* success */
685 card->free_slots = card->intr_in_buffer[1];
691 spin_unlock_irqrestore(&card->slock, flags);
692 dbg("%s - intr urb killed", __func__);
695 info("%s - nonzero urb status %d", __func__, urb->status);
699 retval = usb_submit_urb(urb, GFP_ATOMIC);
701 err("%s - failed resubmitting intr urb, error %d",
705 spin_unlock_irqrestore(&card->slock, flags);
706 wake_up_interruptible(card->chan->CPCWait_q);
712 #define CPCMSG_HEADER_LEN_FIRMWARE 11
713 static inline int cpcusb_unalign_and_copy_buffy(unsigned char *out,
718 for (i = 0; i < 3; i++)
721 for (j = 0; j < (in[1] + (CPCMSG_HEADER_LEN_FIRMWARE - 3)); j++)
722 out[j + i + UN_SHIFT] = in[j + i];
728 * callback for bulk IN urb
730 static void cpcusb_read_bulk_callback(struct urb *urb)
732 CPC_USB_T *card = (CPC_USB_T *) urb->context;
734 unsigned char *ibuf = urb->transfer_buffer;
735 int retval, msgCnt, start, again = 0;
739 err("%s - device object lost", __func__);
743 spin_lock_irqsave(&card->slock, flags);
745 if (!card->present) {
746 spin_unlock_irqrestore(&card->slock, flags);
747 info("%s - no such device", __func__);
751 switch (urb->status) {
752 case 0: /* success */
758 spin_unlock_irqrestore(&card->slock, flags);
759 dbg("%s - read urb killed", __func__);
762 info("%s - nonzero urb status %d", __func__, urb->status);
766 if (urb->actual_length) {
767 msgCnt = ibuf[0] & ~0x80;
768 again = ibuf[0] & 0x80;
770 /* we have a 4 byte header */
774 if (!(IsBufferFull(card->chan))) {
776 cpcusb_unalign_and_copy_buffy((unsigned char *)
777 &chan->buf[chan->iidx], &ibuf[start]);
779 if (start > urb->transfer_buffer_length) {
780 err("%d > %d", start, urb->transfer_buffer_length);
785 chan->iidx = (chan->iidx + 1) % CPC_MSG_BUF_CNT;
793 usb_fill_bulk_urb(urb, card->udev,
794 usb_rcvbulkpipe(card->udev, card->num_bulk_in),
795 urb->transfer_buffer,
796 urb->transfer_buffer_length,
797 cpcusb_read_bulk_callback, card);
799 retval = usb_submit_urb(urb, GFP_ATOMIC);
802 err("%s - failed resubmitting read urb, error %d", __func__, retval);
805 spin_unlock_irqrestore(&card->slock, flags);
807 wake_up_interruptible(card->chan->CPCWait_q);
811 * callback for bulk IN urb
813 static void cpcusb_write_bulk_callback(struct urb *urb)
815 CPC_USB_T *card = (CPC_USB_T *) urb->context;
819 spin_lock_irqsave(&card->slock, flags);
822 for (j = 0; j < CPC_USB_URB_CNT; j++) {
823 if (card->wrUrbs[j].urb == urb) {
824 dbg("URB found no. %d", j);
825 /* notify anyone waiting that the write has finished */
826 complete(&card->wrUrbs[j].finished);
827 atomic_set(&card->wrUrbs[j].busy, 0);
832 switch (urb->status) {
833 case 0: /* success */
839 spin_unlock_irqrestore(&card->slock, flags);
840 dbg("%s - write urb no. %d killed", __func__, j);
843 info("%s - nonzero urb status %d", __func__, urb->status);
847 spin_unlock_irqrestore(&card->slock, flags);
849 wake_up_interruptible(card->chan->CPCWait_q);
852 static inline int cpcusb_get_free_slot(void)
856 for (i = 0; i < CPC_USB_CARD_CNT; i++) {
857 if (!CPCUSB_Table[i])
865 * probe function for new CPC-USB devices
867 static int cpcusb_probe(struct usb_interface *interface,
868 const struct usb_device_id *id)
870 CPC_USB_T *card = NULL;
871 CPC_CHAN_T *chan = NULL;
873 struct usb_device *udev = interface_to_usbdev(interface);
874 struct usb_host_interface *iface_desc;
875 struct usb_endpoint_descriptor *endpoint;
877 int i, j, retval = -ENOMEM, slot;
879 slot = cpcusb_get_free_slot();
881 info("No more devices supported");
885 /* allocate memory for our device state and initialize it */
886 card = kzalloc(sizeof(CPC_USB_T), GFP_KERNEL);
888 err("Out of memory");
891 CPCUSB_Table[slot] = card;
893 /* allocate and initialize the channel struct */
894 card->chan = kmalloc(sizeof(CPC_CHAN_T), GFP_KERNEL);
897 err("Out of memory");
902 memset(chan, 0, sizeof(CPC_CHAN_T));
905 init_MUTEX(&card->sem);
906 spin_lock_init(&card->slock);
909 card->interface = interface;
910 if (udev->descriptor.iSerialNumber) {
911 usb_string(udev, udev->descriptor.iSerialNumber, card->serialNumber,
913 info("Serial %s", card->serialNumber);
916 card->productId = udev->descriptor.idProduct;
918 card->productId == USB_CPCUSB_LPC2119_PRODUCT_ID ?
919 "CPC-USB/ARM7" : "CPC-USB/M16C");
921 /* set up the endpoint information */
922 /* check out the endpoints */
923 /* use only the first bulk-in and bulk-out endpoints */
924 iface_desc = &interface->altsetting[0];
925 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
926 endpoint = &iface_desc->endpoint[i].desc;
928 if (!card->num_intr_in &&
929 (endpoint->bEndpointAddress & USB_DIR_IN) &&
930 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
931 == USB_ENDPOINT_XFER_INT)) {
932 card->intr_in_urb = usb_alloc_urb(0, GFP_KERNEL);
933 card->num_intr_in = 1;
935 if (!card->intr_in_urb) {
936 err("No free urbs available");
940 dbg("intr_in urb %d", card->num_intr_in);
943 if (!card->num_bulk_in &&
944 (endpoint->bEndpointAddress & USB_DIR_IN) &&
945 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
946 == USB_ENDPOINT_XFER_BULK)) {
947 card->num_bulk_in = 2;
948 for (j = 0; j < CPC_USB_URB_CNT; j++) {
949 card->urbs[j].size = endpoint->wMaxPacketSize;
950 card->urbs[j].urb = usb_alloc_urb(0, GFP_KERNEL);
951 if (!card->urbs[j].urb) {
952 err("No free urbs available");
955 card->urbs[j].buffer =
956 usb_buffer_alloc(udev,
959 &card->urbs[j].urb->transfer_dma);
960 if (!card->urbs[j].buffer) {
961 err("Couldn't allocate bulk_in_buffer");
965 info("%s - %d reading URB's allocated",
966 __func__, CPC_USB_URB_CNT);
969 if (!card->num_bulk_out &&
970 !(endpoint->bEndpointAddress & USB_DIR_IN) &&
971 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
972 == USB_ENDPOINT_XFER_BULK)) {
974 card->num_bulk_out = 2;
976 for (j = 0; j < CPC_USB_URB_CNT; j++) {
977 card->wrUrbs[j].size =
978 endpoint->wMaxPacketSize;
979 card->wrUrbs[j].urb =
980 usb_alloc_urb(0, GFP_KERNEL);
981 if (!card->wrUrbs[j].urb) {
982 err("No free urbs available");
985 card->wrUrbs[j].buffer = usb_buffer_alloc(udev,
986 card->wrUrbs[j].size, GFP_KERNEL,
987 &card->wrUrbs[j].urb->transfer_dma);
989 if (!card->wrUrbs[j].buffer) {
990 err("Couldn't allocate bulk_out_buffer");
994 usb_fill_bulk_urb(card->wrUrbs[j].urb, udev,
995 usb_sndbulkpipe(udev, endpoint->bEndpointAddress),
996 card->wrUrbs[j].buffer,
997 card->wrUrbs[j].size,
998 cpcusb_write_bulk_callback,
1002 info("%s - %d writing URB's allocated", __func__, CPC_USB_URB_CNT);
1006 if (!(card->num_bulk_in && card->num_bulk_out)) {
1007 err("Couldn't find both bulk-in and bulk-out endpoints");
1011 /* allow device read, write and ioctl */
1014 /* we can register the device now, as it is ready */
1015 usb_set_intfdata(interface, card);
1016 retval = usb_register_dev(interface, &cpcusb_class);
1019 /* something prevented us from registering this driver */
1020 err("Not able to get a minor for this device.");
1021 usb_set_intfdata(interface, NULL);
1025 card->chan->minor = card->minor = interface->minor;
1027 chan->buf = vmalloc(sizeof(CPC_MSG_T) * CPC_MSG_BUF_CNT);
1028 if (chan->buf == NULL) {
1029 err("Out of memory");
1033 info("Allocated memory for %d messages (%lu kbytes)",
1034 CPC_MSG_BUF_CNT, (long unsigned int)(sizeof(CPC_MSG_T) * CPC_MSG_BUF_CNT) / 1000);
1035 memset(chan->buf, 0, sizeof(CPC_MSG_T) * CPC_MSG_BUF_CNT);
1039 card->chan->CPCWait_q = kmalloc(sizeof(wait_queue_head_t), GFP_KERNEL);
1040 if (!card->chan->CPCWait_q) {
1041 err("Out of memory");
1045 init_waitqueue_head(card->chan->CPCWait_q);
1047 CPCUSB_Table[slot] = card;
1051 /* let the user know what node this device is now attached to */
1052 info("Device now attached to USB-%d", card->minor);
1056 for (j = 0; j < CPC_USB_URB_CNT; j++) {
1057 if (card->urbs[j].buffer) {
1058 usb_buffer_free(card->udev, card->urbs[j].size,
1059 card->urbs[j].buffer,
1060 card->urbs[j].urb->transfer_dma);
1061 card->urbs[j].buffer = NULL;
1063 if (card->urbs[j].urb) {
1064 usb_free_urb(card->urbs[j].urb);
1065 card->urbs[j].urb = NULL;
1069 cpcusb_delete(card);
1074 * called by the usb core when the device is removed from the system
1076 static void cpcusb_disconnect(struct usb_interface *interface)
1078 CPC_USB_T *card = NULL;
1081 /* prevent races with open() */
1082 down(&disconnect_sem);
1084 card = usb_get_intfdata(interface);
1085 usb_set_intfdata(interface, NULL);
1089 /* prevent device read, write and ioctl */
1092 minor = card->minor;
1094 /* free all urbs and their buffers */
1095 for (j = 0; j < CPC_USB_URB_CNT; j++) {
1096 /* terminate an ongoing write */
1097 if (atomic_read(&card->wrUrbs[j].busy)) {
1098 usb_kill_urb(card->wrUrbs[j].urb);
1099 wait_for_completion(&card->wrUrbs[j].finished);
1101 usb_buffer_free(card->udev, card->wrUrbs[j].size,
1102 card->wrUrbs[j].buffer,
1103 card->wrUrbs[j].urb->transfer_dma);
1104 usb_free_urb(card->wrUrbs[j].urb);
1106 info("%d write URBs freed", CPC_USB_URB_CNT);
1108 /* free all urbs and their buffers */
1109 for (j = 0; j < CPC_USB_URB_CNT; j++) {
1110 usb_buffer_free(card->udev, card->urbs[j].size,
1111 card->urbs[j].buffer,
1112 card->urbs[j].urb->transfer_dma);
1113 usb_free_urb(card->urbs[j].urb);
1115 info("%d read URBs freed", CPC_USB_URB_CNT);
1116 usb_free_urb(card->intr_in_urb);
1118 /* give back our minor */
1119 usb_deregister_dev(interface, &cpcusb_class);
1123 /* if the device is opened, cpcusb_release will clean this up */
1125 cpcusb_delete(card);
1127 wake_up_interruptible(card->chan->CPCWait_q);
1129 up(&disconnect_sem);
1132 info("USB-%d now disconnected", minor);
1135 static int __init CPCUsb_Init(void)
1139 info(DRIVER_DESC " v" DRIVER_VERSION);
1140 info("Build on " __DATE__ " at " __TIME__);
1142 for (i = 0; i < CPC_USB_CARD_CNT; i++)
1143 CPCUSB_Table[i] = 0;
1145 /* register this driver with the USB subsystem */
1146 result = usb_register(&cpcusb_driver);
1148 err("usb_register failed. Error number %d", result);
1152 procDir = proc_mkdir(CPC_USB_PROC_DIR, NULL);
1154 err("Could not create proc entry");
1156 procEntry = create_proc_read_entry("info", 0444, procDir,
1157 cpcusb_proc_read_info,
1160 err("Could not create proc entry %s", CPC_USB_PROC_DIR "/info");
1161 remove_proc_entry(CPC_USB_PROC_DIR, NULL);
1169 static void __exit CPCUsb_Exit(void)
1171 wait_event(rmmodWq, !atomic_read(&useCount));
1173 /* deregister this driver with the USB subsystem */
1174 usb_deregister(&cpcusb_driver);
1178 remove_proc_entry("info", procDir);
1179 remove_proc_entry(CPC_USB_PROC_DIR, NULL);
1183 module_init(CPCUsb_Init);
1184 module_exit(CPCUsb_Exit);