usbatm.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328
  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 ([email protected]) 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 ([email protected])
  25. *
  26. * 1.5A: - Version for inclusion in 2.5 series kernel
  27. * - Modifications by Richard Purdie ([email protected])
  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 [email protected])
  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 <[email protected]>"
  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, num_rcv_urbs + num_snd_urbs);
  813. instance = kzalloc(size, GFP_KERNEL);
  814. if (!instance)
  815. return -ENOMEM;
  816. /* public fields */
  817. instance->driver = driver;
  818. strscpy(instance->driver_name, driver->driver_name,
  819. sizeof(instance->driver_name));
  820. instance->usb_dev = usb_dev;
  821. instance->usb_intf = intf;
  822. buf = instance->description;
  823. length = sizeof(instance->description);
  824. if ((i = usb_string(usb_dev, usb_dev->descriptor.iProduct, buf, length)) < 0)
  825. goto bind;
  826. buf += i;
  827. length -= i;
  828. i = scnprintf(buf, length, " (");
  829. buf += i;
  830. length -= i;
  831. if (length <= 0 || (i = usb_make_path(usb_dev, buf, length)) < 0)
  832. goto bind;
  833. buf += i;
  834. length -= i;
  835. snprintf(buf, length, ")");
  836. bind:
  837. if (driver->bind && (error = driver->bind(instance, intf, id)) < 0) {
  838. dev_err(dev, "%s: bind failed: %d!\n", __func__, error);
  839. goto fail_free;
  840. }
  841. /* private fields */
  842. kref_init(&instance->refcount); /* dropped in usbatm_usb_disconnect */
  843. mutex_init(&instance->serialize);
  844. instance->thread = NULL;
  845. init_completion(&instance->thread_started);
  846. init_completion(&instance->thread_exited);
  847. INIT_LIST_HEAD(&instance->vcc_list);
  848. skb_queue_head_init(&instance->sndqueue);
  849. usbatm_init_channel(&instance->rx_channel);
  850. usbatm_init_channel(&instance->tx_channel);
  851. tasklet_setup(&instance->rx_channel.tasklet, usbatm_rx_process);
  852. tasklet_setup(&instance->tx_channel.tasklet, usbatm_tx_process);
  853. instance->rx_channel.stride = ATM_CELL_SIZE + driver->rx_padding;
  854. instance->tx_channel.stride = ATM_CELL_SIZE + driver->tx_padding;
  855. instance->rx_channel.usbatm = instance->tx_channel.usbatm = instance;
  856. if ((instance->flags & UDSL_USE_ISOC) && driver->isoc_in)
  857. instance->rx_channel.endpoint = usb_rcvisocpipe(usb_dev, driver->isoc_in);
  858. else
  859. instance->rx_channel.endpoint = usb_rcvbulkpipe(usb_dev, driver->bulk_in);
  860. instance->tx_channel.endpoint = usb_sndbulkpipe(usb_dev, driver->bulk_out);
  861. /* tx buffer size must be a positive multiple of the stride */
  862. instance->tx_channel.buf_size = max(instance->tx_channel.stride,
  863. snd_buf_bytes - (snd_buf_bytes % instance->tx_channel.stride));
  864. /* rx buffer size must be a positive multiple of the endpoint maxpacket */
  865. maxpacket = usb_maxpacket(usb_dev, instance->rx_channel.endpoint);
  866. if ((maxpacket < 1) || (maxpacket > UDSL_MAX_BUF_SIZE)) {
  867. dev_err(dev, "%s: invalid endpoint %02x!\n", __func__,
  868. usb_pipeendpoint(instance->rx_channel.endpoint));
  869. error = -EINVAL;
  870. goto fail_unbind;
  871. }
  872. num_packets = max(1U, (rcv_buf_bytes + maxpacket / 2) / maxpacket); /* round */
  873. if (num_packets * maxpacket > UDSL_MAX_BUF_SIZE)
  874. num_packets--;
  875. instance->rx_channel.buf_size = num_packets * maxpacket;
  876. instance->rx_channel.packet_size = maxpacket;
  877. for (i = 0; i < 2; i++) {
  878. struct usbatm_channel *channel = i ?
  879. &instance->tx_channel : &instance->rx_channel;
  880. dev_dbg(dev, "%s: using %d byte buffer for %s channel 0x%p\n",
  881. __func__, channel->buf_size, i ? "tx" : "rx", channel);
  882. }
  883. /* initialize urbs */
  884. for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++) {
  885. u8 *buffer;
  886. struct usbatm_channel *channel = i < num_rcv_urbs ?
  887. &instance->rx_channel : &instance->tx_channel;
  888. struct urb *urb;
  889. unsigned int iso_packets = usb_pipeisoc(channel->endpoint) ? channel->buf_size / channel->packet_size : 0;
  890. urb = usb_alloc_urb(iso_packets, GFP_KERNEL);
  891. if (!urb) {
  892. error = -ENOMEM;
  893. goto fail_unbind;
  894. }
  895. instance->urbs[i] = urb;
  896. /* zero the tx padding to avoid leaking information */
  897. buffer = kzalloc(channel->buf_size, GFP_KERNEL);
  898. if (!buffer) {
  899. error = -ENOMEM;
  900. goto fail_unbind;
  901. }
  902. usb_fill_bulk_urb(urb, instance->usb_dev, channel->endpoint,
  903. buffer, channel->buf_size, usbatm_complete, channel);
  904. if (iso_packets) {
  905. int j;
  906. urb->interval = 1;
  907. urb->transfer_flags = URB_ISO_ASAP;
  908. urb->number_of_packets = iso_packets;
  909. for (j = 0; j < iso_packets; j++) {
  910. urb->iso_frame_desc[j].offset = channel->packet_size * j;
  911. urb->iso_frame_desc[j].length = channel->packet_size;
  912. }
  913. }
  914. /* put all tx URBs on the list of spares */
  915. if (i >= num_rcv_urbs)
  916. list_add_tail(&urb->urb_list, &channel->list);
  917. vdbg(&intf->dev, "%s: alloced buffer 0x%p buf size %u urb 0x%p",
  918. __func__, urb->transfer_buffer, urb->transfer_buffer_length, urb);
  919. }
  920. instance->cached_vpi = ATM_VPI_UNSPEC;
  921. instance->cached_vci = ATM_VCI_UNSPEC;
  922. instance->cell_buf = kmalloc(instance->rx_channel.stride, GFP_KERNEL);
  923. if (!instance->cell_buf) {
  924. error = -ENOMEM;
  925. goto fail_unbind;
  926. }
  927. if (!(instance->flags & UDSL_SKIP_HEAVY_INIT) && driver->heavy_init) {
  928. error = usbatm_heavy_init(instance);
  929. } else {
  930. complete(&instance->thread_exited); /* pretend that heavy_init was run */
  931. error = usbatm_atm_init(instance);
  932. }
  933. if (error < 0)
  934. goto fail_unbind;
  935. usb_get_dev(usb_dev);
  936. usb_set_intfdata(intf, instance);
  937. return 0;
  938. fail_unbind:
  939. if (instance->driver->unbind)
  940. instance->driver->unbind(instance, intf);
  941. fail_free:
  942. kfree(instance->cell_buf);
  943. for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++) {
  944. if (instance->urbs[i])
  945. kfree(instance->urbs[i]->transfer_buffer);
  946. usb_free_urb(instance->urbs[i]);
  947. }
  948. kfree(instance);
  949. return error;
  950. }
  951. EXPORT_SYMBOL_GPL(usbatm_usb_probe);
  952. void usbatm_usb_disconnect(struct usb_interface *intf)
  953. {
  954. struct device *dev = &intf->dev;
  955. struct usbatm_data *instance = usb_get_intfdata(intf);
  956. struct usbatm_vcc_data *vcc_data;
  957. int i;
  958. if (!instance) {
  959. dev_dbg(dev, "%s: NULL instance!\n", __func__);
  960. return;
  961. }
  962. usb_set_intfdata(intf, NULL);
  963. mutex_lock(&instance->serialize);
  964. instance->disconnected = 1;
  965. if (instance->thread != NULL)
  966. send_sig(SIGTERM, instance->thread, 1);
  967. mutex_unlock(&instance->serialize);
  968. wait_for_completion(&instance->thread_exited);
  969. mutex_lock(&instance->serialize);
  970. list_for_each_entry(vcc_data, &instance->vcc_list, list)
  971. vcc_release_async(vcc_data->vcc, -EPIPE);
  972. mutex_unlock(&instance->serialize);
  973. tasklet_disable(&instance->rx_channel.tasklet);
  974. tasklet_disable(&instance->tx_channel.tasklet);
  975. for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++)
  976. usb_kill_urb(instance->urbs[i]);
  977. del_timer_sync(&instance->rx_channel.delay);
  978. del_timer_sync(&instance->tx_channel.delay);
  979. /* turn usbatm_[rt]x_process into something close to a no-op */
  980. /* no need to take the spinlock */
  981. INIT_LIST_HEAD(&instance->rx_channel.list);
  982. INIT_LIST_HEAD(&instance->tx_channel.list);
  983. tasklet_enable(&instance->rx_channel.tasklet);
  984. tasklet_enable(&instance->tx_channel.tasklet);
  985. if (instance->atm_dev && instance->driver->atm_stop)
  986. instance->driver->atm_stop(instance, instance->atm_dev);
  987. if (instance->driver->unbind)
  988. instance->driver->unbind(instance, intf);
  989. instance->driver_data = NULL;
  990. for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++) {
  991. kfree(instance->urbs[i]->transfer_buffer);
  992. usb_free_urb(instance->urbs[i]);
  993. }
  994. kfree(instance->cell_buf);
  995. /* ATM finalize */
  996. if (instance->atm_dev) {
  997. atm_dev_deregister(instance->atm_dev);
  998. instance->atm_dev = NULL;
  999. }
  1000. usbatm_put_instance(instance); /* taken in usbatm_usb_probe */
  1001. }
  1002. EXPORT_SYMBOL_GPL(usbatm_usb_disconnect);
  1003. /***********
  1004. ** init **
  1005. ***********/
  1006. static int __init usbatm_usb_init(void)
  1007. {
  1008. if (sizeof(struct usbatm_control) > sizeof_field(struct sk_buff, cb)) {
  1009. pr_err("%s unusable with this kernel!\n", usbatm_driver_name);
  1010. return -EIO;
  1011. }
  1012. if ((num_rcv_urbs > UDSL_MAX_RCV_URBS)
  1013. || (num_snd_urbs > UDSL_MAX_SND_URBS)
  1014. || (rcv_buf_bytes < 1)
  1015. || (rcv_buf_bytes > UDSL_MAX_BUF_SIZE)
  1016. || (snd_buf_bytes < 1)
  1017. || (snd_buf_bytes > UDSL_MAX_BUF_SIZE))
  1018. return -EINVAL;
  1019. return 0;
  1020. }
  1021. module_init(usbatm_usb_init);
  1022. static void __exit usbatm_usb_exit(void)
  1023. {
  1024. }
  1025. module_exit(usbatm_usb_exit);
  1026. MODULE_AUTHOR(DRIVER_AUTHOR);
  1027. MODULE_DESCRIPTION(DRIVER_DESC);
  1028. MODULE_LICENSE("GPL");
  1029. /************
  1030. ** debug **
  1031. ************/
  1032. #ifdef VERBOSE_DEBUG
  1033. static int usbatm_print_packet(struct usbatm_data *instance,
  1034. const unsigned char *data, int len)
  1035. {
  1036. unsigned char buffer[256];
  1037. int i = 0, j = 0;
  1038. for (i = 0; i < len;) {
  1039. buffer[0] = '\0';
  1040. sprintf(buffer, "%.3d :", i);
  1041. for (j = 0; (j < 16) && (i < len); j++, i++)
  1042. sprintf(buffer, "%s %2.2x", buffer, data[i]);
  1043. dev_dbg(&instance->usb_intf->dev, "%s", buffer);
  1044. }
  1045. return i;
  1046. }
  1047. #endif