0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/module.h>
0009 #include <linux/fs.h>
0010 #include <linux/usb.h>
0011 #include <linux/slab.h>
0012 #include <linux/init.h>
0013 #include <linux/cdev.h>
0014 #include <linux/device.h>
0015 #include <linux/list.h>
0016 #include <linux/completion.h>
0017 #include <linux/mutex.h>
0018 #include <linux/spinlock.h>
0019 #include <linux/interrupt.h>
0020 #include <linux/workqueue.h>
0021 #include <linux/sysfs.h>
0022 #include <linux/dma-mapping.h>
0023 #include <linux/etherdevice.h>
0024 #include <linux/uaccess.h>
0025 #include <linux/most.h>
0026
0027 #define USB_MTU 512
0028 #define NO_ISOCHRONOUS_URB 0
0029 #define AV_PACKETS_PER_XACT 2
0030 #define BUF_CHAIN_SIZE 0xFFFF
0031 #define MAX_NUM_ENDPOINTS 30
0032 #define MAX_SUFFIX_LEN 10
0033 #define MAX_STRING_LEN 80
0034 #define MAX_BUF_SIZE 0xFFFF
0035
0036 #define USB_VENDOR_ID_SMSC 0x0424
0037 #define USB_DEV_ID_BRDG 0xC001
0038 #define USB_DEV_ID_OS81118 0xCF18
0039 #define USB_DEV_ID_OS81119 0xCF19
0040 #define USB_DEV_ID_OS81210 0xCF30
0041
0042 #define DRCI_REG_NI_STATE 0x0100
0043 #define DRCI_REG_PACKET_BW 0x0101
0044 #define DRCI_REG_NODE_ADDR 0x0102
0045 #define DRCI_REG_NODE_POS 0x0103
0046 #define DRCI_REG_MEP_FILTER 0x0140
0047 #define DRCI_REG_HASH_TBL0 0x0141
0048 #define DRCI_REG_HASH_TBL1 0x0142
0049 #define DRCI_REG_HASH_TBL2 0x0143
0050 #define DRCI_REG_HASH_TBL3 0x0144
0051 #define DRCI_REG_HW_ADDR_HI 0x0145
0052 #define DRCI_REG_HW_ADDR_MI 0x0146
0053 #define DRCI_REG_HW_ADDR_LO 0x0147
0054 #define DRCI_REG_BASE 0x1100
0055 #define DRCI_COMMAND 0x02
0056 #define DRCI_READ_REQ 0xA0
0057 #define DRCI_WRITE_REQ 0xA1
0058
0059
0060
0061
0062
0063
0064
0065 struct most_dci_obj {
0066 struct device dev;
0067 struct usb_device *usb_device;
0068 u16 reg_addr;
0069 };
0070
0071 #define to_dci_obj(p) container_of(p, struct most_dci_obj, dev)
0072
0073 struct most_dev;
0074
0075 struct clear_hold_work {
0076 struct work_struct ws;
0077 struct most_dev *mdev;
0078 unsigned int channel;
0079 int pipe;
0080 };
0081
0082 #define to_clear_hold_work(w) container_of(w, struct clear_hold_work, ws)
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102 struct most_dev {
0103 struct device dev;
0104 struct usb_device *usb_device;
0105 struct most_interface iface;
0106 struct most_channel_capability *cap;
0107 struct most_channel_config *conf;
0108 struct most_dci_obj *dci;
0109 u8 *ep_address;
0110 char description[MAX_STRING_LEN];
0111 char suffix[MAX_NUM_ENDPOINTS][MAX_SUFFIX_LEN];
0112 spinlock_t channel_lock[MAX_NUM_ENDPOINTS];
0113 bool padding_active[MAX_NUM_ENDPOINTS];
0114 bool is_channel_healthy[MAX_NUM_ENDPOINTS];
0115 struct clear_hold_work clear_work[MAX_NUM_ENDPOINTS];
0116 struct usb_anchor *busy_urbs;
0117 struct mutex io_mutex;
0118 struct timer_list link_stat_timer;
0119 struct work_struct poll_work_obj;
0120 void (*on_netinfo)(struct most_interface *most_iface,
0121 unsigned char link_state, unsigned char *addrs);
0122 };
0123
0124 #define to_mdev(d) container_of(d, struct most_dev, iface)
0125 #define to_mdev_from_dev(d) container_of(d, struct most_dev, dev)
0126 #define to_mdev_from_work(w) container_of(w, struct most_dev, poll_work_obj)
0127
0128 static void wq_clear_halt(struct work_struct *wq_obj);
0129 static void wq_netinfo(struct work_struct *wq_obj);
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139 static inline int drci_rd_reg(struct usb_device *dev, u16 reg, u16 *buf)
0140 {
0141 int retval;
0142 __le16 *dma_buf;
0143 u8 req_type = USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
0144
0145 dma_buf = kzalloc(sizeof(*dma_buf), GFP_KERNEL);
0146 if (!dma_buf)
0147 return -ENOMEM;
0148
0149 retval = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
0150 DRCI_READ_REQ, req_type,
0151 0x0000,
0152 reg, dma_buf, sizeof(*dma_buf),
0153 USB_CTRL_GET_TIMEOUT);
0154 *buf = le16_to_cpu(*dma_buf);
0155 kfree(dma_buf);
0156
0157 if (retval < 0)
0158 return retval;
0159 return 0;
0160 }
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170 static inline int drci_wr_reg(struct usb_device *dev, u16 reg, u16 data)
0171 {
0172 return usb_control_msg(dev,
0173 usb_sndctrlpipe(dev, 0),
0174 DRCI_WRITE_REQ,
0175 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
0176 data,
0177 reg,
0178 NULL,
0179 0,
0180 USB_CTRL_SET_TIMEOUT);
0181 }
0182
0183 static inline int start_sync_ep(struct usb_device *usb_dev, u16 ep)
0184 {
0185 return drci_wr_reg(usb_dev, DRCI_REG_BASE + DRCI_COMMAND + ep * 16, 1);
0186 }
0187
0188
0189
0190
0191
0192
0193 static unsigned int get_stream_frame_size(struct device *dev,
0194 struct most_channel_config *cfg)
0195 {
0196 unsigned int frame_size;
0197 unsigned int sub_size = cfg->subbuffer_size;
0198
0199 if (!sub_size) {
0200 dev_warn(dev, "Misconfig: Subbuffer size zero.\n");
0201 return 0;
0202 }
0203 switch (cfg->data_type) {
0204 case MOST_CH_ISOC:
0205 frame_size = AV_PACKETS_PER_XACT * sub_size;
0206 break;
0207 case MOST_CH_SYNC:
0208 if (cfg->packets_per_xact == 0) {
0209 dev_warn(dev, "Misconfig: Packets per XACT zero\n");
0210 frame_size = 0;
0211 } else if (cfg->packets_per_xact == 0xFF) {
0212 frame_size = (USB_MTU / sub_size) * sub_size;
0213 } else {
0214 frame_size = cfg->packets_per_xact * sub_size;
0215 }
0216 break;
0217 default:
0218 dev_warn(dev, "Query frame size of non-streaming channel\n");
0219 frame_size = 0;
0220 break;
0221 }
0222 return frame_size;
0223 }
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236 static int hdm_poison_channel(struct most_interface *iface, int channel)
0237 {
0238 struct most_dev *mdev = to_mdev(iface);
0239 unsigned long flags;
0240 spinlock_t *lock;
0241
0242 if (channel < 0 || channel >= iface->num_channels) {
0243 dev_warn(&mdev->usb_device->dev, "Channel ID out of range.\n");
0244 return -ECHRNG;
0245 }
0246
0247 lock = mdev->channel_lock + channel;
0248 spin_lock_irqsave(lock, flags);
0249 mdev->is_channel_healthy[channel] = false;
0250 spin_unlock_irqrestore(lock, flags);
0251
0252 cancel_work_sync(&mdev->clear_work[channel].ws);
0253
0254 mutex_lock(&mdev->io_mutex);
0255 usb_kill_anchored_urbs(&mdev->busy_urbs[channel]);
0256 if (mdev->padding_active[channel])
0257 mdev->padding_active[channel] = false;
0258
0259 if (mdev->conf[channel].data_type == MOST_CH_ASYNC) {
0260 del_timer_sync(&mdev->link_stat_timer);
0261 cancel_work_sync(&mdev->poll_work_obj);
0262 }
0263 mutex_unlock(&mdev->io_mutex);
0264 return 0;
0265 }
0266
0267
0268
0269
0270
0271
0272
0273
0274
0275
0276 static int hdm_add_padding(struct most_dev *mdev, int channel, struct mbo *mbo)
0277 {
0278 struct most_channel_config *conf = &mdev->conf[channel];
0279 unsigned int frame_size = get_stream_frame_size(&mdev->dev, conf);
0280 unsigned int j, num_frames;
0281
0282 if (!frame_size)
0283 return -EINVAL;
0284 num_frames = mbo->buffer_length / frame_size;
0285
0286 if (num_frames < 1) {
0287 dev_err(&mdev->usb_device->dev,
0288 "Missed minimal transfer unit.\n");
0289 return -EINVAL;
0290 }
0291
0292 for (j = num_frames - 1; j > 0; j--)
0293 memmove(mbo->virt_address + j * USB_MTU,
0294 mbo->virt_address + j * frame_size,
0295 frame_size);
0296 mbo->buffer_length = num_frames * USB_MTU;
0297 return 0;
0298 }
0299
0300
0301
0302
0303
0304
0305
0306
0307
0308
0309 static int hdm_remove_padding(struct most_dev *mdev, int channel,
0310 struct mbo *mbo)
0311 {
0312 struct most_channel_config *const conf = &mdev->conf[channel];
0313 unsigned int frame_size = get_stream_frame_size(&mdev->dev, conf);
0314 unsigned int j, num_frames;
0315
0316 if (!frame_size)
0317 return -EINVAL;
0318 num_frames = mbo->processed_length / USB_MTU;
0319
0320 for (j = 1; j < num_frames; j++)
0321 memmove(mbo->virt_address + frame_size * j,
0322 mbo->virt_address + USB_MTU * j,
0323 frame_size);
0324
0325 mbo->processed_length = frame_size * num_frames;
0326 return 0;
0327 }
0328
0329
0330
0331
0332
0333
0334
0335
0336
0337
0338
0339
0340 static void hdm_write_completion(struct urb *urb)
0341 {
0342 struct mbo *mbo = urb->context;
0343 struct most_dev *mdev = to_mdev(mbo->ifp);
0344 unsigned int channel = mbo->hdm_channel_id;
0345 spinlock_t *lock = mdev->channel_lock + channel;
0346 unsigned long flags;
0347
0348 spin_lock_irqsave(lock, flags);
0349
0350 mbo->processed_length = 0;
0351 mbo->status = MBO_E_INVAL;
0352 if (likely(mdev->is_channel_healthy[channel])) {
0353 switch (urb->status) {
0354 case 0:
0355 case -ESHUTDOWN:
0356 mbo->processed_length = urb->actual_length;
0357 mbo->status = MBO_SUCCESS;
0358 break;
0359 case -EPIPE:
0360 dev_warn(&mdev->usb_device->dev,
0361 "Broken pipe on ep%02x\n",
0362 mdev->ep_address[channel]);
0363 mdev->is_channel_healthy[channel] = false;
0364 mdev->clear_work[channel].pipe = urb->pipe;
0365 schedule_work(&mdev->clear_work[channel].ws);
0366 break;
0367 case -ENODEV:
0368 case -EPROTO:
0369 mbo->status = MBO_E_CLOSE;
0370 break;
0371 }
0372 }
0373
0374 spin_unlock_irqrestore(lock, flags);
0375
0376 if (likely(mbo->complete))
0377 mbo->complete(mbo);
0378 usb_free_urb(urb);
0379 }
0380
0381
0382
0383
0384
0385
0386
0387
0388
0389
0390
0391
0392 static void hdm_read_completion(struct urb *urb)
0393 {
0394 struct mbo *mbo = urb->context;
0395 struct most_dev *mdev = to_mdev(mbo->ifp);
0396 unsigned int channel = mbo->hdm_channel_id;
0397 struct device *dev = &mdev->usb_device->dev;
0398 spinlock_t *lock = mdev->channel_lock + channel;
0399 unsigned long flags;
0400
0401 spin_lock_irqsave(lock, flags);
0402
0403 mbo->processed_length = 0;
0404 mbo->status = MBO_E_INVAL;
0405 if (likely(mdev->is_channel_healthy[channel])) {
0406 switch (urb->status) {
0407 case 0:
0408 case -ESHUTDOWN:
0409 mbo->processed_length = urb->actual_length;
0410 mbo->status = MBO_SUCCESS;
0411 if (mdev->padding_active[channel] &&
0412 hdm_remove_padding(mdev, channel, mbo)) {
0413 mbo->processed_length = 0;
0414 mbo->status = MBO_E_INVAL;
0415 }
0416 break;
0417 case -EPIPE:
0418 dev_warn(dev, "Broken pipe on ep%02x\n",
0419 mdev->ep_address[channel]);
0420 mdev->is_channel_healthy[channel] = false;
0421 mdev->clear_work[channel].pipe = urb->pipe;
0422 schedule_work(&mdev->clear_work[channel].ws);
0423 break;
0424 case -ENODEV:
0425 case -EPROTO:
0426 mbo->status = MBO_E_CLOSE;
0427 break;
0428 case -EOVERFLOW:
0429 dev_warn(dev, "Babble on ep%02x\n",
0430 mdev->ep_address[channel]);
0431 break;
0432 }
0433 }
0434
0435 spin_unlock_irqrestore(lock, flags);
0436
0437 if (likely(mbo->complete))
0438 mbo->complete(mbo);
0439 usb_free_urb(urb);
0440 }
0441
0442
0443
0444
0445
0446
0447
0448
0449
0450
0451
0452
0453
0454
0455
0456
0457 static int hdm_enqueue(struct most_interface *iface, int channel,
0458 struct mbo *mbo)
0459 {
0460 struct most_dev *mdev = to_mdev(iface);
0461 struct most_channel_config *conf;
0462 int retval = 0;
0463 struct urb *urb;
0464 unsigned long length;
0465 void *virt_address;
0466
0467 if (!mbo)
0468 return -EINVAL;
0469 if (iface->num_channels <= channel || channel < 0)
0470 return -ECHRNG;
0471
0472 urb = usb_alloc_urb(NO_ISOCHRONOUS_URB, GFP_KERNEL);
0473 if (!urb)
0474 return -ENOMEM;
0475
0476 conf = &mdev->conf[channel];
0477
0478 mutex_lock(&mdev->io_mutex);
0479 if (!mdev->usb_device) {
0480 retval = -ENODEV;
0481 goto err_free_urb;
0482 }
0483
0484 if ((conf->direction & MOST_CH_TX) && mdev->padding_active[channel] &&
0485 hdm_add_padding(mdev, channel, mbo)) {
0486 retval = -EINVAL;
0487 goto err_free_urb;
0488 }
0489
0490 urb->transfer_dma = mbo->bus_address;
0491 virt_address = mbo->virt_address;
0492 length = mbo->buffer_length;
0493
0494 if (conf->direction & MOST_CH_TX) {
0495 usb_fill_bulk_urb(urb, mdev->usb_device,
0496 usb_sndbulkpipe(mdev->usb_device,
0497 mdev->ep_address[channel]),
0498 virt_address,
0499 length,
0500 hdm_write_completion,
0501 mbo);
0502 if (conf->data_type != MOST_CH_ISOC &&
0503 conf->data_type != MOST_CH_SYNC)
0504 urb->transfer_flags |= URB_ZERO_PACKET;
0505 } else {
0506 usb_fill_bulk_urb(urb, mdev->usb_device,
0507 usb_rcvbulkpipe(mdev->usb_device,
0508 mdev->ep_address[channel]),
0509 virt_address,
0510 length + conf->extra_len,
0511 hdm_read_completion,
0512 mbo);
0513 }
0514 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
0515
0516 usb_anchor_urb(urb, &mdev->busy_urbs[channel]);
0517
0518 retval = usb_submit_urb(urb, GFP_KERNEL);
0519 if (retval) {
0520 dev_err(&mdev->usb_device->dev,
0521 "URB submit failed with error %d.\n", retval);
0522 goto err_unanchor_urb;
0523 }
0524 mutex_unlock(&mdev->io_mutex);
0525 return 0;
0526
0527 err_unanchor_urb:
0528 usb_unanchor_urb(urb);
0529 err_free_urb:
0530 usb_free_urb(urb);
0531 mutex_unlock(&mdev->io_mutex);
0532 return retval;
0533 }
0534
0535 static void *hdm_dma_alloc(struct mbo *mbo, u32 size)
0536 {
0537 struct most_dev *mdev = to_mdev(mbo->ifp);
0538
0539 return usb_alloc_coherent(mdev->usb_device, size, GFP_KERNEL,
0540 &mbo->bus_address);
0541 }
0542
0543 static void hdm_dma_free(struct mbo *mbo, u32 size)
0544 {
0545 struct most_dev *mdev = to_mdev(mbo->ifp);
0546
0547 usb_free_coherent(mdev->usb_device, size, mbo->virt_address,
0548 mbo->bus_address);
0549 }
0550
0551
0552
0553
0554
0555
0556
0557
0558
0559
0560
0561
0562
0563
0564
0565
0566 static int hdm_configure_channel(struct most_interface *iface, int channel,
0567 struct most_channel_config *conf)
0568 {
0569 unsigned int num_frames;
0570 unsigned int frame_size;
0571 struct most_dev *mdev = to_mdev(iface);
0572 struct device *dev = &mdev->usb_device->dev;
0573
0574 if (!conf) {
0575 dev_err(dev, "Bad config pointer.\n");
0576 return -EINVAL;
0577 }
0578 if (channel < 0 || channel >= iface->num_channels) {
0579 dev_err(dev, "Channel ID out of range.\n");
0580 return -EINVAL;
0581 }
0582
0583 mdev->is_channel_healthy[channel] = true;
0584 mdev->clear_work[channel].channel = channel;
0585 mdev->clear_work[channel].mdev = mdev;
0586 INIT_WORK(&mdev->clear_work[channel].ws, wq_clear_halt);
0587
0588 if (!conf->num_buffers || !conf->buffer_size) {
0589 dev_err(dev, "Misconfig: buffer size or #buffers zero.\n");
0590 return -EINVAL;
0591 }
0592
0593 if (conf->data_type != MOST_CH_SYNC &&
0594 !(conf->data_type == MOST_CH_ISOC &&
0595 conf->packets_per_xact != 0xFF)) {
0596 mdev->padding_active[channel] = false;
0597
0598
0599
0600
0601
0602 goto exit;
0603 }
0604
0605 mdev->padding_active[channel] = true;
0606
0607 frame_size = get_stream_frame_size(&mdev->dev, conf);
0608 if (frame_size == 0 || frame_size > USB_MTU) {
0609 dev_warn(dev, "Misconfig: frame size wrong\n");
0610 return -EINVAL;
0611 }
0612
0613 num_frames = conf->buffer_size / frame_size;
0614
0615 if (conf->buffer_size % frame_size) {
0616 u16 old_size = conf->buffer_size;
0617
0618 conf->buffer_size = num_frames * frame_size;
0619 dev_warn(dev, "%s: fixed buffer size (%d -> %d)\n",
0620 mdev->suffix[channel], old_size, conf->buffer_size);
0621 }
0622
0623
0624 conf->extra_len = num_frames * (USB_MTU - frame_size);
0625
0626 exit:
0627 mdev->conf[channel] = *conf;
0628 if (conf->data_type == MOST_CH_ASYNC) {
0629 u16 ep = mdev->ep_address[channel];
0630
0631 if (start_sync_ep(mdev->usb_device, ep) < 0)
0632 dev_warn(dev, "sync for ep%02x failed", ep);
0633 }
0634 return 0;
0635 }
0636
0637
0638
0639
0640
0641
0642
0643
0644
0645
0646 static void hdm_request_netinfo(struct most_interface *iface, int channel,
0647 void (*on_netinfo)(struct most_interface *,
0648 unsigned char,
0649 unsigned char *))
0650 {
0651 struct most_dev *mdev = to_mdev(iface);
0652
0653 mdev->on_netinfo = on_netinfo;
0654 if (!on_netinfo)
0655 return;
0656
0657 mdev->link_stat_timer.expires = jiffies + HZ;
0658 mod_timer(&mdev->link_stat_timer, mdev->link_stat_timer.expires);
0659 }
0660
0661
0662
0663
0664
0665
0666
0667
0668 static void link_stat_timer_handler(struct timer_list *t)
0669 {
0670 struct most_dev *mdev = from_timer(mdev, t, link_stat_timer);
0671
0672 schedule_work(&mdev->poll_work_obj);
0673 mdev->link_stat_timer.expires = jiffies + (2 * HZ);
0674 add_timer(&mdev->link_stat_timer);
0675 }
0676
0677
0678
0679
0680
0681
0682
0683 static void wq_netinfo(struct work_struct *wq_obj)
0684 {
0685 struct most_dev *mdev = to_mdev_from_work(wq_obj);
0686 struct usb_device *usb_device = mdev->usb_device;
0687 struct device *dev = &usb_device->dev;
0688 u16 hi, mi, lo, link;
0689 u8 hw_addr[6];
0690
0691 if (drci_rd_reg(usb_device, DRCI_REG_HW_ADDR_HI, &hi)) {
0692 dev_err(dev, "Vendor request 'hw_addr_hi' failed\n");
0693 return;
0694 }
0695
0696 if (drci_rd_reg(usb_device, DRCI_REG_HW_ADDR_MI, &mi)) {
0697 dev_err(dev, "Vendor request 'hw_addr_mid' failed\n");
0698 return;
0699 }
0700
0701 if (drci_rd_reg(usb_device, DRCI_REG_HW_ADDR_LO, &lo)) {
0702 dev_err(dev, "Vendor request 'hw_addr_low' failed\n");
0703 return;
0704 }
0705
0706 if (drci_rd_reg(usb_device, DRCI_REG_NI_STATE, &link)) {
0707 dev_err(dev, "Vendor request 'link status' failed\n");
0708 return;
0709 }
0710
0711 hw_addr[0] = hi >> 8;
0712 hw_addr[1] = hi;
0713 hw_addr[2] = mi >> 8;
0714 hw_addr[3] = mi;
0715 hw_addr[4] = lo >> 8;
0716 hw_addr[5] = lo;
0717
0718 if (mdev->on_netinfo)
0719 mdev->on_netinfo(&mdev->iface, link, hw_addr);
0720 }
0721
0722
0723
0724
0725
0726
0727
0728 static void wq_clear_halt(struct work_struct *wq_obj)
0729 {
0730 struct clear_hold_work *clear_work = to_clear_hold_work(wq_obj);
0731 struct most_dev *mdev = clear_work->mdev;
0732 unsigned int channel = clear_work->channel;
0733 int pipe = clear_work->pipe;
0734 int snd_pipe;
0735 int peer;
0736
0737 mutex_lock(&mdev->io_mutex);
0738 most_stop_enqueue(&mdev->iface, channel);
0739 usb_kill_anchored_urbs(&mdev->busy_urbs[channel]);
0740 if (usb_clear_halt(mdev->usb_device, pipe))
0741 dev_warn(&mdev->usb_device->dev, "Failed to reset endpoint.\n");
0742
0743
0744
0745
0746
0747
0748
0749
0750
0751 if (mdev->conf[channel].data_type == MOST_CH_ASYNC &&
0752 mdev->conf[channel].direction == MOST_CH_RX) {
0753 if (channel == 0)
0754 peer = 1;
0755 else
0756 peer = 0;
0757 snd_pipe = usb_sndbulkpipe(mdev->usb_device,
0758 mdev->ep_address[peer]);
0759 usb_clear_halt(mdev->usb_device, snd_pipe);
0760 }
0761 mdev->is_channel_healthy[channel] = true;
0762 most_resume_enqueue(&mdev->iface, channel);
0763 mutex_unlock(&mdev->io_mutex);
0764 }
0765
0766
0767
0768
0769 static const struct file_operations hdm_usb_fops = {
0770 .owner = THIS_MODULE,
0771 };
0772
0773
0774
0775
0776 static const struct usb_device_id usbid[] = {
0777 { USB_DEVICE(USB_VENDOR_ID_SMSC, USB_DEV_ID_BRDG), },
0778 { USB_DEVICE(USB_VENDOR_ID_SMSC, USB_DEV_ID_OS81118), },
0779 { USB_DEVICE(USB_VENDOR_ID_SMSC, USB_DEV_ID_OS81119), },
0780 { USB_DEVICE(USB_VENDOR_ID_SMSC, USB_DEV_ID_OS81210), },
0781 { }
0782 };
0783
0784 struct regs {
0785 const char *name;
0786 u16 reg;
0787 };
0788
0789 static const struct regs ro_regs[] = {
0790 { "ni_state", DRCI_REG_NI_STATE },
0791 { "packet_bandwidth", DRCI_REG_PACKET_BW },
0792 { "node_address", DRCI_REG_NODE_ADDR },
0793 { "node_position", DRCI_REG_NODE_POS },
0794 };
0795
0796 static const struct regs rw_regs[] = {
0797 { "mep_filter", DRCI_REG_MEP_FILTER },
0798 { "mep_hash0", DRCI_REG_HASH_TBL0 },
0799 { "mep_hash1", DRCI_REG_HASH_TBL1 },
0800 { "mep_hash2", DRCI_REG_HASH_TBL2 },
0801 { "mep_hash3", DRCI_REG_HASH_TBL3 },
0802 { "mep_eui48_hi", DRCI_REG_HW_ADDR_HI },
0803 { "mep_eui48_mi", DRCI_REG_HW_ADDR_MI },
0804 { "mep_eui48_lo", DRCI_REG_HW_ADDR_LO },
0805 };
0806
0807 static int get_stat_reg_addr(const struct regs *regs, int size,
0808 const char *name, u16 *reg_addr)
0809 {
0810 int i;
0811
0812 for (i = 0; i < size; i++) {
0813 if (sysfs_streq(name, regs[i].name)) {
0814 *reg_addr = regs[i].reg;
0815 return 0;
0816 }
0817 }
0818 return -EINVAL;
0819 }
0820
0821 #define get_static_reg_addr(regs, name, reg_addr) \
0822 get_stat_reg_addr(regs, ARRAY_SIZE(regs), name, reg_addr)
0823
0824 static ssize_t value_show(struct device *dev, struct device_attribute *attr,
0825 char *buf)
0826 {
0827 const char *name = attr->attr.name;
0828 struct most_dci_obj *dci_obj = to_dci_obj(dev);
0829 u16 val;
0830 u16 reg_addr;
0831 int err;
0832
0833 if (sysfs_streq(name, "arb_address"))
0834 return sysfs_emit(buf, "%04x\n", dci_obj->reg_addr);
0835
0836 if (sysfs_streq(name, "arb_value"))
0837 reg_addr = dci_obj->reg_addr;
0838 else if (get_static_reg_addr(ro_regs, name, ®_addr) &&
0839 get_static_reg_addr(rw_regs, name, ®_addr))
0840 return -EINVAL;
0841
0842 err = drci_rd_reg(dci_obj->usb_device, reg_addr, &val);
0843 if (err < 0)
0844 return err;
0845
0846 return sysfs_emit(buf, "%04x\n", val);
0847 }
0848
0849 static ssize_t value_store(struct device *dev, struct device_attribute *attr,
0850 const char *buf, size_t count)
0851 {
0852 u16 val;
0853 u16 reg_addr;
0854 const char *name = attr->attr.name;
0855 struct most_dci_obj *dci_obj = to_dci_obj(dev);
0856 struct usb_device *usb_dev = dci_obj->usb_device;
0857 int err;
0858
0859 err = kstrtou16(buf, 16, &val);
0860 if (err)
0861 return err;
0862
0863 if (sysfs_streq(name, "arb_address")) {
0864 dci_obj->reg_addr = val;
0865 return count;
0866 }
0867
0868 if (sysfs_streq(name, "arb_value"))
0869 err = drci_wr_reg(usb_dev, dci_obj->reg_addr, val);
0870 else if (sysfs_streq(name, "sync_ep"))
0871 err = start_sync_ep(usb_dev, val);
0872 else if (!get_static_reg_addr(rw_regs, name, ®_addr))
0873 err = drci_wr_reg(usb_dev, reg_addr, val);
0874 else
0875 return -EINVAL;
0876
0877 if (err < 0)
0878 return err;
0879
0880 return count;
0881 }
0882
0883 static DEVICE_ATTR(ni_state, 0444, value_show, NULL);
0884 static DEVICE_ATTR(packet_bandwidth, 0444, value_show, NULL);
0885 static DEVICE_ATTR(node_address, 0444, value_show, NULL);
0886 static DEVICE_ATTR(node_position, 0444, value_show, NULL);
0887 static DEVICE_ATTR(sync_ep, 0200, NULL, value_store);
0888 static DEVICE_ATTR(mep_filter, 0644, value_show, value_store);
0889 static DEVICE_ATTR(mep_hash0, 0644, value_show, value_store);
0890 static DEVICE_ATTR(mep_hash1, 0644, value_show, value_store);
0891 static DEVICE_ATTR(mep_hash2, 0644, value_show, value_store);
0892 static DEVICE_ATTR(mep_hash3, 0644, value_show, value_store);
0893 static DEVICE_ATTR(mep_eui48_hi, 0644, value_show, value_store);
0894 static DEVICE_ATTR(mep_eui48_mi, 0644, value_show, value_store);
0895 static DEVICE_ATTR(mep_eui48_lo, 0644, value_show, value_store);
0896 static DEVICE_ATTR(arb_address, 0644, value_show, value_store);
0897 static DEVICE_ATTR(arb_value, 0644, value_show, value_store);
0898
0899 static struct attribute *dci_attrs[] = {
0900 &dev_attr_ni_state.attr,
0901 &dev_attr_packet_bandwidth.attr,
0902 &dev_attr_node_address.attr,
0903 &dev_attr_node_position.attr,
0904 &dev_attr_sync_ep.attr,
0905 &dev_attr_mep_filter.attr,
0906 &dev_attr_mep_hash0.attr,
0907 &dev_attr_mep_hash1.attr,
0908 &dev_attr_mep_hash2.attr,
0909 &dev_attr_mep_hash3.attr,
0910 &dev_attr_mep_eui48_hi.attr,
0911 &dev_attr_mep_eui48_mi.attr,
0912 &dev_attr_mep_eui48_lo.attr,
0913 &dev_attr_arb_address.attr,
0914 &dev_attr_arb_value.attr,
0915 NULL,
0916 };
0917
0918 ATTRIBUTE_GROUPS(dci);
0919
0920 static void release_dci(struct device *dev)
0921 {
0922 struct most_dci_obj *dci = to_dci_obj(dev);
0923
0924 put_device(dev->parent);
0925 kfree(dci);
0926 }
0927
0928 static void release_mdev(struct device *dev)
0929 {
0930 struct most_dev *mdev = to_mdev_from_dev(dev);
0931
0932 kfree(mdev);
0933 }
0934
0935
0936
0937
0938
0939
0940
0941
0942
0943
0944
0945
0946 static int
0947 hdm_probe(struct usb_interface *interface, const struct usb_device_id *id)
0948 {
0949 struct usb_host_interface *usb_iface_desc = interface->cur_altsetting;
0950 struct usb_device *usb_dev = interface_to_usbdev(interface);
0951 struct device *dev = &usb_dev->dev;
0952 struct most_dev *mdev;
0953 unsigned int i;
0954 unsigned int num_endpoints;
0955 struct most_channel_capability *tmp_cap;
0956 struct usb_endpoint_descriptor *ep_desc;
0957 int ret = -ENOMEM;
0958
0959 mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
0960 if (!mdev)
0961 return -ENOMEM;
0962
0963 usb_set_intfdata(interface, mdev);
0964 num_endpoints = usb_iface_desc->desc.bNumEndpoints;
0965 if (num_endpoints > MAX_NUM_ENDPOINTS) {
0966 kfree(mdev);
0967 return -EINVAL;
0968 }
0969 mutex_init(&mdev->io_mutex);
0970 INIT_WORK(&mdev->poll_work_obj, wq_netinfo);
0971 timer_setup(&mdev->link_stat_timer, link_stat_timer_handler, 0);
0972
0973 mdev->usb_device = usb_dev;
0974 mdev->link_stat_timer.expires = jiffies + (2 * HZ);
0975
0976 mdev->iface.mod = hdm_usb_fops.owner;
0977 mdev->iface.dev = &mdev->dev;
0978 mdev->iface.driver_dev = &interface->dev;
0979 mdev->iface.interface = ITYPE_USB;
0980 mdev->iface.configure = hdm_configure_channel;
0981 mdev->iface.request_netinfo = hdm_request_netinfo;
0982 mdev->iface.enqueue = hdm_enqueue;
0983 mdev->iface.poison_channel = hdm_poison_channel;
0984 mdev->iface.dma_alloc = hdm_dma_alloc;
0985 mdev->iface.dma_free = hdm_dma_free;
0986 mdev->iface.description = mdev->description;
0987 mdev->iface.num_channels = num_endpoints;
0988
0989 snprintf(mdev->description, sizeof(mdev->description),
0990 "%d-%s:%d.%d",
0991 usb_dev->bus->busnum,
0992 usb_dev->devpath,
0993 usb_dev->config->desc.bConfigurationValue,
0994 usb_iface_desc->desc.bInterfaceNumber);
0995
0996 mdev->dev.init_name = mdev->description;
0997 mdev->dev.parent = &interface->dev;
0998 mdev->dev.release = release_mdev;
0999 mdev->conf = kcalloc(num_endpoints, sizeof(*mdev->conf), GFP_KERNEL);
1000 if (!mdev->conf)
1001 goto err_free_mdev;
1002
1003 mdev->cap = kcalloc(num_endpoints, sizeof(*mdev->cap), GFP_KERNEL);
1004 if (!mdev->cap)
1005 goto err_free_conf;
1006
1007 mdev->iface.channel_vector = mdev->cap;
1008 mdev->ep_address =
1009 kcalloc(num_endpoints, sizeof(*mdev->ep_address), GFP_KERNEL);
1010 if (!mdev->ep_address)
1011 goto err_free_cap;
1012
1013 mdev->busy_urbs =
1014 kcalloc(num_endpoints, sizeof(*mdev->busy_urbs), GFP_KERNEL);
1015 if (!mdev->busy_urbs)
1016 goto err_free_ep_address;
1017
1018 tmp_cap = mdev->cap;
1019 for (i = 0; i < num_endpoints; i++) {
1020 ep_desc = &usb_iface_desc->endpoint[i].desc;
1021 mdev->ep_address[i] = ep_desc->bEndpointAddress;
1022 mdev->padding_active[i] = false;
1023 mdev->is_channel_healthy[i] = true;
1024
1025 snprintf(&mdev->suffix[i][0], MAX_SUFFIX_LEN, "ep%02x",
1026 mdev->ep_address[i]);
1027
1028 tmp_cap->name_suffix = &mdev->suffix[i][0];
1029 tmp_cap->buffer_size_packet = MAX_BUF_SIZE;
1030 tmp_cap->buffer_size_streaming = MAX_BUF_SIZE;
1031 tmp_cap->num_buffers_packet = BUF_CHAIN_SIZE;
1032 tmp_cap->num_buffers_streaming = BUF_CHAIN_SIZE;
1033 tmp_cap->data_type = MOST_CH_CONTROL | MOST_CH_ASYNC |
1034 MOST_CH_ISOC | MOST_CH_SYNC;
1035 if (usb_endpoint_dir_in(ep_desc))
1036 tmp_cap->direction = MOST_CH_RX;
1037 else
1038 tmp_cap->direction = MOST_CH_TX;
1039 tmp_cap++;
1040 init_usb_anchor(&mdev->busy_urbs[i]);
1041 spin_lock_init(&mdev->channel_lock[i]);
1042 }
1043 dev_dbg(dev, "claimed gadget: Vendor=%4.4x ProdID=%4.4x Bus=%02x Device=%02x\n",
1044 le16_to_cpu(usb_dev->descriptor.idVendor),
1045 le16_to_cpu(usb_dev->descriptor.idProduct),
1046 usb_dev->bus->busnum,
1047 usb_dev->devnum);
1048
1049 dev_dbg(dev, "device path: /sys/bus/usb/devices/%d-%s:%d.%d\n",
1050 usb_dev->bus->busnum,
1051 usb_dev->devpath,
1052 usb_dev->config->desc.bConfigurationValue,
1053 usb_iface_desc->desc.bInterfaceNumber);
1054
1055 ret = most_register_interface(&mdev->iface);
1056 if (ret)
1057 goto err_free_busy_urbs;
1058
1059 mutex_lock(&mdev->io_mutex);
1060 if (le16_to_cpu(usb_dev->descriptor.idProduct) == USB_DEV_ID_OS81118 ||
1061 le16_to_cpu(usb_dev->descriptor.idProduct) == USB_DEV_ID_OS81119 ||
1062 le16_to_cpu(usb_dev->descriptor.idProduct) == USB_DEV_ID_OS81210) {
1063 mdev->dci = kzalloc(sizeof(*mdev->dci), GFP_KERNEL);
1064 if (!mdev->dci) {
1065 mutex_unlock(&mdev->io_mutex);
1066 most_deregister_interface(&mdev->iface);
1067 ret = -ENOMEM;
1068 goto err_free_busy_urbs;
1069 }
1070
1071 mdev->dci->dev.init_name = "dci";
1072 mdev->dci->dev.parent = get_device(mdev->iface.dev);
1073 mdev->dci->dev.groups = dci_groups;
1074 mdev->dci->dev.release = release_dci;
1075 if (device_register(&mdev->dci->dev)) {
1076 mutex_unlock(&mdev->io_mutex);
1077 most_deregister_interface(&mdev->iface);
1078 ret = -ENOMEM;
1079 goto err_free_dci;
1080 }
1081 mdev->dci->usb_device = mdev->usb_device;
1082 }
1083 mutex_unlock(&mdev->io_mutex);
1084 return 0;
1085 err_free_dci:
1086 put_device(&mdev->dci->dev);
1087 err_free_busy_urbs:
1088 kfree(mdev->busy_urbs);
1089 err_free_ep_address:
1090 kfree(mdev->ep_address);
1091 err_free_cap:
1092 kfree(mdev->cap);
1093 err_free_conf:
1094 kfree(mdev->conf);
1095 err_free_mdev:
1096 put_device(&mdev->dev);
1097 return ret;
1098 }
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109 static void hdm_disconnect(struct usb_interface *interface)
1110 {
1111 struct most_dev *mdev = usb_get_intfdata(interface);
1112
1113 mutex_lock(&mdev->io_mutex);
1114 usb_set_intfdata(interface, NULL);
1115 mdev->usb_device = NULL;
1116 mutex_unlock(&mdev->io_mutex);
1117
1118 del_timer_sync(&mdev->link_stat_timer);
1119 cancel_work_sync(&mdev->poll_work_obj);
1120
1121 if (mdev->dci)
1122 device_unregister(&mdev->dci->dev);
1123 most_deregister_interface(&mdev->iface);
1124
1125 kfree(mdev->busy_urbs);
1126 kfree(mdev->cap);
1127 kfree(mdev->conf);
1128 kfree(mdev->ep_address);
1129 put_device(&mdev->dci->dev);
1130 put_device(&mdev->dev);
1131 }
1132
1133 static int hdm_suspend(struct usb_interface *interface, pm_message_t message)
1134 {
1135 struct most_dev *mdev = usb_get_intfdata(interface);
1136 int i;
1137
1138 mutex_lock(&mdev->io_mutex);
1139 for (i = 0; i < mdev->iface.num_channels; i++) {
1140 most_stop_enqueue(&mdev->iface, i);
1141 usb_kill_anchored_urbs(&mdev->busy_urbs[i]);
1142 }
1143 mutex_unlock(&mdev->io_mutex);
1144 return 0;
1145 }
1146
1147 static int hdm_resume(struct usb_interface *interface)
1148 {
1149 struct most_dev *mdev = usb_get_intfdata(interface);
1150 int i;
1151
1152 mutex_lock(&mdev->io_mutex);
1153 for (i = 0; i < mdev->iface.num_channels; i++)
1154 most_resume_enqueue(&mdev->iface, i);
1155 mutex_unlock(&mdev->io_mutex);
1156 return 0;
1157 }
1158
1159 static struct usb_driver hdm_usb = {
1160 .name = "hdm_usb",
1161 .id_table = usbid,
1162 .probe = hdm_probe,
1163 .disconnect = hdm_disconnect,
1164 .resume = hdm_resume,
1165 .suspend = hdm_suspend,
1166 };
1167
1168 module_usb_driver(hdm_usb);
1169 MODULE_LICENSE("GPL");
1170 MODULE_AUTHOR("Christian Gromm <christian.gromm@microchip.com>");
1171 MODULE_DESCRIPTION("HDM_4_USB");