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