Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ft1000_usb.c
Go to the documentation of this file.
1 /*=====================================================
2  * CopyRight (C) 2007 Qualcomm Inc. All Rights Reserved.
3  *
4  *
5  * This file is part of Express Card USB Driver
6  *
7  * $Id:
8  *====================================================
9  */
10 #include <linux/init.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/usb.h>
14 #include <linux/netdevice.h>
15 #include <linux/etherdevice.h>
16 #include <linux/firmware.h>
17 #include "ft1000_usb.h"
18 
19 #include <linux/kthread.h>
20 
21 MODULE_DESCRIPTION("FT1000 EXPRESS CARD DRIVER");
22 MODULE_LICENSE("Dual MPL/GPL");
23 MODULE_SUPPORTED_DEVICE("QFT FT1000 Express Cards");
24 
25 void *pFileStart;
26 size_t FileLength;
27 
28 #define VENDOR_ID 0x1291 /* Qualcomm vendor id */
29 #define PRODUCT_ID 0x11 /* fake product id */
30 
31 /* table of devices that work with this driver */
32 static struct usb_device_id id_table[] = {
33  {USB_DEVICE(VENDOR_ID, PRODUCT_ID)},
34  {},
35 };
36 
37 MODULE_DEVICE_TABLE(usb, id_table);
38 
39 static bool gPollingfailed = FALSE;
40 static int ft1000_poll_thread(void *arg)
41 {
42  int ret;
43 
44  while (!kthread_should_stop()) {
45  msleep(10);
46  if (!gPollingfailed) {
47  ret = ft1000_poll(arg);
48  if (ret != STATUS_SUCCESS) {
49  DEBUG("ft1000_poll_thread: polling failed\n");
50  gPollingfailed = TRUE;
51  }
52  }
53  }
54  return STATUS_SUCCESS;
55 }
56 
57 static int ft1000_probe(struct usb_interface *interface,
58  const struct usb_device_id *id)
59 {
60  struct usb_host_interface *iface_desc;
62  struct usb_device *dev;
63  unsigned numaltsetting;
64  int i, ret = 0, size;
65 
66  struct ft1000_device *ft1000dev;
67  struct ft1000_info *pft1000info = NULL;
68  const struct firmware *dsp_fw;
69 
70  ft1000dev = kzalloc(sizeof(struct ft1000_device), GFP_KERNEL);
71 
72  if (!ft1000dev) {
73  pr_err("out of memory allocating device structure\n");
74  return -ENOMEM;
75  }
76 
77  dev = interface_to_usbdev(interface);
78  DEBUG("ft1000_probe: usb device descriptor info:\n");
79  DEBUG("ft1000_probe: number of configuration is %d\n",
80  dev->descriptor.bNumConfigurations);
81 
82  ft1000dev->dev = dev;
83  ft1000dev->status = 0;
84  ft1000dev->net = NULL;
85  ft1000dev->tx_urb = usb_alloc_urb(0, GFP_ATOMIC);
86  ft1000dev->rx_urb = usb_alloc_urb(0, GFP_ATOMIC);
87 
88  DEBUG("ft1000_probe is called\n");
89  numaltsetting = interface->num_altsetting;
90  DEBUG("ft1000_probe: number of alt settings is :%d\n", numaltsetting);
91  iface_desc = interface->cur_altsetting;
92  DEBUG("ft1000_probe: number of endpoints is %d\n",
93  iface_desc->desc.bNumEndpoints);
94  DEBUG("ft1000_probe: descriptor type is %d\n",
95  iface_desc->desc.bDescriptorType);
96  DEBUG("ft1000_probe: interface number is %d\n",
97  iface_desc->desc.bInterfaceNumber);
98  DEBUG("ft1000_probe: alternatesetting is %d\n",
99  iface_desc->desc.bAlternateSetting);
100  DEBUG("ft1000_probe: interface class is %d\n",
101  iface_desc->desc.bInterfaceClass);
102  DEBUG("ft1000_probe: control endpoint info:\n");
103  DEBUG("ft1000_probe: descriptor0 type -- %d\n",
104  iface_desc->endpoint[0].desc.bmAttributes);
105  DEBUG("ft1000_probe: descriptor1 type -- %d\n",
106  iface_desc->endpoint[1].desc.bmAttributes);
107  DEBUG("ft1000_probe: descriptor2 type -- %d\n",
108  iface_desc->endpoint[2].desc.bmAttributes);
109 
110  for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
111  endpoint =
112  (struct usb_endpoint_descriptor *)&iface_desc->
113  endpoint[i].desc;
114  DEBUG("endpoint %d\n", i);
115  DEBUG("bEndpointAddress=%x, bmAttributes=%x\n",
116  endpoint->bEndpointAddress, endpoint->bmAttributes);
117  if ((endpoint->bEndpointAddress & USB_DIR_IN)
118  && ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
120  ft1000dev->bulk_in_endpointAddr =
121  endpoint->bEndpointAddress;
122  DEBUG("ft1000_probe: in: %d\n",
123  endpoint->bEndpointAddress);
124  }
125 
126  if (!(endpoint->bEndpointAddress & USB_DIR_IN)
127  && ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
129  ft1000dev->bulk_out_endpointAddr =
130  endpoint->bEndpointAddress;
131  DEBUG("ft1000_probe: out: %d\n",
132  endpoint->bEndpointAddress);
133  }
134  }
135 
136  DEBUG("bulk_in=%d, bulk_out=%d\n", ft1000dev->bulk_in_endpointAddr,
137  ft1000dev->bulk_out_endpointAddr);
138 
139  ret = request_firmware(&dsp_fw, "ft3000.img", &dev->dev);
140  if (ret < 0) {
141  pr_err("Error request_firmware().\n");
142  goto err_fw;
143  }
144 
145  size = max_t(uint, dsp_fw->size, 4096);
147 
148  if (!pFileStart) {
149  release_firmware(dsp_fw);
150  ret = -ENOMEM;
151  goto err_fw;
152  }
153 
154  memcpy(pFileStart, dsp_fw->data, dsp_fw->size);
155  FileLength = dsp_fw->size;
156  release_firmware(dsp_fw);
157 
158  DEBUG("ft1000_probe: start downloading dsp image...\n");
159 
160  ret = init_ft1000_netdev(ft1000dev);
161  if (ret)
162  goto err_load;
163 
164  pft1000info = netdev_priv(ft1000dev->net);
165 
166  DEBUG("In probe: pft1000info=%p\n", pft1000info);
167  ret = dsp_reload(ft1000dev);
168  if (ret) {
169  pr_err("Problem with DSP image loading\n");
170  goto err_load;
171  }
172 
173  gPollingfailed = FALSE;
174  pft1000info->pPollThread =
175  kthread_run(ft1000_poll_thread, ft1000dev, "ft1000_poll");
176 
177  if (IS_ERR(pft1000info->pPollThread)) {
178  ret = PTR_ERR(pft1000info->pPollThread);
179  goto err_load;
180  }
181 
182  msleep(500);
183 
184  while (!pft1000info->CardReady) {
185  if (gPollingfailed) {
186  ret = -EIO;
187  goto err_thread;
188  }
189  msleep(100);
190  DEBUG("ft1000_probe::Waiting for Card Ready\n");
191  }
192 
193  DEBUG("ft1000_probe::Card Ready!!!! Registering network device\n");
194 
195  ret = reg_ft1000_netdev(ft1000dev, interface);
196  if (ret)
197  goto err_thread;
198 
199  ret = ft1000_init_proc(ft1000dev->net);
200  if (ret)
201  goto err_proc;
202 
203  pft1000info->NetDevRegDone = 1;
204 
205  return 0;
206 
207 err_proc:
208  unregister_netdev(ft1000dev->net);
209  free_netdev(ft1000dev->net);
210 err_thread:
211  kthread_stop(pft1000info->pPollThread);
212 err_load:
213  kfree(pFileStart);
214 err_fw:
215  kfree(ft1000dev);
216  return ret;
217 }
218 
219 static void ft1000_disconnect(struct usb_interface *interface)
220 {
221  struct ft1000_info *pft1000info;
222 
223  DEBUG("ft1000_disconnect is called\n");
224 
225  pft1000info = (struct ft1000_info *) usb_get_intfdata(interface);
226  DEBUG("In disconnect pft1000info=%p\n", pft1000info);
227 
228  if (pft1000info) {
229  ft1000_cleanup_proc(pft1000info);
230  if (pft1000info->pPollThread)
231  kthread_stop(pft1000info->pPollThread);
232 
233  DEBUG("ft1000_disconnect: threads are terminated\n");
234 
235  if (pft1000info->pFt1000Dev->net) {
236  DEBUG("ft1000_disconnect: destroy char driver\n");
237  ft1000_destroy_dev(pft1000info->pFt1000Dev->net);
238  unregister_netdev(pft1000info->pFt1000Dev->net);
239  DEBUG
240  ("ft1000_disconnect: network device unregistered\n");
241  free_netdev(pft1000info->pFt1000Dev->net);
242 
243  }
244 
245  usb_free_urb(pft1000info->pFt1000Dev->rx_urb);
246  usb_free_urb(pft1000info->pFt1000Dev->tx_urb);
247 
248  DEBUG("ft1000_disconnect: urb freed\n");
249 
250  kfree(pft1000info->pFt1000Dev);
251  }
252  kfree(pFileStart);
253 
254  return;
255 }
256 
257 static struct usb_driver ft1000_usb_driver = {
258  .name = "ft1000usb",
259  .probe = ft1000_probe,
260  .disconnect = ft1000_disconnect,
261  .id_table = id_table,
262 };
263 
264 module_usb_driver(ft1000_usb_driver);