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