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