2 * hdm_usb.c - Hardware dependent module for USB
4 * Copyright (C) 2013-2015 Microchip Technology Germany II GmbH & Co. KG
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
11 * This file is licensed under GPLv2.
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/module.h>
17 #include <linux/usb.h>
18 #include <linux/slab.h>
19 #include <linux/init.h>
20 #include <linux/cdev.h>
21 #include <linux/device.h>
22 #include <linux/list.h>
23 #include <linux/completion.h>
24 #include <linux/mutex.h>
25 #include <linux/spinlock.h>
26 #include <linux/interrupt.h>
27 #include <linux/workqueue.h>
28 #include <linux/sysfs.h>
29 #include <linux/dma-mapping.h>
30 #include <linux/etherdevice.h>
31 #include <linux/uaccess.h>
35 #define NO_ISOCHRONOUS_URB 0
36 #define AV_PACKETS_PER_XACT 2
37 #define BUF_CHAIN_SIZE 0xFFFF
38 #define MAX_NUM_ENDPOINTS 30
39 #define MAX_SUFFIX_LEN 10
40 #define MAX_STRING_LEN 80
41 #define MAX_BUF_SIZE 0xFFFF
43 #define USB_VENDOR_ID_SMSC 0x0424 /* VID: SMSC */
44 #define USB_DEV_ID_BRDG 0xC001 /* PID: USB Bridge */
45 #define USB_DEV_ID_OS81118 0xCF18 /* PID: USB OS81118 */
46 #define USB_DEV_ID_OS81119 0xCF19 /* PID: USB OS81119 */
47 #define USB_DEV_ID_OS81210 0xCF30 /* PID: USB OS81210 */
49 #define DRCI_REG_NI_STATE 0x0100
50 #define DRCI_REG_PACKET_BW 0x0101
51 #define DRCI_REG_NODE_ADDR 0x0102
52 #define DRCI_REG_NODE_POS 0x0103
53 #define DRCI_REG_MEP_FILTER 0x0140
54 #define DRCI_REG_HASH_TBL0 0x0141
55 #define DRCI_REG_HASH_TBL1 0x0142
56 #define DRCI_REG_HASH_TBL2 0x0143
57 #define DRCI_REG_HASH_TBL3 0x0144
58 #define DRCI_REG_HW_ADDR_HI 0x0145
59 #define DRCI_REG_HW_ADDR_MI 0x0146
60 #define DRCI_REG_HW_ADDR_LO 0x0147
61 #define DRCI_REG_BASE 0x1100
62 #define DRCI_COMMAND 0x02
63 #define DRCI_READ_REQ 0xA0
64 #define DRCI_WRITE_REQ 0xA1
67 * struct most_dci_obj - Direct Communication Interface
68 * @kobj:position in sysfs
69 * @usb_device: pointer to the usb device
70 * @reg_addr: register address for arbitrary DCI access
74 struct usb_device *usb_device;
78 #define to_dci_obj(p) container_of(p, struct most_dci_obj, kobj)
82 struct clear_hold_work {
83 struct work_struct ws;
84 struct most_dev *mdev;
89 #define to_clear_hold_work(w) container_of(w, struct clear_hold_work, ws)
92 * struct most_dev - holds all usb interface specific stuff
93 * @parent: parent object in sysfs
94 * @usb_device: pointer to usb device
95 * @iface: hardware interface
96 * @cap: channel capabilities
97 * @conf: channel configuration
98 * @dci: direct communication interface of hardware
99 * @ep_address: endpoint address table
100 * @description: device description
101 * @suffix: suffix for channel name
102 * @channel_lock: synchronize channel access
103 * @padding_active: indicates channel uses padding
104 * @is_channel_healthy: health status table of each channel
105 * @busy_urbs: list of anchored items
106 * @io_mutex: synchronize I/O with disconnect
107 * @link_stat_timer: timer for link status reports
108 * @poll_work_obj: work for polling link status
111 struct kobject *parent;
112 struct usb_device *usb_device;
113 struct most_interface iface;
114 struct most_channel_capability *cap;
115 struct most_channel_config *conf;
116 struct most_dci_obj *dci;
118 char description[MAX_STRING_LEN];
119 char suffix[MAX_NUM_ENDPOINTS][MAX_SUFFIX_LEN];
120 spinlock_t channel_lock[MAX_NUM_ENDPOINTS]; /* sync channel access */
121 bool padding_active[MAX_NUM_ENDPOINTS];
122 bool is_channel_healthy[MAX_NUM_ENDPOINTS];
123 struct clear_hold_work clear_work[MAX_NUM_ENDPOINTS];
124 struct usb_anchor *busy_urbs;
125 struct mutex io_mutex;
126 struct timer_list link_stat_timer;
127 struct work_struct poll_work_obj;
128 void (*on_netinfo)(struct most_interface *, unsigned char,
132 #define to_mdev(d) container_of(d, struct most_dev, iface)
133 #define to_mdev_from_work(w) container_of(w, struct most_dev, poll_work_obj)
135 static void wq_clear_halt(struct work_struct *wq_obj);
136 static void wq_netinfo(struct work_struct *wq_obj);
139 * drci_rd_reg - read a DCI register
141 * @reg: register address
142 * @buf: buffer to store data
144 * This is reads data from INIC's direct register communication interface
146 static inline int drci_rd_reg(struct usb_device *dev, u16 reg, u16 *buf)
149 __le16 *dma_buf = kzalloc(sizeof(*dma_buf), GFP_KERNEL);
150 u8 req_type = USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
155 retval = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
156 DRCI_READ_REQ, req_type,
158 reg, dma_buf, sizeof(*dma_buf), 5 * HZ);
159 *buf = le16_to_cpu(*dma_buf);
166 * drci_wr_reg - write a DCI register
168 * @reg: register address
169 * @data: data to write
171 * This is writes data to INIC's direct register communication interface
173 static inline int drci_wr_reg(struct usb_device *dev, u16 reg, u16 data)
175 return usb_control_msg(dev,
176 usb_sndctrlpipe(dev, 0),
178 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
186 static inline int start_sync_ep(struct usb_device *usb_dev, u16 ep)
188 return drci_wr_reg(usb_dev, DRCI_REG_BASE + DRCI_COMMAND + ep * 16, 1);
192 * get_stream_frame_size - calculate frame size of current configuration
193 * @cfg: channel configuration
195 static unsigned int get_stream_frame_size(struct most_channel_config *cfg)
197 unsigned int frame_size = 0;
198 unsigned int sub_size = cfg->subbuffer_size;
201 pr_warn("Misconfig: Subbuffer size zero.\n");
204 switch (cfg->data_type) {
206 frame_size = AV_PACKETS_PER_XACT * sub_size;
209 if (cfg->packets_per_xact == 0) {
210 pr_warn("Misconfig: Packets per XACT zero\n");
212 } else if (cfg->packets_per_xact == 0xFF) {
213 frame_size = (USB_MTU / sub_size) * sub_size;
215 frame_size = cfg->packets_per_xact * sub_size;
219 pr_warn("Query frame size of non-streaming channel\n");
226 * hdm_poison_channel - mark buffers of this channel as invalid
227 * @iface: pointer to the interface
228 * @channel: channel ID
230 * This unlinks all URBs submitted to the HCD,
231 * calls the associated completion function of the core and removes
232 * them from the list.
234 * Returns 0 on success or error code otherwise.
236 static int hdm_poison_channel(struct most_interface *iface, int channel)
238 struct most_dev *mdev = to_mdev(iface);
240 spinlock_t *lock; /* temp. lock */
242 if (unlikely(!iface)) {
243 dev_warn(&mdev->usb_device->dev, "Poison: Bad interface.\n");
246 if (unlikely(channel < 0 || channel >= iface->num_channels)) {
247 dev_warn(&mdev->usb_device->dev, "Channel ID out of range.\n");
251 lock = mdev->channel_lock + channel;
252 spin_lock_irqsave(lock, flags);
253 mdev->is_channel_healthy[channel] = false;
254 spin_unlock_irqrestore(lock, flags);
256 cancel_work_sync(&mdev->clear_work[channel].ws);
258 mutex_lock(&mdev->io_mutex);
259 usb_kill_anchored_urbs(&mdev->busy_urbs[channel]);
260 if (mdev->padding_active[channel])
261 mdev->padding_active[channel] = false;
263 if (mdev->conf[channel].data_type == MOST_CH_ASYNC) {
264 del_timer_sync(&mdev->link_stat_timer);
265 cancel_work_sync(&mdev->poll_work_obj);
267 mutex_unlock(&mdev->io_mutex);
272 * hdm_add_padding - add padding bytes
274 * @channel: channel ID
275 * @mbo: buffer object
277 * This inserts the INIC hardware specific padding bytes into a streaming
280 static int hdm_add_padding(struct most_dev *mdev, int channel, struct mbo *mbo)
282 struct most_channel_config *conf = &mdev->conf[channel];
283 unsigned int frame_size = get_stream_frame_size(conf);
284 unsigned int j, num_frames;
288 num_frames = mbo->buffer_length / frame_size;
290 if (num_frames < 1) {
291 dev_err(&mdev->usb_device->dev,
292 "Missed minimal transfer unit.\n");
296 for (j = num_frames - 1; j > 0; j--)
297 memmove(mbo->virt_address + j * USB_MTU,
298 mbo->virt_address + j * frame_size,
300 mbo->buffer_length = num_frames * USB_MTU;
305 * hdm_remove_padding - remove padding bytes
307 * @channel: channel ID
308 * @mbo: buffer object
310 * This takes the INIC hardware specific padding bytes off a streaming
313 static int hdm_remove_padding(struct most_dev *mdev, int channel,
316 struct most_channel_config *const conf = &mdev->conf[channel];
317 unsigned int frame_size = get_stream_frame_size(conf);
318 unsigned int j, num_frames;
322 num_frames = mbo->processed_length / USB_MTU;
324 for (j = 1; j < num_frames; j++)
325 memmove(mbo->virt_address + frame_size * j,
326 mbo->virt_address + USB_MTU * j,
329 mbo->processed_length = frame_size * num_frames;
334 * hdm_write_completion - completion function for submitted Tx URBs
335 * @urb: the URB that has been completed
337 * This checks the status of the completed URB. In case the URB has been
338 * unlinked before, it is immediately freed. On any other error the MBO
339 * transfer flag is set. On success it frees allocated resources and calls
340 * the completion function.
342 * Context: interrupt!
344 static void hdm_write_completion(struct urb *urb)
346 struct mbo *mbo = urb->context;
347 struct most_dev *mdev = to_mdev(mbo->ifp);
348 unsigned int channel = mbo->hdm_channel_id;
349 struct device *dev = &mdev->usb_device->dev;
350 spinlock_t *lock = mdev->channel_lock + channel;
353 spin_lock_irqsave(lock, flags);
355 mbo->processed_length = 0;
356 mbo->status = MBO_E_INVAL;
357 if (likely(mdev->is_channel_healthy[channel])) {
358 switch (urb->status) {
361 mbo->processed_length = urb->actual_length;
362 mbo->status = MBO_SUCCESS;
365 dev_warn(dev, "Broken OUT pipe detected\n");
366 mdev->is_channel_healthy[channel] = false;
367 mdev->clear_work[channel].pipe = urb->pipe;
368 schedule_work(&mdev->clear_work[channel].ws);
372 mbo->status = MBO_E_CLOSE;
377 spin_unlock_irqrestore(lock, flags);
379 if (likely(mbo->complete))
385 * hdm_read_completion - completion function for submitted Rx URBs
386 * @urb: the URB that has been completed
388 * This checks the status of the completed URB. In case the URB has been
389 * unlinked before it is immediately freed. On any other error the MBO transfer
390 * flag is set. On success it frees allocated resources, removes
391 * padding bytes -if necessary- and calls the completion function.
393 * Context: interrupt!
395 * **************************************************************************
396 * Error codes returned by in urb->status
397 * or in iso_frame_desc[n].status (for ISO)
398 * *************************************************************************
400 * USB device drivers may only test urb status values in completion handlers.
401 * This is because otherwise there would be a race between HCDs updating
402 * these values on one CPU, and device drivers testing them on another CPU.
404 * A transfer's actual_length may be positive even when an error has been
405 * reported. That's because transfers often involve several packets, so that
406 * one or more packets could finish before an error stops further endpoint I/O.
408 * For isochronous URBs, the urb status value is non-zero only if the URB is
409 * unlinked, the device is removed, the host controller is disabled or the total
410 * transferred length is less than the requested length and the URB_SHORT_NOT_OK
411 * flag is set. Completion handlers for isochronous URBs should only see
412 * urb->status set to zero, -ENOENT, -ECONNRESET, -ESHUTDOWN, or -EREMOTEIO.
413 * Individual frame descriptor status fields may report more status codes.
416 * 0 Transfer completed successfully
418 * -ENOENT URB was synchronously unlinked by usb_unlink_urb
420 * -EINPROGRESS URB still pending, no results yet
421 * (That is, if drivers see this it's a bug.)
423 * -EPROTO (*, **) a) bitstuff error
424 * b) no response packet received within the
425 * prescribed bus turn-around time
426 * c) unknown USB error
428 * -EILSEQ (*, **) a) CRC mismatch
429 * b) no response packet received within the
430 * prescribed bus turn-around time
431 * c) unknown USB error
433 * Note that often the controller hardware does not
434 * distinguish among cases a), b), and c), so a
435 * driver cannot tell whether there was a protocol
436 * error, a failure to respond (often caused by
437 * device disconnect), or some other fault.
439 * -ETIME (**) No response packet received within the prescribed
440 * bus turn-around time. This error may instead be
441 * reported as -EPROTO or -EILSEQ.
443 * -ETIMEDOUT Synchronous USB message functions use this code
444 * to indicate timeout expired before the transfer
445 * completed, and no other error was reported by HC.
447 * -EPIPE (**) Endpoint stalled. For non-control endpoints,
448 * reset this status with usb_clear_halt().
450 * -ECOMM During an IN transfer, the host controller
451 * received data from an endpoint faster than it
452 * could be written to system memory
454 * -ENOSR During an OUT transfer, the host controller
455 * could not retrieve data from system memory fast
456 * enough to keep up with the USB data rate
458 * -EOVERFLOW (*) The amount of data returned by the endpoint was
459 * greater than either the max packet size of the
460 * endpoint or the remaining buffer size. "Babble".
462 * -EREMOTEIO The data read from the endpoint did not fill the
463 * specified buffer, and URB_SHORT_NOT_OK was set in
464 * urb->transfer_flags.
466 * -ENODEV Device was removed. Often preceded by a burst of
467 * other errors, since the hub driver doesn't detect
468 * device removal events immediately.
470 * -EXDEV ISO transfer only partially completed
471 * (only set in iso_frame_desc[n].status, not urb->status)
473 * -EINVAL ISO madness, if this happens: Log off and go home
475 * -ECONNRESET URB was asynchronously unlinked by usb_unlink_urb
477 * -ESHUTDOWN The device or host controller has been disabled due
478 * to some problem that could not be worked around,
479 * such as a physical disconnect.
482 * (*) Error codes like -EPROTO, -EILSEQ and -EOVERFLOW normally indicate
483 * hardware problems such as bad devices (including firmware) or cables.
485 * (**) This is also one of several codes that different kinds of host
486 * controller use to indicate a transfer has failed because of device
487 * disconnect. In the interval before the hub driver starts disconnect
488 * processing, devices may receive such fault reports for every request.
490 * See <https://www.kernel.org/doc/Documentation/driver-api/usb/error-codes.rst>
492 static void hdm_read_completion(struct urb *urb)
494 struct mbo *mbo = urb->context;
495 struct most_dev *mdev = to_mdev(mbo->ifp);
496 unsigned int channel = mbo->hdm_channel_id;
497 struct device *dev = &mdev->usb_device->dev;
498 spinlock_t *lock = mdev->channel_lock + channel;
501 spin_lock_irqsave(lock, flags);
503 mbo->processed_length = 0;
504 mbo->status = MBO_E_INVAL;
505 if (likely(mdev->is_channel_healthy[channel])) {
506 switch (urb->status) {
509 mbo->processed_length = urb->actual_length;
510 mbo->status = MBO_SUCCESS;
511 if (mdev->padding_active[channel] &&
512 hdm_remove_padding(mdev, channel, mbo)) {
513 mbo->processed_length = 0;
514 mbo->status = MBO_E_INVAL;
518 dev_warn(dev, "Broken IN pipe detected\n");
519 mdev->is_channel_healthy[channel] = false;
520 mdev->clear_work[channel].pipe = urb->pipe;
521 schedule_work(&mdev->clear_work[channel].ws);
525 mbo->status = MBO_E_CLOSE;
528 dev_warn(dev, "Babble on IN pipe detected\n");
533 spin_unlock_irqrestore(lock, flags);
535 if (likely(mbo->complete))
541 * hdm_enqueue - receive a buffer to be used for data transfer
542 * @iface: interface to enqueue to
543 * @channel: ID of the channel
544 * @mbo: pointer to the buffer object
546 * This allocates a new URB and fills it according to the channel
547 * that is being used for transmission of data. Before the URB is
548 * submitted it is stored in the private anchor list.
550 * Returns 0 on success. On any error the URB is freed and a error code
553 * Context: Could in _some_ cases be interrupt!
555 static int hdm_enqueue(struct most_interface *iface, int channel,
558 struct most_dev *mdev;
559 struct most_channel_config *conf;
563 unsigned long length;
566 if (unlikely(!iface || !mbo))
568 if (unlikely(iface->num_channels <= channel || channel < 0))
571 mdev = to_mdev(iface);
572 conf = &mdev->conf[channel];
573 dev = &mdev->usb_device->dev;
575 if (!mdev->usb_device)
578 urb = usb_alloc_urb(NO_ISOCHRONOUS_URB, GFP_ATOMIC);
582 if ((conf->direction & MOST_CH_TX) && mdev->padding_active[channel] &&
583 hdm_add_padding(mdev, channel, mbo)) {
588 urb->transfer_dma = mbo->bus_address;
589 virt_address = mbo->virt_address;
590 length = mbo->buffer_length;
592 if (conf->direction & MOST_CH_TX) {
593 usb_fill_bulk_urb(urb, mdev->usb_device,
594 usb_sndbulkpipe(mdev->usb_device,
595 mdev->ep_address[channel]),
598 hdm_write_completion,
600 if (conf->data_type != MOST_CH_ISOC)
601 urb->transfer_flags |= URB_ZERO_PACKET;
603 usb_fill_bulk_urb(urb, mdev->usb_device,
604 usb_rcvbulkpipe(mdev->usb_device,
605 mdev->ep_address[channel]),
607 length + conf->extra_len,
611 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
613 usb_anchor_urb(urb, &mdev->busy_urbs[channel]);
615 retval = usb_submit_urb(urb, GFP_KERNEL);
617 dev_err(dev, "URB submit failed with error %d.\n", retval);
623 usb_unanchor_urb(urb);
630 * hdm_configure_channel - receive channel configuration from core
632 * @channel: channel ID
633 * @conf: structure that holds the configuration information
635 * The attached network interface controller (NIC) supports a padding mode
636 * to avoid short packets on USB, hence increasing the performance due to a
637 * lower interrupt load. This mode is default for synchronous data and can
638 * be switched on for isochronous data. In case padding is active the
639 * driver needs to know the frame size of the payload in order to calculate
640 * the number of bytes it needs to pad when transmitting or to cut off when
644 static int hdm_configure_channel(struct most_interface *iface, int channel,
645 struct most_channel_config *conf)
647 unsigned int num_frames;
648 unsigned int frame_size;
649 struct most_dev *mdev = to_mdev(iface);
650 struct device *dev = &mdev->usb_device->dev;
652 mdev->is_channel_healthy[channel] = true;
653 mdev->clear_work[channel].channel = channel;
654 mdev->clear_work[channel].mdev = mdev;
655 INIT_WORK(&mdev->clear_work[channel].ws, wq_clear_halt);
657 if (unlikely(!iface || !conf)) {
658 dev_err(dev, "Bad interface or config pointer.\n");
661 if (unlikely(channel < 0 || channel >= iface->num_channels)) {
662 dev_err(dev, "Channel ID out of range.\n");
665 if (!conf->num_buffers || !conf->buffer_size) {
666 dev_err(dev, "Misconfig: buffer size or #buffers zero.\n");
670 if (conf->data_type != MOST_CH_SYNC &&
671 !(conf->data_type == MOST_CH_ISOC &&
672 conf->packets_per_xact != 0xFF)) {
673 mdev->padding_active[channel] = false;
675 * Since the NIC's padding mode is not going to be
676 * used, we can skip the frame size calculations and
677 * move directly on to exit.
682 mdev->padding_active[channel] = true;
684 frame_size = get_stream_frame_size(conf);
685 if (frame_size == 0 || frame_size > USB_MTU) {
686 dev_warn(dev, "Misconfig: frame size wrong\n");
690 num_frames = conf->buffer_size / frame_size;
692 if (conf->buffer_size % frame_size) {
693 u16 old_size = conf->buffer_size;
695 conf->buffer_size = num_frames * frame_size;
696 dev_warn(dev, "%s: fixed buffer size (%d -> %d)\n",
697 mdev->suffix[channel], old_size, conf->buffer_size);
700 /* calculate extra length to comply w/ HW padding */
701 conf->extra_len = num_frames * (USB_MTU - frame_size);
704 mdev->conf[channel] = *conf;
705 if (conf->data_type == MOST_CH_ASYNC) {
706 u16 ep = mdev->ep_address[channel];
708 if (start_sync_ep(mdev->usb_device, ep) < 0)
709 dev_warn(dev, "sync for ep%02x failed", ep);
715 * hdm_request_netinfo - request network information
716 * @iface: pointer to interface
717 * @channel: channel ID
719 * This is used as trigger to set up the link status timer that
720 * polls for the NI state of the INIC every 2 seconds.
723 static void hdm_request_netinfo(struct most_interface *iface, int channel,
724 void (*on_netinfo)(struct most_interface *,
728 struct most_dev *mdev;
731 mdev = to_mdev(iface);
732 mdev->on_netinfo = on_netinfo;
736 mdev->link_stat_timer.expires = jiffies + HZ;
737 mod_timer(&mdev->link_stat_timer, mdev->link_stat_timer.expires);
741 * link_stat_timer_handler - schedule work obtaining mac address and link status
742 * @data: pointer to USB device instance
744 * The handler runs in interrupt context. That's why we need to defer the
745 * tasks to a work queue.
747 static void link_stat_timer_handler(unsigned long data)
749 struct most_dev *mdev = (struct most_dev *)data;
751 schedule_work(&mdev->poll_work_obj);
752 mdev->link_stat_timer.expires = jiffies + (2 * HZ);
753 add_timer(&mdev->link_stat_timer);
757 * wq_netinfo - work queue function to deliver latest networking information
758 * @wq_obj: object that holds data for our deferred work to do
760 * This retrieves the network interface status of the USB INIC
762 static void wq_netinfo(struct work_struct *wq_obj)
764 struct most_dev *mdev = to_mdev_from_work(wq_obj);
765 struct usb_device *usb_device = mdev->usb_device;
766 struct device *dev = &usb_device->dev;
767 u16 hi, mi, lo, link;
770 if (drci_rd_reg(usb_device, DRCI_REG_HW_ADDR_HI, &hi) < 0) {
771 dev_err(dev, "Vendor request 'hw_addr_hi' failed\n");
775 if (drci_rd_reg(usb_device, DRCI_REG_HW_ADDR_MI, &mi) < 0) {
776 dev_err(dev, "Vendor request 'hw_addr_mid' failed\n");
780 if (drci_rd_reg(usb_device, DRCI_REG_HW_ADDR_LO, &lo) < 0) {
781 dev_err(dev, "Vendor request 'hw_addr_low' failed\n");
785 if (drci_rd_reg(usb_device, DRCI_REG_NI_STATE, &link) < 0) {
786 dev_err(dev, "Vendor request 'link status' failed\n");
790 hw_addr[0] = hi >> 8;
792 hw_addr[2] = mi >> 8;
794 hw_addr[4] = lo >> 8;
797 if (mdev->on_netinfo)
798 mdev->on_netinfo(&mdev->iface, link, hw_addr);
802 * wq_clear_halt - work queue function
803 * @wq_obj: work_struct object to execute
805 * This sends a clear_halt to the given USB pipe.
807 static void wq_clear_halt(struct work_struct *wq_obj)
809 struct clear_hold_work *clear_work = to_clear_hold_work(wq_obj);
810 struct most_dev *mdev = clear_work->mdev;
811 unsigned int channel = clear_work->channel;
812 int pipe = clear_work->pipe;
814 mutex_lock(&mdev->io_mutex);
815 most_stop_enqueue(&mdev->iface, channel);
816 usb_kill_anchored_urbs(&mdev->busy_urbs[channel]);
817 if (usb_clear_halt(mdev->usb_device, pipe))
818 dev_warn(&mdev->usb_device->dev, "Failed to reset endpoint.\n");
820 mdev->is_channel_healthy[channel] = true;
821 most_resume_enqueue(&mdev->iface, channel);
822 mutex_unlock(&mdev->io_mutex);
826 * hdm_usb_fops - file operation table for USB driver
828 static const struct file_operations hdm_usb_fops = {
829 .owner = THIS_MODULE,
833 * usb_device_id - ID table for HCD device probing
835 static const struct usb_device_id usbid[] = {
836 { USB_DEVICE(USB_VENDOR_ID_SMSC, USB_DEV_ID_BRDG), },
837 { USB_DEVICE(USB_VENDOR_ID_SMSC, USB_DEV_ID_OS81118), },
838 { USB_DEVICE(USB_VENDOR_ID_SMSC, USB_DEV_ID_OS81119), },
839 { USB_DEVICE(USB_VENDOR_ID_SMSC, USB_DEV_ID_OS81210), },
840 { } /* Terminating entry */
843 #define MOST_DCI_RO_ATTR(_name) \
844 struct most_dci_attribute most_dci_attr_##_name = \
845 __ATTR(_name, 0444, show_value, NULL)
847 #define MOST_DCI_ATTR(_name) \
848 struct most_dci_attribute most_dci_attr_##_name = \
849 __ATTR(_name, 0644, show_value, store_value)
851 #define MOST_DCI_WO_ATTR(_name) \
852 struct most_dci_attribute most_dci_attr_##_name = \
853 __ATTR(_name, 0200, NULL, store_value)
856 * struct most_dci_attribute - to access the attributes of a dci object
857 * @attr: attributes of a dci object
858 * @show: pointer to the show function
859 * @store: pointer to the store function
861 struct most_dci_attribute {
862 struct attribute attr;
863 ssize_t (*show)(struct most_dci_obj *d,
864 struct most_dci_attribute *attr,
866 ssize_t (*store)(struct most_dci_obj *d,
867 struct most_dci_attribute *attr,
872 #define to_dci_attr(a) container_of(a, struct most_dci_attribute, attr)
875 * dci_attr_show - show function for dci object
876 * @kobj: pointer to kobject
877 * @attr: pointer to attribute struct
880 static ssize_t dci_attr_show(struct kobject *kobj, struct attribute *attr,
883 struct most_dci_attribute *dci_attr = to_dci_attr(attr);
884 struct most_dci_obj *dci_obj = to_dci_obj(kobj);
889 return dci_attr->show(dci_obj, dci_attr, buf);
893 * dci_attr_store - store function for dci object
894 * @kobj: pointer to kobject
895 * @attr: pointer to attribute struct
897 * @len: length of buffer
899 static ssize_t dci_attr_store(struct kobject *kobj,
900 struct attribute *attr,
904 struct most_dci_attribute *dci_attr = to_dci_attr(attr);
905 struct most_dci_obj *dci_obj = to_dci_obj(kobj);
907 if (!dci_attr->store)
910 return dci_attr->store(dci_obj, dci_attr, buf, len);
913 static const struct sysfs_ops most_dci_sysfs_ops = {
914 .show = dci_attr_show,
915 .store = dci_attr_store,
919 * most_dci_release - release function for dci object
920 * @kobj: pointer to kobject
922 * This frees the memory allocated for the dci object
924 static void most_dci_release(struct kobject *kobj)
926 struct most_dci_obj *dci_obj = to_dci_obj(kobj);
936 static const struct regs ro_regs[] = {
937 { "ni_state", DRCI_REG_NI_STATE },
938 { "packet_bandwidth", DRCI_REG_PACKET_BW },
939 { "node_address", DRCI_REG_NODE_ADDR },
940 { "node_position", DRCI_REG_NODE_POS },
943 static const struct regs rw_regs[] = {
944 { "mep_filter", DRCI_REG_MEP_FILTER },
945 { "mep_hash0", DRCI_REG_HASH_TBL0 },
946 { "mep_hash1", DRCI_REG_HASH_TBL1 },
947 { "mep_hash2", DRCI_REG_HASH_TBL2 },
948 { "mep_hash3", DRCI_REG_HASH_TBL3 },
949 { "mep_eui48_hi", DRCI_REG_HW_ADDR_HI },
950 { "mep_eui48_mi", DRCI_REG_HW_ADDR_MI },
951 { "mep_eui48_lo", DRCI_REG_HW_ADDR_LO },
954 static int get_stat_reg_addr(const struct regs *regs, int size,
955 const char *name, u16 *reg_addr)
959 for (i = 0; i < size; i++) {
960 if (!strcmp(name, regs[i].name)) {
961 *reg_addr = regs[i].reg;
968 #define get_static_reg_addr(regs, name, reg_addr) \
969 get_stat_reg_addr(regs, ARRAY_SIZE(regs), name, reg_addr)
971 static ssize_t show_value(struct most_dci_obj *dci_obj,
972 struct most_dci_attribute *attr, char *buf)
974 const char *name = attr->attr.name;
979 if (!strcmp(name, "arb_address"))
980 return snprintf(buf, PAGE_SIZE, "%04x\n", dci_obj->reg_addr);
982 if (!strcmp(name, "arb_value"))
983 reg_addr = dci_obj->reg_addr;
984 else if (get_static_reg_addr(ro_regs, name, ®_addr) &&
985 get_static_reg_addr(rw_regs, name, ®_addr))
988 err = drci_rd_reg(dci_obj->usb_device, reg_addr, &val);
992 return snprintf(buf, PAGE_SIZE, "%04x\n", val);
995 static ssize_t store_value(struct most_dci_obj *dci_obj,
996 struct most_dci_attribute *attr,
997 const char *buf, size_t count)
1001 const char *name = attr->attr.name;
1002 struct usb_device *usb_dev = dci_obj->usb_device;
1003 int err = kstrtou16(buf, 16, &val);
1008 if (!strcmp(name, "arb_address")) {
1009 dci_obj->reg_addr = val;
1013 if (!strcmp(name, "arb_value"))
1014 err = drci_wr_reg(usb_dev, dci_obj->reg_addr, val);
1015 else if (!strcmp(name, "sync_ep"))
1016 err = start_sync_ep(usb_dev, val);
1017 else if (!get_static_reg_addr(rw_regs, name, ®_addr))
1018 err = drci_wr_reg(usb_dev, reg_addr, val);
1028 static MOST_DCI_RO_ATTR(ni_state);
1029 static MOST_DCI_RO_ATTR(packet_bandwidth);
1030 static MOST_DCI_RO_ATTR(node_address);
1031 static MOST_DCI_RO_ATTR(node_position);
1032 static MOST_DCI_WO_ATTR(sync_ep);
1033 static MOST_DCI_ATTR(mep_filter);
1034 static MOST_DCI_ATTR(mep_hash0);
1035 static MOST_DCI_ATTR(mep_hash1);
1036 static MOST_DCI_ATTR(mep_hash2);
1037 static MOST_DCI_ATTR(mep_hash3);
1038 static MOST_DCI_ATTR(mep_eui48_hi);
1039 static MOST_DCI_ATTR(mep_eui48_mi);
1040 static MOST_DCI_ATTR(mep_eui48_lo);
1041 static MOST_DCI_ATTR(arb_address);
1042 static MOST_DCI_ATTR(arb_value);
1045 * most_dci_def_attrs - array of default attribute files of the dci object
1047 static struct attribute *most_dci_def_attrs[] = {
1048 &most_dci_attr_ni_state.attr,
1049 &most_dci_attr_packet_bandwidth.attr,
1050 &most_dci_attr_node_address.attr,
1051 &most_dci_attr_node_position.attr,
1052 &most_dci_attr_sync_ep.attr,
1053 &most_dci_attr_mep_filter.attr,
1054 &most_dci_attr_mep_hash0.attr,
1055 &most_dci_attr_mep_hash1.attr,
1056 &most_dci_attr_mep_hash2.attr,
1057 &most_dci_attr_mep_hash3.attr,
1058 &most_dci_attr_mep_eui48_hi.attr,
1059 &most_dci_attr_mep_eui48_mi.attr,
1060 &most_dci_attr_mep_eui48_lo.attr,
1061 &most_dci_attr_arb_address.attr,
1062 &most_dci_attr_arb_value.attr,
1069 static struct kobj_type most_dci_ktype = {
1070 .sysfs_ops = &most_dci_sysfs_ops,
1071 .release = most_dci_release,
1072 .default_attrs = most_dci_def_attrs,
1076 * create_most_dci_obj - allocates a dci object
1077 * @parent: parent kobject
1079 * This creates a dci object and registers it with sysfs.
1080 * Returns a pointer to the object or NULL when something went wrong.
1083 most_dci_obj *create_most_dci_obj(struct kobject *parent)
1085 struct most_dci_obj *most_dci = kzalloc(sizeof(*most_dci), GFP_KERNEL);
1091 retval = kobject_init_and_add(&most_dci->kobj, &most_dci_ktype, parent,
1094 kobject_put(&most_dci->kobj);
1101 * destroy_most_dci_obj - DCI object release function
1102 * @p: pointer to dci object
1104 static void destroy_most_dci_obj(struct most_dci_obj *p)
1106 kobject_put(&p->kobj);
1110 * hdm_probe - probe function of USB device driver
1111 * @interface: Interface of the attached USB device
1112 * @id: Pointer to the USB ID table.
1114 * This allocates and initializes the device instance, adds the new
1115 * entry to the internal list, scans the USB descriptors and registers
1116 * the interface with the core.
1117 * Additionally, the DCI objects are created and the hardware is sync'd.
1119 * Return 0 on success. In case of an error a negative number is returned.
1122 hdm_probe(struct usb_interface *interface, const struct usb_device_id *id)
1124 struct usb_host_interface *usb_iface_desc = interface->cur_altsetting;
1125 struct usb_device *usb_dev = interface_to_usbdev(interface);
1126 struct device *dev = &usb_dev->dev;
1127 struct most_dev *mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
1129 unsigned int num_endpoints;
1130 struct most_channel_capability *tmp_cap;
1131 struct usb_endpoint_descriptor *ep_desc;
1137 usb_set_intfdata(interface, mdev);
1138 num_endpoints = usb_iface_desc->desc.bNumEndpoints;
1139 mutex_init(&mdev->io_mutex);
1140 INIT_WORK(&mdev->poll_work_obj, wq_netinfo);
1141 setup_timer(&mdev->link_stat_timer, link_stat_timer_handler,
1142 (unsigned long)mdev);
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");