2 * hdm_usb.c - Hardware dependent module for USB
4 * Copyright (C) 2013-2015 Microchip Technology Germany II GmbH & Co. KG
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
11 * This file is licensed under GPLv2.
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/module.h>
17 #include <linux/usb.h>
18 #include <linux/slab.h>
19 #include <linux/init.h>
20 #include <linux/cdev.h>
21 #include <linux/device.h>
22 #include <linux/list.h>
23 #include <linux/completion.h>
24 #include <linux/mutex.h>
25 #include <linux/spinlock.h>
26 #include <linux/interrupt.h>
27 #include <linux/workqueue.h>
28 #include <linux/sysfs.h>
29 #include <linux/dma-mapping.h>
30 #include <linux/etherdevice.h>
31 #include <linux/uaccess.h>
33 #include "networking.h"
36 #define NO_ISOCHRONOUS_URB 0
37 #define AV_PACKETS_PER_XACT 2
38 #define BUF_CHAIN_SIZE 0xFFFF
39 #define MAX_NUM_ENDPOINTS 30
40 #define MAX_SUFFIX_LEN 10
41 #define MAX_STRING_LEN 80
42 #define MAX_BUF_SIZE 0xFFFF
44 #define USB_VENDOR_ID_SMSC 0x0424 /* VID: SMSC */
45 #define USB_DEV_ID_BRDG 0xC001 /* PID: USB Bridge */
46 #define USB_DEV_ID_OS81118 0xCF18 /* PID: USB OS81118 */
47 #define USB_DEV_ID_OS81119 0xCF19 /* PID: USB OS81119 */
48 #define USB_DEV_ID_OS81210 0xCF30 /* PID: USB OS81210 */
50 #define DRCI_REG_NI_STATE 0x0100
51 #define DRCI_REG_PACKET_BW 0x0101
52 #define DRCI_REG_NODE_ADDR 0x0102
53 #define DRCI_REG_NODE_POS 0x0103
54 #define DRCI_REG_MEP_FILTER 0x0140
55 #define DRCI_REG_HASH_TBL0 0x0141
56 #define DRCI_REG_HASH_TBL1 0x0142
57 #define DRCI_REG_HASH_TBL2 0x0143
58 #define DRCI_REG_HASH_TBL3 0x0144
59 #define DRCI_REG_HW_ADDR_HI 0x0145
60 #define DRCI_REG_HW_ADDR_MI 0x0146
61 #define DRCI_REG_HW_ADDR_LO 0x0147
62 #define DRCI_REG_BASE 0x1100
63 #define DRCI_COMMAND 0x02
64 #define DRCI_READ_REQ 0xA0
65 #define DRCI_WRITE_REQ 0xA1
68 * struct most_dci_obj - Direct Communication Interface
69 * @kobj:position in sysfs
70 * @usb_device: pointer to the usb device
71 * @reg_addr: register address for arbitrary DCI access
75 struct usb_device *usb_device;
79 #define to_dci_obj(p) container_of(p, struct most_dci_obj, kobj)
83 struct clear_hold_work {
84 struct work_struct ws;
85 struct most_dev *mdev;
90 #define to_clear_hold_work(w) container_of(w, struct clear_hold_work, ws)
93 * struct most_dev - holds all usb interface specific stuff
94 * @parent: parent object in sysfs
95 * @usb_device: pointer to usb device
96 * @iface: hardware interface
97 * @cap: channel capabilities
98 * @conf: channel configuration
99 * @dci: direct communication interface of hardware
100 * @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;
131 #define to_mdev(d) container_of(d, struct most_dev, iface)
132 #define to_mdev_from_work(w) container_of(w, struct most_dev, poll_work_obj)
134 static void wq_clear_halt(struct work_struct *wq_obj);
135 static void wq_netinfo(struct work_struct *wq_obj);
138 * drci_rd_reg - read a DCI register
140 * @reg: register address
141 * @buf: buffer to store data
143 * This is reads data from INIC's direct register communication interface
145 static inline int drci_rd_reg(struct usb_device *dev, u16 reg, u16 *buf)
148 __le16 *dma_buf = kzalloc(sizeof(*dma_buf), GFP_KERNEL);
149 u8 req_type = USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
154 retval = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
155 DRCI_READ_REQ, req_type,
157 reg, dma_buf, sizeof(*dma_buf), 5 * HZ);
158 *buf = le16_to_cpu(*dma_buf);
165 * drci_wr_reg - write a DCI register
167 * @reg: register address
168 * @data: data to write
170 * This is writes data to INIC's direct register communication interface
172 static inline int drci_wr_reg(struct usb_device *dev, u16 reg, u16 data)
174 return usb_control_msg(dev,
175 usb_sndctrlpipe(dev, 0),
177 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
185 static inline int start_sync_ep(struct usb_device *usb_dev, u16 ep)
187 return drci_wr_reg(usb_dev, DRCI_REG_BASE + DRCI_COMMAND + ep * 16, 1);
191 * get_stream_frame_size - calculate frame size of current configuration
192 * @cfg: channel configuration
194 static unsigned int get_stream_frame_size(struct most_channel_config *cfg)
196 unsigned int frame_size = 0;
197 unsigned int sub_size = cfg->subbuffer_size;
200 pr_warn("Misconfig: Subbuffer size zero.\n");
203 switch (cfg->data_type) {
205 frame_size = AV_PACKETS_PER_XACT * sub_size;
208 if (cfg->packets_per_xact == 0) {
209 pr_warn("Misconfig: Packets per XACT zero\n");
211 } else if (cfg->packets_per_xact == 0xFF) {
212 frame_size = (USB_MTU / sub_size) * sub_size;
214 frame_size = cfg->packets_per_xact * sub_size;
218 pr_warn("Query frame size of non-streaming channel\n");
225 * hdm_poison_channel - mark buffers of this channel as invalid
226 * @iface: pointer to the interface
227 * @channel: channel ID
229 * This unlinks all URBs submitted to the HCD,
230 * calls the associated completion function of the core and removes
231 * them from the list.
233 * Returns 0 on success or error code otherwise.
235 static int hdm_poison_channel(struct most_interface *iface, int channel)
237 struct most_dev *mdev = to_mdev(iface);
239 spinlock_t *lock; /* temp. lock */
241 if (unlikely(!iface)) {
242 dev_warn(&mdev->usb_device->dev, "Poison: Bad interface.\n");
245 if (unlikely(channel < 0 || channel >= iface->num_channels)) {
246 dev_warn(&mdev->usb_device->dev, "Channel ID out of range.\n");
250 lock = mdev->channel_lock + channel;
251 spin_lock_irqsave(lock, flags);
252 mdev->is_channel_healthy[channel] = false;
253 spin_unlock_irqrestore(lock, flags);
255 cancel_work_sync(&mdev->clear_work[channel].ws);
257 mutex_lock(&mdev->io_mutex);
258 usb_kill_anchored_urbs(&mdev->busy_urbs[channel]);
259 if (mdev->padding_active[channel])
260 mdev->padding_active[channel] = false;
262 if (mdev->conf[channel].data_type == MOST_CH_ASYNC) {
263 del_timer_sync(&mdev->link_stat_timer);
264 cancel_work_sync(&mdev->poll_work_obj);
266 mutex_unlock(&mdev->io_mutex);
271 * hdm_add_padding - add padding bytes
273 * @channel: channel ID
274 * @mbo: buffer object
276 * This inserts the INIC hardware specific padding bytes into a streaming
279 static int hdm_add_padding(struct most_dev *mdev, int channel, struct mbo *mbo)
281 struct most_channel_config *conf = &mdev->conf[channel];
282 unsigned int frame_size = get_stream_frame_size(conf);
283 unsigned int j, num_frames;
284 u16 rd_addr, wr_addr;
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 = 1; j < num_frames; j++) {
297 wr_addr = (num_frames - j) * USB_MTU;
298 rd_addr = (num_frames - j) * frame_size;
299 memmove(mbo->virt_address + wr_addr,
300 mbo->virt_address + rd_addr,
303 mbo->buffer_length = num_frames * USB_MTU;
308 * hdm_remove_padding - remove padding bytes
310 * @channel: channel ID
311 * @mbo: buffer object
313 * This takes the INIC hardware specific padding bytes off a streaming
316 static int hdm_remove_padding(struct most_dev *mdev, int channel,
319 struct most_channel_config *const conf = &mdev->conf[channel];
320 unsigned int frame_size = get_stream_frame_size(conf);
321 unsigned int j, num_frames;
325 num_frames = mbo->processed_length / USB_MTU;
327 for (j = 1; j < num_frames; j++)
328 memmove(mbo->virt_address + frame_size * j,
329 mbo->virt_address + USB_MTU * j,
332 mbo->processed_length = frame_size * num_frames;
337 * hdm_write_completion - completion function for submitted Tx URBs
338 * @urb: the URB that has been completed
340 * This checks the status of the completed URB. In case the URB has been
341 * unlinked before, it is immediately freed. On any other error the MBO
342 * transfer flag is set. On success it frees allocated resources and calls
343 * the completion function.
345 * Context: interrupt!
347 static void hdm_write_completion(struct urb *urb)
349 struct mbo *mbo = urb->context;
350 struct most_dev *mdev = to_mdev(mbo->ifp);
351 unsigned int channel = mbo->hdm_channel_id;
352 struct device *dev = &mdev->usb_device->dev;
353 spinlock_t *lock = mdev->channel_lock + channel;
356 spin_lock_irqsave(lock, flags);
358 mbo->processed_length = 0;
359 mbo->status = MBO_E_INVAL;
360 if (likely(mdev->is_channel_healthy[channel])) {
361 switch (urb->status) {
364 mbo->processed_length = urb->actual_length;
365 mbo->status = MBO_SUCCESS;
368 dev_warn(dev, "Broken OUT pipe detected\n");
369 mdev->is_channel_healthy[channel] = false;
370 mdev->clear_work[channel].pipe = urb->pipe;
371 schedule_work(&mdev->clear_work[channel].ws);
375 mbo->status = MBO_E_CLOSE;
380 spin_unlock_irqrestore(lock, flags);
382 if (likely(mbo->complete))
388 * hdm_read_completion - completion function for submitted Rx URBs
389 * @urb: the URB that has been completed
391 * This checks the status of the completed URB. In case the URB has been
392 * unlinked before it is immediately freed. On any other error the MBO transfer
393 * flag is set. On success it frees allocated resources, removes
394 * padding bytes -if necessary- and calls the completion function.
396 * Context: interrupt!
398 * **************************************************************************
399 * Error codes returned by in urb->status
400 * or in iso_frame_desc[n].status (for ISO)
401 * *************************************************************************
403 * USB device drivers may only test urb status values in completion handlers.
404 * This is because otherwise there would be a race between HCDs updating
405 * these values on one CPU, and device drivers testing them on another CPU.
407 * A transfer's actual_length may be positive even when an error has been
408 * reported. That's because transfers often involve several packets, so that
409 * one or more packets could finish before an error stops further endpoint I/O.
411 * For isochronous URBs, the urb status value is non-zero only if the URB is
412 * unlinked, the device is removed, the host controller is disabled or the total
413 * transferred length is less than the requested length and the URB_SHORT_NOT_OK
414 * flag is set. Completion handlers for isochronous URBs should only see
415 * urb->status set to zero, -ENOENT, -ECONNRESET, -ESHUTDOWN, or -EREMOTEIO.
416 * Individual frame descriptor status fields may report more status codes.
419 * 0 Transfer completed successfully
421 * -ENOENT URB was synchronously unlinked by usb_unlink_urb
423 * -EINPROGRESS URB still pending, no results yet
424 * (That is, if drivers see this it's a bug.)
426 * -EPROTO (*, **) a) bitstuff error
427 * b) no response packet received within the
428 * prescribed bus turn-around time
429 * c) unknown USB error
431 * -EILSEQ (*, **) a) CRC mismatch
432 * b) no response packet received within the
433 * prescribed bus turn-around time
434 * c) unknown USB error
436 * Note that often the controller hardware does not
437 * distinguish among cases a), b), and c), so a
438 * driver cannot tell whether there was a protocol
439 * error, a failure to respond (often caused by
440 * device disconnect), or some other fault.
442 * -ETIME (**) No response packet received within the prescribed
443 * bus turn-around time. This error may instead be
444 * reported as -EPROTO or -EILSEQ.
446 * -ETIMEDOUT Synchronous USB message functions use this code
447 * to indicate timeout expired before the transfer
448 * completed, and no other error was reported by HC.
450 * -EPIPE (**) Endpoint stalled. For non-control endpoints,
451 * reset this status with usb_clear_halt().
453 * -ECOMM During an IN transfer, the host controller
454 * received data from an endpoint faster than it
455 * could be written to system memory
457 * -ENOSR During an OUT transfer, the host controller
458 * could not retrieve data from system memory fast
459 * enough to keep up with the USB data rate
461 * -EOVERFLOW (*) The amount of data returned by the endpoint was
462 * greater than either the max packet size of the
463 * endpoint or the remaining buffer size. "Babble".
465 * -EREMOTEIO The data read from the endpoint did not fill the
466 * specified buffer, and URB_SHORT_NOT_OK was set in
467 * urb->transfer_flags.
469 * -ENODEV Device was removed. Often preceded by a burst of
470 * other errors, since the hub driver doesn't detect
471 * device removal events immediately.
473 * -EXDEV ISO transfer only partially completed
474 * (only set in iso_frame_desc[n].status, not urb->status)
476 * -EINVAL ISO madness, if this happens: Log off and go home
478 * -ECONNRESET URB was asynchronously unlinked by usb_unlink_urb
480 * -ESHUTDOWN The device or host controller has been disabled due
481 * to some problem that could not be worked around,
482 * such as a physical disconnect.
485 * (*) Error codes like -EPROTO, -EILSEQ and -EOVERFLOW normally indicate
486 * hardware problems such as bad devices (including firmware) or cables.
488 * (**) This is also one of several codes that different kinds of host
489 * controller use to indicate a transfer has failed because of device
490 * disconnect. In the interval before the hub driver starts disconnect
491 * processing, devices may receive such fault reports for every request.
493 * See <https://www.kernel.org/doc/Documentation/usb/error-codes.txt>
495 static void hdm_read_completion(struct urb *urb)
497 struct mbo *mbo = urb->context;
498 struct most_dev *mdev = to_mdev(mbo->ifp);
499 unsigned int channel = mbo->hdm_channel_id;
500 struct device *dev = &mdev->usb_device->dev;
501 spinlock_t *lock = mdev->channel_lock + channel;
504 spin_lock_irqsave(lock, flags);
506 mbo->processed_length = 0;
507 mbo->status = MBO_E_INVAL;
508 if (likely(mdev->is_channel_healthy[channel])) {
509 switch (urb->status) {
512 mbo->processed_length = urb->actual_length;
513 mbo->status = MBO_SUCCESS;
514 if (mdev->padding_active[channel] &&
515 hdm_remove_padding(mdev, channel, mbo)) {
516 mbo->processed_length = 0;
517 mbo->status = MBO_E_INVAL;
521 dev_warn(dev, "Broken IN pipe detected\n");
522 mdev->is_channel_healthy[channel] = false;
523 mdev->clear_work[channel].pipe = urb->pipe;
524 schedule_work(&mdev->clear_work[channel].ws);
528 mbo->status = MBO_E_CLOSE;
531 dev_warn(dev, "Babble on IN pipe detected\n");
536 spin_unlock_irqrestore(lock, flags);
538 if (likely(mbo->complete))
544 * hdm_enqueue - receive a buffer to be used for data transfer
545 * @iface: interface to enqueue to
546 * @channel: ID of the channel
547 * @mbo: pointer to the buffer object
549 * This allocates a new URB and fills it according to the channel
550 * that is being used for transmission of data. Before the URB is
551 * submitted it is stored in the private anchor list.
553 * Returns 0 on success. On any error the URB is freed and a error code
556 * Context: Could in _some_ cases be interrupt!
558 static int hdm_enqueue(struct most_interface *iface, int channel,
561 struct most_dev *mdev;
562 struct most_channel_config *conf;
566 unsigned long length;
569 if (unlikely(!iface || !mbo))
571 if (unlikely(iface->num_channels <= channel || channel < 0))
574 mdev = to_mdev(iface);
575 conf = &mdev->conf[channel];
576 dev = &mdev->usb_device->dev;
578 if (!mdev->usb_device)
581 urb = usb_alloc_urb(NO_ISOCHRONOUS_URB, GFP_ATOMIC);
585 if ((conf->direction & MOST_CH_TX) && mdev->padding_active[channel] &&
586 hdm_add_padding(mdev, channel, mbo)) {
591 urb->transfer_dma = mbo->bus_address;
592 virt_address = mbo->virt_address;
593 length = mbo->buffer_length;
595 if (conf->direction & MOST_CH_TX) {
596 usb_fill_bulk_urb(urb, mdev->usb_device,
597 usb_sndbulkpipe(mdev->usb_device,
598 mdev->ep_address[channel]),
601 hdm_write_completion,
603 if (conf->data_type != MOST_CH_ISOC)
604 urb->transfer_flags |= URB_ZERO_PACKET;
606 usb_fill_bulk_urb(urb, mdev->usb_device,
607 usb_rcvbulkpipe(mdev->usb_device,
608 mdev->ep_address[channel]),
610 length + conf->extra_len,
614 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
616 usb_anchor_urb(urb, &mdev->busy_urbs[channel]);
618 retval = usb_submit_urb(urb, GFP_KERNEL);
620 dev_err(dev, "URB submit failed with error %d.\n", retval);
626 usb_unanchor_urb(urb);
633 * hdm_configure_channel - receive channel configuration from core
635 * @channel: channel ID
636 * @conf: structure that holds the configuration information
638 * The attached network interface controller (NIC) supports a padding mode
639 * to avoid short packets on USB, hence increasing the performance due to a
640 * lower interrupt load. This mode is default for synchronous data and can
641 * be switched on for isochronous data. In case padding is active the
642 * driver needs to know the frame size of the payload in order to calculate
643 * the number of bytes it needs to pad when transmitting or to cut off when
647 static int hdm_configure_channel(struct most_interface *iface, int channel,
648 struct most_channel_config *conf)
650 unsigned int num_frames;
651 unsigned int frame_size;
652 unsigned int temp_size;
653 unsigned int tail_space;
654 struct most_dev *mdev = to_mdev(iface);
655 struct device *dev = &mdev->usb_device->dev;
657 mdev->is_channel_healthy[channel] = true;
658 mdev->clear_work[channel].channel = channel;
659 mdev->clear_work[channel].mdev = mdev;
660 INIT_WORK(&mdev->clear_work[channel].ws, wq_clear_halt);
662 if (unlikely(!iface || !conf)) {
663 dev_err(dev, "Bad interface or config pointer.\n");
666 if (unlikely(channel < 0 || channel >= iface->num_channels)) {
667 dev_err(dev, "Channel ID out of range.\n");
670 if (!conf->num_buffers || !conf->buffer_size) {
671 dev_err(dev, "Misconfig: buffer size or #buffers zero.\n");
675 if (conf->data_type != MOST_CH_SYNC &&
676 !(conf->data_type == MOST_CH_ISOC &&
677 conf->packets_per_xact != 0xFF)) {
678 mdev->padding_active[channel] = false;
680 * Since the NIC's padding mode is not going to be
681 * used, we can skip the frame size calculations and
682 * move directly on to exit.
687 mdev->padding_active[channel] = true;
688 temp_size = conf->buffer_size;
690 frame_size = get_stream_frame_size(conf);
691 if (frame_size == 0 || frame_size > USB_MTU) {
692 dev_warn(dev, "Misconfig: frame size wrong\n");
696 if (conf->buffer_size % frame_size) {
699 tmp_val = conf->buffer_size / frame_size;
700 conf->buffer_size = tmp_val * frame_size;
702 "Channel %d - rounding buffer size to %d bytes, channel config says %d bytes\n",
708 num_frames = conf->buffer_size / frame_size;
709 tail_space = num_frames * (USB_MTU - frame_size);
710 temp_size += tail_space;
712 /* calculate extra length to comply w/ HW padding */
713 conf->extra_len = (DIV_ROUND_UP(temp_size, USB_MTU) * USB_MTU)
716 mdev->conf[channel] = *conf;
717 if (conf->data_type == MOST_CH_ASYNC) {
718 u16 ep = mdev->ep_address[channel];
720 if (start_sync_ep(mdev->usb_device, ep) < 0)
721 dev_warn(dev, "sync for ep%02x failed", ep);
727 * hdm_request_netinfo - request network information
728 * @iface: pointer to interface
729 * @channel: channel ID
731 * This is used as trigger to set up the link status timer that
732 * polls for the NI state of the INIC every 2 seconds.
735 static void hdm_request_netinfo(struct most_interface *iface, int channel)
737 struct most_dev *mdev;
740 mdev = to_mdev(iface);
741 mdev->link_stat_timer.expires = jiffies + HZ;
742 mod_timer(&mdev->link_stat_timer, mdev->link_stat_timer.expires);
746 * link_stat_timer_handler - schedule work obtaining mac address and link status
747 * @data: pointer to USB device instance
749 * The handler runs in interrupt context. That's why we need to defer the
750 * tasks to a work queue.
752 static void link_stat_timer_handler(unsigned long data)
754 struct most_dev *mdev = (struct most_dev *)data;
756 schedule_work(&mdev->poll_work_obj);
757 mdev->link_stat_timer.expires = jiffies + (2 * HZ);
758 add_timer(&mdev->link_stat_timer);
762 * wq_netinfo - work queue function to deliver latest networking information
763 * @wq_obj: object that holds data for our deferred work to do
765 * This retrieves the network interface status of the USB INIC
767 static void wq_netinfo(struct work_struct *wq_obj)
769 struct most_dev *mdev = to_mdev_from_work(wq_obj);
770 struct usb_device *usb_device = mdev->usb_device;
771 struct device *dev = &usb_device->dev;
772 u16 hi, mi, lo, link;
775 if (drci_rd_reg(usb_device, DRCI_REG_HW_ADDR_HI, &hi) < 0) {
776 dev_err(dev, "Vendor request 'hw_addr_hi' failed\n");
780 if (drci_rd_reg(usb_device, DRCI_REG_HW_ADDR_MI, &mi) < 0) {
781 dev_err(dev, "Vendor request 'hw_addr_mid' failed\n");
785 if (drci_rd_reg(usb_device, DRCI_REG_HW_ADDR_LO, &lo) < 0) {
786 dev_err(dev, "Vendor request 'hw_addr_low' failed\n");
790 if (drci_rd_reg(usb_device, DRCI_REG_NI_STATE, &link) < 0) {
791 dev_err(dev, "Vendor request 'link status' failed\n");
795 hw_addr[0] = hi >> 8;
797 hw_addr[2] = mi >> 8;
799 hw_addr[4] = lo >> 8;
802 most_deliver_netinfo(&mdev->iface, link, hw_addr);
806 * wq_clear_halt - work queue function
807 * @wq_obj: work_struct object to execute
809 * This sends a clear_halt to the given USB pipe.
811 static void wq_clear_halt(struct work_struct *wq_obj)
813 struct clear_hold_work *clear_work = to_clear_hold_work(wq_obj);
814 struct most_dev *mdev = clear_work->mdev;
815 unsigned int channel = clear_work->channel;
816 int pipe = clear_work->pipe;
818 mutex_lock(&mdev->io_mutex);
819 most_stop_enqueue(&mdev->iface, channel);
820 usb_kill_anchored_urbs(&mdev->busy_urbs[channel]);
821 if (usb_clear_halt(mdev->usb_device, pipe))
822 dev_warn(&mdev->usb_device->dev, "Failed to reset endpoint.\n");
824 mdev->is_channel_healthy[channel] = true;
825 most_resume_enqueue(&mdev->iface, channel);
826 mutex_unlock(&mdev->io_mutex);
830 * hdm_usb_fops - file operation table for USB driver
832 static const struct file_operations hdm_usb_fops = {
833 .owner = THIS_MODULE,
837 * usb_device_id - ID table for HCD device probing
839 static struct usb_device_id usbid[] = {
840 { USB_DEVICE(USB_VENDOR_ID_SMSC, USB_DEV_ID_BRDG), },
841 { USB_DEVICE(USB_VENDOR_ID_SMSC, USB_DEV_ID_OS81118), },
842 { USB_DEVICE(USB_VENDOR_ID_SMSC, USB_DEV_ID_OS81119), },
843 { USB_DEVICE(USB_VENDOR_ID_SMSC, USB_DEV_ID_OS81210), },
844 { } /* Terminating entry */
847 #define MOST_DCI_RO_ATTR(_name) \
848 struct most_dci_attribute most_dci_attr_##_name = \
849 __ATTR(_name, 0444, show_value, NULL)
851 #define MOST_DCI_ATTR(_name) \
852 struct most_dci_attribute most_dci_attr_##_name = \
853 __ATTR(_name, 0644, show_value, store_value)
855 #define MOST_DCI_WO_ATTR(_name) \
856 struct most_dci_attribute most_dci_attr_##_name = \
857 __ATTR(_name, 0200, NULL, store_value)
860 * struct most_dci_attribute - to access the attributes of a dci object
861 * @attr: attributes of a dci object
862 * @show: pointer to the show function
863 * @store: pointer to the store function
865 struct most_dci_attribute {
866 struct attribute attr;
867 ssize_t (*show)(struct most_dci_obj *d,
868 struct most_dci_attribute *attr,
870 ssize_t (*store)(struct most_dci_obj *d,
871 struct most_dci_attribute *attr,
876 #define to_dci_attr(a) container_of(a, struct most_dci_attribute, attr)
879 * dci_attr_show - show function for dci object
880 * @kobj: pointer to kobject
881 * @attr: pointer to attribute struct
884 static ssize_t dci_attr_show(struct kobject *kobj, struct attribute *attr,
887 struct most_dci_attribute *dci_attr = to_dci_attr(attr);
888 struct most_dci_obj *dci_obj = to_dci_obj(kobj);
893 return dci_attr->show(dci_obj, dci_attr, buf);
897 * dci_attr_store - store function for dci object
898 * @kobj: pointer to kobject
899 * @attr: pointer to attribute struct
901 * @len: length of buffer
903 static ssize_t dci_attr_store(struct kobject *kobj,
904 struct attribute *attr,
908 struct most_dci_attribute *dci_attr = to_dci_attr(attr);
909 struct most_dci_obj *dci_obj = to_dci_obj(kobj);
911 if (!dci_attr->store)
914 return dci_attr->store(dci_obj, dci_attr, buf, len);
917 static const struct sysfs_ops most_dci_sysfs_ops = {
918 .show = dci_attr_show,
919 .store = dci_attr_store,
923 * most_dci_release - release function for dci object
924 * @kobj: pointer to kobject
926 * This frees the memory allocated for the dci object
928 static void most_dci_release(struct kobject *kobj)
930 struct most_dci_obj *dci_obj = to_dci_obj(kobj);
940 static const struct regs ro_regs[] = {
941 { "ni_state", DRCI_REG_NI_STATE },
942 { "packet_bandwidth", DRCI_REG_PACKET_BW },
943 { "node_address", DRCI_REG_NODE_ADDR },
944 { "node_position", DRCI_REG_NODE_POS },
947 static const struct regs rw_regs[] = {
948 { "mep_filter", DRCI_REG_MEP_FILTER },
949 { "mep_hash0", DRCI_REG_HASH_TBL0 },
950 { "mep_hash1", DRCI_REG_HASH_TBL1 },
951 { "mep_hash2", DRCI_REG_HASH_TBL2 },
952 { "mep_hash3", DRCI_REG_HASH_TBL3 },
953 { "mep_eui48_hi", DRCI_REG_HW_ADDR_HI },
954 { "mep_eui48_mi", DRCI_REG_HW_ADDR_MI },
955 { "mep_eui48_lo", DRCI_REG_HW_ADDR_LO },
958 static int get_stat_reg_addr(const struct regs *regs, int size,
959 const char *name, u16 *reg_addr)
963 for (i = 0; i < size; i++) {
964 if (!strcmp(name, regs[i].name)) {
965 *reg_addr = regs[i].reg;
972 #define get_static_reg_addr(regs, name, reg_addr) \
973 get_stat_reg_addr(regs, ARRAY_SIZE(regs), name, reg_addr)
975 static ssize_t show_value(struct most_dci_obj *dci_obj,
976 struct most_dci_attribute *attr, char *buf)
978 const char *name = attr->attr.name;
983 if (!strcmp(name, "arb_address"))
984 return snprintf(buf, PAGE_SIZE, "%04x\n", dci_obj->reg_addr);
986 if (!strcmp(name, "arb_value"))
987 reg_addr = dci_obj->reg_addr;
988 else if (get_static_reg_addr(ro_regs, name, ®_addr) &&
989 get_static_reg_addr(rw_regs, name, ®_addr))
992 err = drci_rd_reg(dci_obj->usb_device, reg_addr, &val);
996 return snprintf(buf, PAGE_SIZE, "%04x\n", val);
999 static ssize_t store_value(struct most_dci_obj *dci_obj,
1000 struct most_dci_attribute *attr,
1001 const char *buf, size_t count)
1005 const char *name = attr->attr.name;
1006 struct usb_device *usb_dev = dci_obj->usb_device;
1007 int err = kstrtou16(buf, 16, &val);
1012 if (!strcmp(name, "arb_address")) {
1013 dci_obj->reg_addr = val;
1017 if (!strcmp(name, "arb_value"))
1018 err = drci_wr_reg(usb_dev, dci_obj->reg_addr, val);
1019 else if (!strcmp(name, "sync_ep"))
1020 err = start_sync_ep(usb_dev, val);
1021 else if (!get_static_reg_addr(ro_regs, name, ®_addr))
1022 err = drci_wr_reg(usb_dev, reg_addr, val);
1032 static MOST_DCI_RO_ATTR(ni_state);
1033 static MOST_DCI_RO_ATTR(packet_bandwidth);
1034 static MOST_DCI_RO_ATTR(node_address);
1035 static MOST_DCI_RO_ATTR(node_position);
1036 static MOST_DCI_WO_ATTR(sync_ep);
1037 static MOST_DCI_ATTR(mep_filter);
1038 static MOST_DCI_ATTR(mep_hash0);
1039 static MOST_DCI_ATTR(mep_hash1);
1040 static MOST_DCI_ATTR(mep_hash2);
1041 static MOST_DCI_ATTR(mep_hash3);
1042 static MOST_DCI_ATTR(mep_eui48_hi);
1043 static MOST_DCI_ATTR(mep_eui48_mi);
1044 static MOST_DCI_ATTR(mep_eui48_lo);
1045 static MOST_DCI_ATTR(arb_address);
1046 static MOST_DCI_ATTR(arb_value);
1049 * most_dci_def_attrs - array of default attribute files of the dci object
1051 static struct attribute *most_dci_def_attrs[] = {
1052 &most_dci_attr_ni_state.attr,
1053 &most_dci_attr_packet_bandwidth.attr,
1054 &most_dci_attr_node_address.attr,
1055 &most_dci_attr_node_position.attr,
1056 &most_dci_attr_sync_ep.attr,
1057 &most_dci_attr_mep_filter.attr,
1058 &most_dci_attr_mep_hash0.attr,
1059 &most_dci_attr_mep_hash1.attr,
1060 &most_dci_attr_mep_hash2.attr,
1061 &most_dci_attr_mep_hash3.attr,
1062 &most_dci_attr_mep_eui48_hi.attr,
1063 &most_dci_attr_mep_eui48_mi.attr,
1064 &most_dci_attr_mep_eui48_lo.attr,
1065 &most_dci_attr_arb_address.attr,
1066 &most_dci_attr_arb_value.attr,
1073 static struct kobj_type most_dci_ktype = {
1074 .sysfs_ops = &most_dci_sysfs_ops,
1075 .release = most_dci_release,
1076 .default_attrs = most_dci_def_attrs,
1080 * create_most_dci_obj - allocates a dci object
1081 * @parent: parent kobject
1083 * This creates a dci object and registers it with sysfs.
1084 * Returns a pointer to the object or NULL when something went wrong.
1087 most_dci_obj *create_most_dci_obj(struct kobject *parent)
1089 struct most_dci_obj *most_dci = kzalloc(sizeof(*most_dci), GFP_KERNEL);
1095 retval = kobject_init_and_add(&most_dci->kobj, &most_dci_ktype, parent,
1098 kobject_put(&most_dci->kobj);
1105 * destroy_most_dci_obj - DCI object release function
1106 * @p: pointer to dci object
1108 static void destroy_most_dci_obj(struct most_dci_obj *p)
1110 kobject_put(&p->kobj);
1114 * hdm_probe - probe function of USB device driver
1115 * @interface: Interface of the attached USB device
1116 * @id: Pointer to the USB ID table.
1118 * This allocates and initializes the device instance, adds the new
1119 * entry to the internal list, scans the USB descriptors and registers
1120 * the interface with the core.
1121 * Additionally, the DCI objects are created and the hardware is sync'd.
1123 * Return 0 on success. In case of an error a negative number is returned.
1126 hdm_probe(struct usb_interface *interface, const struct usb_device_id *id)
1128 struct usb_host_interface *usb_iface_desc = interface->cur_altsetting;
1129 struct usb_device *usb_dev = interface_to_usbdev(interface);
1130 struct device *dev = &usb_dev->dev;
1131 struct most_dev *mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
1133 unsigned int num_endpoints;
1134 struct most_channel_capability *tmp_cap;
1135 struct usb_endpoint_descriptor *ep_desc;
1141 usb_set_intfdata(interface, mdev);
1142 num_endpoints = usb_iface_desc->desc.bNumEndpoints;
1143 mutex_init(&mdev->io_mutex);
1144 INIT_WORK(&mdev->poll_work_obj, wq_netinfo);
1145 setup_timer(&mdev->link_stat_timer, link_stat_timer_handler,
1146 (unsigned long)mdev);
1148 mdev->usb_device = usb_dev;
1149 mdev->link_stat_timer.expires = jiffies + (2 * HZ);
1151 mdev->iface.mod = hdm_usb_fops.owner;
1152 mdev->iface.interface = ITYPE_USB;
1153 mdev->iface.configure = hdm_configure_channel;
1154 mdev->iface.request_netinfo = hdm_request_netinfo;
1155 mdev->iface.enqueue = hdm_enqueue;
1156 mdev->iface.poison_channel = hdm_poison_channel;
1157 mdev->iface.description = mdev->description;
1158 mdev->iface.num_channels = num_endpoints;
1160 snprintf(mdev->description, sizeof(mdev->description),
1161 "usb_device %d-%s:%d.%d",
1162 usb_dev->bus->busnum,
1164 usb_dev->config->desc.bConfigurationValue,
1165 usb_iface_desc->desc.bInterfaceNumber);
1167 mdev->conf = kcalloc(num_endpoints, sizeof(*mdev->conf), GFP_KERNEL);
1171 mdev->cap = kcalloc(num_endpoints, sizeof(*mdev->cap), GFP_KERNEL);
1175 mdev->iface.channel_vector = mdev->cap;
1176 mdev->iface.priv = NULL;
1179 kcalloc(num_endpoints, sizeof(*mdev->ep_address), GFP_KERNEL);
1180 if (!mdev->ep_address)
1184 kcalloc(num_endpoints, sizeof(*mdev->busy_urbs), GFP_KERNEL);
1185 if (!mdev->busy_urbs)
1188 tmp_cap = mdev->cap;
1189 for (i = 0; i < num_endpoints; i++) {
1190 ep_desc = &usb_iface_desc->endpoint[i].desc;
1191 mdev->ep_address[i] = ep_desc->bEndpointAddress;
1192 mdev->padding_active[i] = false;
1193 mdev->is_channel_healthy[i] = true;
1195 snprintf(&mdev->suffix[i][0], MAX_SUFFIX_LEN, "ep%02x",
1196 mdev->ep_address[i]);
1198 tmp_cap->name_suffix = &mdev->suffix[i][0];
1199 tmp_cap->buffer_size_packet = MAX_BUF_SIZE;
1200 tmp_cap->buffer_size_streaming = MAX_BUF_SIZE;
1201 tmp_cap->num_buffers_packet = BUF_CHAIN_SIZE;
1202 tmp_cap->num_buffers_streaming = BUF_CHAIN_SIZE;
1203 tmp_cap->data_type = MOST_CH_CONTROL | MOST_CH_ASYNC |
1204 MOST_CH_ISOC | MOST_CH_SYNC;
1205 if (usb_endpoint_dir_in(ep_desc))
1206 tmp_cap->direction = MOST_CH_RX;
1208 tmp_cap->direction = MOST_CH_TX;
1210 init_usb_anchor(&mdev->busy_urbs[i]);
1211 spin_lock_init(&mdev->channel_lock[i]);
1213 dev_notice(dev, "claimed gadget: Vendor=%4.4x ProdID=%4.4x Bus=%02x Device=%02x\n",
1214 le16_to_cpu(usb_dev->descriptor.idVendor),
1215 le16_to_cpu(usb_dev->descriptor.idProduct),
1216 usb_dev->bus->busnum,
1219 dev_notice(dev, "device path: /sys/bus/usb/devices/%d-%s:%d.%d\n",
1220 usb_dev->bus->busnum,
1222 usb_dev->config->desc.bConfigurationValue,
1223 usb_iface_desc->desc.bInterfaceNumber);
1225 mdev->parent = most_register_interface(&mdev->iface);
1226 if (IS_ERR(mdev->parent)) {
1227 ret = PTR_ERR(mdev->parent);
1231 mutex_lock(&mdev->io_mutex);
1232 if (le16_to_cpu(usb_dev->descriptor.idProduct) == USB_DEV_ID_OS81118 ||
1233 le16_to_cpu(usb_dev->descriptor.idProduct) == USB_DEV_ID_OS81119 ||
1234 le16_to_cpu(usb_dev->descriptor.idProduct) == USB_DEV_ID_OS81210) {
1235 /* this increments the reference count of the instance
1236 * object of the core
1238 mdev->dci = create_most_dci_obj(mdev->parent);
1240 mutex_unlock(&mdev->io_mutex);
1241 most_deregister_interface(&mdev->iface);
1246 kobject_uevent(&mdev->dci->kobj, KOBJ_ADD);
1247 mdev->dci->usb_device = mdev->usb_device;
1249 mutex_unlock(&mdev->io_mutex);
1253 kfree(mdev->busy_urbs);
1255 kfree(mdev->ep_address);
1263 if (ret == 0 || ret == -ENOMEM) {
1265 dev_err(dev, "out of memory\n");
1271 * hdm_disconnect - disconnect function of USB device driver
1272 * @interface: Interface of the attached USB device
1274 * This deregisters the interface with the core, removes the kernel timer
1275 * and frees resources.
1277 * Context: hub kernel thread
1279 static void hdm_disconnect(struct usb_interface *interface)
1281 struct most_dev *mdev = usb_get_intfdata(interface);
1283 mutex_lock(&mdev->io_mutex);
1284 usb_set_intfdata(interface, NULL);
1285 mdev->usb_device = NULL;
1286 mutex_unlock(&mdev->io_mutex);
1288 del_timer_sync(&mdev->link_stat_timer);
1289 cancel_work_sync(&mdev->poll_work_obj);
1291 destroy_most_dci_obj(mdev->dci);
1292 most_deregister_interface(&mdev->iface);
1294 kfree(mdev->busy_urbs);
1297 kfree(mdev->ep_address);
1301 static struct usb_driver hdm_usb = {
1305 .disconnect = hdm_disconnect,
1308 static int __init hdm_usb_init(void)
1310 pr_info("hdm_usb_init()\n");
1311 if (usb_register(&hdm_usb)) {
1312 pr_err("could not register hdm_usb driver\n");
1319 static void __exit hdm_usb_exit(void)
1321 pr_info("hdm_usb_exit()\n");
1322 usb_deregister(&hdm_usb);
1325 module_init(hdm_usb_init);
1326 module_exit(hdm_usb_exit);
1327 MODULE_LICENSE("GPL");
1328 MODULE_AUTHOR("Christian Gromm <christian.gromm@microchip.com>");
1329 MODULE_DESCRIPTION("HDM_4_USB");