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