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