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