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; | |
d35bbfaa AS |
120 | void (*on_netinfo)(struct most_interface *, unsigned char, |
121 | unsigned char *); | |
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: |
59ed0480 | 357 | dev_warn(dev, "Broken OUT pipe detected\n"); |
879c93fe | 358 | mdev->is_channel_healthy[channel] = false; |
cc28983c CG |
359 | mdev->clear_work[channel].pipe = urb->pipe; |
360 | schedule_work(&mdev->clear_work[channel].ws); | |
3a542007 | 361 | break; |
a4198cdf CG |
362 | case -ENODEV: |
363 | case -EPROTO: | |
364 | mbo->status = MBO_E_CLOSE; | |
365 | break; | |
a4198cdf | 366 | } |
a4198cdf CG |
367 | } |
368 | ||
cf6a599e | 369 | spin_unlock_irqrestore(lock, flags); |
a4198cdf CG |
370 | |
371 | if (likely(mbo->complete)) | |
372 | mbo->complete(mbo); | |
373 | usb_free_urb(urb); | |
374 | } | |
375 | ||
376 | /** | |
9f17591f | 377 | * hdm_read_completion - completion function for submitted Rx URBs |
a4198cdf CG |
378 | * @urb: the URB that has been completed |
379 | * | |
380 | * This checks the status of the completed URB. In case the URB has been | |
381 | * unlinked before it is immediately freed. On any other error the MBO transfer | |
382 | * flag is set. On success it frees allocated resources, removes | |
383 | * padding bytes -if necessary- and calls the completion function. | |
384 | * | |
385 | * Context: interrupt! | |
386 | * | |
387 | * ************************************************************************** | |
388 | * Error codes returned by in urb->status | |
389 | * or in iso_frame_desc[n].status (for ISO) | |
390 | * ************************************************************************* | |
391 | * | |
392 | * USB device drivers may only test urb status values in completion handlers. | |
393 | * This is because otherwise there would be a race between HCDs updating | |
394 | * these values on one CPU, and device drivers testing them on another CPU. | |
395 | * | |
396 | * A transfer's actual_length may be positive even when an error has been | |
397 | * reported. That's because transfers often involve several packets, so that | |
398 | * one or more packets could finish before an error stops further endpoint I/O. | |
399 | * | |
400 | * For isochronous URBs, the urb status value is non-zero only if the URB is | |
401 | * unlinked, the device is removed, the host controller is disabled or the total | |
402 | * transferred length is less than the requested length and the URB_SHORT_NOT_OK | |
403 | * flag is set. Completion handlers for isochronous URBs should only see | |
404 | * urb->status set to zero, -ENOENT, -ECONNRESET, -ESHUTDOWN, or -EREMOTEIO. | |
405 | * Individual frame descriptor status fields may report more status codes. | |
406 | * | |
407 | * | |
408 | * 0 Transfer completed successfully | |
409 | * | |
410 | * -ENOENT URB was synchronously unlinked by usb_unlink_urb | |
411 | * | |
412 | * -EINPROGRESS URB still pending, no results yet | |
413 | * (That is, if drivers see this it's a bug.) | |
414 | * | |
415 | * -EPROTO (*, **) a) bitstuff error | |
416 | * b) no response packet received within the | |
417 | * prescribed bus turn-around time | |
418 | * c) unknown USB error | |
419 | * | |
420 | * -EILSEQ (*, **) a) CRC mismatch | |
421 | * b) no response packet received within the | |
422 | * prescribed bus turn-around time | |
423 | * c) unknown USB error | |
424 | * | |
425 | * Note that often the controller hardware does not | |
426 | * distinguish among cases a), b), and c), so a | |
427 | * driver cannot tell whether there was a protocol | |
428 | * error, a failure to respond (often caused by | |
429 | * device disconnect), or some other fault. | |
430 | * | |
431 | * -ETIME (**) No response packet received within the prescribed | |
432 | * bus turn-around time. This error may instead be | |
433 | * reported as -EPROTO or -EILSEQ. | |
434 | * | |
435 | * -ETIMEDOUT Synchronous USB message functions use this code | |
436 | * to indicate timeout expired before the transfer | |
437 | * completed, and no other error was reported by HC. | |
438 | * | |
439 | * -EPIPE (**) Endpoint stalled. For non-control endpoints, | |
440 | * reset this status with usb_clear_halt(). | |
441 | * | |
442 | * -ECOMM During an IN transfer, the host controller | |
443 | * received data from an endpoint faster than it | |
444 | * could be written to system memory | |
445 | * | |
446 | * -ENOSR During an OUT transfer, the host controller | |
447 | * could not retrieve data from system memory fast | |
448 | * enough to keep up with the USB data rate | |
449 | * | |
450 | * -EOVERFLOW (*) The amount of data returned by the endpoint was | |
451 | * greater than either the max packet size of the | |
452 | * endpoint or the remaining buffer size. "Babble". | |
453 | * | |
454 | * -EREMOTEIO The data read from the endpoint did not fill the | |
455 | * specified buffer, and URB_SHORT_NOT_OK was set in | |
456 | * urb->transfer_flags. | |
457 | * | |
458 | * -ENODEV Device was removed. Often preceded by a burst of | |
459 | * other errors, since the hub driver doesn't detect | |
460 | * device removal events immediately. | |
461 | * | |
462 | * -EXDEV ISO transfer only partially completed | |
463 | * (only set in iso_frame_desc[n].status, not urb->status) | |
464 | * | |
465 | * -EINVAL ISO madness, if this happens: Log off and go home | |
466 | * | |
467 | * -ECONNRESET URB was asynchronously unlinked by usb_unlink_urb | |
468 | * | |
469 | * -ESHUTDOWN The device or host controller has been disabled due | |
470 | * to some problem that could not be worked around, | |
471 | * such as a physical disconnect. | |
472 | * | |
473 | * | |
474 | * (*) Error codes like -EPROTO, -EILSEQ and -EOVERFLOW normally indicate | |
475 | * hardware problems such as bad devices (including firmware) or cables. | |
476 | * | |
477 | * (**) This is also one of several codes that different kinds of host | |
478 | * controller use to indicate a transfer has failed because of device | |
479 | * disconnect. In the interval before the hub driver starts disconnect | |
480 | * processing, devices may receive such fault reports for every request. | |
481 | * | |
e1c3e6e1 | 482 | * See <https://www.kernel.org/doc/Documentation/driver-api/usb/error-codes.rst> |
a4198cdf CG |
483 | */ |
484 | static void hdm_read_completion(struct urb *urb) | |
485 | { | |
089612f1 | 486 | struct mbo *mbo = urb->context; |
089612f1 CG |
487 | struct most_dev *mdev = to_mdev(mbo->ifp); |
488 | unsigned int channel = mbo->hdm_channel_id; | |
489 | struct device *dev = &mdev->usb_device->dev; | |
88d1878b | 490 | spinlock_t *lock = mdev->channel_lock + channel; |
a4198cdf | 491 | unsigned long flags; |
a4198cdf | 492 | |
b24c9fe9 | 493 | spin_lock_irqsave(lock, flags); |
a4198cdf | 494 | |
3a542007 AS |
495 | mbo->processed_length = 0; |
496 | mbo->status = MBO_E_INVAL; | |
497 | if (likely(mdev->is_channel_healthy[channel])) { | |
a4198cdf | 498 | switch (urb->status) { |
3a542007 AS |
499 | case 0: |
500 | case -ESHUTDOWN: | |
501 | mbo->processed_length = urb->actual_length; | |
502 | mbo->status = MBO_SUCCESS; | |
503 | if (mdev->padding_active[channel] && | |
504 | hdm_remove_padding(mdev, channel, mbo)) { | |
505 | mbo->processed_length = 0; | |
506 | mbo->status = MBO_E_INVAL; | |
507 | } | |
508 | break; | |
a4198cdf | 509 | case -EPIPE: |
59ed0480 | 510 | dev_warn(dev, "Broken IN pipe detected\n"); |
879c93fe | 511 | mdev->is_channel_healthy[channel] = false; |
cc28983c CG |
512 | mdev->clear_work[channel].pipe = urb->pipe; |
513 | schedule_work(&mdev->clear_work[channel].ws); | |
3a542007 | 514 | break; |
a4198cdf CG |
515 | case -ENODEV: |
516 | case -EPROTO: | |
517 | mbo->status = MBO_E_CLOSE; | |
518 | break; | |
519 | case -EOVERFLOW: | |
59ed0480 | 520 | dev_warn(dev, "Babble on IN pipe detected\n"); |
a4198cdf CG |
521 | break; |
522 | } | |
a4198cdf | 523 | } |
b24c9fe9 | 524 | |
cf6a599e | 525 | spin_unlock_irqrestore(lock, flags); |
a4198cdf CG |
526 | |
527 | if (likely(mbo->complete)) | |
528 | mbo->complete(mbo); | |
529 | usb_free_urb(urb); | |
530 | } | |
531 | ||
532 | /** | |
533 | * hdm_enqueue - receive a buffer to be used for data transfer | |
534 | * @iface: interface to enqueue to | |
535 | * @channel: ID of the channel | |
536 | * @mbo: pointer to the buffer object | |
537 | * | |
538 | * This allocates a new URB and fills it according to the channel | |
539 | * that is being used for transmission of data. Before the URB is | |
540 | * submitted it is stored in the private anchor list. | |
541 | * | |
542 | * Returns 0 on success. On any error the URB is freed and a error code | |
543 | * is returned. | |
544 | * | |
545 | * Context: Could in _some_ cases be interrupt! | |
546 | */ | |
ba170ee2 CG |
547 | static int hdm_enqueue(struct most_interface *iface, int channel, |
548 | struct mbo *mbo) | |
a4198cdf CG |
549 | { |
550 | struct most_dev *mdev; | |
a4198cdf | 551 | struct most_channel_config *conf; |
59ed0480 | 552 | struct device *dev; |
a4198cdf CG |
553 | int retval = 0; |
554 | struct urb *urb; | |
a4198cdf CG |
555 | unsigned long length; |
556 | void *virt_address; | |
557 | ||
59ed0480 | 558 | if (unlikely(!iface || !mbo)) |
a4198cdf | 559 | return -EIO; |
dd53ecba | 560 | if (unlikely(iface->num_channels <= channel || channel < 0)) |
a4198cdf | 561 | return -ECHRNG; |
a4198cdf CG |
562 | |
563 | mdev = to_mdev(iface); | |
564 | conf = &mdev->conf[channel]; | |
59ed0480 | 565 | dev = &mdev->usb_device->dev; |
a4198cdf CG |
566 | |
567 | if (!mdev->usb_device) | |
568 | return -ENODEV; | |
569 | ||
570 | urb = usb_alloc_urb(NO_ISOCHRONOUS_URB, GFP_ATOMIC); | |
ca47d8f3 | 571 | if (!urb) |
a4198cdf | 572 | return -ENOMEM; |
a4198cdf | 573 | |
dd53ecba CG |
574 | if ((conf->direction & MOST_CH_TX) && mdev->padding_active[channel] && |
575 | hdm_add_padding(mdev, channel, mbo)) { | |
576 | retval = -EIO; | |
577 | goto _error; | |
578 | } | |
a4198cdf CG |
579 | |
580 | urb->transfer_dma = mbo->bus_address; | |
581 | virt_address = mbo->virt_address; | |
582 | length = mbo->buffer_length; | |
583 | ||
584 | if (conf->direction & MOST_CH_TX) { | |
585 | usb_fill_bulk_urb(urb, mdev->usb_device, | |
586 | usb_sndbulkpipe(mdev->usb_device, | |
587 | mdev->ep_address[channel]), | |
588 | virt_address, | |
589 | length, | |
590 | hdm_write_completion, | |
591 | mbo); | |
0540609f | 592 | if (conf->data_type != MOST_CH_ISOC) |
a4198cdf CG |
593 | urb->transfer_flags |= URB_ZERO_PACKET; |
594 | } else { | |
595 | usb_fill_bulk_urb(urb, mdev->usb_device, | |
596 | usb_rcvbulkpipe(mdev->usb_device, | |
597 | mdev->ep_address[channel]), | |
598 | virt_address, | |
9161e931 | 599 | length + conf->extra_len, |
a4198cdf CG |
600 | hdm_read_completion, |
601 | mbo); | |
602 | } | |
603 | urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; | |
604 | ||
27e6245e | 605 | usb_anchor_urb(urb, &mdev->busy_urbs[channel]); |
ec7e0a18 | 606 | |
a4198cdf CG |
607 | retval = usb_submit_urb(urb, GFP_KERNEL); |
608 | if (retval) { | |
59ed0480 | 609 | dev_err(dev, "URB submit failed with error %d.\n", retval); |
a4198cdf CG |
610 | goto _error_1; |
611 | } | |
612 | return 0; | |
613 | ||
614 | _error_1: | |
27e6245e | 615 | usb_unanchor_urb(urb); |
a4198cdf CG |
616 | _error: |
617 | usb_free_urb(urb); | |
618 | return retval; | |
619 | } | |
620 | ||
621 | /** | |
622 | * hdm_configure_channel - receive channel configuration from core | |
623 | * @iface: interface | |
624 | * @channel: channel ID | |
625 | * @conf: structure that holds the configuration information | |
25e3854c CG |
626 | * |
627 | * The attached network interface controller (NIC) supports a padding mode | |
628 | * to avoid short packets on USB, hence increasing the performance due to a | |
629 | * lower interrupt load. This mode is default for synchronous data and can | |
630 | * be switched on for isochronous data. In case padding is active the | |
631 | * driver needs to know the frame size of the payload in order to calculate | |
632 | * the number of bytes it needs to pad when transmitting or to cut off when | |
633 | * receiving data. | |
634 | * | |
a4198cdf | 635 | */ |
23fe15fa AR |
636 | static int hdm_configure_channel(struct most_interface *iface, int channel, |
637 | struct most_channel_config *conf) | |
a4198cdf CG |
638 | { |
639 | unsigned int num_frames; | |
640 | unsigned int frame_size; | |
089612f1 CG |
641 | struct most_dev *mdev = to_mdev(iface); |
642 | struct device *dev = &mdev->usb_device->dev; | |
59ed0480 | 643 | |
59ed0480 | 644 | mdev->is_channel_healthy[channel] = true; |
cc28983c CG |
645 | mdev->clear_work[channel].channel = channel; |
646 | mdev->clear_work[channel].mdev = mdev; | |
647 | INIT_WORK(&mdev->clear_work[channel].ws, wq_clear_halt); | |
a4198cdf CG |
648 | |
649 | if (unlikely(!iface || !conf)) { | |
59ed0480 | 650 | dev_err(dev, "Bad interface or config pointer.\n"); |
a4198cdf CG |
651 | return -EINVAL; |
652 | } | |
dd53ecba | 653 | if (unlikely(channel < 0 || channel >= iface->num_channels)) { |
59ed0480 | 654 | dev_err(dev, "Channel ID out of range.\n"); |
a4198cdf CG |
655 | return -EINVAL; |
656 | } | |
dd53ecba | 657 | if (!conf->num_buffers || !conf->buffer_size) { |
59ed0480 | 658 | dev_err(dev, "Misconfig: buffer size or #buffers zero.\n"); |
a4198cdf CG |
659 | return -EINVAL; |
660 | } | |
661 | ||
dd53ecba | 662 | if (conf->data_type != MOST_CH_SYNC && |
0540609f | 663 | !(conf->data_type == MOST_CH_ISOC && |
dd53ecba | 664 | conf->packets_per_xact != 0xFF)) { |
a4198cdf | 665 | mdev->padding_active[channel] = false; |
25e3854c CG |
666 | /* |
667 | * Since the NIC's padding mode is not going to be | |
668 | * used, we can skip the frame size calculations and | |
669 | * move directly on to exit. | |
670 | */ | |
a4198cdf CG |
671 | goto exit; |
672 | } | |
673 | ||
674 | mdev->padding_active[channel] = true; | |
a4198cdf | 675 | |
a4198cdf | 676 | frame_size = get_stream_frame_size(conf); |
dd53ecba | 677 | if (frame_size == 0 || frame_size > USB_MTU) { |
59ed0480 | 678 | dev_warn(dev, "Misconfig: frame size wrong\n"); |
a4198cdf CG |
679 | return -EINVAL; |
680 | } | |
681 | ||
f5001928 AS |
682 | num_frames = conf->buffer_size / frame_size; |
683 | ||
a4198cdf | 684 | if (conf->buffer_size % frame_size) { |
f5001928 | 685 | u16 old_size = conf->buffer_size; |
a4198cdf | 686 | |
f5001928 AS |
687 | conf->buffer_size = num_frames * frame_size; |
688 | dev_warn(dev, "%s: fixed buffer size (%d -> %d)\n", | |
689 | mdev->suffix[channel], old_size, conf->buffer_size); | |
690 | } | |
a4198cdf CG |
691 | |
692 | /* calculate extra length to comply w/ HW padding */ | |
f5001928 AS |
693 | conf->extra_len = num_frames * (USB_MTU - frame_size); |
694 | ||
a4198cdf CG |
695 | exit: |
696 | mdev->conf[channel] = *conf; | |
7c23baa9 AS |
697 | if (conf->data_type == MOST_CH_ASYNC) { |
698 | u16 ep = mdev->ep_address[channel]; | |
7c23baa9 | 699 | |
e33269f6 | 700 | if (start_sync_ep(mdev->usb_device, ep) < 0) |
7c23baa9 AS |
701 | dev_warn(dev, "sync for ep%02x failed", ep); |
702 | } | |
a4198cdf CG |
703 | return 0; |
704 | } | |
705 | ||
a4198cdf CG |
706 | /** |
707 | * hdm_request_netinfo - request network information | |
708 | * @iface: pointer to interface | |
709 | * @channel: channel ID | |
710 | * | |
711 | * This is used as trigger to set up the link status timer that | |
712 | * polls for the NI state of the INIC every 2 seconds. | |
713 | * | |
714 | */ | |
d35bbfaa AS |
715 | static void hdm_request_netinfo(struct most_interface *iface, int channel, |
716 | void (*on_netinfo)(struct most_interface *, | |
717 | unsigned char, | |
718 | unsigned char *)) | |
a4198cdf CG |
719 | { |
720 | struct most_dev *mdev; | |
721 | ||
722 | BUG_ON(!iface); | |
723 | mdev = to_mdev(iface); | |
d35bbfaa AS |
724 | mdev->on_netinfo = on_netinfo; |
725 | if (!on_netinfo) | |
726 | return; | |
727 | ||
a4198cdf CG |
728 | mdev->link_stat_timer.expires = jiffies + HZ; |
729 | mod_timer(&mdev->link_stat_timer, mdev->link_stat_timer.expires); | |
730 | } | |
731 | ||
732 | /** | |
f28e6cd3 | 733 | * link_stat_timer_handler - schedule work obtaining mac address and link status |
a4198cdf CG |
734 | * @data: pointer to USB device instance |
735 | * | |
736 | * The handler runs in interrupt context. That's why we need to defer the | |
737 | * tasks to a work queue. | |
738 | */ | |
e99e88a9 | 739 | static void link_stat_timer_handler(struct timer_list *t) |
a4198cdf | 740 | { |
e99e88a9 | 741 | struct most_dev *mdev = from_timer(mdev, t, link_stat_timer); |
a4198cdf | 742 | |
e3479f77 | 743 | schedule_work(&mdev->poll_work_obj); |
a4198cdf CG |
744 | mdev->link_stat_timer.expires = jiffies + (2 * HZ); |
745 | add_timer(&mdev->link_stat_timer); | |
746 | } | |
747 | ||
748 | /** | |
f28e6cd3 | 749 | * wq_netinfo - work queue function to deliver latest networking information |
a4198cdf CG |
750 | * @wq_obj: object that holds data for our deferred work to do |
751 | * | |
752 | * This retrieves the network interface status of the USB INIC | |
a4198cdf CG |
753 | */ |
754 | static void wq_netinfo(struct work_struct *wq_obj) | |
755 | { | |
089612f1 | 756 | struct most_dev *mdev = to_mdev_from_work(wq_obj); |
f28e6cd3 AS |
757 | struct usb_device *usb_device = mdev->usb_device; |
758 | struct device *dev = &usb_device->dev; | |
759 | u16 hi, mi, lo, link; | |
760 | u8 hw_addr[6]; | |
761 | ||
762 | if (drci_rd_reg(usb_device, DRCI_REG_HW_ADDR_HI, &hi) < 0) { | |
763 | dev_err(dev, "Vendor request 'hw_addr_hi' failed\n"); | |
764 | return; | |
765 | } | |
a4198cdf | 766 | |
f28e6cd3 AS |
767 | if (drci_rd_reg(usb_device, DRCI_REG_HW_ADDR_MI, &mi) < 0) { |
768 | dev_err(dev, "Vendor request 'hw_addr_mid' failed\n"); | |
769 | return; | |
770 | } | |
771 | ||
772 | if (drci_rd_reg(usb_device, DRCI_REG_HW_ADDR_LO, &lo) < 0) { | |
773 | dev_err(dev, "Vendor request 'hw_addr_low' failed\n"); | |
774 | return; | |
775 | } | |
a4198cdf | 776 | |
f28e6cd3 AS |
777 | if (drci_rd_reg(usb_device, DRCI_REG_NI_STATE, &link) < 0) { |
778 | dev_err(dev, "Vendor request 'link status' failed\n"); | |
a4198cdf | 779 | return; |
f28e6cd3 AS |
780 | } |
781 | ||
782 | hw_addr[0] = hi >> 8; | |
783 | hw_addr[1] = hi; | |
784 | hw_addr[2] = mi >> 8; | |
785 | hw_addr[3] = mi; | |
786 | hw_addr[4] = lo >> 8; | |
787 | hw_addr[5] = lo; | |
788 | ||
d35bbfaa AS |
789 | if (mdev->on_netinfo) |
790 | mdev->on_netinfo(&mdev->iface, link, hw_addr); | |
a4198cdf CG |
791 | } |
792 | ||
793 | /** | |
794 | * wq_clear_halt - work queue function | |
795 | * @wq_obj: work_struct object to execute | |
796 | * | |
797 | * This sends a clear_halt to the given USB pipe. | |
798 | */ | |
799 | static void wq_clear_halt(struct work_struct *wq_obj) | |
800 | { | |
cc28983c CG |
801 | struct clear_hold_work *clear_work = to_clear_hold_work(wq_obj); |
802 | struct most_dev *mdev = clear_work->mdev; | |
803 | unsigned int channel = clear_work->channel; | |
804 | int pipe = clear_work->pipe; | |
a4198cdf | 805 | |
cc28983c | 806 | mutex_lock(&mdev->io_mutex); |
bf9503f1 | 807 | most_stop_enqueue(&mdev->iface, channel); |
3a542007 | 808 | usb_kill_anchored_urbs(&mdev->busy_urbs[channel]); |
cc28983c | 809 | if (usb_clear_halt(mdev->usb_device, pipe)) |
59ed0480 | 810 | dev_warn(&mdev->usb_device->dev, "Failed to reset endpoint.\n"); |
a4198cdf | 811 | |
8f20f2dc CG |
812 | /* If the functional Stall condition has been set on an |
813 | * asynchronous rx channel, we need to clear the tx channel | |
814 | * too, since the hardware runs its clean-up sequence on both | |
815 | * channels, as they are physically one on the network. | |
816 | * | |
817 | * The USB interface that exposes the asynchronous channels | |
818 | * contains always two endpoints, and two only. | |
819 | */ | |
820 | if (mdev->conf[channel].data_type == MOST_CH_ASYNC && | |
821 | mdev->conf[channel].direction == MOST_CH_RX) { | |
822 | int peer = 1 - channel; | |
823 | int snd_pipe = usb_sndbulkpipe(mdev->usb_device, | |
824 | mdev->ep_address[peer]); | |
825 | usb_clear_halt(mdev->usb_device, snd_pipe); | |
826 | } | |
879c93fe | 827 | mdev->is_channel_healthy[channel] = true; |
72df4a55 | 828 | most_resume_enqueue(&mdev->iface, channel); |
cc28983c | 829 | mutex_unlock(&mdev->io_mutex); |
a4198cdf CG |
830 | } |
831 | ||
832 | /** | |
833 | * hdm_usb_fops - file operation table for USB driver | |
834 | */ | |
835 | static const struct file_operations hdm_usb_fops = { | |
836 | .owner = THIS_MODULE, | |
837 | }; | |
838 | ||
839 | /** | |
840 | * usb_device_id - ID table for HCD device probing | |
841 | */ | |
27acb557 | 842 | static const struct usb_device_id usbid[] = { |
a4198cdf | 843 | { USB_DEVICE(USB_VENDOR_ID_SMSC, USB_DEV_ID_BRDG), }, |
654f7ec4 | 844 | { USB_DEVICE(USB_VENDOR_ID_SMSC, USB_DEV_ID_OS81118), }, |
b50762ea | 845 | { USB_DEVICE(USB_VENDOR_ID_SMSC, USB_DEV_ID_OS81119), }, |
5bf9bd8d | 846 | { USB_DEVICE(USB_VENDOR_ID_SMSC, USB_DEV_ID_OS81210), }, |
a4198cdf CG |
847 | { } /* Terminating entry */ |
848 | }; | |
849 | ||
a747b42c CG |
850 | struct regs { |
851 | const char *name; | |
852 | u16 reg; | |
853 | }; | |
854 | ||
855 | static const struct regs ro_regs[] = { | |
856 | { "ni_state", DRCI_REG_NI_STATE }, | |
857 | { "packet_bandwidth", DRCI_REG_PACKET_BW }, | |
858 | { "node_address", DRCI_REG_NODE_ADDR }, | |
859 | { "node_position", DRCI_REG_NODE_POS }, | |
860 | }; | |
861 | ||
862 | static const struct regs rw_regs[] = { | |
863 | { "mep_filter", DRCI_REG_MEP_FILTER }, | |
864 | { "mep_hash0", DRCI_REG_HASH_TBL0 }, | |
865 | { "mep_hash1", DRCI_REG_HASH_TBL1 }, | |
866 | { "mep_hash2", DRCI_REG_HASH_TBL2 }, | |
867 | { "mep_hash3", DRCI_REG_HASH_TBL3 }, | |
868 | { "mep_eui48_hi", DRCI_REG_HW_ADDR_HI }, | |
869 | { "mep_eui48_mi", DRCI_REG_HW_ADDR_MI }, | |
870 | { "mep_eui48_lo", DRCI_REG_HW_ADDR_LO }, | |
871 | }; | |
872 | ||
873 | static int get_stat_reg_addr(const struct regs *regs, int size, | |
874 | const char *name, u16 *reg_addr) | |
875 | { | |
876 | int i; | |
877 | ||
878 | for (i = 0; i < size; i++) { | |
879 | if (!strcmp(name, regs[i].name)) { | |
880 | *reg_addr = regs[i].reg; | |
881 | return 0; | |
882 | } | |
883 | } | |
884 | return -EFAULT; | |
885 | } | |
886 | ||
887 | #define get_static_reg_addr(regs, name, reg_addr) \ | |
888 | get_stat_reg_addr(regs, ARRAY_SIZE(regs), name, reg_addr) | |
889 | ||
3d9c54b5 | 890 | static ssize_t value_show(struct device *dev, struct device_attribute *attr, |
4d5f022f | 891 | char *buf) |
a4198cdf | 892 | { |
98a3c4d7 | 893 | const char *name = attr->attr.name; |
4d5f022f | 894 | struct most_dci_obj *dci_obj = to_dci_obj(dev); |
0e9b9d08 | 895 | u16 val; |
a4198cdf CG |
896 | u16 reg_addr; |
897 | int err; | |
898 | ||
98a3c4d7 | 899 | if (!strcmp(name, "arb_address")) |
c0554645 | 900 | return snprintf(buf, PAGE_SIZE, "%04x\n", dci_obj->reg_addr); |
98a3c4d7 CG |
901 | |
902 | if (!strcmp(name, "arb_value")) | |
c0554645 | 903 | reg_addr = dci_obj->reg_addr; |
98a3c4d7 CG |
904 | else if (get_static_reg_addr(ro_regs, name, ®_addr) && |
905 | get_static_reg_addr(rw_regs, name, ®_addr)) | |
a27cb25b | 906 | return -EFAULT; |
a4198cdf | 907 | |
0e9b9d08 | 908 | err = drci_rd_reg(dci_obj->usb_device, reg_addr, &val); |
a4198cdf CG |
909 | if (err < 0) |
910 | return err; | |
911 | ||
0e9b9d08 | 912 | return snprintf(buf, PAGE_SIZE, "%04x\n", val); |
a4198cdf CG |
913 | } |
914 | ||
3d9c54b5 | 915 | static ssize_t value_store(struct device *dev, struct device_attribute *attr, |
a4198cdf CG |
916 | const char *buf, size_t count) |
917 | { | |
f1b9a843 | 918 | u16 val; |
a4198cdf | 919 | u16 reg_addr; |
98a3c4d7 | 920 | const char *name = attr->attr.name; |
4d5f022f | 921 | struct most_dci_obj *dci_obj = to_dci_obj(dev); |
e33269f6 | 922 | struct usb_device *usb_dev = dci_obj->usb_device; |
ac33fbb8 | 923 | int err = kstrtou16(buf, 16, &val); |
a4198cdf | 924 | |
c0554645 CG |
925 | if (err) |
926 | return err; | |
927 | ||
98a3c4d7 | 928 | if (!strcmp(name, "arb_address")) { |
c0554645 CG |
929 | dci_obj->reg_addr = val; |
930 | return count; | |
931 | } | |
98a3c4d7 | 932 | |
e33269f6 AS |
933 | if (!strcmp(name, "arb_value")) |
934 | err = drci_wr_reg(usb_dev, dci_obj->reg_addr, val); | |
935 | else if (!strcmp(name, "sync_ep")) | |
936 | err = start_sync_ep(usb_dev, val); | |
b2e8aa52 | 937 | else if (!get_static_reg_addr(rw_regs, name, ®_addr)) |
e33269f6 AS |
938 | err = drci_wr_reg(usb_dev, reg_addr, val); |
939 | else | |
d5cfb0ff | 940 | return -EFAULT; |
a4198cdf | 941 | |
a4198cdf CG |
942 | if (err < 0) |
943 | return err; | |
944 | ||
945 | return count; | |
946 | } | |
947 | ||
f15e3ad3 CG |
948 | static DEVICE_ATTR(ni_state, 0444, value_show, NULL); |
949 | static DEVICE_ATTR(packet_bandwidth, 0444, value_show, NULL); | |
950 | static DEVICE_ATTR(node_address, 0444, value_show, NULL); | |
951 | static DEVICE_ATTR(node_position, 0444, value_show, NULL); | |
952 | static DEVICE_ATTR(sync_ep, 0200, NULL, value_store); | |
953 | static DEVICE_ATTR(mep_filter, 0644, value_show, value_store); | |
954 | static DEVICE_ATTR(mep_hash0, 0644, value_show, value_store); | |
955 | static DEVICE_ATTR(mep_hash1, 0644, value_show, value_store); | |
956 | static DEVICE_ATTR(mep_hash2, 0644, value_show, value_store); | |
957 | static DEVICE_ATTR(mep_hash3, 0644, value_show, value_store); | |
958 | static DEVICE_ATTR(mep_eui48_hi, 0644, value_show, value_store); | |
959 | static DEVICE_ATTR(mep_eui48_mi, 0644, value_show, value_store); | |
960 | static DEVICE_ATTR(mep_eui48_lo, 0644, value_show, value_store); | |
961 | static DEVICE_ATTR(arb_address, 0644, value_show, value_store); | |
962 | static DEVICE_ATTR(arb_value, 0644, value_show, value_store); | |
4d5f022f CG |
963 | |
964 | static struct attribute *dci_attrs[] = { | |
965 | &dev_attr_ni_state.attr, | |
966 | &dev_attr_packet_bandwidth.attr, | |
967 | &dev_attr_node_address.attr, | |
968 | &dev_attr_node_position.attr, | |
969 | &dev_attr_sync_ep.attr, | |
970 | &dev_attr_mep_filter.attr, | |
971 | &dev_attr_mep_hash0.attr, | |
972 | &dev_attr_mep_hash1.attr, | |
973 | &dev_attr_mep_hash2.attr, | |
974 | &dev_attr_mep_hash3.attr, | |
975 | &dev_attr_mep_eui48_hi.attr, | |
976 | &dev_attr_mep_eui48_mi.attr, | |
977 | &dev_attr_mep_eui48_lo.attr, | |
978 | &dev_attr_arb_address.attr, | |
979 | &dev_attr_arb_value.attr, | |
a4198cdf CG |
980 | NULL, |
981 | }; | |
982 | ||
4d5f022f CG |
983 | static struct attribute_group dci_attr_group = { |
984 | .attrs = dci_attrs, | |
a4198cdf CG |
985 | }; |
986 | ||
4d5f022f CG |
987 | static const struct attribute_group *dci_attr_groups[] = { |
988 | &dci_attr_group, | |
989 | NULL, | |
990 | }; | |
a4198cdf CG |
991 | |
992 | /** | |
993 | * hdm_probe - probe function of USB device driver | |
994 | * @interface: Interface of the attached USB device | |
995 | * @id: Pointer to the USB ID table. | |
996 | * | |
997 | * This allocates and initializes the device instance, adds the new | |
998 | * entry to the internal list, scans the USB descriptors and registers | |
999 | * the interface with the core. | |
1000 | * Additionally, the DCI objects are created and the hardware is sync'd. | |
1001 | * | |
1002 | * Return 0 on success. In case of an error a negative number is returned. | |
1003 | */ | |
1004 | static int | |
1005 | hdm_probe(struct usb_interface *interface, const struct usb_device_id *id) | |
1006 | { | |
089612f1 CG |
1007 | struct usb_host_interface *usb_iface_desc = interface->cur_altsetting; |
1008 | struct usb_device *usb_dev = interface_to_usbdev(interface); | |
1009 | struct device *dev = &usb_dev->dev; | |
1010 | struct most_dev *mdev = kzalloc(sizeof(*mdev), GFP_KERNEL); | |
a4198cdf CG |
1011 | unsigned int i; |
1012 | unsigned int num_endpoints; | |
1013 | struct most_channel_capability *tmp_cap; | |
a4198cdf CG |
1014 | struct usb_endpoint_descriptor *ep_desc; |
1015 | int ret = 0; | |
1016 | ||
a4198cdf CG |
1017 | if (!mdev) |
1018 | goto exit_ENOMEM; | |
1019 | ||
1020 | usb_set_intfdata(interface, mdev); | |
1021 | num_endpoints = usb_iface_desc->desc.bNumEndpoints; | |
1022 | mutex_init(&mdev->io_mutex); | |
1023 | INIT_WORK(&mdev->poll_work_obj, wq_netinfo); | |
e99e88a9 | 1024 | timer_setup(&mdev->link_stat_timer, link_stat_timer_handler, 0); |
a4198cdf CG |
1025 | |
1026 | mdev->usb_device = usb_dev; | |
a4198cdf CG |
1027 | mdev->link_stat_timer.expires = jiffies + (2 * HZ); |
1028 | ||
1029 | mdev->iface.mod = hdm_usb_fops.owner; | |
1030 | mdev->iface.interface = ITYPE_USB; | |
1031 | mdev->iface.configure = hdm_configure_channel; | |
1032 | mdev->iface.request_netinfo = hdm_request_netinfo; | |
1033 | mdev->iface.enqueue = hdm_enqueue; | |
1034 | mdev->iface.poison_channel = hdm_poison_channel; | |
1035 | mdev->iface.description = mdev->description; | |
1036 | mdev->iface.num_channels = num_endpoints; | |
1037 | ||
1038 | snprintf(mdev->description, sizeof(mdev->description), | |
1039 | "usb_device %d-%s:%d.%d", | |
1040 | usb_dev->bus->busnum, | |
1041 | usb_dev->devpath, | |
1042 | usb_dev->config->desc.bConfigurationValue, | |
1043 | usb_iface_desc->desc.bInterfaceNumber); | |
1044 | ||
1045 | mdev->conf = kcalloc(num_endpoints, sizeof(*mdev->conf), GFP_KERNEL); | |
1046 | if (!mdev->conf) | |
1047 | goto exit_free; | |
1048 | ||
1049 | mdev->cap = kcalloc(num_endpoints, sizeof(*mdev->cap), GFP_KERNEL); | |
1050 | if (!mdev->cap) | |
1051 | goto exit_free1; | |
1052 | ||
1053 | mdev->iface.channel_vector = mdev->cap; | |
a4198cdf CG |
1054 | mdev->ep_address = |
1055 | kcalloc(num_endpoints, sizeof(*mdev->ep_address), GFP_KERNEL); | |
1056 | if (!mdev->ep_address) | |
1057 | goto exit_free2; | |
1058 | ||
27e6245e CG |
1059 | mdev->busy_urbs = |
1060 | kcalloc(num_endpoints, sizeof(*mdev->busy_urbs), GFP_KERNEL); | |
1061 | if (!mdev->busy_urbs) | |
a4198cdf CG |
1062 | goto exit_free3; |
1063 | ||
1064 | tmp_cap = mdev->cap; | |
1065 | for (i = 0; i < num_endpoints; i++) { | |
1066 | ep_desc = &usb_iface_desc->endpoint[i].desc; | |
1067 | mdev->ep_address[i] = ep_desc->bEndpointAddress; | |
1068 | mdev->padding_active[i] = false; | |
1069 | mdev->is_channel_healthy[i] = true; | |
1070 | ||
1071 | snprintf(&mdev->suffix[i][0], MAX_SUFFIX_LEN, "ep%02x", | |
1072 | mdev->ep_address[i]); | |
1073 | ||
1074 | tmp_cap->name_suffix = &mdev->suffix[i][0]; | |
1075 | tmp_cap->buffer_size_packet = MAX_BUF_SIZE; | |
1076 | tmp_cap->buffer_size_streaming = MAX_BUF_SIZE; | |
1077 | tmp_cap->num_buffers_packet = BUF_CHAIN_SIZE; | |
1078 | tmp_cap->num_buffers_streaming = BUF_CHAIN_SIZE; | |
1079 | tmp_cap->data_type = MOST_CH_CONTROL | MOST_CH_ASYNC | | |
0540609f | 1080 | MOST_CH_ISOC | MOST_CH_SYNC; |
afd14cef | 1081 | if (usb_endpoint_dir_in(ep_desc)) |
a4198cdf CG |
1082 | tmp_cap->direction = MOST_CH_RX; |
1083 | else | |
1084 | tmp_cap->direction = MOST_CH_TX; | |
1085 | tmp_cap++; | |
27e6245e | 1086 | init_usb_anchor(&mdev->busy_urbs[i]); |
88d1878b | 1087 | spin_lock_init(&mdev->channel_lock[i]); |
a4198cdf | 1088 | } |
59ed0480 CG |
1089 | dev_notice(dev, "claimed gadget: Vendor=%4.4x ProdID=%4.4x Bus=%02x Device=%02x\n", |
1090 | le16_to_cpu(usb_dev->descriptor.idVendor), | |
1091 | le16_to_cpu(usb_dev->descriptor.idProduct), | |
1092 | usb_dev->bus->busnum, | |
1093 | usb_dev->devnum); | |
1094 | ||
1095 | dev_notice(dev, "device path: /sys/bus/usb/devices/%d-%s:%d.%d\n", | |
1096 | usb_dev->bus->busnum, | |
1097 | usb_dev->devpath, | |
1098 | usb_dev->config->desc.bConfigurationValue, | |
1099 | usb_iface_desc->desc.bInterfaceNumber); | |
a4198cdf | 1100 | |
4d5f022f CG |
1101 | ret = most_register_interface(&mdev->iface); |
1102 | if (ret) | |
a4198cdf | 1103 | goto exit_free4; |
a4198cdf CG |
1104 | |
1105 | mutex_lock(&mdev->io_mutex); | |
654f7ec4 | 1106 | if (le16_to_cpu(usb_dev->descriptor.idProduct) == USB_DEV_ID_OS81118 || |
5bf9bd8d CG |
1107 | le16_to_cpu(usb_dev->descriptor.idProduct) == USB_DEV_ID_OS81119 || |
1108 | le16_to_cpu(usb_dev->descriptor.idProduct) == USB_DEV_ID_OS81210) { | |
4d5f022f | 1109 | mdev->dci = kzalloc(sizeof(*mdev->dci), GFP_KERNEL); |
a4198cdf CG |
1110 | if (!mdev->dci) { |
1111 | mutex_unlock(&mdev->io_mutex); | |
1112 | most_deregister_interface(&mdev->iface); | |
1113 | ret = -ENOMEM; | |
1114 | goto exit_free4; | |
1115 | } | |
1116 | ||
4d5f022f CG |
1117 | mdev->dci->dev.init_name = "dci"; |
1118 | mdev->dci->dev.parent = &mdev->iface.dev; | |
1119 | mdev->dci->dev.groups = dci_attr_groups; | |
1120 | if (device_register(&mdev->dci->dev)) { | |
1121 | mutex_unlock(&mdev->io_mutex); | |
1122 | most_deregister_interface(&mdev->iface); | |
1123 | ret = -ENOMEM; | |
1124 | goto exit_free5; | |
1125 | } | |
a4198cdf | 1126 | mdev->dci->usb_device = mdev->usb_device; |
a4198cdf CG |
1127 | } |
1128 | mutex_unlock(&mdev->io_mutex); | |
1129 | return 0; | |
4d5f022f CG |
1130 | exit_free5: |
1131 | kfree(mdev->dci); | |
a4198cdf | 1132 | exit_free4: |
27e6245e | 1133 | kfree(mdev->busy_urbs); |
a4198cdf CG |
1134 | exit_free3: |
1135 | kfree(mdev->ep_address); | |
1136 | exit_free2: | |
1137 | kfree(mdev->cap); | |
1138 | exit_free1: | |
1139 | kfree(mdev->conf); | |
1140 | exit_free: | |
1141 | kfree(mdev); | |
1142 | exit_ENOMEM: | |
1143 | if (ret == 0 || ret == -ENOMEM) { | |
1144 | ret = -ENOMEM; | |
59ed0480 | 1145 | dev_err(dev, "out of memory\n"); |
a4198cdf CG |
1146 | } |
1147 | return ret; | |
1148 | } | |
1149 | ||
1150 | /** | |
1151 | * hdm_disconnect - disconnect function of USB device driver | |
1152 | * @interface: Interface of the attached USB device | |
1153 | * | |
1154 | * This deregisters the interface with the core, removes the kernel timer | |
1155 | * and frees resources. | |
1156 | * | |
1157 | * Context: hub kernel thread | |
1158 | */ | |
1159 | static void hdm_disconnect(struct usb_interface *interface) | |
1160 | { | |
089612f1 | 1161 | struct most_dev *mdev = usb_get_intfdata(interface); |
a4198cdf | 1162 | |
a4198cdf CG |
1163 | mutex_lock(&mdev->io_mutex); |
1164 | usb_set_intfdata(interface, NULL); | |
1165 | mdev->usb_device = NULL; | |
1166 | mutex_unlock(&mdev->io_mutex); | |
1167 | ||
1168 | del_timer_sync(&mdev->link_stat_timer); | |
1169 | cancel_work_sync(&mdev->poll_work_obj); | |
1170 | ||
4d5f022f CG |
1171 | device_unregister(&mdev->dci->dev); |
1172 | kfree(mdev->dci); | |
a4198cdf CG |
1173 | most_deregister_interface(&mdev->iface); |
1174 | ||
27e6245e | 1175 | kfree(mdev->busy_urbs); |
a4198cdf CG |
1176 | kfree(mdev->cap); |
1177 | kfree(mdev->conf); | |
1178 | kfree(mdev->ep_address); | |
1179 | kfree(mdev); | |
1180 | } | |
1181 | ||
1182 | static struct usb_driver hdm_usb = { | |
1183 | .name = "hdm_usb", | |
1184 | .id_table = usbid, | |
1185 | .probe = hdm_probe, | |
1186 | .disconnect = hdm_disconnect, | |
1187 | }; | |
1188 | ||
b9d7adc4 | 1189 | module_usb_driver(hdm_usb); |
a4198cdf CG |
1190 | MODULE_LICENSE("GPL"); |
1191 | MODULE_AUTHOR("Christian Gromm <christian.gromm@microchip.com>"); | |
1192 | MODULE_DESCRIPTION("HDM_4_USB"); |