1
2
3
4
5
6
7
8
9
10
11
12
13
14#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15#include <linux/module.h>
16#include <linux/fs.h>
17#include <linux/usb.h>
18#include <linux/slab.h>
19#include <linux/init.h>
20#include <linux/cdev.h>
21#include <linux/device.h>
22#include <linux/list.h>
23#include <linux/completion.h>
24#include <linux/mutex.h>
25#include <linux/spinlock.h>
26#include <linux/interrupt.h>
27#include <linux/workqueue.h>
28#include <linux/sysfs.h>
29#include <linux/dma-mapping.h>
30#include <linux/etherdevice.h>
31#include <linux/uaccess.h>
32#include "mostcore.h"
33#include "networking.h"
34
35#define USB_MTU 512
36#define NO_ISOCHRONOUS_URB 0
37#define AV_PACKETS_PER_XACT 2
38#define BUF_CHAIN_SIZE 0xFFFF
39#define MAX_NUM_ENDPOINTS 30
40#define MAX_SUFFIX_LEN 10
41#define MAX_STRING_LEN 80
42#define MAX_BUF_SIZE 0xFFFF
43
44#define USB_VENDOR_ID_SMSC 0x0424
45#define USB_DEV_ID_BRDG 0xC001
46#define USB_DEV_ID_OS81118 0xCF18
47#define USB_DEV_ID_OS81119 0xCF19
48#define USB_DEV_ID_OS81210 0xCF30
49
50#define DRCI_REG_NI_STATE 0x0100
51#define DRCI_REG_PACKET_BW 0x0101
52#define DRCI_REG_NODE_ADDR 0x0102
53#define DRCI_REG_NODE_POS 0x0103
54#define DRCI_REG_MEP_FILTER 0x0140
55#define DRCI_REG_HASH_TBL0 0x0141
56#define DRCI_REG_HASH_TBL1 0x0142
57#define DRCI_REG_HASH_TBL2 0x0143
58#define DRCI_REG_HASH_TBL3 0x0144
59#define DRCI_REG_HW_ADDR_HI 0x0145
60#define DRCI_REG_HW_ADDR_MI 0x0146
61#define DRCI_REG_HW_ADDR_LO 0x0147
62#define DRCI_REG_BASE 0x1100
63#define DRCI_COMMAND 0x02
64#define DRCI_READ_REQ 0xA0
65#define DRCI_WRITE_REQ 0xA1
66
67
68
69
70
71
72
73struct most_dci_obj {
74 struct kobject kobj;
75 struct usb_device *usb_device;
76 u16 reg_addr;
77};
78
79#define to_dci_obj(p) container_of(p, struct most_dci_obj, kobj)
80
81struct most_dev;
82
83struct clear_hold_work {
84 struct work_struct ws;
85 struct most_dev *mdev;
86 unsigned int channel;
87 int pipe;
88};
89
90#define to_clear_hold_work(w) container_of(w, struct clear_hold_work, ws)
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111struct most_dev {
112 struct kobject *parent;
113 struct usb_device *usb_device;
114 struct most_interface iface;
115 struct most_channel_capability *cap;
116 struct most_channel_config *conf;
117 struct most_dci_obj *dci;
118 u8 *ep_address;
119 char description[MAX_STRING_LEN];
120 char suffix[MAX_NUM_ENDPOINTS][MAX_SUFFIX_LEN];
121 spinlock_t channel_lock[MAX_NUM_ENDPOINTS];
122 bool padding_active[MAX_NUM_ENDPOINTS];
123 bool is_channel_healthy[MAX_NUM_ENDPOINTS];
124 struct clear_hold_work clear_work[MAX_NUM_ENDPOINTS];
125 struct usb_anchor *busy_urbs;
126 struct mutex io_mutex;
127 struct timer_list link_stat_timer;
128 struct work_struct poll_work_obj;
129};
130
131#define to_mdev(d) container_of(d, struct most_dev, iface)
132#define to_mdev_from_work(w) container_of(w, struct most_dev, poll_work_obj)
133
134static void wq_clear_halt(struct work_struct *wq_obj);
135static void wq_netinfo(struct work_struct *wq_obj);
136
137
138
139
140
141
142
143
144
145static inline int drci_rd_reg(struct usb_device *dev, u16 reg, u16 *buf)
146{
147 int retval;
148 __le16 *dma_buf = kzalloc(sizeof(*dma_buf), GFP_KERNEL);
149 u8 req_type = USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
150
151 if (!dma_buf)
152 return -ENOMEM;
153
154 retval = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
155 DRCI_READ_REQ, req_type,
156 0x0000,
157 reg, dma_buf, sizeof(*dma_buf), 5 * HZ);
158 *buf = le16_to_cpu(*dma_buf);
159 kfree(dma_buf);
160
161 return retval;
162}
163
164
165
166
167
168
169
170
171
172static inline int drci_wr_reg(struct usb_device *dev, u16 reg, u16 data)
173{
174 return usb_control_msg(dev,
175 usb_sndctrlpipe(dev, 0),
176 DRCI_WRITE_REQ,
177 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
178 data,
179 reg,
180 NULL,
181 0,
182 5 * HZ);
183}
184
185static inline int start_sync_ep(struct usb_device *usb_dev, u16 ep)
186{
187 return drci_wr_reg(usb_dev, DRCI_REG_BASE + DRCI_COMMAND + ep * 16, 1);
188}
189
190
191
192
193
194static unsigned int get_stream_frame_size(struct most_channel_config *cfg)
195{
196 unsigned int frame_size = 0;
197 unsigned int sub_size = cfg->subbuffer_size;
198
199 if (!sub_size) {
200 pr_warn("Misconfig: Subbuffer size zero.\n");
201 return frame_size;
202 }
203 switch (cfg->data_type) {
204 case MOST_CH_ISOC:
205 frame_size = AV_PACKETS_PER_XACT * sub_size;
206 break;
207 case MOST_CH_SYNC:
208 if (cfg->packets_per_xact == 0) {
209 pr_warn("Misconfig: Packets per XACT zero\n");
210 frame_size = 0;
211 } else if (cfg->packets_per_xact == 0xFF) {
212 frame_size = (USB_MTU / sub_size) * sub_size;
213 } else {
214 frame_size = cfg->packets_per_xact * sub_size;
215 }
216 break;
217 default:
218 pr_warn("Query frame size of non-streaming channel\n");
219 break;
220 }
221 return frame_size;
222}
223
224
225
226
227
228
229
230
231
232
233
234
235static int hdm_poison_channel(struct most_interface *iface, int channel)
236{
237 struct most_dev *mdev = to_mdev(iface);
238 unsigned long flags;
239 spinlock_t *lock;
240
241 if (unlikely(!iface)) {
242 dev_warn(&mdev->usb_device->dev, "Poison: Bad interface.\n");
243 return -EIO;
244 }
245 if (unlikely(channel < 0 || channel >= iface->num_channels)) {
246 dev_warn(&mdev->usb_device->dev, "Channel ID out of range.\n");
247 return -ECHRNG;
248 }
249
250 lock = mdev->channel_lock + channel;
251 spin_lock_irqsave(lock, flags);
252 mdev->is_channel_healthy[channel] = false;
253 spin_unlock_irqrestore(lock, flags);
254
255 cancel_work_sync(&mdev->clear_work[channel].ws);
256
257 mutex_lock(&mdev->io_mutex);
258 usb_kill_anchored_urbs(&mdev->busy_urbs[channel]);
259 if (mdev->padding_active[channel])
260 mdev->padding_active[channel] = false;
261
262 if (mdev->conf[channel].data_type == MOST_CH_ASYNC) {
263 del_timer_sync(&mdev->link_stat_timer);
264 cancel_work_sync(&mdev->poll_work_obj);
265 }
266 mutex_unlock(&mdev->io_mutex);
267 return 0;
268}
269
270
271
272
273
274
275
276
277
278
279static int hdm_add_padding(struct most_dev *mdev, int channel, struct mbo *mbo)
280{
281 struct most_channel_config *conf = &mdev->conf[channel];
282 unsigned int frame_size = get_stream_frame_size(conf);
283 unsigned int j, num_frames;
284 u16 rd_addr, wr_addr;
285
286 if (!frame_size)
287 return -EIO;
288 num_frames = mbo->buffer_length / frame_size;
289
290 if (num_frames < 1) {
291 dev_err(&mdev->usb_device->dev,
292 "Missed minimal transfer unit.\n");
293 return -EIO;
294 }
295
296 for (j = 1; j < num_frames; j++) {
297 wr_addr = (num_frames - j) * USB_MTU;
298 rd_addr = (num_frames - j) * frame_size;
299 memmove(mbo->virt_address + wr_addr,
300 mbo->virt_address + rd_addr,
301 frame_size);
302 }
303 mbo->buffer_length = num_frames * USB_MTU;
304 return 0;
305}
306
307
308
309
310
311
312
313
314
315
316static int hdm_remove_padding(struct most_dev *mdev, int channel,
317 struct mbo *mbo)
318{
319 struct most_channel_config *const conf = &mdev->conf[channel];
320 unsigned int frame_size = get_stream_frame_size(conf);
321 unsigned int j, num_frames;
322
323 if (!frame_size)
324 return -EIO;
325 num_frames = mbo->processed_length / USB_MTU;
326
327 for (j = 1; j < num_frames; j++)
328 memmove(mbo->virt_address + frame_size * j,
329 mbo->virt_address + USB_MTU * j,
330 frame_size);
331
332 mbo->processed_length = frame_size * num_frames;
333 return 0;
334}
335
336
337
338
339
340
341
342
343
344
345
346
347static void hdm_write_completion(struct urb *urb)
348{
349 struct mbo *mbo = urb->context;
350 struct most_dev *mdev = to_mdev(mbo->ifp);
351 unsigned int channel = mbo->hdm_channel_id;
352 struct device *dev = &mdev->usb_device->dev;
353 spinlock_t *lock = mdev->channel_lock + channel;
354 unsigned long flags;
355
356 spin_lock_irqsave(lock, flags);
357
358 mbo->processed_length = 0;
359 mbo->status = MBO_E_INVAL;
360 if (likely(mdev->is_channel_healthy[channel])) {
361 switch (urb->status) {
362 case 0:
363 case -ESHUTDOWN:
364 mbo->processed_length = urb->actual_length;
365 mbo->status = MBO_SUCCESS;
366 break;
367 case -EPIPE:
368 dev_warn(dev, "Broken OUT pipe detected\n");
369 mdev->is_channel_healthy[channel] = false;
370 mdev->clear_work[channel].pipe = urb->pipe;
371 schedule_work(&mdev->clear_work[channel].ws);
372 break;
373 case -ENODEV:
374 case -EPROTO:
375 mbo->status = MBO_E_CLOSE;
376 break;
377 }
378 }
379
380 spin_unlock_irqrestore(lock, flags);
381
382 if (likely(mbo->complete))
383 mbo->complete(mbo);
384 usb_free_urb(urb);
385}
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495static void hdm_read_completion(struct urb *urb)
496{
497 struct mbo *mbo = urb->context;
498 struct most_dev *mdev = to_mdev(mbo->ifp);
499 unsigned int channel = mbo->hdm_channel_id;
500 struct device *dev = &mdev->usb_device->dev;
501 spinlock_t *lock = mdev->channel_lock + channel;
502 unsigned long flags;
503
504 spin_lock_irqsave(lock, flags);
505
506 mbo->processed_length = 0;
507 mbo->status = MBO_E_INVAL;
508 if (likely(mdev->is_channel_healthy[channel])) {
509 switch (urb->status) {
510 case 0:
511 case -ESHUTDOWN:
512 mbo->processed_length = urb->actual_length;
513 mbo->status = MBO_SUCCESS;
514 if (mdev->padding_active[channel] &&
515 hdm_remove_padding(mdev, channel, mbo)) {
516 mbo->processed_length = 0;
517 mbo->status = MBO_E_INVAL;
518 }
519 break;
520 case -EPIPE:
521 dev_warn(dev, "Broken IN pipe detected\n");
522 mdev->is_channel_healthy[channel] = false;
523 mdev->clear_work[channel].pipe = urb->pipe;
524 schedule_work(&mdev->clear_work[channel].ws);
525 break;
526 case -ENODEV:
527 case -EPROTO:
528 mbo->status = MBO_E_CLOSE;
529 break;
530 case -EOVERFLOW:
531 dev_warn(dev, "Babble on IN pipe detected\n");
532 break;
533 }
534 }
535
536 spin_unlock_irqrestore(lock, flags);
537
538 if (likely(mbo->complete))
539 mbo->complete(mbo);
540 usb_free_urb(urb);
541}
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558static int hdm_enqueue(struct most_interface *iface, int channel,
559 struct mbo *mbo)
560{
561 struct most_dev *mdev;
562 struct most_channel_config *conf;
563 struct device *dev;
564 int retval = 0;
565 struct urb *urb;
566 unsigned long length;
567 void *virt_address;
568
569 if (unlikely(!iface || !mbo))
570 return -EIO;
571 if (unlikely(iface->num_channels <= channel || channel < 0))
572 return -ECHRNG;
573
574 mdev = to_mdev(iface);
575 conf = &mdev->conf[channel];
576 dev = &mdev->usb_device->dev;
577
578 if (!mdev->usb_device)
579 return -ENODEV;
580
581 urb = usb_alloc_urb(NO_ISOCHRONOUS_URB, GFP_ATOMIC);
582 if (!urb)
583 return -ENOMEM;
584
585 if ((conf->direction & MOST_CH_TX) && mdev->padding_active[channel] &&
586 hdm_add_padding(mdev, channel, mbo)) {
587 retval = -EIO;
588 goto _error;
589 }
590
591 urb->transfer_dma = mbo->bus_address;
592 virt_address = mbo->virt_address;
593 length = mbo->buffer_length;
594
595 if (conf->direction & MOST_CH_TX) {
596 usb_fill_bulk_urb(urb, mdev->usb_device,
597 usb_sndbulkpipe(mdev->usb_device,
598 mdev->ep_address[channel]),
599 virt_address,
600 length,
601 hdm_write_completion,
602 mbo);
603 if (conf->data_type != MOST_CH_ISOC)
604 urb->transfer_flags |= URB_ZERO_PACKET;
605 } else {
606 usb_fill_bulk_urb(urb, mdev->usb_device,
607 usb_rcvbulkpipe(mdev->usb_device,
608 mdev->ep_address[channel]),
609 virt_address,
610 length + conf->extra_len,
611 hdm_read_completion,
612 mbo);
613 }
614 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
615
616 usb_anchor_urb(urb, &mdev->busy_urbs[channel]);
617
618 retval = usb_submit_urb(urb, GFP_KERNEL);
619 if (retval) {
620 dev_err(dev, "URB submit failed with error %d.\n", retval);
621 goto _error_1;
622 }
623 return 0;
624
625_error_1:
626 usb_unanchor_urb(urb);
627_error:
628 usb_free_urb(urb);
629 return retval;
630}
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647static int hdm_configure_channel(struct most_interface *iface, int channel,
648 struct most_channel_config *conf)
649{
650 unsigned int num_frames;
651 unsigned int frame_size;
652 unsigned int temp_size;
653 unsigned int tail_space;
654 struct most_dev *mdev = to_mdev(iface);
655 struct device *dev = &mdev->usb_device->dev;
656
657 mdev->is_channel_healthy[channel] = true;
658 mdev->clear_work[channel].channel = channel;
659 mdev->clear_work[channel].mdev = mdev;
660 INIT_WORK(&mdev->clear_work[channel].ws, wq_clear_halt);
661
662 if (unlikely(!iface || !conf)) {
663 dev_err(dev, "Bad interface or config pointer.\n");
664 return -EINVAL;
665 }
666 if (unlikely(channel < 0 || channel >= iface->num_channels)) {
667 dev_err(dev, "Channel ID out of range.\n");
668 return -EINVAL;
669 }
670 if (!conf->num_buffers || !conf->buffer_size) {
671 dev_err(dev, "Misconfig: buffer size or #buffers zero.\n");
672 return -EINVAL;
673 }
674
675 if (conf->data_type != MOST_CH_SYNC &&
676 !(conf->data_type == MOST_CH_ISOC &&
677 conf->packets_per_xact != 0xFF)) {
678 mdev->padding_active[channel] = false;
679
680
681
682
683
684 goto exit;
685 }
686
687 mdev->padding_active[channel] = true;
688 temp_size = conf->buffer_size;
689
690 frame_size = get_stream_frame_size(conf);
691 if (frame_size == 0 || frame_size > USB_MTU) {
692 dev_warn(dev, "Misconfig: frame size wrong\n");
693 return -EINVAL;
694 }
695
696 if (conf->buffer_size % frame_size) {
697 u16 tmp_val;
698
699 tmp_val = conf->buffer_size / frame_size;
700 conf->buffer_size = tmp_val * frame_size;
701 dev_notice(dev,
702 "Channel %d - rounding buffer size to %d bytes, channel config says %d bytes\n",
703 channel,
704 conf->buffer_size,
705 temp_size);
706 }
707
708 num_frames = conf->buffer_size / frame_size;
709 tail_space = num_frames * (USB_MTU - frame_size);
710 temp_size += tail_space;
711
712
713 conf->extra_len = (DIV_ROUND_UP(temp_size, USB_MTU) * USB_MTU)
714 - conf->buffer_size;
715exit:
716 mdev->conf[channel] = *conf;
717 if (conf->data_type == MOST_CH_ASYNC) {
718 u16 ep = mdev->ep_address[channel];
719
720 if (start_sync_ep(mdev->usb_device, ep) < 0)
721 dev_warn(dev, "sync for ep%02x failed", ep);
722 }
723 return 0;
724}
725
726
727
728
729
730
731
732
733
734
735static void hdm_request_netinfo(struct most_interface *iface, int channel)
736{
737 struct most_dev *mdev;
738
739 BUG_ON(!iface);
740 mdev = to_mdev(iface);
741 mdev->link_stat_timer.expires = jiffies + HZ;
742 mod_timer(&mdev->link_stat_timer, mdev->link_stat_timer.expires);
743}
744
745
746
747
748
749
750
751
752static void link_stat_timer_handler(unsigned long data)
753{
754 struct most_dev *mdev = (struct most_dev *)data;
755
756 schedule_work(&mdev->poll_work_obj);
757 mdev->link_stat_timer.expires = jiffies + (2 * HZ);
758 add_timer(&mdev->link_stat_timer);
759}
760
761
762
763
764
765
766
767static void wq_netinfo(struct work_struct *wq_obj)
768{
769 struct most_dev *mdev = to_mdev_from_work(wq_obj);
770 struct usb_device *usb_device = mdev->usb_device;
771 struct device *dev = &usb_device->dev;
772 u16 hi, mi, lo, link;
773 u8 hw_addr[6];
774
775 if (drci_rd_reg(usb_device, DRCI_REG_HW_ADDR_HI, &hi) < 0) {
776 dev_err(dev, "Vendor request 'hw_addr_hi' failed\n");
777 return;
778 }
779
780 if (drci_rd_reg(usb_device, DRCI_REG_HW_ADDR_MI, &mi) < 0) {
781 dev_err(dev, "Vendor request 'hw_addr_mid' failed\n");
782 return;
783 }
784
785 if (drci_rd_reg(usb_device, DRCI_REG_HW_ADDR_LO, &lo) < 0) {
786 dev_err(dev, "Vendor request 'hw_addr_low' failed\n");
787 return;
788 }
789
790 if (drci_rd_reg(usb_device, DRCI_REG_NI_STATE, &link) < 0) {
791 dev_err(dev, "Vendor request 'link status' failed\n");
792 return;
793 }
794
795 hw_addr[0] = hi >> 8;
796 hw_addr[1] = hi;
797 hw_addr[2] = mi >> 8;
798 hw_addr[3] = mi;
799 hw_addr[4] = lo >> 8;
800 hw_addr[5] = lo;
801
802 most_deliver_netinfo(&mdev->iface, link, hw_addr);
803}
804
805
806
807
808
809
810
811static void wq_clear_halt(struct work_struct *wq_obj)
812{
813 struct clear_hold_work *clear_work = to_clear_hold_work(wq_obj);
814 struct most_dev *mdev = clear_work->mdev;
815 unsigned int channel = clear_work->channel;
816 int pipe = clear_work->pipe;
817
818 mutex_lock(&mdev->io_mutex);
819 most_stop_enqueue(&mdev->iface, channel);
820 usb_kill_anchored_urbs(&mdev->busy_urbs[channel]);
821 if (usb_clear_halt(mdev->usb_device, pipe))
822 dev_warn(&mdev->usb_device->dev, "Failed to reset endpoint.\n");
823
824 mdev->is_channel_healthy[channel] = true;
825 most_resume_enqueue(&mdev->iface, channel);
826 mutex_unlock(&mdev->io_mutex);
827}
828
829
830
831
832static const struct file_operations hdm_usb_fops = {
833 .owner = THIS_MODULE,
834};
835
836
837
838
839static struct usb_device_id usbid[] = {
840 { USB_DEVICE(USB_VENDOR_ID_SMSC, USB_DEV_ID_BRDG), },
841 { USB_DEVICE(USB_VENDOR_ID_SMSC, USB_DEV_ID_OS81118), },
842 { USB_DEVICE(USB_VENDOR_ID_SMSC, USB_DEV_ID_OS81119), },
843 { USB_DEVICE(USB_VENDOR_ID_SMSC, USB_DEV_ID_OS81210), },
844 { }
845};
846
847#define MOST_DCI_RO_ATTR(_name) \
848 struct most_dci_attribute most_dci_attr_##_name = \
849 __ATTR(_name, 0444, show_value, NULL)
850
851#define MOST_DCI_ATTR(_name) \
852 struct most_dci_attribute most_dci_attr_##_name = \
853 __ATTR(_name, 0644, show_value, store_value)
854
855#define MOST_DCI_WO_ATTR(_name) \
856 struct most_dci_attribute most_dci_attr_##_name = \
857 __ATTR(_name, 0200, NULL, store_value)
858
859
860
861
862
863
864
865struct most_dci_attribute {
866 struct attribute attr;
867 ssize_t (*show)(struct most_dci_obj *d,
868 struct most_dci_attribute *attr,
869 char *buf);
870 ssize_t (*store)(struct most_dci_obj *d,
871 struct most_dci_attribute *attr,
872 const char *buf,
873 size_t count);
874};
875
876#define to_dci_attr(a) container_of(a, struct most_dci_attribute, attr)
877
878
879
880
881
882
883
884static ssize_t dci_attr_show(struct kobject *kobj, struct attribute *attr,
885 char *buf)
886{
887 struct most_dci_attribute *dci_attr = to_dci_attr(attr);
888 struct most_dci_obj *dci_obj = to_dci_obj(kobj);
889
890 if (!dci_attr->show)
891 return -EIO;
892
893 return dci_attr->show(dci_obj, dci_attr, buf);
894}
895
896
897
898
899
900
901
902
903static ssize_t dci_attr_store(struct kobject *kobj,
904 struct attribute *attr,
905 const char *buf,
906 size_t len)
907{
908 struct most_dci_attribute *dci_attr = to_dci_attr(attr);
909 struct most_dci_obj *dci_obj = to_dci_obj(kobj);
910
911 if (!dci_attr->store)
912 return -EIO;
913
914 return dci_attr->store(dci_obj, dci_attr, buf, len);
915}
916
917static const struct sysfs_ops most_dci_sysfs_ops = {
918 .show = dci_attr_show,
919 .store = dci_attr_store,
920};
921
922
923
924
925
926
927
928static void most_dci_release(struct kobject *kobj)
929{
930 struct most_dci_obj *dci_obj = to_dci_obj(kobj);
931
932 kfree(dci_obj);
933}
934
935struct regs {
936 const char *name;
937 u16 reg;
938};
939
940static const struct regs ro_regs[] = {
941 { "ni_state", DRCI_REG_NI_STATE },
942 { "packet_bandwidth", DRCI_REG_PACKET_BW },
943 { "node_address", DRCI_REG_NODE_ADDR },
944 { "node_position", DRCI_REG_NODE_POS },
945};
946
947static const struct regs rw_regs[] = {
948 { "mep_filter", DRCI_REG_MEP_FILTER },
949 { "mep_hash0", DRCI_REG_HASH_TBL0 },
950 { "mep_hash1", DRCI_REG_HASH_TBL1 },
951 { "mep_hash2", DRCI_REG_HASH_TBL2 },
952 { "mep_hash3", DRCI_REG_HASH_TBL3 },
953 { "mep_eui48_hi", DRCI_REG_HW_ADDR_HI },
954 { "mep_eui48_mi", DRCI_REG_HW_ADDR_MI },
955 { "mep_eui48_lo", DRCI_REG_HW_ADDR_LO },
956};
957
958static int get_stat_reg_addr(const struct regs *regs, int size,
959 const char *name, u16 *reg_addr)
960{
961 int i;
962
963 for (i = 0; i < size; i++) {
964 if (!strcmp(name, regs[i].name)) {
965 *reg_addr = regs[i].reg;
966 return 0;
967 }
968 }
969 return -EFAULT;
970}
971
972#define get_static_reg_addr(regs, name, reg_addr) \
973 get_stat_reg_addr(regs, ARRAY_SIZE(regs), name, reg_addr)
974
975static ssize_t show_value(struct most_dci_obj *dci_obj,
976 struct most_dci_attribute *attr, char *buf)
977{
978 const char *name = attr->attr.name;
979 u16 val;
980 u16 reg_addr;
981 int err;
982
983 if (!strcmp(name, "arb_address"))
984 return snprintf(buf, PAGE_SIZE, "%04x\n", dci_obj->reg_addr);
985
986 if (!strcmp(name, "arb_value"))
987 reg_addr = dci_obj->reg_addr;
988 else if (get_static_reg_addr(ro_regs, name, ®_addr) &&
989 get_static_reg_addr(rw_regs, name, ®_addr))
990 return -EFAULT;
991
992 err = drci_rd_reg(dci_obj->usb_device, reg_addr, &val);
993 if (err < 0)
994 return err;
995
996 return snprintf(buf, PAGE_SIZE, "%04x\n", val);
997}
998
999static ssize_t store_value(struct most_dci_obj *dci_obj,
1000 struct most_dci_attribute *attr,
1001 const char *buf, size_t count)
1002{
1003 u16 val;
1004 u16 reg_addr;
1005 const char *name = attr->attr.name;
1006 struct usb_device *usb_dev = dci_obj->usb_device;
1007 int err = kstrtou16(buf, 16, &val);
1008
1009 if (err)
1010 return err;
1011
1012 if (!strcmp(name, "arb_address")) {
1013 dci_obj->reg_addr = val;
1014 return count;
1015 }
1016
1017 if (!strcmp(name, "arb_value"))
1018 err = drci_wr_reg(usb_dev, dci_obj->reg_addr, val);
1019 else if (!strcmp(name, "sync_ep"))
1020 err = start_sync_ep(usb_dev, val);
1021 else if (!get_static_reg_addr(ro_regs, name, ®_addr))
1022 err = drci_wr_reg(usb_dev, reg_addr, val);
1023 else
1024 return -EFAULT;
1025
1026 if (err < 0)
1027 return err;
1028
1029 return count;
1030}
1031
1032static MOST_DCI_RO_ATTR(ni_state);
1033static MOST_DCI_RO_ATTR(packet_bandwidth);
1034static MOST_DCI_RO_ATTR(node_address);
1035static MOST_DCI_RO_ATTR(node_position);
1036static MOST_DCI_WO_ATTR(sync_ep);
1037static MOST_DCI_ATTR(mep_filter);
1038static MOST_DCI_ATTR(mep_hash0);
1039static MOST_DCI_ATTR(mep_hash1);
1040static MOST_DCI_ATTR(mep_hash2);
1041static MOST_DCI_ATTR(mep_hash3);
1042static MOST_DCI_ATTR(mep_eui48_hi);
1043static MOST_DCI_ATTR(mep_eui48_mi);
1044static MOST_DCI_ATTR(mep_eui48_lo);
1045static MOST_DCI_ATTR(arb_address);
1046static MOST_DCI_ATTR(arb_value);
1047
1048
1049
1050
1051static struct attribute *most_dci_def_attrs[] = {
1052 &most_dci_attr_ni_state.attr,
1053 &most_dci_attr_packet_bandwidth.attr,
1054 &most_dci_attr_node_address.attr,
1055 &most_dci_attr_node_position.attr,
1056 &most_dci_attr_sync_ep.attr,
1057 &most_dci_attr_mep_filter.attr,
1058 &most_dci_attr_mep_hash0.attr,
1059 &most_dci_attr_mep_hash1.attr,
1060 &most_dci_attr_mep_hash2.attr,
1061 &most_dci_attr_mep_hash3.attr,
1062 &most_dci_attr_mep_eui48_hi.attr,
1063 &most_dci_attr_mep_eui48_mi.attr,
1064 &most_dci_attr_mep_eui48_lo.attr,
1065 &most_dci_attr_arb_address.attr,
1066 &most_dci_attr_arb_value.attr,
1067 NULL,
1068};
1069
1070
1071
1072
1073static struct kobj_type most_dci_ktype = {
1074 .sysfs_ops = &most_dci_sysfs_ops,
1075 .release = most_dci_release,
1076 .default_attrs = most_dci_def_attrs,
1077};
1078
1079
1080
1081
1082
1083
1084
1085
1086static struct
1087most_dci_obj *create_most_dci_obj(struct kobject *parent)
1088{
1089 struct most_dci_obj *most_dci = kzalloc(sizeof(*most_dci), GFP_KERNEL);
1090 int retval;
1091
1092 if (!most_dci)
1093 return NULL;
1094
1095 retval = kobject_init_and_add(&most_dci->kobj, &most_dci_ktype, parent,
1096 "dci");
1097 if (retval) {
1098 kobject_put(&most_dci->kobj);
1099 return NULL;
1100 }
1101 return most_dci;
1102}
1103
1104
1105
1106
1107
1108static void destroy_most_dci_obj(struct most_dci_obj *p)
1109{
1110 kobject_put(&p->kobj);
1111}
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125static int
1126hdm_probe(struct usb_interface *interface, const struct usb_device_id *id)
1127{
1128 struct usb_host_interface *usb_iface_desc = interface->cur_altsetting;
1129 struct usb_device *usb_dev = interface_to_usbdev(interface);
1130 struct device *dev = &usb_dev->dev;
1131 struct most_dev *mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
1132 unsigned int i;
1133 unsigned int num_endpoints;
1134 struct most_channel_capability *tmp_cap;
1135 struct usb_endpoint_descriptor *ep_desc;
1136 int ret = 0;
1137
1138 if (!mdev)
1139 goto exit_ENOMEM;
1140
1141 usb_set_intfdata(interface, mdev);
1142 num_endpoints = usb_iface_desc->desc.bNumEndpoints;
1143 mutex_init(&mdev->io_mutex);
1144 INIT_WORK(&mdev->poll_work_obj, wq_netinfo);
1145 setup_timer(&mdev->link_stat_timer, link_stat_timer_handler,
1146 (unsigned long)mdev);
1147
1148 mdev->usb_device = usb_dev;
1149 mdev->link_stat_timer.expires = jiffies + (2 * HZ);
1150
1151 mdev->iface.mod = hdm_usb_fops.owner;
1152 mdev->iface.interface = ITYPE_USB;
1153 mdev->iface.configure = hdm_configure_channel;
1154 mdev->iface.request_netinfo = hdm_request_netinfo;
1155 mdev->iface.enqueue = hdm_enqueue;
1156 mdev->iface.poison_channel = hdm_poison_channel;
1157 mdev->iface.description = mdev->description;
1158 mdev->iface.num_channels = num_endpoints;
1159
1160 snprintf(mdev->description, sizeof(mdev->description),
1161 "usb_device %d-%s:%d.%d",
1162 usb_dev->bus->busnum,
1163 usb_dev->devpath,
1164 usb_dev->config->desc.bConfigurationValue,
1165 usb_iface_desc->desc.bInterfaceNumber);
1166
1167 mdev->conf = kcalloc(num_endpoints, sizeof(*mdev->conf), GFP_KERNEL);
1168 if (!mdev->conf)
1169 goto exit_free;
1170
1171 mdev->cap = kcalloc(num_endpoints, sizeof(*mdev->cap), GFP_KERNEL);
1172 if (!mdev->cap)
1173 goto exit_free1;
1174
1175 mdev->iface.channel_vector = mdev->cap;
1176 mdev->iface.priv = NULL;
1177
1178 mdev->ep_address =
1179 kcalloc(num_endpoints, sizeof(*mdev->ep_address), GFP_KERNEL);
1180 if (!mdev->ep_address)
1181 goto exit_free2;
1182
1183 mdev->busy_urbs =
1184 kcalloc(num_endpoints, sizeof(*mdev->busy_urbs), GFP_KERNEL);
1185 if (!mdev->busy_urbs)
1186 goto exit_free3;
1187
1188 tmp_cap = mdev->cap;
1189 for (i = 0; i < num_endpoints; i++) {
1190 ep_desc = &usb_iface_desc->endpoint[i].desc;
1191 mdev->ep_address[i] = ep_desc->bEndpointAddress;
1192 mdev->padding_active[i] = false;
1193 mdev->is_channel_healthy[i] = true;
1194
1195 snprintf(&mdev->suffix[i][0], MAX_SUFFIX_LEN, "ep%02x",
1196 mdev->ep_address[i]);
1197
1198 tmp_cap->name_suffix = &mdev->suffix[i][0];
1199 tmp_cap->buffer_size_packet = MAX_BUF_SIZE;
1200 tmp_cap->buffer_size_streaming = MAX_BUF_SIZE;
1201 tmp_cap->num_buffers_packet = BUF_CHAIN_SIZE;
1202 tmp_cap->num_buffers_streaming = BUF_CHAIN_SIZE;
1203 tmp_cap->data_type = MOST_CH_CONTROL | MOST_CH_ASYNC |
1204 MOST_CH_ISOC | MOST_CH_SYNC;
1205 if (usb_endpoint_dir_in(ep_desc))
1206 tmp_cap->direction = MOST_CH_RX;
1207 else
1208 tmp_cap->direction = MOST_CH_TX;
1209 tmp_cap++;
1210 init_usb_anchor(&mdev->busy_urbs[i]);
1211 spin_lock_init(&mdev->channel_lock[i]);
1212 }
1213 dev_notice(dev, "claimed gadget: Vendor=%4.4x ProdID=%4.4x Bus=%02x Device=%02x\n",
1214 le16_to_cpu(usb_dev->descriptor.idVendor),
1215 le16_to_cpu(usb_dev->descriptor.idProduct),
1216 usb_dev->bus->busnum,
1217 usb_dev->devnum);
1218
1219 dev_notice(dev, "device path: /sys/bus/usb/devices/%d-%s:%d.%d\n",
1220 usb_dev->bus->busnum,
1221 usb_dev->devpath,
1222 usb_dev->config->desc.bConfigurationValue,
1223 usb_iface_desc->desc.bInterfaceNumber);
1224
1225 mdev->parent = most_register_interface(&mdev->iface);
1226 if (IS_ERR(mdev->parent)) {
1227 ret = PTR_ERR(mdev->parent);
1228 goto exit_free4;
1229 }
1230
1231 mutex_lock(&mdev->io_mutex);
1232 if (le16_to_cpu(usb_dev->descriptor.idProduct) == USB_DEV_ID_OS81118 ||
1233 le16_to_cpu(usb_dev->descriptor.idProduct) == USB_DEV_ID_OS81119 ||
1234 le16_to_cpu(usb_dev->descriptor.idProduct) == USB_DEV_ID_OS81210) {
1235
1236
1237
1238 mdev->dci = create_most_dci_obj(mdev->parent);
1239 if (!mdev->dci) {
1240 mutex_unlock(&mdev->io_mutex);
1241 most_deregister_interface(&mdev->iface);
1242 ret = -ENOMEM;
1243 goto exit_free4;
1244 }
1245
1246 kobject_uevent(&mdev->dci->kobj, KOBJ_ADD);
1247 mdev->dci->usb_device = mdev->usb_device;
1248 }
1249 mutex_unlock(&mdev->io_mutex);
1250 return 0;
1251
1252exit_free4:
1253 kfree(mdev->busy_urbs);
1254exit_free3:
1255 kfree(mdev->ep_address);
1256exit_free2:
1257 kfree(mdev->cap);
1258exit_free1:
1259 kfree(mdev->conf);
1260exit_free:
1261 kfree(mdev);
1262exit_ENOMEM:
1263 if (ret == 0 || ret == -ENOMEM) {
1264 ret = -ENOMEM;
1265 dev_err(dev, "out of memory\n");
1266 }
1267 return ret;
1268}
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279static void hdm_disconnect(struct usb_interface *interface)
1280{
1281 struct most_dev *mdev = usb_get_intfdata(interface);
1282
1283 mutex_lock(&mdev->io_mutex);
1284 usb_set_intfdata(interface, NULL);
1285 mdev->usb_device = NULL;
1286 mutex_unlock(&mdev->io_mutex);
1287
1288 del_timer_sync(&mdev->link_stat_timer);
1289 cancel_work_sync(&mdev->poll_work_obj);
1290
1291 destroy_most_dci_obj(mdev->dci);
1292 most_deregister_interface(&mdev->iface);
1293
1294 kfree(mdev->busy_urbs);
1295 kfree(mdev->cap);
1296 kfree(mdev->conf);
1297 kfree(mdev->ep_address);
1298 kfree(mdev);
1299}
1300
1301static struct usb_driver hdm_usb = {
1302 .name = "hdm_usb",
1303 .id_table = usbid,
1304 .probe = hdm_probe,
1305 .disconnect = hdm_disconnect,
1306};
1307
1308static int __init hdm_usb_init(void)
1309{
1310 pr_info("hdm_usb_init()\n");
1311 if (usb_register(&hdm_usb)) {
1312 pr_err("could not register hdm_usb driver\n");
1313 return -EIO;
1314 }
1315
1316 return 0;
1317}
1318
1319static void __exit hdm_usb_exit(void)
1320{
1321 pr_info("hdm_usb_exit()\n");
1322 usb_deregister(&hdm_usb);
1323}
1324
1325module_init(hdm_usb_init);
1326module_exit(hdm_usb_exit);
1327MODULE_LICENSE("GPL");
1328MODULE_AUTHOR("Christian Gromm <christian.gromm@microchip.com>");
1329MODULE_DESCRIPTION("HDM_4_USB");
1330