2 * hdm_usb.c - Hardware dependent module for USB
4 * Copyright (C) 2013-2015 Microchip Technology Germany II GmbH & Co. KG
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
11 * This file is licensed under GPLv2.
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/module.h>
17 #include <linux/usb.h>
18 #include <linux/slab.h>
19 #include <linux/init.h>
20 #include <linux/cdev.h>
21 #include <linux/device.h>
22 #include <linux/list.h>
23 #include <linux/completion.h>
24 #include <linux/mutex.h>
25 #include <linux/spinlock.h>
26 #include <linux/interrupt.h>
27 #include <linux/workqueue.h>
28 #include <linux/sysfs.h>
29 #include <linux/dma-mapping.h>
30 #include <linux/etherdevice.h>
31 #include <linux/uaccess.h>
33 #include "networking.h"
36 #define NO_ISOCHRONOUS_URB 0
37 #define AV_PACKETS_PER_XACT 2
38 #define BUF_CHAIN_SIZE 0xFFFF
39 #define MAX_NUM_ENDPOINTS 30
40 #define MAX_SUFFIX_LEN 10
41 #define MAX_STRING_LEN 80
42 #define MAX_BUF_SIZE 0xFFFF
43 #define CEILING(x, y) (((x) + (y) - 1) / (y))
45 #define USB_VENDOR_ID_SMSC 0x0424 /* VID: SMSC */
46 #define USB_DEV_ID_BRDG 0xC001 /* PID: USB Bridge */
47 #define USB_DEV_ID_INIC 0xCF18 /* PID: USB INIC */
48 #define HW_RESYNC 0x0000
50 #define DRCI_REG_NI_STATE 0x0100
51 #define DRCI_REG_PACKET_BW 0x0101
52 #define DRCI_REG_NODE_ADDR 0x0102
53 #define DRCI_REG_NODE_POS 0x0103
54 #define DRCI_REG_MEP_FILTER 0x0140
55 #define DRCI_REG_HASH_TBL0 0x0141
56 #define DRCI_REG_HASH_TBL1 0x0142
57 #define DRCI_REG_HASH_TBL2 0x0143
58 #define DRCI_REG_HASH_TBL3 0x0144
59 #define DRCI_REG_HW_ADDR_HI 0x0145
60 #define DRCI_REG_HW_ADDR_MI 0x0146
61 #define DRCI_REG_HW_ADDR_LO 0x0147
62 #define DRCI_READ_REQ 0xA0
63 #define DRCI_WRITE_REQ 0xA1
66 * struct buf_anchor - used to create a list of pending URBs
67 * @urb: pointer to USB request block
74 struct work_struct clear_work_obj
;
75 struct list_head list
;
76 struct completion urb_compl
;
78 #define to_buf_anchor(w) container_of(w, struct buf_anchor, clear_work_obj)
81 * struct most_dci_obj - Direct Communication Interface
82 * @kobj:position in sysfs
83 * @usb_device: pointer to the usb device
87 struct usb_device
*usb_device
;
89 #define to_dci_obj(p) container_of(p, struct most_dci_obj, kobj)
92 * struct most_dev - holds all usb interface specific stuff
93 * @parent: parent object in sysfs
94 * @usb_device: pointer to usb device
95 * @iface: hardware interface
96 * @cap: channel capabilities
97 * @conf: channel configuration
98 * @dci: direct communication interface of hardware
99 * @hw_addr: MAC address of hardware
100 * @ep_address: endpoint address table
101 * @link_stat: link status of hardware
102 * @description: device description
103 * @suffix: suffix for channel name
104 * @anchor_list_lock: locks list access
105 * @padding_active: indicates channel uses padding
106 * @is_channel_healthy: health status table of each channel
107 * @anchor_list: list of anchored items
108 * @io_mutex: synchronize I/O with disconnect
109 * @link_stat_timer: timer for link status reports
110 * @poll_work_obj: work for polling link status
113 struct kobject
*parent
;
114 struct usb_device
*usb_device
;
115 struct most_interface iface
;
116 struct most_channel_capability
*cap
;
117 struct most_channel_config
*conf
;
118 struct most_dci_obj
*dci
;
122 char description
[MAX_STRING_LEN
];
123 char suffix
[MAX_NUM_ENDPOINTS
][MAX_SUFFIX_LEN
];
124 spinlock_t anchor_list_lock
[MAX_NUM_ENDPOINTS
];
125 bool padding_active
[MAX_NUM_ENDPOINTS
];
126 bool is_channel_healthy
[MAX_NUM_ENDPOINTS
];
127 struct list_head
*anchor_list
;
128 struct mutex io_mutex
;
129 struct timer_list link_stat_timer
;
130 struct work_struct poll_work_obj
;
132 #define to_mdev(d) container_of(d, struct most_dev, iface)
133 #define to_mdev_from_work(w) container_of(w, struct most_dev, poll_work_obj)
135 static struct workqueue_struct
*schedule_usb_work
;
136 static void wq_clear_halt(struct work_struct
*wq_obj
);
137 static void wq_netinfo(struct work_struct
*wq_obj
);
140 * trigger_resync_vr - Vendor request to trigger HW re-sync mechanism
144 static void trigger_resync_vr(struct usb_device
*dev
)
147 u8 request_type
= USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_ENDPOINT
;
148 int *data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
153 retval
= usb_control_msg(dev
,
154 usb_sndctrlpipe(dev
, 0),
166 dev_err(&dev
->dev
, "Vendor request \"stall\" failed\n");
170 * drci_rd_reg - read a DCI register
172 * @reg: register address
173 * @buf: buffer to store data
175 * This is reads data from INIC's direct register communication interface
177 static inline int drci_rd_reg(struct usb_device
*dev
, u16 reg
, void *buf
)
179 return usb_control_msg(dev
,
180 usb_rcvctrlpipe(dev
, 0),
182 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
191 * drci_wr_reg - write a DCI register
193 * @reg: register address
194 * @data: data to write
196 * This is writes data to INIC's direct register communication interface
198 static inline int drci_wr_reg(struct usb_device
*dev
, u16 reg
, u16 data
)
200 return usb_control_msg(dev
,
201 usb_sndctrlpipe(dev
, 0),
203 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_DEVICE
,
212 * free_anchored_buffers - free device's anchored items
214 * @channel: channel ID
216 static void free_anchored_buffers(struct most_dev
*mdev
, unsigned int channel
)
219 struct buf_anchor
*anchor
, *tmp
;
222 spin_lock_irqsave(&mdev
->anchor_list_lock
[channel
], flags
);
223 list_for_each_entry_safe(anchor
, tmp
, &mdev
->anchor_list
[channel
], list
) {
224 struct urb
*urb
= anchor
->urb
;
226 spin_unlock_irqrestore(&mdev
->anchor_list_lock
[channel
], flags
);
229 if (!irqs_disabled()) {
233 wait_for_completion(&anchor
->urb_compl
);
235 if ((mbo
) && (mbo
->complete
)) {
236 mbo
->status
= MBO_E_CLOSE
;
237 mbo
->processed_length
= 0;
242 spin_lock_irqsave(&mdev
->anchor_list_lock
[channel
], flags
);
243 list_del(&anchor
->list
);
246 spin_unlock_irqrestore(&mdev
->anchor_list_lock
[channel
], flags
);
250 * get_stream_frame_size - calculate frame size of current configuration
251 * @cfg: channel configuration
253 static unsigned int get_stream_frame_size(struct most_channel_config
*cfg
)
255 unsigned int frame_size
= 0;
256 unsigned int sub_size
= cfg
->subbuffer_size
;
259 pr_warn("Misconfig: Subbuffer size zero.\n");
262 switch (cfg
->data_type
) {
263 case MOST_CH_ISOC_AVP
:
264 frame_size
= AV_PACKETS_PER_XACT
* sub_size
;
267 if (cfg
->packets_per_xact
== 0) {
268 pr_warn("Misconfig: Packets per XACT zero\n");
270 } else if (cfg
->packets_per_xact
== 0xFF)
271 frame_size
= (USB_MTU
/ sub_size
) * sub_size
;
273 frame_size
= cfg
->packets_per_xact
* sub_size
;
276 pr_warn("Query frame size of non-streaming channel\n");
283 * hdm_poison_channel - mark buffers of this channel as invalid
284 * @iface: pointer to the interface
285 * @channel: channel ID
287 * This unlinks all URBs submitted to the HCD,
288 * calls the associated completion function of the core and removes
289 * them from the list.
291 * Returns 0 on success or error code otherwise.
293 static int hdm_poison_channel(struct most_interface
*iface
, int channel
)
295 struct most_dev
*mdev
;
297 mdev
= to_mdev(iface
);
298 if (unlikely(!iface
)) {
299 dev_warn(&mdev
->usb_device
->dev
, "Poison: Bad interface.\n");
302 if (unlikely((channel
< 0) || (channel
>= iface
->num_channels
))) {
303 dev_warn(&mdev
->usb_device
->dev
, "Channel ID out of range.\n");
307 mdev
->is_channel_healthy
[channel
] = false;
309 mutex_lock(&mdev
->io_mutex
);
310 free_anchored_buffers(mdev
, channel
);
311 if (mdev
->padding_active
[channel
] == true)
312 mdev
->padding_active
[channel
] = false;
314 if (mdev
->conf
[channel
].data_type
== MOST_CH_ASYNC
) {
315 del_timer_sync(&mdev
->link_stat_timer
);
316 cancel_work_sync(&mdev
->poll_work_obj
);
318 mutex_unlock(&mdev
->io_mutex
);
323 * hdm_add_padding - add padding bytes
325 * @channel: channel ID
326 * @mbo: buffer object
328 * This inserts the INIC hardware specific padding bytes into a streaming
331 static int hdm_add_padding(struct most_dev
*mdev
, int channel
, struct mbo
*mbo
)
333 struct most_channel_config
*conf
= &mdev
->conf
[channel
];
334 unsigned int j
, num_frames
, frame_size
;
335 u16 rd_addr
, wr_addr
;
337 frame_size
= get_stream_frame_size(conf
);
340 num_frames
= mbo
->buffer_length
/ frame_size
;
342 if (num_frames
< 1) {
343 dev_err(&mdev
->usb_device
->dev
,
344 "Missed minimal transfer unit.\n");
348 for (j
= 1; j
< num_frames
; j
++) {
349 wr_addr
= (num_frames
- j
) * USB_MTU
;
350 rd_addr
= (num_frames
- j
) * frame_size
;
351 memmove(mbo
->virt_address
+ wr_addr
,
352 mbo
->virt_address
+ rd_addr
,
355 mbo
->buffer_length
= num_frames
* USB_MTU
;
360 * hdm_remove_padding - remove padding bytes
362 * @channel: channel ID
363 * @mbo: buffer object
365 * This takes the INIC hardware specific padding bytes off a streaming
368 static int hdm_remove_padding(struct most_dev
*mdev
, int channel
, struct mbo
*mbo
)
370 unsigned int j
, num_frames
, frame_size
;
371 struct most_channel_config
*const conf
= &mdev
->conf
[channel
];
373 frame_size
= get_stream_frame_size(conf
);
376 num_frames
= mbo
->processed_length
/ USB_MTU
;
378 for (j
= 1; j
< num_frames
; j
++)
379 memmove(mbo
->virt_address
+ frame_size
* j
,
380 mbo
->virt_address
+ USB_MTU
* j
,
383 mbo
->processed_length
= frame_size
* num_frames
;
388 * hdm_write_completion - completion function for submitted Tx URBs
389 * @urb: the URB that has been completed
391 * This checks the status of the completed URB. In case the URB has been
392 * unlinked before, it is immediately freed. On any other error the MBO
393 * transfer flag is set. On success it frees allocated resources and calls
394 * the completion function.
396 * Context: interrupt!
398 static void hdm_write_completion(struct urb
*urb
)
401 struct buf_anchor
*anchor
;
402 struct most_dev
*mdev
;
404 unsigned int channel
;
409 mdev
= to_mdev(mbo
->ifp
);
410 channel
= mbo
->hdm_channel_id
;
411 dev
= &mdev
->usb_device
->dev
;
413 if ((urb
->status
== -ENOENT
) || (urb
->status
== -ECONNRESET
) ||
414 (mdev
->is_channel_healthy
[channel
] == false)) {
415 complete(&anchor
->urb_compl
);
419 if (unlikely(urb
->status
&& !(urb
->status
== -ENOENT
||
420 urb
->status
== -ECONNRESET
||
421 urb
->status
== -ESHUTDOWN
))) {
422 mbo
->processed_length
= 0;
423 switch (urb
->status
) {
425 dev_warn(dev
, "Broken OUT pipe detected\n");
426 most_stop_enqueue(&mdev
->iface
, channel
);
427 mbo
->status
= MBO_E_INVAL
;
429 INIT_WORK(&anchor
->clear_work_obj
, wq_clear_halt
);
430 queue_work(schedule_usb_work
, &anchor
->clear_work_obj
);
434 mbo
->status
= MBO_E_CLOSE
;
437 mbo
->status
= MBO_E_INVAL
;
441 mbo
->status
= MBO_SUCCESS
;
442 mbo
->processed_length
= urb
->actual_length
;
445 spin_lock_irqsave(&mdev
->anchor_list_lock
[channel
], flags
);
446 list_del(&anchor
->list
);
447 spin_unlock_irqrestore(&mdev
->anchor_list_lock
[channel
], flags
);
450 if (likely(mbo
->complete
))
456 * hdm_read_completion - completion funciton for submitted Rx URBs
457 * @urb: the URB that has been completed
459 * This checks the status of the completed URB. In case the URB has been
460 * unlinked before it is immediately freed. On any other error the MBO transfer
461 * flag is set. On success it frees allocated resources, removes
462 * padding bytes -if necessary- and calls the completion function.
464 * Context: interrupt!
466 * **************************************************************************
467 * Error codes returned by in urb->status
468 * or in iso_frame_desc[n].status (for ISO)
469 * *************************************************************************
471 * USB device drivers may only test urb status values in completion handlers.
472 * This is because otherwise there would be a race between HCDs updating
473 * these values on one CPU, and device drivers testing them on another CPU.
475 * A transfer's actual_length may be positive even when an error has been
476 * reported. That's because transfers often involve several packets, so that
477 * one or more packets could finish before an error stops further endpoint I/O.
479 * For isochronous URBs, the urb status value is non-zero only if the URB is
480 * unlinked, the device is removed, the host controller is disabled or the total
481 * transferred length is less than the requested length and the URB_SHORT_NOT_OK
482 * flag is set. Completion handlers for isochronous URBs should only see
483 * urb->status set to zero, -ENOENT, -ECONNRESET, -ESHUTDOWN, or -EREMOTEIO.
484 * Individual frame descriptor status fields may report more status codes.
487 * 0 Transfer completed successfully
489 * -ENOENT URB was synchronously unlinked by usb_unlink_urb
491 * -EINPROGRESS URB still pending, no results yet
492 * (That is, if drivers see this it's a bug.)
494 * -EPROTO (*, **) a) bitstuff error
495 * b) no response packet received within the
496 * prescribed bus turn-around time
497 * c) unknown USB error
499 * -EILSEQ (*, **) a) CRC mismatch
500 * b) no response packet received within the
501 * prescribed bus turn-around time
502 * c) unknown USB error
504 * Note that often the controller hardware does not
505 * distinguish among cases a), b), and c), so a
506 * driver cannot tell whether there was a protocol
507 * error, a failure to respond (often caused by
508 * device disconnect), or some other fault.
510 * -ETIME (**) No response packet received within the prescribed
511 * bus turn-around time. This error may instead be
512 * reported as -EPROTO or -EILSEQ.
514 * -ETIMEDOUT Synchronous USB message functions use this code
515 * to indicate timeout expired before the transfer
516 * completed, and no other error was reported by HC.
518 * -EPIPE (**) Endpoint stalled. For non-control endpoints,
519 * reset this status with usb_clear_halt().
521 * -ECOMM During an IN transfer, the host controller
522 * received data from an endpoint faster than it
523 * could be written to system memory
525 * -ENOSR During an OUT transfer, the host controller
526 * could not retrieve data from system memory fast
527 * enough to keep up with the USB data rate
529 * -EOVERFLOW (*) The amount of data returned by the endpoint was
530 * greater than either the max packet size of the
531 * endpoint or the remaining buffer size. "Babble".
533 * -EREMOTEIO The data read from the endpoint did not fill the
534 * specified buffer, and URB_SHORT_NOT_OK was set in
535 * urb->transfer_flags.
537 * -ENODEV Device was removed. Often preceded by a burst of
538 * other errors, since the hub driver doesn't detect
539 * device removal events immediately.
541 * -EXDEV ISO transfer only partially completed
542 * (only set in iso_frame_desc[n].status, not urb->status)
544 * -EINVAL ISO madness, if this happens: Log off and go home
546 * -ECONNRESET URB was asynchronously unlinked by usb_unlink_urb
548 * -ESHUTDOWN The device or host controller has been disabled due
549 * to some problem that could not be worked around,
550 * such as a physical disconnect.
553 * (*) Error codes like -EPROTO, -EILSEQ and -EOVERFLOW normally indicate
554 * hardware problems such as bad devices (including firmware) or cables.
556 * (**) This is also one of several codes that different kinds of host
557 * controller use to indicate a transfer has failed because of device
558 * disconnect. In the interval before the hub driver starts disconnect
559 * processing, devices may receive such fault reports for every request.
561 * See <https://www.kernel.org/doc/Documentation/usb/error-codes.txt>
563 static void hdm_read_completion(struct urb
*urb
)
566 struct buf_anchor
*anchor
;
567 struct most_dev
*mdev
;
570 unsigned int channel
;
571 struct most_channel_config
*conf
;
575 mdev
= to_mdev(mbo
->ifp
);
576 channel
= mbo
->hdm_channel_id
;
577 dev
= &mdev
->usb_device
->dev
;
579 if ((urb
->status
== -ENOENT
) || (urb
->status
== -ECONNRESET
) ||
580 (mdev
->is_channel_healthy
[channel
] == false)) {
581 complete(&anchor
->urb_compl
);
585 conf
= &mdev
->conf
[channel
];
587 if (unlikely(urb
->status
&& !(urb
->status
== -ENOENT
||
588 urb
->status
== -ECONNRESET
||
589 urb
->status
== -ESHUTDOWN
))) {
590 mbo
->processed_length
= 0;
591 switch (urb
->status
) {
593 dev_warn(dev
, "Broken IN pipe detected\n");
594 mbo
->status
= MBO_E_INVAL
;
596 INIT_WORK(&anchor
->clear_work_obj
, wq_clear_halt
);
597 queue_work(schedule_usb_work
, &anchor
->clear_work_obj
);
601 mbo
->status
= MBO_E_CLOSE
;
604 dev_warn(dev
, "Babble on IN pipe detected\n");
606 mbo
->status
= MBO_E_INVAL
;
610 mbo
->processed_length
= urb
->actual_length
;
611 if (mdev
->padding_active
[channel
] == false) {
612 mbo
->status
= MBO_SUCCESS
;
614 if (hdm_remove_padding(mdev
, channel
, mbo
)) {
615 mbo
->processed_length
= 0;
616 mbo
->status
= MBO_E_INVAL
;
618 mbo
->status
= MBO_SUCCESS
;
622 spin_lock_irqsave(&mdev
->anchor_list_lock
[channel
], flags
);
623 list_del(&anchor
->list
);
624 spin_unlock_irqrestore(&mdev
->anchor_list_lock
[channel
], flags
);
627 if (likely(mbo
->complete
))
633 * hdm_enqueue - receive a buffer to be used for data transfer
634 * @iface: interface to enqueue to
635 * @channel: ID of the channel
636 * @mbo: pointer to the buffer object
638 * This allocates a new URB and fills it according to the channel
639 * that is being used for transmission of data. Before the URB is
640 * submitted it is stored in the private anchor list.
642 * Returns 0 on success. On any error the URB is freed and a error code
645 * Context: Could in _some_ cases be interrupt!
647 static int hdm_enqueue(struct most_interface
*iface
, int channel
, struct mbo
*mbo
)
649 struct most_dev
*mdev
;
650 struct buf_anchor
*anchor
;
651 struct most_channel_config
*conf
;
656 unsigned long length
;
659 if (unlikely(!iface
|| !mbo
))
661 if (unlikely(iface
->num_channels
<= channel
) || (channel
< 0))
664 mdev
= to_mdev(iface
);
665 conf
= &mdev
->conf
[channel
];
666 dev
= &mdev
->usb_device
->dev
;
668 if (!mdev
->usb_device
)
671 urb
= usb_alloc_urb(NO_ISOCHRONOUS_URB
, GFP_ATOMIC
);
673 dev_err(dev
, "Failed to allocate URB\n");
677 anchor
= kzalloc(sizeof(*anchor
), GFP_ATOMIC
);
684 init_completion(&anchor
->urb_compl
);
687 spin_lock_irqsave(&mdev
->anchor_list_lock
[channel
], flags
);
688 list_add_tail(&anchor
->list
, &mdev
->anchor_list
[channel
]);
689 spin_unlock_irqrestore(&mdev
->anchor_list_lock
[channel
], flags
);
691 if ((mdev
->padding_active
[channel
] == true) &&
692 (conf
->direction
& MOST_CH_TX
))
693 if (hdm_add_padding(mdev
, channel
, mbo
)) {
698 urb
->transfer_dma
= mbo
->bus_address
;
699 virt_address
= mbo
->virt_address
;
700 length
= mbo
->buffer_length
;
702 if (conf
->direction
& MOST_CH_TX
) {
703 usb_fill_bulk_urb(urb
, mdev
->usb_device
,
704 usb_sndbulkpipe(mdev
->usb_device
,
705 mdev
->ep_address
[channel
]),
708 hdm_write_completion
,
710 if (conf
->data_type
!= MOST_CH_ISOC_AVP
)
711 urb
->transfer_flags
|= URB_ZERO_PACKET
;
713 usb_fill_bulk_urb(urb
, mdev
->usb_device
,
714 usb_rcvbulkpipe(mdev
->usb_device
,
715 mdev
->ep_address
[channel
]),
721 urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
723 retval
= usb_submit_urb(urb
, GFP_KERNEL
);
725 dev_err(dev
, "URB submit failed with error %d.\n", retval
);
731 spin_lock_irqsave(&mdev
->anchor_list_lock
[channel
], flags
);
732 list_del(&anchor
->list
);
733 spin_unlock_irqrestore(&mdev
->anchor_list_lock
[channel
], flags
);
741 * hdm_configure_channel - receive channel configuration from core
743 * @channel: channel ID
744 * @conf: structure that holds the configuration information
746 static int hdm_configure_channel(struct most_interface
*iface
, int channel
,
747 struct most_channel_config
*conf
)
749 unsigned int num_frames
;
750 unsigned int frame_size
;
751 unsigned int temp_size
;
752 unsigned int tail_space
;
753 struct most_dev
*mdev
;
756 mdev
= to_mdev(iface
);
757 mdev
->is_channel_healthy
[channel
] = true;
758 dev
= &mdev
->usb_device
->dev
;
760 if (unlikely(!iface
|| !conf
)) {
761 dev_err(dev
, "Bad interface or config pointer.\n");
764 if (unlikely((channel
< 0) || (channel
>= iface
->num_channels
))) {
765 dev_err(dev
, "Channel ID out of range.\n");
768 if ((!conf
->num_buffers
) || (!conf
->buffer_size
)) {
769 dev_err(dev
, "Misconfig: buffer size or #buffers zero.\n");
773 if (!(conf
->data_type
== MOST_CH_SYNC
) &&
774 !((conf
->data_type
== MOST_CH_ISOC_AVP
) &&
775 (conf
->packets_per_xact
!= 0xFF))) {
776 mdev
->padding_active
[channel
] = false;
780 mdev
->padding_active
[channel
] = true;
781 temp_size
= conf
->buffer_size
;
783 if ((conf
->data_type
!= MOST_CH_SYNC
) &&
784 (conf
->data_type
!= MOST_CH_ISOC_AVP
)) {
785 dev_warn(dev
, "Unsupported data type\n");
789 frame_size
= get_stream_frame_size(conf
);
790 if ((frame_size
== 0) || (frame_size
> USB_MTU
)) {
791 dev_warn(dev
, "Misconfig: frame size wrong\n");
795 if (conf
->buffer_size
% frame_size
) {
798 tmp_val
= conf
->buffer_size
/ frame_size
;
799 conf
->buffer_size
= tmp_val
* frame_size
;
801 "Channel %d - rouding buffer size to %d bytes, "
802 "channel config says %d bytes\n",
808 num_frames
= conf
->buffer_size
/ frame_size
;
809 tail_space
= num_frames
* (USB_MTU
- frame_size
);
810 temp_size
+= tail_space
;
812 /* calculate extra length to comply w/ HW padding */
813 conf
->extra_len
= (CEILING(temp_size
, USB_MTU
) * USB_MTU
)
816 mdev
->conf
[channel
] = *conf
;
821 * hdm_update_netinfo - retrieve latest networking information
822 * @mdev: device interface
824 * This triggers the USB vendor requests to read the hardware address and
825 * the current link status of the attached device.
827 static int hdm_update_netinfo(struct most_dev
*mdev
)
829 struct device
*dev
= &mdev
->usb_device
->dev
;
834 if (!is_valid_ether_addr(mdev
->hw_addr
)) {
835 if (0 > drci_rd_reg(mdev
->usb_device
,
836 DRCI_REG_HW_ADDR_HI
, addr
)) {
837 dev_err(dev
, "Vendor request \"hw_addr_hi\" failed\n");
840 if (0 > drci_rd_reg(mdev
->usb_device
,
841 DRCI_REG_HW_ADDR_MI
, addr
+ 2)) {
842 dev_err(dev
, "Vendor request \"hw_addr_mid\" failed\n");
845 if (0 > drci_rd_reg(mdev
->usb_device
,
846 DRCI_REG_HW_ADDR_LO
, addr
+ 4)) {
847 dev_err(dev
, "Vendor request \"hw_addr_low\" failed\n");
850 mutex_lock(&mdev
->io_mutex
);
851 for (i
= 0; i
< 6; i
++)
852 mdev
->hw_addr
[i
] = addr
[i
];
853 mutex_unlock(&mdev
->io_mutex
);
856 if (0 > drci_rd_reg(mdev
->usb_device
, DRCI_REG_NI_STATE
, &link
)) {
857 dev_err(dev
, "Vendor request \"link status\" failed\n");
861 mutex_lock(&mdev
->io_mutex
);
862 mdev
->link_stat
= link
;
863 mutex_unlock(&mdev
->io_mutex
);
868 * hdm_request_netinfo - request network information
869 * @iface: pointer to interface
870 * @channel: channel ID
872 * This is used as trigger to set up the link status timer that
873 * polls for the NI state of the INIC every 2 seconds.
876 static void hdm_request_netinfo(struct most_interface
*iface
, int channel
)
878 struct most_dev
*mdev
;
881 mdev
= to_mdev(iface
);
882 mdev
->link_stat_timer
.expires
= jiffies
+ HZ
;
883 mod_timer(&mdev
->link_stat_timer
, mdev
->link_stat_timer
.expires
);
887 * link_stat_timer_handler - add work to link_stat work queue
888 * @data: pointer to USB device instance
890 * The handler runs in interrupt context. That's why we need to defer the
891 * tasks to a work queue.
893 static void link_stat_timer_handler(unsigned long data
)
895 struct most_dev
*mdev
= (struct most_dev
*)data
;
897 queue_work(schedule_usb_work
, &mdev
->poll_work_obj
);
898 mdev
->link_stat_timer
.expires
= jiffies
+ (2 * HZ
);
899 add_timer(&mdev
->link_stat_timer
);
903 * wq_netinfo - work queue function
904 * @wq_obj: object that holds data for our deferred work to do
906 * This retrieves the network interface status of the USB INIC
907 * and compares it with the current status. If the status has
908 * changed, it updates the status of the core.
910 static void wq_netinfo(struct work_struct
*wq_obj
)
912 struct most_dev
*mdev
;
913 int i
, prev_link_stat
;
916 mdev
= to_mdev_from_work(wq_obj
);
917 prev_link_stat
= mdev
->link_stat
;
919 for (i
= 0; i
< 6; i
++)
920 prev_hw_addr
[i
] = mdev
->hw_addr
[i
];
922 if (0 > hdm_update_netinfo(mdev
))
924 if ((prev_link_stat
!= mdev
->link_stat
) ||
925 (prev_hw_addr
[0] != mdev
->hw_addr
[0]) ||
926 (prev_hw_addr
[1] != mdev
->hw_addr
[1]) ||
927 (prev_hw_addr
[2] != mdev
->hw_addr
[2]) ||
928 (prev_hw_addr
[3] != mdev
->hw_addr
[3]) ||
929 (prev_hw_addr
[4] != mdev
->hw_addr
[4]) ||
930 (prev_hw_addr
[5] != mdev
->hw_addr
[5]))
931 most_deliver_netinfo(&mdev
->iface
, mdev
->link_stat
,
936 * wq_clear_halt - work queue function
937 * @wq_obj: work_struct object to execute
939 * This sends a clear_halt to the given USB pipe.
941 static void wq_clear_halt(struct work_struct
*wq_obj
)
943 struct buf_anchor
*anchor
;
944 struct most_dev
*mdev
;
947 unsigned int channel
;
950 anchor
= to_buf_anchor(wq_obj
);
953 mdev
= to_mdev(mbo
->ifp
);
954 channel
= mbo
->hdm_channel_id
;
956 if (usb_clear_halt(urb
->dev
, urb
->pipe
))
957 dev_warn(&mdev
->usb_device
->dev
, "Failed to reset endpoint.\n");
960 spin_lock_irqsave(&mdev
->anchor_list_lock
[channel
], flags
);
961 list_del(&anchor
->list
);
962 spin_unlock_irqrestore(&mdev
->anchor_list_lock
[channel
], flags
);
964 if (likely(mbo
->complete
))
966 if (mdev
->conf
[channel
].direction
& MOST_CH_TX
)
967 most_resume_enqueue(&mdev
->iface
, channel
);
973 * hdm_usb_fops - file operation table for USB driver
975 static const struct file_operations hdm_usb_fops
= {
976 .owner
= THIS_MODULE
,
980 * usb_device_id - ID table for HCD device probing
982 static struct usb_device_id usbid
[] = {
983 { USB_DEVICE(USB_VENDOR_ID_SMSC
, USB_DEV_ID_BRDG
), },
984 { USB_DEVICE(USB_VENDOR_ID_SMSC
, USB_DEV_ID_INIC
), },
985 { } /* Terminating entry */
988 #define MOST_DCI_RO_ATTR(_name) \
989 struct most_dci_attribute most_dci_attr_##_name = \
990 __ATTR(_name, S_IRUGO, show_value, NULL)
992 #define MOST_DCI_ATTR(_name) \
993 struct most_dci_attribute most_dci_attr_##_name = \
994 __ATTR(_name, S_IRUGO | S_IWUSR, show_value, store_value)
997 * struct most_dci_attribute - to access the attributes of a dci object
998 * @attr: attributes of a dci object
999 * @show: pointer to the show function
1000 * @store: pointer to the store function
1002 struct most_dci_attribute
{
1003 struct attribute attr
;
1004 ssize_t (*show
)(struct most_dci_obj
*d
,
1005 struct most_dci_attribute
*attr
,
1007 ssize_t (*store
)(struct most_dci_obj
*d
,
1008 struct most_dci_attribute
*attr
,
1012 #define to_dci_attr(a) container_of(a, struct most_dci_attribute, attr)
1016 * dci_attr_show - show function for dci object
1017 * @kobj: pointer to kobject
1018 * @attr: pointer to attribute struct
1021 static ssize_t
dci_attr_show(struct kobject
*kobj
, struct attribute
*attr
,
1024 struct most_dci_attribute
*dci_attr
= to_dci_attr(attr
);
1025 struct most_dci_obj
*dci_obj
= to_dci_obj(kobj
);
1027 if (!dci_attr
->show
)
1030 return dci_attr
->show(dci_obj
, dci_attr
, buf
);
1034 * dci_attr_store - store function for dci object
1035 * @kobj: pointer to kobject
1036 * @attr: pointer to attribute struct
1038 * @len: length of buffer
1040 static ssize_t
dci_attr_store(struct kobject
*kobj
,
1041 struct attribute
*attr
,
1045 struct most_dci_attribute
*dci_attr
= to_dci_attr(attr
);
1046 struct most_dci_obj
*dci_obj
= to_dci_obj(kobj
);
1048 if (!dci_attr
->store
)
1051 return dci_attr
->store(dci_obj
, dci_attr
, buf
, len
);
1054 static const struct sysfs_ops most_dci_sysfs_ops
= {
1055 .show
= dci_attr_show
,
1056 .store
= dci_attr_store
,
1060 * most_dci_release - release function for dci object
1061 * @kobj: pointer to kobject
1063 * This frees the memory allocated for the dci object
1065 static void most_dci_release(struct kobject
*kobj
)
1067 struct most_dci_obj
*dci_obj
= to_dci_obj(kobj
);
1072 static ssize_t
show_value(struct most_dci_obj
*dci_obj
,
1073 struct most_dci_attribute
*attr
, char *buf
)
1079 if (!strcmp(attr
->attr
.name
, "ni_state"))
1080 reg_addr
= DRCI_REG_NI_STATE
;
1081 else if (!strcmp(attr
->attr
.name
, "packet_bandwidth"))
1082 reg_addr
= DRCI_REG_PACKET_BW
;
1083 else if (!strcmp(attr
->attr
.name
, "node_address"))
1084 reg_addr
= DRCI_REG_NODE_ADDR
;
1085 else if (!strcmp(attr
->attr
.name
, "node_position"))
1086 reg_addr
= DRCI_REG_NODE_POS
;
1087 else if (!strcmp(attr
->attr
.name
, "mep_filter"))
1088 reg_addr
= DRCI_REG_MEP_FILTER
;
1089 else if (!strcmp(attr
->attr
.name
, "mep_hash0"))
1090 reg_addr
= DRCI_REG_HASH_TBL0
;
1091 else if (!strcmp(attr
->attr
.name
, "mep_hash1"))
1092 reg_addr
= DRCI_REG_HASH_TBL1
;
1093 else if (!strcmp(attr
->attr
.name
, "mep_hash2"))
1094 reg_addr
= DRCI_REG_HASH_TBL2
;
1095 else if (!strcmp(attr
->attr
.name
, "mep_hash3"))
1096 reg_addr
= DRCI_REG_HASH_TBL3
;
1097 else if (!strcmp(attr
->attr
.name
, "mep_eui48_hi"))
1098 reg_addr
= DRCI_REG_HW_ADDR_HI
;
1099 else if (!strcmp(attr
->attr
.name
, "mep_eui48_mi"))
1100 reg_addr
= DRCI_REG_HW_ADDR_MI
;
1101 else if (!strcmp(attr
->attr
.name
, "mep_eui48_lo"))
1102 reg_addr
= DRCI_REG_HW_ADDR_LO
;
1106 err
= drci_rd_reg(dci_obj
->usb_device
, reg_addr
, &tmp_val
);
1110 return snprintf(buf
, PAGE_SIZE
, "%04x\n", le16_to_cpu(tmp_val
));
1113 static ssize_t
store_value(struct most_dci_obj
*dci_obj
,
1114 struct most_dci_attribute
*attr
,
1115 const char *buf
, size_t count
)
1121 if (!strcmp(attr
->attr
.name
, "mep_filter"))
1122 reg_addr
= DRCI_REG_MEP_FILTER
;
1123 else if (!strcmp(attr
->attr
.name
, "mep_hash0"))
1124 reg_addr
= DRCI_REG_HASH_TBL0
;
1125 else if (!strcmp(attr
->attr
.name
, "mep_hash1"))
1126 reg_addr
= DRCI_REG_HASH_TBL1
;
1127 else if (!strcmp(attr
->attr
.name
, "mep_hash2"))
1128 reg_addr
= DRCI_REG_HASH_TBL2
;
1129 else if (!strcmp(attr
->attr
.name
, "mep_hash3"))
1130 reg_addr
= DRCI_REG_HASH_TBL3
;
1131 else if (!strcmp(attr
->attr
.name
, "mep_eui48_hi"))
1132 reg_addr
= DRCI_REG_HW_ADDR_HI
;
1133 else if (!strcmp(attr
->attr
.name
, "mep_eui48_mi"))
1134 reg_addr
= DRCI_REG_HW_ADDR_MI
;
1135 else if (!strcmp(attr
->attr
.name
, "mep_eui48_lo"))
1136 reg_addr
= DRCI_REG_HW_ADDR_LO
;
1140 err
= kstrtou16(buf
, 16, &v16
);
1144 err
= drci_wr_reg(dci_obj
->usb_device
, reg_addr
, cpu_to_le16(v16
));
1151 static MOST_DCI_RO_ATTR(ni_state
);
1152 static MOST_DCI_RO_ATTR(packet_bandwidth
);
1153 static MOST_DCI_RO_ATTR(node_address
);
1154 static MOST_DCI_RO_ATTR(node_position
);
1155 static MOST_DCI_ATTR(mep_filter
);
1156 static MOST_DCI_ATTR(mep_hash0
);
1157 static MOST_DCI_ATTR(mep_hash1
);
1158 static MOST_DCI_ATTR(mep_hash2
);
1159 static MOST_DCI_ATTR(mep_hash3
);
1160 static MOST_DCI_ATTR(mep_eui48_hi
);
1161 static MOST_DCI_ATTR(mep_eui48_mi
);
1162 static MOST_DCI_ATTR(mep_eui48_lo
);
1165 * most_dci_def_attrs - array of default attribute files of the dci object
1167 static struct attribute
*most_dci_def_attrs
[] = {
1168 &most_dci_attr_ni_state
.attr
,
1169 &most_dci_attr_packet_bandwidth
.attr
,
1170 &most_dci_attr_node_address
.attr
,
1171 &most_dci_attr_node_position
.attr
,
1172 &most_dci_attr_mep_filter
.attr
,
1173 &most_dci_attr_mep_hash0
.attr
,
1174 &most_dci_attr_mep_hash1
.attr
,
1175 &most_dci_attr_mep_hash2
.attr
,
1176 &most_dci_attr_mep_hash3
.attr
,
1177 &most_dci_attr_mep_eui48_hi
.attr
,
1178 &most_dci_attr_mep_eui48_mi
.attr
,
1179 &most_dci_attr_mep_eui48_lo
.attr
,
1186 static struct kobj_type most_dci_ktype
= {
1187 .sysfs_ops
= &most_dci_sysfs_ops
,
1188 .release
= most_dci_release
,
1189 .default_attrs
= most_dci_def_attrs
,
1193 * create_most_dci_obj - allocates a dci object
1194 * @parent: parent kobject
1196 * This creates a dci object and registers it with sysfs.
1197 * Returns a pointer to the object or NULL when something went wrong.
1200 most_dci_obj
*create_most_dci_obj(struct kobject
*parent
)
1202 struct most_dci_obj
*most_dci
;
1205 most_dci
= kzalloc(sizeof(*most_dci
), GFP_KERNEL
);
1209 retval
= kobject_init_and_add(&most_dci
->kobj
, &most_dci_ktype
, parent
,
1212 kobject_put(&most_dci
->kobj
);
1219 * destroy_most_dci_obj - DCI object release function
1220 * @p: pointer to dci object
1222 static void destroy_most_dci_obj(struct most_dci_obj
*p
)
1224 kobject_put(&p
->kobj
);
1228 * hdm_probe - probe function of USB device driver
1229 * @interface: Interface of the attached USB device
1230 * @id: Pointer to the USB ID table.
1232 * This allocates and initializes the device instance, adds the new
1233 * entry to the internal list, scans the USB descriptors and registers
1234 * the interface with the core.
1235 * Additionally, the DCI objects are created and the hardware is sync'd.
1237 * Return 0 on success. In case of an error a negative number is returned.
1240 hdm_probe(struct usb_interface
*interface
, const struct usb_device_id
*id
)
1243 unsigned int num_endpoints
;
1244 struct most_channel_capability
*tmp_cap
;
1245 struct most_dev
*mdev
;
1246 struct usb_device
*usb_dev
;
1248 struct usb_host_interface
*usb_iface_desc
;
1249 struct usb_endpoint_descriptor
*ep_desc
;
1252 usb_iface_desc
= interface
->cur_altsetting
;
1253 usb_dev
= interface_to_usbdev(interface
);
1254 dev
= &usb_dev
->dev
;
1255 mdev
= kzalloc(sizeof(*mdev
), GFP_KERNEL
);
1259 usb_set_intfdata(interface
, mdev
);
1260 num_endpoints
= usb_iface_desc
->desc
.bNumEndpoints
;
1261 mutex_init(&mdev
->io_mutex
);
1262 INIT_WORK(&mdev
->poll_work_obj
, wq_netinfo
);
1263 init_timer(&mdev
->link_stat_timer
);
1265 mdev
->usb_device
= usb_dev
;
1266 mdev
->link_stat_timer
.function
= link_stat_timer_handler
;
1267 mdev
->link_stat_timer
.data
= (unsigned long)mdev
;
1268 mdev
->link_stat_timer
.expires
= jiffies
+ (2 * HZ
);
1270 mdev
->iface
.mod
= hdm_usb_fops
.owner
;
1271 mdev
->iface
.interface
= ITYPE_USB
;
1272 mdev
->iface
.configure
= hdm_configure_channel
;
1273 mdev
->iface
.request_netinfo
= hdm_request_netinfo
;
1274 mdev
->iface
.enqueue
= hdm_enqueue
;
1275 mdev
->iface
.poison_channel
= hdm_poison_channel
;
1276 mdev
->iface
.description
= mdev
->description
;
1277 mdev
->iface
.num_channels
= num_endpoints
;
1279 snprintf(mdev
->description
, sizeof(mdev
->description
),
1280 "usb_device %d-%s:%d.%d",
1281 usb_dev
->bus
->busnum
,
1283 usb_dev
->config
->desc
.bConfigurationValue
,
1284 usb_iface_desc
->desc
.bInterfaceNumber
);
1286 mdev
->conf
= kcalloc(num_endpoints
, sizeof(*mdev
->conf
), GFP_KERNEL
);
1290 mdev
->cap
= kcalloc(num_endpoints
, sizeof(*mdev
->cap
), GFP_KERNEL
);
1294 mdev
->iface
.channel_vector
= mdev
->cap
;
1295 mdev
->iface
.priv
= NULL
;
1298 kcalloc(num_endpoints
, sizeof(*mdev
->ep_address
), GFP_KERNEL
);
1299 if (!mdev
->ep_address
)
1303 kcalloc(num_endpoints
, sizeof(*mdev
->anchor_list
), GFP_KERNEL
);
1304 if (!mdev
->anchor_list
)
1307 tmp_cap
= mdev
->cap
;
1308 for (i
= 0; i
< num_endpoints
; i
++) {
1309 ep_desc
= &usb_iface_desc
->endpoint
[i
].desc
;
1310 mdev
->ep_address
[i
] = ep_desc
->bEndpointAddress
;
1311 mdev
->padding_active
[i
] = false;
1312 mdev
->is_channel_healthy
[i
] = true;
1314 snprintf(&mdev
->suffix
[i
][0], MAX_SUFFIX_LEN
, "ep%02x",
1315 mdev
->ep_address
[i
]);
1317 tmp_cap
->name_suffix
= &mdev
->suffix
[i
][0];
1318 tmp_cap
->buffer_size_packet
= MAX_BUF_SIZE
;
1319 tmp_cap
->buffer_size_streaming
= MAX_BUF_SIZE
;
1320 tmp_cap
->num_buffers_packet
= BUF_CHAIN_SIZE
;
1321 tmp_cap
->num_buffers_streaming
= BUF_CHAIN_SIZE
;
1322 tmp_cap
->data_type
= MOST_CH_CONTROL
| MOST_CH_ASYNC
|
1323 MOST_CH_ISOC_AVP
| MOST_CH_SYNC
;
1324 if (ep_desc
->bEndpointAddress
& USB_DIR_IN
)
1325 tmp_cap
->direction
= MOST_CH_RX
;
1327 tmp_cap
->direction
= MOST_CH_TX
;
1329 INIT_LIST_HEAD(&mdev
->anchor_list
[i
]);
1330 spin_lock_init(&mdev
->anchor_list_lock
[i
]);
1332 dev_notice(dev
, "claimed gadget: Vendor=%4.4x ProdID=%4.4x Bus=%02x Device=%02x\n",
1333 le16_to_cpu(usb_dev
->descriptor
.idVendor
),
1334 le16_to_cpu(usb_dev
->descriptor
.idProduct
),
1335 usb_dev
->bus
->busnum
,
1338 dev_notice(dev
, "device path: /sys/bus/usb/devices/%d-%s:%d.%d\n",
1339 usb_dev
->bus
->busnum
,
1341 usb_dev
->config
->desc
.bConfigurationValue
,
1342 usb_iface_desc
->desc
.bInterfaceNumber
);
1344 mdev
->parent
= most_register_interface(&mdev
->iface
);
1345 if (IS_ERR(mdev
->parent
)) {
1346 ret
= PTR_ERR(mdev
->parent
);
1350 mutex_lock(&mdev
->io_mutex
);
1351 if (le16_to_cpu(usb_dev
->descriptor
.idProduct
) == USB_DEV_ID_INIC
) {
1352 /* this increments the reference count of the instance
1353 * object of the core
1355 mdev
->dci
= create_most_dci_obj(mdev
->parent
);
1357 mutex_unlock(&mdev
->io_mutex
);
1358 most_deregister_interface(&mdev
->iface
);
1363 kobject_uevent(&mdev
->dci
->kobj
, KOBJ_ADD
);
1364 mdev
->dci
->usb_device
= mdev
->usb_device
;
1365 trigger_resync_vr(usb_dev
);
1367 mutex_unlock(&mdev
->io_mutex
);
1371 kfree(mdev
->anchor_list
);
1373 kfree(mdev
->ep_address
);
1381 if (ret
== 0 || ret
== -ENOMEM
) {
1383 dev_err(dev
, "out of memory\n");
1389 * hdm_disconnect - disconnect function of USB device driver
1390 * @interface: Interface of the attached USB device
1392 * This deregisters the interface with the core, removes the kernel timer
1393 * and frees resources.
1395 * Context: hub kernel thread
1397 static void hdm_disconnect(struct usb_interface
*interface
)
1399 struct most_dev
*mdev
;
1401 mdev
= usb_get_intfdata(interface
);
1402 mutex_lock(&mdev
->io_mutex
);
1403 usb_set_intfdata(interface
, NULL
);
1404 mdev
->usb_device
= NULL
;
1405 mutex_unlock(&mdev
->io_mutex
);
1407 del_timer_sync(&mdev
->link_stat_timer
);
1408 cancel_work_sync(&mdev
->poll_work_obj
);
1410 destroy_most_dci_obj(mdev
->dci
);
1411 most_deregister_interface(&mdev
->iface
);
1413 kfree(mdev
->anchor_list
);
1416 kfree(mdev
->ep_address
);
1420 static struct usb_driver hdm_usb
= {
1424 .disconnect
= hdm_disconnect
,
1427 static int __init
hdm_usb_init(void)
1429 pr_info("hdm_usb_init()\n");
1430 if (usb_register(&hdm_usb
)) {
1431 pr_err("could not register hdm_usb driver\n");
1434 schedule_usb_work
= create_workqueue("hdmu_work");
1435 if (schedule_usb_work
== NULL
) {
1436 pr_err("could not create workqueue\n");
1437 usb_deregister(&hdm_usb
);
1443 static void __exit
hdm_usb_exit(void)
1445 pr_info("hdm_usb_exit()\n");
1446 destroy_workqueue(schedule_usb_work
);
1447 usb_deregister(&hdm_usb
);
1450 module_init(hdm_usb_init
);
1451 module_exit(hdm_usb_exit
);
1452 MODULE_LICENSE("GPL");
1453 MODULE_AUTHOR("Christian Gromm <christian.gromm@microchip.com>");
1454 MODULE_DESCRIPTION("HDM_4_USB");