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
44 #define USB_VENDOR_ID_SMSC 0x0424 /* VID: SMSC */
45 #define USB_DEV_ID_BRDG 0xC001 /* PID: USB Bridge */
46 #define USB_DEV_ID_OS81118 0xCF18 /* PID: USB OS81118 */
47 #define USB_DEV_ID_OS81119 0xCF19 /* PID: USB OS81119 */
48 #define USB_DEV_ID_OS81210 0xCF30 /* PID: USB OS81210 */
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_REG_BASE 0x1100
63 #define DRCI_COMMAND 0x02
64 #define DRCI_READ_REQ 0xA0
65 #define DRCI_WRITE_REQ 0xA1
68 * struct most_dci_obj - Direct Communication Interface
69 * @kobj:position in sysfs
70 * @usb_device: pointer to the usb device
71 * @reg_addr: register address for arbitrary DCI access
75 struct usb_device *usb_device;
79 #define to_dci_obj(p) container_of(p, struct most_dci_obj, kobj)
83 struct clear_hold_work {
84 struct work_struct ws;
85 struct most_dev *mdev;
90 #define to_clear_hold_work(w) container_of(w, struct clear_hold_work, ws)
93 * struct most_dev - holds all usb interface specific stuff
94 * @parent: parent object in sysfs
95 * @usb_device: pointer to usb device
96 * @iface: hardware interface
97 * @cap: channel capabilities
98 * @conf: channel configuration
99 * @dci: direct communication interface of hardware
100 * @hw_addr: MAC address of hardware
101 * @ep_address: endpoint address table
102 * @link_stat: link status of hardware
103 * @description: device description
104 * @suffix: suffix for channel name
105 * @channel_lock: synchronize channel access
106 * @padding_active: indicates channel uses padding
107 * @is_channel_healthy: health status table of each channel
108 * @busy_urbs: list of anchored items
109 * @io_mutex: synchronize I/O with disconnect
110 * @link_stat_timer: timer for link status reports
111 * @poll_work_obj: work for polling link status
114 struct kobject *parent;
115 struct usb_device *usb_device;
116 struct most_interface iface;
117 struct most_channel_capability *cap;
118 struct most_channel_config *conf;
119 struct most_dci_obj *dci;
123 char description[MAX_STRING_LEN];
124 char suffix[MAX_NUM_ENDPOINTS][MAX_SUFFIX_LEN];
125 spinlock_t channel_lock[MAX_NUM_ENDPOINTS]; /* sync channel access */
126 bool padding_active[MAX_NUM_ENDPOINTS];
127 bool is_channel_healthy[MAX_NUM_ENDPOINTS];
128 struct clear_hold_work clear_work[MAX_NUM_ENDPOINTS];
129 struct usb_anchor *busy_urbs;
130 struct mutex io_mutex;
131 struct timer_list link_stat_timer;
132 struct work_struct poll_work_obj;
135 #define to_mdev(d) container_of(d, struct most_dev, iface)
136 #define to_mdev_from_work(w) container_of(w, struct most_dev, poll_work_obj)
138 static void wq_clear_halt(struct work_struct *wq_obj);
139 static void wq_netinfo(struct work_struct *wq_obj);
142 * drci_rd_reg - read a DCI register
144 * @reg: register address
145 * @buf: buffer to store data
147 * This is reads data from INIC's direct register communication interface
149 static inline int drci_rd_reg(struct usb_device *dev, u16 reg, u16 *buf)
152 u16 *dma_buf = kzalloc(sizeof(u16), GFP_KERNEL);
153 u8 req_type = USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
158 retval = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
159 DRCI_READ_REQ, req_type,
161 reg, dma_buf, sizeof(u16), 5 * HZ);
162 *buf = le16_to_cpu(*dma_buf);
169 * drci_wr_reg - write a DCI register
171 * @reg: register address
172 * @data: data to write
174 * This is writes data to INIC's direct register communication interface
176 static inline int drci_wr_reg(struct usb_device *dev, u16 reg, u16 data)
178 return usb_control_msg(dev,
179 usb_sndctrlpipe(dev, 0),
181 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
190 * free_anchored_buffers - free device's anchored items
192 * @channel: channel ID
193 * @status: status of MBO termination
195 static void free_anchored_buffers(struct most_dev *mdev, unsigned int channel,
196 enum mbo_status_flags status)
201 while ((urb = usb_get_from_anchor(&mdev->busy_urbs[channel]))) {
204 if (mbo && mbo->complete) {
205 mbo->status = status;
206 mbo->processed_length = 0;
214 * get_stream_frame_size - calculate frame size of current configuration
215 * @cfg: channel configuration
217 static unsigned int get_stream_frame_size(struct most_channel_config *cfg)
219 unsigned int frame_size = 0;
220 unsigned int sub_size = cfg->subbuffer_size;
223 pr_warn("Misconfig: Subbuffer size zero.\n");
226 switch (cfg->data_type) {
228 frame_size = AV_PACKETS_PER_XACT * sub_size;
231 if (cfg->packets_per_xact == 0) {
232 pr_warn("Misconfig: Packets per XACT zero\n");
234 } else if (cfg->packets_per_xact == 0xFF) {
235 frame_size = (USB_MTU / sub_size) * sub_size;
237 frame_size = cfg->packets_per_xact * sub_size;
241 pr_warn("Query frame size of non-streaming channel\n");
248 * hdm_poison_channel - mark buffers of this channel as invalid
249 * @iface: pointer to the interface
250 * @channel: channel ID
252 * This unlinks all URBs submitted to the HCD,
253 * calls the associated completion function of the core and removes
254 * them from the list.
256 * Returns 0 on success or error code otherwise.
258 static int hdm_poison_channel(struct most_interface *iface, int channel)
260 struct most_dev *mdev = to_mdev(iface);
262 spinlock_t *lock; /* temp. lock */
264 if (unlikely(!iface)) {
265 dev_warn(&mdev->usb_device->dev, "Poison: Bad interface.\n");
268 if (unlikely(channel < 0 || channel >= iface->num_channels)) {
269 dev_warn(&mdev->usb_device->dev, "Channel ID out of range.\n");
273 lock = mdev->channel_lock + channel;
274 spin_lock_irqsave(lock, flags);
275 mdev->is_channel_healthy[channel] = false;
276 spin_unlock_irqrestore(lock, flags);
278 cancel_work_sync(&mdev->clear_work[channel].ws);
280 mutex_lock(&mdev->io_mutex);
281 free_anchored_buffers(mdev, channel, MBO_E_CLOSE);
282 if (mdev->padding_active[channel])
283 mdev->padding_active[channel] = false;
285 if (mdev->conf[channel].data_type == MOST_CH_ASYNC) {
286 del_timer_sync(&mdev->link_stat_timer);
287 cancel_work_sync(&mdev->poll_work_obj);
289 mutex_unlock(&mdev->io_mutex);
294 * hdm_add_padding - add padding bytes
296 * @channel: channel ID
297 * @mbo: buffer object
299 * This inserts the INIC hardware specific padding bytes into a streaming
302 static int hdm_add_padding(struct most_dev *mdev, int channel, struct mbo *mbo)
304 struct most_channel_config *conf = &mdev->conf[channel];
305 unsigned int frame_size = get_stream_frame_size(conf);
306 unsigned int j, num_frames;
307 u16 rd_addr, wr_addr;
311 num_frames = mbo->buffer_length / frame_size;
313 if (num_frames < 1) {
314 dev_err(&mdev->usb_device->dev,
315 "Missed minimal transfer unit.\n");
319 for (j = 1; j < num_frames; j++) {
320 wr_addr = (num_frames - j) * USB_MTU;
321 rd_addr = (num_frames - j) * frame_size;
322 memmove(mbo->virt_address + wr_addr,
323 mbo->virt_address + rd_addr,
326 mbo->buffer_length = num_frames * USB_MTU;
331 * hdm_remove_padding - remove padding bytes
333 * @channel: channel ID
334 * @mbo: buffer object
336 * This takes the INIC hardware specific padding bytes off a streaming
339 static int hdm_remove_padding(struct most_dev *mdev, int channel,
342 struct most_channel_config *const conf = &mdev->conf[channel];
343 unsigned int frame_size = get_stream_frame_size(conf);
344 unsigned int j, num_frames;
348 num_frames = mbo->processed_length / USB_MTU;
350 for (j = 1; j < num_frames; j++)
351 memmove(mbo->virt_address + frame_size * j,
352 mbo->virt_address + USB_MTU * j,
355 mbo->processed_length = frame_size * num_frames;
360 * hdm_write_completion - completion function for submitted Tx URBs
361 * @urb: the URB that has been completed
363 * This checks the status of the completed URB. In case the URB has been
364 * unlinked before, it is immediately freed. On any other error the MBO
365 * transfer flag is set. On success it frees allocated resources and calls
366 * the completion function.
368 * Context: interrupt!
370 static void hdm_write_completion(struct urb *urb)
372 struct mbo *mbo = urb->context;
373 struct most_dev *mdev = to_mdev(mbo->ifp);
374 unsigned int channel = mbo->hdm_channel_id;
375 struct device *dev = &mdev->usb_device->dev;
376 spinlock_t *lock = mdev->channel_lock + channel;
379 spin_lock_irqsave(lock, flags);
380 if (urb->status == -ENOENT || urb->status == -ECONNRESET ||
381 !mdev->is_channel_healthy[channel]) {
382 spin_unlock_irqrestore(lock, flags);
386 if (unlikely(urb->status && urb->status != -ESHUTDOWN)) {
387 mbo->processed_length = 0;
388 switch (urb->status) {
390 dev_warn(dev, "Broken OUT pipe detected\n");
391 mdev->is_channel_healthy[channel] = false;
392 spin_unlock_irqrestore(lock, flags);
393 mdev->clear_work[channel].pipe = urb->pipe;
394 schedule_work(&mdev->clear_work[channel].ws);
398 mbo->status = MBO_E_CLOSE;
401 mbo->status = MBO_E_INVAL;
405 mbo->status = MBO_SUCCESS;
406 mbo->processed_length = urb->actual_length;
409 spin_unlock_irqrestore(lock, flags);
411 if (likely(mbo->complete))
417 * hdm_read_completion - completion function for submitted Rx URBs
418 * @urb: the URB that has been completed
420 * This checks the status of the completed URB. In case the URB has been
421 * unlinked before it is immediately freed. On any other error the MBO transfer
422 * flag is set. On success it frees allocated resources, removes
423 * padding bytes -if necessary- and calls the completion function.
425 * Context: interrupt!
427 * **************************************************************************
428 * Error codes returned by in urb->status
429 * or in iso_frame_desc[n].status (for ISO)
430 * *************************************************************************
432 * USB device drivers may only test urb status values in completion handlers.
433 * This is because otherwise there would be a race between HCDs updating
434 * these values on one CPU, and device drivers testing them on another CPU.
436 * A transfer's actual_length may be positive even when an error has been
437 * reported. That's because transfers often involve several packets, so that
438 * one or more packets could finish before an error stops further endpoint I/O.
440 * For isochronous URBs, the urb status value is non-zero only if the URB is
441 * unlinked, the device is removed, the host controller is disabled or the total
442 * transferred length is less than the requested length and the URB_SHORT_NOT_OK
443 * flag is set. Completion handlers for isochronous URBs should only see
444 * urb->status set to zero, -ENOENT, -ECONNRESET, -ESHUTDOWN, or -EREMOTEIO.
445 * Individual frame descriptor status fields may report more status codes.
448 * 0 Transfer completed successfully
450 * -ENOENT URB was synchronously unlinked by usb_unlink_urb
452 * -EINPROGRESS URB still pending, no results yet
453 * (That is, if drivers see this it's a bug.)
455 * -EPROTO (*, **) a) bitstuff error
456 * b) no response packet received within the
457 * prescribed bus turn-around time
458 * c) unknown USB error
460 * -EILSEQ (*, **) a) CRC mismatch
461 * b) no response packet received within the
462 * prescribed bus turn-around time
463 * c) unknown USB error
465 * Note that often the controller hardware does not
466 * distinguish among cases a), b), and c), so a
467 * driver cannot tell whether there was a protocol
468 * error, a failure to respond (often caused by
469 * device disconnect), or some other fault.
471 * -ETIME (**) No response packet received within the prescribed
472 * bus turn-around time. This error may instead be
473 * reported as -EPROTO or -EILSEQ.
475 * -ETIMEDOUT Synchronous USB message functions use this code
476 * to indicate timeout expired before the transfer
477 * completed, and no other error was reported by HC.
479 * -EPIPE (**) Endpoint stalled. For non-control endpoints,
480 * reset this status with usb_clear_halt().
482 * -ECOMM During an IN transfer, the host controller
483 * received data from an endpoint faster than it
484 * could be written to system memory
486 * -ENOSR During an OUT transfer, the host controller
487 * could not retrieve data from system memory fast
488 * enough to keep up with the USB data rate
490 * -EOVERFLOW (*) The amount of data returned by the endpoint was
491 * greater than either the max packet size of the
492 * endpoint or the remaining buffer size. "Babble".
494 * -EREMOTEIO The data read from the endpoint did not fill the
495 * specified buffer, and URB_SHORT_NOT_OK was set in
496 * urb->transfer_flags.
498 * -ENODEV Device was removed. Often preceded by a burst of
499 * other errors, since the hub driver doesn't detect
500 * device removal events immediately.
502 * -EXDEV ISO transfer only partially completed
503 * (only set in iso_frame_desc[n].status, not urb->status)
505 * -EINVAL ISO madness, if this happens: Log off and go home
507 * -ECONNRESET URB was asynchronously unlinked by usb_unlink_urb
509 * -ESHUTDOWN The device or host controller has been disabled due
510 * to some problem that could not be worked around,
511 * such as a physical disconnect.
514 * (*) Error codes like -EPROTO, -EILSEQ and -EOVERFLOW normally indicate
515 * hardware problems such as bad devices (including firmware) or cables.
517 * (**) This is also one of several codes that different kinds of host
518 * controller use to indicate a transfer has failed because of device
519 * disconnect. In the interval before the hub driver starts disconnect
520 * processing, devices may receive such fault reports for every request.
522 * See <https://www.kernel.org/doc/Documentation/usb/error-codes.txt>
524 static void hdm_read_completion(struct urb *urb)
526 struct mbo *mbo = urb->context;
527 struct most_dev *mdev = to_mdev(mbo->ifp);
528 unsigned int channel = mbo->hdm_channel_id;
529 struct device *dev = &mdev->usb_device->dev;
530 spinlock_t *lock = mdev->channel_lock + channel;
533 spin_lock_irqsave(lock, flags);
534 if (urb->status == -ENOENT || urb->status == -ECONNRESET ||
535 !mdev->is_channel_healthy[channel]) {
536 spin_unlock_irqrestore(lock, flags);
540 if (unlikely(urb->status && urb->status != -ESHUTDOWN)) {
541 mbo->processed_length = 0;
542 switch (urb->status) {
544 dev_warn(dev, "Broken IN pipe detected\n");
545 mdev->is_channel_healthy[channel] = false;
546 spin_unlock_irqrestore(lock, flags);
547 mdev->clear_work[channel].pipe = urb->pipe;
548 schedule_work(&mdev->clear_work[channel].ws);
552 mbo->status = MBO_E_CLOSE;
555 dev_warn(dev, "Babble on IN pipe detected\n");
557 mbo->status = MBO_E_INVAL;
561 mbo->processed_length = urb->actual_length;
562 mbo->status = MBO_SUCCESS;
563 if (mdev->padding_active[channel] &&
564 hdm_remove_padding(mdev, channel, mbo)) {
565 mbo->processed_length = 0;
566 mbo->status = MBO_E_INVAL;
570 spin_unlock_irqrestore(lock, flags);
572 if (likely(mbo->complete))
578 * hdm_enqueue - receive a buffer to be used for data transfer
579 * @iface: interface to enqueue to
580 * @channel: ID of the channel
581 * @mbo: pointer to the buffer object
583 * This allocates a new URB and fills it according to the channel
584 * that is being used for transmission of data. Before the URB is
585 * submitted it is stored in the private anchor list.
587 * Returns 0 on success. On any error the URB is freed and a error code
590 * Context: Could in _some_ cases be interrupt!
592 static int hdm_enqueue(struct most_interface *iface, int channel,
595 struct most_dev *mdev;
596 struct most_channel_config *conf;
600 unsigned long length;
603 if (unlikely(!iface || !mbo))
605 if (unlikely(iface->num_channels <= channel || channel < 0))
608 mdev = to_mdev(iface);
609 conf = &mdev->conf[channel];
610 dev = &mdev->usb_device->dev;
612 if (!mdev->usb_device)
615 urb = usb_alloc_urb(NO_ISOCHRONOUS_URB, GFP_ATOMIC);
619 if ((conf->direction & MOST_CH_TX) && mdev->padding_active[channel] &&
620 hdm_add_padding(mdev, channel, mbo)) {
625 urb->transfer_dma = mbo->bus_address;
626 virt_address = mbo->virt_address;
627 length = mbo->buffer_length;
629 if (conf->direction & MOST_CH_TX) {
630 usb_fill_bulk_urb(urb, mdev->usb_device,
631 usb_sndbulkpipe(mdev->usb_device,
632 mdev->ep_address[channel]),
635 hdm_write_completion,
637 if (conf->data_type != MOST_CH_ISOC)
638 urb->transfer_flags |= URB_ZERO_PACKET;
640 usb_fill_bulk_urb(urb, mdev->usb_device,
641 usb_rcvbulkpipe(mdev->usb_device,
642 mdev->ep_address[channel]),
644 length + conf->extra_len,
648 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
650 usb_anchor_urb(urb, &mdev->busy_urbs[channel]);
652 retval = usb_submit_urb(urb, GFP_KERNEL);
654 dev_err(dev, "URB submit failed with error %d.\n", retval);
660 usb_unanchor_urb(urb);
667 * hdm_configure_channel - receive channel configuration from core
669 * @channel: channel ID
670 * @conf: structure that holds the configuration information
672 static int hdm_configure_channel(struct most_interface *iface, int channel,
673 struct most_channel_config *conf)
675 unsigned int num_frames;
676 unsigned int frame_size;
677 unsigned int temp_size;
678 unsigned int tail_space;
679 struct most_dev *mdev = to_mdev(iface);
680 struct device *dev = &mdev->usb_device->dev;
682 mdev->is_channel_healthy[channel] = true;
683 mdev->clear_work[channel].channel = channel;
684 mdev->clear_work[channel].mdev = mdev;
685 INIT_WORK(&mdev->clear_work[channel].ws, wq_clear_halt);
687 if (unlikely(!iface || !conf)) {
688 dev_err(dev, "Bad interface or config pointer.\n");
691 if (unlikely(channel < 0 || channel >= iface->num_channels)) {
692 dev_err(dev, "Channel ID out of range.\n");
695 if (!conf->num_buffers || !conf->buffer_size) {
696 dev_err(dev, "Misconfig: buffer size or #buffers zero.\n");
700 if (conf->data_type != MOST_CH_SYNC &&
701 !(conf->data_type == MOST_CH_ISOC &&
702 conf->packets_per_xact != 0xFF)) {
703 mdev->padding_active[channel] = false;
707 mdev->padding_active[channel] = true;
708 temp_size = conf->buffer_size;
710 frame_size = get_stream_frame_size(conf);
711 if (frame_size == 0 || frame_size > USB_MTU) {
712 dev_warn(dev, "Misconfig: frame size wrong\n");
716 if (conf->buffer_size % frame_size) {
719 tmp_val = conf->buffer_size / frame_size;
720 conf->buffer_size = tmp_val * frame_size;
722 "Channel %d - rounding buffer size to %d bytes, channel config says %d bytes\n",
728 num_frames = conf->buffer_size / frame_size;
729 tail_space = num_frames * (USB_MTU - frame_size);
730 temp_size += tail_space;
732 /* calculate extra length to comply w/ HW padding */
733 conf->extra_len = (DIV_ROUND_UP(temp_size, USB_MTU) * USB_MTU)
736 mdev->conf[channel] = *conf;
741 * hdm_update_netinfo - retrieve latest networking information
742 * @mdev: device interface
744 * This triggers the USB vendor requests to read the hardware address and
745 * the current link status of the attached device.
747 static int hdm_update_netinfo(struct most_dev *mdev)
749 struct usb_device *usb_device = mdev->usb_device;
750 struct device *dev = &usb_device->dev;
751 u16 hi, mi, lo, link;
753 if (!is_valid_ether_addr(mdev->hw_addr)) {
754 if (drci_rd_reg(usb_device, DRCI_REG_HW_ADDR_HI, &hi) < 0) {
755 dev_err(dev, "Vendor request \"hw_addr_hi\" failed\n");
759 if (drci_rd_reg(usb_device, DRCI_REG_HW_ADDR_MI, &mi) < 0) {
760 dev_err(dev, "Vendor request \"hw_addr_mid\" failed\n");
764 if (drci_rd_reg(usb_device, DRCI_REG_HW_ADDR_LO, &lo) < 0) {
765 dev_err(dev, "Vendor request \"hw_addr_low\" failed\n");
769 mutex_lock(&mdev->io_mutex);
770 mdev->hw_addr[0] = hi >> 8;
771 mdev->hw_addr[1] = hi;
772 mdev->hw_addr[2] = mi >> 8;
773 mdev->hw_addr[3] = mi;
774 mdev->hw_addr[4] = lo >> 8;
775 mdev->hw_addr[5] = lo;
776 mutex_unlock(&mdev->io_mutex);
779 if (drci_rd_reg(usb_device, DRCI_REG_NI_STATE, &link) < 0) {
780 dev_err(dev, "Vendor request \"link status\" failed\n");
784 mutex_lock(&mdev->io_mutex);
785 mdev->link_stat = link;
786 mutex_unlock(&mdev->io_mutex);
791 * hdm_request_netinfo - request network information
792 * @iface: pointer to interface
793 * @channel: channel ID
795 * This is used as trigger to set up the link status timer that
796 * polls for the NI state of the INIC every 2 seconds.
799 static void hdm_request_netinfo(struct most_interface *iface, int channel)
801 struct most_dev *mdev;
804 mdev = to_mdev(iface);
805 mdev->link_stat_timer.expires = jiffies + HZ;
806 mod_timer(&mdev->link_stat_timer, mdev->link_stat_timer.expires);
810 * link_stat_timer_handler - add work to link_stat work queue
811 * @data: pointer to USB device instance
813 * The handler runs in interrupt context. That's why we need to defer the
814 * tasks to a work queue.
816 static void link_stat_timer_handler(unsigned long data)
818 struct most_dev *mdev = (struct most_dev *)data;
820 schedule_work(&mdev->poll_work_obj);
821 mdev->link_stat_timer.expires = jiffies + (2 * HZ);
822 add_timer(&mdev->link_stat_timer);
826 * wq_netinfo - work queue function
827 * @wq_obj: object that holds data for our deferred work to do
829 * This retrieves the network interface status of the USB INIC
830 * and compares it with the current status. If the status has
831 * changed, it updates the status of the core.
833 static void wq_netinfo(struct work_struct *wq_obj)
835 struct most_dev *mdev = to_mdev_from_work(wq_obj);
836 int i, prev_link_stat = mdev->link_stat;
839 for (i = 0; i < 6; i++)
840 prev_hw_addr[i] = mdev->hw_addr[i];
842 if (hdm_update_netinfo(mdev) < 0)
844 if (prev_link_stat != mdev->link_stat ||
845 prev_hw_addr[0] != mdev->hw_addr[0] ||
846 prev_hw_addr[1] != mdev->hw_addr[1] ||
847 prev_hw_addr[2] != mdev->hw_addr[2] ||
848 prev_hw_addr[3] != mdev->hw_addr[3] ||
849 prev_hw_addr[4] != mdev->hw_addr[4] ||
850 prev_hw_addr[5] != mdev->hw_addr[5])
851 most_deliver_netinfo(&mdev->iface, mdev->link_stat,
856 * wq_clear_halt - work queue function
857 * @wq_obj: work_struct object to execute
859 * This sends a clear_halt to the given USB pipe.
861 static void wq_clear_halt(struct work_struct *wq_obj)
863 struct clear_hold_work *clear_work = to_clear_hold_work(wq_obj);
864 struct most_dev *mdev = clear_work->mdev;
865 unsigned int channel = clear_work->channel;
866 int pipe = clear_work->pipe;
868 mutex_lock(&mdev->io_mutex);
869 most_stop_enqueue(&mdev->iface, channel);
870 free_anchored_buffers(mdev, channel, MBO_E_INVAL);
871 if (usb_clear_halt(mdev->usb_device, pipe))
872 dev_warn(&mdev->usb_device->dev, "Failed to reset endpoint.\n");
874 mdev->is_channel_healthy[channel] = true;
875 most_resume_enqueue(&mdev->iface, channel);
876 mutex_unlock(&mdev->io_mutex);
880 * hdm_usb_fops - file operation table for USB driver
882 static const struct file_operations hdm_usb_fops = {
883 .owner = THIS_MODULE,
887 * usb_device_id - ID table for HCD device probing
889 static struct usb_device_id usbid[] = {
890 { USB_DEVICE(USB_VENDOR_ID_SMSC, USB_DEV_ID_BRDG), },
891 { USB_DEVICE(USB_VENDOR_ID_SMSC, USB_DEV_ID_OS81118), },
892 { USB_DEVICE(USB_VENDOR_ID_SMSC, USB_DEV_ID_OS81119), },
893 { USB_DEVICE(USB_VENDOR_ID_SMSC, USB_DEV_ID_OS81210), },
894 { } /* Terminating entry */
897 #define MOST_DCI_RO_ATTR(_name) \
898 struct most_dci_attribute most_dci_attr_##_name = \
899 __ATTR(_name, S_IRUGO, show_value, NULL)
901 #define MOST_DCI_ATTR(_name) \
902 struct most_dci_attribute most_dci_attr_##_name = \
903 __ATTR(_name, S_IRUGO | S_IWUSR, show_value, store_value)
905 #define MOST_DCI_WO_ATTR(_name) \
906 struct most_dci_attribute most_dci_attr_##_name = \
907 __ATTR(_name, S_IWUSR, NULL, store_value)
910 * struct most_dci_attribute - to access the attributes of a dci object
911 * @attr: attributes of a dci object
912 * @show: pointer to the show function
913 * @store: pointer to the store function
915 struct most_dci_attribute {
916 struct attribute attr;
917 ssize_t (*show)(struct most_dci_obj *d,
918 struct most_dci_attribute *attr,
920 ssize_t (*store)(struct most_dci_obj *d,
921 struct most_dci_attribute *attr,
926 #define to_dci_attr(a) container_of(a, struct most_dci_attribute, attr)
929 * dci_attr_show - show function for dci object
930 * @kobj: pointer to kobject
931 * @attr: pointer to attribute struct
934 static ssize_t dci_attr_show(struct kobject *kobj, struct attribute *attr,
937 struct most_dci_attribute *dci_attr = to_dci_attr(attr);
938 struct most_dci_obj *dci_obj = to_dci_obj(kobj);
943 return dci_attr->show(dci_obj, dci_attr, buf);
947 * dci_attr_store - store function for dci object
948 * @kobj: pointer to kobject
949 * @attr: pointer to attribute struct
951 * @len: length of buffer
953 static ssize_t dci_attr_store(struct kobject *kobj,
954 struct attribute *attr,
958 struct most_dci_attribute *dci_attr = to_dci_attr(attr);
959 struct most_dci_obj *dci_obj = to_dci_obj(kobj);
961 if (!dci_attr->store)
964 return dci_attr->store(dci_obj, dci_attr, buf, len);
967 static const struct sysfs_ops most_dci_sysfs_ops = {
968 .show = dci_attr_show,
969 .store = dci_attr_store,
973 * most_dci_release - release function for dci object
974 * @kobj: pointer to kobject
976 * This frees the memory allocated for the dci object
978 static void most_dci_release(struct kobject *kobj)
980 struct most_dci_obj *dci_obj = to_dci_obj(kobj);
990 static const struct regs ro_regs[] = {
991 { "ni_state", DRCI_REG_NI_STATE },
992 { "packet_bandwidth", DRCI_REG_PACKET_BW },
993 { "node_address", DRCI_REG_NODE_ADDR },
994 { "node_position", DRCI_REG_NODE_POS },
997 static const struct regs rw_regs[] = {
998 { "mep_filter", DRCI_REG_MEP_FILTER },
999 { "mep_hash0", DRCI_REG_HASH_TBL0 },
1000 { "mep_hash1", DRCI_REG_HASH_TBL1 },
1001 { "mep_hash2", DRCI_REG_HASH_TBL2 },
1002 { "mep_hash3", DRCI_REG_HASH_TBL3 },
1003 { "mep_eui48_hi", DRCI_REG_HW_ADDR_HI },
1004 { "mep_eui48_mi", DRCI_REG_HW_ADDR_MI },
1005 { "mep_eui48_lo", DRCI_REG_HW_ADDR_LO },
1008 static int get_stat_reg_addr(const struct regs *regs, int size,
1009 const char *name, u16 *reg_addr)
1013 for (i = 0; i < size; i++) {
1014 if (!strcmp(name, regs[i].name)) {
1015 *reg_addr = regs[i].reg;
1022 #define get_static_reg_addr(regs, name, reg_addr) \
1023 get_stat_reg_addr(regs, ARRAY_SIZE(regs), name, reg_addr)
1025 static ssize_t show_value(struct most_dci_obj *dci_obj,
1026 struct most_dci_attribute *attr, char *buf)
1028 const char *name = attr->attr.name;
1033 if (!strcmp(name, "arb_address"))
1034 return snprintf(buf, PAGE_SIZE, "%04x\n", dci_obj->reg_addr);
1036 if (!strcmp(name, "arb_value"))
1037 reg_addr = dci_obj->reg_addr;
1038 else if (get_static_reg_addr(ro_regs, name, ®_addr) &&
1039 get_static_reg_addr(rw_regs, name, ®_addr))
1042 err = drci_rd_reg(dci_obj->usb_device, reg_addr, &val);
1046 return snprintf(buf, PAGE_SIZE, "%04x\n", val);
1049 static ssize_t store_value(struct most_dci_obj *dci_obj,
1050 struct most_dci_attribute *attr,
1051 const char *buf, size_t count)
1055 const char *name = attr->attr.name;
1056 int err = kstrtou16(buf, 16, &val);
1061 if (!strcmp(name, "arb_address")) {
1062 dci_obj->reg_addr = val;
1066 if (!strcmp(name, "arb_value")) {
1067 reg_addr = dci_obj->reg_addr;
1068 } else if (!strcmp(name, "sync_ep")) {
1071 reg_addr = DRCI_REG_BASE + DRCI_COMMAND + ep * 16;
1073 } else if (get_static_reg_addr(ro_regs, name, ®_addr)) {
1077 err = drci_wr_reg(dci_obj->usb_device, reg_addr, val);
1084 static MOST_DCI_RO_ATTR(ni_state);
1085 static MOST_DCI_RO_ATTR(packet_bandwidth);
1086 static MOST_DCI_RO_ATTR(node_address);
1087 static MOST_DCI_RO_ATTR(node_position);
1088 static MOST_DCI_WO_ATTR(sync_ep);
1089 static MOST_DCI_ATTR(mep_filter);
1090 static MOST_DCI_ATTR(mep_hash0);
1091 static MOST_DCI_ATTR(mep_hash1);
1092 static MOST_DCI_ATTR(mep_hash2);
1093 static MOST_DCI_ATTR(mep_hash3);
1094 static MOST_DCI_ATTR(mep_eui48_hi);
1095 static MOST_DCI_ATTR(mep_eui48_mi);
1096 static MOST_DCI_ATTR(mep_eui48_lo);
1097 static MOST_DCI_ATTR(arb_address);
1098 static MOST_DCI_ATTR(arb_value);
1101 * most_dci_def_attrs - array of default attribute files of the dci object
1103 static struct attribute *most_dci_def_attrs[] = {
1104 &most_dci_attr_ni_state.attr,
1105 &most_dci_attr_packet_bandwidth.attr,
1106 &most_dci_attr_node_address.attr,
1107 &most_dci_attr_node_position.attr,
1108 &most_dci_attr_sync_ep.attr,
1109 &most_dci_attr_mep_filter.attr,
1110 &most_dci_attr_mep_hash0.attr,
1111 &most_dci_attr_mep_hash1.attr,
1112 &most_dci_attr_mep_hash2.attr,
1113 &most_dci_attr_mep_hash3.attr,
1114 &most_dci_attr_mep_eui48_hi.attr,
1115 &most_dci_attr_mep_eui48_mi.attr,
1116 &most_dci_attr_mep_eui48_lo.attr,
1117 &most_dci_attr_arb_address.attr,
1118 &most_dci_attr_arb_value.attr,
1125 static struct kobj_type most_dci_ktype = {
1126 .sysfs_ops = &most_dci_sysfs_ops,
1127 .release = most_dci_release,
1128 .default_attrs = most_dci_def_attrs,
1132 * create_most_dci_obj - allocates a dci object
1133 * @parent: parent kobject
1135 * This creates a dci object and registers it with sysfs.
1136 * Returns a pointer to the object or NULL when something went wrong.
1139 most_dci_obj *create_most_dci_obj(struct kobject *parent)
1141 struct most_dci_obj *most_dci = kzalloc(sizeof(*most_dci), GFP_KERNEL);
1147 retval = kobject_init_and_add(&most_dci->kobj, &most_dci_ktype, parent,
1150 kobject_put(&most_dci->kobj);
1157 * destroy_most_dci_obj - DCI object release function
1158 * @p: pointer to dci object
1160 static void destroy_most_dci_obj(struct most_dci_obj *p)
1162 kobject_put(&p->kobj);
1166 * hdm_probe - probe function of USB device driver
1167 * @interface: Interface of the attached USB device
1168 * @id: Pointer to the USB ID table.
1170 * This allocates and initializes the device instance, adds the new
1171 * entry to the internal list, scans the USB descriptors and registers
1172 * the interface with the core.
1173 * Additionally, the DCI objects are created and the hardware is sync'd.
1175 * Return 0 on success. In case of an error a negative number is returned.
1178 hdm_probe(struct usb_interface *interface, const struct usb_device_id *id)
1180 struct usb_host_interface *usb_iface_desc = interface->cur_altsetting;
1181 struct usb_device *usb_dev = interface_to_usbdev(interface);
1182 struct device *dev = &usb_dev->dev;
1183 struct most_dev *mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
1185 unsigned int num_endpoints;
1186 struct most_channel_capability *tmp_cap;
1187 struct usb_endpoint_descriptor *ep_desc;
1194 usb_set_intfdata(interface, mdev);
1195 num_endpoints = usb_iface_desc->desc.bNumEndpoints;
1196 mutex_init(&mdev->io_mutex);
1197 INIT_WORK(&mdev->poll_work_obj, wq_netinfo);
1198 setup_timer(&mdev->link_stat_timer, link_stat_timer_handler,
1199 (unsigned long)mdev);
1201 mdev->usb_device = usb_dev;
1202 mdev->link_stat_timer.expires = jiffies + (2 * HZ);
1204 mdev->iface.mod = hdm_usb_fops.owner;
1205 mdev->iface.interface = ITYPE_USB;
1206 mdev->iface.configure = hdm_configure_channel;
1207 mdev->iface.request_netinfo = hdm_request_netinfo;
1208 mdev->iface.enqueue = hdm_enqueue;
1209 mdev->iface.poison_channel = hdm_poison_channel;
1210 mdev->iface.description = mdev->description;
1211 mdev->iface.num_channels = num_endpoints;
1213 snprintf(mdev->description, sizeof(mdev->description),
1214 "usb_device %d-%s:%d.%d",
1215 usb_dev->bus->busnum,
1217 usb_dev->config->desc.bConfigurationValue,
1218 usb_iface_desc->desc.bInterfaceNumber);
1220 mdev->conf = kcalloc(num_endpoints, sizeof(*mdev->conf), GFP_KERNEL);
1224 mdev->cap = kcalloc(num_endpoints, sizeof(*mdev->cap), GFP_KERNEL);
1228 mdev->iface.channel_vector = mdev->cap;
1229 mdev->iface.priv = NULL;
1232 kcalloc(num_endpoints, sizeof(*mdev->ep_address), GFP_KERNEL);
1233 if (!mdev->ep_address)
1237 kcalloc(num_endpoints, sizeof(*mdev->busy_urbs), GFP_KERNEL);
1238 if (!mdev->busy_urbs)
1241 tmp_cap = mdev->cap;
1242 for (i = 0; i < num_endpoints; i++) {
1243 ep_desc = &usb_iface_desc->endpoint[i].desc;
1244 mdev->ep_address[i] = ep_desc->bEndpointAddress;
1245 mdev->padding_active[i] = false;
1246 mdev->is_channel_healthy[i] = true;
1248 snprintf(&mdev->suffix[i][0], MAX_SUFFIX_LEN, "ep%02x",
1249 mdev->ep_address[i]);
1251 tmp_cap->name_suffix = &mdev->suffix[i][0];
1252 tmp_cap->buffer_size_packet = MAX_BUF_SIZE;
1253 tmp_cap->buffer_size_streaming = MAX_BUF_SIZE;
1254 tmp_cap->num_buffers_packet = BUF_CHAIN_SIZE;
1255 tmp_cap->num_buffers_streaming = BUF_CHAIN_SIZE;
1256 tmp_cap->data_type = MOST_CH_CONTROL | MOST_CH_ASYNC |
1257 MOST_CH_ISOC | MOST_CH_SYNC;
1258 if (usb_endpoint_dir_in(ep_desc))
1259 tmp_cap->direction = MOST_CH_RX;
1261 tmp_cap->direction = MOST_CH_TX;
1263 init_usb_anchor(&mdev->busy_urbs[i]);
1264 spin_lock_init(&mdev->channel_lock[i]);
1265 err = drci_wr_reg(usb_dev,
1266 DRCI_REG_BASE + DRCI_COMMAND +
1267 ep_desc->bEndpointAddress * 16,
1270 dev_warn(dev, "DCI Sync for EP %02x failed",
1271 ep_desc->bEndpointAddress);
1273 dev_notice(dev, "claimed gadget: Vendor=%4.4x ProdID=%4.4x Bus=%02x Device=%02x\n",
1274 le16_to_cpu(usb_dev->descriptor.idVendor),
1275 le16_to_cpu(usb_dev->descriptor.idProduct),
1276 usb_dev->bus->busnum,
1279 dev_notice(dev, "device path: /sys/bus/usb/devices/%d-%s:%d.%d\n",
1280 usb_dev->bus->busnum,
1282 usb_dev->config->desc.bConfigurationValue,
1283 usb_iface_desc->desc.bInterfaceNumber);
1285 mdev->parent = most_register_interface(&mdev->iface);
1286 if (IS_ERR(mdev->parent)) {
1287 ret = PTR_ERR(mdev->parent);
1291 mutex_lock(&mdev->io_mutex);
1292 if (le16_to_cpu(usb_dev->descriptor.idProduct) == USB_DEV_ID_OS81118 ||
1293 le16_to_cpu(usb_dev->descriptor.idProduct) == USB_DEV_ID_OS81119 ||
1294 le16_to_cpu(usb_dev->descriptor.idProduct) == USB_DEV_ID_OS81210) {
1295 /* this increments the reference count of the instance
1296 * object of the core
1298 mdev->dci = create_most_dci_obj(mdev->parent);
1300 mutex_unlock(&mdev->io_mutex);
1301 most_deregister_interface(&mdev->iface);
1306 kobject_uevent(&mdev->dci->kobj, KOBJ_ADD);
1307 mdev->dci->usb_device = mdev->usb_device;
1309 mutex_unlock(&mdev->io_mutex);
1313 kfree(mdev->busy_urbs);
1315 kfree(mdev->ep_address);
1323 if (ret == 0 || ret == -ENOMEM) {
1325 dev_err(dev, "out of memory\n");
1331 * hdm_disconnect - disconnect function of USB device driver
1332 * @interface: Interface of the attached USB device
1334 * This deregisters the interface with the core, removes the kernel timer
1335 * and frees resources.
1337 * Context: hub kernel thread
1339 static void hdm_disconnect(struct usb_interface *interface)
1341 struct most_dev *mdev = usb_get_intfdata(interface);
1343 mutex_lock(&mdev->io_mutex);
1344 usb_set_intfdata(interface, NULL);
1345 mdev->usb_device = NULL;
1346 mutex_unlock(&mdev->io_mutex);
1348 del_timer_sync(&mdev->link_stat_timer);
1349 cancel_work_sync(&mdev->poll_work_obj);
1351 destroy_most_dci_obj(mdev->dci);
1352 most_deregister_interface(&mdev->iface);
1354 kfree(mdev->busy_urbs);
1357 kfree(mdev->ep_address);
1361 static struct usb_driver hdm_usb = {
1365 .disconnect = hdm_disconnect,
1368 static int __init hdm_usb_init(void)
1370 pr_info("hdm_usb_init()\n");
1371 if (usb_register(&hdm_usb)) {
1372 pr_err("could not register hdm_usb driver\n");
1379 static void __exit hdm_usb_exit(void)
1381 pr_info("hdm_usb_exit()\n");
1382 usb_deregister(&hdm_usb);
1385 module_init(hdm_usb_init);
1386 module_exit(hdm_usb_exit);
1387 MODULE_LICENSE("GPL");
1388 MODULE_AUTHOR("Christian Gromm <christian.gromm@microchip.com>");
1389 MODULE_DESCRIPTION("HDM_4_USB");