Commit | Line | Data |
---|---|---|
1a79f22d | 1 | // SPDX-License-Identifier: GPL-2.0 |
a4198cdf | 2 | /* |
6e01fc77 | 3 | * usb.c - Hardware dependent module for USB |
a4198cdf CG |
4 | * |
5 | * Copyright (C) 2013-2015 Microchip Technology Germany II GmbH & Co. KG | |
a4198cdf CG |
6 | */ |
7 | ||
8 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
9 | #include <linux/module.h> | |
10 | #include <linux/fs.h> | |
11 | #include <linux/usb.h> | |
12 | #include <linux/slab.h> | |
13 | #include <linux/init.h> | |
14 | #include <linux/cdev.h> | |
15 | #include <linux/device.h> | |
16 | #include <linux/list.h> | |
17 | #include <linux/completion.h> | |
18 | #include <linux/mutex.h> | |
19 | #include <linux/spinlock.h> | |
20 | #include <linux/interrupt.h> | |
21 | #include <linux/workqueue.h> | |
22 | #include <linux/sysfs.h> | |
23 | #include <linux/dma-mapping.h> | |
24 | #include <linux/etherdevice.h> | |
25 | #include <linux/uaccess.h> | |
057301cd | 26 | #include "most/core.h" |
a4198cdf CG |
27 | |
28 | #define USB_MTU 512 | |
29 | #define NO_ISOCHRONOUS_URB 0 | |
30 | #define AV_PACKETS_PER_XACT 2 | |
31 | #define BUF_CHAIN_SIZE 0xFFFF | |
32 | #define MAX_NUM_ENDPOINTS 30 | |
33 | #define MAX_SUFFIX_LEN 10 | |
34 | #define MAX_STRING_LEN 80 | |
35 | #define MAX_BUF_SIZE 0xFFFF | |
a4198cdf CG |
36 | |
37 | #define USB_VENDOR_ID_SMSC 0x0424 /* VID: SMSC */ | |
38 | #define USB_DEV_ID_BRDG 0xC001 /* PID: USB Bridge */ | |
654f7ec4 | 39 | #define USB_DEV_ID_OS81118 0xCF18 /* PID: USB OS81118 */ |
b50762ea | 40 | #define USB_DEV_ID_OS81119 0xCF19 /* PID: USB OS81119 */ |
5bf9bd8d | 41 | #define USB_DEV_ID_OS81210 0xCF30 /* PID: USB OS81210 */ |
a4198cdf CG |
42 | /* DRCI Addresses */ |
43 | #define DRCI_REG_NI_STATE 0x0100 | |
44 | #define DRCI_REG_PACKET_BW 0x0101 | |
45 | #define DRCI_REG_NODE_ADDR 0x0102 | |
46 | #define DRCI_REG_NODE_POS 0x0103 | |
47 | #define DRCI_REG_MEP_FILTER 0x0140 | |
48 | #define DRCI_REG_HASH_TBL0 0x0141 | |
49 | #define DRCI_REG_HASH_TBL1 0x0142 | |
50 | #define DRCI_REG_HASH_TBL2 0x0143 | |
51 | #define DRCI_REG_HASH_TBL3 0x0144 | |
52 | #define DRCI_REG_HW_ADDR_HI 0x0145 | |
53 | #define DRCI_REG_HW_ADDR_MI 0x0146 | |
54 | #define DRCI_REG_HW_ADDR_LO 0x0147 | |
d747e8ec CG |
55 | #define DRCI_REG_BASE 0x1100 |
56 | #define DRCI_COMMAND 0x02 | |
a4198cdf CG |
57 | #define DRCI_READ_REQ 0xA0 |
58 | #define DRCI_WRITE_REQ 0xA1 | |
59 | ||
a4198cdf CG |
60 | /** |
61 | * struct most_dci_obj - Direct Communication Interface | |
62 | * @kobj:position in sysfs | |
63 | * @usb_device: pointer to the usb device | |
c0554645 | 64 | * @reg_addr: register address for arbitrary DCI access |
a4198cdf CG |
65 | */ |
66 | struct most_dci_obj { | |
4d5f022f | 67 | struct device dev; |
a4198cdf | 68 | struct usb_device *usb_device; |
c0554645 | 69 | u16 reg_addr; |
a4198cdf | 70 | }; |
9cbe5aa6 | 71 | |
4d5f022f | 72 | #define to_dci_obj(p) container_of(p, struct most_dci_obj, dev) |
a4198cdf | 73 | |
cc28983c CG |
74 | struct most_dev; |
75 | ||
76 | struct clear_hold_work { | |
77 | struct work_struct ws; | |
78 | struct most_dev *mdev; | |
79 | unsigned int channel; | |
80 | int pipe; | |
81 | }; | |
82 | ||
83 | #define to_clear_hold_work(w) container_of(w, struct clear_hold_work, ws) | |
84 | ||
a4198cdf CG |
85 | /** |
86 | * struct most_dev - holds all usb interface specific stuff | |
a4198cdf CG |
87 | * @usb_device: pointer to usb device |
88 | * @iface: hardware interface | |
89 | * @cap: channel capabilities | |
90 | * @conf: channel configuration | |
91 | * @dci: direct communication interface of hardware | |
a4198cdf | 92 | * @ep_address: endpoint address table |
a4198cdf CG |
93 | * @description: device description |
94 | * @suffix: suffix for channel name | |
88d1878b | 95 | * @channel_lock: synchronize channel access |
a4198cdf CG |
96 | * @padding_active: indicates channel uses padding |
97 | * @is_channel_healthy: health status table of each channel | |
27e6245e | 98 | * @busy_urbs: list of anchored items |
a4198cdf CG |
99 | * @io_mutex: synchronize I/O with disconnect |
100 | * @link_stat_timer: timer for link status reports | |
101 | * @poll_work_obj: work for polling link status | |
102 | */ | |
103 | struct most_dev { | |
a4198cdf CG |
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; | |
a4198cdf | 109 | u8 *ep_address; |
a4198cdf CG |
110 | char description[MAX_STRING_LEN]; |
111 | char suffix[MAX_NUM_ENDPOINTS][MAX_SUFFIX_LEN]; | |
88d1878b | 112 | spinlock_t channel_lock[MAX_NUM_ENDPOINTS]; /* sync channel access */ |
a4198cdf CG |
113 | bool padding_active[MAX_NUM_ENDPOINTS]; |
114 | bool is_channel_healthy[MAX_NUM_ENDPOINTS]; | |
cc28983c | 115 | struct clear_hold_work clear_work[MAX_NUM_ENDPOINTS]; |
27e6245e | 116 | struct usb_anchor *busy_urbs; |
a4198cdf CG |
117 | struct mutex io_mutex; |
118 | struct timer_list link_stat_timer; | |
119 | struct work_struct poll_work_obj; | |
9917b209 RE |
120 | void (*on_netinfo)(struct most_interface *most_iface, |
121 | unsigned char link_state, unsigned char *addrs); | |
a4198cdf | 122 | }; |
9cbe5aa6 | 123 | |
a4198cdf CG |
124 | #define to_mdev(d) container_of(d, struct most_dev, iface) |
125 | #define to_mdev_from_work(w) container_of(w, struct most_dev, poll_work_obj) | |
126 | ||
a4198cdf CG |
127 | static void wq_clear_halt(struct work_struct *wq_obj); |
128 | static void wq_netinfo(struct work_struct *wq_obj); | |
129 | ||
a4198cdf CG |
130 | /** |
131 | * drci_rd_reg - read a DCI register | |
132 | * @dev: usb device | |
133 | * @reg: register address | |
134 | * @buf: buffer to store data | |
135 | * | |
136 | * This is reads data from INIC's direct register communication interface | |
137 | */ | |
26370228 | 138 | static inline int drci_rd_reg(struct usb_device *dev, u16 reg, u16 *buf) |
a4198cdf | 139 | { |
26370228 | 140 | int retval; |
5a10380b | 141 | __le16 *dma_buf = kzalloc(sizeof(*dma_buf), GFP_KERNEL); |
26370228 CG |
142 | u8 req_type = USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE; |
143 | ||
144 | if (!dma_buf) | |
145 | return -ENOMEM; | |
146 | ||
147 | retval = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), | |
148 | DRCI_READ_REQ, req_type, | |
149 | 0x0000, | |
5a10380b | 150 | reg, dma_buf, sizeof(*dma_buf), 5 * HZ); |
c81c9c3e | 151 | *buf = le16_to_cpu(*dma_buf); |
26370228 CG |
152 | kfree(dma_buf); |
153 | ||
154 | return retval; | |
a4198cdf CG |
155 | } |
156 | ||
157 | /** | |
158 | * drci_wr_reg - write a DCI register | |
159 | * @dev: usb device | |
160 | * @reg: register address | |
161 | * @data: data to write | |
162 | * | |
163 | * This is writes data to INIC's direct register communication interface | |
164 | */ | |
165 | static inline int drci_wr_reg(struct usb_device *dev, u16 reg, u16 data) | |
166 | { | |
167 | return usb_control_msg(dev, | |
168 | usb_sndctrlpipe(dev, 0), | |
169 | DRCI_WRITE_REQ, | |
170 | USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, | |
171 | data, | |
172 | reg, | |
173 | NULL, | |
174 | 0, | |
175 | 5 * HZ); | |
176 | } | |
177 | ||
e33269f6 AS |
178 | static inline int start_sync_ep(struct usb_device *usb_dev, u16 ep) |
179 | { | |
180 | return drci_wr_reg(usb_dev, DRCI_REG_BASE + DRCI_COMMAND + ep * 16, 1); | |
181 | } | |
182 | ||
a4198cdf CG |
183 | /** |
184 | * get_stream_frame_size - calculate frame size of current configuration | |
185 | * @cfg: channel configuration | |
186 | */ | |
187 | static unsigned int get_stream_frame_size(struct most_channel_config *cfg) | |
188 | { | |
189 | unsigned int frame_size = 0; | |
190 | unsigned int sub_size = cfg->subbuffer_size; | |
191 | ||
192 | if (!sub_size) { | |
59ed0480 | 193 | pr_warn("Misconfig: Subbuffer size zero.\n"); |
a4198cdf CG |
194 | return frame_size; |
195 | } | |
196 | switch (cfg->data_type) { | |
0540609f | 197 | case MOST_CH_ISOC: |
a4198cdf CG |
198 | frame_size = AV_PACKETS_PER_XACT * sub_size; |
199 | break; | |
200 | case MOST_CH_SYNC: | |
201 | if (cfg->packets_per_xact == 0) { | |
59ed0480 | 202 | pr_warn("Misconfig: Packets per XACT zero\n"); |
a4198cdf | 203 | frame_size = 0; |
9deba73d | 204 | } else if (cfg->packets_per_xact == 0xFF) { |
a4198cdf | 205 | frame_size = (USB_MTU / sub_size) * sub_size; |
9deba73d | 206 | } else { |
a4198cdf | 207 | frame_size = cfg->packets_per_xact * sub_size; |
9deba73d | 208 | } |
a4198cdf CG |
209 | break; |
210 | default: | |
211 | pr_warn("Query frame size of non-streaming channel\n"); | |
212 | break; | |
213 | } | |
214 | return frame_size; | |
215 | } | |
216 | ||
217 | /** | |
218 | * hdm_poison_channel - mark buffers of this channel as invalid | |
219 | * @iface: pointer to the interface | |
220 | * @channel: channel ID | |
221 | * | |
222 | * This unlinks all URBs submitted to the HCD, | |
223 | * calls the associated completion function of the core and removes | |
224 | * them from the list. | |
225 | * | |
226 | * Returns 0 on success or error code otherwise. | |
227 | */ | |
23fe15fa | 228 | static int hdm_poison_channel(struct most_interface *iface, int channel) |
a4198cdf | 229 | { |
089612f1 | 230 | struct most_dev *mdev = to_mdev(iface); |
b24c9fe9 CG |
231 | unsigned long flags; |
232 | spinlock_t *lock; /* temp. lock */ | |
a4198cdf CG |
233 | |
234 | if (unlikely(!iface)) { | |
59ed0480 | 235 | dev_warn(&mdev->usb_device->dev, "Poison: Bad interface.\n"); |
a4198cdf CG |
236 | return -EIO; |
237 | } | |
dd53ecba | 238 | if (unlikely(channel < 0 || channel >= iface->num_channels)) { |
59ed0480 | 239 | dev_warn(&mdev->usb_device->dev, "Channel ID out of range.\n"); |
a4198cdf CG |
240 | return -ECHRNG; |
241 | } | |
242 | ||
88d1878b | 243 | lock = mdev->channel_lock + channel; |
b24c9fe9 | 244 | spin_lock_irqsave(lock, flags); |
a4198cdf | 245 | mdev->is_channel_healthy[channel] = false; |
b24c9fe9 | 246 | spin_unlock_irqrestore(lock, flags); |
a4198cdf | 247 | |
cc28983c CG |
248 | cancel_work_sync(&mdev->clear_work[channel].ws); |
249 | ||
a4198cdf | 250 | mutex_lock(&mdev->io_mutex); |
3a542007 | 251 | usb_kill_anchored_urbs(&mdev->busy_urbs[channel]); |
ec58d2a8 | 252 | if (mdev->padding_active[channel]) |
a4198cdf CG |
253 | mdev->padding_active[channel] = false; |
254 | ||
255 | if (mdev->conf[channel].data_type == MOST_CH_ASYNC) { | |
256 | del_timer_sync(&mdev->link_stat_timer); | |
257 | cancel_work_sync(&mdev->poll_work_obj); | |
258 | } | |
259 | mutex_unlock(&mdev->io_mutex); | |
260 | return 0; | |
261 | } | |
262 | ||
263 | /** | |
264 | * hdm_add_padding - add padding bytes | |
265 | * @mdev: most device | |
266 | * @channel: channel ID | |
267 | * @mbo: buffer object | |
268 | * | |
269 | * This inserts the INIC hardware specific padding bytes into a streaming | |
270 | * channel's buffer | |
271 | */ | |
23fe15fa | 272 | static int hdm_add_padding(struct most_dev *mdev, int channel, struct mbo *mbo) |
a4198cdf CG |
273 | { |
274 | struct most_channel_config *conf = &mdev->conf[channel]; | |
ac33fbb8 AS |
275 | unsigned int frame_size = get_stream_frame_size(conf); |
276 | unsigned int j, num_frames; | |
a4198cdf | 277 | |
a4198cdf CG |
278 | if (!frame_size) |
279 | return -EIO; | |
280 | num_frames = mbo->buffer_length / frame_size; | |
281 | ||
282 | if (num_frames < 1) { | |
59ed0480 CG |
283 | dev_err(&mdev->usb_device->dev, |
284 | "Missed minimal transfer unit.\n"); | |
a4198cdf CG |
285 | return -EIO; |
286 | } | |
287 | ||
5c13155d AS |
288 | for (j = num_frames - 1; j > 0; j--) |
289 | memmove(mbo->virt_address + j * USB_MTU, | |
290 | mbo->virt_address + j * frame_size, | |
a4198cdf | 291 | frame_size); |
a4198cdf CG |
292 | mbo->buffer_length = num_frames * USB_MTU; |
293 | return 0; | |
294 | } | |
295 | ||
296 | /** | |
297 | * hdm_remove_padding - remove padding bytes | |
298 | * @mdev: most device | |
299 | * @channel: channel ID | |
300 | * @mbo: buffer object | |
301 | * | |
302 | * This takes the INIC hardware specific padding bytes off a streaming | |
303 | * channel's buffer. | |
304 | */ | |
ba170ee2 CG |
305 | static int hdm_remove_padding(struct most_dev *mdev, int channel, |
306 | struct mbo *mbo) | |
a4198cdf | 307 | { |
a4198cdf | 308 | struct most_channel_config *const conf = &mdev->conf[channel]; |
ac33fbb8 AS |
309 | unsigned int frame_size = get_stream_frame_size(conf); |
310 | unsigned int j, num_frames; | |
a4198cdf | 311 | |
a4198cdf CG |
312 | if (!frame_size) |
313 | return -EIO; | |
314 | num_frames = mbo->processed_length / USB_MTU; | |
315 | ||
316 | for (j = 1; j < num_frames; j++) | |
317 | memmove(mbo->virt_address + frame_size * j, | |
318 | mbo->virt_address + USB_MTU * j, | |
319 | frame_size); | |
320 | ||
321 | mbo->processed_length = frame_size * num_frames; | |
322 | return 0; | |
323 | } | |
324 | ||
325 | /** | |
326 | * hdm_write_completion - completion function for submitted Tx URBs | |
327 | * @urb: the URB that has been completed | |
328 | * | |
329 | * This checks the status of the completed URB. In case the URB has been | |
330 | * unlinked before, it is immediately freed. On any other error the MBO | |
331 | * transfer flag is set. On success it frees allocated resources and calls | |
332 | * the completion function. | |
333 | * | |
334 | * Context: interrupt! | |
335 | */ | |
336 | static void hdm_write_completion(struct urb *urb) | |
337 | { | |
089612f1 | 338 | struct mbo *mbo = urb->context; |
089612f1 CG |
339 | struct most_dev *mdev = to_mdev(mbo->ifp); |
340 | unsigned int channel = mbo->hdm_channel_id; | |
341 | struct device *dev = &mdev->usb_device->dev; | |
88d1878b | 342 | spinlock_t *lock = mdev->channel_lock + channel; |
a4198cdf | 343 | unsigned long flags; |
a4198cdf | 344 | |
b24c9fe9 | 345 | spin_lock_irqsave(lock, flags); |
a4198cdf | 346 | |
3a542007 AS |
347 | mbo->processed_length = 0; |
348 | mbo->status = MBO_E_INVAL; | |
349 | if (likely(mdev->is_channel_healthy[channel])) { | |
a4198cdf | 350 | switch (urb->status) { |
3a542007 AS |
351 | case 0: |
352 | case -ESHUTDOWN: | |
353 | mbo->processed_length = urb->actual_length; | |
354 | mbo->status = MBO_SUCCESS; | |
355 | break; | |
a4198cdf | 356 | case -EPIPE: |
3b1a774b CG |
357 | dev_warn(dev, "Broken pipe on ep%02x\n", |
358 | mdev->ep_address[channel]); | |
879c93fe | 359 | mdev->is_channel_healthy[channel] = false; |
cc28983c CG |
360 | mdev->clear_work[channel].pipe = urb->pipe; |
361 | schedule_work(&mdev->clear_work[channel].ws); | |
3a542007 | 362 | break; |
a4198cdf CG |
363 | case -ENODEV: |
364 | case -EPROTO: | |
365 | mbo->status = MBO_E_CLOSE; | |
366 | break; | |
a4198cdf | 367 | } |
a4198cdf CG |
368 | } |
369 | ||
cf6a599e | 370 | spin_unlock_irqrestore(lock, flags); |
a4198cdf CG |
371 | |
372 | if (likely(mbo->complete)) | |
373 | mbo->complete(mbo); | |
374 | usb_free_urb(urb); | |
375 | } | |
376 | ||
377 | /** | |
9f17591f | 378 | * hdm_read_completion - completion function for submitted Rx URBs |
a4198cdf CG |
379 | * @urb: the URB that has been completed |
380 | * | |
381 | * This checks the status of the completed URB. In case the URB has been | |
382 | * unlinked before it is immediately freed. On any other error the MBO transfer | |
383 | * flag is set. On success it frees allocated resources, removes | |
384 | * padding bytes -if necessary- and calls the completion function. | |
385 | * | |
386 | * Context: interrupt! | |
387 | * | |
388 | * ************************************************************************** | |
389 | * Error codes returned by in urb->status | |
390 | * or in iso_frame_desc[n].status (for ISO) | |
391 | * ************************************************************************* | |
392 | * | |
393 | * USB device drivers may only test urb status values in completion handlers. | |
394 | * This is because otherwise there would be a race between HCDs updating | |
395 | * these values on one CPU, and device drivers testing them on another CPU. | |
396 | * | |
397 | * A transfer's actual_length may be positive even when an error has been | |
398 | * reported. That's because transfers often involve several packets, so that | |
399 | * one or more packets could finish before an error stops further endpoint I/O. | |
400 | * | |
401 | * For isochronous URBs, the urb status value is non-zero only if the URB is | |
402 | * unlinked, the device is removed, the host controller is disabled or the total | |
403 | * transferred length is less than the requested length and the URB_SHORT_NOT_OK | |
404 | * flag is set. Completion handlers for isochronous URBs should only see | |
405 | * urb->status set to zero, -ENOENT, -ECONNRESET, -ESHUTDOWN, or -EREMOTEIO. | |
406 | * Individual frame descriptor status fields may report more status codes. | |
407 | * | |
408 | * | |
409 | * 0 Transfer completed successfully | |
410 | * | |
411 | * -ENOENT URB was synchronously unlinked by usb_unlink_urb | |
412 | * | |
413 | * -EINPROGRESS URB still pending, no results yet | |
414 | * (That is, if drivers see this it's a bug.) | |
415 | * | |
416 | * -EPROTO (*, **) a) bitstuff error | |
417 | * b) no response packet received within the | |
418 | * prescribed bus turn-around time | |
419 | * c) unknown USB error | |
420 | * | |
421 | * -EILSEQ (*, **) a) CRC mismatch | |
422 | * b) no response packet received within the | |
423 | * prescribed bus turn-around time | |
424 | * c) unknown USB error | |
425 | * | |
426 | * Note that often the controller hardware does not | |
427 | * distinguish among cases a), b), and c), so a | |
428 | * driver cannot tell whether there was a protocol | |
429 | * error, a failure to respond (often caused by | |
430 | * device disconnect), or some other fault. | |
431 | * | |
432 | * -ETIME (**) No response packet received within the prescribed | |
433 | * bus turn-around time. This error may instead be | |
434 | * reported as -EPROTO or -EILSEQ. | |
435 | * | |
436 | * -ETIMEDOUT Synchronous USB message functions use this code | |
437 | * to indicate timeout expired before the transfer | |
438 | * completed, and no other error was reported by HC. | |
439 | * | |
440 | * -EPIPE (**) Endpoint stalled. For non-control endpoints, | |
441 | * reset this status with usb_clear_halt(). | |
442 | * | |
443 | * -ECOMM During an IN transfer, the host controller | |
444 | * received data from an endpoint faster than it | |
445 | * could be written to system memory | |
446 | * | |
447 | * -ENOSR During an OUT transfer, the host controller | |
448 | * could not retrieve data from system memory fast | |
449 | * enough to keep up with the USB data rate | |
450 | * | |
451 | * -EOVERFLOW (*) The amount of data returned by the endpoint was | |
452 | * greater than either the max packet size of the | |
453 | * endpoint or the remaining buffer size. "Babble". | |
454 | * | |
455 | * -EREMOTEIO The data read from the endpoint did not fill the | |
456 | * specified buffer, and URB_SHORT_NOT_OK was set in | |
457 | * urb->transfer_flags. | |
458 | * | |
459 | * -ENODEV Device was removed. Often preceded by a burst of | |
460 | * other errors, since the hub driver doesn't detect | |
461 | * device removal events immediately. | |
462 | * | |
463 | * -EXDEV ISO transfer only partially completed | |
464 | * (only set in iso_frame_desc[n].status, not urb->status) | |
465 | * | |
466 | * -EINVAL ISO madness, if this happens: Log off and go home | |
467 | * | |
468 | * -ECONNRESET URB was asynchronously unlinked by usb_unlink_urb | |
469 | * | |
470 | * -ESHUTDOWN The device or host controller has been disabled due | |
471 | * to some problem that could not be worked around, | |
472 | * such as a physical disconnect. | |
473 | * | |
474 | * | |
475 | * (*) Error codes like -EPROTO, -EILSEQ and -EOVERFLOW normally indicate | |
476 | * hardware problems such as bad devices (including firmware) or cables. | |
477 | * | |
478 | * (**) This is also one of several codes that different kinds of host | |
479 | * controller use to indicate a transfer has failed because of device | |
480 | * disconnect. In the interval before the hub driver starts disconnect | |
481 | * processing, devices may receive such fault reports for every request. | |
482 | * | |
e1c3e6e1 | 483 | * See <https://www.kernel.org/doc/Documentation/driver-api/usb/error-codes.rst> |
a4198cdf CG |
484 | */ |
485 | static void hdm_read_completion(struct urb *urb) | |
486 | { | |
089612f1 | 487 | struct mbo *mbo = urb->context; |
089612f1 CG |
488 | struct most_dev *mdev = to_mdev(mbo->ifp); |
489 | unsigned int channel = mbo->hdm_channel_id; | |
490 | struct device *dev = &mdev->usb_device->dev; | |
88d1878b | 491 | spinlock_t *lock = mdev->channel_lock + channel; |
a4198cdf | 492 | unsigned long flags; |
a4198cdf | 493 | |
b24c9fe9 | 494 | spin_lock_irqsave(lock, flags); |
a4198cdf | 495 | |
3a542007 AS |
496 | mbo->processed_length = 0; |
497 | mbo->status = MBO_E_INVAL; | |
498 | if (likely(mdev->is_channel_healthy[channel])) { | |
a4198cdf | 499 | switch (urb->status) { |
3a542007 AS |
500 | case 0: |
501 | case -ESHUTDOWN: | |
502 | mbo->processed_length = urb->actual_length; | |
503 | mbo->status = MBO_SUCCESS; | |
504 | if (mdev->padding_active[channel] && | |
505 | hdm_remove_padding(mdev, channel, mbo)) { | |
506 | mbo->processed_length = 0; | |
507 | mbo->status = MBO_E_INVAL; | |
508 | } | |
509 | break; | |
a4198cdf | 510 | case -EPIPE: |
3b1a774b CG |
511 | dev_warn(dev, "Broken pipe on ep%02x\n", |
512 | mdev->ep_address[channel]); | |
879c93fe | 513 | mdev->is_channel_healthy[channel] = false; |
cc28983c CG |
514 | mdev->clear_work[channel].pipe = urb->pipe; |
515 | schedule_work(&mdev->clear_work[channel].ws); | |
3a542007 | 516 | break; |
a4198cdf CG |
517 | case -ENODEV: |
518 | case -EPROTO: | |
519 | mbo->status = MBO_E_CLOSE; | |
520 | break; | |
521 | case -EOVERFLOW: | |
3b1a774b CG |
522 | dev_warn(dev, "Babble on ep%02x\n", |
523 | mdev->ep_address[channel]); | |
a4198cdf CG |
524 | break; |
525 | } | |
a4198cdf | 526 | } |
b24c9fe9 | 527 | |
cf6a599e | 528 | spin_unlock_irqrestore(lock, flags); |
a4198cdf CG |
529 | |
530 | if (likely(mbo->complete)) | |
531 | mbo->complete(mbo); | |
532 | usb_free_urb(urb); | |
533 | } | |
534 | ||
535 | /** | |
536 | * hdm_enqueue - receive a buffer to be used for data transfer | |
537 | * @iface: interface to enqueue to | |
538 | * @channel: ID of the channel | |
539 | * @mbo: pointer to the buffer object | |
540 | * | |
541 | * This allocates a new URB and fills it according to the channel | |
542 | * that is being used for transmission of data. Before the URB is | |
543 | * submitted it is stored in the private anchor list. | |
544 | * | |
545 | * Returns 0 on success. On any error the URB is freed and a error code | |
546 | * is returned. | |
547 | * | |
548 | * Context: Could in _some_ cases be interrupt! | |
549 | */ | |
ba170ee2 CG |
550 | static int hdm_enqueue(struct most_interface *iface, int channel, |
551 | struct mbo *mbo) | |
a4198cdf CG |
552 | { |
553 | struct most_dev *mdev; | |
a4198cdf | 554 | struct most_channel_config *conf; |
59ed0480 | 555 | struct device *dev; |
a4198cdf CG |
556 | int retval = 0; |
557 | struct urb *urb; | |
a4198cdf CG |
558 | unsigned long length; |
559 | void *virt_address; | |
560 | ||
59ed0480 | 561 | if (unlikely(!iface || !mbo)) |
a4198cdf | 562 | return -EIO; |
dd53ecba | 563 | if (unlikely(iface->num_channels <= channel || channel < 0)) |
a4198cdf | 564 | return -ECHRNG; |
a4198cdf CG |
565 | |
566 | mdev = to_mdev(iface); | |
567 | conf = &mdev->conf[channel]; | |
59ed0480 | 568 | dev = &mdev->usb_device->dev; |
a4198cdf CG |
569 | |
570 | if (!mdev->usb_device) | |
571 | return -ENODEV; | |
572 | ||
573 | urb = usb_alloc_urb(NO_ISOCHRONOUS_URB, GFP_ATOMIC); | |
ca47d8f3 | 574 | if (!urb) |
a4198cdf | 575 | return -ENOMEM; |
a4198cdf | 576 | |
dd53ecba CG |
577 | if ((conf->direction & MOST_CH_TX) && mdev->padding_active[channel] && |
578 | hdm_add_padding(mdev, channel, mbo)) { | |
579 | retval = -EIO; | |
580 | goto _error; | |
581 | } | |
a4198cdf CG |
582 | |
583 | urb->transfer_dma = mbo->bus_address; | |
584 | virt_address = mbo->virt_address; | |
585 | length = mbo->buffer_length; | |
586 | ||
587 | if (conf->direction & MOST_CH_TX) { | |
588 | usb_fill_bulk_urb(urb, mdev->usb_device, | |
589 | usb_sndbulkpipe(mdev->usb_device, | |
590 | mdev->ep_address[channel]), | |
591 | virt_address, | |
592 | length, | |
593 | hdm_write_completion, | |
594 | mbo); | |
0540609f | 595 | if (conf->data_type != MOST_CH_ISOC) |
a4198cdf CG |
596 | urb->transfer_flags |= URB_ZERO_PACKET; |
597 | } else { | |
598 | usb_fill_bulk_urb(urb, mdev->usb_device, | |
599 | usb_rcvbulkpipe(mdev->usb_device, | |
600 | mdev->ep_address[channel]), | |
601 | virt_address, | |
9161e931 | 602 | length + conf->extra_len, |
a4198cdf CG |
603 | hdm_read_completion, |
604 | mbo); | |
605 | } | |
606 | urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; | |
607 | ||
27e6245e | 608 | usb_anchor_urb(urb, &mdev->busy_urbs[channel]); |
ec7e0a18 | 609 | |
a4198cdf CG |
610 | retval = usb_submit_urb(urb, GFP_KERNEL); |
611 | if (retval) { | |
59ed0480 | 612 | dev_err(dev, "URB submit failed with error %d.\n", retval); |
a4198cdf CG |
613 | goto _error_1; |
614 | } | |
615 | return 0; | |
616 | ||
617 | _error_1: | |
27e6245e | 618 | usb_unanchor_urb(urb); |
a4198cdf CG |
619 | _error: |
620 | usb_free_urb(urb); | |
621 | return retval; | |
622 | } | |
623 | ||
3598cec5 CG |
624 | static void *hdm_dma_alloc(struct mbo *mbo, u32 size) |
625 | { | |
626 | struct most_dev *mdev = to_mdev(mbo->ifp); | |
627 | ||
628 | return usb_alloc_coherent(mdev->usb_device, size, GFP_KERNEL, | |
629 | &mbo->bus_address); | |
630 | } | |
631 | ||
632 | static void hdm_dma_free(struct mbo *mbo, u32 size) | |
633 | { | |
634 | struct most_dev *mdev = to_mdev(mbo->ifp); | |
635 | ||
636 | usb_free_coherent(mdev->usb_device, size, mbo->virt_address, | |
637 | mbo->bus_address); | |
638 | } | |
639 | ||
a4198cdf CG |
640 | /** |
641 | * hdm_configure_channel - receive channel configuration from core | |
642 | * @iface: interface | |
643 | * @channel: channel ID | |
644 | * @conf: structure that holds the configuration information | |
25e3854c CG |
645 | * |
646 | * The attached network interface controller (NIC) supports a padding mode | |
647 | * to avoid short packets on USB, hence increasing the performance due to a | |
648 | * lower interrupt load. This mode is default for synchronous data and can | |
649 | * be switched on for isochronous data. In case padding is active the | |
650 | * driver needs to know the frame size of the payload in order to calculate | |
651 | * the number of bytes it needs to pad when transmitting or to cut off when | |
652 | * receiving data. | |
653 | * | |
a4198cdf | 654 | */ |
23fe15fa AR |
655 | static int hdm_configure_channel(struct most_interface *iface, int channel, |
656 | struct most_channel_config *conf) | |
a4198cdf CG |
657 | { |
658 | unsigned int num_frames; | |
659 | unsigned int frame_size; | |
089612f1 CG |
660 | struct most_dev *mdev = to_mdev(iface); |
661 | struct device *dev = &mdev->usb_device->dev; | |
59ed0480 | 662 | |
59ed0480 | 663 | mdev->is_channel_healthy[channel] = true; |
cc28983c CG |
664 | mdev->clear_work[channel].channel = channel; |
665 | mdev->clear_work[channel].mdev = mdev; | |
666 | INIT_WORK(&mdev->clear_work[channel].ws, wq_clear_halt); | |
a4198cdf CG |
667 | |
668 | if (unlikely(!iface || !conf)) { | |
59ed0480 | 669 | dev_err(dev, "Bad interface or config pointer.\n"); |
a4198cdf CG |
670 | return -EINVAL; |
671 | } | |
dd53ecba | 672 | if (unlikely(channel < 0 || channel >= iface->num_channels)) { |
59ed0480 | 673 | dev_err(dev, "Channel ID out of range.\n"); |
a4198cdf CG |
674 | return -EINVAL; |
675 | } | |
dd53ecba | 676 | if (!conf->num_buffers || !conf->buffer_size) { |
59ed0480 | 677 | dev_err(dev, "Misconfig: buffer size or #buffers zero.\n"); |
a4198cdf CG |
678 | return -EINVAL; |
679 | } | |
680 | ||
dd53ecba | 681 | if (conf->data_type != MOST_CH_SYNC && |
0540609f | 682 | !(conf->data_type == MOST_CH_ISOC && |
dd53ecba | 683 | conf->packets_per_xact != 0xFF)) { |
a4198cdf | 684 | mdev->padding_active[channel] = false; |
25e3854c CG |
685 | /* |
686 | * Since the NIC's padding mode is not going to be | |
687 | * used, we can skip the frame size calculations and | |
688 | * move directly on to exit. | |
689 | */ | |
a4198cdf CG |
690 | goto exit; |
691 | } | |
692 | ||
693 | mdev->padding_active[channel] = true; | |
a4198cdf | 694 | |
a4198cdf | 695 | frame_size = get_stream_frame_size(conf); |
dd53ecba | 696 | if (frame_size == 0 || frame_size > USB_MTU) { |
59ed0480 | 697 | dev_warn(dev, "Misconfig: frame size wrong\n"); |
a4198cdf CG |
698 | return -EINVAL; |
699 | } | |
700 | ||
f5001928 AS |
701 | num_frames = conf->buffer_size / frame_size; |
702 | ||
a4198cdf | 703 | if (conf->buffer_size % frame_size) { |
f5001928 | 704 | u16 old_size = conf->buffer_size; |
a4198cdf | 705 | |
f5001928 AS |
706 | conf->buffer_size = num_frames * frame_size; |
707 | dev_warn(dev, "%s: fixed buffer size (%d -> %d)\n", | |
708 | mdev->suffix[channel], old_size, conf->buffer_size); | |
709 | } | |
a4198cdf CG |
710 | |
711 | /* calculate extra length to comply w/ HW padding */ | |
f5001928 AS |
712 | conf->extra_len = num_frames * (USB_MTU - frame_size); |
713 | ||
a4198cdf CG |
714 | exit: |
715 | mdev->conf[channel] = *conf; | |
7c23baa9 AS |
716 | if (conf->data_type == MOST_CH_ASYNC) { |
717 | u16 ep = mdev->ep_address[channel]; | |
7c23baa9 | 718 | |
e33269f6 | 719 | if (start_sync_ep(mdev->usb_device, ep) < 0) |
7c23baa9 AS |
720 | dev_warn(dev, "sync for ep%02x failed", ep); |
721 | } | |
a4198cdf CG |
722 | return 0; |
723 | } | |
724 | ||
a4198cdf CG |
725 | /** |
726 | * hdm_request_netinfo - request network information | |
727 | * @iface: pointer to interface | |
728 | * @channel: channel ID | |
729 | * | |
730 | * This is used as trigger to set up the link status timer that | |
731 | * polls for the NI state of the INIC every 2 seconds. | |
732 | * | |
733 | */ | |
d35bbfaa AS |
734 | static void hdm_request_netinfo(struct most_interface *iface, int channel, |
735 | void (*on_netinfo)(struct most_interface *, | |
736 | unsigned char, | |
737 | unsigned char *)) | |
a4198cdf CG |
738 | { |
739 | struct most_dev *mdev; | |
740 | ||
741 | BUG_ON(!iface); | |
742 | mdev = to_mdev(iface); | |
d35bbfaa AS |
743 | mdev->on_netinfo = on_netinfo; |
744 | if (!on_netinfo) | |
745 | return; | |
746 | ||
a4198cdf CG |
747 | mdev->link_stat_timer.expires = jiffies + HZ; |
748 | mod_timer(&mdev->link_stat_timer, mdev->link_stat_timer.expires); | |
749 | } | |
750 | ||
751 | /** | |
f28e6cd3 | 752 | * link_stat_timer_handler - schedule work obtaining mac address and link status |
a4198cdf CG |
753 | * @data: pointer to USB device instance |
754 | * | |
755 | * The handler runs in interrupt context. That's why we need to defer the | |
756 | * tasks to a work queue. | |
757 | */ | |
e99e88a9 | 758 | static void link_stat_timer_handler(struct timer_list *t) |
a4198cdf | 759 | { |
e99e88a9 | 760 | struct most_dev *mdev = from_timer(mdev, t, link_stat_timer); |
a4198cdf | 761 | |
e3479f77 | 762 | schedule_work(&mdev->poll_work_obj); |
a4198cdf CG |
763 | mdev->link_stat_timer.expires = jiffies + (2 * HZ); |
764 | add_timer(&mdev->link_stat_timer); | |
765 | } | |
766 | ||
767 | /** | |
f28e6cd3 | 768 | * wq_netinfo - work queue function to deliver latest networking information |
a4198cdf CG |
769 | * @wq_obj: object that holds data for our deferred work to do |
770 | * | |
771 | * This retrieves the network interface status of the USB INIC | |
a4198cdf CG |
772 | */ |
773 | static void wq_netinfo(struct work_struct *wq_obj) | |
774 | { | |
089612f1 | 775 | struct most_dev *mdev = to_mdev_from_work(wq_obj); |
f28e6cd3 AS |
776 | struct usb_device *usb_device = mdev->usb_device; |
777 | struct device *dev = &usb_device->dev; | |
778 | u16 hi, mi, lo, link; | |
779 | u8 hw_addr[6]; | |
780 | ||
781 | if (drci_rd_reg(usb_device, DRCI_REG_HW_ADDR_HI, &hi) < 0) { | |
782 | dev_err(dev, "Vendor request 'hw_addr_hi' failed\n"); | |
783 | return; | |
784 | } | |
a4198cdf | 785 | |
f28e6cd3 AS |
786 | if (drci_rd_reg(usb_device, DRCI_REG_HW_ADDR_MI, &mi) < 0) { |
787 | dev_err(dev, "Vendor request 'hw_addr_mid' failed\n"); | |
788 | return; | |
789 | } | |
790 | ||
791 | if (drci_rd_reg(usb_device, DRCI_REG_HW_ADDR_LO, &lo) < 0) { | |
792 | dev_err(dev, "Vendor request 'hw_addr_low' failed\n"); | |
793 | return; | |
794 | } | |
a4198cdf | 795 | |
f28e6cd3 AS |
796 | if (drci_rd_reg(usb_device, DRCI_REG_NI_STATE, &link) < 0) { |
797 | dev_err(dev, "Vendor request 'link status' failed\n"); | |
a4198cdf | 798 | return; |
f28e6cd3 AS |
799 | } |
800 | ||
801 | hw_addr[0] = hi >> 8; | |
802 | hw_addr[1] = hi; | |
803 | hw_addr[2] = mi >> 8; | |
804 | hw_addr[3] = mi; | |
805 | hw_addr[4] = lo >> 8; | |
806 | hw_addr[5] = lo; | |
807 | ||
d35bbfaa AS |
808 | if (mdev->on_netinfo) |
809 | mdev->on_netinfo(&mdev->iface, link, hw_addr); | |
a4198cdf CG |
810 | } |
811 | ||
812 | /** | |
813 | * wq_clear_halt - work queue function | |
814 | * @wq_obj: work_struct object to execute | |
815 | * | |
816 | * This sends a clear_halt to the given USB pipe. | |
817 | */ | |
818 | static void wq_clear_halt(struct work_struct *wq_obj) | |
819 | { | |
cc28983c CG |
820 | struct clear_hold_work *clear_work = to_clear_hold_work(wq_obj); |
821 | struct most_dev *mdev = clear_work->mdev; | |
822 | unsigned int channel = clear_work->channel; | |
823 | int pipe = clear_work->pipe; | |
a4198cdf | 824 | |
cc28983c | 825 | mutex_lock(&mdev->io_mutex); |
bf9503f1 | 826 | most_stop_enqueue(&mdev->iface, channel); |
3a542007 | 827 | usb_kill_anchored_urbs(&mdev->busy_urbs[channel]); |
cc28983c | 828 | if (usb_clear_halt(mdev->usb_device, pipe)) |
59ed0480 | 829 | dev_warn(&mdev->usb_device->dev, "Failed to reset endpoint.\n"); |
a4198cdf | 830 | |
8f20f2dc CG |
831 | /* If the functional Stall condition has been set on an |
832 | * asynchronous rx channel, we need to clear the tx channel | |
833 | * too, since the hardware runs its clean-up sequence on both | |
834 | * channels, as they are physically one on the network. | |
835 | * | |
836 | * The USB interface that exposes the asynchronous channels | |
837 | * contains always two endpoints, and two only. | |
838 | */ | |
839 | if (mdev->conf[channel].data_type == MOST_CH_ASYNC && | |
840 | mdev->conf[channel].direction == MOST_CH_RX) { | |
841 | int peer = 1 - channel; | |
842 | int snd_pipe = usb_sndbulkpipe(mdev->usb_device, | |
843 | mdev->ep_address[peer]); | |
844 | usb_clear_halt(mdev->usb_device, snd_pipe); | |
845 | } | |
879c93fe | 846 | mdev->is_channel_healthy[channel] = true; |
72df4a55 | 847 | most_resume_enqueue(&mdev->iface, channel); |
cc28983c | 848 | mutex_unlock(&mdev->io_mutex); |
a4198cdf CG |
849 | } |
850 | ||
851 | /** | |
852 | * hdm_usb_fops - file operation table for USB driver | |
853 | */ | |
854 | static const struct file_operations hdm_usb_fops = { | |
855 | .owner = THIS_MODULE, | |
856 | }; | |
857 | ||
858 | /** | |
859 | * usb_device_id - ID table for HCD device probing | |
860 | */ | |
27acb557 | 861 | static const struct usb_device_id usbid[] = { |
a4198cdf | 862 | { USB_DEVICE(USB_VENDOR_ID_SMSC, USB_DEV_ID_BRDG), }, |
654f7ec4 | 863 | { USB_DEVICE(USB_VENDOR_ID_SMSC, USB_DEV_ID_OS81118), }, |
b50762ea | 864 | { USB_DEVICE(USB_VENDOR_ID_SMSC, USB_DEV_ID_OS81119), }, |
5bf9bd8d | 865 | { USB_DEVICE(USB_VENDOR_ID_SMSC, USB_DEV_ID_OS81210), }, |
a4198cdf CG |
866 | { } /* Terminating entry */ |
867 | }; | |
868 | ||
a747b42c CG |
869 | struct regs { |
870 | const char *name; | |
871 | u16 reg; | |
872 | }; | |
873 | ||
874 | static const struct regs ro_regs[] = { | |
875 | { "ni_state", DRCI_REG_NI_STATE }, | |
876 | { "packet_bandwidth", DRCI_REG_PACKET_BW }, | |
877 | { "node_address", DRCI_REG_NODE_ADDR }, | |
878 | { "node_position", DRCI_REG_NODE_POS }, | |
879 | }; | |
880 | ||
881 | static const struct regs rw_regs[] = { | |
882 | { "mep_filter", DRCI_REG_MEP_FILTER }, | |
883 | { "mep_hash0", DRCI_REG_HASH_TBL0 }, | |
884 | { "mep_hash1", DRCI_REG_HASH_TBL1 }, | |
885 | { "mep_hash2", DRCI_REG_HASH_TBL2 }, | |
886 | { "mep_hash3", DRCI_REG_HASH_TBL3 }, | |
887 | { "mep_eui48_hi", DRCI_REG_HW_ADDR_HI }, | |
888 | { "mep_eui48_mi", DRCI_REG_HW_ADDR_MI }, | |
889 | { "mep_eui48_lo", DRCI_REG_HW_ADDR_LO }, | |
890 | }; | |
891 | ||
892 | static int get_stat_reg_addr(const struct regs *regs, int size, | |
893 | const char *name, u16 *reg_addr) | |
894 | { | |
895 | int i; | |
896 | ||
897 | for (i = 0; i < size; i++) { | |
898 | if (!strcmp(name, regs[i].name)) { | |
899 | *reg_addr = regs[i].reg; | |
900 | return 0; | |
901 | } | |
902 | } | |
903 | return -EFAULT; | |
904 | } | |
905 | ||
906 | #define get_static_reg_addr(regs, name, reg_addr) \ | |
907 | get_stat_reg_addr(regs, ARRAY_SIZE(regs), name, reg_addr) | |
908 | ||
3d9c54b5 | 909 | static ssize_t value_show(struct device *dev, struct device_attribute *attr, |
4d5f022f | 910 | char *buf) |
a4198cdf | 911 | { |
98a3c4d7 | 912 | const char *name = attr->attr.name; |
4d5f022f | 913 | struct most_dci_obj *dci_obj = to_dci_obj(dev); |
0e9b9d08 | 914 | u16 val; |
a4198cdf CG |
915 | u16 reg_addr; |
916 | int err; | |
917 | ||
98a3c4d7 | 918 | if (!strcmp(name, "arb_address")) |
c0554645 | 919 | return snprintf(buf, PAGE_SIZE, "%04x\n", dci_obj->reg_addr); |
98a3c4d7 CG |
920 | |
921 | if (!strcmp(name, "arb_value")) | |
c0554645 | 922 | reg_addr = dci_obj->reg_addr; |
98a3c4d7 CG |
923 | else if (get_static_reg_addr(ro_regs, name, ®_addr) && |
924 | get_static_reg_addr(rw_regs, name, ®_addr)) | |
a27cb25b | 925 | return -EFAULT; |
a4198cdf | 926 | |
0e9b9d08 | 927 | err = drci_rd_reg(dci_obj->usb_device, reg_addr, &val); |
a4198cdf CG |
928 | if (err < 0) |
929 | return err; | |
930 | ||
0e9b9d08 | 931 | return snprintf(buf, PAGE_SIZE, "%04x\n", val); |
a4198cdf CG |
932 | } |
933 | ||
3d9c54b5 | 934 | static ssize_t value_store(struct device *dev, struct device_attribute *attr, |
a4198cdf CG |
935 | const char *buf, size_t count) |
936 | { | |
f1b9a843 | 937 | u16 val; |
a4198cdf | 938 | u16 reg_addr; |
98a3c4d7 | 939 | const char *name = attr->attr.name; |
4d5f022f | 940 | struct most_dci_obj *dci_obj = to_dci_obj(dev); |
e33269f6 | 941 | struct usb_device *usb_dev = dci_obj->usb_device; |
ac33fbb8 | 942 | int err = kstrtou16(buf, 16, &val); |
a4198cdf | 943 | |
c0554645 CG |
944 | if (err) |
945 | return err; | |
946 | ||
98a3c4d7 | 947 | if (!strcmp(name, "arb_address")) { |
c0554645 CG |
948 | dci_obj->reg_addr = val; |
949 | return count; | |
950 | } | |
98a3c4d7 | 951 | |
e33269f6 AS |
952 | if (!strcmp(name, "arb_value")) |
953 | err = drci_wr_reg(usb_dev, dci_obj->reg_addr, val); | |
954 | else if (!strcmp(name, "sync_ep")) | |
955 | err = start_sync_ep(usb_dev, val); | |
b2e8aa52 | 956 | else if (!get_static_reg_addr(rw_regs, name, ®_addr)) |
e33269f6 AS |
957 | err = drci_wr_reg(usb_dev, reg_addr, val); |
958 | else | |
d5cfb0ff | 959 | return -EFAULT; |
a4198cdf | 960 | |
a4198cdf CG |
961 | if (err < 0) |
962 | return err; | |
963 | ||
964 | return count; | |
965 | } | |
966 | ||
f15e3ad3 CG |
967 | static DEVICE_ATTR(ni_state, 0444, value_show, NULL); |
968 | static DEVICE_ATTR(packet_bandwidth, 0444, value_show, NULL); | |
969 | static DEVICE_ATTR(node_address, 0444, value_show, NULL); | |
970 | static DEVICE_ATTR(node_position, 0444, value_show, NULL); | |
971 | static DEVICE_ATTR(sync_ep, 0200, NULL, value_store); | |
972 | static DEVICE_ATTR(mep_filter, 0644, value_show, value_store); | |
973 | static DEVICE_ATTR(mep_hash0, 0644, value_show, value_store); | |
974 | static DEVICE_ATTR(mep_hash1, 0644, value_show, value_store); | |
975 | static DEVICE_ATTR(mep_hash2, 0644, value_show, value_store); | |
976 | static DEVICE_ATTR(mep_hash3, 0644, value_show, value_store); | |
977 | static DEVICE_ATTR(mep_eui48_hi, 0644, value_show, value_store); | |
978 | static DEVICE_ATTR(mep_eui48_mi, 0644, value_show, value_store); | |
979 | static DEVICE_ATTR(mep_eui48_lo, 0644, value_show, value_store); | |
980 | static DEVICE_ATTR(arb_address, 0644, value_show, value_store); | |
981 | static DEVICE_ATTR(arb_value, 0644, value_show, value_store); | |
4d5f022f CG |
982 | |
983 | static struct attribute *dci_attrs[] = { | |
984 | &dev_attr_ni_state.attr, | |
985 | &dev_attr_packet_bandwidth.attr, | |
986 | &dev_attr_node_address.attr, | |
987 | &dev_attr_node_position.attr, | |
988 | &dev_attr_sync_ep.attr, | |
989 | &dev_attr_mep_filter.attr, | |
990 | &dev_attr_mep_hash0.attr, | |
991 | &dev_attr_mep_hash1.attr, | |
992 | &dev_attr_mep_hash2.attr, | |
993 | &dev_attr_mep_hash3.attr, | |
994 | &dev_attr_mep_eui48_hi.attr, | |
995 | &dev_attr_mep_eui48_mi.attr, | |
996 | &dev_attr_mep_eui48_lo.attr, | |
997 | &dev_attr_arb_address.attr, | |
998 | &dev_attr_arb_value.attr, | |
a4198cdf CG |
999 | NULL, |
1000 | }; | |
1001 | ||
4d5f022f CG |
1002 | static struct attribute_group dci_attr_group = { |
1003 | .attrs = dci_attrs, | |
a4198cdf CG |
1004 | }; |
1005 | ||
4d5f022f CG |
1006 | static const struct attribute_group *dci_attr_groups[] = { |
1007 | &dci_attr_group, | |
1008 | NULL, | |
1009 | }; | |
a4198cdf CG |
1010 | |
1011 | /** | |
1012 | * hdm_probe - probe function of USB device driver | |
1013 | * @interface: Interface of the attached USB device | |
1014 | * @id: Pointer to the USB ID table. | |
1015 | * | |
1016 | * This allocates and initializes the device instance, adds the new | |
1017 | * entry to the internal list, scans the USB descriptors and registers | |
1018 | * the interface with the core. | |
1019 | * Additionally, the DCI objects are created and the hardware is sync'd. | |
1020 | * | |
1021 | * Return 0 on success. In case of an error a negative number is returned. | |
1022 | */ | |
1023 | static int | |
1024 | hdm_probe(struct usb_interface *interface, const struct usb_device_id *id) | |
1025 | { | |
089612f1 CG |
1026 | struct usb_host_interface *usb_iface_desc = interface->cur_altsetting; |
1027 | struct usb_device *usb_dev = interface_to_usbdev(interface); | |
1028 | struct device *dev = &usb_dev->dev; | |
1029 | struct most_dev *mdev = kzalloc(sizeof(*mdev), GFP_KERNEL); | |
a4198cdf CG |
1030 | unsigned int i; |
1031 | unsigned int num_endpoints; | |
1032 | struct most_channel_capability *tmp_cap; | |
a4198cdf CG |
1033 | struct usb_endpoint_descriptor *ep_desc; |
1034 | int ret = 0; | |
1035 | ||
a4198cdf CG |
1036 | if (!mdev) |
1037 | goto exit_ENOMEM; | |
1038 | ||
1039 | usb_set_intfdata(interface, mdev); | |
1040 | num_endpoints = usb_iface_desc->desc.bNumEndpoints; | |
1041 | mutex_init(&mdev->io_mutex); | |
1042 | INIT_WORK(&mdev->poll_work_obj, wq_netinfo); | |
e99e88a9 | 1043 | timer_setup(&mdev->link_stat_timer, link_stat_timer_handler, 0); |
a4198cdf CG |
1044 | |
1045 | mdev->usb_device = usb_dev; | |
a4198cdf CG |
1046 | mdev->link_stat_timer.expires = jiffies + (2 * HZ); |
1047 | ||
1048 | mdev->iface.mod = hdm_usb_fops.owner; | |
69c90cf1 | 1049 | mdev->iface.driver_dev = &interface->dev; |
a4198cdf CG |
1050 | mdev->iface.interface = ITYPE_USB; |
1051 | mdev->iface.configure = hdm_configure_channel; | |
1052 | mdev->iface.request_netinfo = hdm_request_netinfo; | |
1053 | mdev->iface.enqueue = hdm_enqueue; | |
1054 | mdev->iface.poison_channel = hdm_poison_channel; | |
3598cec5 CG |
1055 | mdev->iface.dma_alloc = hdm_dma_alloc; |
1056 | mdev->iface.dma_free = hdm_dma_free; | |
a4198cdf CG |
1057 | mdev->iface.description = mdev->description; |
1058 | mdev->iface.num_channels = num_endpoints; | |
1059 | ||
1060 | snprintf(mdev->description, sizeof(mdev->description), | |
1061 | "usb_device %d-%s:%d.%d", | |
1062 | usb_dev->bus->busnum, | |
1063 | usb_dev->devpath, | |
1064 | usb_dev->config->desc.bConfigurationValue, | |
1065 | usb_iface_desc->desc.bInterfaceNumber); | |
1066 | ||
1067 | mdev->conf = kcalloc(num_endpoints, sizeof(*mdev->conf), GFP_KERNEL); | |
1068 | if (!mdev->conf) | |
1069 | goto exit_free; | |
1070 | ||
1071 | mdev->cap = kcalloc(num_endpoints, sizeof(*mdev->cap), GFP_KERNEL); | |
1072 | if (!mdev->cap) | |
1073 | goto exit_free1; | |
1074 | ||
1075 | mdev->iface.channel_vector = mdev->cap; | |
a4198cdf CG |
1076 | mdev->ep_address = |
1077 | kcalloc(num_endpoints, sizeof(*mdev->ep_address), GFP_KERNEL); | |
1078 | if (!mdev->ep_address) | |
1079 | goto exit_free2; | |
1080 | ||
27e6245e CG |
1081 | mdev->busy_urbs = |
1082 | kcalloc(num_endpoints, sizeof(*mdev->busy_urbs), GFP_KERNEL); | |
1083 | if (!mdev->busy_urbs) | |
a4198cdf CG |
1084 | goto exit_free3; |
1085 | ||
1086 | tmp_cap = mdev->cap; | |
1087 | for (i = 0; i < num_endpoints; i++) { | |
1088 | ep_desc = &usb_iface_desc->endpoint[i].desc; | |
1089 | mdev->ep_address[i] = ep_desc->bEndpointAddress; | |
1090 | mdev->padding_active[i] = false; | |
1091 | mdev->is_channel_healthy[i] = true; | |
1092 | ||
1093 | snprintf(&mdev->suffix[i][0], MAX_SUFFIX_LEN, "ep%02x", | |
1094 | mdev->ep_address[i]); | |
1095 | ||
1096 | tmp_cap->name_suffix = &mdev->suffix[i][0]; | |
1097 | tmp_cap->buffer_size_packet = MAX_BUF_SIZE; | |
1098 | tmp_cap->buffer_size_streaming = MAX_BUF_SIZE; | |
1099 | tmp_cap->num_buffers_packet = BUF_CHAIN_SIZE; | |
1100 | tmp_cap->num_buffers_streaming = BUF_CHAIN_SIZE; | |
1101 | tmp_cap->data_type = MOST_CH_CONTROL | MOST_CH_ASYNC | | |
0540609f | 1102 | MOST_CH_ISOC | MOST_CH_SYNC; |
afd14cef | 1103 | if (usb_endpoint_dir_in(ep_desc)) |
a4198cdf CG |
1104 | tmp_cap->direction = MOST_CH_RX; |
1105 | else | |
1106 | tmp_cap->direction = MOST_CH_TX; | |
1107 | tmp_cap++; | |
27e6245e | 1108 | init_usb_anchor(&mdev->busy_urbs[i]); |
88d1878b | 1109 | spin_lock_init(&mdev->channel_lock[i]); |
a4198cdf | 1110 | } |
59ed0480 CG |
1111 | dev_notice(dev, "claimed gadget: Vendor=%4.4x ProdID=%4.4x Bus=%02x Device=%02x\n", |
1112 | le16_to_cpu(usb_dev->descriptor.idVendor), | |
1113 | le16_to_cpu(usb_dev->descriptor.idProduct), | |
1114 | usb_dev->bus->busnum, | |
1115 | usb_dev->devnum); | |
1116 | ||
1117 | dev_notice(dev, "device path: /sys/bus/usb/devices/%d-%s:%d.%d\n", | |
1118 | usb_dev->bus->busnum, | |
1119 | usb_dev->devpath, | |
1120 | usb_dev->config->desc.bConfigurationValue, | |
1121 | usb_iface_desc->desc.bInterfaceNumber); | |
a4198cdf | 1122 | |
4d5f022f CG |
1123 | ret = most_register_interface(&mdev->iface); |
1124 | if (ret) | |
a4198cdf | 1125 | goto exit_free4; |
a4198cdf CG |
1126 | |
1127 | mutex_lock(&mdev->io_mutex); | |
654f7ec4 | 1128 | if (le16_to_cpu(usb_dev->descriptor.idProduct) == USB_DEV_ID_OS81118 || |
5bf9bd8d CG |
1129 | le16_to_cpu(usb_dev->descriptor.idProduct) == USB_DEV_ID_OS81119 || |
1130 | le16_to_cpu(usb_dev->descriptor.idProduct) == USB_DEV_ID_OS81210) { | |
4d5f022f | 1131 | mdev->dci = kzalloc(sizeof(*mdev->dci), GFP_KERNEL); |
a4198cdf CG |
1132 | if (!mdev->dci) { |
1133 | mutex_unlock(&mdev->io_mutex); | |
1134 | most_deregister_interface(&mdev->iface); | |
1135 | ret = -ENOMEM; | |
1136 | goto exit_free4; | |
1137 | } | |
1138 | ||
4d5f022f CG |
1139 | mdev->dci->dev.init_name = "dci"; |
1140 | mdev->dci->dev.parent = &mdev->iface.dev; | |
1141 | mdev->dci->dev.groups = dci_attr_groups; | |
1142 | if (device_register(&mdev->dci->dev)) { | |
1143 | mutex_unlock(&mdev->io_mutex); | |
1144 | most_deregister_interface(&mdev->iface); | |
1145 | ret = -ENOMEM; | |
1146 | goto exit_free5; | |
1147 | } | |
a4198cdf | 1148 | mdev->dci->usb_device = mdev->usb_device; |
a4198cdf CG |
1149 | } |
1150 | mutex_unlock(&mdev->io_mutex); | |
1151 | return 0; | |
4d5f022f CG |
1152 | exit_free5: |
1153 | kfree(mdev->dci); | |
a4198cdf | 1154 | exit_free4: |
27e6245e | 1155 | kfree(mdev->busy_urbs); |
a4198cdf CG |
1156 | exit_free3: |
1157 | kfree(mdev->ep_address); | |
1158 | exit_free2: | |
1159 | kfree(mdev->cap); | |
1160 | exit_free1: | |
1161 | kfree(mdev->conf); | |
1162 | exit_free: | |
1163 | kfree(mdev); | |
1164 | exit_ENOMEM: | |
1165 | if (ret == 0 || ret == -ENOMEM) { | |
1166 | ret = -ENOMEM; | |
59ed0480 | 1167 | dev_err(dev, "out of memory\n"); |
a4198cdf CG |
1168 | } |
1169 | return ret; | |
1170 | } | |
1171 | ||
1172 | /** | |
1173 | * hdm_disconnect - disconnect function of USB device driver | |
1174 | * @interface: Interface of the attached USB device | |
1175 | * | |
1176 | * This deregisters the interface with the core, removes the kernel timer | |
1177 | * and frees resources. | |
1178 | * | |
1179 | * Context: hub kernel thread | |
1180 | */ | |
1181 | static void hdm_disconnect(struct usb_interface *interface) | |
1182 | { | |
089612f1 | 1183 | struct most_dev *mdev = usb_get_intfdata(interface); |
a4198cdf | 1184 | |
a4198cdf CG |
1185 | mutex_lock(&mdev->io_mutex); |
1186 | usb_set_intfdata(interface, NULL); | |
1187 | mdev->usb_device = NULL; | |
1188 | mutex_unlock(&mdev->io_mutex); | |
1189 | ||
1190 | del_timer_sync(&mdev->link_stat_timer); | |
1191 | cancel_work_sync(&mdev->poll_work_obj); | |
1192 | ||
4d5f022f CG |
1193 | device_unregister(&mdev->dci->dev); |
1194 | kfree(mdev->dci); | |
a4198cdf CG |
1195 | most_deregister_interface(&mdev->iface); |
1196 | ||
27e6245e | 1197 | kfree(mdev->busy_urbs); |
a4198cdf CG |
1198 | kfree(mdev->cap); |
1199 | kfree(mdev->conf); | |
1200 | kfree(mdev->ep_address); | |
1201 | kfree(mdev); | |
1202 | } | |
1203 | ||
1204 | static struct usb_driver hdm_usb = { | |
1205 | .name = "hdm_usb", | |
1206 | .id_table = usbid, | |
1207 | .probe = hdm_probe, | |
1208 | .disconnect = hdm_disconnect, | |
1209 | }; | |
1210 | ||
b9d7adc4 | 1211 | module_usb_driver(hdm_usb); |
a4198cdf CG |
1212 | MODULE_LICENSE("GPL"); |
1213 | MODULE_AUTHOR("Christian Gromm <christian.gromm@microchip.com>"); | |
1214 | MODULE_DESCRIPTION("HDM_4_USB"); |