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