Greg Kroah-Hartman | 1a79f22 | 2017-11-07 14:58:50 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 2 | /* |
Christian Gromm | 6e01fc7 | 2017-11-21 15:04:41 +0100 | [diff] [blame] | 3 | * usb.c - Hardware dependent module for USB |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2013-2015 Microchip Technology Germany II GmbH & Co. KG |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 8 | #include <linux/module.h> |
| 9 | #include <linux/fs.h> |
| 10 | #include <linux/usb.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/cdev.h> |
| 14 | #include <linux/device.h> |
| 15 | #include <linux/list.h> |
| 16 | #include <linux/completion.h> |
| 17 | #include <linux/mutex.h> |
| 18 | #include <linux/spinlock.h> |
| 19 | #include <linux/interrupt.h> |
| 20 | #include <linux/workqueue.h> |
| 21 | #include <linux/sysfs.h> |
| 22 | #include <linux/dma-mapping.h> |
| 23 | #include <linux/etherdevice.h> |
| 24 | #include <linux/uaccess.h> |
Christian Gromm | b276527 | 2020-03-10 14:02:40 +0100 | [diff] [blame] | 25 | #include <linux/most.h> |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 26 | |
| 27 | #define USB_MTU 512 |
| 28 | #define NO_ISOCHRONOUS_URB 0 |
| 29 | #define AV_PACKETS_PER_XACT 2 |
| 30 | #define BUF_CHAIN_SIZE 0xFFFF |
| 31 | #define MAX_NUM_ENDPOINTS 30 |
| 32 | #define MAX_SUFFIX_LEN 10 |
| 33 | #define MAX_STRING_LEN 80 |
| 34 | #define MAX_BUF_SIZE 0xFFFF |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 35 | |
| 36 | #define USB_VENDOR_ID_SMSC 0x0424 /* VID: SMSC */ |
| 37 | #define USB_DEV_ID_BRDG 0xC001 /* PID: USB Bridge */ |
Christian Gromm | 654f7ec | 2016-08-19 11:12:50 +0200 | [diff] [blame] | 38 | #define USB_DEV_ID_OS81118 0xCF18 /* PID: USB OS81118 */ |
Christian Gromm | b50762e | 2016-08-19 11:12:49 +0200 | [diff] [blame] | 39 | #define USB_DEV_ID_OS81119 0xCF19 /* PID: USB OS81119 */ |
Christian Gromm | 5bf9bd8 | 2016-08-19 11:13:01 +0200 | [diff] [blame] | 40 | #define USB_DEV_ID_OS81210 0xCF30 /* PID: USB OS81210 */ |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 41 | /* DRCI Addresses */ |
| 42 | #define DRCI_REG_NI_STATE 0x0100 |
| 43 | #define DRCI_REG_PACKET_BW 0x0101 |
| 44 | #define DRCI_REG_NODE_ADDR 0x0102 |
| 45 | #define DRCI_REG_NODE_POS 0x0103 |
| 46 | #define DRCI_REG_MEP_FILTER 0x0140 |
| 47 | #define DRCI_REG_HASH_TBL0 0x0141 |
| 48 | #define DRCI_REG_HASH_TBL1 0x0142 |
| 49 | #define DRCI_REG_HASH_TBL2 0x0143 |
| 50 | #define DRCI_REG_HASH_TBL3 0x0144 |
| 51 | #define DRCI_REG_HW_ADDR_HI 0x0145 |
| 52 | #define DRCI_REG_HW_ADDR_MI 0x0146 |
| 53 | #define DRCI_REG_HW_ADDR_LO 0x0147 |
Christian Gromm | d747e8e | 2015-09-28 17:18:38 +0200 | [diff] [blame] | 54 | #define DRCI_REG_BASE 0x1100 |
| 55 | #define DRCI_COMMAND 0x02 |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 56 | #define DRCI_READ_REQ 0xA0 |
| 57 | #define DRCI_WRITE_REQ 0xA1 |
| 58 | |
| 59 | /** |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 60 | * struct most_dci_obj - Direct Communication Interface |
| 61 | * @kobj:position in sysfs |
| 62 | * @usb_device: pointer to the usb device |
Christian Gromm | c055464 | 2016-09-09 15:25:35 +0200 | [diff] [blame] | 63 | * @reg_addr: register address for arbitrary DCI access |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 64 | */ |
| 65 | struct most_dci_obj { |
Christian Gromm | 4d5f022 | 2017-11-21 15:04:43 +0100 | [diff] [blame] | 66 | struct device dev; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 67 | struct usb_device *usb_device; |
Christian Gromm | c055464 | 2016-09-09 15:25:35 +0200 | [diff] [blame] | 68 | u16 reg_addr; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 69 | }; |
Christian Gromm | 9cbe5aa | 2015-10-21 17:50:45 +0200 | [diff] [blame] | 70 | |
Christian Gromm | 4d5f022 | 2017-11-21 15:04:43 +0100 | [diff] [blame] | 71 | #define to_dci_obj(p) container_of(p, struct most_dci_obj, dev) |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 72 | |
Christian Gromm | cc28983 | 2016-08-19 11:12:47 +0200 | [diff] [blame] | 73 | struct most_dev; |
| 74 | |
| 75 | struct clear_hold_work { |
| 76 | struct work_struct ws; |
| 77 | struct most_dev *mdev; |
| 78 | unsigned int channel; |
| 79 | int pipe; |
| 80 | }; |
| 81 | |
| 82 | #define to_clear_hold_work(w) container_of(w, struct clear_hold_work, ws) |
| 83 | |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 84 | /** |
| 85 | * struct most_dev - holds all usb interface specific stuff |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 86 | * @usb_device: pointer to usb device |
| 87 | * @iface: hardware interface |
| 88 | * @cap: channel capabilities |
| 89 | * @conf: channel configuration |
| 90 | * @dci: direct communication interface of hardware |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 91 | * @ep_address: endpoint address table |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 92 | * @description: device description |
| 93 | * @suffix: suffix for channel name |
Christian Gromm | 88d1878 | 2016-09-19 17:40:25 +0200 | [diff] [blame] | 94 | * @channel_lock: synchronize channel access |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 95 | * @padding_active: indicates channel uses padding |
| 96 | * @is_channel_healthy: health status table of each channel |
Christian Gromm | 27e6245 | 2016-09-19 17:40:22 +0200 | [diff] [blame] | 97 | * @busy_urbs: list of anchored items |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 98 | * @io_mutex: synchronize I/O with disconnect |
| 99 | * @link_stat_timer: timer for link status reports |
| 100 | * @poll_work_obj: work for polling link status |
| 101 | */ |
| 102 | struct most_dev { |
Christian Gromm | 723de0f | 2020-01-23 16:38:17 +0100 | [diff] [blame] | 103 | struct device dev; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 104 | struct usb_device *usb_device; |
| 105 | struct most_interface iface; |
| 106 | struct most_channel_capability *cap; |
| 107 | struct most_channel_config *conf; |
| 108 | struct most_dci_obj *dci; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 109 | u8 *ep_address; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 110 | char description[MAX_STRING_LEN]; |
| 111 | char suffix[MAX_NUM_ENDPOINTS][MAX_SUFFIX_LEN]; |
Christian Gromm | 88d1878 | 2016-09-19 17:40:25 +0200 | [diff] [blame] | 112 | spinlock_t channel_lock[MAX_NUM_ENDPOINTS]; /* sync channel access */ |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 113 | bool padding_active[MAX_NUM_ENDPOINTS]; |
| 114 | bool is_channel_healthy[MAX_NUM_ENDPOINTS]; |
Christian Gromm | cc28983 | 2016-08-19 11:12:47 +0200 | [diff] [blame] | 115 | struct clear_hold_work clear_work[MAX_NUM_ENDPOINTS]; |
Christian Gromm | 27e6245 | 2016-09-19 17:40:22 +0200 | [diff] [blame] | 116 | struct usb_anchor *busy_urbs; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 117 | struct mutex io_mutex; |
| 118 | struct timer_list link_stat_timer; |
| 119 | struct work_struct poll_work_obj; |
Ravi Eluri | 9917b20 | 2017-12-22 15:39:02 +0530 | [diff] [blame] | 120 | void (*on_netinfo)(struct most_interface *most_iface, |
| 121 | unsigned char link_state, unsigned char *addrs); |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 122 | }; |
Christian Gromm | 9cbe5aa | 2015-10-21 17:50:45 +0200 | [diff] [blame] | 123 | |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 124 | #define to_mdev(d) container_of(d, struct most_dev, iface) |
Christian Gromm | 723de0f | 2020-01-23 16:38:17 +0100 | [diff] [blame] | 125 | #define to_mdev_from_dev(d) container_of(d, struct most_dev, dev) |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 126 | #define to_mdev_from_work(w) container_of(w, struct most_dev, poll_work_obj) |
| 127 | |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 128 | static void wq_clear_halt(struct work_struct *wq_obj); |
| 129 | static void wq_netinfo(struct work_struct *wq_obj); |
| 130 | |
| 131 | /** |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 132 | * drci_rd_reg - read a DCI register |
| 133 | * @dev: usb device |
| 134 | * @reg: register address |
| 135 | * @buf: buffer to store data |
| 136 | * |
| 137 | * This is reads data from INIC's direct register communication interface |
| 138 | */ |
Christian Gromm | 2637022 | 2015-09-28 17:18:43 +0200 | [diff] [blame] | 139 | static inline int drci_rd_reg(struct usb_device *dev, u16 reg, u16 *buf) |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 140 | { |
Christian Gromm | 2637022 | 2015-09-28 17:18:43 +0200 | [diff] [blame] | 141 | int retval; |
Christian Gromm | a0dbe1b | 2020-05-27 11:06:19 +0200 | [diff] [blame] | 142 | __le16 *dma_buf; |
Christian Gromm | 2637022 | 2015-09-28 17:18:43 +0200 | [diff] [blame] | 143 | u8 req_type = USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE; |
| 144 | |
Christian Gromm | a0dbe1b | 2020-05-27 11:06:19 +0200 | [diff] [blame] | 145 | dma_buf = kzalloc(sizeof(*dma_buf), GFP_KERNEL); |
Christian Gromm | 2637022 | 2015-09-28 17:18:43 +0200 | [diff] [blame] | 146 | if (!dma_buf) |
| 147 | return -ENOMEM; |
| 148 | |
| 149 | retval = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), |
| 150 | DRCI_READ_REQ, req_type, |
| 151 | 0x0000, |
Johan Hovold | 63b3e810 | 2021-10-25 13:58:11 +0200 | [diff] [blame] | 152 | reg, dma_buf, sizeof(*dma_buf), |
| 153 | USB_CTRL_GET_TIMEOUT); |
Christian Gromm | c81c9c3 | 2015-09-28 17:18:44 +0200 | [diff] [blame] | 154 | *buf = le16_to_cpu(*dma_buf); |
Christian Gromm | 2637022 | 2015-09-28 17:18:43 +0200 | [diff] [blame] | 155 | kfree(dma_buf); |
| 156 | |
Christian Gromm | ffd069e | 2020-05-27 11:06:20 +0200 | [diff] [blame] | 157 | if (retval < 0) |
| 158 | return retval; |
| 159 | return 0; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /** |
| 163 | * drci_wr_reg - write a DCI register |
| 164 | * @dev: usb device |
| 165 | * @reg: register address |
| 166 | * @data: data to write |
| 167 | * |
| 168 | * This is writes data to INIC's direct register communication interface |
| 169 | */ |
| 170 | static inline int drci_wr_reg(struct usb_device *dev, u16 reg, u16 data) |
| 171 | { |
| 172 | return usb_control_msg(dev, |
| 173 | usb_sndctrlpipe(dev, 0), |
| 174 | DRCI_WRITE_REQ, |
| 175 | USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, |
| 176 | data, |
| 177 | reg, |
| 178 | NULL, |
| 179 | 0, |
Johan Hovold | 63b3e810 | 2021-10-25 13:58:11 +0200 | [diff] [blame] | 180 | USB_CTRL_SET_TIMEOUT); |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 181 | } |
| 182 | |
Andrey Shvetsov | e33269f | 2016-10-25 17:44:21 +0200 | [diff] [blame] | 183 | static inline int start_sync_ep(struct usb_device *usb_dev, u16 ep) |
| 184 | { |
| 185 | return drci_wr_reg(usb_dev, DRCI_REG_BASE + DRCI_COMMAND + ep * 16, 1); |
| 186 | } |
| 187 | |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 188 | /** |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 189 | * get_stream_frame_size - calculate frame size of current configuration |
Christian Gromm | 1c538a4 | 2020-05-27 11:06:18 +0200 | [diff] [blame] | 190 | * @dev: device structure |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 191 | * @cfg: channel configuration |
| 192 | */ |
Christian Gromm | 1c538a4 | 2020-05-27 11:06:18 +0200 | [diff] [blame] | 193 | static unsigned int get_stream_frame_size(struct device *dev, |
| 194 | struct most_channel_config *cfg) |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 195 | { |
Christian Gromm | 2c069b6 | 2020-05-27 11:06:21 +0200 | [diff] [blame] | 196 | unsigned int frame_size; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 197 | unsigned int sub_size = cfg->subbuffer_size; |
| 198 | |
| 199 | if (!sub_size) { |
Christian Gromm | 6257322 | 2020-05-15 11:20:59 +0200 | [diff] [blame] | 200 | dev_warn(dev, "Misconfig: Subbuffer size zero.\n"); |
Christian Gromm | 2c069b6 | 2020-05-27 11:06:21 +0200 | [diff] [blame] | 201 | return 0; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 202 | } |
| 203 | switch (cfg->data_type) { |
Andrey Shvetsov | 0540609 | 2016-09-21 14:49:10 +0200 | [diff] [blame] | 204 | case MOST_CH_ISOC: |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 205 | frame_size = AV_PACKETS_PER_XACT * sub_size; |
| 206 | break; |
| 207 | case MOST_CH_SYNC: |
| 208 | if (cfg->packets_per_xact == 0) { |
Christian Gromm | 6257322 | 2020-05-15 11:20:59 +0200 | [diff] [blame] | 209 | dev_warn(dev, "Misconfig: Packets per XACT zero\n"); |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 210 | frame_size = 0; |
Christian Gromm | 9deba73 | 2015-10-21 17:50:44 +0200 | [diff] [blame] | 211 | } else if (cfg->packets_per_xact == 0xFF) { |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 212 | frame_size = (USB_MTU / sub_size) * sub_size; |
Christian Gromm | 9deba73 | 2015-10-21 17:50:44 +0200 | [diff] [blame] | 213 | } else { |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 214 | frame_size = cfg->packets_per_xact * sub_size; |
Christian Gromm | 9deba73 | 2015-10-21 17:50:44 +0200 | [diff] [blame] | 215 | } |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 216 | break; |
| 217 | default: |
Christian Gromm | 6257322 | 2020-05-15 11:20:59 +0200 | [diff] [blame] | 218 | dev_warn(dev, "Query frame size of non-streaming channel\n"); |
Christian Gromm | 11974ac | 2020-05-28 14:41:43 +0200 | [diff] [blame] | 219 | frame_size = 0; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 220 | break; |
| 221 | } |
| 222 | return frame_size; |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * hdm_poison_channel - mark buffers of this channel as invalid |
| 227 | * @iface: pointer to the interface |
| 228 | * @channel: channel ID |
| 229 | * |
| 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. |
| 233 | * |
| 234 | * Returns 0 on success or error code otherwise. |
| 235 | */ |
Adrian Remonda | 23fe15f | 2015-08-14 12:18:02 +0200 | [diff] [blame] | 236 | static int hdm_poison_channel(struct most_interface *iface, int channel) |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 237 | { |
Christian Gromm | 089612f | 2016-08-19 11:12:59 +0200 | [diff] [blame] | 238 | struct most_dev *mdev = to_mdev(iface); |
Christian Gromm | b24c9fe | 2016-08-19 11:12:56 +0200 | [diff] [blame] | 239 | unsigned long flags; |
| 240 | spinlock_t *lock; /* temp. lock */ |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 241 | |
Christian Gromm | 188d5b4 | 2020-05-05 12:00:28 +0200 | [diff] [blame] | 242 | if (channel < 0 || channel >= iface->num_channels) { |
Christian Gromm | 59ed048 | 2015-07-30 18:19:41 +0200 | [diff] [blame] | 243 | dev_warn(&mdev->usb_device->dev, "Channel ID out of range.\n"); |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 244 | return -ECHRNG; |
| 245 | } |
| 246 | |
Christian Gromm | 88d1878 | 2016-09-19 17:40:25 +0200 | [diff] [blame] | 247 | lock = mdev->channel_lock + channel; |
Christian Gromm | b24c9fe | 2016-08-19 11:12:56 +0200 | [diff] [blame] | 248 | spin_lock_irqsave(lock, flags); |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 249 | mdev->is_channel_healthy[channel] = false; |
Christian Gromm | b24c9fe | 2016-08-19 11:12:56 +0200 | [diff] [blame] | 250 | spin_unlock_irqrestore(lock, flags); |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 251 | |
Christian Gromm | cc28983 | 2016-08-19 11:12:47 +0200 | [diff] [blame] | 252 | cancel_work_sync(&mdev->clear_work[channel].ws); |
| 253 | |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 254 | mutex_lock(&mdev->io_mutex); |
Andrey Shvetsov | 3a54200 | 2016-10-04 17:10:21 +0200 | [diff] [blame] | 255 | usb_kill_anchored_urbs(&mdev->busy_urbs[channel]); |
Sudip Mukherjee | ec58d2a | 2015-09-04 16:22:06 +0530 | [diff] [blame] | 256 | if (mdev->padding_active[channel]) |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 257 | mdev->padding_active[channel] = false; |
| 258 | |
| 259 | if (mdev->conf[channel].data_type == MOST_CH_ASYNC) { |
| 260 | del_timer_sync(&mdev->link_stat_timer); |
| 261 | cancel_work_sync(&mdev->poll_work_obj); |
| 262 | } |
| 263 | mutex_unlock(&mdev->io_mutex); |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * hdm_add_padding - add padding bytes |
| 269 | * @mdev: most device |
| 270 | * @channel: channel ID |
| 271 | * @mbo: buffer object |
| 272 | * |
| 273 | * This inserts the INIC hardware specific padding bytes into a streaming |
| 274 | * channel's buffer |
| 275 | */ |
Adrian Remonda | 23fe15f | 2015-08-14 12:18:02 +0200 | [diff] [blame] | 276 | static int hdm_add_padding(struct most_dev *mdev, int channel, struct mbo *mbo) |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 277 | { |
| 278 | struct most_channel_config *conf = &mdev->conf[channel]; |
Christian Gromm | 1c538a4 | 2020-05-27 11:06:18 +0200 | [diff] [blame] | 279 | unsigned int frame_size = get_stream_frame_size(&mdev->dev, conf); |
Andrey Shvetsov | ac33fbb | 2016-09-09 15:25:41 +0200 | [diff] [blame] | 280 | unsigned int j, num_frames; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 281 | |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 282 | if (!frame_size) |
Christian Gromm | 441be56 | 2020-05-05 12:00:27 +0200 | [diff] [blame] | 283 | return -EINVAL; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 284 | num_frames = mbo->buffer_length / frame_size; |
| 285 | |
| 286 | if (num_frames < 1) { |
Christian Gromm | 59ed048 | 2015-07-30 18:19:41 +0200 | [diff] [blame] | 287 | dev_err(&mdev->usb_device->dev, |
| 288 | "Missed minimal transfer unit.\n"); |
Christian Gromm | 441be56 | 2020-05-05 12:00:27 +0200 | [diff] [blame] | 289 | return -EINVAL; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 290 | } |
| 291 | |
Andrey Shvetsov | 5c13155 | 2017-04-07 15:38:39 +0200 | [diff] [blame] | 292 | for (j = num_frames - 1; j > 0; j--) |
| 293 | memmove(mbo->virt_address + j * USB_MTU, |
| 294 | mbo->virt_address + j * frame_size, |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 295 | frame_size); |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 296 | mbo->buffer_length = num_frames * USB_MTU; |
| 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * hdm_remove_padding - remove padding bytes |
| 302 | * @mdev: most device |
| 303 | * @channel: channel ID |
| 304 | * @mbo: buffer object |
| 305 | * |
| 306 | * This takes the INIC hardware specific padding bytes off a streaming |
| 307 | * channel's buffer. |
| 308 | */ |
Christian Gromm | ba170ee | 2015-10-15 13:28:56 +0200 | [diff] [blame] | 309 | static int hdm_remove_padding(struct most_dev *mdev, int channel, |
| 310 | struct mbo *mbo) |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 311 | { |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 312 | struct most_channel_config *const conf = &mdev->conf[channel]; |
Christian Gromm | 1c538a4 | 2020-05-27 11:06:18 +0200 | [diff] [blame] | 313 | unsigned int frame_size = get_stream_frame_size(&mdev->dev, conf); |
Andrey Shvetsov | ac33fbb | 2016-09-09 15:25:41 +0200 | [diff] [blame] | 314 | unsigned int j, num_frames; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 315 | |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 316 | if (!frame_size) |
Christian Gromm | 441be56 | 2020-05-05 12:00:27 +0200 | [diff] [blame] | 317 | return -EINVAL; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 318 | num_frames = mbo->processed_length / USB_MTU; |
| 319 | |
| 320 | for (j = 1; j < num_frames; j++) |
| 321 | memmove(mbo->virt_address + frame_size * j, |
| 322 | mbo->virt_address + USB_MTU * j, |
| 323 | frame_size); |
| 324 | |
| 325 | mbo->processed_length = frame_size * num_frames; |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * hdm_write_completion - completion function for submitted Tx URBs |
| 331 | * @urb: the URB that has been completed |
| 332 | * |
| 333 | * This checks the status of the completed URB. In case the URB has been |
| 334 | * unlinked before, it is immediately freed. On any other error the MBO |
| 335 | * transfer flag is set. On success it frees allocated resources and calls |
| 336 | * the completion function. |
| 337 | * |
| 338 | * Context: interrupt! |
| 339 | */ |
| 340 | static void hdm_write_completion(struct urb *urb) |
| 341 | { |
Christian Gromm | 089612f | 2016-08-19 11:12:59 +0200 | [diff] [blame] | 342 | struct mbo *mbo = urb->context; |
Christian Gromm | 089612f | 2016-08-19 11:12:59 +0200 | [diff] [blame] | 343 | struct most_dev *mdev = to_mdev(mbo->ifp); |
| 344 | unsigned int channel = mbo->hdm_channel_id; |
Christian Gromm | 88d1878 | 2016-09-19 17:40:25 +0200 | [diff] [blame] | 345 | spinlock_t *lock = mdev->channel_lock + channel; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 346 | unsigned long flags; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 347 | |
Christian Gromm | b24c9fe | 2016-08-19 11:12:56 +0200 | [diff] [blame] | 348 | spin_lock_irqsave(lock, flags); |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 349 | |
Andrey Shvetsov | 3a54200 | 2016-10-04 17:10:21 +0200 | [diff] [blame] | 350 | mbo->processed_length = 0; |
| 351 | mbo->status = MBO_E_INVAL; |
| 352 | if (likely(mdev->is_channel_healthy[channel])) { |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 353 | switch (urb->status) { |
Andrey Shvetsov | 3a54200 | 2016-10-04 17:10:21 +0200 | [diff] [blame] | 354 | case 0: |
| 355 | case -ESHUTDOWN: |
| 356 | mbo->processed_length = urb->actual_length; |
| 357 | mbo->status = MBO_SUCCESS; |
| 358 | break; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 359 | case -EPIPE: |
Christian Gromm | be8a8ca | 2018-05-08 11:45:16 +0200 | [diff] [blame] | 360 | dev_warn(&mdev->usb_device->dev, |
| 361 | "Broken pipe on ep%02x\n", |
Christian Gromm | 3b1a774 | 2018-05-08 11:45:07 +0200 | [diff] [blame] | 362 | mdev->ep_address[channel]); |
Christian Gromm | 879c93fe | 2016-08-19 11:12:51 +0200 | [diff] [blame] | 363 | mdev->is_channel_healthy[channel] = false; |
Christian Gromm | cc28983 | 2016-08-19 11:12:47 +0200 | [diff] [blame] | 364 | mdev->clear_work[channel].pipe = urb->pipe; |
| 365 | schedule_work(&mdev->clear_work[channel].ws); |
Andrey Shvetsov | 3a54200 | 2016-10-04 17:10:21 +0200 | [diff] [blame] | 366 | break; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 367 | case -ENODEV: |
| 368 | case -EPROTO: |
| 369 | mbo->status = MBO_E_CLOSE; |
| 370 | break; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 371 | } |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 372 | } |
| 373 | |
Christian Gromm | cf6a599 | 2016-08-19 11:12:55 +0200 | [diff] [blame] | 374 | spin_unlock_irqrestore(lock, flags); |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 375 | |
| 376 | if (likely(mbo->complete)) |
| 377 | mbo->complete(mbo); |
| 378 | usb_free_urb(urb); |
| 379 | } |
| 380 | |
| 381 | /** |
Masanari Iida | 9f17591 | 2015-10-10 13:42:49 +0900 | [diff] [blame] | 382 | * hdm_read_completion - completion function for submitted Rx URBs |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 383 | * @urb: the URB that has been completed |
| 384 | * |
| 385 | * This checks the status of the completed URB. In case the URB has been |
| 386 | * unlinked before it is immediately freed. On any other error the MBO transfer |
| 387 | * flag is set. On success it frees allocated resources, removes |
| 388 | * padding bytes -if necessary- and calls the completion function. |
| 389 | * |
| 390 | * Context: interrupt! |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 391 | */ |
| 392 | static void hdm_read_completion(struct urb *urb) |
| 393 | { |
Christian Gromm | 089612f | 2016-08-19 11:12:59 +0200 | [diff] [blame] | 394 | struct mbo *mbo = urb->context; |
Christian Gromm | 089612f | 2016-08-19 11:12:59 +0200 | [diff] [blame] | 395 | struct most_dev *mdev = to_mdev(mbo->ifp); |
| 396 | unsigned int channel = mbo->hdm_channel_id; |
| 397 | struct device *dev = &mdev->usb_device->dev; |
Christian Gromm | 88d1878 | 2016-09-19 17:40:25 +0200 | [diff] [blame] | 398 | spinlock_t *lock = mdev->channel_lock + channel; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 399 | unsigned long flags; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 400 | |
Christian Gromm | b24c9fe | 2016-08-19 11:12:56 +0200 | [diff] [blame] | 401 | spin_lock_irqsave(lock, flags); |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 402 | |
Andrey Shvetsov | 3a54200 | 2016-10-04 17:10:21 +0200 | [diff] [blame] | 403 | mbo->processed_length = 0; |
| 404 | mbo->status = MBO_E_INVAL; |
| 405 | if (likely(mdev->is_channel_healthy[channel])) { |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 406 | switch (urb->status) { |
Andrey Shvetsov | 3a54200 | 2016-10-04 17:10:21 +0200 | [diff] [blame] | 407 | case 0: |
| 408 | case -ESHUTDOWN: |
| 409 | mbo->processed_length = urb->actual_length; |
| 410 | mbo->status = MBO_SUCCESS; |
| 411 | if (mdev->padding_active[channel] && |
| 412 | hdm_remove_padding(mdev, channel, mbo)) { |
| 413 | mbo->processed_length = 0; |
| 414 | mbo->status = MBO_E_INVAL; |
| 415 | } |
| 416 | break; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 417 | case -EPIPE: |
Christian Gromm | 3b1a774 | 2018-05-08 11:45:07 +0200 | [diff] [blame] | 418 | dev_warn(dev, "Broken pipe on ep%02x\n", |
| 419 | mdev->ep_address[channel]); |
Christian Gromm | 879c93fe | 2016-08-19 11:12:51 +0200 | [diff] [blame] | 420 | mdev->is_channel_healthy[channel] = false; |
Christian Gromm | cc28983 | 2016-08-19 11:12:47 +0200 | [diff] [blame] | 421 | mdev->clear_work[channel].pipe = urb->pipe; |
| 422 | schedule_work(&mdev->clear_work[channel].ws); |
Andrey Shvetsov | 3a54200 | 2016-10-04 17:10:21 +0200 | [diff] [blame] | 423 | break; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 424 | case -ENODEV: |
| 425 | case -EPROTO: |
| 426 | mbo->status = MBO_E_CLOSE; |
| 427 | break; |
| 428 | case -EOVERFLOW: |
Christian Gromm | 3b1a774 | 2018-05-08 11:45:07 +0200 | [diff] [blame] | 429 | dev_warn(dev, "Babble on ep%02x\n", |
| 430 | mdev->ep_address[channel]); |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 431 | break; |
| 432 | } |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 433 | } |
Christian Gromm | b24c9fe | 2016-08-19 11:12:56 +0200 | [diff] [blame] | 434 | |
Christian Gromm | cf6a599 | 2016-08-19 11:12:55 +0200 | [diff] [blame] | 435 | spin_unlock_irqrestore(lock, flags); |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 436 | |
| 437 | if (likely(mbo->complete)) |
| 438 | mbo->complete(mbo); |
| 439 | usb_free_urb(urb); |
| 440 | } |
| 441 | |
| 442 | /** |
| 443 | * hdm_enqueue - receive a buffer to be used for data transfer |
| 444 | * @iface: interface to enqueue to |
| 445 | * @channel: ID of the channel |
| 446 | * @mbo: pointer to the buffer object |
| 447 | * |
| 448 | * This allocates a new URB and fills it according to the channel |
| 449 | * that is being used for transmission of data. Before the URB is |
| 450 | * submitted it is stored in the private anchor list. |
| 451 | * |
| 452 | * Returns 0 on success. On any error the URB is freed and a error code |
| 453 | * is returned. |
| 454 | * |
| 455 | * Context: Could in _some_ cases be interrupt! |
| 456 | */ |
Christian Gromm | ba170ee | 2015-10-15 13:28:56 +0200 | [diff] [blame] | 457 | static int hdm_enqueue(struct most_interface *iface, int channel, |
| 458 | struct mbo *mbo) |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 459 | { |
Christian Gromm | e3881eb | 2020-05-05 12:00:29 +0200 | [diff] [blame] | 460 | struct most_dev *mdev = to_mdev(iface); |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 461 | struct most_channel_config *conf; |
| 462 | int retval = 0; |
| 463 | struct urb *urb; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 464 | unsigned long length; |
| 465 | void *virt_address; |
| 466 | |
Christian Gromm | 188d5b4 | 2020-05-05 12:00:28 +0200 | [diff] [blame] | 467 | if (!mbo) |
Christian Gromm | 441be56 | 2020-05-05 12:00:27 +0200 | [diff] [blame] | 468 | return -EINVAL; |
Christian Gromm | 188d5b4 | 2020-05-05 12:00:28 +0200 | [diff] [blame] | 469 | if (iface->num_channels <= channel || channel < 0) |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 470 | return -ECHRNG; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 471 | |
Christian Gromm | 8bf56cf | 2020-05-27 11:06:22 +0200 | [diff] [blame] | 472 | urb = usb_alloc_urb(NO_ISOCHRONOUS_URB, GFP_KERNEL); |
| 473 | if (!urb) |
| 474 | return -ENOMEM; |
| 475 | |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 476 | conf = &mdev->conf[channel]; |
Christian Gromm | c06b99e | 2018-05-08 11:45:15 +0200 | [diff] [blame] | 477 | |
| 478 | mutex_lock(&mdev->io_mutex); |
| 479 | if (!mdev->usb_device) { |
| 480 | retval = -ENODEV; |
Christian Gromm | 8bf56cf | 2020-05-27 11:06:22 +0200 | [diff] [blame] | 481 | goto err_free_urb; |
Christian Gromm | c06b99e | 2018-05-08 11:45:15 +0200 | [diff] [blame] | 482 | } |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 483 | |
Christian Gromm | dd53ecb | 2016-08-19 11:12:58 +0200 | [diff] [blame] | 484 | if ((conf->direction & MOST_CH_TX) && mdev->padding_active[channel] && |
| 485 | hdm_add_padding(mdev, channel, mbo)) { |
Christian Gromm | 441be56 | 2020-05-05 12:00:27 +0200 | [diff] [blame] | 486 | retval = -EINVAL; |
Christian Gromm | bddd3c2 | 2018-09-21 11:28:51 +0200 | [diff] [blame] | 487 | goto err_free_urb; |
Christian Gromm | dd53ecb | 2016-08-19 11:12:58 +0200 | [diff] [blame] | 488 | } |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 489 | |
| 490 | urb->transfer_dma = mbo->bus_address; |
| 491 | virt_address = mbo->virt_address; |
| 492 | length = mbo->buffer_length; |
| 493 | |
| 494 | if (conf->direction & MOST_CH_TX) { |
| 495 | usb_fill_bulk_urb(urb, mdev->usb_device, |
| 496 | usb_sndbulkpipe(mdev->usb_device, |
| 497 | mdev->ep_address[channel]), |
| 498 | virt_address, |
| 499 | length, |
| 500 | hdm_write_completion, |
| 501 | mbo); |
Christian Gromm | 9a32315 | 2018-05-08 11:45:14 +0200 | [diff] [blame] | 502 | if (conf->data_type != MOST_CH_ISOC && |
| 503 | conf->data_type != MOST_CH_SYNC) |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 504 | urb->transfer_flags |= URB_ZERO_PACKET; |
| 505 | } else { |
| 506 | usb_fill_bulk_urb(urb, mdev->usb_device, |
| 507 | usb_rcvbulkpipe(mdev->usb_device, |
| 508 | mdev->ep_address[channel]), |
| 509 | virt_address, |
Christian Gromm | 9161e93 | 2015-09-28 17:18:34 +0200 | [diff] [blame] | 510 | length + conf->extra_len, |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 511 | hdm_read_completion, |
| 512 | mbo); |
| 513 | } |
| 514 | urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
| 515 | |
Christian Gromm | 27e6245 | 2016-09-19 17:40:22 +0200 | [diff] [blame] | 516 | usb_anchor_urb(urb, &mdev->busy_urbs[channel]); |
Christian Gromm | ec7e0a1 | 2016-07-18 17:25:27 +0200 | [diff] [blame] | 517 | |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 518 | retval = usb_submit_urb(urb, GFP_KERNEL); |
| 519 | if (retval) { |
Christian Gromm | be8a8ca | 2018-05-08 11:45:16 +0200 | [diff] [blame] | 520 | dev_err(&mdev->usb_device->dev, |
| 521 | "URB submit failed with error %d.\n", retval); |
Christian Gromm | bddd3c2 | 2018-09-21 11:28:51 +0200 | [diff] [blame] | 522 | goto err_unanchor_urb; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 523 | } |
Christian Gromm | 6405fe2 | 2020-05-27 11:06:23 +0200 | [diff] [blame] | 524 | mutex_unlock(&mdev->io_mutex); |
| 525 | return 0; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 526 | |
Christian Gromm | bddd3c2 | 2018-09-21 11:28:51 +0200 | [diff] [blame] | 527 | err_unanchor_urb: |
Christian Gromm | 27e6245 | 2016-09-19 17:40:22 +0200 | [diff] [blame] | 528 | usb_unanchor_urb(urb); |
Christian Gromm | bddd3c2 | 2018-09-21 11:28:51 +0200 | [diff] [blame] | 529 | err_free_urb: |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 530 | usb_free_urb(urb); |
Christian Gromm | c06b99e | 2018-05-08 11:45:15 +0200 | [diff] [blame] | 531 | mutex_unlock(&mdev->io_mutex); |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 532 | return retval; |
| 533 | } |
| 534 | |
Christian Gromm | 3598cec | 2018-05-08 11:45:03 +0200 | [diff] [blame] | 535 | static void *hdm_dma_alloc(struct mbo *mbo, u32 size) |
| 536 | { |
| 537 | struct most_dev *mdev = to_mdev(mbo->ifp); |
| 538 | |
| 539 | return usb_alloc_coherent(mdev->usb_device, size, GFP_KERNEL, |
| 540 | &mbo->bus_address); |
| 541 | } |
| 542 | |
| 543 | static void hdm_dma_free(struct mbo *mbo, u32 size) |
| 544 | { |
| 545 | struct most_dev *mdev = to_mdev(mbo->ifp); |
| 546 | |
| 547 | usb_free_coherent(mdev->usb_device, size, mbo->virt_address, |
| 548 | mbo->bus_address); |
| 549 | } |
| 550 | |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 551 | /** |
| 552 | * hdm_configure_channel - receive channel configuration from core |
| 553 | * @iface: interface |
| 554 | * @channel: channel ID |
| 555 | * @conf: structure that holds the configuration information |
Christian Gromm | 25e3854 | 2016-10-28 14:46:20 +0200 | [diff] [blame] | 556 | * |
| 557 | * The attached network interface controller (NIC) supports a padding mode |
| 558 | * to avoid short packets on USB, hence increasing the performance due to a |
| 559 | * lower interrupt load. This mode is default for synchronous data and can |
| 560 | * be switched on for isochronous data. In case padding is active the |
| 561 | * driver needs to know the frame size of the payload in order to calculate |
| 562 | * the number of bytes it needs to pad when transmitting or to cut off when |
| 563 | * receiving data. |
| 564 | * |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 565 | */ |
Adrian Remonda | 23fe15f | 2015-08-14 12:18:02 +0200 | [diff] [blame] | 566 | static int hdm_configure_channel(struct most_interface *iface, int channel, |
| 567 | struct most_channel_config *conf) |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 568 | { |
| 569 | unsigned int num_frames; |
| 570 | unsigned int frame_size; |
Christian Gromm | 089612f | 2016-08-19 11:12:59 +0200 | [diff] [blame] | 571 | struct most_dev *mdev = to_mdev(iface); |
| 572 | struct device *dev = &mdev->usb_device->dev; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 573 | |
Christian Gromm | 188d5b4 | 2020-05-05 12:00:28 +0200 | [diff] [blame] | 574 | if (!conf) { |
Christian Gromm | 3e8621a | 2020-05-05 12:00:26 +0200 | [diff] [blame] | 575 | dev_err(dev, "Bad config pointer.\n"); |
Christian Gromm | 59ed048 | 2015-07-30 18:19:41 +0200 | [diff] [blame] | 576 | return -EINVAL; |
| 577 | } |
Christian Gromm | 188d5b4 | 2020-05-05 12:00:28 +0200 | [diff] [blame] | 578 | if (channel < 0 || channel >= iface->num_channels) { |
Christian Gromm | 59ed048 | 2015-07-30 18:19:41 +0200 | [diff] [blame] | 579 | dev_err(dev, "Channel ID out of range.\n"); |
| 580 | return -EINVAL; |
| 581 | } |
Colin Ian King | d92e699 | 2020-05-07 16:06:52 +0100 | [diff] [blame] | 582 | |
| 583 | mdev->is_channel_healthy[channel] = true; |
| 584 | mdev->clear_work[channel].channel = channel; |
| 585 | mdev->clear_work[channel].mdev = mdev; |
| 586 | INIT_WORK(&mdev->clear_work[channel].ws, wq_clear_halt); |
| 587 | |
Christian Gromm | dd53ecb | 2016-08-19 11:12:58 +0200 | [diff] [blame] | 588 | if (!conf->num_buffers || !conf->buffer_size) { |
Christian Gromm | 59ed048 | 2015-07-30 18:19:41 +0200 | [diff] [blame] | 589 | dev_err(dev, "Misconfig: buffer size or #buffers zero.\n"); |
| 590 | return -EINVAL; |
| 591 | } |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 592 | |
Christian Gromm | dd53ecb | 2016-08-19 11:12:58 +0200 | [diff] [blame] | 593 | if (conf->data_type != MOST_CH_SYNC && |
Andrey Shvetsov | 0540609 | 2016-09-21 14:49:10 +0200 | [diff] [blame] | 594 | !(conf->data_type == MOST_CH_ISOC && |
Christian Gromm | dd53ecb | 2016-08-19 11:12:58 +0200 | [diff] [blame] | 595 | conf->packets_per_xact != 0xFF)) { |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 596 | mdev->padding_active[channel] = false; |
Christian Gromm | 25e3854 | 2016-10-28 14:46:20 +0200 | [diff] [blame] | 597 | /* |
| 598 | * Since the NIC's padding mode is not going to be |
| 599 | * used, we can skip the frame size calculations and |
| 600 | * move directly on to exit. |
| 601 | */ |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 602 | goto exit; |
| 603 | } |
| 604 | |
| 605 | mdev->padding_active[channel] = true; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 606 | |
Christian Gromm | 1c538a4 | 2020-05-27 11:06:18 +0200 | [diff] [blame] | 607 | frame_size = get_stream_frame_size(&mdev->dev, conf); |
Christian Gromm | dd53ecb | 2016-08-19 11:12:58 +0200 | [diff] [blame] | 608 | if (frame_size == 0 || frame_size > USB_MTU) { |
Christian Gromm | 59ed048 | 2015-07-30 18:19:41 +0200 | [diff] [blame] | 609 | dev_warn(dev, "Misconfig: frame size wrong\n"); |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 610 | return -EINVAL; |
| 611 | } |
| 612 | |
Andrey Shvetsov | f500192 | 2017-04-07 15:38:38 +0200 | [diff] [blame] | 613 | num_frames = conf->buffer_size / frame_size; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 614 | |
Andrey Shvetsov | f500192 | 2017-04-07 15:38:38 +0200 | [diff] [blame] | 615 | if (conf->buffer_size % frame_size) { |
| 616 | u16 old_size = conf->buffer_size; |
| 617 | |
| 618 | conf->buffer_size = num_frames * frame_size; |
| 619 | dev_warn(dev, "%s: fixed buffer size (%d -> %d)\n", |
| 620 | mdev->suffix[channel], old_size, conf->buffer_size); |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 621 | } |
| 622 | |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 623 | /* calculate extra length to comply w/ HW padding */ |
Andrey Shvetsov | f500192 | 2017-04-07 15:38:38 +0200 | [diff] [blame] | 624 | conf->extra_len = num_frames * (USB_MTU - frame_size); |
| 625 | |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 626 | exit: |
| 627 | mdev->conf[channel] = *conf; |
Andrey Shvetsov | 7c23baa | 2016-10-25 17:44:20 +0200 | [diff] [blame] | 628 | if (conf->data_type == MOST_CH_ASYNC) { |
| 629 | u16 ep = mdev->ep_address[channel]; |
Andrey Shvetsov | 7c23baa | 2016-10-25 17:44:20 +0200 | [diff] [blame] | 630 | |
Andrey Shvetsov | e33269f | 2016-10-25 17:44:21 +0200 | [diff] [blame] | 631 | if (start_sync_ep(mdev->usb_device, ep) < 0) |
Andrey Shvetsov | 7c23baa | 2016-10-25 17:44:20 +0200 | [diff] [blame] | 632 | dev_warn(dev, "sync for ep%02x failed", ep); |
| 633 | } |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 634 | return 0; |
| 635 | } |
| 636 | |
| 637 | /** |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 638 | * hdm_request_netinfo - request network information |
| 639 | * @iface: pointer to interface |
| 640 | * @channel: channel ID |
| 641 | * |
| 642 | * This is used as trigger to set up the link status timer that |
| 643 | * polls for the NI state of the INIC every 2 seconds. |
| 644 | * |
| 645 | */ |
Andrey Shvetsov | d35bbfa | 2017-05-12 12:59:54 +0200 | [diff] [blame] | 646 | static void hdm_request_netinfo(struct most_interface *iface, int channel, |
| 647 | void (*on_netinfo)(struct most_interface *, |
| 648 | unsigned char, |
| 649 | unsigned char *)) |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 650 | { |
Christian Gromm | e3881eb | 2020-05-05 12:00:29 +0200 | [diff] [blame] | 651 | struct most_dev *mdev = to_mdev(iface); |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 652 | |
Andrey Shvetsov | d35bbfa | 2017-05-12 12:59:54 +0200 | [diff] [blame] | 653 | mdev->on_netinfo = on_netinfo; |
| 654 | if (!on_netinfo) |
| 655 | return; |
| 656 | |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 657 | mdev->link_stat_timer.expires = jiffies + HZ; |
| 658 | mod_timer(&mdev->link_stat_timer, mdev->link_stat_timer.expires); |
| 659 | } |
| 660 | |
| 661 | /** |
Andrey Shvetsov | f28e6cd | 2016-10-04 17:10:19 +0200 | [diff] [blame] | 662 | * link_stat_timer_handler - schedule work obtaining mac address and link status |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 663 | * @data: pointer to USB device instance |
| 664 | * |
| 665 | * The handler runs in interrupt context. That's why we need to defer the |
| 666 | * tasks to a work queue. |
| 667 | */ |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 668 | static void link_stat_timer_handler(struct timer_list *t) |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 669 | { |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 670 | struct most_dev *mdev = from_timer(mdev, t, link_stat_timer); |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 671 | |
Amitoj Kaur Chawla | e3479f7 | 2016-02-20 15:15:38 +0530 | [diff] [blame] | 672 | schedule_work(&mdev->poll_work_obj); |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 673 | mdev->link_stat_timer.expires = jiffies + (2 * HZ); |
| 674 | add_timer(&mdev->link_stat_timer); |
| 675 | } |
| 676 | |
| 677 | /** |
Andrey Shvetsov | f28e6cd | 2016-10-04 17:10:19 +0200 | [diff] [blame] | 678 | * wq_netinfo - work queue function to deliver latest networking information |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 679 | * @wq_obj: object that holds data for our deferred work to do |
| 680 | * |
| 681 | * This retrieves the network interface status of the USB INIC |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 682 | */ |
| 683 | static void wq_netinfo(struct work_struct *wq_obj) |
| 684 | { |
Christian Gromm | 089612f | 2016-08-19 11:12:59 +0200 | [diff] [blame] | 685 | struct most_dev *mdev = to_mdev_from_work(wq_obj); |
Andrey Shvetsov | f28e6cd | 2016-10-04 17:10:19 +0200 | [diff] [blame] | 686 | struct usb_device *usb_device = mdev->usb_device; |
| 687 | struct device *dev = &usb_device->dev; |
| 688 | u16 hi, mi, lo, link; |
| 689 | u8 hw_addr[6]; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 690 | |
Christian Gromm | ffd069e | 2020-05-27 11:06:20 +0200 | [diff] [blame] | 691 | if (drci_rd_reg(usb_device, DRCI_REG_HW_ADDR_HI, &hi)) { |
Andrey Shvetsov | f28e6cd | 2016-10-04 17:10:19 +0200 | [diff] [blame] | 692 | dev_err(dev, "Vendor request 'hw_addr_hi' failed\n"); |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 693 | return; |
Andrey Shvetsov | f28e6cd | 2016-10-04 17:10:19 +0200 | [diff] [blame] | 694 | } |
| 695 | |
Christian Gromm | ffd069e | 2020-05-27 11:06:20 +0200 | [diff] [blame] | 696 | if (drci_rd_reg(usb_device, DRCI_REG_HW_ADDR_MI, &mi)) { |
Andrey Shvetsov | f28e6cd | 2016-10-04 17:10:19 +0200 | [diff] [blame] | 697 | dev_err(dev, "Vendor request 'hw_addr_mid' failed\n"); |
| 698 | return; |
| 699 | } |
| 700 | |
Christian Gromm | ffd069e | 2020-05-27 11:06:20 +0200 | [diff] [blame] | 701 | if (drci_rd_reg(usb_device, DRCI_REG_HW_ADDR_LO, &lo)) { |
Andrey Shvetsov | f28e6cd | 2016-10-04 17:10:19 +0200 | [diff] [blame] | 702 | dev_err(dev, "Vendor request 'hw_addr_low' failed\n"); |
| 703 | return; |
| 704 | } |
| 705 | |
Christian Gromm | ffd069e | 2020-05-27 11:06:20 +0200 | [diff] [blame] | 706 | if (drci_rd_reg(usb_device, DRCI_REG_NI_STATE, &link)) { |
Andrey Shvetsov | f28e6cd | 2016-10-04 17:10:19 +0200 | [diff] [blame] | 707 | dev_err(dev, "Vendor request 'link status' failed\n"); |
| 708 | return; |
| 709 | } |
| 710 | |
| 711 | hw_addr[0] = hi >> 8; |
| 712 | hw_addr[1] = hi; |
| 713 | hw_addr[2] = mi >> 8; |
| 714 | hw_addr[3] = mi; |
| 715 | hw_addr[4] = lo >> 8; |
| 716 | hw_addr[5] = lo; |
| 717 | |
Andrey Shvetsov | d35bbfa | 2017-05-12 12:59:54 +0200 | [diff] [blame] | 718 | if (mdev->on_netinfo) |
| 719 | mdev->on_netinfo(&mdev->iface, link, hw_addr); |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | /** |
| 723 | * wq_clear_halt - work queue function |
| 724 | * @wq_obj: work_struct object to execute |
| 725 | * |
| 726 | * This sends a clear_halt to the given USB pipe. |
| 727 | */ |
| 728 | static void wq_clear_halt(struct work_struct *wq_obj) |
| 729 | { |
Christian Gromm | cc28983 | 2016-08-19 11:12:47 +0200 | [diff] [blame] | 730 | struct clear_hold_work *clear_work = to_clear_hold_work(wq_obj); |
| 731 | struct most_dev *mdev = clear_work->mdev; |
| 732 | unsigned int channel = clear_work->channel; |
| 733 | int pipe = clear_work->pipe; |
Christian Gromm | 1fd4fb8 | 2020-05-27 11:06:24 +0200 | [diff] [blame] | 734 | int snd_pipe; |
| 735 | int peer; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 736 | |
Christian Gromm | cc28983 | 2016-08-19 11:12:47 +0200 | [diff] [blame] | 737 | mutex_lock(&mdev->io_mutex); |
Christian Gromm | bf9503f | 2016-08-19 11:12:54 +0200 | [diff] [blame] | 738 | most_stop_enqueue(&mdev->iface, channel); |
Andrey Shvetsov | 3a54200 | 2016-10-04 17:10:21 +0200 | [diff] [blame] | 739 | usb_kill_anchored_urbs(&mdev->busy_urbs[channel]); |
Christian Gromm | cc28983 | 2016-08-19 11:12:47 +0200 | [diff] [blame] | 740 | if (usb_clear_halt(mdev->usb_device, pipe)) |
Christian Gromm | 59ed048 | 2015-07-30 18:19:41 +0200 | [diff] [blame] | 741 | dev_warn(&mdev->usb_device->dev, "Failed to reset endpoint.\n"); |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 742 | |
Christian Gromm | 8f20f2d | 2017-11-21 15:05:15 +0100 | [diff] [blame] | 743 | /* If the functional Stall condition has been set on an |
| 744 | * asynchronous rx channel, we need to clear the tx channel |
| 745 | * too, since the hardware runs its clean-up sequence on both |
| 746 | * channels, as they are physically one on the network. |
| 747 | * |
| 748 | * The USB interface that exposes the asynchronous channels |
| 749 | * contains always two endpoints, and two only. |
| 750 | */ |
| 751 | if (mdev->conf[channel].data_type == MOST_CH_ASYNC && |
| 752 | mdev->conf[channel].direction == MOST_CH_RX) { |
Christian Gromm | 1fd4fb8 | 2020-05-27 11:06:24 +0200 | [diff] [blame] | 753 | if (channel == 0) |
| 754 | peer = 1; |
| 755 | else |
| 756 | peer = 0; |
| 757 | snd_pipe = usb_sndbulkpipe(mdev->usb_device, |
| 758 | mdev->ep_address[peer]); |
Christian Gromm | 8f20f2d | 2017-11-21 15:05:15 +0100 | [diff] [blame] | 759 | usb_clear_halt(mdev->usb_device, snd_pipe); |
| 760 | } |
Christian Gromm | 879c93fe | 2016-08-19 11:12:51 +0200 | [diff] [blame] | 761 | mdev->is_channel_healthy[channel] = true; |
Christian Gromm | 72df4a5 | 2016-08-19 11:12:48 +0200 | [diff] [blame] | 762 | most_resume_enqueue(&mdev->iface, channel); |
Christian Gromm | cc28983 | 2016-08-19 11:12:47 +0200 | [diff] [blame] | 763 | mutex_unlock(&mdev->io_mutex); |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | /** |
| 767 | * hdm_usb_fops - file operation table for USB driver |
| 768 | */ |
| 769 | static const struct file_operations hdm_usb_fops = { |
| 770 | .owner = THIS_MODULE, |
| 771 | }; |
| 772 | |
| 773 | /** |
| 774 | * usb_device_id - ID table for HCD device probing |
| 775 | */ |
Arvind Yadav | 27acb55 | 2017-08-08 22:53:13 +0530 | [diff] [blame] | 776 | static const struct usb_device_id usbid[] = { |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 777 | { USB_DEVICE(USB_VENDOR_ID_SMSC, USB_DEV_ID_BRDG), }, |
Christian Gromm | 654f7ec | 2016-08-19 11:12:50 +0200 | [diff] [blame] | 778 | { USB_DEVICE(USB_VENDOR_ID_SMSC, USB_DEV_ID_OS81118), }, |
Christian Gromm | b50762e | 2016-08-19 11:12:49 +0200 | [diff] [blame] | 779 | { USB_DEVICE(USB_VENDOR_ID_SMSC, USB_DEV_ID_OS81119), }, |
Christian Gromm | 5bf9bd8 | 2016-08-19 11:13:01 +0200 | [diff] [blame] | 780 | { USB_DEVICE(USB_VENDOR_ID_SMSC, USB_DEV_ID_OS81210), }, |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 781 | { } /* Terminating entry */ |
| 782 | }; |
| 783 | |
Christian Gromm | a747b42 | 2016-09-09 15:25:38 +0200 | [diff] [blame] | 784 | struct regs { |
| 785 | const char *name; |
| 786 | u16 reg; |
| 787 | }; |
| 788 | |
| 789 | static const struct regs ro_regs[] = { |
| 790 | { "ni_state", DRCI_REG_NI_STATE }, |
| 791 | { "packet_bandwidth", DRCI_REG_PACKET_BW }, |
| 792 | { "node_address", DRCI_REG_NODE_ADDR }, |
| 793 | { "node_position", DRCI_REG_NODE_POS }, |
| 794 | }; |
| 795 | |
| 796 | static const struct regs rw_regs[] = { |
| 797 | { "mep_filter", DRCI_REG_MEP_FILTER }, |
| 798 | { "mep_hash0", DRCI_REG_HASH_TBL0 }, |
| 799 | { "mep_hash1", DRCI_REG_HASH_TBL1 }, |
| 800 | { "mep_hash2", DRCI_REG_HASH_TBL2 }, |
| 801 | { "mep_hash3", DRCI_REG_HASH_TBL3 }, |
| 802 | { "mep_eui48_hi", DRCI_REG_HW_ADDR_HI }, |
| 803 | { "mep_eui48_mi", DRCI_REG_HW_ADDR_MI }, |
| 804 | { "mep_eui48_lo", DRCI_REG_HW_ADDR_LO }, |
| 805 | }; |
| 806 | |
| 807 | static int get_stat_reg_addr(const struct regs *regs, int size, |
| 808 | const char *name, u16 *reg_addr) |
| 809 | { |
| 810 | int i; |
| 811 | |
| 812 | for (i = 0; i < size; i++) { |
Christian Gromm | 549d2db | 2020-05-27 11:06:27 +0200 | [diff] [blame] | 813 | if (sysfs_streq(name, regs[i].name)) { |
Christian Gromm | a747b42 | 2016-09-09 15:25:38 +0200 | [diff] [blame] | 814 | *reg_addr = regs[i].reg; |
| 815 | return 0; |
| 816 | } |
| 817 | } |
Christian Gromm | f470a5b | 2020-05-27 11:06:25 +0200 | [diff] [blame] | 818 | return -EINVAL; |
Christian Gromm | a747b42 | 2016-09-09 15:25:38 +0200 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | #define get_static_reg_addr(regs, name, reg_addr) \ |
| 822 | get_stat_reg_addr(regs, ARRAY_SIZE(regs), name, reg_addr) |
| 823 | |
Christian Gromm | 3d9c54b | 2017-11-21 15:05:24 +0100 | [diff] [blame] | 824 | static ssize_t value_show(struct device *dev, struct device_attribute *attr, |
Christian Gromm | 4d5f022 | 2017-11-21 15:04:43 +0100 | [diff] [blame] | 825 | char *buf) |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 826 | { |
Christian Gromm | 98a3c4d | 2016-09-09 15:25:43 +0200 | [diff] [blame] | 827 | const char *name = attr->attr.name; |
Christian Gromm | 4d5f022 | 2017-11-21 15:04:43 +0100 | [diff] [blame] | 828 | struct most_dci_obj *dci_obj = to_dci_obj(dev); |
Christian Gromm | 0e9b9d0 | 2016-09-09 15:25:40 +0200 | [diff] [blame] | 829 | u16 val; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 830 | u16 reg_addr; |
| 831 | int err; |
| 832 | |
Christian Gromm | 549d2db | 2020-05-27 11:06:27 +0200 | [diff] [blame] | 833 | if (sysfs_streq(name, "arb_address")) |
Jing Yao | cd455eb | 2021-11-10 02:53:41 +0000 | [diff] [blame] | 834 | return sysfs_emit(buf, "%04x\n", dci_obj->reg_addr); |
Christian Gromm | 98a3c4d | 2016-09-09 15:25:43 +0200 | [diff] [blame] | 835 | |
Christian Gromm | 549d2db | 2020-05-27 11:06:27 +0200 | [diff] [blame] | 836 | if (sysfs_streq(name, "arb_value")) |
Christian Gromm | c055464 | 2016-09-09 15:25:35 +0200 | [diff] [blame] | 837 | reg_addr = dci_obj->reg_addr; |
Christian Gromm | 98a3c4d | 2016-09-09 15:25:43 +0200 | [diff] [blame] | 838 | else if (get_static_reg_addr(ro_regs, name, ®_addr) && |
| 839 | get_static_reg_addr(rw_regs, name, ®_addr)) |
Christian Gromm | f470a5b | 2020-05-27 11:06:25 +0200 | [diff] [blame] | 840 | return -EINVAL; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 841 | |
Christian Gromm | 0e9b9d0 | 2016-09-09 15:25:40 +0200 | [diff] [blame] | 842 | err = drci_rd_reg(dci_obj->usb_device, reg_addr, &val); |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 843 | if (err < 0) |
| 844 | return err; |
| 845 | |
Jing Yao | cd455eb | 2021-11-10 02:53:41 +0000 | [diff] [blame] | 846 | return sysfs_emit(buf, "%04x\n", val); |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 847 | } |
| 848 | |
Christian Gromm | 3d9c54b | 2017-11-21 15:05:24 +0100 | [diff] [blame] | 849 | static ssize_t value_store(struct device *dev, struct device_attribute *attr, |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 850 | const char *buf, size_t count) |
| 851 | { |
Christian Gromm | f1b9a84 | 2015-09-28 17:18:39 +0200 | [diff] [blame] | 852 | u16 val; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 853 | u16 reg_addr; |
Christian Gromm | 98a3c4d | 2016-09-09 15:25:43 +0200 | [diff] [blame] | 854 | const char *name = attr->attr.name; |
Christian Gromm | 4d5f022 | 2017-11-21 15:04:43 +0100 | [diff] [blame] | 855 | struct most_dci_obj *dci_obj = to_dci_obj(dev); |
Andrey Shvetsov | e33269f | 2016-10-25 17:44:21 +0200 | [diff] [blame] | 856 | struct usb_device *usb_dev = dci_obj->usb_device; |
Christian Gromm | a0dbe1b | 2020-05-27 11:06:19 +0200 | [diff] [blame] | 857 | int err; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 858 | |
Christian Gromm | a0dbe1b | 2020-05-27 11:06:19 +0200 | [diff] [blame] | 859 | err = kstrtou16(buf, 16, &val); |
Christian Gromm | c055464 | 2016-09-09 15:25:35 +0200 | [diff] [blame] | 860 | if (err) |
| 861 | return err; |
| 862 | |
Christian Gromm | 549d2db | 2020-05-27 11:06:27 +0200 | [diff] [blame] | 863 | if (sysfs_streq(name, "arb_address")) { |
Christian Gromm | c055464 | 2016-09-09 15:25:35 +0200 | [diff] [blame] | 864 | dci_obj->reg_addr = val; |
| 865 | return count; |
| 866 | } |
Christian Gromm | 98a3c4d | 2016-09-09 15:25:43 +0200 | [diff] [blame] | 867 | |
Christian Gromm | 549d2db | 2020-05-27 11:06:27 +0200 | [diff] [blame] | 868 | if (sysfs_streq(name, "arb_value")) |
Andrey Shvetsov | e33269f | 2016-10-25 17:44:21 +0200 | [diff] [blame] | 869 | err = drci_wr_reg(usb_dev, dci_obj->reg_addr, val); |
Christian Gromm | 549d2db | 2020-05-27 11:06:27 +0200 | [diff] [blame] | 870 | else if (sysfs_streq(name, "sync_ep")) |
Andrey Shvetsov | e33269f | 2016-10-25 17:44:21 +0200 | [diff] [blame] | 871 | err = start_sync_ep(usb_dev, val); |
Christian Gromm | b2e8aa5 | 2017-04-07 15:38:40 +0200 | [diff] [blame] | 872 | else if (!get_static_reg_addr(rw_regs, name, ®_addr)) |
Andrey Shvetsov | e33269f | 2016-10-25 17:44:21 +0200 | [diff] [blame] | 873 | err = drci_wr_reg(usb_dev, reg_addr, val); |
| 874 | else |
Christian Gromm | f470a5b | 2020-05-27 11:06:25 +0200 | [diff] [blame] | 875 | return -EINVAL; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 876 | |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 877 | if (err < 0) |
| 878 | return err; |
| 879 | |
| 880 | return count; |
| 881 | } |
| 882 | |
Christian Gromm | f15e3ad | 2017-11-27 15:20:06 +0100 | [diff] [blame] | 883 | static DEVICE_ATTR(ni_state, 0444, value_show, NULL); |
| 884 | static DEVICE_ATTR(packet_bandwidth, 0444, value_show, NULL); |
| 885 | static DEVICE_ATTR(node_address, 0444, value_show, NULL); |
| 886 | static DEVICE_ATTR(node_position, 0444, value_show, NULL); |
| 887 | static DEVICE_ATTR(sync_ep, 0200, NULL, value_store); |
| 888 | static DEVICE_ATTR(mep_filter, 0644, value_show, value_store); |
| 889 | static DEVICE_ATTR(mep_hash0, 0644, value_show, value_store); |
| 890 | static DEVICE_ATTR(mep_hash1, 0644, value_show, value_store); |
| 891 | static DEVICE_ATTR(mep_hash2, 0644, value_show, value_store); |
| 892 | static DEVICE_ATTR(mep_hash3, 0644, value_show, value_store); |
| 893 | static DEVICE_ATTR(mep_eui48_hi, 0644, value_show, value_store); |
| 894 | static DEVICE_ATTR(mep_eui48_mi, 0644, value_show, value_store); |
| 895 | static DEVICE_ATTR(mep_eui48_lo, 0644, value_show, value_store); |
| 896 | static DEVICE_ATTR(arb_address, 0644, value_show, value_store); |
| 897 | static DEVICE_ATTR(arb_value, 0644, value_show, value_store); |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 898 | |
Christian Gromm | 4d5f022 | 2017-11-21 15:04:43 +0100 | [diff] [blame] | 899 | static struct attribute *dci_attrs[] = { |
| 900 | &dev_attr_ni_state.attr, |
| 901 | &dev_attr_packet_bandwidth.attr, |
| 902 | &dev_attr_node_address.attr, |
| 903 | &dev_attr_node_position.attr, |
| 904 | &dev_attr_sync_ep.attr, |
| 905 | &dev_attr_mep_filter.attr, |
| 906 | &dev_attr_mep_hash0.attr, |
| 907 | &dev_attr_mep_hash1.attr, |
| 908 | &dev_attr_mep_hash2.attr, |
| 909 | &dev_attr_mep_hash3.attr, |
| 910 | &dev_attr_mep_eui48_hi.attr, |
| 911 | &dev_attr_mep_eui48_mi.attr, |
| 912 | &dev_attr_mep_eui48_lo.attr, |
| 913 | &dev_attr_arb_address.attr, |
| 914 | &dev_attr_arb_value.attr, |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 915 | NULL, |
| 916 | }; |
| 917 | |
Christian Gromm | dfeb938 | 2020-05-15 11:21:04 +0200 | [diff] [blame] | 918 | ATTRIBUTE_GROUPS(dci); |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 919 | |
Christian Gromm | 869d3ac | 2018-09-28 23:37:19 +0200 | [diff] [blame] | 920 | static void release_dci(struct device *dev) |
| 921 | { |
| 922 | struct most_dci_obj *dci = to_dci_obj(dev); |
| 923 | |
Christian Gromm | f1f4823 | 2020-05-27 11:06:26 +0200 | [diff] [blame] | 924 | put_device(dev->parent); |
Christian Gromm | 869d3ac | 2018-09-28 23:37:19 +0200 | [diff] [blame] | 925 | kfree(dci); |
| 926 | } |
| 927 | |
Christian Gromm | 723de0f | 2020-01-23 16:38:17 +0100 | [diff] [blame] | 928 | static void release_mdev(struct device *dev) |
| 929 | { |
| 930 | struct most_dev *mdev = to_mdev_from_dev(dev); |
| 931 | |
| 932 | kfree(mdev); |
| 933 | } |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 934 | /** |
| 935 | * hdm_probe - probe function of USB device driver |
| 936 | * @interface: Interface of the attached USB device |
| 937 | * @id: Pointer to the USB ID table. |
| 938 | * |
| 939 | * This allocates and initializes the device instance, adds the new |
| 940 | * entry to the internal list, scans the USB descriptors and registers |
| 941 | * the interface with the core. |
| 942 | * Additionally, the DCI objects are created and the hardware is sync'd. |
| 943 | * |
| 944 | * Return 0 on success. In case of an error a negative number is returned. |
| 945 | */ |
| 946 | static int |
| 947 | hdm_probe(struct usb_interface *interface, const struct usb_device_id *id) |
| 948 | { |
Christian Gromm | 089612f | 2016-08-19 11:12:59 +0200 | [diff] [blame] | 949 | struct usb_host_interface *usb_iface_desc = interface->cur_altsetting; |
| 950 | struct usb_device *usb_dev = interface_to_usbdev(interface); |
| 951 | struct device *dev = &usb_dev->dev; |
Christian Gromm | a0dbe1b | 2020-05-27 11:06:19 +0200 | [diff] [blame] | 952 | struct most_dev *mdev; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 953 | unsigned int i; |
| 954 | unsigned int num_endpoints; |
| 955 | struct most_channel_capability *tmp_cap; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 956 | struct usb_endpoint_descriptor *ep_desc; |
Christian Gromm | c1a57be | 2020-05-15 11:21:01 +0200 | [diff] [blame] | 957 | int ret = -ENOMEM; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 958 | |
Christian Gromm | a0dbe1b | 2020-05-27 11:06:19 +0200 | [diff] [blame] | 959 | mdev = kzalloc(sizeof(*mdev), GFP_KERNEL); |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 960 | if (!mdev) |
Christian Gromm | c1a57be | 2020-05-15 11:21:01 +0200 | [diff] [blame] | 961 | return -ENOMEM; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 962 | |
| 963 | usb_set_intfdata(interface, mdev); |
| 964 | num_endpoints = usb_iface_desc->desc.bNumEndpoints; |
Christian Gromm | c1a57be | 2020-05-15 11:21:01 +0200 | [diff] [blame] | 965 | if (num_endpoints > MAX_NUM_ENDPOINTS) { |
| 966 | kfree(mdev); |
| 967 | return -EINVAL; |
| 968 | } |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 969 | mutex_init(&mdev->io_mutex); |
| 970 | INIT_WORK(&mdev->poll_work_obj, wq_netinfo); |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 971 | timer_setup(&mdev->link_stat_timer, link_stat_timer_handler, 0); |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 972 | |
| 973 | mdev->usb_device = usb_dev; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 974 | mdev->link_stat_timer.expires = jiffies + (2 * HZ); |
| 975 | |
| 976 | mdev->iface.mod = hdm_usb_fops.owner; |
Christian Gromm | 723de0f | 2020-01-23 16:38:17 +0100 | [diff] [blame] | 977 | mdev->iface.dev = &mdev->dev; |
Christian Gromm | 69c90cf1 | 2018-05-08 11:45:04 +0200 | [diff] [blame] | 978 | mdev->iface.driver_dev = &interface->dev; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 979 | mdev->iface.interface = ITYPE_USB; |
| 980 | mdev->iface.configure = hdm_configure_channel; |
| 981 | mdev->iface.request_netinfo = hdm_request_netinfo; |
| 982 | mdev->iface.enqueue = hdm_enqueue; |
| 983 | mdev->iface.poison_channel = hdm_poison_channel; |
Christian Gromm | 3598cec | 2018-05-08 11:45:03 +0200 | [diff] [blame] | 984 | mdev->iface.dma_alloc = hdm_dma_alloc; |
| 985 | mdev->iface.dma_free = hdm_dma_free; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 986 | mdev->iface.description = mdev->description; |
| 987 | mdev->iface.num_channels = num_endpoints; |
| 988 | |
| 989 | snprintf(mdev->description, sizeof(mdev->description), |
Christian Gromm | 5b082c2 | 2019-04-03 15:19:50 +0200 | [diff] [blame] | 990 | "%d-%s:%d.%d", |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 991 | usb_dev->bus->busnum, |
| 992 | usb_dev->devpath, |
| 993 | usb_dev->config->desc.bConfigurationValue, |
| 994 | usb_iface_desc->desc.bInterfaceNumber); |
| 995 | |
Christian Gromm | 723de0f | 2020-01-23 16:38:17 +0100 | [diff] [blame] | 996 | mdev->dev.init_name = mdev->description; |
| 997 | mdev->dev.parent = &interface->dev; |
| 998 | mdev->dev.release = release_mdev; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 999 | mdev->conf = kcalloc(num_endpoints, sizeof(*mdev->conf), GFP_KERNEL); |
| 1000 | if (!mdev->conf) |
Christian Gromm | bddd3c2 | 2018-09-21 11:28:51 +0200 | [diff] [blame] | 1001 | goto err_free_mdev; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 1002 | |
| 1003 | mdev->cap = kcalloc(num_endpoints, sizeof(*mdev->cap), GFP_KERNEL); |
| 1004 | if (!mdev->cap) |
Christian Gromm | bddd3c2 | 2018-09-21 11:28:51 +0200 | [diff] [blame] | 1005 | goto err_free_conf; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 1006 | |
| 1007 | mdev->iface.channel_vector = mdev->cap; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 1008 | mdev->ep_address = |
| 1009 | kcalloc(num_endpoints, sizeof(*mdev->ep_address), GFP_KERNEL); |
| 1010 | if (!mdev->ep_address) |
Christian Gromm | bddd3c2 | 2018-09-21 11:28:51 +0200 | [diff] [blame] | 1011 | goto err_free_cap; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 1012 | |
Christian Gromm | 27e6245 | 2016-09-19 17:40:22 +0200 | [diff] [blame] | 1013 | mdev->busy_urbs = |
| 1014 | kcalloc(num_endpoints, sizeof(*mdev->busy_urbs), GFP_KERNEL); |
| 1015 | if (!mdev->busy_urbs) |
Christian Gromm | bddd3c2 | 2018-09-21 11:28:51 +0200 | [diff] [blame] | 1016 | goto err_free_ep_address; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 1017 | |
| 1018 | tmp_cap = mdev->cap; |
| 1019 | for (i = 0; i < num_endpoints; i++) { |
| 1020 | ep_desc = &usb_iface_desc->endpoint[i].desc; |
| 1021 | mdev->ep_address[i] = ep_desc->bEndpointAddress; |
| 1022 | mdev->padding_active[i] = false; |
| 1023 | mdev->is_channel_healthy[i] = true; |
| 1024 | |
| 1025 | snprintf(&mdev->suffix[i][0], MAX_SUFFIX_LEN, "ep%02x", |
| 1026 | mdev->ep_address[i]); |
| 1027 | |
| 1028 | tmp_cap->name_suffix = &mdev->suffix[i][0]; |
| 1029 | tmp_cap->buffer_size_packet = MAX_BUF_SIZE; |
| 1030 | tmp_cap->buffer_size_streaming = MAX_BUF_SIZE; |
| 1031 | tmp_cap->num_buffers_packet = BUF_CHAIN_SIZE; |
| 1032 | tmp_cap->num_buffers_streaming = BUF_CHAIN_SIZE; |
| 1033 | tmp_cap->data_type = MOST_CH_CONTROL | MOST_CH_ASYNC | |
Andrey Shvetsov | 0540609 | 2016-09-21 14:49:10 +0200 | [diff] [blame] | 1034 | MOST_CH_ISOC | MOST_CH_SYNC; |
Sandhya Bankar | afd14ce | 2016-03-06 15:50:35 +0530 | [diff] [blame] | 1035 | if (usb_endpoint_dir_in(ep_desc)) |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 1036 | tmp_cap->direction = MOST_CH_RX; |
| 1037 | else |
| 1038 | tmp_cap->direction = MOST_CH_TX; |
| 1039 | tmp_cap++; |
Christian Gromm | 27e6245 | 2016-09-19 17:40:22 +0200 | [diff] [blame] | 1040 | init_usb_anchor(&mdev->busy_urbs[i]); |
Christian Gromm | 88d1878 | 2016-09-19 17:40:25 +0200 | [diff] [blame] | 1041 | spin_lock_init(&mdev->channel_lock[i]); |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 1042 | } |
Christian Gromm | 3dcf93fe | 2020-05-15 11:21:02 +0200 | [diff] [blame] | 1043 | dev_dbg(dev, "claimed gadget: Vendor=%4.4x ProdID=%4.4x Bus=%02x Device=%02x\n", |
| 1044 | le16_to_cpu(usb_dev->descriptor.idVendor), |
| 1045 | le16_to_cpu(usb_dev->descriptor.idProduct), |
| 1046 | usb_dev->bus->busnum, |
| 1047 | usb_dev->devnum); |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 1048 | |
Christian Gromm | 3dcf93fe | 2020-05-15 11:21:02 +0200 | [diff] [blame] | 1049 | dev_dbg(dev, "device path: /sys/bus/usb/devices/%d-%s:%d.%d\n", |
| 1050 | usb_dev->bus->busnum, |
| 1051 | usb_dev->devpath, |
| 1052 | usb_dev->config->desc.bConfigurationValue, |
| 1053 | usb_iface_desc->desc.bInterfaceNumber); |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 1054 | |
Christian Gromm | 4d5f022 | 2017-11-21 15:04:43 +0100 | [diff] [blame] | 1055 | ret = most_register_interface(&mdev->iface); |
| 1056 | if (ret) |
Christian Gromm | bddd3c2 | 2018-09-21 11:28:51 +0200 | [diff] [blame] | 1057 | goto err_free_busy_urbs; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 1058 | |
| 1059 | mutex_lock(&mdev->io_mutex); |
Christian Gromm | 654f7ec | 2016-08-19 11:12:50 +0200 | [diff] [blame] | 1060 | if (le16_to_cpu(usb_dev->descriptor.idProduct) == USB_DEV_ID_OS81118 || |
Christian Gromm | 5bf9bd8 | 2016-08-19 11:13:01 +0200 | [diff] [blame] | 1061 | le16_to_cpu(usb_dev->descriptor.idProduct) == USB_DEV_ID_OS81119 || |
| 1062 | le16_to_cpu(usb_dev->descriptor.idProduct) == USB_DEV_ID_OS81210) { |
Christian Gromm | 4d5f022 | 2017-11-21 15:04:43 +0100 | [diff] [blame] | 1063 | mdev->dci = kzalloc(sizeof(*mdev->dci), GFP_KERNEL); |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 1064 | if (!mdev->dci) { |
| 1065 | mutex_unlock(&mdev->io_mutex); |
| 1066 | most_deregister_interface(&mdev->iface); |
| 1067 | ret = -ENOMEM; |
Christian Gromm | bddd3c2 | 2018-09-21 11:28:51 +0200 | [diff] [blame] | 1068 | goto err_free_busy_urbs; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 1069 | } |
| 1070 | |
Christian Gromm | 4d5f022 | 2017-11-21 15:04:43 +0100 | [diff] [blame] | 1071 | mdev->dci->dev.init_name = "dci"; |
Christian Gromm | 723de0f | 2020-01-23 16:38:17 +0100 | [diff] [blame] | 1072 | mdev->dci->dev.parent = get_device(mdev->iface.dev); |
Christian Gromm | dfeb938 | 2020-05-15 11:21:04 +0200 | [diff] [blame] | 1073 | mdev->dci->dev.groups = dci_groups; |
Christian Gromm | 869d3ac | 2018-09-28 23:37:19 +0200 | [diff] [blame] | 1074 | mdev->dci->dev.release = release_dci; |
Christian Gromm | 4d5f022 | 2017-11-21 15:04:43 +0100 | [diff] [blame] | 1075 | if (device_register(&mdev->dci->dev)) { |
| 1076 | mutex_unlock(&mdev->io_mutex); |
| 1077 | most_deregister_interface(&mdev->iface); |
| 1078 | ret = -ENOMEM; |
Christian Gromm | bddd3c2 | 2018-09-21 11:28:51 +0200 | [diff] [blame] | 1079 | goto err_free_dci; |
Christian Gromm | 4d5f022 | 2017-11-21 15:04:43 +0100 | [diff] [blame] | 1080 | } |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 1081 | mdev->dci->usb_device = mdev->usb_device; |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 1082 | } |
| 1083 | mutex_unlock(&mdev->io_mutex); |
| 1084 | return 0; |
Christian Gromm | bddd3c2 | 2018-09-21 11:28:51 +0200 | [diff] [blame] | 1085 | err_free_dci: |
Christian Gromm | 723de0f | 2020-01-23 16:38:17 +0100 | [diff] [blame] | 1086 | put_device(&mdev->dci->dev); |
Christian Gromm | bddd3c2 | 2018-09-21 11:28:51 +0200 | [diff] [blame] | 1087 | err_free_busy_urbs: |
Christian Gromm | 27e6245 | 2016-09-19 17:40:22 +0200 | [diff] [blame] | 1088 | kfree(mdev->busy_urbs); |
Christian Gromm | bddd3c2 | 2018-09-21 11:28:51 +0200 | [diff] [blame] | 1089 | err_free_ep_address: |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 1090 | kfree(mdev->ep_address); |
Christian Gromm | bddd3c2 | 2018-09-21 11:28:51 +0200 | [diff] [blame] | 1091 | err_free_cap: |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 1092 | kfree(mdev->cap); |
Christian Gromm | bddd3c2 | 2018-09-21 11:28:51 +0200 | [diff] [blame] | 1093 | err_free_conf: |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 1094 | kfree(mdev->conf); |
Christian Gromm | bddd3c2 | 2018-09-21 11:28:51 +0200 | [diff] [blame] | 1095 | err_free_mdev: |
Christian Gromm | 723de0f | 2020-01-23 16:38:17 +0100 | [diff] [blame] | 1096 | put_device(&mdev->dev); |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 1097 | return ret; |
| 1098 | } |
| 1099 | |
| 1100 | /** |
| 1101 | * hdm_disconnect - disconnect function of USB device driver |
| 1102 | * @interface: Interface of the attached USB device |
| 1103 | * |
| 1104 | * This deregisters the interface with the core, removes the kernel timer |
| 1105 | * and frees resources. |
| 1106 | * |
| 1107 | * Context: hub kernel thread |
| 1108 | */ |
| 1109 | static void hdm_disconnect(struct usb_interface *interface) |
| 1110 | { |
Christian Gromm | 089612f | 2016-08-19 11:12:59 +0200 | [diff] [blame] | 1111 | struct most_dev *mdev = usb_get_intfdata(interface); |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 1112 | |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 1113 | mutex_lock(&mdev->io_mutex); |
| 1114 | usb_set_intfdata(interface, NULL); |
| 1115 | mdev->usb_device = NULL; |
| 1116 | mutex_unlock(&mdev->io_mutex); |
| 1117 | |
| 1118 | del_timer_sync(&mdev->link_stat_timer); |
| 1119 | cancel_work_sync(&mdev->poll_work_obj); |
| 1120 | |
Christian Gromm | fc15799 | 2020-01-23 16:38:23 +0100 | [diff] [blame] | 1121 | if (mdev->dci) |
| 1122 | device_unregister(&mdev->dci->dev); |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 1123 | most_deregister_interface(&mdev->iface); |
| 1124 | |
Christian Gromm | 27e6245 | 2016-09-19 17:40:22 +0200 | [diff] [blame] | 1125 | kfree(mdev->busy_urbs); |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 1126 | kfree(mdev->cap); |
| 1127 | kfree(mdev->conf); |
| 1128 | kfree(mdev->ep_address); |
Christian Gromm | f1f4823 | 2020-05-27 11:06:26 +0200 | [diff] [blame] | 1129 | put_device(&mdev->dci->dev); |
Christian Gromm | 723de0f | 2020-01-23 16:38:17 +0100 | [diff] [blame] | 1130 | put_device(&mdev->dev); |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 1131 | } |
| 1132 | |
Christian Gromm | 08e1b42 | 2020-05-05 14:14:52 +0200 | [diff] [blame] | 1133 | static int hdm_suspend(struct usb_interface *interface, pm_message_t message) |
| 1134 | { |
| 1135 | struct most_dev *mdev = usb_get_intfdata(interface); |
| 1136 | int i; |
| 1137 | |
| 1138 | mutex_lock(&mdev->io_mutex); |
| 1139 | for (i = 0; i < mdev->iface.num_channels; i++) { |
| 1140 | most_stop_enqueue(&mdev->iface, i); |
| 1141 | usb_kill_anchored_urbs(&mdev->busy_urbs[i]); |
| 1142 | } |
| 1143 | mutex_unlock(&mdev->io_mutex); |
| 1144 | return 0; |
| 1145 | } |
| 1146 | |
| 1147 | static int hdm_resume(struct usb_interface *interface) |
| 1148 | { |
| 1149 | struct most_dev *mdev = usb_get_intfdata(interface); |
| 1150 | int i; |
| 1151 | |
| 1152 | mutex_lock(&mdev->io_mutex); |
| 1153 | for (i = 0; i < mdev->iface.num_channels; i++) |
| 1154 | most_resume_enqueue(&mdev->iface, i); |
| 1155 | mutex_unlock(&mdev->io_mutex); |
| 1156 | return 0; |
| 1157 | } |
| 1158 | |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 1159 | static struct usb_driver hdm_usb = { |
| 1160 | .name = "hdm_usb", |
| 1161 | .id_table = usbid, |
| 1162 | .probe = hdm_probe, |
| 1163 | .disconnect = hdm_disconnect, |
Christian Gromm | 08e1b42 | 2020-05-05 14:14:52 +0200 | [diff] [blame] | 1164 | .resume = hdm_resume, |
| 1165 | .suspend = hdm_suspend, |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 1166 | }; |
| 1167 | |
Alex Briskin | b9d7adc | 2017-08-27 08:22:10 +0300 | [diff] [blame] | 1168 | module_usb_driver(hdm_usb); |
Christian Gromm | a4198cd | 2015-07-24 16:11:55 +0200 | [diff] [blame] | 1169 | MODULE_LICENSE("GPL"); |
| 1170 | MODULE_AUTHOR("Christian Gromm <christian.gromm@microchip.com>"); |
| 1171 | MODULE_DESCRIPTION("HDM_4_USB"); |