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