Commit | Line | Data |
---|---|---|
a4198cdf CG |
1 | /* |
2 | * hdm_usb.c - Hardware dependent module for USB | |
3 | * | |
4 | * Copyright (C) 2013-2015 Microchip Technology Germany II GmbH & Co. KG | |
5 | * | |
6 | * This program is distributed in the hope that it will be useful, | |
7 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
8 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
9 | * GNU General Public License for more details. | |
10 | * | |
11 | * This file is licensed under GPLv2. | |
12 | */ | |
13 | ||
14 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
15 | #include <linux/module.h> | |
16 | #include <linux/fs.h> | |
17 | #include <linux/usb.h> | |
18 | #include <linux/slab.h> | |
19 | #include <linux/init.h> | |
20 | #include <linux/cdev.h> | |
21 | #include <linux/device.h> | |
22 | #include <linux/list.h> | |
23 | #include <linux/completion.h> | |
24 | #include <linux/mutex.h> | |
25 | #include <linux/spinlock.h> | |
26 | #include <linux/interrupt.h> | |
27 | #include <linux/workqueue.h> | |
28 | #include <linux/sysfs.h> | |
29 | #include <linux/dma-mapping.h> | |
30 | #include <linux/etherdevice.h> | |
31 | #include <linux/uaccess.h> | |
32 | #include "mostcore.h" | |
33 | #include "networking.h" | |
34 | ||
35 | #define USB_MTU 512 | |
36 | #define NO_ISOCHRONOUS_URB 0 | |
37 | #define AV_PACKETS_PER_XACT 2 | |
38 | #define BUF_CHAIN_SIZE 0xFFFF | |
39 | #define MAX_NUM_ENDPOINTS 30 | |
40 | #define MAX_SUFFIX_LEN 10 | |
41 | #define MAX_STRING_LEN 80 | |
42 | #define MAX_BUF_SIZE 0xFFFF | |
a4198cdf CG |
43 | |
44 | #define USB_VENDOR_ID_SMSC 0x0424 /* VID: SMSC */ | |
45 | #define USB_DEV_ID_BRDG 0xC001 /* PID: USB Bridge */ | |
654f7ec4 | 46 | #define USB_DEV_ID_OS81118 0xCF18 /* PID: USB OS81118 */ |
b50762ea | 47 | #define USB_DEV_ID_OS81119 0xCF19 /* PID: USB OS81119 */ |
5bf9bd8d | 48 | #define USB_DEV_ID_OS81210 0xCF30 /* PID: USB OS81210 */ |
a4198cdf CG |
49 | /* DRCI Addresses */ |
50 | #define DRCI_REG_NI_STATE 0x0100 | |
51 | #define DRCI_REG_PACKET_BW 0x0101 | |
52 | #define DRCI_REG_NODE_ADDR 0x0102 | |
53 | #define DRCI_REG_NODE_POS 0x0103 | |
54 | #define DRCI_REG_MEP_FILTER 0x0140 | |
55 | #define DRCI_REG_HASH_TBL0 0x0141 | |
56 | #define DRCI_REG_HASH_TBL1 0x0142 | |
57 | #define DRCI_REG_HASH_TBL2 0x0143 | |
58 | #define DRCI_REG_HASH_TBL3 0x0144 | |
59 | #define DRCI_REG_HW_ADDR_HI 0x0145 | |
60 | #define DRCI_REG_HW_ADDR_MI 0x0146 | |
61 | #define DRCI_REG_HW_ADDR_LO 0x0147 | |
d747e8ec CG |
62 | #define DRCI_REG_BASE 0x1100 |
63 | #define DRCI_COMMAND 0x02 | |
a4198cdf CG |
64 | #define DRCI_READ_REQ 0xA0 |
65 | #define DRCI_WRITE_REQ 0xA1 | |
66 | ||
67 | /** | |
68 | * struct buf_anchor - used to create a list of pending URBs | |
69 | * @urb: pointer to USB request block | |
a4198cdf CG |
70 | * @list: linked list |
71 | * @urb_completion: | |
72 | */ | |
73 | struct buf_anchor { | |
74 | struct urb *urb; | |
a4198cdf | 75 | struct list_head list; |
a4198cdf | 76 | }; |
9cbe5aa6 | 77 | |
a4198cdf CG |
78 | /** |
79 | * struct most_dci_obj - Direct Communication Interface | |
80 | * @kobj:position in sysfs | |
81 | * @usb_device: pointer to the usb device | |
c0554645 | 82 | * @reg_addr: register address for arbitrary DCI access |
a4198cdf CG |
83 | */ |
84 | struct most_dci_obj { | |
85 | struct kobject kobj; | |
86 | struct usb_device *usb_device; | |
c0554645 | 87 | u16 reg_addr; |
a4198cdf | 88 | }; |
9cbe5aa6 | 89 | |
a4198cdf CG |
90 | #define to_dci_obj(p) container_of(p, struct most_dci_obj, kobj) |
91 | ||
cc28983c CG |
92 | struct most_dev; |
93 | ||
94 | struct clear_hold_work { | |
95 | struct work_struct ws; | |
96 | struct most_dev *mdev; | |
97 | unsigned int channel; | |
98 | int pipe; | |
99 | }; | |
100 | ||
101 | #define to_clear_hold_work(w) container_of(w, struct clear_hold_work, ws) | |
102 | ||
a4198cdf CG |
103 | /** |
104 | * struct most_dev - holds all usb interface specific stuff | |
105 | * @parent: parent object in sysfs | |
106 | * @usb_device: pointer to usb device | |
107 | * @iface: hardware interface | |
108 | * @cap: channel capabilities | |
109 | * @conf: channel configuration | |
110 | * @dci: direct communication interface of hardware | |
111 | * @hw_addr: MAC address of hardware | |
112 | * @ep_address: endpoint address table | |
113 | * @link_stat: link status of hardware | |
114 | * @description: device description | |
115 | * @suffix: suffix for channel name | |
116 | * @anchor_list_lock: locks list access | |
117 | * @padding_active: indicates channel uses padding | |
118 | * @is_channel_healthy: health status table of each channel | |
119 | * @anchor_list: list of anchored items | |
120 | * @io_mutex: synchronize I/O with disconnect | |
121 | * @link_stat_timer: timer for link status reports | |
122 | * @poll_work_obj: work for polling link status | |
123 | */ | |
124 | struct most_dev { | |
125 | struct kobject *parent; | |
126 | struct usb_device *usb_device; | |
127 | struct most_interface iface; | |
128 | struct most_channel_capability *cap; | |
129 | struct most_channel_config *conf; | |
130 | struct most_dci_obj *dci; | |
131 | u8 hw_addr[6]; | |
132 | u8 *ep_address; | |
133 | u16 link_stat; | |
134 | char description[MAX_STRING_LEN]; | |
135 | char suffix[MAX_NUM_ENDPOINTS][MAX_SUFFIX_LEN]; | |
136 | spinlock_t anchor_list_lock[MAX_NUM_ENDPOINTS]; | |
137 | bool padding_active[MAX_NUM_ENDPOINTS]; | |
138 | bool is_channel_healthy[MAX_NUM_ENDPOINTS]; | |
cc28983c | 139 | struct clear_hold_work clear_work[MAX_NUM_ENDPOINTS]; |
a4198cdf CG |
140 | struct list_head *anchor_list; |
141 | struct mutex io_mutex; | |
142 | struct timer_list link_stat_timer; | |
143 | struct work_struct poll_work_obj; | |
144 | }; | |
9cbe5aa6 | 145 | |
a4198cdf CG |
146 | #define to_mdev(d) container_of(d, struct most_dev, iface) |
147 | #define to_mdev_from_work(w) container_of(w, struct most_dev, poll_work_obj) | |
148 | ||
a4198cdf CG |
149 | static void wq_clear_halt(struct work_struct *wq_obj); |
150 | static void wq_netinfo(struct work_struct *wq_obj); | |
151 | ||
a4198cdf CG |
152 | /** |
153 | * drci_rd_reg - read a DCI register | |
154 | * @dev: usb device | |
155 | * @reg: register address | |
156 | * @buf: buffer to store data | |
157 | * | |
158 | * This is reads data from INIC's direct register communication interface | |
159 | */ | |
26370228 | 160 | static inline int drci_rd_reg(struct usb_device *dev, u16 reg, u16 *buf) |
a4198cdf | 161 | { |
26370228 CG |
162 | int retval; |
163 | u16 *dma_buf = kzalloc(sizeof(u16), GFP_KERNEL); | |
164 | u8 req_type = USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE; | |
165 | ||
166 | if (!dma_buf) | |
167 | return -ENOMEM; | |
168 | ||
169 | retval = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), | |
170 | DRCI_READ_REQ, req_type, | |
171 | 0x0000, | |
172 | reg, dma_buf, sizeof(u16), 5 * HZ); | |
c81c9c3e | 173 | *buf = le16_to_cpu(*dma_buf); |
26370228 CG |
174 | kfree(dma_buf); |
175 | ||
176 | return retval; | |
a4198cdf CG |
177 | } |
178 | ||
179 | /** | |
180 | * drci_wr_reg - write a DCI register | |
181 | * @dev: usb device | |
182 | * @reg: register address | |
183 | * @data: data to write | |
184 | * | |
185 | * This is writes data to INIC's direct register communication interface | |
186 | */ | |
187 | static inline int drci_wr_reg(struct usb_device *dev, u16 reg, u16 data) | |
188 | { | |
189 | return usb_control_msg(dev, | |
190 | usb_sndctrlpipe(dev, 0), | |
191 | DRCI_WRITE_REQ, | |
192 | USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, | |
193 | data, | |
194 | reg, | |
195 | NULL, | |
196 | 0, | |
197 | 5 * HZ); | |
198 | } | |
199 | ||
200 | /** | |
201 | * free_anchored_buffers - free device's anchored items | |
202 | * @mdev: the device | |
203 | * @channel: channel ID | |
9ed745ff | 204 | * @status: status of MBO termination |
a4198cdf | 205 | */ |
9ed745ff CG |
206 | static void free_anchored_buffers(struct most_dev *mdev, unsigned int channel, |
207 | enum mbo_status_flags status) | |
a4198cdf CG |
208 | { |
209 | struct mbo *mbo; | |
210 | struct buf_anchor *anchor, *tmp; | |
cf6a599e | 211 | spinlock_t *lock = mdev->anchor_list_lock + channel; /* temp. lock */ |
a4198cdf CG |
212 | unsigned long flags; |
213 | ||
cf6a599e | 214 | spin_lock_irqsave(lock, flags); |
ba170ee2 CG |
215 | list_for_each_entry_safe(anchor, tmp, &mdev->anchor_list[channel], |
216 | list) { | |
a4198cdf CG |
217 | struct urb *urb = anchor->urb; |
218 | ||
cf6a599e | 219 | spin_unlock_irqrestore(lock, flags); |
a4198cdf CG |
220 | if (likely(urb)) { |
221 | mbo = urb->context; | |
4b1a7cf1 | 222 | usb_kill_urb(urb); |
dd53ecba | 223 | if (mbo && mbo->complete) { |
9ed745ff | 224 | mbo->status = status; |
a4198cdf CG |
225 | mbo->processed_length = 0; |
226 | mbo->complete(mbo); | |
227 | } | |
228 | usb_free_urb(urb); | |
229 | } | |
cf6a599e | 230 | spin_lock_irqsave(lock, flags); |
a4198cdf CG |
231 | list_del(&anchor->list); |
232 | kfree(anchor); | |
233 | } | |
cf6a599e | 234 | spin_unlock_irqrestore(lock, flags); |
a4198cdf CG |
235 | } |
236 | ||
237 | /** | |
238 | * get_stream_frame_size - calculate frame size of current configuration | |
239 | * @cfg: channel configuration | |
240 | */ | |
241 | static unsigned int get_stream_frame_size(struct most_channel_config *cfg) | |
242 | { | |
243 | unsigned int frame_size = 0; | |
244 | unsigned int sub_size = cfg->subbuffer_size; | |
245 | ||
246 | if (!sub_size) { | |
59ed0480 | 247 | pr_warn("Misconfig: Subbuffer size zero.\n"); |
a4198cdf CG |
248 | return frame_size; |
249 | } | |
250 | switch (cfg->data_type) { | |
251 | case MOST_CH_ISOC_AVP: | |
252 | frame_size = AV_PACKETS_PER_XACT * sub_size; | |
253 | break; | |
254 | case MOST_CH_SYNC: | |
255 | if (cfg->packets_per_xact == 0) { | |
59ed0480 | 256 | pr_warn("Misconfig: Packets per XACT zero\n"); |
a4198cdf | 257 | frame_size = 0; |
9deba73d | 258 | } else if (cfg->packets_per_xact == 0xFF) { |
a4198cdf | 259 | frame_size = (USB_MTU / sub_size) * sub_size; |
9deba73d | 260 | } else { |
a4198cdf | 261 | frame_size = cfg->packets_per_xact * sub_size; |
9deba73d | 262 | } |
a4198cdf CG |
263 | break; |
264 | default: | |
265 | pr_warn("Query frame size of non-streaming channel\n"); | |
266 | break; | |
267 | } | |
268 | return frame_size; | |
269 | } | |
270 | ||
271 | /** | |
272 | * hdm_poison_channel - mark buffers of this channel as invalid | |
273 | * @iface: pointer to the interface | |
274 | * @channel: channel ID | |
275 | * | |
276 | * This unlinks all URBs submitted to the HCD, | |
277 | * calls the associated completion function of the core and removes | |
278 | * them from the list. | |
279 | * | |
280 | * Returns 0 on success or error code otherwise. | |
281 | */ | |
23fe15fa | 282 | static int hdm_poison_channel(struct most_interface *iface, int channel) |
a4198cdf | 283 | { |
089612f1 | 284 | struct most_dev *mdev = to_mdev(iface); |
b24c9fe9 CG |
285 | unsigned long flags; |
286 | spinlock_t *lock; /* temp. lock */ | |
a4198cdf CG |
287 | |
288 | if (unlikely(!iface)) { | |
59ed0480 | 289 | dev_warn(&mdev->usb_device->dev, "Poison: Bad interface.\n"); |
a4198cdf CG |
290 | return -EIO; |
291 | } | |
dd53ecba | 292 | if (unlikely(channel < 0 || channel >= iface->num_channels)) { |
59ed0480 | 293 | dev_warn(&mdev->usb_device->dev, "Channel ID out of range.\n"); |
a4198cdf CG |
294 | return -ECHRNG; |
295 | } | |
296 | ||
b24c9fe9 CG |
297 | lock = mdev->anchor_list_lock + channel; |
298 | spin_lock_irqsave(lock, flags); | |
a4198cdf | 299 | mdev->is_channel_healthy[channel] = false; |
b24c9fe9 | 300 | spin_unlock_irqrestore(lock, flags); |
a4198cdf | 301 | |
cc28983c CG |
302 | cancel_work_sync(&mdev->clear_work[channel].ws); |
303 | ||
a4198cdf | 304 | mutex_lock(&mdev->io_mutex); |
9ed745ff | 305 | free_anchored_buffers(mdev, channel, MBO_E_CLOSE); |
ec58d2a8 | 306 | if (mdev->padding_active[channel]) |
a4198cdf CG |
307 | mdev->padding_active[channel] = false; |
308 | ||
309 | if (mdev->conf[channel].data_type == MOST_CH_ASYNC) { | |
310 | del_timer_sync(&mdev->link_stat_timer); | |
311 | cancel_work_sync(&mdev->poll_work_obj); | |
312 | } | |
313 | mutex_unlock(&mdev->io_mutex); | |
314 | return 0; | |
315 | } | |
316 | ||
317 | /** | |
318 | * hdm_add_padding - add padding bytes | |
319 | * @mdev: most device | |
320 | * @channel: channel ID | |
321 | * @mbo: buffer object | |
322 | * | |
323 | * This inserts the INIC hardware specific padding bytes into a streaming | |
324 | * channel's buffer | |
325 | */ | |
23fe15fa | 326 | static int hdm_add_padding(struct most_dev *mdev, int channel, struct mbo *mbo) |
a4198cdf CG |
327 | { |
328 | struct most_channel_config *conf = &mdev->conf[channel]; | |
ac33fbb8 AS |
329 | unsigned int frame_size = get_stream_frame_size(conf); |
330 | unsigned int j, num_frames; | |
a4198cdf CG |
331 | u16 rd_addr, wr_addr; |
332 | ||
a4198cdf CG |
333 | if (!frame_size) |
334 | return -EIO; | |
335 | num_frames = mbo->buffer_length / frame_size; | |
336 | ||
337 | if (num_frames < 1) { | |
59ed0480 CG |
338 | dev_err(&mdev->usb_device->dev, |
339 | "Missed minimal transfer unit.\n"); | |
a4198cdf CG |
340 | return -EIO; |
341 | } | |
342 | ||
343 | for (j = 1; j < num_frames; j++) { | |
344 | wr_addr = (num_frames - j) * USB_MTU; | |
345 | rd_addr = (num_frames - j) * frame_size; | |
346 | memmove(mbo->virt_address + wr_addr, | |
347 | mbo->virt_address + rd_addr, | |
348 | frame_size); | |
349 | } | |
350 | mbo->buffer_length = num_frames * USB_MTU; | |
351 | return 0; | |
352 | } | |
353 | ||
354 | /** | |
355 | * hdm_remove_padding - remove padding bytes | |
356 | * @mdev: most device | |
357 | * @channel: channel ID | |
358 | * @mbo: buffer object | |
359 | * | |
360 | * This takes the INIC hardware specific padding bytes off a streaming | |
361 | * channel's buffer. | |
362 | */ | |
ba170ee2 CG |
363 | static int hdm_remove_padding(struct most_dev *mdev, int channel, |
364 | struct mbo *mbo) | |
a4198cdf | 365 | { |
a4198cdf | 366 | struct most_channel_config *const conf = &mdev->conf[channel]; |
ac33fbb8 AS |
367 | unsigned int frame_size = get_stream_frame_size(conf); |
368 | unsigned int j, num_frames; | |
a4198cdf | 369 | |
a4198cdf CG |
370 | if (!frame_size) |
371 | return -EIO; | |
372 | num_frames = mbo->processed_length / USB_MTU; | |
373 | ||
374 | for (j = 1; j < num_frames; j++) | |
375 | memmove(mbo->virt_address + frame_size * j, | |
376 | mbo->virt_address + USB_MTU * j, | |
377 | frame_size); | |
378 | ||
379 | mbo->processed_length = frame_size * num_frames; | |
380 | return 0; | |
381 | } | |
382 | ||
383 | /** | |
384 | * hdm_write_completion - completion function for submitted Tx URBs | |
385 | * @urb: the URB that has been completed | |
386 | * | |
387 | * This checks the status of the completed URB. In case the URB has been | |
388 | * unlinked before, it is immediately freed. On any other error the MBO | |
389 | * transfer flag is set. On success it frees allocated resources and calls | |
390 | * the completion function. | |
391 | * | |
392 | * Context: interrupt! | |
393 | */ | |
394 | static void hdm_write_completion(struct urb *urb) | |
395 | { | |
089612f1 CG |
396 | struct mbo *mbo = urb->context; |
397 | struct buf_anchor *anchor = mbo->priv; | |
398 | struct most_dev *mdev = to_mdev(mbo->ifp); | |
399 | unsigned int channel = mbo->hdm_channel_id; | |
400 | struct device *dev = &mdev->usb_device->dev; | |
401 | spinlock_t *lock = mdev->anchor_list_lock + channel; /* temp. lock */ | |
a4198cdf | 402 | unsigned long flags; |
a4198cdf | 403 | |
b24c9fe9 | 404 | spin_lock_irqsave(lock, flags); |
dd53ecba CG |
405 | if (urb->status == -ENOENT || urb->status == -ECONNRESET || |
406 | !mdev->is_channel_healthy[channel]) { | |
b24c9fe9 | 407 | spin_unlock_irqrestore(lock, flags); |
a4198cdf CG |
408 | return; |
409 | } | |
410 | ||
4246501e | 411 | if (unlikely(urb->status && urb->status != -ESHUTDOWN)) { |
a4198cdf CG |
412 | mbo->processed_length = 0; |
413 | switch (urb->status) { | |
414 | case -EPIPE: | |
59ed0480 | 415 | dev_warn(dev, "Broken OUT pipe detected\n"); |
879c93fe | 416 | mdev->is_channel_healthy[channel] = false; |
b24c9fe9 | 417 | spin_unlock_irqrestore(lock, flags); |
cc28983c CG |
418 | mdev->clear_work[channel].pipe = urb->pipe; |
419 | schedule_work(&mdev->clear_work[channel].ws); | |
a4198cdf CG |
420 | return; |
421 | case -ENODEV: | |
422 | case -EPROTO: | |
423 | mbo->status = MBO_E_CLOSE; | |
424 | break; | |
425 | default: | |
426 | mbo->status = MBO_E_INVAL; | |
427 | break; | |
428 | } | |
429 | } else { | |
430 | mbo->status = MBO_SUCCESS; | |
431 | mbo->processed_length = urb->actual_length; | |
432 | } | |
433 | ||
a4198cdf | 434 | list_del(&anchor->list); |
cf6a599e | 435 | spin_unlock_irqrestore(lock, flags); |
a4198cdf CG |
436 | kfree(anchor); |
437 | ||
438 | if (likely(mbo->complete)) | |
439 | mbo->complete(mbo); | |
440 | usb_free_urb(urb); | |
441 | } | |
442 | ||
443 | /** | |
9f17591f | 444 | * hdm_read_completion - completion function for submitted Rx URBs |
a4198cdf CG |
445 | * @urb: the URB that has been completed |
446 | * | |
447 | * This checks the status of the completed URB. In case the URB has been | |
448 | * unlinked before it is immediately freed. On any other error the MBO transfer | |
449 | * flag is set. On success it frees allocated resources, removes | |
450 | * padding bytes -if necessary- and calls the completion function. | |
451 | * | |
452 | * Context: interrupt! | |
453 | * | |
454 | * ************************************************************************** | |
455 | * Error codes returned by in urb->status | |
456 | * or in iso_frame_desc[n].status (for ISO) | |
457 | * ************************************************************************* | |
458 | * | |
459 | * USB device drivers may only test urb status values in completion handlers. | |
460 | * This is because otherwise there would be a race between HCDs updating | |
461 | * these values on one CPU, and device drivers testing them on another CPU. | |
462 | * | |
463 | * A transfer's actual_length may be positive even when an error has been | |
464 | * reported. That's because transfers often involve several packets, so that | |
465 | * one or more packets could finish before an error stops further endpoint I/O. | |
466 | * | |
467 | * For isochronous URBs, the urb status value is non-zero only if the URB is | |
468 | * unlinked, the device is removed, the host controller is disabled or the total | |
469 | * transferred length is less than the requested length and the URB_SHORT_NOT_OK | |
470 | * flag is set. Completion handlers for isochronous URBs should only see | |
471 | * urb->status set to zero, -ENOENT, -ECONNRESET, -ESHUTDOWN, or -EREMOTEIO. | |
472 | * Individual frame descriptor status fields may report more status codes. | |
473 | * | |
474 | * | |
475 | * 0 Transfer completed successfully | |
476 | * | |
477 | * -ENOENT URB was synchronously unlinked by usb_unlink_urb | |
478 | * | |
479 | * -EINPROGRESS URB still pending, no results yet | |
480 | * (That is, if drivers see this it's a bug.) | |
481 | * | |
482 | * -EPROTO (*, **) a) bitstuff error | |
483 | * b) no response packet received within the | |
484 | * prescribed bus turn-around time | |
485 | * c) unknown USB error | |
486 | * | |
487 | * -EILSEQ (*, **) a) CRC mismatch | |
488 | * b) no response packet received within the | |
489 | * prescribed bus turn-around time | |
490 | * c) unknown USB error | |
491 | * | |
492 | * Note that often the controller hardware does not | |
493 | * distinguish among cases a), b), and c), so a | |
494 | * driver cannot tell whether there was a protocol | |
495 | * error, a failure to respond (often caused by | |
496 | * device disconnect), or some other fault. | |
497 | * | |
498 | * -ETIME (**) No response packet received within the prescribed | |
499 | * bus turn-around time. This error may instead be | |
500 | * reported as -EPROTO or -EILSEQ. | |
501 | * | |
502 | * -ETIMEDOUT Synchronous USB message functions use this code | |
503 | * to indicate timeout expired before the transfer | |
504 | * completed, and no other error was reported by HC. | |
505 | * | |
506 | * -EPIPE (**) Endpoint stalled. For non-control endpoints, | |
507 | * reset this status with usb_clear_halt(). | |
508 | * | |
509 | * -ECOMM During an IN transfer, the host controller | |
510 | * received data from an endpoint faster than it | |
511 | * could be written to system memory | |
512 | * | |
513 | * -ENOSR During an OUT transfer, the host controller | |
514 | * could not retrieve data from system memory fast | |
515 | * enough to keep up with the USB data rate | |
516 | * | |
517 | * -EOVERFLOW (*) The amount of data returned by the endpoint was | |
518 | * greater than either the max packet size of the | |
519 | * endpoint or the remaining buffer size. "Babble". | |
520 | * | |
521 | * -EREMOTEIO The data read from the endpoint did not fill the | |
522 | * specified buffer, and URB_SHORT_NOT_OK was set in | |
523 | * urb->transfer_flags. | |
524 | * | |
525 | * -ENODEV Device was removed. Often preceded by a burst of | |
526 | * other errors, since the hub driver doesn't detect | |
527 | * device removal events immediately. | |
528 | * | |
529 | * -EXDEV ISO transfer only partially completed | |
530 | * (only set in iso_frame_desc[n].status, not urb->status) | |
531 | * | |
532 | * -EINVAL ISO madness, if this happens: Log off and go home | |
533 | * | |
534 | * -ECONNRESET URB was asynchronously unlinked by usb_unlink_urb | |
535 | * | |
536 | * -ESHUTDOWN The device or host controller has been disabled due | |
537 | * to some problem that could not be worked around, | |
538 | * such as a physical disconnect. | |
539 | * | |
540 | * | |
541 | * (*) Error codes like -EPROTO, -EILSEQ and -EOVERFLOW normally indicate | |
542 | * hardware problems such as bad devices (including firmware) or cables. | |
543 | * | |
544 | * (**) This is also one of several codes that different kinds of host | |
545 | * controller use to indicate a transfer has failed because of device | |
546 | * disconnect. In the interval before the hub driver starts disconnect | |
547 | * processing, devices may receive such fault reports for every request. | |
548 | * | |
549 | * See <https://www.kernel.org/doc/Documentation/usb/error-codes.txt> | |
550 | */ | |
551 | static void hdm_read_completion(struct urb *urb) | |
552 | { | |
089612f1 CG |
553 | struct mbo *mbo = urb->context; |
554 | struct buf_anchor *anchor = mbo->priv; | |
555 | struct most_dev *mdev = to_mdev(mbo->ifp); | |
556 | unsigned int channel = mbo->hdm_channel_id; | |
557 | struct device *dev = &mdev->usb_device->dev; | |
558 | spinlock_t *lock = mdev->anchor_list_lock + channel; /* temp. lock */ | |
a4198cdf | 559 | unsigned long flags; |
a4198cdf | 560 | |
b24c9fe9 | 561 | spin_lock_irqsave(lock, flags); |
dd53ecba CG |
562 | if (urb->status == -ENOENT || urb->status == -ECONNRESET || |
563 | !mdev->is_channel_healthy[channel]) { | |
b24c9fe9 | 564 | spin_unlock_irqrestore(lock, flags); |
a4198cdf CG |
565 | return; |
566 | } | |
567 | ||
4246501e | 568 | if (unlikely(urb->status && urb->status != -ESHUTDOWN)) { |
a4198cdf CG |
569 | mbo->processed_length = 0; |
570 | switch (urb->status) { | |
571 | case -EPIPE: | |
59ed0480 | 572 | dev_warn(dev, "Broken IN pipe detected\n"); |
879c93fe | 573 | mdev->is_channel_healthy[channel] = false; |
b24c9fe9 | 574 | spin_unlock_irqrestore(lock, flags); |
cc28983c CG |
575 | mdev->clear_work[channel].pipe = urb->pipe; |
576 | schedule_work(&mdev->clear_work[channel].ws); | |
a4198cdf CG |
577 | return; |
578 | case -ENODEV: | |
579 | case -EPROTO: | |
580 | mbo->status = MBO_E_CLOSE; | |
581 | break; | |
582 | case -EOVERFLOW: | |
59ed0480 | 583 | dev_warn(dev, "Babble on IN pipe detected\n"); |
a4198cdf CG |
584 | default: |
585 | mbo->status = MBO_E_INVAL; | |
586 | break; | |
587 | } | |
588 | } else { | |
589 | mbo->processed_length = urb->actual_length; | |
cf059183 CG |
590 | mbo->status = MBO_SUCCESS; |
591 | if (mdev->padding_active[channel] && | |
592 | hdm_remove_padding(mdev, channel, mbo)) { | |
593 | mbo->processed_length = 0; | |
594 | mbo->status = MBO_E_INVAL; | |
a4198cdf CG |
595 | } |
596 | } | |
b24c9fe9 | 597 | |
a4198cdf | 598 | list_del(&anchor->list); |
cf6a599e | 599 | spin_unlock_irqrestore(lock, flags); |
a4198cdf CG |
600 | kfree(anchor); |
601 | ||
602 | if (likely(mbo->complete)) | |
603 | mbo->complete(mbo); | |
604 | usb_free_urb(urb); | |
605 | } | |
606 | ||
607 | /** | |
608 | * hdm_enqueue - receive a buffer to be used for data transfer | |
609 | * @iface: interface to enqueue to | |
610 | * @channel: ID of the channel | |
611 | * @mbo: pointer to the buffer object | |
612 | * | |
613 | * This allocates a new URB and fills it according to the channel | |
614 | * that is being used for transmission of data. Before the URB is | |
615 | * submitted it is stored in the private anchor list. | |
616 | * | |
617 | * Returns 0 on success. On any error the URB is freed and a error code | |
618 | * is returned. | |
619 | * | |
620 | * Context: Could in _some_ cases be interrupt! | |
621 | */ | |
ba170ee2 CG |
622 | static int hdm_enqueue(struct most_interface *iface, int channel, |
623 | struct mbo *mbo) | |
a4198cdf CG |
624 | { |
625 | struct most_dev *mdev; | |
626 | struct buf_anchor *anchor; | |
627 | struct most_channel_config *conf; | |
59ed0480 | 628 | struct device *dev; |
a4198cdf CG |
629 | int retval = 0; |
630 | struct urb *urb; | |
631 | unsigned long flags; | |
632 | unsigned long length; | |
633 | void *virt_address; | |
cf6a599e | 634 | spinlock_t *lock; /* temp. lock */ |
a4198cdf | 635 | |
59ed0480 | 636 | if (unlikely(!iface || !mbo)) |
a4198cdf | 637 | return -EIO; |
dd53ecba | 638 | if (unlikely(iface->num_channels <= channel || channel < 0)) |
a4198cdf | 639 | return -ECHRNG; |
a4198cdf CG |
640 | |
641 | mdev = to_mdev(iface); | |
642 | conf = &mdev->conf[channel]; | |
59ed0480 | 643 | dev = &mdev->usb_device->dev; |
a4198cdf CG |
644 | |
645 | if (!mdev->usb_device) | |
646 | return -ENODEV; | |
647 | ||
648 | urb = usb_alloc_urb(NO_ISOCHRONOUS_URB, GFP_ATOMIC); | |
ca47d8f3 | 649 | if (!urb) |
a4198cdf | 650 | return -ENOMEM; |
a4198cdf CG |
651 | |
652 | anchor = kzalloc(sizeof(*anchor), GFP_ATOMIC); | |
653 | if (!anchor) { | |
654 | retval = -ENOMEM; | |
655 | goto _error; | |
656 | } | |
657 | ||
658 | anchor->urb = urb; | |
a4198cdf CG |
659 | mbo->priv = anchor; |
660 | ||
dd53ecba CG |
661 | if ((conf->direction & MOST_CH_TX) && mdev->padding_active[channel] && |
662 | hdm_add_padding(mdev, channel, mbo)) { | |
663 | retval = -EIO; | |
664 | goto _error; | |
665 | } | |
a4198cdf CG |
666 | |
667 | urb->transfer_dma = mbo->bus_address; | |
668 | virt_address = mbo->virt_address; | |
669 | length = mbo->buffer_length; | |
670 | ||
671 | if (conf->direction & MOST_CH_TX) { | |
672 | usb_fill_bulk_urb(urb, mdev->usb_device, | |
673 | usb_sndbulkpipe(mdev->usb_device, | |
674 | mdev->ep_address[channel]), | |
675 | virt_address, | |
676 | length, | |
677 | hdm_write_completion, | |
678 | mbo); | |
679 | if (conf->data_type != MOST_CH_ISOC_AVP) | |
680 | urb->transfer_flags |= URB_ZERO_PACKET; | |
681 | } else { | |
682 | usb_fill_bulk_urb(urb, mdev->usb_device, | |
683 | usb_rcvbulkpipe(mdev->usb_device, | |
684 | mdev->ep_address[channel]), | |
685 | virt_address, | |
9161e931 | 686 | length + conf->extra_len, |
a4198cdf CG |
687 | hdm_read_completion, |
688 | mbo); | |
689 | } | |
690 | urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; | |
691 | ||
cf6a599e CG |
692 | lock = mdev->anchor_list_lock + channel; |
693 | spin_lock_irqsave(lock, flags); | |
ec7e0a18 | 694 | list_add_tail(&anchor->list, &mdev->anchor_list[channel]); |
cf6a599e | 695 | spin_unlock_irqrestore(lock, flags); |
ec7e0a18 | 696 | |
a4198cdf CG |
697 | retval = usb_submit_urb(urb, GFP_KERNEL); |
698 | if (retval) { | |
59ed0480 | 699 | dev_err(dev, "URB submit failed with error %d.\n", retval); |
a4198cdf CG |
700 | goto _error_1; |
701 | } | |
702 | return 0; | |
703 | ||
704 | _error_1: | |
cf6a599e | 705 | spin_lock_irqsave(lock, flags); |
a4198cdf | 706 | list_del(&anchor->list); |
cf6a599e | 707 | spin_unlock_irqrestore(lock, flags); |
a4198cdf CG |
708 | kfree(anchor); |
709 | _error: | |
710 | usb_free_urb(urb); | |
711 | return retval; | |
712 | } | |
713 | ||
714 | /** | |
715 | * hdm_configure_channel - receive channel configuration from core | |
716 | * @iface: interface | |
717 | * @channel: channel ID | |
718 | * @conf: structure that holds the configuration information | |
719 | */ | |
23fe15fa AR |
720 | static int hdm_configure_channel(struct most_interface *iface, int channel, |
721 | struct most_channel_config *conf) | |
a4198cdf CG |
722 | { |
723 | unsigned int num_frames; | |
724 | unsigned int frame_size; | |
725 | unsigned int temp_size; | |
726 | unsigned int tail_space; | |
089612f1 CG |
727 | struct most_dev *mdev = to_mdev(iface); |
728 | struct device *dev = &mdev->usb_device->dev; | |
59ed0480 | 729 | |
59ed0480 | 730 | mdev->is_channel_healthy[channel] = true; |
cc28983c CG |
731 | mdev->clear_work[channel].channel = channel; |
732 | mdev->clear_work[channel].mdev = mdev; | |
733 | INIT_WORK(&mdev->clear_work[channel].ws, wq_clear_halt); | |
a4198cdf CG |
734 | |
735 | if (unlikely(!iface || !conf)) { | |
59ed0480 | 736 | dev_err(dev, "Bad interface or config pointer.\n"); |
a4198cdf CG |
737 | return -EINVAL; |
738 | } | |
dd53ecba | 739 | if (unlikely(channel < 0 || channel >= iface->num_channels)) { |
59ed0480 | 740 | dev_err(dev, "Channel ID out of range.\n"); |
a4198cdf CG |
741 | return -EINVAL; |
742 | } | |
dd53ecba | 743 | if (!conf->num_buffers || !conf->buffer_size) { |
59ed0480 | 744 | dev_err(dev, "Misconfig: buffer size or #buffers zero.\n"); |
a4198cdf CG |
745 | return -EINVAL; |
746 | } | |
747 | ||
dd53ecba CG |
748 | if (conf->data_type != MOST_CH_SYNC && |
749 | !(conf->data_type == MOST_CH_ISOC_AVP && | |
750 | conf->packets_per_xact != 0xFF)) { | |
a4198cdf CG |
751 | mdev->padding_active[channel] = false; |
752 | goto exit; | |
753 | } | |
754 | ||
755 | mdev->padding_active[channel] = true; | |
756 | temp_size = conf->buffer_size; | |
757 | ||
a4198cdf | 758 | frame_size = get_stream_frame_size(conf); |
dd53ecba | 759 | if (frame_size == 0 || frame_size > USB_MTU) { |
59ed0480 | 760 | dev_warn(dev, "Misconfig: frame size wrong\n"); |
a4198cdf CG |
761 | return -EINVAL; |
762 | } | |
763 | ||
764 | if (conf->buffer_size % frame_size) { | |
765 | u16 tmp_val; | |
766 | ||
767 | tmp_val = conf->buffer_size / frame_size; | |
768 | conf->buffer_size = tmp_val * frame_size; | |
59ed0480 | 769 | dev_notice(dev, |
ba170ee2 | 770 | "Channel %d - rounding buffer size to %d bytes, channel config says %d bytes\n", |
59ed0480 CG |
771 | channel, |
772 | conf->buffer_size, | |
773 | temp_size); | |
a4198cdf CG |
774 | } |
775 | ||
776 | num_frames = conf->buffer_size / frame_size; | |
777 | tail_space = num_frames * (USB_MTU - frame_size); | |
778 | temp_size += tail_space; | |
779 | ||
780 | /* calculate extra length to comply w/ HW padding */ | |
dce57aef | 781 | conf->extra_len = (DIV_ROUND_UP(temp_size, USB_MTU) * USB_MTU) |
a4198cdf CG |
782 | - conf->buffer_size; |
783 | exit: | |
784 | mdev->conf[channel] = *conf; | |
785 | return 0; | |
786 | } | |
787 | ||
788 | /** | |
789 | * hdm_update_netinfo - retrieve latest networking information | |
790 | * @mdev: device interface | |
791 | * | |
792 | * This triggers the USB vendor requests to read the hardware address and | |
793 | * the current link status of the attached device. | |
794 | */ | |
23fe15fa | 795 | static int hdm_update_netinfo(struct most_dev *mdev) |
a4198cdf | 796 | { |
cc8d9935 CG |
797 | struct usb_device *usb_device = mdev->usb_device; |
798 | struct device *dev = &usb_device->dev; | |
1ea7e502 | 799 | u16 hi, mi, lo, link; |
a4198cdf CG |
800 | |
801 | if (!is_valid_ether_addr(mdev->hw_addr)) { | |
cc8d9935 | 802 | if (drci_rd_reg(usb_device, DRCI_REG_HW_ADDR_HI, &hi) < 0) { |
59ed0480 | 803 | dev_err(dev, "Vendor request \"hw_addr_hi\" failed\n"); |
d5cfb0ff | 804 | return -EFAULT; |
a4198cdf | 805 | } |
1ea7e502 | 806 | |
cc8d9935 | 807 | if (drci_rd_reg(usb_device, DRCI_REG_HW_ADDR_MI, &mi) < 0) { |
59ed0480 | 808 | dev_err(dev, "Vendor request \"hw_addr_mid\" failed\n"); |
d5cfb0ff | 809 | return -EFAULT; |
a4198cdf | 810 | } |
1ea7e502 | 811 | |
cc8d9935 | 812 | if (drci_rd_reg(usb_device, DRCI_REG_HW_ADDR_LO, &lo) < 0) { |
59ed0480 | 813 | dev_err(dev, "Vendor request \"hw_addr_low\" failed\n"); |
d5cfb0ff | 814 | return -EFAULT; |
a4198cdf | 815 | } |
1ea7e502 | 816 | |
a4198cdf | 817 | mutex_lock(&mdev->io_mutex); |
1ea7e502 CG |
818 | mdev->hw_addr[0] = hi >> 8; |
819 | mdev->hw_addr[1] = hi; | |
820 | mdev->hw_addr[2] = mi >> 8; | |
821 | mdev->hw_addr[3] = mi; | |
822 | mdev->hw_addr[4] = lo >> 8; | |
823 | mdev->hw_addr[5] = lo; | |
a4198cdf | 824 | mutex_unlock(&mdev->io_mutex); |
a4198cdf | 825 | } |
cc8d9935 CG |
826 | |
827 | if (drci_rd_reg(usb_device, DRCI_REG_NI_STATE, &link) < 0) { | |
59ed0480 | 828 | dev_err(dev, "Vendor request \"link status\" failed\n"); |
d5cfb0ff | 829 | return -EFAULT; |
a4198cdf | 830 | } |
cc8d9935 | 831 | |
a4198cdf CG |
832 | mutex_lock(&mdev->io_mutex); |
833 | mdev->link_stat = link; | |
834 | mutex_unlock(&mdev->io_mutex); | |
835 | return 0; | |
836 | } | |
837 | ||
838 | /** | |
839 | * hdm_request_netinfo - request network information | |
840 | * @iface: pointer to interface | |
841 | * @channel: channel ID | |
842 | * | |
843 | * This is used as trigger to set up the link status timer that | |
844 | * polls for the NI state of the INIC every 2 seconds. | |
845 | * | |
846 | */ | |
23fe15fa | 847 | static void hdm_request_netinfo(struct most_interface *iface, int channel) |
a4198cdf CG |
848 | { |
849 | struct most_dev *mdev; | |
850 | ||
851 | BUG_ON(!iface); | |
852 | mdev = to_mdev(iface); | |
853 | mdev->link_stat_timer.expires = jiffies + HZ; | |
854 | mod_timer(&mdev->link_stat_timer, mdev->link_stat_timer.expires); | |
855 | } | |
856 | ||
857 | /** | |
858 | * link_stat_timer_handler - add work to link_stat work queue | |
859 | * @data: pointer to USB device instance | |
860 | * | |
861 | * The handler runs in interrupt context. That's why we need to defer the | |
862 | * tasks to a work queue. | |
863 | */ | |
864 | static void link_stat_timer_handler(unsigned long data) | |
865 | { | |
866 | struct most_dev *mdev = (struct most_dev *)data; | |
867 | ||
e3479f77 | 868 | schedule_work(&mdev->poll_work_obj); |
a4198cdf CG |
869 | mdev->link_stat_timer.expires = jiffies + (2 * HZ); |
870 | add_timer(&mdev->link_stat_timer); | |
871 | } | |
872 | ||
873 | /** | |
874 | * wq_netinfo - work queue function | |
875 | * @wq_obj: object that holds data for our deferred work to do | |
876 | * | |
877 | * This retrieves the network interface status of the USB INIC | |
878 | * and compares it with the current status. If the status has | |
879 | * changed, it updates the status of the core. | |
880 | */ | |
881 | static void wq_netinfo(struct work_struct *wq_obj) | |
882 | { | |
089612f1 CG |
883 | struct most_dev *mdev = to_mdev_from_work(wq_obj); |
884 | int i, prev_link_stat = mdev->link_stat; | |
a4198cdf CG |
885 | u8 prev_hw_addr[6]; |
886 | ||
a4198cdf CG |
887 | for (i = 0; i < 6; i++) |
888 | prev_hw_addr[i] = mdev->hw_addr[i]; | |
889 | ||
ba170ee2 | 890 | if (hdm_update_netinfo(mdev) < 0) |
a4198cdf | 891 | return; |
dd53ecba CG |
892 | if (prev_link_stat != mdev->link_stat || |
893 | prev_hw_addr[0] != mdev->hw_addr[0] || | |
894 | prev_hw_addr[1] != mdev->hw_addr[1] || | |
895 | prev_hw_addr[2] != mdev->hw_addr[2] || | |
896 | prev_hw_addr[3] != mdev->hw_addr[3] || | |
897 | prev_hw_addr[4] != mdev->hw_addr[4] || | |
898 | prev_hw_addr[5] != mdev->hw_addr[5]) | |
a4198cdf CG |
899 | most_deliver_netinfo(&mdev->iface, mdev->link_stat, |
900 | &mdev->hw_addr[0]); | |
901 | } | |
902 | ||
903 | /** | |
904 | * wq_clear_halt - work queue function | |
905 | * @wq_obj: work_struct object to execute | |
906 | * | |
907 | * This sends a clear_halt to the given USB pipe. | |
908 | */ | |
909 | static void wq_clear_halt(struct work_struct *wq_obj) | |
910 | { | |
cc28983c CG |
911 | struct clear_hold_work *clear_work = to_clear_hold_work(wq_obj); |
912 | struct most_dev *mdev = clear_work->mdev; | |
913 | unsigned int channel = clear_work->channel; | |
914 | int pipe = clear_work->pipe; | |
a4198cdf | 915 | |
cc28983c | 916 | mutex_lock(&mdev->io_mutex); |
bf9503f1 | 917 | most_stop_enqueue(&mdev->iface, channel); |
cc28983c CG |
918 | free_anchored_buffers(mdev, channel, MBO_E_INVAL); |
919 | if (usb_clear_halt(mdev->usb_device, pipe)) | |
59ed0480 | 920 | dev_warn(&mdev->usb_device->dev, "Failed to reset endpoint.\n"); |
a4198cdf | 921 | |
879c93fe | 922 | mdev->is_channel_healthy[channel] = true; |
72df4a55 | 923 | most_resume_enqueue(&mdev->iface, channel); |
cc28983c | 924 | mutex_unlock(&mdev->io_mutex); |
a4198cdf CG |
925 | } |
926 | ||
927 | /** | |
928 | * hdm_usb_fops - file operation table for USB driver | |
929 | */ | |
930 | static const struct file_operations hdm_usb_fops = { | |
931 | .owner = THIS_MODULE, | |
932 | }; | |
933 | ||
934 | /** | |
935 | * usb_device_id - ID table for HCD device probing | |
936 | */ | |
937 | static struct usb_device_id usbid[] = { | |
938 | { USB_DEVICE(USB_VENDOR_ID_SMSC, USB_DEV_ID_BRDG), }, | |
654f7ec4 | 939 | { USB_DEVICE(USB_VENDOR_ID_SMSC, USB_DEV_ID_OS81118), }, |
b50762ea | 940 | { USB_DEVICE(USB_VENDOR_ID_SMSC, USB_DEV_ID_OS81119), }, |
5bf9bd8d | 941 | { USB_DEVICE(USB_VENDOR_ID_SMSC, USB_DEV_ID_OS81210), }, |
a4198cdf CG |
942 | { } /* Terminating entry */ |
943 | }; | |
944 | ||
945 | #define MOST_DCI_RO_ATTR(_name) \ | |
946 | struct most_dci_attribute most_dci_attr_##_name = \ | |
947 | __ATTR(_name, S_IRUGO, show_value, NULL) | |
948 | ||
949 | #define MOST_DCI_ATTR(_name) \ | |
950 | struct most_dci_attribute most_dci_attr_##_name = \ | |
951 | __ATTR(_name, S_IRUGO | S_IWUSR, show_value, store_value) | |
952 | ||
c0554645 CG |
953 | #define MOST_DCI_WO_ATTR(_name) \ |
954 | struct most_dci_attribute most_dci_attr_##_name = \ | |
3e2880be | 955 | __ATTR(_name, S_IWUSR, NULL, store_value) |
c0554645 | 956 | |
a4198cdf CG |
957 | /** |
958 | * struct most_dci_attribute - to access the attributes of a dci object | |
959 | * @attr: attributes of a dci object | |
960 | * @show: pointer to the show function | |
961 | * @store: pointer to the store function | |
962 | */ | |
963 | struct most_dci_attribute { | |
964 | struct attribute attr; | |
965 | ssize_t (*show)(struct most_dci_obj *d, | |
966 | struct most_dci_attribute *attr, | |
967 | char *buf); | |
968 | ssize_t (*store)(struct most_dci_obj *d, | |
969 | struct most_dci_attribute *attr, | |
970 | const char *buf, | |
971 | size_t count); | |
972 | }; | |
9cbe5aa6 | 973 | |
a4198cdf CG |
974 | #define to_dci_attr(a) container_of(a, struct most_dci_attribute, attr) |
975 | ||
a4198cdf CG |
976 | /** |
977 | * dci_attr_show - show function for dci object | |
978 | * @kobj: pointer to kobject | |
979 | * @attr: pointer to attribute struct | |
980 | * @buf: buffer | |
981 | */ | |
982 | static ssize_t dci_attr_show(struct kobject *kobj, struct attribute *attr, | |
983 | char *buf) | |
984 | { | |
985 | struct most_dci_attribute *dci_attr = to_dci_attr(attr); | |
986 | struct most_dci_obj *dci_obj = to_dci_obj(kobj); | |
987 | ||
988 | if (!dci_attr->show) | |
989 | return -EIO; | |
990 | ||
991 | return dci_attr->show(dci_obj, dci_attr, buf); | |
992 | } | |
993 | ||
994 | /** | |
995 | * dci_attr_store - store function for dci object | |
996 | * @kobj: pointer to kobject | |
997 | * @attr: pointer to attribute struct | |
998 | * @buf: buffer | |
999 | * @len: length of buffer | |
1000 | */ | |
1001 | static ssize_t dci_attr_store(struct kobject *kobj, | |
1002 | struct attribute *attr, | |
1003 | const char *buf, | |
1004 | size_t len) | |
1005 | { | |
1006 | struct most_dci_attribute *dci_attr = to_dci_attr(attr); | |
1007 | struct most_dci_obj *dci_obj = to_dci_obj(kobj); | |
1008 | ||
1009 | if (!dci_attr->store) | |
1010 | return -EIO; | |
1011 | ||
1012 | return dci_attr->store(dci_obj, dci_attr, buf, len); | |
1013 | } | |
1014 | ||
1015 | static const struct sysfs_ops most_dci_sysfs_ops = { | |
1016 | .show = dci_attr_show, | |
1017 | .store = dci_attr_store, | |
1018 | }; | |
1019 | ||
1020 | /** | |
1021 | * most_dci_release - release function for dci object | |
1022 | * @kobj: pointer to kobject | |
1023 | * | |
1024 | * This frees the memory allocated for the dci object | |
1025 | */ | |
1026 | static void most_dci_release(struct kobject *kobj) | |
1027 | { | |
1028 | struct most_dci_obj *dci_obj = to_dci_obj(kobj); | |
1029 | ||
1030 | kfree(dci_obj); | |
1031 | } | |
1032 | ||
a747b42c CG |
1033 | struct regs { |
1034 | const char *name; | |
1035 | u16 reg; | |
1036 | }; | |
1037 | ||
1038 | static const struct regs ro_regs[] = { | |
1039 | { "ni_state", DRCI_REG_NI_STATE }, | |
1040 | { "packet_bandwidth", DRCI_REG_PACKET_BW }, | |
1041 | { "node_address", DRCI_REG_NODE_ADDR }, | |
1042 | { "node_position", DRCI_REG_NODE_POS }, | |
1043 | }; | |
1044 | ||
1045 | static const struct regs rw_regs[] = { | |
1046 | { "mep_filter", DRCI_REG_MEP_FILTER }, | |
1047 | { "mep_hash0", DRCI_REG_HASH_TBL0 }, | |
1048 | { "mep_hash1", DRCI_REG_HASH_TBL1 }, | |
1049 | { "mep_hash2", DRCI_REG_HASH_TBL2 }, | |
1050 | { "mep_hash3", DRCI_REG_HASH_TBL3 }, | |
1051 | { "mep_eui48_hi", DRCI_REG_HW_ADDR_HI }, | |
1052 | { "mep_eui48_mi", DRCI_REG_HW_ADDR_MI }, | |
1053 | { "mep_eui48_lo", DRCI_REG_HW_ADDR_LO }, | |
1054 | }; | |
1055 | ||
1056 | static int get_stat_reg_addr(const struct regs *regs, int size, | |
1057 | const char *name, u16 *reg_addr) | |
1058 | { | |
1059 | int i; | |
1060 | ||
1061 | for (i = 0; i < size; i++) { | |
1062 | if (!strcmp(name, regs[i].name)) { | |
1063 | *reg_addr = regs[i].reg; | |
1064 | return 0; | |
1065 | } | |
1066 | } | |
1067 | return -EFAULT; | |
1068 | } | |
1069 | ||
1070 | #define get_static_reg_addr(regs, name, reg_addr) \ | |
1071 | get_stat_reg_addr(regs, ARRAY_SIZE(regs), name, reg_addr) | |
1072 | ||
a4198cdf CG |
1073 | static ssize_t show_value(struct most_dci_obj *dci_obj, |
1074 | struct most_dci_attribute *attr, char *buf) | |
1075 | { | |
98a3c4d7 | 1076 | const char *name = attr->attr.name; |
0e9b9d08 | 1077 | u16 val; |
a4198cdf CG |
1078 | u16 reg_addr; |
1079 | int err; | |
1080 | ||
98a3c4d7 | 1081 | if (!strcmp(name, "arb_address")) |
c0554645 | 1082 | return snprintf(buf, PAGE_SIZE, "%04x\n", dci_obj->reg_addr); |
98a3c4d7 CG |
1083 | |
1084 | if (!strcmp(name, "arb_value")) | |
c0554645 | 1085 | reg_addr = dci_obj->reg_addr; |
98a3c4d7 CG |
1086 | else if (get_static_reg_addr(ro_regs, name, ®_addr) && |
1087 | get_static_reg_addr(rw_regs, name, ®_addr)) | |
a27cb25b | 1088 | return -EFAULT; |
a4198cdf | 1089 | |
0e9b9d08 | 1090 | err = drci_rd_reg(dci_obj->usb_device, reg_addr, &val); |
a4198cdf CG |
1091 | if (err < 0) |
1092 | return err; | |
1093 | ||
0e9b9d08 | 1094 | return snprintf(buf, PAGE_SIZE, "%04x\n", val); |
a4198cdf CG |
1095 | } |
1096 | ||
1097 | static ssize_t store_value(struct most_dci_obj *dci_obj, | |
1098 | struct most_dci_attribute *attr, | |
1099 | const char *buf, size_t count) | |
1100 | { | |
f1b9a843 | 1101 | u16 val; |
a4198cdf | 1102 | u16 reg_addr; |
98a3c4d7 | 1103 | const char *name = attr->attr.name; |
ac33fbb8 | 1104 | int err = kstrtou16(buf, 16, &val); |
a4198cdf | 1105 | |
c0554645 CG |
1106 | if (err) |
1107 | return err; | |
1108 | ||
98a3c4d7 | 1109 | if (!strcmp(name, "arb_address")) { |
c0554645 CG |
1110 | dci_obj->reg_addr = val; |
1111 | return count; | |
1112 | } | |
98a3c4d7 CG |
1113 | |
1114 | if (!strcmp(name, "arb_value")) { | |
c0554645 | 1115 | reg_addr = dci_obj->reg_addr; |
98a3c4d7 | 1116 | } else if (!strcmp(name, "sync_ep")) { |
c0554645 CG |
1117 | u16 ep = val; |
1118 | ||
1119 | reg_addr = DRCI_REG_BASE + DRCI_COMMAND + ep * 16; | |
1120 | val = 1; | |
98a3c4d7 | 1121 | } else if (get_static_reg_addr(ro_regs, name, ®_addr)) { |
d5cfb0ff | 1122 | return -EFAULT; |
a747b42c | 1123 | } |
a4198cdf | 1124 | |
f1b9a843 | 1125 | err = drci_wr_reg(dci_obj->usb_device, reg_addr, val); |
a4198cdf CG |
1126 | if (err < 0) |
1127 | return err; | |
1128 | ||
1129 | return count; | |
1130 | } | |
1131 | ||
1132 | static MOST_DCI_RO_ATTR(ni_state); | |
1133 | static MOST_DCI_RO_ATTR(packet_bandwidth); | |
1134 | static MOST_DCI_RO_ATTR(node_address); | |
1135 | static MOST_DCI_RO_ATTR(node_position); | |
c0554645 | 1136 | static MOST_DCI_WO_ATTR(sync_ep); |
a4198cdf CG |
1137 | static MOST_DCI_ATTR(mep_filter); |
1138 | static MOST_DCI_ATTR(mep_hash0); | |
1139 | static MOST_DCI_ATTR(mep_hash1); | |
1140 | static MOST_DCI_ATTR(mep_hash2); | |
1141 | static MOST_DCI_ATTR(mep_hash3); | |
1142 | static MOST_DCI_ATTR(mep_eui48_hi); | |
1143 | static MOST_DCI_ATTR(mep_eui48_mi); | |
1144 | static MOST_DCI_ATTR(mep_eui48_lo); | |
c0554645 CG |
1145 | static MOST_DCI_ATTR(arb_address); |
1146 | static MOST_DCI_ATTR(arb_value); | |
a4198cdf CG |
1147 | |
1148 | /** | |
1149 | * most_dci_def_attrs - array of default attribute files of the dci object | |
1150 | */ | |
1151 | static struct attribute *most_dci_def_attrs[] = { | |
1152 | &most_dci_attr_ni_state.attr, | |
1153 | &most_dci_attr_packet_bandwidth.attr, | |
1154 | &most_dci_attr_node_address.attr, | |
1155 | &most_dci_attr_node_position.attr, | |
c0554645 | 1156 | &most_dci_attr_sync_ep.attr, |
a4198cdf CG |
1157 | &most_dci_attr_mep_filter.attr, |
1158 | &most_dci_attr_mep_hash0.attr, | |
1159 | &most_dci_attr_mep_hash1.attr, | |
1160 | &most_dci_attr_mep_hash2.attr, | |
1161 | &most_dci_attr_mep_hash3.attr, | |
1162 | &most_dci_attr_mep_eui48_hi.attr, | |
1163 | &most_dci_attr_mep_eui48_mi.attr, | |
1164 | &most_dci_attr_mep_eui48_lo.attr, | |
c0554645 CG |
1165 | &most_dci_attr_arb_address.attr, |
1166 | &most_dci_attr_arb_value.attr, | |
a4198cdf CG |
1167 | NULL, |
1168 | }; | |
1169 | ||
1170 | /** | |
1171 | * DCI ktype | |
1172 | */ | |
1173 | static struct kobj_type most_dci_ktype = { | |
1174 | .sysfs_ops = &most_dci_sysfs_ops, | |
1175 | .release = most_dci_release, | |
1176 | .default_attrs = most_dci_def_attrs, | |
1177 | }; | |
1178 | ||
1179 | /** | |
1180 | * create_most_dci_obj - allocates a dci object | |
1181 | * @parent: parent kobject | |
1182 | * | |
1183 | * This creates a dci object and registers it with sysfs. | |
1184 | * Returns a pointer to the object or NULL when something went wrong. | |
1185 | */ | |
1186 | static struct | |
1187 | most_dci_obj *create_most_dci_obj(struct kobject *parent) | |
1188 | { | |
089612f1 | 1189 | struct most_dci_obj *most_dci = kzalloc(sizeof(*most_dci), GFP_KERNEL); |
a4198cdf CG |
1190 | int retval; |
1191 | ||
a4198cdf CG |
1192 | if (!most_dci) |
1193 | return NULL; | |
1194 | ||
1195 | retval = kobject_init_and_add(&most_dci->kobj, &most_dci_ktype, parent, | |
1196 | "dci"); | |
1197 | if (retval) { | |
1198 | kobject_put(&most_dci->kobj); | |
1199 | return NULL; | |
1200 | } | |
1201 | return most_dci; | |
1202 | } | |
1203 | ||
1204 | /** | |
1205 | * destroy_most_dci_obj - DCI object release function | |
1206 | * @p: pointer to dci object | |
1207 | */ | |
1208 | static void destroy_most_dci_obj(struct most_dci_obj *p) | |
1209 | { | |
1210 | kobject_put(&p->kobj); | |
1211 | } | |
1212 | ||
1213 | /** | |
1214 | * hdm_probe - probe function of USB device driver | |
1215 | * @interface: Interface of the attached USB device | |
1216 | * @id: Pointer to the USB ID table. | |
1217 | * | |
1218 | * This allocates and initializes the device instance, adds the new | |
1219 | * entry to the internal list, scans the USB descriptors and registers | |
1220 | * the interface with the core. | |
1221 | * Additionally, the DCI objects are created and the hardware is sync'd. | |
1222 | * | |
1223 | * Return 0 on success. In case of an error a negative number is returned. | |
1224 | */ | |
1225 | static int | |
1226 | hdm_probe(struct usb_interface *interface, const struct usb_device_id *id) | |
1227 | { | |
089612f1 CG |
1228 | struct usb_host_interface *usb_iface_desc = interface->cur_altsetting; |
1229 | struct usb_device *usb_dev = interface_to_usbdev(interface); | |
1230 | struct device *dev = &usb_dev->dev; | |
1231 | struct most_dev *mdev = kzalloc(sizeof(*mdev), GFP_KERNEL); | |
a4198cdf CG |
1232 | unsigned int i; |
1233 | unsigned int num_endpoints; | |
1234 | struct most_channel_capability *tmp_cap; | |
a4198cdf CG |
1235 | struct usb_endpoint_descriptor *ep_desc; |
1236 | int ret = 0; | |
d747e8ec | 1237 | int err; |
a4198cdf | 1238 | |
a4198cdf CG |
1239 | if (!mdev) |
1240 | goto exit_ENOMEM; | |
1241 | ||
1242 | usb_set_intfdata(interface, mdev); | |
1243 | num_endpoints = usb_iface_desc->desc.bNumEndpoints; | |
1244 | mutex_init(&mdev->io_mutex); | |
1245 | INIT_WORK(&mdev->poll_work_obj, wq_netinfo); | |
bff1d83d MFW |
1246 | setup_timer(&mdev->link_stat_timer, link_stat_timer_handler, |
1247 | (unsigned long)mdev); | |
a4198cdf CG |
1248 | |
1249 | mdev->usb_device = usb_dev; | |
a4198cdf CG |
1250 | mdev->link_stat_timer.expires = jiffies + (2 * HZ); |
1251 | ||
1252 | mdev->iface.mod = hdm_usb_fops.owner; | |
1253 | mdev->iface.interface = ITYPE_USB; | |
1254 | mdev->iface.configure = hdm_configure_channel; | |
1255 | mdev->iface.request_netinfo = hdm_request_netinfo; | |
1256 | mdev->iface.enqueue = hdm_enqueue; | |
1257 | mdev->iface.poison_channel = hdm_poison_channel; | |
1258 | mdev->iface.description = mdev->description; | |
1259 | mdev->iface.num_channels = num_endpoints; | |
1260 | ||
1261 | snprintf(mdev->description, sizeof(mdev->description), | |
1262 | "usb_device %d-%s:%d.%d", | |
1263 | usb_dev->bus->busnum, | |
1264 | usb_dev->devpath, | |
1265 | usb_dev->config->desc.bConfigurationValue, | |
1266 | usb_iface_desc->desc.bInterfaceNumber); | |
1267 | ||
1268 | mdev->conf = kcalloc(num_endpoints, sizeof(*mdev->conf), GFP_KERNEL); | |
1269 | if (!mdev->conf) | |
1270 | goto exit_free; | |
1271 | ||
1272 | mdev->cap = kcalloc(num_endpoints, sizeof(*mdev->cap), GFP_KERNEL); | |
1273 | if (!mdev->cap) | |
1274 | goto exit_free1; | |
1275 | ||
1276 | mdev->iface.channel_vector = mdev->cap; | |
1277 | mdev->iface.priv = NULL; | |
1278 | ||
1279 | mdev->ep_address = | |
1280 | kcalloc(num_endpoints, sizeof(*mdev->ep_address), GFP_KERNEL); | |
1281 | if (!mdev->ep_address) | |
1282 | goto exit_free2; | |
1283 | ||
1284 | mdev->anchor_list = | |
1285 | kcalloc(num_endpoints, sizeof(*mdev->anchor_list), GFP_KERNEL); | |
1286 | if (!mdev->anchor_list) | |
1287 | goto exit_free3; | |
1288 | ||
1289 | tmp_cap = mdev->cap; | |
1290 | for (i = 0; i < num_endpoints; i++) { | |
1291 | ep_desc = &usb_iface_desc->endpoint[i].desc; | |
1292 | mdev->ep_address[i] = ep_desc->bEndpointAddress; | |
1293 | mdev->padding_active[i] = false; | |
1294 | mdev->is_channel_healthy[i] = true; | |
1295 | ||
1296 | snprintf(&mdev->suffix[i][0], MAX_SUFFIX_LEN, "ep%02x", | |
1297 | mdev->ep_address[i]); | |
1298 | ||
1299 | tmp_cap->name_suffix = &mdev->suffix[i][0]; | |
1300 | tmp_cap->buffer_size_packet = MAX_BUF_SIZE; | |
1301 | tmp_cap->buffer_size_streaming = MAX_BUF_SIZE; | |
1302 | tmp_cap->num_buffers_packet = BUF_CHAIN_SIZE; | |
1303 | tmp_cap->num_buffers_streaming = BUF_CHAIN_SIZE; | |
1304 | tmp_cap->data_type = MOST_CH_CONTROL | MOST_CH_ASYNC | | |
1305 | MOST_CH_ISOC_AVP | MOST_CH_SYNC; | |
afd14cef | 1306 | if (usb_endpoint_dir_in(ep_desc)) |
a4198cdf CG |
1307 | tmp_cap->direction = MOST_CH_RX; |
1308 | else | |
1309 | tmp_cap->direction = MOST_CH_TX; | |
1310 | tmp_cap++; | |
1311 | INIT_LIST_HEAD(&mdev->anchor_list[i]); | |
1312 | spin_lock_init(&mdev->anchor_list_lock[i]); | |
d747e8ec CG |
1313 | err = drci_wr_reg(usb_dev, |
1314 | DRCI_REG_BASE + DRCI_COMMAND + | |
1315 | ep_desc->bEndpointAddress * 16, | |
f1b9a843 | 1316 | 1); |
d747e8ec CG |
1317 | if (err < 0) |
1318 | pr_warn("DCI Sync for EP %02x failed", | |
1319 | ep_desc->bEndpointAddress); | |
a4198cdf | 1320 | } |
59ed0480 CG |
1321 | dev_notice(dev, "claimed gadget: Vendor=%4.4x ProdID=%4.4x Bus=%02x Device=%02x\n", |
1322 | le16_to_cpu(usb_dev->descriptor.idVendor), | |
1323 | le16_to_cpu(usb_dev->descriptor.idProduct), | |
1324 | usb_dev->bus->busnum, | |
1325 | usb_dev->devnum); | |
1326 | ||
1327 | dev_notice(dev, "device path: /sys/bus/usb/devices/%d-%s:%d.%d\n", | |
1328 | usb_dev->bus->busnum, | |
1329 | usb_dev->devpath, | |
1330 | usb_dev->config->desc.bConfigurationValue, | |
1331 | usb_iface_desc->desc.bInterfaceNumber); | |
a4198cdf CG |
1332 | |
1333 | mdev->parent = most_register_interface(&mdev->iface); | |
1334 | if (IS_ERR(mdev->parent)) { | |
1335 | ret = PTR_ERR(mdev->parent); | |
1336 | goto exit_free4; | |
1337 | } | |
1338 | ||
1339 | mutex_lock(&mdev->io_mutex); | |
654f7ec4 | 1340 | if (le16_to_cpu(usb_dev->descriptor.idProduct) == USB_DEV_ID_OS81118 || |
5bf9bd8d CG |
1341 | le16_to_cpu(usb_dev->descriptor.idProduct) == USB_DEV_ID_OS81119 || |
1342 | le16_to_cpu(usb_dev->descriptor.idProduct) == USB_DEV_ID_OS81210) { | |
a4198cdf CG |
1343 | /* this increments the reference count of the instance |
1344 | * object of the core | |
1345 | */ | |
1346 | mdev->dci = create_most_dci_obj(mdev->parent); | |
1347 | if (!mdev->dci) { | |
1348 | mutex_unlock(&mdev->io_mutex); | |
1349 | most_deregister_interface(&mdev->iface); | |
1350 | ret = -ENOMEM; | |
1351 | goto exit_free4; | |
1352 | } | |
1353 | ||
1354 | kobject_uevent(&mdev->dci->kobj, KOBJ_ADD); | |
1355 | mdev->dci->usb_device = mdev->usb_device; | |
a4198cdf CG |
1356 | } |
1357 | mutex_unlock(&mdev->io_mutex); | |
1358 | return 0; | |
1359 | ||
1360 | exit_free4: | |
1361 | kfree(mdev->anchor_list); | |
1362 | exit_free3: | |
1363 | kfree(mdev->ep_address); | |
1364 | exit_free2: | |
1365 | kfree(mdev->cap); | |
1366 | exit_free1: | |
1367 | kfree(mdev->conf); | |
1368 | exit_free: | |
1369 | kfree(mdev); | |
1370 | exit_ENOMEM: | |
1371 | if (ret == 0 || ret == -ENOMEM) { | |
1372 | ret = -ENOMEM; | |
59ed0480 | 1373 | dev_err(dev, "out of memory\n"); |
a4198cdf CG |
1374 | } |
1375 | return ret; | |
1376 | } | |
1377 | ||
1378 | /** | |
1379 | * hdm_disconnect - disconnect function of USB device driver | |
1380 | * @interface: Interface of the attached USB device | |
1381 | * | |
1382 | * This deregisters the interface with the core, removes the kernel timer | |
1383 | * and frees resources. | |
1384 | * | |
1385 | * Context: hub kernel thread | |
1386 | */ | |
1387 | static void hdm_disconnect(struct usb_interface *interface) | |
1388 | { | |
089612f1 | 1389 | struct most_dev *mdev = usb_get_intfdata(interface); |
a4198cdf | 1390 | |
a4198cdf CG |
1391 | mutex_lock(&mdev->io_mutex); |
1392 | usb_set_intfdata(interface, NULL); | |
1393 | mdev->usb_device = NULL; | |
1394 | mutex_unlock(&mdev->io_mutex); | |
1395 | ||
1396 | del_timer_sync(&mdev->link_stat_timer); | |
1397 | cancel_work_sync(&mdev->poll_work_obj); | |
1398 | ||
1399 | destroy_most_dci_obj(mdev->dci); | |
1400 | most_deregister_interface(&mdev->iface); | |
1401 | ||
1402 | kfree(mdev->anchor_list); | |
1403 | kfree(mdev->cap); | |
1404 | kfree(mdev->conf); | |
1405 | kfree(mdev->ep_address); | |
1406 | kfree(mdev); | |
1407 | } | |
1408 | ||
1409 | static struct usb_driver hdm_usb = { | |
1410 | .name = "hdm_usb", | |
1411 | .id_table = usbid, | |
1412 | .probe = hdm_probe, | |
1413 | .disconnect = hdm_disconnect, | |
1414 | }; | |
1415 | ||
1416 | static int __init hdm_usb_init(void) | |
1417 | { | |
1418 | pr_info("hdm_usb_init()\n"); | |
1419 | if (usb_register(&hdm_usb)) { | |
59ed0480 | 1420 | pr_err("could not register hdm_usb driver\n"); |
a4198cdf CG |
1421 | return -EIO; |
1422 | } | |
e3479f77 | 1423 | |
a4198cdf CG |
1424 | return 0; |
1425 | } | |
1426 | ||
1427 | static void __exit hdm_usb_exit(void) | |
1428 | { | |
1429 | pr_info("hdm_usb_exit()\n"); | |
a4198cdf CG |
1430 | usb_deregister(&hdm_usb); |
1431 | } | |
1432 | ||
1433 | module_init(hdm_usb_init); | |
1434 | module_exit(hdm_usb_exit); | |
1435 | MODULE_LICENSE("GPL"); | |
1436 | MODULE_AUTHOR("Christian Gromm <christian.gromm@microchip.com>"); | |
1437 | MODULE_DESCRIPTION("HDM_4_USB"); |