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 /* usb_kill_urb has been introduced in kernel version 2.6.8 (RC2) */
34 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,8))
35 #define usb_kill_urb usb_unlink_urb
39 # include <linux/proc_fs.h>
49 /* Version Information */
50 #define DRIVER_AUTHOR "Sebastian Haas <haas@ems-wuensche.com>"
51 #define DRIVER_DESC "CPC-USB Driver for Linux Kernel 2.6"
52 #define DRIVER_VERSION CPC_DRIVER_VERSION
54 MODULE_AUTHOR(DRIVER_AUTHOR);
55 MODULE_DESCRIPTION(DRIVER_DESC);
56 MODULE_VERSION(DRIVER_VERSION);
57 MODULE_LICENSE("GPL v2");
59 /* Define these values to match your devices */
60 #define USB_CPCUSB_VENDOR_ID 0x12D6
62 #define USB_CPCUSB_M16C_PRODUCT_ID 0x0888
63 #define USB_CPCUSB_LPC2119_PRODUCT_ID 0x0444
65 #ifndef CONFIG_PROC_FS
66 #error "PROCFS needed"
69 #define CPC_USB_PROC_DIR CPC_PROC_DIR "cpc-usb"
71 static struct proc_dir_entry *procDir = NULL;
72 static struct proc_dir_entry *procEntry = NULL;
74 /* Module parameters */
76 module_param(debug, int, S_IRUGO);
78 /* table of devices that work with this driver */
79 static struct usb_device_id cpcusb_table[] = {
80 {USB_DEVICE(USB_CPCUSB_VENDOR_ID, USB_CPCUSB_M16C_PRODUCT_ID)},
81 {USB_DEVICE(USB_CPCUSB_VENDOR_ID, USB_CPCUSB_LPC2119_PRODUCT_ID)},
82 {} /* Terminating entry */
85 MODULE_DEVICE_TABLE(usb, cpcusb_table);
87 /* use to prevent kernel panic if driver is unloaded
88 * while a programm has still open the device
90 DECLARE_WAIT_QUEUE_HEAD(rmmodWq);
93 static CPC_USB_T *CPCUSB_Table[CPC_USB_CARD_CNT] = { 0 };
94 static unsigned int CPCUsbCnt = 0;
96 /* prevent races between open() and disconnect() */
97 static DECLARE_MUTEX(disconnect_sem);
99 /* local function prototypes */
100 static ssize_t cpcusb_read(struct file *file, char *buffer, size_t count,
102 static ssize_t cpcusb_write(struct file *file, const char *buffer,
103 size_t count, loff_t * ppos);
104 static unsigned int cpcusb_poll(struct file *file, poll_table * wait);
105 static int cpcusb_open(struct inode *inode, struct file *file);
106 static int cpcusb_release(struct inode *inode, struct file *file);
108 static int cpcusb_probe(struct usb_interface *interface,
109 const struct usb_device_id *id);
110 static void cpcusb_disconnect(struct usb_interface *interface);
112 static void cpcusb_read_bulk_callback(struct urb *urb
113 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))
114 , struct pt_regs *regs
117 static void cpcusb_write_bulk_callback(struct urb *urb
118 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))
119 , struct pt_regs *regs
122 static void cpcusb_read_interrupt_callback(struct urb *urb
123 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))
124 , struct pt_regs *regs
128 static int cpcusb_setup_intrep(CPC_USB_T * card);
130 static struct file_operations cpcusb_fops = {
132 * The owner field is part of the module-locking
133 * mechanism. The idea is that the kernel knows
134 * which module to increment the use-counter of
135 * BEFORE it calls the device's open() function.
136 * This also means that the kernel can decrement
137 * the use-counter again before calling release()
138 * or should the open() function fail.
140 .owner = THIS_MODULE,
143 .write = cpcusb_write,
146 .release = cpcusb_release,
150 * usb class driver info in order to get a minor number from the usb core,
151 * and to have the device registered with devfs and the driver core
153 static struct usb_class_driver cpcusb_class = {
154 .name = "usb/cpc_usb%d",
155 .fops = &cpcusb_fops,
156 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14))
158 S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH |
161 .minor_base = CPC_USB_BASE_MNR,
164 /* usb specific object needed to register this driver with the usb subsystem */
165 static struct usb_driver cpcusb_driver = {
166 #if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,14))
167 .owner = THIS_MODULE,
170 .probe = cpcusb_probe,
171 .disconnect = cpcusb_disconnect,
172 .id_table = cpcusb_table,
175 static int cpcusb_create_info_output(char *buf)
179 for (j = 0; j < CPC_USB_CARD_CNT; j++) {
180 if (CPCUSB_Table[j]) {
181 CPC_USB_T *card = CPCUSB_Table[j];
182 CPC_CHAN_T *chan = card->chan;
184 /* MINOR CHANNELNO BUSNO SLOTNO */
185 i += sprintf(&buf[i], "%d %s\n", chan->minor,
193 static int cpcusb_proc_read_info(char *page, char **start, off_t off,
194 int count, int *eof, void *data)
196 int len = cpcusb_create_info_output(page);
198 if (len <= off + count)
211 * Remove CPC-USB and cleanup
213 static inline void cpcusb_delete(CPC_USB_T * card)
218 vfree(card->chan->buf);
220 if (card->chan->CPCWait_q)
221 kfree(card->chan->CPCWait_q);
226 CPCUSB_Table[card->idx] = NULL;
232 * setup the interrupt IN endpoint of a specific CPC-USB device
234 static int cpcusb_setup_intrep(CPC_USB_T * card)
237 struct usb_endpoint_descriptor *ep;
239 ep = &card->interface->altsetting[0].endpoint[card->num_intr_in].desc;
241 card->intr_in_buffer[0] = 0;
242 card->free_slots = 15; /* initial size */
245 usb_fill_int_urb(card->intr_in_urb, card->udev,
246 usb_rcvintpipe(card->udev, card->num_intr_in),
247 card->intr_in_buffer,
248 sizeof(card->intr_in_buffer),
249 cpcusb_read_interrupt_callback,
253 card->intr_in_urb->status = 0; /* needed! */
256 retval = usb_submit_urb(card->intr_in_urb, GFP_KERNEL);
259 err("%s - failed submitting intr urb, error %d", __FUNCTION__, retval);
265 static int cpcusb_open(struct inode *inode, struct file *file)
267 CPC_USB_T *card = NULL;
268 struct usb_interface *interface;
272 subminor = iminor(inode);
274 /* prevent disconnects */
275 down(&disconnect_sem);
277 interface = usb_find_interface(&cpcusb_driver, subminor);
279 err("%s - error, can't find device for minor %d",
280 __FUNCTION__, subminor);
281 retval = CPC_ERR_NO_INTERFACE_PRESENT;
285 card = usb_get_intfdata(interface);
287 retval = CPC_ERR_NO_INTERFACE_PRESENT;
291 /* lock this device */
294 /* increment our usage count for the driver */
296 dbg("device already opened");
297 retval = CPC_ERR_CHANNEL_ALREADY_OPEN;
301 /* save our object in the file's private structure */
302 file->private_data = card;
303 for (j = 0; j < CPC_USB_URB_CNT; j++) {
304 usb_fill_bulk_urb(card->urbs[j].urb, card->udev,
305 usb_rcvbulkpipe(card->udev, card->num_bulk_in),
306 card->urbs[j].buffer, card->urbs[j].size,
307 cpcusb_read_bulk_callback, card);
309 retval = usb_submit_urb(card->urbs[j].urb, GFP_KERNEL);
312 err("%s - failed submitting read urb, error %d",
313 __FUNCTION__, retval);
314 retval = CPC_ERR_TRANSMISSION_FAILED;
319 info("%s - %d URB's submitted", __FUNCTION__, j);
321 ResetBuffer(card->chan);
323 cpcusb_setup_intrep(card);
326 atomic_inc(&useCount);
329 /* unlock this device */
338 static unsigned int cpcusb_poll(struct file *file, poll_table * wait)
340 CPC_USB_T *card = (CPC_USB_T *) file->private_data;
341 unsigned int retval = 0;
344 err("%s - device object lost", __FUNCTION__);
348 poll_wait(file, card->chan->CPCWait_q, wait);
350 if (IsBufferNotEmpty(card->chan) || !(card->present))
351 retval |= (POLLIN | POLLRDNORM);
353 if (card->free_slots)
354 retval |= (POLLOUT | POLLWRNORM);
359 static int cpcusb_release(struct inode *inode, struct file *file)
361 CPC_USB_T *card = (CPC_USB_T *) file->private_data;
365 dbg("%s - object is NULL", __FUNCTION__);
366 return CPC_ERR_NO_INTERFACE_PRESENT;
369 /* lock our device */
373 dbg("%s - device not opened", __FUNCTION__);
374 retval = CPC_ERR_NO_INTERFACE_PRESENT;
375 goto exit_not_opened;
378 /* if device wasn't unplugged kill all urbs */
381 for (j = 0; j < CPC_USB_URB_CNT; j++) {
382 usb_kill_urb(card->urbs[j].urb);
386 usb_kill_urb(card->intr_in_urb);
388 /* kill write urbs */
389 for (j = 0; j < CPC_USB_URB_CNT; j++) {
390 if (atomic_read(&card->wrUrbs[j].busy)) {
391 usb_kill_urb(card->wrUrbs[j].urb);
392 wait_for_completion(&card->wrUrbs[j].finished);
397 atomic_dec(&useCount);
399 /* last process detached */
400 if (atomic_read(&useCount) == 0) {
404 if (!card->present && card->open) {
405 /* the device was unplugged before the file was released */
419 static ssize_t cpcusb_read(struct file *file, char *buffer, size_t count,
422 CPC_USB_T *card = (CPC_USB_T *) file->private_data;
426 if (count < sizeof(CPC_MSG_T))
427 return CPC_ERR_UNKNOWN;
429 /* check if can read from the given address */
430 if (!access_ok(VERIFY_WRITE, buffer, count))
431 return CPC_ERR_UNKNOWN;
433 /* lock this object */
436 /* verify that the device wasn't unplugged */
437 if (!card->present) {
439 return CPC_ERR_NO_INTERFACE_PRESENT;
442 if (IsBufferEmpty(card->chan)) {
448 /* convert LPC2119 params back to SJA1000 params */
449 if (card->deviceRevision >= 0x0200
450 && chan->buf[chan->oidx].type == CPC_MSG_T_CAN_PRMS) {
451 LPC2119_TO_SJA1000_Params(&chan->buf[chan->oidx]);
455 if (copy_to_user(buffer, &chan->buf[chan->oidx], count) != 0) {
456 retval = CPC_ERR_IO_TRANSFER;
458 chan->oidx = (chan->oidx + 1) % CPC_MSG_BUF_CNT;
460 retval = sizeof(CPC_MSG_T);
463 // spin_unlock_irqrestore(&card->slock, flags);
465 /* unlock the device */
472 static void inline cpcusb_align_buffer_alignment(unsigned char *buf)
474 // CPC-USB uploads packed bytes.
475 CPC_MSG_T *cpc = (CPC_MSG_T *) buf;
478 for (i = 0; i < cpc->length + (2 * sizeof(unsigned long)); i++) {
479 ((unsigned char *) &cpc->msgid)[1 + i] =
480 ((unsigned char *) &cpc->msgid)[1 + SHIFT + i];
484 static int cpc_get_buffer_count(CPC_CHAN_T * chan)
486 // check the buffer parameters
487 if (chan->iidx == chan->oidx)
488 return !chan->WnR ? CPC_MSG_BUF_CNT : 0;
489 else if (chan->iidx >= chan->oidx)
490 return (chan->iidx - chan->oidx) % CPC_MSG_BUF_CNT;
492 return (chan->iidx + CPC_MSG_BUF_CNT - chan->oidx) % CPC_MSG_BUF_CNT;
495 static ssize_t cpcusb_write(struct file *file, const char *buffer,
496 size_t count, loff_t * ppos)
498 CPC_USB_T *card = (CPC_USB_T *) file->private_data;
499 CPC_USB_WRITE_URB_T *wrUrb = NULL;
501 ssize_t bytes_written = 0;
505 unsigned char *obuf = NULL;
506 unsigned char type = 0;
507 CPC_MSG_T *info = NULL;
509 dbg("%s - entered minor %d, count = %d, present = %d",
510 __FUNCTION__, card->minor, count, card->present);
512 if (count > sizeof(CPC_MSG_T))
513 return CPC_ERR_UNKNOWN;
515 /* check if can read from the given address */
516 if (!access_ok(VERIFY_READ, buffer, count))
517 return CPC_ERR_UNKNOWN;
519 /* lock this object */
522 /* verify that the device wasn't unplugged */
523 if (!card->present) {
524 retval = CPC_ERR_NO_INTERFACE_PRESENT;
528 /* verify that we actually have some data to write */
530 dbg("%s - write request of 0 bytes", __FUNCTION__);
534 if (card->free_slots <= 5) {
535 info = (CPC_MSG_T *) buffer;
537 if (info->type != CPC_CMD_T_CLEAR_CMD_QUEUE
538 || card->free_slots <= 0) {
539 dbg("%s - send buffer full please try again %d",
540 __FUNCTION__, card->free_slots);
541 retval = CPC_ERR_CAN_NO_TRANSMIT_BUF;
546 /* Find a free write urb */
547 for (j = 0; j < CPC_USB_URB_CNT; j++) {
548 if (!atomic_read(&card->wrUrbs[j].busy)) {
549 wrUrb = &card->wrUrbs[j]; /* remember found URB */
550 atomic_set(&wrUrb->busy, 1); /* lock this URB */
551 init_completion(&wrUrb->finished); /* init completion */
552 dbg("WR URB no. %d started", j);
557 /* don't found write urb say error */
559 dbg("%s - no free send urb available", __FUNCTION__);
560 retval = CPC_ERR_CAN_NO_TRANSMIT_BUF;
563 dbg("URB write req");
565 obuf = (unsigned char *) wrUrb->urb->transfer_buffer;
567 /* copy the data from userspace into our transfer buffer;
568 * this is the only copy required.
570 if (copy_from_user(&obuf[4], buffer, count) != 0) {
571 atomic_set(&wrUrb->busy, 0); /* release urb */
572 retval = CPC_ERR_IO_TRANSFER;
576 /* check if it is a DRIVER information message, so we can
577 * response to that message and not the USB
579 info = (CPC_MSG_T *) & obuf[4];
581 bytes_written = 11 + info->length;
582 if (bytes_written >= wrUrb->size) {
583 retval = CPC_ERR_IO_TRANSFER;
587 switch (info->type) {
588 case CPC_CMD_T_CLEAR_MSG_QUEUE:
589 ResetBuffer(card->chan);
592 case CPC_CMD_T_INQ_MSG_QUEUE_CNT:
593 retval = cpc_get_buffer_count(card->chan);
594 atomic_set(&wrUrb->busy, 0);
598 case CPC_CMD_T_INQ_INFO:
599 if (info->msg.info.source == CPC_INFOMSG_T_DRIVER) {
600 /* release urb cause we'll use it for driver
603 atomic_set(&wrUrb->busy, 0);
604 if (IsBufferFull(card->chan)) {
605 retval = CPC_ERR_IO_TRANSFER;
609 /* it is a driver information request message and we have
610 * free rx slots to store the response
612 type = info->msg.info.type;
613 info = &card->chan->buf[card->chan->iidx];
615 info->type = CPC_MSG_T_INFO;
616 info->msg.info.source = CPC_INFOMSG_T_DRIVER;
617 info->msg.info.type = type;
620 case CPC_INFOMSG_T_VERSION:
621 info->length = strlen(CPC_DRIVER_VERSION) + 2;
622 sprintf(info->msg.info.msg, "%s\n",
626 case CPC_INFOMSG_T_SERIAL:
627 info->length = strlen(CPC_DRIVER_SERIAL) + 2;
628 sprintf(info->msg.info.msg, "%s\n",
634 info->msg.info.type =
635 CPC_INFOMSG_T_UNKNOWN_TYPE;
640 (card->chan->iidx + 1) % CPC_MSG_BUF_CNT;
642 retval = info->length;
646 case CPC_CMD_T_CAN_PRMS:
647 /* Check the controller type. If it's the new CPC-USB, make sure if these are SJA1000 params */
648 if (info->msg.canparams.cc_type != SJA1000
649 && info->msg.canparams.cc_type != M16C_BASIC
650 && (card->productId == USB_CPCUSB_LPC2119_PRODUCT_ID
651 && info->msg.canparams.cc_type != SJA1000)) {
652 /* don't forget to release the urb */
653 atomic_set(&wrUrb->busy, 0);
654 retval = CPC_ERR_WRONG_CONTROLLER_TYPE;
660 /* just convert the params if it is an old CPC-USB with M16C controller */
661 if (card->productId == USB_CPCUSB_M16C_PRODUCT_ID) {
662 /* if it is a parameter message convert it from SJA1000 controller
663 * settings to M16C Basic controller settings
665 SJA1000_TO_M16C_BASIC_Params((CPC_MSG_T *) & obuf[4]);
668 /* don't forget the byte alignment */
669 cpcusb_align_buffer_alignment(&obuf[4]);
671 /* setup a the 4 byte header */
672 obuf[0] = obuf[1] = obuf[2] = obuf[3] = 0;
674 /* this urb was already set up, except for this write size */
675 wrUrb->urb->transfer_buffer_length = bytes_written + 4;
677 /* send the data out the bulk port */
678 /* a character device write uses GFP_KERNEL,
679 unless a spinlock is held */
680 retval = usb_submit_urb(wrUrb->urb, GFP_KERNEL);
682 atomic_set(&wrUrb->busy, 0); /* release urb */
683 err("%s - failed submitting write urb, error %d",
684 __FUNCTION__, retval);
686 retval = bytes_written;
690 /* unlock the device */
693 dbg("%s - leaved", __FUNCTION__);
699 * callback for interrupt IN urb
701 static void cpcusb_read_interrupt_callback(struct urb *urb
702 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))
703 , struct pt_regs *regs
707 CPC_USB_T *card = (CPC_USB_T *) urb->context;
711 spin_lock_irqsave(&card->slock, flags);
713 if (!card->present) {
714 spin_unlock_irqrestore(&card->slock, flags);
715 info("%s - no such device", __FUNCTION__);
719 switch (urb->status) {
720 case 0: /* success */
721 card->free_slots = card->intr_in_buffer[1];
727 spin_unlock_irqrestore(&card->slock, flags);
728 dbg("%s - intr urb killed", __FUNCTION__);
731 info("%s - nonzero urb status %d", __FUNCTION__, urb->status);
735 retval = usb_submit_urb(urb, GFP_ATOMIC);
737 err("%s - failed resubmitting intr urb, error %d",
738 __FUNCTION__, retval);
741 spin_unlock_irqrestore(&card->slock, flags);
742 wake_up_interruptible(card->chan->CPCWait_q);
748 #define CPCMSG_HEADER_LEN_FIRMWARE 11
749 static int inline cpcusb_unalign_and_copy_buffy(unsigned char *out,
754 for (i = 0; i < 3; i++) {
758 for (j = 0; j < (in[1] + (CPCMSG_HEADER_LEN_FIRMWARE - 3)); j++) {
759 out[j + i + UN_SHIFT] = in[j + i];
766 * callback for bulk IN urb
768 static void cpcusb_read_bulk_callback(struct urb *urb
769 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))
770 , struct pt_regs *regs
774 CPC_USB_T *card = (CPC_USB_T *) urb->context;
776 unsigned char *ibuf = urb->transfer_buffer;
777 int retval, msgCnt, start, again = 0;
781 err("%s - device object lost", __FUNCTION__);
785 spin_lock_irqsave(&card->slock, flags);
787 if (!card->present) {
788 spin_unlock_irqrestore(&card->slock, flags);
789 info("%s - no such device", __FUNCTION__);
793 switch (urb->status) {
794 case 0: /* success */
800 spin_unlock_irqrestore(&card->slock, flags);
801 dbg("%s - read urb killed", __FUNCTION__);
804 info("%s - nonzero urb status %d", __FUNCTION__, urb->status);
808 if (urb->actual_length) {
809 msgCnt = ibuf[0] & ~0x80;
810 again = ibuf[0] & 0x80;
812 /* we have a 4 byte header */
816 if (!(IsBufferFull(card->chan))) {
818 cpcusb_unalign_and_copy_buffy((unsigned char *)
819 &chan->buf[chan->iidx], &ibuf[start]);
821 if (start > urb->transfer_buffer_length) {
822 err("%d > %d", start, urb->transfer_buffer_length);
827 chan->iidx = (chan->iidx + 1) % CPC_MSG_BUF_CNT;
835 usb_fill_bulk_urb(urb, card->udev,
836 usb_rcvbulkpipe(card->udev, card->num_bulk_in),
837 urb->transfer_buffer,
838 urb->transfer_buffer_length,
839 cpcusb_read_bulk_callback, card);
841 retval = usb_submit_urb(urb, GFP_ATOMIC);
844 err("%s - failed resubmitting read urb, error %d", __FUNCTION__, retval);
847 spin_unlock_irqrestore(&card->slock, flags);
849 wake_up_interruptible(card->chan->CPCWait_q);
853 * callback for bulk IN urb
855 static void cpcusb_write_bulk_callback(struct urb *urb
856 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))
857 , struct pt_regs *regs
861 CPC_USB_T *card = (CPC_USB_T *) urb->context;
865 spin_lock_irqsave(&card->slock, flags);
868 for (j = 0; j < CPC_USB_URB_CNT; j++) {
869 if (card->wrUrbs[j].urb == urb) {
870 dbg("URB found no. %d", j);
871 /* notify anyone waiting that the write has finished */
872 complete(&card->wrUrbs[j].finished);
873 atomic_set(&card->wrUrbs[j].busy, 0);
878 switch (urb->status) {
879 case 0: /* success */
885 spin_unlock_irqrestore(&card->slock, flags);
886 dbg("%s - write urb no. %d killed", __FUNCTION__, j);
889 info("%s - nonzero urb status %d", __FUNCTION__, urb->status);
893 spin_unlock_irqrestore(&card->slock, flags);
895 wake_up_interruptible(card->chan->CPCWait_q);
898 static inline int cpcusb_get_free_slot(void)
902 for (i = 0; i < CPC_USB_CARD_CNT; i++) {
903 if (!CPCUSB_Table[i])
911 * probe function for new CPC-USB devices
913 static int cpcusb_probe(struct usb_interface *interface,
914 const struct usb_device_id *id)
916 CPC_USB_T *card = NULL;
917 CPC_CHAN_T *chan = NULL;
919 struct usb_device *udev = interface_to_usbdev(interface);
920 struct usb_host_interface *iface_desc;
921 struct usb_endpoint_descriptor *endpoint;
923 int i, j, retval = -ENOMEM, slot;
925 if ((slot = cpcusb_get_free_slot()) < 0) {
926 info("No more devices supported");
930 /* allocate memory for our device state and initialize it */
931 card = kzalloc(sizeof(CPC_USB_T), GFP_KERNEL);
933 err("Out of memory");
936 CPCUSB_Table[slot] = card;
938 /* allocate and initialize the channel struct */
939 card->chan = kmalloc(sizeof(CPC_CHAN_T), GFP_KERNEL);
942 err("Out of memory");
947 memset(chan, 0, sizeof(CPC_CHAN_T));
950 init_MUTEX(&card->sem);
951 spin_lock_init(&card->slock);
954 card->interface = interface;
955 if (udev->descriptor.iSerialNumber) {
956 usb_string(udev, udev->descriptor.iSerialNumber, card->serialNumber,
958 info("Serial %s", card->serialNumber);
961 card->productId = udev->descriptor.idProduct;
963 card->productId == USB_CPCUSB_LPC2119_PRODUCT_ID ?
964 "CPC-USB/ARM7" : "CPC-USB/M16C");
966 /* set up the endpoint information */
967 /* check out the endpoints */
968 /* use only the first bulk-in and bulk-out endpoints */
969 iface_desc = &interface->altsetting[0];
970 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
971 endpoint = &iface_desc->endpoint[i].desc;
973 if (!card->num_intr_in &&
974 (endpoint->bEndpointAddress & USB_DIR_IN) &&
975 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
976 == USB_ENDPOINT_XFER_INT)) {
977 card->intr_in_urb = usb_alloc_urb(0, GFP_KERNEL);
978 card->num_intr_in = 1;
980 if (!card->intr_in_urb) {
981 err("No free urbs available");
985 dbg("intr_in urb %d", card->num_intr_in);
988 if (!card->num_bulk_in &&
989 (endpoint->bEndpointAddress & USB_DIR_IN) &&
990 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
991 == USB_ENDPOINT_XFER_BULK)) {
992 card->num_bulk_in = 2;
993 for (j = 0; j < CPC_USB_URB_CNT; j++) {
994 card->urbs[j].size = endpoint->wMaxPacketSize;
995 card->urbs[j].urb = usb_alloc_urb(0, GFP_KERNEL);
996 if (!card->urbs[j].urb) {
997 err("No free urbs available");
1000 card->urbs[j].buffer =
1001 usb_buffer_alloc(udev,
1004 &card->urbs[j].urb->transfer_dma);
1005 if (!card->urbs[j].buffer) {
1006 err("Couldn't allocate bulk_in_buffer");
1010 info("%s - %d reading URB's allocated",
1011 __FUNCTION__, CPC_USB_URB_CNT);
1014 if (!card->num_bulk_out &&
1015 !(endpoint->bEndpointAddress & USB_DIR_IN) &&
1016 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1017 == USB_ENDPOINT_XFER_BULK)) {
1019 card->num_bulk_out = 2;
1021 for (j = 0; j < CPC_USB_URB_CNT; j++) {
1022 card->wrUrbs[j].size =
1023 endpoint->wMaxPacketSize;
1024 card->wrUrbs[j].urb =
1025 usb_alloc_urb(0, GFP_KERNEL);
1026 if (!card->wrUrbs[j].urb) {
1027 err("No free urbs available");
1030 card->wrUrbs[j].buffer = usb_buffer_alloc(udev,
1031 card->wrUrbs[j].size, GFP_KERNEL,
1032 &card->wrUrbs[j].urb->transfer_dma);
1034 if (!card->wrUrbs[j].buffer) {
1035 err("Couldn't allocate bulk_out_buffer");
1039 usb_fill_bulk_urb(card->wrUrbs[j].urb, udev,
1040 usb_sndbulkpipe(udev, endpoint->bEndpointAddress),
1041 card->wrUrbs[j].buffer,
1042 card->wrUrbs[j].size,
1043 cpcusb_write_bulk_callback,
1047 info("%s - %d writing URB's allocated", __FUNCTION__, CPC_USB_URB_CNT);
1051 if (!(card->num_bulk_in && card->num_bulk_out)) {
1052 err("Couldn't find both bulk-in and bulk-out endpoints");
1056 /* allow device read, write and ioctl */
1059 /* we can register the device now, as it is ready */
1060 usb_set_intfdata(interface, card);
1061 retval = usb_register_dev(interface, &cpcusb_class);
1064 /* something prevented us from registering this driver */
1065 err("Not able to get a minor for this device.");
1066 usb_set_intfdata(interface, NULL);
1070 card->chan->minor = card->minor = interface->minor;
1072 chan->buf = vmalloc(sizeof(CPC_MSG_T) * CPC_MSG_BUF_CNT);
1073 if (chan->buf == NULL) {
1074 err("Out of memory");
1078 info("Allocated memory for %d messages (%d kbytes)",
1079 CPC_MSG_BUF_CNT, (sizeof(CPC_MSG_T) * CPC_MSG_BUF_CNT) / 1000);
1080 memset(chan->buf, 0, sizeof(CPC_MSG_T) * CPC_MSG_BUF_CNT);
1084 card->chan->CPCWait_q = kmalloc(sizeof(wait_queue_head_t), GFP_KERNEL);
1085 if (!card->chan->CPCWait_q) {
1086 err("Out of memory");
1090 init_waitqueue_head(card->chan->CPCWait_q);
1092 CPCUSB_Table[slot] = card;
1096 /* let the user know what node this device is now attached to */
1097 info("Device now attached to USB-%d", card->minor);
1101 for (j = 0; j < CPC_USB_URB_CNT; j++) {
1102 if (card->urbs[j].buffer) {
1103 usb_buffer_free(card->udev, card->urbs[j].size,
1104 card->urbs[j].buffer,
1105 card->urbs[j].urb->transfer_dma);
1106 card->urbs[j].buffer = NULL;
1108 if (card->urbs[j].urb) {
1109 usb_free_urb(card->urbs[j].urb);
1110 card->urbs[j].urb = NULL;
1114 cpcusb_delete(card);
1119 * called by the usb core when the device is removed from the system
1121 static void cpcusb_disconnect(struct usb_interface *interface)
1123 CPC_USB_T *card = NULL;
1126 /* prevent races with open() */
1127 down(&disconnect_sem);
1129 card = usb_get_intfdata(interface);
1130 usb_set_intfdata(interface, NULL);
1134 /* prevent device read, write and ioctl */
1137 minor = card->minor;
1139 /* free all urbs and their buffers */
1140 for (j = 0; j < CPC_USB_URB_CNT; j++) {
1141 /* terminate an ongoing write */
1142 if (atomic_read(&card->wrUrbs[j].busy)) {
1143 usb_kill_urb(card->wrUrbs[j].urb);
1144 wait_for_completion(&card->wrUrbs[j].finished);
1146 usb_buffer_free(card->udev, card->wrUrbs[j].size,
1147 card->wrUrbs[j].buffer,
1148 card->wrUrbs[j].urb->transfer_dma);
1149 usb_free_urb(card->wrUrbs[j].urb);
1151 info("%d write URBs freed", CPC_USB_URB_CNT);
1153 /* free all urbs and their buffers */
1154 for (j = 0; j < CPC_USB_URB_CNT; j++) {
1155 usb_buffer_free(card->udev, card->urbs[j].size,
1156 card->urbs[j].buffer,
1157 card->urbs[j].urb->transfer_dma);
1158 usb_free_urb(card->urbs[j].urb);
1160 info("%d read URBs freed", CPC_USB_URB_CNT);
1161 usb_free_urb(card->intr_in_urb);
1163 /* give back our minor */
1164 usb_deregister_dev(interface, &cpcusb_class);
1168 /* if the device is opened, cpcusb_release will clean this up */
1170 cpcusb_delete(card);
1172 wake_up_interruptible(card->chan->CPCWait_q);
1174 up(&disconnect_sem);
1177 info("USB-%d now disconnected", minor);
1180 static int __init CPCUsb_Init(void)
1184 info(DRIVER_DESC " v" DRIVER_VERSION);
1185 info("Build on " __DATE__ " at " __TIME__);
1187 for (i = 0; i < CPC_USB_CARD_CNT; i++)
1188 CPCUSB_Table[i] = 0;
1190 /* register this driver with the USB subsystem */
1191 result = usb_register(&cpcusb_driver);
1193 err("usb_register failed. Error number %d", result);
1197 procDir = proc_mkdir(CPC_USB_PROC_DIR, NULL);
1199 err("Could not create proc entry");
1201 procEntry = create_proc_read_entry("info", 0444, procDir,
1202 cpcusb_proc_read_info,
1205 err("Could not create proc entry %s", CPC_USB_PROC_DIR "/info");
1206 remove_proc_entry(CPC_USB_PROC_DIR, NULL);
1214 static void __exit CPCUsb_Exit(void)
1216 wait_event(rmmodWq, !atomic_read(&useCount));
1218 /* deregister this driver with the USB subsystem */
1219 usb_deregister(&cpcusb_driver);
1223 remove_proc_entry("info", procDir);
1224 remove_proc_entry(CPC_USB_PROC_DIR, NULL);
1228 module_init(CPCUsb_Init);
1229 module_exit(CPCUsb_Exit);