Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) ST-Ericsson AB 2010 |
| 3 | * Contact: Sjur Brendeland / sjur.brandeland@stericsson.com |
| 4 | * Author: Daniel Martensson / Daniel.Martensson@stericsson.com |
| 5 | * License terms: GNU General Public License (GPL) version 2. |
| 6 | */ |
| 7 | |
| 8 | #include <linux/version.h> |
| 9 | #include <linux/init.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/device.h> |
| 12 | #include <linux/platform_device.h> |
| 13 | #include <linux/string.h> |
| 14 | #include <linux/workqueue.h> |
| 15 | #include <linux/completion.h> |
| 16 | #include <linux/list.h> |
| 17 | #include <linux/interrupt.h> |
| 18 | #include <linux/dma-mapping.h> |
| 19 | #include <linux/delay.h> |
| 20 | #include <linux/sched.h> |
| 21 | #include <linux/debugfs.h> |
| 22 | #include <linux/if_arp.h> |
| 23 | #include <net/caif/caif_layer.h> |
| 24 | #include <net/caif/caif_spi.h> |
| 25 | |
| 26 | #ifndef CONFIG_CAIF_SPI_SYNC |
| 27 | #define FLAVOR "Flavour: Vanilla.\n" |
| 28 | #else |
| 29 | #define FLAVOR "Flavour: Master CMD&LEN at start.\n" |
| 30 | #endif /* CONFIG_CAIF_SPI_SYNC */ |
| 31 | |
| 32 | MODULE_LICENSE("GPL"); |
| 33 | MODULE_AUTHOR("Daniel Martensson<daniel.martensson@stericsson.com>"); |
| 34 | MODULE_DESCRIPTION("CAIF SPI driver"); |
| 35 | |
| 36 | static int spi_loop; |
| 37 | module_param(spi_loop, bool, S_IRUGO); |
| 38 | MODULE_PARM_DESC(spi_loop, "SPI running in loopback mode."); |
| 39 | |
| 40 | /* SPI frame alignment. */ |
| 41 | module_param(spi_frm_align, int, S_IRUGO); |
| 42 | MODULE_PARM_DESC(spi_frm_align, "SPI frame alignment."); |
| 43 | |
| 44 | /* SPI padding options. */ |
| 45 | module_param(spi_up_head_align, int, S_IRUGO); |
| 46 | MODULE_PARM_DESC(spi_up_head_align, "SPI uplink head alignment."); |
| 47 | |
| 48 | module_param(spi_up_tail_align, int, S_IRUGO); |
| 49 | MODULE_PARM_DESC(spi_up_tail_align, "SPI uplink tail alignment."); |
| 50 | |
| 51 | module_param(spi_down_head_align, int, S_IRUGO); |
| 52 | MODULE_PARM_DESC(spi_down_head_align, "SPI downlink head alignment."); |
| 53 | |
| 54 | module_param(spi_down_tail_align, int, S_IRUGO); |
| 55 | MODULE_PARM_DESC(spi_down_tail_align, "SPI downlink tail alignment."); |
| 56 | |
| 57 | #ifdef CONFIG_ARM |
| 58 | #define BYTE_HEX_FMT "%02X" |
| 59 | #else |
| 60 | #define BYTE_HEX_FMT "%02hhX" |
| 61 | #endif |
| 62 | |
| 63 | #define SPI_MAX_PAYLOAD_SIZE 4096 |
| 64 | /* |
| 65 | * Threshold values for the SPI packet queue. Flowcontrol will be asserted |
| 66 | * when the number of packets exceeds HIGH_WATER_MARK. It will not be |
| 67 | * deasserted before the number of packets drops below LOW_WATER_MARK. |
| 68 | */ |
| 69 | #define LOW_WATER_MARK 100 |
| 70 | #define HIGH_WATER_MARK (LOW_WATER_MARK*5) |
| 71 | |
| 72 | #ifdef CONFIG_UML |
| 73 | |
| 74 | /* |
| 75 | * We sometimes use UML for debugging, but it cannot handle |
| 76 | * dma_alloc_coherent so we have to wrap it. |
| 77 | */ |
| 78 | static inline void *dma_alloc(dma_addr_t *daddr) |
| 79 | { |
| 80 | return kmalloc(SPI_DMA_BUF_LEN, GFP_KERNEL); |
| 81 | } |
| 82 | |
| 83 | static inline void dma_free(void *cpu_addr, dma_addr_t handle) |
| 84 | { |
| 85 | kfree(cpu_addr); |
| 86 | } |
| 87 | |
| 88 | #else |
| 89 | |
| 90 | static inline void *dma_alloc(dma_addr_t *daddr) |
| 91 | { |
| 92 | return dma_alloc_coherent(NULL, SPI_DMA_BUF_LEN, daddr, |
| 93 | GFP_KERNEL); |
| 94 | } |
| 95 | |
| 96 | static inline void dma_free(void *cpu_addr, dma_addr_t handle) |
| 97 | { |
| 98 | dma_free_coherent(NULL, SPI_DMA_BUF_LEN, cpu_addr, handle); |
| 99 | } |
| 100 | #endif /* CONFIG_UML */ |
| 101 | |
| 102 | #ifdef CONFIG_DEBUG_FS |
| 103 | |
| 104 | #define DEBUGFS_BUF_SIZE 4096 |
| 105 | |
| 106 | static struct dentry *dbgfs_root; |
| 107 | |
| 108 | static inline void driver_debugfs_create(void) |
| 109 | { |
| 110 | dbgfs_root = debugfs_create_dir(cfspi_spi_driver.driver.name, NULL); |
| 111 | } |
| 112 | |
| 113 | static inline void driver_debugfs_remove(void) |
| 114 | { |
| 115 | debugfs_remove(dbgfs_root); |
| 116 | } |
| 117 | |
| 118 | static inline void dev_debugfs_rem(struct cfspi *cfspi) |
| 119 | { |
| 120 | debugfs_remove(cfspi->dbgfs_frame); |
| 121 | debugfs_remove(cfspi->dbgfs_state); |
| 122 | debugfs_remove(cfspi->dbgfs_dir); |
| 123 | } |
| 124 | |
| 125 | static int dbgfs_open(struct inode *inode, struct file *file) |
| 126 | { |
| 127 | file->private_data = inode->i_private; |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | static ssize_t dbgfs_state(struct file *file, char __user *user_buf, |
| 132 | size_t count, loff_t *ppos) |
| 133 | { |
| 134 | char *buf; |
| 135 | int len = 0; |
| 136 | ssize_t size; |
Joe Perches | cb8b6a9 | 2010-07-12 10:50:05 +0000 | [diff] [blame] | 137 | struct cfspi *cfspi = file->private_data; |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 138 | |
| 139 | buf = kzalloc(DEBUGFS_BUF_SIZE, GFP_KERNEL); |
| 140 | if (!buf) |
| 141 | return 0; |
| 142 | |
| 143 | /* Print out debug information. */ |
| 144 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), |
| 145 | "CAIF SPI debug information:\n"); |
| 146 | |
| 147 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), FLAVOR); |
| 148 | |
| 149 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), |
| 150 | "STATE: %d\n", cfspi->dbg_state); |
| 151 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), |
| 152 | "Previous CMD: 0x%x\n", cfspi->pcmd); |
| 153 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), |
| 154 | "Current CMD: 0x%x\n", cfspi->cmd); |
| 155 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), |
| 156 | "Previous TX len: %d\n", cfspi->tx_ppck_len); |
| 157 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), |
| 158 | "Previous RX len: %d\n", cfspi->rx_ppck_len); |
| 159 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), |
| 160 | "Current TX len: %d\n", cfspi->tx_cpck_len); |
| 161 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), |
| 162 | "Current RX len: %d\n", cfspi->rx_cpck_len); |
| 163 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), |
| 164 | "Next TX len: %d\n", cfspi->tx_npck_len); |
| 165 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), |
| 166 | "Next RX len: %d\n", cfspi->rx_npck_len); |
| 167 | |
Dan Carpenter | 7b7b0b9 | 2010-07-25 21:23:59 +0000 | [diff] [blame] | 168 | if (len > DEBUGFS_BUF_SIZE) |
| 169 | len = DEBUGFS_BUF_SIZE; |
| 170 | |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 171 | size = simple_read_from_buffer(user_buf, count, ppos, buf, len); |
| 172 | kfree(buf); |
| 173 | |
| 174 | return size; |
| 175 | } |
| 176 | |
| 177 | static ssize_t print_frame(char *buf, size_t size, char *frm, |
| 178 | size_t count, size_t cut) |
| 179 | { |
| 180 | int len = 0; |
| 181 | int i; |
| 182 | for (i = 0; i < count; i++) { |
| 183 | len += snprintf((buf + len), (size - len), |
| 184 | "[0x" BYTE_HEX_FMT "]", |
| 185 | frm[i]); |
| 186 | if ((i == cut) && (count > (cut * 2))) { |
| 187 | /* Fast forward. */ |
| 188 | i = count - cut; |
| 189 | len += snprintf((buf + len), (size - len), |
| 190 | "--- %u bytes skipped ---\n", |
| 191 | (int)(count - (cut * 2))); |
| 192 | } |
| 193 | |
| 194 | if ((!(i % 10)) && i) { |
| 195 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), |
| 196 | "\n"); |
| 197 | } |
| 198 | } |
| 199 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), "\n"); |
| 200 | return len; |
| 201 | } |
| 202 | |
| 203 | static ssize_t dbgfs_frame(struct file *file, char __user *user_buf, |
| 204 | size_t count, loff_t *ppos) |
| 205 | { |
| 206 | char *buf; |
| 207 | int len = 0; |
| 208 | ssize_t size; |
| 209 | struct cfspi *cfspi; |
| 210 | |
Joe Perches | cb8b6a9 | 2010-07-12 10:50:05 +0000 | [diff] [blame] | 211 | cfspi = file->private_data; |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 212 | buf = kzalloc(DEBUGFS_BUF_SIZE, GFP_KERNEL); |
| 213 | if (!buf) |
| 214 | return 0; |
| 215 | |
| 216 | /* Print out debug information. */ |
| 217 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), |
| 218 | "Current frame:\n"); |
| 219 | |
| 220 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), |
| 221 | "Tx data (Len: %d):\n", cfspi->tx_cpck_len); |
| 222 | |
| 223 | len += print_frame((buf + len), (DEBUGFS_BUF_SIZE - len), |
| 224 | cfspi->xfer.va_tx, |
| 225 | (cfspi->tx_cpck_len + SPI_CMD_SZ), 100); |
| 226 | |
| 227 | len += snprintf((buf + len), (DEBUGFS_BUF_SIZE - len), |
| 228 | "Rx data (Len: %d):\n", cfspi->rx_cpck_len); |
| 229 | |
| 230 | len += print_frame((buf + len), (DEBUGFS_BUF_SIZE - len), |
| 231 | cfspi->xfer.va_rx, |
| 232 | (cfspi->rx_cpck_len + SPI_CMD_SZ), 100); |
| 233 | |
| 234 | size = simple_read_from_buffer(user_buf, count, ppos, buf, len); |
| 235 | kfree(buf); |
| 236 | |
| 237 | return size; |
| 238 | } |
| 239 | |
| 240 | static const struct file_operations dbgfs_state_fops = { |
| 241 | .open = dbgfs_open, |
| 242 | .read = dbgfs_state, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 243 | .owner = THIS_MODULE, |
| 244 | .llseek = default_llseek, |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 245 | }; |
| 246 | |
| 247 | static const struct file_operations dbgfs_frame_fops = { |
| 248 | .open = dbgfs_open, |
| 249 | .read = dbgfs_frame, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 250 | .owner = THIS_MODULE, |
| 251 | .llseek = default_llseek, |
Sjur Braendeland | 529d6da | 2010-06-29 00:08:21 -0700 | [diff] [blame] | 252 | }; |
| 253 | |
| 254 | static inline void dev_debugfs_add(struct cfspi *cfspi) |
| 255 | { |
| 256 | cfspi->dbgfs_dir = debugfs_create_dir(cfspi->pdev->name, dbgfs_root); |
| 257 | cfspi->dbgfs_state = debugfs_create_file("state", S_IRUGO, |
| 258 | cfspi->dbgfs_dir, cfspi, |
| 259 | &dbgfs_state_fops); |
| 260 | cfspi->dbgfs_frame = debugfs_create_file("frame", S_IRUGO, |
| 261 | cfspi->dbgfs_dir, cfspi, |
| 262 | &dbgfs_frame_fops); |
| 263 | } |
| 264 | |
| 265 | inline void cfspi_dbg_state(struct cfspi *cfspi, int state) |
| 266 | { |
| 267 | cfspi->dbg_state = state; |
| 268 | }; |
| 269 | #else |
| 270 | |
| 271 | static inline void driver_debugfs_create(void) |
| 272 | { |
| 273 | } |
| 274 | |
| 275 | static inline void driver_debugfs_remove(void) |
| 276 | { |
| 277 | } |
| 278 | |
| 279 | static inline void dev_debugfs_add(struct cfspi *cfspi) |
| 280 | { |
| 281 | } |
| 282 | |
| 283 | static inline void dev_debugfs_rem(struct cfspi *cfspi) |
| 284 | { |
| 285 | } |
| 286 | |
| 287 | inline void cfspi_dbg_state(struct cfspi *cfspi, int state) |
| 288 | { |
| 289 | } |
| 290 | #endif /* CONFIG_DEBUG_FS */ |
| 291 | |
| 292 | static LIST_HEAD(cfspi_list); |
| 293 | static spinlock_t cfspi_list_lock; |
| 294 | |
| 295 | /* SPI uplink head alignment. */ |
| 296 | static ssize_t show_up_head_align(struct device_driver *driver, char *buf) |
| 297 | { |
| 298 | return sprintf(buf, "%d\n", spi_up_head_align); |
| 299 | } |
| 300 | |
| 301 | static DRIVER_ATTR(up_head_align, S_IRUSR, show_up_head_align, NULL); |
| 302 | |
| 303 | /* SPI uplink tail alignment. */ |
| 304 | static ssize_t show_up_tail_align(struct device_driver *driver, char *buf) |
| 305 | { |
| 306 | return sprintf(buf, "%d\n", spi_up_tail_align); |
| 307 | } |
| 308 | |
| 309 | static DRIVER_ATTR(up_tail_align, S_IRUSR, show_up_tail_align, NULL); |
| 310 | |
| 311 | /* SPI downlink head alignment. */ |
| 312 | static ssize_t show_down_head_align(struct device_driver *driver, char *buf) |
| 313 | { |
| 314 | return sprintf(buf, "%d\n", spi_down_head_align); |
| 315 | } |
| 316 | |
| 317 | static DRIVER_ATTR(down_head_align, S_IRUSR, show_down_head_align, NULL); |
| 318 | |
| 319 | /* SPI downlink tail alignment. */ |
| 320 | static ssize_t show_down_tail_align(struct device_driver *driver, char *buf) |
| 321 | { |
| 322 | return sprintf(buf, "%d\n", spi_down_tail_align); |
| 323 | } |
| 324 | |
| 325 | static DRIVER_ATTR(down_tail_align, S_IRUSR, show_down_tail_align, NULL); |
| 326 | |
| 327 | /* SPI frame alignment. */ |
| 328 | static ssize_t show_frame_align(struct device_driver *driver, char *buf) |
| 329 | { |
| 330 | return sprintf(buf, "%d\n", spi_frm_align); |
| 331 | } |
| 332 | |
| 333 | static DRIVER_ATTR(frame_align, S_IRUSR, show_frame_align, NULL); |
| 334 | |
| 335 | int cfspi_xmitfrm(struct cfspi *cfspi, u8 *buf, size_t len) |
| 336 | { |
| 337 | u8 *dst = buf; |
| 338 | caif_assert(buf); |
| 339 | |
| 340 | do { |
| 341 | struct sk_buff *skb; |
| 342 | struct caif_payload_info *info; |
| 343 | int spad = 0; |
| 344 | int epad; |
| 345 | |
| 346 | skb = skb_dequeue(&cfspi->chead); |
| 347 | if (!skb) |
| 348 | break; |
| 349 | |
| 350 | /* |
| 351 | * Calculate length of frame including SPI padding. |
| 352 | * The payload position is found in the control buffer. |
| 353 | */ |
| 354 | info = (struct caif_payload_info *)&skb->cb; |
| 355 | |
| 356 | /* |
| 357 | * Compute head offset i.e. number of bytes to add to |
| 358 | * get the start of the payload aligned. |
| 359 | */ |
| 360 | if (spi_up_head_align) { |
| 361 | spad = 1 + ((info->hdr_len + 1) & spi_up_head_align); |
| 362 | *dst = (u8)(spad - 1); |
| 363 | dst += spad; |
| 364 | } |
| 365 | |
| 366 | /* Copy in CAIF frame. */ |
| 367 | skb_copy_bits(skb, 0, dst, skb->len); |
| 368 | dst += skb->len; |
| 369 | cfspi->ndev->stats.tx_packets++; |
| 370 | cfspi->ndev->stats.tx_bytes += skb->len; |
| 371 | |
| 372 | /* |
| 373 | * Compute tail offset i.e. number of bytes to add to |
| 374 | * get the complete CAIF frame aligned. |
| 375 | */ |
| 376 | epad = (skb->len + spad) & spi_up_tail_align; |
| 377 | dst += epad; |
| 378 | |
| 379 | dev_kfree_skb(skb); |
| 380 | |
| 381 | } while ((dst - buf) < len); |
| 382 | |
| 383 | return dst - buf; |
| 384 | } |
| 385 | |
| 386 | int cfspi_xmitlen(struct cfspi *cfspi) |
| 387 | { |
| 388 | struct sk_buff *skb = NULL; |
| 389 | int frm_len = 0; |
| 390 | int pkts = 0; |
| 391 | |
| 392 | /* |
| 393 | * Decommit previously commited frames. |
| 394 | * skb_queue_splice_tail(&cfspi->chead,&cfspi->qhead) |
| 395 | */ |
| 396 | while (skb_peek(&cfspi->chead)) { |
| 397 | skb = skb_dequeue_tail(&cfspi->chead); |
| 398 | skb_queue_head(&cfspi->qhead, skb); |
| 399 | } |
| 400 | |
| 401 | do { |
| 402 | struct caif_payload_info *info = NULL; |
| 403 | int spad = 0; |
| 404 | int epad = 0; |
| 405 | |
| 406 | skb = skb_dequeue(&cfspi->qhead); |
| 407 | if (!skb) |
| 408 | break; |
| 409 | |
| 410 | /* |
| 411 | * Calculate length of frame including SPI padding. |
| 412 | * The payload position is found in the control buffer. |
| 413 | */ |
| 414 | info = (struct caif_payload_info *)&skb->cb; |
| 415 | |
| 416 | /* |
| 417 | * Compute head offset i.e. number of bytes to add to |
| 418 | * get the start of the payload aligned. |
| 419 | */ |
| 420 | if (spi_up_head_align) |
| 421 | spad = 1 + ((info->hdr_len + 1) & spi_up_head_align); |
| 422 | |
| 423 | /* |
| 424 | * Compute tail offset i.e. number of bytes to add to |
| 425 | * get the complete CAIF frame aligned. |
| 426 | */ |
| 427 | epad = (skb->len + spad) & spi_up_tail_align; |
| 428 | |
| 429 | if ((skb->len + spad + epad + frm_len) <= CAIF_MAX_SPI_FRAME) { |
| 430 | skb_queue_tail(&cfspi->chead, skb); |
| 431 | pkts++; |
| 432 | frm_len += skb->len + spad + epad; |
| 433 | } else { |
| 434 | /* Put back packet. */ |
| 435 | skb_queue_head(&cfspi->qhead, skb); |
| 436 | } |
| 437 | } while (pkts <= CAIF_MAX_SPI_PKTS); |
| 438 | |
| 439 | /* |
| 440 | * Send flow on if previously sent flow off |
| 441 | * and now go below the low water mark |
| 442 | */ |
| 443 | if (cfspi->flow_off_sent && cfspi->qhead.qlen < cfspi->qd_low_mark && |
| 444 | cfspi->cfdev.flowctrl) { |
| 445 | cfspi->flow_off_sent = 0; |
| 446 | cfspi->cfdev.flowctrl(cfspi->ndev, 1); |
| 447 | } |
| 448 | |
| 449 | return frm_len; |
| 450 | } |
| 451 | |
| 452 | static void cfspi_ss_cb(bool assert, struct cfspi_ifc *ifc) |
| 453 | { |
| 454 | struct cfspi *cfspi = (struct cfspi *)ifc->priv; |
| 455 | |
| 456 | if (!in_interrupt()) |
| 457 | spin_lock(&cfspi->lock); |
| 458 | if (assert) { |
| 459 | set_bit(SPI_SS_ON, &cfspi->state); |
| 460 | set_bit(SPI_XFER, &cfspi->state); |
| 461 | } else { |
| 462 | set_bit(SPI_SS_OFF, &cfspi->state); |
| 463 | } |
| 464 | if (!in_interrupt()) |
| 465 | spin_unlock(&cfspi->lock); |
| 466 | |
| 467 | /* Wake up the xfer thread. */ |
| 468 | wake_up_interruptible(&cfspi->wait); |
| 469 | } |
| 470 | |
| 471 | static void cfspi_xfer_done_cb(struct cfspi_ifc *ifc) |
| 472 | { |
| 473 | struct cfspi *cfspi = (struct cfspi *)ifc->priv; |
| 474 | |
| 475 | /* Transfer done, complete work queue */ |
| 476 | complete(&cfspi->comp); |
| 477 | } |
| 478 | |
| 479 | static int cfspi_xmit(struct sk_buff *skb, struct net_device *dev) |
| 480 | { |
| 481 | struct cfspi *cfspi = NULL; |
| 482 | unsigned long flags; |
| 483 | if (!dev) |
| 484 | return -EINVAL; |
| 485 | |
| 486 | cfspi = netdev_priv(dev); |
| 487 | |
| 488 | skb_queue_tail(&cfspi->qhead, skb); |
| 489 | |
| 490 | spin_lock_irqsave(&cfspi->lock, flags); |
| 491 | if (!test_and_set_bit(SPI_XFER, &cfspi->state)) { |
| 492 | /* Wake up xfer thread. */ |
| 493 | wake_up_interruptible(&cfspi->wait); |
| 494 | } |
| 495 | spin_unlock_irqrestore(&cfspi->lock, flags); |
| 496 | |
| 497 | /* Send flow off if number of bytes is above high water mark */ |
| 498 | if (!cfspi->flow_off_sent && |
| 499 | cfspi->qhead.qlen > cfspi->qd_high_mark && |
| 500 | cfspi->cfdev.flowctrl) { |
| 501 | cfspi->flow_off_sent = 1; |
| 502 | cfspi->cfdev.flowctrl(cfspi->ndev, 0); |
| 503 | } |
| 504 | |
| 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | int cfspi_rxfrm(struct cfspi *cfspi, u8 *buf, size_t len) |
| 509 | { |
| 510 | u8 *src = buf; |
| 511 | |
| 512 | caif_assert(buf != NULL); |
| 513 | |
| 514 | do { |
| 515 | int res; |
| 516 | struct sk_buff *skb = NULL; |
| 517 | int spad = 0; |
| 518 | int epad = 0; |
| 519 | u8 *dst = NULL; |
| 520 | int pkt_len = 0; |
| 521 | |
| 522 | /* |
| 523 | * Compute head offset i.e. number of bytes added to |
| 524 | * get the start of the payload aligned. |
| 525 | */ |
| 526 | if (spi_down_head_align) { |
| 527 | spad = 1 + *src; |
| 528 | src += spad; |
| 529 | } |
| 530 | |
| 531 | /* Read length of CAIF frame (little endian). */ |
| 532 | pkt_len = *src; |
| 533 | pkt_len |= ((*(src+1)) << 8) & 0xFF00; |
| 534 | pkt_len += 2; /* Add FCS fields. */ |
| 535 | |
| 536 | /* Get a suitable caif packet and copy in data. */ |
| 537 | |
| 538 | skb = netdev_alloc_skb(cfspi->ndev, pkt_len + 1); |
| 539 | caif_assert(skb != NULL); |
| 540 | |
| 541 | dst = skb_put(skb, pkt_len); |
| 542 | memcpy(dst, src, pkt_len); |
| 543 | src += pkt_len; |
| 544 | |
| 545 | skb->protocol = htons(ETH_P_CAIF); |
| 546 | skb_reset_mac_header(skb); |
| 547 | skb->dev = cfspi->ndev; |
| 548 | |
| 549 | /* |
| 550 | * Push received packet up the stack. |
| 551 | */ |
| 552 | if (!spi_loop) |
| 553 | res = netif_rx_ni(skb); |
| 554 | else |
| 555 | res = cfspi_xmit(skb, cfspi->ndev); |
| 556 | |
| 557 | if (!res) { |
| 558 | cfspi->ndev->stats.rx_packets++; |
| 559 | cfspi->ndev->stats.rx_bytes += pkt_len; |
| 560 | } else |
| 561 | cfspi->ndev->stats.rx_dropped++; |
| 562 | |
| 563 | /* |
| 564 | * Compute tail offset i.e. number of bytes added to |
| 565 | * get the complete CAIF frame aligned. |
| 566 | */ |
| 567 | epad = (pkt_len + spad) & spi_down_tail_align; |
| 568 | src += epad; |
| 569 | } while ((src - buf) < len); |
| 570 | |
| 571 | return src - buf; |
| 572 | } |
| 573 | |
| 574 | static int cfspi_open(struct net_device *dev) |
| 575 | { |
| 576 | netif_wake_queue(dev); |
| 577 | return 0; |
| 578 | } |
| 579 | |
| 580 | static int cfspi_close(struct net_device *dev) |
| 581 | { |
| 582 | netif_stop_queue(dev); |
| 583 | return 0; |
| 584 | } |
| 585 | static const struct net_device_ops cfspi_ops = { |
| 586 | .ndo_open = cfspi_open, |
| 587 | .ndo_stop = cfspi_close, |
| 588 | .ndo_start_xmit = cfspi_xmit |
| 589 | }; |
| 590 | |
| 591 | static void cfspi_setup(struct net_device *dev) |
| 592 | { |
| 593 | struct cfspi *cfspi = netdev_priv(dev); |
| 594 | dev->features = 0; |
| 595 | dev->netdev_ops = &cfspi_ops; |
| 596 | dev->type = ARPHRD_CAIF; |
| 597 | dev->flags = IFF_NOARP | IFF_POINTOPOINT; |
| 598 | dev->tx_queue_len = 0; |
| 599 | dev->mtu = SPI_MAX_PAYLOAD_SIZE; |
| 600 | dev->destructor = free_netdev; |
| 601 | skb_queue_head_init(&cfspi->qhead); |
| 602 | skb_queue_head_init(&cfspi->chead); |
| 603 | cfspi->cfdev.link_select = CAIF_LINK_HIGH_BANDW; |
| 604 | cfspi->cfdev.use_frag = false; |
| 605 | cfspi->cfdev.use_stx = false; |
| 606 | cfspi->cfdev.use_fcs = false; |
| 607 | cfspi->ndev = dev; |
| 608 | } |
| 609 | |
| 610 | int cfspi_spi_probe(struct platform_device *pdev) |
| 611 | { |
| 612 | struct cfspi *cfspi = NULL; |
| 613 | struct net_device *ndev; |
| 614 | struct cfspi_dev *dev; |
| 615 | int res; |
| 616 | dev = (struct cfspi_dev *)pdev->dev.platform_data; |
| 617 | |
| 618 | ndev = alloc_netdev(sizeof(struct cfspi), |
| 619 | "cfspi%d", cfspi_setup); |
| 620 | if (!dev) |
| 621 | return -ENODEV; |
| 622 | |
| 623 | cfspi = netdev_priv(ndev); |
| 624 | netif_stop_queue(ndev); |
| 625 | cfspi->ndev = ndev; |
| 626 | cfspi->pdev = pdev; |
| 627 | |
| 628 | /* Set flow info */ |
| 629 | cfspi->flow_off_sent = 0; |
| 630 | cfspi->qd_low_mark = LOW_WATER_MARK; |
| 631 | cfspi->qd_high_mark = HIGH_WATER_MARK; |
| 632 | |
| 633 | /* Assign the SPI device. */ |
| 634 | cfspi->dev = dev; |
| 635 | /* Assign the device ifc to this SPI interface. */ |
| 636 | dev->ifc = &cfspi->ifc; |
| 637 | |
| 638 | /* Allocate DMA buffers. */ |
| 639 | cfspi->xfer.va_tx = dma_alloc(&cfspi->xfer.pa_tx); |
| 640 | if (!cfspi->xfer.va_tx) { |
| 641 | printk(KERN_WARNING |
| 642 | "CFSPI: failed to allocate dma TX buffer.\n"); |
| 643 | res = -ENODEV; |
| 644 | goto err_dma_alloc_tx; |
| 645 | } |
| 646 | |
| 647 | cfspi->xfer.va_rx = dma_alloc(&cfspi->xfer.pa_rx); |
| 648 | |
| 649 | if (!cfspi->xfer.va_rx) { |
| 650 | printk(KERN_WARNING |
| 651 | "CFSPI: failed to allocate dma TX buffer.\n"); |
| 652 | res = -ENODEV; |
| 653 | goto err_dma_alloc_rx; |
| 654 | } |
| 655 | |
| 656 | /* Initialize the work queue. */ |
| 657 | INIT_WORK(&cfspi->work, cfspi_xfer); |
| 658 | |
| 659 | /* Initialize spin locks. */ |
| 660 | spin_lock_init(&cfspi->lock); |
| 661 | |
| 662 | /* Initialize flow control state. */ |
| 663 | cfspi->flow_stop = false; |
| 664 | |
| 665 | /* Initialize wait queue. */ |
| 666 | init_waitqueue_head(&cfspi->wait); |
| 667 | |
| 668 | /* Create work thread. */ |
| 669 | cfspi->wq = create_singlethread_workqueue(dev->name); |
| 670 | if (!cfspi->wq) { |
| 671 | printk(KERN_WARNING "CFSPI: failed to create work queue.\n"); |
| 672 | res = -ENODEV; |
| 673 | goto err_create_wq; |
| 674 | } |
| 675 | |
| 676 | /* Initialize work queue. */ |
| 677 | init_completion(&cfspi->comp); |
| 678 | |
| 679 | /* Create debugfs entries. */ |
| 680 | dev_debugfs_add(cfspi); |
| 681 | |
| 682 | /* Set up the ifc. */ |
| 683 | cfspi->ifc.ss_cb = cfspi_ss_cb; |
| 684 | cfspi->ifc.xfer_done_cb = cfspi_xfer_done_cb; |
| 685 | cfspi->ifc.priv = cfspi; |
| 686 | |
| 687 | /* Add CAIF SPI device to list. */ |
| 688 | spin_lock(&cfspi_list_lock); |
| 689 | list_add_tail(&cfspi->list, &cfspi_list); |
| 690 | spin_unlock(&cfspi_list_lock); |
| 691 | |
| 692 | /* Schedule the work queue. */ |
| 693 | queue_work(cfspi->wq, &cfspi->work); |
| 694 | |
| 695 | /* Register network device. */ |
| 696 | res = register_netdev(ndev); |
| 697 | if (res) { |
| 698 | printk(KERN_ERR "CFSPI: Reg. error: %d.\n", res); |
| 699 | goto err_net_reg; |
| 700 | } |
| 701 | return res; |
| 702 | |
| 703 | err_net_reg: |
| 704 | dev_debugfs_rem(cfspi); |
| 705 | set_bit(SPI_TERMINATE, &cfspi->state); |
| 706 | wake_up_interruptible(&cfspi->wait); |
| 707 | destroy_workqueue(cfspi->wq); |
| 708 | err_create_wq: |
| 709 | dma_free(cfspi->xfer.va_rx, cfspi->xfer.pa_rx); |
| 710 | err_dma_alloc_rx: |
| 711 | dma_free(cfspi->xfer.va_tx, cfspi->xfer.pa_tx); |
| 712 | err_dma_alloc_tx: |
| 713 | free_netdev(ndev); |
| 714 | |
| 715 | return res; |
| 716 | } |
| 717 | |
| 718 | int cfspi_spi_remove(struct platform_device *pdev) |
| 719 | { |
| 720 | struct list_head *list_node; |
| 721 | struct list_head *n; |
| 722 | struct cfspi *cfspi = NULL; |
| 723 | struct cfspi_dev *dev; |
| 724 | |
| 725 | dev = (struct cfspi_dev *)pdev->dev.platform_data; |
| 726 | spin_lock(&cfspi_list_lock); |
| 727 | list_for_each_safe(list_node, n, &cfspi_list) { |
| 728 | cfspi = list_entry(list_node, struct cfspi, list); |
| 729 | /* Find the corresponding device. */ |
| 730 | if (cfspi->dev == dev) { |
| 731 | /* Remove from list. */ |
| 732 | list_del(list_node); |
| 733 | /* Free DMA buffers. */ |
| 734 | dma_free(cfspi->xfer.va_rx, cfspi->xfer.pa_rx); |
| 735 | dma_free(cfspi->xfer.va_tx, cfspi->xfer.pa_tx); |
| 736 | set_bit(SPI_TERMINATE, &cfspi->state); |
| 737 | wake_up_interruptible(&cfspi->wait); |
| 738 | destroy_workqueue(cfspi->wq); |
| 739 | /* Destroy debugfs directory and files. */ |
| 740 | dev_debugfs_rem(cfspi); |
| 741 | unregister_netdev(cfspi->ndev); |
| 742 | spin_unlock(&cfspi_list_lock); |
| 743 | return 0; |
| 744 | } |
| 745 | } |
| 746 | spin_unlock(&cfspi_list_lock); |
| 747 | return -ENODEV; |
| 748 | } |
| 749 | |
| 750 | static void __exit cfspi_exit_module(void) |
| 751 | { |
| 752 | struct list_head *list_node; |
| 753 | struct list_head *n; |
| 754 | struct cfspi *cfspi = NULL; |
| 755 | |
| 756 | list_for_each_safe(list_node, n, &cfspi_list) { |
| 757 | cfspi = list_entry(list_node, struct cfspi, list); |
| 758 | platform_device_unregister(cfspi->pdev); |
| 759 | } |
| 760 | |
| 761 | /* Destroy sysfs files. */ |
| 762 | driver_remove_file(&cfspi_spi_driver.driver, |
| 763 | &driver_attr_up_head_align); |
| 764 | driver_remove_file(&cfspi_spi_driver.driver, |
| 765 | &driver_attr_up_tail_align); |
| 766 | driver_remove_file(&cfspi_spi_driver.driver, |
| 767 | &driver_attr_down_head_align); |
| 768 | driver_remove_file(&cfspi_spi_driver.driver, |
| 769 | &driver_attr_down_tail_align); |
| 770 | driver_remove_file(&cfspi_spi_driver.driver, &driver_attr_frame_align); |
| 771 | /* Unregister platform driver. */ |
| 772 | platform_driver_unregister(&cfspi_spi_driver); |
| 773 | /* Destroy debugfs root directory. */ |
| 774 | driver_debugfs_remove(); |
| 775 | } |
| 776 | |
| 777 | static int __init cfspi_init_module(void) |
| 778 | { |
| 779 | int result; |
| 780 | |
| 781 | /* Initialize spin lock. */ |
| 782 | spin_lock_init(&cfspi_list_lock); |
| 783 | |
| 784 | /* Register platform driver. */ |
| 785 | result = platform_driver_register(&cfspi_spi_driver); |
| 786 | if (result) { |
| 787 | printk(KERN_ERR "Could not register platform SPI driver.\n"); |
| 788 | goto err_dev_register; |
| 789 | } |
| 790 | |
| 791 | /* Create sysfs files. */ |
| 792 | result = |
| 793 | driver_create_file(&cfspi_spi_driver.driver, |
| 794 | &driver_attr_up_head_align); |
| 795 | if (result) { |
| 796 | printk(KERN_ERR "Sysfs creation failed 1.\n"); |
| 797 | goto err_create_up_head_align; |
| 798 | } |
| 799 | |
| 800 | result = |
| 801 | driver_create_file(&cfspi_spi_driver.driver, |
| 802 | &driver_attr_up_tail_align); |
| 803 | if (result) { |
| 804 | printk(KERN_ERR "Sysfs creation failed 2.\n"); |
| 805 | goto err_create_up_tail_align; |
| 806 | } |
| 807 | |
| 808 | result = |
| 809 | driver_create_file(&cfspi_spi_driver.driver, |
| 810 | &driver_attr_down_head_align); |
| 811 | if (result) { |
| 812 | printk(KERN_ERR "Sysfs creation failed 3.\n"); |
| 813 | goto err_create_down_head_align; |
| 814 | } |
| 815 | |
| 816 | result = |
| 817 | driver_create_file(&cfspi_spi_driver.driver, |
| 818 | &driver_attr_down_tail_align); |
| 819 | if (result) { |
| 820 | printk(KERN_ERR "Sysfs creation failed 4.\n"); |
| 821 | goto err_create_down_tail_align; |
| 822 | } |
| 823 | |
| 824 | result = |
| 825 | driver_create_file(&cfspi_spi_driver.driver, |
| 826 | &driver_attr_frame_align); |
| 827 | if (result) { |
| 828 | printk(KERN_ERR "Sysfs creation failed 5.\n"); |
| 829 | goto err_create_frame_align; |
| 830 | } |
| 831 | driver_debugfs_create(); |
| 832 | return result; |
| 833 | |
| 834 | err_create_frame_align: |
| 835 | driver_remove_file(&cfspi_spi_driver.driver, |
| 836 | &driver_attr_down_tail_align); |
| 837 | err_create_down_tail_align: |
| 838 | driver_remove_file(&cfspi_spi_driver.driver, |
| 839 | &driver_attr_down_head_align); |
| 840 | err_create_down_head_align: |
| 841 | driver_remove_file(&cfspi_spi_driver.driver, |
| 842 | &driver_attr_up_tail_align); |
| 843 | err_create_up_tail_align: |
| 844 | driver_remove_file(&cfspi_spi_driver.driver, |
| 845 | &driver_attr_up_head_align); |
| 846 | err_create_up_head_align: |
| 847 | err_dev_register: |
| 848 | return result; |
| 849 | } |
| 850 | |
| 851 | module_init(cfspi_init_module); |
| 852 | module_exit(cfspi_exit_module); |