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