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/module.h>
  84. #include <linux/kref.h>
  85. #include <linux/usb.h>
  86. #include <linux/device.h>
  87. #include <linux/crc32.h>
  88. #include <asm/unaligned.h>
  89. #include <asm/byteorder.h>
  90. #include <asm/uaccess.h>
  91. #include <net/irda/irda.h>
  92. #include <net/irda/wrapper.h>
  93. #include <net/irda/crc.h>
  94. #define KSDAZZLE_VENDOR_ID 0x07d0
  95. #define KSDAZZLE_PRODUCT_ID 0x4100
  96. /* These are the currently known USB ids */
  97. static struct usb_device_id dongles[] = {
  98. /* KingSun Co,Ltd IrDA/USB Bridge */
  99. {USB_DEVICE(KSDAZZLE_VENDOR_ID, KSDAZZLE_PRODUCT_ID)},
  100. {}
  101. };
  102. MODULE_DEVICE_TABLE(usb, dongles);
  103. #define KINGSUN_MTT 0x07
  104. #define KINGSUN_REQ_RECV 0x01
  105. #define KINGSUN_REQ_SEND 0x09
  106. #define KINGSUN_SND_FIFO_SIZE 2048 /* Max packet we can send */
  107. #define KINGSUN_RCV_MAX 2048 /* Max transfer we can receive */
  108. struct ksdazzle_speedparams {
  109. __le32 baudrate; /* baud rate, little endian */
  110. __u8 flags;
  111. __u8 reserved[3];
  112. } __attribute__ ((packed));
  113. #define KS_DATA_5_BITS 0x00
  114. #define KS_DATA_6_BITS 0x01
  115. #define KS_DATA_7_BITS 0x02
  116. #define KS_DATA_8_BITS 0x03
  117. #define KS_STOP_BITS_1 0x00
  118. #define KS_STOP_BITS_2 0x08
  119. #define KS_PAR_DISABLE 0x00
  120. #define KS_PAR_EVEN 0x10
  121. #define KS_PAR_ODD 0x30
  122. #define KS_RESET 0x80
  123. #define KINGSUN_EP_IN 0
  124. #define KINGSUN_EP_OUT 1
  125. struct ksdazzle_cb {
  126. struct usb_device *usbdev; /* init: probe_irda */
  127. struct net_device *netdev; /* network layer */
  128. struct irlap_cb *irlap; /* The link layer we are binded to */
  129. struct net_device_stats stats; /* network statistics */
  130. struct qos_info qos;
  131. struct urb *tx_urb;
  132. __u8 *tx_buf_clear;
  133. unsigned int tx_buf_clear_used;
  134. unsigned int tx_buf_clear_sent;
  135. __u8 tx_payload[8];
  136. struct urb *rx_urb;
  137. __u8 *rx_buf;
  138. iobuff_t rx_unwrap_buff;
  139. struct usb_ctrlrequest *speed_setuprequest;
  140. struct urb *speed_urb;
  141. struct ksdazzle_speedparams speedparams;
  142. unsigned int new_speed;
  143. __u8 ep_in;
  144. __u8 ep_out;
  145. spinlock_t lock;
  146. int receiving;
  147. };
  148. /* Callback transmission routine */
  149. static void ksdazzle_speed_irq(struct urb *urb)
  150. {
  151. /* unlink, shutdown, unplug, other nasties */
  152. if (urb->status != 0) {
  153. err("ksdazzle_speed_irq: urb asynchronously failed - %d",
  154. urb->status);
  155. }
  156. }
  157. /* Send a control request to change speed of the dongle */
  158. static int ksdazzle_change_speed(struct ksdazzle_cb *kingsun, unsigned speed)
  159. {
  160. static unsigned int supported_speeds[] = { 2400, 9600, 19200, 38400,
  161. 57600, 115200, 576000, 1152000, 4000000, 0
  162. };
  163. int err;
  164. unsigned int i;
  165. if (kingsun->speed_setuprequest == NULL || kingsun->speed_urb == NULL)
  166. return -ENOMEM;
  167. /* Check that requested speed is among the supported ones */
  168. for (i = 0; supported_speeds[i] && supported_speeds[i] != speed; i++) ;
  169. if (supported_speeds[i] == 0)
  170. return -EOPNOTSUPP;
  171. memset(&(kingsun->speedparams), 0, sizeof(struct ksdazzle_speedparams));
  172. kingsun->speedparams.baudrate = cpu_to_le32(speed);
  173. kingsun->speedparams.flags = KS_DATA_8_BITS;
  174. /* speed_setuprequest pre-filled in ksdazzle_probe */
  175. usb_fill_control_urb(kingsun->speed_urb, kingsun->usbdev,
  176. usb_sndctrlpipe(kingsun->usbdev, 0),
  177. (unsigned char *)kingsun->speed_setuprequest,
  178. &(kingsun->speedparams),
  179. sizeof(struct ksdazzle_speedparams),
  180. ksdazzle_speed_irq, kingsun);
  181. kingsun->speed_urb->status = 0;
  182. err = usb_submit_urb(kingsun->speed_urb, GFP_ATOMIC);
  183. return err;
  184. }
  185. /* Submit one fragment of an IrDA frame to the dongle */
  186. static void ksdazzle_send_irq(struct urb *urb);
  187. static int ksdazzle_submit_tx_fragment(struct ksdazzle_cb *kingsun)
  188. {
  189. unsigned int wraplen;
  190. int ret;
  191. /* We can send at most 7 bytes of payload at a time */
  192. wraplen = 7;
  193. if (wraplen > kingsun->tx_buf_clear_used)
  194. wraplen = kingsun->tx_buf_clear_used;
  195. /* Prepare payload prefix with used length */
  196. memset(kingsun->tx_payload, 0, 8);
  197. kingsun->tx_payload[0] = (unsigned char)0xf8 + wraplen;
  198. memcpy(kingsun->tx_payload + 1, kingsun->tx_buf_clear, wraplen);
  199. usb_fill_int_urb(kingsun->tx_urb, kingsun->usbdev,
  200. usb_sndintpipe(kingsun->usbdev, kingsun->ep_out),
  201. kingsun->tx_payload, 8, ksdazzle_send_irq, kingsun, 1);
  202. kingsun->tx_urb->status = 0;
  203. ret = usb_submit_urb(kingsun->tx_urb, GFP_ATOMIC);
  204. /* Remember how much data was sent, in order to update at callback */
  205. kingsun->tx_buf_clear_sent = (ret == 0) ? wraplen : 0;
  206. return ret;
  207. }
  208. /* Callback transmission routine */
  209. static void ksdazzle_send_irq(struct urb *urb)
  210. {
  211. struct ksdazzle_cb *kingsun = urb->context;
  212. struct net_device *netdev = kingsun->netdev;
  213. int ret = 0;
  214. /* in process of stopping, just drop data */
  215. if (!netif_running(kingsun->netdev)) {
  216. err("ksdazzle_send_irq: Network not running!");
  217. return;
  218. }
  219. /* unlink, shutdown, unplug, other nasties */
  220. if (urb->status != 0) {
  221. err("ksdazzle_send_irq: urb asynchronously failed - %d",
  222. urb->status);
  223. return;
  224. }
  225. if (kingsun->tx_buf_clear_used > 0) {
  226. /* Update data remaining to be sent */
  227. if (kingsun->tx_buf_clear_sent < kingsun->tx_buf_clear_used) {
  228. memmove(kingsun->tx_buf_clear,
  229. kingsun->tx_buf_clear +
  230. kingsun->tx_buf_clear_sent,
  231. kingsun->tx_buf_clear_used -
  232. kingsun->tx_buf_clear_sent);
  233. }
  234. kingsun->tx_buf_clear_used -= kingsun->tx_buf_clear_sent;
  235. kingsun->tx_buf_clear_sent = 0;
  236. if (kingsun->tx_buf_clear_used > 0) {
  237. /* There is more data to be sent */
  238. if ((ret = ksdazzle_submit_tx_fragment(kingsun)) != 0) {
  239. err("ksdazzle_send_irq: failed tx_urb submit: %d", ret);
  240. switch (ret) {
  241. case -ENODEV:
  242. case -EPIPE:
  243. break;
  244. default:
  245. kingsun->stats.tx_errors++;
  246. netif_start_queue(netdev);
  247. }
  248. }
  249. } else {
  250. /* All data sent, send next speed && wake network queue */
  251. if (kingsun->new_speed != -1 &&
  252. cpu_to_le32(kingsun->new_speed) !=
  253. kingsun->speedparams.baudrate)
  254. ksdazzle_change_speed(kingsun,
  255. kingsun->new_speed);
  256. netif_wake_queue(netdev);
  257. }
  258. }
  259. }
  260. /*
  261. * Called from net/core when new frame is available.
  262. */
  263. static int ksdazzle_hard_xmit(struct sk_buff *skb, struct net_device *netdev)
  264. {
  265. struct ksdazzle_cb *kingsun;
  266. unsigned int wraplen;
  267. int ret = 0;
  268. if (skb == NULL || netdev == NULL)
  269. return -EINVAL;
  270. netif_stop_queue(netdev);
  271. /* the IRDA wrapping routines don't deal with non linear skb */
  272. SKB_LINEAR_ASSERT(skb);
  273. kingsun = netdev_priv(netdev);
  274. spin_lock(&kingsun->lock);
  275. kingsun->new_speed = irda_get_next_speed(skb);
  276. /* Append data to the end of whatever data remains to be transmitted */
  277. wraplen =
  278. async_wrap_skb(skb, kingsun->tx_buf_clear, KINGSUN_SND_FIFO_SIZE);
  279. kingsun->tx_buf_clear_used = wraplen;
  280. if ((ret = ksdazzle_submit_tx_fragment(kingsun)) != 0) {
  281. err("ksdazzle_hard_xmit: failed tx_urb submit: %d", ret);
  282. switch (ret) {
  283. case -ENODEV:
  284. case -EPIPE:
  285. break;
  286. default:
  287. kingsun->stats.tx_errors++;
  288. netif_start_queue(netdev);
  289. }
  290. } else {
  291. kingsun->stats.tx_packets++;
  292. kingsun->stats.tx_bytes += skb->len;
  293. }
  294. dev_kfree_skb(skb);
  295. spin_unlock(&kingsun->lock);
  296. return ret;
  297. }
  298. /* Receive callback function */
  299. static void ksdazzle_rcv_irq(struct urb *urb)
  300. {
  301. struct ksdazzle_cb *kingsun = urb->context;
  302. /* in process of stopping, just drop data */
  303. if (!netif_running(kingsun->netdev)) {
  304. kingsun->receiving = 0;
  305. return;
  306. }
  307. /* unlink, shutdown, unplug, other nasties */
  308. if (urb->status != 0) {
  309. err("ksdazzle_rcv_irq: urb asynchronously failed - %d",
  310. urb->status);
  311. kingsun->receiving = 0;
  312. return;
  313. }
  314. if (urb->actual_length > 0) {
  315. __u8 *bytes = urb->transfer_buffer;
  316. unsigned int i;
  317. for (i = 0; i < urb->actual_length; i++) {
  318. async_unwrap_char(kingsun->netdev, &kingsun->stats,
  319. &kingsun->rx_unwrap_buff, bytes[i]);
  320. }
  321. kingsun->netdev->last_rx = jiffies;
  322. kingsun->receiving =
  323. (kingsun->rx_unwrap_buff.state != OUTSIDE_FRAME) ? 1 : 0;
  324. }
  325. /* This urb has already been filled in ksdazzle_net_open. It is assumed that
  326. urb keeps the pointer to the payload buffer.
  327. */
  328. urb->status = 0;
  329. usb_submit_urb(urb, GFP_ATOMIC);
  330. }
  331. /*
  332. * Function ksdazzle_net_open (dev)
  333. *
  334. * Network device is taken up. Usually this is done by "ifconfig irda0 up"
  335. */
  336. static int ksdazzle_net_open(struct net_device *netdev)
  337. {
  338. struct ksdazzle_cb *kingsun = netdev_priv(netdev);
  339. int err = -ENOMEM;
  340. char hwname[16];
  341. /* At this point, urbs are NULL, and skb is NULL (see ksdazzle_probe) */
  342. kingsun->receiving = 0;
  343. /* Initialize for SIR to copy data directly into skb. */
  344. kingsun->rx_unwrap_buff.in_frame = FALSE;
  345. kingsun->rx_unwrap_buff.state = OUTSIDE_FRAME;
  346. kingsun->rx_unwrap_buff.truesize = IRDA_SKB_MAX_MTU;
  347. kingsun->rx_unwrap_buff.skb = dev_alloc_skb(IRDA_SKB_MAX_MTU);
  348. if (!kingsun->rx_unwrap_buff.skb)
  349. goto free_mem;
  350. skb_reserve(kingsun->rx_unwrap_buff.skb, 1);
  351. kingsun->rx_unwrap_buff.head = kingsun->rx_unwrap_buff.skb->data;
  352. kingsun->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
  353. if (!kingsun->rx_urb)
  354. goto free_mem;
  355. kingsun->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  356. if (!kingsun->tx_urb)
  357. goto free_mem;
  358. kingsun->speed_urb = usb_alloc_urb(0, GFP_KERNEL);
  359. if (!kingsun->speed_urb)
  360. goto free_mem;
  361. /* Initialize speed for dongle */
  362. kingsun->new_speed = 9600;
  363. err = ksdazzle_change_speed(kingsun, 9600);
  364. if (err < 0)
  365. goto free_mem;
  366. /*
  367. * Now that everything should be initialized properly,
  368. * Open new IrLAP layer instance to take care of us...
  369. */
  370. sprintf(hwname, "usb#%d", kingsun->usbdev->devnum);
  371. kingsun->irlap = irlap_open(netdev, &kingsun->qos, hwname);
  372. if (!kingsun->irlap) {
  373. err("ksdazzle-sir: irlap_open failed");
  374. goto free_mem;
  375. }
  376. /* Start reception. */
  377. usb_fill_int_urb(kingsun->rx_urb, kingsun->usbdev,
  378. usb_rcvintpipe(kingsun->usbdev, kingsun->ep_in),
  379. kingsun->rx_buf, KINGSUN_RCV_MAX, ksdazzle_rcv_irq,
  380. kingsun, 1);
  381. kingsun->rx_urb->status = 0;
  382. err = usb_submit_urb(kingsun->rx_urb, GFP_KERNEL);
  383. if (err) {
  384. err("ksdazzle-sir: first urb-submit failed: %d", err);
  385. goto close_irlap;
  386. }
  387. netif_start_queue(netdev);
  388. /* Situation at this point:
  389. - all work buffers allocated
  390. - urbs allocated and ready to fill
  391. - max rx packet known (in max_rx)
  392. - unwrap state machine initialized, in state outside of any frame
  393. - receive request in progress
  394. - IrLAP layer started, about to hand over packets to send
  395. */
  396. return 0;
  397. close_irlap:
  398. irlap_close(kingsun->irlap);
  399. free_mem:
  400. usb_free_urb(kingsun->speed_urb);
  401. kingsun->speed_urb = NULL;
  402. usb_free_urb(kingsun->tx_urb);
  403. kingsun->tx_urb = NULL;
  404. usb_free_urb(kingsun->rx_urb);
  405. kingsun->rx_urb = NULL;
  406. if (kingsun->rx_unwrap_buff.skb) {
  407. kfree_skb(kingsun->rx_unwrap_buff.skb);
  408. kingsun->rx_unwrap_buff.skb = NULL;
  409. kingsun->rx_unwrap_buff.head = NULL;
  410. }
  411. return err;
  412. }
  413. /*
  414. * Function ksdazzle_net_close (dev)
  415. *
  416. * Network device is taken down. Usually this is done by
  417. * "ifconfig irda0 down"
  418. */
  419. static int ksdazzle_net_close(struct net_device *netdev)
  420. {
  421. struct ksdazzle_cb *kingsun = netdev_priv(netdev);
  422. /* Stop transmit processing */
  423. netif_stop_queue(netdev);
  424. /* Mop up receive && transmit urb's */
  425. usb_kill_urb(kingsun->tx_urb);
  426. usb_free_urb(kingsun->tx_urb);
  427. kingsun->tx_urb = NULL;
  428. usb_kill_urb(kingsun->speed_urb);
  429. usb_free_urb(kingsun->speed_urb);
  430. kingsun->speed_urb = NULL;
  431. usb_kill_urb(kingsun->rx_urb);
  432. usb_free_urb(kingsun->rx_urb);
  433. kingsun->rx_urb = NULL;
  434. kfree_skb(kingsun->rx_unwrap_buff.skb);
  435. kingsun->rx_unwrap_buff.skb = NULL;
  436. kingsun->rx_unwrap_buff.head = NULL;
  437. kingsun->rx_unwrap_buff.in_frame = FALSE;
  438. kingsun->rx_unwrap_buff.state = OUTSIDE_FRAME;
  439. kingsun->receiving = 0;
  440. /* Stop and remove instance of IrLAP */
  441. irlap_close(kingsun->irlap);
  442. kingsun->irlap = NULL;
  443. return 0;
  444. }
  445. /*
  446. * IOCTLs : Extra out-of-band network commands...
  447. */
  448. static int ksdazzle_net_ioctl(struct net_device *netdev, struct ifreq *rq,
  449. int cmd)
  450. {
  451. struct if_irda_req *irq = (struct if_irda_req *)rq;
  452. struct ksdazzle_cb *kingsun = netdev_priv(netdev);
  453. int ret = 0;
  454. switch (cmd) {
  455. case SIOCSBANDWIDTH: /* Set bandwidth */
  456. if (!capable(CAP_NET_ADMIN))
  457. return -EPERM;
  458. /* Check if the device is still there */
  459. if (netif_device_present(kingsun->netdev))
  460. return ksdazzle_change_speed(kingsun,
  461. irq->ifr_baudrate);
  462. break;
  463. case SIOCSMEDIABUSY: /* Set media busy */
  464. if (!capable(CAP_NET_ADMIN))
  465. return -EPERM;
  466. /* Check if the IrDA stack is still there */
  467. if (netif_running(kingsun->netdev))
  468. irda_device_set_media_busy(kingsun->netdev, TRUE);
  469. break;
  470. case SIOCGRECEIVING:
  471. /* Only approximately true */
  472. irq->ifr_receiving = kingsun->receiving;
  473. break;
  474. default:
  475. ret = -EOPNOTSUPP;
  476. }
  477. return ret;
  478. }
  479. /*
  480. * Get device stats (for /proc/net/dev and ifconfig)
  481. */
  482. static struct net_device_stats *ksdazzle_net_get_stats(struct net_device
  483. *netdev)
  484. {
  485. struct ksdazzle_cb *kingsun = netdev_priv(netdev);
  486. return &kingsun->stats;
  487. }
  488. /*
  489. * This routine is called by the USB subsystem for each new device
  490. * in the system. We need to check if the device is ours, and in
  491. * this case start handling it.
  492. */
  493. static int ksdazzle_probe(struct usb_interface *intf,
  494. const struct usb_device_id *id)
  495. {
  496. struct usb_host_interface *interface;
  497. struct usb_endpoint_descriptor *endpoint;
  498. struct usb_device *dev = interface_to_usbdev(intf);
  499. struct ksdazzle_cb *kingsun = NULL;
  500. struct net_device *net = NULL;
  501. int ret = -ENOMEM;
  502. int pipe, maxp_in, maxp_out;
  503. __u8 ep_in;
  504. __u8 ep_out;
  505. /* Check that there really are two interrupt endpoints. Check based on the
  506. one in drivers/usb/input/usbmouse.c
  507. */
  508. interface = intf->cur_altsetting;
  509. if (interface->desc.bNumEndpoints != 2) {
  510. err("ksdazzle: expected 2 endpoints, found %d",
  511. interface->desc.bNumEndpoints);
  512. return -ENODEV;
  513. }
  514. endpoint = &interface->endpoint[KINGSUN_EP_IN].desc;
  515. if (!usb_endpoint_is_int_in(endpoint)) {
  516. err("ksdazzle: endpoint 0 is not interrupt IN");
  517. return -ENODEV;
  518. }
  519. ep_in = endpoint->bEndpointAddress;
  520. pipe = usb_rcvintpipe(dev, ep_in);
  521. maxp_in = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
  522. if (maxp_in > 255 || maxp_in <= 1) {
  523. err("ksdazzle: endpoint 0 has max packet size %d not in range [2..255]", maxp_in);
  524. return -ENODEV;
  525. }
  526. endpoint = &interface->endpoint[KINGSUN_EP_OUT].desc;
  527. if (!usb_endpoint_is_int_out(endpoint)) {
  528. err("ksdazzle: endpoint 1 is not interrupt OUT");
  529. return -ENODEV;
  530. }
  531. ep_out = endpoint->bEndpointAddress;
  532. pipe = usb_sndintpipe(dev, ep_out);
  533. maxp_out = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
  534. /* Allocate network device container. */
  535. net = alloc_irdadev(sizeof(*kingsun));
  536. if (!net)
  537. goto err_out1;
  538. SET_NETDEV_DEV(net, &intf->dev);
  539. kingsun = netdev_priv(net);
  540. kingsun->netdev = net;
  541. kingsun->usbdev = dev;
  542. kingsun->ep_in = ep_in;
  543. kingsun->ep_out = ep_out;
  544. kingsun->irlap = NULL;
  545. kingsun->tx_urb = NULL;
  546. kingsun->tx_buf_clear = NULL;
  547. kingsun->tx_buf_clear_used = 0;
  548. kingsun->tx_buf_clear_sent = 0;
  549. kingsun->rx_urb = NULL;
  550. kingsun->rx_buf = NULL;
  551. kingsun->rx_unwrap_buff.in_frame = FALSE;
  552. kingsun->rx_unwrap_buff.state = OUTSIDE_FRAME;
  553. kingsun->rx_unwrap_buff.skb = NULL;
  554. kingsun->receiving = 0;
  555. spin_lock_init(&kingsun->lock);
  556. kingsun->speed_setuprequest = NULL;
  557. kingsun->speed_urb = NULL;
  558. kingsun->speedparams.baudrate = 0;
  559. /* Allocate input buffer */
  560. kingsun->rx_buf = kmalloc(KINGSUN_RCV_MAX, GFP_KERNEL);
  561. if (!kingsun->rx_buf)
  562. goto free_mem;
  563. /* Allocate output buffer */
  564. kingsun->tx_buf_clear = kmalloc(KINGSUN_SND_FIFO_SIZE, GFP_KERNEL);
  565. if (!kingsun->tx_buf_clear)
  566. goto free_mem;
  567. /* Allocate and initialize speed setup packet */
  568. kingsun->speed_setuprequest =
  569. kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);
  570. if (!kingsun->speed_setuprequest)
  571. goto free_mem;
  572. kingsun->speed_setuprequest->bRequestType =
  573. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
  574. kingsun->speed_setuprequest->bRequest = KINGSUN_REQ_SEND;
  575. kingsun->speed_setuprequest->wValue = cpu_to_le16(0x0200);
  576. kingsun->speed_setuprequest->wIndex = cpu_to_le16(0x0001);
  577. kingsun->speed_setuprequest->wLength =
  578. cpu_to_le16(sizeof(struct ksdazzle_speedparams));
  579. printk(KERN_INFO "KingSun/Dazzle IRDA/USB found at address %d, "
  580. "Vendor: %x, Product: %x\n",
  581. dev->devnum, le16_to_cpu(dev->descriptor.idVendor),
  582. le16_to_cpu(dev->descriptor.idProduct));
  583. /* Initialize QoS for this device */
  584. irda_init_max_qos_capabilies(&kingsun->qos);
  585. /* Baud rates known to be supported. Please uncomment if devices (other
  586. than a SonyEriccson K300 phone) can be shown to support higher speeds
  587. with this dongle.
  588. */
  589. kingsun->qos.baud_rate.bits =
  590. IR_2400 | IR_9600 | IR_19200 | IR_38400 | IR_57600 | IR_115200;
  591. kingsun->qos.min_turn_time.bits &= KINGSUN_MTT;
  592. irda_qos_bits_to_value(&kingsun->qos);
  593. /* Override the network functions we need to use */
  594. net->hard_start_xmit = ksdazzle_hard_xmit;
  595. net->open = ksdazzle_net_open;
  596. net->stop = ksdazzle_net_close;
  597. net->get_stats = ksdazzle_net_get_stats;
  598. net->do_ioctl = ksdazzle_net_ioctl;
  599. ret = register_netdev(net);
  600. if (ret != 0)
  601. goto free_mem;
  602. info("IrDA: Registered KingSun/Dazzle device %s", 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");