blob: 6d5544ae0cbfeb84e8b1b9e40bf340619ea67127 [file] [log] [blame]
Jack Phame32cf322011-09-26 10:20:17 -07001/*
Hemant Kumar7a067f12012-01-05 15:35:15 -08002 * Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved.
Jack Phame32cf322011-09-26 10:20:17 -07003 *
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.
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
Jack Pham76e61dd2012-06-14 18:48:53 -070014/* add additional information to our printk's */
15#define pr_fmt(fmt) "%s: " fmt "\n", __func__
16
Jack Phame32cf322011-09-26 10:20:17 -070017#include <linux/kernel.h>
18#include <linux/errno.h>
19#include <linux/init.h>
20#include <linux/slab.h>
21#include <linux/module.h>
22#include <linux/kref.h>
23#include <linux/platform_device.h>
Jack Phamcca03322012-06-14 19:00:31 -070024#include <linux/ratelimit.h>
Jack Phame32cf322011-09-26 10:20:17 -070025#include <linux/uaccess.h>
26#include <linux/usb.h>
Jack Pham9a08de42012-02-06 15:43:23 -080027#include <linux/debugfs.h>
Jack Phame32cf322011-09-26 10:20:17 -070028#include <mach/diag_bridge.h>
29
30#define DRIVER_DESC "USB host diag bridge driver"
Jack Phamf6ed5582011-11-02 16:08:31 -070031#define DRIVER_VERSION "1.0"
Jack Phame32cf322011-09-26 10:20:17 -070032
33struct diag_bridge {
34 struct usb_device *udev;
35 struct usb_interface *ifc;
36 struct usb_anchor submitted;
37 __u8 in_epAddr;
38 __u8 out_epAddr;
Jack Pham3ae820f2011-12-14 16:21:04 -080039 int err;
Jack Phame32cf322011-09-26 10:20:17 -070040 struct kref kref;
41 struct diag_bridge_ops *ops;
42 struct platform_device *pdev;
Jack Pham9a08de42012-02-06 15:43:23 -080043
44 /* debugging counters */
45 unsigned long bytes_to_host;
46 unsigned long bytes_to_mdm;
47 unsigned pending_reads;
48 unsigned pending_writes;
Jack Phame32cf322011-09-26 10:20:17 -070049};
50struct diag_bridge *__dev;
51
Jack Phamf6ed5582011-11-02 16:08:31 -070052int diag_bridge_open(struct diag_bridge_ops *ops)
Jack Phame32cf322011-09-26 10:20:17 -070053{
54 struct diag_bridge *dev = __dev;
55
56 if (!dev) {
Jack Pham76e61dd2012-06-14 18:48:53 -070057 pr_err("dev is null");
Jack Phame32cf322011-09-26 10:20:17 -070058 return -ENODEV;
59 }
60
61 dev->ops = ops;
Jack Pham3ae820f2011-12-14 16:21:04 -080062 dev->err = 0;
Jack Phame32cf322011-09-26 10:20:17 -070063
Jack Pham76e61dd2012-06-14 18:48:53 -070064 kref_get(&dev->kref);
65
Jack Phame32cf322011-09-26 10:20:17 -070066 return 0;
67}
Jack Phamf6ed5582011-11-02 16:08:31 -070068EXPORT_SYMBOL(diag_bridge_open);
Jack Phame32cf322011-09-26 10:20:17 -070069
Jack Pham76e61dd2012-06-14 18:48:53 -070070static void diag_bridge_delete(struct kref *kref)
71{
72 struct diag_bridge *dev = container_of(kref, struct diag_bridge, kref);
73
74 usb_put_dev(dev->udev);
75 __dev = 0;
76 kfree(dev);
77}
78
Jack Phamf6ed5582011-11-02 16:08:31 -070079void diag_bridge_close(void)
Jack Phame32cf322011-09-26 10:20:17 -070080{
81 struct diag_bridge *dev = __dev;
82
Jack Phame8741502012-06-13 17:34:07 -070083 dev_dbg(&dev->ifc->dev, "%s:\n", __func__);
Jack Phame32cf322011-09-26 10:20:17 -070084
85 usb_kill_anchored_urbs(&dev->submitted);
Jack Phame32cf322011-09-26 10:20:17 -070086 dev->ops = 0;
Jack Pham76e61dd2012-06-14 18:48:53 -070087 kref_put(&dev->kref, diag_bridge_delete);
Jack Phame32cf322011-09-26 10:20:17 -070088}
Jack Phamf6ed5582011-11-02 16:08:31 -070089EXPORT_SYMBOL(diag_bridge_close);
Jack Phame32cf322011-09-26 10:20:17 -070090
Jack Phamf6ed5582011-11-02 16:08:31 -070091static void diag_bridge_read_cb(struct urb *urb)
Jack Phame32cf322011-09-26 10:20:17 -070092{
93 struct diag_bridge *dev = urb->context;
94 struct diag_bridge_ops *cbs = dev->ops;
95
Jack Phame8741502012-06-13 17:34:07 -070096 dev_dbg(&dev->ifc->dev, "%s: status:%d actual:%d\n", __func__,
Jack Phame32cf322011-09-26 10:20:17 -070097 urb->status, urb->actual_length);
Jack Phame32cf322011-09-26 10:20:17 -070098
Jack Pham3ae820f2011-12-14 16:21:04 -080099 if (urb->status == -EPROTO) {
Jack Phame8741502012-06-13 17:34:07 -0700100 dev_err(&dev->ifc->dev, "%s: proto error\n", __func__);
Jack Phamcca03322012-06-14 19:00:31 -0700101 /* save error so that subsequent read/write returns ENODEV */
Jack Pham3ae820f2011-12-14 16:21:04 -0800102 dev->err = urb->status;
Jack Pham76e61dd2012-06-14 18:48:53 -0700103 kref_put(&dev->kref, diag_bridge_delete);
Jack Pham3ae820f2011-12-14 16:21:04 -0800104 return;
105 }
106
Vamsi Krishna90125642012-05-12 16:06:13 -0700107 if (cbs && cbs->read_complete_cb)
108 cbs->read_complete_cb(cbs->ctxt,
Jack Phame32cf322011-09-26 10:20:17 -0700109 urb->transfer_buffer,
110 urb->transfer_buffer_length,
Jack Phamf6ed5582011-11-02 16:08:31 -0700111 urb->status < 0 ? urb->status : urb->actual_length);
Jack Pham9a08de42012-02-06 15:43:23 -0800112
113 dev->bytes_to_host += urb->actual_length;
114 dev->pending_reads--;
Jack Pham76e61dd2012-06-14 18:48:53 -0700115 kref_put(&dev->kref, diag_bridge_delete);
Jack Phame32cf322011-09-26 10:20:17 -0700116}
117
Dixon Peterson32e70bb2011-12-16 13:26:45 -0800118int diag_bridge_read(char *data, int size)
Jack Phame32cf322011-09-26 10:20:17 -0700119{
120 struct urb *urb = NULL;
121 unsigned int pipe;
122 struct diag_bridge *dev = __dev;
123 int ret;
124
Jack Pham76e61dd2012-06-14 18:48:53 -0700125 pr_debug("reading %d bytes", size);
126
127 if (!dev || !dev->ifc) {
128 pr_err("device is disconnected");
129 return -ENODEV;
130 }
131
132 if (!dev->ops) {
133 pr_err("bridge is not open");
134 return -ENODEV;
135 }
Jack Phame32cf322011-09-26 10:20:17 -0700136
137 if (!size) {
Jack Phame8741502012-06-13 17:34:07 -0700138 dev_err(&dev->ifc->dev, "invalid size:%d\n", size);
Jack Phame32cf322011-09-26 10:20:17 -0700139 return -EINVAL;
140 }
141
Jack Pham3ae820f2011-12-14 16:21:04 -0800142 /* if there was a previous unrecoverable error, just quit */
143 if (dev->err)
Jack Phamcca03322012-06-14 19:00:31 -0700144 return -ENODEV;
Jack Pham3ae820f2011-12-14 16:21:04 -0800145
Jack Pham76e61dd2012-06-14 18:48:53 -0700146 kref_get(&dev->kref);
147
Jack Phamb60775a2012-02-14 17:57:41 -0800148 urb = usb_alloc_urb(0, GFP_KERNEL);
Jack Phame32cf322011-09-26 10:20:17 -0700149 if (!urb) {
Jack Phame8741502012-06-13 17:34:07 -0700150 dev_err(&dev->ifc->dev, "unable to allocate urb\n");
Jack Pham76e61dd2012-06-14 18:48:53 -0700151 ret = -ENOMEM;
152 goto error;
Jack Phame32cf322011-09-26 10:20:17 -0700153 }
154
Jack Phamb60775a2012-02-14 17:57:41 -0800155 ret = usb_autopm_get_interface(dev->ifc);
Jack Phamcca03322012-06-14 19:00:31 -0700156 if (ret < 0 && ret != -EAGAIN && ret != -EACCES) {
157 pr_err_ratelimited("read: autopm_get failed:%d", ret);
Jack Pham76e61dd2012-06-14 18:48:53 -0700158 goto free_error;
Jack Phamb60775a2012-02-14 17:57:41 -0800159 }
160
Jack Phame32cf322011-09-26 10:20:17 -0700161 pipe = usb_rcvbulkpipe(dev->udev, dev->in_epAddr);
162 usb_fill_bulk_urb(urb, dev->udev, pipe, data, size,
Jack Phamf6ed5582011-11-02 16:08:31 -0700163 diag_bridge_read_cb, dev);
Jack Phame32cf322011-09-26 10:20:17 -0700164 usb_anchor_urb(urb, &dev->submitted);
Jack Pham9a08de42012-02-06 15:43:23 -0800165 dev->pending_reads++;
Jack Phame32cf322011-09-26 10:20:17 -0700166
Jack Phamb60775a2012-02-14 17:57:41 -0800167 ret = usb_submit_urb(urb, GFP_KERNEL);
Jack Phame32cf322011-09-26 10:20:17 -0700168 if (ret) {
Jack Phamcca03322012-06-14 19:00:31 -0700169 pr_err_ratelimited("submitting urb failed err:%d", ret);
Jack Pham9a08de42012-02-06 15:43:23 -0800170 dev->pending_reads--;
Jack Phame32cf322011-09-26 10:20:17 -0700171 usb_unanchor_urb(urb);
Jack Phame32cf322011-09-26 10:20:17 -0700172 }
Jack Phamb60775a2012-02-14 17:57:41 -0800173 usb_autopm_put_interface(dev->ifc);
Jack Phame32cf322011-09-26 10:20:17 -0700174
Jack Pham76e61dd2012-06-14 18:48:53 -0700175free_error:
176 usb_free_urb(urb);
177error:
178 if (ret) /* otherwise this is done in the completion handler */
179 kref_put(&dev->kref, diag_bridge_delete);
180 return ret;
Jack Phame32cf322011-09-26 10:20:17 -0700181}
Jack Phamf6ed5582011-11-02 16:08:31 -0700182EXPORT_SYMBOL(diag_bridge_read);
Jack Phame32cf322011-09-26 10:20:17 -0700183
Jack Phamf6ed5582011-11-02 16:08:31 -0700184static void diag_bridge_write_cb(struct urb *urb)
Jack Phame32cf322011-09-26 10:20:17 -0700185{
186 struct diag_bridge *dev = urb->context;
187 struct diag_bridge_ops *cbs = dev->ops;
188
Jack Phame8741502012-06-13 17:34:07 -0700189 dev_dbg(&dev->ifc->dev, "%s:\n", __func__);
Jack Phame32cf322011-09-26 10:20:17 -0700190
Jack Phamb60775a2012-02-14 17:57:41 -0800191 usb_autopm_put_interface_async(dev->ifc);
192
Jack Pham3ae820f2011-12-14 16:21:04 -0800193 if (urb->status == -EPROTO) {
Jack Phame8741502012-06-13 17:34:07 -0700194 dev_err(&dev->ifc->dev, "%s: proto error\n", __func__);
Jack Phamcca03322012-06-14 19:00:31 -0700195 /* save error so that subsequent read/write returns ENODEV */
Jack Pham3ae820f2011-12-14 16:21:04 -0800196 dev->err = urb->status;
Jack Pham76e61dd2012-06-14 18:48:53 -0700197 kref_put(&dev->kref, diag_bridge_delete);
Jack Pham3ae820f2011-12-14 16:21:04 -0800198 return;
199 }
200
Vamsi Krishna90125642012-05-12 16:06:13 -0700201 if (cbs && cbs->write_complete_cb)
202 cbs->write_complete_cb(cbs->ctxt,
Jack Phame32cf322011-09-26 10:20:17 -0700203 urb->transfer_buffer,
204 urb->transfer_buffer_length,
Jack Phamf6ed5582011-11-02 16:08:31 -0700205 urb->status < 0 ? urb->status : urb->actual_length);
Jack Pham9a08de42012-02-06 15:43:23 -0800206
207 dev->bytes_to_mdm += urb->actual_length;
208 dev->pending_writes--;
Jack Pham76e61dd2012-06-14 18:48:53 -0700209 kref_put(&dev->kref, diag_bridge_delete);
Jack Phame32cf322011-09-26 10:20:17 -0700210}
211
Dixon Peterson32e70bb2011-12-16 13:26:45 -0800212int diag_bridge_write(char *data, int size)
Jack Phame32cf322011-09-26 10:20:17 -0700213{
214 struct urb *urb = NULL;
215 unsigned int pipe;
216 struct diag_bridge *dev = __dev;
217 int ret;
218
Jack Pham76e61dd2012-06-14 18:48:53 -0700219 pr_debug("writing %d bytes", size);
220
221 if (!dev || !dev->ifc) {
222 pr_err("device is disconnected");
223 return -ENODEV;
224 }
225
226 if (!dev->ops) {
227 pr_err("bridge is not open");
228 return -ENODEV;
229 }
Jack Phame32cf322011-09-26 10:20:17 -0700230
231 if (!size) {
Jack Phame8741502012-06-13 17:34:07 -0700232 dev_err(&dev->ifc->dev, "invalid size:%d\n", size);
Jack Phame32cf322011-09-26 10:20:17 -0700233 return -EINVAL;
234 }
235
Jack Pham3ae820f2011-12-14 16:21:04 -0800236 /* if there was a previous unrecoverable error, just quit */
237 if (dev->err)
Jack Phamcca03322012-06-14 19:00:31 -0700238 return -ENODEV;
Jack Pham3ae820f2011-12-14 16:21:04 -0800239
Jack Pham76e61dd2012-06-14 18:48:53 -0700240 kref_get(&dev->kref);
241
Jack Phamb60775a2012-02-14 17:57:41 -0800242 urb = usb_alloc_urb(0, GFP_KERNEL);
Jack Phame32cf322011-09-26 10:20:17 -0700243 if (!urb) {
Jack Phame8741502012-06-13 17:34:07 -0700244 dev_err(&dev->ifc->dev, "unable to allocate urb\n");
Jack Pham76e61dd2012-06-14 18:48:53 -0700245 ret = -ENOMEM;
246 goto error;
Jack Phame32cf322011-09-26 10:20:17 -0700247 }
248
Jack Phamb60775a2012-02-14 17:57:41 -0800249 ret = usb_autopm_get_interface(dev->ifc);
Jack Phamcca03322012-06-14 19:00:31 -0700250 if (ret < 0 && ret != -EAGAIN && ret != -EACCES) {
251 pr_err_ratelimited("write: autopm_get failed:%d", ret);
Jack Pham76e61dd2012-06-14 18:48:53 -0700252 goto free_error;
Jack Phamb60775a2012-02-14 17:57:41 -0800253 }
254
Jack Phame32cf322011-09-26 10:20:17 -0700255 pipe = usb_sndbulkpipe(dev->udev, dev->out_epAddr);
256 usb_fill_bulk_urb(urb, dev->udev, pipe, data, size,
Jack Phamf6ed5582011-11-02 16:08:31 -0700257 diag_bridge_write_cb, dev);
Jack Pham5a10c6f2012-06-26 11:41:28 -0700258 urb->transfer_flags |= URB_ZERO_PACKET;
Jack Phame32cf322011-09-26 10:20:17 -0700259 usb_anchor_urb(urb, &dev->submitted);
Jack Pham9a08de42012-02-06 15:43:23 -0800260 dev->pending_writes++;
Jack Phame32cf322011-09-26 10:20:17 -0700261
Jack Phamb60775a2012-02-14 17:57:41 -0800262 ret = usb_submit_urb(urb, GFP_KERNEL);
Jack Phame32cf322011-09-26 10:20:17 -0700263 if (ret) {
Jack Phamcca03322012-06-14 19:00:31 -0700264 pr_err_ratelimited("submitting urb failed err:%d", ret);
Jack Pham9a08de42012-02-06 15:43:23 -0800265 dev->pending_writes--;
Jack Phame32cf322011-09-26 10:20:17 -0700266 usb_unanchor_urb(urb);
Jack Phamb60775a2012-02-14 17:57:41 -0800267 usb_autopm_put_interface(dev->ifc);
Jack Pham76e61dd2012-06-14 18:48:53 -0700268 goto free_error;
Jack Phame32cf322011-09-26 10:20:17 -0700269 }
270
Jack Pham76e61dd2012-06-14 18:48:53 -0700271free_error:
Jack Phame32cf322011-09-26 10:20:17 -0700272 usb_free_urb(urb);
Jack Pham76e61dd2012-06-14 18:48:53 -0700273error:
274 if (ret) /* otherwise this is done in the completion handler */
275 kref_put(&dev->kref, diag_bridge_delete);
276 return ret;
Jack Phame32cf322011-09-26 10:20:17 -0700277}
Jack Phamf6ed5582011-11-02 16:08:31 -0700278EXPORT_SYMBOL(diag_bridge_write);
Jack Phame32cf322011-09-26 10:20:17 -0700279
Jack Pham9a08de42012-02-06 15:43:23 -0800280#if defined(CONFIG_DEBUG_FS)
281#define DEBUG_BUF_SIZE 512
282static ssize_t diag_read_stats(struct file *file, char __user *ubuf,
283 size_t count, loff_t *ppos)
284{
285 struct diag_bridge *dev = __dev;
286 char *buf;
287 int ret;
288
Jack Pham76e61dd2012-06-14 18:48:53 -0700289 if (!dev)
290 return -ENODEV;
291
Jack Pham9a08de42012-02-06 15:43:23 -0800292 buf = kzalloc(sizeof(char) * DEBUG_BUF_SIZE, GFP_KERNEL);
293 if (!buf)
294 return -ENOMEM;
295
296 ret = scnprintf(buf, DEBUG_BUF_SIZE,
297 "epin:%d, epout:%d\n"
298 "bytes to host: %lu\n"
299 "bytes to mdm: %lu\n"
300 "pending reads: %u\n"
301 "pending writes: %u\n"
302 "last error: %d\n",
303 dev->in_epAddr, dev->out_epAddr,
304 dev->bytes_to_host, dev->bytes_to_mdm,
305 dev->pending_reads, dev->pending_writes,
306 dev->err);
307
308 ret = simple_read_from_buffer(ubuf, count, ppos, buf, ret);
309 kfree(buf);
310 return ret;
311}
312
313static ssize_t diag_reset_stats(struct file *file, const char __user *buf,
314 size_t count, loff_t *ppos)
315{
316 struct diag_bridge *dev = __dev;
317
Jack Pham76e61dd2012-06-14 18:48:53 -0700318 if (dev) {
319 dev->bytes_to_host = dev->bytes_to_mdm = 0;
320 dev->pending_reads = dev->pending_writes = 0;
321 }
Jack Pham9a08de42012-02-06 15:43:23 -0800322
323 return count;
324}
325
326const struct file_operations diag_stats_ops = {
327 .read = diag_read_stats,
328 .write = diag_reset_stats,
329};
330
331static struct dentry *dent;
332
333static void diag_bridge_debugfs_init(void)
334{
335 struct dentry *dfile;
336
337 dent = debugfs_create_dir("diag_bridge", 0);
338 if (IS_ERR(dent))
339 return;
340
341 dfile = debugfs_create_file("status", 0444, dent, 0, &diag_stats_ops);
342 if (!dfile || IS_ERR(dfile))
343 debugfs_remove(dent);
344}
345
346static void diag_bridge_debugfs_cleanup(void)
347{
348 if (dent) {
349 debugfs_remove_recursive(dent);
350 dent = NULL;
351 }
352}
353#else
354static inline void diag_bridge_debugfs_init(void) { }
355static inline void diag_bridge_debugfs_cleanup(void) { }
356#endif
357
Jack Phame32cf322011-09-26 10:20:17 -0700358static int
Jack Phamf6ed5582011-11-02 16:08:31 -0700359diag_bridge_probe(struct usb_interface *ifc, const struct usb_device_id *id)
Jack Phame32cf322011-09-26 10:20:17 -0700360{
361 struct diag_bridge *dev;
362 struct usb_host_interface *ifc_desc;
363 struct usb_endpoint_descriptor *ep_desc;
364 int i;
365 int ret = -ENOMEM;
366 __u8 ifc_num;
367
Jack Pham76e61dd2012-06-14 18:48:53 -0700368 pr_debug("id:%lu", id->driver_info);
Jack Phame32cf322011-09-26 10:20:17 -0700369
370 ifc_num = ifc->cur_altsetting->desc.bInterfaceNumber;
371
372 /* is this interface supported ? */
373 if (ifc_num != id->driver_info)
374 return -ENODEV;
375
376 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
377 if (!dev) {
Jack Pham76e61dd2012-06-14 18:48:53 -0700378 pr_err("unable to allocate dev");
Jack Phame32cf322011-09-26 10:20:17 -0700379 return -ENOMEM;
380 }
381 dev->pdev = platform_device_alloc("diag_bridge", -1);
382 if (!dev->pdev) {
Jack Pham76e61dd2012-06-14 18:48:53 -0700383 pr_err("unable to allocate platform device");
Jack Phame32cf322011-09-26 10:20:17 -0700384 kfree(dev);
385 return -ENOMEM;
386 }
387 __dev = dev;
388
389 dev->udev = usb_get_dev(interface_to_usbdev(ifc));
390 dev->ifc = ifc;
391 kref_init(&dev->kref);
392 init_usb_anchor(&dev->submitted);
393
394 ifc_desc = ifc->cur_altsetting;
395 for (i = 0; i < ifc_desc->desc.bNumEndpoints; i++) {
396 ep_desc = &ifc_desc->endpoint[i].desc;
397
398 if (!dev->in_epAddr && usb_endpoint_is_bulk_in(ep_desc))
399 dev->in_epAddr = ep_desc->bEndpointAddress;
400
401 if (!dev->out_epAddr && usb_endpoint_is_bulk_out(ep_desc))
402 dev->out_epAddr = ep_desc->bEndpointAddress;
403 }
404
405 if (!(dev->in_epAddr && dev->out_epAddr)) {
Jack Pham76e61dd2012-06-14 18:48:53 -0700406 pr_err("could not find bulk in and bulk out endpoints");
Jack Phame32cf322011-09-26 10:20:17 -0700407 ret = -ENODEV;
408 goto error;
409 }
410
411 usb_set_intfdata(ifc, dev);
Jack Pham9a08de42012-02-06 15:43:23 -0800412 diag_bridge_debugfs_init();
Jack Phame32cf322011-09-26 10:20:17 -0700413 platform_device_add(dev->pdev);
414
Jack Phame8741502012-06-13 17:34:07 -0700415 dev_dbg(&dev->ifc->dev, "%s: complete\n", __func__);
Jack Phame32cf322011-09-26 10:20:17 -0700416
417 return 0;
418
419error:
420 if (dev)
Jack Phamf6ed5582011-11-02 16:08:31 -0700421 kref_put(&dev->kref, diag_bridge_delete);
Jack Phame32cf322011-09-26 10:20:17 -0700422
423 return ret;
424}
425
Jack Phamf6ed5582011-11-02 16:08:31 -0700426static void diag_bridge_disconnect(struct usb_interface *ifc)
Jack Phame32cf322011-09-26 10:20:17 -0700427{
428 struct diag_bridge *dev = usb_get_intfdata(ifc);
429
Jack Phame8741502012-06-13 17:34:07 -0700430 dev_dbg(&dev->ifc->dev, "%s:\n", __func__);
Jack Phame32cf322011-09-26 10:20:17 -0700431
Jack Pham1b1ba472012-06-13 16:40:15 -0700432 platform_device_unregister(dev->pdev);
Jack Pham76e61dd2012-06-14 18:48:53 -0700433 dev->ifc = NULL;
Jack Pham9a08de42012-02-06 15:43:23 -0800434 diag_bridge_debugfs_cleanup();
Jack Phamf6ed5582011-11-02 16:08:31 -0700435 kref_put(&dev->kref, diag_bridge_delete);
Jack Phame32cf322011-09-26 10:20:17 -0700436 usb_set_intfdata(ifc, NULL);
437}
438
Jack Phamb60775a2012-02-14 17:57:41 -0800439static int diag_bridge_suspend(struct usb_interface *ifc, pm_message_t message)
440{
441 struct diag_bridge *dev = usb_get_intfdata(ifc);
442 struct diag_bridge_ops *cbs = dev->ops;
443 int ret = 0;
444
445 if (cbs && cbs->suspend) {
446 ret = cbs->suspend(cbs->ctxt);
447 if (ret) {
Jack Phame8741502012-06-13 17:34:07 -0700448 dev_dbg(&dev->ifc->dev,
Jack Phamb60775a2012-02-14 17:57:41 -0800449 "%s: diag veto'd suspend\n", __func__);
450 return ret;
451 }
452
453 usb_kill_anchored_urbs(&dev->submitted);
454 }
455
456 return ret;
457}
458
459static int diag_bridge_resume(struct usb_interface *ifc)
460{
461 struct diag_bridge *dev = usb_get_intfdata(ifc);
462 struct diag_bridge_ops *cbs = dev->ops;
463
464
465 if (cbs && cbs->resume)
466 cbs->resume(cbs->ctxt);
467
468 return 0;
469}
Jack Phame32cf322011-09-26 10:20:17 -0700470
471#define VALID_INTERFACE_NUM 0
Jack Phamf6ed5582011-11-02 16:08:31 -0700472static const struct usb_device_id diag_bridge_ids[] = {
Hemant Kumar7a067f12012-01-05 15:35:15 -0800473 { USB_DEVICE(0x5c6, 0x9001),
474 .driver_info = VALID_INTERFACE_NUM, },
475 { USB_DEVICE(0x5c6, 0x9034),
476 .driver_info = VALID_INTERFACE_NUM, },
Hemant Kumar7f236832011-12-16 12:13:21 -0800477 { USB_DEVICE(0x5c6, 0x9048),
Jack Phame32cf322011-09-26 10:20:17 -0700478 .driver_info = VALID_INTERFACE_NUM, },
Hemant Kumarcfc0dad2012-02-21 14:34:39 -0800479 { USB_DEVICE(0x5c6, 0x904C),
480 .driver_info = VALID_INTERFACE_NUM, },
Jack Phame32cf322011-09-26 10:20:17 -0700481
482 {} /* terminating entry */
483};
Jack Phamf6ed5582011-11-02 16:08:31 -0700484MODULE_DEVICE_TABLE(usb, diag_bridge_ids);
Jack Phame32cf322011-09-26 10:20:17 -0700485
Jack Phamf6ed5582011-11-02 16:08:31 -0700486static struct usb_driver diag_bridge_driver = {
487 .name = "diag_bridge",
488 .probe = diag_bridge_probe,
489 .disconnect = diag_bridge_disconnect,
Jack Phamb60775a2012-02-14 17:57:41 -0800490 .suspend = diag_bridge_suspend,
491 .resume = diag_bridge_resume,
Jack Phamf6ed5582011-11-02 16:08:31 -0700492 .id_table = diag_bridge_ids,
Jack Phamb60775a2012-02-14 17:57:41 -0800493 .supports_autosuspend = 1,
Jack Phame32cf322011-09-26 10:20:17 -0700494};
495
Jack Phamf6ed5582011-11-02 16:08:31 -0700496static int __init diag_bridge_init(void)
Jack Phame32cf322011-09-26 10:20:17 -0700497{
498 int ret;
499
Jack Phamf6ed5582011-11-02 16:08:31 -0700500 ret = usb_register(&diag_bridge_driver);
Jack Phame32cf322011-09-26 10:20:17 -0700501 if (ret) {
Jack Pham76e61dd2012-06-14 18:48:53 -0700502 pr_err("unable to register diag driver");
Jack Phame32cf322011-09-26 10:20:17 -0700503 return ret;
504 }
505
506 return 0;
507}
508
Jack Phamf6ed5582011-11-02 16:08:31 -0700509static void __exit diag_bridge_exit(void)
Jack Phame32cf322011-09-26 10:20:17 -0700510{
Jack Phamf6ed5582011-11-02 16:08:31 -0700511 usb_deregister(&diag_bridge_driver);
Jack Phame32cf322011-09-26 10:20:17 -0700512}
513
Jack Phamf6ed5582011-11-02 16:08:31 -0700514module_init(diag_bridge_init);
515module_exit(diag_bridge_exit);
Jack Phame32cf322011-09-26 10:20:17 -0700516
517MODULE_DESCRIPTION(DRIVER_DESC);
Jack Phamf6ed5582011-11-02 16:08:31 -0700518MODULE_VERSION(DRIVER_VERSION);
519MODULE_LICENSE("GPL v2");