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