ksdazzle-sir.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  1. /*****************************************************************************
  2. *
  3. * Filename: ksdazzle.c
  4. * Version: 0.1.2
  5. * Description: Irda KingSun Dazzle USB Dongle
  6. * Status: Experimental
  7. * Author: Alex Villacís Lasso <a_villacis@palosanto.com>
  8. *
  9. * Based on stir4200, mcs7780, kingsun-sir drivers.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. *
  24. *****************************************************************************/
  25. /*
  26. * Following is my most current (2007-07-26) understanding of how the Kingsun
  27. * 07D0:4100 dongle (sometimes known as the MA-660) is supposed to work. This
  28. * information was deduced by examining the USB traffic captured with USBSnoopy
  29. * from the WinXP driver. Feel free to update here as more of the dongle is
  30. * known.
  31. *
  32. * General: This dongle exposes one interface with two interrupt endpoints, one
  33. * IN and one OUT. In this regard, it is similar to what the Kingsun/Donshine
  34. * dongle (07c0:4200) exposes. Traffic is raw and needs to be wrapped and
  35. * unwrapped manually as in stir4200, kingsun-sir, and ks959-sir.
  36. *
  37. * Transmission: To transmit an IrDA frame, it is necessary to wrap it, then
  38. * split it into multiple segments of up to 7 bytes each, and transmit each in
  39. * sequence. It seems that sending a single big block (like kingsun-sir does)
  40. * won't work with this dongle. Each segment needs to be prefixed with a value
  41. * equal to (unsigned char)0xF8 + <number of bytes in segment>, inside a payload
  42. * of exactly 8 bytes. For example, a segment of 1 byte gets prefixed by 0xF9,
  43. * and one of 7 bytes gets prefixed by 0xFF. The bytes at the end of the
  44. * payload, not considered by the prefix, are ignored (set to 0 by this
  45. * implementation).
  46. *
  47. * Reception: To receive data, the driver must poll the dongle regularly (like
  48. * kingsun-sir.c) with interrupt URBs. If data is available, it will be returned
  49. * in payloads from 0 to 8 bytes long. When concatenated, these payloads form
  50. * a raw IrDA stream that needs to be unwrapped as in stir4200 and kingsun-sir
  51. *
  52. * Speed change: To change the speed of the dongle, the driver prepares a
  53. * control URB with the following as a setup packet:
  54. * bRequestType USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE
  55. * bRequest 0x09
  56. * wValue 0x0200
  57. * wIndex 0x0001
  58. * wLength 0x0008 (length of the payload)
  59. * The payload is a 8-byte record, apparently identical to the one used in
  60. * drivers/usb/serial/cypress_m8.c to change speed:
  61. * __u32 baudSpeed;
  62. * unsigned int dataBits : 2; // 0 - 5 bits 3 - 8 bits
  63. * unsigned int : 1;
  64. * unsigned int stopBits : 1;
  65. * unsigned int parityEnable : 1;
  66. * unsigned int parityType : 1;
  67. * unsigned int : 1;
  68. * unsigned int reset : 1;
  69. * unsigned char reserved[3]; // set to 0
  70. *
  71. * For now only SIR speeds have been observed with this dongle. Therefore,
  72. * nothing is known on what changes (if any) must be done to frame wrapping /
  73. * unwrapping for higher than SIR speeds. This driver assumes no change is
  74. * necessary and announces support for all the way to 115200 bps.
  75. */
  76. #include <linux/module.h>
  77. #include <linux/moduleparam.h>
  78. #include <linux/kernel.h>
  79. #include <linux/types.h>
  80. #include <linux/errno.h>
  81. #include <linux/init.h>
  82. #include <linux/slab.h>
  83. #include <linux/kref.h>
  84. #include <linux/usb.h>
  85. #include <linux/device.h>
  86. #include <linux/crc32.h>
  87. #include <asm/unaligned.h>
  88. #include <asm/byteorder.h>
  89. #include <asm/uaccess.h>
  90. #include <net/irda/irda.h>
  91. #include <net/irda/wrapper.h>
  92. #include <net/irda/crc.h>
  93. #define KSDAZZLE_VENDOR_ID 0x07d0
  94. #define KSDAZZLE_PRODUCT_ID 0x4100
  95. /* These are the currently known USB ids */
  96. static struct usb_device_id dongles[] = {
  97. /* KingSun Co,Ltd IrDA/USB Bridge */
  98. {USB_DEVICE(KSDAZZLE_VENDOR_ID, KSDAZZLE_PRODUCT_ID)},
  99. {}
  100. };
  101. MODULE_DEVICE_TABLE(usb, dongles);
  102. #define KINGSUN_MTT 0x07
  103. #define KINGSUN_REQ_RECV 0x01
  104. #define KINGSUN_REQ_SEND 0x09
  105. #define KINGSUN_SND_FIFO_SIZE 2048 /* Max packet we can send */
  106. #define KINGSUN_RCV_MAX 2048 /* Max transfer we can receive */
  107. struct ksdazzle_speedparams {
  108. __le32 baudrate; /* baud rate, little endian */
  109. __u8 flags;
  110. __u8 reserved[3];
  111. } __attribute__ ((packed));
  112. #define KS_DATA_5_BITS 0x00
  113. #define KS_DATA_6_BITS 0x01
  114. #define KS_DATA_7_BITS 0x02
  115. #define KS_DATA_8_BITS 0x03
  116. #define KS_STOP_BITS_1 0x00
  117. #define KS_STOP_BITS_2 0x08
  118. #define KS_PAR_DISABLE 0x00
  119. #define KS_PAR_EVEN 0x10
  120. #define KS_PAR_ODD 0x30
  121. #define KS_RESET 0x80
  122. #define KINGSUN_EP_IN 0
  123. #define KINGSUN_EP_OUT 1
  124. struct ksdazzle_cb {
  125. struct usb_device *usbdev; /* init: probe_irda */
  126. struct net_device *netdev; /* network layer */
  127. struct irlap_cb *irlap; /* The link layer we are binded to */
  128. struct net_device_stats stats; /* network statistics */
  129. struct qos_info qos;
  130. struct urb *tx_urb;
  131. __u8 *tx_buf_clear;
  132. unsigned int tx_buf_clear_used;
  133. unsigned int tx_buf_clear_sent;
  134. __u8 tx_payload[8];
  135. struct urb *rx_urb;
  136. __u8 *rx_buf;
  137. iobuff_t rx_unwrap_buff;
  138. struct usb_ctrlrequest *speed_setuprequest;
  139. struct urb *speed_urb;
  140. struct ksdazzle_speedparams speedparams;
  141. unsigned int new_speed;
  142. __u8 ep_in;
  143. __u8 ep_out;
  144. spinlock_t lock;
  145. int receiving;
  146. };
  147. /* Callback transmission routine */
  148. static void ksdazzle_speed_irq(struct urb *urb)
  149. {
  150. /* unlink, shutdown, unplug, other nasties */
  151. if (urb->status != 0) {
  152. err("ksdazzle_speed_irq: urb asynchronously failed - %d",
  153. urb->status);
  154. }
  155. }
  156. /* Send a control request to change speed of the dongle */
  157. static int ksdazzle_change_speed(struct ksdazzle_cb *kingsun, unsigned speed)
  158. {
  159. static unsigned int supported_speeds[] = { 2400, 9600, 19200, 38400,
  160. 57600, 115200, 576000, 1152000, 4000000, 0
  161. };
  162. int err;
  163. unsigned int i;
  164. if (kingsun->speed_setuprequest == NULL || kingsun->speed_urb == NULL)
  165. return -ENOMEM;
  166. /* Check that requested speed is among the supported ones */
  167. for (i = 0; supported_speeds[i] && supported_speeds[i] != speed; i++) ;
  168. if (supported_speeds[i] == 0)
  169. return -EOPNOTSUPP;
  170. memset(&(kingsun->speedparams), 0, sizeof(struct ksdazzle_speedparams));
  171. kingsun->speedparams.baudrate = cpu_to_le32(speed);
  172. kingsun->speedparams.flags = KS_DATA_8_BITS;
  173. /* speed_setuprequest pre-filled in ksdazzle_probe */
  174. usb_fill_control_urb(kingsun->speed_urb, kingsun->usbdev,
  175. usb_sndctrlpipe(kingsun->usbdev, 0),
  176. (unsigned char *)kingsun->speed_setuprequest,
  177. &(kingsun->speedparams),
  178. sizeof(struct ksdazzle_speedparams),
  179. ksdazzle_speed_irq, kingsun);
  180. kingsun->speed_urb->status = 0;
  181. err = usb_submit_urb(kingsun->speed_urb, GFP_ATOMIC);
  182. return err;
  183. }
  184. /* Submit one fragment of an IrDA frame to the dongle */
  185. static void ksdazzle_send_irq(struct urb *urb);
  186. static int ksdazzle_submit_tx_fragment(struct ksdazzle_cb *kingsun)
  187. {
  188. unsigned int wraplen;
  189. int ret;
  190. /* We can send at most 7 bytes of payload at a time */
  191. wraplen = 7;
  192. if (wraplen > kingsun->tx_buf_clear_used)
  193. wraplen = kingsun->tx_buf_clear_used;
  194. /* Prepare payload prefix with used length */
  195. memset(kingsun->tx_payload, 0, 8);
  196. kingsun->tx_payload[0] = (unsigned char)0xf8 + wraplen;
  197. memcpy(kingsun->tx_payload + 1, kingsun->tx_buf_clear, wraplen);
  198. usb_fill_int_urb(kingsun->tx_urb, kingsun->usbdev,
  199. usb_sndintpipe(kingsun->usbdev, kingsun->ep_out),
  200. kingsun->tx_payload, 8, ksdazzle_send_irq, kingsun, 1);
  201. kingsun->tx_urb->status = 0;
  202. ret = usb_submit_urb(kingsun->tx_urb, GFP_ATOMIC);
  203. /* Remember how much data was sent, in order to update at callback */
  204. kingsun->tx_buf_clear_sent = (ret == 0) ? wraplen : 0;
  205. return ret;
  206. }
  207. /* Callback transmission routine */
  208. static void ksdazzle_send_irq(struct urb *urb)
  209. {
  210. struct ksdazzle_cb *kingsun = urb->context;
  211. struct net_device *netdev = kingsun->netdev;
  212. int ret = 0;
  213. /* in process of stopping, just drop data */
  214. if (!netif_running(kingsun->netdev)) {
  215. err("ksdazzle_send_irq: Network not running!");
  216. return;
  217. }
  218. /* unlink, shutdown, unplug, other nasties */
  219. if (urb->status != 0) {
  220. err("ksdazzle_send_irq: urb asynchronously failed - %d",
  221. urb->status);
  222. return;
  223. }
  224. if (kingsun->tx_buf_clear_used > 0) {
  225. /* Update data remaining to be sent */
  226. if (kingsun->tx_buf_clear_sent < kingsun->tx_buf_clear_used) {
  227. memmove(kingsun->tx_buf_clear,
  228. kingsun->tx_buf_clear +
  229. kingsun->tx_buf_clear_sent,
  230. kingsun->tx_buf_clear_used -
  231. kingsun->tx_buf_clear_sent);
  232. }
  233. kingsun->tx_buf_clear_used -= kingsun->tx_buf_clear_sent;
  234. kingsun->tx_buf_clear_sent = 0;
  235. if (kingsun->tx_buf_clear_used > 0) {
  236. /* There is more data to be sent */
  237. if ((ret = ksdazzle_submit_tx_fragment(kingsun)) != 0) {
  238. err("ksdazzle_send_irq: failed tx_urb submit: %d", ret);
  239. switch (ret) {
  240. case -ENODEV:
  241. case -EPIPE:
  242. break;
  243. default:
  244. kingsun->stats.tx_errors++;
  245. netif_start_queue(netdev);
  246. }
  247. }
  248. } else {
  249. /* All data sent, send next speed && wake network queue */
  250. if (kingsun->new_speed != -1 &&
  251. cpu_to_le32(kingsun->new_speed) !=
  252. kingsun->speedparams.baudrate)
  253. ksdazzle_change_speed(kingsun,
  254. kingsun->new_speed);
  255. netif_wake_queue(netdev);
  256. }
  257. }
  258. }
  259. /*
  260. * Called from net/core when new frame is available.
  261. */
  262. static int ksdazzle_hard_xmit(struct sk_buff *skb, struct net_device *netdev)
  263. {
  264. struct ksdazzle_cb *kingsun;
  265. unsigned int wraplen;
  266. int ret = 0;
  267. if (skb == NULL || netdev == NULL)
  268. return -EINVAL;
  269. netif_stop_queue(netdev);
  270. /* the IRDA wrapping routines don't deal with non linear skb */
  271. SKB_LINEAR_ASSERT(skb);
  272. kingsun = netdev_priv(netdev);
  273. spin_lock(&kingsun->lock);
  274. kingsun->new_speed = irda_get_next_speed(skb);
  275. /* Append data to the end of whatever data remains to be transmitted */
  276. wraplen =
  277. async_wrap_skb(skb, kingsun->tx_buf_clear, KINGSUN_SND_FIFO_SIZE);
  278. kingsun->tx_buf_clear_used = wraplen;
  279. if ((ret = ksdazzle_submit_tx_fragment(kingsun)) != 0) {
  280. err("ksdazzle_hard_xmit: failed tx_urb submit: %d", ret);
  281. switch (ret) {
  282. case -ENODEV:
  283. case -EPIPE:
  284. break;
  285. default:
  286. kingsun->stats.tx_errors++;
  287. netif_start_queue(netdev);
  288. }
  289. } else {
  290. kingsun->stats.tx_packets++;
  291. kingsun->stats.tx_bytes += skb->len;
  292. }
  293. dev_kfree_skb(skb);
  294. spin_unlock(&kingsun->lock);
  295. return ret;
  296. }
  297. /* Receive callback function */
  298. static void ksdazzle_rcv_irq(struct urb *urb)
  299. {
  300. struct ksdazzle_cb *kingsun = urb->context;
  301. /* in process of stopping, just drop data */
  302. if (!netif_running(kingsun->netdev)) {
  303. kingsun->receiving = 0;
  304. return;
  305. }
  306. /* unlink, shutdown, unplug, other nasties */
  307. if (urb->status != 0) {
  308. err("ksdazzle_rcv_irq: urb asynchronously failed - %d",
  309. urb->status);
  310. kingsun->receiving = 0;
  311. return;
  312. }
  313. if (urb->actual_length > 0) {
  314. __u8 *bytes = urb->transfer_buffer;
  315. unsigned int i;
  316. for (i = 0; i < urb->actual_length; i++) {
  317. async_unwrap_char(kingsun->netdev, &kingsun->stats,
  318. &kingsun->rx_unwrap_buff, bytes[i]);
  319. }
  320. kingsun->netdev->last_rx = jiffies;
  321. kingsun->receiving =
  322. (kingsun->rx_unwrap_buff.state != OUTSIDE_FRAME) ? 1 : 0;
  323. }
  324. /* This urb has already been filled in ksdazzle_net_open. It is assumed that
  325. urb keeps the pointer to the payload buffer.
  326. */
  327. urb->status = 0;
  328. usb_submit_urb(urb, GFP_ATOMIC);
  329. }
  330. /*
  331. * Function ksdazzle_net_open (dev)
  332. *
  333. * Network device is taken up. Usually this is done by "ifconfig irda0 up"
  334. */
  335. static int ksdazzle_net_open(struct net_device *netdev)
  336. {
  337. struct ksdazzle_cb *kingsun = netdev_priv(netdev);
  338. int err = -ENOMEM;
  339. char hwname[16];
  340. /* At this point, urbs are NULL, and skb is NULL (see ksdazzle_probe) */
  341. kingsun->receiving = 0;
  342. /* Initialize for SIR to copy data directly into skb. */
  343. kingsun->rx_unwrap_buff.in_frame = FALSE;
  344. kingsun->rx_unwrap_buff.state = OUTSIDE_FRAME;
  345. kingsun->rx_unwrap_buff.truesize = IRDA_SKB_MAX_MTU;
  346. kingsun->rx_unwrap_buff.skb = dev_alloc_skb(IRDA_SKB_MAX_MTU);
  347. if (!kingsun->rx_unwrap_buff.skb)
  348. goto free_mem;
  349. skb_reserve(kingsun->rx_unwrap_buff.skb, 1);
  350. kingsun->rx_unwrap_buff.head = kingsun->rx_unwrap_buff.skb->data;
  351. kingsun->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
  352. if (!kingsun->rx_urb)
  353. goto free_mem;
  354. kingsun->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  355. if (!kingsun->tx_urb)
  356. goto free_mem;
  357. kingsun->speed_urb = usb_alloc_urb(0, GFP_KERNEL);
  358. if (!kingsun->speed_urb)
  359. goto free_mem;
  360. /* Initialize speed for dongle */
  361. kingsun->new_speed = 9600;
  362. err = ksdazzle_change_speed(kingsun, 9600);
  363. if (err < 0)
  364. goto free_mem;
  365. /*
  366. * Now that everything should be initialized properly,
  367. * Open new IrLAP layer instance to take care of us...
  368. */
  369. sprintf(hwname, "usb#%d", kingsun->usbdev->devnum);
  370. kingsun->irlap = irlap_open(netdev, &kingsun->qos, hwname);
  371. if (!kingsun->irlap) {
  372. err("ksdazzle-sir: irlap_open failed");
  373. goto free_mem;
  374. }
  375. /* Start reception. */
  376. usb_fill_int_urb(kingsun->rx_urb, kingsun->usbdev,
  377. usb_rcvintpipe(kingsun->usbdev, kingsun->ep_in),
  378. kingsun->rx_buf, KINGSUN_RCV_MAX, ksdazzle_rcv_irq,
  379. kingsun, 1);
  380. kingsun->rx_urb->status = 0;
  381. err = usb_submit_urb(kingsun->rx_urb, GFP_KERNEL);
  382. if (err) {
  383. err("ksdazzle-sir: first urb-submit failed: %d", err);
  384. goto close_irlap;
  385. }
  386. netif_start_queue(netdev);
  387. /* Situation at this point:
  388. - all work buffers allocated
  389. - urbs allocated and ready to fill
  390. - max rx packet known (in max_rx)
  391. - unwrap state machine initialized, in state outside of any frame
  392. - receive request in progress
  393. - IrLAP layer started, about to hand over packets to send
  394. */
  395. return 0;
  396. close_irlap:
  397. irlap_close(kingsun->irlap);
  398. free_mem:
  399. usb_free_urb(kingsun->speed_urb);
  400. kingsun->speed_urb = NULL;
  401. usb_free_urb(kingsun->tx_urb);
  402. kingsun->tx_urb = NULL;
  403. usb_free_urb(kingsun->rx_urb);
  404. kingsun->rx_urb = NULL;
  405. if (kingsun->rx_unwrap_buff.skb) {
  406. kfree_skb(kingsun->rx_unwrap_buff.skb);
  407. kingsun->rx_unwrap_buff.skb = NULL;
  408. kingsun->rx_unwrap_buff.head = NULL;
  409. }
  410. return err;
  411. }
  412. /*
  413. * Function ksdazzle_net_close (dev)
  414. *
  415. * Network device is taken down. Usually this is done by
  416. * "ifconfig irda0 down"
  417. */
  418. static int ksdazzle_net_close(struct net_device *netdev)
  419. {
  420. struct ksdazzle_cb *kingsun = netdev_priv(netdev);
  421. /* Stop transmit processing */
  422. netif_stop_queue(netdev);
  423. /* Mop up receive && transmit urb's */
  424. usb_kill_urb(kingsun->tx_urb);
  425. usb_free_urb(kingsun->tx_urb);
  426. kingsun->tx_urb = NULL;
  427. usb_kill_urb(kingsun->speed_urb);
  428. usb_free_urb(kingsun->speed_urb);
  429. kingsun->speed_urb = NULL;
  430. usb_kill_urb(kingsun->rx_urb);
  431. usb_free_urb(kingsun->rx_urb);
  432. kingsun->rx_urb = NULL;
  433. kfree_skb(kingsun->rx_unwrap_buff.skb);
  434. kingsun->rx_unwrap_buff.skb = NULL;
  435. kingsun->rx_unwrap_buff.head = NULL;
  436. kingsun->rx_unwrap_buff.in_frame = FALSE;
  437. kingsun->rx_unwrap_buff.state = OUTSIDE_FRAME;
  438. kingsun->receiving = 0;
  439. /* Stop and remove instance of IrLAP */
  440. irlap_close(kingsun->irlap);
  441. kingsun->irlap = NULL;
  442. return 0;
  443. }
  444. /*
  445. * IOCTLs : Extra out-of-band network commands...
  446. */
  447. static int ksdazzle_net_ioctl(struct net_device *netdev, struct ifreq *rq,
  448. int cmd)
  449. {
  450. struct if_irda_req *irq = (struct if_irda_req *)rq;
  451. struct ksdazzle_cb *kingsun = netdev_priv(netdev);
  452. int ret = 0;
  453. switch (cmd) {
  454. case SIOCSBANDWIDTH: /* Set bandwidth */
  455. if (!capable(CAP_NET_ADMIN))
  456. return -EPERM;
  457. /* Check if the device is still there */
  458. if (netif_device_present(kingsun->netdev))
  459. return ksdazzle_change_speed(kingsun,
  460. irq->ifr_baudrate);
  461. break;
  462. case SIOCSMEDIABUSY: /* Set media busy */
  463. if (!capable(CAP_NET_ADMIN))
  464. return -EPERM;
  465. /* Check if the IrDA stack is still there */
  466. if (netif_running(kingsun->netdev))
  467. irda_device_set_media_busy(kingsun->netdev, TRUE);
  468. break;
  469. case SIOCGRECEIVING:
  470. /* Only approximately true */
  471. irq->ifr_receiving = kingsun->receiving;
  472. break;
  473. default:
  474. ret = -EOPNOTSUPP;
  475. }
  476. return ret;
  477. }
  478. /*
  479. * Get device stats (for /proc/net/dev and ifconfig)
  480. */
  481. static struct net_device_stats *ksdazzle_net_get_stats(struct net_device
  482. *netdev)
  483. {
  484. struct ksdazzle_cb *kingsun = netdev_priv(netdev);
  485. return &kingsun->stats;
  486. }
  487. /*
  488. * This routine is called by the USB subsystem for each new device
  489. * in the system. We need to check if the device is ours, and in
  490. * this case start handling it.
  491. */
  492. static int ksdazzle_probe(struct usb_interface *intf,
  493. const struct usb_device_id *id)
  494. {
  495. struct usb_host_interface *interface;
  496. struct usb_endpoint_descriptor *endpoint;
  497. struct usb_device *dev = interface_to_usbdev(intf);
  498. struct ksdazzle_cb *kingsun = NULL;
  499. struct net_device *net = NULL;
  500. int ret = -ENOMEM;
  501. int pipe, maxp_in, maxp_out;
  502. __u8 ep_in;
  503. __u8 ep_out;
  504. /* Check that there really are two interrupt endpoints. Check based on the
  505. one in drivers/usb/input/usbmouse.c
  506. */
  507. interface = intf->cur_altsetting;
  508. if (interface->desc.bNumEndpoints != 2) {
  509. err("ksdazzle: expected 2 endpoints, found %d",
  510. interface->desc.bNumEndpoints);
  511. return -ENODEV;
  512. }
  513. endpoint = &interface->endpoint[KINGSUN_EP_IN].desc;
  514. if (!usb_endpoint_is_int_in(endpoint)) {
  515. err("ksdazzle: endpoint 0 is not interrupt IN");
  516. return -ENODEV;
  517. }
  518. ep_in = endpoint->bEndpointAddress;
  519. pipe = usb_rcvintpipe(dev, ep_in);
  520. maxp_in = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
  521. if (maxp_in > 255 || maxp_in <= 1) {
  522. err("ksdazzle: endpoint 0 has max packet size %d not in range [2..255]", maxp_in);
  523. return -ENODEV;
  524. }
  525. endpoint = &interface->endpoint[KINGSUN_EP_OUT].desc;
  526. if (!usb_endpoint_is_int_out(endpoint)) {
  527. err("ksdazzle: endpoint 1 is not interrupt OUT");
  528. return -ENODEV;
  529. }
  530. ep_out = endpoint->bEndpointAddress;
  531. pipe = usb_sndintpipe(dev, ep_out);
  532. maxp_out = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
  533. /* Allocate network device container. */
  534. net = alloc_irdadev(sizeof(*kingsun));
  535. if (!net)
  536. goto err_out1;
  537. SET_NETDEV_DEV(net, &intf->dev);
  538. kingsun = netdev_priv(net);
  539. kingsun->netdev = net;
  540. kingsun->usbdev = dev;
  541. kingsun->ep_in = ep_in;
  542. kingsun->ep_out = ep_out;
  543. kingsun->irlap = NULL;
  544. kingsun->tx_urb = NULL;
  545. kingsun->tx_buf_clear = NULL;
  546. kingsun->tx_buf_clear_used = 0;
  547. kingsun->tx_buf_clear_sent = 0;
  548. kingsun->rx_urb = NULL;
  549. kingsun->rx_buf = NULL;
  550. kingsun->rx_unwrap_buff.in_frame = FALSE;
  551. kingsun->rx_unwrap_buff.state = OUTSIDE_FRAME;
  552. kingsun->rx_unwrap_buff.skb = NULL;
  553. kingsun->receiving = 0;
  554. spin_lock_init(&kingsun->lock);
  555. kingsun->speed_setuprequest = NULL;
  556. kingsun->speed_urb = NULL;
  557. kingsun->speedparams.baudrate = 0;
  558. /* Allocate input buffer */
  559. kingsun->rx_buf = kmalloc(KINGSUN_RCV_MAX, GFP_KERNEL);
  560. if (!kingsun->rx_buf)
  561. goto free_mem;
  562. /* Allocate output buffer */
  563. kingsun->tx_buf_clear = kmalloc(KINGSUN_SND_FIFO_SIZE, GFP_KERNEL);
  564. if (!kingsun->tx_buf_clear)
  565. goto free_mem;
  566. /* Allocate and initialize speed setup packet */
  567. kingsun->speed_setuprequest =
  568. kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
  569. if (!kingsun->speed_setuprequest)
  570. goto free_mem;
  571. kingsun->speed_setuprequest->bRequestType =
  572. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
  573. kingsun->speed_setuprequest->bRequest = KINGSUN_REQ_SEND;
  574. kingsun->speed_setuprequest->wValue = cpu_to_le16(0x0200);
  575. kingsun->speed_setuprequest->wIndex = cpu_to_le16(0x0001);
  576. kingsun->speed_setuprequest->wLength =
  577. cpu_to_le16(sizeof(struct ksdazzle_speedparams));
  578. printk(KERN_INFO "KingSun/Dazzle IRDA/USB found at address %d, "
  579. "Vendor: %x, Product: %x\n",
  580. dev->devnum, le16_to_cpu(dev->descriptor.idVendor),
  581. le16_to_cpu(dev->descriptor.idProduct));
  582. /* Initialize QoS for this device */
  583. irda_init_max_qos_capabilies(&kingsun->qos);
  584. /* Baud rates known to be supported. Please uncomment if devices (other
  585. than a SonyEriccson K300 phone) can be shown to support higher speeds
  586. with this dongle.
  587. */
  588. kingsun->qos.baud_rate.bits =
  589. IR_2400 | IR_9600 | IR_19200 | IR_38400 | IR_57600 | IR_115200;
  590. kingsun->qos.min_turn_time.bits &= KINGSUN_MTT;
  591. irda_qos_bits_to_value(&kingsun->qos);
  592. /* Override the network functions we need to use */
  593. net->hard_start_xmit = ksdazzle_hard_xmit;
  594. net->open = ksdazzle_net_open;
  595. net->stop = ksdazzle_net_close;
  596. net->get_stats = ksdazzle_net_get_stats;
  597. net->do_ioctl = ksdazzle_net_ioctl;
  598. ret = register_netdev(net);
  599. if (ret != 0)
  600. goto free_mem;
  601. dev_info(&net->dev, "IrDA: Registered KingSun/Dazzle device %s\n",
  602. net->name);
  603. usb_set_intfdata(intf, kingsun);
  604. /* Situation at this point:
  605. - all work buffers allocated
  606. - setup requests pre-filled
  607. - urbs not allocated, set to NULL
  608. - max rx packet known (is KINGSUN_FIFO_SIZE)
  609. - unwrap state machine (partially) initialized, but skb == NULL
  610. */
  611. return 0;
  612. free_mem:
  613. kfree(kingsun->speed_setuprequest);
  614. kfree(kingsun->tx_buf_clear);
  615. kfree(kingsun->rx_buf);
  616. free_netdev(net);
  617. err_out1:
  618. return ret;
  619. }
  620. /*
  621. * The current device is removed, the USB layer tell us to shut it down...
  622. */
  623. static void ksdazzle_disconnect(struct usb_interface *intf)
  624. {
  625. struct ksdazzle_cb *kingsun = usb_get_intfdata(intf);
  626. if (!kingsun)
  627. return;
  628. unregister_netdev(kingsun->netdev);
  629. /* Mop up receive && transmit urb's */
  630. usb_kill_urb(kingsun->speed_urb);
  631. usb_free_urb(kingsun->speed_urb);
  632. kingsun->speed_urb = NULL;
  633. usb_kill_urb(kingsun->tx_urb);
  634. usb_free_urb(kingsun->tx_urb);
  635. kingsun->tx_urb = NULL;
  636. usb_kill_urb(kingsun->rx_urb);
  637. usb_free_urb(kingsun->rx_urb);
  638. kingsun->rx_urb = NULL;
  639. kfree(kingsun->speed_setuprequest);
  640. kfree(kingsun->tx_buf_clear);
  641. kfree(kingsun->rx_buf);
  642. free_netdev(kingsun->netdev);
  643. usb_set_intfdata(intf, NULL);
  644. }
  645. #ifdef CONFIG_PM
  646. /* USB suspend, so power off the transmitter/receiver */
  647. static int ksdazzle_suspend(struct usb_interface *intf, pm_message_t message)
  648. {
  649. struct ksdazzle_cb *kingsun = usb_get_intfdata(intf);
  650. netif_device_detach(kingsun->netdev);
  651. if (kingsun->speed_urb != NULL)
  652. usb_kill_urb(kingsun->speed_urb);
  653. if (kingsun->tx_urb != NULL)
  654. usb_kill_urb(kingsun->tx_urb);
  655. if (kingsun->rx_urb != NULL)
  656. usb_kill_urb(kingsun->rx_urb);
  657. return 0;
  658. }
  659. /* Coming out of suspend, so reset hardware */
  660. static int ksdazzle_resume(struct usb_interface *intf)
  661. {
  662. struct ksdazzle_cb *kingsun = usb_get_intfdata(intf);
  663. if (kingsun->rx_urb != NULL) {
  664. /* Setup request already filled in ksdazzle_probe */
  665. usb_submit_urb(kingsun->rx_urb, GFP_KERNEL);
  666. }
  667. netif_device_attach(kingsun->netdev);
  668. return 0;
  669. }
  670. #endif
  671. /*
  672. * USB device callbacks
  673. */
  674. static struct usb_driver irda_driver = {
  675. .name = "ksdazzle-sir",
  676. .probe = ksdazzle_probe,
  677. .disconnect = ksdazzle_disconnect,
  678. .id_table = dongles,
  679. #ifdef CONFIG_PM
  680. .suspend = ksdazzle_suspend,
  681. .resume = ksdazzle_resume,
  682. #endif
  683. };
  684. /*
  685. * Module insertion
  686. */
  687. static int __init ksdazzle_init(void)
  688. {
  689. return usb_register(&irda_driver);
  690. }
  691. module_init(ksdazzle_init);
  692. /*
  693. * Module removal
  694. */
  695. static void __exit ksdazzle_cleanup(void)
  696. {
  697. /* Deregister the driver and remove all pending instances */
  698. usb_deregister(&irda_driver);
  699. }
  700. module_exit(ksdazzle_cleanup);
  701. MODULE_AUTHOR("Alex Villacís Lasso <a_villacis@palosanto.com>");
  702. MODULE_DESCRIPTION("IrDA-USB Dongle Driver for KingSun Dazzle");
  703. MODULE_LICENSE("GPL");