usbatm.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400
  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. #ifdef VERBOSE_DEBUG
  81. static int usbatm_print_packet(const unsigned char *data, int len);
  82. #define PACKETDEBUG(arg...) usbatm_print_packet (arg)
  83. #define vdbg(arg...) dbg (arg)
  84. #else
  85. #define PACKETDEBUG(arg...)
  86. #define vdbg(arg...)
  87. #endif
  88. #define DRIVER_AUTHOR "Johan Verrept, Duncan Sands <duncan.sands@free.fr>"
  89. #define DRIVER_VERSION "1.10"
  90. #define DRIVER_DESC "Generic USB ATM/DSL I/O, version " DRIVER_VERSION
  91. static const char usbatm_driver_name[] = "usbatm";
  92. #define UDSL_MAX_RCV_URBS 16
  93. #define UDSL_MAX_SND_URBS 16
  94. #define UDSL_MAX_BUF_SIZE 65536
  95. #define UDSL_DEFAULT_RCV_URBS 4
  96. #define UDSL_DEFAULT_SND_URBS 4
  97. #define UDSL_DEFAULT_RCV_BUF_SIZE 3392 /* 64 * ATM_CELL_SIZE */
  98. #define UDSL_DEFAULT_SND_BUF_SIZE 3392 /* 64 * ATM_CELL_SIZE */
  99. #define ATM_CELL_HEADER (ATM_CELL_SIZE - ATM_CELL_PAYLOAD)
  100. #define THROTTLE_MSECS 100 /* delay to recover processing after urb submission fails */
  101. static unsigned int num_rcv_urbs = UDSL_DEFAULT_RCV_URBS;
  102. static unsigned int num_snd_urbs = UDSL_DEFAULT_SND_URBS;
  103. static unsigned int rcv_buf_bytes = UDSL_DEFAULT_RCV_BUF_SIZE;
  104. static unsigned int snd_buf_bytes = UDSL_DEFAULT_SND_BUF_SIZE;
  105. module_param(num_rcv_urbs, uint, S_IRUGO);
  106. MODULE_PARM_DESC(num_rcv_urbs,
  107. "Number of urbs used for reception (range: 0-"
  108. __MODULE_STRING(UDSL_MAX_RCV_URBS) ", default: "
  109. __MODULE_STRING(UDSL_DEFAULT_RCV_URBS) ")");
  110. module_param(num_snd_urbs, uint, S_IRUGO);
  111. MODULE_PARM_DESC(num_snd_urbs,
  112. "Number of urbs used for transmission (range: 0-"
  113. __MODULE_STRING(UDSL_MAX_SND_URBS) ", default: "
  114. __MODULE_STRING(UDSL_DEFAULT_SND_URBS) ")");
  115. module_param(rcv_buf_bytes, uint, S_IRUGO);
  116. MODULE_PARM_DESC(rcv_buf_bytes,
  117. "Size of the buffers used for reception, in bytes (range: 1-"
  118. __MODULE_STRING(UDSL_MAX_BUF_SIZE) ", default: "
  119. __MODULE_STRING(UDSL_DEFAULT_RCV_BUF_SIZE) ")");
  120. module_param(snd_buf_bytes, uint, S_IRUGO);
  121. MODULE_PARM_DESC(snd_buf_bytes,
  122. "Size of the buffers used for transmission, in bytes (range: 1-"
  123. __MODULE_STRING(UDSL_MAX_BUF_SIZE) ", default: "
  124. __MODULE_STRING(UDSL_DEFAULT_SND_BUF_SIZE) ")");
  125. /* receive */
  126. struct usbatm_vcc_data {
  127. /* vpi/vci lookup */
  128. struct list_head list;
  129. short vpi;
  130. int vci;
  131. struct atm_vcc *vcc;
  132. /* raw cell reassembly */
  133. struct sk_buff *sarb;
  134. };
  135. /* send */
  136. struct usbatm_control {
  137. struct atm_skb_data atm;
  138. u32 len;
  139. u32 crc;
  140. };
  141. #define UDSL_SKB(x) ((struct usbatm_control *)(x)->cb)
  142. /* ATM */
  143. static void usbatm_atm_dev_close(struct atm_dev *atm_dev);
  144. static int usbatm_atm_open(struct atm_vcc *vcc);
  145. static void usbatm_atm_close(struct atm_vcc *vcc);
  146. static int usbatm_atm_ioctl(struct atm_dev *atm_dev, unsigned int cmd, void __user * arg);
  147. static int usbatm_atm_send(struct atm_vcc *vcc, struct sk_buff *skb);
  148. static int usbatm_atm_proc_read(struct atm_dev *atm_dev, loff_t * pos, char *page);
  149. static struct atmdev_ops usbatm_atm_devops = {
  150. .dev_close = usbatm_atm_dev_close,
  151. .open = usbatm_atm_open,
  152. .close = usbatm_atm_close,
  153. .ioctl = usbatm_atm_ioctl,
  154. .send = usbatm_atm_send,
  155. .proc_read = usbatm_atm_proc_read,
  156. .owner = THIS_MODULE,
  157. };
  158. /***********
  159. ** misc **
  160. ***********/
  161. static inline unsigned int usbatm_pdu_length(unsigned int length)
  162. {
  163. length += ATM_CELL_PAYLOAD - 1 + ATM_AAL5_TRAILER;
  164. return length - length % ATM_CELL_PAYLOAD;
  165. }
  166. static inline void usbatm_pop(struct atm_vcc *vcc, struct sk_buff *skb)
  167. {
  168. if (vcc->pop)
  169. vcc->pop(vcc, skb);
  170. else
  171. dev_kfree_skb_any(skb);
  172. }
  173. /***********
  174. ** urbs **
  175. ************/
  176. static struct urb *usbatm_pop_urb(struct usbatm_channel *channel)
  177. {
  178. struct urb *urb;
  179. spin_lock_irq(&channel->lock);
  180. if (list_empty(&channel->list)) {
  181. spin_unlock_irq(&channel->lock);
  182. return NULL;
  183. }
  184. urb = list_entry(channel->list.next, struct urb, urb_list);
  185. list_del(&urb->urb_list);
  186. spin_unlock_irq(&channel->lock);
  187. return urb;
  188. }
  189. static int usbatm_submit_urb(struct urb *urb)
  190. {
  191. struct usbatm_channel *channel = urb->context;
  192. int ret;
  193. vdbg("%s: submitting urb 0x%p, size %u",
  194. __func__, urb, urb->transfer_buffer_length);
  195. ret = usb_submit_urb(urb, GFP_ATOMIC);
  196. if (ret) {
  197. if (printk_ratelimit())
  198. atm_warn(channel->usbatm, "%s: urb 0x%p submission failed (%d)!\n",
  199. __func__, urb, ret);
  200. /* consider all errors transient and return the buffer back to the queue */
  201. urb->status = -EAGAIN;
  202. spin_lock_irq(&channel->lock);
  203. /* must add to the front when sending; doesn't matter when receiving */
  204. list_add(&urb->urb_list, &channel->list);
  205. spin_unlock_irq(&channel->lock);
  206. /* make sure the channel doesn't stall */
  207. mod_timer(&channel->delay, jiffies + msecs_to_jiffies(THROTTLE_MSECS));
  208. }
  209. return ret;
  210. }
  211. static void usbatm_complete(struct urb *urb)
  212. {
  213. struct usbatm_channel *channel = urb->context;
  214. unsigned long flags;
  215. int status = urb->status;
  216. vdbg("%s: urb 0x%p, status %d, actual_length %d",
  217. __func__, urb, status, urb->actual_length);
  218. /* usually in_interrupt(), but not always */
  219. spin_lock_irqsave(&channel->lock, flags);
  220. /* must add to the back when receiving; doesn't matter when sending */
  221. list_add_tail(&urb->urb_list, &channel->list);
  222. spin_unlock_irqrestore(&channel->lock, flags);
  223. if (unlikely(status) &&
  224. (!(channel->usbatm->flags & UDSL_IGNORE_EILSEQ) ||
  225. status != -EILSEQ ))
  226. {
  227. if (status == -ESHUTDOWN)
  228. return;
  229. if (printk_ratelimit())
  230. atm_warn(channel->usbatm, "%s: urb 0x%p failed (%d)!\n",
  231. __func__, urb, status);
  232. /* throttle processing in case of an error */
  233. mod_timer(&channel->delay, jiffies + msecs_to_jiffies(THROTTLE_MSECS));
  234. } else
  235. tasklet_schedule(&channel->tasklet);
  236. }
  237. /*************
  238. ** decode **
  239. *************/
  240. static inline struct usbatm_vcc_data *usbatm_find_vcc(struct usbatm_data *instance,
  241. short vpi, int vci)
  242. {
  243. struct usbatm_vcc_data *vcc_data;
  244. list_for_each_entry(vcc_data, &instance->vcc_list, list)
  245. if ((vcc_data->vci == vci) && (vcc_data->vpi == vpi))
  246. return vcc_data;
  247. return NULL;
  248. }
  249. static void usbatm_extract_one_cell(struct usbatm_data *instance, unsigned char *source)
  250. {
  251. struct atm_vcc *vcc;
  252. struct sk_buff *sarb;
  253. short vpi = ((source[0] & 0x0f) << 4) | (source[1] >> 4);
  254. int vci = ((source[1] & 0x0f) << 12) | (source[2] << 4) | (source[3] >> 4);
  255. u8 pti = ((source[3] & 0xe) >> 1);
  256. vdbg("%s: vpi %hd, vci %d, pti %d", __func__, vpi, vci, pti);
  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. UDSL_ASSERT(sarb->tail + ATM_CELL_PAYLOAD <= sarb->end);
  282. }
  283. memcpy(skb_tail_pointer(sarb), source + ATM_CELL_HEADER, ATM_CELL_PAYLOAD);
  284. __skb_put(sarb, ATM_CELL_PAYLOAD);
  285. if (pti & 1) {
  286. struct sk_buff *skb;
  287. unsigned int length;
  288. unsigned int pdu_length;
  289. length = (source[ATM_CELL_SIZE - 6] << 8) + source[ATM_CELL_SIZE - 5];
  290. /* guard against overflow */
  291. if (length > ATM_MAX_AAL5_PDU) {
  292. atm_rldbg(instance, "%s: bogus length %u (vcc: 0x%p)!\n",
  293. __func__, length, vcc);
  294. atomic_inc(&vcc->stats->rx_err);
  295. goto out;
  296. }
  297. pdu_length = usbatm_pdu_length(length);
  298. if (sarb->len < pdu_length) {
  299. atm_rldbg(instance, "%s: bogus pdu_length %u (sarb->len: %u, vcc: 0x%p)!\n",
  300. __func__, pdu_length, sarb->len, vcc);
  301. atomic_inc(&vcc->stats->rx_err);
  302. goto out;
  303. }
  304. if (crc32_be(~0, skb_tail_pointer(sarb) - pdu_length, pdu_length) != 0xc704dd7b) {
  305. atm_rldbg(instance, "%s: packet failed crc check (vcc: 0x%p)!\n",
  306. __func__, vcc);
  307. atomic_inc(&vcc->stats->rx_err);
  308. goto out;
  309. }
  310. vdbg("%s: got packet (length: %u, pdu_length: %u, vcc: 0x%p)", __func__, length, pdu_length, vcc);
  311. if (!(skb = dev_alloc_skb(length))) {
  312. if (printk_ratelimit())
  313. atm_err(instance, "%s: no memory for skb (length: %u)!\n",
  314. __func__, length);
  315. atomic_inc(&vcc->stats->rx_drop);
  316. goto out;
  317. }
  318. vdbg("%s: allocated new sk_buff (skb: 0x%p, skb->truesize: %u)", __func__, skb, skb->truesize);
  319. if (!atm_charge(vcc, skb->truesize)) {
  320. atm_rldbg(instance, "%s: failed atm_charge (skb->truesize: %u)!\n",
  321. __func__, skb->truesize);
  322. dev_kfree_skb_any(skb);
  323. goto out; /* atm_charge increments rx_drop */
  324. }
  325. skb_copy_to_linear_data(skb,
  326. skb_tail_pointer(sarb) - pdu_length,
  327. length);
  328. __skb_put(skb, length);
  329. vdbg("%s: sending skb 0x%p, skb->len %u, skb->truesize %u",
  330. __func__, skb, skb->len, skb->truesize);
  331. PACKETDEBUG(skb->data, skb->len);
  332. vcc->push(vcc, skb);
  333. atomic_inc(&vcc->stats->rx);
  334. out:
  335. skb_trim(sarb, 0);
  336. }
  337. }
  338. static void usbatm_extract_cells(struct usbatm_data *instance,
  339. unsigned char *source, unsigned int avail_data)
  340. {
  341. unsigned int stride = instance->rx_channel.stride;
  342. unsigned int buf_usage = instance->buf_usage;
  343. /* extract cells from incoming data, taking into account that
  344. * the length of avail data may not be a multiple of stride */
  345. if (buf_usage > 0) {
  346. /* we have a partially received atm cell */
  347. unsigned char *cell_buf = instance->cell_buf;
  348. unsigned int space_left = stride - buf_usage;
  349. UDSL_ASSERT(buf_usage <= stride);
  350. if (avail_data >= space_left) {
  351. /* add new data and process cell */
  352. memcpy(cell_buf + buf_usage, source, space_left);
  353. source += space_left;
  354. avail_data -= space_left;
  355. usbatm_extract_one_cell(instance, cell_buf);
  356. instance->buf_usage = 0;
  357. } else {
  358. /* not enough data to fill the cell */
  359. memcpy(cell_buf + buf_usage, source, avail_data);
  360. instance->buf_usage = buf_usage + avail_data;
  361. return;
  362. }
  363. }
  364. for (; avail_data >= stride; avail_data -= stride, source += stride)
  365. usbatm_extract_one_cell(instance, source);
  366. if (avail_data > 0) {
  367. /* length was not a multiple of stride -
  368. * save remaining data for next call */
  369. memcpy(instance->cell_buf, source, avail_data);
  370. instance->buf_usage = avail_data;
  371. }
  372. }
  373. /*************
  374. ** encode **
  375. *************/
  376. static unsigned int usbatm_write_cells(struct usbatm_data *instance,
  377. struct sk_buff *skb,
  378. u8 *target, unsigned int avail_space)
  379. {
  380. struct usbatm_control *ctrl = UDSL_SKB(skb);
  381. struct atm_vcc *vcc = ctrl->atm.vcc;
  382. unsigned int bytes_written;
  383. unsigned int stride = instance->tx_channel.stride;
  384. vdbg("%s: skb->len=%d, avail_space=%u", __func__, skb->len, avail_space);
  385. UDSL_ASSERT(!(avail_space % stride));
  386. for (bytes_written = 0; bytes_written < avail_space && ctrl->len;
  387. bytes_written += stride, target += stride) {
  388. unsigned int data_len = min_t(unsigned int, skb->len, ATM_CELL_PAYLOAD);
  389. unsigned int left = ATM_CELL_PAYLOAD - data_len;
  390. u8 *ptr = target;
  391. ptr[0] = vcc->vpi >> 4;
  392. ptr[1] = (vcc->vpi << 4) | (vcc->vci >> 12);
  393. ptr[2] = vcc->vci >> 4;
  394. ptr[3] = vcc->vci << 4;
  395. ptr[4] = 0xec;
  396. ptr += ATM_CELL_HEADER;
  397. skb_copy_from_linear_data(skb, ptr, data_len);
  398. ptr += data_len;
  399. __skb_pull(skb, data_len);
  400. if(!left)
  401. continue;
  402. memset(ptr, 0, left);
  403. if (left >= ATM_AAL5_TRAILER) { /* trailer will go in this cell */
  404. u8 *trailer = target + ATM_CELL_SIZE - ATM_AAL5_TRAILER;
  405. /* trailer[0] = 0; UU = 0 */
  406. /* trailer[1] = 0; CPI = 0 */
  407. trailer[2] = ctrl->len >> 8;
  408. trailer[3] = ctrl->len;
  409. ctrl->crc = ~ crc32_be(ctrl->crc, ptr, left - 4);
  410. trailer[4] = ctrl->crc >> 24;
  411. trailer[5] = ctrl->crc >> 16;
  412. trailer[6] = ctrl->crc >> 8;
  413. trailer[7] = ctrl->crc;
  414. target[3] |= 0x2; /* adjust PTI */
  415. ctrl->len = 0; /* tag this skb finished */
  416. }
  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("%s: processing urb 0x%p", __func__, urb);
  431. if (usb_pipeisoc(urb->pipe)) {
  432. unsigned char *merge_start = NULL;
  433. unsigned int merge_length = 0;
  434. const unsigned int packet_size = instance->rx_channel.packet_size;
  435. int i;
  436. for (i = 0; i < urb->number_of_packets; i++) {
  437. if (!urb->iso_frame_desc[i].status) {
  438. unsigned int actual_length = urb->iso_frame_desc[i].actual_length;
  439. UDSL_ASSERT(actual_length <= packet_size);
  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("%s: wrote %u bytes from skb 0x%p to urb 0x%p",
  492. __func__, bytes_written, skb, urb);
  493. if (!UDSL_SKB(skb)->len) {
  494. struct atm_vcc *vcc = UDSL_SKB(skb)->atm.vcc;
  495. usbatm_pop(vcc, skb);
  496. atomic_inc(&vcc->stats->tx);
  497. skb = skb_dequeue(&instance->sndqueue);
  498. }
  499. if (bytes_written == buf_size || (!skb && bytes_written)) {
  500. urb->transfer_buffer_length = bytes_written;
  501. if (usbatm_submit_urb(urb))
  502. break;
  503. urb = NULL;
  504. }
  505. }
  506. instance->current_skb = skb;
  507. }
  508. static void usbatm_cancel_send(struct usbatm_data *instance,
  509. struct atm_vcc *vcc)
  510. {
  511. struct sk_buff *skb, *n;
  512. atm_dbg(instance, "%s entered\n", __func__);
  513. spin_lock_irq(&instance->sndqueue.lock);
  514. for (skb = instance->sndqueue.next, n = skb->next;
  515. skb != (struct sk_buff *)&instance->sndqueue;
  516. skb = n, n = skb->next)
  517. if (UDSL_SKB(skb)->atm.vcc == vcc) {
  518. atm_dbg(instance, "%s: popping skb 0x%p\n", __func__, skb);
  519. __skb_unlink(skb, &instance->sndqueue);
  520. usbatm_pop(vcc, skb);
  521. }
  522. spin_unlock_irq(&instance->sndqueue.lock);
  523. tasklet_disable(&instance->tx_channel.tasklet);
  524. if ((skb = instance->current_skb) && (UDSL_SKB(skb)->atm.vcc == vcc)) {
  525. atm_dbg(instance, "%s: popping current skb (0x%p)\n", __func__, skb);
  526. instance->current_skb = NULL;
  527. usbatm_pop(vcc, skb);
  528. }
  529. tasklet_enable(&instance->tx_channel.tasklet);
  530. atm_dbg(instance, "%s done\n", __func__);
  531. }
  532. static int usbatm_atm_send(struct atm_vcc *vcc, struct sk_buff *skb)
  533. {
  534. struct usbatm_data *instance = vcc->dev->dev_data;
  535. struct usbatm_control *ctrl = UDSL_SKB(skb);
  536. int err;
  537. vdbg("%s called (skb 0x%p, len %u)", __func__, skb, skb->len);
  538. /* racy disconnection check - fine */
  539. if (!instance || instance->disconnected) {
  540. #ifdef DEBUG
  541. if (printk_ratelimit())
  542. printk(KERN_DEBUG "%s: %s!\n", __func__, instance ? "disconnected" : "NULL instance");
  543. #endif
  544. err = -ENODEV;
  545. goto fail;
  546. }
  547. if (vcc->qos.aal != ATM_AAL5) {
  548. atm_rldbg(instance, "%s: unsupported ATM type %d!\n", __func__, vcc->qos.aal);
  549. err = -EINVAL;
  550. goto fail;
  551. }
  552. if (skb->len > ATM_MAX_AAL5_PDU) {
  553. atm_rldbg(instance, "%s: packet too long (%d vs %d)!\n",
  554. __func__, skb->len, ATM_MAX_AAL5_PDU);
  555. err = -EINVAL;
  556. goto fail;
  557. }
  558. PACKETDEBUG(skb->data, skb->len);
  559. /* initialize the control block */
  560. ctrl->atm.vcc = vcc;
  561. ctrl->len = skb->len;
  562. ctrl->crc = crc32_be(~0, skb->data, skb->len);
  563. skb_queue_tail(&instance->sndqueue, skb);
  564. tasklet_schedule(&instance->tx_channel.tasklet);
  565. return 0;
  566. fail:
  567. usbatm_pop(vcc, skb);
  568. return err;
  569. }
  570. /********************
  571. ** bean counting **
  572. ********************/
  573. static void usbatm_destroy_instance(struct kref *kref)
  574. {
  575. struct usbatm_data *instance = container_of(kref, struct usbatm_data, refcount);
  576. dbg("%s", __func__);
  577. tasklet_kill(&instance->rx_channel.tasklet);
  578. tasklet_kill(&instance->tx_channel.tasklet);
  579. usb_put_dev(instance->usb_dev);
  580. kfree(instance);
  581. }
  582. static void usbatm_get_instance(struct usbatm_data *instance)
  583. {
  584. dbg("%s", __func__);
  585. kref_get(&instance->refcount);
  586. }
  587. static void usbatm_put_instance(struct usbatm_data *instance)
  588. {
  589. dbg("%s", __func__);
  590. kref_put(&instance->refcount, usbatm_destroy_instance);
  591. }
  592. /**********
  593. ** ATM **
  594. **********/
  595. static void usbatm_atm_dev_close(struct atm_dev *atm_dev)
  596. {
  597. struct usbatm_data *instance = atm_dev->dev_data;
  598. dbg("%s", __func__);
  599. if (!instance)
  600. return;
  601. atm_dev->dev_data = NULL; /* catch bugs */
  602. usbatm_put_instance(instance); /* taken in usbatm_atm_init */
  603. }
  604. static int usbatm_atm_proc_read(struct atm_dev *atm_dev, loff_t * pos, char *page)
  605. {
  606. struct usbatm_data *instance = atm_dev->dev_data;
  607. int left = *pos;
  608. if (!instance) {
  609. dbg("%s: NULL instance!", __func__);
  610. return -ENODEV;
  611. }
  612. if (!left--)
  613. return sprintf(page, "%s\n", instance->description);
  614. if (!left--)
  615. return sprintf(page, "MAC: %02x:%02x:%02x:%02x:%02x:%02x\n",
  616. atm_dev->esi[0], atm_dev->esi[1],
  617. atm_dev->esi[2], atm_dev->esi[3],
  618. atm_dev->esi[4], atm_dev->esi[5]);
  619. if (!left--)
  620. return sprintf(page,
  621. "AAL5: tx %d ( %d err ), rx %d ( %d err, %d drop )\n",
  622. atomic_read(&atm_dev->stats.aal5.tx),
  623. atomic_read(&atm_dev->stats.aal5.tx_err),
  624. atomic_read(&atm_dev->stats.aal5.rx),
  625. atomic_read(&atm_dev->stats.aal5.rx_err),
  626. atomic_read(&atm_dev->stats.aal5.rx_drop));
  627. if (!left--) {
  628. if (instance->disconnected)
  629. return sprintf(page, "Disconnected\n");
  630. else
  631. switch (atm_dev->signal) {
  632. case ATM_PHY_SIG_FOUND:
  633. return sprintf(page, "Line up\n");
  634. case ATM_PHY_SIG_LOST:
  635. return sprintf(page, "Line down\n");
  636. default:
  637. return sprintf(page, "Line state unknown\n");
  638. }
  639. }
  640. return 0;
  641. }
  642. static int usbatm_atm_open(struct atm_vcc *vcc)
  643. {
  644. struct usbatm_data *instance = vcc->dev->dev_data;
  645. struct usbatm_vcc_data *new = NULL;
  646. int ret;
  647. int vci = vcc->vci;
  648. short vpi = vcc->vpi;
  649. if (!instance) {
  650. dbg("%s: NULL data!", __func__);
  651. return -ENODEV;
  652. }
  653. atm_dbg(instance, "%s: vpi %hd, vci %d\n", __func__, vpi, vci);
  654. /* only support AAL5 */
  655. if ((vcc->qos.aal != ATM_AAL5)) {
  656. atm_warn(instance, "%s: unsupported ATM type %d!\n", __func__, vcc->qos.aal);
  657. return -EINVAL;
  658. }
  659. /* sanity checks */
  660. if ((vcc->qos.rxtp.max_sdu < 0) || (vcc->qos.rxtp.max_sdu > ATM_MAX_AAL5_PDU)) {
  661. atm_dbg(instance, "%s: max_sdu %d out of range!\n", __func__, vcc->qos.rxtp.max_sdu);
  662. return -EINVAL;
  663. }
  664. mutex_lock(&instance->serialize); /* vs self, usbatm_atm_close, usbatm_usb_disconnect */
  665. if (instance->disconnected) {
  666. atm_dbg(instance, "%s: disconnected!\n", __func__);
  667. ret = -ENODEV;
  668. goto fail;
  669. }
  670. if (usbatm_find_vcc(instance, vpi, vci)) {
  671. atm_dbg(instance, "%s: %hd/%d already in use!\n", __func__, vpi, vci);
  672. ret = -EADDRINUSE;
  673. goto fail;
  674. }
  675. if (!(new = kzalloc(sizeof(struct usbatm_vcc_data), GFP_KERNEL))) {
  676. atm_err(instance, "%s: no memory for vcc_data!\n", __func__);
  677. ret = -ENOMEM;
  678. goto fail;
  679. }
  680. new->vcc = vcc;
  681. new->vpi = vpi;
  682. new->vci = vci;
  683. new->sarb = alloc_skb(usbatm_pdu_length(vcc->qos.rxtp.max_sdu), GFP_KERNEL);
  684. if (!new->sarb) {
  685. atm_err(instance, "%s: no memory for SAR buffer!\n", __func__);
  686. ret = -ENOMEM;
  687. goto fail;
  688. }
  689. vcc->dev_data = new;
  690. tasklet_disable(&instance->rx_channel.tasklet);
  691. instance->cached_vcc = new;
  692. instance->cached_vpi = vpi;
  693. instance->cached_vci = vci;
  694. list_add(&new->list, &instance->vcc_list);
  695. tasklet_enable(&instance->rx_channel.tasklet);
  696. set_bit(ATM_VF_ADDR, &vcc->flags);
  697. set_bit(ATM_VF_PARTIAL, &vcc->flags);
  698. set_bit(ATM_VF_READY, &vcc->flags);
  699. mutex_unlock(&instance->serialize);
  700. atm_dbg(instance, "%s: allocated vcc data 0x%p\n", __func__, new);
  701. return 0;
  702. fail:
  703. kfree(new);
  704. mutex_unlock(&instance->serialize);
  705. return ret;
  706. }
  707. static void usbatm_atm_close(struct atm_vcc *vcc)
  708. {
  709. struct usbatm_data *instance = vcc->dev->dev_data;
  710. struct usbatm_vcc_data *vcc_data = vcc->dev_data;
  711. if (!instance || !vcc_data) {
  712. dbg("%s: NULL data!", __func__);
  713. return;
  714. }
  715. atm_dbg(instance, "%s entered\n", __func__);
  716. atm_dbg(instance, "%s: deallocating vcc 0x%p with vpi %d vci %d\n",
  717. __func__, vcc_data, vcc_data->vpi, vcc_data->vci);
  718. usbatm_cancel_send(instance, vcc);
  719. mutex_lock(&instance->serialize); /* vs self, usbatm_atm_open, usbatm_usb_disconnect */
  720. tasklet_disable(&instance->rx_channel.tasklet);
  721. if (instance->cached_vcc == vcc_data) {
  722. instance->cached_vcc = NULL;
  723. instance->cached_vpi = ATM_VPI_UNSPEC;
  724. instance->cached_vci = ATM_VCI_UNSPEC;
  725. }
  726. list_del(&vcc_data->list);
  727. tasklet_enable(&instance->rx_channel.tasklet);
  728. kfree_skb(vcc_data->sarb);
  729. vcc_data->sarb = NULL;
  730. kfree(vcc_data);
  731. vcc->dev_data = NULL;
  732. vcc->vpi = ATM_VPI_UNSPEC;
  733. vcc->vci = ATM_VCI_UNSPEC;
  734. clear_bit(ATM_VF_READY, &vcc->flags);
  735. clear_bit(ATM_VF_PARTIAL, &vcc->flags);
  736. clear_bit(ATM_VF_ADDR, &vcc->flags);
  737. mutex_unlock(&instance->serialize);
  738. atm_dbg(instance, "%s successful\n", __func__);
  739. }
  740. static int usbatm_atm_ioctl(struct atm_dev *atm_dev, unsigned int cmd,
  741. void __user * arg)
  742. {
  743. struct usbatm_data *instance = atm_dev->dev_data;
  744. if (!instance || instance->disconnected) {
  745. dbg("%s: %s!", __func__, instance ? "disconnected" : "NULL instance");
  746. return -ENODEV;
  747. }
  748. switch (cmd) {
  749. case ATM_QUERYLOOP:
  750. return put_user(ATM_LM_NONE, (int __user *)arg) ? -EFAULT : 0;
  751. default:
  752. return -ENOIOCTLCMD;
  753. }
  754. }
  755. static int usbatm_atm_init(struct usbatm_data *instance)
  756. {
  757. struct atm_dev *atm_dev;
  758. int ret, i;
  759. /* ATM init. The ATM initialization scheme suffers from an intrinsic race
  760. * condition: callbacks we register can be executed at once, before we have
  761. * initialized the struct atm_dev. To protect against this, all callbacks
  762. * abort if atm_dev->dev_data is NULL. */
  763. atm_dev = atm_dev_register(instance->driver_name, &usbatm_atm_devops, -1, NULL);
  764. if (!atm_dev) {
  765. usb_err(instance, "%s: failed to register ATM device!\n", __func__);
  766. return -1;
  767. }
  768. instance->atm_dev = atm_dev;
  769. atm_dev->ci_range.vpi_bits = ATM_CI_MAX;
  770. atm_dev->ci_range.vci_bits = ATM_CI_MAX;
  771. atm_dev->signal = ATM_PHY_SIG_UNKNOWN;
  772. /* temp init ATM device, set to 128kbit */
  773. atm_dev->link_rate = 128 * 1000 / 424;
  774. ret = sysfs_create_link(&atm_dev->class_dev.kobj,
  775. &instance->usb_intf->dev.kobj, "device");
  776. if (ret) {
  777. atm_err(instance, "%s: sysfs_create_link failed: %d\n",
  778. __func__, ret);
  779. goto fail_sysfs;
  780. }
  781. if (instance->driver->atm_start && ((ret = instance->driver->atm_start(instance, atm_dev)) < 0)) {
  782. atm_err(instance, "%s: atm_start failed: %d!\n", __func__, ret);
  783. goto fail;
  784. }
  785. usbatm_get_instance(instance); /* dropped in usbatm_atm_dev_close */
  786. /* ready for ATM callbacks */
  787. mb();
  788. atm_dev->dev_data = instance;
  789. /* submit all rx URBs */
  790. for (i = 0; i < num_rcv_urbs; i++)
  791. usbatm_submit_urb(instance->urbs[i]);
  792. return 0;
  793. fail:
  794. sysfs_remove_link(&atm_dev->class_dev.kobj, "device");
  795. fail_sysfs:
  796. instance->atm_dev = NULL;
  797. atm_dev_deregister(atm_dev); /* usbatm_atm_dev_close will eventually be called */
  798. return ret;
  799. }
  800. /**********
  801. ** USB **
  802. **********/
  803. static int usbatm_do_heavy_init(void *arg)
  804. {
  805. struct usbatm_data *instance = arg;
  806. int ret;
  807. daemonize(instance->driver->driver_name);
  808. allow_signal(SIGTERM);
  809. instance->thread_pid = current->pid;
  810. complete(&instance->thread_started);
  811. ret = instance->driver->heavy_init(instance, instance->usb_intf);
  812. if (!ret)
  813. ret = usbatm_atm_init(instance);
  814. mutex_lock(&instance->serialize);
  815. instance->thread_pid = -1;
  816. mutex_unlock(&instance->serialize);
  817. complete_and_exit(&instance->thread_exited, ret);
  818. }
  819. static int usbatm_heavy_init(struct usbatm_data *instance)
  820. {
  821. int ret = kernel_thread(usbatm_do_heavy_init, instance, CLONE_FS | CLONE_FILES);
  822. if (ret < 0) {
  823. usb_err(instance, "%s: failed to create kernel_thread (%d)!\n", __func__, ret);
  824. return ret;
  825. }
  826. wait_for_completion(&instance->thread_started);
  827. return 0;
  828. }
  829. static void usbatm_tasklet_schedule(unsigned long data)
  830. {
  831. tasklet_schedule((struct tasklet_struct *) data);
  832. }
  833. static void usbatm_init_channel(struct usbatm_channel *channel)
  834. {
  835. spin_lock_init(&channel->lock);
  836. INIT_LIST_HEAD(&channel->list);
  837. channel->delay.function = usbatm_tasklet_schedule;
  838. channel->delay.data = (unsigned long) &channel->tasklet;
  839. init_timer(&channel->delay);
  840. }
  841. int usbatm_usb_probe(struct usb_interface *intf, const struct usb_device_id *id,
  842. struct usbatm_driver *driver)
  843. {
  844. struct device *dev = &intf->dev;
  845. struct usb_device *usb_dev = interface_to_usbdev(intf);
  846. struct usbatm_data *instance;
  847. char *buf;
  848. int error = -ENOMEM;
  849. int i, length;
  850. unsigned int maxpacket, num_packets;
  851. dev_dbg(dev, "%s: trying driver %s with vendor=%04x, product=%04x, ifnum %2d\n",
  852. __func__, driver->driver_name,
  853. le16_to_cpu(usb_dev->descriptor.idVendor),
  854. le16_to_cpu(usb_dev->descriptor.idProduct),
  855. intf->altsetting->desc.bInterfaceNumber);
  856. /* instance init */
  857. instance = kzalloc(sizeof(*instance) + sizeof(struct urb *) * (num_rcv_urbs + num_snd_urbs), GFP_KERNEL);
  858. if (!instance) {
  859. dev_err(dev, "%s: no memory for instance data!\n", __func__);
  860. return -ENOMEM;
  861. }
  862. /* public fields */
  863. instance->driver = driver;
  864. snprintf(instance->driver_name, sizeof(instance->driver_name), driver->driver_name);
  865. instance->usb_dev = usb_dev;
  866. instance->usb_intf = intf;
  867. buf = instance->description;
  868. length = sizeof(instance->description);
  869. if ((i = usb_string(usb_dev, usb_dev->descriptor.iProduct, buf, length)) < 0)
  870. goto bind;
  871. buf += i;
  872. length -= i;
  873. i = scnprintf(buf, length, " (");
  874. buf += i;
  875. length -= i;
  876. if (length <= 0 || (i = usb_make_path(usb_dev, buf, length)) < 0)
  877. goto bind;
  878. buf += i;
  879. length -= i;
  880. snprintf(buf, length, ")");
  881. bind:
  882. if (driver->bind && (error = driver->bind(instance, intf, id)) < 0) {
  883. dev_err(dev, "%s: bind failed: %d!\n", __func__, error);
  884. goto fail_free;
  885. }
  886. /* private fields */
  887. kref_init(&instance->refcount); /* dropped in usbatm_usb_disconnect */
  888. mutex_init(&instance->serialize);
  889. instance->thread_pid = -1;
  890. init_completion(&instance->thread_started);
  891. init_completion(&instance->thread_exited);
  892. INIT_LIST_HEAD(&instance->vcc_list);
  893. skb_queue_head_init(&instance->sndqueue);
  894. usbatm_init_channel(&instance->rx_channel);
  895. usbatm_init_channel(&instance->tx_channel);
  896. tasklet_init(&instance->rx_channel.tasklet, usbatm_rx_process, (unsigned long)instance);
  897. tasklet_init(&instance->tx_channel.tasklet, usbatm_tx_process, (unsigned long)instance);
  898. instance->rx_channel.stride = ATM_CELL_SIZE + driver->rx_padding;
  899. instance->tx_channel.stride = ATM_CELL_SIZE + driver->tx_padding;
  900. instance->rx_channel.usbatm = instance->tx_channel.usbatm = instance;
  901. if ((instance->flags & UDSL_USE_ISOC) && driver->isoc_in)
  902. instance->rx_channel.endpoint = usb_rcvisocpipe(usb_dev, driver->isoc_in);
  903. else
  904. instance->rx_channel.endpoint = usb_rcvbulkpipe(usb_dev, driver->bulk_in);
  905. instance->tx_channel.endpoint = usb_sndbulkpipe(usb_dev, driver->bulk_out);
  906. /* tx buffer size must be a positive multiple of the stride */
  907. instance->tx_channel.buf_size = max (instance->tx_channel.stride,
  908. snd_buf_bytes - (snd_buf_bytes % instance->tx_channel.stride));
  909. /* rx buffer size must be a positive multiple of the endpoint maxpacket */
  910. maxpacket = usb_maxpacket(usb_dev, instance->rx_channel.endpoint, 0);
  911. if ((maxpacket < 1) || (maxpacket > UDSL_MAX_BUF_SIZE)) {
  912. dev_err(dev, "%s: invalid endpoint %02x!\n", __func__,
  913. usb_pipeendpoint(instance->rx_channel.endpoint));
  914. error = -EINVAL;
  915. goto fail_unbind;
  916. }
  917. num_packets = max (1U, (rcv_buf_bytes + maxpacket / 2) / maxpacket); /* round */
  918. if (num_packets * maxpacket > UDSL_MAX_BUF_SIZE)
  919. num_packets--;
  920. instance->rx_channel.buf_size = num_packets * maxpacket;
  921. instance->rx_channel.packet_size = maxpacket;
  922. #ifdef DEBUG
  923. for (i = 0; i < 2; i++) {
  924. struct usbatm_channel *channel = i ?
  925. &instance->tx_channel : &instance->rx_channel;
  926. dev_dbg(dev, "%s: using %d byte buffer for %s channel 0x%p\n", __func__, channel->buf_size, i ? "tx" : "rx", channel);
  927. }
  928. #endif
  929. /* initialize urbs */
  930. for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++) {
  931. u8 *buffer;
  932. struct usbatm_channel *channel = i < num_rcv_urbs ?
  933. &instance->rx_channel : &instance->tx_channel;
  934. struct urb *urb;
  935. unsigned int iso_packets = usb_pipeisoc(channel->endpoint) ? channel->buf_size / channel->packet_size : 0;
  936. UDSL_ASSERT(!usb_pipeisoc(channel->endpoint) || usb_pipein(channel->endpoint));
  937. urb = usb_alloc_urb(iso_packets, GFP_KERNEL);
  938. if (!urb) {
  939. dev_err(dev, "%s: no memory for urb %d!\n", __func__, i);
  940. error = -ENOMEM;
  941. goto fail_unbind;
  942. }
  943. instance->urbs[i] = urb;
  944. /* zero the tx padding to avoid leaking information */
  945. buffer = kzalloc(channel->buf_size, GFP_KERNEL);
  946. if (!buffer) {
  947. dev_err(dev, "%s: no memory for buffer %d!\n", __func__, i);
  948. error = -ENOMEM;
  949. goto fail_unbind;
  950. }
  951. usb_fill_bulk_urb(urb, instance->usb_dev, channel->endpoint,
  952. buffer, channel->buf_size, usbatm_complete, channel);
  953. if (iso_packets) {
  954. int j;
  955. urb->interval = 1;
  956. urb->transfer_flags = URB_ISO_ASAP;
  957. urb->number_of_packets = iso_packets;
  958. for (j = 0; j < iso_packets; j++) {
  959. urb->iso_frame_desc[j].offset = channel->packet_size * j;
  960. urb->iso_frame_desc[j].length = channel->packet_size;
  961. }
  962. }
  963. /* put all tx URBs on the list of spares */
  964. if (i >= num_rcv_urbs)
  965. list_add_tail(&urb->urb_list, &channel->list);
  966. vdbg("%s: alloced buffer 0x%p buf size %u urb 0x%p",
  967. __func__, urb->transfer_buffer, urb->transfer_buffer_length, urb);
  968. }
  969. instance->cached_vpi = ATM_VPI_UNSPEC;
  970. instance->cached_vci = ATM_VCI_UNSPEC;
  971. instance->cell_buf = kmalloc(instance->rx_channel.stride, GFP_KERNEL);
  972. if (!instance->cell_buf) {
  973. dev_err(dev, "%s: no memory for cell buffer!\n", __func__);
  974. error = -ENOMEM;
  975. goto fail_unbind;
  976. }
  977. if (!(instance->flags & UDSL_SKIP_HEAVY_INIT) && driver->heavy_init) {
  978. error = usbatm_heavy_init(instance);
  979. } else {
  980. complete(&instance->thread_exited); /* pretend that heavy_init was run */
  981. error = usbatm_atm_init(instance);
  982. }
  983. if (error < 0)
  984. goto fail_unbind;
  985. usb_get_dev(usb_dev);
  986. usb_set_intfdata(intf, instance);
  987. return 0;
  988. fail_unbind:
  989. if (instance->driver->unbind)
  990. instance->driver->unbind(instance, intf);
  991. fail_free:
  992. kfree(instance->cell_buf);
  993. for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++) {
  994. if (instance->urbs[i])
  995. kfree(instance->urbs[i]->transfer_buffer);
  996. usb_free_urb(instance->urbs[i]);
  997. }
  998. kfree (instance);
  999. return error;
  1000. }
  1001. EXPORT_SYMBOL_GPL(usbatm_usb_probe);
  1002. void usbatm_usb_disconnect(struct usb_interface *intf)
  1003. {
  1004. struct device *dev = &intf->dev;
  1005. struct usbatm_data *instance = usb_get_intfdata(intf);
  1006. struct usbatm_vcc_data *vcc_data;
  1007. int i;
  1008. dev_dbg(dev, "%s entered\n", __func__);
  1009. if (!instance) {
  1010. dev_dbg(dev, "%s: NULL instance!\n", __func__);
  1011. return;
  1012. }
  1013. usb_set_intfdata(intf, NULL);
  1014. mutex_lock(&instance->serialize);
  1015. instance->disconnected = 1;
  1016. if (instance->thread_pid >= 0)
  1017. kill_proc(instance->thread_pid, SIGTERM, 1);
  1018. mutex_unlock(&instance->serialize);
  1019. wait_for_completion(&instance->thread_exited);
  1020. mutex_lock(&instance->serialize);
  1021. list_for_each_entry(vcc_data, &instance->vcc_list, list)
  1022. vcc_release_async(vcc_data->vcc, -EPIPE);
  1023. mutex_unlock(&instance->serialize);
  1024. tasklet_disable(&instance->rx_channel.tasklet);
  1025. tasklet_disable(&instance->tx_channel.tasklet);
  1026. for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++)
  1027. usb_kill_urb(instance->urbs[i]);
  1028. del_timer_sync(&instance->rx_channel.delay);
  1029. del_timer_sync(&instance->tx_channel.delay);
  1030. /* turn usbatm_[rt]x_process into something close to a no-op */
  1031. /* no need to take the spinlock */
  1032. INIT_LIST_HEAD(&instance->rx_channel.list);
  1033. INIT_LIST_HEAD(&instance->tx_channel.list);
  1034. tasklet_enable(&instance->rx_channel.tasklet);
  1035. tasklet_enable(&instance->tx_channel.tasklet);
  1036. if (instance->atm_dev && instance->driver->atm_stop)
  1037. instance->driver->atm_stop(instance, instance->atm_dev);
  1038. if (instance->driver->unbind)
  1039. instance->driver->unbind(instance, intf);
  1040. instance->driver_data = NULL;
  1041. for (i = 0; i < num_rcv_urbs + num_snd_urbs; i++) {
  1042. kfree(instance->urbs[i]->transfer_buffer);
  1043. usb_free_urb(instance->urbs[i]);
  1044. }
  1045. kfree(instance->cell_buf);
  1046. /* ATM finalize */
  1047. if (instance->atm_dev) {
  1048. sysfs_remove_link(&instance->atm_dev->class_dev.kobj, "device");
  1049. atm_dev_deregister(instance->atm_dev);
  1050. }
  1051. usbatm_put_instance(instance); /* taken in usbatm_usb_probe */
  1052. }
  1053. EXPORT_SYMBOL_GPL(usbatm_usb_disconnect);
  1054. /***********
  1055. ** init **
  1056. ***********/
  1057. static int __init usbatm_usb_init(void)
  1058. {
  1059. dbg("%s: driver version %s", __func__, DRIVER_VERSION);
  1060. if (sizeof(struct usbatm_control) > sizeof(((struct sk_buff *) 0)->cb)) {
  1061. printk(KERN_ERR "%s unusable with this kernel!\n", usbatm_driver_name);
  1062. return -EIO;
  1063. }
  1064. if ((num_rcv_urbs > UDSL_MAX_RCV_URBS)
  1065. || (num_snd_urbs > UDSL_MAX_SND_URBS)
  1066. || (rcv_buf_bytes < 1)
  1067. || (rcv_buf_bytes > UDSL_MAX_BUF_SIZE)
  1068. || (snd_buf_bytes < 1)
  1069. || (snd_buf_bytes > UDSL_MAX_BUF_SIZE))
  1070. return -EINVAL;
  1071. return 0;
  1072. }
  1073. module_init(usbatm_usb_init);
  1074. static void __exit usbatm_usb_exit(void)
  1075. {
  1076. dbg("%s", __func__);
  1077. }
  1078. module_exit(usbatm_usb_exit);
  1079. MODULE_AUTHOR(DRIVER_AUTHOR);
  1080. MODULE_DESCRIPTION(DRIVER_DESC);
  1081. MODULE_LICENSE("GPL");
  1082. MODULE_VERSION(DRIVER_VERSION);
  1083. /************
  1084. ** debug **
  1085. ************/
  1086. #ifdef VERBOSE_DEBUG
  1087. static int usbatm_print_packet(const unsigned char *data, int len)
  1088. {
  1089. unsigned char buffer[256];
  1090. int i = 0, j = 0;
  1091. for (i = 0; i < len;) {
  1092. buffer[0] = '\0';
  1093. sprintf(buffer, "%.3d :", i);
  1094. for (j = 0; (j < 16) && (i < len); j++, i++) {
  1095. sprintf(buffer, "%s %2.2x", buffer, data[i]);
  1096. }
  1097. dbg("%s", buffer);
  1098. }
  1099. return i;
  1100. }
  1101. #endif