blob: 8270197cffd050b2c3d39322e6075104e32fe3aa [file] [log] [blame]
Duy Truong790f06d2013-02-13 16:38:12 -08001/* Copyright (c) 2012, The Linux Foundation. All rights reserved.
Jack Phamccbbfab2012-04-09 19:50:20 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13/* add additional information to our printk's */
14#define pr_fmt(fmt) "%s: " fmt "\n", __func__
15
16#include <linux/kernel.h>
17#include <linux/errno.h>
18#include <linux/init.h>
19#include <linux/slab.h>
20#include <linux/module.h>
21#include <linux/kref.h>
22#include <linux/platform_device.h>
23#include <linux/uaccess.h>
24#include <linux/usb.h>
25#include <linux/debugfs.h>
26
27#include "hsic_sysmon.h"
28#include "sysmon.h"
29
30#define DRIVER_DESC "HSIC System monitor driver"
31
32enum hsic_sysmon_op {
33 HSIC_SYSMON_OP_READ = 0,
34 HSIC_SYSMON_OP_WRITE,
35 NUM_OPS
36};
37
38struct hsic_sysmon {
39 struct usb_device *udev;
40 struct usb_interface *ifc;
41 __u8 in_epaddr;
42 __u8 out_epaddr;
43 unsigned int pipe[NUM_OPS];
44 struct kref kref;
45 struct platform_device pdev;
46 int id;
47
48 /* debugging counters */
49 atomic_t dbg_bytecnt[NUM_OPS];
50 atomic_t dbg_pending[NUM_OPS];
51};
52static struct hsic_sysmon *hsic_sysmon_devices[NUM_HSIC_SYSMON_DEVS];
53
54static void hsic_sysmon_delete(struct kref *kref)
55{
56 struct hsic_sysmon *hs = container_of(kref, struct hsic_sysmon, kref);
57
58 usb_put_dev(hs->udev);
59 hsic_sysmon_devices[hs->id] = NULL;
60 kfree(hs);
61}
62
63/**
64 * hsic_sysmon_open() - Opens the system monitor bridge.
65 * @id: the HSIC system monitor device to open
66 *
67 * This should only be called after the platform_device "sys_mon" with id
68 * SYSMON_SS_EXT_MODEM has been added. The simplest way to do that is to
69 * register a platform_driver and its probe will be called when the HSIC
70 * device is ready.
71 */
72int hsic_sysmon_open(enum hsic_sysmon_device_id id)
73{
74 struct hsic_sysmon *hs;
75
76 if (id >= NUM_HSIC_SYSMON_DEVS) {
77 pr_err("invalid dev id(%d)", id);
78 return -ENODEV;
79 }
80
81 hs = hsic_sysmon_devices[id];
82 if (!hs) {
83 pr_err("dev is null");
84 return -ENODEV;
85 }
86
87 kref_get(&hs->kref);
88
89 return 0;
90}
91EXPORT_SYMBOL(hsic_sysmon_open);
92
93/**
94 * hsic_sysmon_close() - Closes the system monitor bridge.
95 * @id: the HSIC system monitor device to close
96 */
97void hsic_sysmon_close(enum hsic_sysmon_device_id id)
98{
99 struct hsic_sysmon *hs;
100
101 if (id >= NUM_HSIC_SYSMON_DEVS) {
102 pr_err("invalid dev id(%d)", id);
103 return;
104 }
105
106 hs = hsic_sysmon_devices[id];
107 kref_put(&hs->kref, hsic_sysmon_delete);
108}
109EXPORT_SYMBOL(hsic_sysmon_close);
110
111/**
112 * hsic_sysmon_readwrite() - Common function to send read/write over HSIC
113 */
114static int hsic_sysmon_readwrite(enum hsic_sysmon_device_id id, void *data,
115 size_t len, size_t *actual_len, int timeout,
116 enum hsic_sysmon_op op)
117{
118 struct hsic_sysmon *hs;
119 int ret;
120 const char *opstr = (op == HSIC_SYSMON_OP_READ) ?
121 "read" : "write";
122
123 pr_debug("%s: id:%d, data len:%d, timeout:%d", opstr, id, len, timeout);
124
125 if (id >= NUM_HSIC_SYSMON_DEVS) {
126 pr_err("invalid dev id(%d)", id);
127 return -ENODEV;
128 }
129
130 if (!len) {
131 pr_err("length(%d) must be greater than 0", len);
132 return -EINVAL;
133 }
134
135 hs = hsic_sysmon_devices[id];
136 if (!hs) {
137 pr_err("device was not opened");
138 return -ENODEV;
139 }
140
141 if (!hs->ifc) {
Jack Phame8741502012-06-13 17:34:07 -0700142 pr_err("can't %s, device disconnected", opstr);
Jack Phamccbbfab2012-04-09 19:50:20 -0700143 return -ENODEV;
144 }
145
146 ret = usb_autopm_get_interface(hs->ifc);
147 if (ret < 0) {
Jack Phame8741502012-06-13 17:34:07 -0700148 dev_err(&hs->ifc->dev, "can't %s, autopm_get failed:%d\n",
Jack Phamccbbfab2012-04-09 19:50:20 -0700149 opstr, ret);
150 return ret;
151 }
152
153 atomic_inc(&hs->dbg_pending[op]);
154
155 ret = usb_bulk_msg(hs->udev, hs->pipe[op], data, len, actual_len,
156 timeout);
157
158 atomic_dec(&hs->dbg_pending[op]);
159
160 if (ret)
Jack Phame8741502012-06-13 17:34:07 -0700161 dev_err(&hs->ifc->dev,
Jack Phamccbbfab2012-04-09 19:50:20 -0700162 "can't %s, usb_bulk_msg failed, err:%d\n", opstr, ret);
163 else
164 atomic_add(*actual_len, &hs->dbg_bytecnt[op]);
165
166 usb_autopm_put_interface(hs->ifc);
167 return ret;
168}
169
170/**
171 * hsic_sysmon_read() - Read data from the HSIC sysmon interface.
172 * @id: the HSIC system monitor device to open
173 * @data: pointer to caller-allocated buffer to fill in
174 * @len: length in bytes of the buffer
175 * @actual_len: pointer to a location to put the actual length read
176 * in bytes
177 * @timeout: time in msecs to wait for the message to complete before
178 * timing out (if 0 the wait is forever)
179 *
180 * Context: !in_interrupt ()
181 *
182 * Synchronously reads data from the HSIC interface. The call will return
183 * after the read has completed, encountered an error, or timed out. Upon
184 * successful return actual_len will reflect the number of bytes read.
185 *
186 * If successful, it returns 0, otherwise a negative error number. The number
187 * of actual bytes transferred will be stored in the actual_len paramater.
188 */
189int hsic_sysmon_read(enum hsic_sysmon_device_id id, char *data, size_t len,
190 size_t *actual_len, int timeout)
191{
192 return hsic_sysmon_readwrite(id, data, len, actual_len,
193 timeout, HSIC_SYSMON_OP_READ);
194}
195EXPORT_SYMBOL(hsic_sysmon_read);
196
197/**
198 * hsic_sysmon_write() - Write data to the HSIC sysmon interface.
199 * @id: the HSIC system monitor device to open
200 * @data: pointer to caller-allocated buffer to write
201 * @len: length in bytes of the data in buffer to write
202 * @actual_len: pointer to a location to put the actual length written
203 * in bytes
204 * @timeout: time in msecs to wait for the message to complete before
205 * timing out (if 0 the wait is forever)
206 *
207 * Context: !in_interrupt ()
208 *
209 * Synchronously writes data to the HSIC interface. The call will return
210 * after the write has completed, encountered an error, or timed out. Upon
211 * successful return actual_len will reflect the number of bytes written.
212 *
213 * If successful, it returns 0, otherwise a negative error number. The number
214 * of actual bytes transferred will be stored in the actual_len paramater.
215 */
216int hsic_sysmon_write(enum hsic_sysmon_device_id id, const char *data,
217 size_t len, int timeout)
218{
219 size_t actual_len;
220 return hsic_sysmon_readwrite(id, (void *)data, len, &actual_len,
221 timeout, HSIC_SYSMON_OP_WRITE);
222}
223EXPORT_SYMBOL(hsic_sysmon_write);
224
225#if defined(CONFIG_DEBUG_FS)
226#define DEBUG_BUF_SIZE 512
227static ssize_t sysmon_debug_read_stats(struct file *file, char __user *ubuf,
228 size_t count, loff_t *ppos)
229{
230 char *buf;
231 int i, ret = 0;
232
233 buf = kzalloc(sizeof(char) * DEBUG_BUF_SIZE, GFP_KERNEL);
234 if (!buf)
235 return -ENOMEM;
236
237 for (i = 0; i < NUM_HSIC_SYSMON_DEVS; i++) {
238 struct hsic_sysmon *hs = hsic_sysmon_devices[i];
239 if (!hs)
240 continue;
241
242 ret += scnprintf(buf, DEBUG_BUF_SIZE,
243 "---HSIC Sysmon #%d---\n"
244 "epin:%d, epout:%d\n"
245 "bytes to host: %d\n"
246 "bytes to mdm: %d\n"
247 "pending reads: %d\n"
248 "pending writes: %d\n",
249 i, hs->in_epaddr & ~0x80, hs->out_epaddr,
250 atomic_read(
251 &hs->dbg_bytecnt[HSIC_SYSMON_OP_READ]),
252 atomic_read(
253 &hs->dbg_bytecnt[HSIC_SYSMON_OP_WRITE]),
254 atomic_read(
255 &hs->dbg_pending[HSIC_SYSMON_OP_READ]),
256 atomic_read(
257 &hs->dbg_pending[HSIC_SYSMON_OP_WRITE])
258 );
259 }
260
261 ret = simple_read_from_buffer(ubuf, count, ppos, buf, ret);
262 kfree(buf);
263 return ret;
264}
265
266static ssize_t sysmon_debug_reset_stats(struct file *file,
267 const char __user *buf,
268 size_t count, loff_t *ppos)
269{
270 int i;
271
272 for (i = 0; i < NUM_HSIC_SYSMON_DEVS; i++) {
273 struct hsic_sysmon *hs = hsic_sysmon_devices[i];
274 if (hs) {
275 atomic_set(&hs->dbg_bytecnt[HSIC_SYSMON_OP_READ], 0);
276 atomic_set(&hs->dbg_bytecnt[HSIC_SYSMON_OP_WRITE], 0);
277 atomic_set(&hs->dbg_pending[HSIC_SYSMON_OP_READ], 0);
278 atomic_set(&hs->dbg_pending[HSIC_SYSMON_OP_WRITE], 0);
279 }
280 }
281
282 return count;
283}
284
285const struct file_operations sysmon_stats_ops = {
286 .read = sysmon_debug_read_stats,
287 .write = sysmon_debug_reset_stats,
288};
289
290static struct dentry *dent;
291
292static void hsic_sysmon_debugfs_init(void)
293{
294 struct dentry *dfile;
295
296 dent = debugfs_create_dir("hsic_sysmon", 0);
297 if (IS_ERR(dent))
298 return;
299
300 dfile = debugfs_create_file("status", 0444, dent, 0, &sysmon_stats_ops);
301 if (!dfile || IS_ERR(dfile))
302 debugfs_remove(dent);
303}
304
305static void hsic_sysmon_debugfs_cleanup(void)
306{
307 if (dent) {
308 debugfs_remove_recursive(dent);
309 dent = NULL;
310 }
311}
312#else
313static inline void hsic_sysmon_debugfs_init(void) { }
314static inline void hsic_sysmon_debugfs_cleanup(void) { }
315#endif
316
Jack Phamaf1c8132012-06-13 17:40:44 -0700317static void hsic_sysmon_pdev_release(struct device *dev) { }
318
Jack Phamccbbfab2012-04-09 19:50:20 -0700319static int
320hsic_sysmon_probe(struct usb_interface *ifc, const struct usb_device_id *id)
321{
322 struct hsic_sysmon *hs;
323 struct usb_host_interface *ifc_desc;
324 struct usb_endpoint_descriptor *ep_desc;
325 int i;
326 int ret = -ENOMEM;
327 __u8 ifc_num;
328
329 pr_debug("id:%lu", id->driver_info);
330
331 ifc_num = ifc->cur_altsetting->desc.bInterfaceNumber;
332
333 /* is this the interface we're looking for? */
334 if (ifc_num != id->driver_info)
335 return -ENODEV;
336
337 hs = kzalloc(sizeof(*hs), GFP_KERNEL);
338 if (!hs) {
339 pr_err("unable to allocate hsic_sysmon");
340 return -ENOMEM;
341 }
342
343 hs->udev = usb_get_dev(interface_to_usbdev(ifc));
344 hs->ifc = ifc;
345 kref_init(&hs->kref);
346
347 ifc_desc = ifc->cur_altsetting;
348 for (i = 0; i < ifc_desc->desc.bNumEndpoints; i++) {
349 ep_desc = &ifc_desc->endpoint[i].desc;
350
351 if (!hs->in_epaddr && usb_endpoint_is_bulk_in(ep_desc)) {
352 hs->in_epaddr = ep_desc->bEndpointAddress;
353 hs->pipe[HSIC_SYSMON_OP_READ] =
354 usb_rcvbulkpipe(hs->udev, hs->in_epaddr);
355 }
356
357 if (!hs->out_epaddr && usb_endpoint_is_bulk_out(ep_desc)) {
358 hs->out_epaddr = ep_desc->bEndpointAddress;
359 hs->pipe[HSIC_SYSMON_OP_WRITE] =
360 usb_sndbulkpipe(hs->udev, hs->out_epaddr);
361 }
362 }
363
364 if (!(hs->in_epaddr && hs->out_epaddr)) {
365 pr_err("could not find bulk in and bulk out endpoints");
366 ret = -ENODEV;
367 goto error;
368 }
369
370 hs->id = HSIC_SYSMON_DEV_EXT_MODEM;
371 hsic_sysmon_devices[HSIC_SYSMON_DEV_EXT_MODEM] = hs;
372 usb_set_intfdata(ifc, hs);
373
374 hs->pdev.name = "sys_mon";
375 hs->pdev.id = SYSMON_SS_EXT_MODEM;
Jack Phamaf1c8132012-06-13 17:40:44 -0700376 hs->pdev.dev.release = hsic_sysmon_pdev_release;
Jack Phamccbbfab2012-04-09 19:50:20 -0700377 platform_device_register(&hs->pdev);
378
379 pr_debug("complete");
380
381 return 0;
382
383error:
384 if (hs)
385 kref_put(&hs->kref, hsic_sysmon_delete);
386
387 return ret;
388}
389
390static void hsic_sysmon_disconnect(struct usb_interface *ifc)
391{
392 struct hsic_sysmon *hs = usb_get_intfdata(ifc);
393
394 platform_device_unregister(&hs->pdev);
395 kref_put(&hs->kref, hsic_sysmon_delete);
396 usb_set_intfdata(ifc, NULL);
397}
398
399static int hsic_sysmon_suspend(struct usb_interface *ifc, pm_message_t message)
400{
401 return 0;
402}
403
404static int hsic_sysmon_resume(struct usb_interface *ifc)
405{
406 return 0;
407}
408
409/* driver_info maps to the interface number corresponding to sysmon */
410static const struct usb_device_id hsic_sysmon_ids[] = {
411 { USB_DEVICE(0x5c6, 0x9048), .driver_info = 1, },
412 { USB_DEVICE(0x5c6, 0x904C), .driver_info = 1, },
Hemant Kumarce3c5bf2012-12-06 15:52:02 -0800413 { USB_DEVICE(0x5c6, 0x9075), .driver_info = 1, },
Jack Phamccbbfab2012-04-09 19:50:20 -0700414 {} /* terminating entry */
415};
416MODULE_DEVICE_TABLE(usb, hsic_sysmon_ids);
417
418static struct usb_driver hsic_sysmon_driver = {
419 .name = "hsic_sysmon",
420 .probe = hsic_sysmon_probe,
421 .disconnect = hsic_sysmon_disconnect,
422 .suspend = hsic_sysmon_suspend,
423 .resume = hsic_sysmon_resume,
424 .id_table = hsic_sysmon_ids,
425 .supports_autosuspend = 1,
426};
427
428static int __init hsic_sysmon_init(void)
429{
430 int ret;
431
432 ret = usb_register(&hsic_sysmon_driver);
433 if (ret) {
434 pr_err("unable to register " DRIVER_DESC);
435 return ret;
436 }
437
438 hsic_sysmon_debugfs_init();
439 return 0;
440}
441
442static void __exit hsic_sysmon_exit(void)
443{
444 hsic_sysmon_debugfs_cleanup();
445 usb_deregister(&hsic_sysmon_driver);
446}
447
448module_init(hsic_sysmon_init);
449module_exit(hsic_sysmon_exit);
450
451MODULE_DESCRIPTION(DRIVER_DESC);
452MODULE_LICENSE("GPL v2");