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