1 // SPDX-License-Identifier: GPL-2.0
3 * hdm_usb.c - Hardware dependent module for USB
5 * Copyright (C) 2013-2015 Microchip Technology Germany II GmbH & Co. KG
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * This file is licensed under GPLv2.
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/module.h>
18 #include <linux/usb.h>
19 #include <linux/slab.h>
20 #include <linux/init.h>
21 #include <linux/cdev.h>
22 #include <linux/device.h>
23 #include <linux/list.h>
24 #include <linux/completion.h>
25 #include <linux/mutex.h>
26 #include <linux/spinlock.h>
27 #include <linux/interrupt.h>
28 #include <linux/workqueue.h>
29 #include <linux/sysfs.h>
30 #include <linux/dma-mapping.h>
31 #include <linux/etherdevice.h>
32 #include <linux/uaccess.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 * @ep_address: endpoint address table
101 * @description: device description
102 * @suffix: suffix for channel name
103 * @channel_lock: synchronize channel access
104 * @padding_active: indicates channel uses padding
105 * @is_channel_healthy: health status table of each channel
106 * @busy_urbs: list of anchored items
107 * @io_mutex: synchronize I/O with disconnect
108 * @link_stat_timer: timer for link status reports
109 * @poll_work_obj: work for polling link status
112 struct kobject *parent;
113 struct usb_device *usb_device;
114 struct most_interface iface;
115 struct most_channel_capability *cap;
116 struct most_channel_config *conf;
117 struct most_dci_obj *dci;
119 char description[MAX_STRING_LEN];
120 char suffix[MAX_NUM_ENDPOINTS][MAX_SUFFIX_LEN];
121 spinlock_t channel_lock[MAX_NUM_ENDPOINTS]; /* sync channel access */
122 bool padding_active[MAX_NUM_ENDPOINTS];
123 bool is_channel_healthy[MAX_NUM_ENDPOINTS];
124 struct clear_hold_work clear_work[MAX_NUM_ENDPOINTS];
125 struct usb_anchor *busy_urbs;
126 struct mutex io_mutex;
127 struct timer_list link_stat_timer;
128 struct work_struct poll_work_obj;
129 void (*on_netinfo)(struct most_interface *, unsigned char,
133 #define to_mdev(d) container_of(d, struct most_dev, iface)
134 #define to_mdev_from_work(w) container_of(w, struct most_dev, poll_work_obj)
136 static void wq_clear_halt(struct work_struct *wq_obj);
137 static void wq_netinfo(struct work_struct *wq_obj);
140 * drci_rd_reg - read a DCI register
142 * @reg: register address
143 * @buf: buffer to store data
145 * This is reads data from INIC's direct register communication interface
147 static inline int drci_rd_reg(struct usb_device *dev, u16 reg, u16 *buf)
150 __le16 *dma_buf = kzalloc(sizeof(*dma_buf), GFP_KERNEL);
151 u8 req_type = USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
156 retval = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
157 DRCI_READ_REQ, req_type,
159 reg, dma_buf, sizeof(*dma_buf), 5 * HZ);
160 *buf = le16_to_cpu(*dma_buf);
167 * drci_wr_reg - write a DCI register
169 * @reg: register address
170 * @data: data to write
172 * This is writes data to INIC's direct register communication interface
174 static inline int drci_wr_reg(struct usb_device *dev, u16 reg, u16 data)
176 return usb_control_msg(dev,
177 usb_sndctrlpipe(dev, 0),
179 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
187 static inline int start_sync_ep(struct usb_device *usb_dev, u16 ep)
189 return drci_wr_reg(usb_dev, DRCI_REG_BASE + DRCI_COMMAND + ep * 16, 1);
193 * get_stream_frame_size - calculate frame size of current configuration
194 * @cfg: channel configuration
196 static unsigned int get_stream_frame_size(struct most_channel_config *cfg)
198 unsigned int frame_size = 0;
199 unsigned int sub_size = cfg->subbuffer_size;
202 pr_warn("Misconfig: Subbuffer size zero.\n");
205 switch (cfg->data_type) {
207 frame_size = AV_PACKETS_PER_XACT * sub_size;
210 if (cfg->packets_per_xact == 0) {
211 pr_warn("Misconfig: Packets per XACT zero\n");
213 } else if (cfg->packets_per_xact == 0xFF) {
214 frame_size = (USB_MTU / sub_size) * sub_size;
216 frame_size = cfg->packets_per_xact * sub_size;
220 pr_warn("Query frame size of non-streaming channel\n");
227 * hdm_poison_channel - mark buffers of this channel as invalid
228 * @iface: pointer to the interface
229 * @channel: channel ID
231 * This unlinks all URBs submitted to the HCD,
232 * calls the associated completion function of the core and removes
233 * them from the list.
235 * Returns 0 on success or error code otherwise.
237 static int hdm_poison_channel(struct most_interface *iface, int channel)
239 struct most_dev *mdev = to_mdev(iface);
241 spinlock_t *lock; /* temp. lock */
243 if (unlikely(!iface)) {
244 dev_warn(&mdev->usb_device->dev, "Poison: Bad interface.\n");
247 if (unlikely(channel < 0 || channel >= iface->num_channels)) {
248 dev_warn(&mdev->usb_device->dev, "Channel ID out of range.\n");
252 lock = mdev->channel_lock + channel;
253 spin_lock_irqsave(lock, flags);
254 mdev->is_channel_healthy[channel] = false;
255 spin_unlock_irqrestore(lock, flags);
257 cancel_work_sync(&mdev->clear_work[channel].ws);
259 mutex_lock(&mdev->io_mutex);
260 usb_kill_anchored_urbs(&mdev->busy_urbs[channel]);
261 if (mdev->padding_active[channel])
262 mdev->padding_active[channel] = false;
264 if (mdev->conf[channel].data_type == MOST_CH_ASYNC) {
265 del_timer_sync(&mdev->link_stat_timer);
266 cancel_work_sync(&mdev->poll_work_obj);
268 mutex_unlock(&mdev->io_mutex);
273 * hdm_add_padding - add padding bytes
275 * @channel: channel ID
276 * @mbo: buffer object
278 * This inserts the INIC hardware specific padding bytes into a streaming
281 static int hdm_add_padding(struct most_dev *mdev, int channel, struct mbo *mbo)
283 struct most_channel_config *conf = &mdev->conf[channel];
284 unsigned int frame_size = get_stream_frame_size(conf);
285 unsigned int j, num_frames;
289 num_frames = mbo->buffer_length / frame_size;
291 if (num_frames < 1) {
292 dev_err(&mdev->usb_device->dev,
293 "Missed minimal transfer unit.\n");
297 for (j = num_frames - 1; j > 0; j--)
298 memmove(mbo->virt_address + j * USB_MTU,
299 mbo->virt_address + j * frame_size,
301 mbo->buffer_length = num_frames * USB_MTU;
306 * hdm_remove_padding - remove padding bytes
308 * @channel: channel ID
309 * @mbo: buffer object
311 * This takes the INIC hardware specific padding bytes off a streaming
314 static int hdm_remove_padding(struct most_dev *mdev, int channel,
317 struct most_channel_config *const conf = &mdev->conf[channel];
318 unsigned int frame_size = get_stream_frame_size(conf);
319 unsigned int j, num_frames;
323 num_frames = mbo->processed_length / USB_MTU;
325 for (j = 1; j < num_frames; j++)
326 memmove(mbo->virt_address + frame_size * j,
327 mbo->virt_address + USB_MTU * j,
330 mbo->processed_length = frame_size * num_frames;
335 * hdm_write_completion - completion function for submitted Tx URBs
336 * @urb: the URB that has been completed
338 * This checks the status of the completed URB. In case the URB has been
339 * unlinked before, it is immediately freed. On any other error the MBO
340 * transfer flag is set. On success it frees allocated resources and calls
341 * the completion function.
343 * Context: interrupt!
345 static void hdm_write_completion(struct urb *urb)
347 struct mbo *mbo = urb->context;
348 struct most_dev *mdev = to_mdev(mbo->ifp);
349 unsigned int channel = mbo->hdm_channel_id;
350 struct device *dev = &mdev->usb_device->dev;
351 spinlock_t *lock = mdev->channel_lock + channel;
354 spin_lock_irqsave(lock, flags);
356 mbo->processed_length = 0;
357 mbo->status = MBO_E_INVAL;
358 if (likely(mdev->is_channel_healthy[channel])) {
359 switch (urb->status) {
362 mbo->processed_length = urb->actual_length;
363 mbo->status = MBO_SUCCESS;
366 dev_warn(dev, "Broken OUT pipe detected\n");
367 mdev->is_channel_healthy[channel] = false;
368 mdev->clear_work[channel].pipe = urb->pipe;
369 schedule_work(&mdev->clear_work[channel].ws);
373 mbo->status = MBO_E_CLOSE;
378 spin_unlock_irqrestore(lock, flags);
380 if (likely(mbo->complete))
386 * hdm_read_completion - completion function for submitted Rx URBs
387 * @urb: the URB that has been completed
389 * This checks the status of the completed URB. In case the URB has been
390 * unlinked before it is immediately freed. On any other error the MBO transfer
391 * flag is set. On success it frees allocated resources, removes
392 * padding bytes -if necessary- and calls the completion function.
394 * Context: interrupt!
396 * **************************************************************************
397 * Error codes returned by in urb->status
398 * or in iso_frame_desc[n].status (for ISO)
399 * *************************************************************************
401 * USB device drivers may only test urb status values in completion handlers.
402 * This is because otherwise there would be a race between HCDs updating
403 * these values on one CPU, and device drivers testing them on another CPU.
405 * A transfer's actual_length may be positive even when an error has been
406 * reported. That's because transfers often involve several packets, so that
407 * one or more packets could finish before an error stops further endpoint I/O.
409 * For isochronous URBs, the urb status value is non-zero only if the URB is
410 * unlinked, the device is removed, the host controller is disabled or the total
411 * transferred length is less than the requested length and the URB_SHORT_NOT_OK
412 * flag is set. Completion handlers for isochronous URBs should only see
413 * urb->status set to zero, -ENOENT, -ECONNRESET, -ESHUTDOWN, or -EREMOTEIO.
414 * Individual frame descriptor status fields may report more status codes.
417 * 0 Transfer completed successfully
419 * -ENOENT URB was synchronously unlinked by usb_unlink_urb
421 * -EINPROGRESS URB still pending, no results yet
422 * (That is, if drivers see this it's a bug.)
424 * -EPROTO (*, **) a) bitstuff error
425 * b) no response packet received within the
426 * prescribed bus turn-around time
427 * c) unknown USB error
429 * -EILSEQ (*, **) a) CRC mismatch
430 * b) no response packet received within the
431 * prescribed bus turn-around time
432 * c) unknown USB error
434 * Note that often the controller hardware does not
435 * distinguish among cases a), b), and c), so a
436 * driver cannot tell whether there was a protocol
437 * error, a failure to respond (often caused by
438 * device disconnect), or some other fault.
440 * -ETIME (**) No response packet received within the prescribed
441 * bus turn-around time. This error may instead be
442 * reported as -EPROTO or -EILSEQ.
444 * -ETIMEDOUT Synchronous USB message functions use this code
445 * to indicate timeout expired before the transfer
446 * completed, and no other error was reported by HC.
448 * -EPIPE (**) Endpoint stalled. For non-control endpoints,
449 * reset this status with usb_clear_halt().
451 * -ECOMM During an IN transfer, the host controller
452 * received data from an endpoint faster than it
453 * could be written to system memory
455 * -ENOSR During an OUT transfer, the host controller
456 * could not retrieve data from system memory fast
457 * enough to keep up with the USB data rate
459 * -EOVERFLOW (*) The amount of data returned by the endpoint was
460 * greater than either the max packet size of the
461 * endpoint or the remaining buffer size. "Babble".
463 * -EREMOTEIO The data read from the endpoint did not fill the
464 * specified buffer, and URB_SHORT_NOT_OK was set in
465 * urb->transfer_flags.
467 * -ENODEV Device was removed. Often preceded by a burst of
468 * other errors, since the hub driver doesn't detect
469 * device removal events immediately.
471 * -EXDEV ISO transfer only partially completed
472 * (only set in iso_frame_desc[n].status, not urb->status)
474 * -EINVAL ISO madness, if this happens: Log off and go home
476 * -ECONNRESET URB was asynchronously unlinked by usb_unlink_urb
478 * -ESHUTDOWN The device or host controller has been disabled due
479 * to some problem that could not be worked around,
480 * such as a physical disconnect.
483 * (*) Error codes like -EPROTO, -EILSEQ and -EOVERFLOW normally indicate
484 * hardware problems such as bad devices (including firmware) or cables.
486 * (**) This is also one of several codes that different kinds of host
487 * controller use to indicate a transfer has failed because of device
488 * disconnect. In the interval before the hub driver starts disconnect
489 * processing, devices may receive such fault reports for every request.
491 * See <https://www.kernel.org/doc/Documentation/driver-api/usb/error-codes.rst>
493 static void hdm_read_completion(struct urb *urb)
495 struct mbo *mbo = urb->context;
496 struct most_dev *mdev = to_mdev(mbo->ifp);
497 unsigned int channel = mbo->hdm_channel_id;
498 struct device *dev = &mdev->usb_device->dev;
499 spinlock_t *lock = mdev->channel_lock + channel;
502 spin_lock_irqsave(lock, flags);
504 mbo->processed_length = 0;
505 mbo->status = MBO_E_INVAL;
506 if (likely(mdev->is_channel_healthy[channel])) {
507 switch (urb->status) {
510 mbo->processed_length = urb->actual_length;
511 mbo->status = MBO_SUCCESS;
512 if (mdev->padding_active[channel] &&
513 hdm_remove_padding(mdev, channel, mbo)) {
514 mbo->processed_length = 0;
515 mbo->status = MBO_E_INVAL;
519 dev_warn(dev, "Broken IN pipe detected\n");
520 mdev->is_channel_healthy[channel] = false;
521 mdev->clear_work[channel].pipe = urb->pipe;
522 schedule_work(&mdev->clear_work[channel].ws);
526 mbo->status = MBO_E_CLOSE;
529 dev_warn(dev, "Babble on IN pipe detected\n");
534 spin_unlock_irqrestore(lock, flags);
536 if (likely(mbo->complete))
542 * hdm_enqueue - receive a buffer to be used for data transfer
543 * @iface: interface to enqueue to
544 * @channel: ID of the channel
545 * @mbo: pointer to the buffer object
547 * This allocates a new URB and fills it according to the channel
548 * that is being used for transmission of data. Before the URB is
549 * submitted it is stored in the private anchor list.
551 * Returns 0 on success. On any error the URB is freed and a error code
554 * Context: Could in _some_ cases be interrupt!
556 static int hdm_enqueue(struct most_interface *iface, int channel,
559 struct most_dev *mdev;
560 struct most_channel_config *conf;
564 unsigned long length;
567 if (unlikely(!iface || !mbo))
569 if (unlikely(iface->num_channels <= channel || channel < 0))
572 mdev = to_mdev(iface);
573 conf = &mdev->conf[channel];
574 dev = &mdev->usb_device->dev;
576 if (!mdev->usb_device)
579 urb = usb_alloc_urb(NO_ISOCHRONOUS_URB, GFP_ATOMIC);
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 urb->transfer_flags |= URB_ZERO_PACKET;
604 usb_fill_bulk_urb(urb, mdev->usb_device,
605 usb_rcvbulkpipe(mdev->usb_device,
606 mdev->ep_address[channel]),
608 length + conf->extra_len,
612 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
614 usb_anchor_urb(urb, &mdev->busy_urbs[channel]);
616 retval = usb_submit_urb(urb, GFP_KERNEL);
618 dev_err(dev, "URB submit failed with error %d.\n", retval);
624 usb_unanchor_urb(urb);
631 * hdm_configure_channel - receive channel configuration from core
633 * @channel: channel ID
634 * @conf: structure that holds the configuration information
636 * The attached network interface controller (NIC) supports a padding mode
637 * to avoid short packets on USB, hence increasing the performance due to a
638 * lower interrupt load. This mode is default for synchronous data and can
639 * be switched on for isochronous data. In case padding is active the
640 * driver needs to know the frame size of the payload in order to calculate
641 * the number of bytes it needs to pad when transmitting or to cut off when
645 static int hdm_configure_channel(struct most_interface *iface, int channel,
646 struct most_channel_config *conf)
648 unsigned int num_frames;
649 unsigned int frame_size;
650 struct most_dev *mdev = to_mdev(iface);
651 struct device *dev = &mdev->usb_device->dev;
653 mdev->is_channel_healthy[channel] = true;
654 mdev->clear_work[channel].channel = channel;
655 mdev->clear_work[channel].mdev = mdev;
656 INIT_WORK(&mdev->clear_work[channel].ws, wq_clear_halt);
658 if (unlikely(!iface || !conf)) {
659 dev_err(dev, "Bad interface or config pointer.\n");
662 if (unlikely(channel < 0 || channel >= iface->num_channels)) {
663 dev_err(dev, "Channel ID out of range.\n");
666 if (!conf->num_buffers || !conf->buffer_size) {
667 dev_err(dev, "Misconfig: buffer size or #buffers zero.\n");
671 if (conf->data_type != MOST_CH_SYNC &&
672 !(conf->data_type == MOST_CH_ISOC &&
673 conf->packets_per_xact != 0xFF)) {
674 mdev->padding_active[channel] = false;
676 * Since the NIC's padding mode is not going to be
677 * used, we can skip the frame size calculations and
678 * move directly on to exit.
683 mdev->padding_active[channel] = true;
685 frame_size = get_stream_frame_size(conf);
686 if (frame_size == 0 || frame_size > USB_MTU) {
687 dev_warn(dev, "Misconfig: frame size wrong\n");
691 num_frames = conf->buffer_size / frame_size;
693 if (conf->buffer_size % frame_size) {
694 u16 old_size = conf->buffer_size;
696 conf->buffer_size = num_frames * frame_size;
697 dev_warn(dev, "%s: fixed buffer size (%d -> %d)\n",
698 mdev->suffix[channel], old_size, conf->buffer_size);
701 /* calculate extra length to comply w/ HW padding */
702 conf->extra_len = num_frames * (USB_MTU - frame_size);
705 mdev->conf[channel] = *conf;
706 if (conf->data_type == MOST_CH_ASYNC) {
707 u16 ep = mdev->ep_address[channel];
709 if (start_sync_ep(mdev->usb_device, ep) < 0)
710 dev_warn(dev, "sync for ep%02x failed", ep);
716 * hdm_request_netinfo - request network information
717 * @iface: pointer to interface
718 * @channel: channel ID
720 * This is used as trigger to set up the link status timer that
721 * polls for the NI state of the INIC every 2 seconds.
724 static void hdm_request_netinfo(struct most_interface *iface, int channel,
725 void (*on_netinfo)(struct most_interface *,
729 struct most_dev *mdev;
732 mdev = to_mdev(iface);
733 mdev->on_netinfo = on_netinfo;
737 mdev->link_stat_timer.expires = jiffies + HZ;
738 mod_timer(&mdev->link_stat_timer, mdev->link_stat_timer.expires);
742 * link_stat_timer_handler - schedule work obtaining mac address and link status
743 * @data: pointer to USB device instance
745 * The handler runs in interrupt context. That's why we need to defer the
746 * tasks to a work queue.
748 static void link_stat_timer_handler(struct timer_list *t)
750 struct most_dev *mdev = from_timer(mdev, t, link_stat_timer);
752 schedule_work(&mdev->poll_work_obj);
753 mdev->link_stat_timer.expires = jiffies + (2 * HZ);
754 add_timer(&mdev->link_stat_timer);
758 * wq_netinfo - work queue function to deliver latest networking information
759 * @wq_obj: object that holds data for our deferred work to do
761 * This retrieves the network interface status of the USB INIC
763 static void wq_netinfo(struct work_struct *wq_obj)
765 struct most_dev *mdev = to_mdev_from_work(wq_obj);
766 struct usb_device *usb_device = mdev->usb_device;
767 struct device *dev = &usb_device->dev;
768 u16 hi, mi, lo, link;
771 if (drci_rd_reg(usb_device, DRCI_REG_HW_ADDR_HI, &hi) < 0) {
772 dev_err(dev, "Vendor request 'hw_addr_hi' failed\n");
776 if (drci_rd_reg(usb_device, DRCI_REG_HW_ADDR_MI, &mi) < 0) {
777 dev_err(dev, "Vendor request 'hw_addr_mid' failed\n");
781 if (drci_rd_reg(usb_device, DRCI_REG_HW_ADDR_LO, &lo) < 0) {
782 dev_err(dev, "Vendor request 'hw_addr_low' failed\n");
786 if (drci_rd_reg(usb_device, DRCI_REG_NI_STATE, &link) < 0) {
787 dev_err(dev, "Vendor request 'link status' failed\n");
791 hw_addr[0] = hi >> 8;
793 hw_addr[2] = mi >> 8;
795 hw_addr[4] = lo >> 8;
798 if (mdev->on_netinfo)
799 mdev->on_netinfo(&mdev->iface, link, hw_addr);
803 * wq_clear_halt - work queue function
804 * @wq_obj: work_struct object to execute
806 * This sends a clear_halt to the given USB pipe.
808 static void wq_clear_halt(struct work_struct *wq_obj)
810 struct clear_hold_work *clear_work = to_clear_hold_work(wq_obj);
811 struct most_dev *mdev = clear_work->mdev;
812 unsigned int channel = clear_work->channel;
813 int pipe = clear_work->pipe;
815 mutex_lock(&mdev->io_mutex);
816 most_stop_enqueue(&mdev->iface, channel);
817 usb_kill_anchored_urbs(&mdev->busy_urbs[channel]);
818 if (usb_clear_halt(mdev->usb_device, pipe))
819 dev_warn(&mdev->usb_device->dev, "Failed to reset endpoint.\n");
821 mdev->is_channel_healthy[channel] = true;
822 most_resume_enqueue(&mdev->iface, channel);
823 mutex_unlock(&mdev->io_mutex);
827 * hdm_usb_fops - file operation table for USB driver
829 static const struct file_operations hdm_usb_fops = {
830 .owner = THIS_MODULE,
834 * usb_device_id - ID table for HCD device probing
836 static const struct usb_device_id usbid[] = {
837 { USB_DEVICE(USB_VENDOR_ID_SMSC, USB_DEV_ID_BRDG), },
838 { USB_DEVICE(USB_VENDOR_ID_SMSC, USB_DEV_ID_OS81118), },
839 { USB_DEVICE(USB_VENDOR_ID_SMSC, USB_DEV_ID_OS81119), },
840 { USB_DEVICE(USB_VENDOR_ID_SMSC, USB_DEV_ID_OS81210), },
841 { } /* Terminating entry */
844 #define MOST_DCI_RO_ATTR(_name) \
845 struct most_dci_attribute most_dci_attr_##_name = \
846 __ATTR(_name, 0444, show_value, NULL)
848 #define MOST_DCI_ATTR(_name) \
849 struct most_dci_attribute most_dci_attr_##_name = \
850 __ATTR(_name, 0644, show_value, store_value)
852 #define MOST_DCI_WO_ATTR(_name) \
853 struct most_dci_attribute most_dci_attr_##_name = \
854 __ATTR(_name, 0200, NULL, store_value)
857 * struct most_dci_attribute - to access the attributes of a dci object
858 * @attr: attributes of a dci object
859 * @show: pointer to the show function
860 * @store: pointer to the store function
862 struct most_dci_attribute {
863 struct attribute attr;
864 ssize_t (*show)(struct most_dci_obj *d,
865 struct most_dci_attribute *attr,
867 ssize_t (*store)(struct most_dci_obj *d,
868 struct most_dci_attribute *attr,
873 #define to_dci_attr(a) container_of(a, struct most_dci_attribute, attr)
876 * dci_attr_show - show function for dci object
877 * @kobj: pointer to kobject
878 * @attr: pointer to attribute struct
881 static ssize_t dci_attr_show(struct kobject *kobj, struct attribute *attr,
884 struct most_dci_attribute *dci_attr = to_dci_attr(attr);
885 struct most_dci_obj *dci_obj = to_dci_obj(kobj);
890 return dci_attr->show(dci_obj, dci_attr, buf);
894 * dci_attr_store - store function for dci object
895 * @kobj: pointer to kobject
896 * @attr: pointer to attribute struct
898 * @len: length of buffer
900 static ssize_t dci_attr_store(struct kobject *kobj,
901 struct attribute *attr,
905 struct most_dci_attribute *dci_attr = to_dci_attr(attr);
906 struct most_dci_obj *dci_obj = to_dci_obj(kobj);
908 if (!dci_attr->store)
911 return dci_attr->store(dci_obj, dci_attr, buf, len);
914 static const struct sysfs_ops most_dci_sysfs_ops = {
915 .show = dci_attr_show,
916 .store = dci_attr_store,
920 * most_dci_release - release function for dci object
921 * @kobj: pointer to kobject
923 * This frees the memory allocated for the dci object
925 static void most_dci_release(struct kobject *kobj)
927 struct most_dci_obj *dci_obj = to_dci_obj(kobj);
937 static const struct regs ro_regs[] = {
938 { "ni_state", DRCI_REG_NI_STATE },
939 { "packet_bandwidth", DRCI_REG_PACKET_BW },
940 { "node_address", DRCI_REG_NODE_ADDR },
941 { "node_position", DRCI_REG_NODE_POS },
944 static const struct regs rw_regs[] = {
945 { "mep_filter", DRCI_REG_MEP_FILTER },
946 { "mep_hash0", DRCI_REG_HASH_TBL0 },
947 { "mep_hash1", DRCI_REG_HASH_TBL1 },
948 { "mep_hash2", DRCI_REG_HASH_TBL2 },
949 { "mep_hash3", DRCI_REG_HASH_TBL3 },
950 { "mep_eui48_hi", DRCI_REG_HW_ADDR_HI },
951 { "mep_eui48_mi", DRCI_REG_HW_ADDR_MI },
952 { "mep_eui48_lo", DRCI_REG_HW_ADDR_LO },
955 static int get_stat_reg_addr(const struct regs *regs, int size,
956 const char *name, u16 *reg_addr)
960 for (i = 0; i < size; i++) {
961 if (!strcmp(name, regs[i].name)) {
962 *reg_addr = regs[i].reg;
969 #define get_static_reg_addr(regs, name, reg_addr) \
970 get_stat_reg_addr(regs, ARRAY_SIZE(regs), name, reg_addr)
972 static ssize_t show_value(struct most_dci_obj *dci_obj,
973 struct most_dci_attribute *attr, char *buf)
975 const char *name = attr->attr.name;
980 if (!strcmp(name, "arb_address"))
981 return snprintf(buf, PAGE_SIZE, "%04x\n", dci_obj->reg_addr);
983 if (!strcmp(name, "arb_value"))
984 reg_addr = dci_obj->reg_addr;
985 else if (get_static_reg_addr(ro_regs, name, ®_addr) &&
986 get_static_reg_addr(rw_regs, name, ®_addr))
989 err = drci_rd_reg(dci_obj->usb_device, reg_addr, &val);
993 return snprintf(buf, PAGE_SIZE, "%04x\n", val);
996 static ssize_t store_value(struct most_dci_obj *dci_obj,
997 struct most_dci_attribute *attr,
998 const char *buf, size_t count)
1002 const char *name = attr->attr.name;
1003 struct usb_device *usb_dev = dci_obj->usb_device;
1004 int err = kstrtou16(buf, 16, &val);
1009 if (!strcmp(name, "arb_address")) {
1010 dci_obj->reg_addr = val;
1014 if (!strcmp(name, "arb_value"))
1015 err = drci_wr_reg(usb_dev, dci_obj->reg_addr, val);
1016 else if (!strcmp(name, "sync_ep"))
1017 err = start_sync_ep(usb_dev, val);
1018 else if (!get_static_reg_addr(rw_regs, name, ®_addr))
1019 err = drci_wr_reg(usb_dev, reg_addr, val);
1029 static MOST_DCI_RO_ATTR(ni_state);
1030 static MOST_DCI_RO_ATTR(packet_bandwidth);
1031 static MOST_DCI_RO_ATTR(node_address);
1032 static MOST_DCI_RO_ATTR(node_position);
1033 static MOST_DCI_WO_ATTR(sync_ep);
1034 static MOST_DCI_ATTR(mep_filter);
1035 static MOST_DCI_ATTR(mep_hash0);
1036 static MOST_DCI_ATTR(mep_hash1);
1037 static MOST_DCI_ATTR(mep_hash2);
1038 static MOST_DCI_ATTR(mep_hash3);
1039 static MOST_DCI_ATTR(mep_eui48_hi);
1040 static MOST_DCI_ATTR(mep_eui48_mi);
1041 static MOST_DCI_ATTR(mep_eui48_lo);
1042 static MOST_DCI_ATTR(arb_address);
1043 static MOST_DCI_ATTR(arb_value);
1046 * most_dci_def_attrs - array of default attribute files of the dci object
1048 static struct attribute *most_dci_def_attrs[] = {
1049 &most_dci_attr_ni_state.attr,
1050 &most_dci_attr_packet_bandwidth.attr,
1051 &most_dci_attr_node_address.attr,
1052 &most_dci_attr_node_position.attr,
1053 &most_dci_attr_sync_ep.attr,
1054 &most_dci_attr_mep_filter.attr,
1055 &most_dci_attr_mep_hash0.attr,
1056 &most_dci_attr_mep_hash1.attr,
1057 &most_dci_attr_mep_hash2.attr,
1058 &most_dci_attr_mep_hash3.attr,
1059 &most_dci_attr_mep_eui48_hi.attr,
1060 &most_dci_attr_mep_eui48_mi.attr,
1061 &most_dci_attr_mep_eui48_lo.attr,
1062 &most_dci_attr_arb_address.attr,
1063 &most_dci_attr_arb_value.attr,
1070 static struct kobj_type most_dci_ktype = {
1071 .sysfs_ops = &most_dci_sysfs_ops,
1072 .release = most_dci_release,
1073 .default_attrs = most_dci_def_attrs,
1077 * create_most_dci_obj - allocates a dci object
1078 * @parent: parent kobject
1080 * This creates a dci object and registers it with sysfs.
1081 * Returns a pointer to the object or NULL when something went wrong.
1084 most_dci_obj *create_most_dci_obj(struct kobject *parent)
1086 struct most_dci_obj *most_dci = kzalloc(sizeof(*most_dci), GFP_KERNEL);
1092 retval = kobject_init_and_add(&most_dci->kobj, &most_dci_ktype, parent,
1095 kobject_put(&most_dci->kobj);
1102 * destroy_most_dci_obj - DCI object release function
1103 * @p: pointer to dci object
1105 static void destroy_most_dci_obj(struct most_dci_obj *p)
1107 kobject_put(&p->kobj);
1111 * hdm_probe - probe function of USB device driver
1112 * @interface: Interface of the attached USB device
1113 * @id: Pointer to the USB ID table.
1115 * This allocates and initializes the device instance, adds the new
1116 * entry to the internal list, scans the USB descriptors and registers
1117 * the interface with the core.
1118 * Additionally, the DCI objects are created and the hardware is sync'd.
1120 * Return 0 on success. In case of an error a negative number is returned.
1123 hdm_probe(struct usb_interface *interface, const struct usb_device_id *id)
1125 struct usb_host_interface *usb_iface_desc = interface->cur_altsetting;
1126 struct usb_device *usb_dev = interface_to_usbdev(interface);
1127 struct device *dev = &usb_dev->dev;
1128 struct most_dev *mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
1130 unsigned int num_endpoints;
1131 struct most_channel_capability *tmp_cap;
1132 struct usb_endpoint_descriptor *ep_desc;
1138 usb_set_intfdata(interface, mdev);
1139 num_endpoints = usb_iface_desc->desc.bNumEndpoints;
1140 mutex_init(&mdev->io_mutex);
1141 INIT_WORK(&mdev->poll_work_obj, wq_netinfo);
1142 timer_setup(&mdev->link_stat_timer, link_stat_timer_handler, 0);
1144 mdev->usb_device = usb_dev;
1145 mdev->link_stat_timer.expires = jiffies + (2 * HZ);
1147 mdev->iface.mod = hdm_usb_fops.owner;
1148 mdev->iface.interface = ITYPE_USB;
1149 mdev->iface.configure = hdm_configure_channel;
1150 mdev->iface.request_netinfo = hdm_request_netinfo;
1151 mdev->iface.enqueue = hdm_enqueue;
1152 mdev->iface.poison_channel = hdm_poison_channel;
1153 mdev->iface.description = mdev->description;
1154 mdev->iface.num_channels = num_endpoints;
1156 snprintf(mdev->description, sizeof(mdev->description),
1157 "usb_device %d-%s:%d.%d",
1158 usb_dev->bus->busnum,
1160 usb_dev->config->desc.bConfigurationValue,
1161 usb_iface_desc->desc.bInterfaceNumber);
1163 mdev->conf = kcalloc(num_endpoints, sizeof(*mdev->conf), GFP_KERNEL);
1167 mdev->cap = kcalloc(num_endpoints, sizeof(*mdev->cap), GFP_KERNEL);
1171 mdev->iface.channel_vector = mdev->cap;
1172 mdev->iface.priv = NULL;
1175 kcalloc(num_endpoints, sizeof(*mdev->ep_address), GFP_KERNEL);
1176 if (!mdev->ep_address)
1180 kcalloc(num_endpoints, sizeof(*mdev->busy_urbs), GFP_KERNEL);
1181 if (!mdev->busy_urbs)
1184 tmp_cap = mdev->cap;
1185 for (i = 0; i < num_endpoints; i++) {
1186 ep_desc = &usb_iface_desc->endpoint[i].desc;
1187 mdev->ep_address[i] = ep_desc->bEndpointAddress;
1188 mdev->padding_active[i] = false;
1189 mdev->is_channel_healthy[i] = true;
1191 snprintf(&mdev->suffix[i][0], MAX_SUFFIX_LEN, "ep%02x",
1192 mdev->ep_address[i]);
1194 tmp_cap->name_suffix = &mdev->suffix[i][0];
1195 tmp_cap->buffer_size_packet = MAX_BUF_SIZE;
1196 tmp_cap->buffer_size_streaming = MAX_BUF_SIZE;
1197 tmp_cap->num_buffers_packet = BUF_CHAIN_SIZE;
1198 tmp_cap->num_buffers_streaming = BUF_CHAIN_SIZE;
1199 tmp_cap->data_type = MOST_CH_CONTROL | MOST_CH_ASYNC |
1200 MOST_CH_ISOC | MOST_CH_SYNC;
1201 if (usb_endpoint_dir_in(ep_desc))
1202 tmp_cap->direction = MOST_CH_RX;
1204 tmp_cap->direction = MOST_CH_TX;
1206 init_usb_anchor(&mdev->busy_urbs[i]);
1207 spin_lock_init(&mdev->channel_lock[i]);
1209 dev_notice(dev, "claimed gadget: Vendor=%4.4x ProdID=%4.4x Bus=%02x Device=%02x\n",
1210 le16_to_cpu(usb_dev->descriptor.idVendor),
1211 le16_to_cpu(usb_dev->descriptor.idProduct),
1212 usb_dev->bus->busnum,
1215 dev_notice(dev, "device path: /sys/bus/usb/devices/%d-%s:%d.%d\n",
1216 usb_dev->bus->busnum,
1218 usb_dev->config->desc.bConfigurationValue,
1219 usb_iface_desc->desc.bInterfaceNumber);
1221 mdev->parent = most_register_interface(&mdev->iface);
1222 if (IS_ERR(mdev->parent)) {
1223 ret = PTR_ERR(mdev->parent);
1227 mutex_lock(&mdev->io_mutex);
1228 if (le16_to_cpu(usb_dev->descriptor.idProduct) == USB_DEV_ID_OS81118 ||
1229 le16_to_cpu(usb_dev->descriptor.idProduct) == USB_DEV_ID_OS81119 ||
1230 le16_to_cpu(usb_dev->descriptor.idProduct) == USB_DEV_ID_OS81210) {
1231 /* this increments the reference count of the instance
1232 * object of the core
1234 mdev->dci = create_most_dci_obj(mdev->parent);
1236 mutex_unlock(&mdev->io_mutex);
1237 most_deregister_interface(&mdev->iface);
1242 kobject_uevent(&mdev->dci->kobj, KOBJ_ADD);
1243 mdev->dci->usb_device = mdev->usb_device;
1245 mutex_unlock(&mdev->io_mutex);
1249 kfree(mdev->busy_urbs);
1251 kfree(mdev->ep_address);
1259 if (ret == 0 || ret == -ENOMEM) {
1261 dev_err(dev, "out of memory\n");
1267 * hdm_disconnect - disconnect function of USB device driver
1268 * @interface: Interface of the attached USB device
1270 * This deregisters the interface with the core, removes the kernel timer
1271 * and frees resources.
1273 * Context: hub kernel thread
1275 static void hdm_disconnect(struct usb_interface *interface)
1277 struct most_dev *mdev = usb_get_intfdata(interface);
1279 mutex_lock(&mdev->io_mutex);
1280 usb_set_intfdata(interface, NULL);
1281 mdev->usb_device = NULL;
1282 mutex_unlock(&mdev->io_mutex);
1284 del_timer_sync(&mdev->link_stat_timer);
1285 cancel_work_sync(&mdev->poll_work_obj);
1287 destroy_most_dci_obj(mdev->dci);
1288 most_deregister_interface(&mdev->iface);
1290 kfree(mdev->busy_urbs);
1293 kfree(mdev->ep_address);
1297 static struct usb_driver hdm_usb = {
1301 .disconnect = hdm_disconnect,
1304 module_usb_driver(hdm_usb);
1305 MODULE_LICENSE("GPL");
1306 MODULE_AUTHOR("Christian Gromm <christian.gromm@microchip.com>");
1307 MODULE_DESCRIPTION("HDM_4_USB");