sb1000.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  1. /* sb1000.c: A General Instruments SB1000 driver for linux. */
  2. /*
  3. Written 1998 by Franco Venturi.
  4. Copyright 1998 by Franco Venturi.
  5. Copyright 1994,1995 by Donald Becker.
  6. Copyright 1993 United States Government as represented by the
  7. Director, National Security Agency.
  8. This driver is for the General Instruments SB1000 (internal SURFboard)
  9. The author may be reached as fventuri@mediaone.net
  10. This program is free software; you can redistribute it
  11. and/or modify it under the terms of the GNU General
  12. Public License as published by the Free Software
  13. Foundation; either version 2 of the License, or (at
  14. your option) any later version.
  15. Changes:
  16. 981115 Steven Hirsch <shirsch@adelphia.net>
  17. Linus changed the timer interface. Should work on all recent
  18. development kernels.
  19. 980608 Steven Hirsch <shirsch@adelphia.net>
  20. Small changes to make it work with 2.1.x kernels. Hopefully,
  21. nothing major will change before official release of Linux 2.2.
  22. Merged with 2.2 - Alan Cox
  23. */
  24. static char version[] = "sb1000.c:v1.1.2 6/01/98 (fventuri@mediaone.net)\n";
  25. #include <linux/module.h>
  26. #include <linux/kernel.h>
  27. #include <linux/sched.h>
  28. #include <linux/string.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/errno.h>
  31. #include <linux/if_cablemodem.h> /* for SIOGCM/SIOSCM stuff */
  32. #include <linux/in.h>
  33. #include <linux/ioport.h>
  34. #include <linux/netdevice.h>
  35. #include <linux/if_arp.h>
  36. #include <linux/skbuff.h>
  37. #include <linux/delay.h> /* for udelay() */
  38. #include <linux/etherdevice.h>
  39. #include <linux/pnp.h>
  40. #include <linux/init.h>
  41. #include <linux/bitops.h>
  42. #include <linux/gfp.h>
  43. #include <asm/io.h>
  44. #include <asm/processor.h>
  45. #include <linux/uaccess.h>
  46. #ifdef SB1000_DEBUG
  47. static int sb1000_debug = SB1000_DEBUG;
  48. #else
  49. static const int sb1000_debug = 1;
  50. #endif
  51. static const int SB1000_IO_EXTENT = 8;
  52. /* SB1000 Maximum Receive Unit */
  53. static const int SB1000_MRU = 1500; /* octects */
  54. #define NPIDS 4
  55. struct sb1000_private {
  56. struct sk_buff *rx_skb[NPIDS];
  57. short rx_dlen[NPIDS];
  58. unsigned int rx_frames;
  59. short rx_error_count;
  60. short rx_error_dpc_count;
  61. unsigned char rx_session_id[NPIDS];
  62. unsigned char rx_frame_id[NPIDS];
  63. unsigned char rx_pkt_type[NPIDS];
  64. };
  65. /* prototypes for Linux interface */
  66. extern int sb1000_probe(struct net_device *dev);
  67. static int sb1000_open(struct net_device *dev);
  68. static int sb1000_dev_ioctl (struct net_device *dev, struct ifreq *ifr, int cmd);
  69. static netdev_tx_t sb1000_start_xmit(struct sk_buff *skb,
  70. struct net_device *dev);
  71. static irqreturn_t sb1000_interrupt(int irq, void *dev_id);
  72. static int sb1000_close(struct net_device *dev);
  73. /* SB1000 hardware routines to be used during open/configuration phases */
  74. static int card_wait_for_busy_clear(const int ioaddr[],
  75. const char* name);
  76. static int card_wait_for_ready(const int ioaddr[], const char* name,
  77. unsigned char in[]);
  78. static int card_send_command(const int ioaddr[], const char* name,
  79. const unsigned char out[], unsigned char in[]);
  80. /* SB1000 hardware routines to be used during frame rx interrupt */
  81. static int sb1000_wait_for_ready(const int ioaddr[], const char* name);
  82. static int sb1000_wait_for_ready_clear(const int ioaddr[],
  83. const char* name);
  84. static void sb1000_send_command(const int ioaddr[], const char* name,
  85. const unsigned char out[]);
  86. static void sb1000_read_status(const int ioaddr[], unsigned char in[]);
  87. static void sb1000_issue_read_command(const int ioaddr[],
  88. const char* name);
  89. /* SB1000 commands for open/configuration */
  90. static int sb1000_reset(const int ioaddr[], const char* name);
  91. static int sb1000_check_CRC(const int ioaddr[], const char* name);
  92. static inline int sb1000_start_get_set_command(const int ioaddr[],
  93. const char* name);
  94. static int sb1000_end_get_set_command(const int ioaddr[],
  95. const char* name);
  96. static int sb1000_activate(const int ioaddr[], const char* name);
  97. static int sb1000_get_firmware_version(const int ioaddr[],
  98. const char* name, unsigned char version[], int do_end);
  99. static int sb1000_get_frequency(const int ioaddr[], const char* name,
  100. int* frequency);
  101. static int sb1000_set_frequency(const int ioaddr[], const char* name,
  102. int frequency);
  103. static int sb1000_get_PIDs(const int ioaddr[], const char* name,
  104. short PID[]);
  105. static int sb1000_set_PIDs(const int ioaddr[], const char* name,
  106. const short PID[]);
  107. /* SB1000 commands for frame rx interrupt */
  108. static int sb1000_rx(struct net_device *dev);
  109. static void sb1000_error_dpc(struct net_device *dev);
  110. static const struct pnp_device_id sb1000_pnp_ids[] = {
  111. { "GIC1000", 0 },
  112. { "", 0 }
  113. };
  114. MODULE_DEVICE_TABLE(pnp, sb1000_pnp_ids);
  115. static const struct net_device_ops sb1000_netdev_ops = {
  116. .ndo_open = sb1000_open,
  117. .ndo_start_xmit = sb1000_start_xmit,
  118. .ndo_do_ioctl = sb1000_dev_ioctl,
  119. .ndo_stop = sb1000_close,
  120. .ndo_set_mac_address = eth_mac_addr,
  121. .ndo_validate_addr = eth_validate_addr,
  122. };
  123. static int
  124. sb1000_probe_one(struct pnp_dev *pdev, const struct pnp_device_id *id)
  125. {
  126. struct net_device *dev;
  127. unsigned short ioaddr[2], irq;
  128. unsigned int serial_number;
  129. int error = -ENODEV;
  130. if (pnp_device_attach(pdev) < 0)
  131. return -ENODEV;
  132. if (pnp_activate_dev(pdev) < 0)
  133. goto out_detach;
  134. if (!pnp_port_valid(pdev, 0) || !pnp_port_valid(pdev, 1))
  135. goto out_disable;
  136. if (!pnp_irq_valid(pdev, 0))
  137. goto out_disable;
  138. serial_number = pdev->card->serial;
  139. ioaddr[0] = pnp_port_start(pdev, 0);
  140. ioaddr[1] = pnp_port_start(pdev, 0);
  141. irq = pnp_irq(pdev, 0);
  142. if (!request_region(ioaddr[0], 16, "sb1000"))
  143. goto out_disable;
  144. if (!request_region(ioaddr[1], 16, "sb1000"))
  145. goto out_release_region0;
  146. dev = alloc_etherdev(sizeof(struct sb1000_private));
  147. if (!dev) {
  148. error = -ENOMEM;
  149. goto out_release_regions;
  150. }
  151. dev->base_addr = ioaddr[0];
  152. /* mem_start holds the second I/O address */
  153. dev->mem_start = ioaddr[1];
  154. dev->irq = irq;
  155. if (sb1000_debug > 0)
  156. printk(KERN_NOTICE "%s: sb1000 at (%#3.3lx,%#3.3lx), "
  157. "S/N %#8.8x, IRQ %d.\n", dev->name, dev->base_addr,
  158. dev->mem_start, serial_number, dev->irq);
  159. /*
  160. * The SB1000 is an rx-only cable modem device. The uplink is a modem
  161. * and we do not want to arp on it.
  162. */
  163. dev->flags = IFF_POINTOPOINT|IFF_NOARP;
  164. SET_NETDEV_DEV(dev, &pdev->dev);
  165. if (sb1000_debug > 0)
  166. printk(KERN_NOTICE "%s", version);
  167. dev->netdev_ops = &sb1000_netdev_ops;
  168. /* hardware address is 0:0:serial_number */
  169. dev->dev_addr[2] = serial_number >> 24 & 0xff;
  170. dev->dev_addr[3] = serial_number >> 16 & 0xff;
  171. dev->dev_addr[4] = serial_number >> 8 & 0xff;
  172. dev->dev_addr[5] = serial_number >> 0 & 0xff;
  173. pnp_set_drvdata(pdev, dev);
  174. error = register_netdev(dev);
  175. if (error)
  176. goto out_free_netdev;
  177. return 0;
  178. out_free_netdev:
  179. free_netdev(dev);
  180. out_release_regions:
  181. release_region(ioaddr[1], 16);
  182. out_release_region0:
  183. release_region(ioaddr[0], 16);
  184. out_disable:
  185. pnp_disable_dev(pdev);
  186. out_detach:
  187. pnp_device_detach(pdev);
  188. return error;
  189. }
  190. static void
  191. sb1000_remove_one(struct pnp_dev *pdev)
  192. {
  193. struct net_device *dev = pnp_get_drvdata(pdev);
  194. unregister_netdev(dev);
  195. release_region(dev->base_addr, 16);
  196. release_region(dev->mem_start, 16);
  197. free_netdev(dev);
  198. }
  199. static struct pnp_driver sb1000_driver = {
  200. .name = "sb1000",
  201. .id_table = sb1000_pnp_ids,
  202. .probe = sb1000_probe_one,
  203. .remove = sb1000_remove_one,
  204. };
  205. /*
  206. * SB1000 hardware routines to be used during open/configuration phases
  207. */
  208. static const int TimeOutJiffies = (875 * HZ) / 100;
  209. /* Card Wait For Busy Clear (cannot be used during an interrupt) */
  210. static int
  211. card_wait_for_busy_clear(const int ioaddr[], const char* name)
  212. {
  213. unsigned char a;
  214. unsigned long timeout;
  215. a = inb(ioaddr[0] + 7);
  216. timeout = jiffies + TimeOutJiffies;
  217. while (a & 0x80 || a & 0x40) {
  218. /* a little sleep */
  219. yield();
  220. a = inb(ioaddr[0] + 7);
  221. if (time_after_eq(jiffies, timeout)) {
  222. printk(KERN_WARNING "%s: card_wait_for_busy_clear timeout\n",
  223. name);
  224. return -ETIME;
  225. }
  226. }
  227. return 0;
  228. }
  229. /* Card Wait For Ready (cannot be used during an interrupt) */
  230. static int
  231. card_wait_for_ready(const int ioaddr[], const char* name, unsigned char in[])
  232. {
  233. unsigned char a;
  234. unsigned long timeout;
  235. a = inb(ioaddr[1] + 6);
  236. timeout = jiffies + TimeOutJiffies;
  237. while (a & 0x80 || !(a & 0x40)) {
  238. /* a little sleep */
  239. yield();
  240. a = inb(ioaddr[1] + 6);
  241. if (time_after_eq(jiffies, timeout)) {
  242. printk(KERN_WARNING "%s: card_wait_for_ready timeout\n",
  243. name);
  244. return -ETIME;
  245. }
  246. }
  247. in[1] = inb(ioaddr[0] + 1);
  248. in[2] = inb(ioaddr[0] + 2);
  249. in[3] = inb(ioaddr[0] + 3);
  250. in[4] = inb(ioaddr[0] + 4);
  251. in[0] = inb(ioaddr[0] + 5);
  252. in[6] = inb(ioaddr[0] + 6);
  253. in[5] = inb(ioaddr[1] + 6);
  254. return 0;
  255. }
  256. /* Card Send Command (cannot be used during an interrupt) */
  257. static int
  258. card_send_command(const int ioaddr[], const char* name,
  259. const unsigned char out[], unsigned char in[])
  260. {
  261. int status, x;
  262. if ((status = card_wait_for_busy_clear(ioaddr, name)))
  263. return status;
  264. outb(0xa0, ioaddr[0] + 6);
  265. outb(out[2], ioaddr[0] + 1);
  266. outb(out[3], ioaddr[0] + 2);
  267. outb(out[4], ioaddr[0] + 3);
  268. outb(out[5], ioaddr[0] + 4);
  269. outb(out[1], ioaddr[0] + 5);
  270. outb(0xa0, ioaddr[0] + 6);
  271. outb(out[0], ioaddr[0] + 7);
  272. if (out[0] != 0x20 && out[0] != 0x30) {
  273. if ((status = card_wait_for_ready(ioaddr, name, in)))
  274. return status;
  275. inb(ioaddr[0] + 7);
  276. if (sb1000_debug > 3)
  277. printk(KERN_DEBUG "%s: card_send_command "
  278. "out: %02x%02x%02x%02x%02x%02x "
  279. "in: %02x%02x%02x%02x%02x%02x%02x\n", name,
  280. out[0], out[1], out[2], out[3], out[4], out[5],
  281. in[0], in[1], in[2], in[3], in[4], in[5], in[6]);
  282. } else {
  283. if (sb1000_debug > 3)
  284. printk(KERN_DEBUG "%s: card_send_command "
  285. "out: %02x%02x%02x%02x%02x%02x\n", name,
  286. out[0], out[1], out[2], out[3], out[4], out[5]);
  287. }
  288. if (out[1] == 0x1b) {
  289. x = (out[2] == 0x02);
  290. } else {
  291. if (out[0] >= 0x80 && in[0] != (out[1] | 0x80))
  292. return -EIO;
  293. }
  294. return 0;
  295. }
  296. /*
  297. * SB1000 hardware routines to be used during frame rx interrupt
  298. */
  299. static const int Sb1000TimeOutJiffies = 7 * HZ;
  300. /* Card Wait For Ready (to be used during frame rx) */
  301. static int
  302. sb1000_wait_for_ready(const int ioaddr[], const char* name)
  303. {
  304. unsigned long timeout;
  305. timeout = jiffies + Sb1000TimeOutJiffies;
  306. while (inb(ioaddr[1] + 6) & 0x80) {
  307. if (time_after_eq(jiffies, timeout)) {
  308. printk(KERN_WARNING "%s: sb1000_wait_for_ready timeout\n",
  309. name);
  310. return -ETIME;
  311. }
  312. }
  313. timeout = jiffies + Sb1000TimeOutJiffies;
  314. while (!(inb(ioaddr[1] + 6) & 0x40)) {
  315. if (time_after_eq(jiffies, timeout)) {
  316. printk(KERN_WARNING "%s: sb1000_wait_for_ready timeout\n",
  317. name);
  318. return -ETIME;
  319. }
  320. }
  321. inb(ioaddr[0] + 7);
  322. return 0;
  323. }
  324. /* Card Wait For Ready Clear (to be used during frame rx) */
  325. static int
  326. sb1000_wait_for_ready_clear(const int ioaddr[], const char* name)
  327. {
  328. unsigned long timeout;
  329. timeout = jiffies + Sb1000TimeOutJiffies;
  330. while (inb(ioaddr[1] + 6) & 0x80) {
  331. if (time_after_eq(jiffies, timeout)) {
  332. printk(KERN_WARNING "%s: sb1000_wait_for_ready_clear timeout\n",
  333. name);
  334. return -ETIME;
  335. }
  336. }
  337. timeout = jiffies + Sb1000TimeOutJiffies;
  338. while (inb(ioaddr[1] + 6) & 0x40) {
  339. if (time_after_eq(jiffies, timeout)) {
  340. printk(KERN_WARNING "%s: sb1000_wait_for_ready_clear timeout\n",
  341. name);
  342. return -ETIME;
  343. }
  344. }
  345. return 0;
  346. }
  347. /* Card Send Command (to be used during frame rx) */
  348. static void
  349. sb1000_send_command(const int ioaddr[], const char* name,
  350. const unsigned char out[])
  351. {
  352. outb(out[2], ioaddr[0] + 1);
  353. outb(out[3], ioaddr[0] + 2);
  354. outb(out[4], ioaddr[0] + 3);
  355. outb(out[5], ioaddr[0] + 4);
  356. outb(out[1], ioaddr[0] + 5);
  357. outb(out[0], ioaddr[0] + 7);
  358. if (sb1000_debug > 3)
  359. printk(KERN_DEBUG "%s: sb1000_send_command out: %02x%02x%02x%02x"
  360. "%02x%02x\n", name, out[0], out[1], out[2], out[3], out[4], out[5]);
  361. }
  362. /* Card Read Status (to be used during frame rx) */
  363. static void
  364. sb1000_read_status(const int ioaddr[], unsigned char in[])
  365. {
  366. in[1] = inb(ioaddr[0] + 1);
  367. in[2] = inb(ioaddr[0] + 2);
  368. in[3] = inb(ioaddr[0] + 3);
  369. in[4] = inb(ioaddr[0] + 4);
  370. in[0] = inb(ioaddr[0] + 5);
  371. }
  372. /* Issue Read Command (to be used during frame rx) */
  373. static void
  374. sb1000_issue_read_command(const int ioaddr[], const char* name)
  375. {
  376. static const unsigned char Command0[6] = {0x20, 0x00, 0x00, 0x01, 0x00, 0x00};
  377. sb1000_wait_for_ready_clear(ioaddr, name);
  378. outb(0xa0, ioaddr[0] + 6);
  379. sb1000_send_command(ioaddr, name, Command0);
  380. }
  381. /*
  382. * SB1000 commands for open/configuration
  383. */
  384. /* reset SB1000 card */
  385. static int
  386. sb1000_reset(const int ioaddr[], const char* name)
  387. {
  388. static const unsigned char Command0[6] = {0x80, 0x16, 0x00, 0x00, 0x00, 0x00};
  389. unsigned char st[7];
  390. int port, status;
  391. port = ioaddr[1] + 6;
  392. outb(0x4, port);
  393. inb(port);
  394. udelay(1000);
  395. outb(0x0, port);
  396. inb(port);
  397. ssleep(1);
  398. outb(0x4, port);
  399. inb(port);
  400. udelay(1000);
  401. outb(0x0, port);
  402. inb(port);
  403. udelay(0);
  404. if ((status = card_send_command(ioaddr, name, Command0, st)))
  405. return status;
  406. if (st[3] != 0xf0)
  407. return -EIO;
  408. return 0;
  409. }
  410. /* check SB1000 firmware CRC */
  411. static int
  412. sb1000_check_CRC(const int ioaddr[], const char* name)
  413. {
  414. static const unsigned char Command0[6] = {0x80, 0x1f, 0x00, 0x00, 0x00, 0x00};
  415. unsigned char st[7];
  416. int crc, status;
  417. /* check CRC */
  418. if ((status = card_send_command(ioaddr, name, Command0, st)))
  419. return status;
  420. if (st[1] != st[3] || st[2] != st[4])
  421. return -EIO;
  422. crc = st[1] << 8 | st[2];
  423. return 0;
  424. }
  425. static inline int
  426. sb1000_start_get_set_command(const int ioaddr[], const char* name)
  427. {
  428. static const unsigned char Command0[6] = {0x80, 0x1b, 0x00, 0x00, 0x00, 0x00};
  429. unsigned char st[7];
  430. return card_send_command(ioaddr, name, Command0, st);
  431. }
  432. static int
  433. sb1000_end_get_set_command(const int ioaddr[], const char* name)
  434. {
  435. static const unsigned char Command0[6] = {0x80, 0x1b, 0x02, 0x00, 0x00, 0x00};
  436. static const unsigned char Command1[6] = {0x20, 0x00, 0x00, 0x00, 0x00, 0x00};
  437. unsigned char st[7];
  438. int status;
  439. if ((status = card_send_command(ioaddr, name, Command0, st)))
  440. return status;
  441. return card_send_command(ioaddr, name, Command1, st);
  442. }
  443. static int
  444. sb1000_activate(const int ioaddr[], const char* name)
  445. {
  446. static const unsigned char Command0[6] = {0x80, 0x11, 0x00, 0x00, 0x00, 0x00};
  447. static const unsigned char Command1[6] = {0x80, 0x16, 0x00, 0x00, 0x00, 0x00};
  448. unsigned char st[7];
  449. int status;
  450. ssleep(1);
  451. if ((status = card_send_command(ioaddr, name, Command0, st)))
  452. return status;
  453. if ((status = card_send_command(ioaddr, name, Command1, st)))
  454. return status;
  455. if (st[3] != 0xf1) {
  456. if ((status = sb1000_start_get_set_command(ioaddr, name)))
  457. return status;
  458. return -EIO;
  459. }
  460. udelay(1000);
  461. return sb1000_start_get_set_command(ioaddr, name);
  462. }
  463. /* get SB1000 firmware version */
  464. static int
  465. sb1000_get_firmware_version(const int ioaddr[], const char* name,
  466. unsigned char version[], int do_end)
  467. {
  468. static const unsigned char Command0[6] = {0x80, 0x23, 0x00, 0x00, 0x00, 0x00};
  469. unsigned char st[7];
  470. int status;
  471. if ((status = sb1000_start_get_set_command(ioaddr, name)))
  472. return status;
  473. if ((status = card_send_command(ioaddr, name, Command0, st)))
  474. return status;
  475. if (st[0] != 0xa3)
  476. return -EIO;
  477. version[0] = st[1];
  478. version[1] = st[2];
  479. if (do_end)
  480. return sb1000_end_get_set_command(ioaddr, name);
  481. else
  482. return 0;
  483. }
  484. /* get SB1000 frequency */
  485. static int
  486. sb1000_get_frequency(const int ioaddr[], const char* name, int* frequency)
  487. {
  488. static const unsigned char Command0[6] = {0x80, 0x44, 0x00, 0x00, 0x00, 0x00};
  489. unsigned char st[7];
  490. int status;
  491. udelay(1000);
  492. if ((status = sb1000_start_get_set_command(ioaddr, name)))
  493. return status;
  494. if ((status = card_send_command(ioaddr, name, Command0, st)))
  495. return status;
  496. *frequency = ((st[1] << 8 | st[2]) << 8 | st[3]) << 8 | st[4];
  497. return sb1000_end_get_set_command(ioaddr, name);
  498. }
  499. /* set SB1000 frequency */
  500. static int
  501. sb1000_set_frequency(const int ioaddr[], const char* name, int frequency)
  502. {
  503. unsigned char st[7];
  504. int status;
  505. unsigned char Command0[6] = {0x80, 0x29, 0x00, 0x00, 0x00, 0x00};
  506. const int FrequencyLowerLimit = 57000;
  507. const int FrequencyUpperLimit = 804000;
  508. if (frequency < FrequencyLowerLimit || frequency > FrequencyUpperLimit) {
  509. printk(KERN_ERR "%s: frequency chosen (%d kHz) is not in the range "
  510. "[%d,%d] kHz\n", name, frequency, FrequencyLowerLimit,
  511. FrequencyUpperLimit);
  512. return -EINVAL;
  513. }
  514. udelay(1000);
  515. if ((status = sb1000_start_get_set_command(ioaddr, name)))
  516. return status;
  517. Command0[5] = frequency & 0xff;
  518. frequency >>= 8;
  519. Command0[4] = frequency & 0xff;
  520. frequency >>= 8;
  521. Command0[3] = frequency & 0xff;
  522. frequency >>= 8;
  523. Command0[2] = frequency & 0xff;
  524. return card_send_command(ioaddr, name, Command0, st);
  525. }
  526. /* get SB1000 PIDs */
  527. static int
  528. sb1000_get_PIDs(const int ioaddr[], const char* name, short PID[])
  529. {
  530. static const unsigned char Command0[6] = {0x80, 0x40, 0x00, 0x00, 0x00, 0x00};
  531. static const unsigned char Command1[6] = {0x80, 0x41, 0x00, 0x00, 0x00, 0x00};
  532. static const unsigned char Command2[6] = {0x80, 0x42, 0x00, 0x00, 0x00, 0x00};
  533. static const unsigned char Command3[6] = {0x80, 0x43, 0x00, 0x00, 0x00, 0x00};
  534. unsigned char st[7];
  535. int status;
  536. udelay(1000);
  537. if ((status = sb1000_start_get_set_command(ioaddr, name)))
  538. return status;
  539. if ((status = card_send_command(ioaddr, name, Command0, st)))
  540. return status;
  541. PID[0] = st[1] << 8 | st[2];
  542. if ((status = card_send_command(ioaddr, name, Command1, st)))
  543. return status;
  544. PID[1] = st[1] << 8 | st[2];
  545. if ((status = card_send_command(ioaddr, name, Command2, st)))
  546. return status;
  547. PID[2] = st[1] << 8 | st[2];
  548. if ((status = card_send_command(ioaddr, name, Command3, st)))
  549. return status;
  550. PID[3] = st[1] << 8 | st[2];
  551. return sb1000_end_get_set_command(ioaddr, name);
  552. }
  553. /* set SB1000 PIDs */
  554. static int
  555. sb1000_set_PIDs(const int ioaddr[], const char* name, const short PID[])
  556. {
  557. static const unsigned char Command4[6] = {0x80, 0x2e, 0x00, 0x00, 0x00, 0x00};
  558. unsigned char st[7];
  559. short p;
  560. int status;
  561. unsigned char Command0[6] = {0x80, 0x31, 0x00, 0x00, 0x00, 0x00};
  562. unsigned char Command1[6] = {0x80, 0x32, 0x00, 0x00, 0x00, 0x00};
  563. unsigned char Command2[6] = {0x80, 0x33, 0x00, 0x00, 0x00, 0x00};
  564. unsigned char Command3[6] = {0x80, 0x34, 0x00, 0x00, 0x00, 0x00};
  565. udelay(1000);
  566. if ((status = sb1000_start_get_set_command(ioaddr, name)))
  567. return status;
  568. p = PID[0];
  569. Command0[3] = p & 0xff;
  570. p >>= 8;
  571. Command0[2] = p & 0xff;
  572. if ((status = card_send_command(ioaddr, name, Command0, st)))
  573. return status;
  574. p = PID[1];
  575. Command1[3] = p & 0xff;
  576. p >>= 8;
  577. Command1[2] = p & 0xff;
  578. if ((status = card_send_command(ioaddr, name, Command1, st)))
  579. return status;
  580. p = PID[2];
  581. Command2[3] = p & 0xff;
  582. p >>= 8;
  583. Command2[2] = p & 0xff;
  584. if ((status = card_send_command(ioaddr, name, Command2, st)))
  585. return status;
  586. p = PID[3];
  587. Command3[3] = p & 0xff;
  588. p >>= 8;
  589. Command3[2] = p & 0xff;
  590. if ((status = card_send_command(ioaddr, name, Command3, st)))
  591. return status;
  592. if ((status = card_send_command(ioaddr, name, Command4, st)))
  593. return status;
  594. return sb1000_end_get_set_command(ioaddr, name);
  595. }
  596. static void
  597. sb1000_print_status_buffer(const char* name, unsigned char st[],
  598. unsigned char buffer[], int size)
  599. {
  600. int i, j, k;
  601. printk(KERN_DEBUG "%s: status: %02x %02x\n", name, st[0], st[1]);
  602. if (buffer[24] == 0x08 && buffer[25] == 0x00 && buffer[26] == 0x45) {
  603. printk(KERN_DEBUG "%s: length: %d protocol: %d from: %d.%d.%d.%d:%d "
  604. "to %d.%d.%d.%d:%d\n", name, buffer[28] << 8 | buffer[29],
  605. buffer[35], buffer[38], buffer[39], buffer[40], buffer[41],
  606. buffer[46] << 8 | buffer[47],
  607. buffer[42], buffer[43], buffer[44], buffer[45],
  608. buffer[48] << 8 | buffer[49]);
  609. } else {
  610. for (i = 0, k = 0; i < (size + 7) / 8; i++) {
  611. printk(KERN_DEBUG "%s: %s", name, i ? " " : "buffer:");
  612. for (j = 0; j < 8 && k < size; j++, k++)
  613. printk(" %02x", buffer[k]);
  614. printk("\n");
  615. }
  616. }
  617. }
  618. /*
  619. * SB1000 commands for frame rx interrupt
  620. */
  621. /* receive a single frame and assemble datagram
  622. * (this is the heart of the interrupt routine)
  623. */
  624. static int
  625. sb1000_rx(struct net_device *dev)
  626. {
  627. #define FRAMESIZE 184
  628. unsigned char st[2], buffer[FRAMESIZE], session_id, frame_id;
  629. short dlen;
  630. int ioaddr, ns;
  631. unsigned int skbsize;
  632. struct sk_buff *skb;
  633. struct sb1000_private *lp = netdev_priv(dev);
  634. struct net_device_stats *stats = &dev->stats;
  635. /* SB1000 frame constants */
  636. const int FrameSize = FRAMESIZE;
  637. const int NewDatagramHeaderSkip = 8;
  638. const int NewDatagramHeaderSize = NewDatagramHeaderSkip + 18;
  639. const int NewDatagramDataSize = FrameSize - NewDatagramHeaderSize;
  640. const int ContDatagramHeaderSkip = 7;
  641. const int ContDatagramHeaderSize = ContDatagramHeaderSkip + 1;
  642. const int ContDatagramDataSize = FrameSize - ContDatagramHeaderSize;
  643. const int TrailerSize = 4;
  644. ioaddr = dev->base_addr;
  645. insw(ioaddr, (unsigned short*) st, 1);
  646. #ifdef XXXDEBUG
  647. printk("cm0: received: %02x %02x\n", st[0], st[1]);
  648. #endif /* XXXDEBUG */
  649. lp->rx_frames++;
  650. /* decide if it is a good or bad frame */
  651. for (ns = 0; ns < NPIDS; ns++) {
  652. session_id = lp->rx_session_id[ns];
  653. frame_id = lp->rx_frame_id[ns];
  654. if (st[0] == session_id) {
  655. if (st[1] == frame_id || (!frame_id && (st[1] & 0xf0) == 0x30)) {
  656. goto good_frame;
  657. } else if ((st[1] & 0xf0) == 0x30 && (st[0] & 0x40)) {
  658. goto skipped_frame;
  659. } else {
  660. goto bad_frame;
  661. }
  662. } else if (st[0] == (session_id | 0x40)) {
  663. if ((st[1] & 0xf0) == 0x30) {
  664. goto skipped_frame;
  665. } else {
  666. goto bad_frame;
  667. }
  668. }
  669. }
  670. goto bad_frame;
  671. skipped_frame:
  672. stats->rx_frame_errors++;
  673. skb = lp->rx_skb[ns];
  674. if (sb1000_debug > 1)
  675. printk(KERN_WARNING "%s: missing frame(s): got %02x %02x "
  676. "expecting %02x %02x\n", dev->name, st[0], st[1],
  677. skb ? session_id : session_id | 0x40, frame_id);
  678. if (skb) {
  679. dev_kfree_skb(skb);
  680. skb = NULL;
  681. }
  682. good_frame:
  683. lp->rx_frame_id[ns] = 0x30 | ((st[1] + 1) & 0x0f);
  684. /* new datagram */
  685. if (st[0] & 0x40) {
  686. /* get data length */
  687. insw(ioaddr, buffer, NewDatagramHeaderSize / 2);
  688. #ifdef XXXDEBUG
  689. printk("cm0: IP identification: %02x%02x fragment offset: %02x%02x\n", buffer[30], buffer[31], buffer[32], buffer[33]);
  690. #endif /* XXXDEBUG */
  691. if (buffer[0] != NewDatagramHeaderSkip) {
  692. if (sb1000_debug > 1)
  693. printk(KERN_WARNING "%s: new datagram header skip error: "
  694. "got %02x expecting %02x\n", dev->name, buffer[0],
  695. NewDatagramHeaderSkip);
  696. stats->rx_length_errors++;
  697. insw(ioaddr, buffer, NewDatagramDataSize / 2);
  698. goto bad_frame_next;
  699. }
  700. dlen = ((buffer[NewDatagramHeaderSkip + 3] & 0x0f) << 8 |
  701. buffer[NewDatagramHeaderSkip + 4]) - 17;
  702. if (dlen > SB1000_MRU) {
  703. if (sb1000_debug > 1)
  704. printk(KERN_WARNING "%s: datagram length (%d) greater "
  705. "than MRU (%d)\n", dev->name, dlen, SB1000_MRU);
  706. stats->rx_length_errors++;
  707. insw(ioaddr, buffer, NewDatagramDataSize / 2);
  708. goto bad_frame_next;
  709. }
  710. lp->rx_dlen[ns] = dlen;
  711. /* compute size to allocate for datagram */
  712. skbsize = dlen + FrameSize;
  713. if ((skb = alloc_skb(skbsize, GFP_ATOMIC)) == NULL) {
  714. if (sb1000_debug > 1)
  715. printk(KERN_WARNING "%s: can't allocate %d bytes long "
  716. "skbuff\n", dev->name, skbsize);
  717. stats->rx_dropped++;
  718. insw(ioaddr, buffer, NewDatagramDataSize / 2);
  719. goto dropped_frame;
  720. }
  721. skb->dev = dev;
  722. skb_reset_mac_header(skb);
  723. skb->protocol = (unsigned short) buffer[NewDatagramHeaderSkip + 16];
  724. insw(ioaddr, skb_put(skb, NewDatagramDataSize),
  725. NewDatagramDataSize / 2);
  726. lp->rx_skb[ns] = skb;
  727. } else {
  728. /* continuation of previous datagram */
  729. insw(ioaddr, buffer, ContDatagramHeaderSize / 2);
  730. if (buffer[0] != ContDatagramHeaderSkip) {
  731. if (sb1000_debug > 1)
  732. printk(KERN_WARNING "%s: cont datagram header skip error: "
  733. "got %02x expecting %02x\n", dev->name, buffer[0],
  734. ContDatagramHeaderSkip);
  735. stats->rx_length_errors++;
  736. insw(ioaddr, buffer, ContDatagramDataSize / 2);
  737. goto bad_frame_next;
  738. }
  739. skb = lp->rx_skb[ns];
  740. insw(ioaddr, skb_put(skb, ContDatagramDataSize),
  741. ContDatagramDataSize / 2);
  742. dlen = lp->rx_dlen[ns];
  743. }
  744. if (skb->len < dlen + TrailerSize) {
  745. lp->rx_session_id[ns] &= ~0x40;
  746. return 0;
  747. }
  748. /* datagram completed: send to upper level */
  749. skb_trim(skb, dlen);
  750. netif_rx(skb);
  751. stats->rx_bytes+=dlen;
  752. stats->rx_packets++;
  753. lp->rx_skb[ns] = NULL;
  754. lp->rx_session_id[ns] |= 0x40;
  755. return 0;
  756. bad_frame:
  757. insw(ioaddr, buffer, FrameSize / 2);
  758. if (sb1000_debug > 1)
  759. printk(KERN_WARNING "%s: frame error: got %02x %02x\n",
  760. dev->name, st[0], st[1]);
  761. stats->rx_frame_errors++;
  762. bad_frame_next:
  763. if (sb1000_debug > 2)
  764. sb1000_print_status_buffer(dev->name, st, buffer, FrameSize);
  765. dropped_frame:
  766. stats->rx_errors++;
  767. if (ns < NPIDS) {
  768. if ((skb = lp->rx_skb[ns])) {
  769. dev_kfree_skb(skb);
  770. lp->rx_skb[ns] = NULL;
  771. }
  772. lp->rx_session_id[ns] |= 0x40;
  773. }
  774. return -1;
  775. }
  776. static void
  777. sb1000_error_dpc(struct net_device *dev)
  778. {
  779. static const unsigned char Command0[6] = {0x80, 0x26, 0x00, 0x00, 0x00, 0x00};
  780. char *name;
  781. unsigned char st[5];
  782. int ioaddr[2];
  783. struct sb1000_private *lp = netdev_priv(dev);
  784. const int ErrorDpcCounterInitialize = 200;
  785. ioaddr[0] = dev->base_addr;
  786. /* mem_start holds the second I/O address */
  787. ioaddr[1] = dev->mem_start;
  788. name = dev->name;
  789. sb1000_wait_for_ready_clear(ioaddr, name);
  790. sb1000_send_command(ioaddr, name, Command0);
  791. sb1000_wait_for_ready(ioaddr, name);
  792. sb1000_read_status(ioaddr, st);
  793. if (st[1] & 0x10)
  794. lp->rx_error_dpc_count = ErrorDpcCounterInitialize;
  795. }
  796. /*
  797. * Linux interface functions
  798. */
  799. static int
  800. sb1000_open(struct net_device *dev)
  801. {
  802. char *name;
  803. int ioaddr[2], status;
  804. struct sb1000_private *lp = netdev_priv(dev);
  805. const unsigned short FirmwareVersion[] = {0x01, 0x01};
  806. ioaddr[0] = dev->base_addr;
  807. /* mem_start holds the second I/O address */
  808. ioaddr[1] = dev->mem_start;
  809. name = dev->name;
  810. /* initialize sb1000 */
  811. if ((status = sb1000_reset(ioaddr, name)))
  812. return status;
  813. ssleep(1);
  814. if ((status = sb1000_check_CRC(ioaddr, name)))
  815. return status;
  816. /* initialize private data before board can catch interrupts */
  817. lp->rx_skb[0] = NULL;
  818. lp->rx_skb[1] = NULL;
  819. lp->rx_skb[2] = NULL;
  820. lp->rx_skb[3] = NULL;
  821. lp->rx_dlen[0] = 0;
  822. lp->rx_dlen[1] = 0;
  823. lp->rx_dlen[2] = 0;
  824. lp->rx_dlen[3] = 0;
  825. lp->rx_frames = 0;
  826. lp->rx_error_count = 0;
  827. lp->rx_error_dpc_count = 0;
  828. lp->rx_session_id[0] = 0x50;
  829. lp->rx_session_id[1] = 0x48;
  830. lp->rx_session_id[2] = 0x44;
  831. lp->rx_session_id[3] = 0x42;
  832. lp->rx_frame_id[0] = 0;
  833. lp->rx_frame_id[1] = 0;
  834. lp->rx_frame_id[2] = 0;
  835. lp->rx_frame_id[3] = 0;
  836. if (request_irq(dev->irq, sb1000_interrupt, 0, "sb1000", dev)) {
  837. return -EAGAIN;
  838. }
  839. if (sb1000_debug > 2)
  840. printk(KERN_DEBUG "%s: Opening, IRQ %d\n", name, dev->irq);
  841. /* Activate board and check firmware version */
  842. udelay(1000);
  843. if ((status = sb1000_activate(ioaddr, name)))
  844. return status;
  845. udelay(0);
  846. if ((status = sb1000_get_firmware_version(ioaddr, name, version, 0)))
  847. return status;
  848. if (version[0] != FirmwareVersion[0] || version[1] != FirmwareVersion[1])
  849. printk(KERN_WARNING "%s: found firmware version %x.%02x "
  850. "(should be %x.%02x)\n", name, version[0], version[1],
  851. FirmwareVersion[0], FirmwareVersion[1]);
  852. netif_start_queue(dev);
  853. return 0; /* Always succeed */
  854. }
  855. static int sb1000_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  856. {
  857. char* name;
  858. unsigned char version[2];
  859. short PID[4];
  860. int ioaddr[2], status, frequency;
  861. unsigned int stats[5];
  862. struct sb1000_private *lp = netdev_priv(dev);
  863. if (!(dev && dev->flags & IFF_UP))
  864. return -ENODEV;
  865. ioaddr[0] = dev->base_addr;
  866. /* mem_start holds the second I/O address */
  867. ioaddr[1] = dev->mem_start;
  868. name = dev->name;
  869. switch (cmd) {
  870. case SIOCGCMSTATS: /* get statistics */
  871. stats[0] = dev->stats.rx_bytes;
  872. stats[1] = lp->rx_frames;
  873. stats[2] = dev->stats.rx_packets;
  874. stats[3] = dev->stats.rx_errors;
  875. stats[4] = dev->stats.rx_dropped;
  876. if(copy_to_user(ifr->ifr_data, stats, sizeof(stats)))
  877. return -EFAULT;
  878. status = 0;
  879. break;
  880. case SIOCGCMFIRMWARE: /* get firmware version */
  881. if ((status = sb1000_get_firmware_version(ioaddr, name, version, 1)))
  882. return status;
  883. if(copy_to_user(ifr->ifr_data, version, sizeof(version)))
  884. return -EFAULT;
  885. break;
  886. case SIOCGCMFREQUENCY: /* get frequency */
  887. if ((status = sb1000_get_frequency(ioaddr, name, &frequency)))
  888. return status;
  889. if(put_user(frequency, (int __user *) ifr->ifr_data))
  890. return -EFAULT;
  891. break;
  892. case SIOCSCMFREQUENCY: /* set frequency */
  893. if (!capable(CAP_NET_ADMIN))
  894. return -EPERM;
  895. if(get_user(frequency, (int __user *) ifr->ifr_data))
  896. return -EFAULT;
  897. if ((status = sb1000_set_frequency(ioaddr, name, frequency)))
  898. return status;
  899. break;
  900. case SIOCGCMPIDS: /* get PIDs */
  901. if ((status = sb1000_get_PIDs(ioaddr, name, PID)))
  902. return status;
  903. if(copy_to_user(ifr->ifr_data, PID, sizeof(PID)))
  904. return -EFAULT;
  905. break;
  906. case SIOCSCMPIDS: /* set PIDs */
  907. if (!capable(CAP_NET_ADMIN))
  908. return -EPERM;
  909. if(copy_from_user(PID, ifr->ifr_data, sizeof(PID)))
  910. return -EFAULT;
  911. if ((status = sb1000_set_PIDs(ioaddr, name, PID)))
  912. return status;
  913. /* set session_id, frame_id and pkt_type too */
  914. lp->rx_session_id[0] = 0x50 | (PID[0] & 0x0f);
  915. lp->rx_session_id[1] = 0x48;
  916. lp->rx_session_id[2] = 0x44;
  917. lp->rx_session_id[3] = 0x42;
  918. lp->rx_frame_id[0] = 0;
  919. lp->rx_frame_id[1] = 0;
  920. lp->rx_frame_id[2] = 0;
  921. lp->rx_frame_id[3] = 0;
  922. break;
  923. default:
  924. status = -EINVAL;
  925. break;
  926. }
  927. return status;
  928. }
  929. /* transmit function: do nothing since SB1000 can't send anything out */
  930. static netdev_tx_t
  931. sb1000_start_xmit(struct sk_buff *skb, struct net_device *dev)
  932. {
  933. printk(KERN_WARNING "%s: trying to transmit!!!\n", dev->name);
  934. /* sb1000 can't xmit datagrams */
  935. dev_kfree_skb(skb);
  936. return NETDEV_TX_OK;
  937. }
  938. /* SB1000 interrupt handler. */
  939. static irqreturn_t sb1000_interrupt(int irq, void *dev_id)
  940. {
  941. static const unsigned char Command0[6] = {0x80, 0x2c, 0x00, 0x00, 0x00, 0x00};
  942. static const unsigned char Command1[6] = {0x80, 0x2e, 0x00, 0x00, 0x00, 0x00};
  943. char *name;
  944. unsigned char st;
  945. int ioaddr[2];
  946. struct net_device *dev = dev_id;
  947. struct sb1000_private *lp = netdev_priv(dev);
  948. const int MaxRxErrorCount = 6;
  949. ioaddr[0] = dev->base_addr;
  950. /* mem_start holds the second I/O address */
  951. ioaddr[1] = dev->mem_start;
  952. name = dev->name;
  953. /* is it a good interrupt? */
  954. st = inb(ioaddr[1] + 6);
  955. if (!(st & 0x08 && st & 0x20)) {
  956. return IRQ_NONE;
  957. }
  958. if (sb1000_debug > 3)
  959. printk(KERN_DEBUG "%s: entering interrupt\n", dev->name);
  960. st = inb(ioaddr[0] + 7);
  961. if (sb1000_rx(dev))
  962. lp->rx_error_count++;
  963. #ifdef SB1000_DELAY
  964. udelay(SB1000_DELAY);
  965. #endif /* SB1000_DELAY */
  966. sb1000_issue_read_command(ioaddr, name);
  967. if (st & 0x01) {
  968. sb1000_error_dpc(dev);
  969. sb1000_issue_read_command(ioaddr, name);
  970. }
  971. if (lp->rx_error_dpc_count && !(--lp->rx_error_dpc_count)) {
  972. sb1000_wait_for_ready_clear(ioaddr, name);
  973. sb1000_send_command(ioaddr, name, Command0);
  974. sb1000_wait_for_ready(ioaddr, name);
  975. sb1000_issue_read_command(ioaddr, name);
  976. }
  977. if (lp->rx_error_count >= MaxRxErrorCount) {
  978. sb1000_wait_for_ready_clear(ioaddr, name);
  979. sb1000_send_command(ioaddr, name, Command1);
  980. sb1000_wait_for_ready(ioaddr, name);
  981. sb1000_issue_read_command(ioaddr, name);
  982. lp->rx_error_count = 0;
  983. }
  984. return IRQ_HANDLED;
  985. }
  986. static int sb1000_close(struct net_device *dev)
  987. {
  988. int i;
  989. int ioaddr[2];
  990. struct sb1000_private *lp = netdev_priv(dev);
  991. if (sb1000_debug > 2)
  992. printk(KERN_DEBUG "%s: Shutting down sb1000.\n", dev->name);
  993. netif_stop_queue(dev);
  994. ioaddr[0] = dev->base_addr;
  995. /* mem_start holds the second I/O address */
  996. ioaddr[1] = dev->mem_start;
  997. free_irq(dev->irq, dev);
  998. /* If we don't do this, we can't re-insmod it later. */
  999. release_region(ioaddr[1], SB1000_IO_EXTENT);
  1000. release_region(ioaddr[0], SB1000_IO_EXTENT);
  1001. /* free rx_skb's if needed */
  1002. for (i=0; i<4; i++) {
  1003. if (lp->rx_skb[i]) {
  1004. dev_kfree_skb(lp->rx_skb[i]);
  1005. }
  1006. }
  1007. return 0;
  1008. }
  1009. MODULE_AUTHOR("Franco Venturi <fventuri@mediaone.net>");
  1010. MODULE_DESCRIPTION("General Instruments SB1000 driver");
  1011. MODULE_LICENSE("GPL");
  1012. module_pnp_driver(sb1000_driver);