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