Version:  2.0.40 2.2.26 2.4.37 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0 4.1 4.2 4.3 4.4 4.5 4.6

Linux/drivers/staging/gdm724x/gdm_mux.c

  1 /*
  2  * Copyright (c) 2012 GCT Semiconductor, Inc. All rights reserved.
  3  *
  4  * This software is licensed under the terms of the GNU General Public
  5  * License version 2, as published by the Free Software Foundation, and
  6  * may be copied, distributed, and modified under those terms.
  7  *
  8  * This program is distributed in the hope that it will be useful,
  9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 11  * GNU General Public License for more details.
 12  */
 13 
 14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 15 
 16 #include <linux/module.h>
 17 #include <linux/kernel.h>
 18 #include <linux/usb.h>
 19 #include <linux/errno.h>
 20 #include <linux/init.h>
 21 #include <linux/tty.h>
 22 #include <linux/tty_driver.h>
 23 #include <linux/tty_flip.h>
 24 #include <linux/slab.h>
 25 #include <linux/usb/cdc.h>
 26 
 27 #include "gdm_mux.h"
 28 
 29 static u16 packet_type[TTY_MAX_COUNT] = {0xF011, 0xF010};
 30 
 31 #define USB_DEVICE_CDC_DATA(vid, pid) \
 32         .match_flags = \
 33                 USB_DEVICE_ID_MATCH_DEVICE |\
 34                 USB_DEVICE_ID_MATCH_INT_CLASS |\
 35                 USB_DEVICE_ID_MATCH_INT_SUBCLASS,\
 36         .idVendor = vid,\
 37         .idProduct = pid,\
 38         .bInterfaceClass = USB_CLASS_COMM,\
 39         .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM
 40 
 41 static const struct usb_device_id id_table[] = {
 42         { USB_DEVICE_CDC_DATA(0x1076, 0x8000) }, /* GCT GDM7240 */
 43         { USB_DEVICE_CDC_DATA(0x1076, 0x8f00) }, /* GCT GDM7243 */
 44         { USB_DEVICE_CDC_DATA(0x1076, 0x9000) }, /* GCT GDM7243 */
 45         { USB_DEVICE_CDC_DATA(0x1d74, 0x2300) }, /* LGIT Phoenix */
 46         {}
 47 };
 48 
 49 MODULE_DEVICE_TABLE(usb, id_table);
 50 
 51 static int packet_type_to_index(u16 packetType)
 52 {
 53         int i;
 54 
 55         for (i = 0; i < TTY_MAX_COUNT; i++) {
 56                 if (packet_type[i] == packetType)
 57                         return i;
 58         }
 59 
 60         return -1;
 61 }
 62 
 63 static struct mux_tx *alloc_mux_tx(int len)
 64 {
 65         struct mux_tx *t = NULL;
 66 
 67         t = kzalloc(sizeof(*t), GFP_ATOMIC);
 68         if (!t)
 69                 return NULL;
 70 
 71         t->urb = usb_alloc_urb(0, GFP_ATOMIC);
 72         t->buf = kmalloc(MUX_TX_MAX_SIZE, GFP_ATOMIC);
 73         if (!t->urb || !t->buf) {
 74                 usb_free_urb(t->urb);
 75                 kfree(t->buf);
 76                 kfree(t);
 77                 return NULL;
 78         }
 79 
 80         return t;
 81 }
 82 
 83 static void free_mux_tx(struct mux_tx *t)
 84 {
 85         if (t) {
 86                 usb_free_urb(t->urb);
 87                 kfree(t->buf);
 88                 kfree(t);
 89         }
 90 }
 91 
 92 static struct mux_rx *alloc_mux_rx(void)
 93 {
 94         struct mux_rx *r = NULL;
 95 
 96         r = kzalloc(sizeof(*r), GFP_KERNEL);
 97         if (!r)
 98                 return NULL;
 99 
100         r->urb = usb_alloc_urb(0, GFP_KERNEL);
101         r->buf = kmalloc(MUX_RX_MAX_SIZE, GFP_KERNEL);
102         if (!r->urb || !r->buf) {
103                 usb_free_urb(r->urb);
104                 kfree(r->buf);
105                 kfree(r);
106                 return NULL;
107         }
108 
109         return r;
110 }
111 
112 static void free_mux_rx(struct mux_rx *r)
113 {
114         if (r) {
115                 usb_free_urb(r->urb);
116                 kfree(r->buf);
117                 kfree(r);
118         }
119 }
120 
121 static struct mux_rx *get_rx_struct(struct rx_cxt *rx)
122 {
123         struct mux_rx *r;
124         unsigned long flags;
125 
126         spin_lock_irqsave(&rx->free_list_lock, flags);
127 
128         if (list_empty(&rx->rx_free_list)) {
129                 spin_unlock_irqrestore(&rx->free_list_lock, flags);
130                 return NULL;
131         }
132 
133         r = list_entry(rx->rx_free_list.prev, struct mux_rx, free_list);
134         list_del(&r->free_list);
135 
136         spin_unlock_irqrestore(&rx->free_list_lock, flags);
137 
138         return r;
139 }
140 
141 static void put_rx_struct(struct rx_cxt *rx, struct mux_rx *r)
142 {
143         unsigned long flags;
144 
145         spin_lock_irqsave(&rx->free_list_lock, flags);
146         list_add_tail(&r->free_list, &rx->rx_free_list);
147         spin_unlock_irqrestore(&rx->free_list_lock, flags);
148 }
149 
150 static int up_to_host(struct mux_rx *r)
151 {
152         struct mux_dev *mux_dev = r->mux_dev;
153         struct mux_pkt_header *mux_header;
154         unsigned int start_flag;
155         unsigned int payload_size;
156         unsigned short packet_type;
157         int total_len;
158         u32 packet_size_sum = r->offset;
159         int index;
160         int ret = TO_HOST_INVALID_PACKET;
161         int len = r->len;
162 
163         while (1) {
164                 mux_header = (struct mux_pkt_header *)(r->buf +
165                                                        packet_size_sum);
166                 start_flag = __le32_to_cpu(mux_header->start_flag);
167                 payload_size = __le32_to_cpu(mux_header->payload_size);
168                 packet_type = __le16_to_cpu(mux_header->packet_type);
169 
170                 if (start_flag != START_FLAG) {
171                         pr_err("invalid START_FLAG %x\n", start_flag);
172                         break;
173                 }
174 
175                 total_len = ALIGN(MUX_HEADER_SIZE + payload_size, 4);
176 
177                 if (len - packet_size_sum <
178                         total_len) {
179                         pr_err("invalid payload : %d %d %04x\n",
180                                payload_size, len, packet_type);
181                         break;
182                 }
183 
184                 index = packet_type_to_index(packet_type);
185                 if (index < 0) {
186                         pr_err("invalid index %d\n", index);
187                         break;
188                 }
189 
190                 ret = r->callback(mux_header->data,
191                                 payload_size,
192                                 index,
193                                 mux_dev->tty_dev,
194                                 RECV_PACKET_PROCESS_CONTINUE
195                                 );
196                 if (ret == TO_HOST_BUFFER_REQUEST_FAIL) {
197                         r->offset += packet_size_sum;
198                         break;
199                 }
200 
201                 packet_size_sum += total_len;
202                 if (len - packet_size_sum <= MUX_HEADER_SIZE + 2) {
203                         ret = r->callback(NULL,
204                                         0,
205                                         index,
206                                         mux_dev->tty_dev,
207                                         RECV_PACKET_PROCESS_COMPLETE
208                                         );
209                         break;
210                 }
211         }
212 
213         return ret;
214 }
215 
216 static void do_rx(struct work_struct *work)
217 {
218         struct mux_dev *mux_dev =
219                 container_of(work, struct mux_dev, work_rx.work);
220         struct mux_rx *r;
221         struct rx_cxt *rx = &mux_dev->rx;
222         unsigned long flags;
223         int ret = 0;
224 
225         while (1) {
226                 spin_lock_irqsave(&rx->to_host_lock, flags);
227                 if (list_empty(&rx->to_host_list)) {
228                         spin_unlock_irqrestore(&rx->to_host_lock, flags);
229                         break;
230                 }
231                 r = list_entry(rx->to_host_list.next, struct mux_rx,
232                                to_host_list);
233                 list_del(&r->to_host_list);
234                 spin_unlock_irqrestore(&rx->to_host_lock, flags);
235 
236                 ret = up_to_host(r);
237                 if (ret == TO_HOST_BUFFER_REQUEST_FAIL)
238                         pr_err("failed to send mux data to host\n");
239                 else
240                         put_rx_struct(rx, r);
241         }
242 }
243 
244 static void remove_rx_submit_list(struct mux_rx *r, struct rx_cxt *rx)
245 {
246         unsigned long flags;
247         struct mux_rx   *r_remove, *r_remove_next;
248 
249         spin_lock_irqsave(&rx->submit_list_lock, flags);
250         list_for_each_entry_safe(r_remove, r_remove_next, &rx->rx_submit_list,
251                                  rx_submit_list) {
252                 if (r == r_remove)
253                         list_del(&r->rx_submit_list);
254         }
255         spin_unlock_irqrestore(&rx->submit_list_lock, flags);
256 }
257 
258 static void gdm_mux_rcv_complete(struct urb *urb)
259 {
260         struct mux_rx *r = urb->context;
261         struct mux_dev *mux_dev = r->mux_dev;
262         struct rx_cxt *rx = &mux_dev->rx;
263         unsigned long flags;
264 
265         remove_rx_submit_list(r, rx);
266 
267         if (urb->status) {
268                 if (mux_dev->usb_state == PM_NORMAL)
269                         dev_err(&urb->dev->dev, "%s: urb status error %d\n",
270                                 __func__, urb->status);
271                 put_rx_struct(rx, r);
272         } else {
273                 r->len = r->urb->actual_length;
274                 spin_lock_irqsave(&rx->to_host_lock, flags);
275                 list_add_tail(&r->to_host_list, &rx->to_host_list);
276                 schedule_work(&mux_dev->work_rx.work);
277                 spin_unlock_irqrestore(&rx->to_host_lock, flags);
278         }
279 }
280 
281 static int gdm_mux_recv(void *priv_dev, int (*cb)(void *data, int len,
282                         int tty_index, struct tty_dev *tty_dev, int complete))
283 {
284         struct mux_dev *mux_dev = priv_dev;
285         struct usb_device *usbdev = mux_dev->usbdev;
286         struct mux_rx *r;
287         struct rx_cxt *rx = &mux_dev->rx;
288         unsigned long flags;
289         int ret;
290 
291         if (!usbdev) {
292                 pr_err("device is disconnected\n");
293                 return -ENODEV;
294         }
295 
296         r = get_rx_struct(rx);
297         if (!r) {
298                 pr_err("get_rx_struct fail\n");
299                 return -ENOMEM;
300         }
301 
302         r->offset = 0;
303         r->mux_dev = (void *)mux_dev;
304         r->callback = cb;
305         mux_dev->rx_cb = cb;
306 
307         usb_fill_bulk_urb(r->urb,
308                           usbdev,
309                           usb_rcvbulkpipe(usbdev, 0x86),
310                           r->buf,
311                           MUX_RX_MAX_SIZE,
312                           gdm_mux_rcv_complete,
313                           r);
314 
315         spin_lock_irqsave(&rx->submit_list_lock, flags);
316         list_add_tail(&r->rx_submit_list, &rx->rx_submit_list);
317         spin_unlock_irqrestore(&rx->submit_list_lock, flags);
318 
319         ret = usb_submit_urb(r->urb, GFP_KERNEL);
320 
321         if (ret) {
322                 spin_lock_irqsave(&rx->submit_list_lock, flags);
323                 list_del(&r->rx_submit_list);
324                 spin_unlock_irqrestore(&rx->submit_list_lock, flags);
325 
326                 put_rx_struct(rx, r);
327 
328                 pr_err("usb_submit_urb ret=%d\n", ret);
329         }
330 
331         usb_mark_last_busy(usbdev);
332 
333         return ret;
334 }
335 
336 static void gdm_mux_send_complete(struct urb *urb)
337 {
338         struct mux_tx *t = urb->context;
339 
340         if (urb->status == -ECONNRESET) {
341                 dev_info(&urb->dev->dev, "CONNRESET\n");
342                 free_mux_tx(t);
343                 return;
344         }
345 
346         if (t->callback)
347                 t->callback(t->cb_data);
348 
349         free_mux_tx(t);
350 }
351 
352 static int gdm_mux_send(void *priv_dev, void *data, int len, int tty_index,
353                         void (*cb)(void *data), void *cb_data)
354 {
355         struct mux_dev *mux_dev = priv_dev;
356         struct usb_device *usbdev = mux_dev->usbdev;
357         struct mux_pkt_header *mux_header;
358         struct mux_tx *t = NULL;
359         static u32 seq_num = 1;
360         int total_len;
361         int ret;
362         unsigned long flags;
363 
364         if (mux_dev->usb_state == PM_SUSPEND) {
365                 ret = usb_autopm_get_interface(mux_dev->intf);
366                 if (!ret)
367                         usb_autopm_put_interface(mux_dev->intf);
368         }
369 
370         spin_lock_irqsave(&mux_dev->write_lock, flags);
371 
372         total_len = ALIGN(MUX_HEADER_SIZE + len, 4);
373 
374         t = alloc_mux_tx(total_len);
375         if (!t) {
376                 pr_err("alloc_mux_tx fail\n");
377                 spin_unlock_irqrestore(&mux_dev->write_lock, flags);
378                 return -ENOMEM;
379         }
380 
381         mux_header = (struct mux_pkt_header *)t->buf;
382         mux_header->start_flag = __cpu_to_le32(START_FLAG);
383         mux_header->seq_num = __cpu_to_le32(seq_num++);
384         mux_header->payload_size = __cpu_to_le32((u32)len);
385         mux_header->packet_type = __cpu_to_le16(packet_type[tty_index]);
386 
387         memcpy(t->buf + MUX_HEADER_SIZE, data, len);
388         memset(t->buf + MUX_HEADER_SIZE + len, 0, total_len - MUX_HEADER_SIZE -
389                len);
390 
391         t->len = total_len;
392         t->callback = cb;
393         t->cb_data = cb_data;
394 
395         usb_fill_bulk_urb(t->urb,
396                           usbdev,
397                           usb_sndbulkpipe(usbdev, 5),
398                           t->buf,
399                           total_len,
400                           gdm_mux_send_complete,
401                           t);
402 
403         ret = usb_submit_urb(t->urb, GFP_ATOMIC);
404 
405         spin_unlock_irqrestore(&mux_dev->write_lock, flags);
406 
407         if (ret)
408                 pr_err("usb_submit_urb Error: %d\n", ret);
409 
410         usb_mark_last_busy(usbdev);
411 
412         return ret;
413 }
414 
415 static int gdm_mux_send_control(void *priv_dev, int request, int value,
416                                 void *buf, int len)
417 {
418         struct mux_dev *mux_dev = priv_dev;
419         struct usb_device *usbdev = mux_dev->usbdev;
420         int ret;
421 
422         ret = usb_control_msg(usbdev,
423                               usb_sndctrlpipe(usbdev, 0),
424                               request,
425                               USB_RT_ACM,
426                               value,
427                               2,
428                               buf,
429                               len,
430                               5000
431                              );
432 
433         if (ret < 0)
434                 pr_err("usb_control_msg error: %d\n", ret);
435 
436         return min(ret, 0);
437 }
438 
439 static void release_usb(struct mux_dev *mux_dev)
440 {
441         struct rx_cxt           *rx = &mux_dev->rx;
442         struct mux_rx           *r, *r_next;
443         unsigned long           flags;
444 
445         cancel_delayed_work(&mux_dev->work_rx);
446 
447         spin_lock_irqsave(&rx->submit_list_lock, flags);
448         list_for_each_entry_safe(r, r_next, &rx->rx_submit_list,
449                                  rx_submit_list) {
450                 spin_unlock_irqrestore(&rx->submit_list_lock, flags);
451                 usb_kill_urb(r->urb);
452                 spin_lock_irqsave(&rx->submit_list_lock, flags);
453         }
454         spin_unlock_irqrestore(&rx->submit_list_lock, flags);
455 
456         spin_lock_irqsave(&rx->free_list_lock, flags);
457         list_for_each_entry_safe(r, r_next, &rx->rx_free_list, free_list) {
458                 list_del(&r->free_list);
459                 free_mux_rx(r);
460         }
461         spin_unlock_irqrestore(&rx->free_list_lock, flags);
462 
463         spin_lock_irqsave(&rx->to_host_lock, flags);
464         list_for_each_entry_safe(r, r_next, &rx->to_host_list, to_host_list) {
465                 if (r->mux_dev == (void *)mux_dev) {
466                         list_del(&r->to_host_list);
467                         free_mux_rx(r);
468                 }
469         }
470         spin_unlock_irqrestore(&rx->to_host_lock, flags);
471 }
472 
473 static int init_usb(struct mux_dev *mux_dev)
474 {
475         struct mux_rx *r;
476         struct rx_cxt *rx = &mux_dev->rx;
477         int ret = 0;
478         int i;
479 
480         spin_lock_init(&mux_dev->write_lock);
481         INIT_LIST_HEAD(&rx->to_host_list);
482         INIT_LIST_HEAD(&rx->rx_submit_list);
483         INIT_LIST_HEAD(&rx->rx_free_list);
484         spin_lock_init(&rx->to_host_lock);
485         spin_lock_init(&rx->submit_list_lock);
486         spin_lock_init(&rx->free_list_lock);
487 
488         for (i = 0; i < MAX_ISSUE_NUM * 2; i++) {
489                 r = alloc_mux_rx();
490                 if (!r) {
491                         ret = -ENOMEM;
492                         break;
493                 }
494 
495                 list_add(&r->free_list, &rx->rx_free_list);
496         }
497 
498         INIT_DELAYED_WORK(&mux_dev->work_rx, do_rx);
499 
500         return ret;
501 }
502 
503 static int gdm_mux_probe(struct usb_interface *intf,
504                          const struct usb_device_id *id)
505 {
506         struct mux_dev *mux_dev;
507         struct tty_dev *tty_dev;
508         u16 idVendor, idProduct;
509         int bInterfaceNumber;
510         int ret;
511         int i;
512         struct usb_device *usbdev = interface_to_usbdev(intf);
513 
514         bInterfaceNumber = intf->cur_altsetting->desc.bInterfaceNumber;
515 
516         idVendor = __le16_to_cpu(usbdev->descriptor.idVendor);
517         idProduct = __le16_to_cpu(usbdev->descriptor.idProduct);
518 
519         pr_info("mux vid = 0x%04x pid = 0x%04x\n", idVendor, idProduct);
520 
521         if (bInterfaceNumber != 2)
522                 return -ENODEV;
523 
524         mux_dev = kzalloc(sizeof(*mux_dev), GFP_KERNEL);
525         if (!mux_dev)
526                 return -ENOMEM;
527 
528         tty_dev = kzalloc(sizeof(*tty_dev), GFP_KERNEL);
529         if (!tty_dev) {
530                 ret = -ENOMEM;
531                 goto err_free_mux;
532         }
533 
534         mux_dev->usbdev = usbdev;
535         mux_dev->control_intf = intf;
536 
537         ret = init_usb(mux_dev);
538         if (ret)
539                 goto err_free_usb;
540 
541         tty_dev->priv_dev = (void *)mux_dev;
542         tty_dev->send_func = gdm_mux_send;
543         tty_dev->recv_func = gdm_mux_recv;
544         tty_dev->send_control = gdm_mux_send_control;
545 
546         ret = register_lte_tty_device(tty_dev, &intf->dev);
547         if (ret)
548                 goto err_unregister_tty;
549 
550         for (i = 0; i < TTY_MAX_COUNT; i++)
551                 mux_dev->tty_dev = tty_dev;
552 
553         mux_dev->intf = intf;
554         mux_dev->usb_state = PM_NORMAL;
555 
556         usb_get_dev(usbdev);
557         usb_set_intfdata(intf, tty_dev);
558 
559         return 0;
560 
561 err_unregister_tty:
562         unregister_lte_tty_device(tty_dev);
563 err_free_usb:
564         release_usb(mux_dev);
565         kfree(tty_dev);
566 err_free_mux:
567         kfree(mux_dev);
568 
569         return ret;
570 }
571 
572 static void gdm_mux_disconnect(struct usb_interface *intf)
573 {
574         struct tty_dev *tty_dev;
575         struct mux_dev *mux_dev;
576         struct usb_device *usbdev = interface_to_usbdev(intf);
577 
578         tty_dev = usb_get_intfdata(intf);
579 
580         mux_dev = tty_dev->priv_dev;
581 
582         release_usb(mux_dev);
583         unregister_lte_tty_device(tty_dev);
584 
585         kfree(mux_dev);
586         kfree(tty_dev);
587 
588         usb_put_dev(usbdev);
589 }
590 
591 static int gdm_mux_suspend(struct usb_interface *intf, pm_message_t pm_msg)
592 {
593         struct tty_dev *tty_dev;
594         struct mux_dev *mux_dev;
595         struct rx_cxt *rx;
596         struct mux_rx *r, *r_next;
597         unsigned long flags;
598 
599         tty_dev = usb_get_intfdata(intf);
600         mux_dev = tty_dev->priv_dev;
601         rx = &mux_dev->rx;
602 
603         cancel_work_sync(&mux_dev->work_rx.work);
604 
605         if (mux_dev->usb_state != PM_NORMAL) {
606                 dev_err(intf->usb_dev, "usb suspend - invalid state\n");
607                 return -1;
608         }
609 
610         mux_dev->usb_state = PM_SUSPEND;
611 
612         spin_lock_irqsave(&rx->submit_list_lock, flags);
613         list_for_each_entry_safe(r, r_next, &rx->rx_submit_list,
614                                  rx_submit_list) {
615                 spin_unlock_irqrestore(&rx->submit_list_lock, flags);
616                 usb_kill_urb(r->urb);
617                 spin_lock_irqsave(&rx->submit_list_lock, flags);
618         }
619         spin_unlock_irqrestore(&rx->submit_list_lock, flags);
620 
621         return 0;
622 }
623 
624 static int gdm_mux_resume(struct usb_interface *intf)
625 {
626         struct tty_dev *tty_dev;
627         struct mux_dev *mux_dev;
628         u8 i;
629 
630         tty_dev = usb_get_intfdata(intf);
631         mux_dev = tty_dev->priv_dev;
632 
633         if (mux_dev->usb_state != PM_SUSPEND) {
634                 dev_err(intf->usb_dev, "usb resume - invalid state\n");
635                 return -1;
636         }
637 
638         mux_dev->usb_state = PM_NORMAL;
639 
640         for (i = 0; i < MAX_ISSUE_NUM; i++)
641                 gdm_mux_recv(mux_dev, mux_dev->rx_cb);
642 
643         return 0;
644 }
645 
646 static struct usb_driver gdm_mux_driver = {
647         .name = "gdm_mux",
648         .probe = gdm_mux_probe,
649         .disconnect = gdm_mux_disconnect,
650         .id_table = id_table,
651         .supports_autosuspend = 1,
652         .suspend = gdm_mux_suspend,
653         .resume = gdm_mux_resume,
654         .reset_resume = gdm_mux_resume,
655 };
656 
657 static int __init gdm_usb_mux_init(void)
658 {
659         register_lte_tty_driver();
660 
661         return usb_register(&gdm_mux_driver);
662 }
663 
664 static void __exit gdm_usb_mux_exit(void)
665 {
666         unregister_lte_tty_driver();
667 
668         usb_deregister(&gdm_mux_driver);
669 }
670 
671 module_init(gdm_usb_mux_init);
672 module_exit(gdm_usb_mux_exit);
673 
674 MODULE_DESCRIPTION("GCT LTE TTY Device Driver");
675 MODULE_LICENSE("GPL");
676 

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us