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