usbatm.c 35 KB

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