1 // SPDX-License-Identifier: GPL-2.0
3 * usb.c - Hardware dependent module for USB
5 * Copyright (C) 2013-2015 Microchip Technology Germany II GmbH & Co. KG
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 #include <linux/module.h>
11 #include <linux/usb.h>
12 #include <linux/slab.h>
13 #include <linux/init.h>
14 #include <linux/cdev.h>
15 #include <linux/device.h>
16 #include <linux/list.h>
17 #include <linux/completion.h>
18 #include <linux/mutex.h>
19 #include <linux/spinlock.h>
20 #include <linux/interrupt.h>
21 #include <linux/workqueue.h>
22 #include <linux/sysfs.h>
23 #include <linux/dma-mapping.h>
24 #include <linux/etherdevice.h>
25 #include <linux/uaccess.h>
26 #include "most/core.h"
29 #define NO_ISOCHRONOUS_URB 0
30 #define AV_PACKETS_PER_XACT 2
31 #define BUF_CHAIN_SIZE 0xFFFF
32 #define MAX_NUM_ENDPOINTS 30
33 #define MAX_SUFFIX_LEN 10
34 #define MAX_STRING_LEN 80
35 #define MAX_BUF_SIZE 0xFFFF
37 #define USB_VENDOR_ID_SMSC 0x0424 /* VID: SMSC */
38 #define USB_DEV_ID_BRDG 0xC001 /* PID: USB Bridge */
39 #define USB_DEV_ID_OS81118 0xCF18 /* PID: USB OS81118 */
40 #define USB_DEV_ID_OS81119 0xCF19 /* PID: USB OS81119 */
41 #define USB_DEV_ID_OS81210 0xCF30 /* PID: USB OS81210 */
43 #define DRCI_REG_NI_STATE 0x0100
44 #define DRCI_REG_PACKET_BW 0x0101
45 #define DRCI_REG_NODE_ADDR 0x0102
46 #define DRCI_REG_NODE_POS 0x0103
47 #define DRCI_REG_MEP_FILTER 0x0140
48 #define DRCI_REG_HASH_TBL0 0x0141
49 #define DRCI_REG_HASH_TBL1 0x0142
50 #define DRCI_REG_HASH_TBL2 0x0143
51 #define DRCI_REG_HASH_TBL3 0x0144
52 #define DRCI_REG_HW_ADDR_HI 0x0145
53 #define DRCI_REG_HW_ADDR_MI 0x0146
54 #define DRCI_REG_HW_ADDR_LO 0x0147
55 #define DRCI_REG_BASE 0x1100
56 #define DRCI_COMMAND 0x02
57 #define DRCI_READ_REQ 0xA0
58 #define DRCI_WRITE_REQ 0xA1
61 * struct most_dci_obj - Direct Communication Interface
62 * @kobj:position in sysfs
63 * @usb_device: pointer to the usb device
64 * @reg_addr: register address for arbitrary DCI access
68 struct usb_device *usb_device;
72 #define to_dci_obj(p) container_of(p, struct most_dci_obj, dev)
76 struct clear_hold_work {
77 struct work_struct ws;
78 struct most_dev *mdev;
83 #define to_clear_hold_work(w) container_of(w, struct clear_hold_work, ws)
86 * struct most_dev - holds all usb interface specific stuff
87 * @usb_device: pointer to usb device
88 * @iface: hardware interface
89 * @cap: channel capabilities
90 * @conf: channel configuration
91 * @dci: direct communication interface of hardware
92 * @ep_address: endpoint address table
93 * @description: device description
94 * @suffix: suffix for channel name
95 * @channel_lock: synchronize channel access
96 * @padding_active: indicates channel uses padding
97 * @is_channel_healthy: health status table of each channel
98 * @busy_urbs: list of anchored items
99 * @io_mutex: synchronize I/O with disconnect
100 * @link_stat_timer: timer for link status reports
101 * @poll_work_obj: work for polling link status
104 struct usb_device *usb_device;
105 struct most_interface iface;
106 struct most_channel_capability *cap;
107 struct most_channel_config *conf;
108 struct most_dci_obj *dci;
110 char description[MAX_STRING_LEN];
111 char suffix[MAX_NUM_ENDPOINTS][MAX_SUFFIX_LEN];
112 spinlock_t channel_lock[MAX_NUM_ENDPOINTS]; /* sync channel access */
113 bool padding_active[MAX_NUM_ENDPOINTS];
114 bool is_channel_healthy[MAX_NUM_ENDPOINTS];
115 struct clear_hold_work clear_work[MAX_NUM_ENDPOINTS];
116 struct usb_anchor *busy_urbs;
117 struct mutex io_mutex;
118 struct timer_list link_stat_timer;
119 struct work_struct poll_work_obj;
120 void (*on_netinfo)(struct most_interface *most_iface,
121 unsigned char link_state, unsigned char *addrs);
124 #define to_mdev(d) container_of(d, struct most_dev, iface)
125 #define to_mdev_from_work(w) container_of(w, struct most_dev, poll_work_obj)
127 static void wq_clear_halt(struct work_struct *wq_obj);
128 static void wq_netinfo(struct work_struct *wq_obj);
131 * drci_rd_reg - read a DCI register
133 * @reg: register address
134 * @buf: buffer to store data
136 * This is reads data from INIC's direct register communication interface
138 static inline int drci_rd_reg(struct usb_device *dev, u16 reg, u16 *buf)
141 __le16 *dma_buf = kzalloc(sizeof(*dma_buf), GFP_KERNEL);
142 u8 req_type = USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
147 retval = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
148 DRCI_READ_REQ, req_type,
150 reg, dma_buf, sizeof(*dma_buf), 5 * HZ);
151 *buf = le16_to_cpu(*dma_buf);
158 * drci_wr_reg - write a DCI register
160 * @reg: register address
161 * @data: data to write
163 * This is writes data to INIC's direct register communication interface
165 static inline int drci_wr_reg(struct usb_device *dev, u16 reg, u16 data)
167 return usb_control_msg(dev,
168 usb_sndctrlpipe(dev, 0),
170 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
178 static inline int start_sync_ep(struct usb_device *usb_dev, u16 ep)
180 return drci_wr_reg(usb_dev, DRCI_REG_BASE + DRCI_COMMAND + ep * 16, 1);
184 * get_stream_frame_size - calculate frame size of current configuration
185 * @cfg: channel configuration
187 static unsigned int get_stream_frame_size(struct most_channel_config *cfg)
189 unsigned int frame_size = 0;
190 unsigned int sub_size = cfg->subbuffer_size;
193 pr_warn("Misconfig: Subbuffer size zero.\n");
196 switch (cfg->data_type) {
198 frame_size = AV_PACKETS_PER_XACT * sub_size;
201 if (cfg->packets_per_xact == 0) {
202 pr_warn("Misconfig: Packets per XACT zero\n");
204 } else if (cfg->packets_per_xact == 0xFF) {
205 frame_size = (USB_MTU / sub_size) * sub_size;
207 frame_size = cfg->packets_per_xact * sub_size;
211 pr_warn("Query frame size of non-streaming channel\n");
218 * hdm_poison_channel - mark buffers of this channel as invalid
219 * @iface: pointer to the interface
220 * @channel: channel ID
222 * This unlinks all URBs submitted to the HCD,
223 * calls the associated completion function of the core and removes
224 * them from the list.
226 * Returns 0 on success or error code otherwise.
228 static int hdm_poison_channel(struct most_interface *iface, int channel)
230 struct most_dev *mdev = to_mdev(iface);
232 spinlock_t *lock; /* temp. lock */
234 if (unlikely(!iface)) {
235 dev_warn(&mdev->usb_device->dev, "Poison: Bad interface.\n");
238 if (unlikely(channel < 0 || channel >= iface->num_channels)) {
239 dev_warn(&mdev->usb_device->dev, "Channel ID out of range.\n");
243 lock = mdev->channel_lock + channel;
244 spin_lock_irqsave(lock, flags);
245 mdev->is_channel_healthy[channel] = false;
246 spin_unlock_irqrestore(lock, flags);
248 cancel_work_sync(&mdev->clear_work[channel].ws);
250 mutex_lock(&mdev->io_mutex);
251 usb_kill_anchored_urbs(&mdev->busy_urbs[channel]);
252 if (mdev->padding_active[channel])
253 mdev->padding_active[channel] = false;
255 if (mdev->conf[channel].data_type == MOST_CH_ASYNC) {
256 del_timer_sync(&mdev->link_stat_timer);
257 cancel_work_sync(&mdev->poll_work_obj);
259 mutex_unlock(&mdev->io_mutex);
264 * hdm_add_padding - add padding bytes
266 * @channel: channel ID
267 * @mbo: buffer object
269 * This inserts the INIC hardware specific padding bytes into a streaming
272 static int hdm_add_padding(struct most_dev *mdev, int channel, struct mbo *mbo)
274 struct most_channel_config *conf = &mdev->conf[channel];
275 unsigned int frame_size = get_stream_frame_size(conf);
276 unsigned int j, num_frames;
280 num_frames = mbo->buffer_length / frame_size;
282 if (num_frames < 1) {
283 dev_err(&mdev->usb_device->dev,
284 "Missed minimal transfer unit.\n");
288 for (j = num_frames - 1; j > 0; j--)
289 memmove(mbo->virt_address + j * USB_MTU,
290 mbo->virt_address + j * frame_size,
292 mbo->buffer_length = num_frames * USB_MTU;
297 * hdm_remove_padding - remove padding bytes
299 * @channel: channel ID
300 * @mbo: buffer object
302 * This takes the INIC hardware specific padding bytes off a streaming
305 static int hdm_remove_padding(struct most_dev *mdev, int channel,
308 struct most_channel_config *const conf = &mdev->conf[channel];
309 unsigned int frame_size = get_stream_frame_size(conf);
310 unsigned int j, num_frames;
314 num_frames = mbo->processed_length / USB_MTU;
316 for (j = 1; j < num_frames; j++)
317 memmove(mbo->virt_address + frame_size * j,
318 mbo->virt_address + USB_MTU * j,
321 mbo->processed_length = frame_size * num_frames;
326 * hdm_write_completion - completion function for submitted Tx URBs
327 * @urb: the URB that has been completed
329 * This checks the status of the completed URB. In case the URB has been
330 * unlinked before, it is immediately freed. On any other error the MBO
331 * transfer flag is set. On success it frees allocated resources and calls
332 * the completion function.
334 * Context: interrupt!
336 static void hdm_write_completion(struct urb *urb)
338 struct mbo *mbo = urb->context;
339 struct most_dev *mdev = to_mdev(mbo->ifp);
340 unsigned int channel = mbo->hdm_channel_id;
341 spinlock_t *lock = mdev->channel_lock + channel;
344 spin_lock_irqsave(lock, flags);
346 mbo->processed_length = 0;
347 mbo->status = MBO_E_INVAL;
348 if (likely(mdev->is_channel_healthy[channel])) {
349 switch (urb->status) {
352 mbo->processed_length = urb->actual_length;
353 mbo->status = MBO_SUCCESS;
356 dev_warn(&mdev->usb_device->dev,
357 "Broken pipe on ep%02x\n",
358 mdev->ep_address[channel]);
359 mdev->is_channel_healthy[channel] = false;
360 mdev->clear_work[channel].pipe = urb->pipe;
361 schedule_work(&mdev->clear_work[channel].ws);
365 mbo->status = MBO_E_CLOSE;
370 spin_unlock_irqrestore(lock, flags);
372 if (likely(mbo->complete))
378 * hdm_read_completion - completion function for submitted Rx URBs
379 * @urb: the URB that has been completed
381 * This checks the status of the completed URB. In case the URB has been
382 * unlinked before it is immediately freed. On any other error the MBO transfer
383 * flag is set. On success it frees allocated resources, removes
384 * padding bytes -if necessary- and calls the completion function.
386 * Context: interrupt!
388 * **************************************************************************
389 * Error codes returned by in urb->status
390 * or in iso_frame_desc[n].status (for ISO)
391 * *************************************************************************
393 * USB device drivers may only test urb status values in completion handlers.
394 * This is because otherwise there would be a race between HCDs updating
395 * these values on one CPU, and device drivers testing them on another CPU.
397 * A transfer's actual_length may be positive even when an error has been
398 * reported. That's because transfers often involve several packets, so that
399 * one or more packets could finish before an error stops further endpoint I/O.
401 * For isochronous URBs, the urb status value is non-zero only if the URB is
402 * unlinked, the device is removed, the host controller is disabled or the total
403 * transferred length is less than the requested length and the URB_SHORT_NOT_OK
404 * flag is set. Completion handlers for isochronous URBs should only see
405 * urb->status set to zero, -ENOENT, -ECONNRESET, -ESHUTDOWN, or -EREMOTEIO.
406 * Individual frame descriptor status fields may report more status codes.
409 * 0 Transfer completed successfully
411 * -ENOENT URB was synchronously unlinked by usb_unlink_urb
413 * -EINPROGRESS URB still pending, no results yet
414 * (That is, if drivers see this it's a bug.)
416 * -EPROTO (*, **) a) bitstuff error
417 * b) no response packet received within the
418 * prescribed bus turn-around time
419 * c) unknown USB error
421 * -EILSEQ (*, **) a) CRC mismatch
422 * b) no response packet received within the
423 * prescribed bus turn-around time
424 * c) unknown USB error
426 * Note that often the controller hardware does not
427 * distinguish among cases a), b), and c), so a
428 * driver cannot tell whether there was a protocol
429 * error, a failure to respond (often caused by
430 * device disconnect), or some other fault.
432 * -ETIME (**) No response packet received within the prescribed
433 * bus turn-around time. This error may instead be
434 * reported as -EPROTO or -EILSEQ.
436 * -ETIMEDOUT Synchronous USB message functions use this code
437 * to indicate timeout expired before the transfer
438 * completed, and no other error was reported by HC.
440 * -EPIPE (**) Endpoint stalled. For non-control endpoints,
441 * reset this status with usb_clear_halt().
443 * -ECOMM During an IN transfer, the host controller
444 * received data from an endpoint faster than it
445 * could be written to system memory
447 * -ENOSR During an OUT transfer, the host controller
448 * could not retrieve data from system memory fast
449 * enough to keep up with the USB data rate
451 * -EOVERFLOW (*) The amount of data returned by the endpoint was
452 * greater than either the max packet size of the
453 * endpoint or the remaining buffer size. "Babble".
455 * -EREMOTEIO The data read from the endpoint did not fill the
456 * specified buffer, and URB_SHORT_NOT_OK was set in
457 * urb->transfer_flags.
459 * -ENODEV Device was removed. Often preceded by a burst of
460 * other errors, since the hub driver doesn't detect
461 * device removal events immediately.
463 * -EXDEV ISO transfer only partially completed
464 * (only set in iso_frame_desc[n].status, not urb->status)
466 * -EINVAL ISO madness, if this happens: Log off and go home
468 * -ECONNRESET URB was asynchronously unlinked by usb_unlink_urb
470 * -ESHUTDOWN The device or host controller has been disabled due
471 * to some problem that could not be worked around,
472 * such as a physical disconnect.
475 * (*) Error codes like -EPROTO, -EILSEQ and -EOVERFLOW normally indicate
476 * hardware problems such as bad devices (including firmware) or cables.
478 * (**) This is also one of several codes that different kinds of host
479 * controller use to indicate a transfer has failed because of device
480 * disconnect. In the interval before the hub driver starts disconnect
481 * processing, devices may receive such fault reports for every request.
483 * See <https://www.kernel.org/doc/Documentation/driver-api/usb/error-codes.rst>
485 static void hdm_read_completion(struct urb *urb)
487 struct mbo *mbo = urb->context;
488 struct most_dev *mdev = to_mdev(mbo->ifp);
489 unsigned int channel = mbo->hdm_channel_id;
490 struct device *dev = &mdev->usb_device->dev;
491 spinlock_t *lock = mdev->channel_lock + channel;
494 spin_lock_irqsave(lock, flags);
496 mbo->processed_length = 0;
497 mbo->status = MBO_E_INVAL;
498 if (likely(mdev->is_channel_healthy[channel])) {
499 switch (urb->status) {
502 mbo->processed_length = urb->actual_length;
503 mbo->status = MBO_SUCCESS;
504 if (mdev->padding_active[channel] &&
505 hdm_remove_padding(mdev, channel, mbo)) {
506 mbo->processed_length = 0;
507 mbo->status = MBO_E_INVAL;
511 dev_warn(dev, "Broken pipe on ep%02x\n",
512 mdev->ep_address[channel]);
513 mdev->is_channel_healthy[channel] = false;
514 mdev->clear_work[channel].pipe = urb->pipe;
515 schedule_work(&mdev->clear_work[channel].ws);
519 mbo->status = MBO_E_CLOSE;
522 dev_warn(dev, "Babble on ep%02x\n",
523 mdev->ep_address[channel]);
528 spin_unlock_irqrestore(lock, flags);
530 if (likely(mbo->complete))
536 * hdm_enqueue - receive a buffer to be used for data transfer
537 * @iface: interface to enqueue to
538 * @channel: ID of the channel
539 * @mbo: pointer to the buffer object
541 * This allocates a new URB and fills it according to the channel
542 * that is being used for transmission of data. Before the URB is
543 * submitted it is stored in the private anchor list.
545 * Returns 0 on success. On any error the URB is freed and a error code
548 * Context: Could in _some_ cases be interrupt!
550 static int hdm_enqueue(struct most_interface *iface, int channel,
553 struct most_dev *mdev;
554 struct most_channel_config *conf;
557 unsigned long length;
560 if (unlikely(!iface || !mbo))
562 if (unlikely(iface->num_channels <= channel || channel < 0))
565 mdev = to_mdev(iface);
566 conf = &mdev->conf[channel];
568 mutex_lock(&mdev->io_mutex);
569 if (!mdev->usb_device) {
571 goto unlock_io_mutex;
574 urb = usb_alloc_urb(NO_ISOCHRONOUS_URB, GFP_ATOMIC);
577 goto unlock_io_mutex;
580 if ((conf->direction & MOST_CH_TX) && mdev->padding_active[channel] &&
581 hdm_add_padding(mdev, channel, mbo)) {
586 urb->transfer_dma = mbo->bus_address;
587 virt_address = mbo->virt_address;
588 length = mbo->buffer_length;
590 if (conf->direction & MOST_CH_TX) {
591 usb_fill_bulk_urb(urb, mdev->usb_device,
592 usb_sndbulkpipe(mdev->usb_device,
593 mdev->ep_address[channel]),
596 hdm_write_completion,
598 if (conf->data_type != MOST_CH_ISOC &&
599 conf->data_type != MOST_CH_SYNC)
600 urb->transfer_flags |= URB_ZERO_PACKET;
602 usb_fill_bulk_urb(urb, mdev->usb_device,
603 usb_rcvbulkpipe(mdev->usb_device,
604 mdev->ep_address[channel]),
606 length + conf->extra_len,
610 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
612 usb_anchor_urb(urb, &mdev->busy_urbs[channel]);
614 retval = usb_submit_urb(urb, GFP_KERNEL);
616 dev_err(&mdev->usb_device->dev,
617 "URB submit failed with error %d.\n", retval);
618 goto err_unanchor_urb;
620 goto unlock_io_mutex;
623 usb_unanchor_urb(urb);
627 mutex_unlock(&mdev->io_mutex);
631 static void *hdm_dma_alloc(struct mbo *mbo, u32 size)
633 struct most_dev *mdev = to_mdev(mbo->ifp);
635 return usb_alloc_coherent(mdev->usb_device, size, GFP_KERNEL,
639 static void hdm_dma_free(struct mbo *mbo, u32 size)
641 struct most_dev *mdev = to_mdev(mbo->ifp);
643 usb_free_coherent(mdev->usb_device, size, mbo->virt_address,
648 * hdm_configure_channel - receive channel configuration from core
650 * @channel: channel ID
651 * @conf: structure that holds the configuration information
653 * The attached network interface controller (NIC) supports a padding mode
654 * to avoid short packets on USB, hence increasing the performance due to a
655 * lower interrupt load. This mode is default for synchronous data and can
656 * be switched on for isochronous data. In case padding is active the
657 * driver needs to know the frame size of the payload in order to calculate
658 * the number of bytes it needs to pad when transmitting or to cut off when
662 static int hdm_configure_channel(struct most_interface *iface, int channel,
663 struct most_channel_config *conf)
665 unsigned int num_frames;
666 unsigned int frame_size;
667 struct most_dev *mdev = to_mdev(iface);
668 struct device *dev = &mdev->usb_device->dev;
670 mdev->is_channel_healthy[channel] = true;
671 mdev->clear_work[channel].channel = channel;
672 mdev->clear_work[channel].mdev = mdev;
673 INIT_WORK(&mdev->clear_work[channel].ws, wq_clear_halt);
675 if (unlikely(!iface || !conf)) {
676 dev_err(dev, "Bad interface or config pointer.\n");
679 if (unlikely(channel < 0 || channel >= iface->num_channels)) {
680 dev_err(dev, "Channel ID out of range.\n");
683 if (!conf->num_buffers || !conf->buffer_size) {
684 dev_err(dev, "Misconfig: buffer size or #buffers zero.\n");
688 if (conf->data_type != MOST_CH_SYNC &&
689 !(conf->data_type == MOST_CH_ISOC &&
690 conf->packets_per_xact != 0xFF)) {
691 mdev->padding_active[channel] = false;
693 * Since the NIC's padding mode is not going to be
694 * used, we can skip the frame size calculations and
695 * move directly on to exit.
700 mdev->padding_active[channel] = true;
702 frame_size = get_stream_frame_size(conf);
703 if (frame_size == 0 || frame_size > USB_MTU) {
704 dev_warn(dev, "Misconfig: frame size wrong\n");
708 num_frames = conf->buffer_size / frame_size;
710 if (conf->buffer_size % frame_size) {
711 u16 old_size = conf->buffer_size;
713 conf->buffer_size = num_frames * frame_size;
714 dev_warn(dev, "%s: fixed buffer size (%d -> %d)\n",
715 mdev->suffix[channel], old_size, conf->buffer_size);
718 /* calculate extra length to comply w/ HW padding */
719 conf->extra_len = num_frames * (USB_MTU - frame_size);
722 mdev->conf[channel] = *conf;
723 if (conf->data_type == MOST_CH_ASYNC) {
724 u16 ep = mdev->ep_address[channel];
726 if (start_sync_ep(mdev->usb_device, ep) < 0)
727 dev_warn(dev, "sync for ep%02x failed", ep);
733 * hdm_request_netinfo - request network information
734 * @iface: pointer to interface
735 * @channel: channel ID
737 * This is used as trigger to set up the link status timer that
738 * polls for the NI state of the INIC every 2 seconds.
741 static void hdm_request_netinfo(struct most_interface *iface, int channel,
742 void (*on_netinfo)(struct most_interface *,
746 struct most_dev *mdev;
749 mdev = to_mdev(iface);
750 mdev->on_netinfo = on_netinfo;
754 mdev->link_stat_timer.expires = jiffies + HZ;
755 mod_timer(&mdev->link_stat_timer, mdev->link_stat_timer.expires);
759 * link_stat_timer_handler - schedule work obtaining mac address and link status
760 * @data: pointer to USB device instance
762 * The handler runs in interrupt context. That's why we need to defer the
763 * tasks to a work queue.
765 static void link_stat_timer_handler(struct timer_list *t)
767 struct most_dev *mdev = from_timer(mdev, t, link_stat_timer);
769 schedule_work(&mdev->poll_work_obj);
770 mdev->link_stat_timer.expires = jiffies + (2 * HZ);
771 add_timer(&mdev->link_stat_timer);
775 * wq_netinfo - work queue function to deliver latest networking information
776 * @wq_obj: object that holds data for our deferred work to do
778 * This retrieves the network interface status of the USB INIC
780 static void wq_netinfo(struct work_struct *wq_obj)
782 struct most_dev *mdev = to_mdev_from_work(wq_obj);
783 struct usb_device *usb_device = mdev->usb_device;
784 struct device *dev = &usb_device->dev;
785 u16 hi, mi, lo, link;
788 if (drci_rd_reg(usb_device, DRCI_REG_HW_ADDR_HI, &hi) < 0) {
789 dev_err(dev, "Vendor request 'hw_addr_hi' failed\n");
793 if (drci_rd_reg(usb_device, DRCI_REG_HW_ADDR_MI, &mi) < 0) {
794 dev_err(dev, "Vendor request 'hw_addr_mid' failed\n");
798 if (drci_rd_reg(usb_device, DRCI_REG_HW_ADDR_LO, &lo) < 0) {
799 dev_err(dev, "Vendor request 'hw_addr_low' failed\n");
803 if (drci_rd_reg(usb_device, DRCI_REG_NI_STATE, &link) < 0) {
804 dev_err(dev, "Vendor request 'link status' failed\n");
808 hw_addr[0] = hi >> 8;
810 hw_addr[2] = mi >> 8;
812 hw_addr[4] = lo >> 8;
815 if (mdev->on_netinfo)
816 mdev->on_netinfo(&mdev->iface, link, hw_addr);
820 * wq_clear_halt - work queue function
821 * @wq_obj: work_struct object to execute
823 * This sends a clear_halt to the given USB pipe.
825 static void wq_clear_halt(struct work_struct *wq_obj)
827 struct clear_hold_work *clear_work = to_clear_hold_work(wq_obj);
828 struct most_dev *mdev = clear_work->mdev;
829 unsigned int channel = clear_work->channel;
830 int pipe = clear_work->pipe;
832 mutex_lock(&mdev->io_mutex);
833 most_stop_enqueue(&mdev->iface, channel);
834 usb_kill_anchored_urbs(&mdev->busy_urbs[channel]);
835 if (usb_clear_halt(mdev->usb_device, pipe))
836 dev_warn(&mdev->usb_device->dev, "Failed to reset endpoint.\n");
838 /* If the functional Stall condition has been set on an
839 * asynchronous rx channel, we need to clear the tx channel
840 * too, since the hardware runs its clean-up sequence on both
841 * channels, as they are physically one on the network.
843 * The USB interface that exposes the asynchronous channels
844 * contains always two endpoints, and two only.
846 if (mdev->conf[channel].data_type == MOST_CH_ASYNC &&
847 mdev->conf[channel].direction == MOST_CH_RX) {
848 int peer = 1 - channel;
849 int snd_pipe = usb_sndbulkpipe(mdev->usb_device,
850 mdev->ep_address[peer]);
851 usb_clear_halt(mdev->usb_device, snd_pipe);
853 mdev->is_channel_healthy[channel] = true;
854 most_resume_enqueue(&mdev->iface, channel);
855 mutex_unlock(&mdev->io_mutex);
859 * hdm_usb_fops - file operation table for USB driver
861 static const struct file_operations hdm_usb_fops = {
862 .owner = THIS_MODULE,
866 * usb_device_id - ID table for HCD device probing
868 static const struct usb_device_id usbid[] = {
869 { USB_DEVICE(USB_VENDOR_ID_SMSC, USB_DEV_ID_BRDG), },
870 { USB_DEVICE(USB_VENDOR_ID_SMSC, USB_DEV_ID_OS81118), },
871 { USB_DEVICE(USB_VENDOR_ID_SMSC, USB_DEV_ID_OS81119), },
872 { USB_DEVICE(USB_VENDOR_ID_SMSC, USB_DEV_ID_OS81210), },
873 { } /* Terminating entry */
881 static const struct regs ro_regs[] = {
882 { "ni_state", DRCI_REG_NI_STATE },
883 { "packet_bandwidth", DRCI_REG_PACKET_BW },
884 { "node_address", DRCI_REG_NODE_ADDR },
885 { "node_position", DRCI_REG_NODE_POS },
888 static const struct regs rw_regs[] = {
889 { "mep_filter", DRCI_REG_MEP_FILTER },
890 { "mep_hash0", DRCI_REG_HASH_TBL0 },
891 { "mep_hash1", DRCI_REG_HASH_TBL1 },
892 { "mep_hash2", DRCI_REG_HASH_TBL2 },
893 { "mep_hash3", DRCI_REG_HASH_TBL3 },
894 { "mep_eui48_hi", DRCI_REG_HW_ADDR_HI },
895 { "mep_eui48_mi", DRCI_REG_HW_ADDR_MI },
896 { "mep_eui48_lo", DRCI_REG_HW_ADDR_LO },
899 static int get_stat_reg_addr(const struct regs *regs, int size,
900 const char *name, u16 *reg_addr)
904 for (i = 0; i < size; i++) {
905 if (!strcmp(name, regs[i].name)) {
906 *reg_addr = regs[i].reg;
913 #define get_static_reg_addr(regs, name, reg_addr) \
914 get_stat_reg_addr(regs, ARRAY_SIZE(regs), name, reg_addr)
916 static ssize_t value_show(struct device *dev, struct device_attribute *attr,
919 const char *name = attr->attr.name;
920 struct most_dci_obj *dci_obj = to_dci_obj(dev);
925 if (!strcmp(name, "arb_address"))
926 return snprintf(buf, PAGE_SIZE, "%04x\n", dci_obj->reg_addr);
928 if (!strcmp(name, "arb_value"))
929 reg_addr = dci_obj->reg_addr;
930 else if (get_static_reg_addr(ro_regs, name, ®_addr) &&
931 get_static_reg_addr(rw_regs, name, ®_addr))
934 err = drci_rd_reg(dci_obj->usb_device, reg_addr, &val);
938 return snprintf(buf, PAGE_SIZE, "%04x\n", val);
941 static ssize_t value_store(struct device *dev, struct device_attribute *attr,
942 const char *buf, size_t count)
946 const char *name = attr->attr.name;
947 struct most_dci_obj *dci_obj = to_dci_obj(dev);
948 struct usb_device *usb_dev = dci_obj->usb_device;
949 int err = kstrtou16(buf, 16, &val);
954 if (!strcmp(name, "arb_address")) {
955 dci_obj->reg_addr = val;
959 if (!strcmp(name, "arb_value"))
960 err = drci_wr_reg(usb_dev, dci_obj->reg_addr, val);
961 else if (!strcmp(name, "sync_ep"))
962 err = start_sync_ep(usb_dev, val);
963 else if (!get_static_reg_addr(rw_regs, name, ®_addr))
964 err = drci_wr_reg(usb_dev, reg_addr, val);
974 static DEVICE_ATTR(ni_state, 0444, value_show, NULL);
975 static DEVICE_ATTR(packet_bandwidth, 0444, value_show, NULL);
976 static DEVICE_ATTR(node_address, 0444, value_show, NULL);
977 static DEVICE_ATTR(node_position, 0444, value_show, NULL);
978 static DEVICE_ATTR(sync_ep, 0200, NULL, value_store);
979 static DEVICE_ATTR(mep_filter, 0644, value_show, value_store);
980 static DEVICE_ATTR(mep_hash0, 0644, value_show, value_store);
981 static DEVICE_ATTR(mep_hash1, 0644, value_show, value_store);
982 static DEVICE_ATTR(mep_hash2, 0644, value_show, value_store);
983 static DEVICE_ATTR(mep_hash3, 0644, value_show, value_store);
984 static DEVICE_ATTR(mep_eui48_hi, 0644, value_show, value_store);
985 static DEVICE_ATTR(mep_eui48_mi, 0644, value_show, value_store);
986 static DEVICE_ATTR(mep_eui48_lo, 0644, value_show, value_store);
987 static DEVICE_ATTR(arb_address, 0644, value_show, value_store);
988 static DEVICE_ATTR(arb_value, 0644, value_show, value_store);
990 static struct attribute *dci_attrs[] = {
991 &dev_attr_ni_state.attr,
992 &dev_attr_packet_bandwidth.attr,
993 &dev_attr_node_address.attr,
994 &dev_attr_node_position.attr,
995 &dev_attr_sync_ep.attr,
996 &dev_attr_mep_filter.attr,
997 &dev_attr_mep_hash0.attr,
998 &dev_attr_mep_hash1.attr,
999 &dev_attr_mep_hash2.attr,
1000 &dev_attr_mep_hash3.attr,
1001 &dev_attr_mep_eui48_hi.attr,
1002 &dev_attr_mep_eui48_mi.attr,
1003 &dev_attr_mep_eui48_lo.attr,
1004 &dev_attr_arb_address.attr,
1005 &dev_attr_arb_value.attr,
1009 static struct attribute_group dci_attr_group = {
1013 static const struct attribute_group *dci_attr_groups[] = {
1018 static void release_dci(struct device *dev)
1020 struct most_dci_obj *dci = to_dci_obj(dev);
1026 * hdm_probe - probe function of USB device driver
1027 * @interface: Interface of the attached USB device
1028 * @id: Pointer to the USB ID table.
1030 * This allocates and initializes the device instance, adds the new
1031 * entry to the internal list, scans the USB descriptors and registers
1032 * the interface with the core.
1033 * Additionally, the DCI objects are created and the hardware is sync'd.
1035 * Return 0 on success. In case of an error a negative number is returned.
1038 hdm_probe(struct usb_interface *interface, const struct usb_device_id *id)
1040 struct usb_host_interface *usb_iface_desc = interface->cur_altsetting;
1041 struct usb_device *usb_dev = interface_to_usbdev(interface);
1042 struct device *dev = &usb_dev->dev;
1043 struct most_dev *mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
1045 unsigned int num_endpoints;
1046 struct most_channel_capability *tmp_cap;
1047 struct usb_endpoint_descriptor *ep_desc;
1051 goto err_out_of_memory;
1053 usb_set_intfdata(interface, mdev);
1054 num_endpoints = usb_iface_desc->desc.bNumEndpoints;
1055 mutex_init(&mdev->io_mutex);
1056 INIT_WORK(&mdev->poll_work_obj, wq_netinfo);
1057 timer_setup(&mdev->link_stat_timer, link_stat_timer_handler, 0);
1059 mdev->usb_device = usb_dev;
1060 mdev->link_stat_timer.expires = jiffies + (2 * HZ);
1062 mdev->iface.mod = hdm_usb_fops.owner;
1063 mdev->iface.driver_dev = &interface->dev;
1064 mdev->iface.interface = ITYPE_USB;
1065 mdev->iface.configure = hdm_configure_channel;
1066 mdev->iface.request_netinfo = hdm_request_netinfo;
1067 mdev->iface.enqueue = hdm_enqueue;
1068 mdev->iface.poison_channel = hdm_poison_channel;
1069 mdev->iface.dma_alloc = hdm_dma_alloc;
1070 mdev->iface.dma_free = hdm_dma_free;
1071 mdev->iface.description = mdev->description;
1072 mdev->iface.num_channels = num_endpoints;
1074 snprintf(mdev->description, sizeof(mdev->description),
1076 usb_dev->bus->busnum,
1078 usb_dev->config->desc.bConfigurationValue,
1079 usb_iface_desc->desc.bInterfaceNumber);
1081 mdev->conf = kcalloc(num_endpoints, sizeof(*mdev->conf), GFP_KERNEL);
1085 mdev->cap = kcalloc(num_endpoints, sizeof(*mdev->cap), GFP_KERNEL);
1089 mdev->iface.channel_vector = mdev->cap;
1091 kcalloc(num_endpoints, sizeof(*mdev->ep_address), GFP_KERNEL);
1092 if (!mdev->ep_address)
1096 kcalloc(num_endpoints, sizeof(*mdev->busy_urbs), GFP_KERNEL);
1097 if (!mdev->busy_urbs)
1098 goto err_free_ep_address;
1100 tmp_cap = mdev->cap;
1101 for (i = 0; i < num_endpoints; i++) {
1102 ep_desc = &usb_iface_desc->endpoint[i].desc;
1103 mdev->ep_address[i] = ep_desc->bEndpointAddress;
1104 mdev->padding_active[i] = false;
1105 mdev->is_channel_healthy[i] = true;
1107 snprintf(&mdev->suffix[i][0], MAX_SUFFIX_LEN, "ep%02x",
1108 mdev->ep_address[i]);
1110 tmp_cap->name_suffix = &mdev->suffix[i][0];
1111 tmp_cap->buffer_size_packet = MAX_BUF_SIZE;
1112 tmp_cap->buffer_size_streaming = MAX_BUF_SIZE;
1113 tmp_cap->num_buffers_packet = BUF_CHAIN_SIZE;
1114 tmp_cap->num_buffers_streaming = BUF_CHAIN_SIZE;
1115 tmp_cap->data_type = MOST_CH_CONTROL | MOST_CH_ASYNC |
1116 MOST_CH_ISOC | MOST_CH_SYNC;
1117 if (usb_endpoint_dir_in(ep_desc))
1118 tmp_cap->direction = MOST_CH_RX;
1120 tmp_cap->direction = MOST_CH_TX;
1122 init_usb_anchor(&mdev->busy_urbs[i]);
1123 spin_lock_init(&mdev->channel_lock[i]);
1125 dev_notice(dev, "claimed gadget: Vendor=%4.4x ProdID=%4.4x Bus=%02x Device=%02x\n",
1126 le16_to_cpu(usb_dev->descriptor.idVendor),
1127 le16_to_cpu(usb_dev->descriptor.idProduct),
1128 usb_dev->bus->busnum,
1131 dev_notice(dev, "device path: /sys/bus/usb/devices/%d-%s:%d.%d\n",
1132 usb_dev->bus->busnum,
1134 usb_dev->config->desc.bConfigurationValue,
1135 usb_iface_desc->desc.bInterfaceNumber);
1137 ret = most_register_interface(&mdev->iface);
1139 goto err_free_busy_urbs;
1141 mutex_lock(&mdev->io_mutex);
1142 if (le16_to_cpu(usb_dev->descriptor.idProduct) == USB_DEV_ID_OS81118 ||
1143 le16_to_cpu(usb_dev->descriptor.idProduct) == USB_DEV_ID_OS81119 ||
1144 le16_to_cpu(usb_dev->descriptor.idProduct) == USB_DEV_ID_OS81210) {
1145 mdev->dci = kzalloc(sizeof(*mdev->dci), GFP_KERNEL);
1147 mutex_unlock(&mdev->io_mutex);
1148 most_deregister_interface(&mdev->iface);
1150 goto err_free_busy_urbs;
1153 mdev->dci->dev.init_name = "dci";
1154 mdev->dci->dev.parent = &mdev->iface.dev;
1155 mdev->dci->dev.groups = dci_attr_groups;
1156 mdev->dci->dev.release = release_dci;
1157 if (device_register(&mdev->dci->dev)) {
1158 mutex_unlock(&mdev->io_mutex);
1159 most_deregister_interface(&mdev->iface);
1163 mdev->dci->usb_device = mdev->usb_device;
1165 mutex_unlock(&mdev->io_mutex);
1170 kfree(mdev->busy_urbs);
1171 err_free_ep_address:
1172 kfree(mdev->ep_address);
1180 if (ret == 0 || ret == -ENOMEM) {
1182 dev_err(dev, "out of memory\n");
1188 * hdm_disconnect - disconnect function of USB device driver
1189 * @interface: Interface of the attached USB device
1191 * This deregisters the interface with the core, removes the kernel timer
1192 * and frees resources.
1194 * Context: hub kernel thread
1196 static void hdm_disconnect(struct usb_interface *interface)
1198 struct most_dev *mdev = usb_get_intfdata(interface);
1200 mutex_lock(&mdev->io_mutex);
1201 usb_set_intfdata(interface, NULL);
1202 mdev->usb_device = NULL;
1203 mutex_unlock(&mdev->io_mutex);
1205 del_timer_sync(&mdev->link_stat_timer);
1206 cancel_work_sync(&mdev->poll_work_obj);
1208 device_unregister(&mdev->dci->dev);
1209 most_deregister_interface(&mdev->iface);
1211 kfree(mdev->busy_urbs);
1214 kfree(mdev->ep_address);
1218 static struct usb_driver hdm_usb = {
1222 .disconnect = hdm_disconnect,
1225 module_usb_driver(hdm_usb);
1226 MODULE_LICENSE("GPL");
1227 MODULE_AUTHOR("Christian Gromm <christian.gromm@microchip.com>");
1228 MODULE_DESCRIPTION("HDM_4_USB");