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>
30 #define NO_ISOCHRONOUS_URB 0
31 #define AV_PACKETS_PER_XACT 2
32 #define BUF_CHAIN_SIZE 0xFFFF
33 #define MAX_NUM_ENDPOINTS 30
34 #define MAX_SUFFIX_LEN 10
35 #define MAX_STRING_LEN 80
36 #define MAX_BUF_SIZE 0xFFFF
38 #define USB_VENDOR_ID_SMSC 0x0424 /* VID: SMSC */
39 #define USB_DEV_ID_BRDG 0xC001 /* PID: USB Bridge */
40 #define USB_DEV_ID_OS81118 0xCF18 /* PID: USB OS81118 */
41 #define USB_DEV_ID_OS81119 0xCF19 /* PID: USB OS81119 */
42 #define USB_DEV_ID_OS81210 0xCF30 /* PID: USB OS81210 */
44 #define DRCI_REG_NI_STATE 0x0100
45 #define DRCI_REG_PACKET_BW 0x0101
46 #define DRCI_REG_NODE_ADDR 0x0102
47 #define DRCI_REG_NODE_POS 0x0103
48 #define DRCI_REG_MEP_FILTER 0x0140
49 #define DRCI_REG_HASH_TBL0 0x0141
50 #define DRCI_REG_HASH_TBL1 0x0142
51 #define DRCI_REG_HASH_TBL2 0x0143
52 #define DRCI_REG_HASH_TBL3 0x0144
53 #define DRCI_REG_HW_ADDR_HI 0x0145
54 #define DRCI_REG_HW_ADDR_MI 0x0146
55 #define DRCI_REG_HW_ADDR_LO 0x0147
56 #define DRCI_REG_BASE 0x1100
57 #define DRCI_COMMAND 0x02
58 #define DRCI_READ_REQ 0xA0
59 #define DRCI_WRITE_REQ 0xA1
62 * struct most_dci_obj - Direct Communication Interface
63 * @kobj:position in sysfs
64 * @usb_device: pointer to the usb device
65 * @reg_addr: register address for arbitrary DCI access
69 struct usb_device *usb_device;
73 #define to_dci_obj(p) container_of(p, struct most_dci_obj, dev)
77 struct clear_hold_work {
78 struct work_struct ws;
79 struct most_dev *mdev;
84 #define to_clear_hold_work(w) container_of(w, struct clear_hold_work, ws)
87 * struct most_dev - holds all usb interface specific stuff
88 * @usb_device: pointer to usb device
89 * @iface: hardware interface
90 * @cap: channel capabilities
91 * @conf: channel configuration
92 * @dci: direct communication interface of hardware
93 * @ep_address: endpoint address table
94 * @description: device description
95 * @suffix: suffix for channel name
96 * @channel_lock: synchronize channel access
97 * @padding_active: indicates channel uses padding
98 * @is_channel_healthy: health status table of each channel
99 * @busy_urbs: list of anchored items
100 * @io_mutex: synchronize I/O with disconnect
101 * @link_stat_timer: timer for link status reports
102 * @poll_work_obj: work for polling link status
106 struct usb_device *usb_device;
107 struct most_interface iface;
108 struct most_channel_capability *cap;
109 struct most_channel_config *conf;
110 struct most_dci_obj *dci;
112 char description[MAX_STRING_LEN];
113 char suffix[MAX_NUM_ENDPOINTS][MAX_SUFFIX_LEN];
114 spinlock_t channel_lock[MAX_NUM_ENDPOINTS]; /* sync channel access */
115 bool padding_active[MAX_NUM_ENDPOINTS];
116 bool is_channel_healthy[MAX_NUM_ENDPOINTS];
117 struct clear_hold_work clear_work[MAX_NUM_ENDPOINTS];
118 struct usb_anchor *busy_urbs;
119 struct mutex io_mutex;
120 struct timer_list link_stat_timer;
121 struct work_struct poll_work_obj;
122 void (*on_netinfo)(struct most_interface *most_iface,
123 unsigned char link_state, unsigned char *addrs);
126 #define to_mdev(d) container_of(d, struct most_dev, iface)
127 #define to_mdev_from_dev(d) container_of(d, struct most_dev, dev)
128 #define to_mdev_from_work(w) container_of(w, struct most_dev, poll_work_obj)
130 static void wq_clear_halt(struct work_struct *wq_obj);
131 static void wq_netinfo(struct work_struct *wq_obj);
134 * drci_rd_reg - read a DCI register
136 * @reg: register address
137 * @buf: buffer to store data
139 * This is reads data from INIC's direct register communication interface
141 static inline int drci_rd_reg(struct usb_device *dev, u16 reg, u16 *buf)
144 __le16 *dma_buf = kzalloc(sizeof(*dma_buf), GFP_KERNEL);
145 u8 req_type = USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
150 retval = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
151 DRCI_READ_REQ, req_type,
153 reg, dma_buf, sizeof(*dma_buf), 5 * HZ);
154 *buf = le16_to_cpu(*dma_buf);
161 * drci_wr_reg - write a DCI register
163 * @reg: register address
164 * @data: data to write
166 * This is writes data to INIC's direct register communication interface
168 static inline int drci_wr_reg(struct usb_device *dev, u16 reg, u16 data)
170 return usb_control_msg(dev,
171 usb_sndctrlpipe(dev, 0),
173 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
181 static inline int start_sync_ep(struct usb_device *usb_dev, u16 ep)
183 return drci_wr_reg(usb_dev, DRCI_REG_BASE + DRCI_COMMAND + ep * 16, 1);
187 * get_stream_frame_size - calculate frame size of current configuration
188 * @cfg: channel configuration
190 static unsigned int get_stream_frame_size(struct most_channel_config *cfg)
192 unsigned int frame_size = 0;
193 unsigned int sub_size = cfg->subbuffer_size;
196 pr_warn("Misconfig: Subbuffer size zero.\n");
199 switch (cfg->data_type) {
201 frame_size = AV_PACKETS_PER_XACT * sub_size;
204 if (cfg->packets_per_xact == 0) {
205 pr_warn("Misconfig: Packets per XACT zero\n");
207 } else if (cfg->packets_per_xact == 0xFF) {
208 frame_size = (USB_MTU / sub_size) * sub_size;
210 frame_size = cfg->packets_per_xact * sub_size;
214 pr_warn("Query frame size of non-streaming channel\n");
221 * hdm_poison_channel - mark buffers of this channel as invalid
222 * @iface: pointer to the interface
223 * @channel: channel ID
225 * This unlinks all URBs submitted to the HCD,
226 * calls the associated completion function of the core and removes
227 * them from the list.
229 * Returns 0 on success or error code otherwise.
231 static int hdm_poison_channel(struct most_interface *iface, int channel)
233 struct most_dev *mdev = to_mdev(iface);
235 spinlock_t *lock; /* temp. lock */
237 if (unlikely(!iface)) {
238 dev_warn(&mdev->usb_device->dev, "Poison: Bad interface.\n");
241 if (unlikely(channel < 0 || channel >= iface->num_channels)) {
242 dev_warn(&mdev->usb_device->dev, "Channel ID out of range.\n");
246 lock = mdev->channel_lock + channel;
247 spin_lock_irqsave(lock, flags);
248 mdev->is_channel_healthy[channel] = false;
249 spin_unlock_irqrestore(lock, flags);
251 cancel_work_sync(&mdev->clear_work[channel].ws);
253 mutex_lock(&mdev->io_mutex);
254 usb_kill_anchored_urbs(&mdev->busy_urbs[channel]);
255 if (mdev->padding_active[channel])
256 mdev->padding_active[channel] = false;
258 if (mdev->conf[channel].data_type == MOST_CH_ASYNC) {
259 del_timer_sync(&mdev->link_stat_timer);
260 cancel_work_sync(&mdev->poll_work_obj);
262 mutex_unlock(&mdev->io_mutex);
267 * hdm_add_padding - add padding bytes
269 * @channel: channel ID
270 * @mbo: buffer object
272 * This inserts the INIC hardware specific padding bytes into a streaming
275 static int hdm_add_padding(struct most_dev *mdev, int channel, struct mbo *mbo)
277 struct most_channel_config *conf = &mdev->conf[channel];
278 unsigned int frame_size = get_stream_frame_size(conf);
279 unsigned int j, num_frames;
283 num_frames = mbo->buffer_length / frame_size;
285 if (num_frames < 1) {
286 dev_err(&mdev->usb_device->dev,
287 "Missed minimal transfer unit.\n");
291 for (j = num_frames - 1; j > 0; j--)
292 memmove(mbo->virt_address + j * USB_MTU,
293 mbo->virt_address + j * frame_size,
295 mbo->buffer_length = num_frames * USB_MTU;
300 * hdm_remove_padding - remove padding bytes
302 * @channel: channel ID
303 * @mbo: buffer object
305 * This takes the INIC hardware specific padding bytes off a streaming
308 static int hdm_remove_padding(struct most_dev *mdev, int channel,
311 struct most_channel_config *const conf = &mdev->conf[channel];
312 unsigned int frame_size = get_stream_frame_size(conf);
313 unsigned int j, num_frames;
317 num_frames = mbo->processed_length / USB_MTU;
319 for (j = 1; j < num_frames; j++)
320 memmove(mbo->virt_address + frame_size * j,
321 mbo->virt_address + USB_MTU * j,
324 mbo->processed_length = frame_size * num_frames;
329 * hdm_write_completion - completion function for submitted Tx URBs
330 * @urb: the URB that has been completed
332 * This checks the status of the completed URB. In case the URB has been
333 * unlinked before, it is immediately freed. On any other error the MBO
334 * transfer flag is set. On success it frees allocated resources and calls
335 * the completion function.
337 * Context: interrupt!
339 static void hdm_write_completion(struct urb *urb)
341 struct mbo *mbo = urb->context;
342 struct most_dev *mdev = to_mdev(mbo->ifp);
343 unsigned int channel = mbo->hdm_channel_id;
344 spinlock_t *lock = mdev->channel_lock + channel;
347 spin_lock_irqsave(lock, flags);
349 mbo->processed_length = 0;
350 mbo->status = MBO_E_INVAL;
351 if (likely(mdev->is_channel_healthy[channel])) {
352 switch (urb->status) {
355 mbo->processed_length = urb->actual_length;
356 mbo->status = MBO_SUCCESS;
359 dev_warn(&mdev->usb_device->dev,
360 "Broken pipe on ep%02x\n",
361 mdev->ep_address[channel]);
362 mdev->is_channel_healthy[channel] = false;
363 mdev->clear_work[channel].pipe = urb->pipe;
364 schedule_work(&mdev->clear_work[channel].ws);
368 mbo->status = MBO_E_CLOSE;
373 spin_unlock_irqrestore(lock, flags);
375 if (likely(mbo->complete))
381 * hdm_read_completion - completion function for submitted Rx URBs
382 * @urb: the URB that has been completed
384 * This checks the status of the completed URB. In case the URB has been
385 * unlinked before it is immediately freed. On any other error the MBO transfer
386 * flag is set. On success it frees allocated resources, removes
387 * padding bytes -if necessary- and calls the completion function.
389 * Context: interrupt!
391 * **************************************************************************
392 * Error codes returned by in urb->status
393 * or in iso_frame_desc[n].status (for ISO)
394 * *************************************************************************
396 * USB device drivers may only test urb status values in completion handlers.
397 * This is because otherwise there would be a race between HCDs updating
398 * these values on one CPU, and device drivers testing them on another CPU.
400 * A transfer's actual_length may be positive even when an error has been
401 * reported. That's because transfers often involve several packets, so that
402 * one or more packets could finish before an error stops further endpoint I/O.
404 * For isochronous URBs, the urb status value is non-zero only if the URB is
405 * unlinked, the device is removed, the host controller is disabled or the total
406 * transferred length is less than the requested length and the URB_SHORT_NOT_OK
407 * flag is set. Completion handlers for isochronous URBs should only see
408 * urb->status set to zero, -ENOENT, -ECONNRESET, -ESHUTDOWN, or -EREMOTEIO.
409 * Individual frame descriptor status fields may report more status codes.
412 * 0 Transfer completed successfully
414 * -ENOENT URB was synchronously unlinked by usb_unlink_urb
416 * -EINPROGRESS URB still pending, no results yet
417 * (That is, if drivers see this it's a bug.)
419 * -EPROTO (*, **) a) bitstuff error
420 * b) no response packet received within the
421 * prescribed bus turn-around time
422 * c) unknown USB error
424 * -EILSEQ (*, **) a) CRC mismatch
425 * b) no response packet received within the
426 * prescribed bus turn-around time
427 * c) unknown USB error
429 * Note that often the controller hardware does not
430 * distinguish among cases a), b), and c), so a
431 * driver cannot tell whether there was a protocol
432 * error, a failure to respond (often caused by
433 * device disconnect), or some other fault.
435 * -ETIME (**) No response packet received within the prescribed
436 * bus turn-around time. This error may instead be
437 * reported as -EPROTO or -EILSEQ.
439 * -ETIMEDOUT Synchronous USB message functions use this code
440 * to indicate timeout expired before the transfer
441 * completed, and no other error was reported by HC.
443 * -EPIPE (**) Endpoint stalled. For non-control endpoints,
444 * reset this status with usb_clear_halt().
446 * -ECOMM During an IN transfer, the host controller
447 * received data from an endpoint faster than it
448 * could be written to system memory
450 * -ENOSR During an OUT transfer, the host controller
451 * could not retrieve data from system memory fast
452 * enough to keep up with the USB data rate
454 * -EOVERFLOW (*) The amount of data returned by the endpoint was
455 * greater than either the max packet size of the
456 * endpoint or the remaining buffer size. "Babble".
458 * -EREMOTEIO The data read from the endpoint did not fill the
459 * specified buffer, and URB_SHORT_NOT_OK was set in
460 * urb->transfer_flags.
462 * -ENODEV Device was removed. Often preceded by a burst of
463 * other errors, since the hub driver doesn't detect
464 * device removal events immediately.
466 * -EXDEV ISO transfer only partially completed
467 * (only set in iso_frame_desc[n].status, not urb->status)
469 * -EINVAL ISO madness, if this happens: Log off and go home
471 * -ECONNRESET URB was asynchronously unlinked by usb_unlink_urb
473 * -ESHUTDOWN The device or host controller has been disabled due
474 * to some problem that could not be worked around,
475 * such as a physical disconnect.
478 * (*) Error codes like -EPROTO, -EILSEQ and -EOVERFLOW normally indicate
479 * hardware problems such as bad devices (including firmware) or cables.
481 * (**) This is also one of several codes that different kinds of host
482 * controller use to indicate a transfer has failed because of device
483 * disconnect. In the interval before the hub driver starts disconnect
484 * processing, devices may receive such fault reports for every request.
486 * See <https://www.kernel.org/doc/Documentation/driver-api/usb/error-codes.rst>
488 static void hdm_read_completion(struct urb *urb)
490 struct mbo *mbo = urb->context;
491 struct most_dev *mdev = to_mdev(mbo->ifp);
492 unsigned int channel = mbo->hdm_channel_id;
493 struct device *dev = &mdev->usb_device->dev;
494 spinlock_t *lock = mdev->channel_lock + channel;
497 spin_lock_irqsave(lock, flags);
499 mbo->processed_length = 0;
500 mbo->status = MBO_E_INVAL;
501 if (likely(mdev->is_channel_healthy[channel])) {
502 switch (urb->status) {
505 mbo->processed_length = urb->actual_length;
506 mbo->status = MBO_SUCCESS;
507 if (mdev->padding_active[channel] &&
508 hdm_remove_padding(mdev, channel, mbo)) {
509 mbo->processed_length = 0;
510 mbo->status = MBO_E_INVAL;
514 dev_warn(dev, "Broken pipe on ep%02x\n",
515 mdev->ep_address[channel]);
516 mdev->is_channel_healthy[channel] = false;
517 mdev->clear_work[channel].pipe = urb->pipe;
518 schedule_work(&mdev->clear_work[channel].ws);
522 mbo->status = MBO_E_CLOSE;
525 dev_warn(dev, "Babble on ep%02x\n",
526 mdev->ep_address[channel]);
531 spin_unlock_irqrestore(lock, flags);
533 if (likely(mbo->complete))
539 * hdm_enqueue - receive a buffer to be used for data transfer
540 * @iface: interface to enqueue to
541 * @channel: ID of the channel
542 * @mbo: pointer to the buffer object
544 * This allocates a new URB and fills it according to the channel
545 * that is being used for transmission of data. Before the URB is
546 * submitted it is stored in the private anchor list.
548 * Returns 0 on success. On any error the URB is freed and a error code
551 * Context: Could in _some_ cases be interrupt!
553 static int hdm_enqueue(struct most_interface *iface, int channel,
556 struct most_dev *mdev;
557 struct most_channel_config *conf;
560 unsigned long length;
563 if (unlikely(!iface || !mbo))
565 if (unlikely(iface->num_channels <= channel || channel < 0))
568 mdev = to_mdev(iface);
569 conf = &mdev->conf[channel];
571 mutex_lock(&mdev->io_mutex);
572 if (!mdev->usb_device) {
574 goto unlock_io_mutex;
577 urb = usb_alloc_urb(NO_ISOCHRONOUS_URB, GFP_ATOMIC);
580 goto unlock_io_mutex;
583 if ((conf->direction & MOST_CH_TX) && mdev->padding_active[channel] &&
584 hdm_add_padding(mdev, channel, mbo)) {
589 urb->transfer_dma = mbo->bus_address;
590 virt_address = mbo->virt_address;
591 length = mbo->buffer_length;
593 if (conf->direction & MOST_CH_TX) {
594 usb_fill_bulk_urb(urb, mdev->usb_device,
595 usb_sndbulkpipe(mdev->usb_device,
596 mdev->ep_address[channel]),
599 hdm_write_completion,
601 if (conf->data_type != MOST_CH_ISOC &&
602 conf->data_type != MOST_CH_SYNC)
603 urb->transfer_flags |= URB_ZERO_PACKET;
605 usb_fill_bulk_urb(urb, mdev->usb_device,
606 usb_rcvbulkpipe(mdev->usb_device,
607 mdev->ep_address[channel]),
609 length + conf->extra_len,
613 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
615 usb_anchor_urb(urb, &mdev->busy_urbs[channel]);
617 retval = usb_submit_urb(urb, GFP_KERNEL);
619 dev_err(&mdev->usb_device->dev,
620 "URB submit failed with error %d.\n", retval);
621 goto err_unanchor_urb;
623 goto unlock_io_mutex;
626 usb_unanchor_urb(urb);
630 mutex_unlock(&mdev->io_mutex);
634 static void *hdm_dma_alloc(struct mbo *mbo, u32 size)
636 struct most_dev *mdev = to_mdev(mbo->ifp);
638 return usb_alloc_coherent(mdev->usb_device, size, GFP_KERNEL,
642 static void hdm_dma_free(struct mbo *mbo, u32 size)
644 struct most_dev *mdev = to_mdev(mbo->ifp);
646 usb_free_coherent(mdev->usb_device, size, mbo->virt_address,
651 * hdm_configure_channel - receive channel configuration from core
653 * @channel: channel ID
654 * @conf: structure that holds the configuration information
656 * The attached network interface controller (NIC) supports a padding mode
657 * to avoid short packets on USB, hence increasing the performance due to a
658 * lower interrupt load. This mode is default for synchronous data and can
659 * be switched on for isochronous data. In case padding is active the
660 * driver needs to know the frame size of the payload in order to calculate
661 * the number of bytes it needs to pad when transmitting or to cut off when
665 static int hdm_configure_channel(struct most_interface *iface, int channel,
666 struct most_channel_config *conf)
668 unsigned int num_frames;
669 unsigned int frame_size;
670 struct most_dev *mdev = to_mdev(iface);
671 struct device *dev = &mdev->usb_device->dev;
673 mdev->is_channel_healthy[channel] = true;
674 mdev->clear_work[channel].channel = channel;
675 mdev->clear_work[channel].mdev = mdev;
676 INIT_WORK(&mdev->clear_work[channel].ws, wq_clear_halt);
678 if (unlikely(!iface || !conf)) {
679 dev_err(dev, "Bad interface or config pointer.\n");
682 if (unlikely(channel < 0 || channel >= iface->num_channels)) {
683 dev_err(dev, "Channel ID out of range.\n");
686 if (!conf->num_buffers || !conf->buffer_size) {
687 dev_err(dev, "Misconfig: buffer size or #buffers zero.\n");
691 if (conf->data_type != MOST_CH_SYNC &&
692 !(conf->data_type == MOST_CH_ISOC &&
693 conf->packets_per_xact != 0xFF)) {
694 mdev->padding_active[channel] = false;
696 * Since the NIC's padding mode is not going to be
697 * used, we can skip the frame size calculations and
698 * move directly on to exit.
703 mdev->padding_active[channel] = true;
705 frame_size = get_stream_frame_size(conf);
706 if (frame_size == 0 || frame_size > USB_MTU) {
707 dev_warn(dev, "Misconfig: frame size wrong\n");
711 num_frames = conf->buffer_size / frame_size;
713 if (conf->buffer_size % frame_size) {
714 u16 old_size = conf->buffer_size;
716 conf->buffer_size = num_frames * frame_size;
717 dev_warn(dev, "%s: fixed buffer size (%d -> %d)\n",
718 mdev->suffix[channel], old_size, conf->buffer_size);
721 /* calculate extra length to comply w/ HW padding */
722 conf->extra_len = num_frames * (USB_MTU - frame_size);
725 mdev->conf[channel] = *conf;
726 if (conf->data_type == MOST_CH_ASYNC) {
727 u16 ep = mdev->ep_address[channel];
729 if (start_sync_ep(mdev->usb_device, ep) < 0)
730 dev_warn(dev, "sync for ep%02x failed", ep);
736 * hdm_request_netinfo - request network information
737 * @iface: pointer to interface
738 * @channel: channel ID
740 * This is used as trigger to set up the link status timer that
741 * polls for the NI state of the INIC every 2 seconds.
744 static void hdm_request_netinfo(struct most_interface *iface, int channel,
745 void (*on_netinfo)(struct most_interface *,
749 struct most_dev *mdev;
752 mdev = to_mdev(iface);
753 mdev->on_netinfo = on_netinfo;
757 mdev->link_stat_timer.expires = jiffies + HZ;
758 mod_timer(&mdev->link_stat_timer, mdev->link_stat_timer.expires);
762 * link_stat_timer_handler - schedule work obtaining mac address and link status
763 * @data: pointer to USB device instance
765 * The handler runs in interrupt context. That's why we need to defer the
766 * tasks to a work queue.
768 static void link_stat_timer_handler(struct timer_list *t)
770 struct most_dev *mdev = from_timer(mdev, t, link_stat_timer);
772 schedule_work(&mdev->poll_work_obj);
773 mdev->link_stat_timer.expires = jiffies + (2 * HZ);
774 add_timer(&mdev->link_stat_timer);
778 * wq_netinfo - work queue function to deliver latest networking information
779 * @wq_obj: object that holds data for our deferred work to do
781 * This retrieves the network interface status of the USB INIC
783 static void wq_netinfo(struct work_struct *wq_obj)
785 struct most_dev *mdev = to_mdev_from_work(wq_obj);
786 struct usb_device *usb_device = mdev->usb_device;
787 struct device *dev = &usb_device->dev;
788 u16 hi, mi, lo, link;
791 if (drci_rd_reg(usb_device, DRCI_REG_HW_ADDR_HI, &hi) < 0) {
792 dev_err(dev, "Vendor request 'hw_addr_hi' failed\n");
796 if (drci_rd_reg(usb_device, DRCI_REG_HW_ADDR_MI, &mi) < 0) {
797 dev_err(dev, "Vendor request 'hw_addr_mid' failed\n");
801 if (drci_rd_reg(usb_device, DRCI_REG_HW_ADDR_LO, &lo) < 0) {
802 dev_err(dev, "Vendor request 'hw_addr_low' failed\n");
806 if (drci_rd_reg(usb_device, DRCI_REG_NI_STATE, &link) < 0) {
807 dev_err(dev, "Vendor request 'link status' failed\n");
811 hw_addr[0] = hi >> 8;
813 hw_addr[2] = mi >> 8;
815 hw_addr[4] = lo >> 8;
818 if (mdev->on_netinfo)
819 mdev->on_netinfo(&mdev->iface, link, hw_addr);
823 * wq_clear_halt - work queue function
824 * @wq_obj: work_struct object to execute
826 * This sends a clear_halt to the given USB pipe.
828 static void wq_clear_halt(struct work_struct *wq_obj)
830 struct clear_hold_work *clear_work = to_clear_hold_work(wq_obj);
831 struct most_dev *mdev = clear_work->mdev;
832 unsigned int channel = clear_work->channel;
833 int pipe = clear_work->pipe;
835 mutex_lock(&mdev->io_mutex);
836 most_stop_enqueue(&mdev->iface, channel);
837 usb_kill_anchored_urbs(&mdev->busy_urbs[channel]);
838 if (usb_clear_halt(mdev->usb_device, pipe))
839 dev_warn(&mdev->usb_device->dev, "Failed to reset endpoint.\n");
841 /* If the functional Stall condition has been set on an
842 * asynchronous rx channel, we need to clear the tx channel
843 * too, since the hardware runs its clean-up sequence on both
844 * channels, as they are physically one on the network.
846 * The USB interface that exposes the asynchronous channels
847 * contains always two endpoints, and two only.
849 if (mdev->conf[channel].data_type == MOST_CH_ASYNC &&
850 mdev->conf[channel].direction == MOST_CH_RX) {
851 int peer = 1 - channel;
852 int snd_pipe = usb_sndbulkpipe(mdev->usb_device,
853 mdev->ep_address[peer]);
854 usb_clear_halt(mdev->usb_device, snd_pipe);
856 mdev->is_channel_healthy[channel] = true;
857 most_resume_enqueue(&mdev->iface, channel);
858 mutex_unlock(&mdev->io_mutex);
862 * hdm_usb_fops - file operation table for USB driver
864 static const struct file_operations hdm_usb_fops = {
865 .owner = THIS_MODULE,
869 * usb_device_id - ID table for HCD device probing
871 static const struct usb_device_id usbid[] = {
872 { USB_DEVICE(USB_VENDOR_ID_SMSC, USB_DEV_ID_BRDG), },
873 { USB_DEVICE(USB_VENDOR_ID_SMSC, USB_DEV_ID_OS81118), },
874 { USB_DEVICE(USB_VENDOR_ID_SMSC, USB_DEV_ID_OS81119), },
875 { USB_DEVICE(USB_VENDOR_ID_SMSC, USB_DEV_ID_OS81210), },
876 { } /* Terminating entry */
884 static const struct regs ro_regs[] = {
885 { "ni_state", DRCI_REG_NI_STATE },
886 { "packet_bandwidth", DRCI_REG_PACKET_BW },
887 { "node_address", DRCI_REG_NODE_ADDR },
888 { "node_position", DRCI_REG_NODE_POS },
891 static const struct regs rw_regs[] = {
892 { "mep_filter", DRCI_REG_MEP_FILTER },
893 { "mep_hash0", DRCI_REG_HASH_TBL0 },
894 { "mep_hash1", DRCI_REG_HASH_TBL1 },
895 { "mep_hash2", DRCI_REG_HASH_TBL2 },
896 { "mep_hash3", DRCI_REG_HASH_TBL3 },
897 { "mep_eui48_hi", DRCI_REG_HW_ADDR_HI },
898 { "mep_eui48_mi", DRCI_REG_HW_ADDR_MI },
899 { "mep_eui48_lo", DRCI_REG_HW_ADDR_LO },
902 static int get_stat_reg_addr(const struct regs *regs, int size,
903 const char *name, u16 *reg_addr)
907 for (i = 0; i < size; i++) {
908 if (!strcmp(name, regs[i].name)) {
909 *reg_addr = regs[i].reg;
916 #define get_static_reg_addr(regs, name, reg_addr) \
917 get_stat_reg_addr(regs, ARRAY_SIZE(regs), name, reg_addr)
919 static ssize_t value_show(struct device *dev, struct device_attribute *attr,
922 const char *name = attr->attr.name;
923 struct most_dci_obj *dci_obj = to_dci_obj(dev);
928 if (!strcmp(name, "arb_address"))
929 return snprintf(buf, PAGE_SIZE, "%04x\n", dci_obj->reg_addr);
931 if (!strcmp(name, "arb_value"))
932 reg_addr = dci_obj->reg_addr;
933 else if (get_static_reg_addr(ro_regs, name, ®_addr) &&
934 get_static_reg_addr(rw_regs, name, ®_addr))
937 err = drci_rd_reg(dci_obj->usb_device, reg_addr, &val);
941 return snprintf(buf, PAGE_SIZE, "%04x\n", val);
944 static ssize_t value_store(struct device *dev, struct device_attribute *attr,
945 const char *buf, size_t count)
949 const char *name = attr->attr.name;
950 struct most_dci_obj *dci_obj = to_dci_obj(dev);
951 struct usb_device *usb_dev = dci_obj->usb_device;
952 int err = kstrtou16(buf, 16, &val);
957 if (!strcmp(name, "arb_address")) {
958 dci_obj->reg_addr = val;
962 if (!strcmp(name, "arb_value"))
963 err = drci_wr_reg(usb_dev, dci_obj->reg_addr, val);
964 else if (!strcmp(name, "sync_ep"))
965 err = start_sync_ep(usb_dev, val);
966 else if (!get_static_reg_addr(rw_regs, name, ®_addr))
967 err = drci_wr_reg(usb_dev, reg_addr, val);
977 static DEVICE_ATTR(ni_state, 0444, value_show, NULL);
978 static DEVICE_ATTR(packet_bandwidth, 0444, value_show, NULL);
979 static DEVICE_ATTR(node_address, 0444, value_show, NULL);
980 static DEVICE_ATTR(node_position, 0444, value_show, NULL);
981 static DEVICE_ATTR(sync_ep, 0200, NULL, value_store);
982 static DEVICE_ATTR(mep_filter, 0644, value_show, value_store);
983 static DEVICE_ATTR(mep_hash0, 0644, value_show, value_store);
984 static DEVICE_ATTR(mep_hash1, 0644, value_show, value_store);
985 static DEVICE_ATTR(mep_hash2, 0644, value_show, value_store);
986 static DEVICE_ATTR(mep_hash3, 0644, value_show, value_store);
987 static DEVICE_ATTR(mep_eui48_hi, 0644, value_show, value_store);
988 static DEVICE_ATTR(mep_eui48_mi, 0644, value_show, value_store);
989 static DEVICE_ATTR(mep_eui48_lo, 0644, value_show, value_store);
990 static DEVICE_ATTR(arb_address, 0644, value_show, value_store);
991 static DEVICE_ATTR(arb_value, 0644, value_show, value_store);
993 static struct attribute *dci_attrs[] = {
994 &dev_attr_ni_state.attr,
995 &dev_attr_packet_bandwidth.attr,
996 &dev_attr_node_address.attr,
997 &dev_attr_node_position.attr,
998 &dev_attr_sync_ep.attr,
999 &dev_attr_mep_filter.attr,
1000 &dev_attr_mep_hash0.attr,
1001 &dev_attr_mep_hash1.attr,
1002 &dev_attr_mep_hash2.attr,
1003 &dev_attr_mep_hash3.attr,
1004 &dev_attr_mep_eui48_hi.attr,
1005 &dev_attr_mep_eui48_mi.attr,
1006 &dev_attr_mep_eui48_lo.attr,
1007 &dev_attr_arb_address.attr,
1008 &dev_attr_arb_value.attr,
1012 static struct attribute_group dci_attr_group = {
1016 static const struct attribute_group *dci_attr_groups[] = {
1021 static void release_dci(struct device *dev)
1023 struct most_dci_obj *dci = to_dci_obj(dev);
1028 static void release_mdev(struct device *dev)
1030 struct most_dev *mdev = to_mdev_from_dev(dev);
1035 * hdm_probe - probe function of USB device driver
1036 * @interface: Interface of the attached USB device
1037 * @id: Pointer to the USB ID table.
1039 * This allocates and initializes the device instance, adds the new
1040 * entry to the internal list, scans the USB descriptors and registers
1041 * the interface with the core.
1042 * Additionally, the DCI objects are created and the hardware is sync'd.
1044 * Return 0 on success. In case of an error a negative number is returned.
1047 hdm_probe(struct usb_interface *interface, const struct usb_device_id *id)
1049 struct usb_host_interface *usb_iface_desc = interface->cur_altsetting;
1050 struct usb_device *usb_dev = interface_to_usbdev(interface);
1051 struct device *dev = &usb_dev->dev;
1052 struct most_dev *mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
1054 unsigned int num_endpoints;
1055 struct most_channel_capability *tmp_cap;
1056 struct usb_endpoint_descriptor *ep_desc;
1060 goto err_out_of_memory;
1062 usb_set_intfdata(interface, mdev);
1063 num_endpoints = usb_iface_desc->desc.bNumEndpoints;
1064 mutex_init(&mdev->io_mutex);
1065 INIT_WORK(&mdev->poll_work_obj, wq_netinfo);
1066 timer_setup(&mdev->link_stat_timer, link_stat_timer_handler, 0);
1068 mdev->usb_device = usb_dev;
1069 mdev->link_stat_timer.expires = jiffies + (2 * HZ);
1071 mdev->iface.mod = hdm_usb_fops.owner;
1072 mdev->iface.dev = &mdev->dev;
1073 mdev->iface.driver_dev = &interface->dev;
1074 mdev->iface.interface = ITYPE_USB;
1075 mdev->iface.configure = hdm_configure_channel;
1076 mdev->iface.request_netinfo = hdm_request_netinfo;
1077 mdev->iface.enqueue = hdm_enqueue;
1078 mdev->iface.poison_channel = hdm_poison_channel;
1079 mdev->iface.dma_alloc = hdm_dma_alloc;
1080 mdev->iface.dma_free = hdm_dma_free;
1081 mdev->iface.description = mdev->description;
1082 mdev->iface.num_channels = num_endpoints;
1084 snprintf(mdev->description, sizeof(mdev->description),
1086 usb_dev->bus->busnum,
1088 usb_dev->config->desc.bConfigurationValue,
1089 usb_iface_desc->desc.bInterfaceNumber);
1091 mdev->dev.init_name = mdev->description;
1092 mdev->dev.parent = &interface->dev;
1093 mdev->dev.release = release_mdev;
1094 mdev->conf = kcalloc(num_endpoints, sizeof(*mdev->conf), GFP_KERNEL);
1098 mdev->cap = kcalloc(num_endpoints, sizeof(*mdev->cap), GFP_KERNEL);
1102 mdev->iface.channel_vector = mdev->cap;
1104 kcalloc(num_endpoints, sizeof(*mdev->ep_address), GFP_KERNEL);
1105 if (!mdev->ep_address)
1109 kcalloc(num_endpoints, sizeof(*mdev->busy_urbs), GFP_KERNEL);
1110 if (!mdev->busy_urbs)
1111 goto err_free_ep_address;
1113 tmp_cap = mdev->cap;
1114 for (i = 0; i < num_endpoints; i++) {
1115 ep_desc = &usb_iface_desc->endpoint[i].desc;
1116 mdev->ep_address[i] = ep_desc->bEndpointAddress;
1117 mdev->padding_active[i] = false;
1118 mdev->is_channel_healthy[i] = true;
1120 snprintf(&mdev->suffix[i][0], MAX_SUFFIX_LEN, "ep%02x",
1121 mdev->ep_address[i]);
1123 tmp_cap->name_suffix = &mdev->suffix[i][0];
1124 tmp_cap->buffer_size_packet = MAX_BUF_SIZE;
1125 tmp_cap->buffer_size_streaming = MAX_BUF_SIZE;
1126 tmp_cap->num_buffers_packet = BUF_CHAIN_SIZE;
1127 tmp_cap->num_buffers_streaming = BUF_CHAIN_SIZE;
1128 tmp_cap->data_type = MOST_CH_CONTROL | MOST_CH_ASYNC |
1129 MOST_CH_ISOC | MOST_CH_SYNC;
1130 if (usb_endpoint_dir_in(ep_desc))
1131 tmp_cap->direction = MOST_CH_RX;
1133 tmp_cap->direction = MOST_CH_TX;
1135 init_usb_anchor(&mdev->busy_urbs[i]);
1136 spin_lock_init(&mdev->channel_lock[i]);
1138 dev_notice(dev, "claimed gadget: Vendor=%4.4x ProdID=%4.4x Bus=%02x Device=%02x\n",
1139 le16_to_cpu(usb_dev->descriptor.idVendor),
1140 le16_to_cpu(usb_dev->descriptor.idProduct),
1141 usb_dev->bus->busnum,
1144 dev_notice(dev, "device path: /sys/bus/usb/devices/%d-%s:%d.%d\n",
1145 usb_dev->bus->busnum,
1147 usb_dev->config->desc.bConfigurationValue,
1148 usb_iface_desc->desc.bInterfaceNumber);
1150 ret = most_register_interface(&mdev->iface);
1152 goto err_free_busy_urbs;
1154 mutex_lock(&mdev->io_mutex);
1155 if (le16_to_cpu(usb_dev->descriptor.idProduct) == USB_DEV_ID_OS81118 ||
1156 le16_to_cpu(usb_dev->descriptor.idProduct) == USB_DEV_ID_OS81119 ||
1157 le16_to_cpu(usb_dev->descriptor.idProduct) == USB_DEV_ID_OS81210) {
1158 mdev->dci = kzalloc(sizeof(*mdev->dci), GFP_KERNEL);
1160 mutex_unlock(&mdev->io_mutex);
1161 most_deregister_interface(&mdev->iface);
1163 goto err_free_busy_urbs;
1166 mdev->dci->dev.init_name = "dci";
1167 mdev->dci->dev.parent = get_device(mdev->iface.dev);
1168 mdev->dci->dev.groups = dci_attr_groups;
1169 mdev->dci->dev.release = release_dci;
1170 if (device_register(&mdev->dci->dev)) {
1171 mutex_unlock(&mdev->io_mutex);
1172 most_deregister_interface(&mdev->iface);
1176 mdev->dci->usb_device = mdev->usb_device;
1178 mutex_unlock(&mdev->io_mutex);
1181 put_device(&mdev->dci->dev);
1183 kfree(mdev->busy_urbs);
1184 err_free_ep_address:
1185 kfree(mdev->ep_address);
1191 put_device(&mdev->dev);
1193 if (ret == 0 || ret == -ENOMEM) {
1195 dev_err(dev, "out of memory\n");
1201 * hdm_disconnect - disconnect function of USB device driver
1202 * @interface: Interface of the attached USB device
1204 * This deregisters the interface with the core, removes the kernel timer
1205 * and frees resources.
1207 * Context: hub kernel thread
1209 static void hdm_disconnect(struct usb_interface *interface)
1211 struct most_dev *mdev = usb_get_intfdata(interface);
1213 mutex_lock(&mdev->io_mutex);
1214 usb_set_intfdata(interface, NULL);
1215 mdev->usb_device = NULL;
1216 mutex_unlock(&mdev->io_mutex);
1218 del_timer_sync(&mdev->link_stat_timer);
1219 cancel_work_sync(&mdev->poll_work_obj);
1222 device_unregister(&mdev->dci->dev);
1223 most_deregister_interface(&mdev->iface);
1225 kfree(mdev->busy_urbs);
1228 kfree(mdev->ep_address);
1229 put_device(&mdev->dev);
1232 static struct usb_driver hdm_usb = {
1236 .disconnect = hdm_disconnect,
1239 module_usb_driver(hdm_usb);
1240 MODULE_LICENSE("GPL");
1241 MODULE_AUTHOR("Christian Gromm <christian.gromm@microchip.com>");
1242 MODULE_DESCRIPTION("HDM_4_USB");