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