usbatm.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344
  1. /******************************************************************************
  2. * usbatm.c - Generic USB xDSL driver core
  3. *
  4. * Copyright (C) 2001, Alcatel
  5. * Copyright (C) 2003, Duncan Sands, SolNegro, Josep Comas
  6. * Copyright (C) 2004, David Woodhouse, Roman Kagan
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the Free
  10. * Software Foundation; either version 2 of the License, or (at your option)
  11. * any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16. * more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along with
  19. * this program; if not, write to the Free Software Foundation, Inc., 59
  20. * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. *
  22. ******************************************************************************/
  23. /*
  24. * Written by Johan Verrept, Duncan Sands (duncan.sands@free.fr) and David Woodhouse
  25. *
  26. * 1.7+: - See the check-in logs
  27. *
  28. * 1.6: - No longer opens a connection if the firmware is not loaded
  29. * - Added support for the speedtouch 330
  30. * - Removed the limit on the number of devices
  31. * - Module now autoloads on device plugin
  32. * - Merged relevant parts of sarlib
  33. * - Replaced the kernel thread with a tasklet
  34. * - New packet transmission code
  35. * - Changed proc file contents
  36. * - Fixed all known SMP races
  37. * - Many fixes and cleanups
  38. * - Various fixes by Oliver Neukum (oliver@neukum.name)
  39. *
  40. * 1.5A: - Version for inclusion in 2.5 series kernel
  41. * - Modifications by Richard Purdie (rpurdie@rpsys.net)
  42. * - made compatible with kernel 2.5.6 onwards by changing
  43. * usbatm_usb_send_data_context->urb to a pointer and adding code
  44. * to alloc and free it
  45. * - remove_wait_queue() added to usbatm_atm_processqueue_thread()
  46. *
  47. * 1.5: - fixed memory leak when atmsar_decode_aal5 returned NULL.
  48. * (reported by stephen.robinson@zen.co.uk)
  49. *
  50. * 1.4: - changed the spin_lock() under interrupt to spin_lock_irqsave()
  51. * - unlink all active send urbs of a vcc that is being closed.
  52. *
  53. * 1.3.1: - added the version number
  54. *
  55. * 1.3: - Added multiple send urb support
  56. * - fixed memory leak and vcc->tx_inuse starvation bug
  57. * when not enough memory left in vcc.
  58. *
  59. * 1.2: - Fixed race condition in usbatm_usb_send_data()
  60. * 1.1: - Turned off packet debugging
  61. *
  62. */
  63. #include "usbatm.h"
  64. #include <asm/uaccess.h>
  65. #include <linux/crc32.h>
  66. #include <linux/errno.h>
  67. #include <linux/init.h>
  68. #include <linux/interrupt.h>
  69. #include <linux/kernel.h>
  70. #include <linux/module.h>
  71. #include <linux/moduleparam.h>
  72. #include <linux/netdevice.h>
  73. #include <linux/proc_fs.h>
  74. #include <linux/sched.h>
  75. #include <linux/signal.h>
  76. #include <linux/slab.h>
  77. #include <linux/stat.h>
  78. #include <linux/timer.h>
  79. #include <linux/wait.h>
  80. #include <linux/kthread.h>
  81. #include <linux/ratelimit.h>
  82. #ifdef VERBOSE_DEBUG
  83. static int usbatm_print_packet(struct usbatm_data *instance, const unsigned char *data, int len);
  84. #define PACKETDEBUG(arg...) usbatm_print_packet(arg)
  85. #define vdbg(arg...) dev_dbg(arg)
  86. #else
  87. #define PACKETDEBUG(arg...)
  88. #define vdbg(arg...)
  89. #endif
  90. #define DRIVER_AUTHOR "Johan Verrept, Duncan Sands <duncan.sands@free.fr>"
  91. #define DRIVER_VERSION "1.10"
  92. #define DRIVER_DESC "Generic USB ATM/DSL I/O, version " DRIVER_VERSION
  93. static const char usbatm_driver_name[] = "usbatm";
  94. #define UDSL_MAX_RCV_URBS 16
  95. #define UDSL_MAX_SND_URBS 16
  96. #define UDSL_MAX_BUF_SIZE 65536
  97. #define UDSL_DEFAULT_RCV_URBS 4
  98. #define UDSL_DEFAULT_SND_URBS 4
  99. #define UDSL_DEFAULT_RCV_BUF_SIZE 3392 /* 64 * ATM_CELL_SIZE */
  100. #define UDSL_DEFAULT_SND_BUF_SIZE 3392 /* 64 * ATM_CELL_SIZE */
  101. #define ATM_CELL_HEADER (ATM_CELL_SIZE - ATM_CELL_PAYLOAD)
  102. #define THROTTLE_MSECS 100 /* delay to recover processing after urb submission fails */
  103. static unsigned int num_rcv_urbs = UDSL_DEFAULT_RCV_URBS;
  104. static unsigned int num_snd_urbs = UDSL_DEFAULT_SND_URBS;
  105. static unsigned int rcv_buf_bytes = UDSL_DEFAULT_RCV_BUF_SIZE;
  106. static unsigned int snd_buf_bytes = UDSL_DEFAULT_SND_BUF_SIZE;
  107. module_param(num_rcv_urbs, uint, S_IRUGO);
  108. MODULE_PARM_DESC(num_rcv_urbs,
  109. "Number of urbs used for reception (range: 0-"
  110. __MODULE_STRING(UDSL_MAX_RCV_URBS) ", default: "
  111. __MODULE_STRING(UDSL_DEFAULT_RCV_URBS) ")");
  112. module_param(num_snd_urbs, uint, S_IRUGO);
  113. MODULE_PARM_DESC(num_snd_urbs,
  114. "Number of urbs used for transmission (range: 0-"
  115. __MODULE_STRING(UDSL_MAX_SND_URBS) ", default: "
  116. __MODULE_STRING(UDSL_DEFAULT_SND_URBS) ")");
  117. module_param(rcv_buf_bytes, uint, S_IRUGO);
  118. MODULE_PARM_DESC(rcv_buf_bytes,
  119. "Size of the buffers used for reception, in bytes (range: 1-"
  120. __MODULE_STRING(UDSL_MAX_BUF_SIZE) ", default: "
  121. __MODULE_STRING(UDSL_DEFAULT_RCV_BUF_SIZE) ")");
  122. module_param(snd_buf_bytes, uint, S_IRUGO);
  123. MODULE_PARM_DESC(snd_buf_bytes,
  124. "Size of the buffers used for transmission, in bytes (range: 1-"
  125. __MODULE_STRING(UDSL_MAX_BUF_SIZE) ", default: "
  126. __MODULE_STRING(UDSL_DEFAULT_SND_BUF_SIZE) ")");
  127. /* receive */
  128. struct usbatm_vcc_data {
  129. /* vpi/vci lookup */
  130. struct list_head list;
  131. short vpi;
  132. int vci;
  133. struct atm_vcc *vcc;
  134. /* raw cell reassembly */
  135. struct sk_buff *sarb;
  136. };
  137. /* send */
  138. struct usbatm_control {
  139. struct atm_skb_data atm;
  140. u32 len;
  141. u32 crc;
  142. };
  143. #define UDSL_SKB(x) ((struct usbatm_control *)(x)->cb)
  144. /* ATM */
  145. static void usbatm_atm_dev_close(struct atm_dev *atm_dev);
  146. static int usbatm_atm_open(struct atm_vcc *vcc);
  147. static void usbatm_atm_close(struct atm_vcc *vcc);
  148. static int usbatm_atm_ioctl(struct atm_dev *atm_dev, unsigned int cmd, void __user * arg);
  149. static int usbatm_atm_send(struct atm_vcc *vcc, struct sk_buff *skb);
  150. static int usbatm_atm_proc_read(struct atm_dev *atm_dev, loff_t * pos, char *page);
  151. static struct atmdev_ops usbatm_atm_devops = {
  152. .dev_close = usbatm_atm_dev_close,
  153. .open = usbatm_atm_open,
  154. .close = usbatm_atm_close,
  155. .ioctl = usbatm_atm_ioctl,
  156. .send = usbatm_atm_send,
  157. .proc_read = usbatm_atm_proc_read,
  158. .owner = THIS_MODULE,
  159. };
  160. /***********
  161. ** misc **
  162. ***********/
  163. static inline unsigned int usbatm_pdu_length(unsigned int length)
  164. {
  165. length += ATM_CELL_PAYLOAD - 1 + ATM_AAL5_TRAILER;
  166. return length - length % ATM_CELL_PAYLOAD;
  167. }
  168. static inline void usbatm_pop(struct atm_vcc *vcc, struct sk_buff *skb)
  169. {
  170. if (vcc->pop)
  171. vcc->pop(vcc, skb);
  172. else
  173. dev_kfree_skb_any(skb);
  174. }
  175. /***********
  176. ** urbs **
  177. ************/
  178. static struct urb *usbatm_pop_urb(struct usbatm_channel *channel)
  179. {
  180. struct urb *urb;
  181. spin_lock_irq(&channel->lock);
  182. if (list_empty(&channel->list)) {
  183. spin_unlock_irq(&channel->lock);
  184. return NULL;
  185. }
  186. urb = list_entry(channel->list.next, struct urb, urb_list);
  187. list_del(&urb->urb_list);
  188. spin_unlock_irq(&channel->lock);
  189. return urb;
  190. }
  191. static int usbatm_submit_urb(struct urb *urb)
  192. {
  193. struct usbatm_channel *channel = urb->context;
  194. int ret;
  195. /* vdbg("%s: submitting urb 0x%p, size %u",
  196. __func__, urb, urb->transfer_buffer_length); */
  197. ret = usb_submit_urb(urb, GFP_ATOMIC);
  198. if (ret) {
  199. if (printk_ratelimit())
  200. atm_warn(channel->usbatm, "%s: urb 0x%p submission failed (%d)!\n",
  201. __func__, urb, ret);
  202. /* consider all errors transient and return the buffer back to the queue */
  203. urb->status = -EAGAIN;
  204. spin_lock_irq(&channel->lock);
  205. /* must add to the front when sending; doesn't matter when receiving */
  206. list_add(&urb->urb_list, &channel->list);
  207. spin_unlock_irq(&channel->lock);
  208. /* make sure the channel doesn't stall */
  209. mod_timer(&channel->delay, jiffies + msecs_to_jiffies(THROTTLE_MSECS));
  210. }
  211. return ret;
  212. }
  213. static void usbatm_complete(struct urb *urb)
  214. {
  215. struct usbatm_channel *channel = urb->context;
  216. unsigned long flags;
  217. int status = urb->status;
  218. /* vdbg("%s: urb 0x%p, status %d, actual_length %d",
  219. __func__, urb, status, urb->actual_length); */
  220. /* usually in_interrupt(), but not always */
  221. spin_lock_irqsave(&channel->lock, flags);
  222. /* must add to the back when receiving; doesn't matter when sending */
  223. list_add_tail(&urb->urb_list, &channel->list);
  224. spin_unlock_irqrestore(&channel->lock, flags);
  225. if (unlikely(status) &&
  226. (!(channel->usbatm->flags & UDSL_IGNORE_EILSEQ) ||
  227. status != -EILSEQ)) {
  228. if (status == -ESHUTDOWN)
  229. return;
  230. if (printk_ratelimit())
  231. atm_warn(channel->usbatm, "%s: urb 0x%p failed (%d)!\n",
  232. __func__, urb, status);
  233. /* throttle processing in case of an error */
  234. mod_timer(&channel->delay, jiffies + msecs_to_jiffies(THROTTLE_MSECS));
  235. } else
  236. tasklet_schedule(&channel->tasklet);
  237. }
  238. /*************
  239. ** decode **
  240. *************/
  241. static inline struct usbatm_vcc_data *usbatm_find_vcc(struct usbatm_data *instance,
  242. short vpi, int vci)
  243. {
  244. struct usbatm_vcc_data *vcc_data;
  245. list_for_each_entry(vcc_data, &instance->vcc_list, list)
  246. if ((vcc_data->vci == vci) && (vcc_data->vpi == vpi))
  247. return vcc_data;
  248. return NULL;
  249. }
  250. static void usbatm_extract_one_cell(struct usbatm_data *instance, unsigned char *source)
  251. {
  252. struct atm_vcc *vcc;
  253. struct sk_buff *sarb;
  254. short vpi = ((source[0] & 0x0f) << 4) | (source[1] >> 4);
  255. int vci = ((source[1] & 0x0f) << 12) | (source[2] << 4) | (source[3] >> 4);
  256. u8 pti = ((source[3] & 0xe) >> 1);
  257. if ((vci != instance->cached_vci) || (vpi != instance->cached_vpi)) {
  258. instance->cached_vpi = vpi;
  259. instance->cached_vci = vci;
  260. instance->cached_vcc = usbatm_find_vcc(instance, vpi, vci);
  261. if (!instance->cached_vcc)
  262. atm_rldbg(instance, "%s: unknown vpi/vci (%hd/%d)!\n", __func__, vpi, vci);
  263. }
  264. if (!instance->cached_vcc)
  265. return;
  266. vcc = instance->cached_vcc->vcc;
  267. /* OAM F5 end-to-end */
  268. if (pti == ATM_PTI_E2EF5) {
  269. if (printk_ratelimit())
  270. atm_warn(instance, "%s: OAM not supported (vpi %d, vci %d)!\n",
  271. __func__, vpi, vci);
  272. atomic_inc(&vcc->stats->rx_err);
  273. return;
  274. }
  275. sarb = instance->cached_vcc->sarb;
  276. if (sarb->tail + ATM_CELL_PAYLOAD > sarb->end) {
  277. atm_rldbg(instance, "%s: buffer overrun (sarb->len %u, vcc: 0x%p)!\n",
  278. __func__, sarb->len, vcc);
  279. /* discard cells already received */
  280. skb_trim(sarb, 0);
  281. }
  282. memcpy(skb_tail_pointer(sarb), source + ATM_CELL_HEADER, ATM_CELL_PAYLOAD);
  283. __skb_put(sarb, ATM_CELL_PAYLOAD);
  284. if (pti & 1) {
  285. struct sk_buff *skb;
  286. unsigned int length;
  287. unsigned int pdu_length;
  288. length = (source[ATM_CELL_SIZE - 6] << 8) + source[ATM_CELL_SIZE - 5];
  289. /* guard against overflow */
  290. if (length > ATM_MAX_AAL5_PDU) {
  291. atm_rldbg(instance, "%s: bogus length %u (vcc: 0x%p)!\n",
  292. __func__, length, vcc);
  293. atomic_inc(&vcc->stats->rx_err);
  294. goto out;
  295. }
  296. pdu_length = usbatm_pdu_length(length);
  297. if (sarb->len < pdu_length) {
  298. atm_rldbg(instance, "%s: bogus pdu_length %u (sarb->len: %u, vcc: 0x%p)!\n",
  299. __func__, pdu_length, sarb->len, vcc);
  300. atomic_inc(&vcc->stats->rx_err);
  301. goto out;
  302. }
  303. if (crc32_be(~0, skb_tail_pointer(sarb) - pdu_length, pdu_length) != 0xc704dd7b) {
  304. atm_rldbg(instance, "%s: packet failed crc check (vcc: 0x%p)!\n",
  305. __func__, vcc);
  306. atomic_inc(&vcc->stats->rx_err);
  307. goto out;
  308. }
  309. vdbg(&instance->usb_intf->dev,
  310. "%s: got packet (length: %u, pdu_length: %u, vcc: 0x%p)",
  311. __func__, length, pdu_length, vcc);
  312. if (!(skb = dev_alloc_skb(length))) {
  313. if (printk_ratelimit())
  314. atm_err(instance, "%s: no memory for skb (length: %u)!\n",
  315. __func__, length);
  316. atomic_inc(&vcc->stats->rx_drop);
  317. goto out;
  318. }
  319. vdbg(&instance->usb_intf->dev,
  320. "%s: allocated new sk_buff (skb: 0x%p, skb->truesize: %u)",
  321. __func__, skb, skb->truesize);
  322. if (!atm_charge(vcc, skb->truesize)) {
  323. atm_rldbg(instance, "%s: failed atm_charge (skb->truesize: %u)!\n",
  324. __func__, skb->truesize);
  325. dev_kfree_skb_any(skb);
  326. goto out; /* atm_charge increments rx_drop */
  327. }
  328. skb_copy_to_linear_data(skb,
  329. skb_tail_pointer(sarb) - pdu_length,
  330. length);
  331. __skb_put(skb, length);
  332. vdbg(&instance->usb_intf->dev,
  333. "%s: sending skb 0x%p, skb->len %u, skb->truesize %u",
  334. __func__, skb, skb->len, skb->truesize);
  335. PACKETDEBUG(instance, skb->data, skb->len);
  336. vcc->push(vcc, skb);
  337. atomic_inc(&vcc->stats->rx);
  338. out:
  339. skb_trim(sarb, 0);
  340. }
  341. }
  342. static void usbatm_extract_cells(struct usbatm_data *instance,
  343. unsigned char *source, unsigned int avail_data)
  344. {
  345. unsigned int stride = instance->rx_channel.stride;
  346. unsigned int buf_usage = instance->buf_usage;
  347. /* extract cells from incoming data, taking into account that
  348. * the length of avail data may not be a multiple of stride */
  349. if (buf_usage > 0) {
  350. /* we have a partially received atm cell */
  351. unsigned char *cell_buf = instance->cell_buf;
  352. unsigned int space_left = stride - buf_usage;
  353. if (avail_data >= space_left) {
  354. /* add new data and process cell */
  355. memcpy(cell_buf + buf_usage, source, space_left);
  356. source += space_left;
  357. avail_data -= space_left;
  358. usbatm_extract_one_cell(instance, cell_buf);
  359. instance->buf_usage = 0;
  360. } else {
  361. /* not enough data to fill the cell */
  362. memcpy(cell_buf + buf_usage, source, avail_data);
  363. instance->buf_usage = buf_usage + avail_data;
  364. return;
  365. }
  366. }
  367. for (; avail_data >= stride; avail_data -= stride, source += stride)
  368. usbatm_extract_one_cell(instance, source);
  369. if (avail_data > 0) {
  370. /* length was not a multiple of stride -
  371. * save remaining data for next call */
  372. memcpy(instance->cell_buf, source, avail_data);
  373. instance->buf_usage = avail_data;
  374. }
  375. }
  376. /*************
  377. ** encode **
  378. *************/
  379. static unsigned int usbatm_write_cells(struct usbatm_data *instance,
  380. struct sk_buff *skb,
  381. u8 *target, unsigned int avail_space)
  382. {
  383. struct usbatm_control *ctrl = UDSL_SKB(skb);
  384. struct atm_vcc *vcc = ctrl->atm.vcc;
  385. unsigned int bytes_written;
  386. unsigned int stride = instance->tx_channel.stride;
  387. for (bytes_written = 0; bytes_written < avail_space && ctrl->len;
  388. bytes_written += stride, target += stride) {
  389. unsigned int data_len = min_t(unsigned int, skb->len, ATM_CELL_PAYLOAD);
  390. unsigned int left = ATM_CELL_PAYLOAD - data_len;
  391. u8 *ptr = target;
  392. ptr[0] = vcc->vpi >> 4;
  393. ptr[1] = (vcc->vpi << 4) | (vcc->vci >> 12);
  394. ptr[2] = vcc->vci >> 4;
  395. ptr[3] = vcc->vci << 4;
  396. ptr[4] = 0xec;
  397. ptr += ATM_CELL_HEADER;
  398. skb_copy_from_linear_data(skb, ptr, data_len);
  399. ptr += data_len;
  400. __skb_pull(skb, data_len);
  401. if (!left)
  402. continue;
  403. memset(ptr, 0, left);
  404. if (left >= ATM_AAL5_TRAILER) { /* trailer will go in this cell */
  405. u8 *trailer = target + ATM_CELL_SIZE - ATM_AAL5_TRAILER;
  406. /* trailer[0] = 0; UU = 0 */
  407. /* trailer[1] = 0; CPI = 0 */
  408. trailer[2] = ctrl->len >> 8;
  409. trailer[3] = ctrl->len;
  410. ctrl->crc = ~crc32_be(ctrl->crc, ptr, left - 4);
  411. trailer[4] = ctrl->crc >> 24;
  412. trailer[5] = ctrl->crc >> 16;
  413. trailer[6] = ctrl->crc >> 8;
  414. trailer[7] = ctrl->crc;
  415. target[3] |= 0x2; /* adjust PTI */
  416. ctrl->len = 0; /* tag this skb finished */
  417. } else
  418. ctrl->crc = crc32_be(ctrl->crc, ptr, left);
  419. }
  420. return bytes_written;
  421. }
  422. /**************
  423. ** receive **
  424. **************/
  425. static void usbatm_rx_process(unsigned long data)
  426. {
  427. struct usbatm_data *instance = (struct usbatm_data *)data;
  428. struct urb *urb;
  429. while ((urb = usbatm_pop_urb(&instance->rx_channel))) {
  430. vdbg(&instance->usb_intf->dev,
  431. "%s: processing urb 0x%p", __func__, urb);
  432. if (usb_pipeisoc(urb->pipe)) {
  433. unsigned char *merge_start = NULL;
  434. unsigned int merge_length = 0;
  435. const unsigned int packet_size = instance->rx_channel.packet_size;
  436. int i;
  437. for (i = 0; i < urb->number_of_packets; i++) {
  438. if (!urb->iso_frame_desc[i].status) {
  439. unsigned int actual_length = urb->iso_frame_desc[i].actual_length;
  440. if (!merge_length)
  441. merge_start = (unsigned char *)urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  442. merge_length += actual_length;
  443. if (merge_length && (actual_length < packet_size)) {
  444. usbatm_extract_cells(instance, merge_start, merge_length);
  445. merge_length = 0;
  446. }
  447. } else {
  448. atm_rldbg(instance, "%s: status %d in frame %d!\n", __func__, urb->status, i);
  449. if (merge_length)
  450. usbatm_extract_cells(instance, merge_start, merge_length);
  451. merge_length = 0;
  452. instance->buf_usage = 0;
  453. }
  454. }
  455. if (merge_length)
  456. usbatm_extract_cells(instance, merge_start, merge_length);
  457. } else
  458. if (!urb->status)
  459. usbatm_extract_cells(instance, urb->transfer_buffer, urb->actual_length);
  460. else
  461. instance->buf_usage = 0;
  462. if (usbatm_submit_urb(urb))
  463. return;
  464. }
  465. }
  466. /***********
  467. ** send **
  468. ***********/
  469. static void usbatm_tx_process(unsigned long data)
  470. {
  471. struct usbatm_data *instance = (struct usbatm_data *)data;
  472. struct sk_buff *skb = instance->current_skb;
  473. struct urb *urb = NULL;
  474. const unsigned int buf_size = instance->tx_channel.buf_size;
  475. unsigned int bytes_written = 0;
  476. u8 *buffer = NULL;
  477. if (!skb)
  478. skb = skb_dequeue(&instance->sndqueue);
  479. while (skb) {
  480. if (!urb) {
  481. urb = usbatm_pop_urb(&instance->tx_channel);
  482. if (!urb)
  483. break; /* no more senders */
  484. buffer = urb->transfer_buffer;
  485. bytes_written = (urb->status == -EAGAIN) ?
  486. urb->transfer_buffer_length : 0;
  487. }
  488. bytes_written += usbatm_write_cells(instance, skb,
  489. buffer + bytes_written,
  490. buf_size - bytes_written);
  491. vdbg(&instance->usb_intf->dev,
  492. "%s: wrote %u bytes from skb 0x%p to urb 0x%p",
  493. __func__, bytes_written, skb, urb);
  494. if (!UDSL_SKB(skb)->len) {
  495. struct atm_vcc *vcc = UDSL_SKB(skb)->atm.vcc;
  496. usbatm_pop(vcc, skb);
  497. atomic_inc(&vcc->stats->tx);
  498. skb = skb_dequeue(&instance->sndqueue);
  499. }
  500. if (bytes_written == buf_size || (!skb && bytes_written)) {
  501. urb->transfer_buffer_length = bytes_written;
  502. if (usbatm_submit_urb(urb))
  503. break;
  504. urb = NULL;
  505. }
  506. }
  507. instance->current_skb = skb;
  508. }
  509. static void usbatm_cancel_send(struct usbatm_data *instance,
  510. struct atm_vcc *vcc)
  511. {
  512. struct sk_buff *skb, *n;
  513. spin_lock_irq(&instance->sndqueue.lock);
  514. skb_queue_walk_safe(&instance->sndqueue, skb, n) {
  515. if (UDSL_SKB(skb)->atm.vcc == vcc) {
  516. atm_dbg(instance, "%s: popping skb 0x%p\n", __func__, skb);
  517. __skb_unlink(skb, &instance->sndqueue);
  518. usbatm_pop(vcc, skb);
  519. }
  520. }
  521. spin_unlock_irq(&instance->sndqueue.lock);
  522. tasklet_disable(&instance->tx_channel.tasklet);
  523. if ((skb = instance->current_skb) && (UDSL_SKB(skb)->atm.vcc == vcc)) {
  524. atm_dbg(instance, "%s: popping current skb (0x%p)\n", __func__, skb);
  525. instance->current_skb = NULL;
  526. usbatm_pop(vcc, skb);
  527. }
  528. tasklet_enable(&instance->tx_channel.tasklet);
  529. }
  530. static int usbatm_atm_send(struct atm_vcc *vcc, struct sk_buff *skb)
  531. {
  532. struct usbatm_data *instance = vcc->dev->dev_data;
  533. struct usbatm_control *ctrl = UDSL_SKB(skb);
  534. int err;
  535. /* racy disconnection check - fine */
  536. if (!instance || instance->disconnected) {
  537. #ifdef VERBOSE_DEBUG
  538. printk_ratelimited(KERN_DEBUG "%s: %s!\n", __func__, instance ? "disconnected" : "NULL instance");
  539. #endif
  540. err = -ENODEV;
  541. goto fail;
  542. }
  543. if (vcc->qos.aal != ATM_AAL5) {
  544. atm_rldbg(instance, "%s: unsupported ATM type %d!\n", __func__, vcc->qos.aal);
  545. err = -EINVAL;
  546. goto fail;
  547. }
  548. if (skb->len > ATM_MAX_AAL5_PDU) {
  549. atm_rldbg(instance, "%s: packet too long (%d vs %d)!\n",
  550. __func__, skb->len, ATM_MAX_AAL5_PDU);
  551. err = -EINVAL;
  552. goto fail;
  553. }
  554. PACKETDEBUG(instance, skb->data, skb->len);
  555. /* initialize the control block */
  556. ctrl->atm.vcc = vcc;
  557. ctrl->len = skb->len;
  558. ctrl->crc = crc32_be(~0, skb->data, skb->len);
  559. skb_queue_tail(&instance->sndqueue, skb);
  560. tasklet_schedule(&instance->tx_channel.tasklet);
  561. return 0;
  562. fail:
  563. usbatm_pop(vcc, skb);
  564. return err;
  565. }
  566. /********************
  567. ** bean counting **
  568. ********************/
  569. static void usbatm_destroy_instance(struct kref *kref)
  570. {
  571. struct usbatm_data *instance = container_of(kref, struct usbatm_data, refcount);
  572. tasklet_kill(&instance->rx_channel.tasklet);
  573. tasklet_kill(&instance->tx_channel.tasklet);
  574. usb_put_dev(instance->usb_dev);
  575. kfree(instance);
  576. }
  577. static void usbatm_get_instance(struct usbatm_data *instance)
  578. {
  579. kref_get(&instance->refcount);
  580. }
  581. static void usbatm_put_instance(struct usbatm_data *instance)
  582. {
  583. kref_put(&instance->refcount, usbatm_destroy_instance);
  584. }
  585. /**********
  586. ** ATM **
  587. **********/
  588. static void usbatm_atm_dev_close(struct atm_dev *atm_dev)
  589. {
  590. struct usbatm_data *instance = atm_dev->dev_data;
  591. if (!instance)
  592. return;
  593. atm_dev->dev_data = NULL; /* catch bugs */
  594. usbatm_put_instance(instance); /* taken in usbatm_atm_init */
  595. }
  596. static int usbatm_atm_proc_read(struct atm_dev *atm_dev, loff_t * pos, char *page)
  597. {
  598. struct usbatm_data *instance = atm_dev->dev_data;
  599. int left = *pos;
  600. if (!instance)
  601. return -ENODEV;
  602. if (!left--)
  603. return sprintf(page, "%s\n", instance->description);
  604. if (!left--)
  605. return sprintf(page, "MAC: %pM\n", atm_dev->esi);
  606. if (!left--)
  607. return sprintf(page,
  608. "AAL5: tx %d ( %d err ), rx %d ( %d err, %d drop )\n",
  609. atomic_read(&atm_dev->stats.aal5.tx),
  610. atomic_read(&atm_dev->stats.aal5.tx_err),
  611. atomic_read(&atm_dev->stats.aal5.rx),
  612. atomic_read(&atm_dev->stats.aal5.rx_err),
  613. atomic_read(&atm_dev->stats.aal5.rx_drop));
  614. if (!left--) {
  615. if (instance->disconnected)
  616. return sprintf(page, "Disconnected\n");
  617. else
  618. switch (atm_dev->signal) {
  619. case ATM_PHY_SIG_FOUND:
  620. return sprintf(page, "Line up\n");
  621. case ATM_PHY_SIG_LOST:
  622. return sprintf(page, "Line down\n");
  623. default:
  624. return sprintf(page, "Line state unknown\n");
  625. }
  626. }
  627. return 0;
  628. }
  629. static int usbatm_atm_open(struct atm_vcc *vcc)
  630. {
  631. struct usbatm_data *instance = vcc->dev->dev_data;
  632. struct usbatm_vcc_data *new = NULL;
  633. int ret;
  634. int vci = vcc->vci;
  635. short vpi = vcc->vpi;
  636. if (!instance)
  637. return -ENODEV;
  638. /* only support AAL5 */
  639. if ((vcc->qos.aal != ATM_AAL5)) {
  640. atm_warn(instance, "%s: unsupported ATM type %d!\n", __func__, vcc->qos.aal);
  641. return -EINVAL;
  642. }
  643. /* sanity checks */
  644. if ((vcc->qos.rxtp.max_sdu < 0) || (vcc->qos.rxtp.max_sdu > ATM_MAX_AAL5_PDU)) {
  645. atm_dbg(instance, "%s: max_sdu %d out of range!\n", __func__, vcc->qos.rxtp.max_sdu);
  646. return -EINVAL;
  647. }
  648. mutex_lock(&instance->serialize); /* vs self, usbatm_atm_close, usbatm_usb_disconnect */
  649. if (instance->disconnected) {
  650. atm_dbg(instance, "%s: disconnected!\n", __func__);
  651. ret = -ENODEV;
  652. goto fail;
  653. }
  654. if (usbatm_find_vcc(instance, vpi, vci)) {
  655. atm_dbg(instance, "%s: %hd/%d already in use!\n", __func__, vpi, vci);
  656. ret = -EADDRINUSE;
  657. goto fail;
  658. }
  659. if (!(new = kzalloc(sizeof(struct usbatm_vcc_data), GFP_KERNEL))) {
  660. atm_err(instance, "%s: no memory for vcc_data!\n", __func__);
  661. ret = -ENOMEM;
  662. goto fail;
  663. }
  664. new->vcc = vcc;
  665. new->vpi = vpi;
  666. new->vci = vci;
  667. new->sarb = alloc_skb(usbatm_pdu_length(vcc->qos.rxtp.max_sdu), GFP_KERNEL);
  668. if (!new->sarb) {
  669. atm_err(instance, "%s: no memory for SAR buffer!\n", __func__);
  670. ret = -ENOMEM;
  671. goto fail;
  672. }
  673. vcc->dev_data = new;
  674. tasklet_disable(&instance->rx_channel.tasklet);
  675. instance->cached_vcc = new;
  676. instance->cached_vpi = vpi;
  677. instance->cached_vci = vci;
  678. list_add(&new->list, &instance->vcc_list);
  679. tasklet_enable(&instance->rx_channel.tasklet);
  680. set_bit(ATM_VF_ADDR, &vcc->flags);
  681. set_bit(ATM_VF_PARTIAL, &vcc->flags);
  682. set_bit(ATM_VF_READY, &vcc->flags);
  683. mutex_unlock(&instance->serialize);
  684. atm_dbg(instance, "%s: allocated vcc data 0x%p\n", __func__, new);
  685. return 0;
  686. fail:
  687. kfree(new);
  688. mutex_unlock(&instance->serialize);
  689. return ret;
  690. }
  691. static void usbatm_atm_close(struct atm_vcc *vcc)
  692. {
  693. struct usbatm_data *instance = vcc->dev->dev_data;
  694. struct usbatm_vcc_data *vcc_data = vcc->dev_data;
  695. if (!instance || !vcc_data)
  696. return;
  697. usbatm_cancel_send(instance, vcc);
  698. mutex_lock(&instance->serialize); /* vs self, usbatm_atm_open, usbatm_usb_disconnect */
  699. tasklet_disable(&instance->rx_channel.tasklet);
  700. if (instance->cached_vcc == vcc_data) {
  701. instance->cached_vcc = NULL;
  702. instance->cached_vpi = ATM_VPI_UNSPEC;
  703. instance->cached_vci = ATM_VCI_UNSPEC;
  704. }
  705. list_del(&vcc_data->list);
  706. tasklet_enable(&instance->rx_channel.tasklet);
  707. kfree_skb(vcc_data->sarb);
  708. vcc_data->sarb = NULL;
  709. kfree(vcc_data);
  710. vcc->dev_data = NULL;
  711. vcc->vpi = ATM_VPI_UNSPEC;
  712. vcc->vci = ATM_VCI_UNSPEC;
  713. clear_bit(ATM_VF_READY, &vcc->flags);
  714. clear_bit(ATM_VF_PARTIAL, &vcc->flags);
  715. clear_bit(ATM_VF_ADDR, &vcc->flags);
  716. mutex_unlock(&instance->serialize);
  717. }
  718. static int usbatm_atm_ioctl(struct atm_dev *atm_dev, unsigned int cmd,
  719. void __user * arg)
  720. {
  721. struct usbatm_data *instance = atm_dev->dev_data;
  722. if (!instance || instance->disconnected)
  723. return -ENODEV;
  724. switch (cmd) {
  725. case ATM_QUERYLOOP:
  726. return put_user(ATM_LM_NONE, (int __user *)arg) ? -EFAULT : 0;
  727. default:
  728. return -ENOIOCTLCMD;
  729. }
  730. }
  731. static int usbatm_atm_init(struct usbatm_data *instance)
  732. {
  733. struct atm_dev *atm_dev;
  734. int ret, i;
  735. /* ATM init. The ATM initialization scheme suffers from an intrinsic race
  736. * condition: callbacks we register can be executed at once, before we have
  737. * initialized the struct atm_dev. To protect against this, all callbacks
  738. * abort if atm_dev->dev_data is NULL. */
  739. atm_dev = atm_dev_register(instance->driver_name,
  740. &instance->usb_intf->dev, &usbatm_atm_devops,
  741. -1, NULL);
  742. if (!atm_dev) {
  743. usb_err(instance, "%s: failed to register ATM device!\n", __func__);
  744. return -1;
  745. }
  746. instance->atm_dev = atm_dev;
  747. atm_dev->ci_range.vpi_bits = ATM_CI_MAX;
  748. atm_dev->ci_range.vci_bits = ATM_CI_MAX;
  749. atm_dev->signal = ATM_PHY_SIG_UNKNOWN;
  750. /* temp init ATM device, set to 128kbit */
  751. atm_dev->link_rate = 128 * 1000 / 424;
  752. if (instance->driver->atm_start && ((ret = instance->driver->atm_start(instance, atm_dev)) < 0)) {
  753. atm_err(instance, "%s: atm_start failed: %d!\n", __func__, ret);
  754. goto fail;
  755. }
  756. usbatm_get_instance(instance); /* dropped in usbatm_atm_dev_close */
  757. /* ready for ATM callbacks */
  758. mb();
  759. atm_dev->dev_data = instance;
  760. /* submit all rx URBs */
  761. for (i = 0; i < num_rcv_urbs; i++)
  762. usbatm_submit_urb(instance->urbs[i]);
  763. return 0;
  764. fail:
  765. instance->atm_dev = NULL;
  766. atm_dev_deregister(atm_dev); /* usbatm_atm_dev_close will eventually be called */
  767. return ret;
  768. }
  769. /**********
  770. ** USB **
  771. **********/
  772. static int usbatm_do_heavy_init(void *arg)
  773. {
  774. struct usbatm_data *instance = arg;
  775. int ret;
  776. allow_signal(SIGTERM);
  777. complete(&instance->thread_started);
  778. ret = instance->driver->heavy_init(instance, instance->usb_intf);
  779. if (!ret)
  780. ret = usbatm_atm_init(instance);
  781. mutex_lock(&instance->serialize);
  782. instance->thread = NULL;
  783. mutex_unlock(&instance->serialize);
  784. complete_and_exit(&instance->thread_exited, ret);
  785. }
  786. static int usbatm_heavy_init(struct usbatm_data *instance)
  787. {
  788. struct task_struct *t;
  789. t = kthread_create(usbatm_do_heavy_init, instance, "%s",
  790. instance->driver->driver_name);
  791. if (IS_ERR(t)) {
  792. usb_err(instance, "%s: failed to create kernel_thread (%ld)!\n",
  793. __func__, PTR_ERR(t));
  794. return PTR_ERR(t);
  795. }
  796. instance->thread = t;
  797. wake_up_process(t);
  798. wait_for_completion(&instance->thread_started);
  799. return 0;
  800. }
  801. static void usbatm_tasklet_schedule(unsigned long data)
  802. {
  803. tasklet_schedule((struct tasklet_struct *) data);
  804. }
  805. static void usbatm_init_channel(struct usbatm_channel *channel)
  806. {
  807. spin_lock_init(&channel->lock);
  808. INIT_LIST_HEAD(&channel->list);
  809. channel->delay.function = usbatm_tasklet_schedule;
  810. channel->delay.data = (unsigned long) &channel->tasklet;
  811. init_timer(&channel->delay);
  812. }
  813. int usbatm_usb_probe(struct usb_interface *intf, const struct usb_device_id *id,
  814. struct usbatm_driver *driver)
  815. {
  816. struct device *dev = &intf->dev;
  817. struct usb_device *usb_dev = interface_to_usbdev(intf);
  818. struct usbatm_data *instance;
  819. char *buf;
  820. int error = -ENOMEM;
  821. int i, length;
  822. unsigned int maxpacket, num_packets;
  823. /* instance init */
  824. instance = kzalloc(sizeof(*instance) + sizeof(struct urb *) * (num_rcv_urbs + num_snd_urbs), GFP_KERNEL);
  825. if (!instance) {
  826. dev_err(dev, "%s: no memory for instance data!\n", __func__);
  827. return -ENOMEM;
  828. }
  829. /* public fields */
  830. instance->driver = driver;
  831. strlcpy(instance->driver_name, driver->driver_name,
  832. sizeof(instance->driver_name));
  833. instance->usb_dev = usb_dev;
  834. instance->usb_intf = intf;
  835. buf = instance->description;
  836. length = sizeof(instance->description);
  837. if ((i = usb_string(usb_dev, usb_dev->descriptor.iProduct, buf, length)) < 0)
  838. goto bind;
  839. buf += i;
  840. length -= i;
  841. i = scnprintf(buf, length, " (");
  842. buf += i;
  843. length -= i;
  844. if (length <= 0 || (i = usb_make_path(usb_dev, buf, length)) < 0)
  845. goto bind;
  846. buf += i;
  847. length -= i;
  848. snprintf(buf, length, ")");
  849. bind:
  850. if (driver->bind && (error = driver->bind(instance, intf, id)) < 0) {
  851. dev_err(dev, "%s: bind failed: %d!\n", __func__, error);
  852. goto fail_free;
  853. }
  854. /* private fields */
  855. kref_init(&instance->refcount); /* dropped in usbatm_usb_disconnect */
  856. mutex_init(&instance->serialize);
  857. instance->thread = NULL;
  858. init_completion(&instance->thread_started);
  859. init_completion(&instance->thread_exited);
  860. INIT_LIST_HEAD(&instance->vcc_list);
  861. skb_queue_head_init(&instance->sndqueue);
  862. usbatm_init_channel(&instance->rx_channel);
  863. usbatm_init_channel(&instance->tx_channel);
  864. tasklet_init(&instance->rx_channel.tasklet, usbatm_rx_process, (unsigned long)instance);
  865. tasklet_init(&instance->tx_channel.tasklet, usbatm_tx_process, (unsigned long)instance);
  866. instance->rx_channel.stride = ATM_CELL_SIZE + driver->rx_padding;
  867. instance->tx_channel.stride = ATM_CELL_SIZE + driver->tx_padding;
  868. instance->rx_channel.usbatm = instance->tx_channel.usbatm = instance;
  869. if ((instance->flags & UDSL_USE_ISOC) && driver->isoc_in)
  870. instance->rx_channel.endpoint = usb_rcvisocpipe(usb_dev, driver->isoc_in);
  871. else
  872. instance->rx_channel.endpoint = usb_rcvbulkpipe(usb_dev, driver->bulk_in);
  873. instance->tx_channel.endpoint = usb_sndbulkpipe(usb_dev, driver->bulk_out);
  874. /* tx buffer size must be a positive multiple of the stride */
  875. instance->tx_channel.buf_size = max(instance->tx_channel.stride,
  876. snd_buf_bytes - (snd_buf_bytes % instance->tx_channel.stride));
  877. /* rx buffer size must be a positive multiple of the endpoint maxpacket */
  878. maxpacket = usb_maxpacket(usb_dev, instance->rx_channel.endpoint, 0);
  879. if ((maxpacket < 1) || (maxpacket > UDSL_MAX_BUF_SIZE)) {
  880. dev_err(dev, "%s: invalid endpoint %02x!\n", __func__,
  881. usb_pipeendpoint(instance->rx_channel.endpoint));
  882. error = -EINVAL;
  883. goto fail_unbind;
  884. }
  885. num_packets = max(1U, (rcv_buf_bytes + maxpacket / 2) / maxpacket); /* round */
  886. if (num_packets * maxpacket > UDSL_MAX_BUF_SIZE)
  887. num_packets--;
  888. instance->rx_channel.buf_size = num_packets * maxpacket;
  889. instance->rx_channel.packet_size = maxpacket;
  890. for (i = 0; i < 2; i++) {
  891. struct usbatm_channel *channel = i ?
  892. &instance->tx_channel : &instance->rx_channel;
  893. dev_dbg(dev, "%s: using %d byte buffer for %s channel 0x%p\n",
  894. __func__, channel->buf_size, i ? "tx" : "rx", channel);
  895. }
  896. /* initialize urbs */
  897. for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++) {
  898. u8 *buffer;
  899. struct usbatm_channel *channel = i < num_rcv_urbs ?
  900. &instance->rx_channel : &instance->tx_channel;
  901. struct urb *urb;
  902. unsigned int iso_packets = usb_pipeisoc(channel->endpoint) ? channel->buf_size / channel->packet_size : 0;
  903. urb = usb_alloc_urb(iso_packets, GFP_KERNEL);
  904. if (!urb) {
  905. dev_err(dev, "%s: no memory for urb %d!\n", __func__, i);
  906. error = -ENOMEM;
  907. goto fail_unbind;
  908. }
  909. instance->urbs[i] = urb;
  910. /* zero the tx padding to avoid leaking information */
  911. buffer = kzalloc(channel->buf_size, GFP_KERNEL);
  912. if (!buffer) {
  913. dev_err(dev, "%s: no memory for buffer %d!\n", __func__, i);
  914. error = -ENOMEM;
  915. goto fail_unbind;
  916. }
  917. usb_fill_bulk_urb(urb, instance->usb_dev, channel->endpoint,
  918. buffer, channel->buf_size, usbatm_complete, channel);
  919. if (iso_packets) {
  920. int j;
  921. urb->interval = 1;
  922. urb->transfer_flags = URB_ISO_ASAP;
  923. urb->number_of_packets = iso_packets;
  924. for (j = 0; j < iso_packets; j++) {
  925. urb->iso_frame_desc[j].offset = channel->packet_size * j;
  926. urb->iso_frame_desc[j].length = channel->packet_size;
  927. }
  928. }
  929. /* put all tx URBs on the list of spares */
  930. if (i >= num_rcv_urbs)
  931. list_add_tail(&urb->urb_list, &channel->list);
  932. vdbg(&intf->dev, "%s: alloced buffer 0x%p buf size %u urb 0x%p",
  933. __func__, urb->transfer_buffer, urb->transfer_buffer_length, urb);
  934. }
  935. instance->cached_vpi = ATM_VPI_UNSPEC;
  936. instance->cached_vci = ATM_VCI_UNSPEC;
  937. instance->cell_buf = kmalloc(instance->rx_channel.stride, GFP_KERNEL);
  938. if (!instance->cell_buf) {
  939. dev_err(dev, "%s: no memory for cell buffer!\n", __func__);
  940. error = -ENOMEM;
  941. goto fail_unbind;
  942. }
  943. if (!(instance->flags & UDSL_SKIP_HEAVY_INIT) && driver->heavy_init) {
  944. error = usbatm_heavy_init(instance);
  945. } else {
  946. complete(&instance->thread_exited); /* pretend that heavy_init was run */
  947. error = usbatm_atm_init(instance);
  948. }
  949. if (error < 0)
  950. goto fail_unbind;
  951. usb_get_dev(usb_dev);
  952. usb_set_intfdata(intf, instance);
  953. return 0;
  954. fail_unbind:
  955. if (instance->driver->unbind)
  956. instance->driver->unbind(instance, intf);
  957. fail_free:
  958. kfree(instance->cell_buf);
  959. for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++) {
  960. if (instance->urbs[i])
  961. kfree(instance->urbs[i]->transfer_buffer);
  962. usb_free_urb(instance->urbs[i]);
  963. }
  964. kfree(instance);
  965. return error;
  966. }
  967. EXPORT_SYMBOL_GPL(usbatm_usb_probe);
  968. void usbatm_usb_disconnect(struct usb_interface *intf)
  969. {
  970. struct device *dev = &intf->dev;
  971. struct usbatm_data *instance = usb_get_intfdata(intf);
  972. struct usbatm_vcc_data *vcc_data;
  973. int i;
  974. if (!instance) {
  975. dev_dbg(dev, "%s: NULL instance!\n", __func__);
  976. return;
  977. }
  978. usb_set_intfdata(intf, NULL);
  979. mutex_lock(&instance->serialize);
  980. instance->disconnected = 1;
  981. if (instance->thread != NULL)
  982. send_sig(SIGTERM, instance->thread, 1);
  983. mutex_unlock(&instance->serialize);
  984. wait_for_completion(&instance->thread_exited);
  985. mutex_lock(&instance->serialize);
  986. list_for_each_entry(vcc_data, &instance->vcc_list, list)
  987. vcc_release_async(vcc_data->vcc, -EPIPE);
  988. mutex_unlock(&instance->serialize);
  989. tasklet_disable(&instance->rx_channel.tasklet);
  990. tasklet_disable(&instance->tx_channel.tasklet);
  991. for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++)
  992. usb_kill_urb(instance->urbs[i]);
  993. del_timer_sync(&instance->rx_channel.delay);
  994. del_timer_sync(&instance->tx_channel.delay);
  995. /* turn usbatm_[rt]x_process into something close to a no-op */
  996. /* no need to take the spinlock */
  997. INIT_LIST_HEAD(&instance->rx_channel.list);
  998. INIT_LIST_HEAD(&instance->tx_channel.list);
  999. tasklet_enable(&instance->rx_channel.tasklet);
  1000. tasklet_enable(&instance->tx_channel.tasklet);
  1001. if (instance->atm_dev && instance->driver->atm_stop)
  1002. instance->driver->atm_stop(instance, instance->atm_dev);
  1003. if (instance->driver->unbind)
  1004. instance->driver->unbind(instance, intf);
  1005. instance->driver_data = NULL;
  1006. for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++) {
  1007. kfree(instance->urbs[i]->transfer_buffer);
  1008. usb_free_urb(instance->urbs[i]);
  1009. }
  1010. kfree(instance->cell_buf);
  1011. /* ATM finalize */
  1012. if (instance->atm_dev) {
  1013. atm_dev_deregister(instance->atm_dev);
  1014. instance->atm_dev = NULL;
  1015. }
  1016. usbatm_put_instance(instance); /* taken in usbatm_usb_probe */
  1017. }
  1018. EXPORT_SYMBOL_GPL(usbatm_usb_disconnect);
  1019. /***********
  1020. ** init **
  1021. ***********/
  1022. static int __init usbatm_usb_init(void)
  1023. {
  1024. if (sizeof(struct usbatm_control) > FIELD_SIZEOF(struct sk_buff, cb)) {
  1025. printk(KERN_ERR "%s unusable with this kernel!\n", usbatm_driver_name);
  1026. return -EIO;
  1027. }
  1028. if ((num_rcv_urbs > UDSL_MAX_RCV_URBS)
  1029. || (num_snd_urbs > UDSL_MAX_SND_URBS)
  1030. || (rcv_buf_bytes < 1)
  1031. || (rcv_buf_bytes > UDSL_MAX_BUF_SIZE)
  1032. || (snd_buf_bytes < 1)
  1033. || (snd_buf_bytes > UDSL_MAX_BUF_SIZE))
  1034. return -EINVAL;
  1035. return 0;
  1036. }
  1037. module_init(usbatm_usb_init);
  1038. static void __exit usbatm_usb_exit(void)
  1039. {
  1040. }
  1041. module_exit(usbatm_usb_exit);
  1042. MODULE_AUTHOR(DRIVER_AUTHOR);
  1043. MODULE_DESCRIPTION(DRIVER_DESC);
  1044. MODULE_LICENSE("GPL");
  1045. MODULE_VERSION(DRIVER_VERSION);
  1046. /************
  1047. ** debug **
  1048. ************/
  1049. #ifdef VERBOSE_DEBUG
  1050. static int usbatm_print_packet(struct usbatm_data *instance,
  1051. const unsigned char *data, int len)
  1052. {
  1053. unsigned char buffer[256];
  1054. int i = 0, j = 0;
  1055. for (i = 0; i < len;) {
  1056. buffer[0] = '\0';
  1057. sprintf(buffer, "%.3d :", i);
  1058. for (j = 0; (j < 16) && (i < len); j++, i++)
  1059. sprintf(buffer, "%s %2.2x", buffer, data[i]);
  1060. dev_dbg(&instance->usb_intf->dev, "%s", buffer);
  1061. }
  1062. return i;
  1063. }
  1064. #endif