1 /* qmidevice.c - gobi QMI device
2 * Copyright (c) 2010, Code Aurora Forum. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
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.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 #include "qmidevice.h"
21 #include <linux/poll.h>
24 struct list_head node;
31 struct list_head node;
32 void (*func)(struct qcusbnet *, u16, void *);
38 struct list_head node;
40 struct list_head reads;
41 struct list_head notifies;
42 struct list_head urbs;
43 wait_queue_head_t read_wait;
60 static int qcusbnet2k_fwdelay;
62 static bool device_valid(struct qcusbnet *dev);
63 static struct client *client_bycid(struct qcusbnet *dev, u16 cid);
64 static bool client_addread(struct qcusbnet *dev, u16 cid, u16 tid, void *data, u16 size);
65 static bool client_delread(struct qcusbnet *dev, u16 cid, u16 tid, void **data, u16 *size);
66 static bool client_addnotify(struct qcusbnet *dev, u16 cid, u16 tid,
67 void (*hook)(struct qcusbnet *, u16 cid, void *),
69 static bool client_notify(struct qcusbnet *dev, u16 cid, u16 tid);
70 static bool client_addurb(struct qcusbnet *dev, u16 cid, struct urb *urb);
71 static struct urb *client_delurb(struct qcusbnet *dev, u16 cid);
73 static int devqmi_open(struct inode *inode, struct file *file);
74 static long devqmi_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
75 static int devqmi_close(struct file *file, fl_owner_t ftable);
76 static ssize_t devqmi_read(struct file *file, char __user *buf, size_t size,
78 static ssize_t devqmi_write(struct file *file, const char __user *buf,
79 size_t size, loff_t *pos);
80 static unsigned int devqmi_poll(struct file *file, poll_table *wait);
82 static bool qmi_ready(struct qcusbnet *dev, u16 timeout);
83 static void wds_callback(struct qcusbnet *dev, u16 cid, void *data);
84 static int setup_wds_callback(struct qcusbnet *dev);
85 static int qmidms_getmeid(struct qcusbnet *dev);
87 #define IOCTL_QMI_GET_SERVICE_FILE (0x8BE0 + 1)
88 #define IOCTL_QMI_GET_DEVICE_VIDPID (0x8BE0 + 2)
89 #define IOCTL_QMI_GET_DEVICE_MEID (0x8BE0 + 3)
90 #define CDC_GET_MASK 0xFFFFll
91 #define CDC_GET_ENCAPSULATED_RESPONSE 0x01A1ll
92 #define CDC_CONNECTION_SPEED_CHANGE 0x08000000002AA1ll
94 static const struct file_operations devqmi_fops = {
97 .write = devqmi_write,
98 .unlocked_ioctl = devqmi_ioctl,
100 .flush = devqmi_close,
105 static inline void assert_locked(struct qcusbnet *dev)
107 BUG_ON(!spin_is_locked(&dev->qmi.clients_lock));
110 static inline void assert_locked(struct qcusbnet *dev)
116 static bool device_valid(struct qcusbnet *dev)
118 return dev && dev->valid;
121 void qc_setdown(struct qcusbnet *dev, u8 reason)
123 set_bit(reason, &dev->down);
124 netif_carrier_off(dev->usbnet->net);
127 void qc_cleardown(struct qcusbnet *dev, u8 reason)
129 clear_bit(reason, &dev->down);
131 netif_carrier_on(dev->usbnet->net);
134 bool qc_isdown(struct qcusbnet *dev, u8 reason)
136 return test_bit(reason, &dev->down);
139 static void read_callback(struct urb *urb)
141 struct list_head *node;
144 struct client *client;
148 struct qcusbnet *dev;
153 ERR("bad read URB\n");
158 if (!device_valid(dev)) {
159 ERR("Invalid device!\n");
164 DBG("Read status = %d\n", urb->status);
168 DBG("Read %d bytes\n", urb->actual_length);
170 data = urb->transfer_buffer;
171 size = urb->actual_length;
174 print_hex_dump(KERN_INFO, "QCUSBNet2k: ", DUMP_PREFIX_OFFSET,
175 16, 1, data, size, true);
177 result = qmux_parse(&cid, data, size);
179 ERR("Read error parsing QMUX %d\n", result);
183 if (size < result + 3) {
184 DBG("Data buffer too small to parse\n");
189 tid = *(u8 *)(data + result + 1);
191 tid = *(u16 *)(data + result + 1);
192 spin_lock_irqsave(&dev->qmi.clients_lock, flags);
194 list_for_each(node, &dev->qmi.clients) {
195 client = list_entry(node, struct client, node);
196 if (client->cid == cid || (client->cid | 0xff00) == cid) {
197 copy = kmalloc(size, GFP_ATOMIC);
199 ERR("malloc failed\n");
200 spin_unlock_irqrestore(&dev->qmi.clients_lock, flags);
203 memcpy(copy, data, size);
204 if (!client_addread(dev, client->cid, tid, copy, size)) {
205 ERR("Error allocating pReadMemListEntry "
206 "read will be discarded\n");
208 spin_unlock_irqrestore(&dev->qmi.clients_lock, flags);
212 wake_up_interruptible(&client->read_wait);
214 DBG("Creating new readListEntry for client 0x%04X, TID %x\n",
217 client_notify(dev, client->cid, tid);
219 if (cid >> 8 != 0xff)
224 spin_unlock_irqrestore(&dev->qmi.clients_lock, flags);
227 static void int_callback(struct urb *urb)
231 struct qcusbnet *dev = (struct qcusbnet *)urb->context;
233 if (!device_valid(dev)) {
234 ERR("Invalid device!\n");
239 DBG("Int status = %d\n", urb->status);
240 if (urb->status != -EOVERFLOW)
243 if ((urb->actual_length == 8) &&
244 (*(u64 *)urb->transfer_buffer & CDC_GET_MASK) ==
245 CDC_GET_ENCAPSULATED_RESPONSE) {
246 usb_fill_control_urb(dev->qmi.readurb, dev->usbnet->udev,
247 usb_rcvctrlpipe(dev->usbnet->udev, 0),
248 (unsigned char *)dev->qmi.readsetup,
250 DEFAULT_READ_URB_LENGTH,
252 status = usb_submit_urb(dev->qmi.readurb, GFP_ATOMIC);
254 ERR("Error submitting Read URB %d\n", status);
257 } else if ((urb->actual_length == 16) &&
258 (*(u64 *)urb->transfer_buffer == CDC_CONNECTION_SPEED_CHANGE)) {
259 /* if upstream or downstream is 0, stop traffic.
260 * Otherwise resume it */
261 if ((*(u32 *)(urb->transfer_buffer + 8) == 0) ||
262 (*(u32 *)(urb->transfer_buffer + 12) == 0)) {
263 qc_setdown(dev, DOWN_CDC_CONNECTION_SPEED);
264 DBG("traffic stopping due to CONNECTION_SPEED_CHANGE\n");
266 qc_cleardown(dev, DOWN_CDC_CONNECTION_SPEED);
267 DBG("resuming traffic due to CONNECTION_SPEED_CHANGE\n");
270 DBG("ignoring invalid interrupt in packet\n");
272 print_hex_dump(KERN_INFO, "QCUSBNet2k: ",
273 DUMP_PREFIX_OFFSET, 16, 1,
274 urb->transfer_buffer,
275 urb->actual_length, true);
279 interval = (dev->usbnet->udev->speed == USB_SPEED_HIGH) ? 7 : 3;
281 usb_fill_int_urb(urb, urb->dev, urb->pipe, urb->transfer_buffer,
282 urb->transfer_buffer_length, urb->complete,
283 urb->context, interval);
284 status = usb_submit_urb(urb, GFP_ATOMIC);
286 ERR("Error re-submitting Int URB %d\n", status);
290 int qc_startread(struct qcusbnet *dev)
295 struct usb_host_endpoint *endpoint = NULL;
297 if (!device_valid(dev)) {
298 ERR("Invalid device!\n");
302 dev->qmi.readurb = usb_alloc_urb(0, GFP_KERNEL);
303 if (!dev->qmi.readurb) {
304 ERR("Error allocating read urb\n");
308 dev->qmi.inturb = usb_alloc_urb(0, GFP_KERNEL);
309 if (!dev->qmi.inturb) {
310 usb_free_urb(dev->qmi.readurb);
311 ERR("Error allocating int urb\n");
315 dev->qmi.readbuf = kmalloc(DEFAULT_READ_URB_LENGTH, GFP_KERNEL);
316 if (!dev->qmi.readbuf) {
317 usb_free_urb(dev->qmi.readurb);
318 usb_free_urb(dev->qmi.inturb);
319 ERR("Error allocating read buffer\n");
323 dev->qmi.intbuf = kmalloc(DEFAULT_READ_URB_LENGTH, GFP_KERNEL);
324 if (!dev->qmi.intbuf) {
325 usb_free_urb(dev->qmi.readurb);
326 usb_free_urb(dev->qmi.inturb);
327 kfree(dev->qmi.readbuf);
328 ERR("Error allocating int buffer\n");
332 dev->qmi.readsetup = kmalloc(sizeof(*dev->qmi.readsetup), GFP_KERNEL);
333 if (!dev->qmi.readsetup) {
334 usb_free_urb(dev->qmi.readurb);
335 usb_free_urb(dev->qmi.inturb);
336 kfree(dev->qmi.readbuf);
337 kfree(dev->qmi.intbuf);
338 ERR("Error allocating setup packet buffer\n");
342 dev->qmi.readsetup->type = 0xA1;
343 dev->qmi.readsetup->code = 1;
344 dev->qmi.readsetup->value = 0;
345 dev->qmi.readsetup->index = dev->iface->cur_altsetting->desc.bInterfaceNumber;
346 dev->qmi.readsetup->len = DEFAULT_READ_URB_LENGTH;
348 interval = (dev->usbnet->udev->speed == USB_SPEED_HIGH) ? 7 : 3;
350 numends = dev->iface->cur_altsetting->desc.bNumEndpoints;
351 for (i = 0; i < numends; i++) {
352 endpoint = dev->iface->cur_altsetting->endpoint + i;
354 ERR("invalid endpoint %u\n", i);
358 if (usb_endpoint_dir_in(&endpoint->desc)
359 && usb_endpoint_xfer_int(&endpoint->desc)) {
360 DBG("Interrupt endpoint is %x\n", endpoint->desc.bEndpointAddress);
365 usb_fill_int_urb(dev->qmi.inturb, dev->usbnet->udev,
366 usb_rcvintpipe(dev->usbnet->udev, endpoint->desc.bEndpointAddress),
367 dev->qmi.intbuf, DEFAULT_READ_URB_LENGTH,
368 int_callback, dev, interval);
370 return usb_submit_urb(dev->qmi.inturb, GFP_KERNEL);
373 void qc_stopread(struct qcusbnet *dev)
375 if (dev->qmi.readurb) {
376 DBG("Killing read URB\n");
377 usb_kill_urb(dev->qmi.readurb);
380 if (dev->qmi.inturb) {
381 DBG("Killing int URB\n");
382 usb_kill_urb(dev->qmi.inturb);
385 kfree(dev->qmi.readsetup);
386 dev->qmi.readsetup = NULL;
387 kfree(dev->qmi.readbuf);
388 dev->qmi.readbuf = NULL;
389 kfree(dev->qmi.intbuf);
390 dev->qmi.intbuf = NULL;
392 usb_free_urb(dev->qmi.readurb);
393 dev->qmi.readurb = NULL;
394 usb_free_urb(dev->qmi.inturb);
395 dev->qmi.inturb = NULL;
398 static int read_async(struct qcusbnet *dev, u16 cid, u16 tid,
399 void (*hook)(struct qcusbnet *, u16, void *),
402 struct list_head *node;
403 struct client *client;
404 struct readreq *readreq;
408 if (!device_valid(dev)) {
409 ERR("Invalid device!\n");
413 spin_lock_irqsave(&dev->qmi.clients_lock, flags);
415 client = client_bycid(dev, cid);
417 ERR("Could not find matching client ID 0x%04X\n", cid);
418 spin_unlock_irqrestore(&dev->qmi.clients_lock, flags);
422 list_for_each(node, &client->reads) {
423 readreq = list_entry(node, struct readreq, node);
424 if (!tid || tid == readreq->tid) {
425 spin_unlock_irqrestore(&dev->qmi.clients_lock, flags);
426 hook(dev, cid, data);
431 if (!client_addnotify(dev, cid, tid, hook, data))
432 ERR("Unable to register for notification\n");
434 spin_unlock_irqrestore(&dev->qmi.clients_lock, flags);
438 static void upsem(struct qcusbnet *dev, u16 cid, void *data)
440 DBG("0x%04X\n", cid);
441 up((struct semaphore *)data);
444 static int read_sync(struct qcusbnet *dev, void **buf, u16 cid, u16 tid)
446 struct list_head *node;
448 struct client *client;
449 struct notifyreq *notify;
450 struct semaphore sem;
455 if (!device_valid(dev)) {
456 ERR("Invalid device!\n");
460 spin_lock_irqsave(&dev->qmi.clients_lock, flags);
462 client = client_bycid(dev, cid);
464 ERR("Could not find matching client ID 0x%04X\n", cid);
465 spin_unlock_irqrestore(&dev->qmi.clients_lock, flags);
469 while (!client_delread(dev, cid, tid, &data, &size)) {
471 if (!client_addnotify(dev, cid, tid, upsem, &sem)) {
472 ERR("unable to register for notification\n");
473 spin_unlock_irqrestore(&dev->qmi.clients_lock, flags);
477 spin_unlock_irqrestore(&dev->qmi.clients_lock, flags);
479 result = down_interruptible(&sem);
481 DBG("Interrupted %d\n", result);
482 spin_lock_irqsave(&dev->qmi.clients_lock, flags);
483 list_for_each(node, &client->notifies) {
484 notify = list_entry(node, struct notifyreq, node);
485 if (notify->data == &sem) {
486 list_del(¬ify->node);
492 spin_unlock_irqrestore(&dev->qmi.clients_lock, flags);
496 if (!device_valid(dev)) {
497 ERR("Invalid device!\n");
501 spin_lock_irqsave(&dev->qmi.clients_lock, flags);
504 spin_unlock_irqrestore(&dev->qmi.clients_lock, flags);
509 static void write_callback(struct urb *urb)
516 DBG("Write status/size %d/%d\n", urb->status, urb->actual_length);
517 up((struct semaphore *)urb->context);
520 static int write_sync(struct qcusbnet *dev, char *buf, int size, u16 cid)
523 struct semaphore sem;
525 struct urbsetup setup;
528 if (!device_valid(dev)) {
529 ERR("Invalid device!\n");
533 urb = usb_alloc_urb(0, GFP_KERNEL);
535 ERR("URB mem error\n");
539 result = qmux_fill(cid, buf, size);
545 /* CDC Send Encapsulated Request packet */
549 setup.index = dev->iface->cur_altsetting->desc.bInterfaceNumber;
553 usb_fill_control_urb(urb, dev->usbnet->udev,
554 usb_sndctrlpipe(dev->usbnet->udev, 0),
555 (unsigned char *)&setup, (void *)buf, size,
558 DBG("Actual Write:\n");
560 print_hex_dump(KERN_INFO, "QCUSBNet2k: ", DUMP_PREFIX_OFFSET,
561 16, 1, buf, size, true);
565 urb->complete = write_callback;
568 result = usb_autopm_get_interface(dev->iface);
570 ERR("unable to resume interface: %d\n", result);
571 if (result == -EPERM)
572 qc_suspend(dev->iface, PMSG_SUSPEND);
577 spin_lock_irqsave(&dev->qmi.clients_lock, flags);
579 if (!client_addurb(dev, cid, urb)) {
581 spin_unlock_irqrestore(&dev->qmi.clients_lock, flags);
582 usb_autopm_put_interface(dev->iface);
586 result = usb_submit_urb(urb, GFP_KERNEL);
588 ERR("submit URB error %d\n", result);
589 if (client_delurb(dev, cid) != urb)
590 ERR("Didn't get write URB back\n");
594 spin_unlock_irqrestore(&dev->qmi.clients_lock, flags);
595 usb_autopm_put_interface(dev->iface);
599 spin_unlock_irqrestore(&dev->qmi.clients_lock, flags);
600 result = down_interruptible(&sem);
601 if (!device_valid(dev)) {
602 ERR("Invalid device!\n");
606 usb_autopm_put_interface(dev->iface);
607 spin_lock_irqsave(&dev->qmi.clients_lock, flags);
608 if (client_delurb(dev, cid) != urb) {
609 ERR("Didn't get write URB back\n");
610 spin_unlock_irqrestore(&dev->qmi.clients_lock, flags);
613 spin_unlock_irqrestore(&dev->qmi.clients_lock, flags);
619 ERR("bad status = %d\n", urb->status);
620 result = urb->status;
623 ERR("Interrupted %d !!!\n", result);
624 ERR("Device may be in bad state and need reset !!!\n");
632 static int client_alloc(struct qcusbnet *dev, u8 type)
635 struct client *client;
644 if (!device_valid(dev)) {
645 ERR("Invalid device!\n");
650 tid = atomic_add_return(1, &dev->qmi.qmitid);
652 atomic_add_return(1, &dev->qmi.qmitid);
653 wbuf = qmictl_new_getcid(tid, type, &wbufsize);
656 result = write_sync(dev, wbuf, wbufsize, QMICTL);
662 result = read_sync(dev, &rbuf, QMICTL, tid);
664 ERR("bad read data %d\n", result);
669 result = qmictl_alloccid_resp(rbuf, rbufsize, &cid);
678 spin_lock_irqsave(&dev->qmi.clients_lock, flags);
679 if (client_bycid(dev, cid)) {
680 DBG("Client memory already exists\n");
681 spin_unlock_irqrestore(&dev->qmi.clients_lock, flags);
682 return -ETOOMANYREFS;
685 client = kmalloc(sizeof(*client), GFP_ATOMIC);
687 ERR("Error allocating read list\n");
688 spin_unlock_irqrestore(&dev->qmi.clients_lock, flags);
692 list_add_tail(&client->node, &dev->qmi.clients);
694 INIT_LIST_HEAD(&client->reads);
695 INIT_LIST_HEAD(&client->notifies);
696 INIT_LIST_HEAD(&client->urbs);
697 init_waitqueue_head(&client->read_wait);
698 spin_unlock_irqrestore(&dev->qmi.clients_lock, flags);
702 static void client_free(struct qcusbnet *dev, u16 cid)
704 struct list_head *node, *tmp;
706 struct client *client;
717 if (!device_valid(dev)) {
718 ERR("invalid device\n");
722 DBG("releasing 0x%04X\n", cid);
725 tid = atomic_add_return(1, &dev->qmi.qmitid);
727 tid = atomic_add_return(1, &dev->qmi.qmitid);
728 wbuf = qmictl_new_releasecid(tid, cid, &wbufsize);
730 ERR("memory error\n");
732 result = write_sync(dev, wbuf, wbufsize, QMICTL);
736 ERR("bad write status %d\n", result);
738 result = read_sync(dev, &rbuf, QMICTL, tid);
740 ERR("bad read status %d\n", result);
743 result = qmictl_freecid_resp(rbuf, rbufsize);
746 ERR("error %d parsing response\n", result);
752 spin_lock_irqsave(&dev->qmi.clients_lock, flags);
753 list_for_each_safe(node, tmp, &dev->qmi.clients) {
754 client = list_entry(node, struct client, node);
755 if (client->cid == cid) {
756 while (client_notify(dev, cid, 0))
759 urb = client_delurb(dev, cid);
760 while (urb != NULL) {
763 urb = client_delurb(dev, cid);
766 while (client_delread(dev, cid, 0, &data, &size))
769 list_del(&client->node);
774 spin_unlock_irqrestore(&dev->qmi.clients_lock, flags);
777 struct client *client_bycid(struct qcusbnet *dev, u16 cid)
779 struct list_head *node;
780 struct client *client;
782 if (!device_valid(dev)) {
783 ERR("Invalid device\n");
789 list_for_each(node, &dev->qmi.clients) {
790 client = list_entry(node, struct client, node);
791 if (client->cid == cid)
795 DBG("Could not find client mem 0x%04X\n", cid);
799 static bool client_addread(struct qcusbnet *dev, u16 cid, u16 tid, void *data,
802 struct client *client;
807 client = client_bycid(dev, cid);
809 ERR("Could not find this client's memory 0x%04X\n", cid);
813 req = kmalloc(sizeof(*req), GFP_ATOMIC);
823 list_add_tail(&req->node, &client->reads);
828 static bool client_delread(struct qcusbnet *dev, u16 cid, u16 tid, void **data,
831 struct client *client;
833 struct list_head *node;
837 client = client_bycid(dev, cid);
839 ERR("Could not find this client's memory 0x%04X\n", cid);
843 list_for_each(node, &client->reads) {
844 req = list_entry(node, struct readreq, node);
845 if (!tid || tid == req->tid) {
848 list_del(&req->node);
853 DBG("skipping 0x%04X data TID = %x\n", cid, req->tid);
856 DBG("No read memory to pop, Client 0x%04X, TID = %x\n", cid, tid);
860 static bool client_addnotify(struct qcusbnet *dev, u16 cid, u16 tid,
861 void (*hook)(struct qcusbnet *, u16, void *),
864 struct client *client;
865 struct notifyreq *req;
869 client = client_bycid(dev, cid);
871 ERR("Could not find this client's memory 0x%04X\n", cid);
875 req = kmalloc(sizeof(*req), GFP_ATOMIC);
881 list_add_tail(&req->node, &client->notifies);
889 static bool client_notify(struct qcusbnet *dev, u16 cid, u16 tid)
891 struct client *client;
892 struct notifyreq *delnotify, *notify;
893 struct list_head *node;
897 client = client_bycid(dev, cid);
899 ERR("Could not find this client's memory 0x%04X\n", cid);
905 list_for_each(node, &client->notifies) {
906 notify = list_entry(node, struct notifyreq, node);
907 if (!tid || !notify->tid || tid == notify->tid) {
912 DBG("skipping data TID = %x\n", notify->tid);
916 list_del(&delnotify->node);
917 if (delnotify->func) {
918 spin_unlock(&dev->qmi.clients_lock);
919 delnotify->func(dev, cid, delnotify->data);
920 spin_lock(&dev->qmi.clients_lock);
926 DBG("no one to notify for TID %x\n", tid);
930 static bool client_addurb(struct qcusbnet *dev, u16 cid, struct urb *urb)
932 struct client *client;
937 client = client_bycid(dev, cid);
939 ERR("Could not find this client's memory 0x%04X\n", cid);
943 req = kmalloc(sizeof(*req), GFP_ATOMIC);
950 list_add_tail(&req->node, &client->urbs);
955 static struct urb *client_delurb(struct qcusbnet *dev, u16 cid)
957 struct client *client;
963 client = client_bycid(dev, cid);
965 ERR("Could not find this client's memory 0x%04X\n", cid);
969 if (list_empty(&client->urbs)) {
970 DBG("No URB's to pop\n");
974 req = list_first_entry(&client->urbs, struct urbreq, node);
975 list_del(&req->node);
981 static int devqmi_open(struct inode *inode, struct file *file)
983 struct qmihandle *handle;
984 struct qmidev *qmidev = container_of(inode->i_cdev, struct qmidev, cdev);
985 struct qcusbnet *dev = container_of(qmidev, struct qcusbnet, qmi);
987 if (!device_valid(dev)) {
988 ERR("Invalid device\n");
992 file->private_data = kmalloc(sizeof(struct qmihandle), GFP_KERNEL);
993 if (!file->private_data) {
998 handle = (struct qmihandle *)file->private_data;
999 handle->cid = (u16)-1;
1005 static long devqmi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1010 struct qmihandle *handle = (struct qmihandle *)file->private_data;
1013 ERR("Bad file data\n");
1017 if (!device_valid(handle->dev)) {
1018 ERR("Invalid device! Updating f_ops\n");
1019 file->f_op = file->f_dentry->d_inode->i_fop;
1024 case IOCTL_QMI_GET_SERVICE_FILE:
1026 DBG("Setting up QMI for service %lu\n", arg);
1028 ERR("Cannot use QMICTL from userspace\n");
1032 if (handle->cid != (u16)-1) {
1033 ERR("Close the current connection before opening a new one\n");
1037 result = client_alloc(handle->dev, (u8)arg);
1040 handle->cid = result;
1046 case IOCTL_QMI_GET_DEVICE_VIDPID:
1048 ERR("Bad VIDPID buffer\n");
1052 if (!handle->dev->usbnet) {
1053 ERR("Bad usbnet\n");
1057 if (!handle->dev->usbnet->udev) {
1062 vidpid = ((le16_to_cpu(handle->dev->usbnet->udev->descriptor.idVendor) << 16)
1063 + le16_to_cpu(handle->dev->usbnet->udev->descriptor.idProduct));
1065 result = copy_to_user((unsigned int *)arg, &vidpid, 4);
1067 ERR("Copy to userspace failure\n");
1072 case IOCTL_QMI_GET_DEVICE_MEID:
1074 ERR("Bad MEID buffer\n");
1078 result = copy_to_user((unsigned int *)arg, &handle->dev->meid[0], 14);
1080 ERR("copy to userspace failure\n");
1089 static int devqmi_close(struct file *file, fl_owner_t ftable)
1091 struct qmihandle *handle = (struct qmihandle *)file->private_data;
1092 struct list_head *tasks;
1093 struct task_struct *task;
1094 struct fdtable *fdtable;
1097 unsigned long flags;
1100 ERR("bad file data\n");
1104 if (file_count(file) != 1) {
1105 /* XXX: This can't possibly be safe. We don't hold any sort of
1106 * lock here, and we're walking a list of threads... */
1107 list_for_each(tasks, ¤t->group_leader->tasks) {
1108 task = container_of(tasks, struct task_struct, tasks);
1109 if (!task || !task->files)
1111 spin_lock_irqsave(&task->files->file_lock, flags);
1112 fdtable = files_fdtable(task->files);
1113 for (count = 0; count < fdtable->max_fds; count++) {
1114 /* Before this function was called, this file was removed
1115 * from our task's file table so if we find it in a file
1116 * table then it is being used by another task
1118 if (fdtable->fd[count] == file) {
1123 spin_unlock_irqrestore(&task->files->file_lock, flags);
1127 DBG("not closing, as this FD is open by %d other process\n", used);
1132 if (!device_valid(handle->dev)) {
1133 ERR("Invalid device! Updating f_ops\n");
1134 file->f_op = file->f_dentry->d_inode->i_fop;
1138 DBG("0x%04X\n", handle->cid);
1140 file->private_data = NULL;
1142 if (handle->cid != (u16)-1)
1143 client_free(handle->dev, handle->cid);
1149 static ssize_t devqmi_read(struct file *file, char __user *buf, size_t size,
1155 struct qmihandle *handle = (struct qmihandle *)file->private_data;
1158 ERR("Bad file data\n");
1162 if (!device_valid(handle->dev)) {
1163 ERR("Invalid device! Updating f_ops\n");
1164 file->f_op = file->f_dentry->d_inode->i_fop;
1168 if (handle->cid == (u16)-1) {
1169 ERR("Client ID must be set before reading 0x%04X\n",
1174 result = read_sync(handle->dev, &data, handle->cid, 0);
1178 result -= qmux_size;
1179 smalldata = data + qmux_size;
1181 if (result > size) {
1182 ERR("Read data is too large for amount user has requested\n");
1187 if (copy_to_user(buf, smalldata, result)) {
1188 ERR("Error copying read data to user\n");
1196 static ssize_t devqmi_write(struct file *file, const char __user * buf,
1197 size_t size, loff_t *pos)
1201 struct qmihandle *handle = (struct qmihandle *)file->private_data;
1204 ERR("Bad file data\n");
1208 if (!device_valid(handle->dev)) {
1209 ERR("Invalid device! Updating f_ops\n");
1210 file->f_op = file->f_dentry->d_inode->i_fop;
1214 if (handle->cid == (u16)-1) {
1215 ERR("Client ID must be set before writing 0x%04X\n",
1220 wbuf = kmalloc(size + qmux_size, GFP_KERNEL);
1223 status = copy_from_user(wbuf + qmux_size, buf, size);
1225 ERR("Unable to copy data from userspace %d\n", status);
1230 status = write_sync(handle->dev, wbuf, size + qmux_size,
1234 if (status == size + qmux_size)
1239 static unsigned int devqmi_poll(struct file *file, poll_table *wait)
1241 struct qmihandle *handle = (struct qmihandle *)file->private_data;
1242 struct client *client;
1243 unsigned int mask = 0;
1244 unsigned long flags;
1247 ERR("Bad file data\n");
1251 if (!device_valid(handle->dev)) {
1252 ERR("Invalid device! Updating f_ops\n");
1253 file->f_op = file->f_dentry->d_inode->i_fop;
1257 if (handle->cid == (u16)-1) {
1258 ERR("Client ID must be set before polling 0x%04X\n",
1263 spin_lock_irqsave(&handle->dev->qmi.clients_lock, flags);
1265 client = client_bycid(handle->dev, handle->cid);
1267 ERR("Could not find matching client ID 0x%04X\n", handle->cid);
1268 spin_unlock_irqrestore(&handle->dev->qmi.clients_lock, flags);
1272 poll_wait(file, &client->read_wait, wait);
1274 if (!list_empty(&client->reads))
1275 mask |= POLLIN | POLLRDNORM;
1277 spin_unlock_irqrestore(&handle->dev->qmi.clients_lock, flags);
1282 int qc_register(struct qcusbnet *dev)
1289 cdev_init(&dev->qmi.cdev, &devqmi_fops);
1290 dev->qmi.cdev.owner = THIS_MODULE;
1293 result = client_alloc(dev, QMICTL);
1298 atomic_set(&dev->qmi.qmitid, 1);
1300 result = qc_startread(dev);
1306 if (!qmi_ready(dev, 30000)) {
1307 ERR("Device unresponsive to QMI\n");
1311 result = setup_wds_callback(dev);
1317 result = qmidms_getmeid(dev);
1323 result = alloc_chrdev_region(&devno, 0, 1, "qcqmi");
1327 result = cdev_add(&dev->qmi.cdev, devno, 1);
1329 ERR("error adding cdev\n");
1333 name = strstr(dev->usbnet->net->name, "qmi");
1335 ERR("Bad net name: %s\n", dev->usbnet->net->name);
1338 name += strlen("qmi");
1339 qmiidx = simple_strtoul(name, NULL, 10);
1341 ERR("Bad minor number\n");
1345 printk(KERN_INFO "creating qcqmi%d\n", qmiidx);
1346 device_create(dev->qmi.devclass, NULL, devno, NULL, "qcqmi%d", qmiidx);
1348 dev->qmi.devnum = devno;
1352 void qc_deregister(struct qcusbnet *dev)
1354 struct list_head *node;
1355 struct list_head *next;
1356 struct client *client;
1357 struct inode *inode;
1358 struct list_head *inodes;
1359 struct list_head *tasks;
1360 struct task_struct *task;
1361 struct fdtable *fdtable;
1363 unsigned long flags;
1366 if (!device_valid(dev)) {
1367 ERR("wrong device\n");
1371 list_for_each_safe(node, next, &dev->qmi.clients) {
1372 client = list_entry(node, struct client, node);
1373 DBG("release 0x%04X\n", client->cid);
1374 client_free(dev, client->cid);
1379 list_for_each(inodes, &dev->qmi.cdev.list) {
1380 inode = container_of(inodes, struct inode, i_devices);
1381 if (inode != NULL && !IS_ERR(inode)) {
1382 list_for_each(tasks, ¤t->group_leader->tasks) {
1383 task = container_of(tasks, struct task_struct, tasks);
1384 if (!task || !task->files)
1386 spin_lock_irqsave(&task->files->file_lock, flags);
1387 fdtable = files_fdtable(task->files);
1388 for (count = 0; count < fdtable->max_fds; count++) {
1389 file = fdtable->fd[count];
1390 if (file != NULL && file->f_dentry != NULL) {
1391 if (file->f_dentry->d_inode == inode) {
1392 rcu_assign_pointer(fdtable->fd[count], NULL);
1393 spin_unlock_irqrestore(&task->files->file_lock, flags);
1394 DBG("forcing close of open file handle\n");
1395 filp_close(file, task->files);
1396 spin_lock_irqsave(&task->files->file_lock, flags);
1400 spin_unlock_irqrestore(&task->files->file_lock, flags);
1405 if (!IS_ERR(dev->qmi.devclass))
1406 device_destroy(dev->qmi.devclass, dev->qmi.devnum);
1407 cdev_del(&dev->qmi.cdev);
1408 unregister_chrdev_region(dev->qmi.devnum, 1);
1411 static bool qmi_ready(struct qcusbnet *dev, u16 timeout)
1418 struct semaphore sem;
1420 unsigned long flags;
1423 if (!device_valid(dev)) {
1424 ERR("Invalid device\n");
1429 for (now = 0; now < timeout; now += 100) {
1432 tid = atomic_add_return(1, &dev->qmi.qmitid);
1434 tid = atomic_add_return(1, &dev->qmi.qmitid);
1436 wbuf = qmictl_new_ready(tid, &wbufsize);
1440 result = read_async(dev, QMICTL, tid, upsem, &sem);
1446 write_sync(dev, wbuf, wbufsize, QMICTL);
1449 if (!down_trylock(&sem)) {
1450 spin_lock_irqsave(&dev->qmi.clients_lock, flags);
1451 if (client_delread(dev, QMICTL, tid, &rbuf, &rbufsize)) {
1452 spin_unlock_irqrestore(&dev->qmi.clients_lock, flags);
1456 spin_unlock_irqrestore(&dev->qmi.clients_lock, flags);
1458 spin_lock_irqsave(&dev->qmi.clients_lock, flags);
1459 client_notify(dev, QMICTL, tid);
1460 spin_unlock_irqrestore(&dev->qmi.clients_lock, flags);
1469 DBG("QMI Ready after %u milliseconds\n", now);
1471 /* 3580 and newer doesn't need a delay; older needs 5000ms */
1472 if (qcusbnet2k_fwdelay)
1473 msleep(qcusbnet2k_fwdelay * 1000);
1478 static void wds_callback(struct qcusbnet *dev, u16 cid, void *data)
1485 struct net_device_stats *stats = &(dev->usbnet->net->stats);
1487 struct qmiwds_stats dstats = {
1494 .txbytesok = (u64)-1,
1495 .rxbytesok = (u64)-1,
1497 unsigned long flags;
1499 if (!device_valid(dev)) {
1500 ERR("Invalid device\n");
1504 spin_lock_irqsave(&dev->qmi.clients_lock, flags);
1505 ret = client_delread(dev, cid, 0, &rbuf, &rbufsize);
1506 spin_unlock_irqrestore(&dev->qmi.clients_lock, flags);
1509 ERR("WDS callback failed to get data\n");
1513 dstats.linkstate = !qc_isdown(dev, DOWN_NO_NDIS_CONNECTION);
1514 dstats.reconfigure = false;
1516 result = qmiwds_event_resp(rbuf, rbufsize, &dstats);
1518 ERR("bad WDS packet\n");
1520 if (dstats.txofl != (u32)-1)
1521 stats->tx_fifo_errors = dstats.txofl;
1523 if (dstats.rxofl != (u32)-1)
1524 stats->rx_fifo_errors = dstats.rxofl;
1526 if (dstats.txerr != (u32)-1)
1527 stats->tx_errors = dstats.txerr;
1529 if (dstats.rxerr != (u32)-1)
1530 stats->rx_errors = dstats.rxerr;
1532 if (dstats.txok != (u32)-1)
1533 stats->tx_packets = dstats.txok + stats->tx_errors;
1535 if (dstats.rxok != (u32)-1)
1536 stats->rx_packets = dstats.rxok + stats->rx_errors;
1538 if (dstats.txbytesok != (u64)-1)
1539 stats->tx_bytes = dstats.txbytesok;
1541 if (dstats.rxbytesok != (u64)-1)
1542 stats->rx_bytes = dstats.rxbytesok;
1544 if (dstats.reconfigure) {
1545 DBG("Net device link reset\n");
1546 qc_setdown(dev, DOWN_NO_NDIS_CONNECTION);
1547 qc_cleardown(dev, DOWN_NO_NDIS_CONNECTION);
1549 if (dstats.linkstate) {
1550 DBG("Net device link is connected\n");
1551 qc_cleardown(dev, DOWN_NO_NDIS_CONNECTION);
1553 DBG("Net device link is disconnected\n");
1554 qc_setdown(dev, DOWN_NO_NDIS_CONNECTION);
1561 result = read_async(dev, cid, 0, wds_callback, data);
1563 ERR("unable to setup next async read\n");
1566 static int setup_wds_callback(struct qcusbnet *dev)
1573 if (!device_valid(dev)) {
1574 ERR("Invalid device\n");
1578 result = client_alloc(dev, QMIWDS);
1583 buf = qmiwds_new_seteventreport(1, &size);
1587 result = write_sync(dev, buf, size, cid);
1593 buf = qmiwds_new_getpkgsrvcstatus(2, &size);
1597 result = write_sync(dev, buf, size, cid);
1603 result = read_async(dev, cid, 0, wds_callback, NULL);
1605 ERR("unable to setup async read\n");
1609 result = usb_control_msg(dev->usbnet->udev,
1610 usb_sndctrlpipe(dev->usbnet->udev, 0),
1612 dev->iface->cur_altsetting->desc.bInterfaceNumber,
1615 ERR("Bad SetControlLineState status %d\n", result);
1622 static int qmidms_getmeid(struct qcusbnet *dev)
1631 if (!device_valid(dev)) {
1632 ERR("Invalid device\n");
1636 result = client_alloc(dev, QMIDMS);
1641 wbuf = qmidms_new_getmeid(1, &wbufsize);
1645 result = write_sync(dev, wbuf, wbufsize, cid);
1651 result = read_sync(dev, &rbuf, cid, 1);
1656 result = qmidms_meid_resp(rbuf, rbufsize, &dev->meid[0], 14);
1660 ERR("bad get MEID resp\n");
1661 memset(&dev->meid[0], '0', 14);
1664 client_free(dev, cid);
1668 module_param(qcusbnet2k_fwdelay, int, S_IRUGO | S_IWUSR);
1669 MODULE_PARM_DESC(qcusbnet2k_fwdelay, "Delay for old firmware");