Commit | Line | Data |
---|---|---|
b5e47729 SR |
1 | /* |
2 | * nosy - Snoop mode driver for TI PCILynx 1394 controllers | |
3 | * Copyright (C) 2002-2007 Kristian Høgsberg | |
28646821 SR |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; either version 2 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software Foundation, | |
17 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
18 | */ | |
19 | ||
28646821 | 20 | #include <linux/errno.h> |
b5e47729 | 21 | #include <linux/fs.h> |
28646821 | 22 | #include <linux/init.h> |
b5e47729 SR |
23 | #include <linux/interrupt.h> |
24 | #include <linux/io.h> | |
25 | #include <linux/kernel.h> | |
424d66ce | 26 | #include <linux/kref.h> |
b5e47729 SR |
27 | #include <linux/miscdevice.h> |
28 | #include <linux/module.h> | |
424d66ce | 29 | #include <linux/mutex.h> |
28646821 | 30 | #include <linux/pci.h> |
28646821 | 31 | #include <linux/poll.h> |
b5e47729 SR |
32 | #include <linux/sched.h> /* required for linux/wait.h */ |
33 | #include <linux/slab.h> | |
34 | #include <linux/spinlock.h> | |
35 | #include <linux/timex.h> | |
36 | #include <linux/uaccess.h> | |
37 | #include <linux/wait.h> | |
38 | ||
28646821 | 39 | #include <asm/atomic.h> |
b5e47729 | 40 | #include <asm/byteorder.h> |
28646821 SR |
41 | |
42 | #include "nosy.h" | |
43 | #include "nosy-user.h" | |
44 | ||
45 | #define TCODE_PHY_PACKET 0x10 | |
46 | #define PCI_DEVICE_ID_TI_PCILYNX 0x8000 | |
47 | ||
48 | #define notify(s, args...) printk(KERN_NOTICE s, ## args) | |
49 | #define error(s, args...) printk(KERN_ERR s, ## args) | |
50 | #define debug(s, args...) printk(KERN_DEBUG s, ## args) | |
51 | ||
b5e47729 | 52 | static char driver_name[] = KBUILD_MODNAME; |
28646821 | 53 | |
28646821 SR |
54 | /* this is the physical layout of a PCL, its size is 128 bytes */ |
55 | struct pcl { | |
fd8c8d46 SR |
56 | __le32 next; |
57 | __le32 async_error_next; | |
58 | u32 user_data; | |
59 | __le32 pcl_status; | |
60 | __le32 remaining_transfer_count; | |
61 | __le32 next_data_buffer; | |
62 | struct { | |
63 | __le32 control; | |
64 | __le32 pointer; | |
65 | } buffer[13]; | |
66 | }; | |
28646821 SR |
67 | |
68 | struct packet { | |
b5e47729 | 69 | unsigned int length; |
28646821 SR |
70 | char data[0]; |
71 | }; | |
72 | ||
73 | struct packet_buffer { | |
74 | char *data; | |
75 | size_t capacity; | |
76 | long total_packet_count, lost_packet_count; | |
77 | atomic_t size; | |
b5e47729 SR |
78 | struct packet *head, *tail; |
79 | wait_queue_head_t wait; | |
28646821 SR |
80 | }; |
81 | ||
82 | struct pcilynx { | |
83 | struct pci_dev *pci_device; | |
c89db7b8 | 84 | __iomem char *registers; |
28646821 SR |
85 | |
86 | struct pcl *rcv_start_pcl, *rcv_pcl; | |
fd8c8d46 | 87 | __le32 *rcv_buffer; |
28646821 SR |
88 | |
89 | dma_addr_t rcv_start_pcl_bus, rcv_pcl_bus, rcv_buffer_bus; | |
90 | ||
91 | spinlock_t client_list_lock; | |
92 | struct list_head client_list; | |
93 | ||
94 | struct miscdevice misc; | |
424d66ce SR |
95 | struct list_head link; |
96 | struct kref kref; | |
28646821 SR |
97 | }; |
98 | ||
424d66ce SR |
99 | static inline struct pcilynx * |
100 | lynx_get(struct pcilynx *lynx) | |
101 | { | |
102 | kref_get(&lynx->kref); | |
103 | ||
104 | return lynx; | |
105 | } | |
106 | ||
107 | static void | |
108 | lynx_release(struct kref *kref) | |
109 | { | |
110 | kfree(container_of(kref, struct pcilynx, kref)); | |
111 | } | |
112 | ||
113 | static inline void | |
114 | lynx_put(struct pcilynx *lynx) | |
115 | { | |
116 | kref_put(&lynx->kref, lynx_release); | |
117 | } | |
118 | ||
28646821 SR |
119 | struct client { |
120 | struct pcilynx *lynx; | |
c7b2a99c | 121 | u32 tcode_mask; |
28646821 SR |
122 | struct packet_buffer buffer; |
123 | struct list_head link; | |
124 | }; | |
125 | ||
424d66ce SR |
126 | static DEFINE_MUTEX(card_mutex); |
127 | static LIST_HEAD(card_list); | |
28646821 SR |
128 | |
129 | static int | |
130 | packet_buffer_init(struct packet_buffer *buffer, size_t capacity) | |
131 | { | |
132 | buffer->data = kmalloc(capacity, GFP_KERNEL); | |
133 | if (buffer->data == NULL) | |
134 | return -ENOMEM; | |
135 | buffer->head = (struct packet *) buffer->data; | |
136 | buffer->tail = (struct packet *) buffer->data; | |
137 | buffer->capacity = capacity; | |
138 | buffer->lost_packet_count = 0; | |
139 | atomic_set(&buffer->size, 0); | |
b5e47729 | 140 | init_waitqueue_head(&buffer->wait); |
28646821 SR |
141 | |
142 | return 0; | |
143 | } | |
144 | ||
145 | static void | |
146 | packet_buffer_destroy(struct packet_buffer *buffer) | |
147 | { | |
148 | kfree(buffer->data); | |
149 | } | |
150 | ||
151 | static int | |
c89db7b8 | 152 | packet_buffer_get(struct client *client, char __user *data, size_t user_length) |
28646821 | 153 | { |
424d66ce | 154 | struct packet_buffer *buffer = &client->buffer; |
28646821 SR |
155 | size_t length; |
156 | char *end; | |
157 | ||
158 | if (wait_event_interruptible(buffer->wait, | |
424d66ce SR |
159 | atomic_read(&buffer->size) > 0) || |
160 | list_empty(&client->lynx->link)) | |
28646821 SR |
161 | return -ERESTARTSYS; |
162 | ||
424d66ce SR |
163 | if (atomic_read(&buffer->size) == 0) |
164 | return -ENODEV; | |
165 | ||
28646821 SR |
166 | /* FIXME: Check length <= user_length. */ |
167 | ||
168 | end = buffer->data + buffer->capacity; | |
169 | length = buffer->head->length; | |
170 | ||
171 | if (&buffer->head->data[length] < end) { | |
172 | if (copy_to_user(data, buffer->head->data, length)) | |
173 | return -EFAULT; | |
174 | buffer->head = (struct packet *) &buffer->head->data[length]; | |
b5e47729 | 175 | } else { |
28646821 SR |
176 | size_t split = end - buffer->head->data; |
177 | ||
178 | if (copy_to_user(data, buffer->head->data, split)) | |
179 | return -EFAULT; | |
180 | if (copy_to_user(data + split, buffer->data, length - split)) | |
181 | return -EFAULT; | |
182 | buffer->head = (struct packet *) &buffer->data[length - split]; | |
183 | } | |
184 | ||
b5e47729 SR |
185 | /* |
186 | * Decrease buffer->size as the last thing, since this is what | |
28646821 | 187 | * keeps the interrupt from overwriting the packet we are |
b5e47729 SR |
188 | * retrieving from the buffer. |
189 | */ | |
190 | atomic_sub(sizeof(struct packet) + length, &buffer->size); | |
28646821 SR |
191 | |
192 | return length; | |
193 | } | |
194 | ||
195 | static void | |
196 | packet_buffer_put(struct packet_buffer *buffer, void *data, size_t length) | |
197 | { | |
198 | char *end; | |
199 | ||
200 | buffer->total_packet_count++; | |
201 | ||
b5e47729 SR |
202 | if (buffer->capacity < |
203 | atomic_read(&buffer->size) + sizeof(struct packet) + length) { | |
28646821 SR |
204 | buffer->lost_packet_count++; |
205 | return; | |
206 | } | |
207 | ||
208 | end = buffer->data + buffer->capacity; | |
209 | buffer->tail->length = length; | |
210 | ||
211 | if (&buffer->tail->data[length] < end) { | |
212 | memcpy(buffer->tail->data, data, length); | |
213 | buffer->tail = (struct packet *) &buffer->tail->data[length]; | |
b5e47729 | 214 | } else { |
28646821 SR |
215 | size_t split = end - buffer->tail->data; |
216 | ||
217 | memcpy(buffer->tail->data, data, split); | |
218 | memcpy(buffer->data, data + split, length - split); | |
219 | buffer->tail = (struct packet *) &buffer->data[length - split]; | |
220 | } | |
b5e47729 | 221 | |
28646821 SR |
222 | /* Finally, adjust buffer size and wake up userspace reader. */ |
223 | ||
b5e47729 | 224 | atomic_add(sizeof(struct packet) + length, &buffer->size); |
28646821 SR |
225 | wake_up_interruptible(&buffer->wait); |
226 | } | |
227 | ||
228 | static inline void | |
229 | reg_write(struct pcilynx *lynx, int offset, u32 data) | |
230 | { | |
b5e47729 | 231 | writel(data, lynx->registers + offset); |
28646821 SR |
232 | } |
233 | ||
234 | static inline u32 | |
235 | reg_read(struct pcilynx *lynx, int offset) | |
236 | { | |
b5e47729 | 237 | return readl(lynx->registers + offset); |
28646821 SR |
238 | } |
239 | ||
240 | static inline void | |
241 | reg_set_bits(struct pcilynx *lynx, int offset, u32 mask) | |
242 | { | |
b5e47729 | 243 | reg_write(lynx, offset, (reg_read(lynx, offset) | mask)); |
28646821 SR |
244 | } |
245 | ||
b5e47729 SR |
246 | /* |
247 | * Maybe the pcl programs could be set up to just append data instead | |
248 | * of using a whole packet. | |
249 | */ | |
250 | static inline void | |
251 | run_pcl(struct pcilynx *lynx, dma_addr_t pcl_bus, | |
252 | int dmachan) | |
28646821 | 253 | { |
b5e47729 SR |
254 | reg_write(lynx, DMA0_CURRENT_PCL + dmachan * 0x20, pcl_bus); |
255 | reg_write(lynx, DMA0_CHAN_CTRL + dmachan * 0x20, | |
256 | DMA_CHAN_CTRL_ENABLE | DMA_CHAN_CTRL_LINK); | |
28646821 SR |
257 | } |
258 | ||
259 | static int | |
260 | set_phy_reg(struct pcilynx *lynx, int addr, int val) | |
261 | { | |
b5e47729 SR |
262 | if (addr > 15) { |
263 | debug("PHY register address %d out of range\n", addr); | |
264 | return -1; | |
265 | } | |
266 | ||
267 | if (val > 0xff) { | |
268 | debug("PHY register value %d out of range\n", val); | |
269 | return -1; | |
270 | } | |
271 | ||
272 | reg_write(lynx, LINK_PHY, LINK_PHY_WRITE | | |
28646821 SR |
273 | LINK_PHY_ADDR(addr) | LINK_PHY_WDATA(val)); |
274 | ||
b5e47729 | 275 | return 0; |
28646821 SR |
276 | } |
277 | ||
55e77c06 SR |
278 | static int |
279 | nosy_open(struct inode *inode, struct file *file) | |
28646821 | 280 | { |
55e77c06 | 281 | int minor = iminor(inode); |
28646821 | 282 | struct client *client; |
424d66ce SR |
283 | struct pcilynx *tmp, *lynx = NULL; |
284 | ||
285 | mutex_lock(&card_mutex); | |
286 | list_for_each_entry(tmp, &card_list, link) | |
287 | if (tmp->misc.minor == minor) { | |
288 | lynx = lynx_get(tmp); | |
289 | break; | |
290 | } | |
291 | mutex_unlock(&card_mutex); | |
292 | if (lynx == NULL) | |
55e77c06 SR |
293 | return -ENODEV; |
294 | ||
28646821 | 295 | client = kmalloc(sizeof *client, GFP_KERNEL); |
55e77c06 | 296 | if (client == NULL) |
424d66ce | 297 | goto fail; |
55e77c06 | 298 | |
28646821 | 299 | client->tcode_mask = ~0; |
424d66ce | 300 | client->lynx = lynx; |
28646821 SR |
301 | INIT_LIST_HEAD(&client->link); |
302 | ||
424d66ce SR |
303 | if (packet_buffer_init(&client->buffer, 128 * 1024) < 0) |
304 | goto fail; | |
28646821 | 305 | |
55e77c06 | 306 | file->private_data = client; |
28646821 | 307 | |
55e77c06 | 308 | return 0; |
424d66ce SR |
309 | fail: |
310 | kfree(client); | |
311 | lynx_put(lynx); | |
312 | ||
313 | return -ENOMEM; | |
28646821 SR |
314 | } |
315 | ||
316 | static int | |
55e77c06 | 317 | nosy_release(struct inode *inode, struct file *file) |
28646821 | 318 | { |
55e77c06 | 319 | struct client *client = file->private_data; |
424d66ce | 320 | struct pcilynx *lynx = client->lynx; |
28646821 | 321 | |
424d66ce | 322 | spin_lock_irq(&lynx->client_list_lock); |
55e77c06 | 323 | list_del_init(&client->link); |
424d66ce | 324 | spin_unlock_irq(&lynx->client_list_lock); |
28646821 | 325 | |
55e77c06 SR |
326 | packet_buffer_destroy(&client->buffer); |
327 | kfree(client); | |
424d66ce | 328 | lynx_put(lynx); |
28646821 | 329 | |
b5e47729 | 330 | return 0; |
28646821 SR |
331 | } |
332 | ||
333 | static unsigned int | |
334 | nosy_poll(struct file *file, poll_table *pt) | |
335 | { | |
336 | struct client *client = file->private_data; | |
424d66ce | 337 | unsigned int ret = 0; |
28646821 SR |
338 | |
339 | poll_wait(file, &client->buffer.wait, pt); | |
340 | ||
341 | if (atomic_read(&client->buffer.size) > 0) | |
424d66ce SR |
342 | ret = POLLIN | POLLRDNORM; |
343 | ||
344 | if (list_empty(&client->lynx->link)) | |
345 | ret |= POLLHUP; | |
346 | ||
347 | return ret; | |
28646821 SR |
348 | } |
349 | ||
350 | static ssize_t | |
c89db7b8 | 351 | nosy_read(struct file *file, char __user *buffer, size_t count, loff_t *offset) |
28646821 SR |
352 | { |
353 | struct client *client = file->private_data; | |
354 | ||
424d66ce | 355 | return packet_buffer_get(client, buffer, count); |
28646821 SR |
356 | } |
357 | ||
c7b2a99c SR |
358 | static long |
359 | nosy_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |
28646821 SR |
360 | { |
361 | struct client *client = file->private_data; | |
c7b2a99c | 362 | spinlock_t *client_list_lock = &client->lynx->client_list_lock; |
b5e47729 | 363 | struct nosy_stats stats; |
28646821 SR |
364 | |
365 | switch (cmd) { | |
b5e47729 | 366 | case NOSY_IOC_GET_STATS: |
c7b2a99c | 367 | spin_lock_irq(client_list_lock); |
28646821 | 368 | stats.total_packet_count = client->buffer.total_packet_count; |
c7b2a99c SR |
369 | stats.lost_packet_count = client->buffer.lost_packet_count; |
370 | spin_unlock_irq(client_list_lock); | |
371 | ||
c89db7b8 | 372 | if (copy_to_user((void __user *) arg, &stats, sizeof stats)) |
28646821 SR |
373 | return -EFAULT; |
374 | else | |
375 | return 0; | |
b5e47729 | 376 | |
28646821 | 377 | case NOSY_IOC_START: |
55e77c06 SR |
378 | spin_lock_irq(client_list_lock); |
379 | list_add_tail(&client->link, &client->lynx->client_list); | |
380 | spin_unlock_irq(client_list_lock); | |
381 | ||
28646821 SR |
382 | return 0; |
383 | ||
384 | case NOSY_IOC_STOP: | |
55e77c06 SR |
385 | spin_lock_irq(client_list_lock); |
386 | list_del_init(&client->link); | |
387 | spin_unlock_irq(client_list_lock); | |
388 | ||
28646821 SR |
389 | return 0; |
390 | ||
391 | case NOSY_IOC_FILTER: | |
c7b2a99c | 392 | spin_lock_irq(client_list_lock); |
28646821 | 393 | client->tcode_mask = arg; |
c7b2a99c | 394 | spin_unlock_irq(client_list_lock); |
55e77c06 | 395 | |
28646821 SR |
396 | return 0; |
397 | ||
398 | default: | |
399 | return -EINVAL; | |
400 | /* Flush buffer, configure filter. */ | |
401 | } | |
402 | } | |
403 | ||
b5e47729 | 404 | static const struct file_operations nosy_ops = { |
c7b2a99c SR |
405 | .owner = THIS_MODULE, |
406 | .read = nosy_read, | |
407 | .unlocked_ioctl = nosy_ioctl, | |
408 | .poll = nosy_poll, | |
409 | .open = nosy_open, | |
410 | .release = nosy_release, | |
28646821 SR |
411 | }; |
412 | ||
413 | #define PHY_PACKET_SIZE 12 /* 1 payload, 1 inverse, 1 ack = 3 quadlets */ | |
414 | ||
28646821 | 415 | static void |
685c3f80 | 416 | packet_irq_handler(struct pcilynx *lynx) |
28646821 | 417 | { |
28646821 | 418 | struct client *client; |
fd8c8d46 | 419 | u32 tcode_mask, tcode; |
28646821 | 420 | size_t length; |
28646821 SR |
421 | struct timeval tv; |
422 | ||
423 | /* FIXME: Also report rcv_speed. */ | |
424 | ||
fd8c8d46 SR |
425 | length = __le32_to_cpu(lynx->rcv_pcl->pcl_status) & 0x00001fff; |
426 | tcode = __le32_to_cpu(lynx->rcv_buffer[1]) >> 4 & 0xf; | |
28646821 SR |
427 | |
428 | do_gettimeofday(&tv); | |
fd8c8d46 | 429 | lynx->rcv_buffer[0] = (__force __le32)tv.tv_usec; |
28646821 SR |
430 | |
431 | if (length == PHY_PACKET_SIZE) | |
432 | tcode_mask = 1 << TCODE_PHY_PACKET; | |
433 | else | |
fd8c8d46 | 434 | tcode_mask = 1 << tcode; |
28646821 | 435 | |
685c3f80 | 436 | spin_lock(&lynx->client_list_lock); |
28646821 | 437 | |
b5e47729 | 438 | list_for_each_entry(client, &lynx->client_list, link) |
28646821 | 439 | if (client->tcode_mask & tcode_mask) |
b5e47729 | 440 | packet_buffer_put(&client->buffer, |
28646821 | 441 | lynx->rcv_buffer, length + 4); |
28646821 | 442 | |
685c3f80 | 443 | spin_unlock(&lynx->client_list_lock); |
28646821 SR |
444 | } |
445 | ||
446 | static void | |
685c3f80 | 447 | bus_reset_irq_handler(struct pcilynx *lynx) |
28646821 | 448 | { |
28646821 SR |
449 | struct client *client; |
450 | struct timeval tv; | |
451 | ||
452 | do_gettimeofday(&tv); | |
453 | ||
685c3f80 | 454 | spin_lock(&lynx->client_list_lock); |
28646821 | 455 | |
b5e47729 | 456 | list_for_each_entry(client, &lynx->client_list, link) |
28646821 | 457 | packet_buffer_put(&client->buffer, &tv.tv_usec, 4); |
28646821 | 458 | |
685c3f80 | 459 | spin_unlock(&lynx->client_list_lock); |
28646821 SR |
460 | } |
461 | ||
28646821 SR |
462 | static irqreturn_t |
463 | irq_handler(int irq, void *device) | |
464 | { | |
b5e47729 | 465 | struct pcilynx *lynx = device; |
28646821 | 466 | u32 pci_int_status; |
b5e47729 SR |
467 | |
468 | pci_int_status = reg_read(lynx, PCI_INT_STATUS); | |
28646821 | 469 | |
16547667 SR |
470 | if (pci_int_status == ~0) |
471 | /* Card was ejected. */ | |
472 | return IRQ_NONE; | |
473 | ||
28646821 SR |
474 | if ((pci_int_status & PCI_INT_INT_PEND) == 0) |
475 | /* Not our interrupt, bail out quickly. */ | |
476 | return IRQ_NONE; | |
477 | ||
478 | if ((pci_int_status & PCI_INT_P1394_INT) != 0) { | |
479 | u32 link_int_status; | |
480 | ||
481 | link_int_status = reg_read(lynx, LINK_INT_STATUS); | |
482 | reg_write(lynx, LINK_INT_STATUS, link_int_status); | |
483 | ||
484 | if ((link_int_status & LINK_INT_PHY_BUSRESET) > 0) | |
685c3f80 | 485 | bus_reset_irq_handler(lynx); |
28646821 SR |
486 | } |
487 | ||
488 | /* Clear the PCI_INT_STATUS register only after clearing the | |
489 | * LINK_INT_STATUS register; otherwise the PCI_INT_P1394 will | |
490 | * be set again immediately. */ | |
491 | ||
492 | reg_write(lynx, PCI_INT_STATUS, pci_int_status); | |
493 | ||
494 | if ((pci_int_status & PCI_INT_DMA0_HLT) > 0) { | |
685c3f80 | 495 | packet_irq_handler(lynx); |
28646821 SR |
496 | run_pcl(lynx, lynx->rcv_start_pcl_bus, 0); |
497 | } | |
498 | ||
499 | return IRQ_HANDLED; | |
500 | } | |
501 | ||
502 | static void | |
503 | remove_card(struct pci_dev *dev) | |
504 | { | |
424d66ce SR |
505 | struct pcilynx *lynx = pci_get_drvdata(dev); |
506 | struct client *client; | |
28646821 | 507 | |
424d66ce SR |
508 | mutex_lock(&card_mutex); |
509 | list_del_init(&lynx->link); | |
510 | misc_deregister(&lynx->misc); | |
511 | mutex_unlock(&card_mutex); | |
28646821 SR |
512 | |
513 | reg_write(lynx, PCI_INT_ENABLE, 0); | |
514 | free_irq(lynx->pci_device->irq, lynx); | |
515 | ||
424d66ce SR |
516 | spin_lock_irq(&lynx->client_list_lock); |
517 | list_for_each_entry(client, &lynx->client_list, link) | |
518 | wake_up_interruptible(&client->buffer.wait); | |
519 | spin_unlock_irq(&lynx->client_list_lock); | |
520 | ||
b5e47729 | 521 | pci_free_consistent(lynx->pci_device, sizeof(struct pcl), |
28646821 | 522 | lynx->rcv_start_pcl, lynx->rcv_start_pcl_bus); |
b5e47729 | 523 | pci_free_consistent(lynx->pci_device, sizeof(struct pcl), |
28646821 SR |
524 | lynx->rcv_pcl, lynx->rcv_pcl_bus); |
525 | pci_free_consistent(lynx->pci_device, PAGE_SIZE, | |
526 | lynx->rcv_buffer, lynx->rcv_buffer_bus); | |
527 | ||
528 | iounmap(lynx->registers); | |
b6d9c125 | 529 | pci_disable_device(dev); |
424d66ce | 530 | lynx_put(lynx); |
28646821 SR |
531 | } |
532 | ||
533 | #define RCV_BUFFER_SIZE (16 * 1024) | |
534 | ||
28646821 SR |
535 | static int __devinit |
536 | add_card(struct pci_dev *dev, const struct pci_device_id *unused) | |
537 | { | |
b5e47729 | 538 | struct pcilynx *lynx; |
28646821 | 539 | u32 p, end; |
b6d9c125 | 540 | int ret, i; |
28646821 | 541 | |
b5e47729 SR |
542 | if (pci_set_dma_mask(dev, 0xffffffff)) { |
543 | error("DMA address limits not supported " | |
544 | "for PCILynx hardware\n"); | |
545 | return -ENXIO; | |
546 | } | |
547 | if (pci_enable_device(dev)) { | |
548 | error("Failed to enable PCILynx hardware\n"); | |
549 | return -ENXIO; | |
550 | } | |
551 | pci_set_master(dev); | |
28646821 SR |
552 | |
553 | lynx = kzalloc(sizeof *lynx, GFP_KERNEL); | |
b5e47729 SR |
554 | if (lynx == NULL) { |
555 | error("Failed to allocate control structure memory\n"); | |
b6d9c125 SR |
556 | ret = -ENOMEM; |
557 | goto fail_disable; | |
b5e47729 SR |
558 | } |
559 | lynx->pci_device = dev; | |
560 | pci_set_drvdata(dev, lynx); | |
28646821 SR |
561 | |
562 | spin_lock_init(&lynx->client_list_lock); | |
563 | INIT_LIST_HEAD(&lynx->client_list); | |
424d66ce | 564 | kref_init(&lynx->kref); |
28646821 | 565 | |
b5e47729 SR |
566 | lynx->registers = ioremap_nocache(pci_resource_start(dev, 0), |
567 | PCILYNX_MAX_REGISTER); | |
568 | ||
569 | lynx->rcv_start_pcl = pci_alloc_consistent(lynx->pci_device, | |
570 | sizeof(struct pcl), &lynx->rcv_start_pcl_bus); | |
571 | lynx->rcv_pcl = pci_alloc_consistent(lynx->pci_device, | |
572 | sizeof(struct pcl), &lynx->rcv_pcl_bus); | |
573 | lynx->rcv_buffer = pci_alloc_consistent(lynx->pci_device, | |
574 | RCV_BUFFER_SIZE, &lynx->rcv_buffer_bus); | |
575 | if (lynx->rcv_start_pcl == NULL || | |
28646821 | 576 | lynx->rcv_pcl == NULL || |
b5e47729 | 577 | lynx->rcv_buffer == NULL) { |
b5e47729 | 578 | error("Failed to allocate receive buffer\n"); |
b6d9c125 SR |
579 | ret = -ENOMEM; |
580 | goto fail_deallocate; | |
b5e47729 | 581 | } |
fd8c8d46 SR |
582 | lynx->rcv_start_pcl->next = cpu_to_le32(lynx->rcv_pcl_bus); |
583 | lynx->rcv_pcl->next = cpu_to_le32(PCL_NEXT_INVALID); | |
584 | lynx->rcv_pcl->async_error_next = cpu_to_le32(PCL_NEXT_INVALID); | |
28646821 | 585 | |
b5e47729 | 586 | lynx->rcv_pcl->buffer[0].control = |
fd8c8d46 SR |
587 | cpu_to_le32(PCL_CMD_RCV | PCL_BIGENDIAN | 2044); |
588 | lynx->rcv_pcl->buffer[0].pointer = | |
589 | cpu_to_le32(lynx->rcv_buffer_bus + 4); | |
28646821 SR |
590 | p = lynx->rcv_buffer_bus + 2048; |
591 | end = lynx->rcv_buffer_bus + RCV_BUFFER_SIZE; | |
592 | for (i = 1; p < end; i++, p += 2048) { | |
593 | lynx->rcv_pcl->buffer[i].control = | |
fd8c8d46 SR |
594 | cpu_to_le32(PCL_CMD_RCV | PCL_BIGENDIAN | 2048); |
595 | lynx->rcv_pcl->buffer[i].pointer = cpu_to_le32(p); | |
28646821 | 596 | } |
fd8c8d46 | 597 | lynx->rcv_pcl->buffer[i - 1].control |= cpu_to_le32(PCL_LAST_BUFF); |
28646821 | 598 | |
b5e47729 SR |
599 | reg_set_bits(lynx, MISC_CONTROL, MISC_CONTROL_SWRESET); |
600 | /* Fix buggy cards with autoboot pin not tied low: */ | |
601 | reg_write(lynx, DMA0_CHAN_CTRL, 0); | |
602 | reg_write(lynx, DMA_GLOBAL_REGISTER, 0x00 << 24); | |
28646821 SR |
603 | |
604 | #if 0 | |
b5e47729 SR |
605 | /* now, looking for PHY register set */ |
606 | if ((get_phy_reg(lynx, 2) & 0xe0) == 0xe0) { | |
607 | lynx->phyic.reg_1394a = 1; | |
608 | PRINT(KERN_INFO, lynx->id, | |
609 | "found 1394a conform PHY (using extended register set)"); | |
610 | lynx->phyic.vendor = get_phy_vendorid(lynx); | |
611 | lynx->phyic.product = get_phy_productid(lynx); | |
612 | } else { | |
613 | lynx->phyic.reg_1394a = 0; | |
614 | PRINT(KERN_INFO, lynx->id, "found old 1394 PHY"); | |
615 | } | |
28646821 SR |
616 | #endif |
617 | ||
618 | /* Setup the general receive FIFO max size. */ | |
619 | reg_write(lynx, FIFO_SIZES, 255); | |
620 | ||
b5e47729 | 621 | reg_set_bits(lynx, PCI_INT_ENABLE, PCI_INT_DMA_ALL); |
28646821 | 622 | |
b5e47729 | 623 | reg_write(lynx, LINK_INT_ENABLE, |
28646821 SR |
624 | LINK_INT_PHY_TIME_OUT | LINK_INT_PHY_REG_RCVD | |
625 | LINK_INT_PHY_BUSRESET | LINK_INT_IT_STUCK | | |
626 | LINK_INT_AT_STUCK | LINK_INT_SNTRJ | | |
627 | LINK_INT_TC_ERR | LINK_INT_GRF_OVER_FLOW | | |
628 | LINK_INT_ITF_UNDER_FLOW | LINK_INT_ATF_UNDER_FLOW); | |
629 | ||
630 | /* Disable the L flag in self ID packets. */ | |
631 | set_phy_reg(lynx, 4, 0); | |
632 | ||
633 | /* Put this baby into snoop mode */ | |
634 | reg_set_bits(lynx, LINK_CONTROL, LINK_CONTROL_SNOOP_ENABLE); | |
635 | ||
636 | run_pcl(lynx, lynx->rcv_start_pcl_bus, 0); | |
637 | ||
b5e47729 SR |
638 | if (request_irq(dev->irq, irq_handler, IRQF_SHARED, |
639 | driver_name, lynx)) { | |
640 | error("Failed to allocate shared interrupt %d\n", dev->irq); | |
b6d9c125 SR |
641 | ret = -EIO; |
642 | goto fail_deallocate; | |
b5e47729 | 643 | } |
28646821 SR |
644 | |
645 | lynx->misc.parent = &dev->dev; | |
646 | lynx->misc.minor = MISC_DYNAMIC_MINOR; | |
647 | lynx->misc.name = "nosy"; | |
648 | lynx->misc.fops = &nosy_ops; | |
424d66ce SR |
649 | |
650 | mutex_lock(&card_mutex); | |
b6d9c125 SR |
651 | ret = misc_register(&lynx->misc); |
652 | if (ret) { | |
b5e47729 | 653 | error("Failed to register misc char device\n"); |
424d66ce | 654 | mutex_unlock(&card_mutex); |
b6d9c125 | 655 | goto fail_free_irq; |
b5e47729 | 656 | } |
424d66ce SR |
657 | list_add_tail(&lynx->link, &card_list); |
658 | mutex_unlock(&card_mutex); | |
28646821 SR |
659 | |
660 | notify("Initialized PCILynx IEEE1394 card, irq=%d\n", dev->irq); | |
661 | ||
b5e47729 | 662 | return 0; |
b6d9c125 SR |
663 | |
664 | fail_free_irq: | |
665 | reg_write(lynx, PCI_INT_ENABLE, 0); | |
666 | free_irq(lynx->pci_device->irq, lynx); | |
667 | ||
668 | fail_deallocate: | |
669 | if (lynx->rcv_start_pcl) | |
670 | pci_free_consistent(lynx->pci_device, sizeof(struct pcl), | |
671 | lynx->rcv_start_pcl, lynx->rcv_start_pcl_bus); | |
672 | if (lynx->rcv_pcl) | |
673 | pci_free_consistent(lynx->pci_device, sizeof(struct pcl), | |
674 | lynx->rcv_pcl, lynx->rcv_pcl_bus); | |
675 | if (lynx->rcv_buffer) | |
676 | pci_free_consistent(lynx->pci_device, PAGE_SIZE, | |
677 | lynx->rcv_buffer, lynx->rcv_buffer_bus); | |
678 | iounmap(lynx->registers); | |
679 | kfree(lynx); | |
680 | ||
681 | fail_disable: | |
682 | pci_disable_device(dev); | |
683 | ||
684 | return ret; | |
28646821 SR |
685 | } |
686 | ||
687 | static struct pci_device_id pci_table[] __devinitdata = { | |
688 | { | |
b5e47729 SR |
689 | .vendor = PCI_VENDOR_ID_TI, |
690 | .device = PCI_DEVICE_ID_TI_PCILYNX, | |
691 | .subvendor = PCI_ANY_ID, | |
692 | .subdevice = PCI_ANY_ID, | |
28646821 SR |
693 | }, |
694 | { } /* Terminating entry */ | |
695 | }; | |
696 | ||
697 | static struct pci_driver lynx_pci_driver = { | |
b5e47729 | 698 | .name = driver_name, |
28646821 SR |
699 | .id_table = pci_table, |
700 | .probe = add_card, | |
b5e47729 | 701 | .remove = remove_card, |
28646821 SR |
702 | }; |
703 | ||
b5e47729 | 704 | MODULE_AUTHOR("Kristian Hoegsberg"); |
28646821 SR |
705 | MODULE_DESCRIPTION("Snoop mode driver for TI pcilynx 1394 controllers"); |
706 | MODULE_LICENSE("GPL"); | |
707 | MODULE_DEVICE_TABLE(pci, pci_table); | |
708 | ||
709 | static int __init nosy_init(void) | |
710 | { | |
b5e47729 | 711 | return pci_register_driver(&lynx_pci_driver); |
28646821 SR |
712 | } |
713 | ||
714 | static void __exit nosy_cleanup(void) | |
715 | { | |
b5e47729 | 716 | pci_unregister_driver(&lynx_pci_driver); |
28646821 SR |
717 | |
718 | notify("Unloaded %s.\n", driver_name); | |
719 | } | |
720 | ||
28646821 SR |
721 | module_init(nosy_init); |
722 | module_exit(nosy_cleanup); |